LLVM
8.0.1
|
#include "InstCombineInternal.h"
#include "llvm/ADT/SetVector.h"
#include "llvm/Analysis/ConstantFolding.h"
#include "llvm/Analysis/TargetLibraryInfo.h"
#include "llvm/IR/DataLayout.h"
#include "llvm/IR/DIBuilder.h"
#include "llvm/IR/PatternMatch.h"
#include "llvm/Support/KnownBits.h"
Go to the source code of this file.
Macros | |
#define | DEBUG_TYPE "instcombine" |
Functions | |
static Value * | decomposeSimpleLinearExpr (Value *Val, unsigned &Scale, uint64_t &Offset) |
Analyze 'Val', seeing if it is a simple linear expression. More... | |
static bool | canAlwaysEvaluateInType (Value *V, Type *Ty) |
Constants and extensions/truncates from the destination type are always free to be evaluated in that type. More... | |
static bool | canNotEvaluateInType (Value *V, Type *Ty) |
Filter out values that we can not evaluate in the destination type for free. More... | |
static bool | canEvaluateTruncated (Value *V, Type *Ty, InstCombiner &IC, Instruction *CxtI) |
Return true if we can evaluate the specified expression tree as type Ty instead of its larger type, and arrive with the same value. More... | |
static Instruction * | foldVecTruncToExtElt (TruncInst &Trunc, InstCombiner &IC) |
Given a vector that is bitcast to an integer, optionally logically right-shifted, and truncated, convert it to an extractelement. More... | |
static Instruction * | shrinkSplatShuffle (TruncInst &Trunc, InstCombiner::BuilderTy &Builder) |
Try to narrow the width of a splat shuffle. More... | |
static Instruction * | shrinkInsertElt (CastInst &Trunc, InstCombiner::BuilderTy &Builder) |
Try to narrow the width of an insert element. More... | |
static bool | canEvaluateZExtd (Value *V, Type *Ty, unsigned &BitsToClear, InstCombiner &IC, Instruction *CxtI) |
Determine if the specified value can be computed in the specified wider type and produce the same low bits. More... | |
static bool | canEvaluateSExtd (Value *V, Type *Ty) |
Return true if we can take the specified value and return it as type Ty without inserting any new casts and without changing the value of the common low bits. More... | |
static bool | fitsInFPType (ConstantFP *CFP, const fltSemantics &Sem) |
Return a Constant* for the specified floating-point constant if it fits in the specified FP type without changing its value. More... | |
static Type * | shrinkFPConstant (ConstantFP *CFP) |
static Type * | shrinkFPConstantVector (Value *V) |
static Type * | getMinimumFPType (Value *V) |
Find the minimum FP type we can safely truncate to. More... | |
static Instruction * | optimizeVectorResize (Value *InVal, VectorType *DestTy, InstCombiner &IC) |
This input value (which is known to have vector type) is being zero extended or truncated to the specified vector type. More... | |
static bool | isMultipleOfTypeSize (unsigned Value, Type *Ty) |
static unsigned | getTypeSizeIndex (unsigned Value, Type *Ty) |
static bool | collectInsertionElements (Value *V, unsigned Shift, SmallVectorImpl< Value *> &Elements, Type *VecEltTy, bool isBigEndian) |
V is a value which is inserted into a vector of VecEltTy. More... | |
static Value * | optimizeIntegerToVectorInsertions (BitCastInst &CI, InstCombiner &IC) |
If the input is an 'or' instruction, we may be doing shifts and ors to assemble the elements of the vector manually. More... | |
static Instruction * | canonicalizeBitCastExtElt (BitCastInst &BitCast, InstCombiner &IC) |
Canonicalize scalar bitcasts of extracted elements into a bitcast of the vector followed by extract element. More... | |
static Instruction * | foldBitCastBitwiseLogic (BitCastInst &BitCast, InstCombiner::BuilderTy &Builder) |
Change the type of a bitwise logic operation if we can eliminate a bitcast. More... | |
static Instruction * | foldBitCastSelect (BitCastInst &BitCast, InstCombiner::BuilderTy &Builder) |
Change the type of a select if we can eliminate a bitcast. More... | |
static bool | hasStoreUsersOnly (CastInst &CI) |
Check if all users of CI are StoreInsts. More... | |
#define DEBUG_TYPE "instcombine" |
Definition at line 25 of file InstCombineCasts.cpp.
Constants and extensions/truncates from the destination type are always free to be evaluated in that type.
This is a helper for canEvaluate*.
Definition at line 306 of file InstCombineCasts.cpp.
References llvm::Value::getType(), llvm::PatternMatch::m_Trunc(), llvm::PatternMatch::m_Value(), llvm::PatternMatch::m_ZExtOrSExt(), llvm::PatternMatch::match(), and X.
Referenced by canEvaluateSExtd(), canEvaluateTruncated(), and canEvaluateZExtd().
Return true if we can take the specified value and return it as type Ty without inserting any new casts and without changing the value of the common low bits.
This is used by code that tries to promote integer operations to a wider types will allow us to eliminate the extension.
This function works on both vectors and scalars.
Definition at line 1313 of file InstCombineCasts.cpp.
References llvm::MCID::Add, assert(), canAlwaysEvaluateInType(), canNotEvaluateInType(), llvm::Type::getScalarSizeInBits(), llvm::Value::getType(), I, llvm::PHINode::incoming_values(), and llvm::MCID::Select.
Referenced by llvm::InstCombiner::visitSExt().
|
static |
Return true if we can evaluate the specified expression tree as type Ty instead of its larger type, and arrive with the same value.
This is used by code that tries to eliminate truncates.
Ty will always be a type smaller than V. We should return true if trunc(V) can be computed by computing V in the smaller type. If V is an instruction, then trunc(inst(x,y)) can be computed as inst(trunc(x),trunc(y)), which only makes sense if x and y can be efficiently truncated.
This function works on both vectors and scalars.
Definition at line 342 of file InstCombineCasts.cpp.
References llvm::MCID::Add, assert(), canAlwaysEvaluateInType(), canNotEvaluateInType(), llvm::APInt::getBitsSetFrom(), llvm::SelectInst::getFalseValue(), llvm::APInt::getLimitedValue(), llvm::Type::getScalarSizeInBits(), llvm::SelectInst::getTrueValue(), llvm::Value::getType(), I, llvm::PHINode::incoming_values(), llvm::PatternMatch::m_APInt(), llvm::BitmaskEnumDetail::Mask(), llvm::PatternMatch::match(), llvm::MCID::Select, and SI.
Referenced by llvm::InstCombiner::visitTrunc().
|
static |
Determine if the specified value can be computed in the specified wider type and produce the same low bits.
If not, return false.
If this function returns true, it can also return a non-zero number of bits (in BitsToClear) which indicates that the value it computes is correct for the zero extend, but that the additional BitsToClear bits need to be zero'd out. For example, to promote something like:
B = trunc i64 A to i32 C = lshr i32 B, 8 E = zext i32 C to i64
CanEvaluateZExtd for the 'lshr' will return true, and BitsToClear will be set to 8 to indicate that the promoted value needs to have bits 24-31 cleared in addition to bits 32-63. Since an 'and' will be generated to clear the top bits anyway, doing this has no extra cost.
This function works on both vectors and scalars.
Definition at line 983 of file InstCombineCasts.cpp.
References llvm::MCID::Add, canAlwaysEvaluateInType(), canNotEvaluateInType(), llvm::APInt::getHighBitsSet(), llvm::PHINode::getIncomingValue(), llvm::PHINode::getNumIncomingValues(), llvm::Type::getScalarSizeInBits(), llvm::Value::getType(), llvm::APInt::getZExtValue(), I, llvm::PatternMatch::m_APInt(), llvm::PatternMatch::match(), and llvm::MCID::Select.
Referenced by llvm::InstCombiner::visitZExt().
Filter out values that we can not evaluate in the destination type for free.
This is a helper for canEvaluate*.
Definition at line 319 of file InstCombineCasts.cpp.
References assert(), and llvm::Value::hasOneUse().
Referenced by canEvaluateSExtd(), canEvaluateTruncated(), and canEvaluateZExtd().
|
static |
Canonicalize scalar bitcasts of extracted elements into a bitcast of the vector followed by extract element.
The backend tends to handle bitcasts of vectors better than bitcasts of scalars because vector registers are usually not type-specific like scalar integer or scalar floating-point.
Definition at line 2030 of file InstCombineCasts.cpp.
References llvm::InstCombiner::Builder, llvm::ExtractElementInst::Create(), llvm::IRBuilder< T, Inserter >::CreateBitCast(), llvm::dyn_cast(), llvm::VectorType::get(), llvm::User::getOperand(), llvm::Value::getType(), and llvm::VectorType::isValidElementType().
Referenced by llvm::InstCombiner::visitBitCast().
|
static |
V is a value which is inserted into a vector of VecEltTy.
Look through the value to see if we can decompose it into insertions into the vector. See the example in the comment for OptimizeIntegerToVectorInsertions for the pattern this handles. The type of V is always a non-zero multiple of VecEltTy's size. Shift is the number of bits between the lsb of V and the lsb of the vector.
This returns false if the pattern can't be matched or true if it can, filling in Elements with the elements found here.
Definition at line 1892 of file InstCombineCasts.cpp.
References assert(), C, llvm::dyn_cast(), llvm::IntegerType::get(), llvm::ConstantInt::get(), llvm::ConstantExpr::getBitCast(), llvm::Value::getContext(), llvm::ConstantExpr::getLShr(), llvm::Instruction::getOpcode(), llvm::User::getOperand(), llvm::Type::getPrimitiveSizeInBits(), llvm::ConstantExpr::getTrunc(), llvm::Value::getType(), getTypeSizeIndex(), llvm::ConstantInt::getZExtValue(), llvm::Value::hasOneUse(), I, isMultipleOfTypeSize(), and llvm::SmallVectorBase::size().
Referenced by optimizeIntegerToVectorInsertions().
Analyze 'Val', seeing if it is a simple linear expression.
If so, decompose it, returning some value X, such that Val is X*Scale+Offset.
Definition at line 31 of file InstCombineCasts.cpp.
References llvm::MCID::Add, C, llvm::ConstantFoldConstant(), llvm::BinaryOperator::Create(), llvm::SelectInst::Create(), llvm::PHINode::Create(), llvm::CastInst::CreateIntegerCast(), llvm::dyn_cast(), llvm::ConstantInt::get(), llvm::AllocaInst::getAlignment(), llvm::AllocaInst::getAllocatedType(), llvm::AllocaInst::getArraySize(), llvm::CastInst::getDestTy(), llvm::PointerType::getElementType(), llvm::PHINode::getIncomingBlock(), llvm::PHINode::getIncomingValue(), llvm::ConstantExpr::getIntegerCast(), llvm::PHINode::getNumIncomingValues(), llvm::Instruction::getOpcode(), llvm::CastInst::getOpcode(), llvm::User::getOperand(), llvm::CastInst::getSrcTy(), llvm::AllocaInst::getType(), llvm::Value::getType(), llvm::OverflowingBinaryOperator::hasNoSignedWrap(), llvm::OverflowingBinaryOperator::hasNoUnsignedWrap(), llvm::Value::hasOneUse(), I, llvm::CastInst::isEliminableCastPair(), llvm::Type::isPtrOrPtrVectorTy(), llvm::Type::isSized(), llvm::AllocaInst::isUsedWithInAlloca(), llvm_unreachable, llvm::MCID::Select, llvm::AllocaInst::setAlignment(), llvm::AllocaInst::setUsedWithInAlloca(), and llvm::Value::takeName().
|
static |
Return a Constant* for the specified floating-point constant if it fits in the specified FP type without changing its value.
Definition at line 1452 of file InstCombineCasts.cpp.
References llvm::APFloat::convert(), F(), llvm::ConstantFP::getValueAPF(), and llvm::APFloatBase::rmNearestTiesToEven.
Referenced by shrinkFPConstant().
|
static |
Change the type of a bitwise logic operation if we can eliminate a bitcast.
Definition at line 2051 of file InstCombineCasts.cpp.
References C, llvm::BinaryOperator::Create(), llvm::IRBuilder< T, Inserter >::CreateBitCast(), llvm::ConstantExpr::getBitCast(), llvm::BinaryOperator::getOpcode(), llvm::User::getOperand(), llvm::Value::getType(), llvm::Instruction::isBitwiseLogicOp(), llvm::Type::isIntOrIntVectorTy(), llvm::Type::isVectorTy(), llvm::PatternMatch::m_BinOp(), llvm::PatternMatch::m_BitCast(), llvm::PatternMatch::m_Constant(), llvm::PatternMatch::m_OneUse(), llvm::PatternMatch::m_Value(), llvm::PatternMatch::match(), and X.
Referenced by llvm::InstCombiner::visitBitCast().
|
static |
Change the type of a select if we can eliminate a bitcast.
Definition at line 2097 of file InstCombineCasts.cpp.
References llvm::SelectInst::Create(), llvm::IRBuilder< T, Inserter >::CreateBitCast(), llvm::User::getOperand(), llvm::Value::getType(), llvm::Type::getVectorNumElements(), llvm::Type::isVectorTy(), llvm::PatternMatch::m_BitCast(), llvm::PatternMatch::m_OneUse(), llvm::PatternMatch::m_Select(), llvm::PatternMatch::m_Value(), llvm::PatternMatch::match(), and X.
Referenced by llvm::InstCombiner::visitBitCast().
|
static |
Given a vector that is bitcast to an integer, optionally logically right-shifted, and truncated, convert it to an extractelement.
Example (big endian): trunc (lshr (bitcast <4 x i32> X to i128), 32) to i32 —> extractelement <4 x i32> X, 1
Definition at line 457 of file InstCombineCasts.cpp.
References llvm::MCID::Add, assert(), llvm::InstCombiner::Builder, C, llvm::BinaryOperator::Create(), llvm::CallInst::Create(), llvm::ExtractElementInst::Create(), llvm::IRBuilder< T, Inserter >::CreateBitCast(), F(), llvm::Intrinsic::fshl, llvm::Intrinsic::fshr, llvm::VectorType::get(), llvm::InstCombiner::getDataLayout(), llvm::Intrinsic::getDeclaration(), llvm::SequentialType::getElementType(), llvm::APInt::getHighBitsSet(), llvm::IRBuilderBase::getInt32(), llvm::Instruction::getModule(), getOpcode(), llvm::BinaryOperator::getOpcode(), llvm::User::getOperand(), llvm::Type::getPrimitiveSizeInBits(), llvm::Type::getScalarSizeInBits(), llvm::CastInst::getSrcTy(), llvm::ConstantExpr::getTrunc(), llvm::Value::getType(), llvm::ConstantInt::getZExtValue(), llvm::Value::hasOneUse(), llvm::DataLayout::isBigEndian(), llvm::isPowerOf2_32(), llvm::PatternMatch::m_And(), llvm::PatternMatch::m_BinOp(), llvm::PatternMatch::m_BitCast(), llvm::PatternMatch::m_CombineOr(), llvm::PatternMatch::m_Constant(), llvm::PatternMatch::m_ConstantInt(), llvm::PatternMatch::m_LogicalShift(), llvm::PatternMatch::m_LShr(), llvm::PatternMatch::m_Neg(), llvm::PatternMatch::m_OneUse(), llvm::PatternMatch::m_Or(), llvm::PatternMatch::m_Specific(), llvm::PatternMatch::m_SpecificInt(), llvm::PatternMatch::m_Sub(), llvm::PatternMatch::m_Value(), llvm::PatternMatch::m_ZExt(), llvm::PatternMatch::m_ZExtOrSExt(), llvm::BitmaskEnumDetail::Mask(), llvm::MaskedValueIsZero(), llvm::PatternMatch::match(), and X.
Referenced by llvm::InstCombiner::visitTrunc().
Find the minimum FP type we can safely truncate to.
Definition at line 1507 of file InstCombineCasts.cpp.
References llvm::Value::getType(), shrinkFPConstant(), shrinkFPConstantVector(), and T.
Referenced by llvm::InstCombiner::visitFPTrunc().
Definition at line 1878 of file InstCombineCasts.cpp.
References llvm::Type::getPrimitiveSizeInBits().
Referenced by collectInsertionElements().
Check if all users of CI are StoreInsts.
Definition at line 2141 of file InstCombineCasts.cpp.
References llvm::PHINode::addIncoming(), assert(), C, llvm::dyn_cast(), llvm::SmallVectorBase::empty(), llvm::ConstantExpr::getBitCast(), llvm::User::getOperand(), llvm::Value::getType(), llvm::SetVector< T, SmallVector< T, N >, SmallDenseSet< T, N > >::insert(), llvm::SmallVectorImpl< T >::pop_back_val(), llvm::SmallVectorTemplateBase< T >::push_back(), SI, and llvm::Value::users().
Definition at line 1874 of file InstCombineCasts.cpp.
References llvm::Type::getPrimitiveSizeInBits().
Referenced by collectInsertionElements().
|
static |
If the input is an 'or' instruction, we may be doing shifts and ors to assemble the elements of the vector manually.
Try to rip the code out and replace it with insertelements. This is to optimize code like this:
tmp37 = bitcast float inc to i32 tmp38 = zext i32 tmp37 to i64 tmp31 = bitcast float inc5 to i32 tmp32 = zext i32 tmp31 to i64 tmp33 = shl i64 tmp32, 32 ins35 = or i64 tmp33, tmp38 tmp43 = bitcast i64 ins35 to <2 x float>
Into two insertelements that do "buildvector{%inc, %inc5}".
Definition at line 2001 of file InstCombineCasts.cpp.
References llvm::InstCombiner::Builder, collectInsertionElements(), llvm::IRBuilder< T, Inserter >::CreateInsertElement(), llvm::InstCombiner::getDataLayout(), llvm::SequentialType::getElementType(), llvm::IRBuilderBase::getInt32(), llvm::Constant::getNullValue(), llvm::SequentialType::getNumElements(), llvm::User::getOperand(), llvm::Value::getType(), and llvm::DataLayout::isBigEndian().
Referenced by llvm::InstCombiner::visitBitCast().
|
static |
This input value (which is known to have vector type) is being zero extended or truncated to the specified vector type.
Try to replace it with a shuffle (and vector/vector bitcast) if possible.
The source and destination vector types may have different element types.
Definition at line 1822 of file InstCombineCasts.cpp.
References llvm::InstCombiner::Builder, llvm::IRBuilder< T, Inserter >::CreateBitCast(), llvm::VectorType::get(), llvm::ConstantDataVector::get(), llvm::UndefValue::get(), llvm::Value::getContext(), llvm::SequentialType::getElementType(), llvm::Constant::getNullValue(), llvm::SequentialType::getNumElements(), llvm::Type::getPrimitiveSizeInBits(), llvm::Value::getType(), llvm::SmallVectorTemplateBase< T, bool >::push_back(), and llvm::NVPTX::PTXLdStInstCode::V2.
Referenced by llvm::InstCombiner::visitBitCast().
|
static |
Definition at line 1459 of file InstCombineCasts.cpp.
References fitsInFPType(), llvm::Value::getContext(), llvm::Type::getDoubleTy(), llvm::Type::getFloatTy(), llvm::Type::getHalfTy(), llvm::Type::getPPC_FP128Ty(), llvm::Value::getType(), llvm::APFloatBase::IEEEdouble(), llvm::APFloatBase::IEEEhalf(), llvm::APFloatBase::IEEEsingle(), and llvm::Type::isDoubleTy().
Referenced by getMinimumFPType(), and shrinkFPConstantVector().
Definition at line 1479 of file InstCombineCasts.cpp.
References llvm::dyn_cast(), llvm::VectorType::get(), llvm::Type::getFPMantissaWidth(), llvm::Type::getVectorNumElements(), and shrinkFPConstant().
Referenced by getMinimumFPType().
|
static |
Try to narrow the width of an insert element.
This could be generalized for any vector constant, but we limit the transform to insertion into undef to avoid potential backend problems from unsupported insertion widths. This could also be extended to handle the case of inserting a scalar constant into a vector variable.
Definition at line 658 of file InstCombineCasts.cpp.
References assert(), llvm::InsertElementInst::Create(), llvm::IRBuilder< T, Inserter >::CreateCast(), llvm::dyn_cast(), llvm::UndefValue::get(), llvm::CastInst::getOpcode(), llvm::User::getOperand(), llvm::Type::getScalarType(), and llvm::Value::getType().
Referenced by llvm::InstCombiner::visitFPTrunc(), and llvm::InstCombiner::visitTrunc().
|
static |
Try to narrow the width of a splat shuffle.
This could be generalized to any shuffle with a constant operand, but we limit the transform to avoid creating a shuffle type that targets may not be able to lower effectively.
Definition at line 638 of file InstCombineCasts.cpp.
References llvm::IRBuilder< T, Inserter >::CreateTrunc(), llvm::dyn_cast(), llvm::UndefValue::get(), llvm::User::getOperand(), and llvm::Value::getType().
Referenced by llvm::InstCombiner::visitTrunc().