aboutsummaryrefslogtreecommitdiffstats
path: root/lib/Transforms
diff options
context:
space:
mode:
authorBill Wendling <isanbard@gmail.com>2008-11-30 05:01:05 +0000
committerBill Wendling <isanbard@gmail.com>2008-11-30 05:01:05 +0000
commit524d61aab17ce8b6734f09f2dd15b2dfb2e300f7 (patch)
treed0701269fd0842f8d8836012164ab7b7fb8a05f2 /lib/Transforms
parent156a66847d6988b5947b5d62e7e65648e7aa5c6f (diff)
downloadexternal_llvm-524d61aab17ce8b6734f09f2dd15b2dfb2e300f7.zip
external_llvm-524d61aab17ce8b6734f09f2dd15b2dfb2e300f7.tar.gz
external_llvm-524d61aab17ce8b6734f09f2dd15b2dfb2e300f7.tar.bz2
From Hacker's Delight:
"For signed integers, the determination of overflow of x*y is not so simple. If x and y have the same sign, then overflow occurs iff xy > 2**31 - 1. If they have opposite signs, then overflow occurs iff xy < -2**31." In this case, x == -1. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@60278 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib/Transforms')
-rw-r--r--lib/Transforms/Scalar/InstructionCombining.cpp18
1 files changed, 10 insertions, 8 deletions
diff --git a/lib/Transforms/Scalar/InstructionCombining.cpp b/lib/Transforms/Scalar/InstructionCombining.cpp
index 8461aa7..fce2373 100644
--- a/lib/Transforms/Scalar/InstructionCombining.cpp
+++ b/lib/Transforms/Scalar/InstructionCombining.cpp
@@ -2957,20 +2957,22 @@ Instruction *InstCombiner::visitSDiv(BinaryOperator &I) {
return BinaryOperator::CreateNeg(Op0);
ConstantInt *RHSNeg = cast<ConstantInt>(ConstantExpr::getNeg(RHS));
+ APInt RHSNegAPI(RHSNeg->getBitWidth(), RHSNeg->getSExtValue(), true);
+
+ APInt NegOne = -APInt(RHSNeg->getBitWidth(), 1, true);
+ APInt TwoToExp(RHSNeg->getBitWidth(), 1 << (RHSNeg->getBitWidth() - 1),
+ true);
// -X/C -> X/-C, if and only if negation doesn't overflow.
- if ((RHS->getSExtValue() < 0 &&
- RHS->getSExtValue() < RHSNeg->getSExtValue()) ||
- (RHS->getSExtValue() > 0 &&
- RHS->getSExtValue() > RHSNeg->getSExtValue())) {
+ if ((RHS->getSExtValue() < 0 && RHSNegAPI.slt(TwoToExp - 1)) ||
+ (RHS->getSExtValue() > 0 && RHSNegAPI.sgt(TwoToExp * NegOne))) {
if (Value *LHSNeg = dyn_castNegVal(Op0)) {
if (ConstantInt *CI = dyn_cast<ConstantInt>(LHSNeg)) {
ConstantInt *CINeg = cast<ConstantInt>(ConstantExpr::getNeg(CI));
+ APInt CINegAPI(CINeg->getBitWidth(), CINeg->getSExtValue(), true);
- if ((CI->getSExtValue() < 0 &&
- CI->getSExtValue() < CINeg->getSExtValue()) ||
- (CI->getSExtValue() > 0 &&
- CI->getSExtValue() > CINeg->getSExtValue()))
+ if ((CI->getSExtValue() < 0 && CINegAPI.slt(TwoToExp - 1)) ||
+ (CI->getSExtValue() > 0 && CINegAPI.sgt(TwoToExp * NegOne)))
return BinaryOperator::CreateSDiv(LHSNeg,
ConstantExpr::getNeg(RHS));
}