[llvm] r319964 - [InstCombine] canonicalize constant-minus-boolean to select-of-constants

Sanjay Patel via llvm-commits llvm-commits at lists.llvm.org
Wed Dec 6 13:22:57 PST 2017


Author: spatel
Date: Wed Dec  6 13:22:57 2017
New Revision: 319964

URL: http://llvm.org/viewvc/llvm-project?rev=319964&view=rev
Log:
[InstCombine] canonicalize constant-minus-boolean to select-of-constants

This restores the half of:
https://reviews.llvm.org/rL75531
that was reverted at:
https://reviews.llvm.org/rL159230

For the x86 case mentioned there, we now produce:
leal 1(%rdi), %eax
subl %esi, %eax

We have target hooks to invert this in DAGCombiner (and x86 is enabled) with:
https://reviews.llvm.org/rL296977
https://reviews.llvm.org/rL311731

AArch64 and possibly other targets would probably benefit from enabling those hooks too. 
See PR30327:
https://bugs.llvm.org/show_bug.cgi?id=30327#c2

Differential Revision: https://reviews.llvm.org/D40612

Modified:
    llvm/trunk/lib/Transforms/InstCombine/InstCombineAddSub.cpp
    llvm/trunk/test/Transforms/InstCombine/zext-bool-add-sub.ll

Modified: llvm/trunk/lib/Transforms/InstCombine/InstCombineAddSub.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/InstCombine/InstCombineAddSub.cpp?rev=319964&r1=319963&r2=319964&view=diff
==============================================================================
--- llvm/trunk/lib/Transforms/InstCombine/InstCombineAddSub.cpp (original)
+++ llvm/trunk/lib/Transforms/InstCombine/InstCombineAddSub.cpp Wed Dec  6 13:22:57 2017
@@ -1520,8 +1520,13 @@ Instruction *InstCombiner::visitSub(Bina
     return BinaryOperator::CreateNot(Op1);
 
   if (Constant *C = dyn_cast<Constant>(Op0)) {
+    Value *X;
+    // C - zext(bool) -> bool ? C - 1 : C
+    if (match(Op1, m_ZExt(m_Value(X))) &&
+        X->getType()->getScalarSizeInBits() == 1)
+      return SelectInst::Create(X, SubOne(C), C);
+
     // C - ~X == X + (1+C)
-    Value *X = nullptr;
     if (match(Op1, m_Not(m_Value(X))))
       return BinaryOperator::CreateAdd(X, AddOne(C));
 

Modified: llvm/trunk/test/Transforms/InstCombine/zext-bool-add-sub.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/InstCombine/zext-bool-add-sub.ll?rev=319964&r1=319963&r2=319964&view=diff
==============================================================================
--- llvm/trunk/test/Transforms/InstCombine/zext-bool-add-sub.ll (original)
+++ llvm/trunk/test/Transforms/InstCombine/zext-bool-add-sub.ll Wed Dec  6 13:22:57 2017
@@ -20,8 +20,7 @@ define i32 @a(i1 zeroext %x, i1 zeroext
 
 define i32 @zextsub(i1 %x) {
 ; CHECK-LABEL: @zextsub(
-; CHECK-NEXT:    [[ZEXT:%.*]] = zext i1 %x to i32
-; CHECK-NEXT:    [[SUB:%.*]] = sub nsw i32 11, [[ZEXT]]
+; CHECK-NEXT:    [[SUB:%.*]] = select i1 %x, i32 10, i32 11
 ; CHECK-NEXT:    ret i32 [[SUB]]
 ;
   %zext = zext i1 %x to i32
@@ -31,8 +30,7 @@ define i32 @zextsub(i1 %x) {
 
 define <2 x i32> @zextsub_splat(<2 x i1> %x) {
 ; CHECK-LABEL: @zextsub_splat(
-; CHECK-NEXT:    [[ZEXT:%.*]] = zext <2 x i1> %x to <2 x i32>
-; CHECK-NEXT:    [[SUB:%.*]] = sub nsw <2 x i32> <i32 42, i32 42>, [[ZEXT]]
+; CHECK-NEXT:    [[SUB:%.*]] = select <2 x i1> %x, <2 x i32> <i32 41, i32 41>, <2 x i32> <i32 42, i32 42>
 ; CHECK-NEXT:    ret <2 x i32> [[SUB]]
 ;
   %zext = zext <2 x i1> %x to <2 x i32>
@@ -42,8 +40,7 @@ define <2 x i32> @zextsub_splat(<2 x i1>
 
 define <2 x i32> @zextsub_vec(<2 x i1> %x) {
 ; CHECK-LABEL: @zextsub_vec(
-; CHECK-NEXT:    [[ZEXT:%.*]] = zext <2 x i1> %x to <2 x i32>
-; CHECK-NEXT:    [[SUB:%.*]] = sub nsw <2 x i32> <i32 11, i32 42>, [[ZEXT]]
+; CHECK-NEXT:    [[SUB:%.*]] = select <2 x i1> %x, <2 x i32> <i32 10, i32 41>, <2 x i32> <i32 11, i32 42>
 ; CHECK-NEXT:    ret <2 x i32> [[SUB]]
 ;
   %zext = zext <2 x i1> %x to <2 x i32>




More information about the llvm-commits mailing list