LLVM
15.0.0git
|
#include "InstCombineInternal.h"
#include "llvm/ADT/MapVector.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.
Macros | |
#define | DEBUG_TYPE "instcombine" |
Functions | |
STATISTIC (NumDeadStore, "Number of dead stores eliminated") | |
STATISTIC (NumGlobalCopies, "Number of allocas copied from constant global") | |
static bool | isOnlyCopiedFromConstantMemory (AAResults *AA, Value *V, MemTransferInst *&TheCopy, SmallVectorImpl< Instruction * > &ToDelete) |
isOnlyCopiedFromConstantGlobal - Recursively walk the uses of a (derived) pointer to an alloca. More... | |
static MemTransferInst * | isOnlyCopiedFromConstantMemory (AAResults *AA, AllocaInst *AI, SmallVectorImpl< Instruction * > &ToDelete) |
isOnlyCopiedFromConstantGlobal - Return true if the specified alloca is only modified by a copy from a constant global. More... | |
static bool | isDereferenceableForAllocaSize (const Value *V, const AllocaInst *AI, const DataLayout &DL) |
Returns true if V is dereferenceable for size of alloca. More... | |
static Instruction * | simplifyAllocaArraySize (InstCombinerImpl &IC, AllocaInst &AI) |
static bool | isSupportedAtomicType (Type *Ty) |
static StoreInst * | combineStoreToNewValue (InstCombinerImpl &IC, StoreInst &SI, Value *V) |
Combine a store to a new type. More... | |
static bool | isMinMaxWithLoads (Value *V, Type *&LoadTy) |
Returns true if instruction represent minmax pattern like: select ((cmp load V1, load V2), V1, V2). More... | |
static Instruction * | combineLoadToOperationType (InstCombinerImpl &IC, LoadInst &LI) |
Combine loads to match the type of their uses' value after looking through intervening bitcasts. More... | |
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) |
template<typename T > | |
static Instruction * | replaceGEPIdxWithZero (InstCombinerImpl &IC, Value *Ptr, T &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. More... | |
static bool | combineStoreToValueType (InstCombinerImpl &IC, StoreInst &SI) |
Combine stores to match the type of value being stored. More... | |
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. More... | |
static bool | removeBitcastsFromLoadStoreOnMinMax (InstCombinerImpl &IC, StoreInst &SI) |
Converts store (bitcast (load (bitcast (select ...)))) to store (load (select ...)), where select is minmax: select ((cmp load V1, load V2), V1, V2). More... | |
#define DEBUG_TYPE "instcombine" |
Definition at line 29 of file InstCombineLoadStoreAlloca.cpp.
|
static |
Definition at line 811 of file InstCombineLoadStoreAlloca.cpp.
References llvm::InstCombinerImpl::computeKnownBits(), DL, llvm::numbers::e, llvm::InstCombiner::getDataLayout(), llvm::GetElementPtrInst::getIndexedType(), llvm::User::getNumOperands(), llvm::User::getOperand(), llvm::GetElementPtrInst::getSourceElementType(), i, I, llvm::GetElementPtrInst::idx_begin(), llvm::ARM_PROC::IE, llvm::GetElementPtrInst::isInBounds(), llvm::KnownBits::isNonNegative(), isObjectSizeLessThanOrEq(), and llvm::Type::isSized().
Referenced by replaceGEPIdxWithZero().
Definition at line 915 of file InstCombineLoadStoreAlloca.cpp.
References llvm::Instruction::getFunction(), llvm::LoadInst::getPointerAddressSpace(), and llvm::NullPointerIsDefined().
Referenced by llvm::InstCombinerImpl::visitLoadInst().
|
static |
Definition at line 904 of file InstCombineLoadStoreAlloca.cpp.
References llvm::NullPointerIsDefined(), and SI.
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 577 of file InstCombineLoadStoreAlloca.cpp.
References assert(), llvm::InstCombinerImpl::combineLoadToNewType(), DL, llvm::InstCombinerImpl::eraseInstFromFunction(), llvm::InstCombiner::getDataLayout(), llvm::LoadInst::getPointerOperand(), llvm::Value::getType(), llvm::Value::hasOneUse(), llvm::Instruction::isAtomic(), llvm::Type::isPtrOrPtrVectorTy(), isSupportedAtomicType(), llvm::Value::isSwiftError(), llvm::LoadInst::isUnordered(), llvm::Type::isX86_AMXTy(), llvm::Value::replaceAllUsesWith(), llvm::Value::use_empty(), and llvm::Instruction::user_back().
Referenced by llvm::InstCombinerImpl::visitLoadInst().
|
static |
Combine a store to a new type.
Returns the newly created store instruction.
Definition at line 483 of file InstCombineLoadStoreAlloca.cpp.
References assert(), llvm::InstCombiner::Builder, llvm::IRBuilderBase::CreateAlignedStore(), llvm::IRBuilderBase::CreateBitCast(), llvm::Type::getPointerTo(), llvm::Value::getType(), isSupportedAtomicType(), N, llvm::StoreInst::setAtomic(), llvm::Instruction::setMetadata(), and SI.
Referenced by combineStoreToValueType(), removeBitcastsFromLoadStoreOnMinMax(), 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 1105 of file InstCombineLoadStoreAlloca.cpp.
References assert(), combineStoreToNewValue(), llvm::Value::getType(), isSupportedAtomicType(), llvm::Type::isX86_AMXTy(), likeBitCastFromVector(), and SI.
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 1255 of file InstCombineLoadStoreAlloca.cpp.
References B.
Referenced by llvm::InstCombinerImpl::visitStoreInst().
|
static |
Returns true if V is dereferenceable for size of alloca.
Definition at line 156 of file InstCombineLoadStoreAlloca.cpp.
References DL, llvm::AllocaInst::getAlign(), llvm::AllocaInst::getAllocatedType(), llvm::AllocaInst::isArrayAllocation(), and llvm::isDereferenceableAndAlignedPointer().
Referenced by llvm::InstCombinerImpl::visitAllocaInst().
Returns true if instruction represent minmax pattern like: select ((cmp load V1, load V2), V1, V2).
Definition at line 539 of file InstCombineLoadStoreAlloca.cpp.
References assert(), llvm::Value::getType(), llvm::Type::isPointerTy(), L2, LHS, llvm::PatternMatch::m_Cmp(), llvm::PatternMatch::m_Instruction(), llvm::PatternMatch::m_Load(), llvm::PatternMatch::m_Select(), llvm::PatternMatch::m_Specific(), llvm::PatternMatch::m_Value(), llvm::PatternMatch::match(), llvm::InstCombiner::peekThroughBitcast(), and RHS.
Referenced by removeBitcastsFromLoadStoreOnMinMax().
|
static |
Definition at line 732 of file InstCombineLoadStoreAlloca.cpp.
References llvm::append_range(), DL, llvm::ConstantInt::getValue(), llvm::SmallPtrSetImpl< PtrType >::insert(), P, llvm::SmallVectorImpl< T >::pop_back_val(), SI, and llvm::APInt::zext().
Referenced by canReplaceGEPIdxWithZero().
|
static |
isOnlyCopiedFromConstantGlobal - Return true if the specified alloca is only modified by a copy from a constant global.
If we can prove this, we can replace any uses of the alloca with uses of the global directly.
Definition at line 146 of file InstCombineLoadStoreAlloca.cpp.
References isOnlyCopiedFromConstantMemory().
|
static |
isOnlyCopiedFromConstantGlobal - 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 global, we can optimize this.
Definition at line 42 of file InstCombineLoadStoreAlloca.cpp.
References assert(), llvm::SmallVectorImpl< T >::emplace_back(), GEP, I, MI, and llvm::SmallVectorImpl< T >::pop_back_val().
Referenced by isOnlyCopiedFromConstantMemory(), and llvm::InstCombinerImpl::visitAllocaInst().
|
static |
Definition at line 447 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 1044 of file InstCombineLoadStoreAlloca.cpp.
References DL, E, llvm::InstCombiner::getDataLayout(), llvm::Value::getType(), IV, llvm::PatternMatch::m_Undef(), llvm::PatternMatch::match(), llvm::ARM_MB::ST, and llvm::RISCVFenceField::W.
Referenced by combineStoreToValueType().
|
static |
Converts store (bitcast (load (bitcast (select ...)))) to store (load (select ...)), where select is minmax: select ((cmp load V1, load V2), V1, V2).
Definition at line 1279 of file InstCombineLoadStoreAlloca.cpp.
References llvm::all_of(), llvm::InstCombiner::Builder, llvm::InstCombinerImpl::combineLoadToNewType(), combineStoreToNewValue(), DL, llvm::InstCombinerImpl::eraseInstFromFunction(), llvm::PoisonValue::get(), llvm::InstCombiner::getDataLayout(), isMinMaxWithLoads(), llvm::PatternMatch::m_BitCast(), llvm::PatternMatch::m_Load(), llvm::PatternMatch::m_Value(), llvm::PatternMatch::match(), llvm::InstCombinerImpl::replaceInstUsesWith(), llvm::IRBuilderBase::SetInsertPoint(), and SI.
Referenced by llvm::InstCombinerImpl::visitStoreInst().
|
static |
Definition at line 887 of file InstCombineLoadStoreAlloca.cpp.
References canReplaceGEPIdxWithZero(), llvm::Instruction::clone(), llvm::ConstantInt::get(), llvm::Instruction::insertBefore(), and llvm::User::setOperand().
Referenced by llvm::InstCombinerImpl::visitLoadInst(), and llvm::InstCombinerImpl::visitStoreInst().
|
static |
Definition at line 167 of file InstCombineLoadStoreAlloca.cpp.
References llvm::InstCombiner::Builder, llvm::IRBuilderBase::CreateAlloca(), llvm::GetElementPtrInst::CreateInBounds(), llvm::IRBuilderBase::CreateIntCast(), GEP, llvm::ArrayType::get(), llvm::AllocaInst::getAddressSpace(), llvm::AllocaInst::getAlign(), llvm::AllocaInst::getAllocatedType(), llvm::AllocaInst::getArraySize(), llvm::InstCombiner::getDataLayout(), llvm::IRBuilderBase::getInt32(), llvm::DataLayout::getIntPtrType(), llvm::Value::getName(), llvm::Constant::getNullValue(), llvm::AllocaInst::getType(), llvm::Value::getType(), llvm::InstCombinerImpl::InsertNewInstBefore(), llvm::AllocaInst::isArrayAllocation(), llvm::Type::isIntegerTy(), llvm::InstCombinerImpl::replaceInstUsesWith(), and llvm::InstCombinerImpl::replaceOperand().
Referenced by llvm::InstCombinerImpl::visitAllocaInst().
|
static |
Definition at line 622 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::ConstantInt::get(), llvm::UndefValue::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(), i, llvm::LoadInst::isSimple(), llvm::makeArrayRef(), llvm::InstCombiner::MaxArraySizeForCombine, llvm::InstCombinerImpl::replaceInstUsesWith(), llvm::Instruction::setAAMetadata(), llvm::Value::setName(), and llvm::ARM_MB::ST.
Referenced by llvm::InstCombinerImpl::visitLoadInst().
|
static |
Definition at line 1143 of file InstCombineLoadStoreAlloca.cpp.
References Addr, llvm::InstCombiner::Builder, combineStoreToNewValue(), llvm::commonAlignment(), llvm::IRBuilderBase::CreateAlignedStore(), llvm::IRBuilderBase::CreateExtractValue(), llvm::IRBuilderBase::CreateInBoundsGEP(), DL, llvm::ConstantInt::get(), llvm::InstCombiner::getDataLayout(), llvm::Type::getInt32Ty(), llvm::Type::getInt64Ty(), llvm::Value::getName(), llvm::Value::getType(), i, llvm::makeArrayRef(), llvm::InstCombiner::MaxArraySizeForCombine, llvm::Instruction::setAAMetadata(), SI, and llvm::ARM_MB::ST.
Referenced by llvm::InstCombinerImpl::visitStoreInst().