Go to the documentation of this file.
112 using namespace llvm;
115 #define DEBUG_TYPE "instcombine"
118 "Number of instruction combining iterations performed");
120 STATISTIC(NumCombined ,
"Number of insts combined");
121 STATISTIC(NumConstProp,
"Number of constant folds");
122 STATISTIC(NumDeadInst ,
"Number of dead inst eliminated");
123 STATISTIC(NumSunkInst ,
"Number of instructions sunk");
124 STATISTIC(NumExpand,
"Number of expansions");
125 STATISTIC(NumFactor ,
"Number of factorizations");
126 STATISTIC(NumReassoc ,
"Number of reassociations");
128 "Controls which instructions are visited");
143 "instcombine-max-iterations",
144 cl::desc(
"Limit the maximum number of instruction combining iterations"),
148 "instcombine-infinite-loop-threshold",
149 cl::desc(
"Number of instruction combining iterations considered an "
155 cl::desc(
"Maximum array size considered when doing a combine"));
178 bool &KnownBitsComputed) {
195 *
this, II, DemandedElts, UndefElts, UndefElts2, UndefElts3,
214 bool InstCombinerImpl::shouldChangeType(
unsigned FromWidth,
215 unsigned ToWidth)
const {
216 bool FromLegal = FromWidth == 1 ||
DL.isLegalInteger(FromWidth);
217 bool ToLegal = ToWidth == 1 ||
DL.isLegalInteger(ToWidth);
221 if (ToWidth < FromWidth && (ToWidth == 8 || ToWidth == 16 || ToWidth == 32))
226 if (FromLegal && !ToLegal)
231 if (!FromLegal && !ToLegal && ToWidth > FromWidth)
242 bool InstCombinerImpl::shouldChangeType(
Type *
From,
Type *To)
const {
248 unsigned FromWidth =
From->getPrimitiveSizeInBits();
250 return shouldChangeType(FromWidth, ToWidth);
259 auto *OBO = dyn_cast<OverflowingBinaryOperator>(&
I);
260 if (!OBO || !OBO->hasNoSignedWrap())
268 const APInt *BVal, *CVal;
272 bool Overflow =
false;
274 (void)BVal->
sadd_ov(*CVal, Overflow);
276 (
void)BVal->
ssub_ov(*CVal, Overflow);
282 auto *OBO = dyn_cast<OverflowingBinaryOperator>(&
I);
283 return OBO && OBO->hasNoUnsignedWrap();
287 auto *OBO = dyn_cast<OverflowingBinaryOperator>(&
I);
288 return OBO && OBO->hasNoSignedWrap();
297 I.clearSubclassOptionalData();
302 I.clearSubclassOptionalData();
303 I.setFastMathFlags(FMF);
312 auto *Cast = dyn_cast<CastInst>(BinOp1->
getOperand(0));
313 if (!Cast || !Cast->hasOneUse())
317 auto CastOpcode = Cast->getOpcode();
318 if (CastOpcode != Instruction::ZExt)
326 auto *BinOp2 = dyn_cast<BinaryOperator>(Cast->getOperand(0));
327 if (!BinOp2 || !BinOp2->hasOneUse() || BinOp2->getOpcode() != AssocOpcode)
341 Type *DestTy =
C1->getType();
371 bool Changed =
false;
377 if (
I.isCommutative() && getComplexity(
I.getOperand(0)) <
378 getComplexity(
I.getOperand(1)))
379 Changed = !
I.swapOperands();
384 if (
I.isAssociative()) {
394 replaceOperand(
I, 0, A);
395 replaceOperand(
I, 1, V);
407 I.setHasNoUnsignedWrap(
true);
410 I.setHasNoSignedWrap(
true);
420 Value *A =
I.getOperand(0);
427 replaceOperand(
I, 0, V);
428 replaceOperand(
I, 1,
C);
439 if (
I.isAssociative() &&
I.isCommutative()) {
455 replaceOperand(
I, 0, V);
456 replaceOperand(
I, 1,
B);
468 Value *A =
I.getOperand(0);
475 replaceOperand(
I, 0,
B);
476 replaceOperand(
I, 1, V);
501 if (isa<FPMathOperator>(NewBO)) {
507 InsertNewInstWith(NewBO,
I);
509 replaceOperand(
I, 0, NewBO);
515 I.setHasNoUnsignedWrap(
true);
533 if (LOp == Instruction::And)
534 return ROp == Instruction::Or || ROp == Instruction::Xor;
537 if (LOp == Instruction::Or)
538 return ROp == Instruction::And;
542 if (LOp == Instruction::Mul)
566 if (isa<Constant>(V))
580 assert(
Op &&
"Expected a binary operator");
581 LHS =
Op->getOperand(0);
582 RHS =
Op->getOperand(1);
588 return Instruction::Mul;
592 return Op->getOpcode();
601 assert(A &&
B &&
C &&
D &&
"All values must be provided");
604 Value *SimplifiedInst =
nullptr;
605 Value *LHS =
I.getOperand(0), *RHS =
I.getOperand(1);
615 if (A ==
C || (InnerCommutative && A ==
D)) {
623 if (!V && LHS->
hasOneUse() && RHS->hasOneUse())
624 V =
Builder.CreateBinOp(TopLevelOpcode,
B,
D, RHS->getName());
626 SimplifiedInst =
Builder.CreateBinOp(InnerOpcode, A, V);
634 if (
B ==
D || (InnerCommutative &&
B ==
C)) {
643 if (!V && LHS->
hasOneUse() && RHS->hasOneUse())
646 SimplifiedInst =
Builder.CreateBinOp(InnerOpcode, V,
B);
650 if (SimplifiedInst) {
655 if (
BinaryOperator *BO = dyn_cast<BinaryOperator>(SimplifiedInst)) {
656 if (isa<OverflowingBinaryOperator>(SimplifiedInst)) {
659 if (isa<OverflowingBinaryOperator>(&
I)) {
660 HasNSW =
I.hasNoSignedWrap();
661 HasNUW =
I.hasNoUnsignedWrap();
664 if (
auto *LOBO = dyn_cast<OverflowingBinaryOperator>(LHS)) {
665 HasNSW &= LOBO->hasNoSignedWrap();
666 HasNUW &= LOBO->hasNoUnsignedWrap();
669 if (
auto *ROBO = dyn_cast<OverflowingBinaryOperator>(RHS)) {
670 HasNSW &= ROBO->hasNoSignedWrap();
671 HasNUW &= ROBO->hasNoUnsignedWrap();
675 InnerOpcode == Instruction::Mul) {
686 BO->setHasNoSignedWrap(HasNSW);
690 BO->setHasNoUnsignedWrap(HasNUW);
695 return SimplifiedInst;
704 Value *LHS =
I.getOperand(0), *RHS =
I.getOperand(1);
720 if (Op0 && Op1 && LHSOpcode == RHSOpcode)
721 if (
Value *V = tryFactorization(
I, LHSOpcode, A,
B,
C,
D))
728 if (
Value *V = tryFactorization(
I, LHSOpcode, A,
B, RHS, Ident))
735 if (
Value *V = tryFactorization(
I, RHSOpcode, LHS, Ident,
C,
D))
747 auto SQDistributive = SQ.getWithInstruction(&
I).getWithoutUndef();
755 C =
Builder.CreateBinOp(InnerOpcode, L, R);
764 C =
Builder.CreateBinOp(TopLevelOpcode,
B,
C);
773 C =
Builder.CreateBinOp(TopLevelOpcode, A,
C);
786 auto SQDistributive = SQ.getWithInstruction(&
I).getWithoutUndef();
794 A =
Builder.CreateBinOp(InnerOpcode, L, R);
803 A =
Builder.CreateBinOp(TopLevelOpcode, A,
C);
812 A =
Builder.CreateBinOp(TopLevelOpcode, A,
B);
818 return SimplifySelectsFeedingBinaryOp(
I, LHS, RHS);
827 if (!LHSIsSelect && !RHSIsSelect)
832 if (isa<FPMathOperator>(&
I)) {
833 FMF =
I.getFastMathFlags();
840 Value *
Cond, *True =
nullptr, *False =
nullptr;
841 if (LHSIsSelect && RHSIsSelect && A ==
D) {
849 True =
Builder.CreateBinOp(Opcode,
B,
E);
850 else if (True && !False)
851 False =
Builder.CreateBinOp(Opcode,
C,
F);
853 }
else if (LHSIsSelect && LHS->
hasOneUse()) {
858 }
else if (RHSIsSelect && RHS->
hasOneUse()) {
875 void InstCombinerImpl::freelyInvertAllUsersOf(
Value *
I) {
876 for (
User *U :
I->users()) {
877 switch (cast<Instruction>(U)->
getOpcode()) {
879 auto *
SI = cast<SelectInst>(U);
881 SI->swapProfMetadata();
884 case Instruction::Br:
885 cast<BranchInst>(U)->swapSuccessors();
887 case Instruction::Xor:
888 replaceInstUsesWith(cast<Instruction>(*U),
I);
892 "canFreelyInvertAllUsersOf() ?");
899 Value *InstCombinerImpl::dyn_castNegVal(
Value *V)
const {
909 if (
C->getType()->getElementType()->isIntegerTy())
913 for (
unsigned i = 0,
e = CV->getNumOperands();
i !=
e; ++
i) {
918 if (isa<UndefValue>(Elt))
921 if (!isa<ConstantInt>(Elt))
932 if (
auto *Cast = dyn_cast<CastInst>(&
I))
933 return Builder.CreateCast(Cast->getOpcode(), SO,
I.getType());
935 assert(
I.isBinaryOp() &&
"Unexpected opcode for select folding");
938 bool ConstIsRHS = isa<Constant>(
I.getOperand(1));
939 Constant *ConstOperand = cast<Constant>(
I.getOperand(ConstIsRHS));
941 if (
auto *SOC = dyn_cast<Constant>(SO)) {
947 Value *Op0 = SO, *Op1 = ConstOperand;
951 auto *BO = cast<BinaryOperator>(&
I);
952 Value *RI =
Builder.CreateBinOp(BO->getOpcode(), Op0, Op1,
954 auto *FPInst = dyn_cast<Instruction>(RI);
955 if (FPInst && isa<FPMathOperator>(FPInst))
956 FPInst->copyFastMathFlags(BO);
963 if (!
SI->hasOneUse())
966 Value *TV =
SI->getTrueValue();
967 Value *FV =
SI->getFalseValue();
968 if (!(isa<Constant>(TV) || isa<Constant>(FV)))
972 if (
SI->getType()->isIntOrIntVectorTy(1))
977 if (
auto *BC = dyn_cast<BitCastInst>(&
Op)) {
978 VectorType *DestTy = dyn_cast<VectorType>(BC->getDestTy());
979 VectorType *SrcTy = dyn_cast<VectorType>(BC->getSrcTy());
982 if ((SrcTy ==
nullptr) != (DestTy ==
nullptr))
997 if (
auto *CI = dyn_cast<CmpInst>(
SI->getCondition())) {
998 if (CI->hasOneUse()) {
999 Value *Op0 = CI->getOperand(0), *Op1 = CI->getOperand(1);
1014 if (!A->getType()->isIntOrIntVectorTy() || A->getType() !=
B->getType())
1023 if ((areLooselyEqual(TV, Op0) && areLooselyEqual(FV, Op1)) ||
1024 (areLooselyEqual(FV, Op0) && areLooselyEqual(TV, Op1)))
1036 bool ConstIsRHS = isa<Constant>(
I->getOperand(1));
1037 Constant *
C = cast<Constant>(
I->getOperand(ConstIsRHS));
1039 if (
auto *InC = dyn_cast<Constant>(InV)) {
1045 Value *Op0 = InV, *Op1 =
C;
1049 Value *RI =
Builder.CreateBinOp(
I->getOpcode(), Op0, Op1,
"phi.bo");
1050 auto *FPInst = dyn_cast<Instruction>(RI);
1051 if (FPInst && isa<FPMathOperator>(FPInst))
1052 FPInst->copyFastMathFlags(
I);
1058 if (NumPHIValues == 0)
1068 if (UI != &
I && !
I.isIdenticalTo(UI))
1080 for (
unsigned i = 0;
i != NumPHIValues; ++
i) {
1087 if (isa<PHINode>(InVal))
return nullptr;
1088 if (NonConstBB)
return nullptr;
1094 if (isa<InvokeInst>(InVal))
1095 if (cast<Instruction>(InVal)->
getParent() == NonConstBB)
1110 if (NonConstBB !=
nullptr) {
1112 if (!BI || !BI->
isUnconditional() || !DT.isReachableFromEntry(NonConstBB))
1118 InsertNewInstBefore(NewPN, *PN);
1130 Value *TrueV =
SI->getTrueValue();
1131 Value *FalseV =
SI->getFalseValue();
1133 for (
unsigned i = 0;
i != NumPHIValues; ++
i) {
1137 Value *InV =
nullptr;
1145 if (InC && isa<ConstantInt>(InC))
1146 InV = InC->
isNullValue() ? FalseVInPred : TrueVInPred;
1158 FalseVInPred,
"phi.sel");
1162 }
else if (
CmpInst *CI = dyn_cast<CmpInst>(&
I)) {
1163 Constant *
C = cast<Constant>(
I.getOperand(1));
1164 for (
unsigned i = 0;
i != NumPHIValues; ++
i) {
1165 Value *InV =
nullptr;
1173 }
else if (
auto *BO = dyn_cast<BinaryOperator>(&
I)) {
1174 for (
unsigned i = 0;
i != NumPHIValues; ++
i) {
1179 }
else if (isa<FreezeInst>(&
I)) {
1180 for (
unsigned i = 0;
i != NumPHIValues; ++
i) {
1191 for (
unsigned i = 0;
i != NumPHIValues; ++
i) {
1197 I.getType(),
"phi.cast");
1204 if (
User == &
I)
continue;
1205 replaceInstUsesWith(*
User, NewPN);
1206 eraseInstFromFunction(*
User);
1208 return replaceInstUsesWith(
I, NewPN);
1212 if (!isa<Constant>(
I.getOperand(1)))
1215 if (
auto *Sel = dyn_cast<SelectInst>(
I.getOperand(0))) {
1218 }
else if (
auto *PN = dyn_cast<PHINode>(
I.getOperand(0))) {
1239 Type *IndexTy =
DL.getIndexType(PtrTy);
1240 int64_t FirstIdx = 0;
1241 if (int64_t TySize =
DL.getTypeAllocSize(Ty)) {
1242 FirstIdx =
Offset/TySize;
1243 Offset -= FirstIdx*TySize;
1251 assert((uint64_t)
Offset < (uint64_t)TySize &&
"Out of range offset");
1259 if (uint64_t(
Offset * 8) >=
DL.getTypeSizeInBits(Ty))
1262 if (
StructType *STy = dyn_cast<StructType>(Ty)) {
1265 "Offset must stay within the indexed type");
1272 Ty = STy->getElementType(Elt);
1273 }
else if (
ArrayType *AT = dyn_cast<ArrayType>(Ty)) {
1274 uint64_t EltSize =
DL.getTypeAllocSize(AT->getElementType());
1275 assert(EltSize &&
"Cannot index into a zero-sized array");
1278 Ty = AT->getElementType();
1301 assert(isa<IntegerType>(Val->
getType()) &&
"Can only descale integers!");
1303 Scale.
getBitWidth() &&
"Scale not compatible with value!");
1307 NoSignedWrap =
true;
1339 std::pair<Instruction *, unsigned> Parent;
1343 bool RequireNoSignedWrap =
false;
1348 for (;;
Op = Parent.first->getOperand(Parent.second)) {
1351 APInt Quotient(Scale), Remainder(Scale);
1358 NoSignedWrap =
true;
1363 if (BO->getOpcode() == Instruction::Mul) {
1365 NoSignedWrap = BO->hasNoSignedWrap();
1366 if (RequireNoSignedWrap && !NoSignedWrap)
1372 Value *LHS = BO->getOperand(0);
1373 Value *RHS = BO->getOperand(1);
1375 if (
ConstantInt *CI = dyn_cast<ConstantInt>(RHS)) {
1377 if (CI->getValue() == Scale) {
1385 if (!
Op->hasOneUse())
1388 Parent = std::make_pair(BO, 1);
1394 if (!
Op->hasOneUse())
1397 Parent = std::make_pair(BO, 0);
1401 if (logScale > 0 && BO->getOpcode() == Instruction::Shl &&
1402 isa<ConstantInt>(BO->getOperand(1))) {
1404 NoSignedWrap = BO->hasNoSignedWrap();
1405 if (RequireNoSignedWrap && !NoSignedWrap)
1408 Value *LHS = BO->getOperand(0);
1409 int32_t Amt = cast<ConstantInt>(BO->getOperand(1))->
1413 if (Amt == logScale) {
1424 Parent = std::make_pair(BO, 1);
1430 if (!
Op->hasOneUse())
1433 if (
CastInst *Cast = dyn_cast<CastInst>(
Op)) {
1434 if (Cast->getOpcode() == Instruction::SExt) {
1436 unsigned SmallSize = Cast->getSrcTy()->getPrimitiveSizeInBits();
1448 RequireNoSignedWrap =
true;
1451 Parent = std::make_pair(Cast, 0);
1456 if (Cast->getOpcode() == Instruction::Trunc) {
1463 if (RequireNoSignedWrap)
1467 unsigned LargeSize = Cast->getSrcTy()->getPrimitiveSizeInBits();
1468 Parent = std::make_pair(Cast, 0);
1469 Scale = Scale.
sext(LargeSize);
1470 if (logScale + 1 == (int32_t)Cast->getType()->getPrimitiveSizeInBits())
1483 NoSignedWrap =
true;
1497 assert(Parent.first->hasOneUse() &&
"Drilled down when more than one use!");
1498 assert(
Op != Parent.first->getOperand(Parent.second) &&
1499 "Descaling was a no-op?");
1500 replaceOperand(*Parent.first, Parent.second,
Op);
1501 Worklist.push(Parent.first);
1516 NoSignedWrap &= OpNoSignedWrap;
1517 if (NoSignedWrap != OpNoSignedWrap) {
1518 BO->setHasNoSignedWrap(NoSignedWrap);
1519 Worklist.push(Ancestor);
1521 }
else if (Ancestor->
getOpcode() == Instruction::Trunc) {
1525 NoSignedWrap =
false;
1527 assert((Ancestor->
getOpcode() != Instruction::SExt || NoSignedWrap) &&
1528 "Failed to keep proper track of nsw flags while drilling down?");
1530 if (Ancestor == Val)
1535 assert(Ancestor->
hasOneUse() &&
"Drilled down when more than one use!");
1541 if (!isa<VectorType>(Inst.
getType()))
1546 assert(cast<VectorType>(LHS->
getType())->getElementCount() ==
1547 cast<VectorType>(Inst.
getType())->getElementCount());
1548 assert(cast<VectorType>(RHS->getType())->getElementCount() ==
1549 cast<VectorType>(Inst.
getType())->getElementCount());
1554 Value *L0, *L1, *R0, *R1;
1559 cast<ShuffleVectorInst>(LHS)->isConcat() &&
1560 cast<ShuffleVectorInst>(RHS)->isConcat()) {
1567 if (
auto *BO = dyn_cast<BinaryOperator>(NewBO0))
1568 BO->copyIRFlags(&Inst);
1570 if (
auto *BO = dyn_cast<BinaryOperator>(NewBO1))
1571 BO->copyIRFlags(&Inst);
1583 if (
auto *BO = dyn_cast<BinaryOperator>(XY))
1584 BO->copyIRFlags(&Inst);
1594 (LHS->
hasOneUse() || RHS->hasOneUse() || LHS == RHS)) {
1596 return createBinOpShuffle(V1,
V2,
Mask);
1605 auto *LShuf = cast<ShuffleVectorInst>(LHS);
1606 auto *RShuf = cast<ShuffleVectorInst>(RHS);
1611 if (LShuf->isSelect() &&
1613 RShuf->isSelect() &&
1631 auto *InstVTy = dyn_cast<FixedVectorType>(Inst.
getType());
1636 cast<FixedVectorType>(V1->
getType())->getNumElements() <=
1637 InstVTy->getNumElements()) {
1639 "Shuffle should not change scalar type");
1646 bool ConstOp1 = isa<Constant>(RHS);
1648 unsigned SrcVecNumElts =
1649 cast<FixedVectorType>(V1->
getType())->getNumElements();
1652 bool MayChange =
true;
1653 unsigned NumElts = InstVTy->getNumElements();
1654 for (
unsigned I = 0;
I < NumElts; ++
I) {
1656 if (ShMask[
I] >= 0) {
1657 assert(ShMask[
I] < (
int)NumElts &&
"Not expecting narrowing shuffle");
1665 if (!CElt || (!isa<UndefValue>(NewCElt) && NewCElt != CElt) ||
1666 I >= SrcVecNumElts) {
1670 NewVecC[ShMask[
I]] = CElt;
1681 if (
I >= SrcVecNumElts || ShMask[
I] < 0) {
1685 if (!isa<UndefValue>(MaybeUndef)) {
1697 NewC = getSafeVectorConstantForBinop(Opcode, NewC, ConstOp1);
1701 Value *NewLHS = ConstOp1 ? V1 : NewC;
1702 Value *NewRHS = ConstOp1 ? NewC : V1;
1703 return createBinOpShuffle(NewLHS, NewRHS,
Mask);
1710 if (isa<ShuffleVectorInst>(RHS))
1743 Value *NewSplat =
Builder.CreateShuffleVector(NewBO, NewMask);
1748 if (isa<FPMathOperator>(R)) {
1749 R->copyFastMathFlags(&Inst);
1752 if (
auto *NewInstBO = dyn_cast<BinaryOperator>(NewBO))
1753 NewInstBO->copyIRFlags(R);
1782 cast<Operator>(Op1)->getOpcode() == CastOpc &&
1783 (Op0->
hasOneUse() || Op1->hasOneUse()))) {
1807 if (
auto *NewBinOp = dyn_cast<BinaryOperator>(NarrowBO)) {
1809 NewBinOp->setHasNoSignedWrap();
1811 NewBinOp->setHasNoUnsignedWrap();
1829 if (!
GEP.hasAllConstantIndices())
1844 bool IsInBounds =
GEP.isInBounds();
1845 Type *Ty =
GEP.getSourceElementType();
1846 Value *NewTrueC = IsInBounds ?
Builder.CreateInBoundsGEP(Ty, TrueC, IndexC)
1847 :
Builder.CreateGEP(Ty, TrueC, IndexC);
1848 Value *NewFalseC = IsInBounds ?
Builder.CreateInBoundsGEP(Ty, FalseC, IndexC)
1849 :
Builder.CreateGEP(Ty, FalseC, IndexC);
1855 Type *GEPType =
GEP.getType();
1856 Type *GEPEltType =
GEP.getSourceElementType();
1857 bool IsGEPSrcEleScalable = isa<ScalableVectorType>(GEPEltType);
1859 return replaceInstUsesWith(
GEP, V);
1864 if (
auto *GEPFVTy = dyn_cast<FixedVectorType>(GEPType)) {
1865 auto VWidth = GEPFVTy->getNumElements();
1866 APInt UndefElts(VWidth, 0);
1868 if (
Value *V = SimplifyDemandedVectorElts(&
GEP, AllOnesEltMask,
1871 return replaceInstUsesWith(
GEP, V);
1884 bool MadeChange =
false;
1888 Type *NewScalarIndexTy =
1889 DL.getIndexType(
GEP.getPointerOperandType()->getScalarType());
1898 Type *IndexTy = (*I)->getType();
1899 Type *NewIndexType =
1902 cast<VectorType>(IndexTy)->getElementCount())
1908 if (EltTy->
isSized() &&
DL.getTypeAllocSize(EltTy).isZero())
1914 if (IndexTy != NewIndexType) {
1918 *
I =
Builder.CreateIntCast(*
I, NewIndexType,
true);
1926 if (
auto *PN = dyn_cast<PHINode>(PtrOp)) {
1927 auto *Op1 = dyn_cast<GetElementPtrInst>(PN->getOperand(0));
1942 for (
auto I = PN->op_begin()+1,
E = PN->op_end();
I !=
E; ++
I) {
1943 auto *Op2 = dyn_cast<GetElementPtrInst>(*
I);
1944 if (!Op2 || Op1->getNumOperands() != Op2->getNumOperands())
1952 Type *CurTy =
nullptr;
1954 for (
unsigned J = 0,
F = Op1->getNumOperands(); J !=
F; ++J) {
1955 if (Op1->getOperand(J)->getType() != Op2->getOperand(J)->getType())
1958 if (Op1->getOperand(J) != Op2->getOperand(J)) {
1967 assert(CurTy &&
"No current type?");
1987 CurTy = Op1->getSourceElementType();
1999 if (DI != -1 && !PN->hasOneUse())
2002 auto *NewGEP = cast<GetElementPtrInst>(Op1->clone());
2014 NewPN =
Builder.CreatePHI(Op1->getOperand(DI)->getType(),
2015 PN->getNumOperands());
2018 for (
auto &
I : PN->operands())
2019 NewPN->
addIncoming(cast<GEPOperator>(
I)->getOperand(DI),
2020 PN->getIncomingBlock(
I));
2022 NewGEP->setOperand(DI, NewPN);
2025 GEP.getParent()->getInstList().insert(
2026 GEP.getParent()->getFirstInsertionPt(), NewGEP);
2027 replaceOperand(
GEP, 0, NewGEP);
2034 if (
auto *Src = dyn_cast<GEPOperator>(PtrOp)) {
2039 if (LI && Src->getNumOperands() == 2 &&
GEP.getNumOperands() == 2 &&
2041 if (
Loop *L = LI->getLoopFor(
GEP.getParent())) {
2043 Value *SO1 = Src->getOperand(1);
2047 if (L->isLoopInvariant(GO1) && !L->isLoopInvariant(SO1)) {
2065 auto *SO0 = Src->getOperand(0);
2067 if (!isa<VectorType>(GEPType) ||
2068 isa<VectorType>(SO0Ty)) {
2069 Src->setOperand(1, GO1);
2070 GEP.setOperand(1, SO1);
2076 Builder.SetInsertPoint(cast<Instruction>(PtrOp));
2077 auto *NewSrc = cast<GetElementPtrInst>(
2079 NewSrc->setIsInBounds(Src->isInBounds());
2091 if (
auto *SrcGEP = dyn_cast<GEPOperator>(Src->getOperand(0)))
2098 bool EndsWithSequential =
false;
2101 EndsWithSequential =
I.isSequential();
2104 if (EndsWithSequential) {
2107 Value *SO1 = Src->getOperand(Src->getNumOperands()-1);
2125 if (Src->getNumOperands() == 2) {
2127 replaceOperand(
GEP, 0, Src->getOperand(0));
2128 replaceOperand(
GEP, 1, Sum);
2131 Indices.
append(Src->op_begin()+1, Src->op_end()-1);
2132 Indices.push_back(Sum);
2134 }
else if (isa<Constant>(*
GEP.idx_begin()) &&
2135 cast<Constant>(*
GEP.idx_begin())->isNullValue() &&
2136 Src->getNumOperands() != 1) {
2138 Indices.
append(Src->op_begin()+1, Src->op_end());
2142 if (!Indices.empty())
2145 Src->getSourceElementType(), Src->getOperand(0), Indices,
2148 Src->getOperand(0), Indices,
2154 if (
GEP.getNumIndices() == 1 && !IsGEPSrcEleScalable) {
2155 unsigned AS =
GEP.getPointerAddressSpace();
2156 if (
GEP.getOperand(1)->getType()->getScalarSizeInBits() ==
2157 DL.getIndexSizeInBits(AS)) {
2158 uint64_t TyAllocSize =
DL.getTypeAllocSize(GEPEltType).getFixedSize();
2160 bool Matched =
false;
2163 if (TyAllocSize == 1) {
2164 V =
GEP.getOperand(1);
2166 }
else if (
match(
GEP.getOperand(1),
2168 if (TyAllocSize == 1ULL <<
C)
2170 }
else if (
match(
GEP.getOperand(1),
2172 if (TyAllocSize ==
C)
2196 if (StrippedPtr != PtrOp) {
2197 bool HasZeroPointerIndex =
false;
2200 if (
auto *
C = dyn_cast<ConstantInt>(
GEP.getOperand(1)))
2201 HasZeroPointerIndex =
C->isZero();
2210 if (HasZeroPointerIndex) {
2211 if (
auto *CATy = dyn_cast<ArrayType>(GEPEltType)) {
2213 if (CATy->getElementType() == StrippedPtrEltTy) {
2217 StrippedPtrEltTy, StrippedPtr, Idx,
GEP.getName());
2230 if (
auto *XATy = dyn_cast<ArrayType>(StrippedPtrEltTy)) {
2232 if (CATy->getElementType() == XATy->getElementType()) {
2239 GEP.setSourceElementType(XATy);
2240 return replaceOperand(
GEP, 0, StrippedPtr);
2254 ?
Builder.CreateInBoundsGEP(StrippedPtrEltTy, StrippedPtr,
2256 :
Builder.CreateGEP(StrippedPtrEltTy, StrippedPtr, Idx,
2262 }
else if (
GEP.getNumOperands() == 2 && !IsGEPSrcEleScalable) {
2270 DL.getTypeAllocSize(GEPEltType)) {
2271 Type *IdxType =
DL.getIndexType(GEPType);
2275 ?
Builder.CreateInBoundsGEP(StrippedPtrEltTy, StrippedPtr, Idx,
2277 :
Builder.CreateGEP(StrippedPtrEltTy, StrippedPtr, Idx,
2291 uint64_t ResSize =
DL.getTypeAllocSize(GEPEltType).getFixedSize();
2292 uint64_t SrcSize =
DL.getTypeAllocSize(StrippedPtrEltTy).getFixedSize();
2293 if (ResSize && SrcSize % ResSize == 0) {
2296 uint64_t Scale = SrcSize / ResSize;
2302 "Index type does not match the Data Layout preferences");
2310 GEP.isInBounds() && NSW
2311 ?
Builder.CreateInBoundsGEP(StrippedPtrEltTy, StrippedPtr,
2312 NewIdx,
GEP.getName())
2313 :
Builder.CreateGEP(StrippedPtrEltTy, StrippedPtr, NewIdx,
2331 uint64_t ResSize =
DL.getTypeAllocSize(GEPEltType).getFixedSize();
2332 uint64_t ArrayEltSize =
2335 if (ResSize && ArrayEltSize % ResSize == 0) {
2338 uint64_t Scale = ArrayEltSize / ResSize;
2344 "Index type does not match the Data Layout preferences");
2351 Type *IndTy =
DL.getIndexType(GEPType);
2355 GEP.isInBounds() && NSW
2356 ?
Builder.CreateInBoundsGEP(StrippedPtrEltTy, StrippedPtr,
2358 :
Builder.CreateGEP(StrippedPtrEltTy, StrippedPtr, Off,
2372 Value *ASCStrippedPtrOp = PtrOp;
2373 if (
auto *ASC = dyn_cast<AddrSpaceCastInst>(PtrOp)) {
2378 if (
auto *BC = dyn_cast<BitCastInst>(ASC->getOperand(0)))
2379 ASCStrippedPtrOp = BC;
2382 if (
auto *BCI = dyn_cast<BitCastInst>(ASCStrippedPtrOp)) {
2384 PointerType *SrcType = cast<PointerType>(BCI->getSrcTy());
2391 auto areMatchingArrayAndVecTypes = [](
Type *ArrTy,
Type *VecTy,
2393 auto *VecVTy = cast<FixedVectorType>(VecTy);
2396 DL.getTypeAllocSize(ArrTy) ==
DL.getTypeAllocSize(VecTy);
2398 if (
GEP.getNumOperands() == 3 &&
2399 ((GEPEltType->
isArrayTy() && isa<FixedVectorType>(SrcEltType) &&
2400 areMatchingArrayAndVecTypes(GEPEltType, SrcEltType,
DL)) ||
2401 (isa<FixedVectorType>(GEPEltType) && SrcEltType->
isArrayTy() &&
2402 areMatchingArrayAndVecTypes(SrcEltType, GEPEltType,
DL)))) {
2410 ?
Builder.CreateInBoundsGEP(SrcEltType,
SrcOp, {Ops[1], Ops[2]})
2411 :
Builder.CreateGEP(SrcEltType,
SrcOp, {Ops[1], Ops[2]});
2418 return replaceInstUsesWith(
GEP, NGEP);
2426 unsigned OffsetBits =
DL.getIndexTypeSizeInBits(GEPType);
2435 if (!isa<BitCastInst>(
SrcOp) &&
GEP.accumulateConstantOffset(
DL,
Offset) &&
2442 if (isa<AllocaInst>(
SrcOp)) {
2447 BCI->getParent()->getInstList().insert(BCI->getIterator(),
I);
2448 replaceInstUsesWith(*BCI,
I);
2463 if (FindElementAtOffset(SrcType,
Offset.getSExtValue(), NewIndices)) {
2466 ?
Builder.CreateInBoundsGEP(SrcEltType,
SrcOp, NewIndices)
2469 if (NGEP->
getType() == GEPType)
2470 return replaceInstUsesWith(
GEP, NGEP);
2480 if (!
GEP.isInBounds()) {
2483 APInt BasePtrOffset(IdxWidth, 0);
2484 Value *UnderlyingPtrOp =
2487 if (
auto *AI = dyn_cast<AllocaInst>(UnderlyingPtrOp)) {
2488 if (
GEP.accumulateConstantOffset(
DL, BasePtrOffset) &&
2492 DL.getTypeAllocSize(AI->getAllocatedType()).getKnownMinSize());
2493 if (BasePtrOffset.
ule(AllocSize)) {
2510 if (isa<ConstantPointerNull>(V))
2512 if (
auto *LI = dyn_cast<LoadInst>(V))
2513 return isa<GlobalVariable>(LI->getPointerOperand());
2526 Worklist.push_back(AI);
2532 switch (
I->getOpcode()) {
2537 case Instruction::AddrSpaceCast:
2538 case Instruction::BitCast:
2539 case Instruction::GetElementPtr:
2541 Worklist.push_back(
I);
2544 case Instruction::ICmp: {
2551 unsigned OtherIndex = (ICI->
getOperand(0) == PI) ? 1 : 0;
2565 case Intrinsic::memmove:
2567 case Intrinsic::memset: {
2569 if (
MI->isVolatile() ||
MI->getRawDest() != PI)
2573 case Intrinsic::assume:
2574 case Intrinsic::invariant_start:
2575 case Intrinsic::invariant_end:
2576 case Intrinsic::lifetime_start:
2577 case Intrinsic::lifetime_end:
2578 case Intrinsic::objectsize:
2592 if (
SI->isVolatile() ||
SI->getPointerOperand() != PI)
2600 }
while (!Worklist.empty());
2620 std::unique_ptr<DIBuilder> DIB;
2621 if (isa<AllocaInst>(
MI)) {
2627 for (
unsigned i = 0,
e =
Users.size();
i !=
e; ++
i) {
2639 replaceInstUsesWith(*
I, Result);
2640 eraseInstFromFunction(*
I);
2645 for (
unsigned i = 0,
e =
Users.size();
i !=
e; ++
i) {
2652 replaceInstUsesWith(*
C,
2654 C->isFalseWhenEqual()));
2655 }
else if (
auto *
SI = dyn_cast<StoreInst>(
I)) {
2656 for (
auto *DVI : DVIs)
2657 if (DVI->isAddressOfVariable())
2664 eraseInstFromFunction(*
I);
2697 for (
auto *DVI : DVIs)
2698 if (DVI->isAddressOfVariable() || DVI->getExpression()->startsWithDeref())
2699 DVI->eraseFromParent();
2701 return eraseInstFromFunction(
MI);
2745 if (FreeInstrBB->
size() != 2) {
2747 if (&Inst == &FI || &Inst == FreeInstrBBTerminator)
2749 auto *Cast = dyn_cast<CastInst>(&Inst);
2750 if (!Cast || !Cast->isNoopCast(
DL))
2771 "Broken CFG: missing edge from predecessor to successor");
2778 if (&Instr == FreeInstrBBTerminator)
2783 "Only the branch instruction should remain");
2791 if (isa<UndefValue>(
Op)) {
2793 CreateNonTerminatorUnreachable(&FI);
2794 return eraseInstFromFunction(FI);
2799 if (isa<ConstantPointerNull>(
Op))
2800 return eraseInstFromFunction(FI);
2807 if (
auto *A = dyn_cast<Argument>(
Op->stripPointerCasts()))
2808 if (A->hasAttribute(Attribute::NoFree) ||
2812 CreateNonTerminatorUnreachable(&FI);
2813 return eraseInstFromFunction(FI);
2828 if (TLI.getLibFunc(FI, Func) && TLI.has(Func) && Func == LibFunc_free)
2837 if (
auto *CI = dyn_cast<CallInst>(V))
2838 return CI->isMustTailCall();
2848 if (!VTy->
isIntegerTy() || isa<Constant>(ResultOp))
2859 return replaceOperand(RI, 0,
2870 if (Prev && !Prev->
isEHPad() &&
2876 if (
auto *
SI = dyn_cast<StoreInst>(Prev))
2877 if (
SI->isVolatile())
2883 eraseInstFromFunction(*Prev);
2898 return isa<DbgInfoIntrinsic>(BBI) ||
2899 (isa<BitCastInst>(BBI) && BBI->getType()->isPointerTy());
2904 if (BBI != FirstInstr)
2906 }
while (BBI != FirstInstr && IsNoopInstrForStoreMerging(BBI));
2908 return dyn_cast<StoreInst>(BBI);
2912 if (mergeStoreIntoSuccessor(*
SI))
2920 return visitUnconditionalBranchInst(BI);
2925 !isa<Constant>(
X)) {
2928 return replaceOperand(BI, 0,
X);
2935 return replaceOperand(
2942 !isCanonicalPredicate(Pred)) {
2947 Worklist.push(
Cond);
2960 for (
auto Case :
SI.cases()) {
2962 assert(isa<ConstantInt>(NewCase) &&
2963 "Result of expression should be constant");
2964 Case.setValue(cast<ConstantInt>(NewCase));
2966 return replaceOperand(
SI, 0, Op0);
2975 for (
auto &
C :
SI.cases()) {
2977 LeadingKnownZeros,
C.getCaseValue()->getValue().countLeadingZeros());
2979 LeadingKnownOnes,
C.getCaseValue()->getValue().countLeadingOnes());
2988 if (NewWidth > 0 && NewWidth < Known.
getBitWidth() &&
2989 shouldChangeType(Known.
getBitWidth(), NewWidth)) {
2994 for (
auto Case :
SI.cases()) {
2995 APInt TruncatedCase = Case.getCaseValue()->getValue().
trunc(NewWidth);
2998 return replaceOperand(
SI, 0, NewCond);
3008 return replaceInstUsesWith(EV, Agg);
3011 SQ.getWithInstruction(&EV)))
3012 return replaceInstUsesWith(EV, V);
3016 const unsigned *exti, *exte, *insi, *inse;
3017 for (exti = EV.
idx_begin(), insi = IV->idx_begin(),
3018 exte = EV.
idx_end(), inse = IV->idx_end();
3019 exti != exte && insi != inse;
3033 if (exti == exte && insi == inse)
3038 return replaceInstUsesWith(EV, IV->getInsertedValueOperand());
3048 Value *NewEV =
Builder.CreateExtractValue(IV->getAggregateOperand(),
3069 if (WO->hasOneUse()) {
3074 Value *LHS = WO->getLHS(), *RHS = WO->getRHS();
3076 eraseInstFromFunction(*WO);
3083 if (WO->getIntrinsicID() == Intrinsic::uadd_with_overflow)
3084 if (
ConstantInt *CI = dyn_cast<ConstantInt>(WO->getRHS()))
3089 if (
LoadInst *L = dyn_cast<LoadInst>(Agg))
3095 if (L->isSimple() && L->hasOneUse()) {
3099 Indices.push_back(
Builder.getInt32(0));
3100 for (
unsigned Idx : EV.
indices())
3101 Indices.push_back(
Builder.getInt32(Idx));
3107 L->getPointerOperand(), Indices);
3112 L->getAAMetadata(Nodes);
3113 NL->setAAMetadata(Nodes);
3116 return replaceInstUsesWith(EV,
NL);
3131 switch (Personality) {
3160 cast<ArrayType>(LHS->
getType())->getNumElements()
3162 cast<ArrayType>(RHS->
getType())->getNumElements();
3174 bool MakeNewInstruction =
false;
3180 bool isLastClause =
i + 1 ==
e;
3188 if (AlreadyCaught.
insert(TypeInfo).second) {
3190 NewClauses.push_back(CatchClause);
3193 MakeNewInstruction =
true;
3200 MakeNewInstruction =
true;
3201 CleanupFlag =
false;
3220 if (!NumTypeInfos) {
3221 NewClauses.push_back(FilterClause);
3223 MakeNewInstruction =
true;
3224 CleanupFlag =
false;
3228 bool MakeNewFilter =
false;
3230 if (isa<ConstantAggregateZero>(FilterClause)) {
3232 assert(NumTypeInfos > 0 &&
"Should have handled empty filter already!");
3238 MakeNewInstruction =
true;
3244 NewFilterElts.push_back(TypeInfo);
3245 if (NumTypeInfos > 1)
3246 MakeNewFilter =
true;
3250 NewFilterElts.
reserve(NumTypeInfos);
3255 bool SawCatchAll =
false;
3256 for (
unsigned j = 0;
j != NumTypeInfos; ++
j) {
3284 if (SeenInFilter.
insert(TypeInfo).second)
3285 NewFilterElts.push_back(cast<Constant>(Elt));
3290 MakeNewInstruction =
true;
3295 if (NewFilterElts.size() < NumTypeInfos)
3296 MakeNewFilter =
true;
3298 if (MakeNewFilter) {
3300 NewFilterElts.size());
3302 MakeNewInstruction =
true;
3305 NewClauses.push_back(FilterClause);
3311 if (MakeNewFilter && !NewFilterElts.size()) {
3312 assert(MakeNewInstruction &&
"New filter but not a new instruction!");
3313 CleanupFlag =
false;
3324 for (
unsigned i = 0,
e = NewClauses.size();
i + 1 <
e; ) {
3327 for (
j =
i;
j !=
e; ++
j)
3328 if (!isa<ArrayType>(NewClauses[
j]->
getType()))
3334 for (
unsigned k =
i; k + 1 <
j; ++k)
3340 MakeNewInstruction =
true;
3359 for (
unsigned i = 0;
i + 1 < NewClauses.size(); ++
i) {
3369 for (
unsigned j = NewClauses.size() - 1;
j !=
i; --
j) {
3370 Value *LFilter = NewClauses[
j];
3381 NewClauses.
erase(J);
3382 MakeNewInstruction =
true;
3392 if (isa<ConstantAggregateZero>(LFilter)) {
3395 if (isa<ConstantAggregateZero>(
Filter)) {
3396 assert(FElts <= LElts &&
"Should have handled this case earlier!");
3398 NewClauses.
erase(J);
3399 MakeNewInstruction =
true;
3405 if (isa<ConstantAggregateZero>(
Filter)) {
3408 assert(FElts > 0 &&
"Should have eliminated the empty filter earlier!");
3409 for (
unsigned l = 0;
l != LElts; ++
l)
3412 NewClauses.
erase(J);
3413 MakeNewInstruction =
true;
3424 bool AllFound =
true;
3425 for (
unsigned f = 0;
f != FElts; ++
f) {
3428 for (
unsigned l = 0;
l != LElts; ++
l) {
3430 if (LTypeInfo == FTypeInfo) {
3440 NewClauses.
erase(J);
3441 MakeNewInstruction =
true;
3449 if (MakeNewInstruction) {
3452 for (
unsigned i = 0,
e = NewClauses.size();
i !=
e; ++
i)
3457 if (NewClauses.empty())
3466 assert(!CleanupFlag &&
"Adding a cleanup, not removing one?!");
3475 Value *Op0 =
I.getOperand(0);
3478 return replaceInstUsesWith(
I, V);
3481 if (
auto *PN = dyn_cast<PHINode>(Op0)) {
3493 for (
const auto *U :
I.users()) {
3498 else if (
const auto *
SI = dyn_cast<SelectInst>(U)) {
3499 if (
SI->getCondition() == &
I) {
3500 APInt CondVal(1, isa<Constant>(
SI->getFalseValue()) ? 0 : 1);
3507 else if (BestValue !=
C)
3508 BestValue = NullValue;
3511 return replaceInstUsesWith(
I, BestValue);
3522 assert(
I->getSingleUndroppableUse() &&
"Invariants didn't hold!");
3526 if (isa<PHINode>(
I) ||
I->isEHPad() ||
I->mayHaveSideEffects() ||
3534 if (isa<AllocaInst>(
I))
3542 if (
auto *CI = dyn_cast<CallInst>(
I)) {
3543 if (CI->isConvergent())
3548 if (
I->mayReadFromMemory()) {
3555 E =
I->getParent()->end();
3557 if (Scan->mayWriteToMemory())
3561 I->dropDroppableUses([DestBlock](
const Use *U) {
3562 if (
auto *
I = dyn_cast<Instruction>(U->getUser()))
3563 return I->getParent() != DestBlock;
3570 I->moveBefore(&*InsertPos);
3584 if (DVI->getParent() == SrcBlock)
3585 DbgUsersToSink.push_back(DVI);
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)
3606 DIIClones.emplace_back(cast<DbgVariableIntrinsic>(
User->clone()));
3607 if (isa<DbgDeclareInst>(
User) && isa<CastInst>(
I))
3608 DIIClones.back()->replaceVariableLocationOp(
I,
I->getOperand(0));
3613 if (!DIIClones.empty()) {
3618 DIIClone->insertBefore(&*InsertPos);
3627 while (!Worklist.isEmpty()) {
3636 eraseInstFromFunction(*
I);
3645 if (
I ==
nullptr)
continue;
3649 eraseInstFromFunction(*
I);
3658 if (!
I->use_empty() &&
3659 (
I->getNumOperands() == 0 || isa<Constant>(
I->getOperand(0)))) {
3665 replaceInstUsesWith(*
I,
C);
3668 eraseInstFromFunction(*
I);
3669 MadeIRChange =
true;
3677 if (
Use *SingleUse =
I->getSingleUndroppableUse()) {
3679 Instruction *UserInst = cast<Instruction>(SingleUse->getUser());
3683 if (
PHINode *PN = dyn_cast<PHINode>(UserInst))
3684 UserParent = PN->getIncomingBlock(*SingleUse);
3690 if (UserParent !=
BB && DT.isReachableFromEntry(UserParent)) {
3702 ShouldSink = isa<ReturnInst>(
Term) || isa<UnreachableInst>(
Term);
3705 assert(DT.dominates(
BB, UserParent) &&
3706 "Dominance relation broken?");
3710 MadeIRChange =
true;
3714 for (
Use &U :
I->operands())
3715 if (
Instruction *OpI = dyn_cast<Instruction>(U.get()))
3724 Builder.CollectMetadataToCopy(
3725 I, {LLVMContext::MD_dbg, LLVMContext::MD_annotation});
3738 <<
" New = " << *Result <<
'\n');
3740 Result->copyMetadata(*
I,
3741 {LLVMContext::MD_dbg, LLVMContext::MD_annotation});
3743 I->replaceAllUsesWith(Result);
3746 Result->takeName(
I);
3753 if (isa<PHINode>(Result) != isa<PHINode>(
I)) {
3755 if (isa<PHINode>(
I))
3761 InstParent->
getInstList().insert(InsertPos, Result);
3764 Worklist.pushUsersToWorkList(*Result);
3765 Worklist.push(Result);
3767 eraseInstFromFunction(*
I);
3770 <<
" New = " << *
I <<
'\n');
3775 eraseInstFromFunction(*
I);
3777 Worklist.pushUsersToWorkList(*
I);
3781 MadeIRChange =
true;
3786 return MadeIRChange;
3802 if (!
I->hasMetadataOtherThanDebugLoc())
3805 auto Track = [](
Metadata *ScopeList,
auto &Container) {
3806 const auto *MDScopeList = dyn_cast_or_null<MDNode>(ScopeList);
3807 if (!MDScopeList || !Container.insert(MDScopeList).second)
3809 for (
auto &
MDOperand : MDScopeList->operands())
3810 if (
auto *MDScope = dyn_cast<MDNode>(
MDOperand))
3811 Container.insert(MDScope);
3814 Track(
I->getMetadata(LLVMContext::MD_alias_scope), UsedAliasScopesAndLists);
3815 Track(
I->getMetadata(LLVMContext::MD_noalias), UsedNoAliasScopesAndLists);
3824 "llvm.experimental.noalias.scope.decl in use ?");
3827 "llvm.experimental.noalias.scope should refer to a single scope");
3829 if (
auto *MD = dyn_cast<MDNode>(
MDOperand))
3830 return !UsedAliasScopesAndLists.
contains(MD) ||
3831 !UsedNoAliasScopesAndLists.
contains(MD);
3849 bool MadeIRChange =
false;
3852 Worklist.push_back(&
F.front());
3878 MadeIRChange =
true;
3884 if (!isa<ConstantVector>(U) && !isa<ConstantExpr>(U))
3887 auto *
C = cast<Constant>(U);
3888 Constant *&FoldRes = FoldedConstants[
C];
3894 <<
"\n Old = " << *
C
3895 <<
"\n New = " << *FoldRes <<
'\n');
3897 MadeIRChange =
true;
3905 InstrsForInstCombineWorklist.push_back(Inst);
3906 SeenAliasScopes.
analyse(Inst);
3913 if (
BranchInst *BI = dyn_cast<BranchInst>(TI)) {
3914 if (BI->isConditional() && isa<ConstantInt>(BI->getCondition())) {
3915 bool CondVal = cast<ConstantInt>(BI->getCondition())->getZExtValue();
3916 BasicBlock *ReachableBB = BI->getSuccessor(!CondVal);
3917 Worklist.push_back(ReachableBB);
3920 }
else if (
SwitchInst *
SI = dyn_cast<SwitchInst>(TI)) {
3922 Worklist.push_back(
SI->findCaseValue(
Cond)->getCaseSuccessor());
3928 }
while (!Worklist.empty());
3937 unsigned NumDeadInstInBB;
3938 unsigned NumDeadDbgInstInBB;
3939 std::tie(NumDeadInstInBB, NumDeadDbgInstInBB) =
3942 MadeIRChange |= NumDeadInstInBB + NumDeadDbgInstInBB > 0;
3943 NumDeadInst += NumDeadInstInBB;
3951 ICWorklist.
reserve(InstrsForInstCombineWorklist.size());
3960 Inst->eraseFromParent();
3961 MadeIRChange =
true;
3965 ICWorklist.
push(Inst);
3968 return MadeIRChange;
3976 auto &
DL =
F.getParent()->getDataLayout();
3985 if (
auto *Assume = dyn_cast<AssumeInst>(
I))
3991 bool MadeIRChange =
false;
3996 unsigned Iteration = 0;
3998 ++NumWorklistIterations;
4003 "Instruction Combining seems stuck in an infinite loop after " +
4007 if (Iteration > MaxIterations) {
4008 LLVM_DEBUG(
dbgs() <<
"\n\n[IC] Iteration limit #" << MaxIterations
4009 <<
" on " <<
F.getName()
4010 <<
" reached; stopping before reaching a fixpoint\n");
4014 LLVM_DEBUG(
dbgs() <<
"\n\nINSTCOMBINE ITERATION #" << Iteration <<
" on "
4015 <<
F.getName() <<
"\n");
4020 ORE,
BFI, PSI,
DL, LI);
4026 MadeIRChange =
true;
4029 return MadeIRChange;
4035 : MaxIterations(MaxIterations) {}
4051 auto *
BFI = (PSI && PSI->hasProfileSummary()) ?
4055 BFI, PSI, MaxIterations, LI))
4089 auto AA = &getAnalysis<AAResultsWrapperPass>().getAAResults();
4090 auto &AC = getAnalysis<AssumptionCacheTracker>().getAssumptionCache(
F);
4091 auto &TLI = getAnalysis<TargetLibraryInfoWrapperPass>().getTLI(
F);
4092 auto &
TTI = getAnalysis<TargetTransformInfoWrapperPass>().getTTI(
F);
4093 auto &DT = getAnalysis<DominatorTreeWrapperPass>().getDomTree();
4094 auto &ORE = getAnalysis<OptimizationRemarkEmitterWrapperPass>().getORE();
4097 auto *LIWP = getAnalysisIfAvailable<LoopInfoWrapperPass>();
4098 auto *LI = LIWP ? &LIWP->getLoopInfo() :
nullptr;
4100 &getAnalysis<ProfileSummaryInfoWrapperPass>().getPSI();
4103 &getAnalysis<LazyBlockFrequencyInfoPass>().getBFI() :
4107 BFI, PSI, MaxIterations, LI);
4123 "Combine redundant instructions",
false,
false)
Analysis pass providing a never-invalidated alias analysis result.
A set of analyses that are preserved following a run of a transformation pass.
Analysis pass providing a never-invalidated alias analysis result.
An analysis over an "inner" IR unit that provides access to an analysis manager over a "outer" IR uni...
A manager for alias analyses.
static Instruction * foldSelectGEP(GetElementPtrInst &GEP, InstCombiner::BuilderTy &Builder)
Thread a GEP operation with constant indices through the constant true/false arms of a select.
bool isSized(SmallPtrSetImpl< Type * > *Visited=nullptr) const
Return true if it makes sense to take the size of this type.
Analysis pass providing the TargetTransformInfo.
static LandingPadInst * Create(Type *RetTy, unsigned NumReservedClauses, const Twine &NameStr="", Instruction *InsertBefore=nullptr)
Constructors - NumReservedClauses is a hint for the number of incoming clauses that this landingpad w...
void reserve(size_t Size)
bool isAssociative() const LLVM_READONLY
Return true if the instruction is associative:
void setIsInBounds(bool b=true)
Set or clear the inbounds flag on this GEP instruction.
Value * tryFactorization(BinaryOperator &, Instruction::BinaryOps, Value *, Value *, Value *, Value *)
This tries to simplify binary operations by factorizing out common terms (e.
APInt sadd_ov(const APInt &RHS, bool &Overflow) const
iterator erase(const_iterator CI)
bool isCleanup() const
Return 'true' if this landingpad instruction is a cleanup.
static InvokeInst * Create(FunctionType *Ty, Value *Func, BasicBlock *IfNormal, BasicBlock *IfException, ArrayRef< Value * > Args, const Twine &NameStr, Instruction *InsertBefore=nullptr)
static IntegerType * getInt1Ty(LLVMContext &C)
Instruction * visitGetElementPtrInst(GetElementPtrInst &GEP)
static Value * foldOperationIntoSelectOperand(Instruction &I, Value *SO, InstCombiner::BuilderTy &Builder)
static CastInst * CreatePointerBitCastOrAddrSpaceCast(Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd)
Create a BitCast or an AddrSpaceCast cast instruction.
We currently emits eax Perhaps this is what we really should generate is Is imull three or four cycles eax eax The current instruction priority is based on pattern complexity The former is more complex because it folds a load so the latter will not be emitted Perhaps we should use AddedComplexity to give LEA32r a higher priority We should always try to match LEA first since the LEA matching code does some estimate to determine whether the match is profitable if we care more about code then imull is better It s two bytes shorter than movl leal On a Pentium M
void ConvertDebugDeclareToDebugValue(DbgVariableIntrinsic *DII, StoreInst *SI, DIBuilder &Builder)
===------------------------------------------------------------------—===// Dbg Intrinsic utilities
const Module * getModule() const
Return the module owning the function this instruction belongs to or nullptr it the function does not...
auto drop_begin(T &&RangeOrContainer, size_t N=1)
Return a range covering RangeOrContainer with the first N elements excluded.
void registerAssumption(AssumeInst *CI)
Add an @llvm.assume intrinsic to this function's cache.
Return a value (possibly void), from a function.
static Constant * getNot(Constant *C)
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)".
A parsed version of the target data layout string in and methods for querying it.
static bool isBitwiseLogicOp(unsigned Opcode)
Determine if the Opcode is and/or/xor.
bool hasOneUse() const
Return true if there is exactly one use of this value.
Instruction * visitBranchInst(BranchInst &BI)
Function * getDeclaration(Module *M, ID id, ArrayRef< Type * > Tys=None)
Create or insert an LLVM Function declaration for an intrinsic, and return it.
Predicate
This enumeration lists the possible predicates for CmpInst subclasses.
InstListType::iterator iterator
Instruction iterators...
bool isSafeToSpeculativelyExecute(const Value *V, const Instruction *CtxI=nullptr, const DominatorTree *DT=nullptr, const TargetLibraryInfo *TLI=nullptr)
Return true if the instruction does not have any effects besides calculating the result and does not ...
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - This function should be overriden by passes that need analysis information to do t...
bool run()
Run the combiner over the entire worklist until it is empty.
const Function * getParent() const
Return the enclosing method, or null if none.
struct LLVMOpaquePassRegistry * LLVMPassRegistryRef
PassT::Result & getResult(IRUnitT &IR, ExtraArgTs... ExtraArgs)
Get the result of an analysis pass for a given IR unit.
Optional< Instruction * > targetInstCombineIntrinsic(IntrinsicInst &II)
Instruction * foldBinOpIntoSelectOrPhi(BinaryOperator &I)
This is a convenience wrapper function for the above two functions.
Type * getElementType() const
Represents a single loop in the control flow graph.
bool ule(const APInt &RHS) const
Unsigned less or equal comparison.
Intrinsic::ID getIntrinsicID() const
Return the intrinsic ID of this intrinsic.
A raw_ostream that writes to an std::string.
This class represents a no-op cast from one type to another.
Instruction * visitExtractValueInst(ExtractValueInst &EV)
instcombine should handle this C2 when C1
bool hasProfileSummary() const
Returns true if profile summary is available.
bool isGuaranteedNotToBeUndefOrPoison(const Value *V, AssumptionCache *AC=nullptr, const Instruction *CtxI=nullptr, const DominatorTree *DT=nullptr, unsigned Depth=0)
Return true if this function can prove that V does not have undef bits and is never poison.
Type * getScalarType() const
If this is a vector type, return the element type, otherwise return 'this'.
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
The landingpad instruction holds all of the information necessary to generate correct exception handl...
Optional< Value * > targetSimplifyDemandedVectorEltsIntrinsic(IntrinsicInst &II, APInt DemandedElts, APInt &UndefElts, APInt &UndefElts2, APInt &UndefElts3, std::function< void(Instruction *, unsigned, APInt, APInt &)> SimplifyAndSetOp)
BinaryOp_match< LHS, RHS, Instruction::Add > m_Add(const LHS &L, const RHS &R)
bool skipFunction(const Function &F) const
Optional passes call this function to check whether the pass should be skipped.
match_combine_or< LTy, RTy > m_CombineOr(const LTy &L, const RTy &R)
Combine two pattern matchers matching L || R.
unsigned getPointerAddressSpace() const
Get the address space of this pointer or pointer vector type.
static InsertValueInst * Create(Value *Agg, Value *Val, ArrayRef< unsigned > Idxs, const Twine &NameStr="", Instruction *InsertBefore=nullptr)
static Instruction::BinaryOps getBinOpsForFactorization(Instruction::BinaryOps TopOpcode, BinaryOperator *Op, Value *&LHS, Value *&RHS)
This function predicates factorization using distributive laws.
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 ...
unsigned getAddressSpace() const
Return the address space of the Pointer type.
Predicate getInversePredicate() const
For example, EQ -> NE, UGT -> ULE, SLT -> SGE, OEQ -> UNE, UGT -> OLE, OLT -> UGE,...
void findDbgUsers(SmallVectorImpl< DbgVariableIntrinsic * > &DbgInsts, Value *V)
Finds the debug info intrinsics describing a value.
bool isAllocLikeFn(const Value *V, const TargetLibraryInfo *TLI, bool LookThroughBitCast=false)
Tests if a value is a call or invoke to a library function that allocates memory (either malloc,...
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree.
bool hasFnAttr(Attribute::AttrKind Kind) const
Determine whether this call has the given attribute.
MDNode * getScopeList() const
brc_match< Cond_t, bind_ty< BasicBlock >, bind_ty< BasicBlock > > m_Br(const Cond_t &C, BasicBlock *&T, BasicBlock *&F)
uint64_t MaxArraySizeForCombine
Maximum size of array considered when transforming.
A collection of metadata nodes that might be associated with a memory access used by the alias-analys...
static void getLazyBFIAnalysisUsage(AnalysisUsage &AU)
Helper for client passes to set up the analysis usage on behalf of this pass.
InstructionCombiningPass()
The instances of the Type class are immutable: once they are created, they are never changed.
const APInt & getConstant() const
Returns the value when all bits have a known value.
unsigned getBitWidth() const
Return the number of bits in the APInt.
Instruction * FoldOpIntoSelect(Instruction &Op, SelectInst *SI)
Given an instruction with a select as one operand and a constant as the other operand,...
Instruction * visitAllocSite(Instruction &FI)
auto reverse(ContainerTy &&C, std::enable_if_t< has_rbegin< ContainerTy >::value > *=nullptr)
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)
SmallSet - This maintains a set of unique values, optimizing for the case when the set is small (less...
static GetElementPtrInst * CreateInBounds(Value *Ptr, ArrayRef< Value * > IdxList, const Twine &NameStr="", Instruction *InsertBefore=nullptr)
Create an "inbounds" getelementptr.
Value * SimplifyFreezeInst(Value *Op, const SimplifyQuery &Q)
Given an operand for a Freeze, see if we can fold the result.
This is the common base class for memset/memcpy/memmove.
bool hasFnAttribute(Attribute::AttrKind Kind) const
Return true if the function has the attribute.
class_match< BinaryOperator > m_BinOp()
Match an arbitrary binary operation and ignore it.
DiagnosticInfoOptimizationBase::Argument NV
BinaryOp_match< LHS, RHS, Instruction::AShr > m_AShr(const LHS &L, const RHS &R)
static bool isMustTailCall(Value *V)
Value * Descale(Value *Val, APInt Scale, bool &NoSignedWrap)
Returns a value X such that Val = X * Scale, or null if none.
const BasicBlock * getSinglePredecessor() const
Return the predecessor of this block if it has a single predecessor block.
LLVM_NODISCARD T pop_back_val()
void LLVMInitializeInstCombine(LLVMPassRegistryRef R)
Class to represent array types.
succ_range successors(Instruction *I)
gep_type_iterator gep_type_begin(const User *GEP)
BinaryOp_match< ValTy, cst_pred_ty< is_all_ones >, Instruction::Xor, true > m_Not(const ValTy &V)
Matches a 'Not' as 'xor V, -1' or 'xor -1, V'.
Convenience struct for specifying and reasoning about fast-math flags.
std::underlying_type_t< E > Mask()
Get a bitmask with 1s in all places up to the high-order bit of E's largest value.
Value * EmitGEPOffset(IRBuilderTy *Builder, const DataLayout &DL, User *GEP, bool NoAssumptions=false)
Given a getelementptr instruction/constantexpr, emit the code necessary to compute the offset from th...
INITIALIZE_PASS_END(RegBankSelect, DEBUG_TYPE, "Assign register bank of generic virtual registers", false, false) RegBankSelect
bool isMinValue() const
Determine if this is the smallest unsigned value.
static IntegerType * getInt32Ty(LLVMContext &C)
bool isAllocationFn(const Value *V, const TargetLibraryInfo *TLI, bool LookThroughBitCast=false)
Tests if a value is a call or invoke to a library function that allocates or reallocates memory (eith...
void swapSuccessors()
Swap the successors of this branch instruction.
Value * SimplifySelectsFeedingBinaryOp(BinaryOperator &I, Value *LHS, Value *RHS)
void initializeInstCombine(PassRegistry &)
Initialize all passes linked into the InstCombine library.
This is an alternative analysis pass to BlockFrequencyInfoWrapperPass.
LLVM Basic Block Representation.
gep_type_iterator gep_type_end(const User *GEP)
unsigned getNumOperands() const
Return number of MDNode operands.
Instruction * visitUnreachableInst(UnreachableInst &I)
constexpr int UndefMaskElem
OneUse_match< T > m_OneUse(const T &SubPattern)
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
static Constant * getSub(Constant *C1, Constant *C2, bool HasNUW=false, bool HasNSW=false)
EHPersonality classifyEHPersonality(const Value *Pers)
See if the given exception handling personality function is one that we understand.
Instruction * visitFree(CallInst &FI)
apint_match m_APInt(const APInt *&Res)
Match a ConstantInt or splatted ConstantVector, binding the specified pointer to the contained APInt.
AnyBinaryOp_match< LHS, RHS, true > m_c_BinOp(const LHS &L, const RHS &R)
Matches a BinaryOperator with LHS and RHS in either order.
This is the shared class of boolean and integer constants.
bool isArrayTy() const
True if this is an instance of ArrayType.
FunctionType * getType(LLVMContext &Context, ID id, ArrayRef< Type * > Tys=None)
Return the function type for an intrinsic.
unsigned getOpcode() const
Returns a member of one of the enums like Instruction::Add.
bool isNonNegative() const
Determine if this APInt Value is non-negative (>= 0)
static bool combineInstructionsOverFunction(Function &F, InstCombineWorklist &Worklist, AliasAnalysis *AA, AssumptionCache &AC, TargetLibraryInfo &TLI, TargetTransformInfo &TTI, DominatorTree &DT, OptimizationRemarkEmitter &ORE, BlockFrequencyInfo *BFI, ProfileSummaryInfo *PSI, unsigned MaxIterations, LoopInfo *LI)
BlockFrequencyInfo pass uses BlockFrequencyInfoImpl implementation to estimate IR basic block frequen...
bool isSplatValue(const Value *V, int Index=-1, unsigned Depth=0)
Return true if each element of the vector value V is poisoned or equal to every other non-poisoned el...
static SelectInst * Create(Value *C, Value *S1, Value *S2, const Twine &NameStr="", Instruction *InsertBefore=nullptr, Instruction *MDFrom=nullptr)
static bool TryToSinkInstruction(Instruction *I, BasicBlock *DestBlock)
Try to move the specified instruction from its current block into the beginning of DestBlock,...
static bool hasNoUnsignedWrap(BinaryOperator &I)
This class represents a conversion between pointers from one address space to another.
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
ThreeOps_match< Cond, LHS, RHS, Instruction::Select > m_Select(const Cond &C, const LHS &L, const RHS &R)
Matches SelectInst.
uint64_t getNumElements() const
match_combine_or< CastClass_match< OpTy, Instruction::ZExt >, CastClass_match< OpTy, Instruction::SExt > > m_ZExtOrSExt(const OpTy &Op)
bool match(Val *V, const Pattern &P)
Value * getIncomingValue(unsigned i) const
Return incoming value number x.
INITIALIZE_PASS_BEGIN(InstructionCombiningPass, "instcombine", "Combine redundant instructions", false, false) INITIALIZE_PASS_END(InstructionCombiningPass
bool isNullValue() const
Return true if this is the value that would be returned by getNullValue.
Itanium Name Demangler i e convert the string _Z1fv into f()". You can also use the CRTP base ManglingParser to perform some simple analysis on the mangled name
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
void append(in_iter in_start, in_iter in_end)
Add the specified range to the end of the SmallVector.
(vector float) vec_cmpeq(*A, *B) C
Instruction * foldOpIntoPhi(Instruction &I, PHINode *PN)
Given a binary operator, cast instruction, or select which has a PHI node as operand #0,...
unsigned getNumClauses() const
Get the number of clauses for this landing pad.
Function * getCalledFunction() const
Returns the function called, or null if this is an indirect function invocation.
iterator begin()
Instruction iterator methods.
static GCMetadataPrinterRegistry::Add< OcamlGCMetadataPrinter > Y("ocaml", "ocaml 3.10-compatible collector")
Represent the analysis usage information of a pass.
Instruction * visitFreeze(FreezeInst &I)
static Optional< unsigned > getOpcode(ArrayRef< VPValue * > Values)
Returns the opcode of Values or ~0 if they do not all agree.
DEBUG_COUNTER(VisitCounter, "instcombine-visit", "Controls which instructions are visited")
bool isVectorTy() const
True if this is an instance of VectorType.
Instruction * visitUnconditionalBranchInst(BranchInst &BI)
bind_ty< Instruction > m_Instruction(Instruction *&I)
Match an instruction, capturing it if we match.
This requires reassociating to forms of expressions that are already something that reassoc doesn t think about yet These two functions should generate the same code on big endian int * l
const_iterator getFirstInsertionPt() const
Returns an iterator to the first instruction in this block that is suitable for inserting a non-PHI i...
ElementCount getElementCount() const
Return an ElementCount instance to represent the (possibly scalable) number of elements in the vector...
CastClass_match< OpTy, Instruction::ZExt > m_ZExt(const OpTy &Op)
Matches ZExt.
bool isPotentiallyReachable(const Instruction *From, const Instruction *To, const SmallPtrSetImpl< BasicBlock * > *ExclusionSet=nullptr, const DominatorTree *DT=nullptr, const LoopInfo *LI=nullptr)
Determine whether instruction 'To' is reachable from 'From', without passing through any blocks in Ex...
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
std::pair< unsigned, unsigned > removeAllNonTerminatorAndEHPadInstructions(BasicBlock *BB)
Remove all instructions from a basic block other than its terminator and any present EH pad instructi...
Class to represent integer types.
ConstantArray - Constant Array Declarations.
static Constant * getAllOnesValue(Type *Ty)
BinaryOp_match< LHS, RHS, Instruction::SDiv > m_SDiv(const LHS &L, const RHS &R)
BinaryOps getOpcode() const
class_match< ConstantInt > m_ConstantInt()
Match an arbitrary ConstantInt and ignore it.
static bool shouldExecute(unsigned CounterName)
Legacy analysis pass which computes a DominatorTree.
PassRegistry - This class manages the registration and intitialization of the pass subsystem as appli...
Attribute unwrap(LLVMAttributeRef Attr)
LLVM_ATTRIBUTE_NORETURN void report_fatal_error(Error Err, bool gen_crash_diag=true)
Report a serious error, calling any installed error handler.
STATISTIC(NumFunctions, "Total number of functions")
void addClause(Constant *ClauseVal)
Add a catch or filter clause to the landing pad.
static UndefValue * get(Type *T)
Static factory methods - Return an 'undef' object of the specified type.
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.
const Value * getUnderlyingObject(const Value *V, unsigned MaxLookup=6)
This method strips off any GEP address adjustments and pointer casts from the specified value,...
InstCombineWorklist - This is the worklist management logic for InstCombine.
const Instruction * getFirstNonPHI() const
Returns a pointer to the first instruction in this block that is not a PHINode instruction.
Value * SimplifyGEPInst(Type *SrcTy, ArrayRef< Value * > Ops, const SimplifyQuery &Q)
Given operands for a GetElementPtrInst, fold the result or return null.
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)".
apint_match m_APIntAllowUndef(const APInt *&Res)
Match APInt while allowing undefs in splat vector constants.
Analysis pass which computes BlockFrequencyInfo.
unsigned getNumIncomingValues() const
Return the number of incoming edges.
static void ClearSubclassDataAfterReassociation(BinaryOperator &I)
Conservatively clears subclassOptionalData after a reassociation or commutation.
static GCMetadataPrinterRegistry::Add< ErlangGCPrinter > X("erlang", "erlang-compatible garbage collector")
bool SimplifyAssociativeOrCommutative(BinaryOperator &I)
Performs a few simplifications for operators which are associative or commutative.
static bool simplifyAssocCastAssoc(BinaryOperator *BinOp1, InstCombinerImpl &IC)
Combine constant operands of associative operations either before or after a cast to eliminate one of...
bool isCommutative() const LLVM_READONLY
Return true if the instruction is commutative:
Analysis providing profile information.
Value * getCondition() const
This class is the base class for the comparison instructions.
bool isIntegerTy() const
True if this is an instance of IntegerType.
const MDOperand & getOperand(unsigned I) const
void push(Instruction *I)
Push the instruction onto the worklist stack.
static Value * foldOperationIntoPhiValue(BinaryOperator *I, Value *InV, InstCombiner::BuilderTy &Builder)
Base class of all SIMD vector types.
Type * getArrayElementType() const
static Constant * getTrunc(Constant *C, Type *Ty, bool OnlyIfReduced=false)
bool hasAllZeroIndices() const
Return true if all of the indices of this GEP are zeros.
static constexpr unsigned InstCombineDefaultInfiniteLoopThreshold
This is the common base class for debug info intrinsics for variables.
static Constant * getCompare(unsigned short pred, Constant *C1, Constant *C2, bool OnlyIfReduced=false)
Return an ICmp or FCmp comparison operator constant expression.
inst_range instructions(Function *F)
is_zero m_Zero()
Match any null constant or a vector with all elements equal to 0.
An instruction for storing to memory.
This is an important base class in LLVM.
static bool isNeverEqualToUnescapedAlloc(Value *V, const TargetLibraryInfo *TLI, Instruction *AI)
SymbolTableList< Instruction >::iterator eraseFromParent()
This method unlinks 'this' from the containing basic block and deletes it.
br_match m_UnconditionalBr(BasicBlock *&Succ)
match_combine_and< class_match< Constant >, match_unless< class_match< ConstantExpr > > > m_ImmConstant()
Match an arbitrary immediate Constant and ignore it.
This instruction compares its operands according to the predicate given to the constructor.
static Constant * getBinOpIdentity(unsigned Opcode, Type *Ty, bool AllowRHSConstant=false)
Return the identity constant for a binary opcode.
static cl::opt< bool > EnableCodeSinking("instcombine-code-sinking", cl::desc("Enable code sinking"), cl::init(true))
unsigned countMinLeadingOnes() const
Returns the minimum number of leading one bits.
Used to lazily calculate structure layout information for a target machine, based on the DataLayout s...
static GCRegistry::Add< StatepointGC > D("statepoint-example", "an example strategy for statepoint")
static cl::opt< unsigned > MaxArraySize("instcombine-maxarray-size", cl::init(1024), cl::desc("Maximum array size considered when doing a combine"))
Instruction * user_back()
Specialize the methods defined in Value, as we know that an instruction can only be used by other ins...
A function analysis which provides an AssumptionCache.
static void sdivrem(const APInt &LHS, const APInt &RHS, APInt &Quotient, APInt &Remainder)
INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
void preserve()
Mark an analysis as preserved.
Legacy wrapper pass to provide the BasicAAResult object.
Value * SimplifyBinOp(unsigned Opcode, Value *LHS, Value *RHS, const SimplifyQuery &Q)
Given operands for a BinaryOperator, fold the result or return null.
void addIncoming(Value *V, BasicBlock *BB)
Add an incoming value to the end of the PHI list.
BinaryOp_match< LHS, RHS, Instruction::Or > m_Or(const LHS &L, const RHS &R)
void analyse(Instruction *I)
static bool isAllocSiteRemovable(Instruction *AI, SmallVectorImpl< WeakTrackingVH > &Users, const TargetLibraryInfo *TLI)
static Constant * get(unsigned Opcode, Constant *C1, unsigned Flags=0, Type *OnlyIfReducedTy=nullptr)
get - Return a unary operator constant expression, folding if possible.
A vector constant whose element type is a simple 1/2/4/8-byte integer or float/double,...
'undef' values are things that do not have specified contents.
an instruction for type-safe pointer arithmetic to access elements of arrays and structs
static Constant * getShl(Constant *C1, Constant *C2, bool HasNUW=false, bool HasNSW=false)
initializer< Ty > init(const Ty &Val)
iterator_range< early_inc_iterator_impl< detail::IterOfRange< RangeT > > > make_early_inc_range(RangeT &&Range)
Make a range that does early increment to allow mutation of the underlying range without disrupting i...
Value * lowerObjectSizeCall(IntrinsicInst *ObjectSize, const DataLayout &DL, const TargetLibraryInfo *TLI, bool MustSucceed)
Try to turn a call to @llvm.objectsize into an integer value of the given Type.
Identifies a unique instance of a variable.
An analysis pass based on legacy pass manager to deliver ProfileSummaryInfo.
unsigned getElementContainingOffset(uint64_t Offset) const
Given a valid byte offset into the structure, returns the structure index that contains it.
Class to represent pointers.
bool is_contained(R &&Range, const E &Element)
Wrapper function around std::find to detect if an element exists in a container.
static bool isEquality(Predicate P)
Return true if this predicate is either EQ or NE.
Constant * getClause(unsigned Idx) const
Get the value of the clause at index Idx.
static cl::opt< unsigned > ShouldLowerDbgDeclare("instcombine-lower-dbg-declare", cl::Hidden, cl::init(true))
Constant Vector Declarations.
void computeKnownBits(const Value *V, KnownBits &Known, const DataLayout &DL, unsigned Depth=0, AssumptionCache *AC=nullptr, const Instruction *CxtI=nullptr, const DominatorTree *DT=nullptr, OptimizationRemarkEmitter *ORE=nullptr, bool UseInstrInfo=true)
Determine which bits of V are known to be either zero or one and return them in the KnownZero/KnownOn...
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
Utility class for floating point operations which can have information about relaxed accuracy require...
BinaryOp_match< LHS, RHS, Instruction::Sub > m_Sub(const LHS &L, const RHS &R)
uint64_t getArrayNumElements() const
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
void salvageDebugInfoForDbgValues(Instruction &I, ArrayRef< DbgVariableIntrinsic * > Insns)
Implementation of salvageDebugInfo, applying only to instructions in Insns, rather than all debug use...
<%struct.s * > cast struct s *S to sbyte *< sbyte * > sbyte uint cast struct s *agg result to sbyte *< sbyte * > sbyte uint cast struct s *memtmp to sbyte *< sbyte * > sbyte uint ret void llc ends up issuing two memcpy or custom lower memcpy(of small size) to be ldmia/stmia. I think option 2 is better but the current register allocator cannot allocate a chunk of registers at a time. A feasible temporary solution is to use specific physical registers at the lowering time for small(<
static Constant * getCast(unsigned ops, Constant *C, Type *Ty, bool OnlyIfReduced=false)
Convenience function for getting a Cast operation.
Type * getIndexedType() const
unsigned countMinLeadingZeros() const
Returns the minimum number of leading zero bits.
This class represents the LLVM 'select' instruction.
print Print MemDeps of function
SimplifyQuery getWithInstruction(Instruction *I) const
static ArrayType * get(Type *ElementType, uint64_t NumElements)
This static method is the primary way to construct an ArrayType.
static bool isCatchAll(EHPersonality Personality, Constant *TypeInfo)
Return 'true' if the given typeinfo will match anything.
A Module instance is used to store all the information related to an LLVM module.
static constexpr unsigned InstCombineDefaultMaxIterations
class_match< Constant > m_Constant()
Match an arbitrary Constant and ignore it.
Provides an 'InsertHelper' that calls a user-provided callback after performing the default insertion...
Value * SimplifyUsingDistributiveLaws(BinaryOperator &I)
Tries to simplify binary operations which some other binary operation distributes over.
Instruction * visitSwitchInst(SwitchInst &SI)
size_type count(ConstPtrType Ptr) const
count - Return 1 if the specified pointer is in the set, 0 otherwise.
bool isUnconditional() const
class_match< Value > m_Value()
Match an arbitrary value and ignore it.
Class for arbitrary precision integers.
struct LLVMOpaquePassManager * LLVMPassManagerRef
Instruction * foldVectorBinop(BinaryOperator &Inst)
Canonicalize the position of binops relative to shufflevector.
static GetElementPtrInst * Create(Type *PointeeType, Value *Ptr, ArrayRef< Value * > IdxList, const Twine &NameStr="", Instruction *InsertBefore=nullptr)
An analysis pass based on the new PM to deliver ProfileSummaryInfo.
CastClass_match< OpTy, Instruction::SExt > m_SExt(const OpTy &Op)
Matches SExt.
An immutable pass that tracks lazily created AssumptionCache objects.
bool isInstructionTriviallyDead(Instruction *I, const TargetLibraryInfo *TLI=nullptr)
Return true if the result produced by the instruction is not used, and the instruction has no side ef...
iterator_range< filter_iterator< BasicBlock::const_iterator, std::function< bool(const Instruction &)> > > instructionsWithoutDebug(bool SkipPseudoOp=false) const
Return a const iterator range over the instructions in the block, skipping any debug instructions.
FastMathFlags getFastMathFlags() const
Convenience function for getting all the fast-math flags, which must be an operator which supports th...
APInt ssub_ov(const APInt &RHS, bool &Overflow) const
static APInt getAllOnesValue(unsigned numBits)
Get the all-ones value.
Expected< ExpressionValue > min(const ExpressionValue &Lhs, const ExpressionValue &Rhs)
Class to represent struct types.
SmallVector< MachineOperand, 4 > Cond
void setPreservesCFG()
This function should be called by the pass, iff they do not:
uint64_t getSizeInBytes() const
A cache of @llvm.assume calls within a function.
static Instruction * tryToMoveFreeBeforeNullTest(CallInst &FI, const DataLayout &DL)
Move the call to free before a NULL test.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
Constant * getAggregateElement(unsigned Elt) const
For aggregates (struct/array/vector) return the constant that corresponds to the specified element if...
Value * SimplifyExtractValueInst(Value *Agg, ArrayRef< unsigned > Idxs, const SimplifyQuery &Q)
Given operands for an ExtractValueInst, fold the result or return null.
Type * getType() const
All values are typed, get the type of this value.
Represents analyses that only rely on functions' control flow.
const BasicBlock * getUniquePredecessor() const
Return the predecessor of this block if it has a unique predecessor block.
const Function * getFunction() const
Return the function this instruction belongs to.
TwoOps_match< V1_t, V2_t, Instruction::ShuffleVector > m_Shuffle(const V1_t &v1, const V2_t &v2)
Matches ShuffleVectorInst independently of mask value.
AnalysisUsage & addPreserved()
Add the specified Pass class to the set of analyses preserved by this pass.
void replaceAllUsesWith(Value *V)
Change all uses of this to point to a new Value.
static const Function * getParent(const Value *V)
void append_range(Container &C, Range &&R)
Wrapper function to append a range to a container.
self_iterator getIterator()
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
bool isFilter(unsigned Idx) const
Return 'true' if the clause and index Idx is a filter clause.
static bool isTargetIntrinsic(Intrinsic::ID IID)
isTargetIntrinsic - Returns true if IID is an intrinsic specific to a certain target.
static Constant * get(ArrayRef< Constant * > V)
This is the base class for all instructions that perform data casts.
Represents an op.with.overflow intrinsic.
std::pair< NoneType, bool > insert(const T &V)
insert - Insert an element into the set if it isn't already there.
#define LLVM_FALLTHROUGH
LLVM_FALLTHROUGH - Mark fallthrough cases in switch statements.
StringRef getName() const
Return a constant reference to the value's name.
LLVMContext & getContext() const
Return the LLVMContext in which this type was uniqued.
An instruction for reading from memory.
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...
const Constant * stripPointerCasts() const
const Value * stripPointerCasts() const
Strip off pointer casts, all-zero GEPs and address space casts.
static ConstantInt * getFalse(LLVMContext &Context)
bool runOnFunction(Function &F) override
runOnFunction - Virtual method overriden by subclasses to do the per-function processing of the pass.
const Value * stripAndAccumulateInBoundsConstantOffsets(const DataLayout &DL, APInt &Offset) const
This is a wrapper around stripAndAccumulateConstantOffsets with the in-bounds requirement set to fals...
void setFastMathFlags(FastMathFlags FMF)
Convenience function for setting multiple fast-math flags on this instruction, which must be an opera...
static bool shorter_filter(const Value *LHS, const Value *RHS)
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
void stable_sort(R &&Range)
TargetFolder - Create constants with target dependent folding.
APInt trunc(unsigned width) const
Truncate to new width.
static Constant * getNullValue(Type *Ty)
Constructor to create a '0' constant of arbitrary type.
bool isMinSignedValue() const
Determine if this is the smallest signed value.
bool isNoAliasScopeDeclDead(Instruction *Inst)
static Constant * getNeg(Constant *C, bool HasNUW=false, bool HasNSW=false)
static bool maintainNoSignedWrap(BinaryOperator &I, Value *B, Value *C)
static BinaryOperator * CreateNUW(BinaryOps Opc, Value *V1, Value *V2, const Twine &Name="")
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
uint64_t getElementOffset(unsigned Idx) const
static PHINode * Create(Type *Ty, unsigned NumReservedValues, const Twine &NameStr="", Instruction *InsertBefore=nullptr)
Constructors - NumReservedValues is a hint for the number of incoming edges that this phi node will h...
bool isInBounds() const
Test whether this is an inbounds GEP, as defined by LangRef.html.
bool isGuaranteedToTransferExecutionToSuccessor(const Instruction *I)
Return true if this function can prove that the instruction I will always transfer execution to one o...
PassT::Result * getCachedResult(IRUnitT &IR) const
Get the cached result of an analysis pass for a given IR unit.
static Type * getTypeAtIndex(Type *Ty, Value *Idx)
Return the type of the element at the given index of an indexable type.
constexpr unsigned BitWidth
void add(Instruction *I)
Add instruction to the worklist.
void sort(IteratorTy Start, IteratorTy End)
void salvageDebugInfo(Instruction &I)
Assuming the instruction I is going to be deleted, attempt to salvage debug users of I by writing the...
Provides information about what library functions are available for the current target.
int32_t exactLogBase2() const
static Constant * get(ArrayType *T, ArrayRef< Constant * > V)
bool isEHPad() const
Return true if the instruction is a variety of EH-block.
static bool hasNoSignedWrap(BinaryOper