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