On Mar 12, 1:59 pm, Robbert Krebbers <
newsgro...@robbertkrebbers.nl>
wrote:
> I am wondering what kind of actually used compiler optimizations take
> advantage of sequence point violations. Moreover, I am looking for
> example programs with sequence point violations that when compiled with
> an actual optimizing compiler (like gcc or clang) omit surprising results.
Here's a simple example:
An old and well-known method to evaluate an expression using the
minimum number of registers is to evaluate those sub-expressions first
that need more registers. If I want to evaluate (expr1) + (expr2), and
these two expressions need 2 resp. 3 registers to evaluate, then I
evaluate expr2 first using three registers, keep the result in one
register, use the other two to evaluate expr1, and add, so the total
evaluation used 3 registers. Evaluating expr1 first needs 4 registers:
One to hold the result of expr1, three to evaluate expr2.
Now let's say the expressions are (*p = something) + (*q =
somethingelse). If p and q are different pointers, then it doesn't
matter which side is evaluated first. If p and q are the same,
behaviour is undefined, and that allows the compiler to evaluate
whichever side it wants first, so the algorithm above can be used to
evaluate the whole expression using the minimal number of registers.
If behaviour was defined like in Java where the first assignment
happens first, and the second assignment happens second, the compiler
wouldn't have that freedom and might have to produce less good code if
the first subexpression needed more registers to evaluate than the
second.