Chris Lattner | 18f1609 | 2004-04-19 18:07:02 +0000 | [diff] [blame] | 1 | //===-- LoopUnswitch.cpp - Hoist loop-invariant conditionals in loop ------===// |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2 | // |
Chris Lattner | 18f1609 | 2004-04-19 18:07:02 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 4ee451d | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 7 | // |
Chris Lattner | 18f1609 | 2004-04-19 18:07:02 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This pass transforms loops that contain branches on loop-invariant conditions |
| 11 | // to have multiple loops. For example, it turns the left into the right code: |
| 12 | // |
| 13 | // for (...) if (lic) |
| 14 | // A for (...) |
| 15 | // if (lic) A; B; C |
| 16 | // B else |
| 17 | // C for (...) |
| 18 | // A; C |
| 19 | // |
| 20 | // This can increase the size of the code exponentially (doubling it every time |
| 21 | // a loop is unswitched) so we only unswitch if the resultant code will be |
| 22 | // smaller than a threshold. |
| 23 | // |
| 24 | // This pass expects LICM to be run before it to hoist invariant conditions out |
| 25 | // of the loop, to make the unswitching opportunity obvious. |
| 26 | // |
| 27 | //===----------------------------------------------------------------------===// |
| 28 | |
| 29 | #define DEBUG_TYPE "loop-unswitch" |
| 30 | #include "llvm/Transforms/Scalar.h" |
Chandler Carruth | d04a8d4 | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 31 | #include "llvm/ADT/STLExtras.h" |
| 32 | #include "llvm/ADT/SmallPtrSet.h" |
| 33 | #include "llvm/ADT/Statistic.h" |
| 34 | #include "llvm/Analysis/CodeMetrics.h" |
| 35 | #include "llvm/Analysis/Dominators.h" |
| 36 | #include "llvm/Analysis/InstructionSimplify.h" |
| 37 | #include "llvm/Analysis/LoopInfo.h" |
| 38 | #include "llvm/Analysis/LoopPass.h" |
| 39 | #include "llvm/Analysis/ScalarEvolution.h" |
Chandler Carruth | a5157e6 | 2013-01-21 13:04:33 +0000 | [diff] [blame] | 40 | #include "llvm/Analysis/TargetTransformInfo.h" |
Chandler Carruth | 0b8c9a8 | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 41 | #include "llvm/IR/Constants.h" |
| 42 | #include "llvm/IR/DerivedTypes.h" |
| 43 | #include "llvm/IR/Function.h" |
| 44 | #include "llvm/IR/Instructions.h" |
Chris Lattner | e487abb | 2006-02-09 20:15:48 +0000 | [diff] [blame] | 45 | #include "llvm/Support/CommandLine.h" |
Reid Spencer | 9133fe2 | 2007-02-05 23:32:05 +0000 | [diff] [blame] | 46 | #include "llvm/Support/Debug.h" |
Daniel Dunbar | ce63ffb | 2009-07-25 00:23:56 +0000 | [diff] [blame] | 47 | #include "llvm/Support/raw_ostream.h" |
Chandler Carruth | d04a8d4 | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 48 | #include "llvm/Transforms/Utils/BasicBlockUtils.h" |
| 49 | #include "llvm/Transforms/Utils/Cloning.h" |
| 50 | #include "llvm/Transforms/Utils/Local.h" |
Alkis Evlogimenos | 20aa474 | 2004-09-03 18:19:51 +0000 | [diff] [blame] | 51 | #include <algorithm> |
Stepan Dyatkovskiy | 209287d | 2012-01-15 09:44:07 +0000 | [diff] [blame] | 52 | #include <map> |
Chris Lattner | 2f4b898 | 2006-02-09 19:14:52 +0000 | [diff] [blame] | 53 | #include <set> |
Chris Lattner | 18f1609 | 2004-04-19 18:07:02 +0000 | [diff] [blame] | 54 | using namespace llvm; |
| 55 | |
Chris Lattner | 0e5f499 | 2006-12-19 21:40:18 +0000 | [diff] [blame] | 56 | STATISTIC(NumBranches, "Number of branches unswitched"); |
| 57 | STATISTIC(NumSwitches, "Number of switches unswitched"); |
| 58 | STATISTIC(NumSelects , "Number of selects unswitched"); |
| 59 | STATISTIC(NumTrivial , "Number of unswitches that are trivial"); |
| 60 | STATISTIC(NumSimplify, "Number of simplifications of unswitched code"); |
Stepan Dyatkovskiy | 88c5c42 | 2012-01-11 08:40:51 +0000 | [diff] [blame] | 61 | STATISTIC(TotalInsts, "Total number of instructions analyzed"); |
Chris Lattner | 0e5f499 | 2006-12-19 21:40:18 +0000 | [diff] [blame] | 62 | |
Stepan Dyatkovskiy | 27bf560 | 2012-01-16 20:48:04 +0000 | [diff] [blame] | 63 | // The specific value of 100 here was chosen based only on intuition and a |
Dan Gohman | b24f6c7 | 2009-10-13 17:50:43 +0000 | [diff] [blame] | 64 | // few specific examples. |
Dan Gohman | 844731a | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 65 | static cl::opt<unsigned> |
| 66 | Threshold("loop-unswitch-threshold", cl::desc("Max loop size to unswitch"), |
Stepan Dyatkovskiy | 88c5c42 | 2012-01-11 08:40:51 +0000 | [diff] [blame] | 67 | cl::init(100), cl::Hidden); |
Andrew Trick | 64c0748 | 2012-04-10 05:14:37 +0000 | [diff] [blame] | 68 | |
Dan Gohman | 844731a | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 69 | namespace { |
Andrew Trick | 64c0748 | 2012-04-10 05:14:37 +0000 | [diff] [blame] | 70 | |
Stepan Dyatkovskiy | 209287d | 2012-01-15 09:44:07 +0000 | [diff] [blame] | 71 | class LUAnalysisCache { |
| 72 | |
| 73 | typedef DenseMap<const SwitchInst*, SmallPtrSet<const Value *, 8> > |
| 74 | UnswitchedValsMap; |
Andrew Trick | 64c0748 | 2012-04-10 05:14:37 +0000 | [diff] [blame] | 75 | |
Stepan Dyatkovskiy | 209287d | 2012-01-15 09:44:07 +0000 | [diff] [blame] | 76 | typedef UnswitchedValsMap::iterator UnswitchedValsIt; |
Andrew Trick | 64c0748 | 2012-04-10 05:14:37 +0000 | [diff] [blame] | 77 | |
Stepan Dyatkovskiy | 209287d | 2012-01-15 09:44:07 +0000 | [diff] [blame] | 78 | struct LoopProperties { |
| 79 | unsigned CanBeUnswitchedCount; |
| 80 | unsigned SizeEstimation; |
| 81 | UnswitchedValsMap UnswitchedVals; |
| 82 | }; |
Andrew Trick | 64c0748 | 2012-04-10 05:14:37 +0000 | [diff] [blame] | 83 | |
| 84 | // Here we use std::map instead of DenseMap, since we need to keep valid |
Stepan Dyatkovskiy | 209287d | 2012-01-15 09:44:07 +0000 | [diff] [blame] | 85 | // LoopProperties pointer for current loop for better performance. |
| 86 | typedef std::map<const Loop*, LoopProperties> LoopPropsMap; |
| 87 | typedef LoopPropsMap::iterator LoopPropsMapIt; |
Andrew Trick | 64c0748 | 2012-04-10 05:14:37 +0000 | [diff] [blame] | 88 | |
Stepan Dyatkovskiy | 209287d | 2012-01-15 09:44:07 +0000 | [diff] [blame] | 89 | LoopPropsMap LoopsProperties; |
Jakub Staszak | 7198ee6 | 2013-08-06 17:03:42 +0000 | [diff] [blame] | 90 | UnswitchedValsMap *CurLoopInstructions; |
| 91 | LoopProperties *CurrentLoopProperties; |
Andrew Trick | 64c0748 | 2012-04-10 05:14:37 +0000 | [diff] [blame] | 92 | |
Stepan Dyatkovskiy | 209287d | 2012-01-15 09:44:07 +0000 | [diff] [blame] | 93 | // Max size of code we can produce on remained iterations. |
| 94 | unsigned MaxSize; |
Andrew Trick | 64c0748 | 2012-04-10 05:14:37 +0000 | [diff] [blame] | 95 | |
Stepan Dyatkovskiy | 209287d | 2012-01-15 09:44:07 +0000 | [diff] [blame] | 96 | public: |
Andrew Trick | 64c0748 | 2012-04-10 05:14:37 +0000 | [diff] [blame] | 97 | |
Stepan Dyatkovskiy | 209287d | 2012-01-15 09:44:07 +0000 | [diff] [blame] | 98 | LUAnalysisCache() : |
Jakub Staszak | 7198ee6 | 2013-08-06 17:03:42 +0000 | [diff] [blame] | 99 | CurLoopInstructions(0), CurrentLoopProperties(0), |
Stepan Dyatkovskiy | 209287d | 2012-01-15 09:44:07 +0000 | [diff] [blame] | 100 | MaxSize(Threshold) |
| 101 | {} |
Andrew Trick | 64c0748 | 2012-04-10 05:14:37 +0000 | [diff] [blame] | 102 | |
Stepan Dyatkovskiy | 209287d | 2012-01-15 09:44:07 +0000 | [diff] [blame] | 103 | // Analyze loop. Check its size, calculate is it possible to unswitch |
| 104 | // it. Returns true if we can unswitch this loop. |
Jakub Staszak | 7198ee6 | 2013-08-06 17:03:42 +0000 | [diff] [blame] | 105 | bool countLoop(const Loop *L, const TargetTransformInfo &TTI); |
Andrew Trick | 64c0748 | 2012-04-10 05:14:37 +0000 | [diff] [blame] | 106 | |
Stepan Dyatkovskiy | 209287d | 2012-01-15 09:44:07 +0000 | [diff] [blame] | 107 | // Clean all data related to given loop. |
Jakub Staszak | 7198ee6 | 2013-08-06 17:03:42 +0000 | [diff] [blame] | 108 | void forgetLoop(const Loop *L); |
Andrew Trick | 64c0748 | 2012-04-10 05:14:37 +0000 | [diff] [blame] | 109 | |
Stepan Dyatkovskiy | 209287d | 2012-01-15 09:44:07 +0000 | [diff] [blame] | 110 | // Mark case value as unswitched. |
| 111 | // Since SI instruction can be partly unswitched, in order to avoid |
| 112 | // extra unswitching in cloned loops keep track all unswitched values. |
Jakub Staszak | 7198ee6 | 2013-08-06 17:03:42 +0000 | [diff] [blame] | 113 | void setUnswitched(const SwitchInst *SI, const Value *V); |
Andrew Trick | 64c0748 | 2012-04-10 05:14:37 +0000 | [diff] [blame] | 114 | |
Stepan Dyatkovskiy | 209287d | 2012-01-15 09:44:07 +0000 | [diff] [blame] | 115 | // Check was this case value unswitched before or not. |
Jakub Staszak | 7198ee6 | 2013-08-06 17:03:42 +0000 | [diff] [blame] | 116 | bool isUnswitched(const SwitchInst *SI, const Value *V); |
Andrew Trick | 64c0748 | 2012-04-10 05:14:37 +0000 | [diff] [blame] | 117 | |
Stepan Dyatkovskiy | 209287d | 2012-01-15 09:44:07 +0000 | [diff] [blame] | 118 | // Clone all loop-unswitch related loop properties. |
| 119 | // Redistribute unswitching quotas. |
| 120 | // Note, that new loop data is stored inside the VMap. |
Jakub Staszak | 7198ee6 | 2013-08-06 17:03:42 +0000 | [diff] [blame] | 121 | void cloneData(const Loop *NewLoop, const Loop *OldLoop, |
| 122 | const ValueToValueMapTy &VMap); |
Stepan Dyatkovskiy | 209287d | 2012-01-15 09:44:07 +0000 | [diff] [blame] | 123 | }; |
Andrew Trick | 64c0748 | 2012-04-10 05:14:37 +0000 | [diff] [blame] | 124 | |
Chris Lattner | 3e8b663 | 2009-09-02 06:11:42 +0000 | [diff] [blame] | 125 | class LoopUnswitch : public LoopPass { |
Chris Lattner | 18f1609 | 2004-04-19 18:07:02 +0000 | [diff] [blame] | 126 | LoopInfo *LI; // Loop information |
Devang Patel | 1bc8936 | 2007-03-07 00:26:10 +0000 | [diff] [blame] | 127 | LPPassManager *LPM; |
Chris Lattner | a6fc94b | 2006-02-18 07:57:38 +0000 | [diff] [blame] | 128 | |
Devang Patel | 1bc8936 | 2007-03-07 00:26:10 +0000 | [diff] [blame] | 129 | // LoopProcessWorklist - Used to check if second loop needs processing |
| 130 | // after RewriteLoopBodyWithConditionConstant rewrites first loop. |
Chris Lattner | a6fc94b | 2006-02-18 07:57:38 +0000 | [diff] [blame] | 131 | std::vector<Loop*> LoopProcessWorklist; |
Stepan Dyatkovskiy | 209287d | 2012-01-15 09:44:07 +0000 | [diff] [blame] | 132 | |
Stepan Dyatkovskiy | 209287d | 2012-01-15 09:44:07 +0000 | [diff] [blame] | 133 | LUAnalysisCache BranchesInfo; |
Andrew Trick | 64c0748 | 2012-04-10 05:14:37 +0000 | [diff] [blame] | 134 | |
Devang Patel | 743f7e8 | 2007-06-06 00:21:03 +0000 | [diff] [blame] | 135 | bool OptimizeForSize; |
Devang Patel | 6f62af6 | 2007-07-30 23:07:10 +0000 | [diff] [blame] | 136 | bool redoLoop; |
Devang Patel | 5c4cd0d | 2007-10-05 22:29:34 +0000 | [diff] [blame] | 137 | |
Devang Patel | e6962df | 2008-07-02 01:18:13 +0000 | [diff] [blame] | 138 | Loop *currentLoop; |
Devang Patel | 5c4cd0d | 2007-10-05 22:29:34 +0000 | [diff] [blame] | 139 | DominatorTree *DT; |
Devang Patel | e6962df | 2008-07-02 01:18:13 +0000 | [diff] [blame] | 140 | BasicBlock *loopHeader; |
| 141 | BasicBlock *loopPreheader; |
Andrew Trick | 64c0748 | 2012-04-10 05:14:37 +0000 | [diff] [blame] | 142 | |
Devang Patel | 1e41f6d | 2008-07-02 01:44:29 +0000 | [diff] [blame] | 143 | // LoopBlocks contains all of the basic blocks of the loop, including the |
Andrew Trick | 64c0748 | 2012-04-10 05:14:37 +0000 | [diff] [blame] | 144 | // preheader of the loop, the body of the loop, and the exit blocks of the |
Devang Patel | 1e41f6d | 2008-07-02 01:44:29 +0000 | [diff] [blame] | 145 | // loop, in that order. |
| 146 | std::vector<BasicBlock*> LoopBlocks; |
| 147 | // NewBlocks contained cloned copy of basic blocks from LoopBlocks. |
| 148 | std::vector<BasicBlock*> NewBlocks; |
Devang Patel | 77a0113 | 2008-07-03 17:37:52 +0000 | [diff] [blame] | 149 | |
Chris Lattner | 18f1609 | 2004-04-19 18:07:02 +0000 | [diff] [blame] | 150 | public: |
Devang Patel | 1997473 | 2007-05-03 01:11:54 +0000 | [diff] [blame] | 151 | static char ID; // Pass ID, replacement for typeid |
Andrew Trick | 64c0748 | 2012-04-10 05:14:37 +0000 | [diff] [blame] | 152 | explicit LoopUnswitch(bool Os = false) : |
| 153 | LoopPass(ID), OptimizeForSize(Os), redoLoop(false), |
Jakub Staszak | 7198ee6 | 2013-08-06 17:03:42 +0000 | [diff] [blame] | 154 | currentLoop(0), DT(0), loopHeader(0), |
| 155 | loopPreheader(0) { |
Owen Anderson | 081c34b | 2010-10-19 17:21:58 +0000 | [diff] [blame] | 156 | initializeLoopUnswitchPass(*PassRegistry::getPassRegistry()); |
| 157 | } |
Devang Patel | 794fd75 | 2007-05-01 21:15:47 +0000 | [diff] [blame] | 158 | |
Devang Patel | 1bc8936 | 2007-03-07 00:26:10 +0000 | [diff] [blame] | 159 | bool runOnLoop(Loop *L, LPPassManager &LPM); |
Devang Patel | e6962df | 2008-07-02 01:18:13 +0000 | [diff] [blame] | 160 | bool processCurrentLoop(); |
Chris Lattner | 18f1609 | 2004-04-19 18:07:02 +0000 | [diff] [blame] | 161 | |
| 162 | /// This transformation requires natural loop information & requires that |
Chris Lattner | ea10923 | 2010-08-29 17:23:19 +0000 | [diff] [blame] | 163 | /// loop preheaders be inserted into the CFG. |
Chris Lattner | 18f1609 | 2004-04-19 18:07:02 +0000 | [diff] [blame] | 164 | /// |
| 165 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
| 166 | AU.addRequiredID(LoopSimplifyID); |
Chris Lattner | f4f5f4e | 2006-02-09 22:15:42 +0000 | [diff] [blame] | 167 | AU.addPreservedID(LoopSimplifyID); |
Chris Lattner | 18f1609 | 2004-04-19 18:07:02 +0000 | [diff] [blame] | 168 | AU.addRequired<LoopInfo>(); |
| 169 | AU.addPreserved<LoopInfo>(); |
Owen Anderson | 6edf399 | 2006-06-12 21:49:21 +0000 | [diff] [blame] | 170 | AU.addRequiredID(LCSSAID); |
Devang Patel | 15c260a | 2007-07-31 08:03:26 +0000 | [diff] [blame] | 171 | AU.addPreservedID(LCSSAID); |
Devang Patel | 4be7d29 | 2008-07-03 06:48:21 +0000 | [diff] [blame] | 172 | AU.addPreserved<DominatorTree>(); |
Cameron Zwarich | 71132af | 2011-02-11 06:08:28 +0000 | [diff] [blame] | 173 | AU.addPreserved<ScalarEvolution>(); |
Chandler Carruth | a5157e6 | 2013-01-21 13:04:33 +0000 | [diff] [blame] | 174 | AU.addRequired<TargetTransformInfo>(); |
Chris Lattner | 18f1609 | 2004-04-19 18:07:02 +0000 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | private: |
Devang Patel | 15c260a | 2007-07-31 08:03:26 +0000 | [diff] [blame] | 178 | |
Dan Gohman | 5c89b52 | 2009-09-08 15:45:00 +0000 | [diff] [blame] | 179 | virtual void releaseMemory() { |
Stepan Dyatkovskiy | 209287d | 2012-01-15 09:44:07 +0000 | [diff] [blame] | 180 | BranchesInfo.forgetLoop(currentLoop); |
Dan Gohman | 5c89b52 | 2009-09-08 15:45:00 +0000 | [diff] [blame] | 181 | } |
| 182 | |
Chris Lattner | a6fc94b | 2006-02-18 07:57:38 +0000 | [diff] [blame] | 183 | /// RemoveLoopFromWorklist - If the specified loop is on the loop worklist, |
| 184 | /// remove it. |
| 185 | void RemoveLoopFromWorklist(Loop *L) { |
| 186 | std::vector<Loop*>::iterator I = std::find(LoopProcessWorklist.begin(), |
| 187 | LoopProcessWorklist.end(), L); |
| 188 | if (I != LoopProcessWorklist.end()) |
| 189 | LoopProcessWorklist.erase(I); |
| 190 | } |
Andrew Trick | 64c0748 | 2012-04-10 05:14:37 +0000 | [diff] [blame] | 191 | |
Devang Patel | e6962df | 2008-07-02 01:18:13 +0000 | [diff] [blame] | 192 | void initLoopData() { |
| 193 | loopHeader = currentLoop->getHeader(); |
| 194 | loopPreheader = currentLoop->getLoopPreheader(); |
| 195 | } |
Andrew Trick | 64c0748 | 2012-04-10 05:14:37 +0000 | [diff] [blame] | 196 | |
Chris Lattner | 48a80b0 | 2008-04-21 00:25:49 +0000 | [diff] [blame] | 197 | /// Split all of the edges from inside the loop to their exit blocks. |
| 198 | /// Update the appropriate Phi nodes as we do so. |
Craig Topper | a0ec3f9 | 2013-07-14 04:42:23 +0000 | [diff] [blame] | 199 | void SplitExitEdges(Loop *L, const SmallVectorImpl<BasicBlock *> &ExitBlocks); |
Devang Patel | 5c4cd0d | 2007-10-05 22:29:34 +0000 | [diff] [blame] | 200 | |
Devang Patel | e6962df | 2008-07-02 01:18:13 +0000 | [diff] [blame] | 201 | bool UnswitchIfProfitable(Value *LoopCond, Constant *Val); |
Chris Lattner | f4412d8 | 2006-02-18 01:27:45 +0000 | [diff] [blame] | 202 | void UnswitchTrivialCondition(Loop *L, Value *Cond, Constant *Val, |
Chris Lattner | bd28e3f | 2006-02-22 06:37:14 +0000 | [diff] [blame] | 203 | BasicBlock *ExitBlock); |
Andrew Trick | d9fc1ce | 2012-04-10 05:14:42 +0000 | [diff] [blame] | 204 | void UnswitchNontrivialCondition(Value *LIC, Constant *OnVal, Loop *L); |
Chris Lattner | a6fc94b | 2006-02-18 07:57:38 +0000 | [diff] [blame] | 205 | |
| 206 | void RewriteLoopBodyWithConditionConstant(Loop *L, Value *LIC, |
| 207 | Constant *Val, bool isEqual); |
Devang Patel | cce624a | 2007-06-28 00:49:00 +0000 | [diff] [blame] | 208 | |
| 209 | void EmitPreheaderBranchOnCondition(Value *LIC, Constant *Val, |
Andrew Trick | 64c0748 | 2012-04-10 05:14:37 +0000 | [diff] [blame] | 210 | BasicBlock *TrueDest, |
Devang Patel | cce624a | 2007-06-28 00:49:00 +0000 | [diff] [blame] | 211 | BasicBlock *FalseDest, |
| 212 | Instruction *InsertPt); |
| 213 | |
Devang Patel | 15c260a | 2007-07-31 08:03:26 +0000 | [diff] [blame] | 214 | void SimplifyCode(std::vector<Instruction*> &Worklist, Loop *L); |
Chris Lattner | a6fc94b | 2006-02-18 07:57:38 +0000 | [diff] [blame] | 215 | void RemoveLoopFromHierarchy(Loop *L); |
Devang Patel | e6962df | 2008-07-02 01:18:13 +0000 | [diff] [blame] | 216 | bool IsTrivialUnswitchCondition(Value *Cond, Constant **Val = 0, |
| 217 | BasicBlock **LoopExit = 0); |
| 218 | |
Chris Lattner | 18f1609 | 2004-04-19 18:07:02 +0000 | [diff] [blame] | 219 | }; |
Chris Lattner | 18f1609 | 2004-04-19 18:07:02 +0000 | [diff] [blame] | 220 | } |
Stepan Dyatkovskiy | 209287d | 2012-01-15 09:44:07 +0000 | [diff] [blame] | 221 | |
| 222 | // Analyze loop. Check its size, calculate is it possible to unswitch |
| 223 | // it. Returns true if we can unswitch this loop. |
Chandler Carruth | a5157e6 | 2013-01-21 13:04:33 +0000 | [diff] [blame] | 224 | bool LUAnalysisCache::countLoop(const Loop *L, const TargetTransformInfo &TTI) { |
Andrew Trick | 64c0748 | 2012-04-10 05:14:37 +0000 | [diff] [blame] | 225 | |
Jakub Staszak | 7198ee6 | 2013-08-06 17:03:42 +0000 | [diff] [blame] | 226 | LoopPropsMapIt PropsIt; |
| 227 | bool Inserted; |
| 228 | llvm::tie(PropsIt, Inserted) = |
Stepan Dyatkovskiy | 209287d | 2012-01-15 09:44:07 +0000 | [diff] [blame] | 229 | LoopsProperties.insert(std::make_pair(L, LoopProperties())); |
Andrew Trick | 64c0748 | 2012-04-10 05:14:37 +0000 | [diff] [blame] | 230 | |
Jakub Staszak | 7198ee6 | 2013-08-06 17:03:42 +0000 | [diff] [blame] | 231 | LoopProperties &Props = PropsIt->second; |
Andrew Trick | 64c0748 | 2012-04-10 05:14:37 +0000 | [diff] [blame] | 232 | |
Jakub Staszak | 7198ee6 | 2013-08-06 17:03:42 +0000 | [diff] [blame] | 233 | if (Inserted) { |
Stepan Dyatkovskiy | 209287d | 2012-01-15 09:44:07 +0000 | [diff] [blame] | 234 | // New loop. |
| 235 | |
| 236 | // Limit the number of instructions to avoid causing significant code |
| 237 | // expansion, and the number of basic blocks, to avoid loops with |
| 238 | // large numbers of branches which cause loop unswitching to go crazy. |
| 239 | // This is a very ad-hoc heuristic. |
Andrew Trick | 64c0748 | 2012-04-10 05:14:37 +0000 | [diff] [blame] | 240 | |
Stepan Dyatkovskiy | 209287d | 2012-01-15 09:44:07 +0000 | [diff] [blame] | 241 | // FIXME: This is overly conservative because it does not take into |
| 242 | // consideration code simplification opportunities and code that can |
| 243 | // be shared by the resultant unswitched loops. |
| 244 | CodeMetrics Metrics; |
Jakub Staszak | 7198ee6 | 2013-08-06 17:03:42 +0000 | [diff] [blame] | 245 | for (Loop::block_iterator I = L->block_begin(), E = L->block_end(); |
Stepan Dyatkovskiy | 209287d | 2012-01-15 09:44:07 +0000 | [diff] [blame] | 246 | I != E; ++I) |
Chandler Carruth | a5157e6 | 2013-01-21 13:04:33 +0000 | [diff] [blame] | 247 | Metrics.analyzeBasicBlock(*I, TTI); |
Stepan Dyatkovskiy | 209287d | 2012-01-15 09:44:07 +0000 | [diff] [blame] | 248 | |
| 249 | Props.SizeEstimation = std::min(Metrics.NumInsts, Metrics.NumBlocks * 5); |
| 250 | Props.CanBeUnswitchedCount = MaxSize / (Props.SizeEstimation); |
| 251 | MaxSize -= Props.SizeEstimation * Props.CanBeUnswitchedCount; |
James Molloy | 67ae135 | 2012-12-20 16:04:27 +0000 | [diff] [blame] | 252 | |
| 253 | if (Metrics.notDuplicatable) { |
| 254 | DEBUG(dbgs() << "NOT unswitching loop %" |
Jakub Staszak | 7198ee6 | 2013-08-06 17:03:42 +0000 | [diff] [blame] | 255 | << L->getHeader()->getName() << ", contents cannot be " |
| 256 | << "duplicated!\n"); |
James Molloy | 67ae135 | 2012-12-20 16:04:27 +0000 | [diff] [blame] | 257 | return false; |
| 258 | } |
Andrew Trick | 64c0748 | 2012-04-10 05:14:37 +0000 | [diff] [blame] | 259 | } |
| 260 | |
Stepan Dyatkovskiy | 209287d | 2012-01-15 09:44:07 +0000 | [diff] [blame] | 261 | if (!Props.CanBeUnswitchedCount) { |
| 262 | DEBUG(dbgs() << "NOT unswitching loop %" |
Jakub Staszak | 7198ee6 | 2013-08-06 17:03:42 +0000 | [diff] [blame] | 263 | << L->getHeader()->getName() << ", cost too high: " |
| 264 | << L->getBlocks().size() << "\n"); |
Stepan Dyatkovskiy | 209287d | 2012-01-15 09:44:07 +0000 | [diff] [blame] | 265 | return false; |
| 266 | } |
Andrew Trick | 64c0748 | 2012-04-10 05:14:37 +0000 | [diff] [blame] | 267 | |
Stepan Dyatkovskiy | 209287d | 2012-01-15 09:44:07 +0000 | [diff] [blame] | 268 | // Be careful. This links are good only before new loop addition. |
| 269 | CurrentLoopProperties = &Props; |
| 270 | CurLoopInstructions = &Props.UnswitchedVals; |
Andrew Trick | 64c0748 | 2012-04-10 05:14:37 +0000 | [diff] [blame] | 271 | |
Stepan Dyatkovskiy | 209287d | 2012-01-15 09:44:07 +0000 | [diff] [blame] | 272 | return true; |
| 273 | } |
| 274 | |
| 275 | // Clean all data related to given loop. |
Jakub Staszak | 7198ee6 | 2013-08-06 17:03:42 +0000 | [diff] [blame] | 276 | void LUAnalysisCache::forgetLoop(const Loop *L) { |
Andrew Trick | 64c0748 | 2012-04-10 05:14:37 +0000 | [diff] [blame] | 277 | |
Stepan Dyatkovskiy | 209287d | 2012-01-15 09:44:07 +0000 | [diff] [blame] | 278 | LoopPropsMapIt LIt = LoopsProperties.find(L); |
| 279 | |
| 280 | if (LIt != LoopsProperties.end()) { |
Jakub Staszak | 7198ee6 | 2013-08-06 17:03:42 +0000 | [diff] [blame] | 281 | LoopProperties &Props = LIt->second; |
Stepan Dyatkovskiy | 209287d | 2012-01-15 09:44:07 +0000 | [diff] [blame] | 282 | MaxSize += Props.CanBeUnswitchedCount * Props.SizeEstimation; |
| 283 | LoopsProperties.erase(LIt); |
| 284 | } |
Andrew Trick | 64c0748 | 2012-04-10 05:14:37 +0000 | [diff] [blame] | 285 | |
Jakub Staszak | 7198ee6 | 2013-08-06 17:03:42 +0000 | [diff] [blame] | 286 | CurrentLoopProperties = 0; |
| 287 | CurLoopInstructions = 0; |
Stepan Dyatkovskiy | 209287d | 2012-01-15 09:44:07 +0000 | [diff] [blame] | 288 | } |
| 289 | |
| 290 | // Mark case value as unswitched. |
| 291 | // Since SI instruction can be partly unswitched, in order to avoid |
| 292 | // extra unswitching in cloned loops keep track all unswitched values. |
Jakub Staszak | 7198ee6 | 2013-08-06 17:03:42 +0000 | [diff] [blame] | 293 | void LUAnalysisCache::setUnswitched(const SwitchInst *SI, const Value *V) { |
Stepan Dyatkovskiy | 209287d | 2012-01-15 09:44:07 +0000 | [diff] [blame] | 294 | (*CurLoopInstructions)[SI].insert(V); |
| 295 | } |
| 296 | |
| 297 | // Check was this case value unswitched before or not. |
Jakub Staszak | 7198ee6 | 2013-08-06 17:03:42 +0000 | [diff] [blame] | 298 | bool LUAnalysisCache::isUnswitched(const SwitchInst *SI, const Value *V) { |
Andrew Trick | 64c0748 | 2012-04-10 05:14:37 +0000 | [diff] [blame] | 299 | return (*CurLoopInstructions)[SI].count(V); |
Stepan Dyatkovskiy | 209287d | 2012-01-15 09:44:07 +0000 | [diff] [blame] | 300 | } |
| 301 | |
| 302 | // Clone all loop-unswitch related loop properties. |
| 303 | // Redistribute unswitching quotas. |
| 304 | // Note, that new loop data is stored inside the VMap. |
Jakub Staszak | 7198ee6 | 2013-08-06 17:03:42 +0000 | [diff] [blame] | 305 | void LUAnalysisCache::cloneData(const Loop *NewLoop, const Loop *OldLoop, |
| 306 | const ValueToValueMapTy &VMap) { |
Andrew Trick | 64c0748 | 2012-04-10 05:14:37 +0000 | [diff] [blame] | 307 | |
Jakub Staszak | 7198ee6 | 2013-08-06 17:03:42 +0000 | [diff] [blame] | 308 | LoopProperties &NewLoopProps = LoopsProperties[NewLoop]; |
| 309 | LoopProperties &OldLoopProps = *CurrentLoopProperties; |
| 310 | UnswitchedValsMap &Insts = OldLoopProps.UnswitchedVals; |
Andrew Trick | 64c0748 | 2012-04-10 05:14:37 +0000 | [diff] [blame] | 311 | |
Stepan Dyatkovskiy | 209287d | 2012-01-15 09:44:07 +0000 | [diff] [blame] | 312 | // Reallocate "can-be-unswitched quota" |
| 313 | |
| 314 | --OldLoopProps.CanBeUnswitchedCount; |
| 315 | unsigned Quota = OldLoopProps.CanBeUnswitchedCount; |
| 316 | NewLoopProps.CanBeUnswitchedCount = Quota / 2; |
| 317 | OldLoopProps.CanBeUnswitchedCount = Quota - Quota / 2; |
Andrew Trick | 64c0748 | 2012-04-10 05:14:37 +0000 | [diff] [blame] | 318 | |
Stepan Dyatkovskiy | 209287d | 2012-01-15 09:44:07 +0000 | [diff] [blame] | 319 | NewLoopProps.SizeEstimation = OldLoopProps.SizeEstimation; |
Andrew Trick | 64c0748 | 2012-04-10 05:14:37 +0000 | [diff] [blame] | 320 | |
Stepan Dyatkovskiy | 209287d | 2012-01-15 09:44:07 +0000 | [diff] [blame] | 321 | // Clone unswitched values info: |
| 322 | // for new loop switches we clone info about values that was |
| 323 | // already unswitched and has redundant successors. |
| 324 | for (UnswitchedValsIt I = Insts.begin(); I != Insts.end(); ++I) { |
Jakub Staszak | 7198ee6 | 2013-08-06 17:03:42 +0000 | [diff] [blame] | 325 | const SwitchInst *OldInst = I->first; |
| 326 | Value *NewI = VMap.lookup(OldInst); |
| 327 | const SwitchInst *NewInst = cast_or_null<SwitchInst>(NewI); |
Stepan Dyatkovskiy | 209287d | 2012-01-15 09:44:07 +0000 | [diff] [blame] | 328 | assert(NewInst && "All instructions that are in SrcBB must be in VMap."); |
Andrew Trick | 64c0748 | 2012-04-10 05:14:37 +0000 | [diff] [blame] | 329 | |
Stepan Dyatkovskiy | 209287d | 2012-01-15 09:44:07 +0000 | [diff] [blame] | 330 | NewLoopProps.UnswitchedVals[NewInst] = OldLoopProps.UnswitchedVals[OldInst]; |
| 331 | } |
| 332 | } |
| 333 | |
Dan Gohman | 844731a | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 334 | char LoopUnswitch::ID = 0; |
Owen Anderson | 2ab36d3 | 2010-10-12 19:48:12 +0000 | [diff] [blame] | 335 | INITIALIZE_PASS_BEGIN(LoopUnswitch, "loop-unswitch", "Unswitch loops", |
| 336 | false, false) |
Chandler Carruth | a5157e6 | 2013-01-21 13:04:33 +0000 | [diff] [blame] | 337 | INITIALIZE_AG_DEPENDENCY(TargetTransformInfo) |
Owen Anderson | 2ab36d3 | 2010-10-12 19:48:12 +0000 | [diff] [blame] | 338 | INITIALIZE_PASS_DEPENDENCY(LoopSimplify) |
| 339 | INITIALIZE_PASS_DEPENDENCY(LoopInfo) |
| 340 | INITIALIZE_PASS_DEPENDENCY(LCSSA) |
Owen Anderson | 2ab36d3 | 2010-10-12 19:48:12 +0000 | [diff] [blame] | 341 | INITIALIZE_PASS_END(LoopUnswitch, "loop-unswitch", "Unswitch loops", |
| 342 | false, false) |
Chris Lattner | 18f1609 | 2004-04-19 18:07:02 +0000 | [diff] [blame] | 343 | |
Andrew Trick | 64c0748 | 2012-04-10 05:14:37 +0000 | [diff] [blame] | 344 | Pass *llvm::createLoopUnswitchPass(bool Os) { |
| 345 | return new LoopUnswitch(Os); |
Devang Patel | 743f7e8 | 2007-06-06 00:21:03 +0000 | [diff] [blame] | 346 | } |
Chris Lattner | a6fc94b | 2006-02-18 07:57:38 +0000 | [diff] [blame] | 347 | |
| 348 | /// FindLIVLoopCondition - Cond is a condition that occurs in L. If it is |
| 349 | /// invariant in the loop, or has an invariant piece, return the invariant. |
| 350 | /// Otherwise, return null. |
| 351 | static Value *FindLIVLoopCondition(Value *Cond, Loop *L, bool &Changed) { |
Andrew Trick | 64c0748 | 2012-04-10 05:14:37 +0000 | [diff] [blame] | 352 | |
Stepan Dyatkovskiy | 88c5c42 | 2012-01-11 08:40:51 +0000 | [diff] [blame] | 353 | // We started analyze new instruction, increment scanned instructions counter. |
| 354 | ++TotalInsts; |
Andrew Trick | 64c0748 | 2012-04-10 05:14:37 +0000 | [diff] [blame] | 355 | |
Chris Lattner | 3d606bb | 2010-02-02 02:26:54 +0000 | [diff] [blame] | 356 | // We can never unswitch on vector conditions. |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 357 | if (Cond->getType()->isVectorTy()) |
Chris Lattner | 3d606bb | 2010-02-02 02:26:54 +0000 | [diff] [blame] | 358 | return 0; |
| 359 | |
Chris Lattner | a6fc94b | 2006-02-18 07:57:38 +0000 | [diff] [blame] | 360 | // Constants should be folded, not unswitched on! |
Dan Gohman | a1fcd77 | 2008-10-17 00:56:52 +0000 | [diff] [blame] | 361 | if (isa<Constant>(Cond)) return 0; |
Devang Patel | 558f1b8 | 2007-06-28 00:44:10 +0000 | [diff] [blame] | 362 | |
Chris Lattner | a6fc94b | 2006-02-18 07:57:38 +0000 | [diff] [blame] | 363 | // TODO: Handle: br (VARIANT|INVARIANT). |
Devang Patel | 265ca5d | 2008-11-03 19:38:07 +0000 | [diff] [blame] | 364 | |
Dan Gohman | 0df6e09 | 2009-07-14 01:37:59 +0000 | [diff] [blame] | 365 | // Hoist simple values out. |
Dan Gohman | bdc017e | 2009-07-15 01:25:43 +0000 | [diff] [blame] | 366 | if (L->makeLoopInvariant(Cond, Changed)) |
Dan Gohman | 0df6e09 | 2009-07-14 01:37:59 +0000 | [diff] [blame] | 367 | return Cond; |
Dan Gohman | 0df6e09 | 2009-07-14 01:37:59 +0000 | [diff] [blame] | 368 | |
Chris Lattner | a6fc94b | 2006-02-18 07:57:38 +0000 | [diff] [blame] | 369 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(Cond)) |
| 370 | if (BO->getOpcode() == Instruction::And || |
| 371 | BO->getOpcode() == Instruction::Or) { |
| 372 | // If either the left or right side is invariant, we can unswitch on this, |
| 373 | // which will cause the branch to go away in one loop and the condition to |
| 374 | // simplify in the other one. |
| 375 | if (Value *LHS = FindLIVLoopCondition(BO->getOperand(0), L, Changed)) |
| 376 | return LHS; |
| 377 | if (Value *RHS = FindLIVLoopCondition(BO->getOperand(1), L, Changed)) |
| 378 | return RHS; |
| 379 | } |
Andrew Trick | 64c0748 | 2012-04-10 05:14:37 +0000 | [diff] [blame] | 380 | |
Devang Patel | 5c4cd0d | 2007-10-05 22:29:34 +0000 | [diff] [blame] | 381 | return 0; |
Chris Lattner | a6fc94b | 2006-02-18 07:57:38 +0000 | [diff] [blame] | 382 | } |
| 383 | |
Devang Patel | 1bc8936 | 2007-03-07 00:26:10 +0000 | [diff] [blame] | 384 | bool LoopUnswitch::runOnLoop(Loop *L, LPPassManager &LPM_Ref) { |
Devang Patel | 1bc8936 | 2007-03-07 00:26:10 +0000 | [diff] [blame] | 385 | LI = &getAnalysis<LoopInfo>(); |
| 386 | LPM = &LPM_Ref; |
Duncan Sands | 1465d61 | 2009-01-28 13:14:17 +0000 | [diff] [blame] | 387 | DT = getAnalysisIfAvailable<DominatorTree>(); |
Devang Patel | e6962df | 2008-07-02 01:18:13 +0000 | [diff] [blame] | 388 | currentLoop = L; |
Devang Patel | deafefa | 2008-09-04 22:43:59 +0000 | [diff] [blame] | 389 | Function *F = currentLoop->getHeader()->getParent(); |
Chris Lattner | a6fc94b | 2006-02-18 07:57:38 +0000 | [diff] [blame] | 390 | bool Changed = false; |
Devang Patel | 6f62af6 | 2007-07-30 23:07:10 +0000 | [diff] [blame] | 391 | do { |
Dan Gohman | bbf81d8 | 2010-03-10 19:38:49 +0000 | [diff] [blame] | 392 | assert(currentLoop->isLCSSAForm(*DT)); |
Devang Patel | 6f62af6 | 2007-07-30 23:07:10 +0000 | [diff] [blame] | 393 | redoLoop = false; |
Devang Patel | e6962df | 2008-07-02 01:18:13 +0000 | [diff] [blame] | 394 | Changed |= processCurrentLoop(); |
Devang Patel | 6f62af6 | 2007-07-30 23:07:10 +0000 | [diff] [blame] | 395 | } while(redoLoop); |
| 396 | |
Devang Patel | deafefa | 2008-09-04 22:43:59 +0000 | [diff] [blame] | 397 | if (Changed) { |
| 398 | // FIXME: Reconstruct dom info, because it is not preserved properly. |
| 399 | if (DT) |
| 400 | DT->runOnFunction(*F); |
Devang Patel | deafefa | 2008-09-04 22:43:59 +0000 | [diff] [blame] | 401 | } |
Devang Patel | 6f62af6 | 2007-07-30 23:07:10 +0000 | [diff] [blame] | 402 | return Changed; |
| 403 | } |
| 404 | |
Andrew Trick | 64c0748 | 2012-04-10 05:14:37 +0000 | [diff] [blame] | 405 | /// processCurrentLoop - Do actual work and unswitch loop if possible |
Devang Patel | e6962df | 2008-07-02 01:18:13 +0000 | [diff] [blame] | 406 | /// and profitable. |
| 407 | bool LoopUnswitch::processCurrentLoop() { |
Devang Patel | 6f62af6 | 2007-07-30 23:07:10 +0000 | [diff] [blame] | 408 | bool Changed = false; |
Stepan Dyatkovskiy | 88c5c42 | 2012-01-11 08:40:51 +0000 | [diff] [blame] | 409 | |
| 410 | initLoopData(); |
Andrew Trick | 64c0748 | 2012-04-10 05:14:37 +0000 | [diff] [blame] | 411 | |
Stepan Dyatkovskiy | 88c5c42 | 2012-01-11 08:40:51 +0000 | [diff] [blame] | 412 | // If LoopSimplify was unable to form a preheader, don't do any unswitching. |
| 413 | if (!loopPreheader) |
| 414 | return false; |
Andrew Trick | 64c0748 | 2012-04-10 05:14:37 +0000 | [diff] [blame] | 415 | |
Andrew Trick | d9fc1ce | 2012-04-10 05:14:42 +0000 | [diff] [blame] | 416 | // Loops with indirectbr cannot be cloned. |
| 417 | if (!currentLoop->isSafeToClone()) |
| 418 | return false; |
| 419 | |
| 420 | // Without dedicated exits, splitting the exit edge may fail. |
| 421 | if (!currentLoop->hasDedicatedExits()) |
| 422 | return false; |
| 423 | |
Stepan Dyatkovskiy | 88c5c42 | 2012-01-11 08:40:51 +0000 | [diff] [blame] | 424 | LLVMContext &Context = loopHeader->getContext(); |
Andrew Trick | 64c0748 | 2012-04-10 05:14:37 +0000 | [diff] [blame] | 425 | |
Stepan Dyatkovskiy | 88c5c42 | 2012-01-11 08:40:51 +0000 | [diff] [blame] | 426 | // Probably we reach the quota of branches for this loop. If so |
| 427 | // stop unswitching. |
Chandler Carruth | a5157e6 | 2013-01-21 13:04:33 +0000 | [diff] [blame] | 428 | if (!BranchesInfo.countLoop(currentLoop, getAnalysis<TargetTransformInfo>())) |
Stepan Dyatkovskiy | 88c5c42 | 2012-01-11 08:40:51 +0000 | [diff] [blame] | 429 | return false; |
Devang Patel | 6f62af6 | 2007-07-30 23:07:10 +0000 | [diff] [blame] | 430 | |
Chris Lattner | a6fc94b | 2006-02-18 07:57:38 +0000 | [diff] [blame] | 431 | // Loop over all of the basic blocks in the loop. If we find an interior |
| 432 | // block that is branching on a loop-invariant condition, we can unswitch this |
| 433 | // loop. |
Andrew Trick | 64c0748 | 2012-04-10 05:14:37 +0000 | [diff] [blame] | 434 | for (Loop::block_iterator I = currentLoop->block_begin(), |
Chris Lattner | 9e18580 | 2010-04-05 21:18:32 +0000 | [diff] [blame] | 435 | E = currentLoop->block_end(); I != E; ++I) { |
Chris Lattner | a6fc94b | 2006-02-18 07:57:38 +0000 | [diff] [blame] | 436 | TerminatorInst *TI = (*I)->getTerminator(); |
| 437 | if (BranchInst *BI = dyn_cast<BranchInst>(TI)) { |
| 438 | // If this isn't branching on an invariant condition, we can't unswitch |
| 439 | // it. |
| 440 | if (BI->isConditional()) { |
| 441 | // See if this, or some part of it, is loop invariant. If so, we can |
| 442 | // unswitch on it if we desire. |
Andrew Trick | 64c0748 | 2012-04-10 05:14:37 +0000 | [diff] [blame] | 443 | Value *LoopCond = FindLIVLoopCondition(BI->getCondition(), |
Devang Patel | e6962df | 2008-07-02 01:18:13 +0000 | [diff] [blame] | 444 | currentLoop, Changed); |
Andrew Trick | 64c0748 | 2012-04-10 05:14:37 +0000 | [diff] [blame] | 445 | if (LoopCond && UnswitchIfProfitable(LoopCond, |
Owen Anderson | 5defacc | 2009-07-31 17:39:07 +0000 | [diff] [blame] | 446 | ConstantInt::getTrue(Context))) { |
Chris Lattner | a6fc94b | 2006-02-18 07:57:38 +0000 | [diff] [blame] | 447 | ++NumBranches; |
| 448 | return true; |
| 449 | } |
Andrew Trick | 64c0748 | 2012-04-10 05:14:37 +0000 | [diff] [blame] | 450 | } |
Chris Lattner | a6fc94b | 2006-02-18 07:57:38 +0000 | [diff] [blame] | 451 | } else if (SwitchInst *SI = dyn_cast<SwitchInst>(TI)) { |
Andrew Trick | 64c0748 | 2012-04-10 05:14:37 +0000 | [diff] [blame] | 452 | Value *LoopCond = FindLIVLoopCondition(SI->getCondition(), |
Devang Patel | e6962df | 2008-07-02 01:18:13 +0000 | [diff] [blame] | 453 | currentLoop, Changed); |
Andrew Trick | 64c0748 | 2012-04-10 05:14:37 +0000 | [diff] [blame] | 454 | unsigned NumCases = SI->getNumCases(); |
Stepan Dyatkovskiy | 2447312 | 2012-02-01 07:49:51 +0000 | [diff] [blame] | 455 | if (LoopCond && NumCases) { |
Chris Lattner | a6fc94b | 2006-02-18 07:57:38 +0000 | [diff] [blame] | 456 | // Find a value to unswitch on: |
| 457 | // FIXME: this should chose the most expensive case! |
Nick Lewycky | 444f297 | 2011-06-03 06:27:15 +0000 | [diff] [blame] | 458 | // FIXME: scan for a case with a non-critical edge? |
Jakub Staszak | 7198ee6 | 2013-08-06 17:03:42 +0000 | [diff] [blame] | 459 | Constant *UnswitchVal = 0; |
Andrew Trick | 64c0748 | 2012-04-10 05:14:37 +0000 | [diff] [blame] | 460 | |
Devang Patel | 5295692 | 2007-02-26 19:31:58 +0000 | [diff] [blame] | 461 | // Do not process same value again and again. |
Chad Rosier | a816bf7 | 2011-12-22 21:10:46 +0000 | [diff] [blame] | 462 | // At this point we have some cases already unswitched and |
| 463 | // some not yet unswitched. Let's find the first not yet unswitched one. |
Stepan Dyatkovskiy | 3d3abe0 | 2012-03-11 06:09:17 +0000 | [diff] [blame] | 464 | for (SwitchInst::CaseIt i = SI->case_begin(), e = SI->case_end(); |
Stepan Dyatkovskiy | c10fa6c | 2012-03-08 07:06:20 +0000 | [diff] [blame] | 465 | i != e; ++i) { |
Jakub Staszak | 7198ee6 | 2013-08-06 17:03:42 +0000 | [diff] [blame] | 466 | Constant *UnswitchValCandidate = i.getCaseValue(); |
Stepan Dyatkovskiy | 209287d | 2012-01-15 09:44:07 +0000 | [diff] [blame] | 467 | if (!BranchesInfo.isUnswitched(SI, UnswitchValCandidate)) { |
Chad Rosier | a816bf7 | 2011-12-22 21:10:46 +0000 | [diff] [blame] | 468 | UnswitchVal = UnswitchValCandidate; |
| 469 | break; |
| 470 | } |
| 471 | } |
Andrew Trick | 64c0748 | 2012-04-10 05:14:37 +0000 | [diff] [blame] | 472 | |
Chad Rosier | a816bf7 | 2011-12-22 21:10:46 +0000 | [diff] [blame] | 473 | if (!UnswitchVal) |
Devang Patel | 5295692 | 2007-02-26 19:31:58 +0000 | [diff] [blame] | 474 | continue; |
Devang Patel | 5295692 | 2007-02-26 19:31:58 +0000 | [diff] [blame] | 475 | |
Devang Patel | e6962df | 2008-07-02 01:18:13 +0000 | [diff] [blame] | 476 | if (UnswitchIfProfitable(LoopCond, UnswitchVal)) { |
Chris Lattner | a6fc94b | 2006-02-18 07:57:38 +0000 | [diff] [blame] | 477 | ++NumSwitches; |
| 478 | return true; |
| 479 | } |
| 480 | } |
| 481 | } |
Andrew Trick | 64c0748 | 2012-04-10 05:14:37 +0000 | [diff] [blame] | 482 | |
Chris Lattner | a6fc94b | 2006-02-18 07:57:38 +0000 | [diff] [blame] | 483 | // Scan the instructions to check for unswitchable values. |
Andrew Trick | 64c0748 | 2012-04-10 05:14:37 +0000 | [diff] [blame] | 484 | for (BasicBlock::iterator BBI = (*I)->begin(), E = (*I)->end(); |
Chris Lattner | a6fc94b | 2006-02-18 07:57:38 +0000 | [diff] [blame] | 485 | BBI != E; ++BBI) |
| 486 | if (SelectInst *SI = dyn_cast<SelectInst>(BBI)) { |
Andrew Trick | 64c0748 | 2012-04-10 05:14:37 +0000 | [diff] [blame] | 487 | Value *LoopCond = FindLIVLoopCondition(SI->getCondition(), |
Devang Patel | e6962df | 2008-07-02 01:18:13 +0000 | [diff] [blame] | 488 | currentLoop, Changed); |
Andrew Trick | 64c0748 | 2012-04-10 05:14:37 +0000 | [diff] [blame] | 489 | if (LoopCond && UnswitchIfProfitable(LoopCond, |
Owen Anderson | 5defacc | 2009-07-31 17:39:07 +0000 | [diff] [blame] | 490 | ConstantInt::getTrue(Context))) { |
Chris Lattner | a6fc94b | 2006-02-18 07:57:38 +0000 | [diff] [blame] | 491 | ++NumSelects; |
| 492 | return true; |
| 493 | } |
| 494 | } |
| 495 | } |
Chris Lattner | 18f1609 | 2004-04-19 18:07:02 +0000 | [diff] [blame] | 496 | return Changed; |
| 497 | } |
| 498 | |
Dan Gohman | b0a5721 | 2010-09-01 21:46:45 +0000 | [diff] [blame] | 499 | /// isTrivialLoopExitBlock - Check to see if all paths from BB exit the |
| 500 | /// loop with no side effects (including infinite loops). |
Chris Lattner | 4e13239 | 2006-02-15 22:03:36 +0000 | [diff] [blame] | 501 | /// |
Dan Gohman | b0a5721 | 2010-09-01 21:46:45 +0000 | [diff] [blame] | 502 | /// If true, we return true and set ExitBB to the block we |
Chris Lattner | 4e13239 | 2006-02-15 22:03:36 +0000 | [diff] [blame] | 503 | /// exit through. |
| 504 | /// |
| 505 | static bool isTrivialLoopExitBlockHelper(Loop *L, BasicBlock *BB, |
| 506 | BasicBlock *&ExitBB, |
| 507 | std::set<BasicBlock*> &Visited) { |
Chris Lattner | 0017d48 | 2006-02-17 06:39:56 +0000 | [diff] [blame] | 508 | if (!Visited.insert(BB).second) { |
Nick Lewycky | 8a5641d | 2011-12-23 23:49:25 +0000 | [diff] [blame] | 509 | // Already visited. Without more analysis, this could indicate an infinite |
| 510 | // loop. |
Dan Gohman | b0a5721 | 2010-09-01 21:46:45 +0000 | [diff] [blame] | 511 | return false; |
Jakub Staszak | 7198ee6 | 2013-08-06 17:03:42 +0000 | [diff] [blame] | 512 | } |
| 513 | if (!L->contains(BB)) { |
Chris Lattner | 0017d48 | 2006-02-17 06:39:56 +0000 | [diff] [blame] | 514 | // Otherwise, this is a loop exit, this is fine so long as this is the |
| 515 | // first exit. |
| 516 | if (ExitBB != 0) return false; |
| 517 | ExitBB = BB; |
Edward O'Callaghan | 2579843 | 2009-11-25 05:38:41 +0000 | [diff] [blame] | 518 | return true; |
Chris Lattner | 0017d48 | 2006-02-17 06:39:56 +0000 | [diff] [blame] | 519 | } |
Andrew Trick | 64c0748 | 2012-04-10 05:14:37 +0000 | [diff] [blame] | 520 | |
Chris Lattner | 0017d48 | 2006-02-17 06:39:56 +0000 | [diff] [blame] | 521 | // Otherwise, this is an unvisited intra-loop node. Check all successors. |
Chris Lattner | 4e13239 | 2006-02-15 22:03:36 +0000 | [diff] [blame] | 522 | for (succ_iterator SI = succ_begin(BB), E = succ_end(BB); SI != E; ++SI) { |
Chris Lattner | 0017d48 | 2006-02-17 06:39:56 +0000 | [diff] [blame] | 523 | // Check to see if the successor is a trivial loop exit. |
| 524 | if (!isTrivialLoopExitBlockHelper(L, *SI, ExitBB, Visited)) |
| 525 | return false; |
Chris Lattner | 708e1a5 | 2006-02-10 02:30:37 +0000 | [diff] [blame] | 526 | } |
Chris Lattner | 4e13239 | 2006-02-15 22:03:36 +0000 | [diff] [blame] | 527 | |
| 528 | // Okay, everything after this looks good, check to make sure that this block |
| 529 | // doesn't include any side effects. |
Chris Lattner | a48654e | 2006-02-15 22:52:05 +0000 | [diff] [blame] | 530 | for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I) |
Duncan Sands | 7af1c78 | 2009-05-06 06:49:50 +0000 | [diff] [blame] | 531 | if (I->mayHaveSideEffects()) |
Chris Lattner | 4e13239 | 2006-02-15 22:03:36 +0000 | [diff] [blame] | 532 | return false; |
Andrew Trick | 64c0748 | 2012-04-10 05:14:37 +0000 | [diff] [blame] | 533 | |
Chris Lattner | 4e13239 | 2006-02-15 22:03:36 +0000 | [diff] [blame] | 534 | return true; |
Chris Lattner | 708e1a5 | 2006-02-10 02:30:37 +0000 | [diff] [blame] | 535 | } |
| 536 | |
Chris Lattner | a6fc94b | 2006-02-18 07:57:38 +0000 | [diff] [blame] | 537 | /// isTrivialLoopExitBlock - Return true if the specified block unconditionally |
Andrew Trick | 64c0748 | 2012-04-10 05:14:37 +0000 | [diff] [blame] | 538 | /// leads to an exit from the specified loop, and has no side-effects in the |
Chris Lattner | a6fc94b | 2006-02-18 07:57:38 +0000 | [diff] [blame] | 539 | /// process. If so, return the block that is exited to, otherwise return null. |
Chris Lattner | 4e13239 | 2006-02-15 22:03:36 +0000 | [diff] [blame] | 540 | static BasicBlock *isTrivialLoopExitBlock(Loop *L, BasicBlock *BB) { |
| 541 | std::set<BasicBlock*> Visited; |
Dan Gohman | b0a5721 | 2010-09-01 21:46:45 +0000 | [diff] [blame] | 542 | Visited.insert(L->getHeader()); // Branches to header make infinite loops. |
Chris Lattner | 4e13239 | 2006-02-15 22:03:36 +0000 | [diff] [blame] | 543 | BasicBlock *ExitBB = 0; |
| 544 | if (isTrivialLoopExitBlockHelper(L, BB, ExitBB, Visited)) |
| 545 | return ExitBB; |
| 546 | return 0; |
| 547 | } |
Chris Lattner | 708e1a5 | 2006-02-10 02:30:37 +0000 | [diff] [blame] | 548 | |
Chris Lattner | 4c41d49 | 2006-02-10 01:24:09 +0000 | [diff] [blame] | 549 | /// IsTrivialUnswitchCondition - Check to see if this unswitch condition is |
| 550 | /// trivial: that is, that the condition controls whether or not the loop does |
| 551 | /// anything at all. If this is a trivial condition, unswitching produces no |
| 552 | /// code duplications (equivalently, it produces a simpler loop and a new empty |
| 553 | /// loop, which gets deleted). |
| 554 | /// |
Chris Lattner | bd28e3f | 2006-02-22 06:37:14 +0000 | [diff] [blame] | 555 | /// If this is a trivial condition, return true, otherwise return false. When |
| 556 | /// returning true, this sets Cond and Val to the condition that controls the |
| 557 | /// trivial condition: when Cond dynamically equals Val, the loop is known to |
| 558 | /// exit. Finally, this sets LoopExit to the BB that the loop exits to when |
| 559 | /// Cond == Val. |
| 560 | /// |
Devang Patel | e6962df | 2008-07-02 01:18:13 +0000 | [diff] [blame] | 561 | bool LoopUnswitch::IsTrivialUnswitchCondition(Value *Cond, Constant **Val, |
| 562 | BasicBlock **LoopExit) { |
| 563 | BasicBlock *Header = currentLoop->getHeader(); |
Chris Lattner | a48654e | 2006-02-15 22:52:05 +0000 | [diff] [blame] | 564 | TerminatorInst *HeaderTerm = Header->getTerminator(); |
Owen Anderson | e922c02 | 2009-07-22 00:24:57 +0000 | [diff] [blame] | 565 | LLVMContext &Context = Header->getContext(); |
Andrew Trick | 64c0748 | 2012-04-10 05:14:37 +0000 | [diff] [blame] | 566 | |
Chris Lattner | a48654e | 2006-02-15 22:52:05 +0000 | [diff] [blame] | 567 | BasicBlock *LoopExitBB = 0; |
| 568 | if (BranchInst *BI = dyn_cast<BranchInst>(HeaderTerm)) { |
| 569 | // If the header block doesn't end with a conditional branch on Cond, we |
| 570 | // can't handle it. |
| 571 | if (!BI->isConditional() || BI->getCondition() != Cond) |
| 572 | return false; |
Andrew Trick | 64c0748 | 2012-04-10 05:14:37 +0000 | [diff] [blame] | 573 | |
| 574 | // Check to see if a successor of the branch is guaranteed to |
| 575 | // exit through a unique exit block without having any |
Chris Lattner | a48654e | 2006-02-15 22:52:05 +0000 | [diff] [blame] | 576 | // side-effects. If so, determine the value of Cond that causes it to do |
| 577 | // this. |
Andrew Trick | 64c0748 | 2012-04-10 05:14:37 +0000 | [diff] [blame] | 578 | if ((LoopExitBB = isTrivialLoopExitBlock(currentLoop, |
Devang Patel | e6962df | 2008-07-02 01:18:13 +0000 | [diff] [blame] | 579 | BI->getSuccessor(0)))) { |
Owen Anderson | 5defacc | 2009-07-31 17:39:07 +0000 | [diff] [blame] | 580 | if (Val) *Val = ConstantInt::getTrue(Context); |
Andrew Trick | 64c0748 | 2012-04-10 05:14:37 +0000 | [diff] [blame] | 581 | } else if ((LoopExitBB = isTrivialLoopExitBlock(currentLoop, |
Devang Patel | e6962df | 2008-07-02 01:18:13 +0000 | [diff] [blame] | 582 | BI->getSuccessor(1)))) { |
Owen Anderson | 5defacc | 2009-07-31 17:39:07 +0000 | [diff] [blame] | 583 | if (Val) *Val = ConstantInt::getFalse(Context); |
Chris Lattner | a48654e | 2006-02-15 22:52:05 +0000 | [diff] [blame] | 584 | } |
| 585 | } else if (SwitchInst *SI = dyn_cast<SwitchInst>(HeaderTerm)) { |
| 586 | // If this isn't a switch on Cond, we can't handle it. |
| 587 | if (SI->getCondition() != Cond) return false; |
Andrew Trick | 64c0748 | 2012-04-10 05:14:37 +0000 | [diff] [blame] | 588 | |
Chris Lattner | a48654e | 2006-02-15 22:52:05 +0000 | [diff] [blame] | 589 | // Check to see if a successor of the switch is guaranteed to go to the |
Andrew Trick | 64c0748 | 2012-04-10 05:14:37 +0000 | [diff] [blame] | 590 | // latch block or exit through a one exit block without having any |
Chris Lattner | a48654e | 2006-02-15 22:52:05 +0000 | [diff] [blame] | 591 | // side-effects. If so, determine the value of Cond that causes it to do |
Andrew Trick | 64c0748 | 2012-04-10 05:14:37 +0000 | [diff] [blame] | 592 | // this. |
Chad Rosier | a816bf7 | 2011-12-22 21:10:46 +0000 | [diff] [blame] | 593 | // Note that we can't trivially unswitch on the default case or |
| 594 | // on already unswitched cases. |
Stepan Dyatkovskiy | 3d3abe0 | 2012-03-11 06:09:17 +0000 | [diff] [blame] | 595 | for (SwitchInst::CaseIt i = SI->case_begin(), e = SI->case_end(); |
Stepan Dyatkovskiy | c10fa6c | 2012-03-08 07:06:20 +0000 | [diff] [blame] | 596 | i != e; ++i) { |
Jakub Staszak | 7198ee6 | 2013-08-06 17:03:42 +0000 | [diff] [blame] | 597 | BasicBlock *LoopExitCandidate; |
Andrew Trick | 64c0748 | 2012-04-10 05:14:37 +0000 | [diff] [blame] | 598 | if ((LoopExitCandidate = isTrivialLoopExitBlock(currentLoop, |
Stepan Dyatkovskiy | c10fa6c | 2012-03-08 07:06:20 +0000 | [diff] [blame] | 599 | i.getCaseSuccessor()))) { |
Chris Lattner | a48654e | 2006-02-15 22:52:05 +0000 | [diff] [blame] | 600 | // Okay, we found a trivial case, remember the value that is trivial. |
Jakub Staszak | 7198ee6 | 2013-08-06 17:03:42 +0000 | [diff] [blame] | 601 | ConstantInt *CaseVal = i.getCaseValue(); |
Chad Rosier | a816bf7 | 2011-12-22 21:10:46 +0000 | [diff] [blame] | 602 | |
| 603 | // Check that it was not unswitched before, since already unswitched |
| 604 | // trivial vals are looks trivial too. |
Stepan Dyatkovskiy | 209287d | 2012-01-15 09:44:07 +0000 | [diff] [blame] | 605 | if (BranchesInfo.isUnswitched(SI, CaseVal)) |
Chad Rosier | a816bf7 | 2011-12-22 21:10:46 +0000 | [diff] [blame] | 606 | continue; |
| 607 | LoopExitBB = LoopExitCandidate; |
| 608 | if (Val) *Val = CaseVal; |
Chris Lattner | a48654e | 2006-02-15 22:52:05 +0000 | [diff] [blame] | 609 | break; |
| 610 | } |
Chad Rosier | a816bf7 | 2011-12-22 21:10:46 +0000 | [diff] [blame] | 611 | } |
Chris Lattner | 4e13239 | 2006-02-15 22:03:36 +0000 | [diff] [blame] | 612 | } |
| 613 | |
Chris Lattner | f8bf116 | 2006-02-22 23:55:00 +0000 | [diff] [blame] | 614 | // If we didn't find a single unique LoopExit block, or if the loop exit block |
| 615 | // contains phi nodes, this isn't trivial. |
| 616 | if (!LoopExitBB || isa<PHINode>(LoopExitBB->begin())) |
Chris Lattner | 4e13239 | 2006-02-15 22:03:36 +0000 | [diff] [blame] | 617 | return false; // Can't handle this. |
Andrew Trick | 64c0748 | 2012-04-10 05:14:37 +0000 | [diff] [blame] | 618 | |
Chris Lattner | a48654e | 2006-02-15 22:52:05 +0000 | [diff] [blame] | 619 | if (LoopExit) *LoopExit = LoopExitBB; |
Andrew Trick | 64c0748 | 2012-04-10 05:14:37 +0000 | [diff] [blame] | 620 | |
Chris Lattner | 4c41d49 | 2006-02-10 01:24:09 +0000 | [diff] [blame] | 621 | // We already know that nothing uses any scalar values defined inside of this |
| 622 | // loop. As such, we just have to check to see if this loop will execute any |
| 623 | // side-effecting instructions (e.g. stores, calls, volatile loads) in the |
Chris Lattner | 4e13239 | 2006-02-15 22:03:36 +0000 | [diff] [blame] | 624 | // part of the loop that the code *would* execute. We already checked the |
| 625 | // tail, check the header now. |
Chris Lattner | 4c41d49 | 2006-02-10 01:24:09 +0000 | [diff] [blame] | 626 | for (BasicBlock::iterator I = Header->begin(), E = Header->end(); I != E; ++I) |
Duncan Sands | 7af1c78 | 2009-05-06 06:49:50 +0000 | [diff] [blame] | 627 | if (I->mayHaveSideEffects()) |
Chris Lattner | dd3ee6d | 2006-02-10 02:01:22 +0000 | [diff] [blame] | 628 | return false; |
Chris Lattner | dd3ee6d | 2006-02-10 02:01:22 +0000 | [diff] [blame] | 629 | return true; |
Chris Lattner | 4c41d49 | 2006-02-10 01:24:09 +0000 | [diff] [blame] | 630 | } |
| 631 | |
Devang Patel | e6962df | 2008-07-02 01:18:13 +0000 | [diff] [blame] | 632 | /// UnswitchIfProfitable - We have found that we can unswitch currentLoop when |
Chris Lattner | c235809 | 2006-02-11 00:43:37 +0000 | [diff] [blame] | 633 | /// LoopCond == Val to simplify the loop. If we decide that this is profitable, |
| 634 | /// unswitch the loop, reprocess the pieces, then return true. |
Chris Lattner | 3d606bb | 2010-02-02 02:26:54 +0000 | [diff] [blame] | 635 | bool LoopUnswitch::UnswitchIfProfitable(Value *LoopCond, Constant *Val) { |
Dan Gohman | f68d0c1 | 2009-12-09 22:55:01 +0000 | [diff] [blame] | 636 | Function *F = loopHeader->getParent(); |
Evan Cheng | 0272024 | 2010-04-03 02:23:43 +0000 | [diff] [blame] | 637 | Constant *CondVal = 0; |
| 638 | BasicBlock *ExitBlock = 0; |
Bill Wendling | 6aa3327 | 2012-04-30 09:23:48 +0000 | [diff] [blame] | 639 | |
Devang Patel | e6962df | 2008-07-02 01:18:13 +0000 | [diff] [blame] | 640 | if (IsTrivialUnswitchCondition(LoopCond, &CondVal, &ExitBlock)) { |
Evan Cheng | 0272024 | 2010-04-03 02:23:43 +0000 | [diff] [blame] | 641 | // If the condition is trivial, always unswitch. There is no code growth |
| 642 | // for this case. |
Devang Patel | e6962df | 2008-07-02 01:18:13 +0000 | [diff] [blame] | 643 | UnswitchTrivialCondition(currentLoop, LoopCond, CondVal, ExitBlock); |
Evan Cheng | 0272024 | 2010-04-03 02:23:43 +0000 | [diff] [blame] | 644 | return true; |
Chris Lattner | c235809 | 2006-02-11 00:43:37 +0000 | [diff] [blame] | 645 | } |
Devang Patel | 4be7d29 | 2008-07-03 06:48:21 +0000 | [diff] [blame] | 646 | |
Evan Cheng | 0272024 | 2010-04-03 02:23:43 +0000 | [diff] [blame] | 647 | // Check to see if it would be profitable to unswitch current loop. |
| 648 | |
| 649 | // Do not do non-trivial unswitch while optimizing for size. |
Bill Wendling | 6765834 | 2012-10-09 07:45:08 +0000 | [diff] [blame] | 650 | if (OptimizeForSize || |
Bill Wendling | 831737d | 2012-12-30 10:32:01 +0000 | [diff] [blame] | 651 | F->getAttributes().hasAttribute(AttributeSet::FunctionIndex, |
| 652 | Attribute::OptimizeForSize)) |
Evan Cheng | 0272024 | 2010-04-03 02:23:43 +0000 | [diff] [blame] | 653 | return false; |
| 654 | |
Andrew Trick | d9fc1ce | 2012-04-10 05:14:42 +0000 | [diff] [blame] | 655 | UnswitchNontrivialCondition(LoopCond, Val, currentLoop); |
| 656 | return true; |
Chris Lattner | c235809 | 2006-02-11 00:43:37 +0000 | [diff] [blame] | 657 | } |
| 658 | |
Chris Lattner | 18f1609 | 2004-04-19 18:07:02 +0000 | [diff] [blame] | 659 | /// CloneLoop - Recursively clone the specified loop and all of its children, |
| 660 | /// mapping the blocks with the specified map. |
Rafael Espindola | 1ed219a | 2010-10-13 01:36:30 +0000 | [diff] [blame] | 661 | static Loop *CloneLoop(Loop *L, Loop *PL, ValueToValueMapTy &VM, |
Devang Patel | 1bc8936 | 2007-03-07 00:26:10 +0000 | [diff] [blame] | 662 | LoopInfo *LI, LPPassManager *LPM) { |
Chris Lattner | 18f1609 | 2004-04-19 18:07:02 +0000 | [diff] [blame] | 663 | Loop *New = new Loop(); |
Devang Patel | 1bc8936 | 2007-03-07 00:26:10 +0000 | [diff] [blame] | 664 | LPM->insertLoop(New, PL); |
Chris Lattner | 18f1609 | 2004-04-19 18:07:02 +0000 | [diff] [blame] | 665 | |
| 666 | // Add all of the blocks in L to the new loop. |
| 667 | for (Loop::block_iterator I = L->block_begin(), E = L->block_end(); |
| 668 | I != E; ++I) |
| 669 | if (LI->getLoopFor(*I) == L) |
Owen Anderson | d735ee8 | 2007-11-27 03:43:35 +0000 | [diff] [blame] | 670 | New->addBasicBlockToLoop(cast<BasicBlock>(VM[*I]), LI->getBase()); |
Chris Lattner | 18f1609 | 2004-04-19 18:07:02 +0000 | [diff] [blame] | 671 | |
| 672 | // Add all of the subloops to the new loop. |
| 673 | for (Loop::iterator I = L->begin(), E = L->end(); I != E; ++I) |
Devang Patel | 1bc8936 | 2007-03-07 00:26:10 +0000 | [diff] [blame] | 674 | CloneLoop(*I, New, VM, LI, LPM); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 675 | |
Chris Lattner | 18f1609 | 2004-04-19 18:07:02 +0000 | [diff] [blame] | 676 | return New; |
| 677 | } |
| 678 | |
Chris Lattner | fed5d9d | 2006-02-15 00:07:43 +0000 | [diff] [blame] | 679 | /// EmitPreheaderBranchOnCondition - Emit a conditional branch on two values |
| 680 | /// if LIC == Val, branch to TrueDst, otherwise branch to FalseDest. Insert the |
| 681 | /// code immediately before InsertPt. |
Devang Patel | cce624a | 2007-06-28 00:49:00 +0000 | [diff] [blame] | 682 | void LoopUnswitch::EmitPreheaderBranchOnCondition(Value *LIC, Constant *Val, |
| 683 | BasicBlock *TrueDest, |
| 684 | BasicBlock *FalseDest, |
| 685 | Instruction *InsertPt) { |
Chris Lattner | fed5d9d | 2006-02-15 00:07:43 +0000 | [diff] [blame] | 686 | // Insert a conditional branch on LIC to the two preheaders. The original |
| 687 | // code is the true version and the new code is the false version. |
| 688 | Value *BranchVal = LIC; |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 689 | if (!isa<ConstantInt>(Val) || |
| 690 | Val->getType() != Type::getInt1Ty(LIC->getContext())) |
Benjamin Kramer | a9390a4 | 2011-09-27 20:39:19 +0000 | [diff] [blame] | 691 | BranchVal = new ICmpInst(InsertPt, ICmpInst::ICMP_EQ, LIC, Val); |
Owen Anderson | 5defacc | 2009-07-31 17:39:07 +0000 | [diff] [blame] | 692 | else if (Val != ConstantInt::getTrue(Val->getContext())) |
Chris Lattner | fed5d9d | 2006-02-15 00:07:43 +0000 | [diff] [blame] | 693 | // We want to enter the new loop when the condition is true. |
| 694 | std::swap(TrueDest, FalseDest); |
Chris Lattner | fed5d9d | 2006-02-15 00:07:43 +0000 | [diff] [blame] | 695 | |
| 696 | // Insert the new branch. |
Dan Gohman | 5c89b52 | 2009-09-08 15:45:00 +0000 | [diff] [blame] | 697 | BranchInst *BI = BranchInst::Create(TrueDest, FalseDest, BranchVal, InsertPt); |
| 698 | |
| 699 | // If either edge is critical, split it. This helps preserve LoopSimplify |
| 700 | // form for enclosing loops. |
Bill Wendling | bfbab99 | 2012-04-30 10:44:54 +0000 | [diff] [blame] | 701 | SplitCriticalEdge(BI, 0, this, false, false, true); |
| 702 | SplitCriticalEdge(BI, 1, this, false, false, true); |
Chris Lattner | fed5d9d | 2006-02-15 00:07:43 +0000 | [diff] [blame] | 703 | } |
| 704 | |
Chris Lattner | 4c41d49 | 2006-02-10 01:24:09 +0000 | [diff] [blame] | 705 | /// UnswitchTrivialCondition - Given a loop that has a trivial unswitchable |
| 706 | /// condition in it (a cond branch from its header block to its latch block, |
Andrew Trick | 64c0748 | 2012-04-10 05:14:37 +0000 | [diff] [blame] | 707 | /// where the path through the loop that doesn't execute its body has no |
Chris Lattner | 4c41d49 | 2006-02-10 01:24:09 +0000 | [diff] [blame] | 708 | /// side-effects), unswitch it. This doesn't involve any code duplication, just |
| 709 | /// moving the conditional branch outside of the loop and updating loop info. |
Andrew Trick | 64c0748 | 2012-04-10 05:14:37 +0000 | [diff] [blame] | 710 | void LoopUnswitch::UnswitchTrivialCondition(Loop *L, Value *Cond, |
| 711 | Constant *Val, |
Chris Lattner | dd3ee6d | 2006-02-10 02:01:22 +0000 | [diff] [blame] | 712 | BasicBlock *ExitBlock) { |
David Greene | d4c56fb | 2010-01-05 01:27:04 +0000 | [diff] [blame] | 713 | DEBUG(dbgs() << "loop-unswitch: Trivial-Unswitch loop %" |
Daniel Dunbar | ce63ffb | 2009-07-25 00:23:56 +0000 | [diff] [blame] | 714 | << loopHeader->getName() << " [" << L->getBlocks().size() |
| 715 | << " blocks] in Function " << L->getHeader()->getParent()->getName() |
| 716 | << " on cond: " << *Val << " == " << *Cond << "\n"); |
Andrew Trick | 64c0748 | 2012-04-10 05:14:37 +0000 | [diff] [blame] | 717 | |
Chris Lattner | b2bc315 | 2006-02-10 23:16:39 +0000 | [diff] [blame] | 718 | // First step, split the preheader, so that we know that there is a safe place |
Devang Patel | e6962df | 2008-07-02 01:18:13 +0000 | [diff] [blame] | 719 | // to insert the conditional branch. We will change loopPreheader to have a |
Chris Lattner | 4c41d49 | 2006-02-10 01:24:09 +0000 | [diff] [blame] | 720 | // conditional branch on Cond. |
Devang Patel | e6962df | 2008-07-02 01:18:13 +0000 | [diff] [blame] | 721 | BasicBlock *NewPH = SplitEdge(loopPreheader, loopHeader, this); |
Chris Lattner | 4c41d49 | 2006-02-10 01:24:09 +0000 | [diff] [blame] | 722 | |
| 723 | // Now that we have a place to insert the conditional branch, create a place |
Chris Lattner | dd3ee6d | 2006-02-10 02:01:22 +0000 | [diff] [blame] | 724 | // to branch to: this is the exit block out of the loop that we should |
| 725 | // short-circuit to. |
Andrew Trick | 64c0748 | 2012-04-10 05:14:37 +0000 | [diff] [blame] | 726 | |
Chris Lattner | 4e13239 | 2006-02-15 22:03:36 +0000 | [diff] [blame] | 727 | // Split this block now, so that the loop maintains its exit block, and so |
| 728 | // that the jump from the preheader can execute the contents of the exit block |
| 729 | // without actually branching to it (the exit block should be dominated by the |
| 730 | // loop header, not the preheader). |
Chris Lattner | dd3ee6d | 2006-02-10 02:01:22 +0000 | [diff] [blame] | 731 | assert(!L->contains(ExitBlock) && "Exit block is in the loop?"); |
Devang Patel | 05c1dc6 | 2007-07-06 22:03:47 +0000 | [diff] [blame] | 732 | BasicBlock *NewExit = SplitBlock(ExitBlock, ExitBlock->begin(), this); |
Andrew Trick | 64c0748 | 2012-04-10 05:14:37 +0000 | [diff] [blame] | 733 | |
| 734 | // Okay, now we have a position to branch from and a position to branch to, |
Chris Lattner | 4c41d49 | 2006-02-10 01:24:09 +0000 | [diff] [blame] | 735 | // insert the new conditional branch. |
Andrew Trick | 64c0748 | 2012-04-10 05:14:37 +0000 | [diff] [blame] | 736 | EmitPreheaderBranchOnCondition(Cond, Val, NewExit, NewPH, |
Devang Patel | e6962df | 2008-07-02 01:18:13 +0000 | [diff] [blame] | 737 | loopPreheader->getTerminator()); |
Devang Patel | e6962df | 2008-07-02 01:18:13 +0000 | [diff] [blame] | 738 | LPM->deleteSimpleAnalysisValue(loopPreheader->getTerminator(), L); |
| 739 | loopPreheader->getTerminator()->eraseFromParent(); |
Chris Lattner | 4c41d49 | 2006-02-10 01:24:09 +0000 | [diff] [blame] | 740 | |
Chris Lattner | a6fc94b | 2006-02-18 07:57:38 +0000 | [diff] [blame] | 741 | // We need to reprocess this loop, it could be unswitched again. |
Devang Patel | 6f62af6 | 2007-07-30 23:07:10 +0000 | [diff] [blame] | 742 | redoLoop = true; |
Andrew Trick | 64c0748 | 2012-04-10 05:14:37 +0000 | [diff] [blame] | 743 | |
Chris Lattner | 4c41d49 | 2006-02-10 01:24:09 +0000 | [diff] [blame] | 744 | // Now that we know that the loop is never entered when this condition is a |
| 745 | // particular value, rewrite the loop with this info. We know that this will |
| 746 | // at least eliminate the old branch. |
Chris Lattner | bd28e3f | 2006-02-22 06:37:14 +0000 | [diff] [blame] | 747 | RewriteLoopBodyWithConditionConstant(L, Cond, Val, false); |
Chris Lattner | 3dd4c40 | 2006-02-14 01:01:41 +0000 | [diff] [blame] | 748 | ++NumTrivial; |
Chris Lattner | 4c41d49 | 2006-02-10 01:24:09 +0000 | [diff] [blame] | 749 | } |
| 750 | |
Chris Lattner | 48a80b0 | 2008-04-21 00:25:49 +0000 | [diff] [blame] | 751 | /// SplitExitEdges - Split all of the edges from inside the loop to their exit |
| 752 | /// blocks. Update the appropriate Phi nodes as we do so. |
Andrew Trick | 64c0748 | 2012-04-10 05:14:37 +0000 | [diff] [blame] | 753 | void LoopUnswitch::SplitExitEdges(Loop *L, |
Craig Topper | a0ec3f9 | 2013-07-14 04:42:23 +0000 | [diff] [blame] | 754 | const SmallVectorImpl<BasicBlock *> &ExitBlocks){ |
Devang Patel | 5c4cd0d | 2007-10-05 22:29:34 +0000 | [diff] [blame] | 755 | |
Chris Lattner | 4c41d49 | 2006-02-10 01:24:09 +0000 | [diff] [blame] | 756 | for (unsigned i = 0, e = ExitBlocks.size(); i != e; ++i) { |
Chris Lattner | b2bc315 | 2006-02-10 23:16:39 +0000 | [diff] [blame] | 757 | BasicBlock *ExitBlock = ExitBlocks[i]; |
Dan Gohman | 5c89b52 | 2009-09-08 15:45:00 +0000 | [diff] [blame] | 758 | SmallVector<BasicBlock *, 4> Preds(pred_begin(ExitBlock), |
| 759 | pred_end(ExitBlock)); |
Bill Wendling | d552098 | 2011-09-27 00:59:31 +0000 | [diff] [blame] | 760 | |
Nick Lewycky | 444f297 | 2011-06-03 06:27:15 +0000 | [diff] [blame] | 761 | // Although SplitBlockPredecessors doesn't preserve loop-simplify in |
| 762 | // general, if we call it on all predecessors of all exits then it does. |
Bill Wendling | d552098 | 2011-09-27 00:59:31 +0000 | [diff] [blame] | 763 | if (!ExitBlock->isLandingPad()) { |
Jakub Staszak | 2fac1d5 | 2011-12-09 21:19:53 +0000 | [diff] [blame] | 764 | SplitBlockPredecessors(ExitBlock, Preds, ".us-lcssa", this); |
Bill Wendling | d552098 | 2011-09-27 00:59:31 +0000 | [diff] [blame] | 765 | } else { |
| 766 | SmallVector<BasicBlock*, 2> NewBBs; |
| 767 | SplitLandingPadPredecessors(ExitBlock, Preds, ".us-lcssa", ".us-lcssa", |
| 768 | this, NewBBs); |
| 769 | } |
Chris Lattner | 4c41d49 | 2006-02-10 01:24:09 +0000 | [diff] [blame] | 770 | } |
Devang Patel | f476e8e | 2007-10-03 21:16:08 +0000 | [diff] [blame] | 771 | } |
| 772 | |
Andrew Trick | 64c0748 | 2012-04-10 05:14:37 +0000 | [diff] [blame] | 773 | /// UnswitchNontrivialCondition - We determined that the loop is profitable |
| 774 | /// to unswitch when LIC equal Val. Split it into loop versions and test the |
Devang Patel | c1e2660 | 2007-10-03 21:17:43 +0000 | [diff] [blame] | 775 | /// condition outside of either loop. Return the loops created as Out1/Out2. |
Andrew Trick | d9fc1ce | 2012-04-10 05:14:42 +0000 | [diff] [blame] | 776 | void LoopUnswitch::UnswitchNontrivialCondition(Value *LIC, Constant *Val, |
Devang Patel | f476e8e | 2007-10-03 21:16:08 +0000 | [diff] [blame] | 777 | Loop *L) { |
Devang Patel | e6962df | 2008-07-02 01:18:13 +0000 | [diff] [blame] | 778 | Function *F = loopHeader->getParent(); |
David Greene | d4c56fb | 2010-01-05 01:27:04 +0000 | [diff] [blame] | 779 | DEBUG(dbgs() << "loop-unswitch: Unswitching loop %" |
Daniel Dunbar | ce63ffb | 2009-07-25 00:23:56 +0000 | [diff] [blame] | 780 | << loopHeader->getName() << " [" << L->getBlocks().size() |
| 781 | << " blocks] in Function " << F->getName() |
| 782 | << " when '" << *Val << "' == " << *LIC << "\n"); |
Devang Patel | f476e8e | 2007-10-03 21:16:08 +0000 | [diff] [blame] | 783 | |
Cameron Zwarich | 71132af | 2011-02-11 06:08:28 +0000 | [diff] [blame] | 784 | if (ScalarEvolution *SE = getAnalysisIfAvailable<ScalarEvolution>()) |
| 785 | SE->forgetLoop(L); |
| 786 | |
Devang Patel | 1e41f6d | 2008-07-02 01:44:29 +0000 | [diff] [blame] | 787 | LoopBlocks.clear(); |
| 788 | NewBlocks.clear(); |
Devang Patel | f476e8e | 2007-10-03 21:16:08 +0000 | [diff] [blame] | 789 | |
| 790 | // First step, split the preheader and exit blocks, and add these blocks to |
| 791 | // the LoopBlocks list. |
Devang Patel | e6962df | 2008-07-02 01:18:13 +0000 | [diff] [blame] | 792 | BasicBlock *NewPreheader = SplitEdge(loopPreheader, loopHeader, this); |
Devang Patel | f476e8e | 2007-10-03 21:16:08 +0000 | [diff] [blame] | 793 | LoopBlocks.push_back(NewPreheader); |
| 794 | |
| 795 | // We want the loop to come after the preheader, but before the exit blocks. |
| 796 | LoopBlocks.insert(LoopBlocks.end(), L->block_begin(), L->block_end()); |
| 797 | |
| 798 | SmallVector<BasicBlock*, 8> ExitBlocks; |
| 799 | L->getUniqueExitBlocks(ExitBlocks); |
| 800 | |
| 801 | // Split all of the edges from inside the loop to their exit blocks. Update |
| 802 | // the appropriate Phi nodes as we do so. |
Devang Patel | 77a0113 | 2008-07-03 17:37:52 +0000 | [diff] [blame] | 803 | SplitExitEdges(L, ExitBlocks); |
Devang Patel | f476e8e | 2007-10-03 21:16:08 +0000 | [diff] [blame] | 804 | |
Chris Lattner | b2bc315 | 2006-02-10 23:16:39 +0000 | [diff] [blame] | 805 | // The exit blocks may have been changed due to edge splitting, recompute. |
| 806 | ExitBlocks.clear(); |
Devang Patel | 4b8f36f | 2006-08-29 22:29:16 +0000 | [diff] [blame] | 807 | L->getUniqueExitBlocks(ExitBlocks); |
| 808 | |
Chris Lattner | b2bc315 | 2006-02-10 23:16:39 +0000 | [diff] [blame] | 809 | // Add exit blocks to the loop blocks. |
| 810 | LoopBlocks.insert(LoopBlocks.end(), ExitBlocks.begin(), ExitBlocks.end()); |
Chris Lattner | 18f1609 | 2004-04-19 18:07:02 +0000 | [diff] [blame] | 811 | |
| 812 | // Next step, clone all of the basic blocks that make up the loop (including |
| 813 | // the loop preheader and exit blocks), keeping track of the mapping between |
| 814 | // the instructions and blocks. |
Chris Lattner | 18f1609 | 2004-04-19 18:07:02 +0000 | [diff] [blame] | 815 | NewBlocks.reserve(LoopBlocks.size()); |
Rafael Espindola | 1ed219a | 2010-10-13 01:36:30 +0000 | [diff] [blame] | 816 | ValueToValueMapTy VMap; |
Chris Lattner | 18f1609 | 2004-04-19 18:07:02 +0000 | [diff] [blame] | 817 | for (unsigned i = 0, e = LoopBlocks.size(); i != e; ++i) { |
Devang Patel | e9916a3 | 2010-06-24 00:33:28 +0000 | [diff] [blame] | 818 | BasicBlock *NewBB = CloneBasicBlock(LoopBlocks[i], VMap, ".us", F); |
Andrew Trick | 64c0748 | 2012-04-10 05:14:37 +0000 | [diff] [blame] | 819 | |
Evan Cheng | 0f1666b | 2010-04-05 21:16:25 +0000 | [diff] [blame] | 820 | NewBlocks.push_back(NewBB); |
Devang Patel | e9916a3 | 2010-06-24 00:33:28 +0000 | [diff] [blame] | 821 | VMap[LoopBlocks[i]] = NewBB; // Keep the BB mapping. |
Evan Cheng | 0f1666b | 2010-04-05 21:16:25 +0000 | [diff] [blame] | 822 | LPM->cloneBasicBlockSimpleAnalysis(LoopBlocks[i], NewBB, L); |
Chris Lattner | 18f1609 | 2004-04-19 18:07:02 +0000 | [diff] [blame] | 823 | } |
| 824 | |
| 825 | // Splice the newly inserted blocks into the function right before the |
| 826 | // original preheader. |
Evan Cheng | 0f1666b | 2010-04-05 21:16:25 +0000 | [diff] [blame] | 827 | F->getBasicBlockList().splice(NewPreheader, F->getBasicBlockList(), |
Chris Lattner | 18f1609 | 2004-04-19 18:07:02 +0000 | [diff] [blame] | 828 | NewBlocks[0], F->end()); |
| 829 | |
| 830 | // Now we create the new Loop object for the versioned loop. |
Devang Patel | e9916a3 | 2010-06-24 00:33:28 +0000 | [diff] [blame] | 831 | Loop *NewLoop = CloneLoop(L, L->getParentLoop(), VMap, LI, LPM); |
Stepan Dyatkovskiy | 209287d | 2012-01-15 09:44:07 +0000 | [diff] [blame] | 832 | |
| 833 | // Recalculate unswitching quota, inherit simplified switches info for NewBB, |
| 834 | // Probably clone more loop-unswitch related loop properties. |
| 835 | BranchesInfo.cloneData(NewLoop, L, VMap); |
| 836 | |
Chris Lattner | e825593 | 2006-02-10 23:26:14 +0000 | [diff] [blame] | 837 | Loop *ParentLoop = L->getParentLoop(); |
| 838 | if (ParentLoop) { |
Chris Lattner | 18f1609 | 2004-04-19 18:07:02 +0000 | [diff] [blame] | 839 | // Make sure to add the cloned preheader and exit blocks to the parent loop |
| 840 | // as well. |
Owen Anderson | d735ee8 | 2007-11-27 03:43:35 +0000 | [diff] [blame] | 841 | ParentLoop->addBasicBlockToLoop(NewBlocks[0], LI->getBase()); |
Chris Lattner | e825593 | 2006-02-10 23:26:14 +0000 | [diff] [blame] | 842 | } |
Bill Wendling | d552098 | 2011-09-27 00:59:31 +0000 | [diff] [blame] | 843 | |
Chris Lattner | e825593 | 2006-02-10 23:26:14 +0000 | [diff] [blame] | 844 | for (unsigned i = 0, e = ExitBlocks.size(); i != e; ++i) { |
Devang Patel | e9916a3 | 2010-06-24 00:33:28 +0000 | [diff] [blame] | 845 | BasicBlock *NewExit = cast<BasicBlock>(VMap[ExitBlocks[i]]); |
Chris Lattner | 25cae0f | 2006-02-18 00:55:32 +0000 | [diff] [blame] | 846 | // The new exit block should be in the same loop as the old one. |
| 847 | if (Loop *ExitBBLoop = LI->getLoopFor(ExitBlocks[i])) |
Owen Anderson | d735ee8 | 2007-11-27 03:43:35 +0000 | [diff] [blame] | 848 | ExitBBLoop->addBasicBlockToLoop(NewExit, LI->getBase()); |
Andrew Trick | 64c0748 | 2012-04-10 05:14:37 +0000 | [diff] [blame] | 849 | |
Chris Lattner | e825593 | 2006-02-10 23:26:14 +0000 | [diff] [blame] | 850 | assert(NewExit->getTerminator()->getNumSuccessors() == 1 && |
| 851 | "Exit block should have been split to have one successor!"); |
| 852 | BasicBlock *ExitSucc = NewExit->getTerminator()->getSuccessor(0); |
Devang Patel | 77a0113 | 2008-07-03 17:37:52 +0000 | [diff] [blame] | 853 | |
Chris Lattner | e825593 | 2006-02-10 23:26:14 +0000 | [diff] [blame] | 854 | // If the successor of the exit block had PHI nodes, add an entry for |
| 855 | // NewExit. |
Jakub Staszak | 7198ee6 | 2013-08-06 17:03:42 +0000 | [diff] [blame] | 856 | for (BasicBlock::iterator I = ExitSucc->begin(); |
| 857 | PHINode *PN = dyn_cast<PHINode>(I); ++I) { |
Chris Lattner | e825593 | 2006-02-10 23:26:14 +0000 | [diff] [blame] | 858 | Value *V = PN->getIncomingValueForBlock(ExitBlocks[i]); |
Rafael Espindola | 1ed219a | 2010-10-13 01:36:30 +0000 | [diff] [blame] | 859 | ValueToValueMapTy::iterator It = VMap.find(V); |
Devang Patel | e9916a3 | 2010-06-24 00:33:28 +0000 | [diff] [blame] | 860 | if (It != VMap.end()) V = It->second; |
Chris Lattner | e825593 | 2006-02-10 23:26:14 +0000 | [diff] [blame] | 861 | PN->addIncoming(V, NewExit); |
| 862 | } |
Bill Wendling | d552098 | 2011-09-27 00:59:31 +0000 | [diff] [blame] | 863 | |
| 864 | if (LandingPadInst *LPad = NewExit->getLandingPadInst()) { |
Jakub Staszak | 7198ee6 | 2013-08-06 17:03:42 +0000 | [diff] [blame] | 865 | PHINode *PN = PHINode::Create(LPad->getType(), 0, "", |
| 866 | ExitSucc->getFirstInsertionPt()); |
Bill Wendling | d552098 | 2011-09-27 00:59:31 +0000 | [diff] [blame] | 867 | |
| 868 | for (pred_iterator I = pred_begin(ExitSucc), E = pred_end(ExitSucc); |
| 869 | I != E; ++I) { |
| 870 | BasicBlock *BB = *I; |
| 871 | LandingPadInst *LPI = BB->getLandingPadInst(); |
| 872 | LPI->replaceAllUsesWith(PN); |
| 873 | PN->addIncoming(LPI, BB); |
| 874 | } |
| 875 | } |
Chris Lattner | 18f1609 | 2004-04-19 18:07:02 +0000 | [diff] [blame] | 876 | } |
| 877 | |
| 878 | // Rewrite the code to refer to itself. |
Nick Lewycky | 280a6e6 | 2008-04-25 16:53:59 +0000 | [diff] [blame] | 879 | for (unsigned i = 0, e = NewBlocks.size(); i != e; ++i) |
| 880 | for (BasicBlock::iterator I = NewBlocks[i]->begin(), |
| 881 | E = NewBlocks[i]->end(); I != E; ++I) |
Chris Lattner | b5fa5fc | 2011-01-08 08:15:20 +0000 | [diff] [blame] | 882 | RemapInstruction(I, VMap,RF_NoModuleLevelChanges|RF_IgnoreMissingEntries); |
Andrew Trick | 64c0748 | 2012-04-10 05:14:37 +0000 | [diff] [blame] | 883 | |
Chris Lattner | 18f1609 | 2004-04-19 18:07:02 +0000 | [diff] [blame] | 884 | // Rewrite the original preheader to select between versions of the loop. |
Devang Patel | e6962df | 2008-07-02 01:18:13 +0000 | [diff] [blame] | 885 | BranchInst *OldBR = cast<BranchInst>(loopPreheader->getTerminator()); |
Chris Lattner | fed5d9d | 2006-02-15 00:07:43 +0000 | [diff] [blame] | 886 | assert(OldBR->isUnconditional() && OldBR->getSuccessor(0) == LoopBlocks[0] && |
Chris Lattner | 18f1609 | 2004-04-19 18:07:02 +0000 | [diff] [blame] | 887 | "Preheader splitting did not work correctly!"); |
Chris Lattner | 18f1609 | 2004-04-19 18:07:02 +0000 | [diff] [blame] | 888 | |
Chris Lattner | fed5d9d | 2006-02-15 00:07:43 +0000 | [diff] [blame] | 889 | // Emit the new branch that selects between the two versions of this loop. |
| 890 | EmitPreheaderBranchOnCondition(LIC, Val, NewBlocks[0], LoopBlocks[0], OldBR); |
Devang Patel | 15c260a | 2007-07-31 08:03:26 +0000 | [diff] [blame] | 891 | LPM->deleteSimpleAnalysisValue(OldBR, L); |
Devang Patel | 9ee49c5 | 2007-09-20 23:45:50 +0000 | [diff] [blame] | 892 | OldBR->eraseFromParent(); |
Devang Patel | 1ff6138 | 2007-08-02 15:25:57 +0000 | [diff] [blame] | 893 | |
Chris Lattner | a6fc94b | 2006-02-18 07:57:38 +0000 | [diff] [blame] | 894 | LoopProcessWorklist.push_back(NewLoop); |
Devang Patel | 6f62af6 | 2007-07-30 23:07:10 +0000 | [diff] [blame] | 895 | redoLoop = true; |
Chris Lattner | 18f1609 | 2004-04-19 18:07:02 +0000 | [diff] [blame] | 896 | |
Chris Lattner | a78130c | 2010-04-20 05:09:16 +0000 | [diff] [blame] | 897 | // Keep a WeakVH holding onto LIC. If the first call to RewriteLoopBody |
| 898 | // deletes the instruction (for example by simplifying a PHI that feeds into |
| 899 | // the condition that we're unswitching on), we don't rewrite the second |
| 900 | // iteration. |
| 901 | WeakVH LICHandle(LIC); |
Andrew Trick | 64c0748 | 2012-04-10 05:14:37 +0000 | [diff] [blame] | 902 | |
Chris Lattner | 18f1609 | 2004-04-19 18:07:02 +0000 | [diff] [blame] | 903 | // Now we rewrite the original code to know that the condition is true and the |
| 904 | // new code to know that the condition is false. |
Evan Cheng | 0f1666b | 2010-04-05 21:16:25 +0000 | [diff] [blame] | 905 | RewriteLoopBodyWithConditionConstant(L, LIC, Val, false); |
Devang Patel | 77a0113 | 2008-07-03 17:37:52 +0000 | [diff] [blame] | 906 | |
Chris Lattner | a78130c | 2010-04-20 05:09:16 +0000 | [diff] [blame] | 907 | // It's possible that simplifying one loop could cause the other to be |
| 908 | // changed to another value or a constant. If its a constant, don't simplify |
| 909 | // it. |
| 910 | if (!LoopProcessWorklist.empty() && LoopProcessWorklist.back() == NewLoop && |
| 911 | LICHandle && !isa<Constant>(LICHandle)) |
| 912 | RewriteLoopBodyWithConditionConstant(NewLoop, LICHandle, Val, true); |
Chris Lattner | 18f1609 | 2004-04-19 18:07:02 +0000 | [diff] [blame] | 913 | } |
| 914 | |
Chris Lattner | 52221f7 | 2006-02-17 00:31:07 +0000 | [diff] [blame] | 915 | /// RemoveFromWorklist - Remove all instances of I from the worklist vector |
| 916 | /// specified. |
Andrew Trick | 64c0748 | 2012-04-10 05:14:37 +0000 | [diff] [blame] | 917 | static void RemoveFromWorklist(Instruction *I, |
Chris Lattner | 52221f7 | 2006-02-17 00:31:07 +0000 | [diff] [blame] | 918 | std::vector<Instruction*> &Worklist) { |
Jakub Staszak | be6f884 | 2012-10-16 19:52:32 +0000 | [diff] [blame] | 919 | |
| 920 | Worklist.erase(std::remove(Worklist.begin(), Worklist.end(), I), |
| 921 | Worklist.end()); |
Chris Lattner | 52221f7 | 2006-02-17 00:31:07 +0000 | [diff] [blame] | 922 | } |
| 923 | |
| 924 | /// ReplaceUsesOfWith - When we find that I really equals V, remove I from the |
| 925 | /// program, replacing all uses with V and update the worklist. |
Andrew Trick | 64c0748 | 2012-04-10 05:14:37 +0000 | [diff] [blame] | 926 | static void ReplaceUsesOfWith(Instruction *I, Value *V, |
Devang Patel | 15c260a | 2007-07-31 08:03:26 +0000 | [diff] [blame] | 927 | std::vector<Instruction*> &Worklist, |
| 928 | Loop *L, LPPassManager *LPM) { |
David Greene | d4c56fb | 2010-01-05 01:27:04 +0000 | [diff] [blame] | 929 | DEBUG(dbgs() << "Replace with '" << *V << "': " << *I); |
Chris Lattner | 52221f7 | 2006-02-17 00:31:07 +0000 | [diff] [blame] | 930 | |
| 931 | // Add uses to the worklist, which may be dead now. |
| 932 | for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) |
| 933 | if (Instruction *Use = dyn_cast<Instruction>(I->getOperand(i))) |
| 934 | Worklist.push_back(Use); |
| 935 | |
| 936 | // Add users to the worklist which may be simplified now. |
| 937 | for (Value::use_iterator UI = I->use_begin(), E = I->use_end(); |
| 938 | UI != E; ++UI) |
| 939 | Worklist.push_back(cast<Instruction>(*UI)); |
Devang Patel | 15c260a | 2007-07-31 08:03:26 +0000 | [diff] [blame] | 940 | LPM->deleteSimpleAnalysisValue(I, L); |
Chris Lattner | 52221f7 | 2006-02-17 00:31:07 +0000 | [diff] [blame] | 941 | RemoveFromWorklist(I, Worklist); |
Devang Patel | 9ee49c5 | 2007-09-20 23:45:50 +0000 | [diff] [blame] | 942 | I->replaceAllUsesWith(V); |
| 943 | I->eraseFromParent(); |
Chris Lattner | 52221f7 | 2006-02-17 00:31:07 +0000 | [diff] [blame] | 944 | ++NumSimplify; |
| 945 | } |
| 946 | |
Chris Lattner | a6fc94b | 2006-02-18 07:57:38 +0000 | [diff] [blame] | 947 | /// RemoveLoopFromHierarchy - We have discovered that the specified loop has |
| 948 | /// become unwrapped, either because the backedge was deleted, or because the |
| 949 | /// edge into the header was removed. If the edge into the header from the |
| 950 | /// latch block was removed, the loop is unwrapped but subloops are still alive, |
| 951 | /// so they just reparent loops. If the loops are actually dead, they will be |
| 952 | /// removed later. |
| 953 | void LoopUnswitch::RemoveLoopFromHierarchy(Loop *L) { |
Devang Patel | 1bc8936 | 2007-03-07 00:26:10 +0000 | [diff] [blame] | 954 | LPM->deleteLoopFromQueue(L); |
Chris Lattner | a6fc94b | 2006-02-18 07:57:38 +0000 | [diff] [blame] | 955 | RemoveLoopFromWorklist(L); |
| 956 | } |
| 957 | |
Chris Lattner | c235809 | 2006-02-11 00:43:37 +0000 | [diff] [blame] | 958 | // RewriteLoopBodyWithConditionConstant - We know either that the value LIC has |
| 959 | // the value specified by Val in the specified loop, or we know it does NOT have |
| 960 | // that value. Rewrite any uses of LIC or of properties correlated to it. |
Chris Lattner | 18f1609 | 2004-04-19 18:07:02 +0000 | [diff] [blame] | 961 | void LoopUnswitch::RewriteLoopBodyWithConditionConstant(Loop *L, Value *LIC, |
Chris Lattner | c235809 | 2006-02-11 00:43:37 +0000 | [diff] [blame] | 962 | Constant *Val, |
| 963 | bool IsEqual) { |
Chris Lattner | 4c41d49 | 2006-02-10 01:24:09 +0000 | [diff] [blame] | 964 | assert(!isa<Constant>(LIC) && "Why are we unswitching on a constant?"); |
Andrew Trick | 64c0748 | 2012-04-10 05:14:37 +0000 | [diff] [blame] | 965 | |
Chris Lattner | 18f1609 | 2004-04-19 18:07:02 +0000 | [diff] [blame] | 966 | // FIXME: Support correlated properties, like: |
| 967 | // for (...) |
| 968 | // if (li1 < li2) |
| 969 | // ... |
| 970 | // if (li1 > li2) |
| 971 | // ... |
Andrew Trick | 64c0748 | 2012-04-10 05:14:37 +0000 | [diff] [blame] | 972 | |
Chris Lattner | 708e1a5 | 2006-02-10 02:30:37 +0000 | [diff] [blame] | 973 | // FOLD boolean conditions (X|LIC), (X&LIC). Fold conditional branches, |
| 974 | // selects, switches. |
Chris Lattner | 52221f7 | 2006-02-17 00:31:07 +0000 | [diff] [blame] | 975 | std::vector<Instruction*> Worklist; |
Owen Anderson | e922c02 | 2009-07-22 00:24:57 +0000 | [diff] [blame] | 976 | LLVMContext &Context = Val->getContext(); |
| 977 | |
Chris Lattner | 52221f7 | 2006-02-17 00:31:07 +0000 | [diff] [blame] | 978 | // If we know that LIC == Val, or that LIC == NotVal, just replace uses of LIC |
| 979 | // in the loop with the appropriate one directly. |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 980 | if (IsEqual || (isa<ConstantInt>(Val) && |
Duncan Sands | b0bc6c3 | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 981 | Val->getType()->isIntegerTy(1))) { |
Chris Lattner | bd28e3f | 2006-02-22 06:37:14 +0000 | [diff] [blame] | 982 | Value *Replacement; |
| 983 | if (IsEqual) |
| 984 | Replacement = Val; |
| 985 | else |
Andrew Trick | 64c0748 | 2012-04-10 05:14:37 +0000 | [diff] [blame] | 986 | Replacement = ConstantInt::get(Type::getInt1Ty(Val->getContext()), |
Reid Spencer | 579dca1 | 2007-01-12 04:24:46 +0000 | [diff] [blame] | 987 | !cast<ConstantInt>(Val)->getZExtValue()); |
Andrew Trick | 64c0748 | 2012-04-10 05:14:37 +0000 | [diff] [blame] | 988 | |
Evan Cheng | 424641e | 2011-05-24 23:12:57 +0000 | [diff] [blame] | 989 | for (Value::use_iterator UI = LIC->use_begin(), E = LIC->use_end(); |
| 990 | UI != E; ++UI) { |
Evan Cheng | 21d378d | 2011-05-24 23:47:50 +0000 | [diff] [blame] | 991 | Instruction *U = dyn_cast<Instruction>(*UI); |
| 992 | if (!U || !L->contains(U)) |
Evan Cheng | 424641e | 2011-05-24 23:12:57 +0000 | [diff] [blame] | 993 | continue; |
Evan Cheng | 424641e | 2011-05-24 23:12:57 +0000 | [diff] [blame] | 994 | Worklist.push_back(U); |
| 995 | } |
Andrew Trick | 64c0748 | 2012-04-10 05:14:37 +0000 | [diff] [blame] | 996 | |
Jakub Staszak | 7198ee6 | 2013-08-06 17:03:42 +0000 | [diff] [blame] | 997 | for (std::vector<Instruction*>::iterator UI = Worklist.begin(), |
| 998 | UE = Worklist.end(); UI != UE; ++UI) |
Andrew Trick | 64c0748 | 2012-04-10 05:14:37 +0000 | [diff] [blame] | 999 | (*UI)->replaceUsesOfWith(LIC, Replacement); |
| 1000 | |
Chris Lattner | 9e18580 | 2010-04-05 21:18:32 +0000 | [diff] [blame] | 1001 | SimplifyCode(Worklist, L); |
| 1002 | return; |
| 1003 | } |
Andrew Trick | 64c0748 | 2012-04-10 05:14:37 +0000 | [diff] [blame] | 1004 | |
Chris Lattner | 9e18580 | 2010-04-05 21:18:32 +0000 | [diff] [blame] | 1005 | // Otherwise, we don't know the precise value of LIC, but we do know that it |
| 1006 | // is certainly NOT "Val". As such, simplify any uses in the loop that we |
| 1007 | // can. This case occurs when we unswitch switch statements. |
Evan Cheng | 424641e | 2011-05-24 23:12:57 +0000 | [diff] [blame] | 1008 | for (Value::use_iterator UI = LIC->use_begin(), E = LIC->use_end(); |
| 1009 | UI != E; ++UI) { |
Evan Cheng | 21d378d | 2011-05-24 23:47:50 +0000 | [diff] [blame] | 1010 | Instruction *U = dyn_cast<Instruction>(*UI); |
| 1011 | if (!U || !L->contains(U)) |
Chris Lattner | 9e18580 | 2010-04-05 21:18:32 +0000 | [diff] [blame] | 1012 | continue; |
Chris Lattner | 52221f7 | 2006-02-17 00:31:07 +0000 | [diff] [blame] | 1013 | |
Chris Lattner | 9e18580 | 2010-04-05 21:18:32 +0000 | [diff] [blame] | 1014 | Worklist.push_back(U); |
Chris Lattner | 52221f7 | 2006-02-17 00:31:07 +0000 | [diff] [blame] | 1015 | |
Andrew Trick | 64c0748 | 2012-04-10 05:14:37 +0000 | [diff] [blame] | 1016 | // TODO: We could do other simplifications, for example, turning |
Chris Lattner | 9e18580 | 2010-04-05 21:18:32 +0000 | [diff] [blame] | 1017 | // 'icmp eq LIC, Val' -> false. |
| 1018 | |
| 1019 | // If we know that LIC is not Val, use this info to simplify code. |
| 1020 | SwitchInst *SI = dyn_cast<SwitchInst>(U); |
| 1021 | if (SI == 0 || !isa<ConstantInt>(Val)) continue; |
Andrew Trick | 64c0748 | 2012-04-10 05:14:37 +0000 | [diff] [blame] | 1022 | |
Stepan Dyatkovskiy | c10fa6c | 2012-03-08 07:06:20 +0000 | [diff] [blame] | 1023 | SwitchInst::CaseIt DeadCase = SI->findCaseValue(cast<ConstantInt>(Val)); |
Stepan Dyatkovskiy | 2447312 | 2012-02-01 07:49:51 +0000 | [diff] [blame] | 1024 | // Default case is live for multiple values. |
Stepan Dyatkovskiy | 3d3abe0 | 2012-03-11 06:09:17 +0000 | [diff] [blame] | 1025 | if (DeadCase == SI->case_default()) continue; |
Andrew Trick | 64c0748 | 2012-04-10 05:14:37 +0000 | [diff] [blame] | 1026 | |
| 1027 | // Found a dead case value. Don't remove PHI nodes in the |
Chris Lattner | 9e18580 | 2010-04-05 21:18:32 +0000 | [diff] [blame] | 1028 | // successor if they become single-entry, those PHI nodes may |
| 1029 | // be in the Users list. |
Nick Lewycky | 444f297 | 2011-06-03 06:27:15 +0000 | [diff] [blame] | 1030 | |
Evan Cheng | 424641e | 2011-05-24 23:12:57 +0000 | [diff] [blame] | 1031 | BasicBlock *Switch = SI->getParent(); |
Stepan Dyatkovskiy | c10fa6c | 2012-03-08 07:06:20 +0000 | [diff] [blame] | 1032 | BasicBlock *SISucc = DeadCase.getCaseSuccessor(); |
Evan Cheng | 424641e | 2011-05-24 23:12:57 +0000 | [diff] [blame] | 1033 | BasicBlock *Latch = L->getLoopLatch(); |
Andrew Trick | 64c0748 | 2012-04-10 05:14:37 +0000 | [diff] [blame] | 1034 | |
Stepan Dyatkovskiy | 209287d | 2012-01-15 09:44:07 +0000 | [diff] [blame] | 1035 | BranchesInfo.setUnswitched(SI, Val); |
Andrew Trick | 64c0748 | 2012-04-10 05:14:37 +0000 | [diff] [blame] | 1036 | |
Nick Lewycky | 444f297 | 2011-06-03 06:27:15 +0000 | [diff] [blame] | 1037 | if (!SI->findCaseDest(SISucc)) continue; // Edge is critical. |
Evan Cheng | 237d10d | 2011-05-25 18:17:13 +0000 | [diff] [blame] | 1038 | // If the DeadCase successor dominates the loop latch, then the |
| 1039 | // transformation isn't safe since it will delete the sole predecessor edge |
| 1040 | // to the latch. |
| 1041 | if (Latch && DT->dominates(SISucc, Latch)) |
| 1042 | continue; |
Evan Cheng | 424641e | 2011-05-24 23:12:57 +0000 | [diff] [blame] | 1043 | |
Chris Lattner | 9e18580 | 2010-04-05 21:18:32 +0000 | [diff] [blame] | 1044 | // FIXME: This is a hack. We need to keep the successor around |
| 1045 | // and hooked up so as to preserve the loop structure, because |
| 1046 | // trying to update it is complicated. So instead we preserve the |
| 1047 | // loop structure and put the block on a dead code path. |
Evan Cheng | 424641e | 2011-05-24 23:12:57 +0000 | [diff] [blame] | 1048 | SplitEdge(Switch, SISucc, this); |
Chris Lattner | 9e18580 | 2010-04-05 21:18:32 +0000 | [diff] [blame] | 1049 | // Compute the successors instead of relying on the return value |
| 1050 | // of SplitEdge, since it may have split the switch successor |
| 1051 | // after PHI nodes. |
Stepan Dyatkovskiy | c10fa6c | 2012-03-08 07:06:20 +0000 | [diff] [blame] | 1052 | BasicBlock *NewSISucc = DeadCase.getCaseSuccessor(); |
Chris Lattner | 9e18580 | 2010-04-05 21:18:32 +0000 | [diff] [blame] | 1053 | BasicBlock *OldSISucc = *succ_begin(NewSISucc); |
| 1054 | // Create an "unreachable" destination. |
| 1055 | BasicBlock *Abort = BasicBlock::Create(Context, "us-unreachable", |
| 1056 | Switch->getParent(), |
| 1057 | OldSISucc); |
| 1058 | new UnreachableInst(Context, Abort); |
| 1059 | // Force the new case destination to branch to the "unreachable" |
| 1060 | // block while maintaining a (dead) CFG edge to the old block. |
| 1061 | NewSISucc->getTerminator()->eraseFromParent(); |
| 1062 | BranchInst::Create(Abort, OldSISucc, |
| 1063 | ConstantInt::getTrue(Context), NewSISucc); |
| 1064 | // Release the PHI operands for this edge. |
| 1065 | for (BasicBlock::iterator II = NewSISucc->begin(); |
| 1066 | PHINode *PN = dyn_cast<PHINode>(II); ++II) |
| 1067 | PN->setIncomingValue(PN->getBasicBlockIndex(Switch), |
| 1068 | UndefValue::get(PN->getType())); |
| 1069 | // Tell the domtree about the new block. We don't fully update the |
| 1070 | // domtree here -- instead we force it to do a full recomputation |
| 1071 | // after the pass is complete -- but we do need to inform it of |
| 1072 | // new blocks. |
| 1073 | if (DT) |
| 1074 | DT->addNewBlock(Abort, NewSISucc); |
Chris Lattner | 52221f7 | 2006-02-17 00:31:07 +0000 | [diff] [blame] | 1075 | } |
Andrew Trick | 64c0748 | 2012-04-10 05:14:37 +0000 | [diff] [blame] | 1076 | |
Devang Patel | 15c260a | 2007-07-31 08:03:26 +0000 | [diff] [blame] | 1077 | SimplifyCode(Worklist, L); |
Chris Lattner | a6fc94b | 2006-02-18 07:57:38 +0000 | [diff] [blame] | 1078 | } |
| 1079 | |
Mike Stump | c02f0d7 | 2009-09-09 17:57:16 +0000 | [diff] [blame] | 1080 | /// SimplifyCode - Okay, now that we have simplified some instructions in the |
Chris Lattner | a6fc94b | 2006-02-18 07:57:38 +0000 | [diff] [blame] | 1081 | /// loop, walk over it and constant prop, dce, and fold control flow where |
| 1082 | /// possible. Note that this is effectively a very simple loop-structure-aware |
| 1083 | /// optimizer. During processing of this loop, L could very well be deleted, so |
| 1084 | /// it must not be used. |
| 1085 | /// |
| 1086 | /// FIXME: When the loop optimizer is more mature, separate this out to a new |
| 1087 | /// pass. |
| 1088 | /// |
Devang Patel | 15c260a | 2007-07-31 08:03:26 +0000 | [diff] [blame] | 1089 | void LoopUnswitch::SimplifyCode(std::vector<Instruction*> &Worklist, Loop *L) { |
Chris Lattner | 52221f7 | 2006-02-17 00:31:07 +0000 | [diff] [blame] | 1090 | while (!Worklist.empty()) { |
| 1091 | Instruction *I = Worklist.back(); |
| 1092 | Worklist.pop_back(); |
Duncan Sands | 0f82763 | 2010-11-23 20:24:21 +0000 | [diff] [blame] | 1093 | |
Chris Lattner | 52221f7 | 2006-02-17 00:31:07 +0000 | [diff] [blame] | 1094 | // Simple DCE. |
| 1095 | if (isInstructionTriviallyDead(I)) { |
David Greene | d4c56fb | 2010-01-05 01:27:04 +0000 | [diff] [blame] | 1096 | DEBUG(dbgs() << "Remove dead instruction '" << *I); |
Andrew Trick | 64c0748 | 2012-04-10 05:14:37 +0000 | [diff] [blame] | 1097 | |
Chris Lattner | 52221f7 | 2006-02-17 00:31:07 +0000 | [diff] [blame] | 1098 | // Add uses to the worklist, which may be dead now. |
| 1099 | for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) |
| 1100 | if (Instruction *Use = dyn_cast<Instruction>(I->getOperand(i))) |
| 1101 | Worklist.push_back(Use); |
Devang Patel | 15c260a | 2007-07-31 08:03:26 +0000 | [diff] [blame] | 1102 | LPM->deleteSimpleAnalysisValue(I, L); |
Chris Lattner | 52221f7 | 2006-02-17 00:31:07 +0000 | [diff] [blame] | 1103 | RemoveFromWorklist(I, Worklist); |
Devang Patel | 9ee49c5 | 2007-09-20 23:45:50 +0000 | [diff] [blame] | 1104 | I->eraseFromParent(); |
Chris Lattner | 52221f7 | 2006-02-17 00:31:07 +0000 | [diff] [blame] | 1105 | ++NumSimplify; |
| 1106 | continue; |
| 1107 | } |
Duncan Sands | d0c6f3d | 2010-11-18 19:59:41 +0000 | [diff] [blame] | 1108 | |
Chris Lattner | 4f1f6f6 | 2010-04-20 05:33:18 +0000 | [diff] [blame] | 1109 | // See if instruction simplification can hack this up. This is common for |
| 1110 | // things like "select false, X, Y" after unswitching made the condition be |
Peter Collingbourne | fcaf5fa | 2012-05-20 01:32:09 +0000 | [diff] [blame] | 1111 | // 'false'. TODO: update the domtree properly so we can pass it here. |
| 1112 | if (Value *V = SimplifyInstruction(I)) |
Duncan Sands | d0c6f3d | 2010-11-18 19:59:41 +0000 | [diff] [blame] | 1113 | if (LI->replacementPreservesLCSSAForm(I, V)) { |
| 1114 | ReplaceUsesOfWith(I, V, Worklist, L, LPM); |
| 1115 | continue; |
| 1116 | } |
| 1117 | |
Chris Lattner | 52221f7 | 2006-02-17 00:31:07 +0000 | [diff] [blame] | 1118 | // Special case hacks that appear commonly in unswitched code. |
Chris Lattner | 4f1f6f6 | 2010-04-20 05:33:18 +0000 | [diff] [blame] | 1119 | if (BranchInst *BI = dyn_cast<BranchInst>(I)) { |
Chris Lattner | 52221f7 | 2006-02-17 00:31:07 +0000 | [diff] [blame] | 1120 | if (BI->isUnconditional()) { |
| 1121 | // If BI's parent is the only pred of the successor, fold the two blocks |
| 1122 | // together. |
| 1123 | BasicBlock *Pred = BI->getParent(); |
| 1124 | BasicBlock *Succ = BI->getSuccessor(0); |
| 1125 | BasicBlock *SinglePred = Succ->getSinglePredecessor(); |
| 1126 | if (!SinglePred) continue; // Nothing to do. |
| 1127 | assert(SinglePred == Pred && "CFG broken"); |
| 1128 | |
Andrew Trick | 64c0748 | 2012-04-10 05:14:37 +0000 | [diff] [blame] | 1129 | DEBUG(dbgs() << "Merging blocks: " << Pred->getName() << " <- " |
Daniel Dunbar | ce63ffb | 2009-07-25 00:23:56 +0000 | [diff] [blame] | 1130 | << Succ->getName() << "\n"); |
Andrew Trick | 64c0748 | 2012-04-10 05:14:37 +0000 | [diff] [blame] | 1131 | |
Chris Lattner | 52221f7 | 2006-02-17 00:31:07 +0000 | [diff] [blame] | 1132 | // Resolve any single entry PHI nodes in Succ. |
| 1133 | while (PHINode *PN = dyn_cast<PHINode>(Succ->begin())) |
Devang Patel | 15c260a | 2007-07-31 08:03:26 +0000 | [diff] [blame] | 1134 | ReplaceUsesOfWith(PN, PN->getIncomingValue(0), Worklist, L, LPM); |
Andrew Trick | 64c0748 | 2012-04-10 05:14:37 +0000 | [diff] [blame] | 1135 | |
Jay Foad | 95c3e48 | 2011-06-23 09:09:15 +0000 | [diff] [blame] | 1136 | // If Succ has any successors with PHI nodes, update them to have |
| 1137 | // entries coming from Pred instead of Succ. |
| 1138 | Succ->replaceAllUsesWith(Pred); |
Andrew Trick | 64c0748 | 2012-04-10 05:14:37 +0000 | [diff] [blame] | 1139 | |
Chris Lattner | 52221f7 | 2006-02-17 00:31:07 +0000 | [diff] [blame] | 1140 | // Move all of the successor contents from Succ to Pred. |
| 1141 | Pred->getInstList().splice(BI, Succ->getInstList(), Succ->begin(), |
| 1142 | Succ->end()); |
Devang Patel | 15c260a | 2007-07-31 08:03:26 +0000 | [diff] [blame] | 1143 | LPM->deleteSimpleAnalysisValue(BI, L); |
Devang Patel | 9ee49c5 | 2007-09-20 23:45:50 +0000 | [diff] [blame] | 1144 | BI->eraseFromParent(); |
Chris Lattner | 52221f7 | 2006-02-17 00:31:07 +0000 | [diff] [blame] | 1145 | RemoveFromWorklist(BI, Worklist); |
Andrew Trick | 64c0748 | 2012-04-10 05:14:37 +0000 | [diff] [blame] | 1146 | |
Chris Lattner | 52221f7 | 2006-02-17 00:31:07 +0000 | [diff] [blame] | 1147 | // Remove Succ from the loop tree. |
| 1148 | LI->removeBlock(Succ); |
Devang Patel | 15c260a | 2007-07-31 08:03:26 +0000 | [diff] [blame] | 1149 | LPM->deleteSimpleAnalysisValue(Succ, L); |
Devang Patel | 9ee49c5 | 2007-09-20 23:45:50 +0000 | [diff] [blame] | 1150 | Succ->eraseFromParent(); |
Chris Lattner | f4412d8 | 2006-02-18 01:27:45 +0000 | [diff] [blame] | 1151 | ++NumSimplify; |
Chris Lattner | 4f1f6f6 | 2010-04-20 05:33:18 +0000 | [diff] [blame] | 1152 | continue; |
Chris Lattner | 9e18580 | 2010-04-05 21:18:32 +0000 | [diff] [blame] | 1153 | } |
Andrew Trick | 64c0748 | 2012-04-10 05:14:37 +0000 | [diff] [blame] | 1154 | |
Chris Lattner | 4f1f6f6 | 2010-04-20 05:33:18 +0000 | [diff] [blame] | 1155 | continue; |
Chris Lattner | 52221f7 | 2006-02-17 00:31:07 +0000 | [diff] [blame] | 1156 | } |
| 1157 | } |
Chris Lattner | 18f1609 | 2004-04-19 18:07:02 +0000 | [diff] [blame] | 1158 | } |