111
|
1 // escape.cc -- Go escape analysis (based on Go compiler algorithm).
|
|
2
|
|
3 // Copyright 2016 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.
|
|
6
|
|
7 #include "go-system.h"
|
|
8
|
|
9 #include <limits>
|
|
10 #include <stack>
|
|
11 #include <sstream>
|
|
12
|
|
13 #include "gogo.h"
|
|
14 #include "types.h"
|
|
15 #include "expressions.h"
|
|
16 #include "statements.h"
|
|
17 #include "escape.h"
|
131
|
18 #include "lex.h"
|
111
|
19 #include "ast-dump.h"
|
|
20 #include "go-optimize.h"
|
|
21 #include "go-diagnostics.h"
|
131
|
22 #include "go-sha1.h"
|
111
|
23
|
|
24 // class Node.
|
|
25
|
|
26 // Return the node's type, if it makes sense for it to have one.
|
|
27
|
|
28 Type*
|
|
29 Node::type() const
|
|
30 {
|
|
31 if (this->object() != NULL
|
|
32 && this->object()->is_variable())
|
|
33 return this->object()->var_value()->type();
|
|
34 else if (this->object() != NULL
|
|
35 && this->object()->is_function())
|
|
36 return this->object()->func_value()->type();
|
|
37 else if (this->expr() != NULL)
|
|
38 return this->expr()->type();
|
131
|
39 else if (this->is_indirect())
|
|
40 {
|
|
41 if (this->child()->type()->deref()->is_void_type())
|
|
42 // This is a void*. The referred type can be actually any type,
|
|
43 // which may also be pointer. We model it as another void*, so
|
|
44 // we don't lose pointer-ness.
|
|
45 return this->child()->type();
|
|
46 else if (this->child()->type()->is_slice_type())
|
|
47 // We model "indirect" of a slice as dereferencing its pointer
|
|
48 // field (to get element). Use element type here.
|
|
49 return this->child()->type()->array_type()->element_type();
|
|
50 else if (this->child()->type()->is_string_type())
|
|
51 return Type::lookup_integer_type("uint8");
|
|
52 else
|
|
53 return this->child()->type()->deref();
|
|
54 }
|
|
55 else if (this->statement() != NULL
|
|
56 && this->statement()->temporary_statement() != NULL)
|
|
57 return this->statement()->temporary_statement()->type();
|
111
|
58 else
|
|
59 return NULL;
|
|
60 }
|
|
61
|
|
62 // A helper for reporting; return this node's location.
|
|
63
|
|
64 Location
|
|
65 Node::location() const
|
|
66 {
|
|
67 if (this->object() != NULL && !this->object()->is_sink())
|
|
68 return this->object()->location();
|
|
69 else if (this->expr() != NULL)
|
|
70 return this->expr()->location();
|
|
71 else if (this->statement() != NULL)
|
|
72 return this->statement()->location();
|
131
|
73 else if (this->is_indirect())
|
|
74 return this->child()->location();
|
111
|
75 else
|
|
76 return Linemap::unknown_location();
|
|
77 }
|
|
78
|
131
|
79 // A helper for reporting; return the location where the underlying
|
|
80 // object is defined.
|
|
81
|
|
82 Location
|
|
83 Node::definition_location() const
|
|
84 {
|
|
85 if (this->object() != NULL && !this->object()->is_sink())
|
|
86 {
|
|
87 Named_object* no = this->object();
|
|
88 if (no->is_variable() || no->is_result_variable())
|
|
89 return no->location();
|
|
90 }
|
|
91 else if (this->expr() != NULL)
|
|
92 {
|
|
93 Var_expression* ve = this->expr()->var_expression();
|
|
94 if (ve != NULL)
|
|
95 {
|
|
96 Named_object* no = ve->named_object();
|
|
97 if (no->is_variable() || no->is_result_variable())
|
|
98 return no->location();
|
|
99 }
|
|
100 Enclosed_var_expression* eve = this->expr()->enclosed_var_expression();
|
|
101 if (eve != NULL)
|
|
102 {
|
|
103 Named_object* no = eve->variable();
|
|
104 if (no->is_variable() || no->is_result_variable())
|
|
105 return no->location();
|
|
106 }
|
|
107 }
|
|
108 return this->location();
|
|
109 }
|
|
110
|
111
|
111 // To match the cmd/gc debug output, strip away the packed prefixes on functions
|
|
112 // and variable/expressions.
|
|
113
|
|
114 std::string
|
|
115 strip_packed_prefix(Gogo* gogo, const std::string& s)
|
|
116 {
|
|
117 std::string packed_prefix = "." + gogo->pkgpath() + ".";
|
|
118 std::string fmt = s;
|
|
119 for (size_t pos = fmt.find(packed_prefix);
|
|
120 pos != std::string::npos;
|
|
121 pos = fmt.find(packed_prefix))
|
|
122 fmt.erase(pos, packed_prefix.length());
|
|
123 return fmt;
|
|
124 }
|
|
125
|
|
126 // A helper for debugging; return this node's AST formatted string.
|
|
127 // This is an implementation of gc's Nconv with obj.FmtShort.
|
|
128
|
|
129 std::string
|
|
130 Node::ast_format(Gogo* gogo) const
|
|
131 {
|
|
132 std::ostringstream ss;
|
|
133 if (this->is_sink())
|
|
134 ss << ".sink";
|
|
135 else if (this->object() != NULL)
|
|
136 {
|
|
137 Named_object* no = this->object();
|
|
138 if (no->is_function() && no->func_value()->enclosing() != NULL)
|
|
139 return "func literal";
|
131
|
140 ss << no->message_name();
|
111
|
141 }
|
|
142 else if (this->expr() != NULL)
|
|
143 {
|
|
144 Expression* e = this->expr();
|
|
145 bool is_call = e->call_expression() != NULL;
|
|
146 if (is_call)
|
|
147 e->call_expression()->fn();
|
|
148 Func_expression* fe = e->func_expression();;
|
|
149
|
|
150 bool is_closure = fe != NULL && fe->closure() != NULL;
|
|
151 if (is_closure)
|
|
152 {
|
|
153 if (is_call)
|
|
154 return "(func literal)()";
|
|
155 return "func literal";
|
|
156 }
|
|
157 Ast_dump_context::dump_to_stream(this->expr(), &ss);
|
|
158 }
|
131
|
159 else if (this->statement() != NULL)
|
111
|
160 {
|
|
161 Statement* s = this->statement();
|
|
162 Goto_unnamed_statement* unnamed = s->goto_unnamed_statement();
|
|
163 if (unnamed != NULL)
|
|
164 {
|
|
165 Statement* derived = unnamed->unnamed_label()->derived_from();
|
|
166 if (derived != NULL)
|
|
167 {
|
|
168 switch (derived->classification())
|
|
169 {
|
|
170 case Statement::STATEMENT_FOR:
|
|
171 case Statement::STATEMENT_FOR_RANGE:
|
|
172 return "for loop";
|
|
173 break;
|
|
174
|
|
175 case Statement::STATEMENT_SWITCH:
|
|
176 return "switch";
|
|
177 break;
|
|
178
|
|
179 case Statement::STATEMENT_TYPE_SWITCH:
|
|
180 return "type switch";
|
|
181 break;
|
|
182
|
|
183 default:
|
|
184 break;
|
|
185 }
|
|
186 }
|
|
187 }
|
131
|
188 Temporary_statement* tmp = s->temporary_statement();
|
|
189 if (tmp != NULL)
|
|
190 {
|
|
191 // Temporary's format can never match gc's output, and
|
|
192 // temporaries are inserted differently anyway. We just
|
|
193 // print something convenient.
|
|
194 ss << "tmp." << (uintptr_t) tmp;
|
|
195 if (tmp->init() != NULL)
|
|
196 {
|
|
197 ss << " [ = ";
|
|
198 Ast_dump_context::dump_to_stream(tmp->init(), &ss);
|
|
199 ss << " ]";
|
|
200 }
|
|
201 }
|
|
202 else
|
|
203 Ast_dump_context::dump_to_stream(s, &ss);
|
111
|
204 }
|
131
|
205 else if (this->is_indirect())
|
|
206 return "*(" + this->child()->ast_format(gogo) + ")";
|
|
207
|
|
208 std::string s = strip_packed_prefix(gogo, ss.str());
|
|
209
|
|
210 // trim trailing space
|
|
211 return s.substr(0, s.find_last_not_of(' ') + 1);
|
111
|
212 }
|
|
213
|
|
214 // A helper for debugging; return this node's detailed format string.
|
|
215 // This is an implementation of gc's Jconv with obj.FmtShort.
|
|
216
|
|
217 std::string
|
131
|
218 Node::details()
|
111
|
219 {
|
|
220 std::stringstream details;
|
|
221
|
|
222 if (!this->is_sink())
|
|
223 details << " l(" << Linemap::location_to_line(this->location()) << ")";
|
|
224
|
|
225 bool is_varargs = false;
|
|
226 bool is_address_taken = false;
|
|
227 bool is_in_heap = false;
|
|
228 bool is_assigned = false;
|
|
229 std::string class_name;
|
|
230
|
|
231 Expression* e = this->expr();
|
|
232 Named_object* node_object = NULL;
|
|
233 if (this->object() != NULL)
|
|
234 node_object = this->object();
|
|
235 else if (e != NULL && e->var_expression() != NULL)
|
|
236 node_object = e->var_expression()->named_object();
|
|
237
|
|
238 if (node_object)
|
|
239 {
|
|
240 // TODO(cmang): For named variables and functions, we want to output
|
|
241 // the function depth.
|
|
242 if (node_object->is_variable())
|
|
243 {
|
|
244 Variable* var = node_object->var_value();
|
|
245 is_varargs = var->is_varargs_parameter();
|
|
246 is_address_taken = (var->is_address_taken()
|
|
247 || var->is_non_escaping_address_taken());
|
|
248 is_in_heap = var->is_in_heap();
|
|
249 is_assigned = var->init() != NULL;
|
|
250
|
|
251 if (var->is_global())
|
|
252 class_name = "PEXTERN";
|
|
253 else if (var->is_parameter())
|
|
254 class_name = "PPARAM";
|
|
255 else if (var->is_closure())
|
|
256 class_name = "PPARAMREF";
|
|
257 else
|
|
258 class_name = "PAUTO";
|
|
259 }
|
|
260 else if (node_object->is_result_variable())
|
|
261 class_name = "PPARAMOUT";
|
|
262 else if (node_object->is_function()
|
|
263 || node_object->is_function_declaration())
|
|
264 class_name = "PFUNC";
|
|
265 }
|
|
266 else if (e != NULL && e->enclosed_var_expression() != NULL)
|
|
267 {
|
|
268 Named_object* enclosed = e->enclosed_var_expression()->variable();
|
|
269 if (enclosed->is_variable())
|
|
270 {
|
|
271 Variable* var = enclosed->var_value();
|
|
272 is_address_taken = (var->is_address_taken()
|
|
273 || var->is_non_escaping_address_taken());
|
|
274 }
|
|
275 else
|
|
276 {
|
|
277 Result_variable* var = enclosed->result_var_value();
|
|
278 is_address_taken = (var->is_address_taken()
|
|
279 || var->is_non_escaping_address_taken());
|
|
280 }
|
|
281 class_name = "PPARAMREF";
|
|
282 }
|
|
283
|
|
284 if (!class_name.empty())
|
|
285 {
|
|
286 details << " class(" << class_name;
|
|
287 if (is_in_heap)
|
|
288 details << ",heap";
|
|
289 details << ")";
|
|
290 }
|
|
291
|
|
292 switch ((this->encoding() & ESCAPE_MASK))
|
|
293 {
|
|
294 case Node::ESCAPE_UNKNOWN:
|
|
295 break;
|
|
296
|
|
297 case Node::ESCAPE_HEAP:
|
|
298 details << " esc(h)";
|
|
299 break;
|
|
300
|
|
301 case Node::ESCAPE_NONE:
|
|
302 details << " esc(no)";
|
|
303 break;
|
|
304
|
|
305 case Node::ESCAPE_NEVER:
|
|
306 details << " esc(N)";
|
|
307 break;
|
|
308
|
|
309 default:
|
|
310 details << " esc(" << this->encoding() << ")";
|
|
311 break;
|
|
312 }
|
|
313
|
|
314 if (this->state_ != NULL && this->state_->loop_depth != 0)
|
|
315 details << " ld(" << this->state_->loop_depth << ")";
|
|
316
|
|
317 if (is_varargs)
|
|
318 details << " isddd(1)";
|
|
319 if (is_address_taken)
|
|
320 details << " addrtaken";
|
|
321 if (is_assigned)
|
|
322 details << " assigned";
|
|
323
|
|
324 return details.str();
|
|
325 }
|
|
326
|
|
327 std::string
|
|
328 Node::op_format() const
|
|
329 {
|
|
330 std::stringstream op;
|
|
331 Ast_dump_context adc(&op, false);
|
|
332 if (this->expr() != NULL)
|
|
333 {
|
|
334 Expression* e = this->expr();
|
|
335 switch (e->classification())
|
|
336 {
|
|
337 case Expression::EXPRESSION_UNARY:
|
|
338 adc.dump_operator(e->unary_expression()->op());
|
|
339 break;
|
|
340
|
|
341 case Expression::EXPRESSION_BINARY:
|
|
342 adc.dump_operator(e->binary_expression()->op());
|
|
343 break;
|
|
344
|
|
345 case Expression::EXPRESSION_CALL:
|
|
346 op << "function call";
|
|
347 break;
|
|
348
|
|
349 case Expression::EXPRESSION_FUNC_REFERENCE:
|
|
350 if (e->func_expression()->is_runtime_function())
|
|
351 {
|
|
352 switch (e->func_expression()->runtime_code())
|
|
353 {
|
|
354 case Runtime::GOPANIC:
|
|
355 op << "panic";
|
|
356 break;
|
|
357
|
|
358 case Runtime::GROWSLICE:
|
|
359 op << "append";
|
|
360 break;
|
|
361
|
|
362 case Runtime::SLICECOPY:
|
|
363 case Runtime::SLICESTRINGCOPY:
|
|
364 case Runtime::TYPEDSLICECOPY:
|
|
365 op << "copy";
|
|
366 break;
|
|
367
|
|
368 case Runtime::MAKECHAN:
|
131
|
369 case Runtime::MAKECHAN64:
|
111
|
370 case Runtime::MAKEMAP:
|
|
371 case Runtime::MAKESLICE:
|
|
372 case Runtime::MAKESLICE64:
|
|
373 op << "make";
|
|
374 break;
|
|
375
|
|
376 case Runtime::DEFERPROC:
|
|
377 op << "defer";
|
|
378 break;
|
|
379
|
|
380 case Runtime::GORECOVER:
|
|
381 op << "recover";
|
|
382 break;
|
|
383
|
|
384 case Runtime::CLOSE:
|
|
385 op << "close";
|
|
386 break;
|
|
387
|
|
388 default:
|
|
389 break;
|
|
390 }
|
|
391 }
|
|
392 break;
|
|
393
|
|
394 case Expression::EXPRESSION_ALLOCATION:
|
|
395 op << "new";
|
|
396 break;
|
|
397
|
|
398 case Expression::EXPRESSION_RECEIVE:
|
|
399 op << "<-";
|
|
400 break;
|
|
401
|
|
402 default:
|
|
403 break;
|
|
404 }
|
|
405 }
|
|
406
|
|
407 if (this->statement() != NULL)
|
|
408 {
|
|
409 switch (this->statement()->classification())
|
|
410 {
|
|
411 case Statement::STATEMENT_DEFER:
|
|
412 op << "defer";
|
|
413 break;
|
|
414
|
|
415 case Statement::STATEMENT_RETURN:
|
|
416 op << "return";
|
|
417 break;
|
|
418
|
|
419 default:
|
|
420 break;
|
|
421 }
|
|
422 }
|
131
|
423 if (this->is_indirect())
|
|
424 op << "*";
|
111
|
425 return op.str();
|
|
426 }
|
|
427
|
|
428 // Return this node's state, creating it if has not been initialized.
|
|
429
|
|
430 Node::Escape_state*
|
|
431 Node::state(Escape_context* context, Named_object* fn)
|
|
432 {
|
|
433 if (this->state_ == NULL)
|
|
434 {
|
|
435 if (this->expr() != NULL && this->expr()->var_expression() != NULL)
|
|
436 {
|
|
437 // Tie state of variable references to underlying variables.
|
|
438 Named_object* var_no = this->expr()->var_expression()->named_object();
|
|
439 Node* var_node = Node::make_node(var_no);
|
|
440 this->state_ = var_node->state(context, fn);
|
|
441 }
|
|
442 else
|
|
443 {
|
|
444 this->state_ = new Node::Escape_state;
|
|
445 if (fn == NULL)
|
|
446 fn = context->current_function();
|
|
447
|
|
448 this->state_->fn = fn;
|
|
449 }
|
|
450 }
|
|
451 go_assert(this->state_ != NULL);
|
|
452 return this->state_;
|
|
453 }
|
|
454
|
131
|
455 Node::~Node()
|
|
456 {
|
|
457 if (this->state_ != NULL)
|
|
458 {
|
|
459 if (this->expr() == NULL || this->expr()->var_expression() == NULL)
|
|
460 // Var expression Node is excluded since it shares state with the
|
|
461 // underlying var Node.
|
|
462 delete this->state_;
|
|
463 }
|
|
464 }
|
|
465
|
|
466 int
|
|
467 Node::encoding()
|
|
468 {
|
|
469 if (this->expr() != NULL
|
|
470 && this->expr()->var_expression() != NULL)
|
|
471 {
|
|
472 // Get the underlying object's encoding.
|
|
473 Named_object* no = this->expr()->var_expression()->named_object();
|
|
474 int enc = Node::make_node(no)->encoding();
|
|
475 this->encoding_ = enc;
|
|
476 }
|
|
477 return this->encoding_;
|
|
478 }
|
|
479
|
111
|
480 void
|
|
481 Node::set_encoding(int enc)
|
|
482 {
|
|
483 this->encoding_ = enc;
|
131
|
484 if (this->expr() != NULL)
|
111
|
485 {
|
131
|
486 if (this->expr()->var_expression() != NULL)
|
|
487 {
|
|
488 // Set underlying object as well.
|
|
489 Named_object* no = this->expr()->var_expression()->named_object();
|
|
490 Node::make_node(no)->set_encoding(enc);
|
|
491 }
|
|
492 else if (this->expr()->func_expression() != NULL)
|
|
493 {
|
|
494 // Propagate the escape state to the underlying
|
|
495 // closure (heap expression).
|
|
496 Expression* closure = this->expr()->func_expression()->closure();
|
|
497 if (closure != NULL)
|
|
498 Node::make_node(closure)->set_encoding(enc);
|
|
499 }
|
111
|
500 }
|
|
501 }
|
|
502
|
|
503 bool
|
|
504 Node::is_big(Escape_context* context) const
|
|
505 {
|
|
506 Type* t = this->type();
|
|
507 if (t == NULL
|
|
508 || t->is_call_multiple_result_type()
|
|
509 || t->is_sink_type()
|
|
510 || t->is_void_type()
|
|
511 || t->is_abstract())
|
|
512 return false;
|
|
513
|
|
514 int64_t size;
|
|
515 bool ok = t->backend_type_size(context->gogo(), &size);
|
|
516 bool big = ok && (size < 0 || size > 10 * 1024 * 1024);
|
|
517
|
|
518 if (this->expr() != NULL)
|
|
519 {
|
|
520 if (this->expr()->allocation_expression() != NULL)
|
|
521 {
|
|
522 ok = t->deref()->backend_type_size(context->gogo(), &size);
|
|
523 big = big || size <= 0 || size >= (1 << 16);
|
|
524 }
|
|
525 else if (this->expr()->call_expression() != NULL)
|
|
526 {
|
|
527 Call_expression* call = this->expr()->call_expression();
|
|
528 Func_expression* fn = call->fn()->func_expression();
|
|
529 if (fn != NULL
|
|
530 && fn->is_runtime_function()
|
|
531 && (fn->runtime_code() == Runtime::MAKESLICE
|
|
532 || fn->runtime_code() == Runtime::MAKESLICE64))
|
|
533 {
|
|
534 // Second argument is length.
|
|
535 Expression_list::iterator p = call->args()->begin();
|
|
536 ++p;
|
|
537
|
131
|
538 Expression* e = *p;
|
|
539 if (e->temporary_reference_expression() != NULL)
|
|
540 {
|
|
541 Temporary_reference_expression* tre = e->temporary_reference_expression();
|
|
542 if (tre->statement() != NULL && tre->statement()->init() != NULL)
|
|
543 e = tre->statement()->init();
|
|
544 }
|
|
545
|
111
|
546 Numeric_constant nc;
|
|
547 unsigned long v;
|
131
|
548 if (e->numeric_constant_value(&nc)
|
111
|
549 && nc.to_unsigned_long(&v) == Numeric_constant::NC_UL_VALID)
|
|
550 big = big || v >= (1 << 16);
|
|
551 }
|
|
552 }
|
|
553 }
|
|
554
|
|
555 return big;
|
|
556 }
|
|
557
|
|
558 bool
|
|
559 Node::is_sink() const
|
|
560 {
|
|
561 if (this->object() != NULL
|
|
562 && this->object()->is_sink())
|
|
563 return true;
|
|
564 else if (this->expr() != NULL
|
|
565 && this->expr()->is_sink_expression())
|
|
566 return true;
|
|
567 return false;
|
|
568 }
|
|
569
|
|
570 std::map<Named_object*, Node*> Node::objects;
|
|
571 std::map<Expression*, Node*> Node::expressions;
|
|
572 std::map<Statement*, Node*> Node::statements;
|
131
|
573 std::vector<Node*> Node::indirects;
|
111
|
574
|
|
575 // Make a object node or return a cached node for this object.
|
|
576
|
|
577 Node*
|
|
578 Node::make_node(Named_object* no)
|
|
579 {
|
|
580 if (Node::objects.find(no) != Node::objects.end())
|
|
581 return Node::objects[no];
|
|
582
|
|
583 Node* n = new Node(no);
|
|
584 std::pair<Named_object*, Node*> val(no, n);
|
|
585 Node::objects.insert(val);
|
|
586 return n;
|
|
587 }
|
|
588
|
|
589 // Make an expression node or return a cached node for this expression.
|
|
590
|
|
591 Node*
|
|
592 Node::make_node(Expression* e)
|
|
593 {
|
|
594 if (Node::expressions.find(e) != Node::expressions.end())
|
|
595 return Node::expressions[e];
|
|
596
|
|
597 Node* n = new Node(e);
|
|
598 std::pair<Expression*, Node*> val(e, n);
|
|
599 Node::expressions.insert(val);
|
|
600 return n;
|
|
601 }
|
|
602
|
|
603 // Make a statement node or return a cached node for this statement.
|
|
604
|
|
605 Node*
|
|
606 Node::make_node(Statement* s)
|
|
607 {
|
|
608 if (Node::statements.find(s) != Node::statements.end())
|
|
609 return Node::statements[s];
|
|
610
|
|
611 Node* n = new Node(s);
|
|
612 std::pair<Statement*, Node*> val(s, n);
|
|
613 Node::statements.insert(val);
|
|
614 return n;
|
|
615 }
|
|
616
|
131
|
617 // Make an indirect node with given child.
|
|
618
|
|
619 Node*
|
|
620 Node::make_indirect_node(Node* child)
|
|
621 {
|
|
622 Node* n = new Node(child);
|
|
623 Node::indirects.push_back(n);
|
|
624 return n;
|
|
625 }
|
|
626
|
111
|
627 // Returns the maximum of an exisiting escape value
|
|
628 // (and its additional parameter flow flags) and a new escape type.
|
|
629
|
|
630 int
|
|
631 Node::max_encoding(int e, int etype)
|
|
632 {
|
131
|
633 if ((e & ESCAPE_MASK) > etype)
|
111
|
634 return e;
|
|
635 if (etype == Node::ESCAPE_NONE || etype == Node::ESCAPE_RETURN)
|
|
636 return (e & ~ESCAPE_MASK) | etype;
|
|
637 return etype;
|
|
638 }
|
|
639
|
|
640 // Return a modified encoding for an input parameter that flows into an
|
|
641 // output parameter.
|
|
642
|
|
643 int
|
|
644 Node::note_inout_flows(int e, int index, Level level)
|
|
645 {
|
|
646 // Flow+level is encoded in two bits.
|
|
647 // 00 = not flow, xx = level+1 for 0 <= level <= maxEncodedLevel.
|
|
648 // 16 bits for Esc allows 6x2bits or 4x3bits or 3x4bits if additional
|
|
649 // information would be useful.
|
|
650 if (level.value() <= 0 && level.suffix_value() > 0)
|
|
651 return Node::max_encoding(e|ESCAPE_CONTENT_ESCAPES, Node::ESCAPE_NONE);
|
|
652 if (level.value() < 0)
|
|
653 return Node::ESCAPE_HEAP;
|
|
654 if (level.value() > ESCAPE_MAX_ENCODED_LEVEL)
|
|
655 level = Level::From(ESCAPE_MAX_ENCODED_LEVEL);
|
|
656
|
|
657 int encoded = level.value() + 1;
|
|
658 int shift = ESCAPE_BITS_PER_OUTPUT_IN_TAG * index + ESCAPE_RETURN_BITS;
|
|
659 int old = (e >> shift) & ESCAPE_BITS_MASK_FOR_TAG;
|
|
660 if (old == 0
|
|
661 || (encoded != 0 && encoded < old))
|
|
662 old = encoded;
|
|
663
|
|
664 int encoded_flow = old << shift;
|
|
665 if (((encoded_flow >> shift) & ESCAPE_BITS_MASK_FOR_TAG) != old)
|
|
666 {
|
|
667 // Failed to encode. Put this on the heap.
|
|
668 return Node::ESCAPE_HEAP;
|
|
669 }
|
|
670
|
|
671 return (e & ~(ESCAPE_BITS_MASK_FOR_TAG << shift)) | encoded_flow;
|
|
672 }
|
|
673
|
|
674 // Class Escape_context.
|
|
675
|
|
676 Escape_context::Escape_context(Gogo* gogo, bool recursive)
|
|
677 : gogo_(gogo), current_function_(NULL), recursive_(recursive),
|
|
678 sink_(Node::make_node(Named_object::make_sink())), loop_depth_(0),
|
|
679 flood_id_(0), pdepth_(0)
|
|
680 {
|
|
681 // The sink always escapes to heap and strictly lives outside of the
|
|
682 // current function i.e. loop_depth == -1.
|
|
683 Node::Escape_state* state = this->sink_->state(this, NULL);
|
|
684 state->loop_depth = -1;
|
|
685 }
|
|
686
|
|
687 std::string
|
|
688 debug_function_name(Named_object* fn)
|
|
689 {
|
|
690 if (fn == NULL)
|
|
691 return "<S>";
|
|
692
|
131
|
693 if (!fn->is_function())
|
111
|
694 return Gogo::unpack_hidden_name(fn->name());
|
|
695
|
131
|
696 std::string fnname = Gogo::unpack_hidden_name(fn->name());
|
|
697 if (fn->func_value()->is_method())
|
|
698 {
|
|
699 // Methods in gc compiler are named "T.m" or "(*T).m" where
|
|
700 // T is the receiver type. Add the receiver here.
|
|
701 Type* rt = fn->func_value()->type()->receiver()->type();
|
|
702 switch (rt->classification())
|
|
703 {
|
|
704 case Type::TYPE_NAMED:
|
|
705 fnname = rt->named_type()->name() + "." + fnname;
|
|
706 break;
|
|
707
|
|
708 case Type::TYPE_POINTER:
|
|
709 {
|
|
710 Named_type* nt = rt->points_to()->named_type();
|
|
711 if (nt != NULL)
|
|
712 fnname = "(*" + nt->name() + ")." + fnname;
|
|
713 break;
|
|
714 }
|
|
715
|
|
716 default:
|
|
717 break;
|
|
718 }
|
|
719 }
|
|
720
|
|
721 return fnname;
|
111
|
722 }
|
|
723
|
|
724 // Return the name of the current function.
|
|
725
|
|
726 std::string
|
|
727 Escape_context::current_function_name() const
|
|
728 {
|
|
729 return debug_function_name(this->current_function_);
|
|
730 }
|
|
731
|
|
732 // Initialize the dummy return values for this Node N using the results
|
|
733 // in FNTYPE.
|
|
734
|
|
735 void
|
|
736 Escape_context::init_retvals(Node* n, Function_type* fntype)
|
|
737 {
|
|
738 if (fntype == NULL || fntype->results() == NULL)
|
|
739 return;
|
|
740
|
|
741 Node::Escape_state* state = n->state(this, NULL);
|
131
|
742 state->retvals.clear();
|
111
|
743 Location loc = n->location();
|
|
744
|
|
745 int i = 0;
|
|
746 char buf[50];
|
|
747 for (Typed_identifier_list::const_iterator p = fntype->results()->begin();
|
|
748 p != fntype->results()->end();
|
|
749 ++p, ++i)
|
|
750 {
|
|
751 snprintf(buf, sizeof buf, ".out%d", i);
|
|
752 Variable* dummy_var = new Variable(p->type(), NULL, false, false,
|
|
753 false, loc);
|
|
754 dummy_var->set_is_used();
|
|
755 Named_object* dummy_no =
|
|
756 Named_object::make_variable(buf, NULL, dummy_var);
|
|
757 Node* dummy_node = Node::make_node(dummy_no);
|
|
758 // Initialize the state of the dummy output node.
|
131
|
759 Node::Escape_state* dummy_node_state = dummy_node->state(this, NULL);
|
|
760 dummy_node_state->loop_depth = this->loop_depth_;
|
111
|
761
|
|
762 // Add dummy node to the retvals of n.
|
|
763 state->retvals.push_back(dummy_node);
|
|
764 }
|
|
765 }
|
|
766
|
|
767
|
|
768 // Apply an indirection to N and return the result.
|
|
769
|
|
770 Node*
|
|
771 Escape_context::add_dereference(Node* n)
|
|
772 {
|
131
|
773 Expression* e = n->expr();
|
|
774 Location loc = n->location();
|
|
775 Node* ind;
|
|
776 if (e != NULL
|
|
777 && e->type()->points_to() != NULL
|
|
778 && !e->type()->points_to()->is_void_type())
|
|
779 {
|
|
780 // We don't dereference void*, which can be actually any pointer type.
|
|
781 Expression* deref_expr = Expression::make_unary(OPERATOR_MULT, e, loc);
|
|
782 ind = Node::make_node(deref_expr);
|
|
783 }
|
|
784 else
|
|
785 // The gc compiler simply makes an OIND node. We can't do it
|
|
786 // for non-pointer type because that will result in a type error.
|
|
787 // Instead, we model this by making a node with a special flavor.
|
|
788 ind = Node::make_indirect_node(n);
|
|
789
|
|
790 // Initialize the state.
|
|
791 Node::Escape_state* state = ind->state(this, NULL);
|
|
792 state->loop_depth = n->state(this, NULL)->loop_depth;
|
111
|
793 return ind;
|
|
794 }
|
|
795
|
|
796 void
|
|
797 Escape_context::track(Node* n)
|
|
798 {
|
|
799 n->set_encoding(Node::ESCAPE_NONE);
|
|
800 // Initialize this node's state if it hasn't been encountered
|
|
801 // before.
|
|
802 Node::Escape_state* state = n->state(this, NULL);
|
|
803 state->loop_depth = this->loop_depth_;
|
|
804
|
|
805 this->noesc_.push_back(n);
|
|
806 }
|
|
807
|
|
808 // Return the string representation of an escapement encoding.
|
|
809
|
|
810 std::string
|
|
811 Escape_note::make_tag(int encoding)
|
|
812 {
|
|
813 char buf[50];
|
|
814 snprintf(buf, sizeof buf, "esc:0x%x", encoding);
|
|
815 return buf;
|
|
816 }
|
|
817
|
|
818 // Return the escapement encoding for a string tag.
|
|
819
|
|
820 int
|
|
821 Escape_note::parse_tag(std::string* tag)
|
|
822 {
|
|
823 if (tag == NULL || tag->substr(0, 4) != "esc:")
|
|
824 return Node::ESCAPE_UNKNOWN;
|
|
825 int encoding = (int)strtol(tag->substr(4).c_str(), NULL, 0);
|
|
826 if (encoding == 0)
|
|
827 return Node::ESCAPE_UNKNOWN;
|
|
828 return encoding;
|
|
829 }
|
|
830
|
|
831
|
|
832 // The -fgo-optimize-alloc flag activates this escape analysis.
|
|
833
|
131
|
834 Go_optimize optimize_allocation_flag("allocs", true);
|
|
835
|
|
836 // A helper function to compute whether a function name has a
|
|
837 // matching hash value.
|
|
838
|
|
839 static bool
|
|
840 escape_hash_match(std::string suffix, std::string name)
|
|
841 {
|
|
842 if (suffix.empty())
|
|
843 return true;
|
|
844 if (suffix.at(0) == '-')
|
|
845 return !escape_hash_match(suffix.substr(1), name);
|
|
846
|
|
847 const char* p = name.c_str();
|
|
848 Go_sha1_helper* sha1_helper = go_create_sha1_helper();
|
|
849 sha1_helper->process_bytes(p, strlen(p));
|
|
850 std::string s = sha1_helper->finish();
|
|
851 delete sha1_helper;
|
|
852
|
|
853 int j = suffix.size() - 1;
|
|
854 for (int i = s.size() - 1; i >= 0; i--)
|
|
855 {
|
|
856 char c = s.at(i);
|
|
857 for (int k = 0; k < 8; k++, j--, c>>=1)
|
|
858 {
|
|
859 if (j < 0)
|
|
860 return true;
|
|
861 char bit = suffix.at(j) - '0';
|
|
862 if ((c&1) != bit)
|
|
863 return false;
|
|
864 }
|
|
865 }
|
|
866 return false;
|
|
867 }
|
111
|
868
|
|
869 // Analyze the program flow for escape information.
|
|
870
|
|
871 void
|
|
872 Gogo::analyze_escape()
|
|
873 {
|
131
|
874 if (saw_errors())
|
|
875 return;
|
|
876
|
|
877 if (!optimize_allocation_flag.is_enabled()
|
|
878 && !this->compiling_runtime())
|
|
879 // We always run escape analysis when compiling runtime.
|
111
|
880 return;
|
|
881
|
|
882 // Discover strongly connected groups of functions to analyze for escape
|
|
883 // information in this package.
|
|
884 this->discover_analysis_sets();
|
|
885
|
131
|
886 if (!this->debug_escape_hash().empty())
|
|
887 std::cerr << "debug-escape-hash " << this->debug_escape_hash() << "\n";
|
|
888
|
111
|
889 for (std::vector<Analysis_set>::iterator p = this->analysis_sets_.begin();
|
|
890 p != this->analysis_sets_.end();
|
|
891 ++p)
|
|
892 {
|
|
893 std::vector<Named_object*> stack = p->first;
|
131
|
894
|
|
895 if (!this->debug_escape_hash().empty())
|
|
896 {
|
|
897 bool match = false;
|
|
898 for (std::vector<Named_object*>::const_iterator fn = stack.begin();
|
|
899 fn != stack.end();
|
|
900 ++fn)
|
|
901 match = match || escape_hash_match(this->debug_escape_hash(), (*fn)->message_name());
|
|
902 if (!match)
|
|
903 {
|
|
904 // Escape analysis won't run on these functions, but still
|
|
905 // need to tag them, so the caller knows.
|
|
906 for (std::vector<Named_object*>::iterator fn = stack.begin();
|
|
907 fn != stack.end();
|
|
908 ++fn)
|
|
909 if ((*fn)->is_function())
|
|
910 {
|
|
911 Function_type* fntype = (*fn)->func_value()->type();
|
|
912 fntype->set_is_tagged();
|
|
913
|
|
914 std::cerr << "debug-escape-hash disables " << debug_function_name(*fn) << "\n";
|
|
915 }
|
|
916
|
|
917 continue;
|
|
918 }
|
|
919 for (std::vector<Named_object*>::const_iterator fn = stack.begin();
|
|
920 fn != stack.end();
|
|
921 ++fn)
|
|
922 if ((*fn)->is_function())
|
|
923 std::cerr << "debug-escape-hash triggers " << debug_function_name(*fn) << "\n";
|
|
924 }
|
|
925
|
111
|
926 Escape_context* context = new Escape_context(this, p->second);
|
|
927
|
|
928 // Analyze the flow of each function; build the connection graph.
|
|
929 // This is the assign phase.
|
|
930 for (std::vector<Named_object*>::reverse_iterator fn = stack.rbegin();
|
|
931 fn != stack.rend();
|
|
932 ++fn)
|
|
933 {
|
|
934 context->set_current_function(*fn);
|
|
935 this->assign_connectivity(context, *fn);
|
|
936 }
|
|
937
|
|
938 // Propagate levels across each dst. This is the flood phase.
|
|
939 std::set<Node*> dsts = context->dsts();
|
131
|
940 Unordered_map(Node*, int) escapes;
|
111
|
941 for (std::set<Node*>::iterator n = dsts.begin();
|
|
942 n != dsts.end();
|
|
943 ++n)
|
131
|
944 {
|
|
945 escapes[*n] = (*n)->encoding();
|
|
946 this->propagate_escape(context, *n);
|
|
947 }
|
|
948 for (;;)
|
|
949 {
|
|
950 // Reflood if the roots' escape states increase. Run until fix point.
|
|
951 // This is rare.
|
|
952 bool done = true;
|
|
953 for (std::set<Node*>::iterator n = dsts.begin();
|
|
954 n != dsts.end();
|
|
955 ++n)
|
|
956 {
|
|
957 if ((*n)->object() == NULL
|
|
958 && ((*n)->expr() == NULL
|
|
959 || ((*n)->expr()->var_expression() == NULL
|
|
960 && (*n)->expr()->enclosed_var_expression() == NULL
|
|
961 && (*n)->expr()->func_expression() == NULL)))
|
|
962 continue;
|
|
963 if (escapes[*n] != (*n)->encoding())
|
|
964 {
|
|
965 done = false;
|
|
966 if (this->debug_escape_level() > 2)
|
|
967 go_inform((*n)->location(), "Reflooding %s %s",
|
|
968 debug_function_name((*n)->state(context, NULL)->fn).c_str(),
|
|
969 (*n)->ast_format(this).c_str());
|
|
970 escapes[*n] = (*n)->encoding();
|
|
971 this->propagate_escape(context, *n);
|
|
972 }
|
|
973 }
|
|
974 if (done)
|
|
975 break;
|
|
976 }
|
111
|
977
|
|
978 // Tag each exported function's parameters with escape information.
|
|
979 for (std::vector<Named_object*>::iterator fn = stack.begin();
|
|
980 fn != stack.end();
|
|
981 ++fn)
|
|
982 this->tag_function(context, *fn);
|
|
983
|
|
984 if (this->debug_escape_level() != 0)
|
|
985 {
|
|
986 std::vector<Node*> noesc = context->non_escaping_nodes();
|
|
987 for (std::vector<Node*>::const_iterator n = noesc.begin();
|
|
988 n != noesc.end();
|
|
989 ++n)
|
|
990 {
|
|
991 Node::Escape_state* state = (*n)->state(context, NULL);
|
131
|
992 if ((*n)->encoding() == Node::ESCAPE_NONE)
|
111
|
993 go_inform((*n)->location(), "%s %s does not escape",
|
|
994 strip_packed_prefix(this, debug_function_name(state->fn)).c_str(),
|
|
995 (*n)->ast_format(this).c_str());
|
|
996 }
|
|
997 }
|
|
998 delete context;
|
|
999 }
|
|
1000 }
|
|
1001
|
|
1002 // Traverse the program, discovering the functions that are roots of strongly
|
|
1003 // connected components. The goal of this phase to produce a set of functions
|
|
1004 // that must be analyzed in order.
|
|
1005
|
|
1006 class Escape_analysis_discover : public Traverse
|
|
1007 {
|
|
1008 public:
|
|
1009 Escape_analysis_discover(Gogo* gogo)
|
131
|
1010 : Traverse(traverse_functions | traverse_func_declarations),
|
111
|
1011 gogo_(gogo), component_ids_()
|
|
1012 { }
|
|
1013
|
|
1014 int
|
|
1015 function(Named_object*);
|
|
1016
|
|
1017 int
|
131
|
1018 function_declaration(Named_object*);
|
|
1019
|
|
1020 int
|
111
|
1021 visit(Named_object*);
|
|
1022
|
|
1023 int
|
|
1024 visit_code(Named_object*, int);
|
|
1025
|
|
1026 private:
|
|
1027 // A counter used to generate the ID for the function node in the graph.
|
|
1028 static int id;
|
|
1029
|
|
1030 // Type used to map functions to an ID in a graph of connected components.
|
|
1031 typedef Unordered_map(Named_object*, int) Component_ids;
|
|
1032
|
|
1033 // The Go IR.
|
|
1034 Gogo* gogo_;
|
|
1035 // The list of functions encountered during connected component discovery.
|
|
1036 Component_ids component_ids_;
|
|
1037 // The stack of functions that this component consists of.
|
|
1038 std::stack<Named_object*> stack_;
|
|
1039 };
|
|
1040
|
|
1041 int Escape_analysis_discover::id = 0;
|
|
1042
|
|
1043 // Visit each function.
|
|
1044
|
|
1045 int
|
|
1046 Escape_analysis_discover::function(Named_object* fn)
|
|
1047 {
|
|
1048 this->visit(fn);
|
|
1049 return TRAVERSE_CONTINUE;
|
|
1050 }
|
|
1051
|
131
|
1052 int
|
|
1053 Escape_analysis_discover::function_declaration(Named_object* fn)
|
|
1054 {
|
|
1055 this->visit(fn);
|
|
1056 return TRAVERSE_CONTINUE;
|
|
1057 }
|
|
1058
|
111
|
1059 // Visit a function FN, adding it to the current stack of functions
|
|
1060 // in this connected component. If this is the root of the component,
|
|
1061 // create a set of functions to be analyzed later.
|
|
1062 //
|
|
1063 // Finding these sets is finding strongly connected components
|
|
1064 // in the static call graph. The algorithm for doing that is taken
|
|
1065 // from Sedgewick, Algorithms, Second Edition, p. 482, with two
|
|
1066 // adaptations.
|
|
1067 //
|
|
1068 // First, a closure (fn->func_value()->enclosing() == NULL) cannot be the
|
|
1069 // root of a connected component. Refusing to use it as a root
|
|
1070 // forces it into the component of the function in which it appears.
|
|
1071 // This is more convenient for escape analysis.
|
|
1072 //
|
|
1073 // Second, each function becomes two virtual nodes in the graph,
|
|
1074 // with numbers n and n+1. We record the function's node number as n
|
|
1075 // but search from node n+1. If the search tells us that the component
|
|
1076 // number (min) is n+1, we know that this is a trivial component: one function
|
|
1077 // plus its closures. If the search tells us that the component number is
|
|
1078 // n, then there was a path from node n+1 back to node n, meaning that
|
|
1079 // the function set is mutually recursive. The escape analysis can be
|
|
1080 // more precise when analyzing a single non-recursive function than
|
|
1081 // when analyzing a set of mutually recursive functions.
|
|
1082
|
|
1083 int
|
|
1084 Escape_analysis_discover::visit(Named_object* fn)
|
|
1085 {
|
|
1086 Component_ids::const_iterator p = this->component_ids_.find(fn);
|
|
1087 if (p != this->component_ids_.end())
|
|
1088 // Already visited.
|
|
1089 return p->second;
|
|
1090
|
|
1091 this->id++;
|
|
1092 int id = this->id;
|
|
1093 this->component_ids_[fn] = id;
|
|
1094 this->id++;
|
|
1095 int min = this->id;
|
|
1096
|
|
1097 this->stack_.push(fn);
|
|
1098 min = this->visit_code(fn, min);
|
|
1099 if ((min == id || min == id + 1)
|
131
|
1100 && ((fn->is_function() && fn->func_value()->enclosing() == NULL)
|
|
1101 || fn->is_function_declaration()))
|
111
|
1102 {
|
|
1103 bool recursive = min == id;
|
|
1104 std::vector<Named_object*> group;
|
|
1105
|
|
1106 for (; !this->stack_.empty(); this->stack_.pop())
|
|
1107 {
|
|
1108 Named_object* n = this->stack_.top();
|
|
1109 if (n == fn)
|
|
1110 {
|
|
1111 this->stack_.pop();
|
|
1112 break;
|
|
1113 }
|
|
1114
|
|
1115 group.push_back(n);
|
|
1116 this->component_ids_[n] = std::numeric_limits<int>::max();
|
|
1117 }
|
|
1118 group.push_back(fn);
|
|
1119 this->component_ids_[fn] = std::numeric_limits<int>::max();
|
|
1120
|
|
1121 std::reverse(group.begin(), group.end());
|
|
1122 this->gogo_->add_analysis_set(group, recursive);
|
|
1123 }
|
|
1124
|
|
1125 return min;
|
|
1126 }
|
|
1127
|
|
1128 // Helper class for discovery step. Traverse expressions looking for
|
|
1129 // function calls and closures to visit during the discovery step.
|
|
1130
|
|
1131 class Escape_discover_expr : public Traverse
|
|
1132 {
|
|
1133 public:
|
|
1134 Escape_discover_expr(Escape_analysis_discover* ead, int min)
|
|
1135 : Traverse(traverse_expressions),
|
|
1136 ead_(ead), min_(min)
|
|
1137 { }
|
|
1138
|
|
1139 int
|
|
1140 min()
|
|
1141 { return this->min_; }
|
|
1142
|
|
1143 int
|
|
1144 expression(Expression** pexpr);
|
|
1145
|
|
1146 private:
|
|
1147 // The original discovery analysis.
|
|
1148 Escape_analysis_discover* ead_;
|
|
1149 // The minimum component ID in this group.
|
|
1150 int min_;
|
|
1151 };
|
|
1152
|
|
1153 // Visit any calls or closures found when discovering expressions.
|
|
1154
|
|
1155 int
|
|
1156 Escape_discover_expr::expression(Expression** pexpr)
|
|
1157 {
|
|
1158 Expression* e = *pexpr;
|
|
1159 Named_object* fn = NULL;
|
|
1160 if (e->call_expression() != NULL
|
|
1161 && e->call_expression()->fn()->func_expression() != NULL)
|
|
1162 {
|
|
1163 // Method call or function call.
|
|
1164 fn = e->call_expression()->fn()->func_expression()->named_object();
|
|
1165 }
|
|
1166 else if (e->func_expression() != NULL
|
|
1167 && e->func_expression()->closure() != NULL)
|
|
1168 {
|
|
1169 // Closure.
|
|
1170 fn = e->func_expression()->named_object();
|
|
1171 }
|
|
1172
|
|
1173 if (fn != NULL)
|
|
1174 this->min_ = std::min(this->min_, this->ead_->visit(fn));
|
|
1175 return TRAVERSE_CONTINUE;
|
|
1176 }
|
|
1177
|
|
1178 // Visit the body of each function, returns ID of the minimum connected
|
|
1179 // component found in the body.
|
|
1180
|
|
1181 int
|
|
1182 Escape_analysis_discover::visit_code(Named_object* fn, int min)
|
|
1183 {
|
|
1184 if (!fn->is_function())
|
|
1185 return min;
|
|
1186
|
|
1187 Escape_discover_expr ede(this, min);
|
|
1188 fn->func_value()->traverse(&ede);
|
|
1189 return ede.min();
|
|
1190 }
|
|
1191
|
|
1192 // Discover strongly connected groups of functions to analyze.
|
|
1193
|
|
1194 void
|
|
1195 Gogo::discover_analysis_sets()
|
|
1196 {
|
|
1197 Escape_analysis_discover ead(this);
|
|
1198 this->traverse(&ead);
|
|
1199 }
|
|
1200
|
|
1201 // Traverse all label and goto statements and mark the underlying label
|
|
1202 // as looping or not looping.
|
|
1203
|
|
1204 class Escape_analysis_loop : public Traverse
|
|
1205 {
|
|
1206 public:
|
|
1207 Escape_analysis_loop()
|
|
1208 : Traverse(traverse_statements)
|
|
1209 { }
|
|
1210
|
|
1211 int
|
|
1212 statement(Block*, size_t*, Statement*);
|
|
1213 };
|
|
1214
|
|
1215 int
|
|
1216 Escape_analysis_loop::statement(Block*, size_t*, Statement* s)
|
|
1217 {
|
|
1218 if (s->label_statement() != NULL)
|
|
1219 s->label_statement()->label()->set_nonlooping();
|
|
1220 else if (s->goto_statement() != NULL)
|
|
1221 {
|
|
1222 if (s->goto_statement()->label()->nonlooping())
|
|
1223 s->goto_statement()->label()->set_looping();
|
|
1224 }
|
|
1225 return TRAVERSE_CONTINUE;
|
|
1226 }
|
|
1227
|
|
1228 // Traversal class used to look at all interesting statements within a function
|
|
1229 // in order to build a connectivity graph between all nodes within a context's
|
|
1230 // scope.
|
|
1231
|
|
1232 class Escape_analysis_assign : public Traverse
|
|
1233 {
|
|
1234 public:
|
|
1235 Escape_analysis_assign(Escape_context* context, Named_object* fn)
|
|
1236 : Traverse(traverse_statements
|
|
1237 | traverse_expressions),
|
|
1238 context_(context), fn_(fn)
|
|
1239 { }
|
|
1240
|
|
1241 // Model statements within a function as assignments and flows between nodes.
|
|
1242 int
|
|
1243 statement(Block*, size_t*, Statement*);
|
|
1244
|
|
1245 // Model expressions within a function as assignments and flows between nodes.
|
|
1246 int
|
|
1247 expression(Expression**);
|
|
1248
|
|
1249 // Model calls within a function as assignments and flows between arguments
|
|
1250 // and results.
|
|
1251 void
|
|
1252 call(Call_expression* call);
|
|
1253
|
|
1254 // Model the assignment of DST to SRC.
|
|
1255 void
|
|
1256 assign(Node* dst, Node* src);
|
|
1257
|
|
1258 // Model the assignment of DST to dereference of SRC.
|
|
1259 void
|
|
1260 assign_deref(Node* dst, Node* src);
|
|
1261
|
|
1262 // Model the input-to-output assignment flow of one of a function call's
|
|
1263 // arguments, where the flow is encoding in NOTE.
|
|
1264 int
|
|
1265 assign_from_note(std::string* note, const std::vector<Node*>& dsts,
|
|
1266 Node* src);
|
|
1267
|
|
1268 // Record the flow of SRC to DST in DST.
|
|
1269 void
|
|
1270 flows(Node* dst, Node* src);
|
|
1271
|
|
1272 private:
|
|
1273 // The escape context for this set of functions.
|
|
1274 Escape_context* context_;
|
|
1275 // The current function being analyzed.
|
|
1276 Named_object* fn_;
|
|
1277 };
|
|
1278
|
131
|
1279 // Helper function to detect self assignment like the following.
|
|
1280 //
|
|
1281 // func (b *Buffer) Foo() {
|
|
1282 // n, m := ...
|
|
1283 // b.buf = b.buf[n:m]
|
|
1284 // }
|
|
1285
|
|
1286 static bool
|
|
1287 is_self_assignment(Expression* lhs, Expression* rhs)
|
|
1288 {
|
|
1289 Unary_expression* lue =
|
|
1290 (lhs->field_reference_expression() != NULL
|
|
1291 ? lhs->field_reference_expression()->expr()->unary_expression()
|
|
1292 : lhs->unary_expression());
|
|
1293 Var_expression* lve =
|
|
1294 (lue != NULL && lue->op() == OPERATOR_MULT ? lue->operand()->var_expression() : NULL);
|
|
1295 Array_index_expression* raie = rhs->array_index_expression();
|
|
1296 String_index_expression* rsie = rhs->string_index_expression();
|
|
1297 Expression* rarray =
|
|
1298 (raie != NULL && raie->end() != NULL && raie->array()->type()->is_slice_type()
|
|
1299 ? raie->array()
|
|
1300 : (rsie != NULL && rsie->type()->is_string_type() ? rsie->string() : NULL));
|
|
1301 Unary_expression* rue =
|
|
1302 (rarray != NULL && rarray->field_reference_expression() != NULL
|
|
1303 ? rarray->field_reference_expression()->expr()->unary_expression()
|
|
1304 : (rarray != NULL ? rarray->unary_expression() : NULL));
|
|
1305 Var_expression* rve =
|
|
1306 (rue != NULL && rue->op() == OPERATOR_MULT ? rue->operand()->var_expression() : NULL);
|
|
1307 return lve != NULL && rve != NULL
|
|
1308 && lve->named_object() == rve->named_object();
|
|
1309 }
|
|
1310
|
111
|
1311 // Model statements within a function as assignments and flows between nodes.
|
|
1312
|
|
1313 int
|
|
1314 Escape_analysis_assign::statement(Block*, size_t*, Statement* s)
|
|
1315 {
|
|
1316 // Adjust the loop depth as we enter/exit blocks related to for statements.
|
|
1317 bool is_for_statement = (s->is_block_statement()
|
|
1318 && s->block_statement()->is_lowered_for_statement());
|
|
1319 if (is_for_statement)
|
|
1320 this->context_->increase_loop_depth();
|
|
1321
|
|
1322 s->traverse_contents(this);
|
|
1323
|
|
1324 if (is_for_statement)
|
|
1325 this->context_->decrease_loop_depth();
|
|
1326
|
|
1327 Gogo* gogo = this->context_->gogo();
|
|
1328 int debug_level = gogo->debug_escape_level();
|
|
1329 if (debug_level > 1
|
|
1330 && s->unnamed_label_statement() == NULL
|
|
1331 && s->expression_statement() == NULL
|
|
1332 && !s->is_block_statement())
|
|
1333 {
|
|
1334 Node* n = Node::make_node(s);
|
|
1335 std::string fn_name = this->context_->current_function_name();
|
|
1336 go_inform(s->location(), "[%d] %s esc: %s",
|
|
1337 this->context_->loop_depth(), fn_name.c_str(),
|
|
1338 n->ast_format(gogo).c_str());
|
|
1339 }
|
|
1340
|
|
1341 switch (s->classification())
|
|
1342 {
|
|
1343 case Statement::STATEMENT_VARIABLE_DECLARATION:
|
|
1344 {
|
|
1345 Named_object* var = s->variable_declaration_statement()->var();
|
|
1346 Node* var_node = Node::make_node(var);
|
|
1347 Node::Escape_state* state = var_node->state(this->context_, NULL);
|
|
1348 state->loop_depth = this->context_->loop_depth();
|
|
1349
|
|
1350 // Set the loop depth for this declaration.
|
|
1351 if (var->is_variable()
|
|
1352 && var->var_value()->init() != NULL)
|
|
1353 {
|
|
1354 Node* init_node = Node::make_node(var->var_value()->init());
|
|
1355 this->assign(var_node, init_node);
|
|
1356 }
|
|
1357 }
|
|
1358 break;
|
|
1359
|
131
|
1360 case Statement::STATEMENT_TEMPORARY:
|
|
1361 {
|
|
1362 Expression* init = s->temporary_statement()->init();
|
|
1363 if (init != NULL)
|
|
1364 {
|
|
1365 Node* n = Node::make_node(init);
|
|
1366 if (s->temporary_statement()->value_escapes())
|
|
1367 this->assign(this->context_->sink(), n);
|
|
1368 else
|
|
1369 this->assign(Node::make_node(s), n);
|
|
1370 }
|
|
1371 }
|
|
1372 break;
|
|
1373
|
111
|
1374 case Statement::STATEMENT_LABEL:
|
|
1375 {
|
|
1376 Label_statement* label_stmt = s->label_statement();
|
|
1377 if (label_stmt->label()->looping())
|
|
1378 this->context_->increase_loop_depth();
|
|
1379
|
|
1380 if (debug_level > 1)
|
|
1381 {
|
|
1382 std::string label_type = (label_stmt->label()->looping()
|
|
1383 ? "looping"
|
|
1384 : "nonlooping");
|
|
1385 go_inform(s->location(), "%s %s label",
|
|
1386 label_stmt->label()->name().c_str(),
|
|
1387 label_type.c_str());
|
|
1388 }
|
|
1389 }
|
|
1390 break;
|
|
1391
|
|
1392 case Statement::STATEMENT_SWITCH:
|
|
1393 case Statement::STATEMENT_TYPE_SWITCH:
|
|
1394 // Want to model the assignment of each case variable to the switched upon
|
|
1395 // variable. This should be lowered into assignment statements; nothing
|
|
1396 // to here if that's the case.
|
|
1397 break;
|
|
1398
|
|
1399 case Statement::STATEMENT_ASSIGNMENT:
|
|
1400 {
|
|
1401 Assignment_statement* assn = s->assignment_statement();
|
131
|
1402 Expression* lhs = assn->lhs();
|
|
1403 Expression* rhs = assn->rhs();
|
|
1404 Node* lhs_node = Node::make_node(lhs);
|
|
1405 Node* rhs_node = Node::make_node(rhs);
|
|
1406
|
|
1407 // Filter out the following special case.
|
|
1408 //
|
|
1409 // func (b *Buffer) Foo() {
|
|
1410 // n, m := ...
|
|
1411 // b.buf = b.buf[n:m]
|
|
1412 // }
|
|
1413 //
|
|
1414 // This assignment is a no-op for escape analysis,
|
|
1415 // it does not store any new pointers into b that were not already there.
|
|
1416 // However, without this special case b will escape.
|
|
1417 if (is_self_assignment(lhs, rhs))
|
|
1418 {
|
|
1419 if (debug_level != 0)
|
|
1420 go_inform(s->location(), "%s ignoring self-assignment to %s",
|
|
1421 strip_packed_prefix(gogo, this->context_->current_function_name()).c_str(),
|
|
1422 lhs_node->ast_format(gogo).c_str());
|
|
1423 break;
|
|
1424 }
|
|
1425
|
|
1426 this->assign(lhs_node, rhs_node);
|
111
|
1427 }
|
|
1428 break;
|
|
1429
|
|
1430 case Statement::STATEMENT_SEND:
|
|
1431 {
|
|
1432 Node* sent_node = Node::make_node(s->send_statement()->val());
|
|
1433 this->assign(this->context_->sink(), sent_node);
|
|
1434 }
|
|
1435 break;
|
|
1436
|
|
1437 case Statement::STATEMENT_DEFER:
|
|
1438 if (this->context_->loop_depth() == 1)
|
131
|
1439 {
|
|
1440 // Defer statement may need to allocate a thunk. When it is
|
|
1441 // not inside a loop, this can be stack allocated, as it
|
|
1442 // runs before the function finishes.
|
|
1443 Node* n = Node::make_node(s);
|
|
1444 n->set_encoding(Node::ESCAPE_NONE);
|
|
1445 break;
|
|
1446 }
|
111
|
1447 // fallthrough
|
|
1448
|
|
1449 case Statement::STATEMENT_GO:
|
|
1450 {
|
|
1451 // Defer f(x) or go f(x).
|
|
1452 // Both f and x escape to the heap.
|
|
1453 Thunk_statement* thunk = s->thunk_statement();
|
|
1454 if (thunk->call()->call_expression() == NULL)
|
|
1455 break;
|
|
1456
|
|
1457 Call_expression* call = thunk->call()->call_expression();
|
|
1458 Node* func_node = Node::make_node(call->fn());
|
|
1459 this->assign(this->context_->sink(), func_node);
|
|
1460 if (call->args() != NULL)
|
|
1461 {
|
|
1462 for (Expression_list::const_iterator p = call->args()->begin();
|
|
1463 p != call->args()->end();
|
|
1464 ++p)
|
|
1465 {
|
|
1466 Node* arg_node = Node::make_node(*p);
|
|
1467 this->assign(this->context_->sink(), arg_node);
|
|
1468 }
|
|
1469 }
|
|
1470 }
|
|
1471 break;
|
|
1472
|
|
1473 default:
|
|
1474 break;
|
|
1475 }
|
|
1476 return TRAVERSE_SKIP_COMPONENTS;
|
|
1477 }
|
|
1478
|
131
|
1479 // Helper function to emit moved-to-heap diagnostics.
|
|
1480
|
|
1481 static void
|
|
1482 move_to_heap(Gogo* gogo, Expression *expr)
|
|
1483 {
|
|
1484 Named_object* no;
|
|
1485 if (expr->var_expression() != NULL)
|
|
1486 no = expr->var_expression()->named_object();
|
|
1487 else if (expr->enclosed_var_expression() != NULL)
|
|
1488 no = expr->enclosed_var_expression()->variable();
|
|
1489 else
|
|
1490 return;
|
|
1491
|
|
1492 if ((no->is_variable()
|
|
1493 && !no->var_value()->is_global())
|
|
1494 || no->is_result_variable())
|
|
1495 {
|
|
1496 Node* n = Node::make_node(expr);
|
|
1497 if (gogo->debug_escape_level() != 0)
|
|
1498 go_inform(n->definition_location(),
|
|
1499 "moved to heap: %s",
|
|
1500 n->ast_format(gogo).c_str());
|
|
1501 if (gogo->compiling_runtime() && gogo->package_name() == "runtime")
|
|
1502 go_error_at(expr->location(),
|
|
1503 "%s escapes to heap, not allowed in runtime",
|
|
1504 n->ast_format(gogo).c_str());
|
|
1505 }
|
|
1506 }
|
|
1507
|
111
|
1508 // Model expressions within a function as assignments and flows between nodes.
|
|
1509
|
|
1510 int
|
|
1511 Escape_analysis_assign::expression(Expression** pexpr)
|
|
1512 {
|
|
1513 Gogo* gogo = this->context_->gogo();
|
|
1514 int debug_level = gogo->debug_escape_level();
|
|
1515
|
|
1516 // Big stuff escapes unconditionally.
|
|
1517 Node* n = Node::make_node(*pexpr);
|
|
1518 if ((n->encoding() & ESCAPE_MASK) != int(Node::ESCAPE_HEAP)
|
|
1519 && n->is_big(this->context_))
|
|
1520 {
|
|
1521 if (debug_level > 1)
|
131
|
1522 go_inform((*pexpr)->location(), "%s too large for stack",
|
|
1523 n->ast_format(gogo).c_str());
|
|
1524 move_to_heap(gogo, *pexpr);
|
111
|
1525 n->set_encoding(Node::ESCAPE_HEAP);
|
|
1526 (*pexpr)->address_taken(true);
|
|
1527 this->assign(this->context_->sink(), n);
|
|
1528 }
|
|
1529
|
|
1530 if ((*pexpr)->func_expression() == NULL)
|
|
1531 (*pexpr)->traverse_subexpressions(this);
|
|
1532
|
|
1533 if (debug_level > 1)
|
|
1534 {
|
|
1535 Node* n = Node::make_node(*pexpr);
|
|
1536 std::string fn_name = this->context_->current_function_name();
|
|
1537 go_inform((*pexpr)->location(), "[%d] %s esc: %s",
|
|
1538 this->context_->loop_depth(), fn_name.c_str(),
|
|
1539 n->ast_format(gogo).c_str());
|
|
1540 }
|
|
1541
|
|
1542 switch ((*pexpr)->classification())
|
|
1543 {
|
|
1544 case Expression::EXPRESSION_CALL:
|
|
1545 {
|
|
1546 Call_expression* call = (*pexpr)->call_expression();
|
131
|
1547 if (call->is_builtin())
|
|
1548 {
|
|
1549 Builtin_call_expression* bce = call->builtin_call_expression();
|
|
1550 switch (bce->code())
|
|
1551 {
|
|
1552 case Builtin_call_expression::BUILTIN_PANIC:
|
|
1553 {
|
|
1554 // Argument could leak through recover.
|
|
1555 Node* panic_arg = Node::make_node(call->args()->front());
|
|
1556 this->assign(this->context_->sink(), panic_arg);
|
|
1557 }
|
|
1558 break;
|
|
1559
|
|
1560 case Builtin_call_expression::BUILTIN_APPEND:
|
|
1561 {
|
|
1562 // The contents being appended leak.
|
|
1563 if (call->is_varargs())
|
|
1564 {
|
|
1565 // append(slice1, slice2...) -- slice2 itself does not escape, but contents do
|
|
1566 Node* appended = Node::make_node(call->args()->back());
|
|
1567 this->assign_deref(this->context_->sink(), appended);
|
|
1568 if (debug_level > 2)
|
|
1569 go_inform((*pexpr)->location(),
|
|
1570 "special treatment of append(slice1, slice2...)");
|
|
1571 }
|
|
1572 else
|
|
1573 {
|
|
1574 for (Expression_list::const_iterator pa =
|
|
1575 call->args()->begin() + 1;
|
|
1576 pa != call->args()->end();
|
|
1577 ++pa)
|
|
1578 {
|
|
1579 Node* arg = Node::make_node(*pa);
|
|
1580 this->assign(this->context_->sink(), arg);
|
|
1581 }
|
|
1582 }
|
|
1583
|
|
1584 // The content of the original slice leaks as well.
|
|
1585 Node* appendee = Node::make_node(call->args()->front());
|
|
1586 this->assign_deref(this->context_->sink(), appendee);
|
|
1587 }
|
|
1588 break;
|
|
1589
|
|
1590 case Builtin_call_expression::BUILTIN_COPY:
|
|
1591 {
|
|
1592 // Lose track of the copied content.
|
|
1593 Node* copied = Node::make_node(call->args()->back());
|
|
1594 this->assign_deref(this->context_->sink(), copied);
|
|
1595 }
|
|
1596 break;
|
|
1597
|
|
1598 default:
|
|
1599 break;
|
|
1600 }
|
|
1601 break;
|
|
1602 }
|
111
|
1603 Func_expression* fe = call->fn()->func_expression();
|
|
1604 if (fe != NULL && fe->is_runtime_function())
|
|
1605 {
|
|
1606 switch (fe->runtime_code())
|
|
1607 {
|
|
1608 case Runtime::MAKECHAN:
|
131
|
1609 case Runtime::MAKECHAN64:
|
111
|
1610 case Runtime::MAKEMAP:
|
|
1611 case Runtime::MAKESLICE:
|
|
1612 case Runtime::MAKESLICE64:
|
131
|
1613 this->context_->track(n);
|
111
|
1614 break;
|
|
1615
|
131
|
1616 case Runtime::MAPASSIGN:
|
|
1617 {
|
|
1618 // Map key escapes. The last argument is the address
|
|
1619 // of the key.
|
|
1620 Node* key_node = Node::make_node(call->args()->back());
|
|
1621 this->assign_deref(this->context_->sink(), key_node);
|
|
1622 }
|
|
1623 break;
|
|
1624
|
|
1625 case Runtime::IFACEE2T2:
|
|
1626 case Runtime::IFACEI2T2:
|
|
1627 {
|
|
1628 // x, ok = v.(T), where T is non-pointer non-interface,
|
|
1629 // is lowered to
|
|
1630 // ok = IFACEI2T2(type, v, (void*)&tmp_x)
|
|
1631 // Here v flows to tmp_x.
|
|
1632 // Note: other IFACEX2Y2 returns the conversion result.
|
|
1633 // Those are handled in ::assign.
|
|
1634 Node* src_node = Node::make_node(call->args()->at(1));
|
|
1635 Node* dst_node;
|
|
1636 Expression* arg2 = call->args()->at(2);
|
|
1637 // Try to pull tmp_x out of the arg2 expression, and let v
|
|
1638 // flows into it, instead of simply dereference arg2,
|
|
1639 // which looks like dereference of an arbitrary pointer
|
|
1640 // and causes v immediately escape.
|
|
1641 // The expression form matches statement.cc,
|
|
1642 // Tuple_type_guard_assignment_statement::lower_to_object_type.
|
|
1643 Unary_expression* ue =
|
|
1644 (arg2->conversion_expression() != NULL
|
|
1645 ? arg2->conversion_expression()->expr()->unary_expression()
|
|
1646 : arg2->unary_expression());
|
|
1647 if (ue != NULL && ue->op() == OPERATOR_AND)
|
|
1648 {
|
|
1649 if (!ue->operand()->type()->has_pointer())
|
|
1650 // Don't bother flowing non-pointer.
|
|
1651 break;
|
|
1652 dst_node = Node::make_node(ue->operand());
|
|
1653 }
|
|
1654 else
|
|
1655 dst_node = this->context_->add_dereference(Node::make_node(arg2));
|
|
1656 this->assign(dst_node, src_node);
|
|
1657 }
|
|
1658 break;
|
|
1659
|
111
|
1660 default:
|
|
1661 break;
|
|
1662 }
|
|
1663 }
|
131
|
1664 else
|
|
1665 this->call(call);
|
111
|
1666 }
|
|
1667 break;
|
|
1668
|
|
1669 case Expression::EXPRESSION_ALLOCATION:
|
131
|
1670 // This is Runtime::NEW.
|
|
1671 this->context_->track(n);
|
|
1672 break;
|
|
1673
|
|
1674 case Expression::EXPRESSION_STRING_CONCAT:
|
|
1675 this->context_->track(n);
|
111
|
1676 break;
|
|
1677
|
|
1678 case Expression::EXPRESSION_CONVERSION:
|
|
1679 {
|
|
1680 Type_conversion_expression* tce = (*pexpr)->conversion_expression();
|
131
|
1681 Type* ft = tce->expr()->type();
|
|
1682 Type* tt = tce->type();
|
|
1683 if ((ft->is_string_type() && tt->is_slice_type())
|
|
1684 || (ft->is_slice_type() && tt->is_string_type())
|
|
1685 || (ft->integer_type() != NULL && tt->is_string_type()))
|
|
1686 {
|
|
1687 // string([]byte), string([]rune), []byte(string), []rune(string), string(rune)
|
|
1688 this->context_->track(n);
|
|
1689 break;
|
|
1690 }
|
111
|
1691 Node* tce_node = Node::make_node(tce);
|
|
1692 Node* converted = Node::make_node(tce->expr());
|
|
1693 this->context_->track(tce_node);
|
|
1694
|
|
1695 this->assign(tce_node, converted);
|
|
1696 }
|
|
1697 break;
|
|
1698
|
|
1699 case Expression::EXPRESSION_FIXED_ARRAY_CONSTRUCTION:
|
|
1700 case Expression::EXPRESSION_SLICE_CONSTRUCTION:
|
|
1701 {
|
|
1702 Node* array_node = Node::make_node(*pexpr);
|
|
1703 if ((*pexpr)->slice_literal() != NULL)
|
|
1704 this->context_->track(array_node);
|
|
1705
|
|
1706 Expression_list* vals = ((*pexpr)->slice_literal() != NULL
|
|
1707 ? (*pexpr)->slice_literal()->vals()
|
|
1708 : (*pexpr)->array_literal()->vals());
|
|
1709
|
|
1710 if (vals != NULL)
|
|
1711 {
|
|
1712 // Connect the array to its values.
|
|
1713 for (Expression_list::const_iterator p = vals->begin();
|
|
1714 p != vals->end();
|
|
1715 ++p)
|
|
1716 if ((*p) != NULL)
|
|
1717 this->assign(array_node, Node::make_node(*p));
|
|
1718 }
|
|
1719 }
|
|
1720 break;
|
|
1721
|
|
1722 case Expression::EXPRESSION_STRUCT_CONSTRUCTION:
|
|
1723 {
|
|
1724 Node* struct_node = Node::make_node(*pexpr);
|
|
1725 Expression_list* vals = (*pexpr)->struct_literal()->vals();
|
|
1726 if (vals != NULL)
|
|
1727 {
|
|
1728 // Connect the struct to its values.
|
|
1729 for (Expression_list::const_iterator p = vals->begin();
|
|
1730 p != vals->end();
|
|
1731 ++p)
|
|
1732 {
|
|
1733 if ((*p) != NULL)
|
|
1734 this->assign(struct_node, Node::make_node(*p));
|
|
1735 }
|
|
1736 }
|
|
1737 }
|
|
1738 break;
|
|
1739
|
|
1740 case Expression::EXPRESSION_HEAP:
|
|
1741 {
|
|
1742 Node* pointer_node = Node::make_node(*pexpr);
|
|
1743 Node* lit_node = Node::make_node((*pexpr)->heap_expression()->expr());
|
|
1744 this->context_->track(pointer_node);
|
|
1745
|
|
1746 // Connect pointer node to literal node; if the pointer node escapes, so
|
|
1747 // does the literal node.
|
|
1748 this->assign(pointer_node, lit_node);
|
|
1749 }
|
|
1750 break;
|
|
1751
|
|
1752 case Expression::EXPRESSION_BOUND_METHOD:
|
|
1753 {
|
|
1754 Node* bound_node = Node::make_node(*pexpr);
|
|
1755 this->context_->track(bound_node);
|
|
1756
|
|
1757 Expression* obj = (*pexpr)->bound_method_expression()->first_argument();
|
|
1758 Node* obj_node = Node::make_node(obj);
|
|
1759
|
|
1760 // A bound method implies the receiver will be used outside of the
|
|
1761 // lifetime of the method in some way. We lose track of the receiver.
|
|
1762 this->assign(this->context_->sink(), obj_node);
|
|
1763 }
|
|
1764 break;
|
|
1765
|
|
1766 case Expression::EXPRESSION_MAP_CONSTRUCTION:
|
|
1767 {
|
|
1768 Map_construction_expression* mce = (*pexpr)->map_literal();
|
|
1769 Node* map_node = Node::make_node(mce);
|
|
1770 this->context_->track(map_node);
|
|
1771
|
|
1772 // All keys and values escape to memory.
|
|
1773 if (mce->vals() != NULL)
|
|
1774 {
|
|
1775 for (Expression_list::const_iterator p = mce->vals()->begin();
|
|
1776 p != mce->vals()->end();
|
|
1777 ++p)
|
|
1778 {
|
|
1779 if ((*p) != NULL)
|
|
1780 this->assign(this->context_->sink(), Node::make_node(*p));
|
|
1781 }
|
|
1782 }
|
|
1783 }
|
|
1784 break;
|
|
1785
|
|
1786 case Expression::EXPRESSION_FUNC_REFERENCE:
|
|
1787 {
|
|
1788 Func_expression* fe = (*pexpr)->func_expression();
|
|
1789 if (fe->closure() != NULL)
|
|
1790 {
|
|
1791 // Connect captured variables to the closure.
|
|
1792 Node* closure_node = Node::make_node(fe);
|
|
1793 this->context_->track(closure_node);
|
|
1794
|
|
1795 // A closure expression already exists as the heap expression:
|
|
1796 // &struct{f func_code, v []*Variable}{...}.
|
|
1797 // Link closure to the addresses of the variables enclosed.
|
|
1798 Heap_expression* he = fe->closure()->heap_expression();
|
|
1799 Struct_construction_expression* sce = he->expr()->struct_literal();
|
|
1800
|
|
1801 // First field is function code, other fields are variable
|
|
1802 // references.
|
|
1803 Expression_list::const_iterator p = sce->vals()->begin();
|
|
1804 ++p;
|
|
1805 for (; p != sce->vals()->end(); ++p)
|
|
1806 {
|
|
1807 Node* enclosed_node = Node::make_node(*p);
|
131
|
1808 this->context_->track(enclosed_node);
|
111
|
1809 this->assign(closure_node, enclosed_node);
|
|
1810 }
|
|
1811 }
|
|
1812 }
|
|
1813 break;
|
|
1814
|
|
1815 case Expression::EXPRESSION_UNARY:
|
|
1816 {
|
|
1817 Expression* operand = (*pexpr)->unary_expression()->operand();
|
131
|
1818
|
|
1819 if ((*pexpr)->unary_expression()->op() == OPERATOR_AND)
|
|
1820 {
|
|
1821 this->context_->track(n);
|
|
1822
|
|
1823 Named_object* var = NULL;
|
|
1824 if (operand->var_expression() != NULL)
|
|
1825 var = operand->var_expression()->named_object();
|
|
1826 else if (operand->enclosed_var_expression() != NULL)
|
|
1827 var = operand->enclosed_var_expression()->variable();
|
|
1828
|
|
1829 if (var != NULL
|
|
1830 && ((var->is_variable() && var->var_value()->is_parameter())
|
|
1831 || var->is_result_variable()))
|
|
1832 {
|
|
1833 Node::Escape_state* addr_state = n->state(this->context_, NULL);
|
|
1834 addr_state->loop_depth = 1;
|
|
1835 break;
|
|
1836 }
|
|
1837 }
|
|
1838
|
|
1839 if ((*pexpr)->unary_expression()->op() != OPERATOR_AND
|
|
1840 && (*pexpr)->unary_expression()->op() != OPERATOR_MULT)
|
|
1841 break;
|
|
1842
|
|
1843 // For &x and *x, use the loop depth of x if known.
|
|
1844 Node::Escape_state* expr_state = n->state(this->context_, NULL);
|
|
1845 Node* operand_node = Node::make_node(operand);
|
|
1846 Node::Escape_state* operand_state = operand_node->state(this->context_, NULL);
|
|
1847 if (operand_state->loop_depth != 0)
|
|
1848 expr_state->loop_depth = operand_state->loop_depth;
|
|
1849 }
|
|
1850 break;
|
|
1851
|
|
1852 case Expression::EXPRESSION_ARRAY_INDEX:
|
|
1853 {
|
|
1854 Array_index_expression* aie = (*pexpr)->array_index_expression();
|
|
1855
|
|
1856 // Propagate the loopdepth to element.
|
|
1857 Node* array_node = Node::make_node(aie->array());
|
|
1858 Node::Escape_state* elem_state = n->state(this->context_, NULL);
|
|
1859 Node::Escape_state* array_state = array_node->state(this->context_, NULL);
|
|
1860 elem_state->loop_depth = array_state->loop_depth;
|
|
1861
|
|
1862 if (aie->end() != NULL && !aie->array()->type()->is_slice_type())
|
|
1863 {
|
|
1864 // Slicing an array. This effectively takes the address of the array.
|
|
1865 Expression* addr = Expression::make_unary(OPERATOR_AND, aie->array(),
|
|
1866 aie->location());
|
|
1867 Node* addr_node = Node::make_node(addr);
|
|
1868 n->set_child(addr_node);
|
|
1869 this->context_->track(addr_node);
|
|
1870
|
|
1871 Node::Escape_state* addr_state = addr_node->state(this->context_, NULL);
|
|
1872 addr_state->loop_depth = array_state->loop_depth;
|
|
1873 }
|
|
1874 }
|
|
1875 break;
|
|
1876
|
|
1877 case Expression::EXPRESSION_FIELD_REFERENCE:
|
|
1878 {
|
|
1879 // Propagate the loopdepth to field.
|
|
1880 Node* struct_node =
|
|
1881 Node::make_node((*pexpr)->field_reference_expression()->expr());
|
|
1882 Node::Escape_state* field_state = n->state(this->context_, NULL);
|
|
1883 Node::Escape_state* struct_state = struct_node->state(this->context_, NULL);
|
|
1884 field_state->loop_depth = struct_state->loop_depth;
|
111
|
1885 }
|
|
1886 break;
|
|
1887
|
|
1888 default:
|
|
1889 break;
|
|
1890 }
|
|
1891 return TRAVERSE_SKIP_COMPONENTS;
|
|
1892 }
|
|
1893
|
|
1894 // Model calls within a function as assignments and flows between arguments
|
|
1895 // and results.
|
|
1896
|
|
1897 void
|
|
1898 Escape_analysis_assign::call(Call_expression* call)
|
|
1899 {
|
|
1900 Gogo* gogo = this->context_->gogo();
|
|
1901 int debug_level = gogo->debug_escape_level();
|
|
1902
|
|
1903 Func_expression* fn = call->fn()->func_expression();
|
|
1904 Function_type* fntype = call->get_function_type();
|
|
1905 bool indirect = false;
|
|
1906
|
|
1907 // Interface method calls or closure calls are indirect calls.
|
|
1908 if (fntype == NULL
|
|
1909 || (fntype->is_method()
|
|
1910 && fntype->receiver()->type()->interface_type() != NULL)
|
|
1911 || fn == NULL
|
|
1912 || (fn->named_object()->is_function()
|
|
1913 && fn->named_object()->func_value()->enclosing() != NULL))
|
|
1914 indirect = true;
|
|
1915
|
|
1916 Node* call_node = Node::make_node(call);
|
|
1917 std::vector<Node*> arg_nodes;
|
|
1918 if (call->fn()->interface_field_reference_expression() != NULL)
|
|
1919 {
|
|
1920 Interface_field_reference_expression* ifre =
|
|
1921 call->fn()->interface_field_reference_expression();
|
|
1922 Node* field_node = Node::make_node(ifre->expr());
|
|
1923 arg_nodes.push_back(field_node);
|
|
1924 }
|
|
1925
|
|
1926 if (call->args() != NULL)
|
|
1927 {
|
|
1928 for (Expression_list::const_iterator p = call->args()->begin();
|
|
1929 p != call->args()->end();
|
|
1930 ++p)
|
|
1931 arg_nodes.push_back(Node::make_node(*p));
|
|
1932 }
|
|
1933
|
|
1934 if (indirect)
|
|
1935 {
|
|
1936 // We don't know what happens to the parameters through indirect calls.
|
|
1937 // Be conservative and assume they all flow to theSink.
|
|
1938 for (std::vector<Node*>::iterator p = arg_nodes.begin();
|
|
1939 p != arg_nodes.end();
|
|
1940 ++p)
|
|
1941 {
|
|
1942 if (debug_level > 2)
|
|
1943 go_inform(call->location(),
|
|
1944 "esccall:: indirect call <- %s, untracked",
|
|
1945 (*p)->ast_format(gogo).c_str());
|
|
1946 this->assign(this->context_->sink(), *p);
|
|
1947 }
|
|
1948
|
|
1949 this->context_->init_retvals(call_node, fntype);
|
131
|
1950
|
|
1951 // It could be a closure call that returns captured variable.
|
|
1952 // Model this by flowing the func expression to result.
|
|
1953 // See issue #14409.
|
|
1954 Node* fn_node = Node::make_node(call->fn());
|
|
1955 std::vector<Node*> retvals = call_node->state(this->context_, NULL)->retvals;
|
|
1956 for (std::vector<Node*>::const_iterator p = retvals.begin();
|
|
1957 p != retvals.end();
|
|
1958 ++p)
|
|
1959 this->assign_deref(*p, fn_node);
|
|
1960
|
111
|
1961 return;
|
|
1962 }
|
|
1963
|
|
1964 // If FN is an untagged function.
|
|
1965 if (fn != NULL
|
|
1966 && fn->named_object()->is_function()
|
|
1967 && !fntype->is_tagged())
|
|
1968 {
|
|
1969 if (debug_level > 2)
|
|
1970 go_inform(call->location(), "esccall:: %s in recursive group",
|
|
1971 call_node->ast_format(gogo).c_str());
|
|
1972
|
|
1973 Function* f = fn->named_object()->func_value();
|
|
1974 const Bindings* callee_bindings = f->block()->bindings();
|
131
|
1975 Function::Results* results = f->result_variables();
|
111
|
1976 if (results != NULL)
|
|
1977 {
|
|
1978 // Setup output list on this call node.
|
|
1979 Node::Escape_state* state = call_node->state(this->context_, NULL);
|
131
|
1980 for (Function::Results::const_iterator p1 = results->begin();
|
111
|
1981 p1 != results->end();
|
|
1982 ++p1)
|
|
1983 {
|
131
|
1984 Node* result_node = Node::make_node(*p1);
|
111
|
1985 state->retvals.push_back(result_node);
|
|
1986 }
|
|
1987 }
|
|
1988
|
|
1989 std::vector<Node*>::iterator p = arg_nodes.begin();
|
131
|
1990 if (fntype->is_method())
|
111
|
1991 {
|
|
1992 std::string rcvr_name = fntype->receiver()->name();
|
131
|
1993 if (rcvr_name.empty() || Gogo::is_sink_name(rcvr_name)
|
|
1994 || !fntype->receiver()->type()->has_pointer())
|
111
|
1995 ;
|
|
1996 else
|
|
1997 {
|
|
1998 Named_object* rcvr_no =
|
|
1999 callee_bindings->lookup_local(fntype->receiver()->name());
|
|
2000 go_assert(rcvr_no != NULL);
|
|
2001 Node* rcvr_node = Node::make_node(rcvr_no);
|
131
|
2002 if (fntype->receiver()->type()->points_to() == NULL
|
|
2003 && (*p)->expr()->type()->points_to() != NULL)
|
|
2004 // This is a call to a value method that has been lowered into a call
|
|
2005 // to a pointer method. Gccgo generates a pointer method for all
|
|
2006 // method calls and takes the address of the value passed as the
|
|
2007 // receiver then immediately dereferences it within the function.
|
|
2008 // In this case, the receiver address does not escape; its content
|
|
2009 // flows to the call.
|
|
2010 this->assign_deref(rcvr_node, *p);
|
|
2011 else
|
|
2012 this->assign(rcvr_node, *p);
|
111
|
2013 }
|
|
2014 ++p;
|
|
2015 }
|
|
2016
|
|
2017 const Typed_identifier_list* til = fntype->parameters();
|
|
2018 if (til != NULL)
|
|
2019 {
|
|
2020 for (Typed_identifier_list::const_iterator p1 = til->begin();
|
|
2021 p1 != til->end();
|
|
2022 ++p1, ++p)
|
|
2023 {
|
|
2024 if (p1->name().empty() || Gogo::is_sink_name(p1->name()))
|
|
2025 continue;
|
|
2026
|
|
2027 Named_object* param_no =
|
|
2028 callee_bindings->lookup_local(p1->name());
|
|
2029 go_assert(param_no != NULL);
|
|
2030 Expression* arg = (*p)->expr();
|
|
2031 if (arg->var_expression() != NULL
|
|
2032 && arg->var_expression()->named_object() == param_no)
|
|
2033 continue;
|
|
2034
|
|
2035 Node* param_node = Node::make_node(param_no);
|
|
2036 this->assign(param_node, *p);
|
|
2037 }
|
|
2038
|
|
2039 for (; p != arg_nodes.end(); ++p)
|
|
2040 {
|
|
2041 if (debug_level > 2)
|
|
2042 go_inform(call->location(), "esccall:: ... <- %s, untracked",
|
|
2043 (*p)->ast_format(gogo).c_str());
|
|
2044 this->assign(this->context_->sink(), *p);
|
|
2045 }
|
|
2046 }
|
|
2047
|
|
2048 return;
|
|
2049 }
|
|
2050
|
|
2051 if (debug_level > 2)
|
|
2052 go_inform(call->location(), "esccall:: %s not recursive",
|
|
2053 call_node->ast_format(gogo).c_str());
|
|
2054
|
|
2055 Node::Escape_state* call_state = call_node->state(this->context_, NULL);
|
|
2056 if (!call_state->retvals.empty())
|
|
2057 go_error_at(Linemap::unknown_location(),
|
|
2058 "esc already decorated call %s",
|
|
2059 call_node->ast_format(gogo).c_str());
|
|
2060 this->context_->init_retvals(call_node, fntype);
|
|
2061
|
|
2062 // Receiver.
|
|
2063 std::vector<Node*>::iterator p = arg_nodes.begin();
|
|
2064 if (fntype->is_method()
|
|
2065 && p != arg_nodes.end())
|
|
2066 {
|
|
2067 // First argument to call will be the receiver.
|
|
2068 std::string* note = fntype->receiver()->note();
|
|
2069 if (fntype->receiver()->type()->points_to() == NULL
|
131
|
2070 && (*p)->expr()->type()->points_to() != NULL)
|
|
2071 // This is a call to a value method that has been lowered into a call
|
|
2072 // to a pointer method. Gccgo generates a pointer method for all
|
|
2073 // method calls and takes the address of the value passed as the
|
|
2074 // receiver then immediately dereferences it within the function.
|
|
2075 // In this case, the receiver address does not escape; its content
|
|
2076 // flows to the call.
|
|
2077 this->assign_from_note(note, call_state->retvals,
|
|
2078 this->context_->add_dereference(*p));
|
111
|
2079 else
|
|
2080 {
|
|
2081 if (!Type::are_identical(fntype->receiver()->type(),
|
131
|
2082 (*p)->expr()->type(), Type::COMPARE_TAGS,
|
|
2083 NULL))
|
111
|
2084 {
|
|
2085 // This will be converted later, preemptively track it instead
|
|
2086 // of its conversion expression which will show up in a later pass.
|
|
2087 this->context_->track(*p);
|
|
2088 }
|
|
2089 this->assign_from_note(note, call_state->retvals, *p);
|
|
2090 }
|
|
2091 p++;
|
|
2092 }
|
|
2093
|
|
2094 const Typed_identifier_list* til = fntype->parameters();
|
|
2095 if (til != NULL)
|
|
2096 {
|
|
2097 for (Typed_identifier_list::const_iterator pn = til->begin();
|
|
2098 pn != til->end() && p != arg_nodes.end();
|
|
2099 ++pn, ++p)
|
|
2100 {
|
|
2101 if (!Type::are_identical(pn->type(), (*p)->expr()->type(),
|
131
|
2102 Type::COMPARE_TAGS, NULL))
|
111
|
2103 {
|
|
2104 // This will be converted later, preemptively track it instead
|
|
2105 // of its conversion expression which will show up in a later pass.
|
|
2106 this->context_->track(*p);
|
|
2107 }
|
|
2108
|
|
2109 Type* t = pn->type();
|
|
2110 if (t != NULL
|
|
2111 && t->has_pointer())
|
|
2112 {
|
|
2113 std::string* note = pn->note();
|
|
2114 int enc = this->assign_from_note(note, call_state->retvals, *p);
|
|
2115 if (enc == Node::ESCAPE_NONE
|
131
|
2116 && !call->is_deferred()
|
|
2117 && !call->is_concurrent())
|
111
|
2118 {
|
131
|
2119 // TODO(cmang): Mark the argument as strictly non-escaping?
|
|
2120 // In the gc compiler this is for limiting the lifetime of
|
|
2121 // temporaries. We probably don't need this?
|
111
|
2122 }
|
|
2123 }
|
|
2124 }
|
|
2125
|
|
2126 for (; p != arg_nodes.end(); ++p)
|
|
2127 {
|
|
2128 if (debug_level > 2)
|
|
2129 go_inform(call->location(), "esccall:: ... <- %s, untracked",
|
|
2130 (*p)->ast_format(gogo).c_str());
|
|
2131 this->assign(this->context_->sink(), *p);
|
|
2132 }
|
|
2133 }
|
|
2134 }
|
|
2135
|
|
2136 // Model the assignment of DST to SRC.
|
|
2137 // Assert that SRC somehow gets assigned to DST.
|
|
2138 // DST might need to be examined for evaluations that happen inside of it.
|
|
2139 // For example, in [DST]*f(x) = [SRC]y, we lose track of the indirection and
|
|
2140 // DST becomes the sink in our model.
|
|
2141
|
|
2142 void
|
|
2143 Escape_analysis_assign::assign(Node* dst, Node* src)
|
|
2144 {
|
|
2145 Gogo* gogo = this->context_->gogo();
|
|
2146 int debug_level = gogo->debug_escape_level();
|
|
2147 if (debug_level > 1)
|
|
2148 go_inform(dst->location(), "[%d] %s escassign: %s(%s)[%s] = %s(%s)[%s]",
|
|
2149 this->context_->loop_depth(),
|
|
2150 strip_packed_prefix(gogo, this->context_->current_function_name()).c_str(),
|
|
2151 dst->ast_format(gogo).c_str(), dst->details().c_str(),
|
|
2152 dst->op_format().c_str(),
|
|
2153 src->ast_format(gogo).c_str(), src->details().c_str(),
|
|
2154 src->op_format().c_str());
|
|
2155
|
131
|
2156 if (dst->is_indirect())
|
|
2157 // Lose track of the dereference.
|
|
2158 dst = this->context_->sink();
|
|
2159 else if (dst->expr() != NULL)
|
111
|
2160 {
|
|
2161 // Analyze the lhs of the assignment.
|
|
2162 // Replace DST with this->context_->sink() if we can't track it.
|
|
2163 Expression* e = dst->expr();
|
|
2164 switch (e->classification())
|
|
2165 {
|
|
2166 case Expression::EXPRESSION_VAR_REFERENCE:
|
|
2167 {
|
|
2168 // If DST is a global variable, we have no way to track it.
|
|
2169 Named_object* var = e->var_expression()->named_object();
|
|
2170 if (var->is_variable() && var->var_value()->is_global())
|
|
2171 dst = this->context_->sink();
|
|
2172 }
|
|
2173 break;
|
|
2174
|
|
2175 case Expression::EXPRESSION_FIELD_REFERENCE:
|
|
2176 {
|
|
2177 Expression* strct = e->field_reference_expression()->expr();
|
|
2178 if (strct->heap_expression() != NULL)
|
|
2179 {
|
|
2180 // When accessing the field of a struct reference, we lose
|
|
2181 // track of the indirection.
|
|
2182 dst = this->context_->sink();
|
|
2183 break;
|
|
2184 }
|
|
2185
|
|
2186 // Treat DST.x = SRC as if it were DST = SRC.
|
|
2187 Node* struct_node = Node::make_node(strct);
|
|
2188 this->assign(struct_node, src);
|
|
2189 return;
|
|
2190 }
|
|
2191
|
|
2192 case Expression::EXPRESSION_ARRAY_INDEX:
|
|
2193 {
|
|
2194 Array_index_expression* are = e->array_index_expression();
|
|
2195 if (!are->array()->type()->is_slice_type())
|
|
2196 {
|
|
2197 // Treat DST[i] = SRC as if it were DST = SRC if DST if a fixed
|
|
2198 // array.
|
|
2199 Node* array_node = Node::make_node(are->array());
|
|
2200 this->assign(array_node, src);
|
|
2201 return;
|
|
2202 }
|
|
2203
|
|
2204 // Lose track of the slice dereference.
|
|
2205 dst = this->context_->sink();
|
|
2206 }
|
|
2207 break;
|
|
2208
|
|
2209 case Expression::EXPRESSION_UNARY:
|
|
2210 // Lose track of the dereference.
|
|
2211 if (e->unary_expression()->op() == OPERATOR_MULT)
|
|
2212 dst = this->context_->sink();
|
|
2213 break;
|
|
2214
|
|
2215 case Expression::EXPRESSION_MAP_INDEX:
|
|
2216 {
|
|
2217 // Lose track of the map's key and value.
|
|
2218 Expression* index = e->map_index_expression()->index();
|
|
2219 Node* index_node = Node::make_node(index);
|
|
2220 this->assign(this->context_->sink(), index_node);
|
|
2221
|
|
2222 dst = this->context_->sink();
|
|
2223 }
|
|
2224 break;
|
|
2225
|
131
|
2226 case Expression::EXPRESSION_TEMPORARY_REFERENCE:
|
|
2227 {
|
|
2228 // Temporary is tracked through the underlying Temporary_statement.
|
|
2229 Temporary_statement* t =
|
|
2230 dst->expr()->temporary_reference_expression()->statement();
|
|
2231 if (t->value_escapes())
|
|
2232 dst = this->context_->sink();
|
|
2233 else
|
|
2234 dst = Node::make_node(t);
|
|
2235 }
|
|
2236 break;
|
|
2237
|
111
|
2238 default:
|
|
2239 // TODO(cmang): Add debugging info here: only a few expressions
|
|
2240 // should leave DST unmodified.
|
|
2241 break;
|
|
2242 }
|
|
2243 }
|
|
2244
|
131
|
2245 if (src->object() != NULL)
|
|
2246 this->flows(dst, src);
|
|
2247 else if (src->is_indirect())
|
|
2248 this->flows(dst, src);
|
|
2249 else if (src->expr() != NULL)
|
111
|
2250 {
|
|
2251 Expression* e = src->expr();
|
|
2252 switch (e->classification())
|
|
2253 {
|
|
2254 case Expression::EXPRESSION_VAR_REFERENCE:
|
131
|
2255 case Expression::EXPRESSION_ENCLOSED_VAR_REFERENCE:
|
111
|
2256 // DST = var
|
|
2257 case Expression::EXPRESSION_HEAP:
|
|
2258 // DST = &T{...}.
|
|
2259 case Expression::EXPRESSION_FIXED_ARRAY_CONSTRUCTION:
|
|
2260 case Expression::EXPRESSION_SLICE_CONSTRUCTION:
|
|
2261 // DST = [...]T{...}.
|
|
2262 case Expression::EXPRESSION_MAP_CONSTRUCTION:
|
|
2263 // DST = map[T]V{...}.
|
|
2264 case Expression::EXPRESSION_STRUCT_CONSTRUCTION:
|
|
2265 // DST = T{...}.
|
|
2266 case Expression::EXPRESSION_ALLOCATION:
|
|
2267 // DST = new(T).
|
|
2268 case Expression::EXPRESSION_BOUND_METHOD:
|
|
2269 // DST = x.M.
|
131
|
2270 case Expression::EXPRESSION_STRING_CONCAT:
|
|
2271 // DST = str1 + str2
|
111
|
2272 this->flows(dst, src);
|
|
2273 break;
|
|
2274
|
|
2275 case Expression::EXPRESSION_UNSAFE_CONVERSION:
|
|
2276 {
|
|
2277 Expression* underlying = e->unsafe_conversion_expression()->expr();
|
|
2278 Node* underlying_node = Node::make_node(underlying);
|
|
2279 this->assign(dst, underlying_node);
|
|
2280 }
|
|
2281 break;
|
|
2282
|
|
2283 case Expression::EXPRESSION_CALL:
|
|
2284 {
|
|
2285 Call_expression* call = e->call_expression();
|
131
|
2286 if (call->is_builtin())
|
|
2287 {
|
|
2288 Builtin_call_expression* bce = call->builtin_call_expression();
|
|
2289 if (bce->code() == Builtin_call_expression::BUILTIN_APPEND)
|
|
2290 {
|
|
2291 // Append returns the first argument.
|
|
2292 // The subsequent arguments are already leaked because
|
|
2293 // they are operands to append.
|
|
2294 Node* appendee = Node::make_node(call->args()->front());
|
|
2295 this->assign(dst, appendee);
|
|
2296 }
|
|
2297 break;
|
|
2298 }
|
111
|
2299 Func_expression* fe = call->fn()->func_expression();
|
|
2300 if (fe != NULL && fe->is_runtime_function())
|
|
2301 {
|
|
2302 switch (fe->runtime_code())
|
|
2303 {
|
|
2304 case Runtime::MAKECHAN:
|
131
|
2305 case Runtime::MAKECHAN64:
|
111
|
2306 case Runtime::MAKEMAP:
|
|
2307 case Runtime::MAKESLICE:
|
|
2308 case Runtime::MAKESLICE64:
|
|
2309 // DST = make(...).
|
131
|
2310 this->flows(dst, src);
|
|
2311 break;
|
111
|
2312
|
|
2313 default:
|
|
2314 break;
|
|
2315 }
|
|
2316 break;
|
|
2317 }
|
|
2318 else if (fe != NULL
|
|
2319 && fe->named_object()->is_function()
|
|
2320 && fe->named_object()->func_value()->is_method()
|
|
2321 && (call->is_deferred()
|
|
2322 || call->is_concurrent()))
|
|
2323 {
|
|
2324 // For a method call thunk, lose track of the call and treat it
|
|
2325 // as if DST = RECEIVER.
|
|
2326 Node* rcvr_node = Node::make_node(call->args()->front());
|
|
2327 this->assign(dst, rcvr_node);
|
|
2328 break;
|
|
2329 }
|
|
2330
|
131
|
2331 // Result flows to dst.
|
|
2332 Node* call_node = Node::make_node(e);
|
|
2333 Node::Escape_state* call_state = call_node->state(this->context_, NULL);
|
|
2334 std::vector<Node*> retvals = call_state->retvals;
|
|
2335 for (std::vector<Node*>::const_iterator p = retvals.begin();
|
|
2336 p != retvals.end();
|
|
2337 ++p)
|
|
2338 this->flows(dst, *p);
|
111
|
2339 }
|
|
2340 break;
|
|
2341
|
131
|
2342 case Expression::EXPRESSION_CALL_RESULT:
|
|
2343 {
|
|
2344 Call_result_expression* cre = e->call_result_expression();
|
|
2345 Call_expression* call = cre->call()->call_expression();
|
|
2346 if (call->is_builtin())
|
|
2347 break;
|
|
2348 if (call->fn()->func_expression() != NULL
|
|
2349 && call->fn()->func_expression()->is_runtime_function())
|
|
2350 {
|
|
2351 switch (call->fn()->func_expression()->runtime_code())
|
|
2352 {
|
|
2353 case Runtime::IFACEE2E2:
|
|
2354 case Runtime::IFACEI2E2:
|
|
2355 case Runtime::IFACEE2I2:
|
|
2356 case Runtime::IFACEI2I2:
|
|
2357 case Runtime::IFACEE2T2P:
|
|
2358 case Runtime::IFACEI2T2P:
|
|
2359 {
|
|
2360 // x, ok = v.(T), where T is a pointer or interface,
|
|
2361 // is lowered to
|
|
2362 // x, ok = IFACEI2E2(v), or
|
|
2363 // x, ok = IFACEI2I2(type, v)
|
|
2364 // The last arg flows to the first result.
|
|
2365 // Note: IFACEX2T2 has different signature, handled by
|
|
2366 // ::expression.
|
|
2367 if (cre->index() != 0)
|
|
2368 break;
|
|
2369 Node* arg_node = Node::make_node(call->args()->back());
|
|
2370 this->assign(dst, arg_node);
|
|
2371 }
|
|
2372 break;
|
|
2373
|
|
2374 default:
|
|
2375 break;
|
|
2376 }
|
|
2377 break;
|
|
2378 }
|
|
2379
|
|
2380 Node* call_node = Node::make_node(call);
|
|
2381 Node* ret_node = call_node->state(context_, NULL)->retvals[cre->index()];
|
|
2382 this->assign(dst, ret_node);
|
|
2383 }
|
|
2384 break;
|
|
2385
|
111
|
2386 case Expression::EXPRESSION_FUNC_REFERENCE:
|
|
2387 if (e->func_expression()->closure() != NULL)
|
131
|
2388 this->flows(dst, src);
|
111
|
2389 break;
|
|
2390
|
131
|
2391 case Expression::EXPRESSION_CONVERSION:
|
|
2392 {
|
|
2393 Type_conversion_expression* tce = e->conversion_expression();
|
|
2394 Type* ft = tce->expr()->type();
|
|
2395 Type* tt = tce->type();
|
|
2396 if ((ft->is_string_type() && tt->is_slice_type())
|
|
2397 || (ft->is_slice_type() && tt->is_string_type())
|
|
2398 || (ft->integer_type() != NULL && tt->is_string_type()))
|
|
2399 {
|
|
2400 // string([]byte), string([]rune), []byte(string), []rune(string), string(rune)
|
|
2401 this->flows(dst, src);
|
|
2402 break;
|
|
2403 }
|
|
2404 // Conversion preserves input value.
|
|
2405 Expression* underlying = tce->expr();
|
|
2406 this->assign(dst, Node::make_node(underlying));
|
|
2407 }
|
|
2408 break;
|
|
2409
|
111
|
2410 case Expression::EXPRESSION_FIELD_REFERENCE:
|
|
2411 {
|
|
2412 // A non-pointer can't escape from a struct.
|
|
2413 if (!e->type()->has_pointer())
|
|
2414 break;
|
|
2415 }
|
|
2416 // Fall through.
|
|
2417
|
|
2418 case Expression::EXPRESSION_TYPE_GUARD:
|
|
2419 case Expression::EXPRESSION_ARRAY_INDEX:
|
|
2420 case Expression::EXPRESSION_STRING_INDEX:
|
|
2421 {
|
|
2422 Expression* left = NULL;
|
|
2423 if (e->field_reference_expression() != NULL)
|
|
2424 {
|
|
2425 left = e->field_reference_expression()->expr();
|
|
2426 if (left->unary_expression() != NULL
|
|
2427 && left->unary_expression()->op() == OPERATOR_MULT)
|
|
2428 {
|
|
2429 // DST = (*x).f
|
|
2430 this->flows(dst, src);
|
|
2431 break;
|
|
2432 }
|
|
2433 }
|
|
2434 else if (e->type_guard_expression() != NULL)
|
|
2435 left = e->type_guard_expression()->expr();
|
|
2436 else if (e->array_index_expression() != NULL)
|
|
2437 {
|
|
2438 Array_index_expression* aie = e->array_index_expression();
|
131
|
2439 if (aie->end() != NULL)
|
|
2440 // slicing
|
|
2441 if (aie->array()->type()->is_slice_type())
|
|
2442 left = aie->array();
|
|
2443 else
|
|
2444 {
|
|
2445 // slicing an array
|
|
2446 // The gc compiler has an implicit address operator.
|
|
2447 go_assert(src->child() != NULL);
|
|
2448 this->assign(dst, src->child());
|
|
2449 break;
|
|
2450 }
|
111
|
2451 else if (!aie->array()->type()->is_slice_type())
|
|
2452 {
|
|
2453 // Indexing an array preserves the input value.
|
|
2454 Node* array_node = Node::make_node(aie->array());
|
|
2455 this->assign(dst, array_node);
|
|
2456 break;
|
|
2457 }
|
|
2458 else
|
|
2459 {
|
|
2460 this->flows(dst, src);
|
|
2461 break;
|
|
2462 }
|
|
2463 }
|
|
2464 else if (e->string_index_expression() != NULL)
|
|
2465 {
|
|
2466 String_index_expression* sie = e->string_index_expression();
|
131
|
2467 if (e->type()->is_string_type())
|
|
2468 // slicing
|
111
|
2469 left = sie->string();
|
|
2470 else
|
|
2471 {
|
|
2472 this->flows(dst, src);
|
|
2473 break;
|
|
2474 }
|
|
2475 }
|
|
2476 go_assert(left != NULL);
|
|
2477
|
|
2478 // Conversions, field access, and slicing all preserve the input
|
|
2479 // value.
|
|
2480 Node* left_node = Node::make_node(left);
|
|
2481 this->assign(dst, left_node);
|
|
2482 }
|
|
2483 break;
|
|
2484
|
|
2485 case Expression::EXPRESSION_BINARY:
|
|
2486 {
|
|
2487 switch (e->binary_expression()->op())
|
|
2488 {
|
|
2489 case OPERATOR_PLUS:
|
|
2490 case OPERATOR_MINUS:
|
|
2491 case OPERATOR_XOR:
|
131
|
2492 case OPERATOR_OR:
|
111
|
2493 case OPERATOR_MULT:
|
|
2494 case OPERATOR_DIV:
|
|
2495 case OPERATOR_MOD:
|
|
2496 case OPERATOR_LSHIFT:
|
|
2497 case OPERATOR_RSHIFT:
|
|
2498 case OPERATOR_AND:
|
|
2499 case OPERATOR_BITCLEAR:
|
|
2500 {
|
|
2501 Node* left = Node::make_node(e->binary_expression()->left());
|
|
2502 this->assign(dst, left);
|
|
2503 Node* right = Node::make_node(e->binary_expression()->right());
|
|
2504 this->assign(dst, right);
|
|
2505 }
|
|
2506 break;
|
|
2507
|
|
2508 default:
|
|
2509 break;
|
|
2510 }
|
|
2511 }
|
|
2512 break;
|
|
2513
|
|
2514 case Expression::EXPRESSION_UNARY:
|
|
2515 {
|
|
2516 switch (e->unary_expression()->op())
|
|
2517 {
|
|
2518 case OPERATOR_PLUS:
|
|
2519 case OPERATOR_MINUS:
|
|
2520 case OPERATOR_XOR:
|
|
2521 {
|
|
2522 Node* op_node =
|
|
2523 Node::make_node(e->unary_expression()->operand());
|
|
2524 this->assign(dst, op_node);
|
|
2525 }
|
|
2526 break;
|
|
2527
|
|
2528 case OPERATOR_MULT:
|
|
2529 // DST = *x
|
|
2530 case OPERATOR_AND:
|
|
2531 // DST = &x
|
|
2532 this->flows(dst, src);
|
|
2533 break;
|
|
2534
|
|
2535 default:
|
|
2536 break;
|
|
2537 }
|
|
2538 }
|
|
2539 break;
|
|
2540
|
|
2541 case Expression::EXPRESSION_TEMPORARY_REFERENCE:
|
|
2542 {
|
|
2543 Statement* temp = e->temporary_reference_expression()->statement();
|
131
|
2544 this->assign(dst, Node::make_node(temp));
|
111
|
2545 }
|
|
2546 break;
|
|
2547
|
|
2548 default:
|
|
2549 // TODO(cmang): Add debug info here; this should not be reachable.
|
|
2550 // For now, just to be conservative, we'll just say dst flows to src.
|
|
2551 break;
|
|
2552 }
|
|
2553 }
|
131
|
2554 else if (src->statement() != NULL && src->statement()->temporary_statement() != NULL)
|
|
2555 this->flows(dst, src);
|
111
|
2556 }
|
|
2557
|
|
2558 // Model the assignment of DST to an indirection of SRC.
|
|
2559
|
|
2560 void
|
|
2561 Escape_analysis_assign::assign_deref(Node* dst, Node* src)
|
|
2562 {
|
|
2563 if (src->expr() != NULL)
|
|
2564 {
|
|
2565 switch (src->expr()->classification())
|
|
2566 {
|
|
2567 case Expression::EXPRESSION_BOOLEAN:
|
|
2568 case Expression::EXPRESSION_STRING:
|
|
2569 case Expression::EXPRESSION_INTEGER:
|
|
2570 case Expression::EXPRESSION_FLOAT:
|
|
2571 case Expression::EXPRESSION_COMPLEX:
|
|
2572 case Expression::EXPRESSION_NIL:
|
|
2573 case Expression::EXPRESSION_IOTA:
|
|
2574 // No need to try indirections on literal values
|
|
2575 // or numeric constants.
|
|
2576 return;
|
|
2577
|
|
2578 default:
|
|
2579 break;
|
|
2580 }
|
|
2581 }
|
|
2582
|
|
2583 this->assign(dst, this->context_->add_dereference(src));
|
|
2584 }
|
|
2585
|
|
2586 // Model the input-to-output assignment flow of one of a function call's
|
|
2587 // arguments, where the flow is encoded in NOTE.
|
|
2588
|
|
2589 int
|
|
2590 Escape_analysis_assign::assign_from_note(std::string* note,
|
|
2591 const std::vector<Node*>& dsts,
|
|
2592 Node* src)
|
|
2593 {
|
|
2594 int enc = Escape_note::parse_tag(note);
|
|
2595 if (src->expr() != NULL)
|
|
2596 {
|
|
2597 switch (src->expr()->classification())
|
|
2598 {
|
|
2599 case Expression::EXPRESSION_BOOLEAN:
|
|
2600 case Expression::EXPRESSION_STRING:
|
|
2601 case Expression::EXPRESSION_INTEGER:
|
|
2602 case Expression::EXPRESSION_FLOAT:
|
|
2603 case Expression::EXPRESSION_COMPLEX:
|
|
2604 case Expression::EXPRESSION_NIL:
|
|
2605 case Expression::EXPRESSION_IOTA:
|
|
2606 // There probably isn't a note for a literal value. Literal values
|
|
2607 // usually don't escape unless we lost track of the value some how.
|
|
2608 return enc;
|
|
2609
|
|
2610 default:
|
|
2611 break;
|
|
2612 }
|
|
2613 }
|
|
2614
|
|
2615 if (this->context_->gogo()->debug_escape_level() > 2)
|
131
|
2616 go_inform(src->location(), "assignfromtag:: src=%s em=%s",
|
|
2617 src->ast_format(context_->gogo()).c_str(),
|
111
|
2618 Escape_note::make_tag(enc).c_str());
|
|
2619
|
|
2620 if (enc == Node::ESCAPE_UNKNOWN)
|
|
2621 {
|
|
2622 // Lost track of the value.
|
|
2623 this->assign(this->context_->sink(), src);
|
|
2624 return enc;
|
|
2625 }
|
|
2626 else if (enc == Node::ESCAPE_NONE)
|
|
2627 return enc;
|
|
2628
|
|
2629 // If the content inside a parameter (reached via indirection) escapes to
|
|
2630 // the heap, mark it.
|
|
2631 if ((enc & ESCAPE_CONTENT_ESCAPES) != 0)
|
|
2632 this->assign(this->context_->sink(), this->context_->add_dereference(src));
|
|
2633
|
|
2634 int save_enc = enc;
|
|
2635 enc >>= ESCAPE_RETURN_BITS;
|
|
2636 for (std::vector<Node*>::const_iterator p = dsts.begin();
|
|
2637 enc != 0 && p != dsts.end();
|
|
2638 ++p)
|
|
2639 {
|
|
2640 // Prefer the lowest-level path to the reference (for escape purposes).
|
|
2641 // Two-bit encoding (for example. 1, 3, and 4 bits are other options)
|
|
2642 // 01 = 0-level
|
|
2643 // 10 = 1-level, (content escapes),
|
|
2644 // 11 = 2-level, (content of content escapes).
|
|
2645 int bits = enc & ESCAPE_BITS_MASK_FOR_TAG;
|
|
2646 if (bits > 0)
|
|
2647 {
|
|
2648 Node* n = src;
|
|
2649 for (int i = 0; i < bits - 1; ++i)
|
|
2650 {
|
|
2651 // Encode level > 0 as indirections.
|
|
2652 n = this->context_->add_dereference(n);
|
|
2653 }
|
|
2654 this->assign(*p, n);
|
|
2655 }
|
|
2656 enc >>= ESCAPE_BITS_PER_OUTPUT_IN_TAG;
|
|
2657 }
|
|
2658
|
|
2659 // If there are too many outputs to fit in the tag, that is handled on the
|
|
2660 // encoding end as ESCAPE_HEAP, so there is no need to check here.
|
|
2661 return save_enc;
|
|
2662 }
|
|
2663
|
|
2664 // Record the flow of SRC to DST in DST.
|
|
2665
|
|
2666 void
|
|
2667 Escape_analysis_assign::flows(Node* dst, Node* src)
|
|
2668 {
|
|
2669 // Don't bother capturing the flow from scalars.
|
131
|
2670 if (src->type() != NULL && !src->type()->has_pointer())
|
111
|
2671 return;
|
|
2672
|
|
2673 // Don't confuse a blank identifier with the sink.
|
|
2674 if (dst->is_sink() && dst != this->context_->sink())
|
|
2675 return;
|
|
2676
|
|
2677 Node::Escape_state* dst_state = dst->state(this->context_, NULL);
|
|
2678 Node::Escape_state* src_state = src->state(this->context_, NULL);
|
|
2679 if (dst == src
|
|
2680 || dst_state == src_state
|
131
|
2681 || dst_state->flows.find(src) != dst_state->flows.end())
|
111
|
2682 return;
|
|
2683
|
|
2684 Gogo* gogo = this->context_->gogo();
|
|
2685 if (gogo->debug_escape_level() > 2)
|
|
2686 go_inform(Linemap::unknown_location(), "flows:: %s <- %s",
|
|
2687 dst->ast_format(gogo).c_str(), src->ast_format(gogo).c_str());
|
|
2688
|
|
2689 if (dst_state->flows.empty())
|
|
2690 this->context_->add_dst(dst);
|
|
2691
|
|
2692 dst_state->flows.insert(src);
|
|
2693 }
|
|
2694
|
|
2695 // Build a connectivity graph between nodes in the function being analyzed.
|
|
2696
|
|
2697 void
|
|
2698 Gogo::assign_connectivity(Escape_context* context, Named_object* fn)
|
|
2699 {
|
|
2700 // Must be defined outside of this package.
|
|
2701 if (!fn->is_function())
|
|
2702 return;
|
|
2703
|
|
2704 int save_depth = context->loop_depth();
|
|
2705 context->set_loop_depth(1);
|
|
2706
|
|
2707 Escape_analysis_assign ea(context, fn);
|
|
2708 Function::Results* res = fn->func_value()->result_variables();
|
|
2709 if (res != NULL)
|
|
2710 {
|
|
2711 for (Function::Results::const_iterator p = res->begin();
|
|
2712 p != res->end();
|
|
2713 ++p)
|
|
2714 {
|
|
2715 Node* res_node = Node::make_node(*p);
|
|
2716 Node::Escape_state* res_state = res_node->state(context, fn);
|
131
|
2717 res_state->fn = fn;
|
111
|
2718 res_state->loop_depth = 0;
|
|
2719
|
|
2720 // If this set of functions is recursive, we lose track of the return values.
|
|
2721 // Just say that the result flows to the sink.
|
|
2722 if (context->recursive())
|
|
2723 ea.flows(context->sink(), res_node);
|
|
2724 }
|
|
2725 }
|
|
2726
|
|
2727 const Bindings* callee_bindings = fn->func_value()->block()->bindings();
|
|
2728 Function_type* fntype = fn->func_value()->type();
|
|
2729 Typed_identifier_list* params = (fntype->parameters() != NULL
|
|
2730 ? fntype->parameters()->copy()
|
|
2731 : new Typed_identifier_list);
|
|
2732 if (fntype->receiver() != NULL)
|
|
2733 params->push_back(*fntype->receiver());
|
|
2734
|
|
2735 for (Typed_identifier_list::const_iterator p = params->begin();
|
|
2736 p != params->end();
|
|
2737 ++p)
|
|
2738 {
|
|
2739 if (p->name().empty() || Gogo::is_sink_name(p->name()))
|
|
2740 continue;
|
|
2741
|
|
2742 Named_object* param_no = callee_bindings->lookup_local(p->name());
|
|
2743 go_assert(param_no != NULL);
|
|
2744 Node* param_node = Node::make_node(param_no);
|
|
2745 Node::Escape_state* param_state = param_node->state(context, fn);
|
131
|
2746 param_state->fn = fn;
|
111
|
2747 param_state->loop_depth = 1;
|
|
2748
|
|
2749 if (!p->type()->has_pointer())
|
|
2750 continue;
|
|
2751
|
|
2752 // External function? Parameters must escape unless //go:noescape is set.
|
|
2753 // TODO(cmang): Implement //go:noescape directive.
|
|
2754 if (fn->package() != NULL)
|
|
2755 param_node->set_encoding(Node::ESCAPE_HEAP);
|
|
2756 else
|
131
|
2757 {
|
|
2758 param_node->set_encoding(Node::ESCAPE_NONE);
|
|
2759 context->track(param_node);
|
|
2760 }
|
111
|
2761 }
|
|
2762
|
|
2763 Escape_analysis_loop el;
|
|
2764 fn->func_value()->traverse(&el);
|
|
2765
|
|
2766 fn->func_value()->traverse(&ea);
|
|
2767 context->set_loop_depth(save_depth);
|
|
2768 }
|
|
2769
|
|
2770 class Escape_analysis_flood
|
|
2771 {
|
|
2772 public:
|
|
2773 Escape_analysis_flood(Escape_context* context)
|
|
2774 : context_(context)
|
|
2775 { }
|
|
2776
|
|
2777 // Use the escape information in dst to update the escape information in src
|
|
2778 // and src's upstream.
|
|
2779 void
|
|
2780 flood(Level, Node* dst, Node* src, int);
|
|
2781
|
|
2782 private:
|
|
2783 // The escape context for the group of functions being flooded.
|
|
2784 Escape_context* context_;
|
|
2785 };
|
|
2786
|
|
2787 // Whenever we hit a dereference node, the level goes up by one, and whenever
|
|
2788 // we hit an address-of, the level goes down by one. as long as we're on a
|
|
2789 // level > 0 finding an address-of just means we're following the upstream
|
|
2790 // of a dereference, so this address doesn't leak (yet).
|
|
2791 // If level == 0, it means the /value/ of this node can reach the root of this
|
|
2792 // flood so if this node is an address-of, its argument should be marked as
|
|
2793 // escaping iff its current function and loop depth are different from the
|
|
2794 // flood's root.
|
|
2795 // Once an object has been moved to the heap, all of its upstream should be
|
|
2796 // considered escaping to the global scope.
|
|
2797 // This is an implementation of gc/esc.go:escwalkBody.
|
|
2798
|
|
2799 void
|
|
2800 Escape_analysis_flood::flood(Level level, Node* dst, Node* src,
|
|
2801 int extra_loop_depth)
|
|
2802 {
|
|
2803 // No need to flood src if it is a literal.
|
|
2804 if (src->expr() != NULL)
|
|
2805 {
|
|
2806 switch (src->expr()->classification())
|
|
2807 {
|
|
2808 case Expression::EXPRESSION_BOOLEAN:
|
|
2809 case Expression::EXPRESSION_STRING:
|
|
2810 case Expression::EXPRESSION_INTEGER:
|
|
2811 case Expression::EXPRESSION_FLOAT:
|
|
2812 case Expression::EXPRESSION_COMPLEX:
|
|
2813 case Expression::EXPRESSION_NIL:
|
|
2814 case Expression::EXPRESSION_IOTA:
|
|
2815 return;
|
|
2816
|
|
2817 default:
|
|
2818 break;
|
|
2819 }
|
|
2820 }
|
|
2821
|
|
2822 Node::Escape_state* src_state = src->state(this->context_, NULL);
|
|
2823 if (src_state->flood_id == this->context_->flood_id())
|
|
2824 {
|
|
2825 // Esclevels are vectors, do not compare as integers,
|
|
2826 // and must use "min" of old and new to guarantee
|
|
2827 // convergence.
|
|
2828 level = level.min(src_state->level);
|
|
2829 if (level == src_state->level)
|
|
2830 {
|
|
2831 // Have we been here already with an extraloopdepth,
|
|
2832 // or is the extraloopdepth provided no improvement on
|
|
2833 // what's already been seen?
|
|
2834 if (src_state->max_extra_loop_depth >= extra_loop_depth
|
|
2835 || src_state->loop_depth >= extra_loop_depth)
|
|
2836 return;
|
|
2837 src_state->max_extra_loop_depth = extra_loop_depth;
|
|
2838 }
|
|
2839 }
|
|
2840 else
|
|
2841 src_state->max_extra_loop_depth = -1;
|
|
2842
|
|
2843 src_state->flood_id = this->context_->flood_id();
|
|
2844 src_state->level = level;
|
|
2845 int mod_loop_depth = std::max(extra_loop_depth, src_state->loop_depth);
|
|
2846
|
|
2847 Gogo* gogo = this->context_->gogo();
|
|
2848 int debug_level = gogo->debug_escape_level();
|
|
2849 if (debug_level > 1)
|
|
2850 go_inform(Linemap::unknown_location(),
|
|
2851 "escwalk: level:{%d %d} depth:%d "
|
|
2852 "op=%s %s(%s) "
|
|
2853 "scope:%s[%d] "
|
|
2854 "extraloopdepth=%d",
|
|
2855 level.value(), level.suffix_value(), this->context_->pdepth(),
|
|
2856 src->op_format().c_str(),
|
|
2857 src->ast_format(gogo).c_str(),
|
|
2858 src->details().c_str(),
|
|
2859 debug_function_name(src_state->fn).c_str(),
|
|
2860 src_state->loop_depth,
|
|
2861 extra_loop_depth);
|
|
2862
|
|
2863 this->context_->increase_pdepth();
|
|
2864
|
|
2865 // Input parameter flowing into output parameter?
|
|
2866 Named_object* src_no = NULL;
|
|
2867 if (src->expr() != NULL && src->expr()->var_expression() != NULL)
|
|
2868 src_no = src->expr()->var_expression()->named_object();
|
|
2869 else
|
|
2870 src_no = src->object();
|
|
2871 bool src_is_param = (src_no != NULL
|
|
2872 && src_no->is_variable()
|
|
2873 && src_no->var_value()->is_parameter());
|
|
2874
|
|
2875 Named_object* dst_no = NULL;
|
|
2876 if (dst->expr() != NULL && dst->expr()->var_expression() != NULL)
|
|
2877 dst_no = dst->expr()->var_expression()->named_object();
|
|
2878 else
|
|
2879 dst_no = dst->object();
|
|
2880 bool dst_is_result = dst_no != NULL && dst_no->is_result_variable();
|
131
|
2881 Node::Escape_state* dst_state = dst->state(this->context_, NULL);
|
111
|
2882
|
|
2883 if (src_is_param
|
|
2884 && dst_is_result
|
131
|
2885 && src_state->fn == dst_state->fn
|
|
2886 && (src->encoding() & ESCAPE_MASK) < int(Node::ESCAPE_HEAP)
|
111
|
2887 && dst->encoding() != Node::ESCAPE_HEAP)
|
|
2888 {
|
|
2889 // This case handles:
|
|
2890 // 1. return in
|
|
2891 // 2. return &in
|
|
2892 // 3. tmp := in; return &tmp
|
|
2893 // 4. return *in
|
|
2894 if (debug_level != 0)
|
|
2895 {
|
|
2896 if (debug_level == 1)
|
131
|
2897 go_inform(src->definition_location(),
|
111
|
2898 "leaking param: %s to result %s level=%d",
|
|
2899 src->ast_format(gogo).c_str(),
|
|
2900 dst->ast_format(gogo).c_str(),
|
|
2901 level.value());
|
|
2902 else
|
131
|
2903 go_inform(src->definition_location(),
|
111
|
2904 "leaking param: %s to result %s level={%d %d}",
|
|
2905 src->ast_format(gogo).c_str(),
|
|
2906 dst->ast_format(gogo).c_str(),
|
|
2907 level.value(), level.suffix_value());
|
|
2908 }
|
|
2909
|
|
2910 if ((src->encoding() & ESCAPE_MASK) != Node::ESCAPE_RETURN)
|
|
2911 {
|
|
2912 int enc =
|
|
2913 Node::ESCAPE_RETURN | (src->encoding() & ESCAPE_CONTENT_ESCAPES);
|
|
2914 src->set_encoding(enc);
|
|
2915 }
|
|
2916
|
|
2917 int enc = Node::note_inout_flows(src->encoding(),
|
|
2918 dst_no->result_var_value()->index(),
|
|
2919 level);
|
|
2920 src->set_encoding(enc);
|
|
2921
|
|
2922 // In gc/esc.go:escwalkBody, this is a goto to the label for recursively
|
|
2923 // flooding the connection graph. Inlined here for convenience.
|
|
2924 level = level.copy();
|
|
2925 for (std::set<Node*>::const_iterator p = src_state->flows.begin();
|
|
2926 p != src_state->flows.end();
|
|
2927 ++p)
|
|
2928 this->flood(level, dst, *p, extra_loop_depth);
|
|
2929 return;
|
|
2930 }
|
|
2931
|
|
2932 // If parameter content escape to heap, set ESCAPE_CONTENT_ESCAPES.
|
|
2933 // Note minor confusion around escape from pointer-to-struct vs
|
|
2934 // escape from struct.
|
|
2935 if (src_is_param
|
|
2936 && dst->encoding() == Node::ESCAPE_HEAP
|
131
|
2937 && (src->encoding() & ESCAPE_MASK) < int(Node::ESCAPE_HEAP)
|
111
|
2938 && level.value() > 0)
|
|
2939 {
|
|
2940 int enc =
|
|
2941 Node::max_encoding((src->encoding() | ESCAPE_CONTENT_ESCAPES),
|
|
2942 Node::ESCAPE_NONE);
|
|
2943 src->set_encoding(enc);
|
|
2944 if (debug_level != 0)
|
131
|
2945 go_inform(src->definition_location(), "mark escaped content: %s",
|
111
|
2946 src->ast_format(gogo).c_str());
|
|
2947 }
|
|
2948
|
|
2949 // A src object leaks if its value or address is assigned to a dst object
|
|
2950 // in a different scope (at a different loop depth).
|
|
2951 bool src_leaks = (level.value() <= 0
|
|
2952 && level.suffix_value() <= 0
|
|
2953 && dst_state->loop_depth < mod_loop_depth);
|
131
|
2954 src_leaks = src_leaks || (level.value() <= 0
|
|
2955 && (dst->encoding() & ESCAPE_MASK) == Node::ESCAPE_HEAP);
|
|
2956 // old src encoding, used to prevent duplicate error messages
|
|
2957 int osrcesc = src->encoding();
|
111
|
2958
|
|
2959 if (src_is_param
|
|
2960 && (src_leaks || dst_state->loop_depth < 0)
|
131
|
2961 && (src->encoding() & ESCAPE_MASK) < int(Node::ESCAPE_HEAP))
|
111
|
2962 {
|
|
2963 if (level.suffix_value() > 0)
|
|
2964 {
|
|
2965 int enc =
|
|
2966 Node::max_encoding((src->encoding() | ESCAPE_CONTENT_ESCAPES),
|
|
2967 Node::ESCAPE_NONE);
|
|
2968 src->set_encoding(enc);
|
131
|
2969 if (debug_level != 0 && osrcesc != src->encoding())
|
|
2970 go_inform(src->definition_location(), "leaking param content: %s",
|
111
|
2971 src->ast_format(gogo).c_str());
|
|
2972 }
|
|
2973 else
|
|
2974 {
|
|
2975 if (debug_level != 0)
|
131
|
2976 go_inform(src->definition_location(), "leaking param: %s",
|
|
2977 src->ast_format(gogo).c_str());
|
|
2978 src->set_encoding(Node::ESCAPE_HEAP);
|
111
|
2979 }
|
|
2980 }
|
|
2981 else if (src->expr() != NULL)
|
|
2982 {
|
|
2983 Expression* e = src->expr();
|
|
2984 if (e->enclosed_var_expression() != NULL)
|
|
2985 {
|
|
2986 if (src_leaks && debug_level != 0)
|
|
2987 go_inform(src->location(), "leaking closure reference %s",
|
|
2988 src->ast_format(gogo).c_str());
|
|
2989
|
|
2990 Node* enclosed_node =
|
|
2991 Node::make_node(e->enclosed_var_expression()->variable());
|
|
2992 this->flood(level, dst, enclosed_node, -1);
|
|
2993 }
|
|
2994 else if (e->heap_expression() != NULL
|
|
2995 || (e->unary_expression() != NULL
|
|
2996 && e->unary_expression()->op() == OPERATOR_AND))
|
|
2997 {
|
|
2998 // Pointer literals and address-of expressions.
|
|
2999 Expression* underlying;
|
|
3000 if (e->heap_expression())
|
|
3001 underlying = e->heap_expression()->expr();
|
|
3002 else
|
|
3003 underlying = e->unary_expression()->operand();
|
|
3004 Node* underlying_node = Node::make_node(underlying);
|
|
3005
|
|
3006 // If the address leaks, the underyling object must be moved
|
|
3007 // to the heap.
|
|
3008 underlying->address_taken(src_leaks);
|
|
3009 if (src_leaks)
|
|
3010 {
|
|
3011 src->set_encoding(Node::ESCAPE_HEAP);
|
131
|
3012 if (osrcesc != src->encoding())
|
|
3013 {
|
|
3014 move_to_heap(gogo, underlying);
|
|
3015 if (debug_level > 1)
|
|
3016 go_inform(src->location(),
|
|
3017 "%s escapes to heap, level={%d %d}, "
|
|
3018 "dst.eld=%d, src.eld=%d",
|
|
3019 src->ast_format(gogo).c_str(), level.value(),
|
|
3020 level.suffix_value(), dst_state->loop_depth,
|
|
3021 mod_loop_depth);
|
|
3022 else if (debug_level > 0)
|
|
3023 go_inform(src->location(), "%s escapes to heap",
|
|
3024 src->ast_format(gogo).c_str());
|
|
3025 }
|
111
|
3026
|
|
3027 this->flood(level.decrease(), dst,
|
|
3028 underlying_node, mod_loop_depth);
|
|
3029 extra_loop_depth = mod_loop_depth;
|
|
3030 }
|
|
3031 else
|
|
3032 {
|
|
3033 // Decrease the level each time we take the address of the object.
|
|
3034 this->flood(level.decrease(), dst, underlying_node, -1);
|
|
3035 }
|
|
3036 }
|
|
3037 else if (e->slice_literal() != NULL)
|
|
3038 {
|
|
3039 Slice_construction_expression* slice = e->slice_literal();
|
|
3040 if (slice->vals() != NULL)
|
|
3041 {
|
|
3042 for (Expression_list::const_iterator p = slice->vals()->begin();
|
|
3043 p != slice->vals()->end();
|
|
3044 ++p)
|
|
3045 {
|
|
3046 if ((*p) != NULL)
|
|
3047 this->flood(level.decrease(), dst, Node::make_node(*p), -1);
|
|
3048 }
|
|
3049 }
|
|
3050 if (src_leaks)
|
|
3051 {
|
|
3052 src->set_encoding(Node::ESCAPE_HEAP);
|
131
|
3053 if (debug_level != 0 && osrcesc != src->encoding())
|
111
|
3054 go_inform(src->location(), "%s escapes to heap",
|
|
3055 src->ast_format(gogo).c_str());
|
|
3056 extra_loop_depth = mod_loop_depth;
|
|
3057 }
|
|
3058 }
|
|
3059 else if (e->call_expression() != NULL)
|
|
3060 {
|
|
3061 Call_expression* call = e->call_expression();
|
131
|
3062 if (call->is_builtin())
|
|
3063 {
|
|
3064 Builtin_call_expression* bce = call->builtin_call_expression();
|
|
3065 if (bce->code() == Builtin_call_expression::BUILTIN_APPEND)
|
|
3066 {
|
|
3067 // Propagate escape information to appendee.
|
|
3068 Expression* appendee = call->args()->front();
|
|
3069 this->flood(level, dst, Node::make_node(appendee), -1);
|
|
3070 }
|
|
3071 }
|
|
3072 else if (call->fn()->func_expression() != NULL
|
|
3073 && call->fn()->func_expression()->is_runtime_function())
|
|
3074 {
|
|
3075 switch (call->fn()->func_expression()->runtime_code())
|
|
3076 {
|
|
3077 case Runtime::MAKECHAN:
|
|
3078 case Runtime::MAKECHAN64:
|
|
3079 case Runtime::MAKEMAP:
|
|
3080 case Runtime::MAKESLICE:
|
|
3081 case Runtime::MAKESLICE64:
|
|
3082 if (src_leaks)
|
|
3083 {
|
|
3084 src->set_encoding(Node::ESCAPE_HEAP);
|
|
3085 if (debug_level != 0 && osrcesc != src->encoding())
|
|
3086 go_inform(src->location(), "%s escapes to heap",
|
|
3087 src->ast_format(gogo).c_str());
|
|
3088 extra_loop_depth = mod_loop_depth;
|
|
3089 }
|
|
3090 break;
|
|
3091
|
|
3092 default:
|
|
3093 break;
|
|
3094 }
|
|
3095 }
|
|
3096 else if (src_state->retvals.size() > 0)
|
|
3097 {
|
|
3098 // In this case a link went directly to a call, but should really go
|
|
3099 // to the dummy .outN outputs that were created for the call that
|
|
3100 // themselves link to the inputs with levels adjusted.
|
|
3101 // See e.g. #10466.
|
|
3102 // This can only happen with functions returning a single result.
|
|
3103 go_assert(src_state->retvals.size() == 1);
|
|
3104 if (debug_level > 2)
|
|
3105 go_inform(src->location(), "[%d] dst %s escwalk replace src: %s with %s",
|
|
3106 this->context_->loop_depth(),
|
|
3107 dst->ast_format(gogo).c_str(),
|
|
3108 src->ast_format(gogo).c_str(),
|
|
3109 src_state->retvals[0]->ast_format(gogo).c_str());
|
|
3110 src = src_state->retvals[0];
|
|
3111 src_state = src->state(this->context_, NULL);
|
|
3112 }
|
111
|
3113 }
|
|
3114 else if (e->allocation_expression() != NULL && src_leaks)
|
|
3115 {
|
|
3116 // Calls to Runtime::NEW get lowered into an allocation expression.
|
|
3117 src->set_encoding(Node::ESCAPE_HEAP);
|
131
|
3118 if (debug_level != 0 && osrcesc != src->encoding())
|
111
|
3119 go_inform(src->location(), "%s escapes to heap",
|
|
3120 src->ast_format(gogo).c_str());
|
131
|
3121 extra_loop_depth = mod_loop_depth;
|
111
|
3122 }
|
131
|
3123 else if ((e->map_literal() != NULL
|
|
3124 || e->string_concat_expression() != NULL
|
|
3125 || (e->func_expression() != NULL && e->func_expression()->closure() != NULL)
|
|
3126 || e->bound_method_expression() != NULL)
|
|
3127 && src_leaks)
|
|
3128 {
|
|
3129 src->set_encoding(Node::ESCAPE_HEAP);
|
|
3130 if (debug_level != 0 && osrcesc != src->encoding())
|
|
3131 go_inform(src->location(), "%s escapes to heap",
|
|
3132 src->ast_format(gogo).c_str());
|
|
3133 extra_loop_depth = mod_loop_depth;
|
|
3134 }
|
|
3135 else if (e->conversion_expression() != NULL && src_leaks)
|
|
3136 {
|
|
3137 Type_conversion_expression* tce = e->conversion_expression();
|
|
3138 Type* ft = tce->expr()->type();
|
|
3139 Type* tt = tce->type();
|
|
3140 if ((ft->is_string_type() && tt->is_slice_type())
|
|
3141 || (ft->is_slice_type() && tt->is_string_type())
|
|
3142 || (ft->integer_type() != NULL && tt->is_string_type()))
|
|
3143 {
|
|
3144 // string([]byte), string([]rune), []byte(string), []rune(string), string(rune)
|
|
3145 src->set_encoding(Node::ESCAPE_HEAP);
|
|
3146 if (debug_level != 0 && osrcesc != src->encoding())
|
|
3147 go_inform(src->location(), "%s escapes to heap",
|
|
3148 src->ast_format(gogo).c_str());
|
|
3149 extra_loop_depth = mod_loop_depth;
|
|
3150 }
|
|
3151 }
|
|
3152 else if (e->array_index_expression() != NULL
|
|
3153 && !e->array_index_expression()->array()->type()->is_slice_type())
|
|
3154 {
|
|
3155 Array_index_expression* aie = e->array_index_expression();
|
|
3156 if (aie->end() != NULL)
|
|
3157 {
|
|
3158 // Slicing an array.
|
|
3159 // Flow its implicit address-of node to DST.
|
|
3160 this->flood(level, dst, src->child(), -1);
|
|
3161 }
|
|
3162 else
|
|
3163 {
|
|
3164 // Indexing an array.
|
|
3165 // An array element flowing to DST behaves like the array
|
|
3166 // flowing to DST.
|
|
3167 Expression* underlying = e->array_index_expression()->array();
|
|
3168 Node* underlying_node = Node::make_node(underlying);
|
|
3169 this->flood(level, dst, underlying_node, -1);
|
|
3170 }
|
|
3171 }
|
111
|
3172 else if ((e->field_reference_expression() != NULL
|
|
3173 && e->field_reference_expression()->expr()->unary_expression() == NULL)
|
|
3174 || e->type_guard_expression() != NULL
|
|
3175 || (e->array_index_expression() != NULL
|
131
|
3176 && e->array_index_expression()->end() != NULL)
|
111
|
3177 || (e->string_index_expression() != NULL
|
131
|
3178 && e->type()->is_string_type()))
|
111
|
3179 {
|
|
3180 Expression* underlying;
|
|
3181 if (e->field_reference_expression() != NULL)
|
|
3182 underlying = e->field_reference_expression()->expr();
|
|
3183 else if (e->type_guard_expression() != NULL)
|
|
3184 underlying = e->type_guard_expression()->expr();
|
|
3185 else if (e->array_index_expression() != NULL)
|
|
3186 underlying = e->array_index_expression()->array();
|
|
3187 else
|
|
3188 underlying = e->string_index_expression()->string();
|
|
3189
|
|
3190 Node* underlying_node = Node::make_node(underlying);
|
|
3191 this->flood(level, dst, underlying_node, -1);
|
|
3192 }
|
|
3193 else if ((e->field_reference_expression() != NULL
|
|
3194 && e->field_reference_expression()->expr()->unary_expression() != NULL)
|
|
3195 || e->array_index_expression() != NULL
|
|
3196 || e->map_index_expression() != NULL
|
|
3197 || (e->unary_expression() != NULL
|
|
3198 && e->unary_expression()->op() == OPERATOR_MULT))
|
|
3199 {
|
|
3200 Expression* underlying;
|
|
3201 if (e->field_reference_expression() != NULL)
|
|
3202 {
|
|
3203 underlying = e->field_reference_expression()->expr();
|
|
3204 underlying = underlying->unary_expression()->operand();
|
|
3205 }
|
|
3206 else if (e->array_index_expression() != NULL)
|
131
|
3207 underlying = e->array_index_expression()->array();
|
111
|
3208 else if (e->map_index_expression() != NULL)
|
|
3209 underlying = e->map_index_expression()->map();
|
|
3210 else
|
|
3211 underlying = e->unary_expression()->operand();
|
|
3212
|
|
3213 // Increase the level for a dereference.
|
|
3214 Node* underlying_node = Node::make_node(underlying);
|
|
3215 this->flood(level.increase(), dst, underlying_node, -1);
|
|
3216 }
|
131
|
3217 else if (e->temporary_reference_expression() != NULL)
|
|
3218 {
|
|
3219 Statement* t = e->temporary_reference_expression()->statement();
|
|
3220 this->flood(level, dst, Node::make_node(t), -1);
|
|
3221 }
|
111
|
3222 }
|
131
|
3223 else if (src->is_indirect())
|
|
3224 // Increase the level for a dereference.
|
|
3225 this->flood(level.increase(), dst, src->child(), -1);
|
111
|
3226
|
|
3227 level = level.copy();
|
|
3228 for (std::set<Node*>::const_iterator p = src_state->flows.begin();
|
|
3229 p != src_state->flows.end();
|
|
3230 ++p)
|
|
3231 this->flood(level, dst, *p, extra_loop_depth);
|
|
3232
|
|
3233 this->context_->decrease_pdepth();
|
|
3234 }
|
|
3235
|
|
3236 // Propagate escape information across the nodes modeled in this Analysis_set.
|
|
3237 // This is an implementation of gc/esc.go:escflood.
|
|
3238
|
|
3239 void
|
|
3240 Gogo::propagate_escape(Escape_context* context, Node* dst)
|
|
3241 {
|
131
|
3242 if (dst->object() == NULL
|
|
3243 && (dst->expr() == NULL
|
|
3244 || (dst->expr()->var_expression() == NULL
|
|
3245 && dst->expr()->enclosed_var_expression() == NULL
|
|
3246 && dst->expr()->func_expression() == NULL)))
|
|
3247 return;
|
|
3248
|
111
|
3249 Node::Escape_state* state = dst->state(context, NULL);
|
|
3250 Gogo* gogo = context->gogo();
|
|
3251 if (gogo->debug_escape_level() > 1)
|
|
3252 go_inform(Linemap::unknown_location(), "escflood:%d: dst %s scope:%s[%d]",
|
|
3253 context->flood_id(), dst->ast_format(gogo).c_str(),
|
|
3254 debug_function_name(state->fn).c_str(),
|
|
3255 state->loop_depth);
|
|
3256
|
|
3257 Escape_analysis_flood eaf(context);
|
|
3258 for (std::set<Node*>::const_iterator p = state->flows.begin();
|
|
3259 p != state->flows.end();
|
|
3260 ++p)
|
|
3261 {
|
|
3262 context->increase_flood_id();
|
|
3263 eaf.flood(Level::From(0), dst, *p, -1);
|
|
3264 }
|
|
3265 }
|
|
3266
|
|
3267 class Escape_analysis_tag
|
|
3268 {
|
|
3269 public:
|
|
3270 Escape_analysis_tag(Escape_context* context)
|
|
3271 : context_(context)
|
|
3272 { }
|
|
3273
|
|
3274 // Add notes to the function's type about the escape information of its
|
|
3275 // input parameters.
|
|
3276 void
|
|
3277 tag(Named_object* fn);
|
|
3278
|
|
3279 private:
|
|
3280 Escape_context* context_;
|
|
3281 };
|
|
3282
|
|
3283 void
|
|
3284 Escape_analysis_tag::tag(Named_object* fn)
|
|
3285 {
|
|
3286 // External functions are assumed unsafe
|
|
3287 // unless //go:noescape is given before the declaration.
|
131
|
3288 if (fn->package() != NULL)
|
|
3289 return;
|
|
3290
|
|
3291 if (fn->is_function_declaration())
|
111
|
3292 {
|
131
|
3293 Function_declaration* fdcl = fn->func_declaration_value();
|
|
3294 if ((fdcl->pragmas() & GOPRAGMA_NOESCAPE) != 0)
|
|
3295 {
|
|
3296 Function_type* fntype = fdcl->type();
|
|
3297 if (fntype->parameters() != NULL)
|
|
3298 {
|
|
3299 const Typed_identifier_list* til = fntype->parameters();
|
|
3300 int i = 0;
|
|
3301 for (Typed_identifier_list::const_iterator p = til->begin();
|
|
3302 p != til->end();
|
|
3303 ++p, ++i)
|
|
3304 if (p->type()->has_pointer())
|
|
3305 fntype->add_parameter_note(i, Node::ESCAPE_NONE);
|
|
3306 }
|
|
3307 }
|
111
|
3308 }
|
|
3309
|
131
|
3310 if (!fn->is_function())
|
|
3311 return;
|
|
3312
|
111
|
3313 Function_type* fntype = fn->func_value()->type();
|
|
3314 Bindings* bindings = fn->func_value()->block()->bindings();
|
|
3315
|
131
|
3316 if (fntype->is_method())
|
111
|
3317 {
|
131
|
3318 if (fntype->receiver()->name().empty()
|
|
3319 || Gogo::is_sink_name(fntype->receiver()->name()))
|
|
3320 // Unnamed receiver is not used in the function body, does not escape.
|
|
3321 fntype->add_receiver_note(Node::ESCAPE_NONE);
|
|
3322 else
|
|
3323 {
|
|
3324 Named_object* rcvr_no = bindings->lookup(fntype->receiver()->name());
|
|
3325 go_assert(rcvr_no != NULL);
|
|
3326 Node* rcvr_node = Node::make_node(rcvr_no);
|
|
3327 switch ((rcvr_node->encoding() & ESCAPE_MASK))
|
|
3328 {
|
|
3329 case Node::ESCAPE_NONE: // not touched by flood
|
|
3330 case Node::ESCAPE_RETURN:
|
|
3331 if (fntype->receiver()->type()->has_pointer())
|
|
3332 // Don't bother tagging for scalars.
|
|
3333 fntype->add_receiver_note(rcvr_node->encoding());
|
|
3334 break;
|
|
3335
|
|
3336 case Node::ESCAPE_HEAP: // flooded, moved to heap.
|
|
3337 break;
|
|
3338
|
|
3339 default:
|
|
3340 break;
|
|
3341 }
|
|
3342 }
|
111
|
3343 }
|
|
3344
|
|
3345 int i = 0;
|
|
3346 if (fntype->parameters() != NULL)
|
|
3347 {
|
|
3348 const Typed_identifier_list* til = fntype->parameters();
|
|
3349 for (Typed_identifier_list::const_iterator p = til->begin();
|
|
3350 p != til->end();
|
|
3351 ++p, ++i)
|
|
3352 {
|
|
3353 if (p->name().empty() || Gogo::is_sink_name(p->name()))
|
131
|
3354 {
|
|
3355 // Parameter not used in the function body, does not escape.
|
|
3356 if (p->type()->has_pointer())
|
|
3357 fntype->add_parameter_note(i, Node::ESCAPE_NONE);
|
|
3358 continue;
|
|
3359 }
|
111
|
3360
|
|
3361 Named_object* param_no = bindings->lookup(p->name());
|
|
3362 go_assert(param_no != NULL);
|
|
3363 Node* param_node = Node::make_node(param_no);
|
|
3364 switch ((param_node->encoding() & ESCAPE_MASK))
|
|
3365 {
|
|
3366 case Node::ESCAPE_NONE: // not touched by flood
|
|
3367 case Node::ESCAPE_RETURN:
|
|
3368 if (p->type()->has_pointer())
|
|
3369 // Don't bother tagging for scalars.
|
|
3370 fntype->add_parameter_note(i, param_node->encoding());
|
|
3371 break;
|
|
3372
|
|
3373 case Node::ESCAPE_HEAP: // flooded, moved to heap.
|
|
3374 break;
|
|
3375
|
|
3376 default:
|
|
3377 break;
|
|
3378 }
|
|
3379 }
|
|
3380 }
|
|
3381 fntype->set_is_tagged();
|
|
3382 }
|
|
3383
|
|
3384 // Tag each top-level function with escape information that will be used to
|
|
3385 // retain analysis results across imports.
|
|
3386
|
|
3387 void
|
|
3388 Gogo::tag_function(Escape_context* context, Named_object* fn)
|
|
3389 {
|
|
3390 Escape_analysis_tag eat(context);
|
|
3391 eat.tag(fn);
|
|
3392 }
|
131
|
3393
|
|
3394 // Reclaim memory of escape analysis Nodes.
|
|
3395
|
|
3396 void
|
|
3397 Gogo::reclaim_escape_nodes()
|
|
3398 {
|
|
3399 Node::reclaim_nodes();
|
|
3400 }
|
|
3401
|
|
3402 void
|
|
3403 Node::reclaim_nodes()
|
|
3404 {
|
|
3405 for (std::map<Named_object*, Node*>::iterator p = Node::objects.begin();
|
|
3406 p != Node::objects.end();
|
|
3407 ++p)
|
|
3408 delete p->second;
|
|
3409 Node::objects.clear();
|
|
3410
|
|
3411 for (std::map<Expression*, Node*>::iterator p = Node::expressions.begin();
|
|
3412 p != Node::expressions.end();
|
|
3413 ++p)
|
|
3414 delete p->second;
|
|
3415 Node::expressions.clear();
|
|
3416
|
|
3417 for (std::map<Statement*, Node*>::iterator p = Node::statements.begin();
|
|
3418 p != Node::statements.end();
|
|
3419 ++p)
|
|
3420 delete p->second;
|
|
3421 Node::statements.clear();
|
|
3422
|
|
3423 for (std::vector<Node*>::iterator p = Node::indirects.begin();
|
|
3424 p != Node::indirects.end();
|
|
3425 ++p)
|
|
3426 delete *p;
|
|
3427 Node::indirects.clear();
|
|
3428 }
|