Andrew Trick | 3ec331e | 2011-08-10 03:46:27 +0000 | [diff] [blame] | 1 | //===-- SimplifyIndVar.cpp - Induction variable simplification ------------===// |
| 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 file implements induction variable simplification. It does |
| 11 | // not define any actual pass or policy, but provides a single function to |
| 12 | // simplify a loop's induction variables based on ScalarEvolution. |
| 13 | // |
| 14 | //===----------------------------------------------------------------------===// |
| 15 | |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 16 | #include "llvm/Transforms/Utils/SimplifyIndVar.h" |
Chandler Carruth | 8a8cd2b | 2014-01-07 11:48:04 +0000 | [diff] [blame] | 17 | #include "llvm/ADT/STLExtras.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 18 | #include "llvm/ADT/SmallVector.h" |
| 19 | #include "llvm/ADT/Statistic.h" |
Andrew Trick | 3ec331e | 2011-08-10 03:46:27 +0000 | [diff] [blame] | 20 | #include "llvm/Analysis/LoopInfo.h" |
| 21 | #include "llvm/Analysis/LoopPass.h" |
| 22 | #include "llvm/Analysis/ScalarEvolutionExpressions.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 23 | #include "llvm/IR/DataLayout.h" |
Chandler Carruth | 5ad5f15 | 2014-01-13 09:26:24 +0000 | [diff] [blame] | 24 | #include "llvm/IR/Dominators.h" |
Chandler Carruth | 8a8cd2b | 2014-01-07 11:48:04 +0000 | [diff] [blame] | 25 | #include "llvm/IR/IRBuilder.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 26 | #include "llvm/IR/Instructions.h" |
Andrew Trick | 0ba77a0 | 2013-12-23 23:31:49 +0000 | [diff] [blame] | 27 | #include "llvm/IR/IntrinsicInst.h" |
Andrew Trick | 3ec331e | 2011-08-10 03:46:27 +0000 | [diff] [blame] | 28 | #include "llvm/Support/Debug.h" |
| 29 | #include "llvm/Support/raw_ostream.h" |
Andrew Trick | 3ec331e | 2011-08-10 03:46:27 +0000 | [diff] [blame] | 30 | |
| 31 | using namespace llvm; |
| 32 | |
Chandler Carruth | 964daaa | 2014-04-22 02:55:47 +0000 | [diff] [blame] | 33 | #define DEBUG_TYPE "indvars" |
| 34 | |
Andrew Trick | 3ec331e | 2011-08-10 03:46:27 +0000 | [diff] [blame] | 35 | STATISTIC(NumElimIdentity, "Number of IV identities eliminated"); |
| 36 | STATISTIC(NumElimOperand, "Number of IV operands folded into a use"); |
| 37 | STATISTIC(NumElimRem , "Number of IV remainder operations eliminated"); |
| 38 | STATISTIC(NumElimCmp , "Number of IV comparisons eliminated"); |
| 39 | |
| 40 | namespace { |
Sanjay Patel | 7777b50 | 2014-11-12 18:07:42 +0000 | [diff] [blame] | 41 | /// This is a utility for simplifying induction variables |
Andrew Trick | 3ec331e | 2011-08-10 03:46:27 +0000 | [diff] [blame] | 42 | /// based on ScalarEvolution. It is the primary instrument of the |
| 43 | /// IndvarSimplify pass, but it may also be directly invoked to cleanup after |
| 44 | /// other loop passes that preserve SCEV. |
| 45 | class SimplifyIndvar { |
| 46 | Loop *L; |
| 47 | LoopInfo *LI; |
Andrew Trick | 3ec331e | 2011-08-10 03:46:27 +0000 | [diff] [blame] | 48 | ScalarEvolution *SE; |
Sanjoy Das | 5c8bead | 2015-10-06 21:44:49 +0000 | [diff] [blame] | 49 | DominatorTree *DT; |
Andrew Trick | 3ec331e | 2011-08-10 03:46:27 +0000 | [diff] [blame] | 50 | |
| 51 | SmallVectorImpl<WeakVH> &DeadInsts; |
| 52 | |
| 53 | bool Changed; |
| 54 | |
| 55 | public: |
Sanjoy Das | 5c8bead | 2015-10-06 21:44:49 +0000 | [diff] [blame] | 56 | SimplifyIndvar(Loop *Loop, ScalarEvolution *SE, DominatorTree *DT, |
| 57 | LoopInfo *LI,SmallVectorImpl<WeakVH> &Dead) |
| 58 | : L(Loop), LI(LI), SE(SE), DT(DT), DeadInsts(Dead), Changed(false) { |
Andrew Trick | e629d00 | 2011-08-10 04:22:26 +0000 | [diff] [blame] | 59 | assert(LI && "IV simplification requires LoopInfo"); |
Andrew Trick | 3ec331e | 2011-08-10 03:46:27 +0000 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | bool hasChanged() const { return Changed; } |
| 63 | |
| 64 | /// Iteratively perform simplification on a worklist of users of the |
| 65 | /// specified induction variable. This is the top-level driver that applies |
Benjamin Kramer | df005cb | 2015-08-08 18:27:36 +0000 | [diff] [blame] | 66 | /// all simplifications to users of an IV. |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 67 | void simplifyUsers(PHINode *CurrIV, IVVisitor *V = nullptr); |
Andrew Trick | 3ec331e | 2011-08-10 03:46:27 +0000 | [diff] [blame] | 68 | |
Andrew Trick | 74664d5 | 2011-08-10 04:01:31 +0000 | [diff] [blame] | 69 | Value *foldIVUser(Instruction *UseInst, Instruction *IVOperand); |
Andrew Trick | 3ec331e | 2011-08-10 03:46:27 +0000 | [diff] [blame] | 70 | |
Sanjoy Das | 088bb0e | 2015-10-06 21:44:39 +0000 | [diff] [blame] | 71 | bool eliminateIdentitySCEV(Instruction *UseInst, Instruction *IVOperand); |
| 72 | |
Sanjoy Das | ae09b3c | 2016-05-29 00:36:25 +0000 | [diff] [blame^] | 73 | bool eliminateOverflowIntrinsic(CallInst *CI); |
Andrew Trick | 3ec331e | 2011-08-10 03:46:27 +0000 | [diff] [blame] | 74 | bool eliminateIVUser(Instruction *UseInst, Instruction *IVOperand); |
| 75 | void eliminateIVComparison(ICmpInst *ICmp, Value *IVOperand); |
| 76 | void eliminateIVRemainder(BinaryOperator *Rem, Value *IVOperand, |
| 77 | bool IsSigned); |
Sanjoy Das | 7c0ce26 | 2015-01-06 19:02:56 +0000 | [diff] [blame] | 78 | bool strengthenOverflowingOperation(BinaryOperator *OBO, Value *IVOperand); |
Andrew Trick | 0ba77a0 | 2013-12-23 23:31:49 +0000 | [diff] [blame] | 79 | |
| 80 | Instruction *splitOverflowIntrinsic(Instruction *IVUser, |
| 81 | const DominatorTree *DT); |
Andrew Trick | 3ec331e | 2011-08-10 03:46:27 +0000 | [diff] [blame] | 82 | }; |
Alexander Kornienko | f00654e | 2015-06-23 09:49:53 +0000 | [diff] [blame] | 83 | } |
Andrew Trick | 3ec331e | 2011-08-10 03:46:27 +0000 | [diff] [blame] | 84 | |
Sanjay Patel | 7777b50 | 2014-11-12 18:07:42 +0000 | [diff] [blame] | 85 | /// Fold an IV operand into its use. This removes increments of an |
Andrew Trick | 3ec331e | 2011-08-10 03:46:27 +0000 | [diff] [blame] | 86 | /// aligned IV when used by a instruction that ignores the low bits. |
Andrew Trick | 74664d5 | 2011-08-10 04:01:31 +0000 | [diff] [blame] | 87 | /// |
Andrew Trick | 7251e41 | 2011-09-19 17:54:39 +0000 | [diff] [blame] | 88 | /// IVOperand is guaranteed SCEVable, but UseInst may not be. |
| 89 | /// |
Andrew Trick | 74664d5 | 2011-08-10 04:01:31 +0000 | [diff] [blame] | 90 | /// Return the operand of IVOperand for this induction variable if IVOperand can |
Andrew Trick | 6dbb060 | 2011-08-10 18:07:05 +0000 | [diff] [blame] | 91 | /// be folded (in case more folding opportunities have been exposed). |
Andrew Trick | 74664d5 | 2011-08-10 04:01:31 +0000 | [diff] [blame] | 92 | /// Otherwise return null. |
| 93 | Value *SimplifyIndvar::foldIVUser(Instruction *UseInst, Instruction *IVOperand) { |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 94 | Value *IVSrc = nullptr; |
Andrew Trick | 3ec331e | 2011-08-10 03:46:27 +0000 | [diff] [blame] | 95 | unsigned OperIdx = 0; |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 96 | const SCEV *FoldedExpr = nullptr; |
Andrew Trick | 3ec331e | 2011-08-10 03:46:27 +0000 | [diff] [blame] | 97 | switch (UseInst->getOpcode()) { |
| 98 | default: |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 99 | return nullptr; |
Andrew Trick | 3ec331e | 2011-08-10 03:46:27 +0000 | [diff] [blame] | 100 | case Instruction::UDiv: |
| 101 | case Instruction::LShr: |
| 102 | // We're only interested in the case where we know something about |
| 103 | // the numerator and have a constant denominator. |
| 104 | if (IVOperand != UseInst->getOperand(OperIdx) || |
| 105 | !isa<ConstantInt>(UseInst->getOperand(1))) |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 106 | return nullptr; |
Andrew Trick | 3ec331e | 2011-08-10 03:46:27 +0000 | [diff] [blame] | 107 | |
| 108 | // Attempt to fold a binary operator with constant operand. |
| 109 | // e.g. ((I + 1) >> 2) => I >> 2 |
Andrew Trick | 9490458 | 2011-11-17 23:36:35 +0000 | [diff] [blame] | 110 | if (!isa<BinaryOperator>(IVOperand) |
| 111 | || !isa<ConstantInt>(IVOperand->getOperand(1))) |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 112 | return nullptr; |
Andrew Trick | 3ec331e | 2011-08-10 03:46:27 +0000 | [diff] [blame] | 113 | |
| 114 | IVSrc = IVOperand->getOperand(0); |
| 115 | // IVSrc must be the (SCEVable) IV, since the other operand is const. |
| 116 | assert(SE->isSCEVable(IVSrc->getType()) && "Expect SCEVable IV operand"); |
| 117 | |
| 118 | ConstantInt *D = cast<ConstantInt>(UseInst->getOperand(1)); |
| 119 | if (UseInst->getOpcode() == Instruction::LShr) { |
| 120 | // Get a constant for the divisor. See createSCEV. |
| 121 | uint32_t BitWidth = cast<IntegerType>(UseInst->getType())->getBitWidth(); |
| 122 | if (D->getValue().uge(BitWidth)) |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 123 | return nullptr; |
Andrew Trick | 3ec331e | 2011-08-10 03:46:27 +0000 | [diff] [blame] | 124 | |
| 125 | D = ConstantInt::get(UseInst->getContext(), |
Benjamin Kramer | fc3ea6f | 2013-07-11 16:05:50 +0000 | [diff] [blame] | 126 | APInt::getOneBitSet(BitWidth, D->getZExtValue())); |
Andrew Trick | 3ec331e | 2011-08-10 03:46:27 +0000 | [diff] [blame] | 127 | } |
| 128 | FoldedExpr = SE->getUDivExpr(SE->getSCEV(IVSrc), SE->getSCEV(D)); |
| 129 | } |
| 130 | // We have something that might fold it's operand. Compare SCEVs. |
| 131 | if (!SE->isSCEVable(UseInst->getType())) |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 132 | return nullptr; |
Andrew Trick | 3ec331e | 2011-08-10 03:46:27 +0000 | [diff] [blame] | 133 | |
| 134 | // Bypass the operand if SCEV can prove it has no effect. |
| 135 | if (SE->getSCEV(UseInst) != FoldedExpr) |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 136 | return nullptr; |
Andrew Trick | 3ec331e | 2011-08-10 03:46:27 +0000 | [diff] [blame] | 137 | |
| 138 | DEBUG(dbgs() << "INDVARS: Eliminated IV operand: " << *IVOperand |
| 139 | << " -> " << *UseInst << '\n'); |
| 140 | |
| 141 | UseInst->setOperand(OperIdx, IVSrc); |
| 142 | assert(SE->getSCEV(UseInst) == FoldedExpr && "bad SCEV with folded oper"); |
| 143 | |
| 144 | ++NumElimOperand; |
| 145 | Changed = true; |
| 146 | if (IVOperand->use_empty()) |
Benjamin Kramer | f5e2fc4 | 2015-05-29 19:43:39 +0000 | [diff] [blame] | 147 | DeadInsts.emplace_back(IVOperand); |
Andrew Trick | 74664d5 | 2011-08-10 04:01:31 +0000 | [diff] [blame] | 148 | return IVSrc; |
Andrew Trick | 3ec331e | 2011-08-10 03:46:27 +0000 | [diff] [blame] | 149 | } |
| 150 | |
Sanjay Patel | 7777b50 | 2014-11-12 18:07:42 +0000 | [diff] [blame] | 151 | /// SimplifyIVUsers helper for eliminating useless |
Andrew Trick | 3ec331e | 2011-08-10 03:46:27 +0000 | [diff] [blame] | 152 | /// comparisons against an induction variable. |
| 153 | void SimplifyIndvar::eliminateIVComparison(ICmpInst *ICmp, Value *IVOperand) { |
| 154 | unsigned IVOperIdx = 0; |
| 155 | ICmpInst::Predicate Pred = ICmp->getPredicate(); |
| 156 | if (IVOperand != ICmp->getOperand(0)) { |
| 157 | // Swapped |
| 158 | assert(IVOperand == ICmp->getOperand(1) && "Can't find IVOperand"); |
| 159 | IVOperIdx = 1; |
| 160 | Pred = ICmpInst::getSwappedPredicate(Pred); |
| 161 | } |
| 162 | |
| 163 | // Get the SCEVs for the ICmp operands. |
| 164 | const SCEV *S = SE->getSCEV(ICmp->getOperand(IVOperIdx)); |
| 165 | const SCEV *X = SE->getSCEV(ICmp->getOperand(1 - IVOperIdx)); |
| 166 | |
| 167 | // Simplify unnecessary loops away. |
| 168 | const Loop *ICmpLoop = LI->getLoopFor(ICmp->getParent()); |
| 169 | S = SE->getSCEVAtScope(S, ICmpLoop); |
| 170 | X = SE->getSCEVAtScope(X, ICmpLoop); |
| 171 | |
Sanjoy Das | 5dab205 | 2015-07-27 21:42:49 +0000 | [diff] [blame] | 172 | ICmpInst::Predicate InvariantPredicate; |
| 173 | const SCEV *InvariantLHS, *InvariantRHS; |
| 174 | |
Andrew Trick | 3ec331e | 2011-08-10 03:46:27 +0000 | [diff] [blame] | 175 | // If the condition is always true or always false, replace it with |
| 176 | // a constant value. |
Sanjoy Das | 5dab205 | 2015-07-27 21:42:49 +0000 | [diff] [blame] | 177 | if (SE->isKnownPredicate(Pred, S, X)) { |
Andrew Trick | 3ec331e | 2011-08-10 03:46:27 +0000 | [diff] [blame] | 178 | ICmp->replaceAllUsesWith(ConstantInt::getTrue(ICmp->getContext())); |
Sanjoy Das | 5dab205 | 2015-07-27 21:42:49 +0000 | [diff] [blame] | 179 | DeadInsts.emplace_back(ICmp); |
Sanjoy Das | c18115d | 2015-08-06 20:43:28 +0000 | [diff] [blame] | 180 | DEBUG(dbgs() << "INDVARS: Eliminated comparison: " << *ICmp << '\n'); |
Sanjoy Das | 5dab205 | 2015-07-27 21:42:49 +0000 | [diff] [blame] | 181 | } else if (SE->isKnownPredicate(ICmpInst::getInversePredicate(Pred), S, X)) { |
Andrew Trick | 3ec331e | 2011-08-10 03:46:27 +0000 | [diff] [blame] | 182 | ICmp->replaceAllUsesWith(ConstantInt::getFalse(ICmp->getContext())); |
Sanjoy Das | 5dab205 | 2015-07-27 21:42:49 +0000 | [diff] [blame] | 183 | DeadInsts.emplace_back(ICmp); |
Sanjoy Das | c18115d | 2015-08-06 20:43:28 +0000 | [diff] [blame] | 184 | DEBUG(dbgs() << "INDVARS: Eliminated comparison: " << *ICmp << '\n'); |
Sanjoy Das | 5dab205 | 2015-07-27 21:42:49 +0000 | [diff] [blame] | 185 | } else if (isa<PHINode>(IVOperand) && |
Sanjoy Das | 60fb899 | 2016-03-18 20:37:07 +0000 | [diff] [blame] | 186 | SE->isLoopInvariantPredicate(Pred, S, X, L, InvariantPredicate, |
| 187 | InvariantLHS, InvariantRHS)) { |
Sanjoy Das | 5dab205 | 2015-07-27 21:42:49 +0000 | [diff] [blame] | 188 | |
Benjamin Kramer | df005cb | 2015-08-08 18:27:36 +0000 | [diff] [blame] | 189 | // Rewrite the comparison to a loop invariant comparison if it can be done |
Sanjoy Das | 5dab205 | 2015-07-27 21:42:49 +0000 | [diff] [blame] | 190 | // cheaply, where cheaply means "we don't need to emit any new |
| 191 | // instructions". |
| 192 | |
| 193 | Value *NewLHS = nullptr, *NewRHS = nullptr; |
| 194 | |
| 195 | if (S == InvariantLHS || X == InvariantLHS) |
| 196 | NewLHS = |
| 197 | ICmp->getOperand(S == InvariantLHS ? IVOperIdx : (1 - IVOperIdx)); |
| 198 | |
| 199 | if (S == InvariantRHS || X == InvariantRHS) |
| 200 | NewRHS = |
| 201 | ICmp->getOperand(S == InvariantRHS ? IVOperIdx : (1 - IVOperIdx)); |
| 202 | |
Sanjoy Das | 74af78e3 | 2016-03-18 20:37:11 +0000 | [diff] [blame] | 203 | auto *PN = cast<PHINode>(IVOperand); |
| 204 | for (unsigned i = 0, e = PN->getNumIncomingValues(); |
| 205 | i != e && (!NewLHS || !NewRHS); |
| 206 | ++i) { |
| 207 | |
| 208 | // If this is a value incoming from the backedge, then it cannot be a loop |
| 209 | // invariant value (since we know that IVOperand is an induction variable). |
| 210 | if (L->contains(PN->getIncomingBlock(i))) |
| 211 | continue; |
| 212 | |
| 213 | // NB! This following assert does not fundamentally have to be true, but |
| 214 | // it is true today given how SCEV analyzes induction variables. |
| 215 | // Specifically, today SCEV will *not* recognize %iv as an induction |
| 216 | // variable in the following case: |
| 217 | // |
| 218 | // define void @f(i32 %k) { |
| 219 | // entry: |
| 220 | // br i1 undef, label %r, label %l |
| 221 | // |
| 222 | // l: |
| 223 | // %k.inc.l = add i32 %k, 1 |
| 224 | // br label %loop |
| 225 | // |
| 226 | // r: |
| 227 | // %k.inc.r = add i32 %k, 1 |
| 228 | // br label %loop |
| 229 | // |
| 230 | // loop: |
| 231 | // %iv = phi i32 [ %k.inc.l, %l ], [ %k.inc.r, %r ], [ %iv.inc, %loop ] |
| 232 | // %iv.inc = add i32 %iv, 1 |
| 233 | // br label %loop |
| 234 | // } |
| 235 | // |
| 236 | // but if it starts to, at some point, then the assertion below will have |
| 237 | // to be changed to a runtime check. |
| 238 | |
| 239 | Value *Incoming = PN->getIncomingValue(i); |
| 240 | |
| 241 | #ifndef NDEBUG |
| 242 | if (auto *I = dyn_cast<Instruction>(Incoming)) |
| 243 | assert(DT->dominates(I, ICmp) && "Should be a unique loop dominating value!"); |
| 244 | #endif |
Sanjoy Das | 5dab205 | 2015-07-27 21:42:49 +0000 | [diff] [blame] | 245 | |
| 246 | const SCEV *IncomingS = SE->getSCEV(Incoming); |
| 247 | |
| 248 | if (!NewLHS && IncomingS == InvariantLHS) |
| 249 | NewLHS = Incoming; |
| 250 | if (!NewRHS && IncomingS == InvariantRHS) |
| 251 | NewRHS = Incoming; |
| 252 | } |
| 253 | |
| 254 | if (!NewLHS || !NewRHS) |
| 255 | // We could not find an existing value to replace either LHS or RHS. |
| 256 | // Generating new instructions has subtler tradeoffs, so avoid doing that |
| 257 | // for now. |
| 258 | return; |
| 259 | |
Sanjoy Das | c18115d | 2015-08-06 20:43:28 +0000 | [diff] [blame] | 260 | DEBUG(dbgs() << "INDVARS: Simplified comparison: " << *ICmp << '\n'); |
Sanjoy Das | 5dab205 | 2015-07-27 21:42:49 +0000 | [diff] [blame] | 261 | ICmp->setPredicate(InvariantPredicate); |
| 262 | ICmp->setOperand(0, NewLHS); |
| 263 | ICmp->setOperand(1, NewRHS); |
| 264 | } else |
Andrew Trick | 3ec331e | 2011-08-10 03:46:27 +0000 | [diff] [blame] | 265 | return; |
| 266 | |
Andrew Trick | 3ec331e | 2011-08-10 03:46:27 +0000 | [diff] [blame] | 267 | ++NumElimCmp; |
| 268 | Changed = true; |
Andrew Trick | 3ec331e | 2011-08-10 03:46:27 +0000 | [diff] [blame] | 269 | } |
| 270 | |
Sanjay Patel | 7777b50 | 2014-11-12 18:07:42 +0000 | [diff] [blame] | 271 | /// SimplifyIVUsers helper for eliminating useless |
Andrew Trick | 3ec331e | 2011-08-10 03:46:27 +0000 | [diff] [blame] | 272 | /// remainder operations operating on an induction variable. |
| 273 | void SimplifyIndvar::eliminateIVRemainder(BinaryOperator *Rem, |
| 274 | Value *IVOperand, |
| 275 | bool IsSigned) { |
| 276 | // We're only interested in the case where we know something about |
| 277 | // the numerator. |
| 278 | if (IVOperand != Rem->getOperand(0)) |
| 279 | return; |
| 280 | |
| 281 | // Get the SCEVs for the ICmp operands. |
| 282 | const SCEV *S = SE->getSCEV(Rem->getOperand(0)); |
| 283 | const SCEV *X = SE->getSCEV(Rem->getOperand(1)); |
| 284 | |
| 285 | // Simplify unnecessary loops away. |
| 286 | const Loop *ICmpLoop = LI->getLoopFor(Rem->getParent()); |
| 287 | S = SE->getSCEVAtScope(S, ICmpLoop); |
| 288 | X = SE->getSCEVAtScope(X, ICmpLoop); |
| 289 | |
| 290 | // i % n --> i if i is in [0,n). |
| 291 | if ((!IsSigned || SE->isKnownNonNegative(S)) && |
| 292 | SE->isKnownPredicate(IsSigned ? ICmpInst::ICMP_SLT : ICmpInst::ICMP_ULT, |
| 293 | S, X)) |
| 294 | Rem->replaceAllUsesWith(Rem->getOperand(0)); |
| 295 | else { |
| 296 | // (i+1) % n --> (i+1)==n?0:(i+1) if i is in [0,n). |
Sanjoy Das | 2aacc0e | 2015-09-23 01:59:04 +0000 | [diff] [blame] | 297 | const SCEV *LessOne = SE->getMinusSCEV(S, SE->getOne(S->getType())); |
Andrew Trick | 3ec331e | 2011-08-10 03:46:27 +0000 | [diff] [blame] | 298 | if (IsSigned && !SE->isKnownNonNegative(LessOne)) |
| 299 | return; |
| 300 | |
| 301 | if (!SE->isKnownPredicate(IsSigned ? |
| 302 | ICmpInst::ICMP_SLT : ICmpInst::ICMP_ULT, |
| 303 | LessOne, X)) |
| 304 | return; |
| 305 | |
| 306 | ICmpInst *ICmp = new ICmpInst(Rem, ICmpInst::ICMP_EQ, |
Benjamin Kramer | 547b6c5 | 2011-09-27 20:39:19 +0000 | [diff] [blame] | 307 | Rem->getOperand(0), Rem->getOperand(1)); |
Andrew Trick | 3ec331e | 2011-08-10 03:46:27 +0000 | [diff] [blame] | 308 | SelectInst *Sel = |
| 309 | SelectInst::Create(ICmp, |
| 310 | ConstantInt::get(Rem->getType(), 0), |
| 311 | Rem->getOperand(0), "tmp", Rem); |
| 312 | Rem->replaceAllUsesWith(Sel); |
| 313 | } |
| 314 | |
Andrew Trick | 3ec331e | 2011-08-10 03:46:27 +0000 | [diff] [blame] | 315 | DEBUG(dbgs() << "INDVARS: Simplified rem: " << *Rem << '\n'); |
| 316 | ++NumElimRem; |
| 317 | Changed = true; |
Benjamin Kramer | f5e2fc4 | 2015-05-29 19:43:39 +0000 | [diff] [blame] | 318 | DeadInsts.emplace_back(Rem); |
Andrew Trick | 3ec331e | 2011-08-10 03:46:27 +0000 | [diff] [blame] | 319 | } |
| 320 | |
Sanjoy Das | ae09b3c | 2016-05-29 00:36:25 +0000 | [diff] [blame^] | 321 | bool SimplifyIndvar::eliminateOverflowIntrinsic(CallInst *CI) { |
| 322 | auto *F = CI->getCalledFunction(); |
| 323 | if (!F) |
| 324 | return false; |
| 325 | |
| 326 | typedef const SCEV *(ScalarEvolution::*OperationFunctionTy)( |
| 327 | const SCEV *, const SCEV *, SCEV::NoWrapFlags); |
| 328 | typedef const SCEV *(ScalarEvolution::*ExtensionFunctionTy)( |
| 329 | const SCEV *, Type *); |
| 330 | |
| 331 | OperationFunctionTy Operation; |
| 332 | ExtensionFunctionTy Extension; |
| 333 | |
| 334 | Instruction::BinaryOps RawOp; |
| 335 | |
| 336 | // We always have exactly one of nsw or nuw. If NoSignedOverflow is false, we |
| 337 | // have nuw. |
| 338 | bool NoSignedOverflow; |
| 339 | |
| 340 | switch (F->getIntrinsicID()) { |
| 341 | default: |
| 342 | return false; |
| 343 | |
| 344 | case Intrinsic::sadd_with_overflow: |
| 345 | Operation = &ScalarEvolution::getAddExpr; |
| 346 | Extension = &ScalarEvolution::getSignExtendExpr; |
| 347 | RawOp = Instruction::Add; |
| 348 | NoSignedOverflow = true; |
| 349 | break; |
| 350 | |
| 351 | case Intrinsic::uadd_with_overflow: |
| 352 | Operation = &ScalarEvolution::getAddExpr; |
| 353 | Extension = &ScalarEvolution::getZeroExtendExpr; |
| 354 | RawOp = Instruction::Add; |
| 355 | NoSignedOverflow = false; |
| 356 | break; |
| 357 | |
| 358 | case Intrinsic::ssub_with_overflow: |
| 359 | Operation = &ScalarEvolution::getMinusSCEV; |
| 360 | Extension = &ScalarEvolution::getSignExtendExpr; |
| 361 | RawOp = Instruction::Sub; |
| 362 | NoSignedOverflow = true; |
| 363 | break; |
| 364 | |
| 365 | case Intrinsic::usub_with_overflow: |
| 366 | Operation = &ScalarEvolution::getMinusSCEV; |
| 367 | Extension = &ScalarEvolution::getZeroExtendExpr; |
| 368 | RawOp = Instruction::Sub; |
| 369 | NoSignedOverflow = false; |
| 370 | break; |
| 371 | } |
| 372 | |
| 373 | const SCEV *LHS = SE->getSCEV(CI->getArgOperand(0)); |
| 374 | const SCEV *RHS = SE->getSCEV(CI->getArgOperand(1)); |
| 375 | |
| 376 | auto *NarrowTy = cast<IntegerType>(LHS->getType()); |
| 377 | auto *WideTy = |
| 378 | IntegerType::get(NarrowTy->getContext(), NarrowTy->getBitWidth() * 2); |
| 379 | |
| 380 | const SCEV *A = |
| 381 | (SE->*Extension)((SE->*Operation)(LHS, RHS, SCEV::FlagAnyWrap), WideTy); |
| 382 | const SCEV *B = |
| 383 | (SE->*Operation)((SE->*Extension)(LHS, WideTy), |
| 384 | (SE->*Extension)(RHS, WideTy), SCEV::FlagAnyWrap); |
| 385 | |
| 386 | if (A != B) |
| 387 | return false; |
| 388 | |
| 389 | // Proved no overflow, nuke the overflow check and, if possible, the overflow |
| 390 | // intrinsic as well. |
| 391 | |
| 392 | BinaryOperator *NewResult = BinaryOperator::Create( |
| 393 | RawOp, CI->getArgOperand(0), CI->getArgOperand(1), "", CI); |
| 394 | |
| 395 | if (NoSignedOverflow) |
| 396 | NewResult->setHasNoSignedWrap(true); |
| 397 | else |
| 398 | NewResult->setHasNoUnsignedWrap(true); |
| 399 | |
| 400 | SmallVector<ExtractValueInst *, 4> ToDelete; |
| 401 | |
| 402 | for (auto *U : CI->users()) { |
| 403 | if (auto *EVI = dyn_cast<ExtractValueInst>(U)) { |
| 404 | if (EVI->getIndices()[0] == 1) |
| 405 | EVI->replaceAllUsesWith(ConstantInt::getFalse(CI->getContext())); |
| 406 | else { |
| 407 | assert(EVI->getIndices()[0] == 0 && "Only two possibilities!"); |
| 408 | EVI->replaceAllUsesWith(NewResult); |
| 409 | } |
| 410 | ToDelete.push_back(EVI); |
| 411 | } |
| 412 | } |
| 413 | |
| 414 | for (auto *EVI : ToDelete) |
| 415 | EVI->eraseFromParent(); |
| 416 | |
| 417 | if (CI->use_empty()) |
| 418 | CI->eraseFromParent(); |
| 419 | |
| 420 | return true; |
| 421 | } |
| 422 | |
Sanjoy Das | 088bb0e | 2015-10-06 21:44:39 +0000 | [diff] [blame] | 423 | /// Eliminate an operation that consumes a simple IV and has no observable |
| 424 | /// side-effect given the range of IV values. IVOperand is guaranteed SCEVable, |
| 425 | /// but UseInst may not be. |
Andrew Trick | 3ec331e | 2011-08-10 03:46:27 +0000 | [diff] [blame] | 426 | bool SimplifyIndvar::eliminateIVUser(Instruction *UseInst, |
| 427 | Instruction *IVOperand) { |
| 428 | if (ICmpInst *ICmp = dyn_cast<ICmpInst>(UseInst)) { |
| 429 | eliminateIVComparison(ICmp, IVOperand); |
| 430 | return true; |
| 431 | } |
| 432 | if (BinaryOperator *Rem = dyn_cast<BinaryOperator>(UseInst)) { |
| 433 | bool IsSigned = Rem->getOpcode() == Instruction::SRem; |
| 434 | if (IsSigned || Rem->getOpcode() == Instruction::URem) { |
| 435 | eliminateIVRemainder(Rem, IVOperand, IsSigned); |
| 436 | return true; |
| 437 | } |
| 438 | } |
| 439 | |
Sanjoy Das | ae09b3c | 2016-05-29 00:36:25 +0000 | [diff] [blame^] | 440 | if (auto *CI = dyn_cast<CallInst>(UseInst)) |
| 441 | if (eliminateOverflowIntrinsic(CI)) |
| 442 | return true; |
| 443 | |
Sanjoy Das | 088bb0e | 2015-10-06 21:44:39 +0000 | [diff] [blame] | 444 | if (eliminateIdentitySCEV(UseInst, IVOperand)) |
| 445 | return true; |
| 446 | |
| 447 | return false; |
| 448 | } |
| 449 | |
| 450 | /// Eliminate any operation that SCEV can prove is an identity function. |
| 451 | bool SimplifyIndvar::eliminateIdentitySCEV(Instruction *UseInst, |
| 452 | Instruction *IVOperand) { |
Andrew Trick | 3ec331e | 2011-08-10 03:46:27 +0000 | [diff] [blame] | 453 | if (!SE->isSCEVable(UseInst->getType()) || |
| 454 | (UseInst->getType() != IVOperand->getType()) || |
| 455 | (SE->getSCEV(UseInst) != SE->getSCEV(IVOperand))) |
| 456 | return false; |
| 457 | |
Sanjoy Das | 5c8bead | 2015-10-06 21:44:49 +0000 | [diff] [blame] | 458 | // getSCEV(X) == getSCEV(Y) does not guarantee that X and Y are related in the |
| 459 | // dominator tree, even if X is an operand to Y. For instance, in |
| 460 | // |
| 461 | // %iv = phi i32 {0,+,1} |
| 462 | // br %cond, label %left, label %merge |
| 463 | // |
| 464 | // left: |
| 465 | // %X = add i32 %iv, 0 |
| 466 | // br label %merge |
| 467 | // |
| 468 | // merge: |
| 469 | // %M = phi (%X, %iv) |
| 470 | // |
| 471 | // getSCEV(%M) == getSCEV(%X) == {0,+,1}, but %X does not dominate %M, and |
| 472 | // %M.replaceAllUsesWith(%X) would be incorrect. |
| 473 | |
| 474 | if (isa<PHINode>(UseInst)) |
| 475 | // If UseInst is not a PHI node then we know that IVOperand dominates |
| 476 | // UseInst directly from the legality of SSA. |
| 477 | if (!DT || !DT->dominates(IVOperand, UseInst)) |
| 478 | return false; |
| 479 | |
Sanjoy Das | 0015e5a | 2015-10-07 17:38:31 +0000 | [diff] [blame] | 480 | if (!LI->replacementPreservesLCSSAForm(UseInst, IVOperand)) |
| 481 | return false; |
| 482 | |
Andrew Trick | 3ec331e | 2011-08-10 03:46:27 +0000 | [diff] [blame] | 483 | DEBUG(dbgs() << "INDVARS: Eliminated identity: " << *UseInst << '\n'); |
| 484 | |
| 485 | UseInst->replaceAllUsesWith(IVOperand); |
| 486 | ++NumElimIdentity; |
| 487 | Changed = true; |
Benjamin Kramer | f5e2fc4 | 2015-05-29 19:43:39 +0000 | [diff] [blame] | 488 | DeadInsts.emplace_back(UseInst); |
Andrew Trick | 3ec331e | 2011-08-10 03:46:27 +0000 | [diff] [blame] | 489 | return true; |
| 490 | } |
| 491 | |
Sanjoy Das | 7c0ce26 | 2015-01-06 19:02:56 +0000 | [diff] [blame] | 492 | /// Annotate BO with nsw / nuw if it provably does not signed-overflow / |
| 493 | /// unsigned-overflow. Returns true if anything changed, false otherwise. |
| 494 | bool SimplifyIndvar::strengthenOverflowingOperation(BinaryOperator *BO, |
| 495 | Value *IVOperand) { |
| 496 | |
Sanjoy Das | a5397c0 | 2015-03-04 22:24:23 +0000 | [diff] [blame] | 497 | // Fastpath: we don't have any work to do if `BO` is `nuw` and `nsw`. |
Sanjoy Das | 7c0ce26 | 2015-01-06 19:02:56 +0000 | [diff] [blame] | 498 | if (BO->hasNoUnsignedWrap() && BO->hasNoSignedWrap()) |
| 499 | return false; |
| 500 | |
Sanjoy Das | a5397c0 | 2015-03-04 22:24:23 +0000 | [diff] [blame] | 501 | const SCEV *(ScalarEvolution::*GetExprForBO)(const SCEV *, const SCEV *, |
| 502 | SCEV::NoWrapFlags); |
Sanjoy Das | 7c0ce26 | 2015-01-06 19:02:56 +0000 | [diff] [blame] | 503 | |
Sanjoy Das | a5397c0 | 2015-03-04 22:24:23 +0000 | [diff] [blame] | 504 | switch (BO->getOpcode()) { |
| 505 | default: |
Sanjoy Das | 7c0ce26 | 2015-01-06 19:02:56 +0000 | [diff] [blame] | 506 | return false; |
| 507 | |
Sanjoy Das | a5397c0 | 2015-03-04 22:24:23 +0000 | [diff] [blame] | 508 | case Instruction::Add: |
| 509 | GetExprForBO = &ScalarEvolution::getAddExpr; |
| 510 | break; |
Sanjoy Das | 7c0ce26 | 2015-01-06 19:02:56 +0000 | [diff] [blame] | 511 | |
Sanjoy Das | a5397c0 | 2015-03-04 22:24:23 +0000 | [diff] [blame] | 512 | case Instruction::Sub: |
| 513 | GetExprForBO = &ScalarEvolution::getMinusSCEV; |
| 514 | break; |
Sanjoy Das | 7c0ce26 | 2015-01-06 19:02:56 +0000 | [diff] [blame] | 515 | |
Sanjoy Das | a5397c0 | 2015-03-04 22:24:23 +0000 | [diff] [blame] | 516 | case Instruction::Mul: |
| 517 | GetExprForBO = &ScalarEvolution::getMulExpr; |
| 518 | break; |
| 519 | } |
Sanjoy Das | 7c0ce26 | 2015-01-06 19:02:56 +0000 | [diff] [blame] | 520 | |
Sanjoy Das | a5397c0 | 2015-03-04 22:24:23 +0000 | [diff] [blame] | 521 | unsigned BitWidth = cast<IntegerType>(BO->getType())->getBitWidth(); |
| 522 | Type *WideTy = IntegerType::get(BO->getContext(), BitWidth * 2); |
| 523 | const SCEV *LHS = SE->getSCEV(BO->getOperand(0)); |
| 524 | const SCEV *RHS = SE->getSCEV(BO->getOperand(1)); |
Sanjoy Das | 7c0ce26 | 2015-01-06 19:02:56 +0000 | [diff] [blame] | 525 | |
Sanjoy Das | a5397c0 | 2015-03-04 22:24:23 +0000 | [diff] [blame] | 526 | bool Changed = false; |
Sanjoy Das | 7c0ce26 | 2015-01-06 19:02:56 +0000 | [diff] [blame] | 527 | |
Sanjoy Das | a5397c0 | 2015-03-04 22:24:23 +0000 | [diff] [blame] | 528 | if (!BO->hasNoUnsignedWrap()) { |
| 529 | const SCEV *ExtendAfterOp = SE->getZeroExtendExpr(SE->getSCEV(BO), WideTy); |
| 530 | const SCEV *OpAfterExtend = (SE->*GetExprForBO)( |
| 531 | SE->getZeroExtendExpr(LHS, WideTy), SE->getZeroExtendExpr(RHS, WideTy), |
| 532 | SCEV::FlagAnyWrap); |
| 533 | if (ExtendAfterOp == OpAfterExtend) { |
| 534 | BO->setHasNoUnsignedWrap(); |
| 535 | SE->forgetValue(BO); |
| 536 | Changed = true; |
Sanjoy Das | 7c0ce26 | 2015-01-06 19:02:56 +0000 | [diff] [blame] | 537 | } |
| 538 | } |
| 539 | |
Sanjoy Das | a5397c0 | 2015-03-04 22:24:23 +0000 | [diff] [blame] | 540 | if (!BO->hasNoSignedWrap()) { |
| 541 | const SCEV *ExtendAfterOp = SE->getSignExtendExpr(SE->getSCEV(BO), WideTy); |
| 542 | const SCEV *OpAfterExtend = (SE->*GetExprForBO)( |
| 543 | SE->getSignExtendExpr(LHS, WideTy), SE->getSignExtendExpr(RHS, WideTy), |
| 544 | SCEV::FlagAnyWrap); |
| 545 | if (ExtendAfterOp == OpAfterExtend) { |
| 546 | BO->setHasNoSignedWrap(); |
| 547 | SE->forgetValue(BO); |
Sanjoy Das | 7c0ce26 | 2015-01-06 19:02:56 +0000 | [diff] [blame] | 548 | Changed = true; |
| 549 | } |
| 550 | } |
| 551 | |
| 552 | return Changed; |
| 553 | } |
| 554 | |
Andrew Trick | 0ba77a0 | 2013-12-23 23:31:49 +0000 | [diff] [blame] | 555 | /// \brief Split sadd.with.overflow into add + sadd.with.overflow to allow |
| 556 | /// analysis and optimization. |
| 557 | /// |
| 558 | /// \return A new value representing the non-overflowing add if possible, |
| 559 | /// otherwise return the original value. |
| 560 | Instruction *SimplifyIndvar::splitOverflowIntrinsic(Instruction *IVUser, |
| 561 | const DominatorTree *DT) { |
| 562 | IntrinsicInst *II = dyn_cast<IntrinsicInst>(IVUser); |
| 563 | if (!II || II->getIntrinsicID() != Intrinsic::sadd_with_overflow) |
| 564 | return IVUser; |
| 565 | |
| 566 | // Find a branch guarded by the overflow check. |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 567 | BranchInst *Branch = nullptr; |
| 568 | Instruction *AddVal = nullptr; |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 569 | for (User *U : II->users()) { |
| 570 | if (ExtractValueInst *ExtractInst = dyn_cast<ExtractValueInst>(U)) { |
Andrew Trick | 0ba77a0 | 2013-12-23 23:31:49 +0000 | [diff] [blame] | 571 | if (ExtractInst->getNumIndices() != 1) |
| 572 | continue; |
| 573 | if (ExtractInst->getIndices()[0] == 0) |
| 574 | AddVal = ExtractInst; |
| 575 | else if (ExtractInst->getIndices()[0] == 1 && ExtractInst->hasOneUse()) |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 576 | Branch = dyn_cast<BranchInst>(ExtractInst->user_back()); |
Andrew Trick | 0ba77a0 | 2013-12-23 23:31:49 +0000 | [diff] [blame] | 577 | } |
| 578 | } |
| 579 | if (!AddVal || !Branch) |
| 580 | return IVUser; |
| 581 | |
| 582 | BasicBlock *ContinueBB = Branch->getSuccessor(1); |
Benjamin Kramer | b6d0bd4 | 2014-03-02 12:27:27 +0000 | [diff] [blame] | 583 | if (std::next(pred_begin(ContinueBB)) != pred_end(ContinueBB)) |
Andrew Trick | 0ba77a0 | 2013-12-23 23:31:49 +0000 | [diff] [blame] | 584 | return IVUser; |
| 585 | |
| 586 | // Check if all users of the add are provably NSW. |
| 587 | bool AllNSW = true; |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 588 | for (Use &U : AddVal->uses()) { |
| 589 | if (Instruction *UseInst = dyn_cast<Instruction>(U.getUser())) { |
Andrew Trick | 0ba77a0 | 2013-12-23 23:31:49 +0000 | [diff] [blame] | 590 | BasicBlock *UseBB = UseInst->getParent(); |
| 591 | if (PHINode *PHI = dyn_cast<PHINode>(UseInst)) |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 592 | UseBB = PHI->getIncomingBlock(U); |
Andrew Trick | 0ba77a0 | 2013-12-23 23:31:49 +0000 | [diff] [blame] | 593 | if (!DT->dominates(ContinueBB, UseBB)) { |
| 594 | AllNSW = false; |
| 595 | break; |
| 596 | } |
| 597 | } |
| 598 | } |
| 599 | if (!AllNSW) |
| 600 | return IVUser; |
| 601 | |
| 602 | // Go for it... |
| 603 | IRBuilder<> Builder(IVUser); |
| 604 | Instruction *AddInst = dyn_cast<Instruction>( |
| 605 | Builder.CreateNSWAdd(II->getOperand(0), II->getOperand(1))); |
| 606 | |
| 607 | // The caller expects the new add to have the same form as the intrinsic. The |
| 608 | // IV operand position must be the same. |
| 609 | assert((AddInst->getOpcode() == Instruction::Add && |
| 610 | AddInst->getOperand(0) == II->getOperand(0)) && |
| 611 | "Bad add instruction created from overflow intrinsic."); |
| 612 | |
| 613 | AddVal->replaceAllUsesWith(AddInst); |
Benjamin Kramer | f5e2fc4 | 2015-05-29 19:43:39 +0000 | [diff] [blame] | 614 | DeadInsts.emplace_back(AddVal); |
Andrew Trick | 0ba77a0 | 2013-12-23 23:31:49 +0000 | [diff] [blame] | 615 | return AddInst; |
| 616 | } |
| 617 | |
Sanjay Patel | 7777b50 | 2014-11-12 18:07:42 +0000 | [diff] [blame] | 618 | /// Add all uses of Def to the current IV's worklist. |
Andrew Trick | 3ec331e | 2011-08-10 03:46:27 +0000 | [diff] [blame] | 619 | static void pushIVUsers( |
| 620 | Instruction *Def, |
| 621 | SmallPtrSet<Instruction*,16> &Simplified, |
| 622 | SmallVectorImpl< std::pair<Instruction*,Instruction*> > &SimpleIVUsers) { |
| 623 | |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 624 | for (User *U : Def->users()) { |
| 625 | Instruction *UI = cast<Instruction>(U); |
Andrew Trick | 3ec331e | 2011-08-10 03:46:27 +0000 | [diff] [blame] | 626 | |
| 627 | // Avoid infinite or exponential worklist processing. |
| 628 | // Also ensure unique worklist users. |
| 629 | // If Def is a LoopPhi, it may not be in the Simplified set, so check for |
| 630 | // self edges first. |
David Blaikie | 70573dc | 2014-11-19 07:49:26 +0000 | [diff] [blame] | 631 | if (UI != Def && Simplified.insert(UI).second) |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 632 | SimpleIVUsers.push_back(std::make_pair(UI, Def)); |
Andrew Trick | 3ec331e | 2011-08-10 03:46:27 +0000 | [diff] [blame] | 633 | } |
| 634 | } |
| 635 | |
Sanjay Patel | 7777b50 | 2014-11-12 18:07:42 +0000 | [diff] [blame] | 636 | /// Return true if this instruction generates a simple SCEV |
Andrew Trick | 3ec331e | 2011-08-10 03:46:27 +0000 | [diff] [blame] | 637 | /// expression in terms of that IV. |
| 638 | /// |
Andrew Trick | 6dbb060 | 2011-08-10 18:07:05 +0000 | [diff] [blame] | 639 | /// This is similar to IVUsers' isInteresting() but processes each instruction |
Andrew Trick | 3ec331e | 2011-08-10 03:46:27 +0000 | [diff] [blame] | 640 | /// non-recursively when the operand is already known to be a simpleIVUser. |
| 641 | /// |
| 642 | static bool isSimpleIVUser(Instruction *I, const Loop *L, ScalarEvolution *SE) { |
| 643 | if (!SE->isSCEVable(I->getType())) |
| 644 | return false; |
| 645 | |
| 646 | // Get the symbolic expression for this instruction. |
| 647 | const SCEV *S = SE->getSCEV(I); |
| 648 | |
| 649 | // Only consider affine recurrences. |
| 650 | const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(S); |
| 651 | if (AR && AR->getLoop() == L) |
| 652 | return true; |
| 653 | |
| 654 | return false; |
| 655 | } |
| 656 | |
Sanjay Patel | 7777b50 | 2014-11-12 18:07:42 +0000 | [diff] [blame] | 657 | /// Iteratively perform simplification on a worklist of users |
Andrew Trick | 3ec331e | 2011-08-10 03:46:27 +0000 | [diff] [blame] | 658 | /// of the specified induction variable. Each successive simplification may push |
| 659 | /// more users which may themselves be candidates for simplification. |
| 660 | /// |
| 661 | /// This algorithm does not require IVUsers analysis. Instead, it simplifies |
| 662 | /// instructions in-place during analysis. Rather than rewriting induction |
| 663 | /// variables bottom-up from their users, it transforms a chain of IVUsers |
Benjamin Kramer | df005cb | 2015-08-08 18:27:36 +0000 | [diff] [blame] | 664 | /// top-down, updating the IR only when it encounters a clear optimization |
| 665 | /// opportunity. |
Andrew Trick | 3ec331e | 2011-08-10 03:46:27 +0000 | [diff] [blame] | 666 | /// |
| 667 | /// Once DisableIVRewrite is default, LSR will be the only client of IVUsers. |
| 668 | /// |
| 669 | void SimplifyIndvar::simplifyUsers(PHINode *CurrIV, IVVisitor *V) { |
Andrew Trick | 7251e41 | 2011-09-19 17:54:39 +0000 | [diff] [blame] | 670 | if (!SE->isSCEVable(CurrIV->getType())) |
| 671 | return; |
| 672 | |
Andrew Trick | 3ec331e | 2011-08-10 03:46:27 +0000 | [diff] [blame] | 673 | // Instructions processed by SimplifyIndvar for CurrIV. |
| 674 | SmallPtrSet<Instruction*,16> Simplified; |
| 675 | |
| 676 | // Use-def pairs if IV users waiting to be processed for CurrIV. |
| 677 | SmallVector<std::pair<Instruction*, Instruction*>, 8> SimpleIVUsers; |
| 678 | |
| 679 | // Push users of the current LoopPhi. In rare cases, pushIVUsers may be |
| 680 | // called multiple times for the same LoopPhi. This is the proper thing to |
| 681 | // do for loop header phis that use each other. |
| 682 | pushIVUsers(CurrIV, Simplified, SimpleIVUsers); |
| 683 | |
| 684 | while (!SimpleIVUsers.empty()) { |
| 685 | std::pair<Instruction*, Instruction*> UseOper = |
| 686 | SimpleIVUsers.pop_back_val(); |
Andrew Trick | 0ba77a0 | 2013-12-23 23:31:49 +0000 | [diff] [blame] | 687 | Instruction *UseInst = UseOper.first; |
| 688 | |
Andrew Trick | 3ec331e | 2011-08-10 03:46:27 +0000 | [diff] [blame] | 689 | // Bypass back edges to avoid extra work. |
Andrew Trick | 0ba77a0 | 2013-12-23 23:31:49 +0000 | [diff] [blame] | 690 | if (UseInst == CurrIV) continue; |
| 691 | |
| 692 | if (V && V->shouldSplitOverflowInstrinsics()) { |
| 693 | UseInst = splitOverflowIntrinsic(UseInst, V->getDomTree()); |
| 694 | if (!UseInst) |
| 695 | continue; |
| 696 | } |
Andrew Trick | 3ec331e | 2011-08-10 03:46:27 +0000 | [diff] [blame] | 697 | |
Andrew Trick | 74664d5 | 2011-08-10 04:01:31 +0000 | [diff] [blame] | 698 | Instruction *IVOperand = UseOper.second; |
| 699 | for (unsigned N = 0; IVOperand; ++N) { |
| 700 | assert(N <= Simplified.size() && "runaway iteration"); |
Andrew Trick | 3ec331e | 2011-08-10 03:46:27 +0000 | [diff] [blame] | 701 | |
Andrew Trick | 74664d5 | 2011-08-10 04:01:31 +0000 | [diff] [blame] | 702 | Value *NewOper = foldIVUser(UseOper.first, IVOperand); |
| 703 | if (!NewOper) |
| 704 | break; // done folding |
| 705 | IVOperand = dyn_cast<Instruction>(NewOper); |
| 706 | } |
| 707 | if (!IVOperand) |
| 708 | continue; |
| 709 | |
| 710 | if (eliminateIVUser(UseOper.first, IVOperand)) { |
| 711 | pushIVUsers(IVOperand, Simplified, SimpleIVUsers); |
Andrew Trick | 3ec331e | 2011-08-10 03:46:27 +0000 | [diff] [blame] | 712 | continue; |
| 713 | } |
Sanjoy Das | 7c0ce26 | 2015-01-06 19:02:56 +0000 | [diff] [blame] | 714 | |
| 715 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(UseOper.first)) { |
| 716 | if (isa<OverflowingBinaryOperator>(BO) && |
| 717 | strengthenOverflowingOperation(BO, IVOperand)) { |
| 718 | // re-queue uses of the now modified binary operator and fall |
| 719 | // through to the checks that remain. |
| 720 | pushIVUsers(IVOperand, Simplified, SimpleIVUsers); |
| 721 | } |
| 722 | } |
| 723 | |
Andrew Trick | 3ec331e | 2011-08-10 03:46:27 +0000 | [diff] [blame] | 724 | CastInst *Cast = dyn_cast<CastInst>(UseOper.first); |
| 725 | if (V && Cast) { |
| 726 | V->visitCast(Cast); |
| 727 | continue; |
| 728 | } |
| 729 | if (isSimpleIVUser(UseOper.first, L, SE)) { |
| 730 | pushIVUsers(UseOper.first, Simplified, SimpleIVUsers); |
| 731 | } |
| 732 | } |
| 733 | } |
| 734 | |
| 735 | namespace llvm { |
| 736 | |
David Blaikie | a379b181 | 2011-12-20 02:50:00 +0000 | [diff] [blame] | 737 | void IVVisitor::anchor() { } |
| 738 | |
Sanjay Patel | 7777b50 | 2014-11-12 18:07:42 +0000 | [diff] [blame] | 739 | /// Simplify instructions that use this induction variable |
Andrew Trick | 3ec331e | 2011-08-10 03:46:27 +0000 | [diff] [blame] | 740 | /// by using ScalarEvolution to analyze the IV's recurrence. |
Sanjoy Das | 5c8bead | 2015-10-06 21:44:49 +0000 | [diff] [blame] | 741 | bool simplifyUsersOfIV(PHINode *CurrIV, ScalarEvolution *SE, DominatorTree *DT, |
Justin Bogner | 843fb20 | 2015-12-15 19:40:57 +0000 | [diff] [blame] | 742 | LoopInfo *LI, SmallVectorImpl<WeakVH> &Dead, |
| 743 | IVVisitor *V) { |
Sanjoy Das | 5c8bead | 2015-10-06 21:44:49 +0000 | [diff] [blame] | 744 | SimplifyIndvar SIV(LI->getLoopFor(CurrIV->getParent()), SE, DT, LI, Dead); |
Andrew Trick | 3ec331e | 2011-08-10 03:46:27 +0000 | [diff] [blame] | 745 | SIV.simplifyUsers(CurrIV, V); |
| 746 | return SIV.hasChanged(); |
| 747 | } |
| 748 | |
Sanjay Patel | 7777b50 | 2014-11-12 18:07:42 +0000 | [diff] [blame] | 749 | /// Simplify users of induction variables within this |
Andrew Trick | 3ec331e | 2011-08-10 03:46:27 +0000 | [diff] [blame] | 750 | /// loop. This does not actually change or add IVs. |
Sanjoy Das | 5c8bead | 2015-10-06 21:44:49 +0000 | [diff] [blame] | 751 | bool simplifyLoopIVs(Loop *L, ScalarEvolution *SE, DominatorTree *DT, |
Justin Bogner | 843fb20 | 2015-12-15 19:40:57 +0000 | [diff] [blame] | 752 | LoopInfo *LI, SmallVectorImpl<WeakVH> &Dead) { |
Andrew Trick | 3ec331e | 2011-08-10 03:46:27 +0000 | [diff] [blame] | 753 | bool Changed = false; |
| 754 | for (BasicBlock::iterator I = L->getHeader()->begin(); isa<PHINode>(I); ++I) { |
Justin Bogner | 843fb20 | 2015-12-15 19:40:57 +0000 | [diff] [blame] | 755 | Changed |= simplifyUsersOfIV(cast<PHINode>(I), SE, DT, LI, Dead); |
Andrew Trick | 3ec331e | 2011-08-10 03:46:27 +0000 | [diff] [blame] | 756 | } |
| 757 | return Changed; |
| 758 | } |
| 759 | |
Andrew Trick | 3ec331e | 2011-08-10 03:46:27 +0000 | [diff] [blame] | 760 | } // namespace llvm |