[llvm] r204948 - InstCombine: Don't combine constants on unsigned icmps

Reid Kleckner reid at kleckner.net
Thu Mar 27 10:49:27 PDT 2014


Author: rnk
Date: Thu Mar 27 12:49:27 2014
New Revision: 204948

URL: http://llvm.org/viewvc/llvm-project?rev=204948&view=rev
Log:
InstCombine: Don't combine constants on unsigned icmps

Fixes a miscompile introduced in r204912.  It would miscompile code like
(unsigned)(a + -49) <= 5U.  The transform would turn this into
(unsigned)a < 55U, which would return true for values in [0, 49], when
it should not.

Modified:
    llvm/trunk/lib/Transforms/InstCombine/InstCombineCompares.cpp
    llvm/trunk/test/Transforms/InstCombine/icmp.ll

Modified: llvm/trunk/lib/Transforms/InstCombine/InstCombineCompares.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/InstCombine/InstCombineCompares.cpp?rev=204948&r1=204947&r2=204948&view=diff
==============================================================================
--- llvm/trunk/lib/Transforms/InstCombine/InstCombineCompares.cpp (original)
+++ llvm/trunk/lib/Transforms/InstCombine/InstCombineCompares.cpp Thu Mar 27 12:49:27 2014
@@ -3010,7 +3010,8 @@ Instruction *InstCombiner::visitICmpInst
       return FoldICmpAddOpCst(I, X, Cst, I.getSwappedPredicate());
 
     ConstantInt *Cst2;
-    if (match(Op1, m_ConstantInt(Cst)) &&
+    if (I.isSigned() &&
+        match(Op1, m_ConstantInt(Cst)) &&
         match(Op0, m_Add(m_Value(X), m_ConstantInt(Cst2))) &&
         cast<BinaryOperator>(Op0)->hasNoSignedWrap()) {
       // icmp X+Cst2, Cst --> icmp X, Cst-Cst2

Modified: llvm/trunk/test/Transforms/InstCombine/icmp.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/InstCombine/icmp.ll?rev=204948&r1=204947&r2=204948&view=diff
==============================================================================
--- llvm/trunk/test/Transforms/InstCombine/icmp.ll (original)
+++ llvm/trunk/test/Transforms/InstCombine/icmp.ll Thu Mar 27 12:49:27 2014
@@ -1409,3 +1409,13 @@ entry:
   %conv = zext i1 %cmp to i32
   ret i32 %conv
 }
+
+; CHECK-LABEL: icmp_add_const_ult
+; CHECK: %cmp = icmp ult i32 %add, 6
+define i32 @icmp_add_const_ult(i32 %a) #0 {
+entry:
+  %add = add nsw i32 %a, -49
+  %cmp = icmp ult i32 %add, 6
+  %conv = zext i1 %cmp to i32
+  ret i32 %conv
+}





More information about the llvm-commits mailing list