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