Fold "and (LOAD P), 255" -> zextload. This allows us to compile:
authorChris Lattner <sabre@nondot.org>
Tue, 28 Feb 2006 06:35:35 +0000 (06:35 +0000)
committerChris Lattner <sabre@nondot.org>
Tue, 28 Feb 2006 06:35:35 +0000 (06:35 +0000)
unsigned foo3(unsigned *P) { return *P & 255; }
as:
_foo3:
        lbz r3, 3(r3)
        blr

instead of:

_foo3:
        lwz r2, 0(r3)
        rlwinm r3, r2, 0, 24, 31
        blr

and:

unsigned short foo2(float a) { return a; }

as:
_foo2:
        fctiwz f0, f1
        stfd f0, -8(r1)
        lhz r3, -2(r1)
        blr

instead of:

_foo2:
        fctiwz f0, f1
        stfd f0, -8(r1)
        lwz r2, -4(r1)
        rlwinm r3, r2, 0, 16, 31
        blr

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@26417 91177308-0d34-0410-b5e6-96231b3b80d8

lib/CodeGen/SelectionDAG/DAGCombiner.cpp

index da63be3a1e153ec2b2aa4bcc366704af537f4be7..82b8720ea173d65a1914b7191066c28d408059cf 100644 (file)
@@ -1042,6 +1042,39 @@ SDOperand DAGCombiner::visitAND(SDNode *N) {
       return SDOperand();
     }
   }
+  
+  // fold (and (load x), 255) -> (zextload x)
+  if (N1C && N0.getOpcode() == ISD::LOAD && N0.hasOneUse()) {
+    MVT::ValueType EVT;
+    if (N1C->getValue() == 255)
+      EVT = MVT::i8;
+    else if (N1C->getValue() == 65535)
+      EVT = MVT::i16;
+    else if (N1C->getValue() == ~0U)
+      EVT = MVT::i32;
+    else
+      EVT = MVT::Other;
+    if (EVT != MVT::Other) {
+      assert(MVT::getSizeInBits(VT) > MVT::getSizeInBits(EVT) &&
+             "Cannot zext to larger type!");
+      MVT::ValueType PtrType = N0.getOperand(1).getValueType();
+      // For big endian targets, we need to add an offset to the pointer to load
+      // the correct bytes.  For little endian systems, we merely need to read
+      // fewer bytes from the same pointer.
+      uint64_t PtrOff = (MVT::getSizeInBits(VT) - MVT::getSizeInBits(EVT)) / 8;
+      SDOperand NewPtr = TLI.isLittleEndian() ? N0.getOperand(1) : 
+        DAG.getNode(ISD::ADD, PtrType, N0.getOperand(1),
+                    DAG.getConstant(PtrOff, PtrType));
+      WorkList.push_back(NewPtr.Val);
+      SDOperand Load =
+        DAG.getExtLoad(ISD::ZEXTLOAD, VT, N0.getOperand(0), NewPtr,
+                       N0.getOperand(2), EVT);
+      WorkList.push_back(N);
+      CombineTo(N0.Val, Load, Load.getValue(1));
+      return SDOperand();
+    }
+  }
+  
   return SDOperand();
 }