LLVM  4.0.0
Macros | Enumerations | Functions
InstructionSimplify.cpp File Reference
#include "llvm/Analysis/InstructionSimplify.h"
#include "llvm/ADT/SetVector.h"
#include "llvm/ADT/Statistic.h"
#include "llvm/Analysis/AliasAnalysis.h"
#include "llvm/Analysis/CaptureTracking.h"
#include "llvm/Analysis/ConstantFolding.h"
#include "llvm/Analysis/MemoryBuiltins.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/Operator.h"
#include "llvm/IR/PatternMatch.h"
#include "llvm/IR/ValueHandle.h"
#include <algorithm>
#include "llvm/IR/Instruction.def"
Include dependency graph for InstructionSimplify.cpp:

Go to the source code of this file.

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 Query &Q, unsigned MaxRecurse)
 Given operands for an And, see if we can fold the result. More...
 
static ValueSimplifyBinOp (unsigned Opcode, Value *LHS, Value *RHS, const Query &Q, unsigned MaxRecurse)
 Given operands for a BinaryOperator, see if we can fold the result. More...
 
static ValueSimplifyFPBinOp (unsigned Opcode, Value *LHS, Value *RHS, const FastMathFlags &FMF, const Query &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 Query &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 Query &Q, unsigned MaxRecurse)
 Given operands for an ICmpInst, see if we can fold the result. More...
 
static ValueSimplifyOrInst (Value *Op0, Value *Op1, const Query &Q, unsigned MaxRecurse)
 Given operands for an Or, see if we can fold the result. More...
 
static ValueSimplifyXorInst (Value *Op0, Value *Op1, const Query &Q, unsigned MaxRecurse)
 Given operands for a Xor, see if we can fold the result. More...
 
static ValueSimplifyCastInst (unsigned, Value *, Type *, const Query &, unsigned)
 
static ConstantgetFalse (Type *Ty)
 For a boolean type, or a vector of boolean type, return false, or a vector with every element false, as appropriate for the type. More...
 
static ConstantgetTrue (Type *Ty)
 For a boolean type, or a vector of boolean type, return true, or a vector with every element true, as appropriate for the type. 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 bool ValueDominatesPHI (Value *V, PHINode *P, const DominatorTree *DT)
 Does the given value dominate the specified phi node? More...
 
static ValueExpandBinOp (unsigned Opcode, Value *LHS, Value *RHS, unsigned OpcToExpand, const Query &Q, unsigned MaxRecurse)
 Simplify "A op (B op' C)" by distributing op over op', turning it into "(A op B) op' (A op C)". More...
 
static ValueSimplifyAssociativeBinOp (unsigned Opc, Value *LHS, Value *RHS, const Query &Q, unsigned MaxRecurse)
 Generic simplifications for associative binary operations. More...
 
static ValueThreadBinOpOverSelect (unsigned Opcode, Value *LHS, Value *RHS, const Query &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 Query &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 (unsigned Opcode, Value *LHS, Value *RHS, const Query &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 Query &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 ValueSimplifyAddInst (Value *Op0, Value *Op1, bool isNSW, bool isNUW, const Query &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 Query &Q, unsigned MaxRecurse)
 Given operands for a Sub, see if we can fold the result. More...
 
static ValueSimplifyFAddInst (Value *Op0, Value *Op1, FastMathFlags FMF, const Query &Q, unsigned MaxRecurse)
 Given operands for an FAdd, see if we can fold the result. More...
 
static ValueSimplifyFSubInst (Value *Op0, Value *Op1, FastMathFlags FMF, const Query &Q, unsigned MaxRecurse)
 Given operands for an FSub, see if we can fold the result. More...
 
static ValueSimplifyFMulInst (Value *Op0, Value *Op1, FastMathFlags FMF, const Query &Q, unsigned MaxRecurse)
 Given the operands for an FMul, see if we can fold the result. More...
 
static ValueSimplifyMulInst (Value *Op0, Value *Op1, const Query &Q, unsigned MaxRecurse)
 Given operands for a Mul, see if we can fold the result. More...
 
static ValueSimplifyDiv (Instruction::BinaryOps Opcode, Value *Op0, Value *Op1, const Query &Q, unsigned MaxRecurse)
 Given operands for an SDiv or UDiv, see if we can fold the result. More...
 
static ValueSimplifySDivInst (Value *Op0, Value *Op1, const Query &Q, unsigned MaxRecurse)
 Given operands for an SDiv, see if we can fold the result. More...
 
static ValueSimplifyUDivInst (Value *Op0, Value *Op1, const Query &Q, unsigned MaxRecurse)
 Given operands for a UDiv, see if we can fold the result. More...
 
static ValueSimplifyFDivInst (Value *Op0, Value *Op1, FastMathFlags FMF, const Query &Q, unsigned)
 
static ValueSimplifyRem (Instruction::BinaryOps Opcode, Value *Op0, Value *Op1, const Query &Q, unsigned MaxRecurse)
 Given operands for an SRem or URem, see if we can fold the result. More...
 
static ValueSimplifySRemInst (Value *Op0, Value *Op1, const Query &Q, unsigned MaxRecurse)
 Given operands for an SRem, see if we can fold the result. More...
 
static ValueSimplifyURemInst (Value *Op0, Value *Op1, const Query &Q, unsigned MaxRecurse)
 Given operands for a URem, see if we can fold the result. More...
 
static ValueSimplifyFRemInst (Value *Op0, Value *Op1, FastMathFlags FMF, const Query &, unsigned)
 
static bool isUndefShift (Value *Amount)
 Returns true if a shift by Amount always yields undef. More...
 
static ValueSimplifyShift (unsigned Opcode, Value *Op0, Value *Op1, const Query &Q, unsigned MaxRecurse)
 Given operands for an Shl, LShr or AShr, see if we can fold the result. More...
 
static ValueSimplifyRightShift (unsigned Opcode, Value *Op0, Value *Op1, bool isExact, const Query &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 Query &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 Query &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 Query &Q, unsigned MaxRecurse)
 Given operands for an AShr, see if we can fold the result. More...
 
static ValuesimplifyUnsignedRangeCheck (ICmpInst *ZeroICmp, ICmpInst *UnsignedICmp, bool IsAnd)
 
static ValuesimplifyAndOfICmpsWithSameOperands (ICmpInst *Op0, ICmpInst *Op1)
 Commuted variants are assumed to be handled by calling this function again with the parameters swapped. More...
 
static ValueSimplifyAndOfICmps (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 ValueSimplifyOrOfICmps (ICmpInst *Op0, ICmpInst *Op1)
 Commuted variants are assumed to be handled by calling this function again with the parameters swapped. 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 (const DataLayout &DL, const TargetLibraryInfo *TLI, const DominatorTree *DT, CmpInst::Predicate Pred, const Instruction *CxtI, Value *LHS, Value *RHS)
 
static ValuesimplifyICmpOfBools (CmpInst::Predicate Pred, Value *LHS, Value *RHS, const Query &Q)
 Fold an icmp when its operands have i1 scalar type. More...
 
static ValuesimplifyICmpWithZero (CmpInst::Predicate Pred, Value *LHS, Value *RHS, const Query &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)
 
static ValuesimplifyICmpWithBinOp (CmpInst::Predicate Pred, Value *LHS, Value *RHS, const Query &Q, unsigned MaxRecurse)
 
static ValuesimplifyICmpWithMinMax (CmpInst::Predicate Pred, Value *LHS, Value *RHS, const Query &Q, unsigned MaxRecurse)
 Simplify integer comparisons where at least one operand of the compare matches an integer min/max idiom. More...
 
static ValueSimplifyFCmpInst (unsigned Predicate, Value *LHS, Value *RHS, FastMathFlags FMF, const Query &Q, unsigned MaxRecurse)
 Given operands for an FCmpInst, see if we can fold the result. More...
 
static const ValueSimplifyWithOpReplaced (Value *V, Value *Op, Value *RepOp, const Query &Q, unsigned MaxRecurse)
 See if V simplifies when its operand Op is replaced with RepOp. More...
 
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 *TrueVal, Value *FalseVal, bool TrueWhenUnset)
 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 Query &Q, unsigned MaxRecurse)
 Try to simplify a select instruction when its condition operand is an integer comparison. More...
 
static ValueSimplifySelectInst (Value *CondVal, Value *TrueVal, Value *FalseVal, const Query &Q, unsigned MaxRecurse)
 Given operands for a SelectInst, see if we can fold the result. More...
 
static ValueSimplifyGEPInst (Type *SrcTy, ArrayRef< Value * > Ops, const Query &Q, unsigned)
 Given operands for an GetElementPtrInst, see if we can fold the result. More...
 
static ValueSimplifyInsertValueInst (Value *Agg, Value *Val, ArrayRef< unsigned > Idxs, const Query &Q, unsigned)
 Given operands for an InsertValueInst, see if we can fold the result. More...
 
static ValueSimplifyExtractValueInst (Value *Agg, ArrayRef< unsigned > Idxs, const Query &, unsigned)
 Given operands for an ExtractValueInst, see if we can fold the result. More...
 
static ValueSimplifyExtractElementInst (Value *Vec, Value *Idx, const Query &, unsigned)
 Given operands for an ExtractElementInst, see if we can fold the result. More...
 
static ValueSimplifyPHINode (PHINode *PN, const Query &Q)
 See if we can fold the given phi. If not, returns null. More...
 
static bool IsIdempotent (Intrinsic::ID ID)
 
static ValueSimplifyRelativeLoad (Constant *Ptr, Constant *Offset, const DataLayout &DL)
 
static bool maskIsAllZeroOrUndef (Value *Mask)
 
template<typename IterTy >
static ValueSimplifyIntrinsic (Function *F, IterTy ArgBegin, IterTy ArgEnd, const Query &Q, unsigned MaxRecurse)
 
template<typename IterTy >
static ValueSimplifyCall (Value *V, IterTy ArgBegin, IterTy ArgEnd, const Query &Q, unsigned MaxRecurse)
 
static bool replaceAndRecursivelySimplifyImpl (Instruction *I, Value *SimpleV, const TargetLibraryInfo *TLI, const DominatorTree *DT, AssumptionCache *AC)
 Implementation of recursive simplification through an instruction's uses. More...
 

Macro Definition Documentation

#define DEBUG_TYPE   "instsimplify"

Definition at line 41 of file InstructionSimplify.cpp.

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

Enumeration Type Documentation

anonymous enum
Enumerator
RecursionLimit 

Definition at line 43 of file InstructionSimplify.cpp.

Function Documentation

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 647 of file InstructionSimplify.cpp.

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

Referenced by SimplifySubInst().

static Constant* computePointerICmp ( const DataLayout DL,
const TargetLibraryInfo TLI,
const DominatorTree DT,
CmpInst::Predicate  Pred,
const Instruction CxtI,
Value LHS,
Value RHS 
)
static
static Value* ExpandBinOp ( unsigned  Opcode,
Value LHS,
Value RHS,
unsigned  OpcToExpand,
const Query Q,
unsigned  MaxRecurse 
)
static

Simplify "A op (B op' C)" by distributing op over op', turning it into "(A op B) op' (A op C)".

Here "op" is given by Opcode and "op'" is given by OpcodeToExpand, while "A" corresponds to LHS and "B op' C" to RHS. Also performs the transform "(A op' B) op C" -> "(A op C) op' (B op C)". Returns the simplified value, or null if no simplification was performed.

Definition at line 143 of file InstructionSimplify.cpp.

References A, B, C, llvm::Instruction::isCommutative(), L, and llvm::SimplifyBinOp().

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

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 2031 of file InstructionSimplify.cpp.

References llvm::dyn_cast(), llvm::SelectInst::getCondition(), llvm::User::getOperand(), llvm::CmpInst::getPredicate(), llvm::CmpInst::getSwappedPredicate(), and SI.

Referenced by simplifyICmpWithMinMax().

static Type* GetCompareTy ( Value Op)
static
static Constant* getFalse ( Type Ty)
static

For a boolean type, or a vector of boolean type, return false, or a vector with every element false, as appropriate for the type.

Definition at line 79 of file InstructionSimplify.cpp.

References assert(), llvm::Constant::getNullValue(), llvm::Type::getScalarType(), and llvm::Type::isIntegerTy().

Referenced by reuseTableCompare(), SimplifyAndOfICmps(), simplifyAndOfICmpsWithSameOperands(), SimplifyFCmpInst(), simplifyICmpWithBinOp(), simplifyICmpWithMinMax(), simplifyICmpWithZero(), simplifyUnsignedRangeCheck(), and ThreadCmpOverSelect().

static Constant* getTrue ( Type Ty)
static
static bool IsIdempotent ( Intrinsic::ID  ID)
static

Definition at line 4162 of file InstructionSimplify.cpp.

References round().

Referenced by SimplifyIntrinsic().

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 94 of file InstructionSimplify.cpp.

References llvm::dyn_cast(), llvm::User::getOperand(), llvm::CmpInst::getPredicate(), and llvm::CmpInst::getSwappedPredicate().

Referenced by ThreadCmpOverSelect().

static bool isUndefShift ( Value Amount)
static

Returns true if a shift by Amount always yields undef.

Definition at line 1310 of file InstructionSimplify.cpp.

References C, llvm::dyn_cast(), E, llvm::Constant::getAggregateElement(), llvm::Value::getType(), llvm::Type::getVectorNumElements(), and I.

Referenced by SimplifyShift().

static bool maskIsAllZeroOrUndef ( Value Mask)
static

Definition at line 4234 of file InstructionSimplify.cpp.

References llvm::dyn_cast(), E, I, and llvm::BitmaskEnumDetail::Mask().

Referenced by SimplifyIntrinsic().

static bool replaceAndRecursivelySimplifyImpl ( Instruction I,
Value SimpleV,
const TargetLibraryInfo TLI,
const DominatorTree DT,
AssumptionCache AC 
)
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.

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

Definition at line 4570 of file InstructionSimplify.cpp.

References llvm::Instruction::eraseFromParent(), llvm::Module::getDataLayout(), llvm::Instruction::getModule(), llvm::Instruction::getParent(), I, llvm::SetVector< T, SmallVector< T, N >, SmallDenseSet< T, N > >::insert(), llvm::Instruction::isEHPad(), llvm::Instruction::mayHaveSideEffects(), llvm::Value::replaceAllUsesWith(), llvm::JumpTable::Simplified, llvm::SimplifyInstruction(), llvm::SetVector< T, SmallVector< T, N >, SmallDenseSet< T, N > >::size(), and llvm::Value::users().

Referenced by llvm::recursivelySimplifyInstruction(), and llvm::replaceAndRecursivelySimplify().

static Value* SimplifyAddInst ( Value Op0,
Value Op1,
bool  isNSW,
bool  isNUW,
const Query Q,
unsigned  MaxRecurse 
)
static
static Value * SimplifyAndInst ( Value Op0,
Value Op1,
const Query Q,
unsigned  MaxRecurse 
)
static
static Value* SimplifyAndOfICmps ( ICmpInst Op0,
ICmpInst Op1 
)
static
static Value* simplifyAndOfICmpsWithSameOperands ( ICmpInst Op0,
ICmpInst Op1 
)
static
static Value* SimplifyAShrInst ( Value Op0,
Value Op1,
bool  isExact,
const Query Q,
unsigned  MaxRecurse 
)
static
static Value* SimplifyAssociativeBinOp ( unsigned  Opc,
Value LHS,
Value RHS,
const Query Q,
unsigned  MaxRecurse 
)
static

Generic simplifications for associative binary operations.

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

Definition at line 202 of file InstructionSimplify.cpp.

References A, assert(), B, C, llvm::dyn_cast(), llvm::BinaryOperator::getOpcode(), llvm::User::getOperand(), llvm::Instruction::isAssociative(), llvm::Instruction::isCommutative(), and llvm::SimplifyBinOp().

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

static Value * SimplifyBinOp ( unsigned  Opcode,
Value LHS,
Value RHS,
const Query Q,
unsigned  MaxRecurse 
)
static
template<typename IterTy >
static Value* SimplifyCall ( Value V,
IterTy  ArgBegin,
IterTy  ArgEnd,
const Query Q,
unsigned  MaxRecurse 
)
static
static Value * SimplifyCastInst ( unsigned  CastOpc,
Value Op,
Type Ty,
const Query Q,
unsigned  MaxRecurse 
)
static
static Value * SimplifyCmpInst ( unsigned  Predicate,
Value LHS,
Value RHS,
const Query Q,
unsigned  MaxRecurse 
)
static

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

Definition at line 4147 of file InstructionSimplify.cpp.

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

Referenced by llvm::SimplifyCmpInst().

static Value* SimplifyDiv ( Instruction::BinaryOps  Opcode,
Value Op0,
Value Op1,
const Query Q,
unsigned  MaxRecurse 
)
static
static Value* SimplifyExtractElementInst ( Value Vec,
Value Idx,
const Query ,
unsigned   
)
static

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

If not, this returns null.

Definition at line 3936 of file InstructionSimplify.cpp.

References llvm::ConstantFoldExtractElementInstruction(), llvm::findScalarElement(), llvm::UndefValue::get(), llvm::Value::getType(), and llvm::Type::getVectorElementType().

Referenced by llvm::SimplifyExtractElementInst().

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

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

If not, this returns null.

Definition at line 3901 of file InstructionSimplify.cpp.

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

Referenced by llvm::SimplifyExtractValueInst().

static Value* SimplifyFAddInst ( Value Op0,
Value Op1,
FastMathFlags  FMF,
const Query Q,
unsigned  MaxRecurse 
)
static
static Value* SimplifyFCmpInst ( unsigned  Predicate,
Value LHS,
Value RHS,
FastMathFlags  FMF,
const Query Q,
unsigned  MaxRecurse 
)
static
static Value* SimplifyFDivInst ( Value Op0,
Value Op1,
FastMathFlags  FMF,
const Query Q,
unsigned   
)
static
static Value* SimplifyFMulInst ( Value Op0,
Value Op1,
FastMathFlags  FMF,
const Query Q,
unsigned  MaxRecurse 
)
static
static Value * SimplifyFPBinOp ( unsigned  Opcode,
Value LHS,
Value RHS,
const FastMathFlags FMF,
const Query Q,
unsigned  MaxRecurse 
)
static

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

If not, this returns null. In contrast to SimplifyBinOp, try to use FastMathFlag when folding the result. In case we don't need FastMathFlags, simply fall to SimplifyBinOp.

Definition at line 4112 of file InstructionSimplify.cpp.

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

Referenced by llvm::SimplifyFPBinOp().

static Value* SimplifyFRemInst ( Value Op0,
Value Op1,
FastMathFlags  FMF,
const Query ,
unsigned   
)
static
static Value* SimplifyFSubInst ( Value Op0,
Value Op1,
FastMathFlags  FMF,
const Query Q,
unsigned  MaxRecurse 
)
static
static Value* SimplifyGEPInst ( Type SrcTy,
ArrayRef< Value * >  Ops,
const Query Q,
unsigned   
)
static
static Value * SimplifyICmpInst ( unsigned  Predicate,
Value LHS,
Value RHS,
const Query Q,
unsigned  MaxRecurse 
)
static

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

If not, this returns null.

Definition at line 3033 of file InstructionSimplify.cpp.

References assert(), C, llvm::computeKnownBits(), computePointerICmp(), llvm::ConstantFoldCompareInstOperands(), llvm::dyn_cast(), llvm::ConstantInt::get(), llvm::APInt::getBitWidth(), llvm::ConstantExpr::getCast(), GetCompareTy(), llvm::getConstantRangeFromMetadata(), llvm::Type::getContext(), llvm::Value::getContext(), llvm::ConstantInt::getFalse(), llvm::ConstantExpr::getGetElementPtr(), llvm::ConstantExpr::getICmp(), llvm::ConstantExpr::getIntToPtr(), llvm::CmpInst::getInversePredicate(), llvm::Constant::getNullValue(), llvm::User::getOperand(), llvm::Type::getPrimitiveSizeInBits(), llvm::ICmpInst::getSignedPredicate(), llvm::CmpInst::getSwappedPredicate(), llvm::ConstantInt::getTrue(), llvm::ConstantExpr::getTrunc(), llvm::Value::getType(), 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::ICmpInst::isEquality(), llvm::CmpInst::isIntPredicate(), llvm::isKnownNonEqual(), llvm::Type::isPointerTy(), llvm::CmpInst::isTrueWhenEqual(), llvm_unreachable, llvm::PatternMatch::m_APInt(), llvm::ConstantRange::makeSatisfyingICmpRegion(), llvm::PatternMatch::match(), llvm::LLVMContext::MD_range, llvm::SimplifyICmpInst(), simplifyICmpOfBools(), simplifyICmpWithBinOp(), simplifyICmpWithConstant(), simplifyICmpWithMinMax(), simplifyICmpWithZero(), std::swap(), ThreadCmpOverPHI(), and ThreadCmpOverSelect().

Referenced by llvm::SimplifyICmpInst().

static Value* simplifyICmpOfBools ( CmpInst::Predicate  Pred,
Value LHS,
Value RHS,
const Query 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 2256 of file InstructionSimplify.cpp.

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

Referenced by SimplifyICmpInst().

static Value* simplifyICmpWithBinOp ( CmpInst::Predicate  Pred,
Value LHS,
Value RHS,
const Query Q,
unsigned  MaxRecurse 
)
static
static Value* simplifyICmpWithConstant ( CmpInst::Predicate  Pred,
Value LHS,
Value RHS 
)
static
static Value* simplifyICmpWithMinMax ( CmpInst::Predicate  Pred,
Value LHS,
Value RHS,
const Query Q,
unsigned  MaxRecurse 
)
static
static Value* simplifyICmpWithZero ( CmpInst::Predicate  Pred,
Value LHS,
Value RHS,
const Query Q 
)
static
static Value* SimplifyInsertValueInst ( Value Agg,
Value Val,
ArrayRef< unsigned Idxs,
const Query Q,
unsigned   
)
static

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

If not, this returns null.

Definition at line 3864 of file InstructionSimplify.cpp.

References llvm::ConstantFoldInsertValueInstruction(), llvm::Value::getType(), llvm::PatternMatch::m_Undef(), and llvm::PatternMatch::match().

Referenced by llvm::SimplifyInsertValueInst().

template<typename IterTy >
static Value* SimplifyIntrinsic ( Function F,
IterTy  ArgBegin,
IterTy  ArgEnd,
const Query Q,
unsigned  MaxRecurse 
)
static
static Value* SimplifyLShrInst ( Value Op0,
Value Op1,
bool  isExact,
const Query Q,
unsigned  MaxRecurse 
)
static

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

If not, this returns null.

Definition at line 1447 of file InstructionSimplify.cpp.

References llvm::PatternMatch::m_NUWShl(), llvm::PatternMatch::m_Specific(), llvm::PatternMatch::m_Value(), llvm::PatternMatch::match(), SimplifyRightShift(), and X.

Referenced by llvm::SimplifyLShrInst().

static Value* SimplifyMulInst ( Value Op0,
Value Op1,
const Query Q,
unsigned  MaxRecurse 
)
static
static Value * SimplifyOrInst ( Value Op0,
Value Op1,
const Query Q,
unsigned  MaxRecurse 
)
static
static Value* SimplifyOrOfICmps ( ICmpInst Op0,
ICmpInst Op1 
)
static
static Value* simplifyOrOfICmpsWithSameOperands ( ICmpInst Op0,
ICmpInst Op1 
)
static
static Value* SimplifyPHINode ( PHINode PN,
const Query Q 
)
static

See if we can fold the given phi. If not, returns null.

Definition at line 3967 of file InstructionSimplify.cpp.

References llvm::UndefValue::get(), llvm::Value::getType(), llvm::PHINode::incoming_values(), and ValueDominatesPHI().

Referenced by llvm::SCEVExpander::replaceCongruentIVs(), and llvm::SimplifyInstruction().

static Value* SimplifyRelativeLoad ( Constant Ptr,
Constant Offset,
const DataLayout DL 
)
static
static Value* SimplifyRem ( Instruction::BinaryOps  Opcode,
Value Op0,
Value Op1,
const Query Q,
unsigned  MaxRecurse 
)
static
static Value* SimplifyRightShift ( unsigned  Opcode,
Value Op0,
Value Op1,
bool  isExact,
const Query Q,
unsigned  MaxRecurse 
)
static

Given operands for an Shl, LShr or AShr, see if we can fold the result.

If not, this returns null.

Definition at line 1389 of file InstructionSimplify.cpp.

References llvm::computeKnownBits(), llvm::Constant::getNullValue(), llvm::Type::getScalarSizeInBits(), llvm::Value::getType(), llvm::PatternMatch::m_Undef(), llvm::PatternMatch::match(), and SimplifyShift().

Referenced by SimplifyAShrInst(), and SimplifyLShrInst().

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

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

If not, this returns null.

Definition at line 1086 of file InstructionSimplify.cpp.

References SimplifyDiv().

Referenced by llvm::SimplifySDivInst().

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 3557 of file InstructionSimplify.cpp.

References C, llvm::APInt::isPowerOf2(), llvm::PatternMatch::m_And(), llvm::PatternMatch::m_APInt(), llvm::PatternMatch::m_Or(), llvm::PatternMatch::m_Specific(), and llvm::PatternMatch::match().

Referenced by simplifySelectWithFakeICmpEq(), and simplifySelectWithICmpCond().

static Value* SimplifySelectInst ( Value CondVal,
Value TrueVal,
Value FalseVal,
const Query Q,
unsigned  MaxRecurse 
)
static

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

If not, this returns null.

Definition at line 3701 of file InstructionSimplify.cpp.

References simplifySelectWithICmpCond().

Referenced by llvm::SimplifySelectInst().

static Value* simplifySelectWithFakeICmpEq ( Value CmpLHS,
Value TrueVal,
Value FalseVal,
bool  TrueWhenUnset 
)
static
static Value* simplifySelectWithICmpCond ( Value CondVal,
Value TrueVal,
Value FalseVal,
const Query Q,
unsigned  MaxRecurse 
)
static
static Value* SimplifyShift ( unsigned  Opcode,
Value Op0,
Value Op1,
const Query Q,
unsigned  MaxRecurse 
)
static
static Value* SimplifyShlInst ( Value Op0,
Value Op1,
bool  isNSW,
bool  isNUW,
const Query Q,
unsigned  MaxRecurse 
)
static
static Value* SimplifySRemInst ( Value Op0,
Value Op1,
const Query Q,
unsigned  MaxRecurse 
)
static

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

If not, this returns null.

Definition at line 1237 of file InstructionSimplify.cpp.

References SimplifyRem().

Referenced by llvm::SimplifySRemInst().

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

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

If not, this returns null.

Definition at line 1104 of file InstructionSimplify.cpp.

References C, llvm::Constant::getNullValue(), llvm::Value::getType(), llvm::CmpInst::ICMP_ULT, SimplifyDiv(), and llvm::SimplifyICmpInst().

Referenced by llvm::SimplifyUDivInst().

static Value* simplifyUnsignedRangeCheck ( ICmpInst ZeroICmp,
ICmpInst UnsignedICmp,
bool  IsAnd 
)
static
static Value* SimplifyURemInst ( Value Op0,
Value Op1,
const Query Q,
unsigned  MaxRecurse 
)
static

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

If not, this returns null.

Definition at line 1255 of file InstructionSimplify.cpp.

References C, llvm::CmpInst::ICMP_ULT, llvm::SimplifyICmpInst(), and SimplifyRem().

Referenced by llvm::SimplifyURemInst().

static const Value* SimplifyWithOpReplaced ( Value V,
Value Op,
Value RepOp,
const Query Q,
unsigned  MaxRecurse 
)
static
static Value * SimplifyXorInst ( Value Op0,
Value Op1,
const Query Q,
unsigned  MaxRecurse 
)
static
STATISTIC ( NumExpand  ,
"Number of expansions"   
)
STATISTIC ( NumReassoc  ,
"Number of reassociations"   
)
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 603 of file InstructionSimplify.cpp.

References assert(), GEP, llvm::ConstantInt::get(), llvm::DataLayout::getIntPtrType(), llvm::APInt::getNullValue(), llvm::Operator::getOpcode(), llvm::Type::getScalarType(), llvm::ConstantVector::getSplat(), llvm::Value::getType(), llvm::Type::getVectorNumElements(), llvm::SmallPtrSetImpl< PtrType >::insert(), llvm::Type::isPointerTy(), llvm::Type::isVectorTy(), and Offset.

Referenced by computePointerDifference(), and computePointerICmp().

static Value* ThreadBinOpOverPHI ( unsigned  Opcode,
Value LHS,
Value RHS,
const Query 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 454 of file InstructionSimplify.cpp.

References assert(), llvm::PHINode::incoming_values(), llvm::SimplifyBinOp(), and ValueDominatesPHI().

Referenced by SimplifyAndInst(), SimplifyBinOp(), SimplifyDiv(), SimplifyMulInst(), SimplifyOrInst(), SimplifyRem(), and SimplifyShift().

static Value* ThreadBinOpOverSelect ( unsigned  Opcode,
Value LHS,
Value RHS,
const Query 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 301 of file InstructionSimplify.cpp.

References assert(), llvm::dyn_cast(), llvm::SelectInst::getFalseValue(), llvm::Instruction::getOpcode(), llvm::User::getOperand(), llvm::SelectInst::getTrueValue(), llvm::Instruction::isCommutative(), SI, llvm::JumpTable::Simplified, and llvm::SimplifyBinOp().

Referenced by SimplifyAndInst(), SimplifyBinOp(), SimplifyDiv(), SimplifyMulInst(), SimplifyOrInst(), SimplifyRem(), and SimplifyShift().

static Value* ThreadCmpOverPHI ( CmpInst::Predicate  Pred,
Value LHS,
Value RHS,
const Query 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 496 of file InstructionSimplify.cpp.

References assert(), llvm::CmpInst::getSwappedPredicate(), llvm::PHINode::incoming_values(), llvm::SimplifyCmpInst(), std::swap(), and ValueDominatesPHI().

Referenced by SimplifyFCmpInst(), and SimplifyICmpInst().

static Value* ThreadCmpOverSelect ( CmpInst::Predicate  Pred,
Value LHS,
Value RHS,
const Query Q,
unsigned  MaxRecurse 
)
static
static bool ValueDominatesPHI ( Value V,
PHINode P,
const DominatorTree DT 
)
static