2d0bf162f4ff07384ba9760403d63e437031e38b
[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   assert(is_Block(n) && "Argh: user of ProjX is no block");
82
83   if (n == get_irg_end_block(get_irn_irg(n)))
84     return 1;
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
103     switch (get_irn_opcode(succ)) {
104     case iro_Store:
105       if (get_Store_value(succ) == n) {
106         /*
107          * We are storing n. As long as we do not further
108          * evaluate things, the pointer 'escape' here
109          */
110         return 1;
111       }
112       break;
113
114     case iro_Conv:
115       /*
116        * Should not happen, but if it does we leave the pointer
117        * path and do not track further
118        */
119       return 1;
120
121     case iro_Call: { /* most complicated case */
122       ir_node *ptr = get_Call_ptr(succ);
123       entity *ent;
124
125       if (get_irn_op(ptr) == op_SymConst &&
126           get_SymConst_kind(ptr) == symconst_addr_ent) {
127         ent = get_SymConst_entity(ptr);
128
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 */
135               return 1;
136           }
137         }
138       }
139       else {
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);
143
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 */
149                 return 1;
150             }
151           }
152         }
153       }
154       break;
155     }
156
157     case iro_Return:
158       /* Bad: the allocate object is returned */
159       return 1;
160
161     case iro_Raise:
162       /* Hmm: if we do NOT leave the method, it's local */
163       return is_method_leaving_raise(succ);
164
165     case iro_Tuple: {
166       ir_node *proj;
167
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)
171           break;
172
173       assert(j >= 0);
174
175
176       for (k = get_irn_n_outs(succ); k >= 0; --k) {
177         proj = get_irn_out(succ, k);
178
179         if (get_Proj_proj(proj) == j) {
180           /* we found the right Proj */
181           succ = proj;
182           break;
183         }
184       }
185
186       /*
187        * If we haven't found the right Proj, succ is still
188        * the Tuple and the search will end here.
189        */
190       break;
191     }
192
193     default:
194       break;
195
196     }
197
198     if (! mode_is_reference(get_irn_mode(succ)))
199       continue;
200
201     if (do_escape(succ))
202       return 1;
203   }
204   return 0;
205 }
206
207 /**
208  * walker: search for Alloc nodes and follow the usages
209  */
210 static void find_allocations(ir_node *alloc, void *ctx)
211 {
212   int i;
213   ir_node *adr;
214   walk_env_t *env = ctx;
215
216   if (get_irn_op(alloc) != op_Alloc)
217     return;
218
219   /* we searching only for heap allocations */
220   if (get_Alloc_where(alloc) != heap_alloc)
221     return;
222
223   adr = NULL;
224   for (i = get_irn_n_outs(alloc) - 1; i >= 0; --i) {
225     ir_node *proj = get_irn_out(alloc, i);
226
227     if (get_Proj_proj(proj) == pn_Alloc_res) {
228       adr = proj;
229       break;
230     }
231   }
232
233   if (! adr) {
234     /*
235      * bad: no-one wants the result, should NOT happen but
236      * if it does we could delete it.
237      */
238     set_irn_link(alloc, env->dead_allocs);
239     env->dead_allocs = alloc;
240
241     return;
242   }
243
244   if (! do_escape(adr)) {
245     set_irn_link(alloc, env->found_allocs);
246     env->found_allocs = alloc;
247   }
248 }
249
250 /**
251  * do the necessary graph transformations
252  */
253 static void transform_allocs(ir_graph *irg, walk_env_t *env)
254 {
255   ir_node *alloc, *next, *mem, *sel;
256   type *ftp;
257   entity *ent;
258   char name[32];
259   unsigned nr = 0;
260   dbg_info *dbg;
261
262   /* kill all dead allocs */
263   for (alloc = env->dead_allocs; alloc; alloc = next) {
264     next = get_irn_link(alloc);
265
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));
270
271     ++env->nr_deads;
272   }
273
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);
279
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);
282
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);
286
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);
291
292     ++env->nr_changed;
293   }
294
295   if (env->nr_changed | env->nr_deads) {
296     set_irg_outs_inconsistent(irg);
297
298     if (env->nr_deads)
299       set_irg_dom_inconsistent(irg);
300   }
301 }
302
303 /* Do simple and fast escape analysis for one graph. */
304 void escape_enalysis_irg(ir_graph *irg)
305 {
306   walk_env_t env;
307
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");
311     return;
312   }
313
314   if (get_irg_outs_state(irg) != outs_consistent)
315     compute_irg_outs(irg);
316
317   env.found_allocs = NULL;
318   env.dead_allocs  = NULL;
319   env.nr_changed   = 0;
320   env.nr_deads     = 0;
321
322   irg_walk_graph(irg, NULL, find_allocations, &env);
323
324   transform_allocs(irg, &env);
325 }
326
327 /* Do simple and fast escape analysis for all graphs. */
328 void escape_analysis(int run_scalar_replace)
329 {
330   ir_graph *irg;
331   int i;
332   struct obstack obst;
333   walk_env_t *env, *elist;
334
335   if (get_irp_callee_info_state() != irg_callee_info_consistent) {
336     assert(! "need callee info");
337     return;
338   }
339
340   /*
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
344    * over graphs
345    */
346   obstack_init(&obst);
347   elist = NULL;
348
349   env = obstack_alloc(&obst, sizeof(*env));
350   env->found_allocs = NULL;
351   env->dead_allocs  = NULL;
352
353   for (i = get_irp_n_irgs() - 1; i >= 0; --i) {
354     irg = get_irp_irg(i);
355
356     if (get_irg_outs_state(irg) != outs_consistent)
357       compute_irg_outs(irg);
358
359     irg_walk_graph(irg, NULL, find_allocations, env);
360
361     if (env->found_allocs || env->dead_allocs) {
362       env->nr_changed   = 0;
363       env->nr_deads     = 0;
364       env->irg          = irg;
365       env->next         = elist;
366
367       elist = env;
368
369       env = obstack_alloc(&obst, sizeof(*env));
370       env->found_allocs = NULL;
371       env->dead_allocs  = NULL;
372     }
373   }
374
375   for (env = elist; env; env = env->next) {
376     transform_allocs(env->irg, env);
377   }
378
379   obstack_free(&obst, NULL);
380 }