1 //===- FlatternCFG.cpp - Code to perform CFG flattening ---------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // Reduce conditional branches in CFG.
12 //===----------------------------------------------------------------------===//
14 #define DEBUG_TYPE "flattencfg"
15 #include "llvm/Transforms/Utils/Local.h"
16 #include "llvm/ADT/SmallPtrSet.h"
17 #include "llvm/Analysis/AliasAnalysis.h"
18 #include "llvm/Analysis/ValueTracking.h"
19 #include "llvm/IR/IRBuilder.h"
20 #include "llvm/Support/Debug.h"
21 #include "llvm/Transforms/Utils/BasicBlockUtils.h"
27 /// \brief Use parallel-and or parallel-or to generate conditions for
28 /// conditional branches.
29 bool FlattenParallelAndOr(BasicBlock *BB, IRBuilder<> &Builder, Pass *P = 0);
30 /// \brief If \param BB is the merge block of an if-region, attempt to merge
31 /// the if-region with an adjacent if-region upstream if two if-regions
32 /// contain identical instructions.
33 bool MergeIfRegion(BasicBlock *BB, IRBuilder<> &Builder, Pass *P = 0);
34 /// \brief Compare a pair of blocks: \p Block1 and \p Block2, which
35 /// are from two if-regions whose entry blocks are \p Head1 and \p
36 /// Head2. \returns true if \p Block1 and \p Block2 contain identical
37 /// instructions, and have no memory reference alias with \p Head2.
38 /// This is used as a legality check for merging if-regions.
39 bool CompareIfRegionBlock(BasicBlock *Head1, BasicBlock *Head2,
40 BasicBlock *Block1, BasicBlock *Block2);
43 FlattenCFGOpt(AliasAnalysis *AA) : AA(AA) {}
44 bool run(BasicBlock *BB);
48 /// If \param [in] BB has more than one predecessor that is a conditional
49 /// branch, attempt to use parallel and/or for the branch condition. \returns
54 /// %cmp10 = fcmp une float %tmp1, %tmp2
55 /// br i1 %cmp1, label %if.then, label %lor.rhs
59 /// %cmp11 = fcmp une float %tmp3, %tmp4
60 /// br i1 %cmp11, label %if.then, label %ifend
62 /// if.end: // the merge block
65 /// if.then: // has two predecessors, both of them contains conditional branch.
71 /// %cmp10 = fcmp une float %tmp1, %tmp2
73 /// %cmp11 = fcmp une float %tmp3, %tmp4
74 /// %cmp12 = or i1 %cmp10, %cmp11 // parallel-or mode.
75 /// br i1 %cmp12, label %if.then, label %ifend
84 /// Current implementation handles two cases.
85 /// Case 1: \param BB is on the else-path.
91 /// BB3 \ | where, BB1, BB2 contain conditional branches.
92 /// \ | / BB3 contains unconditional branch.
93 /// \ | / BB4 corresponds to \param BB which is also the merge.
97 /// Corresponding source code:
99 /// if (a == b && c == d)
100 /// statement; // BB3
102 /// Case 2: \param BB BB is on the then-path.
107 /// \ / | where BB1, BB2 contain conditional branches.
108 /// BB => BB3 | BB3 contains unconditiona branch and corresponds
109 /// \ / to \param BB. BB4 is the merge.
112 /// Corresponding source code:
114 /// if (a == b || c == d)
115 /// statement; // BB3
117 /// In both cases, \param BB is the common successor of conditional branches.
118 /// In Case 1, \param BB (BB4) has an unconditional branch (BB3) as
119 /// its predecessor. In Case 2, \param BB (BB3) only has conditional branches
120 /// as its predecessors.
122 bool FlattenCFGOpt::FlattenParallelAndOr(BasicBlock *BB, IRBuilder<> &Builder,
124 PHINode *PHI = dyn_cast<PHINode>(BB->begin());
126 return false; // For simplicity, avoid cases containing PHI nodes.
128 BasicBlock *LastCondBlock = NULL;
129 BasicBlock *FirstCondBlock = NULL;
130 BasicBlock *UnCondBlock = NULL;
133 // Check predecessors of \param BB.
134 SmallPtrSet<BasicBlock *, 16> Preds(pred_begin(BB), pred_end(BB));
135 for (SmallPtrSetIterator<BasicBlock *> PI = Preds.begin(), PE = Preds.end();
137 BasicBlock *Pred = *PI;
138 BranchInst *PBI = dyn_cast<BranchInst>(Pred->getTerminator());
140 // All predecessors should terminate with a branch.
144 BasicBlock *PP = Pred->getSinglePredecessor();
146 if (PBI->isUnconditional()) {
147 // Case 1: Pred (BB3) is an unconditional block, it should
148 // have a single predecessor (BB2) that is also a predecessor
149 // of \param BB (BB4) and should not have address-taken.
150 // There should exist only one such unconditional
151 // branch among the predecessors.
152 if (UnCondBlock || !PP || (Preds.count(PP) == 0) ||
153 Pred->hasAddressTaken())
160 // Only conditional branches are allowed beyond this point.
161 assert(PBI->isConditional());
163 // Condition's unique use should be the branch instruction.
164 Value *PC = PBI->getCondition();
165 if (!PC || !PC->hasOneUse())
168 if (PP && Preds.count(PP)) {
169 // These are internal condition blocks to be merged from, e.g.,
170 // BB2 in both cases.
171 // Should not be address-taken.
172 if (Pred->hasAddressTaken())
175 // Instructions in the internal condition blocks should be safe
177 for (BasicBlock::iterator BI = Pred->begin(), BE = PBI; BI != BE;) {
178 Instruction *CI = BI++;
179 if (isa<PHINode>(CI) || !isSafeToSpeculativelyExecute(CI))
183 // This is the condition block to be merged into, e.g. BB1 in
187 FirstCondBlock = Pred;
190 // Find whether BB is uniformly on the true (or false) path
191 // for all of its predecessors.
192 BasicBlock *PS1 = PBI->getSuccessor(0);
193 BasicBlock *PS2 = PBI->getSuccessor(1);
194 BasicBlock *PS = (PS1 == BB) ? PS2 : PS1;
195 int CIdx = (PS1 == BB) ? 0 : 1;
199 else if (CIdx != Idx)
202 // PS is the successor which is not BB. Check successors to identify
203 // the last conditional branch.
204 if (Preds.count(PS) == 0) {
206 LastCondBlock = Pred;
209 BranchInst *BPS = dyn_cast<BranchInst>(PS->getTerminator());
210 if (BPS && BPS->isUnconditional()) {
211 // Case 1: PS(BB3) should be an unconditional branch.
212 LastCondBlock = Pred;
217 if (!FirstCondBlock || !LastCondBlock || (FirstCondBlock == LastCondBlock))
220 TerminatorInst *TBB = LastCondBlock->getTerminator();
221 BasicBlock *PS1 = TBB->getSuccessor(0);
222 BasicBlock *PS2 = TBB->getSuccessor(1);
223 BranchInst *PBI1 = dyn_cast<BranchInst>(PS1->getTerminator());
224 BranchInst *PBI2 = dyn_cast<BranchInst>(PS2->getTerminator());
226 // If PS1 does not jump into PS2, but PS2 jumps into PS1,
227 // attempt branch inversion.
228 if (!PBI1 || !PBI1->isUnconditional() ||
229 (PS1->getTerminator()->getSuccessor(0) != PS2)) {
230 // Check whether PS2 jumps into PS1.
231 if (!PBI2 || !PBI2->isUnconditional() ||
232 (PS2->getTerminator()->getSuccessor(0) != PS1))
235 // Do branch inversion.
236 BasicBlock *CurrBlock = LastCondBlock;
237 bool EverChanged = false;
239 BranchInst *BI = dyn_cast<BranchInst>(CurrBlock->getTerminator());
240 CmpInst *CI = dyn_cast<CmpInst>(BI->getCondition());
241 CmpInst::Predicate Predicate = CI->getPredicate();
242 // Cannonicalize icmp_ne -> icmp_eq, fcmp_one -> fcmp_oeq
243 if ((Predicate == CmpInst::ICMP_NE) || (Predicate == CmpInst::FCMP_ONE)) {
244 CI->setPredicate(ICmpInst::getInversePredicate(Predicate));
245 BI->swapSuccessors();
248 if (CurrBlock == FirstCondBlock)
250 CurrBlock = CurrBlock->getSinglePredecessor();
255 // PS1 must have a conditional branch.
256 if (!PBI1 || !PBI1->isUnconditional())
259 // PS2 should not contain PHI node.
260 PHI = dyn_cast<PHINode>(PS2->begin());
264 // Do the transformation.
266 BranchInst *PBI = dyn_cast<BranchInst>(FirstCondBlock->getTerminator());
267 bool Iteration = true;
268 BasicBlock *SaveInsertBB = Builder.GetInsertBlock();
269 BasicBlock::iterator SaveInsertPt = Builder.GetInsertPoint();
270 Value *PC = PBI->getCondition();
273 CB = PBI->getSuccessor(1 - Idx);
274 // Delete the conditional branch.
275 FirstCondBlock->getInstList().pop_back();
276 FirstCondBlock->getInstList()
277 .splice(FirstCondBlock->end(), CB->getInstList());
278 PBI = cast<BranchInst>(FirstCondBlock->getTerminator());
279 Value *CC = PBI->getCondition();
281 Builder.SetInsertPoint(PBI);
284 // Case 2, use parallel or.
285 NC = Builder.CreateOr(PC, CC);
287 // Case 1, use parallel and.
288 NC = Builder.CreateAnd(PC, CC);
290 PBI->replaceUsesOfWith(CC, NC);
292 if (CB == LastCondBlock)
294 // Remove internal conditional branches.
295 CB->dropAllReferences();
296 // make CB unreachable and let downstream to delete the block.
297 new UnreachableInst(CB->getContext(), CB);
300 Builder.SetInsertPoint(SaveInsertBB, SaveInsertPt);
301 DEBUG(dbgs() << "Use parallel and/or in:\n" << *FirstCondBlock);
305 /// Compare blocks from two if-regions, where \param Head1 is the entry of the
306 /// 1st if-region. \param Head2 is the entry of the 2nd if-region. \param
307 /// Block1 is a block in the 1st if-region to compare. \param Block2 is a block
308 // in the 2nd if-region to compare. \returns true if \param Block1 and \param
309 /// Block2 have identical instructions and do not have memory reference alias
310 /// with \param Head2.
312 bool FlattenCFGOpt::CompareIfRegionBlock(BasicBlock *Head1, BasicBlock *Head2,
314 BasicBlock *Block2) {
315 TerminatorInst *PTI2 = Head2->getTerminator();
316 Instruction *PBI2 = Head2->begin();
318 bool eq1 = (Block1 == Head1);
319 bool eq2 = (Block2 == Head2);
321 // An empty then-path or else-path.
325 // Check whether instructions in Block1 and Block2 are identical
326 // and do not alias with instructions in Head2.
327 BasicBlock::iterator iter1 = Block1->begin();
328 BasicBlock::iterator end1 = Block1->getTerminator();
329 BasicBlock::iterator iter2 = Block2->begin();
330 BasicBlock::iterator end2 = Block2->getTerminator();
339 if (!iter1->isIdenticalTo(iter2))
342 // Illegal to remove instructions with side effects except
343 // non-volatile stores.
344 if (iter1->mayHaveSideEffects()) {
345 Instruction *CurI = &*iter1;
346 StoreInst *SI = dyn_cast<StoreInst>(CurI);
347 if (!SI || SI->isVolatile())
351 // For simplicity and speed, data dependency check can be
352 // avoided if read from memory doesn't exist.
353 if (iter1->mayReadFromMemory())
356 if (iter1->mayWriteToMemory()) {
357 for (BasicBlock::iterator BI = PBI2, BE = PTI2; BI != BE; ++BI) {
358 if (BI->mayReadFromMemory() || BI->mayWriteToMemory()) {
359 // Check alias with Head2.
360 if (!AA || AA->alias(iter1, BI))
372 /// Check whether \param BB is the merge block of a if-region. If yes, check
373 /// whether there exists an adjacent if-region upstream, the two if-regions
374 /// contain identical instuctions and can be legally merged. \returns true if
375 /// the two if-regions are merged.
387 bool FlattenCFGOpt::MergeIfRegion(BasicBlock *BB, IRBuilder<> &Builder,
389 BasicBlock *IfTrue2, *IfFalse2;
390 Value *IfCond2 = GetIfCondition(BB, IfTrue2, IfFalse2);
391 Instruction *CInst2 = dyn_cast_or_null<Instruction>(IfCond2);
395 BasicBlock *SecondEntryBlock = CInst2->getParent();
396 if (SecondEntryBlock->hasAddressTaken())
399 BasicBlock *IfTrue1, *IfFalse1;
400 Value *IfCond1 = GetIfCondition(SecondEntryBlock, IfTrue1, IfFalse1);
401 Instruction *CInst1 = dyn_cast_or_null<Instruction>(IfCond1);
405 BasicBlock *FirstEntryBlock = CInst1->getParent();
407 // Either then-path or else-path should be empty.
408 if ((IfTrue1 != FirstEntryBlock) && (IfFalse1 != FirstEntryBlock))
410 if ((IfTrue2 != SecondEntryBlock) && (IfFalse2 != SecondEntryBlock))
413 TerminatorInst *PTI2 = SecondEntryBlock->getTerminator();
414 Instruction *PBI2 = SecondEntryBlock->begin();
416 if (!CompareIfRegionBlock(FirstEntryBlock, SecondEntryBlock, IfTrue1,
420 if (!CompareIfRegionBlock(FirstEntryBlock, SecondEntryBlock, IfFalse1,
424 // Check whether \param SecondEntryBlock has side-effect and is safe to
426 for (BasicBlock::iterator BI = PBI2, BE = PTI2; BI != BE; ++BI) {
427 Instruction *CI = BI;
428 if (isa<PHINode>(CI) || CI->mayHaveSideEffects() ||
429 !isSafeToSpeculativelyExecute(CI))
433 // Merge \param SecondEntryBlock into \param FirstEntryBlock.
434 FirstEntryBlock->getInstList().pop_back();
435 FirstEntryBlock->getInstList()
436 .splice(FirstEntryBlock->end(), SecondEntryBlock->getInstList());
437 BranchInst *PBI = dyn_cast<BranchInst>(FirstEntryBlock->getTerminator());
438 Value *CC = PBI->getCondition();
439 BasicBlock *SaveInsertBB = Builder.GetInsertBlock();
440 BasicBlock::iterator SaveInsertPt = Builder.GetInsertPoint();
441 Builder.SetInsertPoint(PBI);
442 Value *NC = Builder.CreateOr(CInst1, CC);
443 PBI->replaceUsesOfWith(CC, NC);
444 Builder.SetInsertPoint(SaveInsertBB, SaveInsertPt);
447 if (IfTrue1 != FirstEntryBlock) {
448 IfTrue1->dropAllReferences();
449 IfTrue1->eraseFromParent();
453 if (IfFalse1 != FirstEntryBlock) {
454 IfFalse1->dropAllReferences();
455 IfFalse1->eraseFromParent();
458 // Remove \param SecondEntryBlock
459 SecondEntryBlock->dropAllReferences();
460 SecondEntryBlock->eraseFromParent();
461 DEBUG(dbgs() << "If conditions merged into:\n" << *FirstEntryBlock);
465 bool FlattenCFGOpt::run(BasicBlock *BB) {
466 bool Changed = false;
467 assert(BB && BB->getParent() && "Block not embedded in function!");
468 assert(BB->getTerminator() && "Degenerate basic block encountered!");
470 IRBuilder<> Builder(BB);
472 if (FlattenParallelAndOr(BB, Builder))
475 if (MergeIfRegion(BB, Builder))
481 /// FlattenCFG - This function is used to flatten a CFG. For
482 /// example, it uses parallel-and and parallel-or mode to collapse
483 // if-conditions and merge if-regions with identical statements.
485 bool llvm::FlattenCFG(BasicBlock *BB, AliasAnalysis *AA) {
486 return FlattenCFGOpt(AA).run(BB);