108#define DEBUG_TYPE "instcombine"
116 "Number of instruction combining iterations performed");
117STATISTIC(NumOneIteration,
"Number of functions with one iteration");
118STATISTIC(NumTwoIterations,
"Number of functions with two iterations");
119STATISTIC(NumThreeIterations,
"Number of functions with three iterations");
121 "Number of functions with four or more iterations");
125STATISTIC(NumDeadInst ,
"Number of dead inst eliminated");
131 "Controls which instructions are visited");
145 "instcombine-max-sink-users",
cl::init(32),
146 cl::desc(
"Maximum number of undroppable users for instruction sinking"));
149 "instcombine-infinite-loop-threshold",
150 cl::desc(
"Number of instruction combining iterations considered an "
156 cl::desc(
"Maximum array size considered when doing a combine"));
168std::optional<Instruction *>
179 bool &KnownBitsComputed) {
196 *
this, II, DemandedElts, UndefElts, UndefElts2, UndefElts3,
215bool InstCombinerImpl::isDesirableIntType(
unsigned BitWidth)
const {
234bool InstCombinerImpl::shouldChangeType(
unsigned FromWidth,
235 unsigned ToWidth)
const {
241 if (ToWidth < FromWidth && isDesirableIntType(ToWidth))
246 if ((FromLegal || isDesirableIntType(FromWidth)) && !ToLegal)
251 if (!FromLegal && !ToLegal && ToWidth > FromWidth)
262bool InstCombinerImpl::shouldChangeType(
Type *
From,
Type *To)
const {
268 unsigned FromWidth =
From->getPrimitiveSizeInBits();
270 return shouldChangeType(FromWidth, ToWidth);
279 auto *OBO = dyn_cast<OverflowingBinaryOperator>(&
I);
280 if (!OBO || !OBO->hasNoSignedWrap())
285 if (Opcode != Instruction::Add && Opcode != Instruction::Sub)
288 const APInt *BVal, *CVal;
292 bool Overflow =
false;
293 if (Opcode == Instruction::Add)
294 (void)BVal->
sadd_ov(*CVal, Overflow);
296 (
void)BVal->
ssub_ov(*CVal, Overflow);
302 auto *OBO = dyn_cast<OverflowingBinaryOperator>(&
I);
303 return OBO && OBO->hasNoUnsignedWrap();
307 auto *OBO = dyn_cast<OverflowingBinaryOperator>(&
I);
308 return OBO && OBO->hasNoSignedWrap();
317 I.clearSubclassOptionalData();
322 I.clearSubclassOptionalData();
323 I.setFastMathFlags(FMF);
332 auto *Cast = dyn_cast<CastInst>(BinOp1->
getOperand(0));
333 if (!Cast || !Cast->hasOneUse())
337 auto CastOpcode = Cast->getOpcode();
338 if (CastOpcode != Instruction::ZExt)
346 auto *BinOp2 = dyn_cast<BinaryOperator>(Cast->getOperand(0));
347 if (!BinOp2 || !BinOp2->hasOneUse() || BinOp2->getOpcode() != AssocOpcode)
371Value *InstCombinerImpl::simplifyIntToPtrRoundTripCast(
Value *Val) {
372 auto *IntToPtr = dyn_cast<IntToPtrInst>(Val);
375 auto *PtrToInt = dyn_cast<PtrToIntInst>(IntToPtr->getOperand(0));
376 Type *CastTy = IntToPtr->getDestTy();
379 PtrToInt->getSrcTy()->getPointerAddressSpace() &&
382 return PtrToInt->getOperand(0);
409 bool Changed =
false;
417 Changed = !
I.swapOperands();
422 if (
I.isAssociative()) {
445 I.setHasNoUnsignedWrap(
true);
448 I.setHasNoSignedWrap(
true);
477 if (
I.isAssociative() &&
I.isCommutative()) {
540 if (isa<FPMathOperator>(NewBO)) {
554 I.setHasNoUnsignedWrap(
true);
572 if (LOp == Instruction::And)
573 return ROp == Instruction::Or || ROp == Instruction::Xor;
576 if (LOp == Instruction::Or)
577 return ROp == Instruction::And;
581 if (LOp == Instruction::Mul)
582 return ROp == Instruction::Add || ROp == Instruction::Sub;
605 if (isa<Constant>(V))
619 assert(Op &&
"Expected a binary operator");
620 LHS = Op->getOperand(0);
621 RHS = Op->getOperand(1);
622 if (TopOpcode == Instruction::Add || TopOpcode == Instruction::Sub) {
627 return Instruction::Mul;
631 return Op->getOpcode();
640 assert(
A &&
B &&
C &&
D &&
"All values must be provided");
643 Value *RetVal =
nullptr;
654 if (
A ==
C || (InnerCommutative &&
A ==
D)) {
666 RetVal =
Builder.CreateBinOp(InnerOpcode,
A, V);
674 if (
B ==
D || (InnerCommutative &&
B ==
C)) {
686 RetVal =
Builder.CreateBinOp(InnerOpcode, V,
B);
697 if (isa<OverflowingBinaryOperator>(RetVal)) {
700 if (isa<OverflowingBinaryOperator>(&
I)) {
701 HasNSW =
I.hasNoSignedWrap();
702 HasNUW =
I.hasNoUnsignedWrap();
704 if (
auto *LOBO = dyn_cast<OverflowingBinaryOperator>(
LHS)) {
705 HasNSW &= LOBO->hasNoSignedWrap();
706 HasNUW &= LOBO->hasNoUnsignedWrap();
709 if (
auto *ROBO = dyn_cast<OverflowingBinaryOperator>(
RHS)) {
710 HasNSW &= ROBO->hasNoSignedWrap();
711 HasNUW &= ROBO->hasNoUnsignedWrap();
714 if (TopLevelOpcode == Instruction::Add && InnerOpcode == Instruction::Mul) {
724 cast<Instruction>(RetVal)->setHasNoSignedWrap(HasNSW);
727 cast<Instruction>(RetVal)->setHasNoUnsignedWrap(HasNUW);
748 if (Op0 && Op1 && LHSOpcode == RHSOpcode)
874 if (!LHSIsSelect && !RHSIsSelect)
879 if (isa<FPMathOperator>(&
I)) {
880 FMF =
I.getFastMathFlags();
887 Value *
Cond, *True =
nullptr, *False =
nullptr;
895 if (Opcode != Instruction::Add || (!True && !False) || (True && False))
910 if (LHSIsSelect && RHSIsSelect &&
A ==
D) {
919 else if (True && !False)
948void InstCombinerImpl::freelyInvertAllUsersOf(
Value *
I,
Value *IgnoredUser) {
950 if (U == IgnoredUser)
952 switch (cast<Instruction>(U)->
getOpcode()) {
953 case Instruction::Select: {
954 auto *
SI = cast<SelectInst>(U);
956 SI->swapProfMetadata();
959 case Instruction::Br:
960 cast<BranchInst>(U)->swapSuccessors();
962 case Instruction::Xor:
967 "canFreelyInvertAllUsersOf() ?");
974Value *InstCombinerImpl::dyn_castNegVal(
Value *V)
const {
984 if (
C->getType()->getElementType()->isIntegerTy())
988 for (
unsigned i = 0, e = CV->getNumOperands(); i != e; ++i) {
993 if (isa<UndefValue>(Elt))
996 if (!isa<ConstantInt>(Elt))
1003 if (
auto *CV = dyn_cast<Constant>(V))
1004 if (CV->getType()->isVectorTy() &&
1005 CV->getType()->getScalarType()->isIntegerTy() && CV->getSplatValue())
1023 !
X->getType()->isIntOrIntVectorTy(1))
1038 for (
Value *Op :
I.operands()) {
1042 C = dyn_cast<Constant>(IsTrueArm ?
SI->getTrueValue()
1043 :
SI->getFalseValue());
1044 }
else if (
match(
SI->getCondition(),
1050 C = dyn_cast<Constant>(Op);
1070 bool FoldWithMultiUse) {
1072 if (!
SI->hasOneUse() && !FoldWithMultiUse)
1075 Value *TV =
SI->getTrueValue();
1076 Value *FV =
SI->getFalseValue();
1077 if (!(isa<Constant>(TV) || isa<Constant>(FV)))
1081 if (
SI->getType()->isIntOrIntVectorTy(1))
1086 if (
auto *BC = dyn_cast<BitCastInst>(&Op)) {
1087 VectorType *DestTy = dyn_cast<VectorType>(BC->getDestTy());
1088 VectorType *SrcTy = dyn_cast<VectorType>(BC->getSrcTy());
1091 if ((SrcTy ==
nullptr) != (DestTy ==
nullptr))
1102 if (!NewTV && !NewFV)
1115 if (NumPHIValues == 0)
1125 if (UI != &
I && !
I.isIdenticalTo(UI))
1136 Value *NonSimplifiedInVal =
nullptr;
1137 for (
unsigned i = 0; i != NumPHIValues; ++i) {
1145 for (
Value *Op :
I.operands()) {
1163 if (isa<PHINode>(InVal))
return nullptr;
1164 if (NonSimplifiedBB)
return nullptr;
1166 NonSimplifiedBB = InBB;
1167 NonSimplifiedInVal = InVal;
1172 if (isa<InvokeInst>(InVal))
1173 if (cast<Instruction>(InVal)->
getParent() == NonSimplifiedBB)
1190 if (NonSimplifiedBB !=
nullptr) {
1206 if (NonSimplifiedBB) {
1210 U = NonSimplifiedInVal;
1212 U = U->DoPHITranslation(PN->
getParent(), NonSimplifiedBB);
1217 for (
unsigned i = 0; i != NumPHIValues; ++i) {
1218 if (NewPhiValues[i])
1226 if (
User == &
I)
continue;
1232 const_cast<PHINode &
>(*NewPN),
1241 auto *Phi0 = dyn_cast<PHINode>(BO.
getOperand(0));
1242 auto *Phi1 = dyn_cast<PHINode>(BO.
getOperand(1));
1243 if (!Phi0 || !Phi1 || !Phi0->hasOneUse() || !Phi1->hasOneUse() ||
1244 Phi0->getNumOperands() != Phi1->getNumOperands())
1265 auto CanFoldIncomingValuePair = [&](std::tuple<Use &, Use &>
T) {
1266 auto &Phi0Use = std::get<0>(
T);
1267 auto &Phi1Use = std::get<1>(
T);
1268 if (Phi0->getIncomingBlock(Phi0Use) != Phi1->getIncomingBlock(Phi1Use))
1270 Value *Phi0UseV = Phi0Use.get();
1271 Value *Phi1UseV = Phi1Use.get();
1274 else if (Phi1UseV ==
C)
1281 if (
all_of(
zip(Phi0->operands(), Phi1->operands()),
1282 CanFoldIncomingValuePair)) {
1285 assert(NewIncomingValues.
size() == Phi0->getNumOperands() &&
1286 "The number of collected incoming values should equal the number "
1287 "of the original PHINode operands!");
1288 for (
unsigned I = 0;
I < Phi0->getNumOperands();
I++)
1289 NewPhi->
addIncoming(NewIncomingValues[
I], Phi0->getIncomingBlock(
I));
1294 if (Phi0->getNumOperands() != 2 || Phi1->getNumOperands() != 2)
1301 ConstBB = Phi0->getIncomingBlock(0);
1302 OtherBB = Phi0->getIncomingBlock(1);
1304 ConstBB = Phi0->getIncomingBlock(1);
1305 OtherBB = Phi0->getIncomingBlock(0);
1315 auto *PredBlockBranch = dyn_cast<BranchInst>(OtherBB->
getTerminator());
1316 if (!PredBlockBranch || PredBlockBranch->isConditional() ||
1323 for (
auto BBIter = BO.
getParent()->
begin(); &*BBIter != &BO; ++BBIter)
1336 Phi0->getIncomingValueForBlock(OtherBB),
1337 Phi1->getIncomingValueForBlock(OtherBB));
1338 if (
auto *NotFoldedNewBO = dyn_cast<BinaryOperator>(NewBO))
1339 NotFoldedNewBO->copyIRFlags(&BO);
1349 if (!isa<Constant>(
I.getOperand(1)))
1352 if (
auto *Sel = dyn_cast<SelectInst>(
I.getOperand(0))) {
1355 }
else if (
auto *PN = dyn_cast<PHINode>(
I.getOperand(0))) {
1366 if (
GEP.hasAllZeroIndices() && !Src.hasAllZeroIndices() &&
1373 if (!isa<VectorType>(Inst.
getType()))
1379 cast<VectorType>(Inst.
getType())->getElementCount());
1381 cast<VectorType>(Inst.
getType())->getElementCount());
1386 Value *L0, *L1, *R0, *R1;
1391 cast<ShuffleVectorInst>(
LHS)->isConcat() &&
1392 cast<ShuffleVectorInst>(
RHS)->isConcat()) {
1399 if (
auto *BO = dyn_cast<BinaryOperator>(NewBO0))
1402 if (
auto *BO = dyn_cast<BinaryOperator>(NewBO1))
1409 if (
auto *BO = dyn_cast<BinaryOperator>(V))
1413 M, Intrinsic::experimental_vector_reverse, V->getType());
1426 return createBinOpReverse(V1, V2);
1430 return createBinOpReverse(V1,
RHS);
1434 return createBinOpReverse(
LHS, V2);
1444 if (
auto *BO = dyn_cast<BinaryOperator>(XY))
1453 V1->
getType() == V2->getType() &&
1456 return createBinOpShuffle(V1, V2, Mask);
1465 auto *LShuf = cast<ShuffleVectorInst>(
LHS);
1466 auto *RShuf = cast<ShuffleVectorInst>(
RHS);
1471 if (LShuf->isSelect() &&
1473 RShuf->isSelect() &&
1491 auto *InstVTy = dyn_cast<FixedVectorType>(Inst.
getType());
1496 cast<FixedVectorType>(V1->
getType())->getNumElements() <=
1497 InstVTy->getNumElements()) {
1499 "Shuffle should not change scalar type");
1506 bool ConstOp1 = isa<Constant>(
RHS);
1508 unsigned SrcVecNumElts =
1509 cast<FixedVectorType>(V1->
getType())->getNumElements();
1512 bool MayChange =
true;
1513 unsigned NumElts = InstVTy->getNumElements();
1514 for (
unsigned I = 0;
I < NumElts; ++
I) {
1516 if (ShMask[
I] >= 0) {
1517 assert(ShMask[
I] < (
int)NumElts &&
"Not expecting narrowing shuffle");
1525 if (!CElt || (!isa<UndefValue>(NewCElt) && NewCElt != CElt) ||
1526 I >= SrcVecNumElts) {
1530 NewVecC[ShMask[
I]] = CElt;
1541 if (
I >= SrcVecNumElts || ShMask[
I] < 0) {
1562 Value *NewLHS = ConstOp1 ? V1 : NewC;
1563 Value *NewRHS = ConstOp1 ? NewC : V1;
1564 return createBinOpShuffle(NewLHS, NewRHS, Mask);
1571 if (isa<ShuffleVectorInst>(
RHS))
1604 if (isa<FPMathOperator>(R)) {
1605 R->copyFastMathFlags(&Inst);
1608 if (
auto *NewInstBO = dyn_cast<BinaryOperator>(NewBO))
1609 NewInstBO->copyIRFlags(R);
1638 cast<Operator>(Op1)->getOpcode() == CastOpc &&
1639 (Op0->
hasOneUse() || Op1->hasOneUse()))) {
1657 if (!willNotOverflow(BO.
getOpcode(),
X,
Y, BO, IsSext))
1663 if (
auto *NewBinOp = dyn_cast<BinaryOperator>(NarrowBO)) {
1665 NewBinOp->setHasNoSignedWrap();
1667 NewBinOp->setHasNoUnsignedWrap();
1685 if (!
GEP.hasAllConstantIndices())
1700 bool IsInBounds =
GEP.isInBounds();
1701 Type *Ty =
GEP.getSourceElementType();
1702 Value *NewTrueC =
Builder.CreateGEP(Ty, TrueC, IndexC,
"", IsInBounds);
1703 Value *NewFalseC =
Builder.CreateGEP(Ty, FalseC, IndexC,
"", IsInBounds);
1716 Type *PtrTy = Src->getType()->getScalarType();
1717 if (
GEP.hasAllConstantIndices() &&
1718 (Src->hasOneUse() || Src->hasAllConstantIndices())) {
1722 bool IsFirstType =
true;
1723 unsigned NumVarIndices = 0;
1724 for (
auto Pair :
enumerate(Src->indices())) {
1725 if (!isa<ConstantInt>(Pair.value())) {
1727 IsFirstType =
false;
1728 NumVarIndices = Pair.index() + 1;
1735 if (NumVarIndices != Src->getNumIndices()) {
1737 if (isa<ScalableVectorType>(
BaseType))
1756 if (!
Offset.isZero() || (!IsFirstType && !ConstIndices[0].isZero())) {
1759 if (Src->hasAllConstantIndices())
1771 Src->getNumIndices() - NumVarIndices));
1778 IsInBounds &=
Idx.isNonNegative() == ConstIndices[0].isNonNegative();
1783 Indices,
"", IsInBounds));
1786 if (Src->getResultElementType() !=
GEP.getSourceElementType())
1792 bool EndsWithSequential =
false;
1795 EndsWithSequential =
I.isSequential();
1798 if (EndsWithSequential) {
1801 Value *SO1 = Src->getOperand(Src->getNumOperands()-1);
1819 if (Src->getNumOperands() == 2) {
1825 Indices.
append(Src->op_begin()+1, Src->op_end()-1);
1828 }
else if (isa<Constant>(*
GEP.idx_begin()) &&
1829 cast<Constant>(*
GEP.idx_begin())->isNullValue() &&
1830 Src->getNumOperands() != 1) {
1832 Indices.
append(Src->op_begin()+1, Src->op_end());
1836 if (!Indices.
empty())
1839 Src->getSourceElementType(), Src->getOperand(0), Indices,
"",
1848 Type *GEPType =
GEP.getType();
1849 Type *GEPEltType =
GEP.getSourceElementType();
1850 bool IsGEPSrcEleScalable = isa<ScalableVectorType>(GEPEltType);
1858 if (
auto *GEPFVTy = dyn_cast<FixedVectorType>(GEPType)) {
1859 auto VWidth = GEPFVTy->getNumElements();
1860 APInt UndefElts(VWidth, 0);
1876 bool MadeChange =
false;
1880 Type *NewScalarIndexTy =
1890 Type *IndexTy = (*I)->getType();
1891 Type *NewIndexType =
1894 cast<VectorType>(IndexTy)->getElementCount())
1906 if (IndexTy != NewIndexType) {
1918 if (
auto *PN = dyn_cast<PHINode>(PtrOp)) {
1919 auto *Op1 = dyn_cast<GetElementPtrInst>(PN->getOperand(0));
1934 for (
auto I = PN->op_begin()+1,
E = PN->op_end();
I !=
E; ++
I) {
1935 auto *Op2 = dyn_cast<GetElementPtrInst>(*
I);
1936 if (!Op2 || Op1->getNumOperands() != Op2->getNumOperands() ||
1937 Op1->getSourceElementType() != Op2->getSourceElementType())
1945 Type *CurTy =
nullptr;
1947 for (
unsigned J = 0,
F = Op1->getNumOperands(); J !=
F; ++J) {
1948 if (Op1->getOperand(J)->getType() != Op2->getOperand(J)->getType())
1951 if (Op1->getOperand(J) != Op2->getOperand(J)) {
1960 assert(CurTy &&
"No current type?");
1980 CurTy = Op1->getSourceElementType();
1992 if (DI != -1 && !PN->hasOneUse())
1995 auto *NewGEP = cast<GetElementPtrInst>(Op1->clone());
2008 PN->getNumOperands());
2011 for (
auto &
I : PN->operands())
2012 NewPN->
addIncoming(cast<GEPOperator>(
I)->getOperand(DI),
2013 PN->getIncomingBlock(
I));
2015 NewGEP->setOperand(DI, NewPN);
2018 NewGEP->insertInto(
GEP.getParent(),
GEP.getParent()->getFirstInsertionPt());
2022 if (
auto *Src = dyn_cast<GEPOperator>(PtrOp))
2028 if (
GEP.getNumIndices() == 1 && !IsGEPSrcEleScalable) {
2029 unsigned AS =
GEP.getPointerAddressSpace();
2030 if (
GEP.getOperand(1)->getType()->getScalarSizeInBits() ==
2034 bool Matched =
false;
2037 if (TyAllocSize == 1) {
2038 V =
GEP.getOperand(1);
2040 }
else if (
match(
GEP.getOperand(1),
2042 if (TyAllocSize == 1ULL <<
C)
2044 }
else if (
match(
GEP.getOperand(1),
2046 if (TyAllocSize ==
C)
2066 if (!
GEP.isInBounds()) {
2069 APInt BasePtrOffset(IdxWidth, 0);
2070 Value *UnderlyingPtrOp =
2073 if (
auto *AI = dyn_cast<AllocaInst>(UnderlyingPtrOp)) {
2074 if (
GEP.accumulateConstantOffset(
DL, BasePtrOffset) &&
2079 if (BasePtrOffset.
ule(AllocSize)) {
2081 GEP.getSourceElementType(), PtrOp, Indices,
GEP.getName());
2095 if (isa<ConstantPointerNull>(V))
2097 if (
auto *LI = dyn_cast<LoadInst>(V))
2098 return isa<GlobalVariable>(LI->getPointerOperand());
2122 return Dest && Dest->Ptr == UsedV;
2136 switch (
I->getOpcode()) {
2141 case Instruction::AddrSpaceCast:
2142 case Instruction::BitCast:
2143 case Instruction::GetElementPtr:
2148 case Instruction::ICmp: {
2155 unsigned OtherIndex = (ICI->
getOperand(0) == PI) ? 1 : 0;
2162 case Instruction::Call:
2169 case Intrinsic::memmove:
2170 case Intrinsic::memcpy:
2171 case Intrinsic::memset: {
2173 if (
MI->isVolatile() ||
MI->getRawDest() != PI)
2177 case Intrinsic::assume:
2178 case Intrinsic::invariant_start:
2179 case Intrinsic::invariant_end:
2180 case Intrinsic::lifetime_start:
2181 case Intrinsic::lifetime_end:
2182 case Intrinsic::objectsize:
2185 case Intrinsic::launder_invariant_group:
2186 case Intrinsic::strip_invariant_group:
2215 case Instruction::Store: {
2217 if (
SI->isVolatile() ||
SI->getPointerOperand() != PI)
2225 }
while (!Worklist.
empty());
2247 std::unique_ptr<DIBuilder> DIB;
2248 if (isa<AllocaInst>(
MI)) {
2254 for (
unsigned i = 0, e =
Users.size(); i != e; ++i) {
2272 for (
unsigned i = 0, e =
Users.size(); i != e; ++i) {
2281 C->isFalseWhenEqual()));
2282 }
else if (
auto *
SI = dyn_cast<StoreInst>(
I)) {
2283 for (
auto *DVI : DVIs)
2284 if (DVI->isAddressOfVariable())
2324 for (
auto *DVI : DVIs)
2325 if (DVI->isAddressOfVariable() || DVI->getExpression()->startsWithDeref())
2326 DVI->eraseFromParent();
2372 if (FreeInstrBB->
size() != 2) {
2374 if (&Inst == &FI || &Inst == FreeInstrBBTerminator)
2376 auto *Cast = dyn_cast<CastInst>(&Inst);
2377 if (!Cast || !Cast->isNoopCast(
DL))
2398 "Broken CFG: missing edge from predecessor to successor");
2403 if (&Instr == FreeInstrBBTerminator)
2405 Instr.moveBefore(TI);
2408 "Only the branch instruction should remain");
2419 Attrs = Attrs.removeParamAttribute(FI.
getContext(), 0, Attribute::NonNull);
2420 Attribute Dereferenceable = Attrs.getParamAttr(0, Attribute::Dereferenceable);
2421 if (Dereferenceable.
isValid()) {
2423 Attrs = Attrs.removeParamAttribute(FI.
getContext(), 0,
2424 Attribute::Dereferenceable);
2425 Attrs = Attrs.addDereferenceableOrNullParamAttr(FI.
getContext(), 0, Bytes);
2434 if (isa<UndefValue>(Op)) {
2442 if (isa<ConstantPointerNull>(Op))
2447 CallInst *CI = dyn_cast<CallInst>(Op);
2482 while (
Instruction *Prev =
I.getPrevNonDebugInstruction()) {
2487 if (Prev->isEHPad())
2500 assert(
I.getParent()->sizeWithoutDebug() == 1 &&
"The block is now empty.");
2514 return BBI->isDebugOrPseudoInst() ||
2515 (isa<BitCastInst>(BBI) && BBI->getType()->isPointerTy());
2520 if (BBI != FirstInstr)
2522 }
while (BBI != FirstInstr && IsNoopInstrForStoreMerging(BBI));
2524 return dyn_cast<StoreInst>(BBI);
2542 bool Changed =
false;
2545 if (!Inst.use_empty() && !Inst.getType()->isTokenTy()) {
2549 if (Inst.isEHPad() || Inst.getType()->isTokenTy())
2562 bool Changed =
false;
2564 if (Succ != LiveSucc)
2586 if (isa<SelectInst>(
Cond) &&
2605 auto *Cmp = cast<CmpInst>(
Cond);
2615 if (
auto *CI = dyn_cast<ConstantInt>(
Cond))
2629 for (
auto Case :
SI.cases()) {
2631 assert(isa<ConstantInt>(NewCase) &&
2632 "Result of expression should be constant");
2633 Case.setValue(cast<ConstantInt>(NewCase));
2639 SI.getParent(),
nullptr, *
this))
2641 if (
auto *CI = dyn_cast<ConstantInt>(
Cond))
2643 SI.getParent(),
SI.findCaseValue(CI)->getCaseSuccessor(), *
this))
2652 for (
const auto &
C :
SI.cases()) {
2654 std::min(LeadingKnownZeros,
C.getCaseValue()->getValue().countl_zero());
2656 std::min(LeadingKnownOnes,
C.getCaseValue()->getValue().countl_one());
2659 unsigned NewWidth = Known.
getBitWidth() - std::max(LeadingKnownZeros, LeadingKnownOnes);
2665 if (NewWidth > 0 && NewWidth < Known.
getBitWidth() &&
2666 shouldChangeType(Known.
getBitWidth(), NewWidth)) {
2671 for (
auto Case :
SI.cases()) {
2672 APInt TruncatedCase = Case.getCaseValue()->getValue().
trunc(NewWidth);
2688 const APInt *
C =
nullptr;
2690 if (*EV.
idx_begin() == 0 && (OvID == Intrinsic::smul_with_overflow ||
2691 OvID == Intrinsic::umul_with_overflow)) {
2696 if (
C->isPowerOf2()) {
2697 return BinaryOperator::CreateShl(
2707 if (!WO->hasOneUse())
2721 assert(*EV.
idx_begin() == 1 &&
"Unexpected extract index for overflow inst");
2724 if (OvID == Intrinsic::usub_with_overflow)
2729 if (OvID == Intrinsic::smul_with_overflow &&
2730 WO->getLHS()->getType()->isIntOrIntVectorTy(1))
2731 return BinaryOperator::CreateAnd(WO->getLHS(), WO->getRHS());
2740 WO->getBinaryOp(), *
C, WO->getNoWrapKind());
2745 auto *OpTy = WO->getRHS()->getType();
2746 auto *NewLHS = WO->getLHS();
2768 const unsigned *exti, *exte, *insi, *inse;
2769 for (exti = EV.
idx_begin(), insi =
IV->idx_begin(),
2770 exte = EV.
idx_end(), inse =
IV->idx_end();
2771 exti != exte && insi != inse;
2785 if (exti == exte && insi == inse)
2818 if (
Instruction *R = foldExtractOfOverflowIntrinsic(EV))
2821 if (
LoadInst *L = dyn_cast<LoadInst>(Agg)) {
2823 if (
auto *STy = dyn_cast<StructType>(Agg->
getType());
2824 STy && STy->containsScalableVectorType())
2832 if (L->isSimple() && L->hasOneUse()) {
2844 L->getPointerOperand(), Indices);
2848 NL->setAAMetadata(L->getAAMetadata());
2855 if (
auto *PN = dyn_cast<PHINode>(Agg))
2872 switch (Personality) {
2901 cast<ArrayType>(
LHS->
getType())->getNumElements()
2903 cast<ArrayType>(
RHS->
getType())->getNumElements();
2915 bool MakeNewInstruction =
false;
2917 bool CleanupFlag =
LI.isCleanup();
2920 for (
unsigned i = 0, e =
LI.getNumClauses(); i != e; ++i) {
2921 bool isLastClause = i + 1 == e;
2922 if (
LI.isCatch(i)) {
2929 if (AlreadyCaught.
insert(TypeInfo).second) {
2934 MakeNewInstruction =
true;
2941 MakeNewInstruction =
true;
2942 CleanupFlag =
false;
2953 assert(
LI.isFilter(i) &&
"Unsupported landingpad clause!");
2961 if (!NumTypeInfos) {
2964 MakeNewInstruction =
true;
2965 CleanupFlag =
false;
2969 bool MakeNewFilter =
false;
2971 if (isa<ConstantAggregateZero>(FilterClause)) {
2973 assert(NumTypeInfos > 0 &&
"Should have handled empty filter already!");
2979 MakeNewInstruction =
true;
2986 if (NumTypeInfos > 1)
2987 MakeNewFilter =
true;
2991 NewFilterElts.
reserve(NumTypeInfos);
2996 bool SawCatchAll =
false;
2997 for (
unsigned j = 0; j != NumTypeInfos; ++j) {
3025 if (SeenInFilter.
insert(TypeInfo).second)
3026 NewFilterElts.
push_back(cast<Constant>(Elt));
3031 MakeNewInstruction =
true;
3036 if (NewFilterElts.
size() < NumTypeInfos)
3037 MakeNewFilter =
true;
3039 if (MakeNewFilter) {
3041 NewFilterElts.
size());
3043 MakeNewInstruction =
true;
3052 if (MakeNewFilter && !NewFilterElts.
size()) {
3053 assert(MakeNewInstruction &&
"New filter but not a new instruction!");
3054 CleanupFlag =
false;
3065 for (
unsigned i = 0, e = NewClauses.
size(); i + 1 < e; ) {
3068 for (j = i; j != e; ++j)
3069 if (!isa<ArrayType>(NewClauses[j]->
getType()))
3075 for (
unsigned k = i; k + 1 < j; ++k)
3079 std::stable_sort(NewClauses.
begin() + i, NewClauses.
begin() + j,
3081 MakeNewInstruction =
true;
3100 for (
unsigned i = 0; i + 1 < NewClauses.
size(); ++i) {
3110 for (
unsigned j = NewClauses.
size() - 1; j != i; --j) {
3111 Value *LFilter = NewClauses[j];
3122 NewClauses.
erase(J);
3123 MakeNewInstruction =
true;
3133 if (isa<ConstantAggregateZero>(LFilter)) {
3136 if (isa<ConstantAggregateZero>(
Filter)) {
3137 assert(FElts <= LElts &&
"Should have handled this case earlier!");
3139 NewClauses.
erase(J);
3140 MakeNewInstruction =
true;
3146 if (isa<ConstantAggregateZero>(
Filter)) {
3149 assert(FElts > 0 &&
"Should have eliminated the empty filter earlier!");
3150 for (
unsigned l = 0; l != LElts; ++l)
3153 NewClauses.
erase(J);
3154 MakeNewInstruction =
true;
3165 bool AllFound =
true;
3166 for (
unsigned f = 0; f != FElts; ++f) {
3169 for (
unsigned l = 0; l != LElts; ++l) {
3171 if (LTypeInfo == FTypeInfo) {
3181 NewClauses.
erase(J);
3182 MakeNewInstruction =
true;
3190 if (MakeNewInstruction) {
3193 for (
unsigned i = 0, e = NewClauses.
size(); i != e; ++i)
3198 if (NewClauses.
empty())
3206 if (
LI.isCleanup() != CleanupFlag) {
3207 assert(!CleanupFlag &&
"Adding a cleanup, not removing one?!");
3208 LI.setCleanup(CleanupFlag);
3232 auto *OrigOpInst = dyn_cast<Instruction>(OrigOp);
3237 if (!OrigOpInst || !OrigOpInst->hasOneUse() || isa<PHINode>(OrigOp))
3251 Use *MaybePoisonOperand =
nullptr;
3252 for (
Use &U : OrigOpInst->operands()) {
3253 if (isa<MetadataAsValue>(U.get()) ||
3256 if (!MaybePoisonOperand)
3257 MaybePoisonOperand = &U;
3262 OrigOpInst->dropPoisonGeneratingFlagsAndMetadata();
3265 if (!MaybePoisonOperand)
3270 MaybePoisonOperand->get(), MaybePoisonOperand->get()->
getName() +
".fr");
3272 replaceUse(*MaybePoisonOperand, FrozenMaybePoisonOperand);
3283 Use *StartU =
nullptr;
3301 Value *StartV = StartU->get();
3313 if (!Visited.
insert(V).second)
3316 if (Visited.
size() > 32)
3333 I->dropPoisonGeneratingFlagsAndMetadata();
3335 if (StartNeedsFreeze) {
3347 if (isa<Constant>(Op) || Op->hasOneUse())
3356 if (isa<Argument>(Op)) {
3365 bool Changed =
false;
3366 if (&FI != MoveBefore) {
3371 Op->replaceUsesWithIf(&FI, [&](
Use &U) ->
bool {
3373 Changed |= Dominates;
3382 for (
auto *U : V->users()) {
3383 if (isa<ShuffleVectorInst>(U))
3392 Value *Op0 =
I.getOperand(0);
3398 if (
auto *PN = dyn_cast<PHINode>(Op0)) {
3421 auto getUndefReplacement = [&
I](
Type *Ty) {
3424 for (
const auto *U :
I.users()) {
3433 else if (BestValue !=
C)
3434 BestValue = NullValue;
3436 assert(BestValue &&
"Must have at least one use");
3451 Constant *ReplaceC = getUndefReplacement(
I.getType()->getScalarType());
3466 auto *CB = dyn_cast<CallBase>(
I);
3485 for (
const User *U :
I.users()) {
3486 if (Visited.
insert(U).second)
3491 while (!AllocaUsers.
empty()) {
3492 auto *UserI = cast<Instruction>(AllocaUsers.
pop_back_val());
3493 if (isa<BitCastInst>(UserI) || isa<GetElementPtrInst>(UserI) ||
3494 isa<AddrSpaceCastInst>(UserI)) {
3515 if (isa<PHINode>(
I) ||
I->isEHPad() ||
I->mayThrow() || !
I->willReturn() ||
3523 if (isa<AllocaInst>(
I))
3531 if (
auto *CI = dyn_cast<CallInst>(
I)) {
3532 if (CI->isConvergent())
3538 if (
I->mayWriteToMemory()) {
3545 if (
I->mayReadFromMemory()) {
3552 E =
I->getParent()->end();
3554 if (Scan->mayWriteToMemory())
3558 I->dropDroppableUses([&](
const Use *U) {
3559 auto *
I = dyn_cast<Instruction>(U->getUser());
3560 if (
I &&
I->getParent() != DestBlock) {
3570 I->moveBefore(&*InsertPos);
3584 if (DVI->getParent() == SrcBlock)
3587 [](
auto *
A,
auto *
B) {
return B->comesBefore(
A); });
3591 for (
auto *
User : DbgUsersToSink) {
3596 if (isa<DbgDeclareInst>(
User))
3601 User->getDebugLoc()->getInlinedAt());
3603 if (!SunkVariables.
insert(DbgUserVariable).second)
3608 if (isa<DbgAssignIntrinsic>(
User))
3611 DIIClones.emplace_back(cast<DbgVariableIntrinsic>(
User->clone()));
3612 if (isa<DbgDeclareInst>(
User) && isa<CastInst>(
I))
3613 DIIClones.back()->replaceVariableLocationOp(
I,
I->getOperand(0));
3618 if (!DIIClones.empty()) {
3623 DIIClone->insertBefore(&*InsertPos);
3650 if (
I ==
nullptr)
continue;
3665 auto getOptionalSinkBlockForInst =
3666 [
this](
Instruction *
I) -> std::optional<BasicBlock *> {
3668 return std::nullopt;
3672 unsigned NumUsers = 0;
3674 for (
auto *U :
I->users()) {
3675 if (U->isDroppable())
3678 return std::nullopt;
3682 if (
PHINode *PN = dyn_cast<PHINode>(UserInst)) {
3683 for (
unsigned i = 0; i < PN->getNumIncomingValues(); i++) {
3684 if (PN->getIncomingValue(i) ==
I) {
3688 if (UserParent && UserParent != PN->getIncomingBlock(i))
3689 return std::nullopt;
3690 UserParent = PN->getIncomingBlock(i);
3693 assert(UserParent &&
"expected to find user block!");
3695 if (UserParent && UserParent != UserInst->
getParent())
3696 return std::nullopt;
3702 if (NumUsers == 0) {
3706 return std::nullopt;
3718 return std::nullopt;
3728 return std::nullopt;
3733 auto OptBB = getOptionalSinkBlockForInst(
I);
3735 auto *UserParent = *OptBB;
3743 for (
Use &U :
I->operands())
3744 if (
Instruction *OpI = dyn_cast<Instruction>(U.get()))
3752 I, {LLVMContext::MD_dbg, LLVMContext::MD_annotation});
3765 <<
" New = " << *Result <<
'\n');
3767 Result->copyMetadata(*
I,
3768 {LLVMContext::MD_dbg, LLVMContext::MD_annotation});
3770 I->replaceAllUsesWith(Result);
3773 Result->takeName(
I);
3780 if (isa<PHINode>(Result) != isa<PHINode>(
I)) {
3782 if (isa<PHINode>(
I))
3788 Result->insertInto(InstParent, InsertPos);
3797 <<
" New = " << *
I <<
'\n');
3829 if (!
I->hasMetadataOtherThanDebugLoc())
3832 auto Track = [](
Metadata *ScopeList,
auto &Container) {
3833 const auto *MDScopeList = dyn_cast_or_null<MDNode>(ScopeList);
3834 if (!MDScopeList || !Container.insert(MDScopeList).second)
3836 for (
const auto &
MDOperand : MDScopeList->operands())
3837 if (
auto *MDScope = dyn_cast<MDNode>(
MDOperand))
3838 Container.insert(MDScope);
3841 Track(
I->getMetadata(LLVMContext::MD_alias_scope), UsedAliasScopesAndLists);
3842 Track(
I->getMetadata(LLVMContext::MD_noalias), UsedNoAliasScopesAndLists);
3851 "llvm.experimental.noalias.scope.decl in use ?");
3854 "llvm.experimental.noalias.scope should refer to a single scope");
3856 if (
auto *MD = dyn_cast<MDNode>(
MDOperand))
3857 return !UsedAliasScopesAndLists.
contains(MD) ||
3858 !UsedNoAliasScopesAndLists.
contains(MD);
3876 bool MadeIRChange =
false;
3889 if (!Visited.
insert(BB).second)
3894 if (!Inst.use_empty() &&
3895 (Inst.getNumOperands() == 0 || isa<Constant>(Inst.getOperand(0))))
3899 Inst.replaceAllUsesWith(
C);
3902 Inst.eraseFromParent();
3903 MadeIRChange =
true;
3908 for (
Use &U : Inst.operands()) {
3909 if (!isa<ConstantVector>(U) && !isa<ConstantExpr>(U))
3912 auto *
C = cast<Constant>(U);
3913 Constant *&FoldRes = FoldedConstants[
C];
3919 <<
"\n Old = " << *
C
3920 <<
"\n New = " << *FoldRes <<
'\n');
3922 MadeIRChange =
true;
3929 if (!Inst.isDebugOrPseudoInst()) {
3930 InstrsForInstructionWorklist.
push_back(&Inst);
3931 SeenAliasScopes.
analyse(&Inst);
3939 if (isa<UndefValue>(BI->getCondition()))
3942 if (
auto *
Cond = dyn_cast<ConstantInt>(BI->getCondition())) {
3943 bool CondVal =
Cond->getZExtValue();
3944 BasicBlock *ReachableBB = BI->getSuccessor(!CondVal);
3948 }
else if (
SwitchInst *
SI = dyn_cast<SwitchInst>(TI)) {
3949 if (isa<UndefValue>(
SI->getCondition()))
3952 if (
auto *
Cond = dyn_cast<ConstantInt>(
SI->getCondition())) {
3959 }
while (!Worklist.
empty());
3965 if (Visited.
count(&BB))
3968 unsigned NumDeadInstInBB;
3969 unsigned NumDeadDbgInstInBB;
3970 std::tie(NumDeadInstInBB, NumDeadDbgInstInBB) =
3973 MadeIRChange |= NumDeadInstInBB + NumDeadDbgInstInBB > 0;
3974 NumDeadInst += NumDeadInstInBB;
3982 ICWorklist.
reserve(InstrsForInstructionWorklist.
size());
3991 Inst->eraseFromParent();
3992 MadeIRChange =
true;
3996 ICWorklist.
push(Inst);
3999 return MadeIRChange;
4007 auto &
DL =
F.getParent()->getDataLayout();
4015 if (
auto *Assume = dyn_cast<AssumeInst>(
I))
4021 bool MadeIRChange =
false;
4026 unsigned Iteration = 0;
4028 ++NumWorklistIterations;
4033 "Instruction Combining seems stuck in an infinite loop after " +
4037 if (Iteration > MaxIterations) {
4038 LLVM_DEBUG(
dbgs() <<
"\n\n[IC] Iteration limit #" << MaxIterations
4039 <<
" on " <<
F.getName()
4040 <<
" reached; stopping before reaching a fixpoint\n");
4044 LLVM_DEBUG(
dbgs() <<
"\n\nINSTCOMBINE ITERATION #" << Iteration <<
" on "
4045 <<
F.getName() <<
"\n");
4050 ORE, BFI, PSI,
DL, LI);
4056 MadeIRChange =
true;
4061 else if (Iteration == 2)
4063 else if (Iteration == 3)
4064 ++NumThreeIterations;
4066 ++NumFourOrMoreIterations;
4068 return MadeIRChange;
4076 OS, MapClassName2PassName);
4079 OS << (Options.
UseLoopInfo ?
"" :
"no-") <<
"use-loop-info";
4101 auto *BFI = (PSI && PSI->hasProfileSummary()) ?
4136 auto AA = &getAnalysis<AAResultsWrapperPass>().getAAResults();
4137 auto &AC = getAnalysis<AssumptionCacheTracker>().getAssumptionCache(
F);
4138 auto &TLI = getAnalysis<TargetLibraryInfoWrapperPass>().getTLI(
F);
4139 auto &
TTI = getAnalysis<TargetTransformInfoWrapperPass>().getTTI(
F);
4140 auto &DT = getAnalysis<DominatorTreeWrapperPass>().getDomTree();
4141 auto &ORE = getAnalysis<OptimizationRemarkEmitterWrapperPass>().getORE();
4144 auto *LIWP = getAnalysisIfAvailable<LoopInfoWrapperPass>();
4145 auto *LI = LIWP ? &LIWP->getLoopInfo() :
nullptr;
4147 &getAnalysis<ProfileSummaryInfoWrapperPass>().getPSI();
4150 &getAnalysis<LazyBlockFrequencyInfoPass>().getBFI() :
4154 BFI, PSI, MaxIterations, LI);
4170 "Combine redundant instructions",
false,
false)
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
This file implements a class to represent arbitrary precision integral constant values and operations...
static const Function * getParent(const Value *V)
This is the interface for LLVM's primary stateless and local alias analysis.
SmallVector< MachineOperand, 4 > Cond
BlockVerifier::State From
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
static GCRegistry::Add< StatepointGC > D("statepoint-example", "an example strategy for statepoint")
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
This file contains the declarations for the subclasses of Constant, which represent the different fla...
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 an implementation of debug counters.
#define DEBUG_COUNTER(VARNAME, COUNTERNAME, DESC)
This file defines the DenseMap class.
static GCMetadataPrinterRegistry::Add< ErlangGCPrinter > X("erlang", "erlang-compatible garbage collector")
This is the interface for a simple mod/ref and alias analysis over globals.
This file provides various utilities for inspecting and working with the control flow graph in LLVM I...
iv Induction Variable Users
This file provides internal interfaces used to implement the InstCombine.
This file provides the primary interface to the instcombine pass.
static bool isUsedWithinShuffleVector(Value *V)
static bool isNeverEqualToUnescapedAlloc(Value *V, const TargetLibraryInfo &TLI, Instruction *AI)
static bool shorter_filter(const Value *LHS, const Value *RHS)
static Instruction * foldSelectGEP(GetElementPtrInst &GEP, InstCombiner::BuilderTy &Builder)
Thread a GEP operation with constant indices through the constant true/false arms of a select.
static bool prepareICWorklistFromFunction(Function &F, const DataLayout &DL, const TargetLibraryInfo *TLI, InstructionWorklist &ICWorklist)
Populate the IC worklist from a function, by walking it in depth-first order and adding all reachable...
static bool shouldMergeGEPs(GEPOperator &GEP, GEPOperator &Src)
static cl::opt< unsigned > MaxArraySize("instcombine-maxarray-size", cl::init(1024), cl::desc("Maximum array size considered when doing a combine"))
static bool combineInstructionsOverFunction(Function &F, InstructionWorklist &Worklist, AliasAnalysis *AA, AssumptionCache &AC, TargetLibraryInfo &TLI, TargetTransformInfo &TTI, DominatorTree &DT, OptimizationRemarkEmitter &ORE, BlockFrequencyInfo *BFI, ProfileSummaryInfo *PSI, unsigned MaxIterations, LoopInfo *LI)
static cl::opt< unsigned > ShouldLowerDbgDeclare("instcombine-lower-dbg-declare", cl::Hidden, cl::init(true))
static bool hasNoSignedWrap(BinaryOperator &I)
static bool simplifyAssocCastAssoc(BinaryOperator *BinOp1, InstCombinerImpl &IC)
Combine constant operands of associative operations either before or after a cast to eliminate one of...
static void ClearSubclassDataAfterReassociation(BinaryOperator &I)
Conservatively clears subclassOptionalData after a reassociation or commutation.
static bool handlePotentiallyDeadBlock(BasicBlock *BB, InstCombiner &IC)
If a block is dead due to a known branch condition, remove instructions in it.
static cl::opt< unsigned > InfiniteLoopDetectionThreshold("instcombine-infinite-loop-threshold", cl::desc("Number of instruction combining iterations considered an " "infinite loop"), cl::init(InstCombineDefaultInfiniteLoopThreshold), cl::Hidden)
static bool isAllocSiteRemovable(Instruction *AI, SmallVectorImpl< WeakTrackingVH > &Users, const TargetLibraryInfo &TLI)
static Value * getIdentityValue(Instruction::BinaryOps Opcode, Value *V)
This function returns identity value for given opcode, which can be used to factor patterns like (X *...
static bool leftDistributesOverRight(Instruction::BinaryOps LOp, Instruction::BinaryOps ROp)
Return whether "X LOp (Y ROp Z)" is always equal to "(X LOp Y) ROp (X LOp Z)".
static Value * foldOperationIntoSelectOperand(Instruction &I, SelectInst *SI, Value *NewOp, InstCombiner &IC)
static constexpr unsigned InstCombineDefaultInfiniteLoopThreshold
static Instruction * tryToMoveFreeBeforeNullTest(CallInst &FI, const DataLayout &DL)
Move the call to free before a NULL test.
static bool handlePotentiallyDeadSuccessors(BasicBlock *BB, BasicBlock *LiveSucc, InstCombiner &IC)
static bool rightDistributesOverLeft(Instruction::BinaryOps LOp, Instruction::BinaryOps ROp)
Return whether "(X LOp Y) ROp Z" is always equal to "(X ROp Z) LOp (Y ROp Z)".
static Value * tryFactorization(BinaryOperator &I, const SimplifyQuery &SQ, InstCombiner::BuilderTy &Builder, Instruction::BinaryOps InnerOpcode, Value *A, Value *B, Value *C, Value *D)
This tries to simplify binary operations by factorizing out common terms (e.
static bool isRemovableWrite(CallBase &CB, Value *UsedV, const TargetLibraryInfo &TLI)
Given a call CB which uses an address UsedV, return true if we can prove the call's only possible eff...
static Instruction::BinaryOps getBinOpsForFactorization(Instruction::BinaryOps TopOpcode, BinaryOperator *Op, Value *&LHS, Value *&RHS)
This function predicates factorization using distributive laws.
static bool hasNoUnsignedWrap(BinaryOperator &I)
static bool SoleWriteToDeadLocal(Instruction *I, TargetLibraryInfo &TLI)
Check for case where the call writes to an otherwise dead alloca.
static cl::opt< unsigned > MaxSinkNumUsers("instcombine-max-sink-users", cl::init(32), cl::desc("Maximum number of undroppable users for instruction sinking"))
static Constant * constantFoldOperationIntoSelectOperand(Instruction &I, SelectInst *SI, bool IsTrueArm)
static bool isCatchAll(EHPersonality Personality, Constant *TypeInfo)
Return 'true' if the given typeinfo will match anything.
static bool isMergedGEPInBounds(GEPOperator &GEP1, GEPOperator &GEP2)
static cl::opt< bool > EnableCodeSinking("instcombine-code-sinking", cl::desc("Enable code sinking"), cl::init(true))
static bool maintainNoSignedWrap(BinaryOperator &I, Value *B, Value *C)
print must be executed print the must be executed context for all instructions
static GCMetadataPrinterRegistry::Add< OcamlGCMetadataPrinter > Y("ocaml", "ocaml 3.10-compatible collector")
This header defines various interfaces for pass management in LLVM.
#define INITIALIZE_PASS_DEPENDENCY(depName)
#define INITIALIZE_PASS_END(passName, arg, name, cfg, analysis)
#define INITIALIZE_PASS_BEGIN(passName, arg, name, cfg, analysis)
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
This file defines the SmallPtrSet class.
This file defines the SmallVector class.
This file defines the 'Statistic' class, which is designed to be an easy way to expose various metric...
#define STATISTIC(VARNAME, DESC)
static SymbolRef::Type getType(const Symbol *Sym)
This defines the Use class.
static std::optional< unsigned > getOpcode(ArrayRef< VPValue * > Values)
Returns the opcode of Values or ~0 if they do not all agree.
static const uint32_t IV[8]
bool isNoAliasScopeDeclDead(Instruction *Inst)
void analyse(Instruction *I)
A manager for alias analyses.
A wrapper pass to provide the legacy pass manager access to a suitably prepared AAResults object.
Class for arbitrary precision integers.
static APInt getAllOnes(unsigned numBits)
Return an APInt of a specified width with all bits set.
bool isMinSignedValue() const
Determine if this is the smallest signed value.
APInt trunc(unsigned width) const
Truncate to new width.
APInt sadd_ov(const APInt &RHS, bool &Overflow) const
bool isNonNegative() const
Determine if this APInt Value is non-negative (>= 0)
bool ule(const APInt &RHS) const
Unsigned less or equal comparison.
APInt ssub_ov(const APInt &RHS, bool &Overflow) const
A container for analyses that lazily runs them and caches their results.
PassT::Result * getCachedResult(IRUnitT &IR) const
Get the cached result of an analysis pass for a given IR unit.
PassT::Result & getResult(IRUnitT &IR, ExtraArgTs... ExtraArgs)
Get the result of an analysis pass for a given IR unit.
Represent the analysis usage information of a pass.
AnalysisUsage & addRequired()
AnalysisUsage & addPreserved()
Add the specified Pass class to the set of analyses preserved by this pass.
void setPreservesCFG()
This function should be called by the pass, iff they do not:
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
size_t size() const
size - Get the array size.
Class to represent array types.
uint64_t getNumElements() const
static ArrayType * get(Type *ElementType, uint64_t NumElements)
This static method is the primary way to construct an ArrayType.
Type * getElementType() const
A function analysis which provides an AssumptionCache.
An immutable pass that tracks lazily created AssumptionCache objects.
A cache of @llvm.assume calls within a function.
void registerAssumption(AssumeInst *CI)
Add an @llvm.assume intrinsic to this function's cache.
uint64_t getDereferenceableBytes() const
Returns the number of dereferenceable bytes from the dereferenceable attribute.
bool isValid() const
Return true if the attribute is any kind of attribute.
Legacy wrapper pass to provide the BasicAAResult object.
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...
iterator_range< filter_iterator< BasicBlock::const_iterator, std::function< bool(const Instruction &)> > > instructionsWithoutDebug(bool SkipPseudoOp=true) const
Return a const iterator range over the instructions in the block, skipping any debug instructions.
const Instruction * getFirstNonPHI() const
Returns a pointer to the first instruction in this block that is not a PHINode instruction.
const BasicBlock * getSinglePredecessor() const
Return the predecessor of this block if it has a single predecessor block.
const BasicBlock * getUniquePredecessor() const
Return the predecessor of this block if it has a unique predecessor block.
const Function * getParent() const
Return the enclosing method, or null if none.
InstListType::iterator iterator
Instruction iterators...
const_iterator getFirstNonPHIOrDbgOrAlloca() const
Returns an iterator to the first instruction in this block that is not a PHINode, a debug intrinsic,...
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...
static BinaryOperator * Create(BinaryOps Op, Value *S1, Value *S2, const Twine &Name=Twine(), Instruction *InsertBefore=nullptr)
Construct a binary instruction, given the opcode and the two operands.
static BinaryOperator * CreateNeg(Value *Op, const Twine &Name="", Instruction *InsertBefore=nullptr)
Helper functions to construct and inspect unary operations (NEG and NOT) via binary operators SUB and...
BinaryOps getOpcode() const
static BinaryOperator * CreateNUW(BinaryOps Opc, Value *V1, Value *V2, const Twine &Name="")
Analysis pass which computes BlockFrequencyInfo.
BlockFrequencyInfo pass uses BlockFrequencyInfoImpl implementation to estimate IR basic block frequen...
Conditional or Unconditional Branch instruction.
void swapSuccessors()
Swap the successors of this branch instruction.
bool isConditional() const
BasicBlock * getSuccessor(unsigned i) const
bool isUnconditional() const
Value * getCondition() const
Represents analyses that only rely on functions' control flow.
Base class for all callable instructions (InvokeInst and CallInst) Holds everything related to callin...
Function * getCalledFunction() const
Returns the function called, or null if this is an indirect function invocation or the function signa...
void setAttributes(AttributeList A)
Set the parameter attributes for this call.
bool doesNotThrow() const
Determine if the call cannot unwind.
Value * getArgOperand(unsigned i) const
AttributeList getAttributes() const
Return the parameter attributes for this call.
This class represents a function call, abstracting a target machine's calling convention.
static CallInst * Create(FunctionType *Ty, Value *F, const Twine &NameStr="", Instruction *InsertBefore=nullptr)
static CastInst * Create(Instruction::CastOps, Value *S, Type *Ty, const Twine &Name="", Instruction *InsertBefore=nullptr)
Provides a way to construct any of the CastInst subclasses using an opcode instead of the subclass's ...
static CastInst * CreatePointerBitCastOrAddrSpaceCast(Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd)
Create a BitCast or an AddrSpaceCast cast instruction.
Predicate
This enumeration lists the possible predicates for CmpInst subclasses.
@ ICMP_ULT
unsigned less than
Predicate getInversePredicate() const
For example, EQ -> NE, UGT -> ULE, SLT -> SGE, OEQ -> UNE, UGT -> OLE, OLT -> UGE,...
ConstantArray - Constant Array Declarations.
static Constant * get(ArrayType *T, ArrayRef< Constant * > V)
A vector constant whose element type is a simple 1/2/4/8-byte integer or float/double,...
static Constant * getCast(unsigned ops, Constant *C, Type *Ty, bool OnlyIfReduced=false)
Convenience function for getting a Cast operation.
static Constant * getSub(Constant *C1, Constant *C2, bool HasNUW=false, bool HasNSW=false)
static Constant * getShl(Constant *C1, Constant *C2, bool HasNUW=false, bool HasNSW=false)
static Constant * get(unsigned Opcode, Constant *C1, Constant *C2, unsigned Flags=0, Type *OnlyIfReducedTy=nullptr)
get - Return a binary or shift operator constant expression, folding if possible.
static Constant * getBinOpIdentity(unsigned Opcode, Type *Ty, bool AllowRHSConstant=false, bool NSZ=false)
Return the identity constant for a binary opcode.
static Constant * getTrunc(Constant *C, Type *Ty, bool OnlyIfReduced=false)
static Constant * getNeg(Constant *C, bool HasNUW=false, bool HasNSW=false)
This is the shared class of boolean and integer constants.
static ConstantInt * getTrue(LLVMContext &Context)
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 ConstantInt * getFalse(LLVMContext &Context)
This class represents a range of values.
bool getEquivalentICmp(CmpInst::Predicate &Pred, APInt &RHS) const
Set up Pred and RHS such that ConstantRange::makeExactICmpRegion(Pred, RHS) == *this.
static ConstantRange makeExactNoWrapRegion(Instruction::BinaryOps BinOp, const APInt &Other, unsigned NoWrapKind)
Produce the range that contains X if and only if "X BinOp Other" does not wrap.
Constant Vector Declarations.
static Constant * get(ArrayRef< Constant * > V)
This is an important base class in LLVM.
static Constant * replaceUndefsWith(Constant *C, Constant *Replacement)
Try to replace undefined constant C or undefined elements in C with Replacement.
static Constant * getAllOnesValue(Type *Ty)
const Constant * stripPointerCasts() const
static Constant * getNullValue(Type *Ty)
Constructor to create a '0' constant of arbitrary type.
Constant * getAggregateElement(unsigned Elt) const
For aggregates (struct/array/vector) return the constant that corresponds to the specified element if...
bool isNullValue() const
Return true if this is the value that would be returned by getNullValue.
A parsed version of the target data layout string in and methods for querying it.
SmallVector< APInt > getGEPIndicesForOffset(Type *&ElemTy, APInt &Offset) const
Get GEP indices to access Offset inside ElemTy.
bool isLegalInteger(uint64_t Width) const
Returns true if the specified type is known to be a native integer type supported by the CPU.
unsigned getIndexTypeSizeInBits(Type *Ty) const
Layout size of the index used in GEP calculation.
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...
unsigned getIndexSizeInBits(unsigned AS) const
Size in bits of index used for address calculation in getelementptr.
TypeSize getTypeSizeInBits(Type *Ty) const
Size examples:
int64_t getIndexedOffsetInType(Type *ElemTy, ArrayRef< Value * > Indices) const
Returns the offset from the beginning of the type for the specified indices.
This is the common base class for debug info intrinsics for variables.
static bool shouldExecute(unsigned CounterName)
Identifies a unique instance of a variable.
Analysis pass which computes a DominatorTree.
Legacy analysis pass which computes a DominatorTree.
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.
Utility class for floating point operations which can have information about relaxed accuracy require...
Convenience struct for specifying and reasoning about fast-math flags.
This class represents a freeze function that returns random concrete value if an operand is either a ...
FunctionPass class - This class is used to implement most global optimizations.
bool skipFunction(const Function &F) const
Optional passes call this function to check whether the pass should be skipped.
const BasicBlock & getEntryBlock() const
static bool isTargetIntrinsic(Intrinsic::ID IID)
isTargetIntrinsic - Returns true if IID is an intrinsic specific to a certain target.
bool isInBounds() const
Test whether this is an inbounds GEP, as defined by LangRef.html.
bool hasAllZeroIndices() const
Return true if all of the indices of this GEP are zeros.
an instruction for type-safe pointer arithmetic to access elements of arrays and structs
static GetElementPtrInst * CreateInBounds(Type *PointeeType, Value *Ptr, ArrayRef< Value * > IdxList, const Twine &NameStr="", Instruction *InsertBefore=nullptr)
Create an "inbounds" getelementptr.
static Type * getTypeAtIndex(Type *Ty, Value *Idx)
Return the type of the element at the given index of an indexable type.
Legacy wrapper pass to provide the GlobalsAAResult object.
This instruction compares its operands according to the predicate given to the constructor.
static bool isEquality(Predicate P)
Return true if this predicate is either EQ or NE.
Value * CreateTrunc(Value *V, Type *DestTy, const Twine &Name="")
Value * CreateExtractValue(Value *Agg, ArrayRef< unsigned > Idxs, const Twine &Name="")
Value * CreateSelect(Value *C, Value *True, Value *False, const Twine &Name="", Instruction *MDFrom=nullptr)