[X86] Prevent folding a load into an AND if that AND is really a ZEXT_INREG that...
authorCraig Topper <craig.topper@intel.com>
Wed, 24 Apr 2019 19:28:38 +0000 (19:28 +0000)
committerCraig Topper <craig.topper@intel.com>
Wed, 24 Apr 2019 19:28:38 +0000 (19:28 +0000)
This can save a 32-bit immediate move.

We would shrink the load and fold it if it was non-volatile, but that's trickier to check for.

llvm-svn: 359129

llvm/lib/Target/X86/X86ISelDAGToDAG.cpp
llvm/test/CodeGen/X86/2008-09-29-VolatileBug.ll
llvm/test/CodeGen/X86/fold-and-shift.ll

index df7f1fc..dd387e5 100644 (file)
@@ -592,6 +592,15 @@ X86DAGToDAGISel::IsProfitableToFold(SDValue N, SDNode *U, SDNode *Root) const {
             Imm->getAPIntValue().isIntN(32))
           return false;
 
+        // If this really a zext_inreg that can be represented with a movzx
+        // instruction, prefer that.
+        // TODO: We could shrink the load and fold if it is non-volatile.
+        if (U->getOpcode() == ISD::AND &&
+            (Imm->getAPIntValue() == UINT8_MAX ||
+             Imm->getAPIntValue() == UINT16_MAX ||
+             Imm->getAPIntValue() == UINT32_MAX))
+          return false;
+
         // ADD/SUB with can negate the immediate and use the opposite operation
         // to fit 128 into a sign extended 8 bit immediate.
         if ((U->getOpcode() == ISD::ADD || U->getOpcode() == ISD::SUB) &&
index ac18c2c..4c81df7 100644 (file)
@@ -8,8 +8,8 @@
 define i32 @main() nounwind {
 ; CHECK-LABEL: main:
 ; CHECK:       # %bb.0: # %entry
-; CHECK-NEXT:    movl $255, %eax
-; CHECK-NEXT:    andl g_407, %eax
+; CHECK-NEXT:    movl g_407, %eax
+; CHECK-NEXT:    movzbl %al, %eax
 ; CHECK-NEXT:    pushl %eax
 ; CHECK-NEXT:    calll func_45
 ; CHECK-NEXT:    addl $4, %esp
index e14e337..6db4b25 100644 (file)
@@ -5,8 +5,8 @@ define i32 @t1(i8* %X, i32 %i) {
 ; CHECK-LABEL: t1:
 ; CHECK:       # %bb.0: # %entry
 ; CHECK-NEXT:    movl {{[0-9]+}}(%esp), %eax
-; CHECK-NEXT:    movl $255, %ecx
-; CHECK-NEXT:    andl {{[0-9]+}}(%esp), %ecx
+; CHECK-NEXT:    movl {{[0-9]+}}(%esp), %ecx
+; CHECK-NEXT:    movzbl %cl, %ecx
 ; CHECK-NEXT:    movl (%eax,%ecx,4), %eax
 ; CHECK-NEXT:    retl
 
@@ -23,8 +23,8 @@ define i32 @t2(i16* %X, i32 %i) {
 ; CHECK-LABEL: t2:
 ; CHECK:       # %bb.0: # %entry
 ; CHECK-NEXT:    movl {{[0-9]+}}(%esp), %eax
-; CHECK-NEXT:    movl $65535, %ecx # imm = 0xFFFF
-; CHECK-NEXT:    andl {{[0-9]+}}(%esp), %ecx
+; CHECK-NEXT:    movl {{[0-9]+}}(%esp), %ecx
+; CHECK-NEXT:    movzwl %cx, %ecx
 ; CHECK-NEXT:    movl (%eax,%ecx,4), %eax
 ; CHECK-NEXT:    retl