FlattenCFG.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549
  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 (SmallPtrSetIterator<BasicBlock *> PI = Preds.begin(), PE = Preds.end();
  135. PI != PE; ++PI) {
  136. BasicBlock *Pred = *PI;
  137. BranchInst *PBI = dyn_cast<BranchInst>(Pred->getTerminator());
  138. // All predecessors should terminate with a branch.
  139. if (!PBI)
  140. return false;
  141. BasicBlock *PP = Pred->getSinglePredecessor();
  142. if (PBI->isUnconditional()) {
  143. // Case 1: Pred (BB3) is an unconditional block, it should
  144. // have a single predecessor (BB2) that is also a predecessor
  145. // of \param BB (BB4) and should not have address-taken.
  146. // There should exist only one such unconditional
  147. // branch among the predecessors.
  148. if (UnCondBlock || !PP || !Preds.contains(PP) ||
  149. Pred->hasAddressTaken())
  150. return false;
  151. UnCondBlock = Pred;
  152. continue;
  153. }
  154. // Only conditional branches are allowed beyond this point.
  155. assert(PBI->isConditional());
  156. // Condition's unique use should be the branch instruction.
  157. Value *PC = PBI->getCondition();
  158. if (!PC || !PC->hasOneUse())
  159. return false;
  160. if (PP && Preds.count(PP)) {
  161. // These are internal condition blocks to be merged from, e.g.,
  162. // BB2 in both cases.
  163. // Should not be address-taken.
  164. if (Pred->hasAddressTaken())
  165. return false;
  166. // Instructions in the internal condition blocks should be safe
  167. // to hoist up.
  168. for (BasicBlock::iterator BI = Pred->begin(), BE = PBI->getIterator();
  169. BI != BE;) {
  170. Instruction *CI = &*BI++;
  171. if (isa<PHINode>(CI) || !isSafeToSpeculativelyExecute(CI))
  172. return false;
  173. }
  174. } else {
  175. // This is the condition block to be merged into, e.g. BB1 in
  176. // both cases.
  177. if (FirstCondBlock)
  178. return false;
  179. FirstCondBlock = Pred;
  180. }
  181. // Find whether BB is uniformly on the true (or false) path
  182. // for all of its predecessors.
  183. BasicBlock *PS1 = PBI->getSuccessor(0);
  184. BasicBlock *PS2 = PBI->getSuccessor(1);
  185. BasicBlock *PS = (PS1 == BB) ? PS2 : PS1;
  186. int CIdx = (PS1 == BB) ? 0 : 1;
  187. if (Idx == -1)
  188. Idx = CIdx;
  189. else if (CIdx != Idx)
  190. return false;
  191. // PS is the successor which is not BB. Check successors to identify
  192. // the last conditional branch.
  193. if (!Preds.contains(PS)) {
  194. // Case 2.
  195. LastCondBlock = Pred;
  196. } else {
  197. // Case 1
  198. BranchInst *BPS = dyn_cast<BranchInst>(PS->getTerminator());
  199. if (BPS && BPS->isUnconditional()) {
  200. // Case 1: PS(BB3) should be an unconditional branch.
  201. LastCondBlock = Pred;
  202. }
  203. }
  204. }
  205. if (!FirstCondBlock || !LastCondBlock || (FirstCondBlock == LastCondBlock))
  206. return false;
  207. Instruction *TBB = LastCondBlock->getTerminator();
  208. BasicBlock *PS1 = TBB->getSuccessor(0);
  209. BasicBlock *PS2 = TBB->getSuccessor(1);
  210. BranchInst *PBI1 = dyn_cast<BranchInst>(PS1->getTerminator());
  211. BranchInst *PBI2 = dyn_cast<BranchInst>(PS2->getTerminator());
  212. // If PS1 does not jump into PS2, but PS2 jumps into PS1,
  213. // attempt branch inversion.
  214. if (!PBI1 || !PBI1->isUnconditional() ||
  215. (PS1->getTerminator()->getSuccessor(0) != PS2)) {
  216. // Check whether PS2 jumps into PS1.
  217. if (!PBI2 || !PBI2->isUnconditional() ||
  218. (PS2->getTerminator()->getSuccessor(0) != PS1))
  219. return false;
  220. // Do branch inversion.
  221. BasicBlock *CurrBlock = LastCondBlock;
  222. bool EverChanged = false;
  223. for (; CurrBlock != FirstCondBlock;
  224. CurrBlock = CurrBlock->getSinglePredecessor()) {
  225. auto *BI = cast<BranchInst>(CurrBlock->getTerminator());
  226. auto *CI = dyn_cast<CmpInst>(BI->getCondition());
  227. if (!CI)
  228. continue;
  229. CmpInst::Predicate Predicate = CI->getPredicate();
  230. // Canonicalize icmp_ne -> icmp_eq, fcmp_one -> fcmp_oeq
  231. if ((Predicate == CmpInst::ICMP_NE) || (Predicate == CmpInst::FCMP_ONE)) {
  232. CI->setPredicate(ICmpInst::getInversePredicate(Predicate));
  233. BI->swapSuccessors();
  234. EverChanged = true;
  235. }
  236. }
  237. return EverChanged;
  238. }
  239. // PS1 must have a conditional branch.
  240. if (!PBI1 || !PBI1->isUnconditional())
  241. return false;
  242. // PS2 should not contain PHI node.
  243. PHI = dyn_cast<PHINode>(PS2->begin());
  244. if (PHI)
  245. return false;
  246. // Do the transformation.
  247. BasicBlock *CB;
  248. BranchInst *PBI = cast<BranchInst>(FirstCondBlock->getTerminator());
  249. bool Iteration = true;
  250. IRBuilder<>::InsertPointGuard Guard(Builder);
  251. Value *PC = PBI->getCondition();
  252. do {
  253. CB = PBI->getSuccessor(1 - Idx);
  254. // Delete the conditional branch.
  255. FirstCondBlock->getInstList().pop_back();
  256. FirstCondBlock->getInstList()
  257. .splice(FirstCondBlock->end(), CB->getInstList());
  258. PBI = cast<BranchInst>(FirstCondBlock->getTerminator());
  259. Value *CC = PBI->getCondition();
  260. // Merge conditions.
  261. Builder.SetInsertPoint(PBI);
  262. Value *NC;
  263. if (Idx == 0)
  264. // Case 2, use parallel or.
  265. NC = Builder.CreateOr(PC, CC);
  266. else
  267. // Case 1, use parallel and.
  268. NC = Builder.CreateAnd(PC, CC);
  269. PBI->replaceUsesOfWith(CC, NC);
  270. PC = NC;
  271. if (CB == LastCondBlock)
  272. Iteration = false;
  273. // Remove internal conditional branches.
  274. CB->dropAllReferences();
  275. // make CB unreachable and let downstream to delete the block.
  276. new UnreachableInst(CB->getContext(), CB);
  277. } while (Iteration);
  278. LLVM_DEBUG(dbgs() << "Use parallel and/or in:\n" << *FirstCondBlock);
  279. return true;
  280. }
  281. /// Compare blocks from two if-regions, where \param Head2 is the entry of the
  282. /// 2nd if-region. \param Block1 is a block in the 1st if-region to compare.
  283. /// \param Block2 is a block in the 2nd if-region to compare. \returns true if
  284. /// Block1 and Block2 have identical instructions and do not have
  285. /// memory reference alias with Head2.
  286. bool FlattenCFGOpt::CompareIfRegionBlock(BasicBlock *Block1, BasicBlock *Block2,
  287. BasicBlock *Head2) {
  288. Instruction *PTI2 = Head2->getTerminator();
  289. Instruction *PBI2 = &Head2->front();
  290. // Check whether instructions in Block1 and Block2 are identical
  291. // and do not alias with instructions in Head2.
  292. BasicBlock::iterator iter1 = Block1->begin();
  293. BasicBlock::iterator end1 = Block1->getTerminator()->getIterator();
  294. BasicBlock::iterator iter2 = Block2->begin();
  295. BasicBlock::iterator end2 = Block2->getTerminator()->getIterator();
  296. while (true) {
  297. if (iter1 == end1) {
  298. if (iter2 != end2)
  299. return false;
  300. break;
  301. }
  302. if (!iter1->isIdenticalTo(&*iter2))
  303. return false;
  304. // Illegal to remove instructions with side effects except
  305. // non-volatile stores.
  306. if (iter1->mayHaveSideEffects()) {
  307. Instruction *CurI = &*iter1;
  308. StoreInst *SI = dyn_cast<StoreInst>(CurI);
  309. if (!SI || SI->isVolatile())
  310. return false;
  311. }
  312. // For simplicity and speed, data dependency check can be
  313. // avoided if read from memory doesn't exist.
  314. if (iter1->mayReadFromMemory())
  315. return false;
  316. if (iter1->mayWriteToMemory()) {
  317. for (BasicBlock::iterator BI(PBI2), BE(PTI2); BI != BE; ++BI) {
  318. if (BI->mayReadFromMemory() || BI->mayWriteToMemory()) {
  319. // Check alias with Head2.
  320. if (!AA || !AA->isNoAlias(&*iter1, &*BI))
  321. return false;
  322. }
  323. }
  324. }
  325. ++iter1;
  326. ++iter2;
  327. }
  328. return true;
  329. }
  330. /// Check whether \param BB is the merge block of a if-region. If yes, check
  331. /// whether there exists an adjacent if-region upstream, the two if-regions
  332. /// contain identical instructions and can be legally merged. \returns true if
  333. /// the two if-regions are merged.
  334. ///
  335. /// From:
  336. /// if (a)
  337. /// statement;
  338. /// if (b)
  339. /// statement;
  340. ///
  341. /// To:
  342. /// if (a || b)
  343. /// statement;
  344. ///
  345. ///
  346. /// And from:
  347. /// if (a)
  348. /// ;
  349. /// else
  350. /// statement;
  351. /// if (b)
  352. /// ;
  353. /// else
  354. /// statement;
  355. ///
  356. /// To:
  357. /// if (a && b)
  358. /// ;
  359. /// else
  360. /// statement;
  361. ///
  362. /// We always take the form of the first if-region. This means that if the
  363. /// statement in the first if-region, is in the "then-path", while in the second
  364. /// if-region it is in the "else-path", then we convert the second to the first
  365. /// form, by inverting the condition and the branch successors. The same
  366. /// approach goes for the opposite case.
  367. bool FlattenCFGOpt::MergeIfRegion(BasicBlock *BB, IRBuilder<> &Builder) {
  368. BasicBlock *IfTrue2, *IfFalse2;
  369. BranchInst *DomBI2 = GetIfCondition(BB, IfTrue2, IfFalse2);
  370. if (!DomBI2)
  371. return false;
  372. Instruction *CInst2 = dyn_cast<Instruction>(DomBI2->getCondition());
  373. if (!CInst2)
  374. return false;
  375. BasicBlock *SecondEntryBlock = CInst2->getParent();
  376. if (SecondEntryBlock->hasAddressTaken())
  377. return false;
  378. BasicBlock *IfTrue1, *IfFalse1;
  379. BranchInst *DomBI1 = GetIfCondition(SecondEntryBlock, IfTrue1, IfFalse1);
  380. if (!DomBI1)
  381. return false;
  382. Instruction *CInst1 = dyn_cast<Instruction>(DomBI1->getCondition());
  383. if (!CInst1)
  384. return false;
  385. BasicBlock *FirstEntryBlock = CInst1->getParent();
  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->getInstList().pop_back();
  427. FirstEntryBlock->getInstList()
  428. .splice(FirstEntryBlock->end(), SecondEntryBlock->getInstList());
  429. BranchInst *PBI = cast<BranchInst>(FirstEntryBlock->getTerminator());
  430. assert(PBI->getCondition() == CInst2);
  431. BasicBlock *SaveInsertBB = Builder.GetInsertBlock();
  432. BasicBlock::iterator SaveInsertPt = Builder.GetInsertPoint();
  433. Builder.SetInsertPoint(PBI);
  434. if (InvertCond2) {
  435. // If this is a "cmp" instruction, only used for branching (and nowhere
  436. // else), then we can simply invert the predicate.
  437. auto Cmp2 = dyn_cast<CmpInst>(CInst2);
  438. if (Cmp2 && Cmp2->hasOneUse())
  439. Cmp2->setPredicate(Cmp2->getInversePredicate());
  440. else
  441. CInst2 = cast<Instruction>(Builder.CreateNot(CInst2));
  442. PBI->swapSuccessors();
  443. }
  444. Value *NC = Builder.CreateBinOp(CombineOp, CInst1, CInst2);
  445. PBI->replaceUsesOfWith(CInst2, NC);
  446. Builder.SetInsertPoint(SaveInsertBB, SaveInsertPt);
  447. // Handle PHI node to replace its predecessors to FirstEntryBlock.
  448. for (BasicBlock *Succ : successors(PBI)) {
  449. for (PHINode &Phi : Succ->phis()) {
  450. for (unsigned i = 0, e = Phi.getNumIncomingValues(); i != e; ++i) {
  451. if (Phi.getIncomingBlock(i) == SecondEntryBlock)
  452. Phi.setIncomingBlock(i, FirstEntryBlock);
  453. }
  454. }
  455. }
  456. // Remove IfTrue1
  457. if (IfTrue1 != FirstEntryBlock) {
  458. IfTrue1->dropAllReferences();
  459. IfTrue1->eraseFromParent();
  460. }
  461. // Remove IfFalse1
  462. if (IfFalse1 != FirstEntryBlock) {
  463. IfFalse1->dropAllReferences();
  464. IfFalse1->eraseFromParent();
  465. }
  466. // Remove \param SecondEntryBlock
  467. SecondEntryBlock->dropAllReferences();
  468. SecondEntryBlock->eraseFromParent();
  469. LLVM_DEBUG(dbgs() << "If conditions merged into:\n" << *FirstEntryBlock);
  470. return true;
  471. }
  472. bool FlattenCFGOpt::run(BasicBlock *BB) {
  473. assert(BB && BB->getParent() && "Block not embedded in function!");
  474. assert(BB->getTerminator() && "Degenerate basic block encountered!");
  475. IRBuilder<> Builder(BB);
  476. if (FlattenParallelAndOr(BB, Builder) || MergeIfRegion(BB, Builder))
  477. return true;
  478. return false;
  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. bool llvm::FlattenCFG(BasicBlock *BB, AAResults *AA) {
  484. return FlattenCFGOpt(AA).run(BB);
  485. }