LLVM  mainline
Defines | Functions
InstCombineLoadStoreAlloca.cpp File Reference
#include "InstCombineInternal.h"
#include "llvm/ADT/Statistic.h"
#include "llvm/Analysis/Loads.h"
#include "llvm/IR/DataLayout.h"
#include "llvm/IR/LLVMContext.h"
#include "llvm/IR/IntrinsicInst.h"
#include "llvm/IR/MDBuilder.h"
#include "llvm/Transforms/Utils/BasicBlockUtils.h"
#include "llvm/Transforms/Utils/Local.h"
Include dependency graph for InstCombineLoadStoreAlloca.cpp:

Go to the source code of this file.

Defines

#define DEBUG_TYPE   "instcombine"

Functions

 STATISTIC (NumDeadStore,"Number of dead stores eliminated")
 STATISTIC (NumGlobalCopies,"Number of allocas copied from constant global")
static bool pointsToConstantGlobal (Value *V)
 pointsToConstantGlobal - Return true if V (possibly indirectly) points to some part of a constant global variable.
static bool isOnlyCopiedFromConstantGlobal (Value *V, MemTransferInst *&TheCopy, SmallVectorImpl< Instruction * > &ToDelete)
 isOnlyCopiedFromConstantGlobal - Recursively walk the uses of a (derived) pointer to an alloca.
static MemTransferInstisOnlyCopiedFromConstantGlobal (AllocaInst *AI, SmallVectorImpl< Instruction * > &ToDelete)
 isOnlyCopiedFromConstantGlobal - Return true if the specified alloca is only modified by a copy from a constant global.
static InstructionsimplifyAllocaArraySize (InstCombiner &IC, AllocaInst &AI)
static LoadInstcombineLoadToNewType (InstCombiner &IC, LoadInst &LI, Type *NewTy, const Twine &Suffix="")
 Helper to combine a load to a new type.
static StoreInstcombineStoreToNewValue (InstCombiner &IC, StoreInst &SI, Value *V)
 Combine a store to a new type.
static InstructioncombineLoadToOperationType (InstCombiner &IC, LoadInst &LI)
 Combine loads to match the type of value their uses after looking through intervening bitcasts.
static InstructionunpackLoadToAggregate (InstCombiner &IC, LoadInst &LI)
static bool isObjectSizeLessThanOrEq (Value *V, uint64_t MaxSize, const DataLayout &DL)
static bool canReplaceGEPIdxWithZero (InstCombiner &IC, GetElementPtrInst *GEPI, Instruction *MemI, unsigned &Idx)
template<typename T >
static InstructionreplaceGEPIdxWithZero (InstCombiner &IC, Value *Ptr, T &MemI)
static bool combineStoreToValueType (InstCombiner &IC, StoreInst &SI)
 Combine stores to match the type of value being stored.
static bool unpackStoreToAggregate (InstCombiner &IC, StoreInst &SI)
static bool equivalentAddressValues (Value *A, Value *B)
 equivalentAddressValues - Test if A and B will obviously have the same value.

Define Documentation

#define DEBUG_TYPE   "instcombine"

Definition at line 25 of file InstCombineLoadStoreAlloca.cpp.


Function Documentation

static bool canReplaceGEPIdxWithZero ( InstCombiner IC,
GetElementPtrInst GEPI,
Instruction MemI,
unsigned Idx 
) [static]
static LoadInst* combineLoadToNewType ( InstCombiner IC,
LoadInst LI,
Type NewTy,
const Twine Suffix = "" 
) [static]

Helper to combine a load to a new type.

This just does the work of combining a load to a new type. It handles metadata, etc., and returns the new instruction. The NewTy should be the loaded *value* type. This will convert it to a pointer, cast the operand to that pointer type, load it, etc.

Note that this will create all of the instructions with whatever insert point the InstCombiner currently is using.

Definition at line 317 of file InstCombineLoadStoreAlloca.cpp.

References llvm::InstCombiner::Builder, llvm::IRBuilder< preserveNames, T, Inserter >::CreateAlignedLoad(), llvm::IRBuilder< preserveNames, T, Inserter >::CreateBitCast(), llvm::ConstantInt::get(), llvm::ConstantPointerNull::get(), llvm::ConstantExpr::getAdd(), llvm::LoadInst::getAlignment(), llvm::Instruction::getAllMetadata(), llvm::Value::getContext(), llvm::Value::getName(), llvm::LoadInst::getPointerAddressSpace(), llvm::LoadInst::getPointerOperand(), llvm::Type::getPointerTo(), llvm::ConstantExpr::getPtrToInt(), llvm::Value::getType(), ID, llvm::Type::isIntegerTy(), llvm::Type::isPointerTy(), llvm::LLVMContext::MD_alias_scope, llvm::LLVMContext::MD_dbg, llvm::LLVMContext::MD_fpmath, llvm::LLVMContext::MD_invariant_load, llvm::LLVMContext::MD_mem_parallel_loop_access, llvm::LLVMContext::MD_noalias, llvm::LLVMContext::MD_nonnull, llvm::LLVMContext::MD_nontemporal, llvm::LLVMContext::MD_prof, llvm::LLVMContext::MD_range, llvm::LLVMContext::MD_tbaa, llvm::LLVMContext::MD_tbaa_struct, N, and llvm::Instruction::setMetadata().

Referenced by combineLoadToOperationType(), and unpackLoadToAggregate().

static Instruction* combineLoadToOperationType ( InstCombiner IC,
LoadInst LI 
) [static]

Combine loads to match the type of value their uses 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 446 of file InstCombineLoadStoreAlloca.cpp.

References llvm::all_of(), llvm::InstCombiner::Builder, combineLoadToNewType(), combineStoreToNewValue(), llvm::DL, llvm::dyn_cast(), llvm::InstCombiner::EraseInstFromFunction(), llvm::Value::getContext(), llvm::InstCombiner::getDataLayout(), llvm::Type::getIntNTy(), llvm::Value::getType(), llvm::DataLayout::getTypeSizeInBits(), llvm::DataLayout::getTypeStoreSizeInBits(), llvm::Value::hasOneUse(), llvm::Type::isIntegerTy(), llvm::DataLayout::isLegalInteger(), llvm::LoadInst::isSimple(), llvm::Type::isSized(), llvm::Value::replaceAllUsesWith(), llvm::IRBuilderBase::SetInsertPoint(), llvm::SI, llvm::Value::use_empty(), llvm::Instruction::user_back(), llvm::Value::user_begin(), and llvm::Value::user_end().

Referenced by llvm::InstCombiner::visitLoadInst().

static StoreInst* combineStoreToNewValue ( InstCombiner IC,
StoreInst SI,
Value V 
) [static]
static bool combineStoreToValueType ( InstCombiner 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 sas otherwise there is no way to signal whether it was combined or not: IC.EraseInstFromFunction returns a null pointer.

Definition at line 847 of file InstCombineLoadStoreAlloca.cpp.

References combineStoreToNewValue(), llvm::StoreInst::getValueOperand(), and llvm::StoreInst::isSimple().

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

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

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

static bool isObjectSizeLessThanOrEq ( Value V,
uint64_t  MaxSize,
const DataLayout DL 
) [static]
static bool isOnlyCopiedFromConstantGlobal ( Value V,
MemTransferInst *&  TheCopy,
SmallVectorImpl< Instruction * > &  ToDelete 
) [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 54 of file InstCombineLoadStoreAlloca.cpp.

References llvm::CS, llvm::dyn_cast(), llvm::SmallVectorBase::empty(), llvm::MemTransferInst::getSource(), I, llvm::MemIntrinsic::isVolatile(), llvm::AArch64CC::MI, pointsToConstantGlobal(), llvm::SmallVectorImpl< T >::pop_back_val(), llvm::SmallVectorTemplateBase< T, isPodLike >::push_back(), and llvm::SmallVectorTemplateBase< T, isPodLike< T >::value >::push_back().

Referenced by isOnlyCopiedFromConstantGlobal(), and llvm::InstCombiner::visitAllocaInst().

static MemTransferInst* isOnlyCopiedFromConstantGlobal ( AllocaInst AI,
SmallVectorImpl< Instruction * > &  ToDelete 
) [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 159 of file InstCombineLoadStoreAlloca.cpp.

References isOnlyCopiedFromConstantGlobal().

static bool pointsToConstantGlobal ( Value V) [static]

pointsToConstantGlobal - Return true if V (possibly indirectly) points to some part of a constant global variable.

This intentionally only accepts constant expressions because we can't rewrite arbitrary instructions.

Definition at line 33 of file InstCombineLoadStoreAlloca.cpp.

References llvm::AddrSpaceCast, and llvm::WinEH::CE.

Referenced by isOnlyCopiedFromConstantGlobal().

template<typename T >
static Instruction* replaceGEPIdxWithZero ( InstCombiner IC,
Value Ptr,
T MemI 
) [static]
static Instruction* simplifyAllocaArraySize ( InstCombiner IC,
AllocaInst AI 
) [static]
STATISTIC ( NumDeadStore  ,
"Number of dead stores eliminated"   
)
STATISTIC ( NumGlobalCopies  ,
"Number of allocas copied from constant global"   
)
static Instruction* unpackLoadToAggregate ( InstCombiner IC,
LoadInst LI 
) [static]
static bool unpackStoreToAggregate ( InstCombiner IC,
StoreInst SI 
) [static]