3 * File name: ir/opt/escape_ana.c
4 * Purpose: escape analysis and optimization
9 * Copyright: (c) 1999-2005 Universität Karlsruhe
10 * Licence: This file protected by GPL - GNU GENERAL PUBLIC LICENSE.
16 * A fast and simple Escape analysis.
23 #include "irgraph_t.h"
27 #include "analyze_irg_args.h"
30 #include "escape_ana.h"
36 typedef struct _walk_env {
37 ir_node *found_allocs; /**< list of all found non-escaped allocs */
38 ir_node *dead_allocs; /**< list of all found dead alloc */
39 unsigned nr_removed; /**< number of removed allocs (placed of frame) */
40 unsigned nr_changed; /**< number of changed allocs (allocated on stack now) */
41 unsigned nr_deads; /**< number of dead allocs */
43 /* these fields are only used in the global escape analysis */
44 ir_graph *irg; /**< the irg for this environment */
45 struct _walk_env *next; /**< for linking environments */
50 firm_dbg_module_t *dbgHandle;
53 * checks whether a Raise leaves a method
55 static int is_method_leaving_raise(ir_node *raise)
61 for (i = get_irn_n_outs(raise) - 1; i >= 0; --i) {
62 ir_node *succ = get_irn_out(raise, i);
64 /* there should be only one ProjX node */
65 if (get_Proj_proj(succ) == pn_Raise_X) {
72 /* Hmm: no ProjX from a Raise? This should be a verification
73 * error. For now we just assert and return.
75 assert(! "No ProjX after Raise found");
79 if (get_irn_n_outs(proj) != 1) {
80 /* Hmm: more than one user of ProjX: This is a verification
83 assert(! "More than one user of ProjX");
87 n = get_irn_out(proj, 0);
88 assert(is_Block(n) && "Argh: user of ProjX is no block");
90 if (n == get_irg_end_block(get_irn_irg(n)))
93 /* ok, we get here so the raise will not leave the function */
98 * determine if a value calculated by n "escape", ie
99 * is stored somewhere we could not track
101 static int can_escape(ir_node *n) {
104 /* should always be pointer mode or we made some mistake */
105 assert(mode_is_reference(get_irn_mode(n)));
107 for (i = get_irn_n_outs(n) - 1; i >= 0; --i) {
108 ir_node *succ = get_irn_out(n, i);
110 switch (get_irn_opcode(succ)) {
112 if (get_Store_value(succ) == n) {
114 * We are storing n. As long as we do not further
115 * evaluate things, the pointer 'escape' here
123 * Should not happen, but if it does we leave the pointer
124 * path and do not track further
128 case iro_Call: { /* most complicated case */
129 ir_node *ptr = get_Call_ptr(succ);
132 if (get_irn_op(ptr) == op_SymConst &&
133 get_SymConst_kind(ptr) == symconst_addr_ent) {
134 ent = get_SymConst_entity(ptr);
136 /* we know the called entity */
137 for (j = get_Call_n_params(succ) - 1; j >= 0; --j) {
138 if (get_Call_param(succ, j) == n) {
139 /* n is the j'th param of the call */
140 if (get_method_param_access(ent, j) & ptr_access_store)
141 /* n is store in ent */
146 else if (get_irn_op(ptr) == op_Sel) {
147 /* go through all possible callees */
148 for (k = get_Call_n_callees(succ) - 1; k >= 0; --k) {
149 ent = get_Call_callee(succ, k);
151 if (ent == unknown_entity) {
152 /* we don't know what will be called, a possible escape */
156 for (j = get_Call_n_params(succ) - 1; j >= 0; --j) {
157 if (get_Call_param(succ, j) == n) {
158 /* n is the j'th param of the call */
159 if (get_method_param_access(ent, j) & ptr_access_store)
160 /* n is store in ent */
166 else /* we don't know want will called */
173 /* Bad: the allocate object is returned */
177 /* Hmm: if we do NOT leave the method, it's local */
178 return is_method_leaving_raise(succ);
183 /* Bad: trace the tuple backwards */
184 for (j = get_irn_arity(succ) - 1; j >= 0; --j)
185 if (get_irn_n(succ, j) == n)
191 for (k = get_irn_n_outs(succ); k >= 0; --k) {
192 proj = get_irn_out(succ, k);
194 if (get_Proj_proj(proj) == j) {
195 /* we found the right Proj */
202 * If we haven't found the right Proj, succ is still
203 * the Tuple and the search will end here.
213 if (! mode_is_reference(get_irn_mode(succ)))
216 if (can_escape(succ))
223 * walker: search for Alloc nodes and follow the usages
225 static void find_allocations(ir_node *alloc, void *ctx)
229 walk_env_t *env = ctx;
231 if (get_irn_op(alloc) != op_Alloc)
234 /* we searching only for heap allocations */
235 if (get_Alloc_where(alloc) != heap_alloc)
239 for (i = get_irn_n_outs(alloc) - 1; i >= 0; --i) {
240 ir_node *proj = get_irn_out(alloc, i);
242 if (get_Proj_proj(proj) == pn_Alloc_res) {
250 * bad: no-one wants the result, should NOT happen but
251 * if it does we could delete it.
253 set_irn_link(alloc, env->dead_allocs);
254 env->dead_allocs = alloc;
259 if (! can_escape(adr)) {
260 set_irn_link(alloc, env->found_allocs);
261 env->found_allocs = alloc;
266 * do the necessary graph transformations
268 static void transform_allocs(ir_graph *irg, walk_env_t *env)
270 ir_node *alloc, *next, *mem, *sel, *size;
271 type *ftp, *atp, *tp;
277 /* kill all dead allocs */
278 for (alloc = env->dead_allocs; alloc; alloc = next) {
279 next = get_irn_link(alloc);
281 DBG((dbgHandle, LEVEL_1, "%+F allocation of %+F unused, deleted.\n", irg, alloc));
283 mem = get_Alloc_mem(alloc);
284 turn_into_tuple(alloc, pn_Alloc_max);
285 set_Tuple_pred(alloc, pn_Alloc_M, mem);
286 set_Tuple_pred(alloc, pn_Alloc_X_except, new_r_Bad(irg));
291 /* convert all non-escaped heap allocs into frame variables */
292 ftp = get_irg_frame_type(irg);
293 for (alloc = env->found_allocs; alloc; alloc = next) {
294 next = get_irn_link(alloc);
295 size = get_Alloc_size(alloc);
296 atp = get_Alloc_type(alloc);
299 if (get_irn_op(size) == op_SymConst && get_SymConst_kind(size) == symconst_size) {
300 /* if the size is a type size and the types matched */
301 assert(atp == get_SymConst_type(size));
304 else if (is_Const(size)) {
305 tarval *tv = get_Const_tarval(size);
307 if (tv != tarval_bad && tarval_is_long(tv) &&
308 get_type_state(atp) == layout_fixed &&
309 get_tarval_long(tv) == get_type_size_bytes(atp)) {
310 /* a already lowered type size */
315 if (tp && tp != firm_unknown_type) {
316 /* we could determine the type, so we could place it on the frame */
317 dbg = get_irn_dbg_info(alloc);
319 DBG((dbgHandle, LEVEL_DEFAULT, "%+F allocation of %+F type %+F placed on frame\n", irg, alloc, tp));
321 snprintf(name, sizeof(name), "%s_NE_%u", get_entity_name(get_irg_entity(irg)), nr++);
322 ent = new_d_entity(ftp, new_id_from_str(name), get_Alloc_type(alloc), dbg);
324 sel = new_rd_simpleSel(dbg, irg, get_nodes_block(alloc),
325 get_irg_no_mem(irg), get_irg_frame(irg), ent);
326 mem = get_Alloc_mem(alloc);
328 turn_into_tuple(alloc, pn_Alloc_max);
329 set_Tuple_pred(alloc, pn_Alloc_M, mem);
330 set_Tuple_pred(alloc, pn_Alloc_X_except, new_r_Bad(irg));
331 set_Tuple_pred(alloc, pn_Alloc_res, sel);
337 * We could not determine the type or it is variable size.
338 * At least, we could place it on the stack
340 DBG((dbgHandle, LEVEL_DEFAULT, "%+F allocation of %+F type %+F placed on stack\n", irg, alloc));
341 set_Alloc_where(alloc, stack_alloc);
347 /* if allocs were removed somehow */
348 if (env->nr_removed | env->nr_deads) {
349 set_irg_outs_inconsistent(irg);
352 set_irg_dom_inconsistent(irg);
356 /* Do simple and fast escape analysis for one graph. */
357 void escape_enalysis_irg(ir_graph *irg)
361 if (get_irg_callee_info_state(irg) != irg_callee_info_consistent) {
362 /* no way yet to calculate this for one irg */
363 assert(! "need callee info");
367 if (get_irg_outs_state(irg) != outs_consistent)
368 compute_irg_outs(irg);
370 env.found_allocs = NULL;
371 env.dead_allocs = NULL;
376 irg_walk_graph(irg, NULL, find_allocations, &env);
378 transform_allocs(irg, &env);
381 /* Do simple and fast escape analysis for all graphs. */
382 void escape_analysis(int run_scalar_replace)
387 walk_env_t *env, *elist;
389 if (get_irp_callee_info_state() != irg_callee_info_consistent) {
390 assert(! "need callee info");
395 dbgHandle = firm_dbg_register("firm.opt.escape_ana");
398 * We treat memory for speed: we first collect all info in a
399 * list of environments, than do the transformation.
400 * Doing it this way, no analysis info gets invalid while we run
406 env = obstack_alloc(&obst, sizeof(*env));
407 env->found_allocs = NULL;
408 env->dead_allocs = NULL;
410 for (i = get_irp_n_irgs() - 1; i >= 0; --i) {
411 irg = get_irp_irg(i);
413 if (get_irg_outs_state(irg) != outs_consistent)
414 compute_irg_outs(irg);
416 irg_walk_graph(irg, NULL, find_allocations, env);
418 if (env->found_allocs || env->dead_allocs) {
426 env = obstack_alloc(&obst, sizeof(*env));
427 env->found_allocs = NULL;
428 env->dead_allocs = NULL;
432 for (env = elist; env; env = env->next) {
433 transform_allocs(env->irg, env);
436 obstack_free(&obst, NULL);