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.
13 /** @file escape_ana.c
22 #include "irgraph_t.h"
25 #include "analyze_irg_args.h"
28 #include "escape_ana.h"
33 typedef struct _walk_env {
34 ir_node *found_allocs; /**< list of all found non-escaped allocs */
35 ir_node *dead_allocs; /**< list of all found dead alloc */
36 unsigned nr_changed; /**< number of changed allocs */
37 unsigned nr_deads; /**< number of dead allocs */
39 /* these fields are only used in the global escape analysis */
40 ir_graph *irg; /**< the irg for this environment */
41 struct _walk_env *next; /**< for linking environments */
46 * checks whether a Raise leaves a method
48 static int is_method_leaving_raise(ir_node *raise)
54 for (i = get_irn_n_outs(raise) - 1; i >= 0; --i) {
55 ir_node *succ = get_irn_out(raise, i);
57 /* there should be only one ProjX node */
58 if (get_Proj_proj(succ) == pn_Raise_X) {
65 /* Hmm: no ProjX from a Raise? This should be a verification
66 * error. For now we just assert and return.
68 assert(! "No ProjX after Raise found");
72 if (get_irn_n_outs(proj) != 1) {
73 /* Hmm: more than one user of ProjX: This is a verification
76 assert(! "More than one user of ProjX");
80 n = get_irn_out(proj, 0);
81 if (get_irn_op(n) == op_End)
84 assert(is_Block(n) && "Argh: user of ProjX is neither block for End");
86 /* ok, we get here so the raise will not leave the function */
91 * determine if a value calculated by n "escape", ie
92 * is stored somewhere we could not track
94 static int do_escape(ir_node *n) {
97 /* should always be pointer mode or we made some mistake */
98 assert(mode_is_reference(get_irn_mode(n)));
100 for (i = get_irn_n_outs(n) - 1; i >= 0; --i) {
101 ir_node *succ = get_irn_out(n, i);
102 ir_op *op = get_irn_op(succ);
104 switch (get_irn_opcode(succ)) {
106 if (get_Store_value(succ) == n) {
108 * We are storing n. As long as we do not further
109 * evaluate things, the pointer 'escape' here
117 * Should not happen, but if it does we leave the pointer
118 * path and do not track further
122 case iro_Call: { /* most complicated case */
123 ir_node *ptr = get_Call_ptr(succ);
126 if (get_irn_op(ptr) == op_SymConst &&
127 get_SymConst_kind(ptr) == symconst_addr_ent) {
128 ent = get_SymConst_entity(ptr);
130 /* we know the called entity */
131 for (j = get_Call_n_params(succ) - 1; j >= 0; --j) {
132 if (get_Call_param(succ, j) == n) {
133 /* n is the j'th param of the call */
134 if (get_method_param_access(ent, j) & ptr_access_store)
135 /* n is store in ent */
141 /* go through all possible callees */
142 for (k = get_Call_n_callees(succ) - 1; k >= 0; --k) {
143 ent = get_Call_callee(succ, k);
145 for (j = get_Call_n_params(succ) - 1; j >= 0; --j) {
146 if (get_Call_param(succ, j) == n) {
147 /* n is the j'th param of the call */
148 if (get_method_param_access(ent, j) & ptr_access_store)
149 /* n is store in ent */
159 /* Bad: the allocate object is returned */
163 /* Hmm: if we do NOT leave the method, it's local */
164 return is_method_leaving_raise(succ);
169 /* Bad: trace the tuple backwards */
170 for (j = get_irn_arity(succ) - 1; j >= 0; --j)
171 if (get_irn_n(succ, j) == n)
177 for (k = get_irn_n_outs(succ); k >= 0; --k) {
178 proj = get_irn_out(succ, k);
180 if (get_Proj_proj(proj) == j) {
181 /* we found the right Proj */
188 * If we haven't found the right Proj, succ is still
189 * the Tuple and the search will end here.
199 if (! mode_is_reference(get_irn_mode(succ)))
209 * walker: search for Alloc nodes and follow the usages
211 static void find_allocations(ir_node *alloc, void *ctx)
215 walk_env_t *env = ctx;
217 if (get_irn_op(alloc) != op_Alloc)
220 /* we searching only for heap allocations */
221 if (get_Alloc_where(alloc) != heap_alloc)
225 for (i = get_irn_n_outs(alloc) - 1; i >= 0; --i) {
226 ir_node *proj = get_irn_out(alloc, i);
228 if (get_Proj_proj(proj) == pn_Alloc_res) {
236 * bad: no-one wants the result, should NOT happen but
237 * if it does we could delete it.
239 set_irn_link(alloc, env->dead_allocs);
240 env->dead_allocs = alloc;
245 if (! do_escape(adr)) {
246 set_irn_link(alloc, env->found_allocs);
247 env->found_allocs = alloc;
252 * do the necessary graph transformations
254 static void transform_allocs(ir_graph *irg, walk_env_t *env)
256 ir_node *alloc, *next, *mem, *sel;
263 /* kill all dead allocs */
264 for (alloc = env->dead_allocs; alloc; alloc = next) {
265 next = get_irn_link(alloc);
267 mem = get_Alloc_mem(alloc);
268 turn_into_tuple(alloc, pn_Alloc_max);
269 set_Tuple_pred(alloc, pn_Alloc_M, mem);
270 set_Tuple_pred(alloc, pn_Alloc_X_except, new_r_Bad(irg));
275 /* convert all non-escaped heap allocs into frame variables */
276 ftp = get_irg_frame_type(irg);
277 for (alloc = env->found_allocs; alloc; alloc = next) {
278 next = get_irn_link(alloc);
279 dbg = get_irn_dbg_info(alloc);
281 snprintf(name, sizeof(name), "_not_escaped_%u", nr++);
282 ent = new_d_entity(ftp, new_id_from_str(name), get_Alloc_type(alloc), dbg);
284 sel = new_rd_simpleSel(dbg, irg, get_nodes_block(alloc),
285 get_irg_no_mem(irg), get_irg_frame(irg), ent);
286 mem = get_Alloc_mem(alloc);
288 turn_into_tuple(alloc, pn_Alloc_max);
289 set_Tuple_pred(alloc, pn_Alloc_M, mem);
290 set_Tuple_pred(alloc, pn_Alloc_X_except, new_r_Bad(irg));
291 set_Tuple_pred(alloc, pn_Alloc_res, sel);
296 if (env->nr_changed | env->nr_deads) {
297 set_irg_outs_inconsistent(irg);
300 set_irg_dom_inconsistent(irg);
304 /* Do simple and fast escape analysis for one graph. */
305 void escape_enalysis_irg(ir_graph *irg)
309 if (get_irg_callee_info_state(irg) != irg_callee_info_consistent) {
310 /* no way yet to calculate this for one irg */
311 assert(! "need callee info");
315 if (get_irg_outs_state(irg) != outs_consistent)
316 compute_irg_outs(irg);
318 env.found_allocs = NULL;
319 env.dead_allocs = NULL;
323 irg_walk_graph(irg, NULL, find_allocations, &env);
325 transform_allocs(irg, &env);
328 /* Do simple and fast escape analysis for all graphs. */
329 void escape_analysis(int run_scalar_replace)
334 walk_env_t *env, *elist;
336 if (get_irp_callee_info_state() != irg_callee_info_consistent) {
337 assert(! "need callee info");
342 * We treat memory for speed: we first collect all info in a
343 * list of environments, than do the transformation.
344 * Doing it this way, no analysis info gets invalid while we run
350 env = obstack_alloc(&obst, sizeof(*env));
351 env->found_allocs = NULL;
352 env->dead_allocs = NULL;
354 for (i = get_irp_n_irgs() - 1; i >= 0; --i) {
355 irg = get_irp_irg(i);
357 if (get_irg_outs_state(irg) != outs_consistent)
358 compute_irg_outs(irg);
360 irg_walk_graph(irg, NULL, find_allocations, env);
362 if (env->found_allocs || env->dead_allocs) {
370 env = obstack_alloc(&obst, sizeof(*env));
371 env->found_allocs = NULL;
372 env->dead_allocs = NULL;
376 for (env = elist; env; env = env->next) {
377 transform_allocs(env->irg, env);
380 obstack_free(&obst, NULL);