On Wed, 22 Feb 2023 at 11:49, Richard Biener <rguent...@suse.de> wrote:
>
> On Wed, 22 Feb 2023, Jakub Jelinek wrote:
>
> > On Wed, Feb 22, 2023 at 09:52:06AM +0000, Richard Biener wrote:
> > > > The following testcase ICEs because we still have some spots that
> > > > treat BUILT_IN_UNREACHABLE specially but not BUILT_IN_UNREACHABLE_TRAP
> > > > the same.
> >
> > This patch uses (fndecl_built_in_p (node, BUILT_IN_UNREACHABLE)
> >                || fndecl_built_in_p (node, BUILT_IN_UNREACHABLE_TRAP))
> > a lot and from grepping around, we do something like that in lots of
> > other places, or in some spots instead as
> > (fndecl_built_in_p (node, BUILT_IN_NORMAL)
> >  && (DECL_FUNCTION_CODE (node) == BUILT_IN_WHATEVER1
> >      || DECL_FUNCTION_CODE (node) == BUILT_IN_WHATEVER2))
> > The following patch adds an overload for this case, so we can write
> > it in a shorter way.  It isn't worth for 3+, code in that case
> > typically uses the fndecl_built_in_p (node, BUILT_IN_NORMAL)
> > + switch in DECL_FUNCTION_CODE.
> >
> > If this isn't appropriate for GCC 13 (or not at all), I think we'll
> > need to change at least ipa-prop.cc because it suffers from the same
> > problem as the previous patch was fixing.
>
> Is it possible to use C++ (template) magic to expand the > 1 argument
> case to
>
>   if (fndecl_built_in_p (BUILT_IN_NORMA)
>       && (... || ... || ...
>
> lispy we'd expand to the head check and then recursively on the
> first and the remaining args.

In C++17 yes, there are fold expressions, so you'd write it as literally:

if (fndecl_built_in_p (BUILT_IN_NORMA)
      && (DECL_FUNCTION_CODE (node) == name || ...)

Where "name" is a parameter pack, and the "..." is literally what the
code would contain, not an abbreviation for the example :-)

For C++11 you can write it recursively. Something like:


// Single argument case terminates recursion.
inline bool
fndecl_built_in_matches_name_p (const_tree node, built_in_function name1)
{
  return DECL_FUNCTION_CODE (node) == name1;
}

// Recursive case. If names... is an empty pack then the overload above
// is a better match.
template<typename... Functions>
inline bool
fndecl_built_in_matches_name_p (const_tree node, built_in_function name1,
                Functions... names)
{
  return DECL_FUNCTION_CODE (node) == name1
           || fndecl_built_in_matches_name_p (node, names...);
}

// Call with one or more names.
template<typename... Functions>
inline bool
fndecl_built_in_p (const_tree node, built_in_function name1,
                Functions names...)
{
  return (fndecl_built_in_p (node, BUILT_IN_NORMAL)
       && fndecl_built_in_matches_name_p (node, name1, names...);
}

I think the "is a better match" comment is the status of C++ after a
DR, so might not actually be true in C++11 with GCC 4.8, I can check
that (and if needed, rewrite the recursive case to avoid the problem).

Reply via email to