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/CommandLine.h" |
| 29 | #include "llvm/Support/Debug.h" |
| 30 | #include "llvm/Support/raw_ostream.h" |
Andrew Trick | 3ec331e | 2011-08-10 03:46:27 +0000 | [diff] [blame] | 31 | |
| 32 | using namespace llvm; |
| 33 | |
Chandler Carruth | 964daaa | 2014-04-22 02:55:47 +0000 | [diff] [blame] | 34 | #define DEBUG_TYPE "indvars" |
| 35 | |
Andrew Trick | 3ec331e | 2011-08-10 03:46:27 +0000 | [diff] [blame] | 36 | STATISTIC(NumElimIdentity, "Number of IV identities eliminated"); |
| 37 | STATISTIC(NumElimOperand, "Number of IV operands folded into a use"); |
| 38 | STATISTIC(NumElimRem , "Number of IV remainder operations eliminated"); |
| 39 | STATISTIC(NumElimCmp , "Number of IV comparisons eliminated"); |
| 40 | |
| 41 | namespace { |
Sanjay Patel | 7777b50 | 2014-11-12 18:07:42 +0000 | [diff] [blame] | 42 | /// This is a utility for simplifying induction variables |
Andrew Trick | 3ec331e | 2011-08-10 03:46:27 +0000 | [diff] [blame] | 43 | /// based on ScalarEvolution. It is the primary instrument of the |
| 44 | /// IndvarSimplify pass, but it may also be directly invoked to cleanup after |
| 45 | /// other loop passes that preserve SCEV. |
| 46 | class SimplifyIndvar { |
| 47 | Loop *L; |
| 48 | LoopInfo *LI; |
Andrew Trick | 3ec331e | 2011-08-10 03:46:27 +0000 | [diff] [blame] | 49 | ScalarEvolution *SE; |
Sanjoy Das | 5c8bead | 2015-10-06 21:44:49 +0000 | [diff] [blame] | 50 | DominatorTree *DT; |
Andrew Trick | 3ec331e | 2011-08-10 03:46:27 +0000 | [diff] [blame] | 51 | |
| 52 | SmallVectorImpl<WeakVH> &DeadInsts; |
| 53 | |
| 54 | bool Changed; |
| 55 | |
| 56 | public: |
Sanjoy Das | 5c8bead | 2015-10-06 21:44:49 +0000 | [diff] [blame] | 57 | SimplifyIndvar(Loop *Loop, ScalarEvolution *SE, DominatorTree *DT, |
| 58 | LoopInfo *LI,SmallVectorImpl<WeakVH> &Dead) |
| 59 | : L(Loop), LI(LI), SE(SE), DT(DT), DeadInsts(Dead), Changed(false) { |
Andrew Trick | e629d00 | 2011-08-10 04:22:26 +0000 | [diff] [blame] | 60 | assert(LI && "IV simplification requires LoopInfo"); |
Andrew Trick | 3ec331e | 2011-08-10 03:46:27 +0000 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | bool hasChanged() const { return Changed; } |
| 64 | |
| 65 | /// Iteratively perform simplification on a worklist of users of the |
| 66 | /// specified induction variable. This is the top-level driver that applies |
Benjamin Kramer | df005cb | 2015-08-08 18:27:36 +0000 | [diff] [blame] | 67 | /// all simplifications to users of an IV. |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 68 | void simplifyUsers(PHINode *CurrIV, IVVisitor *V = nullptr); |
Andrew Trick | 3ec331e | 2011-08-10 03:46:27 +0000 | [diff] [blame] | 69 | |
Andrew Trick | 74664d5 | 2011-08-10 04:01:31 +0000 | [diff] [blame] | 70 | Value *foldIVUser(Instruction *UseInst, Instruction *IVOperand); |
Andrew Trick | 3ec331e | 2011-08-10 03:46:27 +0000 | [diff] [blame] | 71 | |
Sanjoy Das | 088bb0e | 2015-10-06 21:44:39 +0000 | [diff] [blame] | 72 | bool eliminateIdentitySCEV(Instruction *UseInst, Instruction *IVOperand); |
| 73 | |
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) && |
| 186 | SE->isLoopInvariantPredicate(Pred, S, X, ICmpLoop, |
| 187 | InvariantPredicate, InvariantLHS, |
| 188 | InvariantRHS)) { |
| 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 | |
| 204 | for (Value *Incoming : cast<PHINode>(IVOperand)->incoming_values()) { |
| 205 | if (NewLHS && NewRHS) |
| 206 | break; |
| 207 | |
| 208 | const SCEV *IncomingS = SE->getSCEV(Incoming); |
| 209 | |
| 210 | if (!NewLHS && IncomingS == InvariantLHS) |
| 211 | NewLHS = Incoming; |
| 212 | if (!NewRHS && IncomingS == InvariantRHS) |
| 213 | NewRHS = Incoming; |
| 214 | } |
| 215 | |
| 216 | if (!NewLHS || !NewRHS) |
| 217 | // We could not find an existing value to replace either LHS or RHS. |
| 218 | // Generating new instructions has subtler tradeoffs, so avoid doing that |
| 219 | // for now. |
| 220 | return; |
| 221 | |
Sanjoy Das | c18115d | 2015-08-06 20:43:28 +0000 | [diff] [blame] | 222 | DEBUG(dbgs() << "INDVARS: Simplified comparison: " << *ICmp << '\n'); |
Sanjoy Das | 5dab205 | 2015-07-27 21:42:49 +0000 | [diff] [blame] | 223 | ICmp->setPredicate(InvariantPredicate); |
| 224 | ICmp->setOperand(0, NewLHS); |
| 225 | ICmp->setOperand(1, NewRHS); |
| 226 | } else |
Andrew Trick | 3ec331e | 2011-08-10 03:46:27 +0000 | [diff] [blame] | 227 | return; |
| 228 | |
Andrew Trick | 3ec331e | 2011-08-10 03:46:27 +0000 | [diff] [blame] | 229 | ++NumElimCmp; |
| 230 | Changed = true; |
Andrew Trick | 3ec331e | 2011-08-10 03:46:27 +0000 | [diff] [blame] | 231 | } |
| 232 | |
Sanjay Patel | 7777b50 | 2014-11-12 18:07:42 +0000 | [diff] [blame] | 233 | /// SimplifyIVUsers helper for eliminating useless |
Andrew Trick | 3ec331e | 2011-08-10 03:46:27 +0000 | [diff] [blame] | 234 | /// remainder operations operating on an induction variable. |
| 235 | void SimplifyIndvar::eliminateIVRemainder(BinaryOperator *Rem, |
| 236 | Value *IVOperand, |
| 237 | bool IsSigned) { |
| 238 | // We're only interested in the case where we know something about |
| 239 | // the numerator. |
| 240 | if (IVOperand != Rem->getOperand(0)) |
| 241 | return; |
| 242 | |
| 243 | // Get the SCEVs for the ICmp operands. |
| 244 | const SCEV *S = SE->getSCEV(Rem->getOperand(0)); |
| 245 | const SCEV *X = SE->getSCEV(Rem->getOperand(1)); |
| 246 | |
| 247 | // Simplify unnecessary loops away. |
| 248 | const Loop *ICmpLoop = LI->getLoopFor(Rem->getParent()); |
| 249 | S = SE->getSCEVAtScope(S, ICmpLoop); |
| 250 | X = SE->getSCEVAtScope(X, ICmpLoop); |
| 251 | |
| 252 | // i % n --> i if i is in [0,n). |
| 253 | if ((!IsSigned || SE->isKnownNonNegative(S)) && |
| 254 | SE->isKnownPredicate(IsSigned ? ICmpInst::ICMP_SLT : ICmpInst::ICMP_ULT, |
| 255 | S, X)) |
| 256 | Rem->replaceAllUsesWith(Rem->getOperand(0)); |
| 257 | else { |
| 258 | // (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] | 259 | const SCEV *LessOne = SE->getMinusSCEV(S, SE->getOne(S->getType())); |
Andrew Trick | 3ec331e | 2011-08-10 03:46:27 +0000 | [diff] [blame] | 260 | if (IsSigned && !SE->isKnownNonNegative(LessOne)) |
| 261 | return; |
| 262 | |
| 263 | if (!SE->isKnownPredicate(IsSigned ? |
| 264 | ICmpInst::ICMP_SLT : ICmpInst::ICMP_ULT, |
| 265 | LessOne, X)) |
| 266 | return; |
| 267 | |
| 268 | ICmpInst *ICmp = new ICmpInst(Rem, ICmpInst::ICMP_EQ, |
Benjamin Kramer | 547b6c5 | 2011-09-27 20:39:19 +0000 | [diff] [blame] | 269 | Rem->getOperand(0), Rem->getOperand(1)); |
Andrew Trick | 3ec331e | 2011-08-10 03:46:27 +0000 | [diff] [blame] | 270 | SelectInst *Sel = |
| 271 | SelectInst::Create(ICmp, |
| 272 | ConstantInt::get(Rem->getType(), 0), |
| 273 | Rem->getOperand(0), "tmp", Rem); |
| 274 | Rem->replaceAllUsesWith(Sel); |
| 275 | } |
| 276 | |
Andrew Trick | 3ec331e | 2011-08-10 03:46:27 +0000 | [diff] [blame] | 277 | DEBUG(dbgs() << "INDVARS: Simplified rem: " << *Rem << '\n'); |
| 278 | ++NumElimRem; |
| 279 | Changed = true; |
Benjamin Kramer | f5e2fc4 | 2015-05-29 19:43:39 +0000 | [diff] [blame] | 280 | DeadInsts.emplace_back(Rem); |
Andrew Trick | 3ec331e | 2011-08-10 03:46:27 +0000 | [diff] [blame] | 281 | } |
| 282 | |
Sanjoy Das | 088bb0e | 2015-10-06 21:44:39 +0000 | [diff] [blame] | 283 | /// Eliminate an operation that consumes a simple IV and has no observable |
| 284 | /// side-effect given the range of IV values. IVOperand is guaranteed SCEVable, |
| 285 | /// but UseInst may not be. |
Andrew Trick | 3ec331e | 2011-08-10 03:46:27 +0000 | [diff] [blame] | 286 | bool SimplifyIndvar::eliminateIVUser(Instruction *UseInst, |
| 287 | Instruction *IVOperand) { |
| 288 | if (ICmpInst *ICmp = dyn_cast<ICmpInst>(UseInst)) { |
| 289 | eliminateIVComparison(ICmp, IVOperand); |
| 290 | return true; |
| 291 | } |
| 292 | if (BinaryOperator *Rem = dyn_cast<BinaryOperator>(UseInst)) { |
| 293 | bool IsSigned = Rem->getOpcode() == Instruction::SRem; |
| 294 | if (IsSigned || Rem->getOpcode() == Instruction::URem) { |
| 295 | eliminateIVRemainder(Rem, IVOperand, IsSigned); |
| 296 | return true; |
| 297 | } |
| 298 | } |
| 299 | |
Sanjoy Das | 088bb0e | 2015-10-06 21:44:39 +0000 | [diff] [blame] | 300 | if (eliminateIdentitySCEV(UseInst, IVOperand)) |
| 301 | return true; |
| 302 | |
| 303 | return false; |
| 304 | } |
| 305 | |
| 306 | /// Eliminate any operation that SCEV can prove is an identity function. |
| 307 | bool SimplifyIndvar::eliminateIdentitySCEV(Instruction *UseInst, |
| 308 | Instruction *IVOperand) { |
Andrew Trick | 3ec331e | 2011-08-10 03:46:27 +0000 | [diff] [blame] | 309 | if (!SE->isSCEVable(UseInst->getType()) || |
| 310 | (UseInst->getType() != IVOperand->getType()) || |
| 311 | (SE->getSCEV(UseInst) != SE->getSCEV(IVOperand))) |
| 312 | return false; |
| 313 | |
Sanjoy Das | 5c8bead | 2015-10-06 21:44:49 +0000 | [diff] [blame] | 314 | // getSCEV(X) == getSCEV(Y) does not guarantee that X and Y are related in the |
| 315 | // dominator tree, even if X is an operand to Y. For instance, in |
| 316 | // |
| 317 | // %iv = phi i32 {0,+,1} |
| 318 | // br %cond, label %left, label %merge |
| 319 | // |
| 320 | // left: |
| 321 | // %X = add i32 %iv, 0 |
| 322 | // br label %merge |
| 323 | // |
| 324 | // merge: |
| 325 | // %M = phi (%X, %iv) |
| 326 | // |
| 327 | // getSCEV(%M) == getSCEV(%X) == {0,+,1}, but %X does not dominate %M, and |
| 328 | // %M.replaceAllUsesWith(%X) would be incorrect. |
| 329 | |
| 330 | if (isa<PHINode>(UseInst)) |
| 331 | // If UseInst is not a PHI node then we know that IVOperand dominates |
| 332 | // UseInst directly from the legality of SSA. |
| 333 | if (!DT || !DT->dominates(IVOperand, UseInst)) |
| 334 | return false; |
| 335 | |
Sanjoy Das | 0015e5a | 2015-10-07 17:38:31 +0000 | [diff] [blame] | 336 | if (!LI->replacementPreservesLCSSAForm(UseInst, IVOperand)) |
| 337 | return false; |
| 338 | |
Andrew Trick | 3ec331e | 2011-08-10 03:46:27 +0000 | [diff] [blame] | 339 | DEBUG(dbgs() << "INDVARS: Eliminated identity: " << *UseInst << '\n'); |
| 340 | |
| 341 | UseInst->replaceAllUsesWith(IVOperand); |
| 342 | ++NumElimIdentity; |
| 343 | Changed = true; |
Benjamin Kramer | f5e2fc4 | 2015-05-29 19:43:39 +0000 | [diff] [blame] | 344 | DeadInsts.emplace_back(UseInst); |
Andrew Trick | 3ec331e | 2011-08-10 03:46:27 +0000 | [diff] [blame] | 345 | return true; |
| 346 | } |
| 347 | |
Sanjoy Das | 7c0ce26 | 2015-01-06 19:02:56 +0000 | [diff] [blame] | 348 | /// Annotate BO with nsw / nuw if it provably does not signed-overflow / |
| 349 | /// unsigned-overflow. Returns true if anything changed, false otherwise. |
| 350 | bool SimplifyIndvar::strengthenOverflowingOperation(BinaryOperator *BO, |
| 351 | Value *IVOperand) { |
| 352 | |
Sanjoy Das | a5397c0 | 2015-03-04 22:24:23 +0000 | [diff] [blame] | 353 | // 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] | 354 | if (BO->hasNoUnsignedWrap() && BO->hasNoSignedWrap()) |
| 355 | return false; |
| 356 | |
Sanjoy Das | a5397c0 | 2015-03-04 22:24:23 +0000 | [diff] [blame] | 357 | const SCEV *(ScalarEvolution::*GetExprForBO)(const SCEV *, const SCEV *, |
| 358 | SCEV::NoWrapFlags); |
Sanjoy Das | 7c0ce26 | 2015-01-06 19:02:56 +0000 | [diff] [blame] | 359 | |
Sanjoy Das | a5397c0 | 2015-03-04 22:24:23 +0000 | [diff] [blame] | 360 | switch (BO->getOpcode()) { |
| 361 | default: |
Sanjoy Das | 7c0ce26 | 2015-01-06 19:02:56 +0000 | [diff] [blame] | 362 | return false; |
| 363 | |
Sanjoy Das | a5397c0 | 2015-03-04 22:24:23 +0000 | [diff] [blame] | 364 | case Instruction::Add: |
| 365 | GetExprForBO = &ScalarEvolution::getAddExpr; |
| 366 | break; |
Sanjoy Das | 7c0ce26 | 2015-01-06 19:02:56 +0000 | [diff] [blame] | 367 | |
Sanjoy Das | a5397c0 | 2015-03-04 22:24:23 +0000 | [diff] [blame] | 368 | case Instruction::Sub: |
| 369 | GetExprForBO = &ScalarEvolution::getMinusSCEV; |
| 370 | break; |
Sanjoy Das | 7c0ce26 | 2015-01-06 19:02:56 +0000 | [diff] [blame] | 371 | |
Sanjoy Das | a5397c0 | 2015-03-04 22:24:23 +0000 | [diff] [blame] | 372 | case Instruction::Mul: |
| 373 | GetExprForBO = &ScalarEvolution::getMulExpr; |
| 374 | break; |
| 375 | } |
Sanjoy Das | 7c0ce26 | 2015-01-06 19:02:56 +0000 | [diff] [blame] | 376 | |
Sanjoy Das | a5397c0 | 2015-03-04 22:24:23 +0000 | [diff] [blame] | 377 | unsigned BitWidth = cast<IntegerType>(BO->getType())->getBitWidth(); |
| 378 | Type *WideTy = IntegerType::get(BO->getContext(), BitWidth * 2); |
| 379 | const SCEV *LHS = SE->getSCEV(BO->getOperand(0)); |
| 380 | const SCEV *RHS = SE->getSCEV(BO->getOperand(1)); |
Sanjoy Das | 7c0ce26 | 2015-01-06 19:02:56 +0000 | [diff] [blame] | 381 | |
Sanjoy Das | a5397c0 | 2015-03-04 22:24:23 +0000 | [diff] [blame] | 382 | bool Changed = false; |
Sanjoy Das | 7c0ce26 | 2015-01-06 19:02:56 +0000 | [diff] [blame] | 383 | |
Sanjoy Das | a5397c0 | 2015-03-04 22:24:23 +0000 | [diff] [blame] | 384 | if (!BO->hasNoUnsignedWrap()) { |
| 385 | const SCEV *ExtendAfterOp = SE->getZeroExtendExpr(SE->getSCEV(BO), WideTy); |
| 386 | const SCEV *OpAfterExtend = (SE->*GetExprForBO)( |
| 387 | SE->getZeroExtendExpr(LHS, WideTy), SE->getZeroExtendExpr(RHS, WideTy), |
| 388 | SCEV::FlagAnyWrap); |
| 389 | if (ExtendAfterOp == OpAfterExtend) { |
| 390 | BO->setHasNoUnsignedWrap(); |
| 391 | SE->forgetValue(BO); |
| 392 | Changed = true; |
Sanjoy Das | 7c0ce26 | 2015-01-06 19:02:56 +0000 | [diff] [blame] | 393 | } |
| 394 | } |
| 395 | |
Sanjoy Das | a5397c0 | 2015-03-04 22:24:23 +0000 | [diff] [blame] | 396 | if (!BO->hasNoSignedWrap()) { |
| 397 | const SCEV *ExtendAfterOp = SE->getSignExtendExpr(SE->getSCEV(BO), WideTy); |
| 398 | const SCEV *OpAfterExtend = (SE->*GetExprForBO)( |
| 399 | SE->getSignExtendExpr(LHS, WideTy), SE->getSignExtendExpr(RHS, WideTy), |
| 400 | SCEV::FlagAnyWrap); |
| 401 | if (ExtendAfterOp == OpAfterExtend) { |
| 402 | BO->setHasNoSignedWrap(); |
| 403 | SE->forgetValue(BO); |
Sanjoy Das | 7c0ce26 | 2015-01-06 19:02:56 +0000 | [diff] [blame] | 404 | Changed = true; |
| 405 | } |
| 406 | } |
| 407 | |
| 408 | return Changed; |
| 409 | } |
| 410 | |
Andrew Trick | 0ba77a0 | 2013-12-23 23:31:49 +0000 | [diff] [blame] | 411 | /// \brief Split sadd.with.overflow into add + sadd.with.overflow to allow |
| 412 | /// analysis and optimization. |
| 413 | /// |
| 414 | /// \return A new value representing the non-overflowing add if possible, |
| 415 | /// otherwise return the original value. |
| 416 | Instruction *SimplifyIndvar::splitOverflowIntrinsic(Instruction *IVUser, |
| 417 | const DominatorTree *DT) { |
| 418 | IntrinsicInst *II = dyn_cast<IntrinsicInst>(IVUser); |
| 419 | if (!II || II->getIntrinsicID() != Intrinsic::sadd_with_overflow) |
| 420 | return IVUser; |
| 421 | |
| 422 | // Find a branch guarded by the overflow check. |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 423 | BranchInst *Branch = nullptr; |
| 424 | Instruction *AddVal = nullptr; |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 425 | for (User *U : II->users()) { |
| 426 | if (ExtractValueInst *ExtractInst = dyn_cast<ExtractValueInst>(U)) { |
Andrew Trick | 0ba77a0 | 2013-12-23 23:31:49 +0000 | [diff] [blame] | 427 | if (ExtractInst->getNumIndices() != 1) |
| 428 | continue; |
| 429 | if (ExtractInst->getIndices()[0] == 0) |
| 430 | AddVal = ExtractInst; |
| 431 | else if (ExtractInst->getIndices()[0] == 1 && ExtractInst->hasOneUse()) |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 432 | Branch = dyn_cast<BranchInst>(ExtractInst->user_back()); |
Andrew Trick | 0ba77a0 | 2013-12-23 23:31:49 +0000 | [diff] [blame] | 433 | } |
| 434 | } |
| 435 | if (!AddVal || !Branch) |
| 436 | return IVUser; |
| 437 | |
| 438 | BasicBlock *ContinueBB = Branch->getSuccessor(1); |
Benjamin Kramer | b6d0bd4 | 2014-03-02 12:27:27 +0000 | [diff] [blame] | 439 | if (std::next(pred_begin(ContinueBB)) != pred_end(ContinueBB)) |
Andrew Trick | 0ba77a0 | 2013-12-23 23:31:49 +0000 | [diff] [blame] | 440 | return IVUser; |
| 441 | |
| 442 | // Check if all users of the add are provably NSW. |
| 443 | bool AllNSW = true; |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 444 | for (Use &U : AddVal->uses()) { |
| 445 | if (Instruction *UseInst = dyn_cast<Instruction>(U.getUser())) { |
Andrew Trick | 0ba77a0 | 2013-12-23 23:31:49 +0000 | [diff] [blame] | 446 | BasicBlock *UseBB = UseInst->getParent(); |
| 447 | if (PHINode *PHI = dyn_cast<PHINode>(UseInst)) |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 448 | UseBB = PHI->getIncomingBlock(U); |
Andrew Trick | 0ba77a0 | 2013-12-23 23:31:49 +0000 | [diff] [blame] | 449 | if (!DT->dominates(ContinueBB, UseBB)) { |
| 450 | AllNSW = false; |
| 451 | break; |
| 452 | } |
| 453 | } |
| 454 | } |
| 455 | if (!AllNSW) |
| 456 | return IVUser; |
| 457 | |
| 458 | // Go for it... |
| 459 | IRBuilder<> Builder(IVUser); |
| 460 | Instruction *AddInst = dyn_cast<Instruction>( |
| 461 | Builder.CreateNSWAdd(II->getOperand(0), II->getOperand(1))); |
| 462 | |
| 463 | // The caller expects the new add to have the same form as the intrinsic. The |
| 464 | // IV operand position must be the same. |
| 465 | assert((AddInst->getOpcode() == Instruction::Add && |
| 466 | AddInst->getOperand(0) == II->getOperand(0)) && |
| 467 | "Bad add instruction created from overflow intrinsic."); |
| 468 | |
| 469 | AddVal->replaceAllUsesWith(AddInst); |
Benjamin Kramer | f5e2fc4 | 2015-05-29 19:43:39 +0000 | [diff] [blame] | 470 | DeadInsts.emplace_back(AddVal); |
Andrew Trick | 0ba77a0 | 2013-12-23 23:31:49 +0000 | [diff] [blame] | 471 | return AddInst; |
| 472 | } |
| 473 | |
Sanjay Patel | 7777b50 | 2014-11-12 18:07:42 +0000 | [diff] [blame] | 474 | /// Add all uses of Def to the current IV's worklist. |
Andrew Trick | 3ec331e | 2011-08-10 03:46:27 +0000 | [diff] [blame] | 475 | static void pushIVUsers( |
| 476 | Instruction *Def, |
| 477 | SmallPtrSet<Instruction*,16> &Simplified, |
| 478 | SmallVectorImpl< std::pair<Instruction*,Instruction*> > &SimpleIVUsers) { |
| 479 | |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 480 | for (User *U : Def->users()) { |
| 481 | Instruction *UI = cast<Instruction>(U); |
Andrew Trick | 3ec331e | 2011-08-10 03:46:27 +0000 | [diff] [blame] | 482 | |
| 483 | // Avoid infinite or exponential worklist processing. |
| 484 | // Also ensure unique worklist users. |
| 485 | // If Def is a LoopPhi, it may not be in the Simplified set, so check for |
| 486 | // self edges first. |
David Blaikie | 70573dc | 2014-11-19 07:49:26 +0000 | [diff] [blame] | 487 | if (UI != Def && Simplified.insert(UI).second) |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 488 | SimpleIVUsers.push_back(std::make_pair(UI, Def)); |
Andrew Trick | 3ec331e | 2011-08-10 03:46:27 +0000 | [diff] [blame] | 489 | } |
| 490 | } |
| 491 | |
Sanjay Patel | 7777b50 | 2014-11-12 18:07:42 +0000 | [diff] [blame] | 492 | /// Return true if this instruction generates a simple SCEV |
Andrew Trick | 3ec331e | 2011-08-10 03:46:27 +0000 | [diff] [blame] | 493 | /// expression in terms of that IV. |
| 494 | /// |
Andrew Trick | 6dbb060 | 2011-08-10 18:07:05 +0000 | [diff] [blame] | 495 | /// This is similar to IVUsers' isInteresting() but processes each instruction |
Andrew Trick | 3ec331e | 2011-08-10 03:46:27 +0000 | [diff] [blame] | 496 | /// non-recursively when the operand is already known to be a simpleIVUser. |
| 497 | /// |
| 498 | static bool isSimpleIVUser(Instruction *I, const Loop *L, ScalarEvolution *SE) { |
| 499 | if (!SE->isSCEVable(I->getType())) |
| 500 | return false; |
| 501 | |
| 502 | // Get the symbolic expression for this instruction. |
| 503 | const SCEV *S = SE->getSCEV(I); |
| 504 | |
| 505 | // Only consider affine recurrences. |
| 506 | const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(S); |
| 507 | if (AR && AR->getLoop() == L) |
| 508 | return true; |
| 509 | |
| 510 | return false; |
| 511 | } |
| 512 | |
Sanjay Patel | 7777b50 | 2014-11-12 18:07:42 +0000 | [diff] [blame] | 513 | /// Iteratively perform simplification on a worklist of users |
Andrew Trick | 3ec331e | 2011-08-10 03:46:27 +0000 | [diff] [blame] | 514 | /// of the specified induction variable. Each successive simplification may push |
| 515 | /// more users which may themselves be candidates for simplification. |
| 516 | /// |
| 517 | /// This algorithm does not require IVUsers analysis. Instead, it simplifies |
| 518 | /// instructions in-place during analysis. Rather than rewriting induction |
| 519 | /// variables bottom-up from their users, it transforms a chain of IVUsers |
Benjamin Kramer | df005cb | 2015-08-08 18:27:36 +0000 | [diff] [blame] | 520 | /// top-down, updating the IR only when it encounters a clear optimization |
| 521 | /// opportunity. |
Andrew Trick | 3ec331e | 2011-08-10 03:46:27 +0000 | [diff] [blame] | 522 | /// |
| 523 | /// Once DisableIVRewrite is default, LSR will be the only client of IVUsers. |
| 524 | /// |
| 525 | void SimplifyIndvar::simplifyUsers(PHINode *CurrIV, IVVisitor *V) { |
Andrew Trick | 7251e41 | 2011-09-19 17:54:39 +0000 | [diff] [blame] | 526 | if (!SE->isSCEVable(CurrIV->getType())) |
| 527 | return; |
| 528 | |
Andrew Trick | 3ec331e | 2011-08-10 03:46:27 +0000 | [diff] [blame] | 529 | // Instructions processed by SimplifyIndvar for CurrIV. |
| 530 | SmallPtrSet<Instruction*,16> Simplified; |
| 531 | |
| 532 | // Use-def pairs if IV users waiting to be processed for CurrIV. |
| 533 | SmallVector<std::pair<Instruction*, Instruction*>, 8> SimpleIVUsers; |
| 534 | |
| 535 | // Push users of the current LoopPhi. In rare cases, pushIVUsers may be |
| 536 | // called multiple times for the same LoopPhi. This is the proper thing to |
| 537 | // do for loop header phis that use each other. |
| 538 | pushIVUsers(CurrIV, Simplified, SimpleIVUsers); |
| 539 | |
| 540 | while (!SimpleIVUsers.empty()) { |
| 541 | std::pair<Instruction*, Instruction*> UseOper = |
| 542 | SimpleIVUsers.pop_back_val(); |
Andrew Trick | 0ba77a0 | 2013-12-23 23:31:49 +0000 | [diff] [blame] | 543 | Instruction *UseInst = UseOper.first; |
| 544 | |
Andrew Trick | 3ec331e | 2011-08-10 03:46:27 +0000 | [diff] [blame] | 545 | // Bypass back edges to avoid extra work. |
Andrew Trick | 0ba77a0 | 2013-12-23 23:31:49 +0000 | [diff] [blame] | 546 | if (UseInst == CurrIV) continue; |
| 547 | |
| 548 | if (V && V->shouldSplitOverflowInstrinsics()) { |
| 549 | UseInst = splitOverflowIntrinsic(UseInst, V->getDomTree()); |
| 550 | if (!UseInst) |
| 551 | continue; |
| 552 | } |
Andrew Trick | 3ec331e | 2011-08-10 03:46:27 +0000 | [diff] [blame] | 553 | |
Andrew Trick | 74664d5 | 2011-08-10 04:01:31 +0000 | [diff] [blame] | 554 | Instruction *IVOperand = UseOper.second; |
| 555 | for (unsigned N = 0; IVOperand; ++N) { |
| 556 | assert(N <= Simplified.size() && "runaway iteration"); |
Andrew Trick | 3ec331e | 2011-08-10 03:46:27 +0000 | [diff] [blame] | 557 | |
Andrew Trick | 74664d5 | 2011-08-10 04:01:31 +0000 | [diff] [blame] | 558 | Value *NewOper = foldIVUser(UseOper.first, IVOperand); |
| 559 | if (!NewOper) |
| 560 | break; // done folding |
| 561 | IVOperand = dyn_cast<Instruction>(NewOper); |
| 562 | } |
| 563 | if (!IVOperand) |
| 564 | continue; |
| 565 | |
| 566 | if (eliminateIVUser(UseOper.first, IVOperand)) { |
| 567 | pushIVUsers(IVOperand, Simplified, SimpleIVUsers); |
Andrew Trick | 3ec331e | 2011-08-10 03:46:27 +0000 | [diff] [blame] | 568 | continue; |
| 569 | } |
Sanjoy Das | 7c0ce26 | 2015-01-06 19:02:56 +0000 | [diff] [blame] | 570 | |
| 571 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(UseOper.first)) { |
| 572 | if (isa<OverflowingBinaryOperator>(BO) && |
| 573 | strengthenOverflowingOperation(BO, IVOperand)) { |
| 574 | // re-queue uses of the now modified binary operator and fall |
| 575 | // through to the checks that remain. |
| 576 | pushIVUsers(IVOperand, Simplified, SimpleIVUsers); |
| 577 | } |
| 578 | } |
| 579 | |
Andrew Trick | 3ec331e | 2011-08-10 03:46:27 +0000 | [diff] [blame] | 580 | CastInst *Cast = dyn_cast<CastInst>(UseOper.first); |
| 581 | if (V && Cast) { |
| 582 | V->visitCast(Cast); |
| 583 | continue; |
| 584 | } |
| 585 | if (isSimpleIVUser(UseOper.first, L, SE)) { |
| 586 | pushIVUsers(UseOper.first, Simplified, SimpleIVUsers); |
| 587 | } |
| 588 | } |
| 589 | } |
| 590 | |
| 591 | namespace llvm { |
| 592 | |
David Blaikie | a379b181 | 2011-12-20 02:50:00 +0000 | [diff] [blame] | 593 | void IVVisitor::anchor() { } |
| 594 | |
Sanjay Patel | 7777b50 | 2014-11-12 18:07:42 +0000 | [diff] [blame] | 595 | /// Simplify instructions that use this induction variable |
Andrew Trick | 3ec331e | 2011-08-10 03:46:27 +0000 | [diff] [blame] | 596 | /// by using ScalarEvolution to analyze the IV's recurrence. |
Sanjoy Das | 5c8bead | 2015-10-06 21:44:49 +0000 | [diff] [blame] | 597 | bool simplifyUsersOfIV(PHINode *CurrIV, ScalarEvolution *SE, DominatorTree *DT, |
| 598 | LPPassManager *LPM, SmallVectorImpl<WeakVH> &Dead, IVVisitor *V) |
Andrew Trick | 3ec331e | 2011-08-10 03:46:27 +0000 | [diff] [blame] | 599 | { |
Chandler Carruth | 4f8f307 | 2015-01-17 14:16:18 +0000 | [diff] [blame] | 600 | LoopInfo *LI = &LPM->getAnalysis<LoopInfoWrapperPass>().getLoopInfo(); |
Sanjoy Das | 5c8bead | 2015-10-06 21:44:49 +0000 | [diff] [blame] | 601 | |
| 602 | SimplifyIndvar SIV(LI->getLoopFor(CurrIV->getParent()), SE, DT, LI, Dead); |
Andrew Trick | 3ec331e | 2011-08-10 03:46:27 +0000 | [diff] [blame] | 603 | SIV.simplifyUsers(CurrIV, V); |
| 604 | return SIV.hasChanged(); |
| 605 | } |
| 606 | |
Sanjay Patel | 7777b50 | 2014-11-12 18:07:42 +0000 | [diff] [blame] | 607 | /// Simplify users of induction variables within this |
Andrew Trick | 3ec331e | 2011-08-10 03:46:27 +0000 | [diff] [blame] | 608 | /// loop. This does not actually change or add IVs. |
Sanjoy Das | 5c8bead | 2015-10-06 21:44:49 +0000 | [diff] [blame] | 609 | bool simplifyLoopIVs(Loop *L, ScalarEvolution *SE, DominatorTree *DT, |
| 610 | LPPassManager *LPM, SmallVectorImpl<WeakVH> &Dead) { |
Andrew Trick | 3ec331e | 2011-08-10 03:46:27 +0000 | [diff] [blame] | 611 | bool Changed = false; |
| 612 | for (BasicBlock::iterator I = L->getHeader()->begin(); isa<PHINode>(I); ++I) { |
Sanjoy Das | 5c8bead | 2015-10-06 21:44:49 +0000 | [diff] [blame] | 613 | Changed |= simplifyUsersOfIV(cast<PHINode>(I), SE, DT, LPM, Dead); |
Andrew Trick | 3ec331e | 2011-08-10 03:46:27 +0000 | [diff] [blame] | 614 | } |
| 615 | return Changed; |
| 616 | } |
| 617 | |
Andrew Trick | 3ec331e | 2011-08-10 03:46:27 +0000 | [diff] [blame] | 618 | } // namespace llvm |