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"
26 #include "analyze_irg_args.h"
29 #include "escape_ana.h"
35 typedef struct _walk_env {
36 ir_node *found_allocs; /**< list of all found non-escaped allocs */
37 ir_node *dead_allocs; /**< list of all found dead alloc */
38 unsigned nr_changed; /**< number of changed allocs */
39 unsigned nr_deads; /**< number of dead allocs */
41 /* these fields are only used in the global escape analysis */
42 ir_graph *irg; /**< the irg for this environment */
43 struct _walk_env *next; /**< for linking environments */
48 firm_dbg_module_t *dbgHandle;
51 * checks whether a Raise leaves a method
53 static int is_method_leaving_raise(ir_node *raise)
59 for (i = get_irn_n_outs(raise) - 1; i >= 0; --i) {
60 ir_node *succ = get_irn_out(raise, i);
62 /* there should be only one ProjX node */
63 if (get_Proj_proj(succ) == pn_Raise_X) {
70 /* Hmm: no ProjX from a Raise? This should be a verification
71 * error. For now we just assert and return.
73 assert(! "No ProjX after Raise found");
77 if (get_irn_n_outs(proj) != 1) {
78 /* Hmm: more than one user of ProjX: This is a verification
81 assert(! "More than one user of ProjX");
85 n = get_irn_out(proj, 0);
86 assert(is_Block(n) && "Argh: user of ProjX is no block");
88 if (n == get_irg_end_block(get_irn_irg(n)))
91 /* ok, we get here so the raise will not leave the function */
96 * determine if a value calculated by n "escape", ie
97 * is stored somewhere we could not track
99 static int can_escape(ir_node *n) {
102 /* should always be pointer mode or we made some mistake */
103 assert(mode_is_reference(get_irn_mode(n)));
105 for (i = get_irn_n_outs(n) - 1; i >= 0; --i) {
106 ir_node *succ = get_irn_out(n, i);
108 switch (get_irn_opcode(succ)) {
110 if (get_Store_value(succ) == n) {
112 * We are storing n. As long as we do not further
113 * evaluate things, the pointer 'escape' here
121 * Should not happen, but if it does we leave the pointer
122 * path and do not track further
126 case iro_Call: { /* most complicated case */
127 ir_node *ptr = get_Call_ptr(succ);
130 if (get_irn_op(ptr) == op_SymConst &&
131 get_SymConst_kind(ptr) == symconst_addr_ent) {
132 ent = get_SymConst_entity(ptr);
134 /* we know the called entity */
135 for (j = get_Call_n_params(succ) - 1; j >= 0; --j) {
136 if (get_Call_param(succ, j) == n) {
137 /* n is the j'th param of the call */
138 if (get_method_param_access(ent, j) & ptr_access_store)
139 /* n is store in ent */
144 else if (get_irn_op(ptr) == op_Sel) {
145 /* go through all possible callees */
146 for (k = get_Call_n_callees(succ) - 1; k >= 0; --k) {
147 ent = get_Call_callee(succ, k);
149 if (ent == unknown_entity) {
150 /* we don't know what will be called, a possible escape */
154 for (j = get_Call_n_params(succ) - 1; j >= 0; --j) {
155 if (get_Call_param(succ, j) == n) {
156 /* n is the j'th param of the call */
157 if (get_method_param_access(ent, j) & ptr_access_store)
158 /* n is store in ent */
164 else /* we don't know want will called */
171 /* Bad: the allocate object is returned */
175 /* Hmm: if we do NOT leave the method, it's local */
176 return is_method_leaving_raise(succ);
181 /* Bad: trace the tuple backwards */
182 for (j = get_irn_arity(succ) - 1; j >= 0; --j)
183 if (get_irn_n(succ, j) == n)
189 for (k = get_irn_n_outs(succ); k >= 0; --k) {
190 proj = get_irn_out(succ, k);
192 if (get_Proj_proj(proj) == j) {
193 /* we found the right Proj */
200 * If we haven't found the right Proj, succ is still
201 * the Tuple and the search will end here.
211 if (! mode_is_reference(get_irn_mode(succ)))
214 if (can_escape(succ))
221 * walker: search for Alloc nodes and follow the usages
223 static void find_allocations(ir_node *alloc, void *ctx)
227 walk_env_t *env = ctx;
229 if (get_irn_op(alloc) != op_Alloc)
232 /* we searching only for heap allocations */
233 if (get_Alloc_where(alloc) != heap_alloc)
237 for (i = get_irn_n_outs(alloc) - 1; i >= 0; --i) {
238 ir_node *proj = get_irn_out(alloc, i);
240 if (get_Proj_proj(proj) == pn_Alloc_res) {
248 * bad: no-one wants the result, should NOT happen but
249 * if it does we could delete it.
251 set_irn_link(alloc, env->dead_allocs);
252 env->dead_allocs = alloc;
257 if (! can_escape(adr)) {
258 set_irn_link(alloc, env->found_allocs);
259 env->found_allocs = alloc;
264 * do the necessary graph transformations
266 static void transform_allocs(ir_graph *irg, walk_env_t *env)
268 ir_node *alloc, *next, *mem, *sel;
275 /* kill all dead allocs */
276 for (alloc = env->dead_allocs; alloc; alloc = next) {
277 next = get_irn_link(alloc);
279 mem = get_Alloc_mem(alloc);
280 turn_into_tuple(alloc, pn_Alloc_max);
281 set_Tuple_pred(alloc, pn_Alloc_M, mem);
282 set_Tuple_pred(alloc, pn_Alloc_X_except, new_r_Bad(irg));
287 /* convert all non-escaped heap allocs into frame variables */
288 ftp = get_irg_frame_type(irg);
289 for (alloc = env->found_allocs; alloc; alloc = next) {
290 next = get_irn_link(alloc);
291 dbg = get_irn_dbg_info(alloc);
293 DBG((dbgHandle, LEVEL_1, "%+F allocation of %+F\n", irg, alloc));
295 snprintf(name, sizeof(name), "%s_NE_%u", get_entity_name(get_irg_entity(irg)), nr++);
296 ent = new_d_entity(ftp, new_id_from_str(name), get_Alloc_type(alloc), dbg);
298 sel = new_rd_simpleSel(dbg, irg, get_nodes_block(alloc),
299 get_irg_no_mem(irg), get_irg_frame(irg), ent);
300 mem = get_Alloc_mem(alloc);
302 turn_into_tuple(alloc, pn_Alloc_max);
303 set_Tuple_pred(alloc, pn_Alloc_M, mem);
304 set_Tuple_pred(alloc, pn_Alloc_X_except, new_r_Bad(irg));
305 set_Tuple_pred(alloc, pn_Alloc_res, sel);
310 if (env->nr_changed | env->nr_deads) {
311 set_irg_outs_inconsistent(irg);
314 set_irg_dom_inconsistent(irg);
318 /* Do simple and fast escape analysis for one graph. */
319 void escape_enalysis_irg(ir_graph *irg)
323 if (get_irg_callee_info_state(irg) != irg_callee_info_consistent) {
324 /* no way yet to calculate this for one irg */
325 assert(! "need callee info");
329 if (get_irg_outs_state(irg) != outs_consistent)
330 compute_irg_outs(irg);
332 env.found_allocs = NULL;
333 env.dead_allocs = NULL;
337 irg_walk_graph(irg, NULL, find_allocations, &env);
339 transform_allocs(irg, &env);
342 /* Do simple and fast escape analysis for all graphs. */
343 void escape_analysis(int run_scalar_replace)
348 walk_env_t *env, *elist;
350 if (get_irp_callee_info_state() != irg_callee_info_consistent) {
351 assert(! "need callee info");
356 dbgHandle = firm_dbg_register("firm.opt.escape_ana");
359 * We treat memory for speed: we first collect all info in a
360 * list of environments, than do the transformation.
361 * Doing it this way, no analysis info gets invalid while we run
367 env = obstack_alloc(&obst, sizeof(*env));
368 env->found_allocs = NULL;
369 env->dead_allocs = NULL;
371 for (i = get_irp_n_irgs() - 1; i >= 0; --i) {
372 irg = get_irp_irg(i);
374 if (get_irg_outs_state(irg) != outs_consistent)
375 compute_irg_outs(irg);
377 irg_walk_graph(irg, NULL, find_allocations, env);
379 if (env->found_allocs || env->dead_allocs) {
387 env = obstack_alloc(&obst, sizeof(*env));
388 env->found_allocs = NULL;
389 env->dead_allocs = NULL;
393 for (env = elist; env; env = env->next) {
394 transform_allocs(env->irg, env);
397 obstack_free(&obst, NULL);