[llvm] r283905 - [DAG] add fold for masked negated sign-extended bool

Sanjay Patel via llvm-commits llvm-commits at lists.llvm.org
Tue Oct 11 10:05:53 PDT 2016


Author: spatel
Date: Tue Oct 11 12:05:52 2016
New Revision: 283905

URL: http://llvm.org/viewvc/llvm-project?rev=283905&view=rev
Log:
[DAG] add fold for masked negated sign-extended bool

This enhances the fold added with:
https://reviews.llvm.org/rL283900


Modified:
    llvm/trunk/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
    llvm/trunk/test/CodeGen/X86/mask-negated-bool.ll

Modified: llvm/trunk/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/CodeGen/SelectionDAG/DAGCombiner.cpp?rev=283905&r1=283904&r2=283905&view=diff
==============================================================================
--- llvm/trunk/lib/CodeGen/SelectionDAG/DAGCombiner.cpp (original)
+++ llvm/trunk/lib/CodeGen/SelectionDAG/DAGCombiner.cpp Tue Oct 11 12:05:52 2016
@@ -3314,18 +3314,24 @@ SDValue DAGCombiner::visitAND(SDNode *N)
     if (SDValue Tmp = SimplifyBinOpWithSameOpcodeHands(N))
       return Tmp;
 
-  // Masking the negated extension of a boolean is just the extended boolean:
+  // Masking the negated extension of a boolean is just the zero-extended
+  // boolean:
   // and (sub 0, zext(bool X)), 1 --> zext(bool X)
+  // and (sub 0, sext(bool X)), 1 --> zext(bool X)
   //
   // Note: the SimplifyDemandedBits fold below can make an information-losing
   // transform, and then we have no way to find this better fold.
   if (N1C && N1C->isOne() && N0.getOpcode() == ISD::SUB) {
     ConstantSDNode *SubLHS = isConstOrConstSplat(N0.getOperand(0));
     SDValue SubRHS = N0.getOperand(1);
-    if (SubLHS && SubLHS->isNullValue() &&
-        SubRHS.getOpcode() == ISD::ZERO_EXTEND &&
-        SubRHS.getOperand(0).getScalarValueSizeInBits() == 1)
-      return SubRHS;
+    if (SubLHS && SubLHS->isNullValue()) {
+      if (SubRHS.getOpcode() == ISD::ZERO_EXTEND &&
+          SubRHS.getOperand(0).getScalarValueSizeInBits() == 1)
+        return SubRHS;
+      if (SubRHS.getOpcode() == ISD::SIGN_EXTEND &&
+          SubRHS.getOperand(0).getScalarValueSizeInBits() == 1)
+        return DAG.getNode(ISD::ZERO_EXTEND, SDLoc(N), VT, SubRHS.getOperand(0));
+    }
   }
 
   // fold (and (sign_extend_inreg x, i16 to i32), 1) -> (and x, 1)

Modified: llvm/trunk/test/CodeGen/X86/mask-negated-bool.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/CodeGen/X86/mask-negated-bool.ll?rev=283905&r1=283904&r2=283905&view=diff
==============================================================================
--- llvm/trunk/test/CodeGen/X86/mask-negated-bool.ll (original)
+++ llvm/trunk/test/CodeGen/X86/mask-negated-bool.ll Tue Oct 11 12:05:52 2016
@@ -41,7 +41,6 @@ define <4 x i32> @mask_negated_zext_bool
 define i32 @mask_negated_sext_bool1(i1 %x) {
 ; CHECK-LABEL: mask_negated_sext_bool1:
 ; CHECK:       # BB#0:
-; CHECK-NEXT:    negl %edi
 ; CHECK-NEXT:    andl $1, %edi
 ; CHECK-NEXT:    movl %edi, %eax
 ; CHECK-NEXT:    retq
@@ -56,8 +55,6 @@ define i32 @mask_negated_sext_bool2(i1 z
 ; CHECK-LABEL: mask_negated_sext_bool2:
 ; CHECK:       # BB#0:
 ; CHECK-NEXT:    movzbl %dil, %eax
-; CHECK-NEXT:    negl %eax
-; CHECK-NEXT:    andl $1, %eax
 ; CHECK-NEXT:    retq
 ;
   %ext = sext i1 %x to i32
@@ -69,12 +66,7 @@ define i32 @mask_negated_sext_bool2(i1 z
 define <4 x i32> @mask_negated_sext_bool_vec(<4 x i1> %x) {
 ; CHECK-LABEL: mask_negated_sext_bool_vec:
 ; CHECK:       # BB#0:
-; CHECK-NEXT:    pslld $31, %xmm0
-; CHECK-NEXT:    psrad $31, %xmm0
-; CHECK-NEXT:    pxor %xmm1, %xmm1
-; CHECK-NEXT:    psubd %xmm0, %xmm1
-; CHECK-NEXT:    pand {{.*}}(%rip), %xmm1
-; CHECK-NEXT:    movdqa %xmm1, %xmm0
+; CHECK-NEXT:    andps {{.*}}(%rip), %xmm0
 ; CHECK-NEXT:    retq
 ;
   %ext = sext <4 x i1> %x to <4 x i32>




More information about the llvm-commits mailing list