bc47dc8512bffbc1ba10d04adcd585314410ea39
[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 (ent == unknown_entity) {
70           /* we don't know which entity is called here */
71           return;
72         }
73         if ((get_entity_additional_properties(ent) & mtp_property_const) == 0)
74           return;
75       }
76       ++ctx->n_calls_removed_Sel;
77     }
78     else
79       return;
80
81     /* ok, if we get here we found a call to a const function,
82      * route the NoMem node to the call */
83     mem   = get_Call_mem(call);
84
85     set_irn_link(call, mem);
86     set_Call_mem(call, new_r_NoMem(current_ir_graph));
87
88     /* finally, this call can float */
89     set_irn_pinned(call, op_pin_state_floats);
90
91     hook_func_call(current_ir_graph, call);
92
93     ctx->changed = 1;
94   }
95   else if (get_irn_op(node) == op_Proj) {
96     /*
97      * Remove memory and exception Proj's from
98      * const function calls.
99      */
100     call = get_Proj_pred(node);
101     if ((get_irn_op(call) != op_Call) ||
102         (get_irn_op(get_Call_mem(call)) != op_NoMem))
103       return;
104
105     switch (get_Proj_proj(node)) {
106     case pn_Call_M_regular: {
107       ir_node *old_mem = get_irn_link(call);
108       if (old_mem)
109         exchange(node, old_mem);
110     } break;
111     case pn_Call_X_except:
112     case pn_Call_M_except:
113       exchange(node, new_Bad());
114       break;
115     default: ;
116     }
117   }
118 }
119
120 /*
121  * optimize function calls by handling const functions
122  */
123 void optimize_funccalls(int force_run)
124 {
125   int i, j;
126   int change;
127   unsigned num_pure = 0;
128
129   if (! get_opt_real_function_call())
130     return;
131
132   /* first step: detect, which functions are const, i.e. do NOT touch any memory */
133   for (i = get_irp_n_irgs() - 1; i >= 0; --i) {
134     ir_graph *irg  = get_irp_irg(i);
135     ir_node *end   = get_irg_end(irg);
136     ir_node *endbl = get_nodes_block(end);
137
138     change = 0;
139
140     if (get_irg_additional_properties(irg) & mtp_property_const) {
141       /* already marked as a const function */
142       ++num_pure;
143     }
144     else {
145       /* visit every Return */
146       for (j = get_Block_n_cfgpreds(endbl) - 1; j >= 0; --j) {
147         ir_node *node = get_Block_cfgpred(endbl, j);
148         ir_op   *op   = get_irn_op(node);
149         ir_node *mem;
150
151         /* Bad nodes usually do NOT produce anything, so it's ok */
152         if (op == op_Bad)
153           continue;
154
155         if (op == op_Return) {
156           mem = get_Return_mem(node);
157
158           /* Bad nodes usually do NOT produce anything, so it's ok */
159           if (is_Bad(mem))
160             continue;
161
162           change = mem != get_irg_initial_mem(irg);
163           if (change)
164             break;
165         }
166         else {
167           /* exception found */
168           change = 1;
169           break;
170         }
171       }
172
173       if (! change) {
174         /* check, if a keep-alive exists */
175         for (j = get_End_n_keepalives(end) - 1; j >= 0; --j) {
176           ir_node *mem = get_End_keepalive(end, j);
177
178           if (mode_M != get_irn_mode(mem))
179             continue;
180
181           change = mem != get_irg_initial_mem(irg);
182           if (change)
183             break;
184         }
185       }
186
187       if (! change) {
188         /* no memory changes found, it's a const function */
189         set_irg_additional_property(irg, mtp_property_const);
190         ++num_pure;
191       }
192     }
193   }
194
195   if (force_run || num_pure > 0) {
196     env_t ctx;
197
198     ctx.n_calls_removed_SymConst = 0;
199     ctx.n_calls_removed_Sel = 0;
200
201     /* all calls of pure functions can be transformed into FuncCalls */
202     for (i = get_irp_n_irgs() - 1; i >= 0; --i) {
203       ir_graph *irg  = get_irp_irg(i);
204
205       /* no need to do this on const functions */
206       if ((get_irg_additional_properties(irg) & mtp_property_const) == 0) {
207         ctx.changed = 0;
208         irg_walk_graph(irg, NULL, rem_mem_from_const_fkt_calls, &ctx);
209
210         if (ctx.changed) {
211           /* changes were done */
212           set_irg_outs_inconsistent(irg);
213           set_irg_loopinfo_state(current_ir_graph, loopinfo_cf_inconsistent);
214         }
215       }
216     }
217
218     if (get_firm_verbosity()) {
219       printf("Detected %d graphs without side effects.\n", num_pure);
220       printf("Optimizes %d(SymConst) + %d(Sel) calls to const functions.\n",
221              ctx.n_calls_removed_SymConst, ctx.n_calls_removed_Sel);
222     }
223   }
224   else {
225     if (get_firm_verbosity()) {
226       printf("No graphs without side effects detected\n");
227     }
228   }
229 }