@@ -1569,7 +1569,8 @@ Instruction *InstCombiner::foldICmpTruncConstant(ICmpInst &Cmp,
1569
1569
}
1570
1570
1571
1571
// / Fold icmp (xor X, Y), C.
1572
- Instruction *InstCombiner::foldICmpXorConstant (ICmpInst &Cmp, Instruction *Xor,
1572
+ Instruction *InstCombiner::foldICmpXorConstant (ICmpInst &Cmp,
1573
+ BinaryOperator *Xor,
1573
1574
const APInt *C) {
1574
1575
Value *X = Xor->getOperand (0 );
1575
1576
Value *Y = Xor->getOperand (1 );
@@ -1634,7 +1635,8 @@ Instruction *InstCombiner::foldICmpXorConstant(ICmpInst &Cmp, Instruction *Xor,
1634
1635
return nullptr ;
1635
1636
}
1636
1637
1637
- Instruction *InstCombiner::foldICmpAndConstant (ICmpInst &ICI, Instruction *LHSI,
1638
+ Instruction *InstCombiner::foldICmpAndConstant (ICmpInst &ICI,
1639
+ BinaryOperator *LHSI,
1638
1640
const APInt *RHSV) {
1639
1641
// FIXME: This check restricts all folds under here to scalar types.
1640
1642
ConstantInt *RHS = dyn_cast<ConstantInt>(ICI.getOperand (1 ));
@@ -1875,7 +1877,7 @@ Instruction *InstCombiner::foldICmpAndConstant(ICmpInst &ICI, Instruction *LHSI,
1875
1877
}
1876
1878
1877
1879
// / Fold icmp (or X, Y), C.
1878
- Instruction *InstCombiner::foldICmpOrConstant (ICmpInst &Cmp, Instruction *Or,
1880
+ Instruction *InstCombiner::foldICmpOrConstant (ICmpInst &Cmp, BinaryOperator *Or,
1879
1881
const APInt *C) {
1880
1882
ICmpInst::Predicate Pred = Cmp.getPredicate ();
1881
1883
if (*C == 1 ) {
@@ -1906,7 +1908,8 @@ Instruction *InstCombiner::foldICmpOrConstant(ICmpInst &Cmp, Instruction *Or,
1906
1908
}
1907
1909
1908
1910
// / Fold icmp (mul X, Y), C.
1909
- Instruction *InstCombiner::foldICmpMulConstant (ICmpInst &Cmp, Instruction *Mul,
1911
+ Instruction *InstCombiner::foldICmpMulConstant (ICmpInst &Cmp,
1912
+ BinaryOperator *Mul,
1910
1913
const APInt *C) {
1911
1914
const APInt *MulC;
1912
1915
if (!match (Mul->getOperand (1 ), m_APInt (MulC)))
@@ -1915,7 +1918,7 @@ Instruction *InstCombiner::foldICmpMulConstant(ICmpInst &Cmp, Instruction *Mul,
1915
1918
// If this is a test of the sign bit and the multiply is sign-preserving with
1916
1919
// a constant operand, use the multiply LHS operand instead.
1917
1920
ICmpInst::Predicate Pred = Cmp.getPredicate ();
1918
- if (isSignTest (Pred, *C) && cast<BinaryOperator>( Mul) ->hasNoSignedWrap ()) {
1921
+ if (isSignTest (Pred, *C) && Mul->hasNoSignedWrap ()) {
1919
1922
if (MulC->isNegative ())
1920
1923
Pred = ICmpInst::getSwappedPredicate (Pred);
1921
1924
return new ICmpInst (Pred, Mul->getOperand (0 ),
@@ -1988,7 +1991,8 @@ static Instruction *foldICmpShlOne(ICmpInst &Cmp, Instruction *Shl,
1988
1991
}
1989
1992
1990
1993
// / Fold icmp (shl X, Y), C.
1991
- Instruction *InstCombiner::foldICmpShlConstant (ICmpInst &Cmp, Instruction *Shl,
1994
+ Instruction *InstCombiner::foldICmpShlConstant (ICmpInst &Cmp,
1995
+ BinaryOperator *Shl,
1992
1996
const APInt *C) {
1993
1997
const APInt *ShiftAmt;
1994
1998
if (!match (Shl->getOperand (1 ), m_APInt (ShiftAmt)))
@@ -2006,12 +2010,12 @@ Instruction *InstCombiner::foldICmpShlConstant(ICmpInst &Cmp, Instruction *Shl,
2006
2010
// If the shift is NUW, then it is just shifting out zeros, no need for an
2007
2011
// AND.
2008
2012
Constant *LShrC = ConstantInt::get (Shl->getType (), C->lshr (*ShiftAmt));
2009
- if (cast<BinaryOperator>( Shl) ->hasNoUnsignedWrap ())
2013
+ if (Shl->hasNoUnsignedWrap ())
2010
2014
return new ICmpInst (Pred, X, LShrC);
2011
2015
2012
2016
// If the shift is NSW and we compare to 0, then it is just shifting out
2013
2017
// sign bits, no need for an AND either.
2014
- if (cast<BinaryOperator>( Shl) ->hasNoSignedWrap () && *C == 0 )
2018
+ if (Shl->hasNoSignedWrap () && *C == 0 )
2015
2019
return new ICmpInst (Pred, X, LShrC);
2016
2020
2017
2021
if (Shl->hasOneUse ()) {
@@ -2027,7 +2031,7 @@ Instruction *InstCombiner::foldICmpShlConstant(ICmpInst &Cmp, Instruction *Shl,
2027
2031
// If this is a signed comparison to 0 and the shift is sign preserving,
2028
2032
// use the shift LHS operand instead; isSignTest may change 'Pred', so only
2029
2033
// do that if we're sure to not continue on in this function.
2030
- if (cast<BinaryOperator>( Shl) ->hasNoSignedWrap () && isSignTest (Pred, *C))
2034
+ if (Shl->hasNoSignedWrap () && isSignTest (Pred, *C))
2031
2035
return new ICmpInst (Pred, X, Constant::getNullValue (X->getType ()));
2032
2036
2033
2037
// Otherwise, if this is a comparison of the sign bit, simplify to and/test.
@@ -2062,30 +2066,30 @@ Instruction *InstCombiner::foldICmpShlConstant(ICmpInst &Cmp, Instruction *Shl,
2062
2066
}
2063
2067
2064
2068
// / Fold icmp ({al}shr X, Y), C.
2065
- Instruction *InstCombiner::foldICmpShrConstant (ICmpInst &ICI, Instruction *LHSI,
2066
- const APInt *RHSV) {
2069
+ Instruction *InstCombiner::foldICmpShrConstant (ICmpInst &Cmp,
2070
+ BinaryOperator *Shr,
2071
+ const APInt *C) {
2067
2072
// An exact shr only shifts out zero bits, so:
2068
2073
// icmp eq/ne (shr X, Y), 0 --> icmp eq/ne X, 0
2069
- CmpInst::Predicate Pred = ICI.getPredicate ();
2070
- BinaryOperator *BO = cast<BinaryOperator>(LHSI);
2071
- if (ICI.isEquality () && BO->isExact () && BO->hasOneUse () && *RHSV == 0 )
2072
- return new ICmpInst (Pred, BO->getOperand (0 ), ICI.getOperand (1 ));
2074
+ CmpInst::Predicate Pred = Cmp.getPredicate ();
2075
+ if (Cmp.isEquality () && Shr->isExact () && Shr->hasOneUse () && *C == 0 )
2076
+ return new ICmpInst (Pred, Shr->getOperand (0 ), Cmp.getOperand (1 ));
2073
2077
2074
2078
// FIXME: This check restricts all folds under here to scalar types.
2075
2079
// Handle equality comparisons of shift-by-constant.
2076
- ConstantInt *ShAmt = dyn_cast<ConstantInt>(LHSI ->getOperand (1 ));
2080
+ ConstantInt *ShAmt = dyn_cast<ConstantInt>(Shr ->getOperand (1 ));
2077
2081
if (!ShAmt)
2078
2082
return nullptr ;
2079
2083
2080
- if (Instruction *Res = foldICmpShrConstConst (ICI, BO , ShAmt))
2084
+ if (Instruction *Res = foldICmpShrConstConst (Cmp, Shr , ShAmt))
2081
2085
return Res;
2082
2086
2083
2087
return nullptr ;
2084
2088
}
2085
2089
2086
2090
// / Fold icmp (udiv X, Y), C.
2087
2091
Instruction *InstCombiner::foldICmpUDivConstant (ICmpInst &Cmp,
2088
- Instruction *UDiv,
2092
+ BinaryOperator *UDiv,
2089
2093
const APInt *C) {
2090
2094
const APInt *C2;
2091
2095
if (!match (UDiv->getOperand (0 ), m_APInt (C2)))
@@ -2112,7 +2116,8 @@ Instruction *InstCombiner::foldICmpUDivConstant(ICmpInst &Cmp,
2112
2116
return nullptr ;
2113
2117
}
2114
2118
2115
- Instruction *InstCombiner::foldICmpDivConstant (ICmpInst &ICI, Instruction *LHSI,
2119
+ Instruction *InstCombiner::foldICmpDivConstant (ICmpInst &ICI,
2120
+ BinaryOperator *LHSI,
2116
2121
const APInt *RHSV) {
2117
2122
// FIXME: This check restricts all folds under here to scalar types.
2118
2123
ConstantInt *RHS = dyn_cast<ConstantInt>(ICI.getOperand (1 ));
@@ -2127,14 +2132,15 @@ Instruction *InstCombiner::foldICmpDivConstant(ICmpInst &ICI, Instruction *LHSI,
2127
2132
// See: InsertRangeTest above for the kinds of replacements possible.
2128
2133
if (ConstantInt *DivRHS = dyn_cast<ConstantInt>(LHSI->getOperand (1 )))
2129
2134
if (Instruction *R =
2130
- foldICmpDivConstConst (ICI, cast<BinaryOperator>( LHSI) , DivRHS))
2135
+ foldICmpDivConstConst (ICI, LHSI, DivRHS))
2131
2136
return R;
2132
2137
2133
2138
return nullptr ;
2134
2139
}
2135
2140
2136
2141
// / Fold icmp (sub X, Y), C.
2137
- Instruction *InstCombiner::foldICmpSubConstant (ICmpInst &Cmp, Instruction *Sub,
2142
+ Instruction *InstCombiner::foldICmpSubConstant (ICmpInst &Cmp,
2143
+ BinaryOperator *Sub,
2138
2144
const APInt *C) {
2139
2145
const APInt *C2;
2140
2146
if (!match (Sub->getOperand (0 ), m_APInt (C2)) || !Sub->hasOneUse ())
@@ -2162,7 +2168,8 @@ Instruction *InstCombiner::foldICmpSubConstant(ICmpInst &Cmp, Instruction *Sub,
2162
2168
}
2163
2169
2164
2170
// / Fold icmp (add X, Y), C.
2165
- Instruction *InstCombiner::foldICmpAddConstant (ICmpInst &Cmp, Instruction *Add,
2171
+ Instruction *InstCombiner::foldICmpAddConstant (ICmpInst &Cmp,
2172
+ BinaryOperator *Add,
2166
2173
const APInt *C) {
2167
2174
Value *Y = Add->getOperand (1 );
2168
2175
const APInt *C2;
@@ -2210,61 +2217,66 @@ Instruction *InstCombiner::foldICmpAddConstant(ICmpInst &Cmp, Instruction *Add,
2210
2217
}
2211
2218
2212
2219
// / Try to fold integer comparisons with a constant operand: icmp Pred X, C.
2213
- Instruction *InstCombiner::foldICmpWithConstant (ICmpInst &ICI) {
2214
- Instruction *LHSI;
2215
- const APInt *RHSV;
2216
- if (!match (ICI.getOperand (0 ), m_Instruction (LHSI)) ||
2217
- !match (ICI.getOperand (1 ), m_APInt (RHSV)))
2220
+ Instruction *InstCombiner::foldICmpWithConstant (ICmpInst &Cmp) {
2221
+ const APInt *C;
2222
+ if (!match (Cmp.getOperand (1 ), m_APInt (C)))
2218
2223
return nullptr ;
2219
2224
2220
- switch (LHSI->getOpcode ()) {
2221
- case Instruction::Trunc:
2222
- if (Instruction *I = foldICmpTruncConstant (ICI, LHSI, RHSV))
2223
- return I;
2224
- break ;
2225
- case Instruction::Xor:
2226
- if (Instruction *I = foldICmpXorConstant (ICI, LHSI, RHSV))
2227
- return I;
2228
- break ;
2229
- case Instruction::And:
2230
- if (Instruction *I = foldICmpAndConstant (ICI, LHSI, RHSV))
2231
- return I;
2232
- break ;
2233
- case Instruction::Or:
2234
- if (Instruction *I = foldICmpOrConstant (ICI, LHSI, RHSV))
2235
- return I;
2236
- break ;
2237
- case Instruction::Mul:
2238
- if (Instruction *I = foldICmpMulConstant (ICI, LHSI, RHSV))
2239
- return I;
2240
- break ;
2241
- case Instruction::Shl:
2242
- if (Instruction *I = foldICmpShlConstant (ICI, LHSI, RHSV))
2243
- return I;
2244
- break ;
2245
- case Instruction::LShr:
2246
- case Instruction::AShr:
2247
- if (Instruction *I = foldICmpShrConstant (ICI, LHSI, RHSV))
2248
- return I;
2249
- break ;
2250
- case Instruction::UDiv:
2251
- if (Instruction *I = foldICmpUDivConstant (ICI, LHSI, RHSV))
2252
- return I;
2253
- LLVM_FALLTHROUGH;
2254
- case Instruction::SDiv:
2255
- if (Instruction *I = foldICmpDivConstant (ICI, LHSI, RHSV))
2256
- return I;
2257
- break ;
2258
- case Instruction::Sub:
2259
- if (Instruction *I = foldICmpSubConstant (ICI, LHSI, RHSV))
2260
- return I;
2261
- break ;
2262
- case Instruction::Add:
2263
- if (Instruction *I = foldICmpAddConstant (ICI, LHSI, RHSV))
2264
- return I;
2265
- break ;
2225
+ BinaryOperator *BO;
2226
+ if (match (Cmp.getOperand (0 ), m_BinOp (BO))) {
2227
+ switch (BO->getOpcode ()) {
2228
+ case Instruction::Xor:
2229
+ if (Instruction *I = foldICmpXorConstant (Cmp, BO, C))
2230
+ return I;
2231
+ break ;
2232
+ case Instruction::And:
2233
+ if (Instruction *I = foldICmpAndConstant (Cmp, BO, C))
2234
+ return I;
2235
+ break ;
2236
+ case Instruction::Or:
2237
+ if (Instruction *I = foldICmpOrConstant (Cmp, BO, C))
2238
+ return I;
2239
+ break ;
2240
+ case Instruction::Mul:
2241
+ if (Instruction *I = foldICmpMulConstant (Cmp, BO, C))
2242
+ return I;
2243
+ break ;
2244
+ case Instruction::Shl:
2245
+ if (Instruction *I = foldICmpShlConstant (Cmp, BO, C))
2246
+ return I;
2247
+ break ;
2248
+ case Instruction::LShr:
2249
+ case Instruction::AShr:
2250
+ if (Instruction *I = foldICmpShrConstant (Cmp, BO, C))
2251
+ return I;
2252
+ break ;
2253
+ case Instruction::UDiv:
2254
+ if (Instruction *I = foldICmpUDivConstant (Cmp, BO, C))
2255
+ return I;
2256
+ LLVM_FALLTHROUGH;
2257
+ case Instruction::SDiv:
2258
+ if (Instruction *I = foldICmpDivConstant (Cmp, BO, C))
2259
+ return I;
2260
+ break ;
2261
+ case Instruction::Sub:
2262
+ if (Instruction *I = foldICmpSubConstant (Cmp, BO, C))
2263
+ return I;
2264
+ break ;
2265
+ case Instruction::Add:
2266
+ if (Instruction *I = foldICmpAddConstant (Cmp, BO, C))
2267
+ return I;
2268
+ break ;
2269
+ default :
2270
+ break ;
2271
+ }
2266
2272
}
2267
2273
2274
+ Instruction *LHSI;
2275
+ if (match (Cmp.getOperand (0 ), m_Instruction (LHSI)) &&
2276
+ LHSI->getOpcode () == Instruction::Trunc)
2277
+ if (Instruction *I = foldICmpTruncConstant (Cmp, LHSI, C))
2278
+ return I;
2279
+
2268
2280
return nullptr ;
2269
2281
}
2270
2282
0 commit comments