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