LLVM 22.0.0git
llvm::SCEVExpander Class Reference

This class uses information about analyze scalars to rewrite expressions in canonical form. More...

#include "llvm/Transforms/Utils/ScalarEvolutionExpander.h"

Inheritance diagram for llvm::SCEVExpander:
[legend]

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.
ScalarEvolutiongetSE ()
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 InstructiongetIVIncOperand (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 ValueexpandCodeFor (const SCEV *SH, Type *Ty, BasicBlock::iterator I)
 Insert code to directly compute the specified SCEV expression into the program.
ValueexpandCodeFor (const SCEV *SH, Type *Ty, Instruction *I)
LLVM_ABI ValueexpandCodeFor (const SCEV *SH, Type *Ty=nullptr)
 Insert code to directly compute the specified SCEV expression into the program.
LLVM_ABI ValueexpandCodeForPredicate (const SCEVPredicate *Pred, Instruction *Loc)
 Generates a code sequence that evaluates this predicate.
LLVM_ABI ValueexpandComparePredicate (const SCEVComparePredicate *Pred, Instruction *Loc)
 A specialized variant of expandCodeForPredicate, handling the case when we are expanding code for a SCEVComparePredicate.
LLVM_ABI ValuegenerateOverflowCheck (const SCEVAddRecExpr *AR, Instruction *Loc, bool Signed)
 Generates code that evaluates if the AR expression will overflow.
LLVM_ABI ValueexpandWrapPredicate (const SCEVWrapPredicate *P, Instruction *Loc)
 A specialized variant of expandCodeForPredicate, handling the case when we are expanding code for a SCEVWrapPredicate.
LLVM_ABI ValueexpandUnionPredicate (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 * >
Valuevisit (const SCEV *S)
ValuevisitCouldNotCompute (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 * >

Detailed Description

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.

Constructor & Destructor Documentation

◆ SCEVExpander()

llvm::SCEVExpander::SCEVExpander ( ScalarEvolution & se,
const DataLayout & DL,
const char * name,
bool PreserveLCSSA = true )
inlineexplicit

Construct a SCEVExpander in "canonical" mode.

Definition at line 186 of file ScalarEvolutionExpander.h.

References I, and name.

Referenced by SCEVExpanderCleaner, and SCEVVisitor< SCEVExpander, Value * >.

◆ ~SCEVExpander()

llvm::SCEVExpander::~SCEVExpander ( )
inline

Definition at line 199 of file ScalarEvolutionExpander.h.

References assert().

Member Function Documentation

◆ canReuseFlagsFromOriginalIVInc()

bool SCEVExpander::canReuseFlagsFromOriginalIVInc ( PHINode * OrigPhi,
PHINode * WidePhi,
Instruction * OrigInc,
Instruction * WideInc )
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().

◆ clear()

void llvm::SCEVExpander::clear ( )
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().

◆ clearInsertPoint()

void llvm::SCEVExpander::clearInsertPoint ( )
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.

◆ clearPostInc()

void llvm::SCEVExpander::clearPostInc ( )
inline

Disable all post-inc expansion.

Definition at line 370 of file ScalarEvolutionExpander.h.

◆ disableCanonicalMode()

void llvm::SCEVExpander::disableCanonicalMode ( )
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.

◆ enableLSRMode()

void llvm::SCEVExpander::enableLSRMode ( )
inline

Definition at line 383 of file ScalarEvolutionExpander.h.

◆ eraseDeadInstructions()

◆ expandCodeFor() [1/3]

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().

◆ expandCodeFor() [2/3]

Value * llvm::SCEVExpander::expandCodeFor ( const SCEV * SH,
Type * Ty,
Instruction * I )
inline

Definition at line 318 of file ScalarEvolutionExpander.h.

References expandCodeFor(), and I.

◆ expandCodeFor() [3/3]

Value * SCEVExpander::expandCodeFor ( const SCEV * SH,
Type * Ty = nullptr )

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().

◆ expandCodeForPredicate()

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().

◆ expandComparePredicate()

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().

◆ expandUnionPredicate()

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().

◆ expandWrapPredicate()

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().

◆ findInsertPointAfter()

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().

◆ generateOverflowCheck()

◆ getAllInsertedInstructions()

SmallVector< Instruction *, 32 > llvm::SCEVExpander::getAllInsertedInstructions ( ) const
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().

◆ getCurrentDebugLocation()

DebugLoc llvm::SCEVExpander::getCurrentDebugLocation ( ) const
inline

Get location information used by debugging information.

Definition at line 408 of file ScalarEvolutionExpander.h.

Referenced by SCEVExpanderCleaner.

◆ getInsertedIVs()

const SmallVectorImpl< WeakVH > & llvm::SCEVExpander::getInsertedIVs ( ) const
inline

Definition at line 222 of file ScalarEvolutionExpander.h.

◆ getIVIncOperand()

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().

◆ getSE()

ScalarEvolution * llvm::SCEVExpander::getSE ( )
inline

Definition at line 221 of file ScalarEvolutionExpander.h.

Referenced by llvm::addDiffRuntimeChecks().

◆ hasRelatedExistingExpansion()

bool SCEVExpander::hasRelatedExistingExpansion ( const SCEV * S,
const Instruction * At,
Loop * L )

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().

◆ hoistIVInc()

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().

◆ isHighCostExpansion()

bool llvm::SCEVExpander::isHighCostExpansion ( ArrayRef< const SCEV * > Exprs,
Loop * L,
unsigned Budget,
const TargetTransformInfo * TTI,
const Instruction * At )
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().

◆ isInsertedInstruction()

bool llvm::SCEVExpander::isInsertedInstruction ( Instruction * I) const
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().

◆ isSafeToExpand()

bool SCEVExpander::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.

Definition at line 2426 of file ScalarEvolutionExpander.cpp.

References llvm::visitAll().

Referenced by canFoldTermCondOfLoop(), isSafeToExpandAt(), and RunTermFold().

◆ isSafeToExpandAt()

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().

◆ replaceCongruentIVs()

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().

◆ setChainedPhi()

void llvm::SCEVExpander::setChainedPhi ( PHINode * PN)
inline

Definition at line 419 of file ScalarEvolutionExpander.h.

◆ SetCurrentDebugLocation()

void llvm::SCEVExpander::SetCurrentDebugLocation ( DebugLoc L)
inline

Set location information used by debugging information.

Definition at line 403 of file ScalarEvolutionExpander.h.

◆ setInsertPoint() [1/2]

void llvm::SCEVExpander::setInsertPoint ( BasicBlock::iterator IP)
inline

Definition at line 394 of file ScalarEvolutionExpander.h.

◆ setInsertPoint() [2/2]

void llvm::SCEVExpander::setInsertPoint ( Instruction * IP)
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().

◆ setIVIncInsertPos()

void llvm::SCEVExpander::setIVIncInsertPos ( const Loop * L,
Instruction * Pos )
inline

Set the current IV increment loop and position.

Definition at line 354 of file ScalarEvolutionExpander.h.

References assert().

◆ setPostInc()

void llvm::SCEVExpander::setPostInc ( const PostIncLoopSet & L)
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().

◆ SCEVExpanderCleaner

friend class SCEVExpanderCleaner
friend

◆ SCEVVisitor< SCEVExpander, Value * >

friend struct SCEVVisitor< SCEVExpander, Value * >
friend

Definition at line 176 of file ScalarEvolutionExpander.h.

References SCEVExpander().


The documentation for this class was generated from the following files: