27using namespace PatternMatch;
29#define DEBUG_TYPE "instcombine"
31STATISTIC(NumDeadStore,
"Number of dead stores eliminated");
32STATISTIC(NumGlobalCopies,
"Number of allocas copied from constant global");
35 "instcombine-max-copied-from-constant-users",
cl::init(300),
36 cl::desc(
"Maximum users to visit in copy from constant transform"),
42 cl::desc(
"Enable the InferAlignment pass, disabling alignment inference in "
65 while (!Worklist.
empty()) {
67 if (!Visited.
insert(Elem).second)
72 const auto [
Value, IsOffset] = Elem;
74 auto *
I = cast<Instruction>(U.getUser());
76 if (
auto *LI = dyn_cast<LoadInst>(
I)) {
78 if (!LI->isSimple())
return false;
82 if (isa<PHINode, SelectInst>(
I)) {
89 if (isa<BitCastInst, AddrSpaceCastInst>(
I)) {
94 if (
auto *
GEP = dyn_cast<GetElementPtrInst>(
I)) {
101 if (
auto *Call = dyn_cast<CallBase>(
I)) {
104 if (Call->isCallee(&U))
107 unsigned DataOpNo = Call->getDataOperandNo(&U);
108 bool IsArgOperand = Call->isArgOperand(&U);
111 if (IsArgOperand && Call->isInAllocaArgument(DataOpNo))
117 bool NoCapture = Call->doesNotCapture(DataOpNo);
118 if ((Call->onlyReadsMemory() && (Call->use_empty() || NoCapture)) ||
119 (Call->onlyReadsMemory(DataOpNo) && NoCapture))
124 if (IsArgOperand && Call->isByValArgument(DataOpNo))
129 if (
I->isLifetimeStartOrEnd()) {
130 assert(
I->use_empty() &&
"Lifetime markers have no result to use!");
142 if (
MI->isVolatile())
147 if (U.getOperandNo() == 1)
151 if (TheCopy)
return false;
155 if (IsOffset)
return false;
158 if (U.getOperandNo() != 0)
return false;
211 if (
C->getValue().getActiveBits() <= 64) {
249class PointerReplacer {
252 : IC(IC), Root(Root), FromAS(SrcAS) {}
255 void replacePointer(
Value *V);
262 return I == &Root || Worklist.contains(
I);
266 unsigned FromAS)
const {
267 const auto *ASC = dyn_cast<AddrSpaceCastInst>(
I);
270 unsigned ToAS = ASC->getDestAddressSpace();
271 return (FromAS == ToAS) || IC.isValidAddrSpaceCast(FromAS, ToAS);
283bool PointerReplacer::collectUsers() {
284 if (!collectUsersRecursive(Root))
290 for (
auto *Inst : ValuesToRevisit)
291 if (!Worklist.contains(Inst))
296bool PointerReplacer::collectUsersRecursive(
Instruction &
I) {
297 for (
auto *U :
I.users()) {
298 auto *Inst = cast<Instruction>(&*U);
299 if (
auto *Load = dyn_cast<LoadInst>(Inst)) {
300 if (
Load->isVolatile())
302 Worklist.insert(Load);
303 }
else if (
auto *
PHI = dyn_cast<PHINode>(Inst)) {
306 [](
Value *V) { return !isa<Instruction>(V); }))
313 return !isAvailable(cast<Instruction>(V));
315 ValuesToRevisit.insert(Inst);
319 Worklist.insert(
PHI);
320 if (!collectUsersRecursive(*
PHI))
322 }
else if (
auto *SI = dyn_cast<SelectInst>(Inst)) {
323 if (!isa<Instruction>(
SI->getTrueValue()) ||
324 !isa<Instruction>(
SI->getFalseValue()))
329 ValuesToRevisit.insert(Inst);
333 if (!collectUsersRecursive(*SI))
335 }
else if (isa<GetElementPtrInst>(Inst)) {
336 Worklist.insert(Inst);
337 if (!collectUsersRecursive(*Inst))
339 }
else if (
auto *
MI = dyn_cast<MemTransferInst>(Inst)) {
340 if (
MI->isVolatile())
342 Worklist.insert(Inst);
343 }
else if (isEqualOrValidAddrSpaceCast(Inst, FromAS)) {
344 Worklist.insert(Inst);
345 if (!collectUsersRecursive(*Inst))
347 }
else if (Inst->isLifetimeStartOrEnd()) {
352 LLVM_DEBUG(
dbgs() <<
"Cannot handle pointer user: " << *U <<
'\n');
360Value *PointerReplacer::getReplacement(
Value *V) {
return WorkMap.lookup(V); }
363 if (getReplacement(
I))
366 if (
auto *LT = dyn_cast<LoadInst>(
I)) {
367 auto *
V = getReplacement(
LT->getPointerOperand());
368 assert(V &&
"Operand not replaced");
369 auto *NewI =
new LoadInst(
LT->getType(), V,
"",
LT->isVolatile(),
370 LT->getAlign(),
LT->getOrdering(),
371 LT->getSyncScopeID());
375 IC.InsertNewInstWith(NewI,
LT->getIterator());
376 IC.replaceInstUsesWith(*LT, NewI);
378 }
else if (
auto *
PHI = dyn_cast<PHINode>(
I)) {
379 Type *NewTy = getReplacement(
PHI->getIncomingValue(0))->getType();
381 PHI->getName(),
PHI->getIterator());
382 for (
unsigned int I = 0;
I <
PHI->getNumIncomingValues(); ++
I)
383 NewPHI->addIncoming(getReplacement(
PHI->getIncomingValue(
I)),
384 PHI->getIncomingBlock(
I));
385 WorkMap[
PHI] = NewPHI;
386 }
else if (
auto *
GEP = dyn_cast<GetElementPtrInst>(
I)) {
387 auto *
V = getReplacement(
GEP->getPointerOperand());
388 assert(V &&
"Operand not replaced");
392 IC.InsertNewInstWith(NewI,
GEP->getIterator());
394 NewI->setNoWrapFlags(
GEP->getNoWrapFlags());
396 }
else if (
auto *SI = dyn_cast<SelectInst>(
I)) {
397 Value *TrueValue =
SI->getTrueValue();
398 Value *FalseValue =
SI->getFalseValue();
399 if (
Value *Replacement = getReplacement(TrueValue))
400 TrueValue = Replacement;
401 if (
Value *Replacement = getReplacement(FalseValue))
402 FalseValue = Replacement;
404 SI->getName(),
nullptr, SI);
405 IC.InsertNewInstWith(NewSI,
SI->getIterator());
408 }
else if (
auto *MemCpy = dyn_cast<MemTransferInst>(
I)) {
409 auto *DestV = MemCpy->getRawDest();
410 auto *SrcV = MemCpy->getRawSource();
412 if (
auto *DestReplace = getReplacement(DestV))
414 if (
auto *SrcReplace = getReplacement(SrcV))
417 IC.Builder.SetInsertPoint(MemCpy);
418 auto *NewI = IC.Builder.CreateMemTransferInst(
419 MemCpy->getIntrinsicID(), DestV, MemCpy->getDestAlign(), SrcV,
420 MemCpy->getSourceAlign(), MemCpy->getLength(), MemCpy->isVolatile());
421 AAMDNodes AAMD = MemCpy->getAAMetadata();
423 NewI->setAAMetadata(AAMD);
425 IC.eraseInstFromFunction(*MemCpy);
426 WorkMap[MemCpy] = NewI;
427 }
else if (
auto *ASC = dyn_cast<AddrSpaceCastInst>(
I)) {
428 auto *
V = getReplacement(ASC->getPointerOperand());
429 assert(V &&
"Operand not replaced");
430 assert(isEqualOrValidAddrSpaceCast(
431 ASC,
V->getType()->getPointerAddressSpace()) &&
432 "Invalid address space cast!");
434 if (
V->getType()->getPointerAddressSpace() !=
435 ASC->getType()->getPointerAddressSpace()) {
438 IC.InsertNewInstWith(NewI, ASC->getIterator());
449void PointerReplacer::replacePointer(
Value *V) {
451 auto *PT = cast<PointerType>(Root.getType());
452 auto *
NT = cast<PointerType>(
V->getType());
453 assert(PT != NT &&
"Invalid usage");
480 if (FirstInst != &AI) {
484 AllocaInst *EntryAI = dyn_cast<AllocaInst>(FirstInst);
510 Value *TheSrc = Copy->getSource();
513 TheSrc, AllocaAlign,
DL, &AI, &
AC, &
DT);
514 if (AllocaAlign <= SourceAlign &&
516 !isa<Instruction>(TheSrc)) {
519 LLVM_DEBUG(
dbgs() <<
"Found alloca equal to global: " << AI <<
'\n');
532 PointerReplacer PtrReplacer(*
this, AI, SrcAddrSpace);
533 if (PtrReplacer.collectUsers()) {
537 PtrReplacer.replacePointer(TheSrc);
563 const Twine &Suffix) {
565 "can't fold an atomic load to requested type");
569 LI.isVolatile(),
LI.getName() + Suffix);
581 "can't fold an atomic store of requested type");
583 Value *
Ptr = SI.getPointerOperand();
585 SI.getAllMetadata(MD);
589 NewStore->
setAtomic(SI.getOrdering(), SI.getSyncScopeID());
590 for (
const auto &MDPair : MD) {
591 unsigned ID = MDPair.first;
602 case LLVMContext::MD_dbg:
603 case LLVMContext::MD_DIAssignID:
604 case LLVMContext::MD_tbaa:
605 case LLVMContext::MD_prof:
606 case LLVMContext::MD_fpmath:
607 case LLVMContext::MD_tbaa_struct:
608 case LLVMContext::MD_alias_scope:
609 case LLVMContext::MD_noalias:
610 case LLVMContext::MD_nontemporal:
611 case LLVMContext::MD_mem_parallel_loop_access:
612 case LLVMContext::MD_access_group:
616 case LLVMContext::MD_invariant_load:
617 case LLVMContext::MD_nonnull:
618 case LLVMContext::MD_noundef:
619 case LLVMContext::MD_range:
620 case LLVMContext::MD_align:
621 case LLVMContext::MD_dereferenceable:
622 case LLVMContext::MD_dereferenceable_or_null:
652 if (!Load.isUnordered())
655 if (Load.use_empty())
659 if (Load.getPointerOperand()->isSwiftError())
665 if (Load.hasOneUse()) {
668 Type *LoadTy = Load.getType();
669 if (
auto *BC = dyn_cast<BitCastInst>(Load.user_back())) {
671 if (BC->getType()->isX86_AMXTy())
675 if (
auto *CastUser = dyn_cast<CastInst>(Load.user_back())) {
676 Type *DestTy = CastUser->getDestTy();
700 if (!
T->isAggregateType())
705 if (
auto *ST = dyn_cast<StructType>(
T)) {
707 auto NumElements = ST->getNumElements();
708 if (NumElements == 1) {
719 auto *SL =
DL.getStructLayout(ST);
722 if (SL->getSizeInBits().isScalable())
725 if (SL->hasPadding())
731 auto *Zero = ConstantInt::get(IdxType, 0);
734 for (
unsigned i = 0; i < NumElements; i++) {
735 Value *Indices[2] = {
737 ConstantInt::get(IdxType, i),
742 ST->getElementType(i),
Ptr,
753 if (
auto *AT = dyn_cast<ArrayType>(
T)) {
754 auto *ET = AT->getElementType();
755 auto NumElements = AT->getNumElements();
756 if (NumElements == 1) {
776 auto *Zero = ConstantInt::get(IdxType, 0);
780 for (
uint64_t i = 0; i < NumElements; i++) {
781 Value *Indices[2] = {
783 ConstantInt::get(IdxType, i),
789 EltAlign,
Name +
".unpack");
815 P =
P->stripPointerCasts();
826 if (
PHINode *PN = dyn_cast<PHINode>(
P)) {
832 if (GA->isInterposable())
841 if (!AI->getAllocatedType()->isSized())
844 ConstantInt *CS = dyn_cast<ConstantInt>(AI->getArraySize());
848 TypeSize TS =
DL.getTypeAllocSize(AI->getAllocatedType());
860 if (!GV->hasDefinitiveInitializer() || !GV->isConstant())
863 uint64_t InitSize =
DL.getTypeAllocSize(GV->getValueType());
864 if (InitSize > MaxSize)
870 }
while (!Worklist.
empty());
902 if (
const ConstantInt *CI = dyn_cast<ConstantInt>(V))
914 Idx = FirstNZIdx(GEPI);
928 if (!AllocTy || !AllocTy->
isSized())
931 uint64_t TyAllocSize =
DL.getTypeAllocSize(AllocTy).getFixedValue();
937 auto IsAllNonNegative = [&]() {
972 ConstantInt::get(GEPI->getOperand(
Idx)->getType(), 0));
985 auto *
Ptr = SI.getPointerOperand();
987 Ptr = GEPI->getOperand(0);
988 return (isa<ConstantPointerNull>(
Ptr) &&
994 const Value *GEPI0 = GEPI->getOperand(0);
995 if (isa<ConstantPointerNull>(GEPI0) &&
999 if (isa<UndefValue>(
Op) ||
1000 (isa<ConstantPointerNull>(
Op) &&
1019 if (KnownAlign >
LI.getAlign())
1020 LI.setAlignment(KnownAlign);
1033 bool IsLoadCSE =
false;
1041 LI.getName() +
".cast"));
1046 if (!
LI.isUnordered())
return nullptr;
1056 if (
Op->hasOneUse()) {
1069 Align Alignment =
LI.getAlign();
1071 Alignment,
DL, SI) &&
1073 Alignment,
DL, SI)) {
1076 SI->getOperand(1)->getName() +
".val");
1079 SI->getOperand(2)->getName() +
".val");
1080 assert(
LI.isUnordered() &&
"implied by above");
1083 V2->setAlignment(Alignment);
1084 V2->setAtomic(
LI.getOrdering(),
LI.getSyncScopeID());
1089 if (isa<ConstantPointerNull>(SI->getOperand(1)) &&
1091 LI.getPointerAddressSpace()))
1095 if (isa<ConstantPointerNull>(SI->getOperand(2)) &&
1097 LI.getPointerAddressSpace()))
1120 while (
auto *
IV = dyn_cast<InsertValueInst>(V)) {
1121 auto *E = dyn_cast<ExtractElementInst>(
IV->getInsertedValueOperand());
1124 auto *W = E->getVectorOperand();
1129 auto *CI = dyn_cast<ConstantInt>(E->getIndexOperand());
1130 if (!CI ||
IV->getNumIndices() != 1 || CI->getZExtValue() != *
IV->idx_begin())
1132 V =
IV->getAggregateOperand();
1137 auto *UT = cast<VectorType>(U->getType());
1138 auto *VT = V->getType();
1141 if (
DL.getTypeStoreSizeInBits(UT) !=
DL.getTypeStoreSizeInBits(VT)) {
1144 if (
auto *AT = dyn_cast<ArrayType>(VT)) {
1145 if (AT->getNumElements() != cast<FixedVectorType>(UT)->getNumElements())
1148 auto *ST = cast<StructType>(VT);
1149 if (ST->getNumElements() != cast<FixedVectorType>(UT)->getNumElements())
1151 for (
const auto *EltT : ST->elements()) {
1152 if (EltT != UT->getElementType())
1182 if (!SI.isUnordered())
1186 if (SI.getPointerOperand()->isSwiftError())
1189 Value *V = SI.getValueOperand();
1192 if (
auto *BC = dyn_cast<BitCastInst>(V)) {
1193 assert(!BC->getType()->isX86_AMXTy() &&
1194 "store to x86_amx* should not happen!");
1195 V = BC->getOperand(0);
1198 if (V->getType()->isX86_AMXTy())
1223 Value *V = SI.getValueOperand();
1224 Type *
T = V->getType();
1226 if (!
T->isAggregateType())
1229 if (
auto *ST = dyn_cast<StructType>(
T)) {
1231 unsigned Count = ST->getNumElements();
1241 auto *SL =
DL.getStructLayout(ST);
1244 if (SL->getSizeInBits().isScalable())
1247 if (SL->hasPadding())
1250 const auto Align = SI.getAlign();
1254 auto *
Addr = SI.getPointerOperand();
1256 AddrName +=
".repack";
1259 auto *Zero = ConstantInt::get(IdxType, 0);
1260 for (
unsigned i = 0; i < Count; i++) {
1261 Value *Indices[2] = {
1263 ConstantInt::get(IdxType, i),
1276 if (
auto *AT = dyn_cast<ArrayType>(
T)) {
1278 auto NumElements = AT->getNumElements();
1279 if (NumElements == 1) {
1293 TypeSize EltSize =
DL.getTypeAllocSize(AT->getElementType());
1294 const auto Align = SI.getAlign();
1298 auto *
Addr = SI.getPointerOperand();
1300 AddrName +=
".repack";
1303 auto *Zero = ConstantInt::get(IdxType, 0);
1306 for (
uint64_t i = 0; i < NumElements; i++) {
1307 Value *Indices[2] = {
1309 ConstantInt::get(IdxType, i),
1336 if (
A ==
B)
return true;
1343 if (isa<BinaryOperator>(
A) ||
1346 isa<GetElementPtrInst>(
A))
1348 if (cast<Instruction>(
A)->isIdenticalToWhenDefined(BI))
1356 Value *Val = SI.getOperand(0);
1367 if (KnownAlign > SI.getAlign())
1368 SI.setAlignment(KnownAlign);
1381 if (!SI.isUnordered())
return nullptr;
1385 if (
Ptr->hasOneUse()) {
1386 if (isa<AllocaInst>(
Ptr))
1389 if (isa<AllocaInst>(
GEP->getOperand(0))) {
1390 if (
GEP->getOperand(0)->hasOneUse())
1406 for (
unsigned ScanInsts = 6; BBI != SI.getParent()->begin() && ScanInsts;
1411 if (BBI->isDebugOrPseudoInst()) {
1416 if (
StoreInst *PrevSI = dyn_cast<StoreInst>(BBI)) {
1418 if (PrevSI->isUnordered() &&
1420 PrevSI->getValueOperand()->getType() ==
1421 SI.getValueOperand()->getType()) {
1436 if (
LoadInst *
LI = dyn_cast<LoadInst>(BBI)) {
1438 assert(SI.isUnordered() &&
"can't eliminate ordering operation");
1448 if (BBI->mayWriteToMemory() || BBI->mayReadFromMemory() || BBI->mayThrow())
1455 if (!isa<PoisonValue>(Val))
1461 if (isa<UndefValue>(
Ptr)) {
1477 if (isa<UndefValue>(Val))
1489 if (!SI.isUnordered())
1500 if (*PredIter == StoreBB)
1506 if (StoreBB == DestBB || OtherBB == DestBB)
1511 BranchInst *OtherBr = dyn_cast<BranchInst>(BBI);
1512 if (!OtherBr || BBI == OtherBB->
begin())
1515 auto OtherStoreIsMergeable = [&](
StoreInst *OtherStore) ->
bool {
1517 OtherStore->getPointerOperand() != SI.getPointerOperand())
1520 auto *SIVTy = SI.getValueOperand()->getType();
1521 auto *OSVTy = OtherStore->getValueOperand()->getType();
1523 SI.hasSameSpecialState(OtherStore);
1532 while (BBI->isDebugOrPseudoInst()) {
1533 if (BBI==OtherBB->
begin())
1539 OtherStore = dyn_cast<StoreInst>(BBI);
1540 if (!OtherStoreIsMergeable(OtherStore))
1554 OtherStore = dyn_cast<StoreInst>(BBI);
1555 if (OtherStoreIsMergeable(OtherStore))
1560 if (BBI->mayReadFromMemory() || BBI->mayThrow() ||
1561 BBI->mayWriteToMemory() || BBI == OtherBB->
begin())
1569 if (
I->mayReadFromMemory() ||
I->mayThrow() ||
I->mayWriteToMemory())
1579 if (MergedVal != SI.getValueOperand()) {
1582 PN->
addIncoming(SI.getValueOperand(), SI.getParent());
1593 new StoreInst(MergedVal, SI.getOperand(1), SI.isVolatile(), SI.getAlign(),
1594 SI.getOrdering(), SI.getSyncScopeID());
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
Returns the sub type a function will return at a given Idx Should correspond to the result type of an ExtractValue instruction executed with just that one unsigned Idx
This file provides internal interfaces used to implement the InstCombine.
static StoreInst * combineStoreToNewValue(InstCombinerImpl &IC, StoreInst &SI, Value *V)
Combine a store to a new type.
static Instruction * combineLoadToOperationType(InstCombinerImpl &IC, LoadInst &Load)
Combine loads to match the type of their uses' value after looking through intervening bitcasts.
static Instruction * replaceGEPIdxWithZero(InstCombinerImpl &IC, Value *Ptr, Instruction &MemI)
static Instruction * simplifyAllocaArraySize(InstCombinerImpl &IC, AllocaInst &AI, DominatorTree &DT)
static bool canSimplifyNullStoreOrGEP(StoreInst &SI)
static bool equivalentAddressValues(Value *A, Value *B)
equivalentAddressValues - Test if A and B will obviously have the same value.
static bool canReplaceGEPIdxWithZero(InstCombinerImpl &IC, GetElementPtrInst *GEPI, Instruction *MemI, unsigned &Idx)
static bool canSimplifyNullLoadOrGEP(LoadInst &LI, Value *Op)
static bool isSupportedAtomicType(Type *Ty)
static bool isDereferenceableForAllocaSize(const Value *V, const AllocaInst *AI, const DataLayout &DL)
Returns true if V is dereferenceable for size of alloca.
static Instruction * unpackLoadToAggregate(InstCombinerImpl &IC, LoadInst &LI)
static 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)
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 Value * likeBitCastFromVector(InstCombinerImpl &IC, Value *V)
Look for extractelement/insertvalue sequence that acts like a bitcast.
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 bool isObjectSizeLessThanOrEq(Value *V, uint64_t MaxSize, const DataLayout &DL)
This file provides the interface for the instcombine pass implementation.
This file implements a map that provides insertion order iteration.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
This file defines the SmallString class.
This file defines the 'Statistic' class, which is designed to be an easy way to expose various metric...
#define STATISTIC(VARNAME, DESC)
static const uint32_t IV[8]
ModRefInfo getModRefInfoMask(const MemoryLocation &Loc, bool IgnoreLocals=false)
Returns a bitmask that should be unconditionally applied to the ModRef info of a memory location.
Class for arbitrary precision integers.
APInt zext(unsigned width) const
Zero extend to a new width.
This class represents a conversion between pointers from one address space to another.
an instruction to allocate memory on the stack
Align getAlign() const
Return the alignment of the memory that is being allocated by the instruction.
PointerType * getType() const
Overload to return most specific pointer type.
Type * getAllocatedType() const
Return the type that is being allocated by the instruction.
bool isUsedWithInAlloca() const
Return true if this alloca is used as an inalloca argument to a call.
unsigned getAddressSpace() const
Return the address space for the allocation.
bool isArrayAllocation() const
Return true if there is an allocation size parameter to the allocation instruction that is not 1.
void setAlignment(Align Align)
const Value * getArraySize() const
Get the number of elements allocated.
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
LLVM Basic Block Representation.
iterator begin()
Instruction iterator methods.
const_iterator getFirstInsertionPt() const
Returns an iterator to the first instruction in this block that is suitable for inserting a non-PHI i...
bool hasNPredecessors(unsigned N) const
Return true if this block has exactly N predecessors.
const Instruction * getFirstNonPHIOrDbg(bool SkipPseudoOp=true) const
Returns a pointer to the first instruction in this block that is not a PHINode or a debug intrinsic,...
InstListType::iterator iterator
Instruction iterators...
const Instruction * getTerminator() const LLVM_READONLY
Returns the terminator instruction if the block is well formed or null if the block is not well forme...
This class is a wrapper over an AAResults, and it is intended to be used only when there are no IR ch...
Conditional or Unconditional Branch instruction.
BasicBlock * getSuccessor(unsigned i) const
bool isUnconditional() const
static bool isBitOrNoopPointerCastable(Type *SrcTy, Type *DestTy, const DataLayout &DL)
Check whether a bitcast, inttoptr, or ptrtoint cast between these types is valid and a no-op.
This is the shared class of boolean and integer constants.
const APInt & getValue() const
Return the constant as an APInt value reference.
static Constant * getNullValue(Type *Ty)
Constructor to create a '0' constant of arbitrary type.
static DILocation * getMergedLocation(DILocation *LocA, DILocation *LocB)
When two instructions are combined into a single instruction we also need to combine the original loc...
This class represents an Operation in the Expression.
A parsed version of the target data layout string in and methods for querying it.
IntegerType * getIndexType(LLVMContext &C, unsigned AddressSpace) const
Returns the type of a GEP index in AddressSpace.
TypeSize getTypeAllocSize(Type *Ty) const
Returns the offset in bytes between successive objects of the specified type, including alignment pad...
Align getPrefTypeAlign(Type *Ty) const
Returns the preferred stack/global alignment for the specified type.
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree.
an instruction for type-safe pointer arithmetic to access elements of arrays and structs
bool isInBounds() const
Determine whether the GEP has the inbounds flag.
static GetElementPtrInst * Create(Type *PointeeType, Value *Ptr, ArrayRef< Value * > IdxList, const Twine &NameStr="", InsertPosition InsertBefore=nullptr)
static Type * getIndexedType(Type *Ty, ArrayRef< Value * > IdxList)
Returns the result type of a getelementptr with the given source element type and indexes.
Type * getSourceElementType() const
AllocaInst * CreateAlloca(Type *Ty, unsigned AddrSpace, Value *ArraySize=nullptr, const Twine &Name="")
Value * CreateInsertValue(Value *Agg, Value *Val, ArrayRef< unsigned > Idxs, const Twine &Name="")
LoadInst * CreateAlignedLoad(Type *Ty, Value *Ptr, MaybeAlign Align, const char *Name)
Value * CreateExtractValue(Value *Agg, ArrayRef< unsigned > Idxs, const Twine &Name="")
Value * CreateInBoundsGEP(Type *Ty, Value *Ptr, ArrayRef< Value * > IdxList, const Twine &Name="")
ConstantInt * getInt32(uint32_t C)
Get a constant 32-bit value.
Value * CreateBitOrPointerCast(Value *V, Type *DestTy, const Twine &Name="")
LoadInst * CreateLoad(Type *Ty, Value *Ptr, const char *Name)
Provided to resolve 'CreateLoad(Ty, Ptr, "...")' correctly, instead of converting the string to 'bool...
Value * CreateIntCast(Value *V, Type *DestTy, bool isSigned, const Twine &Name="")
void SetInsertPoint(BasicBlock *TheBB)
This specifies that created instructions should be appended to the end of the specified block.
StoreInst * CreateAlignedStore(Value *Val, Value *Ptr, MaybeAlign Align, bool isVolatile=false)
void handleUnreachableFrom(Instruction *I, SmallVectorImpl< BasicBlock * > &Worklist)
Instruction * visitLoadInst(LoadInst &LI)
void handlePotentiallyDeadBlocks(SmallVectorImpl< BasicBlock * > &Worklist)
Instruction * eraseInstFromFunction(Instruction &I) override
Combiner aware instruction erasure.
Instruction * visitStoreInst(StoreInst &SI)
bool mergeStoreIntoSuccessor(StoreInst &SI)
Try to transform: if () { *P = v1; } else { *P = v2 } or: *P = v1; if () { *P = v2; } into a phi node...
void CreateNonTerminatorUnreachable(Instruction *InsertAt)
Create and insert the idiom we use to indicate a block is unreachable without having to rewrite the C...
bool removeInstructionsBeforeUnreachable(Instruction &I)
LoadInst * combineLoadToNewType(LoadInst &LI, Type *NewTy, const Twine &Suffix="")
Helper to combine a load to a new type.
Instruction * visitAllocSite(Instruction &FI)
Instruction * visitAllocaInst(AllocaInst &AI)
const DataLayout & getDataLayout() const
Instruction * InsertNewInstBefore(Instruction *New, BasicBlock::iterator Old)
Inserts an instruction New before instruction Old.
Instruction * replaceInstUsesWith(Instruction &I, Value *V)
A combiner-aware RAUW-like routine.
uint64_t MaxArraySizeForCombine
Maximum size of array considered when transforming.
InstructionWorklist & Worklist
A worklist of the instructions that need to be simplified.
Instruction * replaceOperand(Instruction &I, unsigned OpNum, Value *V)
Replace operand of instruction and add old operand to the worklist.
void computeKnownBits(const Value *V, KnownBits &Known, unsigned Depth, const Instruction *CxtI) const
void push(Instruction *I)
Push the instruction onto the worklist stack.
Instruction * clone() const
Create a copy of 'this' instruction that is identical in all ways except the following:
void mergeDIAssignID(ArrayRef< const Instruction * > SourceInstructions)
Merge the DIAssignID metadata from this instruction and those attached to instructions in SourceInstr...
const DebugLoc & getDebugLoc() const
Return the debug location for this node as a DebugLoc.
void setAAMetadata(const AAMDNodes &N)
Sets the AA metadata on this instruction from the AAMDNodes structure.
const Function * getFunction() const
Return the function this instruction belongs to.
BasicBlock * getSuccessor(unsigned Idx) const LLVM_READONLY
Return the specified successor. This instruction must be a terminator.
void setMetadata(unsigned KindID, MDNode *Node)
Set the metadata of the specified kind to the specified node.
AAMDNodes getAAMetadata() const
Returns the AA metadata for this instruction.
void setDebugLoc(DebugLoc Loc)
Set the debug location information for this instruction.
void moveBefore(Instruction *MovePos)
Unlink this instruction from its current basic block and insert it into the basic block that MovePos ...
An instruction for reading from memory.
unsigned getPointerAddressSpace() const
Returns the address space of the pointer operand.
void setAlignment(Align Align)
Value * getPointerOperand()
void setAtomic(AtomicOrdering Ordering, SyncScope::ID SSID=SyncScope::System)
Sets the ordering constraint and the synchronization scope ID of this load instruction.
Align getAlign() const
Return the alignment of the access that is being performed.
This class implements a map that also provides access to all stored values in a deterministic order.
This class wraps the llvm.memcpy/memmove intrinsics.
void addIncoming(Value *V, BasicBlock *BB)
Add an incoming value to the end of the PHI list.
static PHINode * Create(Type *Ty, unsigned NumReservedValues, const Twine &NameStr="", InsertPosition InsertBefore=nullptr)
Constructors - NumReservedValues is a hint for the number of incoming edges that this phi node will h...
PointerIntPair - This class implements a pair of a pointer and small integer.
static PoisonValue * get(Type *T)
Static factory methods - Return an 'poison' object of the specified type.
This class represents the LLVM 'select' instruction.
static SelectInst * Create(Value *C, Value *S1, Value *S2, const Twine &NameStr="", InsertPosition InsertBefore=nullptr, Instruction *MDFrom=nullptr)
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements.
A SetVector that performs no allocations if smaller than a certain size.
SmallString - A SmallString is just a SmallVector with methods and accessors that make it work better...
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
reference emplace_back(ArgTypes &&... Args)
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
An instruction for storing to memory.
Value * getValueOperand()
void setAtomic(AtomicOrdering Ordering, SyncScope::ID SSID=SyncScope::System)
Sets the ordering constraint and the synchronization scope ID of this store instruction.
StringRef - Represent a constant reference to a string, i.e.
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
static constexpr TypeSize getZero()
The instances of the Type class are immutable: once they are created, they are never changed.
unsigned getPointerAddressSpace() const
Get the address space of this pointer or pointer vector type.
bool isSized(SmallPtrSetImpl< Type * > *Visited=nullptr) const
Return true if it makes sense to take the size of this type.
bool isFloatingPointTy() const
Return true if this is one of the floating-point types.
bool isPtrOrPtrVectorTy() const
Return true if this is a pointer type or a vector of pointer types.
bool isX86_AMXTy() const
Return true if this is X86 AMX.
bool isScalableTy() const
Return true if this is a type whose size is a known multiple of vscale.
bool isIntOrPtrTy() const
Return true if this is an integer type or a pointer type.
static IntegerType * getInt32Ty(LLVMContext &C)
static IntegerType * getInt64Ty(LLVMContext &C)
bool isIntegerTy() const
True if this is an instance of IntegerType.
void setOperand(unsigned i, Value *Val)
Value * getOperand(unsigned i) const
unsigned getNumOperands() const
LLVM Value Representation.
Type * getType() const
All values are typed, get the type of this value.
void replaceAllUsesWith(Value *V)
Change all uses of this to point to a new Value.
iterator_range< use_iterator > uses()
StringRef getName() const
Return a constant reference to the value's name.
constexpr ScalarTy getFixedValue() const
constexpr bool isScalable() const
Returns whether the quantity is scaled by a runtime quantity (vscale).
constexpr ScalarTy getKnownMinValue() const
Returns the minimum value this quantity can represent.
const ParentTy * getParent() const
self_iterator getIterator()
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
@ C
The default llvm calling convention, compatible with C.
bool match(Val *V, const Pattern &P)
auto m_Undef()
Match an arbitrary undef constant.
initializer< Ty > init(const Ty &Val)
This is an optimization pass for GlobalISel generic memory operations.
bool isSafeToLoadUnconditionally(Value *V, Align Alignment, const APInt &Size, const DataLayout &DL, Instruction *ScanFrom=nullptr, AssumptionCache *AC=nullptr, const DominatorTree *DT=nullptr, const TargetLibraryInfo *TLI=nullptr)
Return true if we know that executing a load from this value cannot trap.
bool isDereferenceableAndAlignedPointer(const Value *V, Type *Ty, Align Alignment, const DataLayout &DL, const Instruction *CtxI=nullptr, AssumptionCache *AC=nullptr, const DominatorTree *DT=nullptr, const TargetLibraryInfo *TLI=nullptr)
Returns true if V is always a dereferenceable pointer with alignment greater or equal than requested.
void copyMetadataForLoad(LoadInst &Dest, const LoadInst &Source)
Copy the metadata from the source instruction to the destination (the replacement for the source inst...
void append_range(Container &C, Range &&R)
Wrapper function to append range R to container C.
Value * FindAvailableLoadedValue(LoadInst *Load, BasicBlock *ScanBB, BasicBlock::iterator &ScanFrom, unsigned MaxInstsToScan=DefMaxInstsToScan, BatchAAResults *AA=nullptr, bool *IsLoadCSE=nullptr, unsigned *NumScanedInst=nullptr)
Scan backwards to see if we have the value of the given load available locally within a small number ...
bool any_of(R &&range, UnaryPredicate P)
Provide wrappers to std::any_of which take ranges instead of having to pass begin/end explicitly.
pred_iterator pred_begin(BasicBlock *BB)
Align getOrEnforceKnownAlignment(Value *V, MaybeAlign PrefAlign, const DataLayout &DL, const Instruction *CxtI=nullptr, AssumptionCache *AC=nullptr, const DominatorTree *DT=nullptr)
Try to ensure that the alignment of V is at least PrefAlign bytes.
bool isModSet(const ModRefInfo MRI)
bool NullPointerIsDefined(const Function *F, unsigned AS=0)
Check whether null pointer dereferencing is considered undefined behavior for a given function or an ...
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
bool replaceAllDbgUsesWith(Instruction &From, Value &To, Instruction &DomPoint, DominatorTree &DT)
Point debug users of From to To or salvage them.
Value * simplifyLoadInst(LoadInst *LI, Value *PtrOp, const SimplifyQuery &Q)
Given a load instruction and its pointer operand, fold the result or return null.
void combineMetadataForCSE(Instruction *K, const Instruction *J, bool DoesKMove)
Combine the metadata of two instructions so that K can replace J.
cl::opt< bool > EnableInferAlignmentPass
void replace(Container &Cont, typename Container::iterator ContIt, typename Container::iterator ContEnd, RandomAccessIterator ValIt, RandomAccessIterator ValEnd)
Given a sequence container Cont, replace the range [ContIt, ContEnd) with the range [ValIt,...
Align commonAlignment(Align A, uint64_t Offset)
Returns the alignment that satisfies both alignments.
A collection of metadata nodes that might be associated with a memory access used by the alias-analys...
AAMDNodes merge(const AAMDNodes &Other) const
Given two sets of AAMDNodes applying to potentially different locations, determine the best AAMDNodes...
This struct is a compact representation of a valid (non-zero power of two) alignment.
bool isNonNegative() const
Returns true if this value is known to be non-negative.
SimplifyQuery getWithInstruction(const Instruction *I) const