[llvm-commits] [llvm] r91631 - in /llvm/trunk: lib/Transforms/Scalar/InstructionCombining.cpp test/Transforms/InstCombine/2004-11-27-SetCCForCastLargerAndConstant.ll test/Transforms/InstCombine/2006-10-19-SignedToUnsignedCastAndConst.ll

Eli Friedman eli.friedman at gmail.com
Thu Dec 17 14:42:30 PST 2009


Author: efriedma
Date: Thu Dec 17 16:42:29 2009
New Revision: 91631

URL: http://llvm.org/viewvc/llvm-project?rev=91631&view=rev
Log:
Allow instcombine to combine "sext(a) >u const" to "a >u trunc(const)".


Removed:
    llvm/trunk/test/Transforms/InstCombine/2006-10-19-SignedToUnsignedCastAndConst.ll
Modified:
    llvm/trunk/lib/Transforms/Scalar/InstructionCombining.cpp
    llvm/trunk/test/Transforms/InstCombine/2004-11-27-SetCCForCastLargerAndConstant.ll

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

==============================================================================
--- llvm/trunk/lib/Transforms/Scalar/InstructionCombining.cpp (original)
+++ llvm/trunk/lib/Transforms/Scalar/InstructionCombining.cpp Thu Dec 17 16:42:29 2009
@@ -7299,19 +7299,17 @@
 
   // If the re-extended constant didn't change...
   if (Res2 == CI) {
-    // Make sure that sign of the Cmp and the sign of the Cast are the same.
-    // For example, we might have:
-    //    %A = sext i16 %X to i32
-    //    %B = icmp ugt i32 %A, 1330
-    // It is incorrect to transform this into 
-    //    %B = icmp ugt i16 %X, 1330
-    // because %A may have negative value. 
-    //
-    // However, we allow this when the compare is EQ/NE, because they are
-    // signless.
-    if (isSignedExt == isSignedCmp || ICI.isEquality())
+    // Deal with equality cases early.
+    if (ICI.isEquality())
       return new ICmpInst(ICI.getPredicate(), LHSCIOp, Res1);
-    return 0;
+
+    // A signed comparison of sign extended values simplifies into a
+    // signed comparison.
+    if (isSignedExt && isSignedCmp)
+      return new ICmpInst(ICI.getPredicate(), LHSCIOp, Res1);
+
+    // The other three cases all fold into an unsigned comparison.
+    return new ICmpInst(ICI.getUnsignedPredicate(), LHSCIOp, Res1);
   }
 
   // The re-extended constant changed so the constant cannot be represented 

Modified: llvm/trunk/test/Transforms/InstCombine/2004-11-27-SetCCForCastLargerAndConstant.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/InstCombine/2004-11-27-SetCCForCastLargerAndConstant.ll?rev=91631&r1=91630&r2=91631&view=diff

==============================================================================
--- llvm/trunk/test/Transforms/InstCombine/2004-11-27-SetCCForCastLargerAndConstant.ll (original)
+++ llvm/trunk/test/Transforms/InstCombine/2004-11-27-SetCCForCastLargerAndConstant.ll Thu Dec 17 16:42:29 2009
@@ -33,6 +33,14 @@
 ; CHECK: ret i1 false
 }
 
+define i1 @lt_signed_to_small_unsigned(i8 %SB) {
+        %Y = sext i8 %SB to i32
+        %C = icmp ult i32 %Y, 17
+        ret i1 %C
+; CHECK: %C = icmp ult i8 %SB, 17
+; CHECK: ret i1 %C
+}
+
 define i1 @lt_signed_to_small_signed(i8 %SB) {
         %Y = sext i8 %SB to i32         ; <i32> [#uses=1]
         %C = icmp slt i32 %Y, 17                ; <i1> [#uses=1]
@@ -77,6 +85,14 @@
 ; CHECK: ret i1 %C
 }
 
+define i1 @lt_unsigned_to_small_signed(i8 %SB) {
+        %Y = zext i8 %SB to i32
+        %C = icmp slt i32 %Y, 17
+        ret i1 %C
+; CHECK: %C = icmp ult i8 %SB, 17
+; CHECK: ret i1 %C
+}
+
 define i1 @lt_unsigned_to_small_negative(i8 %SB) {
         %Y = zext i8 %SB to i32         ; <i32> [#uses=1]
         %C = icmp slt i32 %Y, -17               ; <i1> [#uses=1]
@@ -106,6 +122,14 @@
 ; CHECK: ret i1 true
 }
 
+define i1 @gt_signed_to_small_unsigned(i8 %SB) {
+        %Y = sext i8 %SB to i32
+        %C = icmp ugt i32 %Y, 17
+        ret i1 %C
+; CHECK: %C = icmp ugt i8 %SB, 17
+; CHECK: ret i1 %C
+}
+
 define i1 @gt_signed_to_small_signed(i8 %SB) {
         %Y = sext i8 %SB to i32         ; <i32> [#uses=1]
         %C = icmp sgt i32 %Y, 17                ; <i1> [#uses=1]
@@ -151,6 +175,14 @@
 ; CHECK: ret i1 %C
 }
 
+define i1 @gt_unsigned_to_small_signed(i8 %SB) {
+        %Y = zext i8 %SB to i32
+        %C = icmp sgt i32 %Y, 17
+        ret i1 %C
+; CHECK: %C = icmp ugt i8 %SB, 17
+; CHECK: ret i1 %C
+}
+
 define i1 @gt_unsigned_to_small_negative(i8 %SB) {
         %Y = zext i8 %SB to i32         ; <i32> [#uses=1]
         %C = icmp sgt i32 %Y, -17               ; <i1> [#uses=1]

Removed: llvm/trunk/test/Transforms/InstCombine/2006-10-19-SignedToUnsignedCastAndConst.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/InstCombine/2006-10-19-SignedToUnsignedCastAndConst.ll?rev=91630&view=auto

==============================================================================
--- llvm/trunk/test/Transforms/InstCombine/2006-10-19-SignedToUnsignedCastAndConst.ll (original)
+++ llvm/trunk/test/Transforms/InstCombine/2006-10-19-SignedToUnsignedCastAndConst.ll (removed)
@@ -1,12 +0,0 @@
-; This test case is reduced from llvmAsmParser.cpp
-; The optimizer should not remove the cast here.
-; RUN: opt < %s -instcombine -S | \
-; RUN:    grep sext.*i32
-
-
-define i1 @test(i16 %X) {
-        %A = sext i16 %X to i32         ; <i32> [#uses=1]
-        %B = icmp ugt i32 %A, 1330              ; <i1> [#uses=1]
-        ret i1 %B
-}
-





More information about the llvm-commits mailing list