0

I've got a highly optimized compiled C++ object file (compiled with g++, specifying -O3 -g -march=amdfam10 -Wall) with debug info.

I'm using objdump -S "objname".

Unfortunately interleaving the source doesn't seem to work, because sometimes I see the same group of lines (not just a single line) repeated, and not just one code line but multiple of them, many times, followed by only one assembly line, then other 3/4 source lines, without making much sense.

I see for example, 3/4 C++ code lines with iterators, and maps initializations followed by only 1/2 ASM lines? Does it make sense?

Any idea what might be happening?

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Emanuele
  • 1,408
  • 1
  • 15
  • 39

1 Answers1

3

Optimized code (especially with the aggressive optimizations turned on by O3) no longer has a direct correspondence with the source. For instance, the compiler will often eliminate variables since the value doesn't ever need to be stored in memory, only in registers. It will also reorder operations so that they are faster. For instance, a simple optimization is to turn a conditional inside a loop into a conditional that chooses between two different loops, turning something like this

while(1){
    if(foo){
        bar();
    } else {
        baz();
    }
}

into something like this

if(foo){
    while(1){
        bar();
    }
} else {
    while(1){
        baz();
    }
}

which is equivalent, but avoids making the comparison on each iteration.

If you want to be able to see a direct correspondence with the source code, O1 is about as high as you can go.

Dirk Holsopple
  • 8,731
  • 1
  • 24
  • 37
  • So do you think is not even worth it trying to analyze the code with _-O3_? Is it basically a pointless activity? – Emanuele Aug 03 '12 at 20:01
  • You can do it, but it's much more difficult since you can't directly compare it to the source code. It takes a better understanding of assembly to do it. I would only do it if you have a bug that only occurs in the O3 build or maybe if you need to optimize for performance (this will be a secondary tool, a profiler should be your main tool for this). – Dirk Holsopple Aug 03 '12 at 20:14
  • FYI I'm already using _-pg_ and _valgrind_ to simulate the execution and profiling. – Emanuele Aug 04 '12 at 06:57
  • Given the answers/discussions above I guess the answer to this question should be _basically useless_ given the optimization level. Right? – Emanuele Aug 09 '12 at 12:58