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