25 #define DEBUG_TYPE "instcombine"
27 STATISTIC(NumDeadStore,
"Number of dead stores eliminated");
28 STATISTIC(NumGlobalCopies,
"Number of allocas copied from constant global");
35 return GV->isConstant();
38 if (CE->getOpcode() == Instruction::BitCast ||
39 CE->getOpcode() == Instruction::AddrSpaceCast ||
40 CE->getOpcode() == Instruction::GetElementPtr)
61 ValuesToInspect.
push_back(std::make_pair(V,
false));
62 while (!ValuesToInspect.
empty()) {
64 const bool IsOffset = ValuePair.second;
65 for (
auto &U : ValuePair.first->uses()) {
68 if (
LoadInst *LI = dyn_cast<LoadInst>(I)) {
70 if (!LI->isSimple())
return false;
74 if (isa<BitCastInst>(I) || isa<AddrSpaceCastInst>(
I)) {
76 ValuesToInspect.
push_back(std::make_pair(I, IsOffset));
83 std::make_pair(I, IsOffset || !
GEP->hasAllZeroIndices()));
94 unsigned ArgNo =
CS.getArgumentNo(&U);
95 if (
CS.isInAllocaArgument(ArgNo))
101 if (
CS.onlyReadsMemory() &&
102 (
CS.getInstruction()->use_empty() ||
CS.doesNotCapture(ArgNo)))
107 if (
CS.isByValArgument(ArgNo))
113 if (II->getIntrinsicID() == Intrinsic::lifetime_start ||
114 II->getIntrinsicID() == Intrinsic::lifetime_end) {
115 assert(II->use_empty() &&
"Lifetime markers have no result to use!");
129 if (U.getOperandNo() == 1) {
135 if (TheCopy)
return false;
139 if (IsOffset)
return false;
142 if (U.getOperandNo() != 0)
return false;
190 while (isa<AllocaInst>(*It) || isa<DbgInfoIntrinsic>(*It))
198 Value *Idx[2] = {NullIdx, NullIdx};
247 if (FirstInst != &AI) {
287 if (AI.getAlignment() <= SourceAlign) {
288 DEBUG(
dbgs() <<
"Found alloca equal to global: " << AI <<
'\n');
289 DEBUG(
dbgs() <<
" memcpy = " << *Copy <<
'\n');
290 for (
unsigned i = 0, e = ToDelete.
size(); i != e; ++i)
292 Constant *TheSrc = cast<Constant>(Copy->getSource());
318 const Twine &Suffix =
"") {
328 for (
const auto &MDPair : MD) {
329 unsigned ID = MDPair.first;
361 auto *ITy = cast<IntegerType>(NewTy);
367 MDB.createRange(NonNullInt, NullInt));
393 for (
const auto &MDPair : MD) {
394 unsigned ID = MDPair.first;
467 return SI &&
SI->getPointerOperand() != &LI;
474 auto *
SI = cast<StoreInst>(*UI++);
479 assert(LI.
use_empty() &&
"Failed to remove all users of the load!");
490 if (
auto* CI = dyn_cast<CastInst>(LI.
user_back())) {
491 if (CI->isNoopCast(DL)) {
514 assert(LI.
getAlignment() &&
"Alignement must be set at this point");
516 if (
auto *
ST = dyn_cast<StructType>(T)) {
518 if (
ST->getNumElements() == 1) {
526 if (
auto *AT = dyn_cast<ArrayType>(T)) {
528 if (AT->getNumElements() == 1) {
554 if (!Visited.
insert(P).second)
563 if (
PHINode *PN = dyn_cast<PHINode>(P)) {
564 for (
Value *IncValue : PN->incoming_values())
570 if (GA->mayBeOverridden())
578 if (
AllocaInst *AI = dyn_cast<AllocaInst>(P)) {
579 if (!AI->getAllocatedType()->isSized())
595 if (!GV->hasDefinitiveInitializer() || !GV->isConstant())
599 if (InitSize > MaxSize)
605 }
while (!Worklist.
empty());
636 if (
const ConstantInt *CI = dyn_cast<ConstantInt>(V))
648 Idx = FirstNZIdx(GEPI);
659 if (!AllocTy || !AllocTy->isSized())
668 auto IsAllNonNegative = [&]() {
669 for (
unsigned i = Idx+1, e = GEPI->
getNumOperands(); i != e; ++i) {
670 bool KnownNonNegative, KnownNegative;
672 KnownNegative, 0, MemI);
673 if (KnownNonNegative)
698 template <
typename T>
708 MemI.setOperand(MemI.getPointerOperandIndex(), NewGEPI);
727 unsigned EffectiveLoadAlign =
730 if (KnownAlign > EffectiveLoadAlign)
732 else if (LoadAlign == 0)
755 if (
LoadInst *NLI = dyn_cast<LoadInst>(AvailableVal)) {
756 unsigned KnownIDs[] = {
774 const Value *GEPI0 = GEPI->getOperand(0);
776 if (isa<ConstantPointerNull>(GEPI0) && GEPI->getPointerAddressSpace() == 0){
789 if (isa<UndefValue>(Op) ||
816 SI->getOperand(1)->getName()+
".val");
818 SI->getOperand(2)->getName()+
".val");
825 if (isa<ConstantPointerNull>(
SI->getOperand(1)) &&
832 if (isa<ConstantPointerNull>(
SI->getOperand(2)) &&
871 if (
auto *BC = dyn_cast<BitCastInst>(V)) {
872 V = BC->getOperand(0);
894 if (
auto *
ST = dyn_cast<StructType>(T)) {
896 if (
ST->getNumElements() == 1) {
903 if (
auto *AT = dyn_cast<ArrayType>(T)) {
905 if (AT->getNumElements() == 1) {
925 if (A == B)
return true;
932 if (isa<BinaryOperator>(A) ||
935 isa<GetElementPtrInst>(A))
937 if (cast<Instruction>(A)->isIdenticalToWhenDefined(BI))
956 unsigned EffectiveStoreAlign =
959 if (KnownAlign > EffectiveStoreAlign)
961 else if (StoreAlign == 0)
981 if (isa<AllocaInst>(Ptr))
984 if (isa<AllocaInst>(
GEP->getOperand(0))) {
985 if (
GEP->getOperand(0)->hasOneUse())
995 for (
unsigned ScanInsts = 6; BBI != SI.
getParent()->
begin() && ScanInsts;
1000 if (isa<DbgInfoIntrinsic>(BBI) ||
1001 (isa<BitCastInst>(BBI) && BBI->getType()->isPointerTy())) {
1006 if (
StoreInst *PrevSI = dyn_cast<StoreInst>(BBI)) {
1021 if (
LoadInst *LI = dyn_cast<LoadInst>(BBI)) {
1032 if (BBI->mayWriteToMemory() || BBI->mayReadFromMemory())
1038 if (!isa<UndefValue>(Val)) {
1047 if (isa<UndefValue>(Val))
1056 }
while (isa<DbgInfoIntrinsic>(BBI) ||
1057 (isa<BitCastInst>(BBI) && BBI->getType()->isPointerTy()));
1058 if (
BranchInst *BI = dyn_cast<BranchInst>(BBI))
1059 if (BI->isUnconditional())
1060 if (SimplifyStoreAtEndOfBlock(SI))
1074 bool InstCombiner::SimplifyStoreAtEndOfBlock(
StoreInst &SI) {
1105 if (StoreBB == DestBB || OtherBB == DestBB)
1111 if (!OtherBr || BBI == OtherBB->
begin())
1120 while (isa<DbgInfoIntrinsic>(BBI) ||
1121 (isa<BitCastInst>(BBI) && BBI->getType()->isPointerTy())) {
1122 if (BBI==OtherBB->
begin())
1144 if ((OtherStore = dyn_cast<StoreInst>(BBI))) {
1152 if (BBI->mayReadFromMemory() || BBI->mayWriteToMemory() ||
1153 BBI == OtherBB->
begin())
1162 if (
I->mayReadFromMemory() ||
I->mayWriteToMemory())
Value * getValueOperand()
void push_back(const T &Elt)
A parsed version of the target data layout string in and methods for querying it. ...
AllocaInst * CreateAlloca(Type *Ty, Value *ArraySize=nullptr, const Twine &Name="")
LoadInst * CreateLoad(Value *Ptr, const char *Name)
void addIncoming(Value *V, BasicBlock *BB)
addIncoming - Add an incoming value to the end of the PHI list
unsigned getOrEnforceKnownAlignment(Value *V, unsigned PrefAlign, const DataLayout &DL, const Instruction *CxtI=nullptr, AssumptionCache *AC=nullptr, const DominatorTree *DT=nullptr)
getOrEnforceKnownAlignment - If the specified pointer has an alignment that we can determine...
static Constant * getPointerBitCastOrAddrSpaceCast(Constant *C, Type *Ty)
Create a BitCast or AddrSpaceCast for a pointer type depending on the address space.
STATISTIC(NumFunctions,"Total number of functions")
bool isVolatile() const
isVolatile - Return true if this is a store to a volatile memory location.
SynchronizationScope getSynchScope() const
Value * CreateIntCast(Value *V, Type *DestTy, bool isSigned, const Twine &Name="")
void setAlignment(unsigned Align)
unsigned getNumOperands() const
unsigned getPrefTypeAlignment(Type *Ty) const
Returns the preferred stack/global alignment for the specified type.
const DataLayout & getDataLayout() const
void Add(Instruction *I)
Add - Add the specified instruction to the worklist if it isn't already in it.
const Function * getParent() const
Return the enclosing method, or null if none.
const Instruction & front() const
static LoadInst * combineLoadToNewType(InstCombiner &IC, LoadInst &LI, Type *NewTy, const Twine &Suffix="")
Helper to combine a load to a new type.
LoadInst - an instruction for reading from memory.
static bool pointsToConstantGlobal(Value *V)
pointsToConstantGlobal - Return true if V (possibly indirectly) points to some part of a constant glo...
void getAllMetadata(SmallVectorImpl< std::pair< unsigned, MDNode * >> &MDs) const
getAllMetadata - Get all metadata attached to this Instruction.
static bool isOnlyCopiedFromConstantGlobal(Value *V, MemTransferInst *&TheCopy, SmallVectorImpl< Instruction * > &ToDelete)
isOnlyCopiedFromConstantGlobal - Recursively walk the uses of a (derived) pointer to an alloca...
static Constant * getNullValue(Type *Ty)
StringRef getName() const
Return a constant reference to the value's name.
iterator begin()
Instruction iterator methods.
static Constant * getAdd(Constant *C1, Constant *C2, bool HasNUW=false, bool HasNSW=false)
bool isArrayAllocation() const
isArrayAllocation - Return true if there is an allocation size parameter to the allocation instructio...
Instruction * getFirstNonPHIOrDbg()
Returns a pointer to the first instruction in this block that is not a PHINode or a debug intrinsic...
Value * CreateExtractValue(Value *Agg, ArrayRef< unsigned > Idxs, const Twine &Name="")
bool isUnconditional() const
SelectInst - This class represents the LLVM 'select' instruction.
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
const APInt & getValue() const
Return the constant as an APInt value reference.
T LLVM_ATTRIBUTE_UNUSED_RESULT pop_back_val()
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
static Instruction * replaceGEPIdxWithZero(InstCombiner &IC, Value *Ptr, T &MemI)
The core instruction combiner logic.
bool isSized(SmallPtrSetImpl< const Type * > *Visited=nullptr) const
isSized - Return true if it makes sense to take the size of this type.
Instruction * clone() const
clone() - Create a copy of 'this' instruction that is identical in all ways except the following: ...
unsigned getPointerAddressSpace() const
Returns the address space of the pointer operand.
ConstantExpr - a constant value that is initialized with an expression using other constant values...
static GetElementPtrInst * CreateInBounds(Value *Ptr, ArrayRef< Value * > IdxList, const Twine &NameStr="", Instruction *InsertBefore=nullptr)
Create an "inbounds" getelementptr.
static SelectInst * Create(Value *C, Value *S1, Value *S2, const Twine &NameStr="", Instruction *InsertBefore=nullptr)
APInt LLVM_ATTRIBUTE_UNUSED_RESULT zextOrSelf(unsigned width) const
Zero extend or truncate to width.
bool LLVM_ATTRIBUTE_UNUSED_RESULT empty() const
static Instruction * simplifyAllocaArraySize(InstCombiner &IC, AllocaInst &AI)
BasicBlock * getSuccessor(unsigned i) const
This class represents a no-op cast from one type to another.
StoreInst - an instruction for storing to memory.
void replaceAllUsesWith(Value *V)
Change all uses of this to point to a new Value.
void SetInsertPoint(BasicBlock *TheBB)
This specifies that created instructions should be appended to the end of the specified block...
bool isInBounds() const
isInBounds - Determine whether the GEP has the inbounds flag.
GetElementPtrInst - an instruction for type-safe pointer arithmetic to access elements of arrays and ...
static Instruction * combineLoadToOperationType(InstCombiner &IC, LoadInst &LI)
Combine loads to match the type of value their uses after looking through intervening bitcasts...
static bool equivalentAddressValues(Value *A, Value *B)
equivalentAddressValues - Test if A and B will obviously have the same value.
static bool unpackStoreToAggregate(InstCombiner &IC, StoreInst &SI)
unsigned getAlignment() const
getAlignment - Return the alignment of the access that is being performed
void setDebugLoc(DebugLoc Loc)
setDebugLoc - Set the debug location information for this instruction.
static ConstantPointerNull * get(PointerType *T)
get() - Static factory methods - Return objects of the specified value
void insertBefore(Instruction *InsertPos)
Insert an unlinked instruction into a basic block immediately before the specified instruction...
void setAAMetadata(const AAMDNodes &N)
setAAMetadata - Sets the metadata on this instruction from the AAMDNodes structure.
LLVM Basic Block Representation.
The instances of the Type class are immutable: once they are created, they are never changed...
BasicBlock * getSuccessor(unsigned idx) const
Return the specified successor.
uint64_t getTypeStoreSizeInBits(Type *Ty) const
Returns the maximum number of bits that may be overwritten by storing the specified type; always a mu...
BranchInst - Conditional or Unconditional Branch instruction.
This is an important base class in LLVM.
PointerType * getType() const
getType - Overload to return most specific pointer type
Instruction * ReplaceInstUsesWith(Instruction &I, Value *V)
A combiner-aware RAUW-like routine.
unsigned getAlignment() const
getAlignment - Return the alignment of the memory that is being allocated by the instruction.
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
static StoreInst * combineStoreToNewValue(InstCombiner &IC, StoreInst &SI, Value *V)
Combine a store to a new type.
Interval::pred_iterator pred_begin(Interval *I)
pred_begin/pred_end - define methods so that Intervals may be used just like BasicBlocks can with the...
const DebugLoc & getDebugLoc() const
getDebugLoc - Return the debug location for this node as a DebugLoc.
Instruction * visitAllocaInst(AllocaInst &AI)
Value * getOperand(unsigned i) const
Interval::pred_iterator pred_end(Interval *I)
Value * getPointerOperand()
void setAlignment(unsigned Align)
bool isPointerTy() const
isPointerTy - True if this is an instance of PointerType.
static UndefValue * get(Type *T)
get() - Static factory methods - Return an 'undef' object of the specified type.
LLVMContext & getContext() const
All values hold a context through their type.
PointerType * getPointerTo(unsigned AddrSpace=0)
getPointerTo - Return a pointer to the current type.
StoreInst * CreateAlignedStore(Value *Val, Value *Ptr, unsigned Align, bool isVolatile=false)
LoadInst * CreateAlignedLoad(Value *Ptr, unsigned Align, const char *Name)
void setMetadata(unsigned KindID, MDNode *Node)
setMetadata - Set the metadata of the specified kind to the specified node.
InstCombineWorklist & Worklist
A worklist of the instructions that need to be simplified.
IntegerType * getIntPtrType(LLVMContext &C, unsigned AddressSpace=0) const
Returns an integer type with size at least as big as that of a pointer in the given address space...
unsigned getABITypeAlignment(Type *Ty) const
Returns the minimum ABI-required alignment for the specified type.
AtomicOrdering getOrdering() const
Returns the ordering effect of this store.
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements...
This is the shared class of boolean and integer constants.
uint64_t getTypeAllocSize(Type *Ty) const
Returns the offset in bytes between successive objects of the specified type, including alignment pad...
Value * CreateBitCast(Value *V, Type *DestTy, const Twine &Name="")
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small...
Type * getType() const
All values are typed, get the type of this value.
Instruction * user_back()
user_back - Specialize the methods defined in Value, as we know that an instruction can only be used ...
A collection of metadata nodes that might be associated with a memory access used by the alias-analys...
ConstantInt * getInt32(uint32_t C)
Get a constant 32-bit value.
static IntegerType * getIntNTy(LLVMContext &C, unsigned N)
Value * stripPointerCasts()
Strip off pointer casts, all-zero GEPs, and aliases.
static Constant * get(Type *Ty, uint64_t V, bool isSigned=false)
If Ty is a vector type, return a Constant with a splat of the given value.
static PHINode * Create(Type *Ty, unsigned NumReservedValues, const Twine &NameStr="", Instruction *InsertBefore=nullptr)
Constructors - NumReservedValues is a hint for the number of incoming edges that this phi node will h...
const BasicBlock & getEntryBlock() const
unsigned getPointerAddressSpace() const
Returns the address space of the pointer operand.
static cl::opt< AlignMode > Align(cl::desc("Load/store alignment support"), cl::Hidden, cl::init(NoStrictAlign), cl::values(clEnumValN(StrictAlign,"aarch64-strict-align","Disallow all unaligned memory accesses"), clEnumValN(NoStrictAlign,"aarch64-no-strict-align","Allow unaligned memory accesses"), clEnumValEnd))
static bool combineStoreToValueType(InstCombiner &IC, StoreInst &SI)
Combine stores to match the type of value being stored.
void setOperand(unsigned i, Value *Val)
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Class for arbitrary precision integers.
bool isIntegerTy() const
isIntegerTy - True if this is an instance of IntegerType.
LLVM_ATTRIBUTE_UNUSED_RESULT std::enable_if< !is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
static bool isObjectSizeLessThanOrEq(Value *V, uint64_t MaxSize, const DataLayout &DL)
Value * CreateInsertValue(Value *Agg, Value *Val, ArrayRef< unsigned > Idxs, const Twine &Name="")
const Type * getScalarType() const LLVM_READONLY
getScalarType - If this is a vector type, return the element type, otherwise return 'this'...
static Type * getIndexedType(Type *Ty, ArrayRef< Value * > IdxList)
getIndexedType - Returns the type of the element that would be loaded with a load instruction with th...
Value * getSource() const
getSource - This is just like getRawSource, but it strips off any cast instructions that feed it...
MemTransferInst - This class wraps the llvm.memcpy/memmove intrinsics.
bool isAggregateType() const
isAggregateType - Return true if the type is an aggregate type.
bool isLegalInteger(unsigned Width) const
Returns true if the specified type is known to be a native integer type supported by the CPU...
unsigned getAlignment() const
getAlignment - Return the alignment of the access that is being performed
void getAAMetadata(AAMDNodes &N, bool Merge=false) const
getAAMetadata - Fills the AAMDNodes structure with AA metadata from this instruction.
static Constant * getPtrToInt(Constant *C, Type *Ty, bool OnlyIfReduced=false)
TerminatorInst * getTerminator()
Returns the terminator instruction if the block is well formed or null if the block is not well forme...
bool hasOneUse() const
Return true if there is exactly one user of this value.
static ArrayType * get(Type *ElementType, uint64_t NumElements)
ArrayType::get - This static method is the primary way to construct an ArrayType. ...
Instruction * InsertNewInstBefore(Instruction *New, Instruction &Old)
Inserts an instruction New before instruction Old.
void ComputeSignBit(Value *V, bool &KnownZero, bool &KnownOne, unsigned Depth=0, Instruction *CxtI=nullptr) const
bool all_of(R &&Range, UnaryPredicate &&P)
Provide wrappers to std::all_of which take ranges instead of having to pass being/end explicitly...
user_iterator user_begin()
static bool canReplaceGEPIdxWithZero(InstCombiner &IC, GetElementPtrInst *GEPI, Instruction *MemI, unsigned &Idx)
Value * FindAvailableLoadedValue(Value *Ptr, BasicBlock *ScanBB, BasicBlock::iterator &ScanFrom, unsigned MaxInstsToScan=6, AliasAnalysis *AA=nullptr, AAMDNodes *AATags=nullptr)
FindAvailableLoadedValue - Scan the ScanBB block backwards (starting at the instruction before ScanFr...
Instruction * visitLoadInst(LoadInst &LI)
LLVM Value Representation.
void setAlignment(unsigned Align)
This file provides internal interfaces used to implement the InstCombine.
Instruction * visitStoreInst(StoreInst &SI)
const Value * getArraySize() const
getArraySize - Get the number of elements allocated.
Instruction * EraseInstFromFunction(Instruction &I)
Combiner aware instruction erasure.
void moveBefore(Instruction *MovePos)
moveBefore - Unlink this instruction from its current basic block and insert it into the basic block ...
uint64_t getTypeSizeInBits(Type *Ty) const
Size examples:
bool isSafeToLoadUnconditionally(Value *V, Instruction *ScanFrom, unsigned Align)
isSafeToLoadUnconditionally - Return true if we know that executing a load from this value cannot tra...
bool isSameOperationAs(const Instruction *I, unsigned flags=0) const
This function determines if the specified instruction executes the same operation as the current one...
Value * CreateBitOrPointerCast(Value *V, Type *DestTy, const Twine &Name="")
iterator getFirstInsertionPt()
Returns an iterator to the first instruction in this block that is suitable for inserting a non-PHI i...
Type * getAllocatedType() const
getAllocatedType - Return the type that is being allocated by the instruction.
Value * getPointerOperand()
static Instruction * unpackLoadToAggregate(InstCombiner &IC, LoadInst &LI)
Instruction * visitAllocSite(Instruction &FI)
void combineMetadata(Instruction *K, const Instruction *J, ArrayRef< unsigned > KnownIDs)
Combine the metadata of two instructions so that K can replace J.
const BasicBlock * getParent() const
IntrinsicInst - A useful wrapper class for inspecting calls to intrinsic functions.
AllocaInst - an instruction to allocate memory on the stack.