From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 1130) id 2D4A73858D35; Tue, 23 May 2023 10:34:53 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 2D4A73858D35 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1684838093; bh=exSUPrlMOC2XGIg8PC9kzTnZU0ElwSU/bPXGO+j3ZVY=; h=From:To:Subject:Date:From; b=H5PF7h4BbYR4QqY8pmJKMZ3cH2S++JqoY5GG8gfBb4e+sXxmjhriWxSneNGtoE1lH DCY07MKaXDmSOi6JEc1yLAZiWrS4B4bbHfeYPaaMyije49qP+/Ejo3XcmDBMWqv7+Y CDdF3DvvsXug6hkqVB2nHcHLHCkrQPqSHX2xSeyg= MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Type: text/plain; charset="utf-8" From: Richard Sandiford To: gcc-cvs@gcc.gnu.org Subject: [gcc r14-1130] md: Allow to refer to the value of int iterator FOO X-Act-Checkin: gcc X-Git-Author: Richard Sandiford X-Git-Refname: refs/heads/trunk X-Git-Oldrev: 51fd69ec2d57721ed502344ebe68b1643d9e7f34 X-Git-Newrev: b0ad9159a5ab35ba2cb41c273156216c2f305c0b Message-Id: <20230523103453.2D4A73858D35@sourceware.org> Date: Tue, 23 May 2023 10:34:53 +0000 (GMT) List-Id: https://gcc.gnu.org/g:b0ad9159a5ab35ba2cb41c273156216c2f305c0b commit r14-1130-gb0ad9159a5ab35ba2cb41c273156216c2f305c0b Author: Richard Sandiford Date: Tue May 23 11:34:41 2023 +0100 md: Allow to refer to the value of int iterator FOO 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 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 - the equivalent of Because of this ambiguity, it seemed better to stick to the current approach for modes. For codes it's less clear-cut, but and 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. gcc/ * doc/md.texi: Document that 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 expand to the current value of iterator FOO. (initialize_iterators): Set has_self_attr for int iterators. Diff: --- 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, 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,