1002450f2126e0831b992b47459d4a3a7d1223cc
[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-2004 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  changed;                   /**< flag, is set if a graph was changed */
27   int  n_calls_removed_SymConst;
28   int  n_calls_removed_Sel;
29 } env_t;
30
31 /**
32  * remove memory from const function calls by rerouting
33  * it's ProjM and connection the call with a NoMem node.
34  *
35  * Note: By "const function" we understand a function that did neither
36  * read nor write memory. Hence its result depends solely on its
37  * arguments.
38  */
39 static void rem_mem_from_const_fkt_calls(ir_node *node, void *env)
40 {
41   env_t *ctx = env;
42   ir_node *call, *ptr, *mem;
43   entity *ent;
44
45   if (get_irn_op(node) == op_Call) {
46     call = node;
47
48     set_irn_link(call, NULL);
49
50     ptr = get_Call_ptr(call);
51     if (get_irn_op(ptr) == op_SymConst && get_SymConst_kind(ptr) == symconst_addr_ent) {
52       ent = get_SymConst_entity(ptr);
53
54       if ((get_entity_additional_properties(ent) & mtp_property_const) == 0)
55         return;
56       ++ctx->n_calls_removed_SymConst;
57     }
58     else if (get_irn_op(ptr) == op_Sel &&
59              get_irg_callee_info_state(current_ir_graph) == irg_callee_info_consistent) {
60       /* If all possible callees are real 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       for (i = 0; i < n_callees; ++i) {
68         ent = get_Call_callee(call, i);
69         if ((get_entity_additional_properties(ent) & mtp_property_const) == 0)
70                 return;
71       }
72       ++ctx->n_calls_removed_Sel;
73     }
74     else
75       return;
76
77     /* ok, if we get here we found a call to a const function,
78      * route the NoMem node to the call */
79     mem   = get_Call_mem(call);
80
81     set_irn_link(call, mem);
82     set_Call_mem(call, new_r_NoMem(current_ir_graph));
83
84     /* finally, this call can float */
85     set_irn_pinned(call, op_pin_state_floats);
86
87     hook_func_call(current_ir_graph, call);
88
89     ctx->changed = 1;
90   }
91   else if (get_irn_op(node) == op_Proj) {
92     /*
93      * Remove memory and exception Proj's from
94      * const function calls.
95      */
96     call = get_Proj_pred(node);
97     if ((get_irn_op(call) != op_Call) ||
98         (get_irn_op(get_Call_mem(call)) != op_NoMem))
99       return;
100
101     switch (get_Proj_proj(node)) {
102     case pn_Call_M_regular: {
103       ir_node *old_mem = get_irn_link(call);
104       if (old_mem)
105         exchange(node, old_mem);
106     } break;
107     case pn_Call_X_except:
108     case pn_Call_M_except:
109       exchange(node, new_Bad());
110       break;
111     default: ;
112     }
113   }
114 }
115
116 /*
117  * optimize function calls by handling const functions
118  */
119 void optimize_funccalls(int force_run)
120 {
121   int i, j;
122   int change;
123   unsigned num_pure = 0;
124
125   if (! get_opt_real_func_call())
126     return;
127
128   /* first step: detect, which functions are const, i.e. do NOT touch any memory */
129   for (i = get_irp_n_irgs() - 1; i >= 0; --i) {
130     ir_graph *irg  = get_irp_irg(i);
131     ir_node *end   = get_irg_end(irg);
132     ir_node *endbl = get_nodes_block(end);
133
134     change = 0;
135
136     if (get_irg_additional_properties(irg) & mtp_property_const) {
137       /* already marked as a const function */
138       ++num_pure;
139     }
140     else {
141       /* visit every Return */
142       for (j = get_Block_n_cfgpreds(endbl) - 1; j >= 0; --j) {
143         ir_node *node = get_Block_cfgpred(endbl, j);
144         ir_op   *op   = get_irn_op(node);
145         ir_node *mem;
146
147         /* Bad nodes usually do NOT produce anything, so it's ok */
148         if (op == op_Bad)
149           continue;
150
151         if (op == op_Return) {
152           mem = get_Return_mem(node);
153
154           /* Bad nodes usually do NOT produce anything, so it's ok */
155           if (is_Bad(mem))
156             continue;
157
158           change = mem != get_irg_initial_mem(irg);
159           if (change)
160             break;
161         }
162         else {
163           /* exception found */
164           change = 1;
165           break;
166         }
167       }
168
169       if (! change) {
170         /* check, if a keep-alive exists */
171         for (j = get_End_n_keepalives(end) - 1; j >= 0; --j) {
172           ir_node *mem = get_End_keepalive(end, j);
173
174           if (mode_M != get_irn_mode(mem))
175             continue;
176
177           change = mem != get_irg_initial_mem(irg);
178           if (change)
179             break;
180         }
181       }
182
183       if (! change) {
184         /* no memory changes found, it's a const function */
185         set_irg_additional_property(irg, mtp_property_const);
186         ++num_pure;
187       }
188     }
189   }
190
191   if (force_run || num_pure > 0) {
192     env_t ctx;
193
194     ctx.n_calls_removed_SymConst = 0;
195     ctx.n_calls_removed_Sel = 0;
196
197     /* all calls of pure functions can be transformed into FuncCalls */
198     for (i = get_irp_n_irgs() - 1; i >= 0; --i) {
199       ir_graph *irg  = get_irp_irg(i);
200
201       /* no need to do this on const functions */
202       if ((get_irg_additional_properties(irg) & mtp_property_const) == 0) {
203         ctx.changed = 0;
204         irg_walk_graph(irg, NULL, rem_mem_from_const_fkt_calls, &ctx);
205
206         if (ctx.changed) {
207           /* changes were done */
208           set_irg_outs_inconsistent(irg);
209           set_irg_loopinfo_state(current_ir_graph, loopinfo_cf_inconsistent);
210         }
211       }
212     }
213
214     if (get_firm_verbosity()) {
215       printf("Detected %d graphs without side effects.\n", num_pure);
216       printf("Optimizes %d(SymConst) + %d(Sel) calls to const functions.\n",
217              ctx.n_calls_removed_SymConst, ctx.n_calls_removed_Sel);
218     }
219   }
220   else {
221     if (get_firm_verbosity()) {
222       printf("No graphs without side effects detected\n");
223     }
224   }
225 }