1 // wb.cc -- Add write barriers as needed.
3 // Copyright 2017 The Go Authors. All rights reserved.
4 // Use of this source code is governed by a BSD-style
5 // license that can be found in the LICENSE file.
10 #include "go-diagnostics.h"
14 #include "expressions.h"
15 #include "statements.h"
19 // Mark variables whose addresses are taken. This has to be done
20 // before the write barrier pass and after the escape analysis pass.
21 // It would be nice to do this elsewhere but there isn't an obvious
24 class Mark_address_taken
: public Traverse
27 Mark_address_taken(Gogo
* gogo
)
28 : Traverse(traverse_expressions
),
33 expression(Expression
**);
39 // Mark variable addresses taken.
42 Mark_address_taken::expression(Expression
** pexpr
)
44 Expression
* expr
= *pexpr
;
45 Unary_expression
* ue
= expr
->unary_expression();
47 ue
->check_operand_address_taken(this->gogo_
);
49 Array_index_expression
* aie
= expr
->array_index_expression();
52 && !aie
->array()->type()->is_slice_type())
54 // Slice of an array. The escape analysis models this with
55 // a child Node representing the address of the array.
57 Node
* n
= Node::make_node(expr
);
58 if (n
->child() == NULL
59 || (n
->child()->encoding() & ESCAPE_MASK
) != Node::ESCAPE_NONE
)
61 aie
->array()->address_taken(escapes
);
64 if (expr
->allocation_expression() != NULL
)
66 Node
* n
= Node::make_node(expr
);
67 if ((n
->encoding() & ESCAPE_MASK
) == Node::ESCAPE_NONE
)
68 expr
->allocation_expression()->set_allocate_on_stack();
70 if (expr
->heap_expression() != NULL
)
72 Node
* n
= Node::make_node(expr
);
73 if ((n
->encoding() & ESCAPE_MASK
) == Node::ESCAPE_NONE
)
74 expr
->heap_expression()->set_allocate_on_stack();
76 if (expr
->slice_literal() != NULL
)
78 Node
* n
= Node::make_node(expr
);
79 if ((n
->encoding() & ESCAPE_MASK
) == Node::ESCAPE_NONE
)
80 expr
->slice_literal()->set_storage_does_not_escape();
83 // Rewrite non-escaping makeslice with constant size to stack allocation.
84 Unsafe_type_conversion_expression
* uce
=
85 expr
->unsafe_conversion_expression();
87 && uce
->type()->is_slice_type()
88 && Node::make_node(uce
->expr())->encoding() == Node::ESCAPE_NONE
89 && uce
->expr()->call_expression() != NULL
)
91 Call_expression
* call
= uce
->expr()->call_expression();
92 if (call
->fn()->func_expression() != NULL
93 && call
->fn()->func_expression()->runtime_code() == Runtime::MAKESLICE
)
95 Expression
* len_arg
= call
->args()->at(1);
96 Expression
* cap_arg
= call
->args()->at(2);
97 Numeric_constant nclen
;
98 Numeric_constant nccap
;
101 if (len_arg
->numeric_constant_value(&nclen
)
102 && cap_arg
->numeric_constant_value(&nccap
)
103 && nclen
.to_unsigned_long(&vlen
) == Numeric_constant::NC_UL_VALID
104 && nccap
.to_unsigned_long(&vcap
) == Numeric_constant::NC_UL_VALID
)
106 // Turn it into a slice expression of an addressable array,
107 // which is allocated on stack.
108 Location loc
= expr
->location();
109 Type
* elmt_type
= expr
->type()->array_type()->element_type();
110 Expression
* len_expr
=
111 Expression::make_integer_ul(vcap
, cap_arg
->type(), loc
);
112 Type
* array_type
= Type::make_array_type(elmt_type
, len_expr
);
113 Expression
* alloc
= Expression::make_allocation(array_type
, loc
);
114 alloc
->allocation_expression()->set_allocate_on_stack();
115 Expression
* array
= Expression::make_unary(OPERATOR_MULT
, alloc
, loc
);
116 Expression
* zero
= Expression::make_integer_ul(0, len_arg
->type(), loc
);
118 Expression::make_array_index(array
, zero
, len_arg
, cap_arg
, loc
);
123 return TRAVERSE_CONTINUE
;
126 // Check variables and closures do not escape when compiling runtime.
128 class Check_escape
: public Traverse
131 Check_escape(Gogo
* gogo
)
132 : Traverse(traverse_expressions
| traverse_variables
),
137 expression(Expression
**);
140 variable(Named_object
*);
147 Check_escape::variable(Named_object
* no
)
149 if ((no
->is_variable() && no
->var_value()->is_in_heap())
150 || (no
->is_result_variable()
151 && no
->result_var_value()->is_in_heap()))
152 go_error_at(no
->location(),
153 "%s escapes to heap, not allowed in runtime",
154 no
->message_name().c_str());
155 return TRAVERSE_CONTINUE
;
159 Check_escape::expression(Expression
** pexpr
)
161 Expression
* expr
= *pexpr
;
162 Func_expression
* fe
= expr
->func_expression();
163 if (fe
!= NULL
&& fe
->closure() != NULL
)
165 Node
* n
= Node::make_node(expr
);
166 if (n
->encoding() == Node::ESCAPE_HEAP
)
167 go_error_at(expr
->location(),
168 "heap-allocated closure, not allowed in runtime");
170 return TRAVERSE_CONTINUE
;
173 // Add write barriers to the IR. This are required by the concurrent
174 // garbage collector. A write barrier is needed for any write of a
175 // pointer into memory controlled by the garbage collector. Write
176 // barriers are not required for writes to local variables that live
177 // on the stack. Write barriers are only required when the runtime
178 // enables them, which can be checked using a run time check on
179 // runtime.writeBarrier.enabled.
181 // Essentially, for each assignment A = B, where A is or contains a
182 // pointer, and where A is not, or at any rate may not be, a stack
183 // variable, we rewrite it into
184 // if runtime.writeBarrier.enabled {
185 // typedmemmove(typeof(A), &A, &B)
190 // The test of runtime.writeBarrier.Enabled is implemented by treating
191 // the variable as a *uint32, and testing *runtime.writeBarrier != 0.
192 // This is compatible with the definition in the runtime package.
194 // For types that are pointer shared (pointers, maps, chans, funcs),
195 // we replaced the call to typedmemmove with writebarrierptr(&A, B).
196 // As far as the GC is concerned, all pointers are the same, so it
197 // doesn't need the type descriptor.
199 // There are possible optimizations that are not implemented.
201 // runtime.writeBarrier can only change when the goroutine is
202 // preempted, which in practice means when a call is made into the
203 // runtime package, so we could optimize by only testing it once
204 // between function calls.
206 // A slice could be handled with a call to writebarrierptr plus two
209 // Traverse the IR adding write barriers.
211 class Write_barriers
: public Traverse
214 Write_barriers(Gogo
* gogo
)
215 : Traverse(traverse_functions
| traverse_variables
| traverse_statements
),
216 gogo_(gogo
), function_(NULL
)
220 function(Named_object
*);
223 variable(Named_object
*);
226 statement(Block
*, size_t* pindex
, Statement
*);
235 // Traverse a function. Just record it for later.
238 Write_barriers::function(Named_object
* no
)
240 go_assert(this->function_
== NULL
);
241 this->function_
= no
->func_value();
242 int t
= this->function_
->traverse(this);
243 this->function_
= NULL
;
245 if (t
== TRAVERSE_EXIT
)
247 return TRAVERSE_SKIP_COMPONENTS
;
250 // Insert write barriers for a global variable: ensure that variable
251 // initialization is handled correctly. This is rarely needed, since
252 // we currently don't enable background GC until after all global
253 // variables are initialized. But we do need this if an init function
257 Write_barriers::variable(Named_object
* no
)
259 // We handle local variables in the variable declaration statement.
260 // We only have to handle global variables here.
261 if (!no
->is_variable())
262 return TRAVERSE_CONTINUE
;
263 Variable
* var
= no
->var_value();
264 if (!var
->is_global())
265 return TRAVERSE_CONTINUE
;
267 // Nothing to do if there is no initializer.
268 Expression
* init
= var
->init();
270 return TRAVERSE_CONTINUE
;
272 // Nothing to do for variables that do not contain any pointers.
273 if (!var
->type()->has_pointer())
274 return TRAVERSE_CONTINUE
;
276 // Nothing to do if the initializer is static.
277 init
= Expression::make_cast(var
->type(), init
, var
->location());
278 if (!var
->has_pre_init() && init
->is_static_initializer())
279 return TRAVERSE_CONTINUE
;
281 // Nothing to do for a type that can not be in the heap, or a
282 // pointer to a type that can not be in the heap.
283 if (!var
->type()->in_heap())
284 return TRAVERSE_CONTINUE
;
285 if (var
->type()->points_to() != NULL
&& !var
->type()->points_to()->in_heap())
286 return TRAVERSE_CONTINUE
;
288 // Otherwise change the initializer into a pre_init assignment
289 // statement with a write barrier.
291 // We can't check for a dependency of the variable on itself after
292 // we make this change, because the preinit statement will always
293 // depend on the variable (since it assigns to it). So check for a
294 // self-dependency now.
295 this->gogo_
->check_self_dep(no
);
297 // Replace the initializer.
298 Location loc
= init
->location();
299 Expression
* ref
= Expression::make_var_reference(no
, loc
);
301 Statement_inserter
inserter(this->gogo_
, var
);
302 Statement
* s
= this->gogo_
->assign_with_write_barrier(NULL
, NULL
, &inserter
,
305 var
->add_preinit_statement(this->gogo_
, s
);
308 return TRAVERSE_CONTINUE
;
311 // Insert write barriers for statements.
314 Write_barriers::statement(Block
* block
, size_t* pindex
, Statement
* s
)
316 switch (s
->classification())
321 case Statement::STATEMENT_VARIABLE_DECLARATION
:
323 Variable_declaration_statement
* vds
=
324 s
->variable_declaration_statement();
325 Named_object
* no
= vds
->var();
326 Variable
* var
= no
->var_value();
328 // We may need to emit a write barrier for the initialization
331 // Nothing to do for a variable with no initializer.
332 Expression
* init
= var
->init();
336 // Nothing to do if the variable is not in the heap. Only
337 // local variables get declaration statements, and local
338 // variables on the stack do not require write barriers.
339 if (!var
->is_in_heap())
342 // Nothing to do if the variable does not contain any pointers.
343 if (!var
->type()->has_pointer())
346 // Nothing to do for a type that can not be in the heap, or a
347 // pointer to a type that can not be in the heap.
348 if (!var
->type()->in_heap())
350 if (var
->type()->points_to() != NULL
351 && !var
->type()->points_to()->in_heap())
354 // Otherwise initialize the variable with a write barrier.
356 Function
* function
= this->function_
;
357 Location loc
= init
->location();
358 Statement_inserter
inserter(block
, pindex
);
360 // Insert the variable declaration statement with no
361 // initializer, so that the variable exists.
365 // Create a statement that initializes the variable with a
367 Expression
* ref
= Expression::make_var_reference(no
, loc
);
368 Statement
* assign
= this->gogo_
->assign_with_write_barrier(function
,
374 // Replace the old variable declaration statement with the new
376 block
->replace_statement(*pindex
, assign
);
380 case Statement::STATEMENT_ASSIGNMENT
:
382 Assignment_statement
* as
= s
->assignment_statement();
383 Expression
* lhs
= as
->lhs();
384 Expression
* rhs
= as
->rhs();
386 // We may need to emit a write barrier for the assignment.
388 if (!this->gogo_
->assign_needs_write_barrier(lhs
))
391 // Change the assignment to use a write barrier.
392 Function
* function
= this->function_
;
393 Location loc
= as
->location();
394 Statement_inserter inserter
= Statement_inserter(block
, pindex
);
395 Statement
* assign
= this->gogo_
->assign_with_write_barrier(function
,
400 block
->replace_statement(*pindex
, assign
);
405 return TRAVERSE_CONTINUE
;
408 // The write barrier pass.
411 Gogo::add_write_barriers()
416 Mark_address_taken
mat(this);
417 this->traverse(&mat
);
419 if (this->compiling_runtime() && this->package_name() == "runtime")
421 Check_escape
chk(this);
422 this->traverse(&chk
);
425 Write_barriers
wb(this);
429 // Return the runtime.writeBarrier variable.
432 Gogo::write_barrier_variable()
434 static Named_object
* write_barrier_var
;
435 if (write_barrier_var
== NULL
)
437 Location bloc
= Linemap::predeclared_location();
439 // We pretend that writeBarrier is a uint32, so that we do a
440 // 32-bit load. That is what the gc toolchain does.
441 Type
* uint32_type
= Type::lookup_integer_type("uint32");
442 Variable
* var
= new Variable(uint32_type
, NULL
, true, false, false,
446 Package
* package
= this->add_imported_package("runtime", "_", false,
447 "runtime", "runtime",
448 bloc
, &add_to_globals
);
449 write_barrier_var
= Named_object::make_variable("writeBarrier",
453 return write_barrier_var
;
456 // Return whether an assignment that sets LHS needs a write barrier.
459 Gogo::assign_needs_write_barrier(Expression
* lhs
)
461 // Nothing to do if the variable does not contain any pointers.
462 if (!lhs
->type()->has_pointer())
465 // An assignment to a field is handled like an assignment to the
469 // Nothing to do for a type that can not be in the heap, or a
470 // pointer to a type that can not be in the heap. We check this
471 // at each level of a struct.
472 if (!lhs
->type()->in_heap())
474 if (lhs
->type()->points_to() != NULL
475 && !lhs
->type()->points_to()->in_heap())
478 Field_reference_expression
* fre
= lhs
->field_reference_expression();
484 // Nothing to do for an assignment to a temporary.
485 if (lhs
->temporary_reference_expression() != NULL
)
488 // Nothing to do for an assignment to a sink.
489 if (lhs
->is_sink_expression())
492 // Nothing to do for an assignment to a local variable that is not
494 Var_expression
* ve
= lhs
->var_expression();
497 Named_object
* no
= ve
->named_object();
498 if (no
->is_variable())
500 Variable
* var
= no
->var_value();
501 if (!var
->is_global() && !var
->is_in_heap())
504 else if (no
->is_result_variable())
506 Result_variable
* rvar
= no
->result_var_value();
507 if (!rvar
->is_in_heap())
512 // For a struct assignment, we don't need a write barrier if all the
513 // pointer types can not be in the heap.
514 Struct_type
* st
= lhs
->type()->struct_type();
517 bool in_heap
= false;
518 const Struct_field_list
* fields
= st
->fields();
519 for (Struct_field_list::const_iterator p
= fields
->begin();
523 Type
* ft
= p
->type();
524 if (!ft
->has_pointer())
528 if (ft
->points_to() != NULL
&& !ft
->points_to()->in_heap())
537 // Write barrier needed in other cases.
541 // Return a statement that sets LHS to RHS using a write barrier.
542 // ENCLOSING is the enclosing block.
545 Gogo::assign_with_write_barrier(Function
* function
, Block
* enclosing
,
546 Statement_inserter
* inserter
, Expression
* lhs
,
547 Expression
* rhs
, Location loc
)
550 && ((function
->pragmas() & GOPRAGMA_NOWRITEBARRIER
) != 0
551 || (function
->pragmas() & GOPRAGMA_NOWRITEBARRIERREC
) != 0))
552 go_error_at(loc
, "write barrier prohibited");
554 Type
* type
= lhs
->type();
555 go_assert(type
->has_pointer());
558 if (lhs
->unary_expression() != NULL
559 && lhs
->unary_expression()->op() == OPERATOR_MULT
)
560 addr
= lhs
->unary_expression()->operand();
563 addr
= Expression::make_unary(OPERATOR_AND
, lhs
, loc
);
564 addr
->unary_expression()->set_does_not_escape();
566 Temporary_statement
* lhs_temp
= Statement::make_temporary(NULL
, addr
, loc
);
567 inserter
->insert(lhs_temp
);
568 lhs
= Expression::make_temporary_reference(lhs_temp
, loc
);
570 if (!Type::are_identical(type
, rhs
->type(), false, NULL
)
571 && rhs
->type()->interface_type() != NULL
572 && !rhs
->is_variable())
574 // May need a temporary for interface conversion.
575 Temporary_statement
* temp
= Statement::make_temporary(NULL
, rhs
, loc
);
576 inserter
->insert(temp
);
577 rhs
= Expression::make_temporary_reference(temp
, loc
);
579 rhs
= Expression::convert_for_assignment(this, type
, rhs
, loc
);
580 Temporary_statement
* rhs_temp
= NULL
;
581 if (!rhs
->is_variable() && !rhs
->is_constant())
583 rhs_temp
= Statement::make_temporary(NULL
, rhs
, loc
);
584 inserter
->insert(rhs_temp
);
585 rhs
= Expression::make_temporary_reference(rhs_temp
, loc
);
589 Expression::make_dereference(lhs
, Expression::NIL_CHECK_DEFAULT
, loc
);
590 Statement
* assign
= Statement::make_assignment(indir
, rhs
, loc
);
592 lhs
= Expression::make_temporary_reference(lhs_temp
, loc
);
593 if (rhs_temp
!= NULL
)
594 rhs
= Expression::make_temporary_reference(rhs_temp
, loc
);
596 Type
* unsafe_ptr_type
= Type::make_pointer_type(Type::make_void_type());
597 lhs
= Expression::make_unsafe_cast(unsafe_ptr_type
, lhs
, loc
);
600 switch (type
->base()->classification())
605 case Type::TYPE_ERROR
:
608 case Type::TYPE_POINTER
:
609 case Type::TYPE_FUNCTION
:
611 case Type::TYPE_CHANNEL
:
612 // These types are all represented by a single pointer.
613 call
= Runtime::make_call(Runtime::WRITEBARRIERPTR
, loc
, 2, lhs
, rhs
);
616 case Type::TYPE_STRING
:
617 case Type::TYPE_STRUCT
:
618 case Type::TYPE_ARRAY
:
619 case Type::TYPE_INTERFACE
:
621 rhs
= Expression::make_unary(OPERATOR_AND
, rhs
, loc
);
622 rhs
->unary_expression()->set_does_not_escape();
623 call
= Runtime::make_call(Runtime::TYPEDMEMMOVE
, loc
, 3,
624 Expression::make_type_descriptor(type
, loc
),
630 return this->check_write_barrier(enclosing
, assign
,
631 Statement::make_statement(call
, false));
634 // Return a statement that tests whether write barriers are enabled
635 // and executes either the efficient code or the write barrier
636 // function call, depending.
639 Gogo::check_write_barrier(Block
* enclosing
, Statement
* without
,
642 Location loc
= without
->location();
643 Named_object
* wb
= this->write_barrier_variable();
644 Expression
* ref
= Expression::make_var_reference(wb
, loc
);
645 Expression
* zero
= Expression::make_integer_ul(0, ref
->type(), loc
);
646 Expression
* cond
= Expression::make_binary(OPERATOR_EQEQ
, ref
, zero
, loc
);
648 Block
* then_block
= new Block(enclosing
, loc
);
649 then_block
->add_statement(without
);
651 Block
* else_block
= new Block(enclosing
, loc
);
652 else_block
->add_statement(with
);
654 return Statement::make_if_statement(cond
, then_block
, else_block
, loc
);