69 using namespace llvm::PatternMatch;
71 #define DEBUG_TYPE "instcombine"
73 STATISTIC(NumCombined ,
"Number of insts combined");
74 STATISTIC(NumConstProp,
"Number of constant folds");
75 STATISTIC(NumDeadInst ,
"Number of dead inst eliminated");
76 STATISTIC(NumSunkInst ,
"Number of instructions sunk");
77 STATISTIC(NumExpand,
"Number of expansions");
78 STATISTIC(NumFactor ,
"Number of factorizations");
79 STATISTIC(NumReassoc ,
"Number of reassociations");
83 cl::desc(
"Enable expensive instruction combines"));
93 bool InstCombiner::ShouldChangeType(
unsigned FromWidth,
94 unsigned ToWidth)
const {
95 bool FromLegal =
DL.isLegalInteger(FromWidth);
96 bool ToLegal =
DL.isLegalInteger(ToWidth);
100 if (FromLegal && !ToLegal)
105 if (!FromLegal && !ToLegal && ToWidth > FromWidth)
114 bool InstCombiner::ShouldChangeType(
Type *From,
Type *To)
const {
119 return ShouldChangeType(FromWidth, ToWidth);
137 const APInt *BVal, *CVal;
141 bool Overflow =
false;
143 BVal->
sadd_ov(*CVal, Overflow);
145 BVal->
ssub_ov(*CVal, Overflow);
171 if (!Cast || !Cast->hasOneUse())
176 if (CastOpcode != Instruction::ZExt)
185 if (!BinOp2 || !BinOp2->hasOneUse() || BinOp2->getOpcode() != AssocOpcode)
202 Cast->setOperand(0, BinOp2->getOperand(0));
227 bool InstCombiner::SimplifyAssociativeOrCommutative(
BinaryOperator &
I) {
229 bool Changed =
false;
273 if (Op1 && Op1->getOpcode() == Opcode) {
275 Value *B = Op1->getOperand(0);
276 Value *C = Op1->getOperand(1);
321 if (Op1 && Op1->getOpcode() == Opcode) {
323 Value *B = Op1->getOperand(0);
324 Value *C = Op1->getOperand(1);
343 Op0->
getOpcode() == Opcode && Op1->getOpcode() == Opcode &&
345 isa<Constant>(Op1->getOperand(1)) &&
349 Value *B = Op1->getOperand(0);
350 Constant *C2 = cast<Constant>(Op1->getOperand(1));
354 if (isa<FPMathOperator>(New)) {
357 Flags &= Op1->getFastMathFlags();
360 InsertNewInstWith(New, I);
396 case Instruction::Mul:
402 case Instruction::Sub:
436 case Instruction::Shl:
437 case Instruction::LShr:
438 case Instruction::AShr:
451 if (isa<Constant>(V))
454 if (OpCode == Instruction::Mul)
472 return Instruction::BinaryOpsEnd;
477 switch (TopLevelOpcode) {
482 case Instruction::Sub:
483 if (Op->
getOpcode() == Instruction::Shl) {
487 return Instruction::Mul;
505 if (!A || !C || !B || !D)
509 Value *SimplifiedInst =
nullptr;
520 if (A == C || (InnerCommutative && A == D)) {
528 if (!V && LHS->
hasOneUse() && RHS->hasOneUse())
531 SimplifiedInst = Builder->
CreateBinOp(InnerOpcode, A, V);
539 if (B == D || (InnerCommutative && B == C)) {
548 if (!V && LHS->
hasOneUse() && RHS->hasOneUse())
551 SimplifiedInst = Builder->
CreateBinOp(InnerOpcode, V, B);
555 if (SimplifiedInst) {
561 if (
BinaryOperator *BO = dyn_cast<BinaryOperator>(SimplifiedInst)) {
562 if (isa<OverflowingBinaryOperator>(SimplifiedInst)) {
564 if (isa<OverflowingBinaryOperator>(&I))
568 if (isa<OverflowingBinaryOperator>(Op0))
572 if (isa<OverflowingBinaryOperator>(Op1))
573 HasNSW &= Op1->hasNoSignedWrap();
584 InnerOpcode == Instruction::Mul)
586 BO->setHasNoSignedWrap(HasNSW);
590 return SimplifiedInst;
604 Value *A =
nullptr, *B =
nullptr, *C =
nullptr, *
D =
nullptr;
611 if (LHSOpcode == RHSOpcode) {
641 if ((
L == A && R == B) ||
648 C = Builder->CreateBinOp(InnerOpcode,
L, R);
657 Value *A = LHS, *B = Op1->getOperand(0), *C = Op1->getOperand(1);
666 if ((
L == B && R == C) ||
673 A = Builder->CreateBinOp(InnerOpcode,
L, R);
681 if (
auto *SI0 = dyn_cast<SelectInst>(LHS)) {
682 if (
auto *SI1 = dyn_cast<SelectInst>(RHS)) {
683 if (SI0->getCondition() == SI1->getCondition()) {
686 SI1->getFalseValue(),
DL, &TLI, &DT, &AC))
687 SI = Builder->CreateSelect(SI0->getCondition(),
688 Builder->CreateBinOp(TopLevelOpcode,
690 SI1->getTrueValue()),
693 SI1->getTrueValue(),
DL, &TLI, &DT, &AC))
694 SI = Builder->CreateSelect(
695 SI0->getCondition(), V,
696 Builder->CreateBinOp(TopLevelOpcode, SI0->getFalseValue(),
697 SI1->getFalseValue()));
711 Value *InstCombiner::dyn_castNegVal(
Value *V)
const {
728 Value *InstCombiner::dyn_castFNegVal(
Value *V,
bool IgnoreZeroSign)
const {
745 if (
auto *Cast = dyn_cast<CastInst>(&I))
751 bool ConstIsRHS = isa<Constant>(I.
getOperand(1));
754 if (
auto *SOC = dyn_cast<Constant>(SO)) {
760 Value *Op0 = SO, *Op1 = ConstOperand;
764 auto *BO = cast<BinaryOperator>(&
I);
768 if (FPInst && isa<FPMathOperator>(FPInst))
780 if (!(isa<Constant>(TV) || isa<Constant>(FV)))
789 if (
auto *BC = dyn_cast<BitCastInst>(&Op)) {
794 if ((SrcTy ==
nullptr) != (DestTy ==
nullptr))
810 if (CI->hasOneUse()) {
826 if (NumPHIValues == 0)
848 for (
unsigned i = 0;
i != NumPHIValues; ++
i) {
850 if (isa<Constant>(InVal) && !isa<ConstantExpr>(InVal))
853 if (isa<PHINode>(InVal))
return nullptr;
854 if (NonConstBB)
return nullptr;
860 if (
InvokeInst *II = dyn_cast<InvokeInst>(InVal))
861 if (II->getParent() == NonConstBB)
875 if (NonConstBB !=
nullptr) {
882 InsertNewInstBefore(NewPN, *PN);
891 if (
SelectInst *SI = dyn_cast<SelectInst>(&I)) {
897 for (
unsigned i = 0;
i != NumPHIValues; ++
i) {
901 Value *InV =
nullptr;
905 if (InC && !isa<ConstantExpr>(InC))
906 InV = InC->
isNullValue() ? FalseVInPred : TrueVInPred;
909 TrueVInPred, FalseVInPred,
"phitmp");
912 }
else if (
CmpInst *CI = dyn_cast<CmpInst>(&I)) {
914 for (
unsigned i = 0;
i != NumPHIValues; ++
i) {
915 Value *InV =
nullptr;
918 else if (isa<ICmpInst>(CI))
928 for (
unsigned i = 0;
i != NumPHIValues; ++
i) {
929 Value *InV =
nullptr;
933 InV = Builder->CreateBinOp(cast<BinaryOperator>(I).getOpcode(),
940 for (
unsigned i = 0;
i != NumPHIValues; ++
i) {
945 InV = Builder->CreateCast(CI->
getOpcode(),
953 if (User == &I)
continue;
954 replaceInstUsesWith(*User, NewPN);
955 eraseInstFromFunction(*User);
957 return replaceInstUsesWith(I, NewPN);
963 if (
auto *Sel = dyn_cast<SelectInst>(I.
getOperand(0))) {
964 if (
Instruction *NewSel = FoldOpIntoSelect(I, Sel))
986 Type *IntPtrTy =
DL.getIntPtrType(PtrTy);
987 int64_t FirstIdx = 0;
988 if (int64_t TySize =
DL.getTypeAllocSize(Ty)) {
989 FirstIdx = Offset/TySize;
990 Offset -= FirstIdx*TySize;
998 assert((uint64_t)Offset < (uint64_t)TySize &&
"Out of range offset");
1006 if (uint64_t(Offset * 8) >=
DL.getTypeSizeInBits(Ty))
1009 if (
StructType *STy = dyn_cast<StructType>(Ty)) {
1012 "Offset must stay within the indexed type");
1019 Ty = STy->getElementType(Elt);
1020 }
else if (
ArrayType *AT = dyn_cast<ArrayType>(Ty)) {
1021 uint64_t EltSize =
DL.getTypeAllocSize(AT->getElementType());
1022 assert(EltSize &&
"Cannot index into a zero-sized array");
1025 Ty = AT->getElementType();
1047 Value *InstCombiner::Descale(
Value *Val,
APInt Scale,
bool &NoSignedWrap) {
1048 assert(isa<IntegerType>(Val->
getType()) &&
"Can only descale integers!");
1050 Scale.
getBitWidth() &&
"Scale not compatible with value!");
1054 NoSignedWrap =
true;
1086 std::pair<Instruction*, unsigned> Parent;
1090 bool RequireNoSignedWrap =
false;
1095 for (;; Op = Parent.first->getOperand(Parent.second)) {
1097 if (
ConstantInt *CI = dyn_cast<ConstantInt>(Op)) {
1099 APInt Quotient(Scale), Remainder(Scale);
1101 if (!Remainder.isMinValue())
1106 NoSignedWrap =
true;
1112 if (BO->getOpcode() == Instruction::Mul) {
1114 NoSignedWrap = BO->hasNoSignedWrap();
1115 if (RequireNoSignedWrap && !NoSignedWrap)
1121 Value *LHS = BO->getOperand(0);
1122 Value *RHS = BO->getOperand(1);
1124 if (
ConstantInt *CI = dyn_cast<ConstantInt>(RHS)) {
1126 if (CI->getValue() == Scale) {
1137 Parent = std::make_pair(BO, 1);
1146 Parent = std::make_pair(BO, 0);
1150 if (logScale > 0 && BO->getOpcode() == Instruction::Shl &&
1151 isa<ConstantInt>(BO->getOperand(1))) {
1153 NoSignedWrap = BO->hasNoSignedWrap();
1154 if (RequireNoSignedWrap && !NoSignedWrap)
1157 Value *LHS = BO->getOperand(0);
1158 int32_t Amt = cast<ConstantInt>(BO->getOperand(1))->
1162 if (Amt == logScale) {
1168 if (Amt < logScale || !Op->hasOneUse())
1173 Parent = std::make_pair(BO, 1);
1182 if (
CastInst *Cast = dyn_cast<CastInst>(Op)) {
1183 if (Cast->getOpcode() == Instruction::SExt) {
1185 unsigned SmallSize = Cast->getSrcTy()->getPrimitiveSizeInBits();
1197 RequireNoSignedWrap =
true;
1200 Parent = std::make_pair(Cast, 0);
1205 if (Cast->getOpcode() == Instruction::Trunc) {
1212 if (RequireNoSignedWrap)
1216 unsigned LargeSize = Cast->getSrcTy()->getPrimitiveSizeInBits();
1217 Parent = std::make_pair(Cast, 0);
1218 Scale = Scale.
sext(LargeSize);
1219 if (logScale + 1 == (int32_t)Cast->getType()->getPrimitiveSizeInBits())
1232 NoSignedWrap =
true;
1246 assert(Parent.first->hasOneUse() &&
"Drilled down when more than one use!");
1247 assert(Op != Parent.first->getOperand(Parent.second) &&
1248 "Descaling was a no-op?");
1249 Parent.first->setOperand(Parent.second, Op);
1250 Worklist.Add(Parent.first);
1265 NoSignedWrap &= OpNoSignedWrap;
1266 if (NoSignedWrap != OpNoSignedWrap) {
1267 BO->setHasNoSignedWrap(NoSignedWrap);
1268 Worklist.Add(Ancestor);
1270 }
else if (Ancestor->
getOpcode() == Instruction::Trunc) {
1274 NoSignedWrap =
false;
1276 assert((Ancestor->
getOpcode() != Instruction::SExt || NoSignedWrap) &&
1277 "Failed to keep proper track of nsw flags while drilling down?");
1279 if (Ancestor == Val)
1284 assert(Ancestor->
hasOneUse() &&
"Drilled down when more than one use!");
1296 NewBO->copyIRFlags(&Inst);
1313 unsigned VWidth = cast<VectorType>(Inst.
getType())->getNumElements();
1315 assert(cast<VectorType>(LHS->
getType())->getNumElements() == VWidth);
1316 assert(cast<VectorType>(RHS->
getType())->getNumElements() == VWidth);
1322 if (isa<ShuffleVectorInst>(LHS) && isa<ShuffleVectorInst>(RHS)) {
1326 isa<UndefValue>(RShuf->getOperand(1)) &&
1328 LShuf->
getMask() == RShuf->getMask()) {
1330 RShuf->getOperand(0), Builder);
1331 return Builder->CreateShuffleVector(NewBO,
1340 if (isa<ShuffleVectorInst>(LHS)) Shuffle = cast<ShuffleVectorInst>(LHS);
1341 if (isa<ShuffleVectorInst>(RHS)) Shuffle = cast<ShuffleVectorInst>(RHS);
1342 if (isa<Constant>(LHS)) C1 = cast<Constant>(LHS);
1343 if (isa<Constant>(RHS)) C1 = cast<Constant>(RHS);
1344 if (Shuffle && C1 &&
1345 (isa<ConstantVector>(C1) || isa<ConstantDataVector>(C1)) &&
1355 bool MayChange =
true;
1356 for (
unsigned I = 0; I < VWidth; ++
I) {
1357 if (ShMask[I] >= 0) {
1358 assert(ShMask[I] < (
int)VWidth);
1359 if (!isa<UndefValue>(C2M[ShMask[I]])) {
1371 return Builder->CreateShuffleVector(NewBO,
1384 return replaceInstUsesWith(GEP, V);
1390 bool MadeChange =
false;
1402 Type *IndexTy = (*I)->getType();
1409 if (EltTy->isSized() && DL.getTypeAllocSize(EltTy) == 0)
1410 if (!isa<Constant>(*I) || !cast<Constant>(*I)->isNullValue()) {
1415 if (IndexTy != NewIndexType) {
1419 *I = Builder->CreateIntCast(*I, NewIndexType,
true);
1427 if (
PHINode *PN = dyn_cast<PHINode>(PtrOp)) {
1453 Type *CurTy =
nullptr;
1486 }
else if (
CompositeType *CT = dyn_cast<CompositeType>(CurTy)) {
1487 CurTy = CT->getTypeAtIndex(Op1->
getOperand(J));
1514 Builder->SetInsertPoint(PN);
1520 NewPN->
addIncoming(cast<GEPOperator>(I)->getOperand(DI),
1537 if (
GEPOperator *Src = dyn_cast<GEPOperator>(PtrOp)) {
1545 dyn_cast<GEPOperator>(Src->getOperand(0)))
1552 bool EndsWithSequential =
false;
1555 EndsWithSequential = I.isSequential();
1558 if (EndsWithSequential) {
1563 Value *SO1 = Src->getOperand(Src->getNumOperands()-1);
1579 if (!isa<Constant>(GO1) || !isa<Constant>(SO1))
1581 Sum = Builder->CreateAdd(SO1, GO1, PtrOp->
getName()+
".sum");
1585 if (Src->getNumOperands() == 2) {
1590 Indices.
append(Src->op_begin()+1, Src->op_end()-1);
1593 }
else if (isa<Constant>(*GEP.
idx_begin()) &&
1594 cast<Constant>(*GEP.
idx_begin())->isNullValue() &&
1595 Src->getNumOperands() != 1) {
1597 Indices.
append(Src->op_begin()+1, Src->op_end());
1601 if (!Indices.
empty())
1604 Src->getSourceElementType(), Src->getOperand(0), Indices,
1607 Src->getOperand(0), Indices,
1614 DL.getPointerSizeInBits(AS)) {
1616 uint64_t TyAllocSize = DL.getTypeAllocSize(Ty);
1618 bool Matched =
false;
1621 if (TyAllocSize == 1) {
1626 if (TyAllocSize == 1ULL << C)
1630 if (TyAllocSize == C)
1640 Operator *Index = cast<Operator>(V);
1641 Value *PtrToInt = Builder->CreatePtrToInt(PtrOp, Index->
getType());
1665 if (StrippedPtr != PtrOp) {
1666 bool HasZeroPointerIndex =
false;
1668 HasZeroPointerIndex = C->isZero();
1677 if (HasZeroPointerIndex) {
1701 if (CATy->getElementType() == XATy->getElementType()) {
1723 ? Builder->CreateInBoundsGEP(
1724 nullptr, StrippedPtr, Idx, GEP.
getName())
1725 : Builder->CreateGEP(
nullptr, StrippedPtr, Idx,
1739 DL.getTypeAllocSize(ResElTy)) {
1744 ? Builder->CreateInBoundsGEP(
nullptr, StrippedPtr, Idx,
1746 : Builder->CreateGEP(
nullptr, StrippedPtr, Idx, GEP.
getName());
1760 uint64_t ResSize = DL.getTypeAllocSize(ResElTy);
1761 uint64_t SrcSize = DL.getTypeAllocSize(SrcElTy);
1762 if (ResSize && SrcSize % ResSize == 0) {
1765 uint64_t Scale = SrcSize / ResSize;
1770 "Index not cast to pointer width?");
1773 if (
Value *NewIdx = Descale(Idx,
APInt(BitWidth, Scale), NSW)) {
1779 ? Builder->CreateInBoundsGEP(
nullptr, StrippedPtr, NewIdx,
1781 : Builder->CreateGEP(
nullptr, StrippedPtr, NewIdx,
1798 uint64_t ResSize = DL.getTypeAllocSize(ResElTy);
1799 uint64_t ArrayEltSize =
1801 if (ResSize && ArrayEltSize % ResSize == 0) {
1804 uint64_t Scale = ArrayEltSize / ResSize;
1809 "Index not cast to pointer width?");
1812 if (
Value *NewIdx = Descale(Idx,
APInt(BitWidth, Scale), NSW)) {
1821 ? Builder->CreateInBoundsGEP(
1822 SrcElTy, StrippedPtr, Off, GEP.
getName())
1823 : Builder->CreateGEP(SrcElTy, StrippedPtr, Off,
1842 if (
BitCastInst *BC = dyn_cast<BitCastInst>(ASC->getOperand(0)))
1851 if (
BitCastInst *BCI = dyn_cast<BitCastInst>(PtrOp)) {
1852 Value *Operand = BCI->getOperand(0);
1854 unsigned OffsetBits = DL.getPointerTypeSizeInBits(GEP.
getType());
1856 if (!isa<BitCastInst>(Operand) &&
1869 BCI->getParent()->getInstList().insert(BCI->getIterator(),
I);
1870 replaceInstUsesWith(*BCI, I);
1885 if (FindElementAtOffset(OpType, Offset.
getSExtValue(), NewIndices)) {
1888 ? Builder->CreateInBoundsGEP(
nullptr, Operand, NewIndices)
1889 : Builder->CreateGEP(
nullptr, Operand, NewIndices);
1892 return replaceInstUsesWith(GEP, NGEP);
1905 APInt BasePtrOffset(PtrWidth, 0);
1906 Value *UnderlyingPtrOp =
1909 if (
auto *AI = dyn_cast<AllocaInst>(UnderlyingPtrOp)) {
1912 APInt AllocSize(PtrWidth, DL.getTypeAllocSize(AI->getAllocatedType()));
1913 if (BasePtrOffset.
ule(AllocSize)) {
1926 if (isa<ConstantPointerNull>(V))
1928 if (
auto *LI = dyn_cast<LoadInst>(V))
1929 return isa<GlobalVariable>(LI->getPointerOperand());
1953 case Instruction::BitCast:
1954 case Instruction::GetElementPtr:
1959 case Instruction::ICmp: {
1966 unsigned OtherIndex = (ICI->
getOperand(0) == PI) ? 1 : 0;
1976 switch (II->getIntrinsicID()) {
1980 case Intrinsic::memmove:
1981 case Intrinsic::memcpy:
1982 case Intrinsic::memset: {
1988 case Intrinsic::dbg_declare:
1989 case Intrinsic::dbg_value:
1990 case Intrinsic::invariant_start:
1991 case Intrinsic::invariant_end:
1992 case Intrinsic::lifetime_start:
1993 case Intrinsic::lifetime_end:
1994 case Intrinsic::objectsize:
2016 }
while (!Worklist.
empty());
2026 for (
unsigned i = 0, e = Users.
size();
i != e; ++
i) {
2035 if (II->getIntrinsicID() == Intrinsic::objectsize) {
2038 replaceInstUsesWith(*I, Result);
2039 eraseInstFromFunction(*I);
2044 for (
unsigned i = 0, e = Users.
size();
i != e; ++
i) {
2050 if (
ICmpInst *C = dyn_cast<ICmpInst>(I)) {
2051 replaceInstUsesWith(*C,
2053 C->isFalseWhenEqual()));
2054 }
else if (isa<BitCastInst>(I) || isa<GetElementPtrInst>(I)) {
2057 eraseInstFromFunction(*I);
2060 if (
InvokeInst *II = dyn_cast<InvokeInst>(&MI)) {
2062 Module *M = II->getModule();
2065 None,
"", II->getParent());
2067 return eraseInstFromFunction(MI);
2104 if (FreeInstrBB->
size() != 2)
2123 "Broken CFG: missing edge from predecessor to successor");
2134 if (isa<UndefValue>(Op)) {
2138 return eraseInstFromFunction(FI);
2143 if (isa<ConstantPointerNull>(Op))
2144 return eraseInstFromFunction(FI);
2171 APInt KnownZero(BitWidth, 0), KnownOne(BitWidth, 0);
2173 if ((KnownZero|KnownOne).isAllOnesValue())
2185 !isa<Constant>(X)) {
2204 TrueDest, FalseDest)) &&
2220 TrueDest, FalseDest)) &&
2244 assert(isa<ConstantInt>(NewCase) &&
2245 "Result of expression should be constant");
2246 CaseIter.setValue(cast<ConstantInt>(NewCase));
2253 APInt KnownZero(BitWidth, 0), KnownOne(BitWidth, 0);
2255 unsigned LeadingKnownZeros = KnownZero.countLeadingOnes();
2260 for (
auto &C : SI.
cases()) {
2262 LeadingKnownZeros, C.getCaseValue()->getValue().countLeadingZeros());
2264 LeadingKnownOnes, C.getCaseValue()->getValue().countLeadingOnes());
2267 unsigned NewWidth = BitWidth - std::max(LeadingKnownZeros, LeadingKnownOnes);
2272 if (NewWidth > 0 && NewWidth < BitWidth) {
2274 Builder->SetInsertPoint(&SI);
2275 Value *NewCond = Builder->CreateTrunc(Cond, Ty,
"trunc");
2279 APInt TruncatedCase = CaseIter.getCaseValue()->getValue().
trunc(NewWidth);
2292 return replaceInstUsesWith(EV, Agg);
2296 return replaceInstUsesWith(EV, V);
2300 const unsigned *exti, *exte, *insi, *inse;
2301 for (exti = EV.
idx_begin(), insi = IV->idx_begin(),
2302 exte = EV.
idx_end(), inse = IV->idx_end();
2303 exti != exte && insi != inse;
2317 if (exti == exte && insi == inse)
2322 return replaceInstUsesWith(EV, IV->getInsertedValueOperand());
2332 Value *NewEV = Builder->CreateExtractValue(IV->getAggregateOperand(),
2353 if (II->hasOneUse()) {
2357 switch (II->getIntrinsicID()) {
2358 case Intrinsic::uadd_with_overflow:
2359 case Intrinsic::sadd_with_overflow:
2361 Value *LHS = II->getArgOperand(0), *RHS = II->getArgOperand(1);
2363 eraseInstFromFunction(*II);
2370 if (II->getIntrinsicID() == Intrinsic::uadd_with_overflow)
2371 if (
ConstantInt *CI = dyn_cast<ConstantInt>(II->getArgOperand(1)))
2375 case Intrinsic::usub_with_overflow:
2376 case Intrinsic::ssub_with_overflow:
2378 Value *LHS = II->getArgOperand(0), *RHS = II->getArgOperand(1);
2380 eraseInstFromFunction(*II);
2381 return BinaryOperator::CreateSub(LHS, RHS);
2384 case Intrinsic::umul_with_overflow:
2385 case Intrinsic::smul_with_overflow:
2387 Value *LHS = II->getArgOperand(0), *RHS = II->getArgOperand(1);
2389 eraseInstFromFunction(*II);
2398 if (
LoadInst *
L = dyn_cast<LoadInst>(Agg))
2404 if (
L->isSimple() &&
L->hasOneUse()) {
2408 Indices.
push_back(Builder->getInt32(0));
2411 Indices.
push_back(Builder->getInt32(*I));
2415 Builder->SetInsertPoint(
L);
2416 Value *
GEP = Builder->CreateInBoundsGEP(
L->getType(),
2417 L->getPointerOperand(), Indices);
2420 return replaceInstUsesWith(EV, Builder->CreateLoad(GEP));
2435 switch (Personality) {
2462 cast<ArrayType>(LHS->
getType())->getNumElements()
2464 cast<ArrayType>(RHS->
getType())->getNumElements();
2476 bool MakeNewInstruction =
false;
2482 bool isLastClause =
i + 1 == e;
2490 if (AlreadyCaught.
insert(TypeInfo).second) {
2495 MakeNewInstruction =
true;
2502 MakeNewInstruction =
true;
2503 CleanupFlag =
false;
2516 ArrayType *FilterType = cast<ArrayType>(FilterClause->getType());
2522 if (!NumTypeInfos) {
2525 MakeNewInstruction =
true;
2526 CleanupFlag =
false;
2530 bool MakeNewFilter =
false;
2532 if (isa<ConstantAggregateZero>(FilterClause)) {
2534 assert(NumTypeInfos > 0 &&
"Should have handled empty filter already!");
2540 MakeNewInstruction =
true;
2547 if (NumTypeInfos > 1)
2548 MakeNewFilter =
true;
2552 NewFilterElts.
reserve(NumTypeInfos);
2557 bool SawCatchAll =
false;
2558 for (
unsigned j = 0; j != NumTypeInfos; ++j) {
2586 if (SeenInFilter.insert(TypeInfo).second)
2587 NewFilterElts.
push_back(cast<Constant>(Elt));
2592 MakeNewInstruction =
true;
2597 if (NewFilterElts.
size() < NumTypeInfos)
2598 MakeNewFilter =
true;
2600 if (MakeNewFilter) {
2602 NewFilterElts.
size());
2604 MakeNewInstruction =
true;
2613 if (MakeNewFilter && !NewFilterElts.
size()) {
2614 assert(MakeNewInstruction &&
"New filter but not a new instruction!");
2615 CleanupFlag =
false;
2626 for (
unsigned i = 0, e = NewClauses.
size();
i + 1 < e; ) {
2629 for (j =
i; j != e; ++j)
2630 if (!isa<ArrayType>(NewClauses[j]->
getType()))
2636 for (
unsigned k =
i; k + 1 < j; ++k)
2640 std::stable_sort(NewClauses.
begin() +
i, NewClauses.
begin() + j,
2642 MakeNewInstruction =
true;
2661 for (
unsigned i = 0;
i + 1 < NewClauses.
size(); ++
i) {
2671 for (
unsigned j = NewClauses.
size() - 1; j !=
i; --j) {
2672 Value *LFilter = NewClauses[j];
2683 NewClauses.
erase(J);
2684 MakeNewInstruction =
true;
2694 if (isa<ConstantAggregateZero>(LFilter)) {
2697 if (isa<ConstantAggregateZero>(Filter)) {
2698 assert(FElts <= LElts &&
"Should have handled this case earlier!");
2700 NewClauses.
erase(J);
2701 MakeNewInstruction =
true;
2707 if (isa<ConstantAggregateZero>(Filter)) {
2710 assert(FElts > 0 &&
"Should have eliminated the empty filter earlier!");
2711 for (
unsigned l = 0; l != LElts; ++l)
2714 NewClauses.
erase(J);
2715 MakeNewInstruction =
true;
2726 bool AllFound =
true;
2727 for (
unsigned f = 0; f != FElts; ++f) {
2730 for (
unsigned l = 0; l != LElts; ++l) {
2732 if (LTypeInfo == FTypeInfo) {
2742 NewClauses.
erase(J);
2743 MakeNewInstruction =
true;
2751 if (MakeNewInstruction) {
2754 for (
unsigned i = 0, e = NewClauses.
size();
i != e; ++
i)
2759 if (NewClauses.
empty())
2768 assert(!CleanupFlag &&
"Adding a cleanup, not removing one?!");
2785 isa<TerminatorInst>(
I))
2789 if (isa<AllocaInst>(I) && I->
getParent() ==
2798 if (
auto *CI = dyn_cast<CallInst>(I)) {
2799 if (CI->isConvergent())
2808 if (Scan->mayWriteToMemory())
2819 while (!Worklist.isEmpty()) {
2821 if (I ==
nullptr)
continue;
2825 DEBUG(
dbgs() <<
"IC: DCE: " << *I <<
'\n');
2826 eraseInstFromFunction(*I);
2828 MadeIRChange =
true;
2836 DEBUG(
dbgs() <<
"IC: ConstFold to: " << *C <<
" from: " << *I <<
'\n');
2839 replaceInstUsesWith(*I, C);
2842 eraseInstFromFunction(*I);
2843 MadeIRChange =
true;
2853 APInt KnownZero(BitWidth, 0);
2854 APInt KnownOne(BitWidth, 0);
2856 if ((KnownZero | KnownOne).isAllOnesValue()) {
2858 DEBUG(
dbgs() <<
"IC: ConstFold (all bits known) to: " << *C <<
2859 " from: " << *I <<
'\n');
2862 replaceInstUsesWith(*I, C);
2865 eraseInstFromFunction(*I);
2866 MadeIRChange =
true;
2878 if (
PHINode *PN = dyn_cast<PHINode>(UserInst))
2883 if (UserParent != BB) {
2884 bool UserIsSuccessor =
false;
2887 if (*SI == UserParent) {
2888 UserIsSuccessor =
true;
2895 if (UserIsSuccessor && UserParent->getUniquePredecessor()) {
2898 DEBUG(
dbgs() <<
"IC: Sink: " << *I <<
'\n');
2899 MadeIRChange =
true;
2904 if (
Instruction *OpI = dyn_cast<Instruction>(U.get()))
2912 Builder->SetInsertPoint(I);
2913 Builder->SetCurrentDebugLocation(I->
getDebugLoc());
2919 DEBUG(
dbgs() <<
"IC: Visiting: " << OrigI <<
'\n');
2925 DEBUG(
dbgs() <<
"IC: Old = " << *I <<
'\n'
2926 <<
" New = " << *Result <<
'\n');
2934 Result->takeName(I);
2937 Worklist.Add(Result);
2938 Worklist.AddUsersToWorkList(*Result);
2946 if (!isa<PHINode>(Result) && isa<PHINode>(InsertPos))
2951 eraseInstFromFunction(*I);
2953 DEBUG(
dbgs() <<
"IC: Mod = " << OrigI <<
'\n'
2954 <<
" New = " << *I <<
'\n');
2959 eraseInstFromFunction(*I);
2962 Worklist.AddUsersToWorkList(*I);
2965 MadeIRChange =
true;
2970 return MadeIRChange;
2986 bool MadeIRChange =
false;
2997 if (!Visited.
insert(BB).second)
3006 DEBUG(
dbgs() <<
"IC: DCE: " << *Inst <<
'\n');
3015 DEBUG(
dbgs() <<
"IC: ConstFold to: " << *C <<
" from: "
3027 if (!isa<ConstantVector>(
i) && !isa<ConstantExpr>(
i))
3030 auto *C = cast<Constant>(
i);
3031 Constant *&FoldRes = FoldedConstants[
C];
3039 MadeIRChange =
true;
3043 InstrsForInstCombineWorklist.
push_back(Inst);
3049 if (
BranchInst *BI = dyn_cast<BranchInst>(TI)) {
3051 bool CondVal = cast<ConstantInt>(BI->
getCondition())->getZExtValue();
3056 }
else if (
SwitchInst *SI = dyn_cast<SwitchInst>(TI)) {
3061 if (
i.getCaseValue() == Cond) {
3068 Worklist.
push_back(SI->getDefaultDest());
3075 }
while (!Worklist.
empty());
3084 return MadeIRChange;
3095 bool MadeIRChange =
false;
3108 if (Visited.count(&BB))
3112 MadeIRChange |= NumDeadInstInBB > 0;
3113 NumDeadInst += NumDeadInstInBB;
3116 return MadeIRChange;
3123 bool ExpensiveCombines =
true,
3135 using namespace llvm::PatternMatch;
3136 if (
match(I, m_Intrinsic<Intrinsic::assume>()))
3148 DEBUG(
dbgs() <<
"\n\nINSTCOMBINE ITERATION #" << Iteration <<
" on "
3154 AA, AC, TLI, DT, DL, LI);
3155 Changed |= IC.
run();
3161 return DbgDeclaresChanged || Iteration > 1;
3174 ExpensiveCombines, LI))
3198 if (skipFunction(F))
3202 auto AA = &getAnalysis<AAResultsWrapperPass>().getAAResults();
3203 auto &AC = getAnalysis<AssumptionCacheTracker>().getAssumptionCache(F);
3204 auto &TLI = getAnalysis<TargetLibraryInfoWrapperPass>().getTLI();
3205 auto &DT = getAnalysis<DominatorTreeWrapperPass>().getDomTree();
3208 auto *LIWP = getAnalysisIfAvailable<LoopInfoWrapperPass>();
3209 auto *LI = LIWP ? &LIWP->getLoopInfo() :
nullptr;
3212 ExpensiveCombines, LI);
3217 "Combine redundant instructions",
false,
false)
Legacy wrapper pass to provide the GlobalsAAResult object.
static unsigned getBitWidth(Type *Ty, const DataLayout &DL)
Returns the bitwidth of the given scalar or pointer type (if unknown returns 0).
A vector constant whose element type is a simple 1/2/4/8-byte integer or float/double, and whose elements are just simple data values (i.e.
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...
Return a value (possibly void), from a function.
SymbolTableList< Instruction >::iterator eraseFromParent()
This method unlinks 'this' from the containing basic block and deletes it.
void computeKnownBits(const Value *V, APInt &KnownZero, APInt &KnownOne, const DataLayout &DL, unsigned Depth=0, AssumptionCache *AC=nullptr, const Instruction *CxtI=nullptr, const DominatorTree *DT=nullptr)
Determine which bits of V are known to be either zero or one and return them in the KnownZero/KnownOn...
void push_back(const T &Elt)
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...
A parsed version of the target data layout string in and methods for querying it. ...
Type * getIndexedType() const
static bool prepareICWorklistFromFunction(Function &F, const DataLayout &DL, TargetLibraryInfo *TLI, InstCombineWorklist &ICWorklist)
Populate the IC worklist from a function, and prune any dead basic blocks discovered in the process...
void copyFastMathFlags(FastMathFlags FMF)
Convenience function for transferring all fast-math flag values to this instruction, which must be an operator which supports these flags.
Instruction::CastOps getOpcode() const
Return the opcode of this CastInst.
class_match< Value > m_Value()
Match an arbitrary value and ignore it.
This class is the base class for the comparison instructions.
void setFastMathFlags(FastMathFlags FMF)
Convenience function for setting multiple fast-math flags on this instruction, which must be an opera...
AnalysisUsage & addPreserved()
Add the specified Pass class to the set of analyses preserved by this pass.
static IntegerType * getInt1Ty(LLVMContext &C)
void addIncoming(Value *V, BasicBlock *BB)
Add an incoming value to the end of the PHI list.
LLVMContext & getContext() const
getContext - Return a reference to the LLVMContext associated with this function. ...
Value * CreateBinOp(Instruction::BinaryOps Opc, Value *LHS, Value *RHS, const Twine &Name="", MDNode *FPMathTag=nullptr)
iterator_range< CaseIt > cases()
Iteration adapter for range-for loops.
BinaryOp_match< LHS, RHS, Instruction::Sub > m_Sub(const LHS &L, const RHS &R)
static const Value * getFNegArgument(const Value *BinOp)
Type * getSourceElementType() const
void LLVMInitializeInstCombine(LLVMPassRegistryRef R)
STATISTIC(NumFunctions,"Total number of functions")
bool isVolatile() const
Return true if this is a store to a volatile memory location.
void swapSuccessors()
Swap the successors of this branch instruction.
This is the interface for a simple mod/ref and alias analysis over globals.
A Module instance is used to store all the information related to an LLVM module. ...
Constant * getClause(unsigned Idx) const
Get the value of the clause at index Idx.
static void ClearSubclassDataAfterReassociation(BinaryOperator &I)
Conservatively clears subclassOptionalData after a reassociation or commutation.
match_zero m_Zero()
Match an arbitrary zero/null constant.
This file provides the primary interface to the instcombine pass.
INITIALIZE_PASS_BEGIN(InstructionCombiningPass,"instcombine","Combine redundant instructions", false, false) INITIALIZE_PASS_END(InstructionCombiningPass
unsigned getNumOperands() const
bool hasNoSignedWrap() const
Test whether this operation is known to never undergo signed overflow, aka the nsw property...
static Value * foldOperationIntoSelectOperand(Instruction &I, Value *SO, InstCombiner *IC)
unsigned getPointerAddressSpace() const
Returns the address space of the pointer operand.
bool isNonNegative() const
Determine if this APInt Value is non-negative (>= 0)
br_match m_UnconditionalBr(BasicBlock *&Succ)
A global registry used in conjunction with static constructors to make pluggable components (like tar...
static Value * tryFactorization(InstCombiner::BuilderTy *Builder, const DataLayout &DL, BinaryOperator &I, Instruction::BinaryOps InnerOpcode, Value *A, Value *B, Value *C, Value *D)
This tries to simplify binary operations by factorizing out common terms (e.
This class represents a function call, abstracting a target machine's calling convention.
Predicate getInversePredicate() const
For example, EQ -> NE, UGT -> ULE, SLT -> SGE, OEQ -> UNE, UGT -> OLE, OLT -> UGE, etc.
An immutable pass that tracks lazily created AssumptionCache objects.
class_match< Constant > m_Constant()
Match an arbitrary Constant and ignore it.
gep_type_iterator gep_type_end(const User *GEP)
BinaryOp_match< LHS, RHS, Instruction::AShr > m_AShr(const LHS &L, const RHS &R)
bool mayHaveSideEffects() const
Return true if the instruction may have side effects.
A cache of .assume calls within a function.
static void sdivrem(const APInt &LHS, const APInt &RHS, APInt &Quotient, APInt &Remainder)
This instruction constructs a fixed permutation of two input vectors.
static SelectInst * Create(Value *C, Value *S1, Value *S2, const Twine &NameStr="", Instruction *InsertBefore=nullptr, Instruction *MDFrom=nullptr)
struct LLVMOpaquePassRegistry * LLVMPassRegistryRef
void Add(Instruction *I)
Add - Add the specified instruction to the worklist if it isn't already in it.
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 bool isEquality(Predicate P)
Return true if this predicate is either EQ or NE.
FastMathFlags getFastMathFlags() const
Convenience function for getting all the fast-math flags, which must be an operator which supports th...
const Function * getParent() const
Return the enclosing method, or null if none.
Analysis pass which computes a DominatorTree.
unsigned getAddressSpace() const
Return the address space of the Pointer type.
An instruction for reading from memory.
FunctionType * getType(LLVMContext &Context, ID id, ArrayRef< Type * > Tys=None)
Return the function type for an intrinsic.
static Constant * getCompare(unsigned short pred, Constant *C1, Constant *C2, bool OnlyIfReduced=false)
Return an ICmp or FCmp comparison operator constant expression.
static Constant * getSub(Constant *C1, Constant *C2, bool HasNUW=false, bool HasNSW=false)
iv Induction Variable Users
Type * getElementType() const
void reserve(size_type N)
static bool MaintainNoSignedWrap(BinaryOperator &I, Value *B, Value *C)
static Value * CreateBinOpAsGiven(BinaryOperator &Inst, Value *LHS, Value *RHS, InstCombiner::BuilderTy *B)
Creates node of binary operation with the same attributes as the specified one but with other operand...
unsigned getNumIndices() const
APInt ssub_ov(const APInt &RHS, bool &Overflow) const
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...
bool optForMinSize() const
Optimize this function for minimum size (-Oz).
Constant * getMask() const
const CallInst * isFreeCall(const Value *I, const TargetLibraryInfo *TLI)
isFreeCall - Returns non-null if the value is a call to the builtin free()
static Constant * getNullValue(Type *Ty)
Constructor to create a '0' constant of arbitrary type.
StringRef getName() const
Return a constant reference to the value's name.
A templated base class for SmallPtrSet which provides the typesafe interface that is common across al...
iterator begin()
Instruction iterator methods.
bool swapOperands()
Exchange the two operands to this instruction.
PassT::Result * getCachedResult(IRUnitT &IR) const
Get the cached result of an analysis pass for a given IR unit.
bool match(Val *V, const Pattern &P)
AnalysisUsage & addRequired()
Used to lazily calculate structure layout information for a target machine, based on the DataLayout s...
#define INITIALIZE_PASS_DEPENDENCY(depName)
static const Value * getNegArgument(const Value *BinOp)
Helper functions to extract the unary argument of a NEG, FNEG or NOT operation implemented via Sub...
CmpClass_match< LHS, RHS, FCmpInst, FCmpInst::Predicate > m_FCmp(FCmpInst::Predicate &Pred, const LHS &L, const RHS &R)
bool isUnconditional() const
This class represents a conversion between pointers from one address space to another.
static unsigned getComplexity(Value *V)
Assign a complexity or rank value to LLVM Values.
This class represents the LLVM 'select' instruction.
bool isIdenticalTo(const Instruction *I) const
Return true if the specified instruction is exactly identical to the current one. ...
struct fuzzer::@269 Flags
Attribute unwrap(LLVMAttributeRef Attr)
This is the base class for all instructions that perform data casts.
ArrayRef< T > makeArrayRef(const T &OneElt)
Construct an ArrayRef from a single element.
void print(raw_ostream &O, bool IsForDebug=false) const
Implement operator<< on Value.
Class to represent struct types.
Type * getArrayElementType() const
A Use represents the edge between a Value definition and its users.
static GCRegistry::Add< StatepointGC > D("statepoint-example","an example strategy for statepoint")
static Constant * get(ArrayRef< Constant * > V)
This provides a uniform API for creating instructions and inserting them into a basic block: either a...
The core instruction combiner logic.
Constant * getPersonalityFn() const
Get the personality function associated with this function.
0 1 0 1 True if ordered and less than or equal
static cl::opt< bool > EnableExpensiveCombines("expensive-combines", cl::desc("Enable expensive instruction combines"))
Analysis pass that exposes the LoopInfo for a function.
Instruction * clone() const
Create a copy of 'this' instruction that is identical in all ways except the following: ...
static bool shorter_filter(const Value *LHS, const Value *RHS)
Value * stripAndAccumulateInBoundsConstantOffsets(const DataLayout &DL, APInt &Offset)
Accumulate offsets from stripInBoundsConstantOffsets().
Type * getPointerOperandType() const
Method to return the pointer operand as a PointerType.
not_match< LHS > m_Not(const LHS &L)
BinaryOp_match< LHS, RHS, Instruction::Add > m_Add(const LHS &L, const RHS &R)
LLVM_NODISCARD bool empty() const
Instruction * visitReturnInst(ReturnInst &RI)
Interval::succ_iterator succ_begin(Interval *I)
succ_begin/succ_end - define methods so that Intervals may be used just like BasicBlocks can with the...
Instruction * visitBranchInst(BranchInst &BI)
Constant * ConstantFoldConstant(const Constant *C, const DataLayout &DL, const TargetLibraryInfo *TLI=nullptr)
ConstantFoldConstant - Attempt to fold the constant using the specified DataLayout.
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 GetElementPtrInst * CreateInBounds(Value *Ptr, ArrayRef< Value * > IdxList, const Twine &NameStr="", Instruction *InsertBefore=nullptr)
Create an "inbounds" getelementptr.
void setCleanup(bool V)
Indicate that this landingpad instruction is a cleanup.
bool mayReadFromMemory() const
Return true if this instruction may read memory.
static bool isNeverEqualToUnescapedAlloc(Value *V, const TargetLibraryInfo *TLI, Instruction *AI)
LLVMContext & getContext() const
Return the LLVMContext in which this type was uniqued.
bool isAssociative() const
Return true if the instruction is associative:
Class to represent array types.
This instruction compares its operands according to the predicate given to the constructor.
BasicBlock * getSuccessor(unsigned i) const
This class represents a no-op cast from one type to another.
class_match< ConstantInt > m_ConstantInt()
Match an arbitrary ConstantInt and ignore it.
bool isFloatingPointTy() const
Return true if this is one of the six floating-point types.
TargetFolder - Create constants with target dependent folding.
Function * getDeclaration(Module *M, ID id, ArrayRef< Type * > Tys=None)
Create or insert an LLVM Function declaration for an intrinsic, and return it.
unsigned getNumClauses() const
Get the number of clauses for this landing pad.
static GCRegistry::Add< OcamlGC > B("ocaml","ocaml 3.10-compatible GC")
An instruction for storing to memory.
void replaceAllUsesWith(Value *V)
Change all uses of this to point to a new Value.
bool isArrayTy() const
True if this is an instance of ArrayType.
static bool AddReachableCodeToWorklist(BasicBlock *BB, const DataLayout &DL, SmallPtrSetImpl< BasicBlock * > &Visited, InstCombineWorklist &ICWorklist, const TargetLibraryInfo *TLI)
Walk the function in depth-first order, adding all reachable code to the worklist.
FunctionPass * createInstructionCombiningPass(bool ExpensiveCombines=true)
void takeName(Value *V)
Transfer the name from V to this value.
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree...
Type * getScalarType() const LLVM_READONLY
If this is a vector type, return the element type, otherwise return 'this'.
Type * getElementType() const
static BinaryOperator * CreateAdd(Value *S1, Value *S2, const Twine &Name, Instruction *InsertBefore, Value *FlagsOp)
Maximum length of the test input libFuzzer tries to guess a good value based on the corpus and reports it always prefer smaller inputs during the corpus shuffle When libFuzzer itself reports a bug this exit code will be used If indicates the maximal total time in seconds to run the fuzzer minimizes the provided crash input Use with etc Experimental Use value profile to guide fuzzing Number of simultaneous worker processes to run the jobs If min(jobs, NumberOfCpuCores()/2)\" is used.") FUZZER_FLAG_INT(reload
bool isInBounds() const
Determine whether the GEP has the inbounds flag.
Class to represent pointers.
static GCRegistry::Add< CoreCLRGC > E("coreclr","CoreCLR-compatible GC")
Interval::succ_iterator succ_end(Interval *I)
unsigned getNumIncomingValues() const
Return the number of incoming edges.
PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM)
uint64_t getElementOffset(unsigned Idx) const
bool isSized(SmallPtrSetImpl< Type * > *Visited=nullptr) const
Return true if it makes sense to take the size of this type.
an instruction for type-safe pointer arithmetic to access elements of arrays and structs ...
bool isFilter(unsigned Idx) const
Return 'true' if the clause and index Idx is a filter clause.
bool isIntOrIntVectorTy() const
Return true if this is an integer type or a vector of integer types.
static Constant * getFNeg(Constant *C)
The landingpad instruction holds all of the information necessary to generate correct exception handl...
static Instruction::BinaryOps getBinOpsForFactorization(Instruction::BinaryOps TopLevelOpcode, BinaryOperator *Op, Value *&LHS, Value *&RHS)
This function factors binary ops which can be combined using distributive laws.
Subclasses of this class are all able to terminate a basic block.
A set of analyses that are preserved following a run of a transformation pass.
apint_match m_APInt(const APInt *&Res)
Match a ConstantInt or splatted ConstantVector, binding the specified pointer to the contained APInt...
BinaryOp_match< LHS, RHS, Instruction::SDiv > m_SDiv(const LHS &L, const RHS &R)
Constant * stripPointerCasts()
bool run()
Run the combiner over the entire worklist until it is empty.
APInt trunc(unsigned width) const
Truncate to new width.
PassT::Result & getResult(IRUnitT &IR, ExtraArgTs...ExtraArgs)
Get the result of an analysis pass for a given IR unit.
ConstantInt * lowerObjectSizeCall(IntrinsicInst *ObjectSize, const DataLayout &DL, const TargetLibraryInfo *TLI, bool MustSucceed)
Try to turn a call to .objectsize into an integer value of the given Type.
LLVM Basic Block Representation.
The instances of the Type class are immutable: once they are created, they are never changed...
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 *...
Conditional or Unconditional Branch instruction.
bool isVectorTy() const
True if this is an instance of VectorType.
static bool isCatchAll(EHPersonality Personality, Constant *TypeInfo)
Return 'true' if the given typeinfo will match anything.
This is an important base class in LLVM.
const Value * getCondition() const
LLVM_ATTRIBUTE_ALWAYS_INLINE iterator begin()
int64_t getSExtValue() const
Get sign extended value.
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)".
APInt Or(const APInt &LHS, const APInt &RHS)
Bitwise OR function for APInt.
bool hasNoSignedWrap() const
Determine whether the no signed wrap flag is set.
static Constant * get(ArrayType *T, ArrayRef< Constant * > V)
ConstantFP - Floating Point Values [float, double].
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
APInt Xor(const APInt &LHS, const APInt &RHS)
Bitwise XOR function for APInt.
Constant * ConstantFoldInstruction(Instruction *I, const DataLayout &DL, const TargetLibraryInfo *TLI=nullptr)
ConstantFoldInstruction - Try to constant fold the specified instruction.
Value * getRawDest() const
specificval_ty m_Specific(const Value *V)
Match if we have a specific specified value.
const DebugLoc & getDebugLoc() const
Return the debug location for this node as a DebugLoc.
APInt sext(unsigned width) const
Sign extend to a new width.
EHPersonality classifyEHPersonality(const Value *Pers)
See if the given exception handling personality function is one that we understand.
brc_match< Cond_t > m_Br(const Cond_t &C, BasicBlock *&T, BasicBlock *&F)
Represent the analysis usage information of a pass.
BasicBlock * getIncomingBlock(unsigned i) const
Return incoming basic block number i.
const InstListType & getInstList() const
Return the underlying instruction list container.
static GCMetadataPrinterRegistry::Add< ErlangGCPrinter > X("erlang","erlang-compatible garbage collector")
This instruction compares its operands according to the predicate given to the constructor.
Predicate
This enumeration lists the possible predicates for CmpInst subclasses.
Utility class for integer arithmetic operators which may exhibit overflow - Add, Sub, and Mul.
INITIALIZE_PASS_END(RegBankSelect, DEBUG_TYPE,"Assign register bank of generic virtual registers", false, false) RegBankSelect
unsigned getBitWidth() const
Return the number of bits in the APInt.
uint64_t getNumElements() const
void addClause(Constant *ClauseVal)
Add a catch or filter clause to the landing pad.
FunctionPass class - This class is used to implement most global optimizations.
Value * getOperand(unsigned i) const
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - This function should be overriden by passes that need analysis information to do t...
static CastInst * CreatePointerBitCastOrAddrSpaceCast(Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd)
Create a BitCast or an AddrSpaceCast cast instruction.
bool isCommutative() const
Return true if the instruction is commutative:
bool isPotentiallyReachable(const Instruction *From, const Instruction *To, const DominatorTree *DT=nullptr, const LoopInfo *LI=nullptr)
Determine whether instruction 'To' is reachable from 'From', returning true if uncertain.
self_iterator getIterator()
Class to represent integer types.
The legacy pass manager's instcombine pass.
static Constant * getNot(Constant *C)
void setSourceElementType(Type *Ty)
Constant * getAggregateElement(unsigned Elt) const
For aggregates (struct/array/vector) return the constant that corresponds to the specified element if...
void append(in_iter in_start, in_iter in_end)
Add the specified range to the end of the SmallVector.
InstCombineWorklist - This is the worklist management logic for InstCombine.
static UndefValue * get(Type *T)
Static factory methods - Return an 'undef' object of the specified type.
iterator erase(const_iterator CI)
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
bool runOnFunction(Function &F) override
runOnFunction - Virtual method overriden by subclasses to do the per-function processing of the pass...
LLVMContext & getContext() const
All values hold a context through their type.
bool LowerDbgDeclare(Function &F)
Lowers llvm.dbg.declare intrinsics into appropriate set of llvm.dbg.value intrinsics.
const Value * getTrueValue() const
static GetElementPtrInst * Create(Type *PointeeType, Value *Ptr, ArrayRef< Value * > IdxList, const Twine &NameStr="", Instruction *InsertBefore=nullptr)
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
static Constant * getIntegerValue(Type *Ty, const APInt &V)
Return the value for an integer or pointer constant, or a vector thereof, with the given scalar value...
unsigned getAddressSpace() const
Returns the address space of this instruction's pointer type.
neg_match< LHS > m_Neg(const LHS &L)
Match an integer negate.
bool isConditional() const
static InvokeInst * Create(Value *Func, BasicBlock *IfNormal, BasicBlock *IfException, ArrayRef< Value * > Args, const Twine &NameStr, Instruction *InsertBefore=nullptr)
A function analysis which provides an AssumptionCache.
BinaryOps getOpcode() const
static IntegerType * get(LLVMContext &C, unsigned NumBits)
This static method is the primary way of constructing an IntegerType.
void setHasNoSignedWrap(bool b=true)
Set or clear the nsw flag on this instruction, which must be an operator which supports this flag...
This is the common base class for memset/memcpy/memmove.
Iterator for intrusive lists based on ilist_node.
static PointerType * getInt1PtrTy(LLVMContext &C, unsigned AS=0)
This is the shared class of boolean and integer constants.
static bool simplifyAssocCastAssoc(BinaryOperator *BinOp1)
Combine constant operands of associative operations either before or after a cast to eliminate one of...
Value * getIncomingValue(unsigned i) const
Return incoming value number x.
Value * DoPHITranslation(const BasicBlock *CurBB, const BasicBlock *PredBB)
Translate PHI node to its predecessor from the given basic block.
unsigned getScalarSizeInBits() const LLVM_READONLY
If this is a vector type, return the getPrimitiveSizeInBits value for the element type...
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small...
Utility class for floating point operations which can have information about relaxed accuracy require...
This is a utility class that provides an abstraction for the common functionality between Instruction...
Type * getType() const
All values are typed, get the type of this value.
Instruction * user_back()
Specialize the methods defined in Value, as we know that an instruction can only be used by other ins...
Provides information about what library functions are available for the current target.
bool hasAllZeroIndices() const
Return true if all of the indices of this GEP are zeros.
LLVM_NODISCARD T pop_back_val()
uint64_t getSizeInBytes() const
Value * stripPointerCasts()
Strip off pointer casts, all-zero GEPs, and aliases.
unsigned getElementContainingOffset(uint64_t Offset) const
Given a valid byte offset into the structure, returns the structure index that contains it...
Instruction * visitFree(CallInst &FI)
static Constant * get(Type *Ty, uint64_t V, bool isSigned=false)
If Ty is a vector type, return a Constant with a splat of the given value.
static PHINode * Create(Type *Ty, unsigned NumReservedValues, const Twine &NameStr="", Instruction *InsertBefore=nullptr)
Constructors - NumReservedValues is a hint for the number of incoming edges that this phi node will h...
void setPreservesCFG()
This function should be called by the pass, iff they do not:
int32_t exactLogBase2() const
const BasicBlock & getEntryBlock() const
void initializeInstCombine(PassRegistry &)
Initialize all passes linked into the InstCombine library.
unsigned removeAllNonTerminatorAndEHPadInstructions(BasicBlock *BB)
Remove all instructions from a basic block other than it's terminator and any present EH pad instruct...
bool isNullValue() const
Return true if this is the value that would be returned by getNullValue.
static ConstantInt * getTrue(LLVMContext &Context)
void setPredicate(Predicate P)
Set the predicate for this instruction to the specified value.
static GCRegistry::Add< ShadowStackGC > C("shadow-stack","Very portable GC for uncooperative code generators")
void setOperand(unsigned i, Value *Val)
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
bool accumulateConstantOffset(const DataLayout &DL, APInt &Offset) const
Accumulate the constant address offset of this GEP if possible.
Value * getArgOperand(unsigned i) const
getArgOperand/setArgOperand - Return/set the i-th call argument.
Class to represent vector types.
static bool isNeg(const Value *V)
Check if the given Value is a NEG, FNeg, or NOT instruction.
ConstantArray - Constant Array Declarations.
Class for arbitrary precision integers.
static bool combineInstructionsOverFunction(Function &F, InstCombineWorklist &Worklist, AliasAnalysis *AA, AssumptionCache &AC, TargetLibraryInfo &TLI, DominatorTree &DT, bool ExpensiveCombines=true, LoopInfo *LI=nullptr)
bool isIntegerTy() const
True if this is an instance of IntegerType.
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.
CastClass_match< OpTy, Instruction::PtrToInt > m_PtrToInt(const OpTy &Op)
Matches PtrToInt.
iterator_range< user_iterator > users()
BasicBlock * getSinglePredecessor()
Return the predecessor of this block if it has a single predecessor block.
Instruction * visitSwitchInst(SwitchInst &SI)
Value * SimplifyBinOp(unsigned Opcode, Value *LHS, Value *RHS, const DataLayout &DL, const TargetLibraryInfo *TLI=nullptr, const DominatorTree *DT=nullptr, AssumptionCache *AC=nullptr, const Instruction *CxtI=nullptr)
Given operands for a BinaryOperator, fold the result or return null.
unsigned getVectorNumElements() const
void AddInitialGroup(ArrayRef< Instruction * > List)
AddInitialGroup - Add the specified batch of stuff in reverse order.
static bool TryToSinkInstruction(Instruction *I, BasicBlock *DestBlock)
Try to move the specified instruction from its current block into the beginning of DestBlock...
Instruction * visitExtractValueInst(ExtractValueInst &EV)
static Constant * getCast(unsigned ops, Constant *C, Type *Ty, bool OnlyIfReduced=false)
Convenience function for getting a Cast operation.
Value * getCondition() const
bool isMinValue() const
Determine if this is the smallest unsigned value.
Common super class of ArrayType, StructType and VectorType.
APInt And(const APInt &LHS, const APInt &RHS)
Bitwise AND function for APInt.
bool isStructTy() const
True if this is an instance of StructType.
Instruction * visitLandingPadInst(LandingPadInst &LI)
bool isEHPad() const
Return true if the instruction is a variety of EH-block.
static Constant * getNeg(Constant *C, bool HasNUW=false, bool HasNSW=false)
Value * SimplifyGEPInst(Type *SrcTy, ArrayRef< Value * > Ops, const DataLayout &DL, const TargetLibraryInfo *TLI=nullptr, const DominatorTree *DT=nullptr, AssumptionCache *AC=nullptr, const Instruction *CxtI=nullptr)
Given operands for a GetElementPtrInst, fold the result or return null.
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 countLeadingOnes() const
Count the number of leading one bits.
const DataLayout & getDataLayout() const
Get the data layout for the module's target platform.
Provides an 'InsertHelper' that calls a user-provided callback after performing the default insertion...
Value * getCondition() const
iterator insert(iterator where, pointer New)
void emplace_back(ArgTypes &&...Args)
void registerAssumption(CallInst *CI)
Add an .assume intrinsic to this function's cache.
bool isMinSignedValue() const
Determine if this is the smallest signed value.
static IntegerType * getInt32Ty(LLVMContext &C)
void setCondition(Value *V)
unsigned greater or equal
static bool isFNeg(const Value *V, bool IgnoreZeroSign=false)
static InsertValueInst * Create(Value *Agg, Value *Val, ArrayRef< unsigned > Idxs, const Twine &NameStr="", Instruction *InsertBefore=nullptr)
bool isCatch(unsigned Idx) const
Return 'true' if the clause and index Idx is a catch clause.
TerminatorInst * getTerminator()
Returns the terminator instruction if the block is well formed or null if the block is not well forme...
LLVM_ATTRIBUTE_ALWAYS_INLINE size_type size() const
bool hasOneUse() const
Return true if there is exactly one user of this 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...
0 1 1 0 True if ordered and operands are unequal
static ArrayType * get(Type *ElementType, uint64_t NumElements)
This static method is the primary way to construct an ArrayType.
LLVM_NODISCARD std::enable_if<!is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
static Constant * getShl(Constant *C1, Constant *C2, bool HasNUW=false, bool HasNSW=false)
VectorType * getType() const
Overload to return most specific vector type.
void preserve()
Mark an analysis as preserved.
APInt sadd_ov(const APInt &RHS, bool &Overflow) const
Value * SimplifyExtractValueInst(Value *Agg, ArrayRef< unsigned > Idxs, const DataLayout &DL, const TargetLibraryInfo *TLI=nullptr, const DominatorTree *DT=nullptr, AssumptionCache *AC=nullptr, const Instruction *CxtI=nullptr)
Given operands for an ExtractValueInst, fold the result or return null.
void initializeInstructionCombiningPassPass(PassRegistry &)
void setCondition(Value *V)
Analysis pass providing the TargetLibraryInfo.
Value * CreateCast(Instruction::CastOps Op, Value *V, Type *DestTy, const Twine &Name="")
static bool isAllocSiteRemovable(Instruction *AI, SmallVectorImpl< WeakVH > &Users, const TargetLibraryInfo *TLI)
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
user_iterator user_begin()
const BasicBlock & front() const
bool isSafeToSpeculativelyExecute(const Value *V, const Instruction *CtxI=nullptr, const DominatorTree *DT=nullptr)
Return true if the instruction does not have any effects besides calculating the result and does not ...
A raw_ostream that writes to an std::string.
unsigned getPrimitiveSizeInBits() const LLVM_READONLY
Return the basic size of this type if it is a primitive type.
Module * getParent()
Get the module that this global value is contained inside of...
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...
LLVM Value Representation.
This file provides internal interfaces used to implement the InstCombine.
unsigned getOpcode() const
Returns a member of one of the enums like Instruction::Add.
void clearSubclassOptionalData()
Clear the optional flags contained in this value.
static VectorType * get(Type *ElementType, unsigned NumElements)
This static method is the primary way to construct an VectorType.
#define LLVM_FALLTHROUGH
LLVM_FALLTHROUGH - Mark fallthrough cases in switch statements.
void moveBefore(Instruction *MovePos)
Unlink this instruction from its current basic block and insert it into the basic block that MovePos ...
static Instruction * tryToMoveFreeBeforeNullTest(CallInst &FI)
Move the call to free before a NULL test.
Instruction * visitGetElementPtrInst(GetElementPtrInst &GEP)
bool isCleanup() const
Return 'true' if this landingpad instruction is a cleanup.
const Value * getFalseValue() const
Convenience struct for specifying and reasoning about fast-math flags.
This is the interface for LLVM's primary stateless and local alias analysis.
inst_range instructions(Function *F)
PassRegistry - This class manages the registration and intitialization of the pass subsystem as appli...
A container for analyses that lazily runs them and caches their results.
Legacy analysis pass which computes a DominatorTree.
static GCMetadataPrinterRegistry::Add< OcamlGCMetadataPrinter > Y("ocaml","ocaml 3.10-compatible collector")
iterator getFirstInsertionPt()
Returns an iterator to the first instruction in this block that is suitable for inserting a non-PHI i...
A wrapper pass to provide the legacy pass manager access to a suitably prepared AAResults object...
static BinaryOperator * CreateMul(Value *S1, Value *S2, const Twine &Name, Instruction *InsertBefore, Value *FlagsOp)
static bool shouldMergeGEPs(GEPOperator &GEP, GEPOperator &Src)
bool isBitwiseLogicOp() const
Return true if this is and/or/xor.
static void getShuffleMask(Constant *Mask, SmallVectorImpl< int > &Result)
Convert the input shuffle mask operand to a vector of integers.
0 0 1 1 True if ordered and greater than or equal
unsigned getPointerAddressSpace() const
Get the address space of this pointer or pointer vector type.
static GCRegistry::Add< ErlangGC > A("erlang","erlang-compatible garbage collector")
bool ule(const APInt &RHS) const
Unsigned less or equal comparison.
Value * getPointerOperand()
Instruction * visitAllocSite(Instruction &FI)
const BasicBlock * getParent() const
A wrapper class for inspecting calls to intrinsic functions.
This instruction inserts a struct field of array element value into an aggregate value.
CmpClass_match< LHS, RHS, ICmpInst, ICmpInst::Predicate > m_ICmp(ICmpInst::Predicate &Pred, const LHS &L, const RHS &R)
Legacy wrapper pass to provide the BasicAAResult object.
gep_type_iterator gep_type_begin(const User *GEP)