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 #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/Support/raw_ostream.h"
21 #include "llvm/Transforms/Utils/BasicBlockUtils.h"
24 #define DEBUG_TYPE "flattencfg"
29 /// \brief Use parallel-and or parallel-or to generate conditions for
30 /// conditional branches.
31 bool FlattenParallelAndOr(BasicBlock *BB, IRBuilder<> &Builder);
32 /// \brief If \param BB is the merge block of an if-region, attempt to merge
33 /// the if-region with an adjacent if-region upstream if two if-regions
34 /// contain identical instructions.
35 bool MergeIfRegion(BasicBlock *BB, IRBuilder<> &Builder);
36 /// \brief Compare a pair of blocks: \p Block1 and \p Block2, which
37 /// are from two if-regions whose entry blocks are \p Head1 and \p
38 /// Head2. \returns true if \p Block1 and \p Block2 contain identical
39 /// instructions, and have no memory reference alias with \p Head2.
40 /// This is used as a legality check for merging if-regions.
41 bool CompareIfRegionBlock(BasicBlock *Head1, BasicBlock *Head2,
42 BasicBlock *Block1, BasicBlock *Block2);
45 FlattenCFGOpt(AliasAnalysis *AA) : AA(AA) {}
46 bool run(BasicBlock *BB);
50 /// If \param [in] BB has more than one predecessor that is a conditional
51 /// branch, attempt to use parallel and/or for the branch condition. \returns
56 /// %cmp10 = fcmp une float %tmp1, %tmp2
57 /// br i1 %cmp1, label %if.then, label %lor.rhs
61 /// %cmp11 = fcmp une float %tmp3, %tmp4
62 /// br i1 %cmp11, label %if.then, label %ifend
64 /// if.end: // the merge block
67 /// if.then: // has two predecessors, both of them contains conditional branch.
73 /// %cmp10 = fcmp une float %tmp1, %tmp2
75 /// %cmp11 = fcmp une float %tmp3, %tmp4
76 /// %cmp12 = or i1 %cmp10, %cmp11 // parallel-or mode.
77 /// br i1 %cmp12, label %if.then, label %ifend
86 /// Current implementation handles two cases.
87 /// Case 1: \param BB is on the else-path.
93 /// BB3 \ | where, BB1, BB2 contain conditional branches.
94 /// \ | / BB3 contains unconditional branch.
95 /// \ | / BB4 corresponds to \param BB which is also the merge.
99 /// Corresponding source code:
101 /// if (a == b && c == d)
102 /// statement; // BB3
104 /// Case 2: \param BB BB is on the then-path.
109 /// \ / | where BB1, BB2 contain conditional branches.
110 /// BB => BB3 | BB3 contains unconditiona branch and corresponds
111 /// \ / to \param BB. BB4 is the merge.
114 /// Corresponding source code:
116 /// if (a == b || c == d)
117 /// statement; // BB3
119 /// In both cases, \param BB is the common successor of conditional branches.
120 /// In Case 1, \param BB (BB4) has an unconditional branch (BB3) as
121 /// its predecessor. In Case 2, \param BB (BB3) only has conditional branches
122 /// as its predecessors.
124 bool FlattenCFGOpt::FlattenParallelAndOr(BasicBlock *BB, IRBuilder<> &Builder) {
125 PHINode *PHI = dyn_cast<PHINode>(BB->begin());
127 return false; // For simplicity, avoid cases containing PHI nodes.
129 BasicBlock *LastCondBlock = nullptr;
130 BasicBlock *FirstCondBlock = nullptr;
131 BasicBlock *UnCondBlock = nullptr;
134 // Check predecessors of \param BB.
135 SmallPtrSet<BasicBlock *, 16> Preds(pred_begin(BB), pred_end(BB));
136 for (SmallPtrSetIterator<BasicBlock *> PI = Preds.begin(), PE = Preds.end();
138 BasicBlock *Pred = *PI;
139 BranchInst *PBI = dyn_cast<BranchInst>(Pred->getTerminator());
141 // All predecessors should terminate with a branch.
145 BasicBlock *PP = Pred->getSinglePredecessor();
147 if (PBI->isUnconditional()) {
148 // Case 1: Pred (BB3) is an unconditional block, it should
149 // have a single predecessor (BB2) that is also a predecessor
150 // of \param BB (BB4) and should not have address-taken.
151 // There should exist only one such unconditional
152 // branch among the predecessors.
153 if (UnCondBlock || !PP || (Preds.count(PP) == 0) ||
154 Pred->hasAddressTaken())
161 // Only conditional branches are allowed beyond this point.
162 assert(PBI->isConditional());
164 // Condition's unique use should be the branch instruction.
165 Value *PC = PBI->getCondition();
166 if (!PC || !PC->hasOneUse())
169 if (PP && Preds.count(PP)) {
170 // These are internal condition blocks to be merged from, e.g.,
171 // BB2 in both cases.
172 // Should not be address-taken.
173 if (Pred->hasAddressTaken())
176 // Instructions in the internal condition blocks should be safe
178 for (BasicBlock::iterator BI = Pred->begin(), BE = PBI->getIterator();
180 Instruction *CI = &*BI++;
181 if (isa<PHINode>(CI) || !isSafeToSpeculativelyExecute(CI))
185 // This is the condition block to be merged into, e.g. BB1 in
189 FirstCondBlock = Pred;
192 // Find whether BB is uniformly on the true (or false) path
193 // for all of its predecessors.
194 BasicBlock *PS1 = PBI->getSuccessor(0);
195 BasicBlock *PS2 = PBI->getSuccessor(1);
196 BasicBlock *PS = (PS1 == BB) ? PS2 : PS1;
197 int CIdx = (PS1 == BB) ? 0 : 1;
201 else if (CIdx != Idx)
204 // PS is the successor which is not BB. Check successors to identify
205 // the last conditional branch.
206 if (Preds.count(PS) == 0) {
208 LastCondBlock = Pred;
211 BranchInst *BPS = dyn_cast<BranchInst>(PS->getTerminator());
212 if (BPS && BPS->isUnconditional()) {
213 // Case 1: PS(BB3) should be an unconditional branch.
214 LastCondBlock = Pred;
219 if (!FirstCondBlock || !LastCondBlock || (FirstCondBlock == LastCondBlock))
222 TerminatorInst *TBB = LastCondBlock->getTerminator();
223 BasicBlock *PS1 = TBB->getSuccessor(0);
224 BasicBlock *PS2 = TBB->getSuccessor(1);
225 BranchInst *PBI1 = dyn_cast<BranchInst>(PS1->getTerminator());
226 BranchInst *PBI2 = dyn_cast<BranchInst>(PS2->getTerminator());
228 // If PS1 does not jump into PS2, but PS2 jumps into PS1,
229 // attempt branch inversion.
230 if (!PBI1 || !PBI1->isUnconditional() ||
231 (PS1->getTerminator()->getSuccessor(0) != PS2)) {
232 // Check whether PS2 jumps into PS1.
233 if (!PBI2 || !PBI2->isUnconditional() ||
234 (PS2->getTerminator()->getSuccessor(0) != PS1))
237 // Do branch inversion.
238 BasicBlock *CurrBlock = LastCondBlock;
239 bool EverChanged = false;
240 for (;CurrBlock != FirstCondBlock;
241 CurrBlock = CurrBlock->getSinglePredecessor()) {
242 BranchInst *BI = dyn_cast<BranchInst>(CurrBlock->getTerminator());
243 CmpInst *CI = dyn_cast<CmpInst>(BI->getCondition());
247 CmpInst::Predicate Predicate = CI->getPredicate();
248 // Canonicalize icmp_ne -> icmp_eq, fcmp_one -> fcmp_oeq
249 if ((Predicate == CmpInst::ICMP_NE) || (Predicate == CmpInst::FCMP_ONE)) {
250 CI->setPredicate(ICmpInst::getInversePredicate(Predicate));
251 BI->swapSuccessors();
258 // PS1 must have a conditional branch.
259 if (!PBI1 || !PBI1->isUnconditional())
262 // PS2 should not contain PHI node.
263 PHI = dyn_cast<PHINode>(PS2->begin());
267 // Do the transformation.
269 BranchInst *PBI = dyn_cast<BranchInst>(FirstCondBlock->getTerminator());
270 bool Iteration = true;
271 IRBuilder<>::InsertPointGuard Guard(Builder);
272 Value *PC = PBI->getCondition();
275 CB = PBI->getSuccessor(1 - Idx);
276 // Delete the conditional branch.
277 FirstCondBlock->getInstList().pop_back();
278 FirstCondBlock->getInstList()
279 .splice(FirstCondBlock->end(), CB->getInstList());
280 PBI = cast<BranchInst>(FirstCondBlock->getTerminator());
281 Value *CC = PBI->getCondition();
283 Builder.SetInsertPoint(PBI);
286 // Case 2, use parallel or.
287 NC = Builder.CreateOr(PC, CC);
289 // Case 1, use parallel and.
290 NC = Builder.CreateAnd(PC, CC);
292 PBI->replaceUsesOfWith(CC, NC);
294 if (CB == LastCondBlock)
296 // Remove internal conditional branches.
297 CB->dropAllReferences();
298 // make CB unreachable and let downstream to delete the block.
299 new UnreachableInst(CB->getContext(), CB);
302 DEBUG(dbgs() << "Use parallel and/or in:\n" << *FirstCondBlock);
306 /// Compare blocks from two if-regions, where \param Head1 is the entry of the
307 /// 1st if-region. \param Head2 is the entry of the 2nd if-region. \param
308 /// Block1 is a block in the 1st if-region to compare. \param Block2 is a block
309 // in the 2nd if-region to compare. \returns true if \param Block1 and \param
310 /// Block2 have identical instructions and do not have memory reference alias
311 /// with \param Head2.
313 bool FlattenCFGOpt::CompareIfRegionBlock(BasicBlock *Head1, BasicBlock *Head2,
315 BasicBlock *Block2) {
316 TerminatorInst *PTI2 = Head2->getTerminator();
317 Instruction *PBI2 = &Head2->front();
319 bool eq1 = (Block1 == Head1);
320 bool eq2 = (Block2 == Head2);
322 // An empty then-path or else-path.
326 // Check whether instructions in Block1 and Block2 are identical
327 // and do not alias with instructions in Head2.
328 BasicBlock::iterator iter1 = Block1->begin();
329 BasicBlock::iterator end1 = Block1->getTerminator()->getIterator();
330 BasicBlock::iterator iter2 = Block2->begin();
331 BasicBlock::iterator end2 = Block2->getTerminator()->getIterator();
340 if (!iter1->isIdenticalTo(&*iter2))
343 // Illegal to remove instructions with side effects except
344 // non-volatile stores.
345 if (iter1->mayHaveSideEffects()) {
346 Instruction *CurI = &*iter1;
347 StoreInst *SI = dyn_cast<StoreInst>(CurI);
348 if (!SI || SI->isVolatile())
352 // For simplicity and speed, data dependency check can be
353 // avoided if read from memory doesn't exist.
354 if (iter1->mayReadFromMemory())
357 if (iter1->mayWriteToMemory()) {
358 for (BasicBlock::iterator BI(PBI2), BE(PTI2); BI != BE; ++BI) {
359 if (BI->mayReadFromMemory() || BI->mayWriteToMemory()) {
360 // Check alias with Head2.
361 if (!AA || AA->alias(&*iter1, &*BI))
373 /// Check whether \param BB is the merge block of a if-region. If yes, check
374 /// whether there exists an adjacent if-region upstream, the two if-regions
375 /// contain identical instructions and can be legally merged. \returns true if
376 /// the two if-regions are merged.
388 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->front();
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);