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"
29 // Hidden options for help debugging.
30 cl::opt<int> IfCvtFnStart("ifcvt-fn-start", cl::init(-1), cl::Hidden);
31 cl::opt<int> IfCvtFnStop("ifcvt-fn-stop", cl::init(-1), cl::Hidden);
32 cl::opt<int> IfCvtLimit("ifcvt-limit", cl::init(-1), cl::Hidden);
33 cl::opt<bool> DisableSimple("disable-ifcvt-simple",
34 cl::init(false), cl::Hidden);
35 cl::opt<bool> DisableSimpleF("disable-ifcvt-simple-false",
36 cl::init(false), cl::Hidden);
37 cl::opt<bool> DisableTriangle("disable-ifcvt-triangle",
38 cl::init(false), cl::Hidden);
39 cl::opt<bool> DisableTriangleF("disable-ifcvt-triangle-false",
40 cl::init(false), cl::Hidden);
41 cl::opt<bool> DisableTriangleFR("disable-ifcvt-triangle-false-rev",
42 cl::init(false), cl::Hidden);
43 cl::opt<bool> DisableDiamond("disable-ifcvt-diamond",
44 cl::init(false), cl::Hidden);
47 STATISTIC(NumSimple, "Number of simple if-conversions performed");
48 STATISTIC(NumSimpleFalse, "Number of simple (F) if-conversions performed");
49 STATISTIC(NumTriangle, "Number of triangle if-conversions performed");
50 STATISTIC(NumTriangleFalse,"Number of triangle (F) if-conversions performed");
51 STATISTIC(NumTriangleFRev, "Number of triangle (F/R) if-conversions performed");
52 STATISTIC(NumDiamonds, "Number of diamond if-conversions performed");
53 STATISTIC(NumIfConvBBs, "Number of if-converted blocks");
56 class IfConverter : public MachineFunctionPass {
58 ICNotClassfied, // BB data valid, but not classified.
59 ICSimple, // BB is entry of an one split, no rejoin sub-CFG.
60 ICSimpleFalse, // Same as ICSimple, but on the false path.
61 ICTriangle, // BB is entry of a triangle sub-CFG.
62 ICTriangleFalse, // Same as ICTriangle, but on the false path.
63 ICTriangleFRev, // Same as ICTriangleFalse, but false path rev condition.
64 ICDiamond // BB is entry of a diamond sub-CFG.
67 /// BBInfo - One per MachineBasicBlock, this is used to cache the result
68 /// if-conversion feasibility analysis. This includes results from
69 /// TargetInstrInfo::AnalyzeBranch() (i.e. TBB, FBB, and Cond), and its
70 /// classification, and common tail block of its successors (if it's a
71 /// diamond shape), its size, whether it's predicable, and whether any
72 /// instruction can clobber the 'would-be' predicate.
74 /// Kind - Type of block. See BBICKind.
75 /// IsDone - True if BB is not to be considered for ifcvt.
76 /// IsBeingAnalyzed - True if BB is currently being analyzed.
77 /// IsAnalyzed - True if BB has been analyzed (info is still valid).
78 /// IsEnqueued - True if BB has been enqueued to be ifcvt'ed.
79 /// IsBrAnalyzable - True if AnalyzeBranch() returns false.
80 /// HasFallThrough - True if BB may fallthrough to the following BB.
81 /// IsUnpredicable - True if BB is known to be unpredicable.
82 /// ClobbersPredicate- True if BB would modify the predicate (e.g. has
84 /// NonPredSize - Number of non-predicated instructions.
85 /// BB - Corresponding MachineBasicBlock.
86 /// TrueBB / FalseBB- See AnalyzeBranch().
87 /// BrCond - Conditions for end of block conditional branches.
88 /// Predicate - Predicate used in the BB.
92 bool IsBeingAnalyzed : 1;
95 bool IsBrAnalyzable : 1;
96 bool HasFallThrough : 1;
97 bool IsUnpredicable : 1;
98 bool ClobbersPred : 1;
100 MachineBasicBlock *BB;
101 MachineBasicBlock *TrueBB;
102 MachineBasicBlock *FalseBB;
103 MachineBasicBlock *TailBB;
104 std::vector<MachineOperand> BrCond;
105 std::vector<MachineOperand> Predicate;
106 BBInfo() : Kind(ICNotClassfied), IsDone(false), IsBeingAnalyzed(false),
107 IsAnalyzed(false), IsEnqueued(false), IsBrAnalyzable(false),
108 HasFallThrough(false), IsUnpredicable(false),
109 ClobbersPred(false), NonPredSize(0),
110 BB(0), TrueBB(0), FalseBB(0), TailBB(0) {}
113 /// Roots - Basic blocks that do not have successors. These are the starting
114 /// points of Graph traversal.
115 std::vector<MachineBasicBlock*> Roots;
117 /// BBAnalysis - Results of if-conversion feasibility analysis indexed by
118 /// basic block number.
119 std::vector<BBInfo> BBAnalysis;
121 const TargetLowering *TLI;
122 const TargetInstrInfo *TII;
126 IfConverter() : MachineFunctionPass((intptr_t)&ID) {}
128 virtual bool runOnMachineFunction(MachineFunction &MF);
129 virtual const char *getPassName() const { return "If converter"; }
132 bool ReverseBranchCondition(BBInfo &BBI);
133 bool ValidSimple(BBInfo &TrueBBI) const;
134 bool ValidTriangle(BBInfo &TrueBBI, BBInfo &FalseBBI,
135 bool FalseBranch = false) const;
136 bool ValidDiamond(BBInfo &TrueBBI, BBInfo &FalseBBI) const;
137 void ScanInstructions(BBInfo &BBI);
138 BBInfo &AnalyzeBlock(MachineBasicBlock *BB);
139 bool FeasibilityAnalysis(BBInfo &BBI, std::vector<MachineOperand> &Cond,
140 bool isTriangle = false, bool RevBranch = false);
141 bool AttemptRestructuring(BBInfo &BBI);
142 bool AnalyzeBlocks(MachineFunction &MF,
143 std::vector<BBInfo*> &Candidates);
144 void ReTryPreds(MachineBasicBlock *BB);
145 void RemoveExtraEdges(BBInfo &BBI);
146 bool IfConvertSimple(BBInfo &BBI);
147 bool IfConvertTriangle(BBInfo &BBI);
148 bool IfConvertDiamond(BBInfo &BBI);
149 void PredicateBlock(BBInfo &BBI,
150 std::vector<MachineOperand> &Cond,
151 bool IgnoreTerm = false);
152 void MergeBlocks(BBInfo &TrueBBI, BBInfo &FalseBBI);
154 // blockAlwaysFallThrough - Block ends without a terminator.
155 bool blockAlwaysFallThrough(BBInfo &BBI) const {
156 return BBI.IsBrAnalyzable && BBI.TrueBB == NULL;
159 // IfcvtCandidateCmp - Used to sort if-conversion candidates.
160 static bool IfcvtCandidateCmp(BBInfo* C1, BBInfo* C2){
161 // Favor diamond over triangle, etc.
162 return (unsigned)C1->Kind < (unsigned)C2->Kind;
165 char IfConverter::ID = 0;
168 FunctionPass *llvm::createIfConverterPass() { return new IfConverter(); }
170 bool IfConverter::runOnMachineFunction(MachineFunction &MF) {
171 TLI = MF.getTarget().getTargetLowering();
172 TII = MF.getTarget().getInstrInfo();
173 if (!TII) return false;
175 static int FnNum = -1;
176 DOUT << "\nIfcvt: function (" << ++FnNum << ") \'"
177 << MF.getFunction()->getName() << "\'";
179 if (FnNum < IfCvtFnStart || (IfCvtFnStop != -1 && FnNum > IfCvtFnStop)) {
180 DOUT << " skipped\n";
186 BBAnalysis.resize(MF.getNumBlockIDs());
188 // Look for root nodes, i.e. blocks without successors.
189 for (MachineFunction::iterator I = MF.begin(), E = MF.end(); I != E; ++I)
190 if (I->succ_size() == 0)
193 std::vector<BBInfo*> Candidates;
195 while (IfCvtLimit == -1 || (int)NumIfConvBBs < IfCvtLimit) {
196 // Do an intial analysis for each basic block and finding all the potential
197 // candidates to perform if-convesion.
198 bool Change = AnalyzeBlocks(MF, Candidates);
199 while (!Candidates.empty()) {
200 BBInfo &BBI = *Candidates.back();
201 Candidates.pop_back();
203 // If the block has been evicted out of the queue or it has already been
204 // marked dead (due to it being predicated), then skip it.
205 if (!BBI.IsEnqueued || BBI.IsDone)
210 default: assert(false && "Unexpected!");
213 case ICSimpleFalse: {
214 bool isFalse = BBI.Kind == ICSimpleFalse;
215 if ((isFalse && DisableSimpleF) || (!isFalse && DisableSimple)) break;
216 DOUT << "Ifcvt (Simple" << (BBI.Kind == ICSimpleFalse ? " false" : "")
217 << "): BB#" << BBI.BB->getNumber() << " ("
218 << ((BBI.Kind == ICSimpleFalse)
219 ? BBI.FalseBB->getNumber() : BBI.TrueBB->getNumber()) << ") ";
220 RetVal = IfConvertSimple(BBI);
221 DOUT << (RetVal ? "succeeded!" : "failed!") << "\n";
223 if (isFalse) NumSimpleFalse++;
228 case ICTriangleFalse:
229 case ICTriangleFRev: {
230 bool isFalse = BBI.Kind == ICTriangleFalse;
231 bool isFalseRev = BBI.Kind == ICTriangleFRev;
232 if (DisableTriangle && !isFalse && !isFalseRev) break;
233 if (DisableTriangleF && isFalse) break;
234 if (DisableTriangleFR && isFalseRev) break;
235 DOUT << "Ifcvt (Triangle";
239 DOUT << " false/rev";
240 DOUT << "): BB#" << BBI.BB->getNumber() << " (T:"
241 << BBI.TrueBB->getNumber() << ",F:" << BBI.FalseBB->getNumber()
243 RetVal = IfConvertTriangle(BBI);
244 DOUT << (RetVal ? "succeeded!" : "failed!") << "\n";
246 if (isFalseRev) NumTriangleFRev++;
247 else if (isFalse) NumTriangleFalse++;
253 if (DisableDiamond) break;
254 DOUT << "Ifcvt (Diamond): BB#" << BBI.BB->getNumber() << " (T:"
255 << BBI.TrueBB->getNumber() << ",F:" << BBI.FalseBB->getNumber();
257 DOUT << "," << BBI.TailBB->getNumber() ;
259 RetVal = IfConvertDiamond(BBI);
260 DOUT << (RetVal ? "succeeded!" : "failed!") << "\n";
261 if (RetVal) NumDiamonds++;
266 if (IfCvtLimit != -1 && (int)NumIfConvBBs >= IfCvtLimit)
272 MadeChange |= Change;
281 static MachineBasicBlock *findFalseBlock(MachineBasicBlock *BB,
282 MachineBasicBlock *TrueBB) {
283 for (MachineBasicBlock::succ_iterator SI = BB->succ_begin(),
284 E = BB->succ_end(); SI != E; ++SI) {
285 MachineBasicBlock *SuccBB = *SI;
286 if (SuccBB != TrueBB)
292 bool IfConverter::ReverseBranchCondition(BBInfo &BBI) {
293 if (!TII->ReverseBranchCondition(BBI.BrCond)) {
294 TII->RemoveBranch(*BBI.BB);
295 TII->InsertBranch(*BBI.BB, BBI.FalseBB, BBI.TrueBB, BBI.BrCond);
296 std::swap(BBI.TrueBB, BBI.FalseBB);
302 /// getNextBlock - Returns the next block in the function blocks ordering. If
303 /// it is the end, returns NULL.
304 static inline MachineBasicBlock *getNextBlock(MachineBasicBlock *BB) {
305 MachineFunction::iterator I = BB;
306 MachineFunction::iterator E = BB->getParent()->end();
312 /// ValidSimple - Returns true if the 'true' block (along with its
313 /// predecessor) forms a valid simple shape for ifcvt.
314 bool IfConverter::ValidSimple(BBInfo &TrueBBI) const {
315 if (TrueBBI.IsBeingAnalyzed)
318 return !blockAlwaysFallThrough(TrueBBI) &&
319 TrueBBI.BrCond.size() == 0 && TrueBBI.BB->pred_size() == 1;
322 /// ValidTriangle - Returns true if the 'true' and 'false' blocks (along
323 /// with their common predecessor) forms a valid triangle shape for ifcvt.
324 bool IfConverter::ValidTriangle(BBInfo &TrueBBI, BBInfo &FalseBBI,
325 bool FalseBranch) const {
326 if (TrueBBI.IsBeingAnalyzed)
329 if (TrueBBI.BB->pred_size() != 1)
332 MachineBasicBlock *TExit = FalseBranch ? TrueBBI.FalseBB : TrueBBI.TrueBB;
333 if (!TExit && blockAlwaysFallThrough(TrueBBI)) {
334 MachineFunction::iterator I = TrueBBI.BB;
335 if (++I == TrueBBI.BB->getParent()->end())
339 return TExit && TExit == FalseBBI.BB;
342 /// ValidDiamond - Returns true if the 'true' and 'false' blocks (along
343 /// with their common predecessor) forms a valid diamond shape for ifcvt.
344 bool IfConverter::ValidDiamond(BBInfo &TrueBBI, BBInfo &FalseBBI) const {
345 if (TrueBBI.IsBeingAnalyzed || FalseBBI.IsBeingAnalyzed)
348 MachineBasicBlock *TT = TrueBBI.TrueBB;
349 MachineBasicBlock *FT = FalseBBI.TrueBB;
351 if (!TT && blockAlwaysFallThrough(TrueBBI))
352 TT = getNextBlock(TrueBBI.BB);
353 if (!FT && blockAlwaysFallThrough(FalseBBI))
354 FT = getNextBlock(FalseBBI.BB);
357 if (TT == NULL && (TrueBBI.IsBrAnalyzable || FalseBBI.IsBrAnalyzable))
359 // FIXME: Allow false block to have an early exit?
360 return (TrueBBI.BB->pred_size() == 1 &&
361 FalseBBI.BB->pred_size() == 1 &&
362 !TrueBBI.FalseBB && !FalseBBI.FalseBB);
365 /// ScanInstructions - Scan all the instructions in the block to determine if
366 /// the block is predicable. In most cases, that means all the instructions
367 /// in the block has M_PREDICABLE flag. Also checks if the block contains any
368 /// instruction which can clobber a predicate (e.g. condition code register).
369 /// If so, the block is not predicable unless it's the last instruction.
370 void IfConverter::ScanInstructions(BBInfo &BBI) {
374 // First analyze the end of BB branches.
375 BBI.TrueBB = BBI.FalseBB;
378 !TII->AnalyzeBranch(*BBI.BB, BBI.TrueBB, BBI.FalseBB, BBI.BrCond);
379 BBI.HasFallThrough = BBI.IsBrAnalyzable && BBI.FalseBB == NULL;
381 if (BBI.BrCond.size()) {
382 // No false branch. This BB must end with a conditional branch and a
385 BBI.FalseBB = findFalseBlock(BBI.BB, BBI.TrueBB);
386 assert(BBI.FalseBB && "Expected to find the fallthrough block!");
389 // Then scan all the instructions.
391 BBI.ClobbersPred = false;
392 bool SeenCondBr = false;
393 for (MachineBasicBlock::iterator I = BBI.BB->begin(), E = BBI.BB->end();
395 const TargetInstrDescriptor *TID = I->getInstrDescriptor();
396 bool isPredicated = TII->isPredicated(I);
397 bool isCondBr = BBI.IsBrAnalyzable &&
398 (TID->Flags & M_BRANCH_FLAG) != 0 && (TID->Flags & M_BARRIER_FLAG) == 0;
400 if (!isPredicated && !isCondBr)
403 if (BBI.ClobbersPred && !isPredicated) {
404 // Predicate modification instruction should end the block (except for
405 // already predicated instructions and end of block branches).
409 // Conditional branches is not predicable. But it may be eliminated.
413 // Predicate may have been modified, the subsequent (currently)
414 // unpredocated instructions cannot be correctly predicated.
415 BBI.IsUnpredicable = true;
419 if (TID->Flags & M_CLOBBERS_PRED)
420 BBI.ClobbersPred = true;
422 if (!I->isPredicable()) {
423 BBI.IsUnpredicable = true;
429 /// FeasibilityAnalysis - Determine if the block is a suitable candidate to be
430 /// predicated by the specified predicate.
431 bool IfConverter::FeasibilityAnalysis(BBInfo &BBI,
432 std::vector<MachineOperand> &Pred,
433 bool isTriangle, bool RevBranch) {
434 // Forget about it if it's unpredicable.
435 if (BBI.IsUnpredicable)
438 // If the block is dead, or it is going to be the entry block of a sub-CFG
439 // that will be if-converted, then it cannot be predicated.
440 if (BBI.IsDone || BBI.IsEnqueued)
443 // Check predication threshold.
444 if (BBI.NonPredSize == 0 || BBI.NonPredSize > TLI->getIfCvtBlockSizeLimit())
447 // If it is already predicated, check if its predicate subsumes the new
449 if (BBI.Predicate.size() && !TII->SubsumesPredicate(BBI.Predicate, Pred))
452 if (BBI.BrCond.size()) {
456 // Test predicate subsumsion.
457 std::vector<MachineOperand> RevPred(Pred);
458 std::vector<MachineOperand> Cond(BBI.BrCond);
460 if (TII->ReverseBranchCondition(Cond))
463 if (TII->ReverseBranchCondition(RevPred) ||
464 !TII->SubsumesPredicate(Cond, RevPred))
471 /// AnalyzeBlock - Analyze the structure of the sub-CFG starting from
472 /// the specified block. Record its successors and whether it looks like an
473 /// if-conversion candidate.
474 IfConverter::BBInfo &IfConverter::AnalyzeBlock(MachineBasicBlock *BB) {
475 BBInfo &BBI = BBAnalysis[BB->getNumber()];
477 if (BBI.IsAnalyzed || BBI.IsBeingAnalyzed)
481 BBI.IsBeingAnalyzed = true;
482 BBI.Kind = ICNotClassfied;
484 ScanInstructions(BBI);
486 // Unanalyable or ends with fallthrough or unconditional branch.
487 if (!BBI.IsBrAnalyzable || BBI.BrCond.size() == 0) {
488 BBI.IsBeingAnalyzed = false;
489 BBI.IsAnalyzed = true;
493 // Do not ifcvt if either path is a back edge to the entry block.
494 if (BBI.TrueBB == BB || BBI.FalseBB == BB) {
495 BBI.IsBeingAnalyzed = false;
496 BBI.IsAnalyzed = true;
500 BBInfo &TrueBBI = AnalyzeBlock(BBI.TrueBB);
501 BBInfo &FalseBBI = AnalyzeBlock(BBI.FalseBB);
503 if (TrueBBI.IsDone && FalseBBI.IsDone) {
504 BBI.IsBeingAnalyzed = false;
505 BBI.IsAnalyzed = true;
509 std::vector<MachineOperand> RevCond(BBI.BrCond);
510 bool CanRevCond = !TII->ReverseBranchCondition(RevCond);
512 if (CanRevCond && ValidDiamond(TrueBBI, FalseBBI) &&
513 !(TrueBBI.ClobbersPred && FalseBBI.ClobbersPred) &&
514 FeasibilityAnalysis(TrueBBI, BBI.BrCond) &&
515 FeasibilityAnalysis(FalseBBI, RevCond)) {
523 // Note TailBB can be empty.
524 BBI.Kind = ICDiamond;
525 BBI.TailBB = TrueBBI.TrueBB;
527 // FIXME: Consider duplicating if BB is small.
528 if (ValidTriangle(TrueBBI, FalseBBI) &&
529 FeasibilityAnalysis(TrueBBI, BBI.BrCond, true)) {
537 BBI.Kind = ICTriangle;
538 } else if (ValidSimple(TrueBBI) &&
539 FeasibilityAnalysis(TrueBBI, BBI.BrCond)) {
540 // Simple (split, no rejoin):
548 } else if (CanRevCond) {
549 // Try the other path...
550 if (ValidTriangle(FalseBBI, TrueBBI) &&
551 FeasibilityAnalysis(FalseBBI, RevCond, true)) {
552 BBI.Kind = ICTriangleFalse;
553 } else if (ValidTriangle(FalseBBI, TrueBBI, true) &&
554 FeasibilityAnalysis(FalseBBI, RevCond, true, true)) {
555 BBI.Kind = ICTriangleFRev;
556 } else if (ValidSimple(FalseBBI) &&
557 FeasibilityAnalysis(FalseBBI, RevCond)) {
558 BBI.Kind = ICSimpleFalse;
563 BBI.IsBeingAnalyzed = false;
564 BBI.IsAnalyzed = true;
568 /// AttemptRestructuring - Restructure the sub-CFG rooted in the given block to
569 /// expose more if-conversion opportunities. e.g.
591 bool IfConverter::AttemptRestructuring(BBInfo &BBI) {
595 /// AnalyzeBlocks - Analyze all blocks and find entries for all if-conversion
596 /// candidates. It returns true if any CFG restructuring is done to expose more
597 /// if-conversion opportunities.
598 bool IfConverter::AnalyzeBlocks(MachineFunction &MF,
599 std::vector<BBInfo*> &Candidates) {
601 std::set<MachineBasicBlock*> Visited;
602 for (unsigned i = 0, e = Roots.size(); i != e; ++i) {
603 for (idf_ext_iterator<MachineBasicBlock*> I=idf_ext_begin(Roots[i],Visited),
604 E = idf_ext_end(Roots[i], Visited); I != E; ++I) {
605 MachineBasicBlock *BB = *I;
606 BBInfo &BBI = AnalyzeBlock(BB);
612 Candidates.push_back(&BBI);
615 Change |= AttemptRestructuring(BBI);
621 // Sort to favor more complex ifcvt scheme.
622 std::stable_sort(Candidates.begin(), Candidates.end(), IfcvtCandidateCmp);
627 /// canFallThroughTo - Returns true either if ToBB is the next block after BB or
628 /// that all the intervening blocks are empty (given BB can fall through to its
630 static bool canFallThroughTo(MachineBasicBlock *BB, MachineBasicBlock *ToBB) {
631 MachineFunction::iterator I = BB;
632 MachineFunction::iterator TI = ToBB;
633 MachineFunction::iterator E = BB->getParent()->end();
635 if (I == E || !I->empty())
640 /// ReTryPreds - Invalidate predecessor BB info so it would be re-analyzed
641 /// to determine if it can be if-converted.
642 void IfConverter::ReTryPreds(MachineBasicBlock *BB) {
643 for (MachineBasicBlock::pred_iterator PI = BB->pred_begin(),
644 E = BB->pred_end(); PI != E; ++PI) {
645 BBInfo &PBBI = BBAnalysis[(*PI)->getNumber()];
646 if (!PBBI.IsDone && PBBI.Kind == ICNotClassfied) {
647 assert(PBBI.IsEnqueued && "Unexpected");
648 PBBI.IsAnalyzed = false;
653 /// InsertUncondBranch - Inserts an unconditional branch from BB to ToBB.
655 static void InsertUncondBranch(MachineBasicBlock *BB, MachineBasicBlock *ToBB,
656 const TargetInstrInfo *TII) {
657 std::vector<MachineOperand> NoCond;
658 TII->InsertBranch(*BB, ToBB, NULL, NoCond);
661 /// RemoveExtraEdges - Remove true / false edges if either / both are no longer
663 void IfConverter::RemoveExtraEdges(BBInfo &BBI) {
664 MachineBasicBlock *TBB = NULL, *FBB = NULL;
665 std::vector<MachineOperand> Cond;
666 bool isAnalyzable = !TII->AnalyzeBranch(*BBI.BB, TBB, FBB, Cond);
667 bool CanFallthrough = isAnalyzable && (TBB == NULL || FBB == NULL);
668 if (BBI.TrueBB && BBI.BB->isSuccessor(BBI.TrueBB))
669 if (!(BBI.TrueBB == TBB || BBI.TrueBB == FBB ||
670 (CanFallthrough && getNextBlock(BBI.BB) == BBI.TrueBB)))
671 BBI.BB->removeSuccessor(BBI.TrueBB);
672 if (BBI.FalseBB && BBI.BB->isSuccessor(BBI.FalseBB))
673 if (!(BBI.FalseBB == TBB || BBI.FalseBB == FBB ||
674 (CanFallthrough && getNextBlock(BBI.BB) == BBI.FalseBB)))
675 BBI.BB->removeSuccessor(BBI.FalseBB);
678 /// IfConvertSimple - If convert a simple (split, no rejoin) sub-CFG.
680 bool IfConverter::IfConvertSimple(BBInfo &BBI) {
681 BBInfo &TrueBBI = BBAnalysis[BBI.TrueBB->getNumber()];
682 BBInfo &FalseBBI = BBAnalysis[BBI.FalseBB->getNumber()];
683 BBInfo *CvtBBI = &TrueBBI;
684 BBInfo *NextBBI = &FalseBBI;
686 std::vector<MachineOperand> Cond(BBI.BrCond);
687 if (BBI.Kind == ICSimpleFalse) {
688 std::swap(CvtBBI, NextBBI);
689 TII->ReverseBranchCondition(Cond);
692 PredicateBlock(*CvtBBI, Cond);
694 // Merge converted block into entry block.
695 BBI.NonPredSize -= TII->RemoveBranch(*BBI.BB);
696 MergeBlocks(BBI, *CvtBBI);
698 bool IterIfcvt = true;
699 if (!canFallThroughTo(BBI.BB, NextBBI->BB)) {
700 InsertUncondBranch(BBI.BB, NextBBI->BB, TII);
701 BBI.HasFallThrough = false;
702 // Now ifcvt'd block will look like this:
709 // We cannot further ifcvt this block because the unconditional branch
710 // will have to be predicated on the new condition, that will not be
711 // available if cmp executes.
715 RemoveExtraEdges(BBI);
717 // Update block info. BB can be iteratively if-converted.
721 CvtBBI->IsDone = true;
723 // FIXME: Must maintain LiveIns.
727 /// IfConvertTriangle - If convert a triangle sub-CFG.
729 bool IfConverter::IfConvertTriangle(BBInfo &BBI) {
730 BBInfo &TrueBBI = BBAnalysis[BBI.TrueBB->getNumber()];
731 BBInfo &FalseBBI = BBAnalysis[BBI.FalseBB->getNumber()];
732 BBInfo *CvtBBI = &TrueBBI;
733 BBInfo *NextBBI = &FalseBBI;
735 std::vector<MachineOperand> Cond(BBI.BrCond);
736 if (BBI.Kind == ICTriangleFalse || BBI.Kind == ICTriangleFRev) {
737 if (BBI.Kind == ICTriangleFRev) {
738 ReverseBranchCondition(*NextBBI);
739 // False BB has been changed, modify its predecessors (except for this
740 // one) so they don't get ifcvt'ed based on bad intel.
741 for (MachineBasicBlock::pred_iterator PI = NextBBI->BB->pred_begin(),
742 E = NextBBI->BB->pred_end(); PI != E; ++PI) {
743 MachineBasicBlock *PBB = *PI;
746 BBInfo &PBBI = BBAnalysis[PBB->getNumber()];
748 PBBI.IsEnqueued = false;
751 std::swap(CvtBBI, NextBBI);
752 TII->ReverseBranchCondition(Cond);
755 // Predicate the 'true' block after removing its branch.
756 TrueBBI.NonPredSize -= TII->RemoveBranch(*CvtBBI->BB);
757 PredicateBlock(*CvtBBI, Cond);
759 // If 'true' block has a 'false' successor, add an exit branch to it.
760 bool HasEarlyExit = CvtBBI->FalseBB != NULL;
762 std::vector<MachineOperand> RevCond(CvtBBI->BrCond);
763 if (TII->ReverseBranchCondition(RevCond))
764 assert(false && "Unable to reverse branch condition!");
765 TII->InsertBranch(*CvtBBI->BB, CvtBBI->FalseBB, NULL, RevCond);
768 // Now merge the entry of the triangle with the true block.
769 BBI.NonPredSize -= TII->RemoveBranch(*BBI.BB);
770 MergeBlocks(BBI, *CvtBBI);
772 // Merge in the 'false' block if the 'false' block has no other
773 // predecessors. Otherwise, add a unconditional branch from to 'false'.
774 bool FalseBBDead = false;
775 bool IterIfcvt = true;
776 bool isFallThrough = canFallThroughTo(BBI.BB, NextBBI->BB);
777 if (!isFallThrough) {
778 // Only merge them if the true block does not fallthrough to the false
779 // block. By not merging them, we make it possible to iteratively
781 if (!HasEarlyExit && NextBBI->BB->pred_size() == 1) {
782 MergeBlocks(BBI, *NextBBI);
785 InsertUncondBranch(BBI.BB, NextBBI->BB, TII);
786 BBI.HasFallThrough = false;
788 // Mixed predicated and unpredicated code. This cannot be iteratively
793 RemoveExtraEdges(BBI);
795 // Update block info. BB can be iteratively if-converted.
799 CvtBBI->IsDone = true;
801 NextBBI->IsDone = true;
803 // FIXME: Must maintain LiveIns.
807 /// IfConvertDiamond - If convert a diamond sub-CFG.
809 bool IfConverter::IfConvertDiamond(BBInfo &BBI) {
810 BBInfo &TrueBBI = BBAnalysis[BBI.TrueBB->getNumber()];
811 BBInfo &FalseBBI = BBAnalysis[BBI.FalseBB->getNumber()];
813 SmallVector<MachineInstr*, 2> Dups;
815 // No common merge block. Check if the terminators (e.g. return) are
816 // the same or predicable.
817 MachineBasicBlock::iterator TT = BBI.TrueBB->getFirstTerminator();
818 MachineBasicBlock::iterator FT = BBI.FalseBB->getFirstTerminator();
819 while (TT != BBI.TrueBB->end() && FT != BBI.FalseBB->end()) {
820 if (TT->isIdenticalTo(FT))
821 Dups.push_back(TT); // Will erase these later.
822 else if (!TT->isPredicable() && !FT->isPredicable())
823 return false; // Can't if-convert. Abort!
828 // One of the two pathes have more terminators, make sure they are
830 while (TT != BBI.TrueBB->end()) {
831 if (!TT->isPredicable()) {
832 return false; // Can't if-convert. Abort!
836 while (FT != BBI.FalseBB->end()) {
837 if (!FT->isPredicable()) {
838 return false; // Can't if-convert. Abort!
844 // Remove the duplicated instructions from the 'true' block.
845 for (unsigned i = 0, e = Dups.size(); i != e; ++i) {
846 Dups[i]->eraseFromParent();
847 --TrueBBI.NonPredSize;
850 // Merge the 'true' and 'false' blocks by copying the instructions
851 // from the 'false' block to the 'true' block. That is, unless the true
852 // block would clobber the predicate, in that case, do the opposite.
853 BBInfo *BBI1 = &TrueBBI;
854 BBInfo *BBI2 = &FalseBBI;
855 std::vector<MachineOperand> RevCond(BBI.BrCond);
856 TII->ReverseBranchCondition(RevCond);
857 std::vector<MachineOperand> *Cond1 = &BBI.BrCond;
858 std::vector<MachineOperand> *Cond2 = &RevCond;
859 // Check the 'true' and 'false' blocks if either isn't ended with a branch.
860 // Either the block fallthrough to another block or it ends with a
861 // return. If it's the former, add a branch to its successor.
862 bool NeedBr1 = !BBI1->TrueBB && BBI1->BB->succ_size();
863 bool NeedBr2 = !BBI2->TrueBB && BBI2->BB->succ_size();
865 if ((TrueBBI.ClobbersPred && !FalseBBI.ClobbersPred) ||
866 (!TrueBBI.ClobbersPred && !FalseBBI.ClobbersPred &&
867 NeedBr1 && !NeedBr2)) {
868 std::swap(BBI1, BBI2);
869 std::swap(Cond1, Cond2);
870 std::swap(NeedBr1, NeedBr2);
873 // Predicate the 'true' block after removing its branch.
874 BBI1->NonPredSize -= TII->RemoveBranch(*BBI1->BB);
875 PredicateBlock(*BBI1, *Cond1);
877 // Add an early exit branch if needed.
879 TII->InsertBranch(*BBI1->BB, *BBI1->BB->succ_begin(), NULL, *Cond1);
881 // Predicate the 'false' block.
882 PredicateBlock(*BBI2, *Cond2, true);
884 // Add an unconditional branch from 'false' to to 'false' successor if it
885 // will not be the fallthrough block.
886 if (NeedBr2 && !NeedBr1) {
887 // If BBI2 isn't going to be merged in, then the existing fallthrough
888 // or branch is fine.
889 if (!canFallThroughTo(BBI.BB, *BBI2->BB->succ_begin())) {
890 InsertUncondBranch(BBI2->BB, *BBI2->BB->succ_begin(), TII);
891 BBI2->HasFallThrough = false;
895 // Keep them as two separate blocks if there is an early exit.
897 MergeBlocks(*BBI1, *BBI2);
899 // Remove the conditional branch from entry to the blocks.
900 BBI.NonPredSize -= TII->RemoveBranch(*BBI.BB);
902 // Merge the combined block into the entry of the diamond.
903 MergeBlocks(BBI, *BBI1);
905 // 'True' and 'false' aren't combined, see if we need to add a unconditional
906 // branch to the 'false' block.
907 if (NeedBr1 && !canFallThroughTo(BBI.BB, BBI2->BB)) {
908 InsertUncondBranch(BBI.BB, BBI2->BB, TII);
909 BBI1->HasFallThrough = false;
912 // If the if-converted block fallthrough or unconditionally branch into the
913 // tail block, and the tail block does not have other predecessors, then
914 // fold the tail block in as well.
915 BBInfo *CvtBBI = NeedBr1 ? BBI2 : &BBI;
917 BBI.TailBB->pred_size() == 1 && CvtBBI->BB->succ_size() == 1) {
918 CvtBBI->NonPredSize -= TII->RemoveBranch(*CvtBBI->BB);
919 BBInfo TailBBI = BBAnalysis[BBI.TailBB->getNumber()];
920 MergeBlocks(*CvtBBI, TailBBI);
921 TailBBI.IsDone = true;
924 RemoveExtraEdges(BBI);
926 // Update block info.
927 BBI.IsDone = TrueBBI.IsDone = FalseBBI.IsDone = true;
929 // FIXME: Must maintain LiveIns.
933 /// PredicateBlock - Predicate every instruction in the block with the specified
934 /// condition. If IgnoreTerm is true, skip over all terminator instructions.
935 void IfConverter::PredicateBlock(BBInfo &BBI,
936 std::vector<MachineOperand> &Cond,
938 for (MachineBasicBlock::iterator I = BBI.BB->begin(), E = BBI.BB->end();
940 if (IgnoreTerm && TII->isTerminatorInstr(I->getOpcode()))
942 if (TII->isPredicated(I))
944 if (!TII->PredicateInstruction(I, Cond)) {
945 cerr << "Unable to predicate " << *I << "!\n";
951 std::copy(Cond.begin(), Cond.end(), std::back_inserter(BBI.Predicate));
956 /// MergeBlocks - Move all instructions from FromBB to the end of ToBB.
958 void IfConverter::MergeBlocks(BBInfo &ToBBI, BBInfo &FromBBI) {
959 ToBBI.BB->splice(ToBBI.BB->end(),
960 FromBBI.BB, FromBBI.BB->begin(), FromBBI.BB->end());
962 // Redirect all branches to FromBB to ToBB.
963 std::vector<MachineBasicBlock *> Preds(FromBBI.BB->pred_begin(),
964 FromBBI.BB->pred_end());
965 for (unsigned i = 0, e = Preds.size(); i != e; ++i) {
966 MachineBasicBlock *Pred = Preds[i];
967 if (Pred == ToBBI.BB)
969 Pred->ReplaceUsesOfBlockWith(FromBBI.BB, ToBBI.BB);
972 std::vector<MachineBasicBlock *> Succs(FromBBI.BB->succ_begin(),
973 FromBBI.BB->succ_end());
974 MachineBasicBlock *NBB = getNextBlock(FromBBI.BB);
975 MachineBasicBlock *FallThrough = FromBBI.HasFallThrough ? NBB : NULL;
977 for (unsigned i = 0, e = Succs.size(); i != e; ++i) {
978 MachineBasicBlock *Succ = Succs[i];
979 // Fallthrough edge can't be transferred.
980 if (Succ == FallThrough)
982 FromBBI.BB->removeSuccessor(Succ);
983 if (!ToBBI.BB->isSuccessor(Succ))
984 ToBBI.BB->addSuccessor(Succ);
987 // Now FromBBI always fall through to the next block!
989 FromBBI.BB->addSuccessor(NBB);
991 ToBBI.NonPredSize += FromBBI.NonPredSize;
992 FromBBI.NonPredSize = 0;
994 ToBBI.ClobbersPred |= FromBBI.ClobbersPred;
995 ToBBI.HasFallThrough = FromBBI.HasFallThrough;
997 std::copy(FromBBI.Predicate.begin(), FromBBI.Predicate.end(),
998 std::back_inserter(ToBBI.Predicate));
999 FromBBI.Predicate.clear();