This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: if/then/else hint ?


Running gcc 3.2.2 on i686

We have a situation where we have coded in a heavily used loop
that is critical for performance:


if (<condition>) <fastpath>; else <slowpath>;

<fastpath> is taken about 250 times for each time <slowpath> is
taken; however if <condition> was randomly distributed then
<slowpath> would be taken more often.  (This is the instruction
fetch piece of an emulator, <condition> is the instruction address
so we know that generally <condition> is not randomly distributed).

In addition to what everyone else has said, GCC can do a lot of this automatically for you using a feature called profiled feedback.

1) Compile your application using the flag "-fprofile-arcs" in addition
to your normal optimization options.
2) Run it and put it through its normal paces.  This will gather a bunch
of information about the execuatable.
3) Now recompile your application substituting the flag
"-fbranch-probabilities" for the "-fprofile-arcs" you added above.  This
tells the compiler to use the statistics that you just gathered to
optimize the expected branches taken.
4) You now have an self-optimized executable.

Profiled feedback has been greatly improved in under-development GCC
3.4, but in GCC 3.2 it works good enough for government work :)

Kelley Cook



Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]