FlattenCFG.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548
  1. //===- FlatternCFG.cpp - Code to perform CFG flattening -------------------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // Reduce conditional branches in CFG.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #include "llvm/ADT/SmallPtrSet.h"
  13. #include "llvm/Analysis/AliasAnalysis.h"
  14. #include "llvm/Transforms/Utils/Local.h"
  15. #include "llvm/Analysis/ValueTracking.h"
  16. #include "llvm/IR/BasicBlock.h"
  17. #include "llvm/IR/IRBuilder.h"
  18. #include "llvm/IR/InstrTypes.h"
  19. #include "llvm/IR/Instruction.h"
  20. #include "llvm/IR/Instructions.h"
  21. #include "llvm/IR/Value.h"
  22. #include "llvm/Support/Casting.h"
  23. #include "llvm/Support/Debug.h"
  24. #include "llvm/Support/raw_ostream.h"
  25. #include "llvm/Transforms/Utils/BasicBlockUtils.h"
  26. #include <cassert>
  27. using namespace llvm;
  28. #define DEBUG_TYPE "flattencfg"
  29. namespace {
  30. class FlattenCFGOpt {
  31. AliasAnalysis *AA;
  32. /// Use parallel-and or parallel-or to generate conditions for
  33. /// conditional branches.
  34. bool FlattenParallelAndOr(BasicBlock *BB, IRBuilder<> &Builder);
  35. /// If \param BB is the merge block of an if-region, attempt to merge
  36. /// the if-region with an adjacent if-region upstream if two if-regions
  37. /// contain identical instructions.
  38. bool MergeIfRegion(BasicBlock *BB, IRBuilder<> &Builder);
  39. /// Compare a pair of blocks: \p Block1 and \p Block2, which
  40. /// are from two if-regions, where \p Head2 is the entry block of the 2nd
  41. /// if-region. \returns true if \p Block1 and \p Block2 contain identical
  42. /// instructions, and have no memory reference alias with \p Head2.
  43. /// This is used as a legality check for merging if-regions.
  44. bool CompareIfRegionBlock(BasicBlock *Block1, BasicBlock *Block2,
  45. BasicBlock *Head2);
  46. public:
  47. FlattenCFGOpt(AliasAnalysis *AA) : AA(AA) {}
  48. bool run(BasicBlock *BB);
  49. };
  50. } // end anonymous namespace
  51. /// If \param [in] BB has more than one predecessor that is a conditional
  52. /// branch, attempt to use parallel and/or for the branch condition. \returns
  53. /// true on success.
  54. ///
  55. /// Before:
  56. /// ......
  57. /// %cmp10 = fcmp une float %tmp1, %tmp2
  58. /// br i1 %cmp10, label %if.then, label %lor.rhs
  59. ///
  60. /// lor.rhs:
  61. /// ......
  62. /// %cmp11 = fcmp une float %tmp3, %tmp4
  63. /// br i1 %cmp11, label %if.then, label %ifend
  64. ///
  65. /// if.end: // the merge block
  66. /// ......
  67. ///
  68. /// if.then: // has two predecessors, both of them contains conditional branch.
  69. /// ......
  70. /// br label %if.end;
  71. ///
  72. /// After:
  73. /// ......
  74. /// %cmp10 = fcmp une float %tmp1, %tmp2
  75. /// ......
  76. /// %cmp11 = fcmp une float %tmp3, %tmp4
  77. /// %cmp12 = or i1 %cmp10, %cmp11 // parallel-or mode.
  78. /// br i1 %cmp12, label %if.then, label %ifend
  79. ///
  80. /// if.end:
  81. /// ......
  82. ///
  83. /// if.then:
  84. /// ......
  85. /// br label %if.end;
  86. ///
  87. /// Current implementation handles two cases.
  88. /// Case 1: BB is on the else-path.
  89. ///
  90. /// BB1
  91. /// / |
  92. /// BB2 |
  93. /// / \ |
  94. /// BB3 \ | where, BB1, BB2 contain conditional branches.
  95. /// \ | / BB3 contains unconditional branch.
  96. /// \ | / BB4 corresponds to BB which is also the merge.
  97. /// BB => BB4
  98. ///
  99. ///
  100. /// Corresponding source code:
  101. ///
  102. /// if (a == b && c == d)
  103. /// statement; // BB3
  104. ///
  105. /// Case 2: BB is on the then-path.
  106. ///
  107. /// BB1
  108. /// / |
  109. /// | BB2
  110. /// \ / | where BB1, BB2 contain conditional branches.
  111. /// BB => BB3 | BB3 contains unconditiona branch and corresponds
  112. /// \ / to BB. BB4 is the merge.
  113. /// BB4
  114. ///
  115. /// Corresponding source code:
  116. ///
  117. /// if (a == b || c == d)
  118. /// statement; // BB3
  119. ///
  120. /// In both cases, BB is the common successor of conditional branches.
  121. /// In Case 1, BB (BB4) has an unconditional branch (BB3) as
  122. /// its predecessor. In Case 2, BB (BB3) only has conditional branches
  123. /// as its predecessors.
  124. bool FlattenCFGOpt::FlattenParallelAndOr(BasicBlock *BB, IRBuilder<> &Builder) {
  125. PHINode *PHI = dyn_cast<PHINode>(BB->begin());
  126. if (PHI)
  127. return false; // For simplicity, avoid cases containing PHI nodes.
  128. BasicBlock *LastCondBlock = nullptr;
  129. BasicBlock *FirstCondBlock = nullptr;
  130. BasicBlock *UnCondBlock = nullptr;
  131. int Idx = -1;
  132. // Check predecessors of \param BB.
  133. SmallPtrSet<BasicBlock *, 16> Preds(pred_begin(BB), pred_end(BB));
  134. for (BasicBlock *Pred : Preds) {
  135. BranchInst *PBI = dyn_cast<BranchInst>(Pred->getTerminator());
  136. // All predecessors should terminate with a branch.
  137. if (!PBI)
  138. return false;
  139. BasicBlock *PP = Pred->getSinglePredecessor();
  140. if (PBI->isUnconditional()) {
  141. // Case 1: Pred (BB3) is an unconditional block, it should
  142. // have a single predecessor (BB2) that is also a predecessor
  143. // of \param BB (BB4) and should not have address-taken.
  144. // There should exist only one such unconditional
  145. // branch among the predecessors.
  146. if (UnCondBlock || !PP || !Preds.contains(PP) ||
  147. Pred->hasAddressTaken())
  148. return false;
  149. UnCondBlock = Pred;
  150. continue;
  151. }
  152. // Only conditional branches are allowed beyond this point.
  153. assert(PBI->isConditional());
  154. // Condition's unique use should be the branch instruction.
  155. Value *PC = PBI->getCondition();
  156. if (!PC || !PC->hasOneUse())
  157. return false;
  158. if (PP && Preds.count(PP)) {
  159. // These are internal condition blocks to be merged from, e.g.,
  160. // BB2 in both cases.
  161. // Should not be address-taken.
  162. if (Pred->hasAddressTaken())
  163. return false;
  164. // Instructions in the internal condition blocks should be safe
  165. // to hoist up.
  166. for (BasicBlock::iterator BI = Pred->begin(), BE = PBI->getIterator();
  167. BI != BE;) {
  168. Instruction *CI = &*BI++;
  169. if (isa<PHINode>(CI) || !isSafeToSpeculativelyExecute(CI))
  170. return false;
  171. }
  172. } else {
  173. // This is the condition block to be merged into, e.g. BB1 in
  174. // both cases.
  175. if (FirstCondBlock)
  176. return false;
  177. FirstCondBlock = Pred;
  178. }
  179. // Find whether BB is uniformly on the true (or false) path
  180. // for all of its predecessors.
  181. BasicBlock *PS1 = PBI->getSuccessor(0);
  182. BasicBlock *PS2 = PBI->getSuccessor(1);
  183. BasicBlock *PS = (PS1 == BB) ? PS2 : PS1;
  184. int CIdx = (PS1 == BB) ? 0 : 1;
  185. if (Idx == -1)
  186. Idx = CIdx;
  187. else if (CIdx != Idx)
  188. return false;
  189. // PS is the successor which is not BB. Check successors to identify
  190. // the last conditional branch.
  191. if (!Preds.contains(PS)) {
  192. // Case 2.
  193. LastCondBlock = Pred;
  194. } else {
  195. // Case 1
  196. BranchInst *BPS = dyn_cast<BranchInst>(PS->getTerminator());
  197. if (BPS && BPS->isUnconditional()) {
  198. // Case 1: PS(BB3) should be an unconditional branch.
  199. LastCondBlock = Pred;
  200. }
  201. }
  202. }
  203. if (!FirstCondBlock || !LastCondBlock || (FirstCondBlock == LastCondBlock))
  204. return false;
  205. Instruction *TBB = LastCondBlock->getTerminator();
  206. BasicBlock *PS1 = TBB->getSuccessor(0);
  207. BasicBlock *PS2 = TBB->getSuccessor(1);
  208. BranchInst *PBI1 = dyn_cast<BranchInst>(PS1->getTerminator());
  209. BranchInst *PBI2 = dyn_cast<BranchInst>(PS2->getTerminator());
  210. // If PS1 does not jump into PS2, but PS2 jumps into PS1,
  211. // attempt branch inversion.
  212. if (!PBI1 || !PBI1->isUnconditional() ||
  213. (PS1->getTerminator()->getSuccessor(0) != PS2)) {
  214. // Check whether PS2 jumps into PS1.
  215. if (!PBI2 || !PBI2->isUnconditional() ||
  216. (PS2->getTerminator()->getSuccessor(0) != PS1))
  217. return false;
  218. // Do branch inversion.
  219. BasicBlock *CurrBlock = LastCondBlock;
  220. bool EverChanged = false;
  221. for (; CurrBlock != FirstCondBlock;
  222. CurrBlock = CurrBlock->getSinglePredecessor()) {
  223. auto *BI = cast<BranchInst>(CurrBlock->getTerminator());
  224. auto *CI = dyn_cast<CmpInst>(BI->getCondition());
  225. if (!CI)
  226. continue;
  227. CmpInst::Predicate Predicate = CI->getPredicate();
  228. // Canonicalize icmp_ne -> icmp_eq, fcmp_one -> fcmp_oeq
  229. if ((Predicate == CmpInst::ICMP_NE) || (Predicate == CmpInst::FCMP_ONE)) {
  230. CI->setPredicate(ICmpInst::getInversePredicate(Predicate));
  231. BI->swapSuccessors();
  232. EverChanged = true;
  233. }
  234. }
  235. return EverChanged;
  236. }
  237. // PS1 must have a conditional branch.
  238. if (!PBI1 || !PBI1->isUnconditional())
  239. return false;
  240. // PS2 should not contain PHI node.
  241. PHI = dyn_cast<PHINode>(PS2->begin());
  242. if (PHI)
  243. return false;
  244. // Do the transformation.
  245. BasicBlock *CB;
  246. BranchInst *PBI = cast<BranchInst>(FirstCondBlock->getTerminator());
  247. bool Iteration = true;
  248. IRBuilder<>::InsertPointGuard Guard(Builder);
  249. Value *PC = PBI->getCondition();
  250. do {
  251. CB = PBI->getSuccessor(1 - Idx);
  252. // Delete the conditional branch.
  253. FirstCondBlock->back().eraseFromParent();
  254. FirstCondBlock->splice(FirstCondBlock->end(), CB);
  255. PBI = cast<BranchInst>(FirstCondBlock->getTerminator());
  256. Value *CC = PBI->getCondition();
  257. // Merge conditions.
  258. Builder.SetInsertPoint(PBI);
  259. Value *NC;
  260. if (Idx == 0)
  261. // Case 2, use parallel or.
  262. NC = Builder.CreateOr(PC, CC);
  263. else
  264. // Case 1, use parallel and.
  265. NC = Builder.CreateAnd(PC, CC);
  266. PBI->replaceUsesOfWith(CC, NC);
  267. PC = NC;
  268. if (CB == LastCondBlock)
  269. Iteration = false;
  270. // Remove internal conditional branches.
  271. CB->dropAllReferences();
  272. // make CB unreachable and let downstream to delete the block.
  273. new UnreachableInst(CB->getContext(), CB);
  274. } while (Iteration);
  275. LLVM_DEBUG(dbgs() << "Use parallel and/or in:\n" << *FirstCondBlock);
  276. return true;
  277. }
  278. /// Compare blocks from two if-regions, where \param Head2 is the entry of the
  279. /// 2nd if-region. \param Block1 is a block in the 1st if-region to compare.
  280. /// \param Block2 is a block in the 2nd if-region to compare. \returns true if
  281. /// Block1 and Block2 have identical instructions and do not have
  282. /// memory reference alias with Head2.
  283. bool FlattenCFGOpt::CompareIfRegionBlock(BasicBlock *Block1, BasicBlock *Block2,
  284. BasicBlock *Head2) {
  285. Instruction *PTI2 = Head2->getTerminator();
  286. Instruction *PBI2 = &Head2->front();
  287. // Check whether instructions in Block1 and Block2 are identical
  288. // and do not alias with instructions in Head2.
  289. BasicBlock::iterator iter1 = Block1->begin();
  290. BasicBlock::iterator end1 = Block1->getTerminator()->getIterator();
  291. BasicBlock::iterator iter2 = Block2->begin();
  292. BasicBlock::iterator end2 = Block2->getTerminator()->getIterator();
  293. while (true) {
  294. if (iter1 == end1) {
  295. if (iter2 != end2)
  296. return false;
  297. break;
  298. }
  299. if (!iter1->isIdenticalTo(&*iter2))
  300. return false;
  301. // Illegal to remove instructions with side effects except
  302. // non-volatile stores.
  303. if (iter1->mayHaveSideEffects()) {
  304. Instruction *CurI = &*iter1;
  305. StoreInst *SI = dyn_cast<StoreInst>(CurI);
  306. if (!SI || SI->isVolatile())
  307. return false;
  308. }
  309. // For simplicity and speed, data dependency check can be
  310. // avoided if read from memory doesn't exist.
  311. if (iter1->mayReadFromMemory())
  312. return false;
  313. if (iter1->mayWriteToMemory()) {
  314. for (BasicBlock::iterator BI(PBI2), BE(PTI2); BI != BE; ++BI) {
  315. if (BI->mayReadFromMemory() || BI->mayWriteToMemory()) {
  316. // Check alias with Head2.
  317. if (!AA || !AA->isNoAlias(&*iter1, &*BI))
  318. return false;
  319. }
  320. }
  321. }
  322. ++iter1;
  323. ++iter2;
  324. }
  325. return true;
  326. }
  327. /// Check whether \param BB is the merge block of a if-region. If yes, check
  328. /// whether there exists an adjacent if-region upstream, the two if-regions
  329. /// contain identical instructions and can be legally merged. \returns true if
  330. /// the two if-regions are merged.
  331. ///
  332. /// From:
  333. /// if (a)
  334. /// statement;
  335. /// if (b)
  336. /// statement;
  337. ///
  338. /// To:
  339. /// if (a || b)
  340. /// statement;
  341. ///
  342. ///
  343. /// And from:
  344. /// if (a)
  345. /// ;
  346. /// else
  347. /// statement;
  348. /// if (b)
  349. /// ;
  350. /// else
  351. /// statement;
  352. ///
  353. /// To:
  354. /// if (a && b)
  355. /// ;
  356. /// else
  357. /// statement;
  358. ///
  359. /// We always take the form of the first if-region. This means that if the
  360. /// statement in the first if-region, is in the "then-path", while in the second
  361. /// if-region it is in the "else-path", then we convert the second to the first
  362. /// form, by inverting the condition and the branch successors. The same
  363. /// approach goes for the opposite case.
  364. bool FlattenCFGOpt::MergeIfRegion(BasicBlock *BB, IRBuilder<> &Builder) {
  365. BasicBlock *IfTrue2, *IfFalse2;
  366. BranchInst *DomBI2 = GetIfCondition(BB, IfTrue2, IfFalse2);
  367. if (!DomBI2)
  368. return false;
  369. Instruction *CInst2 = dyn_cast<Instruction>(DomBI2->getCondition());
  370. if (!CInst2)
  371. return false;
  372. BasicBlock *SecondEntryBlock = CInst2->getParent();
  373. if (SecondEntryBlock->hasAddressTaken())
  374. return false;
  375. BasicBlock *IfTrue1, *IfFalse1;
  376. BranchInst *DomBI1 = GetIfCondition(SecondEntryBlock, IfTrue1, IfFalse1);
  377. if (!DomBI1)
  378. return false;
  379. Instruction *CInst1 = dyn_cast<Instruction>(DomBI1->getCondition());
  380. if (!CInst1)
  381. return false;
  382. BasicBlock *FirstEntryBlock = CInst1->getParent();
  383. // Don't die trying to process degenerate/unreachable code.
  384. if (FirstEntryBlock == SecondEntryBlock)
  385. return false;
  386. // Either then-path or else-path should be empty.
  387. bool InvertCond2 = false;
  388. BinaryOperator::BinaryOps CombineOp;
  389. if (IfFalse1 == FirstEntryBlock) {
  390. // The else-path is empty, so we must use "or" operation to combine the
  391. // conditions.
  392. CombineOp = BinaryOperator::Or;
  393. if (IfFalse2 != SecondEntryBlock) {
  394. if (IfTrue2 != SecondEntryBlock)
  395. return false;
  396. InvertCond2 = true;
  397. std::swap(IfTrue2, IfFalse2);
  398. }
  399. if (!CompareIfRegionBlock(IfTrue1, IfTrue2, SecondEntryBlock))
  400. return false;
  401. } else if (IfTrue1 == FirstEntryBlock) {
  402. // The then-path is empty, so we must use "and" operation to combine the
  403. // conditions.
  404. CombineOp = BinaryOperator::And;
  405. if (IfTrue2 != SecondEntryBlock) {
  406. if (IfFalse2 != SecondEntryBlock)
  407. return false;
  408. InvertCond2 = true;
  409. std::swap(IfTrue2, IfFalse2);
  410. }
  411. if (!CompareIfRegionBlock(IfFalse1, IfFalse2, SecondEntryBlock))
  412. return false;
  413. } else
  414. return false;
  415. Instruction *PTI2 = SecondEntryBlock->getTerminator();
  416. Instruction *PBI2 = &SecondEntryBlock->front();
  417. // Check whether \param SecondEntryBlock has side-effect and is safe to
  418. // speculate.
  419. for (BasicBlock::iterator BI(PBI2), BE(PTI2); BI != BE; ++BI) {
  420. Instruction *CI = &*BI;
  421. if (isa<PHINode>(CI) || CI->mayHaveSideEffects() ||
  422. !isSafeToSpeculativelyExecute(CI))
  423. return false;
  424. }
  425. // Merge \param SecondEntryBlock into \param FirstEntryBlock.
  426. FirstEntryBlock->back().eraseFromParent();
  427. FirstEntryBlock->splice(FirstEntryBlock->end(), SecondEntryBlock);
  428. BranchInst *PBI = cast<BranchInst>(FirstEntryBlock->getTerminator());
  429. assert(PBI->getCondition() == CInst2);
  430. BasicBlock *SaveInsertBB = Builder.GetInsertBlock();
  431. BasicBlock::iterator SaveInsertPt = Builder.GetInsertPoint();
  432. Builder.SetInsertPoint(PBI);
  433. if (InvertCond2) {
  434. // If this is a "cmp" instruction, only used for branching (and nowhere
  435. // else), then we can simply invert the predicate.
  436. auto Cmp2 = dyn_cast<CmpInst>(CInst2);
  437. if (Cmp2 && Cmp2->hasOneUse())
  438. Cmp2->setPredicate(Cmp2->getInversePredicate());
  439. else
  440. CInst2 = cast<Instruction>(Builder.CreateNot(CInst2));
  441. PBI->swapSuccessors();
  442. }
  443. Value *NC = Builder.CreateBinOp(CombineOp, CInst1, CInst2);
  444. PBI->replaceUsesOfWith(CInst2, NC);
  445. Builder.SetInsertPoint(SaveInsertBB, SaveInsertPt);
  446. // Handle PHI node to replace its predecessors to FirstEntryBlock.
  447. for (BasicBlock *Succ : successors(PBI)) {
  448. for (PHINode &Phi : Succ->phis()) {
  449. for (unsigned i = 0, e = Phi.getNumIncomingValues(); i != e; ++i) {
  450. if (Phi.getIncomingBlock(i) == SecondEntryBlock)
  451. Phi.setIncomingBlock(i, FirstEntryBlock);
  452. }
  453. }
  454. }
  455. // Remove IfTrue1
  456. if (IfTrue1 != FirstEntryBlock) {
  457. IfTrue1->dropAllReferences();
  458. IfTrue1->eraseFromParent();
  459. }
  460. // Remove IfFalse1
  461. if (IfFalse1 != FirstEntryBlock) {
  462. IfFalse1->dropAllReferences();
  463. IfFalse1->eraseFromParent();
  464. }
  465. // Remove \param SecondEntryBlock
  466. SecondEntryBlock->dropAllReferences();
  467. SecondEntryBlock->eraseFromParent();
  468. LLVM_DEBUG(dbgs() << "If conditions merged into:\n" << *FirstEntryBlock);
  469. return true;
  470. }
  471. bool FlattenCFGOpt::run(BasicBlock *BB) {
  472. assert(BB && BB->getParent() && "Block not embedded in function!");
  473. assert(BB->getTerminator() && "Degenerate basic block encountered!");
  474. IRBuilder<> Builder(BB);
  475. if (FlattenParallelAndOr(BB, Builder) || MergeIfRegion(BB, Builder))
  476. return true;
  477. return false;
  478. }
  479. /// FlattenCFG - This function is used to flatten a CFG. For
  480. /// example, it uses parallel-and and parallel-or mode to collapse
  481. /// if-conditions and merge if-regions with identical statements.
  482. bool llvm::FlattenCFG(BasicBlock *BB, AAResults *AA) {
  483. return FlattenCFGOpt(AA).run(BB);
  484. }