diff mbox

[1/2] PR49847: Add hook to place read-only lookup-tables in named address-space

Message ID 9706f7c7-c427-4e64-af62-4cecf52cbe02@gjlay.de
State New
Headers show

Commit Message

Georg-Johann Lay July 27, 2017, 12:41 p.m. UTC
On 27.07.2017 14:29, Georg-Johann Lay wrote:
> For some targets, the best place to put read-only lookup tables as
> generated by -ftree-switch-conversion is not the generic address space
> but some target specific address space.
> 
> This is the case for AVR, where for most devices .rodata must be
> located in RAM.
> 
> Part #1 adds a new, optional target hook that queries the back-end
> about the desired address space.  There is currently only one user:
> tree-switch-conversion.c::build_one_array() which re-builds value_type
> and array_type if the address space returned by the backend is not
> the generic one.
> 
> Part #2 is the AVR part that implements the new hook and adds some
> sugar around it.

This is the gcc part which adds the new hook 
TARGET_ADDR_SPACE_FOR_ARTIFICIAL_RODATA resp. 
targetm.addr_space.for_artificial_rodata().

The default implementation returns ADDR_SPACE_GENERIC which represents
a no-op.  Only if !ADDR_SPACE_GENERIC_P, the array and value type are
re-built.

The accesses must be in such a way that any access to the newly created
array matches the address space.  This is the reason for why the
back-end cannot do it on its own:  Just putting the stuff in a specific
section does *not* do the trick.

Bootstrapped ok on x86_64.

Johann

	PR 49857
	* doc/tm.texi.in (Named Address Spaces)
	[TARGET_ADDR_SPACE_FOR_ARTIFICIAL_RODATA]: Add hook anchor.
	* doc/tm.texi: Regenerate.
	* target.def (addr_space) [for_artificial_rodata]: New optional hook.
	* targhooks.c (default_addr_space_for_artificial_rodata): New function.
	* targhooks.h (default_addr_space_for_artificial_rodata): New proto.
	* tree-switch-conversion.c (target.h): Include it.
	(build_one_array): Set address space of value_type according to
	targetm.addr_space.for_artificial_rodata() and rebuild array_type
	if needed.
diff mbox

Patch

Index: target.def
===================================================================
--- target.def	(revision 250302)
+++ target.def	(working copy)
@@ -3285,6 +3285,25 @@  The default implementation does nothing.
  void, (addr_space_t as, location_t loc),
  default_addr_space_diagnose_usage)
 
+/* Function to get the address space of some compiler-generated
+   read-only data.  Used for optimization purposes only.  */
+DEFHOOK
+(for_artificial_rodata,
+ "Define this hook to return an address space to be used for @var{type},\n\
+usually an artificial lookup-table that would reside in @code{.rodata}.\n\
+It is always safe not to implement this hook or to return\n\
+@code{ADDR_SPACE_GENERIC}.\n\
+\n\
+The hook can be used to put compiler-generated, artificial data in\n\
+static stzorage into a specific address space when this is better suited\n\
+than the generic address space.\n\
+The compiler will also generate all accesses to the respective data\n\
+so that all associated accesses will also use the desired address space.\n\
+An example for such data are the @code{CSWTCH} lookup tables as generated\n\
+by @option{-ftree-switch-conversion}.",
+ addr_space_t, (tree type),
+ default_addr_space_for_artificial_rodata)
+
 HOOK_VECTOR_END (addr_space)
 
 #undef HOOK_PREFIX
Index: targhooks.c
===================================================================
--- targhooks.c	(revision 250302)
+++ targhooks.c	(working copy)
@@ -1397,6 +1397,14 @@  default_addr_space_convert (rtx op ATTRI
   gcc_unreachable ();
 }
 
+/* The default hook for TARGET_ADDR_SPACE_FOR_ARTIFICIAL_RODATA.  */
+
+addr_space_t
+default_addr_space_for_artificial_rodata (tree)
+{
+  return ADDR_SPACE_GENERIC;
+}
+
 bool
 default_hard_regno_scratch_ok (unsigned int regno ATTRIBUTE_UNUSED)
 {
Index: targhooks.h
===================================================================
--- targhooks.h	(revision 250302)
+++ targhooks.h	(working copy)
@@ -184,6 +184,7 @@  extern bool default_addr_space_zero_addr
 extern int default_addr_space_debug (addr_space_t);
 extern void default_addr_space_diagnose_usage (addr_space_t, location_t);
 extern rtx default_addr_space_convert (rtx, tree, tree);
+extern addr_space_t default_addr_space_for_artificial_rodata (tree);
 extern unsigned int default_case_values_threshold (void);
 extern bool default_have_conditional_execution (void);
 
Index: tree-switch-conversion.c
===================================================================
--- tree-switch-conversion.c	(revision 250302)
+++ tree-switch-conversion.c	(working copy)
@@ -46,6 +46,7 @@  Software Foundation, 51 Franklin Street,
 #include "gimplify-me.h"
 #include "tree-cfg.h"
 #include "cfgloop.h"
+#include "target.h"
 
 /* ??? For lang_hooks.types.type_for_mode, but is there a word_mode
    type in the GIMPLE type system that is language-independent?  */
@@ -1136,6 +1137,16 @@  build_one_array (gswitch *swtch, int num
       default_type = TREE_TYPE (info->default_values[num]);
       value_type = array_value_type (swtch, default_type, num, info);
       array_type = build_array_type (value_type, arr_index_type);
+      // Run the following hook on the complete array so the back-end
+      // can inspect details of it.
+      addr_space_t as = targetm.addr_space.for_artificial_rodata (array_type);
+      if (!ADDR_SPACE_GENERIC_P (as))
+	{
+	  int quals = (TYPE_QUALS_NO_ADDR_SPACE (value_type)
+		       | ENCODE_QUAL_ADDR_SPACE (as));
+	  value_type = build_qualified_type (value_type, quals);
+	  array_type = build_array_type (value_type, arr_index_type);
+	}
       if (default_type != value_type)
 	{
 	  unsigned int i;
Index: doc/tm.texi
===================================================================
--- doc/tm.texi	(revision 250302)
+++ doc/tm.texi	(working copy)
@@ -10595,6 +10595,21 @@  the address space as registered with @co
 The default implementation does nothing.
 @end deftypefn
 
+@deftypefn {Target Hook} addr_space_t TARGET_ADDR_SPACE_FOR_ARTIFICIAL_RODATA (tree @var{type})
+Define this hook to return an address space to be used for @var{type},
+usually an artificial lookup-table that would reside in @code{.rodata}.
+It is always safe not to implement this hook or to return
+@code{ADDR_SPACE_GENERIC}.
+
+The hook can be used to put compiler-generated, artificial data in
+static stzorage into a specific address space when this is better suited
+than the generic address space.
+The compiler will also generate all accesses to the respective data
+so that all associated accesses will also use the desired address space.
+An example for such data are the @code{CSWTCH} lookup tables as generated
+by @option{-ftree-switch-conversion}.
+@end deftypefn
+
 @node Misc
 @section Miscellaneous Parameters
 @cindex parameters, miscellaneous
Index: doc/tm.texi.in
===================================================================
--- doc/tm.texi.in	(revision 250302)
+++ doc/tm.texi.in	(working copy)
@@ -7529,6 +7529,8 @@  c_register_addr_space ("__ea", ADDR_SPAC
 
 @hook TARGET_ADDR_SPACE_DIAGNOSE_USAGE
 
+@hook TARGET_ADDR_SPACE_FOR_ARTIFICIAL_RODATA
+
 @node Misc
 @section Miscellaneous Parameters
 @cindex parameters, miscellaneous