Eugene Zelenko | 57bd5a0 | 2017-10-27 01:09:08 +0000 | [diff] [blame] | 1 | //===- StraightLineStrengthReduce.cpp - -----------------------------------===// |
Jingyue Wu | d7966ff | 2015-02-03 19:37:06 +0000 | [diff] [blame] | 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 |
Jingyue Wu | d7966ff | 2015-02-03 19:37:06 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | // This file implements straight-line strength reduction (SLSR). Unlike loop |
| 10 | // strength reduction, this algorithm is designed to reduce arithmetic |
| 11 | // redundancy in straight-line code instead of loops. It has proven to be |
| 12 | // effective in simplifying arithmetic statements derived from an unrolled loop. |
| 13 | // It can also simplify the logic of SeparateConstOffsetFromGEP. |
| 14 | // |
| 15 | // There are many optimizations we can perform in the domain of SLSR. This file |
| 16 | // for now contains only an initial step. Specifically, we look for strength |
Jingyue Wu | 43885eb | 2015-04-15 16:46:13 +0000 | [diff] [blame] | 17 | // reduction candidates in the following forms: |
Jingyue Wu | d7966ff | 2015-02-03 19:37:06 +0000 | [diff] [blame] | 18 | // |
Jingyue Wu | 43885eb | 2015-04-15 16:46:13 +0000 | [diff] [blame] | 19 | // Form 1: B + i * S |
| 20 | // Form 2: (B + i) * S |
| 21 | // Form 3: &B[i * S] |
Jingyue Wu | d7966ff | 2015-02-03 19:37:06 +0000 | [diff] [blame] | 22 | // |
Jingyue Wu | 177a815 | 2015-03-26 16:49:24 +0000 | [diff] [blame] | 23 | // where S is an integer variable, and i is a constant integer. If we found two |
Jingyue Wu | 43885eb | 2015-04-15 16:46:13 +0000 | [diff] [blame] | 24 | // candidates S1 and S2 in the same form and S1 dominates S2, we may rewrite S2 |
| 25 | // in a simpler way with respect to S1. For example, |
| 26 | // |
| 27 | // S1: X = B + i * S |
| 28 | // S2: Y = B + i' * S => X + (i' - i) * S |
Jingyue Wu | d7966ff | 2015-02-03 19:37:06 +0000 | [diff] [blame] | 29 | // |
Jingyue Wu | 177a815 | 2015-03-26 16:49:24 +0000 | [diff] [blame] | 30 | // S1: X = (B + i) * S |
Jingyue Wu | 43885eb | 2015-04-15 16:46:13 +0000 | [diff] [blame] | 31 | // S2: Y = (B + i') * S => X + (i' - i) * S |
Jingyue Wu | 177a815 | 2015-03-26 16:49:24 +0000 | [diff] [blame] | 32 | // |
| 33 | // S1: X = &B[i * S] |
Jingyue Wu | 43885eb | 2015-04-15 16:46:13 +0000 | [diff] [blame] | 34 | // S2: Y = &B[i' * S] => &X[(i' - i) * S] |
Jingyue Wu | d7966ff | 2015-02-03 19:37:06 +0000 | [diff] [blame] | 35 | // |
Jingyue Wu | 43885eb | 2015-04-15 16:46:13 +0000 | [diff] [blame] | 36 | // Note: (i' - i) * S is folded to the extent possible. |
Jingyue Wu | d7966ff | 2015-02-03 19:37:06 +0000 | [diff] [blame] | 37 | // |
Jingyue Wu | 43885eb | 2015-04-15 16:46:13 +0000 | [diff] [blame] | 38 | // This rewriting is in general a good idea. The code patterns we focus on |
| 39 | // usually come from loop unrolling, so (i' - i) * S is likely the same |
| 40 | // across iterations and can be reused. When that happens, the optimized form |
| 41 | // takes only one add starting from the second iteration. |
Jingyue Wu | d7966ff | 2015-02-03 19:37:06 +0000 | [diff] [blame] | 42 | // |
Jingyue Wu | 43885eb | 2015-04-15 16:46:13 +0000 | [diff] [blame] | 43 | // When such rewriting is possible, we call S1 a "basis" of S2. When S2 has |
| 44 | // multiple bases, we choose to rewrite S2 with respect to its "immediate" |
| 45 | // basis, the basis that is the closest ancestor in the dominator tree. |
Jingyue Wu | d7966ff | 2015-02-03 19:37:06 +0000 | [diff] [blame] | 46 | // |
| 47 | // TODO: |
| 48 | // |
Jingyue Wu | d7966ff | 2015-02-03 19:37:06 +0000 | [diff] [blame] | 49 | // - Floating point arithmetics when fast math is enabled. |
| 50 | // |
| 51 | // - SLSR may decrease ILP at the architecture level. Targets that are very |
| 52 | // sensitive to ILP may want to disable it. Having SLSR to consider ILP is |
| 53 | // left as future work. |
Jingyue Wu | 43885eb | 2015-04-15 16:46:13 +0000 | [diff] [blame] | 54 | // |
| 55 | // - When (i' - i) is constant but i and i' are not, we could still perform |
| 56 | // SLSR. |
Eugene Zelenko | 57bd5a0 | 2017-10-27 01:09:08 +0000 | [diff] [blame] | 57 | |
| 58 | #include "llvm/ADT/APInt.h" |
| 59 | #include "llvm/ADT/DepthFirstIterator.h" |
| 60 | #include "llvm/ADT/SmallVector.h" |
Jingyue Wu | 177a815 | 2015-03-26 16:49:24 +0000 | [diff] [blame] | 61 | #include "llvm/Analysis/ScalarEvolution.h" |
| 62 | #include "llvm/Analysis/TargetTransformInfo.h" |
Jingyue Wu | 80a96d29 | 2015-05-15 17:07:48 +0000 | [diff] [blame] | 63 | #include "llvm/Analysis/ValueTracking.h" |
Eugene Zelenko | 57bd5a0 | 2017-10-27 01:09:08 +0000 | [diff] [blame] | 64 | #include "llvm/IR/Constants.h" |
Jingyue Wu | 177a815 | 2015-03-26 16:49:24 +0000 | [diff] [blame] | 65 | #include "llvm/IR/DataLayout.h" |
Eugene Zelenko | 57bd5a0 | 2017-10-27 01:09:08 +0000 | [diff] [blame] | 66 | #include "llvm/IR/DerivedTypes.h" |
Jingyue Wu | d7966ff | 2015-02-03 19:37:06 +0000 | [diff] [blame] | 67 | #include "llvm/IR/Dominators.h" |
Eugene Zelenko | 57bd5a0 | 2017-10-27 01:09:08 +0000 | [diff] [blame] | 68 | #include "llvm/IR/GetElementPtrTypeIterator.h" |
Jingyue Wu | d7966ff | 2015-02-03 19:37:06 +0000 | [diff] [blame] | 69 | #include "llvm/IR/IRBuilder.h" |
Eugene Zelenko | 57bd5a0 | 2017-10-27 01:09:08 +0000 | [diff] [blame] | 70 | #include "llvm/IR/InstrTypes.h" |
| 71 | #include "llvm/IR/Instruction.h" |
| 72 | #include "llvm/IR/Instructions.h" |
Jingyue Wu | d7966ff | 2015-02-03 19:37:06 +0000 | [diff] [blame] | 73 | #include "llvm/IR/Module.h" |
Eugene Zelenko | 57bd5a0 | 2017-10-27 01:09:08 +0000 | [diff] [blame] | 74 | #include "llvm/IR/Operator.h" |
Jingyue Wu | d7966ff | 2015-02-03 19:37:06 +0000 | [diff] [blame] | 75 | #include "llvm/IR/PatternMatch.h" |
Eugene Zelenko | 57bd5a0 | 2017-10-27 01:09:08 +0000 | [diff] [blame] | 76 | #include "llvm/IR/Type.h" |
| 77 | #include "llvm/IR/Value.h" |
Reid Kleckner | 05da2fe | 2019-11-13 13:15:01 -0800 | [diff] [blame^] | 78 | #include "llvm/InitializePasses.h" |
Eugene Zelenko | 57bd5a0 | 2017-10-27 01:09:08 +0000 | [diff] [blame] | 79 | #include "llvm/Pass.h" |
| 80 | #include "llvm/Support/Casting.h" |
| 81 | #include "llvm/Support/ErrorHandling.h" |
Jingyue Wu | d7966ff | 2015-02-03 19:37:06 +0000 | [diff] [blame] | 82 | #include "llvm/Transforms/Scalar.h" |
Reid Kleckner | 05da2fe | 2019-11-13 13:15:01 -0800 | [diff] [blame^] | 83 | #include "llvm/Transforms/Utils/Local.h" |
Eugene Zelenko | 57bd5a0 | 2017-10-27 01:09:08 +0000 | [diff] [blame] | 84 | #include <cassert> |
| 85 | #include <cstdint> |
| 86 | #include <limits> |
Duncan P. N. Exon Smith | 8b4e4af | 2016-09-11 21:29:34 +0000 | [diff] [blame] | 87 | #include <list> |
Duncan P. N. Exon Smith | 077f5b4 | 2016-09-11 21:04:36 +0000 | [diff] [blame] | 88 | #include <vector> |
Jingyue Wu | d7966ff | 2015-02-03 19:37:06 +0000 | [diff] [blame] | 89 | |
| 90 | using namespace llvm; |
| 91 | using namespace PatternMatch; |
| 92 | |
Eugene Zelenko | 57bd5a0 | 2017-10-27 01:09:08 +0000 | [diff] [blame] | 93 | static const unsigned UnknownAddressSpace = |
| 94 | std::numeric_limits<unsigned>::max(); |
Jingyue Wu | d7966ff | 2015-02-03 19:37:06 +0000 | [diff] [blame] | 95 | |
Eugene Zelenko | 57bd5a0 | 2017-10-27 01:09:08 +0000 | [diff] [blame] | 96 | namespace { |
Matt Arsenault | ba437c6 | 2016-04-27 00:32:09 +0000 | [diff] [blame] | 97 | |
Jingyue Wu | d7966ff | 2015-02-03 19:37:06 +0000 | [diff] [blame] | 98 | class StraightLineStrengthReduce : public FunctionPass { |
Jingyue Wu | 177a815 | 2015-03-26 16:49:24 +0000 | [diff] [blame] | 99 | public: |
Jingyue Wu | 43885eb | 2015-04-15 16:46:13 +0000 | [diff] [blame] | 100 | // SLSR candidate. Such a candidate must be in one of the forms described in |
| 101 | // the header comments. |
Duncan P. N. Exon Smith | 8b4e4af | 2016-09-11 21:29:34 +0000 | [diff] [blame] | 102 | struct Candidate { |
Jingyue Wu | 177a815 | 2015-03-26 16:49:24 +0000 | [diff] [blame] | 103 | enum Kind { |
| 104 | Invalid, // reserved for the default constructor |
Jingyue Wu | 43885eb | 2015-04-15 16:46:13 +0000 | [diff] [blame] | 105 | Add, // B + i * S |
Jingyue Wu | 177a815 | 2015-03-26 16:49:24 +0000 | [diff] [blame] | 106 | Mul, // (B + i) * S |
| 107 | GEP, // &B[..][i * S][..] |
| 108 | }; |
| 109 | |
Eugene Zelenko | 57bd5a0 | 2017-10-27 01:09:08 +0000 | [diff] [blame] | 110 | Candidate() = default; |
Jingyue Wu | 177a815 | 2015-03-26 16:49:24 +0000 | [diff] [blame] | 111 | Candidate(Kind CT, const SCEV *B, ConstantInt *Idx, Value *S, |
| 112 | Instruction *I) |
Eugene Zelenko | 57bd5a0 | 2017-10-27 01:09:08 +0000 | [diff] [blame] | 113 | : CandidateKind(CT), Base(B), Index(Idx), Stride(S), Ins(I) {} |
| 114 | |
| 115 | Kind CandidateKind = Invalid; |
| 116 | |
| 117 | const SCEV *Base = nullptr; |
| 118 | |
Jingyue Wu | 43885eb | 2015-04-15 16:46:13 +0000 | [diff] [blame] | 119 | // Note that Index and Stride of a GEP candidate do not necessarily have the |
| 120 | // same integer type. In that case, during rewriting, Stride will be |
Jingyue Wu | 177a815 | 2015-03-26 16:49:24 +0000 | [diff] [blame] | 121 | // sign-extended or truncated to Index's type. |
Eugene Zelenko | 57bd5a0 | 2017-10-27 01:09:08 +0000 | [diff] [blame] | 122 | ConstantInt *Index = nullptr; |
| 123 | |
| 124 | Value *Stride = nullptr; |
| 125 | |
Jingyue Wu | d7966ff | 2015-02-03 19:37:06 +0000 | [diff] [blame] | 126 | // The instruction this candidate corresponds to. It helps us to rewrite a |
| 127 | // candidate with respect to its immediate basis. Note that one instruction |
Jingyue Wu | 43885eb | 2015-04-15 16:46:13 +0000 | [diff] [blame] | 128 | // can correspond to multiple candidates depending on how you associate the |
Jingyue Wu | d7966ff | 2015-02-03 19:37:06 +0000 | [diff] [blame] | 129 | // expression. For instance, |
| 130 | // |
| 131 | // (a + 1) * (b + 2) |
| 132 | // |
| 133 | // can be treated as |
| 134 | // |
| 135 | // <Base: a, Index: 1, Stride: b + 2> |
| 136 | // |
| 137 | // or |
| 138 | // |
| 139 | // <Base: b, Index: 2, Stride: a + 1> |
Eugene Zelenko | 57bd5a0 | 2017-10-27 01:09:08 +0000 | [diff] [blame] | 140 | Instruction *Ins = nullptr; |
| 141 | |
Jingyue Wu | d7966ff | 2015-02-03 19:37:06 +0000 | [diff] [blame] | 142 | // Points to the immediate basis of this candidate, or nullptr if we cannot |
| 143 | // find any basis for this candidate. |
Eugene Zelenko | 57bd5a0 | 2017-10-27 01:09:08 +0000 | [diff] [blame] | 144 | Candidate *Basis = nullptr; |
Jingyue Wu | d7966ff | 2015-02-03 19:37:06 +0000 | [diff] [blame] | 145 | }; |
| 146 | |
| 147 | static char ID; |
| 148 | |
Eugene Zelenko | 57bd5a0 | 2017-10-27 01:09:08 +0000 | [diff] [blame] | 149 | StraightLineStrengthReduce() : FunctionPass(ID) { |
Jingyue Wu | d7966ff | 2015-02-03 19:37:06 +0000 | [diff] [blame] | 150 | initializeStraightLineStrengthReducePass(*PassRegistry::getPassRegistry()); |
| 151 | } |
| 152 | |
| 153 | void getAnalysisUsage(AnalysisUsage &AU) const override { |
| 154 | AU.addRequired<DominatorTreeWrapperPass>(); |
Chandler Carruth | 2f1fd16 | 2015-08-17 02:08:17 +0000 | [diff] [blame] | 155 | AU.addRequired<ScalarEvolutionWrapperPass>(); |
Jingyue Wu | 177a815 | 2015-03-26 16:49:24 +0000 | [diff] [blame] | 156 | AU.addRequired<TargetTransformInfoWrapperPass>(); |
Jingyue Wu | d7966ff | 2015-02-03 19:37:06 +0000 | [diff] [blame] | 157 | // We do not modify the shape of the CFG. |
| 158 | AU.setPreservesCFG(); |
| 159 | } |
| 160 | |
Jingyue Wu | 177a815 | 2015-03-26 16:49:24 +0000 | [diff] [blame] | 161 | bool doInitialization(Module &M) override { |
| 162 | DL = &M.getDataLayout(); |
| 163 | return false; |
| 164 | } |
| 165 | |
Jingyue Wu | d7966ff | 2015-02-03 19:37:06 +0000 | [diff] [blame] | 166 | bool runOnFunction(Function &F) override; |
| 167 | |
Jingyue Wu | 177a815 | 2015-03-26 16:49:24 +0000 | [diff] [blame] | 168 | private: |
Jingyue Wu | d7966ff | 2015-02-03 19:37:06 +0000 | [diff] [blame] | 169 | // Returns true if Basis is a basis for C, i.e., Basis dominates C and they |
| 170 | // share the same base and stride. |
| 171 | bool isBasisFor(const Candidate &Basis, const Candidate &C); |
Eugene Zelenko | 57bd5a0 | 2017-10-27 01:09:08 +0000 | [diff] [blame] | 172 | |
Jingyue Wu | 43885eb | 2015-04-15 16:46:13 +0000 | [diff] [blame] | 173 | // Returns whether the candidate can be folded into an addressing mode. |
| 174 | bool isFoldable(const Candidate &C, TargetTransformInfo *TTI, |
| 175 | const DataLayout *DL); |
Eugene Zelenko | 57bd5a0 | 2017-10-27 01:09:08 +0000 | [diff] [blame] | 176 | |
Jingyue Wu | 43885eb | 2015-04-15 16:46:13 +0000 | [diff] [blame] | 177 | // Returns true if C is already in a simplest form and not worth being |
| 178 | // rewritten. |
| 179 | bool isSimplestForm(const Candidate &C); |
Eugene Zelenko | 57bd5a0 | 2017-10-27 01:09:08 +0000 | [diff] [blame] | 180 | |
Jingyue Wu | d7966ff | 2015-02-03 19:37:06 +0000 | [diff] [blame] | 181 | // Checks whether I is in a candidate form. If so, adds all the matching forms |
| 182 | // to Candidates, and tries to find the immediate basis for each of them. |
Jingyue Wu | 43885eb | 2015-04-15 16:46:13 +0000 | [diff] [blame] | 183 | void allocateCandidatesAndFindBasis(Instruction *I); |
Eugene Zelenko | 57bd5a0 | 2017-10-27 01:09:08 +0000 | [diff] [blame] | 184 | |
Jingyue Wu | 43885eb | 2015-04-15 16:46:13 +0000 | [diff] [blame] | 185 | // Allocate candidates and find bases for Add instructions. |
| 186 | void allocateCandidatesAndFindBasisForAdd(Instruction *I); |
Eugene Zelenko | 57bd5a0 | 2017-10-27 01:09:08 +0000 | [diff] [blame] | 187 | |
Jingyue Wu | 43885eb | 2015-04-15 16:46:13 +0000 | [diff] [blame] | 188 | // Given I = LHS + RHS, factors RHS into i * S and makes (LHS + i * S) a |
| 189 | // candidate. |
| 190 | void allocateCandidatesAndFindBasisForAdd(Value *LHS, Value *RHS, |
| 191 | Instruction *I); |
Jingyue Wu | 177a815 | 2015-03-26 16:49:24 +0000 | [diff] [blame] | 192 | // Allocate candidates and find bases for Mul instructions. |
Jingyue Wu | 43885eb | 2015-04-15 16:46:13 +0000 | [diff] [blame] | 193 | void allocateCandidatesAndFindBasisForMul(Instruction *I); |
Eugene Zelenko | 57bd5a0 | 2017-10-27 01:09:08 +0000 | [diff] [blame] | 194 | |
Jingyue Wu | 177a815 | 2015-03-26 16:49:24 +0000 | [diff] [blame] | 195 | // Splits LHS into Base + Index and, if succeeds, calls |
Jingyue Wu | 43885eb | 2015-04-15 16:46:13 +0000 | [diff] [blame] | 196 | // allocateCandidatesAndFindBasis. |
| 197 | void allocateCandidatesAndFindBasisForMul(Value *LHS, Value *RHS, |
| 198 | Instruction *I); |
Eugene Zelenko | 57bd5a0 | 2017-10-27 01:09:08 +0000 | [diff] [blame] | 199 | |
Jingyue Wu | 177a815 | 2015-03-26 16:49:24 +0000 | [diff] [blame] | 200 | // Allocate candidates and find bases for GetElementPtr instructions. |
Jingyue Wu | 43885eb | 2015-04-15 16:46:13 +0000 | [diff] [blame] | 201 | void allocateCandidatesAndFindBasisForGEP(GetElementPtrInst *GEP); |
Eugene Zelenko | 57bd5a0 | 2017-10-27 01:09:08 +0000 | [diff] [blame] | 202 | |
Jingyue Wu | 177a815 | 2015-03-26 16:49:24 +0000 | [diff] [blame] | 203 | // A helper function that scales Idx with ElementSize before invoking |
Jingyue Wu | 43885eb | 2015-04-15 16:46:13 +0000 | [diff] [blame] | 204 | // allocateCandidatesAndFindBasis. |
| 205 | void allocateCandidatesAndFindBasisForGEP(const SCEV *B, ConstantInt *Idx, |
| 206 | Value *S, uint64_t ElementSize, |
| 207 | Instruction *I); |
Eugene Zelenko | 57bd5a0 | 2017-10-27 01:09:08 +0000 | [diff] [blame] | 208 | |
Jingyue Wu | 177a815 | 2015-03-26 16:49:24 +0000 | [diff] [blame] | 209 | // Adds the given form <CT, B, Idx, S> to Candidates, and finds its immediate |
| 210 | // basis. |
Jingyue Wu | 43885eb | 2015-04-15 16:46:13 +0000 | [diff] [blame] | 211 | void allocateCandidatesAndFindBasis(Candidate::Kind CT, const SCEV *B, |
| 212 | ConstantInt *Idx, Value *S, |
| 213 | Instruction *I); |
Eugene Zelenko | 57bd5a0 | 2017-10-27 01:09:08 +0000 | [diff] [blame] | 214 | |
Jingyue Wu | d7966ff | 2015-02-03 19:37:06 +0000 | [diff] [blame] | 215 | // Rewrites candidate C with respect to Basis. |
| 216 | void rewriteCandidateWithBasis(const Candidate &C, const Candidate &Basis); |
Eugene Zelenko | 57bd5a0 | 2017-10-27 01:09:08 +0000 | [diff] [blame] | 217 | |
Jingyue Wu | 177a815 | 2015-03-26 16:49:24 +0000 | [diff] [blame] | 218 | // A helper function that factors ArrayIdx to a product of a stride and a |
Jingyue Wu | 43885eb | 2015-04-15 16:46:13 +0000 | [diff] [blame] | 219 | // constant index, and invokes allocateCandidatesAndFindBasis with the |
Jingyue Wu | 177a815 | 2015-03-26 16:49:24 +0000 | [diff] [blame] | 220 | // factorings. |
| 221 | void factorArrayIndex(Value *ArrayIdx, const SCEV *Base, uint64_t ElementSize, |
| 222 | GetElementPtrInst *GEP); |
Eugene Zelenko | 57bd5a0 | 2017-10-27 01:09:08 +0000 | [diff] [blame] | 223 | |
Jingyue Wu | 177a815 | 2015-03-26 16:49:24 +0000 | [diff] [blame] | 224 | // Emit code that computes the "bump" from Basis to C. If the candidate is a |
| 225 | // GEP and the bump is not divisible by the element size of the GEP, this |
| 226 | // function sets the BumpWithUglyGEP flag to notify its caller to bump the |
| 227 | // basis using an ugly GEP. |
| 228 | static Value *emitBump(const Candidate &Basis, const Candidate &C, |
| 229 | IRBuilder<> &Builder, const DataLayout *DL, |
| 230 | bool &BumpWithUglyGEP); |
Jingyue Wu | d7966ff | 2015-02-03 19:37:06 +0000 | [diff] [blame] | 231 | |
Eugene Zelenko | 57bd5a0 | 2017-10-27 01:09:08 +0000 | [diff] [blame] | 232 | const DataLayout *DL = nullptr; |
| 233 | DominatorTree *DT = nullptr; |
Jingyue Wu | 177a815 | 2015-03-26 16:49:24 +0000 | [diff] [blame] | 234 | ScalarEvolution *SE; |
Eugene Zelenko | 57bd5a0 | 2017-10-27 01:09:08 +0000 | [diff] [blame] | 235 | TargetTransformInfo *TTI = nullptr; |
Duncan P. N. Exon Smith | 8b4e4af | 2016-09-11 21:29:34 +0000 | [diff] [blame] | 236 | std::list<Candidate> Candidates; |
Eugene Zelenko | 57bd5a0 | 2017-10-27 01:09:08 +0000 | [diff] [blame] | 237 | |
Jingyue Wu | d7966ff | 2015-02-03 19:37:06 +0000 | [diff] [blame] | 238 | // Temporarily holds all instructions that are unlinked (but not deleted) by |
| 239 | // rewriteCandidateWithBasis. These instructions will be actually removed |
| 240 | // after all rewriting finishes. |
Jingyue Wu | 43885eb | 2015-04-15 16:46:13 +0000 | [diff] [blame] | 241 | std::vector<Instruction *> UnlinkedInstructions; |
Jingyue Wu | d7966ff | 2015-02-03 19:37:06 +0000 | [diff] [blame] | 242 | }; |
Eugene Zelenko | 57bd5a0 | 2017-10-27 01:09:08 +0000 | [diff] [blame] | 243 | |
| 244 | } // end anonymous namespace |
Jingyue Wu | d7966ff | 2015-02-03 19:37:06 +0000 | [diff] [blame] | 245 | |
| 246 | char StraightLineStrengthReduce::ID = 0; |
Eugene Zelenko | 57bd5a0 | 2017-10-27 01:09:08 +0000 | [diff] [blame] | 247 | |
Jingyue Wu | d7966ff | 2015-02-03 19:37:06 +0000 | [diff] [blame] | 248 | INITIALIZE_PASS_BEGIN(StraightLineStrengthReduce, "slsr", |
| 249 | "Straight line strength reduction", false, false) |
| 250 | INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass) |
Chandler Carruth | 2f1fd16 | 2015-08-17 02:08:17 +0000 | [diff] [blame] | 251 | INITIALIZE_PASS_DEPENDENCY(ScalarEvolutionWrapperPass) |
Jingyue Wu | 177a815 | 2015-03-26 16:49:24 +0000 | [diff] [blame] | 252 | INITIALIZE_PASS_DEPENDENCY(TargetTransformInfoWrapperPass) |
Jingyue Wu | d7966ff | 2015-02-03 19:37:06 +0000 | [diff] [blame] | 253 | INITIALIZE_PASS_END(StraightLineStrengthReduce, "slsr", |
| 254 | "Straight line strength reduction", false, false) |
| 255 | |
| 256 | FunctionPass *llvm::createStraightLineStrengthReducePass() { |
| 257 | return new StraightLineStrengthReduce(); |
| 258 | } |
| 259 | |
| 260 | bool StraightLineStrengthReduce::isBasisFor(const Candidate &Basis, |
| 261 | const Candidate &C) { |
| 262 | return (Basis.Ins != C.Ins && // skip the same instruction |
Jingyue Wu | 3abde7b | 2015-06-28 17:45:05 +0000 | [diff] [blame] | 263 | // They must have the same type too. Basis.Base == C.Base doesn't |
| 264 | // guarantee their types are the same (PR23975). |
| 265 | Basis.Ins->getType() == C.Ins->getType() && |
Jingyue Wu | d7966ff | 2015-02-03 19:37:06 +0000 | [diff] [blame] | 266 | // Basis must dominate C in order to rewrite C with respect to Basis. |
| 267 | DT->dominates(Basis.Ins->getParent(), C.Ins->getParent()) && |
Jingyue Wu | 177a815 | 2015-03-26 16:49:24 +0000 | [diff] [blame] | 268 | // They share the same base, stride, and candidate kind. |
Jingyue Wu | 3abde7b | 2015-06-28 17:45:05 +0000 | [diff] [blame] | 269 | Basis.Base == C.Base && Basis.Stride == C.Stride && |
Jingyue Wu | 177a815 | 2015-03-26 16:49:24 +0000 | [diff] [blame] | 270 | Basis.CandidateKind == C.CandidateKind); |
| 271 | } |
| 272 | |
Jingyue Wu | 43885eb | 2015-04-15 16:46:13 +0000 | [diff] [blame] | 273 | static bool isGEPFoldable(GetElementPtrInst *GEP, |
Jingyue Wu | 15f3e82 | 2016-07-08 21:48:05 +0000 | [diff] [blame] | 274 | const TargetTransformInfo *TTI) { |
| 275 | SmallVector<const Value*, 4> Indices; |
| 276 | for (auto I = GEP->idx_begin(); I != GEP->idx_end(); ++I) |
| 277 | Indices.push_back(*I); |
Daniel Jasper | 3344a21 | 2017-10-13 14:04:21 +0000 | [diff] [blame] | 278 | return TTI->getGEPCost(GEP->getSourceElementType(), GEP->getPointerOperand(), |
Jingyue Wu | 15f3e82 | 2016-07-08 21:48:05 +0000 | [diff] [blame] | 279 | Indices) == TargetTransformInfo::TCC_Free; |
Jingyue Wu | d7966ff | 2015-02-03 19:37:06 +0000 | [diff] [blame] | 280 | } |
| 281 | |
Jingyue Wu | 43885eb | 2015-04-15 16:46:13 +0000 | [diff] [blame] | 282 | // Returns whether (Base + Index * Stride) can be folded to an addressing mode. |
| 283 | static bool isAddFoldable(const SCEV *Base, ConstantInt *Index, Value *Stride, |
| 284 | TargetTransformInfo *TTI) { |
Jingyue Wu | debce55 | 2016-07-09 19:13:18 +0000 | [diff] [blame] | 285 | // Index->getSExtValue() may crash if Index is wider than 64-bit. |
| 286 | return Index->getBitWidth() <= 64 && |
| 287 | TTI->isLegalAddressingMode(Base->getType(), nullptr, 0, true, |
Matt Arsenault | ba437c6 | 2016-04-27 00:32:09 +0000 | [diff] [blame] | 288 | Index->getSExtValue(), UnknownAddressSpace); |
Jingyue Wu | 43885eb | 2015-04-15 16:46:13 +0000 | [diff] [blame] | 289 | } |
| 290 | |
| 291 | bool StraightLineStrengthReduce::isFoldable(const Candidate &C, |
| 292 | TargetTransformInfo *TTI, |
| 293 | const DataLayout *DL) { |
| 294 | if (C.CandidateKind == Candidate::Add) |
| 295 | return isAddFoldable(C.Base, C.Index, C.Stride, TTI); |
| 296 | if (C.CandidateKind == Candidate::GEP) |
Jingyue Wu | 15f3e82 | 2016-07-08 21:48:05 +0000 | [diff] [blame] | 297 | return isGEPFoldable(cast<GetElementPtrInst>(C.Ins), TTI); |
Jingyue Wu | 43885eb | 2015-04-15 16:46:13 +0000 | [diff] [blame] | 298 | return false; |
| 299 | } |
| 300 | |
| 301 | // Returns true if GEP has zero or one non-zero index. |
| 302 | static bool hasOnlyOneNonZeroIndex(GetElementPtrInst *GEP) { |
| 303 | unsigned NumNonZeroIndices = 0; |
| 304 | for (auto I = GEP->idx_begin(); I != GEP->idx_end(); ++I) { |
| 305 | ConstantInt *ConstIdx = dyn_cast<ConstantInt>(*I); |
| 306 | if (ConstIdx == nullptr || !ConstIdx->isZero()) |
| 307 | ++NumNonZeroIndices; |
| 308 | } |
| 309 | return NumNonZeroIndices <= 1; |
| 310 | } |
| 311 | |
| 312 | bool StraightLineStrengthReduce::isSimplestForm(const Candidate &C) { |
| 313 | if (C.CandidateKind == Candidate::Add) { |
| 314 | // B + 1 * S or B + (-1) * S |
| 315 | return C.Index->isOne() || C.Index->isMinusOne(); |
| 316 | } |
| 317 | if (C.CandidateKind == Candidate::Mul) { |
| 318 | // (B + 0) * S |
| 319 | return C.Index->isZero(); |
| 320 | } |
| 321 | if (C.CandidateKind == Candidate::GEP) { |
| 322 | // (char*)B + S or (char*)B - S |
| 323 | return ((C.Index->isOne() || C.Index->isMinusOne()) && |
| 324 | hasOnlyOneNonZeroIndex(cast<GetElementPtrInst>(C.Ins))); |
| 325 | } |
| 326 | return false; |
| 327 | } |
| 328 | |
| 329 | // TODO: We currently implement an algorithm whose time complexity is linear in |
| 330 | // the number of existing candidates. However, we could do better by using |
| 331 | // ScopedHashTable. Specifically, while traversing the dominator tree, we could |
| 332 | // maintain all the candidates that dominate the basic block being traversed in |
| 333 | // a ScopedHashTable. This hash table is indexed by the base and the stride of |
| 334 | // a candidate. Therefore, finding the immediate basis of a candidate boils down |
| 335 | // to one hash-table look up. |
| 336 | void StraightLineStrengthReduce::allocateCandidatesAndFindBasis( |
Jingyue Wu | 177a815 | 2015-03-26 16:49:24 +0000 | [diff] [blame] | 337 | Candidate::Kind CT, const SCEV *B, ConstantInt *Idx, Value *S, |
| 338 | Instruction *I) { |
Jingyue Wu | 177a815 | 2015-03-26 16:49:24 +0000 | [diff] [blame] | 339 | Candidate C(CT, B, Idx, S, I); |
Jingyue Wu | 43885eb | 2015-04-15 16:46:13 +0000 | [diff] [blame] | 340 | // SLSR can complicate an instruction in two cases: |
| 341 | // |
| 342 | // 1. If we can fold I into an addressing mode, computing I is likely free or |
| 343 | // takes only one instruction. |
| 344 | // |
| 345 | // 2. I is already in a simplest form. For example, when |
| 346 | // X = B + 8 * S |
| 347 | // Y = B + S, |
| 348 | // rewriting Y to X - 7 * S is probably a bad idea. |
| 349 | // |
| 350 | // In the above cases, we still add I to the candidate list so that I can be |
| 351 | // the basis of other candidates, but we leave I's basis blank so that I |
| 352 | // won't be rewritten. |
| 353 | if (!isFoldable(C, TTI, DL) && !isSimplestForm(C)) { |
| 354 | // Try to compute the immediate basis of C. |
| 355 | unsigned NumIterations = 0; |
| 356 | // Limit the scan radius to avoid running in quadratice time. |
| 357 | static const unsigned MaxNumIterations = 50; |
| 358 | for (auto Basis = Candidates.rbegin(); |
| 359 | Basis != Candidates.rend() && NumIterations < MaxNumIterations; |
| 360 | ++Basis, ++NumIterations) { |
| 361 | if (isBasisFor(*Basis, C)) { |
| 362 | C.Basis = &(*Basis); |
| 363 | break; |
| 364 | } |
Jingyue Wu | d7966ff | 2015-02-03 19:37:06 +0000 | [diff] [blame] | 365 | } |
| 366 | } |
| 367 | // Regardless of whether we find a basis for C, we need to push C to the |
Jingyue Wu | 43885eb | 2015-04-15 16:46:13 +0000 | [diff] [blame] | 368 | // candidate list so that it can be the basis of other candidates. |
Jingyue Wu | d7966ff | 2015-02-03 19:37:06 +0000 | [diff] [blame] | 369 | Candidates.push_back(C); |
| 370 | } |
| 371 | |
Jingyue Wu | 43885eb | 2015-04-15 16:46:13 +0000 | [diff] [blame] | 372 | void StraightLineStrengthReduce::allocateCandidatesAndFindBasis( |
| 373 | Instruction *I) { |
Jingyue Wu | 177a815 | 2015-03-26 16:49:24 +0000 | [diff] [blame] | 374 | switch (I->getOpcode()) { |
Jingyue Wu | 43885eb | 2015-04-15 16:46:13 +0000 | [diff] [blame] | 375 | case Instruction::Add: |
| 376 | allocateCandidatesAndFindBasisForAdd(I); |
| 377 | break; |
Jingyue Wu | 177a815 | 2015-03-26 16:49:24 +0000 | [diff] [blame] | 378 | case Instruction::Mul: |
Jingyue Wu | 43885eb | 2015-04-15 16:46:13 +0000 | [diff] [blame] | 379 | allocateCandidatesAndFindBasisForMul(I); |
Jingyue Wu | 177a815 | 2015-03-26 16:49:24 +0000 | [diff] [blame] | 380 | break; |
| 381 | case Instruction::GetElementPtr: |
Jingyue Wu | 43885eb | 2015-04-15 16:46:13 +0000 | [diff] [blame] | 382 | allocateCandidatesAndFindBasisForGEP(cast<GetElementPtrInst>(I)); |
Jingyue Wu | 177a815 | 2015-03-26 16:49:24 +0000 | [diff] [blame] | 383 | break; |
| 384 | } |
| 385 | } |
| 386 | |
Jingyue Wu | 43885eb | 2015-04-15 16:46:13 +0000 | [diff] [blame] | 387 | void StraightLineStrengthReduce::allocateCandidatesAndFindBasisForAdd( |
| 388 | Instruction *I) { |
| 389 | // Try matching B + i * S. |
| 390 | if (!isa<IntegerType>(I->getType())) |
| 391 | return; |
| 392 | |
| 393 | assert(I->getNumOperands() == 2 && "isn't I an add?"); |
| 394 | Value *LHS = I->getOperand(0), *RHS = I->getOperand(1); |
| 395 | allocateCandidatesAndFindBasisForAdd(LHS, RHS, I); |
| 396 | if (LHS != RHS) |
| 397 | allocateCandidatesAndFindBasisForAdd(RHS, LHS, I); |
| 398 | } |
| 399 | |
| 400 | void StraightLineStrengthReduce::allocateCandidatesAndFindBasisForAdd( |
| 401 | Value *LHS, Value *RHS, Instruction *I) { |
| 402 | Value *S = nullptr; |
| 403 | ConstantInt *Idx = nullptr; |
| 404 | if (match(RHS, m_Mul(m_Value(S), m_ConstantInt(Idx)))) { |
| 405 | // I = LHS + RHS = LHS + Idx * S |
| 406 | allocateCandidatesAndFindBasis(Candidate::Add, SE->getSCEV(LHS), Idx, S, I); |
| 407 | } else if (match(RHS, m_Shl(m_Value(S), m_ConstantInt(Idx)))) { |
| 408 | // I = LHS + RHS = LHS + (S << Idx) = LHS + S * (1 << Idx) |
| 409 | APInt One(Idx->getBitWidth(), 1); |
| 410 | Idx = ConstantInt::get(Idx->getContext(), One << Idx->getValue()); |
| 411 | allocateCandidatesAndFindBasis(Candidate::Add, SE->getSCEV(LHS), Idx, S, I); |
| 412 | } else { |
| 413 | // At least, I = LHS + 1 * RHS |
| 414 | ConstantInt *One = ConstantInt::get(cast<IntegerType>(I->getType()), 1); |
| 415 | allocateCandidatesAndFindBasis(Candidate::Add, SE->getSCEV(LHS), One, RHS, |
| 416 | I); |
| 417 | } |
| 418 | } |
| 419 | |
Jingyue Wu | 80a96d29 | 2015-05-15 17:07:48 +0000 | [diff] [blame] | 420 | // Returns true if A matches B + C where C is constant. |
| 421 | static bool matchesAdd(Value *A, Value *&B, ConstantInt *&C) { |
| 422 | return (match(A, m_Add(m_Value(B), m_ConstantInt(C))) || |
| 423 | match(A, m_Add(m_ConstantInt(C), m_Value(B)))); |
| 424 | } |
| 425 | |
| 426 | // Returns true if A matches B | C where C is constant. |
| 427 | static bool matchesOr(Value *A, Value *&B, ConstantInt *&C) { |
| 428 | return (match(A, m_Or(m_Value(B), m_ConstantInt(C))) || |
| 429 | match(A, m_Or(m_ConstantInt(C), m_Value(B)))); |
| 430 | } |
| 431 | |
Jingyue Wu | 43885eb | 2015-04-15 16:46:13 +0000 | [diff] [blame] | 432 | void StraightLineStrengthReduce::allocateCandidatesAndFindBasisForMul( |
Jingyue Wu | 177a815 | 2015-03-26 16:49:24 +0000 | [diff] [blame] | 433 | Value *LHS, Value *RHS, Instruction *I) { |
Jingyue Wu | d7966ff | 2015-02-03 19:37:06 +0000 | [diff] [blame] | 434 | Value *B = nullptr; |
| 435 | ConstantInt *Idx = nullptr; |
Jingyue Wu | 80a96d29 | 2015-05-15 17:07:48 +0000 | [diff] [blame] | 436 | if (matchesAdd(LHS, B, Idx)) { |
Jingyue Wu | 177a815 | 2015-03-26 16:49:24 +0000 | [diff] [blame] | 437 | // If LHS is in the form of "Base + Index", then I is in the form of |
| 438 | // "(Base + Index) * RHS". |
Jingyue Wu | 43885eb | 2015-04-15 16:46:13 +0000 | [diff] [blame] | 439 | allocateCandidatesAndFindBasis(Candidate::Mul, SE->getSCEV(B), Idx, RHS, I); |
Jingyue Wu | 80a96d29 | 2015-05-15 17:07:48 +0000 | [diff] [blame] | 440 | } else if (matchesOr(LHS, B, Idx) && haveNoCommonBitsSet(B, Idx, *DL)) { |
| 441 | // If LHS is in the form of "Base | Index" and Base and Index have no common |
| 442 | // bits set, then |
| 443 | // Base | Index = Base + Index |
| 444 | // and I is thus in the form of "(Base + Index) * RHS". |
| 445 | allocateCandidatesAndFindBasis(Candidate::Mul, SE->getSCEV(B), Idx, RHS, I); |
Jingyue Wu | 177a815 | 2015-03-26 16:49:24 +0000 | [diff] [blame] | 446 | } else { |
| 447 | // Otherwise, at least try the form (LHS + 0) * RHS. |
| 448 | ConstantInt *Zero = ConstantInt::get(cast<IntegerType>(I->getType()), 0); |
Jingyue Wu | 43885eb | 2015-04-15 16:46:13 +0000 | [diff] [blame] | 449 | allocateCandidatesAndFindBasis(Candidate::Mul, SE->getSCEV(LHS), Zero, RHS, |
Jingyue Wu | 80a96d29 | 2015-05-15 17:07:48 +0000 | [diff] [blame] | 450 | I); |
Jingyue Wu | d7966ff | 2015-02-03 19:37:06 +0000 | [diff] [blame] | 451 | } |
| 452 | } |
| 453 | |
Jingyue Wu | 43885eb | 2015-04-15 16:46:13 +0000 | [diff] [blame] | 454 | void StraightLineStrengthReduce::allocateCandidatesAndFindBasisForMul( |
Jingyue Wu | 177a815 | 2015-03-26 16:49:24 +0000 | [diff] [blame] | 455 | Instruction *I) { |
| 456 | // Try matching (B + i) * S. |
| 457 | // TODO: we could extend SLSR to float and vector types. |
| 458 | if (!isa<IntegerType>(I->getType())) |
| 459 | return; |
| 460 | |
Jingyue Wu | 43885eb | 2015-04-15 16:46:13 +0000 | [diff] [blame] | 461 | assert(I->getNumOperands() == 2 && "isn't I a mul?"); |
Jingyue Wu | 177a815 | 2015-03-26 16:49:24 +0000 | [diff] [blame] | 462 | Value *LHS = I->getOperand(0), *RHS = I->getOperand(1); |
Jingyue Wu | 43885eb | 2015-04-15 16:46:13 +0000 | [diff] [blame] | 463 | allocateCandidatesAndFindBasisForMul(LHS, RHS, I); |
Jingyue Wu | 177a815 | 2015-03-26 16:49:24 +0000 | [diff] [blame] | 464 | if (LHS != RHS) { |
| 465 | // Symmetrically, try to split RHS to Base + Index. |
Jingyue Wu | 43885eb | 2015-04-15 16:46:13 +0000 | [diff] [blame] | 466 | allocateCandidatesAndFindBasisForMul(RHS, LHS, I); |
Jingyue Wu | 177a815 | 2015-03-26 16:49:24 +0000 | [diff] [blame] | 467 | } |
| 468 | } |
| 469 | |
Jingyue Wu | 43885eb | 2015-04-15 16:46:13 +0000 | [diff] [blame] | 470 | void StraightLineStrengthReduce::allocateCandidatesAndFindBasisForGEP( |
Jingyue Wu | 177a815 | 2015-03-26 16:49:24 +0000 | [diff] [blame] | 471 | const SCEV *B, ConstantInt *Idx, Value *S, uint64_t ElementSize, |
| 472 | Instruction *I) { |
Jingyue Wu | 99a6bed | 2015-04-02 21:18:32 +0000 | [diff] [blame] | 473 | // I = B + sext(Idx *nsw S) * ElementSize |
| 474 | // = B + (sext(Idx) * sext(S)) * ElementSize |
Jingyue Wu | 177a815 | 2015-03-26 16:49:24 +0000 | [diff] [blame] | 475 | // = B + (sext(Idx) * ElementSize) * sext(S) |
| 476 | // Casting to IntegerType is safe because we skipped vector GEPs. |
| 477 | IntegerType *IntPtrTy = cast<IntegerType>(DL->getIntPtrType(I->getType())); |
| 478 | ConstantInt *ScaledIdx = ConstantInt::get( |
| 479 | IntPtrTy, Idx->getSExtValue() * (int64_t)ElementSize, true); |
Jingyue Wu | 43885eb | 2015-04-15 16:46:13 +0000 | [diff] [blame] | 480 | allocateCandidatesAndFindBasis(Candidate::GEP, B, ScaledIdx, S, I); |
Jingyue Wu | 177a815 | 2015-03-26 16:49:24 +0000 | [diff] [blame] | 481 | } |
| 482 | |
| 483 | void StraightLineStrengthReduce::factorArrayIndex(Value *ArrayIdx, |
| 484 | const SCEV *Base, |
| 485 | uint64_t ElementSize, |
| 486 | GetElementPtrInst *GEP) { |
Jingyue Wu | 43885eb | 2015-04-15 16:46:13 +0000 | [diff] [blame] | 487 | // At least, ArrayIdx = ArrayIdx *nsw 1. |
| 488 | allocateCandidatesAndFindBasisForGEP( |
Jingyue Wu | 177a815 | 2015-03-26 16:49:24 +0000 | [diff] [blame] | 489 | Base, ConstantInt::get(cast<IntegerType>(ArrayIdx->getType()), 1), |
| 490 | ArrayIdx, ElementSize, GEP); |
| 491 | Value *LHS = nullptr; |
| 492 | ConstantInt *RHS = nullptr; |
Jingyue Wu | 177a815 | 2015-03-26 16:49:24 +0000 | [diff] [blame] | 493 | // One alternative is matching the SCEV of ArrayIdx instead of ArrayIdx |
| 494 | // itself. This would allow us to handle the shl case for free. However, |
| 495 | // matching SCEVs has two issues: |
| 496 | // |
| 497 | // 1. this would complicate rewriting because the rewriting procedure |
| 498 | // would have to translate SCEVs back to IR instructions. This translation |
| 499 | // is difficult when LHS is further evaluated to a composite SCEV. |
| 500 | // |
| 501 | // 2. ScalarEvolution is designed to be control-flow oblivious. It tends |
| 502 | // to strip nsw/nuw flags which are critical for SLSR to trace into |
| 503 | // sext'ed multiplication. |
| 504 | if (match(ArrayIdx, m_NSWMul(m_Value(LHS), m_ConstantInt(RHS)))) { |
| 505 | // SLSR is currently unsafe if i * S may overflow. |
Jingyue Wu | 99a6bed | 2015-04-02 21:18:32 +0000 | [diff] [blame] | 506 | // GEP = Base + sext(LHS *nsw RHS) * ElementSize |
Jingyue Wu | 43885eb | 2015-04-15 16:46:13 +0000 | [diff] [blame] | 507 | allocateCandidatesAndFindBasisForGEP(Base, RHS, LHS, ElementSize, GEP); |
Jingyue Wu | 96d7400 | 2015-04-06 17:15:48 +0000 | [diff] [blame] | 508 | } else if (match(ArrayIdx, m_NSWShl(m_Value(LHS), m_ConstantInt(RHS)))) { |
| 509 | // GEP = Base + sext(LHS <<nsw RHS) * ElementSize |
| 510 | // = Base + sext(LHS *nsw (1 << RHS)) * ElementSize |
| 511 | APInt One(RHS->getBitWidth(), 1); |
| 512 | ConstantInt *PowerOf2 = |
| 513 | ConstantInt::get(RHS->getContext(), One << RHS->getValue()); |
Jingyue Wu | 43885eb | 2015-04-15 16:46:13 +0000 | [diff] [blame] | 514 | allocateCandidatesAndFindBasisForGEP(Base, PowerOf2, LHS, ElementSize, GEP); |
Jingyue Wu | 177a815 | 2015-03-26 16:49:24 +0000 | [diff] [blame] | 515 | } |
| 516 | } |
| 517 | |
Jingyue Wu | 43885eb | 2015-04-15 16:46:13 +0000 | [diff] [blame] | 518 | void StraightLineStrengthReduce::allocateCandidatesAndFindBasisForGEP( |
Jingyue Wu | 177a815 | 2015-03-26 16:49:24 +0000 | [diff] [blame] | 519 | GetElementPtrInst *GEP) { |
| 520 | // TODO: handle vector GEPs |
| 521 | if (GEP->getType()->isVectorTy()) |
| 522 | return; |
| 523 | |
Jingyue Wu | 2982d4d | 2015-05-18 17:03:25 +0000 | [diff] [blame] | 524 | SmallVector<const SCEV *, 4> IndexExprs; |
| 525 | for (auto I = GEP->idx_begin(); I != GEP->idx_end(); ++I) |
| 526 | IndexExprs.push_back(SE->getSCEV(*I)); |
Jingyue Wu | 177a815 | 2015-03-26 16:49:24 +0000 | [diff] [blame] | 527 | |
| 528 | gep_type_iterator GTI = gep_type_begin(GEP); |
Peter Collingbourne | ab85225b | 2016-12-02 02:24:42 +0000 | [diff] [blame] | 529 | for (unsigned I = 1, E = GEP->getNumOperands(); I != E; ++I, ++GTI) { |
| 530 | if (GTI.isStruct()) |
Jingyue Wu | 177a815 | 2015-03-26 16:49:24 +0000 | [diff] [blame] | 531 | continue; |
Jingyue Wu | 2982d4d | 2015-05-18 17:03:25 +0000 | [diff] [blame] | 532 | |
| 533 | const SCEV *OrigIndexExpr = IndexExprs[I - 1]; |
Sanjoy Das | 2aacc0e | 2015-09-23 01:59:04 +0000 | [diff] [blame] | 534 | IndexExprs[I - 1] = SE->getZero(OrigIndexExpr->getType()); |
Jingyue Wu | 2982d4d | 2015-05-18 17:03:25 +0000 | [diff] [blame] | 535 | |
| 536 | // The base of this candidate is GEP's base plus the offsets of all |
| 537 | // indices except this current one. |
Peter Collingbourne | 8dff039 | 2016-11-13 06:59:50 +0000 | [diff] [blame] | 538 | const SCEV *BaseExpr = SE->getGEPExpr(cast<GEPOperator>(GEP), IndexExprs); |
Jingyue Wu | 2982d4d | 2015-05-18 17:03:25 +0000 | [diff] [blame] | 539 | Value *ArrayIdx = GEP->getOperand(I); |
Peter Collingbourne | ab85225b | 2016-12-02 02:24:42 +0000 | [diff] [blame] | 540 | uint64_t ElementSize = DL->getTypeAllocSize(GTI.getIndexedType()); |
Jingyue Wu | debce55 | 2016-07-09 19:13:18 +0000 | [diff] [blame] | 541 | if (ArrayIdx->getType()->getIntegerBitWidth() <= |
Jingyue Wu | 641cfee | 2016-07-11 18:13:28 +0000 | [diff] [blame] | 542 | DL->getPointerSizeInBits(GEP->getAddressSpace())) { |
Jingyue Wu | debce55 | 2016-07-09 19:13:18 +0000 | [diff] [blame] | 543 | // Skip factoring if ArrayIdx is wider than the pointer size, because |
| 544 | // ArrayIdx is implicitly truncated to the pointer size. |
| 545 | factorArrayIndex(ArrayIdx, BaseExpr, ElementSize, GEP); |
| 546 | } |
Jingyue Wu | 177a815 | 2015-03-26 16:49:24 +0000 | [diff] [blame] | 547 | // When ArrayIdx is the sext of a value, we try to factor that value as |
| 548 | // well. Handling this case is important because array indices are |
| 549 | // typically sign-extended to the pointer size. |
| 550 | Value *TruncatedArrayIdx = nullptr; |
Jingyue Wu | debce55 | 2016-07-09 19:13:18 +0000 | [diff] [blame] | 551 | if (match(ArrayIdx, m_SExt(m_Value(TruncatedArrayIdx))) && |
| 552 | TruncatedArrayIdx->getType()->getIntegerBitWidth() <= |
Jingyue Wu | 641cfee | 2016-07-11 18:13:28 +0000 | [diff] [blame] | 553 | DL->getPointerSizeInBits(GEP->getAddressSpace())) { |
Jingyue Wu | debce55 | 2016-07-09 19:13:18 +0000 | [diff] [blame] | 554 | // Skip factoring if TruncatedArrayIdx is wider than the pointer size, |
| 555 | // because TruncatedArrayIdx is implicitly truncated to the pointer size. |
Jingyue Wu | 2982d4d | 2015-05-18 17:03:25 +0000 | [diff] [blame] | 556 | factorArrayIndex(TruncatedArrayIdx, BaseExpr, ElementSize, GEP); |
Jingyue Wu | debce55 | 2016-07-09 19:13:18 +0000 | [diff] [blame] | 557 | } |
Jingyue Wu | 2982d4d | 2015-05-18 17:03:25 +0000 | [diff] [blame] | 558 | |
| 559 | IndexExprs[I - 1] = OrigIndexExpr; |
Jingyue Wu | 177a815 | 2015-03-26 16:49:24 +0000 | [diff] [blame] | 560 | } |
| 561 | } |
| 562 | |
| 563 | // A helper function that unifies the bitwidth of A and B. |
| 564 | static void unifyBitWidth(APInt &A, APInt &B) { |
| 565 | if (A.getBitWidth() < B.getBitWidth()) |
| 566 | A = A.sext(B.getBitWidth()); |
| 567 | else if (A.getBitWidth() > B.getBitWidth()) |
| 568 | B = B.sext(A.getBitWidth()); |
| 569 | } |
| 570 | |
| 571 | Value *StraightLineStrengthReduce::emitBump(const Candidate &Basis, |
| 572 | const Candidate &C, |
| 573 | IRBuilder<> &Builder, |
| 574 | const DataLayout *DL, |
| 575 | bool &BumpWithUglyGEP) { |
| 576 | APInt Idx = C.Index->getValue(), BasisIdx = Basis.Index->getValue(); |
| 577 | unifyBitWidth(Idx, BasisIdx); |
| 578 | APInt IndexOffset = Idx - BasisIdx; |
| 579 | |
| 580 | BumpWithUglyGEP = false; |
| 581 | if (Basis.CandidateKind == Candidate::GEP) { |
| 582 | APInt ElementSize( |
| 583 | IndexOffset.getBitWidth(), |
Jingyue Wu | debce55 | 2016-07-09 19:13:18 +0000 | [diff] [blame] | 584 | DL->getTypeAllocSize( |
| 585 | cast<GetElementPtrInst>(Basis.Ins)->getResultElementType())); |
Jingyue Wu | 177a815 | 2015-03-26 16:49:24 +0000 | [diff] [blame] | 586 | APInt Q, R; |
| 587 | APInt::sdivrem(IndexOffset, ElementSize, Q, R); |
Jingyue Wu | debce55 | 2016-07-09 19:13:18 +0000 | [diff] [blame] | 588 | if (R == 0) |
Jingyue Wu | 177a815 | 2015-03-26 16:49:24 +0000 | [diff] [blame] | 589 | IndexOffset = Q; |
| 590 | else |
| 591 | BumpWithUglyGEP = true; |
| 592 | } |
Jingyue Wu | 43885eb | 2015-04-15 16:46:13 +0000 | [diff] [blame] | 593 | |
Jingyue Wu | 177a815 | 2015-03-26 16:49:24 +0000 | [diff] [blame] | 594 | // Compute Bump = C - Basis = (i' - i) * S. |
| 595 | // Common case 1: if (i' - i) is 1, Bump = S. |
Jingyue Wu | debce55 | 2016-07-09 19:13:18 +0000 | [diff] [blame] | 596 | if (IndexOffset == 1) |
Jingyue Wu | 177a815 | 2015-03-26 16:49:24 +0000 | [diff] [blame] | 597 | return C.Stride; |
| 598 | // Common case 2: if (i' - i) is -1, Bump = -S. |
Jingyue Wu | debce55 | 2016-07-09 19:13:18 +0000 | [diff] [blame] | 599 | if (IndexOffset.isAllOnesValue()) |
Jingyue Wu | 177a815 | 2015-03-26 16:49:24 +0000 | [diff] [blame] | 600 | return Builder.CreateNeg(C.Stride); |
Jingyue Wu | 43885eb | 2015-04-15 16:46:13 +0000 | [diff] [blame] | 601 | |
| 602 | // Otherwise, Bump = (i' - i) * sext/trunc(S). Note that (i' - i) and S may |
| 603 | // have different bit widths. |
| 604 | IntegerType *DeltaType = |
| 605 | IntegerType::get(Basis.Ins->getContext(), IndexOffset.getBitWidth()); |
| 606 | Value *ExtendedStride = Builder.CreateSExtOrTrunc(C.Stride, DeltaType); |
| 607 | if (IndexOffset.isPowerOf2()) { |
| 608 | // If (i' - i) is a power of 2, Bump = sext/trunc(S) << log(i' - i). |
| 609 | ConstantInt *Exponent = ConstantInt::get(DeltaType, IndexOffset.logBase2()); |
| 610 | return Builder.CreateShl(ExtendedStride, Exponent); |
| 611 | } |
| 612 | if ((-IndexOffset).isPowerOf2()) { |
| 613 | // If (i - i') is a power of 2, Bump = -sext/trunc(S) << log(i' - i). |
| 614 | ConstantInt *Exponent = |
| 615 | ConstantInt::get(DeltaType, (-IndexOffset).logBase2()); |
| 616 | return Builder.CreateNeg(Builder.CreateShl(ExtendedStride, Exponent)); |
| 617 | } |
| 618 | Constant *Delta = ConstantInt::get(DeltaType, IndexOffset); |
Jingyue Wu | 177a815 | 2015-03-26 16:49:24 +0000 | [diff] [blame] | 619 | return Builder.CreateMul(ExtendedStride, Delta); |
| 620 | } |
| 621 | |
Jingyue Wu | d7966ff | 2015-02-03 19:37:06 +0000 | [diff] [blame] | 622 | void StraightLineStrengthReduce::rewriteCandidateWithBasis( |
| 623 | const Candidate &C, const Candidate &Basis) { |
Jingyue Wu | 177a815 | 2015-03-26 16:49:24 +0000 | [diff] [blame] | 624 | assert(C.CandidateKind == Basis.CandidateKind && C.Base == Basis.Base && |
| 625 | C.Stride == Basis.Stride); |
Jingyue Wu | 43885eb | 2015-04-15 16:46:13 +0000 | [diff] [blame] | 626 | // We run rewriteCandidateWithBasis on all candidates in a post-order, so the |
| 627 | // basis of a candidate cannot be unlinked before the candidate. |
| 628 | assert(Basis.Ins->getParent() != nullptr && "the basis is unlinked"); |
Jingyue Wu | 177a815 | 2015-03-26 16:49:24 +0000 | [diff] [blame] | 629 | |
Jingyue Wu | d7966ff | 2015-02-03 19:37:06 +0000 | [diff] [blame] | 630 | // An instruction can correspond to multiple candidates. Therefore, instead of |
| 631 | // simply deleting an instruction when we rewrite it, we mark its parent as |
| 632 | // nullptr (i.e. unlink it) so that we can skip the candidates whose |
| 633 | // instruction is already rewritten. |
| 634 | if (!C.Ins->getParent()) |
| 635 | return; |
Jingyue Wu | 177a815 | 2015-03-26 16:49:24 +0000 | [diff] [blame] | 636 | |
Jingyue Wu | d7966ff | 2015-02-03 19:37:06 +0000 | [diff] [blame] | 637 | IRBuilder<> Builder(C.Ins); |
Jingyue Wu | 177a815 | 2015-03-26 16:49:24 +0000 | [diff] [blame] | 638 | bool BumpWithUglyGEP; |
| 639 | Value *Bump = emitBump(Basis, C, Builder, DL, BumpWithUglyGEP); |
| 640 | Value *Reduced = nullptr; // equivalent to but weaker than C.Ins |
| 641 | switch (C.CandidateKind) { |
Jingyue Wu | 43885eb | 2015-04-15 16:46:13 +0000 | [diff] [blame] | 642 | case Candidate::Add: |
Sanjay Patel | 5141435 | 2018-10-23 14:07:39 +0000 | [diff] [blame] | 643 | case Candidate::Mul: { |
Jingyue Wu | f1edf3e | 2015-04-21 19:56:18 +0000 | [diff] [blame] | 644 | // C = Basis + Bump |
Sanjay Patel | 5141435 | 2018-10-23 14:07:39 +0000 | [diff] [blame] | 645 | Value *NegBump; |
| 646 | if (match(Bump, m_Neg(m_Value(NegBump)))) { |
Jingyue Wu | f1edf3e | 2015-04-21 19:56:18 +0000 | [diff] [blame] | 647 | // If Bump is a neg instruction, emit C = Basis - (-Bump). |
Sanjay Patel | 5141435 | 2018-10-23 14:07:39 +0000 | [diff] [blame] | 648 | Reduced = Builder.CreateSub(Basis.Ins, NegBump); |
Jingyue Wu | f1edf3e | 2015-04-21 19:56:18 +0000 | [diff] [blame] | 649 | // We only use the negative argument of Bump, and Bump itself may be |
| 650 | // trivially dead. |
| 651 | RecursivelyDeleteTriviallyDeadInstructions(Bump); |
Jingyue Wu | 43885eb | 2015-04-15 16:46:13 +0000 | [diff] [blame] | 652 | } else { |
Jingyue Wu | a941129 | 2015-06-18 03:35:57 +0000 | [diff] [blame] | 653 | // It's tempting to preserve nsw on Bump and/or Reduced. However, it's |
| 654 | // usually unsound, e.g., |
| 655 | // |
| 656 | // X = (-2 +nsw 1) *nsw INT_MAX |
| 657 | // Y = (-2 +nsw 3) *nsw INT_MAX |
| 658 | // => |
| 659 | // Y = X + 2 * INT_MAX |
| 660 | // |
| 661 | // Neither + and * in the resultant expression are nsw. |
Jingyue Wu | 43885eb | 2015-04-15 16:46:13 +0000 | [diff] [blame] | 662 | Reduced = Builder.CreateAdd(Basis.Ins, Bump); |
| 663 | } |
Jingyue Wu | 177a815 | 2015-03-26 16:49:24 +0000 | [diff] [blame] | 664 | break; |
Sanjay Patel | 5141435 | 2018-10-23 14:07:39 +0000 | [diff] [blame] | 665 | } |
Jingyue Wu | 177a815 | 2015-03-26 16:49:24 +0000 | [diff] [blame] | 666 | case Candidate::GEP: |
| 667 | { |
| 668 | Type *IntPtrTy = DL->getIntPtrType(C.Ins->getType()); |
Jingyue Wu | 99a6bed | 2015-04-02 21:18:32 +0000 | [diff] [blame] | 669 | bool InBounds = cast<GetElementPtrInst>(C.Ins)->isInBounds(); |
Jingyue Wu | 177a815 | 2015-03-26 16:49:24 +0000 | [diff] [blame] | 670 | if (BumpWithUglyGEP) { |
| 671 | // C = (char *)Basis + Bump |
| 672 | unsigned AS = Basis.Ins->getType()->getPointerAddressSpace(); |
| 673 | Type *CharTy = Type::getInt8PtrTy(Basis.Ins->getContext(), AS); |
| 674 | Reduced = Builder.CreateBitCast(Basis.Ins, CharTy); |
Jingyue Wu | 99a6bed | 2015-04-02 21:18:32 +0000 | [diff] [blame] | 675 | if (InBounds) |
David Blaikie | aa41cd5 | 2015-04-03 21:33:42 +0000 | [diff] [blame] | 676 | Reduced = |
| 677 | Builder.CreateInBoundsGEP(Builder.getInt8Ty(), Reduced, Bump); |
Jingyue Wu | 99a6bed | 2015-04-02 21:18:32 +0000 | [diff] [blame] | 678 | else |
David Blaikie | 93c5444 | 2015-04-03 19:41:44 +0000 | [diff] [blame] | 679 | Reduced = Builder.CreateGEP(Builder.getInt8Ty(), Reduced, Bump); |
Jingyue Wu | 177a815 | 2015-03-26 16:49:24 +0000 | [diff] [blame] | 680 | Reduced = Builder.CreateBitCast(Reduced, C.Ins->getType()); |
| 681 | } else { |
| 682 | // C = gep Basis, Bump |
| 683 | // Canonicalize bump to pointer size. |
| 684 | Bump = Builder.CreateSExtOrTrunc(Bump, IntPtrTy); |
Jingyue Wu | 99a6bed | 2015-04-02 21:18:32 +0000 | [diff] [blame] | 685 | if (InBounds) |
James Y Knight | 7716075 | 2019-02-01 20:44:47 +0000 | [diff] [blame] | 686 | Reduced = Builder.CreateInBoundsGEP( |
| 687 | cast<GetElementPtrInst>(Basis.Ins)->getResultElementType(), |
| 688 | Basis.Ins, Bump); |
Jingyue Wu | 99a6bed | 2015-04-02 21:18:32 +0000 | [diff] [blame] | 689 | else |
James Y Knight | 7716075 | 2019-02-01 20:44:47 +0000 | [diff] [blame] | 690 | Reduced = Builder.CreateGEP( |
| 691 | cast<GetElementPtrInst>(Basis.Ins)->getResultElementType(), |
| 692 | Basis.Ins, Bump); |
Jingyue Wu | 177a815 | 2015-03-26 16:49:24 +0000 | [diff] [blame] | 693 | } |
Eugene Zelenko | 57bd5a0 | 2017-10-27 01:09:08 +0000 | [diff] [blame] | 694 | break; |
Jingyue Wu | 177a815 | 2015-03-26 16:49:24 +0000 | [diff] [blame] | 695 | } |
Jingyue Wu | 177a815 | 2015-03-26 16:49:24 +0000 | [diff] [blame] | 696 | default: |
| 697 | llvm_unreachable("C.CandidateKind is invalid"); |
| 698 | }; |
Jingyue Wu | d7966ff | 2015-02-03 19:37:06 +0000 | [diff] [blame] | 699 | Reduced->takeName(C.Ins); |
| 700 | C.Ins->replaceAllUsesWith(Reduced); |
Jingyue Wu | d7966ff | 2015-02-03 19:37:06 +0000 | [diff] [blame] | 701 | // Unlink C.Ins so that we can skip other candidates also corresponding to |
| 702 | // C.Ins. The actual deletion is postponed to the end of runOnFunction. |
| 703 | C.Ins->removeFromParent(); |
Jingyue Wu | 43885eb | 2015-04-15 16:46:13 +0000 | [diff] [blame] | 704 | UnlinkedInstructions.push_back(C.Ins); |
Jingyue Wu | d7966ff | 2015-02-03 19:37:06 +0000 | [diff] [blame] | 705 | } |
| 706 | |
| 707 | bool StraightLineStrengthReduce::runOnFunction(Function &F) { |
Andrew Kaylor | aa641a5 | 2016-04-22 22:06:11 +0000 | [diff] [blame] | 708 | if (skipFunction(F)) |
Jingyue Wu | d7966ff | 2015-02-03 19:37:06 +0000 | [diff] [blame] | 709 | return false; |
| 710 | |
Jingyue Wu | 177a815 | 2015-03-26 16:49:24 +0000 | [diff] [blame] | 711 | TTI = &getAnalysis<TargetTransformInfoWrapperPass>().getTTI(F); |
Jingyue Wu | d7966ff | 2015-02-03 19:37:06 +0000 | [diff] [blame] | 712 | DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree(); |
Chandler Carruth | 2f1fd16 | 2015-08-17 02:08:17 +0000 | [diff] [blame] | 713 | SE = &getAnalysis<ScalarEvolutionWrapperPass>().getSE(); |
Jingyue Wu | d7966ff | 2015-02-03 19:37:06 +0000 | [diff] [blame] | 714 | // Traverse the dominator tree in the depth-first order. This order makes sure |
| 715 | // all bases of a candidate are in Candidates when we process it. |
Daniel Berlin | a36f463 | 2016-08-19 22:06:23 +0000 | [diff] [blame] | 716 | for (const auto Node : depth_first(DT)) |
| 717 | for (auto &I : *(Node->getBlock())) |
Jingyue Wu | 43885eb | 2015-04-15 16:46:13 +0000 | [diff] [blame] | 718 | allocateCandidatesAndFindBasis(&I); |
Jingyue Wu | d7966ff | 2015-02-03 19:37:06 +0000 | [diff] [blame] | 719 | |
| 720 | // Rewrite candidates in the reverse depth-first order. This order makes sure |
| 721 | // a candidate being rewritten is not a basis for any other candidate. |
| 722 | while (!Candidates.empty()) { |
| 723 | const Candidate &C = Candidates.back(); |
| 724 | if (C.Basis != nullptr) { |
| 725 | rewriteCandidateWithBasis(C, *C.Basis); |
| 726 | } |
| 727 | Candidates.pop_back(); |
| 728 | } |
| 729 | |
| 730 | // Delete all unlink instructions. |
Jingyue Wu | f1edf3e | 2015-04-21 19:56:18 +0000 | [diff] [blame] | 731 | for (auto *UnlinkedInst : UnlinkedInstructions) { |
| 732 | for (unsigned I = 0, E = UnlinkedInst->getNumOperands(); I != E; ++I) { |
| 733 | Value *Op = UnlinkedInst->getOperand(I); |
| 734 | UnlinkedInst->setOperand(I, nullptr); |
| 735 | RecursivelyDeleteTriviallyDeadInstructions(Op); |
| 736 | } |
Reid Kleckner | 96ab872 | 2017-05-18 17:24:10 +0000 | [diff] [blame] | 737 | UnlinkedInst->deleteValue(); |
Jingyue Wu | d7966ff | 2015-02-03 19:37:06 +0000 | [diff] [blame] | 738 | } |
| 739 | bool Ret = !UnlinkedInstructions.empty(); |
| 740 | UnlinkedInstructions.clear(); |
| 741 | return Ret; |
| 742 | } |