20 #define DEBUG_TYPE "instsimplify"
36 using namespace llvm::PatternMatch;
40 STATISTIC(NumExpand,
"Number of expansions");
41 STATISTIC(NumFactor ,
"Number of factorizations");
42 STATISTIC(NumReassoc,
"Number of reassociations");
66 "Expected i1 type or a vector of i1!");
74 "Expected i1 type or a vector of i1!");
86 if (CPred == Pred && CLHS == LHS && CRHS == RHS)
129 unsigned OpcToExpand,
const Query &Q,
130 unsigned MaxRecurse) {
138 if (Op0->getOpcode() == OpcodeToExpand) {
140 Value *
A = Op0->getOperand(0), *B = Op0->getOperand(1), *
C = RHS;
147 && L == B && R == A)) {
161 if (Op1->getOpcode() == OpcodeToExpand) {
163 Value *
A = LHS, *B = Op1->getOperand(0), *
C = Op1->getOperand(1);
170 && L ==
C && R == B)) {
190 unsigned OpcToExtract,
const Query &Q,
191 unsigned MaxRecurse) {
200 if (!Op0 || Op0->
getOpcode() != OpcodeToExtract ||
201 !Op1 || Op1->getOpcode() != OpcodeToExtract)
206 Value *
C = Op1->getOperand(0), *D = Op1->getOperand(1);
219 if (V == B || V == DD) {
221 return V == B ? LHS : RHS;
235 Value *CC = B == D ? C : D;
242 if (V == A || V == CC) {
244 return V == A ? LHS : RHS;
260 const Query &Q,
unsigned MaxRecurse) {
281 if (V == B)
return LHS;
291 if (Op1 && Op1->getOpcode() == Opcode) {
293 Value *B = Op1->getOperand(0);
294 Value *
C = Op1->getOperand(1);
300 if (V == B)
return RHS;
323 if (V == A)
return LHS;
333 if (Op1 && Op1->getOpcode() == Opcode) {
335 Value *B = Op1->getOperand(0);
336 Value *
C = Op1->getOperand(1);
342 if (V == C)
return RHS;
359 const Query &Q,
unsigned MaxRecurse) {
365 if (isa<SelectInst>(LHS)) {
366 SI = cast<SelectInst>(LHS);
368 assert(isa<SelectInst>(RHS) &&
"No select instruction operand!");
369 SI = cast<SelectInst>(RHS);
389 if (TV && isa<UndefValue>(TV))
391 if (FV && isa<UndefValue>(FV))
402 if ((FV && !TV) || (TV && !FV)) {
406 if (Simplified && Simplified->
getOpcode() == Opcode) {
411 Value *UnsimplifiedLHS = SI == LHS ? UnsimplifiedBranch : LHS;
412 Value *UnsimplifiedRHS = SI == LHS ? RHS : UnsimplifiedBranch;
413 if (Simplified->
getOperand(0) == UnsimplifiedLHS &&
417 Simplified->
getOperand(1) == UnsimplifiedLHS &&
432 unsigned MaxRecurse) {
438 if (!isa<SelectInst>(LHS)) {
442 assert(isa<SelectInst>(LHS) &&
"Not comparing with a select instruction!");
454 TCmp =
getTrue(Cond->getType());
460 TCmp =
getTrue(Cond->getType());
513 const Query &Q,
unsigned MaxRecurse) {
519 if (isa<PHINode>(LHS)) {
520 PI = cast<PHINode>(LHS);
525 assert(isa<PHINode>(RHS) &&
"No PHI instruction operand!");
526 PI = cast<PHINode>(RHS);
533 Value *CommonValue = 0;
537 if (Incoming == PI)
continue;
538 Value *V = PI == LHS ?
543 if (!V || (CommonValue && V != CommonValue))
556 const Query &Q,
unsigned MaxRecurse) {
562 if (!isa<PHINode>(LHS)) {
566 assert(isa<PHINode>(LHS) &&
"Not comparing with a phi instruction!");
567 PHINode *PI = cast<PHINode>(LHS);
574 Value *CommonValue = 0;
578 if (Incoming == PI)
continue;
582 if (!V || (CommonValue && V != CommonValue))
593 const Query &Q,
unsigned MaxRecurse) {
594 if (
Constant *CLHS = dyn_cast<Constant>(Op0)) {
595 if (
Constant *CRHS = dyn_cast<Constant>(Op1)) {
672 bool AllowNonInbounds =
false) {
689 if ((!AllowNonInbounds && !GEP->isInBounds()) ||
690 !GEP->accumulateConstantOffset(*TD, Offset))
692 V = GEP->getPointerOperand();
694 V = cast<Operator>(V)->getOperand(0);
695 }
else if (
GlobalAlias *GA = dyn_cast<GlobalAlias>(V)) {
696 if (GA->mayBeOverridden())
698 V = GA->getAliasee();
703 "Unexpected operand type!");
704 }
while (Visited.insert(V));
735 const Query &Q,
unsigned MaxRecurse) {
736 if (
Constant *CLHS = dyn_cast<Constant>(Op0))
737 if (
Constant *CRHS = dyn_cast<Constant>(Op1)) {
823 if (X->getType() == Y->
getType())
869 const Query &Q,
unsigned MaxRecurse) {
870 if (
Constant *CLHS = dyn_cast<Constant>(Op0)) {
871 if (
Constant *CRHS = dyn_cast<Constant>(Op1)) {
911 const Query &Q,
unsigned MaxRecurse) {
912 if (
Constant *CLHS = dyn_cast<Constant>(Op0)) {
913 if (
Constant *CRHS = dyn_cast<Constant>(Op1)) {
949 unsigned MaxRecurse) {
950 if (
Constant *CLHS = dyn_cast<Constant>(Op0)) {
951 if (
Constant *CRHS = dyn_cast<Constant>(Op1)) {
975 unsigned MaxRecurse) {
976 if (
Constant *CLHS = dyn_cast<Constant>(Op0)) {
977 if (
Constant *CRHS = dyn_cast<Constant>(Op1)) {
1022 if (isa<SelectInst>(Op0) || isa<SelectInst>(Op1))
1029 if (isa<PHINode>(Op0) || isa<PHINode>(Op1))
1066 const Query &Q,
unsigned MaxRecurse) {
1067 if (
Constant *C0 = dyn_cast<Constant>(Op0)) {
1068 if (
Constant *C1 = dyn_cast<Constant>(Op1)) {
1074 bool isSigned = Opcode == Instruction::SDiv;
1111 if (Div->getOpcode() == Opcode && Div->getOperand(1) ==
Y)
1122 if (isa<SelectInst>(Op0) || isa<SelectInst>(Op1))
1128 if (isa<PHINode>(Op0) || isa<PHINode>(Op1))
1138 unsigned MaxRecurse) {
1154 unsigned MaxRecurse) {
1189 const Query &Q,
unsigned MaxRecurse) {
1190 if (
Constant *C0 = dyn_cast<Constant>(Op0)) {
1191 if (
Constant *C1 = dyn_cast<Constant>(Op1)) {
1227 if (isa<SelectInst>(Op0) || isa<SelectInst>(Op1))
1233 if (isa<PHINode>(Op0) || isa<PHINode>(Op1))
1243 unsigned MaxRecurse) {
1259 unsigned MaxRecurse) {
1294 const Query &Q,
unsigned MaxRecurse) {
1295 if (
Constant *C0 = dyn_cast<Constant>(Op0)) {
1296 if (
Constant *C1 = dyn_cast<Constant>(Op1)) {
1316 if (CI->getValue().getLimitedValue() >=
1322 if (isa<SelectInst>(Op0) || isa<SelectInst>(Op1))
1328 if (isa<PHINode>(Op0) || isa<PHINode>(Op1))
1338 const Query &Q,
unsigned MaxRecurse) {
1363 const Query &Q,
unsigned MaxRecurse) {
1378 cast<OverflowingBinaryOperator>(Op0)->hasNoUnsignedWrap())
1395 const Query &Q,
unsigned MaxRecurse) {
1414 cast<OverflowingBinaryOperator>(Op0)->hasNoSignedWrap())
1431 unsigned MaxRecurse) {
1432 if (
Constant *CLHS = dyn_cast<Constant>(Op0)) {
1433 if (
Constant *CRHS = dyn_cast<Constant>(Op1)) {
1467 (A == Op1 || B == Op1))
1472 (A == Op0 || B == Op0))
1506 if (isa<SelectInst>(Op0) || isa<SelectInst>(Op1))
1513 if (isa<PHINode>(Op0) || isa<PHINode>(Op1))
1530 unsigned MaxRecurse) {
1531 if (
Constant *CLHS = dyn_cast<Constant>(Op0)) {
1532 if (
Constant *CRHS = dyn_cast<Constant>(Op1)) {
1566 (A == Op1 || B == Op1))
1571 (A == Op0 || B == Op0))
1576 (A == Op1 || B == Op1))
1581 (A == Op0 || B == Op0))
1601 if (isa<SelectInst>(Op0) || isa<SelectInst>(Op1))
1608 if (isa<PHINode>(Op0) || isa<PHINode>(Op1))
1624 unsigned MaxRecurse) {
1625 if (
Constant *CLHS = dyn_cast<Constant>(Op0)) {
1626 if (
Constant *CRHS = dyn_cast<Constant>(Op1)) {
1697 if (Pred == Cmp->
getPredicate() && LHS == CmpLHS && RHS == CmpRHS)
1700 LHS == CmpRHS && RHS == CmpLHS)
1814 if (isa<AllocaInst>(LHS) &&
1815 (isa<AllocaInst>(RHS) || isa<GlobalVariable>(RHS))) {
1818 uint64_t LHSSize, RHSSize;
1819 if (LHSOffsetCI && RHSOffsetCI &&
1823 const APInt &RHSOffsetValue = RHSOffsetCI->getValue();
1826 LHSOffsetValue.
ult(LHSSize) &&
1827 RHSOffsetValue.
ult(RHSSize)) {
1835 if (!cast<PointerType>(LHS->
getType())->isEmptyTy() &&
1836 !cast<PointerType>(RHS->
getType())->isEmptyTy() &&
1862 const Query &Q,
unsigned MaxRecurse) {
1866 if (
Constant *CLHS = dyn_cast<Constant>(LHS)) {
1867 if (
Constant *CRHS = dyn_cast<Constant>(RHS))
1881 if (LHS == RHS || isa<UndefValue>(RHS))
1923 bool LHSKnownNonNegative, LHSKnownNegative;
1942 if (LHSKnownNegative)
1944 if (LHSKnownNonNegative)
1949 if (LHSKnownNegative)
1956 if (LHSKnownNegative)
1958 if (LHSKnownNonNegative)
1963 if (LHSKnownNegative)
1972 if (
ConstantInt *CI = dyn_cast<ConstantInt>(RHS)) {
1992 Lower = (-Upper) + 1;
2007 Lower = IntMin.
sdiv(Val);
2008 Upper = IntMax.
sdiv(Val) + 1;
2030 if (Lower != Upper) {
2040 if (isa<CastInst>(LHS) && (isa<Constant>(RHS) || isa<CastInst>(RHS))) {
2043 Type *SrcTy = SrcOp->getType();
2048 if (MaxRecurse && Q.
TD && isa<PtrToIntInst>(LI) &&
2050 if (
Constant *RHSC = dyn_cast<Constant>(RHS)) {
2056 }
else if (
PtrToIntInst *RI = dyn_cast<PtrToIntInst>(RHS)) {
2057 if (RI->getOperand(0)->getType() == SrcTy)
2065 if (isa<ZExtInst>(LHS)) {
2068 if (
ZExtInst *RI = dyn_cast<ZExtInst>(RHS)) {
2069 if (MaxRecurse && SrcTy == RI->getOperand(0)->getType())
2072 SrcOp, RI->getOperand(0), Q,
2078 else if (
ConstantInt *CI = dyn_cast<ConstantInt>(RHS)) {
2086 if (RExt == CI && MaxRecurse)
2088 SrcOp, Trunc, Q, MaxRecurse-1))
2111 return CI->getValue().isNegative() ?
2117 return CI->getValue().isNegative() ?
2125 if (isa<SExtInst>(LHS)) {
2128 if (
SExtInst *RI = dyn_cast<SExtInst>(RHS)) {
2129 if (MaxRecurse && SrcTy == RI->getOperand(0)->getType())
2137 else if (
ConstantInt *CI = dyn_cast<ConstantInt>(RHS)) {
2145 if (RExt == CI && MaxRecurse)
2163 return CI->getValue().isNegative() ?
2168 return CI->getValue().isNegative() ?
2201 if (MaxRecurse && (LBO || RBO)) {
2203 Value *
A = 0, *B = 0, *
C = 0, *D = 0;
2205 bool NoLHSWrapProblem =
false, NoRHSWrapProblem =
false;
2206 if (LBO && LBO->
getOpcode() == Instruction::Add) {
2212 if (RBO && RBO->getOpcode() == Instruction::Add) {
2213 C = RBO->getOperand(0); D = RBO->getOperand(1);
2220 if ((A == RHS || B == RHS) && NoLHSWrapProblem)
2227 if ((
C == LHS || D == LHS) && NoRHSWrapProblem)
2230 C == LHS ? D :
C, Q, MaxRecurse-1))
2234 if (A && C && (A == C || A == D || B == C || B == D) &&
2235 NoLHSWrapProblem && NoRHSWrapProblem) {
2242 }
else if (A == D) {
2246 }
else if (B == C) {
2263 bool KnownNonNegative, KnownNegative;
2270 if (!KnownNonNegative)
2280 if (!KnownNonNegative)
2292 bool KnownNonNegative, KnownNegative;
2299 if (!KnownNonNegative)
2309 if (!KnownNonNegative)
2328 if (MaxRecurse && LBO && RBO && LBO->
getOpcode() == RBO->getOpcode() &&
2332 case Instruction::UDiv:
2333 case Instruction::LShr:
2337 case Instruction::SDiv:
2338 case Instruction::AShr:
2339 if (!LBO->
isExact() || !RBO->isExact())
2342 RBO->getOperand(0), Q, MaxRecurse-1))
2345 case Instruction::Shl: {
2353 RBO->getOperand(0), Q, MaxRecurse-1))
2372 (A == LHS || B == LHS)) {
2378 (A == RHS || B == RHS)) {
2385 (A == LHS || B == LHS)) {
2442 (A == LHS || B == LHS)) {
2448 (A == RHS || B == RHS)) {
2455 (A == LHS || B == LHS)) {
2508 (A == C || A == D || B == C || B == D)) {
2518 (A == C || A == D || B == C || B == D)) {
2528 (A == C || A == D || B == C || B == D)) {
2538 (A == C || A == D || B == C || B == D)) {
2555 if (
GEPOperator *GRHS = dyn_cast<GEPOperator>(RHS)) {
2556 if (GLHS->getPointerOperand() == GRHS->getPointerOperand() &&
2557 GLHS->hasAllConstantIndices() && GRHS->hasAllConstantIndices() &&
2559 (GLHS->isInBounds() && GRHS->isInBounds() &&
2577 if (isa<SelectInst>(LHS) || isa<SelectInst>(RHS))
2583 if (isa<PHINode>(LHS) || isa<PHINode>(RHS))
2601 const Query &Q,
unsigned MaxRecurse) {
2605 if (
Constant *CLHS = dyn_cast<Constant>(LHS)) {
2606 if (
Constant *CRHS = dyn_cast<Constant>(RHS))
2620 if (isa<UndefValue>(RHS))
2632 if (
Constant *RHSC = dyn_cast<Constant>(RHS)) {
2634 if (
ConstantFP *CFP = dyn_cast<ConstantFP>(RHSC)) {
2635 if (CFP->getValueAPF().isNaN()) {
2639 "Comparison must be either ordered or unordered!");
2644 if (CFP->getValueAPF().isInfinity()) {
2645 if (CFP->getValueAPF().isNegative()) {
2674 if (isa<SelectInst>(LHS) || isa<SelectInst>(RHS))
2680 if (isa<PHINode>(LHS) || isa<PHINode>(RHS))
2699 unsigned MaxRecurse) {
2702 if (
ConstantInt *CB = dyn_cast<ConstantInt>(CondVal))
2703 return CB->getZExtValue() ? TrueVal : FalseVal;
2706 if (TrueVal == FalseVal)
2709 if (isa<UndefValue>(CondVal)) {
2710 if (isa<Constant>(TrueVal))
2714 if (isa<UndefValue>(TrueVal))
2716 if (isa<UndefValue>(FalseVal))
2740 if (Ops.
size() == 1)
2743 if (isa<UndefValue>(Ops[0])) {
2750 if (Ops.
size() == 2) {
2764 for (
unsigned i = 0, e = Ops.
size(); i != e; ++i)
2765 if (!isa<Constant>(Ops[i]))
2782 if (
Constant *CAgg = dyn_cast<Constant>(Agg))
2783 if (
Constant *CVal = dyn_cast<Constant>(Val))
2792 if (EV->getAggregateOperand()->getType() == Agg->
getType() &&
2793 EV->getIndices() == Idxs) {
2796 return EV->getAggregateOperand();
2799 if (Agg == EV->getAggregateOperand())
2819 Value *CommonValue = 0;
2820 bool HasUndefInput =
false;
2824 if (Incoming == PN)
continue;
2825 if (isa<UndefValue>(Incoming)) {
2827 HasUndefInput =
true;
2830 if (CommonValue && Incoming != CommonValue)
2832 CommonValue = Incoming;
2850 if (
Constant *
C = dyn_cast<Constant>(Op))
2867 const Query &Q,
unsigned MaxRecurse) {
2869 case Instruction::Add:
2872 case Instruction::FAdd:
2875 case Instruction::Sub:
2878 case Instruction::FSub:
2881 case Instruction::Mul:
return SimplifyMulInst (LHS, RHS, Q, MaxRecurse);
2882 case Instruction::FMul:
2890 case Instruction::Shl:
2893 case Instruction::LShr:
2895 case Instruction::AShr:
2901 if (
Constant *CLHS = dyn_cast<Constant>(LHS))
2902 if (
Constant *CRHS = dyn_cast<Constant>(RHS)) {
2915 if (isa<SelectInst>(LHS) || isa<SelectInst>(RHS))
2921 if (isa<PHINode>(LHS) || isa<PHINode>(RHS))
2938 const Query &Q,
unsigned MaxRecurse) {
2953 default:
return false;
2967 template <
typename IterTy>
2969 const Query &Q,
unsigned MaxRecurse) {
2975 if (std::distance(ArgBegin, ArgEnd) == 1)
2977 if (II->getIntrinsicID() == IID)
2983 template <
typename IterTy>
2985 const Query &Q,
unsigned MaxRecurse) {
2988 Ty = PTy->getElementType();
2992 if (isa<UndefValue>(V))
3008 ConstantArgs.
reserve(ArgEnd - ArgBegin);
3009 for (IterTy
I = ArgBegin, E = ArgEnd;
I != E; ++
I) {
3045 case Instruction::FAdd:
3049 case Instruction::Add:
3051 cast<BinaryOperator>(
I)->hasNoSignedWrap(),
3052 cast<BinaryOperator>(
I)->hasNoUnsignedWrap(),
3055 case Instruction::FSub:
3059 case Instruction::Sub:
3061 cast<BinaryOperator>(
I)->hasNoSignedWrap(),
3062 cast<BinaryOperator>(
I)->hasNoUnsignedWrap(),
3065 case Instruction::FMul:
3069 case Instruction::Mul:
3072 case Instruction::SDiv:
3075 case Instruction::UDiv:
3078 case Instruction::FDiv:
3081 case Instruction::SRem:
3084 case Instruction::URem:
3087 case Instruction::FRem:
3090 case Instruction::Shl:
3092 cast<BinaryOperator>(
I)->hasNoSignedWrap(),
3093 cast<BinaryOperator>(
I)->hasNoUnsignedWrap(),
3096 case Instruction::LShr:
3098 cast<BinaryOperator>(
I)->isExact(),
3101 case Instruction::AShr:
3103 cast<BinaryOperator>(
I)->isExact(),
3115 case Instruction::ICmp:
3119 case Instruction::FCmp:
3127 case Instruction::GetElementPtr: {
3132 case Instruction::InsertValue: {
3148 case Instruction::Trunc:
3174 bool Simplified =
false;
3183 Worklist.
insert(cast<Instruction>(*UI));
3197 for (
unsigned Idx = 0; Idx != Worklist.
size(); ++Idx) {
3212 Worklist.
insert(cast<Instruction>(*UI));
3236 assert(I != SimpleV &&
"replaceAndRecursivelySimplify(X,X) is not valid!");
3237 assert(SimpleV &&
"Must provide a simplified value.");
MaxMin_match< ICmpInst, LHS, RHS, umin_pred_ty > m_UMin(const LHS &L, const RHS &R)
BinaryOp_match< LHS, RHS, Instruction::And > m_And(const LHS &L, const RHS &R)
static Value * SimplifyCmpInst(unsigned, Value *, Value *, const Query &, unsigned)
APInt LLVM_ATTRIBUTE_UNUSED_RESULT ashr(unsigned shiftAmt) const
Arithmetic right-shift function.
void push_back(const T &Elt)
static ConstantInt * getFalse(LLVMContext &Context)
class_match< Value > m_Value()
m_Value() - Match an arbitrary value and ignore it.
Abstract base class of comparison instructions.
class_match< UndefValue > m_Undef()
m_Undef() - Match an arbitrary undef constant.
APInt LLVM_ATTRIBUTE_UNUSED_RESULT abs() const
Get the absolute value;.
static Value * SimplifyAddInst(Value *Op0, Value *Op1, bool isNSW, bool isNUW, const Query &Q, unsigned MaxRecurse)
static APInt getAllOnesValue(unsigned numBits)
Get the all-ones value.
BinaryOp_match< LHS, RHS, Instruction::Sub > m_Sub(const LHS &L, const RHS &R)
Value * getAggregateOperand()
static Type * makeCmpResultType(Type *opnd_type)
Create a result type for fcmp/icmp.
ArrayRef< unsigned > getIndices() const
Value * SimplifyTruncInst(Value *Op, Type *Ty, const DataLayout *TD=0, const TargetLibraryInfo *TLI=0, const DominatorTree *DT=0)
unsigned getScalarSizeInBits()
bool canConstantFoldCallTo(const Function *F)
bool isReachableFromEntry(const BasicBlock *A) const
BinaryOp_match< LHS, RHS, Instruction::SRem > m_SRem(const LHS &L, const RHS &R)
enable_if_c<!is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
unsigned getBitWidth() const
getBitWidth - Return the bitwidth of this constant.
This class represents zero extension of integer types.
Value * SimplifyFSubInst(Value *LHS, Value *RHS, FastMathFlags FMF, const DataLayout *TD=0, const TargetLibraryInfo *TLI=0, const DominatorTree *DT=0)
bool hasNoSignedWrap() const
static bool isOrdered(unsigned short predicate)
Determine if the predicate is an ordered operation.
BinaryOp_match< LHS, RHS, Instruction::Mul > m_Mul(const LHS &L, const RHS &R)
static Constant * getGetElementPtr(Constant *C, ArrayRef< Constant * > IdxList, bool InBounds=false)
static Value * SimplifyAssociativeBinOp(unsigned Opc, Value *LHS, Value *RHS, const Query &Q, unsigned MaxRecurse)
Predicate getInversePredicate() const
Return the inverse of the instruction's predicate.
static PointerType * get(Type *ElementType, unsigned AddressSpace)
BinaryOp_match< LHS, RHS, Instruction::AShr > m_AShr(const LHS &L, const RHS &R)
Value * SimplifyAddInst(Value *LHS, Value *RHS, bool isNSW, bool isNUW, const DataLayout *TD=0, const TargetLibraryInfo *TLI=0, const DominatorTree *DT=0)
bool isKnownNonNull(const Value *V, const TargetLibraryInfo *TLI=0)
bool isSigned() const
Determine if this instruction is using a signed comparison.
0 1 0 0 True if ordered and less than
FastMathFlags getFastMathFlags() const
const Function * getParent() const
Return the enclosing method, or null if none.
static Value * FactorizeBinOp(unsigned Opcode, Value *LHS, Value *RHS, unsigned OpcToExtract, const Query &Q, unsigned MaxRecurse)
This class represents a sign extension of integer types.
unsigned getAddressSpace() const
Return the address space of the Pointer type.
BinaryOp_match< LHS, RHS, Instruction::FSub > m_FSub(const LHS &L, const RHS &R)
Value * SimplifyInsertValueInst(Value *Agg, Value *Val, ArrayRef< unsigned > Idxs, const DataLayout *TD=0, const TargetLibraryInfo *TLI=0, const DominatorTree *DT=0)
static Constant * getTrue(Type *Ty)
static Constant * getSub(Constant *C1, Constant *C2, bool HasNUW=false, bool HasNSW=false)
static Value * SimplifySubInst(Value *Op0, Value *Op1, bool isNSW, bool isNUW, const Query &Q, unsigned MaxRecurse)
static Constant * computePointerDifference(const DataLayout *TD, Value *LHS, Value *RHS)
Compute the constant difference between two pointer values. If the difference is not a constant...
Value * SimplifyFCmpInst(unsigned Predicate, Value *LHS, Value *RHS, const DataLayout *TD=0, const TargetLibraryInfo *TLI=0, const DominatorTree *DT=0)
static Value * SimplifyLShrInst(Value *Op0, Value *Op1, bool isExact, const Query &Q, unsigned MaxRecurse)
size_type size() const
Determine the number of elements in the SetVector.
Value * SimplifyUDivInst(Value *LHS, Value *RHS, const DataLayout *TD=0, const TargetLibraryInfo *TLI=0, const DominatorTree *DT=0)
static APInt getSignedMaxValue(unsigned numBits)
Gets maximum signed value of APInt for a specific bit width.
LoopInfoBase< BlockT, LoopT > * LI
Value * SimplifySDivInst(Value *LHS, Value *RHS, const DataLayout *TD=0, const TargetLibraryInfo *TLI=0, const DominatorTree *DT=0)
static Constant * getNullValue(Type *Ty)
static Constant * getAdd(Constant *C1, Constant *C2, bool HasNUW=false, bool HasNSW=false)
bool isNegative() const
Determine sign of this APInt.
static Value * ThreadCmpOverPHI(CmpInst::Predicate Pred, Value *LHS, Value *RHS, const Query &Q, unsigned MaxRecurse)
bool match(Val *V, const Pattern &P)
static Value * SimplifySDivInst(Value *Op0, Value *Op1, const Query &Q, unsigned MaxRecurse)
bool hasNoNaNs() const
Determine whether the no-NaNs flag is set.
static Value * SimplifyFDivInst(Value *Op0, Value *Op1, const Query &Q, unsigned)
static Constant * getIntegerCast(Constant *C, Type *Ty, bool isSigned)
Create a ZExt, Bitcast or Trunc for integer -> integer casts.
Value * SimplifyAndInst(Value *LHS, Value *RHS, const DataLayout *TD=0, const TargetLibraryInfo *TLI=0, const DominatorTree *DT=0)
const APInt & getValue() const
Return the constant's value.
Exact_match< T > m_Exact(const T &SubPattern)
BinOp2_match< LHS, RHS, Instruction::LShr, Instruction::AShr > m_Shr(const LHS &L, const RHS &R)
m_Shr - Matches LShr or AShr.
#define llvm_unreachable(msg)
APInt LLVM_ATTRIBUTE_UNUSED_RESULT lshr(unsigned shiftAmt) const
Logical right-shift function.
CastClass_match< OpTy, Instruction::Trunc > m_Trunc(const OpTy &Op)
m_Trunc
static Value * SimplifyICmpInst(unsigned Predicate, Value *LHS, Value *RHS, const Query &Q, unsigned MaxRecurse)
ID
LLVM Calling Convention Representation.
bool replaceAndRecursivelySimplify(Instruction *I, Value *SimpleV, const DataLayout *TD=0, const TargetLibraryInfo *TLI=0, const DominatorTree *DT=0)
Replace all uses of 'I' with 'SimpleV' and simplify the uses recursively.
static Value * SimplifyAShrInst(Value *Op0, Value *Op1, bool isExact, const Query &Q, unsigned MaxRecurse)
static Value * SimplifyOrInst(Value *, Value *, const Query &, unsigned)
not_match< LHS > m_Not(const LHS &L)
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 Value * SimplifyGEPInst(ArrayRef< Value * > Ops, const Query &Q, unsigned)
static Constant * stripAndComputeConstantOffsets(const DataLayout *TD, Value *&V, bool AllowNonInbounds=false)
Compute the base pointer and cumulative constant offsets for V.
static Value * SimplifyFAddInst(Value *Op0, Value *Op1, FastMathFlags FMF, const Query &Q, unsigned MaxRecurse)
static Value * SimplifyBinOp(unsigned, Value *, Value *, const Query &, unsigned)
bool insert(const value_type &X)
Insert a new element into the SetVector.
bool contains(const APInt &Val) const
static Value * ExtractEquivalentCondition(Value *V, CmpInst::Predicate Pred, Value *LHS, Value *RHS)
bool isAssociative() const
static Value * SimplifyPHINode(PHINode *PN, const Query &Q)
SimplifyPHINode - See if we can fold the given phi. If not, returns null.
ArrayRef< T > slice(unsigned N) const
slice(n) - Chop off the first N elements of the array.
static Constant * getICmp(unsigned short pred, Constant *LHS, Constant *RHS)
Value * SimplifyFMulInst(Value *LHS, Value *RHS, FastMathFlags FMF, const DataLayout *TD=0, const TargetLibraryInfo *TLI=0, const DominatorTree *DT=0)
MaxMin_match< ICmpInst, LHS, RHS, smin_pred_ty > m_SMin(const LHS &L, const RHS &R)
STATISTIC(NumExpand,"Number of expansions")
static Value * SimplifyTruncInst(Value *, Type *, const Query &, unsigned)
Value * getInsertedValueOperand()
class_match< ConstantInt > m_ConstantInt()
m_ConstantInt() - Match an arbitrary ConstantInt and ignore it.
Constant * ConstantFoldInstruction(Instruction *I, const DataLayout *TD=0, const TargetLibraryInfo *TLI=0)
Predicate getUnsignedPredicate() const
Return the unsigned version of the predicate.
static Value * SimplifyAndInst(Value *, Value *, const Query &, unsigned)
Constant * ConstantFoldInsertValueInstruction(Constant *Agg, Constant *Val, ArrayRef< unsigned > Idxs)
ValTy * getCalledValue() const
void replaceAllUsesWith(Value *V)
static Value * ThreadBinOpOverSelect(unsigned Opcode, Value *LHS, Value *RHS, const Query &Q, unsigned MaxRecurse)
static Value * SimplifyFSubInst(Value *Op0, Value *Op1, FastMathFlags FMF, const Query &Q, unsigned MaxRecurse)
static Constant * getIntToPtr(Constant *C, Type *Ty)
Query(const DataLayout *td, const TargetLibraryInfo *tli, const DominatorTree *dt)
Value * SimplifyICmpInst(unsigned Predicate, Value *LHS, Value *RHS, const DataLayout *TD=0, const TargetLibraryInfo *TLI=0, const DominatorTree *DT=0)
Type * getElementType() const
size_t size() const
size - Get the array size.
Value * SimplifyGEPInst(ArrayRef< Value * > Ops, const DataLayout *TD=0, const TargetLibraryInfo *TLI=0, const DominatorTree *DT=0)
static Value * SimplifyURemInst(Value *Op0, Value *Op1, const Query &Q, unsigned MaxRecurse)
bool ult(const APInt &RHS) const
Unsigned less than comparison.
unsigned getNumIncomingValues() const
Constant * ConstantFoldCall(Function *F, ArrayRef< Constant * > Operands, const TargetLibraryInfo *TLI=0)
BinaryOp_match< LHS, RHS, Instruction::LShr > m_LShr(const LHS &L, const RHS &R)
static Constant * computePointerICmp(const DataLayout *TD, const TargetLibraryInfo *TLI, CmpInst::Predicate Pred, Value *LHS, Value *RHS)
MaxMin_match< ICmpInst, LHS, RHS, umax_pred_ty > m_UMax(const LHS &L, const RHS &R)
BinaryOp_match< LHS, RHS, Instruction::SDiv > m_SDiv(const LHS &L, const RHS &R)
static Value * SimplifySelectInst(Value *CondVal, Value *TrueVal, Value *FalseVal, const Query &Q, unsigned MaxRecurse)
BinaryOp_match< LHS, RHS, Instruction::Or > m_Or(const LHS &L, const RHS &R)
const TargetLibraryInfo * TLI
unsigned getIntrinsicID() const LLVM_READONLY
LLVM Constant Representation.
static Value * SimplifyCall(Value *V, IterTy ArgBegin, IterTy ArgEnd, const Query &Q, unsigned MaxRecurse)
const Value * getCondition() const
static Value * ThreadCmpOverSelect(CmpInst::Predicate Pred, Value *LHS, Value *RHS, const Query &Q, unsigned MaxRecurse)
static Value * SimplifySRemInst(Value *Op0, Value *Op1, const Query &Q, unsigned MaxRecurse)
APInt Or(const APInt &LHS, const APInt &RHS)
Bitwise OR function for APInt.
static Value * SimplifyFMulInst(Value *Op0, Value *Op1, FastMathFlags FMF, const Query &Q, unsigned MaxRecurse)
Given the operands for an FMul, see if we can fold the result.
APInt Xor(const APInt &LHS, const APInt &RHS)
Bitwise XOR function for APInt.
static Value * SimplifyDiv(Instruction::BinaryOps Opcode, Value *Op0, Value *Op1, const Query &Q, unsigned MaxRecurse)
cst_pred_ty< is_all_ones > m_AllOnes()
m_AllOnes() - Match an integer or vector with all bits set to true.
bool isIntPredicate() const
specificval_ty m_Specific(const Value *V)
m_Specific - Match if we have a specific specified value.
BinaryOp_match< LHS, RHS, Instruction::Shl > m_Shl(const LHS &L, const RHS &R)
bool isFalseWhenEqual() const
Determine if this is false when both operands are the same.
Value * SimplifyShlInst(Value *Op0, Value *Op1, bool isNSW, bool isNUW, const DataLayout *TD=0, const TargetLibraryInfo *TLI=0, const DominatorTree *DT=0)
APInt LLVM_ATTRIBUTE_UNUSED_RESULT sdiv(const APInt &RHS) const
Signed division function for APInt.
Value * getOperand(unsigned i) const
bool isCommutative() const
Constant * ConstantFoldCompareInstOperands(unsigned Predicate, Constant *LHS, Constant *RHS, const DataLayout *TD=0, const TargetLibraryInfo *TLI=0)
Predicate getPredicate() const
Return the predicate for this instruction.
static bool ValueDominatesPHI(Value *V, PHINode *P, const DominatorTree *DT)
ValueDominatesPHI - Does the given value dominate the specified phi node?
static Constant * getAllOnesValue(Type *Ty)
Get the all ones value.
1 1 1 1 Always true (always folded)
bool dominates(const DomTreeNode *A, const DomTreeNode *B) const
static Value * SimplifyInsertValueInst(Value *Agg, Value *Val, ArrayRef< unsigned > Idxs, const Query &Q, unsigned)
match_combine_or< match_zero, match_neg_zero > m_AnyZero()
static UndefValue * get(Type *T)
Value * SimplifyInstruction(Instruction *I, const DataLayout *TD=0, const TargetLibraryInfo *TLI=0, const DominatorTree *DT=0)
LLVMContext & getContext() const
All values hold a context through their type.
bool hasNoSignedWrap() const
hasNoSignedWrap - Determine whether the no signed wrap flag is set.
const Value * getTrueValue() const
1 1 0 1 True if unordered, less than, or equal
Value * SimplifyBinOp(unsigned Opcode, Value *LHS, Value *RHS, const DataLayout *TD=0, const TargetLibraryInfo *TLI=0, const DominatorTree *DT=0)
static Value * ExpandBinOp(unsigned Opcode, Value *LHS, Value *RHS, unsigned OpcToExpand, const Query &Q, unsigned MaxRecurse)
neg_match< LHS > m_Neg(const LHS &L)
m_Neg - Match an integer negate.
IntegerType * getIntPtrType(LLVMContext &C, unsigned AddressSpace=0) const
static Value * SimplifyUDivInst(Value *Op0, Value *Op1, const Query &Q, unsigned MaxRecurse)
0 0 1 0 True if ordered and greater than
BinaryOps getOpcode() const
static Constant * getSplat(unsigned NumElts, Constant *Elt)
static IntegerType * get(LLVMContext &C, unsigned NumBits)
Get or create an IntegerType instance.
A SetVector that performs no allocations if smaller than a certain size.
static Value * SimplifyRem(Instruction::BinaryOps Opcode, Value *Op0, Value *Op1, const Query &Q, unsigned MaxRecurse)
unsigned getIntegerBitWidth() const
Class for constant integers.
static Value * SimplifyShlInst(Value *Op0, Value *Op1, bool isNSW, bool isNUW, const Query &Q, unsigned MaxRecurse)
Value * getIncomingValue(unsigned i) const
uint64_t getTypeAllocSize(Type *Ty) const
unsigned getVectorNumElements() const
ConstantRange inverse() const
BinaryOp_match< LHS, RHS, Instruction::URem > m_URem(const LHS &L, const RHS &R)
BinaryOp_match< LHS, RHS, Instruction::UDiv > m_UDiv(const LHS &L, const RHS &R)
bool isTrueWhenEqual() const
Determine if this is true when both operands are the same.
bool isKnownToBeAPowerOfTwo(Value *V, bool OrZero=false, unsigned Depth=0)
Value * stripPointerCasts()
Strips off any unneeded pointer casts, all-zero GEPs and aliases from the specified value...
Predicate getSwappedPredicate() const
Return the predicate as if the operands were swapped.
static Constant * get(Type *Ty, uint64_t V, bool isSigned=false)
static Value * SimplifyShift(unsigned Opcode, Value *Op0, Value *Op1, const Query &Q, unsigned MaxRecurse)
static Constant * getTrunc(Constant *C, Type *Ty)
const BasicBlock & getEntryBlock() const
Value * SimplifyCmpInst(unsigned Predicate, Value *LHS, Value *RHS, const DataLayout *TD=0, const TargetLibraryInfo *TLI=0, const DominatorTree *DT=0)
bool isExact() const
isExact - Determine whether the exact flag is set.
Value * SimplifyXorInst(Value *LHS, Value *RHS, const DataLayout *TD=0, const TargetLibraryInfo *TLI=0, const DominatorTree *DT=0)
static ConstantInt * getTrue(LLVMContext &Context)
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
Class for arbitrary precision integers.
static Value * SimplifyIntrinsic(Intrinsic::ID IID, IterTy ArgBegin, IterTy ArgEnd, const Query &Q, unsigned MaxRecurse)
static Constant * getFalse(Type *Ty)
bool noNaNs()
Flag queries.
CastClass_match< OpTy, Instruction::PtrToInt > m_PtrToInt(const OpTy &Op)
m_PtrToInt
static bool replaceAndRecursivelySimplifyImpl(Instruction *I, Value *SimpleV, const DataLayout *TD, const TargetLibraryInfo *TLI, const DominatorTree *DT)
Implementation of recursive simplification through an instructions uses.
bool isMinValue() const
Determine if this is the smallest unsigned value.
bool isFPPredicate() const
APInt And(const APInt &LHS, const APInt &RHS)
Bitwise AND function for APInt.
unsigned getOpcode() const
void ComputeSignBit(Value *V, bool &KnownZero, bool &KnownOne, const DataLayout *TD=0, unsigned Depth=0)
specific_fpval m_FPOne()
Match a float 1.0 or vector with all elements equal to 1.0.
Value * SimplifySRemInst(Value *LHS, Value *RHS, const DataLayout *TD=0, const TargetLibraryInfo *TLI=0, const DominatorTree *DT=0)
Value * SimplifySelectInst(Value *Cond, Value *TrueVal, Value *FalseVal, const DataLayout *TD=0, const TargetLibraryInfo *TLI=0, const DominatorTree *DT=0)
APInt LLVM_ATTRIBUTE_UNUSED_RESULT udiv(const APInt &RHS) const
Unsigned division operation.
unsigned greater or equal
Value * SimplifyOrInst(Value *LHS, Value *RHS, const DataLayout *TD=0, const TargetLibraryInfo *TLI=0, const DominatorTree *DT=0)
static Value * SimplifyFRemInst(Value *Op0, Value *Op1, const Query &, unsigned)
bool hasNoInfs() const
Determine whether the no-infs flag is set.
MaxMin_match< ICmpInst, LHS, RHS, smax_pred_ty > m_SMax(const LHS &L, const RHS &R)
static Value * SimplifyMulInst(Value *Op0, Value *Op1, const Query &Q, unsigned MaxRecurse)
static ConstantRange makeConstantRange(Predicate pred, const APInt &C)
Make a ConstantRange for a relation with a constant value.
const Type * getScalarType() const
unsigned getPrimitiveSizeInBits() const
static bool IsIdempotent(Intrinsic::ID ID)
static Value * SimplifyFCmpInst(unsigned Predicate, Value *LHS, Value *RHS, const Query &Q, unsigned MaxRecurse)
Value * SimplifyFDivInst(Value *LHS, Value *RHS, const DataLayout *TD=0, const TargetLibraryInfo *TLI=0, const DominatorTree *DT=0)
BinOp2_match< LHS, RHS, Instruction::SDiv, Instruction::UDiv > m_IDiv(const LHS &L, const RHS &R)
m_IDiv - Matches UDiv and SDiv.
bool CannotBeNegativeZero(const Value *V, unsigned Depth=0)
bool isUnsigned() const
Determine if this instruction is using an unsigned comparison.
match_neg_zero m_NegZero()
Type * getReturnType() const
static Type * getIndexedType(Type *Ptr, ArrayRef< Value * > IdxList)
static APInt getSignedMinValue(unsigned numBits)
Gets minimum signed value of APInt for a specific bit width.
Value * SimplifyURemInst(Value *LHS, Value *RHS, const DataLayout *TD=0, const TargetLibraryInfo *TLI=0, const DominatorTree *DT=0)
LLVM Value Representation.
bool hasNoUnsignedWrap() const
hasNoUnsignedWrap - Determine whether the no unsigned wrap flag is set.
1 0 1 1 True if unordered, greater than, or equal
unsigned getOpcode() const
getOpcode() returns a member of one of the enums like Instruction::Add.
cst_pred_ty< is_one > m_One()
m_One() - Match an integer 1 or a vector with all elements equal to 1.
Value * SimplifyCall(Value *V, User::op_iterator ArgBegin, User::op_iterator ArgEnd, const DataLayout *TD=0, const TargetLibraryInfo *TLI=0, const DominatorTree *DT=0)
Given a function and iterators over arguments, see if we can fold the result.
Value * SimplifyLShrInst(Value *Op0, Value *Op1, bool isExact, const DataLayout *TD=0, const TargetLibraryInfo *TLI=0, const DominatorTree *DT=0)
bool isKnownNonZero(Value *V, const DataLayout *TD=0, unsigned Depth=0)
static bool isUnordered(unsigned short predicate)
Determine if the predicate is an unordered operation.
Constant * ConstantFoldInstOperands(unsigned Opcode, Type *DestTy, ArrayRef< Constant * > Ops, const DataLayout *TD=0, const TargetLibraryInfo *TLI=0)
uint64_t getTypeSizeInBits(Type *Ty) const
static Value * ThreadBinOpOverPHI(unsigned Opcode, Value *LHS, Value *RHS, const Query &Q, unsigned MaxRecurse)
Predicate getSignedPredicate() const
Return the signed version of the predicate.
const Value * getFalseValue() const
Convenience struct for specifying and reasoning about fast-math flags.
Value * SimplifyFRemInst(Value *LHS, Value *RHS, const DataLayout *TD=0, const TargetLibraryInfo *TLI=0, const DominatorTree *DT=0)
Value * SimplifyAShrInst(Value *Op0, Value *Op1, bool isExact, const DataLayout *TD=0, const TargetLibraryInfo *TLI=0, const DominatorTree *DT=0)
static GCMetadataPrinterRegistry::Add< OcamlGCMetadataPrinter > Y("ocaml","ocaml 3.10-compatible collector")
bool getObjectSize(const Value *Ptr, uint64_t &Size, const DataLayout *DL, const TargetLibraryInfo *TLI, bool RoundToAlign=false)
Compute the size of the object pointed by Ptr. Returns true and the object size in Size if successful...
static APInt getNullValue(unsigned numBits)
Get the '0' value.
static bool isSameCompare(Value *V, CmpInst::Predicate Pred, Value *LHS, Value *RHS)
isSameCompare - Is V equivalent to the comparison "LHS Pred RHS"?
Value * SimplifySubInst(Value *LHS, Value *RHS, bool isNSW, bool isNUW, const DataLayout *TD=0, const TargetLibraryInfo *TLI=0, const DominatorTree *DT=0)
static Constant * getCast(unsigned ops, Constant *C, Type *Ty)
static Value * SimplifyXorInst(Value *, Value *, const Query &, unsigned)
static RegisterPass< NVPTXAllocaHoisting > X("alloca-hoisting","Hoisting alloca instructions in non-entry ""blocks to the entry block")
const BasicBlock * getParent() const
INITIALIZE_PASS(GlobalMerge,"global-merge","Global Merge", false, false) bool GlobalMerge const DataLayout * TD
static Type * GetCompareTy(Value *Op)
bool hasNoUnsignedWrap() const
Value * SimplifyMulInst(Value *LHS, Value *RHS, const DataLayout *TD=0, const TargetLibraryInfo *TLI=0, const DominatorTree *DT=0)
0 0 0 0 Always false (always folded)
Value * SimplifyFAddInst(Value *LHS, Value *RHS, FastMathFlags FMF, const DataLayout *TD=0, const TargetLibraryInfo *TLI=0, const DominatorTree *DT=0)
bool recursivelySimplifyInstruction(Instruction *I, const DataLayout *TD=0, const TargetLibraryInfo *TLI=0, const DominatorTree *DT=0)
Recursively attempt to simplify an instruction.