LLVM 22.0.0git
|
This class uses information about analyze scalars to rewrite expressions in canonical form. More...
#include "llvm/Transforms/Utils/ScalarEvolutionExpander.h"
Public Member Functions | |
SCEVExpander (ScalarEvolution &se, const DataLayout &DL, const char *name, bool PreserveLCSSA=true) | |
Construct a SCEVExpander in "canonical" mode. | |
~SCEVExpander () | |
void | clear () |
Erase the contents of the InsertedExpressions map so that users trying to expand the same expression into multiple BasicBlocks or different places within the same BasicBlock can do so. | |
ScalarEvolution * | getSE () |
const SmallVectorImpl< WeakVH > & | getInsertedIVs () const |
SmallVector< Instruction *, 32 > | getAllInsertedInstructions () const |
Return a vector containing all instructions inserted during expansion. | |
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 Instruction * | getIVIncOperand (Instruction *IncV, Instruction *InsertPos, bool allowScale) |
Return the induction variable increment's IV operand. | |
LLVM_ABI bool | hoistIVInc (Instruction *IncV, Instruction *InsertPos, bool RecomputePoisonFlags=false) |
Utility for hoisting IncV (with all subexpressions requried for its computation) before InsertPos . | |
LLVM_ABI unsigned | replaceCongruentIVs (Loop *L, const DominatorTree *DT, SmallVectorImpl< WeakTrackingVH > &DeadInsts, const TargetTransformInfo *TTI=nullptr) |
replace congruent phis with their most canonical representative. | |
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 safe to speculate anywhere their operands are defined, and the expander is capable of expanding the expression. | |
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 defined and safe to speculate at the specified location and their operands are defined at this location. | |
LLVM_ABI Value * | expandCodeFor (const SCEV *SH, Type *Ty, BasicBlock::iterator I) |
Insert code to directly compute the specified SCEV expression into the program. | |
Value * | expandCodeFor (const SCEV *SH, Type *Ty, Instruction *I) |
LLVM_ABI Value * | expandCodeFor (const SCEV *SH, Type *Ty=nullptr) |
Insert code to directly compute the specified SCEV expression into the program. | |
LLVM_ABI Value * | expandCodeForPredicate (const SCEVPredicate *Pred, Instruction *Loc) |
Generates a code sequence that evaluates this predicate. | |
LLVM_ABI Value * | expandComparePredicate (const SCEVComparePredicate *Pred, Instruction *Loc) |
A specialized variant of expandCodeForPredicate, handling the case when we are expanding code for a SCEVComparePredicate. | |
LLVM_ABI Value * | generateOverflowCheck (const SCEVAddRecExpr *AR, Instruction *Loc, bool Signed) |
Generates code that evaluates if the AR expression will overflow. | |
LLVM_ABI Value * | expandWrapPredicate (const SCEVWrapPredicate *P, Instruction *Loc) |
A specialized variant of expandCodeForPredicate, handling the case when we are expanding code for a SCEVWrapPredicate. | |
LLVM_ABI Value * | expandUnionPredicate (const SCEVUnionPredicate *Pred, Instruction *Loc) |
A specialized variant of expandCodeForPredicate, handling the case when we are expanding code for a SCEVUnionPredicate. | |
void | setIVIncInsertPos (const Loop *L, Instruction *Pos) |
Set the current IV increment loop and position. | |
void | setPostInc (const PostIncLoopSet &L) |
Enable post-inc expansion for addrecs referring to the given loops. | |
void | clearPostInc () |
Disable all post-inc expansion. | |
void | disableCanonicalMode () |
Disable the behavior of expanding expressions in canonical form rather than in a more literal form. | |
void | enableLSRMode () |
void | setInsertPoint (Instruction *IP) |
Set the current insertion point. | |
void | setInsertPoint (BasicBlock::iterator IP) |
void | clearInsertPoint () |
Clear the current insertion point. | |
void | SetCurrentDebugLocation (DebugLoc L) |
Set location information used by debugging information. | |
DebugLoc | getCurrentDebugLocation () const |
Get location information used by debugging information. | |
bool | isInsertedInstruction (Instruction *I) const |
Return true if the specified instruction was inserted by the code rewriter. | |
void | setChainedPhi (PHINode *PN) |
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. | |
LLVM_ABI BasicBlock::iterator | findInsertPointAfter (Instruction *I, Instruction *MustDominate) const |
Returns a suitable insert point after I , that dominates MustDominate . | |
void | eraseDeadInstructions (Value *Root) |
Remove inserted instructions that are dead, e.g. | |
Public Member Functions inherited from llvm::SCEVVisitor< SCEVExpander, Value * > | |
Value * | visit (const SCEV *S) |
Value * | visitCouldNotCompute (const SCEVCouldNotCompute *S) |
Static Public Member Functions | |
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 opcode. |
Friends | |
class | SCEVExpanderCleaner |
struct | SCEVVisitor< SCEVExpander, Value * > |
This class uses information about analyze scalars to rewrite expressions in canonical form.
Clients should create an instance of this class when rewriting is needed, and destroy it when finished to allow the release of the associated memory.
Definition at line 64 of file ScalarEvolutionExpander.h.
|
inlineexplicit |
Construct a SCEVExpander in "canonical" mode.
Definition at line 186 of file ScalarEvolutionExpander.h.
Referenced by SCEVExpanderCleaner, and SCEVVisitor< SCEVExpander, Value * >.
|
inline |
Definition at line 199 of file ScalarEvolutionExpander.h.
References assert().
|
static |
Return true if both increments directly increment the corresponding IV PHI nodes and have the same opcode.
It is not safe to re-use the flags from the original increment, if it is more complex and SCEV expansion may have yielded a more simplified wider increment.
Definition at line 870 of file ScalarEvolutionExpander.cpp.
References llvm::Instruction::getOpcode(), llvm::PatternMatch::m_c_BinOp(), llvm::PatternMatch::m_Specific(), llvm::PatternMatch::m_Value(), and llvm::PatternMatch::match().
|
inline |
Erase the contents of the InsertedExpressions map so that users trying to expand the same expression into multiple BasicBlocks or different places within the same BasicBlock can do so.
Definition at line 211 of file ScalarEvolutionExpander.h.
Referenced by RunTermFold().
|
inline |
Clear the current insertion point.
This is useful if the instruction that had been serving as the insertion point may have been deleted.
Definition at line 400 of file ScalarEvolutionExpander.h.
|
inline |
Disable all post-inc expansion.
Definition at line 370 of file ScalarEvolutionExpander.h.
|
inline |
Disable the behavior of expanding expressions in canonical form rather than in a more literal form.
Non-canonical mode is useful for late optimization passes.
Definition at line 381 of file ScalarEvolutionExpander.h.
|
inline |
Definition at line 383 of file ScalarEvolutionExpander.h.
void SCEVExpander::eraseDeadInstructions | ( | Value * | Root | ) |
Remove inserted instructions that are dead, e.g.
due to InstSimplifyFolder simplifications. Root
is assumed to be used and won't be removed.
Definition at line 179 of file ScalarEvolutionExpander.cpp.
References llvm::append_range(), llvm::SmallPtrSetImpl< PtrType >::contains(), llvm::dyn_cast(), llvm::SmallVectorTemplateCommon< T, typename >::empty(), getAllInsertedInstructions(), I, llvm::SmallPtrSetImpl< PtrType >::insert(), isInsertedInstruction(), llvm::isInstructionTriviallyDead(), and llvm::SmallVectorImpl< T >::pop_back_val().
Referenced by llvm::addDiffRuntimeChecks().
Value * SCEVExpander::expandCodeFor | ( | const SCEV * | SH, |
Type * | Ty, | ||
BasicBlock::iterator | I ) |
Insert code to directly compute the specified SCEV expression into the program.
The code is inserted into the specified block.
Definition at line 1511 of file ScalarEvolutionExpander.cpp.
References expandCodeFor(), and setInsertPoint().
Referenced by llvm::addDiffRuntimeChecks(), expandCodeFor(), expandCodeFor(), llvm::VPlanTransforms::expandSCEVs(), llvm::LoopStructure::parseLoopStructure(), llvm::peelLoop(), llvm::LoopConstrainer::run(), RunTermFold(), llvm::splitLoopBound(), and llvm::UnrollRuntimeLoopRemainder().
|
inline |
Definition at line 318 of file ScalarEvolutionExpander.h.
References expandCodeFor(), and I.
Insert code to directly compute the specified SCEV expression into the program.
The code is inserted into the SCEVExpander's current insertion point. If a type is specified, the result will be expanded to have that type, with a cast if necessary.
Definition at line 1518 of file ScalarEvolutionExpander.cpp.
References assert(), and llvm::SCEV::getType().
Value * SCEVExpander::expandCodeForPredicate | ( | const SCEVPredicate * | Pred, |
Instruction * | Loc ) |
Generates a code sequence that evaluates this predicate.
The inserted instructions will be at position Loc
. The result will be of type i1 and will have a value of 0 when the predicate is false and 1 otherwise.
Definition at line 2133 of file ScalarEvolutionExpander.cpp.
References assert(), llvm::cast(), expandComparePredicate(), expandUnionPredicate(), expandWrapPredicate(), llvm_unreachable, llvm::SCEVPredicate::P_Compare, llvm::SCEVPredicate::P_Union, and llvm::SCEVPredicate::P_Wrap.
Referenced by expandUnionPredicate().
Value * SCEVExpander::expandComparePredicate | ( | const SCEVComparePredicate * | Pred, |
Instruction * | Loc ) |
A specialized variant of expandCodeForPredicate, handling the case when we are expanding code for a SCEVComparePredicate.
Definition at line 2149 of file ScalarEvolutionExpander.cpp.
References llvm::CmpInst::getInversePredicate(), and I.
Referenced by expandCodeForPredicate().
Value * SCEVExpander::expandUnionPredicate | ( | const SCEVUnionPredicate * | Pred, |
Instruction * | Loc ) |
A specialized variant of expandCodeForPredicate, handling the case when we are expanding code for a SCEVUnionPredicate.
Definition at line 2316 of file ScalarEvolutionExpander.cpp.
References llvm::SmallVectorTemplateCommon< T, typename >::empty(), expandCodeForPredicate(), llvm::Value::getContext(), llvm::ConstantInt::getFalse(), and llvm::SmallVectorTemplateBase< T, bool >::push_back().
Referenced by expandCodeForPredicate().
Value * SCEVExpander::expandWrapPredicate | ( | const SCEVWrapPredicate * | P, |
Instruction * | Loc ) |
A specialized variant of expandCodeForPredicate, handling the case when we are expanding code for a SCEVWrapPredicate.
Definition at line 2291 of file ScalarEvolutionExpander.cpp.
References A(), llvm::cast(), generateOverflowCheck(), llvm::Value::getContext(), llvm::ConstantInt::getFalse(), llvm::SCEVWrapPredicate::IncrementNSSW, and llvm::SCEVWrapPredicate::IncrementNUSW.
Referenced by expandCodeForPredicate().
BasicBlock::iterator SCEVExpander::findInsertPointAfter | ( | Instruction * | I, |
Instruction * | MustDominate ) const |
Returns a suitable insert point after I
, that dominates MustDominate
.
Skips instructions inserted by the expander.
Definition at line 153 of file ScalarEvolutionExpander.cpp.
References assert(), llvm::dyn_cast(), llvm::ilist_detail::node_parent_access< NodeTy, ParentTy >::getParent(), I, II, llvm::isa(), and isInsertedInstruction().
Value * SCEVExpander::generateOverflowCheck | ( | const SCEVAddRecExpr * | AR, |
Instruction * | Loc, | ||
bool | Signed ) |
Generates code that evaluates if the AR
expression will overflow.
Definition at line 2160 of file ScalarEvolutionExpander.cpp.
References llvm::Add, assert(), llvm::IntegerType::get(), llvm::Value::getContext(), llvm::ConstantInt::getFalse(), llvm::SCEVAddRecExpr::getLoop(), llvm::APInt::getMaxValue(), llvm::SCEVAddRecExpr::getStart(), llvm::SCEVAddRecExpr::getStepRecurrence(), llvm::SCEV::getType(), llvm::SCEVAddRecExpr::getType(), llvm::APInt::getZero(), llvm::CmpInst::ICMP_NE, llvm::CmpInst::ICMP_SGT, llvm::CmpInst::ICMP_SLT, llvm::CmpInst::ICMP_UGT, llvm::CmpInst::ICMP_ULT, llvm::isa(), llvm::SCEVAddRecExpr::isAffine(), llvm::SCEV::isOne(), llvm::Mul, llvm::Signed, llvm::Sub, and llvm::APInt::zext().
Referenced by expandWrapPredicate().
|
inline |
Return a vector containing all instructions inserted during expansion.
Definition at line 225 of file ScalarEvolutionExpander.h.
References llvm::dyn_cast().
Referenced by eraseDeadInstructions().
|
inline |
Get location information used by debugging information.
Definition at line 408 of file ScalarEvolutionExpander.h.
Referenced by SCEVExpanderCleaner.
|
inline |
Definition at line 222 of file ScalarEvolutionExpander.h.
Instruction * SCEVExpander::getIVIncOperand | ( | Instruction * | IncV, |
Instruction * | InsertPos, | ||
bool | allowScale ) |
Return the induction variable increment's IV operand.
getIVIncOperand returns an induction variable increment's induction variable operand.
If allowScale is set, any type of GEP is allowed as long as the nonIV operands dominate InsertPos.
If allowScale is not set, ensure that a GEP increment conforms to one of the simple patterns generated by getAddRecExprPHILiterally and expandAddtoGEP. If the pattern isn't recognized, return NULL.
Definition at line 757 of file ScalarEvolutionExpander.cpp.
References llvm::cast(), llvm::drop_begin(), llvm::dyn_cast(), llvm::Instruction::getOpcode(), llvm::User::getOperand(), llvm::isa(), and llvm::User::operands().
Referenced by hoistIVInc().
|
inline |
Definition at line 221 of file ScalarEvolutionExpander.h.
Referenced by llvm::addDiffRuntimeChecks().
Determine whether there is an existing expansion of S that can be reused.
This is used to check whether S can be expanded cheaply.
L is a hint which tells in which loop to look for the suitable value.
Note that this function does not perform an exhaustive search. I.e if it didn't find any value it does not mean that there is no such value.
Definition at line 1875 of file ScalarEvolutionExpander.cpp.
References llvm::PatternMatch::m_BasicBlock(), llvm::PatternMatch::m_Br(), llvm::PatternMatch::m_ICmp(), llvm::PatternMatch::m_Instruction(), and llvm::PatternMatch::match().
bool SCEVExpander::hoistIVInc | ( | Instruction * | IncV, |
Instruction * | InsertPos, | ||
bool | RecomputePoisonFlags = false ) |
Utility for hoisting IncV
(with all subexpressions requried for its computation) before InsertPos
.
hoistStep - Attempt to hoist a simple IV increment above InsertPos to make it available to other uses in this loop.
If RecomputePoisonFlags
is set, drops all poison-generating flags from instructions being hoisted and tries to re-infer them in the new location. It should be used when we are going to introduce a new use in the new position that didn't exist before, and may trigger new UB in case of poison.
Recursively hoist any operands, until we reach a value that dominates InsertPos.
Definition at line 817 of file ScalarEvolutionExpander.cpp.
References llvm::cast(), llvm::dyn_cast(), llvm::SCEV::FlagNSW, llvm::SCEV::FlagNUW, llvm::ilist_node_impl< OptionsT >::getIterator(), getIVIncOperand(), llvm::ilist_detail::node_parent_access< NodeTy, ParentTy >::getParent(), I, llvm::isa(), llvm::ScalarEvolution::maskFlags(), llvm::SmallVectorTemplateBase< T, bool >::push_back(), llvm::reverse(), llvm::Instruction::setHasNoSignedWrap(), and llvm::Instruction::setHasNoUnsignedWrap().
|
inline |
Return true for expressions that can't be evaluated at runtime within given Budget.
At
is a parameter which specifies point in code where user is going to expand these expressions. Sometimes this knowledge can lead to a less pessimistic cost estimation.
Definition at line 251 of file ScalarEvolutionExpander.h.
References assert(), llvm::SmallVectorImpl< T >::emplace_back(), llvm::SmallVectorTemplateCommon< T, typename >::empty(), llvm::SmallVectorImpl< T >::pop_back_val(), and llvm::TargetTransformInfo::TCC_Basic.
Referenced by canFoldTermCondOfLoop(), shouldPeelLastIteration(), and llvm::UnrollRuntimeLoopRemainder().
|
inline |
Return true if the specified instruction was inserted by the code rewriter.
If so, the client should not modify the instruction. Note that this also includes instructions re-used during expansion.
Definition at line 415 of file ScalarEvolutionExpander.h.
References I.
Referenced by eraseDeadInstructions(), and findInsertPointAfter().
Return true if the given expression is safe to expand in the sense that all materialized values are safe to speculate anywhere their operands are defined, and the expander is capable of expanding the expression.
Definition at line 2426 of file ScalarEvolutionExpander.cpp.
References llvm::visitAll().
Referenced by canFoldTermCondOfLoop(), isSafeToExpandAt(), and RunTermFold().
bool SCEVExpander::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 defined and safe to speculate at the specified location and their operands are defined at this location.
Definition at line 2432 of file ScalarEvolutionExpander.cpp.
References llvm::dyn_cast(), llvm::is_contained(), and isSafeToExpand().
Referenced by llvm::LoopConstrainer::run().
unsigned SCEVExpander::replaceCongruentIVs | ( | Loop * | L, |
const DominatorTree * | DT, | ||
SmallVectorImpl< WeakTrackingVH > & | DeadInsts, | ||
const TargetTransformInfo * | TTI = nullptr ) |
replace congruent phis with their most canonical representative.
replaceCongruentIVs - Check for congruent phis in this loop header and replace them with their most canonical representative.
Return the number of phis eliminated.
Return the number of phis eliminated.
This does not depend on any SCEVExpander state but should be used in the same context that SCEVExpander is used.
Definition at line 1778 of file ScalarEvolutionExpander.cpp.
References llvm::SmallVectorTemplateCommon< T, typename >::back(), llvm::dbgs(), llvm::dyn_cast(), llvm::SmallVectorImpl< T >::emplace_back(), llvm::Value::getType(), llvm::isa(), llvm::Type::isPointerTy(), llvm::make_pointer_range(), SCEV_DEBUG_WITH_TYPE, llvm::simplifyInstruction(), and llvm::stable_sort().
|
inline |
Definition at line 419 of file ScalarEvolutionExpander.h.
|
inline |
Set location information used by debugging information.
Definition at line 403 of file ScalarEvolutionExpander.h.
|
inline |
Definition at line 394 of file ScalarEvolutionExpander.h.
|
inline |
Set the current insertion point.
This is useful if multiple calls to expandCodeFor() are going to be made with the same insert point and the insert point may be moved during one of the expansions (e.g. if the insert point is not a block terminator).
Definition at line 389 of file ScalarEvolutionExpander.h.
References assert().
Referenced by expandCodeFor().
|
inline |
Set the current IV increment loop and position.
Definition at line 354 of file ScalarEvolutionExpander.h.
References assert().
|
inline |
Enable post-inc expansion for addrecs referring to the given loops.
Post-inc expansion is only supported in non-canonical mode.
Definition at line 363 of file ScalarEvolutionExpander.h.
References assert().
|
friend |
Definition at line 65 of file ScalarEvolutionExpander.h.
References B(), llvm::Block, getCurrentDebugLocation(), SCEVExpander(), and SCEVExpanderCleaner.
Referenced by SCEVExpanderCleaner.
|
friend |
Definition at line 176 of file ScalarEvolutionExpander.h.
References SCEVExpander().