Skip to content
This repository was archived by the owner on Mar 28, 2020. It is now read-only.

Commit 558d668

Browse files
committed
[InstCombine] clean up foldICmpDivConstant; NFCI
1. Fix comments to match variable names 2. Remove redundant CmpRHS variable 3. Add FIXME to replace some checks with asserts git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@280112 91177308-0d34-0410-b5e6-96231b3b80d8
1 parent f81c893 commit 558d668

File tree

1 file changed

+20
-16
lines changed

1 file changed

+20
-16
lines changed

lib/Transforms/InstCombine/InstCombineCompares.cpp

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1971,55 +1971,59 @@ Instruction *InstCombiner::foldICmpUDivConstant(ICmpInst &Cmp,
19711971
Instruction *InstCombiner::foldICmpDivConstant(ICmpInst &Cmp,
19721972
BinaryOperator *Div,
19731973
const APInt *C) {
1974-
// FIXME: This check restricts all folds under here to scalar types.
1974+
// FIXME: These checks restrict all folds under here to scalar types.
19751975
ConstantInt *RHS = dyn_cast<ConstantInt>(Cmp.getOperand(1));
19761976
if (!RHS)
19771977
return nullptr;
19781978

1979-
// Fold: icmp pred ([us]div X, C1), C2 -> range test
1979+
ConstantInt *DivRHS = dyn_cast<ConstantInt>(Div->getOperand(1));
1980+
if (!DivRHS)
1981+
return nullptr;
1982+
1983+
// Fold: icmp pred ([us]div X, C2), C -> range test
19801984
// Fold this div into the comparison, producing a range check.
19811985
// Determine, based on the divide type, what the range is being
19821986
// checked. If there is an overflow on the low or high side, remember
19831987
// it, otherwise compute the range [low, hi) bounding the new value.
19841988
// See: InsertRangeTest above for the kinds of replacements possible.
1985-
ConstantInt *DivRHS = dyn_cast<ConstantInt>(Div->getOperand(1));
1986-
if (!DivRHS)
1989+
const APInt *C2;
1990+
if (!match(Div->getOperand(1), m_APInt(C2)))
19871991
return nullptr;
19881992

1989-
ConstantInt *CmpRHS = cast<ConstantInt>(Cmp.getOperand(1));
1990-
19911993
// FIXME: If the operand types don't match the type of the divide
19921994
// then don't attempt this transform. The code below doesn't have the
19931995
// logic to deal with a signed divide and an unsigned compare (and
1994-
// vice versa). This is because (x /s C1) <s C2 produces different
1995-
// results than (x /s C1) <u C2 or (x /u C1) <s C2 or even
1996-
// (x /u C1) <u C2. Simply casting the operands and result won't
1996+
// vice versa). This is because (x /s C2) <s C produces different
1997+
// results than (x /s C2) <u C or (x /u C2) <s C or even
1998+
// (x /u C2) <u C. Simply casting the operands and result won't
19971999
// work. :( The if statement below tests that condition and bails
19982000
// if it finds it.
19992001
bool DivIsSigned = Div->getOpcode() == Instruction::SDiv;
20002002
if (!Cmp.isEquality() && DivIsSigned != Cmp.isSigned())
20012003
return nullptr;
2002-
if (DivRHS->isZero())
2004+
2005+
// FIXME: These 3 checks can be asserts.
2006+
if (*C2 == 0)
20032007
return nullptr; // The ProdOV computation fails on divide by zero.
2004-
if (DivIsSigned && DivRHS->isAllOnesValue())
2008+
if (DivIsSigned && C2->isAllOnesValue())
20052009
return nullptr; // The overflow computation also screws up here
2006-
if (DivRHS->isOne()) {
2010+
if (*C2 == 1) {
20072011
// This eliminates some funny cases with INT_MIN.
20082012
Cmp.setOperand(0, Div->getOperand(0)); // X/1 == X.
20092013
return &Cmp;
20102014
}
20112015

20122016
// Compute Prod = CI * DivRHS. We are essentially solving an equation
2013-
// of form X/C1=C2. We solve for X by multiplying C1 (DivRHS) and
2014-
// C2 (CI). By solving for X we can turn this into a range check
2017+
// of form X/C2=C. We solve for X by multiplying C2 (DivRHS) and
2018+
// C (CI). By solving for X we can turn this into a range check
20152019
// instead of computing a divide.
2016-
Constant *Prod = ConstantExpr::getMul(CmpRHS, DivRHS);
2020+
Constant *Prod = ConstantExpr::getMul(RHS, DivRHS);
20172021

20182022
// Determine if the product overflows by seeing if the product is
20192023
// not equal to the divide. Make sure we do the same kind of divide
20202024
// as in the LHS instruction that we're folding.
20212025
bool ProdOV = (DivIsSigned ? ConstantExpr::getSDiv(Prod, DivRHS) :
2022-
ConstantExpr::getUDiv(Prod, DivRHS)) != CmpRHS;
2026+
ConstantExpr::getUDiv(Prod, DivRHS)) != RHS;
20232027

20242028
// Get the ICmp opcode
20252029
ICmpInst::Predicate Pred = Cmp.getPredicate();

0 commit comments

Comments
 (0)