LLVM 24.0.0git
ConstraintElimination.cpp File Reference

Go to the source code of this file.

Macros

#define DEBUG_TYPE   "constraint-elimination"

Functions

 STATISTIC (NumCondsRemoved, "Number of instructions removed")
 DEBUG_COUNTER (EliminatedCounter, "conds-eliminated", "Controls which conditions are eliminated")
static InstructiongetContextInstForUse (Use &U)
static InstructionfindCommonDominatorOfUses (Instruction &I, DominatorTree &DT)
 Returns the closest program point dominating all uses of I.
static OffsetResult collectOffsets (GEPOperator &GEP, const DataLayout &DL)
static Decomposition decompose (Value *V, const ConstraintInfo &Info, bool IsSigned, const DataLayout &DL)
static bool canUseSExt (ConstantInt *CI)
static bool doesHoldInRange (const ConstraintInfo &Info, Value *Op, const ConstantRange &R, bool Signed)
 Returns true if Info implies that Op is in R, interpreting R as a signed range if Signed is set and as an unsigned range otherwise.
static bool isKnownNoWrap (Instruction::BinaryOps Opcode, Value *Op0, Value *Op1, unsigned NoWrapFlags, const ConstraintInfo &Info, bool Signed)
 Returns true if Opcode applied to Op0 and Op1 with NoWrapFlags is known to not wrap in signed or unsigned, depending on Signed.
static bool isKnownNoWrap (Value *V, const ConstraintInfo &Info, bool Signed)
 Returns true if V is known to not wrap in signed or unsigned, depending on Signed.
static Decomposition decomposeGEP (GEPOperator &GEP, const ConstraintInfo &Info, bool IsSigned, const DataLayout &DL)
static RowTy getRowForLessEqual (const Decomposition &ADec, const Decomposition &BDec, const DenseMap< Value *, unsigned > &Value2Index, SmallVectorImpl< Value * > &NewVariables)
 Build the row for 'ADec <= BDec', using the indices from Value2Index.
static void dumpConstraint (ArrayRef< Entry > C, const DenseMap< Value *, unsigned > &Value2Index)
static std::pair< Value *, Value * > getStartAndBackedgeValue (const PHINode &PN, const BasicBlock *LoopPred)
 Splits the induction phi PN into the start value, coming from the loop predecessor LoopPred, and the backedge value, coming from inside the loop.
template<typename PhiMatchTy>
static auto m_IncrementOf (const PhiMatchTy &PhiM, const APInt *&Off)
 Matches an increment of PhiM by a constant offset, captured in Off.
static bool getConstraintFromMemoryAccess (GetElementPtrInst &GEP, uint64_t AccessSize, CmpPredicate &Pred, Value *&A, Value *&B, const DataLayout &DL, const TargetLibraryInfo &TLI)
static bool canStrengthenFlags (Instruction *I)
 Returns true if I is a candidate whose poison-generating flags may be strengthened using the constraint systems.
static bool tryToStrengthenFlags (Instruction *I, ConstraintInfo &Info)
 Try to strengthen I's poison generating flags using Info.
static void dumpUnpackedICmp (raw_ostream &OS, ICmpInst::Predicate Pred, Value *LHS, Value *RHS)
static void generateReproducer (Instruction *Cond, bool IsSigned, Module *M, ArrayRef< ReproducerEntry > Stack, ConstraintInfo &Info, DominatorTree &DT)
 Helper function to generate a reproducer function for simplifying Cond.
static std::optional< boolcheckCondition (CmpInst::Predicate Pred, Value *A, Value *B, Instruction *CheckInst, ConstraintInfo &Info)
static bool checkAndReplaceCondition (CmpPredicate Pred, Value *A, Value *B, Instruction *CheckInst, ConstraintInfo &Info, unsigned NumIn, unsigned NumOut, Instruction *ContextInst, Module *ReproducerModule, ArrayRef< ReproducerEntry > ReproducerCondStack, DominatorTree &DT, SmallVectorImpl< Instruction * > &ToRemove)
static bool checkAndReplaceMinMax (MinMaxIntrinsic *MinMax, ConstraintInfo &Info, SmallVectorImpl< Instruction * > &ToRemove)
static bool checkAndReplaceCmp (CmpIntrinsic *I, ConstraintInfo &Info, SmallVectorImpl< Instruction * > &ToRemove)
static bool checkAndReplaceUSubSat (SaturatingInst *USub, ConstraintInfo &Info, SmallVectorImpl< Instruction * > &ToRemove)
 Try to replace USub by a plain subtract, if Info proves it cannot saturate.
static void removeEntryFromStack (const StackEntry &E, ConstraintInfo &Info, Module *ReproducerModule, SmallVectorImpl< ReproducerEntry > &ReproducerCondStack, SmallVectorImpl< StackEntry > &DFSInStack)
static bool checkOrAndOpImpliedByOther (FactOrCheck &CB, ConstraintInfo &Info, Module *ReproducerModule, SmallVectorImpl< ReproducerEntry > &ReproducerCondStack, SmallVectorImpl< StackEntry > &DFSInStack, SmallVectorImpl< Instruction * > &ToRemove)
 Check if either the first condition of an AND or OR is implied by the (negated in case of OR) second condition or vice versa.
static bool replaceOverflowUses (IntrinsicInst *II, Instruction::BinaryOps Opcode, Value *A, Value *B, SmallVectorImpl< Instruction * > &ToRemove)
 Replace the uses of the overflow intrinsic II, which has been proven not to signed-overflow, by (Opcode A, B).
static bool tryToSimplifyOverflowMath (IntrinsicInst *II, ConstraintInfo &Info, SmallVectorImpl< Instruction * > &ToRemove)
static bool eliminateConstraints (Function &F, DominatorTree &DT, LoopInfo &LI, ScalarEvolution &SE, OptimizationRemarkEmitter &ORE, TargetLibraryInfo &TLI)

Variables

static cl::opt< unsignedMaxRows ("constraint-elimination-max-rows", cl::init(500), cl::Hidden, cl::desc("Maximum number of rows to keep in constraint system"))
static cl::opt< boolDumpReproducers ("constraint-elimination-dump-reproducers", cl::init(false), cl::Hidden, cl::desc("Dump IR to reproduce successful transformations."))
static int64_t MaxConstraintValue = std::numeric_limits<int64_t>::max()
static int64_t MinSignedConstraintValue = std::numeric_limits<int64_t>::min()

Macro Definition Documentation

◆ DEBUG_TYPE

#define DEBUG_TYPE   "constraint-elimination"

Definition at line 54 of file ConstraintElimination.cpp.

Function Documentation

◆ canStrengthenFlags()

bool canStrengthenFlags ( Instruction * I)
static

Returns true if I is a candidate whose poison-generating flags may be strengthened using the constraint systems.

Definition at line 1389 of file ConstraintElimination.cpp.

References llvm::dyn_cast(), I, and llvm::isa().

Referenced by tryToStrengthenFlags().

◆ canUseSExt()

◆ checkAndReplaceCmp()

bool checkAndReplaceCmp ( CmpIntrinsic * I,
ConstraintInfo & Info,
SmallVectorImpl< Instruction * > & ToRemove )
static

◆ checkAndReplaceCondition()

◆ checkAndReplaceMinMax()

bool checkAndReplaceMinMax ( MinMaxIntrinsic * MinMax,
ConstraintInfo & Info,
SmallVectorImpl< Instruction * > & ToRemove )
static

◆ checkAndReplaceUSubSat()

bool checkAndReplaceUSubSat ( SaturatingInst * USub,
ConstraintInfo & Info,
SmallVectorImpl< Instruction * > & ToRemove )
static

Try to replace USub by a plain subtract, if Info proves it cannot saturate.

Returns true if USub was replaced.

Definition at line 1966 of file ConstraintElimination.cpp.

References A(), B(), checkCondition(), llvm::BinaryOpIntrinsic::getLHS(), llvm::BinaryOpIntrinsic::getRHS(), llvm::CmpInst::ICMP_UGE, llvm::Value::replaceAllUsesWith(), llvm::Sub, and ToRemove.

◆ checkCondition()

◆ checkOrAndOpImpliedByOther()

◆ collectOffsets()

◆ DEBUG_COUNTER()

DEBUG_COUNTER ( EliminatedCounter ,
"conds-eliminated" ,
"Controls which conditions are eliminated"  )

◆ decompose()

◆ decomposeGEP()

Decomposition decomposeGEP ( GEPOperator & GEP,
const ConstraintInfo & Info,
bool IsSigned,
const DataLayout & DL )
static

Definition at line 598 of file ConstraintElimination.cpp.

References assert(), collectOffsets(), decompose(), DL, GEP, and llvm::GEPNoWrapFlags::none().

Referenced by decompose().

◆ doesHoldInRange()

bool doesHoldInRange ( const ConstraintInfo & Info,
Value * Op,
const ConstantRange & R,
bool Signed )
static

Returns true if Info implies that Op is in R, interpreting R as a signed range if Signed is set and as an unsigned range otherwise.

Definition at line 504 of file ConstraintElimination.cpp.

References llvm::BitWidth, llvm::APInt::getMaxValue(), llvm::APInt::getMinValue(), llvm::APInt::getSignedMaxValue(), llvm::APInt::getSignedMinValue(), llvm::CmpInst::ICMP_SGE, llvm::CmpInst::ICMP_SLE, llvm::CmpInst::ICMP_UGE, llvm::CmpInst::ICMP_ULE, MaxConstraintValue, and Signed.

Referenced by isKnownNoWrap(), and tryToSimplifyOverflowMath().

◆ dumpConstraint()

◆ dumpUnpackedICmp()

void dumpUnpackedICmp ( raw_ostream & OS,
ICmpInst::Predicate Pred,
Value * LHS,
Value * RHS )
static

Definition at line 1637 of file ConstraintElimination.cpp.

References LHS, and RHS.

Referenced by checkCondition(), and generateReproducer().

◆ eliminateConstraints()

◆ findCommonDominatorOfUses()

◆ generateReproducer()

void generateReproducer ( Instruction * Cond,
bool IsSigned,
Module * M,
ArrayRef< ReproducerEntry > Stack,
ConstraintInfo & Info,
DominatorTree & DT )
static

Helper function to generate a reproducer function for simplifying Cond.

The reproducer function contains a series of @llvm.assume calls, one for each condition in Stack. For each condition, the operand instruction are cloned until we reach operands that have an entry in Value2Index. Those will then be added as function arguments. DT is used to order cloned instructions. The reproducer function will get added to M, if it is non-null. Otherwise no reproducer function is generated.

Definition at line 1668 of file ConstraintElimination.cpp.

References A(), AbstractManglingParser< Derived, Alloc >::Ops, llvm::append_range(), assert(), B(), llvm::CmpInst::BAD_ICMP_PREDICATE, Cond, llvm::BasicBlock::Create(), llvm::Function::Create(), llvm::dbgs(), llvm::DominatorTree::dominates(), llvm::Instruction::dropUnknownNonDebugMetadata(), dumpUnpackedICmp(), llvm::dyn_cast(), llvm::SmallVectorTemplateCommon< T, typename >::empty(), llvm::ValueMap< KeyT, ValueT, Config >::end(), llvm::GlobalValue::ExternalLinkage, F, llvm::ValueMap< KeyT, ValueT, Config >::find(), llvm::FunctionType::get(), getName(), I, llvm::SmallPtrSetImpl< PtrType >::insert(), llvm::Instruction::insertBefore(), llvm::isa(), llvm::CmpInst::isSigned(), LLVM_DEBUG, P, llvm::SmallVectorImpl< T >::pop_back_val(), llvm::SmallVectorTemplateBase< T, bool >::push_back(), llvm::remapInstructionsInBlocks(), llvm::Instruction::setDebugLoc(), llvm::sort(), and llvm::verifyFunction().

Referenced by checkAndReplaceCondition().

◆ getConstraintFromMemoryAccess()

◆ getContextInstForUse()

Instruction * getContextInstForUse ( Use & U)
static

Definition at line 71 of file ConstraintElimination.cpp.

References llvm::cast(), and llvm::dyn_cast().

Referenced by checkAndReplaceCondition(), and findCommonDominatorOfUses().

◆ getRowForLessEqual()

RowTy getRowForLessEqual ( const Decomposition & ADec,
const Decomposition & BDec,
const DenseMap< Value *, unsigned > & Value2Index,
SmallVectorImpl< Value * > & NewVariables )
static

◆ getStartAndBackedgeValue()

std::pair< Value *, Value * > getStartAndBackedgeValue ( const PHINode & PN,
const BasicBlock * LoopPred )
static

Splits the induction phi PN into the start value, coming from the loop predecessor LoopPred, and the backedge value, coming from inside the loop.

Returns {nullptr, nullptr} if PN has other incoming values.

Definition at line 1039 of file ConstraintElimination.cpp.

References assert(), llvm::PHINode::getBasicBlockIndex(), llvm::PHINode::getIncomingBlock(), llvm::PHINode::getIncomingValue(), and llvm::PHINode::getNumIncomingValues().

◆ isKnownNoWrap() [1/2]

bool isKnownNoWrap ( Instruction::BinaryOps Opcode,
Value * Op0,
Value * Op1,
unsigned NoWrapFlags,
const ConstraintInfo & Info,
bool Signed )
static

Returns true if Opcode applied to Op0 and Op1 with NoWrapFlags is known to not wrap in signed or unsigned, depending on Signed.

Definition at line 537 of file ConstraintElimination.cpp.

References C(), doesHoldInRange(), llvm::dyn_cast(), llvm::CmpInst::ICMP_SGE, llvm::CmpInst::ICMP_UGE, llvm::ConstantRange::makeExactNoWrapRegion(), and Signed.

Referenced by decompose(), isKnownNoWrap(), and tryToStrengthenFlags().

◆ isKnownNoWrap() [2/2]

bool isKnownNoWrap ( Value * V,
const ConstraintInfo & Info,
bool Signed )
static

Returns true if V is known to not wrap in signed or unsigned, depending on Signed.

Definition at line 576 of file ConstraintElimination.cpp.

References llvm::dyn_cast(), isKnownNoWrap(), llvm::PatternMatch::m_DisjointOr(), llvm::PatternMatch::m_Value(), llvm::SCEVPatternMatch::match(), and Signed.

◆ m_IncrementOf()

template<typename PhiMatchTy>
auto m_IncrementOf ( const PhiMatchTy & PhiM,
const APInt *& Off )
static

Matches an increment of PhiM by a constant offset, captured in Off.

The increment must be a plain IR add or [u|s]add.with.overflow.

Definition at line 1051 of file ConstraintElimination.cpp.

References llvm::PatternMatch::m_APInt(), llvm::PatternMatch::m_c_Add(), llvm::PatternMatch::m_c_Intrinsic(), llvm::PatternMatchHelpers::m_CombineOr(), and llvm::PatternMatch::m_ExtractValue().

◆ removeEntryFromStack()

void removeEntryFromStack ( const StackEntry & E,
ConstraintInfo & Info,
Module * ReproducerModule,
SmallVectorImpl< ReproducerEntry > & ReproducerCondStack,
SmallVectorImpl< StackEntry > & DFSInStack )
static

◆ replaceOverflowUses()

bool replaceOverflowUses ( IntrinsicInst * II,
Instruction::BinaryOps Opcode,
Value * A,
Value * B,
SmallVectorImpl< Instruction * > & ToRemove )
static

Replace the uses of the overflow intrinsic II, which has been proven not to signed-overflow, by (Opcode A, B).

Definition at line 2183 of file ConstraintElimination.cpp.

References A(), B(), llvm::cast(), Changed, llvm::PoisonValue::get(), I, II, llvm::PatternMatch::m_ExtractValue(), llvm::PatternMatch::m_Value(), llvm::make_early_inc_range(), llvm::SCEVPatternMatch::match(), llvm::Value::replaceAllUsesWith(), and ToRemove.

Referenced by tryToSimplifyOverflowMath().

◆ STATISTIC()

STATISTIC ( NumCondsRemoved ,
"Number of instructions removed"  )

◆ tryToSimplifyOverflowMath()

◆ tryToStrengthenFlags()

bool tryToStrengthenFlags ( Instruction * I,
ConstraintInfo & Info )
static

Try to strengthen I's poison generating flags using Info.

Returns true if I was modified.

Definition at line 1421 of file ConstraintElimination.cpp.

References assert(), canStrengthenFlags(), Changed, llvm::dbgs(), I, isKnownNoWrap(), and LLVM_DEBUG.

Variable Documentation

◆ DumpReproducers

cl::opt< bool > DumpReproducers("constraint-elimination-dump-reproducers", cl::init(false), cl::Hidden, cl::desc("Dump IR to reproduce successful transformations.")) ( "constraint-elimination-dump-reproducers" ,
cl::init(false) ,
cl::Hidden ,
cl::desc("Dump IR to reproduce successful transformations.")  )
static

Referenced by eliminateConstraints().

◆ MaxConstraintValue

int64_t MaxConstraintValue = std::numeric_limits<int64_t>::max()
static

Definition at line 68 of file ConstraintElimination.cpp.

Referenced by canUseSExt(), decompose(), and doesHoldInRange().

◆ MaxRows

cl::opt< unsigned > MaxRows("constraint-elimination-max-rows", cl::init(500), cl::Hidden, cl::desc("Maximum number of rows to keep in constraint system")) ( "constraint-elimination-max-rows" ,
cl::init(500) ,
cl::Hidden ,
cl::desc("Maximum number of rows to keep in constraint system")  )
static

◆ MinSignedConstraintValue

int64_t MinSignedConstraintValue = std::numeric_limits<int64_t>::min()
static

Definition at line 69 of file ConstraintElimination.cpp.

Referenced by canUseSExt().