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