28#define DEBUG_TYPE "instcombine"
32 cl::desc(
"Maximum number phis to handle in intptr/ptrint folding"));
35 "Number of phi-of-insertvalue turned into insertvalue-of-phis");
37 "Number of phi-of-extractvalue turned into extractvalue-of-phi");
38STATISTIC(NumPHICSEs,
"Number of PHI's that got CSE'd");
48 assert(!isa<CallInst>(Inst));
51 auto *
I = cast<Instruction>(V);
112 auto *IntToPtr = dyn_cast<IntToPtrInst>(PN.
user_back());
118 for (
User *U : IIP->users()) {
120 if (
LoadInst *LoadI = dyn_cast<LoadInst>(U)) {
121 Ptr = LoadI->getPointerOperand();
122 }
else if (
StoreInst *SI = dyn_cast<StoreInst>(U)) {
123 Ptr = SI->getPointerOperand();
125 Ptr = GI->getPointerOperand();
134 if (!HasPointerUse(IntToPtr))
147 if (!isa<Instruction>(Arg) && !isa<Argument>(Arg))
151 if (
auto *PI = dyn_cast<PtrToIntInst>(Arg)) {
157 Value *ArgIntToPtr =
nullptr;
159 if (isa<IntToPtrInst>(U) && U->getType() == IntToPtr->getType() &&
161 cast<Instruction>(U)->getParent() == BB)) {
174 if (isa<PHINode>(Arg)) {
180 auto *LoadI = dyn_cast<LoadInst>(Arg);
184 if (!LoadI->hasOneUse())
196 "Not enough available ptr typed incoming values");
197 PHINode *MatchingPtrPHI =
nullptr;
198 unsigned NumPhis = 0;
199 for (
PHINode &PtrPHI : BB->phis()) {
203 if (&PtrPHI == &PN || PtrPHI.
getType() != IntToPtr->getType())
206 [&](
const auto &BlockAndValue) {
207 BasicBlock *BB = std::get<0>(BlockAndValue);
208 Value *V = std::get<1>(BlockAndValue);
209 return PtrPHI.getIncomingValueForBlock(BB) != V;
212 MatchingPtrPHI = &PtrPHI;
216 if (MatchingPtrPHI) {
218 "Phi's Type does not match with IntToPtr");
229 return (V->getType() != IntToPtr->getType()) || isa<IntToPtrInst>(V);
238 if (V->getType() == IntToPtr->getType())
240 auto *Inst = dyn_cast<Instruction>(V);
243 if (Inst->isTerminator())
245 auto *BB = Inst->getParent();
246 if (isa<PHINode>(Inst) && BB->getFirstInsertionPt() == BB->end())
258 auto *IncomingBB = std::get<0>(
Incoming);
259 auto *IncomingVal = std::get<1>(
Incoming);
261 if (IncomingVal->getType() == IntToPtr->getType()) {
267 LoadInst *LoadI = dyn_cast<LoadInst>(IncomingVal);
268 assert((isa<PHINode>(IncomingVal) ||
269 IncomingVal->getType()->isPointerTy() ||
271 "Can not replace LoadInst with multiple uses");
284 IncomingVal->getName() +
".ptr");
285 if (
auto *IncomingI = dyn_cast<Instruction>(IncomingVal)) {
289 if (isa<PHINode>(IncomingI))
291 assert(InsertPos != BB->
end() &&
"should have checked above");
294 auto *InsertBB = &IncomingBB->getParent()->getEntryBlock();
319 bool OperandWithRoundTripCast =
false;
324 OperandWithRoundTripCast =
true;
327 if (!OperandWithRoundTripCast)
341 auto *
I = dyn_cast<InsertValueInst>(V);
342 if (!
I || !
I->hasOneUser() ||
I->getIndices() != FirstIVI->getIndices())
347 std::array<PHINode *, 2> NewOperands;
348 for (
int OpIdx : {0, 1}) {
349 auto *&NewOperand = NewOperands[OpIdx];
354 FirstIVI->getOperand(OpIdx)->getName() +
".pn");
357 NewOperand->addIncoming(
358 cast<InsertValueInst>(std::get<1>(
Incoming))->getOperand(OpIdx),
365 FirstIVI->getIndices(), PN.
getName());
368 ++NumPHIsOfInsertValues;
381 auto *
I = dyn_cast<ExtractValueInst>(V);
382 if (!
I || !
I->hasOneUser() ||
I->getIndices() != FirstEVI->getIndices() ||
383 I->getAggregateOperand()->getType() !=
384 FirstEVI->getAggregateOperand()->getType())
392 FirstEVI->getAggregateOperand()->getName() +
".pn");
395 NewAggregateOperand->addIncoming(
396 cast<ExtractValueInst>(std::get<1>(
Incoming))->getAggregateOperand(),
402 FirstEVI->getIndices(), PN.
getName());
405 ++NumPHIsOfExtractValues;
413 assert(isa<BinaryOperator>(FirstInst) || isa<CmpInst>(FirstInst));
424 if (!
I ||
I->getOpcode() != Opc || !
I->hasOneUser() ||
427 I->getOperand(0)->getType() != LHSType ||
428 I->getOperand(1)->getType() != RHSType)
432 if (
CmpInst *CI = dyn_cast<CmpInst>(
I))
433 if (CI->getPredicate() != cast<CmpInst>(FirstInst)->getPredicate())
437 if (
I->getOperand(0) != LHSVal) LHSVal =
nullptr;
438 if (
I->getOperand(1) != RHSVal) RHSVal =
nullptr;
445 if (!LHSVal && !RHSVal)
452 PHINode *NewLHS =
nullptr, *NewRHS =
nullptr;
470 if (NewLHS || NewRHS) {
481 NewRHS->addIncoming(NewInRHS, InBB);
486 if (
CmpInst *CIOp = dyn_cast<CmpInst>(FirstInst)) {
513 bool AllBasePointersAreAllocas =
true;
518 bool NeededPhi =
false;
525 if (!
GEP || !
GEP->hasOneUser() ||
530 NW &=
GEP->getNoWrapFlags();
533 if (AllBasePointersAreAllocas &&
534 (!isa<AllocaInst>(
GEP->getOperand(0)) ||
535 !
GEP->hasAllConstantIndices()))
536 AllBasePointersAreAllocas =
false;
549 isa<ConstantInt>(
GEP->getOperand(
Op)))
553 GEP->getOperand(
Op)->getType())
563 FixedOperands[
Op] =
nullptr;
574 if (AllBasePointersAreAllocas)
581 bool HasAnyPHIs =
false;
582 for (
unsigned I = 0, E = FixedOperands.
size();
I != E; ++
I) {
583 if (FixedOperands[
I])
591 OperandPhis[
I] = NewPN;
592 FixedOperands[
I] = NewPN;
603 for (
unsigned Op = 0, E = OperandPhis.
size();
Op != E; ++
Op)
612 ArrayRef(FixedOperands).slice(1), NW);
627 for (++BBI; BBI != E; ++BBI)
628 if (BBI->mayWriteToMemory()) {
631 if (
auto *CB = dyn_cast<CallBase>(BBI))
632 if (CB->onlyAccessesInaccessibleMemory())
639 if (
AllocaInst *AI = dyn_cast<AllocaInst>(L->getOperand(0))) {
640 bool IsAddressTaken =
false;
641 for (
User *U : AI->users()) {
642 if (isa<LoadInst>(U))
continue;
643 if (
StoreInst *SI = dyn_cast<StoreInst>(U)) {
645 if (SI->getOperand(1) == AI)
continue;
647 IsAddressTaken =
true;
651 if (!IsAddressTaken && AI->isStaticAlloca())
661 if (
AllocaInst *AI = dyn_cast<AllocaInst>(
GEP->getOperand(0)))
662 if (AI->isStaticAlloca() &&
GEP->hasAllConstantIndices())
696 FirstLI->
getParent()->getTerminator()->getNumSuccessors() != 1)
703 if (!
LI || !
LI->hasOneUser() ||
LI->isAtomic())
707 if (
LI->isVolatile() != IsVolatile ||
708 LI->getPointerAddressSpace() != LoadAddrSpace)
712 if (
LI->getOperand(0)->isSwiftError())
720 LoadAlignment = std::min(LoadAlignment,
LI->getAlign());
725 if (IsVolatile &&
LI->getParent()->getTerminator()->getNumSuccessors() != 1)
738 new LoadInst(FirstLI->
getType(), NewPN,
"", IsVolatile, LoadAlignment);
740 unsigned KnownIDs[] = {
741 LLVMContext::MD_tbaa,
742 LLVMContext::MD_range,
743 LLVMContext::MD_invariant_load,
744 LLVMContext::MD_alias_scope,
745 LLVMContext::MD_noalias,
746 LLVMContext::MD_nonnull,
747 LLVMContext::MD_align,
748 LLVMContext::MD_dereferenceable,
749 LLVMContext::MD_dereferenceable_or_null,
750 LLVMContext::MD_access_group,
751 LLVMContext::MD_noundef,
754 for (
unsigned ID : KnownIDs)
763 Value *NewInVal =
LI->getOperand(0);
764 if (NewInVal != InVal)
783 cast<LoadInst>(IncValue)->setVolatile(
false);
795 if (
Instruction *TI = Phi.getParent()->getTerminator())
802 unsigned NumIncomingValues = Phi.getNumIncomingValues();
803 if (NumIncomingValues < 3)
807 Type *NarrowType =
nullptr;
808 for (
Value *V : Phi.incoming_values()) {
809 if (
auto *Zext = dyn_cast<ZExtInst>(V)) {
810 NarrowType = Zext->getSrcTy();
820 unsigned NumZexts = 0;
821 unsigned NumConsts = 0;
822 for (
Value *V : Phi.incoming_values()) {
823 if (
auto *Zext = dyn_cast<ZExtInst>(V)) {
825 if (Zext->getSrcTy() != NarrowType || !Zext->hasOneUser())
827 NewIncoming.
push_back(Zext->getOperand(0));
829 }
else if (
auto *
C = dyn_cast<Constant>(V)) {
848 if (NumConsts == 0 || NumZexts < 2)
855 Phi.getName() +
".shrunk");
856 for (
unsigned I = 0;
I != NumIncomingValues; ++
I)
857 NewPhi->
addIncoming(NewIncoming[
I], Phi.getIncomingBlock(
I));
875 if (isa<GetElementPtrInst>(FirstInst))
877 if (isa<LoadInst>(FirstInst))
879 if (isa<InsertValueInst>(FirstInst))
881 if (isa<ExtractValueInst>(FirstInst))
889 Type *CastSrcTy =
nullptr;
891 if (isa<CastInst>(FirstInst)) {
897 if (!shouldChangeType(PN.
getType(), CastSrcTy))
900 }
else if (isa<BinaryOperator>(FirstInst) || isa<CmpInst>(FirstInst)) {
903 ConstantOp = dyn_cast<Constant>(FirstInst->
getOperand(1));
913 if (!
I || !
I->hasOneUser() || !
I->isSameOperationAs(FirstInst))
916 if (
I->getOperand(0)->getType() != CastSrcTy)
918 }
else if (
I->getOperand(1) != ConstantOp) {
936 Value *NewInVal = cast<Instruction>(V)->getOperand(0);
937 if (NewInVal != InVal)
954 if (
CastInst *FirstCI = dyn_cast<CastInst>(FirstInst)) {
961 if (
BinaryOperator *BinOp = dyn_cast<BinaryOperator>(FirstInst)) {
966 BinOp->andIRFlags(V);
972 CmpInst *CIOp = cast<CmpInst>(FirstInst);
986 if (!PotentiallyDeadPHIs.
insert(PN).second)
990 if (PotentiallyDeadPHIs.
size() == 16)
1005 if (!ValueEqualPHIs.
insert(PN).second)
1009 if (ValueEqualPHIs.
size() == 16)
1015 if (
PHINode *OpPN = dyn_cast<PHINode>(
Op)) {
1021 }
else if (
Op != NonPhiInVal)
1031 assert(isa<IntegerType>(PN.
getType()) &&
"Expect only integer type phi");
1033 if (
auto *ConstVA = dyn_cast<ConstantInt>(V))
1034 if (!ConstVA->isZero())
1036 return ConstantInt::get(cast<IntegerType>(PN.
getType()), 1);
1040struct PHIUsageRecord {
1046 : PHIId(Pn), Shift(Sh), Inst(
User) {}
1049 if (PHIId <
RHS.PHIId)
return true;
1050 if (PHIId >
RHS.PHIId)
return false;
1051 if (Shift <
RHS.Shift)
return true;
1052 if (Shift >
RHS.Shift)
return false;
1058struct LoweredPHIRecord {
1063 LoweredPHIRecord(
PHINode *Phi,
unsigned Sh,
Type *Ty)
1064 : PN(
Phi), Shift(Sh), Width(Ty->getPrimitiveSizeInBits()) {}
1067 LoweredPHIRecord(
PHINode *Phi,
unsigned Sh) : PN(
Phi), Shift(Sh), Width(0) {}
1075 return LoweredPHIRecord(
nullptr, 0);
1078 return LoweredPHIRecord(
nullptr, 1);
1085 const LoweredPHIRecord &RHS) {
1114 PHIsInspected.
insert(&FirstPhi);
1116 for (
unsigned PHIId = 0; PHIId != PHIsToSlice.
size(); ++PHIId) {
1117 PHINode *PN = PHIsToSlice[PHIId];
1129 if (
II->getParent() != BB)
1141 for (
auto *Pred : PN->
blocks())
1142 if (Pred->getFirstInsertionPt() == Pred->end())
1149 if (
PHINode *UserPN = dyn_cast<PHINode>(UserI)) {
1150 if (PHIsInspected.
insert(UserPN).second)
1156 if (isa<TruncInst>(UserI)) {
1157 PHIUsers.
push_back(PHIUsageRecord(PHIId, 0, UserI));
1162 if (UserI->
getOpcode() != Instruction::LShr ||
1169 if (cast<ConstantInt>(UserI->
getOperand(1))->getValue().uge(SizeInBits))
1172 unsigned Shift = cast<ConstantInt>(UserI->
getOperand(1))->getZExtValue();
1178 if (PHIUsers.
empty())
1186 for (
unsigned I = 1;
I != PHIsToSlice.
size(); ++
I)
dbgs()
1187 <<
"AND USER PHI #" <<
I <<
": " << *PHIsToSlice[
I] <<
'\n');
1197 for (
unsigned UserI = 0, UserE = PHIUsers.
size(); UserI != UserE; ++UserI) {
1198 unsigned PHIId = PHIUsers[UserI].PHIId;
1199 PHINode *PN = PHIsToSlice[PHIId];
1200 unsigned Offset = PHIUsers[UserI].Shift;
1201 Type *Ty = PHIUsers[UserI].Inst->getType();
1207 if ((EltPHI = ExtractedVals[LoweredPHIRecord(PN,
Offset, Ty)]) ==
nullptr) {
1214 "Truncate didn't shrink phi?");
1219 Value *&PredVal = PredValues[Pred];
1234 if (
PHINode *InPHI = dyn_cast<PHINode>(PN)) {
1237 if (
Value *Res = ExtractedVals[LoweredPHIRecord(InPHI,
Offset, Ty)]) {
1249 Res, ConstantInt::get(InVal->
getType(),
Offset),
"extract");
1258 if (
PHINode *OldInVal = dyn_cast<PHINode>(InVal))
1259 if (PHIsInspected.
count(OldInVal)) {
1261 find(PHIsToSlice, OldInVal) - PHIsToSlice.
begin();
1263 PHIUsageRecord(RefPHIId,
Offset, cast<Instruction>(Res)));
1270 << *EltPHI <<
'\n');
1271 ExtractedVals[LoweredPHIRecord(PN,
Offset, Ty)] = EltPHI;
1316 SuccForValue[
C] = Succ;
1319 if (
auto *BI = dyn_cast<BranchInst>(IDom->getTerminator())) {
1320 if (BI->isUnconditional())
1323 Cond = BI->getCondition();
1326 }
else if (
auto *SI = dyn_cast<SwitchInst>(IDom->getTerminator())) {
1327 Cond = SI->getCondition();
1328 ++SuccCount[SI->getDefaultDest()];
1329 for (
auto Case : SI->cases())
1330 AddSucc(Case.getCaseValue(), Case.getCaseSuccessor());
1340 std::optional<bool> Invert;
1342 auto *Input = cast<ConstantInt>(std::get<0>(Pair));
1348 auto It = SuccForValue.
find(Input);
1349 return It != SuccForValue.
end() && SuccCount[It->second] == 1 &&
1356 if (IsCorrectInput(Input))
1357 NeedsInvert =
false;
1364 if (Invert && *Invert != NeedsInvert)
1367 Invert = NeedsInvert;
1377 if (InsertPt != BB->
end()) {
1397 auto MatchOuterIV = [&](
Value *V1,
Value *V2) {
1401 IvNext = cast<Instruction>(V2);
1412 Value *Iv2Start, *Iv2Step;
1417 auto *BO = dyn_cast<BinaryOperator>(IvNext);
1421 if (Iv2Start != Identity)
1426 auto *
GEP = cast<GEPOperator>(IvNext);
1427 return Builder.
CreateGEP(
GEP->getSourceElementType(), Start, Iv2,
"",
1428 cast<GEPOperator>(IvNext)->getNoWrapFlags());
1431 assert(BO->isCommutative() &&
"Must be commutative");
1433 cast<Instruction>(Res)->copyIRFlags(BO);
1453 if (Inst0 && Inst1 && Inst0->getOpcode() == Inst1->getOpcode() &&
1454 Inst0->hasOneUser())
1468 if (IV0 != IV0Stripped &&
1470 return !CheckedIVs.insert(IV).second ||
1471 IV0Stripped == IV->stripPointerCasts();
1485 if (
PHINode *PU = dyn_cast<PHINode>(PHIUser)) {
1487 PotentiallyDeadPHIs.
insert(&PN);
1499 (isa<BinaryOperator>(PHIUser) || isa<UnaryOperator>(PHIUser) ||
1500 isa<GetElementPtrInst>(PHIUser)) &&
1521 auto *CmpInst = dyn_cast<ICmpInst>(U);
1525 if (U->hasOneUse() && match(U, m_c_Or(m_Specific(&PN), m_Value()))) {
1526 DropPoisonFlags.push_back(cast<Instruction>(U));
1527 CmpInst = dyn_cast<ICmpInst>(U->user_back());
1537 if (AllUsesOfPhiEndsInCmp) {
1539 bool MadeChange =
false;
1546 if (NonZeroConst != VA) {
1550 I->dropPoisonGeneratingFlags();
1571 while (InValNo != NumIncomingVals &&
1572 isa<PHINode>(PN.getIncomingValue(InValNo)))
1575 Value *NonPhiInVal =
1576 InValNo != NumIncomingVals ? PN.getIncomingValue(InValNo) :
nullptr;
1581 for (++InValNo; InValNo != NumIncomingVals; ++InValNo) {
1582 Value *OpVal = PN.getIncomingValue(InValNo);
1583 if (OpVal != NonPhiInVal && !isa<PHINode>(OpVal))
1590 if (InValNo == NumIncomingVals) {
1593 return replaceInstUsesWith(PN, NonPhiInVal);
1601 auto Res = PredOrder.try_emplace(PN.getParent());
1603 const auto &Preds = Res.first->second;
1604 for (
unsigned I = 0, E = PN.getNumIncomingValues();
I != E; ++
I) {
1608 Value *VA = PN.getIncomingValue(
I);
1609 unsigned J = PN.getBasicBlockIndex(BBB);
1610 Value *
VB = PN.getIncomingValue(J);
1611 PN.setIncomingBlock(
I, BBB);
1612 PN.setIncomingValue(
I, VB);
1613 PN.setIncomingBlock(J, BBA);
1614 PN.setIncomingValue(J, VA);
1629 if (&IdenticalPN == &PN)
1634 if (!PN.isIdenticalToWhenDefined(&IdenticalPN))
1638 return replaceInstUsesWith(PN, &IdenticalPN);
1645 if (PN.getType()->isIntegerTy() &&
1646 !
DL.isLegalInteger(PN.getType()->getPrimitiveSizeInBits()))
1647 if (
Instruction *Res = SliceUpIllegalIntegerPHI(PN))
1652 return replaceInstUsesWith(PN, V);
1655 return replaceInstUsesWith(PN, Res);
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
This file provides internal interfaces used to implement the InstCombine.
static ConstantInt * getAnyNonZeroConstInt(PHINode &PN)
Return an existing non-zero constant if this phi node has one, otherwise return constant 1.
static Value * foldDependentIVs(PHINode &PN, IRBuilderBase &Builder)
static bool isDeadPHICycle(PHINode *PN, SmallPtrSetImpl< PHINode * > &PotentiallyDeadPHIs)
Return true if this PHI node is only used by a PHI node cycle that is dead.
static bool isSafeAndProfitableToSinkLoad(LoadInst *L)
Return true if we know that it is safe to sink the load out of the block that defines it.
static Value * simplifyUsingControlFlow(InstCombiner &Self, PHINode &PN, const DominatorTree &DT)
static bool PHIsEqualValue(PHINode *PN, Value *&NonPhiInVal, SmallPtrSetImpl< PHINode * > &ValueEqualPHIs)
Return true if this phi node is always equal to NonPhiInVal.
static cl::opt< unsigned > MaxNumPhis("instcombine-max-num-phis", cl::init(512), cl::desc("Maximum number phis to handle in intptr/ptrint folding"))
This file provides the interface for the instcombine pass implementation.
uint64_t IntrinsicInst * II
const SmallVectorImpl< MachineOperand > & Cond
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
This file defines the SmallPtrSet 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]
an instruction to allocate memory on the stack
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
LLVM Basic Block Representation.
const_iterator getFirstInsertionPt() const
Returns an iterator to the first instruction in this block that is suitable for inserting a non-PHI i...
const Function * getParent() const
Return the enclosing method, or null if none.
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...
BinaryOps getOpcode() const
static BinaryOperator * Create(BinaryOps Op, Value *S1, Value *S2, const Twine &Name=Twine(), InsertPosition InsertBefore=nullptr)
Construct a binary instruction, given the opcode and the two operands.
This is the base class for all instructions that perform data casts.
static CastInst * CreatePointerCast(Value *S, Type *Ty, const Twine &Name="", InsertPosition InsertBefore=nullptr)
Create a BitCast, AddrSpaceCast or a PtrToInt cast instruction.
static CastInst * CreateBitOrPointerCast(Value *S, Type *Ty, const Twine &Name="", InsertPosition InsertBefore=nullptr)
Create a BitCast, a PtrToInt, or an IntToPTr cast instruction.
static CastInst * CreateZExtOrBitCast(Value *S, Type *Ty, const Twine &Name="", InsertPosition InsertBefore=nullptr)
Create a ZExt or BitCast cast instruction.
static CastInst * Create(Instruction::CastOps, Value *S, Type *Ty, const Twine &Name="", InsertPosition InsertBefore=nullptr)
Provides a way to construct any of the CastInst subclasses using an opcode instead of the subclass's ...
This class is the base class for the comparison instructions.
static bool isEquality(Predicate pred)
Determine if this is an equals/not equals predicate.
static CmpInst * Create(OtherOps Op, Predicate Pred, Value *S1, Value *S2, const Twine &Name="", InsertPosition InsertBefore=nullptr)
Construct a compare instruction, given the opcode, the predicate and the two operands.
Predicate getPredicate() const
Return the predicate for this instruction.
OtherOps getOpcode() const
Get the opcode casted to the right type.
static Constant * getNot(Constant *C)
static Constant * getBinOpIdentity(unsigned Opcode, Type *Ty, bool AllowRHSConstant=false, bool NSZ=false)
Return the identity constant for a binary opcode.
This is the shared class of boolean and integer constants.
static ConstantInt * getTrue(LLVMContext &Context)
static ConstantInt * getFalse(LLVMContext &Context)
This is an important base class in LLVM.
static Constant * getNullValue(Type *Ty)
Constructor to create a '0' constant of arbitrary type.
This class represents an Operation in the Expression.
unsigned getPointerSizeInBits(unsigned AS=0) const
Layout pointer size, in bits FIXME: The defaults need to be removed once all of the backends/clients ...
TypeSize getTypeSizeInBits(Type *Ty) const
Size examples:
iterator find(const_arg_type_t< KeyT > Val)
DomTreeNodeBase * getIDom() const
DomTreeNodeBase< NodeT > * getNode(const NodeT *BB) const
getNode - return the (Post)DominatorTree node for the specified basic block.
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree.
bool isReachableFromEntry(const Use &U) const
Provide an overload for a Use.
bool dominates(const BasicBlock *BB, const Use &U) const
Return true if the (end of the) basic block BB dominates the use U.
Represents flags for the getelementptr instruction/expression.
static GEPNoWrapFlags all()
an instruction for type-safe pointer arithmetic to access elements of arrays and structs
static GetElementPtrInst * Create(Type *PointeeType, Value *Ptr, ArrayRef< Value * > IdxList, const Twine &NameStr="", InsertPosition InsertBefore=nullptr)
Type * getSourceElementType() const
Common base class shared among various IRBuilders.
Value * CreateLShr(Value *LHS, Value *RHS, const Twine &Name="", bool isExact=false)
Value * CreateGEP(Type *Ty, Value *Ptr, ArrayRef< Value * > IdxList, const Twine &Name="", GEPNoWrapFlags NW=GEPNoWrapFlags::none())
Value * CreateNot(Value *V, const Twine &Name="")
Value * CreateTrunc(Value *V, Type *DestTy, const Twine &Name="", bool IsNUW=false, bool IsNSW=false)
Value * CreateBinOp(Instruction::BinaryOps Opc, Value *LHS, Value *RHS, const Twine &Name="", MDNode *FPMathTag=nullptr)
void SetInsertPoint(BasicBlock *TheBB)
This specifies that created instructions should be appended to the end of the specified block.
static InsertValueInst * Create(Value *Agg, Value *Val, ArrayRef< unsigned > Idxs, const Twine &NameStr="", InsertPosition InsertBefore=nullptr)
Instruction * foldPHIArgInsertValueInstructionIntoPHI(PHINode &PN)
If we have something like phi [insertvalue(a,b,0), insertvalue(c,d,0)], turn this into a phi[a,...
Instruction * foldPHIArgBinOpIntoPHI(PHINode &PN)
If we have something like phi [add (a,b), add(a,c)] and if a/b/c and the adds all have a single user,...
Constant * getLosslessUnsignedTrunc(Constant *C, Type *TruncTy)
Instruction * eraseInstFromFunction(Instruction &I) override
Combiner aware instruction erasure.
Instruction * visitPHINode(PHINode &PN)
Instruction * foldPHIArgOpIntoPHI(PHINode &PN)
Try to rotate an operation below a PHI node, using PHI nodes for its operands.
Instruction * foldPHIArgZextsIntoPHI(PHINode &PN)
TODO: This function could handle other cast types, but then it might require special-casing a cast fr...
Instruction * foldPHIArgLoadIntoPHI(PHINode &PN)
bool foldIntegerTypedPHI(PHINode &PN)
If an integer typed PHI has only one use which is an IntToPtr operation, replace the PHI with an exis...
Instruction * foldPHIArgIntToPtrToPHI(PHINode &PN)
Instruction * SliceUpIllegalIntegerPHI(PHINode &PN)
This is an integer PHI and we know that it has an illegal type: see if it is only used by trunc or tr...
Instruction * foldPHIArgGEPIntoPHI(PHINode &PN)
void PHIArgMergedDebugLoc(Instruction *Inst, PHINode &PN)
Helper function for FoldPHIArgXIntoPHI() to set debug location for the folded operation.
Instruction * foldPHIArgExtractValueInstructionIntoPHI(PHINode &PN)
If we have something like phi [extractvalue(a,0), extractvalue(b,0)], turn this into a phi[a,...
The core instruction combiner logic.
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.
Instruction * replaceOperand(Instruction &I, unsigned OpNum, Value *V)
Replace operand of instruction and add old operand to the worklist.
const SimplifyQuery & getSimplifyQuery() const
void copyIRFlags(const Value *V, bool IncludeWrapFlags=true)
Convenience method to copy supported exact, fast-math, and (optionally) wrapping flags from V to this...
const DebugLoc & getDebugLoc() const
Return the debug location for this node as a DebugLoc.
void andIRFlags(const Value *V)
Logical 'and' of any supported wrapping, exact, and fast-math flags of V and this instruction.
bool isAtomic() const LLVM_READONLY
Return true if this instruction has an AtomicOrdering of unordered or higher.
Instruction * user_back()
Specialize the methods defined in Value, as we know that an instruction can only be used by other ins...
MDNode * getMetadata(unsigned KindID) const
Get the metadata of given kind attached to this Instruction.
void setMetadata(unsigned KindID, MDNode *Node)
Set the metadata of the specified kind to the specified node.
unsigned getOpcode() const
Returns a member of one of the enums like Instruction::Add.
void applyMergedLocation(DILocation *LocA, DILocation *LocB)
Merge 2 debug locations and apply it to the Instruction.
void setDebugLoc(DebugLoc Loc)
Set the debug location information for this instruction.
This is an important class for using LLVM in a threaded context.
An instruction for reading from memory.
unsigned getPointerAddressSpace() const
Returns the address space of the pointer operand.
bool isVolatile() const
Return true if this is a load from a volatile memory location.
Align getAlign() const
Return the alignment of the access that is being performed.
void addIncoming(Value *V, BasicBlock *BB)
Add an incoming value to the end of the PHI list.
iterator_range< const_block_iterator > blocks() const
op_range incoming_values()
BasicBlock * getIncomingBlock(unsigned i) const
Return incoming basic block number i.
Value * getIncomingValue(unsigned i) const
Return incoming value number x.
unsigned getNumIncomingValues() const
Return the number of incoming edges.
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...
static PoisonValue * get(Type *T)
Static factory methods - Return an 'poison' object of the specified type.
A templated base class for SmallPtrSet which provides the typesafe interface that is common across al...
size_type count(ConstPtrType Ptr) const
count - Return 1 if the specified pointer is in the set, 0 otherwise.
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.
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.
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
The instances of the Type class are immutable: once they are created, they are never changed.
bool isPointerTy() const
True if this is an instance of PointerType.
unsigned getScalarSizeInBits() const LLVM_READONLY
If this is a vector type, return the getPrimitiveSizeInBits value for the element type.
bool isIntegerTy() const
True if this is an instance of IntegerType.
TypeSize getPrimitiveSizeInBits() const LLVM_READONLY
Return the basic size of this type if it is a primitive type.
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.
bool hasOneUse() const
Return true if there is exactly one use of this value.
iterator_range< user_iterator > users()
bool hasNUsesOrMore(unsigned N) const
Return true if this value has N uses or more.
const Value * stripPointerCasts() const
Strip off pointer casts, all-zero GEPs and address space casts.
bool isSwiftError() const
Return true if this value is a swifterror value.
LLVMContext & getContext() const
All values hold a context through their type.
StringRef getName() const
Return a constant reference to the value's name.
const ParentTy * getParent() const
self_iterator getIterator()
@ C
The default llvm calling convention, compatible with C.
class_match< BinaryOperator > m_BinOp()
Match an arbitrary binary operation and ignore it.
bool match(Val *V, const Pattern &P)
specificval_ty m_Specific(const Value *V)
Match if we have a specific specified value.
auto m_GEP(const OperandTypes &...Ops)
Matches GetElementPtrInst.
AnyBinaryOp_match< LHS, RHS, true > m_c_BinOp(const LHS &L, const RHS &R)
Matches a BinaryOperator with LHS and RHS in either order.
is_zero m_Zero()
Match any null constant or a vector with all elements equal to 0.
initializer< Ty > init(const Ty &Val)
NodeAddr< PhiNode * > Phi
This is an optimization pass for GlobalISel generic memory operations.
auto drop_begin(T &&RangeOrContainer, size_t N=1)
Return a range covering RangeOrContainer with the first N elements excluded.
detail::zippy< detail::zip_shortest, T, U, Args... > zip(T &&t, U &&u, Args &&...args)
zip iterator for two or more iteratable types.
bool operator<(int64_t V1, const APSInt &V2)
auto find(R &&Range, const T &Val)
Provide wrappers to std::find which take ranges instead of having to pass begin/end explicitly.
bool all_of(R &&range, UnaryPredicate P)
Provide wrappers to std::all_of which take ranges instead of having to pass begin/end explicitly.
void append_range(Container &C, Range &&R)
Wrapper function to append range R to container C.
Value * simplifyInstruction(Instruction *I, const SimplifyQuery &Q)
See if we can compute a simplified version of this instruction.
bool matchSimpleRecurrence(const PHINode *P, BinaryOperator *&BO, Value *&Start, Value *&Step)
Attempt to match a simple first order recurrence cycle of the form: iv = phi Ty [Start,...
bool any_of(R &&range, UnaryPredicate P)
Provide wrappers to std::any_of which take ranges instead of having to pass begin/end explicitly.
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
void combineMetadata(Instruction *K, const Instruction *J, ArrayRef< unsigned > KnownIDs, bool DoesKMove)
Combine the metadata of two instructions so that K can replace J.
bool isKnownNonZero(const Value *V, const SimplifyQuery &Q, unsigned Depth=0)
Return true if the given value is known to be non-zero when defined.
DWARFExpression::Operation Op
void array_pod_sort(IteratorTy Start, IteratorTy End)
array_pod_sort - This sorts an array with the specified start and end extent.
This struct is a compact representation of a valid (non-zero power of two) alignment.
static bool isEqual(const LoweredPHIRecord &LHS, const LoweredPHIRecord &RHS)
static unsigned getHashValue(const LoweredPHIRecord &Val)
static LoweredPHIRecord getEmptyKey()
static LoweredPHIRecord getTombstoneKey()
An information struct used to provide DenseMap with the various necessary components for a given valu...
Incoming for lane maks phi as machine instruction, incoming register Reg and incoming block Block are...
SimplifyQuery getWithInstruction(const Instruction *I) const