45 #define DEBUG_TYPE "instsimplify" 49 STATISTIC(NumExpand,
"Number of expansions");
50 STATISTIC(NumReassoc,
"Number of reassociations");
71 if (
auto *BO = dyn_cast<BinaryOperator>(Cond))
72 BinOpCode = BO->getOpcode();
77 if (BinOpCode == BinaryOperator::Or) {
79 }
else if (BinOpCode == BinaryOperator::And) {
101 Pred1 != Pred2 || Pred1 != ExpectedPred)
104 if (X == TrueVal || X == FalseVal || Y == TrueVal || Y == FalseVal)
105 return BinOpCode == BinaryOperator::Or ? TrueVal : FalseVal;
130 if (CPred == Pred && CLHS == LHS && CRHS == RHS)
176 if (Op0->getOpcode() == OpcodeToExpand) {
178 Value *
A = Op0->getOperand(0), *
B = Op0->getOperand(1), *
C = RHS;
185 && L ==
B &&
R == A)) {
199 if (Op1->getOpcode() == OpcodeToExpand) {
208 && L ==
C &&
R == B)) {
228 unsigned MaxRecurse) {
248 if (V == B)
return LHS;
258 if (Op1 && Op1->getOpcode() == Opcode) {
260 Value *
B = Op1->getOperand(0);
267 if (V == B)
return RHS;
290 if (V == A)
return LHS;
300 if (Op1 && Op1->getOpcode() == Opcode) {
302 Value *
B = Op1->getOperand(0);
309 if (V == C)
return RHS;
327 unsigned MaxRecurse) {
333 if (isa<SelectInst>(LHS)) {
334 SI = cast<SelectInst>(LHS);
336 assert(isa<SelectInst>(RHS) &&
"No select instruction operand!");
337 SI = cast<SelectInst>(RHS);
357 if (TV && isa<UndefValue>(TV))
359 if (FV && isa<UndefValue>(FV))
370 if ((FV && !TV) || (TV && !FV)) {
379 Value *UnsimplifiedLHS = SI == LHS ? UnsimplifiedBranch : LHS;
380 Value *UnsimplifiedRHS = SI == LHS ? RHS : UnsimplifiedBranch;
381 if (Simplified->
getOperand(0) == UnsimplifiedLHS &&
385 Simplified->
getOperand(1) == UnsimplifiedLHS &&
399 unsigned MaxRecurse) {
405 if (!isa<SelectInst>(LHS)) {
409 assert(isa<SelectInst>(LHS) &&
"Not comparing with a select instruction!");
421 TCmp =
getTrue(Cond->getType());
427 TCmp =
getTrue(Cond->getType());
481 unsigned MaxRecurse) {
487 if (isa<PHINode>(LHS)) {
488 PI = cast<PHINode>(LHS);
493 assert(isa<PHINode>(RHS) &&
"No PHI instruction operand!");
494 PI = cast<PHINode>(RHS);
501 Value *CommonValue =
nullptr;
504 if (Incoming == PI)
continue;
505 Value *V = PI == LHS ?
510 if (!V || (CommonValue && V != CommonValue))
529 if (!isa<PHINode>(LHS)) {
533 assert(isa<PHINode>(LHS) &&
"Not comparing with a phi instruction!");
534 PHINode *PI = cast<PHINode>(LHS);
541 Value *CommonValue =
nullptr;
544 if (Incoming == PI)
continue;
548 if (!V || (CommonValue && V != CommonValue))
559 if (
auto *CLHS = dyn_cast<Constant>(Op0)) {
560 if (
auto *CRHS = dyn_cast<Constant>(Op1))
652 bool AllowNonInbounds =
false) {
664 if ((!AllowNonInbounds && !
GEP->isInBounds()) ||
665 !
GEP->accumulateConstantOffset(DL, Offset))
667 V =
GEP->getPointerOperand();
669 V = cast<Operator>(V)->getOperand(0);
670 }
else if (
GlobalAlias *GA = dyn_cast<GlobalAlias>(V)) {
671 if (GA->isInterposable())
673 V = GA->getAliasee();
676 if (
Value *RV = CS.getReturnedArgOperand()) {
683 }
while (Visited.insert(V).second);
738 if (Known.Zero.isMaxSignedValue()) {
751 Value *
X =
nullptr, *
Y =
nullptr, *
Z = Op1;
849 unsigned MaxRecurse) {
885 if (isa<SelectInst>(Op0) || isa<SelectInst>(Op1))
892 if (isa<PHINode>(Op0) || isa<PHINode>(Op1))
925 for (
unsigned i = 0; i != NumElts; ++i) {
927 if (Elt && (Elt->
isNullValue() || isa<UndefValue>(Elt)))
966 Constant *
C = dyn_cast_or_null<Constant>(V);
973 unsigned MaxRecurse,
bool IsSigned) {
1031 bool IsSigned = Opcode == Instruction::SDiv;
1036 auto *Mul = cast<OverflowingBinaryOperator>(Op0);
1038 if ((IsSigned && Mul->hasNoSignedWrap()) ||
1039 (!IsSigned && Mul->hasNoUnsignedWrap()))
1064 if (isa<SelectInst>(Op0) || isa<SelectInst>(Op1))
1070 if (isa<PHINode>(Op0) || isa<PHINode>(Op1))
1074 if (
isDivZero(Op0, Op1, Q, MaxRecurse, IsSigned))
1090 if ((Opcode == Instruction::SRem &&
1092 (Opcode == Instruction::URem &&
1097 if ((Opcode == Instruction::SRem &&
1099 (Opcode == Instruction::URem &&
1105 if (isa<SelectInst>(Op0) || isa<SelectInst>(Op1))
1111 if (isa<PHINode>(Op0) || isa<PHINode>(Op1))
1116 if (
isDivZero(Op0, Op1, Q, MaxRecurse, Opcode == Instruction::SRem))
1125 unsigned MaxRecurse) {
1130 return simplifyDiv(Instruction::SDiv, Op0, Op1, Q, MaxRecurse);
1140 unsigned MaxRecurse) {
1141 return simplifyDiv(Instruction::UDiv, Op0, Op1, Q, MaxRecurse);
1151 unsigned MaxRecurse) {
1162 return simplifyRem(Instruction::SRem, Op0, Op1, Q, MaxRecurse);
1172 unsigned MaxRecurse) {
1173 return simplifyRem(Instruction::URem, Op0, Op1, Q, MaxRecurse);
1187 if (isa<UndefValue>(C))
1192 if (CI->getValue().getLimitedValue() >=
1193 CI->getType()->getScalarSizeInBits())
1197 if (isa<ConstantVector>(C) || isa<ConstantDataVector>(
C)) {
1232 if (isa<SelectInst>(Op0) || isa<SelectInst>(Op1))
1238 if (isa<PHINode>(Op0) || isa<PHINode>(Op1))
1261 unsigned MaxRecurse) {
1277 if (Op0Known.
One[0])
1334 const APInt *ShRAmt, *ShLAmt;
1337 *ShRAmt == *ShLAmt) {
1341 if (ShRAmt->
uge(EffWidthY))
1387 ICmpInst *UnsignedICmp,
bool IsAnd) {
1399 else if (
match(UnsignedICmp,
1409 return IsAnd ? UnsignedICmp : ZeroICmp;
1416 return UnsignedICmp;
1487 const APInt *C0, *C1;
1497 if (IsAnd && Range0.intersectWith(Range1).isEmptySet())
1502 if (!IsAnd && Range0.unionWith(Range1).isFullSet())
1510 if (Range0.contains(Range1))
1511 return IsAnd ? Cmp1 : Cmp0;
1512 if (Range1.contains(Range0))
1513 return IsAnd ? Cmp0 : Cmp1;
1558 const APInt *C0, *C1;
1566 auto *AddInst = cast<BinaryOperator>(Op0->
getOperand(0));
1567 if (AddInst->getOperand(1) != Op1->
getOperand(1))
1571 bool isNSW = AddInst->hasNoSignedWrap();
1572 bool isNUW = AddInst->hasNoUnsignedWrap();
1574 const APInt Delta = *C1 - *C0;
1575 if (C0->isStrictlyPositive()) {
1589 if (C0->getBoolValue() && isNUW) {
1629 const APInt *C0, *C1;
1637 auto *AddInst = cast<BinaryOperator>(Op0->
getOperand(0));
1638 if (AddInst->getOperand(1) != Op1->
getOperand(1))
1642 bool isNSW = AddInst->hasNoSignedWrap();
1643 bool isNUW = AddInst->hasNoUnsignedWrap();
1645 const APInt Delta = *C1 - *C0;
1646 if (C0->isStrictlyPositive()) {
1660 if (C0->getBoolValue() && isNUW) {
1738 if (Cast0 && Cast1 && Cast0->getOpcode() == Cast1->getOpcode() &&
1739 Cast0->getSrcTy() == Cast1->getSrcTy()) {
1740 Op0 = Cast0->getOperand(0);
1741 Op1 = Cast1->getOperand(0);
1763 if (
auto *
C = dyn_cast<Constant>(V))
1772 unsigned MaxRecurse) {
1813 (~(*Mask)).lshr(*ShAmt).isNullValue())
1819 (~(*Mask)).shl(*ShAmt).isNullValue())
1854 if (isa<SelectInst>(Op0) || isa<SelectInst>(Op1))
1861 if (isa<PHINode>(Op0) || isa<PHINode>(Op1))
1885 if (EffWidthY <= ShftCnt) {
1910 unsigned MaxRecurse) {
1999 if (isa<SelectInst>(Op0) || isa<SelectInst>(Op1))
2005 const APInt *C1, *C2;
2032 if (isa<PHINode>(Op0) || isa<PHINode>(Op1))
2046 unsigned MaxRecurse) {
2105 if (Pred == Cmp->
getPredicate() && LHS == CmpLHS && RHS == CmpRHS)
2108 LHS == CmpRHS && RHS == CmpLHS)
2223 if (isa<AllocaInst>(LHS) &&
2224 (isa<AllocaInst>(RHS) || isa<GlobalVariable>(RHS))) {
2227 uint64_t LHSSize, RHSSize;
2231 if (LHSOffsetCI && RHSOffsetCI &&
2235 const APInt &RHSOffsetValue = RHSOffsetCI->getValue();
2238 LHSOffsetValue.
ult(LHSSize) &&
2239 RHSOffsetValue.
ult(RHSSize)) {
2247 if (!cast<PointerType>(LHS->
getType())->isEmptyTy() &&
2248 !cast<PointerType>(RHS->
getType())->isEmptyTy() &&
2288 if (
const AllocaInst *AI = dyn_cast<AllocaInst>(V))
2289 return AI->getParent() && AI->getFunction() && AI->isStaticAlloca();
2290 if (
const GlobalValue *GV = dyn_cast<GlobalValue>(V))
2291 return (GV->hasLocalLinkage() || GV->hasHiddenVisibility() ||
2292 GV->hasProtectedVisibility() || GV->hasGlobalUnnamedAddr()) &&
2293 !GV->isThreadLocal();
2294 if (
const Argument *
A = dyn_cast<Argument>(V))
2295 return A->hasByValAttr();
2300 if ((IsNAC(LHSUObjs) && IsAllocDisjoint(RHSUObjs)) ||
2301 (IsNAC(RHSUObjs) && IsAllocDisjoint(LHSUObjs)))
2490 case Instruction::And:
2496 case Instruction::Or:
2502 case Instruction::AShr:
2508 unsigned ShiftAmount = Width - 1;
2514 Upper = C->
ashr(ShiftAmount) + 1;
2517 Lower = C->
ashr(ShiftAmount);
2523 case Instruction::LShr:
2529 unsigned ShiftAmount = Width - 1;
2532 Lower = C->
lshr(ShiftAmount);
2537 case Instruction::Shl:
2547 Lower = C->
shl(ShiftAmount);
2553 Upper = C->
shl(ShiftAmount) + 1;
2559 case Instruction::SDiv:
2571 Lower = IntMin.
sdiv(*C);
2572 Upper = IntMax.
sdiv(*C);
2573 if (Lower.
sgt(Upper))
2576 assert(Upper != Lower &&
"Upper part of range has wrapped!");
2582 Upper = Lower.
lshr(1) + 1;
2585 Upper = C->
abs() + 1;
2586 Lower = (-Upper) + 1;
2591 case Instruction::UDiv:
2601 case Instruction::SRem:
2605 Lower = (-Upper) + 1;
2609 case Instruction::URem:
2651 if (
auto *BO = dyn_cast<BinaryOperator>(LHS))
2655 Lower != Upper ?
ConstantRange(Lower, Upper) : ConstantRange(Width,
true);
2657 if (
auto *
I = dyn_cast<Instruction>(LHS))
2676 unsigned MaxRecurse) {
2681 if (MaxRecurse && (LBO || RBO)) {
2683 Value *
A =
nullptr, *
B =
nullptr, *
C =
nullptr, *
D =
nullptr;
2685 bool NoLHSWrapProblem =
false, NoRHSWrapProblem =
false;
2696 D = RBO->getOperand(1);
2704 if ((A == RHS ||
B == RHS) && NoLHSWrapProblem)
2711 if ((
C == LHS ||
D == LHS) && NoRHSWrapProblem)
2714 C == LHS ?
D :
C, Q, MaxRecurse - 1))
2718 if (A && C && (A == C || A ==
D ||
B == C ||
B ==
D) && NoLHSWrapProblem &&
2726 }
else if (A ==
D) {
2730 }
else if (
B == C) {
2798 if (
ConstantInt *RHSC = dyn_cast<ConstantInt>(RHS)) {
2799 if (RHSC->getValue().isStrictlyPositive()) {
2809 if (RHSC->getValue().isNonNegative()) {
2905 if (
auto *CI = dyn_cast<ConstantInt>(RHS)) {
2906 const APInt *CI2Val, *CIVal = &CI->getValue();
2935 if (MaxRecurse && LBO && RBO && LBO->
getOpcode() == RBO->getOpcode() &&
2940 case Instruction::UDiv:
2941 case Instruction::LShr:
2945 RBO->getOperand(0), Q, MaxRecurse - 1))
2948 case Instruction::SDiv:
2952 RBO->getOperand(0), Q, MaxRecurse - 1))
2955 case Instruction::AShr:
2956 if (!LBO->
isExact() || !RBO->isExact())
2959 RBO->getOperand(0), Q, MaxRecurse - 1))
2962 case Instruction::Shl: {
2970 RBO->getOperand(0), Q, MaxRecurse - 1))
2983 unsigned MaxRecurse) {
2997 (A == LHS || B == LHS)) {
3004 (A == RHS || B == RHS)) {
3012 (A == LHS || B == LHS)) {
3071 (A == LHS || B == LHS)) {
3078 (A == RHS || B == RHS)) {
3086 (A == LHS || B == LHS)) {
3140 (A == C || A == D || B == C || B == D)) {
3150 (A == C || A == D || B == C || B == D)) {
3160 (A == C || A == D || B == C || B == D)) {
3170 (A == C || A == D || B == C || B == D)) {
3190 if (
Constant *CLHS = dyn_cast<Constant>(LHS)) {
3191 if (
Constant *CRHS = dyn_cast<Constant>(RHS))
3203 if (LHS == RHS || isa<UndefValue>(RHS))
3217 if (isa<Instruction>(RHS) && isa<Instruction>(LHS)) {
3218 auto RHS_Instr = cast<Instruction>(RHS);
3219 auto LHS_Instr = cast<Instruction>(LHS);
3229 if (Satisfied_CR.contains(LHS_CR))
3234 if (InversedSatisfied_CR.contains(LHS_CR))
3240 if (isa<CastInst>(LHS) && (isa<Constant>(RHS) || isa<CastInst>(RHS))) {
3243 Type *SrcTy = SrcOp->getType();
3248 if (MaxRecurse && isa<PtrToIntInst>(LI) &&
3250 if (
Constant *RHSC = dyn_cast<Constant>(RHS)) {
3256 }
else if (
PtrToIntInst *RI = dyn_cast<PtrToIntInst>(RHS)) {
3257 if (RI->getOperand(0)->getType() == SrcTy)
3265 if (isa<ZExtInst>(LHS)) {
3268 if (
ZExtInst *RI = dyn_cast<ZExtInst>(RHS)) {
3269 if (MaxRecurse && SrcTy == RI->getOperand(0)->getType())
3272 SrcOp, RI->getOperand(0), Q,
3278 else if (
ConstantInt *CI = dyn_cast<ConstantInt>(RHS)) {
3286 if (RExt == CI && MaxRecurse)
3288 SrcOp, Trunc, Q, MaxRecurse-1))
3311 return CI->getValue().isNegative() ?
3317 return CI->getValue().isNegative() ?
3325 if (isa<SExtInst>(LHS)) {
3328 if (
SExtInst *RI = dyn_cast<SExtInst>(RHS)) {
3329 if (MaxRecurse && SrcTy == RI->getOperand(0)->getType())
3337 else if (
ConstantInt *CI = dyn_cast<ConstantInt>(RHS)) {
3345 if (RExt == CI && MaxRecurse)
3363 return CI->getValue().isNegative() ?
3368 return CI->getValue().isNegative() ?
3416 if (
auto *CLHS = dyn_cast<PtrToIntOperator>(LHS))
3417 if (
auto *CRHS = dyn_cast<PtrToIntOperator>(RHS))
3423 CLHS->getPointerOperand(),
3424 CRHS->getPointerOperand()))
3428 if (
GEPOperator *GRHS = dyn_cast<GEPOperator>(RHS)) {
3429 if (GLHS->getPointerOperand() == GRHS->getPointerOperand() &&
3430 GLHS->hasAllConstantIndices() && GRHS->hasAllConstantIndices() &&
3432 (GLHS->isInBounds() && GRHS->isInBounds() &&
3440 GLHS->getSourceElementType(), Null, IndicesLHS);
3444 GLHS->getSourceElementType(), Null, IndicesRHS);
3452 if (isa<SelectInst>(LHS) || isa<SelectInst>(RHS))
3458 if (isa<PHINode>(LHS) || isa<PHINode>(RHS))
3474 unsigned MaxRecurse) {
3478 if (
Constant *CLHS = dyn_cast<Constant>(LHS)) {
3479 if (
Constant *CRHS = dyn_cast<Constant>(RHS))
3504 "Comparison must be either ordered or unordered");
3510 if (isa<UndefValue>(LHS) || isa<UndefValue>(RHS)) {
3568 assert(!C->
isNaN() &&
"Unexpected NaN constant!");
3594 if (isa<SelectInst>(LHS) || isa<SelectInst>(RHS))
3600 if (isa<PHINode>(LHS) || isa<PHINode>(RHS))
3615 unsigned MaxRecurse) {
3621 if (isa<Constant>(Op))
3629 if (
auto *
B = dyn_cast<BinaryOperator>(
I)) {
3636 if (isa<OverflowingBinaryOperator>(
B))
3637 if (
B->hasNoSignedWrap() ||
B->hasNoUnsignedWrap())
3639 if (isa<PossiblyExactOperator>(
B))
3644 if (
B->getOperand(0) ==
Op)
3647 if (
B->getOperand(1) ==
Op)
3654 if (
CmpInst *
C = dyn_cast<CmpInst>(
I)) {
3666 if (
auto *
GEP = dyn_cast<GetElementPtrInst>(
I)) {
3670 [&](
Value *V) {
return V == Op ? RepOp : V; });
3680 if (
Constant *CRepOp = dyn_cast<Constant>(RepOp)) {
3683 for (
unsigned i = 0, e =
I->getNumOperands(); i != e; ++i) {
3684 if (
I->getOperand(i) ==
Op)
3686 else if (
Constant *COp = dyn_cast<Constant>(
I->getOperand(i)))
3693 if (ConstOps.
size() ==
I->getNumOperands()) {
3696 ConstOps[1], Q.
DL, Q.
TLI);
3698 if (
LoadInst *LI = dyn_cast<LoadInst>(
I))
3699 if (!LI->isVolatile())
3712 const APInt *
Y,
bool TrueWhenUnset) {
3719 return TrueWhenUnset ? FalseVal : TrueVal;
3725 return TrueWhenUnset ? FalseVal : TrueVal;
3732 return TrueWhenUnset ? TrueVal : FalseVal;
3738 return TrueWhenUnset ? TrueVal : FalseVal;
3762 unsigned MaxRecurse) {
3764 Value *CmpLHS, *CmpRHS;
3816 if (
auto *CondC = dyn_cast<Constant>(Cond)) {
3817 if (
auto *TrueC = dyn_cast<Constant>(TrueVal))
3818 if (
auto *FalseC = dyn_cast<Constant>(FalseVal))
3822 if (isa<UndefValue>(CondC))
3823 return isa<Constant>(FalseVal) ? FalseVal : TrueVal;
3828 if (CondC->isAllOnesValue())
3831 if (CondC->isNullValue())
3836 if (TrueVal == FalseVal)
3839 if (isa<UndefValue>(TrueVal))
3841 if (isa<UndefValue>(FalseVal))
3865 cast<PointerType>(Ops[0]->getType()->getScalarType())->getAddressSpace();
3868 if (Ops.
size() == 1)
3879 if (isa<UndefValue>(Ops[0]))
3882 if (Ops.
size() == 2) {
3884 if (
match(Ops[1],
m_Zero()) && Ops[0]->getType() == GEPTy)
3893 if (TyAllocSize == 0 && Ops[0]->
getType() == GEPTy)
3898 if (Ops[1]->
getType()->getScalarSizeInBits() ==
3900 auto PtrToIntOrZero = [GEPTy](
Value *
P) ->
Value * {
3911 if (TyAllocSize == 1 &&
3913 if (
Value *
R = PtrToIntOrZero(P))
3921 TyAllocSize == 1ULL <<
C)
3922 if (
Value *
R = PtrToIntOrZero(P))
3930 if (
Value *
R = PtrToIntOrZero(P))
3942 APInt BasePtrOffset(IdxWidth, 0);
3943 Value *StrippedBasePtr =
3944 Ops[0]->stripAndAccumulateInBoundsConstantOffsets(Q.
DL,
3963 if (!
all_of(Ops, [](
Value *V) {
return isa<Constant>(V); }))
3983 if (
Constant *CAgg = dyn_cast<Constant>(Agg))
3984 if (
Constant *CVal = dyn_cast<Constant>(Val))
3993 if (EV->getAggregateOperand()->getType() == Agg->
getType() &&
3994 EV->getIndices() == Idxs) {
3997 return EV->getAggregateOperand();
4000 if (Agg == EV->getAggregateOperand())
4019 if (VecC && ValC && IdxC)
4023 if (
auto *CI = dyn_cast<ConstantInt>(Idx)) {
4024 uint64_t NumElements = cast<VectorType>(Vec->
getType())->getNumElements();
4025 if (CI->uge(NumElements))
4030 if (isa<UndefValue>(Idx))
4040 if (
auto *CAgg = dyn_cast<Constant>(Agg))
4044 unsigned NumIdxs = Idxs.
size();
4045 for (
auto *IVI = dyn_cast<InsertValueInst>(Agg); IVI !=
nullptr;
4048 unsigned NumInsertValueIdxs = InsertValueIdxs.
size();
4049 unsigned NumCommonIdxs = std::min(NumInsertValueIdxs, NumIdxs);
4050 if (InsertValueIdxs.
slice(0, NumCommonIdxs) ==
4051 Idxs.
slice(0, NumCommonIdxs)) {
4052 if (NumIdxs == NumInsertValueIdxs)
4053 return IVI->getInsertedValueOperand();
4070 if (
auto *CVec = dyn_cast<Constant>(Vec)) {
4071 if (
auto *CIdx = dyn_cast<Constant>(Idx))
4075 if (
auto *Splat = CVec->getSplatValue())
4078 if (isa<UndefValue>(Vec))
4084 if (
auto *IdxC = dyn_cast<ConstantInt>(Idx)) {
4094 if (isa<UndefValue>(Idx))
4109 Value *CommonValue =
nullptr;
4110 bool HasUndefInput =
false;
4113 if (Incoming == PN)
continue;
4114 if (isa<UndefValue>(Incoming)) {
4116 HasUndefInput =
true;
4119 if (CommonValue && Incoming != CommonValue)
4121 CommonValue = Incoming;
4140 if (
auto *
C = dyn_cast<Constant>(Op))
4143 if (
auto *CI = dyn_cast<CastInst>(Op)) {
4144 auto *Src = CI->getOperand(0);
4145 Type *SrcTy = Src->getType();
4146 Type *MidTy = CI->getType();
4148 if (Src->getType() == Ty) {
4158 SrcIntPtrTy, MidIntPtrTy,
4159 DstIntPtrTy) == Instruction::BitCast)
4165 if (CastOpc == Instruction::BitCast)
4181 int MaskVal,
Value *RootVec,
4182 unsigned MaxRecurse) {
4193 int RootElt = MaskVal;
4194 Value *SourceOp = Op0;
4195 if (MaskVal >= InVecNumElts) {
4196 RootElt = MaskVal - InVecNumElts;
4202 if (
auto *SourceShuf = dyn_cast<ShuffleVectorInst>(SourceOp)) {
4204 DestElt, SourceShuf->getOperand(0), SourceShuf->getOperand(1),
4205 SourceShuf->getMaskValue(RootElt), RootVec, MaxRecurse);
4217 if (RootVec != SourceOp)
4222 if (RootElt != DestElt)
4230 unsigned MaxRecurse) {
4231 if (isa<UndefValue>(Mask))
4241 "Size of Indices not same as number of mask elements?");
4245 bool MaskSelects0 =
false, MaskSelects1 =
false;
4246 for (
unsigned i = 0; i != MaskNumElts; ++i) {
4247 if (Indices[i] == -1)
4249 if ((
unsigned)Indices[i] < InVecNumElts)
4250 MaskSelects0 =
true;
4252 MaskSelects1 =
true;
4263 if (Op0Const && Op1Const)
4268 if (Op0Const && !Op1Const) {
4275 if (
auto *OpShuf = dyn_cast<ShuffleVectorInst>(Op0))
4276 if (isa<UndefValue>(Op1) && RetTy == InVecTy &&
4277 OpShuf->getMask()->getSplatValue())
4283 if (
find(Indices, -1) != Indices.
end())
4290 Value *RootVec =
nullptr;
4291 for (
unsigned i = 0; i != MaskNumElts; ++i) {
4298 if (!RootVec || RootVec->
getType() != RetTy)
4321 if (isa<UndefValue>(Op0) || isa<UndefValue>(Op1))
4396 if (FMF.
noNaNs() && Op0 == Op1)
4424 if (Op0 == Op1 &&
match(Op0, m_Intrinsic<Intrinsic::sqrt>(
m_Value(X))) &&
4531 case Instruction::Sub:
4533 case Instruction::Mul:
4535 case Instruction::SDiv:
4537 case Instruction::UDiv:
4539 case Instruction::SRem:
4541 case Instruction::URem:
4543 case Instruction::Shl:
4545 case Instruction::LShr:
4547 case Instruction::AShr:
4549 case Instruction::And:
4551 case Instruction::Or:
4553 case Instruction::Xor:
4555 case Instruction::FAdd:
4557 case Instruction::FSub:
4559 case Instruction::FMul:
4561 case Instruction::FDiv:
4563 case Instruction::FRem:
4576 unsigned MaxRecurse) {
4578 case Instruction::FAdd:
4580 case Instruction::FSub:
4582 case Instruction::FMul:
4584 case Instruction::FDiv:
4616 default:
return false;
4619 case Intrinsic::fabs:
4620 case Intrinsic::floor:
4621 case Intrinsic::ceil:
4622 case Intrinsic::trunc:
4623 case Intrinsic::rint:
4624 case Intrinsic::nearbyint:
4626 case Intrinsic::canonicalize:
4644 if (!OffsetConstInt || OffsetConstInt->getType()->getBitWidth() > 64)
4648 if (OffsetInt % 4 != 0)
4662 if (LoadedCE->getOpcode() == Instruction::Trunc) {
4668 if (LoadedCE->getOpcode() != Instruction::Sub)
4672 if (!LoadedLHS || LoadedLHS->getOpcode() != Instruction::PtrToInt)
4674 auto *LoadedLHSPtr = LoadedLHS->
getOperand(0);
4678 APInt LoadedRHSOffset;
4681 PtrSym != LoadedRHSSym || PtrOffset != LoadedRHSOffset)
4691 if (ConstMask->isNullValue() || isa<UndefValue>(ConstMask))
4693 for (
unsigned I = 0,
E = ConstMask->getType()->getVectorNumElements();
I !=
E;
4695 if (
auto *MaskElt = ConstMask->getAggregateElement(
I))
4696 if (MaskElt->isNullValue() || isa<UndefValue>(MaskElt))
4708 if (
auto *II = dyn_cast<IntrinsicInst>(Op0))
4709 if (II->getIntrinsicID() == IID)
4714 case Intrinsic::fabs:
4717 case Intrinsic::bswap:
4721 case Intrinsic::bitreverse:
4725 case Intrinsic::exp:
4728 match(Op0, m_Intrinsic<Intrinsic::log>(
m_Value(X))))
return X;
4730 case Intrinsic::exp2:
4733 match(Op0, m_Intrinsic<Intrinsic::log2>(
m_Value(X))))
return X;
4735 case Intrinsic::log:
4738 match(Op0, m_Intrinsic<Intrinsic::exp>(
m_Value(X))))
return X;
4743 match(Op0, m_Intrinsic<Intrinsic::exp2>(
m_Value(X))))
return X;
4757 case Intrinsic::usub_with_overflow:
4758 case Intrinsic::ssub_with_overflow:
4764 if (isa<UndefValue>(Op0) || isa<UndefValue>(Op1))
4767 case Intrinsic::uadd_with_overflow:
4768 case Intrinsic::sadd_with_overflow:
4770 if (isa<UndefValue>(Op0) || isa<UndefValue>(Op1))
4773 case Intrinsic::umul_with_overflow:
4774 case Intrinsic::smul_with_overflow:
4784 case Intrinsic::load_relative:
4785 if (
auto *C0 = dyn_cast<Constant>(Op0))
4786 if (
auto *C1 = dyn_cast<Constant>(Op1))
4789 case Intrinsic::powi:
4790 if (
auto *Power = dyn_cast<ConstantInt>(Op1)) {
4792 if (Power->isZero())
4812 template <
typename IterTy>
4816 unsigned NumOperands = std::distance(ArgBegin, ArgEnd);
4817 if (NumOperands == 0)
4821 if (NumOperands == 1)
4824 if (NumOperands == 2)
4829 case Intrinsic::masked_load: {
4830 Value *MaskArg = ArgBegin[2];
4831 Value *PassthruArg = ArgBegin[3];
4837 case Intrinsic::fshl:
4838 case Intrinsic::fshr: {
4839 Value *ShAmtArg = ArgBegin[2];
4840 const APInt *ShAmtC;
4846 return ArgBegin[IID == Intrinsic::fshl ? 0 : 1];
4855 template <
typename IterTy>
4858 unsigned MaxRecurse) {
4861 Ty = PTy->getElementType();
4866 if (isa<UndefValue>(V) || isa<ConstantPointerNull>(V))
4873 if (F->isIntrinsic())
4881 ConstantArgs.
reserve(ArgEnd - ArgBegin);
4882 for (IterTy
I = ArgBegin,
E = ArgEnd;
I !=
E; ++
I) {
4921 case Instruction::FAdd:
4927 cast<BinaryOperator>(
I)->hasNoSignedWrap(),
4928 cast<BinaryOperator>(
I)->hasNoUnsignedWrap(), Q);
4930 case Instruction::FSub:
4934 case Instruction::Sub:
4936 cast<BinaryOperator>(
I)->hasNoSignedWrap(),
4937 cast<BinaryOperator>(
I)->hasNoUnsignedWrap(), Q);
4939 case Instruction::FMul:
4943 case Instruction::Mul:
4946 case Instruction::SDiv:
4949 case Instruction::UDiv:
4952 case Instruction::FDiv:
4956 case Instruction::SRem:
4959 case Instruction::URem:
4962 case Instruction::FRem:
4966 case Instruction::Shl:
4968 cast<BinaryOperator>(
I)->hasNoSignedWrap(),
4969 cast<BinaryOperator>(
I)->hasNoUnsignedWrap(), Q);
4971 case Instruction::LShr:
4973 cast<BinaryOperator>(
I)->isExact(), Q);
4975 case Instruction::AShr:
4977 cast<BinaryOperator>(
I)->isExact(), Q);
4979 case Instruction::And:
4982 case Instruction::Or:
4985 case Instruction::Xor:
4988 case Instruction::ICmp:
4992 case Instruction::FCmp:
5001 case Instruction::GetElementPtr: {
5003 Result =
SimplifyGEPInst(cast<GetElementPtrInst>(I)->getSourceElementType(),
5007 case Instruction::InsertValue: {
5014 case Instruction::InsertElement: {
5015 auto *
IE = cast<InsertElementInst>(
I);
5017 IE->getOperand(2), Q);
5020 case Instruction::ExtractValue: {
5021 auto *EVI = cast<ExtractValueInst>(
I);
5023 EVI->getIndices(), Q);
5026 case Instruction::ExtractElement: {
5027 auto *EEI = cast<ExtractElementInst>(
I);
5029 EEI->getIndexOperand(), Q);
5032 case Instruction::ShuffleVector: {
5033 auto *SVI = cast<ShuffleVectorInst>(
I);
5035 SVI->getMask(), SVI->getType(), Q);
5038 case Instruction::PHI:
5046 #define HANDLE_CAST_INST(num, opc, clas) case Instruction::opc: 5047 #include "llvm/IR/Instruction.def" 5048 #undef HANDLE_CAST_INST 5052 case Instruction::Alloca:
5096 Worklist.
insert(cast<Instruction>(U));
5111 for (
unsigned Idx = 0; Idx != Worklist.
size(); ++Idx) {
5125 Worklist.
insert(cast<Instruction>(U));
5150 assert(I != SimpleV &&
"replaceAndRecursivelySimplify(X,X) is not valid!");
5151 assert(SimpleV &&
"Must provide a simplified value.");
5158 auto *DT = DTWP ? &DTWP->
getDomTree() :
nullptr;
5160 auto *TLI = TLIWP ? &TLIWP->
getTLI() :
nullptr;
5168 return {DL, &AR.
TLI, &AR.
DT, &AR.
AC};
5171 template <
class T,
class... TArgs>
5174 auto *DT = AM.template getCachedResult<DominatorTreeAnalysis>(
F);
5175 auto *TLI = AM.template getCachedResult<TargetLibraryAnalysis>(
F);
5176 auto *AC = AM.template getCachedResult<AssumptionAnalysis>(
F);
Constant * ConstantFoldCastOperand(unsigned Opcode, Constant *C, Type *DestTy, const DataLayout &DL)
Attempt to constant fold a cast with the specified operand.
static Value * SimplifyURemInst(Value *Op0, Value *Op1, const SimplifyQuery &Q, unsigned MaxRecurse)
Given operands for a URem, see if we can fold the result.
MaxMin_match< ICmpInst, LHS, RHS, umin_pred_ty > m_UMin(const LHS &L, const RHS &R)
APInt abs() const
Get the absolute value;.
Pass interface - Implemented by all 'passes'.
bool getObjectSize(const Value *Ptr, uint64_t &Size, const DataLayout &DL, const TargetLibraryInfo *TLI, ObjectSizeOpts Opts={})
Compute the size of the object pointed by Ptr.
bool isFPPredicate() const
Type * getVectorElementType() const
BinaryOp_match< LHS, RHS, Instruction::And > m_And(const LHS &L, const RHS &R)
Value * SimplifyICmpInst(unsigned Predicate, Value *LHS, Value *RHS, const SimplifyQuery &Q)
Given operands for an ICmpInst, fold the result or return null.
static Value * SimplifyAShrInst(Value *Op0, Value *Op1, bool isExact, const SimplifyQuery &Q, unsigned MaxRecurse)
Given operands for an AShr, see if we can fold the result.
BinOpPred_match< LHS, RHS, is_right_shift_op > m_Shr(const LHS &L, const RHS &R)
Matches logical shift operations.
SymbolTableList< Instruction >::iterator eraseFromParent()
This method unlinks 'this' from the containing basic block and deletes it.
void push_back(const T &Elt)
unsigned Log2_32_Ceil(uint32_t Value)
Return the ceil log base 2 of the specified value, 32 if the value is zero.
A parsed version of the target data layout string in and methods for querying it. ...
static ConstantInt * getFalse(LLVMContext &Context)
bool isKnownToBeAPowerOfTwo(const Value *V, const DataLayout &DL, bool OrZero=false, unsigned Depth=0, AssumptionCache *AC=nullptr, const Instruction *CxtI=nullptr, const DominatorTree *DT=nullptr)
Return true if the given value is known to have exactly one bit set when defined. ...
class_match< Value > m_Value()
Match an arbitrary value and ignore it.
This class is the base class for the comparison instructions.
class_match< UndefValue > m_Undef()
Match an arbitrary undef constant.
static GCMetadataPrinterRegistry::Add< ErlangGCPrinter > X("erlang", "erlang-compatible garbage collector")
static Value * SimplifySRemInst(Value *Op0, Value *Op1, const SimplifyQuery &Q, unsigned MaxRecurse)
Given operands for an SRem, see if we can fold the result.
bool isSignMask() const
Check if the APInt's value is returned by getSignMask.
bool isAllOnesValue() const
Return true if this is the value that would be returned by getAllOnesValue.
unsigned getIndexSizeInBits(unsigned AS) const
Size in bits of index used for address calculation in getelementptr.
static Value * SimplifyMulInst(Value *Op0, Value *Op1, const SimplifyQuery &Q, unsigned MaxRecurse)
Given operands for a Mul, see if we can fold the result.
static APInt getAllOnesValue(unsigned numBits)
Get the all-ones value.
This class represents an incoming formal argument to a Function.
const T & back() const
back - Get the last element.
BinaryOp_match< LHS, RHS, Instruction::Sub > m_Sub(const LHS &L, const RHS &R)
Value * getAggregateOperand()
bool isNaN() const
Return true if this is a floating-point NaN constant or a vector floating-point constant with all NaN...
is_zero m_Zero()
Match any null constant or a vector with all elements equal to 0.
Value * SimplifyFCmpInst(unsigned Predicate, Value *LHS, Value *RHS, FastMathFlags FMF, const SimplifyQuery &Q)
Given operands for an FCmpInst, fold the result or return null.
static const Value * getFNegArgument(const Value *BinOp)
static bool isDivZero(Value *X, Value *Y, const SimplifyQuery &Q, unsigned MaxRecurse, bool IsSigned)
Return true if we can simplify X / Y to 0.
static Type * makeCmpResultType(Type *opnd_type)
Create a result type for fcmp/icmp.
Value * findScalarElement(Value *V, unsigned EltNo)
Given a vector and an element number, see if the scalar value is already around as a register...
Compute iterated dominance frontiers using a linear time algorithm.
SimplifyQuery getWithInstruction(Instruction *I) const
BinaryOps getOpcode() const
static Value * SimplifySelectInst(Value *Cond, Value *TrueVal, Value *FalseVal, const SimplifyQuery &Q, unsigned MaxRecurse)
Given operands for a SelectInst, see if we can fold the result.
BinaryOp_match< LHS, RHS, Instruction::Xor, true > m_c_Xor(const LHS &L, const RHS &R)
Matches an Xor with LHS and RHS in either order.
size_type size() const
Determine the number of elements in the SetVector.
static Constant * propagateNaN(Constant *In)
static Value * SimplifyCastInst(unsigned, Value *, Type *, const SimplifyQuery &, unsigned)
static Value * SimplifyRightShift(Instruction::BinaryOps Opcode, Value *Op0, Value *Op1, bool isExact, const SimplifyQuery &Q, unsigned MaxRecurse)
Given operands for an Shl, LShr or AShr, see if we can fold the result.
static Constant * getNaN(Type *Ty, bool Negative=false, unsigned type=0)
m_Intrinsic_Ty< Opnd0 >::Ty m_BitReverse(const Opnd0 &Op0)
static Constant * getGetElementPtr(Type *Ty, Constant *C, ArrayRef< Constant *> IdxList, bool InBounds=false, Optional< unsigned > InRangeIndex=None, Type *OnlyIfReducedTy=nullptr)
Getelementptr form.
bool isSized(SmallPtrSetImpl< Type *> *Visited=nullptr) const
Return true if it makes sense to take the size of this type.
APInt sdiv(const APInt &RHS) const
Signed division function for APInt.
bool decomposeBitTestICmp(Value *LHS, Value *RHS, CmpInst::Predicate &Pred, Value *&X, APInt &Mask, bool LookThroughTrunc=true)
Decompose an icmp into the form ((X & Mask) pred 0) if possible.
BinaryOp_match< LHS, RHS, Instruction::SRem > m_SRem(const LHS &L, const RHS &R)
This class represents zero extension of integer types.
static Value * simplifyOrOfICmps(ICmpInst *Op0, ICmpInst *Op1)
static Value * SimplifyCall(ImmutableCallSite CS, Value *V, IterTy ArgBegin, IterTy ArgEnd, const SimplifyQuery &Q, unsigned MaxRecurse)
bool NullIsUnknownSize
If this is true, null pointers in address space 0 will be treated as though they can't be evaluated...
Constant * ConstantFoldExtractElementInstruction(Constant *Val, Constant *Idx)
Attempt to constant fold an extractelement instruction with the specified operands and indices...
constexpr T getValueOr(U &&value) const LLVM_LVALUE_FUNCTION
APInt udiv(const APInt &RHS) const
Unsigned division operation.
cst_pred_ty< is_sign_mask > m_SignMask()
Match an integer or vector with only the sign bit(s) set.
static APInt getLowBitsSet(unsigned numBits, unsigned loBitsSet)
Get a value with low bits set.
static Value * simplifyUnsignedRangeCheck(ICmpInst *ZeroICmp, ICmpInst *UnsignedICmp, bool IsAnd)
Commuted variants are assumed to be handled by calling this function again with the parameters swappe...
An immutable pass that tracks lazily created AssumptionCache objects.
static PointerType * get(Type *ElementType, unsigned AddressSpace)
This constructs a pointer to an object of the specified type in a numbered address space...
static uint64_t round(uint64_t Acc, uint64_t Input)
static Value * SimplifyGEPInst(Type *, ArrayRef< Value *>, const SimplifyQuery &, unsigned)
Given operands for an GetElementPtrInst, see if we can fold the result.
m_Intrinsic_Ty< Opnd0 >::Ty m_BSwap(const Opnd0 &Op0)
const Value * getTrueValue() const
BinaryOp_match< LHS, RHS, Instruction::AShr > m_AShr(const LHS &L, const RHS &R)
bool isSubsetOf(const APInt &RHS) const
This operation checks that all bits set in this APInt are also set in RHS.
static Constant * getIntToPtr(Constant *C, Type *Ty, bool OnlyIfReduced=false)
A cache of @llvm.assume calls within a function.
0 1 0 0 True if ordered and less than
static Value * simplifyAndOrOfCmps(Value *Op0, Value *Op1, bool IsAnd)
bool IsConstantOffsetFromGlobal(Constant *C, GlobalValue *&GV, APInt &Offset, const DataLayout &DL)
If this constant is a constant offset from a global, return the global and the constant.
LLVMContext & getContext() const
All values hold a context through their type.
1 1 1 0 True if unordered or not equal
Value * SimplifySDivInst(Value *LHS, Value *RHS, const SimplifyQuery &Q)
Given operands for an SDiv, fold the result or return null.
CmpClass_match< LHS, RHS, ICmpInst, ICmpInst::Predicate, true > m_c_ICmp(ICmpInst::Predicate &Pred, const LHS &L, const RHS &R)
Matches an ICmp with a predicate over LHS and RHS in either order.
bool sgt(const APInt &RHS) const
Signed greather than comparison.
static Value * SimplifyShift(Instruction::BinaryOps Opcode, Value *Op0, Value *Op1, const SimplifyQuery &Q, unsigned MaxRecurse)
Given operands for an Shl, LShr or AShr, see if we can fold the result.
static Value * simplifyICmpWithMinMax(CmpInst::Predicate Pred, Value *LHS, Value *RHS, const SimplifyQuery &Q, unsigned MaxRecurse)
Simplify integer comparisons where at least one operand of the compare matches an integer min/max idi...
static bool isOrdered(Predicate predicate)
Determine if the predicate is an ordered operation.
bool all_of(R &&range, UnaryPredicate P)
Provide wrappers to std::all_of which take ranges instead of having to pass begin/end explicitly...
static Value * simplifyAndOfICmpsWithAdd(ICmpInst *Op0, ICmpInst *Op1)
Value * SimplifyUDivInst(Value *LHS, Value *RHS, const SimplifyQuery &Q)
Given operands for a UDiv, fold the result or return null.
STATISTIC(NumFunctions, "Total number of functions")
The adaptor from a function pass to a loop pass computes these analyses and makes them available to t...
This class represents a sign extension of integer types.
bool isNoAliasCall(const Value *V)
Return true if this pointer is returned by a noalias function.
Value * SimplifyShlInst(Value *Op0, Value *Op1, bool isNSW, bool isNUW, const SimplifyQuery &Q)
Given operands for a Shl, fold the result or return null.
BinaryOp_match< LHS, RHS, Instruction::FSub > m_FSub(const LHS &L, const RHS &R)
An instruction for reading from memory.
static IntegerType * getInt64Ty(LLVMContext &C)
static Constant * getTrue(Type *Ty)
For a boolean type or a vector of boolean type, return true or a vector with every element true...
static Constant * getSub(Constant *C1, Constant *C2, bool HasNUW=false, bool HasNSW=false)
bool isVectorTy() const
True if this is an instance of VectorType.
static Value * simplifySelectWithICmpCond(Value *CondVal, Value *TrueVal, Value *FalseVal, const SimplifyQuery &Q, unsigned MaxRecurse)
Try to simplify a select instruction when its condition operand is an integer comparison.
const SimplifyQuery getBestSimplifyQuery(Pass &, Function &)
void reserve(size_type N)
AnyBinaryOp_match< LHS, RHS, true > m_c_BinOp(const LHS &L, const RHS &R)
Matches a BinaryOperator with LHS and RHS in either order.
unsigned getBitWidth() const
Get the bit width of this value.
bool hasNoSignedWrap() const
Determine whether the no signed wrap flag is set.
const TargetLibraryInfo * TLI
static APInt getSignedMaxValue(unsigned numBits)
Gets maximum signed value of APInt for a specific bit width.
unsigned getBitWidth() const
Return the number of bits in the APInt.
static bool valueDominatesPHI(Value *V, PHINode *P, const DominatorTree *DT)
Does the given value dominate the specified phi node?
Value * SimplifyOrInst(Value *LHS, Value *RHS, const SimplifyQuery &Q)
Given operands for an Or, fold the result or return null.
static Value * SimplifyLShrInst(Value *Op0, Value *Op1, bool isExact, const SimplifyQuery &Q, unsigned MaxRecurse)
Given operands for an LShr, see if we can fold the result.
static Value * simplifyAndOfICmpsWithSameOperands(ICmpInst *Op0, ICmpInst *Op1)
Commuted variants are assumed to be handled by calling this function again with the parameters swappe...
static Constant * getNullValue(Type *Ty)
Constructor to create a '0' constant of arbitrary type.
unsigned countMinTrailingZeros() const
Returns the minimum number of trailing zero bits.
static Constant * getAdd(Constant *C1, Constant *C2, bool HasNUW=false, bool HasNSW=false)
Value * SimplifyLShrInst(Value *Op0, Value *Op1, bool isExact, const SimplifyQuery &Q)
Given operands for a LShr, fold the result or return null.
Value * SimplifyFPBinOp(unsigned Opcode, Value *LHS, Value *RHS, FastMathFlags FMF, const SimplifyQuery &Q)
Given operands for an FP BinaryOperator, fold the result or return null.
unsigned countTrailingZeros() const
Count the number of trailing zero bits.
static GCMetadataPrinterRegistry::Add< OcamlGCMetadataPrinter > Y("ocaml", "ocaml 3.10-compatible collector")
bool match(Val *V, const Pattern &P)
static Value * simplifyICmpWithConstant(CmpInst::Predicate Pred, Value *LHS, Value *RHS)
Value * SimplifyExtractValueInst(Value *Agg, ArrayRef< unsigned > Idxs, const SimplifyQuery &Q)
Given operands for an ExtractValueInst, fold the result or return null.
1 0 0 0 True if unordered: isnan(X) | isnan(Y)
static Constant * getIntegerCast(Constant *C, Type *Ty, bool isSigned)
Create a ZExt, Bitcast or Trunc for integer -> integer casts.
BinaryOp_match< LHS, RHS, Instruction::Xor > m_Xor(const LHS &L, const RHS &R)
static Value * SimplifyXorInst(Value *, Value *, const SimplifyQuery &, unsigned)
Given operands for a Xor, see if we can fold the result.
This class represents the LLVM 'select' instruction.
Predicate getInversePredicate() const
For example, EQ -> NE, UGT -> ULE, SLT -> SGE, OEQ -> UNE, UGT -> OLE, OLT -> UGE, etc.
const DataLayout & getDataLayout() const
Get the data layout for the module's target platform.
This is the base class for all instructions that perform data casts.
static Value * simplifyICmpWithBinOp(CmpInst::Predicate Pred, Value *LHS, Value *RHS, const SimplifyQuery &Q, unsigned MaxRecurse)
TODO: A large part of this logic is duplicated in InstCombine's foldICmpBinOp().
static Value * SimplifyFPBinOp(unsigned, Value *, Value *, const FastMathFlags &, const SimplifyQuery &, unsigned)
Given operands for a BinaryOperator, see if we can fold the result.
TargetLibraryInfo & getTLI()
static Constant * getNegativeZero(Type *Ty)
APInt shl(unsigned shiftAmt) const
Left-shift function.
Exact_match< T > m_Exact(const T &SubPattern)
Value * SimplifyGEPInst(Type *SrcTy, ArrayRef< Value *> Ops, const SimplifyQuery &Q)
Given operands for a GetElementPtrInst, fold the result or return null.
A Use represents the edge between a Value definition and its users.
PointerType * getPointerTo(unsigned AddrSpace=0) const
Return a pointer to the current type.
static Value * simplifyIntrinsic(Function *F, IterTy ArgBegin, IterTy ArgEnd, const SimplifyQuery &Q)
CastClass_match< OpTy, Instruction::Trunc > m_Trunc(const OpTy &Op)
Matches Trunc.
static bool isImpliedTrueByMatchingCmp(Predicate Pred1, Predicate Pred2)
Determine if Pred1 implies Pred2 is true when two compares have matching operands.
BinaryOp_match< LHS, RHS, Instruction::FMul, true > m_c_FMul(const LHS &L, const RHS &R)
Matches FMul with LHS and RHS in either order.
0 1 0 1 True if ordered and less than or equal
InstrTy * getInstruction() const
static Value * SimplifyFSubInst(Value *Op0, Value *Op1, FastMathFlags FMF, const SimplifyQuery &Q, unsigned MaxRecurse)
Given operands for an FSub, see if we can fold the result.
DominatorTree & getDomTree()
static Value * SimplifyShlInst(Value *Op0, Value *Op1, bool isNSW, bool isNUW, const SimplifyQuery &Q, unsigned MaxRecurse)
Given operands for an Shl, see if we can fold the result.
bool contains(const APInt &Val) const
Return true if the specified value is in the set.
static Value * SimplifyFCmpInst(unsigned Predicate, Value *LHS, Value *RHS, FastMathFlags FMF, const SimplifyQuery &Q, unsigned MaxRecurse)
Given operands for an FCmpInst, see if we can fold the result.
static Value * simplifyAndOrOfICmpsWithConstants(ICmpInst *Cmp0, ICmpInst *Cmp1, bool IsAnd)
Test if a pair of compares with a shared operand and 2 constants has an empty set intersection...
BinaryOp_match< LHS, RHS, Instruction::Add > m_Add(const LHS &L, const RHS &R)
This class represents a cast from a pointer to an integer.
static Constant * foldOrCommuteConstant(Instruction::BinaryOps Opcode, Value *&Op0, Value *&Op1, const SimplifyQuery &Q)
ValTy * getCalledValue() const
Return the pointer to function that is being called.
Constant * ConstantFoldConstant(const Constant *C, const DataLayout &DL, const TargetLibraryInfo *TLI=nullptr)
ConstantFoldConstant - Attempt to fold the constant using the specified DataLayout.
static bool isICmpTrue(ICmpInst::Predicate Pred, Value *LHS, Value *RHS, const SimplifyQuery &Q, unsigned MaxRecurse)
Given a predicate and two operands, return true if the comparison is true.
static Value * SimplifyFRemInst(Value *Op0, Value *Op1, FastMathFlags FMF, const SimplifyQuery &Q, unsigned)
bool isNullValue() const
Return true if this is the value that would be returned by getNullValue.
Class to represent function types.
A constant value that is initialized with an expression using other constant values.
Value * SimplifyFAddInst(Value *LHS, Value *RHS, FastMathFlags FMF, const SimplifyQuery &Q)
Given operands for an FAdd, fold the result or return null.
FastMathFlags getFastMathFlags() const
Convenience function for getting all the fast-math flags, which must be an operator which supports th...
static Value * SimplifyFDivInst(Value *Op0, Value *Op1, FastMathFlags FMF, const SimplifyQuery &Q, unsigned)
Value * SimplifyCmpInst(unsigned Predicate, Value *LHS, Value *RHS, const SimplifyQuery &Q)
Given operands for a CmpInst, fold the result or return null.
Type * getType() const
All values are typed, get the type of this value.
cstfp_pred_ty< is_nan > m_NaN()
Match an arbitrary NaN constant.
bool insert(const value_type &X)
Insert a new element into the SetVector.
apfloat_match m_APFloat(const APFloat *&Res)
Match a ConstantFP or splatted ConstantVector, binding the specified pointer to the contained APFloat...
match_combine_or< LTy, RTy > m_CombineOr(const LTy &L, const RTy &R)
Combine two pattern matchers matching L || R.
static Value * ExtractEquivalentCondition(Value *V, CmpInst::Predicate Pred, Value *LHS, Value *RHS)
Rummage around inside V looking for something equivalent to the comparison "LHS Pred RHS"...
CastClass_match< OpTy, Instruction::ZExt > m_ZExt(const OpTy &Op)
Matches ZExt.
This instruction compares its operands according to the predicate given to the constructor.
Value * SimplifyMulInst(Value *LHS, Value *RHS, const SimplifyQuery &Q)
Given operands for a Mul, fold the result or return null.
static Value * SimplifyRelativeLoad(Constant *Ptr, Constant *Offset, const DataLayout &DL)
MaxMin_match< ICmpInst, LHS, RHS, smin_pred_ty > m_SMin(const LHS &L, const RHS &R)
ConstantRange intersectWith(const ConstantRange &CR) const
Return the range that results from the intersection of this range with another range.
This header provides classes for managing per-loop analyses.
static bool maskIsAllZeroOrUndef(Value *Mask)
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory)...
Value * getInsertedValueOperand()
class_match< ConstantInt > m_ConstantInt()
Match an arbitrary ConstantInt and ignore it.
const APInt & getValue() const
Return the constant as an APInt value reference.
unsigned getOpcode() const
Returns a member of one of the enums like Instruction::Add.
cstfp_pred_ty< is_pos_zero_fp > m_PosZeroFP()
Match a floating-point positive zero.
static Value * ExpandBinOp(Instruction::BinaryOps Opcode, Value *LHS, Value *RHS, Instruction::BinaryOps OpcodeToExpand, const SimplifyQuery &Q, unsigned MaxRecurse)
Simplify "A op (B op' C)" by distributing op over op', turning it into "(A op B) op' (A op C)"...
Constant * ConstantFoldInsertValueInstruction(Constant *Agg, Constant *Val, ArrayRef< unsigned > Idxs)
ConstantFoldInsertValueInstruction - Attempt to constant fold an insertvalue instruction with the spe...
bool isIntOrIntVectorTy() const
Return true if this is an integer type or a vector of integer types.
void replaceAllUsesWith(Value *V)
Change all uses of this to point to a new Value.
static Value * SimplifyICmpInst(unsigned Predicate, Value *LHS, Value *RHS, const SimplifyQuery &Q, unsigned MaxRecurse)
Given operands for an ICmpInst, see if we can fold the result.
static Function * getFunction(Constant *C)
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree...
static Value * simplifyDiv(Instruction::BinaryOps Opcode, Value *Op0, Value *Op1, const SimplifyQuery &Q, unsigned MaxRecurse)
These are simplifications common to SDiv and UDiv.
The operation itself must be expressed in terms of simpler actions on this target.
Value * getOperand(unsigned i) const
static Value * SimplifyAndInst(Value *, Value *, const SimplifyQuery &, unsigned)
Given operands for an And, see if we can fold the result.
Class to represent pointers.
static ConstantRange makeSatisfyingICmpRegion(CmpInst::Predicate Pred, const ConstantRange &Other)
Produce the largest range such that all values in the returned range satisfy the given predicate with...
bool CannotBeNegativeZero(const Value *V, const TargetLibraryInfo *TLI, unsigned Depth=0)
Return true if we can prove that the specified FP value is never equal to -0.0.
Constant * getAggregateElement(unsigned Elt) const
For aggregates (struct/array/vector) return the constant that corresponds to the specified element if...
Value * getOperand(unsigned i_nocapture) const
static void setLimitsForBinOp(BinaryOperator &BO, APInt &Lower, APInt &Upper)
Many binary operators with a constant operand have an easy-to-compute range of outputs.
bool isNegative() const
Returns true if this value is known to be negative.
static Value * SimplifyFMulInst(Value *Op0, Value *Op1, FastMathFlags FMF, const SimplifyQuery &Q, unsigned MaxRecurse)
Given the operands for an FMul, see if we can fold the result.
static Constant * getBitCast(Constant *C, Type *Ty, bool OnlyIfReduced=false)
Value * SimplifyInsertElementInst(Value *Vec, Value *Elt, Value *Idx, const SimplifyQuery &Q)
Given operands for an InsertElement, fold the result or return null.
static Value * SimplifyCmpInst(unsigned, Value *, Value *, const SimplifyQuery &, unsigned)
Given operands for a CmpInst, see if we can fold the result.
const BasicBlock & getEntryBlock() const
an instruction for type-safe pointer arithmetic to access elements of arrays and structs ...
IntegerType * getIntPtrType(LLVMContext &C, unsigned AddressSpace=0) const
Returns an integer type with size at least as big as that of a pointer in the given address space...
static Value * SimplifyUDivInst(Value *Op0, Value *Op1, const SimplifyQuery &Q, unsigned MaxRecurse)
Given operands for a UDiv, see if we can fold the result.
bool isNegative() const
Determine sign of this APInt.
Constant * ConstantFoldShuffleVectorInstruction(Constant *V1, Constant *V2, Constant *Mask)
Attempt to constant fold a shufflevector instruction with the specified operands and indices...
BinaryOp_match< LHS, RHS, Instruction::LShr > m_LShr(const LHS &L, const RHS &R)
Type * getReturnType() const
Returns the type of the ret val.
Value * SimplifySelectInst(Value *Cond, Value *TrueVal, Value *FalseVal, const SimplifyQuery &Q)
Given operands for a SelectInst, fold the result or return null.
bool isAllOnesValue() const
Determine if all bits are set.
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
MaxMin_match< ICmpInst, LHS, RHS, umax_pred_ty > m_UMax(const LHS &L, const RHS &R)
static Value * SimplifySDivInst(Value *Op0, Value *Op1, const SimplifyQuery &Q, unsigned MaxRecurse)
Given operands for an SDiv, see if we can fold the result.
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)
APInt urem(const APInt &RHS) const
Unsigned remainder operation.
BinaryOp_match< LHS, RHS, Instruction::Add, true > m_c_Add(const LHS &L, const RHS &R)
Matches a Add with LHS and RHS in either order.
bool isKnownNegation(const Value *X, const Value *Y, bool NeedNSW=false)
Return true if the two given values are negation.
OverflowingBinaryOp_match< LHS, RHS, Instruction::Shl, OverflowingBinaryOperator::NoUnsignedWrap > m_NUWShl(const LHS &L, const RHS &R)
ConstantRange getConstantRangeFromMetadata(const MDNode &RangeMD)
Parse out a conservative ConstantRange from !range metadata.
The instances of the Type class are immutable: once they are created, they are never changed...
BinaryOp_match< LHS, RHS, Instruction::Or > m_Or(const LHS &L, const RHS &R)
Value * SimplifyURemInst(Value *LHS, Value *RHS, const SimplifyQuery &Q)
Given operands for a URem, fold the result or return null.
static Value * simplifyBinaryIntrinsic(Function *F, Value *Op0, Value *Op1, const SimplifyQuery &Q)
static Constant * stripAndComputeConstantOffsets(const DataLayout &DL, Value *&V, bool AllowNonInbounds=false)
Compute the base pointer and cumulative constant offsets for V.
bool ult(const APInt &RHS) const
Unsigned less than comparison.
size_t size() const
size - Get the array size.
CastClass_match< OpTy, Instruction::BitCast > m_BitCast(const OpTy &Op)
Matches BitCast.
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
This is an important base class in LLVM.
const APInt & getConstant() const
Returns the value when all bits have a known value.
bool isPointerTy() const
True if this is an instance of PointerType.
bool MaskedValueIsZero(const Value *V, const APInt &Mask, const DataLayout &DL, unsigned Depth=0, AssumptionCache *AC=nullptr, const Instruction *CxtI=nullptr, const DominatorTree *DT=nullptr)
Return true if 'V & Mask' is known to be zero.
BinaryOp_match< LHS, RHS, Instruction::And, true > m_c_And(const LHS &L, const RHS &R)
Matches an And with LHS and RHS in either order.
static Value * ThreadCmpOverSelect(CmpInst::Predicate Pred, Value *LHS, Value *RHS, const SimplifyQuery &Q, unsigned MaxRecurse)
In the case of a comparison with a select instruction, try to simplify the comparison by seeing wheth...
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
bool isMask(unsigned numBits) const
bool isOneValue() const
Determine if this is a value of 1.
bool mayHaveSideEffects() const
Return true if the instruction may have side effects.
Constant * ConstantFoldInstruction(Instruction *I, const DataLayout &DL, const TargetLibraryInfo *TLI=nullptr)
ConstantFoldInstruction - Try to constant fold the specified instruction.
cst_pred_ty< is_all_ones > m_AllOnes()
Match an integer or vector with all bits set.
specificval_ty m_Specific(const Value *V)
Match if we have a specific specified value.
Value * SimplifyCastInst(unsigned CastOpc, Value *Op, Type *Ty, const SimplifyQuery &Q)
Given operands for a CastInst, fold the result or return null.
static Value * ThreadCmpOverPHI(CmpInst::Predicate Pred, Value *LHS, Value *RHS, const SimplifyQuery &Q, unsigned MaxRecurse)
In the case of a comparison with a PHI instruction, try to simplify the comparison by seeing whether ...
Value * SimplifyAddInst(Value *LHS, Value *RHS, bool isNSW, bool isNUW, const SimplifyQuery &Q)
Given operands for an Add, fold the result or return null.
bool isAssociative() const LLVM_READONLY
Return true if the instruction is associative:
bool isMinSignedValue() const
Determine if this is the smallest signed value.
BinaryOp_match< LHS, RHS, Instruction::Shl > m_Shl(const LHS &L, const RHS &R)
bool isConstant() const
Returns true if we know the value of all bits.
This instruction compares its operands according to the predicate given to the constructor.
Value * SimplifyFDivInst(Value *LHS, Value *RHS, FastMathFlags FMF, const SimplifyQuery &Q)
Given operands for an FDiv, fold the result or return null.
Predicate
This enumeration lists the possible predicates for CmpInst subclasses.
0 1 1 1 True if ordered (no nans)
static Constant * getICmp(unsigned short pred, Constant *LHS, Constant *RHS, bool OnlyIfReduced=false)
get* - Return some common constants without having to specify the full Instruction::OPCODE identifier...
bool isFullSet() const
Return true if this set contains all of the elements possible for this data-type. ...
const Function * getFunction() const
Return the function this instruction belongs to.
Constant * ConstantFoldLoadFromConstPtr(Constant *C, Type *Ty, const DataLayout &DL)
ConstantFoldLoadFromConstPtr - Return the value that a load from C would produce if it is constant an...
static double log2(double V)
const Value * getCondition() const
static Constant * getAllOnesValue(Type *Ty)
1 1 1 1 Always true (always folded)
BinOpPred_match< LHS, RHS, is_idiv_op > m_IDiv(const LHS &L, const RHS &R)
Matches integer division operations.
static Value * simplifyAndOrOfICmpsWithZero(ICmpInst *Cmp0, ICmpInst *Cmp1, bool IsAnd)
static Value * ThreadBinOpOverSelect(Instruction::BinaryOps Opcode, Value *LHS, Value *RHS, const SimplifyQuery &Q, unsigned MaxRecurse)
In the case of a binary operation with a select instruction as an operand, try to simplify the binop ...
static UndefValue * get(Type *T)
Static factory methods - Return an 'undef' object of the specified type.
const Value * stripPointerCasts() const
Strip off pointer casts, all-zero GEPs, and aliases.
bool PointerMayBeCaptured(const Value *V, bool ReturnCaptures, bool StoreCaptures)
PointerMayBeCaptured - Return true if this pointer value may be captured by the enclosing function (w...
static wasm::ValType getType(const TargetRegisterClass *RC)
static Value * simplifyAndOrOfFCmps(FCmpInst *LHS, FCmpInst *RHS, bool IsAnd)
auto find(R &&Range, const T &Val) -> decltype(adl_begin(Range))
Provide wrappers to std::find which take ranges instead of having to pass begin/end explicitly...
static PointerType * getInt8PtrTy(LLVMContext &C, unsigned AS=0)
bool isExact() const
Determine whether the exact flag is set.
static Constant * simplifyFPBinop(Value *Op0, Value *Op1)
Constant * ConstantFoldCompareInstOperands(unsigned Predicate, Constant *LHS, Constant *RHS, const DataLayout &DL, const TargetLibraryInfo *TLI=nullptr)
ConstantFoldCompareInstOperands - Attempt to constant fold a compare instruction (icmp/fcmp) with the...
1 1 0 1 True if unordered, less than, or equal
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
LLVM_READONLY APFloat maxnum(const APFloat &A, const APFloat &B)
Implements IEEE maxNum semantics.
static Value * simplifyOrOfICmpsWithAdd(ICmpInst *Op0, ICmpInst *Op1)
APInt lshr(unsigned shiftAmt) const
Logical right-shift function.
Value * SimplifyFRemInst(Value *LHS, Value *RHS, FastMathFlags FMF, const SimplifyQuery &Q)
Given operands for an FRem, fold the result or return null.
bool isKnownNeverNaN(const Value *V)
Return true if the floating-point scalar value is not a NaN or if the floating-point vector value has...
Value * SimplifyShuffleVectorInst(Value *Op0, Value *Op1, Constant *Mask, Type *RetTy, const SimplifyQuery &Q)
Given operands for a ShuffleVectorInst, fold the result or return null.
bool recursivelySimplifyInstruction(Instruction *I, const TargetLibraryInfo *TLI=nullptr, const DominatorTree *DT=nullptr, AssumptionCache *AC=nullptr)
Recursively attempt to simplify an instruction.
CastClass_match< OpTy, Instruction::SExt > m_SExt(const OpTy &Op)
Matches SExt.
bool isEmptySet() const
Return true if this set contains no members.
0 0 1 0 True if ordered and greater than
APInt ashr(unsigned ShiftAmt) const
Arithmetic right-shift function.
bool isPtrOrPtrVectorTy() const
Return true if this is a pointer type or a vector of pointer types.
static Constant * getSplat(unsigned NumElts, Constant *Elt)
Return a ConstantVector with the specified constant in each element.
Predicate getPredicate(unsigned Condition, unsigned Hint)
Return predicate consisting of specified condition and hint bits.
cst_pred_ty< is_negative > m_Negative()
Match an integer or vector of negative values.
A SetVector that performs no allocations if smaller than a certain size.
static ConstantRange makeExactICmpRegion(CmpInst::Predicate Pred, const APInt &Other)
Produce the exact range such that all values in the returned range satisfy the given predicate with a...
BinaryOp_match< LHS, RHS, Instruction::Or, true > m_c_Or(const LHS &L, const RHS &R)
Matches an Or with LHS and RHS in either order.
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements...
This is the shared class of boolean and integer constants.
unsigned ComputeNumSignBits(const Value *Op, const DataLayout &DL, unsigned Depth=0, AssumptionCache *AC=nullptr, const Instruction *CxtI=nullptr, const DominatorTree *DT=nullptr)
Return the number of times the sign bit of the register is replicated into the other bits...
static Value * simplifyICmpOfBools(CmpInst::Predicate Pred, Value *LHS, Value *RHS, const SimplifyQuery &Q)
Fold an icmp when its operands have i1 scalar type.
unsigned getScalarSizeInBits() const LLVM_READONLY
If this is a vector type, return the getPrimitiveSizeInBits value for the element type...
static Value * simplifyDivRem(Value *Op0, Value *Op1, bool IsDiv)
Check for common or similar folds of integer division or integer remainder.
bool dominates(const Instruction *Def, const Use &U) const
Return true if Def dominates a use in User.
Provides information about what library functions are available for the current target.
static Value * simplifySelectBitTest(Value *TrueVal, Value *FalseVal, Value *X, const APInt *Y, bool TrueWhenUnset)
Try to simplify a select instruction when its condition operand is an integer comparison where one op...
static Value * SimplifyOrInst(Value *, Value *, const SimplifyQuery &, unsigned)
Given operands for an Or, see if we can fold the result.
BinaryOp_match< LHS, RHS, Instruction::URem > m_URem(const LHS &L, const RHS &R)
This class represents a range of values.
static Value * SimplifyBinOp(unsigned, Value *, Value *, const SimplifyQuery &, unsigned)
Given operands for a BinaryOperator, see if we can fold the result.
static Value * simplifyUnaryIntrinsic(Function *F, Value *Op0, const SimplifyQuery &Q)
BinaryOp_match< LHS, RHS, Instruction::UDiv > m_UDiv(const LHS &L, const RHS &R)
Type * getReturnType() const
static unsigned isEliminableCastPair(Instruction::CastOps firstOpcode, Instruction::CastOps secondOpcode, Type *SrcTy, Type *MidTy, Type *DstTy, Type *SrcIntPtrTy, Type *MidIntPtrTy, Type *DstIntPtrTy)
Determine how a pair of casts can be eliminated, if they can be at all.
static Constant * getTrunc(Constant *C, Type *Ty, bool OnlyIfReduced=false)
static GCRegistry::Add< StatepointGC > D("statepoint-example", "an example strategy for statepoint")
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.
BinaryOp_match< LHS, RHS, Instruction::Mul, true > m_c_Mul(const LHS &L, const RHS &R)
Matches a Mul with LHS and RHS in either order.
Predicate getSignedPredicate() const
For example, EQ->EQ, SLE->SLE, UGT->SGT, etc.
CastClass_match< OpTy, Instruction::UIToFP > m_UIToFP(const OpTy &Op)
Matches UIToFP.
static Constant * get(Type *Ty, double V)
This returns a ConstantFP, or a vector containing a splat of a ConstantFP, for the specified value in...
bool uge(const APInt &RHS) const
Unsigned greater or equal comparison.
Value * SimplifyCall(ImmutableCallSite CS, const SimplifyQuery &Q)
Given a callsite, fold the result or return null.
static Value * simplifyAndOfICmps(ICmpInst *Op0, ICmpInst *Op1)
bool NullPointerIsDefined(const Function *F, unsigned AS=0)
Check whether null pointer dereferencing is considered undefined behavior for a given function or an ...
static ConstantInt * getTrue(LLVMContext &Context)
bool isCommutative() const
Return true if the instruction is commutative:
Intrinsic::ID getIntrinsicID() const LLVM_READONLY
getIntrinsicID - This method returns the ID number of the specified function, or Intrinsic::not_intri...
static Value * ThreadBinOpOverPHI(Instruction::BinaryOps Opcode, Value *LHS, Value *RHS, const SimplifyQuery &Q, unsigned MaxRecurse)
In the case of a binary operation with an operand that is a PHI instruction, try to simplify the bino...
BinaryOp_match< cst_pred_ty< is_zero_int >, ValTy, Instruction::Sub > m_Neg(const ValTy &V)
Matches a 'Neg' as 'sub 0, V'.
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
unsigned getVectorNumElements() const
bool isIntPredicate() const
bool isTrueWhenEqual() const
This is just a convenience.
Class to represent vector types.
const Module * getModule() const
Return the module owning the function this instruction belongs to or nullptr it the function does not...
Class for arbitrary precision integers.
bool isFalseWhenEqual() const
This is just a convenience.
static Constant * getFalse(Type *Ty)
For a boolean type or a vector of boolean type, return false or a vector with every element false...
cstfp_pred_ty< is_neg_zero_fp > m_NegZeroFP()
Match a floating-point negative zero.
Value * SimplifyFSubInst(Value *LHS, Value *RHS, FastMathFlags FMF, const SimplifyQuery &Q)
Given operands for an FSub, fold the result or return null.
bool isPowerOf2() const
Check if this APInt's value is a power of two greater than zero.
CastClass_match< OpTy, Instruction::PtrToInt > m_PtrToInt(const OpTy &Op)
Matches PtrToInt.
iterator_range< user_iterator > users()
static Constant * getCast(unsigned ops, Constant *C, Type *Ty, bool OnlyIfReduced=false)
Convenience function for getting a Cast operation.
ArrayRef< T > slice(size_t N, size_t M) const
slice(n, m) - Chop off the first N elements of the array, and keep M elements in the array...
static Value * simplifyOrOfICmpsWithSameOperands(ICmpInst *Op0, ICmpInst *Op1)
Commuted variants are assumed to be handled by calling this function again with the parameters swappe...
static APInt getMaxValue(unsigned numBits)
Gets maximum unsigned value of APInt for specific bit width.
const Value * getFalseValue() const
Value * SimplifyFMulInst(Value *LHS, Value *RHS, FastMathFlags FMF, const SimplifyQuery &Q)
Given operands for an FMul, fold the result or return null.
uint64_t getTypeSizeInBits(Type *Ty) const
Size examples:
uint64_t getTypeAllocSize(Type *Ty) const
Returns the offset in bytes between successive objects of the specified type, including alignment pad...
specific_fpval m_FPOne()
Match a float 1.0 or vector with all elements equal to 1.0.
Value * SimplifyAndInst(Value *LHS, Value *RHS, const SimplifyQuery &Q)
Given operands for an And, fold the result or return null.
Predicate getPredicate() const
Return the predicate for this instruction.
static Value * SimplifyAssociativeBinOp(Instruction::BinaryOps Opcode, Value *LHS, Value *RHS, const SimplifyQuery &Q, unsigned MaxRecurse)
Generic simplifications for associative binary operations.
LLVM_ATTRIBUTE_ALWAYS_INLINE iterator end()
static Constant * computePointerDifference(const DataLayout &DL, Value *LHS, Value *RHS)
Compute the constant difference between two pointer values.
Constant * ConstantFoldSelectInstruction(Constant *Cond, Constant *V1, Constant *V2)
Attempt to constant fold a select instruction with the specified operands.
bool intersects(const APInt &RHS) const
This operation tests if there are any pairs of corresponding bits between this APInt and RHS that are...
static Value * SimplifyExtractValueInst(Value *Agg, ArrayRef< unsigned > Idxs, const SimplifyQuery &, unsigned)
Given operands for an ExtractValueInst, see if we can fold the result.
static Type * getIndexedType(Type *Ty, ArrayRef< Value *> IdxList)
Returns the type of the element that would be loaded with a load instruction with the specified param...
static IntegerType * getInt32Ty(LLVMContext &C)
static Constant * computePointerICmp(const DataLayout &DL, const TargetLibraryInfo *TLI, const DominatorTree *DT, CmpInst::Predicate Pred, AssumptionCache *AC, const Instruction *CxtI, Value *LHS, Value *RHS)
unsigned greater or equal
static bool isUnordered(Predicate predicate)
Determine if the predicate is an unordered operation.
uint64_t getLimitedValue(uint64_t Limit=UINT64_MAX) const
If this value is smaller than the specified limit, return it, otherwise return the limit value...
static bool replaceAndRecursivelySimplifyImpl(Instruction *I, Value *SimpleV, const TargetLibraryInfo *TLI, const DominatorTree *DT, AssumptionCache *AC)
Implementation of recursive simplification through an instruction's uses.
static bool isFNeg(const Value *V, bool IgnoreZeroSign=false)
Establish a view to a call site for examination.
Value * SimplifyAShrInst(Value *Op0, Value *Op1, bool isExact, const SimplifyQuery &Q)
Given operands for a AShr, fold the result or return nulll.
static const Value * SimplifyWithOpReplaced(Value *V, Value *Op, Value *RepOp, const SimplifyQuery &Q, unsigned MaxRecurse)
See if V simplifies when its operand Op is replaced with RepOp.
bool isEquality() const
Return true if this predicate is either EQ or NE.
OverflowingBinaryOp_match< LHS, RHS, Instruction::Shl, OverflowingBinaryOperator::NoSignedWrap > m_NSWShl(const LHS &L, const RHS &R)
bool SignBitMustBeZero(const Value *V, const TargetLibraryInfo *TLI)
Return true if we can prove that the specified FP value's sign bit is 0.
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
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...
MaxMin_match< ICmpInst, LHS, RHS, smax_pred_ty > m_SMax(const LHS &L, const RHS &R)
Constant * ConstantFoldInsertElementInstruction(Constant *Val, Constant *Elt, Constant *Idx)
Attempt to constant fold an insertelement instruction with the specified operands and indices...
static Value * SimplifySubInst(Value *Op0, Value *Op1, bool isNSW, bool isNUW, const SimplifyQuery &Q, unsigned MaxRecurse)
Given operands for a Sub, see if we can fold the result.
static void Query(const MachineInstr &MI, AliasAnalysis &AA, bool &Read, bool &Write, bool &Effects, bool &StackPointer)
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 Value * SimplifyShuffleVectorInst(Value *Op0, Value *Op1, Constant *Mask, Type *RetTy, const SimplifyQuery &Q, unsigned MaxRecurse)
Constant * ConstantFoldInstOperands(Instruction *I, ArrayRef< Constant *> Ops, const DataLayout &DL, const TargetLibraryInfo *TLI=nullptr)
ConstantFoldInstOperands - Attempt to constant fold an instruction with the specified operands...
Value * SimplifyXorInst(Value *LHS, Value *RHS, const SimplifyQuery &Q)
Given operands for an Xor, fold the result or return null.
static bool IsIdempotent(Intrinsic::ID ID)
static Value * SimplifyAddInst(Value *Op0, Value *Op1, bool IsNSW, bool IsNUW, const SimplifyQuery &Q, unsigned MaxRecurse)
Given operands for an Add, see if we can fold the result.
1 0 1 0 True if unordered or greater than
bool hasNoUnsignedWrap() const
Determine whether the no unsigned wrap flag is set.
bool isKnownNonEqual(const Value *V1, const Value *V2, const DataLayout &DL, AssumptionCache *AC=nullptr, const Instruction *CxtI=nullptr, const DominatorTree *DT=nullptr)
Return true if the given values are known to be non-equal when defined.
OutputIt transform(R &&Range, OutputIt d_first, UnaryPredicate P)
Wrapper function around std::transform to apply a function to a range and store the result elsewhere...
Value * SimplifySRemInst(Value *LHS, Value *RHS, const SimplifyQuery &Q)
Given operands for an SRem, fold the result or return null.
APInt umul_ov(const APInt &RHS, bool &Overflow) const
Optional< bool > isImpliedCondition(const Value *LHS, const Value *RHS, const DataLayout &DL, bool LHSIsTrue=true, unsigned Depth=0)
Return true if RHS is known to be implied true by LHS.
void GetUnderlyingObjects(Value *V, SmallVectorImpl< Value *> &Objects, const DataLayout &DL, LoopInfo *LI=nullptr, unsigned MaxLookup=6)
This method is similar to GetUnderlyingObject except that it can look through phi and select instruct...
Value * SimplifyBinOp(unsigned Opcode, Value *LHS, Value *RHS, const SimplifyQuery &Q)
Given operands for a BinaryOperator, fold the result or return null.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
static Value * SimplifyFAddInst(Value *Op0, Value *Op1, FastMathFlags FMF, const SimplifyQuery &Q, unsigned MaxRecurse)
Given operands for an FAdd, see if we can fold the result.
static APInt getSignedMinValue(unsigned numBits)
Gets minimum signed value of APInt for a specific bit width.
SmallVector< int, 16 > getShuffleMask() const
static Value * SimplifyExtractElementInst(Value *Vec, Value *Idx, const SimplifyQuery &, unsigned)
Given operands for an ExtractElementInst, see if we can fold the result.
static Value * SimplifyInsertValueInst(Value *Agg, Value *Val, ArrayRef< unsigned > Idxs, const SimplifyQuery &Q, unsigned)
Given operands for an InsertValueInst, see if we can fold the result.
unsigned getPrimitiveSizeInBits() const LLVM_READONLY
Return the basic size of this type if it is a primitive type.
0 0 0 1 True if ordered and equal
ArrayRef< unsigned > getIndices() const
static Value * foldIdentityShuffles(int DestElt, Value *Op0, Value *Op1, int MaskVal, Value *RootVec, unsigned MaxRecurse)
For the given destination element of a shuffle, peek through shuffles to match a root vector source o...
Module * getParent()
Get the module that this global value is contained inside of...
LLVM Value Representation.
static Value * simplifyICmpWithZero(CmpInst::Predicate Pred, Value *LHS, Value *RHS, const SimplifyQuery &Q)
Try hard to fold icmp with zero RHS because this is a common case.
1 0 1 1 True if unordered, greater than, or equal
cst_pred_ty< is_one > m_One()
Match an integer 1 or a vector with all elements equal to 1.
bool hasAllowReassoc() const
Determine whether the allow-reassociation flag is set.
AnalysisType * getAnalysisIfAvailable() const
getAnalysisIfAvailable<AnalysisType>() - Subclasses use this function to get analysis information tha...
unsigned getOpcode() const
Return the opcode for this Instruction or ConstantExpr.
static VectorType * get(Type *ElementType, unsigned NumElements)
This static method is the primary way to construct an VectorType.
bool canConstantFoldCallTo(ImmutableCallSite CS, const Function *F)
canConstantFoldCallTo - Return true if its even possible to fold a call to the specified function...
Value * SimplifyExtractElementInst(Value *Vec, Value *Idx, const SimplifyQuery &Q)
Given operands for an ExtractElementInst, fold the result or return null.
std::underlying_type< E >::type Mask()
Get a bitmask with 1s in all places up to the high-order bit of E's largest value.
bool allowReassoc() const
Flag queries.
static Value * SimplifyPHINode(PHINode *PN, const SimplifyQuery &Q)
See if we can fold the given phi. If not, returns null.
Value * SimplifySubInst(Value *LHS, Value *RHS, bool isNSW, bool isNUW, const SimplifyQuery &Q)
Given operands for a Sub, fold the result or return null.
unsigned countMinLeadingZeros() const
Returns the minimum number of leading zero bits.
AssumptionCache & getAssumptionCache(Function &F)
Get the cached assumptions for a function.
bool isEHPad() const
Return true if the instruction is a variety of EH-block.
match_combine_and< LTy, RTy > m_CombineAnd(const LTy &L, const RTy &R)
Combine two pattern matchers matching L && R.
Value * SimplifyInsertValueInst(Value *Agg, Value *Val, ArrayRef< unsigned > Idxs, const SimplifyQuery &Q)
Given operands for an InsertValueInst, fold the result or return null.
bool isKnownNonZero(const Value *V, const DataLayout &DL, unsigned Depth=0, AssumptionCache *AC=nullptr, const Instruction *CxtI=nullptr, const DominatorTree *DT=nullptr)
Return true if the given value is known to be non-zero when defined.
#define LLVM_FALLTHROUGH
LLVM_FALLTHROUGH - Mark fallthrough cases in switch statements.
Convenience struct for specifying and reasoning about fast-math flags.
Various options to control the behavior of getObjectSize.
Predicate getSwappedPredicate() const
For example, EQ->EQ, SLE->SGE, ULT->UGT, OEQ->OEQ, ULE->UGE, OLT->OGT, etc.
unsigned countLeadingOnes() const
Count the number of leading one bits.
bool isNonNegative() const
Returns true if this value is known to be non-negative.
static bool isUndefShift(Value *Amount)
Returns true if a shift by Amount always yields undef.
Constant * ConstantFoldExtractValueInstruction(Constant *Agg, ArrayRef< unsigned > Idxs)
Attempt to constant fold an extractvalue instruction with the specified operands and indices...
A container for analyses that lazily runs them and caches their results.
Legacy analysis pass which computes a DominatorTree.
unsigned countLeadingZeros() const
The APInt version of the countLeadingZeros functions in MathExtras.h.
bool CannotBeOrderedLessThanZero(const Value *V, const TargetLibraryInfo *TLI)
Return true if we can prove that the specified FP value is either NaN or never less than -0...
static APInt getNullValue(unsigned numBits)
Get the '0' value.
specific_intval m_SpecificInt(uint64_t V)
Match a specific integer value or vector with all elements equal to the value.
int64_t getSExtValue() const
Return the constant as a 64-bit integer value after it has been sign extended as appropriate for the ...
cstfp_pred_ty< is_any_zero_fp > m_AnyZeroFP()
Match a floating-point negative zero or positive zero.
static bool isSameCompare(Value *V, CmpInst::Predicate Pred, Value *LHS, Value *RHS)
isSameCompare - Is V equivalent to the comparison "LHS Pred RHS"?
bool replaceAndRecursivelySimplify(Instruction *I, Value *SimpleV, const TargetLibraryInfo *TLI=nullptr, const DominatorTree *DT=nullptr, AssumptionCache *AC=nullptr)
Replace all uses of 'I' with 'SimpleV' and simplify the uses recursively.
op_range incoming_values()
ConstantRange inverse() const
Return a new range that is the logical not of the current set.
Predicate getUnsignedPredicate() const
For example, EQ->EQ, SLE->ULE, UGT->UGT, etc.
Constant * ConstantFoldCall(ImmutableCallSite CS, Function *F, ArrayRef< Constant *> Operands, const TargetLibraryInfo *TLI=nullptr)
ConstantFoldCall - Attempt to constant fold a call to the specified function with the specified argum...
Value * SimplifyInstruction(Instruction *I, const SimplifyQuery &Q, OptimizationRemarkEmitter *ORE=nullptr)
See if we can compute a simplified version of this instruction.
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)
Determine which bits of V are known to be either zero or one and return them in the KnownZero/KnownOn...
static Value * simplifySelectWithFakeICmpEq(Value *CmpLHS, Value *CmpRHS, ICmpInst::Predicate Pred, Value *TrueVal, Value *FalseVal)
An alternative way to test if a bit is set or not uses sgt/slt instead of eq/ne.
constexpr char Args[]
Key for Kernel::Metadata::mArgs.
static Value * simplifyRem(Instruction::BinaryOps Opcode, Value *Op0, Value *Op1, const SimplifyQuery &Q, unsigned MaxRecurse)
These are simplifications common to SRem and URem.
LLVM_READONLY APFloat minnum(const APFloat &A, const APFloat &B)
Implements IEEE minNum semantics.
static Type * GetCompareTy(Value *Op)
static void commuteShuffleMask(MutableArrayRef< int > Mask, unsigned InVecNumElts)
Change values in a shuffle permute mask assuming the two vector operands of length InVecNumElts have ...
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'.
0 0 0 0 Always false (always folded)
bool isNullValue() const
Determine if all bits are clear.
static Value * foldSelectWithBinaryOp(Value *Cond, Value *TrueVal, Value *FalseVal)
bool noSignedZeros() const
const BasicBlock * getParent() const
an instruction to allocate memory on the stack
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)
Constant * ConstantFoldBinaryOpOperands(unsigned Opcode, Constant *LHS, Constant *RHS, const DataLayout &DL)
Attempt to constant fold a binary operation with the specified operands.