How to avoid code elimination

David Brown david@westcontrol.com
Mon Nov 27 07:48:00 GMT 2017


On 26/11/17 14:19, Oleg Endo wrote:
> On Sun, 2017-11-26 at 12:08 +0100, phi gcc wrote:
>> .
>> .
>> if(cond)
>> { e=e;
>> }
>> .
>> ....
>>  
>> I tried to compile with -O0 but to no joy gcc always remove my 'NOP'
>> intruction knowing better than me what I could do with it.
>>
>> Is there any other idioms I should use, may be an asm("nop") or the
>> like I guess ?
> 
> Try declaring e "volatile".
> 

"volatile" is the way to do it.  It will work regardless of optimisation
levels.

Just remember that other code can still be optimised, and moved back and
forth around the volatile access.  -O0 is, IMHO, often painful for
debugging - the code produced is too big and complicated if you like to
look at the generated assembly.  I like -O1 or -Og for debugging - it is
a better compromise of decent code but little re-ordering.




More information about the Gcc-help mailing list