blob: cadc5921346dfeb81d5ef172327d613fe23584fb [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.
Craig Topperf40110f2014-04-25 05:29:35 +000031 bool FlattenParallelAndOr(BasicBlock *BB, IRBuilder<> &Builder,
32 Pass *P = nullptr);
Tom Stellardaa664d92013-08-06 02:43:45 +000033 /// \brief If \param BB is the merge block of an if-region, attempt to merge
34 /// the if-region with an adjacent if-region upstream if two if-regions
35 /// contain identical instructions.
Craig Topperf40110f2014-04-25 05:29:35 +000036 bool MergeIfRegion(BasicBlock *BB, IRBuilder<> &Builder, Pass *P = nullptr);
Tom Stellardaa664d92013-08-06 02:43:45 +000037 /// \brief Compare a pair of blocks: \p Block1 and \p Block2, which
38 /// are from two if-regions whose entry blocks are \p Head1 and \p
39 /// Head2. \returns true if \p Block1 and \p Block2 contain identical
40 /// instructions, and have no memory reference alias with \p Head2.
41 /// This is used as a legality check for merging if-regions.
42 bool CompareIfRegionBlock(BasicBlock *Head1, BasicBlock *Head2,
43 BasicBlock *Block1, BasicBlock *Block2);
44
45public:
46 FlattenCFGOpt(AliasAnalysis *AA) : AA(AA) {}
47 bool run(BasicBlock *BB);
48};
Alexander Kornienkof00654e2015-06-23 09:49:53 +000049}
Tom Stellardaa664d92013-08-06 02:43:45 +000050
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 %cmp1, 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: \param 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 \param 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: \param BB 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 \param 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, \param BB is the common successor of conditional branches.
121/// In Case 1, \param BB (BB4) has an unconditional branch (BB3) as
122/// its predecessor. In Case 2, \param BB (BB3) only has conditional branches
123/// as its predecessors.
124///
125bool FlattenCFGOpt::FlattenParallelAndOr(BasicBlock *BB, IRBuilder<> &Builder,
126 Pass *P) {
127 PHINode *PHI = dyn_cast<PHINode>(BB->begin());
128 if (PHI)
129 return false; // For simplicity, avoid cases containing PHI nodes.
130
Craig Topperf40110f2014-04-25 05:29:35 +0000131 BasicBlock *LastCondBlock = nullptr;
132 BasicBlock *FirstCondBlock = nullptr;
133 BasicBlock *UnCondBlock = nullptr;
Tom Stellardaa664d92013-08-06 02:43:45 +0000134 int Idx = -1;
135
136 // Check predecessors of \param BB.
137 SmallPtrSet<BasicBlock *, 16> Preds(pred_begin(BB), pred_end(BB));
138 for (SmallPtrSetIterator<BasicBlock *> PI = Preds.begin(), PE = Preds.end();
139 PI != PE; ++PI) {
140 BasicBlock *Pred = *PI;
141 BranchInst *PBI = dyn_cast<BranchInst>(Pred->getTerminator());
142
143 // All predecessors should terminate with a branch.
144 if (!PBI)
145 return false;
146
147 BasicBlock *PP = Pred->getSinglePredecessor();
148
149 if (PBI->isUnconditional()) {
150 // Case 1: Pred (BB3) is an unconditional block, it should
151 // have a single predecessor (BB2) that is also a predecessor
152 // of \param BB (BB4) and should not have address-taken.
153 // There should exist only one such unconditional
154 // branch among the predecessors.
155 if (UnCondBlock || !PP || (Preds.count(PP) == 0) ||
156 Pred->hasAddressTaken())
157 return false;
158
159 UnCondBlock = Pred;
160 continue;
161 }
162
163 // Only conditional branches are allowed beyond this point.
164 assert(PBI->isConditional());
165
166 // Condition's unique use should be the branch instruction.
167 Value *PC = PBI->getCondition();
168 if (!PC || !PC->hasOneUse())
169 return false;
170
171 if (PP && Preds.count(PP)) {
172 // These are internal condition blocks to be merged from, e.g.,
173 // BB2 in both cases.
174 // Should not be address-taken.
175 if (Pred->hasAddressTaken())
176 return false;
177
178 // Instructions in the internal condition blocks should be safe
179 // to hoist up.
Duncan P. N. Exon Smith5b4c8372015-10-13 02:39:05 +0000180 for (BasicBlock::iterator BI = Pred->begin(), BE = PBI->getIterator();
181 BI != BE;) {
182 Instruction *CI = &*BI++;
Tom Stellardaa664d92013-08-06 02:43:45 +0000183 if (isa<PHINode>(CI) || !isSafeToSpeculativelyExecute(CI))
184 return false;
185 }
186 } else {
187 // This is the condition block to be merged into, e.g. BB1 in
188 // both cases.
189 if (FirstCondBlock)
190 return false;
191 FirstCondBlock = Pred;
192 }
193
194 // Find whether BB is uniformly on the true (or false) path
195 // for all of its predecessors.
196 BasicBlock *PS1 = PBI->getSuccessor(0);
197 BasicBlock *PS2 = PBI->getSuccessor(1);
198 BasicBlock *PS = (PS1 == BB) ? PS2 : PS1;
199 int CIdx = (PS1 == BB) ? 0 : 1;
200
201 if (Idx == -1)
202 Idx = CIdx;
203 else if (CIdx != Idx)
204 return false;
205
206 // PS is the successor which is not BB. Check successors to identify
207 // the last conditional branch.
208 if (Preds.count(PS) == 0) {
209 // Case 2.
210 LastCondBlock = Pred;
211 } else {
212 // Case 1
213 BranchInst *BPS = dyn_cast<BranchInst>(PS->getTerminator());
214 if (BPS && BPS->isUnconditional()) {
215 // Case 1: PS(BB3) should be an unconditional branch.
216 LastCondBlock = Pred;
217 }
218 }
219 }
220
221 if (!FirstCondBlock || !LastCondBlock || (FirstCondBlock == LastCondBlock))
222 return false;
223
224 TerminatorInst *TBB = LastCondBlock->getTerminator();
225 BasicBlock *PS1 = TBB->getSuccessor(0);
226 BasicBlock *PS2 = TBB->getSuccessor(1);
227 BranchInst *PBI1 = dyn_cast<BranchInst>(PS1->getTerminator());
228 BranchInst *PBI2 = dyn_cast<BranchInst>(PS2->getTerminator());
229
230 // If PS1 does not jump into PS2, but PS2 jumps into PS1,
231 // attempt branch inversion.
232 if (!PBI1 || !PBI1->isUnconditional() ||
233 (PS1->getTerminator()->getSuccessor(0) != PS2)) {
234 // Check whether PS2 jumps into PS1.
235 if (!PBI2 || !PBI2->isUnconditional() ||
236 (PS2->getTerminator()->getSuccessor(0) != PS1))
237 return false;
238
239 // Do branch inversion.
240 BasicBlock *CurrBlock = LastCondBlock;
241 bool EverChanged = false;
Jan Vesely0cd3ec62014-08-13 20:31:53 +0000242 for (;CurrBlock != FirstCondBlock;
243 CurrBlock = CurrBlock->getSinglePredecessor()) {
Tom Stellardaa664d92013-08-06 02:43:45 +0000244 BranchInst *BI = dyn_cast<BranchInst>(CurrBlock->getTerminator());
245 CmpInst *CI = dyn_cast<CmpInst>(BI->getCondition());
Jan Vesely0cd3ec62014-08-13 20:31:53 +0000246 if (!CI)
247 continue;
248
Tom Stellardaa664d92013-08-06 02:43:45 +0000249 CmpInst::Predicate Predicate = CI->getPredicate();
Alp Tokercb402912014-01-24 17:20:08 +0000250 // Canonicalize icmp_ne -> icmp_eq, fcmp_one -> fcmp_oeq
Tom Stellardaa664d92013-08-06 02:43:45 +0000251 if ((Predicate == CmpInst::ICMP_NE) || (Predicate == CmpInst::FCMP_ONE)) {
252 CI->setPredicate(ICmpInst::getInversePredicate(Predicate));
253 BI->swapSuccessors();
254 EverChanged = true;
255 }
Tom Stellardaa664d92013-08-06 02:43:45 +0000256 }
257 return EverChanged;
258 }
259
260 // PS1 must have a conditional branch.
261 if (!PBI1 || !PBI1->isUnconditional())
262 return false;
263
264 // PS2 should not contain PHI node.
265 PHI = dyn_cast<PHINode>(PS2->begin());
266 if (PHI)
267 return false;
268
269 // Do the transformation.
270 BasicBlock *CB;
271 BranchInst *PBI = dyn_cast<BranchInst>(FirstCondBlock->getTerminator());
272 bool Iteration = true;
Benjamin Kramer6e931522013-09-30 15:40:17 +0000273 IRBuilder<>::InsertPointGuard Guard(Builder);
Tom Stellardaa664d92013-08-06 02:43:45 +0000274 Value *PC = PBI->getCondition();
275
276 do {
277 CB = PBI->getSuccessor(1 - Idx);
278 // Delete the conditional branch.
279 FirstCondBlock->getInstList().pop_back();
280 FirstCondBlock->getInstList()
281 .splice(FirstCondBlock->end(), CB->getInstList());
282 PBI = cast<BranchInst>(FirstCondBlock->getTerminator());
283 Value *CC = PBI->getCondition();
284 // Merge conditions.
285 Builder.SetInsertPoint(PBI);
286 Value *NC;
287 if (Idx == 0)
288 // Case 2, use parallel or.
289 NC = Builder.CreateOr(PC, CC);
290 else
291 // Case 1, use parallel and.
292 NC = Builder.CreateAnd(PC, CC);
293
294 PBI->replaceUsesOfWith(CC, NC);
295 PC = NC;
296 if (CB == LastCondBlock)
297 Iteration = false;
298 // Remove internal conditional branches.
299 CB->dropAllReferences();
300 // make CB unreachable and let downstream to delete the block.
301 new UnreachableInst(CB->getContext(), CB);
302 } while (Iteration);
303
Tom Stellardaa664d92013-08-06 02:43:45 +0000304 DEBUG(dbgs() << "Use parallel and/or in:\n" << *FirstCondBlock);
305 return true;
306}
307
308/// Compare blocks from two if-regions, where \param Head1 is the entry of the
309/// 1st if-region. \param Head2 is the entry of the 2nd if-region. \param
310/// Block1 is a block in the 1st if-region to compare. \param Block2 is a block
311// in the 2nd if-region to compare. \returns true if \param Block1 and \param
312/// Block2 have identical instructions and do not have memory reference alias
313/// with \param Head2.
314///
315bool FlattenCFGOpt::CompareIfRegionBlock(BasicBlock *Head1, BasicBlock *Head2,
316 BasicBlock *Block1,
317 BasicBlock *Block2) {
318 TerminatorInst *PTI2 = Head2->getTerminator();
Duncan P. N. Exon Smith5b4c8372015-10-13 02:39:05 +0000319 Instruction *PBI2 = &Head2->front();
Tom Stellardaa664d92013-08-06 02:43:45 +0000320
321 bool eq1 = (Block1 == Head1);
322 bool eq2 = (Block2 == Head2);
323 if (eq1 || eq2) {
324 // An empty then-path or else-path.
325 return (eq1 == eq2);
326 }
327
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();
Duncan P. N. Exon Smith5b4c8372015-10-13 02:39:05 +0000331 BasicBlock::iterator end1 = Block1->getTerminator()->getIterator();
Tom Stellardaa664d92013-08-06 02:43:45 +0000332 BasicBlock::iterator iter2 = Block2->begin();
Duncan P. N. Exon Smith5b4c8372015-10-13 02:39:05 +0000333 BasicBlock::iterator end2 = Block2->getTerminator()->getIterator();
Tom Stellardaa664d92013-08-06 02:43:45 +0000334
335 while (1) {
336 if (iter1 == end1) {
337 if (iter2 != end2)
338 return false;
339 break;
340 }
341
Duncan P. N. Exon Smith5b4c8372015-10-13 02:39:05 +0000342 if (!iter1->isIdenticalTo(&*iter2))
Tom Stellardaa664d92013-08-06 02:43:45 +0000343 return false;
344
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;
352 }
353
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;
358
359 if (iter1->mayWriteToMemory()) {
Duncan P. N. Exon Smith5b4c8372015-10-13 02:39:05 +0000360 for (BasicBlock::iterator BI(PBI2), BE(PTI2); BI != BE; ++BI) {
Tom Stellardaa664d92013-08-06 02:43:45 +0000361 if (BI->mayReadFromMemory() || BI->mayWriteToMemory()) {
362 // Check alias with Head2.
Duncan P. N. Exon Smith5b4c8372015-10-13 02:39:05 +0000363 if (!AA || AA->alias(&*iter1, &*BI))
Tom Stellardaa664d92013-08-06 02:43:45 +0000364 return false;
365 }
366 }
367 }
368 ++iter1;
369 ++iter2;
370 }
371
372 return true;
373}
374
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
Robert Wilhelm042f10c2013-09-14 09:34:59 +0000377/// contain identical instructions and can be legally merged. \returns true if
Tom Stellardaa664d92013-08-06 02:43:45 +0000378/// the two if-regions are merged.
379///
380/// From:
381/// if (a)
382/// statement;
383/// if (b)
384/// statement;
385///
386/// To:
387/// if (a || b)
388/// statement;
389///
390bool FlattenCFGOpt::MergeIfRegion(BasicBlock *BB, IRBuilder<> &Builder,
391 Pass *P) {
392 BasicBlock *IfTrue2, *IfFalse2;
393 Value *IfCond2 = GetIfCondition(BB, IfTrue2, IfFalse2);
394 Instruction *CInst2 = dyn_cast_or_null<Instruction>(IfCond2);
395 if (!CInst2)
396 return false;
397
398 BasicBlock *SecondEntryBlock = CInst2->getParent();
399 if (SecondEntryBlock->hasAddressTaken())
400 return false;
401
402 BasicBlock *IfTrue1, *IfFalse1;
403 Value *IfCond1 = GetIfCondition(SecondEntryBlock, IfTrue1, IfFalse1);
404 Instruction *CInst1 = dyn_cast_or_null<Instruction>(IfCond1);
405 if (!CInst1)
406 return false;
407
408 BasicBlock *FirstEntryBlock = CInst1->getParent();
409
410 // Either then-path or else-path should be empty.
411 if ((IfTrue1 != FirstEntryBlock) && (IfFalse1 != FirstEntryBlock))
412 return false;
413 if ((IfTrue2 != SecondEntryBlock) && (IfFalse2 != SecondEntryBlock))
414 return false;
415
416 TerminatorInst *PTI2 = SecondEntryBlock->getTerminator();
Duncan P. N. Exon Smith5b4c8372015-10-13 02:39:05 +0000417 Instruction *PBI2 = &SecondEntryBlock->front();
Tom Stellardaa664d92013-08-06 02:43:45 +0000418
419 if (!CompareIfRegionBlock(FirstEntryBlock, SecondEntryBlock, IfTrue1,
420 IfTrue2))
421 return false;
422
423 if (!CompareIfRegionBlock(FirstEntryBlock, SecondEntryBlock, IfFalse1,
424 IfFalse2))
425 return false;
426
427 // Check whether \param SecondEntryBlock has side-effect and is safe to
428 // speculate.
Duncan P. N. Exon Smith5b4c8372015-10-13 02:39:05 +0000429 for (BasicBlock::iterator BI(PBI2), BE(PTI2); BI != BE; ++BI) {
430 Instruction *CI = &*BI;
Tom Stellardaa664d92013-08-06 02:43:45 +0000431 if (isa<PHINode>(CI) || CI->mayHaveSideEffects() ||
432 !isSafeToSpeculativelyExecute(CI))
433 return false;
434 }
435
436 // Merge \param SecondEntryBlock into \param FirstEntryBlock.
437 FirstEntryBlock->getInstList().pop_back();
438 FirstEntryBlock->getInstList()
439 .splice(FirstEntryBlock->end(), SecondEntryBlock->getInstList());
440 BranchInst *PBI = dyn_cast<BranchInst>(FirstEntryBlock->getTerminator());
441 Value *CC = PBI->getCondition();
442 BasicBlock *SaveInsertBB = Builder.GetInsertBlock();
443 BasicBlock::iterator SaveInsertPt = Builder.GetInsertPoint();
444 Builder.SetInsertPoint(PBI);
445 Value *NC = Builder.CreateOr(CInst1, CC);
446 PBI->replaceUsesOfWith(CC, NC);
447 Builder.SetInsertPoint(SaveInsertBB, SaveInsertPt);
448
449 // Remove IfTrue1
450 if (IfTrue1 != FirstEntryBlock) {
451 IfTrue1->dropAllReferences();
452 IfTrue1->eraseFromParent();
453 }
454
455 // Remove IfFalse1
456 if (IfFalse1 != FirstEntryBlock) {
457 IfFalse1->dropAllReferences();
458 IfFalse1->eraseFromParent();
459 }
460
461 // Remove \param SecondEntryBlock
462 SecondEntryBlock->dropAllReferences();
463 SecondEntryBlock->eraseFromParent();
464 DEBUG(dbgs() << "If conditions merged into:\n" << *FirstEntryBlock);
465 return true;
466}
467
468bool FlattenCFGOpt::run(BasicBlock *BB) {
469 bool Changed = false;
470 assert(BB && BB->getParent() && "Block not embedded in function!");
471 assert(BB->getTerminator() && "Degenerate basic block encountered!");
472
473 IRBuilder<> Builder(BB);
474
475 if (FlattenParallelAndOr(BB, Builder))
476 return true;
477
478 if (MergeIfRegion(BB, Builder))
479 return true;
480
481 return Changed;
482}
483
484/// FlattenCFG - This function is used to flatten a CFG. For
485/// example, it uses parallel-and and parallel-or mode to collapse
486// if-conditions and merge if-regions with identical statements.
487///
488bool llvm::FlattenCFG(BasicBlock *BB, AliasAnalysis *AA) {
489 return FlattenCFGOpt(AA).run(BB);
490}