Can different optimization levels lead to functionally different code?

前端 未结 13 1853
[愿得一人]
[愿得一人] 2020-12-13 12:39

I am curious about the liberties that a compiler has when optimizing. Let\'s limit this question to GCC and C/C++ (any version, any flavour of standard):

Is it possi

13条回答
  •  时光说笑
    2020-12-13 13:18

    For C, almost all operations are strictly defined in the abstract machine and optimizations are only allowed if the observable result is exactly that of that abstract machine. Exceptions of that rule that come to mind:

    • undefined behavior don't has to be consistent between different compiler runs or executions of the faulty code
    • floating point operations may cause different rounding
    • arguments to function calls can be evaluated in any order
    • expressions with volatile qualified type may or may not be evaluated just for their side effects
    • identical const qualified compound literals may or may be not folded into one static memory location

提交回复
热议问题