@@ -1079,31 +1079,31 @@ Instruction *InstCombiner::foldAllocaCmp(ICmpInst &ICI,
1079
1079
ConstantInt::get (CmpTy, !CmpInst::isTrueWhenEqual (ICI.getPredicate ())));
1080
1080
}
1081
1081
1082
- // / Fold "icmp pred (X+CI ), X".
1083
- Instruction *InstCombiner::foldICmpAddOpConst (Value *X, ConstantInt *CI ,
1082
+ // / Fold "icmp pred (X+C ), X".
1083
+ Instruction *InstCombiner::foldICmpAddOpConst (Value *X, const APInt &C ,
1084
1084
ICmpInst::Predicate Pred) {
1085
1085
// From this point on, we know that (X+C <= X) --> (X+C < X) because C != 0,
1086
1086
// so the values can never be equal. Similarly for all other "or equals"
1087
1087
// operators.
1088
+ assert (!!C && " C should not be zero!" );
1088
1089
1089
1090
// (X+1) <u X --> X >u (MAXUINT-1) --> X == 255
1090
1091
// (X+2) <u X --> X >u (MAXUINT-2) --> X > 253
1091
1092
// (X+MAXUINT) <u X --> X >u (MAXUINT-MAXUINT) --> X != 0
1092
1093
if (Pred == ICmpInst::ICMP_ULT || Pred == ICmpInst::ICMP_ULE) {
1093
- Value *R =
1094
- ConstantExpr::getSub ( ConstantInt::getAllOnesValue (CI-> getType ()), CI );
1094
+ Constant *R = ConstantInt::get (X-> getType (),
1095
+ APInt::getMaxValue (C. getBitWidth ()) - C );
1095
1096
return new ICmpInst (ICmpInst::ICMP_UGT, X, R);
1096
1097
}
1097
1098
1098
1099
// (X+1) >u X --> X <u (0-1) --> X != 255
1099
1100
// (X+2) >u X --> X <u (0-2) --> X <u 254
1100
1101
// (X+MAXUINT) >u X --> X <u (0-MAXUINT) --> X <u 1 --> X == 0
1101
1102
if (Pred == ICmpInst::ICMP_UGT || Pred == ICmpInst::ICMP_UGE)
1102
- return new ICmpInst (ICmpInst::ICMP_ULT, X, ConstantExpr::getNeg (CI));
1103
+ return new ICmpInst (ICmpInst::ICMP_ULT, X,
1104
+ ConstantInt::get (X->getType (), -C));
1103
1105
1104
- unsigned BitWidth = CI->getType ()->getPrimitiveSizeInBits ();
1105
- ConstantInt *SMax = ConstantInt::get (X->getContext (),
1106
- APInt::getSignedMaxValue (BitWidth));
1106
+ APInt SMax = APInt::getSignedMaxValue (C.getBitWidth ());
1107
1107
1108
1108
// (X+ 1) <s X --> X >s (MAXSINT-1) --> X == 127
1109
1109
// (X+ 2) <s X --> X >s (MAXSINT-2) --> X >s 125
@@ -1112,7 +1112,8 @@ Instruction *InstCombiner::foldICmpAddOpConst(Value *X, ConstantInt *CI,
1112
1112
// (X+ -2) <s X --> X >s (MAXSINT- -2) --> X >s 126
1113
1113
// (X+ -1) <s X --> X >s (MAXSINT- -1) --> X != 127
1114
1114
if (Pred == ICmpInst::ICMP_SLT || Pred == ICmpInst::ICMP_SLE)
1115
- return new ICmpInst (ICmpInst::ICMP_SGT, X, ConstantExpr::getSub (SMax, CI));
1115
+ return new ICmpInst (ICmpInst::ICMP_SGT, X,
1116
+ ConstantInt::get (X->getType (), SMax - C));
1116
1117
1117
1118
// (X+ 1) >s X --> X <s (MAXSINT-(1-1)) --> X != 127
1118
1119
// (X+ 2) >s X --> X <s (MAXSINT-(2-1)) --> X <s 126
@@ -1122,8 +1123,8 @@ Instruction *InstCombiner::foldICmpAddOpConst(Value *X, ConstantInt *CI,
1122
1123
// (X+ -1) >s X --> X <s (MAXSINT-(-1-1)) --> X == -128
1123
1124
1124
1125
assert (Pred == ICmpInst::ICMP_SGT || Pred == ICmpInst::ICMP_SGE);
1125
- Constant *C = Builder. getInt (CI-> getValue () - 1 );
1126
- return new ICmpInst (ICmpInst::ICMP_SLT, X, ConstantExpr::getSub (SMax, C ));
1126
+ return new ICmpInst (ICmpInst::ICMP_SLT, X,
1127
+ ConstantInt::get (X-> getType (), SMax - (C - 1 ) ));
1127
1128
}
1128
1129
1129
1130
// / Handle "(icmp eq/ne (ashr/lshr AP2, A), AP1)" ->
@@ -4877,14 +4878,15 @@ Instruction *InstCombiner::visitICmpInst(ICmpInst &I) {
4877
4878
return ExtractValueInst::Create (ACXI, 1 );
4878
4879
4879
4880
{
4880
- Value *X; ConstantInt *Cst;
4881
+ Value *X;
4882
+ const APInt *C;
4881
4883
// icmp X+Cst, X
4882
- if (match (Op0, m_Add (m_Value (X), m_ConstantInt (Cst ))) && Op1 == X)
4883
- return foldICmpAddOpConst (X, Cst , I.getPredicate ());
4884
+ if (match (Op0, m_Add (m_Value (X), m_APInt (C ))) && Op1 == X)
4885
+ return foldICmpAddOpConst (X, *C , I.getPredicate ());
4884
4886
4885
4887
// icmp X, X+Cst
4886
- if (match (Op1, m_Add (m_Value (X), m_ConstantInt (Cst ))) && Op0 == X)
4887
- return foldICmpAddOpConst (X, Cst , I.getSwappedPredicate ());
4888
+ if (match (Op1, m_Add (m_Value (X), m_APInt (C ))) && Op0 == X)
4889
+ return foldICmpAddOpConst (X, *C , I.getSwappedPredicate ());
4888
4890
}
4889
4891
4890
4892
if (I.getType ()->isVectorTy ())
0 commit comments