[PATCH]: RTL iterators

Daniel Berlin dan@cgsoftware.com
Thu Aug 9 21:15:00 GMT 2001


Richard Henderson <rth@redhat.com> writes:

> On Thu, Aug 09, 2001 at 10:06:30PM -0400, Daniel Berlin wrote:
> > The reason for this is not because for_each_rtx doesn't exist, since
>> it does.  So it must be something else.
>
> Incorrect reasoning.  Most of this code pre-dates for_each_rtx.
> When for_each_rtx was added, we did not go back and change all
> of the old code.

I would argue this is not true.
for_each_rtx is from August, 1998.
Since then, the only place it's been used is the SSA stuff.
Plenty of new code, like the load and store motion, use the same old
"copy and paste an iteration function" style.
I certainly don't use for_each_rtx, because i don't want to write twice as many
functions (if i'm iterating through rtl in a BB, calling a function,
using for_each_rtx doubles the number of callbacks i need).
>
>> This way, you can just say "Give me the next x" where x is something
>> you care about.
>
> I can't see how "next" is by itself meaningful in any way.
You've taken it out of context, of course.
You missed there's a cookie, which it uses to give you the next x in
the piece of RTL you originally started with, after whatever you last
got.

I.E.
(set (reg:SI a) (reg:SI b))
get_next_REG would first hand you (reg:SI a), then (reg:SI b), then
NULL.
or for
a parallel with multiple sets,
get_next_SET would hand you each set in the parallel, in turn.

>of the time we need to know whether "X" is read, set, or clobbered.
while ((rtx = get_next_SET (insn, cookie)) != NULL)
{
        if (SET_DEST (rtx) == X)
           /* It's written */
        else if (SET_SRC (rtx) == X)
           /* It's read */     
}
(Best I can do, or anyone could do, without knowing what *type* of X
you are looking for.)
  
         
> We also sometimes care about "operands" not "mems" or "regs".
And for these times, there's one to give you the next operand.

for_each_rtx is too general for quite a lot of cases, and requiring a
callback just makes it less useful.
I don't want to write 4 callbacks for the four things i want to do,
and still have to check the code on each thing, determine if i want
subtree iteration, etc.

If you find the iterators not useful, don't use them.
I'm pretty sure a lot of people will, which is why i submitted them.
Iterators are quite useful in a lot of cases. 

Just like for_each isn't used all that much in C++, because it
requires a callback to be useful, while for loops going from .begin()
to .end() are used all the time.
Why?
Because it's easier, and there is nothing better about writing tons of
small callbacks.
--Dan

>
>
> r~

-- 
"I was once arrested for walking in someone else's sleep.
"-Steven Wright



More information about the Gcc-patches mailing list