Skip to content

Commit 8b9c7fb

Browse files
compiler: for package-scope "a = b; b = x" just set "a = x"
This avoids requiring an init function to initialize the variable. This can only be done if x is a static initializer. The go1.15rc1 runtime package relies on this optimization. The package has a variable "var maxSearchAddr = maxOffAddr". The maxSearchAddr variable is used by code that runs before package initialization is complete. Change-Id: Ied19a6d06f44c0c3e69632960a85f8d32139095c Reviewed-on: https://go-review.googlesource.com/c/gofrontend/+/245098 Reviewed-by: Cherry Zhang <[email protected]>
1 parent e86f2cb commit 8b9c7fb

File tree

1 file changed

+30
-7
lines changed

1 file changed

+30
-7
lines changed

go/gogo.cc

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1622,16 +1622,31 @@ Gogo::write_globals()
16221622
// The initializer is constant if it is the zero-value of the
16231623
// variable's type or if the initial value is an immutable value
16241624
// that is not copied to the heap.
1625-
bool is_static_initializer = false;
1626-
if (var->init() == NULL)
1625+
Expression* init = var->init();
1626+
1627+
// If we see "a = b; b = x", and x is a static
1628+
// initializer, just set a to x.
1629+
while (init != NULL && init->var_expression() != NULL)
1630+
{
1631+
Named_object* ino = init->var_expression()->named_object();
1632+
if (!ino->is_variable() || ino->package() != NULL)
1633+
break;
1634+
Expression* ino_init = ino->var_value()->init();
1635+
if (ino->var_value()->has_pre_init()
1636+
|| ino_init == NULL
1637+
|| !ino_init->is_static_initializer())
1638+
break;
1639+
init = ino_init;
1640+
}
1641+
1642+
bool is_static_initializer;
1643+
if (init == NULL)
16271644
is_static_initializer = true;
16281645
else
16291646
{
16301647
Type* var_type = var->type();
1631-
Expression* init = var->init();
1632-
Expression* init_cast =
1633-
Expression::make_cast(var_type, init, var->location());
1634-
is_static_initializer = init_cast->is_static_initializer();
1648+
init = Expression::make_cast(var_type, init, var->location());
1649+
is_static_initializer = init->is_static_initializer();
16351650
}
16361651

16371652
// Non-constant variable initializations might need to create
@@ -1650,7 +1665,15 @@ Gogo::write_globals()
16501665
}
16511666
var_init_fn = init_fndecl;
16521667
}
1653-
Bexpression* var_binit = var->get_init(this, var_init_fn);
1668+
1669+
Bexpression* var_binit;
1670+
if (init == NULL)
1671+
var_binit = NULL;
1672+
else
1673+
{
1674+
Translate_context context(this, var_init_fn, NULL, NULL);
1675+
var_binit = init->get_backend(&context);
1676+
}
16541677

16551678
if (var_binit == NULL)
16561679
;

0 commit comments

Comments
 (0)