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/Debug.h"
23 #include "llvm/ADT/DepthFirstIterator.h"
24 #include "llvm/ADT/Statistic.h"
27 STATISTIC(NumSimple, "Number of simple if-conversions performed");
28 STATISTIC(NumSimpleRev, "Number of simple (reversed) if-conversions performed");
29 STATISTIC(NumTriangle, "Number of triangle if-conversions performed");
30 STATISTIC(NumDiamonds, "Number of diamond if-conversions performed");
31 STATISTIC(NumIfConvBBs, "Number of if-converted blocks");
34 class IfConverter : public MachineFunctionPass {
36 ICNotAnalyzed, // BB has not been analyzed.
37 ICReAnalyze, // BB must be re-analyzed.
38 ICNotClassfied, // BB data valid, but not classified.
39 ICSimple, // BB is entry of an one split, no rejoin sub-CFG.
40 ICSimpleFalse, // Same as ICSimple, but on the false path.
41 ICTriangle, // BB is entry of a triangle sub-CFG.
42 ICDiamond, // BB is entry of a diamond sub-CFG.
43 ICChild, // BB is part of the sub-CFG that'll be predicated.
44 ICDead // BB cannot be if-converted again.
47 /// BBInfo - One per MachineBasicBlock, this is used to cache the result
48 /// if-conversion feasibility analysis. This includes results from
49 /// TargetInstrInfo::AnalyzeBranch() (i.e. TBB, FBB, and Cond), and its
50 /// classification, and common tail block of its successors (if it's a
51 /// diamond shape), its size, whether it's predicable, and whether any
52 /// instruction can clobber the 'would-be' predicate.
54 /// Kind - Type of block. See BBICKind.
55 /// NonPredSize - Number of non-predicated instructions.
56 /// IsAnalyzable - True if AnalyzeBranch() returns false.
57 /// ModifyPredicate - True if BB would modify the predicate (e.g. has
59 /// BB - Corresponding MachineBasicBlock.
60 /// TrueBB / FalseBB- See AnalyzeBranch().
61 /// BrCond - Conditions for end of block conditional branches.
62 /// Predicate - Predicate used in the BB.
68 MachineBasicBlock *BB;
69 MachineBasicBlock *TrueBB;
70 MachineBasicBlock *FalseBB;
71 MachineBasicBlock *TailBB;
72 std::vector<MachineOperand> BrCond;
73 std::vector<MachineOperand> Predicate;
74 BBInfo() : Kind(ICNotAnalyzed), NonPredSize(0),
75 IsAnalyzable(false), ModifyPredicate(false),
76 BB(0), TrueBB(0), FalseBB(0), TailBB(0) {}
79 /// Roots - Basic blocks that do not have successors. These are the starting
80 /// points of Graph traversal.
81 std::vector<MachineBasicBlock*> Roots;
83 /// BBAnalysis - Results of if-conversion feasibility analysis indexed by
84 /// basic block number.
85 std::vector<BBInfo> BBAnalysis;
87 const TargetLowering *TLI;
88 const TargetInstrInfo *TII;
92 IfConverter() : MachineFunctionPass((intptr_t)&ID) {}
94 virtual bool runOnMachineFunction(MachineFunction &MF);
95 virtual const char *getPassName() const { return "If converter"; }
98 bool ReverseBranchCondition(BBInfo &BBI);
99 bool BlockModifyPredicate(MachineBasicBlock *BB) const;
100 void StructuralAnalysis(MachineBasicBlock *BB);
101 bool FeasibilityAnalysis(BBInfo &BBI,
102 std::vector<MachineOperand> &Cond,
103 bool IgnoreTerm = false);
104 bool AttemptRestructuring(BBInfo &BBI);
105 bool AnalyzeBlocks(MachineFunction &MF,
106 std::vector<BBInfo*> &Candidates);
107 void ReTryPreds(MachineBasicBlock *BB);
108 bool IfConvertSimple(BBInfo &BBI);
109 bool IfConvertTriangle(BBInfo &BBI);
110 bool IfConvertDiamond(BBInfo &BBI);
111 void PredicateBlock(BBInfo &BBI,
112 std::vector<MachineOperand> &Cond,
113 bool IgnoreTerm = false);
114 void MergeBlocks(BBInfo &TrueBBI, BBInfo &FalseBBI);
116 // blockFallsThrough - Block ends without a terminator.
117 bool blockFallsThrough(BBInfo &BBI) const {
118 return BBI.IsAnalyzable && BBI.TrueBB == NULL;
121 // IfcvtCandidateCmp - Used to sort if-conversion candidates.
122 static bool IfcvtCandidateCmp(BBInfo* C1, BBInfo* C2){
123 // Favor diamond over triangle, etc.
124 return (unsigned)C1->Kind < (unsigned)C2->Kind;
127 char IfConverter::ID = 0;
130 FunctionPass *llvm::createIfConverterPass() { return new IfConverter(); }
132 bool IfConverter::runOnMachineFunction(MachineFunction &MF) {
133 TLI = MF.getTarget().getTargetLowering();
134 TII = MF.getTarget().getInstrInfo();
135 if (!TII) return false;
137 DOUT << "\nIfcvt: function \'" << MF.getFunction()->getName() << "\'\n";
140 BBAnalysis.resize(MF.getNumBlockIDs());
142 // Look for root nodes, i.e. blocks without successors.
143 for (MachineFunction::iterator I = MF.begin(), E = MF.end(); I != E; ++I)
144 if (I->succ_size() == 0)
147 std::vector<BBInfo*> Candidates;
150 // Do an intial analysis for each basic block and finding all the potential
151 // candidates to perform if-convesion.
152 bool Change = AnalyzeBlocks(MF, Candidates);
153 while (!Candidates.empty()) {
154 BBInfo &BBI = *Candidates.back();
155 Candidates.pop_back();
159 default: assert(false && "Unexpected!");
162 // One or more of 'children' have been modified, abort!
164 // Block has been already been if-converted, abort!
167 case ICSimpleFalse: {
168 bool isRev = BBI.Kind == ICSimpleFalse;
169 DOUT << "Ifcvt (Simple" << (BBI.Kind == ICSimpleFalse ? " false" : "")
170 << "): BB#" << BBI.BB->getNumber() << " ("
171 << ((BBI.Kind == ICSimpleFalse)
172 ? BBI.FalseBB->getNumber() : BBI.TrueBB->getNumber()) << ") ";
173 RetVal = IfConvertSimple(BBI);
174 DOUT << (RetVal ? "succeeded!" : "failed!") << "\n";
176 if (isRev) NumSimpleRev++;
181 DOUT << "Ifcvt (Triangle): BB#" << BBI.BB->getNumber() << " (T:"
182 << BBI.TrueBB->getNumber() << ",F:" << BBI.FalseBB->getNumber()
184 RetVal = IfConvertTriangle(BBI);
185 DOUT << (RetVal ? "succeeded!" : "failed!") << "\n";
186 if (RetVal) NumTriangle++;
189 DOUT << "Ifcvt (Diamond): BB#" << BBI.BB->getNumber() << " (T:"
190 << BBI.TrueBB->getNumber() << ",F:" << BBI.FalseBB->getNumber();
192 DOUT << "," << BBI.TailBB->getNumber() ;
194 RetVal = IfConvertDiamond(BBI);
195 DOUT << (RetVal ? "succeeded!" : "failed!") << "\n";
196 if (RetVal) NumDiamonds++;
204 MadeChange |= Change;
213 static MachineBasicBlock *findFalseBlock(MachineBasicBlock *BB,
214 MachineBasicBlock *TrueBB) {
215 for (MachineBasicBlock::succ_iterator SI = BB->succ_begin(),
216 E = BB->succ_end(); SI != E; ++SI) {
217 MachineBasicBlock *SuccBB = *SI;
218 if (SuccBB != TrueBB)
224 bool IfConverter::ReverseBranchCondition(BBInfo &BBI) {
225 if (!TII->ReverseBranchCondition(BBI.BrCond)) {
226 TII->RemoveBranch(*BBI.BB);
227 TII->InsertBranch(*BBI.BB, BBI.FalseBB, BBI.TrueBB, BBI.BrCond);
228 std::swap(BBI.TrueBB, BBI.FalseBB);
234 /// BlockModifyPredicate - Returns true if any instruction in the block may
235 /// clobber the condition code or register(s) used to predicate instructions,
237 bool IfConverter::BlockModifyPredicate(MachineBasicBlock *BB) const {
238 for (MachineBasicBlock::const_reverse_iterator I = BB->rbegin(),
239 E = BB->rend(); I != E; ++I)
240 if (I->getInstrDescriptor()->Flags & M_CLOBBERS_PRED)
245 /// StructuralAnalysis - Analyze the structure of the sub-CFG starting from
246 /// the specified block. Record its successors and whether it looks like an
247 /// if-conversion candidate.
248 void IfConverter::StructuralAnalysis(MachineBasicBlock *BB) {
249 BBInfo &BBI = BBAnalysis[BB->getNumber()];
251 if (BBI.Kind == ICReAnalyze) {
253 BBI.TrueBB = BBI.FalseBB = NULL;
255 if (BBI.Kind != ICNotAnalyzed)
256 return; // Already analyzed.
258 BBI.NonPredSize = std::distance(BB->begin(), BB->end());
259 BBI.ModifyPredicate = BlockModifyPredicate(BB);
262 // Look for 'root' of a simple (non-nested) triangle or diamond.
263 BBI.Kind = ICNotClassfied;
265 !TII->AnalyzeBranch(*BB, BBI.TrueBB, BBI.FalseBB, BBI.BrCond);
266 if (!BBI.IsAnalyzable || BBI.BrCond.size() == 0)
268 // Do not ifcvt if either path is a back edge to the entry block.
269 if (BBI.TrueBB == BB || BBI.FalseBB == BB)
272 StructuralAnalysis(BBI.TrueBB);
273 BBInfo &TrueBBI = BBAnalysis[BBI.TrueBB->getNumber()];
275 // No false branch. This BB must end with a conditional branch and a
278 BBI.FalseBB = findFalseBlock(BB, BBI.TrueBB);
279 assert(BBI.FalseBB && "Expected to find the fallthrough block!");
281 StructuralAnalysis(BBI.FalseBB);
282 BBInfo &FalseBBI = BBAnalysis[BBI.FalseBB->getNumber()];
284 // If both paths are dead, then forget about it.
285 if (TrueBBI.Kind == ICDead && FalseBBI.Kind == ICDead) {
290 // Look for more opportunities to if-convert a triangle. Try to restructure
291 // the CFG to form a triangle with the 'false' path.
292 std::vector<MachineOperand> RevCond(BBI.BrCond);
293 bool CanRevCond = !TII->ReverseBranchCondition(RevCond);
294 if (FalseBBI.FalseBB) {
295 if (TrueBBI.TrueBB && TrueBBI.TrueBB == BBI.FalseBB)
297 std::vector<MachineOperand> Cond(BBI.BrCond);
299 FalseBBI.TrueBB && FalseBBI.BB->pred_size() == 1 &&
300 FeasibilityAnalysis(FalseBBI, RevCond, true)) {
301 std::vector<MachineOperand> FalseCond(FalseBBI.BrCond);
302 if (FalseBBI.TrueBB == BBI.TrueBB &&
303 TII->SubsumesPredicate(FalseCond, BBI.BrCond)) {
304 // Reverse 'true' and 'false' paths.
305 ReverseBranchCondition(BBI);
306 BBI.Kind = ICTriangle;
307 FalseBBI.Kind = ICChild;
308 } else if (FalseBBI.FalseBB == BBI.TrueBB &&
309 !TII->ReverseBranchCondition(FalseCond) &&
310 TII->SubsumesPredicate(FalseCond, BBI.BrCond)) {
311 // Reverse 'false' block's 'true' and 'false' paths and then
312 // reverse 'true' and 'false' paths.
313 ReverseBranchCondition(FalseBBI);
314 ReverseBranchCondition(BBI);
315 BBI.Kind = ICTriangle;
316 FalseBBI.Kind = ICChild;
319 } else if (TrueBBI.TrueBB == FalseBBI.TrueBB && CanRevCond &&
320 TrueBBI.BB->pred_size() == 1 &&
321 FalseBBI.BB->pred_size() == 1 &&
322 // Check the 'true' and 'false' blocks if either isn't ended with
323 // a branch. If the block does not fallthrough to another block
324 // then we need to add a branch to its successor.
325 !(TrueBBI.ModifyPredicate &&
326 !TrueBBI.TrueBB && TrueBBI.BB->succ_size()) &&
327 !(FalseBBI.ModifyPredicate &&
328 !FalseBBI.TrueBB && FalseBBI.BB->succ_size()) &&
329 FeasibilityAnalysis(TrueBBI, BBI.BrCond) &&
330 FeasibilityAnalysis(FalseBBI, RevCond)) {
338 // Note TailBB can be empty.
339 BBI.Kind = ICDiamond;
340 TrueBBI.Kind = FalseBBI.Kind = ICChild;
341 BBI.TailBB = TrueBBI.TrueBB;
343 // FIXME: Consider duplicating if BB is small.
344 bool TryTriangle = TrueBBI.TrueBB && TrueBBI.TrueBB == BBI.FalseBB &&
345 TrueBBI.BB->pred_size() == 1;
346 bool TrySimple = TrueBBI.BrCond.size() == 0 && TrueBBI.BB->pred_size() == 1;
347 if ((TryTriangle || TrySimple) &&
348 FeasibilityAnalysis(TrueBBI, BBI.BrCond)) {
357 BBI.Kind = ICTriangle;
358 TrueBBI.Kind = ICChild;
360 // Simple (split, no rejoin):
368 TrueBBI.Kind = ICChild;
370 } else if (FalseBBI.BrCond.size() == 0 && FalseBBI.BB->pred_size() == 1) {
371 // Try the other path...
372 bool TryTriangle = FalseBBI.TrueBB && FalseBBI.TrueBB == BBI.TrueBB &&
373 FalseBBI.BB->pred_size() == 1;
374 std::vector<MachineOperand> RevCond(BBI.BrCond);
375 if (!TII->ReverseBranchCondition(RevCond) &&
376 FeasibilityAnalysis(FalseBBI, RevCond)) {
378 // Reverse 'true' and 'false' paths.
379 ReverseBranchCondition(BBI);
380 BBI.Kind = ICTriangle;
381 FalseBBI.Kind = ICChild;
383 BBI.Kind = ICSimpleFalse;
384 FalseBBI.Kind = ICChild;
392 /// FeasibilityAnalysis - Determine if the block is predicable. In most
393 /// cases, that means all the instructions in the block has M_PREDICABLE flag.
394 /// Also checks if the block contains any instruction which can clobber a
395 /// predicate (e.g. condition code register). If so, the block is not
396 /// predicable unless it's the last instruction. If IgnoreTerm is true then
397 /// all the terminator instructions are skipped.
398 bool IfConverter::FeasibilityAnalysis(BBInfo &BBI,
399 std::vector<MachineOperand> &Cond,
401 // If the block is dead, or it is going to be the entry block of a sub-CFG
402 // that will be if-converted, then it cannot be predicated.
403 if (BBI.Kind != ICNotAnalyzed &&
404 BBI.Kind != ICNotClassfied &&
408 // Check predication threshold.
409 if (BBI.NonPredSize == 0 || BBI.NonPredSize > TLI->getIfCvtBlockSizeLimit())
412 // If it is already predicated, check if its predicate subsumes the new
414 if (BBI.Predicate.size() && !TII->SubsumesPredicate(BBI.Predicate, Cond))
417 for (MachineBasicBlock::iterator I = BBI.BB->begin(), E = BBI.BB->end();
419 if (IgnoreTerm && TII->isTerminatorInstr(I->getOpcode()))
421 // TODO: check if instruction clobbers predicate.
422 if (!I->isPredicable())
429 /// AttemptRestructuring - Restructure the sub-CFG rooted in the given block to
430 /// expose more if-conversion opportunities. e.g.
452 bool IfConverter::AttemptRestructuring(BBInfo &BBI) {
456 /// AnalyzeBlocks - Analyze all blocks and find entries for all if-conversion
457 /// candidates. It returns true if any CFG restructuring is done to expose more
458 /// if-conversion opportunities.
459 bool IfConverter::AnalyzeBlocks(MachineFunction &MF,
460 std::vector<BBInfo*> &Candidates) {
462 std::set<MachineBasicBlock*> Visited;
463 for (unsigned i = 0, e = Roots.size(); i != e; ++i) {
464 for (idf_ext_iterator<MachineBasicBlock*> I=idf_ext_begin(Roots[i],Visited),
465 E = idf_ext_end(Roots[i], Visited); I != E; ++I) {
466 MachineBasicBlock *BB = *I;
467 StructuralAnalysis(BB);
468 BBInfo &BBI = BBAnalysis[BB->getNumber()];
474 Candidates.push_back(&BBI);
477 Change |= AttemptRestructuring(BBI);
483 // Sort to favor more complex ifcvt scheme.
484 std::stable_sort(Candidates.begin(), Candidates.end(), IfcvtCandidateCmp);
489 /// isNextBlock - Returns true either if ToBB the next block after BB or
490 /// that all the intervening blocks are empty.
491 static bool isNextBlock(MachineBasicBlock *BB, MachineBasicBlock *ToBB) {
492 MachineFunction::iterator I = BB;
493 MachineFunction::iterator TI = ToBB;
494 MachineFunction::iterator E = BB->getParent()->end();
496 if (I == E || !I->empty())
501 /// ReTryPreds - Invalidate predecessor BB info so it would be re-analyzed
502 /// to determine if it can be if-converted.
503 void IfConverter::ReTryPreds(MachineBasicBlock *BB) {
504 for (MachineBasicBlock::pred_iterator PI = BB->pred_begin(),
505 E = BB->pred_end(); PI != E; ++PI) {
506 BBInfo &PBBI = BBAnalysis[(*PI)->getNumber()];
507 PBBI.Kind = ICReAnalyze;
511 /// InsertUncondBranch - Inserts an unconditional branch from BB to ToBB.
513 static void InsertUncondBranch(MachineBasicBlock *BB, MachineBasicBlock *ToBB,
514 const TargetInstrInfo *TII) {
515 std::vector<MachineOperand> NoCond;
516 TII->InsertBranch(*BB, ToBB, NULL, NoCond);
519 /// IfConvertSimple - If convert a simple (split, no rejoin) sub-CFG.
521 bool IfConverter::IfConvertSimple(BBInfo &BBI) {
522 BBInfo &TrueBBI = BBAnalysis[BBI.TrueBB->getNumber()];
523 BBInfo &FalseBBI = BBAnalysis[BBI.FalseBB->getNumber()];
524 BBInfo *CvtBBI = &TrueBBI;
525 BBInfo *NextBBI = &FalseBBI;
527 std::vector<MachineOperand> Cond(BBI.BrCond);
528 if (BBI.Kind == ICSimpleFalse) {
529 std::swap(CvtBBI, NextBBI);
530 TII->ReverseBranchCondition(Cond);
533 PredicateBlock(*CvtBBI, Cond);
534 // If the 'true' block ends without a branch, add a conditional branch
535 // to its successor unless that happens to be the 'false' block.
536 if (CvtBBI->IsAnalyzable && CvtBBI->TrueBB == NULL) {
537 assert(CvtBBI->BB->succ_size() == 1 && "Unexpected!");
538 MachineBasicBlock *SuccBB = *CvtBBI->BB->succ_begin();
539 if (SuccBB != NextBBI->BB)
540 TII->InsertBranch(*CvtBBI->BB, SuccBB, NULL, Cond);
543 // Merge converted block into entry block. Also add an unconditional branch
544 // to the 'false' branch.
545 BBI.NonPredSize -= TII->RemoveBranch(*BBI.BB);
546 MergeBlocks(BBI, *CvtBBI);
547 bool IterIfcvt = true;
548 if (!isNextBlock(BBI.BB, NextBBI->BB)) {
549 InsertUncondBranch(BBI.BB, NextBBI->BB, TII);
550 if (BBI.ModifyPredicate)
551 // Now ifcvt'd block will look like this:
558 // We cannot further ifcvt this block because the unconditional branch will
559 // have to be predicated on the new condition, that will not be available
563 std::copy(Cond.begin(), Cond.end(), std::back_inserter(BBI.Predicate));
565 // Update block info. BB can be iteratively if-converted.
567 BBI.Kind = ICReAnalyze;
571 CvtBBI->Kind = ICDead;
573 // FIXME: Must maintain LiveIns.
577 /// IfConvertTriangle - If convert a triangle sub-CFG.
579 bool IfConverter::IfConvertTriangle(BBInfo &BBI) {
580 BBInfo &TrueBBI = BBAnalysis[BBI.TrueBB->getNumber()];
582 // Predicate the 'true' block after removing its branch.
583 TrueBBI.NonPredSize -= TII->RemoveBranch(*BBI.TrueBB);
584 PredicateBlock(TrueBBI, BBI.BrCond);
586 // If 'true' block has a 'false' successor, add an exit branch to it.
587 bool HasEarlyExit = TrueBBI.FalseBB != NULL;
589 std::vector<MachineOperand> RevCond(TrueBBI.BrCond);
590 if (TII->ReverseBranchCondition(RevCond))
591 assert(false && "Unable to reverse branch condition!");
592 TII->InsertBranch(*BBI.TrueBB, TrueBBI.FalseBB, NULL, RevCond);
595 // Join the 'true' and 'false' blocks if the 'false' block has no other
596 // predecessors. Otherwise, add a unconditional branch from 'true' to 'false'.
597 BBInfo &FalseBBI = BBAnalysis[BBI.FalseBB->getNumber()];
598 bool FalseBBDead = false;
599 bool IterIfcvt = true;
600 if (!HasEarlyExit && FalseBBI.BB->pred_size() == 2) {
601 MergeBlocks(TrueBBI, FalseBBI);
603 } else if (!isNextBlock(TrueBBI.BB, FalseBBI.BB)) {
604 InsertUncondBranch(TrueBBI.BB, FalseBBI.BB, TII);
605 if (BBI.ModifyPredicate || TrueBBI.ModifyPredicate)
606 // Now ifcvt'd block will look like this:
613 // We cannot further ifcvt this block because the unconditional branch will
614 // have to be predicated on the new condition, that will not be available
619 // Now merge the entry of the triangle with the true block.
620 BBI.NonPredSize -= TII->RemoveBranch(*BBI.BB);
621 MergeBlocks(BBI, TrueBBI);
622 std::copy(BBI.BrCond.begin(), BBI.BrCond.end(),
623 std::back_inserter(BBI.Predicate));
625 // Update block info. BB can be iteratively if-converted.
627 BBI.Kind = ICReAnalyze;
631 TrueBBI.Kind = ICDead;
633 FalseBBI.Kind = ICDead;
635 // FIXME: Must maintain LiveIns.
639 /// IfConvertDiamond - If convert a diamond sub-CFG.
641 bool IfConverter::IfConvertDiamond(BBInfo &BBI) {
642 BBInfo &TrueBBI = BBAnalysis[BBI.TrueBB->getNumber()];
643 BBInfo &FalseBBI = BBAnalysis[BBI.FalseBB->getNumber()];
645 SmallVector<MachineInstr*, 2> Dups;
647 // No common merge block. Check if the terminators (e.g. return) are
648 // the same or predicable.
649 MachineBasicBlock::iterator TT = BBI.TrueBB->getFirstTerminator();
650 MachineBasicBlock::iterator FT = BBI.FalseBB->getFirstTerminator();
651 while (TT != BBI.TrueBB->end() && FT != BBI.FalseBB->end()) {
652 if (TT->isIdenticalTo(FT))
653 Dups.push_back(TT); // Will erase these later.
654 else if (!TT->isPredicable() && !FT->isPredicable())
655 return false; // Can't if-convert. Abort!
660 // One of the two pathes have more terminators, make sure they are
662 while (TT != BBI.TrueBB->end()) {
663 if (!TT->isPredicable()) {
664 return false; // Can't if-convert. Abort!
668 while (FT != BBI.FalseBB->end()) {
669 if (!FT->isPredicable()) {
670 return false; // Can't if-convert. Abort!
676 // Remove the duplicated instructions from the 'true' block.
677 for (unsigned i = 0, e = Dups.size(); i != e; ++i) {
678 Dups[i]->eraseFromParent();
679 --TrueBBI.NonPredSize;
682 // Merge the 'true' and 'false' blocks by copying the instructions
683 // from the 'false' block to the 'true' block. That is, unless the true
684 // block would clobber the predicate, in that case, do the opposite.
685 BBInfo *BBI1 = &TrueBBI;
686 BBInfo *BBI2 = &FalseBBI;
687 std::vector<MachineOperand> RevCond(BBI.BrCond);
688 TII->ReverseBranchCondition(RevCond);
689 std::vector<MachineOperand> *Cond1 = &BBI.BrCond;
690 std::vector<MachineOperand> *Cond2 = &RevCond;
691 // Check the 'true' and 'false' blocks if either isn't ended with a branch.
692 // Either the block fallthrough to another block or it ends with a
693 // return. If it's the former, add a branch to its successor.
694 bool NeedBr1 = !BBI1->TrueBB && BBI1->BB->succ_size();
695 bool NeedBr2 = !BBI2->TrueBB && BBI2->BB->succ_size();
697 if ((TrueBBI.ModifyPredicate && !FalseBBI.ModifyPredicate) ||
698 (!TrueBBI.ModifyPredicate && !FalseBBI.ModifyPredicate &&
699 NeedBr1 && !NeedBr2)) {
700 std::swap(BBI1, BBI2);
701 std::swap(Cond1, Cond2);
702 std::swap(NeedBr1, NeedBr2);
705 // Predicate the 'true' block after removing its branch.
706 BBI1->NonPredSize -= TII->RemoveBranch(*BBI1->BB);
707 PredicateBlock(*BBI1, *Cond1);
709 // Add an early exit branch if needed.
711 TII->InsertBranch(*BBI1->BB, *BBI1->BB->succ_begin(), NULL, *Cond1);
713 // Predicate the 'false' block.
714 PredicateBlock(*BBI2, *Cond2, true);
716 // Add an unconditional branch from 'false' to to 'false' successor if it
717 // will not be the fallthrough block.
718 if (NeedBr2 && !isNextBlock(BBI2->BB, *BBI2->BB->succ_begin()))
719 InsertUncondBranch(BBI2->BB, *BBI2->BB->succ_begin(), TII);
721 // Keep them as two separate blocks if there is an early exit.
723 MergeBlocks(*BBI1, *BBI2);
725 // Remove the conditional branch from entry to the blocks.
726 BBI.NonPredSize -= TII->RemoveBranch(*BBI.BB);
728 // Merge the combined block into the entry of the diamond.
729 MergeBlocks(BBI, *BBI1);
731 // 'True' and 'false' aren't combined, see if we need to add a unconditional
732 // branch to the 'false' block.
733 if (NeedBr1 && !isNextBlock(BBI.BB, BBI2->BB))
734 InsertUncondBranch(BBI1->BB, BBI2->BB, TII);
736 // If the if-converted block fallthrough or unconditionally branch into the
737 // tail block, and the tail block does not have other predecessors, then
738 // fold the tail block in as well.
739 BBInfo *CvtBBI = NeedBr1 ? BBI2 : &BBI;
741 BBI.TailBB->pred_size() == 1 && CvtBBI->BB->succ_size() == 1) {
742 CvtBBI->NonPredSize -= TII->RemoveBranch(*CvtBBI->BB);
743 BBInfo TailBBI = BBAnalysis[BBI.TailBB->getNumber()];
744 MergeBlocks(*CvtBBI, TailBBI);
745 TailBBI.Kind = ICDead;
748 // Update block info.
750 TrueBBI.Kind = ICDead;
751 FalseBBI.Kind = ICDead;
753 // FIXME: Must maintain LiveIns.
757 /// PredicateBlock - Predicate every instruction in the block with the specified
758 /// condition. If IgnoreTerm is true, skip over all terminator instructions.
759 void IfConverter::PredicateBlock(BBInfo &BBI,
760 std::vector<MachineOperand> &Cond,
762 for (MachineBasicBlock::iterator I = BBI.BB->begin(), E = BBI.BB->end();
764 if (IgnoreTerm && TII->isTerminatorInstr(I->getOpcode()))
766 if (TII->isPredicated(I))
768 if (!TII->PredicateInstruction(I, Cond)) {
769 cerr << "Unable to predicate " << *I << "!\n";
778 /// TransferPreds - Transfer all the predecessors of FromBB to ToBB.
780 static void TransferPreds(MachineBasicBlock *ToBB, MachineBasicBlock *FromBB) {
781 for (MachineBasicBlock::pred_iterator I = FromBB->pred_begin(),
782 E = FromBB->pred_end(); I != E; ++I) {
783 MachineBasicBlock *Pred = *I;
784 Pred->removeSuccessor(FromBB);
785 if (!Pred->isSuccessor(ToBB))
786 Pred->addSuccessor(ToBB);
790 /// TransferSuccs - Transfer all the successors of FromBB to ToBB.
792 static void TransferSuccs(MachineBasicBlock *ToBB, MachineBasicBlock *FromBB) {
793 for (MachineBasicBlock::succ_iterator I = FromBB->succ_begin(),
794 E = FromBB->succ_end(); I != E; ++I) {
795 MachineBasicBlock *Succ = *I;
796 FromBB->removeSuccessor(Succ);
797 if (!ToBB->isSuccessor(Succ))
798 ToBB->addSuccessor(Succ);
802 /// MergeBlocks - Move all instructions from FromBB to the end of ToBB.
804 void IfConverter::MergeBlocks(BBInfo &ToBBI, BBInfo &FromBBI) {
805 ToBBI.BB->splice(ToBBI.BB->end(),
806 FromBBI.BB, FromBBI.BB->begin(), FromBBI.BB->end());
808 // If FromBBI is previously a successor, remove it from ToBBI's successor
809 // list and update its TrueBB / FalseBB field if needed.
810 if (ToBBI.BB->isSuccessor(FromBBI.BB))
811 ToBBI.BB->removeSuccessor(FromBBI.BB);
813 // Redirect all branches to FromBB to ToBB.
814 std::vector<MachineBasicBlock *> Preds(FromBBI.BB->pred_begin(),
815 FromBBI.BB->pred_end());
816 for (unsigned i = 0, e = Preds.size(); i != e; ++i)
817 Preds[i]->ReplaceUsesOfBlockWith(FromBBI.BB, ToBBI.BB);
819 // Transfer preds / succs and update size.
820 TransferPreds(ToBBI.BB, FromBBI.BB);
821 if (!blockFallsThrough(FromBBI))
822 TransferSuccs(ToBBI.BB, FromBBI.BB);
823 ToBBI.NonPredSize += FromBBI.NonPredSize;
824 FromBBI.NonPredSize = 0;
826 ToBBI.ModifyPredicate |= FromBBI.ModifyPredicate;