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