simplify-rtx: Simplify trunc of and of shiftrt
authorsegher <segher@138bc75d-0d04-0410-961f-82ee72b054a4>
Wed, 11 Nov 2015 14:09:30 +0000 (14:09 +0000)
committersegher <segher@138bc75d-0d04-0410-961f-82ee72b054a4>
Wed, 11 Nov 2015 14:09:30 +0000 (14:09 +0000)
If we have

(truncate:M1 (and:M2 (lshiftrt:M2 (x:M2) C) C2))

we can write it instead as

(and:M1 (lshiftrt:M1 (truncate:M1 (x:M2)) C) C2)

(if that is valid, of course), which has smaller modes for the
binary ops, and the truncate can often simplify further (if "x"
is a register, for example).

* gcc/simplify-rtx.c (simplify_truncation): Simplify TRUNCATE
of AND of [LA]SHIFTRT.

git-svn-id: svn+ssh://gcc.gnu.org/svn/gcc/trunk@230164 138bc75d-0d04-0410-961f-82ee72b054a4

gcc/ChangeLog
gcc/simplify-rtx.c

index fb7e4f0..eb556d0 100644 (file)
@@ -1,3 +1,8 @@
+2015-11-11  Segher Boessenkool  <segher@kernel.crashing.org>
+
+       * gcc/simplify-rtx.c (simplify_truncation): Simplify TRUNCATE
+       of AND of [LA]SHIFTRT.
+
 2015-11-11  Martin Liska  <mliska@suse.cz>
            Richard Biener  <rguenther@suse.de>
 
index 17568ba..c4fc42a 100644 (file)
@@ -714,6 +714,34 @@ simplify_truncation (machine_mode mode, rtx op,
     return simplify_gen_binary (ASHIFT, mode,
                                XEXP (XEXP (op, 0), 0), XEXP (op, 1));
 
+  /* Likewise (truncate:QI (and:SI (lshiftrt:SI (x:SI) C) C2)) into
+     (and:QI (lshiftrt:QI (truncate:QI (x:SI)) C) C2) for suitable C
+     and C2.  */
+  if (GET_CODE (op) == AND
+      && (GET_CODE (XEXP (op, 0)) == LSHIFTRT
+         || GET_CODE (XEXP (op, 0)) == ASHIFTRT)
+      && CONST_INT_P (XEXP (XEXP (op, 0), 1))
+      && CONST_INT_P (XEXP (op, 1)))
+    {
+      rtx op0 = (XEXP (XEXP (op, 0), 0));
+      rtx shift_op = XEXP (XEXP (op, 0), 1);
+      rtx mask_op = XEXP (op, 1);
+      unsigned HOST_WIDE_INT shift = UINTVAL (shift_op);
+      unsigned HOST_WIDE_INT mask = UINTVAL (mask_op);
+
+      if (shift < precision
+         /* If doing this transform works for an X with all bits set,
+            it works for any X.  */
+         && ((GET_MODE_MASK (mode) >> shift) & mask)
+            == ((GET_MODE_MASK (op_mode) >> shift) & mask)
+         && (op0 = simplify_gen_unary (TRUNCATE, mode, op0, op_mode))
+         && (op0 = simplify_gen_binary (LSHIFTRT, mode, op0, shift_op)))
+       {
+         mask_op = GEN_INT (trunc_int_for_mode (mask, mode));
+         return simplify_gen_binary (AND, mode, op0, mask_op);
+       }
+    }
+
   /* Recognize a word extraction from a multi-word subreg.  */
   if ((GET_CODE (op) == LSHIFTRT
        || GET_CODE (op) == ASHIFTRT)