2 * Copyright (C) 1995-2011 University of Karlsruhe. All right reserved.
4 * This file is part of libFirm.
6 * This file may be distributed and/or modified under the terms of the
7 * GNU General Public License version 2 as published by the Free Software
8 * Foundation and appearing in the file LICENSE.GPL included in the
9 * packaging of this file.
11 * Licensees holding valid libFirm Professional Edition licenses may use
12 * this file in accordance with the libFirm Commercial License.
13 * Agreement provided with the Software.
15 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
16 * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * @author Michael Beck
24 * @brief A fast and simple Escape analysis.
28 #include "iroptimize.h"
30 #include "irgraph_t.h"
35 #include "analyze_irg_args.h"
46 typedef struct walk_env {
47 ir_node *found_allocs; /**< list of all found non-escaped allocs */
48 ir_node *dead_allocs; /**< list of all found dead alloc */
49 check_alloc_entity_func callback; /**< callback that checks a given entity for allocation */
50 unsigned nr_removed; /**< number of removed allocs (placed of frame) */
51 unsigned nr_changed; /**< number of changed allocs (allocated on stack now) */
52 unsigned nr_deads; /**< number of dead allocs */
54 /* these fields are only used in the global escape analysis */
55 ir_graph *irg; /**< the irg for this environment */
56 struct walk_env *next; /**< for linking environments */
60 DEBUG_ONLY(static firm_dbg_module_t *dbgHandle;)
63 * checks whether a Raise leaves a method
65 static int is_method_leaving_raise(ir_node *raise)
71 for (i = get_irn_n_outs(raise) - 1; i >= 0; --i) {
72 ir_node *succ = get_irn_out(raise, i);
74 /* there should be only one ProjX node */
75 if (get_Proj_proj(succ) == pn_Raise_X) {
82 /* Hmm: no ProjX from a Raise? This should be a verification
83 * error. For now we just assert and return.
85 panic("No ProjX after Raise found");
88 if (get_irn_n_outs(proj) != 1) {
89 /* Hmm: more than one user of ProjX: This is a verification
92 panic("More than one user of ProjX");
95 n = get_irn_out(proj, 0);
96 assert(is_Block(n) && "Argh: user of ProjX is no block");
98 if (n == get_irg_end_block(get_irn_irg(n)))
101 /* ok, we get here so the raise will not leave the function */
106 * returns an Alloc node if the node adr Select
109 static ir_node *is_depend_alloc(ir_node *adr)
116 /* should be a simple Sel */
117 if (get_Sel_n_indexs(adr) != 0)
120 alloc = skip_Proj(get_Sel_ptr(adr));
121 if (!is_Alloc(alloc))
124 /* hmm, we depend on this Alloc */
125 ir_printf("depend alloc %+F\n", alloc);
131 * determine if a value calculated by n "escape", ie
132 * is stored somewhere we could not track
134 static int can_escape(ir_node *n)
138 /* should always be pointer mode or we made some mistake */
139 assert(mode_is_reference(get_irn_mode(n)));
141 for (i = get_irn_n_outs(n) - 1; i >= 0; --i) {
142 ir_node *succ = get_irn_out(n, i);
144 switch (get_irn_opcode(succ)) {
146 if (get_Store_value(succ) == n) {
147 ir_node *adr = get_Store_ptr(succ);
150 * if this Alloc depends on another one,
153 if (is_depend_alloc(adr))
157 * We are storing n. As long as we do not further
158 * evaluate things, the pointer 'escape' here
166 * Should not happen, but if it does we leave the pointer
167 * path and do not track further
171 case iro_Call: { /* most complicated case */
172 ir_node *ptr = get_Call_ptr(succ);
175 if (is_SymConst_addr_ent(ptr)) {
177 ent = get_SymConst_entity(ptr);
179 /* we know the called entity */
180 for (j = get_Call_n_params(succ); j > 0;) {
181 if (get_Call_param(succ, --j) == n) {
182 /* n is the j'th param of the call */
183 if (get_method_param_access(ent, j) & ptr_access_store)
184 /* n is store in ent */
188 } else if (is_Sel(ptr)) {
191 /* go through all possible callees */
192 for (k = get_Call_n_callees(succ); k > 0;) {
194 ent = get_Call_callee(succ, --k);
196 if (is_unknown_entity(ent)) {
197 /* we don't know what will be called, a possible escape */
201 for (j = get_Call_n_params(succ); j > 0;) {
202 if (get_Call_param(succ, --j) == n) {
203 /* n is the j'th param of the call */
204 if (get_method_param_access(ent, j) & ptr_access_store)
205 /* n is store in ent */
210 } else /* we don't know want will called */
217 /* Bad: the allocate object is returned */
221 /* Hmm: if we do NOT leave the method, it's local */
222 if (is_method_leaving_raise(succ))
230 /* Bad: trace the tuple backwards */
231 for (j = get_irn_arity(succ) - 1; j >= 0; --j)
232 if (get_irn_n(succ, j) == n)
238 for (k = get_irn_n_outs(succ); k >= 0; --k) {
239 proj = get_irn_out(succ, k);
241 if (get_Proj_proj(proj) == j) {
242 /* we found the right Proj */
249 * If we haven't found the right Proj, succ is still
250 * the Tuple and the search will end here.
260 if (! mode_is_reference(get_irn_mode(succ)))
263 if (can_escape(succ))
270 * walker: search for Alloc nodes and follow the usages
272 static void find_allocations(ir_node *alloc, void *ctx)
274 walk_env_t *env = (walk_env_t*)ctx;
278 if (! is_Alloc(alloc))
281 /* we searching only for heap allocations */
282 if (get_Alloc_where(alloc) != heap_alloc)
286 for (i = get_irn_n_outs(alloc) - 1; i >= 0; --i) {
287 ir_node *proj = get_irn_out(alloc, i);
289 if (get_Proj_proj(proj) == pn_Alloc_res) {
297 * bad: no-one wants the result, should NOT happen but
298 * if it does we could delete it.
300 set_irn_link(alloc, env->dead_allocs);
301 env->dead_allocs = alloc;
306 if (! can_escape(adr)) {
307 set_irn_link(alloc, env->found_allocs);
308 env->found_allocs = alloc;
313 * walker: search for allocation Call nodes and follow the usages
315 static void find_allocation_calls(ir_node *call, void *ctx)
317 walk_env_t *env = (walk_env_t*)ctx;
324 adr = get_Call_ptr(call);
325 if (! is_SymConst_addr_ent(adr))
327 ent = get_SymConst_entity(adr);
328 if (! env->callback(ent))
332 for (i = get_irn_n_outs(call) - 1; i >= 0; --i) {
333 ir_node *res_proj = get_irn_out(call, i);
335 if (get_Proj_proj(res_proj) == pn_Call_T_result) {
336 for (i = get_irn_n_outs(res_proj) - 1; i >= 0; --i) {
337 ir_node *proj = get_irn_out(res_proj, i);
339 if (get_Proj_proj(proj) == 0) {
340 /* found first result */
351 * bad: no-one wants the result, should NOT happen but
352 * if it does we could delete it.
354 set_irn_link(call, env->dead_allocs);
355 env->dead_allocs = call;
360 if (! can_escape(adr)) {
361 set_irn_link(call, env->found_allocs);
362 env->found_allocs = call;
367 * Do the necessary graph transformations to transform
370 static void transform_allocs(ir_graph *irg, walk_env_t *env)
372 ir_node *alloc, *next, *mem, *sel, *size, *blk;
373 ir_type *ftp, *atp, *tp;
379 /* kill all dead allocs */
380 for (alloc = env->dead_allocs; alloc; alloc = next) {
381 next = (ir_node*)get_irn_link(alloc);
383 DBG((dbgHandle, LEVEL_1, "%+F allocation of %+F unused, deleted.\n", irg, alloc));
385 mem = get_Alloc_mem(alloc);
386 blk = get_nodes_block(alloc);
387 ir_node *const in[] = {
389 [pn_Alloc_X_regular] = new_r_Jmp(blk),
390 [pn_Alloc_X_except] = new_r_Bad(irg, mode_X),
392 turn_into_tuple(alloc, ARRAY_SIZE(in), in);
397 /* convert all non-escaped heap allocs into frame variables */
398 ftp = get_irg_frame_type(irg);
399 for (alloc = env->found_allocs; alloc; alloc = next) {
400 next = (ir_node*)get_irn_link(alloc);
401 size = get_Alloc_count(alloc);
402 atp = get_Alloc_type(alloc);
405 if (is_SymConst(size) && get_SymConst_kind(size) == symconst_type_size) {
406 /* if the size is a type size and the types matched */
407 assert(atp == get_SymConst_type(size));
409 } else if (is_Const(size)) {
410 ir_tarval *tv = get_Const_tarval(size);
412 if (tv != tarval_bad && tarval_is_long(tv) &&
413 get_type_state(atp) == layout_fixed &&
414 (unsigned)get_tarval_long(tv) == get_type_size_bytes(atp)) {
415 /* a already lowered type size */
420 if (tp && !is_unknown_type(tp)) {
421 /* we could determine the type, so we could place it on the frame */
422 dbg = get_irn_dbg_info(alloc);
423 blk = get_nodes_block(alloc);
425 DBG((dbgHandle, LEVEL_DEFAULT, "%+F allocation of %+F type %+F placed on frame\n", irg, alloc, tp));
427 snprintf(name, sizeof(name), "%s_NE_%u", get_entity_name(get_irg_entity(irg)), nr++);
428 name[sizeof(name) - 1] = '\0';
429 ent = new_d_entity(ftp, new_id_from_str(name), get_Alloc_type(alloc), dbg);
431 sel = new_rd_simpleSel(dbg, get_nodes_block(alloc), get_irg_no_mem(irg), get_irg_frame(irg), ent);
432 mem = get_Alloc_mem(alloc);
434 ir_node *const in[] = {
436 [pn_Alloc_res] = sel,
437 [pn_Alloc_X_regular] = new_r_Jmp(blk),
438 [pn_Alloc_X_except] = new_r_Bad(irg, mode_X),
440 turn_into_tuple(alloc, ARRAY_SIZE(in), in);
446 * We could not determine the type or it is variable size.
447 * At least, we could place it on the stack
449 DBG((dbgHandle, LEVEL_DEFAULT, "%+F allocation of %+F type %+F placed on stack\n", irg, alloc));
450 set_Alloc_where(alloc, stack_alloc);
456 /* if allocs were removed somehow */
457 if (env->nr_removed && env->nr_deads) {
458 confirm_irg_properties(irg, IR_GRAPH_PROPERTIES_NONE);
463 * Do the necessary graph transformations to transform
466 static void transform_alloc_calls(ir_graph *irg, walk_env_t *env)
468 ir_node *call, *next, *mem, *blk;
470 /* kill all dead allocs */
471 for (call = env->dead_allocs; call; call = next) {
472 next = (ir_node*)get_irn_link(call);
474 DBG((dbgHandle, LEVEL_1, "%+F allocation of %+F unused, deleted.\n", irg, call));
476 mem = get_Call_mem(call);
477 blk = get_nodes_block(call);
478 ir_node *const in[] = {
480 [pn_Call_T_result] = new_r_Bad(irg, mode_T),
481 [pn_Call_X_regular] = new_r_Jmp(blk),
482 [pn_Call_X_except] = new_r_Bad(irg, mode_X),
484 turn_into_tuple(call, ARRAY_SIZE(in), in);
489 /* convert all non-escaped heap allocs into frame variables */
490 for (call = env->found_allocs; call; call = next) {
491 next = (ir_node*)get_irn_link(call);
494 confirm_irg_properties(irg, IR_GRAPH_PROPERTIES_NONE);
498 /* Do simple and fast escape analysis for one graph. */
499 void escape_enalysis_irg(ir_graph *irg, check_alloc_entity_func callback)
503 if (get_irg_callee_info_state(irg) != irg_callee_info_consistent) {
504 /* no way yet to calculate this for one irg */
505 assert(! "need callee info");
509 if (irg_has_properties(irg, IR_GRAPH_PROPERTY_CONSISTENT_OUTS))
510 compute_irg_outs(irg);
512 env.found_allocs = NULL;
513 env.dead_allocs = NULL;
514 env.callback = callback;
520 /* search for Calls */
521 irg_walk_graph(irg, NULL, find_allocation_calls, &env);
522 transform_alloc_calls(irg, &env);
524 /* search for Alloc nodes */
525 irg_walk_graph(irg, NULL, find_allocations, &env);
526 transform_allocs(irg, &env);
530 /* Do simple and fast escape analysis for all graphs. */
531 void escape_analysis(int run_scalar_replace, check_alloc_entity_func callback)
535 walk_env_t *env, *elist;
536 (void) run_scalar_replace;
538 if (get_irp_callee_info_state() != irg_callee_info_consistent) {
539 assert(! "need callee info");
543 FIRM_DBG_REGISTER(dbgHandle, "firm.opt.escape_ana");
546 * We treat memory for speed: we first collect all info in a
547 * list of environments, than do the transformation.
548 * Doing it this way, no analysis info gets invalid while we run
554 env = OALLOC(&obst, walk_env_t);
555 env->found_allocs = NULL;
556 env->dead_allocs = NULL;
557 env->callback = callback;
559 for (i = 0, n = get_irp_n_irgs(); i < n; ++i) {
560 ir_graph *irg = get_irp_irg(i);
562 assure_irg_properties(irg, IR_GRAPH_PROPERTY_CONSISTENT_OUTS);
565 /* search for Calls */
566 irg_walk_graph(irg, NULL, find_allocation_calls, env);
568 /* search for Alloc nodes */
569 irg_walk_graph(irg, NULL, find_allocations, env);
572 if (env->found_allocs || env->dead_allocs) {
580 env = OALLOC(&obst, walk_env_t);
581 env->found_allocs = NULL;
582 env->dead_allocs = NULL;
583 env->callback = callback;
588 for (env = elist; env; env = env->next) {
589 transform_alloc_calls(env->irg, env);
592 for (env = elist; env; env = env->next) {
593 transform_allocs(env->irg, env);
597 obstack_free(&obst, NULL);