LLVM  9.0.0svn
Macros | Functions | Variables
MemCpyOptimizer.cpp File Reference
#include "llvm/Transforms/Scalar/MemCpyOptimizer.h"
#include "llvm/ADT/DenseSet.h"
#include "llvm/ADT/None.h"
#include "llvm/ADT/STLExtras.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/ADT/Statistic.h"
#include "llvm/ADT/iterator_range.h"
#include "llvm/Analysis/AliasAnalysis.h"
#include "llvm/Analysis/AssumptionCache.h"
#include "llvm/Analysis/GlobalsModRef.h"
#include "llvm/Analysis/MemoryDependenceAnalysis.h"
#include "llvm/Analysis/MemoryLocation.h"
#include "llvm/Analysis/TargetLibraryInfo.h"
#include "llvm/Transforms/Utils/Local.h"
#include "llvm/Analysis/ValueTracking.h"
#include "llvm/IR/Argument.h"
#include "llvm/IR/BasicBlock.h"
#include "llvm/IR/CallSite.h"
#include "llvm/IR/Constants.h"
#include "llvm/IR/DataLayout.h"
#include "llvm/IR/DerivedTypes.h"
#include "llvm/IR/Dominators.h"
#include "llvm/IR/Function.h"
#include "llvm/IR/GetElementPtrTypeIterator.h"
#include "llvm/IR/GlobalVariable.h"
#include "llvm/IR/IRBuilder.h"
#include "llvm/IR/InstrTypes.h"
#include "llvm/IR/Instruction.h"
#include "llvm/IR/Instructions.h"
#include "llvm/IR/IntrinsicInst.h"
#include "llvm/IR/Intrinsics.h"
#include "llvm/IR/LLVMContext.h"
#include "llvm/IR/Module.h"
#include "llvm/IR/Operator.h"
#include "llvm/IR/PassManager.h"
#include "llvm/IR/Type.h"
#include "llvm/IR/User.h"
#include "llvm/IR/Value.h"
#include "llvm/Pass.h"
#include "llvm/Support/Casting.h"
#include "llvm/Support/Debug.h"
#include "llvm/Support/MathExtras.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/Transforms/Scalar.h"
#include <algorithm>
#include <cassert>
#include <cstdint>
#include <utility>
Include dependency graph for MemCpyOptimizer.cpp:

Go to the source code of this file.

Macros

#define DEBUG_TYPE   "memcpyopt"
 

Functions

 STATISTIC (NumMemCpyInstr, "Number of memcpy instructions deleted")
 
 STATISTIC (NumMemSetInfer, "Number of memsets inferred")
 
 STATISTIC (NumMoveToCpy, "Number of memmoves converted to memcpy")
 
 STATISTIC (NumCpyToSet, "Number of memcpys converted to memset")
 
static int64_t GetOffsetFromIndex (const GEPOperator *GEP, unsigned Idx, bool &VariableIdxFound, const DataLayout &DL)
 
static bool IsPointerOffset (Value *Ptr1, Value *Ptr2, int64_t &Offset, const DataLayout &DL)
 Return true if Ptr1 is provably equal to Ptr2 plus a constant offset, and return that constant offset. More...
 
 INITIALIZE_PASS_BEGIN (MemCpyOptLegacyPass, "memcpyopt", "MemCpy Optimization", false, false) INITIALIZE_PASS_END(MemCpyOptLegacyPass
 
static unsigned findStoreAlignment (const DataLayout &DL, const StoreInst *SI)
 
static unsigned findLoadAlignment (const DataLayout &DL, const LoadInst *LI)
 
static unsigned findCommonAlignment (const DataLayout &DL, const StoreInst *SI, const LoadInst *LI)
 
static bool moveUp (AliasAnalysis &AA, StoreInst *SI, Instruction *P, const LoadInst *LI)
 
static bool hasUndefContents (Instruction *I, ConstantInt *Size)
 Determine whether the instruction has undefined content for the given Size, either because it was freshly alloca'd or started its lifetime. More...
 

Variables

 memcpyopt
 
MemCpy Optimization
 
MemCpy false
 

Macro Definition Documentation

◆ DEBUG_TYPE

#define DEBUG_TYPE   "memcpyopt"

Definition at line 65 of file MemCpyOptimizer.cpp.

Function Documentation

◆ findCommonAlignment()

static unsigned findCommonAlignment ( const DataLayout DL,
const StoreInst SI,
const LoadInst LI 
)
static

Definition at line 517 of file MemCpyOptimizer.cpp.

References findLoadAlignment(), findStoreAlignment(), and llvm::MinAlign().

Referenced by moveUp().

◆ findLoadAlignment()

static unsigned findLoadAlignment ( const DataLayout DL,
const LoadInst LI 
)
static

◆ findStoreAlignment()

static unsigned findStoreAlignment ( const DataLayout DL,
const StoreInst SI 
)
static

◆ GetOffsetFromIndex()

static int64_t GetOffsetFromIndex ( const GEPOperator GEP,
unsigned  Idx,
bool VariableIdxFound,
const DataLayout DL 
)
static

◆ hasUndefContents()

static bool hasUndefContents ( Instruction I,
ConstantInt Size 
)
static

Determine whether the instruction has undefined content for the given Size, either because it was freshly alloca'd or started its lifetime.

Definition at line 1149 of file MemCpyOptimizer.cpp.

References llvm::AMDGPU::HSAMD::Kernel::Arg::Key::Align, llvm::CallSiteBase< FunTy, BBTy, ValTy, UserTy, UseTy, InstrTy, CallTy, InvokeTy, CallBrTy, IterTy >::arg_size(), C, llvm::IRBuilderBase::CreateMemSet(), llvm::dbgs(), llvm::dyn_cast(), llvm::Instruction::eraseFromParent(), F(), llvm::CallSiteBase< FunTy, BBTy, ValTy, UserTy, UseTy, InstrTy, CallTy, InvokeTy, CallBrTy, IterTy >::getArgument(), llvm::CallSiteBase< FunTy, BBTy, ValTy, UserTy, UseTy, InstrTy, CallTy, InvokeTy, CallBrTy, IterTy >::getCaller(), llvm::Module::getDataLayout(), llvm::Intrinsic::getDeclaration(), llvm::MemIntrinsicBase< Derived >::getDest(), llvm::MemIntrinsicBase< Derived >::getDestAlignment(), llvm::MemoryLocation::getForDest(), llvm::MemoryLocation::getForSource(), llvm::MemDepResult::getInst(), llvm::CallSiteBase< FunTy, BBTy, ValTy, UserTy, UseTy, InstrTy, CallTy, InvokeTy, CallBrTy, IterTy >::getInstruction(), llvm::ilist_node_impl< OptionsT >::getIterator(), llvm::MemIntrinsicBase< Derived >::getLength(), llvm::Instruction::getModule(), llvm::User::getOperand(), llvm::getOrEnforceKnownAlignment(), llvm::CallSiteBase< FunTy, BBTy, ValTy, UserTy, UseTy, InstrTy, CallTy, InvokeTy, CallBrTy, IterTy >::getParamAlignment(), llvm::Instruction::getParent(), llvm::GlobalValue::getParent(), llvm::Type::getPointerAddressSpace(), llvm::MemIntrinsicBase< Derived >::getRawDest(), llvm::MemTransferBase< BaseCL >::getRawSource(), llvm::MemTransferBase< BaseCL >::getSource(), llvm::MemTransferBase< BaseCL >::getSourceAlignment(), llvm::Value::getType(), llvm::DataLayout::getTypeAllocSize(), llvm::ConstantInt::getValue(), llvm::ConstantInt::getZExtValue(), llvm::APInt::getZExtValue(), I, llvm::isBytewiseValue(), llvm::CallSiteBase< FunTy, BBTy, ValTy, UserTy, UseTy, InstrTy, CallTy, InvokeTy, CallBrTy, IterTy >::isByValArgument(), llvm::MemDepResult::isClobber(), llvm::MemDepResult::isDef(), llvm::AAResults::isMustAlias(), llvm::AAResults::isNoAlias(), llvm::DominatorTree::isReachableFromEntry(), llvm::MemIntrinsic::isVolatile(), LLVM_DEBUG, llvm::MinAlign(), llvm::LocationSize::precise(), llvm::CallSiteBase< FunTy, BBTy, ValTy, UserTy, UseTy, InstrTy, CallTy, InvokeTy, CallBrTy, IterTy >::setArgument(), llvm::CallBase::setCalledFunction(), and llvm::Value::stripPointerCasts().

◆ INITIALIZE_PASS_BEGIN()

INITIALIZE_PASS_BEGIN ( MemCpyOptLegacyPass  ,
"memcpyopt"  ,
"MemCpy Optimization ,
false  ,
false   
)

◆ IsPointerOffset()

static bool IsPointerOffset ( Value Ptr1,
Value Ptr2,
int64_t &  Offset,
const DataLayout DL 
)
static

Return true if Ptr1 is provably equal to Ptr2 plus a constant offset, and return that constant offset.

For example, Ptr1 might be &A[42], and Ptr2 might be &A[40]. In this case offset would be -8.

Definition at line 106 of file MemCpyOptimizer.cpp.

References llvm::dyn_cast(), llvm::User::getNumOperands(), GetOffsetFromIndex(), llvm::User::getOperand(), and llvm::Value::stripPointerCasts().

◆ moveUp()

static bool moveUp ( AliasAnalysis AA,
StoreInst SI,
Instruction P,
const LoadInst LI 
)
static

Definition at line 528 of file MemCpyOptimizer.cpp.

References llvm::AMDGPU::HSAMD::Kernel::Arg::Key::Align, llvm::any_of(), llvm::CallSiteBase< FunTy, BBTy, ValTy, UserTy, UseTy, InstrTy, CallTy, InvokeTy, CallBrTy, IterTy >::arg_size(), llvm::AMDGPU::HSAMD::Kernel::Key::Args, assert(), C, llvm::AAResults::callCapturesBefore(), llvm::combineMetadata(), llvm::IRBuilder< T, Inserter >::CreateGEP(), llvm::IRBuilder< T, Inserter >::CreateICmpULE(), llvm::IRBuilderBase::CreateMemCpy(), llvm::IRBuilderBase::CreateMemMove(), llvm::IRBuilderBase::CreateMemSet(), llvm::CastInst::CreatePointerCast(), llvm::IRBuilder< T, Inserter >::CreateSelect(), llvm::IRBuilder< T, Inserter >::CreateSub(), llvm::IRBuilder< T, Inserter >::CreateZExt(), llvm::dbgs(), llvm::CallSiteBase< FunTy, BBTy, ValTy, UserTy, UseTy, InstrTy, CallTy, InvokeTy, CallBrTy, IterTy >::doesNotCapture(), llvm::DominatorTree::dominates(), llvm::dyn_cast(), E, llvm::detail::DenseSetImpl< ValueT, DenseMap< ValueT, detail::DenseSetEmpty, ValueInfoT, detail::DenseSetPair< ValueT > >, ValueInfoT >::erase(), llvm::Instruction::eraseFromParent(), F(), findCommonAlignment(), findLoadAlignment(), findStoreAlignment(), G, llvm::MemoryLocation::get(), llvm::DataLayout::getABITypeAlignment(), llvm::AllocaInst::getAlignment(), llvm::StoreInst::getAlignment(), llvm::AllocaInst::getAllocatedType(), llvm::CallSiteBase< FunTy, BBTy, ValTy, UserTy, UseTy, InstrTy, CallTy, InvokeTy, CallBrTy, IterTy >::getArgument(), llvm::AllocaInst::getArraySize(), llvm::CallBase::getCalledFunction(), llvm::Module::getDataLayout(), llvm::MemIntrinsicBase< Derived >::getDest(), llvm::MemIntrinsicBase< Derived >::getDestAlignment(), llvm::MemoryLocation::getForDest(), llvm::MemoryLocation::getForSource(), llvm::MemDepResult::getInst(), llvm::Type::getIntegerBitWidth(), llvm::ilist_node_impl< OptionsT >::getIterator(), llvm::MemIntrinsicBase< Derived >::getLength(), llvm::Instruction::getMetadata(), llvm::AAResults::getModRefInfo(), llvm::Instruction::getModule(), llvm::Value::getName(), llvm::Constant::getNullValue(), llvm::User::getNumOperands(), llvm::User::getOperand(), llvm::Instruction::getParent(), llvm::Type::getPointerAddressSpace(), llvm::Type::getPointerElementType(), llvm::StoreInst::getPointerOperand(), llvm::MemIntrinsicBase< Derived >::getRawDest(), llvm::MemTransferBase< BaseCL >::getRawSource(), llvm::MemTransferBase< BaseCL >::getSource(), llvm::MemTransferBase< BaseCL >::getSourceAlignment(), llvm::Value::getType(), llvm::DataLayout::getTypeAllocSize(), llvm::DataLayout::getTypeStoreSize(), llvm::MemSetBase< BaseCL >::getValue(), llvm::ConstantInt::getZExtValue(), I, llvm::detail::DenseSetImpl< ValueT, DenseMap< ValueT, detail::DenseSetEmpty, ValueInfoT, detail::DenseSetPair< ValueT > >, ValueInfoT >::insert(), llvm::isBytewiseValue(), llvm::MemDepResult::isClobber(), llvm::isModOrRefSet(), llvm::isModSet(), llvm::AAResults::isNoAlias(), llvm::StoreInst::isSimple(), llvm::Type::isSized(), llvm::MemIntrinsic::isVolatile(), IT, LLVM_DEBUG, llvm::make_range(), llvm::max(), llvm::MayAlias, llvm::Instruction::mayThrow(), llvm::LLVMContext::MD_access_group, llvm::LLVMContext::MD_alias_scope, llvm::LLVMContext::MD_invariant_group, llvm::LLVMContext::MD_noalias, llvm::LLVMContext::MD_nontemporal, llvm::LLVMContext::MD_tbaa, llvm::MinAlign(), llvm::None, P, llvm::LocationSize::precise(), llvm::SmallVectorTemplateBase< T >::push_back(), llvm::reverse(), llvm::CallSiteBase< FunTy, BBTy, ValTy, UserTy, UseTy, InstrTy, CallTy, InvokeTy, CallBrTy, IterTy >::setArgument(), SI, Size, llvm::Value::stripPointerCasts(), llvm::Value::user_begin(), llvm::Value::user_end(), and llvm::Value::users().

◆ STATISTIC() [1/4]

STATISTIC ( NumMemCpyInstr  ,
"Number of memcpy instructions deleted"   
)

◆ STATISTIC() [2/4]

STATISTIC ( NumMemSetInfer  ,
"Number of memsets inferred"   
)

◆ STATISTIC() [3/4]

STATISTIC ( NumMoveToCpy  ,
"Number of memmoves converted to memcpy"   
)

◆ STATISTIC() [4/4]

STATISTIC ( NumCpyToSet  ,
"Number of memcpys converted to memset"   
)

Variable Documentation

◆ false

MemCpy false

Definition at line 381 of file MemCpyOptimizer.cpp.

◆ memcpyopt

memcpyopt

Definition at line 381 of file MemCpyOptimizer.cpp.

◆ Optimization

MemCpy Optimization

Definition at line 381 of file MemCpyOptimizer.cpp.