46 #define DEBUG_TYPE "instsimplify" 50 STATISTIC(NumExpand,
"Number of expansions");
51 STATISTIC(NumReassoc,
"Number of reassociations");
75 if (
auto *BO = dyn_cast<BinaryOperator>(Cond))
76 BinOpCode = BO->getOpcode();
81 if (BinOpCode == BinaryOperator::Or) {
83 }
else if (BinOpCode == BinaryOperator::And) {
105 Pred1 != Pred2 || Pred1 != ExpectedPred)
108 if (X == TrueVal || X == FalseVal || Y == TrueVal || Y == FalseVal)
109 return BinOpCode == BinaryOperator::Or ? TrueVal : FalseVal;
134 if (CPred == Pred && CLHS == LHS && CRHS == RHS)
150 if (SimplifiedCmp == Cond) {
153 }
else if (!SimplifiedCmp &&
isSameCompare(Cond, Pred, LHS, RHS)) {
158 return SimplifiedCmp;
165 unsigned MaxRecurse) {
174 unsigned MaxRecurse) {
184 unsigned MaxRecurse) {
245 if (Op0->getOpcode() == OpcodeToExpand) {
247 Value *
A = Op0->getOperand(0), *
B = Op0->getOperand(1), *
C = RHS;
254 && L ==
B &&
R == A)) {
268 if (Op1->getOpcode() == OpcodeToExpand) {
277 && L ==
C &&
R == B)) {
297 unsigned MaxRecurse) {
317 if (V == B)
return LHS;
327 if (Op1 && Op1->getOpcode() == Opcode) {
329 Value *
B = Op1->getOperand(0);
336 if (V == B)
return RHS;
359 if (V == A)
return LHS;
369 if (Op1 && Op1->getOpcode() == Opcode) {
371 Value *
B = Op1->getOperand(0);
378 if (V == C)
return RHS;
396 unsigned MaxRecurse) {
402 if (isa<SelectInst>(LHS)) {
403 SI = cast<SelectInst>(LHS);
405 assert(isa<SelectInst>(RHS) &&
"No select instruction operand!");
406 SI = cast<SelectInst>(RHS);
426 if (TV && isa<UndefValue>(TV))
428 if (FV && isa<UndefValue>(FV))
439 if ((FV && !TV) || (TV && !FV)) {
448 Value *UnsimplifiedLHS = SI == LHS ? UnsimplifiedBranch : LHS;
449 Value *UnsimplifiedRHS = SI == LHS ? RHS : UnsimplifiedBranch;
450 if (Simplified->
getOperand(0) == UnsimplifiedLHS &&
454 Simplified->
getOperand(1) == UnsimplifiedLHS &&
474 unsigned MaxRecurse) {
480 if (!isa<SelectInst>(LHS)) {
484 assert(isa<SelectInst>(LHS) &&
"Not comparing with a select instruction!");
520 unsigned MaxRecurse) {
526 if (isa<PHINode>(LHS)) {
527 PI = cast<PHINode>(LHS);
532 assert(isa<PHINode>(RHS) &&
"No PHI instruction operand!");
533 PI = cast<PHINode>(RHS);
540 Value *CommonValue =
nullptr;
543 if (Incoming == PI)
continue;
544 Value *V = PI == LHS ?
549 if (!V || (CommonValue && V != CommonValue))
568 if (!isa<PHINode>(LHS)) {
572 assert(isa<PHINode>(LHS) &&
"Not comparing with a phi instruction!");
573 PHINode *PI = cast<PHINode>(LHS);
580 Value *CommonValue =
nullptr;
585 if (Incoming == PI)
continue;
593 if (!V || (CommonValue && V != CommonValue))
604 if (
auto *CLHS = dyn_cast<Constant>(Op0)) {
605 if (
auto *CRHS = dyn_cast<Constant>(Op1))
697 bool AllowNonInbounds =
false) {
706 IntIdxTy = DL.
getIndexType(V->getType())->getScalarType();
707 Offset = Offset.
sextOrTrunc(IntIdxTy->getIntegerBitWidth());
710 if (V->getType()->isVectorTy())
762 if (Known.Zero.isMaxSignedValue()) {
775 Value *
X =
nullptr, *
Y =
nullptr, *
Z = Op1;
873 unsigned MaxRecurse) {
911 if (isa<SelectInst>(Op0) || isa<SelectInst>(Op1))
918 if (isa<PHINode>(Op0) || isa<PHINode>(Op1))
951 for (
unsigned i = 0; i != NumElts; ++i) {
953 if (Elt && (Elt->
isNullValue() || isa<UndefValue>(Elt)))
992 Constant *
C = dyn_cast_or_null<Constant>(V);
999 unsigned MaxRecurse,
bool IsSigned) {
1057 bool IsSigned = Opcode == Instruction::SDiv;
1062 auto *Mul = cast<OverflowingBinaryOperator>(Op0);
1090 if (isa<SelectInst>(Op0) || isa<SelectInst>(Op1))
1096 if (isa<PHINode>(Op0) || isa<PHINode>(Op1))
1100 if (
isDivZero(Op0, Op1, Q, MaxRecurse, IsSigned))
1116 if ((Opcode == Instruction::SRem &&
1118 (Opcode == Instruction::URem &&
1124 ((Opcode == Instruction::SRem &&
1126 (Opcode == Instruction::URem &&
1132 if (isa<SelectInst>(Op0) || isa<SelectInst>(Op1))
1138 if (isa<PHINode>(Op0) || isa<PHINode>(Op1))
1143 if (
isDivZero(Op0, Op1, Q, MaxRecurse, Opcode == Instruction::SRem))
1152 unsigned MaxRecurse) {
1157 return simplifyDiv(Instruction::SDiv, Op0, Op1, Q, MaxRecurse);
1167 unsigned MaxRecurse) {
1168 return simplifyDiv(Instruction::UDiv, Op0, Op1, Q, MaxRecurse);
1178 unsigned MaxRecurse) {
1189 return simplifyRem(Instruction::SRem, Op0, Op1, Q, MaxRecurse);
1199 unsigned MaxRecurse) {
1200 return simplifyRem(Instruction::URem, Op0, Op1, Q, MaxRecurse);
1214 if (isa<UndefValue>(C))
1219 if (CI->getValue().getLimitedValue() >=
1220 CI->getType()->getScalarSizeInBits())
1224 if (isa<ConstantVector>(C) || isa<ConstantDataVector>(
C)) {
1259 if (isa<SelectInst>(Op0) || isa<SelectInst>(Op1))
1265 if (isa<PHINode>(Op0) || isa<PHINode>(Op1))
1288 unsigned MaxRecurse) {
1304 if (Op0Known.
One[0])
1362 const APInt *ShRAmt, *ShLAmt;
1365 *ShRAmt == *ShLAmt) {
1369 if (ShRAmt->
uge(EffWidthY))
1415 ICmpInst *UnsignedICmp,
bool IsAnd,
1429 if (
match(UnsignedICmp,
1450 return IsAnd ? UnsignedICmp : ZeroICmp;
1456 return IsAnd ? ZeroICmp : UnsignedICmp;
1462 if (
match(UnsignedICmp,
1470 return UnsignedICmp;
1474 return UnsignedICmp;
1481 else if (
match(UnsignedICmp,
1491 return IsAnd ? UnsignedICmp : ZeroICmp;
1497 return IsAnd ? UnsignedICmp : ZeroICmp;
1502 return IsAnd ? ZeroICmp : UnsignedICmp;
1508 return IsAnd ? ZeroICmp : UnsignedICmp;
1583 const APInt *C0, *C1;
1593 if (IsAnd && Range0.intersectWith(Range1).isEmptySet())
1598 if (!IsAnd && Range0.unionWith(Range1).isFullSet())
1606 if (Range0.contains(Range1))
1607 return IsAnd ? Cmp1 : Cmp0;
1608 if (Range1.contains(Range0))
1609 return IsAnd ? Cmp0 : Cmp1;
1655 const APInt *C0, *C1;
1663 auto *AddInst = cast<OverflowingBinaryOperator>(Op0->
getOperand(0));
1664 if (AddInst->getOperand(1) != Op1->
getOperand(1))
1671 const APInt Delta = *C1 - *C0;
1672 if (C0->isStrictlyPositive()) {
1686 if (C0->getBoolValue() && isNUW) {
1728 const APInt *C0, *C1;
1736 auto *AddInst = cast<BinaryOperator>(Op0->
getOperand(0));
1737 if (AddInst->getOperand(1) != Op1->
getOperand(1))
1744 const APInt Delta = *C1 - *C0;
1745 if (C0->isStrictlyPositive()) {
1759 if (C0->getBoolValue() && isNUW) {
1840 if (Cast0 && Cast1 && Cast0->getOpcode() == Cast1->getOpcode() &&
1841 Cast0->getSrcTy() == Cast1->getSrcTy()) {
1842 Op0 = Cast0->getOperand(0);
1843 Op1 = Cast1->getOperand(0);
1865 if (
auto *
C = dyn_cast<Constant>(V))
1878 if (!Extract || !Extract->getIndices().equals(1))
1880 Value *Agg = Extract->getAggregateOperand();
1883 m_Intrinsic<Intrinsic::smul_with_overflow>())))
1905 Pred != ICmpInst::Predicate::ICMP_NE)
1929 Pred != ICmpInst::Predicate::ICMP_EQ)
1945 unsigned MaxRecurse) {
1986 (~(*Mask)).lshr(*ShAmt).isNullValue())
1992 (~(*Mask)).shl(*ShAmt).isNullValue())
2045 if (isa<SelectInst>(Op0) || isa<SelectInst>(Op1))
2052 if (isa<PHINode>(Op0) || isa<PHINode>(Op1))
2076 if (EffWidthY <= ShftCnt) {
2101 unsigned MaxRecurse) {
2198 if (isa<SelectInst>(Op0) || isa<SelectInst>(Op1))
2204 const APInt *C1, *C2;
2231 if (isa<PHINode>(Op0) || isa<PHINode>(Op1))
2245 unsigned MaxRecurse) {
2304 if (Pred == Cmp->
getPredicate() && LHS == CmpLHS && RHS == CmpRHS)
2307 LHS == CmpRHS && RHS == CmpLHS)
2352 isa<ConstantPointerNull>(RHS) &&
2424 if (isa<AllocaInst>(LHS) &&
2425 (isa<AllocaInst>(RHS) || isa<GlobalVariable>(RHS))) {
2428 uint64_t LHSSize, RHSSize;
2432 if (LHSOffsetCI && RHSOffsetCI &&
2436 const APInt &RHSOffsetValue = RHSOffsetCI->getValue();
2439 LHSOffsetValue.
ult(LHSSize) &&
2440 RHSOffsetValue.
ult(RHSSize)) {
2448 if (!cast<PointerType>(LHS->
getType())->isEmptyTy() &&
2449 !cast<PointerType>(RHS->
getType())->isEmptyTy() &&
2489 if (
const AllocaInst *AI = dyn_cast<AllocaInst>(V))
2490 return AI->getParent() && AI->getFunction() && AI->isStaticAlloca();
2491 if (
const GlobalValue *GV = dyn_cast<GlobalValue>(V))
2492 return (GV->hasLocalLinkage() || GV->hasHiddenVisibility() ||
2493 GV->hasProtectedVisibility() || GV->hasGlobalUnnamedAddr()) &&
2494 !GV->isThreadLocal();
2495 if (
const Argument *
A = dyn_cast<Argument>(V))
2496 return A->hasByValAttr();
2501 if ((IsNAC(LHSUObjs) && IsAllocDisjoint(RHSUObjs)) ||
2502 (IsNAC(RHSUObjs) && IsAllocDisjoint(LHSUObjs)))
2707 unsigned MaxRecurse) {
2712 if (MaxRecurse && (LBO || RBO)) {
2714 Value *
A =
nullptr, *
B =
nullptr, *
C =
nullptr, *
D =
nullptr;
2716 bool NoLHSWrapProblem =
false, NoRHSWrapProblem =
false;
2729 D = RBO->getOperand(1);
2739 if ((A == RHS ||
B == RHS) && NoLHSWrapProblem)
2746 if ((
C == LHS ||
D == LHS) && NoRHSWrapProblem)
2749 C == LHS ?
D :
C, Q, MaxRecurse - 1))
2753 if (A && C && (A == C || A ==
D ||
B == C ||
B ==
D) && NoLHSWrapProblem &&
2761 }
else if (A ==
D) {
2765 }
else if (
B == C) {
2833 if (
ConstantInt *RHSC = dyn_cast<ConstantInt>(RHS)) {
2834 if (RHSC->getValue().isStrictlyPositive()) {
2844 if (RHSC->getValue().isNonNegative()) {
2940 if (
auto *CI = dyn_cast<ConstantInt>(RHS)) {
2941 const APInt *CI2Val, *CIVal = &CI->getValue();
2971 if (MaxRecurse && LBO && RBO && LBO->
getOpcode() == RBO->getOpcode() &&
2976 case Instruction::UDiv:
2977 case Instruction::LShr:
2982 RBO->getOperand(0), Q, MaxRecurse - 1))
2985 case Instruction::SDiv:
2990 RBO->getOperand(0), Q, MaxRecurse - 1))
2993 case Instruction::AShr:
2997 RBO->getOperand(0), Q, MaxRecurse - 1))
3000 case Instruction::Shl: {
3008 RBO->getOperand(0), Q, MaxRecurse - 1))
3021 unsigned MaxRecurse) {
3035 (A == LHS || B == LHS)) {
3042 (A == RHS || B == RHS)) {
3050 (A == LHS || B == LHS)) {
3109 (A == LHS || B == LHS)) {
3116 (A == RHS || B == RHS)) {
3124 (A == LHS || B == LHS)) {
3178 (A == C || A == D || B == C || B == D)) {
3188 (A == C || A == D || B == C || B == D)) {
3198 (A == C || A == D || B == C || B == D)) {
3208 (A == C || A == D || B == C || B == D)) {
3228 if (
Constant *CLHS = dyn_cast<Constant>(LHS)) {
3229 if (
Constant *CRHS = dyn_cast<Constant>(RHS))
3236 assert(!isa<UndefValue>(LHS) &&
"Unexpected icmp undef,%X");
3248 if (LHS == RHS || isa<UndefValue>(RHS))
3262 if (isa<Instruction>(RHS) && isa<Instruction>(LHS)) {
3263 auto RHS_Instr = cast<Instruction>(RHS);
3264 auto LHS_Instr = cast<Instruction>(LHS);
3269 *RHS_Instr->getMetadata(LLVMContext::MD_range));
3271 *LHS_Instr->getMetadata(LLVMContext::MD_range));
3274 if (Satisfied_CR.contains(LHS_CR))
3279 if (InversedSatisfied_CR.contains(LHS_CR))
3285 if (isa<CastInst>(LHS) && (isa<Constant>(RHS) || isa<CastInst>(RHS))) {
3288 Type *SrcTy = SrcOp->getType();
3293 if (MaxRecurse && isa<PtrToIntInst>(LI) &&
3295 if (
Constant *RHSC = dyn_cast<Constant>(RHS)) {
3301 }
else if (
PtrToIntInst *RI = dyn_cast<PtrToIntInst>(RHS)) {
3302 if (RI->getOperand(0)->getType() == SrcTy)
3310 if (isa<ZExtInst>(LHS)) {
3313 if (
ZExtInst *RI = dyn_cast<ZExtInst>(RHS)) {
3314 if (MaxRecurse && SrcTy == RI->getOperand(0)->getType())
3317 SrcOp, RI->getOperand(0), Q,
3323 else if (
ConstantInt *CI = dyn_cast<ConstantInt>(RHS)) {
3331 if (RExt == CI && MaxRecurse)
3333 SrcOp, Trunc, Q, MaxRecurse-1))
3356 return CI->getValue().isNegative() ?
3362 return CI->getValue().isNegative() ?
3370 if (isa<SExtInst>(LHS)) {
3373 if (
SExtInst *RI = dyn_cast<SExtInst>(RHS)) {
3374 if (MaxRecurse && SrcTy == RI->getOperand(0)->getType())
3382 else if (
ConstantInt *CI = dyn_cast<ConstantInt>(RHS)) {
3390 if (RExt == CI && MaxRecurse)
3408 return CI->getValue().isNegative() ?
3413 return CI->getValue().isNegative() ?
3461 if (
auto *CLHS = dyn_cast<PtrToIntOperator>(LHS))
3462 if (
auto *CRHS = dyn_cast<PtrToIntOperator>(RHS))
3468 Q.
IIQ, CLHS->getPointerOperand(),
3469 CRHS->getPointerOperand()))
3473 if (
GEPOperator *GRHS = dyn_cast<GEPOperator>(RHS)) {
3474 if (GLHS->getPointerOperand() == GRHS->getPointerOperand() &&
3475 GLHS->hasAllConstantIndices() && GRHS->hasAllConstantIndices() &&
3477 (GLHS->isInBounds() && GRHS->isInBounds() &&
3485 GLHS->getSourceElementType(), Null, IndicesLHS);
3489 GLHS->getSourceElementType(), Null, IndicesRHS);
3497 if (isa<SelectInst>(LHS) || isa<SelectInst>(RHS))
3503 if (isa<PHINode>(LHS) || isa<PHINode>(RHS))
3519 unsigned MaxRecurse) {
3523 if (
Constant *CLHS = dyn_cast<Constant>(LHS)) {
3524 if (
Constant *CRHS = dyn_cast<Constant>(RHS))
3547 "Comparison must be either ordered or unordered");
3553 if (isa<UndefValue>(LHS) || isa<UndefValue>(RHS)) {
3599 assert(!C->
isNaN() &&
"Unexpected NaN constant!");
3686 if (isa<SelectInst>(LHS) || isa<SelectInst>(RHS))
3692 if (isa<PHINode>(LHS) || isa<PHINode>(RHS))
3707 unsigned MaxRecurse) {
3713 if (isa<Constant>(Op))
3721 if (
auto *
B = dyn_cast<BinaryOperator>(
I)) {
3731 if (isa<OverflowingBinaryOperator>(
B))
3738 if (
B->getOperand(0) ==
Op)
3741 if (
B->getOperand(1) ==
Op)
3748 if (
CmpInst *
C = dyn_cast<CmpInst>(
I)) {
3760 if (
auto *
GEP = dyn_cast<GetElementPtrInst>(
I)) {
3764 [&](
Value *V) {
return V == Op ? RepOp : V; });
3774 if (
Constant *CRepOp = dyn_cast<Constant>(RepOp)) {
3777 for (
unsigned i = 0,
e =
I->getNumOperands(); i !=
e; ++i) {
3778 if (
I->getOperand(i) ==
Op)
3780 else if (
Constant *COp = dyn_cast<Constant>(
I->getOperand(i)))
3787 if (ConstOps.
size() ==
I->getNumOperands()) {
3790 ConstOps[1], Q.
DL, Q.
TLI);
3792 if (
LoadInst *LI = dyn_cast<LoadInst>(
I))
3793 if (!LI->isVolatile())
3806 const APInt *
Y,
bool TrueWhenUnset) {
3813 return TrueWhenUnset ? FalseVal : TrueVal;
3819 return TrueWhenUnset ? FalseVal : TrueVal;
3826 return TrueWhenUnset ? TrueVal : FalseVal;
3832 return TrueWhenUnset ? TrueVal : FalseVal;
3856 unsigned MaxRecurse) {
3858 Value *CmpLHS, *CmpRHS;
3878 if (
match(TrueVal, isFsh) && FalseVal == X && CmpLHS == ShAmt &&
3883 if (
match(FalseVal, isFsh) && TrueVal == X && CmpLHS == ShAmt &&
3895 m_Intrinsic<Intrinsic::fshr>(
m_Value(X),
3900 if (
match(TrueVal, isRotate) && FalseVal == X && CmpLHS == ShAmt &&
3905 if (
match(FalseVal, isRotate) && TrueVal == X && CmpLHS == ShAmt &&
3957 bool HasNoSignedZeros = Q.
CxtI && isa<FPMathOperator>(Q.
CxtI) &&
3980 if (
auto *CondC = dyn_cast<Constant>(Cond)) {
3981 if (
auto *TrueC = dyn_cast<Constant>(TrueVal))
3982 if (
auto *FalseC = dyn_cast<Constant>(FalseVal))
3986 if (isa<UndefValue>(CondC))
3987 return isa<Constant>(FalseVal) ? FalseVal : TrueVal;
3992 if (CondC->isAllOnesValue())
3995 if (CondC->isNullValue())
4001 "Select must have bool or bool vector condition");
4003 "Select must have same types for true/false ops");
4009 if (TrueVal == FalseVal)
4012 if (isa<UndefValue>(TrueVal))
4014 if (isa<UndefValue>(FalseVal))
4029 return *Imp ? TrueVal : FalseVal;
4045 cast<PointerType>(Ops[0]->getType()->getScalarType())->getAddressSpace();
4048 if (Ops.
size() == 1)
4059 if (isa<UndefValue>(Ops[0]))
4062 if (Ops.
size() == 2) {
4064 if (
match(Ops[1],
m_Zero()) && Ops[0]->getType() == GEPTy)
4073 if (TyAllocSize == 0 && Ops[0]->
getType() == GEPTy)
4080 auto PtrToIntOrZero = [GEPTy](
Value *
P) ->
Value * {
4091 if (TyAllocSize == 1 &&
4093 if (
Value *
R = PtrToIntOrZero(P))
4101 TyAllocSize == 1ULL <<
C)
4102 if (
Value *
R = PtrToIntOrZero(P))
4110 if (
Value *
R = PtrToIntOrZero(P))
4122 APInt BasePtrOffset(IdxWidth, 0);
4123 Value *StrippedBasePtr =
4124 Ops[0]->stripAndAccumulateInBoundsConstantOffsets(Q.
DL,
4143 if (!
all_of(Ops, [](
Value *V) {
return isa<Constant>(V); }))
4163 if (
Constant *CAgg = dyn_cast<Constant>(Agg))
4164 if (
Constant *CVal = dyn_cast<Constant>(Val))
4173 if (EV->getAggregateOperand()->getType() == Agg->
getType() &&
4174 EV->getIndices() == Idxs) {
4177 return EV->getAggregateOperand();
4180 if (Agg == EV->getAggregateOperand())
4199 if (VecC && ValC && IdxC)
4203 if (
auto *CI = dyn_cast<ConstantInt>(Idx)) {
4204 uint64_t NumElements = cast<VectorType>(Vec->
getType())->getNumElements();
4205 if (CI->uge(NumElements))
4210 if (isa<UndefValue>(Idx))
4215 if (isa<UndefValue>(Val))
4231 if (
auto *CAgg = dyn_cast<Constant>(Agg))
4235 unsigned NumIdxs = Idxs.
size();
4236 for (
auto *IVI = dyn_cast<InsertValueInst>(Agg); IVI !=
nullptr;
4239 unsigned NumInsertValueIdxs = InsertValueIdxs.
size();
4240 unsigned NumCommonIdxs = std::min(NumInsertValueIdxs, NumIdxs);
4241 if (InsertValueIdxs.
slice(0, NumCommonIdxs) ==
4242 Idxs.
slice(0, NumCommonIdxs)) {
4243 if (NumIdxs == NumInsertValueIdxs)
4244 return IVI->getInsertedValueOperand();
4261 if (
auto *CVec = dyn_cast<Constant>(Vec)) {
4262 if (
auto *CIdx = dyn_cast<Constant>(Idx))
4266 if (
auto *Splat = CVec->getSplatValue())
4269 if (isa<UndefValue>(Vec))
4275 if (
auto *IdxC = dyn_cast<ConstantInt>(Idx)) {
4285 if (isa<UndefValue>(Idx))
4300 Value *CommonValue =
nullptr;
4301 bool HasUndefInput =
false;
4304 if (Incoming == PN)
continue;
4305 if (isa<UndefValue>(Incoming)) {
4307 HasUndefInput =
true;
4310 if (CommonValue && Incoming != CommonValue)
4312 CommonValue = Incoming;
4331 if (
auto *
C = dyn_cast<Constant>(Op))
4334 if (
auto *CI = dyn_cast<CastInst>(Op)) {
4335 auto *Src = CI->getOperand(0);
4336 Type *SrcTy = Src->getType();
4337 Type *MidTy = CI->getType();
4339 if (Src->getType() == Ty) {
4349 SrcIntPtrTy, MidIntPtrTy,
4350 DstIntPtrTy) == Instruction::BitCast)
4356 if (CastOpc == Instruction::BitCast)
4372 int MaskVal,
Value *RootVec,
4373 unsigned MaxRecurse) {
4384 int RootElt = MaskVal;
4385 Value *SourceOp = Op0;
4386 if (MaskVal >= InVecNumElts) {
4387 RootElt = MaskVal - InVecNumElts;
4393 if (
auto *SourceShuf = dyn_cast<ShuffleVectorInst>(SourceOp)) {
4395 DestElt, SourceShuf->getOperand(0), SourceShuf->getOperand(1),
4396 SourceShuf->getMaskValue(RootElt), RootVec, MaxRecurse);
4408 if (RootVec != SourceOp)
4413 if (RootElt != DestElt)
4421 unsigned MaxRecurse) {
4422 if (isa<UndefValue>(Mask))
4432 "Size of Indices not same as number of mask elements?");
4436 bool MaskSelects0 =
false, MaskSelects1 =
false;
4437 for (
unsigned i = 0; i != MaskNumElts; ++i) {
4438 if (Indices[i] == -1)
4440 if ((
unsigned)Indices[i] < InVecNumElts)
4441 MaskSelects0 =
true;
4443 MaskSelects1 =
true;
4454 if (Op0Const && Op1Const)
4459 if (Op0Const && !Op1Const) {
4474 if (
all_of(Indices, [InsertIndex](
int MaskElt) {
4475 return MaskElt == InsertIndex || MaskElt == -1;
4477 assert(isa<UndefValue>(Op1) &&
"Expected undef operand 1 for splat");
4481 for (
unsigned i = 0; i != MaskNumElts; ++i)
4482 if (Indices[i] == -1)
4490 if (
auto *OpShuf = dyn_cast<ShuffleVectorInst>(Op0))
4491 if (isa<UndefValue>(Op1) && RetTy == InVecTy &&
4492 OpShuf->getMask()->getSplatValue())
4498 if (
find(Indices, -1) != Indices.
end())
4505 Value *RootVec =
nullptr;
4506 for (
unsigned i = 0; i != MaskNumElts; ++i) {
4513 if (!RootVec || RootVec->
getType() != RetTy)
4527 if (
auto *
C = dyn_cast<Constant>(Op))
4566 if (
any_of(Ops, [](
Value *V) {
return isa<UndefValue>(V); }))
4569 for (
Value *V : Ops)
4657 if (FMF.
noNaNs() && Op0 == Op1)
4696 if (Op0 == Op1 &&
match(Op0, m_Intrinsic<Intrinsic::sqrt>(
m_Value(X))) &&
4812 unsigned MaxRecurse) {
4814 case Instruction::FNeg:
4828 case Instruction::FNeg:
4851 case Instruction::Sub:
4853 case Instruction::Mul:
4855 case Instruction::SDiv:
4857 case Instruction::UDiv:
4859 case Instruction::SRem:
4861 case Instruction::URem:
4863 case Instruction::Shl:
4865 case Instruction::LShr:
4867 case Instruction::AShr:
4869 case Instruction::And:
4871 case Instruction::Or:
4873 case Instruction::Xor:
4875 case Instruction::FAdd:
4877 case Instruction::FSub:
4879 case Instruction::FMul:
4881 case Instruction::FDiv:
4883 case Instruction::FRem:
4895 unsigned MaxRecurse) {
4897 case Instruction::FAdd:
4899 case Instruction::FSub:
4901 case Instruction::FMul:
4903 case Instruction::FDiv:
4935 default:
return false;
4938 case Intrinsic::fabs:
4939 case Intrinsic::floor:
4940 case Intrinsic::ceil:
4941 case Intrinsic::trunc:
4942 case Intrinsic::rint:
4943 case Intrinsic::nearbyint:
4945 case Intrinsic::canonicalize:
4963 if (!OffsetConstInt || OffsetConstInt->getType()->getBitWidth() > 64)
4967 if (OffsetInt % 4 != 0)
4981 if (LoadedCE->getOpcode() == Instruction::Trunc) {
4987 if (LoadedCE->getOpcode() != Instruction::Sub)
4991 if (!LoadedLHS || LoadedLHS->getOpcode() != Instruction::PtrToInt)
4993 auto *LoadedLHSPtr = LoadedLHS->
getOperand(0);
4997 APInt LoadedRHSOffset;
5000 PtrSym != LoadedRHSSym || PtrOffset != LoadedRHSOffset)
5011 if (
auto *II = dyn_cast<IntrinsicInst>(Op0))
5012 if (II->getIntrinsicID() == IID)
5017 case Intrinsic::fabs:
5020 case Intrinsic::bswap:
5024 case Intrinsic::bitreverse:
5028 case Intrinsic::exp:
5031 match(Op0, m_Intrinsic<Intrinsic::log>(
m_Value(X))))
return X;
5033 case Intrinsic::exp2:
5036 match(Op0, m_Intrinsic<Intrinsic::log2>(
m_Value(X))))
return X;
5038 case Intrinsic::log:
5041 match(Op0, m_Intrinsic<Intrinsic::exp>(
m_Value(X))))
return X;
5046 (
match(Op0, m_Intrinsic<Intrinsic::exp2>(
m_Value(X))) ||
5050 case Intrinsic::log10:
5056 case Intrinsic::floor:
5057 case Intrinsic::trunc:
5058 case Intrinsic::ceil:
5060 case Intrinsic::nearbyint:
5061 case Intrinsic::rint: {
5084 case Intrinsic::usub_with_overflow:
5085 case Intrinsic::ssub_with_overflow:
5090 case Intrinsic::uadd_with_overflow:
5091 case Intrinsic::sadd_with_overflow:
5096 if (isa<UndefValue>(Op0) || isa<UndefValue>(Op1)) {
5098 cast<StructType>(ReturnType),
5103 case Intrinsic::umul_with_overflow:
5104 case Intrinsic::smul_with_overflow:
5114 case Intrinsic::uadd_sat:
5120 case Intrinsic::sadd_sat:
5135 case Intrinsic::usub_sat:
5140 case Intrinsic::ssub_sat:
5148 case Intrinsic::load_relative:
5149 if (
auto *C0 = dyn_cast<Constant>(Op0))
5150 if (
auto *C1 = dyn_cast<Constant>(Op1))
5153 case Intrinsic::powi:
5154 if (
auto *Power = dyn_cast<ConstantInt>(Op1)) {
5156 if (Power->isZero())
5163 case Intrinsic::copysign:
5178 if (Op0 == Op1)
return Op0;
5189 return PropagateNaN ? Op0 : Op1;
5191 return PropagateNaN ? Op1 : Op0;
5195 if (
auto *M0 = dyn_cast<IntrinsicInst>(Op0))
5196 if (M0->getIntrinsicID() == IID &&
5197 (M0->getOperand(0) == Op1 || M0->getOperand(1) == Op1))
5199 if (
auto *M1 = dyn_cast<IntrinsicInst>(Op1))
5200 if (M1->getIntrinsicID() == IID &&
5201 (M1->getOperand(0) == Op0 || M1->getOperand(1) == Op0))
5236 if (NumOperands == 1)
5239 if (NumOperands == 2)
5245 case Intrinsic::masked_load:
5246 case Intrinsic::masked_gather: {
5254 case Intrinsic::fshl:
5255 case Intrinsic::fshr: {
5267 const APInt *ShAmtC;
5276 case Intrinsic::fma:
5277 case Intrinsic::fmuladd: {
5295 if (isa<UndefValue>(Callee) || isa<ConstantPointerNull>(Callee))
5302 if (F->isIntrinsic())
5311 ConstantArgs.
reserve(NumArgs);
5312 for (
auto &
Arg : Call->
args()) {
5347 case Instruction::FNeg:
5350 case Instruction::FAdd:
5360 case Instruction::FSub:
5364 case Instruction::Sub:
5370 case Instruction::FMul:
5374 case Instruction::Mul:
5377 case Instruction::SDiv:
5380 case Instruction::UDiv:
5383 case Instruction::FDiv:
5387 case Instruction::SRem:
5390 case Instruction::URem:
5393 case Instruction::FRem:
5397 case Instruction::Shl:
5403 case Instruction::LShr:
5407 case Instruction::AShr:
5411 case Instruction::And:
5414 case Instruction::Or:
5417 case Instruction::Xor:
5420 case Instruction::ICmp:
5424 case Instruction::FCmp:
5433 case Instruction::GetElementPtr: {
5435 Result =
SimplifyGEPInst(cast<GetElementPtrInst>(I)->getSourceElementType(),
5439 case Instruction::InsertValue: {
5446 case Instruction::InsertElement: {
5447 auto *
IE = cast<InsertElementInst>(
I);
5449 IE->getOperand(2), Q);
5452 case Instruction::ExtractValue: {
5453 auto *EVI = cast<ExtractValueInst>(
I);
5455 EVI->getIndices(), Q);
5458 case Instruction::ExtractElement: {
5459 auto *EEI = cast<ExtractElementInst>(
I);
5461 EEI->getIndexOperand(), Q);
5464 case Instruction::ShuffleVector: {
5465 auto *SVI = cast<ShuffleVectorInst>(
I);
5467 SVI->getMask(), SVI->getType(), Q);
5470 case Instruction::PHI:
5477 case Instruction::Freeze:
5480 #define HANDLE_CAST_INST(num, opc, clas) case Instruction::opc: 5481 #include "llvm/IR/Instruction.def" 5482 #undef HANDLE_CAST_INST 5486 case Instruction::Alloca:
5532 Worklist.
insert(cast<Instruction>(U));
5547 for (
unsigned Idx = 0; Idx != Worklist.
size(); ++Idx) {
5553 if (UnsimplifiedUsers)
5554 UnsimplifiedUsers->insert(I);
5564 Worklist.
insert(cast<Instruction>(U));
5589 assert(I != SimpleV &&
"replaceAndRecursivelySimplify(X,X) is not valid!");
5590 assert(SimpleV &&
"Must provide a simplified value.");
5598 auto *DT = DTWP ? &DTWP->
getDomTree() :
nullptr;
5600 auto *TLI = TLIWP ? &TLIWP->
getTLI(F) :
nullptr;
5608 return {DL, &AR.
TLI, &AR.
DT, &AR.
AC};
5611 template <
class T,
class... TArgs>
5614 auto *DT = AM.template getCachedResult<DominatorTreeAnalysis>(
F);
5615 auto *TLI = AM.template getCachedResult<TargetLibraryAnalysis>(
F);
5616 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.
void computeKnownBits(const Value *V, KnownBits &Known, const DataLayout &DL, unsigned Depth=0, AssumptionCache *AC=nullptr, const Instruction *CxtI=nullptr, const DominatorTree *DT=nullptr, OptimizationRemarkEmitter *ORE=nullptr, bool UseInstrInfo=true)
Determine which bits of V are known to be either zero or one and return them in the KnownZero/KnownOn...
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.
static Value * simplifyIntrinsic(CallBase *Call, const SimplifyQuery &Q)
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)
class_match< Value > m_Value()
Match an arbitrary value and ignore it.
static Value * SimplifyFreezeInst(Value *Op0)
Given operands for a Freeze, see if we can fold the result.
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 hasNoSignedZeros() const
Determine whether the no-signed-zeros flag is set.
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.
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 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...
This class represents lattice values for constants.
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 Value * simplifyCmpSelFalseCase(CmpInst::Predicate Pred, Value *LHS, Value *RHS, Value *Cond, const SimplifyQuery &Q, unsigned MaxRecurse)
Simplify comparison with false branch of select.
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.
bool isGuaranteedNotToBeUndefOrPoison(const Value *V)
Return true if this function can prove that V is never undef value or poison value.
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.
static Constant * getInfinity(Type *Ty, bool Negative=false)
Optional< bool > isImpliedByDomCondition(const Value *Cond, const Instruction *ContextI, const DataLayout &DL)
Return the boolean condition value in the context of the given instruction if it is known based on do...
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)
BinaryOp_match< cstfp_pred_ty< is_any_zero_fp >, RHS, Instruction::FSub > m_FNegNSZ(const RHS &X)
Match 'fneg X' as 'fsub +-0.0, X'.
static Value * handleOtherCmpSelSimplifications(Value *TCmp, Value *FCmp, Value *Cond, const SimplifyQuery &Q, unsigned MaxRecurse)
We know comparison with both branches of select can be simplified, but they are not equal...
Constant * ConstantFoldCall(const CallBase *Call, Function *F, ArrayRef< Constant *> Operands, const TargetLibraryInfo *TLI=nullptr)
ConstantFoldCall - Attempt to constant fold a call to the specified function with the specified argum...
BinaryOp_match< LHS, RHS, Instruction::FAdd, true > m_c_FAdd(const LHS &L, const RHS &R)
Matches FAdd with LHS and RHS in either order.
This class represents zero extension of integer types.
void push_back(const T &Elt)
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
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.
ThreeOps_match< Val_t, Elt_t, Idx_t, Instruction::InsertElement > m_InsertElement(const Val_t &Val, const Elt_t &Elt, const Idx_t &Idx)
Matches InsertElementInst.
static Value * simplifyFPUnOp(unsigned, Value *, const FastMathFlags &, const SimplifyQuery &, unsigned)
Given the operand for a UnaryOperator, see if we can fold the result.
An immutable pass that tracks lazily created AssumptionCache objects.
class_match< Constant > m_Constant()
Match an arbitrary Constant and ignore it.
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
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.
bool isTerminator() const
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.
static Constant * simplifyFPOp(ArrayRef< Value *> Ops)
Perform folds that are common to any floating-point operation.
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...
unsigned getPointerSizeInBits(unsigned AS=0) const
Layout pointer size, in bits FIXME: The defaults need to be removed once all of the backends/clients ...
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...
Base class for all callable instructions (InvokeInst and CallInst) Holds everything related to callin...
Type * getStructElementType(unsigned N) const
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)
ConstantRange computeConstantRange(const Value *V, bool UseInstrInfo=true)
Determine the possible constant range of an integer or vector of integer value.
static Constant * getTrue(Type *Ty)
For a boolean type or a vector of boolean type, return true or a vector with every element true...
TypeSize getTypeSizeInBits(Type *Ty) const
Size examples:
static Constant * getSub(Constant *C1, Constant *C2, bool HasNUW=false, bool HasNSW=false)
const Instruction * getTerminator() const LLVM_READONLY
Returns the terminator instruction if the block is well formed or null if the block is not well forme...
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.
LLVM_READONLY APFloat maximum(const APFloat &A, const APFloat &B)
Implements IEEE 754-2018 maximum semantics.
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.
const TargetLibraryInfo * TLI
static Value * simplifySelectWithFCmp(Value *Cond, Value *T, Value *F, const SimplifyQuery &Q)
Try to simplify a select instruction when its condition operand is a floating-point comparison...
cst_pred_ty< is_zero_int > m_ZeroInt()
Match an integer 0 or a vector with all elements equal to 0.
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 * getArgOperand(unsigned i) const
1 0 0 1 True if unordered or equal
static GCMetadataPrinterRegistry::Add< OcamlGCMetadataPrinter > Y("ocaml", "ocaml 3.10-compatible collector")
specific_fpval m_SpecificFP(double V)
Match a specific floating point value or vector with all elements equal to the value.
bool match(Val *V, const Pattern &P)
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)
CmpClass_match< LHS, RHS, FCmpInst, FCmpInst::Predicate > m_FCmp(FCmpInst::Predicate &Pred, const LHS &L, const RHS &R)
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)
Type * getIndexType(Type *PtrTy) const
Returns the type of a GEP index.
static Value * SimplifyXorInst(Value *, Value *, const SimplifyQuery &, unsigned)
Given operands for a Xor, see if we can fold the result.
bool replaceAndRecursivelySimplify(Instruction *I, Value *SimpleV, const TargetLibraryInfo *TLI=nullptr, const DominatorTree *DT=nullptr, AssumptionCache *AC=nullptr, SmallSetVector< Instruction *, 8 > *UnsimplifiedUsers=nullptr)
Replace all uses of 'I' with 'SimpleV' and simplify the uses recursively.
TwoOps_match< Val_t, Idx_t, Instruction::ExtractElement > m_ExtractElement(const Val_t &Val, const Idx_t &Idx)
Matches ExtractElementInst.
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, bool UseInstrInfo=true)
Return true if the given value is known to have exactly one bit set when defined. ...
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.
static Value * omitCheckForZeroBeforeMulWithOverflow(Value *Op0, Value *Op1)
The .
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 Constant * getNegativeZero(Type *Ty)
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.
PointerType * getPointerTo(unsigned AddrSpace=0) const
Return a pointer to the current type.
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
LLVM_READONLY APFloat minimum(const APFloat &A, const APFloat &B)
Implements IEEE 754-2018 minimum semantics.
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, bool UseInstrInfo=true)
Return true if 'V & Mask' is known to be zero.
bool hasNoUnsignedWrap(const InstT *Op) 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.
Value * SimplifyFNegInst(Value *Op, FastMathFlags FMF, const SimplifyQuery &Q)
Given operand for an FNeg, fold the result or return null.
static Constant * foldOrCommuteConstant(Instruction::BinaryOps Opcode, Value *&Op0, Value *&Op1, const SimplifyQuery &Q)
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.
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.
APInt sextOrTrunc(unsigned width) const
Sign extend or truncate to width.
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)
This header provides classes for managing per-loop analyses.
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...
static Value * simplifyOrOfICmps(ICmpInst *Op0, ICmpInst *Op1, const SimplifyQuery &Q)
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)
bool isExact(const BinaryOperator *Op) const
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.
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.
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...
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...
MDNode * getMetadata(const Instruction *I, unsigned KindID) const
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...
unsigned ComputeNumSignBits(const Value *Op, const DataLayout &DL, unsigned Depth=0, AssumptionCache *AC=nullptr, const Instruction *CxtI=nullptr, const DominatorTree *DT=nullptr, bool UseInstrInfo=true)
Return the number of times the sign bit of the register is replicated into the other bits...
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.
void GetUnderlyingObjects(const Value *V, SmallVectorImpl< const 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...
uint64_t getZExtValue() const
Return the constant as a 64-bit unsigned integer value after it has been zero extended as appropriate...
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.
Value * getCalledValue() const
TypeSize getPrimitiveSizeInBits() const LLVM_READONLY
Return the basic size of this type if it is a primitive type.
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.
Value * SimplifyFMAFMul(Value *LHS, Value *RHS, FastMathFlags FMF, const SimplifyQuery &Q)
Given operands for the multiplication of a FMA, 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.
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.
TypeSize getTypeAllocSize(Type *Ty) const
Returns the offset in bytes between successive objects of the specified type, including alignment pad...
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...
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.
specific_intval m_SpecificInt(APInt V)
Match a specific integer value or vector with all elements equal to the value.
cst_pred_ty< is_all_ones > m_AllOnes()
Match an integer or vector with all bits set.
InstrInfoQuery provides an interface to query additional information for instructions like metadata o...
specificval_ty m_Specific(const Value *V)
Match if we have a specific specified value.
Value * SimplifyUnOp(unsigned Opcode, Value *Op, const SimplifyQuery &Q)
Given operand for a UnaryOperator, fold the result or return null.
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 any_of(R &&range, UnaryPredicate P)
Provide wrappers to std::any_of which take ranges instead of having to pass begin/end explicitly...
static Value * simplifyCmpSelCase(CmpInst::Predicate Pred, Value *LHS, Value *RHS, Value *Cond, const SimplifyQuery &Q, unsigned MaxRecurse, Constant *TrueOrFalse)
Simplify comparison with true or false branch of select: sel = select i1 cond, i32 tv...
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.
amdgpu Simplify well known AMD library false FunctionCallee Value * Arg
static Constant * get(StructType *T, ArrayRef< Constant *> V)
0 1 1 1 True if ordered (no nans)
static Value * simplifyUnOp(unsigned, Value *, const SimplifyQuery &, unsigned)
Given the operand for a UnaryOperator, see if we can fold the result.
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...
TargetLibraryInfo & getTLI(const Function &F)
Value * SimplifyCall(CallBase *Call, const SimplifyQuery &Q)
Given a callsite, fold the result or return null.
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 Value * simplifyICmpWithConstant(CmpInst::Predicate Pred, Value *LHS, Value *RHS, const InstrInfoQuery &IIQ)
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 ...
bool maskIsAllZeroOrUndef(Value *Mask)
Given a mask vector of the form <Y x="" i1>="">, Return true if all of the elements of this predicate...
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 address space casts.
static wasm::ValType getType(const TargetRegisterClass *RC)
Value * getIncomingValue(unsigned i) const
Return incoming value number x.
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)
iterator_range< User::op_iterator > args()
Iteration adapter for range-for loops.
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.
static Value * simplifyFNegInst(Value *Op, FastMathFlags FMF, const SimplifyQuery &Q, unsigned MaxRecurse)
Given the operand for an FNeg, see if we can fold the result.
LLVM_READONLY APFloat maxnum(const APFloat &A, const APFloat &B)
Implements IEEE maxNum semantics.
deferredval_ty< Value > m_Deferred(Value *const &V)
A commutative-friendly version of m_Specific().
bool canConstantFoldCallTo(const CallBase *Call, const Function *F)
canConstantFoldCallTo - Return true if its even possible to fold a call to the specified function...
Value * SimplifyFRemInst(Value *LHS, Value *RHS, FastMathFlags FMF, const SimplifyQuery &Q)
Given operands for an FRem, fold the result or return null.
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
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 Value * simplifyAndOfICmps(ICmpInst *Op0, ICmpInst *Op1, const SimplifyQuery &Q)
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.
static unsigned getIntrinsicID(const SDNode *N)
This is the shared class of boolean and integer constants.
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.
1 1 0 0 True if unordered or less than
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small...
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.
static Value * simplifyAndOrOfFCmps(const TargetLibraryInfo *TLI, FCmpInst *LHS, FCmpInst *RHS, bool IsAnd)
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)
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...
unsigned getNumIncomingValues() const
Return the number of incoming edges.
bool uge(const APInt &RHS) const
Unsigned greater or equal comparison.
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.
amdgpu Simplify well known AMD library false FunctionCallee Callee
static Value * simplifyOrOfICmpsWithAdd(ICmpInst *Op0, ICmpInst *Op1, const InstrInfoQuery &IIQ)
Value * SimplifyFSubInst(Value *LHS, Value *RHS, FastMathFlags FMF, const SimplifyQuery &Q)
Given operands for an FSub, 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, bool UseInstrInfo=true)
Return true if the given value is known to be non-zero when defined.
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...
const Value * getFalseValue() const
static Value * simplifyAndOfICmpsWithAdd(ICmpInst *Op0, ICmpInst *Op1, const InstrInfoQuery &IIQ)
Value * SimplifyFMulInst(Value *LHS, Value *RHS, FastMathFlags FMF, const SimplifyQuery &Q)
Given operands for an FMul, fold the result or return null.
const Value * stripAndAccumulateConstantOffsets(const DataLayout &DL, APInt &Offset, bool AllowNonInbounds) const
Accumulate the constant offset this value has compared to a base pointer.
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 * simplifyUnsignedRangeCheck(ICmpInst *ZeroICmp, ICmpInst *UnsignedICmp, bool IsAnd, const SimplifyQuery &Q)
Commuted variants are assumed to be handled by calling this function again with the parameters swappe...
static Value * SimplifyAssociativeBinOp(Instruction::BinaryOps Opcode, Value *LHS, Value *RHS, const SimplifyQuery &Q, unsigned MaxRecurse)
Generic simplifications for associative binary operations.
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.
FNeg_match< OpTy > m_FNeg(const OpTy &X)
Match 'fneg X' as 'fsub -0.0, X'.
unsigned getNumArgOperands() const
static Constant * getNaN(Type *Ty, bool Negative=false, uint64_t Payload=0)
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)
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 VectorType * get(Type *ElementType, ElementCount EC)
This static method is the primary way to construct an VectorType.
BasicBlock * getIncomingBlock(unsigned i) const
Return incoming basic block number i.
Function * getCalledFunction() const
Returns the function called, or null if this is an indirect function invocation.
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 isKnownNonEqual(const Value *V1, const Value *V2, const DataLayout &DL, AssumptionCache *AC=nullptr, const Instruction *CxtI=nullptr, const DominatorTree *DT=nullptr, bool UseInstrInfo=true)
Return true if the given values are known to be non-equal when defined.
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 * ConstantFoldUnaryOpOperand(unsigned Opcode, Constant *Op, const DataLayout &DL)
Attempt to constant fold a unary operation with the specified operand.
Constant * ConstantFoldInsertElementInstruction(Constant *Val, Constant *Elt, Constant *Idx)
Attempt to constant fold an insertelement instruction with the specified operands and indices...
0 1 1 0 True if ordered and operands are unequal
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.
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)
CastClass_match< OpTy, Instruction::SIToFP > m_SIToFP(const OpTy &Op)
Matches SIToFP.
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 isKnownNeverNaN(const Value *V, const TargetLibraryInfo *TLI, unsigned Depth=0)
Return true if the floating-point scalar value is not a NaN or if the floating-point vector value has...
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.
Value * SimplifyFreezeInst(Value *Op, const SimplifyQuery &Q)
Given an operand for a Freeze, see if we can fold the result.
static Value * simplifyAndOrOfCmps(const SimplifyQuery &Q, Value *Op0, Value *Op1, bool IsAnd)
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 Constant * computePointerICmp(const DataLayout &DL, const TargetLibraryInfo *TLI, const DominatorTree *DT, CmpInst::Predicate Pred, AssumptionCache *AC, const Instruction *CxtI, const InstrInfoQuery &IIQ, Value *LHS, Value *RHS)
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.
static Constant * foldConstant(Instruction::UnaryOps Opcode, Value *&Op, const SimplifyQuery &Q)
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...
#define LLVM_FALLTHROUGH
LLVM_FALLTHROUGH - Mark fallthrough cases in switch statements.
Value * SimplifyExtractElementInst(Value *Vec, Value *Idx, const SimplifyQuery &Q)
Given operands for an ExtractElementInst, fold the result or return null.
static bool replaceAndRecursivelySimplifyImpl(Instruction *I, Value *SimpleV, const TargetLibraryInfo *TLI, const DominatorTree *DT, AssumptionCache *AC, SmallSetVector< Instruction *, 8 > *UnsimplifiedUsers=nullptr)
Implementation of recursive simplification through an instruction's uses.
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.
static bool omitCheckForZeroBeforeMulWithOverflowInternal(Value *Op1, Value *X)
Check that the Op1 is in expected form, i.e.
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.
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.
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.
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.
int64_t getSExtValue() const
Return the constant as a 64-bit integer value after it has been sign extended as appropriate for the ...
static Value * omitCheckForZeroBeforeInvertedMulWithOverflow(Value *Op0, Value *NotOp1)
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"?
op_range incoming_values()
ConstantRange inverse() const
Return a new range that is the logical not of the current set.
0 0 1 1 True if ordered and greater than or equal
Predicate getUnsignedPredicate() const
For example, EQ->EQ, SLE->ULE, UGT->UGT, etc.
static Value * SimplifyFMAFMul(Value *Op0, Value *Op1, FastMathFlags FMF, const SimplifyQuery &Q, unsigned MaxRecurse)
bool hasNoSignedWrap(const InstT *Op) const
Value * SimplifyInstruction(Instruction *I, const SimplifyQuery &Q, OptimizationRemarkEmitter *ORE=nullptr)
See if we can compute a simplified version of this instruction.
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.
static Value * simplifyCmpSelTrueCase(CmpInst::Predicate Pred, Value *LHS, Value *RHS, Value *Cond, const SimplifyQuery &Q, unsigned MaxRecurse)
Simplify comparison with true branch of select.
static Constant * get(ArrayRef< Constant *> V)
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'.
bool isNullValue() const
Determine if all bits are clear.
0 0 0 0 Always false (always folded)
cmpResult compare(const APFloat &RHS) const
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)
bool PointerMayBeCaptured(const Value *V, bool ReturnCaptures, bool StoreCaptures, unsigned MaxUsesToExplore=DefaultMaxUsesToExplore)
PointerMayBeCaptured - Return true if this pointer value may be captured by the enclosing function (w...
Constant * ConstantFoldBinaryOpOperands(unsigned Opcode, Constant *LHS, Constant *RHS, const DataLayout &DL)
Attempt to constant fold a binary operation with the specified operands.