PING: PATCH [4/n]: Prepare x32: Permute the conversion and addition if one operand is a constant

H.J. Lu hjl.tools@gmail.com
Mon Jul 11 17:26:00 GMT 2011


On Mon, Jul 11, 2011 at 9:55 AM, H.J. Lu <hjl.tools@gmail.com> wrote:
> On Mon, Jul 11, 2011 at 8:54 AM, H.J. Lu <hjl.tools@gmail.com> wrote:
>> On Mon, Jul 11, 2011 at 4:03 AM, Paolo Bonzini <bonzini@gnu.org> wrote:
>>> On 07/11/2011 02:04 AM, H.J. Lu wrote:
>>>>
>>>> With my original change,  I got
>>>>
>>>> (const:DI (plus:DI (symbol_ref:DI ("iplane.1577") [flags 0x2]
>>>> <var_decl 0x7ffff0857960 iplane>)
>>>>         (const_int -4 [0xfffffffffffffffc])))
>>>>
>>>> I think it is safe to permute the conversion and addition operation
>>>> if one operand is a constant and we are zero-extending.  This is
>>>> how zero-extending works.
>>>
>>> Ok, I think I understand what you mean.  The key is the
>>>
>>>   XEXP (x, 1) == convert_memory_address_addr_space
>>>                  (to_mode, XEXP (x, 1), as)
>>>
>>> test.  It ensures basically that the constant has 31-bit precision, because
>>> otherwise the constant would change from e.g. (const_int -0x7ffffffc) to
>>> (const_int 0x80000004) when zero-extending it from SImode to DImode.
>>>
>>> But I'm not sure it's safe.  You have,
>>>
>>>  (zero_extend:DI (plus:SI FOO:SI) (const_int Y))
>>>
>>> and you want to convert it to
>>>
>>>  (plus:DI FOO:DI (zero_extend:DI (const_int Y)))
>>>
>>> (where the zero_extend is folded).  Ignore that FOO is a SYMBOL_REF (this
>>> piece of code does not assume anything about its shape); if FOO ==
>>> 0xfffffffc and Y = 8, the result will be respectively 0x4 (valid) and
>>> 0x100000004 (invalid).
>>
>> This example contradicts what you said above "It ensures basically that the
>> constant has 31-bit precision".  For zero-extend, the issue is address-wrap.
>> As I understand, to support address-wrap, you need to use ptr_mode.
>>
>
> I am totally confused what the current code
>
>     /* For addition we can safely permute the conversion and addition
>         operation if one operand is a constant and converting the constant
>         does not change it or if one operand is a constant and we are
>         using a ptr_extend instruction  (POINTERS_EXTEND_UNSIGNED < 0).
>         We can always safely permute them if we are making the address
>         narrower.  */
>      if (GET_MODE_SIZE (to_mode) < GET_MODE_SIZE (from_mode)
>          || (GET_CODE (x) == PLUS
>              && CONST_INT_P (XEXP (x, 1))
>              && (XEXP (x, 1) == convert_memory_address_addr_space
>                                   (to_mode, XEXP (x, 1), as)
>                 || POINTERS_EXTEND_UNSIGNED < 0)))
>        return gen_rtx_fmt_ee (GET_CODE (x), to_mode,
>                               convert_memory_address_addr_space
>                                 (to_mode, XEXP (x, 0), as),
>                               XEXP (x, 1));
>
> is trying to do.  It doesn't support address-wrap at all, regardless if
> converting the constant changes the constant.  I think it should be
> OK to permute if no instructions are allowed, like:
>
>     if (GET_MODE_SIZE (to_mode) < GET_MODE_SIZE (from_mode)
>          || (GET_CODE (x) == PLUS
>              && CONST_INT_P (XEXP (x, 1))
>              && POINTERS_EXTEND_UNSIGNED != 0
>              && no_emit))
>        return gen_rtx_fmt_ee (GET_CODE (x), to_mode,
>                               convert_memory_address_addr_space_1
>                                 (to_mode, XEXP (x, 0), as, no_emit),
>                               XEXP (x, 1));
>
>

This patch implements it.

-- 
H.J.
---2011-07-11  H.J. Lu  <hongjiu.lu@intel.com>

	PR middle-end/47727
	* explow.c (convert_memory_address_addr_space_1): New.
	(convert_memory_address_addr_space): Use it.

	* expr.c (convert_modes_1): New.
	(convert_modes): Use it.

	* expr.h (convert_modes_1): New.

	* rtl.h (convert_memory_address_addr_space_1): New.
	(convert_memory_address_1): Likewise.

	* simplify-rtx.c (simplify_unary_operation_1): Call
	convert_memory_address_1 instead of convert_memory_address.
-------------- next part --------------
2011-07-11  H.J. Lu  <hongjiu.lu@intel.com>

	PR middle-end/47727
	* explow.c (convert_memory_address_addr_space_1): New.
	(convert_memory_address_addr_space): Use it.

	* expr.c (convert_modes_1): New.
	(convert_modes): Use it.

	* expr.h (convert_modes_1): New.

	* rtl.h (convert_memory_address_addr_space_1): New.
	(convert_memory_address_1): Likewise.

	* simplify-rtx.c (simplify_unary_operation_1): Call
	convert_memory_address_1 instead of convert_memory_address.

diff --git a/gcc/explow.c b/gcc/explow.c
index 3c692f4..d2c54ff 100644
--- a/gcc/explow.c
+++ b/gcc/explow.c
@@ -320,8 +320,9 @@ break_out_memory_refs (rtx x)
    arithmetic insns can be used.  */
 
 rtx
-convert_memory_address_addr_space (enum machine_mode to_mode ATTRIBUTE_UNUSED,
-				   rtx x, addr_space_t as ATTRIBUTE_UNUSED)
+convert_memory_address_addr_space_1 (enum machine_mode to_mode ATTRIBUTE_UNUSED,
+				     rtx x, addr_space_t as ATTRIBUTE_UNUSED,
+				     bool no_emit ATTRIBUTE_UNUSED)
 {
 #ifndef POINTERS_EXTEND_UNSIGNED
   gcc_assert (GET_MODE (x) == to_mode || GET_MODE (x) == VOIDmode);
@@ -378,27 +379,25 @@ convert_memory_address_addr_space (enum machine_mode to_mode ATTRIBUTE_UNUSED,
 
     case CONST:
       return gen_rtx_CONST (to_mode,
-			    convert_memory_address_addr_space
-			      (to_mode, XEXP (x, 0), as));
+			    convert_memory_address_addr_space_1
+			      (to_mode, XEXP (x, 0), as, no_emit));
       break;
 
     case PLUS:
     case MULT:
-      /* For addition we can safely permute the conversion and addition
-	 operation if one operand is a constant and converting the constant
-	 does not change it or if one operand is a constant and we are
-	 using a ptr_extend instruction  (POINTERS_EXTEND_UNSIGNED < 0).
-	 We can always safely permute them if we are making the address
-	 narrower.  */
+      /* For addition, we permute the conversion and addition operation
+	 if one operand is a constant, no new instructions are allowed
+	 and we are using a ptr_extend instruction or zero-extending
+	 (POINTERS_EXTEND_UNSIGNED != 0).  We can always safely permute
+	 them if we are making the address narrower.  */
       if (GET_MODE_SIZE (to_mode) < GET_MODE_SIZE (from_mode)
 	  || (GET_CODE (x) == PLUS
 	      && CONST_INT_P (XEXP (x, 1))
-	      && (XEXP (x, 1) == convert_memory_address_addr_space
-				   (to_mode, XEXP (x, 1), as)
-                 || POINTERS_EXTEND_UNSIGNED < 0)))
+	      && POINTERS_EXTEND_UNSIGNED != 0
+	      && no_emit))
 	return gen_rtx_fmt_ee (GET_CODE (x), to_mode,
-			       convert_memory_address_addr_space
-				 (to_mode, XEXP (x, 0), as),
+			       convert_memory_address_addr_space_1
+				 (to_mode, XEXP (x, 0), as, no_emit),
 			       XEXP (x, 1));
       break;
 
@@ -406,10 +405,17 @@ convert_memory_address_addr_space (enum machine_mode to_mode ATTRIBUTE_UNUSED,
       break;
     }
 
-  return convert_modes (to_mode, from_mode,
-			x, POINTERS_EXTEND_UNSIGNED);
+  return convert_modes_1 (to_mode, from_mode, x,
+			  POINTERS_EXTEND_UNSIGNED, no_emit);
 #endif /* defined(POINTERS_EXTEND_UNSIGNED) */
 }
+
+rtx
+convert_memory_address_addr_space (enum machine_mode to_mode,
+				   rtx x, addr_space_t as)
+{
+  return convert_memory_address_addr_space_1 (to_mode, x, as, false);
+}
 

 /* Return something equivalent to X but valid as a memory address for something
    of mode MODE in the named address space AS.  When X is not itself valid,
diff --git a/gcc/expr.c b/gcc/expr.c
index fb4379f..de7f150 100644
--- a/gcc/expr.c
+++ b/gcc/expr.c
@@ -693,13 +693,16 @@ convert_to_mode (enum machine_mode mode, rtx x, int unsignedp)
    Both modes may be floating, or both integer.
    UNSIGNEDP is nonzero if X is an unsigned value.
 
+   If NO_EMIT is true, don't emit any instructions.
+
    This can be done by referring to a part of X in place
    or by copying to a new temporary with conversion.
 
    You can give VOIDmode for OLDMODE, if you are sure X has a nonvoid mode.  */
 
 rtx
-convert_modes (enum machine_mode mode, enum machine_mode oldmode, rtx x, int unsignedp)
+convert_modes_1 (enum machine_mode mode, enum machine_mode oldmode,
+		 rtx x, int unsignedp, bool no_emit)
 {
   rtx temp;
 
@@ -709,7 +712,12 @@ convert_modes (enum machine_mode mode, enum machine_mode oldmode, rtx x, int uns
   if (GET_CODE (x) == SUBREG && SUBREG_PROMOTED_VAR_P (x)
       && GET_MODE_SIZE (GET_MODE (SUBREG_REG (x))) >= GET_MODE_SIZE (mode)
       && SUBREG_PROMOTED_UNSIGNED_P (x) == unsignedp)
-    x = gen_lowpart (mode, x);
+    {
+      if (no_emit)
+	x = rtl_hooks.gen_lowpart_no_emit (mode, x);
+      else
+	x = gen_lowpart (mode, x);
+    }
 
   if (GET_MODE (x) != VOIDmode)
     oldmode = GET_MODE (x);
@@ -773,7 +781,10 @@ convert_modes (enum machine_mode mode, enum machine_mode oldmode, rtx x, int uns
 	  return gen_int_mode (val, mode);
 	}
 
-      return gen_lowpart (mode, x);
+      if (no_emit)
+	return rtl_hooks.gen_lowpart_no_emit (mode, x);
+      else
+	return gen_lowpart (mode, x);
     }
 
   /* Converting from integer constant into mode is always equivalent to an
@@ -784,10 +795,18 @@ convert_modes (enum machine_mode mode, enum machine_mode oldmode, rtx x, int uns
       return simplify_gen_subreg (mode, x, oldmode, 0);
     }
 
+  gcc_assert (!no_emit);
   temp = gen_reg_rtx (mode);
   convert_move (temp, x, unsignedp);
   return temp;
 }
+
+rtx
+convert_modes (enum machine_mode mode, enum machine_mode oldmode,
+	       rtx x, int unsignedp)
+{
+  return convert_modes_1 (mode, oldmode, x, unsignedp, false);
+}
 

 /* Return the largest alignment we can use for doing a move (or store)
    of MAX_PIECES.  ALIGN is the largest alignment we could use.  */
diff --git a/gcc/expr.h b/gcc/expr.h
index cb4050d..2ac9788 100644
--- a/gcc/expr.h
+++ b/gcc/expr.h
@@ -267,6 +267,8 @@ extern rtx convert_to_mode (enum machine_mode, rtx, int);
 
 /* Convert an rtx to MODE from OLDMODE and return the result.  */
 extern rtx convert_modes (enum machine_mode, enum machine_mode, rtx, int);
+extern rtx convert_modes_1 (enum machine_mode, enum machine_mode, rtx,
+			    int, bool);
 
 /* Emit code to move a block Y to a block X.  */
 
diff --git a/gcc/rtl.h b/gcc/rtl.h
index e3ceecd..b01eef8 100644
--- a/gcc/rtl.h
+++ b/gcc/rtl.h
@@ -1638,8 +1638,13 @@ extern int byte_lowpart_offset (enum machine_mode, enum machine_mode);
 extern rtx make_safe_from (rtx, rtx);
 extern rtx convert_memory_address_addr_space (enum machine_mode, rtx,
 					      addr_space_t);
+extern rtx convert_memory_address_addr_space_1 (enum machine_mode, rtx,
+						addr_space_t, bool);
 #define convert_memory_address(to_mode,x) \
 	convert_memory_address_addr_space ((to_mode), (x), ADDR_SPACE_GENERIC)
+#define convert_memory_address_1(to_mode,x,no_emit) \
+	convert_memory_address_addr_space_1 ((to_mode), (x), \
+					     ADDR_SPACE_GENERIC, (no_emit))
 extern const char *get_insn_name (int);
 extern rtx get_last_insn_anywhere (void);
 extern rtx get_first_nonnote_insn (void);
diff --git a/gcc/simplify-rtx.c b/gcc/simplify-rtx.c
index 82b818b..189c201 100644
--- a/gcc/simplify-rtx.c
+++ b/gcc/simplify-rtx.c
@@ -1150,7 +1150,7 @@ simplify_unary_operation_1 (enum rtx_code code, enum machine_mode mode, rtx op)
 		  && REG_P (SUBREG_REG (op))
 		  && REG_POINTER (SUBREG_REG (op))
 		  && GET_MODE (SUBREG_REG (op)) == Pmode)))
-	return convert_memory_address (Pmode, op);
+	return convert_memory_address_1 (Pmode, op, true);
 #endif
       break;
 
@@ -1243,7 +1243,7 @@ simplify_unary_operation_1 (enum rtx_code code, enum machine_mode mode, rtx op)
 		  && REG_P (SUBREG_REG (op))
 		  && REG_POINTER (SUBREG_REG (op))
 		  && GET_MODE (SUBREG_REG (op)) == Pmode)))
-	return convert_memory_address (Pmode, op);
+	return convert_memory_address_1 (Pmode, op, true);
 #endif
       break;
 


More information about the Gcc-patches mailing list