]> granicus.if.org Git - llvm/commitdiff
[CGP] match a special-case of unsigned subtract overflow
authorSanjay Patel <spatel@rotateright.com>
Wed, 20 Feb 2019 21:23:04 +0000 (21:23 +0000)
committerSanjay Patel <spatel@rotateright.com>
Wed, 20 Feb 2019 21:23:04 +0000 (21:23 +0000)
This is the 'sub0' (negate) pattern from PR31754:
https://bugs.llvm.org/show_bug.cgi?id=31754

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@354519 91177308-0d34-0410-b5e6-96231b3b80d8

lib/CodeGen/CodeGenPrepare.cpp
test/CodeGen/X86/cgp-usubo.ll
test/Transforms/CodeGenPrepare/X86/overflow-intrinsics.ll

index 20a2ae1233cc395a532ab7b402e978df7381456c..e9611392058e191d8842cc6777bf8bfe9e211715 100644 (file)
@@ -1228,6 +1228,11 @@ static bool combineToUSubWithOverflow(CmpInst *Cmp, const TargetLowering &TLI,
     B = ConstantInt::get(B->getType(), 1);
     Pred = ICmpInst::ICMP_ULT;
   }
+  // Convert special-case: (A != 0) is the same as (0 u< A).
+  if (Pred == ICmpInst::ICMP_NE && match(B, m_ZeroInt())) {
+    std::swap(A, B);
+    Pred = ICmpInst::ICMP_ULT;
+  }
   if (Pred != ICmpInst::ICMP_ULT)
     return false;
 
index ba8042823640cd67311ee96c66dc1353d19c50fb..aa0e15b3784a11c34dc023d29c3138c2b5f15a4c 100644 (file)
@@ -111,11 +111,9 @@ define i1 @usubo_eq_constant1_op1_i32(i32 %x, i32* %p) nounwind {
 define i1 @usubo_ne_constant0_op1_i32(i32 %x, i32* %p) {
 ; CHECK-LABEL: usubo_ne_constant0_op1_i32:
 ; CHECK:       # %bb.0:
-; CHECK-NEXT:    movl %edi, %ecx
-; CHECK-NEXT:    negl %ecx
-; CHECK-NEXT:    testl %edi, %edi
-; CHECK-NEXT:    setne %al
-; CHECK-NEXT:    movl %ecx, (%rsi)
+; CHECK-NEXT:    negl %edi
+; CHECK-NEXT:    setb %al
+; CHECK-NEXT:    movl %edi, (%rsi)
 ; CHECK-NEXT:    retq
   %s = sub i32 0, %x
   %ov = icmp ne i32 %x, 0
index 5107e833ecf3e3f1299226c9d6d69d1a7aebc3d9..4b0bafacaf4076bf144d5314fb49942d4219bf56 100644 (file)
@@ -285,10 +285,11 @@ define i1 @usubo_eq_constant1_op1_i32(i32 %x, i32* %p) {
 
 define i1 @usubo_ne_constant0_op1_i32(i32 %x, i32* %p) {
 ; CHECK-LABEL: @usubo_ne_constant0_op1_i32(
-; CHECK-NEXT:    [[S:%.*]] = sub i32 0, [[X:%.*]]
-; CHECK-NEXT:    [[OV:%.*]] = icmp ne i32 [[X]], 0
-; CHECK-NEXT:    store i32 [[S]], i32* [[P:%.*]]
-; CHECK-NEXT:    ret i1 [[OV]]
+; CHECK-NEXT:    [[TMP1:%.*]] = call { i32, i1 } @llvm.usub.with.overflow.i32(i32 0, i32 [[X:%.*]])
+; CHECK-NEXT:    [[MATH:%.*]] = extractvalue { i32, i1 } [[TMP1]], 0
+; CHECK-NEXT:    [[OV1:%.*]] = extractvalue { i32, i1 } [[TMP1]], 1
+; CHECK-NEXT:    store i32 [[MATH]], i32* [[P:%.*]]
+; CHECK-NEXT:    ret i1 [[OV1]]
 ;
   %s = sub i32 0, %x
   %ov = icmp ne i32 %x, 0