forked from cgeyer/llvm-cbg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cbgPredicationPass.cpp
1921 lines (1642 loc) · 68.6 KB
/
cbgPredicationPass.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* @file cbgPredicationPass.cpp
*
* @date 2011-11-14
* @author Clemens Bernhard Geyer
*
* This file is distributed under the University of Illinois Open Source
* License. See LICENSE.TXT for details.
*
*/
#include "cbg.h"
#include "cbgInstrInfo.h"
#include "llvm/CodeGen/MachineBasicBlock.h"
#include "llvm/CodeGen/MachineFunctionPass.h"
#include "llvm/CodeGen/MachineInstrBuilder.h"
#include "llvm/CodeGen/MachineOperand.h"
#include <vector>
#include <list>
#include <iostream>
using namespace llvm;
namespace {
/**
* @brief Abstract basic class for all predication passes.
* Provides Methods which are required in all predication passes.
*/
class PredicationPass : public MachineFunctionPass {
public:
typedef enum {
IFELSEBRANCH,
IFBRANCH,
NOBRANCH
} branchType;
static char ID;
protected:
TargetMachine &TM;
static int getConditionCode(MachineBasicBlock &MBB);
static branchType getBranchType(MachineBasicBlock &TBB, MachineBasicBlock &FBB);
virtual void removeBranches(MachineBasicBlock &MBB);
virtual void mergeBlocks(MachineBasicBlock &Pre, MachineBasicBlock &Post);
struct BranchMBB {
MachineBasicBlock* TBB;
MachineBasicBlock* FBB;
branchType btype;
};
public:
typedef std::list<BranchMBB> BranchMBB_list;
explicit PredicationPass(TargetMachine &tm) : MachineFunctionPass(ID), TM(tm) {}
virtual bool runOnMachineFunction(MachineFunction &F) = 0;
virtual const char *getPassName() const {
return "CBG predication pass";
}
};
char PredicationPass::ID = 0;
/**
* @brief Pass to insert predicated blocks based on condition
* codes of the status register.
*/
class PredicatedBlocksCCPass : public PredicationPass {
protected:
virtual bool definesICC(MachineBasicBlock &MBB);
virtual bool insertPredicatedBlock(MachineBasicBlock &TBB, MachineBasicBlock &FBB, branchType btype);
public:
explicit PredicatedBlocksCCPass(TargetMachine &tm) : PredicationPass(tm) {}
virtual bool runOnMachineFunction(MachineFunction &F);
};
/**
* @brief Abstract basic class which provides methods for all
* predication passes using predication registers.
*/
class PredicatedRegPass : public PredicationPass {
public:
typedef std::list<unsigned> RegList;
protected:
virtual unsigned getNextFreePredRegister(MachineBasicBlock &MBB);
virtual unsigned getNextFreePredRegister(MachineBasicBlock &TBB, MachineBasicBlock &FBB);
virtual bool isPredRegInList(RegList &rList, unsigned RegNo);
virtual RegList getUsedPredRegisters(MachineBasicBlock &MBB);
virtual RegList getUsedPredRegisters(MachineBasicBlock &TBB, MachineBasicBlock &FBB);
virtual RegList cutPredRegLists(RegList &rList1, RegList &rList2);
virtual RegList unitePredRegLists(RegList &rList1, RegList &rList2);
virtual RegList removePredRegClears(MachineBasicBlock &MBB);
public:
explicit PredicatedRegPass(TargetMachine &tm) : PredicationPass(tm) {}
virtual bool runOnMachineFunction(MachineFunction &F) = 0;
};
/**
* @brief Pass which implements predicated blocks based on predication registers.
*/
class PredicatedBlocksRegPass : public PredicatedRegPass {
protected:
virtual void replacePredEnds(MachineBasicBlock &MBB, unsigned PReg, unsigned TF);
virtual void removePredEnds(MachineBasicBlock &MBB);
virtual bool insertPredicatedBlock(MachineBasicBlock &TBB, MachineBasicBlock &FBB, branchType btype);
public:
explicit PredicatedBlocksRegPass(TargetMachine &tm) : PredicatedRegPass(tm) {}
virtual bool runOnMachineFunction(MachineFunction &F);
};
/**
* @brief Pass which implements a fully predicated instruction set based on
* condition codes of the status register.
*/
class PredicatedInstrCCPass : public PredicatedBlocksCCPass {
protected:
virtual void changeToPredicatedInstruction(MachineBasicBlock &Pre,
MachineBasicBlock &MBB,
unsigned ConditionCode);
virtual bool insertPredicatedBlock(MachineBasicBlock &TBB, MachineBasicBlock &FBB, branchType btype);
public:
explicit PredicatedInstrCCPass(TargetMachine &tm) : PredicatedBlocksCCPass(tm) {}
};
/**
* @brief Pass which implements a fully predicated instruction set based on
* predication registers.
*/
class PredicatedInstrRegPass : public PredicatedRegPass {
protected:
virtual void changeToPredicatedInstruction(MachineBasicBlock &Pre,
MachineBasicBlock &MBB,
unsigned PReg,
unsigned TF);
virtual bool insertPredicatedBlock(MachineBasicBlock &TBB, MachineBasicBlock &FBB, branchType btype);
public:
explicit PredicatedInstrRegPass(TargetMachine &tm) : PredicatedRegPass(tm) {}
virtual bool runOnMachineFunction(MachineFunction &F);
};
}
/**
* @brief Returns the condition code of the last conditional branch of
* a machine basic block.
* @param MBB The Machine basic block to analyze.
* @return Either the condition code of the last conditional branch
* or -1 if there is no conditional branch.
*/
int PredicationPass::getConditionCode(MachineBasicBlock &MBB) {
MachineBasicBlock::reverse_iterator mbb_iter;
for (mbb_iter = MBB.rbegin(); mbb_iter != MBB.rend(); ++mbb_iter) {
if (mbb_iter->getOpcode() == CBG::BCOND) {
return mbb_iter->getOperand(1).getImm();
}
}
return -1;
}
/**
* @brief Analyzes whether to given machine basic blocks may be part of
* of an if-then-else construction.
* @param TBB Represents the "if-then" branch.
* @param FBB Represents the "else" branch in the case of an IFELSEBRANCH or
* the machine basic block logically following TBB in the case of
* an IFBRANCH.
* @return NOBRANCH in the case the machine basic blocks cannot be analyzed,
* IFELSEBRANCH in the case both basic blocks conform to an if-then-else
* graph, or IFBRANCH in the case that TBB is an if-branch and FBB is
* the (logically) following basic block.
*/
PredicationPass::branchType PredicationPass::getBranchType(MachineBasicBlock &TBB, MachineBasicBlock &FBB) {
branchType returnValue = NOBRANCH;
MachineBasicBlock::pred_iterator mbb_iter;
if (&TBB == &FBB) {
returnValue = NOBRANCH;
} // if-else-branch ? => both only have one predecessor
else if (TBB.pred_size() == 1 && FBB.pred_size() == 1) {
// both have the same predecessor
if (*(TBB.pred_begin()) == *(FBB.pred_begin())) {
// both have only one successor
if (TBB.succ_size() == 1 && FBB.succ_size() == 1) {
// both have the same successor
if (*(TBB.succ_begin()) == *(FBB.succ_begin())) {
// we assume the following layout: Pre -> FBB -> TBB
if ((*(TBB.pred_begin()))->isLayoutSuccessor(&FBB) &&
FBB.isLayoutSuccessor(&TBB))
returnValue = IFELSEBRANCH;
}
}
}
} else if (TBB.pred_size() == 1 &&
(*TBB.pred_begin())->isLayoutSuccessor(&TBB)) {
// only if-branch: The (single) predecessor of TBB also has
// to be the layout predecessor of TBB.
if (TBB.succ_size() == 1 && TBB.isSuccessor(&FBB)) {
// TBB must only have one successor and this
// successor has to be FBB.
if ((*(TBB.pred_begin()))->isSuccessor(&FBB)) {
// FBB and TBB must have the same predecessor, but
// FBB does not need to be the layout successor of
// TBB.
returnValue = IFBRANCH;
}
}
}
return returnValue;
}
/**
* @brief Appends the second machine basic block to the first one and
* renumbers all basic blocks of the current function.
* @param Pre Basic block which finally holds the instructions of both basic
* blocks.
* @param Post The basic block which will finally removed.
*/
void PredicationPass::mergeBlocks(MachineBasicBlock &Pre, MachineBasicBlock &Post) {
MachineBasicBlock::iterator mbb_iter;
MachineBasicBlock::succ_iterator succ_iter;
MachineFunction* F = Pre.getParent();
// add all instructions of TBB to Pre basic block
mbb_iter = Post.begin();
while (mbb_iter != Post.end()) {
MachineInstr* MI = mbb_iter->removeFromParent();
Pre.push_back(MI);
mbb_iter = Post.begin();
}
// removing all (old) successors from pre
succ_iter = Pre.succ_begin();
while (succ_iter != Pre.succ_end()) {
Pre.removeSuccessor(*succ_iter);
succ_iter = Pre.succ_begin();
}
Pre.transferSuccessors(&Post);
// removing Post
Post.eraseFromParent();
F->RenumberBlocks(&Pre);
}
/**
* @brief Removes all conditional and unconditional branches of the given
* machine basic block.
* @param MBB The machine basic block from which all branches will be removed.
*/
void PredicationPass::removeBranches(MachineBasicBlock &MBB) {
MachineBasicBlock::reverse_iterator mbb_iter;
std::list<MachineInstr*> branchesToRemove;
std::list<MachineInstr*>::iterator branch_iter;
// save all branches in list
for (mbb_iter = MBB.rbegin(); mbb_iter != MBB.rend(); ++mbb_iter) {
if (mbb_iter->getOpcode() == CBG::BCOND || mbb_iter->getOpcode() == CBG::BA) {
branchesToRemove.push_front(&(*mbb_iter));
}
}
// remove all branches from basic block
for (branch_iter = branchesToRemove.begin();
branch_iter != branchesToRemove.end();
++branch_iter) {
(*branch_iter)->eraseFromParent();
}
}
/**
* @brief Analyzes whether the given register has already been saved in the
* given list.
* @param rList List of registers which are represented by unsigned numbers.
* @param RegNo Register number of the register to be tested.
* @return True if the given register is part of the list, false otherwise.
*/
bool PredicatedRegPass::isPredRegInList(RegList &rList, unsigned RegNo) {
RegList::iterator reg_iter;
bool inList = false;
for (reg_iter = rList.begin(); reg_iter != rList.end(); ++reg_iter) {
if (*reg_iter == RegNo) {
inList = true;
break;
}
}
return inList;
}
/**
* @brief Analyzes the used predication registers of a given machine basic block.
* @param MBB The machine basic block to analyze.
* @return A set (i.e. a list) of used predication registers.
*/
PredicatedRegPass::RegList PredicatedRegPass::getUsedPredRegisters(MachineBasicBlock &MBB) {
RegList rList;
MachineBasicBlock::iterator mbb_iter;
unsigned OpCode;
unsigned RegNo;
for (mbb_iter = MBB.begin(); mbb_iter != MBB.end(); ++mbb_iter) {
OpCode = mbb_iter->getOpcode();
// add all used registers to list
if (OpCode == CBG::PREDREGSETCC || OpCode == CBG::PREDREGSET || OpCode == CBG::PREDREGCLEAR ||
OpCode == CBG::PREDREGSETCC_PREG) {
RegNo = mbb_iter->getOperand(0).getReg();
if (!isPredRegInList(rList, RegNo)) {
rList.push_back(RegNo);
}
}
}
return rList;
}
/**
* @brief Analyzes the used predication registers of two given machine basic blocks.
* @param TBB The first machine basic block to analyze.
* @param FBB The first machine basic block to analyze.
* @return A set (i.e. a list) of used predication registers.
*/
PredicatedRegPass::RegList PredicatedRegPass::getUsedPredRegisters(MachineBasicBlock &TBB, MachineBasicBlock &FBB) {
RegList rList;
MachineBasicBlock::iterator mbb_iter;
unsigned OpCode;
unsigned RegNo;
for (mbb_iter = TBB.begin(); mbb_iter != TBB.end(); ++mbb_iter) {
OpCode = mbb_iter->getOpcode();
// add all used registers to list
if (OpCode == CBG::PREDREGSETCC || OpCode == CBG::PREDREGSET || OpCode == CBG::PREDREGCLEAR ||
OpCode == CBG::PREDREGSETCC_PREG) {
RegNo = mbb_iter->getOperand(0).getReg();
if (!isPredRegInList(rList, RegNo)) {
rList.push_back(RegNo);
}
}
}
for (mbb_iter = FBB.begin(); mbb_iter != FBB.end(); ++mbb_iter) {
OpCode = mbb_iter->getOpcode();
// add all used registers to list
if (OpCode == CBG::PREDREGSETCC || OpCode == CBG::PREDREGSET || OpCode == CBG::PREDREGCLEAR ||
OpCode == CBG::PREDREGSETCC_PREG) {
RegNo = mbb_iter->getOperand(0).getReg();
if (!isPredRegInList(rList, RegNo)) {
rList.push_back(RegNo);
}
}
}
return rList;
}
/**
* @brief Gets a set of registers which are part of both given lists.
* @param rList1 First input set of registers.
* @param rList2 Second input set of registers.
* @return A set (i.e. a list) of registers representing a cut of rList1 and rList2.
*/
PredicatedRegPass::RegList PredicatedRegPass::cutPredRegLists(RegList &rList1, RegList &rList2) {
RegList rList = rList1;
RegList::iterator reglist_iter = rList.begin();
while(reglist_iter != rList.end()) {
if (!isPredRegInList(rList2, *reglist_iter)) {
rList.remove(*reglist_iter);
reglist_iter = rList.begin();
continue;
}
++reglist_iter;
}
return rList;
}
/**
* @brief Gets a set of registers which unite registers of both given lists.
* @param rList1 First input set of registers.
* @param rList2 Second input set of registers.
* @return A set (i.e. a list) of registers representing a unite operation of rList1 and rList2.
*/
PredicatedRegPass::RegList PredicatedRegPass::unitePredRegLists(RegList &rList1, RegList &rList2) {
RegList rList = rList1;
RegList::iterator reglist_iter = rList2.begin();
while(reglist_iter != rList2.end()) {
if (!isPredRegInList(rList, *reglist_iter)) {
rList.push_back(*reglist_iter);
}
++reglist_iter;
}
return rList;
}
/**
* @brief Register allocator for predication registers based on the
* given machine basic block.
* @param MBB The machine basic block which is the basis for the register
* allocator.
* @return The first predication register which is not used in MBB or (-1)
* if there is no free register.
*/
unsigned PredicatedRegPass::getNextFreePredRegister(MachineBasicBlock &MBB) {
RegList rList = getUsedPredRegisters(MBB);
CBG::PREDRegsClass predRegs;
CBG::PREDRegsClass::iterator pregclass_iter;
unsigned nextFreePredRegister = static_cast<unsigned>(-1);
for (pregclass_iter = predRegs.begin(); pregclass_iter != predRegs.end(); ++pregclass_iter) {
if (!isPredRegInList(rList, *pregclass_iter)) {
nextFreePredRegister = *pregclass_iter;
break;
}
}
return nextFreePredRegister;
}
/**
* @brief Register allocator for predication registers based on the
* given machine basic blocks.
* @param MBB First machine basic block which is the basis for the register
* allocator.
* @param FBB Second machine basic block which is the basis for the register
* allocator.
* @return The first predication register which is not used in MBB and FBB
* or (-1) if there is no free register.
*/
unsigned PredicatedRegPass::getNextFreePredRegister(MachineBasicBlock &TBB, MachineBasicBlock &FBB) {
RegList rList = getUsedPredRegisters(TBB, FBB);
CBG::PREDRegsClass predRegs;
CBG::PREDRegsClass::iterator pregclass_iter;
unsigned nextFreePredRegister = static_cast<unsigned>(-1);
for (pregclass_iter = predRegs.begin(); pregclass_iter != predRegs.end(); ++pregclass_iter) {
if (!isPredRegInList(rList, *pregclass_iter)) {
nextFreePredRegister = *pregclass_iter;
break;
}
}
return nextFreePredRegister;
}
/**
* @brief Removes all predclear instructions before any predicated
* block begin instruction from the given MBB.
* @param MBB The machine basic block to analyze.
* @return A List of all removed predicated registers.
*/
PredicatedRegPass::RegList PredicatedRegPass::removePredRegClears(MachineBasicBlock &MBB) {
RegList rList;
MachineBasicBlock::iterator mbb_iter = MBB.begin();
while (mbb_iter != MBB.end()) {
if (mbb_iter->getOpcode() == CBG::PREDREGCLEAR) {
rList.push_back(mbb_iter->getOperand(0).getReg());
mbb_iter->eraseFromParent();
mbb_iter = MBB.begin();
continue;
}
if (mbb_iter->getOpcode() == CBG::PREDBLOCKREG_BEGIN_T ||
mbb_iter->getOpcode() == CBG::PREDBLOCKREG_BEGIN_F) {
break;
}
++mbb_iter;
}
return rList;
}
/**
* @brief Replaces all occurring predend instructions of the given basic block by
* predbegin[preg][t/f].
* @param MBB The machine basic block which is the subject of change.
* @param PReg The predicate register which is source of the predbegin instruction.
* @param TF Sets the t/f flag of the predbegin instruction.
*/
void PredicatedBlocksRegPass::replacePredEnds(MachineBasicBlock &MBB, unsigned PReg, unsigned TF) {
MachineBasicBlock::iterator mbb_iter;
MachineBasicBlock::iterator pred_end;
DebugLoc dbg_loc = MBB.begin()->getDebugLoc();
for (mbb_iter = MBB.begin(); mbb_iter != MBB.end(); ) {
// replace a predend by predbegin[preg][tf] instruction
if (mbb_iter->getOpcode() == CBG::PREDBLOCKREG_END) {
pred_end = mbb_iter;
++mbb_iter;
if (TF == 1) {
BuildMI(MBB, mbb_iter, dbg_loc, TM.getInstrInfo()->get(CBG::PREDBLOCKREG_BEGIN_T))
.addReg(PReg);
} else {
BuildMI(MBB, mbb_iter, dbg_loc, TM.getInstrInfo()->get(CBG::PREDBLOCKREG_BEGIN_F))
.addReg(PReg);
}
// remove predend instruction
pred_end->eraseFromParent();
// set mbb iter to begin of current MBB
mbb_iter = MBB.begin();
continue;
}
++mbb_iter;
}
}
/**
* @brief Removes all redundant instructions terminating a predicated block
* from the given MBB.
* @param MBB The machine basic block to analyze.
*/
void PredicatedBlocksRegPass::removePredEnds(MachineBasicBlock &MBB) {
MachineBasicBlock::iterator mbb_iter;
MachineBasicBlock::iterator pred_end = MBB.end();
for (mbb_iter = MBB.begin(); mbb_iter != MBB.end(); ) {
// pred_end is not set
if (pred_end == MBB.end()) {
// we have found a prendend or predbegin instruction
if (mbb_iter->getOpcode() == CBG::PREDBLOCKREG_END ||
mbb_iter->getOpcode() == CBG::PREDBLOCKREG_BEGIN_T ||
mbb_iter->getOpcode() == CBG::PREDBLOCKREG_BEGIN_F) {
// save that we have found one and continue
pred_end = mbb_iter;
++mbb_iter;
continue;
}
} else {
// pred_end has been set
if (mbb_iter->getOpcode() == CBG::PREDBLOCKREG_END ||
mbb_iter->getOpcode() == CBG::PREDBLOCKREG_BEGIN_T ||
mbb_iter->getOpcode() == CBG::PREDBLOCKREG_BEGIN_F) {
// we have found the beginning of a predicated block, so
// we should remove any predecessing predend or predbegin
// instructions
pred_end->eraseFromParent();
pred_end = MBB.end();
mbb_iter = MBB.begin();
continue;
}
}
// if pred_end has been defined and we did not have any
// predend or predbegin block, we have to clear the pred_end
// instruction
pred_end = MBB.end();
++mbb_iter;
}
}
/**
* @brief Analyzes the given MBB and returns true, if it contains
* at least one instruction which sets the ICCs of the SPARC psr.
* @param MBB The machine basic block to test.
* @return False, if there is no defining instruction, true otherwise.
*/
bool PredicatedBlocksCCPass::definesICC(MachineBasicBlock &MBB) {
int define_counter = 0;
MachineBasicBlock::reverse_iterator mbb_iter;
const TargetRegisterInfo* registerInfo = TM.getRegisterInfo();
// check for ever instruction within current MBB
for (mbb_iter = MBB.rbegin(); mbb_iter != MBB.rend(); ++mbb_iter) {
// if the current instruction defines the ICC, increment counter
if (mbb_iter->definesRegister(CBG::ICC, registerInfo)) {
define_counter++;
}
}
if (define_counter > 0) {
return true;
} else {
return false;
}
}
/**
* @brief Tries to insert a predicated block by merging the predecessor of
* TBB, FBB and their successor, depending on the branch type.
* @param TBB Represents the "if-then" branch.
* @param FBB Represents the "else" branch in the case of an IFELSEBRANCH or
* the machine basic block logically following TBB in the case of
* an IFBRANCH.
* @param btype Result of the getBranchType() method and may be either IFELSEBRANCH
* or IFBRANCH.
* @see getBranchType()
* @return True if the predicated block could be inserted successfully, false if the
* basic blocks have not been touched.
*/
bool PredicatedBlocksCCPass::insertPredicatedBlock(MachineBasicBlock &TBB,
MachineBasicBlock &FBB,
branchType btype) {
DebugLoc dbg_loc = TBB.begin()->getDebugLoc();
MachineBasicBlock* predecessor;
MachineBasicBlock* successor;
MachineBasicBlock::iterator mbb_iter;
int conditionCode;
bool Changed = false;
// in the case of an if-else branch, none of them may change the
// ICC register
if (btype == IFELSEBRANCH && !definesICC(TBB) && !definesICC(FBB)) {
// in the case of an if-then-else construction,
// we have to handle the following CFG:
// Pre -> FBB -> TBB -> [possible other MBBs] -> Post
// get predecessor (by definition, TBB has only one predecessor)
predecessor = *(TBB.pred_begin());
// save the condition code for TBB
conditionCode = getConditionCode(*predecessor);
// if we could not find any conditional branch, do nothing
if (conditionCode >= 0) {
// TBB has exactly one successor
successor = *(TBB.succ_begin());
// if the successor of TBB is also a layout successor
// we can remove branches from FBB and insert the
// PREDBLOCKCC_END instruction at the end
if (TBB.isLayoutSuccessor(successor)) {
removeBranches(FBB);
mbb_iter = FBB.end();
} else {
// otherwise, we have to insert the PREDBLOCKCC_END instruction
// before the unconditional branch
mbb_iter = FBB.end();
--mbb_iter;
}
// remove any branches from TBB and predecessor
// because they will be merged with FBB which would
// handle the final branch
removeBranches(TBB);
removeBranches(*predecessor);
// insert predicated block begin at begin of TBB with the given condition code
BuildMI(TBB, TBB.begin(), dbg_loc, TM.getInstrInfo()->get(CBG::PREDBLOCKCC_BEGIN))
.addImm(conditionCode);
// insert predicated block begin at begin of FBB with the opposite condition code
conditionCode = CBG::getOppositeBranchCondition(static_cast<CBGCC::CondCodes>(conditionCode));
BuildMI(FBB, FBB.begin(), dbg_loc, TM.getInstrInfo()->get(CBG::PREDBLOCKCC_BEGIN))
.addImm(conditionCode);
// insert predicated block end at end of FBB, resp. before the final unconditional
// branch
BuildMI(FBB, mbb_iter, dbg_loc, TM.getInstrInfo()->get(CBG::PREDBLOCKCC_END));
// merge predecessor with TBB and FBB
mergeBlocks(*predecessor, TBB);
mergeBlocks(*predecessor, FBB);
// if the successor has only one predecessor,
// we can merge the two blocks
if (successor->pred_size() == 1) {
mergeBlocks(*predecessor, *successor);
}
// save that we have changed the structure of the function
Changed = true;
}
// in the case of a single if-branch, TBB is not allowed to
// define the ICC
} else if (btype == IFBRANCH && !definesICC(TBB)) {
// in the case of an if-then-end construction,
// we have to handle the following CFG:
// Pre -> TBB -> [possible other MBBs] -> FBB
// get the only predecessor of TBB
predecessor = *(TBB.pred_begin());
// save the condition code for TBB (which is the opposite for TBB)
conditionCode = getConditionCode(*predecessor);
// if we could not find any conditional branch, do nothing
if (conditionCode >= 0) {
// remove all branches from predecessor
removeBranches(*predecessor);
// depending whether FBB is a layout successor of
// TBB, we have to insert PREBLOCKCC_END at the
// end of TBB or straight before the unconditional
// branch
if (!TBB.isLayoutSuccessor(&FBB)) {
mbb_iter = TBB.end();
--mbb_iter;
} else {
removeBranches(TBB);
mbb_iter = TBB.end();
}
// get the opposite condition code for TBB
conditionCode = CBG::getOppositeBranchCondition(static_cast<CBGCC::CondCodes>(conditionCode));
// start predicated block for TBB
BuildMI(TBB, TBB.begin(), dbg_loc, TM.getInstrInfo()->get(CBG::PREDBLOCKCC_BEGIN))
.addImm(conditionCode);
// end predicated block
BuildMI(TBB, mbb_iter, dbg_loc, TM.getInstrInfo()->get(CBG::PREDBLOCKCC_END));
// merge Pre with TBB
mergeBlocks(*predecessor, TBB);
// only merge FBB with rest if it is a layout successor and it only has
// one predecessor
if (predecessor->isLayoutSuccessor(&FBB) && FBB.pred_size() == 1) {
mergeBlocks(*predecessor, FBB);
}
// save that we have changed the structure of the function
Changed = true;
}
}
return Changed;
}
/**
* @brief Checks for any two MBBs of the given machine function, whether they form an
* if-then-else of if-then construction and insert a predicated block if possible.
* @param F The Machine function to analyze.
* @return True, if any predicated block has been inserted, false otherwise.
*/
bool PredicatedBlocksCCPass::runOnMachineFunction(MachineFunction &F) {
bool Changed = false;
MachineFunction::iterator f_outer_iter;
MachineFunction::iterator f_inner_iter;
BranchMBB_list branches;
BranchMBB_list::iterator branch_iter;
branchType btype;
// check for all basic blocks if they form any branches
for (f_outer_iter = F.begin(); f_outer_iter != F.end(); ++f_outer_iter) {
for (f_inner_iter = F.begin(); f_inner_iter != F.end(); ++f_inner_iter) {
btype = getBranchType(*f_inner_iter, *f_outer_iter);
if (btype != NOBRANCH) {
BranchMBB newBranch = {&(*f_inner_iter), &(*f_outer_iter), btype};
branches.push_back(newBranch);
}
}
}
for (branch_iter = branches.begin(); branch_iter != branches.end(); branch_iter++) {
Changed |= insertPredicatedBlock(*(branch_iter->TBB), *(branch_iter->FBB), branch_iter->btype);
}
return Changed;
}
/**
* @brief Creates a new predicated block pass based on the condition codes of the
* status register.
* @param tm The target machine for the given pass.
* @return FunctionPass pointer to the just created pass.
*/
FunctionPass* llvm::createcbgPredBlockCCPass(TargetMachine &tm) {
return new PredicatedBlocksCCPass(tm);
}
/**
* @brief Tries to insert a predicated block by merging the predecessor of
* TBB, FBB and their successor, depending on the branch type.
* @details TBB and FBB may already contain predicated blocks.
* @param TBB Represents the "if-then" branch.
* @param FBB Represents the "else" branch in the case of an IFELSEBRANCH or
* the machine basic block logically following TBB in the case of
* an IFBRANCH.
* @param btype Result of the getBranchType() method and may be either IFELSEBRANCH
* or IFBRANCH.
* @see getBranchType()
* @return True if the predicated block could be inserted successfully, false if the
* basic blocks have not been touched.
*/
bool PredicatedBlocksRegPass::insertPredicatedBlock(MachineBasicBlock &TBB, MachineBasicBlock &FBB, branchType btype) {
DebugLoc dbg_loc = TBB.begin()->getDebugLoc();
RegList pregList;
RegList pregList2;
RegList::iterator preglist_iterator;
MachineBasicBlock* predecessor;
MachineBasicBlock* successor;
MachineBasicBlock::iterator mbb_iter;
int conditionCode;
unsigned nextPReg;
bool Changed = false;
if (btype == IFELSEBRANCH) {
// in the case of an if-then-else construction,
// we have to handle the following CFG:
// Pre -> FBB -> TBB -> [possible other MBBs] -> Post
// get predecessor (by definition, TBB has only one predecessor)
predecessor = *(TBB.pred_begin());
// save the condition code for TBB
conditionCode = getConditionCode(*predecessor);
// if we could not find any conditional branch, do nothing
if (conditionCode >= 0) {
// TBB has exactly one successor
successor = *(TBB.succ_begin());
// if the successor of TBB is also a layout successor
// we can remove branches from FBB and insert the
// PREDBLOCKCC_END instruction at the end
if (TBB.isLayoutSuccessor(successor)) {
removeBranches(FBB);
mbb_iter = FBB.end();
} else {
// otherwise, we have to insert the PREDBLOCKREG_END instruction
// before the unconditional branch
mbb_iter = FBB.end();
--mbb_iter;
}
// allocate the next free predication register
nextPReg = getNextFreePredRegister(TBB, FBB);
// remove any branches from TBB and predecessor
removeBranches(TBB);
removeBranches(*predecessor);
// remove any clear predication register from TBB and FBB
// and save the used registers in list
pregList = removePredRegClears(TBB);
pregList2 = removePredRegClears(FBB);
// clear all pregs before begin of TBB
pregList = unitePredRegLists(pregList, pregList2);
// clear the current predicate register
BuildMI(*predecessor, predecessor->end(), dbg_loc, TM.getInstrInfo()->get(CBG::PREDREGCLEAR))
.addReg(nextPReg, RegState::Define);
// add clear instructions for all used predicate registers of TBB and FBB at end of predecessor
for (preglist_iterator = pregList.begin(); preglist_iterator != pregList.end(); ++preglist_iterator) {
BuildMI(*predecessor, predecessor->end(), dbg_loc, TM.getInstrInfo()->get(CBG::PREDREGCLEAR))
.addReg(*preglist_iterator, RegState::Define);
}
// set the current predication register based on the condition code
BuildMI(*predecessor, predecessor->end(), dbg_loc, TM.getInstrInfo()->get(CBG::PREDREGSETCC))
.addReg(nextPReg, RegState::Define).addImm(conditionCode);
// add begin predicated block instruction at begin of TBB
BuildMI(TBB, TBB.begin(), dbg_loc, TM.getInstrInfo()->get(CBG::PREDBLOCKREG_BEGIN_T))
.addReg(nextPReg);
// replace all occuring predends by predbegin[nextPreg][t]
replacePredEnds(TBB, nextPReg, 1);
// if the list of used pred registers of FBB contains any entries, we have to insert a block end instruction
if (pregList2.size() > 0) {
BuildMI(TBB, TBB.end(), dbg_loc, TM.getInstrInfo()->get(CBG::PREDBLOCKREG_END));
}
// add clear instructions for all used predicate registers of FBB at end of true basic block
for (preglist_iterator = pregList2.begin(); preglist_iterator != pregList2.end(); ++preglist_iterator) {
BuildMI(TBB, TBB.end(), dbg_loc, TM.getInstrInfo()->get(CBG::PREDREGCLEAR))
.addReg(*preglist_iterator, RegState::Define);
}
// add begin predicated block instruction at begin of FBB
BuildMI(FBB, FBB.begin(), dbg_loc, TM.getInstrInfo()->get(CBG::PREDBLOCKREG_BEGIN_F))
.addReg(nextPReg);
// replace all occuring predends by predbegin[nextPreg][t]
replacePredEnds(FBB, nextPReg, 0);
// add end predicated block instruction at end of FBB, resp. before the unconditional branch of FBB
BuildMI(FBB, mbb_iter, dbg_loc, TM.getInstrInfo()->get(CBG::PREDBLOCKREG_END));
// merge predecessor with TBB and FBB
mergeBlocks(*predecessor, TBB);
mergeBlocks(*predecessor, FBB);
// if the successor has only one predecessor,
// we can merge the two blocks
if (successor->pred_size() == 1) {
mergeBlocks(*predecessor, *successor);
}
// remove redundant predends and from newly built MBB
removePredEnds(*predecessor);
// save that we have changed the structure of the function
Changed = true;
}
} else if (btype == IFBRANCH) {
// in the case of an if-then-end construction,
// we have to handle the following CFG:
// Pre -> TBB -> [possible other MBBs] -> FBB
// get the only predecessor of TBB
predecessor = *(TBB.pred_begin());
// save the condition code for TBB (which is the opposite for TBB)
conditionCode = getConditionCode(*predecessor);
// if we could not find any conditional branch, do nothing
if (conditionCode >= 0) {
// remove all branches from predecessor
removeBranches(*predecessor);
// depending whether FBB is a layout successor of
// TBB, we have to insert PREBLOCKREG_END at the
// end of TBB or straight before the unconditional
// branch
if (!TBB.isLayoutSuccessor(&FBB)) {
mbb_iter = TBB.end();
--mbb_iter;
} else {
removeBranches(TBB);
mbb_iter = TBB.end();
}
// allocate the next free predication register
nextPReg = getNextFreePredRegister(TBB);
// remove any clear predication register from TBB
// and get a set of all used predication registers of TBB
pregList = removePredRegClears(TBB);
// clear the current predicate register
BuildMI(*predecessor, predecessor->end(), dbg_loc, TM.getInstrInfo()->get(CBG::PREDREGCLEAR))
.addReg(nextPReg, RegState::Define);
// add clear instructions for all used predicate registers of TBB at end of predecessor
for (preglist_iterator = pregList.begin(); preglist_iterator != pregList.end(); ++preglist_iterator) {
BuildMI(*predecessor, predecessor->end(), dbg_loc, TM.getInstrInfo()->get(CBG::PREDREGCLEAR))
.addReg(*preglist_iterator, RegState::Define);
}
// get the opposite condition code for TBB
conditionCode = CBG::getOppositeBranchCondition(static_cast<CBGCC::CondCodes>(conditionCode));
// set the current predication register based on the condition code
BuildMI(*predecessor, predecessor->end(), dbg_loc, TM.getInstrInfo()->get(CBG::PREDREGSETCC))
.addReg(nextPReg, RegState::Define).addImm(conditionCode);
// add start predicated block instruction at begin of TBB
BuildMI(TBB, TBB.begin(), dbg_loc, TM.getInstrInfo()->get(CBG::PREDBLOCKREG_BEGIN_T))
.addReg(nextPReg);
// replace all predends within current MBB
replacePredEnds(TBB, nextPReg, 1);
// add end predicated block instruction at end of TBB
BuildMI(TBB, mbb_iter, dbg_loc, TM.getInstrInfo()->get(CBG::PREDBLOCKREG_END));
// merge predecessor with TBB
mergeBlocks(*predecessor, TBB);
// only merge FBB with rest if it is a layout successor and it only has