Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Optimization through code for gcc? [duplicate]

I am working on a project that relies on compiler optimisations but I need some code not to be optimised by GCC. Is this possible?

like image 383
limp Avatar asked Nov 23 '22 14:11

limp


2 Answers

GCC 4.4 has an attribute for that:

int foo(int i) __attribute__((optimize("-O3")));

It is documented at: https://gcc.gnu.org/onlinedocs/gcc-5.1.0/gcc/Function-Attributes.html#index-g_t_0040code_007boptimize_007d-function-attribute-3195

like image 56
sehe Avatar answered Nov 26 '22 03:11

sehe


GCC has since 4.4. the #pragma GCC optimize ("whatever"). I would also recommend to wrap the particular code, that is annotated with this pragma with #pragma GCC push_options and #pragma GCC pop_options. The first will save the options as they were before your change, the later will restore them afterwards and the rest of the code will compile with the global options.

For details on the whatever string, you should look into the gcc doc, here the most important part of it: Arguments can either be numbers or strings. Numbers are assumed to be an optimization level. Strings that begin with O are assumed to be an optimization option, while other options are assumed to be used with a -f prefix..

That means if you dont want any optimizations on your particular code your whatever should just be "0".

like image 21
flolo Avatar answered Nov 26 '22 04:11

flolo