1//===- ScalarEvolutionExpander.cpp - Scalar Evolution Analysis ------------===//
2//
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
6//
7//===----------------------------------------------------------------------===//
8//
9// This file contains the implementation of the scalar evolution expander,
10// which is used to generate the code corresponding to a given scalar evolution
11// expression.
12//
13//===----------------------------------------------------------------------===//
14
15#include "llvm/Transforms/Utils/ScalarEvolutionExpander.h"
16#include "llvm/ADT/STLExtras.h"
17#include "llvm/ADT/ScopeExit.h"
18#include "llvm/Analysis/InstructionSimplify.h"
19#include "llvm/Analysis/LoopInfo.h"
20#include "llvm/Analysis/ScalarEvolutionPatternMatch.h"
21#include "llvm/Analysis/TargetTransformInfo.h"
22#include "llvm/Analysis/ValueTracking.h"
23#include "llvm/IR/DataLayout.h"
24#include "llvm/IR/Dominators.h"
25#include "llvm/IR/IntrinsicInst.h"
26#include "llvm/IR/PatternMatch.h"
27#include "llvm/Support/CommandLine.h"
28#include "llvm/Support/raw_ostream.h"
29#include "llvm/Transforms/Utils/Local.h"
30#include "llvm/Transforms/Utils/LoopUtils.h"
31
32#if LLVM_ENABLE_ABI_BREAKING_CHECKS
33#define SCEV_DEBUG_WITH_TYPE(TYPE, X) DEBUG_WITH_TYPE(TYPE, X)
34#else
35#define SCEV_DEBUG_WITH_TYPE(TYPE, X)
36#endif
37
38using namespace llvm;
39
40cl::opt<unsigned> llvm::SCEVCheapExpansionBudget(
41 "scev-cheap-expansion-budget", cl::Hidden, cl::init(Val: 4),
42 cl::desc("When performing SCEV expansion only if it is cheap to do, this "
43 "controls the budget that is considered cheap (default = 4)"));
44
45using namespace PatternMatch;
46using namespace SCEVPatternMatch;
47
48PoisonFlags::PoisonFlags(const Instruction *I) {
49 NUW = false;
50 NSW = false;
51 Exact = false;
52 Disjoint = false;
53 NNeg = false;
54 SameSign = false;
55 GEPNW = GEPNoWrapFlags::none();
56 if (auto *OBO = dyn_cast<OverflowingBinaryOperator>(Val: I)) {
57 NUW = OBO->hasNoUnsignedWrap();
58 NSW = OBO->hasNoSignedWrap();
59 }
60 if (auto *PEO = dyn_cast<PossiblyExactOperator>(Val: I))
61 Exact = PEO->isExact();
62 if (auto *PDI = dyn_cast<PossiblyDisjointInst>(Val: I))
63 Disjoint = PDI->isDisjoint();
64 if (auto *PNI = dyn_cast<PossiblyNonNegInst>(Val: I))
65 NNeg = PNI->hasNonNeg();
66 if (auto *TI = dyn_cast<TruncInst>(Val: I)) {
67 NUW = TI->hasNoUnsignedWrap();
68 NSW = TI->hasNoSignedWrap();
69 }
70 if (auto *GEP = dyn_cast<GetElementPtrInst>(Val: I))
71 GEPNW = GEP->getNoWrapFlags();
72 if (auto *ICmp = dyn_cast<ICmpInst>(Val: I))
73 SameSign = ICmp->hasSameSign();
74}
75
76void PoisonFlags::apply(Instruction *I) {
77 if (isa<OverflowingBinaryOperator>(Val: I)) {
78 I->setHasNoUnsignedWrap(NUW);
79 I->setHasNoSignedWrap(NSW);
80 }
81 if (isa<PossiblyExactOperator>(Val: I))
82 I->setIsExact(Exact);
83 if (auto *PDI = dyn_cast<PossiblyDisjointInst>(Val: I))
84 PDI->setIsDisjoint(Disjoint);
85 if (auto *PNI = dyn_cast<PossiblyNonNegInst>(Val: I))
86 PNI->setNonNeg(NNeg);
87 if (isa<TruncInst>(Val: I)) {
88 I->setHasNoUnsignedWrap(NUW);
89 I->setHasNoSignedWrap(NSW);
90 }
91 if (auto *GEP = dyn_cast<GetElementPtrInst>(Val: I))
92 GEP->setNoWrapFlags(GEPNW);
93 if (auto *ICmp = dyn_cast<ICmpInst>(Val: I))
94 ICmp->setSameSign(SameSign);
95}
96
97/// ReuseOrCreateCast - Arrange for there to be a cast of V to Ty at IP,
98/// reusing an existing cast if a suitable one (= dominating IP) exists, or
99/// creating a new one.
100Value *SCEVExpander::ReuseOrCreateCast(Value *V, Type *Ty,
101 Instruction::CastOps Op,
102 BasicBlock::iterator IP) {
103 // This function must be called with the builder having a valid insertion
104 // point. It doesn't need to be the actual IP where the uses of the returned
105 // cast will be added, but it must dominate such IP.
106 // We use this precondition to produce a cast that will dominate all its
107 // uses. In particular, this is crucial for the case where the builder's
108 // insertion point *is* the point where we were asked to put the cast.
109 // Since we don't know the builder's insertion point is actually
110 // where the uses will be added (only that it dominates it), we are
111 // not allowed to move it.
112 BasicBlock::iterator BIP = Builder.GetInsertPoint();
113
114 Value *Ret = nullptr;
115
116 if (!isa<Constant>(Val: V)) {
117 // Check to see if there is already a cast!
118 for (User *U : V->users()) {
119 if (U->getType() != Ty)
120 continue;
121 CastInst *CI = dyn_cast<CastInst>(Val: U);
122 if (!CI || CI->getOpcode() != Op)
123 continue;
124
125 // Found a suitable cast that is at IP or comes before IP. Use it. Note
126 // that the cast must also properly dominate the Builder's insertion
127 // point.
128 if (IP->getParent() == CI->getParent() && &*BIP != CI &&
129 (&*IP == CI || CI->comesBefore(Other: &*IP))) {
130 Ret = CI;
131 break;
132 }
133 }
134 }
135
136 // Create a new cast.
137 if (!Ret) {
138 SCEVInsertPointGuard Guard(Builder, this);
139 Builder.SetInsertPoint(&*IP);
140 Ret = Builder.CreateCast(Op, V, DestTy: Ty, Name: V->getName());
141 }
142
143 // We assert at the end of the function since IP might point to an
144 // instruction with different dominance properties than a cast
145 // (an invoke for example) and not dominate BIP (but the cast does).
146 assert(!isa<Instruction>(Ret) ||
147 SE.DT.dominates(cast<Instruction>(Ret), &*BIP));
148
149 return Ret;
150}
151
152BasicBlock::iterator
153SCEVExpander::findInsertPointAfter(Instruction *I,
154 Instruction *MustDominate) const {
155 BasicBlock::iterator IP;
156 if (auto MaybeIP = I->getInsertionPointAfterDef()) {
157 IP = *MaybeIP;
158 } else {
159 assert(SE.DT.dominates(I, MustDominate) &&
160 "instruction must dominate the insertion point");
161 IP = MustDominate->getIterator();
162 }
163
164 // Adjust insert point to be after instructions inserted by the expander, so
165 // we can re-use already inserted instructions. Avoid skipping past the
166 // original \p MustDominate, in case it is an inserted instruction.
167 while (isInsertedInstruction(I: &*IP) && &*IP != MustDominate)
168 ++IP;
169
170 return IP;
171}
172
173void SCEVExpander::eraseDeadInstructions(Value *Root) {
174 SmallVector<Value *> WorkList;
175 SmallPtrSet<Value *, 8> DeletedValues;
176 append_range(C&: WorkList, R: getAllInsertedInstructions());
177 while (!WorkList.empty()) {
178 Value *V = WorkList.pop_back_val();
179 if (DeletedValues.contains(Ptr: V))
180 continue;
181 auto *I = dyn_cast<Instruction>(Val: V);
182 if (!I || I == Root || !isInsertedInstruction(I) ||
183 !isInstructionTriviallyDead(I))
184 continue;
185 append_range(C&: WorkList, R: I->operands());
186 InsertedValues.erase(V: I);
187 InsertedPostIncValues.erase(V: I);
188 DeletedValues.insert(Ptr: I);
189 I->eraseFromParent();
190 }
191}
192
193BasicBlock::iterator
194SCEVExpander::GetOptimalInsertionPointForCastOf(Value *V) const {
195 // Cast the argument at the beginning of the entry block, after
196 // any bitcasts of other arguments.
197 if (Argument *A = dyn_cast<Argument>(Val: V)) {
198 BasicBlock::iterator IP = A->getParent()->getEntryBlock().begin();
199 while ((isa<BitCastInst>(Val: IP) &&
200 isa<Argument>(Val: cast<BitCastInst>(Val&: IP)->getOperand(i_nocapture: 0)) &&
201 cast<BitCastInst>(Val&: IP)->getOperand(i_nocapture: 0) != A))
202 ++IP;
203 return IP;
204 }
205
206 // Cast the instruction immediately after the instruction.
207 if (Instruction *I = dyn_cast<Instruction>(Val: V))
208 return findInsertPointAfter(I, MustDominate: &*Builder.GetInsertPoint());
209
210 // Otherwise, this must be some kind of a constant,
211 // so let's plop this cast into the function's entry block.
212 assert(isa<Constant>(V) &&
213 "Expected the cast argument to be a global/constant");
214 return Builder.GetInsertBlock()
215 ->getParent()
216 ->getEntryBlock()
217 .getFirstInsertionPt();
218}
219
220/// InsertNoopCastOfTo - Insert a cast of V to the specified type,
221/// which must be possible with a noop cast, doing what we can to share
222/// the casts.
223Value *SCEVExpander::InsertNoopCastOfTo(Value *V, Type *Ty) {
224 Instruction::CastOps Op = CastInst::getCastOpcode(Val: V, SrcIsSigned: false, Ty, DstIsSigned: false);
225 assert((Op == Instruction::BitCast ||
226 Op == Instruction::PtrToInt ||
227 Op == Instruction::IntToPtr) &&
228 "InsertNoopCastOfTo cannot perform non-noop casts!");
229 assert(SE.getTypeSizeInBits(V->getType()) == SE.getTypeSizeInBits(Ty) &&
230 "InsertNoopCastOfTo cannot change sizes!");
231
232 // inttoptr only works for integral pointers. For non-integral pointers, we
233 // can create a GEP on null with the integral value as index. Note that
234 // it is safe to use GEP of null instead of inttoptr here, because only
235 // expressions already based on a GEP of null should be converted to pointers
236 // during expansion.
237 if (Op == Instruction::IntToPtr) {
238 auto *PtrTy = cast<PointerType>(Val: Ty);
239 if (DL.isNonIntegralPointerType(PT: PtrTy))
240 return Builder.CreatePtrAdd(Ptr: Constant::getNullValue(Ty: PtrTy), Offset: V, Name: "scevgep");
241 }
242 // Short-circuit unnecessary bitcasts.
243 if (Op == Instruction::BitCast) {
244 if (V->getType() == Ty)
245 return V;
246 if (CastInst *CI = dyn_cast<CastInst>(Val: V)) {
247 if (CI->getOperand(i_nocapture: 0)->getType() == Ty)
248 return CI->getOperand(i_nocapture: 0);
249 }
250 }
251 // Short-circuit unnecessary inttoptr<->ptrtoint casts.
252 if ((Op == Instruction::PtrToInt || Op == Instruction::IntToPtr) &&
253 SE.getTypeSizeInBits(Ty) == SE.getTypeSizeInBits(Ty: V->getType())) {
254 if (CastInst *CI = dyn_cast<CastInst>(Val: V))
255 if ((CI->getOpcode() == Instruction::PtrToInt ||
256 CI->getOpcode() == Instruction::IntToPtr) &&
257 SE.getTypeSizeInBits(Ty: CI->getType()) ==
258 SE.getTypeSizeInBits(Ty: CI->getOperand(i_nocapture: 0)->getType()))
259 return CI->getOperand(i_nocapture: 0);
260 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(Val: V))
261 if ((CE->getOpcode() == Instruction::PtrToInt ||
262 CE->getOpcode() == Instruction::IntToPtr) &&
263 SE.getTypeSizeInBits(Ty: CE->getType()) ==
264 SE.getTypeSizeInBits(Ty: CE->getOperand(i_nocapture: 0)->getType()))
265 return CE->getOperand(i_nocapture: 0);
266 }
267
268 // Fold a cast of a constant.
269 if (Constant *C = dyn_cast<Constant>(Val: V))
270 return ConstantExpr::getCast(ops: Op, C, Ty);
271
272 // Try to reuse existing cast, or insert one.
273 return ReuseOrCreateCast(V, Ty, Op, IP: GetOptimalInsertionPointForCastOf(V));
274}
275
276/// InsertBinop - Insert the specified binary operator, doing a small amount
277/// of work to avoid inserting an obviously redundant operation, and hoisting
278/// to an outer loop when the opportunity is there and it is safe.
279Value *SCEVExpander::InsertBinop(Instruction::BinaryOps Opcode,
280 Value *LHS, Value *RHS,
281 SCEV::NoWrapFlags Flags, bool IsSafeToHoist) {
282 // Fold a binop with constant operands.
283 if (Constant *CLHS = dyn_cast<Constant>(Val: LHS))
284 if (Constant *CRHS = dyn_cast<Constant>(Val: RHS))
285 if (Constant *Res = ConstantFoldBinaryOpOperands(Opcode, LHS: CLHS, RHS: CRHS, DL))
286 return Res;
287
288 // Do a quick scan to see if we have this binop nearby. If so, reuse it.
289 unsigned ScanLimit = 6;
290 BasicBlock::iterator BlockBegin = Builder.GetInsertBlock()->begin();
291 // Scanning starts from the last instruction before the insertion point.
292 BasicBlock::iterator IP = Builder.GetInsertPoint();
293 if (IP != BlockBegin) {
294 --IP;
295 for (; ScanLimit; --IP, --ScanLimit) {
296 auto canGenerateIncompatiblePoison = [&Flags](Instruction *I) {
297 // Ensure that no-wrap flags match.
298 if (isa<OverflowingBinaryOperator>(Val: I)) {
299 if (I->hasNoSignedWrap() != any(Val: Flags & SCEV::FlagNSW))
300 return true;
301 if (I->hasNoUnsignedWrap() != any(Val: Flags & SCEV::FlagNUW))
302 return true;
303 }
304 // Conservatively, do not use any instruction which has any of exact
305 // flags installed.
306 if (isa<PossiblyExactOperator>(Val: I) && I->isExact())
307 return true;
308 return false;
309 };
310 if (IP->getOpcode() == (unsigned)Opcode && IP->getOperand(i: 0) == LHS &&
311 IP->getOperand(i: 1) == RHS && !canGenerateIncompatiblePoison(&*IP))
312 return &*IP;
313 if (IP == BlockBegin) break;
314 }
315 }
316
317 // Save the original insertion point so we can restore it when we're done.
318 DebugLoc Loc = Builder.GetInsertPoint()->getDebugLoc();
319 SCEVInsertPointGuard Guard(Builder, this);
320
321 if (IsSafeToHoist) {
322 // Move the insertion point out of as many loops as we can.
323 while (const Loop *L = SE.LI.getLoopFor(BB: Builder.GetInsertBlock())) {
324 if (!L->isLoopInvariant(V: LHS) || !L->isLoopInvariant(V: RHS)) break;
325 BasicBlock *Preheader = L->getLoopPreheader();
326 if (!Preheader) break;
327
328 // Ok, move up a level.
329 Builder.SetInsertPoint(Preheader->getTerminator());
330 }
331 }
332
333 // If we haven't found this binop, insert it.
334 Builder.SetCurrentDebugLocation(Loc);
335 bool IsNUW = any(Val: Flags & SCEV::FlagNUW);
336 bool IsNSW = any(Val: Flags & SCEV::FlagNSW);
337 // Don't use folder when expanding post-inc rewrites in LSRMode to preserve
338 // the rewrites.
339 if (LSRMode && !PostIncLoops.empty() &&
340 all_of(Range&: PostIncLoops, P: [&](const Loop *L) {
341 return !L->contains(BB: Builder.GetInsertBlock());
342 })) {
343 auto *BO = BinaryOperator::Create(Op: Opcode, S1: LHS, S2: RHS);
344 if (IsNUW)
345 BO->setHasNoUnsignedWrap();
346 if (IsNSW)
347 BO->setHasNoSignedWrap();
348 return Builder.Insert(I: BO);
349 }
350 return Builder.CreateNoWrapBinOp(Opc: Opcode, LHS, RHS, IsNUW, IsNSW);
351}
352
353/// expandAddToGEP - Expand an addition expression with a pointer type into
354/// a GEP instead of using ptrtoint+arithmetic+inttoptr. This helps
355/// BasicAliasAnalysis and other passes analyze the result. See the rules
356/// for getelementptr vs. inttoptr in
357/// http://llvm.org/docs/LangRef.html#pointeraliasing
358/// for details.
359///
360/// Design note: The correctness of using getelementptr here depends on
361/// ScalarEvolution not recognizing inttoptr and ptrtoint operators, as
362/// they may introduce pointer arithmetic which may not be safely converted
363/// into getelementptr.
364///
365/// Design note: It might seem desirable for this function to be more
366/// loop-aware. If some of the indices are loop-invariant while others
367/// aren't, it might seem desirable to emit multiple GEPs, keeping the
368/// loop-invariant portions of the overall computation outside the loop.
369/// However, there are a few reasons this is not done here. Hoisting simple
370/// arithmetic is a low-level optimization that often isn't very
371/// important until late in the optimization process. In fact, passes
372/// like InstructionCombining will combine GEPs, even if it means
373/// pushing loop-invariant computation down into loops, so even if the
374/// GEPs were split here, the work would quickly be undone. The
375/// LoopStrengthReduction pass, which is usually run quite late (and
376/// after the last InstructionCombining pass), takes care of hoisting
377/// loop-invariant portions of expressions, after considering what
378/// can be folded using target addressing modes.
379///
380Value *SCEVExpander::expandAddToGEP(const SCEV *Offset, Value *V,
381 SCEV::NoWrapFlags Flags) {
382 assert(!isa<Instruction>(V) ||
383 SE.DT.dominates(cast<Instruction>(V), &*Builder.GetInsertPoint()));
384
385 Value *Idx = expand(S: Offset);
386 GEPNoWrapFlags NW = any(Val: Flags & SCEV::FlagNUW)
387 ? GEPNoWrapFlags::noUnsignedWrap()
388 : GEPNoWrapFlags::none();
389
390 // Fold a GEP with constant operands.
391 if (Constant *CLHS = dyn_cast<Constant>(Val: V))
392 if (Constant *CRHS = dyn_cast<Constant>(Val: Idx))
393 return Builder.CreatePtrAdd(Ptr: CLHS, Offset: CRHS, Name: "", NW);
394
395 // Do a quick scan to see if we have this GEP nearby. If so, reuse it.
396 unsigned ScanLimit = 6;
397 BasicBlock::iterator BlockBegin = Builder.GetInsertBlock()->begin();
398 // Scanning starts from the last instruction before the insertion point.
399 BasicBlock::iterator IP = Builder.GetInsertPoint();
400 if (IP != BlockBegin) {
401 --IP;
402 for (; ScanLimit; --IP, --ScanLimit) {
403 if (auto *GEP = dyn_cast<GetElementPtrInst>(Val&: IP)) {
404 if (GEP->getPointerOperand() == V &&
405 GEP->getSourceElementType() == Builder.getInt8Ty() &&
406 GEP->getOperand(i_nocapture: 1) == Idx) {
407 rememberFlags(I: GEP);
408 GEP->setNoWrapFlags(GEP->getNoWrapFlags() & NW);
409 return &*IP;
410 }
411 }
412 if (IP == BlockBegin) break;
413 }
414 }
415
416 // Save the original insertion point so we can restore it when we're done.
417 SCEVInsertPointGuard Guard(Builder, this);
418
419 // Move the insertion point out of as many loops as we can.
420 while (const Loop *L = SE.LI.getLoopFor(BB: Builder.GetInsertBlock())) {
421 if (!L->isLoopInvariant(V) || !L->isLoopInvariant(V: Idx)) break;
422 BasicBlock *Preheader = L->getLoopPreheader();
423 if (!Preheader) break;
424
425 // Ok, move up a level.
426 Builder.SetInsertPoint(Preheader->getTerminator());
427 }
428
429 // Emit a GEP.
430 return Builder.CreatePtrAdd(Ptr: V, Offset: Idx, Name: "scevgep", NW);
431}
432
433/// PickMostRelevantLoop - Given two loops pick the one that's most relevant for
434/// SCEV expansion. If they are nested, this is the most nested. If they are
435/// neighboring, pick the later.
436static const Loop *PickMostRelevantLoop(const Loop *A, const Loop *B,
437 DominatorTree &DT) {
438 if (!A) return B;
439 if (!B) return A;
440 if (A->contains(L: B)) return B;
441 if (B->contains(L: A)) return A;
442 if (DT.dominates(A: A->getHeader(), B: B->getHeader())) return B;
443 if (DT.dominates(A: B->getHeader(), B: A->getHeader())) return A;
444 return A; // Arbitrarily break the tie.
445}
446
447/// getRelevantLoop - Get the most relevant loop associated with the given
448/// expression, according to PickMostRelevantLoop.
449const Loop *SCEVExpander::getRelevantLoop(const SCEV *S) {
450 // Test whether we've already computed the most relevant loop for this SCEV.
451 auto Pair = RelevantLoops.try_emplace(Key: S);
452 if (!Pair.second)
453 return Pair.first->second;
454
455 switch (S->getSCEVType()) {
456 case scConstant:
457 case scVScale:
458 return nullptr; // A constant has no relevant loops.
459 case scTruncate:
460 case scZeroExtend:
461 case scSignExtend:
462 case scPtrToAddr:
463 case scPtrToInt:
464 case scAddExpr:
465 case scMulExpr:
466 case scUDivExpr:
467 case scAddRecExpr:
468 case scUMaxExpr:
469 case scSMaxExpr:
470 case scUMinExpr:
471 case scSMinExpr:
472 case scSequentialUMinExpr: {
473 const Loop *L = nullptr;
474 if (const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(Val: S))
475 L = AR->getLoop();
476 for (const SCEV *Op : S->operands())
477 L = PickMostRelevantLoop(A: L, B: getRelevantLoop(S: Op), DT&: SE.DT);
478 return RelevantLoops[S] = L;
479 }
480 case scUnknown: {
481 const SCEVUnknown *U = cast<SCEVUnknown>(Val: S);
482 if (const Instruction *I = dyn_cast<Instruction>(Val: U->getValue()))
483 return Pair.first->second = SE.LI.getLoopFor(BB: I->getParent());
484 // A non-instruction has no relevant loops.
485 return nullptr;
486 }
487 case scCouldNotCompute:
488 llvm_unreachable("Attempt to use a SCEVCouldNotCompute object!");
489 }
490 llvm_unreachable("Unexpected SCEV type!");
491}
492
493namespace {
494
495/// LoopCompare - Compare loops by PickMostRelevantLoop.
496class LoopCompare {
497 DominatorTree &DT;
498public:
499 explicit LoopCompare(DominatorTree &dt) : DT(dt) {}
500
501 bool operator()(std::pair<const Loop *, const SCEV *> LHS,
502 std::pair<const Loop *, const SCEV *> RHS) const {
503 // Keep pointer operands sorted at the end.
504 if (LHS.second->getType()->isPointerTy() !=
505 RHS.second->getType()->isPointerTy())
506 return LHS.second->getType()->isPointerTy();
507
508 // Compare loops with PickMostRelevantLoop.
509 if (LHS.first != RHS.first)
510 return PickMostRelevantLoop(A: LHS.first, B: RHS.first, DT) != LHS.first;
511
512 // If one operand is a non-constant negative and the other is not,
513 // put the non-constant negative on the right so that a sub can
514 // be used instead of a negate and add.
515 if (LHS.second->isNonConstantNegative()) {
516 if (!RHS.second->isNonConstantNegative())
517 return false;
518 } else if (RHS.second->isNonConstantNegative())
519 return true;
520
521 // Otherwise they are equivalent according to this comparison.
522 return false;
523 }
524};
525
526}
527
528Value *SCEVExpander::visitAddExpr(SCEVUseT<const SCEVAddExpr *> S) {
529 // Recognize the canonical representation of an unsimplifed urem.
530 const SCEV *URemLHS = nullptr;
531 const SCEV *URemRHS = nullptr;
532 if (match(U: S, P: m_scev_URem(LHS: m_SCEV(V&: URemLHS), RHS: m_SCEV(V&: URemRHS), SE))) {
533 Value *LHS = expand(S: URemLHS);
534 Value *RHS = expand(S: URemRHS);
535 return InsertBinop(Opcode: Instruction::URem, LHS, RHS, Flags: SCEV::FlagAnyWrap,
536 /*IsSafeToHoist*/ false);
537 }
538
539 // Collect all the add operands in a loop, along with their associated loops.
540 // Iterate in reverse so that constants are emitted last, all else equal, and
541 // so that pointer operands are inserted first, which the code below relies on
542 // to form more involved GEPs.
543 SmallVector<std::pair<const Loop *, const SCEV *>, 8> OpsAndLoops;
544 for (const SCEV *Op : reverse(C: S->operands()))
545 OpsAndLoops.push_back(Elt: std::make_pair(x: getRelevantLoop(S: Op), y&: Op));
546
547 // Sort by loop. Use a stable sort so that constants follow non-constants and
548 // pointer operands precede non-pointer operands.
549 llvm::stable_sort(Range&: OpsAndLoops, C: LoopCompare(SE.DT));
550
551 // Emit instructions to add all the operands. Hoist as much as possible
552 // out of loops, and form meaningful getelementptrs where possible.
553 Value *Sum = nullptr;
554 for (auto I = OpsAndLoops.begin(), E = OpsAndLoops.end(); I != E;) {
555 const Loop *CurLoop = I->first;
556 const SCEV *Op = I->second;
557 if (!Sum) {
558 // This is the first operand. Just expand it.
559 Sum = expand(S: Op);
560 ++I;
561 continue;
562 }
563
564 assert(!Op->getType()->isPointerTy() && "Only first op can be pointer");
565 if (isa<PointerType>(Val: Sum->getType())) {
566 // The running sum expression is a pointer. Try to form a getelementptr
567 // at this level with that as the base.
568 SmallVector<SCEVUse, 4> NewOps;
569 for (; I != E && I->first == CurLoop; ++I) {
570 // If the operand is SCEVUnknown and not instructions, peek through
571 // it, to enable more of it to be folded into the GEP.
572 const SCEV *X = I->second;
573 if (const SCEVUnknown *U = dyn_cast<SCEVUnknown>(Val: X))
574 if (!isa<Instruction>(Val: U->getValue()))
575 X = SE.getSCEV(V: U->getValue());
576 NewOps.push_back(Elt: X);
577 }
578 Sum = expandAddToGEP(Offset: SE.getAddExpr(Ops&: NewOps), V: Sum, Flags: S.getNoWrapFlags());
579 } else if (Op->isNonConstantNegative()) {
580 // Instead of doing a negate and add, just do a subtract.
581 Value *W = expand(S: SE.getNegativeSCEV(V: Op));
582 Sum = InsertBinop(Opcode: Instruction::Sub, LHS: Sum, RHS: W, Flags: SCEV::FlagAnyWrap,
583 /*IsSafeToHoist*/ true);
584 ++I;
585 } else {
586 // A simple add.
587 Value *W = expand(S: Op);
588 // Canonicalize a constant to the RHS.
589 if (isa<Constant>(Val: Sum))
590 std::swap(a&: Sum, b&: W);
591 Sum = InsertBinop(Opcode: Instruction::Add, LHS: Sum, RHS: W, Flags: S.getNoWrapFlags(),
592 /*IsSafeToHoist*/ true);
593 ++I;
594 }
595 }
596
597 return Sum;
598}
599
600Value *SCEVExpander::visitMulExpr(SCEVUseT<const SCEVMulExpr *> S) {
601 Type *Ty = S->getType();
602
603 // Collect all the mul operands in a loop, along with their associated loops.
604 // Iterate in reverse so that constants are emitted last, all else equal.
605 SmallVector<std::pair<const Loop *, const SCEV *>, 8> OpsAndLoops;
606 for (const SCEV *Op : reverse(C: S->operands()))
607 OpsAndLoops.push_back(Elt: std::make_pair(x: getRelevantLoop(S: Op), y&: Op));
608
609 // Sort by loop. Use a stable sort so that constants follow non-constants.
610 llvm::stable_sort(Range&: OpsAndLoops, C: LoopCompare(SE.DT));
611
612 // Emit instructions to mul all the operands. Hoist as much as possible
613 // out of loops.
614 Value *Prod = nullptr;
615 auto I = OpsAndLoops.begin();
616
617 // Expand the calculation of X pow N in the following manner:
618 // Let N = P1 + P2 + ... + PK, where all P are powers of 2. Then:
619 // X pow N = (X pow P1) * (X pow P2) * ... * (X pow PK).
620 const auto ExpandOpBinPowN = [this, &I, &OpsAndLoops]() {
621 auto E = I;
622 // Calculate how many times the same operand from the same loop is included
623 // into this power.
624 uint64_t Exponent = 0;
625 const uint64_t MaxExponent = UINT64_MAX >> 1;
626 // No one sane will ever try to calculate such huge exponents, but if we
627 // need this, we stop on UINT64_MAX / 2 because we need to exit the loop
628 // below when the power of 2 exceeds our Exponent, and we want it to be
629 // 1u << 31 at most to not deal with unsigned overflow.
630 while (E != OpsAndLoops.end() && *I == *E && Exponent != MaxExponent) {
631 ++Exponent;
632 ++E;
633 }
634 assert(Exponent > 0 && "Trying to calculate a zeroth exponent of operand?");
635
636 // Calculate powers with exponents 1, 2, 4, 8 etc. and include those of them
637 // that are needed into the result.
638 Value *P = expand(S: I->second);
639 Value *Result = nullptr;
640 if (Exponent & 1)
641 Result = P;
642 for (uint64_t BinExp = 2; BinExp <= Exponent; BinExp <<= 1) {
643 P = InsertBinop(Opcode: Instruction::Mul, LHS: P, RHS: P, Flags: SCEV::FlagAnyWrap,
644 /*IsSafeToHoist*/ true);
645 if (Exponent & BinExp)
646 Result = Result ? InsertBinop(Opcode: Instruction::Mul, LHS: Result, RHS: P,
647 Flags: SCEV::FlagAnyWrap,
648 /*IsSafeToHoist*/ true)
649 : P;
650 }
651
652 I = E;
653 assert(Result && "Nothing was expanded?");
654 return Result;
655 };
656
657 while (I != OpsAndLoops.end()) {
658 if (!Prod) {
659 // This is the first operand. Just expand it.
660 Prod = ExpandOpBinPowN();
661 } else if (I->second->isAllOnesValue()) {
662 // Instead of doing a multiply by negative one, just do a negate.
663 Prod = InsertBinop(Opcode: Instruction::Sub, LHS: Constant::getNullValue(Ty), RHS: Prod,
664 Flags: SCEV::FlagAnyWrap, /*IsSafeToHoist*/ true);
665 ++I;
666 } else {
667 // A simple mul.
668 Value *W = ExpandOpBinPowN();
669 // Canonicalize a constant to the RHS.
670 if (isa<Constant>(Val: Prod)) std::swap(a&: Prod, b&: W);
671 const APInt *RHS;
672 if (match(V: W, P: m_Power2(V&: RHS))) {
673 // Canonicalize Prod*(1<<C) to Prod<<C.
674 assert(!Ty->isVectorTy() && "vector types are not SCEVable");
675 auto NWFlags = S.getNoWrapFlags();
676 // clear nsw flag if shl will produce poison value.
677 if (RHS->logBase2() == RHS->getBitWidth() - 1)
678 NWFlags = ScalarEvolution::clearFlags(Flags: NWFlags, OffFlags: SCEV::FlagNSW);
679 Prod = InsertBinop(Opcode: Instruction::Shl, LHS: Prod,
680 RHS: ConstantInt::get(Ty, V: RHS->logBase2()), Flags: NWFlags,
681 /*IsSafeToHoist*/ true);
682 } else {
683 Prod = InsertBinop(Opcode: Instruction::Mul, LHS: Prod, RHS: W, Flags: S.getNoWrapFlags(),
684 /*IsSafeToHoist*/ true);
685 }
686 }
687 }
688
689 return Prod;
690}
691
692Value *SCEVExpander::visitUDivExpr(SCEVUseT<const SCEVUDivExpr *> S) {
693 Value *LHS = expand(S: S->getLHS());
694 if (const SCEVConstant *SC = dyn_cast<SCEVConstant>(Val: S->getRHS())) {
695 const APInt &RHS = SC->getAPInt();
696 if (RHS.isPowerOf2())
697 return InsertBinop(Opcode: Instruction::LShr, LHS,
698 RHS: ConstantInt::get(Ty: SC->getType(), V: RHS.logBase2()),
699 Flags: SCEV::FlagAnyWrap, /*IsSafeToHoist*/ true);
700 }
701
702 const SCEV *RHSExpr = S->getRHS();
703 Value *RHS = expand(S: RHSExpr);
704 if (SafeUDivMode) {
705 bool GuaranteedNotPoison =
706 ScalarEvolution::isGuaranteedNotToBePoison(Op: RHSExpr);
707 if (!GuaranteedNotPoison)
708 RHS = Builder.CreateFreeze(V: RHS);
709
710 // We need an umax if either RHSExpr is not known to be zero, or if it is
711 // not guaranteed to be non-poison. In the later case, the frozen poison may
712 // be 0.
713 if (!SE.isKnownNonZero(S: RHSExpr) || !GuaranteedNotPoison)
714 RHS = Builder.CreateIntrinsic(RetTy: RHS->getType(), ID: Intrinsic::umax,
715 Args: {RHS, ConstantInt::get(Ty: RHS->getType(), V: 1)});
716 }
717 return InsertBinop(Opcode: Instruction::UDiv, LHS, RHS, Flags: SCEV::FlagAnyWrap,
718 /*IsSafeToHoist*/ SE.isKnownNonZero(S: S->getRHS()));
719}
720
721/// Determine if this is a well-behaved chain of instructions leading back to
722/// the PHI. If so, it may be reused by expanded expressions.
723bool SCEVExpander::isNormalAddRecExprPHI(PHINode *PN, Instruction *IncV,
724 const Loop *L) {
725 if (IncV->getNumOperands() == 0 || isa<PHINode>(Val: IncV) ||
726 (isa<CastInst>(Val: IncV) && !isa<BitCastInst>(Val: IncV)))
727 return false;
728 // If any of the operands don't dominate the insert position, bail.
729 // Addrec operands are always loop-invariant, so this can only happen
730 // if there are instructions which haven't been hoisted.
731 if (L == IVIncInsertLoop) {
732 for (Use &Op : llvm::drop_begin(RangeOrContainer: IncV->operands()))
733 if (Instruction *OInst = dyn_cast<Instruction>(Val&: Op))
734 if (!SE.DT.dominates(Def: OInst, User: IVIncInsertPos))
735 return false;
736 }
737 // Advance to the next instruction.
738 IncV = dyn_cast<Instruction>(Val: IncV->getOperand(i: 0));
739 if (!IncV)
740 return false;
741
742 if (IncV->mayHaveSideEffects())
743 return false;
744
745 if (IncV == PN)
746 return true;
747
748 return isNormalAddRecExprPHI(PN, IncV, L);
749}
750
751/// getIVIncOperand returns an induction variable increment's induction
752/// variable operand.
753///
754/// If allowScale is set, any type of GEP is allowed as long as the nonIV
755/// operands dominate InsertPos.
756///
757/// If allowScale is not set, ensure that a GEP increment conforms to one of the
758/// simple patterns generated by getAddRecExprPHILiterally and
759/// expandAddtoGEP. If the pattern isn't recognized, return NULL.
760Instruction *SCEVExpander::getIVIncOperand(Instruction *IncV,
761 Instruction *InsertPos,
762 bool allowScale) {
763 if (IncV == InsertPos)
764 return nullptr;
765
766 switch (IncV->getOpcode()) {
767 default:
768 return nullptr;
769 // Check for a simple Add/Sub or GEP of a loop invariant step.
770 case Instruction::Add:
771 case Instruction::Sub: {
772 Instruction *OInst = dyn_cast<Instruction>(Val: IncV->getOperand(i: 1));
773 if (!OInst || SE.DT.dominates(Def: OInst, User: InsertPos))
774 return dyn_cast<Instruction>(Val: IncV->getOperand(i: 0));
775 return nullptr;
776 }
777 case Instruction::BitCast:
778 return dyn_cast<Instruction>(Val: IncV->getOperand(i: 0));
779 case Instruction::GetElementPtr:
780 for (Use &U : llvm::drop_begin(RangeOrContainer: IncV->operands())) {
781 if (isa<Constant>(Val: U))
782 continue;
783 if (Instruction *OInst = dyn_cast<Instruction>(Val&: U)) {
784 if (!SE.DT.dominates(Def: OInst, User: InsertPos))
785 return nullptr;
786 }
787 if (allowScale) {
788 // allow any kind of GEP as long as it can be hoisted.
789 continue;
790 }
791 // GEPs produced by SCEVExpander use i8 element type.
792 if (!cast<GEPOperator>(Val: IncV)->getSourceElementType()->isIntegerTy(BitWidth: 8))
793 return nullptr;
794 break;
795 }
796 return dyn_cast<Instruction>(Val: IncV->getOperand(i: 0));
797 }
798}
799
800/// If the insert point of the current builder or any of the builders on the
801/// stack of saved builders has 'I' as its insert point, update it to point to
802/// the instruction after 'I'. This is intended to be used when the instruction
803/// 'I' is being moved. If this fixup is not done and 'I' is moved to a
804/// different block, the inconsistent insert point (with a mismatched
805/// Instruction and Block) can lead to an instruction being inserted in a block
806/// other than its parent.
807void SCEVExpander::fixupInsertPoints(Instruction *I) {
808 BasicBlock::iterator It(*I);
809 BasicBlock::iterator NewInsertPt = std::next(x: It);
810 if (Builder.GetInsertPoint() == It)
811 Builder.SetInsertPoint(&*NewInsertPt);
812 for (auto *InsertPtGuard : InsertPointGuards)
813 if (InsertPtGuard->GetInsertPoint() == It)
814 InsertPtGuard->SetInsertPoint(NewInsertPt);
815}
816
817/// hoistStep - Attempt to hoist a simple IV increment above InsertPos to make
818/// it available to other uses in this loop. Recursively hoist any operands,
819/// until we reach a value that dominates InsertPos.
820bool SCEVExpander::hoistIVInc(Instruction *IncV, Instruction *InsertPos,
821 bool RecomputePoisonFlags) {
822 auto FixupPoisonFlags = [this](Instruction *I) {
823 // Drop flags that are potentially inferred from old context and infer flags
824 // in new context.
825 rememberFlags(I);
826 I->dropPoisonGeneratingFlags();
827 if (auto *OBO = dyn_cast<OverflowingBinaryOperator>(Val: I))
828 if (auto Flags = SE.getStrengthenedNoWrapFlagsFromBinOp(OBO)) {
829 auto *BO = cast<BinaryOperator>(Val: I);
830 BO->setHasNoUnsignedWrap(
831 ScalarEvolution::maskFlags(Flags: *Flags, Mask: SCEV::FlagNUW) == SCEV::FlagNUW);
832 BO->setHasNoSignedWrap(
833 ScalarEvolution::maskFlags(Flags: *Flags, Mask: SCEV::FlagNSW) == SCEV::FlagNSW);
834 }
835 };
836
837 if (SE.DT.dominates(Def: IncV, User: InsertPos)) {
838 if (RecomputePoisonFlags)
839 FixupPoisonFlags(IncV);
840 return true;
841 }
842
843 // InsertPos must itself dominate IncV so that IncV's new position satisfies
844 // its existing users.
845 if (isa<PHINode>(Val: InsertPos) ||
846 !SE.DT.dominates(A: InsertPos->getParent(), B: IncV->getParent()))
847 return false;
848
849 if (!SE.LI.movementPreservesLCSSAForm(Inst: IncV, NewLoc: InsertPos))
850 return false;
851
852 // Check that the chain of IV operands leading back to Phi can be hoisted.
853 SmallVector<Instruction*, 4> IVIncs;
854 for(;;) {
855 Instruction *Oper = getIVIncOperand(IncV, InsertPos, /*allowScale*/true);
856 if (!Oper)
857 return false;
858 // IncV is safe to hoist.
859 IVIncs.push_back(Elt: IncV);
860 IncV = Oper;
861 if (SE.DT.dominates(Def: IncV, User: InsertPos))
862 break;
863 }
864 for (Instruction *I : llvm::reverse(C&: IVIncs)) {
865 fixupInsertPoints(I);
866 I->moveBefore(InsertPos: InsertPos->getIterator());
867 if (RecomputePoisonFlags)
868 FixupPoisonFlags(I);
869 }
870 return true;
871}
872
873bool SCEVExpander::canReuseFlagsFromOriginalIVInc(PHINode *OrigPhi,
874 PHINode *WidePhi,
875 Instruction *OrigInc,
876 Instruction *WideInc) {
877 return match(V: OrigInc, P: m_c_BinOp(L: m_Specific(V: OrigPhi), R: m_Value())) &&
878 match(V: WideInc, P: m_c_BinOp(L: m_Specific(V: WidePhi), R: m_Value())) &&
879 OrigInc->getOpcode() == WideInc->getOpcode();
880}
881
882/// Determine if this cyclic phi is in a form that would have been generated by
883/// LSR. We don't care if the phi was actually expanded in this pass, as long
884/// as it is in a low-cost form, for example, no implied multiplication. This
885/// should match any patterns generated by getAddRecExprPHILiterally and
886/// expandAddtoGEP.
887bool SCEVExpander::isExpandedAddRecExprPHI(PHINode *PN, Instruction *IncV,
888 const Loop *L) {
889 for(Instruction *IVOper = IncV;
890 (IVOper = getIVIncOperand(IncV: IVOper, InsertPos: L->getLoopPreheader()->getTerminator(),
891 /*allowScale=*/false));) {
892 if (IVOper == PN)
893 return true;
894 }
895 return false;
896}
897
898/// expandIVInc - Expand an IV increment at Builder's current InsertPos.
899/// Typically this is the LatchBlock terminator or IVIncInsertPos, but we may
900/// need to materialize IV increments elsewhere to handle difficult situations.
901Value *SCEVExpander::expandIVInc(PHINode *PN, Value *StepV, const Loop *L,
902 bool useSubtract) {
903 Value *IncV;
904 // If the PHI is a pointer, use a GEP, otherwise use an add or sub.
905 if (PN->getType()->isPointerTy()) {
906 // TODO: Change name to IVName.iv.next.
907 IncV = Builder.CreatePtrAdd(Ptr: PN, Offset: StepV, Name: "scevgep");
908 } else {
909 IncV = useSubtract ?
910 Builder.CreateSub(LHS: PN, RHS: StepV, Name: Twine(IVName) + ".iv.next") :
911 Builder.CreateAdd(LHS: PN, RHS: StepV, Name: Twine(IVName) + ".iv.next");
912 }
913 return IncV;
914}
915
916/// Check whether we can cheaply express the requested SCEV in terms of
917/// the available PHI SCEV by truncation and/or inversion of the step.
918static bool canBeCheaplyTransformed(ScalarEvolution &SE,
919 const SCEVAddRecExpr *Phi,
920 const SCEVAddRecExpr *Requested,
921 bool &InvertStep) {
922 // We can't transform to match a pointer PHI.
923 Type *PhiTy = Phi->getType();
924 Type *RequestedTy = Requested->getType();
925 if (PhiTy->isPointerTy() || RequestedTy->isPointerTy())
926 return false;
927
928 if (RequestedTy->getIntegerBitWidth() > PhiTy->getIntegerBitWidth())
929 return false;
930
931 // Try truncate it if necessary.
932 Phi = dyn_cast<SCEVAddRecExpr>(Val: SE.getTruncateOrNoop(V: Phi, Ty: RequestedTy));
933 if (!Phi)
934 return false;
935
936 // Check whether truncation will help.
937 if (Phi == Requested) {
938 InvertStep = false;
939 return true;
940 }
941
942 // Check whether inverting will help: {R,+,-1} == R - {0,+,1}.
943 if (SE.getMinusSCEV(LHS: Requested->getStart(), RHS: Requested) == Phi) {
944 InvertStep = true;
945 return true;
946 }
947
948 return false;
949}
950
951static bool IsIncrementNSW(ScalarEvolution &SE, const SCEVAddRecExpr *AR) {
952 if (!isa<IntegerType>(Val: AR->getType()))
953 return false;
954
955 unsigned BitWidth = cast<IntegerType>(Val: AR->getType())->getBitWidth();
956 Type *WideTy = IntegerType::get(C&: AR->getType()->getContext(), NumBits: BitWidth * 2);
957 const SCEV *Step = AR->getStepRecurrence(SE);
958 const SCEV *OpAfterExtend = SE.getAddExpr(LHS: SE.getSignExtendExpr(Op: Step, Ty: WideTy),
959 RHS: SE.getSignExtendExpr(Op: AR, Ty: WideTy));
960 const SCEV *ExtendAfterOp =
961 SE.getSignExtendExpr(Op: SE.getAddExpr(LHS: AR, RHS: Step), Ty: WideTy);
962 return ExtendAfterOp == OpAfterExtend;
963}
964
965static bool IsIncrementNUW(ScalarEvolution &SE, const SCEVAddRecExpr *AR) {
966 if (!isa<IntegerType>(Val: AR->getType()))
967 return false;
968
969 unsigned BitWidth = cast<IntegerType>(Val: AR->getType())->getBitWidth();
970 Type *WideTy = IntegerType::get(C&: AR->getType()->getContext(), NumBits: BitWidth * 2);
971 const SCEV *Step = AR->getStepRecurrence(SE);
972 const SCEV *OpAfterExtend = SE.getAddExpr(LHS: SE.getZeroExtendExpr(Op: Step, Ty: WideTy),
973 RHS: SE.getZeroExtendExpr(Op: AR, Ty: WideTy));
974 const SCEV *ExtendAfterOp =
975 SE.getZeroExtendExpr(Op: SE.getAddExpr(LHS: AR, RHS: Step), Ty: WideTy);
976 return ExtendAfterOp == OpAfterExtend;
977}
978
979/// getAddRecExprPHILiterally - Helper for expandAddRecExprLiterally. Expand
980/// the base addrec, which is the addrec without any non-loop-dominating
981/// values, and return the PHI.
982PHINode *
983SCEVExpander::getAddRecExprPHILiterally(const SCEVAddRecExpr *Normalized,
984 const Loop *L, Type *&TruncTy,
985 bool &InvertStep) {
986 assert((!IVIncInsertLoop || IVIncInsertPos) &&
987 "Uninitialized insert position");
988
989 // Reuse a previously-inserted PHI, if present.
990 BasicBlock *LatchBlock = L->getLoopLatch();
991 if (LatchBlock) {
992 PHINode *AddRecPhiMatch = nullptr;
993 Instruction *IncV = nullptr;
994 TruncTy = nullptr;
995 InvertStep = false;
996
997 // Only try partially matching scevs that need truncation and/or
998 // step-inversion if we know this loop is outside the current loop.
999 bool TryNonMatchingSCEV =
1000 IVIncInsertLoop &&
1001 SE.DT.properlyDominates(A: LatchBlock, B: IVIncInsertLoop->getHeader());
1002
1003 for (PHINode &PN : L->getHeader()->phis()) {
1004 if (!SE.isSCEVable(Ty: PN.getType()))
1005 continue;
1006
1007 // We should not look for a incomplete PHI. Getting SCEV for a incomplete
1008 // PHI has no meaning at all.
1009 if (!PN.isComplete()) {
1010 SCEV_DEBUG_WITH_TYPE(
1011 DebugType, dbgs() << "One incomplete PHI is found: " << PN << "\n");
1012 continue;
1013 }
1014
1015 const SCEVAddRecExpr *PhiSCEV = dyn_cast<SCEVAddRecExpr>(Val: SE.getSCEV(V: &PN));
1016 if (!PhiSCEV)
1017 continue;
1018
1019 bool IsMatchingSCEV = PhiSCEV == Normalized;
1020 // We only handle truncation and inversion of phi recurrences for the
1021 // expanded expression if the expanded expression's loop dominates the
1022 // loop we insert to. Check now, so we can bail out early.
1023 if (!IsMatchingSCEV && !TryNonMatchingSCEV)
1024 continue;
1025
1026 // TODO: this possibly can be reworked to avoid this cast at all.
1027 Instruction *TempIncV =
1028 dyn_cast<Instruction>(Val: PN.getIncomingValueForBlock(BB: LatchBlock));
1029 if (!TempIncV)
1030 continue;
1031
1032 // Check whether we can reuse this PHI node.
1033 if (LSRMode) {
1034 if (!isExpandedAddRecExprPHI(PN: &PN, IncV: TempIncV, L))
1035 continue;
1036 } else {
1037 if (!isNormalAddRecExprPHI(PN: &PN, IncV: TempIncV, L))
1038 continue;
1039 }
1040
1041 // Stop if we have found an exact match SCEV.
1042 if (IsMatchingSCEV) {
1043 IncV = TempIncV;
1044 TruncTy = nullptr;
1045 InvertStep = false;
1046 AddRecPhiMatch = &PN;
1047 break;
1048 }
1049
1050 // Try whether the phi can be translated into the requested form
1051 // (truncated and/or offset by a constant).
1052 if ((!TruncTy || InvertStep) &&
1053 canBeCheaplyTransformed(SE, Phi: PhiSCEV, Requested: Normalized, InvertStep)) {
1054 // Record the phi node. But don't stop we might find an exact match
1055 // later.
1056 AddRecPhiMatch = &PN;
1057 IncV = TempIncV;
1058 TruncTy = Normalized->getType();
1059 }
1060 }
1061
1062 if (AddRecPhiMatch) {
1063 // Ok, the add recurrence looks usable.
1064 // Remember this PHI, even in post-inc mode.
1065 InsertedValues.insert(V: AddRecPhiMatch);
1066 // Remember the increment.
1067 rememberInstruction(I: IncV);
1068 // Those values were not actually inserted but re-used.
1069 ReusedValues.insert(Ptr: AddRecPhiMatch);
1070 ReusedValues.insert(Ptr: IncV);
1071 return AddRecPhiMatch;
1072 }
1073 }
1074
1075 // Save the original insertion point so we can restore it when we're done.
1076 SCEVInsertPointGuard Guard(Builder, this);
1077
1078 // Another AddRec may need to be recursively expanded below. For example, if
1079 // this AddRec is quadratic, the StepV may itself be an AddRec in this
1080 // loop. Remove this loop from the PostIncLoops set before expanding such
1081 // AddRecs. Otherwise, we cannot find a valid position for the step
1082 // (i.e. StepV can never dominate its loop header). Ideally, we could do
1083 // SavedIncLoops.swap(PostIncLoops), but we generally have a single element,
1084 // so it's not worth implementing SmallPtrSet::swap.
1085 PostIncLoopSet SavedPostIncLoops = PostIncLoops;
1086 PostIncLoops.clear();
1087
1088 // Expand code for the start value into the loop preheader.
1089 assert(L->getLoopPreheader() &&
1090 "Can't expand add recurrences without a loop preheader!");
1091 Value *StartV =
1092 expand(S: Normalized->getStart(), I: L->getLoopPreheader()->getTerminator());
1093
1094 // StartV must have been be inserted into L's preheader to dominate the new
1095 // phi.
1096 assert(!isa<Instruction>(StartV) ||
1097 SE.DT.properlyDominates(cast<Instruction>(StartV)->getParent(),
1098 L->getHeader()));
1099
1100 // Expand code for the step value. Do this before creating the PHI so that PHI
1101 // reuse code doesn't see an incomplete PHI.
1102 const SCEV *Step = Normalized->getStepRecurrence(SE);
1103 Type *ExpandTy = Normalized->getType();
1104 // If the stride is negative, insert a sub instead of an add for the increment
1105 // (unless it's a constant, because subtracts of constants are canonicalized
1106 // to adds).
1107 bool useSubtract = !ExpandTy->isPointerTy() && Step->isNonConstantNegative();
1108 if (useSubtract)
1109 Step = SE.getNegativeSCEV(V: Step);
1110 // Expand the step somewhere that dominates the loop header.
1111 Value *StepV = expand(S: Step, I: L->getHeader()->getFirstInsertionPt());
1112
1113 // The no-wrap behavior proved by IsIncrement(NUW|NSW) is only applicable if
1114 // we actually do emit an addition. It does not apply if we emit a
1115 // subtraction.
1116 bool IncrementIsNUW = !useSubtract && IsIncrementNUW(SE, AR: Normalized);
1117 bool IncrementIsNSW = !useSubtract && IsIncrementNSW(SE, AR: Normalized);
1118
1119 // Create the PHI.
1120 BasicBlock *Header = L->getHeader();
1121 Builder.SetInsertPoint(TheBB: Header, IP: Header->begin());
1122 PHINode *PN =
1123 Builder.CreatePHI(Ty: ExpandTy, NumReservedValues: pred_size(BB: Header), Name: Twine(IVName) + ".iv");
1124
1125 // Create the step instructions and populate the PHI.
1126 for (BasicBlock *Pred : predecessors(BB: Header)) {
1127 // Add a start value.
1128 if (!L->contains(BB: Pred)) {
1129 PN->addIncoming(V: StartV, BB: Pred);
1130 continue;
1131 }
1132
1133 // Create a step value and add it to the PHI.
1134 // If IVIncInsertLoop is non-null and equal to the addrec's loop, insert the
1135 // instructions at IVIncInsertPos.
1136 Instruction *InsertPos = L == IVIncInsertLoop ?
1137 IVIncInsertPos : Pred->getTerminator();
1138 Builder.SetInsertPoint(InsertPos);
1139 Value *IncV = expandIVInc(PN, StepV, L, useSubtract);
1140
1141 if (isa<OverflowingBinaryOperator>(Val: IncV)) {
1142 if (IncrementIsNUW)
1143 cast<BinaryOperator>(Val: IncV)->setHasNoUnsignedWrap();
1144 if (IncrementIsNSW)
1145 cast<BinaryOperator>(Val: IncV)->setHasNoSignedWrap();
1146 }
1147 PN->addIncoming(V: IncV, BB: Pred);
1148 }
1149
1150 // After expanding subexpressions, restore the PostIncLoops set so the caller
1151 // can ensure that IVIncrement dominates the current uses.
1152 PostIncLoops = SavedPostIncLoops;
1153
1154 // Remember this PHI, even in post-inc mode. LSR SCEV-based salvaging is most
1155 // effective when we are able to use an IV inserted here, so record it.
1156 InsertedValues.insert(V: PN);
1157 InsertedIVs.push_back(Elt: PN);
1158 return PN;
1159}
1160
1161Value *
1162SCEVExpander::expandAddRecExprLiterally(SCEVUseT<const SCEVAddRecExpr *> S) {
1163 const Loop *L = S->getLoop();
1164
1165 // Determine a normalized form of this expression, which is the expression
1166 // before any post-inc adjustment is made.
1167 const SCEVAddRecExpr *Normalized = S;
1168 if (PostIncLoops.count(Ptr: L)) {
1169 PostIncLoopSet Loops;
1170 Loops.insert(Ptr: L);
1171 Normalized = cast<SCEVAddRecExpr>(
1172 Val: normalizeForPostIncUse(S, Loops, SE, /*CheckInvertible=*/false));
1173 }
1174
1175 [[maybe_unused]] const SCEV *Start = Normalized->getStart();
1176 const SCEV *Step = Normalized->getStepRecurrence(SE);
1177 assert(SE.properlyDominates(Start, L->getHeader()) &&
1178 "Start does not properly dominate loop header");
1179 assert(SE.dominates(Step, L->getHeader()) && "Step not dominate loop header");
1180
1181 // In some cases, we decide to reuse an existing phi node but need to truncate
1182 // it and/or invert the step.
1183 Type *TruncTy = nullptr;
1184 bool InvertStep = false;
1185 PHINode *PN = getAddRecExprPHILiterally(Normalized, L, TruncTy, InvertStep);
1186
1187 // Accommodate post-inc mode, if necessary.
1188 Value *Result;
1189 if (!PostIncLoops.count(Ptr: L))
1190 Result = PN;
1191 else {
1192 // In PostInc mode, use the post-incremented value.
1193 BasicBlock *LatchBlock = L->getLoopLatch();
1194 assert(LatchBlock && "PostInc mode requires a unique loop latch!");
1195 Result = PN->getIncomingValueForBlock(BB: LatchBlock);
1196
1197 // We might be introducing a new use of the post-inc IV that is not poison
1198 // safe, in which case we should drop poison generating flags. Only keep
1199 // those flags for which SCEV has proven that they always hold.
1200 if (isa<OverflowingBinaryOperator>(Val: Result)) {
1201 auto *I = cast<Instruction>(Val: Result);
1202 if (!S->hasNoUnsignedWrap())
1203 I->setHasNoUnsignedWrap(false);
1204 if (!S->hasNoSignedWrap())
1205 I->setHasNoSignedWrap(false);
1206 }
1207
1208 // For an expansion to use the postinc form, the client must call
1209 // expandCodeFor with an InsertPoint that is either outside the PostIncLoop
1210 // or dominated by IVIncInsertPos.
1211 if (isa<Instruction>(Val: Result) &&
1212 !SE.DT.dominates(Def: cast<Instruction>(Val: Result),
1213 User: &*Builder.GetInsertPoint())) {
1214 // The induction variable's postinc expansion does not dominate this use.
1215 // IVUsers tries to prevent this case, so it is rare. However, it can
1216 // happen when an IVUser outside the loop is not dominated by the latch
1217 // block. Adjusting IVIncInsertPos before expansion begins cannot handle
1218 // all cases. Consider a phi outside whose operand is replaced during
1219 // expansion with the value of the postinc user. Without fundamentally
1220 // changing the way postinc users are tracked, the only remedy is
1221 // inserting an extra IV increment. StepV might fold into PostLoopOffset,
1222 // but hopefully expandCodeFor handles that.
1223 bool useSubtract =
1224 !S->getType()->isPointerTy() && Step->isNonConstantNegative();
1225 if (useSubtract)
1226 Step = SE.getNegativeSCEV(V: Step);
1227 Value *StepV;
1228 {
1229 // Expand the step somewhere that dominates the loop header.
1230 SCEVInsertPointGuard Guard(Builder, this);
1231 StepV = expand(S: Step, I: L->getHeader()->getFirstInsertionPt());
1232 }
1233 Result = expandIVInc(PN, StepV, L, useSubtract);
1234 }
1235 }
1236
1237 // We have decided to reuse an induction variable of a dominating loop. Apply
1238 // truncation and/or inversion of the step.
1239 if (TruncTy) {
1240 if (TruncTy != Result->getType() || InvertStep)
1241 Result = fixupLCSSAFormFor(V: Result);
1242 // Truncate the result.
1243 if (TruncTy != Result->getType())
1244 Result = Builder.CreateTrunc(V: Result, DestTy: TruncTy);
1245
1246 // Invert the result.
1247 if (InvertStep)
1248 Result = Builder.CreateSub(LHS: expand(S: Normalized->getStart()), RHS: Result);
1249 }
1250
1251 return Result;
1252}
1253
1254Value *SCEVExpander::tryToReuseLCSSAPhi(SCEVUseT<const SCEVAddRecExpr *> S) {
1255 Type *STy = S->getType();
1256 const Loop *L = S->getLoop();
1257 BasicBlock *EB = L->getExitBlock();
1258 if (!EB || !EB->getSinglePredecessor() ||
1259 !SE.DT.dominates(A: EB, B: Builder.GetInsertBlock()))
1260 return nullptr;
1261
1262 // Helper to check if the diff between S and ExitSCEV is simple enough to
1263 // allow reusing the LCSSA phi.
1264 auto CanReuse = [&](const SCEV *ExitSCEV) -> const SCEV * {
1265 if (isa<SCEVCouldNotCompute>(Val: ExitSCEV))
1266 return nullptr;
1267 const SCEV *Diff = SE.getMinusSCEV(LHS: S, RHS: ExitSCEV);
1268 const SCEV *Op = Diff;
1269 match(S: Op, P: m_scev_Add(Op0: m_SCEVConstant(), Op1: m_SCEV(V&: Op)));
1270 match(S: Op, P: m_scev_Mul(Op0: m_scev_AllOnes(), Op1: m_SCEV(V&: Op)));
1271 match(S: Op, P: m_scev_PtrToAddr(Op0: m_SCEV(V&: Op))) ||
1272 match(S: Op, P: m_scev_PtrToInt(Op0: m_SCEV(V&: Op)));
1273 if (!isa<SCEVConstant, SCEVUnknown>(Val: Op))
1274 return nullptr;
1275 return Diff;
1276 };
1277
1278 for (auto &PN : EB->phis()) {
1279 if (!SE.isSCEVable(Ty: PN.getType()))
1280 continue;
1281 auto *ExitSCEV = SE.getSCEV(V: &PN);
1282 if (!isa<SCEVAddRecExpr>(Val: ExitSCEV))
1283 continue;
1284 Type *PhiTy = PN.getType();
1285 const SCEV *Diff = nullptr;
1286 if (STy->isIntegerTy() && PhiTy->isPointerTy() &&
1287 DL.getAddressType(PtrTy: PhiTy) == STy) {
1288 // Prefer ptrtoaddr over ptrtoint.
1289 const SCEV *AddrSCEV = SE.getPtrToAddrExpr(Op: ExitSCEV);
1290 Diff = CanReuse(AddrSCEV);
1291 if (!Diff) {
1292 const SCEV *IntSCEV = SE.getPtrToIntExpr(Op: ExitSCEV, Ty: STy);
1293 Diff = CanReuse(IntSCEV);
1294 }
1295 } else if (STy == PhiTy) {
1296 Diff = CanReuse(ExitSCEV);
1297 }
1298 if (!Diff)
1299 continue;
1300
1301 assert(Diff->getType()->isIntegerTy() &&
1302 "difference must be of integer type");
1303 Value *DiffV = expand(S: Diff);
1304 Value *BaseV = fixupLCSSAFormFor(V: &PN);
1305 if (PhiTy->isPointerTy()) {
1306 if (STy->isPointerTy())
1307 return Builder.CreatePtrAdd(Ptr: BaseV, Offset: DiffV);
1308 BaseV = Builder.CreatePtrToAddr(V: BaseV);
1309 }
1310 return Builder.CreateAdd(LHS: BaseV, RHS: DiffV);
1311 }
1312
1313 return nullptr;
1314}
1315
1316Value *SCEVExpander::visitAddRecExpr(SCEVUseT<const SCEVAddRecExpr *> S) {
1317 // In canonical mode we compute the addrec as an expression of a canonical IV
1318 // using evaluateAtIteration and expand the resulting SCEV expression. This
1319 // way we avoid introducing new IVs to carry on the computation of the addrec
1320 // throughout the loop.
1321 //
1322 // For nested addrecs evaluateAtIteration might need a canonical IV of a
1323 // type wider than the addrec itself. Emitting a canonical IV of the
1324 // proper type might produce non-legal types, for example expanding an i64
1325 // {0,+,2,+,1} addrec would need an i65 canonical IV. To avoid this just fall
1326 // back to non-canonical mode for nested addrecs.
1327 if (!CanonicalMode || (S->getNumOperands() > 2))
1328 return expandAddRecExprLiterally(S);
1329
1330 Type *Ty = SE.getEffectiveSCEVType(Ty: S->getType());
1331 const Loop *L = S->getLoop();
1332
1333 // First check for an existing canonical IV in a suitable type.
1334 PHINode *CanonicalIV = nullptr;
1335 if (PHINode *PN = L->getCanonicalInductionVariable())
1336 if (SE.getTypeSizeInBits(Ty: PN->getType()) >= SE.getTypeSizeInBits(Ty))
1337 CanonicalIV = PN;
1338
1339 // Rewrite an AddRec in terms of the canonical induction variable, if
1340 // its type is more narrow.
1341 if (CanonicalIV &&
1342 SE.getTypeSizeInBits(Ty: CanonicalIV->getType()) > SE.getTypeSizeInBits(Ty) &&
1343 !S->getType()->isPointerTy()) {
1344 SmallVector<SCEVUse, 4> NewOps(S->getNumOperands());
1345 for (unsigned i = 0, e = S->getNumOperands(); i != e; ++i)
1346 NewOps[i] = SE.getAnyExtendExpr(Op: S->getOperand(i), Ty: CanonicalIV->getType());
1347 Value *V = expand(
1348 S: SE.getAddRecExpr(Operands&: NewOps, L: S->getLoop(), Flags: S.getNoWrapFlags(Mask: SCEV::FlagNW)));
1349 BasicBlock::iterator NewInsertPt =
1350 isa<Instruction>(Val: V) ? findInsertPointAfter(I: cast<Instruction>(Val: V),
1351 MustDominate: &*Builder.GetInsertPoint())
1352 : Builder.GetInsertPoint();
1353 V = expand(S: SE.getTruncateExpr(Op: SE.getUnknown(V), Ty), I: NewInsertPt);
1354 return V;
1355 }
1356
1357 // If S is expanded outside the defining loop, check if there is a
1358 // matching LCSSA phi node for it.
1359 if (Value *V = tryToReuseLCSSAPhi(S))
1360 return V;
1361
1362 // {X,+,F} --> X + {0,+,F}
1363 if (!S->getStart()->isZero()) {
1364 if (isa<PointerType>(Val: S->getType())) {
1365 Value *StartV = expand(S: SE.getPointerBase(V: S));
1366 return expandAddToGEP(Offset: SE.removePointerBase(S), V: StartV,
1367 Flags: S.getNoWrapFlags(Mask: SCEV::FlagNUW));
1368 }
1369
1370 SmallVector<SCEVUse, 4> NewOps(S->operands());
1371 NewOps[0] = SE.getConstant(Ty, V: 0);
1372 const SCEV *Rest =
1373 SE.getAddRecExpr(Operands&: NewOps, L, Flags: S.getNoWrapFlags(Mask: SCEV::FlagNW));
1374
1375 // Just do a normal add. Pre-expand the operands to suppress folding.
1376 //
1377 // The LHS and RHS values are factored out of the expand call to make the
1378 // output independent of the argument evaluation order.
1379 const SCEV *AddExprLHS = SE.getUnknown(V: expand(S: S->getStart()));
1380 const SCEV *AddExprRHS = SE.getUnknown(V: expand(S: Rest));
1381 return expand(S: SE.getAddExpr(LHS: AddExprLHS, RHS: AddExprRHS));
1382 }
1383
1384 // If we don't yet have a canonical IV, create one.
1385 if (!CanonicalIV) {
1386 // Create and insert the PHI node for the induction variable in the
1387 // specified loop.
1388 BasicBlock *Header = L->getHeader();
1389 pred_iterator HPB = pred_begin(BB: Header), HPE = pred_end(BB: Header);
1390 CanonicalIV = PHINode::Create(Ty, NumReservedValues: std::distance(first: HPB, last: HPE), NameStr: "indvar");
1391 CanonicalIV->insertBefore(InsertPos: Header->begin());
1392 rememberInstruction(I: CanonicalIV);
1393
1394 SmallPtrSet<BasicBlock *, 4> PredSeen;
1395 Constant *One = ConstantInt::get(Ty, V: 1);
1396 for (pred_iterator HPI = HPB; HPI != HPE; ++HPI) {
1397 BasicBlock *HP = *HPI;
1398 if (!PredSeen.insert(Ptr: HP).second) {
1399 // There must be an incoming value for each predecessor, even the
1400 // duplicates!
1401 CanonicalIV->addIncoming(V: CanonicalIV->getIncomingValueForBlock(BB: HP), BB: HP);
1402 continue;
1403 }
1404
1405 if (L->contains(BB: HP)) {
1406 // Insert a unit add instruction right before the terminator
1407 // corresponding to the back-edge.
1408 Instruction *Add = BinaryOperator::CreateAdd(V1: CanonicalIV, V2: One,
1409 Name: "indvar.next",
1410 InsertBefore: HP->getTerminator()->getIterator());
1411 Add->setDebugLoc(HP->getTerminator()->getDebugLoc());
1412 rememberInstruction(I: Add);
1413 CanonicalIV->addIncoming(V: Add, BB: HP);
1414 } else {
1415 CanonicalIV->addIncoming(V: Constant::getNullValue(Ty), BB: HP);
1416 }
1417 }
1418 }
1419
1420 // {0,+,1} --> Insert a canonical induction variable into the loop!
1421 if (S->isAffine() && S->getOperand(i: 1)->isOne()) {
1422 assert(Ty == SE.getEffectiveSCEVType(CanonicalIV->getType()) &&
1423 "IVs with types different from the canonical IV should "
1424 "already have been handled!");
1425 return CanonicalIV;
1426 }
1427
1428 // {0,+,F} --> {0,+,1} * F
1429
1430 // If this is a simple linear addrec, emit it now as a special case.
1431 if (S->isAffine()) // {0,+,F} --> i*F
1432 return
1433 expand(S: SE.getTruncateOrNoop(
1434 V: SE.getMulExpr(LHS: SE.getUnknown(V: CanonicalIV),
1435 RHS: SE.getNoopOrAnyExtend(V: S->getOperand(i: 1),
1436 Ty: CanonicalIV->getType())),
1437 Ty));
1438
1439 // If this is a chain of recurrences, turn it into a closed form, using the
1440 // folders, then expandCodeFor the closed form. This allows the folders to
1441 // simplify the expression without having to build a bunch of special code
1442 // into this folder.
1443 const SCEV *IH = SE.getUnknown(V: CanonicalIV); // Get I as a "symbolic" SCEV.
1444
1445 // Promote S up to the canonical IV type, if the cast is foldable.
1446 const SCEV *NewS = S;
1447 const SCEV *Ext = SE.getNoopOrAnyExtend(V: S, Ty: CanonicalIV->getType());
1448 if (isa<SCEVAddRecExpr>(Val: Ext))
1449 NewS = Ext;
1450
1451 const SCEV *V = cast<SCEVAddRecExpr>(Val: NewS)->evaluateAtIteration(It: IH, SE);
1452
1453 // Truncate the result down to the original type, if needed.
1454 const SCEV *T = SE.getTruncateOrNoop(V, Ty);
1455 return expand(S: T);
1456}
1457
1458Value *SCEVExpander::visitPtrToAddrExpr(SCEVUseT<const SCEVPtrToAddrExpr *> S) {
1459 Value *V = expand(S: S->getOperand());
1460 Type *Ty = S->getType();
1461
1462 // ptrtoaddr and ptrtoint produce the same value, so try to reuse either.
1463 if (!isa<Constant>(Val: V)) {
1464 BasicBlock::iterator BIP = Builder.GetInsertPoint();
1465 for (User *U : V->users()) {
1466 auto *CI = dyn_cast<CastInst>(Val: U);
1467 if (CI && CI->getType() == Ty &&
1468 (CI->getOpcode() == CastInst::PtrToAddr ||
1469 CI->getOpcode() == CastInst::PtrToInt) &&
1470 &*BIP != CI && SE.DT.dominates(Def: CI, User: &*BIP))
1471 return CI;
1472 }
1473 }
1474 return ReuseOrCreateCast(V, Ty, Op: CastInst::PtrToAddr,
1475 IP: GetOptimalInsertionPointForCastOf(V));
1476}
1477
1478Value *SCEVExpander::visitPtrToIntExpr(SCEVUseT<const SCEVPtrToIntExpr *> S) {
1479 Value *V = expand(S: S->getOperand());
1480 return ReuseOrCreateCast(V, Ty: S->getType(), Op: CastInst::PtrToInt,
1481 IP: GetOptimalInsertionPointForCastOf(V));
1482}
1483
1484Value *SCEVExpander::visitTruncateExpr(SCEVUseT<const SCEVTruncateExpr *> S) {
1485 Value *V = expand(S: S->getOperand());
1486 return Builder.CreateTrunc(V, DestTy: S->getType());
1487}
1488
1489Value *
1490SCEVExpander::visitZeroExtendExpr(SCEVUseT<const SCEVZeroExtendExpr *> S) {
1491 Value *V = expand(S: S->getOperand());
1492 return Builder.CreateZExt(V, DestTy: S->getType(), Name: "",
1493 IsNonNeg: SE.isKnownNonNegative(S: S->getOperand()));
1494}
1495
1496Value *
1497SCEVExpander::visitSignExtendExpr(SCEVUseT<const SCEVSignExtendExpr *> S) {
1498 Value *V = expand(S: S->getOperand());
1499 return Builder.CreateSExt(V, DestTy: S->getType());
1500}
1501
1502Value *SCEVExpander::expandMinMaxExpr(SCEVUseT<const SCEVNAryExpr *> S,
1503 Intrinsic::ID IntrinID, Twine Name,
1504 bool IsSequential) {
1505 bool PrevSafeMode = SafeUDivMode;
1506 SafeUDivMode |= IsSequential;
1507 Value *LHS = expand(S: S->getOperand(i: S->getNumOperands() - 1));
1508 Type *Ty = LHS->getType();
1509 if (IsSequential)
1510 LHS = Builder.CreateFreeze(V: LHS);
1511 for (int i = S->getNumOperands() - 2; i >= 0; --i) {
1512 SafeUDivMode = (IsSequential && i != 0) || PrevSafeMode;
1513 Value *RHS = expand(S: S->getOperand(i));
1514 if (IsSequential && i != 0)
1515 RHS = Builder.CreateFreeze(V: RHS);
1516 Value *Sel;
1517 if (Ty->isIntegerTy())
1518 Sel = Builder.CreateIntrinsic(ID: IntrinID, OverloadTypes: {Ty}, Args: {LHS, RHS},
1519 /*FMFSource=*/nullptr, Name);
1520 else {
1521 Value *ICmp =
1522 Builder.CreateICmp(P: MinMaxIntrinsic::getPredicate(ID: IntrinID), LHS, RHS);
1523 Sel = Builder.CreateSelect(C: ICmp, True: LHS, False: RHS, Name);
1524 }
1525 LHS = Sel;
1526 }
1527 SafeUDivMode = PrevSafeMode;
1528 return LHS;
1529}
1530
1531Value *SCEVExpander::visitSMaxExpr(SCEVUseT<const SCEVSMaxExpr *> S) {
1532 return expandMinMaxExpr(S, IntrinID: Intrinsic::smax, Name: "smax");
1533}
1534
1535Value *SCEVExpander::visitUMaxExpr(SCEVUseT<const SCEVUMaxExpr *> S) {
1536 return expandMinMaxExpr(S, IntrinID: Intrinsic::umax, Name: "umax");
1537}
1538
1539Value *SCEVExpander::visitSMinExpr(SCEVUseT<const SCEVSMinExpr *> S) {
1540 return expandMinMaxExpr(S, IntrinID: Intrinsic::smin, Name: "smin");
1541}
1542
1543Value *SCEVExpander::visitUMinExpr(SCEVUseT<const SCEVUMinExpr *> S) {
1544 return expandMinMaxExpr(S, IntrinID: Intrinsic::umin, Name: "umin");
1545}
1546
1547Value *SCEVExpander::visitSequentialUMinExpr(
1548 SCEVUseT<const SCEVSequentialUMinExpr *> S) {
1549 return expandMinMaxExpr(S, IntrinID: Intrinsic::umin, Name: "umin",
1550 /*IsSequential*/ true);
1551}
1552
1553Value *SCEVExpander::visitVScale(SCEVUseT<const SCEVVScale *> S) {
1554 return Builder.CreateVScale(Ty: S->getType());
1555}
1556
1557Value *SCEVExpander::expandCodeFor(SCEVUse SH, Type *Ty,
1558 BasicBlock::iterator IP) {
1559 setInsertPoint(IP);
1560 return expandCodeFor(SH, Ty);
1561}
1562
1563Value *SCEVExpander::expandCodeFor(SCEVUse SH, Type *Ty) {
1564 // Expand the code for this SCEV.
1565 Value *V = expand(S: SH);
1566
1567 if (Ty && Ty != V->getType()) {
1568 assert(SE.getTypeSizeInBits(Ty) == SE.getTypeSizeInBits(SH->getType()) &&
1569 "non-trivial casts should be done with the SCEVs directly!");
1570 V = InsertNoopCastOfTo(V, Ty);
1571 }
1572 return V;
1573}
1574
1575Value *SCEVExpander::FindValueInExprValueMap(
1576 SCEVUse S, const Instruction *InsertPt,
1577 SmallVectorImpl<Instruction *> &DropPoisonGeneratingInsts) {
1578 // If the expansion is not in CanonicalMode, and the SCEV contains any
1579 // sub scAddRecExpr type SCEV, it is required to expand the SCEV literally.
1580 if (!CanonicalMode && SE.containsAddRecurrence(S))
1581 return nullptr;
1582
1583 // If S is a constant or unknown, it may be worse to reuse an existing Value.
1584 if (isa<SCEVConstant>(Val: S) || isa<SCEVUnknown>(Val: S))
1585 return nullptr;
1586
1587 for (Value *V : SE.getSCEVValues(S)) {
1588 Instruction *EntInst = dyn_cast<Instruction>(Val: V);
1589 if (!EntInst)
1590 continue;
1591
1592 // Choose a Value from the set which dominates the InsertPt.
1593 // InsertPt should be inside the Value's parent loop so as not to break
1594 // the LCSSA form.
1595 assert(EntInst->getFunction() == InsertPt->getFunction());
1596 if (S->getType() != V->getType() || !SE.DT.dominates(Def: EntInst, User: InsertPt) ||
1597 !(SE.LI.getLoopFor(BB: EntInst->getParent()) == nullptr ||
1598 SE.LI.getLoopFor(BB: EntInst->getParent())->contains(Inst: InsertPt)))
1599 continue;
1600
1601 // Make sure reusing the instruction is poison-safe.
1602 if (SE.canReuseInstruction(S, I: EntInst, DropPoisonGeneratingInsts))
1603 return V;
1604 DropPoisonGeneratingInsts.clear();
1605 }
1606 return nullptr;
1607}
1608
1609// The expansion of SCEV will either reuse a previous Value in ExprValueMap,
1610// or expand the SCEV literally. Specifically, if the expansion is in LSRMode,
1611// and the SCEV contains any sub scAddRecExpr type SCEV, it will be expanded
1612// literally, to prevent LSR's transformed SCEV from being reverted. Otherwise,
1613// the expansion will try to reuse Value from ExprValueMap, and only when it
1614// fails, expand the SCEV literally.
1615Value *SCEVExpander::expand(SCEVUse S) {
1616 // Compute an insertion point for this SCEV object. Hoist the instructions
1617 // as far out in the loop nest as possible.
1618 BasicBlock::iterator InsertPt = Builder.GetInsertPoint();
1619
1620 // We can move insertion point only if there is no div or rem operations
1621 // otherwise we are risky to move it over the check for zero denominator.
1622 auto SafeToHoist = [](const SCEV *S) {
1623 return !SCEVExprContains(Root: S, Pred: [](const SCEV *S) {
1624 if (const auto *D = dyn_cast<SCEVUDivExpr>(Val: S)) {
1625 if (const auto *SC = dyn_cast<SCEVConstant>(Val: D->getRHS()))
1626 // Division by non-zero constants can be hoisted.
1627 return SC->getValue()->isZero();
1628 // All other divisions should not be moved as they may be
1629 // divisions by zero and should be kept within the
1630 // conditions of the surrounding loops that guard their
1631 // execution (see PR35406).
1632 return true;
1633 }
1634 return false;
1635 });
1636 };
1637 if (SafeToHoist(S)) {
1638 for (Loop *L = SE.LI.getLoopFor(BB: Builder.GetInsertBlock());;
1639 L = L->getParentLoop()) {
1640 if (SE.isLoopInvariant(S, L)) {
1641 if (!L) break;
1642 if (BasicBlock *Preheader = L->getLoopPreheader()) {
1643 InsertPt = Preheader->getTerminator()->getIterator();
1644 } else {
1645 // LSR sets the insertion point for AddRec start/step values to the
1646 // block start to simplify value reuse, even though it's an invalid
1647 // position. SCEVExpander must correct for this in all cases.
1648 InsertPt = L->getHeader()->getFirstInsertionPt();
1649 }
1650 } else {
1651 // If the SCEV is computable at this level, insert it into the header
1652 // after the PHIs (and after any other instructions that we've inserted
1653 // there) so that it is guaranteed to dominate any user inside the loop.
1654 if (L && SE.hasComputableLoopEvolution(S, L) && !PostIncLoops.count(Ptr: L))
1655 InsertPt = L->getHeader()->getFirstInsertionPt();
1656
1657 while (InsertPt != Builder.GetInsertPoint() &&
1658 (isInsertedInstruction(I: &*InsertPt))) {
1659 InsertPt = std::next(x: InsertPt);
1660 }
1661 break;
1662 }
1663 }
1664 }
1665
1666 // Check to see if we already expanded this here.
1667 auto I = InsertedExpressions.find(Val: std::make_pair(x&: S, y: &*InsertPt));
1668 if (I != InsertedExpressions.end())
1669 return I->second;
1670
1671 SCEVInsertPointGuard Guard(Builder, this);
1672 Builder.SetInsertPoint(TheBB: InsertPt->getParent(), IP: InsertPt);
1673
1674 // Expand the expression into instructions.
1675 SmallVector<Instruction *> DropPoisonGeneratingInsts;
1676 Value *V = FindValueInExprValueMap(S, InsertPt: &*InsertPt, DropPoisonGeneratingInsts);
1677 if (!V) {
1678 V = visit(S);
1679 V = fixupLCSSAFormFor(V);
1680 } else {
1681 for (Instruction *I : DropPoisonGeneratingInsts) {
1682 rememberFlags(I);
1683 dropPoisonGeneratingAnnotationsAndReinfer(SE, I);
1684 }
1685 }
1686 // Remember the expanded value for this SCEV at this location.
1687 //
1688 // This is independent of PostIncLoops. The mapped value simply materializes
1689 // the expression at this insertion point. If the mapped value happened to be
1690 // a postinc expansion, it could be reused by a non-postinc user, but only if
1691 // its insertion point was already at the head of the loop.
1692 InsertedExpressions[std::make_pair(x&: S, y: &*InsertPt)] = V;
1693 return V;
1694}
1695
1696void SCEVExpander::rememberInstruction(Value *I) {
1697 auto DoInsert = [this](Value *V) {
1698 if (!PostIncLoops.empty())
1699 InsertedPostIncValues.insert(V);
1700 else
1701 InsertedValues.insert(V);
1702 };
1703 DoInsert(I);
1704}
1705
1706void SCEVExpander::rememberFlags(Instruction *I) {
1707 // If we already have flags for the instruction, keep the existing ones.
1708 OrigFlags.try_emplace(Key: I, Args: PoisonFlags(I));
1709}
1710
1711void SCEVExpander::dropPoisonGeneratingAnnotationsAndReinfer(
1712 ScalarEvolution &SE, Instruction *I) {
1713 I->dropPoisonGeneratingAnnotations();
1714 // See if we can re-infer from first principles any of the flags we just
1715 // dropped.
1716 if (auto *OBO = dyn_cast<OverflowingBinaryOperator>(Val: I))
1717 if (auto Flags = SE.getStrengthenedNoWrapFlagsFromBinOp(OBO)) {
1718 auto *BO = cast<BinaryOperator>(Val: I);
1719 BO->setHasNoUnsignedWrap(
1720 ScalarEvolution::maskFlags(Flags: *Flags, Mask: SCEV::FlagNUW) == SCEV::FlagNUW);
1721 BO->setHasNoSignedWrap(
1722 ScalarEvolution::maskFlags(Flags: *Flags, Mask: SCEV::FlagNSW) == SCEV::FlagNSW);
1723 }
1724 if (auto *NNI = dyn_cast<PossiblyNonNegInst>(Val: I)) {
1725 auto *Src = NNI->getOperand(i_nocapture: 0);
1726 if (isImpliedByDomCondition(Pred: ICmpInst::ICMP_SGE, LHS: Src,
1727 RHS: Constant::getNullValue(Ty: Src->getType()), ContextI: I,
1728 DL: SE.getDataLayout())
1729 .value_or(u: false))
1730 NNI->setNonNeg(true);
1731 }
1732}
1733
1734void SCEVExpander::replaceCongruentIVInc(
1735 PHINode *&Phi, PHINode *&OrigPhi, Loop *L, const DominatorTree *DT,
1736 SmallVectorImpl<WeakTrackingVH> &DeadInsts) {
1737 BasicBlock *LatchBlock = L->getLoopLatch();
1738 if (!LatchBlock)
1739 return;
1740
1741 Instruction *OrigInc =
1742 dyn_cast<Instruction>(Val: OrigPhi->getIncomingValueForBlock(BB: LatchBlock));
1743 Instruction *IsomorphicInc =
1744 dyn_cast<Instruction>(Val: Phi->getIncomingValueForBlock(BB: LatchBlock));
1745 if (!OrigInc || !IsomorphicInc)
1746 return;
1747
1748 // If this phi has the same width but is more canonical, replace the
1749 // original with it. As part of the "more canonical" determination,
1750 // respect a prior decision to use an IV chain.
1751 if (OrigPhi->getType() == Phi->getType()) {
1752 bool Chained = ChainedPhis.contains(V: Phi);
1753 if (!(Chained || isExpandedAddRecExprPHI(PN: OrigPhi, IncV: OrigInc, L)) &&
1754 (Chained || isExpandedAddRecExprPHI(PN: Phi, IncV: IsomorphicInc, L))) {
1755 std::swap(a&: OrigPhi, b&: Phi);
1756 std::swap(a&: OrigInc, b&: IsomorphicInc);
1757 }
1758 }
1759
1760 // Replacing the congruent phi is sufficient because acyclic
1761 // redundancy elimination, CSE/GVN, should handle the
1762 // rest. However, once SCEV proves that a phi is congruent,
1763 // it's often the head of an IV user cycle that is isomorphic
1764 // with the original phi. It's worth eagerly cleaning up the
1765 // common case of a single IV increment so that DeleteDeadPHIs
1766 // can remove cycles that had postinc uses.
1767 // Because we may potentially introduce a new use of OrigIV that didn't
1768 // exist before at this point, its poison flags need readjustment.
1769 const SCEV *TruncExpr =
1770 SE.getTruncateOrNoop(V: SE.getSCEV(V: OrigInc), Ty: IsomorphicInc->getType());
1771 if (OrigInc == IsomorphicInc || TruncExpr != SE.getSCEV(V: IsomorphicInc) ||
1772 !SE.LI.replacementPreservesLCSSAForm(From: IsomorphicInc, To: OrigInc))
1773 return;
1774
1775 bool BothHaveNUW = false;
1776 bool BothHaveNSW = false;
1777 auto *OBOIncV = dyn_cast<OverflowingBinaryOperator>(Val: OrigInc);
1778 auto *OBOIsomorphic = dyn_cast<OverflowingBinaryOperator>(Val: IsomorphicInc);
1779 if (OBOIncV && OBOIsomorphic) {
1780 BothHaveNUW =
1781 OBOIncV->hasNoUnsignedWrap() && OBOIsomorphic->hasNoUnsignedWrap();
1782 BothHaveNSW =
1783 OBOIncV->hasNoSignedWrap() && OBOIsomorphic->hasNoSignedWrap();
1784 }
1785
1786 if (!hoistIVInc(IncV: OrigInc, InsertPos: IsomorphicInc,
1787 /*RecomputePoisonFlags*/ true))
1788 return;
1789
1790 // We are replacing with a wider increment. If both OrigInc and IsomorphicInc
1791 // are NUW/NSW, then we can preserve them on the wider increment; the narrower
1792 // IsomorphicInc would wrap before the wider OrigInc, so the replacement won't
1793 // make IsomorphicInc's uses more poisonous.
1794 assert(OrigInc->getType()->getScalarSizeInBits() >=
1795 IsomorphicInc->getType()->getScalarSizeInBits() &&
1796 "Should only replace an increment with a wider one.");
1797 if (BothHaveNUW || BothHaveNSW) {
1798 OrigInc->setHasNoUnsignedWrap(OBOIncV->hasNoUnsignedWrap() || BothHaveNUW);
1799 OrigInc->setHasNoSignedWrap(OBOIncV->hasNoSignedWrap() || BothHaveNSW);
1800 }
1801
1802 SCEV_DEBUG_WITH_TYPE(DebugType,
1803 dbgs() << "INDVARS: Eliminated congruent iv.inc: "
1804 << *IsomorphicInc << '\n');
1805 Value *NewInc = OrigInc;
1806 if (OrigInc->getType() != IsomorphicInc->getType()) {
1807 BasicBlock::iterator IP;
1808 if (PHINode *PN = dyn_cast<PHINode>(Val: OrigInc))
1809 IP = PN->getParent()->getFirstInsertionPt();
1810 else
1811 IP = OrigInc->getNextNode()->getIterator();
1812
1813 IRBuilder<> Builder(IP->getParent(), IP);
1814 Builder.SetCurrentDebugLocation(IsomorphicInc->getDebugLoc());
1815 NewInc =
1816 Builder.CreateTruncOrBitCast(V: OrigInc, DestTy: IsomorphicInc->getType(), Name: IVName);
1817 }
1818 IsomorphicInc->replaceAllUsesWith(V: NewInc);
1819 DeadInsts.emplace_back(Args&: IsomorphicInc);
1820}
1821
1822/// replaceCongruentIVs - Check for congruent phis in this loop header and
1823/// replace them with their most canonical representative. Return the number of
1824/// phis eliminated.
1825///
1826/// This does not depend on any SCEVExpander state but should be used in
1827/// the same context that SCEVExpander is used.
1828unsigned
1829SCEVExpander::replaceCongruentIVs(Loop *L, const DominatorTree *DT,
1830 SmallVectorImpl<WeakTrackingVH> &DeadInsts,
1831 const TargetTransformInfo *TTI) {
1832 // Find integer phis in order of increasing width.
1833 SmallVector<PHINode *, 8> Phis(
1834 llvm::make_pointer_range(Range: L->getHeader()->phis()));
1835
1836 if (TTI)
1837 // Use stable_sort to preserve order of equivalent PHIs, so the order
1838 // of the sorted Phis is the same from run to run on the same loop.
1839 llvm::stable_sort(Range&: Phis, C: [](Value *LHS, Value *RHS) {
1840 // Put pointers at the back and make sure pointer < pointer = false.
1841 if (!LHS->getType()->isIntegerTy() || !RHS->getType()->isIntegerTy())
1842 return RHS->getType()->isIntegerTy() && !LHS->getType()->isIntegerTy();
1843 return RHS->getType()->getPrimitiveSizeInBits().getFixedValue() <
1844 LHS->getType()->getPrimitiveSizeInBits().getFixedValue();
1845 });
1846
1847 unsigned NumElim = 0;
1848 DenseMap<const SCEV *, PHINode *> ExprToIVMap;
1849 // Process phis from wide to narrow. Map wide phis to their truncation
1850 // so narrow phis can reuse them.
1851 for (PHINode *Phi : Phis) {
1852 auto SimplifyPHINode = [&](PHINode *PN) -> Value * {
1853 if (Value *V = simplifyInstruction(I: PN, Q: {DL, &SE.TLI, &SE.DT, &SE.AC}))
1854 return V;
1855 if (!SE.isSCEVable(Ty: PN->getType()))
1856 return nullptr;
1857 auto *Const = dyn_cast<SCEVConstant>(Val: SE.getSCEV(V: PN));
1858 if (!Const)
1859 return nullptr;
1860 return Const->getValue();
1861 };
1862
1863 // Fold constant phis. They may be congruent to other constant phis and
1864 // would confuse the logic below that expects proper IVs.
1865 if (Value *V = SimplifyPHINode(Phi)) {
1866 if (V->getType() != Phi->getType())
1867 continue;
1868 SE.forgetValue(V: Phi);
1869 Phi->replaceAllUsesWith(V);
1870 DeadInsts.emplace_back(Args&: Phi);
1871 ++NumElim;
1872 SCEV_DEBUG_WITH_TYPE(DebugType,
1873 dbgs() << "INDVARS: Eliminated constant iv: " << *Phi
1874 << '\n');
1875 continue;
1876 }
1877
1878 if (!SE.isSCEVable(Ty: Phi->getType()))
1879 continue;
1880
1881 PHINode *&OrigPhiRef = ExprToIVMap[SE.getSCEV(V: Phi)];
1882 if (!OrigPhiRef) {
1883 OrigPhiRef = Phi;
1884 if (Phi->getType()->isIntegerTy() && TTI &&
1885 TTI->isTruncateFree(Ty1: Phi->getType(), Ty2: Phis.back()->getType())) {
1886 // Make sure we only rewrite using simple induction variables;
1887 // otherwise, we can make the trip count of a loop unanalyzable
1888 // to SCEV.
1889 const SCEV *PhiExpr = SE.getSCEV(V: Phi);
1890 if (isa<SCEVAddRecExpr>(Val: PhiExpr)) {
1891 // This phi can be freely truncated to the narrowest phi type. Map the
1892 // truncated expression to it so it will be reused for narrow types.
1893 const SCEV *TruncExpr =
1894 SE.getTruncateExpr(Op: PhiExpr, Ty: Phis.back()->getType());
1895 ExprToIVMap[TruncExpr] = Phi;
1896 }
1897 }
1898 continue;
1899 }
1900
1901 // Replacing a pointer phi with an integer phi or vice-versa doesn't make
1902 // sense.
1903 if (OrigPhiRef->getType()->isPointerTy() != Phi->getType()->isPointerTy())
1904 continue;
1905
1906 replaceCongruentIVInc(Phi, OrigPhi&: OrigPhiRef, L, DT, DeadInsts);
1907 SCEV_DEBUG_WITH_TYPE(DebugType,
1908 dbgs() << "INDVARS: Eliminated congruent iv: " << *Phi
1909 << '\n');
1910 SCEV_DEBUG_WITH_TYPE(
1911 DebugType, dbgs() << "INDVARS: Original iv: " << *OrigPhiRef << '\n');
1912 ++NumElim;
1913 Value *NewIV = OrigPhiRef;
1914 if (OrigPhiRef->getType() != Phi->getType()) {
1915 IRBuilder<> Builder(L->getHeader(),
1916 L->getHeader()->getFirstInsertionPt());
1917 Builder.SetCurrentDebugLocation(Phi->getDebugLoc());
1918 NewIV = Builder.CreateTruncOrBitCast(V: OrigPhiRef, DestTy: Phi->getType(), Name: IVName);
1919 }
1920 Phi->replaceAllUsesWith(V: NewIV);
1921 DeadInsts.emplace_back(Args&: Phi);
1922 }
1923 return NumElim;
1924}
1925
1926bool SCEVExpander::hasRelatedExistingExpansion(const SCEV *S,
1927 const Instruction *At,
1928 Loop *L) {
1929 using namespace llvm::PatternMatch;
1930
1931 SmallVector<BasicBlock *, 4> ExitingBlocks;
1932 L->getExitingBlocks(ExitingBlocks);
1933
1934 // Look for suitable value in simple conditions at the loop exits.
1935 for (BasicBlock *BB : ExitingBlocks) {
1936 CmpPredicate Pred;
1937 Instruction *LHS, *RHS;
1938
1939 if (!match(V: BB->getTerminator(),
1940 P: m_Br(C: m_ICmp(Pred, L: m_Instruction(I&: LHS), R: m_Instruction(I&: RHS)),
1941 T: m_BasicBlock(), F: m_BasicBlock())))
1942 continue;
1943
1944 if (SE.getSCEV(V: LHS) == S && SE.DT.dominates(Def: LHS, User: At))
1945 return true;
1946
1947 if (SE.getSCEV(V: RHS) == S && SE.DT.dominates(Def: RHS, User: At))
1948 return true;
1949 }
1950
1951 // Use expand's logic which is used for reusing a previous Value in
1952 // ExprValueMap. Note that we don't currently model the cost of
1953 // needing to drop poison generating flags on the instruction if we
1954 // want to reuse it. We effectively assume that has zero cost.
1955 SmallVector<Instruction *> DropPoisonGeneratingInsts;
1956 return FindValueInExprValueMap(S, InsertPt: At, DropPoisonGeneratingInsts) != nullptr;
1957}
1958
1959template<typename T> static InstructionCost costAndCollectOperands(
1960 const SCEVOperand &WorkItem, const TargetTransformInfo &TTI,
1961 TargetTransformInfo::TargetCostKind CostKind,
1962 SmallVectorImpl<SCEVOperand> &Worklist) {
1963
1964 const T *S = cast<T>(WorkItem.S);
1965 InstructionCost Cost = 0;
1966 // Object to help map SCEV operands to expanded IR instructions.
1967 struct OperationIndices {
1968 OperationIndices(unsigned Opc, size_t min, size_t max) :
1969 Opcode(Opc), MinIdx(min), MaxIdx(max) { }
1970 unsigned Opcode;
1971 size_t MinIdx;
1972 size_t MaxIdx;
1973 };
1974
1975 // Collect the operations of all the instructions that will be needed to
1976 // expand the SCEVExpr. This is so that when we come to cost the operands,
1977 // we know what the generated user(s) will be.
1978 SmallVector<OperationIndices, 2> Operations;
1979
1980 auto CastCost = [&](unsigned Opcode) -> InstructionCost {
1981 Operations.emplace_back(Opcode, 0, 0);
1982 return TTI.getCastInstrCost(Opcode, Dst: S->getType(),
1983 Src: S->getOperand(0)->getType(),
1984 CCH: TTI::CastContextHint::None, CostKind);
1985 };
1986
1987 auto ArithCost = [&](unsigned Opcode, unsigned NumRequired,
1988 unsigned MinIdx = 0,
1989 unsigned MaxIdx = 1) -> InstructionCost {
1990 Operations.emplace_back(Opcode, MinIdx, MaxIdx);
1991 return NumRequired *
1992 TTI.getArithmeticInstrCost(Opcode, Ty: S->getType(), CostKind);
1993 };
1994
1995 auto CmpSelCost = [&](unsigned Opcode, unsigned NumRequired, unsigned MinIdx,
1996 unsigned MaxIdx) -> InstructionCost {
1997 Operations.emplace_back(Opcode, MinIdx, MaxIdx);
1998 Type *OpType = S->getType();
1999 return NumRequired * TTI.getCmpSelInstrCost(
2000 Opcode, ValTy: OpType, CondTy: CmpInst::makeCmpResultType(opnd_type: OpType),
2001 VecPred: CmpInst::BAD_ICMP_PREDICATE, CostKind);
2002 };
2003
2004 switch (S->getSCEVType()) {
2005 case scCouldNotCompute:
2006 llvm_unreachable("Attempt to use a SCEVCouldNotCompute object!");
2007 case scUnknown:
2008 case scConstant:
2009 case scVScale:
2010 return 0;
2011 case scPtrToAddr:
2012 Cost = CastCost(Instruction::PtrToAddr);
2013 break;
2014 case scPtrToInt:
2015 Cost = CastCost(Instruction::PtrToInt);
2016 break;
2017 case scTruncate:
2018 Cost = CastCost(Instruction::Trunc);
2019 break;
2020 case scZeroExtend:
2021 Cost = CastCost(Instruction::ZExt);
2022 break;
2023 case scSignExtend:
2024 Cost = CastCost(Instruction::SExt);
2025 break;
2026 case scUDivExpr: {
2027 unsigned Opcode = Instruction::UDiv;
2028 if (auto *SC = dyn_cast<SCEVConstant>(S->getOperand(1)))
2029 if (SC->getAPInt().isPowerOf2())
2030 Opcode = Instruction::LShr;
2031 Cost = ArithCost(Opcode, 1);
2032 break;
2033 }
2034 case scAddExpr:
2035 Cost = ArithCost(Instruction::Add, S->getNumOperands() - 1);
2036 break;
2037 case scMulExpr: {
2038 // Match the actual expansion in visitMulExpr: multiply by -1 is
2039 // expanded as a negate (sub 0, x), and multiply by a power of 2 is
2040 // expanded as a shift. Only handle the common two-operand case with a
2041 // constant LHS; for everything else fall back to the pessimistic
2042 // all-multiplies estimate.
2043 // TODO: this is still pessimistic for the general case because of the
2044 // Bin Pow algorithm actually used by the expander, see
2045 // SCEVExpander::visitMulExpr(), ExpandOpBinPowN().
2046 unsigned OpCode = Instruction::Mul;
2047 if (S->getNumOperands() == 2)
2048 if (auto *SC = dyn_cast<SCEVConstant>(S->getOperand(0))) {
2049 if (SC->getAPInt().isAllOnes()) // -1
2050 OpCode = Instruction::Sub;
2051 else if (SC->getAPInt().isPowerOf2())
2052 OpCode = Instruction::Shl;
2053 }
2054 Cost = ArithCost(OpCode, S->getNumOperands() - 1);
2055 break;
2056 }
2057 case scSMaxExpr:
2058 case scUMaxExpr:
2059 case scSMinExpr:
2060 case scUMinExpr:
2061 case scSequentialUMinExpr: {
2062 // FIXME: should this ask the cost for Intrinsic's?
2063 // The reduction tree.
2064 Cost += CmpSelCost(Instruction::ICmp, S->getNumOperands() - 1, 0, 1);
2065 Cost += CmpSelCost(Instruction::Select, S->getNumOperands() - 1, 0, 2);
2066 switch (S->getSCEVType()) {
2067 case scSequentialUMinExpr: {
2068 // The safety net against poison.
2069 // FIXME: this is broken.
2070 Cost += CmpSelCost(Instruction::ICmp, S->getNumOperands() - 1, 0, 0);
2071 Cost += ArithCost(Instruction::Or,
2072 S->getNumOperands() > 2 ? S->getNumOperands() - 2 : 0);
2073 Cost += CmpSelCost(Instruction::Select, 1, 0, 1);
2074 break;
2075 }
2076 default:
2077 assert(!isa<SCEVSequentialMinMaxExpr>(S) &&
2078 "Unhandled SCEV expression type?");
2079 break;
2080 }
2081 break;
2082 }
2083 case scAddRecExpr: {
2084 // Addrec expands to a phi and add per recurrence.
2085 unsigned NumRecurrences = S->getNumOperands() - 1;
2086 Cost += TTI.getCFInstrCost(Opcode: Instruction::PHI, CostKind) * NumRecurrences;
2087 Cost +=
2088 TTI.getArithmeticInstrCost(Opcode: Instruction::Add, Ty: S->getType(), CostKind) *
2089 NumRecurrences;
2090 // AR start is used in phi.
2091 Worklist.emplace_back(Instruction::PHI, 0, S->getOperand(0));
2092 // Other operands are used in add.
2093 for (const SCEV *Op : S->operands().drop_front())
2094 Worklist.emplace_back(Args: Instruction::Add, Args: 1, Args&: Op);
2095 break;
2096 }
2097 }
2098
2099 for (auto &CostOp : Operations) {
2100 for (auto SCEVOp : enumerate(S->operands())) {
2101 // Clamp the index to account for multiple IR operations being chained.
2102 size_t MinIdx = std::max(SCEVOp.index(), CostOp.MinIdx);
2103 size_t OpIdx = std::min(MinIdx, CostOp.MaxIdx);
2104 Worklist.emplace_back(CostOp.Opcode, OpIdx, SCEVOp.value());
2105 }
2106 }
2107 return Cost;
2108}
2109
2110bool SCEVExpander::isHighCostExpansionHelper(
2111 const SCEVOperand &WorkItem, Loop *L, const Instruction &At,
2112 InstructionCost &Cost, unsigned Budget, const TargetTransformInfo &TTI,
2113 SmallPtrSetImpl<const SCEV *> &Processed,
2114 SmallVectorImpl<SCEVOperand> &Worklist) {
2115 if (Cost > Budget)
2116 return true; // Already run out of budget, give up.
2117
2118 const SCEV *S = WorkItem.S;
2119 // Was the cost of expansion of this expression already accounted for?
2120 if (!isa<SCEVConstant>(Val: S) && !Processed.insert(Ptr: S).second)
2121 return false; // We have already accounted for this expression.
2122
2123 // If we can find an existing value for this scev available at the point "At"
2124 // then consider the expression cheap.
2125 if (hasRelatedExistingExpansion(S, At: &At, L))
2126 return false; // Consider the expression to be free.
2127
2128 TargetTransformInfo::TargetCostKind CostKind =
2129 L->getHeader()->getParent()->hasMinSize()
2130 ? TargetTransformInfo::TCK_CodeSize
2131 : TargetTransformInfo::TCK_RecipThroughput;
2132
2133 switch (S->getSCEVType()) {
2134 case scCouldNotCompute:
2135 llvm_unreachable("Attempt to use a SCEVCouldNotCompute object!");
2136 case scUnknown:
2137 case scVScale:
2138 // Assume to be zero-cost.
2139 return false;
2140 case scConstant: {
2141 // Only evalulate the costs of constants when optimizing for size.
2142 if (CostKind != TargetTransformInfo::TCK_CodeSize)
2143 return false;
2144 const APInt &Imm = cast<SCEVConstant>(Val: S)->getAPInt();
2145 Type *Ty = S->getType();
2146 Cost += TTI.getIntImmCostInst(
2147 Opc: WorkItem.ParentOpcode, Idx: WorkItem.OperandIdx, Imm, Ty, CostKind);
2148 return Cost > Budget;
2149 }
2150 case scTruncate:
2151 case scPtrToAddr:
2152 case scPtrToInt:
2153 case scZeroExtend:
2154 case scSignExtend: {
2155 Cost +=
2156 costAndCollectOperands<SCEVCastExpr>(WorkItem, TTI, CostKind, Worklist);
2157 return false; // Will answer upon next entry into this function.
2158 }
2159 case scUDivExpr: {
2160 // UDivExpr is very likely a UDiv that ScalarEvolution's HowFarToZero or
2161 // HowManyLessThans produced to compute a precise expression, rather than a
2162 // UDiv from the user's code. If we can't find a UDiv in the code with some
2163 // simple searching, we need to account for it's cost.
2164
2165 // At the beginning of this function we already tried to find existing
2166 // value for plain 'S'. Now try to lookup 'S + 1' since it is common
2167 // pattern involving division. This is just a simple search heuristic.
2168 if (hasRelatedExistingExpansion(
2169 S: SE.getAddExpr(LHS: S, RHS: SE.getConstant(Ty: S->getType(), V: 1)), At: &At, L))
2170 return false; // Consider it to be free.
2171
2172 Cost +=
2173 costAndCollectOperands<SCEVUDivExpr>(WorkItem, TTI, CostKind, Worklist);
2174 return false; // Will answer upon next entry into this function.
2175 }
2176 case scAddExpr:
2177 case scMulExpr:
2178 case scUMaxExpr:
2179 case scSMaxExpr:
2180 case scUMinExpr:
2181 case scSMinExpr:
2182 case scSequentialUMinExpr: {
2183 assert(cast<SCEVNAryExpr>(S)->getNumOperands() > 1 &&
2184 "Nary expr should have more than 1 operand.");
2185 // The simple nary expr will require one less op (or pair of ops)
2186 // than the number of it's terms.
2187 Cost +=
2188 costAndCollectOperands<SCEVNAryExpr>(WorkItem, TTI, CostKind, Worklist);
2189 return Cost > Budget;
2190 }
2191 case scAddRecExpr: {
2192 assert(cast<SCEVAddRecExpr>(S)->getNumOperands() >= 2 &&
2193 "Polynomial should be at least linear");
2194 Cost += costAndCollectOperands<SCEVAddRecExpr>(
2195 WorkItem, TTI, CostKind, Worklist);
2196 return Cost > Budget;
2197 }
2198 }
2199 llvm_unreachable("Unknown SCEV kind!");
2200}
2201
2202Value *SCEVExpander::expandCodeForPredicate(const SCEVPredicate *Pred,
2203 Instruction *IP) {
2204 assert(IP);
2205 switch (Pred->getKind()) {
2206 case SCEVPredicate::P_Union:
2207 return expandUnionPredicate(Pred: cast<SCEVUnionPredicate>(Val: Pred), Loc: IP);
2208 case SCEVPredicate::P_Compare:
2209 return expandComparePredicate(Pred: cast<SCEVComparePredicate>(Val: Pred), Loc: IP);
2210 case SCEVPredicate::P_Wrap: {
2211 auto *AddRecPred = cast<SCEVWrapPredicate>(Val: Pred);
2212 return expandWrapPredicate(P: AddRecPred, Loc: IP);
2213 }
2214 }
2215 llvm_unreachable("Unknown SCEV predicate type");
2216}
2217
2218Value *SCEVExpander::expandComparePredicate(const SCEVComparePredicate *Pred,
2219 Instruction *IP) {
2220 Value *Expr0 = expand(S: Pred->getLHS(), I: IP);
2221 Value *Expr1 = expand(S: Pred->getRHS(), I: IP);
2222
2223 Builder.SetInsertPoint(IP);
2224 auto InvPred = ICmpInst::getInversePredicate(pred: Pred->getPredicate());
2225 auto *I = Builder.CreateICmp(P: InvPred, LHS: Expr0, RHS: Expr1, Name: "ident.check");
2226 return I;
2227}
2228
2229Value *SCEVExpander::generateOverflowCheck(const SCEVAddRecExpr *AR,
2230 Instruction *Loc, bool Signed) {
2231 assert(AR->isAffine() && "Cannot generate RT check for "
2232 "non-affine expression");
2233
2234 // FIXME: It is highly suspicious that we're ignoring the predicates here.
2235 SmallVector<const SCEVPredicate *, 4> Pred;
2236 const SCEV *ExitCount =
2237 SE.getPredicatedSymbolicMaxBackedgeTakenCount(L: AR->getLoop(), Predicates&: Pred);
2238
2239 assert(!isa<SCEVCouldNotCompute>(ExitCount) && "Invalid loop count");
2240
2241 const SCEV *Step = AR->getStepRecurrence(SE);
2242 const SCEV *Start = AR->getStart();
2243
2244 Type *ARTy = AR->getType();
2245 unsigned SrcBits = SE.getTypeSizeInBits(Ty: ExitCount->getType());
2246 unsigned DstBits = SE.getTypeSizeInBits(Ty: ARTy);
2247
2248 // The expression {Start,+,Step} has nusw/nssw if
2249 // Step < 0, Start - |Step| * Backedge <= Start
2250 // Step >= 0, Start + |Step| * Backedge > Start
2251 // and |Step| * Backedge doesn't unsigned overflow.
2252
2253 Builder.SetInsertPoint(Loc);
2254 Value *TripCountVal = expand(S: ExitCount, I: Loc);
2255
2256 IntegerType *Ty =
2257 IntegerType::get(C&: Loc->getContext(), NumBits: SE.getTypeSizeInBits(Ty: ARTy));
2258
2259 Value *StepValue = expand(S: Step, I: Loc);
2260 Value *NegStepValue = expand(S: SE.getNegativeSCEV(V: Step), I: Loc);
2261 Value *StartValue = expand(S: Start, I: Loc);
2262
2263 ConstantInt *Zero =
2264 ConstantInt::get(Context&: Loc->getContext(), V: APInt::getZero(numBits: DstBits));
2265
2266 Builder.SetInsertPoint(Loc);
2267 // Compute |Step|
2268 Value *StepCompare = Builder.CreateICmp(P: ICmpInst::ICMP_SLT, LHS: StepValue, RHS: Zero);
2269 Value *AbsStep = Builder.CreateSelect(C: StepCompare, True: NegStepValue, False: StepValue);
2270
2271 // Compute |Step| * Backedge
2272 // Compute:
2273 // 1. Start + |Step| * Backedge < Start
2274 // 2. Start - |Step| * Backedge > Start
2275 //
2276 // And select either 1. or 2. depending on whether step is positive or
2277 // negative. If Step is known to be positive or negative, only create
2278 // either 1. or 2.
2279 auto ComputeEndCheck = [&]() -> Value * {
2280 // Get the backedge taken count and truncate or extended to the AR type.
2281 Value *TruncTripCount = Builder.CreateZExtOrTrunc(V: TripCountVal, DestTy: Ty);
2282
2283 Value *Mul = Builder.CreateIntrinsic(ID: Intrinsic::umul_with_overflow, OverloadTypes: Ty,
2284 Args: {AbsStep, TruncTripCount},
2285 /*FMFSource=*/nullptr, Name: "mul");
2286 Value *MulV = Builder.CreateExtractValue(Agg: Mul, Idxs: 0, Name: "mul.result");
2287 Value *OfMul = Builder.CreateExtractValue(Agg: Mul, Idxs: 1, Name: "mul.overflow");
2288
2289 Value *Add = nullptr, *Sub = nullptr;
2290 bool NeedPosCheck = !SE.isKnownNegative(S: Step);
2291 bool NeedNegCheck = !SE.isKnownPositive(S: Step);
2292
2293 if (isa<PointerType>(Val: ARTy)) {
2294 Value *NegMulV = Builder.CreateNeg(V: MulV);
2295 if (NeedPosCheck)
2296 Add = Builder.CreatePtrAdd(Ptr: StartValue, Offset: MulV);
2297 if (NeedNegCheck)
2298 Sub = Builder.CreatePtrAdd(Ptr: StartValue, Offset: NegMulV);
2299 } else {
2300 if (NeedPosCheck)
2301 Add = Builder.CreateAdd(LHS: StartValue, RHS: MulV);
2302 if (NeedNegCheck)
2303 Sub = Builder.CreateSub(LHS: StartValue, RHS: MulV);
2304 }
2305
2306 Value *EndCompareLT = nullptr;
2307 Value *EndCompareGT = nullptr;
2308 Value *EndCheck = nullptr;
2309 if (NeedPosCheck)
2310 EndCheck = EndCompareLT = Builder.CreateICmp(
2311 P: Signed ? ICmpInst::ICMP_SLT : ICmpInst::ICMP_ULT, LHS: Add, RHS: StartValue);
2312 if (NeedNegCheck)
2313 EndCheck = EndCompareGT = Builder.CreateICmp(
2314 P: Signed ? ICmpInst::ICMP_SGT : ICmpInst::ICMP_UGT, LHS: Sub, RHS: StartValue);
2315 if (NeedPosCheck && NeedNegCheck) {
2316 // Select the answer based on the sign of Step.
2317 EndCheck = Builder.CreateSelect(C: StepCompare, True: EndCompareGT, False: EndCompareLT);
2318 }
2319 return Builder.CreateOr(LHS: EndCheck, RHS: OfMul);
2320 };
2321 Value *EndCheck = ComputeEndCheck();
2322
2323 // If the backedge taken count type is larger than the AR type,
2324 // check that we don't drop any bits by truncating it. If we are
2325 // dropping bits, then we have overflow (unless the step is zero).
2326 if (SrcBits > DstBits) {
2327 auto MaxVal = APInt::getMaxValue(numBits: DstBits).zext(width: SrcBits);
2328 auto *BackedgeCheck =
2329 Builder.CreateICmp(P: ICmpInst::ICMP_UGT, LHS: TripCountVal,
2330 RHS: ConstantInt::get(Context&: Loc->getContext(), V: MaxVal));
2331 BackedgeCheck = Builder.CreateAnd(
2332 LHS: BackedgeCheck, RHS: Builder.CreateICmp(P: ICmpInst::ICMP_NE, LHS: StepValue, RHS: Zero));
2333
2334 EndCheck = Builder.CreateOr(LHS: EndCheck, RHS: BackedgeCheck);
2335 }
2336
2337 return EndCheck;
2338}
2339
2340Value *SCEVExpander::expandWrapPredicate(const SCEVWrapPredicate *Pred,
2341 Instruction *IP) {
2342 const auto *A = cast<SCEVAddRecExpr>(Val: Pred->getExpr());
2343 Value *NSSWCheck = nullptr, *NUSWCheck = nullptr;
2344
2345 // Add a check for NUSW
2346 if (Pred->getFlags() & SCEVWrapPredicate::IncrementNUSW)
2347 NUSWCheck = generateOverflowCheck(AR: A, Loc: IP, Signed: false);
2348
2349 // Add a check for NSSW
2350 if (Pred->getFlags() & SCEVWrapPredicate::IncrementNSSW)
2351 NSSWCheck = generateOverflowCheck(AR: A, Loc: IP, Signed: true);
2352
2353 if (NUSWCheck && NSSWCheck)
2354 return Builder.CreateOr(LHS: NUSWCheck, RHS: NSSWCheck);
2355
2356 if (NUSWCheck)
2357 return NUSWCheck;
2358
2359 if (NSSWCheck)
2360 return NSSWCheck;
2361
2362 return ConstantInt::getFalse(Context&: IP->getContext());
2363}
2364
2365Value *SCEVExpander::expandUnionPredicate(const SCEVUnionPredicate *Union,
2366 Instruction *IP) {
2367 // Loop over all checks in this set.
2368 SmallVector<Value *> Checks;
2369 for (const auto *Pred : Union->getPredicates()) {
2370 Checks.push_back(Elt: expandCodeForPredicate(Pred, IP));
2371 Builder.SetInsertPoint(IP);
2372 }
2373
2374 if (Checks.empty())
2375 return ConstantInt::getFalse(Context&: IP->getContext());
2376 return Builder.CreateOr(Ops: Checks);
2377}
2378
2379Value *SCEVExpander::fixupLCSSAFormFor(Value *V) {
2380 auto *DefI = dyn_cast<Instruction>(Val: V);
2381 if (!PreserveLCSSA || !DefI)
2382 return V;
2383
2384 BasicBlock::iterator InsertPt = Builder.GetInsertPoint();
2385 Loop *DefLoop = SE.LI.getLoopFor(BB: DefI->getParent());
2386 Loop *UseLoop = SE.LI.getLoopFor(BB: InsertPt->getParent());
2387 if (!DefLoop || UseLoop == DefLoop || DefLoop->contains(L: UseLoop))
2388 return V;
2389
2390 // Create a temporary instruction to at the current insertion point, so we
2391 // can hand it off to the helper to create LCSSA PHIs if required for the
2392 // new use.
2393 // FIXME: Ideally formLCSSAForInstructions (used in fixupLCSSAFormFor)
2394 // would accept a insertion point and return an LCSSA phi for that
2395 // insertion point, so there is no need to insert & remove the temporary
2396 // instruction.
2397 Type *ToTy;
2398 if (DefI->getType()->isIntegerTy())
2399 ToTy = PointerType::get(C&: DefI->getContext(), AddressSpace: 0);
2400 else
2401 ToTy = Type::getInt32Ty(C&: DefI->getContext());
2402 Instruction *User =
2403 CastInst::CreateBitOrPointerCast(S: DefI, Ty: ToTy, Name: "tmp.lcssa.user", InsertBefore: InsertPt);
2404 llvm::scope_exit RemoveUserOnExit([User]() { User->eraseFromParent(); });
2405
2406 SmallVector<Instruction *, 1> ToUpdate;
2407 ToUpdate.push_back(Elt: DefI);
2408 SmallVector<PHINode *, 16> PHIsToRemove;
2409 SmallVector<PHINode *, 16> InsertedPHIs;
2410 formLCSSAForInstructions(Worklist&: ToUpdate, DT: SE.DT, LI: SE.LI, SE: &SE, PHIsToRemove: &PHIsToRemove,
2411 InsertedPHIs: &InsertedPHIs);
2412 for (PHINode *PN : InsertedPHIs)
2413 rememberInstruction(I: PN);
2414 for (PHINode *PN : PHIsToRemove) {
2415 if (!PN->use_empty())
2416 continue;
2417 InsertedValues.erase(V: PN);
2418 InsertedPostIncValues.erase(V: PN);
2419 PN->eraseFromParent();
2420 }
2421
2422 return User->getOperand(i: 0);
2423}
2424
2425namespace {
2426// Search for a SCEV subexpression that is not safe to expand. Any expression
2427// that may expand to a !isSafeToSpeculativelyExecute value is unsafe, namely
2428// UDiv expressions. We don't know if the UDiv is derived from an IR divide
2429// instruction, but the important thing is that we prove the denominator is
2430// nonzero before expansion.
2431//
2432// IVUsers already checks that IV-derived expressions are safe. So this check is
2433// only needed when the expression includes some subexpression that is not IV
2434// derived.
2435//
2436// Currently, we only allow division by a value provably non-zero here.
2437//
2438// We cannot generally expand recurrences unless the step dominates the loop
2439// header. The expander handles the special case of affine recurrences by
2440// scaling the recurrence outside the loop, but this technique isn't generally
2441// applicable. Expanding a nested recurrence outside a loop requires computing
2442// binomial coefficients. This could be done, but the recurrence has to be in a
2443// perfectly reduced form, which can't be guaranteed.
2444struct SCEVFindUnsafe {
2445 ScalarEvolution &SE;
2446 bool CanonicalMode;
2447 bool IsUnsafe = false;
2448
2449 SCEVFindUnsafe(ScalarEvolution &SE, bool CanonicalMode)
2450 : SE(SE), CanonicalMode(CanonicalMode) {}
2451
2452 bool follow(const SCEV *S) {
2453 if (const SCEVUDivExpr *D = dyn_cast<SCEVUDivExpr>(Val: S)) {
2454 if (!SE.isKnownNonZero(S: D->getRHS()) ||
2455 !SE.isGuaranteedNotToBePoison(Op: D->getRHS())) {
2456 IsUnsafe = true;
2457 return false;
2458 }
2459 }
2460 if (const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(Val: S)) {
2461 // For non-affine addrecs or in non-canonical mode we need a preheader
2462 // to insert into.
2463 if (!AR->getLoop()->getLoopPreheader() &&
2464 (!CanonicalMode || !AR->isAffine())) {
2465 IsUnsafe = true;
2466 return false;
2467 }
2468 }
2469 return true;
2470 }
2471 bool isDone() const { return IsUnsafe; }
2472};
2473} // namespace
2474
2475bool SCEVExpander::isSafeToExpand(const SCEV *S) const {
2476 SCEVFindUnsafe Search(SE, CanonicalMode);
2477 visitAll(Root: S, Visitor&: Search);
2478 return !Search.IsUnsafe;
2479}
2480
2481bool SCEVExpander::isSafeToExpandAt(const SCEV *S,
2482 const Instruction *InsertionPoint) const {
2483 if (!isSafeToExpand(S))
2484 return false;
2485 // We have to prove that the expanded site of S dominates InsertionPoint.
2486 // This is easy when not in the same block, but hard when S is an instruction
2487 // to be expanded somewhere inside the same block as our insertion point.
2488 // What we really need here is something analogous to an OrderedBasicBlock,
2489 // but for the moment, we paper over the problem by handling two common and
2490 // cheap to check cases.
2491 if (SE.properlyDominates(S, BB: InsertionPoint->getParent()))
2492 return true;
2493 if (SE.dominates(S, BB: InsertionPoint->getParent())) {
2494 if (InsertionPoint->getParent()->getTerminator() == InsertionPoint)
2495 return true;
2496 if (const SCEVUnknown *U = dyn_cast<SCEVUnknown>(Val: S))
2497 if (llvm::is_contained(Range: InsertionPoint->operand_values(), Element: U->getValue()))
2498 return true;
2499 }
2500 return false;
2501}
2502
2503void SCEVExpanderCleaner::cleanup() {
2504 // Result is used, nothing to remove.
2505 if (ResultUsed)
2506 return;
2507
2508 // Restore original poison flags.
2509 for (auto [I, Flags] : Expander.OrigFlags)
2510 Flags.apply(I);
2511
2512 auto InsertedInstructions = Expander.getAllInsertedInstructions();
2513#ifndef NDEBUG
2514 SmallPtrSet<Instruction *, 8> InsertedSet(llvm::from_range,
2515 InsertedInstructions);
2516 (void)InsertedSet;
2517#endif
2518 // Remove sets with value handles.
2519 Expander.clear();
2520
2521 // Remove all inserted instructions.
2522 for (Instruction *I : reverse(C&: InsertedInstructions)) {
2523#ifndef NDEBUG
2524 assert(all_of(I->users(),
2525 [&InsertedSet](Value *U) {
2526 return InsertedSet.contains(cast<Instruction>(U));
2527 }) &&
2528 "removed instruction should only be used by instructions inserted "
2529 "during expansion");
2530#endif
2531 assert(!I->getType()->isVoidTy() &&
2532 "inserted instruction should have non-void types");
2533 I->replaceAllUsesWith(V: PoisonValue::get(T: I->getType()));
2534 I->eraseFromParent();
2535 }
2536}
2537