LLVM  8.0.1
Macros | Functions
InstCombineCompares.cpp File Reference
#include "InstCombineInternal.h"
#include "llvm/ADT/APSInt.h"
#include "llvm/ADT/SetVector.h"
#include "llvm/ADT/Statistic.h"
#include "llvm/Analysis/ConstantFolding.h"
#include "llvm/Analysis/InstructionSimplify.h"
#include "llvm/Analysis/TargetLibraryInfo.h"
#include "llvm/IR/ConstantRange.h"
#include "llvm/IR/DataLayout.h"
#include "llvm/IR/GetElementPtrTypeIterator.h"
#include "llvm/IR/IntrinsicInst.h"
#include "llvm/IR/PatternMatch.h"
#include "llvm/Support/Debug.h"
#include "llvm/Support/KnownBits.h"
Include dependency graph for InstCombineCompares.cpp:

Go to the source code of this file.

Macros

#define DEBUG_TYPE   "instcombine"
 

Functions

 STATISTIC (NumSel, "Number of select opts")
 
static bool addWithOverflow (APInt &Result, const APInt &In1, const APInt &In2, bool IsSigned=false)
 Compute Result = In1+In2, returning true if the result overflowed for this type. More...
 
static bool subWithOverflow (APInt &Result, const APInt &In1, const APInt &In2, bool IsSigned=false)
 Compute Result = In1-In2, returning true if the result overflowed for this type. More...
 
static bool hasBranchUse (ICmpInst &I)
 Given an icmp instruction, return true if any use of this comparison is a branch on sign bit comparison. More...
 
static bool isSignBitCheck (ICmpInst::Predicate Pred, const APInt &RHS, bool &TrueIfSigned)
 Given an exploded icmp instruction, return true if the comparison only checks the sign bit. More...
 
static bool isSignTest (ICmpInst::Predicate &Pred, const APInt &C)
 Returns true if the exploded icmp can be expressed as a signed comparison to zero and updates the predicate accordingly. More...
 
static void computeSignedMinMaxValuesFromKnownBits (const KnownBits &Known, APInt &Min, APInt &Max)
 Given a signed integer type and a set of known zero and one bits, compute the maximum and minimum values that could have the specified known zero and known one bits, returning them in Min/Max. More...
 
static void computeUnsignedMinMaxValuesFromKnownBits (const KnownBits &Known, APInt &Min, APInt &Max)
 Given an unsigned integer type and a set of known zero and one bits, compute the maximum and minimum values that could have the specified known zero and known one bits, returning them in Min/Max. More...
 
static ValueevaluateGEPOffsetExpression (User *GEP, InstCombiner &IC, const DataLayout &DL)
 Return a value that can be used to compare the offset implied by a GEP to zero. More...
 
static bool canRewriteGEPAsOffset (Value *Start, Value *Base, const DataLayout &DL, SetVector< Value *> &Explored)
 Returns true if we can rewrite Start as a GEP with pointer Base and some integer offset. More...
 
static void setInsertionPoint (IRBuilder<> &Builder, Value *V, bool Before=true)
 
static ValuerewriteGEPAsOffset (Value *Start, Value *Base, const DataLayout &DL, SetVector< Value *> &Explored)
 Returns a re-written value of Start as an indexed GEP using Base as a pointer. More...
 
static std::pair< Value *, Value * > getAsConstantIndexedAddress (Value *V, const DataLayout &DL)
 Looks through GEPs, IntToPtrInsts and PtrToIntInsts in order to express the input Value as a constant indexed GEP. More...
 
static InstructiontransformToIndexedCompare (GEPOperator *GEPLHS, Value *RHS, ICmpInst::Predicate Cond, const DataLayout &DL)
 Converts (CMP GEPLHS, RHS) if this change would make RHS a constant. More...
 
static InstructionprocessUGT_ADDCST_ADD (ICmpInst &I, Value *A, Value *B, ConstantInt *CI2, ConstantInt *CI1, InstCombiner &IC)
 The caller has matched a pattern of the form: I = icmp ugt (add (add A, B), CI2), CI1 If this is of the form: sum = a + b if (sum+128 >u 255) Then replace it with llvm.sadd.with.overflow.i8. More...
 
static InstructionfoldICmpShlOne (ICmpInst &Cmp, Instruction *Shl, const APInt &C)
 Fold icmp (shl 1, Y), C. More...
 
static ValuefoldICmpWithLowBitMaskedVal (ICmpInst &I, InstCombiner::BuilderTy &Builder)
 Some comparisons can be simplified. More...
 
static ValuefoldICmpWithTruncSignExtendedVal (ICmpInst &I, InstCombiner::BuilderTy &Builder)
 Some comparisons can be simplified. More...
 
static InstructionfoldICmpWithMinMax (ICmpInst &Cmp)
 Fold icmp Pred min|max(X, Y), X. More...
 
static InstructionprocessUMulZExtIdiom (ICmpInst &I, Value *MulVal, Value *OtherVal, InstCombiner &IC)
 Recognize and process idiom involving test for multiplication overflow. More...
 
static APInt getDemandedBitsLHSMask (ICmpInst &I, unsigned BitWidth)
 When performing a comparison against a constant, it is possible that not all the bits in the LHS are demanded. More...
 
static bool swapMayExposeCSEOpportunities (const Value *Op0, const Value *Op1)
 Check if the order of Op0 and Op1 as operands in an ICmpInst should be swapped. More...
 
static bool isChainSelectCmpBranch (const SelectInst *SI)
 Return true when the instruction sequence within a block is select-cmp-br. More...
 
static ICmpInstcanonicalizeCmpWithConstant (ICmpInst &I)
 If we have an icmp le or icmp ge instruction with a constant operand, turn it into the appropriate icmp lt or icmp gt instruction. More...
 
static InstructioncanonicalizeICmpBool (ICmpInst &I, InstCombiner::BuilderTy &Builder)
 Integer compare with boolean values can always be turned into bitwise ops. More...
 
static InstructionfoldICmpWithHighBitMask (ICmpInst &Cmp, InstCombiner::BuilderTy &Builder)
 
static InstructionfoldVectorCmp (CmpInst &Cmp, InstCombiner::BuilderTy &Builder)
 
static InstructionfoldFCmpReciprocalAndZero (FCmpInst &I, Instruction *LHSI, Constant *RHSC)
 Fold (C / X) < 0.0 –> X < 0.0 if possible. Swap predicate if necessary. More...
 
static InstructionfoldFabsWithFcmpZero (FCmpInst &I)
 Optimize fabs(X) compared with zero. More...
 

Macro Definition Documentation

◆ DEBUG_TYPE

#define DEBUG_TYPE   "instcombine"

Definition at line 32 of file InstCombineCompares.cpp.

Function Documentation

◆ addWithOverflow()

static bool addWithOverflow ( APInt Result,
const APInt In1,
const APInt In2,
bool  IsSigned = false 
)
static

Compute Result = In1+In2, returning true if the result overflowed for this type.

Definition at line 40 of file InstCombineCompares.cpp.

References llvm::APInt::sadd_ov(), and llvm::APInt::uadd_ov().

Referenced by foldICmpShlOne().

◆ canonicalizeCmpWithConstant()

static ICmpInst* canonicalizeCmpWithConstant ( ICmpInst I)
static

◆ canonicalizeICmpBool()

static Instruction* canonicalizeICmpBool ( ICmpInst I,
InstCombiner::BuilderTy Builder 
)
static

◆ canRewriteGEPAsOffset()

static bool canRewriteGEPAsOffset ( Value Start,
Value Base,
const DataLayout DL,
SetVector< Value *> &  Explored 
)
static

Returns true if we can rewrite Start as a GEP with pointer Base and some integer offset.

The nodes that need to be re-written for this transformation will be added to Explored.

Definition at line 548 of file InstCombineCompares.cpp.

Referenced by transformToIndexedCompare().

◆ computeSignedMinMaxValuesFromKnownBits()

static void computeSignedMinMaxValuesFromKnownBits ( const KnownBits Known,
APInt Min,
APInt Max 
)
static

Given a signed integer type and a set of known zero and one bits, compute the maximum and minimum values that could have the specified known zero and known one bits, returning them in Min/Max.

TODO: Move to method on KnownBits struct?

Definition at line 131 of file InstCombineCompares.cpp.

References assert(), llvm::APInt::clearSignBit(), llvm::KnownBits::getBitWidth(), llvm::APInt::getBitWidth(), llvm::KnownBits::One, llvm::APInt::setSignBit(), and llvm::KnownBits::Zero.

Referenced by llvm::InstCombiner::replacedSelectWithOperand().

◆ computeUnsignedMinMaxValuesFromKnownBits()

static void computeUnsignedMinMaxValuesFromKnownBits ( const KnownBits Known,
APInt Min,
APInt Max 
)
static

◆ evaluateGEPOffsetExpression()

static Value* evaluateGEPOffsetExpression ( User GEP,
InstCombiner IC,
const DataLayout DL 
)
static

Return a value that can be used to compare the offset implied by a GEP to zero.

For example, if we have &A[i], we want to return 'i' for "icmp ne i, 0". Note that, in general, indices can be complex, and scales are involved. The above expression would also be legal to codegen as "icmp ne (i*4), 0" (assuming A is a pointer to i32). This latter form is less amenable to optimization though, and we are allowed to generate the first by knowing that pointer arithmetic doesn't overflow.

If we can't emit an optimized form for this expression, this returns null.

Definition at line 454 of file InstCombineCompares.cpp.

References llvm::InstCombiner::Builder, llvm::IRBuilder< T, Inserter >::CreateAdd(), llvm::IRBuilder< T, Inserter >::CreateIntCast(), llvm::IRBuilder< T, Inserter >::CreateTrunc(), llvm::dyn_cast(), llvm::gep_type_begin(), llvm::ConstantInt::get(), llvm::StructLayout::getElementOffset(), llvm::generic_gep_type_iterator< ItTy >::getIndexedType(), llvm::Type::getIntegerBitWidth(), llvm::DataLayout::getIntPtrType(), llvm::User::getNumOperands(), llvm::User::getOperand(), llvm::Type::getPrimitiveSizeInBits(), llvm::ConstantInt::getSExtValue(), llvm::DataLayout::getStructLayout(), llvm::generic_gep_type_iterator< ItTy >::getStructTypeOrNull(), llvm::Value::getType(), llvm::DataLayout::getTypeAllocSize(), llvm::ConstantInt::getZExtValue(), llvm::ConstantInt::isZero(), llvm::SignExtend64(), and Size.

Referenced by transformToIndexedCompare().

◆ foldFabsWithFcmpZero()

static Instruction* foldFabsWithFcmpZero ( FCmpInst I)
static

◆ foldFCmpReciprocalAndZero()

static Instruction* foldFCmpReciprocalAndZero ( FCmpInst I,
Instruction LHSI,
Constant RHSC 
)
static

◆ foldICmpShlOne()

static Instruction* foldICmpShlOne ( ICmpInst Cmp,
Instruction Shl,
const APInt C 
)
static

Fold icmp (shl 1, Y), C.

Definition at line 1837 of file InstCombineCompares.cpp.

References llvm::MCID::Add, addWithOverflow(), llvm::APInt::ashr(), assert(), llvm::MCID::Bitcast, llvm::Intrinsic::bswap, llvm::APInt::byteSwap(), C, llvm::APInt::countTrailingZeros(), llvm::SelectInst::Create(), llvm::Intrinsic::ctlz, llvm::Intrinsic::ctpop, llvm::Intrinsic::cttz, llvm::dyn_cast(), llvm::object::Equal, llvm::IntegerType::get(), llvm::ConstantInt::get(), llvm::VectorType::get(), llvm::Constant::getAllOnesValue(), llvm::CallBase::getArgOperand(), llvm::APInt::getBitWidth(), llvm::ConstantExpr::getCompare(), llvm::SelectInst::getCondition(), llvm::Value::getContext(), llvm::SelectInst::getFalseValue(), llvm::APInt::getHighBitsSet(), llvm::ConstantExpr::getICmp(), llvm::IntrinsicInst::getIntrinsicID(), llvm::APInt::getLimitedValue(), llvm::APInt::getLowBitsSet(), llvm::Value::getName(), llvm::ConstantExpr::getNeg(), llvm::ConstantExpr::getNot(), llvm::Constant::getNullValue(), llvm::APInt::getOneBitSet(), llvm::BinaryOperator::getOpcode(), llvm::User::getOperand(), llvm::Instruction::getParent(), llvm::CmpInst::getPredicate(), llvm::Constant::getSplatValue(), llvm::CastInst::getSrcTy(), llvm::ConstantExpr::getSub(), llvm::CmpInst::getSwappedPredicate(), llvm::SelectInst::getTrueValue(), llvm::Value::getType(), llvm::Type::getVectorNumElements(), llvm::ConstantExpr::getXor(), llvm::GlobalVariable::hasDefinitiveInitializer(), llvm::Instruction::hasNoSignedWrap(), llvm::Instruction::hasNoUnsignedWrap(), llvm::Value::hasOneUse(), I, 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::Constant::isAllOnesValue(), llvm::APInt::isAllOnesValue(), llvm::GlobalVariable::isConstant(), llvm::ICmpInst::isEquality(), llvm::Instruction::isExact(), llvm::Type::isIntegerTy(), llvm::Type::isIntOrIntVectorTy(), llvm::APInt::isMaxSignedValue(), llvm::APInt::isMaxValue(), llvm::APInt::isMinSignedValue(), llvm::APInt::isMinValue(), llvm::Constant::isNullValue(), llvm::APInt::isNullValue(), llvm::APInt::isOneValue(), llvm::APInt::isPowerOf2(), isSignBitCheck(), llvm::CmpInst::isSigned(), llvm::APInt::isSignMask(), isSignTest(), llvm::APInt::isSplat(), llvm::APInt::isStrictlyPositive(), llvm::CmpInst::isUnsigned(), llvm::Type::isVectorTy(), isVolatile(), llvm::ConstantInt::isZero(), llvm::CodeGenOpt::Less, LLVM_FALLTHROUGH, llvm_unreachable, llvm::SPII::Load, llvm::APInt::logBase2(), llvm::APInt::lshr(), llvm::PatternMatch::m_APInt(), llvm::PatternMatch::m_Constant(), llvm::PatternMatch::m_ConstantInt(), llvm::PatternMatch::m_ICmp(), llvm::PatternMatch::m_One(), llvm::PatternMatch::m_Select(), llvm::PatternMatch::m_Shl(), llvm::PatternMatch::m_ShuffleVector(), llvm::PatternMatch::m_Specific(), llvm::PatternMatch::m_Undef(), llvm::PatternMatch::m_Value(), llvm::ConstantRange::makeExactICmpRegion(), llvm::BitmaskEnumDetail::Mask(), llvm::PatternMatch::match(), llvm::APInt::negate(), llvm::APInt::sdiv(), llvm::MCID::Select, llvm::User::setOperand(), llvm::APInt::sgt(), llvm::APInt::shl(), SI, llvm::APInt::ssub_ov(), llvm::ConstantRange::subtract(), subWithOverflow(), llvm::Value::takeName(), llvm::APInt::trunc(), llvm::APInt::udiv(), llvm::APInt::ugt(), llvm::Upper, llvm::APInt::usub_ov(), X, and Y.

◆ foldICmpWithHighBitMask()

static Instruction* foldICmpWithHighBitMask ( ICmpInst Cmp,
InstCombiner::BuilderTy Builder 
)
static

◆ foldICmpWithLowBitMaskedVal()

static Value* foldICmpWithLowBitMaskedVal ( ICmpInst I,
InstCombiner::BuilderTy Builder 
)
static

Some comparisons can be simplified.

In this case, we are looking for comparisons that look like a check for a lossy truncation. Folds: icmp SrcPred (x & Mask), x to icmp DstPred x, Mask Where Mask is some pattern that produces all-ones in low bits: (-1 >> y) ((-1 << y) >> y) <- non-canonical, has extra uses ~(-1 << y) ((1 << y) + (-1)) <- non-canonical, has extra uses The Mask can be a constant, too. For some predicates, the operands are commutative. For others, x can only be on a specific side.

Definition at line 2937 of file InstCombineCompares.cpp.

References assert(), llvm::IRBuilder< T, Inserter >::CreateICmp(), llvm::User::getOperand(), llvm_unreachable, llvm::PatternMatch::m_Add(), llvm::PatternMatch::m_AllOnes(), llvm::PatternMatch::m_c_And(), llvm::PatternMatch::m_c_ICmp(), llvm::PatternMatch::m_CombineAnd(), llvm::PatternMatch::m_CombineOr(), llvm::PatternMatch::m_Constant(), llvm::PatternMatch::m_Deferred(), llvm::PatternMatch::m_LowBitMask(), llvm::PatternMatch::m_LShr(), llvm::PatternMatch::m_NonNegative(), llvm::PatternMatch::m_Not(), llvm::PatternMatch::m_One(), llvm::PatternMatch::m_Shl(), llvm::PatternMatch::m_Value(), llvm::PatternMatch::match(), X, and Y.

Referenced by foldICmpWithTruncSignExtendedVal().

◆ foldICmpWithMinMax()

static Instruction* foldICmpWithMinMax ( ICmpInst Cmp)
static

Fold icmp Pred min|max(X, Y), X.

Definition at line 3455 of file InstCombineCompares.cpp.

References llvm::AlwaysOverflows, assert(), B, C, llvm::computeOverflowForUnsignedAdd(), llvm::computeOverflowForUnsignedMul(), llvm::BinaryOperator::CreateNot(), D, llvm::dyn_cast(), llvm::UndefValue::get(), llvm::Constant::getAllOnesValue(), getBitWidth(), llvm::ConstantInt::getBitWidth(), llvm::ConstantExpr::getCast(), llvm::Type::getIntegerBitWidth(), llvm::ConstantExpr::getIntToPtr(), llvm::ConstantInt::getLimitedValue(), llvm::APInt::getLowBitsSet(), llvm::Value::getName(), llvm::Constant::getNullValue(), llvm::APInt::getOneBitSet(), llvm::CastInst::getOpcode(), llvm::User::getOperand(), llvm::Type::getPointerAddressSpace(), llvm::CmpInst::getPredicate(), llvm::Type::getPrimitiveSizeInBits(), llvm::CmpInst::getSwappedPredicate(), llvm::ConstantExpr::getTrunc(), llvm::ConstantInt::getType(), llvm::Value::getType(), llvm::ICmpInst::getUnsignedPredicate(), llvm::ConstantInt::getValue(), llvm::Value::hasOneUse(), I, 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::Instruction::isCommutative(), llvm::ICmpInst::isEquality(), llvm::APInt::isPowerOf2(), llvm::CmpInst::isSigned(), LLVM_FALLTHROUGH, llvm_unreachable, llvm::APInt::logBase2(), llvm::PatternMatch::m_And(), llvm::PatternMatch::m_AShr(), llvm::PatternMatch::m_BitReverse(), llvm::PatternMatch::m_BSwap(), llvm::PatternMatch::m_c_SMax(), llvm::PatternMatch::m_c_SMin(), llvm::PatternMatch::m_c_UMax(), llvm::PatternMatch::m_c_UMin(), llvm::PatternMatch::m_ConstantInt(), llvm::PatternMatch::m_LShr(), llvm::PatternMatch::m_One(), llvm::PatternMatch::m_OneUse(), llvm::PatternMatch::m_Shl(), llvm::PatternMatch::m_Specific(), llvm::PatternMatch::m_Trunc(), llvm::PatternMatch::m_Value(), llvm::PatternMatch::m_Xor(), llvm::PatternMatch::m_Zero(), llvm::PatternMatch::m_ZExt(), llvm::BitmaskEnumDetail::Mask(), llvm::PatternMatch::match(), NC, llvm::NeverOverflows, llvm::OCF_INVALID, llvm::OCF_SIGNED_ADD, llvm::OCF_SIGNED_MUL, llvm::OCF_SIGNED_SUB, llvm::OCF_UNSIGNED_ADD, llvm::OCF_UNSIGNED_MUL, llvm::OCF_UNSIGNED_SUB, OR, llvm::User::setOperand(), std::swap(), llvm::Value::takeName(), Y, and llvm::APInt::zext().

Referenced by llvm::InstCombiner::visitICmpInst().

◆ foldICmpWithTruncSignExtendedVal()

static Value* foldICmpWithTruncSignExtendedVal ( ICmpInst I,
InstCombiner::BuilderTy Builder 
)
static

Some comparisons can be simplified.

In this case, we are looking for comparisons that look like a check for a lossy signed truncation. Folds: (MaskedBits is a constant.) ((x << MaskedBits) a>> MaskedBits) SrcPred x Into: (add x, (1 << (KeptBits-1))) DstPred (1 << KeptBits) Where KeptBits = bitwidth(x) - MaskedBits

Definition at line 3030 of file InstCombineCompares.cpp.

References llvm::APInt::abs(), llvm::MCID::Add, assert(), B, C, llvm::APInt::countTrailingZeros(), llvm::IRBuilder< T, Inserter >::CreateAdd(), llvm::IRBuilder< T, Inserter >::CreateICmp(), D, llvm::dyn_cast(), foldICmpWithLowBitMaskedVal(), llvm::ConstantInt::get(), llvm::Constant::getAllOnesValue(), llvm::APInt::getBitWidth(), llvm::ConstantInt::getFalse(), llvm::APInt::getLowBitsSet(), llvm::ConstantExpr::getNeg(), llvm::Constant::getNullValue(), llvm::BinaryOperator::getOpcode(), llvm::User::getOperand(), llvm::CmpInst::getPredicate(), llvm::Type::getScalarSizeInBits(), llvm::ICmpInst::getSignedPredicate(), llvm::CmpInst::getSwappedPredicate(), llvm::ConstantInt::getTrue(), llvm::Value::getType(), llvm::ICmpInst::getUnsignedPredicate(), llvm::Instruction::hasNoSignedWrap(), llvm::Instruction::hasNoUnsignedWrap(), llvm::Value::hasOneUse(), 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::Instruction::isExact(), llvm::APInt::isMaxSignedValue(), llvm::APInt::isNegative(), llvm::APInt::isNullValue(), llvm::APInt::isOneValue(), llvm::CmpInst::isSigned(), llvm::APInt::isSignMask(), llvm::CmpInst::isUnsigned(), llvm::APInt::lshr(), llvm::PatternMatch::m_Add(), llvm::PatternMatch::m_AllOnes(), llvm::PatternMatch::m_APInt(), llvm::PatternMatch::m_AShr(), llvm::PatternMatch::m_c_Add(), llvm::PatternMatch::m_c_And(), llvm::PatternMatch::m_c_ICmp(), llvm::PatternMatch::m_Deferred(), llvm::PatternMatch::m_Neg(), llvm::PatternMatch::m_One(), llvm::PatternMatch::m_OneUse(), llvm::PatternMatch::m_Shl(), llvm::PatternMatch::m_Specific(), llvm::PatternMatch::m_Value(), llvm::BitmaskEnumDetail::Mask(), llvm::PatternMatch::match(), llvm::APInt::shl(), T1, llvm::APInt::uge(), llvm::APInt::ugt(), X, and Y.

◆ foldVectorCmp()

static Instruction* foldVectorCmp ( CmpInst Cmp,
InstCombiner::BuilderTy Builder 
)
static

◆ getAsConstantIndexedAddress()

static std::pair<Value *, Value *> getAsConstantIndexedAddress ( Value V,
const DataLayout DL 
)
static

◆ getDemandedBitsLHSMask()

static APInt getDemandedBitsLHSMask ( ICmpInst I,
unsigned  BitWidth 
)
static

◆ hasBranchUse()

static bool hasBranchUse ( ICmpInst I)
static

Given an icmp instruction, return true if any use of this comparison is a branch on sign bit comparison.

Definition at line 66 of file InstCombineCompares.cpp.

References llvm::Value::users().

Referenced by processUGT_ADDCST_ADD().

◆ isChainSelectCmpBranch()

static bool isChainSelectCmpBranch ( const SelectInst SI)
static

Return true when the instruction sequence within a block is select-cmp-br.

Definition at line 4247 of file InstCombineCompares.cpp.

References llvm::dyn_cast(), llvm::User::getOperand(), llvm::Instruction::getParent(), llvm::BasicBlock::getTerminator(), and SI.

Referenced by llvm::InstCombiner::replacedSelectWithOperand().

◆ isSignBitCheck()

static bool isSignBitCheck ( ICmpInst::Predicate  Pred,
const APInt RHS,
bool TrueIfSigned 
)
static

Given an exploded icmp instruction, return true if the comparison only checks the sign bit.

If it only checks the sign bit, set TrueIfSigned if the result of the comparison is true when the input value is signed.

Definition at line 76 of file InstCombineCompares.cpp.

References llvm::CmpInst::ICMP_SGT, llvm::CmpInst::ICMP_SLE, llvm::CmpInst::ICMP_SLT, llvm::CmpInst::ICMP_UGE, llvm::CmpInst::ICMP_UGT, llvm::APInt::isAllOnesValue(), llvm::APInt::isMaxSignedValue(), llvm::APInt::isNullValue(), and llvm::APInt::isSignMask().

Referenced by foldICmpShlOne(), getDemandedBitsLHSMask(), and processUGT_ADDCST_ADD().

◆ isSignTest()

static bool isSignTest ( ICmpInst::Predicate &  Pred,
const APInt C 
)
static

Returns true if the exploded icmp can be expressed as a signed comparison to zero and updates the predicate accordingly.

The signedness of the comparison is preserved. TODO: Refactor with decomposeBitTestICmp()?

Definition at line 105 of file InstCombineCompares.cpp.

References llvm::CmpInst::ICMP_SGE, llvm::CmpInst::ICMP_SGT, llvm::CmpInst::ICMP_SLE, llvm::CmpInst::ICMP_SLT, llvm::APInt::isAllOnesValue(), llvm::APInt::isNullValue(), llvm::APInt::isOneValue(), llvm::ICmpInst::isRelational(), and llvm::CmpInst::isSigned().

Referenced by foldICmpShlOne(), and processUGT_ADDCST_ADD().

◆ processUGT_ADDCST_ADD()

static Instruction* processUGT_ADDCST_ADD ( ICmpInst I,
Value A,
Value B,
ConstantInt CI2,
ConstantInt CI1,
InstCombiner IC 
)
static

The caller has matched a pattern of the form: I = icmp ugt (add (add A, B), CI2), CI1 If this is of the form: sum = a + b if (sum+128 >u 255) Then replace it with llvm.sadd.with.overflow.i8.

Definition at line 1235 of file InstCombineCompares.cpp.

References llvm::MCID::Add, assert(), B, llvm::InstCombiner::Builder, C, llvm::computeKnownBits(), llvm::InstCombiner::ComputeNumSignBits(), llvm::APInt::countTrailingZeros(), llvm::BinaryOperator::Create(), llvm::ExtractValueInst::Create(), llvm::ConstantRange::difference(), llvm::dyn_cast(), llvm::APInt::exactLogBase2(), F(), llvm::SelectPatternResult::Flavor, llvm::IntegerType::get(), llvm::ConstantInt::get(), llvm::VectorType::get(), llvm::Constant::getAllOnesValue(), llvm::ConstantInt::getBitWidth(), llvm::APInt::getBitWidth(), llvm::Value::getContext(), llvm::Intrinsic::getDeclaration(), llvm::ConstantInt::getFalse(), llvm::APInt::getHighBitsSet(), llvm::CmpInst::getInversePredicate(), llvm::APInt::getLowBitsSet(), llvm::Instruction::getModule(), llvm::Value::getName(), llvm::Constant::getNullValue(), llvm::ConstantExpr::getNUWShl(), llvm::BinaryOperator::getOpcode(), llvm::User::getOperand(), llvm::ConstantExpr::getOr(), llvm::Instruction::getParent(), llvm::CmpInst::getPredicate(), llvm::Type::getPrimitiveSizeInBits(), llvm::Type::getScalarSizeInBits(), llvm::ICmpInst::getSignedPredicate(), llvm::ConstantRange::getSingleElement(), llvm::BasicBlock::getSinglePredecessor(), llvm::CmpInst::getSwappedPredicate(), llvm::BasicBlock::getTerminator(), llvm::ConstantInt::getTrue(), llvm::Value::getType(), llvm::ICmpInst::getUnsignedPredicate(), llvm::ConstantInt::getValue(), llvm::Type::getVectorNumElements(), hasBranchUse(), llvm::GlobalVariable::hasDefinitiveInitializer(), llvm::Instruction::hasNoSignedWrap(), llvm::Value::hasOneUse(), I, llvm::CmpInst::ICMP_EQ, llvm::CmpInst::ICMP_NE, llvm::CmpInst::ICMP_SGE, llvm::CmpInst::ICMP_SGT, llvm::CmpInst::ICMP_SLT, llvm::CmpInst::ICMP_UGT, llvm::CmpInst::ICMP_ULE, llvm::CmpInst::ICMP_ULT, llvm::ConstantRange::intersectWith(), llvm::Instruction::isArithmeticShift(), llvm::GlobalVariable::isConstant(), llvm::ConstantRange::isEmptySet(), llvm::ICmpInst::isEquality(), llvm::isImpliedCondition(), llvm::isKnownPositive(), llvm::APInt::isMaxSignedValue(), llvm::APInt::isNegative(), llvm::APInt::isNullValue(), llvm::APInt::isOneValue(), llvm::APInt::isPowerOf2(), llvm::Instruction::isShift(), isSignBitCheck(), llvm::CmpInst::isSigned(), llvm::APInt::isSignMask(), isSignTest(), llvm::Type::isVectorTy(), llvm::APInt::lshr(), llvm::PatternMatch::m_Add(), llvm::PatternMatch::m_APInt(), llvm::PatternMatch::m_Br(), llvm::PatternMatch::m_ConstantInt(), llvm::PatternMatch::m_ICmp(), llvm::PatternMatch::m_LShr(), llvm::PatternMatch::m_One(), llvm::PatternMatch::m_OneUse(), llvm::PatternMatch::m_Or(), llvm::PatternMatch::m_PtrToInt(), llvm::PatternMatch::m_Signum(), llvm::PatternMatch::m_Specific(), llvm::PatternMatch::m_Trunc(), llvm::PatternMatch::m_Value(), llvm::PatternMatch::m_Xor(), llvm::PatternMatch::m_Zero(), llvm::ConstantRange::makeAllowedICmpRegion(), llvm::ConstantRange::makeExactICmpRegion(), llvm::PatternMatch::match(), llvm::matchSelectPattern(), llvm::KnownBits::One, P, llvm::InstCombiner::replaceInstUsesWith(), llvm::Intrinsic::sadd_with_overflow, llvm::IRBuilderBase::SetInsertPoint(), llvm::User::setOperand(), llvm::APInt::shl(), llvm::SPF_SMIN, llvm::SubOne(), llvm::Value::users(), llvm::RISCVFenceField::W, Y, llvm::KnownBits::Zero, and llvm::APInt::zext().

◆ processUMulZExtIdiom()

static Instruction* processUMulZExtIdiom ( ICmpInst I,
Value MulVal,
Value OtherVal,
InstCombiner IC 
)
static

Recognize and process idiom involving test for multiplication overflow.

The caller has matched a pattern of the form: I = cmp u (mul(zext A, zext B), V The function checks if this is a test for overflow and if so replaces multiplication with call to 'mul.with.overflow' intrinsic.

Parameters
ICompare instruction.
MulValResult of 'mult' instruction. It is one of the arguments of the compare instruction. Must be of integer type.
OtherValThe other argument of compare instruction.
Returns
Instruction which must replace the compare instruction, NULL if no replacement required.

Definition at line 3930 of file InstCombineCompares.cpp.

References llvm::InstCombineWorklist::Add(), assert(), B, llvm::InstCombiner::Builder, llvm::APInt::countLeadingZeros(), llvm::ExtractValueInst::Create(), llvm::IRBuilder< T, Inserter >::CreateAnd(), llvm::IRBuilder< T, Inserter >::CreateCall(), llvm::IRBuilder< T, Inserter >::CreateExtractValue(), llvm::BinaryOperator::CreateNot(), llvm::IRBuilder< T, Inserter >::CreateZExt(), llvm::dyn_cast(), llvm::APInt::eq(), F(), llvm::ConstantInt::getBitWidth(), llvm::APInt::getBitWidth(), llvm::Intrinsic::getDeclaration(), llvm::APInt::getMaxValue(), llvm::Instruction::getModule(), llvm::APInt::getOneBitSet(), llvm::BinaryOperator::getOpcode(), llvm::User::getOperand(), llvm::CmpInst::getPredicate(), llvm::Type::getPrimitiveSizeInBits(), llvm::Value::getType(), llvm::ConstantInt::getValue(), llvm::Value::hasNUsesOrMore(), llvm::CmpInst::ICMP_EQ, llvm::CmpInst::ICMP_NE, llvm::CmpInst::ICMP_UGE, llvm::CmpInst::ICMP_UGT, llvm::CmpInst::ICMP_ULE, llvm::CmpInst::ICMP_ULT, llvm::APInt::isPowerOf2(), llvm_unreachable, llvm::APInt::logBase2(), llvm::PatternMatch::m_And(), llvm::PatternMatch::m_ConstantInt(), llvm::PatternMatch::m_Value(), llvm::PatternMatch::match(), llvm::InstCombiner::replaceInstUsesWith(), llvm::IRBuilderBase::SetInsertPoint(), llvm::User::setOperand(), llvm::APInt::trunc(), llvm::Intrinsic::umul_with_overflow, llvm::Value::user_begin(), llvm::Value::user_end(), llvm::Value::users(), llvm::InstCombiner::Worklist, and llvm::APInt::zext().

Referenced by llvm::InstCombiner::visitICmpInst().

◆ rewriteGEPAsOffset()

static Value* rewriteGEPAsOffset ( Value Start,
Value Base,
const DataLayout DL,
SetVector< Value *> &  Explored 
)
static

Returns a re-written value of Start as an indexed GEP using Base as a pointer.

Definition at line 672 of file InstCombineCompares.cpp.

Referenced by transformToIndexedCompare().

◆ setInsertionPoint()

static void setInsertionPoint ( IRBuilder<> &  Builder,
Value V,
bool  Before = true 
)
static

◆ STATISTIC()

STATISTIC ( NumSel  ,
"Number of select opts"   
)

◆ subWithOverflow()

static bool subWithOverflow ( APInt Result,
const APInt In1,
const APInt In2,
bool  IsSigned = false 
)
static

Compute Result = In1-In2, returning true if the result overflowed for this type.

Definition at line 53 of file InstCombineCompares.cpp.

References llvm::APInt::ssub_ov(), and llvm::APInt::usub_ov().

Referenced by foldICmpShlOne().

◆ swapMayExposeCSEOpportunities()

static bool swapMayExposeCSEOpportunities ( const Value Op0,
const Value Op1 
)
static

Check if the order of Op0 and Op1 as operands in an ICmpInst should be swapped.

The decision is based on how many times these two operands are reused as subtract operands and their positions in those instructions. The rationale is that several architectures use the same instruction for both subtract and cmp. Thus, it is better if the order of those operands match.

Returns
true if Op0 and Op1 should be swapped.

Definition at line 4197 of file InstCombineCompares.cpp.

References llvm::Value::getType(), llvm::Type::isPointerTy(), llvm::PatternMatch::m_Specific(), llvm::PatternMatch::m_Sub(), llvm::PatternMatch::match(), and llvm::Value::users().

Referenced by llvm::InstCombiner::visitICmpInst().

◆ transformToIndexedCompare()

static Instruction* transformToIndexedCompare ( GEPOperator GEPLHS,
Value RHS,
ICmpInst::Predicate  Cond,
const DataLayout DL 
)
static

Converts (CMP GEPLHS, RHS) if this change would make RHS a constant.

We can look through PHIs, GEPs and casts in order to determine a common base between GEPLHS and RHS.

Definition at line 830 of file InstCombineCompares.cpp.

References llvm::APInt::ashr(), assert(), C, canRewriteGEPAsOffset(), llvm::APInt::countLeadingOnes(), llvm::APInt::countLeadingZeros(), llvm::APInt::countTrailingZeros(), llvm::EmitGEPOffset(), llvm::SmallVectorBase::empty(), evaluateGEPOffsetExpression(), llvm::ConstantInt::get(), getAsConstantIndexedAddress(), llvm::APInt::getBitWidth(), llvm::CmpInst::getInversePredicate(), llvm::APInt::getMaxValue(), llvm::Constant::getNullValue(), llvm::User::getNumOperands(), llvm::User::getOperand(), llvm::CmpInst::getPredicate(), llvm::Type::getPrimitiveSizeInBits(), llvm::APInt::getSignedMaxValue(), llvm::ICmpInst::getSignedPredicate(), llvm::CmpInst::getSwappedPredicate(), llvm::Value::getType(), llvm::GEPOperator::hasAllConstantIndices(), llvm::GEPOperator::hasAllZeroIndices(), llvm::Value::hasOneUse(), I, 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::APInt::isAllOnesValue(), llvm::ICmpInst::isEquality(), llvm::GEPOperator::isInBounds(), llvm::APInt::isNegative(), llvm::APInt::isNullValue(), llvm::APInt::isPowerOf2(), llvm::CmpInst::isSigned(), llvm::CmpInst::isTrueWhenEqual(), llvm::Intrinsic::lifetime_end, llvm::Intrinsic::lifetime_start, llvm::APInt::logBase2(), llvm::APInt::lshr(), llvm::CmpInst::makeCmpResultType(), llvm::Intrinsic::memcpy, llvm::Intrinsic::memmove, llvm::Intrinsic::memset, Other, llvm::SmallVectorImpl< T >::pop_back_val(), llvm::SmallVectorTemplateBase< T >::push_back(), rewriteGEPAsOffset(), llvm::APInt::sgt(), llvm::APInt::shl(), SI, llvm::SmallVectorBase::size(), llvm::Value::stripPointerCasts(), llvm::Value::uses(), and X.