| 1 |
diff --git a/llvm/include/llvm/Analysis/ScalarEvolution.h b/llvm/include/llvm/Analysis/ScalarEvolution.h |
| 2 |
index b16aa7017719..51558a37b9f0 100644 |
| 3 |
--- a/llvm/include/llvm/Analysis/ScalarEvolution.h |
| 4 |
+++ b/llvm/include/llvm/Analysis/ScalarEvolution.h |
| 5 |
@@ -1599,9 +1599,17 @@ private: |
| 6 |
ConstantRange getRangeForUnknownRecurrence(const SCEVUnknown *U); |
| 7 |
|
| 8 |
/// We know that there is no SCEV for the specified value. Analyze the |
| 9 |
- /// expression. |
| 10 |
+ /// expression recursively. |
| 11 |
const SCEV *createSCEV(Value *V); |
| 12 |
|
| 13 |
+ /// We know that there is no SCEV for the specified value. Create a new SCEV |
| 14 |
+ /// for \p V iteratively. |
| 15 |
+ const SCEV *createSCEVIter(Value *V); |
| 16 |
+ /// Collect operands of \p V for which SCEV expressions should be constructed |
| 17 |
+ /// first. Returns a SCEV directly if it can be constructed trivially for \p |
| 18 |
+ /// V. |
| 19 |
+ const SCEV *getOperandsToCreate(Value *V, SmallVectorImpl<Value *> &Ops); |
| 20 |
+ |
| 21 |
/// Provide the special handling we need to analyze PHI SCEVs. |
| 22 |
const SCEV *createNodeForPHI(PHINode *PN); |
| 23 |
|
| 24 |
diff --git a/llvm/lib/Analysis/ScalarEvolution.cpp b/llvm/lib/Analysis/ScalarEvolution.cpp |
| 25 |
index 977fc0911355..fc9517ebb3f6 100644 |
| 26 |
--- a/llvm/lib/Analysis/ScalarEvolution.cpp |
| 27 |
+++ b/llvm/lib/Analysis/ScalarEvolution.cpp |
| 28 |
@@ -4339,33 +4339,9 @@ void ScalarEvolution::insertValueToMap(Value *V, const SCEV *S) { |
| 29 |
const SCEV *ScalarEvolution::getSCEV(Value *V) { |
| 30 |
assert(isSCEVable(V->getType()) && "Value is not SCEVable!"); |
| 31 |
|
| 32 |
- const SCEV *S = getExistingSCEV(V); |
| 33 |
- if (S == nullptr) { |
| 34 |
- S = createSCEV(V); |
| 35 |
- // During PHI resolution, it is possible to create two SCEVs for the same |
| 36 |
- // V, so it is needed to double check whether V->S is inserted into |
| 37 |
- // ValueExprMap before insert S->{V, 0} into ExprValueMap. |
| 38 |
- std::pair<ValueExprMapType::iterator, bool> Pair = |
| 39 |
- ValueExprMap.insert({SCEVCallbackVH(V, this), S}); |
| 40 |
- if (Pair.second) { |
| 41 |
- ExprValueMap[S].insert({V, nullptr}); |
| 42 |
- |
| 43 |
- // If S == Stripped + Offset, add Stripped -> {V, Offset} into |
| 44 |
- // ExprValueMap. |
| 45 |
- const SCEV *Stripped = S; |
| 46 |
- ConstantInt *Offset = nullptr; |
| 47 |
- std::tie(Stripped, Offset) = splitAddExpr(S); |
| 48 |
- // If stripped is SCEVUnknown, don't bother to save |
| 49 |
- // Stripped -> {V, offset}. It doesn't simplify and sometimes even |
| 50 |
- // increase the complexity of the expansion code. |
| 51 |
- // If V is GetElementPtrInst, don't save Stripped -> {V, offset} |
| 52 |
- // because it may generate add/sub instead of GEP in SCEV expansion. |
| 53 |
- if (Offset != nullptr && !isa<SCEVUnknown>(Stripped) && |
| 54 |
- !isa<GetElementPtrInst>(V)) |
| 55 |
- ExprValueMap[Stripped].insert({V, Offset}); |
| 56 |
- } |
| 57 |
- } |
| 58 |
- return S; |
| 59 |
+ if (const SCEV *S = getExistingSCEV(V)) |
| 60 |
+ return S; |
| 61 |
+ return createSCEVIter(V); |
| 62 |
} |
| 63 |
|
| 64 |
const SCEV *ScalarEvolution::getExistingSCEV(Value *V) { |
| 65 |
@@ -7020,6 +6996,228 @@ bool ScalarEvolution::loopIsFiniteByAssumption(const Loop *L) { |
| 66 |
return isFinite(L) || (isMustProgress(L) && loopHasNoSideEffects(L)); |
| 67 |
} |
| 68 |
|
| 69 |
+const SCEV *ScalarEvolution::createSCEVIter(Value *V) { |
| 70 |
+ // Worklist item with a Value and a bool indicating whether all operands have |
| 71 |
+ // been visited already. |
| 72 |
+ using PointerTy = PointerIntPair<Value *, 1, bool>; |
| 73 |
+ SmallVector<PointerTy> Stack; |
| 74 |
+ |
| 75 |
+ Stack.emplace_back(V, true); |
| 76 |
+ Stack.emplace_back(V, false); |
| 77 |
+ while (!Stack.empty()) { |
| 78 |
+ auto E = Stack.pop_back_val(); |
| 79 |
+ Value *CurV = E.getPointer(); |
| 80 |
+ |
| 81 |
+ if (getExistingSCEV(CurV)) |
| 82 |
+ continue; |
| 83 |
+ |
| 84 |
+ SmallVector<Value *> Ops; |
| 85 |
+ const SCEV *CreatedSCEV = nullptr; |
| 86 |
+ // If all operands have been visited already, create the SCEV. |
| 87 |
+ if (E.getInt()) { |
| 88 |
+ CreatedSCEV = createSCEV(CurV); |
| 89 |
+ } else { |
| 90 |
+ // Otherwise get the operands we need to create SCEV's for before creating |
| 91 |
+ // the SCEV for CurV. If the SCEV for CurV can be constructed trivially, |
| 92 |
+ // just use it. |
| 93 |
+ CreatedSCEV = getOperandsToCreate(CurV, Ops); |
| 94 |
+ } |
| 95 |
+ |
| 96 |
+ if (CreatedSCEV) { |
| 97 |
+ insertValueToMap(CurV, CreatedSCEV); |
| 98 |
+ } else { |
| 99 |
+ // Queue CurV for SCEV creation, followed by its's operands which need to |
| 100 |
+ // be constructed first. |
| 101 |
+ Stack.emplace_back(CurV, true); |
| 102 |
+ for (Value *Op : Ops) |
| 103 |
+ Stack.emplace_back(Op, false); |
| 104 |
+ } |
| 105 |
+ } |
| 106 |
+ |
| 107 |
+ return getExistingSCEV(V); |
| 108 |
+} |
| 109 |
+ |
| 110 |
+const SCEV * |
| 111 |
+ScalarEvolution::getOperandsToCreate(Value *V, SmallVectorImpl<Value *> &Ops) { |
| 112 |
+ if (!isSCEVable(V->getType())) |
| 113 |
+ return getUnknown(V); |
| 114 |
+ |
| 115 |
+ if (Instruction *I = dyn_cast<Instruction>(V)) { |
| 116 |
+ // Don't attempt to analyze instructions in blocks that aren't |
| 117 |
+ // reachable. Such instructions don't matter, and they aren't required |
| 118 |
+ // to obey basic rules for definitions dominating uses which this |
| 119 |
+ // analysis depends on. |
| 120 |
+ if (!DT.isReachableFromEntry(I->getParent())) |
| 121 |
+ return getUnknown(PoisonValue::get(V->getType())); |
| 122 |
+ } else if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) |
| 123 |
+ return getConstant(CI); |
| 124 |
+ else if (GlobalAlias *GA = dyn_cast<GlobalAlias>(V)) { |
| 125 |
+ if (!GA->isInterposable()) { |
| 126 |
+ Ops.push_back(GA->getAliasee()); |
| 127 |
+ return nullptr; |
| 128 |
+ } |
| 129 |
+ return getUnknown(V); |
| 130 |
+ } else if (!isa<ConstantExpr>(V)) |
| 131 |
+ return getUnknown(V); |
| 132 |
+ |
| 133 |
+ Operator *U = cast<Operator>(V); |
| 134 |
+ if (auto BO = MatchBinaryOp(U, DT)) { |
| 135 |
+ bool IsConstArg = isa<ConstantInt>(BO->RHS); |
| 136 |
+ switch (U->getOpcode()) { |
| 137 |
+ case Instruction::Add: |
| 138 |
+ case Instruction::Mul: { |
| 139 |
+ // For additions and multiplications, traverse add/mul chains for which we |
| 140 |
+ // can potentially create a single SCEV, to reduce the number of |
| 141 |
+ // get{Add,Mul}Expr calls. |
| 142 |
+ do { |
| 143 |
+ if (BO->Op) { |
| 144 |
+ if (BO->Op != V && getExistingSCEV(BO->Op)) { |
| 145 |
+ Ops.push_back(BO->Op); |
| 146 |
+ break; |
| 147 |
+ } |
| 148 |
+ } |
| 149 |
+ Ops.push_back(BO->RHS); |
| 150 |
+ auto NewBO = MatchBinaryOp(BO->LHS, DT); |
| 151 |
+ if (!NewBO || |
| 152 |
+ (U->getOpcode() == Instruction::Add && |
| 153 |
+ (NewBO->Opcode != Instruction::Add && |
| 154 |
+ NewBO->Opcode != Instruction::Sub)) || |
| 155 |
+ (U->getOpcode() == Instruction::Mul && |
| 156 |
+ NewBO->Opcode != Instruction::Mul)) { |
| 157 |
+ Ops.push_back(BO->LHS); |
| 158 |
+ break; |
| 159 |
+ } |
| 160 |
+ // CreateSCEV calls getNoWrapFlagsFromUB, which under certain conditions |
| 161 |
+ // requires a SCEV for the LHS. |
| 162 |
+ if (NewBO->Op && (NewBO->IsNSW || NewBO->IsNUW)) { |
| 163 |
+ auto *I = dyn_cast<Instruction>(NewBO->Op); |
| 164 |
+ if (I && programUndefinedIfPoison(I)) { |
| 165 |
+ Ops.push_back(BO->LHS); |
| 166 |
+ break; |
| 167 |
+ } |
| 168 |
+ } |
| 169 |
+ BO = NewBO; |
| 170 |
+ } while (true); |
| 171 |
+ return nullptr; |
| 172 |
+ } |
| 173 |
+ |
| 174 |
+ case Instruction::AShr: |
| 175 |
+ case Instruction::Shl: |
| 176 |
+ case Instruction::Xor: |
| 177 |
+ if (!IsConstArg) |
| 178 |
+ return nullptr; |
| 179 |
+ break; |
| 180 |
+ case Instruction::And: |
| 181 |
+ case Instruction::Or: |
| 182 |
+ if (!IsConstArg && BO->LHS->getType()->isIntegerTy(1)) |
| 183 |
+ return nullptr; |
| 184 |
+ break; |
| 185 |
+ default: |
| 186 |
+ break; |
| 187 |
+ } |
| 188 |
+ |
| 189 |
+ Ops.push_back(BO->LHS); |
| 190 |
+ Ops.push_back(BO->RHS); |
| 191 |
+ return nullptr; |
| 192 |
+ } |
| 193 |
+ |
| 194 |
+ switch (U->getOpcode()) { |
| 195 |
+ case Instruction::Trunc: |
| 196 |
+ case Instruction::ZExt: |
| 197 |
+ case Instruction::SExt: |
| 198 |
+ case Instruction::PtrToInt: |
| 199 |
+ Ops.push_back(U->getOperand(0)); |
| 200 |
+ return nullptr; |
| 201 |
+ |
| 202 |
+ case Instruction::BitCast: |
| 203 |
+ if (isSCEVable(U->getType()) && isSCEVable(U->getOperand(0)->getType())) { |
| 204 |
+ Ops.push_back(U->getOperand(0)); |
| 205 |
+ return nullptr; |
| 206 |
+ } |
| 207 |
+ return getUnknown(V); |
| 208 |
+ |
| 209 |
+ case Instruction::SDiv: |
| 210 |
+ case Instruction::SRem: |
| 211 |
+ Ops.push_back(U->getOperand(0)); |
| 212 |
+ Ops.push_back(U->getOperand(1)); |
| 213 |
+ return nullptr; |
| 214 |
+ |
| 215 |
+ case Instruction::GetElementPtr: |
| 216 |
+ assert(cast<GEPOperator>(U)->getSourceElementType()->isSized() && |
| 217 |
+ "GEP source element type must be sized"); |
| 218 |
+ for (Value *Index : U->operands()) |
| 219 |
+ Ops.push_back(Index); |
| 220 |
+ return nullptr; |
| 221 |
+ |
| 222 |
+ case Instruction::IntToPtr: |
| 223 |
+ return getUnknown(V); |
| 224 |
+ |
| 225 |
+ case Instruction::PHI: |
| 226 |
+ // Keep constructing SCEVs' for phis recursively for now. |
| 227 |
+ return nullptr; |
| 228 |
+ |
| 229 |
+ case Instruction::Select: { |
| 230 |
+ // Check if U is a select that can be simplified to a SCEVUnknown. |
| 231 |
+ auto CanSimplifyToUnknown = [this, U]() { |
| 232 |
+ if (U->getType()->isIntegerTy(1) || isa<ConstantInt>(U->getOperand(0))) |
| 233 |
+ return false; |
| 234 |
+ |
| 235 |
+ auto *ICI = dyn_cast<ICmpInst>(U->getOperand(0)); |
| 236 |
+ if (!ICI) |
| 237 |
+ return false; |
| 238 |
+ Value *LHS = ICI->getOperand(0); |
| 239 |
+ Value *RHS = ICI->getOperand(1); |
| 240 |
+ if (ICI->getPredicate() == CmpInst::ICMP_EQ || |
| 241 |
+ ICI->getPredicate() == CmpInst::ICMP_NE) { |
| 242 |
+ if (!(isa<ConstantInt>(RHS) && cast<ConstantInt>(RHS)->isZero())) |
| 243 |
+ return true; |
| 244 |
+ } else if (getTypeSizeInBits(LHS->getType()) > |
| 245 |
+ getTypeSizeInBits(U->getType())) |
| 246 |
+ return true; |
| 247 |
+ return false; |
| 248 |
+ }; |
| 249 |
+ if (CanSimplifyToUnknown()) |
| 250 |
+ return getUnknown(U); |
| 251 |
+ |
| 252 |
+ for (Value *Inc : U->operands()) |
| 253 |
+ Ops.push_back(Inc); |
| 254 |
+ return nullptr; |
| 255 |
+ break; |
| 256 |
+ } |
| 257 |
+ case Instruction::Call: |
| 258 |
+ case Instruction::Invoke: |
| 259 |
+ if (Value *RV = cast<CallBase>(U)->getReturnedArgOperand()) { |
| 260 |
+ Ops.push_back(RV); |
| 261 |
+ return nullptr; |
| 262 |
+ } |
| 263 |
+ |
| 264 |
+ if (auto *II = dyn_cast<IntrinsicInst>(U)) { |
| 265 |
+ switch (II->getIntrinsicID()) { |
| 266 |
+ case Intrinsic::abs: |
| 267 |
+ Ops.push_back(II->getArgOperand(0)); |
| 268 |
+ return nullptr; |
| 269 |
+ case Intrinsic::umax: |
| 270 |
+ case Intrinsic::umin: |
| 271 |
+ case Intrinsic::smax: |
| 272 |
+ case Intrinsic::smin: |
| 273 |
+ case Intrinsic::usub_sat: |
| 274 |
+ case Intrinsic::uadd_sat: |
| 275 |
+ Ops.push_back(II->getArgOperand(0)); |
| 276 |
+ Ops.push_back(II->getArgOperand(1)); |
| 277 |
+ return nullptr; |
| 278 |
+ case Intrinsic::start_loop_iterations: |
| 279 |
+ Ops.push_back(II->getArgOperand(0)); |
| 280 |
+ return nullptr; |
| 281 |
+ default: |
| 282 |
+ break; |
| 283 |
+ } |
| 284 |
+ } |
| 285 |
+ break; |
| 286 |
+ } |
| 287 |
+ |
| 288 |
+ return nullptr; |
| 289 |
+} |
| 290 |
+ |
| 291 |
const SCEV *ScalarEvolution::createSCEV(Value *V) { |
| 292 |
if (!isSCEVable(V->getType())) |
| 293 |
return getUnknown(V); |