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

Sanjay Patel via llvm-commits llvm-commits at lists.llvm.org
Tue Oct 11 09:26:37 PDT 2016


Author: spatel
Date: Tue Oct 11 11:26:36 2016
New Revision: 283900

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

The non-obvious motivation for adding this fold (which already happens in InstCombine)
is that we want to canonicalize IR towards select instructions and canonicalize DAG 
nodes towards boolean math. So we need to recreate some folds in the DAG to handle that
change in direction. 

An interesting implementation difference for cases like this is that InstCombine
generally works top-down while the DAG goes bottom-up. That means we need to detect 
different patterns. In this case, the SimplifyDemandedBits fold prevents us from 
performing a zext to sext fold that would then be recognized as a negation of a sext. 

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=283900&r1=283899&r2=283900&view=diff
==============================================================================
--- llvm/trunk/lib/CodeGen/SelectionDAG/DAGCombiner.cpp (original)
+++ llvm/trunk/lib/CodeGen/SelectionDAG/DAGCombiner.cpp Tue Oct 11 11:26:36 2016
@@ -3314,10 +3314,23 @@ SDValue DAGCombiner::visitAND(SDNode *N)
     if (SDValue Tmp = SimplifyBinOpWithSameOpcodeHands(N))
       return Tmp;
 
+  // Masking the negated extension of a boolean is just the extended boolean:
+  // and (sub 0, zext(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;
+  }
+
   // fold (and (sign_extend_inreg x, i16 to i32), 1) -> (and x, 1)
   // fold (and (sra)) -> (and (srl)) when possible.
-  if (!VT.isVector() &&
-      SimplifyDemandedBits(SDValue(N, 0)))
+  if (!VT.isVector() && SimplifyDemandedBits(SDValue(N, 0)))
     return SDValue(N, 0);
 
   // fold (zext_inreg (extload x)) -> (zextload x)

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=283900&r1=283899&r2=283900&view=diff
==============================================================================
--- llvm/trunk/test/CodeGen/X86/mask-negated-bool.ll (original)
+++ llvm/trunk/test/CodeGen/X86/mask-negated-bool.ll Tue Oct 11 11:26:36 2016
@@ -4,7 +4,6 @@
 define i32 @mask_negated_extended_bool1(i1 %x) {
 ; CHECK-LABEL: mask_negated_extended_bool1:
 ; CHECK:       # BB#0:
-; CHECK-NEXT:    negl %edi
 ; CHECK-NEXT:    andl $1, %edi
 ; CHECK-NEXT:    movl %edi, %eax
 ; CHECK-NEXT:    retq
@@ -19,8 +18,6 @@ define i32 @mask_negated_extended_bool2(
 ; CHECK-LABEL: mask_negated_extended_bool2:
 ; CHECK:       # BB#0:
 ; CHECK-NEXT:    movzbl %dil, %eax
-; CHECK-NEXT:    negl %eax
-; CHECK-NEXT:    andl $1, %eax
 ; CHECK-NEXT:    retq
 ;
   %ext = zext i1 %x to i32
@@ -32,12 +29,7 @@ define i32 @mask_negated_extended_bool2(
 define <4 x i32> @mask_negated_extended_bool_vec(<4 x i1> %x) {
 ; CHECK-LABEL: mask_negated_extended_bool_vec:
 ; CHECK:       # BB#0:
-; CHECK-NEXT:    movdqa {{.*#+}} xmm2 = [1,1,1,1]
-; CHECK-NEXT:    pand %xmm2, %xmm0
-; CHECK-NEXT:    pxor %xmm1, %xmm1
-; CHECK-NEXT:    psubd %xmm0, %xmm1
-; CHECK-NEXT:    pand %xmm2, %xmm1
-; CHECK-NEXT:    movdqa %xmm1, %xmm0
+; CHECK-NEXT:    andps {{.*}}(%rip), %xmm0
 ; CHECK-NEXT:    retq
 ;
   %ext = zext <4 x i1> %x to <4 x i32>




More information about the llvm-commits mailing list