In this question, I meet the situation that gcc myfile.c -S
produce the assembly code that is better than gcc myfile.c -O0
but worse than gcc myfile.c -O1
.
At -O0, both loops are generated. At -O1, both loops are optimized out. (Thanks @Raymond Chen for reminder. cited from his comments) (using the -S just optimize one loop out)
I search the Internet and only find this:
-S
(cited from Overall options)
Stop after the stage of compilation proper; do not assemble. The output is in the form of an assembler code file for each non-assembler input file specified.
By default, the assembler file name for a source file is made by replacing the suffix ‘.c’, ‘.i’, etc., with ‘.s’.
Input files that don't require compilation are ignored.
So my question is:
what is exactly the optimization level of
-S
option when it compile file? (-O0.5
?)why not just using the
-O0
or-O1
... (or it is a bug?)
Edit: you can use this site to help you reproduce the problem. Code is in the question I mentioned. ( If you just use -S compiler option(or no compiler option), you can get one loop elision. )
step 1:
Open this site and copy the following code in Code Eidtor.
#include <stdio.h>
int main (int argc, char *argv[]) {
unsigned int j = 10;
for (; j > -1; --j) {
printf("%u", j);
}
}
step 2:
Choose g++ 4.8
as compiler. Compiler option is empty.(or -S)
step 3:
You get the first situation. Now, change the j > -1
to j >= -1
and you can see the second.