Every node has now a pinned attribute that is inherited from the op.
[libfirm] / ir / lower / lower_intrinsics.c
1 /*
2  * Project:     libFIRM
3  * File name:   ir/lower/lower_intrinsics.c
4  * Purpose:     lowering of Calls of intrinsic functions
5  * Author:      Michael Beck
6  * Created:
7  * CVS-ID:      $Id$
8  * Copyright:   (c) 1998-2005 Universität Karlsruhe
9  * Licence:     This file protected by GPL -  GNU GENERAL PUBLIC LICENSE.
10  */
11
12 #ifdef HAVE_CONFIG_H
13 #include "config.h"
14 #endif
15
16 #ifdef HAVE_ALLOCA_H
17 #include <alloca.h>
18 #endif
19 #ifdef HAVE_MALLOC_H
20 #include <malloc.h>
21 #endif
22
23 #include "irop_t.h"
24 #include "irprog_t.h"
25 #include "irnode_t.h"
26 #include "irprog_t.h"
27 #include "irgwalk.h"
28 #include "ircons.h"
29 #include "irgmod.h"
30 #include "irgopt.h"
31 #include "trouts.h"
32 #include "lower_intrinsics.h"
33 #include "pmap.h"
34
35 /** Walker environment */
36 typedef struct _walker_env {
37   pmap     *c_map;              /**< The intrinsic call map. */
38   unsigned nr_of_intrinsics;    /**< statistics */
39   i_instr_record **i_map;       /**< The intrinsic instruction map. */
40 } walker_env_t;
41
42 /**
43  * walker: call all mapper functions
44  */
45 static void call_mapper(ir_node *node, void *env) {
46   walker_env_t *wenv = env;
47   ir_op *op = get_irn_op(node);
48
49   if (op == op_Call) {
50     ir_node *symconst;
51     pmap_entry *p;
52     const i_call_record *r;
53     entity *ent;
54
55     symconst = get_Call_ptr(node);
56     if (get_irn_op(symconst) != op_SymConst ||
57         get_SymConst_kind(symconst) != symconst_addr_ent)
58       return;
59
60     ent = get_SymConst_entity(symconst);
61     p   = pmap_find(wenv->c_map, ent);
62
63     if (p) {
64       r = p->value;
65       wenv->nr_of_intrinsics += r->i_mapper(node, r->ctx) ? 1 : 0;
66     }
67   }
68   else {
69     if (0 <= op->code && op->code < ARR_LEN(wenv->i_map)) {
70       const i_instr_record *r = wenv->i_map[op->code];
71       /* run all possible mapper */
72       while (r) {
73         if (r->i_mapper(node, r->ctx)) {
74           ++wenv->nr_of_intrinsics;
75           break;
76         }
77         r = r->link;
78       }
79     }
80   }
81 }
82
83 /* Go through all graphs and map calls to intrinsic functions. */
84 unsigned lower_intrinsics(i_record *list, int length) {
85   int            i, n_ops = get_irp_n_opcodes();
86   ir_graph       *irg;
87   pmap           *c_map = pmap_create_ex(length);
88   i_instr_record **i_map;
89   unsigned       nr_of_intrinsics = 0;
90   walker_env_t   wenv;
91
92   /* we use the ir_op generic pointers here */
93   NEW_ARR_A(const i_instr_record *, i_map, n_ops);
94   memset((void *)i_map, 0, sizeof(*i_map) * n_ops);
95
96   /* fill a map for faster search */
97   for (i = length - 1; i >= 0; --i) {
98     if (list[i].i_call.kind == INTRINSIC_CALL) {
99       pmap_insert(c_map, list[i].i_call.i_ent, (void *)&list[i].i_call);
100     }
101     else {
102       ir_op *op = list[i].i_instr.op;
103       assert(0 <= op->code && op->code < ARR_LEN(i_map));
104
105       list[i].i_instr.link = i_map[op->code];
106       i_map[op->code] = &list[i].i_instr;
107     }
108   }
109
110   wenv.c_map = c_map;
111   wenv.i_map = i_map;
112
113   for (i = get_irp_n_irgs() - 1; i >= 0; --i) {
114     irg = get_irp_irg(i);
115
116     wenv.nr_of_intrinsics = 0;
117     irg_walk_graph(irg, NULL, call_mapper, &wenv);
118
119     if (wenv.nr_of_intrinsics) {
120       /* changes detected */
121       set_irg_outs_inconsistent(irg);
122       set_irg_callee_info_state(irg, irg_callee_info_inconsistent);
123
124       /* exception control flow might have changed */
125       set_irg_doms_inconsistent(irg);
126       set_irg_extblk_inconsistent(irg);
127       set_irg_loopinfo_inconsistent(irg);
128
129       /* calls might be removed/added */
130       set_trouts_inconsistent();
131
132       /* optimize it, tuple might be created */
133       local_optimize_graph(irg);
134
135       nr_of_intrinsics += wenv.nr_of_intrinsics;
136     }
137   }
138   pmap_destroy(c_map);
139
140   return nr_of_intrinsics;
141 }
142
143 /* A mapper for the integer abs. */
144 int i_mapper_Abs(ir_node *call, void *ctx) {
145   ir_node *mem   = get_Call_mem(call);
146   ir_node *block = get_nodes_block(call);
147   ir_node *op    = get_Call_param(call, 0);
148   ir_node *irn;
149   dbg_info *dbg  = get_irn_dbg_info(call);
150
151   irn = new_rd_Abs(dbg, current_ir_graph, block, op, get_irn_mode(op));
152   irn = new_Tuple(1, &irn);
153
154   turn_into_tuple(call, pn_Call_max);
155   set_Tuple_pred(call, pn_Call_M_regular, mem);
156   set_Tuple_pred(call, pn_Call_X_except, new_Bad());
157   set_Tuple_pred(call, pn_Call_T_result, irn);
158   set_Tuple_pred(call, pn_Call_M_except, mem);
159   set_Tuple_pred(call, pn_Call_P_value_res_base, new_Bad());
160
161   return 1;
162 }
163
164 /* A mapper for the alloca() function. */
165 int i_mapper_Alloca(ir_node *call, void *ctx) {
166   ir_node *mem   = get_Call_mem(call);
167   ir_node *block = get_nodes_block(call);
168   ir_node *op    = get_Call_param(call, 0);
169   ir_node *irn, *exc;
170   dbg_info *dbg  = get_irn_dbg_info(call);
171
172   irn = new_rd_Alloc(dbg, current_ir_graph, block, mem, op, firm_unknown_type, stack_alloc);
173   mem = new_Proj(irn, mode_M, pn_Alloc_M);
174   exc = new_Proj(irn, mode_X, pn_Alloc_X_except);
175   irn = new_Proj(irn, get_modeP_data(), pn_Alloc_res);
176   irn = new_Tuple(1, &irn);
177
178   turn_into_tuple(call, pn_Call_max);
179   set_Tuple_pred(call, pn_Call_M_regular, mem);
180   set_Tuple_pred(call, pn_Call_X_except, exc);
181   set_Tuple_pred(call, pn_Call_T_result, irn);
182   set_Tuple_pred(call, pn_Call_M_except, mem);
183   set_Tuple_pred(call, pn_Call_P_value_res_base, new_Bad());
184
185   return 1;
186 }
187
188 #define LMAX(a, b) ((a) > (b) ? (a) : (b))
189
190 /* A mapper for mapping unsupported instructions to runtime calls. */
191 int i_mapper_RuntimeCall(ir_node *node, runtime_rt *rt) {
192   int i, j, arity, first, n_param, n_res;
193   long n_proj;
194   ir_type *mtp;
195   ir_node *mem, *bl, *call, *addr, *res_proj;
196   ir_node **in;
197   ir_graph *irg;
198   symconst_symbol sym;
199
200   /* check if the result modes match */
201   if (get_irn_mode(node) != rt->mode)
202     return 0;
203
204   arity = get_irn_arity(node);
205   if (arity <= 0)
206     return 0;
207
208   mtp     = get_entity_type(rt->ent);
209   n_param = get_method_n_params(mtp);
210   irg     = current_ir_graph;
211
212   mem = get_irn_n(node, 0);
213   if (get_irn_mode(mem) != mode_M) {
214     mem = new_r_NoMem(irg);
215     first = 0;
216   }
217   else
218     first = 1;
219
220   /* check if the modes of the predecessors match the parameter modes */
221   if (arity - first != n_param)
222     return 0;
223
224   for (i = first, j = 0; i < arity; ++i, ++j) {
225     ir_type *param_tp = get_method_param_type(mtp, j);
226     ir_node *pred = get_irn_n(node, i);
227
228     if (get_type_mode(param_tp) != get_irn_mode(pred))
229       return 0;
230   }
231
232   n_res = get_method_n_ress(mtp);
233
234   /* step 0: calculate the number of needed Proj's */
235   n_proj = 0;
236   n_proj = LMAX(n_proj, rt->mem_proj_nr + 1);
237   n_proj = LMAX(n_proj, rt->exc_proj_nr + 1);
238   n_proj = LMAX(n_proj, rt->exc_mem_proj_nr + 1);
239   n_proj = LMAX(n_proj, rt->res_proj_nr + 1);
240
241   if (n_proj > 0) {
242     if (rt->mode != mode_T) /* must be mode_T */
243       return 0;
244   }
245   else {
246     if (n_res > 0)
247       /* must match */
248       if (get_type_mode(get_method_res_type(mtp, 0)) != rt->mode)
249         return 0;
250   }
251
252   /* ok, when we are here, the number of predecessors match as well as the parameter modes */
253   bl = get_nodes_block(node);
254
255   in = NULL;
256   if (n_param > 0) {
257     NEW_ARR_A(ir_node *, in, n_param);
258     for (i = 0; i < n_param; ++i)
259       in[i] = get_irn_n(node, first + i);
260   }
261
262   /* step 1: create the call */
263   sym.entity_p = rt->ent;
264   addr = new_r_SymConst(irg, bl, sym, symconst_addr_ent);
265   call = new_rd_Call(get_irn_dbg_info(node), irg, bl, mem, addr, n_param, in, mtp);
266   set_irn_pinned(call, get_irn_pinned(node));
267
268   if (n_res > 0)
269     res_proj = new_r_Proj(irg, bl, call, mode_T, pn_Call_T_result);
270
271   if (n_proj > 0) {
272     n_proj += n_res - 1;
273
274     /* we are ready */
275     turn_into_tuple(node, n_proj);
276
277     for (i = 0; i < n_proj; ++i)
278       set_Tuple_pred(node, i, new_r_Bad(irg));
279     if (rt->mem_proj_nr >= 0)
280       set_Tuple_pred(node, rt->mem_proj_nr, new_r_Proj(irg, bl, call, mode_M, pn_Call_M_regular));
281     if (get_irn_op(mem) != op_NoMem) {
282       /* Exceptions can only be handled with real memory */
283       if (rt->exc_proj_nr >= 0)
284         set_Tuple_pred(node, rt->mem_proj_nr, new_r_Proj(irg, bl, call, mode_X, pn_Call_X_except));
285       if (rt->exc_mem_proj_nr >= 0)
286         set_Tuple_pred(node, rt->mem_proj_nr, new_r_Proj(irg, bl, call, mode_M, pn_Call_M_except));
287     }
288
289     if (rt->res_proj_nr >= 0)
290       for (i = 0; i < n_res; ++i)
291         set_Tuple_pred(node, rt->res_proj_nr + i,
292           new_r_Proj(irg, bl, res_proj, get_type_mode(get_method_res_type(mtp, i)), i));
293     return 1;
294   }
295   else {
296     /* only one return value supported */
297     if (n_res > 0) {
298       ir_mode *mode = get_type_mode(get_method_res_type(mtp, 0));
299
300       res_proj = new_r_Proj(irg, bl, call, mode_T, pn_Call_T_result);
301       res_proj = new_r_Proj(irg, bl, res_proj, mode, 0);
302
303       exchange(node, res_proj);
304       return 1;
305     }
306   }
307   /* should not happen */
308   return 0;
309 }