Skip to content

Commit 0fd3c95

Browse files
committed
[InstCombine] change param type from Instruction to BinaryOperator for icmp helpers; NFCI
This saves some casting in the helper functions and eases some further refactoring. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@279478 91177308-0d34-0410-b5e6-96231b3b80d8
1 parent 22fca38 commit 0fd3c95

File tree

2 files changed

+109
-97
lines changed

2 files changed

+109
-97
lines changed

lib/Transforms/InstCombine/InstCombineCompares.cpp

Lines changed: 85 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -1569,7 +1569,8 @@ Instruction *InstCombiner::foldICmpTruncConstant(ICmpInst &Cmp,
15691569
}
15701570

15711571
/// Fold icmp (xor X, Y), C.
1572-
Instruction *InstCombiner::foldICmpXorConstant(ICmpInst &Cmp, Instruction *Xor,
1572+
Instruction *InstCombiner::foldICmpXorConstant(ICmpInst &Cmp,
1573+
BinaryOperator *Xor,
15731574
const APInt *C) {
15741575
Value *X = Xor->getOperand(0);
15751576
Value *Y = Xor->getOperand(1);
@@ -1634,7 +1635,8 @@ Instruction *InstCombiner::foldICmpXorConstant(ICmpInst &Cmp, Instruction *Xor,
16341635
return nullptr;
16351636
}
16361637

1637-
Instruction *InstCombiner::foldICmpAndConstant(ICmpInst &ICI, Instruction *LHSI,
1638+
Instruction *InstCombiner::foldICmpAndConstant(ICmpInst &ICI,
1639+
BinaryOperator *LHSI,
16381640
const APInt *RHSV) {
16391641
// FIXME: This check restricts all folds under here to scalar types.
16401642
ConstantInt *RHS = dyn_cast<ConstantInt>(ICI.getOperand(1));
@@ -1875,7 +1877,7 @@ Instruction *InstCombiner::foldICmpAndConstant(ICmpInst &ICI, Instruction *LHSI,
18751877
}
18761878

18771879
/// Fold icmp (or X, Y), C.
1878-
Instruction *InstCombiner::foldICmpOrConstant(ICmpInst &Cmp, Instruction *Or,
1880+
Instruction *InstCombiner::foldICmpOrConstant(ICmpInst &Cmp, BinaryOperator *Or,
18791881
const APInt *C) {
18801882
ICmpInst::Predicate Pred = Cmp.getPredicate();
18811883
if (*C == 1) {
@@ -1906,7 +1908,8 @@ Instruction *InstCombiner::foldICmpOrConstant(ICmpInst &Cmp, Instruction *Or,
19061908
}
19071909

19081910
/// Fold icmp (mul X, Y), C.
1909-
Instruction *InstCombiner::foldICmpMulConstant(ICmpInst &Cmp, Instruction *Mul,
1911+
Instruction *InstCombiner::foldICmpMulConstant(ICmpInst &Cmp,
1912+
BinaryOperator *Mul,
19101913
const APInt *C) {
19111914
const APInt *MulC;
19121915
if (!match(Mul->getOperand(1), m_APInt(MulC)))
@@ -1915,7 +1918,7 @@ Instruction *InstCombiner::foldICmpMulConstant(ICmpInst &Cmp, Instruction *Mul,
19151918
// If this is a test of the sign bit and the multiply is sign-preserving with
19161919
// a constant operand, use the multiply LHS operand instead.
19171920
ICmpInst::Predicate Pred = Cmp.getPredicate();
1918-
if (isSignTest(Pred, *C) && cast<BinaryOperator>(Mul)->hasNoSignedWrap()) {
1921+
if (isSignTest(Pred, *C) && Mul->hasNoSignedWrap()) {
19191922
if (MulC->isNegative())
19201923
Pred = ICmpInst::getSwappedPredicate(Pred);
19211924
return new ICmpInst(Pred, Mul->getOperand(0),
@@ -1988,7 +1991,8 @@ static Instruction *foldICmpShlOne(ICmpInst &Cmp, Instruction *Shl,
19881991
}
19891992

19901993
/// Fold icmp (shl X, Y), C.
1991-
Instruction *InstCombiner::foldICmpShlConstant(ICmpInst &Cmp, Instruction *Shl,
1994+
Instruction *InstCombiner::foldICmpShlConstant(ICmpInst &Cmp,
1995+
BinaryOperator *Shl,
19921996
const APInt *C) {
19931997
const APInt *ShiftAmt;
19941998
if (!match(Shl->getOperand(1), m_APInt(ShiftAmt)))
@@ -2006,12 +2010,12 @@ Instruction *InstCombiner::foldICmpShlConstant(ICmpInst &Cmp, Instruction *Shl,
20062010
// If the shift is NUW, then it is just shifting out zeros, no need for an
20072011
// AND.
20082012
Constant *LShrC = ConstantInt::get(Shl->getType(), C->lshr(*ShiftAmt));
2009-
if (cast<BinaryOperator>(Shl)->hasNoUnsignedWrap())
2013+
if (Shl->hasNoUnsignedWrap())
20102014
return new ICmpInst(Pred, X, LShrC);
20112015

20122016
// If the shift is NSW and we compare to 0, then it is just shifting out
20132017
// sign bits, no need for an AND either.
2014-
if (cast<BinaryOperator>(Shl)->hasNoSignedWrap() && *C == 0)
2018+
if (Shl->hasNoSignedWrap() && *C == 0)
20152019
return new ICmpInst(Pred, X, LShrC);
20162020

20172021
if (Shl->hasOneUse()) {
@@ -2027,7 +2031,7 @@ Instruction *InstCombiner::foldICmpShlConstant(ICmpInst &Cmp, Instruction *Shl,
20272031
// If this is a signed comparison to 0 and the shift is sign preserving,
20282032
// use the shift LHS operand instead; isSignTest may change 'Pred', so only
20292033
// 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))
20312035
return new ICmpInst(Pred, X, Constant::getNullValue(X->getType()));
20322036

20332037
// 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,
20622066
}
20632067

20642068
/// 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) {
20672072
// An exact shr only shifts out zero bits, so:
20682073
// 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));
20732077

20742078
// FIXME: This check restricts all folds under here to scalar types.
20752079
// 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));
20772081
if (!ShAmt)
20782082
return nullptr;
20792083

2080-
if (Instruction *Res = foldICmpShrConstConst(ICI, BO, ShAmt))
2084+
if (Instruction *Res = foldICmpShrConstConst(Cmp, Shr, ShAmt))
20812085
return Res;
20822086

20832087
return nullptr;
20842088
}
20852089

20862090
/// Fold icmp (udiv X, Y), C.
20872091
Instruction *InstCombiner::foldICmpUDivConstant(ICmpInst &Cmp,
2088-
Instruction *UDiv,
2092+
BinaryOperator *UDiv,
20892093
const APInt *C) {
20902094
const APInt *C2;
20912095
if (!match(UDiv->getOperand(0), m_APInt(C2)))
@@ -2112,7 +2116,8 @@ Instruction *InstCombiner::foldICmpUDivConstant(ICmpInst &Cmp,
21122116
return nullptr;
21132117
}
21142118

2115-
Instruction *InstCombiner::foldICmpDivConstant(ICmpInst &ICI, Instruction *LHSI,
2119+
Instruction *InstCombiner::foldICmpDivConstant(ICmpInst &ICI,
2120+
BinaryOperator *LHSI,
21162121
const APInt *RHSV) {
21172122
// FIXME: This check restricts all folds under here to scalar types.
21182123
ConstantInt *RHS = dyn_cast<ConstantInt>(ICI.getOperand(1));
@@ -2127,14 +2132,15 @@ Instruction *InstCombiner::foldICmpDivConstant(ICmpInst &ICI, Instruction *LHSI,
21272132
// See: InsertRangeTest above for the kinds of replacements possible.
21282133
if (ConstantInt *DivRHS = dyn_cast<ConstantInt>(LHSI->getOperand(1)))
21292134
if (Instruction *R =
2130-
foldICmpDivConstConst(ICI, cast<BinaryOperator>(LHSI), DivRHS))
2135+
foldICmpDivConstConst(ICI, LHSI, DivRHS))
21312136
return R;
21322137

21332138
return nullptr;
21342139
}
21352140

21362141
/// Fold icmp (sub X, Y), C.
2137-
Instruction *InstCombiner::foldICmpSubConstant(ICmpInst &Cmp, Instruction *Sub,
2142+
Instruction *InstCombiner::foldICmpSubConstant(ICmpInst &Cmp,
2143+
BinaryOperator *Sub,
21382144
const APInt *C) {
21392145
const APInt *C2;
21402146
if (!match(Sub->getOperand(0), m_APInt(C2)) || !Sub->hasOneUse())
@@ -2162,7 +2168,8 @@ Instruction *InstCombiner::foldICmpSubConstant(ICmpInst &Cmp, Instruction *Sub,
21622168
}
21632169

21642170
/// Fold icmp (add X, Y), C.
2165-
Instruction *InstCombiner::foldICmpAddConstant(ICmpInst &Cmp, Instruction *Add,
2171+
Instruction *InstCombiner::foldICmpAddConstant(ICmpInst &Cmp,
2172+
BinaryOperator *Add,
21662173
const APInt *C) {
21672174
Value *Y = Add->getOperand(1);
21682175
const APInt *C2;
@@ -2210,61 +2217,66 @@ Instruction *InstCombiner::foldICmpAddConstant(ICmpInst &Cmp, Instruction *Add,
22102217
}
22112218

22122219
/// 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)))
22182223
return nullptr;
22192224

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+
}
22662272
}
22672273

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+
22682280
return nullptr;
22692281
}
22702282

lib/Transforms/InstCombine/InstCombineInternal.h

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -559,30 +559,30 @@ class LLVM_LIBRARY_VISIBILITY InstCombiner
559559
Instruction *foldICmpAddOpConst(Instruction &ICI, Value *X, ConstantInt *CI,
560560
ICmpInst::Predicate Pred);
561561
Instruction *foldICmpWithCastAndCast(ICmpInst &ICI);
562-
Instruction *foldICmpWithConstant(ICmpInst &ICI);
563-
564-
Instruction *foldICmpTruncConstant(ICmpInst &ICI, Instruction *LHSI,
565-
const APInt *RHSV);
566-
Instruction *foldICmpAndConstant(ICmpInst &ICI, Instruction *LHSI,
567-
const APInt *RHSV);
568-
Instruction *foldICmpXorConstant(ICmpInst &ICI, Instruction *LHSI,
569-
const APInt *RHSV);
570-
Instruction *foldICmpOrConstant(ICmpInst &ICI, Instruction *LHSI,
571-
const APInt *RHSV);
572-
Instruction *foldICmpMulConstant(ICmpInst &ICI, Instruction *LHSI,
573-
const APInt *RHSV);
574-
Instruction *foldICmpShlConstant(ICmpInst &ICI, Instruction *LHSI,
575-
const APInt *RHSV);
576-
Instruction *foldICmpShrConstant(ICmpInst &ICI, Instruction *LHSI,
577-
const APInt *RHSV);
578-
Instruction *foldICmpUDivConstant(ICmpInst &ICI, Instruction *LHSI,
579-
const APInt *RHSV);
580-
Instruction *foldICmpDivConstant(ICmpInst &ICI, Instruction *LHSI,
581-
const APInt *RHSV);
582-
Instruction *foldICmpSubConstant(ICmpInst &ICI, Instruction *LHSI,
583-
const APInt *RHSV);
584-
Instruction *foldICmpAddConstant(ICmpInst &ICI, Instruction *LHSI,
585-
const APInt *RHSV);
562+
Instruction *foldICmpWithConstant(ICmpInst &Cmp);
563+
564+
Instruction *foldICmpTruncConstant(ICmpInst &Cmp, Instruction *Trunc,
565+
const APInt *C);
566+
Instruction *foldICmpAndConstant(ICmpInst &Cmp, BinaryOperator *And,
567+
const APInt *C);
568+
Instruction *foldICmpXorConstant(ICmpInst &Cmp, BinaryOperator *Xor,
569+
const APInt *C);
570+
Instruction *foldICmpOrConstant(ICmpInst &Cmp, BinaryOperator *Or,
571+
const APInt *C);
572+
Instruction *foldICmpMulConstant(ICmpInst &Cmp, BinaryOperator *Mul,
573+
const APInt *C);
574+
Instruction *foldICmpShlConstant(ICmpInst &Cmp, BinaryOperator *Shl,
575+
const APInt *C);
576+
Instruction *foldICmpShrConstant(ICmpInst &Cmp, BinaryOperator *Shr,
577+
const APInt *C);
578+
Instruction *foldICmpUDivConstant(ICmpInst &Cmp, BinaryOperator *UDiv,
579+
const APInt *C);
580+
Instruction *foldICmpDivConstant(ICmpInst &Cmp, BinaryOperator *Div,
581+
const APInt *C);
582+
Instruction *foldICmpSubConstant(ICmpInst &Cmp, BinaryOperator *Sub,
583+
const APInt *C);
584+
Instruction *foldICmpAddConstant(ICmpInst &Cmp, BinaryOperator *Add,
585+
const APInt *C);
586586

587587
Instruction *foldICmpEqualityWithConstant(ICmpInst &ICI);
588588
Instruction *foldICmpIntrinsicWithConstant(ICmpInst &ICI);

0 commit comments

Comments
 (0)