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