This is the mail archive of the gcc-help@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]

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


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