[cfe-dev] Understand assumptions towards uninitialized variables on stack
Friedman, Eli via cfe-dev
cfe-dev at lists.llvm.org
Wed Nov 21 12:54:44 PST 2018
On 11/21/2018 8:58 AM, Stephan Bergmann via cfe-dev wrote:
> On 21/11/2018 16:03, via cfe-dev wrote:
>>> uninitialized variables on the stack is assumed to be zero due to
>>> some reason,
>>
>> That is not exactly what happened. The assignment is from uninitialized
>> memory, which will have an unknown value. Because the value is unknown,
>> the assignment can be optimized to avoid a read from memory, and
>> substitute any convenient value, without perturbing any defined property
>> of the program. The most convenient value to use here is zero.
>
> But why bother to come up with a specific value at all, why not drop
> the "xorl %eax, %eax" completely and use whatever value is present in
> %eax?
>
The optimizer isn't specifically trying to catch this case. It just runs
a series of transforms which assume that the behavior is defined, and
some of those transforms constrain the behavior of the function. This
eventually leads to generating an xor which wasn't necessary for the
original function. If you're curious about what happens in this
particular case, you can use "-mllvm -print-after-all" to see how
various transforms change the IR.
-Eli
--
Employee of Qualcomm Innovation Center, Inc.
Qualcomm Innovation Center, Inc. is a member of Code Aurora Forum, a Linux Foundation Collaborative Project
More information about the cfe-dev
mailing list