c++ - Sequence points, conditionals and optimizations -
i had argument today 1 of collegues regarding fact compiler change semantics of program when agressive optimizations enabled.
my collegue states when optimizations enabled, compiler might change order of instructions. that:
function foo(int a, int b) { if (a > 5) { if (b < 6) { // } } }
might changed to:
function foo(int a, int b) { if (b < 6) { if (a > 5) { // } } }
of course, in case, doesn't change program general behavior , isn't really important.
from understanding, believe 2 if (condition)
belong 2 different sequence points , compiler can't change order, if changing keep same general behavior.
so, dear users, truth regarding ?
if compiler can verify there no observable difference between two, free make such optimizations.
sequence points conceptual thing: compiler has generate code such behaves as if semantic rules sequence points followed. generated code doesn't have follow rules if not following them produces no observable difference in behavior of program.
even if had:
if (a > 5 && b < 6)
the compiler freely rearrange be
if (b < 6 && > 5)
because there no observable difference between 2 (in specific case a
, b
both int
values). [this assumes safe read both a
, b
; if reading 1 of them cause error (e.g., 1 has trap value), compiler more restricted in optimizations make.]
Comments
Post a Comment