One particular point is: "In contrast, for -O0 the return value seems un-predictable. " <div><br></div><div>Not entirely true - and if you were writing this code to intentionally get a unpredictable value (to seed a random number generator etc) - that's a security problem (has been a bug in crypto libraries etc where they've used similar techniques and eventually the compiler breaks them - or people find ways to compromise the source of the data (by writing specific values to stack variables elsewhere in the program making the values more predictable)<br><br><div class="gmail_quote"><div dir="ltr">On Wed, Nov 21, 2018, 10:03 AM via cfe-dev <<a href="mailto:cfe-dev@lists.llvm.org">cfe-dev@lists.llvm.org</a> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">> IMHO the inconsistency makes a lot of additional effort and perhaps is<br>
> not preferred, but I guess that's eventually the programmer's<br>
> responsibility to solve that? <br>
<br>
Correct. The program has undefined behavior, and it is the programmer's<br>
responsibility to solve that. The Undefined Behavior Sanitizer would<br>
reveal the problem immediately.<br>
<br>
> uninitialized variables on the stack is assumed to be zero due to<br>
> some reason,<br>
<br>
That is not exactly what happened. The assignment is from uninitialized<br>
memory, which will have an unknown value. Because the value is unknown,<br>
the assignment can be optimized to avoid a read from memory, and<br>
substitute any convenient value, without perturbing any defined property<br>
of the program. The most convenient value to use here is zero.<br>
<br>
This is a different sequence of reasoning than what you suggested, which<br>
is more like this: The stack values are assumed to be zero, therefore <br>
we can use value propagation to assign the value zero instead of reading<br>
memory with a known value.<br>
<br>
I agree that the net effect here is the same, but the reasoning is<br>
important for correct understanding of the program's semantics.<br>
--paulr<br>
_______________________________________________<br>
cfe-dev mailing list<br>
<a href="mailto:cfe-dev@lists.llvm.org" target="_blank">cfe-dev@lists.llvm.org</a><br>
<a href="http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev" rel="noreferrer" target="_blank">http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev</a><br>
</blockquote></div></div>