LLVM 20.0.0git
|
#include "InstCombineInternal.h"
#include "llvm/ADT/MapVector.h"
#include "llvm/ADT/SetOperations.h"
#include "llvm/ADT/SmallString.h"
#include "llvm/ADT/Statistic.h"
#include "llvm/Analysis/AliasAnalysis.h"
#include "llvm/Analysis/Loads.h"
#include "llvm/IR/DataLayout.h"
#include "llvm/IR/DebugInfoMetadata.h"
#include "llvm/IR/IntrinsicInst.h"
#include "llvm/IR/LLVMContext.h"
#include "llvm/IR/PatternMatch.h"
#include "llvm/Transforms/InstCombine/InstCombiner.h"
#include "llvm/Transforms/Utils/Local.h"
Go to the source code of this file.
Namespaces | |
namespace | llvm |
This is an optimization pass for GlobalISel generic memory operations. | |
Macros | |
#define | DEBUG_TYPE "instcombine" |
Functions | |
STATISTIC (NumDeadStore, "Number of dead stores eliminated") | |
STATISTIC (NumGlobalCopies, "Number of allocas copied from constant global") | |
cl::opt< bool > | llvm::EnableInferAlignmentPass ("enable-infer-alignment-pass", cl::init(true), cl::Hidden, cl::ZeroOrMore, cl::desc("Enable the InferAlignment pass, disabling alignment inference in " "InstCombine")) |
static bool | isOnlyCopiedFromConstantMemory (AAResults *AA, AllocaInst *V, MemTransferInst *&TheCopy, SmallVectorImpl< Instruction * > &ToDelete) |
isOnlyCopiedFromConstantMemory - Recursively walk the uses of a (derived) pointer to an alloca. | |
static MemTransferInst * | isOnlyCopiedFromConstantMemory (AAResults *AA, AllocaInst *AI, SmallVectorImpl< Instruction * > &ToDelete) |
isOnlyCopiedFromConstantMemory - Return true if the specified alloca is only modified by a copy from a constant memory location. | |
static bool | isDereferenceableForAllocaSize (const Value *V, const AllocaInst *AI, const DataLayout &DL) |
Returns true if V is dereferenceable for size of alloca. | |
static Instruction * | simplifyAllocaArraySize (InstCombinerImpl &IC, AllocaInst &AI, DominatorTree &DT) |
static bool | isSupportedAtomicType (Type *Ty) |
static StoreInst * | combineStoreToNewValue (InstCombinerImpl &IC, StoreInst &SI, Value *V) |
Combine a store to a new type. | |
static Instruction * | combineLoadToOperationType (InstCombinerImpl &IC, LoadInst &Load) |
Combine loads to match the type of their uses' value after looking through intervening bitcasts. | |
static Instruction * | unpackLoadToAggregate (InstCombinerImpl &IC, LoadInst &LI) |
static bool | isObjectSizeLessThanOrEq (Value *V, uint64_t MaxSize, const DataLayout &DL) |
static bool | canReplaceGEPIdxWithZero (InstCombinerImpl &IC, GetElementPtrInst *GEPI, Instruction *MemI, unsigned &Idx) |
static Instruction * | replaceGEPIdxWithZero (InstCombinerImpl &IC, Value *Ptr, Instruction &MemI) |
static bool | canSimplifyNullStoreOrGEP (StoreInst &SI) |
static bool | canSimplifyNullLoadOrGEP (LoadInst &LI, Value *Op) |
static Value * | likeBitCastFromVector (InstCombinerImpl &IC, Value *V) |
Look for extractelement/insertvalue sequence that acts like a bitcast. | |
static bool | combineStoreToValueType (InstCombinerImpl &IC, StoreInst &SI) |
Combine stores to match the type of value being stored. | |
static bool | unpackStoreToAggregate (InstCombinerImpl &IC, StoreInst &SI) |
static bool | equivalentAddressValues (Value *A, Value *B) |
equivalentAddressValues - Test if A and B will obviously have the same value. | |
Variables | |
static cl::opt< unsigned > | MaxCopiedFromConstantUsers ("instcombine-max-copied-from-constant-users", cl::init(300), cl::desc("Maximum users to visit in copy from constant transform"), cl::Hidden) |
#define DEBUG_TYPE "instcombine" |
Definition at line 30 of file InstCombineLoadStoreAlloca.cpp.
|
static |
Definition at line 888 of file InstCombineLoadStoreAlloca.cpp.
References llvm::InstCombiner::computeKnownBits(), DL, llvm::InstCombiner::getDataLayout(), llvm::GetElementPtrInst::getIndexedType(), llvm::User::getNumOperands(), llvm::User::getOperand(), llvm::GetElementPtrInst::getSourceElementType(), I, Idx, llvm::GetElementPtrInst::idx_begin(), llvm::GetElementPtrInst::isInBounds(), llvm::KnownBits::isNonNegative(), isObjectSizeLessThanOrEq(), llvm::Type::isScalableTy(), and llvm::Type::isSized().
Referenced by replaceGEPIdxWithZero().
Definition at line 990 of file InstCombineLoadStoreAlloca.cpp.
References llvm::Instruction::getFunction(), llvm::LoadInst::getPointerAddressSpace(), and llvm::NullPointerIsDefined().
Referenced by llvm::InstCombinerImpl::visitLoadInst().
Definition at line 979 of file InstCombineLoadStoreAlloca.cpp.
References llvm::NullPointerIsDefined(), and Ptr.
Referenced by llvm::InstCombinerImpl::visitStoreInst().
|
static |
Combine loads to match the type of their uses' value after looking through intervening bitcasts.
The core idea here is that if the result of a load is used in an operation, we should load the type most conducive to that operation. For example, when loading an integer and converting that immediately to a pointer, we should instead directly load a pointer.
However, this routine must never change the width of a load or the number of loads as that would introduce a semantic change. This combine is expected to be a semantic no-op which just allows loads to more closely model the types of their consuming operations.
Currently, we also refuse to change the precise type used for an atomic load or a volatile load. This is debatable, and might be reasonable to change later. However, it is risky in case some backend or other part of LLVM is relying on the exact type loaded to select appropriate atomic operations.
Definition at line 646 of file InstCombineLoadStoreAlloca.cpp.
References assert(), llvm::InstCombinerImpl::combineLoadToNewType(), llvm::InstCombinerImpl::eraseInstFromFunction(), llvm::InstCombiner::getDataLayout(), llvm::Type::isPtrOrPtrVectorTy(), isSupportedAtomicType(), llvm::Type::isX86_AMXTy(), and llvm::Value::replaceAllUsesWith().
Referenced by llvm::InstCombinerImpl::visitLoadInst().
|
static |
Combine a store to a new type.
Returns the newly created store instruction.
Definition at line 576 of file InstCombineLoadStoreAlloca.cpp.
References assert(), llvm::InstCombiner::Builder, llvm::IRBuilderBase::CreateAlignedStore(), isSupportedAtomicType(), N, Ptr, llvm::StoreInst::setAtomic(), and llvm::Instruction::setMetadata().
Referenced by combineStoreToValueType(), and unpackStoreToAggregate().
|
static |
Combine stores to match the type of value being stored.
The core idea here is that the memory does not have any intrinsic type and where we can we should match the type of a store to the type of value being stored.
However, this routine must never change the width of a store or the number of stores as that would introduce a semantic change. This combine is expected to be a semantic no-op which just allows stores to more closely model the types of their incoming values.
Currently, we also refuse to change the precise type used for an atomic or volatile store. This is debatable, and might be reasonable to change later. However, it is risky in case some backend or other part of LLVM is relying on the exact type stored to select appropriate atomic operations.
Definition at line 1177 of file InstCombineLoadStoreAlloca.cpp.
References assert(), combineStoreToNewValue(), isSupportedAtomicType(), and likeBitCastFromVector().
Referenced by llvm::InstCombinerImpl::visitStoreInst().
equivalentAddressValues - Test if A and B will obviously have the same value.
This includes recognizing that t0 and t1 will have the same value in code like this: t0 = getelementptr @a, 0, 3 store i32 0, i32* t0 t1 = getelementptr @a, 0, 3 t2 = load i32* t1
Definition at line 1332 of file InstCombineLoadStoreAlloca.cpp.
Referenced by llvm::InstCombinerImpl::visitStoreInst().
|
static |
Returns true if V is dereferenceable for size of alloca.
Definition at line 187 of file InstCombineLoadStoreAlloca.cpp.
References DL, llvm::AllocaInst::getAlign(), llvm::AllocaInst::getAllocatedType(), llvm::AllocaInst::isArrayAllocation(), and llvm::isDereferenceableAndAlignedPointer().
Referenced by llvm::InstCombinerImpl::visitAllocaInst().
|
static |
Definition at line 806 of file InstCombineLoadStoreAlloca.cpp.
References llvm::append_range(), DL, llvm::SmallVectorBase< Size_T >::empty(), llvm::details::FixedOrScalableQuantity< LeafTy, ValueTy >::getFixedValue(), llvm::ConstantInt::getValue(), llvm::SmallPtrSetImpl< PtrType >::insert(), llvm::details::FixedOrScalableQuantity< LeafTy, ValueTy >::isScalable(), P, llvm::SmallVectorImpl< T >::pop_back_val(), llvm::SmallVectorTemplateBase< T, bool >::push_back(), and llvm::APInt::zext().
Referenced by canReplaceGEPIdxWithZero().
|
static |
isOnlyCopiedFromConstantMemory - Return true if the specified alloca is only modified by a copy from a constant memory location.
If we can prove this, we can replace any uses of the alloca with uses of the memory location directly.
Definition at line 177 of file InstCombineLoadStoreAlloca.cpp.
References isOnlyCopiedFromConstantMemory().
|
static |
isOnlyCopiedFromConstantMemory - Recursively walk the uses of a (derived) pointer to an alloca.
Ignore any reads of the pointer, return false if we see any stores or other unknown uses. If we see pointer arithmetic, keep track of whether it moves the pointer (with IsOffset) but otherwise traverse the uses. If we see a memcpy/memmove that targets an unoffseted pointer to the alloca, and if the source pointer is a pointer to a constant memory location, we can optimize this.
Definition at line 55 of file InstCombineLoadStoreAlloca.cpp.
References assert(), llvm::SmallVectorImpl< T >::emplace_back(), llvm::SmallVectorBase< Size_T >::empty(), GEP, llvm::AAResults::getModRefInfoMask(), I, llvm::SmallPtrSetImpl< PtrType >::insert(), llvm::isModSet(), MaxCopiedFromConstantUsers, MI, llvm::SmallVectorImpl< T >::pop_back_val(), llvm::SmallVectorTemplateBase< T, bool >::push_back(), llvm::SmallPtrSetImplBase::size(), and llvm::Value::uses().
Referenced by isOnlyCopiedFromConstantMemory(), and llvm::InstCombinerImpl::visitAllocaInst().
Definition at line 547 of file InstCombineLoadStoreAlloca.cpp.
References llvm::Type::isFloatingPointTy(), and llvm::Type::isIntOrPtrTy().
Referenced by llvm::InstCombinerImpl::combineLoadToNewType(), combineLoadToOperationType(), combineStoreToNewValue(), and combineStoreToValueType().
|
static |
Look for extractelement/insertvalue sequence that acts like a bitcast.
For example, if we have:
%E0 = extractelement <2 x double> %U, i32 0 %V0 = insertvalue [2 x double] undef, double %E0, 0 %E1 = extractelement <2 x double> %U, i32 1 %V1 = insertvalue [2 x double] %V0, double %E1, 1
and the layout of a <2 x double> is isomorphic to a [2 x double], then V1 can be safely approximated by a conceptual "bitcast" of U. Note that U may contain non-undef values where V1 has undef.
Definition at line 1116 of file InstCombineLoadStoreAlloca.cpp.
References DL, llvm::InstCombiner::getDataLayout(), IV, llvm::PatternMatch::m_Undef(), and llvm::PatternMatch::match().
Referenced by combineStoreToValueType().
|
static |
Definition at line 963 of file InstCombineLoadStoreAlloca.cpp.
References canReplaceGEPIdxWithZero(), llvm::Instruction::clone(), llvm::ilist_node_impl< OptionsT >::getIterator(), Idx, llvm::InstCombiner::InsertNewInstBefore(), Ptr, and llvm::User::setOperand().
Referenced by llvm::InstCombinerImpl::visitLoadInst(), and llvm::InstCombinerImpl::visitStoreInst().
|
static |
Definition at line 198 of file InstCombineLoadStoreAlloca.cpp.
References llvm::InstCombiner::Builder, llvm::CallingConv::C, llvm::IRBuilderBase::CreateAlloca(), llvm::IRBuilderBase::CreateIntCast(), llvm::AllocaInst::getAddressSpace(), llvm::AllocaInst::getAlign(), llvm::AllocaInst::getAllocatedType(), llvm::AllocaInst::getArraySize(), llvm::InstCombiner::getDataLayout(), llvm::DataLayout::getIndexType(), llvm::IRBuilderBase::getInt32(), llvm::Value::getName(), llvm::Constant::getNullValue(), llvm::AllocaInst::getType(), llvm::Value::getType(), llvm::AllocaInst::isArrayAllocation(), llvm::Type::isIntegerTy(), llvm::AllocaInst::isUsedWithInAlloca(), llvm::replaceAllDbgUsesWith(), llvm::InstCombiner::replaceInstUsesWith(), and llvm::InstCombiner::replaceOperand().
Referenced by llvm::InstCombinerImpl::visitAllocaInst().
STATISTIC | ( | NumDeadStore | , |
"Number of dead stores eliminated" | |||
) |
STATISTIC | ( | NumGlobalCopies | , |
"Number of allocas copied from constant global" | |||
) |
|
static |
Definition at line 691 of file InstCombineLoadStoreAlloca.cpp.
References Addr, llvm::InstCombiner::Builder, llvm::InstCombinerImpl::combineLoadToNewType(), llvm::commonAlignment(), llvm::IRBuilderBase::CreateAlignedLoad(), llvm::IRBuilderBase::CreateInBoundsGEP(), llvm::IRBuilderBase::CreateInsertValue(), DL, llvm::PoisonValue::get(), llvm::Instruction::getAAMetadata(), llvm::LoadInst::getAlign(), llvm::InstCombiner::getDataLayout(), llvm::Type::getInt32Ty(), llvm::Type::getInt64Ty(), llvm::Value::getName(), llvm::LoadInst::getPointerOperand(), llvm::Value::getType(), llvm::TypeSize::getZero(), llvm::LoadInst::isSimple(), llvm::InstCombiner::MaxArraySizeForCombine, Name, llvm::Offset, Ptr, llvm::InstCombiner::replaceInstUsesWith(), and llvm::Instruction::setAAMetadata().
Referenced by llvm::InstCombinerImpl::visitLoadInst().
|
static |
Definition at line 1215 of file InstCombineLoadStoreAlloca.cpp.
References Addr, llvm::InstCombiner::Builder, combineStoreToNewValue(), llvm::commonAlignment(), llvm::IRBuilderBase::CreateAlignedStore(), llvm::IRBuilderBase::CreateExtractValue(), llvm::IRBuilderBase::CreateInBoundsGEP(), DL, llvm::InstCombiner::getDataLayout(), llvm::Type::getInt32Ty(), llvm::Type::getInt64Ty(), llvm::TypeSize::getZero(), llvm::InstCombiner::MaxArraySizeForCombine, llvm::Offset, Ptr, and llvm::Instruction::setAAMetadata().
Referenced by llvm::InstCombinerImpl::visitStoreInst().
|
static |
Referenced by isOnlyCopiedFromConstantMemory().