|
LLVM
4.0.0
|
#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/MemoryBuiltins.h"#include "llvm/Analysis/TargetLibraryInfo.h"#include "llvm/Analysis/VectorUtils.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"Go to the source code of this file.
Macros | |
| #define | DEBUG_TYPE "instcombine" |
Functions | |
| STATISTIC (NumSel,"Number of select opts") | |
| static ConstantInt * | extractElement (Constant *V, Constant *Idx) |
| static bool | hasAddOverflow (ConstantInt *Result, ConstantInt *In1, ConstantInt *In2, bool IsSigned) |
| static bool | addWithOverflow (Constant *&Result, Constant *In1, Constant *In2, bool IsSigned=false) |
| Compute Result = In1+In2, returning true if the result overflowed for this type. More... | |
| static bool | hasSubOverflow (ConstantInt *Result, ConstantInt *In1, ConstantInt *In2, bool IsSigned) |
| static bool | subWithOverflow (Constant *&Result, Constant *In1, Constant *In2, bool IsSigned=false) |
| Compute Result = In1-In2, returning true if the result overflowed for this type. More... | |
| static bool | isBranchOnSignBitCheck (ICmpInst &I, bool isSignBit) |
| 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 APInt &KnownZero, const APInt &KnownOne, 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 APInt &KnownZero, const APInt &KnownOne, 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 Value * | evaluateGEPOffsetExpression (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 Value * | rewriteGEPAsOffset (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 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, 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 Instruction * | foldICmpShlOne (ICmpInst &Cmp, Instruction *Shl, const APInt *C) |
| Fold icmp (shl 1, Y), C. More... | |
| static Instruction * | foldICmpWithMinMax (ICmpInst &Cmp) |
| Fold icmp Pred min|max(X, Y), X. More... | |
| static Instruction * | processUMulZExtIdiom (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, bool isSignCheck) |
| 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 operand 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... | |
| #define DEBUG_TYPE "instcombine" |
Definition at line 33 of file InstCombineCompares.cpp.
|
static |
Compute Result = In1+In2, returning true if the result overflowed for this type.
Definition at line 56 of file InstCombineCompares.cpp.
References extractElement(), llvm::ConstantInt::get(), llvm::ConstantExpr::getAdd(), llvm::Value::getContext(), llvm::Type::getInt32Ty(), llvm::Value::getType(), hasAddOverflow(), and 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.
This transform allows them to be folded in visitICmpInst.
Definition at line 4134 of file InstCombineCompares.cpp.
References assert(), llvm::dyn_cast(), llvm::ConstantInt::get(), llvm::ConstantExpr::getAdd(), llvm::Constant::getAggregateElement(), llvm::User::getOperand(), llvm::CmpInst::getPredicate(), llvm::ICmpInst::getSignedPredicate(), llvm::Value::getType(), llvm::Type::getVectorNumElements(), i, llvm::CmpInst::ICMP_SGE, llvm::CmpInst::ICMP_SLE, llvm::CmpInst::ICMP_UGE, llvm::CmpInst::ICMP_UGT, llvm::CmpInst::ICMP_ULE, llvm::CmpInst::ICMP_ULT, llvm::APInt::isMaxValue(), llvm::APInt::isMinValue(), llvm::CmpInst::isSigned(), and llvm::Type::isVectorTy().
Referenced by llvm::InstCombiner::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 599 of file InstCombineCompares.cpp.
References llvm::SmallVectorTemplateCommon< T >::back(), llvm::SetVector< T, Vector, Set >::count(), llvm::dyn_cast(), llvm::SmallVectorBase::empty(), llvm::GetElementPtrInst::getNumIndices(), llvm::User::getOperand(), llvm::Value::getType(), llvm::SetVector< T, Vector, Set >::insert(), llvm::GetElementPtrInst::isInBounds(), llvm::SmallVectorTemplateBase< T, isPodLike< T >::value >::pop_back(), llvm::SmallVectorTemplateBase< T, isPodLike< T >::value >::push_back(), llvm::SetVector< T, Vector, Set >::size(), and llvm::Value::uses().
Referenced by transformToIndexedCompare().
|
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.
Definition at line 178 of file InstCombineCompares.cpp.
References assert(), llvm::APInt::clearBit(), llvm::APInt::getBitWidth(), and llvm::APInt::setBit().
|
static |
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.
Definition at line 201 of file InstCombineCompares.cpp.
References assert(), and llvm::APInt::getBitWidth().
|
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 503 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(), i, llvm::ConstantInt::isZero(), and Offset.
|
static |
Definition at line 39 of file InstCombineCompares.cpp.
References llvm::ConstantExpr::getExtractElement().
Referenced by addWithOverflow(), and subWithOverflow().
|
static |
Fold icmp (shl 1, Y), C.
Definition at line 1836 of file InstCombineCompares.cpp.
References llvm::ConstantInt::get(), llvm::APInt::getBitWidth(), llvm::CmpInst::getPredicate(), 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::APInt::isAllOnesValue(), llvm::ICmpInst::isEquality(), llvm::APInt::isPowerOf2(), llvm::CmpInst::isSigned(), llvm::CmpInst::isUnsigned(), llvm::APInt::logBase2(), llvm::PatternMatch::m_One(), llvm::PatternMatch::m_Shl(), llvm::PatternMatch::m_Value(), llvm::PatternMatch::match(), and Y.
|
static |
Fold icmp Pred min|max(X, Y), X.
Definition at line 3038 of file InstCombineCompares.cpp.
References llvm::User::getOperand(), llvm::CmpInst::getPredicate(), llvm::CmpInst::getSwappedPredicate(), 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(), and Y.
Referenced by llvm::InstCombiner::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 840 of file InstCombineCompares.cpp.
References llvm::IntegerType::get(), llvm::ConstantExpr::getAdd(), llvm::Value::getContext(), llvm::Constant::getNullValue(), llvm::GetElementPtrInst::getNumIndices(), llvm::User::getOperand(), llvm::DataLayout::getPointerTypeSizeInBits(), llvm::ConstantExpr::getSExtOrBitCast(), llvm::Value::getType(), llvm::GetElementPtrInst::hasAllConstantIndices(), and llvm::GetElementPtrInst::isInBounds().
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 3725 of file InstCombineCompares.cpp.
References llvm::APInt::countTrailingOnes(), llvm::APInt::countTrailingZeros(), llvm::dyn_cast(), llvm::APInt::getAllOnesValue(), llvm::APInt::getLowBitsSet(), llvm::User::getOperand(), llvm::CmpInst::getPredicate(), llvm::APInt::getSignBit(), llvm::ConstantInt::getValue(), llvm::CmpInst::ICMP_UGT, and llvm::CmpInst::ICMP_ULT.
|
static |
Definition at line 43 of file InstCombineCompares.cpp.
References llvm::ConstantInt::getValue(), llvm::ConstantInt::isNegative(), llvm::APInt::sgt(), llvm::APInt::slt(), and llvm::APInt::ult().
Referenced by addWithOverflow().
|
static |
Definition at line 77 of file InstCombineCompares.cpp.
References llvm::ConstantInt::getValue(), llvm::ConstantInt::isNegative(), llvm::APInt::sgt(), llvm::APInt::slt(), and llvm::APInt::ugt().
Referenced by subWithOverflow().
Given an icmp instruction, return true if any use of this comparison is a branch on sign bit comparison.
Definition at line 114 of file InstCombineCompares.cpp.
References llvm::Value::users().
|
static |
Return true when the instruction sequence within a block is select-cmp-br.
Definition at line 3831 of file InstCombineCompares.cpp.
References llvm::dyn_cast(), llvm::User::getOperand(), llvm::Instruction::getParent(), and llvm::BasicBlock::getTerminator().
Referenced by llvm::InstCombiner::replacedSelectWithOperand().
|
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 124 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(), and llvm::APInt::isSignBit().
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 153 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::ICmpInst::isRelational(), and llvm::CmpInst::isSigned().
|
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 1286 of file InstCombineCompares.cpp.
References llvm::MCID::Add, llvm::InstCombiner::Builder, llvm::Call, llvm::InstCombiner::ComputeNumSignBits(), llvm::APInt::countTrailingZeros(), llvm::ExtractValueInst::Create(), llvm::dyn_cast(), F, llvm::IntegerType::get(), llvm::ConstantInt::getBitWidth(), llvm::Value::getContext(), llvm::Intrinsic::getDeclaration(), llvm::APInt::getLowBitsSet(), llvm::Instruction::getModule(), llvm::Value::getName(), llvm::User::getOperand(), llvm::Type::getPrimitiveSizeInBits(), llvm::Value::getType(), llvm::ConstantInt::getValue(), llvm::Value::hasOneUse(), llvm::APInt::isPowerOf2(), llvm::InstCombiner::replaceInstUsesWith(), llvm::IRBuilderBase::SetInsertPoint(), and llvm::Value::users().
|
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 3501 of file InstCombineCompares.cpp.
References A, llvm::InstCombineWorklist::Add(), llvm::APIntOps::And(), assert(), B, llvm::InstCombiner::Builder, llvm::Call, 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_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::Value::users(), llvm::InstCombiner::Worklist, and llvm::APInt::zext().
Referenced by llvm::InstCombiner::visitICmpInst().
|
static |
Returns a re-written value of Start as an indexed GEP using Base as a pointer.
Definition at line 723 of file InstCombineCompares.cpp.
References llvm::PHINode::addIncoming(), llvm::PHINode::Create(), E, llvm::DenseMapBase< DenseMap< KeyT, ValueT, KeyInfoT, BucketT >, KeyT, ValueT, KeyInfoT, BucketT >::end(), llvm::DenseMapBase< DenseMap< KeyT, ValueT, KeyInfoT, BucketT >, KeyT, ValueT, KeyInfoT, BucketT >::find(), GEP, llvm::IntegerType::get(), llvm::Value::getContext(), llvm::Value::getName(), llvm::Constant::getNullValue(), llvm::User::getOperand(), llvm::Type::getPointerElementType(), llvm::DataLayout::getPointerTypeSizeInBits(), llvm::Type::getScalarSizeInBits(), llvm::Value::getType(), I, llvm::Type::isPointerTy(), isZero(), llvm_unreachable, llvm::makeArrayRef(), llvm::Value::replaceAllUsesWith(), and setInsertionPoint().
Referenced by transformToIndexedCompare().
Definition at line 698 of file InstCombineCompares.cpp.
References A, assert(), llvm::BasicBlock::getFirstInsertionPt(), I, and llvm::IRBuilderBase::SetInsertPoint().
Referenced by rewriteGEPAsOffset().
| STATISTIC | ( | NumSel | , |
| "Number of select opts" | |||
| ) |
|
static |
Compute Result = In1-In2, returning true if the result overflowed for this type.
Definition at line 91 of file InstCombineCompares.cpp.
References extractElement(), llvm::ConstantInt::get(), llvm::Value::getContext(), llvm::Type::getInt32Ty(), llvm::ConstantExpr::getSub(), llvm::Value::getType(), hasSubOverflow(), and i.
Check if the order of Op0 and Op1 as operand 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 rational 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 3766 of file InstCombineCompares.cpp.
References llvm::dyn_cast(), llvm::BinaryOperator::getOpcode(), llvm::User::getOperand(), llvm::Value::getType(), llvm::Type::isPointerTy(), and llvm::Value::users().
Referenced by llvm::InstCombiner::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 881 of file InstCombineCompares.cpp.
References canRewriteGEPAsOffset(), getAsConstantIndexedAddress(), llvm::ICmpInst::getSignedPredicate(), llvm::Value::getType(), llvm::GEPOperator::hasAllConstantIndices(), and rewriteGEPAsOffset().
1.8.6