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" |
Hongbin Zheng | d36f2030 | 2017-10-12 02:54:11 +0000 | [diff] [blame] | 21 | #include "llvm/Analysis/ScalarEvolutionExpander.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 22 | #include "llvm/IR/DataLayout.h" |
Chandler Carruth | 5ad5f15 | 2014-01-13 09:26:24 +0000 | [diff] [blame] | 23 | #include "llvm/IR/Dominators.h" |
Chandler Carruth | 8a8cd2b | 2014-01-07 11:48:04 +0000 | [diff] [blame] | 24 | #include "llvm/IR/IRBuilder.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 25 | #include "llvm/IR/Instructions.h" |
David Green | b26a0a4 | 2017-07-05 13:25:58 +0000 | [diff] [blame] | 26 | #include "llvm/IR/PatternMatch.h" |
Andrew Trick | 3ec331e | 2011-08-10 03:46:27 +0000 | [diff] [blame] | 27 | #include "llvm/Support/Debug.h" |
| 28 | #include "llvm/Support/raw_ostream.h" |
Max Kazantsev | 0ed7962 | 2018-06-13 02:25:32 +0000 | [diff] [blame] | 29 | #include "llvm/Transforms/Utils/Local.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"); |
Hongbin Zheng | d1b7b2e | 2017-09-27 03:11:46 +0000 | [diff] [blame] | 37 | STATISTIC(NumFoldedUser, "Number of IV users folded into a constant"); |
Andrew Trick | 3ec331e | 2011-08-10 03:46:27 +0000 | [diff] [blame] | 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"); |
Hongbin Zheng | f0093e4 | 2017-09-25 17:39:40 +0000 | [diff] [blame] | 42 | STATISTIC( |
| 43 | NumSimplifiedSRem, |
| 44 | "Number of IV signed remainder operations converted to unsigned remainder"); |
Andrew Trick | 3ec331e | 2011-08-10 03:46:27 +0000 | [diff] [blame] | 45 | STATISTIC(NumElimCmp , "Number of IV comparisons eliminated"); |
| 46 | |
| 47 | namespace { |
Sanjay Patel | 7777b50 | 2014-11-12 18:07:42 +0000 | [diff] [blame] | 48 | /// This is a utility for simplifying induction variables |
Andrew Trick | 3ec331e | 2011-08-10 03:46:27 +0000 | [diff] [blame] | 49 | /// based on ScalarEvolution. It is the primary instrument of the |
| 50 | /// IndvarSimplify pass, but it may also be directly invoked to cleanup after |
| 51 | /// other loop passes that preserve SCEV. |
| 52 | class SimplifyIndvar { |
| 53 | Loop *L; |
| 54 | LoopInfo *LI; |
Andrew Trick | 3ec331e | 2011-08-10 03:46:27 +0000 | [diff] [blame] | 55 | ScalarEvolution *SE; |
Sanjoy Das | 5c8bead | 2015-10-06 21:44:49 +0000 | [diff] [blame] | 56 | DominatorTree *DT; |
Hongbin Zheng | d36f2030 | 2017-10-12 02:54:11 +0000 | [diff] [blame] | 57 | SCEVExpander &Rewriter; |
Sanjoy Das | e6bca0e | 2017-05-01 17:07:49 +0000 | [diff] [blame] | 58 | SmallVectorImpl<WeakTrackingVH> &DeadInsts; |
Andrew Trick | 3ec331e | 2011-08-10 03:46:27 +0000 | [diff] [blame] | 59 | |
| 60 | bool Changed; |
| 61 | |
| 62 | public: |
Sanjoy Das | 5c8bead | 2015-10-06 21:44:49 +0000 | [diff] [blame] | 63 | SimplifyIndvar(Loop *Loop, ScalarEvolution *SE, DominatorTree *DT, |
Hongbin Zheng | d36f2030 | 2017-10-12 02:54:11 +0000 | [diff] [blame] | 64 | LoopInfo *LI, SCEVExpander &Rewriter, |
| 65 | SmallVectorImpl<WeakTrackingVH> &Dead) |
| 66 | : L(Loop), LI(LI), SE(SE), DT(DT), Rewriter(Rewriter), DeadInsts(Dead), |
| 67 | Changed(false) { |
Andrew Trick | e629d00 | 2011-08-10 04:22:26 +0000 | [diff] [blame] | 68 | assert(LI && "IV simplification requires LoopInfo"); |
Andrew Trick | 3ec331e | 2011-08-10 03:46:27 +0000 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | bool hasChanged() const { return Changed; } |
| 72 | |
| 73 | /// Iteratively perform simplification on a worklist of users of the |
| 74 | /// specified induction variable. This is the top-level driver that applies |
Benjamin Kramer | df005cb | 2015-08-08 18:27:36 +0000 | [diff] [blame] | 75 | /// all simplifications to users of an IV. |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 76 | void simplifyUsers(PHINode *CurrIV, IVVisitor *V = nullptr); |
Andrew Trick | 3ec331e | 2011-08-10 03:46:27 +0000 | [diff] [blame] | 77 | |
Andrew Trick | 74664d5 | 2011-08-10 04:01:31 +0000 | [diff] [blame] | 78 | Value *foldIVUser(Instruction *UseInst, Instruction *IVOperand); |
Andrew Trick | 3ec331e | 2011-08-10 03:46:27 +0000 | [diff] [blame] | 79 | |
Sanjoy Das | 088bb0e | 2015-10-06 21:44:39 +0000 | [diff] [blame] | 80 | bool eliminateIdentitySCEV(Instruction *UseInst, Instruction *IVOperand); |
Hongbin Zheng | d36f2030 | 2017-10-12 02:54:11 +0000 | [diff] [blame] | 81 | bool replaceIVUserWithLoopInvariant(Instruction *UseInst); |
Sanjoy Das | 088bb0e | 2015-10-06 21:44:39 +0000 | [diff] [blame] | 82 | |
Sanjoy Das | ae09b3c | 2016-05-29 00:36:25 +0000 | [diff] [blame] | 83 | bool eliminateOverflowIntrinsic(CallInst *CI); |
Max Kazantsev | 37da433 | 2018-06-19 04:48:34 +0000 | [diff] [blame] | 84 | bool eliminateTrunc(TruncInst *TI); |
Andrew Trick | 3ec331e | 2011-08-10 03:46:27 +0000 | [diff] [blame] | 85 | bool eliminateIVUser(Instruction *UseInst, Instruction *IVOperand); |
Philip Reames | 7b861f0 | 2017-11-01 19:49:20 +0000 | [diff] [blame] | 86 | bool makeIVComparisonInvariant(ICmpInst *ICmp, Value *IVOperand); |
Andrew Trick | 3ec331e | 2011-08-10 03:46:27 +0000 | [diff] [blame] | 87 | void eliminateIVComparison(ICmpInst *ICmp, Value *IVOperand); |
Hongbin Zheng | f0093e4 | 2017-09-25 17:39:40 +0000 | [diff] [blame] | 88 | void simplifyIVRemainder(BinaryOperator *Rem, Value *IVOperand, |
| 89 | bool IsSigned); |
| 90 | void replaceRemWithNumerator(BinaryOperator *Rem); |
| 91 | void replaceRemWithNumeratorOrZero(BinaryOperator *Rem); |
| 92 | void replaceSRemWithURem(BinaryOperator *Rem); |
Hongbin Zheng | bfd7c38 | 2017-03-30 21:56:56 +0000 | [diff] [blame] | 93 | bool eliminateSDiv(BinaryOperator *SDiv); |
Sanjoy Das | 7c0ce26 | 2015-01-06 19:02:56 +0000 | [diff] [blame] | 94 | bool strengthenOverflowingOperation(BinaryOperator *OBO, Value *IVOperand); |
David Green | b26a0a4 | 2017-07-05 13:25:58 +0000 | [diff] [blame] | 95 | bool strengthenRightShift(BinaryOperator *BO, Value *IVOperand); |
Andrew Trick | 3ec331e | 2011-08-10 03:46:27 +0000 | [diff] [blame] | 96 | }; |
Alexander Kornienko | f00654e | 2015-06-23 09:49:53 +0000 | [diff] [blame] | 97 | } |
Andrew Trick | 3ec331e | 2011-08-10 03:46:27 +0000 | [diff] [blame] | 98 | |
Sanjay Patel | 7777b50 | 2014-11-12 18:07:42 +0000 | [diff] [blame] | 99 | /// Fold an IV operand into its use. This removes increments of an |
Andrew Trick | 3ec331e | 2011-08-10 03:46:27 +0000 | [diff] [blame] | 100 | /// aligned IV when used by a instruction that ignores the low bits. |
Andrew Trick | 74664d5 | 2011-08-10 04:01:31 +0000 | [diff] [blame] | 101 | /// |
Andrew Trick | 7251e41 | 2011-09-19 17:54:39 +0000 | [diff] [blame] | 102 | /// IVOperand is guaranteed SCEVable, but UseInst may not be. |
| 103 | /// |
Andrew Trick | 74664d5 | 2011-08-10 04:01:31 +0000 | [diff] [blame] | 104 | /// Return the operand of IVOperand for this induction variable if IVOperand can |
Andrew Trick | 6dbb060 | 2011-08-10 18:07:05 +0000 | [diff] [blame] | 105 | /// be folded (in case more folding opportunities have been exposed). |
Andrew Trick | 74664d5 | 2011-08-10 04:01:31 +0000 | [diff] [blame] | 106 | /// Otherwise return null. |
| 107 | Value *SimplifyIndvar::foldIVUser(Instruction *UseInst, Instruction *IVOperand) { |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 108 | Value *IVSrc = nullptr; |
Andrew Trick | 3ec331e | 2011-08-10 03:46:27 +0000 | [diff] [blame] | 109 | unsigned OperIdx = 0; |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 110 | const SCEV *FoldedExpr = nullptr; |
Andrew Trick | 3ec331e | 2011-08-10 03:46:27 +0000 | [diff] [blame] | 111 | switch (UseInst->getOpcode()) { |
| 112 | default: |
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 | case Instruction::UDiv: |
| 115 | case Instruction::LShr: |
| 116 | // We're only interested in the case where we know something about |
| 117 | // the numerator and have a constant denominator. |
| 118 | if (IVOperand != UseInst->getOperand(OperIdx) || |
| 119 | !isa<ConstantInt>(UseInst->getOperand(1))) |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 120 | return nullptr; |
Andrew Trick | 3ec331e | 2011-08-10 03:46:27 +0000 | [diff] [blame] | 121 | |
| 122 | // Attempt to fold a binary operator with constant operand. |
| 123 | // e.g. ((I + 1) >> 2) => I >> 2 |
Andrew Trick | 9490458 | 2011-11-17 23:36:35 +0000 | [diff] [blame] | 124 | if (!isa<BinaryOperator>(IVOperand) |
| 125 | || !isa<ConstantInt>(IVOperand->getOperand(1))) |
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 | IVSrc = IVOperand->getOperand(0); |
| 129 | // IVSrc must be the (SCEVable) IV, since the other operand is const. |
| 130 | assert(SE->isSCEVable(IVSrc->getType()) && "Expect SCEVable IV operand"); |
| 131 | |
| 132 | ConstantInt *D = cast<ConstantInt>(UseInst->getOperand(1)); |
| 133 | if (UseInst->getOpcode() == Instruction::LShr) { |
| 134 | // Get a constant for the divisor. See createSCEV. |
| 135 | uint32_t BitWidth = cast<IntegerType>(UseInst->getType())->getBitWidth(); |
| 136 | if (D->getValue().uge(BitWidth)) |
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 | D = ConstantInt::get(UseInst->getContext(), |
Benjamin Kramer | fc3ea6f | 2013-07-11 16:05:50 +0000 | [diff] [blame] | 140 | APInt::getOneBitSet(BitWidth, D->getZExtValue())); |
Andrew Trick | 3ec331e | 2011-08-10 03:46:27 +0000 | [diff] [blame] | 141 | } |
| 142 | FoldedExpr = SE->getUDivExpr(SE->getSCEV(IVSrc), SE->getSCEV(D)); |
| 143 | } |
| 144 | // We have something that might fold it's operand. Compare SCEVs. |
| 145 | if (!SE->isSCEVable(UseInst->getType())) |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 146 | return nullptr; |
Andrew Trick | 3ec331e | 2011-08-10 03:46:27 +0000 | [diff] [blame] | 147 | |
| 148 | // Bypass the operand if SCEV can prove it has no effect. |
| 149 | if (SE->getSCEV(UseInst) != FoldedExpr) |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 150 | return nullptr; |
Andrew Trick | 3ec331e | 2011-08-10 03:46:27 +0000 | [diff] [blame] | 151 | |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 152 | LLVM_DEBUG(dbgs() << "INDVARS: Eliminated IV operand: " << *IVOperand |
| 153 | << " -> " << *UseInst << '\n'); |
Andrew Trick | 3ec331e | 2011-08-10 03:46:27 +0000 | [diff] [blame] | 154 | |
| 155 | UseInst->setOperand(OperIdx, IVSrc); |
| 156 | assert(SE->getSCEV(UseInst) == FoldedExpr && "bad SCEV with folded oper"); |
| 157 | |
| 158 | ++NumElimOperand; |
| 159 | Changed = true; |
| 160 | if (IVOperand->use_empty()) |
Benjamin Kramer | f5e2fc4 | 2015-05-29 19:43:39 +0000 | [diff] [blame] | 161 | DeadInsts.emplace_back(IVOperand); |
Andrew Trick | 74664d5 | 2011-08-10 04:01:31 +0000 | [diff] [blame] | 162 | return IVSrc; |
Andrew Trick | 3ec331e | 2011-08-10 03:46:27 +0000 | [diff] [blame] | 163 | } |
| 164 | |
Philip Reames | 7b861f0 | 2017-11-01 19:49:20 +0000 | [diff] [blame] | 165 | bool SimplifyIndvar::makeIVComparisonInvariant(ICmpInst *ICmp, |
| 166 | Value *IVOperand) { |
| 167 | unsigned IVOperIdx = 0; |
| 168 | ICmpInst::Predicate Pred = ICmp->getPredicate(); |
| 169 | if (IVOperand != ICmp->getOperand(0)) { |
| 170 | // Swapped |
| 171 | assert(IVOperand == ICmp->getOperand(1) && "Can't find IVOperand"); |
| 172 | IVOperIdx = 1; |
| 173 | Pred = ICmpInst::getSwappedPredicate(Pred); |
| 174 | } |
Philip Reames | dc417a9 | 2017-10-31 18:04:57 +0000 | [diff] [blame] | 175 | |
Philip Reames | 7b861f0 | 2017-11-01 19:49:20 +0000 | [diff] [blame] | 176 | // Get the SCEVs for the ICmp operands (in the specific context of the |
| 177 | // current loop) |
| 178 | const Loop *ICmpLoop = LI->getLoopFor(ICmp->getParent()); |
| 179 | const SCEV *S = SE->getSCEVAtScope(ICmp->getOperand(IVOperIdx), ICmpLoop); |
| 180 | const SCEV *X = SE->getSCEVAtScope(ICmp->getOperand(1 - IVOperIdx), ICmpLoop); |
| 181 | |
| 182 | ICmpInst::Predicate InvariantPredicate; |
Philip Reames | dc417a9 | 2017-10-31 18:04:57 +0000 | [diff] [blame] | 183 | const SCEV *InvariantLHS, *InvariantRHS; |
Philip Reames | 7b861f0 | 2017-11-01 19:49:20 +0000 | [diff] [blame] | 184 | |
| 185 | auto *PN = dyn_cast<PHINode>(IVOperand); |
| 186 | if (!PN) |
| 187 | return false; |
| 188 | if (!SE->isLoopInvariantPredicate(Pred, S, X, L, InvariantPredicate, |
Philip Reames | dc417a9 | 2017-10-31 18:04:57 +0000 | [diff] [blame] | 189 | InvariantLHS, InvariantRHS)) |
| 190 | return false; |
| 191 | |
| 192 | // Rewrite the comparison to a loop invariant comparison if it can be done |
| 193 | // cheaply, where cheaply means "we don't need to emit any new |
| 194 | // instructions". |
Philip Reames | dc417a9 | 2017-10-31 18:04:57 +0000 | [diff] [blame] | 195 | |
Philip Reames | 7b861f0 | 2017-11-01 19:49:20 +0000 | [diff] [blame] | 196 | SmallDenseMap<const SCEV*, Value*> CheapExpansions; |
| 197 | CheapExpansions[S] = ICmp->getOperand(IVOperIdx); |
| 198 | CheapExpansions[X] = ICmp->getOperand(1 - IVOperIdx); |
| 199 | |
| 200 | // TODO: Support multiple entry loops? (We currently bail out of these in |
| 201 | // the IndVarSimplify pass) |
| 202 | if (auto *BB = L->getLoopPredecessor()) { |
Philip Reames | 6260cf7 | 2017-12-01 20:57:19 +0000 | [diff] [blame] | 203 | const int Idx = PN->getBasicBlockIndex(BB); |
| 204 | if (Idx >= 0) { |
| 205 | Value *Incoming = PN->getIncomingValue(Idx); |
| 206 | const SCEV *IncomingS = SE->getSCEV(Incoming); |
| 207 | CheapExpansions[IncomingS] = Incoming; |
| 208 | } |
Philip Reames | 7b861f0 | 2017-11-01 19:49:20 +0000 | [diff] [blame] | 209 | } |
| 210 | Value *NewLHS = CheapExpansions[InvariantLHS]; |
| 211 | Value *NewRHS = CheapExpansions[InvariantRHS]; |
| 212 | |
Philip Reames | 6260cf7 | 2017-12-01 20:57:19 +0000 | [diff] [blame] | 213 | if (!NewLHS) |
| 214 | if (auto *ConstLHS = dyn_cast<SCEVConstant>(InvariantLHS)) |
| 215 | NewLHS = ConstLHS->getValue(); |
| 216 | if (!NewRHS) |
| 217 | if (auto *ConstRHS = dyn_cast<SCEVConstant>(InvariantRHS)) |
| 218 | NewRHS = ConstRHS->getValue(); |
| 219 | |
Philip Reames | 7b861f0 | 2017-11-01 19:49:20 +0000 | [diff] [blame] | 220 | if (!NewLHS || !NewRHS) |
| 221 | // We could not find an existing value to replace either LHS or RHS. |
| 222 | // Generating new instructions has subtler tradeoffs, so avoid doing that |
| 223 | // for now. |
| 224 | return false; |
| 225 | |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 226 | LLVM_DEBUG(dbgs() << "INDVARS: Simplified comparison: " << *ICmp << '\n'); |
Philip Reames | 7b861f0 | 2017-11-01 19:49:20 +0000 | [diff] [blame] | 227 | ICmp->setPredicate(InvariantPredicate); |
| 228 | ICmp->setOperand(0, NewLHS); |
| 229 | ICmp->setOperand(1, NewRHS); |
| 230 | return true; |
Philip Reames | dc417a9 | 2017-10-31 18:04:57 +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 | /// comparisons against an induction variable. |
| 235 | void SimplifyIndvar::eliminateIVComparison(ICmpInst *ICmp, Value *IVOperand) { |
| 236 | unsigned IVOperIdx = 0; |
| 237 | ICmpInst::Predicate Pred = ICmp->getPredicate(); |
Max Kazantsev | b9edcbc | 2017-07-08 17:17:30 +0000 | [diff] [blame] | 238 | ICmpInst::Predicate OriginalPred = Pred; |
Andrew Trick | 3ec331e | 2011-08-10 03:46:27 +0000 | [diff] [blame] | 239 | if (IVOperand != ICmp->getOperand(0)) { |
| 240 | // Swapped |
| 241 | assert(IVOperand == ICmp->getOperand(1) && "Can't find IVOperand"); |
| 242 | IVOperIdx = 1; |
| 243 | Pred = ICmpInst::getSwappedPredicate(Pred); |
| 244 | } |
| 245 | |
Philip Reames | 29dd40b | 2017-10-26 22:02:16 +0000 | [diff] [blame] | 246 | // Get the SCEVs for the ICmp operands (in the specific context of the |
| 247 | // current loop) |
Andrew Trick | 3ec331e | 2011-08-10 03:46:27 +0000 | [diff] [blame] | 248 | const Loop *ICmpLoop = LI->getLoopFor(ICmp->getParent()); |
Philip Reames | 29dd40b | 2017-10-26 22:02:16 +0000 | [diff] [blame] | 249 | const SCEV *S = SE->getSCEVAtScope(ICmp->getOperand(IVOperIdx), ICmpLoop); |
| 250 | const SCEV *X = SE->getSCEVAtScope(ICmp->getOperand(1 - IVOperIdx), ICmpLoop); |
Andrew Trick | 3ec331e | 2011-08-10 03:46:27 +0000 | [diff] [blame] | 251 | |
| 252 | // If the condition is always true or always false, replace it with |
| 253 | // a constant value. |
Sanjoy Das | 5dab205 | 2015-07-27 21:42:49 +0000 | [diff] [blame] | 254 | if (SE->isKnownPredicate(Pred, S, X)) { |
Andrew Trick | 3ec331e | 2011-08-10 03:46:27 +0000 | [diff] [blame] | 255 | ICmp->replaceAllUsesWith(ConstantInt::getTrue(ICmp->getContext())); |
Sanjoy Das | 5dab205 | 2015-07-27 21:42:49 +0000 | [diff] [blame] | 256 | DeadInsts.emplace_back(ICmp); |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 257 | LLVM_DEBUG(dbgs() << "INDVARS: Eliminated comparison: " << *ICmp << '\n'); |
Sanjoy Das | 5dab205 | 2015-07-27 21:42:49 +0000 | [diff] [blame] | 258 | } else if (SE->isKnownPredicate(ICmpInst::getInversePredicate(Pred), S, X)) { |
Andrew Trick | 3ec331e | 2011-08-10 03:46:27 +0000 | [diff] [blame] | 259 | ICmp->replaceAllUsesWith(ConstantInt::getFalse(ICmp->getContext())); |
Sanjoy Das | 5dab205 | 2015-07-27 21:42:49 +0000 | [diff] [blame] | 260 | DeadInsts.emplace_back(ICmp); |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 261 | LLVM_DEBUG(dbgs() << "INDVARS: Eliminated comparison: " << *ICmp << '\n'); |
Philip Reames | 7b861f0 | 2017-11-01 19:49:20 +0000 | [diff] [blame] | 262 | } else if (makeIVComparisonInvariant(ICmp, IVOperand)) { |
| 263 | // fallthrough to end of function |
Max Kazantsev | b9edcbc | 2017-07-08 17:17:30 +0000 | [diff] [blame] | 264 | } else if (ICmpInst::isSigned(OriginalPred) && |
| 265 | SE->isKnownNonNegative(S) && SE->isKnownNonNegative(X)) { |
| 266 | // If we were unable to make anything above, all we can is to canonicalize |
| 267 | // the comparison hoping that it will open the doors for other |
| 268 | // optimizations. If we find out that we compare two non-negative values, |
| 269 | // we turn the instruction's predicate to its unsigned version. Note that |
| 270 | // we cannot rely on Pred here unless we check if we have swapped it. |
| 271 | assert(ICmp->getPredicate() == OriginalPred && "Predicate changed?"); |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 272 | LLVM_DEBUG(dbgs() << "INDVARS: Turn to unsigned comparison: " << *ICmp |
| 273 | << '\n'); |
Max Kazantsev | b9edcbc | 2017-07-08 17:17:30 +0000 | [diff] [blame] | 274 | ICmp->setPredicate(ICmpInst::getUnsignedPredicate(OriginalPred)); |
Sanjoy Das | 5dab205 | 2015-07-27 21:42:49 +0000 | [diff] [blame] | 275 | } else |
Andrew Trick | 3ec331e | 2011-08-10 03:46:27 +0000 | [diff] [blame] | 276 | return; |
| 277 | |
Andrew Trick | 3ec331e | 2011-08-10 03:46:27 +0000 | [diff] [blame] | 278 | ++NumElimCmp; |
| 279 | Changed = true; |
Andrew Trick | 3ec331e | 2011-08-10 03:46:27 +0000 | [diff] [blame] | 280 | } |
| 281 | |
Hongbin Zheng | bfd7c38 | 2017-03-30 21:56:56 +0000 | [diff] [blame] | 282 | bool SimplifyIndvar::eliminateSDiv(BinaryOperator *SDiv) { |
| 283 | // Get the SCEVs for the ICmp operands. |
| 284 | auto *N = SE->getSCEV(SDiv->getOperand(0)); |
| 285 | auto *D = SE->getSCEV(SDiv->getOperand(1)); |
| 286 | |
| 287 | // Simplify unnecessary loops away. |
| 288 | const Loop *L = LI->getLoopFor(SDiv->getParent()); |
| 289 | N = SE->getSCEVAtScope(N, L); |
| 290 | D = SE->getSCEVAtScope(D, L); |
| 291 | |
| 292 | // Replace sdiv by udiv if both of the operands are non-negative |
| 293 | if (SE->isKnownNonNegative(N) && SE->isKnownNonNegative(D)) { |
| 294 | auto *UDiv = BinaryOperator::Create( |
| 295 | BinaryOperator::UDiv, SDiv->getOperand(0), SDiv->getOperand(1), |
| 296 | SDiv->getName() + ".udiv", SDiv); |
| 297 | UDiv->setIsExact(SDiv->isExact()); |
| 298 | SDiv->replaceAllUsesWith(UDiv); |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 299 | LLVM_DEBUG(dbgs() << "INDVARS: Simplified sdiv: " << *SDiv << '\n'); |
Hongbin Zheng | bfd7c38 | 2017-03-30 21:56:56 +0000 | [diff] [blame] | 300 | ++NumSimplifiedSDiv; |
| 301 | Changed = true; |
| 302 | DeadInsts.push_back(SDiv); |
| 303 | return true; |
| 304 | } |
| 305 | |
| 306 | return false; |
| 307 | } |
| 308 | |
Hongbin Zheng | f0093e4 | 2017-09-25 17:39:40 +0000 | [diff] [blame] | 309 | // i %s n -> i %u n if i >= 0 and n >= 0 |
| 310 | void SimplifyIndvar::replaceSRemWithURem(BinaryOperator *Rem) { |
| 311 | auto *N = Rem->getOperand(0), *D = Rem->getOperand(1); |
| 312 | auto *URem = BinaryOperator::Create(BinaryOperator::URem, N, D, |
| 313 | Rem->getName() + ".urem", Rem); |
| 314 | Rem->replaceAllUsesWith(URem); |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 315 | LLVM_DEBUG(dbgs() << "INDVARS: Simplified srem: " << *Rem << '\n'); |
Hongbin Zheng | f0093e4 | 2017-09-25 17:39:40 +0000 | [diff] [blame] | 316 | ++NumSimplifiedSRem; |
Hongbin Zheng | bbe448a | 2017-09-25 18:10:36 +0000 | [diff] [blame] | 317 | Changed = true; |
Hongbin Zheng | f0093e4 | 2017-09-25 17:39:40 +0000 | [diff] [blame] | 318 | DeadInsts.emplace_back(Rem); |
| 319 | } |
Andrew Trick | 3ec331e | 2011-08-10 03:46:27 +0000 | [diff] [blame] | 320 | |
Hongbin Zheng | f0093e4 | 2017-09-25 17:39:40 +0000 | [diff] [blame] | 321 | // i % n --> i if i is in [0,n). |
| 322 | void SimplifyIndvar::replaceRemWithNumerator(BinaryOperator *Rem) { |
| 323 | Rem->replaceAllUsesWith(Rem->getOperand(0)); |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 324 | LLVM_DEBUG(dbgs() << "INDVARS: Simplified rem: " << *Rem << '\n'); |
Andrew Trick | 3ec331e | 2011-08-10 03:46:27 +0000 | [diff] [blame] | 325 | ++NumElimRem; |
| 326 | Changed = true; |
Benjamin Kramer | f5e2fc4 | 2015-05-29 19:43:39 +0000 | [diff] [blame] | 327 | DeadInsts.emplace_back(Rem); |
Andrew Trick | 3ec331e | 2011-08-10 03:46:27 +0000 | [diff] [blame] | 328 | } |
| 329 | |
Hongbin Zheng | f0093e4 | 2017-09-25 17:39:40 +0000 | [diff] [blame] | 330 | // (i+1) % n --> (i+1)==n?0:(i+1) if i is in [0,n). |
| 331 | void SimplifyIndvar::replaceRemWithNumeratorOrZero(BinaryOperator *Rem) { |
| 332 | auto *T = Rem->getType(); |
| 333 | auto *N = Rem->getOperand(0), *D = Rem->getOperand(1); |
| 334 | ICmpInst *ICmp = new ICmpInst(Rem, ICmpInst::ICMP_EQ, N, D); |
| 335 | SelectInst *Sel = |
| 336 | SelectInst::Create(ICmp, ConstantInt::get(T, 0), N, "iv.rem", Rem); |
| 337 | Rem->replaceAllUsesWith(Sel); |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 338 | LLVM_DEBUG(dbgs() << "INDVARS: Simplified rem: " << *Rem << '\n'); |
Hongbin Zheng | f0093e4 | 2017-09-25 17:39:40 +0000 | [diff] [blame] | 339 | ++NumElimRem; |
| 340 | Changed = true; |
| 341 | DeadInsts.emplace_back(Rem); |
| 342 | } |
| 343 | |
| 344 | /// SimplifyIVUsers helper for eliminating useless remainder operations |
| 345 | /// operating on an induction variable or replacing srem by urem. |
| 346 | void SimplifyIndvar::simplifyIVRemainder(BinaryOperator *Rem, Value *IVOperand, |
| 347 | bool IsSigned) { |
| 348 | auto *NValue = Rem->getOperand(0); |
| 349 | auto *DValue = Rem->getOperand(1); |
| 350 | // We're only interested in the case where we know something about |
| 351 | // the numerator, unless it is a srem, because we want to replace srem by urem |
| 352 | // in general. |
| 353 | bool UsedAsNumerator = IVOperand == NValue; |
| 354 | if (!UsedAsNumerator && !IsSigned) |
| 355 | return; |
| 356 | |
| 357 | const SCEV *N = SE->getSCEV(NValue); |
| 358 | |
| 359 | // Simplify unnecessary loops away. |
| 360 | const Loop *ICmpLoop = LI->getLoopFor(Rem->getParent()); |
| 361 | N = SE->getSCEVAtScope(N, ICmpLoop); |
| 362 | |
| 363 | bool IsNumeratorNonNegative = !IsSigned || SE->isKnownNonNegative(N); |
| 364 | |
| 365 | // Do not proceed if the Numerator may be negative |
| 366 | if (!IsNumeratorNonNegative) |
| 367 | return; |
| 368 | |
| 369 | const SCEV *D = SE->getSCEV(DValue); |
| 370 | D = SE->getSCEVAtScope(D, ICmpLoop); |
| 371 | |
| 372 | if (UsedAsNumerator) { |
| 373 | auto LT = IsSigned ? ICmpInst::ICMP_SLT : ICmpInst::ICMP_ULT; |
| 374 | if (SE->isKnownPredicate(LT, N, D)) { |
| 375 | replaceRemWithNumerator(Rem); |
| 376 | return; |
| 377 | } |
| 378 | |
| 379 | auto *T = Rem->getType(); |
| 380 | const auto *NLessOne = SE->getMinusSCEV(N, SE->getOne(T)); |
| 381 | if (SE->isKnownPredicate(LT, NLessOne, D)) { |
| 382 | replaceRemWithNumeratorOrZero(Rem); |
| 383 | return; |
| 384 | } |
| 385 | } |
| 386 | |
| 387 | // Try to replace SRem with URem, if both N and D are known non-negative. |
| 388 | // Since we had already check N, we only need to check D now |
| 389 | if (!IsSigned || !SE->isKnownNonNegative(D)) |
| 390 | return; |
| 391 | |
| 392 | replaceSRemWithURem(Rem); |
Hongbin Zheng | f0093e4 | 2017-09-25 17:39:40 +0000 | [diff] [blame] | 393 | } |
| 394 | |
Sanjoy Das | ae09b3c | 2016-05-29 00:36:25 +0000 | [diff] [blame] | 395 | bool SimplifyIndvar::eliminateOverflowIntrinsic(CallInst *CI) { |
| 396 | auto *F = CI->getCalledFunction(); |
| 397 | if (!F) |
| 398 | return false; |
| 399 | |
| 400 | typedef const SCEV *(ScalarEvolution::*OperationFunctionTy)( |
Max Kazantsev | dc80366 | 2017-06-15 11:48:21 +0000 | [diff] [blame] | 401 | const SCEV *, const SCEV *, SCEV::NoWrapFlags, unsigned); |
Sanjoy Das | ae09b3c | 2016-05-29 00:36:25 +0000 | [diff] [blame] | 402 | typedef const SCEV *(ScalarEvolution::*ExtensionFunctionTy)( |
Max Kazantsev | 8d0322e | 2017-06-30 05:04:09 +0000 | [diff] [blame] | 403 | const SCEV *, Type *, unsigned); |
Sanjoy Das | ae09b3c | 2016-05-29 00:36:25 +0000 | [diff] [blame] | 404 | |
| 405 | OperationFunctionTy Operation; |
| 406 | ExtensionFunctionTy Extension; |
| 407 | |
| 408 | Instruction::BinaryOps RawOp; |
| 409 | |
| 410 | // We always have exactly one of nsw or nuw. If NoSignedOverflow is false, we |
| 411 | // have nuw. |
| 412 | bool NoSignedOverflow; |
| 413 | |
| 414 | switch (F->getIntrinsicID()) { |
| 415 | default: |
| 416 | return false; |
| 417 | |
| 418 | case Intrinsic::sadd_with_overflow: |
| 419 | Operation = &ScalarEvolution::getAddExpr; |
| 420 | Extension = &ScalarEvolution::getSignExtendExpr; |
| 421 | RawOp = Instruction::Add; |
| 422 | NoSignedOverflow = true; |
| 423 | break; |
| 424 | |
| 425 | case Intrinsic::uadd_with_overflow: |
| 426 | Operation = &ScalarEvolution::getAddExpr; |
| 427 | Extension = &ScalarEvolution::getZeroExtendExpr; |
| 428 | RawOp = Instruction::Add; |
| 429 | NoSignedOverflow = false; |
| 430 | break; |
| 431 | |
| 432 | case Intrinsic::ssub_with_overflow: |
| 433 | Operation = &ScalarEvolution::getMinusSCEV; |
| 434 | Extension = &ScalarEvolution::getSignExtendExpr; |
| 435 | RawOp = Instruction::Sub; |
| 436 | NoSignedOverflow = true; |
| 437 | break; |
| 438 | |
| 439 | case Intrinsic::usub_with_overflow: |
| 440 | Operation = &ScalarEvolution::getMinusSCEV; |
| 441 | Extension = &ScalarEvolution::getZeroExtendExpr; |
| 442 | RawOp = Instruction::Sub; |
| 443 | NoSignedOverflow = false; |
| 444 | break; |
| 445 | } |
| 446 | |
| 447 | const SCEV *LHS = SE->getSCEV(CI->getArgOperand(0)); |
| 448 | const SCEV *RHS = SE->getSCEV(CI->getArgOperand(1)); |
| 449 | |
| 450 | auto *NarrowTy = cast<IntegerType>(LHS->getType()); |
| 451 | auto *WideTy = |
| 452 | IntegerType::get(NarrowTy->getContext(), NarrowTy->getBitWidth() * 2); |
| 453 | |
| 454 | const SCEV *A = |
Max Kazantsev | 8d0322e | 2017-06-30 05:04:09 +0000 | [diff] [blame] | 455 | (SE->*Extension)((SE->*Operation)(LHS, RHS, SCEV::FlagAnyWrap, 0), |
| 456 | WideTy, 0); |
Sanjoy Das | ae09b3c | 2016-05-29 00:36:25 +0000 | [diff] [blame] | 457 | const SCEV *B = |
Max Kazantsev | 8d0322e | 2017-06-30 05:04:09 +0000 | [diff] [blame] | 458 | (SE->*Operation)((SE->*Extension)(LHS, WideTy, 0), |
| 459 | (SE->*Extension)(RHS, WideTy, 0), SCEV::FlagAnyWrap, 0); |
Sanjoy Das | ae09b3c | 2016-05-29 00:36:25 +0000 | [diff] [blame] | 460 | |
| 461 | if (A != B) |
| 462 | return false; |
| 463 | |
| 464 | // Proved no overflow, nuke the overflow check and, if possible, the overflow |
| 465 | // intrinsic as well. |
| 466 | |
| 467 | BinaryOperator *NewResult = BinaryOperator::Create( |
| 468 | RawOp, CI->getArgOperand(0), CI->getArgOperand(1), "", CI); |
| 469 | |
| 470 | if (NoSignedOverflow) |
| 471 | NewResult->setHasNoSignedWrap(true); |
| 472 | else |
| 473 | NewResult->setHasNoUnsignedWrap(true); |
| 474 | |
| 475 | SmallVector<ExtractValueInst *, 4> ToDelete; |
| 476 | |
| 477 | for (auto *U : CI->users()) { |
| 478 | if (auto *EVI = dyn_cast<ExtractValueInst>(U)) { |
| 479 | if (EVI->getIndices()[0] == 1) |
| 480 | EVI->replaceAllUsesWith(ConstantInt::getFalse(CI->getContext())); |
| 481 | else { |
| 482 | assert(EVI->getIndices()[0] == 0 && "Only two possibilities!"); |
| 483 | EVI->replaceAllUsesWith(NewResult); |
| 484 | } |
| 485 | ToDelete.push_back(EVI); |
| 486 | } |
| 487 | } |
| 488 | |
| 489 | for (auto *EVI : ToDelete) |
| 490 | EVI->eraseFromParent(); |
| 491 | |
| 492 | if (CI->use_empty()) |
| 493 | CI->eraseFromParent(); |
| 494 | |
| 495 | return true; |
| 496 | } |
| 497 | |
Max Kazantsev | 37da433 | 2018-06-19 04:48:34 +0000 | [diff] [blame] | 498 | bool SimplifyIndvar::eliminateTrunc(TruncInst *TI) { |
| 499 | // It is always legal to replace |
| 500 | // icmp <pred> i32 trunc(iv), n |
| 501 | // with |
| 502 | // icmp <pred> i64 sext(trunc(iv)), sext(n), if pred is signed predicate. |
| 503 | // Or with |
| 504 | // icmp <pred> i64 zext(trunc(iv)), zext(n), if pred is unsigned predicate. |
| 505 | // Or with either of these if pred is an equality predicate. |
| 506 | // |
| 507 | // If we can prove that iv == sext(trunc(iv)) or iv == zext(trunc(iv)) for |
| 508 | // every comparison which uses trunc, it means that we can replace each of |
| 509 | // them with comparison of iv against sext/zext(n). We no longer need trunc |
| 510 | // after that. |
| 511 | // |
| 512 | // TODO: Should we do this if we can widen *some* comparisons, but not all |
| 513 | // of them? Sometimes it is enough to enable other optimizations, but the |
| 514 | // trunc instruction will stay in the loop. |
| 515 | Value *IV = TI->getOperand(0); |
| 516 | Type *IVTy = IV->getType(); |
| 517 | const SCEV *IVSCEV = SE->getSCEV(IV); |
| 518 | const SCEV *TISCEV = SE->getSCEV(TI); |
| 519 | |
| 520 | // Check if iv == zext(trunc(iv)) and if iv == sext(trunc(iv)). If so, we can |
| 521 | // get rid of trunc |
| 522 | bool DoesSExtCollapse = false; |
| 523 | bool DoesZExtCollapse = false; |
| 524 | if (IVSCEV == SE->getSignExtendExpr(TISCEV, IVTy)) |
| 525 | DoesSExtCollapse = true; |
| 526 | if (IVSCEV == SE->getZeroExtendExpr(TISCEV, IVTy)) |
| 527 | DoesZExtCollapse = true; |
| 528 | |
| 529 | // If neither sext nor zext does collapse, it is not profitable to do any |
| 530 | // transform. Bail. |
| 531 | if (!DoesSExtCollapse && !DoesZExtCollapse) |
| 532 | return false; |
| 533 | |
| 534 | // Collect users of the trunc that look like comparisons against invariants. |
| 535 | // Bail if we find something different. |
| 536 | SmallVector<ICmpInst *, 4> ICmpUsers; |
| 537 | for (auto *U : TI->users()) { |
Max Kazantsev | f5ba371 | 2018-06-28 08:20:03 +0000 | [diff] [blame] | 538 | // We don't care about users in unreachable blocks. |
| 539 | if (isa<Instruction>(U) && |
| 540 | !DT->isReachableFromEntry(cast<Instruction>(U)->getParent())) |
| 541 | continue; |
Max Kazantsev | 37da433 | 2018-06-19 04:48:34 +0000 | [diff] [blame] | 542 | if (ICmpInst *ICI = dyn_cast<ICmpInst>(U)) { |
| 543 | if (ICI->getOperand(0) == TI && L->isLoopInvariant(ICI->getOperand(1))) { |
| 544 | assert(L->contains(ICI->getParent()) && "LCSSA form broken?"); |
| 545 | // If we cannot get rid of trunc, bail. |
| 546 | if (ICI->isSigned() && !DoesSExtCollapse) |
| 547 | return false; |
| 548 | if (ICI->isUnsigned() && !DoesZExtCollapse) |
| 549 | return false; |
| 550 | // For equality, either signed or unsigned works. |
| 551 | ICmpUsers.push_back(ICI); |
| 552 | } else |
| 553 | return false; |
| 554 | } else |
| 555 | return false; |
| 556 | } |
| 557 | |
Max Kazantsev | 4d98051 | 2018-07-27 09:43:39 +0000 | [diff] [blame^] | 558 | auto CanUseZExt = [&](ICmpInst *ICI) { |
| 559 | // Unsigned comparison can be widened as unsigned. |
| 560 | if (ICI->isUnsigned()) |
| 561 | return true; |
| 562 | // Is it profitable to do zext? |
| 563 | if (!DoesZExtCollapse) |
| 564 | return false; |
| 565 | // For equality, we can safely zext both parts. |
| 566 | if (ICI->isEquality()) |
| 567 | return true; |
| 568 | // Otherwise we can only use zext when comparing two non-negative or two |
| 569 | // negative values. But in practice, we will never pass DoesZExtCollapse |
| 570 | // check for a negative value, because zext(trunc(x)) is non-negative. So |
| 571 | // it only make sense to check for non-negativity here. |
| 572 | const SCEV *SCEVOP1 = SE->getSCEV(ICI->getOperand(0)); |
| 573 | const SCEV *SCEVOP2 = SE->getSCEV(ICI->getOperand(1)); |
| 574 | return SE->isKnownNonNegative(SCEVOP1) && SE->isKnownNonNegative(SCEVOP2); |
| 575 | }; |
Max Kazantsev | 37da433 | 2018-06-19 04:48:34 +0000 | [diff] [blame] | 576 | // Replace all comparisons against trunc with comparisons against IV. |
| 577 | for (auto *ICI : ICmpUsers) { |
| 578 | auto *Op1 = ICI->getOperand(1); |
| 579 | Instruction *Ext = nullptr; |
| 580 | // For signed/unsigned predicate, replace the old comparison with comparison |
| 581 | // of immediate IV against sext/zext of the invariant argument. If we can |
| 582 | // use either sext or zext (i.e. we are dealing with equality predicate), |
| 583 | // then prefer zext as a more canonical form. |
| 584 | // TODO: If we see a signed comparison which can be turned into unsigned, |
| 585 | // we can do it here for canonicalization purposes. |
Max Kazantsev | 4d98051 | 2018-07-27 09:43:39 +0000 | [diff] [blame^] | 586 | ICmpInst::Predicate Pred = ICI->getPredicate(); |
| 587 | if (CanUseZExt(ICI)) { |
Max Kazantsev | 37da433 | 2018-06-19 04:48:34 +0000 | [diff] [blame] | 588 | assert(DoesZExtCollapse && "Unprofitable zext?"); |
| 589 | Ext = new ZExtInst(Op1, IVTy, "zext", ICI); |
Max Kazantsev | 4d98051 | 2018-07-27 09:43:39 +0000 | [diff] [blame^] | 590 | Pred = ICmpInst::getUnsignedPredicate(Pred); |
Max Kazantsev | 37da433 | 2018-06-19 04:48:34 +0000 | [diff] [blame] | 591 | } else { |
| 592 | assert(DoesSExtCollapse && "Unprofitable sext?"); |
| 593 | Ext = new SExtInst(Op1, IVTy, "sext", ICI); |
Max Kazantsev | 4d98051 | 2018-07-27 09:43:39 +0000 | [diff] [blame^] | 594 | assert(Pred == ICmpInst::getSignedPredicate(Pred) && "Must be signed!"); |
Max Kazantsev | 37da433 | 2018-06-19 04:48:34 +0000 | [diff] [blame] | 595 | } |
| 596 | bool Changed; |
| 597 | L->makeLoopInvariant(Ext, Changed); |
| 598 | (void)Changed; |
Max Kazantsev | 4d98051 | 2018-07-27 09:43:39 +0000 | [diff] [blame^] | 599 | ICmpInst *NewICI = new ICmpInst(ICI, Pred, IV, Ext); |
Max Kazantsev | 37da433 | 2018-06-19 04:48:34 +0000 | [diff] [blame] | 600 | ICI->replaceAllUsesWith(NewICI); |
| 601 | DeadInsts.emplace_back(ICI); |
| 602 | } |
| 603 | |
| 604 | // Trunc no longer needed. |
| 605 | TI->replaceAllUsesWith(UndefValue::get(TI->getType())); |
| 606 | DeadInsts.emplace_back(TI); |
| 607 | return true; |
| 608 | } |
| 609 | |
Sanjoy Das | 088bb0e | 2015-10-06 21:44:39 +0000 | [diff] [blame] | 610 | /// Eliminate an operation that consumes a simple IV and has no observable |
| 611 | /// side-effect given the range of IV values. IVOperand is guaranteed SCEVable, |
| 612 | /// but UseInst may not be. |
Andrew Trick | 3ec331e | 2011-08-10 03:46:27 +0000 | [diff] [blame] | 613 | bool SimplifyIndvar::eliminateIVUser(Instruction *UseInst, |
| 614 | Instruction *IVOperand) { |
| 615 | if (ICmpInst *ICmp = dyn_cast<ICmpInst>(UseInst)) { |
| 616 | eliminateIVComparison(ICmp, IVOperand); |
| 617 | return true; |
| 618 | } |
Hongbin Zheng | bfd7c38 | 2017-03-30 21:56:56 +0000 | [diff] [blame] | 619 | if (BinaryOperator *Bin = dyn_cast<BinaryOperator>(UseInst)) { |
| 620 | bool IsSRem = Bin->getOpcode() == Instruction::SRem; |
| 621 | if (IsSRem || Bin->getOpcode() == Instruction::URem) { |
Hongbin Zheng | f0093e4 | 2017-09-25 17:39:40 +0000 | [diff] [blame] | 622 | simplifyIVRemainder(Bin, IVOperand, IsSRem); |
Andrew Trick | 3ec331e | 2011-08-10 03:46:27 +0000 | [diff] [blame] | 623 | return true; |
| 624 | } |
Hongbin Zheng | bfd7c38 | 2017-03-30 21:56:56 +0000 | [diff] [blame] | 625 | |
| 626 | if (Bin->getOpcode() == Instruction::SDiv) |
| 627 | return eliminateSDiv(Bin); |
Andrew Trick | 3ec331e | 2011-08-10 03:46:27 +0000 | [diff] [blame] | 628 | } |
| 629 | |
Sanjoy Das | ae09b3c | 2016-05-29 00:36:25 +0000 | [diff] [blame] | 630 | if (auto *CI = dyn_cast<CallInst>(UseInst)) |
| 631 | if (eliminateOverflowIntrinsic(CI)) |
| 632 | return true; |
| 633 | |
Max Kazantsev | 37da433 | 2018-06-19 04:48:34 +0000 | [diff] [blame] | 634 | if (auto *TI = dyn_cast<TruncInst>(UseInst)) |
| 635 | if (eliminateTrunc(TI)) |
| 636 | return true; |
| 637 | |
Sanjoy Das | 088bb0e | 2015-10-06 21:44:39 +0000 | [diff] [blame] | 638 | if (eliminateIdentitySCEV(UseInst, IVOperand)) |
| 639 | return true; |
| 640 | |
| 641 | return false; |
| 642 | } |
| 643 | |
Hongbin Zheng | d36f2030 | 2017-10-12 02:54:11 +0000 | [diff] [blame] | 644 | static Instruction *GetLoopInvariantInsertPosition(Loop *L, Instruction *Hint) { |
| 645 | if (auto *BB = L->getLoopPreheader()) |
| 646 | return BB->getTerminator(); |
| 647 | |
| 648 | return Hint; |
| 649 | } |
| 650 | |
| 651 | /// Replace the UseInst with a constant if possible. |
| 652 | bool SimplifyIndvar::replaceIVUserWithLoopInvariant(Instruction *I) { |
Hongbin Zheng | d1b7b2e | 2017-09-27 03:11:46 +0000 | [diff] [blame] | 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 | |
Hongbin Zheng | d36f2030 | 2017-10-12 02:54:11 +0000 | [diff] [blame] | 659 | if (!SE->isLoopInvariant(S, L)) |
Hongbin Zheng | c8abdf5 | 2017-09-29 16:32:12 +0000 | [diff] [blame] | 660 | return false; |
Hongbin Zheng | d1b7b2e | 2017-09-27 03:11:46 +0000 | [diff] [blame] | 661 | |
Hongbin Zheng | d36f2030 | 2017-10-12 02:54:11 +0000 | [diff] [blame] | 662 | // Do not generate something ridiculous even if S is loop invariant. |
| 663 | if (Rewriter.isHighCostExpansion(S, L, I)) |
Hongbin Zheng | c8abdf5 | 2017-09-29 16:32:12 +0000 | [diff] [blame] | 664 | return false; |
| 665 | |
Hongbin Zheng | d36f2030 | 2017-10-12 02:54:11 +0000 | [diff] [blame] | 666 | auto *IP = GetLoopInvariantInsertPosition(L, I); |
| 667 | auto *Invariant = Rewriter.expandCodeFor(S, I->getType(), IP); |
| 668 | |
| 669 | I->replaceAllUsesWith(Invariant); |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 670 | LLVM_DEBUG(dbgs() << "INDVARS: Replace IV user: " << *I |
| 671 | << " with loop invariant: " << *S << '\n'); |
Hongbin Zheng | c8abdf5 | 2017-09-29 16:32:12 +0000 | [diff] [blame] | 672 | ++NumFoldedUser; |
| 673 | Changed = true; |
| 674 | DeadInsts.emplace_back(I); |
| 675 | return true; |
Hongbin Zheng | d1b7b2e | 2017-09-27 03:11:46 +0000 | [diff] [blame] | 676 | } |
| 677 | |
Sanjoy Das | 088bb0e | 2015-10-06 21:44:39 +0000 | [diff] [blame] | 678 | /// Eliminate any operation that SCEV can prove is an identity function. |
| 679 | bool SimplifyIndvar::eliminateIdentitySCEV(Instruction *UseInst, |
| 680 | Instruction *IVOperand) { |
Andrew Trick | 3ec331e | 2011-08-10 03:46:27 +0000 | [diff] [blame] | 681 | if (!SE->isSCEVable(UseInst->getType()) || |
| 682 | (UseInst->getType() != IVOperand->getType()) || |
| 683 | (SE->getSCEV(UseInst) != SE->getSCEV(IVOperand))) |
| 684 | return false; |
| 685 | |
Sanjoy Das | 5c8bead | 2015-10-06 21:44:49 +0000 | [diff] [blame] | 686 | // getSCEV(X) == getSCEV(Y) does not guarantee that X and Y are related in the |
| 687 | // dominator tree, even if X is an operand to Y. For instance, in |
| 688 | // |
| 689 | // %iv = phi i32 {0,+,1} |
| 690 | // br %cond, label %left, label %merge |
| 691 | // |
| 692 | // left: |
| 693 | // %X = add i32 %iv, 0 |
| 694 | // br label %merge |
| 695 | // |
| 696 | // merge: |
| 697 | // %M = phi (%X, %iv) |
| 698 | // |
| 699 | // getSCEV(%M) == getSCEV(%X) == {0,+,1}, but %X does not dominate %M, and |
| 700 | // %M.replaceAllUsesWith(%X) would be incorrect. |
| 701 | |
| 702 | if (isa<PHINode>(UseInst)) |
| 703 | // If UseInst is not a PHI node then we know that IVOperand dominates |
| 704 | // UseInst directly from the legality of SSA. |
| 705 | if (!DT || !DT->dominates(IVOperand, UseInst)) |
| 706 | return false; |
| 707 | |
Sanjoy Das | 0015e5a | 2015-10-07 17:38:31 +0000 | [diff] [blame] | 708 | if (!LI->replacementPreservesLCSSAForm(UseInst, IVOperand)) |
| 709 | return false; |
| 710 | |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 711 | LLVM_DEBUG(dbgs() << "INDVARS: Eliminated identity: " << *UseInst << '\n'); |
Andrew Trick | 3ec331e | 2011-08-10 03:46:27 +0000 | [diff] [blame] | 712 | |
| 713 | UseInst->replaceAllUsesWith(IVOperand); |
| 714 | ++NumElimIdentity; |
| 715 | Changed = true; |
Benjamin Kramer | f5e2fc4 | 2015-05-29 19:43:39 +0000 | [diff] [blame] | 716 | DeadInsts.emplace_back(UseInst); |
Andrew Trick | 3ec331e | 2011-08-10 03:46:27 +0000 | [diff] [blame] | 717 | return true; |
| 718 | } |
| 719 | |
Sanjoy Das | 7c0ce26 | 2015-01-06 19:02:56 +0000 | [diff] [blame] | 720 | /// Annotate BO with nsw / nuw if it provably does not signed-overflow / |
| 721 | /// unsigned-overflow. Returns true if anything changed, false otherwise. |
| 722 | bool SimplifyIndvar::strengthenOverflowingOperation(BinaryOperator *BO, |
| 723 | Value *IVOperand) { |
| 724 | |
Sanjoy Das | a5397c0 | 2015-03-04 22:24:23 +0000 | [diff] [blame] | 725 | // 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] | 726 | if (BO->hasNoUnsignedWrap() && BO->hasNoSignedWrap()) |
| 727 | return false; |
| 728 | |
Sanjoy Das | a5397c0 | 2015-03-04 22:24:23 +0000 | [diff] [blame] | 729 | const SCEV *(ScalarEvolution::*GetExprForBO)(const SCEV *, const SCEV *, |
Max Kazantsev | dc80366 | 2017-06-15 11:48:21 +0000 | [diff] [blame] | 730 | SCEV::NoWrapFlags, unsigned); |
Sanjoy Das | a5397c0 | 2015-03-04 22:24:23 +0000 | [diff] [blame] | 731 | switch (BO->getOpcode()) { |
| 732 | default: |
Sanjoy Das | 7c0ce26 | 2015-01-06 19:02:56 +0000 | [diff] [blame] | 733 | return false; |
| 734 | |
Sanjoy Das | a5397c0 | 2015-03-04 22:24:23 +0000 | [diff] [blame] | 735 | case Instruction::Add: |
| 736 | GetExprForBO = &ScalarEvolution::getAddExpr; |
| 737 | break; |
Sanjoy Das | 7c0ce26 | 2015-01-06 19:02:56 +0000 | [diff] [blame] | 738 | |
Sanjoy Das | a5397c0 | 2015-03-04 22:24:23 +0000 | [diff] [blame] | 739 | case Instruction::Sub: |
| 740 | GetExprForBO = &ScalarEvolution::getMinusSCEV; |
| 741 | break; |
Sanjoy Das | 7c0ce26 | 2015-01-06 19:02:56 +0000 | [diff] [blame] | 742 | |
Sanjoy Das | a5397c0 | 2015-03-04 22:24:23 +0000 | [diff] [blame] | 743 | case Instruction::Mul: |
| 744 | GetExprForBO = &ScalarEvolution::getMulExpr; |
| 745 | break; |
| 746 | } |
Sanjoy Das | 7c0ce26 | 2015-01-06 19:02:56 +0000 | [diff] [blame] | 747 | |
Sanjoy Das | a5397c0 | 2015-03-04 22:24:23 +0000 | [diff] [blame] | 748 | unsigned BitWidth = cast<IntegerType>(BO->getType())->getBitWidth(); |
| 749 | Type *WideTy = IntegerType::get(BO->getContext(), BitWidth * 2); |
| 750 | const SCEV *LHS = SE->getSCEV(BO->getOperand(0)); |
| 751 | const SCEV *RHS = SE->getSCEV(BO->getOperand(1)); |
Sanjoy Das | 7c0ce26 | 2015-01-06 19:02:56 +0000 | [diff] [blame] | 752 | |
Sanjoy Das | a5397c0 | 2015-03-04 22:24:23 +0000 | [diff] [blame] | 753 | bool Changed = false; |
Sanjoy Das | 7c0ce26 | 2015-01-06 19:02:56 +0000 | [diff] [blame] | 754 | |
Sanjoy Das | a5397c0 | 2015-03-04 22:24:23 +0000 | [diff] [blame] | 755 | if (!BO->hasNoUnsignedWrap()) { |
| 756 | const SCEV *ExtendAfterOp = SE->getZeroExtendExpr(SE->getSCEV(BO), WideTy); |
| 757 | const SCEV *OpAfterExtend = (SE->*GetExprForBO)( |
| 758 | SE->getZeroExtendExpr(LHS, WideTy), SE->getZeroExtendExpr(RHS, WideTy), |
Max Kazantsev | dc80366 | 2017-06-15 11:48:21 +0000 | [diff] [blame] | 759 | SCEV::FlagAnyWrap, 0u); |
Sanjoy Das | a5397c0 | 2015-03-04 22:24:23 +0000 | [diff] [blame] | 760 | if (ExtendAfterOp == OpAfterExtend) { |
| 761 | BO->setHasNoUnsignedWrap(); |
| 762 | SE->forgetValue(BO); |
| 763 | Changed = true; |
Sanjoy Das | 7c0ce26 | 2015-01-06 19:02:56 +0000 | [diff] [blame] | 764 | } |
| 765 | } |
| 766 | |
Sanjoy Das | a5397c0 | 2015-03-04 22:24:23 +0000 | [diff] [blame] | 767 | if (!BO->hasNoSignedWrap()) { |
| 768 | const SCEV *ExtendAfterOp = SE->getSignExtendExpr(SE->getSCEV(BO), WideTy); |
| 769 | const SCEV *OpAfterExtend = (SE->*GetExprForBO)( |
| 770 | SE->getSignExtendExpr(LHS, WideTy), SE->getSignExtendExpr(RHS, WideTy), |
Max Kazantsev | dc80366 | 2017-06-15 11:48:21 +0000 | [diff] [blame] | 771 | SCEV::FlagAnyWrap, 0u); |
Sanjoy Das | a5397c0 | 2015-03-04 22:24:23 +0000 | [diff] [blame] | 772 | if (ExtendAfterOp == OpAfterExtend) { |
| 773 | BO->setHasNoSignedWrap(); |
| 774 | SE->forgetValue(BO); |
Sanjoy Das | 7c0ce26 | 2015-01-06 19:02:56 +0000 | [diff] [blame] | 775 | Changed = true; |
| 776 | } |
| 777 | } |
| 778 | |
| 779 | return Changed; |
| 780 | } |
| 781 | |
David Green | b26a0a4 | 2017-07-05 13:25:58 +0000 | [diff] [blame] | 782 | /// Annotate the Shr in (X << IVOperand) >> C as exact using the |
| 783 | /// information from the IV's range. Returns true if anything changed, false |
| 784 | /// otherwise. |
| 785 | bool SimplifyIndvar::strengthenRightShift(BinaryOperator *BO, |
| 786 | Value *IVOperand) { |
| 787 | using namespace llvm::PatternMatch; |
| 788 | |
| 789 | if (BO->getOpcode() == Instruction::Shl) { |
| 790 | bool Changed = false; |
| 791 | ConstantRange IVRange = SE->getUnsignedRange(SE->getSCEV(IVOperand)); |
| 792 | for (auto *U : BO->users()) { |
| 793 | const APInt *C; |
| 794 | if (match(U, |
| 795 | m_AShr(m_Shl(m_Value(), m_Specific(IVOperand)), m_APInt(C))) || |
| 796 | match(U, |
| 797 | m_LShr(m_Shl(m_Value(), m_Specific(IVOperand)), m_APInt(C)))) { |
| 798 | BinaryOperator *Shr = cast<BinaryOperator>(U); |
| 799 | if (!Shr->isExact() && IVRange.getUnsignedMin().uge(*C)) { |
| 800 | Shr->setIsExact(true); |
| 801 | Changed = true; |
| 802 | } |
| 803 | } |
| 804 | } |
| 805 | return Changed; |
| 806 | } |
| 807 | |
| 808 | return false; |
| 809 | } |
| 810 | |
Sanjay Patel | 7777b50 | 2014-11-12 18:07:42 +0000 | [diff] [blame] | 811 | /// Add all uses of Def to the current IV's worklist. |
Andrew Trick | 3ec331e | 2011-08-10 03:46:27 +0000 | [diff] [blame] | 812 | static void pushIVUsers( |
Hongbin Zheng | d36f2030 | 2017-10-12 02:54:11 +0000 | [diff] [blame] | 813 | Instruction *Def, Loop *L, |
Andrew Trick | 3ec331e | 2011-08-10 03:46:27 +0000 | [diff] [blame] | 814 | SmallPtrSet<Instruction*,16> &Simplified, |
| 815 | SmallVectorImpl< std::pair<Instruction*,Instruction*> > &SimpleIVUsers) { |
| 816 | |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 817 | for (User *U : Def->users()) { |
| 818 | Instruction *UI = cast<Instruction>(U); |
Andrew Trick | 3ec331e | 2011-08-10 03:46:27 +0000 | [diff] [blame] | 819 | |
| 820 | // Avoid infinite or exponential worklist processing. |
| 821 | // Also ensure unique worklist users. |
| 822 | // If Def is a LoopPhi, it may not be in the Simplified set, so check for |
| 823 | // self edges first. |
Hongbin Zheng | d36f2030 | 2017-10-12 02:54:11 +0000 | [diff] [blame] | 824 | if (UI == Def) |
| 825 | continue; |
| 826 | |
| 827 | // Only change the current Loop, do not change the other parts (e.g. other |
| 828 | // Loops). |
| 829 | if (!L->contains(UI)) |
| 830 | continue; |
| 831 | |
| 832 | // Do not push the same instruction more than once. |
| 833 | if (!Simplified.insert(UI).second) |
| 834 | continue; |
| 835 | |
| 836 | SimpleIVUsers.push_back(std::make_pair(UI, Def)); |
Andrew Trick | 3ec331e | 2011-08-10 03:46:27 +0000 | [diff] [blame] | 837 | } |
| 838 | } |
| 839 | |
Sanjay Patel | 7777b50 | 2014-11-12 18:07:42 +0000 | [diff] [blame] | 840 | /// Return true if this instruction generates a simple SCEV |
Andrew Trick | 3ec331e | 2011-08-10 03:46:27 +0000 | [diff] [blame] | 841 | /// expression in terms of that IV. |
| 842 | /// |
Andrew Trick | 6dbb060 | 2011-08-10 18:07:05 +0000 | [diff] [blame] | 843 | /// This is similar to IVUsers' isInteresting() but processes each instruction |
Andrew Trick | 3ec331e | 2011-08-10 03:46:27 +0000 | [diff] [blame] | 844 | /// non-recursively when the operand is already known to be a simpleIVUser. |
| 845 | /// |
| 846 | static bool isSimpleIVUser(Instruction *I, const Loop *L, ScalarEvolution *SE) { |
| 847 | if (!SE->isSCEVable(I->getType())) |
| 848 | return false; |
| 849 | |
| 850 | // Get the symbolic expression for this instruction. |
| 851 | const SCEV *S = SE->getSCEV(I); |
| 852 | |
| 853 | // Only consider affine recurrences. |
| 854 | const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(S); |
| 855 | if (AR && AR->getLoop() == L) |
| 856 | return true; |
| 857 | |
| 858 | return false; |
| 859 | } |
| 860 | |
Sanjay Patel | 7777b50 | 2014-11-12 18:07:42 +0000 | [diff] [blame] | 861 | /// Iteratively perform simplification on a worklist of users |
Andrew Trick | 3ec331e | 2011-08-10 03:46:27 +0000 | [diff] [blame] | 862 | /// of the specified induction variable. Each successive simplification may push |
| 863 | /// more users which may themselves be candidates for simplification. |
| 864 | /// |
| 865 | /// This algorithm does not require IVUsers analysis. Instead, it simplifies |
| 866 | /// instructions in-place during analysis. Rather than rewriting induction |
| 867 | /// variables bottom-up from their users, it transforms a chain of IVUsers |
Benjamin Kramer | df005cb | 2015-08-08 18:27:36 +0000 | [diff] [blame] | 868 | /// top-down, updating the IR only when it encounters a clear optimization |
| 869 | /// opportunity. |
Andrew Trick | 3ec331e | 2011-08-10 03:46:27 +0000 | [diff] [blame] | 870 | /// |
| 871 | /// Once DisableIVRewrite is default, LSR will be the only client of IVUsers. |
| 872 | /// |
| 873 | void SimplifyIndvar::simplifyUsers(PHINode *CurrIV, IVVisitor *V) { |
Andrew Trick | 7251e41 | 2011-09-19 17:54:39 +0000 | [diff] [blame] | 874 | if (!SE->isSCEVable(CurrIV->getType())) |
| 875 | return; |
| 876 | |
Andrew Trick | 3ec331e | 2011-08-10 03:46:27 +0000 | [diff] [blame] | 877 | // Instructions processed by SimplifyIndvar for CurrIV. |
| 878 | SmallPtrSet<Instruction*,16> Simplified; |
| 879 | |
| 880 | // Use-def pairs if IV users waiting to be processed for CurrIV. |
| 881 | SmallVector<std::pair<Instruction*, Instruction*>, 8> SimpleIVUsers; |
| 882 | |
| 883 | // Push users of the current LoopPhi. In rare cases, pushIVUsers may be |
| 884 | // called multiple times for the same LoopPhi. This is the proper thing to |
| 885 | // do for loop header phis that use each other. |
Hongbin Zheng | d36f2030 | 2017-10-12 02:54:11 +0000 | [diff] [blame] | 886 | pushIVUsers(CurrIV, L, Simplified, SimpleIVUsers); |
Andrew Trick | 3ec331e | 2011-08-10 03:46:27 +0000 | [diff] [blame] | 887 | |
| 888 | while (!SimpleIVUsers.empty()) { |
| 889 | std::pair<Instruction*, Instruction*> UseOper = |
| 890 | SimpleIVUsers.pop_back_val(); |
Andrew Trick | 0ba77a0 | 2013-12-23 23:31:49 +0000 | [diff] [blame] | 891 | Instruction *UseInst = UseOper.first; |
| 892 | |
Max Kazantsev | 0ed7962 | 2018-06-13 02:25:32 +0000 | [diff] [blame] | 893 | // If a user of the IndVar is trivially dead, we prefer just to mark it dead |
| 894 | // rather than try to do some complex analysis or transformation (such as |
| 895 | // widening) basing on it. |
| 896 | // TODO: Propagate TLI and pass it here to handle more cases. |
| 897 | if (isInstructionTriviallyDead(UseInst, /* TLI */ nullptr)) { |
| 898 | DeadInsts.emplace_back(UseInst); |
| 899 | continue; |
| 900 | } |
| 901 | |
Andrew Trick | 3ec331e | 2011-08-10 03:46:27 +0000 | [diff] [blame] | 902 | // Bypass back edges to avoid extra work. |
Andrew Trick | 0ba77a0 | 2013-12-23 23:31:49 +0000 | [diff] [blame] | 903 | if (UseInst == CurrIV) continue; |
| 904 | |
Hongbin Zheng | d36f2030 | 2017-10-12 02:54:11 +0000 | [diff] [blame] | 905 | // Try to replace UseInst with a loop invariant before any other |
| 906 | // simplifications. |
| 907 | if (replaceIVUserWithLoopInvariant(UseInst)) |
Hongbin Zheng | d1b7b2e | 2017-09-27 03:11:46 +0000 | [diff] [blame] | 908 | continue; |
| 909 | |
Andrew Trick | 74664d5 | 2011-08-10 04:01:31 +0000 | [diff] [blame] | 910 | Instruction *IVOperand = UseOper.second; |
| 911 | for (unsigned N = 0; IVOperand; ++N) { |
| 912 | assert(N <= Simplified.size() && "runaway iteration"); |
Andrew Trick | 3ec331e | 2011-08-10 03:46:27 +0000 | [diff] [blame] | 913 | |
Max Kazantsev | b4b2cce | 2018-06-07 08:47:19 +0000 | [diff] [blame] | 914 | Value *NewOper = foldIVUser(UseInst, IVOperand); |
Andrew Trick | 74664d5 | 2011-08-10 04:01:31 +0000 | [diff] [blame] | 915 | if (!NewOper) |
| 916 | break; // done folding |
| 917 | IVOperand = dyn_cast<Instruction>(NewOper); |
| 918 | } |
| 919 | if (!IVOperand) |
| 920 | continue; |
| 921 | |
Max Kazantsev | b4b2cce | 2018-06-07 08:47:19 +0000 | [diff] [blame] | 922 | if (eliminateIVUser(UseInst, IVOperand)) { |
Hongbin Zheng | d36f2030 | 2017-10-12 02:54:11 +0000 | [diff] [blame] | 923 | pushIVUsers(IVOperand, L, Simplified, SimpleIVUsers); |
Andrew Trick | 3ec331e | 2011-08-10 03:46:27 +0000 | [diff] [blame] | 924 | continue; |
| 925 | } |
Sanjoy Das | 7c0ce26 | 2015-01-06 19:02:56 +0000 | [diff] [blame] | 926 | |
Max Kazantsev | b4b2cce | 2018-06-07 08:47:19 +0000 | [diff] [blame] | 927 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(UseInst)) { |
David Green | b26a0a4 | 2017-07-05 13:25:58 +0000 | [diff] [blame] | 928 | if ((isa<OverflowingBinaryOperator>(BO) && |
| 929 | strengthenOverflowingOperation(BO, IVOperand)) || |
| 930 | (isa<ShlOperator>(BO) && strengthenRightShift(BO, IVOperand))) { |
Sanjoy Das | 7c0ce26 | 2015-01-06 19:02:56 +0000 | [diff] [blame] | 931 | // re-queue uses of the now modified binary operator and fall |
| 932 | // through to the checks that remain. |
Hongbin Zheng | d36f2030 | 2017-10-12 02:54:11 +0000 | [diff] [blame] | 933 | pushIVUsers(IVOperand, L, Simplified, SimpleIVUsers); |
Sanjoy Das | 7c0ce26 | 2015-01-06 19:02:56 +0000 | [diff] [blame] | 934 | } |
| 935 | } |
| 936 | |
Max Kazantsev | b4b2cce | 2018-06-07 08:47:19 +0000 | [diff] [blame] | 937 | CastInst *Cast = dyn_cast<CastInst>(UseInst); |
Andrew Trick | 3ec331e | 2011-08-10 03:46:27 +0000 | [diff] [blame] | 938 | if (V && Cast) { |
| 939 | V->visitCast(Cast); |
| 940 | continue; |
| 941 | } |
Max Kazantsev | b4b2cce | 2018-06-07 08:47:19 +0000 | [diff] [blame] | 942 | if (isSimpleIVUser(UseInst, L, SE)) { |
| 943 | pushIVUsers(UseInst, L, Simplified, SimpleIVUsers); |
Andrew Trick | 3ec331e | 2011-08-10 03:46:27 +0000 | [diff] [blame] | 944 | } |
| 945 | } |
| 946 | } |
| 947 | |
| 948 | namespace llvm { |
| 949 | |
David Blaikie | a379b181 | 2011-12-20 02:50:00 +0000 | [diff] [blame] | 950 | void IVVisitor::anchor() { } |
| 951 | |
Sanjay Patel | 7777b50 | 2014-11-12 18:07:42 +0000 | [diff] [blame] | 952 | /// Simplify instructions that use this induction variable |
Andrew Trick | 3ec331e | 2011-08-10 03:46:27 +0000 | [diff] [blame] | 953 | /// by using ScalarEvolution to analyze the IV's recurrence. |
Sanjoy Das | 5c8bead | 2015-10-06 21:44:49 +0000 | [diff] [blame] | 954 | bool simplifyUsersOfIV(PHINode *CurrIV, ScalarEvolution *SE, DominatorTree *DT, |
Sanjoy Das | e6bca0e | 2017-05-01 17:07:49 +0000 | [diff] [blame] | 955 | LoopInfo *LI, SmallVectorImpl<WeakTrackingVH> &Dead, |
Hongbin Zheng | d36f2030 | 2017-10-12 02:54:11 +0000 | [diff] [blame] | 956 | SCEVExpander &Rewriter, IVVisitor *V) { |
| 957 | SimplifyIndvar SIV(LI->getLoopFor(CurrIV->getParent()), SE, DT, LI, Rewriter, |
| 958 | Dead); |
Andrew Trick | 3ec331e | 2011-08-10 03:46:27 +0000 | [diff] [blame] | 959 | SIV.simplifyUsers(CurrIV, V); |
| 960 | return SIV.hasChanged(); |
| 961 | } |
| 962 | |
Sanjay Patel | 7777b50 | 2014-11-12 18:07:42 +0000 | [diff] [blame] | 963 | /// Simplify users of induction variables within this |
Andrew Trick | 3ec331e | 2011-08-10 03:46:27 +0000 | [diff] [blame] | 964 | /// loop. This does not actually change or add IVs. |
Sanjoy Das | 5c8bead | 2015-10-06 21:44:49 +0000 | [diff] [blame] | 965 | bool simplifyLoopIVs(Loop *L, ScalarEvolution *SE, DominatorTree *DT, |
Sanjoy Das | e6bca0e | 2017-05-01 17:07:49 +0000 | [diff] [blame] | 966 | LoopInfo *LI, SmallVectorImpl<WeakTrackingVH> &Dead) { |
Hongbin Zheng | d36f2030 | 2017-10-12 02:54:11 +0000 | [diff] [blame] | 967 | SCEVExpander Rewriter(*SE, SE->getDataLayout(), "indvars"); |
| 968 | #ifndef NDEBUG |
| 969 | Rewriter.setDebugType(DEBUG_TYPE); |
| 970 | #endif |
Andrew Trick | 3ec331e | 2011-08-10 03:46:27 +0000 | [diff] [blame] | 971 | bool Changed = false; |
| 972 | for (BasicBlock::iterator I = L->getHeader()->begin(); isa<PHINode>(I); ++I) { |
Hongbin Zheng | d36f2030 | 2017-10-12 02:54:11 +0000 | [diff] [blame] | 973 | Changed |= simplifyUsersOfIV(cast<PHINode>(I), SE, DT, LI, Dead, Rewriter); |
Andrew Trick | 3ec331e | 2011-08-10 03:46:27 +0000 | [diff] [blame] | 974 | } |
| 975 | return Changed; |
| 976 | } |
| 977 | |
Andrew Trick | 3ec331e | 2011-08-10 03:46:27 +0000 | [diff] [blame] | 978 | } // namespace llvm |