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