Hal Finkel | bf45efd | 2013-11-16 23:59:05 +0000 | [diff] [blame] | 1 | //===-- LoopReroll.cpp - Loop rerolling pass ------------------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This pass implements a simple loop reroller. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Hal Finkel | bf45efd | 2013-11-16 23:59:05 +0000 | [diff] [blame] | 14 | #include "llvm/Transforms/Scalar.h" |
James Molloy | 64419d4 | 2015-01-29 21:52:03 +0000 | [diff] [blame] | 15 | #include "llvm/ADT/MapVector.h" |
Chandler Carruth | 8a8cd2b | 2014-01-07 11:48:04 +0000 | [diff] [blame] | 16 | #include "llvm/ADT/STLExtras.h" |
Elena Demikhovsky | 9914dbd | 2016-02-22 09:38:28 +0000 | [diff] [blame] | 17 | #include "llvm/ADT/BitVector.h" |
Hal Finkel | bf45efd | 2013-11-16 23:59:05 +0000 | [diff] [blame] | 18 | #include "llvm/ADT/SmallSet.h" |
| 19 | #include "llvm/ADT/Statistic.h" |
Hal Finkel | bf45efd | 2013-11-16 23:59:05 +0000 | [diff] [blame] | 20 | #include "llvm/Analysis/AliasAnalysis.h" |
| 21 | #include "llvm/Analysis/AliasSetTracker.h" |
| 22 | #include "llvm/Analysis/LoopPass.h" |
| 23 | #include "llvm/Analysis/ScalarEvolution.h" |
| 24 | #include "llvm/Analysis/ScalarEvolutionExpander.h" |
| 25 | #include "llvm/Analysis/ScalarEvolutionExpressions.h" |
Benjamin Kramer | 799003b | 2015-03-23 19:32:43 +0000 | [diff] [blame] | 26 | #include "llvm/Analysis/TargetLibraryInfo.h" |
Hal Finkel | bf45efd | 2013-11-16 23:59:05 +0000 | [diff] [blame] | 27 | #include "llvm/Analysis/ValueTracking.h" |
| 28 | #include "llvm/IR/DataLayout.h" |
Chandler Carruth | 5ad5f15 | 2014-01-13 09:26:24 +0000 | [diff] [blame] | 29 | #include "llvm/IR/Dominators.h" |
Hal Finkel | bf45efd | 2013-11-16 23:59:05 +0000 | [diff] [blame] | 30 | #include "llvm/IR/IntrinsicInst.h" |
| 31 | #include "llvm/Support/CommandLine.h" |
| 32 | #include "llvm/Support/Debug.h" |
| 33 | #include "llvm/Support/raw_ostream.h" |
Hal Finkel | bf45efd | 2013-11-16 23:59:05 +0000 | [diff] [blame] | 34 | #include "llvm/Transforms/Utils/BasicBlockUtils.h" |
| 35 | #include "llvm/Transforms/Utils/Local.h" |
| 36 | #include "llvm/Transforms/Utils/LoopUtils.h" |
| 37 | |
| 38 | using namespace llvm; |
| 39 | |
Chandler Carruth | 964daaa | 2014-04-22 02:55:47 +0000 | [diff] [blame] | 40 | #define DEBUG_TYPE "loop-reroll" |
| 41 | |
Hal Finkel | bf45efd | 2013-11-16 23:59:05 +0000 | [diff] [blame] | 42 | STATISTIC(NumRerolledLoops, "Number of rerolled loops"); |
| 43 | |
| 44 | static cl::opt<unsigned> |
| 45 | MaxInc("max-reroll-increment", cl::init(2048), cl::Hidden, |
| 46 | cl::desc("The maximum increment for loop rerolling")); |
| 47 | |
James Molloy | e805ad9 | 2015-02-12 15:54:14 +0000 | [diff] [blame] | 48 | static cl::opt<unsigned> |
| 49 | NumToleratedFailedMatches("reroll-num-tolerated-failed-matches", cl::init(400), |
| 50 | cl::Hidden, |
| 51 | cl::desc("The maximum number of failures to tolerate" |
| 52 | " during fuzzy matching. (default: 400)")); |
| 53 | |
Hal Finkel | bf45efd | 2013-11-16 23:59:05 +0000 | [diff] [blame] | 54 | // This loop re-rolling transformation aims to transform loops like this: |
| 55 | // |
| 56 | // int foo(int a); |
| 57 | // void bar(int *x) { |
| 58 | // for (int i = 0; i < 500; i += 3) { |
| 59 | // foo(i); |
| 60 | // foo(i+1); |
| 61 | // foo(i+2); |
| 62 | // } |
| 63 | // } |
| 64 | // |
| 65 | // into a loop like this: |
| 66 | // |
| 67 | // void bar(int *x) { |
| 68 | // for (int i = 0; i < 500; ++i) |
| 69 | // foo(i); |
| 70 | // } |
| 71 | // |
| 72 | // It does this by looking for loops that, besides the latch code, are composed |
| 73 | // of isomorphic DAGs of instructions, with each DAG rooted at some increment |
| 74 | // to the induction variable, and where each DAG is isomorphic to the DAG |
| 75 | // rooted at the induction variable (excepting the sub-DAGs which root the |
| 76 | // other induction-variable increments). In other words, we're looking for loop |
| 77 | // bodies of the form: |
| 78 | // |
| 79 | // %iv = phi [ (preheader, ...), (body, %iv.next) ] |
| 80 | // f(%iv) |
| 81 | // %iv.1 = add %iv, 1 <-- a root increment |
| 82 | // f(%iv.1) |
| 83 | // %iv.2 = add %iv, 2 <-- a root increment |
| 84 | // f(%iv.2) |
| 85 | // %iv.scale_m_1 = add %iv, scale-1 <-- a root increment |
| 86 | // f(%iv.scale_m_1) |
| 87 | // ... |
| 88 | // %iv.next = add %iv, scale |
| 89 | // %cmp = icmp(%iv, ...) |
| 90 | // br %cmp, header, exit |
| 91 | // |
| 92 | // where each f(i) is a set of instructions that, collectively, are a function |
| 93 | // only of i (and other loop-invariant values). |
| 94 | // |
| 95 | // As a special case, we can also reroll loops like this: |
| 96 | // |
| 97 | // int foo(int); |
| 98 | // void bar(int *x) { |
| 99 | // for (int i = 0; i < 500; ++i) { |
| 100 | // x[3*i] = foo(0); |
| 101 | // x[3*i+1] = foo(0); |
| 102 | // x[3*i+2] = foo(0); |
| 103 | // } |
| 104 | // } |
| 105 | // |
| 106 | // into this: |
| 107 | // |
| 108 | // void bar(int *x) { |
| 109 | // for (int i = 0; i < 1500; ++i) |
| 110 | // x[i] = foo(0); |
| 111 | // } |
| 112 | // |
| 113 | // in which case, we're looking for inputs like this: |
| 114 | // |
| 115 | // %iv = phi [ (preheader, ...), (body, %iv.next) ] |
| 116 | // %scaled.iv = mul %iv, scale |
| 117 | // f(%scaled.iv) |
| 118 | // %scaled.iv.1 = add %scaled.iv, 1 |
| 119 | // f(%scaled.iv.1) |
| 120 | // %scaled.iv.2 = add %scaled.iv, 2 |
| 121 | // f(%scaled.iv.2) |
| 122 | // %scaled.iv.scale_m_1 = add %scaled.iv, scale-1 |
| 123 | // f(%scaled.iv.scale_m_1) |
| 124 | // ... |
| 125 | // %iv.next = add %iv, 1 |
| 126 | // %cmp = icmp(%iv, ...) |
| 127 | // br %cmp, header, exit |
| 128 | |
| 129 | namespace { |
James Molloy | 64419d4 | 2015-01-29 21:52:03 +0000 | [diff] [blame] | 130 | enum IterationLimits { |
Elena Demikhovsky | 9914dbd | 2016-02-22 09:38:28 +0000 | [diff] [blame] | 131 | /// The maximum number of iterations that we'll try and reroll. |
| 132 | IL_MaxRerollIterations = 32, |
James Molloy | 64419d4 | 2015-01-29 21:52:03 +0000 | [diff] [blame] | 133 | /// The bitvector index used by loop induction variables and other |
James Molloy | f147359 | 2015-02-11 09:19:47 +0000 | [diff] [blame] | 134 | /// instructions that belong to all iterations. |
| 135 | IL_All, |
James Molloy | 64419d4 | 2015-01-29 21:52:03 +0000 | [diff] [blame] | 136 | IL_End |
| 137 | }; |
| 138 | |
Hal Finkel | bf45efd | 2013-11-16 23:59:05 +0000 | [diff] [blame] | 139 | class LoopReroll : public LoopPass { |
| 140 | public: |
| 141 | static char ID; // Pass ID, replacement for typeid |
| 142 | LoopReroll() : LoopPass(ID) { |
| 143 | initializeLoopRerollPass(*PassRegistry::getPassRegistry()); |
| 144 | } |
| 145 | |
Craig Topper | 3e4c697 | 2014-03-05 09:10:37 +0000 | [diff] [blame] | 146 | bool runOnLoop(Loop *L, LPPassManager &LPM) override; |
Hal Finkel | bf45efd | 2013-11-16 23:59:05 +0000 | [diff] [blame] | 147 | |
Craig Topper | 3e4c697 | 2014-03-05 09:10:37 +0000 | [diff] [blame] | 148 | void getAnalysisUsage(AnalysisUsage &AU) const override { |
Chandler Carruth | b98f63d | 2015-01-15 10:41:28 +0000 | [diff] [blame] | 149 | AU.addRequired<TargetLibraryInfoWrapperPass>(); |
Chandler Carruth | 31088a9 | 2016-02-19 10:45:18 +0000 | [diff] [blame] | 150 | getLoopAnalysisUsage(AU); |
Hal Finkel | bf45efd | 2013-11-16 23:59:05 +0000 | [diff] [blame] | 151 | } |
| 152 | |
James Molloy | 64419d4 | 2015-01-29 21:52:03 +0000 | [diff] [blame] | 153 | protected: |
Hal Finkel | bf45efd | 2013-11-16 23:59:05 +0000 | [diff] [blame] | 154 | AliasAnalysis *AA; |
| 155 | LoopInfo *LI; |
| 156 | ScalarEvolution *SE; |
Hal Finkel | bf45efd | 2013-11-16 23:59:05 +0000 | [diff] [blame] | 157 | TargetLibraryInfo *TLI; |
| 158 | DominatorTree *DT; |
Justin Bogner | 843fb20 | 2015-12-15 19:40:57 +0000 | [diff] [blame] | 159 | bool PreserveLCSSA; |
Hal Finkel | bf45efd | 2013-11-16 23:59:05 +0000 | [diff] [blame] | 160 | |
| 161 | typedef SmallVector<Instruction *, 16> SmallInstructionVector; |
| 162 | typedef SmallSet<Instruction *, 16> SmallInstructionSet; |
| 163 | |
Lawrence Hu | dc8a83b | 2015-07-24 22:01:49 +0000 | [diff] [blame] | 164 | // Map between induction variable and its increment |
| 165 | DenseMap<Instruction *, int64_t> IVToIncMap; |
Lawrence Hu | 1befea2 | 2016-04-30 00:51:22 +0000 | [diff] [blame^] | 166 | // For loop with multiple induction variable, remember the one used only to |
| 167 | // control the loop. |
| 168 | Instruction *LoopControlIV; |
Lawrence Hu | dc8a83b | 2015-07-24 22:01:49 +0000 | [diff] [blame] | 169 | |
| 170 | // A chain of isomorphic instructions, identified by a single-use PHI |
Hal Finkel | bf45efd | 2013-11-16 23:59:05 +0000 | [diff] [blame] | 171 | // representing a reduction. Only the last value may be used outside the |
| 172 | // loop. |
| 173 | struct SimpleLoopReduction { |
| 174 | SimpleLoopReduction(Instruction *P, Loop *L) |
| 175 | : Valid(false), Instructions(1, P) { |
| 176 | assert(isa<PHINode>(P) && "First reduction instruction must be a PHI"); |
| 177 | add(L); |
| 178 | } |
| 179 | |
| 180 | bool valid() const { |
| 181 | return Valid; |
| 182 | } |
| 183 | |
| 184 | Instruction *getPHI() const { |
| 185 | assert(Valid && "Using invalid reduction"); |
| 186 | return Instructions.front(); |
| 187 | } |
| 188 | |
| 189 | Instruction *getReducedValue() const { |
| 190 | assert(Valid && "Using invalid reduction"); |
| 191 | return Instructions.back(); |
| 192 | } |
| 193 | |
| 194 | Instruction *get(size_t i) const { |
| 195 | assert(Valid && "Using invalid reduction"); |
| 196 | return Instructions[i+1]; |
| 197 | } |
| 198 | |
| 199 | Instruction *operator [] (size_t i) const { return get(i); } |
| 200 | |
| 201 | // The size, ignoring the initial PHI. |
| 202 | size_t size() const { |
| 203 | assert(Valid && "Using invalid reduction"); |
| 204 | return Instructions.size()-1; |
| 205 | } |
| 206 | |
| 207 | typedef SmallInstructionVector::iterator iterator; |
| 208 | typedef SmallInstructionVector::const_iterator const_iterator; |
| 209 | |
| 210 | iterator begin() { |
| 211 | assert(Valid && "Using invalid reduction"); |
Benjamin Kramer | b6d0bd4 | 2014-03-02 12:27:27 +0000 | [diff] [blame] | 212 | return std::next(Instructions.begin()); |
Hal Finkel | bf45efd | 2013-11-16 23:59:05 +0000 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | const_iterator begin() const { |
| 216 | assert(Valid && "Using invalid reduction"); |
Benjamin Kramer | b6d0bd4 | 2014-03-02 12:27:27 +0000 | [diff] [blame] | 217 | return std::next(Instructions.begin()); |
Hal Finkel | bf45efd | 2013-11-16 23:59:05 +0000 | [diff] [blame] | 218 | } |
| 219 | |
| 220 | iterator end() { return Instructions.end(); } |
| 221 | const_iterator end() const { return Instructions.end(); } |
| 222 | |
| 223 | protected: |
| 224 | bool Valid; |
| 225 | SmallInstructionVector Instructions; |
| 226 | |
| 227 | void add(Loop *L); |
| 228 | }; |
| 229 | |
| 230 | // The set of all reductions, and state tracking of possible reductions |
| 231 | // during loop instruction processing. |
| 232 | struct ReductionTracker { |
| 233 | typedef SmallVector<SimpleLoopReduction, 16> SmallReductionVector; |
| 234 | |
| 235 | // Add a new possible reduction. |
NAKAMURA Takumi | d0e13af | 2014-10-28 11:54:52 +0000 | [diff] [blame] | 236 | void addSLR(SimpleLoopReduction &SLR) { PossibleReds.push_back(SLR); } |
Hal Finkel | bf45efd | 2013-11-16 23:59:05 +0000 | [diff] [blame] | 237 | |
| 238 | // Setup to track possible reductions corresponding to the provided |
| 239 | // rerolling scale. Only reductions with a number of non-PHI instructions |
| 240 | // that is divisible by the scale are considered. Three instructions sets |
| 241 | // are filled in: |
| 242 | // - A set of all possible instructions in eligible reductions. |
| 243 | // - A set of all PHIs in eligible reductions |
NAKAMURA Takumi | d0e13af | 2014-10-28 11:54:52 +0000 | [diff] [blame] | 244 | // - A set of all reduced values (last instructions) in eligible |
| 245 | // reductions. |
Hal Finkel | bf45efd | 2013-11-16 23:59:05 +0000 | [diff] [blame] | 246 | void restrictToScale(uint64_t Scale, |
| 247 | SmallInstructionSet &PossibleRedSet, |
| 248 | SmallInstructionSet &PossibleRedPHISet, |
| 249 | SmallInstructionSet &PossibleRedLastSet) { |
| 250 | PossibleRedIdx.clear(); |
| 251 | PossibleRedIter.clear(); |
| 252 | Reds.clear(); |
| 253 | |
| 254 | for (unsigned i = 0, e = PossibleReds.size(); i != e; ++i) |
| 255 | if (PossibleReds[i].size() % Scale == 0) { |
| 256 | PossibleRedLastSet.insert(PossibleReds[i].getReducedValue()); |
| 257 | PossibleRedPHISet.insert(PossibleReds[i].getPHI()); |
NAKAMURA Takumi | 335a7bc | 2014-10-28 11:53:30 +0000 | [diff] [blame] | 258 | |
Hal Finkel | bf45efd | 2013-11-16 23:59:05 +0000 | [diff] [blame] | 259 | PossibleRedSet.insert(PossibleReds[i].getPHI()); |
| 260 | PossibleRedIdx[PossibleReds[i].getPHI()] = i; |
NAKAMURA Takumi | 5af50a5 | 2014-10-28 11:54:05 +0000 | [diff] [blame] | 261 | for (Instruction *J : PossibleReds[i]) { |
| 262 | PossibleRedSet.insert(J); |
| 263 | PossibleRedIdx[J] = i; |
Hal Finkel | bf45efd | 2013-11-16 23:59:05 +0000 | [diff] [blame] | 264 | } |
| 265 | } |
| 266 | } |
| 267 | |
| 268 | // The functions below are used while processing the loop instructions. |
| 269 | |
| 270 | // Are the two instructions both from reductions, and furthermore, from |
| 271 | // the same reduction? |
| 272 | bool isPairInSame(Instruction *J1, Instruction *J2) { |
| 273 | DenseMap<Instruction *, int>::iterator J1I = PossibleRedIdx.find(J1); |
| 274 | if (J1I != PossibleRedIdx.end()) { |
| 275 | DenseMap<Instruction *, int>::iterator J2I = PossibleRedIdx.find(J2); |
| 276 | if (J2I != PossibleRedIdx.end() && J1I->second == J2I->second) |
| 277 | return true; |
| 278 | } |
| 279 | |
| 280 | return false; |
| 281 | } |
| 282 | |
| 283 | // The two provided instructions, the first from the base iteration, and |
| 284 | // the second from iteration i, form a matched pair. If these are part of |
| 285 | // a reduction, record that fact. |
| 286 | void recordPair(Instruction *J1, Instruction *J2, unsigned i) { |
| 287 | if (PossibleRedIdx.count(J1)) { |
| 288 | assert(PossibleRedIdx.count(J2) && |
| 289 | "Recording reduction vs. non-reduction instruction?"); |
| 290 | |
| 291 | PossibleRedIter[J1] = 0; |
| 292 | PossibleRedIter[J2] = i; |
| 293 | |
| 294 | int Idx = PossibleRedIdx[J1]; |
| 295 | assert(Idx == PossibleRedIdx[J2] && |
| 296 | "Recording pair from different reductions?"); |
Hal Finkel | 67107ea | 2013-11-17 01:21:54 +0000 | [diff] [blame] | 297 | Reds.insert(Idx); |
Hal Finkel | bf45efd | 2013-11-16 23:59:05 +0000 | [diff] [blame] | 298 | } |
| 299 | } |
| 300 | |
| 301 | // The functions below can be called after we've finished processing all |
| 302 | // instructions in the loop, and we know which reductions were selected. |
| 303 | |
Hal Finkel | bf45efd | 2013-11-16 23:59:05 +0000 | [diff] [blame] | 304 | bool validateSelected(); |
| 305 | void replaceSelected(); |
| 306 | |
| 307 | protected: |
| 308 | // The vector of all possible reductions (for any scale). |
| 309 | SmallReductionVector PossibleReds; |
| 310 | |
| 311 | DenseMap<Instruction *, int> PossibleRedIdx; |
| 312 | DenseMap<Instruction *, int> PossibleRedIter; |
| 313 | DenseSet<int> Reds; |
| 314 | }; |
| 315 | |
James Molloy | f147359 | 2015-02-11 09:19:47 +0000 | [diff] [blame] | 316 | // A DAGRootSet models an induction variable being used in a rerollable |
| 317 | // loop. For example, |
| 318 | // |
| 319 | // x[i*3+0] = y1 |
| 320 | // x[i*3+1] = y2 |
| 321 | // x[i*3+2] = y3 |
| 322 | // |
Lawrence Hu | dc8a83b | 2015-07-24 22:01:49 +0000 | [diff] [blame] | 323 | // Base instruction -> i*3 |
James Molloy | f147359 | 2015-02-11 09:19:47 +0000 | [diff] [blame] | 324 | // +---+----+ |
| 325 | // / | \ |
| 326 | // ST[y1] +1 +2 <-- Roots |
| 327 | // | | |
| 328 | // ST[y2] ST[y3] |
| 329 | // |
| 330 | // There may be multiple DAGRoots, for example: |
| 331 | // |
| 332 | // x[i*2+0] = ... (1) |
| 333 | // x[i*2+1] = ... (1) |
| 334 | // x[i*2+4] = ... (2) |
| 335 | // x[i*2+5] = ... (2) |
| 336 | // x[(i+1234)*2+5678] = ... (3) |
| 337 | // x[(i+1234)*2+5679] = ... (3) |
| 338 | // |
| 339 | // The loop will be rerolled by adding a new loop induction variable, |
| 340 | // one for the Base instruction in each DAGRootSet. |
| 341 | // |
| 342 | struct DAGRootSet { |
| 343 | Instruction *BaseInst; |
| 344 | SmallInstructionVector Roots; |
| 345 | // The instructions between IV and BaseInst (but not including BaseInst). |
| 346 | SmallInstructionSet SubsumedInsts; |
| 347 | }; |
| 348 | |
James Molloy | 5f255eb | 2015-01-29 13:48:05 +0000 | [diff] [blame] | 349 | // The set of all DAG roots, and state tracking of all roots |
| 350 | // for a particular induction variable. |
| 351 | struct DAGRootTracker { |
| 352 | DAGRootTracker(LoopReroll *Parent, Loop *L, Instruction *IV, |
| 353 | ScalarEvolution *SE, AliasAnalysis *AA, |
Justin Bogner | 843fb20 | 2015-12-15 19:40:57 +0000 | [diff] [blame] | 354 | TargetLibraryInfo *TLI, DominatorTree *DT, LoopInfo *LI, |
| 355 | bool PreserveLCSSA, |
Lawrence Hu | 1befea2 | 2016-04-30 00:51:22 +0000 | [diff] [blame^] | 356 | DenseMap<Instruction *, int64_t> &IncrMap, |
| 357 | Instruction *LoopCtrlIV) |
Justin Bogner | 843fb20 | 2015-12-15 19:40:57 +0000 | [diff] [blame] | 358 | : Parent(Parent), L(L), SE(SE), AA(AA), TLI(TLI), DT(DT), LI(LI), |
Lawrence Hu | 1befea2 | 2016-04-30 00:51:22 +0000 | [diff] [blame^] | 359 | PreserveLCSSA(PreserveLCSSA), IV(IV), IVToIncMap(IncrMap), |
| 360 | LoopControlIV(LoopCtrlIV) {} |
James Molloy | 5f255eb | 2015-01-29 13:48:05 +0000 | [diff] [blame] | 361 | |
| 362 | /// Stage 1: Find all the DAG roots for the induction variable. |
| 363 | bool findRoots(); |
| 364 | /// Stage 2: Validate if the found roots are valid. |
| 365 | bool validate(ReductionTracker &Reductions); |
| 366 | /// Stage 3: Assuming validate() returned true, perform the |
| 367 | /// replacement. |
| 368 | /// @param IterCount The maximum iteration count of L. |
| 369 | void replace(const SCEV *IterCount); |
| 370 | |
| 371 | protected: |
Elena Demikhovsky | 9914dbd | 2016-02-22 09:38:28 +0000 | [diff] [blame] | 372 | typedef MapVector<Instruction*, BitVector> UsesTy; |
James Molloy | 64419d4 | 2015-01-29 21:52:03 +0000 | [diff] [blame] | 373 | |
James Molloy | f147359 | 2015-02-11 09:19:47 +0000 | [diff] [blame] | 374 | bool findRootsRecursive(Instruction *IVU, |
| 375 | SmallInstructionSet SubsumedInsts); |
| 376 | bool findRootsBase(Instruction *IVU, SmallInstructionSet SubsumedInsts); |
| 377 | bool collectPossibleRoots(Instruction *Base, |
| 378 | std::map<int64_t,Instruction*> &Roots); |
James Molloy | 5f255eb | 2015-01-29 13:48:05 +0000 | [diff] [blame] | 379 | |
James Molloy | 64419d4 | 2015-01-29 21:52:03 +0000 | [diff] [blame] | 380 | bool collectUsedInstructions(SmallInstructionSet &PossibleRedSet); |
James Molloy | 5f255eb | 2015-01-29 13:48:05 +0000 | [diff] [blame] | 381 | void collectInLoopUserSet(const SmallInstructionVector &Roots, |
| 382 | const SmallInstructionSet &Exclude, |
| 383 | const SmallInstructionSet &Final, |
| 384 | DenseSet<Instruction *> &Users); |
| 385 | void collectInLoopUserSet(Instruction *Root, |
| 386 | const SmallInstructionSet &Exclude, |
| 387 | const SmallInstructionSet &Final, |
| 388 | DenseSet<Instruction *> &Users); |
| 389 | |
James Molloy | e805ad9 | 2015-02-12 15:54:14 +0000 | [diff] [blame] | 390 | UsesTy::iterator nextInstr(int Val, UsesTy &In, |
| 391 | const SmallInstructionSet &Exclude, |
| 392 | UsesTy::iterator *StartI=nullptr); |
James Molloy | f147359 | 2015-02-11 09:19:47 +0000 | [diff] [blame] | 393 | bool isBaseInst(Instruction *I); |
| 394 | bool isRootInst(Instruction *I); |
James Molloy | e805ad9 | 2015-02-12 15:54:14 +0000 | [diff] [blame] | 395 | bool instrDependsOn(Instruction *I, |
| 396 | UsesTy::iterator Start, |
| 397 | UsesTy::iterator End); |
Lawrence Hu | d3d5106 | 2016-01-25 19:43:45 +0000 | [diff] [blame] | 398 | void replaceIV(Instruction *Inst, Instruction *IV, const SCEV *IterCount); |
Lawrence Hu | 1befea2 | 2016-04-30 00:51:22 +0000 | [diff] [blame^] | 399 | void updateNonLoopCtrlIncr(); |
James Molloy | 64419d4 | 2015-01-29 21:52:03 +0000 | [diff] [blame] | 400 | |
James Molloy | 5f255eb | 2015-01-29 13:48:05 +0000 | [diff] [blame] | 401 | LoopReroll *Parent; |
| 402 | |
| 403 | // Members of Parent, replicated here for brevity. |
| 404 | Loop *L; |
| 405 | ScalarEvolution *SE; |
| 406 | AliasAnalysis *AA; |
| 407 | TargetLibraryInfo *TLI; |
Justin Bogner | 843fb20 | 2015-12-15 19:40:57 +0000 | [diff] [blame] | 408 | DominatorTree *DT; |
| 409 | LoopInfo *LI; |
| 410 | bool PreserveLCSSA; |
James Molloy | 5f255eb | 2015-01-29 13:48:05 +0000 | [diff] [blame] | 411 | |
| 412 | // The loop induction variable. |
| 413 | Instruction *IV; |
| 414 | // Loop step amount. |
Lawrence Hu | dc8a83b | 2015-07-24 22:01:49 +0000 | [diff] [blame] | 415 | int64_t Inc; |
James Molloy | 5f255eb | 2015-01-29 13:48:05 +0000 | [diff] [blame] | 416 | // Loop reroll count; if Inc == 1, this records the scaling applied |
| 417 | // to the indvar: a[i*2+0] = ...; a[i*2+1] = ... ; |
| 418 | // If Inc is not 1, Scale = Inc. |
| 419 | uint64_t Scale; |
James Molloy | 5f255eb | 2015-01-29 13:48:05 +0000 | [diff] [blame] | 420 | // The roots themselves. |
James Molloy | f147359 | 2015-02-11 09:19:47 +0000 | [diff] [blame] | 421 | SmallVector<DAGRootSet,16> RootSets; |
James Molloy | 5f255eb | 2015-01-29 13:48:05 +0000 | [diff] [blame] | 422 | // All increment instructions for IV. |
| 423 | SmallInstructionVector LoopIncs; |
James Molloy | 64419d4 | 2015-01-29 21:52:03 +0000 | [diff] [blame] | 424 | // Map of all instructions in the loop (in order) to the iterations |
James Molloy | f147359 | 2015-02-11 09:19:47 +0000 | [diff] [blame] | 425 | // they are used in (or specially, IL_All for instructions |
James Molloy | 64419d4 | 2015-01-29 21:52:03 +0000 | [diff] [blame] | 426 | // used in the loop increment mechanism). |
| 427 | UsesTy Uses; |
Lawrence Hu | dc8a83b | 2015-07-24 22:01:49 +0000 | [diff] [blame] | 428 | // Map between induction variable and its increment |
| 429 | DenseMap<Instruction *, int64_t> &IVToIncMap; |
Lawrence Hu | 1befea2 | 2016-04-30 00:51:22 +0000 | [diff] [blame^] | 430 | Instruction *LoopControlIV; |
James Molloy | 5f255eb | 2015-01-29 13:48:05 +0000 | [diff] [blame] | 431 | }; |
| 432 | |
Lawrence Hu | 1befea2 | 2016-04-30 00:51:22 +0000 | [diff] [blame^] | 433 | // Check if it is a compare-like instruction whose user is a branch |
| 434 | bool isCompareUsedByBranch(Instruction *I) { |
| 435 | auto *TI = I->getParent()->getTerminator(); |
| 436 | if (!isa<BranchInst>(TI) || !isa<CmpInst>(I)) |
| 437 | return false; |
| 438 | return I->hasOneUse() && TI->getOperand(0) == I; |
| 439 | }; |
| 440 | |
| 441 | bool isLoopControlIV(Loop *L, Instruction *IV); |
Hal Finkel | bf45efd | 2013-11-16 23:59:05 +0000 | [diff] [blame] | 442 | void collectPossibleIVs(Loop *L, SmallInstructionVector &PossibleIVs); |
| 443 | void collectPossibleReductions(Loop *L, |
| 444 | ReductionTracker &Reductions); |
Hal Finkel | bf45efd | 2013-11-16 23:59:05 +0000 | [diff] [blame] | 445 | bool reroll(Instruction *IV, Loop *L, BasicBlock *Header, const SCEV *IterCount, |
| 446 | ReductionTracker &Reductions); |
| 447 | }; |
Alexander Kornienko | f00654e | 2015-06-23 09:49:53 +0000 | [diff] [blame] | 448 | } |
Hal Finkel | bf45efd | 2013-11-16 23:59:05 +0000 | [diff] [blame] | 449 | |
| 450 | char LoopReroll::ID = 0; |
| 451 | INITIALIZE_PASS_BEGIN(LoopReroll, "loop-reroll", "Reroll loops", false, false) |
Chandler Carruth | 31088a9 | 2016-02-19 10:45:18 +0000 | [diff] [blame] | 452 | INITIALIZE_PASS_DEPENDENCY(LoopPass) |
Chandler Carruth | b98f63d | 2015-01-15 10:41:28 +0000 | [diff] [blame] | 453 | INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass) |
Hal Finkel | bf45efd | 2013-11-16 23:59:05 +0000 | [diff] [blame] | 454 | INITIALIZE_PASS_END(LoopReroll, "loop-reroll", "Reroll loops", false, false) |
| 455 | |
| 456 | Pass *llvm::createLoopRerollPass() { |
| 457 | return new LoopReroll; |
| 458 | } |
| 459 | |
| 460 | // Returns true if the provided instruction is used outside the given loop. |
| 461 | // This operates like Instruction::isUsedOutsideOfBlock, but considers PHIs in |
| 462 | // non-loop blocks to be outside the loop. |
| 463 | static bool hasUsesOutsideLoop(Instruction *I, Loop *L) { |
James Molloy | 64419d4 | 2015-01-29 21:52:03 +0000 | [diff] [blame] | 464 | for (User *U : I->users()) { |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 465 | if (!L->contains(cast<Instruction>(U))) |
Hal Finkel | bf45efd | 2013-11-16 23:59:05 +0000 | [diff] [blame] | 466 | return true; |
James Molloy | 64419d4 | 2015-01-29 21:52:03 +0000 | [diff] [blame] | 467 | } |
Hal Finkel | bf45efd | 2013-11-16 23:59:05 +0000 | [diff] [blame] | 468 | return false; |
| 469 | } |
| 470 | |
Lawrence Hu | d3d5106 | 2016-01-25 19:43:45 +0000 | [diff] [blame] | 471 | static const SCEVConstant *getIncrmentFactorSCEV(ScalarEvolution *SE, |
| 472 | const SCEV *SCEVExpr, |
| 473 | Instruction &IV) { |
| 474 | const SCEVMulExpr *MulSCEV = dyn_cast<SCEVMulExpr>(SCEVExpr); |
| 475 | |
| 476 | // If StepRecurrence of a SCEVExpr is a constant (c1 * c2, c2 = sizeof(ptr)), |
| 477 | // Return c1. |
| 478 | if (!MulSCEV && IV.getType()->isPointerTy()) |
| 479 | if (const SCEVConstant *IncSCEV = dyn_cast<SCEVConstant>(SCEVExpr)) { |
| 480 | const PointerType *PTy = cast<PointerType>(IV.getType()); |
| 481 | Type *ElTy = PTy->getElementType(); |
| 482 | const SCEV *SizeOfExpr = |
| 483 | SE->getSizeOfExpr(SE->getEffectiveSCEVType(IV.getType()), ElTy); |
| 484 | if (IncSCEV->getValue()->getValue().isNegative()) { |
| 485 | const SCEV *NewSCEV = |
| 486 | SE->getUDivExpr(SE->getNegativeSCEV(SCEVExpr), SizeOfExpr); |
| 487 | return dyn_cast<SCEVConstant>(SE->getNegativeSCEV(NewSCEV)); |
| 488 | } else { |
| 489 | return dyn_cast<SCEVConstant>(SE->getUDivExpr(SCEVExpr, SizeOfExpr)); |
| 490 | } |
| 491 | } |
| 492 | |
| 493 | if (!MulSCEV) |
| 494 | return nullptr; |
| 495 | |
| 496 | // If StepRecurrence of a SCEVExpr is a c * sizeof(x), where c is constant, |
| 497 | // Return c. |
| 498 | const SCEVConstant *CIncSCEV = nullptr; |
| 499 | for (const SCEV *Operand : MulSCEV->operands()) { |
| 500 | if (const SCEVConstant *Constant = dyn_cast<SCEVConstant>(Operand)) { |
| 501 | CIncSCEV = Constant; |
| 502 | } else if (const SCEVUnknown *Unknown = dyn_cast<SCEVUnknown>(Operand)) { |
| 503 | Type *AllocTy; |
| 504 | if (!Unknown->isSizeOf(AllocTy)) |
| 505 | break; |
| 506 | } else { |
| 507 | return nullptr; |
| 508 | } |
| 509 | } |
| 510 | return CIncSCEV; |
| 511 | } |
| 512 | |
Lawrence Hu | 1befea2 | 2016-04-30 00:51:22 +0000 | [diff] [blame^] | 513 | // Check if an IV is only used to control the loop. There are two cases: |
| 514 | // 1. It only has one use which is loop increment, and the increment is only |
| 515 | // used by comparison and the PHI, and the comparison is only used by branch. |
| 516 | // 2. It is used by loop increment and the comparison, the loop increment is |
| 517 | // only used by the PHI, and the comparison is used only by the branch. |
| 518 | bool LoopReroll::isLoopControlIV(Loop *L, Instruction *IV) { |
| 519 | |
| 520 | unsigned IVUses = IV->getNumUses(); |
| 521 | if (IVUses != 2 && IVUses != 1) |
| 522 | return false; |
| 523 | |
| 524 | for (auto *User : IV->users()) { |
| 525 | int32_t IncOrCmpUses = User->getNumUses(); |
| 526 | bool IsCompInst = isCompareUsedByBranch(cast<Instruction>(User)); |
| 527 | |
| 528 | // User can only have one or two uses. |
| 529 | if (IncOrCmpUses != 2 && IncOrCmpUses != 1) |
| 530 | return false; |
| 531 | |
| 532 | // Case 1 |
| 533 | if (IVUses == 1) { |
| 534 | // The only user must be the loop increment. |
| 535 | // The loop increment must have two uses. |
| 536 | if (IsCompInst || IncOrCmpUses != 2) |
| 537 | return false; |
| 538 | } |
| 539 | |
| 540 | // Case 2 |
| 541 | if (IVUses == 2 && IncOrCmpUses != 1) |
| 542 | return false; |
| 543 | |
| 544 | // The users of the IV must be a binary operation or a comparison |
| 545 | if (auto *BO = dyn_cast<BinaryOperator>(User)) { |
| 546 | if (BO->getOpcode() == Instruction::Add) { |
| 547 | // Loop Increment |
| 548 | // User of Loop Increment should be either PHI or CMP |
| 549 | for (auto *UU : User->users()) { |
| 550 | if (PHINode *PN = dyn_cast<PHINode>(UU)) { |
| 551 | if (PN != IV) |
| 552 | return false; |
| 553 | } |
| 554 | // Must be a CMP |
| 555 | else if (!isCompareUsedByBranch(dyn_cast<Instruction>(UU))) |
| 556 | return false; |
| 557 | } |
| 558 | } else |
| 559 | return false; |
| 560 | // Compare : can only have one use, and must be branch |
| 561 | } else if (!IsCompInst) |
| 562 | return false; |
| 563 | } |
| 564 | return true; |
| 565 | } |
| 566 | |
Hal Finkel | bf45efd | 2013-11-16 23:59:05 +0000 | [diff] [blame] | 567 | // Collect the list of loop induction variables with respect to which it might |
| 568 | // be possible to reroll the loop. |
| 569 | void LoopReroll::collectPossibleIVs(Loop *L, |
| 570 | SmallInstructionVector &PossibleIVs) { |
| 571 | BasicBlock *Header = L->getHeader(); |
| 572 | for (BasicBlock::iterator I = Header->begin(), |
| 573 | IE = Header->getFirstInsertionPt(); I != IE; ++I) { |
| 574 | if (!isa<PHINode>(I)) |
| 575 | continue; |
Lawrence Hu | d3d5106 | 2016-01-25 19:43:45 +0000 | [diff] [blame] | 576 | if (!I->getType()->isIntegerTy() && !I->getType()->isPointerTy()) |
Hal Finkel | bf45efd | 2013-11-16 23:59:05 +0000 | [diff] [blame] | 577 | continue; |
| 578 | |
| 579 | if (const SCEVAddRecExpr *PHISCEV = |
Duncan P. N. Exon Smith | be4d8cb | 2015-10-13 19:26:58 +0000 | [diff] [blame] | 580 | dyn_cast<SCEVAddRecExpr>(SE->getSCEV(&*I))) { |
Hal Finkel | bf45efd | 2013-11-16 23:59:05 +0000 | [diff] [blame] | 581 | if (PHISCEV->getLoop() != L) |
| 582 | continue; |
| 583 | if (!PHISCEV->isAffine()) |
| 584 | continue; |
Lawrence Hu | d3d5106 | 2016-01-25 19:43:45 +0000 | [diff] [blame] | 585 | const SCEVConstant *IncSCEV = nullptr; |
| 586 | if (I->getType()->isPointerTy()) |
| 587 | IncSCEV = |
| 588 | getIncrmentFactorSCEV(SE, PHISCEV->getStepRecurrence(*SE), *I); |
| 589 | else |
| 590 | IncSCEV = dyn_cast<SCEVConstant>(PHISCEV->getStepRecurrence(*SE)); |
| 591 | if (IncSCEV) { |
| 592 | const APInt &AInt = IncSCEV->getValue()->getValue().abs(); |
Lawrence Hu | dc8a83b | 2015-07-24 22:01:49 +0000 | [diff] [blame] | 593 | if (IncSCEV->getValue()->isZero() || AInt.uge(MaxInc)) |
Hal Finkel | bf45efd | 2013-11-16 23:59:05 +0000 | [diff] [blame] | 594 | continue; |
Duncan P. N. Exon Smith | be4d8cb | 2015-10-13 19:26:58 +0000 | [diff] [blame] | 595 | IVToIncMap[&*I] = IncSCEV->getValue()->getSExtValue(); |
Lawrence Hu | dc8a83b | 2015-07-24 22:01:49 +0000 | [diff] [blame] | 596 | DEBUG(dbgs() << "LRR: Possible IV: " << *I << " = " << *PHISCEV |
| 597 | << "\n"); |
Lawrence Hu | 1befea2 | 2016-04-30 00:51:22 +0000 | [diff] [blame^] | 598 | |
| 599 | if (isLoopControlIV(L, &*I)) { |
| 600 | assert(!LoopControlIV && "Found two loop control only IV"); |
| 601 | LoopControlIV = &(*I); |
| 602 | DEBUG(dbgs() << "LRR: Possible loop control only IV: " << *I << " = " |
| 603 | << *PHISCEV << "\n"); |
| 604 | } else |
| 605 | PossibleIVs.push_back(&*I); |
Hal Finkel | bf45efd | 2013-11-16 23:59:05 +0000 | [diff] [blame] | 606 | } |
| 607 | } |
| 608 | } |
| 609 | } |
| 610 | |
| 611 | // Add the remainder of the reduction-variable chain to the instruction vector |
| 612 | // (the initial PHINode has already been added). If successful, the object is |
| 613 | // marked as valid. |
| 614 | void LoopReroll::SimpleLoopReduction::add(Loop *L) { |
| 615 | assert(!Valid && "Cannot add to an already-valid chain"); |
| 616 | |
| 617 | // The reduction variable must be a chain of single-use instructions |
| 618 | // (including the PHI), except for the last value (which is used by the PHI |
| 619 | // and also outside the loop). |
| 620 | Instruction *C = Instructions.front(); |
James Molloy | 4c7deb2 | 2015-02-16 17:01:52 +0000 | [diff] [blame] | 621 | if (C->user_empty()) |
| 622 | return; |
Hal Finkel | bf45efd | 2013-11-16 23:59:05 +0000 | [diff] [blame] | 623 | |
| 624 | do { |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 625 | C = cast<Instruction>(*C->user_begin()); |
Hal Finkel | bf45efd | 2013-11-16 23:59:05 +0000 | [diff] [blame] | 626 | if (C->hasOneUse()) { |
| 627 | if (!C->isBinaryOp()) |
| 628 | return; |
| 629 | |
| 630 | if (!(isa<PHINode>(Instructions.back()) || |
| 631 | C->isSameOperationAs(Instructions.back()))) |
| 632 | return; |
| 633 | |
| 634 | Instructions.push_back(C); |
| 635 | } |
| 636 | } while (C->hasOneUse()); |
| 637 | |
| 638 | if (Instructions.size() < 2 || |
| 639 | !C->isSameOperationAs(Instructions.back()) || |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 640 | C->use_empty()) |
Hal Finkel | bf45efd | 2013-11-16 23:59:05 +0000 | [diff] [blame] | 641 | return; |
| 642 | |
| 643 | // C is now the (potential) last instruction in the reduction chain. |
James Molloy | 64419d4 | 2015-01-29 21:52:03 +0000 | [diff] [blame] | 644 | for (User *U : C->users()) { |
Hal Finkel | bf45efd | 2013-11-16 23:59:05 +0000 | [diff] [blame] | 645 | // The only in-loop user can be the initial PHI. |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 646 | if (L->contains(cast<Instruction>(U))) |
| 647 | if (cast<Instruction>(U) != Instructions.front()) |
Hal Finkel | bf45efd | 2013-11-16 23:59:05 +0000 | [diff] [blame] | 648 | return; |
James Molloy | 64419d4 | 2015-01-29 21:52:03 +0000 | [diff] [blame] | 649 | } |
Hal Finkel | bf45efd | 2013-11-16 23:59:05 +0000 | [diff] [blame] | 650 | |
| 651 | Instructions.push_back(C); |
| 652 | Valid = true; |
| 653 | } |
| 654 | |
| 655 | // Collect the vector of possible reduction variables. |
| 656 | void LoopReroll::collectPossibleReductions(Loop *L, |
| 657 | ReductionTracker &Reductions) { |
| 658 | BasicBlock *Header = L->getHeader(); |
| 659 | for (BasicBlock::iterator I = Header->begin(), |
| 660 | IE = Header->getFirstInsertionPt(); I != IE; ++I) { |
| 661 | if (!isa<PHINode>(I)) |
| 662 | continue; |
| 663 | if (!I->getType()->isSingleValueType()) |
| 664 | continue; |
| 665 | |
Duncan P. N. Exon Smith | be4d8cb | 2015-10-13 19:26:58 +0000 | [diff] [blame] | 666 | SimpleLoopReduction SLR(&*I, L); |
Hal Finkel | bf45efd | 2013-11-16 23:59:05 +0000 | [diff] [blame] | 667 | if (!SLR.valid()) |
| 668 | continue; |
| 669 | |
| 670 | DEBUG(dbgs() << "LRR: Possible reduction: " << *I << " (with " << |
| 671 | SLR.size() << " chained instructions)\n"); |
| 672 | Reductions.addSLR(SLR); |
| 673 | } |
| 674 | } |
| 675 | |
| 676 | // Collect the set of all users of the provided root instruction. This set of |
| 677 | // users contains not only the direct users of the root instruction, but also |
| 678 | // all users of those users, and so on. There are two exceptions: |
| 679 | // |
| 680 | // 1. Instructions in the set of excluded instructions are never added to the |
| 681 | // use set (even if they are users). This is used, for example, to exclude |
| 682 | // including root increments in the use set of the primary IV. |
| 683 | // |
| 684 | // 2. Instructions in the set of final instructions are added to the use set |
| 685 | // if they are users, but their users are not added. This is used, for |
| 686 | // example, to prevent a reduction update from forcing all later reduction |
| 687 | // updates into the use set. |
James Molloy | 5f255eb | 2015-01-29 13:48:05 +0000 | [diff] [blame] | 688 | void LoopReroll::DAGRootTracker::collectInLoopUserSet( |
Hal Finkel | bf45efd | 2013-11-16 23:59:05 +0000 | [diff] [blame] | 689 | Instruction *Root, const SmallInstructionSet &Exclude, |
| 690 | const SmallInstructionSet &Final, |
| 691 | DenseSet<Instruction *> &Users) { |
| 692 | SmallInstructionVector Queue(1, Root); |
| 693 | while (!Queue.empty()) { |
| 694 | Instruction *I = Queue.pop_back_val(); |
| 695 | if (!Users.insert(I).second) |
| 696 | continue; |
| 697 | |
| 698 | if (!Final.count(I)) |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 699 | for (Use &U : I->uses()) { |
| 700 | Instruction *User = cast<Instruction>(U.getUser()); |
Hal Finkel | bf45efd | 2013-11-16 23:59:05 +0000 | [diff] [blame] | 701 | if (PHINode *PN = dyn_cast<PHINode>(User)) { |
| 702 | // Ignore "wrap-around" uses to PHIs of this loop's header. |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 703 | if (PN->getIncomingBlock(U) == L->getHeader()) |
Hal Finkel | bf45efd | 2013-11-16 23:59:05 +0000 | [diff] [blame] | 704 | continue; |
| 705 | } |
NAKAMURA Takumi | 335a7bc | 2014-10-28 11:53:30 +0000 | [diff] [blame] | 706 | |
Hal Finkel | bf45efd | 2013-11-16 23:59:05 +0000 | [diff] [blame] | 707 | if (L->contains(User) && !Exclude.count(User)) { |
| 708 | Queue.push_back(User); |
| 709 | } |
| 710 | } |
| 711 | |
| 712 | // We also want to collect single-user "feeder" values. |
| 713 | for (User::op_iterator OI = I->op_begin(), |
| 714 | OIE = I->op_end(); OI != OIE; ++OI) { |
| 715 | if (Instruction *Op = dyn_cast<Instruction>(*OI)) |
| 716 | if (Op->hasOneUse() && L->contains(Op) && !Exclude.count(Op) && |
| 717 | !Final.count(Op)) |
| 718 | Queue.push_back(Op); |
| 719 | } |
| 720 | } |
| 721 | } |
| 722 | |
| 723 | // Collect all of the users of all of the provided root instructions (combined |
| 724 | // into a single set). |
James Molloy | 5f255eb | 2015-01-29 13:48:05 +0000 | [diff] [blame] | 725 | void LoopReroll::DAGRootTracker::collectInLoopUserSet( |
Hal Finkel | bf45efd | 2013-11-16 23:59:05 +0000 | [diff] [blame] | 726 | const SmallInstructionVector &Roots, |
| 727 | const SmallInstructionSet &Exclude, |
| 728 | const SmallInstructionSet &Final, |
| 729 | DenseSet<Instruction *> &Users) { |
| 730 | for (SmallInstructionVector::const_iterator I = Roots.begin(), |
| 731 | IE = Roots.end(); I != IE; ++I) |
James Molloy | 5f255eb | 2015-01-29 13:48:05 +0000 | [diff] [blame] | 732 | collectInLoopUserSet(*I, Exclude, Final, Users); |
Hal Finkel | bf45efd | 2013-11-16 23:59:05 +0000 | [diff] [blame] | 733 | } |
| 734 | |
| 735 | static bool isSimpleLoadStore(Instruction *I) { |
| 736 | if (LoadInst *LI = dyn_cast<LoadInst>(I)) |
| 737 | return LI->isSimple(); |
| 738 | if (StoreInst *SI = dyn_cast<StoreInst>(I)) |
| 739 | return SI->isSimple(); |
| 740 | if (MemIntrinsic *MI = dyn_cast<MemIntrinsic>(I)) |
| 741 | return !MI->isVolatile(); |
| 742 | return false; |
| 743 | } |
| 744 | |
James Molloy | f147359 | 2015-02-11 09:19:47 +0000 | [diff] [blame] | 745 | /// Return true if IVU is a "simple" arithmetic operation. |
| 746 | /// This is used for narrowing the search space for DAGRoots; only arithmetic |
| 747 | /// and GEPs can be part of a DAGRoot. |
| 748 | static bool isSimpleArithmeticOp(User *IVU) { |
| 749 | if (Instruction *I = dyn_cast<Instruction>(IVU)) { |
| 750 | switch (I->getOpcode()) { |
| 751 | default: return false; |
| 752 | case Instruction::Add: |
| 753 | case Instruction::Sub: |
| 754 | case Instruction::Mul: |
| 755 | case Instruction::Shl: |
| 756 | case Instruction::AShr: |
| 757 | case Instruction::LShr: |
| 758 | case Instruction::GetElementPtr: |
| 759 | case Instruction::Trunc: |
| 760 | case Instruction::ZExt: |
| 761 | case Instruction::SExt: |
| 762 | return true; |
| 763 | } |
| 764 | } |
| 765 | return false; |
| 766 | } |
| 767 | |
| 768 | static bool isLoopIncrement(User *U, Instruction *IV) { |
| 769 | BinaryOperator *BO = dyn_cast<BinaryOperator>(U); |
Lawrence Hu | d3d5106 | 2016-01-25 19:43:45 +0000 | [diff] [blame] | 770 | |
| 771 | if ((BO && BO->getOpcode() != Instruction::Add) || |
| 772 | (!BO && !isa<GetElementPtrInst>(U))) |
James Molloy | f147359 | 2015-02-11 09:19:47 +0000 | [diff] [blame] | 773 | return false; |
| 774 | |
Lawrence Hu | d3d5106 | 2016-01-25 19:43:45 +0000 | [diff] [blame] | 775 | for (auto *UU : U->users()) { |
James Molloy | f147359 | 2015-02-11 09:19:47 +0000 | [diff] [blame] | 776 | PHINode *PN = dyn_cast<PHINode>(UU); |
| 777 | if (PN && PN == IV) |
| 778 | return true; |
| 779 | } |
| 780 | return false; |
| 781 | } |
| 782 | |
| 783 | bool LoopReroll::DAGRootTracker:: |
| 784 | collectPossibleRoots(Instruction *Base, std::map<int64_t,Instruction*> &Roots) { |
| 785 | SmallInstructionVector BaseUsers; |
| 786 | |
| 787 | for (auto *I : Base->users()) { |
| 788 | ConstantInt *CI = nullptr; |
| 789 | |
| 790 | if (isLoopIncrement(I, IV)) { |
| 791 | LoopIncs.push_back(cast<Instruction>(I)); |
| 792 | continue; |
| 793 | } |
| 794 | |
| 795 | // The root nodes must be either GEPs, ORs or ADDs. |
| 796 | if (auto *BO = dyn_cast<BinaryOperator>(I)) { |
| 797 | if (BO->getOpcode() == Instruction::Add || |
| 798 | BO->getOpcode() == Instruction::Or) |
| 799 | CI = dyn_cast<ConstantInt>(BO->getOperand(1)); |
| 800 | } else if (auto *GEP = dyn_cast<GetElementPtrInst>(I)) { |
| 801 | Value *LastOperand = GEP->getOperand(GEP->getNumOperands()-1); |
| 802 | CI = dyn_cast<ConstantInt>(LastOperand); |
| 803 | } |
| 804 | |
| 805 | if (!CI) { |
| 806 | if (Instruction *II = dyn_cast<Instruction>(I)) { |
| 807 | BaseUsers.push_back(II); |
| 808 | continue; |
| 809 | } else { |
| 810 | DEBUG(dbgs() << "LRR: Aborting due to non-instruction: " << *I << "\n"); |
| 811 | return false; |
| 812 | } |
| 813 | } |
| 814 | |
Lawrence Hu | dc8a83b | 2015-07-24 22:01:49 +0000 | [diff] [blame] | 815 | int64_t V = std::abs(CI->getValue().getSExtValue()); |
James Molloy | f147359 | 2015-02-11 09:19:47 +0000 | [diff] [blame] | 816 | if (Roots.find(V) != Roots.end()) |
| 817 | // No duplicates, please. |
| 818 | return false; |
| 819 | |
James Molloy | f147359 | 2015-02-11 09:19:47 +0000 | [diff] [blame] | 820 | Roots[V] = cast<Instruction>(I); |
| 821 | } |
| 822 | |
| 823 | if (Roots.empty()) |
| 824 | return false; |
James Molloy | f147359 | 2015-02-11 09:19:47 +0000 | [diff] [blame] | 825 | |
| 826 | // If we found non-loop-inc, non-root users of Base, assume they are |
| 827 | // for the zeroth root index. This is because "add %a, 0" gets optimized |
| 828 | // away. |
James Molloy | e32d806 | 2015-02-16 17:02:00 +0000 | [diff] [blame] | 829 | if (BaseUsers.size()) { |
| 830 | if (Roots.find(0) != Roots.end()) { |
| 831 | DEBUG(dbgs() << "LRR: Multiple roots found for base - aborting!\n"); |
| 832 | return false; |
| 833 | } |
James Molloy | f147359 | 2015-02-11 09:19:47 +0000 | [diff] [blame] | 834 | Roots[0] = Base; |
James Molloy | e32d806 | 2015-02-16 17:02:00 +0000 | [diff] [blame] | 835 | } |
James Molloy | f147359 | 2015-02-11 09:19:47 +0000 | [diff] [blame] | 836 | |
| 837 | // Calculate the number of users of the base, or lowest indexed, iteration. |
| 838 | unsigned NumBaseUses = BaseUsers.size(); |
| 839 | if (NumBaseUses == 0) |
| 840 | NumBaseUses = Roots.begin()->second->getNumUses(); |
Lawrence Hu | dc8a83b | 2015-07-24 22:01:49 +0000 | [diff] [blame] | 841 | |
James Molloy | f147359 | 2015-02-11 09:19:47 +0000 | [diff] [blame] | 842 | // Check that every node has the same number of users. |
| 843 | for (auto &KV : Roots) { |
| 844 | if (KV.first == 0) |
| 845 | continue; |
| 846 | if (KV.second->getNumUses() != NumBaseUses) { |
| 847 | DEBUG(dbgs() << "LRR: Aborting - Root and Base #users not the same: " |
| 848 | << "#Base=" << NumBaseUses << ", #Root=" << |
| 849 | KV.second->getNumUses() << "\n"); |
| 850 | return false; |
| 851 | } |
| 852 | } |
| 853 | |
Lawrence Hu | dc8a83b | 2015-07-24 22:01:49 +0000 | [diff] [blame] | 854 | return true; |
James Molloy | f147359 | 2015-02-11 09:19:47 +0000 | [diff] [blame] | 855 | } |
| 856 | |
| 857 | bool LoopReroll::DAGRootTracker:: |
| 858 | findRootsRecursive(Instruction *I, SmallInstructionSet SubsumedInsts) { |
| 859 | // Does the user look like it could be part of a root set? |
| 860 | // All its users must be simple arithmetic ops. |
| 861 | if (I->getNumUses() > IL_MaxRerollIterations) |
| 862 | return false; |
| 863 | |
| 864 | if ((I->getOpcode() == Instruction::Mul || |
| 865 | I->getOpcode() == Instruction::PHI) && |
| 866 | I != IV && |
| 867 | findRootsBase(I, SubsumedInsts)) |
| 868 | return true; |
| 869 | |
| 870 | SubsumedInsts.insert(I); |
| 871 | |
| 872 | for (User *V : I->users()) { |
| 873 | Instruction *I = dyn_cast<Instruction>(V); |
| 874 | if (std::find(LoopIncs.begin(), LoopIncs.end(), I) != LoopIncs.end()) |
| 875 | continue; |
| 876 | |
| 877 | if (!I || !isSimpleArithmeticOp(I) || |
| 878 | !findRootsRecursive(I, SubsumedInsts)) |
| 879 | return false; |
| 880 | } |
| 881 | return true; |
| 882 | } |
| 883 | |
| 884 | bool LoopReroll::DAGRootTracker:: |
| 885 | findRootsBase(Instruction *IVU, SmallInstructionSet SubsumedInsts) { |
| 886 | |
| 887 | // The base instruction needs to be a multiply so |
| 888 | // that we can erase it. |
| 889 | if (IVU->getOpcode() != Instruction::Mul && |
| 890 | IVU->getOpcode() != Instruction::PHI) |
| 891 | return false; |
| 892 | |
| 893 | std::map<int64_t, Instruction*> V; |
| 894 | if (!collectPossibleRoots(IVU, V)) |
| 895 | return false; |
| 896 | |
Lawrence Hu | dc8a83b | 2015-07-24 22:01:49 +0000 | [diff] [blame] | 897 | // If we didn't get a root for index zero, then IVU must be |
James Molloy | f147359 | 2015-02-11 09:19:47 +0000 | [diff] [blame] | 898 | // subsumed. |
| 899 | if (V.find(0) == V.end()) |
| 900 | SubsumedInsts.insert(IVU); |
| 901 | |
| 902 | // Partition the vector into monotonically increasing indexes. |
| 903 | DAGRootSet DRS; |
| 904 | DRS.BaseInst = nullptr; |
| 905 | |
| 906 | for (auto &KV : V) { |
| 907 | if (!DRS.BaseInst) { |
| 908 | DRS.BaseInst = KV.second; |
| 909 | DRS.SubsumedInsts = SubsumedInsts; |
| 910 | } else if (DRS.Roots.empty()) { |
| 911 | DRS.Roots.push_back(KV.second); |
| 912 | } else if (V.find(KV.first - 1) != V.end()) { |
| 913 | DRS.Roots.push_back(KV.second); |
| 914 | } else { |
| 915 | // Linear sequence terminated. |
| 916 | RootSets.push_back(DRS); |
| 917 | DRS.BaseInst = KV.second; |
| 918 | DRS.SubsumedInsts = SubsumedInsts; |
| 919 | DRS.Roots.clear(); |
| 920 | } |
| 921 | } |
| 922 | RootSets.push_back(DRS); |
| 923 | |
| 924 | return true; |
| 925 | } |
| 926 | |
James Molloy | 5f255eb | 2015-01-29 13:48:05 +0000 | [diff] [blame] | 927 | bool LoopReroll::DAGRootTracker::findRoots() { |
Lawrence Hu | dc8a83b | 2015-07-24 22:01:49 +0000 | [diff] [blame] | 928 | Inc = IVToIncMap[IV]; |
James Molloy | 5f255eb | 2015-01-29 13:48:05 +0000 | [diff] [blame] | 929 | |
James Molloy | f147359 | 2015-02-11 09:19:47 +0000 | [diff] [blame] | 930 | assert(RootSets.empty() && "Unclean state!"); |
Lawrence Hu | dc8a83b | 2015-07-24 22:01:49 +0000 | [diff] [blame] | 931 | if (std::abs(Inc) == 1) { |
James Molloy | f147359 | 2015-02-11 09:19:47 +0000 | [diff] [blame] | 932 | for (auto *IVU : IV->users()) { |
| 933 | if (isLoopIncrement(IVU, IV)) |
| 934 | LoopIncs.push_back(cast<Instruction>(IVU)); |
| 935 | } |
| 936 | if (!findRootsRecursive(IV, SmallInstructionSet())) |
| 937 | return false; |
| 938 | LoopIncs.push_back(IV); |
| 939 | } else { |
| 940 | if (!findRootsBase(IV, SmallInstructionSet())) |
| 941 | return false; |
| 942 | } |
James Molloy | 5f255eb | 2015-01-29 13:48:05 +0000 | [diff] [blame] | 943 | |
James Molloy | f147359 | 2015-02-11 09:19:47 +0000 | [diff] [blame] | 944 | // Ensure all sets have the same size. |
| 945 | if (RootSets.empty()) { |
| 946 | DEBUG(dbgs() << "LRR: Aborting because no root sets found!\n"); |
James Molloy | 5f255eb | 2015-01-29 13:48:05 +0000 | [diff] [blame] | 947 | return false; |
James Molloy | f147359 | 2015-02-11 09:19:47 +0000 | [diff] [blame] | 948 | } |
| 949 | for (auto &V : RootSets) { |
| 950 | if (V.Roots.empty() || V.Roots.size() != RootSets[0].Roots.size()) { |
| 951 | DEBUG(dbgs() |
| 952 | << "LRR: Aborting because not all root sets have the same size\n"); |
| 953 | return false; |
| 954 | } |
| 955 | } |
James Molloy | 5f255eb | 2015-01-29 13:48:05 +0000 | [diff] [blame] | 956 | |
James Molloy | f147359 | 2015-02-11 09:19:47 +0000 | [diff] [blame] | 957 | // And ensure all loop iterations are consecutive. We rely on std::map |
| 958 | // providing ordered traversal. |
| 959 | for (auto &V : RootSets) { |
| 960 | const auto *ADR = dyn_cast<SCEVAddRecExpr>(SE->getSCEV(V.BaseInst)); |
| 961 | if (!ADR) |
| 962 | return false; |
| 963 | |
| 964 | // Consider a DAGRootSet with N-1 roots (so N different values including |
| 965 | // BaseInst). |
| 966 | // Define d = Roots[0] - BaseInst, which should be the same as |
| 967 | // Roots[I] - Roots[I-1] for all I in [1..N). |
| 968 | // Define D = BaseInst@J - BaseInst@J-1, where "@J" means the value at the |
| 969 | // loop iteration J. |
| 970 | // |
| 971 | // Now, For the loop iterations to be consecutive: |
| 972 | // D = d * N |
| 973 | |
| 974 | unsigned N = V.Roots.size() + 1; |
| 975 | const SCEV *StepSCEV = SE->getMinusSCEV(SE->getSCEV(V.Roots[0]), ADR); |
| 976 | const SCEV *ScaleSCEV = SE->getConstant(StepSCEV->getType(), N); |
| 977 | if (ADR->getStepRecurrence(*SE) != SE->getMulExpr(StepSCEV, ScaleSCEV)) { |
| 978 | DEBUG(dbgs() << "LRR: Aborting because iterations are not consecutive\n"); |
| 979 | return false; |
| 980 | } |
| 981 | } |
| 982 | Scale = RootSets[0].Roots.size() + 1; |
| 983 | |
| 984 | if (Scale > IL_MaxRerollIterations) { |
James Molloy | 64419d4 | 2015-01-29 21:52:03 +0000 | [diff] [blame] | 985 | DEBUG(dbgs() << "LRR: Aborting - too many iterations found. " |
James Molloy | f147359 | 2015-02-11 09:19:47 +0000 | [diff] [blame] | 986 | << "#Found=" << Scale << ", #Max=" << IL_MaxRerollIterations |
James Molloy | 64419d4 | 2015-01-29 21:52:03 +0000 | [diff] [blame] | 987 | << "\n"); |
| 988 | return false; |
| 989 | } |
| 990 | |
James Molloy | f147359 | 2015-02-11 09:19:47 +0000 | [diff] [blame] | 991 | DEBUG(dbgs() << "LRR: Successfully found roots: Scale=" << Scale << "\n"); |
James Molloy | 5f255eb | 2015-01-29 13:48:05 +0000 | [diff] [blame] | 992 | |
| 993 | return true; |
| 994 | } |
| 995 | |
James Molloy | 64419d4 | 2015-01-29 21:52:03 +0000 | [diff] [blame] | 996 | bool LoopReroll::DAGRootTracker::collectUsedInstructions(SmallInstructionSet &PossibleRedSet) { |
| 997 | // Populate the MapVector with all instructions in the block, in order first, |
| 998 | // so we can iterate over the contents later in perfect order. |
| 999 | for (auto &I : *L->getHeader()) { |
| 1000 | Uses[&I].resize(IL_End); |
| 1001 | } |
James Molloy | 5f255eb | 2015-01-29 13:48:05 +0000 | [diff] [blame] | 1002 | |
James Molloy | 64419d4 | 2015-01-29 21:52:03 +0000 | [diff] [blame] | 1003 | SmallInstructionSet Exclude; |
James Molloy | f147359 | 2015-02-11 09:19:47 +0000 | [diff] [blame] | 1004 | for (auto &DRS : RootSets) { |
| 1005 | Exclude.insert(DRS.Roots.begin(), DRS.Roots.end()); |
| 1006 | Exclude.insert(DRS.SubsumedInsts.begin(), DRS.SubsumedInsts.end()); |
| 1007 | Exclude.insert(DRS.BaseInst); |
| 1008 | } |
James Molloy | 64419d4 | 2015-01-29 21:52:03 +0000 | [diff] [blame] | 1009 | Exclude.insert(LoopIncs.begin(), LoopIncs.end()); |
| 1010 | |
James Molloy | f147359 | 2015-02-11 09:19:47 +0000 | [diff] [blame] | 1011 | for (auto &DRS : RootSets) { |
| 1012 | DenseSet<Instruction*> VBase; |
| 1013 | collectInLoopUserSet(DRS.BaseInst, Exclude, PossibleRedSet, VBase); |
| 1014 | for (auto *I : VBase) { |
| 1015 | Uses[I].set(0); |
James Molloy | 64419d4 | 2015-01-29 21:52:03 +0000 | [diff] [blame] | 1016 | } |
| 1017 | |
James Molloy | f147359 | 2015-02-11 09:19:47 +0000 | [diff] [blame] | 1018 | unsigned Idx = 1; |
| 1019 | for (auto *Root : DRS.Roots) { |
| 1020 | DenseSet<Instruction*> V; |
| 1021 | collectInLoopUserSet(Root, Exclude, PossibleRedSet, V); |
| 1022 | |
| 1023 | // While we're here, check the use sets are the same size. |
| 1024 | if (V.size() != VBase.size()) { |
| 1025 | DEBUG(dbgs() << "LRR: Aborting - use sets are different sizes\n"); |
| 1026 | return false; |
| 1027 | } |
| 1028 | |
| 1029 | for (auto *I : V) { |
| 1030 | Uses[I].set(Idx); |
| 1031 | } |
| 1032 | ++Idx; |
James Molloy | 64419d4 | 2015-01-29 21:52:03 +0000 | [diff] [blame] | 1033 | } |
James Molloy | f147359 | 2015-02-11 09:19:47 +0000 | [diff] [blame] | 1034 | |
| 1035 | // Make sure our subsumed instructions are remembered too. |
| 1036 | for (auto *I : DRS.SubsumedInsts) { |
| 1037 | Uses[I].set(IL_All); |
| 1038 | } |
James Molloy | 64419d4 | 2015-01-29 21:52:03 +0000 | [diff] [blame] | 1039 | } |
| 1040 | |
| 1041 | // Make sure the loop increments are also accounted for. |
James Molloy | f147359 | 2015-02-11 09:19:47 +0000 | [diff] [blame] | 1042 | |
James Molloy | 64419d4 | 2015-01-29 21:52:03 +0000 | [diff] [blame] | 1043 | Exclude.clear(); |
James Molloy | f147359 | 2015-02-11 09:19:47 +0000 | [diff] [blame] | 1044 | for (auto &DRS : RootSets) { |
| 1045 | Exclude.insert(DRS.Roots.begin(), DRS.Roots.end()); |
| 1046 | Exclude.insert(DRS.SubsumedInsts.begin(), DRS.SubsumedInsts.end()); |
| 1047 | Exclude.insert(DRS.BaseInst); |
| 1048 | } |
James Molloy | 64419d4 | 2015-01-29 21:52:03 +0000 | [diff] [blame] | 1049 | |
| 1050 | DenseSet<Instruction*> V; |
| 1051 | collectInLoopUserSet(LoopIncs, Exclude, PossibleRedSet, V); |
| 1052 | for (auto *I : V) { |
James Molloy | f147359 | 2015-02-11 09:19:47 +0000 | [diff] [blame] | 1053 | Uses[I].set(IL_All); |
James Molloy | 64419d4 | 2015-01-29 21:52:03 +0000 | [diff] [blame] | 1054 | } |
James Molloy | 64419d4 | 2015-01-29 21:52:03 +0000 | [diff] [blame] | 1055 | |
| 1056 | return true; |
| 1057 | |
| 1058 | } |
| 1059 | |
James Molloy | e805ad9 | 2015-02-12 15:54:14 +0000 | [diff] [blame] | 1060 | /// Get the next instruction in "In" that is a member of set Val. |
| 1061 | /// Start searching from StartI, and do not return anything in Exclude. |
| 1062 | /// If StartI is not given, start from In.begin(). |
James Molloy | 64419d4 | 2015-01-29 21:52:03 +0000 | [diff] [blame] | 1063 | LoopReroll::DAGRootTracker::UsesTy::iterator |
| 1064 | LoopReroll::DAGRootTracker::nextInstr(int Val, UsesTy &In, |
James Molloy | e805ad9 | 2015-02-12 15:54:14 +0000 | [diff] [blame] | 1065 | const SmallInstructionSet &Exclude, |
| 1066 | UsesTy::iterator *StartI) { |
| 1067 | UsesTy::iterator I = StartI ? *StartI : In.begin(); |
| 1068 | while (I != In.end() && (I->second.test(Val) == 0 || |
| 1069 | Exclude.count(I->first) != 0)) |
James Molloy | 64419d4 | 2015-01-29 21:52:03 +0000 | [diff] [blame] | 1070 | ++I; |
| 1071 | return I; |
| 1072 | } |
| 1073 | |
James Molloy | f147359 | 2015-02-11 09:19:47 +0000 | [diff] [blame] | 1074 | bool LoopReroll::DAGRootTracker::isBaseInst(Instruction *I) { |
| 1075 | for (auto &DRS : RootSets) { |
| 1076 | if (DRS.BaseInst == I) |
| 1077 | return true; |
| 1078 | } |
| 1079 | return false; |
| 1080 | } |
| 1081 | |
| 1082 | bool LoopReroll::DAGRootTracker::isRootInst(Instruction *I) { |
| 1083 | for (auto &DRS : RootSets) { |
| 1084 | if (std::find(DRS.Roots.begin(), DRS.Roots.end(), I) != DRS.Roots.end()) |
| 1085 | return true; |
| 1086 | } |
| 1087 | return false; |
| 1088 | } |
| 1089 | |
James Molloy | e805ad9 | 2015-02-12 15:54:14 +0000 | [diff] [blame] | 1090 | /// Return true if instruction I depends on any instruction between |
| 1091 | /// Start and End. |
| 1092 | bool LoopReroll::DAGRootTracker::instrDependsOn(Instruction *I, |
| 1093 | UsesTy::iterator Start, |
| 1094 | UsesTy::iterator End) { |
| 1095 | for (auto *U : I->users()) { |
| 1096 | for (auto It = Start; It != End; ++It) |
| 1097 | if (U == It->first) |
| 1098 | return true; |
| 1099 | } |
| 1100 | return false; |
| 1101 | } |
| 1102 | |
Weiming Zhao | 310770a | 2015-09-28 17:03:23 +0000 | [diff] [blame] | 1103 | static bool isIgnorableInst(const Instruction *I) { |
| 1104 | if (isa<DbgInfoIntrinsic>(I)) |
| 1105 | return true; |
| 1106 | const IntrinsicInst* II = dyn_cast<IntrinsicInst>(I); |
| 1107 | if (!II) |
| 1108 | return false; |
| 1109 | switch (II->getIntrinsicID()) { |
| 1110 | default: |
| 1111 | return false; |
| 1112 | case llvm::Intrinsic::annotation: |
| 1113 | case Intrinsic::ptr_annotation: |
| 1114 | case Intrinsic::var_annotation: |
| 1115 | // TODO: the following intrinsics may also be whitelisted: |
| 1116 | // lifetime_start, lifetime_end, invariant_start, invariant_end |
| 1117 | return true; |
| 1118 | } |
| 1119 | return false; |
| 1120 | } |
| 1121 | |
James Molloy | 64419d4 | 2015-01-29 21:52:03 +0000 | [diff] [blame] | 1122 | bool LoopReroll::DAGRootTracker::validate(ReductionTracker &Reductions) { |
James Molloy | 5f255eb | 2015-01-29 13:48:05 +0000 | [diff] [blame] | 1123 | // We now need to check for equivalence of the use graph of each root with |
| 1124 | // that of the primary induction variable (excluding the roots). Our goal |
| 1125 | // here is not to solve the full graph isomorphism problem, but rather to |
| 1126 | // catch common cases without a lot of work. As a result, we will assume |
| 1127 | // that the relative order of the instructions in each unrolled iteration |
| 1128 | // is the same (although we will not make an assumption about how the |
| 1129 | // different iterations are intermixed). Note that while the order must be |
| 1130 | // the same, the instructions may not be in the same basic block. |
James Molloy | 5f255eb | 2015-01-29 13:48:05 +0000 | [diff] [blame] | 1131 | |
| 1132 | // An array of just the possible reductions for this scale factor. When we |
| 1133 | // collect the set of all users of some root instructions, these reduction |
| 1134 | // instructions are treated as 'final' (their uses are not considered). |
| 1135 | // This is important because we don't want the root use set to search down |
| 1136 | // the reduction chain. |
| 1137 | SmallInstructionSet PossibleRedSet; |
| 1138 | SmallInstructionSet PossibleRedLastSet; |
| 1139 | SmallInstructionSet PossibleRedPHISet; |
| 1140 | Reductions.restrictToScale(Scale, PossibleRedSet, |
| 1141 | PossibleRedPHISet, PossibleRedLastSet); |
James Molloy | 5f255eb | 2015-01-29 13:48:05 +0000 | [diff] [blame] | 1142 | |
James Molloy | 64419d4 | 2015-01-29 21:52:03 +0000 | [diff] [blame] | 1143 | // Populate "Uses" with where each instruction is used. |
| 1144 | if (!collectUsedInstructions(PossibleRedSet)) |
| 1145 | return false; |
James Molloy | 5f255eb | 2015-01-29 13:48:05 +0000 | [diff] [blame] | 1146 | |
James Molloy | 64419d4 | 2015-01-29 21:52:03 +0000 | [diff] [blame] | 1147 | // Make sure we mark the reduction PHIs as used in all iterations. |
| 1148 | for (auto *I : PossibleRedPHISet) { |
James Molloy | f147359 | 2015-02-11 09:19:47 +0000 | [diff] [blame] | 1149 | Uses[I].set(IL_All); |
James Molloy | 64419d4 | 2015-01-29 21:52:03 +0000 | [diff] [blame] | 1150 | } |
James Molloy | 5f255eb | 2015-01-29 13:48:05 +0000 | [diff] [blame] | 1151 | |
Lawrence Hu | 1befea2 | 2016-04-30 00:51:22 +0000 | [diff] [blame^] | 1152 | // Make sure we mark loop-control-only PHIs as used in all iterations. See |
| 1153 | // comment above LoopReroll::isLoopControlIV for more information. |
| 1154 | BasicBlock *Header = L->getHeader(); |
| 1155 | if (LoopControlIV && LoopControlIV != IV) { |
| 1156 | for (auto *U : LoopControlIV->users()) { |
| 1157 | Instruction *IVUser = dyn_cast<Instruction>(U); |
| 1158 | // IVUser could be loop increment or compare |
| 1159 | Uses[IVUser].set(IL_All); |
| 1160 | for (auto *UU : IVUser->users()) { |
| 1161 | Instruction *UUser = dyn_cast<Instruction>(UU); |
| 1162 | // UUser could be compare, PHI or branch |
| 1163 | Uses[UUser].set(IL_All); |
| 1164 | // Is UUser a compare instruction? |
| 1165 | if (UU->hasOneUse()) { |
| 1166 | Instruction *BI = dyn_cast<BranchInst>(*UUser->user_begin()); |
| 1167 | if (BI == cast<BranchInst>(Header->getTerminator())) |
| 1168 | Uses[BI].set(IL_All); |
| 1169 | } |
| 1170 | } |
| 1171 | } |
| 1172 | } |
| 1173 | |
James Molloy | 64419d4 | 2015-01-29 21:52:03 +0000 | [diff] [blame] | 1174 | // Make sure all instructions in the loop are in one and only one |
| 1175 | // set. |
| 1176 | for (auto &KV : Uses) { |
Weiming Zhao | 310770a | 2015-09-28 17:03:23 +0000 | [diff] [blame] | 1177 | if (KV.second.count() != 1 && !isIgnorableInst(KV.first)) { |
James Molloy | 64419d4 | 2015-01-29 21:52:03 +0000 | [diff] [blame] | 1178 | DEBUG(dbgs() << "LRR: Aborting - instruction is not used in 1 iteration: " |
| 1179 | << *KV.first << " (#uses=" << KV.second.count() << ")\n"); |
| 1180 | return false; |
Hal Finkel | bf45efd | 2013-11-16 23:59:05 +0000 | [diff] [blame] | 1181 | } |
James Molloy | 64419d4 | 2015-01-29 21:52:03 +0000 | [diff] [blame] | 1182 | } |
Hal Finkel | bf45efd | 2013-11-16 23:59:05 +0000 | [diff] [blame] | 1183 | |
James Molloy | 64419d4 | 2015-01-29 21:52:03 +0000 | [diff] [blame] | 1184 | DEBUG( |
| 1185 | for (auto &KV : Uses) { |
| 1186 | dbgs() << "LRR: " << KV.second.find_first() << "\t" << *KV.first << "\n"; |
| 1187 | } |
| 1188 | ); |
| 1189 | |
| 1190 | for (unsigned Iter = 1; Iter < Scale; ++Iter) { |
James Molloy | 5f255eb | 2015-01-29 13:48:05 +0000 | [diff] [blame] | 1191 | // In addition to regular aliasing information, we need to look for |
| 1192 | // instructions from later (future) iterations that have side effects |
| 1193 | // preventing us from reordering them past other instructions with side |
| 1194 | // effects. |
| 1195 | bool FutureSideEffects = false; |
| 1196 | AliasSetTracker AST(*AA); |
James Molloy | 5f255eb | 2015-01-29 13:48:05 +0000 | [diff] [blame] | 1197 | // The map between instructions in f(%iv.(i+1)) and f(%iv). |
| 1198 | DenseMap<Value *, Value *> BaseMap; |
| 1199 | |
James Molloy | 64419d4 | 2015-01-29 21:52:03 +0000 | [diff] [blame] | 1200 | // Compare iteration Iter to the base. |
James Molloy | e805ad9 | 2015-02-12 15:54:14 +0000 | [diff] [blame] | 1201 | SmallInstructionSet Visited; |
| 1202 | auto BaseIt = nextInstr(0, Uses, Visited); |
| 1203 | auto RootIt = nextInstr(Iter, Uses, Visited); |
James Molloy | 64419d4 | 2015-01-29 21:52:03 +0000 | [diff] [blame] | 1204 | auto LastRootIt = Uses.begin(); |
James Molloy | 5f255eb | 2015-01-29 13:48:05 +0000 | [diff] [blame] | 1205 | |
James Molloy | 64419d4 | 2015-01-29 21:52:03 +0000 | [diff] [blame] | 1206 | while (BaseIt != Uses.end() && RootIt != Uses.end()) { |
| 1207 | Instruction *BaseInst = BaseIt->first; |
| 1208 | Instruction *RootInst = RootIt->first; |
James Molloy | 5f255eb | 2015-01-29 13:48:05 +0000 | [diff] [blame] | 1209 | |
James Molloy | 64419d4 | 2015-01-29 21:52:03 +0000 | [diff] [blame] | 1210 | // Skip over the IV or root instructions; only match their users. |
| 1211 | bool Continue = false; |
James Molloy | f147359 | 2015-02-11 09:19:47 +0000 | [diff] [blame] | 1212 | if (isBaseInst(BaseInst)) { |
James Molloy | e805ad9 | 2015-02-12 15:54:14 +0000 | [diff] [blame] | 1213 | Visited.insert(BaseInst); |
| 1214 | BaseIt = nextInstr(0, Uses, Visited); |
James Molloy | 64419d4 | 2015-01-29 21:52:03 +0000 | [diff] [blame] | 1215 | Continue = true; |
| 1216 | } |
James Molloy | f147359 | 2015-02-11 09:19:47 +0000 | [diff] [blame] | 1217 | if (isRootInst(RootInst)) { |
James Molloy | 64419d4 | 2015-01-29 21:52:03 +0000 | [diff] [blame] | 1218 | LastRootIt = RootIt; |
James Molloy | e805ad9 | 2015-02-12 15:54:14 +0000 | [diff] [blame] | 1219 | Visited.insert(RootInst); |
| 1220 | RootIt = nextInstr(Iter, Uses, Visited); |
James Molloy | 64419d4 | 2015-01-29 21:52:03 +0000 | [diff] [blame] | 1221 | Continue = true; |
| 1222 | } |
| 1223 | if (Continue) continue; |
James Molloy | 5f255eb | 2015-01-29 13:48:05 +0000 | [diff] [blame] | 1224 | |
James Molloy | e805ad9 | 2015-02-12 15:54:14 +0000 | [diff] [blame] | 1225 | if (!BaseInst->isSameOperationAs(RootInst)) { |
| 1226 | // Last chance saloon. We don't try and solve the full isomorphism |
| 1227 | // problem, but try and at least catch the case where two instructions |
| 1228 | // *of different types* are round the wrong way. We won't be able to |
| 1229 | // efficiently tell, given two ADD instructions, which way around we |
| 1230 | // should match them, but given an ADD and a SUB, we can at least infer |
| 1231 | // which one is which. |
| 1232 | // |
| 1233 | // This should allow us to deal with a greater subset of the isomorphism |
| 1234 | // problem. It does however change a linear algorithm into a quadratic |
| 1235 | // one, so limit the number of probes we do. |
| 1236 | auto TryIt = RootIt; |
| 1237 | unsigned N = NumToleratedFailedMatches; |
| 1238 | while (TryIt != Uses.end() && |
| 1239 | !BaseInst->isSameOperationAs(TryIt->first) && |
| 1240 | N--) { |
| 1241 | ++TryIt; |
| 1242 | TryIt = nextInstr(Iter, Uses, Visited, &TryIt); |
| 1243 | } |
| 1244 | |
| 1245 | if (TryIt == Uses.end() || TryIt == RootIt || |
| 1246 | instrDependsOn(TryIt->first, RootIt, TryIt)) { |
| 1247 | DEBUG(dbgs() << "LRR: iteration root match failed at " << *BaseInst << |
| 1248 | " vs. " << *RootInst << "\n"); |
| 1249 | return false; |
| 1250 | } |
Lawrence Hu | dc8a83b | 2015-07-24 22:01:49 +0000 | [diff] [blame] | 1251 | |
James Molloy | e805ad9 | 2015-02-12 15:54:14 +0000 | [diff] [blame] | 1252 | RootIt = TryIt; |
| 1253 | RootInst = TryIt->first; |
| 1254 | } |
| 1255 | |
James Molloy | 64419d4 | 2015-01-29 21:52:03 +0000 | [diff] [blame] | 1256 | // All instructions between the last root and this root |
Lawrence Hu | dc8a83b | 2015-07-24 22:01:49 +0000 | [diff] [blame] | 1257 | // may belong to some other iteration. If they belong to a |
James Molloy | 64419d4 | 2015-01-29 21:52:03 +0000 | [diff] [blame] | 1258 | // future iteration, then they're dangerous to alias with. |
Lawrence Hu | dc8a83b | 2015-07-24 22:01:49 +0000 | [diff] [blame] | 1259 | // |
James Molloy | e805ad9 | 2015-02-12 15:54:14 +0000 | [diff] [blame] | 1260 | // Note that because we allow a limited amount of flexibility in the order |
| 1261 | // that we visit nodes, LastRootIt might be *before* RootIt, in which |
| 1262 | // case we've already checked this set of instructions so we shouldn't |
| 1263 | // do anything. |
| 1264 | for (; LastRootIt < RootIt; ++LastRootIt) { |
James Molloy | 64419d4 | 2015-01-29 21:52:03 +0000 | [diff] [blame] | 1265 | Instruction *I = LastRootIt->first; |
| 1266 | if (LastRootIt->second.find_first() < (int)Iter) |
| 1267 | continue; |
| 1268 | if (I->mayWriteToMemory()) |
| 1269 | AST.add(I); |
| 1270 | // Note: This is specifically guarded by a check on isa<PHINode>, |
| 1271 | // which while a valid (somewhat arbitrary) micro-optimization, is |
| 1272 | // needed because otherwise isSafeToSpeculativelyExecute returns |
| 1273 | // false on PHI nodes. |
| 1274 | if (!isa<PHINode>(I) && !isSimpleLoadStore(I) && |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1275 | !isSafeToSpeculativelyExecute(I)) |
James Molloy | 64419d4 | 2015-01-29 21:52:03 +0000 | [diff] [blame] | 1276 | // Intervening instructions cause side effects. |
| 1277 | FutureSideEffects = true; |
James Molloy | 5f255eb | 2015-01-29 13:48:05 +0000 | [diff] [blame] | 1278 | } |
| 1279 | |
James Molloy | 5f255eb | 2015-01-29 13:48:05 +0000 | [diff] [blame] | 1280 | // Make sure that this instruction, which is in the use set of this |
| 1281 | // root instruction, does not also belong to the base set or the set of |
James Molloy | 64419d4 | 2015-01-29 21:52:03 +0000 | [diff] [blame] | 1282 | // some other root instruction. |
| 1283 | if (RootIt->second.count() > 1) { |
| 1284 | DEBUG(dbgs() << "LRR: iteration root match failed at " << *BaseInst << |
| 1285 | " vs. " << *RootInst << " (prev. case overlap)\n"); |
| 1286 | return false; |
James Molloy | 5f255eb | 2015-01-29 13:48:05 +0000 | [diff] [blame] | 1287 | } |
| 1288 | |
| 1289 | // Make sure that we don't alias with any instruction in the alias set |
| 1290 | // tracker. If we do, then we depend on a future iteration, and we |
| 1291 | // can't reroll. |
James Molloy | 64419d4 | 2015-01-29 21:52:03 +0000 | [diff] [blame] | 1292 | if (RootInst->mayReadFromMemory()) |
| 1293 | for (auto &K : AST) { |
| 1294 | if (K.aliasesUnknownInst(RootInst, *AA)) { |
| 1295 | DEBUG(dbgs() << "LRR: iteration root match failed at " << *BaseInst << |
| 1296 | " vs. " << *RootInst << " (depends on future store)\n"); |
| 1297 | return false; |
James Molloy | 5f255eb | 2015-01-29 13:48:05 +0000 | [diff] [blame] | 1298 | } |
| 1299 | } |
James Molloy | 5f255eb | 2015-01-29 13:48:05 +0000 | [diff] [blame] | 1300 | |
| 1301 | // If we've past an instruction from a future iteration that may have |
| 1302 | // side effects, and this instruction might also, then we can't reorder |
| 1303 | // them, and this matching fails. As an exception, we allow the alias |
| 1304 | // set tracker to handle regular (simple) load/store dependencies. |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1305 | if (FutureSideEffects && ((!isSimpleLoadStore(BaseInst) && |
| 1306 | !isSafeToSpeculativelyExecute(BaseInst)) || |
| 1307 | (!isSimpleLoadStore(RootInst) && |
| 1308 | !isSafeToSpeculativelyExecute(RootInst)))) { |
James Molloy | 64419d4 | 2015-01-29 21:52:03 +0000 | [diff] [blame] | 1309 | DEBUG(dbgs() << "LRR: iteration root match failed at " << *BaseInst << |
| 1310 | " vs. " << *RootInst << |
James Molloy | 5f255eb | 2015-01-29 13:48:05 +0000 | [diff] [blame] | 1311 | " (side effects prevent reordering)\n"); |
James Molloy | 64419d4 | 2015-01-29 21:52:03 +0000 | [diff] [blame] | 1312 | return false; |
James Molloy | 5f255eb | 2015-01-29 13:48:05 +0000 | [diff] [blame] | 1313 | } |
| 1314 | |
| 1315 | // For instructions that are part of a reduction, if the operation is |
| 1316 | // associative, then don't bother matching the operands (because we |
| 1317 | // already know that the instructions are isomorphic, and the order |
| 1318 | // within the iteration does not matter). For non-associative reductions, |
| 1319 | // we do need to match the operands, because we need to reject |
| 1320 | // out-of-order instructions within an iteration! |
| 1321 | // For example (assume floating-point addition), we need to reject this: |
| 1322 | // x += a[i]; x += b[i]; |
| 1323 | // x += a[i+1]; x += b[i+1]; |
| 1324 | // x += b[i+2]; x += a[i+2]; |
James Molloy | 64419d4 | 2015-01-29 21:52:03 +0000 | [diff] [blame] | 1325 | bool InReduction = Reductions.isPairInSame(BaseInst, RootInst); |
James Molloy | 5f255eb | 2015-01-29 13:48:05 +0000 | [diff] [blame] | 1326 | |
James Molloy | 64419d4 | 2015-01-29 21:52:03 +0000 | [diff] [blame] | 1327 | if (!(InReduction && BaseInst->isAssociative())) { |
James Molloy | 5f255eb | 2015-01-29 13:48:05 +0000 | [diff] [blame] | 1328 | bool Swapped = false, SomeOpMatched = false; |
James Molloy | 64419d4 | 2015-01-29 21:52:03 +0000 | [diff] [blame] | 1329 | for (unsigned j = 0; j < BaseInst->getNumOperands(); ++j) { |
| 1330 | Value *Op2 = RootInst->getOperand(j); |
James Molloy | 5f255eb | 2015-01-29 13:48:05 +0000 | [diff] [blame] | 1331 | |
| 1332 | // If this is part of a reduction (and the operation is not |
| 1333 | // associatve), then we match all operands, but not those that are |
| 1334 | // part of the reduction. |
| 1335 | if (InReduction) |
| 1336 | if (Instruction *Op2I = dyn_cast<Instruction>(Op2)) |
James Molloy | 64419d4 | 2015-01-29 21:52:03 +0000 | [diff] [blame] | 1337 | if (Reductions.isPairInSame(RootInst, Op2I)) |
James Molloy | 5f255eb | 2015-01-29 13:48:05 +0000 | [diff] [blame] | 1338 | continue; |
| 1339 | |
| 1340 | DenseMap<Value *, Value *>::iterator BMI = BaseMap.find(Op2); |
James Molloy | f147359 | 2015-02-11 09:19:47 +0000 | [diff] [blame] | 1341 | if (BMI != BaseMap.end()) { |
James Molloy | 5f255eb | 2015-01-29 13:48:05 +0000 | [diff] [blame] | 1342 | Op2 = BMI->second; |
James Molloy | f147359 | 2015-02-11 09:19:47 +0000 | [diff] [blame] | 1343 | } else { |
| 1344 | for (auto &DRS : RootSets) { |
| 1345 | if (DRS.Roots[Iter-1] == (Instruction*) Op2) { |
| 1346 | Op2 = DRS.BaseInst; |
| 1347 | break; |
| 1348 | } |
| 1349 | } |
| 1350 | } |
James Molloy | 5f255eb | 2015-01-29 13:48:05 +0000 | [diff] [blame] | 1351 | |
James Molloy | 64419d4 | 2015-01-29 21:52:03 +0000 | [diff] [blame] | 1352 | if (BaseInst->getOperand(Swapped ? unsigned(!j) : j) != Op2) { |
James Molloy | 5f255eb | 2015-01-29 13:48:05 +0000 | [diff] [blame] | 1353 | // If we've not already decided to swap the matched operands, and |
| 1354 | // we've not already matched our first operand (note that we could |
| 1355 | // have skipped matching the first operand because it is part of a |
| 1356 | // reduction above), and the instruction is commutative, then try |
| 1357 | // the swapped match. |
James Molloy | 64419d4 | 2015-01-29 21:52:03 +0000 | [diff] [blame] | 1358 | if (!Swapped && BaseInst->isCommutative() && !SomeOpMatched && |
| 1359 | BaseInst->getOperand(!j) == Op2) { |
James Molloy | 5f255eb | 2015-01-29 13:48:05 +0000 | [diff] [blame] | 1360 | Swapped = true; |
| 1361 | } else { |
James Molloy | 64419d4 | 2015-01-29 21:52:03 +0000 | [diff] [blame] | 1362 | DEBUG(dbgs() << "LRR: iteration root match failed at " << *BaseInst |
| 1363 | << " vs. " << *RootInst << " (operand " << j << ")\n"); |
| 1364 | return false; |
James Molloy | 5f255eb | 2015-01-29 13:48:05 +0000 | [diff] [blame] | 1365 | } |
| 1366 | } |
| 1367 | |
| 1368 | SomeOpMatched = true; |
| 1369 | } |
| 1370 | } |
| 1371 | |
James Molloy | 64419d4 | 2015-01-29 21:52:03 +0000 | [diff] [blame] | 1372 | if ((!PossibleRedLastSet.count(BaseInst) && |
| 1373 | hasUsesOutsideLoop(BaseInst, L)) || |
| 1374 | (!PossibleRedLastSet.count(RootInst) && |
| 1375 | hasUsesOutsideLoop(RootInst, L))) { |
| 1376 | DEBUG(dbgs() << "LRR: iteration root match failed at " << *BaseInst << |
| 1377 | " vs. " << *RootInst << " (uses outside loop)\n"); |
| 1378 | return false; |
James Molloy | 5f255eb | 2015-01-29 13:48:05 +0000 | [diff] [blame] | 1379 | } |
| 1380 | |
James Molloy | 64419d4 | 2015-01-29 21:52:03 +0000 | [diff] [blame] | 1381 | Reductions.recordPair(BaseInst, RootInst, Iter); |
| 1382 | BaseMap.insert(std::make_pair(RootInst, BaseInst)); |
James Molloy | 5f255eb | 2015-01-29 13:48:05 +0000 | [diff] [blame] | 1383 | |
James Molloy | 64419d4 | 2015-01-29 21:52:03 +0000 | [diff] [blame] | 1384 | LastRootIt = RootIt; |
James Molloy | e805ad9 | 2015-02-12 15:54:14 +0000 | [diff] [blame] | 1385 | Visited.insert(BaseInst); |
| 1386 | Visited.insert(RootInst); |
| 1387 | BaseIt = nextInstr(0, Uses, Visited); |
| 1388 | RootIt = nextInstr(Iter, Uses, Visited); |
James Molloy | 5f255eb | 2015-01-29 13:48:05 +0000 | [diff] [blame] | 1389 | } |
James Molloy | 64419d4 | 2015-01-29 21:52:03 +0000 | [diff] [blame] | 1390 | assert (BaseIt == Uses.end() && RootIt == Uses.end() && |
| 1391 | "Mismatched set sizes!"); |
James Molloy | 5f255eb | 2015-01-29 13:48:05 +0000 | [diff] [blame] | 1392 | } |
| 1393 | |
James Molloy | 5f255eb | 2015-01-29 13:48:05 +0000 | [diff] [blame] | 1394 | DEBUG(dbgs() << "LRR: Matched all iteration increments for " << |
James Molloy | f147359 | 2015-02-11 09:19:47 +0000 | [diff] [blame] | 1395 | *IV << "\n"); |
James Molloy | 5f255eb | 2015-01-29 13:48:05 +0000 | [diff] [blame] | 1396 | |
Hal Finkel | bf45efd | 2013-11-16 23:59:05 +0000 | [diff] [blame] | 1397 | return true; |
| 1398 | } |
| 1399 | |
James Molloy | 5f255eb | 2015-01-29 13:48:05 +0000 | [diff] [blame] | 1400 | void LoopReroll::DAGRootTracker::replace(const SCEV *IterCount) { |
| 1401 | BasicBlock *Header = L->getHeader(); |
| 1402 | // Remove instructions associated with non-base iterations. |
| 1403 | for (BasicBlock::reverse_iterator J = Header->rbegin(); |
| 1404 | J != Header->rend();) { |
James Molloy | 64419d4 | 2015-01-29 21:52:03 +0000 | [diff] [blame] | 1405 | unsigned I = Uses[&*J].find_first(); |
James Molloy | f147359 | 2015-02-11 09:19:47 +0000 | [diff] [blame] | 1406 | if (I > 0 && I < IL_All) { |
James Molloy | 5f255eb | 2015-01-29 13:48:05 +0000 | [diff] [blame] | 1407 | Instruction *D = &*J; |
| 1408 | DEBUG(dbgs() << "LRR: removing: " << *D << "\n"); |
| 1409 | D->eraseFromParent(); |
| 1410 | continue; |
| 1411 | } |
| 1412 | |
| 1413 | ++J; |
| 1414 | } |
| 1415 | |
Lawrence Hu | 1befea2 | 2016-04-30 00:51:22 +0000 | [diff] [blame^] | 1416 | bool HasTwoIVs = LoopControlIV && LoopControlIV != IV; |
| 1417 | |
| 1418 | if (HasTwoIVs) { |
| 1419 | updateNonLoopCtrlIncr(); |
| 1420 | replaceIV(LoopControlIV, LoopControlIV, IterCount); |
| 1421 | } else |
| 1422 | // We need to create a new induction variable for each different BaseInst. |
| 1423 | for (auto &DRS : RootSets) |
| 1424 | // Insert the new induction variable. |
| 1425 | replaceIV(DRS.BaseInst, IV, IterCount); |
Lawrence Hu | b917cd9 | 2016-01-25 19:36:30 +0000 | [diff] [blame] | 1426 | |
| 1427 | SimplifyInstructionsInBlock(Header, TLI); |
| 1428 | DeleteDeadPHIs(Header, TLI); |
Lawrence Hu | 84b6195 | 2016-01-25 18:53:39 +0000 | [diff] [blame] | 1429 | } |
| 1430 | |
Lawrence Hu | 1befea2 | 2016-04-30 00:51:22 +0000 | [diff] [blame^] | 1431 | // For non-loop-control IVs, we only need to update the last increment |
| 1432 | // with right amount, then we are done. |
| 1433 | void LoopReroll::DAGRootTracker::updateNonLoopCtrlIncr() { |
| 1434 | const SCEV *NewInc = nullptr; |
| 1435 | for (auto *LoopInc : LoopIncs) { |
| 1436 | GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(LoopInc); |
| 1437 | const SCEVConstant *COp = nullptr; |
| 1438 | if (GEP && LoopInc->getOperand(0)->getType()->isPointerTy()) { |
| 1439 | COp = dyn_cast<SCEVConstant>(SE->getSCEV(LoopInc->getOperand(1))); |
| 1440 | } else { |
| 1441 | COp = dyn_cast<SCEVConstant>(SE->getSCEV(LoopInc->getOperand(0))); |
| 1442 | if (!COp) |
| 1443 | COp = dyn_cast<SCEVConstant>(SE->getSCEV(LoopInc->getOperand(1))); |
| 1444 | } |
| 1445 | |
| 1446 | assert(COp && "Didn't find constant operand of LoopInc!\n"); |
| 1447 | |
| 1448 | const APInt &AInt = COp->getValue()->getValue(); |
| 1449 | const SCEV *ScaleSCEV = SE->getConstant(COp->getType(), Scale); |
| 1450 | if (AInt.isNegative()) { |
| 1451 | NewInc = SE->getNegativeSCEV(COp); |
| 1452 | NewInc = SE->getUDivExpr(NewInc, ScaleSCEV); |
| 1453 | NewInc = SE->getNegativeSCEV(NewInc); |
| 1454 | } else |
| 1455 | NewInc = SE->getUDivExpr(COp, ScaleSCEV); |
| 1456 | |
| 1457 | LoopInc->setOperand(1, dyn_cast<SCEVConstant>(NewInc)->getValue()); |
| 1458 | } |
| 1459 | } |
| 1460 | |
Lawrence Hu | d3d5106 | 2016-01-25 19:43:45 +0000 | [diff] [blame] | 1461 | void LoopReroll::DAGRootTracker::replaceIV(Instruction *Inst, |
| 1462 | Instruction *InstIV, |
| 1463 | const SCEV *IterCount) { |
| 1464 | BasicBlock *Header = L->getHeader(); |
| 1465 | int64_t Inc = IVToIncMap[InstIV]; |
Lawrence Hu | 1befea2 | 2016-04-30 00:51:22 +0000 | [diff] [blame^] | 1466 | bool NeedNewIV = InstIV == LoopControlIV; |
| 1467 | bool Negative = !NeedNewIV && Inc < 0; |
Lawrence Hu | d3d5106 | 2016-01-25 19:43:45 +0000 | [diff] [blame] | 1468 | |
| 1469 | const SCEVAddRecExpr *RealIVSCEV = cast<SCEVAddRecExpr>(SE->getSCEV(Inst)); |
| 1470 | const SCEV *Start = RealIVSCEV->getStart(); |
| 1471 | |
Lawrence Hu | 1befea2 | 2016-04-30 00:51:22 +0000 | [diff] [blame^] | 1472 | if (NeedNewIV) |
| 1473 | Start = SE->getConstant(Start->getType(), 0); |
| 1474 | |
Lawrence Hu | d3d5106 | 2016-01-25 19:43:45 +0000 | [diff] [blame] | 1475 | const SCEV *SizeOfExpr = nullptr; |
| 1476 | const SCEV *IncrExpr = |
| 1477 | SE->getConstant(RealIVSCEV->getType(), Negative ? -1 : 1); |
| 1478 | if (auto *PTy = dyn_cast<PointerType>(Inst->getType())) { |
| 1479 | Type *ElTy = PTy->getElementType(); |
| 1480 | SizeOfExpr = |
| 1481 | SE->getSizeOfExpr(SE->getEffectiveSCEVType(Inst->getType()), ElTy); |
| 1482 | IncrExpr = SE->getMulExpr(IncrExpr, SizeOfExpr); |
| 1483 | } |
| 1484 | const SCEV *NewIVSCEV = |
| 1485 | SE->getAddRecExpr(Start, IncrExpr, L, SCEV::FlagAnyWrap); |
| 1486 | |
| 1487 | { // Limit the lifetime of SCEVExpander. |
| 1488 | const DataLayout &DL = Header->getModule()->getDataLayout(); |
| 1489 | SCEVExpander Expander(*SE, DL, "reroll"); |
| 1490 | Value *NewIV = |
| 1491 | Expander.expandCodeFor(NewIVSCEV, InstIV->getType(), &Header->front()); |
| 1492 | |
| 1493 | for (auto &KV : Uses) |
| 1494 | if (KV.second.find_first() == 0) |
| 1495 | KV.first->replaceUsesOfWith(Inst, NewIV); |
| 1496 | |
| 1497 | if (BranchInst *BI = dyn_cast<BranchInst>(Header->getTerminator())) { |
| 1498 | // FIXME: Why do we need this check? |
| 1499 | if (Uses[BI].find_first() == IL_All) { |
| 1500 | const SCEV *ICSCEV = RealIVSCEV->evaluateAtIteration(IterCount, *SE); |
| 1501 | |
Lawrence Hu | 1befea2 | 2016-04-30 00:51:22 +0000 | [diff] [blame^] | 1502 | if (NeedNewIV) |
| 1503 | ICSCEV = SE->getMulExpr(IterCount, |
| 1504 | SE->getConstant(IterCount->getType(), Scale)); |
| 1505 | else |
| 1506 | ICSCEV = RealIVSCEV->evaluateAtIteration(IterCount, *SE); |
| 1507 | |
Lawrence Hu | d3d5106 | 2016-01-25 19:43:45 +0000 | [diff] [blame] | 1508 | // Iteration count SCEV minus or plus 1 |
| 1509 | const SCEV *MinusPlus1SCEV = |
| 1510 | SE->getConstant(ICSCEV->getType(), Negative ? -1 : 1); |
| 1511 | if (Inst->getType()->isPointerTy()) { |
| 1512 | assert(SizeOfExpr && "SizeOfExpr is not initialized"); |
| 1513 | MinusPlus1SCEV = SE->getMulExpr(MinusPlus1SCEV, SizeOfExpr); |
| 1514 | } |
| 1515 | |
| 1516 | const SCEV *ICMinusPlus1SCEV = SE->getMinusSCEV(ICSCEV, MinusPlus1SCEV); |
| 1517 | // Iteration count minus 1 |
| 1518 | Value *ICMinusPlus1 = nullptr; |
| 1519 | if (isa<SCEVConstant>(ICMinusPlus1SCEV)) { |
| 1520 | ICMinusPlus1 = |
| 1521 | Expander.expandCodeFor(ICMinusPlus1SCEV, NewIV->getType(), BI); |
| 1522 | } else { |
| 1523 | BasicBlock *Preheader = L->getLoopPreheader(); |
| 1524 | if (!Preheader) |
| 1525 | Preheader = InsertPreheaderForLoop(L, DT, LI, PreserveLCSSA); |
| 1526 | ICMinusPlus1 = Expander.expandCodeFor( |
| 1527 | ICMinusPlus1SCEV, NewIV->getType(), Preheader->getTerminator()); |
| 1528 | } |
| 1529 | |
| 1530 | Value *Cond = |
| 1531 | new ICmpInst(BI, CmpInst::ICMP_EQ, NewIV, ICMinusPlus1, "exitcond"); |
| 1532 | BI->setCondition(Cond); |
| 1533 | |
| 1534 | if (BI->getSuccessor(1) != Header) |
| 1535 | BI->swapSuccessors(); |
| 1536 | } |
| 1537 | } |
| 1538 | } |
| 1539 | } |
| 1540 | |
Hal Finkel | bf45efd | 2013-11-16 23:59:05 +0000 | [diff] [blame] | 1541 | // Validate the selected reductions. All iterations must have an isomorphic |
| 1542 | // part of the reduction chain and, for non-associative reductions, the chain |
| 1543 | // entries must appear in order. |
| 1544 | bool LoopReroll::ReductionTracker::validateSelected() { |
| 1545 | // For a non-associative reduction, the chain entries must appear in order. |
| 1546 | for (DenseSet<int>::iterator RI = Reds.begin(), RIE = Reds.end(); |
| 1547 | RI != RIE; ++RI) { |
| 1548 | int i = *RI; |
| 1549 | int PrevIter = 0, BaseCount = 0, Count = 0; |
NAKAMURA Takumi | 5af50a5 | 2014-10-28 11:54:05 +0000 | [diff] [blame] | 1550 | for (Instruction *J : PossibleReds[i]) { |
| 1551 | // Note that all instructions in the chain must have been found because |
| 1552 | // all instructions in the function must have been assigned to some |
| 1553 | // iteration. |
| 1554 | int Iter = PossibleRedIter[J]; |
Hal Finkel | bf45efd | 2013-11-16 23:59:05 +0000 | [diff] [blame] | 1555 | if (Iter != PrevIter && Iter != PrevIter + 1 && |
| 1556 | !PossibleReds[i].getReducedValue()->isAssociative()) { |
| 1557 | DEBUG(dbgs() << "LRR: Out-of-order non-associative reduction: " << |
NAKAMURA Takumi | 5af50a5 | 2014-10-28 11:54:05 +0000 | [diff] [blame] | 1558 | J << "\n"); |
Hal Finkel | bf45efd | 2013-11-16 23:59:05 +0000 | [diff] [blame] | 1559 | return false; |
| 1560 | } |
| 1561 | |
| 1562 | if (Iter != PrevIter) { |
| 1563 | if (Count != BaseCount) { |
| 1564 | DEBUG(dbgs() << "LRR: Iteration " << PrevIter << |
| 1565 | " reduction use count " << Count << |
| 1566 | " is not equal to the base use count " << |
| 1567 | BaseCount << "\n"); |
| 1568 | return false; |
| 1569 | } |
| 1570 | |
| 1571 | Count = 0; |
| 1572 | } |
| 1573 | |
| 1574 | ++Count; |
| 1575 | if (Iter == 0) |
| 1576 | ++BaseCount; |
| 1577 | |
| 1578 | PrevIter = Iter; |
| 1579 | } |
| 1580 | } |
| 1581 | |
| 1582 | return true; |
| 1583 | } |
| 1584 | |
| 1585 | // For all selected reductions, remove all parts except those in the first |
| 1586 | // iteration (and the PHI). Replace outside uses of the reduced value with uses |
| 1587 | // of the first-iteration reduced value (in other words, reroll the selected |
| 1588 | // reductions). |
| 1589 | void LoopReroll::ReductionTracker::replaceSelected() { |
| 1590 | // Fixup reductions to refer to the last instruction associated with the |
| 1591 | // first iteration (not the last). |
| 1592 | for (DenseSet<int>::iterator RI = Reds.begin(), RIE = Reds.end(); |
| 1593 | RI != RIE; ++RI) { |
| 1594 | int i = *RI; |
| 1595 | int j = 0; |
| 1596 | for (int e = PossibleReds[i].size(); j != e; ++j) |
| 1597 | if (PossibleRedIter[PossibleReds[i][j]] != 0) { |
| 1598 | --j; |
| 1599 | break; |
| 1600 | } |
| 1601 | |
| 1602 | // Replace users with the new end-of-chain value. |
| 1603 | SmallInstructionVector Users; |
James Molloy | 64419d4 | 2015-01-29 21:52:03 +0000 | [diff] [blame] | 1604 | for (User *U : PossibleReds[i].getReducedValue()->users()) { |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 1605 | Users.push_back(cast<Instruction>(U)); |
James Molloy | 64419d4 | 2015-01-29 21:52:03 +0000 | [diff] [blame] | 1606 | } |
Hal Finkel | bf45efd | 2013-11-16 23:59:05 +0000 | [diff] [blame] | 1607 | |
| 1608 | for (SmallInstructionVector::iterator J = Users.begin(), |
| 1609 | JE = Users.end(); J != JE; ++J) |
| 1610 | (*J)->replaceUsesOfWith(PossibleReds[i].getReducedValue(), |
| 1611 | PossibleReds[i][j]); |
| 1612 | } |
| 1613 | } |
| 1614 | |
| 1615 | // Reroll the provided loop with respect to the provided induction variable. |
| 1616 | // Generally, we're looking for a loop like this: |
| 1617 | // |
| 1618 | // %iv = phi [ (preheader, ...), (body, %iv.next) ] |
| 1619 | // f(%iv) |
| 1620 | // %iv.1 = add %iv, 1 <-- a root increment |
| 1621 | // f(%iv.1) |
| 1622 | // %iv.2 = add %iv, 2 <-- a root increment |
| 1623 | // f(%iv.2) |
| 1624 | // %iv.scale_m_1 = add %iv, scale-1 <-- a root increment |
| 1625 | // f(%iv.scale_m_1) |
| 1626 | // ... |
| 1627 | // %iv.next = add %iv, scale |
| 1628 | // %cmp = icmp(%iv, ...) |
| 1629 | // br %cmp, header, exit |
| 1630 | // |
| 1631 | // Notably, we do not require that f(%iv), f(%iv.1), etc. be isolated groups of |
| 1632 | // instructions. In other words, the instructions in f(%iv), f(%iv.1), etc. can |
| 1633 | // be intermixed with eachother. The restriction imposed by this algorithm is |
| 1634 | // that the relative order of the isomorphic instructions in f(%iv), f(%iv.1), |
| 1635 | // etc. be the same. |
| 1636 | // |
| 1637 | // First, we collect the use set of %iv, excluding the other increment roots. |
| 1638 | // This gives us f(%iv). Then we iterate over the loop instructions (scale-1) |
| 1639 | // times, having collected the use set of f(%iv.(i+1)), during which we: |
| 1640 | // - Ensure that the next unmatched instruction in f(%iv) is isomorphic to |
| 1641 | // the next unmatched instruction in f(%iv.(i+1)). |
| 1642 | // - Ensure that both matched instructions don't have any external users |
| 1643 | // (with the exception of last-in-chain reduction instructions). |
| 1644 | // - Track the (aliasing) write set, and other side effects, of all |
| 1645 | // instructions that belong to future iterations that come before the matched |
| 1646 | // instructions. If the matched instructions read from that write set, then |
| 1647 | // f(%iv) or f(%iv.(i+1)) has some dependency on instructions in |
| 1648 | // f(%iv.(j+1)) for some j > i, and we cannot reroll the loop. Similarly, |
| 1649 | // if any of these future instructions had side effects (could not be |
| 1650 | // speculatively executed), and so do the matched instructions, when we |
| 1651 | // cannot reorder those side-effect-producing instructions, and rerolling |
| 1652 | // fails. |
| 1653 | // |
| 1654 | // Finally, we make sure that all loop instructions are either loop increment |
| 1655 | // roots, belong to simple latch code, parts of validated reductions, part of |
| 1656 | // f(%iv) or part of some f(%iv.i). If all of that is true (and all reductions |
| 1657 | // have been validated), then we reroll the loop. |
| 1658 | bool LoopReroll::reroll(Instruction *IV, Loop *L, BasicBlock *Header, |
| 1659 | const SCEV *IterCount, |
| 1660 | ReductionTracker &Reductions) { |
Justin Bogner | 843fb20 | 2015-12-15 19:40:57 +0000 | [diff] [blame] | 1661 | DAGRootTracker DAGRoots(this, L, IV, SE, AA, TLI, DT, LI, PreserveLCSSA, |
Lawrence Hu | 1befea2 | 2016-04-30 00:51:22 +0000 | [diff] [blame^] | 1662 | IVToIncMap, LoopControlIV); |
Hal Finkel | bf45efd | 2013-11-16 23:59:05 +0000 | [diff] [blame] | 1663 | |
James Molloy | 5f255eb | 2015-01-29 13:48:05 +0000 | [diff] [blame] | 1664 | if (!DAGRoots.findRoots()) |
Hal Finkel | bf45efd | 2013-11-16 23:59:05 +0000 | [diff] [blame] | 1665 | return false; |
Hal Finkel | bf45efd | 2013-11-16 23:59:05 +0000 | [diff] [blame] | 1666 | DEBUG(dbgs() << "LRR: Found all root induction increments for: " << |
James Molloy | 5f255eb | 2015-01-29 13:48:05 +0000 | [diff] [blame] | 1667 | *IV << "\n"); |
Lawrence Hu | dc8a83b | 2015-07-24 22:01:49 +0000 | [diff] [blame] | 1668 | |
James Molloy | 5f255eb | 2015-01-29 13:48:05 +0000 | [diff] [blame] | 1669 | if (!DAGRoots.validate(Reductions)) |
Hal Finkel | bf45efd | 2013-11-16 23:59:05 +0000 | [diff] [blame] | 1670 | return false; |
Hal Finkel | bf45efd | 2013-11-16 23:59:05 +0000 | [diff] [blame] | 1671 | if (!Reductions.validateSelected()) |
| 1672 | return false; |
Hal Finkel | bf45efd | 2013-11-16 23:59:05 +0000 | [diff] [blame] | 1673 | // At this point, we've validated the rerolling, and we're committed to |
| 1674 | // making changes! |
| 1675 | |
| 1676 | Reductions.replaceSelected(); |
James Molloy | 5f255eb | 2015-01-29 13:48:05 +0000 | [diff] [blame] | 1677 | DAGRoots.replace(IterCount); |
Hal Finkel | bf45efd | 2013-11-16 23:59:05 +0000 | [diff] [blame] | 1678 | |
Hal Finkel | bf45efd | 2013-11-16 23:59:05 +0000 | [diff] [blame] | 1679 | ++NumRerolledLoops; |
| 1680 | return true; |
| 1681 | } |
| 1682 | |
| 1683 | bool LoopReroll::runOnLoop(Loop *L, LPPassManager &LPM) { |
Andrew Kaylor | aa641a5 | 2016-04-22 22:06:11 +0000 | [diff] [blame] | 1684 | if (skipLoop(L)) |
Paul Robinson | af4e64d | 2014-02-06 00:07:05 +0000 | [diff] [blame] | 1685 | return false; |
| 1686 | |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 1687 | AA = &getAnalysis<AAResultsWrapperPass>().getAAResults(); |
Chandler Carruth | 4f8f307 | 2015-01-17 14:16:18 +0000 | [diff] [blame] | 1688 | LI = &getAnalysis<LoopInfoWrapperPass>().getLoopInfo(); |
Chandler Carruth | 2f1fd16 | 2015-08-17 02:08:17 +0000 | [diff] [blame] | 1689 | SE = &getAnalysis<ScalarEvolutionWrapperPass>().getSE(); |
Chandler Carruth | b98f63d | 2015-01-15 10:41:28 +0000 | [diff] [blame] | 1690 | TLI = &getAnalysis<TargetLibraryInfoWrapperPass>().getTLI(); |
Chandler Carruth | 7352302 | 2014-01-13 13:07:17 +0000 | [diff] [blame] | 1691 | DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree(); |
Justin Bogner | 843fb20 | 2015-12-15 19:40:57 +0000 | [diff] [blame] | 1692 | PreserveLCSSA = mustPreserveAnalysisID(LCSSAID); |
Hal Finkel | bf45efd | 2013-11-16 23:59:05 +0000 | [diff] [blame] | 1693 | |
| 1694 | BasicBlock *Header = L->getHeader(); |
| 1695 | DEBUG(dbgs() << "LRR: F[" << Header->getParent()->getName() << |
| 1696 | "] Loop %" << Header->getName() << " (" << |
| 1697 | L->getNumBlocks() << " block(s))\n"); |
| 1698 | |
Hal Finkel | bf45efd | 2013-11-16 23:59:05 +0000 | [diff] [blame] | 1699 | // For now, we'll handle only single BB loops. |
| 1700 | if (L->getNumBlocks() > 1) |
Zinovy Nis | 07ac2bd | 2016-03-22 13:50:57 +0000 | [diff] [blame] | 1701 | return false; |
Hal Finkel | bf45efd | 2013-11-16 23:59:05 +0000 | [diff] [blame] | 1702 | |
| 1703 | if (!SE->hasLoopInvariantBackedgeTakenCount(L)) |
Zinovy Nis | 07ac2bd | 2016-03-22 13:50:57 +0000 | [diff] [blame] | 1704 | return false; |
Hal Finkel | bf45efd | 2013-11-16 23:59:05 +0000 | [diff] [blame] | 1705 | |
| 1706 | const SCEV *LIBETC = SE->getBackedgeTakenCount(L); |
Sanjoy Das | 2aacc0e | 2015-09-23 01:59:04 +0000 | [diff] [blame] | 1707 | const SCEV *IterCount = SE->getAddExpr(LIBETC, SE->getOne(LIBETC->getType())); |
Hal Finkel | bf45efd | 2013-11-16 23:59:05 +0000 | [diff] [blame] | 1708 | DEBUG(dbgs() << "LRR: iteration count = " << *IterCount << "\n"); |
| 1709 | |
| 1710 | // First, we need to find the induction variable with respect to which we can |
| 1711 | // reroll (there may be several possible options). |
| 1712 | SmallInstructionVector PossibleIVs; |
Lawrence Hu | dc8a83b | 2015-07-24 22:01:49 +0000 | [diff] [blame] | 1713 | IVToIncMap.clear(); |
Lawrence Hu | 1befea2 | 2016-04-30 00:51:22 +0000 | [diff] [blame^] | 1714 | LoopControlIV = nullptr; |
Hal Finkel | bf45efd | 2013-11-16 23:59:05 +0000 | [diff] [blame] | 1715 | collectPossibleIVs(L, PossibleIVs); |
| 1716 | |
| 1717 | if (PossibleIVs.empty()) { |
| 1718 | DEBUG(dbgs() << "LRR: No possible IVs found\n"); |
Zinovy Nis | 07ac2bd | 2016-03-22 13:50:57 +0000 | [diff] [blame] | 1719 | return false; |
Hal Finkel | bf45efd | 2013-11-16 23:59:05 +0000 | [diff] [blame] | 1720 | } |
| 1721 | |
| 1722 | ReductionTracker Reductions; |
| 1723 | collectPossibleReductions(L, Reductions); |
Zinovy Nis | 07ac2bd | 2016-03-22 13:50:57 +0000 | [diff] [blame] | 1724 | bool Changed = false; |
Hal Finkel | bf45efd | 2013-11-16 23:59:05 +0000 | [diff] [blame] | 1725 | |
| 1726 | // For each possible IV, collect the associated possible set of 'root' nodes |
| 1727 | // (i+1, i+2, etc.). |
| 1728 | for (SmallInstructionVector::iterator I = PossibleIVs.begin(), |
| 1729 | IE = PossibleIVs.end(); I != IE; ++I) |
| 1730 | if (reroll(*I, L, Header, IterCount, Reductions)) { |
| 1731 | Changed = true; |
| 1732 | break; |
| 1733 | } |
| 1734 | |
Zinovy Nis | 07ac2bd | 2016-03-22 13:50:57 +0000 | [diff] [blame] | 1735 | // Trip count of L has changed so SE must be re-evaluated. |
| 1736 | if (Changed) |
| 1737 | SE->forgetLoop(L); |
| 1738 | |
Hal Finkel | bf45efd | 2013-11-16 23:59:05 +0000 | [diff] [blame] | 1739 | return Changed; |
| 1740 | } |