1 //===-- IfConversion.cpp - Machine code if conversion pass. ---------------===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by the Evan Cheng and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the machine instruction level if-conversion pass.
12 //===----------------------------------------------------------------------===//
14 #define DEBUG_TYPE "ifcvt"
15 #include "llvm/Function.h"
16 #include "llvm/CodeGen/Passes.h"
17 #include "llvm/CodeGen/MachineModuleInfo.h"
18 #include "llvm/CodeGen/MachineFunctionPass.h"
19 #include "llvm/Target/TargetInstrInfo.h"
20 #include "llvm/Target/TargetLowering.h"
21 #include "llvm/Target/TargetMachine.h"
22 #include "llvm/Support/CommandLine.h"
23 #include "llvm/Support/Debug.h"
24 #include "llvm/ADT/DepthFirstIterator.h"
25 #include "llvm/ADT/Statistic.h"
26 #include "llvm/ADT/STLExtras.h"
30 // Hidden options for help debugging.
31 cl::opt<int> IfCvtFnStart("ifcvt-fn-start", cl::init(-1), cl::Hidden);
32 cl::opt<int> IfCvtFnStop("ifcvt-fn-stop", cl::init(-1), cl::Hidden);
33 cl::opt<int> IfCvtLimit("ifcvt-limit", cl::init(-1), cl::Hidden);
34 cl::opt<bool> DisableSimple("disable-ifcvt-simple",
35 cl::init(false), cl::Hidden);
36 cl::opt<bool> DisableSimpleF("disable-ifcvt-simple-false",
37 cl::init(false), cl::Hidden);
38 cl::opt<bool> DisableTriangle("disable-ifcvt-triangle",
39 cl::init(false), cl::Hidden);
40 cl::opt<bool> DisableTriangleR("disable-ifcvt-triangle-rev",
41 cl::init(false), cl::Hidden);
42 cl::opt<bool> DisableTriangleF("disable-ifcvt-triangle-false",
43 cl::init(false), cl::Hidden);
44 cl::opt<bool> DisableTriangleFR("disable-ifcvt-triangle-false-rev",
45 cl::init(false), cl::Hidden);
46 cl::opt<bool> DisableDiamond("disable-ifcvt-diamond",
47 cl::init(false), cl::Hidden);
50 STATISTIC(NumSimple, "Number of simple if-conversions performed");
51 STATISTIC(NumSimpleFalse, "Number of simple (F) if-conversions performed");
52 STATISTIC(NumTriangle, "Number of triangle if-conversions performed");
53 STATISTIC(NumTriangleRev, "Number of triangle (R) if-conversions performed");
54 STATISTIC(NumTriangleFalse,"Number of triangle (F) if-conversions performed");
55 STATISTIC(NumTriangleFRev, "Number of triangle (F/R) if-conversions performed");
56 STATISTIC(NumDiamonds, "Number of diamond if-conversions performed");
57 STATISTIC(NumIfConvBBs, "Number of if-converted blocks");
58 STATISTIC(NumDupBBs, "Number of duplicated blocks");
61 class IfConverter : public MachineFunctionPass {
63 ICNotClassfied, // BB data valid, but not classified.
64 ICSimpleFalse, // Same as ICSimple, but on the false path.
65 ICSimple, // BB is entry of an one split, no rejoin sub-CFG.
66 ICTriangleFRev, // Same as ICTriangleFalse, but false path rev condition.
67 ICTriangleRev, // Same as ICTriangle, but true path rev condition.
68 ICTriangleFalse, // Same as ICTriangle, but on the false path.
69 ICTriangle, // BB is entry of a triangle sub-CFG.
70 ICDiamond // BB is entry of a diamond sub-CFG.
73 /// BBInfo - One per MachineBasicBlock, this is used to cache the result
74 /// if-conversion feasibility analysis. This includes results from
75 /// TargetInstrInfo::AnalyzeBranch() (i.e. TBB, FBB, and Cond), and its
76 /// classification, and common tail block of its successors (if it's a
77 /// diamond shape), its size, whether it's predicable, and whether any
78 /// instruction can clobber the 'would-be' predicate.
80 /// IsDone - True if BB is not to be considered for ifcvt.
81 /// IsBeingAnalyzed - True if BB is currently being analyzed.
82 /// IsAnalyzed - True if BB has been analyzed (info is still valid).
83 /// IsEnqueued - True if BB has been enqueued to be ifcvt'ed.
84 /// IsBrAnalyzable - True if AnalyzeBranch() returns false.
85 /// HasFallThrough - True if BB may fallthrough to the following BB.
86 /// IsUnpredicable - True if BB is known to be unpredicable.
87 /// ClobbersPredicate- True if BB would modify the predicate (e.g. has
89 /// NonPredSize - Number of non-predicated instructions.
90 /// BB - Corresponding MachineBasicBlock.
91 /// TrueBB / FalseBB- See AnalyzeBranch().
92 /// BrCond - Conditions for end of block conditional branches.
93 /// Predicate - Predicate used in the BB.
96 bool IsBeingAnalyzed : 1;
99 bool IsBrAnalyzable : 1;
100 bool HasFallThrough : 1;
101 bool IsUnpredicable : 1;
102 bool CannotBeCopied : 1;
103 bool ClobbersPred : 1;
104 unsigned NonPredSize;
105 MachineBasicBlock *BB;
106 MachineBasicBlock *TrueBB;
107 MachineBasicBlock *FalseBB;
108 std::vector<MachineOperand> BrCond;
109 std::vector<MachineOperand> Predicate;
110 BBInfo() : IsDone(false), IsBeingAnalyzed(false),
111 IsAnalyzed(false), IsEnqueued(false), IsBrAnalyzable(false),
112 HasFallThrough(false), IsUnpredicable(false),
113 CannotBeCopied(false), ClobbersPred(false), NonPredSize(0),
114 BB(0), TrueBB(0), FalseBB(0) {}
117 /// IfcvtToken - Record information about pending if-conversions to attemp:
118 /// BBI - Corresponding BBInfo.
119 /// Kind - Type of block. See IfcvtKind.
120 /// NeedSubsumsion - True if the to be predicated BB has already been
122 /// NumDups - Number of instructions that would be duplicated due
123 /// to this if-conversion. (For diamonds, the number of
124 /// identical instructions at the beginnings of both
126 /// NumDups2 - For diamonds, the number of identical instructions
127 /// at the ends of both paths.
134 IfcvtToken(BBInfo &b, IfcvtKind k, bool s, unsigned d, unsigned d2 = 0)
135 : BBI(b), Kind(k), NeedSubsumsion(s), NumDups(d), NumDups2(d2) {}
138 /// Roots - Basic blocks that do not have successors. These are the starting
139 /// points of Graph traversal.
140 std::vector<MachineBasicBlock*> Roots;
142 /// BBAnalysis - Results of if-conversion feasibility analysis indexed by
143 /// basic block number.
144 std::vector<BBInfo> BBAnalysis;
146 const TargetLowering *TLI;
147 const TargetInstrInfo *TII;
151 IfConverter() : MachineFunctionPass((intptr_t)&ID) {}
153 virtual bool runOnMachineFunction(MachineFunction &MF);
154 virtual const char *getPassName() const { return "If converter"; }
157 bool ReverseBranchCondition(BBInfo &BBI);
158 bool ValidSimple(BBInfo &TrueBBI, unsigned &Dups) const;
159 bool ValidTriangle(BBInfo &TrueBBI, BBInfo &FalseBBI,
160 bool FalseBranch, unsigned &Dups) const;
161 bool ValidDiamond(BBInfo &TrueBBI, BBInfo &FalseBBI,
162 unsigned &Dups1, unsigned &Dups2) const;
163 void ScanInstructions(BBInfo &BBI);
164 BBInfo &AnalyzeBlock(MachineBasicBlock *BB,
165 std::vector<IfcvtToken*> &Tokens);
166 bool FeasibilityAnalysis(BBInfo &BBI, std::vector<MachineOperand> &Cond,
167 bool isTriangle = false, bool RevBranch = false);
168 bool AnalyzeBlocks(MachineFunction &MF,
169 std::vector<IfcvtToken*> &Tokens);
170 void InvalidatePreds(MachineBasicBlock *BB);
171 void RemoveExtraEdges(BBInfo &BBI);
172 bool IfConvertSimple(BBInfo &BBI, IfcvtKind Kind);
173 bool IfConvertTriangle(BBInfo &BBI, IfcvtKind Kind);
174 bool IfConvertDiamond(BBInfo &BBI, IfcvtKind Kind,
175 unsigned NumDups1, unsigned NumDups2);
176 void PredicateBlock(BBInfo &BBI,
177 MachineBasicBlock::iterator E,
178 std::vector<MachineOperand> &Cond);
179 void CopyAndPredicateBlock(BBInfo &ToBBI, BBInfo &FromBBI,
180 std::vector<MachineOperand> &Cond,
181 bool IgnoreBr = false);
182 void MergeBlocks(BBInfo &ToBBI, BBInfo &FromBBI);
184 bool MeetIfcvtSizeLimit(unsigned Size) const {
185 return Size > 0 && Size <= TLI->getIfCvtBlockSizeLimit();
188 // blockAlwaysFallThrough - Block ends without a terminator.
189 bool blockAlwaysFallThrough(BBInfo &BBI) const {
190 return BBI.IsBrAnalyzable && BBI.TrueBB == NULL;
193 // IfcvtTokenCmp - Used to sort if-conversion candidates.
194 static bool IfcvtTokenCmp(IfcvtToken *C1, IfcvtToken *C2) {
195 int Incr1 = (C1->Kind == ICDiamond)
196 ? -(int)(C1->NumDups + C1->NumDups2) : (int)C1->NumDups;
197 int Incr2 = (C2->Kind == ICDiamond)
198 ? -(int)(C2->NumDups + C2->NumDups2) : (int)C2->NumDups;
201 else if (Incr1 == Incr2) {
202 // Favors subsumsion.
203 if (C1->NeedSubsumsion == false && C2->NeedSubsumsion == true)
205 else if (C1->NeedSubsumsion == C2->NeedSubsumsion) {
206 // Favors diamond over triangle, etc.
207 if ((unsigned)C1->Kind < (unsigned)C2->Kind)
209 else if (C1->Kind == C2->Kind)
210 return C1->BBI.BB->getNumber() < C2->BBI.BB->getNumber();
217 char IfConverter::ID = 0;
220 FunctionPass *llvm::createIfConverterPass() { return new IfConverter(); }
222 bool IfConverter::runOnMachineFunction(MachineFunction &MF) {
223 TLI = MF.getTarget().getTargetLowering();
224 TII = MF.getTarget().getInstrInfo();
225 if (!TII) return false;
227 static int FnNum = -1;
228 DOUT << "\nIfcvt: function (" << ++FnNum << ") \'"
229 << MF.getFunction()->getName() << "\'";
231 if (FnNum < IfCvtFnStart || (IfCvtFnStop != -1 && FnNum > IfCvtFnStop)) {
232 DOUT << " skipped\n";
238 BBAnalysis.resize(MF.getNumBlockIDs());
240 // Look for root nodes, i.e. blocks without successors.
241 for (MachineFunction::iterator I = MF.begin(), E = MF.end(); I != E; ++I)
242 if (I->succ_size() == 0)
245 std::vector<IfcvtToken*> Tokens;
247 unsigned NumIfCvts = NumSimple + NumSimpleFalse + NumTriangle +
248 NumTriangleRev + NumTriangleFalse + NumTriangleFRev + NumDiamonds;
249 while (IfCvtLimit == -1 || (int)NumIfCvts < IfCvtLimit) {
250 // Do an intial analysis for each basic block and finding all the potential
251 // candidates to perform if-convesion.
252 bool Change = AnalyzeBlocks(MF, Tokens);
253 while (!Tokens.empty()) {
254 IfcvtToken *Token = Tokens.back();
256 BBInfo &BBI = Token->BBI;
257 IfcvtKind Kind = Token->Kind;
259 // If the block has been evicted out of the queue or it has already been
260 // marked dead (due to it being predicated), then skip it.
262 BBI.IsEnqueued = false;
266 BBI.IsEnqueued = false;
270 default: assert(false && "Unexpected!");
273 case ICSimpleFalse: {
274 bool isFalse = Kind == ICSimpleFalse;
275 if ((isFalse && DisableSimpleF) || (!isFalse && DisableSimple)) break;
276 DOUT << "Ifcvt (Simple" << (Kind == ICSimpleFalse ? " false" :"")
277 << "): BB#" << BBI.BB->getNumber() << " ("
278 << ((Kind == ICSimpleFalse)
279 ? BBI.FalseBB->getNumber()
280 : BBI.TrueBB->getNumber()) << ") ";
281 RetVal = IfConvertSimple(BBI, Kind);
282 DOUT << (RetVal ? "succeeded!" : "failed!") << "\n";
284 if (isFalse) NumSimpleFalse++;
290 case ICTriangleFalse:
291 case ICTriangleFRev: {
292 bool isFalse = Kind == ICTriangleFalse;
293 bool isRev = (Kind == ICTriangleRev || Kind == ICTriangleFRev);
294 if (DisableTriangle && !isFalse && !isRev) break;
295 if (DisableTriangleR && !isFalse && isRev) break;
296 if (DisableTriangleF && isFalse && !isRev) break;
297 if (DisableTriangleFR && isFalse && isRev) break;
298 DOUT << "Ifcvt (Triangle";
303 DOUT << "): BB#" << BBI.BB->getNumber() << " (T:"
304 << BBI.TrueBB->getNumber() << ",F:"
305 << BBI.FalseBB->getNumber() << ") ";
306 RetVal = IfConvertTriangle(BBI, Kind);
307 DOUT << (RetVal ? "succeeded!" : "failed!") << "\n";
310 if (isRev) NumTriangleFRev++;
311 else NumTriangleFalse++;
313 if (isRev) NumTriangleRev++;
320 if (DisableDiamond) break;
321 DOUT << "Ifcvt (Diamond): BB#" << BBI.BB->getNumber() << " (T:"
322 << BBI.TrueBB->getNumber() << ",F:"
323 << BBI.FalseBB->getNumber() << ") ";
324 RetVal = IfConvertDiamond(BBI, Kind, Token->NumDups, Token->NumDups2);
325 DOUT << (RetVal ? "succeeded!" : "failed!") << "\n";
326 if (RetVal) NumDiamonds++;
333 NumIfCvts = NumSimple + NumSimpleFalse + NumTriangle + NumTriangleRev +
334 NumTriangleFalse + NumTriangleFRev + NumDiamonds;
335 if (IfCvtLimit != -1 && (int)NumIfCvts >= IfCvtLimit)
341 MadeChange |= Change;
344 // Delete tokens in case of early exit.
345 while (!Tokens.empty()) {
346 IfcvtToken *Token = Tokens.back();
358 /// findFalseBlock - BB has a fallthrough. Find its 'false' successor given
359 /// its 'true' successor.
360 static MachineBasicBlock *findFalseBlock(MachineBasicBlock *BB,
361 MachineBasicBlock *TrueBB) {
362 for (MachineBasicBlock::succ_iterator SI = BB->succ_begin(),
363 E = BB->succ_end(); SI != E; ++SI) {
364 MachineBasicBlock *SuccBB = *SI;
365 if (SuccBB != TrueBB)
371 /// ReverseBranchCondition - Reverse the condition of the end of the block
372 /// branchs. Swap block's 'true' and 'false' successors.
373 bool IfConverter::ReverseBranchCondition(BBInfo &BBI) {
374 if (!TII->ReverseBranchCondition(BBI.BrCond)) {
375 TII->RemoveBranch(*BBI.BB);
376 TII->InsertBranch(*BBI.BB, BBI.FalseBB, BBI.TrueBB, BBI.BrCond);
377 std::swap(BBI.TrueBB, BBI.FalseBB);
383 /// getNextBlock - Returns the next block in the function blocks ordering. If
384 /// it is the end, returns NULL.
385 static inline MachineBasicBlock *getNextBlock(MachineBasicBlock *BB) {
386 MachineFunction::iterator I = BB;
387 MachineFunction::iterator E = BB->getParent()->end();
393 /// ValidSimple - Returns true if the 'true' block (along with its
394 /// predecessor) forms a valid simple shape for ifcvt. It also returns the
395 /// number of instructions that the ifcvt would need to duplicate if performed
397 bool IfConverter::ValidSimple(BBInfo &TrueBBI, unsigned &Dups) const {
399 if (TrueBBI.IsBeingAnalyzed || TrueBBI.IsDone)
402 if (TrueBBI.BB->pred_size() > 1) {
403 if (TrueBBI.CannotBeCopied ||
404 TrueBBI.NonPredSize > TLI->getIfCvtDupBlockSizeLimit())
406 Dups = TrueBBI.NonPredSize;
409 return !blockAlwaysFallThrough(TrueBBI) && TrueBBI.BrCond.size() == 0;
412 /// ValidTriangle - Returns true if the 'true' and 'false' blocks (along
413 /// with their common predecessor) forms a valid triangle shape for ifcvt.
414 /// If 'FalseBranch' is true, it checks if 'true' block's false branch
415 /// branches to the false branch rather than the other way around. It also
416 /// returns the number of instructions that the ifcvt would need to duplicate
417 /// if performed in 'Dups'.
418 bool IfConverter::ValidTriangle(BBInfo &TrueBBI, BBInfo &FalseBBI,
419 bool FalseBranch, unsigned &Dups) const {
421 if (TrueBBI.IsBeingAnalyzed || TrueBBI.IsDone)
424 if (TrueBBI.BB->pred_size() > 1) {
425 if (TrueBBI.CannotBeCopied)
428 unsigned Size = TrueBBI.NonPredSize;
429 if (TrueBBI.IsBrAnalyzable) {
430 if (TrueBBI.TrueBB && TrueBBI.BrCond.size() == 0)
431 // End with an unconditional branch. It will be removed.
434 MachineBasicBlock *FExit = FalseBranch
435 ? TrueBBI.TrueBB : TrueBBI.FalseBB;
437 // Require a conditional branch
441 if (Size > TLI->getIfCvtDupBlockSizeLimit())
446 MachineBasicBlock *TExit = FalseBranch ? TrueBBI.FalseBB : TrueBBI.TrueBB;
447 if (!TExit && blockAlwaysFallThrough(TrueBBI)) {
448 MachineFunction::iterator I = TrueBBI.BB;
449 if (++I == TrueBBI.BB->getParent()->end())
453 return TExit && TExit == FalseBBI.BB;
457 MachineBasicBlock::iterator firstNonBranchInst(MachineBasicBlock *BB,
458 const TargetInstrInfo *TII) {
459 MachineBasicBlock::iterator I = BB->end();
460 while (I != BB->begin()) {
462 const TargetInstrDescriptor *TID = I->getInstrDescriptor();
463 if ((TID->Flags & M_BRANCH_FLAG) == 0)
469 /// ValidDiamond - Returns true if the 'true' and 'false' blocks (along
470 /// with their common predecessor) forms a valid diamond shape for ifcvt.
471 bool IfConverter::ValidDiamond(BBInfo &TrueBBI, BBInfo &FalseBBI,
472 unsigned &Dups1, unsigned &Dups2) const {
474 if (TrueBBI.IsBeingAnalyzed || TrueBBI.IsDone ||
475 FalseBBI.IsBeingAnalyzed || FalseBBI.IsDone)
478 MachineBasicBlock *TT = TrueBBI.TrueBB;
479 MachineBasicBlock *FT = FalseBBI.TrueBB;
481 if (!TT && blockAlwaysFallThrough(TrueBBI))
482 TT = getNextBlock(TrueBBI.BB);
483 if (!FT && blockAlwaysFallThrough(FalseBBI))
484 FT = getNextBlock(FalseBBI.BB);
487 if (TT == NULL && (TrueBBI.IsBrAnalyzable || FalseBBI.IsBrAnalyzable))
489 // FIXME: Allow false block to have an early exit?
490 if (TrueBBI.BB->pred_size() > 1 ||
491 FalseBBI.BB->pred_size() > 1 ||
492 TrueBBI.FalseBB || FalseBBI.FalseBB ||
493 (TrueBBI.ClobbersPred && FalseBBI.ClobbersPred))
496 MachineBasicBlock::iterator TI = TrueBBI.BB->begin();
497 MachineBasicBlock::iterator FI = FalseBBI.BB->begin();
498 while (TI != TrueBBI.BB->end() && FI != FalseBBI.BB->end()) {
499 if (!TI->isIdenticalTo(FI))
506 TI = firstNonBranchInst(TrueBBI.BB, TII);
507 FI = firstNonBranchInst(FalseBBI.BB, TII);
508 while (TI != TrueBBI.BB->begin() && FI != FalseBBI.BB->begin()) {
509 if (!TI->isIdenticalTo(FI))
519 /// ScanInstructions - Scan all the instructions in the block to determine if
520 /// the block is predicable. In most cases, that means all the instructions
521 /// in the block has M_PREDICABLE flag. Also checks if the block contains any
522 /// instruction which can clobber a predicate (e.g. condition code register).
523 /// If so, the block is not predicable unless it's the last instruction.
524 void IfConverter::ScanInstructions(BBInfo &BBI) {
528 // First analyze the end of BB branches.
529 BBI.TrueBB = BBI.FalseBB = NULL;
532 !TII->AnalyzeBranch(*BBI.BB, BBI.TrueBB, BBI.FalseBB, BBI.BrCond);
533 BBI.HasFallThrough = BBI.IsBrAnalyzable && BBI.FalseBB == NULL;
535 if (BBI.BrCond.size()) {
536 // No false branch. This BB must end with a conditional branch and a
539 BBI.FalseBB = findFalseBlock(BBI.BB, BBI.TrueBB);
540 assert(BBI.FalseBB && "Expected to find the fallthrough block!");
543 // Then scan all the instructions.
545 BBI.ClobbersPred = false;
546 bool SeenCondBr = false;
547 for (MachineBasicBlock::iterator I = BBI.BB->begin(), E = BBI.BB->end();
549 if (!BBI.CannotBeCopied && !TII->CanBeDuplicated(I))
550 BBI.CannotBeCopied = true;
552 const TargetInstrDescriptor *TID = I->getInstrDescriptor();
553 bool isPredicated = TII->isPredicated(I);
554 bool isCondBr = BBI.IsBrAnalyzable &&
555 (TID->Flags & M_BRANCH_FLAG) != 0 && (TID->Flags & M_BARRIER_FLAG) == 0;
557 if (!isPredicated && !isCondBr)
560 if (BBI.ClobbersPred && !isPredicated) {
561 // Predicate modification instruction should end the block (except for
562 // already predicated instructions and end of block branches).
566 // Conditional branches is not predicable. But it may be eliminated.
570 // Predicate may have been modified, the subsequent (currently)
571 // unpredocated instructions cannot be correctly predicated.
572 BBI.IsUnpredicable = true;
576 if (TID->Flags & M_CLOBBERS_PRED)
577 BBI.ClobbersPred = true;
579 if ((TID->Flags & M_PREDICABLE) == 0) {
580 BBI.IsUnpredicable = true;
586 /// FeasibilityAnalysis - Determine if the block is a suitable candidate to be
587 /// predicated by the specified predicate.
588 bool IfConverter::FeasibilityAnalysis(BBInfo &BBI,
589 std::vector<MachineOperand> &Pred,
590 bool isTriangle, bool RevBranch) {
591 // If the block is dead or unpredicable, then it cannot be predicated.
592 if (BBI.IsDone || BBI.IsUnpredicable)
595 // If it is already predicated, check if its predicate subsumes the new
597 if (BBI.Predicate.size() && !TII->SubsumesPredicate(BBI.Predicate, Pred))
600 if (BBI.BrCond.size()) {
604 // Test predicate subsumsion.
605 std::vector<MachineOperand> RevPred(Pred);
606 std::vector<MachineOperand> Cond(BBI.BrCond);
608 if (TII->ReverseBranchCondition(Cond))
611 if (TII->ReverseBranchCondition(RevPred) ||
612 !TII->SubsumesPredicate(Cond, RevPred))
619 /// AnalyzeBlock - Analyze the structure of the sub-CFG starting from
620 /// the specified block. Record its successors and whether it looks like an
621 /// if-conversion candidate.
622 IfConverter::BBInfo &IfConverter::AnalyzeBlock(MachineBasicBlock *BB,
623 std::vector<IfcvtToken*> &Tokens) {
624 BBInfo &BBI = BBAnalysis[BB->getNumber()];
626 if (BBI.IsAnalyzed || BBI.IsBeingAnalyzed)
630 BBI.IsBeingAnalyzed = true;
632 ScanInstructions(BBI);
634 // Unanalyable or ends with fallthrough or unconditional branch.
635 if (!BBI.IsBrAnalyzable || BBI.BrCond.size() == 0) {
636 BBI.IsBeingAnalyzed = false;
637 BBI.IsAnalyzed = true;
641 // Do not ifcvt if either path is a back edge to the entry block.
642 if (BBI.TrueBB == BB || BBI.FalseBB == BB) {
643 BBI.IsBeingAnalyzed = false;
644 BBI.IsAnalyzed = true;
648 BBInfo &TrueBBI = AnalyzeBlock(BBI.TrueBB, Tokens);
649 BBInfo &FalseBBI = AnalyzeBlock(BBI.FalseBB, Tokens);
651 if (TrueBBI.IsDone && FalseBBI.IsDone) {
652 BBI.IsBeingAnalyzed = false;
653 BBI.IsAnalyzed = true;
657 std::vector<MachineOperand> RevCond(BBI.BrCond);
658 bool CanRevCond = !TII->ReverseBranchCondition(RevCond);
662 bool TNeedSub = TrueBBI.Predicate.size() > 0;
663 bool FNeedSub = FalseBBI.Predicate.size() > 0;
664 bool Enqueued = false;
665 if (CanRevCond && ValidDiamond(TrueBBI, FalseBBI, Dups, Dups2) &&
666 MeetIfcvtSizeLimit(TrueBBI.NonPredSize - (Dups + Dups2)) &&
667 MeetIfcvtSizeLimit(FalseBBI.NonPredSize - (Dups + Dups2)) &&
668 FeasibilityAnalysis(TrueBBI, BBI.BrCond) &&
669 FeasibilityAnalysis(FalseBBI, RevCond)) {
677 // Note TailBB can be empty.
678 Tokens.push_back(new IfcvtToken(BBI, ICDiamond, TNeedSub|FNeedSub, Dups,
683 if (ValidTriangle(TrueBBI, FalseBBI, false, Dups) &&
684 MeetIfcvtSizeLimit(TrueBBI.NonPredSize) &&
685 FeasibilityAnalysis(TrueBBI, BBI.BrCond, true)) {
693 Tokens.push_back(new IfcvtToken(BBI, ICTriangle, TNeedSub, Dups));
697 if (ValidTriangle(TrueBBI, FalseBBI, true, Dups) &&
698 MeetIfcvtSizeLimit(TrueBBI.NonPredSize) &&
699 FeasibilityAnalysis(TrueBBI, BBI.BrCond, true, true)) {
700 Tokens.push_back(new IfcvtToken(BBI, ICTriangleRev, TNeedSub, Dups));
704 if (ValidSimple(TrueBBI, Dups) &&
705 MeetIfcvtSizeLimit(TrueBBI.NonPredSize) &&
706 FeasibilityAnalysis(TrueBBI, BBI.BrCond)) {
707 // Simple (split, no rejoin):
714 Tokens.push_back(new IfcvtToken(BBI, ICSimple, TNeedSub, Dups));
719 // Try the other path...
720 if (ValidTriangle(FalseBBI, TrueBBI, false, Dups) &&
721 MeetIfcvtSizeLimit(FalseBBI.NonPredSize) &&
722 FeasibilityAnalysis(FalseBBI, RevCond, true)) {
723 Tokens.push_back(new IfcvtToken(BBI, ICTriangleFalse, FNeedSub, Dups));
727 if (ValidTriangle(FalseBBI, TrueBBI, true, Dups) &&
728 MeetIfcvtSizeLimit(FalseBBI.NonPredSize) &&
729 FeasibilityAnalysis(FalseBBI, RevCond, true, true)) {
730 Tokens.push_back(new IfcvtToken(BBI, ICTriangleFRev, FNeedSub, Dups));
734 if (ValidSimple(FalseBBI, Dups) &&
735 MeetIfcvtSizeLimit(FalseBBI.NonPredSize) &&
736 FeasibilityAnalysis(FalseBBI, RevCond)) {
737 Tokens.push_back(new IfcvtToken(BBI, ICSimpleFalse, FNeedSub, Dups));
742 BBI.IsEnqueued = Enqueued;
743 BBI.IsBeingAnalyzed = false;
744 BBI.IsAnalyzed = true;
748 /// AnalyzeBlocks - Analyze all blocks and find entries for all if-conversion
749 /// candidates. It returns true if any CFG restructuring is done to expose more
750 /// if-conversion opportunities.
751 bool IfConverter::AnalyzeBlocks(MachineFunction &MF,
752 std::vector<IfcvtToken*> &Tokens) {
754 std::set<MachineBasicBlock*> Visited;
755 for (unsigned i = 0, e = Roots.size(); i != e; ++i) {
756 for (idf_ext_iterator<MachineBasicBlock*> I=idf_ext_begin(Roots[i],Visited),
757 E = idf_ext_end(Roots[i], Visited); I != E; ++I) {
758 MachineBasicBlock *BB = *I;
759 AnalyzeBlock(BB, Tokens);
763 // Sort to favor more complex ifcvt scheme.
764 std::stable_sort(Tokens.begin(), Tokens.end(), IfcvtTokenCmp);
769 /// canFallThroughTo - Returns true either if ToBB is the next block after BB or
770 /// that all the intervening blocks are empty (given BB can fall through to its
772 static bool canFallThroughTo(MachineBasicBlock *BB, MachineBasicBlock *ToBB) {
773 MachineFunction::iterator I = BB;
774 MachineFunction::iterator TI = ToBB;
775 MachineFunction::iterator E = BB->getParent()->end();
777 if (I == E || !I->empty())
782 /// InvalidatePreds - Invalidate predecessor BB info so it would be re-analyzed
783 /// to determine if it can be if-converted. If predecessor is already enqueued,
785 void IfConverter::InvalidatePreds(MachineBasicBlock *BB) {
786 for (MachineBasicBlock::pred_iterator PI = BB->pred_begin(),
787 E = BB->pred_end(); PI != E; ++PI) {
788 BBInfo &PBBI = BBAnalysis[(*PI)->getNumber()];
789 if (PBBI.IsDone || PBBI.BB == BB)
791 PBBI.IsAnalyzed = false;
792 PBBI.IsEnqueued = false;
796 /// InsertUncondBranch - Inserts an unconditional branch from BB to ToBB.
798 static void InsertUncondBranch(MachineBasicBlock *BB, MachineBasicBlock *ToBB,
799 const TargetInstrInfo *TII) {
800 std::vector<MachineOperand> NoCond;
801 TII->InsertBranch(*BB, ToBB, NULL, NoCond);
804 /// RemoveExtraEdges - Remove true / false edges if either / both are no longer
806 void IfConverter::RemoveExtraEdges(BBInfo &BBI) {
807 MachineBasicBlock *TBB = NULL, *FBB = NULL;
808 std::vector<MachineOperand> Cond;
809 bool isAnalyzable = !TII->AnalyzeBranch(*BBI.BB, TBB, FBB, Cond);
810 bool CanFallthrough = isAnalyzable && (TBB == NULL || FBB == NULL);
811 if (BBI.TrueBB && BBI.BB->isSuccessor(BBI.TrueBB))
812 if (!(BBI.TrueBB == TBB || BBI.TrueBB == FBB ||
813 (CanFallthrough && getNextBlock(BBI.BB) == BBI.TrueBB)))
814 BBI.BB->removeSuccessor(BBI.TrueBB);
815 if (BBI.FalseBB && BBI.BB->isSuccessor(BBI.FalseBB))
816 if (!(BBI.FalseBB == TBB || BBI.FalseBB == FBB ||
817 (CanFallthrough && getNextBlock(BBI.BB) == BBI.FalseBB)))
818 BBI.BB->removeSuccessor(BBI.FalseBB);
821 /// IfConvertSimple - If convert a simple (split, no rejoin) sub-CFG.
823 bool IfConverter::IfConvertSimple(BBInfo &BBI, IfcvtKind Kind) {
824 BBInfo &TrueBBI = BBAnalysis[BBI.TrueBB->getNumber()];
825 BBInfo &FalseBBI = BBAnalysis[BBI.FalseBB->getNumber()];
826 BBInfo *CvtBBI = &TrueBBI;
827 BBInfo *NextBBI = &FalseBBI;
829 std::vector<MachineOperand> Cond(BBI.BrCond);
830 if (Kind == ICSimpleFalse)
831 std::swap(CvtBBI, NextBBI);
833 if (CvtBBI->IsDone ||
834 (CvtBBI->CannotBeCopied && CvtBBI->BB->pred_size() > 1)) {
835 // Something has changed. It's no longer safe to predicate this block.
836 BBI.IsAnalyzed = false;
837 CvtBBI->IsAnalyzed = false;
841 if (Kind == ICSimpleFalse)
842 TII->ReverseBranchCondition(Cond);
844 if (CvtBBI->BB->pred_size() > 1) {
845 BBI.NonPredSize -= TII->RemoveBranch(*BBI.BB);
846 // Copy instructions in the true block, predicate them add them to
848 CopyAndPredicateBlock(BBI, *CvtBBI, Cond);
850 PredicateBlock(*CvtBBI, CvtBBI->BB->end(), Cond);
852 // Merge converted block into entry block.
853 BBI.NonPredSize -= TII->RemoveBranch(*BBI.BB);
854 MergeBlocks(BBI, *CvtBBI);
857 bool IterIfcvt = true;
858 if (!canFallThroughTo(BBI.BB, NextBBI->BB)) {
859 InsertUncondBranch(BBI.BB, NextBBI->BB, TII);
860 BBI.HasFallThrough = false;
861 // Now ifcvt'd block will look like this:
868 // We cannot further ifcvt this block because the unconditional branch
869 // will have to be predicated on the new condition, that will not be
870 // available if cmp executes.
874 RemoveExtraEdges(BBI);
876 // Update block info. BB can be iteratively if-converted.
879 InvalidatePreds(BBI.BB);
880 CvtBBI->IsDone = true;
882 // FIXME: Must maintain LiveIns.
886 /// IfConvertTriangle - If convert a triangle sub-CFG.
888 bool IfConverter::IfConvertTriangle(BBInfo &BBI, IfcvtKind Kind) {
889 BBInfo &TrueBBI = BBAnalysis[BBI.TrueBB->getNumber()];
890 BBInfo &FalseBBI = BBAnalysis[BBI.FalseBB->getNumber()];
891 BBInfo *CvtBBI = &TrueBBI;
892 BBInfo *NextBBI = &FalseBBI;
894 std::vector<MachineOperand> Cond(BBI.BrCond);
895 if (Kind == ICTriangleFalse || Kind == ICTriangleFRev)
896 std::swap(CvtBBI, NextBBI);
898 if (CvtBBI->IsDone ||
899 (CvtBBI->CannotBeCopied && CvtBBI->BB->pred_size() > 1)) {
900 // Something has changed. It's no longer safe to predicate this block.
901 BBI.IsAnalyzed = false;
902 CvtBBI->IsAnalyzed = false;
906 if (Kind == ICTriangleFalse || Kind == ICTriangleFRev)
907 TII->ReverseBranchCondition(Cond);
909 if (Kind == ICTriangleRev || Kind == ICTriangleFRev) {
910 ReverseBranchCondition(*CvtBBI);
911 // BB has been changed, modify its predecessors (except for this
912 // one) so they don't get ifcvt'ed based on bad intel.
913 for (MachineBasicBlock::pred_iterator PI = CvtBBI->BB->pred_begin(),
914 E = CvtBBI->BB->pred_end(); PI != E; ++PI) {
915 MachineBasicBlock *PBB = *PI;
918 BBInfo &PBBI = BBAnalysis[PBB->getNumber()];
919 if (PBBI.IsEnqueued) {
920 PBBI.IsAnalyzed = false;
921 PBBI.IsEnqueued = false;
926 bool HasEarlyExit = CvtBBI->FalseBB != NULL;
927 bool DupBB = CvtBBI->BB->pred_size() > 1;
929 BBI.NonPredSize -= TII->RemoveBranch(*BBI.BB);
930 // Copy instructions in the true block, predicate them add them to
932 CopyAndPredicateBlock(BBI, *CvtBBI, Cond, true);
934 // Predicate the 'true' block after removing its branch.
935 CvtBBI->NonPredSize -= TII->RemoveBranch(*CvtBBI->BB);
936 PredicateBlock(*CvtBBI, CvtBBI->BB->end(), Cond);
939 // If 'true' block has a 'false' successor, add an exit branch to it.
941 std::vector<MachineOperand> RevCond(CvtBBI->BrCond);
942 if (TII->ReverseBranchCondition(RevCond))
943 assert(false && "Unable to reverse branch condition!");
945 TII->InsertBranch(*BBI.BB, CvtBBI->FalseBB, NULL, RevCond);
946 BBI.BB->addSuccessor(CvtBBI->FalseBB);
948 TII->InsertBranch(*CvtBBI->BB, CvtBBI->FalseBB, NULL, RevCond);
953 // Now merge the entry of the triangle with the true block.
954 BBI.NonPredSize -= TII->RemoveBranch(*BBI.BB);
955 MergeBlocks(BBI, *CvtBBI);
958 // Merge in the 'false' block if the 'false' block has no other
959 // predecessors. Otherwise, add a unconditional branch from to 'false'.
960 bool FalseBBDead = false;
961 bool IterIfcvt = true;
962 bool isFallThrough = canFallThroughTo(BBI.BB, NextBBI->BB);
963 if (!isFallThrough) {
964 // Only merge them if the true block does not fallthrough to the false
965 // block. By not merging them, we make it possible to iteratively
968 NextBBI->BB->pred_size() == 1 && !NextBBI->HasFallThrough) {
969 MergeBlocks(BBI, *NextBBI);
972 InsertUncondBranch(BBI.BB, NextBBI->BB, TII);
973 BBI.HasFallThrough = false;
975 // Mixed predicated and unpredicated code. This cannot be iteratively
980 RemoveExtraEdges(BBI);
982 // Update block info. BB can be iteratively if-converted.
985 InvalidatePreds(BBI.BB);
986 CvtBBI->IsDone = true;
988 NextBBI->IsDone = true;
990 // FIXME: Must maintain LiveIns.
994 /// IfConvertDiamond - If convert a diamond sub-CFG.
996 bool IfConverter::IfConvertDiamond(BBInfo &BBI, IfcvtKind Kind,
997 unsigned NumDups1, unsigned NumDups2) {
998 BBInfo &TrueBBI = BBAnalysis[BBI.TrueBB->getNumber()];
999 BBInfo &FalseBBI = BBAnalysis[BBI.FalseBB->getNumber()];
1000 MachineBasicBlock *TailBB = TrueBBI.TrueBB;
1001 // True block must fall through or ended with unanalyzable terminator.
1003 if (blockAlwaysFallThrough(TrueBBI))
1004 TailBB = FalseBBI.TrueBB;
1005 assert((TailBB || !TrueBBI.IsBrAnalyzable) && "Unexpected!");
1008 if (TrueBBI.IsDone || FalseBBI.IsDone ||
1009 TrueBBI.BB->pred_size() > 1 ||
1010 FalseBBI.BB->pred_size() > 1) {
1011 // Something has changed. It's no longer safe to predicate these blocks.
1012 BBI.IsAnalyzed = false;
1013 TrueBBI.IsAnalyzed = false;
1014 FalseBBI.IsAnalyzed = false;
1018 // Merge the 'true' and 'false' blocks by copying the instructions
1019 // from the 'false' block to the 'true' block. That is, unless the true
1020 // block would clobber the predicate, in that case, do the opposite.
1021 BBInfo *BBI1 = &TrueBBI;
1022 BBInfo *BBI2 = &FalseBBI;
1023 std::vector<MachineOperand> RevCond(BBI.BrCond);
1024 TII->ReverseBranchCondition(RevCond);
1025 std::vector<MachineOperand> *Cond1 = &BBI.BrCond;
1026 std::vector<MachineOperand> *Cond2 = &RevCond;
1027 bool NeedBr1 = BBI1->FalseBB != NULL;
1028 bool NeedBr2 = BBI2->FalseBB != NULL;
1030 // Figure out the more profitable ordering.
1031 bool DoSwap = false;
1032 if (TrueBBI.ClobbersPred && !FalseBBI.ClobbersPred)
1034 else if (TrueBBI.ClobbersPred == FalseBBI.ClobbersPred) {
1035 if (!NeedBr1 && NeedBr2)
1037 else if (NeedBr1 == NeedBr2) {
1038 if (TrueBBI.NonPredSize > FalseBBI.NonPredSize)
1043 std::swap(BBI1, BBI2);
1044 std::swap(Cond1, Cond2);
1045 std::swap(NeedBr1, NeedBr2);
1048 // Remove the conditional branch from entry to the blocks.
1049 BBI.NonPredSize -= TII->RemoveBranch(*BBI.BB);
1051 // Remove the duplicated instructions at the beginnings of both paths.
1052 MachineBasicBlock::iterator DI1 = BBI1->BB->begin();
1053 MachineBasicBlock::iterator DI2 = BBI2->BB->begin();
1054 BBI1->NonPredSize -= NumDups1;
1055 BBI2->NonPredSize -= NumDups1;
1056 while (NumDups1 != 0) {
1061 BBI.BB->splice(BBI.BB->end(), BBI1->BB, BBI1->BB->begin(), DI1);
1062 BBI2->BB->erase(BBI2->BB->begin(), DI2);
1064 // Predicate the 'true' block after removing its branch.
1065 BBI1->NonPredSize -= TII->RemoveBranch(*BBI1->BB);
1066 DI1 = BBI1->BB->end();
1067 for (unsigned i = 0; i != NumDups2; ++i)
1069 BBI1->BB->erase(DI1, BBI1->BB->end());
1070 PredicateBlock(*BBI1, BBI1->BB->end(), *Cond1);
1072 // Add an early exit branch if needed.
1074 TII->InsertBranch(*BBI1->BB, BBI1->FalseBB, NULL, *Cond1);
1076 // Predicate the 'false' block.
1077 BBI2->NonPredSize -= TII->RemoveBranch(*BBI2->BB);
1078 DI2 = BBI2->BB->end();
1079 while (NumDups2 != 0) {
1083 PredicateBlock(*BBI2, DI2, *Cond2);
1085 // Add an unconditional branch from 'false' to to 'false' successor if it
1086 // will not be the fallthrough block.
1087 if (NeedBr2 && !NeedBr1) {
1088 // If BBI2 isn't going to be merged in, then the existing fallthrough
1089 // or branch is fine.
1090 if (!canFallThroughTo(BBI.BB, BBI2->FalseBB)) {
1091 InsertUncondBranch(BBI2->BB, BBI2->FalseBB, TII);
1092 BBI2->HasFallThrough = false;
1096 // Keep them as two separate blocks if there is an early exit.
1098 MergeBlocks(*BBI1, *BBI2);
1100 // Merge the combined block into the entry of the diamond.
1101 MergeBlocks(BBI, *BBI1);
1103 // 'True' and 'false' aren't combined, see if we need to add a unconditional
1104 // branch to the 'false' block.
1105 if (NeedBr1 && !canFallThroughTo(BBI.BB, BBI2->BB)) {
1106 InsertUncondBranch(BBI.BB, BBI2->BB, TII);
1107 BBI1->HasFallThrough = false;
1110 // If the if-converted block fallthrough or unconditionally branch into the
1111 // tail block, and the tail block does not have other predecessors, then
1112 // fold the tail block in as well. Otherwise, unless it falls through to the
1113 // tail, add a unconditional branch to it.
1115 BBInfo TailBBI = BBAnalysis[TailBB->getNumber()];
1116 BBInfo *LastBBI = NeedBr1 ? BBI2 : &BBI;
1117 bool HasEarlyExit = NeedBr1 ? NeedBr2 : false;
1118 if (!HasEarlyExit &&
1119 TailBB->pred_size() == 1 && !TailBBI.HasFallThrough) {
1120 LastBBI->NonPredSize -= TII->RemoveBranch(*LastBBI->BB);
1121 MergeBlocks(*LastBBI, TailBBI);
1122 TailBBI.IsDone = true;
1124 bool isFallThrough = canFallThroughTo(LastBBI->BB, TailBB);
1125 if (!isFallThrough) {
1126 InsertUncondBranch(LastBBI->BB, TailBB, TII);
1127 LastBBI->HasFallThrough = false;
1132 RemoveExtraEdges(BBI);
1134 // Update block info.
1135 BBI.IsDone = TrueBBI.IsDone = FalseBBI.IsDone = true;
1136 InvalidatePreds(BBI.BB);
1138 // FIXME: Must maintain LiveIns.
1142 /// PredicateBlock - Predicate instructions from the start of the block to the
1143 /// specified end with the specified condition.
1144 void IfConverter::PredicateBlock(BBInfo &BBI,
1145 MachineBasicBlock::iterator E,
1146 std::vector<MachineOperand> &Cond) {
1147 for (MachineBasicBlock::iterator I = BBI.BB->begin(); I != E; ++I) {
1148 if (TII->isPredicated(I))
1150 if (!TII->PredicateInstruction(I, Cond)) {
1151 cerr << "Unable to predicate " << *I << "!\n";
1156 std::copy(Cond.begin(), Cond.end(), std::back_inserter(BBI.Predicate));
1158 BBI.IsAnalyzed = false;
1159 BBI.NonPredSize = 0;
1164 /// CopyAndPredicateBlock - Copy and predicate instructions from source BB to
1165 /// the destination block. Skip end of block branches if IgnoreBr is true.
1166 void IfConverter::CopyAndPredicateBlock(BBInfo &ToBBI, BBInfo &FromBBI,
1167 std::vector<MachineOperand> &Cond,
1169 for (MachineBasicBlock::iterator I = FromBBI.BB->begin(),
1170 E = FromBBI.BB->end(); I != E; ++I) {
1171 const TargetInstrDescriptor *TID = I->getInstrDescriptor();
1172 bool isPredicated = TII->isPredicated(I);
1173 // Do not copy the end of the block branches.
1174 if (IgnoreBr && !isPredicated && (TID->Flags & M_BRANCH_FLAG) != 0)
1177 MachineInstr *MI = I->clone();
1178 ToBBI.BB->insert(ToBBI.BB->end(), MI);
1179 ToBBI.NonPredSize++;
1182 if (!TII->PredicateInstruction(MI, Cond)) {
1183 cerr << "Unable to predicate " << *MI << "!\n";
1188 std::copy(FromBBI.Predicate.begin(), FromBBI.Predicate.end(),
1189 std::back_inserter(ToBBI.Predicate));
1190 std::copy(Cond.begin(), Cond.end(), std::back_inserter(ToBBI.Predicate));
1192 ToBBI.ClobbersPred |= FromBBI.ClobbersPred;
1193 ToBBI.IsAnalyzed = false;
1198 /// MergeBlocks - Move all instructions from FromBB to the end of ToBB.
1200 void IfConverter::MergeBlocks(BBInfo &ToBBI, BBInfo &FromBBI) {
1201 ToBBI.BB->splice(ToBBI.BB->end(),
1202 FromBBI.BB, FromBBI.BB->begin(), FromBBI.BB->end());
1204 // Redirect all branches to FromBB to ToBB.
1205 std::vector<MachineBasicBlock *> Preds(FromBBI.BB->pred_begin(),
1206 FromBBI.BB->pred_end());
1207 for (unsigned i = 0, e = Preds.size(); i != e; ++i) {
1208 MachineBasicBlock *Pred = Preds[i];
1209 if (Pred == ToBBI.BB)
1211 Pred->ReplaceUsesOfBlockWith(FromBBI.BB, ToBBI.BB);
1214 std::vector<MachineBasicBlock *> Succs(FromBBI.BB->succ_begin(),
1215 FromBBI.BB->succ_end());
1216 MachineBasicBlock *NBB = getNextBlock(FromBBI.BB);
1217 MachineBasicBlock *FallThrough = FromBBI.HasFallThrough ? NBB : NULL;
1219 for (unsigned i = 0, e = Succs.size(); i != e; ++i) {
1220 MachineBasicBlock *Succ = Succs[i];
1221 // Fallthrough edge can't be transferred.
1222 if (Succ == FallThrough)
1224 FromBBI.BB->removeSuccessor(Succ);
1225 if (!ToBBI.BB->isSuccessor(Succ))
1226 ToBBI.BB->addSuccessor(Succ);
1229 // Now FromBBI always fall through to the next block!
1231 FromBBI.BB->addSuccessor(NBB);
1233 std::copy(FromBBI.Predicate.begin(), FromBBI.Predicate.end(),
1234 std::back_inserter(ToBBI.Predicate));
1235 FromBBI.Predicate.clear();
1237 ToBBI.NonPredSize += FromBBI.NonPredSize;
1238 FromBBI.NonPredSize = 0;
1240 ToBBI.ClobbersPred |= FromBBI.ClobbersPred;
1241 ToBBI.HasFallThrough = FromBBI.HasFallThrough;
1242 ToBBI.IsAnalyzed = false;
1243 FromBBI.IsAnalyzed = false;