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