LLVM 20.0.0git
Macros | Functions | Variables
InstCombineLoadStoreAlloca.cpp File Reference
#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.

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, AllocaInst *V, MemTransferInst *&TheCopy, SmallVectorImpl< Instruction * > &ToDelete)
 isOnlyCopiedFromConstantMemory - Recursively walk the uses of a (derived) pointer to an alloca.
 
static MemTransferInstisOnlyCopiedFromConstantMemory (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 InstructionsimplifyAllocaArraySize (InstCombinerImpl &IC, AllocaInst &AI, DominatorTree &DT)
 
static bool isSupportedAtomicType (Type *Ty)
 
static StoreInstcombineStoreToNewValue (InstCombinerImpl &IC, StoreInst &SI, Value *V)
 Combine a store to a new type.
 
static InstructioncombineLoadToOperationType (InstCombinerImpl &IC, LoadInst &Load)
 Combine loads to match the type of their uses' value after looking through intervening bitcasts.
 
static InstructionunpackLoadToAggregate (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 InstructionreplaceGEPIdxWithZero (InstCombinerImpl &IC, Value *Ptr, Instruction &MemI)
 
static bool canSimplifyNullStoreOrGEP (StoreInst &SI)
 
static bool canSimplifyNullLoadOrGEP (LoadInst &LI, Value *Op)
 
static ValuelikeBitCastFromVector (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< unsignedMaxCopiedFromConstantUsers ("instcombine-max-copied-from-constant-users", cl::init(300), cl::desc("Maximum users to visit in copy from constant transform"), cl::Hidden)
 

Macro Definition Documentation

◆ DEBUG_TYPE

#define DEBUG_TYPE   "instcombine"

Definition at line 30 of file InstCombineLoadStoreAlloca.cpp.

Function Documentation

◆ canReplaceGEPIdxWithZero()

static bool canReplaceGEPIdxWithZero ( InstCombinerImpl IC,
GetElementPtrInst GEPI,
Instruction MemI,
unsigned Idx 
)
static

◆ canSimplifyNullLoadOrGEP()

static bool canSimplifyNullLoadOrGEP ( LoadInst LI,
Value Op 
)
static

◆ canSimplifyNullStoreOrGEP()

static bool canSimplifyNullStoreOrGEP ( StoreInst SI)
static

◆ combineLoadToOperationType()

static Instruction * combineLoadToOperationType ( InstCombinerImpl IC,
LoadInst Load 
)
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 639 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().

◆ combineStoreToNewValue()

static StoreInst * combineStoreToNewValue ( InstCombinerImpl IC,
StoreInst SI,
Value V 
)
static

◆ combineStoreToValueType()

static bool combineStoreToValueType ( InstCombinerImpl IC,
StoreInst SI 
)
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.

Returns
true if the store was successfully combined away. This indicates the caller must erase the store instruction. We have to let the caller erase the store instruction as otherwise there is no way to signal whether it was combined or not: IC.EraseInstFromFunction returns a null pointer.

Definition at line 1162 of file InstCombineLoadStoreAlloca.cpp.

References assert(), combineStoreToNewValue(), isSupportedAtomicType(), and likeBitCastFromVector().

Referenced by llvm::InstCombinerImpl::visitStoreInst().

◆ equivalentAddressValues()

static bool equivalentAddressValues ( Value A,
Value B 
)
static

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 1317 of file InstCombineLoadStoreAlloca.cpp.

References A, and B.

Referenced by llvm::InstCombinerImpl::visitStoreInst().

◆ isDereferenceableForAllocaSize()

static bool isDereferenceableForAllocaSize ( const Value V,
const AllocaInst AI,
const DataLayout DL 
)
static

◆ isObjectSizeLessThanOrEq()

static bool isObjectSizeLessThanOrEq ( Value V,
uint64_t  MaxSize,
const DataLayout DL 
)
static

◆ isOnlyCopiedFromConstantMemory() [1/2]

static MemTransferInst * isOnlyCopiedFromConstantMemory ( AAResults AA,
AllocaInst AI,
SmallVectorImpl< Instruction * > &  ToDelete 
)
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 170 of file InstCombineLoadStoreAlloca.cpp.

References isOnlyCopiedFromConstantMemory().

◆ isOnlyCopiedFromConstantMemory() [2/2]

static bool isOnlyCopiedFromConstantMemory ( AAResults AA,
AllocaInst V,
MemTransferInst *&  TheCopy,
SmallVectorImpl< Instruction * > &  ToDelete 
)
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 48 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().

◆ isSupportedAtomicType()

static bool isSupportedAtomicType ( Type Ty)
static

◆ likeBitCastFromVector()

static Value * likeBitCastFromVector ( InstCombinerImpl IC,
Value V 
)
static

Look for extractelement/insertvalue sequence that acts like a bitcast.

Returns
underlying value that was "cast", or nullptr otherwise.

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 1101 of file InstCombineLoadStoreAlloca.cpp.

References DL, llvm::InstCombiner::getDataLayout(), IV, llvm::PatternMatch::m_Undef(), and llvm::PatternMatch::match().

Referenced by combineStoreToValueType().

◆ replaceGEPIdxWithZero()

static Instruction * replaceGEPIdxWithZero ( InstCombinerImpl IC,
Value Ptr,
Instruction MemI 
)
static

◆ simplifyAllocaArraySize()

static Instruction * simplifyAllocaArraySize ( InstCombinerImpl IC,
AllocaInst AI,
DominatorTree DT 
)
static

◆ STATISTIC() [1/2]

STATISTIC ( NumDeadStore  ,
"Number of dead stores eliminated"   
)

◆ STATISTIC() [2/2]

STATISTIC ( NumGlobalCopies  ,
"Number of allocas copied from constant global"   
)

◆ unpackLoadToAggregate()

static Instruction * unpackLoadToAggregate ( InstCombinerImpl IC,
LoadInst LI 
)
static

◆ unpackStoreToAggregate()

static bool unpackStoreToAggregate ( InstCombinerImpl IC,
StoreInst SI 
)
static

Variable Documentation

◆ MaxCopiedFromConstantUsers

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) ( "instcombine-max-copied-from-constant-users"  ,
cl::init(300)  ,
cl::desc("Maximum users to visit in copy from constant transform")  ,
cl::Hidden   
)
static