[PATCH] D40612: [InstCombine] canonicalize constant-minus-boolean to select-of-constants

Sanjay Patel via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed Dec 6 13:23:40 PST 2017


This revision was automatically updated to reflect the committed changes.
Closed by commit rL319964: [InstCombine] canonicalize constant-minus-boolean to select-of-constants (authored by spatel).

Changed prior to commit:
  https://reviews.llvm.org/D40612?vs=124770&id=125796#toc

Repository:
  rL LLVM

https://reviews.llvm.org/D40612

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


Index: llvm/trunk/test/Transforms/InstCombine/zext-bool-add-sub.ll
===================================================================
--- llvm/trunk/test/Transforms/InstCombine/zext-bool-add-sub.ll
+++ llvm/trunk/test/Transforms/InstCombine/zext-bool-add-sub.ll
@@ -20,8 +20,7 @@
 
 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 <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_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>
Index: llvm/trunk/lib/Transforms/InstCombine/InstCombineAddSub.cpp
===================================================================
--- llvm/trunk/lib/Transforms/InstCombine/InstCombineAddSub.cpp
+++ llvm/trunk/lib/Transforms/InstCombine/InstCombineAddSub.cpp
@@ -1520,8 +1520,13 @@
     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));
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D40612.125796.patch
Type: text/x-patch
Size: 2167 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20171206/2e5b1a1d/attachment.bin>


More information about the llvm-commits mailing list