0c7ed7b476b9c542fe6b3fa17cf5daca8be79b1a
[libfirm] / ir / opt / escape_ana.c
1 /*
2  * Project:     libFIRM
3  * File name:   ir/opt/escape_ana.c
4  * Purpose:     escape analysis and optimization
5  * Author:      Michael Beck
6  * Modified by:
7  * Created:     03.11.2005
8  * CVS-ID:      $Id$
9  * Copyright:   (c) 1999-2005 Universität Karlsruhe
10  * Licence:     This file protected by GPL -  GNU GENERAL PUBLIC LICENSE.
11  */
12
13 /** @file escape_ana.c
14  *
15  * escape analysis.
16  */
17
18 #ifdef HAVE_CONFIG_H
19 # include "config.h"
20 #endif
21
22 #include "irgraph_t.h"
23 #include "irnode_t.h"
24 #include "irouts.h"
25 #include "analyze_irg_args.h"
26 #include "irgmod.h"
27 #include "ircons.h"
28 #include "escape_ana.h"
29
30 /**
31  * walker environment
32  */
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 */
38
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 */
42
43 } walk_env_t;
44
45 /**
46  * checks whether a Raise leaves a method
47  */
48 static int is_method_leaving_raise(ir_node *raise)
49 {
50   int i;
51   ir_node *proj = NULL;
52   ir_node *n;
53
54   for (i = get_irn_n_outs(raise) - 1; i >= 0; --i) {
55     ir_node *succ = get_irn_out(raise, i);
56
57     /* there should be only one ProjX node */
58     if (get_Proj_proj(succ) == pn_Raise_X) {
59       proj = succ;
60       break;
61     }
62   }
63
64   if (! proj) {
65     /* Hmm: no ProjX from a Raise? This should be a verification
66      * error. For now we just assert and return.
67      */
68     assert(! "No ProjX after Raise found");
69     return 1;
70   }
71
72   if (get_irn_n_outs(proj) != 1) {
73     /* Hmm: more than one user of ProjX: This is a verification
74      * error.
75      */
76     assert(! "More than one user of ProjX");
77     return 1;
78   }
79
80   n = get_irn_out(proj, 0);
81   if (get_irn_op(n) == op_End)
82     return 1;
83
84   assert(is_Block(n) && "Argh: user of ProjX is neither block for End");
85
86   /* ok, we get here so the raise will not leave the function */
87   return 0;
88 }
89
90 /**
91  * determine if a value calculated by n "escape", ie
92  * is stored somewhere we could not track
93  */
94 static int do_escape(ir_node *n) {
95   int i, j, k;
96
97   /* should always be pointer mode or we made some mistake */
98   assert(mode_is_reference(get_irn_mode(n)));
99
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);
103
104     switch (get_irn_opcode(succ)) {
105     case iro_Store:
106       if (get_Store_value(succ) == n) {
107         /*
108          * We are storing n. As long as we do not further
109          * evaluate things, the pointer 'escape' here
110          */
111         return 1;
112       }
113       break;
114
115     case iro_Conv:
116       /*
117        * Should not happen, but if it does we leave the pointer
118        * path and do not track further
119        */
120       return 1;
121
122     case iro_Call: { /* most complicated case */
123       ir_node *ptr = get_Call_ptr(succ);
124       entity *ent;
125
126       if (get_irn_op(ptr) == op_SymConst &&
127           get_SymConst_kind(ptr) == symconst_addr_ent) {
128         ent = get_SymConst_entity(ptr);
129
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 */
136               return 1;
137           }
138         }
139       }
140       else {
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);
144
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 */
150                 return 1;
151             }
152           }
153         }
154       }
155       break;
156     }
157
158     case iro_Return:
159       /* Bad: the allocate object is returned */
160       return 1;
161
162     case iro_Raise:
163       /* Hmm: if we do NOT leave the method, it's local */
164       return is_method_leaving_raise(succ);
165
166     case iro_Tuple: {
167       ir_node *proj;
168
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)
172           break;
173
174       assert(j >= 0);
175
176
177       for (k = get_irn_n_outs(succ); k >= 0; --k) {
178         proj = get_irn_out(succ, k);
179
180         if (get_Proj_proj(proj) == j) {
181           /* we found the right Proj */
182           succ = proj;
183           break;
184         }
185       }
186
187       /*
188        * If we haven't found the right Proj, succ is still
189        * the Tuple and the search will end here.
190        */
191       break;
192     }
193
194     default:
195       break;
196
197     }
198
199     if (! mode_is_reference(get_irn_mode(succ)))
200       continue;
201
202     if (do_escape(succ))
203       return 1;
204   }
205   return 0;
206 }
207
208 /**
209  * walker: search for Alloc nodes and follow the usages
210  */
211 static void find_allocations(ir_node *alloc, void *ctx)
212 {
213   int i;
214   ir_node *adr;
215   walk_env_t *env = ctx;
216
217   if (get_irn_op(alloc) != op_Alloc)
218     return;
219
220   /* we searching only for heap allocations */
221   if (get_Alloc_where(alloc) != heap_alloc)
222     return;
223
224   adr = NULL;
225   for (i = get_irn_n_outs(alloc) - 1; i >= 0; --i) {
226     ir_node *proj = get_irn_out(alloc, i);
227
228     if (get_Proj_proj(proj) == pn_Alloc_res) {
229       adr = proj;
230       break;
231     }
232   }
233
234   if (! adr) {
235     /*
236      * bad: no-one wants the result, should NOT happen but
237      * if it does we could delete it.
238      */
239     set_irn_link(alloc, env->dead_allocs);
240     env->dead_allocs = alloc;
241
242     return;
243   }
244
245   if (! do_escape(adr)) {
246     set_irn_link(alloc, env->found_allocs);
247     env->found_allocs = alloc;
248   }
249 }
250
251 /**
252  * do the necessary graph transformations
253  */
254 static void transform_allocs(ir_graph *irg, walk_env_t *env)
255 {
256   ir_node *alloc, *next, *mem, *sel;
257   type *ftp;
258   entity *ent;
259   char name[32];
260   unsigned nr = 0;
261   dbg_info *dbg;
262
263   /* kill all dead allocs */
264   for (alloc = env->dead_allocs; alloc; alloc = next) {
265     next = get_irn_link(alloc);
266
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));
271
272     ++env->nr_deads;
273   }
274
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);
280
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);
283
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);
287
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);
292
293     ++env->nr_changed;
294   }
295
296   if (env->nr_changed | env->nr_deads) {
297     set_irg_outs_inconsistent(irg);
298
299     if (env->nr_deads)
300       set_irg_dom_inconsistent(irg);
301   }
302 }
303
304 /* Do simple and fast escape analysis for one graph. */
305 void escape_enalysis_irg(ir_graph *irg)
306 {
307   walk_env_t env;
308
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");
312     return;
313   }
314
315   if (get_irg_outs_state(irg) != outs_consistent)
316     compute_irg_outs(irg);
317
318   env.found_allocs = NULL;
319   env.dead_allocs  = NULL;
320   env.nr_changed   = 0;
321   env.nr_deads     = 0;
322
323   irg_walk_graph(irg, NULL, find_allocations, &env);
324
325   transform_allocs(irg, &env);
326 }
327
328 /* Do simple and fast escape analysis for all graphs. */
329 void escape_analysis(int run_scalar_replace)
330 {
331   ir_graph *irg;
332   int i;
333   struct obstack obst;
334   walk_env_t *env, *elist;
335
336   if (get_irp_callee_info_state() != irg_callee_info_consistent) {
337     assert(! "need callee info");
338     return;
339   }
340
341   /*
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
345    * over graphs
346    */
347   obstack_init(&obst);
348   elist = NULL;
349
350   env = obstack_alloc(&obst, sizeof(*env));
351   env->found_allocs = NULL;
352   env->dead_allocs  = NULL;
353
354   for (i = get_irp_n_irgs() - 1; i >= 0; --i) {
355     irg = get_irp_irg(i);
356
357     if (get_irg_outs_state(irg) != outs_consistent)
358       compute_irg_outs(irg);
359
360     irg_walk_graph(irg, NULL, find_allocations, env);
361
362     if (env->found_allocs || env->dead_allocs) {
363       env->nr_changed   = 0;
364       env->nr_deads     = 0;
365       env->irg          = irg;
366       env->next         = elist;
367
368       elist = env;
369
370       env = obstack_alloc(&obst, sizeof(*env));
371       env->found_allocs = NULL;
372       env->dead_allocs  = NULL;
373     }
374   }
375
376   for (env = elist; env; env = env->next) {
377     transform_allocs(env->irg, env);
378   }
379
380   obstack_free(&obst, NULL);
381 }