3 * File name: ir/lower/lower_intrinsics.c
4 * Purpose: lowering of Calls of intrinsic functions
8 * Copyright: (c) 1998-2005 Universität Karlsruhe
9 * Licence: This file protected by GPL - GNU GENERAL PUBLIC LICENSE.
32 #include "lower_intrinsics.h"
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. */
43 * walker: call all mapper functions
45 static void call_mapper(ir_node *node, void *env) {
46 walker_env_t *wenv = env;
47 ir_op *op = get_irn_op(node);
52 const i_call_record *r;
55 symconst = get_Call_ptr(node);
56 if (get_irn_op(symconst) != op_SymConst ||
57 get_SymConst_kind(symconst) != symconst_addr_ent)
60 ent = get_SymConst_entity(symconst);
61 p = pmap_find(wenv->c_map, ent);
65 wenv->nr_of_intrinsics += r->i_mapper(node, r->ctx) ? 1 : 0;
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 */
73 if (r->i_mapper(node, r->ctx)) {
74 ++wenv->nr_of_intrinsics;
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();
87 pmap *c_map = pmap_create_ex(length);
88 i_instr_record **i_map;
89 unsigned nr_of_intrinsics = 0;
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);
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);
102 ir_op *op = list[i].i_instr.op;
103 assert(0 <= op->code && op->code < ARR_LEN(i_map));
105 list[i].i_instr.link = i_map[op->code];
106 i_map[op->code] = &list[i].i_instr;
113 for (i = get_irp_n_irgs() - 1; i >= 0; --i) {
114 irg = get_irp_irg(i);
116 wenv.nr_of_intrinsics = 0;
117 irg_walk_graph(irg, NULL, call_mapper, &wenv);
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);
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);
129 /* calls might be removed/added */
130 set_trouts_inconsistent();
132 /* optimize it, tuple might be created */
133 local_optimize_graph(irg);
135 nr_of_intrinsics += wenv.nr_of_intrinsics;
140 return nr_of_intrinsics;
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);
149 dbg_info *dbg = get_irn_dbg_info(call);
151 irn = new_rd_Abs(dbg, current_ir_graph, block, op, get_irn_mode(op));
152 irn = new_Tuple(1, &irn);
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());
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);
170 dbg_info *dbg = get_irn_dbg_info(call);
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);
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());
188 #define LMAX(a, b) ((a) > (b) ? (a) : (b))
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;
195 ir_node *mem, *bl, *call, *addr, *res_proj;
200 /* check if the result modes match */
201 if (get_irn_mode(node) != rt->mode)
204 arity = get_irn_arity(node);
208 mtp = get_entity_type(rt->ent);
209 n_param = get_method_n_params(mtp);
210 irg = current_ir_graph;
212 mem = get_irn_n(node, 0);
213 if (get_irn_mode(mem) != mode_M) {
214 mem = new_r_NoMem(irg);
220 /* check if the modes of the predecessors match the parameter modes */
221 if (arity - first != n_param)
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);
228 if (get_type_mode(param_tp) != get_irn_mode(pred))
232 n_res = get_method_n_ress(mtp);
234 /* step 0: calculate the number of needed Proj's */
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);
242 if (rt->mode != mode_T) /* must be mode_T */
248 if (get_type_mode(get_method_res_type(mtp, 0)) != rt->mode)
252 /* ok, when we are here, the number of predecessors match as well as the parameter modes */
253 bl = get_nodes_block(node);
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);
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));
269 res_proj = new_r_Proj(irg, bl, call, mode_T, pn_Call_T_result);
277 turn_into_tuple(node, n_proj);
279 for (i = 0; i < n_proj; ++i)
280 set_Tuple_pred(node, i, new_r_Bad(irg));
281 if (rt->mem_proj_nr >= 0)
282 set_Tuple_pred(node, rt->mem_proj_nr, new_r_Proj(irg, bl, call, mode_M, pn_Call_M_regular));
283 if (get_irn_op(mem) != op_NoMem) {
284 /* Exceptions can only be handled with real memory */
285 if (rt->exc_proj_nr >= 0)
286 set_Tuple_pred(node, rt->mem_proj_nr, new_r_Proj(irg, bl, call, mode_X, pn_Call_X_except));
287 if (rt->exc_mem_proj_nr >= 0)
288 set_Tuple_pred(node, rt->mem_proj_nr, new_r_Proj(irg, bl, call, mode_M, pn_Call_M_except));
291 if (rt->res_proj_nr >= 0)
292 for (i = 0; i < n_res; ++i)
293 set_Tuple_pred(node, rt->res_proj_nr + i,
294 new_r_Proj(irg, bl, res_proj, get_type_mode(get_method_res_type(mtp, i)), i));
298 /* only one return value supported */
300 ir_mode *mode = get_type_mode(get_method_res_type(mtp, 0));
302 res_proj = new_r_Proj(irg, bl, call, mode_T, pn_Call_T_result);
303 res_proj = new_r_Proj(irg, bl, res_proj, mode, 0);
305 exchange(node, res_proj);
309 /* should not happen */