compiler: create temporaries for heap variables

The compiler generally doesn't create a temporary for an expression
that is a variable, because it's normally valid to simply reload the
value from the variable.  However, if the variable is in the heap,
then loading the value is a pointer indirection.  The process of
creating GCC IR can cause the variable load and the pointer
indirection to be split, such that the second evaluation only does the
pointer indirection.  If there are conditionals in between the two
uses, this can cause the second use to load the pointer from an
uninitialized register.

Avoid this by introducing a new Expression method that returns whether
it is safe to evaluate an expression multiple times, and use it

The test case is

Fixes golang/go#44383

Change-Id: Ice2a18153457daa024ddbf0a9935f7863fe6818e
Trust: Ian Lance Taylor <>
Reviewed-by: Than McIntosh <>
Reviewed-by: Cherry Zhang <>
5 files changed