|
LLVM 22.0.0git
|
#include "InstCombineInternal.h"#include "llvm/ADT/APInt.h"#include "llvm/ADT/DenseMap.h"#include "llvm/ADT/STLExtras.h"#include "llvm/ADT/STLFunctionalExtras.h"#include "llvm/ADT/SetVector.h"#include "llvm/ADT/SmallVector.h"#include "llvm/Analysis/ConstantFolding.h"#include "llvm/IR/DataLayout.h"#include "llvm/IR/DebugInfo.h"#include "llvm/IR/Instruction.h"#include "llvm/IR/PatternMatch.h"#include "llvm/IR/Type.h"#include "llvm/IR/Value.h"#include "llvm/Support/KnownBits.h"#include "llvm/Transforms/InstCombine/InstCombiner.h"#include <iterator>#include <optional>Go to the source code of this file.
Macros | |
| #define | DEBUG_TYPE "instcombine" |
Typedefs | |
| using | EvaluatedMap = SmallDenseMap<Value *, Value *, 8> |
Functions | |
| static Value * | EvaluateInDifferentTypeImpl (Value *V, Type *Ty, bool isSigned, InstCombinerImpl &IC, EvaluatedMap &Processed) |
| static Instruction * | foldVecTruncToExtElt (TruncInst &Trunc, InstCombinerImpl &IC) |
| Given a vector that is bitcast to an integer, optionally logically right-shifted, and truncated, convert it to an extractelement. | |
| static Instruction * | foldVecExtTruncToExtElt (TruncInst &Trunc, InstCombinerImpl &IC) |
| Whenever an element is extracted from a vector, optionally shifted down, and then truncated, canonicalize by converting it to a bitcast followed by an extractelement. | |
| static Instruction * | shrinkSplatShuffle (TruncInst &Trunc, InstCombiner::BuilderTy &Builder) |
| Try to narrow the width of a splat shuffle. | |
| static Instruction * | shrinkInsertElt (CastInst &Trunc, InstCombiner::BuilderTy &Builder) |
| Try to narrow the width of an insert element. | |
| static bool | fitsInFPType (APFloat F, const fltSemantics &Sem) |
| Return a Constant* for the specified floating-point constant if it fits in the specified FP type without changing its value. | |
| static Type * | shrinkFPConstant (LLVMContext &Ctx, const APFloat &F, bool PreferBFloat) |
| static Type * | shrinkFPConstant (ConstantFP *CFP, bool PreferBFloat) |
| static Type * | shrinkFPConstantVector (Value *V, bool PreferBFloat) |
| static Type * | getMinimumFPType (Value *V, bool PreferBFloat) |
| Find the minimum FP type we can safely truncate to. | |
| static bool | isKnownExactCastIntToFP (CastInst &I, InstCombinerImpl &IC) |
| Return true if the cast from integer to FP can be proven to be exact for all possible inputs (the conversion does not lose any precision). | |
| static Instruction * | foldFPtoI (Instruction &FI, InstCombiner &IC) |
| static Instruction * | optimizeVectorResizeWithIntegerBitCasts (Value *InVal, VectorType *DestTy, InstCombinerImpl &IC) |
| This input value (which is known to have vector type) is being zero extended or truncated to the specified vector type. | |
| 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. | |
| static Value * | optimizeIntegerToVectorInsertions (BitCastInst &CI, InstCombinerImpl &IC) |
| If the input is an 'or' instruction, we may be doing shifts and ors to assemble the elements of the vector manually. | |
| static Instruction * | canonicalizeBitCastExtElt (BitCastInst &BitCast, InstCombinerImpl &IC) |
| Canonicalize scalar bitcasts of extracted elements into a bitcast of the vector followed by extract element. | |
| static Instruction * | foldBitCastBitwiseLogic (BitCastInst &BitCast, InstCombiner::BuilderTy &Builder) |
| Change the type of a bitwise logic operation if we can eliminate a bitcast. | |
| static Instruction * | foldBitCastSelect (BitCastInst &BitCast, InstCombiner::BuilderTy &Builder) |
| Change the type of a select if we can eliminate a bitcast. | |
| static bool | hasStoreUsersOnly (CastInst &CI) |
| Check if all users of CI are StoreInsts. | |
| static Value * | foldCopySignIdioms (BitCastInst &CI, InstCombiner::BuilderTy &Builder, const SimplifyQuery &SQ) |
| Fold (bitcast (or (and (bitcast X to int), signmask), nneg Y) to fp) to copysign((bitcast Y to fp), X) | |
| #define DEBUG_TYPE "instcombine" |
Definition at line 35 of file InstCombineCasts.cpp.
| using EvaluatedMap = SmallDenseMap<Value *, Value *, 8> |
Definition at line 37 of file InstCombineCasts.cpp.
|
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 2739 of file InstCombineCasts.cpp.
References llvm::InstCombiner::Builder, llvm::cast(), llvm::CastInst::Create(), llvm::ExtractElementInst::Create(), llvm::IRBuilderBase::CreateBitCast(), llvm::dyn_cast(), llvm::VectorType::get(), llvm::User::getOperand(), llvm::Value::getType(), llvm::VectorType::isValidElementType(), llvm::Type::isVectorTy(), llvm::PatternMatch::m_ExtractElt(), llvm::MIPatternMatch::m_OneUse(), llvm::PatternMatch::m_Value(), and llvm::PatternMatch::match().
Referenced by llvm::InstCombinerImpl::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 2591 of file InstCombineCasts.cpp.
References assert(), llvm::CallingConv::C, collectInsertionElements(), llvm::ConstantFoldBinaryInstruction(), llvm::dyn_cast(), llvm::IntegerType::get(), llvm::ConstantExpr::getBitCast(), llvm::Type::getPrimitiveSizeInBits(), llvm::ConstantExpr::getTrunc(), getTypeSizeIndex(), llvm::ConstantInt::getZExtValue(), I, llvm::isa(), isBigEndian(), and isMultipleOfTypeSize().
Referenced by collectInsertionElements(), and optimizeIntegerToVectorInsertions().
|
static |
Definition at line 39 of file InstCombineCasts.cpp.
References llvm::PHINode::addIncoming(), llvm::CallingConv::C, llvm::cast(), llvm::ConstantFoldIntegerCast(), llvm::BinaryOperator::Create(), llvm::CallInst::Create(), llvm::CastInst::Create(), llvm::PHINode::Create(), llvm::SelectInst::Create(), llvm::CastInst::CreateIntegerCast(), llvm::dyn_cast(), EvaluateInDifferentTypeImpl(), llvm::VectorType::get(), llvm::InstCombiner::getDataLayout(), llvm::Function::getFunctionType(), llvm::PHINode::getIncomingBlock(), llvm::PHINode::getIncomingValue(), llvm::PHINode::getNumIncomingValues(), llvm::Intrinsic::getOrInsertDeclaration(), I, II, llvm::InstCombiner::InsertNewInstWith(), isSigned(), LHS, llvm_unreachable, llvm::DenseMapBase< DerivedT, KeyT, ValueT, KeyInfoT, BucketT >::lookup(), Opc, RHS, llvm::Instruction::setIsExact(), and llvm::Value::takeName().
Referenced by llvm::InstCombinerImpl::EvaluateInDifferentType(), and EvaluateInDifferentTypeImpl().
|
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 1895 of file InstCombineCasts.cpp.
References F, 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 2766 of file InstCombineCasts.cpp.
References llvm::CallingConv::C, llvm::BinaryOperator::Create(), llvm::CastInst::CreateBitOrPointerCast(), llvm::BinaryOperator::getOpcode(), llvm::User::getOperand(), llvm::Value::getType(), llvm::isa(), llvm::Instruction::isBitwiseLogicOp(), llvm::Type::isFPOrFPVectorTy(), llvm::Type::isIntOrIntVectorTy(), llvm::Type::isVectorTy(), llvm::PatternMatch::m_BinOp(), llvm::PatternMatch::m_BitCast(), llvm::PatternMatch::m_Constant(), llvm::MIPatternMatch::m_OneUse(), llvm::PatternMatch::m_Value(), llvm::PatternMatch::match(), X, and Y.
Referenced by llvm::InstCombinerImpl::visitBitCast().
|
static |
Change the type of a select if we can eliminate a bitcast.
Definition at line 2838 of file InstCombineCasts.cpp.
References llvm::cast(), Cond, llvm::SelectInst::Create(), llvm::dyn_cast(), llvm::User::getOperand(), llvm::Value::getType(), llvm::isa(), llvm::Type::isVectorTy(), llvm::PatternMatch::m_BitCast(), llvm::MIPatternMatch::m_OneUse(), llvm::PatternMatch::m_Select(), llvm::PatternMatch::m_Value(), llvm::PatternMatch::match(), and X.
Referenced by llvm::InstCombinerImpl::visitBitCast().
|
static |
Fold (bitcast (or (and (bitcast X to int), signmask), nneg Y) to fp) to copysign((bitcast Y to fp), X)
Definition at line 3067 of file InstCombineCasts.cpp.
References llvm::Value::getType(), llvm::Type::isFPOrFPVectorTy(), llvm::isKnownNonNegative(), llvm::PatternMatch::m_And(), llvm::PatternMatch::m_c_Or(), llvm::PatternMatch::m_ElementWiseBitCast(), llvm::PatternMatch::m_SignMask(), llvm::PatternMatch::m_Value(), llvm::PatternMatch::match(), X, and Y.
Referenced by llvm::InstCombinerImpl::visitBitCast().
|
static |
Definition at line 2293 of file InstCombineCasts.cpp.
References llvm::computeKnownFPClass(), llvm::fcNormal, llvm::fcPosNormal, llvm::Constant::getNullValue(), llvm::Instruction::getOpcode(), llvm::User::getOperand(), llvm::InstCombiner::getSimplifyQuery(), llvm::Value::getType(), llvm::SimplifyQuery::getWithInstruction(), llvm::KnownFPClass::isKnownNever(), and llvm::InstCombiner::replaceInstUsesWith().
Referenced by llvm::InstCombinerImpl::visitFPToSI(), and llvm::InstCombinerImpl::visitFPToUI().
|
static |
Whenever an element is extracted from a vector, optionally shifted down, and then truncated, canonicalize by converting it to a bitcast followed by an extractelement.
Examples (little endian): trunc (extractelement <4 x i64> X, 0) to i32 ---> extractelement <8 x i32> (bitcast <4 x i64> X to <8 x i32>), i32 0
trunc (lshr (extractelement <4 x i32> X, 0), 8) to i8 ---> extractelement <16 x i8> (bitcast <4 x i32> X to <16 x i8>), i32 1
Definition at line 674 of file InstCombineCasts.cpp.
References assert(), llvm::InstCombiner::Builder, llvm::cast(), llvm::ExtractElementInst::Create(), llvm::IRBuilderBase::CreateBitCast(), llvm::VectorType::get(), llvm::InstCombiner::getDataLayout(), llvm::IRBuilderBase::getInt32(), llvm::User::getOperand(), llvm::Type::getScalarSizeInBits(), llvm::Value::getType(), llvm::APInt::getZExtValue(), llvm::ConstantInt::getZExtValue(), llvm::DataLayout::isBigEndian(), llvm::PatternMatch::m_APInt(), llvm::PatternMatch::m_ConstantInt(), llvm::PatternMatch::m_ExtractElt(), llvm::PatternMatch::m_LShr(), llvm::MIPatternMatch::m_OneUse(), llvm::PatternMatch::m_Value(), llvm::PatternMatch::match(), llvm::APInt::udiv(), llvm::APInt::uge(), and llvm::APInt::urem().
Referenced by llvm::InstCombinerImpl::visitTrunc().
|
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 624 of file InstCombineCasts.cpp.
References llvm::InstCombiner::Builder, llvm::cast(), llvm::ExtractElementInst::Create(), llvm::IRBuilderBase::CreateBitCast(), llvm::FixedVectorType::get(), llvm::InstCombiner::getDataLayout(), llvm::IRBuilderBase::getInt32(), llvm::User::getOperand(), llvm::Type::getPrimitiveSizeInBits(), llvm::Value::getType(), llvm::ConstantInt::getZExtValue(), llvm::isa(), llvm::DataLayout::isBigEndian(), llvm::PatternMatch::m_BitCast(), llvm::PatternMatch::m_CombineOr(), llvm::PatternMatch::m_ConstantInt(), llvm::PatternMatch::m_LShr(), llvm::PatternMatch::m_Value(), and llvm::PatternMatch::match().
Referenced by llvm::InstCombinerImpl::visitTrunc().
Find the minimum FP type we can safely truncate to.
Definition at line 1972 of file InstCombineCasts.cpp.
References llvm::dyn_cast(), llvm::dyn_cast_or_null(), llvm::VectorType::get(), shrinkFPConstant(), shrinkFPConstantVector(), llvm::Splat, and T.
Referenced by llvm::InstCombinerImpl::visitFPTrunc().
Definition at line 2577 of file InstCombineCasts.cpp.
Referenced by collectInsertionElements().
Check if all users of CI are StoreInsts.
Definition at line 2881 of file InstCombineCasts.cpp.
References llvm::isa(), and llvm::Value::users().
|
static |
Return true if the cast from integer to FP can be proven to be exact for all possible inputs (the conversion does not lose any precision).
Definition at line 2000 of file InstCombineCasts.cpp.
References assert(), llvm::InstCombiner::computeKnownBits(), llvm::KnownBits::countMinLeadingZeros(), llvm::KnownBits::countMinTrailingZeros(), F, llvm::Type::getFPMantissaWidth(), I, llvm::PatternMatch::m_FPToSI(), llvm::PatternMatch::m_FPToUI(), llvm::PatternMatch::m_Value(), and llvm::PatternMatch::match().
Referenced by llvm::InstCombinerImpl::foldItoFPtoI(), llvm::InstCombinerImpl::visitFPExt(), and llvm::InstCombinerImpl::visitFPTrunc().
Definition at line 2573 of file InstCombineCasts.cpp.
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 2705 of file InstCombineCasts.cpp.
References llvm::InstCombiner::Builder, llvm::cast(), collectInsertionElements(), llvm::IRBuilderBase::CreateInsertElement(), llvm::InstCombiner::getDataLayout(), llvm::IRBuilderBase::getInt32(), llvm::Constant::getNullValue(), llvm::User::getOperand(), llvm::Value::getType(), llvm::isa(), and llvm::DataLayout::isBigEndian().
Referenced by llvm::InstCombinerImpl::visitBitCast().
|
static |
This input value (which is known to have vector type) is being zero extended or truncated to the specified vector type.
Since the zext/trunc is done using an integer type, we have a (bitcast(cast(bitcast))) pattern, endianness will impact which end of the vector that is extended or truncated.
A vector is always stored with index 0 at the lowest address, which corresponds to the most significant bits for a big endian stored integer and the least significant bits for little endian. A trunc/zext of an integer impacts the big end of the integer. Thus, we need to add/remove elements at the front of the vector for big endian targets, and the back of the vector for little endian targets.
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 2503 of file InstCombineCasts.cpp.
References assert(), llvm::InstCombiner::Builder, llvm::cast(), llvm::IRBuilderBase::CreateBitCast(), llvm::FixedVectorType::get(), llvm::PoisonValue::get(), llvm::InstCombiner::getDataLayout(), llvm::Constant::getNullValue(), llvm::Value::getType(), llvm::DataLayout::isBigEndian(), llvm::seq(), and llvm::to_vector().
Referenced by llvm::InstCombinerImpl::visitBitCast().
|
static |
Definition at line 1921 of file InstCombineCasts.cpp.
References llvm::dyn_cast(), llvm::VectorType::get(), llvm::Value::getContext(), llvm::Value::getType(), llvm::ConstantFP::getValueAPF(), and shrinkFPConstant().
|
static |
Definition at line 1901 of file InstCombineCasts.cpp.
References llvm::APFloatBase::BFloat(), F, fitsInFPType(), llvm::Type::getBFloatTy(), llvm::Type::getDoubleTy(), llvm::Type::getFloatTy(), llvm::Type::getHalfTy(), llvm::APFloatBase::IEEEdouble(), llvm::APFloatBase::IEEEhalf(), and llvm::APFloatBase::IEEEsingle().
Referenced by getMinimumFPType(), shrinkFPConstant(), and shrinkFPConstantVector().
Definition at line 1937 of file InstCombineCasts.cpp.
References llvm::dyn_cast(), llvm::dyn_cast_or_null(), llvm::FixedVectorType::get(), llvm::Type::getFPMantissaWidth(), llvm::isa(), shrinkFPConstant(), and T.
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 943 of file InstCombineCasts.cpp.
References assert(), llvm::InsertElementInst::Create(), llvm::dyn_cast(), llvm::UndefValue::get(), llvm::CastInst::getOpcode(), llvm::User::getOperand(), llvm::Type::getScalarType(), llvm::Value::getType(), llvm::PatternMatch::m_Undef(), and llvm::PatternMatch::match().
Referenced by llvm::InstCombinerImpl::visitFPTrunc(), and llvm::InstCombinerImpl::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 919 of file InstCombineCasts.cpp.
References llvm::all_equal(), llvm::cast(), llvm::dyn_cast(), llvm::User::getOperand(), llvm::Type::getScalarType(), llvm::Value::getType(), llvm::Type::getWithNewType(), llvm::details::FixedOrScalableQuantity< ElementCount, unsigned >::isKnownGE(), llvm::PatternMatch::m_Undef(), and llvm::PatternMatch::match().
Referenced by llvm::InstCombinerImpl::visitTrunc().