2 * Copyright (C) 1995-2007 University of Karlsruhe. All right reserved.
4 * This file is part of libFirm.
6 * This file may be distributed and/or modified under the terms of the
7 * GNU General Public License version 2 as published by the Free Software
8 * Foundation and appearing in the file LICENSE.GPL included in the
9 * packaging of this file.
11 * Licensees holding valid libFirm Professional Edition licenses may use
12 * this file in accordance with the libFirm Commercial License.
13 * Agreement provided with the Software.
15 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
16 * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * @brief lowering of Calls of intrinsic functions
23 * @author Michael Beck
44 /** Walker environment */
45 typedef struct _walker_env {
46 pmap *c_map; /**< The intrinsic call map. */
47 unsigned nr_of_intrinsics; /**< statistics */
48 i_instr_record **i_map; /**< The intrinsic instruction map. */
52 * walker: call all mapper functions
54 static void call_mapper(ir_node *node, void *env) {
55 walker_env_t *wenv = env;
56 ir_op *op = get_irn_op(node);
61 const i_call_record *r;
64 symconst = get_Call_ptr(node);
65 if (get_irn_op(symconst) != op_SymConst ||
66 get_SymConst_kind(symconst) != symconst_addr_ent)
69 ent = get_SymConst_entity(symconst);
70 p = pmap_find(wenv->c_map, ent);
74 wenv->nr_of_intrinsics += r->i_mapper(node, r->ctx) ? 1 : 0;
77 if (op->code < (unsigned)ARR_LEN(wenv->i_map)) {
78 const i_instr_record *r = wenv->i_map[op->code];
79 /* run all possible mapper */
81 if (r->i_mapper(node, r->ctx)) {
82 ++wenv->nr_of_intrinsics;
91 /* Go through all graphs and map calls to intrinsic functions. */
92 unsigned lower_intrinsics(i_record *list, int length) {
93 int i, n_ops = get_irp_n_opcodes();
95 pmap *c_map = pmap_create_ex(length);
96 i_instr_record **i_map;
97 unsigned nr_of_intrinsics = 0;
100 /* we use the ir_op generic pointers here */
101 NEW_ARR_A(const i_instr_record *, i_map, n_ops);
102 memset((void *)i_map, 0, sizeof(*i_map) * n_ops);
104 /* fill a map for faster search */
105 for (i = length - 1; i >= 0; --i) {
106 if (list[i].i_call.kind == INTRINSIC_CALL) {
107 pmap_insert(c_map, list[i].i_call.i_ent, (void *)&list[i].i_call);
109 ir_op *op = list[i].i_instr.op;
110 assert(op->code < (unsigned)ARR_LEN(i_map));
112 list[i].i_instr.link = i_map[op->code];
113 i_map[op->code] = &list[i].i_instr;
120 for (i = get_irp_n_irgs() - 1; i >= 0; --i) {
121 irg = get_irp_irg(i);
123 wenv.nr_of_intrinsics = 0;
124 irg_walk_graph(irg, NULL, call_mapper, &wenv);
126 if (wenv.nr_of_intrinsics) {
127 /* changes detected */
128 set_irg_outs_inconsistent(irg);
129 set_irg_callee_info_state(irg, irg_callee_info_inconsistent);
131 /* exception control flow might have changed */
132 set_irg_doms_inconsistent(irg);
133 set_irg_extblk_inconsistent(irg);
134 set_irg_loopinfo_inconsistent(irg);
136 /* calls might be removed/added */
137 set_trouts_inconsistent();
139 /* optimize it, tuple might be created */
140 local_optimize_graph(irg);
142 nr_of_intrinsics += wenv.nr_of_intrinsics;
147 return nr_of_intrinsics;
150 /* A mapper for the integer abs. */
151 int i_mapper_Abs(ir_node *call, void *ctx) {
152 ir_node *mem = get_Call_mem(call);
153 ir_node *block = get_nodes_block(call);
154 ir_node *op = get_Call_param(call, 0);
156 dbg_info *dbg = get_irn_dbg_info(call);
158 irn = new_rd_Abs(dbg, current_ir_graph, block, op, get_irn_mode(op));
159 irn = new_Tuple(1, &irn);
161 turn_into_tuple(call, pn_Call_max);
162 set_Tuple_pred(call, pn_Call_M_regular, mem);
163 set_Tuple_pred(call, pn_Call_X_regular, new_r_Jmp(current_ir_graph, block));
164 set_Tuple_pred(call, pn_Call_X_except, new_Bad());
165 set_Tuple_pred(call, pn_Call_T_result, irn);
166 set_Tuple_pred(call, pn_Call_M_except, mem);
167 set_Tuple_pred(call, pn_Call_P_value_res_base, new_Bad());
172 /* A mapper for the alloca() function. */
173 int i_mapper_Alloca(ir_node *call, void *ctx) {
174 ir_node *mem = get_Call_mem(call);
175 ir_node *block = get_nodes_block(call);
176 ir_node *op = get_Call_param(call, 0);
177 ir_node *irn, *exc, *no_exc;
178 dbg_info *dbg = get_irn_dbg_info(call);
180 irn = new_rd_Alloc(dbg, current_ir_graph, block, mem, op, firm_unknown_type, stack_alloc);
181 mem = new_Proj(irn, mode_M, pn_Alloc_M);
182 no_exc = new_Proj(irn, mode_X, pn_Alloc_X_regular);
183 exc = new_Proj(irn, mode_X, pn_Alloc_X_except);
184 irn = new_Proj(irn, get_modeP_data(), pn_Alloc_res);
185 irn = new_Tuple(1, &irn);
187 turn_into_tuple(call, pn_Call_max);
188 set_Tuple_pred(call, pn_Call_M_regular, mem);
189 set_Tuple_pred(call, pn_Call_X_regular, no_exc);
190 set_Tuple_pred(call, pn_Call_X_except, exc);
191 set_Tuple_pred(call, pn_Call_T_result, irn);
192 set_Tuple_pred(call, pn_Call_M_except, mem);
193 set_Tuple_pred(call, pn_Call_P_value_res_base, new_Bad());
199 * Returns the result mode of a node.
201 static ir_mode *get_irn_res_mode(ir_node *node) {
202 switch (get_irn_opcode(node)) {
203 case iro_Load: return get_Load_mode(node);
204 case iro_Quot: return get_Quot_resmode(node);
205 case iro_Div: return get_Div_resmode(node);
206 case iro_Mod: return get_Mod_resmode(node);
207 case iro_DivMod: return get_DivMod_resmode(node);
208 default: return NULL;
212 #define LMAX(a, b) ((a) > (b) ? (a) : (b))
214 /* A mapper for mapping unsupported instructions to runtime calls. */
215 int i_mapper_RuntimeCall(ir_node *node, runtime_rt *rt) {
216 int i, j, arity, first, n_param, n_res;
219 ir_node *mem, *bl, *call, *addr, *res_proj;
223 ir_mode *mode = get_irn_mode(node);
225 if (mode != rt->mode)
227 /* check if the result modes match */
228 if (mode == mode_T && rt->res_mode != NULL) {
229 mode = get_irn_res_mode(node);
230 if (mode != rt->res_mode)
234 arity = get_irn_arity(node);
238 mtp = get_entity_type(rt->ent);
239 n_param = get_method_n_params(mtp);
240 irg = current_ir_graph;
242 mem = get_irn_n(node, 0);
243 if (get_irn_mode(mem) != mode_M) {
244 mem = new_r_NoMem(irg);
249 /* check if the modes of the predecessors match the parameter modes */
250 if (arity - first != n_param)
253 for (i = first, j = 0; i < arity; ++i, ++j) {
254 ir_type *param_tp = get_method_param_type(mtp, j);
255 ir_node *pred = get_irn_n(node, i);
257 if (get_type_mode(param_tp) != get_irn_mode(pred))
261 n_res = get_method_n_ress(mtp);
263 /* step 0: calculate the number of needed Proj's */
265 n_proj = LMAX(n_proj, rt->mem_proj_nr + 1);
266 n_proj = LMAX(n_proj, rt->regular_proj_nr + 1);
267 n_proj = LMAX(n_proj, rt->exc_proj_nr + 1);
268 n_proj = LMAX(n_proj, rt->exc_mem_proj_nr + 1);
269 n_proj = LMAX(n_proj, rt->res_proj_nr + 1);
272 if (rt->mode != mode_T) /* must be mode_T */
277 if (get_type_mode(get_method_res_type(mtp, 0)) != rt->mode)
281 /* ok, when we are here, the number of predecessors match as well as the parameter modes */
282 bl = get_nodes_block(node);
286 NEW_ARR_A(ir_node *, in, n_param);
287 for (i = 0; i < n_param; ++i)
288 in[i] = get_irn_n(node, first + i);
291 /* step 1: create the call */
292 sym.entity_p = rt->ent;
293 addr = new_r_SymConst(irg, bl, sym, symconst_addr_ent);
294 call = new_rd_Call(get_irn_dbg_info(node), irg, bl, mem, addr, n_param, in, mtp);
295 set_irn_pinned(call, get_irn_pinned(node));
298 res_proj = new_r_Proj(irg, bl, call, mode_T, pn_Call_T_result);
306 turn_into_tuple(node, n_proj);
308 for (i = 0; i < n_proj; ++i)
309 set_Tuple_pred(node, i, new_r_Bad(irg));
310 if (rt->mem_proj_nr >= 0)
311 set_Tuple_pred(node, rt->mem_proj_nr, new_r_Proj(irg, bl, call, mode_M, pn_Call_M_regular));
312 if (get_irn_op(mem) != op_NoMem) {
313 /* Exceptions can only be handled with real memory */
314 if (rt->regular_proj_nr >= 0)
315 set_Tuple_pred(node, rt->regular_proj_nr, new_r_Proj(irg, bl, call, mode_X, pn_Call_X_regular));
316 if (rt->exc_proj_nr >= 0)
317 set_Tuple_pred(node, rt->exc_proj_nr, new_r_Proj(irg, bl, call, mode_X, pn_Call_X_except));
318 if (rt->exc_mem_proj_nr >= 0)
319 set_Tuple_pred(node, rt->mem_proj_nr, new_r_Proj(irg, bl, call, mode_M, pn_Call_M_except));
322 if (rt->res_proj_nr >= 0)
323 for (i = 0; i < n_res; ++i)
324 set_Tuple_pred(node, rt->res_proj_nr + i,
325 new_r_Proj(irg, bl, res_proj, get_type_mode(get_method_res_type(mtp, i)), i));
328 /* only one return value supported */
330 ir_mode *mode = get_type_mode(get_method_res_type(mtp, 0));
332 res_proj = new_r_Proj(irg, bl, call, mode_T, pn_Call_T_result);
333 res_proj = new_r_Proj(irg, bl, res_proj, mode, 0);
335 exchange(node, res_proj);
339 /* should not happen */