13#ifndef LLVM_TRANSFORMS_UTILS_SCALAREVOLUTIONEXPANDER_H
14#define LLVM_TRANSFORMS_UTILS_SCALAREVOLUTIONEXPANDER_H
108 const Loop *IVIncInsertLoop;
134 bool SafeUDivMode =
false;
144 class SCEVInsertPointGuard {
151 SCEVInsertPointGuard(
const SCEVInsertPointGuard &) =
delete;
152 SCEVInsertPointGuard &operator=(
const SCEVInsertPointGuard &) =
delete;
156 : Builder(
B),
Block(
B.GetInsertBlock()), Point(
B.GetInsertPoint()),
158 SE->InsertPointGuards.push_back(
this);
161 ~SCEVInsertPointGuard() {
168 Builder.SetCurrentDebugLocation(DbgLoc);
179#if LLVM_ENABLE_ABI_BREAKING_CHECKS
180 const char *DebugType;
188 bool PreserveLCSSA =
true)
189 : SE(SE), DL(SE.getDataLayout()), IVName(Name),
190 PreserveLCSSA(PreserveLCSSA), IVIncInsertLoop(nullptr),
191 IVIncInsertPos(nullptr), CanonicalMode(
true), LSRMode(
false),
195#if LLVM_ENABLE_ABI_BREAKING_CHECKS
202 assert(InsertPointGuards.empty());
205#if LLVM_ENABLE_ABI_BREAKING_CHECKS
206 void setDebugType(
const char *s) { DebugType = s; }
213 InsertedExpressions.clear();
214 InsertedValues.clear();
215 InsertedPostIncValues.clear();
216 ReusedValues.clear();
228 for (
const auto &VH : InsertedValues) {
230 if (ReusedValues.contains(V))
233 Result.push_back(Inst);
235 for (
const auto &VH : InsertedPostIncValues) {
237 if (ReusedValues.contains(V))
240 Result.push_back(Inst);
255 assert(
TTI &&
"This function requires TTI to be provided.");
256 assert(At &&
"This function requires At instruction to be provided.");
263 for (
auto *Expr : Exprs)
265 while (!Worklist.
empty()) {
267 if (isHighCostExpansionHelper(
WorkItem, L, *At,
Cost, ScaledBudget, *
TTI,
268 Processed, Worklist))
271 assert(
Cost <= ScaledBudget &&
"Should have returned from inner loop.");
286 bool RecomputePoisonFlags =
false);
368 "IV increment positions are not supported in CanonicalMode");
370 IVIncInsertPos = Pos;
377 "Post-inc expansion is not supported in CanonicalMode");
383 PostIncLoops.clear();
387 InsertedPostIncValues.clear();
403 Builder.SetInsertPoint(IP);
407 Builder.SetInsertPoint(IP->getParent(), IP);
416 Builder.SetCurrentDebugLocation(std::move(L));
421 return Builder.getCurrentDebugLocation();
428 return InsertedValues.count(
I) || InsertedPostIncValues.count(
I);
457 isHighCostExpansionHelper(
const SCEVOperand &
WorkItem, Loop *L,
459 unsigned Budget,
const TargetTransformInfo &
TTI,
460 SmallPtrSetImpl<const SCEV *> &Processed,
461 SmallVectorImpl<SCEVOperand> &Worklist);
480 Value *InsertNoopCastOfTo(Value *V, Type *Ty);
489 Value *FindValueInExprValueMap(
490 SCEVUse S,
const Instruction *InsertPt,
491 SmallVectorImpl<Instruction *> &DropPoisonGeneratingInsts);
504 const Loop *getRelevantLoop(
const SCEV *);
508 bool IsSequential =
false);
511 return S->getValue();
548 void rememberFlags(Instruction *
I);
550 bool isNormalAddRecExprPHI(PHINode *PN, Instruction *IncV,
const Loop *L);
552 bool isExpandedAddRecExprPHI(PHINode *PN, Instruction *IncV,
const Loop *L);
556 PHINode *getAddRecExprPHILiterally(
const SCEVAddRecExpr *Normalized,
557 const Loop *L,
Type *&TruncTy,
559 Value *expandIVInc(PHINode *PN,
Value *StepV,
const Loop *L,
562 void fixupInsertPoints(Instruction *
I);
571 void replaceCongruentIVInc(PHINode *&Phi, PHINode *&OrigPhi, Loop *L,
572 const DominatorTree *DT,
573 SmallVectorImpl<WeakTrackingVH> &DeadInsts);
587 : Expander(Expander), ResultUsed(
false) {}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
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,...
Func getContext().diagnose(DiagnosticInfoUnsupported(Func
This file defines the SmallVector class.
Represent a constant reference to an array (0 or more elements consecutively in memory),...
Value handle that asserts if the Value is deleted.
InstListType::iterator iterator
Instruction iterators...
This is the base class for all instructions that perform data casts.
A parsed version of the target data layout string in and methods for querying it.
Implements a dense probed hash-table based set.
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree.
Represents flags for the getelementptr instruction/expression.
InsertPoint - A saved insertion point.
Common base class shared among various IRBuilders.
Provides an 'InsertHelper' that calls a user-provided callback after performing the default insertion...
This provides a uniform API for creating instructions and inserting them into a basic block: either a...
InstSimplifyFolder - Use InstructionSimplify to fold operations to existing values.
This is an important class for using LLVM in a threaded context.
Represents a single loop in the control flow graph.
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.
friend class SCEVExpanderCleaner
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...
static LLVM_ABI CastInst * findReusableCastForPtrToAddr(Value *PtrOp, Type *Ty, const DataLayout &DL, function_ref< bool(const CastInst *)> Dominates)
Find an existing cast among PtrOp's users that computes the same value as a ptrtoaddr of PtrOp to Ty ...
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.
Value handle that tracks a Value across RAUW.
The instances of the Type class are immutable: once they are created, they are never changed.
LLVM Value Representation.
An efficient, type-erasing, non-owning reference to a callable.
This is an optimization pass for GlobalISel generic memory operations.
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
RelativeUniformCounterPtr ValuesPtrExpr VTableAddr Value
SCEVUseT(SCEVPtrT) -> SCEVUseT< SCEVPtrT >
Deduction guide for various SCEV subclass pointers.
LLVM_ABI cl::opt< unsigned > SCEVCheapExpansionBudget
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.