This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
optimizing constant ifs out of loop?
- From: David Roundy <droundy at jdj5 dot mit dot edu>
- To: gcc-help at gcc dot gnu dot org
- Date: Tue, 30 Sep 2003 18:47:26 -0400
- Subject: optimizing constant ifs out of loop?
Hello. I have a question about how (and whether) I might be able to
convince g++ to move an if outside of some loops. The code I'm working on
is a numerical code with a very tight inner loop that I'm trying to
simultaneously clean up and optimise. Previously I had a number of copies
of the loop (which is actually several nested loops to loop over several
dimensions) for special cases. I'd like to have only one copy of the loop,
which would be a lot cleaner.
The code is (simplified a bit):
const bool have_plus = ...;
const bool have_minus = ...;
const bool have_pml_plus = ...;
const bool have_pml_minus = ...;
LOOP_OVER_OWNED(v, ind) { // Macro for three four loops
...pre
if (have_minus) {
if (have_pml_minus) {
...a
} else {
...b
}
}
if (have_plus) {
if (have_pml_plus) {
...c
} else {
...d
}
}
...post
}
*I* know that the compiler can move the ifs out of the loop to give:
if (have_minus && have_plus) {
if (have_pml_minus && have_pml_plus) {
LOOP_OVER_OWNED(v, ind) ...pre ...a ...c ...post
} else if (have_pml_minus) {
LOOP_OVER_OWNED(v, ind) ...pre ...a ...d ...post
} ... etc ...
but the compiler doesn't seem to know this. Unfortunately, the pretty
version of the code takes about twice as long, which is not acceptable.
I'm compiling with -funroll-loops -O3. Are there any other compiler flags
(or tricks) that I might be able to use to get the compiler to perform this
optimization? btw all the loops involved are simple 0..n-1 style loops with
n being known at runtime when the loop is entered.
Please CC me, as I'm not subscribed. Thanks!
--
David Roundy
http://abridgegame.org/darcs