LLVM 23.0.0git
ScalarEvolutionExpander.h
Go to the documentation of this file.
1//===---- llvm/Analysis/ScalarEvolutionExpander.h - SCEV Exprs --*- C++ -*-===//
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 defines the classes used to generate code from scalar expressions.
10//
11//===----------------------------------------------------------------------===//
12
13#ifndef LLVM_TRANSFORMS_UTILS_SCALAREVOLUTIONEXPANDER_H
14#define LLVM_TRANSFORMS_UTILS_SCALAREVOLUTIONEXPANDER_H
15
16#include "llvm/ADT/DenseMap.h"
17#include "llvm/ADT/DenseSet.h"
23#include "llvm/IR/IRBuilder.h"
24#include "llvm/IR/ValueHandle.h"
28
29namespace llvm {
31
32/// struct for holding enough information to help calculate the cost of the
33/// given SCEV when expanded into IR.
35 explicit SCEVOperand(unsigned Opc, int Idx, const SCEV *S) :
36 ParentOpcode(Opc), OperandIdx(Idx), S(S) { }
37 /// LLVM instruction opcode that uses the operand.
38 unsigned ParentOpcode;
39 /// The use index of an expanded instruction.
41 /// The SCEV operand to be costed.
42 const SCEV* S;
43};
44
46 unsigned NUW : 1;
47 unsigned NSW : 1;
48 unsigned Exact : 1;
49 unsigned Disjoint : 1;
50 unsigned NNeg : 1;
51 unsigned SameSign : 1;
53
56};
57
58/// This class uses information about analyze scalars to rewrite expressions
59/// in canonical form.
60///
61/// Clients should create an instance of this class when rewriting is needed,
62/// and destroy it when finished to allow the release of the associated
63/// memory.
64class SCEVExpander : public SCEVUseVisitor<SCEVExpander, Value *> {
65 friend class SCEVExpanderCleaner;
66
68 const DataLayout &DL;
69
70 // New instructions receive a name to identify them with the current pass.
71 const char *IVName;
72
73 /// Indicates whether LCSSA phis should be created for inserted values.
74 bool PreserveLCSSA;
75
76 // InsertedExpressions caches Values for reuse, so must track RAUW.
78 InsertedExpressions;
79
80 // InsertedValues only flags inserted instructions so needs no RAUW.
81 DenseSet<AssertingVH<Value>> InsertedValues;
82 DenseSet<AssertingVH<Value>> InsertedPostIncValues;
83
84 /// Keep track of the existing IR values re-used during expansion.
85 /// FIXME: Ideally re-used instructions would not be added to
86 /// InsertedValues/InsertedPostIncValues.
87 SmallPtrSet<Value *, 16> ReusedValues;
88
89 /// Original flags of instructions for which they were modified. Used
90 /// by SCEVExpanderCleaner to undo changes.
92
93 // The induction variables generated.
94 SmallVector<WeakVH, 2> InsertedIVs;
95
96 /// A memoization of the "relevant" loop for a given SCEV.
98
99 /// Addrecs referring to any of the given loops are expanded in post-inc
100 /// mode. For example, expanding {1,+,1}<L> in post-inc mode returns the add
101 /// instruction that adds one to the phi for {0,+,1}<L>, as opposed to a new
102 /// phi starting at 1. This is only supported in non-canonical mode.
103 PostIncLoopSet PostIncLoops;
104
105 /// When this is non-null, addrecs expanded in the loop it indicates should
106 /// be inserted with increments at IVIncInsertPos.
107 const Loop *IVIncInsertLoop;
108
109 /// When expanding addrecs in the IVIncInsertLoop loop, insert the IV
110 /// increment at this position.
111 Instruction *IVIncInsertPos;
112
113 /// Phis that complete an IV chain. Reuse
115
116 /// When true, SCEVExpander tries to expand expressions in "canonical" form.
117 /// When false, expressions are expanded in a more literal form.
118 ///
119 /// In "canonical" form addrecs are expanded as arithmetic based on a
120 /// canonical induction variable. Note that CanonicalMode doesn't guarantee
121 /// that all expressions are expanded in "canonical" form. For some
122 /// expressions literal mode can be preferred.
123 bool CanonicalMode;
124
125 /// When invoked from LSR, the expander is in "strength reduction" mode. The
126 /// only difference is that phi's are only reused if they are already in
127 /// "expanded" form.
128 bool LSRMode;
129
130 /// When true, rewrite any divisors of UDiv expressions that may be 0 to
131 /// umax(Divisor, 1) to avoid introducing UB. If the divisor may be poison,
132 /// freeze it first.
133 bool SafeUDivMode = false;
134
136 BuilderType Builder;
137
138 // RAII object that stores the current insertion point and restores it when
139 // the object is destroyed. This includes the debug location. Duplicated
140 // from InsertPointGuard to add SetInsertPoint() which is used to updated
141 // InsertPointGuards stack when insert points are moved during SCEV
142 // expansion.
143 class SCEVInsertPointGuard {
144 IRBuilderBase &Builder;
147 DebugLoc DbgLoc;
148 SCEVExpander *SE;
149
150 SCEVInsertPointGuard(const SCEVInsertPointGuard &) = delete;
151 SCEVInsertPointGuard &operator=(const SCEVInsertPointGuard &) = delete;
152
153 public:
154 SCEVInsertPointGuard(IRBuilderBase &B, SCEVExpander *SE)
155 : Builder(B), Block(B.GetInsertBlock()), Point(B.GetInsertPoint()),
156 DbgLoc(B.getCurrentDebugLocation()), SE(SE) {
157 SE->InsertPointGuards.push_back(this);
158 }
159
160 ~SCEVInsertPointGuard() {
161 // These guards should always created/destroyed in FIFO order since they
162 // are used to guard lexically scoped blocks of code in
163 // ScalarEvolutionExpander.
164 assert(SE->InsertPointGuards.back() == this);
165 SE->InsertPointGuards.pop_back();
166 Builder.restoreIP(IRBuilderBase::InsertPoint(Block, Point));
167 Builder.SetCurrentDebugLocation(DbgLoc);
168 }
169
170 BasicBlock::iterator GetInsertPoint() const { return Point; }
171 void SetInsertPoint(BasicBlock::iterator I) { Point = I; }
172 };
173
174 /// Stack of pointers to saved insert points, used to keep insert points
175 /// consistent when instructions are moved.
177
178#if LLVM_ENABLE_ABI_BREAKING_CHECKS
179 const char *DebugType;
180#endif
181
182 friend struct SCEVUseVisitor<SCEVExpander, Value *>;
183
184public:
185 /// Construct a SCEVExpander in "canonical" mode.
186 explicit SCEVExpander(ScalarEvolution &SE, const char *Name,
187 bool PreserveLCSSA = true)
188 : SE(SE), DL(SE.getDataLayout()), IVName(Name),
189 PreserveLCSSA(PreserveLCSSA), IVIncInsertLoop(nullptr),
190 IVIncInsertPos(nullptr), CanonicalMode(true), LSRMode(false),
191 Builder(SE.getContext(), InstSimplifyFolder(DL),
193 [this](Instruction *I) { rememberInstruction(I); })) {
194#if LLVM_ENABLE_ABI_BREAKING_CHECKS
195 DebugType = "";
196#endif
197 }
198
200 // Make sure the insert point guard stack is consistent.
201 assert(InsertPointGuards.empty());
202 }
203
204#if LLVM_ENABLE_ABI_BREAKING_CHECKS
205 void setDebugType(const char *s) { DebugType = s; }
206#endif
207
208 /// Erase the contents of the InsertedExpressions map so that users trying
209 /// to expand the same expression into multiple BasicBlocks or different
210 /// places within the same BasicBlock can do so.
211 void clear() {
212 InsertedExpressions.clear();
213 InsertedValues.clear();
214 InsertedPostIncValues.clear();
215 ReusedValues.clear();
216 OrigFlags.clear();
217 ChainedPhis.clear();
218 InsertedIVs.clear();
219 }
220
221 ScalarEvolution *getSE() { return &SE; }
222 const SmallVectorImpl<WeakVH> &getInsertedIVs() const { return InsertedIVs; }
223
224 /// Return a vector containing all instructions inserted during expansion.
227 for (const auto &VH : InsertedValues) {
228 Value *V = VH;
229 if (ReusedValues.contains(V))
230 continue;
231 if (auto *Inst = dyn_cast<Instruction>(V))
232 Result.push_back(Inst);
233 }
234 for (const auto &VH : InsertedPostIncValues) {
235 Value *V = VH;
236 if (ReusedValues.contains(V))
237 continue;
238 if (auto *Inst = dyn_cast<Instruction>(V))
239 Result.push_back(Inst);
240 }
241
242 return Result;
243 }
244
245 /// Return true for expressions that can't be evaluated at runtime
246 /// within given \b Budget.
247 ///
248 /// \p At is a parameter which specifies point in code where user is going to
249 /// expand these expressions. Sometimes this knowledge can lead to
250 /// a less pessimistic cost estimation.
252 unsigned Budget, const TargetTransformInfo *TTI,
253 const Instruction *At) {
254 assert(TTI && "This function requires TTI to be provided.");
255 assert(At && "This function requires At instruction to be provided.");
256 if (!TTI) // In assert-less builds, avoid crashing
257 return true; // by always claiming to be high-cost.
261 unsigned ScaledBudget = Budget * TargetTransformInfo::TCC_Basic;
262 for (auto *Expr : Exprs)
263 Worklist.emplace_back(-1, -1, Expr);
264 while (!Worklist.empty()) {
265 const SCEVOperand WorkItem = Worklist.pop_back_val();
266 if (isHighCostExpansionHelper(WorkItem, L, *At, Cost, ScaledBudget, *TTI,
267 Processed, Worklist))
268 return true;
269 }
270 assert(Cost <= ScaledBudget && "Should have returned from inner loop.");
271 return false;
272 }
273
274 /// Return the induction variable increment's IV operand.
276 getIVIncOperand(Instruction *IncV, Instruction *InsertPos, bool allowScale);
277
278 /// Utility for hoisting \p IncV (with all subexpressions requried for its
279 /// computation) before \p InsertPos. If \p RecomputePoisonFlags is set, drops
280 /// all poison-generating flags from instructions being hoisted and tries to
281 /// re-infer them in the new location. It should be used when we are going to
282 /// introduce a new use in the new position that didn't exist before, and may
283 /// trigger new UB in case of poison.
284 LLVM_ABI bool hoistIVInc(Instruction *IncV, Instruction *InsertPos,
285 bool RecomputePoisonFlags = false);
286
287 /// Return true if both increments directly increment the corresponding IV PHI
288 /// nodes and have the same opcode. It is not safe to re-use the flags from
289 /// the original increment, if it is more complex and SCEV expansion may have
290 /// yielded a more simplified wider increment.
292 PHINode *WidePhi,
293 Instruction *OrigInc,
294 Instruction *WideInc);
295
296 /// replace congruent phis with their most canonical representative. Return
297 /// the number of phis eliminated.
298 LLVM_ABI unsigned
301 const TargetTransformInfo *TTI = nullptr);
302
303 /// Return true if the given expression is safe to expand in the sense that
304 /// all materialized values are safe to speculate anywhere their operands are
305 /// defined, and the expander is capable of expanding the expression.
306 LLVM_ABI bool isSafeToExpand(const SCEV *S) const;
307
308 /// Return true if the given expression is safe to expand in the sense that
309 /// all materialized values are defined and safe to speculate at the specified
310 /// location and their operands are defined at this location.
311 LLVM_ABI bool isSafeToExpandAt(const SCEV *S,
312 const Instruction *InsertionPoint) const;
313
314 /// Drop poison-generating flags from \p I, then try re-infer via SCEV.
315 LLVM_ABI static void
317 Instruction *I);
318
319 /// Insert code to directly compute the specified SCEV expression into the
320 /// program. The code is inserted into the specified block.
323 return expandCodeFor(SH, Ty, I->getIterator());
324 }
325
326 /// Insert code to directly compute the specified SCEV expression into the
327 /// program. The code is inserted into the SCEVExpander's current
328 /// insertion point. If a type is specified, the result will be expanded to
329 /// have that type, with a cast if necessary.
330 LLVM_ABI Value *expandCodeFor(SCEVUse SH, Type *Ty = nullptr);
331
332 /// Generates a code sequence that evaluates this predicate. The inserted
333 /// instructions will be at position \p Loc. The result will be of type i1
334 /// and will have a value of 0 when the predicate is false and 1 otherwise.
337
338 /// A specialized variant of expandCodeForPredicate, handling the case when
339 /// we are expanding code for a SCEVComparePredicate.
342
343 /// Generates code that evaluates if the \p AR expression will overflow.
345 Instruction *Loc, bool Signed);
346
347 /// A specialized variant of expandCodeForPredicate, handling the case when
348 /// we are expanding code for a SCEVWrapPredicate.
351
352 /// A specialized variant of expandCodeForPredicate, handling the case when
353 /// we are expanding code for a SCEVUnionPredicate.
356
357 /// Set the current IV increment loop and position.
358 void setIVIncInsertPos(const Loop *L, Instruction *Pos) {
359 assert(!CanonicalMode &&
360 "IV increment positions are not supported in CanonicalMode");
361 IVIncInsertLoop = L;
362 IVIncInsertPos = Pos;
363 }
364
365 /// Enable post-inc expansion for addrecs referring to the given
366 /// loops. Post-inc expansion is only supported in non-canonical mode.
367 void setPostInc(const PostIncLoopSet &L) {
368 assert(!CanonicalMode &&
369 "Post-inc expansion is not supported in CanonicalMode");
370 PostIncLoops = L;
371 }
372
373 /// Disable all post-inc expansion.
375 PostIncLoops.clear();
376
377 // When we change the post-inc loop set, cached expansions may no
378 // longer be valid.
379 InsertedPostIncValues.clear();
380 }
381
382 /// Disable the behavior of expanding expressions in canonical form rather
383 /// than in a more literal form. Non-canonical mode is useful for late
384 /// optimization passes.
385 void disableCanonicalMode() { CanonicalMode = false; }
386
387 void enableLSRMode() { LSRMode = true; }
388
389 /// Set the current insertion point. This is useful if multiple calls to
390 /// expandCodeFor() are going to be made with the same insert point and the
391 /// insert point may be moved during one of the expansions (e.g. if the
392 /// insert point is not a block terminator).
394 assert(IP);
395 Builder.SetInsertPoint(IP);
396 }
397
399 Builder.SetInsertPoint(IP->getParent(), IP);
400 }
401
402 /// Clear the current insertion point. This is useful if the instruction
403 /// that had been serving as the insertion point may have been deleted.
404 void clearInsertPoint() { Builder.ClearInsertionPoint(); }
405
406 /// Set location information used by debugging information.
408 Builder.SetCurrentDebugLocation(std::move(L));
409 }
410
411 /// Get location information used by debugging information.
413 return Builder.getCurrentDebugLocation();
414 }
415
416 /// Return true if the specified instruction was inserted by the code
417 /// rewriter. If so, the client should not modify the instruction. Note that
418 /// this also includes instructions re-used during expansion.
420 return InsertedValues.count(I) || InsertedPostIncValues.count(I);
421 }
422
423 void setChainedPhi(PHINode *PN) { ChainedPhis.insert(PN); }
424
425 /// Determine whether there is an existing expansion of S that can be reused.
426 /// This is used to check whether S can be expanded cheaply.
427 ///
428 /// L is a hint which tells in which loop to look for the suitable value.
429 ///
430 /// Note that this function does not perform an exhaustive search. I.e if it
431 /// didn't find any value it does not mean that there is no such value.
433 const Instruction *At, Loop *L);
434
435 /// Returns a suitable insert point after \p I, that dominates \p
436 /// MustDominate. Skips instructions inserted by the expander.
438 findInsertPointAfter(Instruction *I, Instruction *MustDominate) const;
439
440 /// Remove inserted instructions that are dead, e.g. due to InstSimplifyFolder
441 /// simplifications. \p Root is assumed to be used and won't be removed.
443
444private:
445 LLVMContext &getContext() const { return SE.getContext(); }
446
447 /// Recursive helper function for isHighCostExpansion.
448 LLVM_ABI bool
449 isHighCostExpansionHelper(const SCEVOperand &WorkItem, Loop *L,
450 const Instruction &At, InstructionCost &Cost,
451 unsigned Budget, const TargetTransformInfo &TTI,
452 SmallPtrSetImpl<const SCEV *> &Processed,
453 SmallVectorImpl<SCEVOperand> &Worklist);
454
455 /// Insert the specified binary operator, doing a small amount of work to
456 /// avoid inserting an obviously redundant operation, and hoisting to an
457 /// outer loop when the opportunity is there and it is safe.
458 Value *InsertBinop(Instruction::BinaryOps Opcode, Value *LHS, Value *RHS,
459 SCEV::NoWrapFlags Flags, bool IsSafeToHoist);
460
461 /// We want to cast \p V. What would be the best place for such a cast?
462 BasicBlock::iterator GetOptimalInsertionPointForCastOf(Value *V) const;
463
464 /// Arrange for there to be a cast of V to Ty at IP, reusing an existing
465 /// cast if a suitable one exists, moving an existing cast if a suitable one
466 /// exists but isn't in the right place, or creating a new one.
467 Value *ReuseOrCreateCast(Value *V, Type *Ty, Instruction::CastOps Op,
469
470 /// Insert a cast of V to the specified type, which must be possible with a
471 /// noop cast, doing what we can to share the casts.
472 Value *InsertNoopCastOfTo(Value *V, Type *Ty);
473
474 /// Expand a SCEVAddExpr with a pointer type into a GEP instead of using
475 /// ptrtoint+arithmetic+inttoptr.
476 Value *expandAddToGEP(const SCEV *Op, Value *V, SCEV::NoWrapFlags Flags);
477
478 /// Find a previous Value in ExprValueMap for expand.
479 /// DropPoisonGeneratingInsts is populated with instructions for which
480 /// poison-generating flags must be dropped if the value is reused.
481 Value *FindValueInExprValueMap(
482 SCEVUse S, const Instruction *InsertPt,
483 SmallVectorImpl<Instruction *> &DropPoisonGeneratingInsts);
484
485 LLVM_ABI Value *expand(SCEVUse S);
488 return expand(S);
489 }
490 Value *expand(SCEVUse S, Instruction *I) {
492 return expand(S);
493 }
494
495 /// Determine the most "relevant" loop for the given SCEV.
496 const Loop *getRelevantLoop(const SCEV *);
497
498 Value *expandMinMaxExpr(SCEVUseT<const SCEVNAryExpr *> S,
499 Intrinsic::ID IntrinID, Twine Name,
500 bool IsSequential = false);
501
502 Value *visitConstant(SCEVUseT<const SCEVConstant *> S) {
503 return S->getValue();
504 }
505
506 Value *visitVScale(SCEVUseT<const SCEVVScale *> S);
507
508 Value *visitPtrToAddrExpr(SCEVUseT<const SCEVPtrToAddrExpr *> S);
509
510 Value *visitPtrToIntExpr(SCEVUseT<const SCEVPtrToIntExpr *> S);
511
512 Value *visitTruncateExpr(SCEVUseT<const SCEVTruncateExpr *> S);
513
514 Value *visitZeroExtendExpr(SCEVUseT<const SCEVZeroExtendExpr *> S);
515
516 Value *visitSignExtendExpr(SCEVUseT<const SCEVSignExtendExpr *> S);
517
518 Value *visitAddExpr(SCEVUseT<const SCEVAddExpr *> S);
519
520 Value *visitMulExpr(SCEVUseT<const SCEVMulExpr *> S);
521
522 Value *visitUDivExpr(SCEVUseT<const SCEVUDivExpr *> S);
523
524 Value *visitAddRecExpr(SCEVUseT<const SCEVAddRecExpr *> S);
525
526 Value *visitSMaxExpr(SCEVUseT<const SCEVSMaxExpr *> S);
527
528 Value *visitUMaxExpr(SCEVUseT<const SCEVUMaxExpr *> S);
529
530 Value *visitSMinExpr(SCEVUseT<const SCEVSMinExpr *> S);
531
532 Value *visitUMinExpr(SCEVUseT<const SCEVUMinExpr *> S);
533
534 Value *visitSequentialUMinExpr(SCEVUseT<const SCEVSequentialUMinExpr *> S);
535
536 Value *visitUnknown(SCEVUseT<const SCEVUnknown *> S) { return S->getValue(); }
537
538 LLVM_ABI void rememberInstruction(Value *I);
539
540 void rememberFlags(Instruction *I);
541
542 bool isNormalAddRecExprPHI(PHINode *PN, Instruction *IncV, const Loop *L);
543
544 bool isExpandedAddRecExprPHI(PHINode *PN, Instruction *IncV, const Loop *L);
545
546 Value *tryToReuseLCSSAPhi(SCEVUseT<const SCEVAddRecExpr *> S);
547 Value *expandAddRecExprLiterally(SCEVUseT<const SCEVAddRecExpr *> S);
548 PHINode *getAddRecExprPHILiterally(const SCEVAddRecExpr *Normalized,
549 const Loop *L, Type *&TruncTy,
550 bool &InvertStep);
551 Value *expandIVInc(PHINode *PN, Value *StepV, const Loop *L,
552 bool useSubtract);
553
554 void fixupInsertPoints(Instruction *I);
555
556 /// Create LCSSA PHIs for \p V, if it is required for uses at the Builder's
557 /// current insertion point.
558 Value *fixupLCSSAFormFor(Value *V);
559
560 /// Replace congruent phi increments with their most canonical representative.
561 /// May swap \p Phi and \p OrigPhi, if \p Phi is more canonical, due to its
562 /// increment.
563 void replaceCongruentIVInc(PHINode *&Phi, PHINode *&OrigPhi, Loop *L,
564 const DominatorTree *DT,
565 SmallVectorImpl<WeakTrackingVH> &DeadInsts);
566};
567
568/// Helper to remove instructions inserted during SCEV expansion, unless they
569/// are marked as used.
571 SCEVExpander &Expander;
572
573 /// Indicates whether the result of the expansion is used. If false, the
574 /// instructions added during expansion are removed.
575 bool ResultUsed;
576
577public:
579 : Expander(Expander), ResultUsed(false) {}
580
582
583 /// Indicate that the result of the expansion is used.
584 void markResultUsed() { ResultUsed = true; }
585
586 LLVM_ABI void cleanup();
587};
588} // namespace llvm
589
590#endif
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
#define LLVM_ABI
Definition Compiler.h:213
This file defines the DenseMap class.
This file defines the DenseSet and SmallDenseSet classes.
static Expected< BitVector > expand(StringRef S, StringRef Original)
This file defines an InstructionCost class that is used when calculating the cost of an instruction,...
#define I(x, y, z)
Definition MD5.cpp:57
#define P(N)
This file defines the SmallVector class.
This pass exposes codegen information to IR-level passes.
Value * RHS
Value * LHS
Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:40
Value handle that asserts if the Value is deleted.
InstListType::iterator iterator
Instruction iterators...
Definition BasicBlock.h:170
A parsed version of the target data layout string in and methods for querying it.
Definition DataLayout.h:64
A debug info location.
Definition DebugLoc.h:124
Implements a dense probed hash-table based set.
Definition DenseSet.h:289
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree.
Definition Dominators.h:159
Represents flags for the getelementptr instruction/expression.
InsertPoint - A saved insertion point.
Definition IRBuilder.h:298
Common base class shared among various IRBuilders.
Definition IRBuilder.h:114
Provides an 'InsertHelper' that calls a user-provided callback after performing the default insertion...
Definition IRBuilder.h:75
This provides a uniform API for creating instructions and inserting them into a basic block: either a...
Definition IRBuilder.h:2858
InstSimplifyFolder - Use InstructionSimplify to fold operations to existing values.
This is an important class for using LLVM in a threaded context.
Definition LLVMContext.h:68
Represents a single loop in the control flow graph.
Definition LoopInfo.h:40
This node represents a polynomial recurrence on the trip count of the specified loop.
This class represents an assumption that the expression LHS Pred RHS evaluates to true,...
SCEVExpanderCleaner(SCEVExpander &Expander)
void markResultUsed()
Indicate that the result of the expansion is used.
This class uses information about analyze scalars to rewrite expressions in canonical form.
LLVM_ABI Value * generateOverflowCheck(const SCEVAddRecExpr *AR, Instruction *Loc, bool Signed)
Generates code that evaluates if the AR expression will overflow.
LLVM_ABI bool hasRelatedExistingExpansion(const SCEV *S, const Instruction *At, Loop *L)
Determine whether there is an existing expansion of S that can be reused.
SmallVector< Instruction *, 32 > getAllInsertedInstructions() const
Return a vector containing all instructions inserted during expansion.
void setChainedPhi(PHINode *PN)
LLVM_ABI bool isSafeToExpand(const SCEV *S) const
Return true if the given expression is safe to expand in the sense that all materialized values are s...
void setInsertPoint(BasicBlock::iterator IP)
bool isHighCostExpansion(ArrayRef< const SCEV * > Exprs, Loop *L, unsigned Budget, const TargetTransformInfo *TTI, const Instruction *At)
Return true for expressions that can't be evaluated at runtime within given Budget.
LLVM_ABI bool isSafeToExpandAt(const SCEV *S, const Instruction *InsertionPoint) const
Return true if the given expression is safe to expand in the sense that all materialized values are d...
ScalarEvolution * getSE()
LLVM_ABI unsigned replaceCongruentIVs(Loop *L, const DominatorTree *DT, SmallVectorImpl< WeakTrackingVH > &DeadInsts, const TargetTransformInfo *TTI=nullptr)
replace congruent phis with their most canonical representative.
void clearInsertPoint()
Clear the current insertion point.
static LLVM_ABI void dropPoisonGeneratingAnnotationsAndReinfer(ScalarEvolution &SE, Instruction *I)
Drop poison-generating flags from I, then try re-infer via SCEV.
void clearPostInc()
Disable all post-inc expansion.
LLVM_ABI Value * expandUnionPredicate(const SCEVUnionPredicate *Pred, Instruction *Loc)
A specialized variant of expandCodeForPredicate, handling the case when we are expanding code for a S...
LLVM_ABI bool hoistIVInc(Instruction *IncV, Instruction *InsertPos, bool RecomputePoisonFlags=false)
Utility for hoisting IncV (with all subexpressions requried for its computation) before InsertPos.
void clear()
Erase the contents of the InsertedExpressions map so that users trying to expand the same expression ...
bool isInsertedInstruction(Instruction *I) const
Return true if the specified instruction was inserted by the code rewriter.
LLVM_ABI Value * expandCodeForPredicate(const SCEVPredicate *Pred, Instruction *Loc)
Generates a code sequence that evaluates this predicate.
void setPostInc(const PostIncLoopSet &L)
Enable post-inc expansion for addrecs referring to the given loops.
static LLVM_ABI bool canReuseFlagsFromOriginalIVInc(PHINode *OrigPhi, PHINode *WidePhi, Instruction *OrigInc, Instruction *WideInc)
Return true if both increments directly increment the corresponding IV PHI nodes and have the same op...
DebugLoc getCurrentDebugLocation() const
Get location information used by debugging information.
void SetCurrentDebugLocation(DebugLoc L)
Set location information used by debugging information.
LLVM_ABI Value * expandCodeFor(SCEVUse SH, Type *Ty, BasicBlock::iterator I)
Insert code to directly compute the specified SCEV expression into the program.
LLVM_ABI Value * expandComparePredicate(const SCEVComparePredicate *Pred, Instruction *Loc)
A specialized variant of expandCodeForPredicate, handling the case when we are expanding code for a S...
void setIVIncInsertPos(const Loop *L, Instruction *Pos)
Set the current IV increment loop and position.
const SmallVectorImpl< WeakVH > & getInsertedIVs() const
void disableCanonicalMode()
Disable the behavior of expanding expressions in canonical form rather than in a more literal form.
LLVM_ABI Value * expandWrapPredicate(const SCEVWrapPredicate *P, Instruction *Loc)
A specialized variant of expandCodeForPredicate, handling the case when we are expanding code for a S...
SCEVExpander(ScalarEvolution &SE, const char *Name, bool PreserveLCSSA=true)
Construct a SCEVExpander in "canonical" mode.
Value * expandCodeFor(SCEVUse SH, Type *Ty, Instruction *I)
LLVM_ABI Instruction * getIVIncOperand(Instruction *IncV, Instruction *InsertPos, bool allowScale)
Return the induction variable increment's IV operand.
LLVM_ABI void eraseDeadInstructions(Value *Root)
Remove inserted instructions that are dead, e.g.
LLVM_ABI BasicBlock::iterator findInsertPointAfter(Instruction *I, Instruction *MustDominate) const
Returns a suitable insert point after I, that dominates MustDominate.
void setInsertPoint(Instruction *IP)
Set the current insertion point.
This class represents an assumption made using SCEV expressions which can be checked at run-time.
This class represents a composition of other SCEV predicates, and is the class that most clients will...
This class represents an assumption made on an AddRec expression.
This class represents an analyzed expression in the program.
SCEVNoWrapFlags NoWrapFlags
The main scalar evolution driver.
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements.
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
reference emplace_back(ArgTypes &&... Args)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
This pass provides access to the codegen interfaces that are needed for IR-level transformations.
@ TCC_Basic
The cost of a typical 'add' instruction.
Value handle that tracks a Value across RAUW.
The instances of the Type class are immutable: once they are created, they are never changed.
Definition Type.h:46
LLVM Value Representation.
Definition Value.h:75
This is an optimization pass for GlobalISel generic memory operations.
FunctionAddr VTableAddr Value
Definition InstrProf.h:137
InstructionCost Cost
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:643
SCEVUseT(SCEVPtrT) -> SCEVUseT< SCEVPtrT >
Deduction guide for various SCEV subclass pointers.
LLVM_ABI cl::opt< unsigned > SCEVCheapExpansionBudget
TargetTransformInfo TTI
DWARFExpression::Operation Op
SmallPtrSet< const Loop *, 2 > PostIncLoopSet
SCEVUseT< const SCEV * > SCEVUse
LLVM_ABI void apply(Instruction *I)
LLVM_ABI PoisonFlags(const Instruction *I)
struct for holding enough information to help calculate the cost of the given SCEV when expanded into...
const SCEV * S
The SCEV operand to be costed.
unsigned ParentOpcode
LLVM instruction opcode that uses the operand.
SCEVOperand(unsigned Opc, int Idx, const SCEV *S)
int OperandIdx
The use index of an expanded instruction.
A visitor class for SCEVUse.