LLVM
15.0.0git
|
#include "InstCombineInternal.h"
#include "llvm/ADT/APSInt.h"
#include "llvm/ADT/SetVector.h"
#include "llvm/ADT/Statistic.h"
#include "llvm/Analysis/CmpInstAnalysis.h"
#include "llvm/Analysis/ConstantFolding.h"
#include "llvm/Analysis/InstructionSimplify.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/KnownBits.h"
#include "llvm/Transforms/InstCombine/InstCombiner.h"
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 | 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 Value * | evaluateGEPOffsetExpression (User *GEP, InstCombinerImpl &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 (Type *ElemTy, 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 Value * | rewriteGEPAsOffset (Type *ElemTy, 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 (Type *ElemTy, 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 Instruction * | transformToIndexedCompare (GEPOperator *GEPLHS, Value *RHS, ICmpInst::Predicate Cond, const DataLayout &DL) |
Converts (CMP GEPLHS, RHS) if this change would make RHS a constant. More... | |
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. More... | |
static Instruction * | foldICmpShlOne (ICmpInst &Cmp, Instruction *Shl, const APInt &C) |
Fold icmp (shl 1, Y), C. More... | |
static Instruction * | foldICmpIntrinsicWithIntrinsic (ICmpInst &Cmp) |
Fold an icmp with LLVM intrinsics. More... | |
static Value * | foldICmpWithLowBitMaskedVal (ICmpInst &I, InstCombiner::BuilderTy &Builder) |
Some comparisons can be simplified. More... | |
static Value * | foldICmpWithTruncSignExtendedVal (ICmpInst &I, InstCombiner::BuilderTy &Builder) |
Some comparisons can be simplified. More... | |
static Value * | foldShiftIntoShiftInAnotherHandOfAndInICmp (ICmpInst &I, const SimplifyQuery SQ, InstCombiner::BuilderTy &Builder) |
static Instruction * | foldICmpXNegX (ICmpInst &I) |
static Instruction * | foldICmpWithMinMax (ICmpInst &Cmp) |
Fold icmp Pred min|max(X, Y), X. More... | |
static Instruction * | foldICmpWithTrunc (ICmpInst &ICmp, InstCombiner::BuilderTy &Builder) |
static bool | isNeutralValue (Instruction::BinaryOps BinaryOp, Value *RHS) |
static Instruction * | processUMulZExtIdiom (ICmpInst &I, Value *MulVal, Value *OtherVal, InstCombinerImpl &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 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. More... | |
static Instruction * | canonicalizeICmpBool (ICmpInst &I, InstCombiner::BuilderTy &Builder) |
Integer compare with boolean values can always be turned into bitwise ops. More... | |
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. More... | |
static Instruction * | foldFCmpReciprocalAndZero (FCmpInst &I, Instruction *LHSI, Constant *RHSC) |
Fold (C / X) < 0.0 --> X < 0.0 if possible. Swap predicate if necessary. More... | |
static Instruction * | foldFabsWithFcmpZero (FCmpInst &I, InstCombinerImpl &IC) |
Optimize fabs(X) compared with zero. More... | |
static Instruction * | foldFCmpFNegCommonOp (FCmpInst &I) |
#define DEBUG_TYPE "instcombine" |
Definition at line 31 of file InstCombineCompares.cpp.
|
static |
Compute Result = In1+In2, returning true if the result overflowed for this type.
Definition at line 39 of file InstCombineCompares.cpp.
References llvm::APInt::sadd_ov(), and llvm::APInt::uadd_ov().
Referenced by llvm::InstCombinerImpl::foldICmpDivConstant().
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.
This transform allows them to be folded in visitICmpInst.
Definition at line 5687 of file InstCombineCompares.cpp.
References llvm::InstCombiner::getFlippedStrictnessPredicateAndConstant(), I, llvm::InstCombiner::isCanonicalPredicate(), llvm::ICmpInst::isEquality(), and llvm::CmpInst::isIntPredicate().
Referenced by llvm::InstCombinerImpl::visitICmpInst().
|
static |
Integer compare with boolean values can always be turned into bitwise ops.
Definition at line 5731 of file InstCombineCompares.cpp.
References assert(), B, Builder, 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_FALLTHROUGH, llvm_unreachable, llvm::PatternMatch::m_One(), llvm::PatternMatch::m_Zero(), llvm::PatternMatch::match(), and std::swap().
Referenced by llvm::InstCombinerImpl::visitICmpInst().
|
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 508 of file InstCombineCompares.cpp.
References llvm::sampleprof::Base, llvm::SetVector< T, Vector, Set >::contains(), DL, GEP, llvm::SetVector< T, Vector, Set >::insert(), llvm::SetVector< T, Vector, Set >::size(), and llvm::Value::uses().
Referenced by transformToIndexedCompare().
|
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 413 of file InstCombineCompares.cpp.
References llvm::InstCombiner::Builder, llvm::IRBuilderBase::CreateAdd(), llvm::IRBuilderBase::CreateIntCast(), llvm::IRBuilderBase::CreateTrunc(), DL, llvm::numbers::e, GEP, llvm::gep_type_begin(), llvm::ConstantInt::get(), llvm::TypeSize::getFixedSize(), llvm::generic_gep_type_iterator< ItTy >::getIndexedType(), llvm::Type::getIntegerBitWidth(), llvm::Type::getPrimitiveSizeInBits(), llvm::ConstantInt::getSExtValue(), llvm::generic_gep_type_iterator< ItTy >::getStructTypeOrNull(), llvm::Value::getType(), llvm::ConstantInt::getZExtValue(), i, llvm::ConstantInt::isZero(), and llvm::SignExtend64().
Referenced by llvm::InstCombinerImpl::foldGEPICmp().
|
static |
Optimize fabs(X) compared with zero.
Definition at line 6535 of file InstCombineCompares.cpp.
References assert(), 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_FAbs(), llvm::PatternMatch::m_PosZeroFP(), llvm::PatternMatch::m_Value(), llvm::PatternMatch::match(), P, llvm::InstCombinerImpl::replaceOperand(), and X.
Referenced by llvm::InstCombinerImpl::visitFCmpInst().
|
static |
Definition at line 6597 of file InstCombineCompares.cpp.
References llvm::Constant::getNullValue(), llvm::Value::getType(), I, llvm::PatternMatch::m_FNeg(), llvm::PatternMatch::m_Specific(), llvm::PatternMatch::m_Value(), llvm::PatternMatch::match(), and std::swap().
Referenced by llvm::InstCombinerImpl::visitFCmpInst().
|
static |
Fold (C / X) < 0.0 --> X < 0.0 if possible. Swap predicate if necessary.
Definition at line 6488 of file InstCombineCompares.cpp.
References 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().
|
static |
Fold an icmp with LLVM intrinsics.
Definition at line 3246 of file InstCombineCompares.cpp.
References assert().
Referenced by llvm::InstCombinerImpl::foldICmpEquality().
|
static |
Definition at line 5926 of file InstCombineCompares.cpp.
References llvm::CmpInst::Create(), I, llvm::PatternMatch::m_Instruction(), llvm::PatternMatch::m_Zero(), llvm::PatternMatch::match(), and llvm::NullPointerIsDefined().
Referenced by llvm::InstCombinerImpl::visitICmpInst().
|
static |
Definition at line 5898 of file InstCombineCompares.cpp.
References B, 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_One(), llvm::PatternMatch::m_Value(), llvm::PatternMatch::m_ZeroInt(), and llvm::PatternMatch::match().
Referenced by llvm::InstCombinerImpl::visitICmpInst().
|
static |
Fold icmp (shl 1, Y), C.
Definition at line 2006 of file InstCombineCompares.cpp.
References llvm::ConstantInt::get(), llvm::Value::getType(), 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::PatternMatch::m_One(), llvm::PatternMatch::m_Shl(), llvm::PatternMatch::m_Value(), llvm::PatternMatch::match(), and Y.
Referenced by llvm::InstCombinerImpl::foldICmpShlConstant().
|
static |
Definition at line 5810 of file InstCombineCompares.cpp.
References Builder, 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::PatternMatch::m_Not(), llvm::PatternMatch::m_One(), llvm::PatternMatch::m_OneUse(), llvm::PatternMatch::m_Shl(), llvm::PatternMatch::m_Value(), llvm::PatternMatch::match(), X, and Y.
Referenced by llvm::InstCombinerImpl::visitICmpInst().
|
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 3525 of file InstCombineCompares.cpp.
References assert(), Builder, llvm::numbers::e, llvm::Constant::getAggregateElement(), i, I, llvm_unreachable, M, 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(), llvm::Constant::replaceUndefsWith(), X, and Y.
Referenced by llvm::InstCombinerImpl::foldICmpBinOp().
|
static |
Fold icmp Pred min|max(X, Y), X.
Definition at line 4389 of file InstCombineCompares.cpp.
References 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::PatternMatch::m_c_SMax(), llvm::PatternMatch::m_c_SMin(), llvm::PatternMatch::m_c_UMax(), llvm::PatternMatch::m_c_UMin(), llvm::PatternMatch::m_Specific(), llvm::PatternMatch::m_Value(), llvm::PatternMatch::match(), std::swap(), X, and Y.
Referenced by llvm::InstCombinerImpl::visitICmpInst().
|
static |
Definition at line 4673 of file InstCombineCompares.cpp.
References llvm::And, Builder, llvm::decomposeBitTestICmp(), llvm::ConstantInt::get(), llvm::Constant::getNullValue(), llvm::User::getOperand(), llvm::CmpInst::getPredicate(), llvm::CmpInst::ICMP_EQ, llvm::CmpInst::ICMP_NE, llvm::CmpInst::ICMP_UGT, llvm::CmpInst::ICMP_ULT, llvm::PatternMatch::m_APInt(), llvm::PatternMatch::m_OneUse(), llvm::PatternMatch::m_Trunc(), llvm::PatternMatch::m_Value(), llvm::BitmaskEnumDetail::Mask(), llvm::PatternMatch::match(), and X.
Referenced by llvm::InstCombinerImpl::foldICmpWithCastOp().
|
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 3619 of file InstCombineCompares.cpp.
References assert(), llvm::BitWidth, Builder, C1, llvm::ConstantInt::get(), I, llvm::APInt::isPowerOf2(), llvm::APInt::lshr(), llvm::PatternMatch::m_APInt(), llvm::PatternMatch::m_AShr(), llvm::PatternMatch::m_c_ICmp(), llvm::PatternMatch::m_Deferred(), llvm::PatternMatch::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().
|
static |
Definition at line 3932 of file InstCombineCompares.cpp.
References llvm::CmpInst::Create(), llvm::Constant::getNullValue(), llvm::ICmpInst::getSignedPredicate(), llvm::CmpInst::getSwappedPredicate(), I, llvm::CmpInst::isSigned(), llvm::CmpInst::isUnsigned(), llvm::PatternMatch::m_c_ICmp(), llvm::PatternMatch::m_Deferred(), llvm::PatternMatch::m_NSWNeg(), llvm::PatternMatch::m_Value(), llvm::PatternMatch::match(), and X.
Referenced by llvm::InstCombinerImpl::foldICmpBinOp().
|
static |
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 <pred> 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 <pred> i64 lhs.scalar, rhs.scalar
for <pred> in {ne, eq}.
Definition at line 5947 of file InstCombineCompares.cpp.
References Builder, llvm::CmpInst::Create(), DL, llvm::Value::getName(), llvm::Value::getType(), I, llvm::CmpInst::ICMP_NE, llvm::ICmpInst::isEquality(), LHS, llvm::PatternMatch::m_BitCast(), llvm::PatternMatch::m_ICmp(), llvm::PatternMatch::m_OneUse(), llvm::PatternMatch::m_Value(), llvm::PatternMatch::m_Zero(), llvm::PatternMatch::match(), and RHS.
Referenced by llvm::InstCombinerImpl::visitICmpInst().
|
static |
Definition at line 3693 of file InstCombineCompares.cpp.
References assert(), Builder, llvm::computeKnownBits(), llvm::KnownBits::countMinLeadingZeros(), 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::ConstantExpr::getZExtOrBitCast(), llvm::Value::hasOneUse(), I, 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::PatternMatch::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().
|
static |
Definition at line 5853 of file InstCombineCompares.cpp.
References Builder, llvm::ConstantVector::getSplat(), llvm::Constant::getSplatValue(), llvm::Value::getType(), llvm::Value::hasOneUse(), LHS, M, llvm::PatternMatch::m_Constant(), llvm::PatternMatch::m_Shuffle(), llvm::PatternMatch::m_Undef(), llvm::PatternMatch::m_Value(), llvm::PatternMatch::match(), RHS, and llvm::NVPTX::PTXLdStInstCode::V2.
Referenced by llvm::InstCombinerImpl::visitFCmpInst(), and llvm::InstCombinerImpl::visitICmpInst().
|
static |
Looks through GEPs, IntToPtrInsts and PtrToIntInsts in order to express the input Value as a constant indexed GEP.
Returns a pair containing the GEPs Pointer and Index.
Definition at line 746 of file InstCombineCompares.cpp.
References DL, GEP, llvm::IntegerType::get(), llvm::ConstantExpr::getAdd(), llvm::Value::getContext(), llvm::Constant::getNullValue(), llvm::ConstantExpr::getSExtOrTrunc(), and llvm::Value::getType().
Referenced by transformToIndexedCompare().
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 5192 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::InstCombiner::isSignBitCheck(), llvm::PatternMatch::m_APInt(), llvm::PatternMatch::match(), and RHS.
Referenced by llvm::InstCombinerImpl::foldICmpUsingKnownBits().
|
static |
Given an icmp instruction, return true if any use of this comparison is a branch on sign bit comparison.
Definition at line 65 of file InstCombineCompares.cpp.
References I.
Referenced by llvm::InstCombinerImpl::foldICmpWithDominatingICmp().
|
static |
Return true when the instruction sequence within a block is select-cmp-br.
Definition at line 5279 of file InstCombineCompares.cpp.
Referenced by llvm::InstCombinerImpl::replacedSelectWithOperand().
|
static |
Definition at line 4875 of file InstCombineCompares.cpp.
References llvm::MCID::Add, llvm_unreachable, llvm::PatternMatch::m_One(), llvm::PatternMatch::m_Zero(), llvm::PatternMatch::match(), Mul, and RHS.
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 76 of file InstCombineCompares.cpp.
References llvm::CmpInst::ICMP_SGE, llvm::CmpInst::ICMP_SGT, llvm::CmpInst::ICMP_SLE, llvm::CmpInst::ICMP_SLT, llvm::ICmpInst::isRelational(), and llvm::CmpInst::isSigned().
Referenced by llvm::InstCombinerImpl::foldICmpMulConstant(), and llvm::InstCombinerImpl::foldICmpShlConstant().
|
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 1236 of file InstCombineCompares.cpp.
References B, llvm::InstCombiner::Builder, Builder, llvm::InstCombiner::ComputeMaxSignificantBits(), llvm::APInt::countTrailingZeros(), llvm::ExtractValueInst::Create(), llvm::InstCombinerImpl::eraseInstFromFunction(), F, llvm::IntegerType::get(), llvm::ConstantInt::getBitWidth(), llvm::Value::getContext(), llvm::Intrinsic::getDeclaration(), llvm::APInt::getLowBitsSet(), llvm::User::getOperand(), llvm::Type::getPrimitiveSizeInBits(), llvm::Value::getType(), llvm::ConstantInt::getValue(), llvm::Value::hasOneUse(), I, llvm::APInt::isPowerOf2(), llvm::InstCombinerImpl::replaceInstUsesWith(), and llvm::Value::users().
Referenced by llvm::InstCombinerImpl::foldICmpWithConstant().
|
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.
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. |
Definition at line 4975 of file InstCombineCompares.cpp.
References llvm::InstCombiner::addToWorklist(), assert(), B, llvm::InstCombiner::Builder, Builder, llvm::APInt::countLeadingZeros(), llvm::ExtractValueInst::Create(), llvm::BinaryOperator::CreateNot(), llvm::APInt::eq(), F, llvm::ConstantInt::getBitWidth(), llvm::APInt::getBitWidth(), llvm::Intrinsic::getDeclaration(), llvm::APInt::getMaxValue(), llvm::APInt::getOneBitSet(), llvm::Type::getPrimitiveSizeInBits(), llvm::Value::getType(), llvm::ConstantInt::getValue(), llvm::Value::hasNUsesOrMore(), I, 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(), LHS, llvm_unreachable, llvm::APInt::logBase2(), llvm::PatternMatch::m_And(), llvm::PatternMatch::m_ConstantInt(), llvm::PatternMatch::m_Value(), llvm::make_early_inc_range(), llvm::PatternMatch::match(), Mul, llvm::InstCombinerImpl::replaceInstUsesWith(), RHS, llvm::User::setOperand(), llvm::APInt::trunc(), llvm::Value::users(), and llvm::APInt::zext().
Referenced by llvm::InstCombinerImpl::visitICmpInst().
|
static |
Returns a re-written value of Start as an indexed GEP using Base as a pointer.
Definition at line 632 of file InstCombineCompares.cpp.
References llvm::PHINode::addIncoming(), llvm::sampleprof::Base, Builder, llvm::PHINode::Create(), DL, E, llvm::DenseMapBase< DenseMap< KeyT, ValueT, DenseMapInfo< KeyT >, llvm::detail::DenseMapPair< KeyT, ValueT > >, KeyT, ValueT, DenseMapInfo< KeyT >, llvm::detail::DenseMapPair< KeyT, ValueT > >::end(), llvm::DenseMapBase< DenseMap< KeyT, ValueT, DenseMapInfo< KeyT >, llvm::detail::DenseMapPair< KeyT, ValueT > >, KeyT, ValueT, DenseMapInfo< KeyT >, llvm::detail::DenseMapPair< KeyT, ValueT > >::find(), GEP, llvm::IntegerType::get(), llvm::Constant::getNullValue(), llvm::Type::getPointerTo(), llvm::Value::getType(), I, isZero(), llvm_unreachable, llvm::makeArrayRef(), and setInsertionPoint().
Referenced by transformToIndexedCompare().
Definition at line 607 of file InstCombineCompares.cpp.
Referenced by rewriteGEPAsOffset().
|
static |
Compute Result = In1-In2, returning true if the result overflowed for this type.
Definition at line 52 of file InstCombineCompares.cpp.
References llvm::APInt::ssub_ov(), and llvm::APInt::usub_ov().
Referenced by llvm::InstCombinerImpl::foldICmpDivConstant(), and llvm::InstCombinerImpl::foldICmpSubConstant().
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.
Definition at line 5229 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::InstCombinerImpl::visitICmpInst().
|
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 787 of file InstCombineCompares.cpp.
References canRewriteGEPAsOffset(), Cond, DL, getAsConstantIndexedAddress(), llvm::ICmpInst::getSignedPredicate(), llvm::GEPOperator::getSourceElementType(), llvm::Value::getType(), llvm::GEPOperator::hasAllConstantIndices(), llvm::Type::isVectorTy(), rewriteGEPAsOffset(), and RHS.
Referenced by llvm::InstCombinerImpl::foldGEPICmp().