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, int part_block_used) {
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);
124 collect_phiprojs(irg);
126 wenv.nr_of_intrinsics = 0;
127 irg_walk_graph(irg, NULL, call_mapper, &wenv);
129 if (wenv.nr_of_intrinsics) {
130 /* changes detected */
131 set_irg_outs_inconsistent(irg);
132 set_irg_callee_info_state(irg, irg_callee_info_inconsistent);
134 /* exception control flow might have changed */
135 set_irg_doms_inconsistent(irg);
136 set_irg_extblk_inconsistent(irg);
137 set_irg_loopinfo_inconsistent(irg);
139 /* calls might be removed/added */
140 set_trouts_inconsistent();
142 /* optimize it, tuple might be created */
143 local_optimize_graph(irg);
145 nr_of_intrinsics += wenv.nr_of_intrinsics;
150 return nr_of_intrinsics;
154 * Helper function, replace the call by the given node.
156 * @param irn the result node
157 * @param call the call to replace
158 * @param mem the new mem result
159 * @param reg_jmp new regular control flow, if NULL, a Jmp will be used
160 * @param exc_jmp new exception control flow, if reg_jmp == NULL, a Bad will be used
162 static void replace_call(ir_node *irn, ir_node *call, ir_node *mem, ir_node *reg_jmp, ir_node *exc_jmp) {
163 if (reg_jmp == NULL) {
164 ir_node *block = get_nodes_block(call);
166 /* Beware: do we need here a protection against CSE? Better we do it. */
167 int old_cse = get_opt_cse();
169 reg_jmp = new_r_Jmp(current_ir_graph, block);
170 set_opt_cse(old_cse);
173 irn = new_Tuple(1, &irn);
175 turn_into_tuple(call, pn_Call_max);
176 set_Tuple_pred(call, pn_Call_M_regular, mem);
177 set_Tuple_pred(call, pn_Call_X_regular, reg_jmp);
178 set_Tuple_pred(call, pn_Call_X_except, exc_jmp);
179 set_Tuple_pred(call, pn_Call_T_result, irn);
180 set_Tuple_pred(call, pn_Call_M_except, mem);
181 set_Tuple_pred(call, pn_Call_P_value_res_base, new_Bad());
184 /* A mapper for the integer abs. */
185 int i_mapper_abs(ir_node *call, void *ctx) {
186 ir_node *mem = get_Call_mem(call);
187 ir_node *block = get_nodes_block(call);
188 ir_node *op = get_Call_param(call, 0);
190 dbg_info *dbg = get_irn_dbg_info(call);
193 irn = new_rd_Abs(dbg, current_ir_graph, block, op, get_irn_mode(op));
194 replace_call(irn, call, mem, NULL, NULL);
198 /* A mapper for the alloca() function. */
199 int i_mapper_alloca(ir_node *call, void *ctx) {
200 ir_node *mem = get_Call_mem(call);
201 ir_node *block = get_nodes_block(call);
202 ir_node *op = get_Call_param(call, 0);
203 ir_node *irn, *exc, *no_exc;
204 dbg_info *dbg = get_irn_dbg_info(call);
207 irn = new_rd_Alloc(dbg, current_ir_graph, block, mem, op, firm_unknown_type, stack_alloc);
208 mem = new_Proj(irn, mode_M, pn_Alloc_M);
209 no_exc = new_Proj(irn, mode_X, pn_Alloc_X_regular);
210 exc = new_Proj(irn, mode_X, pn_Alloc_X_except);
211 irn = new_Proj(irn, get_modeP_data(), pn_Alloc_res);
213 replace_call(irn, call, mem, no_exc, exc);
217 /* A mapper for the floating point sqrt. */
218 int i_mapper_sqrt(ir_node *call, void *ctx) {
221 ir_node *op = get_Call_param(call, 0);
227 tv = get_Const_tarval(op);
228 if (! tarval_is_null(tv) && !tarval_is_one(tv))
231 mem = get_Call_mem(call);
233 /* sqrt(0) = 0, sqrt(1) = 1 */
234 replace_call(op, call, mem, NULL, NULL);
238 /* A mapper for the floating point cbrt. */
239 int i_mapper_cbrt(ir_node *call, void *ctx) {
242 ir_node *op = get_Call_param(call, 0);
248 tv = get_Const_tarval(op);
249 if (! tarval_is_null(tv) && !tarval_is_one(tv) && !tarval_is_minus_one(tv))
252 mem = get_Call_mem(call);
254 /* cbrt(0) = 0, cbrt(1) = 1, cbrt(-1) = -1 */
255 replace_call(op, call, mem, NULL, NULL);
259 /* A mapper for the floating point pow. */
260 int i_mapper_pow(ir_node *call, void *ctx) {
263 ir_node *left = get_Call_param(call, 0);
264 ir_node *right = get_Call_param(call, 1);
265 ir_node *block = get_nodes_block(call);
266 ir_node *irn, *reg_jmp = NULL, *exc_jmp = NULL;
269 if (is_Const(left) && is_Const_one(left)) {
270 /* pow (1.0, x) = 1.0 */
272 } else if (is_Const(right)) {
273 tarval *tv = get_Const_tarval(right);
274 if (tarval_is_null(tv)) {
275 /* pow(x, 0.0) = 1.0 */
276 ir_mode *mode = get_tarval_mode(tv);
277 irn = new_r_Const(current_ir_graph, block, mode, get_mode_one(mode));
278 } else if (tarval_is_one(tv)) {
279 /* pow(x, 1.0) = x */
281 } else if (tarval_is_minus_one(tv)) {
282 /* pow(x, -1.0) = 1/x */
290 mem = get_Call_mem(call);
291 dbg = get_irn_dbg_info(call);
294 ir_mode *mode = get_irn_mode(left);
297 irn = new_r_Const(current_ir_graph, block, mode, get_mode_one(mode));
298 quot = new_rd_Quot(dbg, current_ir_graph, block, mem, irn, left, mode, op_pin_state_pinned);
299 mem = new_r_Proj(current_ir_graph, block, quot, mode_M, pn_Quot_M);
300 irn = new_r_Proj(current_ir_graph, block, quot, mode, pn_Quot_res);
301 reg_jmp = new_r_Proj(current_ir_graph, block, quot, mode_X, pn_Quot_X_regular);
302 exc_jmp = new_r_Proj(current_ir_graph, block, quot, mode_X, pn_Quot_X_except);
304 replace_call(irn, call, mem, reg_jmp, exc_jmp);
308 /* A mapper for the floating point exp. */
309 int i_mapper_exp(ir_node *call, void *ctx) {
310 ir_node *val = get_Call_param(call, 0);
313 if (is_Const(val) && is_Const_null(val)) {
315 ir_node *block = get_nodes_block(call);
316 ir_mode *mode = get_irn_mode(val);
317 ir_node *irn = new_r_Const(current_ir_graph, block, mode, get_mode_one(mode));
318 ir_node *mem = get_Call_mem(call);
319 replace_call(irn, call, mem, NULL, NULL);
325 /* A mapper for the floating point log. */
326 int i_mapper_log(ir_node *call, void *ctx) {
327 ir_node *val = get_Call_param(call, 0);
330 if (is_Const(val) && is_Const_one(val)) {
332 ir_node *block = get_nodes_block(call);
333 ir_mode *mode = get_irn_mode(val);
334 ir_node *irn = new_r_Const(current_ir_graph, block, mode, get_mode_null(mode));
335 ir_node *mem = get_Call_mem(call);
336 replace_call(val, call, mem, NULL, NULL);
343 * A mapper for mapping f(0.0) to 0.0.
345 static int i_mapper_zero_to_zero(ir_node *call, void *ctx) {
346 ir_node *val = get_Call_param(call, 0);
349 if (is_Const(val) && is_Const_null(val)) {
351 ir_node *mem = get_Call_mem(call);
352 replace_call(val, call, mem, NULL, NULL);
359 * A mapper for mapping f(1.0) to 0.0.
361 static int i_mapper_one_to_zero(ir_node *call, void *ctx) {
362 ir_node *val = get_Call_param(call, 0);
365 if (is_Const(val) && is_Const_one(val)) {
366 /* acos(1.0) = 0.0 */
367 ir_node *block = get_nodes_block(call);
368 ir_mode *mode = get_irn_mode(val);
369 ir_node *irn = new_r_Const(current_ir_graph, block, mode, get_mode_null(mode));
370 ir_node *mem = get_Call_mem(call);
371 replace_call(irn, call, mem, NULL, NULL);
378 * A mapper for mapping a functions with the following characteristics:
382 static int i_mapper_symmetric_zero_to_one(ir_node *call, void *ctx) {
383 ir_node *val = get_Call_param(call, 0);
386 if (is_strictConv(val)) {
387 ir_node *op = get_Conv_op(val);
389 /* f(-x) = f(x) with strictConv */
390 ir_node *block = get_nodes_block(call);
391 ir_mode *mode = get_irn_mode(val);
392 dbg_info *dbg = get_irn_dbg_info(val);
394 op = get_Minus_op(op);
395 val = new_rd_Conv(dbg, current_ir_graph, block, op, mode);
398 set_Conv_strict(val, 1);
400 set_Call_param(call, 0, val);
402 } else if (is_Minus(val)) {
404 val = get_Minus_op(val);
405 set_Call_param(call, 0, val);
408 if (is_Const(val) && is_Const_null(val)) {
410 ir_node *block = get_nodes_block(call);
411 ir_mode *mode = get_irn_mode(val);
412 ir_node *irn = new_r_Const(current_ir_graph, block, mode, get_mode_one(mode));
413 ir_node *mem = get_Call_mem(call);
414 replace_call(irn, call, mem, NULL, NULL);
420 /* A mapper for the floating point sin. */
421 int i_mapper_sin(ir_node *call, void *ctx) {
423 return i_mapper_zero_to_zero(call, ctx);
426 /* A mapper for the floating point cos. */
427 int i_mapper_cos(ir_node *call, void *ctx) {
428 /* cos(0.0) = 1.0, cos(-x) = x */
429 return i_mapper_symmetric_zero_to_one(call, ctx);
432 /* A mapper for the floating point tan. */
433 int i_mapper_tan(ir_node *call, void *ctx) {
435 return i_mapper_zero_to_zero(call, ctx);
438 /* A mapper for the floating point asin. */
439 int i_mapper_asin(ir_node *call, void *ctx) {
440 /* asin(0.0) = 0.0 */
441 return i_mapper_zero_to_zero(call, ctx);
444 /* A mapper for the floating point acos. */
445 int i_mapper_acos(ir_node *call, void *ctx) {
446 /* acos(1.0) = 0.0 */
447 return i_mapper_one_to_zero(call, ctx);
450 /* A mapper for the floating point atan. */
451 int i_mapper_atan(ir_node *call, void *ctx) {
452 /* atan(0.0) = 0.0 */
453 return i_mapper_zero_to_zero(call, ctx);
456 /* A mapper for the floating point sinh. */
457 int i_mapper_sinh(ir_node *call, void *ctx) {
458 /* sinh(0.0) = 0.0 */
459 return i_mapper_zero_to_zero(call, ctx);
462 /* A mapper for the floating point cosh. */
463 int i_mapper_cosh(ir_node *call, void *ctx) {
464 /* cosh(0.0) = 1.0, cosh(-x) = x */
465 return i_mapper_symmetric_zero_to_one(call, ctx);
468 /* A mapper for the floating point tanh. */
469 int i_mapper_tanh(ir_node *call, void *ctx) {
470 /* tanh(0.0) = 0.0 */
471 return i_mapper_zero_to_zero(call, ctx);
474 /* A mapper for strcmp */
475 int i_mapper_strcmp(ir_node *call, void *ctx) {
476 ir_node *left = get_Call_param(call, 0);
477 ir_node *right = get_Call_param(call, 1);
482 /* a strcmp(s, s) ==> 0 */
483 ir_node *mem = get_Call_mem(call);
484 ir_node *adr = get_Call_ptr(call);
485 ir_entity *ent = get_SymConst_entity(adr);
486 ir_type *call_tp = get_entity_type(ent);
487 ir_type *res_tp = get_method_res_type(call_tp, 0);
488 ir_mode *mode = get_type_mode(res_tp);
489 ir_node *block = get_nodes_block(call);
491 irn = new_r_Const(current_ir_graph, block, mode, get_mode_null(mode));
492 replace_call(irn, call, mem, NULL, NULL);
498 /* A mapper for strncmp */
499 int i_mapper_strncmp(ir_node *call, void *ctx) {
500 ir_node *left = get_Call_param(call, 0);
501 ir_node *right = get_Call_param(call, 1);
502 ir_node *len = get_Call_param(call, 2);
506 if (left == right || (is_Const(len) && is_Const_null(len))) {
507 /* a strncmp(s, s, len) ==> 0 OR
508 a strncmp(a, b, 0) ==> 0 */
509 ir_node *mem = get_Call_mem(call);
510 ir_node *adr = get_Call_ptr(call);
511 ir_entity *ent = get_SymConst_entity(adr);
512 ir_type *call_tp = get_entity_type(ent);
513 ir_type *res_tp = get_method_res_type(call_tp, 0);
514 ir_mode *mode = get_type_mode(res_tp);
515 ir_node *block = get_nodes_block(call);
517 irn = new_r_Const(current_ir_graph, block, mode, get_mode_null(mode));
518 replace_call(irn, call, mem, NULL, NULL);
524 /* A mapper for memcpy */
525 int i_mapper_memcpy(ir_node *call, void *ctx) {
526 ir_node *len = get_Call_param(call, 2);
529 if (is_Const(len) && is_Const_null(len)) {
530 /* a memcpy(d, s, 0) ==> d */
531 ir_node *mem = get_Call_mem(call);
532 ir_node *dst = get_Call_param(call, 0);
534 replace_call(dst, call, mem, NULL, NULL);
540 /* A mapper for memset */
541 int i_mapper_memset(ir_node *call, void *ctx) {
542 ir_node *len = get_Call_param(call, 2);
545 if (is_Const(len) && is_Const_null(len)) {
546 /* a memset(d, C, 0) ==> d */
547 ir_node *mem = get_Call_mem(call);
548 ir_node *dst = get_Call_param(call, 0);
550 replace_call(dst, call, mem, NULL, NULL);
557 * Returns the result mode of a node.
559 static ir_mode *get_irn_res_mode(ir_node *node) {
560 switch (get_irn_opcode(node)) {
561 case iro_Load: return get_Load_mode(node);
562 case iro_Quot: return get_Quot_resmode(node);
563 case iro_Div: return get_Div_resmode(node);
564 case iro_Mod: return get_Mod_resmode(node);
565 case iro_DivMod: return get_DivMod_resmode(node);
566 default: return NULL;
570 #define LMAX(a, b) ((a) > (b) ? (a) : (b))
572 /* A mapper for mapping unsupported instructions to runtime calls. */
573 int i_mapper_RuntimeCall(ir_node *node, runtime_rt *rt) {
574 int i, j, arity, first, n_param, n_res;
577 ir_node *mem, *bl, *call, *addr, *res_proj;
581 ir_mode *mode = get_irn_mode(node);
583 if (mode != rt->mode)
585 /* check if the result modes match */
586 if (mode == mode_T && rt->res_mode != NULL) {
587 mode = get_irn_res_mode(node);
588 if (mode != rt->res_mode)
592 arity = get_irn_arity(node);
596 mtp = get_entity_type(rt->ent);
597 n_param = get_method_n_params(mtp);
598 irg = current_ir_graph;
600 mem = get_irn_n(node, 0);
601 if (get_irn_mode(mem) != mode_M) {
602 mem = new_r_NoMem(irg);
607 /* check if the modes of the predecessors match the parameter modes */
608 if (arity - first != n_param)
611 for (i = first, j = 0; i < arity; ++i, ++j) {
612 ir_type *param_tp = get_method_param_type(mtp, j);
613 ir_node *pred = get_irn_n(node, i);
615 if (get_type_mode(param_tp) != get_irn_mode(pred))
619 n_res = get_method_n_ress(mtp);
621 /* step 0: calculate the number of needed Proj's */
623 n_proj = LMAX(n_proj, rt->mem_proj_nr + 1);
624 n_proj = LMAX(n_proj, rt->regular_proj_nr + 1);
625 n_proj = LMAX(n_proj, rt->exc_proj_nr + 1);
626 n_proj = LMAX(n_proj, rt->exc_mem_proj_nr + 1);
627 n_proj = LMAX(n_proj, rt->res_proj_nr + 1);
630 if (rt->mode != mode_T) /* must be mode_T */
635 if (get_type_mode(get_method_res_type(mtp, 0)) != rt->mode)
639 /* ok, when we are here, the number of predecessors match as well as the parameter modes */
640 bl = get_nodes_block(node);
644 NEW_ARR_A(ir_node *, in, n_param);
645 for (i = 0; i < n_param; ++i)
646 in[i] = get_irn_n(node, first + i);
649 /* step 1: create the call */
650 sym.entity_p = rt->ent;
651 addr = new_r_SymConst(irg, bl, sym, symconst_addr_ent);
652 call = new_rd_Call(get_irn_dbg_info(node), irg, bl, mem, addr, n_param, in, mtp);
653 set_irn_pinned(call, get_irn_pinned(node));
656 res_proj = new_r_Proj(irg, bl, call, mode_T, pn_Call_T_result);
664 turn_into_tuple(node, n_proj);
666 for (i = 0; i < n_proj; ++i)
667 set_Tuple_pred(node, i, new_r_Bad(irg));
668 if (rt->mem_proj_nr >= 0)
669 set_Tuple_pred(node, rt->mem_proj_nr, new_r_Proj(irg, bl, call, mode_M, pn_Call_M_regular));
670 if (get_irn_op(mem) != op_NoMem) {
671 /* Exceptions can only be handled with real memory */
672 if (rt->regular_proj_nr >= 0)
673 set_Tuple_pred(node, rt->regular_proj_nr, new_r_Proj(irg, bl, call, mode_X, pn_Call_X_regular));
674 if (rt->exc_proj_nr >= 0)
675 set_Tuple_pred(node, rt->exc_proj_nr, new_r_Proj(irg, bl, call, mode_X, pn_Call_X_except));
676 if (rt->exc_mem_proj_nr >= 0)
677 set_Tuple_pred(node, rt->mem_proj_nr, new_r_Proj(irg, bl, call, mode_M, pn_Call_M_except));
680 if (rt->res_proj_nr >= 0)
681 for (i = 0; i < n_res; ++i)
682 set_Tuple_pred(node, rt->res_proj_nr + i,
683 new_r_Proj(irg, bl, res_proj, get_type_mode(get_method_res_type(mtp, i)), i));
686 /* only one return value supported */
688 ir_mode *mode = get_type_mode(get_method_res_type(mtp, 0));
690 res_proj = new_r_Proj(irg, bl, call, mode_T, pn_Call_T_result);
691 res_proj = new_r_Proj(irg, bl, res_proj, mode, 0);
693 exchange(node, res_proj);
697 /* should not happen */