loop: will this optimize?
Ian Lance Taylor
iant@google.com
Thu Apr 14 19:29:00 GMT 2011
pieniek <edek.pienkowski@gmail.com> writes:
> if a loop contains if-s on a constant boolean, will gcc create two
> loop versions, for the boolean being true and the boolean being false,
> if the boolean switches on some heavy code (in this case, costly
> debugging code)? The boolean in question being not a template param or
> not really const either, just constant during the loop run.
>
> I do understand that if the boolean is off (no heavy code) this does
> boils down to a couple of jumps not taken or taken, but, with these
> ifs removed, the loop can benefit from things like
> vectorisation. Right?
>
> So, the question rephrased, is there any benefit of templating some
> function containing a loop based on the bool value (or writing the
> code in both versions anyhow)? The goal would be to make the fast
> (call it release) version of the loop really fast.
This optimization is called loop unswitching. You can enable it in gcc
with the -funswitch-loops option. This option is turned on by -O3, but
not by -O2.
Ian
More information about the Gcc-help
mailing list