2 * This file is part of libFirm.
3 * Copyright (C) 2012 University of Karlsruhe.
8 * @brief lowering of Calls of intrinsic functions
29 #include "iropt_dbg.h"
33 #include "firmstat_t.h"
35 /** Walker environment. */
36 typedef struct walker_env {
37 pmap *c_map; /**< The intrinsic call map. */
38 size_t 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)
47 walker_env_t *wenv = (walker_env_t*)env;
48 ir_op *op = get_irn_op(node);
52 const i_call_record *r;
55 symconst = get_Call_ptr(node);
56 if (! is_SymConst_addr_ent(symconst))
59 ent = get_SymConst_entity(symconst);
60 r = pmap_get(i_call_record const, wenv->c_map, ent);
63 wenv->nr_of_intrinsics += r->i_mapper(node, r->ctx) ? 1 : 0;
66 if (op->code < (unsigned) ARR_LEN(wenv->i_map)) {
67 const i_instr_record *r = wenv->i_map[op->code];
68 /* run all possible mapper */
70 if (r->i_mapper(node, r->ctx)) {
71 ++wenv->nr_of_intrinsics;
74 r = (const i_instr_record*)r->link;
80 size_t lower_intrinsics(i_record *list, size_t length, int part_block_used)
83 size_t n_ops = ir_get_n_opcodes();
85 pmap *c_map = pmap_create_ex(length);
86 i_instr_record **i_map;
87 size_t nr_of_intrinsics = 0;
90 /* we use the ir_op generic pointers here */
91 NEW_ARR_A(i_instr_record *, i_map, n_ops);
92 memset((void *)i_map, 0, sizeof(*i_map) * n_ops);
94 /* fill a map for faster search */
95 for (i = 0; i < length; ++i) {
96 if (list[i].i_call.kind == INTRINSIC_CALL) {
97 pmap_insert(c_map, list[i].i_call.i_ent, (void *)&list[i].i_call);
99 ir_op *op = list[i].i_instr.op;
100 assert(op->code < (unsigned) ARR_LEN(i_map));
102 list[i].i_instr.link = i_map[op->code];
103 i_map[op->code] = &list[i].i_instr;
110 for (i = 0, n = get_irp_n_irgs(); i < n; ++i) {
111 irg = get_irp_irg(i);
113 if (part_block_used) {
114 ir_reserve_resources(irg, IR_RESOURCE_IRN_LINK | IR_RESOURCE_PHI_LIST);
115 collect_phiprojs(irg);
118 wenv.nr_of_intrinsics = 0;
119 irg_walk_graph(irg, NULL, call_mapper, &wenv);
122 ir_free_resources(irg, IR_RESOURCE_IRN_LINK | IR_RESOURCE_PHI_LIST);
124 if (wenv.nr_of_intrinsics > 0) {
125 confirm_irg_properties(irg, IR_GRAPH_PROPERTIES_NONE);
126 nr_of_intrinsics += wenv.nr_of_intrinsics;
131 return nr_of_intrinsics;
134 typedef struct pass_t {
143 * Wrapper for running lower_intrinsics() as an ir_prog pass.
145 static int pass_wrapper(ir_prog *irp, void *context)
147 pass_t *pass = (pass_t*)context;
148 (void) irp; /* TODO: set current irp, or remove parameter */
149 lower_intrinsics(pass->list, pass->length, pass->part_block_used);
150 /* probably this pass should not run again */
155 * Creates an ir_prog pass for lower_intrinsics.
157 * @param name the name of this pass or NULL
158 * @param list an array of intrinsic map records
159 * @param length the length of the array
160 * @param part_block_used set to true if part_block() must be using during lowering
162 ir_prog_pass_t *lower_intrinsics_pass(
164 i_record *list, size_t length, int part_block_used)
166 pass_t *const pass = XMALLOCF(pass_t, list, length);
167 memcpy(pass->list, list, sizeof(list[0]) * length);
168 pass->length = length;
169 pass->part_block_used = part_block_used;
171 return def_prog_pass_constructor(
172 &pass->pass, name ? name : "lower_intrinsics", pass_wrapper);
176 * Helper function, replace the call by the given node.
178 * @param irn the result node
179 * @param call the call to replace
180 * @param mem the new mem result
181 * @param reg_jmp new regular control flow, if NULL, a Jmp will be used
182 * @param exc_jmp new exception control flow, if reg_jmp == NULL, a Bad will be used
184 static void replace_call(ir_node *irn, ir_node *call, ir_node *mem,
185 ir_node *reg_jmp, ir_node *exc_jmp)
187 ir_node *block = get_nodes_block(call);
188 ir_graph *irg = get_irn_irg(block);
189 ir_node *rest = new_r_Tuple(block, 1, &irn);
191 if (ir_throws_exception(call)) {
192 if (reg_jmp == NULL) {
193 reg_jmp = new_r_Jmp(block);
195 if (exc_jmp == NULL) {
196 exc_jmp = new_r_Bad(irg, mode_X);
198 ir_node *const in[] = {
200 [pn_Call_T_result] = rest,
201 [pn_Call_X_regular] = reg_jmp,
202 [pn_Call_X_except] = exc_jmp,
204 turn_into_tuple(call, ARRAY_SIZE(in), in);
206 assert(reg_jmp == NULL);
207 assert(exc_jmp == NULL);
208 assert(pn_Call_M <= pn_Call_T_result);
209 assert(pn_Call_X_regular > pn_Call_T_result);
210 assert(pn_Call_X_except > pn_Call_T_result);
211 ir_node *const in[] = {
213 [pn_Call_T_result] = rest,
215 turn_into_tuple(call, ARRAY_SIZE(in), in);
219 int i_mapper_abs(ir_node *call, void *ctx)
221 ir_node *mem = get_Call_mem(call);
222 ir_node *block = get_nodes_block(call);
223 ir_node *op = get_Call_param(call, 0);
224 ir_graph *irg = get_irn_irg(call);
225 ir_mode *mode = get_irn_mode(op);
226 dbg_info *dbg = get_irn_dbg_info(call);
227 ir_node *zero = new_r_Const(irg, get_mode_null(mode));
228 ir_node *cmp = new_rd_Cmp(dbg, block, op, zero, ir_relation_less);
229 ir_node *minus_op = new_rd_Minus(dbg, block, op, mode);
231 arch_allow_ifconv_func allow_ifconv = be_get_backend_param()->allow_ifconv;
234 /* mux allowed by backend? */
235 if (!allow_ifconv(cmp, op, minus_op))
239 mux = new_rd_Mux(dbg, block, cmp, op, minus_op, mode);
240 DBG_OPT_ALGSIM0(call, mux, FS_OPT_RTS_ABS);
241 replace_call(mux, call, mem, NULL, NULL);
245 int i_mapper_bswap(ir_node *call, void *ctx)
247 ir_node *mem = get_Call_mem(call);
248 ir_node *block = get_nodes_block(call);
249 ir_node *op = get_Call_param(call, 0);
250 ir_type *tp = get_Call_type(call);
251 dbg_info *dbg = get_irn_dbg_info(call);
255 ir_graph *const irg = get_Block_irg(block);
256 irn = new_rd_Builtin(dbg, block, get_irg_no_mem(irg), 1, &op, ir_bk_bswap, tp);
257 set_irn_pinned(irn, op_pin_state_floats);
258 irn = new_r_Proj(irn, get_irn_mode(op), pn_Builtin_max+1);
259 replace_call(irn, call, mem, NULL, NULL);
263 int i_mapper_alloca(ir_node *call, void *ctx)
265 ir_node *mem = get_Call_mem(call);
266 ir_node *block = get_nodes_block(call);
267 ir_node *op = get_Call_param(call, 0);
268 ir_node *irn, *exc, *no_exc;
269 dbg_info *dbg = get_irn_dbg_info(call);
272 if (mode_is_signed(get_irn_mode(op))) {
273 ir_mode *mode = get_irn_mode(op);
274 mode = find_unsigned_mode(mode);
276 panic("Cannot find unsigned mode for %M", mode);
278 op = new_rd_Conv(dbg, block, op, mode);
281 irn = new_rd_Alloc(dbg, block, mem, op, get_unknown_type(), stack_alloc);
282 mem = new_rd_Proj(dbg, irn, mode_M, pn_Alloc_M);
283 irn = new_rd_Proj(dbg, irn, get_modeP_data(), pn_Alloc_res);
284 if (ir_throws_exception(call)) {
285 no_exc = new_rd_Proj(dbg, irn, mode_X, pn_Alloc_X_regular);
286 exc = new_rd_Proj(dbg, irn, mode_X, pn_Alloc_X_except);
287 ir_set_throws_exception(irn, true);
293 DBG_OPT_ALGSIM0(call, irn, FS_OPT_RTS_ALLOCA);
294 replace_call(irn, call, mem, no_exc, exc);
298 int i_mapper_sqrt(ir_node *call, void *ctx)
302 ir_node *op = get_Call_param(call, 0);
308 tv = get_Const_tarval(op);
309 if (! tarval_is_null(tv) && !tarval_is_one(tv))
312 mem = get_Call_mem(call);
314 /* sqrt(0) = 0, sqrt(1) = 1 */
315 DBG_OPT_ALGSIM0(call, op, FS_OPT_RTS_SQRT);
316 replace_call(op, call, mem, NULL, NULL);
320 int i_mapper_cbrt(ir_node *call, void *ctx)
324 ir_node *op = get_Call_param(call, 0);
330 tv = get_Const_tarval(op);
331 if (! tarval_is_null(tv) && !tarval_is_one(tv) && !tarval_is_minus_one(tv))
334 mem = get_Call_mem(call);
336 /* cbrt(0) = 0, cbrt(1) = 1, cbrt(-1) = -1 */
337 DBG_OPT_ALGSIM0(call, op, FS_OPT_RTS_CBRT);
338 replace_call(op, call, mem, NULL, NULL);
342 int i_mapper_pow(ir_node *call, void *ctx)
344 ir_node *left = get_Call_param(call, 0);
345 ir_node *right = get_Call_param(call, 1);
346 ir_node *block = get_nodes_block(call);
347 ir_graph *irg = get_irn_irg(block);
348 ir_node *reg_jmp = NULL;
349 ir_node *exc_jmp = NULL;
355 if (is_Const(left) && is_Const_one(left)) {
356 /* pow (1.0, x) = 1.0 */
358 } else if (is_Const(right)) {
359 ir_tarval *tv = get_Const_tarval(right);
360 if (tarval_is_null(tv)) {
361 /* pow(x, 0.0) = 1.0 */
362 ir_mode *mode = get_tarval_mode(tv);
363 irn = new_r_Const(irg, get_mode_one(mode));
364 } else if (tarval_is_one(tv)) {
365 /* pow(x, 1.0) = x */
367 } else if (tarval_is_minus_one(tv)) {
368 /* pow(x, -1.0) = 1/x */
376 mem = get_Call_mem(call);
377 dbg = get_irn_dbg_info(call);
380 ir_mode *result_mode = get_irn_mode(left);
383 ir_mode *mode = result_mode;
384 ir_mode *float_arithmetic = be_get_backend_param()->mode_float_arithmetic;
385 if (float_arithmetic != NULL) {
386 left = new_r_Conv(block, left, float_arithmetic);
387 mode = float_arithmetic;
390 irn = new_r_Const(irg, get_mode_one(mode));
391 div = new_rd_Div(dbg, block, mem, irn, left, mode, op_pin_state_pinned);
392 mem = new_r_Proj(div, mode_M, pn_Div_M);
393 irn = new_r_Proj(div, mode, pn_Div_res);
394 if (ir_throws_exception(call)) {
395 reg_jmp = new_r_Proj(div, mode_X, pn_Div_X_regular);
396 exc_jmp = new_r_Proj(div, mode_X, pn_Div_X_except);
397 ir_set_throws_exception(div, true);
399 if (result_mode != mode) {
400 irn = new_r_Conv(block, irn, result_mode);
403 DBG_OPT_ALGSIM0(call, irn, FS_OPT_RTS_POW);
404 replace_call(irn, call, mem, reg_jmp, exc_jmp);
408 int i_mapper_exp(ir_node *call, void *ctx)
410 ir_node *val = get_Call_param(call, 0);
413 if (is_Const(val) && is_Const_null(val)) {
415 ir_graph *irg = get_irn_irg(val);
416 ir_mode *mode = get_irn_mode(val);
417 ir_node *irn = new_r_Const(irg, get_mode_one(mode));
418 ir_node *mem = get_Call_mem(call);
419 DBG_OPT_ALGSIM0(call, irn, FS_OPT_RTS_EXP);
420 replace_call(irn, call, mem, NULL, NULL);
426 int i_mapper_exp2(ir_node *call, void *ctx)
428 return i_mapper_exp(call, ctx);
431 int i_mapper_exp10(ir_node *call, void *ctx)
433 return i_mapper_exp(call, ctx);
437 * A mapper for mapping f(0.0) to 0.0.
439 static int i_mapper_zero_to_zero(ir_node *call, void *ctx, int reason)
441 ir_node *val = get_Call_param(call, 0);
444 if (is_Const(val) && is_Const_null(val)) {
446 ir_node *mem = get_Call_mem(call);
447 DBG_OPT_ALGSIM0(call, val, reason);
448 replace_call(val, call, mem, NULL, NULL);
455 * A mapper for mapping f(1.0) to 0.0.
457 static int i_mapper_one_to_zero(ir_node *call, void *ctx, int reason)
459 ir_node *val = get_Call_param(call, 0);
462 if (is_Const(val) && is_Const_one(val)) {
463 /* acos(1.0) = 0.0 */
464 ir_graph *irg = get_irn_irg(val);
465 ir_mode *mode = get_irn_mode(val);
466 ir_node *irn = new_r_Const(irg, get_mode_null(mode));
467 ir_node *mem = get_Call_mem(call);
468 DBG_OPT_ALGSIM0(call, irn, reason);
469 replace_call(irn, call, mem, NULL, NULL);
476 * A mapper for mapping a functions with the following characteristics:
480 static int i_mapper_symmetric_zero_to_one(ir_node *call, void *ctx, int reason)
483 ir_node *val = get_Call_param(call, 0);
487 ir_node *op = get_Conv_op(val);
489 /* f(-x) = f(x) with strictConv */
490 ir_node *block = get_nodes_block(call);
491 ir_mode *mode = get_irn_mode(val);
492 dbg_info *dbg = get_irn_dbg_info(val);
494 op = get_Minus_op(op);
495 val = new_rd_Conv(dbg, block, op, mode);
496 DBG_OPT_ALGSIM2(call, op, call, FS_OPT_RTS_SYMMETRIC);
497 set_Call_param(call, 0, val);
500 } else if (is_Minus(val)) {
502 val = get_Minus_op(val);
503 DBG_OPT_ALGSIM2(call, val, call, FS_OPT_RTS_SYMMETRIC);
504 set_Call_param(call, 0, val);
508 if (is_Const(val) && is_Const_null(val)) {
510 ir_graph *irg = get_irn_irg(val);
511 ir_mode *mode = get_irn_mode(val);
512 ir_node *irn = new_r_Const(irg, get_mode_one(mode));
513 ir_node *mem = get_Call_mem(call);
514 DBG_OPT_ALGSIM0(call, irn, reason);
515 replace_call(irn, call, mem, NULL, NULL);
521 int i_mapper_log(ir_node *call, void *ctx)
524 return i_mapper_one_to_zero(call, ctx, FS_OPT_RTS_LOG);
527 int i_mapper_log2(ir_node *call, void *ctx)
529 /* log2(1.0) = 0.0 */
530 return i_mapper_one_to_zero(call, ctx, FS_OPT_RTS_LOG);
533 int i_mapper_log10(ir_node *call, void *ctx)
535 /* log10(1.0) = 0.0 */
536 return i_mapper_one_to_zero(call, ctx, FS_OPT_RTS_LOG);
539 int i_mapper_sin(ir_node *call, void *ctx)
542 return i_mapper_zero_to_zero(call, ctx, FS_OPT_RTS_SIN);
545 int i_mapper_cos(ir_node *call, void *ctx)
547 /* cos(0.0) = 1.0, cos(-x) = x */
548 return i_mapper_symmetric_zero_to_one(call, ctx, FS_OPT_RTS_COS);
551 int i_mapper_tan(ir_node *call, void *ctx)
554 return i_mapper_zero_to_zero(call, ctx, FS_OPT_RTS_TAN);
557 int i_mapper_asin(ir_node *call, void *ctx)
559 /* asin(0.0) = 0.0 */
560 return i_mapper_zero_to_zero(call, ctx, FS_OPT_RTS_ASIN);
563 int i_mapper_acos(ir_node *call, void *ctx)
565 /* acos(1.0) = 0.0 */
566 return i_mapper_one_to_zero(call, ctx, FS_OPT_RTS_ACOS);
569 int i_mapper_atan(ir_node *call, void *ctx)
571 /* atan(0.0) = 0.0 */
572 return i_mapper_zero_to_zero(call, ctx, FS_OPT_RTS_ATAN);
575 int i_mapper_sinh(ir_node *call, void *ctx)
577 /* sinh(0.0) = 0.0 */
578 return i_mapper_zero_to_zero(call, ctx, FS_OPT_RTS_SINH);
581 int i_mapper_cosh(ir_node *call, void *ctx)
583 /* cosh(0.0) = 1.0, cosh(-x) = x */
584 return i_mapper_symmetric_zero_to_one(call, ctx, FS_OPT_RTS_COSH);
587 int i_mapper_tanh(ir_node *call, void *ctx)
589 /* tanh(0.0) = 0.0 */
590 return i_mapper_zero_to_zero(call, ctx, FS_OPT_RTS_TANH);
594 * Return the const entity that is accessed through the pointer ptr or
595 * NULL if there is no entity (or the entity is not constant).
597 * @param ptr the pointer
599 static ir_entity *get_const_entity(ir_node *ptr)
601 if (is_SymConst_addr_ent(ptr)) {
602 ir_entity *ent = get_SymConst_entity(ptr);
604 if (get_entity_linkage(ent) & IR_LINKAGE_CONSTANT) {
605 /* a constant entity */
612 static ir_tarval *get_initializer_value(ir_initializer_t *const init, ir_mode *const mode)
614 switch (get_initializer_kind(init)) {
615 case IR_INITIALIZER_NULL:
616 return get_mode_null(mode);
618 case IR_INITIALIZER_TARVAL:
619 return get_initializer_tarval_value(init);
621 case IR_INITIALIZER_CONST: {
622 ir_node *const irn = get_initializer_const_value(init);
624 return get_Const_tarval(irn);
628 case IR_INITIALIZER_COMPOUND:
632 return get_tarval_undefined();
635 static bool initializer_val_is_null(ir_initializer_t *init)
639 if (get_initializer_kind(init) == IR_INITIALIZER_NULL)
642 if (get_initializer_kind(init) == IR_INITIALIZER_TARVAL) {
643 tv = get_initializer_tarval_value(init);
644 } else if (get_initializer_kind(init) == IR_INITIALIZER_CONST) {
645 ir_node *irn = get_initializer_const_value(init);
648 tv = get_Const_tarval(irn);
653 return tarval_is_null(tv);
657 * Calculate the value of strlen if possible.
659 * @param ent the entity
660 * @param res_tp the result type
662 * @return a Const node containing the strlen() result or NULL
663 * if the evaluation fails
665 static ir_node *eval_strlen(ir_graph *irg, ir_entity *ent, ir_type *res_tp)
667 ir_type *tp = get_entity_type(ent);
669 ir_initializer_t *initializer;
673 if (! is_Array_type(tp))
675 tp = get_array_element_type(tp);
676 if (! is_Primitive_type(tp))
678 mode = get_type_mode(tp);
680 /* FIXME: This is too restrict, as the type char might be more the 8bits */
681 if (!mode_is_int(mode) || get_mode_size_bits(mode) != 8)
684 initializer = get_entity_initializer(ent);
685 if (get_initializer_kind(initializer) != IR_INITIALIZER_COMPOUND)
688 size = get_initializer_compound_n_entries(initializer);
689 for (i = 0; i < size; ++i) {
690 ir_initializer_t *val = get_initializer_compound_value(initializer, i);
691 if (initializer_val_is_null(val)) {
692 ir_tarval *tv = new_tarval_from_long(i, get_type_mode(res_tp));
693 return new_r_Const(irg, tv);
700 int i_mapper_strlen(ir_node *call, void *ctx)
702 ir_node *s = get_Call_param(call, 0);
703 ir_entity *ent = get_const_entity(s);
707 /* FIXME: this cannot handle constant strings inside struct initializers yet */
709 /* a constant entity */
710 ir_type *tp = get_Call_type(call);
713 tp = get_method_res_type(tp, 0);
714 irn = eval_strlen(get_irn_irg(call), ent, tp);
717 ir_node *mem = get_Call_mem(call);
718 DBG_OPT_ALGSIM0(call, irn, FS_OPT_RTS_STRLEN);
719 replace_call(irn, call, mem, NULL, NULL);
727 * Calculate the value of strlen if possible.
729 * @param left the left entity
730 * @param right the right entity
731 * @param res_tp the result type
733 * @return a Const node containing the strcmp() result or NULL
734 * if the evaluation fails
736 static ir_node *eval_strcmp(ir_graph *irg, ir_entity *left, ir_entity *right,
741 ir_initializer_t *init_l;
742 ir_initializer_t *init_r;
748 tp = get_entity_type(left);
749 if (! is_Array_type(tp))
751 tp = get_array_element_type(tp);
752 if (! is_Primitive_type(tp))
754 mode = get_type_mode(tp);
756 /* FIXME: This is too restrict, as the type char might be more the 8bits */
757 if (!mode_is_int(mode) || get_mode_size_bits(mode) != 8)
760 tp = get_entity_type(right);
761 if (! is_Array_type(tp))
763 tp = get_array_element_type(tp);
764 if (! is_Primitive_type(tp))
766 mode = get_type_mode(tp);
768 /* FIXME: This is too restrict, as the type char might be more the 8bits */
769 if (!mode_is_int(mode) || get_mode_size_bits(mode) != 8)
772 init_l = get_entity_initializer(left);
773 init_r = get_entity_initializer(right);
774 if (get_initializer_kind(init_l) != IR_INITIALIZER_COMPOUND ||
775 get_initializer_kind(init_r) != IR_INITIALIZER_COMPOUND)
778 size_l = get_initializer_compound_n_entries(init_l);
779 size_r = get_initializer_compound_n_entries(init_r);
780 size = size_l < size_r ? size_l : size_r;
782 for (i = 0; i != size; ++i) {
783 ir_initializer_t *const val_l = get_initializer_compound_value(init_l, i);
784 ir_tarval *const tv_l = get_initializer_value(val_l, mode);
785 ir_initializer_t *const val_r = get_initializer_compound_value(init_r, i);
786 ir_tarval *const tv_r = get_initializer_value(val_r, mode);
788 if (!tarval_is_constant(tv_l) || !tarval_is_constant(tv_r))
792 ir_mode *const res_mode = get_type_mode(res_tp);
793 ir_tarval *const res_l = tarval_convert_to(tv_l, res_mode);
794 ir_tarval *const res_r = tarval_convert_to(tv_r, res_mode);
795 ir_tarval *const tv = tarval_sub(res_l, res_r, res_mode);
796 return new_r_Const(irg, tv);
799 if (tarval_is_null(tv_l)) {
800 ir_tarval *const tv = get_mode_null(get_type_mode(res_tp));
801 return new_r_Const(irg, tv);
809 * Checks if an entity represents the empty string.
811 * @param ent the entity
813 * @return non-zero if ent represents the empty string
815 static int is_empty_string(ir_entity *ent)
817 ir_type *tp = get_entity_type(ent);
819 ir_initializer_t *initializer;
820 ir_initializer_t *init0;
822 if (! is_Array_type(tp))
824 tp = get_array_element_type(tp);
825 if (! is_Primitive_type(tp))
827 mode = get_type_mode(tp);
829 /* FIXME: This is too restrict, as the type char might be more the 8bits */
830 if (!mode_is_int(mode) || get_mode_size_bits(mode) != 8)
833 initializer = get_entity_initializer(ent);
834 if (get_initializer_kind(initializer) != IR_INITIALIZER_COMPOUND)
837 if (get_initializer_compound_n_entries(initializer) < 1)
840 init0 = get_initializer_compound_value(initializer, 0);
841 return initializer_val_is_null(init0);
844 int i_mapper_strcmp(ir_node *call, void *ctx)
846 ir_node *left = get_Call_param(call, 0);
847 ir_node *right = get_Call_param(call, 1);
851 ir_type *call_tp = get_Call_type(call);
852 ir_type *res_tp = get_method_res_type(call_tp, 0);
853 ir_entity *ent_l, *ent_r;
859 /* do some type checks first */
860 if (! is_Primitive_type(res_tp))
862 char_tp = get_method_param_type(call_tp, 0);
863 if (char_tp != get_method_param_type(call_tp, 1))
865 if (! is_Pointer_type(char_tp))
867 char_tp = get_pointer_points_to_type(char_tp);
869 ir_node *mem = get_Call_mem(call);
871 /* a strcmp(s, s) ==> 0 */
872 ir_graph *irg = get_irn_irg(call);
873 ir_mode *mode = get_type_mode(res_tp);
875 irn = new_r_Const(irg, get_mode_null(mode));
876 DBG_OPT_ALGSIM0(call, irn, FS_OPT_RTS_STRCMP);
877 replace_call(irn, call, mem, NULL, NULL);
880 ent_l = get_const_entity(left);
881 ent_r = get_const_entity(right);
883 if (ent_l != NULL && ent_r != NULL) {
884 /* both entities are const, try to evaluate */
885 irn = eval_strcmp(get_irn_irg(call), ent_l, ent_r, res_tp);
886 } else if (ent_l != NULL) {
887 if (is_empty_string(ent_l)) {
888 /* s strcmp("", s) ==> -(*s)*/
890 goto replace_by_call;
892 } else if (ent_r != NULL) {
893 if (is_empty_string(ent_r)) {
894 /* s strcmp(s, "") ==> (*s) */
901 mem = get_Call_mem(call);
902 block = get_nodes_block(call);
903 dbg = get_irn_dbg_info(call);
904 mode = get_type_mode(char_tp);
906 /* replace the strcmp by (*x) */
907 irn = new_rd_Load(dbg, block, mem, v, mode, cons_none);
908 mem = new_r_Proj(irn, mode_M, pn_Load_M);
909 irn = new_r_Proj(irn, mode, pn_Load_res);
910 if (ir_throws_exception(call)) {
911 exc = new_r_Proj(irn, mode_X, pn_Load_X_except);
912 reg = new_r_Proj(irn, mode_X, pn_Load_X_regular);
913 ir_set_throws_exception(irn, true);
919 /* conv to the result mode */
920 mode = get_type_mode(res_tp);
921 irn = new_rd_Conv(dbg, block, irn, mode);
924 /* negate in the ("", s) case */
925 irn = new_rd_Minus(dbg, block, irn, mode);
931 DBG_OPT_ALGSIM0(call, irn, FS_OPT_RTS_STRCMP);
932 replace_call(irn, call, mem, reg, exc);
939 int i_mapper_strncmp(ir_node *call, void *ctx)
941 ir_node *left = get_Call_param(call, 0);
942 ir_node *right = get_Call_param(call, 1);
943 ir_node *len = get_Call_param(call, 2);
947 if (left == right || (is_Const(len) && is_Const_null(len))) {
948 /* a strncmp(s, s, len) ==> 0 OR
949 a strncmp(a, b, 0) ==> 0 */
950 ir_graph *irg = get_irn_irg(call);
951 ir_node *mem = get_Call_mem(call);
952 ir_node *adr = get_Call_ptr(call);
953 ir_entity *ent = get_SymConst_entity(adr);
954 ir_type *call_tp = get_entity_type(ent);
955 ir_type *res_tp = get_method_res_type(call_tp, 0);
956 ir_mode *mode = get_type_mode(res_tp);
958 irn = new_r_Const(irg, get_mode_null(mode));
959 DBG_OPT_ALGSIM0(call, irn, FS_OPT_RTS_STRNCMP);
960 replace_call(irn, call, mem, NULL, NULL);
966 int i_mapper_strcpy(ir_node *call, void *ctx)
968 ir_node *dst = get_Call_param(call, 0);
969 ir_node *src = get_Call_param(call, 1);
973 /* a strcpy(d, s) ==> d */
974 ir_node *mem = get_Call_mem(call);
975 ir_node *dst = get_Call_param(call, 0);
977 DBG_OPT_ALGSIM0(call, dst, FS_OPT_RTS_STRCPY);
978 replace_call(dst, call, mem, NULL, NULL);
984 int i_mapper_memcpy(ir_node *call, void *ctx)
986 ir_node *dst = get_Call_param(call, 0);
987 ir_node *src = get_Call_param(call, 1);
988 ir_node *len = get_Call_param(call, 2);
991 if (dst == src || (is_Const(len) && is_Const_null(len))) {
992 /* a memcpy(d, d, len) ==> d OR
993 a memcpy(d, s, 0) ==> d */
994 ir_node *mem = get_Call_mem(call);
996 DBG_OPT_ALGSIM0(call, dst, FS_OPT_RTS_MEMCPY);
997 replace_call(dst, call, mem, NULL, NULL);
1003 int i_mapper_mempcpy(ir_node *call, void *ctx)
1005 ir_node *dst = get_Call_param(call, 0);
1006 ir_node *src = get_Call_param(call, 1);
1007 ir_node *len = get_Call_param(call, 2);
1010 if (dst == src || (is_Const(len) && is_Const_null(len))) {
1011 /* a memcpy(d, d, len) ==> d + len OR
1012 a memcpy(d, s, 0) ==> d + 0 */
1013 dbg_info *dbg = get_irn_dbg_info(call);
1014 ir_node *mem = get_Call_mem(call);
1015 ir_node *blk = get_nodes_block(call);
1016 ir_mode *mode = get_irn_mode(dst);
1017 ir_node *res = new_rd_Add(dbg, blk, dst, len, mode);
1019 DBG_OPT_ALGSIM0(call, res, FS_OPT_RTS_MEMPCPY);
1020 replace_call(res, call, mem, NULL, NULL);
1026 int i_mapper_memmove(ir_node *call, void *ctx)
1028 ir_node *dst = get_Call_param(call, 0);
1029 ir_node *src = get_Call_param(call, 1);
1030 ir_node *len = get_Call_param(call, 2);
1033 if (dst == src || (is_Const(len) && is_Const_null(len))) {
1034 /* a memmove(d, d, len) ==> d OR
1035 a memmove(d, s, 0) ==> d */
1036 ir_node *mem = get_Call_mem(call);
1038 DBG_OPT_ALGSIM0(call, dst, FS_OPT_RTS_MEMMOVE);
1039 replace_call(dst, call, mem, NULL, NULL);
1045 int i_mapper_memset(ir_node *call, void *ctx)
1047 ir_node *len = get_Call_param(call, 2);
1050 if (is_Const(len) && is_Const_null(len)) {
1051 /* a memset(d, C, 0) ==> d */
1052 ir_node *mem = get_Call_mem(call);
1053 ir_node *dst = get_Call_param(call, 0);
1055 DBG_OPT_ALGSIM0(call, dst, FS_OPT_RTS_MEMSET);
1056 replace_call(dst, call, mem, NULL, NULL);
1062 int i_mapper_memcmp(ir_node *call, void *ctx)
1064 ir_node *left = get_Call_param(call, 0);
1065 ir_node *right = get_Call_param(call, 1);
1066 ir_node *len = get_Call_param(call, 2);
1070 if (left == right || (is_Const(len) && is_Const_null(len))) {
1071 /* a memcmp(s, s, len) ==> 0 OR
1072 a memcmp(a, b, 0) ==> 0 */
1073 ir_graph *irg = get_irn_irg(call);
1074 ir_node *mem = get_Call_mem(call);
1075 ir_node *adr = get_Call_ptr(call);
1076 ir_entity *ent = get_SymConst_entity(adr);
1077 ir_type *call_tp = get_entity_type(ent);
1078 ir_type *res_tp = get_method_res_type(call_tp, 0);
1079 ir_mode *mode = get_type_mode(res_tp);
1081 irn = new_r_Const(irg, get_mode_null(mode));
1082 DBG_OPT_ALGSIM0(call, irn, FS_OPT_RTS_STRNCMP);
1083 replace_call(irn, call, mem, NULL, NULL);
1090 * Returns the result mode of a node.
1092 static ir_mode *get_irn_res_mode(ir_node *node)
1094 switch (get_irn_opcode(node)) {
1095 case iro_Load: return get_Load_mode(node);
1096 case iro_Div: return get_Div_resmode(node);
1097 case iro_Mod: return get_Mod_resmode(node);
1098 default: return NULL;
1102 int i_mapper_RuntimeCall(ir_node *node, runtime_rt *rt)
1104 int i, j, arity, first, n_param, n_res;
1107 ir_node *mem, *call, *addr, *res_proj;
1109 bool throws_exception;
1111 symconst_symbol sym;
1112 ir_mode *mode = get_irn_mode(node);
1114 if (mode != rt->mode)
1116 /* check if the result modes match */
1117 if (mode == mode_T && rt->res_mode != NULL) {
1118 mode = get_irn_res_mode(node);
1119 if (mode != rt->res_mode)
1123 arity = get_irn_arity(node);
1127 mtp = get_entity_type(rt->ent);
1128 n_param = get_method_n_params(mtp);
1129 ir_node *const bl = get_nodes_block(node);
1130 ir_graph *const irg = get_Block_irg(bl);
1132 mem = get_irn_n(node, 0);
1133 if (get_irn_mode(mem) != mode_M) {
1134 mem = new_r_NoMem(irg);
1139 /* check if the modes of the predecessors match the parameter modes */
1140 if (arity - first != n_param)
1143 for (i = first, j = 0; i < arity; ++i, ++j) {
1144 ir_type *param_tp = get_method_param_type(mtp, j);
1145 ir_node *pred = get_irn_n(node, i);
1147 if (get_type_mode(param_tp) != get_irn_mode(pred))
1151 n_res = get_method_n_ress(mtp);
1152 throws_exception = ir_throws_exception(node);
1154 /* step 0: calculate the number of needed Proj's */
1156 n_proj = MAX(n_proj, rt->mem_proj_nr + 1);
1157 n_proj = MAX(n_proj, rt->res_proj_nr + 1);
1158 if (throws_exception) {
1159 n_proj = MAX(n_proj, rt->regular_proj_nr + 1);
1160 n_proj = MAX(n_proj, rt->exc_proj_nr + 1);
1164 if (rt->mode != mode_T) /* must be mode_T */
1169 if (get_type_mode(get_method_res_type(mtp, 0)) != rt->mode)
1173 /* ok, when we are here, the number of predecessors match as well as the parameter modes */
1174 op = get_irn_op(node);
1178 NEW_ARR_A(ir_node *, in, n_param);
1179 for (i = 0; i < n_param; ++i)
1180 in[i] = get_irn_n(node, first + i);
1183 /* step 1: create the call */
1184 sym.entity_p = rt->ent;
1185 addr = new_r_SymConst(irg, mode_P_code, sym, symconst_addr_ent);
1186 call = new_rd_Call(get_irn_dbg_info(node), bl, mem, addr, n_param, in, mtp);
1187 set_irn_pinned(call, get_irn_pinned(node));
1190 res_proj = new_r_Proj(call, mode_T, pn_Call_T_result);
1195 n_proj += n_res - 1;
1197 ir_node **const in = ALLOCAN(ir_node*, n_proj);
1198 ir_node *const bad = new_r_Bad(irg, mode_ANY);
1199 for (i = 0; i != n_proj; ++i) {
1203 if (rt->mem_proj_nr >= 0)
1204 in[rt->mem_proj_nr] = new_r_Proj(call, mode_M, pn_Call_M);
1205 if (throws_exception) {
1206 in[op->pn_x_regular] = new_r_Proj(call, mode_X, pn_Call_X_regular);
1207 in[op->pn_x_except] = new_r_Proj(call, mode_X, pn_Call_X_except);
1210 if (rt->res_proj_nr >= 0) {
1211 for (i = 0; i < n_res; ++i) {
1212 ir_mode *mode = get_type_mode(get_method_res_type(mtp, i));
1213 ir_node *proj = new_r_Proj(res_proj, mode, i);
1214 in[rt->res_proj_nr + i] = proj;
1218 turn_into_tuple(node, n_proj, in);
1221 /* only one return value supported */
1223 ir_mode *mode = get_type_mode(get_method_res_type(mtp, 0));
1225 res_proj = new_r_Proj(call, mode_T, pn_Call_T_result);
1226 res_proj = new_r_Proj(res_proj, mode, 0);
1228 exchange(node, res_proj);
1232 /* should not happen */