This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
Re: if/then/else hint ?
- From: Kelley Cook <kcook34 at ford dot com>
- To: gcc at gcc dot gnu dot org
- Date: Thu, 08 Jan 2004 15:06:08 -0500
- Subject: Re: if/then/else hint ?
- Hop-count: 1
- References: <3FFD9840.9030000@trex.rtpnc.epa.gov>
- Reply-to: Kelley Cook <kelleycook at yahoo dot com>
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