blob: 7b96fbb11a142143375ad2ccc9d87cd3eab2ee3c [file] [log] [blame]
Tom Stellardaa664d92013-08-06 02:43:45 +00001//===- FlatternCFG.cpp - Code to perform CFG flattening ---------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// Reduce conditional branches in CFG.
11//
12//===----------------------------------------------------------------------===//
13
Tom Stellardaa664d92013-08-06 02:43:45 +000014#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"
Serge Pavlov71044cb2013-08-06 08:44:18 +000020#include "llvm/Support/raw_ostream.h"
Tom Stellardaa664d92013-08-06 02:43:45 +000021#include "llvm/Transforms/Utils/BasicBlockUtils.h"
22using namespace llvm;
23
Chandler Carruth964daaa2014-04-22 02:55:47 +000024#define DEBUG_TYPE "flattencfg"
25
Tom Stellardaa664d92013-08-06 02:43:45 +000026namespace {
27class FlattenCFGOpt {
28 AliasAnalysis *AA;
29 /// \brief Use parallel-and or parallel-or to generate conditions for
30 /// conditional branches.
Justin Bogner843fb202015-12-15 19:40:57 +000031 bool FlattenParallelAndOr(BasicBlock *BB, IRBuilder<> &Builder);
Tom Stellardaa664d92013-08-06 02:43:45 +000032 /// \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.
Justin Bogner843fb202015-12-15 19:40:57 +000035 bool MergeIfRegion(BasicBlock *BB, IRBuilder<> &Builder);
Tom Stellardaa664d92013-08-06 02:43:45 +000036 /// \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);
43
44public:
45 FlattenCFGOpt(AliasAnalysis *AA) : AA(AA) {}
46 bool run(BasicBlock *BB);
47};
Alexander Kornienkof00654e2015-06-23 09:49:53 +000048}
Tom Stellardaa664d92013-08-06 02:43:45 +000049
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
52/// true on success.
53///
54/// Before:
55/// ......
56/// %cmp10 = fcmp une float %tmp1, %tmp2
57/// br i1 %cmp1, label %if.then, label %lor.rhs
58///
59/// lor.rhs:
60/// ......
61/// %cmp11 = fcmp une float %tmp3, %tmp4
62/// br i1 %cmp11, label %if.then, label %ifend
63///
64/// if.end: // the merge block
65/// ......
66///
67/// if.then: // has two predecessors, both of them contains conditional branch.
68/// ......
69/// br label %if.end;
70///
71/// After:
72/// ......
73/// %cmp10 = fcmp une float %tmp1, %tmp2
74/// ......
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
78///
79/// if.end:
80/// ......
81///
82/// if.then:
83/// ......
84/// br label %if.end;
85///
86/// Current implementation handles two cases.
87/// Case 1: \param BB is on the else-path.
88///
89/// BB1
90/// / |
91/// BB2 |
92/// / \ |
93/// BB3 \ | where, BB1, BB2 contain conditional branches.
94/// \ | / BB3 contains unconditional branch.
95/// \ | / BB4 corresponds to \param BB which is also the merge.
96/// BB => BB4
97///
98///
99/// Corresponding source code:
100///
101/// if (a == b && c == d)
102/// statement; // BB3
103///
104/// Case 2: \param BB BB is on the then-path.
105///
106/// BB1
107/// / |
108/// | BB2
109/// \ / | where BB1, BB2 contain conditional branches.
110/// BB => BB3 | BB3 contains unconditiona branch and corresponds
111/// \ / to \param BB. BB4 is the merge.
112/// BB4
113///
114/// Corresponding source code:
115///
116/// if (a == b || c == d)
117/// statement; // BB3
118///
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.
123///
Justin Bogner843fb202015-12-15 19:40:57 +0000124bool FlattenCFGOpt::FlattenParallelAndOr(BasicBlock *BB, IRBuilder<> &Builder) {
Tom Stellardaa664d92013-08-06 02:43:45 +0000125 PHINode *PHI = dyn_cast<PHINode>(BB->begin());
126 if (PHI)
127 return false; // For simplicity, avoid cases containing PHI nodes.
128
Craig Topperf40110f2014-04-25 05:29:35 +0000129 BasicBlock *LastCondBlock = nullptr;
130 BasicBlock *FirstCondBlock = nullptr;
131 BasicBlock *UnCondBlock = nullptr;
Tom Stellardaa664d92013-08-06 02:43:45 +0000132 int Idx = -1;
133
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();
137 PI != PE; ++PI) {
138 BasicBlock *Pred = *PI;
139 BranchInst *PBI = dyn_cast<BranchInst>(Pred->getTerminator());
140
141 // All predecessors should terminate with a branch.
142 if (!PBI)
143 return false;
144
145 BasicBlock *PP = Pred->getSinglePredecessor();
146
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())
155 return false;
156
157 UnCondBlock = Pred;
158 continue;
159 }
160
161 // Only conditional branches are allowed beyond this point.
162 assert(PBI->isConditional());
163
164 // Condition's unique use should be the branch instruction.
165 Value *PC = PBI->getCondition();
166 if (!PC || !PC->hasOneUse())
167 return false;
168
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())
174 return false;
175
176 // Instructions in the internal condition blocks should be safe
177 // to hoist up.
Duncan P. N. Exon Smith5b4c8372015-10-13 02:39:05 +0000178 for (BasicBlock::iterator BI = Pred->begin(), BE = PBI->getIterator();
179 BI != BE;) {
180 Instruction *CI = &*BI++;
Tom Stellardaa664d92013-08-06 02:43:45 +0000181 if (isa<PHINode>(CI) || !isSafeToSpeculativelyExecute(CI))
182 return false;
183 }
184 } else {
185 // This is the condition block to be merged into, e.g. BB1 in
186 // both cases.
187 if (FirstCondBlock)
188 return false;
189 FirstCondBlock = Pred;
190 }
191
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;
198
199 if (Idx == -1)
200 Idx = CIdx;
201 else if (CIdx != Idx)
202 return false;
203
204 // PS is the successor which is not BB. Check successors to identify
205 // the last conditional branch.
206 if (Preds.count(PS) == 0) {
207 // Case 2.
208 LastCondBlock = Pred;
209 } else {
210 // Case 1
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;
215 }
216 }
217 }
218
219 if (!FirstCondBlock || !LastCondBlock || (FirstCondBlock == LastCondBlock))
220 return false;
221
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());
227
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))
235 return false;
236
237 // Do branch inversion.
238 BasicBlock *CurrBlock = LastCondBlock;
239 bool EverChanged = false;
Jan Vesely0cd3ec62014-08-13 20:31:53 +0000240 for (;CurrBlock != FirstCondBlock;
241 CurrBlock = CurrBlock->getSinglePredecessor()) {
Tom Stellardaa664d92013-08-06 02:43:45 +0000242 BranchInst *BI = dyn_cast<BranchInst>(CurrBlock->getTerminator());
243 CmpInst *CI = dyn_cast<CmpInst>(BI->getCondition());
Jan Vesely0cd3ec62014-08-13 20:31:53 +0000244 if (!CI)
245 continue;
246
Tom Stellardaa664d92013-08-06 02:43:45 +0000247 CmpInst::Predicate Predicate = CI->getPredicate();
Alp Tokercb402912014-01-24 17:20:08 +0000248 // Canonicalize icmp_ne -> icmp_eq, fcmp_one -> fcmp_oeq
Tom Stellardaa664d92013-08-06 02:43:45 +0000249 if ((Predicate == CmpInst::ICMP_NE) || (Predicate == CmpInst::FCMP_ONE)) {
250 CI->setPredicate(ICmpInst::getInversePredicate(Predicate));
251 BI->swapSuccessors();
252 EverChanged = true;
253 }
Tom Stellardaa664d92013-08-06 02:43:45 +0000254 }
255 return EverChanged;
256 }
257
258 // PS1 must have a conditional branch.
259 if (!PBI1 || !PBI1->isUnconditional())
260 return false;
261
262 // PS2 should not contain PHI node.
263 PHI = dyn_cast<PHINode>(PS2->begin());
264 if (PHI)
265 return false;
266
267 // Do the transformation.
268 BasicBlock *CB;
269 BranchInst *PBI = dyn_cast<BranchInst>(FirstCondBlock->getTerminator());
270 bool Iteration = true;
Benjamin Kramer6e931522013-09-30 15:40:17 +0000271 IRBuilder<>::InsertPointGuard Guard(Builder);
Tom Stellardaa664d92013-08-06 02:43:45 +0000272 Value *PC = PBI->getCondition();
273
274 do {
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();
282 // Merge conditions.
283 Builder.SetInsertPoint(PBI);
284 Value *NC;
285 if (Idx == 0)
286 // Case 2, use parallel or.
287 NC = Builder.CreateOr(PC, CC);
288 else
289 // Case 1, use parallel and.
290 NC = Builder.CreateAnd(PC, CC);
291
292 PBI->replaceUsesOfWith(CC, NC);
293 PC = NC;
294 if (CB == LastCondBlock)
295 Iteration = false;
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);
300 } while (Iteration);
301
Tom Stellardaa664d92013-08-06 02:43:45 +0000302 DEBUG(dbgs() << "Use parallel and/or in:\n" << *FirstCondBlock);
303 return true;
304}
305
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.
312///
313bool FlattenCFGOpt::CompareIfRegionBlock(BasicBlock *Head1, BasicBlock *Head2,
314 BasicBlock *Block1,
315 BasicBlock *Block2) {
316 TerminatorInst *PTI2 = Head2->getTerminator();
Duncan P. N. Exon Smith5b4c8372015-10-13 02:39:05 +0000317 Instruction *PBI2 = &Head2->front();
Tom Stellardaa664d92013-08-06 02:43:45 +0000318
319 bool eq1 = (Block1 == Head1);
320 bool eq2 = (Block2 == Head2);
321 if (eq1 || eq2) {
322 // An empty then-path or else-path.
323 return (eq1 == eq2);
324 }
325
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();
Duncan P. N. Exon Smith5b4c8372015-10-13 02:39:05 +0000329 BasicBlock::iterator end1 = Block1->getTerminator()->getIterator();
Tom Stellardaa664d92013-08-06 02:43:45 +0000330 BasicBlock::iterator iter2 = Block2->begin();
Duncan P. N. Exon Smith5b4c8372015-10-13 02:39:05 +0000331 BasicBlock::iterator end2 = Block2->getTerminator()->getIterator();
Tom Stellardaa664d92013-08-06 02:43:45 +0000332
333 while (1) {
334 if (iter1 == end1) {
335 if (iter2 != end2)
336 return false;
337 break;
338 }
339
Duncan P. N. Exon Smith5b4c8372015-10-13 02:39:05 +0000340 if (!iter1->isIdenticalTo(&*iter2))
Tom Stellardaa664d92013-08-06 02:43:45 +0000341 return false;
342
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())
349 return false;
350 }
351
352 // For simplicity and speed, data dependency check can be
353 // avoided if read from memory doesn't exist.
354 if (iter1->mayReadFromMemory())
355 return false;
356
357 if (iter1->mayWriteToMemory()) {
Duncan P. N. Exon Smith5b4c8372015-10-13 02:39:05 +0000358 for (BasicBlock::iterator BI(PBI2), BE(PTI2); BI != BE; ++BI) {
Tom Stellardaa664d92013-08-06 02:43:45 +0000359 if (BI->mayReadFromMemory() || BI->mayWriteToMemory()) {
360 // Check alias with Head2.
Duncan P. N. Exon Smith5b4c8372015-10-13 02:39:05 +0000361 if (!AA || AA->alias(&*iter1, &*BI))
Tom Stellardaa664d92013-08-06 02:43:45 +0000362 return false;
363 }
364 }
365 }
366 ++iter1;
367 ++iter2;
368 }
369
370 return true;
371}
372
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
Robert Wilhelm042f10c2013-09-14 09:34:59 +0000375/// contain identical instructions and can be legally merged. \returns true if
Tom Stellardaa664d92013-08-06 02:43:45 +0000376/// the two if-regions are merged.
377///
378/// From:
379/// if (a)
380/// statement;
381/// if (b)
382/// statement;
383///
384/// To:
385/// if (a || b)
386/// statement;
387///
Justin Bogner843fb202015-12-15 19:40:57 +0000388bool FlattenCFGOpt::MergeIfRegion(BasicBlock *BB, IRBuilder<> &Builder) {
Tom Stellardaa664d92013-08-06 02:43:45 +0000389 BasicBlock *IfTrue2, *IfFalse2;
390 Value *IfCond2 = GetIfCondition(BB, IfTrue2, IfFalse2);
391 Instruction *CInst2 = dyn_cast_or_null<Instruction>(IfCond2);
392 if (!CInst2)
393 return false;
394
395 BasicBlock *SecondEntryBlock = CInst2->getParent();
396 if (SecondEntryBlock->hasAddressTaken())
397 return false;
398
399 BasicBlock *IfTrue1, *IfFalse1;
400 Value *IfCond1 = GetIfCondition(SecondEntryBlock, IfTrue1, IfFalse1);
401 Instruction *CInst1 = dyn_cast_or_null<Instruction>(IfCond1);
402 if (!CInst1)
403 return false;
404
405 BasicBlock *FirstEntryBlock = CInst1->getParent();
406
407 // Either then-path or else-path should be empty.
408 if ((IfTrue1 != FirstEntryBlock) && (IfFalse1 != FirstEntryBlock))
409 return false;
410 if ((IfTrue2 != SecondEntryBlock) && (IfFalse2 != SecondEntryBlock))
411 return false;
412
413 TerminatorInst *PTI2 = SecondEntryBlock->getTerminator();
Duncan P. N. Exon Smith5b4c8372015-10-13 02:39:05 +0000414 Instruction *PBI2 = &SecondEntryBlock->front();
Tom Stellardaa664d92013-08-06 02:43:45 +0000415
416 if (!CompareIfRegionBlock(FirstEntryBlock, SecondEntryBlock, IfTrue1,
417 IfTrue2))
418 return false;
419
420 if (!CompareIfRegionBlock(FirstEntryBlock, SecondEntryBlock, IfFalse1,
421 IfFalse2))
422 return false;
423
424 // Check whether \param SecondEntryBlock has side-effect and is safe to
425 // speculate.
Duncan P. N. Exon Smith5b4c8372015-10-13 02:39:05 +0000426 for (BasicBlock::iterator BI(PBI2), BE(PTI2); BI != BE; ++BI) {
427 Instruction *CI = &*BI;
Tom Stellardaa664d92013-08-06 02:43:45 +0000428 if (isa<PHINode>(CI) || CI->mayHaveSideEffects() ||
429 !isSafeToSpeculativelyExecute(CI))
430 return false;
431 }
432
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);
445
446 // Remove IfTrue1
447 if (IfTrue1 != FirstEntryBlock) {
448 IfTrue1->dropAllReferences();
449 IfTrue1->eraseFromParent();
450 }
451
452 // Remove IfFalse1
453 if (IfFalse1 != FirstEntryBlock) {
454 IfFalse1->dropAllReferences();
455 IfFalse1->eraseFromParent();
456 }
457
458 // Remove \param SecondEntryBlock
459 SecondEntryBlock->dropAllReferences();
460 SecondEntryBlock->eraseFromParent();
461 DEBUG(dbgs() << "If conditions merged into:\n" << *FirstEntryBlock);
462 return true;
463}
464
465bool FlattenCFGOpt::run(BasicBlock *BB) {
Tom Stellardaa664d92013-08-06 02:43:45 +0000466 assert(BB && BB->getParent() && "Block not embedded in function!");
467 assert(BB->getTerminator() && "Degenerate basic block encountered!");
468
469 IRBuilder<> Builder(BB);
470
Davide Italiano500929d2016-08-05 20:53:35 +0000471 if (FlattenParallelAndOr(BB, Builder) || MergeIfRegion(BB, Builder))
Tom Stellardaa664d92013-08-06 02:43:45 +0000472 return true;
Davide Italiano500929d2016-08-05 20:53:35 +0000473 return false;
Tom Stellardaa664d92013-08-06 02:43:45 +0000474}
475
476/// FlattenCFG - This function is used to flatten a CFG. For
477/// example, it uses parallel-and and parallel-or mode to collapse
478// if-conditions and merge if-regions with identical statements.
479///
480bool llvm::FlattenCFG(BasicBlock *BB, AliasAnalysis *AA) {
481 return FlattenCFGOpt(AA).run(BB);
482}