This is the mail archive of the
gcc-patches@gcc.gnu.org
mailing list for the GCC project.
Re: -ftree-check for review
On Wednesday 18 April 2007 18:27, Joe Buck wrote:
> I wrote:
> > >You write: "Note that patterns are tried only on the top level of a
> > >statement." And later you write "for example the statement pattern
> > >
> > > @code{"gets (%_)" or "%_ = gets (%_)"}
> > >
> > >matches any call to
> > > @code{gets ()}.
> > >
> > >But the gets result might be passed straight to another function:
> > >
> > > f(gets(arglist));
> > >
> > >presumably the rule would not match in this case, right?
>
> On Wed, Apr 18, 2007 at 10:00:35AM -0400, Sebastian Pop wrote:
> > This should be matched as we're doing the pattern matching at the
> > gimple level.
> > The documentation should be improved to be more precise.
>
> OK. Unfortunately it's a bit tricky for the user who doesn't know GCC
> internals to understand the consequences of the fact that the matching
> is being run on gimple, and might wonder why a pattern with two operators
> never matches.
Oh, it does!
I guess you are saying that because we are matching Gimple code, a pattern
which corresponds to non-gimplified code, such as "%X=%Y*%F+%Z", would never
match. Right?
In fact, such a pattern matches very well. Consider the following program,
called c.c:
#define N 10
void kix_o_main() {
int a[N], i, j, k;
a[i] = a[i] * k + j;
return;
}
And let's check it for the above pattern:
[nic@paco C]$ ~/Gcc/graphite/inst/bin/gcc --tree-check="%X=%Y*%F+%Z"
--tree-checks-verbose -c c.c
c.c: In function âkix_o_mainâ:
c.c:6: warning: user-defined warning %X=%Y*%F+%Z: .
c.c:6: instance = {F <- k, X <- a[i.0], Y <- D.1630, Z <- j},
c.c:6: reached: a[i.0] = D.1632.
Confused? Well, that's the fancy thing about my matcher: it works on Gimple
(and therefore you can catch all cases by considering only the Gimple cases),
but it *inlines on-demand* temporaries in Gimple statements. Let's see the
Gimple form for the only statement in the program:
[nic@paco C]$ ~/Gcc/graphite/inst/bin/gcc --dump-tree-gimple -c c.c
and here is the relevant excerpt from c.c.004t.gimple:
i.0 = i;
i.1 = i;
D.1630 = a[i.1];
D.1631 = D.1630 * k;
D.1632 = D.1631 + j;
a[i.0] = D.1632;
Now, when matching "%X=%Y*%F+%Z" with the last statement (all the other
statements will fail, which is what anyone would expect), it will easily
assign X -> a[i.0], match the "=" token, and see notice that D.1632 is a
temporary, that cannot be directly matched with the remaining expression. It
therefore will (conceptually) inline the temporary as (D.1631 + j), and
continue the match. Step by step, all the original statement will be matched.
Note that this temporary inlining is performed only when required by the
pattern. For instance, the following check will catch D.1631 without inlining
it further:
[nic@paco C]$ ~/Gcc/graphite/inst/bin/gcc --tree-check="%X=%Y+%Z"
--tree-checks-verbose -c c.c
c.c: In function âkix_o_mainâ:
c.c:6: warning: user-defined warning %X=%Y+%Z: .
c.c:6: instance = {X <- a[i.0], Y <- D.1631, Z <- j},
c.c:6: reached: a[i.0] = D.1632.
It's rather unusual (I've written two papers about it), but it works.
Now, it's true that the user has to pay for this fancyness: (s)he sometimes
has to dump the Gimple form a few times, to see what it looks like, and maybe
design the patterns in a gimple-concious way.
... But there's an even bigger reward on that: the matching also works on
(gimpelized) C++ programs, with no change! This is not yet documented,
because I just noticed this fact yesterday!!
Nic.