Skip to content

Commit

Permalink
Minor improvement to genpreds.cc
Browse files Browse the repository at this point in the history
This simple patch implements Richard Biener's suggestion in comment gcc-mirror#6
of PR tree-optimization/52171 (from February 2013) that the insn-preds
code generated by genpreds can avoid using strncmp when matching constant
strings of length one.

The effect of this patch is best explained by the diff of insn-preds.cc:
<       if (!strncmp (str + 1, "g", 1))
---
>       if (str[1] == 'g')
3104c3104
<       if (!strncmp (str + 1, "m", 1))
---
>       if (str[1] == 'm')
3106c3106
<       if (!strncmp (str + 1, "c", 1))
---
>       if (str[1] == 'c')
...

The equivalent optimization is performed by GCC (but perhaps not by the
host compiler), but generating simpler/smaller code may encourage further
optimizations (such as use of a switch statement).

2022-05-24  Roger Sayle  <roger@nextmovesoftware.com>

gcc/ChangeLog
	* genpreds.cc (write_lookup_constraint_1): Avoid generating a call
	to strncmp for strings of length one.
  • Loading branch information
rogersayle committed May 24, 2022
1 parent d0ef9e0 commit 9e7a0e4
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions gcc/genpreds.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1089,10 +1089,15 @@ write_lookup_constraint_1 (void)
{
do
{
printf (" if (!strncmp (str + 1, \"%s\", %lu))\n"
" return CONSTRAINT_%s;\n",
c->name + 1, (unsigned long int) c->namelen - 1,
c->c_name);
if (c->namelen > 2)
printf (" if (!strncmp (str + 1, \"%s\", %lu))\n"
" return CONSTRAINT_%s;\n",
c->name + 1, (unsigned long int) c->namelen - 1,
c->c_name);
else
printf (" if (str[1] == '%c')\n"
" return CONSTRAINT_%s;\n",
c->name[1], c->c_name);
c = c->next_this_letter;
}
while (c);
Expand Down

0 comments on commit 9e7a0e4

Please sign in to comment.