Corrected date in changelog
[official-gcc.git] / gcc / go / gofrontend / wb.cc
blobfb3ef4d59020270c1556ffb92b46e3e00bb63bd0
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.
7 #include "go-system.h"
9 #include "go-c.h"
10 #include "go-diagnostics.h"
11 #include "operator.h"
12 #include "lex.h"
13 #include "types.h"
14 #include "expressions.h"
15 #include "statements.h"
16 #include "runtime.h"
17 #include "gogo.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
22 // place.
24 class Mark_address_taken : public Traverse
26 public:
27 Mark_address_taken(Gogo* gogo)
28 : Traverse(traverse_expressions),
29 gogo_(gogo)
30 { }
32 int
33 expression(Expression**);
35 private:
36 Gogo* gogo_;
39 // Mark variable addresses taken.
41 int
42 Mark_address_taken::expression(Expression** pexpr)
44 Expression* expr = *pexpr;
45 Unary_expression* ue = expr->unary_expression();
46 if (ue != NULL)
47 ue->check_operand_address_taken(this->gogo_);
49 Array_index_expression* aie = expr->array_index_expression();
50 if (aie != NULL
51 && aie->end() != NULL
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.
56 bool escapes = false;
57 Node* n = Node::make_node(expr);
58 if (n->child() == NULL
59 || (n->child()->encoding() & ESCAPE_MASK) != Node::ESCAPE_NONE)
60 escapes = true;
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();
86 if (uce != NULL
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;
99 unsigned long vlen;
100 unsigned long vcap;
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);
117 Expression* slice =
118 Expression::make_array_index(array, zero, len_arg, cap_arg, loc);
119 *pexpr = slice;
123 return TRAVERSE_CONTINUE;
126 // Check variables and closures do not escape when compiling runtime.
128 class Check_escape : public Traverse
130 public:
131 Check_escape(Gogo* gogo)
132 : Traverse(traverse_expressions | traverse_variables),
133 gogo_(gogo)
137 expression(Expression**);
140 variable(Named_object*);
142 private:
143 Gogo* gogo_;
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->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)
186 // } else {
187 // A = B
188 // }
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
207 // integer moves.
209 // Traverse the IR adding write barriers.
211 class Write_barriers : public Traverse
213 public:
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*);
228 private:
229 // General IR.
230 Gogo* gogo_;
231 // Current function.
232 Function* function_;
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)
246 return t;
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
254 // calls runtime.GC.
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();
269 if (init == NULL)
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,
303 ref, init, loc);
305 var->add_preinit_statement(this->gogo_, s);
306 var->clear_init();
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())
318 default:
319 break;
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
329 // of the variable.
331 // Nothing to do for a variable with no initializer.
332 Expression* init = var->init();
333 if (init == NULL)
334 break;
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())
340 break;
342 // Nothing to do if the variable does not contain any pointers.
343 if (!var->type()->has_pointer())
344 break;
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())
349 break;
350 if (var->type()->points_to() != NULL
351 && !var->type()->points_to()->in_heap())
352 break;
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.
362 var->clear_init();
363 inserter.insert(s);
365 // Create a statement that initializes the variable with a
366 // write barrier.
367 Expression* ref = Expression::make_var_reference(no, loc);
368 Statement* assign = this->gogo_->assign_with_write_barrier(function,
369 block,
370 &inserter,
371 ref, init,
372 loc);
374 // Replace the old variable declaration statement with the new
375 // initialization.
376 block->replace_statement(*pindex, assign);
378 break;
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))
389 break;
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,
396 block,
397 &inserter,
398 lhs, rhs,
399 loc);
400 block->replace_statement(*pindex, assign);
402 break;
405 return TRAVERSE_CONTINUE;
408 // The write barrier pass.
410 void
411 Gogo::add_write_barriers()
413 Mark_address_taken mat(this);
414 this->traverse(&mat);
416 if (this->compiling_runtime() && this->package_name() == "runtime")
418 Check_escape chk(this);
419 this->traverse(&chk);
422 Write_barriers wb(this);
423 this->traverse(&wb);
426 // Return the runtime.writeBarrier variable.
428 Named_object*
429 Gogo::write_barrier_variable()
431 static Named_object* write_barrier_var;
432 if (write_barrier_var == NULL)
434 Location bloc = Linemap::predeclared_location();
436 // We pretend that writeBarrier is a uint32, so that we do a
437 // 32-bit load. That is what the gc toolchain does.
438 Type* uint32_type = Type::lookup_integer_type("uint32");
439 Variable* var = new Variable(uint32_type, NULL, true, false, false,
440 bloc);
442 bool add_to_globals;
443 Package* package = this->add_imported_package("runtime", "_", false,
444 "runtime", "runtime",
445 bloc, &add_to_globals);
446 write_barrier_var = Named_object::make_variable("writeBarrier",
447 package, var);
450 return write_barrier_var;
453 // Return whether an assignment that sets LHS needs a write barrier.
455 bool
456 Gogo::assign_needs_write_barrier(Expression* lhs)
458 // Nothing to do if the variable does not contain any pointers.
459 if (!lhs->type()->has_pointer())
460 return false;
462 // An assignment to a field is handled like an assignment to the
463 // struct.
464 while (true)
466 // Nothing to do for a type that can not be in the heap, or a
467 // pointer to a type that can not be in the heap. We check this
468 // at each level of a struct.
469 if (!lhs->type()->in_heap())
470 return false;
471 if (lhs->type()->points_to() != NULL
472 && !lhs->type()->points_to()->in_heap())
473 return false;
475 Field_reference_expression* fre = lhs->field_reference_expression();
476 if (fre == NULL)
477 break;
478 lhs = fre->expr();
481 // Nothing to do for an assignment to a temporary.
482 if (lhs->temporary_reference_expression() != NULL)
483 return false;
485 // Nothing to do for an assignment to a sink.
486 if (lhs->is_sink_expression())
487 return false;
489 // Nothing to do for an assignment to a local variable that is not
490 // on the heap.
491 Var_expression* ve = lhs->var_expression();
492 if (ve != NULL)
494 Named_object* no = ve->named_object();
495 if (no->is_variable())
497 Variable* var = no->var_value();
498 if (!var->is_global() && !var->is_in_heap())
499 return false;
501 else if (no->is_result_variable())
503 Result_variable* rvar = no->result_var_value();
504 if (!rvar->is_in_heap())
505 return false;
509 // For a struct assignment, we don't need a write barrier if all the
510 // pointer types can not be in the heap.
511 Struct_type* st = lhs->type()->struct_type();
512 if (st != NULL)
514 bool in_heap = false;
515 const Struct_field_list* fields = st->fields();
516 for (Struct_field_list::const_iterator p = fields->begin();
517 p != fields->end();
518 p++)
520 Type* ft = p->type();
521 if (!ft->has_pointer())
522 continue;
523 if (!ft->in_heap())
524 continue;
525 if (ft->points_to() != NULL && !ft->points_to()->in_heap())
526 continue;
527 in_heap = true;
528 break;
530 if (!in_heap)
531 return false;
534 // Write barrier needed in other cases.
535 return true;
538 // Return a statement that sets LHS to RHS using a write barrier.
539 // ENCLOSING is the enclosing block.
541 Statement*
542 Gogo::assign_with_write_barrier(Function* function, Block* enclosing,
543 Statement_inserter* inserter, Expression* lhs,
544 Expression* rhs, Location loc)
546 if (function != NULL
547 && ((function->pragmas() & GOPRAGMA_NOWRITEBARRIER) != 0
548 || (function->pragmas() & GOPRAGMA_NOWRITEBARRIERREC) != 0))
549 go_error_at(loc, "write barrier prohibited");
551 Type* type = lhs->type();
552 go_assert(type->has_pointer());
554 Expression* addr;
555 if (lhs->unary_expression() != NULL
556 && lhs->unary_expression()->op() == OPERATOR_MULT)
557 addr = lhs->unary_expression()->operand();
558 else
560 addr = Expression::make_unary(OPERATOR_AND, lhs, loc);
561 addr->unary_expression()->set_does_not_escape();
563 Temporary_statement* lhs_temp = Statement::make_temporary(NULL, addr, loc);
564 inserter->insert(lhs_temp);
565 lhs = Expression::make_temporary_reference(lhs_temp, loc);
567 if (!Type::are_identical(type, rhs->type(), false, NULL)
568 && rhs->type()->interface_type() != NULL
569 && !rhs->is_variable())
571 // May need a temporary for interface conversion.
572 Temporary_statement* temp = Statement::make_temporary(NULL, rhs, loc);
573 inserter->insert(temp);
574 rhs = Expression::make_temporary_reference(temp, loc);
576 rhs = Expression::convert_for_assignment(this, type, rhs, loc);
577 Temporary_statement* rhs_temp = NULL;
578 if (!rhs->is_variable() && !rhs->is_constant())
580 rhs_temp = Statement::make_temporary(NULL, rhs, loc);
581 inserter->insert(rhs_temp);
582 rhs = Expression::make_temporary_reference(rhs_temp, loc);
585 Expression* indir =
586 Expression::make_dereference(lhs, Expression::NIL_CHECK_DEFAULT, loc);
587 Statement* assign = Statement::make_assignment(indir, rhs, loc);
589 lhs = Expression::make_temporary_reference(lhs_temp, loc);
590 if (rhs_temp != NULL)
591 rhs = Expression::make_temporary_reference(rhs_temp, loc);
593 Type* unsafe_ptr_type = Type::make_pointer_type(Type::make_void_type());
594 lhs = Expression::make_unsafe_cast(unsafe_ptr_type, lhs, loc);
596 Expression* call;
597 switch (type->base()->classification())
599 default:
600 go_unreachable();
602 case Type::TYPE_ERROR:
603 return assign;
605 case Type::TYPE_POINTER:
606 case Type::TYPE_FUNCTION:
607 case Type::TYPE_MAP:
608 case Type::TYPE_CHANNEL:
609 // These types are all represented by a single pointer.
610 call = Runtime::make_call(Runtime::WRITEBARRIERPTR, loc, 2, lhs, rhs);
611 break;
613 case Type::TYPE_STRING:
614 case Type::TYPE_STRUCT:
615 case Type::TYPE_ARRAY:
616 case Type::TYPE_INTERFACE:
618 rhs = Expression::make_unary(OPERATOR_AND, rhs, loc);
619 rhs->unary_expression()->set_does_not_escape();
620 call = Runtime::make_call(Runtime::TYPEDMEMMOVE, loc, 3,
621 Expression::make_type_descriptor(type, loc),
622 lhs, rhs);
624 break;
627 return this->check_write_barrier(enclosing, assign,
628 Statement::make_statement(call, false));
631 // Return a statement that tests whether write barriers are enabled
632 // and executes either the efficient code or the write barrier
633 // function call, depending.
635 Statement*
636 Gogo::check_write_barrier(Block* enclosing, Statement* without,
637 Statement* with)
639 Location loc = without->location();
640 Named_object* wb = this->write_barrier_variable();
641 Expression* ref = Expression::make_var_reference(wb, loc);
642 Expression* zero = Expression::make_integer_ul(0, ref->type(), loc);
643 Expression* cond = Expression::make_binary(OPERATOR_EQEQ, ref, zero, loc);
645 Block* then_block = new Block(enclosing, loc);
646 then_block->add_statement(without);
648 Block* else_block = new Block(enclosing, loc);
649 else_block->add_statement(with);
651 return Statement::make_if_statement(cond, then_block, else_block, loc);