LLVM API Documentation
00001 //===- FlatternCFG.cpp - Code to perform CFG flattening ---------------===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // Reduce conditional branches in CFG. 00011 // 00012 //===----------------------------------------------------------------------===// 00013 00014 #include "llvm/Transforms/Utils/Local.h" 00015 #include "llvm/ADT/SmallPtrSet.h" 00016 #include "llvm/Analysis/AliasAnalysis.h" 00017 #include "llvm/Analysis/ValueTracking.h" 00018 #include "llvm/IR/IRBuilder.h" 00019 #include "llvm/Support/Debug.h" 00020 #include "llvm/Support/raw_ostream.h" 00021 #include "llvm/Transforms/Utils/BasicBlockUtils.h" 00022 using namespace llvm; 00023 00024 #define DEBUG_TYPE "flattencfg" 00025 00026 namespace { 00027 class FlattenCFGOpt { 00028 AliasAnalysis *AA; 00029 /// \brief Use parallel-and or parallel-or to generate conditions for 00030 /// conditional branches. 00031 bool FlattenParallelAndOr(BasicBlock *BB, IRBuilder<> &Builder, 00032 Pass *P = nullptr); 00033 /// \brief If \param BB is the merge block of an if-region, attempt to merge 00034 /// the if-region with an adjacent if-region upstream if two if-regions 00035 /// contain identical instructions. 00036 bool MergeIfRegion(BasicBlock *BB, IRBuilder<> &Builder, Pass *P = nullptr); 00037 /// \brief Compare a pair of blocks: \p Block1 and \p Block2, which 00038 /// are from two if-regions whose entry blocks are \p Head1 and \p 00039 /// Head2. \returns true if \p Block1 and \p Block2 contain identical 00040 /// instructions, and have no memory reference alias with \p Head2. 00041 /// This is used as a legality check for merging if-regions. 00042 bool CompareIfRegionBlock(BasicBlock *Head1, BasicBlock *Head2, 00043 BasicBlock *Block1, BasicBlock *Block2); 00044 00045 public: 00046 FlattenCFGOpt(AliasAnalysis *AA) : AA(AA) {} 00047 bool run(BasicBlock *BB); 00048 }; 00049 } 00050 00051 /// If \param [in] BB has more than one predecessor that is a conditional 00052 /// branch, attempt to use parallel and/or for the branch condition. \returns 00053 /// true on success. 00054 /// 00055 /// Before: 00056 /// ...... 00057 /// %cmp10 = fcmp une float %tmp1, %tmp2 00058 /// br i1 %cmp1, label %if.then, label %lor.rhs 00059 /// 00060 /// lor.rhs: 00061 /// ...... 00062 /// %cmp11 = fcmp une float %tmp3, %tmp4 00063 /// br i1 %cmp11, label %if.then, label %ifend 00064 /// 00065 /// if.end: // the merge block 00066 /// ...... 00067 /// 00068 /// if.then: // has two predecessors, both of them contains conditional branch. 00069 /// ...... 00070 /// br label %if.end; 00071 /// 00072 /// After: 00073 /// ...... 00074 /// %cmp10 = fcmp une float %tmp1, %tmp2 00075 /// ...... 00076 /// %cmp11 = fcmp une float %tmp3, %tmp4 00077 /// %cmp12 = or i1 %cmp10, %cmp11 // parallel-or mode. 00078 /// br i1 %cmp12, label %if.then, label %ifend 00079 /// 00080 /// if.end: 00081 /// ...... 00082 /// 00083 /// if.then: 00084 /// ...... 00085 /// br label %if.end; 00086 /// 00087 /// Current implementation handles two cases. 00088 /// Case 1: \param BB is on the else-path. 00089 /// 00090 /// BB1 00091 /// / | 00092 /// BB2 | 00093 /// / \ | 00094 /// BB3 \ | where, BB1, BB2 contain conditional branches. 00095 /// \ | / BB3 contains unconditional branch. 00096 /// \ | / BB4 corresponds to \param BB which is also the merge. 00097 /// BB => BB4 00098 /// 00099 /// 00100 /// Corresponding source code: 00101 /// 00102 /// if (a == b && c == d) 00103 /// statement; // BB3 00104 /// 00105 /// Case 2: \param BB BB is on the then-path. 00106 /// 00107 /// BB1 00108 /// / | 00109 /// | BB2 00110 /// \ / | where BB1, BB2 contain conditional branches. 00111 /// BB => BB3 | BB3 contains unconditiona branch and corresponds 00112 /// \ / to \param BB. BB4 is the merge. 00113 /// BB4 00114 /// 00115 /// Corresponding source code: 00116 /// 00117 /// if (a == b || c == d) 00118 /// statement; // BB3 00119 /// 00120 /// In both cases, \param BB is the common successor of conditional branches. 00121 /// In Case 1, \param BB (BB4) has an unconditional branch (BB3) as 00122 /// its predecessor. In Case 2, \param BB (BB3) only has conditional branches 00123 /// as its predecessors. 00124 /// 00125 bool FlattenCFGOpt::FlattenParallelAndOr(BasicBlock *BB, IRBuilder<> &Builder, 00126 Pass *P) { 00127 PHINode *PHI = dyn_cast<PHINode>(BB->begin()); 00128 if (PHI) 00129 return false; // For simplicity, avoid cases containing PHI nodes. 00130 00131 BasicBlock *LastCondBlock = nullptr; 00132 BasicBlock *FirstCondBlock = nullptr; 00133 BasicBlock *UnCondBlock = nullptr; 00134 int Idx = -1; 00135 00136 // Check predecessors of \param BB. 00137 SmallPtrSet<BasicBlock *, 16> Preds(pred_begin(BB), pred_end(BB)); 00138 for (SmallPtrSetIterator<BasicBlock *> PI = Preds.begin(), PE = Preds.end(); 00139 PI != PE; ++PI) { 00140 BasicBlock *Pred = *PI; 00141 BranchInst *PBI = dyn_cast<BranchInst>(Pred->getTerminator()); 00142 00143 // All predecessors should terminate with a branch. 00144 if (!PBI) 00145 return false; 00146 00147 BasicBlock *PP = Pred->getSinglePredecessor(); 00148 00149 if (PBI->isUnconditional()) { 00150 // Case 1: Pred (BB3) is an unconditional block, it should 00151 // have a single predecessor (BB2) that is also a predecessor 00152 // of \param BB (BB4) and should not have address-taken. 00153 // There should exist only one such unconditional 00154 // branch among the predecessors. 00155 if (UnCondBlock || !PP || (Preds.count(PP) == 0) || 00156 Pred->hasAddressTaken()) 00157 return false; 00158 00159 UnCondBlock = Pred; 00160 continue; 00161 } 00162 00163 // Only conditional branches are allowed beyond this point. 00164 assert(PBI->isConditional()); 00165 00166 // Condition's unique use should be the branch instruction. 00167 Value *PC = PBI->getCondition(); 00168 if (!PC || !PC->hasOneUse()) 00169 return false; 00170 00171 if (PP && Preds.count(PP)) { 00172 // These are internal condition blocks to be merged from, e.g., 00173 // BB2 in both cases. 00174 // Should not be address-taken. 00175 if (Pred->hasAddressTaken()) 00176 return false; 00177 00178 // Instructions in the internal condition blocks should be safe 00179 // to hoist up. 00180 for (BasicBlock::iterator BI = Pred->begin(), BE = PBI; BI != BE;) { 00181 Instruction *CI = BI++; 00182 if (isa<PHINode>(CI) || !isSafeToSpeculativelyExecute(CI)) 00183 return false; 00184 } 00185 } else { 00186 // This is the condition block to be merged into, e.g. BB1 in 00187 // both cases. 00188 if (FirstCondBlock) 00189 return false; 00190 FirstCondBlock = Pred; 00191 } 00192 00193 // Find whether BB is uniformly on the true (or false) path 00194 // for all of its predecessors. 00195 BasicBlock *PS1 = PBI->getSuccessor(0); 00196 BasicBlock *PS2 = PBI->getSuccessor(1); 00197 BasicBlock *PS = (PS1 == BB) ? PS2 : PS1; 00198 int CIdx = (PS1 == BB) ? 0 : 1; 00199 00200 if (Idx == -1) 00201 Idx = CIdx; 00202 else if (CIdx != Idx) 00203 return false; 00204 00205 // PS is the successor which is not BB. Check successors to identify 00206 // the last conditional branch. 00207 if (Preds.count(PS) == 0) { 00208 // Case 2. 00209 LastCondBlock = Pred; 00210 } else { 00211 // Case 1 00212 BranchInst *BPS = dyn_cast<BranchInst>(PS->getTerminator()); 00213 if (BPS && BPS->isUnconditional()) { 00214 // Case 1: PS(BB3) should be an unconditional branch. 00215 LastCondBlock = Pred; 00216 } 00217 } 00218 } 00219 00220 if (!FirstCondBlock || !LastCondBlock || (FirstCondBlock == LastCondBlock)) 00221 return false; 00222 00223 TerminatorInst *TBB = LastCondBlock->getTerminator(); 00224 BasicBlock *PS1 = TBB->getSuccessor(0); 00225 BasicBlock *PS2 = TBB->getSuccessor(1); 00226 BranchInst *PBI1 = dyn_cast<BranchInst>(PS1->getTerminator()); 00227 BranchInst *PBI2 = dyn_cast<BranchInst>(PS2->getTerminator()); 00228 00229 // If PS1 does not jump into PS2, but PS2 jumps into PS1, 00230 // attempt branch inversion. 00231 if (!PBI1 || !PBI1->isUnconditional() || 00232 (PS1->getTerminator()->getSuccessor(0) != PS2)) { 00233 // Check whether PS2 jumps into PS1. 00234 if (!PBI2 || !PBI2->isUnconditional() || 00235 (PS2->getTerminator()->getSuccessor(0) != PS1)) 00236 return false; 00237 00238 // Do branch inversion. 00239 BasicBlock *CurrBlock = LastCondBlock; 00240 bool EverChanged = false; 00241 for (;CurrBlock != FirstCondBlock; 00242 CurrBlock = CurrBlock->getSinglePredecessor()) { 00243 BranchInst *BI = dyn_cast<BranchInst>(CurrBlock->getTerminator()); 00244 CmpInst *CI = dyn_cast<CmpInst>(BI->getCondition()); 00245 if (!CI) 00246 continue; 00247 00248 CmpInst::Predicate Predicate = CI->getPredicate(); 00249 // Canonicalize icmp_ne -> icmp_eq, fcmp_one -> fcmp_oeq 00250 if ((Predicate == CmpInst::ICMP_NE) || (Predicate == CmpInst::FCMP_ONE)) { 00251 CI->setPredicate(ICmpInst::getInversePredicate(Predicate)); 00252 BI->swapSuccessors(); 00253 EverChanged = true; 00254 } 00255 } 00256 return EverChanged; 00257 } 00258 00259 // PS1 must have a conditional branch. 00260 if (!PBI1 || !PBI1->isUnconditional()) 00261 return false; 00262 00263 // PS2 should not contain PHI node. 00264 PHI = dyn_cast<PHINode>(PS2->begin()); 00265 if (PHI) 00266 return false; 00267 00268 // Do the transformation. 00269 BasicBlock *CB; 00270 BranchInst *PBI = dyn_cast<BranchInst>(FirstCondBlock->getTerminator()); 00271 bool Iteration = true; 00272 IRBuilder<>::InsertPointGuard Guard(Builder); 00273 Value *PC = PBI->getCondition(); 00274 00275 do { 00276 CB = PBI->getSuccessor(1 - Idx); 00277 // Delete the conditional branch. 00278 FirstCondBlock->getInstList().pop_back(); 00279 FirstCondBlock->getInstList() 00280 .splice(FirstCondBlock->end(), CB->getInstList()); 00281 PBI = cast<BranchInst>(FirstCondBlock->getTerminator()); 00282 Value *CC = PBI->getCondition(); 00283 // Merge conditions. 00284 Builder.SetInsertPoint(PBI); 00285 Value *NC; 00286 if (Idx == 0) 00287 // Case 2, use parallel or. 00288 NC = Builder.CreateOr(PC, CC); 00289 else 00290 // Case 1, use parallel and. 00291 NC = Builder.CreateAnd(PC, CC); 00292 00293 PBI->replaceUsesOfWith(CC, NC); 00294 PC = NC; 00295 if (CB == LastCondBlock) 00296 Iteration = false; 00297 // Remove internal conditional branches. 00298 CB->dropAllReferences(); 00299 // make CB unreachable and let downstream to delete the block. 00300 new UnreachableInst(CB->getContext(), CB); 00301 } while (Iteration); 00302 00303 DEBUG(dbgs() << "Use parallel and/or in:\n" << *FirstCondBlock); 00304 return true; 00305 } 00306 00307 /// Compare blocks from two if-regions, where \param Head1 is the entry of the 00308 /// 1st if-region. \param Head2 is the entry of the 2nd if-region. \param 00309 /// Block1 is a block in the 1st if-region to compare. \param Block2 is a block 00310 // in the 2nd if-region to compare. \returns true if \param Block1 and \param 00311 /// Block2 have identical instructions and do not have memory reference alias 00312 /// with \param Head2. 00313 /// 00314 bool FlattenCFGOpt::CompareIfRegionBlock(BasicBlock *Head1, BasicBlock *Head2, 00315 BasicBlock *Block1, 00316 BasicBlock *Block2) { 00317 TerminatorInst *PTI2 = Head2->getTerminator(); 00318 Instruction *PBI2 = Head2->begin(); 00319 00320 bool eq1 = (Block1 == Head1); 00321 bool eq2 = (Block2 == Head2); 00322 if (eq1 || eq2) { 00323 // An empty then-path or else-path. 00324 return (eq1 == eq2); 00325 } 00326 00327 // Check whether instructions in Block1 and Block2 are identical 00328 // and do not alias with instructions in Head2. 00329 BasicBlock::iterator iter1 = Block1->begin(); 00330 BasicBlock::iterator end1 = Block1->getTerminator(); 00331 BasicBlock::iterator iter2 = Block2->begin(); 00332 BasicBlock::iterator end2 = Block2->getTerminator(); 00333 00334 while (1) { 00335 if (iter1 == end1) { 00336 if (iter2 != end2) 00337 return false; 00338 break; 00339 } 00340 00341 if (!iter1->isIdenticalTo(iter2)) 00342 return false; 00343 00344 // Illegal to remove instructions with side effects except 00345 // non-volatile stores. 00346 if (iter1->mayHaveSideEffects()) { 00347 Instruction *CurI = &*iter1; 00348 StoreInst *SI = dyn_cast<StoreInst>(CurI); 00349 if (!SI || SI->isVolatile()) 00350 return false; 00351 } 00352 00353 // For simplicity and speed, data dependency check can be 00354 // avoided if read from memory doesn't exist. 00355 if (iter1->mayReadFromMemory()) 00356 return false; 00357 00358 if (iter1->mayWriteToMemory()) { 00359 for (BasicBlock::iterator BI = PBI2, BE = PTI2; BI != BE; ++BI) { 00360 if (BI->mayReadFromMemory() || BI->mayWriteToMemory()) { 00361 // Check alias with Head2. 00362 if (!AA || AA->alias(iter1, BI)) 00363 return false; 00364 } 00365 } 00366 } 00367 ++iter1; 00368 ++iter2; 00369 } 00370 00371 return true; 00372 } 00373 00374 /// Check whether \param BB is the merge block of a if-region. If yes, check 00375 /// whether there exists an adjacent if-region upstream, the two if-regions 00376 /// contain identical instructions and can be legally merged. \returns true if 00377 /// the two if-regions are merged. 00378 /// 00379 /// From: 00380 /// if (a) 00381 /// statement; 00382 /// if (b) 00383 /// statement; 00384 /// 00385 /// To: 00386 /// if (a || b) 00387 /// statement; 00388 /// 00389 bool FlattenCFGOpt::MergeIfRegion(BasicBlock *BB, IRBuilder<> &Builder, 00390 Pass *P) { 00391 BasicBlock *IfTrue2, *IfFalse2; 00392 Value *IfCond2 = GetIfCondition(BB, IfTrue2, IfFalse2); 00393 Instruction *CInst2 = dyn_cast_or_null<Instruction>(IfCond2); 00394 if (!CInst2) 00395 return false; 00396 00397 BasicBlock *SecondEntryBlock = CInst2->getParent(); 00398 if (SecondEntryBlock->hasAddressTaken()) 00399 return false; 00400 00401 BasicBlock *IfTrue1, *IfFalse1; 00402 Value *IfCond1 = GetIfCondition(SecondEntryBlock, IfTrue1, IfFalse1); 00403 Instruction *CInst1 = dyn_cast_or_null<Instruction>(IfCond1); 00404 if (!CInst1) 00405 return false; 00406 00407 BasicBlock *FirstEntryBlock = CInst1->getParent(); 00408 00409 // Either then-path or else-path should be empty. 00410 if ((IfTrue1 != FirstEntryBlock) && (IfFalse1 != FirstEntryBlock)) 00411 return false; 00412 if ((IfTrue2 != SecondEntryBlock) && (IfFalse2 != SecondEntryBlock)) 00413 return false; 00414 00415 TerminatorInst *PTI2 = SecondEntryBlock->getTerminator(); 00416 Instruction *PBI2 = SecondEntryBlock->begin(); 00417 00418 if (!CompareIfRegionBlock(FirstEntryBlock, SecondEntryBlock, IfTrue1, 00419 IfTrue2)) 00420 return false; 00421 00422 if (!CompareIfRegionBlock(FirstEntryBlock, SecondEntryBlock, IfFalse1, 00423 IfFalse2)) 00424 return false; 00425 00426 // Check whether \param SecondEntryBlock has side-effect and is safe to 00427 // speculate. 00428 for (BasicBlock::iterator BI = PBI2, BE = PTI2; BI != BE; ++BI) { 00429 Instruction *CI = BI; 00430 if (isa<PHINode>(CI) || CI->mayHaveSideEffects() || 00431 !isSafeToSpeculativelyExecute(CI)) 00432 return false; 00433 } 00434 00435 // Merge \param SecondEntryBlock into \param FirstEntryBlock. 00436 FirstEntryBlock->getInstList().pop_back(); 00437 FirstEntryBlock->getInstList() 00438 .splice(FirstEntryBlock->end(), SecondEntryBlock->getInstList()); 00439 BranchInst *PBI = dyn_cast<BranchInst>(FirstEntryBlock->getTerminator()); 00440 Value *CC = PBI->getCondition(); 00441 BasicBlock *SaveInsertBB = Builder.GetInsertBlock(); 00442 BasicBlock::iterator SaveInsertPt = Builder.GetInsertPoint(); 00443 Builder.SetInsertPoint(PBI); 00444 Value *NC = Builder.CreateOr(CInst1, CC); 00445 PBI->replaceUsesOfWith(CC, NC); 00446 Builder.SetInsertPoint(SaveInsertBB, SaveInsertPt); 00447 00448 // Remove IfTrue1 00449 if (IfTrue1 != FirstEntryBlock) { 00450 IfTrue1->dropAllReferences(); 00451 IfTrue1->eraseFromParent(); 00452 } 00453 00454 // Remove IfFalse1 00455 if (IfFalse1 != FirstEntryBlock) { 00456 IfFalse1->dropAllReferences(); 00457 IfFalse1->eraseFromParent(); 00458 } 00459 00460 // Remove \param SecondEntryBlock 00461 SecondEntryBlock->dropAllReferences(); 00462 SecondEntryBlock->eraseFromParent(); 00463 DEBUG(dbgs() << "If conditions merged into:\n" << *FirstEntryBlock); 00464 return true; 00465 } 00466 00467 bool FlattenCFGOpt::run(BasicBlock *BB) { 00468 bool Changed = false; 00469 assert(BB && BB->getParent() && "Block not embedded in function!"); 00470 assert(BB->getTerminator() && "Degenerate basic block encountered!"); 00471 00472 IRBuilder<> Builder(BB); 00473 00474 if (FlattenParallelAndOr(BB, Builder)) 00475 return true; 00476 00477 if (MergeIfRegion(BB, Builder)) 00478 return true; 00479 00480 return Changed; 00481 } 00482 00483 /// FlattenCFG - This function is used to flatten a CFG. For 00484 /// example, it uses parallel-and and parallel-or mode to collapse 00485 // if-conditions and merge if-regions with identical statements. 00486 /// 00487 bool llvm::FlattenCFG(BasicBlock *BB, AliasAnalysis *AA) { 00488 return FlattenCFGOpt(AA).run(BB); 00489 }