LLVM  13.0.0git
Namespaces | Macros | Enumerations | Functions
InstructionSimplify.cpp File Reference
#include "llvm/Analysis/InstructionSimplify.h"
#include "llvm/ADT/SetVector.h"
#include "llvm/ADT/SmallPtrSet.h"
#include "llvm/ADT/Statistic.h"
#include "llvm/Analysis/AliasAnalysis.h"
#include "llvm/Analysis/AssumptionCache.h"
#include "llvm/Analysis/CaptureTracking.h"
#include "llvm/Analysis/CmpInstAnalysis.h"
#include "llvm/Analysis/ConstantFolding.h"
#include "llvm/Analysis/LoopAnalysisManager.h"
#include "llvm/Analysis/MemoryBuiltins.h"
#include "llvm/Analysis/OverflowInstAnalysis.h"
#include "llvm/Analysis/ValueTracking.h"
#include "llvm/Analysis/VectorUtils.h"
#include "llvm/IR/ConstantRange.h"
#include "llvm/IR/DataLayout.h"
#include "llvm/IR/Dominators.h"
#include "llvm/IR/GetElementPtrTypeIterator.h"
#include "llvm/IR/GlobalAlias.h"
#include "llvm/IR/InstrTypes.h"
#include "llvm/IR/Instructions.h"
#include "llvm/IR/Operator.h"
#include "llvm/IR/PatternMatch.h"
#include "llvm/IR/ValueHandle.h"
#include "llvm/Support/KnownBits.h"
#include <algorithm>
#include "llvm/IR/Instruction.def"
Include dependency graph for InstructionSimplify.cpp:

Go to the source code of this file.

Namespaces

 llvm
 ---------------------— PointerInfo ------------------------------------—
 

Macros

#define DEBUG_TYPE   "instsimplify"
 
#define HANDLE_CAST_INST(num, opc, clas)   case Instruction::opc:
 

Enumerations

enum  { RecursionLimit = 3 }
 

Functions

 STATISTIC (NumExpand, "Number of expansions")
 
 STATISTIC (NumReassoc, "Number of reassociations")
 
static ValueSimplifyAndInst (Value *Op0, Value *Op1, const SimplifyQuery &Q, unsigned MaxRecurse)
 Given operands for an And, see if we can fold the result. More...
 
static ValuesimplifyUnOp (unsigned Opcode, Value *Op, const SimplifyQuery &Q, unsigned MaxRecurse)
 Given the operand for a UnaryOperator, see if we can fold the result. More...
 
static ValuesimplifyFPUnOp (unsigned Opcode, Value *Op, const FastMathFlags &FMF, const SimplifyQuery &Q, unsigned MaxRecurse)
 Given the operand for a UnaryOperator, see if we can fold the result. More...
 
static ValueSimplifyBinOp (unsigned Opcode, Value *LHS, Value *RHS, const SimplifyQuery &Q, unsigned MaxRecurse)
 Given operands for a BinaryOperator, see if we can fold the result. More...
 
static ValueSimplifyBinOp (unsigned Opcode, Value *LHS, Value *RHS, const FastMathFlags &FMF, const SimplifyQuery &Q, unsigned MaxRecurse)
 Given operands for a BinaryOperator, see if we can fold the result. More...
 
static ValueSimplifyCmpInst (unsigned Predicate, Value *LHS, Value *RHS, const SimplifyQuery &Q, unsigned MaxRecurse)
 Given operands for a CmpInst, see if we can fold the result. More...
 
static ValueSimplifyICmpInst (unsigned Predicate, Value *LHS, Value *RHS, const SimplifyQuery &Q, unsigned MaxRecurse)
 Given operands for an ICmpInst, see if we can fold the result. More...
 
static ValueSimplifyOrInst (Value *Op0, Value *Op1, const SimplifyQuery &Q, unsigned MaxRecurse)
 Given operands for an Or, see if we can fold the result. More...
 
static ValueSimplifyXorInst (Value *Op0, Value *Op1, const SimplifyQuery &Q, unsigned MaxRecurse)
 Given operands for a Xor, see if we can fold the result. More...
 
static ValueSimplifyCastInst (unsigned, Value *, Type *, const SimplifyQuery &, unsigned)
 
static ValueSimplifyGEPInst (Type *SrcTy, ArrayRef< Value * > Ops, const SimplifyQuery &Q, unsigned)
 Given operands for an GetElementPtrInst, see if we can fold the result. More...
 
static ValueSimplifySelectInst (Value *Cond, Value *TrueVal, Value *FalseVal, const SimplifyQuery &Q, unsigned MaxRecurse)
 Given operands for a SelectInst, see if we can fold the result. More...
 
static ValuefoldSelectWithBinaryOp (Value *Cond, Value *TrueVal, Value *FalseVal)
 
static ConstantgetFalse (Type *Ty)
 For a boolean type or a vector of boolean type, return false or a vector with every element false. More...
 
static ConstantgetTrue (Type *Ty)
 For a boolean type or a vector of boolean type, return true or a vector with every element true. More...
 
static bool isSameCompare (Value *V, CmpInst::Predicate Pred, Value *LHS, Value *RHS)
 isSameCompare - Is V equivalent to the comparison "LHS Pred RHS"? More...
 
static ValuesimplifyCmpSelCase (CmpInst::Predicate Pred, Value *LHS, Value *RHS, Value *Cond, const SimplifyQuery &Q, unsigned MaxRecurse, Constant *TrueOrFalse)
 Simplify comparison with true or false branch of select: sel = select i1 cond, i32 tv, i32 fv cmp = icmp sle i32 sel, rhs Compose new comparison by substituting sel with either tv or fv and see if it simplifies. More...
 
static ValuesimplifyCmpSelTrueCase (CmpInst::Predicate Pred, Value *LHS, Value *RHS, Value *Cond, const SimplifyQuery &Q, unsigned MaxRecurse)
 Simplify comparison with true branch of select. More...
 
static ValuesimplifyCmpSelFalseCase (CmpInst::Predicate Pred, Value *LHS, Value *RHS, Value *Cond, const SimplifyQuery &Q, unsigned MaxRecurse)
 Simplify comparison with false branch of select. More...
 
static ValuehandleOtherCmpSelSimplifications (Value *TCmp, Value *FCmp, Value *Cond, const SimplifyQuery &Q, unsigned MaxRecurse)
 We know comparison with both branches of select can be simplified, but they are not equal. More...
 
static bool valueDominatesPHI (Value *V, PHINode *P, const DominatorTree *DT)
 Does the given value dominate the specified phi node? More...
 
static ValueexpandBinOp (Instruction::BinaryOps Opcode, Value *V, Value *OtherOp, Instruction::BinaryOps OpcodeToExpand, const SimplifyQuery &Q, unsigned MaxRecurse)
 Try to simplify a binary operator of form "V op OtherOp" where V is "(B0 opex B1)" by distributing 'op' across 'opex' as "(B0 op OtherOp) opex (B1 op OtherOp)". More...
 
static ValueexpandCommutativeBinOp (Instruction::BinaryOps Opcode, Value *L, Value *R, Instruction::BinaryOps OpcodeToExpand, const SimplifyQuery &Q, unsigned MaxRecurse)
 Try to simplify binops of form "A op (B op' C)" or the commuted variant by distributing op over op'. More...
 
static ValueSimplifyAssociativeBinOp (Instruction::BinaryOps Opcode, Value *LHS, Value *RHS, const SimplifyQuery &Q, unsigned MaxRecurse)
 Generic simplifications for associative binary operations. More...
 
static ValueThreadBinOpOverSelect (Instruction::BinaryOps Opcode, Value *LHS, Value *RHS, const SimplifyQuery &Q, unsigned MaxRecurse)
 In the case of a binary operation with a select instruction as an operand, try to simplify the binop by seeing whether evaluating it on both branches of the select results in the same value. More...
 
static ValueThreadCmpOverSelect (CmpInst::Predicate Pred, Value *LHS, Value *RHS, const SimplifyQuery &Q, unsigned MaxRecurse)
 In the case of a comparison with a select instruction, try to simplify the comparison by seeing whether both branches of the select result in the same value. More...
 
static ValueThreadBinOpOverPHI (Instruction::BinaryOps Opcode, Value *LHS, Value *RHS, const SimplifyQuery &Q, unsigned MaxRecurse)
 In the case of a binary operation with an operand that is a PHI instruction, try to simplify the binop by seeing whether evaluating it on the incoming phi values yields the same result for every value. More...
 
static ValueThreadCmpOverPHI (CmpInst::Predicate Pred, Value *LHS, Value *RHS, const SimplifyQuery &Q, unsigned MaxRecurse)
 In the case of a comparison with a PHI instruction, try to simplify the comparison by seeing whether comparing with all of the incoming phi values yields the same result every time. More...
 
static ConstantfoldOrCommuteConstant (Instruction::BinaryOps Opcode, Value *&Op0, Value *&Op1, const SimplifyQuery &Q)
 
static ValueSimplifyAddInst (Value *Op0, Value *Op1, bool IsNSW, bool IsNUW, const SimplifyQuery &Q, unsigned MaxRecurse)
 Given operands for an Add, see if we can fold the result. More...
 
static ConstantstripAndComputeConstantOffsets (const DataLayout &DL, Value *&V, bool AllowNonInbounds=false)
 Compute the base pointer and cumulative constant offsets for V. More...
 
static ConstantcomputePointerDifference (const DataLayout &DL, Value *LHS, Value *RHS)
 Compute the constant difference between two pointer values. More...
 
static ValueSimplifySubInst (Value *Op0, Value *Op1, bool isNSW, bool isNUW, const SimplifyQuery &Q, unsigned MaxRecurse)
 Given operands for a Sub, see if we can fold the result. More...
 
static ValueSimplifyMulInst (Value *Op0, Value *Op1, const SimplifyQuery &Q, unsigned MaxRecurse)
 Given operands for a Mul, see if we can fold the result. More...
 
static ValuesimplifyDivRem (Instruction::BinaryOps Opcode, Value *Op0, Value *Op1, const SimplifyQuery &Q)
 Check for common or similar folds of integer division or integer remainder. More...
 
static bool isICmpTrue (ICmpInst::Predicate Pred, Value *LHS, Value *RHS, const SimplifyQuery &Q, unsigned MaxRecurse)
 Given a predicate and two operands, return true if the comparison is true. More...
 
static bool isDivZero (Value *X, Value *Y, const SimplifyQuery &Q, unsigned MaxRecurse, bool IsSigned)
 Return true if we can simplify X / Y to 0. More...
 
static ValuesimplifyDiv (Instruction::BinaryOps Opcode, Value *Op0, Value *Op1, const SimplifyQuery &Q, unsigned MaxRecurse)
 These are simplifications common to SDiv and UDiv. More...
 
static ValuesimplifyRem (Instruction::BinaryOps Opcode, Value *Op0, Value *Op1, const SimplifyQuery &Q, unsigned MaxRecurse)
 These are simplifications common to SRem and URem. More...
 
static ValueSimplifySDivInst (Value *Op0, Value *Op1, const SimplifyQuery &Q, unsigned MaxRecurse)
 Given operands for an SDiv, see if we can fold the result. More...
 
static ValueSimplifyUDivInst (Value *Op0, Value *Op1, const SimplifyQuery &Q, unsigned MaxRecurse)
 Given operands for a UDiv, see if we can fold the result. More...
 
static ValueSimplifySRemInst (Value *Op0, Value *Op1, const SimplifyQuery &Q, unsigned MaxRecurse)
 Given operands for an SRem, see if we can fold the result. More...
 
static ValueSimplifyURemInst (Value *Op0, Value *Op1, const SimplifyQuery &Q, unsigned MaxRecurse)
 Given operands for a URem, see if we can fold the result. More...
 
static bool isPoisonShift (Value *Amount, const SimplifyQuery &Q)
 Returns true if a shift by Amount always yields poison. More...
 
static ValueSimplifyShift (Instruction::BinaryOps Opcode, Value *Op0, Value *Op1, bool IsNSW, const SimplifyQuery &Q, unsigned MaxRecurse)
 Given operands for an Shl, LShr or AShr, see if we can fold the result. More...
 
static ValueSimplifyRightShift (Instruction::BinaryOps Opcode, Value *Op0, Value *Op1, bool isExact, const SimplifyQuery &Q, unsigned MaxRecurse)
 Given operands for an Shl, LShr or AShr, see if we can fold the result. More...
 
static ValueSimplifyShlInst (Value *Op0, Value *Op1, bool isNSW, bool isNUW, const SimplifyQuery &Q, unsigned MaxRecurse)
 Given operands for an Shl, see if we can fold the result. More...
 
static ValueSimplifyLShrInst (Value *Op0, Value *Op1, bool isExact, const SimplifyQuery &Q, unsigned MaxRecurse)
 Given operands for an LShr, see if we can fold the result. More...
 
static ValueSimplifyAShrInst (Value *Op0, Value *Op1, bool isExact, const SimplifyQuery &Q, unsigned MaxRecurse)
 Given operands for an AShr, see if we can fold the result. More...
 
static ValuesimplifyUnsignedRangeCheck (ICmpInst *ZeroICmp, ICmpInst *UnsignedICmp, bool IsAnd, const SimplifyQuery &Q)
 Commuted variants are assumed to be handled by calling this function again with the parameters swapped. More...
 
static ValuesimplifyAndOfICmpsWithSameOperands (ICmpInst *Op0, ICmpInst *Op1)
 Commuted variants are assumed to be handled by calling this function again with the parameters swapped. More...
 
static ValuesimplifyOrOfICmpsWithSameOperands (ICmpInst *Op0, ICmpInst *Op1)
 Commuted variants are assumed to be handled by calling this function again with the parameters swapped. More...
 
static ValuesimplifyAndOrOfICmpsWithConstants (ICmpInst *Cmp0, ICmpInst *Cmp1, bool IsAnd)
 Test if a pair of compares with a shared operand and 2 constants has an empty set intersection, full set union, or if one compare is a superset of the other. More...
 
static ValuesimplifyAndOrOfICmpsWithZero (ICmpInst *Cmp0, ICmpInst *Cmp1, bool IsAnd)
 
static ValuesimplifyAndOfICmpsWithAdd (ICmpInst *Op0, ICmpInst *Op1, const InstrInfoQuery &IIQ)
 
static ValuesimplifyAndOrOfICmpsWithLimitConst (ICmpInst *Cmp0, ICmpInst *Cmp1, bool IsAnd)
 Try to eliminate compares with signed or unsigned min/max constants. More...
 
static ValuesimplifyAndOfICmps (ICmpInst *Op0, ICmpInst *Op1, const SimplifyQuery &Q)
 
static ValuesimplifyOrOfICmpsWithAdd (ICmpInst *Op0, ICmpInst *Op1, const InstrInfoQuery &IIQ)
 
static ValuesimplifyOrOfICmps (ICmpInst *Op0, ICmpInst *Op1, const SimplifyQuery &Q)
 
static ValuesimplifyAndOrOfFCmps (const TargetLibraryInfo *TLI, FCmpInst *LHS, FCmpInst *RHS, bool IsAnd)
 
static ValuesimplifyAndOrOfCmps (const SimplifyQuery &Q, Value *Op0, Value *Op1, bool IsAnd)
 
static ValuesimplifyLogicOfAddSub (Value *Op0, Value *Op1, Instruction::BinaryOps Opcode)
 Given a bitwise logic op, check if the operands are add/sub with a common source value and inverted constant (identity: C - X -> ~(X + ~C)). More...
 
static TypeGetCompareTy (Value *Op)
 
static ValueExtractEquivalentCondition (Value *V, CmpInst::Predicate Pred, Value *LHS, Value *RHS)
 Rummage around inside V looking for something equivalent to the comparison "LHS Pred RHS". More...
 
static ConstantcomputePointerICmp (CmpInst::Predicate Pred, Value *LHS, Value *RHS, const SimplifyQuery &Q)
 
static ValuesimplifyICmpOfBools (CmpInst::Predicate Pred, Value *LHS, Value *RHS, const SimplifyQuery &Q)
 Fold an icmp when its operands have i1 scalar type. More...
 
static ValuesimplifyICmpWithZero (CmpInst::Predicate Pred, Value *LHS, Value *RHS, const SimplifyQuery &Q)
 Try hard to fold icmp with zero RHS because this is a common case. More...
 
static ValuesimplifyICmpWithConstant (CmpInst::Predicate Pred, Value *LHS, Value *RHS, const InstrInfoQuery &IIQ)
 
static ValuesimplifyICmpWithBinOpOnLHS (CmpInst::Predicate Pred, BinaryOperator *LBO, Value *RHS, const SimplifyQuery &Q, unsigned MaxRecurse)
 
static bool trySimplifyICmpWithAdds (CmpInst::Predicate Pred, Value *LHS, Value *RHS)
 
static ValuesimplifyICmpWithBinOp (CmpInst::Predicate Pred, Value *LHS, Value *RHS, const SimplifyQuery &Q, unsigned MaxRecurse)
 TODO: A large part of this logic is duplicated in InstCombine's foldICmpBinOp(). More...
 
static ValuesimplifyICmpWithMinMax (CmpInst::Predicate Pred, Value *LHS, Value *RHS, const SimplifyQuery &Q, unsigned MaxRecurse)
 Simplify integer comparisons where at least one operand of the compare matches an integer min/max idiom. More...
 
static ValuesimplifyICmpWithDominatingAssume (CmpInst::Predicate Predicate, Value *LHS, Value *RHS, const SimplifyQuery &Q)
 
static ValueSimplifyFCmpInst (unsigned Predicate, Value *LHS, Value *RHS, FastMathFlags FMF, const SimplifyQuery &Q, unsigned MaxRecurse)
 Given operands for an FCmpInst, see if we can fold the result. More...
 
static ValuesimplifyWithOpReplaced (Value *V, Value *Op, Value *RepOp, const SimplifyQuery &Q, bool AllowRefinement, unsigned MaxRecurse)
 
static ValuesimplifySelectBitTest (Value *TrueVal, Value *FalseVal, Value *X, const APInt *Y, bool TrueWhenUnset)
 Try to simplify a select instruction when its condition operand is an integer comparison where one operand of the compare is a constant. More...
 
static ValuesimplifySelectWithFakeICmpEq (Value *CmpLHS, Value *CmpRHS, ICmpInst::Predicate Pred, Value *TrueVal, Value *FalseVal)
 An alternative way to test if a bit is set or not uses sgt/slt instead of eq/ne. More...
 
static ValuesimplifySelectWithICmpCond (Value *CondVal, Value *TrueVal, Value *FalseVal, const SimplifyQuery &Q, unsigned MaxRecurse)
 Try to simplify a select instruction when its condition operand is an integer comparison. More...
 
static ValuesimplifySelectWithFCmp (Value *Cond, Value *T, Value *F, const SimplifyQuery &Q)
 Try to simplify a select instruction when its condition operand is a floating-point comparison. More...
 
static ValueSimplifyInsertValueInst (Value *Agg, Value *Val, ArrayRef< unsigned > Idxs, const SimplifyQuery &Q, unsigned)
 Given operands for an InsertValueInst, see if we can fold the result. More...
 
static ValueSimplifyExtractValueInst (Value *Agg, ArrayRef< unsigned > Idxs, const SimplifyQuery &, unsigned)
 Given operands for an ExtractValueInst, see if we can fold the result. More...
 
static ValueSimplifyExtractElementInst (Value *Vec, Value *Idx, const SimplifyQuery &Q, unsigned)
 Given operands for an ExtractElementInst, see if we can fold the result. More...
 
static ValueSimplifyPHINode (PHINode *PN, const SimplifyQuery &Q)
 See if we can fold the given phi. If not, returns null. More...
 
static ValuefoldIdentityShuffles (int DestElt, Value *Op0, Value *Op1, int MaskVal, Value *RootVec, unsigned MaxRecurse)
 For the given destination element of a shuffle, peek through shuffles to match a root vector source operand that contains that element in the same vector lane (ie, the same mask index), so we can eliminate the shuffle(s). More...
 
static ValueSimplifyShuffleVectorInst (Value *Op0, Value *Op1, ArrayRef< int > Mask, Type *RetTy, const SimplifyQuery &Q, unsigned MaxRecurse)
 
static ConstantfoldConstant (Instruction::UnaryOps Opcode, Value *&Op, const SimplifyQuery &Q)
 
static ValuesimplifyFNegInst (Value *Op, FastMathFlags FMF, const SimplifyQuery &Q, unsigned MaxRecurse)
 Given the operand for an FNeg, see if we can fold the result. More...
 
static ConstantpropagateNaN (Constant *In)
 
static ConstantsimplifyFPOp (ArrayRef< Value * > Ops, FastMathFlags FMF, const SimplifyQuery &Q, fp::ExceptionBehavior ExBehavior, RoundingMode Rounding)
 Perform folds that are common to any floating-point operation. More...
 
static ValueSimplifyFAddInst (Value *Op0, Value *Op1, FastMathFlags FMF, const SimplifyQuery &Q, unsigned MaxRecurse, fp::ExceptionBehavior ExBehavior=fp::ebIgnore, RoundingMode Rounding=RoundingMode::NearestTiesToEven)
 Given operands for an FAdd, see if we can fold the result. More...
 
static ValueSimplifyFSubInst (Value *Op0, Value *Op1, FastMathFlags FMF, const SimplifyQuery &Q, unsigned MaxRecurse, fp::ExceptionBehavior ExBehavior=fp::ebIgnore, RoundingMode Rounding=RoundingMode::NearestTiesToEven)
 Given operands for an FSub, see if we can fold the result. More...
 
static ValueSimplifyFMAFMul (Value *Op0, Value *Op1, FastMathFlags FMF, const SimplifyQuery &Q, unsigned MaxRecurse, fp::ExceptionBehavior ExBehavior, RoundingMode Rounding)
 
static ValueSimplifyFMulInst (Value *Op0, Value *Op1, FastMathFlags FMF, const SimplifyQuery &Q, unsigned MaxRecurse, fp::ExceptionBehavior ExBehavior=fp::ebIgnore, RoundingMode Rounding=RoundingMode::NearestTiesToEven)
 Given the operands for an FMul, see if we can fold the result. More...
 
static ValueSimplifyFDivInst (Value *Op0, Value *Op1, FastMathFlags FMF, const SimplifyQuery &Q, unsigned, fp::ExceptionBehavior ExBehavior=fp::ebIgnore, RoundingMode Rounding=RoundingMode::NearestTiesToEven)
 
static ValueSimplifyFRemInst (Value *Op0, Value *Op1, FastMathFlags FMF, const SimplifyQuery &Q, unsigned, fp::ExceptionBehavior ExBehavior=fp::ebIgnore, RoundingMode Rounding=RoundingMode::NearestTiesToEven)
 
static bool IsIdempotent (Intrinsic::ID ID)
 
static ValueSimplifyRelativeLoad (Constant *Ptr, Constant *Offset, const DataLayout &DL)
 
static ValuesimplifyUnaryIntrinsic (Function *F, Value *Op0, const SimplifyQuery &Q)
 
static APInt getMaxMinLimit (Intrinsic::ID IID, unsigned BitWidth)
 
static ICmpInst::Predicate getMaxMinPredicate (Intrinsic::ID IID)
 
static ValuefoldMinMaxSharedOp (Intrinsic::ID IID, Value *Op0, Value *Op1)
 Given a min/max intrinsic, see if it can be removed based on having an operand that is another min/max intrinsic with shared operand(s). More...
 
static ValuesimplifyBinaryIntrinsic (Function *F, Value *Op0, Value *Op1, const SimplifyQuery &Q)
 
static ValuesimplifyIntrinsic (CallBase *Call, const SimplifyQuery &Q)
 
static ValuetryConstantFoldCall (CallBase *Call, const SimplifyQuery &Q)
 
static ValueSimplifyFreezeInst (Value *Op0, const SimplifyQuery &Q)
 Given operands for a Freeze, see if we can fold the result. More...
 
static ConstantConstructLoadOperandConstant (Value *Op)
 
static ValueSimplifyLoadInst (LoadInst *LI, const SimplifyQuery &Q)
 
static bool replaceAndRecursivelySimplifyImpl (Instruction *I, Value *SimpleV, const TargetLibraryInfo *TLI, const DominatorTree *DT, AssumptionCache *AC, SmallSetVector< Instruction *, 8 > *UnsimplifiedUsers=nullptr)
 Implementation of recursive simplification through an instruction's uses. More...
 
const SimplifyQuery llvm::getBestSimplifyQuery (Pass &, Function &)
 
const SimplifyQuery llvm::getBestSimplifyQuery (LoopStandardAnalysisResults &, const DataLayout &)
 
template<class T , class... TArgs>
const SimplifyQuery llvm::getBestSimplifyQuery (AnalysisManager< T, TArgs... > &, Function &)
 
const template SimplifyQuery llvm::getBestSimplifyQuery (AnalysisManager< Function > &, Function &)
 

Macro Definition Documentation

◆ DEBUG_TYPE

#define DEBUG_TYPE   "instsimplify"

Definition at line 48 of file InstructionSimplify.cpp.

◆ HANDLE_CAST_INST

#define HANDLE_CAST_INST (   num,
  opc,
  clas 
)    case Instruction::opc:

Enumeration Type Documentation

◆ anonymous enum

anonymous enum
Enumerator
RecursionLimit 

Definition at line 50 of file InstructionSimplify.cpp.

Function Documentation

◆ computePointerDifference()

static Constant* computePointerDifference ( const DataLayout DL,
Value LHS,
Value RHS 
)
static

Compute the constant difference between two pointer values.

If the difference is not a constant, returns zero.

Definition at line 716 of file InstructionSimplify.cpp.

References DL, llvm::ConstantExpr::getSub(), and stripAndComputeConstantOffsets().

Referenced by SimplifySubInst().

◆ computePointerICmp()

static Constant* computePointerICmp ( CmpInst::Predicate  Pred,
Value LHS,
Value RHS,
const SimplifyQuery Q 
)
static

◆ ConstructLoadOperandConstant()

static Constant* ConstructLoadOperandConstant ( Value Op)
static

◆ expandBinOp()

static Value* expandBinOp ( Instruction::BinaryOps  Opcode,
Value V,
Value OtherOp,
Instruction::BinaryOps  OpcodeToExpand,
const SimplifyQuery Q,
unsigned  MaxRecurse 
)
static

Try to simplify a binary operator of form "V op OtherOp" where V is "(B0 opex B1)" by distributing 'op' across 'opex' as "(B0 op OtherOp) opex (B1 op OtherOp)".

Definition at line 241 of file InstructionSimplify.cpp.

References B, llvm::SimplifyQuery::getWithoutUndef(), llvm::Instruction::isCommutative(), S, and llvm::SimplifyBinOp().

Referenced by expandCommutativeBinOp().

◆ expandCommutativeBinOp()

static Value* expandCommutativeBinOp ( Instruction::BinaryOps  Opcode,
Value L,
Value R,
Instruction::BinaryOps  OpcodeToExpand,
const SimplifyQuery Q,
unsigned  MaxRecurse 
)
static

Try to simplify binops of form "A op (B op' C)" or the commuted variant by distributing op over op'.

Definition at line 275 of file InstructionSimplify.cpp.

References expandBinOp().

Referenced by SimplifyAndInst(), SimplifyMulInst(), and SimplifyOrInst().

◆ ExtractEquivalentCondition()

static Value* ExtractEquivalentCondition ( Value V,
CmpInst::Predicate  Pred,
Value LHS,
Value RHS 
)
static

Rummage around inside V looking for something equivalent to the comparison "LHS Pred RHS".

Return such a value if found, otherwise return null. Helper function for analyzing max/min idioms.

Definition at line 2409 of file InstructionSimplify.cpp.

References llvm::CmpInst::getSwappedPredicate(), and SI.

Referenced by simplifyICmpWithMinMax().

◆ foldConstant()

static Constant* foldConstant ( Instruction::UnaryOps  Opcode,
Value *&  Op,
const SimplifyQuery Q 
)
static

◆ foldIdentityShuffles()

static Value* foldIdentityShuffles ( int  DestElt,
Value Op0,
Value Op1,
int  MaskVal,
Value RootVec,
unsigned  MaxRecurse 
)
static

For the given destination element of a shuffle, peek through shuffles to match a root vector source operand that contains that element in the same vector lane (ie, the same mask index), so we can eliminate the shuffle(s).

Definition at line 4648 of file InstructionSimplify.cpp.

References llvm::Value::getType().

Referenced by SimplifyShuffleVectorInst().

◆ foldMinMaxSharedOp()

static Value* foldMinMaxSharedOp ( Intrinsic::ID  IID,
Value Op0,
Value Op1 
)
static

Given a min/max intrinsic, see if it can be removed based on having an operand that is another min/max intrinsic with shared operand(s).

The caller is expected to swap the operand arguments to handle commutation.

Definition at line 5488 of file InstructionSimplify.cpp.

References llvm::getInverseMinMaxIntrinsic(), llvm::PatternMatch::m_c_MaxOrMin(), llvm::PatternMatch::m_MaxOrMin(), llvm::PatternMatch::m_Specific(), llvm::PatternMatch::m_Value(), llvm::PatternMatch::match(), X, and Y.

Referenced by simplifyBinaryIntrinsic().

◆ foldOrCommuteConstant()

static Constant* foldOrCommuteConstant ( Instruction::BinaryOps  Opcode,
Value *&  Op0,
Value *&  Op1,
const SimplifyQuery Q 
)
static

◆ foldSelectWithBinaryOp()

static Value* foldSelectWithBinaryOp ( Value Cond,
Value TrueVal,
Value FalseVal 
)
static

◆ GetCompareTy()

static Type* GetCompareTy ( Value Op)
static

◆ getFalse()

static Constant* getFalse ( Type Ty)
static

◆ getMaxMinLimit()

static APInt getMaxMinLimit ( Intrinsic::ID  IID,
unsigned  BitWidth 
)
static

◆ getMaxMinPredicate()

static ICmpInst::Predicate getMaxMinPredicate ( Intrinsic::ID  IID)
static

◆ getTrue()

static Constant* getTrue ( Type Ty)
static

◆ handleOtherCmpSelSimplifications()

static Value* handleOtherCmpSelSimplifications ( Value TCmp,
Value FCmp,
Value Cond,
const SimplifyQuery Q,
unsigned  MaxRecurse 
)
static

We know comparison with both branches of select can be simplified, but they are not equal.

This routine handles some logical simplifications.

Definition at line 185 of file InstructionSimplify.cpp.

References Cond, llvm::Constant::getAllOnesValue(), llvm::impliesPoison(), llvm::PatternMatch::m_One(), llvm::PatternMatch::m_Zero(), llvm::PatternMatch::match(), llvm::SimplifyAndInst(), llvm::SimplifyOrInst(), and llvm::SimplifyXorInst().

Referenced by ThreadCmpOverSelect().

◆ isDivZero()

static bool isDivZero ( Value X,
Value Y,
const SimplifyQuery Q,
unsigned  MaxRecurse,
bool  IsSigned 
)
static

Return true if we can simplify X / Y to 0.

Remainder can adapt that answer to simplify X % Y to X.

Definition at line 1030 of file InstructionSimplify.cpp.

References llvm::ConstantInt::get(), llvm::CmpInst::ICMP_NE, llvm::CmpInst::ICMP_SGT, llvm::CmpInst::ICMP_SLT, llvm::CmpInst::ICMP_ULT, isICmpTrue(), llvm::PatternMatch::m_APInt(), llvm::PatternMatch::match(), X, and Y.

Referenced by simplifyDiv(), and simplifyRem().

◆ isICmpTrue()

static bool isICmpTrue ( ICmpInst::Predicate  Pred,
Value LHS,
Value RHS,
const SimplifyQuery Q,
unsigned  MaxRecurse 
)
static

Given a predicate and two operands, return true if the comparison is true.

This is a helper for div/rem simplification where we return some other value when we can prove a relationship between the operands.

Definition at line 1021 of file InstructionSimplify.cpp.

References llvm::SimplifyICmpInst().

Referenced by isDivZero(), and simplifyBinaryIntrinsic().

◆ IsIdempotent()

static bool IsIdempotent ( Intrinsic::ID  ID)
static

Definition at line 5302 of file InstructionSimplify.cpp.

References ceil, floor, round(), and trunc.

Referenced by simplifyUnaryIntrinsic().

◆ isPoisonShift()

static bool isPoisonShift ( Value Amount,
const SimplifyQuery Q 
)
static

Returns true if a shift by Amount always yields poison.

Definition at line 1226 of file InstructionSimplify.cpp.

References E, I, and llvm::SimplifyQuery::isUndefValue().

Referenced by SimplifyShift().

◆ isSameCompare()

static bool isSameCompare ( Value V,
CmpInst::Predicate  Pred,
Value LHS,
Value RHS 
)
static

isSameCompare - Is V equivalent to the comparison "LHS Pred RHS"?

Definition at line 131 of file InstructionSimplify.cpp.

References llvm::CmpInst::getSwappedPredicate().

Referenced by simplifyCmpSelCase().

◆ propagateNaN()

static Constant* propagateNaN ( Constant In)
static

Definition at line 4845 of file InstructionSimplify.cpp.

References llvm::ConstantFP::getNaN(), and llvm::tgtok::In.

Referenced by simplifyBinaryIntrinsic(), and simplifyFPOp().

◆ replaceAndRecursivelySimplifyImpl()

static bool replaceAndRecursivelySimplifyImpl ( Instruction I,
Value SimpleV,
const TargetLibraryInfo TLI,
const DominatorTree DT,
AssumptionCache AC,
SmallSetVector< Instruction *, 8 > *  UnsimplifiedUsers = nullptr 
)
static

Implementation of recursive simplification through an instruction's uses.

This is the common implementation of the recursive simplification routines. If we have a pre-simplified value in 'SimpleV', that is forcibly used to replace the instruction 'I'. Otherwise, we simply add 'I' to the list of instructions to process and attempt to simplify it using InstructionSimplify. Recursively visited users which could not be simplified themselves are to the optional UnsimplifiedUsers set for further processing by the caller.

This routine returns 'true' only when it simplifies something. The passed in simplified value does not count toward this.

Definition at line 6242 of file InstructionSimplify.cpp.

References DL, I, llvm::SetVector< T, SmallVector< T, N >, SmallDenseSet< T, N > >::insert(), llvm::JumpTable::Simplified, llvm::SimplifyInstruction(), and llvm::SetVector< T, SmallVector< T, N >, SmallDenseSet< T, N > >::size().

Referenced by llvm::replaceAndRecursivelySimplify().

◆ SimplifyAddInst()

static Value* SimplifyAddInst ( Value Op0,
Value Op1,
bool  IsNSW,
bool  IsNUW,
const SimplifyQuery Q,
unsigned  MaxRecurse 
)
static

◆ SimplifyAndInst()

static Value * SimplifyAndInst ( Value Op0,
Value Op1,
const SimplifyQuery Q,
unsigned  MaxRecurse 
)
static

◆ simplifyAndOfICmps()

static Value* simplifyAndOfICmps ( ICmpInst Op0,
ICmpInst Op1,
const SimplifyQuery Q 
)
static

◆ simplifyAndOfICmpsWithAdd()

static Value* simplifyAndOfICmpsWithAdd ( ICmpInst Op0,
ICmpInst Op1,
const InstrInfoQuery IIQ 
)
static

◆ simplifyAndOfICmpsWithSameOperands()

static Value* simplifyAndOfICmpsWithSameOperands ( ICmpInst Op0,
ICmpInst Op1 
)
static

◆ simplifyAndOrOfCmps()

static Value* simplifyAndOrOfCmps ( const SimplifyQuery Q,
Value Op0,
Value Op1,
bool  IsAnd 
)
static

◆ simplifyAndOrOfFCmps()

static Value* simplifyAndOrOfFCmps ( const TargetLibraryInfo TLI,
FCmpInst LHS,
FCmpInst RHS,
bool  IsAnd 
)
static

◆ simplifyAndOrOfICmpsWithConstants()

static Value* simplifyAndOrOfICmpsWithConstants ( ICmpInst Cmp0,
ICmpInst Cmp1,
bool  IsAnd 
)
static

Test if a pair of compares with a shared operand and 2 constants has an empty set intersection, full set union, or if one compare is a superset of the other.

Definition at line 1617 of file InstructionSimplify.cpp.

References C1, getFalse(), llvm::User::getOperand(), llvm::CmpInst::getPredicate(), getTrue(), llvm::Value::getType(), llvm::PatternMatch::m_APInt(), llvm::ConstantRange::makeExactICmpRegion(), and llvm::PatternMatch::match().

Referenced by simplifyAndOfICmps(), and simplifyOrOfICmps().

◆ simplifyAndOrOfICmpsWithLimitConst()

static Value* simplifyAndOrOfICmpsWithLimitConst ( ICmpInst Cmp0,
ICmpInst Cmp1,
bool  IsAnd 
)
static

◆ simplifyAndOrOfICmpsWithZero()

static Value* simplifyAndOrOfICmpsWithZero ( ICmpInst Cmp0,
ICmpInst Cmp1,
bool  IsAnd 
)
static

◆ SimplifyAShrInst()

static Value* SimplifyAShrInst ( Value Op0,
Value Op1,
bool  isExact,
const SimplifyQuery Q,
unsigned  MaxRecurse 
)
static

◆ SimplifyAssociativeBinOp()

static Value* SimplifyAssociativeBinOp ( Instruction::BinaryOps  Opcode,
Value LHS,
Value RHS,
const SimplifyQuery Q,
unsigned  MaxRecurse 
)
static

Generic simplifications for associative binary operations.

Returns the simpler value, or null if none was found.

Definition at line 293 of file InstructionSimplify.cpp.

References assert(), B, llvm::BinaryOperator::getOpcode(), llvm::User::getOperand(), llvm::Instruction::isAssociative(), llvm::Instruction::isCommutative(), llvm::SimplifyBinOp(), and llvm::RISCVFenceField::W.

Referenced by SimplifyAddInst(), SimplifyAndInst(), SimplifyMulInst(), SimplifyOrInst(), and SimplifyXorInst().

◆ simplifyBinaryIntrinsic()

static Value* simplifyBinaryIntrinsic ( Function F,
Value Op0,
Value Op1,
const SimplifyQuery Q 
)
static

◆ SimplifyBinOp() [1/2]

static Value * SimplifyBinOp ( unsigned  Opcode,
Value LHS,
Value RHS,
const FastMathFlags FMF,
const SimplifyQuery Q,
unsigned  MaxRecurse 
)
static

Given operands for a BinaryOperator, see if we can fold the result.

If not, this returns null. Try to use FastMathFlags when folding the result.

Definition at line 5262 of file InstructionSimplify.cpp.

References llvm::SimplifyBinOp(), llvm::SimplifyFAddInst(), llvm::SimplifyFDivInst(), llvm::SimplifyFMulInst(), and llvm::SimplifyFSubInst().

◆ SimplifyBinOp() [2/2]

static Value * SimplifyBinOp ( unsigned  Opcode,
Value LHS,
Value RHS,
const SimplifyQuery Q,
unsigned  MaxRecurse 
)
static

◆ SimplifyCastInst()

static Value * SimplifyCastInst ( unsigned  CastOpc,
Value Op,
Type Ty,
const SimplifyQuery Q,
unsigned  MaxRecurse 
)
static

◆ SimplifyCmpInst()

static Value * SimplifyCmpInst ( unsigned  Predicate,
Value LHS,
Value RHS,
const SimplifyQuery Q,
unsigned  MaxRecurse 
)
static

Given operands for a CmpInst, see if we can fold the result.

Definition at line 5290 of file InstructionSimplify.cpp.

References llvm::CmpInst::isIntPredicate(), llvm::SimplifyFCmpInst(), and llvm::SimplifyICmpInst().

Referenced by llvm::SimplifyCmpInst().

◆ simplifyCmpSelCase()

static Value* simplifyCmpSelCase ( CmpInst::Predicate  Pred,
Value LHS,
Value RHS,
Value Cond,
const SimplifyQuery Q,
unsigned  MaxRecurse,
Constant TrueOrFalse 
)
static

Simplify comparison with true or false branch of select: sel = select i1 cond, i32 tv, i32 fv cmp = icmp sle i32 sel, rhs Compose new comparison by substituting sel with either tv or fv and see if it simplifies.

Definition at line 149 of file InstructionSimplify.cpp.

References Cond, isSameCompare(), and llvm::SimplifyCmpInst().

Referenced by simplifyCmpSelFalseCase(), and simplifyCmpSelTrueCase().

◆ simplifyCmpSelFalseCase()

static Value* simplifyCmpSelFalseCase ( CmpInst::Predicate  Pred,
Value LHS,
Value RHS,
Value Cond,
const SimplifyQuery Q,
unsigned  MaxRecurse 
)
static

Simplify comparison with false branch of select.

Definition at line 175 of file InstructionSimplify.cpp.

References Cond, getFalse(), and simplifyCmpSelCase().

Referenced by ThreadCmpOverSelect().

◆ simplifyCmpSelTrueCase()

static Value* simplifyCmpSelTrueCase ( CmpInst::Predicate  Pred,
Value LHS,
Value RHS,
Value Cond,
const SimplifyQuery Q,
unsigned  MaxRecurse 
)
static

Simplify comparison with true branch of select.

Definition at line 166 of file InstructionSimplify.cpp.

References Cond, getTrue(), and simplifyCmpSelCase().

Referenced by ThreadCmpOverSelect().

◆ simplifyDiv()

static Value* simplifyDiv ( Instruction::BinaryOps  Opcode,
Value Op0,
Value Op1,
const SimplifyQuery Q,
unsigned  MaxRecurse 
)
static

◆ simplifyDivRem()

static Value* simplifyDivRem ( Instruction::BinaryOps  Opcode,
Value Op0,
Value Op1,
const SimplifyQuery Q 
)
static

◆ SimplifyExtractElementInst()

static Value* SimplifyExtractElementInst ( Value Vec,
Value Idx,
const SimplifyQuery Q,
unsigned   
)
static

Given operands for an ExtractElementInst, see if we can fold the result.

If not, this returns null.

Definition at line 4527 of file InstructionSimplify.cpp.

References llvm::findScalarElement(), llvm::UndefValue::get(), llvm::PoisonValue::get(), llvm::ConstantExpr::getExtractElement(), llvm::getSplatValue(), llvm::Value::getType(), and llvm::SimplifyQuery::isUndefValue().

Referenced by llvm::SimplifyExtractElementInst().

◆ SimplifyExtractValueInst()

static Value* SimplifyExtractValueInst ( Value Agg,
ArrayRef< unsigned >  Idxs,
const SimplifyQuery ,
unsigned   
)
static

Given operands for an ExtractValueInst, see if we can fold the result.

If not, this returns null.

Definition at line 4497 of file InstructionSimplify.cpp.

References llvm::ConstantFoldExtractValueInstruction(), llvm::min(), llvm::ArrayRef< T >::size(), and llvm::ArrayRef< T >::slice().

Referenced by llvm::SimplifyExtractValueInst().

◆ SimplifyFAddInst()

static Value* SimplifyFAddInst ( Value Op0,
Value Op1,
FastMathFlags  FMF,
const SimplifyQuery Q,
unsigned  MaxRecurse,
fp::ExceptionBehavior  ExBehavior = fp::ebIgnore,
RoundingMode  Rounding = RoundingMode::NearestTiesToEven 
)
static

◆ SimplifyFCmpInst()

static Value* SimplifyFCmpInst ( unsigned  Predicate,
Value LHS,
Value RHS,
FastMathFlags  FMF,
const SimplifyQuery Q,
unsigned  MaxRecurse 
)
static

◆ SimplifyFDivInst()

static Value* SimplifyFDivInst ( Value Op0,
Value Op1,
FastMathFlags  FMF,
const SimplifyQuery Q,
unsigned  ,
fp::ExceptionBehavior  ExBehavior = fp::ebIgnore,
RoundingMode  Rounding = RoundingMode::NearestTiesToEven 
)
static

◆ SimplifyFMAFMul()

static Value* SimplifyFMAFMul ( Value Op0,
Value Op1,
FastMathFlags  FMF,
const SimplifyQuery Q,
unsigned  MaxRecurse,
fp::ExceptionBehavior  ExBehavior,
RoundingMode  Rounding 
)
static

◆ SimplifyFMulInst()

static Value* SimplifyFMulInst ( Value Op0,
Value Op1,
FastMathFlags  FMF,
const SimplifyQuery Q,
unsigned  MaxRecurse,
fp::ExceptionBehavior  ExBehavior = fp::ebIgnore,
RoundingMode  Rounding = RoundingMode::NearestTiesToEven 
)
static

Given the operands for an FMul, see if we can fold the result.

Definition at line 5039 of file InstructionSimplify.cpp.

References foldOrCommuteConstant(), llvm::isDefaultFPEnvironment(), and llvm::SimplifyFMAFMul().

Referenced by llvm::SimplifyFMulInst().

◆ simplifyFNegInst()

static Value* simplifyFNegInst ( Value Op,
FastMathFlags  FMF,
const SimplifyQuery Q,
unsigned  MaxRecurse 
)
static

Given the operand for an FNeg, see if we can fold the result.

If not, this returns null.

Definition at line 4827 of file InstructionSimplify.cpp.

References foldConstant(), llvm::PatternMatch::m_FNeg(), llvm::PatternMatch::m_Value(), llvm::PatternMatch::match(), and X.

Referenced by llvm::SimplifyFNegInst(), simplifyFPUnOp(), and simplifyUnOp().

◆ simplifyFPOp()

static Constant* simplifyFPOp ( ArrayRef< Value * >  Ops,
FastMathFlags  FMF,
const SimplifyQuery Q,
fp::ExceptionBehavior  ExBehavior,
RoundingMode  Rounding 
)
static

◆ simplifyFPUnOp()

static Value * simplifyFPUnOp ( unsigned  Opcode,
Value Op,
const FastMathFlags FMF,
const SimplifyQuery Q,
unsigned  MaxRecurse 
)
static

Given the operand for a UnaryOperator, see if we can fold the result.

If not, this returns null. Try to use FastMathFlags when folding the result.

Definition at line 5193 of file InstructionSimplify.cpp.

References simplifyFNegInst(), and simplifyUnOp().

Referenced by llvm::SimplifyUnOp().

◆ SimplifyFreezeInst()

static Value* SimplifyFreezeInst ( Value Op0,
const SimplifyQuery Q 
)
static

Given operands for a Freeze, see if we can fold the result.

Definition at line 5978 of file InstructionSimplify.cpp.

References llvm::SimplifyQuery::AC, llvm::SimplifyQuery::CxtI, llvm::SimplifyQuery::DT, and llvm::isGuaranteedNotToBeUndefOrPoison().

Referenced by llvm::SimplifyFreezeInst().

◆ SimplifyFRemInst()

static Value* SimplifyFRemInst ( Value Op0,
Value Op1,
FastMathFlags  FMF,
const SimplifyQuery Q,
unsigned  ,
fp::ExceptionBehavior  ExBehavior = fp::ebIgnore,
RoundingMode  Rounding = RoundingMode::NearestTiesToEven 
)
static

◆ SimplifyFSubInst()

static Value* SimplifyFSubInst ( Value Op0,
Value Op1,
FastMathFlags  FMF,
const SimplifyQuery Q,
unsigned  MaxRecurse,
fp::ExceptionBehavior  ExBehavior = fp::ebIgnore,
RoundingMode  Rounding = RoundingMode::NearestTiesToEven 
)
static

◆ SimplifyGEPInst()

static Value * SimplifyGEPInst ( Type SrcTy,
ArrayRef< Value * >  Ops,
const SimplifyQuery Q,
unsigned   
)
static

◆ SimplifyICmpInst()

static Value * SimplifyICmpInst ( unsigned  Predicate,
Value LHS,
Value RHS,
const SimplifyQuery Q,
unsigned  MaxRecurse 
)
static

Given operands for an ICmpInst, see if we can fold the result.

If not, this returns null.

Definition at line 3368 of file InstructionSimplify.cpp.

References llvm::SimplifyQuery::AC, assert(), computePointerICmp(), llvm::ConstantFoldCompareInstOperands(), llvm::ConstantFoldConstant(), llvm::SimplifyQuery::CxtI, llvm::SimplifyQuery::DL, llvm::SimplifyQuery::DT, llvm::ConstantInt::get(), llvm::UndefValue::get(), llvm::PoisonValue::get(), llvm::ConstantExpr::getCast(), GetCompareTy(), llvm::getConstantRangeFromMetadata(), llvm::Value::getContext(), llvm::ConstantInt::getFalse(), getFalse(), llvm::ConstantExpr::getGetElementPtr(), llvm::ConstantExpr::getICmp(), llvm::ConstantExpr::getIntToPtr(), llvm::CmpInst::getInversePredicate(), llvm::InstrInfoQuery::getMetadata(), llvm::Constant::getNullValue(), llvm::User::getOperand(), llvm::Type::getPrimitiveSizeInBits(), llvm::ICmpInst::getSignedPredicate(), llvm::CmpInst::getSwappedPredicate(), llvm::ConstantInt::getTrue(), getTrue(), llvm::ConstantExpr::getTrunc(), llvm::Value::getType(), llvm::DataLayout::getTypeSizeInBits(), llvm::ICmpInst::getUnsignedPredicate(), llvm::CmpInst::ICMP_EQ, llvm::CmpInst::ICMP_NE, llvm::CmpInst::ICMP_SGE, llvm::CmpInst::ICMP_SGT, llvm::CmpInst::ICMP_SLE, llvm::CmpInst::ICMP_SLT, llvm::CmpInst::ICMP_UGE, llvm::CmpInst::ICMP_UGT, llvm::CmpInst::ICMP_ULE, llvm::CmpInst::ICMP_ULT, llvm::SimplifyQuery::IIQ, llvm::ICmpInst::isEquality(), llvm::CmpInst::isIntPredicate(), llvm::isKnownNonEqual(), llvm::Type::isPointerTy(), llvm::CmpInst::isTrueWhenEqual(), llvm::SimplifyQuery::isUndefValue(), llvm_unreachable, llvm::PatternMatch::m_Zero(), llvm::PatternMatch::match(), llvm::SimplifyICmpInst(), simplifyICmpOfBools(), simplifyICmpWithBinOp(), simplifyICmpWithConstant(), simplifyICmpWithDominatingAssume(), simplifyICmpWithMinMax(), simplifyICmpWithZero(), std::swap(), ThreadCmpOverPHI(), ThreadCmpOverSelect(), llvm::SimplifyQuery::TLI, and llvm::InstrInfoQuery::UseInstrInfo.

Referenced by llvm::SimplifyICmpInst().

◆ simplifyICmpOfBools()

static Value* simplifyICmpOfBools ( CmpInst::Predicate  Pred,
Value LHS,
Value RHS,
const SimplifyQuery Q 
)
static

Fold an icmp when its operands have i1 scalar type.

For signed comparison, the values for an i1 are 0 and -1 respectively. This maps into a truth table of: LHS | RHS | LHS >=s RHS | LHS implies RHS 0 | 0 | 1 (0 >= 0) | 1 0 | 1 | 1 (0 >= -1) | 1 1 | 0 | 0 (-1 >= 0) | 0 1 | 1 | 1 (-1 >= -1) | 1

Definition at line 2645 of file InstructionSimplify.cpp.

References llvm::SimplifyQuery::DL, GetCompareTy(), getFalse(), getTrue(), llvm::Value::getType(), llvm::Optional< T >::getValueOr(), llvm::CmpInst::ICMP_EQ, llvm::CmpInst::ICMP_NE, llvm::CmpInst::ICMP_SGE, llvm::CmpInst::ICMP_SGT, llvm::CmpInst::ICMP_SLE, llvm::CmpInst::ICMP_SLT, llvm::CmpInst::ICMP_UGE, llvm::CmpInst::ICMP_UGT, llvm::CmpInst::ICMP_ULE, llvm::CmpInst::ICMP_ULT, llvm::isImpliedCondition(), llvm::Type::isIntOrIntVectorTy(), llvm::PatternMatch::m_One(), llvm::PatternMatch::m_Zero(), and llvm::PatternMatch::match().

Referenced by SimplifyICmpInst().

◆ simplifyICmpWithBinOp()

static Value* simplifyICmpWithBinOp ( CmpInst::Predicate  Pred,
Value LHS,
Value RHS,
const SimplifyQuery Q,
unsigned  MaxRecurse 
)
static

◆ simplifyICmpWithBinOpOnLHS()

static Value* simplifyICmpWithBinOpOnLHS ( CmpInst::Predicate  Pred,
BinaryOperator LBO,
Value RHS,
const SimplifyQuery Q,
unsigned  MaxRecurse 
)
static

◆ simplifyICmpWithConstant()

static Value* simplifyICmpWithConstant ( CmpInst::Predicate  Pred,
Value LHS,
Value RHS,
const InstrInfoQuery IIQ 
)
static

◆ simplifyICmpWithDominatingAssume()

static Value* simplifyICmpWithDominatingAssume ( CmpInst::Predicate  Predicate,
Value LHS,
Value RHS,
const SimplifyQuery Q 
)
static

◆ simplifyICmpWithMinMax()

static Value* simplifyICmpWithMinMax ( CmpInst::Predicate  Pred,
Value LHS,
Value RHS,
const SimplifyQuery Q,
unsigned  MaxRecurse 
)
static

◆ simplifyICmpWithZero()

static Value* simplifyICmpWithZero ( CmpInst::Predicate  Pred,
Value LHS,
Value RHS,
const SimplifyQuery Q 
)
static

◆ SimplifyInsertValueInst()

static Value* SimplifyInsertValueInst ( Value Agg,
Value Val,
ArrayRef< unsigned >  Idxs,
const SimplifyQuery Q,
unsigned   
)
static

Given operands for an InsertValueInst, see if we can fold the result.

If not, this returns null.

Definition at line 4427 of file InstructionSimplify.cpp.

References llvm::ConstantFoldInsertValueInstruction(), llvm::Value::getType(), and llvm::SimplifyQuery::isUndefValue().

Referenced by llvm::SimplifyInsertValueInst().

◆ simplifyIntrinsic()

static Value* simplifyIntrinsic ( CallBase Call,
const SimplifyQuery Q 
)
static

◆ SimplifyLoadInst()

static Value* SimplifyLoadInst ( LoadInst LI,
const SimplifyQuery Q 
)
static

◆ simplifyLogicOfAddSub()

static Value* simplifyLogicOfAddSub ( Value Op0,
Value Op1,
Instruction::BinaryOps  Opcode 
)
static

◆ SimplifyLShrInst()

static Value* SimplifyLShrInst ( Value Op0,
Value Op1,
bool  isExact,
const SimplifyQuery Q,
unsigned  MaxRecurse 
)
static

◆ SimplifyMulInst()

static Value* SimplifyMulInst ( Value Op0,
Value Op1,
const SimplifyQuery Q,
unsigned  MaxRecurse 
)
static

◆ SimplifyOrInst()

static Value * SimplifyOrInst ( Value Op0,
Value Op1,
const SimplifyQuery Q,
unsigned  MaxRecurse 
)
static

◆ simplifyOrOfICmps()

static Value* simplifyOrOfICmps ( ICmpInst Op0,
ICmpInst Op1,
const SimplifyQuery Q 
)
static

◆ simplifyOrOfICmpsWithAdd()

static Value* simplifyOrOfICmpsWithAdd ( ICmpInst Op0,
ICmpInst Op1,
const InstrInfoQuery IIQ 
)
static

◆ simplifyOrOfICmpsWithSameOperands()

static Value* simplifyOrOfICmpsWithSameOperands ( ICmpInst Op0,
ICmpInst Op1 
)
static

◆ SimplifyPHINode()

static Value* SimplifyPHINode ( PHINode PN,
const SimplifyQuery Q 
)
static

◆ SimplifyRelativeLoad()

static Value* SimplifyRelativeLoad ( Constant Ptr,
Constant Offset,
const DataLayout DL 
)
static

◆ simplifyRem()

static Value* simplifyRem ( Instruction::BinaryOps  Opcode,
Value Op0,
Value Op1,
const SimplifyQuery Q,
unsigned  MaxRecurse 
)
static

◆ SimplifyRightShift()

static Value* SimplifyRightShift ( Instruction::BinaryOps  Opcode,
Value Op0,
Value Op1,
bool  isExact,
const SimplifyQuery Q,
unsigned  MaxRecurse 
)
static

◆ SimplifySDivInst()

static Value* SimplifySDivInst ( Value Op0,
Value Op1,
const SimplifyQuery Q,
unsigned  MaxRecurse 
)
static

Given operands for an SDiv, see if we can fold the result.

If not, this returns null.

Definition at line 1169 of file InstructionSimplify.cpp.

References llvm::Constant::getAllOnesValue(), llvm::Value::getType(), llvm::isKnownNegation(), and simplifyDiv().

Referenced by llvm::SimplifySDivInst().

◆ simplifySelectBitTest()

static Value* simplifySelectBitTest ( Value TrueVal,
Value FalseVal,
Value X,
const APInt Y,
bool  TrueWhenUnset 
)
static

Try to simplify a select instruction when its condition operand is an integer comparison where one operand of the compare is a constant.

Definition at line 4018 of file InstructionSimplify.cpp.

References llvm::tgtok::FalseVal, llvm::PatternMatch::m_And(), llvm::PatternMatch::m_APInt(), llvm::PatternMatch::m_Or(), llvm::PatternMatch::m_Specific(), llvm::PatternMatch::match(), llvm::tgtok::TrueVal, X, and Y.

Referenced by simplifySelectWithFakeICmpEq(), and simplifySelectWithICmpCond().

◆ SimplifySelectInst()

static Value * SimplifySelectInst ( Value Cond,
Value TrueVal,
Value FalseVal,
const SimplifyQuery Q,
unsigned  MaxRecurse 
)
static

◆ simplifySelectWithFakeICmpEq()

static Value* simplifySelectWithFakeICmpEq ( Value CmpLHS,
Value CmpRHS,
ICmpInst::Predicate  Pred,
Value TrueVal,
Value FalseVal 
)
static

An alternative way to test if a bit is set or not uses sgt/slt instead of eq/ne.

Definition at line 4053 of file InstructionSimplify.cpp.

References llvm::decomposeBitTestICmp(), llvm::tgtok::FalseVal, llvm::CmpInst::ICMP_EQ, llvm::BitmaskEnumDetail::Mask(), simplifySelectBitTest(), llvm::tgtok::TrueVal, and X.

Referenced by simplifySelectWithICmpCond().

◆ simplifySelectWithFCmp()

static Value* simplifySelectWithFCmp ( Value Cond,
Value T,
Value F,
const SimplifyQuery Q 
)
static

◆ simplifySelectWithICmpCond()

static Value* simplifySelectWithICmpCond ( Value CondVal,
Value TrueVal,
Value FalseVal,
const SimplifyQuery Q,
unsigned  MaxRecurse 
)
static

◆ SimplifyShift()

static Value* SimplifyShift ( Instruction::BinaryOps  Opcode,
Value Op0,
Value Op1,
bool  IsNSW,
const SimplifyQuery Q,
unsigned  MaxRecurse 
)
static

◆ SimplifyShlInst()

static Value* SimplifyShlInst ( Value Op0,
Value Op1,
bool  isNSW,
bool  isNUW,
const SimplifyQuery Q,
unsigned  MaxRecurse 
)
static

◆ SimplifyShuffleVectorInst()

static Value* SimplifyShuffleVectorInst ( Value Op0,
Value Op1,
ArrayRef< int Mask,
Type RetTy,
const SimplifyQuery Q,
unsigned  MaxRecurse 
)
static

◆ SimplifySRemInst()

static Value* SimplifySRemInst ( Value Op0,
Value Op1,
const SimplifyQuery Q,
unsigned  MaxRecurse 
)
static

Given operands for an SRem, see if we can fold the result.

If not, this returns null.

Definition at line 1195 of file InstructionSimplify.cpp.

References llvm::Constant::getNullValue(), llvm::Value::getType(), llvm::isKnownNegation(), llvm::PatternMatch::m_SExt(), llvm::PatternMatch::m_Value(), llvm::PatternMatch::match(), simplifyRem(), and X.

Referenced by llvm::SimplifySRemInst().

◆ SimplifySubInst()

static Value* SimplifySubInst ( Value Op0,
Value Op1,
bool  isNSW,
bool  isNUW,
const SimplifyQuery Q,
unsigned  MaxRecurse 
)
static

◆ SimplifyUDivInst()

static Value* SimplifyUDivInst ( Value Op0,
Value Op1,
const SimplifyQuery Q,
unsigned  MaxRecurse 
)
static

Given operands for a UDiv, see if we can fold the result.

If not, this returns null.

Definition at line 1184 of file InstructionSimplify.cpp.

References simplifyDiv().

Referenced by llvm::SimplifyUDivInst().

◆ simplifyUnaryIntrinsic()

static Value* simplifyUnaryIntrinsic ( Function F,
Value Op0,
const SimplifyQuery Q 
)
static

◆ simplifyUnOp()

static Value * simplifyUnOp ( unsigned  Opcode,
Value Op,
const SimplifyQuery Q,
unsigned  MaxRecurse 
)
static

Given the operand for a UnaryOperator, see if we can fold the result.

If not, this returns null.

Definition at line 5180 of file InstructionSimplify.cpp.

References llvm_unreachable, and simplifyFNegInst().

Referenced by simplifyFPUnOp(), and llvm::SimplifyUnOp().

◆ simplifyUnsignedRangeCheck()

static Value* simplifyUnsignedRangeCheck ( ICmpInst ZeroICmp,
ICmpInst UnsignedICmp,
bool  IsAnd,
const SimplifyQuery Q 
)
static

◆ SimplifyURemInst()

static Value* SimplifyURemInst ( Value Op0,
Value Op1,
const SimplifyQuery Q,
unsigned  MaxRecurse 
)
static

Given operands for a URem, see if we can fold the result.

If not, this returns null.

Definition at line 1216 of file InstructionSimplify.cpp.

References simplifyRem().

Referenced by llvm::SimplifyURemInst().

◆ simplifyWithOpReplaced()

static Value* simplifyWithOpReplaced ( Value V,
Value Op,
Value RepOp,
const SimplifyQuery Q,
bool  AllowRefinement,
unsigned  MaxRecurse 
)
static

◆ SimplifyXorInst()

static Value * SimplifyXorInst ( Value Op0,
Value Op1,
const SimplifyQuery Q,
unsigned  MaxRecurse 
)
static

◆ STATISTIC() [1/2]

STATISTIC ( NumExpand  ,
"Number of expansions"   
)

◆ STATISTIC() [2/2]

STATISTIC ( NumReassoc  ,
"Number of reassociations"   
)

◆ stripAndComputeConstantOffsets()

static Constant* stripAndComputeConstantOffsets ( const DataLayout DL,
Value *&  V,
bool  AllowNonInbounds = false 
)
static

Compute the base pointer and cumulative constant offsets for V.

This strips all constant offsets off of V, leaving it the base pointer, and accumulates the total constant offset applied in the returned constant. It returns 0 if V is not a pointer, and returns the constant '0' if there are no constant offsets applied.

This is very similar to GetPointerBaseWithConstantOffset except it doesn't follow non-inbounds geps. This allows it to remain usable for icmp ult/etc. folding.

Definition at line 695 of file InstructionSimplify.cpp.

References assert(), DL, llvm::ConstantInt::get(), llvm::Type::getIntegerBitWidth(), llvm::APInt::getNullValue(), llvm::ConstantVector::getSplat(), llvm::Value::getType(), llvm::Type::isPtrOrPtrVectorTy(), Offset, and llvm::Value::stripAndAccumulateConstantOffsets().

Referenced by computePointerDifference(), and computePointerICmp().

◆ ThreadBinOpOverPHI()

static Value* ThreadBinOpOverPHI ( Instruction::BinaryOps  Opcode,
Value LHS,
Value RHS,
const SimplifyQuery Q,
unsigned  MaxRecurse 
)
static

In the case of a binary operation with an operand that is a PHI instruction, try to simplify the binop by seeing whether evaluating it on the incoming phi values yields the same result for every value.

If so returns the common value, otherwise returns null.

Definition at line 517 of file InstructionSimplify.cpp.

References assert(), llvm::SimplifyQuery::DT, llvm::PHINode::incoming_values(), llvm::SimplifyBinOp(), and valueDominatesPHI().

Referenced by SimplifyAndInst(), simplifyDiv(), SimplifyMulInst(), SimplifyOrInst(), simplifyRem(), and SimplifyShift().

◆ ThreadBinOpOverSelect()

static Value* ThreadBinOpOverSelect ( Instruction::BinaryOps  Opcode,
Value LHS,
Value RHS,
const SimplifyQuery Q,
unsigned  MaxRecurse 
)
static

In the case of a binary operation with a select instruction as an operand, try to simplify the binop by seeing whether evaluating it on both branches of the select results in the same value.

Returns the common value if so, otherwise returns null.

Definition at line 393 of file InstructionSimplify.cpp.

References assert(), llvm::SimplifyQuery::isUndefValue(), SI, llvm::JumpTable::Simplified, and llvm::SimplifyBinOp().

Referenced by SimplifyAndInst(), simplifyDiv(), SimplifyMulInst(), SimplifyOrInst(), simplifyRem(), and SimplifyShift().

◆ ThreadCmpOverPHI()

static Value* ThreadCmpOverPHI ( CmpInst::Predicate  Pred,
Value LHS,
Value RHS,
const SimplifyQuery Q,
unsigned  MaxRecurse 
)
static

In the case of a comparison with a PHI instruction, try to simplify the comparison by seeing whether comparing with all of the incoming phi values yields the same result every time.

If so returns the common result, otherwise returns null.

Definition at line 560 of file InstructionSimplify.cpp.

References assert(), llvm::SimplifyQuery::DT, llvm::numbers::e, llvm::PHINode::getIncomingBlock(), llvm::PHINode::getIncomingValue(), llvm::PHINode::getNumIncomingValues(), llvm::CmpInst::getSwappedPredicate(), llvm::BasicBlock::getTerminator(), llvm::SimplifyQuery::getWithInstruction(), llvm::SimplifyCmpInst(), std::swap(), and valueDominatesPHI().

Referenced by SimplifyFCmpInst(), and SimplifyICmpInst().

◆ ThreadCmpOverSelect()

static Value* ThreadCmpOverSelect ( CmpInst::Predicate  Pred,
Value LHS,
Value RHS,
const SimplifyQuery Q,
unsigned  MaxRecurse 
)
static

In the case of a comparison with a select instruction, try to simplify the comparison by seeing whether both branches of the select result in the same value.

Returns the common value if so, otherwise returns null. For example, if we have: tmp = select i1 cmp, i32 1, i32 2 cmp1 = icmp sle i32 tmp, 3 We can simplify cmp1 to true, because both branches of select are less than 3. We compose new comparison by substituting tmp with both branches of select and see if it can be simplified.

Definition at line 471 of file InstructionSimplify.cpp.

References assert(), Cond, llvm::CmpInst::getSwappedPredicate(), llvm::Value::getType(), handleOtherCmpSelSimplifications(), llvm::Type::isVectorTy(), SI, simplifyCmpSelFalseCase(), simplifyCmpSelTrueCase(), and std::swap().

Referenced by SimplifyFCmpInst(), and SimplifyICmpInst().

◆ tryConstantFoldCall()

static Value* tryConstantFoldCall ( CallBase Call,
const SimplifyQuery Q 
)
static

◆ trySimplifyICmpWithAdds()

static bool trySimplifyICmpWithAdds ( CmpInst::Predicate  Pred,
Value LHS,
Value RHS 
)
static

◆ valueDominatesPHI()

static bool valueDominatesPHI ( Value V,
PHINode P,
const DominatorTree DT 
)
static

Does the given value dominate the specified phi node?

Definition at line 213 of file InstructionSimplify.cpp.

References llvm::DominatorTree::dominates(), I, and P.

Referenced by SimplifyPHINode(), ThreadBinOpOverPHI(), and ThreadCmpOverPHI().