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