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