public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [PATCH 1/2] md: Allow <FOO> to refer to the value of int iterator FOO
@ 2023-05-23 10:36 Richard Sandiford
  0 siblings, 0 replies; only message in thread
From: Richard Sandiford @ 2023-05-23 10:36 UTC (permalink / raw)
  To: gcc-patches

In a follow-up patch, I wanted to use an int iterator to iterate
over various possible values of a const_int.  But one problem
with int iterators was that there was no way of referring to the
current value of the iterator.  This is unlike modes and codes,
which provide automatic "mode", "MODE", "code" and "CODE"
attribbutes.  These automatic definitions are the equivalent
of an explicit:

  (define_mode_attr mode [(QI "qi") (HI "hi") ...])

We obviously can't do that for every possible value of an int.

One option would have been to go for some kind of lazily-populated
attribute.  But that sounds quite complicated.  This patch instead
goes for the simpler approach of allowing <FOO> to refer to the
current value of FOO.

In principle it would be possible to allow the same thing
for mode and code iterators.  But for modes there are at least
4 realistic possiblities:

  - the E_* enumeration value (which is what this patch would give)
  - the user-facing C token, like DImode, SFmode, etc.
  - the equivalent of <MODE>
  - the equivalent of <mode>

Because of this ambiguity, it seemed better to stick to the
current approach for modes.  For codes it's less clear-cut,
but <CODE> and <code> are both realistic possibilities, so again
it seemed better to be explicit.

The patch also removes “Each @var{int} must have the same rtx format.
@xref{RTL Classes}.”, which was erroneously copied from the code
iterator section.

Tested on aarch64-linux-gnu and x86_64-linux-gnu.  Also tested by
checking that no port had an int iterator whose name matched an
existing attribute.  Pushed to trunk.

Richard


gcc/
	* doc/md.texi: Document that <FOO> can be used to refer to the
	numerical value of an int iterator FOO.  Tweak other parts of
	the int iterator documentation.
	* read-rtl.cc (iterator_group::has_self_attr): New field.
	(map_attr_string): When has_self_attr is true, make <FOO>
	expand to the current value of iterator FOO.
	(initialize_iterators): Set has_self_attr for int iterators.
---
 gcc/doc/md.texi | 15 +++++++++------
 gcc/read-rtl.cc | 26 ++++++++++++++++++++++++++
 2 files changed, 35 insertions(+), 6 deletions(-)

diff --git a/gcc/doc/md.texi b/gcc/doc/md.texi
index 8ebce31ba78..6a435eb4461 100644
--- a/gcc/doc/md.texi
+++ b/gcc/doc/md.texi
@@ -11502,11 +11502,10 @@ The construct:
 @end smallexample
 
 defines a pseudo integer constant @var{name} that can be instantiated as
-@var{inti} if condition @var{condi} is true.  Each @var{int} must have the
-same rtx format.  @xref{RTL Classes}.  Int iterators can appear in only
-those rtx fields that have 'i', 'n', 'w', or 'p' as the specifier.  This
-means that each @var{int} has to be a constant defined using define_constant
-or define_c_enum.
+@var{inti} if condition @var{condi} is true.  Int iterators can appear in
+only those rtx fields that have `i', `n', `w', or `p' as the specifier.
+This means that each @var{int} has to be a constant defined using
+@samp{define_constant} or @samp{define_c_enum}.
 
 As with mode and code iterators, each pattern that uses @var{name} will be
 expanded @var{n} times, once with all uses of @var{name} replaced by
@@ -11517,9 +11516,13 @@ It is possible to define attributes for ints as well as for codes and modes.
 Attributes are defined using:
 
 @smallexample
-(define_int_attr @var{name} [(@var{int1} "@var{value1}") @dots{} (@var{intn} "@var{valuen}")])
+(define_int_attr @var{attr_name} [(@var{int1} "@var{value1}") @dots{} (@var{intn} "@var{valuen}")])
 @end smallexample
 
+In additon to these user-defined attributes, it is possible to use
+@samp{<@var{name}>} to refer to the current expansion of iterator
+@var{name} (such as @var{int1}, @var{int2}, and so on).
+
 Here's an example of int iterators in action, taken from the ARM port:
 
 @smallexample
diff --git a/gcc/read-rtl.cc b/gcc/read-rtl.cc
index 8cb25aebdbb..292f8b72d43 100644
--- a/gcc/read-rtl.cc
+++ b/gcc/read-rtl.cc
@@ -80,6 +80,12 @@ struct iterator_group {
 
   /* Return the C token for the given standard mode, code, etc.  */
   const char *(*get_c_token) (int);
+
+  /* True if each iterator name should be treated as an attribute that
+     maps to the C token produced by get_c_token.  This means that for
+     an iterator ITER, <ITER> can be used in strings to refer to the
+     current value of ITER, as a C token.  */
+  bool has_self_attr;
 };
 
 /* Records one use of an iterator.  */
@@ -472,6 +478,25 @@ map_attr_string (file_location loc, const char *p, mapping **iterator_out = 0)
 	      || iterator->name[iterator_name_len] != 0))
 	continue;
 
+      if (iterator->group->has_self_attr
+	  && strcmp (attr, iterator->name) == 0)
+	{
+	  if (iterator_out)
+	    *iterator_out = iterator;
+	  int number = iterator->current_value->number;
+	  const char *string = iterator->group->get_c_token (number);
+	  if (res && strcmp (string, res->string) != 0)
+	    {
+	      error_at (loc, "ambiguous attribute '%s'; could be"
+			" '%s' (via '%s:%s') or '%s' (via '%s:%s')",
+			attr, res->string, prev->name, attr,
+			string, iterator->name, attr);
+	      return res;
+	    }
+	  prev = iterator;
+	  res = new map_value { nullptr, number, string };
+	}
+
       /* Find the attribute specification.  */
       m = (struct mapping *) htab_find (iterator->group->attrs, &attr);
       if (m)
@@ -1035,6 +1060,7 @@ initialize_iterators (void)
   ints.find_builtin = find_int;
   ints.apply_iterator = apply_int_iterator;
   ints.get_c_token = get_int_token;
+  ints.has_self_attr = true;
 
   substs.attrs = htab_create (13, leading_string_hash, leading_string_eq_p, 0);
   substs.iterators = htab_create (13, leading_string_hash,
-- 
2.25.1


^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2023-05-23 10:44 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-05-23 10:36 [PATCH 1/2] md: Allow <FOO> to refer to the value of int iterator FOO Richard Sandiford

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).