annotate gcc/go/gofrontend/escape.h @ 145:1830386684a0

gcc-9.2.0
author anatofuz
date Thu, 13 Feb 2020 11:34:05 +0900
parents 84e7813d76e9
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
111
kono
parents:
diff changeset
1 // escape.h -- Go escape analysis (based on Go compiler algorithm).
kono
parents:
diff changeset
2
kono
parents:
diff changeset
3 // Copyright 2016 The Go Authors. All rights reserved.
kono
parents:
diff changeset
4 // Use of this source code is governed by a BSD-style
kono
parents:
diff changeset
5 // license that can be found in the LICENSE file.
kono
parents:
diff changeset
6
kono
parents:
diff changeset
7 #ifndef GO_ESCAPE_H
kono
parents:
diff changeset
8 #define GO_ESCAPE_H
kono
parents:
diff changeset
9
kono
parents:
diff changeset
10 #include "gogo.h"
kono
parents:
diff changeset
11
kono
parents:
diff changeset
12 class Named_object;
kono
parents:
diff changeset
13 class Expression;
kono
parents:
diff changeset
14 class Statement;
kono
parents:
diff changeset
15 class Escape_context;
kono
parents:
diff changeset
16
kono
parents:
diff changeset
17 // There can be loops in the escape graph that lead to arbitrary recursions.
kono
parents:
diff changeset
18 // See comment in gc/esc.go.
kono
parents:
diff changeset
19 static const int MIN_LEVEL = -2;
kono
parents:
diff changeset
20
kono
parents:
diff changeset
21 // Level models the escapement of a Node using two integers that are computed
kono
parents:
diff changeset
22 // by backwards-analyzing the flow of a function from its sink and increasing or
kono
parents:
diff changeset
23 // decreasing based on dereferences and addressing, respectively.
kono
parents:
diff changeset
24 // One integer, known as the level's VALUE (think absolute value), is just the
kono
parents:
diff changeset
25 // sum of indirections (via referencing or dereferencing) applied to the Node.
kono
parents:
diff changeset
26 // The second, known as the level's SUFFIX_VALUE, is the amount of indirections
kono
parents:
diff changeset
27 // applied after some data has been copied from the node. When accessing a
kono
parents:
diff changeset
28 // field F of an object O and then applying indirections, for example, the field
kono
parents:
diff changeset
29 // access O.F is assumed to copy that data from O before applying indirections.
kono
parents:
diff changeset
30 // With this, even if O.F escapes, it might mean that the content of O escape,
kono
parents:
diff changeset
31 // but not the object O itself.
kono
parents:
diff changeset
32
kono
parents:
diff changeset
33 class Level
kono
parents:
diff changeset
34 {
kono
parents:
diff changeset
35 public:
kono
parents:
diff changeset
36 Level()
kono
parents:
diff changeset
37 : value_(0), suffix_value_(0)
kono
parents:
diff changeset
38 { }
kono
parents:
diff changeset
39
kono
parents:
diff changeset
40 Level(int value, int suffix)
kono
parents:
diff changeset
41 : value_(value), suffix_value_(suffix)
kono
parents:
diff changeset
42 { }
kono
parents:
diff changeset
43
kono
parents:
diff changeset
44 // Return this level's value.
kono
parents:
diff changeset
45 int
kono
parents:
diff changeset
46 value() const
kono
parents:
diff changeset
47 { return this->value_; }
kono
parents:
diff changeset
48
kono
parents:
diff changeset
49 // Return this level's suffix value.
kono
parents:
diff changeset
50 int
kono
parents:
diff changeset
51 suffix_value() const
kono
parents:
diff changeset
52 { return this->suffix_value_; }
kono
parents:
diff changeset
53
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
54 // Increase the level because a node is dereferenced.
111
kono
parents:
diff changeset
55 Level
kono
parents:
diff changeset
56 increase() const
kono
parents:
diff changeset
57 {
kono
parents:
diff changeset
58 if (this->value_ <= MIN_LEVEL)
kono
parents:
diff changeset
59 return Level(MIN_LEVEL, 0);
kono
parents:
diff changeset
60
kono
parents:
diff changeset
61 return Level(this->value_ + 1, this->suffix_value_ + 1);
kono
parents:
diff changeset
62 }
kono
parents:
diff changeset
63
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
64 // Decrease the level because a node is referenced.
111
kono
parents:
diff changeset
65 Level
kono
parents:
diff changeset
66 decrease() const
kono
parents:
diff changeset
67 {
kono
parents:
diff changeset
68 if (this->value_ <= MIN_LEVEL)
kono
parents:
diff changeset
69 return Level(MIN_LEVEL, 0);
kono
parents:
diff changeset
70
kono
parents:
diff changeset
71 return Level(this->value_ - 1, this->suffix_value_ - 1);
kono
parents:
diff changeset
72 }
kono
parents:
diff changeset
73
kono
parents:
diff changeset
74 // Model a node being copied.
kono
parents:
diff changeset
75 Level
kono
parents:
diff changeset
76 copy() const
kono
parents:
diff changeset
77 {
kono
parents:
diff changeset
78 return Level(this->value_, std::max(this->suffix_value_, 0));
kono
parents:
diff changeset
79 }
kono
parents:
diff changeset
80
kono
parents:
diff changeset
81 // Return a level with the minimum values of this level and l.
kono
parents:
diff changeset
82 Level
kono
parents:
diff changeset
83 min(const Level& l) const
kono
parents:
diff changeset
84 {
kono
parents:
diff changeset
85 return Level(std::min(this->value_, l.value()),
kono
parents:
diff changeset
86 std::min(this->suffix_value_, l.suffix_value()));
kono
parents:
diff changeset
87 }
kono
parents:
diff changeset
88
kono
parents:
diff changeset
89 // Compare two levels for equality.
kono
parents:
diff changeset
90 bool
kono
parents:
diff changeset
91 operator==(const Level& l) const
kono
parents:
diff changeset
92 {
kono
parents:
diff changeset
93 return (this->value_ == l.value()
kono
parents:
diff changeset
94 && this->suffix_value_ == l.suffix_value());
kono
parents:
diff changeset
95 }
kono
parents:
diff changeset
96
kono
parents:
diff changeset
97 // Create a level from an integer value.
kono
parents:
diff changeset
98 static Level
kono
parents:
diff changeset
99 From(int i)
kono
parents:
diff changeset
100 {
kono
parents:
diff changeset
101 if (i <= MIN_LEVEL)
kono
parents:
diff changeset
102 return Level(MIN_LEVEL, 0);
kono
parents:
diff changeset
103 return Level(i, 0);
kono
parents:
diff changeset
104 }
kono
parents:
diff changeset
105
kono
parents:
diff changeset
106 private:
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
107 // The sum of all references (-1) and indirects (+1) applied to a Node.
111
kono
parents:
diff changeset
108 int value_;
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
109 // The sum of all references (-1) abd indirects (+1) applied to a copied Node.
111
kono
parents:
diff changeset
110 int suffix_value_;
kono
parents:
diff changeset
111 };
kono
parents:
diff changeset
112
kono
parents:
diff changeset
113 // A node in the escape graph. This node is an alias to a particular node
kono
parents:
diff changeset
114 // in the Go parse tree. Specifically, it can represent an expression node,
kono
parents:
diff changeset
115 // a statement node, or a named object node (a variable or function).
kono
parents:
diff changeset
116
kono
parents:
diff changeset
117 class Node
kono
parents:
diff changeset
118 {
kono
parents:
diff changeset
119 public:
kono
parents:
diff changeset
120 // This classification represents type of nodes in the Go parse tree that are
kono
parents:
diff changeset
121 // interesting during the analysis.
kono
parents:
diff changeset
122 enum Node_classification
kono
parents:
diff changeset
123 {
kono
parents:
diff changeset
124 NODE_OBJECT,
kono
parents:
diff changeset
125 NODE_EXPRESSION,
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
126 NODE_STATEMENT,
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
127 // A "fake" node that models the indirection of its child node.
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
128 // This node does not correspond to an AST node.
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
129 NODE_INDIRECT
111
kono
parents:
diff changeset
130 };
kono
parents:
diff changeset
131
kono
parents:
diff changeset
132 // The state necessary to keep track of how a node escapes.
kono
parents:
diff changeset
133 struct Escape_state
kono
parents:
diff changeset
134 {
kono
parents:
diff changeset
135 // The current function.
kono
parents:
diff changeset
136 Named_object* fn;
kono
parents:
diff changeset
137 // A list of source nodes that flow into this node.
kono
parents:
diff changeset
138 std::set<Node*> flows;
kono
parents:
diff changeset
139 // If the node is a function call, the list of nodes returned.
kono
parents:
diff changeset
140 std::vector<Node*> retvals;
kono
parents:
diff changeset
141 // The node's loop depth.
kono
parents:
diff changeset
142 int loop_depth;
kono
parents:
diff changeset
143 // There is an extra loop depth in the flood phase used to account for
kono
parents:
diff changeset
144 // variables referenced across closures. This is the maximum value of the
kono
parents:
diff changeset
145 // extra loop depth seen during the flood that touches this node.
kono
parents:
diff changeset
146 int max_extra_loop_depth;
kono
parents:
diff changeset
147 // The node's level.
kono
parents:
diff changeset
148 Level level;
kono
parents:
diff changeset
149 // An ID given to a node when it is encountered as a flow from the current
kono
parents:
diff changeset
150 // dst node. This is used to avoid infinite recursion of cyclic nodes.
kono
parents:
diff changeset
151 int flood_id;
kono
parents:
diff changeset
152
kono
parents:
diff changeset
153 Escape_state()
kono
parents:
diff changeset
154 : fn(NULL), loop_depth(0), max_extra_loop_depth(0), flood_id(0)
kono
parents:
diff changeset
155 { }
kono
parents:
diff changeset
156 };
kono
parents:
diff changeset
157
kono
parents:
diff changeset
158 // Note: values in this enum appear in export data, and therefore MUST NOT
kono
parents:
diff changeset
159 // change.
kono
parents:
diff changeset
160 enum Escapement_encoding
kono
parents:
diff changeset
161 {
kono
parents:
diff changeset
162 ESCAPE_UNKNOWN,
kono
parents:
diff changeset
163 // Does not escape to heap, result, or parameters.
kono
parents:
diff changeset
164 ESCAPE_NONE,
kono
parents:
diff changeset
165 // Is returned or reachable from a return statement.
kono
parents:
diff changeset
166 ESCAPE_RETURN,
kono
parents:
diff changeset
167 // Reachable from the heap.
kono
parents:
diff changeset
168 ESCAPE_HEAP,
kono
parents:
diff changeset
169 // By construction will not escape.
kono
parents:
diff changeset
170 ESCAPE_NEVER
kono
parents:
diff changeset
171 };
kono
parents:
diff changeset
172
kono
parents:
diff changeset
173 // Multiple constructors for each classification.
kono
parents:
diff changeset
174 Node(Named_object* no)
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
175 : classification_(NODE_OBJECT), state_(NULL), encoding_(ESCAPE_UNKNOWN),
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
176 child_(NULL)
111
kono
parents:
diff changeset
177 { this->u_.object_val = no; }
kono
parents:
diff changeset
178
kono
parents:
diff changeset
179 Node(Expression* e)
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
180 : classification_(NODE_EXPRESSION), state_(NULL), encoding_(ESCAPE_UNKNOWN),
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
181 child_(NULL)
111
kono
parents:
diff changeset
182 { this->u_.expression_val = e; }
kono
parents:
diff changeset
183
kono
parents:
diff changeset
184 Node(Statement* s)
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
185 : classification_(NODE_STATEMENT), state_(NULL), encoding_(ESCAPE_UNKNOWN),
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
186 child_(NULL)
111
kono
parents:
diff changeset
187 { this->u_.statement_val = s; }
kono
parents:
diff changeset
188
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
189 Node(Node *n)
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
190 : classification_(NODE_INDIRECT), state_(NULL), encoding_(ESCAPE_UNKNOWN),
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
191 child_(n)
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
192 {}
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
193
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
194 ~Node();
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
195
111
kono
parents:
diff changeset
196 // Return this node's type.
kono
parents:
diff changeset
197 Type*
kono
parents:
diff changeset
198 type() const;
kono
parents:
diff changeset
199
kono
parents:
diff changeset
200 // Return this node's location.
kono
parents:
diff changeset
201 Location
kono
parents:
diff changeset
202 location() const;
kono
parents:
diff changeset
203
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
204 // Return the location where the node's underlying object is defined.
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
205 Location
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
206 definition_location() const;
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
207
111
kono
parents:
diff changeset
208 // Return this node's AST formatted string.
kono
parents:
diff changeset
209 std::string
kono
parents:
diff changeset
210 ast_format(Gogo*) const;
kono
parents:
diff changeset
211
kono
parents:
diff changeset
212 // Return this node's detailed format string.
kono
parents:
diff changeset
213 std::string
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
214 details();
111
kono
parents:
diff changeset
215
kono
parents:
diff changeset
216 std::string
kono
parents:
diff changeset
217 op_format() const;
kono
parents:
diff changeset
218
kono
parents:
diff changeset
219 // Return this node's escape state.
kono
parents:
diff changeset
220 Escape_state*
kono
parents:
diff changeset
221 state(Escape_context* context, Named_object* fn);
kono
parents:
diff changeset
222
kono
parents:
diff changeset
223 // Return this node's escape encoding.
kono
parents:
diff changeset
224 int
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
225 encoding();
111
kono
parents:
diff changeset
226
kono
parents:
diff changeset
227 // Set the node's escape encoding.
kono
parents:
diff changeset
228 void
kono
parents:
diff changeset
229 set_encoding(int enc);
kono
parents:
diff changeset
230
kono
parents:
diff changeset
231 bool
kono
parents:
diff changeset
232 is_big(Escape_context*) const;
kono
parents:
diff changeset
233
kono
parents:
diff changeset
234 bool
kono
parents:
diff changeset
235 is_sink() const;
kono
parents:
diff changeset
236
kono
parents:
diff changeset
237 // Methods to return the underlying value in the Node union.
kono
parents:
diff changeset
238 Named_object*
kono
parents:
diff changeset
239 object() const
kono
parents:
diff changeset
240 {
kono
parents:
diff changeset
241 return (this->classification_ == NODE_OBJECT
kono
parents:
diff changeset
242 ? this->u_.object_val
kono
parents:
diff changeset
243 : NULL);
kono
parents:
diff changeset
244 }
kono
parents:
diff changeset
245
kono
parents:
diff changeset
246 Expression*
kono
parents:
diff changeset
247 expr() const
kono
parents:
diff changeset
248 {
kono
parents:
diff changeset
249 return (this->classification_ == NODE_EXPRESSION
kono
parents:
diff changeset
250 ? this->u_.expression_val
kono
parents:
diff changeset
251 : NULL);
kono
parents:
diff changeset
252 }
kono
parents:
diff changeset
253
kono
parents:
diff changeset
254 Statement*
kono
parents:
diff changeset
255 statement() const
kono
parents:
diff changeset
256 {
kono
parents:
diff changeset
257 return (this->classification_ == NODE_STATEMENT
kono
parents:
diff changeset
258 ? this->u_.statement_val
kono
parents:
diff changeset
259 : NULL);
kono
parents:
diff changeset
260 }
kono
parents:
diff changeset
261
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
262 bool
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
263 is_indirect() const
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
264 { return this->classification_ == NODE_INDIRECT; }
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
265
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
266 // Return its child node.
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
267 // Child node is used only in indirect node, and in expression node
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
268 // representing slicing an array.
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
269 Node*
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
270 child() const
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
271 { return this->child_; }
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
272
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
273 // Set the child node.
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
274 void
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
275 set_child(Node* n)
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
276 { this->child_ = n; }
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
277
111
kono
parents:
diff changeset
278 // Static creation methods for each value supported in the union.
kono
parents:
diff changeset
279 static Node*
kono
parents:
diff changeset
280 make_node(Named_object*);
kono
parents:
diff changeset
281
kono
parents:
diff changeset
282 static Node*
kono
parents:
diff changeset
283 make_node(Expression*);
kono
parents:
diff changeset
284
kono
parents:
diff changeset
285 static Node*
kono
parents:
diff changeset
286 make_node(Statement*);
kono
parents:
diff changeset
287
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
288 static Node*
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
289 make_indirect_node(Node*);
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
290
111
kono
parents:
diff changeset
291 // Return the maximum of an existing escape encoding E and a new
kono
parents:
diff changeset
292 // escape type.
kono
parents:
diff changeset
293 static int
kono
parents:
diff changeset
294 max_encoding(int e, int etype);
kono
parents:
diff changeset
295
kono
parents:
diff changeset
296 // Return a modified encoding for an input parameter that flows into an
kono
parents:
diff changeset
297 // output parameter.
kono
parents:
diff changeset
298 static int
kono
parents:
diff changeset
299 note_inout_flows(int e, int index, Level level);
kono
parents:
diff changeset
300
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
301 // Reclaim nodes.
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
302 static void
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
303 reclaim_nodes();
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
304
111
kono
parents:
diff changeset
305 private:
kono
parents:
diff changeset
306 // The classification of this Node.
kono
parents:
diff changeset
307 Node_classification classification_;
kono
parents:
diff changeset
308 // The value union.
kono
parents:
diff changeset
309 union
kono
parents:
diff changeset
310 {
kono
parents:
diff changeset
311 // If NODE_OBJECT.
kono
parents:
diff changeset
312 Named_object* object_val;
kono
parents:
diff changeset
313 // If NODE_EXPRESSION.
kono
parents:
diff changeset
314 Expression* expression_val;
kono
parents:
diff changeset
315 // If NODE_STATEMENT.
kono
parents:
diff changeset
316 Statement* statement_val;
kono
parents:
diff changeset
317 } u_;
kono
parents:
diff changeset
318 // The node's escape state.
kono
parents:
diff changeset
319 Escape_state* state_;
kono
parents:
diff changeset
320 // The node's escape encoding.
kono
parents:
diff changeset
321 // The encoding:
kono
parents:
diff changeset
322 // | Return Encoding: (width - ESCAPE_RETURN_BITS) |
kono
parents:
diff changeset
323 // | Content Escapes bit: 1 |
kono
parents:
diff changeset
324 // | Escapement_encoding: ESCAPE_BITS |
kono
parents:
diff changeset
325 int encoding_;
kono
parents:
diff changeset
326
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
327 // Child node, used only in indirect node, and expression node representing
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
328 // slicing an array.
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
329 Node* child_;
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
330
111
kono
parents:
diff changeset
331 // Cache all the Nodes created via Node::make_node to make the API simpler.
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
332 static Unordered_map(Named_object*, Node*) objects;
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
333 static Unordered_map(Expression*, Node*) expressions;
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
334 static Unordered_map(Statement*, Node*) statements;
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
335
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
336 // Collection of all NODE_INDIRECT Nodes, used for reclaiming memory. This
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
337 // is not a cache -- each make_indirect_node will make a fresh Node.
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
338 static std::vector<Node*> indirects;
111
kono
parents:
diff changeset
339 };
kono
parents:
diff changeset
340
kono
parents:
diff changeset
341 // The amount of bits used for the escapement encoding.
kono
parents:
diff changeset
342 static const int ESCAPE_BITS = 3;
kono
parents:
diff changeset
343
kono
parents:
diff changeset
344 // Mask used to extract encoding.
kono
parents:
diff changeset
345 static const int ESCAPE_MASK = (1 << ESCAPE_BITS) - 1;
kono
parents:
diff changeset
346
kono
parents:
diff changeset
347 // Value obtained by indirect of parameter escapes to heap.
kono
parents:
diff changeset
348 static const int ESCAPE_CONTENT_ESCAPES = 1 << ESCAPE_BITS;
kono
parents:
diff changeset
349
kono
parents:
diff changeset
350 // The amount of bits used in encoding of return values.
kono
parents:
diff changeset
351 static const int ESCAPE_RETURN_BITS = ESCAPE_BITS + 1;
kono
parents:
diff changeset
352
kono
parents:
diff changeset
353 // For each output, the number of bits for a tag.
kono
parents:
diff changeset
354 static const int ESCAPE_BITS_PER_OUTPUT_IN_TAG = 3;
kono
parents:
diff changeset
355
kono
parents:
diff changeset
356 // The bit max to extract a single tag.
kono
parents:
diff changeset
357 static const int ESCAPE_BITS_MASK_FOR_TAG = (1 << ESCAPE_BITS_PER_OUTPUT_IN_TAG) - 1;
kono
parents:
diff changeset
358
kono
parents:
diff changeset
359 // The largest level that can be stored in a tag.
kono
parents:
diff changeset
360 static const int ESCAPE_MAX_ENCODED_LEVEL = ESCAPE_BITS_MASK_FOR_TAG - 1;
kono
parents:
diff changeset
361
kono
parents:
diff changeset
362 // A helper for converting escape notes from encoded integers to a
kono
parents:
diff changeset
363 // textual format and vice-versa.
kono
parents:
diff changeset
364
kono
parents:
diff changeset
365 class Escape_note
kono
parents:
diff changeset
366 {
kono
parents:
diff changeset
367 public:
kono
parents:
diff changeset
368 // Return the string representation of an escapement encoding.
kono
parents:
diff changeset
369 static std::string
kono
parents:
diff changeset
370 make_tag(int encoding);
kono
parents:
diff changeset
371
kono
parents:
diff changeset
372 // Return the escapement encoding for a string tag.
kono
parents:
diff changeset
373 static int
kono
parents:
diff changeset
374 parse_tag(std::string* tag);
kono
parents:
diff changeset
375 };
kono
parents:
diff changeset
376
kono
parents:
diff changeset
377 // The escape context for a set of functions being analyzed.
kono
parents:
diff changeset
378
kono
parents:
diff changeset
379 class Escape_context
kono
parents:
diff changeset
380 {
kono
parents:
diff changeset
381 public:
kono
parents:
diff changeset
382 Escape_context(Gogo* gogo, bool recursive);
kono
parents:
diff changeset
383
kono
parents:
diff changeset
384 // Return the Go IR.
kono
parents:
diff changeset
385 Gogo*
kono
parents:
diff changeset
386 gogo() const
kono
parents:
diff changeset
387 { return this->gogo_; }
kono
parents:
diff changeset
388
kono
parents:
diff changeset
389 // Return the current function being analyzed.
kono
parents:
diff changeset
390 Named_object*
kono
parents:
diff changeset
391 current_function() const
kono
parents:
diff changeset
392 { return this->current_function_; }
kono
parents:
diff changeset
393
kono
parents:
diff changeset
394 // Change the function being analyzed.
kono
parents:
diff changeset
395 void
kono
parents:
diff changeset
396 set_current_function(Named_object* fn)
kono
parents:
diff changeset
397 { this->current_function_ = fn; }
kono
parents:
diff changeset
398
kono
parents:
diff changeset
399 // Return the name of the current function.
kono
parents:
diff changeset
400 std::string
kono
parents:
diff changeset
401 current_function_name() const;
kono
parents:
diff changeset
402
kono
parents:
diff changeset
403 // Return true if this is the context for a mutually recursive set of functions.
kono
parents:
diff changeset
404 bool
kono
parents:
diff changeset
405 recursive() const
kono
parents:
diff changeset
406 { return this->recursive_; }
kono
parents:
diff changeset
407
kono
parents:
diff changeset
408 // Return the special sink node for this context.
kono
parents:
diff changeset
409 Node*
kono
parents:
diff changeset
410 sink()
kono
parents:
diff changeset
411 { return this->sink_; }
kono
parents:
diff changeset
412
kono
parents:
diff changeset
413 // Return the current loop depth.
kono
parents:
diff changeset
414 int
kono
parents:
diff changeset
415 loop_depth() const
kono
parents:
diff changeset
416 { return this->loop_depth_; }
kono
parents:
diff changeset
417
kono
parents:
diff changeset
418 // Increase the loop depth.
kono
parents:
diff changeset
419 void
kono
parents:
diff changeset
420 increase_loop_depth()
kono
parents:
diff changeset
421 { this->loop_depth_++; }
kono
parents:
diff changeset
422
kono
parents:
diff changeset
423 // Decrease the loop depth.
kono
parents:
diff changeset
424 void
kono
parents:
diff changeset
425 decrease_loop_depth()
kono
parents:
diff changeset
426 { this->loop_depth_--; }
kono
parents:
diff changeset
427
kono
parents:
diff changeset
428 void
kono
parents:
diff changeset
429 set_loop_depth(int depth)
kono
parents:
diff changeset
430 { this->loop_depth_ = depth; }
kono
parents:
diff changeset
431
kono
parents:
diff changeset
432 // Return the destination nodes encountered in this context.
kono
parents:
diff changeset
433 const std::set<Node*>&
kono
parents:
diff changeset
434 dsts() const
kono
parents:
diff changeset
435 { return this->dsts_; }
kono
parents:
diff changeset
436
kono
parents:
diff changeset
437 // Add a destination node.
kono
parents:
diff changeset
438 void
kono
parents:
diff changeset
439 add_dst(Node* dst)
kono
parents:
diff changeset
440 { this->dsts_.insert(dst); }
kono
parents:
diff changeset
441
kono
parents:
diff changeset
442 // Return the nodes initially marked as non-escaping before flooding.
kono
parents:
diff changeset
443 const std::vector<Node*>&
kono
parents:
diff changeset
444 non_escaping_nodes() const
kono
parents:
diff changeset
445 { return this->noesc_; }
kono
parents:
diff changeset
446
kono
parents:
diff changeset
447 // Initialize the dummy return values for this Node N using the results
kono
parents:
diff changeset
448 // in FNTYPE.
kono
parents:
diff changeset
449 void
kono
parents:
diff changeset
450 init_retvals(Node* n, Function_type* fntype);
kono
parents:
diff changeset
451
kono
parents:
diff changeset
452 // Return the indirection of Node N.
kono
parents:
diff changeset
453 Node*
kono
parents:
diff changeset
454 add_dereference(Node* n);
kono
parents:
diff changeset
455
kono
parents:
diff changeset
456 // Keep track of possibly non-escaping node N.
kono
parents:
diff changeset
457 void
kono
parents:
diff changeset
458 track(Node* n);
kono
parents:
diff changeset
459
kono
parents:
diff changeset
460 int
kono
parents:
diff changeset
461 flood_id() const
kono
parents:
diff changeset
462 { return this->flood_id_; }
kono
parents:
diff changeset
463
kono
parents:
diff changeset
464 void
kono
parents:
diff changeset
465 increase_flood_id()
kono
parents:
diff changeset
466 { this->flood_id_++; }
kono
parents:
diff changeset
467
kono
parents:
diff changeset
468 int
kono
parents:
diff changeset
469 pdepth() const
kono
parents:
diff changeset
470 { return this->pdepth_; }
kono
parents:
diff changeset
471
kono
parents:
diff changeset
472 void
kono
parents:
diff changeset
473 increase_pdepth()
kono
parents:
diff changeset
474 { this->pdepth_++; }
kono
parents:
diff changeset
475
kono
parents:
diff changeset
476 void
kono
parents:
diff changeset
477 decrease_pdepth()
kono
parents:
diff changeset
478 { this->pdepth_--; }
kono
parents:
diff changeset
479
kono
parents:
diff changeset
480 private:
kono
parents:
diff changeset
481 // The Go IR.
kono
parents:
diff changeset
482 Gogo* gogo_;
kono
parents:
diff changeset
483 // The current function being analyzed.
kono
parents:
diff changeset
484 Named_object* current_function_;
kono
parents:
diff changeset
485 // Return whether this is the context for a recursive function or a group of mutually
kono
parents:
diff changeset
486 // recursive functions.
kono
parents:
diff changeset
487 bool recursive_;
kono
parents:
diff changeset
488 // The sink for this escape context. Nodes whose reference objects created
kono
parents:
diff changeset
489 // outside the current function are assigned to the sink as well as nodes that
kono
parents:
diff changeset
490 // the analysis loses track of.
kono
parents:
diff changeset
491 Node* sink_;
kono
parents:
diff changeset
492 // Used to detect nested loop scopes.
kono
parents:
diff changeset
493 int loop_depth_;
kono
parents:
diff changeset
494 // All the destination nodes considered in this set of analyzed functions.
kono
parents:
diff changeset
495 std::set<Node*> dsts_;
kono
parents:
diff changeset
496 // All the nodes that were noted as possibly not escaping in this context.
kono
parents:
diff changeset
497 std::vector<Node*> noesc_;
kono
parents:
diff changeset
498 // An ID given to each dst and the flows discovered through DFS of that dst.
kono
parents:
diff changeset
499 // This is used to avoid infinite recursion from nodes that point to each
kono
parents:
diff changeset
500 // other within the flooding phase.
kono
parents:
diff changeset
501 int flood_id_;
kono
parents:
diff changeset
502 // The current level of recursion within a flooded section; used to debug.
kono
parents:
diff changeset
503 int pdepth_;
kono
parents:
diff changeset
504 };
kono
parents:
diff changeset
505
kono
parents:
diff changeset
506 #endif // !defined(GO_ESCAPE_H)