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 Instruction * getContextInstForUse (Use &U)
static Instruction * findCommonDominatorOfUses (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, ConstraintInfo &Info, bool IsSigned, const DataLayout &DL)
static bool canUseSExt (ConstantInt *CI)
static bool doesHoldInRange (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, 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, 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, ConstraintInfo &Info, bool IsSigned, const DataLayout &DL)
static Decomposition decomposeImpl (Value *V, ConstraintInfo &Info, bool IsSigned, const DataLayout &DL)
static bool mayLookThrough (Value *V)
 Returns true if V is an operation decomposeImpl can look through.
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 RowTy getDecompositionLinkRow (Value *V, const ConstraintTy &C, ConstraintInfo &Info, const DataLayout &DL)
 If V is a variable in the system and constraint C does not contain V, we managed to decompose V at this point, but likely not earlier when the fact involving V was added.
static std::optional< bool > checkCondition (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 (WithOverflowInst *II, SmallVectorImpl< Instruction * > &ToRemove)
 Replace the uses of II, which is known not to overflow, by the corresponding plain binary operation and a false overflow flag.
static bool tryToSimplifyOverflowMath (WithOverflowInst *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< unsigned > MaxRows ("constraint-elimination-max-rows", cl::init(500), cl::Hidden, cl::desc("Maximum number of rows to keep in constraint system"))
static cl::opt< bool > DumpReproducers ("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 55 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 1457 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 2095 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()

Decomposition decompose ( Value * V,
ConstraintInfo & Info,
bool IsSigned,
const DataLayout & DL )
static

◆ decomposeGEP()

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

Definition at line 620 of file ConstraintElimination.cpp.

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

Referenced by decomposeImpl().

◆ decomposeImpl()

◆ doesHoldInRange()

bool doesHoldInRange ( 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 522 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().

◆ dumpConstraint()

◆ dumpUnpackedICmp()

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

Definition at line 1712 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 1743 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 72 of file ConstraintElimination.cpp.

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

Referenced by checkAndReplaceCondition(), and findCommonDominatorOfUses().

◆ getDecompositionLinkRow()

RowTy getDecompositionLinkRow ( Value * V,
const ConstraintTy & C,
ConstraintInfo & Info,
const DataLayout & DL )
static

If V is a variable in the system and constraint C does not contain V, we managed to decompose V at this point, but likely not earlier when the fact involving V was added.

In that case, return a new row for V <= decompose(V) to link the variable with the decomposition result.

Definition at line 1875 of file ConstraintElimination.cpp.

References llvm::any_of(), C(), decompose(), DL, E(), llvm::SmallVectorTemplateCommon< T, typename >::empty(), and getRowForLessEqual().

Referenced by checkCondition().

◆ 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 1107 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,
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 555 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 decomposeImpl(), isKnownNoWrap(), tryToSimplifyOverflowMath(), and tryToStrengthenFlags().

◆ isKnownNoWrap() [2/2]

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

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

Definition at line 594 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 1119 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().

◆ mayLookThrough()

bool mayLookThrough ( Value * V)
static

Returns true if V is an operation decomposeImpl can look through.

Definition at line 670 of file ConstraintElimination.cpp.

References llvm::dyn_cast().

Referenced by decompose().

◆ removeEntryFromStack()

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

◆ replaceOverflowUses()

bool replaceOverflowUses ( WithOverflowInst * II,
SmallVectorImpl< Instruction * > & ToRemove )
static

Replace the uses of II, which is known not to overflow, by the corresponding plain binary operation and a false overflow flag.

Definition at line 2315 of file ConstraintElimination.cpp.

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

bool tryToSimplifyOverflowMath ( WithOverflowInst * II,
ConstraintInfo & Info,
SmallVectorImpl< Instruction * > & ToRemove )
static

Definition at line 2354 of file ConstraintElimination.cpp.

References II, isKnownNoWrap(), replaceOverflowUses(), and ToRemove.

◆ 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 1489 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 69 of file ConstraintElimination.cpp.

Referenced by canUseSExt(), decomposeImpl(), 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 70 of file ConstraintElimination.cpp.

Referenced by canUseSExt().