LLVM: lib/Transforms/InstCombine/InstCombineCompares.cpp File Reference (original) (raw)

Go to the source code of this file.

Macros
#define DEBUG_TYPE "instcombine"
Enumerations
enum class OffsetKind { Invalid, Value, Select }
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.
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.
static bool hasBranchUse (ICmpInst &I)
Given an icmp instruction, return true if any use of this comparison is a branch on sign bit comparison.
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.
static bool canRewriteGEPAsOffset (Value *Start, Value *Base, GEPNoWrapFlags &NW, const DataLayout &DL, SetVector< Value * > &Explored)
Returns true if we can rewrite Start as a GEP with pointer Base and some integer offset.
static void setInsertionPoint (IRBuilder<> &Builder, Value *V, bool Before=true)
static Value * rewriteGEPAsOffset (Value *Start, Value *Base, GEPNoWrapFlags NW, const DataLayout &DL, SetVector< Value * > &Explored, InstCombiner &IC)
Returns a re-written value of Start as an indexed GEP using Base as a pointer.
static Instruction * transformToIndexedCompare (GEPOperator *GEPLHS, Value *RHS, CmpPredicate Cond, const DataLayout &DL, InstCombiner &IC)
Converts (CMP GEPLHS, RHS) if this change would make RHS a constant.
static Instruction * processUGT_ADDCST_ADD (ICmpInst &I, Value *A, Value *B, ConstantInt *CI2, ConstantInt *CI1, InstCombinerImpl &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.
static Value * foldICmpOrXorSubChain (ICmpInst &Cmp, BinaryOperator *Or, InstCombiner::BuilderTy &Builder)
Fold icmp eq/ne (or (xor/sub (X1, X2), xor/sub (X3, X4))), 0.
static Instruction * foldICmpShlLHSC (ICmpInst &Cmp, Instruction *Shl, const APInt &C)
Fold icmp (shl nuw C2, Y), C.
static Value * createLogicFromTable (const std::bitset< 4 > &Table, Value *Op0, Value *Op1, IRBuilderBase &Builder, bool HasOneUse)
static Instruction * foldCtpopPow2Test (ICmpInst &I, IntrinsicInst *CtpopLhs, const APInt &CRhs, InstCombiner::BuilderTy &Builder, const SimplifyQuery &Q)
static Instruction * foldICmpIntrinsicWithIntrinsic (ICmpInst &Cmp, InstCombiner::BuilderTy &Builder)
Fold an icmp with LLVM intrinsics.
static Instruction * foldICmpUSubSatOrUAddSatWithConstant (CmpPredicate Pred, SaturatingInst *II, const APInt &C, InstCombiner::BuilderTy &Builder)
static Instruction * foldICmpOfCmpIntrinsicWithConstant (CmpPredicate Pred, IntrinsicInst *I, const APInt &C, InstCombiner::BuilderTy &Builder)
static bool isMaskOrZero (const Value *V, bool Not, const SimplifyQuery &Q, unsigned Depth=0)
static Value * foldICmpWithLowBitMaskedVal (CmpPredicate Pred, Value *Op0, Value *Op1, const SimplifyQuery &Q, InstCombiner &IC)
Some comparisons can be simplified.
static Value * foldICmpWithTruncSignExtendedVal (ICmpInst &I, InstCombiner::BuilderTy &Builder)
Some comparisons can be simplified.
static Value * foldShiftIntoShiftInAnotherHandOfAndInICmp (ICmpInst &I, const SimplifyQuery SQ, InstCombiner::BuilderTy &Builder)
static Instruction * foldICmpXNegX (ICmpInst &I, InstCombiner::BuilderTy &Builder)
static Instruction * foldICmpAndXX (ICmpInst &I, const SimplifyQuery &Q, InstCombinerImpl &IC)
static Instruction * foldICmpOrXX (ICmpInst &I, const SimplifyQuery &Q, InstCombinerImpl &IC)
static Instruction * foldICmpXorXX (ICmpInst &I, const SimplifyQuery &Q, InstCombinerImpl &IC)
static bool isMultipleOf (Value *X, const APInt &C, const SimplifyQuery &Q)
Return true if X is a multiple of C.
static Instruction * foldICmpPow2Test (ICmpInst &I, InstCombiner::BuilderTy &Builder)
static void collectOffsetOp (Value *V, SmallVectorImpl< OffsetOp > &Offsets, bool AllowRecursion)
static Instruction * foldICmpEqualityWithOffset (ICmpInst &I, InstCombiner::BuilderTy &Builder, const SimplifyQuery &SQ)
Offset both sides of an equality icmp to see if we can save some instructions: icmp eq/ne X, Y -> icmp eq/ne X op Z, Y op Z.
static bool isNeutralValue (Instruction::BinaryOps BinaryOp, Value *RHS, bool IsSigned)
static Instruction * processUMulZExtIdiom (ICmpInst &I, Value *MulVal, const APInt *OtherVal, InstCombinerImpl &IC)
Recognize and process idiom involving test for multiplication overflow.
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.
static bool isChainSelectCmpBranch (const SelectInst *SI)
Return true when the instruction sequence within a block is select-cmp-br.
static ICmpInst * canonicalizeCmpWithConstant (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.
static Instruction * canonicalizeICmpBool (ICmpInst &I, InstCombiner::BuilderTy &Builder)
Integer compare with boolean values can always be turned into bitwise ops.
static Instruction * foldICmpWithHighBitMask (ICmpInst &Cmp, InstCombiner::BuilderTy &Builder)
static Instruction * foldVectorCmp (CmpInst &Cmp, InstCombiner::BuilderTy &Builder)
static Instruction * foldICmpOfUAddOv (ICmpInst &I)
static Instruction * foldICmpInvariantGroup (ICmpInst &I)
static Instruction * foldReductionIdiom (ICmpInst &I, InstCombiner::BuilderTy &Builder, const DataLayout &DL)
This function folds patterns produced by lowering of reduce idioms, such as llvm.vector.reduce.and which are lowered into instruction chains.
static Instruction * foldFCmpReciprocalAndZero (FCmpInst &I, Instruction *LHSI, Constant *RHSC)
Fold (C / X) < 0.0 --> X < 0.0 if possible. Swap predicate if necessary.
static Instruction * foldFCmpFpTrunc (FCmpInst &I, const Instruction &FPTrunc, const Constant &C)
static Instruction * foldFabsWithFcmpZero (FCmpInst &I, InstCombinerImpl &IC)
Optimize fabs(X) compared with zero.
static Instruction * foldSqrtWithFcmpZero (FCmpInst &I, InstCombinerImpl &IC)
Optimize sqrt(X) compared with zero.
static Instruction * foldFCmpFNegCommonOp (FCmpInst &I)
static Instruction * foldFCmpFSubIntoFCmp (FCmpInst &I, Instruction *LHSI, Constant *RHSC, InstCombinerImpl &CI)
static Instruction * foldFCmpWithFloorAndCeil (FCmpInst &I, InstCombinerImpl &IC)

DEBUG_TYPE

#define DEBUG_TYPE "instcombine"

OffsetOp

OffsetKind

addWithOverflow()

canonicalizeCmpWithConstant()

canonicalizeICmpBool()

Integer compare with boolean values can always be turned into bitwise ops.

Definition at line 7239 of file InstCombineCompares.cpp.

References A(), assert(), B(), llvm::BinaryOperator::CreateNot(), 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_unreachable, llvm::PatternMatch::m_One(), llvm::PatternMatch::m_Zero(), llvm::PatternMatch::match(), and std::swap().

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

canRewriteGEPAsOffset()

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 393 of file InstCombineCompares.cpp.

References llvm::SmallVectorTemplateCommon< T, typename >::back(), llvm::sampleprof::Base, llvm::SetVector< T, Vector, Set, N >::contains(), llvm::count_if(), DL, llvm::dyn_cast(), llvm::SmallVectorTemplateCommon< T, typename >::empty(), GEP, llvm::SetVector< T, Vector, Set, N >::insert(), llvm::GEPNoWrapFlags::intersectForOffsetAdd(), llvm::isa(), PHI, llvm::SmallVectorTemplateBase< T, bool >::pop_back(), llvm::SmallVectorTemplateBase< T, bool >::push_back(), llvm::SetVector< T, Vector, Set, N >::size(), and llvm::Value::uses().

Referenced by transformToIndexedCompare().

collectOffsetOp()

createLogicFromTable()

foldCtpopPow2Test()

Definition at line 3756 of file InstCombineCompares.cpp.

References llvm::SimplifyQuery::AC, assert(), llvm::computeKnownBits(), llvm::KnownBits::countMinPopulation(), llvm::SimplifyQuery::CxtI, llvm::SimplifyQuery::DL, llvm::SimplifyQuery::DT, llvm::CallBase::getArgOperand(), llvm::Constant::getIntegerValue(), llvm::IntrinsicInst::getIntrinsicID(), llvm::Constant::getNullValue(), llvm::Value::hasOneUse(), I, llvm::CmpInst::ICMP_EQ, llvm::CmpInst::ICMP_NE, llvm::CmpInst::ICMP_UGT, llvm::CmpInst::ICMP_ULT, and llvm::KnownBits::One.

Referenced by llvm::InstCombinerImpl::foldICmpIntrinsicWithConstant().

foldFabsWithFcmpZero()

Optimize fabs(X) compared with zero.

Definition at line 8384 of file InstCombineCompares.cpp.

References assert(), llvm::CallingConv::C, F, llvm::CmpInst::FCMP_OEQ, llvm::CmpInst::FCMP_OGE, llvm::CmpInst::FCMP_OGT, llvm::CmpInst::FCMP_OLE, llvm::CmpInst::FCMP_OLT, llvm::CmpInst::FCMP_ONE, llvm::CmpInst::FCMP_ORD, llvm::CmpInst::FCMP_UEQ, llvm::CmpInst::FCMP_UGE, llvm::CmpInst::FCMP_UGT, llvm::CmpInst::FCMP_ULE, llvm::CmpInst::FCMP_ULT, llvm::CmpInst::FCMP_UNE, llvm::CmpInst::FCMP_UNO, llvm::ConstantFP::getZero(), I, llvm_unreachable, llvm::PatternMatch::m_APFloat(), llvm::PatternMatch::m_FAbs(), llvm::PatternMatch::m_Value(), llvm::PatternMatch::match(), Mode, P, llvm::DenormalMode::PositiveZero, llvm::DenormalMode::PreserveSign, llvm::InstCombiner::replaceOperand(), and X.

Referenced by llvm::InstCombinerImpl::visitFCmpInst().

foldFCmpFNegCommonOp()

foldFCmpFpTrunc()

Definition at line 8299 of file InstCombineCompares.cpp.

References llvm::CallingConv::C, llvm::APFloat::convert(), llvm::CmpInst::FCMP_OGE, llvm::CmpInst::FCMP_OGT, llvm::CmpInst::FCMP_OLE, llvm::CmpInst::FCMP_OLT, llvm::CmpInst::FCMP_UGE, llvm::CmpInst::FCMP_UGT, llvm::CmpInst::FCMP_ULE, llvm::CmpInst::FCMP_ULT, llvm::Type::getFltSemantics(), llvm::User::getOperand(), llvm::Type::getScalarType(), llvm::Value::getType(), I, llvm::isFinite(), llvm::APFloat::isInfinity(), llvm::APFloat::isNaN(), llvm::PatternMatch::m_APFloat(), llvm::PatternMatch::match(), llvm::APFloat::next(), llvm::APFloatBase::rmNearestTiesToEven, and llvm::scalbn().

Referenced by llvm::InstCombinerImpl::visitFCmpInst().

foldFCmpFSubIntoFCmp()

Definition at line 8564 of file InstCombineCompares.cpp.

References llvm::CmpInst::FCMP_OEQ, llvm::CmpInst::FCMP_OGE, llvm::CmpInst::FCMP_OGT, llvm::CmpInst::FCMP_OLE, llvm::CmpInst::FCMP_OLT, llvm::CmpInst::FCMP_ONE, llvm::CmpInst::FCMP_UEQ, llvm::CmpInst::FCMP_UGE, llvm::CmpInst::FCMP_UGT, llvm::CmpInst::FCMP_ULE, llvm::CmpInst::FCMP_ULT, llvm::CmpInst::FCMP_UNE, llvm::Type::getFltSemantics(), llvm::DenormalMode::getIEEE(), llvm::User::getOperand(), llvm::Type::getScalarType(), llvm::InstCombiner::getSimplifyQuery(), llvm::Value::getType(), llvm::SimplifyQuery::getWithInstruction(), llvm::Instruction::hasNoInfs(), llvm::Instruction::hasNoNaNs(), I, llvm::isKnownNeverInfinity(), llvm::PatternMatch::m_AnyZeroFP(), llvm::PatternMatch::match(), llvm::InstCombiner::replaceOperand(), X, and Y.

Referenced by llvm::InstCombinerImpl::visitFCmpInst().

foldFCmpReciprocalAndZero()

Fold (C / X) < 0.0 --> X < 0.0 if possible. Swap predicate if necessary.

Definition at line 8245 of file InstCombineCompares.cpp.

References llvm::CallingConv::C, llvm::CmpInst::FCMP_OGE, llvm::CmpInst::FCMP_OGT, llvm::CmpInst::FCMP_OLE, llvm::CmpInst::FCMP_OLT, llvm::User::getOperand(), llvm::Instruction::hasNoInfs(), I, llvm::PatternMatch::m_AnyZeroFP(), llvm::PatternMatch::m_APFloat(), and llvm::PatternMatch::match().

Referenced by llvm::InstCombinerImpl::visitFCmpInst().

foldFCmpWithFloorAndCeil()

Definition at line 8613 of file InstCombineCompares.cpp.

References llvm::CmpInst::FCMP_OGE, llvm::CmpInst::FCMP_OGT, llvm::CmpInst::FCMP_OLE, llvm::CmpInst::FCMP_OLT, llvm::CmpInst::FCMP_ORD, llvm::CmpInst::FCMP_UGE, llvm::CmpInst::FCMP_UGT, llvm::CmpInst::FCMP_ULE, llvm::CmpInst::FCMP_ULT, llvm::CmpInst::FCMP_UNO, llvm::ConstantInt::getFalse(), llvm::ConstantInt::getTrue(), llvm::ConstantFP::getZero(), I, LHS, llvm::PatternMatch::m_Intrinsic(), llvm::PatternMatch::m_Specific(), llvm::PatternMatch::match(), llvm::InstCombiner::replaceInstUsesWith(), RHS, and std::swap().

Referenced by llvm::InstCombinerImpl::visitFCmpInst().

foldICmpAndXX()

Definition at line 4970 of file InstCombineCompares.cpp.

References A(), llvm::InstCombiner::Builder, llvm::InstCombiner::computeKnownBits(), llvm::IRBuilderBase::CreateAnd(), llvm::IRBuilderBase::CreateOr(), llvm::Constant::getAllOnesValue(), llvm::CmpInst::getFlippedStrictnessPredicate(), llvm::InstCombiner::getFreelyInverted(), llvm::Constant::getNullValue(), llvm::InstCombiner::getSimplifyQuery(), llvm::CmpInst::getSwappedPredicate(), llvm::ICmpInst::getUnsignedPredicate(), llvm::SimplifyQuery::getWithInstruction(), llvm::Value::hasOneUse(), I, llvm::CmpInst::ICMP_EQ, llvm::CmpInst::ICMP_NE, llvm::CmpInst::ICMP_SGT, llvm::CmpInst::ICMP_SLE, llvm::CmpInst::ICMP_UGE, llvm::CmpInst::ICMP_ULT, llvm::ICmpInst::isEquality(), llvm::isKnownNegative(), llvm::KnownBits::isNegative(), llvm::KnownBits::isNonNegative(), llvm::CmpInst::isSigned(), llvm::PatternMatch::m_c_And(), llvm::PatternMatch::m_ImmConstant(), llvm::PatternMatch::m_Specific(), llvm::PatternMatch::m_Value(), llvm::PatternMatch::match(), and std::swap().

Referenced by llvm::InstCombinerImpl::foldICmpBinOp().

foldICmpEqualityWithOffset()

Offset both sides of an equality icmp to see if we can save some instructions: icmp eq/ne X, Y -> icmp eq/ne X op Z, Y op Z.

Note: This operation should not introduce poison.

Definition at line 5945 of file InstCombineCompares.cpp.

References llvm::APInt::ashr(), assert(), collectOffsetOp(), llvm::dyn_cast(), llvm::Value::getType(), I, llvm::impliesPoison(), OffsetResult::invalid(), llvm::isa(), llvm::Type::isIntOrIntVectorTy(), llvm::APInt::lshr(), llvm::PatternMatch::m_APInt(), llvm::PatternMatch::m_ImmConstant(), llvm::PatternMatch::m_NSWShl(), llvm::PatternMatch::m_NUWShl(), llvm::PatternMatch::m_Specific(), llvm::PatternMatch::m_Value(), llvm::PatternMatch::match(), RHS, OffsetResult::select(), llvm::APInt::shl(), llvm::simplifyBinOp(), and OffsetResult::value().

Referenced by llvm::InstCombinerImpl::foldICmpEquality().

foldICmpIntrinsicWithIntrinsic()

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

foldICmpInvariantGroup()

foldICmpOfCmpIntrinsicWithConstant()

Definition at line 4129 of file InstCombineCompares.cpp.

References llvm::CallingConv::C, llvm::ICmpInst::getSignedPredicate(), I, llvm::CmpInst::ICMP_EQ, llvm::CmpInst::ICMP_NE, llvm::CmpInst::ICMP_SGT, llvm::CmpInst::ICMP_SLT, llvm::CmpInst::ICMP_UGE, llvm::CmpInst::ICMP_UGT, llvm::CmpInst::ICMP_ULE, llvm::CmpInst::ICMP_ULT, LHS, and RHS.

Referenced by llvm::InstCombinerImpl::foldICmpIntrinsicWithConstant().

foldICmpOfUAddOv()

Definition at line 7431 of file InstCombineCompares.cpp.

References A(), B(), llvm::cast(), llvm::ExtractValueInst::Create(), I, llvm::CmpInst::ICMP_EQ, llvm::CmpInst::ICMP_NE, llvm::CmpInst::ICMP_UGT, llvm::CmpInst::ICMP_ULT, llvm::PatternMatch::m_AllOnes(), llvm::PatternMatch::m_ExtractValue(), llvm::PatternMatch::m_Intrinsic(), llvm::PatternMatch::m_One(), llvm::PatternMatch::m_Value(), llvm::MIPatternMatch::m_ZeroInt(), and llvm::PatternMatch::match().

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

foldICmpOrXorSubChain()

Fold icmp eq/ne (or (xor/sub (X1, X2), xor/sub (X3, X4))), 0.

Definition at line 2022 of file InstCombineCompares.cpp.

References llvm::SmallVectorImpl< T >::emplace_back(), llvm::SmallVectorTemplateCommon< T, typename >::empty(), llvm::CmpInst::ICMP_EQ, llvm::MIPatternMatch::m_OneUse(), llvm::PatternMatch::m_Or(), llvm::PatternMatch::m_Sub(), llvm::PatternMatch::m_Value(), llvm::PatternMatch::m_Xor(), llvm::PatternMatch::match(), llvm::SmallVectorImpl< T >::pop_back_val(), llvm::SmallVectorTemplateBase< T, bool >::push_back(), llvm::SmallVectorTemplateCommon< T, typename >::rbegin(), and llvm::SmallVectorTemplateCommon< T, typename >::rend().

Referenced by llvm::InstCombinerImpl::foldICmpOrConstant().

foldICmpOrXX()

Definition at line 5032 of file InstCombineCompares.cpp.

References A(), llvm::InstCombiner::Builder, llvm::IRBuilderBase::CreateAnd(), llvm::IRBuilderBase::CreateOr(), llvm::Constant::getAllOnesValue(), llvm::InstCombiner::getFreelyInverted(), llvm::Constant::getNullValue(), llvm::CmpInst::getSwappedPredicate(), llvm::Value::hasOneUse(), I, llvm::CmpInst::ICMP_EQ, llvm::CmpInst::ICMP_NE, llvm::CmpInst::ICMP_UGT, llvm::CmpInst::ICMP_ULE, llvm::isa(), llvm::ICmpInst::isEquality(), llvm::PatternMatch::m_c_Or(), llvm::PatternMatch::m_Specific(), llvm::PatternMatch::m_Value(), llvm::PatternMatch::match(), and std::swap().

Referenced by llvm::InstCombinerImpl::foldICmpBinOp().

foldICmpPow2Test()

Definition at line 5824 of file InstCombineCompares.cpp.

References A(), I, llvm::CmpInst::ICMP_EQ, llvm::CmpInst::ICMP_UGE, llvm::CmpInst::ICMP_UGT, llvm::CmpInst::ICMP_ULE, llvm::CmpInst::ICMP_ULT, llvm::CmpInst::isUnsigned(), llvm::PatternMatch::m_Add(), llvm::PatternMatch::m_AllOnes(), llvm::PatternMatch::m_c_And(), llvm::PatternMatch::m_c_Xor(), llvm::PatternMatch::m_Deferred(), llvm::MIPatternMatch::m_Neg(), llvm::MIPatternMatch::m_OneUse(), llvm::PatternMatch::m_Specific(), llvm::PatternMatch::m_Value(), llvm::MIPatternMatch::m_ZeroInt(), and llvm::PatternMatch::match().

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

foldICmpShlLHSC()

Fold icmp (shl nuw C2, Y), C.

Definition at line 2273 of file InstCombineCompares.cpp.

References llvm::CallingConv::C, llvm::Value::getType(), llvm::CmpInst::ICMP_EQ, llvm::CmpInst::ICMP_NE, llvm::CmpInst::ICMP_SGT, llvm::CmpInst::ICMP_SLT, llvm::CmpInst::ICMP_UGE, llvm::CmpInst::ICMP_UGT, llvm::CmpInst::ICMP_ULE, llvm::CmpInst::ICMP_ULT, llvm::APInt::isOne(), llvm::APInt::isPowerOf2(), llvm::APInt::isZero(), llvm::APInt::logBase2(), llvm::PatternMatch::m_APInt(), llvm::PatternMatch::m_NUWShl(), llvm::PatternMatch::m_Value(), llvm::PatternMatch::match(), llvm::APInt::udivrem(), llvm::APInt::ugt(), and Y.

Referenced by llvm::InstCombinerImpl::foldICmpShlConstant().

foldICmpUSubSatOrUAddSatWithConstant()

Definition at line 4044 of file InstCombineCompares.cpp.

References llvm::ConstantRange::add(), llvm::CallingConv::C, llvm::ICmpInst::compare(), llvm::ConstantRange::exactIntersectWith(), llvm::ConstantRange::exactUnionWith(), llvm::APInt::getAllOnes(), llvm::Value::getType(), llvm::APInt::getZero(), II, llvm::ConstantRange::inverse(), llvm_unreachable, llvm::PatternMatch::m_APInt(), llvm::ConstantRange::makeExactICmpRegion(), llvm::ConstantRange::makeExactNoWrapRegion(), llvm::PatternMatch::match(), and llvm::ConstantRange::sub().

Referenced by llvm::InstCombinerImpl::foldICmpIntrinsicWithConstant().

foldICmpWithHighBitMask()

Definition at line 7318 of file InstCombineCompares.cpp.

References llvm::CmpInst::Create(), llvm::Constant::getNullValue(), llvm::Value::getType(), 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::PatternMatch::m_Add(), llvm::PatternMatch::m_AllOnes(), llvm::PatternMatch::m_c_ICmp(), llvm::PatternMatch::m_CombineOr(), llvm::MIPatternMatch::m_Not(), llvm::PatternMatch::m_One(), llvm::MIPatternMatch::m_OneUse(), llvm::PatternMatch::m_Shl(), llvm::PatternMatch::m_Value(), llvm::PatternMatch::match(), X, and Y.

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

foldICmpWithLowBitMaskedVal()

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 icmp SrcPred (x & ~Mask), ~Mask to icmp DstPred x, ~Mask icmp eq/ne (x & ~Mask), 0 to icmp DstPred x, Mask icmp eq/ne (~x | Mask), -1 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 4493 of file InstCombineCompares.cpp.

References llvm::InstCombiner::Builder, Check, llvm::IRBuilderBase::CreateICmp(), llvm::InstCombiner::getFreelyInverted(), 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::isKnownNonNegative(), llvm::isKnownNonZero(), isMaskOrZero(), llvm::CmpInst::isSigned(), llvm::PatternMatch::m_AllOnes(), llvm::PatternMatch::m_And(), llvm::PatternMatch::m_c_And(), llvm::PatternMatch::m_NonNegative(), llvm::MIPatternMatch::m_OneUse(), llvm::PatternMatch::m_Or(), llvm::PatternMatch::m_Specific(), llvm::PatternMatch::m_Value(), llvm::PatternMatch::m_Zero(), llvm::PatternMatch::match(), std::swap(), and X.

Referenced by llvm::InstCombinerImpl::foldICmpCommutative().

foldICmpWithTruncSignExtendedVal()

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 4623 of file InstCombineCompares.cpp.

References assert(), llvm::BitWidth, I, llvm::CmpInst::ICMP_EQ, llvm::CmpInst::ICMP_NE, llvm::CmpInst::ICMP_UGE, llvm::CmpInst::ICMP_ULT, llvm::APInt::isPowerOf2(), llvm::APInt::lshr(), llvm::PatternMatch::m_APInt(), llvm::PatternMatch::m_AShr(), llvm::PatternMatch::m_c_ICmp(), llvm::PatternMatch::m_Deferred(), llvm::MIPatternMatch::m_OneUse(), llvm::PatternMatch::m_Shl(), llvm::PatternMatch::m_Value(), llvm::PatternMatch::match(), llvm::APInt::shl(), T1, llvm::APInt::ugt(), llvm::APInt::ult(), and X.

Referenced by llvm::InstCombinerImpl::foldICmpBinOp().

foldICmpXNegX()

Definition at line 4938 of file InstCombineCompares.cpp.

References llvm::BitWidth, llvm::CmpInst::Create(), llvm::Constant::getNullValue(), llvm::APInt::getSignedMaxValue(), llvm::ICmpInst::getSignedPredicate(), llvm::CmpInst::getSwappedPredicate(), I, llvm::ICmpInst::isEquality(), llvm::CmpInst::isSigned(), llvm::CmpInst::isUnsigned(), llvm::PatternMatch::m_c_ICmp(), llvm::PatternMatch::m_Deferred(), llvm::MIPatternMatch::m_Neg(), llvm::PatternMatch::m_NSWNeg(), llvm::MIPatternMatch::m_OneUse(), llvm::PatternMatch::m_Value(), llvm::PatternMatch::match(), and X.

Referenced by llvm::InstCombinerImpl::foldICmpBinOp().

foldICmpXorXX()

Definition at line 5067 of file InstCombineCompares.cpp.

References A(), llvm::Constant::getNullValue(), llvm::CmpInst::getStrictPredicate(), llvm::CmpInst::getSwappedPredicate(), llvm::Value::getType(), I, llvm::CmpInst::ICMP_SGE, llvm::CmpInst::ICMP_SGT, llvm::CmpInst::ICMP_SLT, llvm::CmpInst::ICMP_UGT, llvm::CmpInst::ICMP_ULT, llvm::isKnownNonZero(), llvm::PatternMatch::m_c_Xor(), llvm::PatternMatch::m_Negative(), llvm::PatternMatch::m_Specific(), llvm::PatternMatch::m_Value(), llvm::PatternMatch::match(), and std::swap().

Referenced by llvm::InstCombinerImpl::foldICmpBinOp().

foldReductionIdiom()

This function folds patterns produced by lowering of reduce idioms, such as llvm.vector.reduce.and which are lowered into instruction chains.

This code attempts to generate fewer number of scalar comparisons instead of vector comparisons when possible.

vec_ne = icmp ne <8 x i8> lhs, rhs scalar_ne = bitcast <8 x i1> vec_ne to i8 res = icmp i8 scalar_ne, 0

into

lhs.scalar = bitcast <8 x i8> lhs to i64 rhs.scalar = bitcast <8 x i8> rhs to i64 res = icmp i64 lhs.scalar, rhs.scalar

for in {ne, eq}.

Definition at line 7480 of file InstCombineCompares.cpp.

References llvm::CmpInst::Create(), DL, llvm::dyn_cast(), I, llvm::CmpInst::ICMP_NE, llvm::ICmpInst::isEquality(), LHS, llvm::PatternMatch::m_BitCast(), llvm::PatternMatch::m_ICmp(), llvm::MIPatternMatch::m_OneUse(), llvm::PatternMatch::m_Value(), llvm::PatternMatch::m_Zero(), llvm::PatternMatch::match(), and RHS.

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

foldShiftIntoShiftInAnotherHandOfAndInICmp()

Definition at line 4697 of file InstCombineCompares.cpp.

References assert(), llvm::CallingConv::C, llvm::computeKnownBits(), llvm::ConstantFoldCastOperand(), llvm::KnownBits::countMinLeadingZeros(), llvm::SimplifyQuery::DL, llvm::dyn_cast(), llvm::dyn_cast_or_null(), llvm::APInt::getAllOnes(), llvm::KnownBits::getBitWidth(), llvm::Constant::getNullValue(), llvm::Instruction::getOpcode(), llvm::User::getOperand(), llvm::Type::getScalarSizeInBits(), llvm::Value::getType(), llvm::Constant::getUniqueInteger(), llvm::SimplifyQuery::getWithInstruction(), llvm::Value::hasOneUse(), I, llvm::CmpInst::ICMP_ULT, llvm::isa(), llvm::Constant::isNullValue(), llvm::Type::isVectorTy(), llvm::PatternMatch::m_BinOp(), llvm::PatternMatch::m_c_And(), llvm::PatternMatch::m_CombineAnd(), llvm::PatternMatch::m_Instruction(), llvm::PatternMatch::m_LogicalShift(), llvm::PatternMatch::m_LShr(), llvm::MIPatternMatch::m_OneUse(), llvm::PatternMatch::m_SpecificInt_ICMP(), llvm::PatternMatch::m_TruncOrSelf(), llvm::PatternMatch::m_Value(), llvm::PatternMatch::m_Zero(), llvm::PatternMatch::m_ZExtOrSelf(), llvm::PatternMatch::match(), llvm::simplifyAddInst(), std::swap(), T1, llvm::APInt::ule(), llvm::APInt::ult(), X, and Y.

Referenced by llvm::InstCombinerImpl::foldICmpBinOp().

foldSqrtWithFcmpZero()

Optimize sqrt(X) compared with zero.

Definition at line 8485 of file InstCombineCompares.cpp.

References llvm::cast(), llvm::CmpInst::FCMP_OEQ, llvm::CmpInst::FCMP_OGE, llvm::CmpInst::FCMP_OGT, llvm::CmpInst::FCMP_OLE, llvm::CmpInst::FCMP_OLT, llvm::CmpInst::FCMP_ONE, llvm::CmpInst::FCMP_ORD, llvm::CmpInst::FCMP_UEQ, llvm::CmpInst::FCMP_UGE, llvm::CmpInst::FCMP_UGT, llvm::CmpInst::FCMP_ULE, llvm::CmpInst::FCMP_ULT, llvm::CmpInst::FCMP_UNE, llvm::CmpInst::FCMP_UNO, I, llvm_unreachable, llvm::PatternMatch::m_PosZeroFP(), llvm::PatternMatch::m_Sqrt(), llvm::PatternMatch::m_Value(), llvm::PatternMatch::match(), P, llvm::InstCombiner::replaceOperand(), and X.

Referenced by llvm::InstCombinerImpl::visitFCmpInst().

foldVectorCmp()

Definition at line 7361 of file InstCombineCompares.cpp.

References llvm::CallingConv::C, llvm::cast(), llvm::CallInst::Create(), llvm::dyn_cast(), F, llvm::Intrinsic::getOrInsertDeclaration(), llvm::ConstantVector::getSplat(), llvm::Value::getType(), I, llvm::isSplatValue(), LHS, llvm::PatternMatch::m_Constant(), llvm::MIPatternMatch::m_OneUse(), llvm::PatternMatch::m_Shuffle(), llvm::PatternMatch::m_Undef(), llvm::PatternMatch::m_Value(), llvm::PatternMatch::m_VecReverse(), llvm::PatternMatch::match(), RHS, X, and Y.

Referenced by llvm::InstCombinerImpl::visitFCmpInst(), and llvm::InstCombinerImpl::visitICmpInst().

getDemandedBitsLHSMask()

When performing a comparison against a constant, it is possible that not all the bits in the LHS are demanded.

This helper method computes the mask that IS demanded.

Definition at line 6752 of file InstCombineCompares.cpp.

References llvm::BitWidth, llvm::APInt::getAllOnes(), llvm::APInt::getBitsSetFrom(), llvm::APInt::getSignMask(), I, llvm::CmpInst::ICMP_UGT, llvm::CmpInst::ICMP_ULT, llvm::isSignBitCheck(), llvm::PatternMatch::m_APInt(), llvm::PatternMatch::match(), and RHS.

Referenced by llvm::InstCombinerImpl::foldICmpUsingKnownBits().

hasBranchUse()

isChainSelectCmpBranch()

isMaskOrZero()

Definition at line 4386 of file InstCombineCompares.cpp.

References llvm::SimplifyQuery::AC, llvm::SimplifyQuery::CxtI, llvm::Depth, llvm::SimplifyQuery::DL, llvm::SimplifyQuery::DT, llvm::dyn_cast(), I, II, llvm::isKnownToBeAPowerOfTwo(), isMaskOrZero(), llvm::PatternMatch::m_Add(), llvm::PatternMatch::m_AllOnes(), llvm::PatternMatch::m_c_Xor(), llvm::PatternMatch::m_Deferred(), llvm::PatternMatch::m_LowBitMaskOrZero(), llvm::MIPatternMatch::m_Neg(), llvm::PatternMatch::m_NegatedPower2OrZero(), llvm::MIPatternMatch::m_Not(), llvm::PatternMatch::m_Value(), llvm::PatternMatch::m_Zero(), llvm::PatternMatch::match(), llvm::MaxAnalysisRecursionDepth, and X.

Referenced by foldICmpWithLowBitMaskedVal(), and isMaskOrZero().

isMultipleOf()

isNeutralValue()

isSignTest()

processUGT_ADDCST_ADD()

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 1060 of file InstCombineCompares.cpp.

References A(), llvm::Add, B(), llvm::InstCombiner::Builder, Call, llvm::cast(), llvm::InstCombiner::ComputeMaxSignificantBits(), llvm::APInt::countr_zero(), llvm::ExtractValueInst::Create(), llvm::dyn_cast(), llvm::InstCombinerImpl::eraseInstFromFunction(), F, llvm::IntegerType::get(), llvm::ConstantInt::getBitWidth(), llvm::Value::getContext(), llvm::APInt::getLowBitsSet(), llvm::User::getOperand(), llvm::Intrinsic::getOrInsertDeclaration(), llvm::Type::getPrimitiveSizeInBits(), llvm::Value::getType(), llvm::ConstantInt::getValue(), llvm::Value::hasOneUse(), I, llvm::APInt::isPowerOf2(), llvm::InstCombiner::replaceInstUsesWith(), llvm::IRBuilderBase::SetInsertPoint(), and llvm::Value::users().

Referenced by llvm::InstCombinerImpl::foldICmpWithConstant().

processUMulZExtIdiom()

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

I Compare instruction.
MulVal Result of 'mult' instruction. It is one of the arguments of the compare instruction. Must be of integer type.
OtherVal The other argument of compare instruction.

Returns

Instruction which must replace the compare instruction, NULL if no replacement required.

Definition at line 6605 of file InstCombineCompares.cpp.

References A(), llvm::InstCombiner::addToWorklist(), assert(), B(), llvm::InstCombiner::Builder, Call, llvm::cast(), llvm::APInt::countl_zero(), llvm::ExtractValueInst::Create(), llvm::BinaryOperator::CreateNot(), llvm::dyn_cast(), llvm::APInt::eq(), llvm::APInt::getBitWidth(), llvm::APInt::getMaxValue(), llvm::APInt::getOneBitSet(), llvm::Type::getPrimitiveSizeInBits(), llvm::Value::getType(), llvm::ConstantInt::getValue(), llvm::Value::hasNUsesOrMore(), I, llvm::CmpInst::ICMP_UGT, llvm::CmpInst::ICMP_ULT, llvm::isa(), LHS, llvm_unreachable, llvm::make_early_inc_range(), Mul, llvm::InstCombiner::replaceInstUsesWith(), RHS, llvm::IRBuilderBase::SetInsertPoint(), llvm::User::setOperand(), llvm::APInt::trunc(), llvm::Value::users(), and llvm::APInt::zext().

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

rewriteGEPAsOffset()

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

Definition at line 507 of file InstCombineCompares.cpp.

References llvm::PHINode::addIncoming(), llvm::InstCombiner::addToWorklist(), llvm::sampleprof::Base, llvm::cast(), llvm::DenseMapBase< DerivedT, KeyT, ValueT, KeyInfoT, BucketT >::contains(), llvm::PHINode::Create(), DL, llvm::dyn_cast(), E(), llvm::emitGEPOffset(), llvm::DenseMapBase< DerivedT, KeyT, ValueT, KeyInfoT, BucketT >::end(), llvm::DenseMapBase< DerivedT, KeyT, ValueT, KeyInfoT, BucketT >::find(), GEP, llvm::IntegerType::get(), llvm::Constant::getNullValue(), llvm::GEPNoWrapFlags::hasNoUnsignedSignedWrap(), llvm::GEPNoWrapFlags::hasNoUnsignedWrap(), I, llvm::isa(), isZero(), llvm_unreachable, PHI, llvm::InstCombiner::replaceInstUsesWith(), and setInsertionPoint().

Referenced by transformToIndexedCompare().

setInsertionPoint()

STATISTIC()

STATISTIC ( NumSel ,
"Number of select opts" )

subWithOverflow()

transformToIndexedCompare()

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 601 of file InstCombineCompares.cpp.

References llvm::InstCombiner::Builder, canRewriteGEPAsOffset(), Cond, DL, llvm::IRBuilderBase::getInt(), llvm::GEPOperator::getNoWrapFlags(), llvm::ICmpInst::getSignedPredicate(), llvm::Value::getType(), llvm::GEPOperator::hasAllConstantIndices(), llvm::Type::isVectorTy(), llvm::Offset, rewriteGEPAsOffset(), RHS, and llvm::Value::stripAndAccumulateConstantOffsets().

Referenced by llvm::InstCombinerImpl::foldGEPICmp().