LLVM 20.0.0git
Classes | Public Member Functions | Static Public Member Functions | Friends | List of all members
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:
Inheritance graph
[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.
 
InstructiongetIVIncOperand (Instruction *IncV, Instruction *InsertPos, bool allowScale)
 Return the induction variable increment's IV operand.
 
bool hoistIVInc (Instruction *IncV, Instruction *InsertPos, bool RecomputePoisonFlags=false)
 Utility for hoisting IncV (with all subexpressions requried for its computation) before InsertPos.
 
unsigned replaceCongruentIVs (Loop *L, const DominatorTree *DT, SmallVectorImpl< WeakTrackingVH > &DeadInsts, const TargetTransformInfo *TTI=nullptr)
 replace congruent phis with their most canonical representative.
 
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.
 
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.
 
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)
 
ValueexpandCodeFor (const SCEV *SH, Type *Ty=nullptr)
 Insert code to directly compute the specified SCEV expression into the program.
 
ValueexpandCodeForPredicate (const SCEVPredicate *Pred, Instruction *Loc)
 Generates a code sequence that evaluates this predicate.
 
ValueexpandComparePredicate (const SCEVComparePredicate *Pred, Instruction *Loc)
 A specialized variant of expandCodeForPredicate, handling the case when we are expanding code for a SCEVComparePredicate.
 
ValuegenerateOverflowCheck (const SCEVAddRecExpr *AR, Instruction *Loc, bool Signed)
 Generates code that evaluates if the AR expression will overflow.
 
ValueexpandWrapPredicate (const SCEVWrapPredicate *P, Instruction *Loc)
 A specialized variant of expandCodeForPredicate, handling the case when we are expanding code for a SCEVWrapPredicate.
 
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)
 
bool hasRelatedExistingExpansion (const SCEV *S, const Instruction *At, Loop *L)
 Determine whether there is an existing expansion of S that can be reused.
 
BasicBlock::iterator findInsertPointAfter (Instruction *I, Instruction *MustDominate) const
 Returns a suitable insert point after I, that dominates MustDominate.
 
- Public Member Functions inherited from llvm::SCEVVisitor< SCEVExpander, Value * >
Valuevisit (const SCEV *S)
 
ValuevisitCouldNotCompute (const SCEVCouldNotCompute *S)
 

Static Public Member Functions

static 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 61 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 178 of file ScalarEvolutionExpander.h.

References I.

◆ ~SCEVExpander()

llvm::SCEVExpander::~SCEVExpander ( )
inline

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 824 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 203 of file ScalarEvolutionExpander.h.

References llvm::DenseMapBase< DerivedT, KeyT, ValueT, KeyInfoT, BucketT >::clear(), llvm::detail::DenseSetImpl< ValueT, MapTy, ValueInfoT >::clear(), llvm::SmallPtrSetImplBase::clear(), and llvm::SmallVectorImpl< T >::clear().

Referenced by llvm::SCEVExpanderCleaner::cleanup(), and 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 385 of file ScalarEvolutionExpander.h.

References llvm::IRBuilderBase::ClearInsertionPoint().

◆ clearPostInc()

void llvm::SCEVExpander::clearPostInc ( )
inline

◆ 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 366 of file ScalarEvolutionExpander.h.

◆ enableLSRMode()

void llvm::SCEVExpander::enableLSRMode ( )
inline

Definition at line 368 of file ScalarEvolutionExpander.h.

◆ 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 1407 of file ScalarEvolutionExpander.cpp.

References expandCodeFor(), and setInsertPoint().

Referenced by llvm::addDiffRuntimeChecks(), calculateRtStride(), expandCodeFor(), llvm::LoopStructure::parseLoopStructure(), 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 306 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 1414 of file ScalarEvolutionExpander.cpp.

References assert(), llvm::SCEV::getType(), and llvm::ScalarEvolution::getTypeSizeInBits().

◆ 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 2057 of file ScalarEvolutionExpander.cpp.

References assert(), expandComparePredicate(), expandUnionPredicate(), expandWrapPredicate(), llvm::SCEVPredicate::getKind(), 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 
)

◆ expandUnionPredicate()

Value * SCEVExpander::expandUnionPredicate ( const SCEVUnionPredicate Pred,
Instruction Loc 
)

◆ expandWrapPredicate()

Value * SCEVExpander::expandWrapPredicate ( const SCEVWrapPredicate P,
Instruction Loc 
)

◆ 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 138 of file ScalarEvolutionExpander.cpp.

References assert(), llvm::ilist_detail::node_parent_access< NodeTy, ParentTy >::getParent(), I, II, and isInsertedInstruction().

◆ generateOverflowCheck()

Value * SCEVExpander::generateOverflowCheck ( const SCEVAddRecExpr AR,
Instruction Loc,
bool  Signed 
)

◆ getAllInsertedInstructions()

SmallVector< Instruction *, 32 > llvm::SCEVExpander::getAllInsertedInstructions ( ) const
inline

Return a vector containing all instructions inserted during expansion.

Definition at line 217 of file ScalarEvolutionExpander.h.

References llvm::SmallPtrSetImpl< PtrType >::contains().

Referenced by llvm::SCEVExpanderCleaner::cleanup().

◆ getCurrentDebugLocation()

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

Get location information used by debugging information.

Definition at line 393 of file ScalarEvolutionExpander.h.

References llvm::IRBuilderBase::getCurrentDebugLocation().

◆ getInsertedIVs()

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

Definition at line 214 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 711 of file ScalarEvolutionExpander.cpp.

References llvm::DominatorTree::dominates(), llvm::drop_begin(), llvm::Instruction::getOpcode(), llvm::User::getOperand(), and llvm::User::operands().

Referenced by hoistIVInc().

◆ getSE()

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

Definition at line 213 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 1773 of file ScalarEvolutionExpander.cpp.

References llvm::DominatorTree::dominates(), llvm::ScalarEvolution::getSCEV(), LHS, llvm::PatternMatch::m_BasicBlock(), llvm::PatternMatch::m_Br(), llvm::PatternMatch::m_ICmp(), llvm::PatternMatch::m_Instruction(), llvm::PatternMatch::match(), and RHS.

◆ 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 771 of file ScalarEvolutionExpander.cpp.

References llvm::DominatorTree::dominates(), llvm::SCEV::FlagNSW, llvm::SCEV::FlagNUW, getIVIncOperand(), llvm::ilist_detail::node_parent_access< NodeTy, ParentTy >::getParent(), llvm::ScalarEvolution::getStrengthenedNoWrapFlagsFromBinOp(), I, llvm::ScalarEvolution::maskFlags(), llvm::LoopInfo::movementPreservesLCSSAForm(), 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 243 of file ScalarEvolutionExpander.h.

References assert(), llvm::SmallVectorImpl< T >::emplace_back(), llvm::SmallVectorBase< Size_T >::empty(), llvm::SmallVectorImpl< T >::pop_back_val(), and llvm::TargetTransformInfo::TCC_Basic.

Referenced by canFoldTermCondOfLoop(), 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 400 of file ScalarEvolutionExpander.h.

References llvm::detail::DenseSetImpl< ValueT, MapTy, ValueInfoT >::count(), and I.

Referenced by 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 2344 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 2350 of file ScalarEvolutionExpander.cpp.

References llvm::ScalarEvolution::dominates(), llvm::ilist_detail::node_parent_access< NodeTy, ParentTy >::getParent(), llvm::is_contained(), isSafeToExpand(), llvm::User::operand_values(), and llvm::ScalarEvolution::properlyDominates().

Referenced by llvm::LoopConstrainer::run().

◆ replaceCongruentIVs()

unsigned SCEVExpander::replaceCongruentIVs ( Loop L,
const DominatorTree DT,
SmallVectorImpl< WeakTrackingVH > &  DeadInsts,
const TargetTransformInfo TTI = nullptr 
)

◆ setChainedPhi()

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

◆ SetCurrentDebugLocation()

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

Set location information used by debugging information.

Definition at line 388 of file ScalarEvolutionExpander.h.

References llvm::IRBuilderBase::SetCurrentDebugLocation().

◆ setInsertPoint() [1/2]

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

Definition at line 379 of file ScalarEvolutionExpander.h.

References llvm::IRBuilderBase::SetInsertPoint().

◆ 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 374 of file ScalarEvolutionExpander.h.

References assert(), and llvm::IRBuilderBase::SetInsertPoint().

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 339 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 348 of file ScalarEvolutionExpander.h.

References assert().

Friends And Related Function Documentation

◆ SCEVExpanderCleaner

friend class SCEVExpanderCleaner
friend

Definition at line 62 of file ScalarEvolutionExpander.h.

◆ SCEVVisitor< SCEVExpander, Value * >

friend struct SCEVVisitor< SCEVExpander, Value * >
friend

Definition at line 168 of file ScalarEvolutionExpander.h.


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