> int x = cond ? 2 : y; /* Is it legal to fold this to int x = 2; ? */
This is permissible (on typical hardware) only if the implementation chooses to 'initialize' y to 2 (possibly skipping the actual write if it is later overwritten), since doing otherwise would result in: int x1 = cond1 ? 2 : y; /* So int x1 = 2; */
int x2 = cond2 ? 3 : y; /* So int x2 = 3; */
No, no it may not do that.Edit1: actually, as vyodaiken points out, that's only valid in the first place if y is auto, not static.
> Can this be lowered to int y = x;
Sure; the compiler is free to implement dereference in a non-trapping manner if possible.
> No one uses the result of the load, can I delete it?
Yes, it's not volatile.
> Can I hoist the load out of the loop?
I don't think so; foo might write to z.
> Note that all of the optimizations I'm alluding to here are ones that would have existed all the way back in the 1980s when C was being standardized, and these are pretty basic, pedestrian optimizations that you will cover in Compilers 101.
Yep, some of the proposed optimizations (x1x2 and hoist) aren't actually available, but they all seem like reasonable things to consider.
Edit, missed these:
> A function marked _Noreturn returns.
If written in C, the body of the function must include a return statement, and is thus a compile time error. If written in assembly, this is no different than overwriting your return address via buffer overflow and ending up in the middle of a function. The compiler should probably stick a trap instruction after the call instruction, though.
> Or you alias two pointers marked restrict.
`restrict` (formerly noalias) is not valid C[0] (or any language), no matter what the standard says; the compiler must emit a compile time error.