This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
Re: How to detect whether we're inside of a loop??
> From: Geoff Keating <geoffk at geoffk dot org>
>
> "Kaveh R. Ghazi" <ghazi at caip dot rutgers dot edu> writes:
>
> > I was interested in experimenting with heuristics which might expand a
> > builtin depending on whether at the point of expansion we're in a loop
> > or not. E.g. It might make sense to expand something which might
> > create slightly larger code only if we think it's likely that it'll be
> > executed a number of times. I'm guessing that being inside a loop is
> > one way to increase that probability. Thus I was curious if there's a
> > way to detect this.
>
> Why don't you just use the support that GCC already has for detecting
> the probability that a block will be executed?
Well first, I don't know how to do that. :-)
But secondly, my limited understanding of probability detection leads
me to believe that using it would answer a different question than the
one I'm posing. E.g. given this code fragment:
{
<code1>
if (foo)
<code2>
else
{
<code3>
exit(1);
}
for (i=0; i<x; i++)
<code4>
}
we know that <code1> is executed once. And we can say that <code2>
has a higher probability of being executed that <code3>, but again it
would be once. It's <code4> that is likely to be executed many times
(on average) for each pass through the fragment and therefore it is
most suitable for an expensive optimization.
I think your approach would be suited for finding which of the if-else
branches to choose. Finding the looping spots is what I'm looking
for because I think it has more impact.
Do you agree with this line of reasoning?
--Kaveh
--
Kaveh R. Ghazi ghazi at caip dot rutgers dot edu