llvm.org GIT mirror llvm / 01d7203
Factor FlattenCFG out from SimplifyCFG Patch by: Mei Ye git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@187764 91177308-0d34-0410-b5e6-96231b3b80d8 Tom Stellard 6 years ago
21 changed file(s) with 835 addition(s) and 706 deletion(s). Raw diff Collapse all Expand all
8585 void initializeCFGOnlyPrinterPass(PassRegistry&);
8686 void initializeCFGOnlyViewerPass(PassRegistry&);
8787 void initializeCFGPrinterPass(PassRegistry&);
88 void initializeCFGOptimizePass(PassRegistry&);
89 void initializeCFGCanonicalizePass(PassRegistry&);
88 void initializeCFGSimplifyPassPass(PassRegistry&);
89 void initializeFlattenCFGPassPass(PassRegistry&);
9090 void initializeStructurizeCFGPass(PassRegistry&);
9191 void initializeCFGViewerPass(PassRegistry&);
9292 void initializeCalculateSpillWeightsPass(PassRegistry&);
195195 // CFGSimplification - Merge basic blocks, eliminate unreachable blocks,
196196 // simplify terminator instructions, etc...
197197 //
198 FunctionPass *createCFGSimplificationPass(bool IsTargetAware = false);
198 FunctionPass *createCFGSimplificationPass();
199
200 //===----------------------------------------------------------------------===//
201 //
202 // FlattenCFG - flatten CFG, reduce number of conditional branches by using
203 // parallel-and and parallel-or mode, etc...
204 //
205 FunctionPass *createFlattenCFGPass();
199206
200207 //===----------------------------------------------------------------------===//
201208 //
204204 TerminatorInst *SplitBlockAndInsertIfThen(Instruction *Cmp,
205205 bool Unreachable, MDNode *BranchWeights = 0);
206206
207 ///
208 /// GetIfCondition - Check whether BB is the merge point of a if-region.
209 /// If so, return the boolean condition that determines which entry into
210 /// BB will be taken. Also, return by references the block that will be
211 /// entered from if the condition is true, and the block that will be
212 /// entered if the condition is false.
213
214 Value *GetIfCondition(BasicBlock *BB, BasicBlock *&IfTrue,
215 BasicBlock *&IfFalse);
207216 } // End llvm namespace
208217
209218 #endif
136136 /// the basic block that was pointed to.
137137 ///
138138 bool SimplifyCFG(BasicBlock *BB, const TargetTransformInfo &TTI,
139 const DataLayout *TD = 0, AliasAnalysis *AA = 0);
139 const DataLayout *TD = 0);
140
141 /// FlatternCFG - This function is used to flatten a CFG. For
142 /// example, it uses parallel-and and parallel-or mode to collapse
143 // if-conditions and merge if-regions with identical statements.
144 ///
145 bool FlattenCFG(BasicBlock *BB, AliasAnalysis *AA = 0);
140146
141147 /// FoldBranchToCommonDest - If this basic block is ONLY a setcc and a branch,
142148 /// and if a predecessor branches to us and one of our successors, fold the
9090 AMDGPUTargetMachine &getAMDGPUTargetMachine() const {
9191 return getTM();
9292 }
93
9493 virtual bool addPreISel();
9594 virtual bool addInstSelector();
9695 virtual bool addPreRegAlloc();
119118 bool
120119 AMDGPUPassConfig::addPreISel() {
121120 const AMDGPUSubtarget &ST = TM->getSubtarget();
121 addPass(createFlattenCFGPass());
122122 if (ST.getGeneration() > AMDGPUSubtarget::NORTHERN_ISLANDS) {
123123 addPass(createStructurizeCFGPass());
124124 addPass(createSIAnnotateControlFlowPass());
234234 }
235235
236236 MPM.add(createAggressiveDCEPass()); // Delete dead instructions
237 MPM.add(createCFGSimplificationPass(true)); // Merge & remove BBs
237 MPM.add(createCFGSimplificationPass()); // Merge & remove BBs
238238 MPM.add(createInstructionCombiningPass()); // Clean up after everything.
239239
240240 // As an experimental mode, run any vectorization passes in a separate
370370 PM.add(createJumpThreadingPass());
371371
372372 // Delete basic blocks, which optimization passes may have killed.
373 PM.add(createCFGSimplificationPass(true));
373 PM.add(createCFGSimplificationPass());
374374
375375 // Now that we have optimized the program, discard unreachable functions.
376376 PM.add(createGlobalDCEPass());
2727 Scalar.cpp
2828 ScalarReplAggregates.cpp
2929 SimplifyCFGPass.cpp
30 FlattenCFGPass.cpp
3031 Sink.cpp
3132 StructurizeCFG.cpp
3233 TailRecursionElimination.cpp
0 //===- FlattenCFGPass.cpp - CFG Flatten Pass ----------------------===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file implements flattening of CFG.
10 //
11 //===----------------------------------------------------------------------===//
12
13 #define DEBUG_TYPE "flattencfg"
14 #include "llvm/Transforms/Scalar.h"
15 #include "llvm/Analysis/AliasAnalysis.h"
16 #include "llvm/Pass.h"
17 #include "llvm/Support/CFG.h"
18 #include "llvm/Transforms/Utils/Local.h"
19 using namespace llvm;
20
21 namespace {
22 struct FlattenCFGPass : public FunctionPass {
23 static char ID; // Pass identification, replacement for typeid
24 public:
25 FlattenCFGPass() : FunctionPass(ID) {
26 initializeFlattenCFGPassPass(*PassRegistry::getPassRegistry());
27 }
28 bool runOnFunction(Function &F);
29
30 void getAnalysisUsage(AnalysisUsage &AU) const {
31 AU.addRequired();
32 }
33
34 private:
35 AliasAnalysis *AA;
36 };
37 }
38
39 char FlattenCFGPass::ID = 0;
40 INITIALIZE_PASS_BEGIN(FlattenCFGPass, "flattencfg", "Flatten the CFG", false,
41 false)
42 INITIALIZE_AG_DEPENDENCY(AliasAnalysis)
43 INITIALIZE_PASS_END(FlattenCFGPass, "flattencfg", "Flatten the CFG", false,
44 false)
45
46 // Public interface to the FlattenCFG pass
47 FunctionPass *llvm::createFlattenCFGPass() { return new FlattenCFGPass(); }
48
49 /// iterativelyFlattenCFG - Call FlattenCFG on all the blocks in the function,
50 /// iterating until no more changes are made.
51 static bool iterativelyFlattenCFG(Function &F, AliasAnalysis *AA) {
52 bool Changed = false;
53 bool LocalChange = true;
54 while (LocalChange) {
55 LocalChange = false;
56
57 // Loop over all of the basic blocks and remove them if they are unneeded...
58 //
59 for (Function::iterator BBIt = F.begin(); BBIt != F.end();) {
60 if (FlattenCFG(BBIt++, AA)) {
61 LocalChange = true;
62 }
63 }
64 Changed |= LocalChange;
65 }
66 return Changed;
67 }
68
69 bool FlattenCFGPass::runOnFunction(Function &F) {
70 AA = &getAnalysis();
71 bool EverChanged = false;
72 // iterativelyFlattenCFG can make some blocks dead.
73 while (iterativelyFlattenCFG(F, AA)) {
74 removeUnreachableBlocks(F);
75 EverChanged = true;
76 }
77 return EverChanged;
78 }
5656 initializeSROAPass(Registry);
5757 initializeSROA_DTPass(Registry);
5858 initializeSROA_SSAUpPass(Registry);
59 initializeCFGCanonicalizePass(Registry);
60 initializeCFGOptimizePass(Registry);
59 initializeCFGSimplifyPassPass(Registry);
6160 initializeStructurizeCFGPass(Registry);
6261 initializeSinkingPass(Registry);
6362 initializeTailCallElimPass(Registry);
2626 #include "llvm/ADT/SmallVector.h"
2727 #include "llvm/ADT/Statistic.h"
2828 #include "llvm/Analysis/TargetTransformInfo.h"
29 #include "llvm/Analysis/AliasAnalysis.h"
3029 #include "llvm/IR/Attributes.h"
3130 #include "llvm/IR/Constants.h"
3231 #include "llvm/IR/DataLayout.h"
4241
4342 namespace {
4443 struct CFGSimplifyPass : public FunctionPass {
45 CFGSimplifyPass(char &ID, bool isTargetAware)
46 : FunctionPass(ID), IsTargetAware(isTargetAware) {}
44 static char ID; // Pass identification, replacement for typeid
45 CFGSimplifyPass() : FunctionPass(ID) {
46 initializeCFGSimplifyPassPass(*PassRegistry::getPassRegistry());
47 }
4748 virtual bool runOnFunction(Function &F);
4849
4950 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
5051 AU.addRequired();
5152 }
52 private:
53 AliasAnalysis *AA;
54 bool IsTargetAware; // Should the pass be target-aware?
5553 };
56
57 // CFGSimplifyPass that does optimizations.
58 struct CFGOptimize : public CFGSimplifyPass {
59 static char ID; // Pass identification, replacement for typeid
60 public:
61 CFGOptimize() : CFGSimplifyPass(ID, true) {
62 initializeCFGOptimizePass(*PassRegistry::getPassRegistry());
63 }
64 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
65 AU.addRequired();
66 AU.addRequired();
67 }
68 };
69
70 // CFGSimplifyPass that does canonicalizations.
71 struct CFGCanonicalize : public CFGSimplifyPass {
72 static char ID; // Pass identification, replacement for typeid
73 public:
74 CFGCanonicalize() : CFGSimplifyPass(ID, false) {
75 initializeCFGCanonicalizePass(*PassRegistry::getPassRegistry());
76 }
77 };
78 }
79
80 char CFGCanonicalize::ID = 0;
81 char CFGOptimize::ID = 0;
82 INITIALIZE_PASS_BEGIN(CFGCanonicalize, "simplifycfg", "Simplify the CFG", false,
54 }
55
56 char CFGSimplifyPass::ID = 0;
57 INITIALIZE_PASS_BEGIN(CFGSimplifyPass, "simplifycfg", "Simplify the CFG", false,
8358 false)
8459 INITIALIZE_AG_DEPENDENCY(TargetTransformInfo)
85 INITIALIZE_PASS_END(CFGCanonicalize, "simplifycfg", "Simplify the CFG", false,
60 INITIALIZE_PASS_END(CFGSimplifyPass, "simplifycfg", "Simplify the CFG", false,
8661 false)
87 INITIALIZE_PASS_BEGIN(CFGOptimize, "optimizecfg", "optimize the CFG", false,
88 false)
89 INITIALIZE_AG_DEPENDENCY(TargetTransformInfo)
90 INITIALIZE_AG_DEPENDENCY(AliasAnalysis)
91 INITIALIZE_PASS_END(CFGOptimize, "optimizecfg", "Optimize the CFG", false,
92 false)
9362
9463 // Public interface to the CFGSimplification pass
95 FunctionPass *llvm::createCFGSimplificationPass(bool IsTargetAware) {
96 if (IsTargetAware)
97 return new CFGOptimize();
98 else
99 return new CFGCanonicalize();
64 FunctionPass *llvm::createCFGSimplificationPass() {
65 return new CFGSimplifyPass();
10066 }
10167
10268 /// changeToUnreachable - Insert an unreachable instruction before the specified
333299 /// iterativelySimplifyCFG - Call SimplifyCFG on all the blocks in the function,
334300 /// iterating until no more changes are made.
335301 static bool iterativelySimplifyCFG(Function &F, const TargetTransformInfo &TTI,
336 const DataLayout *TD, AliasAnalysis *AA) {
302 const DataLayout *TD) {
337303 bool Changed = false;
338304 bool LocalChange = true;
339305 while (LocalChange) {
342308 // Loop over all of the basic blocks and remove them if they are unneeded...
343309 //
344310 for (Function::iterator BBIt = F.begin(); BBIt != F.end(); ) {
345 if (SimplifyCFG(BBIt++, TTI, TD, AA)) {
311 if (SimplifyCFG(BBIt++, TTI, TD)) {
346312 LocalChange = true;
347313 ++NumSimpl;
348314 }
356322 // simplify the CFG.
357323 //
358324 bool CFGSimplifyPass::runOnFunction(Function &F) {
359 if (IsTargetAware)
360 AA = &getAnalysis();
361 else
362 AA = NULL;
363325 const TargetTransformInfo &TTI = getAnalysis();
364326 const DataLayout *TD = getAnalysisIfAvailable();
365327 bool EverChanged = removeUnreachableBlocksFromFn(F);
366328 EverChanged |= mergeEmptyReturnBlocks(F);
367 EverChanged |= iterativelySimplifyCFG(F, TTI, TD, AA);
329 EverChanged |= iterativelySimplifyCFG(F, TTI, TD);
368330
369331 // If neither pass changed anything, we're done.
370332 if (!EverChanged) return false;
378340 return true;
379341
380342 do {
381 EverChanged = iterativelySimplifyCFG(F, TTI, TD, AA);
343 EverChanged = iterativelySimplifyCFG(F, TTI, TD);
382344 EverChanged |= removeUnreachableBlocksFromFn(F);
383345 } while (EverChanged);
384346
664664 ReplaceInstWithInst(HeadOldTerm, HeadNewTerm);
665665 return CheckTerm;
666666 }
667
668 /// GetIfCondition - Given a basic block (BB) with two predecessors,
669 /// check to see if the merge at this block is due
670 /// to an "if condition". If so, return the boolean condition that determines
671 /// which entry into BB will be taken. Also, return by references the block
672 /// that will be entered from if the condition is true, and the block that will
673 /// be entered if the condition is false.
674 ///
675 /// This does no checking to see if the true/false blocks have large or unsavory
676 /// instructions in them.
677 Value *llvm::GetIfCondition(BasicBlock *BB, BasicBlock *&IfTrue,
678 BasicBlock *&IfFalse) {
679 PHINode *SomePHI = dyn_cast(BB->begin());
680 BasicBlock *Pred1 = NULL;
681 BasicBlock *Pred2 = NULL;
682
683 if (SomePHI) {
684 if (SomePHI->getNumIncomingValues() != 2)
685 return NULL;
686 Pred1 = SomePHI->getIncomingBlock(0);
687 Pred2 = SomePHI->getIncomingBlock(1);
688 } else {
689 pred_iterator PI = pred_begin(BB), PE = pred_end(BB);
690 if (PI == PE) // No predecessor
691 return NULL;
692 Pred1 = *PI++;
693 if (PI == PE) // Only one predecessor
694 return NULL;
695 Pred2 = *PI++;
696 if (PI != PE) // More than two predecessors
697 return NULL;
698 }
699
700 // We can only handle branches. Other control flow will be lowered to
701 // branches if possible anyway.
702 BranchInst *Pred1Br = dyn_cast(Pred1->getTerminator());
703 BranchInst *Pred2Br = dyn_cast(Pred2->getTerminator());
704 if (Pred1Br == 0 || Pred2Br == 0)
705 return 0;
706
707 // Eliminate code duplication by ensuring that Pred1Br is conditional if
708 // either are.
709 if (Pred2Br->isConditional()) {
710 // If both branches are conditional, we don't have an "if statement". In
711 // reality, we could transform this case, but since the condition will be
712 // required anyway, we stand no chance of eliminating it, so the xform is
713 // probably not profitable.
714 if (Pred1Br->isConditional())
715 return 0;
716
717 std::swap(Pred1, Pred2);
718 std::swap(Pred1Br, Pred2Br);
719 }
720
721 if (Pred1Br->isConditional()) {
722 // The only thing we have to watch out for here is to make sure that Pred2
723 // doesn't have incoming edges from other blocks. If it does, the condition
724 // doesn't dominate BB.
725 if (Pred2->getSinglePredecessor() == 0)
726 return 0;
727
728 // If we found a conditional branch predecessor, make sure that it branches
729 // to BB and Pred2Br. If it doesn't, this isn't an "if statement".
730 if (Pred1Br->getSuccessor(0) == BB &&
731 Pred1Br->getSuccessor(1) == Pred2) {
732 IfTrue = Pred1;
733 IfFalse = Pred2;
734 } else if (Pred1Br->getSuccessor(0) == Pred2 &&
735 Pred1Br->getSuccessor(1) == BB) {
736 IfTrue = Pred2;
737 IfFalse = Pred1;
738 } else {
739 // We know that one arm of the conditional goes to BB, so the other must
740 // go somewhere unrelated, and this must not be an "if statement".
741 return 0;
742 }
743
744 return Pred1Br->getCondition();
745 }
746
747 // Ok, if we got here, both predecessors end with an unconditional branch to
748 // BB. Don't panic! If both blocks only have a single (identical)
749 // predecessor, and THAT is a conditional branch, then we're all ok!
750 BasicBlock *CommonPred = Pred1->getSinglePredecessor();
751 if (CommonPred == 0 || CommonPred != Pred2->getSinglePredecessor())
752 return 0;
753
754 // Otherwise, if this is a conditional branch, then we can use it!
755 BranchInst *BI = dyn_cast(CommonPred->getTerminator());
756 if (BI == 0) return 0;
757
758 assert(BI->isConditional() && "Two successors but not conditional?");
759 if (BI->getSuccessor(0) == Pred1) {
760 IfTrue = Pred1;
761 IfFalse = Pred2;
762 } else {
763 IfTrue = Pred2;
764 IfFalse = Pred1;
765 }
766 return BI->getCondition();
767 }
2424 PromoteMemoryToRegister.cpp
2525 SSAUpdater.cpp
2626 SimplifyCFG.cpp
27 FlattenCFG.cpp
2728 SimplifyIndVar.cpp
2829 SimplifyInstructions.cpp
2930 SimplifyLibCalls.cpp
0 //===- FlatternCFG.cpp - Code to perform CFG flattening ---------------===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // Reduce conditional branches in CFG.
10 //
11 //===----------------------------------------------------------------------===//
12
13 #define DEBUG_TYPE "flattencfg"
14 #include "llvm/Transforms/Utils/Local.h"
15 #include "llvm/ADT/SmallPtrSet.h"
16 #include "llvm/Analysis/AliasAnalysis.h"
17 #include "llvm/Analysis/ValueTracking.h"
18 #include "llvm/IR/IRBuilder.h"
19 #include "llvm/Support/Debug.h"
20 #include "llvm/Transforms/Utils/BasicBlockUtils.h"
21 using namespace llvm;
22
23 namespace {
24 class FlattenCFGOpt {
25 AliasAnalysis *AA;
26 /// \brief Use parallel-and or parallel-or to generate conditions for
27 /// conditional branches.
28 bool FlattenParallelAndOr(BasicBlock *BB, IRBuilder<> &Builder, Pass *P = 0);
29 /// \brief If \param BB is the merge block of an if-region, attempt to merge
30 /// the if-region with an adjacent if-region upstream if two if-regions
31 /// contain identical instructions.
32 bool MergeIfRegion(BasicBlock *BB, IRBuilder<> &Builder, Pass *P = 0);
33 /// \brief Compare a pair of blocks: \p Block1 and \p Block2, which
34 /// are from two if-regions whose entry blocks are \p Head1 and \p
35 /// Head2. \returns true if \p Block1 and \p Block2 contain identical
36 /// instructions, and have no memory reference alias with \p Head2.
37 /// This is used as a legality check for merging if-regions.
38 bool CompareIfRegionBlock(BasicBlock *Head1, BasicBlock *Head2,
39 BasicBlock *Block1, BasicBlock *Block2);
40
41 public:
42 FlattenCFGOpt(AliasAnalysis *AA) : AA(AA) {}
43 bool run(BasicBlock *BB);
44 };
45 }
46
47 /// If \param [in] BB has more than one predecessor that is a conditional
48 /// branch, attempt to use parallel and/or for the branch condition. \returns
49 /// true on success.
50 ///
51 /// Before:
52 /// ......
53 /// %cmp10 = fcmp une float %tmp1, %tmp2
54 /// br i1 %cmp1, label %if.then, label %lor.rhs
55 ///
56 /// lor.rhs:
57 /// ......
58 /// %cmp11 = fcmp une float %tmp3, %tmp4
59 /// br i1 %cmp11, label %if.then, label %ifend
60 ///
61 /// if.end: // the merge block
62 /// ......
63 ///
64 /// if.then: // has two predecessors, both of them contains conditional branch.
65 /// ......
66 /// br label %if.end;
67 ///
68 /// After:
69 /// ......
70 /// %cmp10 = fcmp une float %tmp1, %tmp2
71 /// ......
72 /// %cmp11 = fcmp une float %tmp3, %tmp4
73 /// %cmp12 = or i1 %cmp10, %cmp11 // parallel-or mode.
74 /// br i1 %cmp12, label %if.then, label %ifend
75 ///
76 /// if.end:
77 /// ......
78 ///
79 /// if.then:
80 /// ......
81 /// br label %if.end;
82 ///
83 /// Current implementation handles two cases.
84 /// Case 1: \param BB is on the else-path.
85 ///
86 /// BB1
87 /// / |
88 /// BB2 |
89 /// / \ |
90 /// BB3 \ | where, BB1, BB2 contain conditional branches.
91 /// \ | / BB3 contains unconditional branch.
92 /// \ | / BB4 corresponds to \param BB which is also the merge.
93 /// BB => BB4
94 ///
95 ///
96 /// Corresponding source code:
97 ///
98 /// if (a == b && c == d)
99 /// statement; // BB3
100 ///
101 /// Case 2: \param BB BB is on the then-path.
102 ///
103 /// BB1
104 /// / |
105 /// | BB2
106 /// \ / | where BB1, BB2 contain conditional branches.
107 /// BB => BB3 | BB3 contains unconditiona branch and corresponds
108 /// \ / to \param BB. BB4 is the merge.
109 /// BB4
110 ///
111 /// Corresponding source code:
112 ///
113 /// if (a == b || c == d)
114 /// statement; // BB3
115 ///
116 /// In both cases, \param BB is the common successor of conditional branches.
117 /// In Case 1, \param BB (BB4) has an unconditional branch (BB3) as
118 /// its predecessor. In Case 2, \param BB (BB3) only has conditional branches
119 /// as its predecessors.
120 ///
121 bool FlattenCFGOpt::FlattenParallelAndOr(BasicBlock *BB, IRBuilder<> &Builder,
122 Pass *P) {
123 PHINode *PHI = dyn_cast(BB->begin());
124 if (PHI)
125 return false; // For simplicity, avoid cases containing PHI nodes.
126
127 BasicBlock *LastCondBlock = NULL;
128 BasicBlock *FirstCondBlock = NULL;
129 BasicBlock *UnCondBlock = NULL;
130 int Idx = -1;
131
132 // Check predecessors of \param BB.
133 SmallPtrSet Preds(pred_begin(BB), pred_end(BB));
134 for (SmallPtrSetIterator PI = Preds.begin(), PE = Preds.end();
135 PI != PE; ++PI) {
136 BasicBlock *Pred = *PI;
137 BranchInst *PBI = dyn_cast(Pred->getTerminator());
138
139 // All predecessors should terminate with a branch.
140 if (!PBI)
141 return false;
142
143 BasicBlock *PP = Pred->getSinglePredecessor();
144
145 if (PBI->isUnconditional()) {
146 // Case 1: Pred (BB3) is an unconditional block, it should
147 // have a single predecessor (BB2) that is also a predecessor
148 // of \param BB (BB4) and should not have address-taken.
149 // There should exist only one such unconditional
150 // branch among the predecessors.
151 if (UnCondBlock || !PP || (Preds.count(PP) == 0) ||
152 Pred->hasAddressTaken())
153 return false;
154
155 UnCondBlock = Pred;
156 continue;
157 }
158
159 // Only conditional branches are allowed beyond this point.
160 assert(PBI->isConditional());
161
162 // Condition's unique use should be the branch instruction.
163 Value *PC = PBI->getCondition();
164 if (!PC || !PC->hasOneUse())
165 return false;
166
167 if (PP && Preds.count(PP)) {
168 // These are internal condition blocks to be merged from, e.g.,
169 // BB2 in both cases.
170 // Should not be address-taken.
171 if (Pred->hasAddressTaken())
172 return false;
173
174 // Instructions in the internal condition blocks should be safe
175 // to hoist up.
176 for (BasicBlock::iterator BI = Pred->begin(), BE = PBI; BI != BE;) {
177 Instruction *CI = BI++;
178 if (isa(CI) || !isSafeToSpeculativelyExecute(CI))
179 return false;
180 }
181 } else {
182 // This is the condition block to be merged into, e.g. BB1 in
183 // both cases.
184 if (FirstCondBlock)
185 return false;
186 FirstCondBlock = Pred;
187 }
188
189 // Find whether BB is uniformly on the true (or false) path
190 // for all of its predecessors.
191 BasicBlock *PS1 = PBI->getSuccessor(0);
192 BasicBlock *PS2 = PBI->getSuccessor(1);
193 BasicBlock *PS = (PS1 == BB) ? PS2 : PS1;
194 int CIdx = (PS1 == BB) ? 0 : 1;
195
196 if (Idx == -1)
197 Idx = CIdx;
198 else if (CIdx != Idx)
199 return false;
200
201 // PS is the successor which is not BB. Check successors to identify
202 // the last conditional branch.
203 if (Preds.count(PS) == 0) {
204 // Case 2.
205 LastCondBlock = Pred;
206 } else {
207 // Case 1
208 BranchInst *BPS = dyn_cast(PS->getTerminator());
209 if (BPS && BPS->isUnconditional()) {
210 // Case 1: PS(BB3) should be an unconditional branch.
211 LastCondBlock = Pred;
212 }
213 }
214 }
215
216 if (!FirstCondBlock || !LastCondBlock || (FirstCondBlock == LastCondBlock))
217 return false;
218
219 TerminatorInst *TBB = LastCondBlock->getTerminator();
220 BasicBlock *PS1 = TBB->getSuccessor(0);
221 BasicBlock *PS2 = TBB->getSuccessor(1);
222 BranchInst *PBI1 = dyn_cast(PS1->getTerminator());
223 BranchInst *PBI2 = dyn_cast(PS2->getTerminator());
224
225 // If PS1 does not jump into PS2, but PS2 jumps into PS1,
226 // attempt branch inversion.
227 if (!PBI1 || !PBI1->isUnconditional() ||
228 (PS1->getTerminator()->getSuccessor(0) != PS2)) {
229 // Check whether PS2 jumps into PS1.
230 if (!PBI2 || !PBI2->isUnconditional() ||
231 (PS2->getTerminator()->getSuccessor(0) != PS1))
232 return false;
233
234 // Do branch inversion.
235 BasicBlock *CurrBlock = LastCondBlock;
236 bool EverChanged = false;
237 while (1) {
238 BranchInst *BI = dyn_cast(CurrBlock->getTerminator());
239 CmpInst *CI = dyn_cast(BI->getCondition());
240 CmpInst::Predicate Predicate = CI->getPredicate();
241 // Cannonicalize icmp_ne -> icmp_eq, fcmp_one -> fcmp_oeq
242 if ((Predicate == CmpInst::ICMP_NE) || (Predicate == CmpInst::FCMP_ONE)) {
243 CI->setPredicate(ICmpInst::getInversePredicate(Predicate));
244 BI->swapSuccessors();
245 EverChanged = true;
246 }
247 if (CurrBlock == FirstCondBlock)
248 break;
249 CurrBlock = CurrBlock->getSinglePredecessor();
250 }
251 return EverChanged;
252 }
253
254 // PS1 must have a conditional branch.
255 if (!PBI1 || !PBI1->isUnconditional())
256 return false;
257
258 // PS2 should not contain PHI node.
259 PHI = dyn_cast(PS2->begin());
260 if (PHI)
261 return false;
262
263 // Do the transformation.
264 BasicBlock *CB;
265 BranchInst *PBI = dyn_cast(FirstCondBlock->getTerminator());
266 bool Iteration = true;
267 BasicBlock *SaveInsertBB = Builder.GetInsertBlock();
268 BasicBlock::iterator SaveInsertPt = Builder.GetInsertPoint();
269 Value *PC = PBI->getCondition();
270
271 do {
272 CB = PBI->getSuccessor(1 - Idx);
273 // Delete the conditional branch.
274 FirstCondBlock->getInstList().pop_back();
275 FirstCondBlock->getInstList()
276 .splice(FirstCondBlock->end(), CB->getInstList());
277 PBI = cast(FirstCondBlock->getTerminator());
278 Value *CC = PBI->getCondition();
279 // Merge conditions.
280 Builder.SetInsertPoint(PBI);
281 Value *NC;
282 if (Idx == 0)
283 // Case 2, use parallel or.
284 NC = Builder.CreateOr(PC, CC);
285 else
286 // Case 1, use parallel and.
287 NC = Builder.CreateAnd(PC, CC);
288
289 PBI->replaceUsesOfWith(CC, NC);
290 PC = NC;
291 if (CB == LastCondBlock)
292 Iteration = false;
293 // Remove internal conditional branches.
294 CB->dropAllReferences();
295 // make CB unreachable and let downstream to delete the block.
296 new UnreachableInst(CB->getContext(), CB);
297 } while (Iteration);
298
299 Builder.SetInsertPoint(SaveInsertBB, SaveInsertPt);
300 DEBUG(dbgs() << "Use parallel and/or in:\n" << *FirstCondBlock);
301 return true;
302 }
303
304 /// Compare blocks from two if-regions, where \param Head1 is the entry of the
305 /// 1st if-region. \param Head2 is the entry of the 2nd if-region. \param
306 /// Block1 is a block in the 1st if-region to compare. \param Block2 is a block
307 // in the 2nd if-region to compare. \returns true if \param Block1 and \param
308 /// Block2 have identical instructions and do not have memory reference alias
309 /// with \param Head2.
310 ///
311 bool FlattenCFGOpt::CompareIfRegionBlock(BasicBlock *Head1, BasicBlock *Head2,
312 BasicBlock *Block1,
313 BasicBlock *Block2) {
314 TerminatorInst *PTI2 = Head2->getTerminator();
315 Instruction *PBI2 = Head2->begin();
316
317 bool eq1 = (Block1 == Head1);
318 bool eq2 = (Block2 == Head2);
319 if (eq1 || eq2) {
320 // An empty then-path or else-path.
321 return (eq1 == eq2);
322 }
323
324 // Check whether instructions in Block1 and Block2 are identical
325 // and do not alias with instructions in Head2.
326 BasicBlock::iterator iter1 = Block1->begin();
327 BasicBlock::iterator end1 = Block1->getTerminator();
328 BasicBlock::iterator iter2 = Block2->begin();
329 BasicBlock::iterator end2 = Block2->getTerminator();
330
331 while (1) {
332 if (iter1 == end1) {
333 if (iter2 != end2)
334 return false;
335 break;
336 }
337
338 if (!iter1->isIdenticalTo(iter2))
339 return false;
340
341 // Illegal to remove instructions with side effects except
342 // non-volatile stores.
343 if (iter1->mayHaveSideEffects()) {
344 Instruction *CurI = &*iter1;
345 StoreInst *SI = dyn_cast(CurI);
346 if (!SI || SI->isVolatile())
347 return false;
348 }
349
350 // For simplicity and speed, data dependency check can be
351 // avoided if read from memory doesn't exist.
352 if (iter1->mayReadFromMemory())
353 return false;
354
355 if (iter1->mayWriteToMemory()) {
356 for (BasicBlock::iterator BI = PBI2, BE = PTI2; BI != BE; ++BI) {
357 if (BI->mayReadFromMemory() || BI->mayWriteToMemory()) {
358 // Check alias with Head2.
359 if (!AA || AA->alias(iter1, BI))
360 return false;
361 }
362 }
363 }
364 ++iter1;
365 ++iter2;
366 }
367
368 return true;
369 }
370
371 /// Check whether \param BB is the merge block of a if-region. If yes, check
372 /// whether there exists an adjacent if-region upstream, the two if-regions
373 /// contain identical instuctions and can be legally merged. \returns true if
374 /// the two if-regions are merged.
375 ///
376 /// From:
377 /// if (a)
378 /// statement;
379 /// if (b)
380 /// statement;
381 ///
382 /// To:
383 /// if (a || b)
384 /// statement;
385 ///
386 bool FlattenCFGOpt::MergeIfRegion(BasicBlock *BB, IRBuilder<> &Builder,
387 Pass *P) {
388 BasicBlock *IfTrue2, *IfFalse2;
389 Value *IfCond2 = GetIfCondition(BB, IfTrue2, IfFalse2);
390 Instruction *CInst2 = dyn_cast_or_null(IfCond2);
391 if (!CInst2)
392 return false;
393
394 BasicBlock *SecondEntryBlock = CInst2->getParent();
395 if (SecondEntryBlock->hasAddressTaken())
396 return false;
397
398 BasicBlock *IfTrue1, *IfFalse1;
399 Value *IfCond1 = GetIfCondition(SecondEntryBlock, IfTrue1, IfFalse1);
400 Instruction *CInst1 = dyn_cast_or_null(IfCond1);
401 if (!CInst1)
402 return false;
403
404 BasicBlock *FirstEntryBlock = CInst1->getParent();
405
406 // Either then-path or else-path should be empty.
407 if ((IfTrue1 != FirstEntryBlock) && (IfFalse1 != FirstEntryBlock))
408 return false;
409 if ((IfTrue2 != SecondEntryBlock) && (IfFalse2 != SecondEntryBlock))
410 return false;
411
412 TerminatorInst *PTI2 = SecondEntryBlock->getTerminator();
413 Instruction *PBI2 = SecondEntryBlock->begin();
414
415 if (!CompareIfRegionBlock(FirstEntryBlock, SecondEntryBlock, IfTrue1,
416 IfTrue2))
417 return false;
418
419 if (!CompareIfRegionBlock(FirstEntryBlock, SecondEntryBlock, IfFalse1,
420 IfFalse2))
421 return false;
422
423 // Check whether \param SecondEntryBlock has side-effect and is safe to
424 // speculate.
425 for (BasicBlock::iterator BI = PBI2, BE = PTI2; BI != BE; ++BI) {
426 Instruction *CI = BI;
427 if (isa(CI) || CI->mayHaveSideEffects() ||
428 !isSafeToSpeculativelyExecute(CI))
429 return false;
430 }
431
432 // Merge \param SecondEntryBlock into \param FirstEntryBlock.
433 FirstEntryBlock->getInstList().pop_back();
434 FirstEntryBlock->getInstList()
435 .splice(FirstEntryBlock->end(), SecondEntryBlock->getInstList());
436 BranchInst *PBI = dyn_cast(FirstEntryBlock->getTerminator());
437 Value *CC = PBI->getCondition();
438 BasicBlock *SaveInsertBB = Builder.GetInsertBlock();
439 BasicBlock::iterator SaveInsertPt = Builder.GetInsertPoint();
440 Builder.SetInsertPoint(PBI);
441 Value *NC = Builder.CreateOr(CInst1, CC);
442 PBI->replaceUsesOfWith(CC, NC);
443 Builder.SetInsertPoint(SaveInsertBB, SaveInsertPt);
444
445 // Remove IfTrue1
446 if (IfTrue1 != FirstEntryBlock) {
447 IfTrue1->dropAllReferences();
448 IfTrue1->eraseFromParent();
449 }
450
451 // Remove IfFalse1
452 if (IfFalse1 != FirstEntryBlock) {
453 IfFalse1->dropAllReferences();
454 IfFalse1->eraseFromParent();
455 }
456
457 // Remove \param SecondEntryBlock
458 SecondEntryBlock->dropAllReferences();
459 SecondEntryBlock->eraseFromParent();
460 DEBUG(dbgs() << "If conditions merged into:\n" << *FirstEntryBlock);
461 return true;
462 }
463
464 bool FlattenCFGOpt::run(BasicBlock *BB) {
465 bool Changed = false;
466 assert(BB && BB->getParent() && "Block not embedded in function!");
467 assert(BB->getTerminator() && "Degenerate basic block encountered!");
468
469 IRBuilder<> Builder(BB);
470
471 if (FlattenParallelAndOr(BB, Builder))
472 return true;
473
474 if (MergeIfRegion(BB, Builder))
475 return true;
476
477 return Changed;
478 }
479
480 /// FlattenCFG - This function is used to flatten a CFG. For
481 /// example, it uses parallel-and and parallel-or mode to collapse
482 // if-conditions and merge if-regions with identical statements.
483 ///
484 bool llvm::FlattenCFG(BasicBlock *BB, AliasAnalysis *AA) {
485 return FlattenCFGOpt(AA).run(BB);
486 }
1818 #include "llvm/ADT/SmallPtrSet.h"
1919 #include "llvm/ADT/SmallVector.h"
2020 #include "llvm/ADT/Statistic.h"
21 #include "llvm/Analysis/AliasAnalysis.h"
2221 #include "llvm/Analysis/InstructionSimplify.h"
2322 #include "llvm/Analysis/TargetTransformInfo.h"
2423 #include "llvm/Analysis/ValueTracking.h"
6564 HoistCondStores("simplifycfg-hoist-cond-stores", cl::Hidden, cl::init(true),
6665 cl::desc("Hoist conditional stores if an unconditional store preceeds"));
6766
68 static cl::opt
69 ParallelAndOr("simplifycfg-parallel-and-or", cl::Hidden, cl::init(true),
70 cl::desc("Use parallel-and-or mode for branch conditions"));
71
7267 STATISTIC(NumBitMaps, "Number of switch instructions turned into bitmaps");
7368 STATISTIC(NumLookupTables, "Number of switch instructions turned into lookup tables");
7469 STATISTIC(NumSinkCommons, "Number of common instructions sunk down to the end block");
9489 class SimplifyCFGOpt {
9590 const TargetTransformInfo &TTI;
9691 const DataLayout *const TD;
97 AliasAnalysis *AA;
98
9992 Value *isValueEqualityComparison(TerminatorInst *TI);
10093 BasicBlock *GetValueEqualityComparisonCases(TerminatorInst *TI,
10194 std::vector &Cases);
112105 bool SimplifyIndirectBr(IndirectBrInst *IBI);
113106 bool SimplifyUncondBranch(BranchInst *BI, IRBuilder <> &Builder);
114107 bool SimplifyCondBranch(BranchInst *BI, IRBuilder <>&Builder);
115 /// \brief Use parallel-and or parallel-or to generate conditions for
116 /// conditional branches.
117 bool SimplifyParallelAndOr(BasicBlock *BB, IRBuilder<> &Builder, Pass *P = 0);
118 /// \brief If \param BB is the merge block of an if-region, attempt to merge
119 /// the if-region with an adjacent if-region upstream if two if-regions
120 /// contain identical instructions.
121 bool MergeIfRegion(BasicBlock *BB, IRBuilder<> &Builder, Pass *P = 0);
122 /// \brief Compare a pair of blocks: \p Block1 and \p Block2, which
123 /// are from two if-regions whose entry blocks are \p Head1 and \p
124 /// Head2. \returns true if \p Block1 and \p Block2 contain identical
125 /// instructions, and have no memory reference alias with \p Head2.
126 /// This is used as a legality check for merging if-regions.
127 bool CompareIfRegionBlock(BasicBlock *Head1, BasicBlock *Head2,
128 BasicBlock *Block1, BasicBlock *Block2);
129108
130109 public:
131 SimplifyCFGOpt(const TargetTransformInfo &TTI, const DataLayout *TD,
132 AliasAnalysis *AA)
133 : TTI(TTI), TD(TD), AA(AA) {}
110 SimplifyCFGOpt(const TargetTransformInfo &TTI, const DataLayout *TD)
111 : TTI(TTI), TD(TD) {}
134112 bool run(BasicBlock *BB);
135113 };
136114 }
214192 for (BasicBlock::iterator I = Succ->begin();
215193 (PN = dyn_cast(I)); ++I)
216194 PN->addIncoming(PN->getIncomingValueForBlock(ExistPred), NewPred);
217 }
218
219
220 /// GetIfCondition - Given a basic block (BB) with two predecessors,
221 /// check to see if the merge at this block is due
222 /// to an "if condition". If so, return the boolean condition that determines
223 /// which entry into BB will be taken. Also, return by references the block
224 /// that will be entered from if the condition is true, and the block that will
225 /// be entered if the condition is false.
226 ///
227 /// This does no checking to see if the true/false blocks have large or unsavory
228 /// instructions in them.
229 static Value *GetIfCondition(BasicBlock *BB, BasicBlock *&IfTrue,
230 BasicBlock *&IfFalse) {
231 PHINode *SomePHI = dyn_cast(BB->begin());
232 BasicBlock *Pred1 = NULL;
233 BasicBlock *Pred2 = NULL;
234
235 if (SomePHI) {
236 if (SomePHI->getNumIncomingValues() != 2)
237 return NULL;
238 Pred1 = SomePHI->getIncomingBlock(0);
239 Pred2 = SomePHI->getIncomingBlock(1);
240 } else {
241 pred_iterator PI = pred_begin(BB), PE = pred_end(BB);
242 if (PI == PE) // No predecessor
243 return NULL;
244 Pred1 = *PI++;
245 if (PI == PE) // Only one predecessor
246 return NULL;
247 Pred2 = *PI++;
248 if (PI != PE) // More than two predecessors
249 return NULL;
250 }
251
252 // We can only handle branches. Other control flow will be lowered to
253 // branches if possible anyway.
254 BranchInst *Pred1Br = dyn_cast(Pred1->getTerminator());
255 BranchInst *Pred2Br = dyn_cast(Pred2->getTerminator());
256 if (Pred1Br == 0 || Pred2Br == 0)
257 return 0;
258
259 // Eliminate code duplication by ensuring that Pred1Br is conditional if
260 // either are.
261 if (Pred2Br->isConditional()) {
262 // If both branches are conditional, we don't have an "if statement". In
263 // reality, we could transform this case, but since the condition will be
264 // required anyway, we stand no chance of eliminating it, so the xform is
265 // probably not profitable.
266 if (Pred1Br->isConditional())
267 return 0;
268
269 std::swap(Pred1, Pred2);
270 std::swap(Pred1Br, Pred2Br);
271 }
272
273 if (Pred1Br->isConditional()) {
274 // The only thing we have to watch out for here is to make sure that Pred2
275 // doesn't have incoming edges from other blocks. If it does, the condition
276 // doesn't dominate BB.
277 if (Pred2->getSinglePredecessor() == 0)
278 return 0;
279
280 // If we found a conditional branch predecessor, make sure that it branches
281 // to BB and Pred2Br. If it doesn't, this isn't an "if statement".
282 if (Pred1Br->getSuccessor(0) == BB &&
283 Pred1Br->getSuccessor(1) == Pred2) {
284 IfTrue = Pred1;
285 IfFalse = Pred2;
286 } else if (Pred1Br->getSuccessor(0) == Pred2 &&
287 Pred1Br->getSuccessor(1) == BB) {
288 IfTrue = Pred2;
289 IfFalse = Pred1;
290 } else {
291 // We know that one arm of the conditional goes to BB, so the other must
292 // go somewhere unrelated, and this must not be an "if statement".
293 return 0;
294 }
295
296 return Pred1Br->getCondition();
297 }
298
299 // Ok, if we got here, both predecessors end with an unconditional branch to
300 // BB. Don't panic! If both blocks only have a single (identical)
301 // predecessor, and THAT is a conditional branch, then we're all ok!
302 BasicBlock *CommonPred = Pred1->getSinglePredecessor();
303 if (CommonPred == 0 || CommonPred != Pred2->getSinglePredecessor())
304 return 0;
305
306 // Otherwise, if this is a conditional branch, then we can use it!
307 BranchInst *BI = dyn_cast(CommonPred->getTerminator());
308 if (BI == 0) return 0;
309
310 assert(BI->isConditional() && "Two successors but not conditional?");
311 if (BI->getSuccessor(0) == Pred1) {
312 IfTrue = Pred1;
313 IfFalse = Pred2;
314 } else {
315 IfTrue = Pred2;
316 IfFalse = Pred1;
317 }
318 return BI->getCondition();
319195 }
320196
321197 /// ComputeSpeculationCost - Compute an abstract "cost" of speculating the
41013977 return false;
41023978 }
41033979
4104 /// If \param [in] BB has more than one predecessor that is a conditional
4105 /// branch, attempt to use parallel and/or for the branch condition. \returns
4106 /// true on success.
4107 ///
4108 /// Before:
4109 /// ......
4110 /// %cmp10 = fcmp une float %tmp1, %tmp2
4111 /// br i1 %cmp1, label %if.then, label %lor.rhs
4112 ///
4113 /// lor.rhs:
4114 /// ......
4115 /// %cmp11 = fcmp une float %tmp3, %tmp4
4116 /// br i1 %cmp11, label %if.then, label %ifend
4117 ///
4118 /// if.end: // the merge block
4119 /// ......
4120 ///
4121 /// if.then: // has two predecessors, both of them contains conditional branch.
4122 /// ......
4123 /// br label %if.end;
4124 ///
4125 /// After:
4126 /// ......
4127 /// %cmp10 = fcmp une float %tmp1, %tmp2
4128 /// ......
4129 /// %cmp11 = fcmp une float %tmp3, %tmp4
4130 /// %cmp12 = or i1 %cmp10, %cmp11 // parallel-or mode.
4131 /// br i1 %cmp12, label %if.then, label %ifend
4132 ///
4133 /// if.end:
4134 /// ......
4135 ///
4136 /// if.then:
4137 /// ......
4138 /// br label %if.end;
4139 ///
4140 /// Current implementation handles two cases.
4141 /// Case 1: \param BB is on the else-path.
4142 ///
4143 /// BB1
4144 /// / |
4145 /// BB2 |
4146 /// / \ |
4147 /// BB3 \ | where, BB1, BB2 contain conditional branches.
4148 /// \ | / BB3 contains unconditional branch.
4149 /// \ | / BB4 corresponds to \param BB which is also the merge.
4150 /// BB => BB4
4151 ///
4152 ///
4153 /// Corresponding source code:
4154 ///
4155 /// if (a == b && c == d)
4156 /// statement; // BB3
4157 ///
4158 /// Case 2: \param BB BB is on the then-path.
4159 ///
4160 /// BB1
4161 /// / |
4162 /// | BB2
4163 /// \ / | where BB1, BB2 contain conditional branches.
4164 /// BB => BB3 | BB3 contains unconditiona branch and corresponds
4165 /// \ / to \param BB. BB4 is the merge.
4166 /// BB4
4167 ///
4168 /// Corresponding source code:
4169 ///
4170 /// if (a == b || c == d)
4171 /// statement; // BB3
4172 ///
4173 /// In both cases, \param BB is the common successor of conditional branches.
4174 /// In Case 1, \param BB (BB4) has an unconditional branch (BB3) as
4175 /// its predecessor. In Case 2, \param BB (BB3) only has conditional branches
4176 /// as its predecessors.
4177 ///
4178 bool SimplifyCFGOpt::SimplifyParallelAndOr(BasicBlock *BB, IRBuilder<> &Builder,
4179 Pass *P) {
4180 PHINode *PHI = dyn_cast(BB->begin());
4181 if (PHI)
4182 return false; // For simplicity, avoid cases containing PHI nodes.
4183
4184 BasicBlock *LastCondBlock = NULL;
4185 BasicBlock *FirstCondBlock = NULL;
4186 BasicBlock *UnCondBlock = NULL;
4187 int Idx = -1;
4188
4189 // Check predecessors of \param BB.
4190 SmallPtrSet Preds(pred_begin(BB), pred_end(BB));
4191 for (SmallPtrSetIterator PI = Preds.begin(), PE = Preds.end();
4192 PI != PE; ++PI) {
4193 BasicBlock *Pred = *PI;
4194 BranchInst *PBI = dyn_cast(Pred->getTerminator());
4195
4196 // All predecessors should terminate with a branch.
4197 if (!PBI)
4198 return false;
4199
4200 BasicBlock *PP = Pred->getSinglePredecessor();
4201
4202 if (PBI->isUnconditional()) {
4203 // Case 1: Pred (BB3) is an unconditional block, it should
4204 // have a single predecessor (BB2) that is also a predecessor
4205 // of \param BB (BB4) and should not have address-taken.
4206 // There should exist only one such unconditional
4207 // branch among the predecessors.
4208 if (UnCondBlock || !PP || (Preds.count(PP) == 0) ||
4209 Pred->hasAddressTaken())
4210 return false;
4211
4212 UnCondBlock = Pred;
4213 continue;
4214 }
4215
4216 // Only conditional branches are allowed beyond this point.
4217 assert(PBI->isConditional());
4218
4219 // Condition's unique use should be the branch instruction.
4220 Value *PC = PBI->getCondition();
4221 if (!PC || !PC->hasOneUse())
4222 return false;
4223
4224 if (PP && Preds.count(PP)) {
4225 // These are internal condition blocks to be merged from, e.g.,
4226 // BB2 in both cases.
4227 // Should not be address-taken.
4228 if (Pred->hasAddressTaken())
4229 return false;
4230
4231 // Instructions in the internal condition blocks should be safe
4232 // to hoist up.
4233 for (BasicBlock::iterator BI = Pred->begin(), BE = PBI; BI != BE;) {
4234 Instruction *CI = BI++;
4235 if (isa(CI) ||
4236 !isSafeToSpeculativelyExecute(CI))
4237 return false;
4238 }
4239 } else {
4240 // This is the condition block to be merged into, e.g. BB1 in
4241 // both cases.
4242 if (FirstCondBlock)
4243 return false;
4244 FirstCondBlock = Pred;
4245 }
4246
4247 // Find whether BB is uniformly on the true (or false) path
4248 // for all of its predecessors.
4249 BasicBlock *PS1 = PBI->getSuccessor(0);
4250 BasicBlock *PS2 = PBI->getSuccessor(1);
4251 BasicBlock *PS = (PS1 == BB) ? PS2 : PS1;
4252 int CIdx = (PS1 == BB) ? 0 : 1;
4253
4254 if (Idx == -1)
4255 Idx = CIdx;
4256 else if (CIdx != Idx)
4257 return false;
4258
4259 // PS is the successor which is not BB. Check successors to identify
4260 // the last conditional branch.
4261 if (Preds.count(PS) == 0) {
4262 // Case 2.
4263 // BB must have an unique successor.
4264 TerminatorInst *TBB = BB->getTerminator();
4265 if (TBB->getNumSuccessors() != 1)
4266 return false;
4267
4268 BasicBlock *SBB = TBB->getSuccessor(0);
4269 PHI = dyn_cast(SBB->begin());
4270 if (PHI)
4271 return false;
4272
4273 // PS (BB4) should be BB's successor.
4274 if (SBB != PS)
4275 return false;
4276 LastCondBlock = Pred;
4277 } else {
4278 BranchInst *BPS = dyn_cast(PS->getTerminator());
4279 if (BPS && BPS->isUnconditional()) {
4280 // Case 1: PS(BB3) should be an unconditional branch.
4281 LastCondBlock = Pred;
4282 }
4283 }
4284 }
4285
4286 if (!FirstCondBlock || !LastCondBlock || (FirstCondBlock == LastCondBlock))
4287 return false;
4288
4289 // Do the transformation.
4290 BasicBlock *CB;
4291 bool Iteration = true;
4292 BasicBlock *SaveInsertBB = Builder.GetInsertBlock();
4293 BasicBlock::iterator SaveInsertPt = Builder.GetInsertPoint();
4294 BranchInst *PBI = dyn_cast(FirstCondBlock->getTerminator());
4295 Value *PC = PBI->getCondition();
4296 do {
4297 CB = PBI->getSuccessor(1 - Idx);
4298 // Delete the conditional branch.
4299 FirstCondBlock->getInstList().pop_back();
4300 FirstCondBlock->getInstList().splice(FirstCondBlock->end(), CB->getInstList());
4301 PBI = cast(FirstCondBlock->getTerminator());
4302 Value *CC = PBI->getCondition();
4303 // Merge conditions.
4304 Builder.SetInsertPoint(PBI);
4305 Value *NC;
4306 if (Idx == 0)
4307 // Case 2, use parallel or.
4308 NC = Builder.CreateOr(PC, CC);
4309 else
4310 // Case 1, use parallel and.
4311 NC = Builder.CreateAnd(PC, CC);
4312
4313 PBI->replaceUsesOfWith(CC, NC);
4314 PC = NC;
4315 if (CB == LastCondBlock)
4316 Iteration = false;
4317 // Remove internal conditional branches.
4318 CB->dropAllReferences();
4319 // make CB unreachable and let downstream to delete the block.
4320 new UnreachableInst(CB->getContext(), CB);
4321 } while (Iteration);
4322 if (SaveInsertBB)
4323 Builder.SetInsertPoint(SaveInsertBB, SaveInsertPt);
4324 DEBUG(dbgs() << "Use parallel and/or in:\n" << *FirstCondBlock);
4325 return true;
4326 }
4327
4328 /// Compare blocks from two if-regions, where \param Head1 is the entry of the
4329 /// 1st if-region. \param Head2 is the entry of the 2nd if-region. \param
4330 /// Block1 is a block in the 1st if-region to compare. \param Block2 is a block
4331 // in the 2nd if-region to compare. \returns true if \param Block1 and \param
4332 /// Block2 have identical instructions and do not have memory reference alias
4333 /// with \param Head2.
4334 ///
4335 bool SimplifyCFGOpt::CompareIfRegionBlock(BasicBlock *Head1, BasicBlock *Head2,
4336 BasicBlock *Block1, BasicBlock *Block2) {
4337 TerminatorInst *PTI2 = Head2->getTerminator();
4338 Instruction *PBI2 = Head2->begin();
4339
4340 bool eq1 = (Block1 == Head1);
4341 bool eq2 = (Block2 == Head2);
4342 if (eq1 || eq2) {
4343 // An empty then-path or else-path.
4344 return (eq1 == eq2);
4345 }
4346
4347 // Check whether instructions in Block1 and Block2 are identical
4348 // and do not alias with instructions in Head2.
4349 BasicBlock::iterator iter1 = Block1->begin();
4350 BasicBlock::iterator end1 = Block1->getTerminator();
4351 BasicBlock::iterator iter2 = Block2->begin();
4352 BasicBlock::iterator end2 = Block2->getTerminator();
4353
4354 while (1) {
4355 if (iter1 == end1) {
4356 if (iter2 != end2)
4357 return false;
4358 break;
4359 }
4360
4361 if (!iter1->isIdenticalTo(iter2))
4362 return false;
4363
4364 // Illegal to remove instructions with side effects except
4365 // non-volatile stores.
4366 if (iter1->mayHaveSideEffects()) {
4367 Instruction *CurI = &*iter1;
4368 StoreInst *SI = dyn_cast(CurI);
4369 if (!SI || SI->isVolatile())
4370 return false;
4371 }
4372
4373 // For simplicity and speed, data dependency check can be
4374 // avoided if read from memory doesn't exist.
4375 if (iter1->mayReadFromMemory())
4376 return false;
4377
4378 if (iter1->mayWriteToMemory()) {
4379 for (BasicBlock::iterator BI = PBI2, BE = PTI2; BI != BE; ++BI) {
4380 if (BI->mayReadFromMemory() || BI->mayWriteToMemory()) {
4381 // Check alias with Head2.
4382 if (!AA || AA->alias(iter1, BI))
4383 return false;
4384 }
4385 }
4386 }
4387 ++iter1;
4388 ++iter2;
4389 }
4390
4391 return true;
4392 }
4393
4394 /// Check whether \param BB is the merge block of a if-region. If yes, check
4395 /// whether there exists an adjacent if-region upstream, the two if-regions
4396 /// contain identical instuctions and can be legally merged. \returns true if
4397 /// the two if-regions are merged.
4398 ///
4399 /// From:
4400 /// if (a)
4401 /// statement;
4402 /// if (b)
4403 /// statement;
4404 ///
4405 /// To:
4406 /// if (a || b)
4407 /// statement;
4408 ///
4409 bool SimplifyCFGOpt::MergeIfRegion(BasicBlock *BB, IRBuilder<> &Builder,
4410 Pass *P) {
4411 BasicBlock *IfTrue2, *IfFalse2;
4412 Value *IfCond2 = GetIfCondition(BB, IfTrue2, IfFalse2);
4413 Instruction *CInst2 = dyn_cast_or_null(IfCond2);
4414 if (!CInst2)
4415 return false;
4416
4417 BasicBlock *SecondEntryBlock = CInst2->getParent();
4418 if (SecondEntryBlock->hasAddressTaken())
4419 return false;
4420
4421 BasicBlock *IfTrue1, *IfFalse1;
4422 Value *IfCond1 = GetIfCondition(SecondEntryBlock, IfTrue1, IfFalse1);
4423 Instruction *CInst1 = dyn_cast_or_null(IfCond1);
4424 if (!CInst1)
4425 return false;
4426
4427 BasicBlock *FirstEntryBlock = CInst1->getParent();
4428
4429 // Either then-path or else-path should be empty.
4430 if ((IfTrue1 != FirstEntryBlock) && (IfFalse1 != FirstEntryBlock))
4431 return false;
4432 if ((IfTrue2 != SecondEntryBlock) && (IfFalse2 != SecondEntryBlock))
4433 return false;
4434
4435 TerminatorInst *PTI2 = SecondEntryBlock->getTerminator();
4436 Instruction *PBI2 = SecondEntryBlock->begin();
4437
4438 if (!CompareIfRegionBlock(FirstEntryBlock, SecondEntryBlock, IfTrue1, IfTrue2))
4439 return false;
4440
4441 if (!CompareIfRegionBlock(FirstEntryBlock, SecondEntryBlock, IfFalse1, IfFalse2))
4442 return false;
4443
4444 // Check whether \param SecondEntryBlock has side-effect and is safe to speculate.
4445 for (BasicBlock::iterator BI = PBI2, BE = PTI2; BI != BE; ++BI) {
4446 Instruction *CI = BI;
4447 if (isa(CI) || CI->mayHaveSideEffects() ||
4448 !isSafeToSpeculativelyExecute(CI))
4449 return false;
4450 }
4451
4452 // Merge \param SecondEntryBlock into \param FirstEntryBlock.
4453 FirstEntryBlock->getInstList().pop_back();
4454 FirstEntryBlock->getInstList().splice(FirstEntryBlock->end(), SecondEntryBlock->getInstList());
4455 BranchInst *PBI = dyn_cast(FirstEntryBlock->getTerminator());
4456 Value *CC = PBI->getCondition();
4457 BasicBlock *SaveInsertBB = Builder.GetInsertBlock();
4458 BasicBlock::iterator SaveInsertPt = Builder.GetInsertPoint();
4459 Builder.SetInsertPoint(PBI);
4460 Value *NC = Builder.CreateOr(CInst1, CC);
4461 PBI->replaceUsesOfWith(CC, NC);
4462 if (SaveInsertBB)
4463 Builder.SetInsertPoint(SaveInsertBB, SaveInsertPt);
4464
4465 // Remove IfTrue1
4466 if (IfTrue1 != FirstEntryBlock) {
4467 IfTrue1->dropAllReferences();
4468 IfTrue1->eraseFromParent();
4469 }
4470
4471 // Remove IfFalse1
4472 if (IfFalse1 != FirstEntryBlock) {
4473 IfFalse1->dropAllReferences();
4474 IfFalse1->eraseFromParent();
4475 }
4476
4477 // Remove \param SecondEntryBlock
4478 SecondEntryBlock->dropAllReferences();
4479 SecondEntryBlock->eraseFromParent();
4480 DEBUG(dbgs() << "If conditions merged into:\n" << *FirstEntryBlock);
4481 return true;
4482 }
4483
44843980 /// Check if passing a value to an instruction will cause undefined behavior.
44853981 static bool passingValueIsAlwaysUndefined(Value *V, Instruction *I) {
44863982 Constant *C = dyn_cast(V);
45834079 return true;
45844080
45854081 IRBuilder<> Builder(BB);
4586 // Whether to optimize conditional branches.
4587 bool OptCB = (ParallelAndOr && AA && TTI.hasBranchDivergence());
4588
4589 if (OptCB && SimplifyParallelAndOr(BB, Builder))
4590 return true;
45914082
45924083 // If there is a trivial two-entry PHI node in this basic block, and we can
45934084 // eliminate it, do so now.
46164107 if (SimplifyIndirectBr(IBI)) return true;
46174108 }
46184109
4619 if (OptCB && MergeIfRegion(BB, Builder))
4620 return true;
4621
46224110 return Changed;
46234111 }
46244112
46284116 /// of the CFG. It returns true if a modification was made.
46294117 ///
46304118 bool llvm::SimplifyCFG(BasicBlock *BB, const TargetTransformInfo &TTI,
4631 const DataLayout *TD, AliasAnalysis *AA) {
4632 return SimplifyCFGOpt(TTI, TD, AA).run(BB);
4633 }
4119 const DataLayout *TD) {
4120 return SimplifyCFGOpt(TTI, TD).run(BB);
4121 }
0 ; Function Attrs: nounwind
1 ; RUN: llc < %s -march=r600 -mcpu=redwood | FileCheck %s
2 ;
3 ; CFG flattening should use parallel-and mode to generate branch conditions and
4 ; then merge if-regions with the same bodies.
5 ;
6 ; CHECK: AND_INT
7 ; CHECK-NEXT: AND_INT
8 ; CHECK-NEXT: OR_INT
9 define void @_Z9chk1D_512v() #0 {
10 entry:
11 %a0 = alloca i32, align 4
12 %b0 = alloca i32, align 4
13 %c0 = alloca i32, align 4
14 %d0 = alloca i32, align 4
15 %a1 = alloca i32, align 4
16 %b1 = alloca i32, align 4
17 %c1 = alloca i32, align 4
18 %d1 = alloca i32, align 4
19 %data = alloca i32, align 4
20 %0 = load i32* %a0, align 4
21 %1 = load i32* %b0, align 4
22 %cmp = icmp ne i32 %0, %1
23 br i1 %cmp, label %land.lhs.true, label %if.end
24
25 land.lhs.true: ; preds = %entry
26 %2 = load i32* %c0, align 4
27 %3 = load i32* %d0, align 4
28 %cmp1 = icmp ne i32 %2, %3
29 br i1 %cmp1, label %if.then, label %if.end
30
31 if.then: ; preds = %land.lhs.true
32 store i32 1, i32* %data, align 4
33 br label %if.end
34
35 if.end: ; preds = %if.then, %land.lhs.true, %entry
36 %4 = load i32* %a1, align 4
37 %5 = load i32* %b1, align 4
38 %cmp2 = icmp ne i32 %4, %5
39 br i1 %cmp2, label %land.lhs.true3, label %if.end6
40
41 land.lhs.true3: ; preds = %if.end
42 %6 = load i32* %c1, align 4
43 %7 = load i32* %d1, align 4
44 %cmp4 = icmp ne i32 %6, %7
45 br i1 %cmp4, label %if.then5, label %if.end6
46
47 if.then5: ; preds = %land.lhs.true3
48 store i32 1, i32* %data, align 4
49 br label %if.end6
50
51 if.end6: ; preds = %if.then5, %land.lhs.true3, %if.end
52 ret void
53 }
0 ; Function Attrs: nounwind
1 ; RUN: llc < %s -march=r600 -mcpu=redwood | FileCheck %s
2 ;
3 ; CFG flattening should use parallel-or to generate branch conditions and
4 ; then merge if-regions with the same bodies.
5 ;
6 ; CHECK: OR_INT
7 ; CHECK-NEXT: OR_INT
8 ; CHECK-NEXT: OR_INT
9 define void @_Z9chk1D_512v() #0 {
10 entry:
11 %a0 = alloca i32, align 4
12 %b0 = alloca i32, align 4
13 %c0 = alloca i32, align 4
14 %d0 = alloca i32, align 4
15 %a1 = alloca i32, align 4
16 %b1 = alloca i32, align 4
17 %c1 = alloca i32, align 4
18 %d1 = alloca i32, align 4
19 %data = alloca i32, align 4
20 %0 = load i32* %a0, align 4
21 %1 = load i32* %b0, align 4
22 %cmp = icmp ne i32 %0, %1
23 br i1 %cmp, label %land.lhs.true, label %if.else
24
25 land.lhs.true: ; preds = %entry
26 %2 = load i32* %c0, align 4
27 %3 = load i32* %d0, align 4
28 %cmp1 = icmp ne i32 %2, %3
29 br i1 %cmp1, label %if.then, label %if.else
30
31 if.then: ; preds = %land.lhs.true
32 br label %if.end
33
34 if.else: ; preds = %land.lhs.true, %entry
35 store i32 1, i32* %data, align 4
36 br label %if.end
37
38 if.end: ; preds = %if.else, %if.then
39 %4 = load i32* %a1, align 4
40 %5 = load i32* %b1, align 4
41 %cmp2 = icmp ne i32 %4, %5
42 br i1 %cmp2, label %land.lhs.true3, label %if.else6
43
44 land.lhs.true3: ; preds = %if.end
45 %6 = load i32* %c1, align 4
46 %7 = load i32* %d1, align 4
47 %cmp4 = icmp ne i32 %6, %7
48 br i1 %cmp4, label %if.then5, label %if.else6
49
50 if.then5: ; preds = %land.lhs.true3
51 br label %if.end7
52
53 if.else6: ; preds = %land.lhs.true3, %if.end
54 store i32 1, i32* %data, align 4
55 br label %if.end7
56
57 if.end7: ; preds = %if.else6, %if.then5
58 ret void
59 }
60
None config.suffixes = ['.ll', '.c', '.cpp']
1
2 targets = set(config.root.targets_to_build.split())
3 if not 'R600' in targets:
4 config.unsupported = True
5
None ; Function Attrs: nounwind
1 ; RUN: opt < %s -mtriple=r600-unknown-linux-gnu -optimizecfg -basicaa -S | FileCheck %s
2 ;
3 ; CFG optimization should use parallel-and mode to generate branch conditions and
4 ; then merge if-regions with the same bodies, which should result in 2 branches.
5 ; To see the assembly output without this transformation, remove -basicaa option.
6 ;
7 ; CHECK: or i1
8 ; CHECK-NEXT: br
9 ; CHECK: br
10 ; CHECK: ret
11 define void @_Z9chk1D_512v() #0 {
12 entry:
13 %a0 = alloca i32, align 4
14 %b0 = alloca i32, align 4
15 %c0 = alloca i32, align 4
16 %d0 = alloca i32, align 4
17 %a1 = alloca i32, align 4
18 %b1 = alloca i32, align 4
19 %c1 = alloca i32, align 4
20 %d1 = alloca i32, align 4
21 %data = alloca i32, align 4
22 %0 = load i32* %a0, align 4
23 %1 = load i32* %b0, align 4
24 %cmp = icmp ne i32 %0, %1
25 br i1 %cmp, label %land.lhs.true, label %if.else
26
27 land.lhs.true: ; preds = %entry
28 %2 = load i32* %c0, align 4
29 %3 = load i32* %d0, align 4
30 %cmp1 = icmp ne i32 %2, %3
31 br i1 %cmp1, label %if.then, label %if.else
32
33 if.then: ; preds = %land.lhs.true
34 br label %if.end
35
36 if.else: ; preds = %land.lhs.true, %entry
37 store i32 1, i32* %data, align 4
38 br label %if.end
39
40 if.end: ; preds = %if.else, %if.then
41 %4 = load i32* %a1, align 4
42 %5 = load i32* %b1, align 4
43 %cmp2 = icmp ne i32 %4, %5
44 br i1 %cmp2, label %land.lhs.true3, label %if.else6
45
46 land.lhs.true3: ; preds = %if.end
47 %6 = load i32* %c1, align 4
48 %7 = load i32* %d1, align 4
49 %cmp4 = icmp ne i32 %6, %7
50 br i1 %cmp4, label %if.then5, label %if.else6
51
52 if.then5: ; preds = %land.lhs.true3
53 br label %if.end7
54
55 if.else6: ; preds = %land.lhs.true3, %if.end
56 store i32 1, i32* %data, align 4
57 br label %if.end7
58
59 if.end7: ; preds = %if.else6, %if.then5
60 ret void
61 }
62
None ; Function Attrs: nounwind
1 ; RUN: opt < %s -mtriple=r600-unknown-linux-gnu -optimizecfg -basicaa -S | FileCheck %s
2 ;
3 ; CFG optimization should use parallel-or mode to generate branch conditions and
4 ; then merge if-regions with the same bodies, which should result in 2 branches.
5 ; To see the assembly output without this transformation, remove -basicaa option.
6 ;
7 ; CHECK: or i1
8 ; CHECK-NEXT: br
9 ; CHECK: br
10 ; CHECK: ret
11 define void @_Z9chk1D_512v() #0 {
12 entry:
13 %a0 = alloca i32, align 4
14 %b0 = alloca i32, align 4
15 %c0 = alloca i32, align 4
16 %d0 = alloca i32, align 4
17 %a1 = alloca i32, align 4
18 %b1 = alloca i32, align 4
19 %c1 = alloca i32, align 4
20 %d1 = alloca i32, align 4
21 %data = alloca i32, align 4
22 %0 = load i32* %a0, align 4
23 %1 = load i32* %b0, align 4
24 %cmp = icmp ne i32 %0, %1
25 br i1 %cmp, label %land.lhs.true, label %if.end
26
27 land.lhs.true: ; preds = %entry
28 %2 = load i32* %c0, align 4
29 %3 = load i32* %d0, align 4
30 %cmp1 = icmp ne i32 %2, %3
31 br i1 %cmp1, label %if.then, label %if.end
32
33 if.then: ; preds = %land.lhs.true
34 store i32 1, i32* %data, align 4
35 br label %if.end
36
37 if.end: ; preds = %if.then, %land.lhs.true, %entry
38 %4 = load i32* %a1, align 4
39 %5 = load i32* %b1, align 4
40 %cmp2 = icmp ne i32 %4, %5
41 br i1 %cmp2, label %land.lhs.true3, label %if.end6
42
43 land.lhs.true3: ; preds = %if.end
44 %6 = load i32* %c1, align 4
45 %7 = load i32* %d1, align 4
46 %cmp4 = icmp ne i32 %6, %7
47 br i1 %cmp4, label %if.then5, label %if.end6
48
49 if.then5: ; preds = %land.lhs.true3
50 store i32 1, i32* %data, align 4
51 br label %if.end6
52
53 if.end6: ; preds = %if.then5, %land.lhs.true3, %if.end
54 ret void
55 }
None config.suffixes = ['.ll', '.c', '.cpp']
117117 initializeGVNPass(R);
118118 initializeMemCpyOptPass(R);
119119 initializeDCEPass(R);
120 initializeCFGCanonicalizePass(R);
120 initializeCFGSimplifyPassPass(R);
121121 }
122122
123123 bool LTOCodeGenerator::addModule(LTOModule* mod, std::string& errMsg) {