[llvm-commits] [llvm] r60278 - /llvm/trunk/lib/Transforms/Scalar/InstructionCombining.cpp

Bill Wendling isanbard at gmail.com
Sat Nov 29 21:01:05 PST 2008


Author: void
Date: Sat Nov 29 23:01:05 2008
New Revision: 60278

URL: http://llvm.org/viewvc/llvm-project?rev=60278&view=rev
Log:
>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.

Modified:
    llvm/trunk/lib/Transforms/Scalar/InstructionCombining.cpp

Modified: llvm/trunk/lib/Transforms/Scalar/InstructionCombining.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/Scalar/InstructionCombining.cpp?rev=60278&r1=60277&r2=60278&view=diff

==============================================================================
--- llvm/trunk/lib/Transforms/Scalar/InstructionCombining.cpp (original)
+++ llvm/trunk/lib/Transforms/Scalar/InstructionCombining.cpp Sat Nov 29 23:01:05 2008
@@ -2957,20 +2957,22 @@
       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));
         }





More information about the llvm-commits mailing list