more space; NFC
[oota-llvm.git] / lib / Transforms / Utils / FlattenCFG.cpp
1 //===- 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
14 #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"
20 #include "llvm/Support/raw_ostream.h"
21 #include "llvm/Transforms/Utils/BasicBlockUtils.h"
22 using namespace llvm;
23
24 #define DEBUG_TYPE "flattencfg"
25
26 namespace {
27 class FlattenCFGOpt {
28   AliasAnalysis *AA;
29   /// \brief Use parallel-and or parallel-or to generate conditions for
30   /// conditional branches.
31   bool FlattenParallelAndOr(BasicBlock *BB, IRBuilder<> &Builder);
32   /// \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.
35   bool MergeIfRegion(BasicBlock *BB, IRBuilder<> &Builder);
36   /// \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
44 public:
45   FlattenCFGOpt(AliasAnalysis *AA) : AA(AA) {}
46   bool run(BasicBlock *BB);
47 };
48 }
49
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 ///
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
129   BasicBlock *LastCondBlock = nullptr;
130   BasicBlock *FirstCondBlock = nullptr;
131   BasicBlock *UnCondBlock = nullptr;
132   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.
178       for (BasicBlock::iterator BI = Pred->begin(), BE = PBI->getIterator();
179            BI != BE;) {
180         Instruction *CI = &*BI++;
181         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;
240     for (;CurrBlock != FirstCondBlock;
241           CurrBlock = CurrBlock->getSinglePredecessor()) {
242       BranchInst *BI = dyn_cast<BranchInst>(CurrBlock->getTerminator());
243       CmpInst *CI = dyn_cast<CmpInst>(BI->getCondition());
244       if (!CI)
245         continue;
246
247       CmpInst::Predicate Predicate = CI->getPredicate();
248       // Canonicalize icmp_ne -> icmp_eq, fcmp_one -> fcmp_oeq
249       if ((Predicate == CmpInst::ICMP_NE) || (Predicate == CmpInst::FCMP_ONE)) {
250         CI->setPredicate(ICmpInst::getInversePredicate(Predicate));
251         BI->swapSuccessors();
252         EverChanged = true;
253       }
254     }
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;
271   IRBuilder<>::InsertPointGuard Guard(Builder);
272   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
302   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 ///
313 bool FlattenCFGOpt::CompareIfRegionBlock(BasicBlock *Head1, BasicBlock *Head2,
314                                          BasicBlock *Block1,
315                                          BasicBlock *Block2) {
316   TerminatorInst *PTI2 = Head2->getTerminator();
317   Instruction *PBI2 = &Head2->front();
318
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();
329   BasicBlock::iterator end1 = Block1->getTerminator()->getIterator();
330   BasicBlock::iterator iter2 = Block2->begin();
331   BasicBlock::iterator end2 = Block2->getTerminator()->getIterator();
332
333   while (1) {
334     if (iter1 == end1) {
335       if (iter2 != end2)
336         return false;
337       break;
338     }
339
340     if (!iter1->isIdenticalTo(&*iter2))
341       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()) {
358       for (BasicBlock::iterator BI(PBI2), BE(PTI2); BI != BE; ++BI) {
359         if (BI->mayReadFromMemory() || BI->mayWriteToMemory()) {
360           // Check alias with Head2.
361           if (!AA || AA->alias(&*iter1, &*BI))
362             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
375 /// contain identical instructions and can be legally merged.  \returns true if
376 /// 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 ///
388 bool FlattenCFGOpt::MergeIfRegion(BasicBlock *BB, IRBuilder<> &Builder) {
389   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();
414   Instruction *PBI2 = &SecondEntryBlock->front();
415
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.
426   for (BasicBlock::iterator BI(PBI2), BE(PTI2); BI != BE; ++BI) {
427     Instruction *CI = &*BI;
428     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
465 bool FlattenCFGOpt::run(BasicBlock *BB) {
466   bool Changed = false;
467   assert(BB && BB->getParent() && "Block not embedded in function!");
468   assert(BB->getTerminator() && "Degenerate basic block encountered!");
469
470   IRBuilder<> Builder(BB);
471
472   if (FlattenParallelAndOr(BB, Builder))
473     return true;
474
475   if (MergeIfRegion(BB, Builder))
476     return true;
477
478   return Changed;
479 }
480
481 /// FlattenCFG - This function is used to flatten a CFG.  For
482 /// example, it uses parallel-and and parallel-or mode to collapse
483 //  if-conditions and merge if-regions with identical statements.
484 ///
485 bool llvm::FlattenCFG(BasicBlock *BB, AliasAnalysis *AA) {
486   return FlattenCFGOpt(AA).run(BB);
487 }