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 assert(is_Block(n) && "Argh: user of ProjX is no block");
83 if (n == get_irg_end_block(get_irn_irg(n)))
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);
103 switch (get_irn_opcode(succ)) {
105 if (get_Store_value(succ) == n) {
107 * We are storing n. As long as we do not further
108 * evaluate things, the pointer 'escape' here
116 * Should not happen, but if it does we leave the pointer
117 * path and do not track further
121 case iro_Call: { /* most complicated case */
122 ir_node *ptr = get_Call_ptr(succ);
125 if (get_irn_op(ptr) == op_SymConst &&
126 get_SymConst_kind(ptr) == symconst_addr_ent) {
127 ent = get_SymConst_entity(ptr);
129 /* we know the called entity */
130 for (j = get_Call_n_params(succ) - 1; j >= 0; --j) {
131 if (get_Call_param(succ, j) == n) {
132 /* n is the j'th param of the call */
133 if (get_method_param_access(ent, j) & ptr_access_store)
134 /* n is store in ent */
140 /* go through all possible callees */
141 for (k = get_Call_n_callees(succ) - 1; k >= 0; --k) {
142 ent = get_Call_callee(succ, k);
144 for (j = get_Call_n_params(succ) - 1; j >= 0; --j) {
145 if (get_Call_param(succ, j) == n) {
146 /* n is the j'th param of the call */
147 if (get_method_param_access(ent, j) & ptr_access_store)
148 /* n is store in ent */
158 /* Bad: the allocate object is returned */
162 /* Hmm: if we do NOT leave the method, it's local */
163 return is_method_leaving_raise(succ);
168 /* Bad: trace the tuple backwards */
169 for (j = get_irn_arity(succ) - 1; j >= 0; --j)
170 if (get_irn_n(succ, j) == n)
176 for (k = get_irn_n_outs(succ); k >= 0; --k) {
177 proj = get_irn_out(succ, k);
179 if (get_Proj_proj(proj) == j) {
180 /* we found the right Proj */
187 * If we haven't found the right Proj, succ is still
188 * the Tuple and the search will end here.
198 if (! mode_is_reference(get_irn_mode(succ)))
208 * walker: search for Alloc nodes and follow the usages
210 static void find_allocations(ir_node *alloc, void *ctx)
214 walk_env_t *env = ctx;
216 if (get_irn_op(alloc) != op_Alloc)
219 /* we searching only for heap allocations */
220 if (get_Alloc_where(alloc) != heap_alloc)
224 for (i = get_irn_n_outs(alloc) - 1; i >= 0; --i) {
225 ir_node *proj = get_irn_out(alloc, i);
227 if (get_Proj_proj(proj) == pn_Alloc_res) {
235 * bad: no-one wants the result, should NOT happen but
236 * if it does we could delete it.
238 set_irn_link(alloc, env->dead_allocs);
239 env->dead_allocs = alloc;
244 if (! do_escape(adr)) {
245 set_irn_link(alloc, env->found_allocs);
246 env->found_allocs = alloc;
251 * do the necessary graph transformations
253 static void transform_allocs(ir_graph *irg, walk_env_t *env)
255 ir_node *alloc, *next, *mem, *sel;
262 /* kill all dead allocs */
263 for (alloc = env->dead_allocs; alloc; alloc = next) {
264 next = get_irn_link(alloc);
266 mem = get_Alloc_mem(alloc);
267 turn_into_tuple(alloc, pn_Alloc_max);
268 set_Tuple_pred(alloc, pn_Alloc_M, mem);
269 set_Tuple_pred(alloc, pn_Alloc_X_except, new_r_Bad(irg));
274 /* convert all non-escaped heap allocs into frame variables */
275 ftp = get_irg_frame_type(irg);
276 for (alloc = env->found_allocs; alloc; alloc = next) {
277 next = get_irn_link(alloc);
278 dbg = get_irn_dbg_info(alloc);
280 snprintf(name, sizeof(name), "_not_escaped_%u", nr++);
281 ent = new_d_entity(ftp, new_id_from_str(name), get_Alloc_type(alloc), dbg);
283 sel = new_rd_simpleSel(dbg, irg, get_nodes_block(alloc),
284 get_irg_no_mem(irg), get_irg_frame(irg), ent);
285 mem = get_Alloc_mem(alloc);
287 turn_into_tuple(alloc, pn_Alloc_max);
288 set_Tuple_pred(alloc, pn_Alloc_M, mem);
289 set_Tuple_pred(alloc, pn_Alloc_X_except, new_r_Bad(irg));
290 set_Tuple_pred(alloc, pn_Alloc_res, sel);
295 if (env->nr_changed | env->nr_deads) {
296 set_irg_outs_inconsistent(irg);
299 set_irg_dom_inconsistent(irg);
303 /* Do simple and fast escape analysis for one graph. */
304 void escape_enalysis_irg(ir_graph *irg)
308 if (get_irg_callee_info_state(irg) != irg_callee_info_consistent) {
309 /* no way yet to calculate this for one irg */
310 assert(! "need callee info");
314 if (get_irg_outs_state(irg) != outs_consistent)
315 compute_irg_outs(irg);
317 env.found_allocs = NULL;
318 env.dead_allocs = NULL;
322 irg_walk_graph(irg, NULL, find_allocations, &env);
324 transform_allocs(irg, &env);
327 /* Do simple and fast escape analysis for all graphs. */
328 void escape_analysis(int run_scalar_replace)
333 walk_env_t *env, *elist;
335 if (get_irp_callee_info_state() != irg_callee_info_consistent) {
336 assert(! "need callee info");
341 * We treat memory for speed: we first collect all info in a
342 * list of environments, than do the transformation.
343 * Doing it this way, no analysis info gets invalid while we run
349 env = obstack_alloc(&obst, sizeof(*env));
350 env->found_allocs = NULL;
351 env->dead_allocs = NULL;
353 for (i = get_irp_n_irgs() - 1; i >= 0; --i) {
354 irg = get_irp_irg(i);
356 if (get_irg_outs_state(irg) != outs_consistent)
357 compute_irg_outs(irg);
359 irg_walk_graph(irg, NULL, find_allocations, env);
361 if (env->found_allocs || env->dead_allocs) {
369 env = obstack_alloc(&obst, sizeof(*env));
370 env->found_allocs = NULL;
371 env->dead_allocs = NULL;
375 for (env = elist; env; env = env->next) {
376 transform_allocs(env->irg, env);
379 obstack_free(&obst, NULL);