[llvm-commits] [llvm] r56076 - in /llvm/trunk: lib/Transforms/Scalar/InstructionCombining.cpp test/Transforms/InstCombine/div-cmp-overflow.ll

Dan Gohman gohman at apple.com
Wed Sep 10 16:30:57 PDT 2008


Author: djg
Date: Wed Sep 10 18:30:57 2008
New Revision: 56076

URL: http://llvm.org/viewvc/llvm-project?rev=56076&view=rev
Log:
Fix an icmp+sdiv optimization to check for and handle an overflow
condition. This fixes PR2740.

Added:
    llvm/trunk/test/Transforms/InstCombine/div-cmp-overflow.ll
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=56076&r1=56075&r2=56076&view=diff

==============================================================================
--- llvm/trunk/lib/Transforms/Scalar/InstructionCombining.cpp (original)
+++ llvm/trunk/lib/Transforms/Scalar/InstructionCombining.cpp Wed Sep 10 18:30:57 2008
@@ -4695,6 +4695,21 @@
     return Result->getValue().ult(In1->getValue());
 }
 
+/// SubWithOverflow - Compute Result = In1-In2, returning true if the result
+/// overflowed for this type.
+static bool SubWithOverflow(ConstantInt *&Result, ConstantInt *In1,
+                            ConstantInt *In2, bool IsSigned = false) {
+  Result = cast<ConstantInt>(Add(In1, In2));
+
+  if (IsSigned)
+    if (In2->getValue().isNegative())
+      return Result->getValue().slt(In1->getValue());
+    else
+      return Result->getValue().sgt(In1->getValue());
+  else
+    return Result->getValue().ugt(In1->getValue());
+}
+
 /// EmitGEPOffset - Given a getelementptr instruction/constantexpr, emit the
 /// code necessary to compute the offset from the base pointer (without adding
 /// in the base pointer).  Return the result as a signed integer of intptr size.
@@ -5774,7 +5789,7 @@
       // e.g. X/-5 op -3  --> [15, 20)
       LoBound = Prod;
       LoOverflow = HiOverflow = ProdOV ? 1 : 0;
-      HiBound = Subtract(Prod, DivRHS);
+      HiOverflow = SubWithOverflow(HiBound, Prod, DivRHS, true);
     }
     
     // Dividing by a negative swaps the condition.  LT <-> GT

Added: llvm/trunk/test/Transforms/InstCombine/div-cmp-overflow.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/InstCombine/div-cmp-overflow.ll?rev=56076&view=auto

==============================================================================
--- llvm/trunk/test/Transforms/InstCombine/div-cmp-overflow.ll (added)
+++ llvm/trunk/test/Transforms/InstCombine/div-cmp-overflow.ll Wed Sep 10 18:30:57 2008
@@ -0,0 +1,8 @@
+; RUN: llvm-as < %s | opt -instcombine | llvm-dis | not grep sdiv
+; PR2740
+
+define i1 @func_75(i32 %i2) nounwind {
+	%i3 = sdiv i32 %i2, -1328634635
+	%i4 = icmp eq i32 %i3, -1
+	ret i1 %i4
+}





More information about the llvm-commits mailing list