[PATCH, middle-end] Transform (and (truncate)) into (truncate (and))

Adam Nemet anemet@caviumnetworks.com
Thu Jul 2 19:47:00 GMT 2009


This is to give a chance to the TARGET_MODE_REP_EXTENDED machinery to remove
another truncate as in:

  (set (reg:SI A) (truncate:SI (reg:DI B)))
  (set (reg:SI C) (and:SI (reg:SI A) (const_int 0xfff)))
->
  (set (subreg:DI (reg:SI C)) (and:DI (reg:DI B) (const_int 0xfff)))

Bootstrapped and regtested on {mips64octeon,x86_64}-linux and regtested on
mipsisa64r2-elf.  No assembly change in gcc.c-torture/execute for x86_64.

OK after the bug-fix patch from yesterday?

Adam


	* simplify-rtx.c (simplify_binary_operation_1) <AND>: Transform (and
        (truncate)) into (truncate (and)).

testsuite/
	* gcc.target/mips/truncate-5.c: New test.

Index: gcc/simplify-rtx.c
===================================================================
--- gcc.orig/simplify-rtx.c	2009-07-01 15:31:09.000000000 -0700
+++ gcc/simplify-rtx.c	2009-07-01 15:31:35.000000000 -0700
@@ -2336,6 +2336,18 @@ simplify_binary_operation_1 (enum rtx_co
 	  return simplify_gen_unary (ZERO_EXTEND, mode, tem, imode);
 	}
 
+      /* Transform (and (truncate X) C) into (truncate (and X C)).  This way
+	 we might be able to further simplify the AND with X and potentially
+	 remove the truncation altogether.  */
+      if (GET_CODE (op0) == TRUNCATE && CONST_INT_P (trueop1))
+	{
+	  rtx x = XEXP (op0, 0);
+	  enum machine_mode xmode = GET_MODE (x);
+	  tem = simplify_gen_binary (AND, xmode, x,
+				     gen_int_mode (INTVAL (trueop1), xmode));
+	  return simplify_gen_unary (TRUNCATE, mode, tem, xmode);
+	}
+
       /* Canonicalize (A | C1) & C2 as (A & C2) | (C1 & C2).  */
       if (GET_CODE (op0) == IOR
 	  && CONST_INT_P (trueop1)
Index: gcc/testsuite/gcc.target/mips/truncate-5.c
===================================================================
--- /dev/null	1970-01-01 00:00:00.000000000 +0000
+++ gcc/testsuite/gcc.target/mips/truncate-5.c	2009-07-01 15:31:35.000000000 -0700
@@ -0,0 +1,14 @@
+/* If we AND in DI mode (i.e. replace the order TRUNCATE and the AND) then we
+   can remove the TRUNCATE.  */
+/* { dg-options "-O -mgp64" } */
+/* { dg-final { scan-assembler-not "\tsll\t\[^\n\]*,0" } } */
+
+struct s
+{
+  unsigned a:5;
+};
+
+f (struct s *s, unsigned long long a)
+{
+  s->a = a & 0x3;
+}



More information about the Gcc-patches mailing list