Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Determine optimization level in preprocessor?

Tags:

-Og is a relatively new optimization option that is intended to improve the debugging experience while apply optimizations. If a user selects -Og, then I'd like my source files to activate alternate code paths to enhance the debugging experience. GCC offers the __OPTIMIZE__ preprocessor macro, but its only set to 1 when optimizations are in effect.

Is there a way to learn the optimization level, like -O1, -O3 or -Og, for use with the preprocessor?

like image 940
jww Avatar asked Jul 30 '15 08:07

jww


People also ask

What is optimization level?

The degree to which the compiler will optimize the code it generates is controlled by the -O flag. No optimization. In the absence of any version of the -O flag, the compiler generates straightforward code with no instruction reordering or other attempt at performance improvement. -O or -O2.

What are the optimization levels in GCC?

-O1 (optimize minimally) -O2 (optimize more) -O3 (optimize even more) -Ofast (optimize very aggressively to the point of breaking standard compliance)

How many levels of optimization are there?

6.4 Optimization levels. In order to control compilation-time and compiler memory usage, and the trade-offs between speed and space for the resulting executable, GCC provides a range of general optimization levels, numbered from 0--3, as well as individual options for specific types of optimization.

What are optimization flags?

Turning on optimization flags makes the compiler attempt to improve the performance and/or code size at the expense of compilation time and possibly the ability to debug the program. The compiler performs optimization based on the knowledge it has of the program.


1 Answers

I don't know if this is clever hack, but it is a hack.

$ gcc -Xpreprocessor -dM -E - < /dev/null > 1 $ gcc -Xpreprocessor -dM -O -E - < /dev/null > 2 $ diff 1 2 53a54 > #define __OPTIMIZE__ 1 68a70 > #define _FORTIFY_SOURCE 2 154d155 < #define __NO_INLINE__ 1 

clang didn't produce the FORTIFY one.

like image 91
mevets Avatar answered Feb 03 '23 11:02

mevets