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