@@ -1971,55 +1971,59 @@ Instruction *InstCombiner::foldICmpUDivConstant(ICmpInst &Cmp,
1971
1971
Instruction *InstCombiner::foldICmpDivConstant (ICmpInst &Cmp,
1972
1972
BinaryOperator *Div,
1973
1973
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.
1975
1975
ConstantInt *RHS = dyn_cast<ConstantInt>(Cmp.getOperand (1 ));
1976
1976
if (!RHS)
1977
1977
return nullptr ;
1978
1978
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
1980
1984
// Fold this div into the comparison, producing a range check.
1981
1985
// Determine, based on the divide type, what the range is being
1982
1986
// checked. If there is an overflow on the low or high side, remember
1983
1987
// it, otherwise compute the range [low, hi) bounding the new value.
1984
1988
// 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)) )
1987
1991
return nullptr ;
1988
1992
1989
- ConstantInt *CmpRHS = cast<ConstantInt>(Cmp.getOperand (1 ));
1990
-
1991
1993
// FIXME: If the operand types don't match the type of the divide
1992
1994
// then don't attempt this transform. The code below doesn't have the
1993
1995
// 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
1997
1999
// work. :( The if statement below tests that condition and bails
1998
2000
// if it finds it.
1999
2001
bool DivIsSigned = Div->getOpcode () == Instruction::SDiv;
2000
2002
if (!Cmp.isEquality () && DivIsSigned != Cmp.isSigned ())
2001
2003
return nullptr ;
2002
- if (DivRHS->isZero ())
2004
+
2005
+ // FIXME: These 3 checks can be asserts.
2006
+ if (*C2 == 0 )
2003
2007
return nullptr ; // The ProdOV computation fails on divide by zero.
2004
- if (DivIsSigned && DivRHS ->isAllOnesValue ())
2008
+ if (DivIsSigned && C2 ->isAllOnesValue ())
2005
2009
return nullptr ; // The overflow computation also screws up here
2006
- if (DivRHS-> isOne () ) {
2010
+ if (*C2 == 1 ) {
2007
2011
// This eliminates some funny cases with INT_MIN.
2008
2012
Cmp.setOperand (0 , Div->getOperand (0 )); // X/1 == X.
2009
2013
return &Cmp;
2010
2014
}
2011
2015
2012
2016
// 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
2015
2019
// instead of computing a divide.
2016
- Constant *Prod = ConstantExpr::getMul (CmpRHS , DivRHS);
2020
+ Constant *Prod = ConstantExpr::getMul (RHS , DivRHS);
2017
2021
2018
2022
// Determine if the product overflows by seeing if the product is
2019
2023
// not equal to the divide. Make sure we do the same kind of divide
2020
2024
// as in the LHS instruction that we're folding.
2021
2025
bool ProdOV = (DivIsSigned ? ConstantExpr::getSDiv (Prod, DivRHS) :
2022
- ConstantExpr::getUDiv (Prod, DivRHS)) != CmpRHS ;
2026
+ ConstantExpr::getUDiv (Prod, DivRHS)) != RHS ;
2023
2027
2024
2028
// Get the ICmp opcode
2025
2029
ICmpInst::Predicate Pred = Cmp.getPredicate ();
0 commit comments