set current_ir_graph
[libfirm] / ir / opt / funccall.c
1 /*
2  * Project:     libFIRM
3  * File name:   ir/opt/funccall.c
4  * Purpose:     optimization of function calls
5  * Author:      Michael Beck
6  * Created:
7  * CVS-ID:      $Id$
8  * Copyright:   (c) 1998-2006 Universität Karlsruhe
9  * Licence:     This file protected by GPL -  GNU GENERAL PUBLIC LICENSE.
10  */
11 #include "irnode_t.h"
12 #include "irgraph_t.h"
13 #include "irgmod.h"
14 #include "irgwalk.h"
15 #include "irvrfy.h"
16 #include "dbginfo_t.h"
17 #include "irflag_t.h"
18 #include "ircons.h"
19 #include "funccall.h"
20 #include "irhooks.h"
21
22 /**
23  * The walker environment for rem_mem_from_const_fkt_calls
24  */
25 typedef struct _env_t {
26   int  n_calls_removed_SymConst;
27   int  n_calls_removed_Sel;
28   ir_node *const_call_list;       /**< The list of all const function calls that will be changed. */
29   ir_node *pure_call_list;        /**< The list of all pure function calls that will be changed. */
30   ir_node *proj_list;             /**< The list of all potential Proj nodes that must be fixed. */
31 } env_t;
32
33 /**
34  * Collect all calls to const and pure functions
35  * to lists. Collect all Proj(Call) nodes into a Proj list.
36  */
37 static void collect_calls(ir_node *node, void *env)
38 {
39   env_t *ctx = env;
40   ir_node *call, *ptr;
41   entity *ent;
42   unsigned mode;
43
44   if (is_Call(node)) {
45     call = node;
46
47     /* set the link to NULL for all non-const/pure calls */
48     set_irn_link(call, NULL);
49     ptr = get_Call_ptr(call);
50     if (is_SymConst(ptr) && get_SymConst_kind(ptr) == symconst_addr_ent) {
51       ent = get_SymConst_entity(ptr);
52
53       mode = get_entity_additional_properties(ent);
54       if ((mode & (mtp_property_const|mtp_property_pure)) == 0)
55         return;
56       ++ctx->n_calls_removed_SymConst;
57     } else if (get_opt_closed_world() &&
58               is_Sel(ptr) &&
59               get_irg_callee_info_state(current_ir_graph) == irg_callee_info_consistent) {
60       /* If all possible callees are const functions, we can remove the memory edge. */
61       int i, n_callees = get_Call_n_callees(call);
62       if (n_callees == 0)
63         /* This is kind of strange:  dying code or a Call that will raise an exception
64            when executed as there is no implementation to call.  So better not
65            optimize. */
66         return;
67
68       /* note that const function are a subset of pure ones */
69       mode = mtp_property_const | mtp_property_pure;
70       for (i = 0; i < n_callees; ++i) {
71         ent = get_Call_callee(call, i);
72         if (ent == unknown_entity) {
73           /* we don't know which entity is called here */
74           return;
75         }
76         mode &= get_entity_additional_properties(ent);
77         if (mode == 0)
78           return;
79       }
80       ++ctx->n_calls_removed_Sel;
81     } else
82       return;
83
84     /* ok, if we get here we found a call to a const or a pure function */
85     if (mode & mtp_property_pure) {
86       set_irn_link(call, ctx->pure_call_list);
87       ctx->pure_call_list = call;
88     } else {
89       set_irn_link(call, ctx->const_call_list);
90       ctx->const_call_list = call;
91     }
92   } else if (is_Proj(node)) {
93     /*
94      * Collect all memory and exception Proj's from
95      * calls.
96      */
97     call = get_Proj_pred(node);
98     if (! is_Call(call))
99       return;
100
101     /* collect the Proj's in the Proj list */
102     switch (get_Proj_proj(node)) {
103     case pn_Call_M_regular:
104     case pn_Call_X_except:
105     case pn_Call_M_except:
106       set_irn_link(node, ctx->proj_list);
107       ctx->proj_list = node;
108       break;
109     default:
110       break;
111     }
112   }
113 }  /* collect_calls */
114
115 /**
116  * Fix the list of collected Calls.
117  *
118  * @param irg        the graph that contained calls to pure functions
119  * @param call_list  the list of all call sites of const functions
120  * @param proj_list  the list of all memory/exception Proj's of this call sites
121  */
122 static void fix_const_call_list(ir_graph *irg, ir_node *call_list, ir_node *proj_list) {
123   ir_node *call, *next, *mem, *proj;
124   int exc_changed = 0;
125   ir_graph *rem = current_ir_graph;
126
127   current_ir_graph = irg;
128
129   /* First step: fix all calls by removing it's memory input.
130      It's original memory input is preserved in their link fields. */
131   for (call = call_list; call; call = next) {
132     next = get_irn_link(call);
133     mem  = get_Call_mem(call);
134
135     set_irn_link(call, mem);
136     set_Call_mem(call, get_irg_no_mem(irg));
137
138     /*
139      * Sorrily we cannot simply set the node to 'float'.
140      * There is a reason for that:
141      *
142      * - The call might be inside a loop/if that is NOT entered
143      *   and calls a endless function. Setting the call to float
144      *   would allow to move it out from the loop/if causing this
145      *   function be called even if the loop/if is not entered ...
146      *
147      * This could be fixed using post-dominators for calls and Pin nodes
148      * but need some more analyzes to ensure that a call that potential
149      * never returns is not executed before some code that generates
150      * observable states...
151      */
152
153     /* finally, this call can float
154     set_irn_pinned(call, op_pin_state_floats); */
155     hook_func_call(irg, call);
156   }
157
158   /* Second step: fix all Proj's */
159   for (proj = proj_list; proj; proj = next) {
160     next = get_irn_link(proj);
161     call = get_Proj_pred(proj);
162     mem  = get_irn_link(call);
163
164     /* beware of calls in the pure call list */
165     if (! mem || get_irn_op(mem) == op_Call)
166       continue;
167     assert(get_irn_mode(mem) == mode_M);
168
169     switch (get_Proj_proj(proj)) {
170     case pn_Call_M_regular: {
171       /* in dead code there might be cycles where proj == mem */
172       if (proj != mem)
173         exchange(proj, mem);
174     } break;
175     case pn_Call_X_except:
176     case pn_Call_M_except:
177       exc_changed = 1;
178       exchange(proj, get_irg_bad(irg));
179       break;
180     default:
181       ;
182     }
183   }
184
185   /* changes were done ... */
186   set_irg_outs_inconsistent(irg);
187   set_irg_loopinfo_state(irg, loopinfo_cf_inconsistent);
188
189   if (exc_changed) {
190     /* ... including exception edges */
191     set_irg_doms_inconsistent(irg);
192   }
193   current_ir_graph = rem;
194 }  /* fix_call_list */
195
196 #if 0
197 /**
198  * Check if a graph represents a const function.
199  *
200  * @param irg  the graph
201  */
202 static int is_const_function(ir_graph *irg)
203 {
204   ir_node *end, *endbl;
205   int j, change;
206
207   if (get_irg_additional_properties(irg) & mtp_property_const) {
208     /* already marked as a const function */
209     return 0;
210   }
211
212   end   = get_irg_end(irg);
213   endbl = get_nodes_block(end);
214   change = 0;
215
216   /* visit every Return */
217   for (j = get_Block_n_cfgpreds(endbl) - 1; j >= 0; --j) {
218     ir_node *node = get_Block_cfgpred(endbl, j);
219     ir_op   *op   = get_irn_op(node);
220     ir_node *mem;
221
222     /* Bad nodes usually do NOT produce anything, so it's ok */
223     if (op == op_Bad)
224       continue;
225
226     if (op == op_Return) {
227       mem = get_Return_mem(node);
228
229       /* Bad nodes usually do NOT produce anything, so it's ok */
230       if (is_Bad(mem))
231         continue;
232
233       change = mem != get_irg_initial_mem(irg);
234       if (change)
235         break;
236     }
237     else {
238       /* exception found */
239       change = 1;
240       break;
241     }
242   }
243
244   if (! change) {
245     /* check, if a keep-alive exists */
246     for (j = get_End_n_keepalives(end) - 1; j >= 0; --j) {
247       ir_node *mem = get_End_keepalive(end, j);
248
249       if (mode_M != get_irn_mode(mem))
250         continue;
251
252       change = mem != get_irg_initial_mem(irg);
253       if (change)
254         break;
255     }
256   }
257
258   if (! change) {
259     /* no memory changes found, it's a const function */
260     set_irg_additional_property(irg, mtp_property_const);
261     return 1;
262   }
263   return 0;
264 }  /* is_const_function */
265 #endif
266
267 /* a marker */
268 static char _mark;
269 #define MARK &_mark
270
271 #define UNMARK_IRG(irg)     set_irg_link((irg), NULL)
272 #define MARK_IRG(irg)       set_irg_link((irg), MARK)
273 #define IS_IRG_MARKED(irg)  (get_irg_link(irg) == MARK)
274
275 /* forward */
276 static int is_pure_function(ir_graph *irg);
277
278 #define UMAX(a,b) (a) > (b) ? (a) : (b)
279
280 /**
281  * Follow the memory chain starting at node and determine
282  * the mtp_property.
283  *
284  * @return mtp_property_const if only calls of const functions are detected
285  *         mtp_property_pure if only Loads and const/pure
286  *         calls detected
287  *         bad_property else
288  */
289 static unsigned _follow_mem(ir_node *node) {
290   unsigned m, mode = mtp_property_const;
291   ir_node  *ptr;
292   int i;
293
294   for (;;) {
295     if (irn_visited(node))
296       return mode;
297
298     mark_irn_visited(node);
299
300     switch (get_irn_opcode(node)) {
301     case iro_Proj:
302       node = get_Proj_pred(node);
303       break;
304
305     case iro_NoMem:
306       /* finish here */
307       return mode;
308
309     case iro_Phi:
310     case iro_Sync:
311       for (i = get_irn_arity(node) - 1; i >= 0; --i) {
312         mode &= _follow_mem(get_irn_n(node, i));
313       }
314       break;
315
316     case iro_Load:
317       /* Beware volatile Loads are NOT allowed in pure functions */
318       if (get_Load_volatility(node) == volatility_is_volatile)
319         return 0;
320       mode = mtp_property_pure;
321       node = get_Load_mem(node);
322       break;
323
324     case iro_Call:
325       /* a call is only tolerable if its either constant or pure */
326       ptr = get_Call_ptr(node);
327       if (get_irn_op(ptr) == op_SymConst &&
328           get_SymConst_kind(ptr) == symconst_addr_ent) {
329         entity   *ent = get_SymConst_entity(ptr);
330         ir_graph *irg = get_entity_irg(ent);
331
332         if (irg == current_ir_graph) {
333           /* A recursive call. The did not mode depend on this call */
334         }
335         else if (irg == NULL) {
336           m = get_entity_additional_properties(ent) & (mtp_property_const|mtp_property_pure);
337           if (! m)
338             return 0;
339           mode = UMAX(mode, m);
340         }
341         else if (irg != NULL) {
342           /* we have a graph. Check if it is already analyzed */
343           if (IS_IRG_MARKED(irg))
344             (void)is_pure_function(irg);
345
346           m = get_irg_additional_properties(irg) & (mtp_property_const|mtp_property_pure);
347           if (! m)
348             return 0;
349           mode = UMAX(mode, m);
350         }
351       }
352       else
353         return 0;
354       node = get_Call_mem(node);
355       break;
356
357     default:
358       return 0;
359     }
360   }
361 }  /* follow_mem */
362
363 /**
364  * Follow the memory chain starting at node and determine
365  * the mtp_property.
366  *
367  * @return mtp_property_const if only calls of const functions are detected
368  *         mtp_property_pure if only Loads and const/pure
369  *         calls detected
370  *         0 else
371  */
372 static unsigned follow_mem(ir_graph *irg, ir_node *node, unsigned mode) {
373   unsigned m;
374
375   inc_irg_visited(irg);
376   /* mark the initial mem: recursion stops here */
377   mark_irn_visited(get_irg_initial_mem(irg));
378   m = _follow_mem(node);
379   if (! m)
380     return 0;
381   return UMAX(mode, m);
382 }  /* follow_mwm */
383
384 /*
385  * Check if a graph represents a pure function.
386  *
387  * @param irg  the graph
388  */
389 static int is_pure_function(ir_graph *irg) {
390   ir_node *end, *endbl;
391   int j;
392   unsigned mode = get_irg_additional_properties(irg);
393   ir_graph *rem = current_ir_graph;
394
395   if (mode & mtp_property_const) {
396     /* already marked as a const function */
397     return mtp_property_const;
398   }
399   if (mode & mtp_property_pure) {
400     /* already marked as a pure function */
401     return mtp_property_const;
402   }
403
404   if (! IS_IRG_MARKED(irg))
405     return 0;
406   UNMARK_IRG(irg);
407
408   end   = get_irg_end(irg);
409   endbl = get_nodes_block(end);
410   mode  = mtp_property_const;
411
412   current_ir_graph = irg;
413
414   /* visit every Return */
415   for (j = get_Block_n_cfgpreds(endbl) - 1; j >= 0; --j) {
416     ir_node *node = get_Block_cfgpred(endbl, j);
417     ir_op   *op   = get_irn_op(node);
418     ir_node *mem;
419
420     /* Bad nodes usually do NOT produce anything, so it's ok */
421     if (op == op_Bad)
422       continue;
423
424     if (op == op_Return) {
425       mem = get_Return_mem(node);
426
427       /* Bad nodes usually do NOT produce anything, so it's ok */
428       if (is_Bad(mem))
429         continue;
430
431       if (mem != get_irg_initial_mem(irg))
432         mode = follow_mem(irg, mem, mode);
433     }
434     else {
435       /* exception found. */
436       mode = follow_mem(irg, node, mode);
437       break;
438     }
439     if (mode == 0)
440       break;
441   }
442
443   if (mode != 0) {
444     /* check, if a keep-alive exists */
445     for (j = get_End_n_keepalives(end) - 1; j >= 0; --j) {
446       ir_node *mem = get_End_keepalive(end, j);
447
448       if (mode_M != get_irn_mode(mem))
449         continue;
450
451       mode = follow_mem(irg, mem, mode);
452       if (mode == 0)
453         break;
454     }
455   }
456
457   if (mode)
458     set_irg_additional_property(irg, mode);
459   current_ir_graph = rem;
460   return mode;
461 }  /* is_pure_function */
462
463 /**
464  * Handle calls to const functions.
465  */
466 static void handle_const_Calls(env_t *ctx)
467 {
468   int i;
469
470   ctx->n_calls_removed_SymConst = 0;
471   ctx->n_calls_removed_Sel      = 0;
472
473   /* all calls of const functions can be transformed */
474   for (i = get_irp_n_irgs() - 1; i >= 0; --i) {
475     ir_graph *irg  = get_irp_irg(i);
476
477     ctx->const_call_list = NULL;
478     ctx->pure_call_list  = NULL;
479     ctx->proj_list = NULL;
480     irg_walk_graph(irg, NULL, collect_calls, ctx);
481
482     if (ctx->const_call_list)
483       fix_const_call_list(irg, ctx->const_call_list, ctx->proj_list);
484   }
485 }  /* handle_const_Calls */
486
487 /*
488  * optimize function calls by handling const functions
489  */
490 void optimize_funccalls(int force_run)
491 {
492   int i, n;
493   unsigned num_const = 0;
494   unsigned num_pure  = 0;
495
496   if (! get_opt_function_call())
497     return;
498
499   /* prepare: mark all graphs as not analyzed */
500   n = get_irp_n_irgs();
501   for (i = n - 1; i >= 0; --i)
502     MARK_IRG(get_irp_irg(i));
503
504   /* first step: detect, which functions are const, i.e. do NOT touch any memory */
505   for (i = n - 1; i >= 0; --i) {
506     ir_graph *irg = get_irp_irg(i);
507     unsigned mode = is_pure_function(irg);
508
509     if (mode & mtp_property_const)
510       ++num_const;
511     else if (mode & mtp_property_pure)
512       ++num_pure;
513   }
514
515   if (force_run || num_const > 0) {
516     env_t ctx;
517
518     handle_const_Calls(&ctx);
519     if (get_firm_verbosity()) {
520       printf("Detected %d graphs without side effects.\n", num_const);
521       printf("Optimizes %d(SymConst) + %d(Sel) calls to const/pure functions.\n",
522                ctx.n_calls_removed_SymConst, ctx.n_calls_removed_Sel);
523     }
524   }
525   else {
526     if (get_firm_verbosity()) {
527       printf("No graphs without side effects detected\n");
528     }
529   }
530 }  /* optimize_funccalls */