beifg: Simplify the quite complicated way to divide a number by 2 in be_ifg_stat().
[libfirm] / ir / lower / lower_intrinsics.c
1 /*
2  * This file is part of libFirm.
3  * Copyright (C) 2012 University of Karlsruhe.
4  */
5
6 /**
7  * @file
8  * @brief   lowering of Calls of intrinsic functions
9  * @author  Michael Beck
10  */
11 #include "config.h"
12
13 #include <stdbool.h>
14
15 #include "lowering.h"
16 #include "irop_t.h"
17 #include "irprog_t.h"
18 #include "irnode_t.h"
19 #include "irprog_t.h"
20 #include "irgwalk.h"
21 #include "ircons.h"
22 #include "irgmod.h"
23 #include "irgopt.h"
24 #include "trouts.h"
25 #include "irverify.h"
26 #include "pmap.h"
27 #include "array_t.h"
28 #include "irpass_t.h"
29 #include "iropt_dbg.h"
30 #include "error.h"
31 #include "be.h"
32 #include "util.h"
33 #include "firmstat_t.h"
34
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. */
40 } walker_env_t;
41
42 /**
43  * walker: call all mapper functions
44  */
45 static void call_mapper(ir_node *node, void *env)
46 {
47         walker_env_t *wenv = (walker_env_t*)env;
48         ir_op *op = get_irn_op(node);
49
50         if (op == op_Call) {
51                 ir_node *symconst;
52                 const i_call_record *r;
53                 ir_entity *ent;
54
55                 symconst = get_Call_ptr(node);
56                 if (! is_SymConst_addr_ent(symconst))
57                         return;
58
59                 ent = get_SymConst_entity(symconst);
60                 r   = pmap_get(i_call_record const, wenv->c_map, ent);
61
62                 if (r != NULL) {
63                         wenv->nr_of_intrinsics += r->i_mapper(node, r->ctx) ? 1 : 0;
64                 }
65         } else {
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 */
69                         while (r) {
70                                 if (r->i_mapper(node, r->ctx)) {
71                                         ++wenv->nr_of_intrinsics;
72                                         break;
73                                 }
74                                 r = (const i_instr_record*)r->link;
75                         }
76                 }
77         }
78 }
79
80 size_t lower_intrinsics(i_record *list, size_t length, int part_block_used)
81 {
82         size_t         i, n;
83         size_t         n_ops = ir_get_n_opcodes();
84         ir_graph       *irg;
85         pmap           *c_map = pmap_create_ex(length);
86         i_instr_record **i_map;
87         size_t         nr_of_intrinsics = 0;
88         walker_env_t   wenv;
89
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);
93
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);
98                 } else {
99                         ir_op *op = list[i].i_instr.op;
100                         assert(op->code < (unsigned) ARR_LEN(i_map));
101
102                         list[i].i_instr.link = i_map[op->code];
103                         i_map[op->code] = &list[i].i_instr;
104                 }
105         }
106
107         wenv.c_map = c_map;
108         wenv.i_map = i_map;
109
110         for (i = 0, n = get_irp_n_irgs(); i < n; ++i) {
111                 irg = get_irp_irg(i);
112
113                 if (part_block_used) {
114                         ir_reserve_resources(irg, IR_RESOURCE_IRN_LINK | IR_RESOURCE_PHI_LIST);
115                         collect_phiprojs(irg);
116                 }
117
118                 wenv.nr_of_intrinsics = 0;
119                 irg_walk_graph(irg, NULL, call_mapper, &wenv);
120
121                 if (part_block_used)
122                         ir_free_resources(irg, IR_RESOURCE_IRN_LINK | IR_RESOURCE_PHI_LIST);
123
124                 if (wenv.nr_of_intrinsics > 0) {
125                         confirm_irg_properties(irg, IR_GRAPH_PROPERTIES_NONE);
126                         nr_of_intrinsics += wenv.nr_of_intrinsics;
127                 }
128         }
129         pmap_destroy(c_map);
130
131         return nr_of_intrinsics;
132 }
133
134 typedef struct pass_t {
135         ir_prog_pass_t pass;
136
137         int      part_block_used;
138         size_t   length;
139         i_record list[1];
140 } pass_t;
141
142 /**
143  * Wrapper for running lower_intrinsics() as an ir_prog pass.
144  */
145 static int pass_wrapper(ir_prog *irp, void *context)
146 {
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 */
151         return 0;
152 }
153
154 /**
155  * Creates an ir_prog pass for lower_intrinsics.
156  *
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
161  */
162 ir_prog_pass_t *lower_intrinsics_pass(
163         const char *name,
164         i_record *list, size_t length, int part_block_used)
165 {
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;
170
171         return def_prog_pass_constructor(
172                 &pass->pass, name ? name : "lower_intrinsics", pass_wrapper);
173 }
174
175 /**
176  * Helper function, replace the call by the given node.
177  *
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
183  */
184 static void replace_call(ir_node *irn, ir_node *call, ir_node *mem,
185                          ir_node *reg_jmp, ir_node *exc_jmp)
186 {
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);
190
191         if (ir_throws_exception(call)) {
192                 if (reg_jmp == NULL) {
193                         reg_jmp = new_r_Jmp(block);
194                 }
195                 if (exc_jmp == NULL) {
196                         exc_jmp = new_r_Bad(irg, mode_X);
197                 }
198                 ir_node *const in[] = {
199                         [pn_Call_M]         = mem,
200                         [pn_Call_T_result]  = rest,
201                         [pn_Call_X_regular] = reg_jmp,
202                         [pn_Call_X_except]  = exc_jmp,
203                 };
204                 turn_into_tuple(call, ARRAY_SIZE(in), in);
205         } else {
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[] = {
212                         [pn_Call_M]         = mem,
213                         [pn_Call_T_result]  = rest,
214                 };
215                 turn_into_tuple(call, ARRAY_SIZE(in), in);
216         }
217 }
218
219 int i_mapper_abs(ir_node *call, void *ctx)
220 {
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);
230         ir_node  *mux;
231         arch_allow_ifconv_func allow_ifconv = be_get_backend_param()->allow_ifconv;
232         (void) ctx;
233
234         /* mux allowed by backend? */
235         if (!allow_ifconv(cmp, op, minus_op))
236                 return 0;
237
238         /* construct Mux */
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);
242         return 1;
243 }
244
245 int i_mapper_bswap(ir_node *call, void *ctx)
246 {
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);
252         ir_node *irn;
253         (void) ctx;
254
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);
260         return 1;
261 }
262
263 int i_mapper_alloca(ir_node *call, void *ctx)
264 {
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);
270         (void) ctx;
271
272         if (mode_is_signed(get_irn_mode(op))) {
273                 ir_mode *mode = get_irn_mode(op);
274                 mode = find_unsigned_mode(mode);
275                 if (mode == NULL) {
276                         panic("Cannot find unsigned mode for %M", mode);
277                 }
278                 op = new_rd_Conv(dbg, block, op, mode);
279         }
280
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);
288         } else {
289                 no_exc = NULL;
290                 exc    = NULL;
291         }
292
293         DBG_OPT_ALGSIM0(call, irn, FS_OPT_RTS_ALLOCA);
294         replace_call(irn, call, mem, no_exc, exc);
295         return 1;
296 }
297
298 int i_mapper_sqrt(ir_node *call, void *ctx)
299 {
300         ir_node   *mem;
301         ir_tarval *tv;
302         ir_node   *op = get_Call_param(call, 0);
303         (void) ctx;
304
305         if (!is_Const(op))
306                 return 0;
307
308         tv = get_Const_tarval(op);
309         if (! tarval_is_null(tv) && !tarval_is_one(tv))
310                 return 0;
311
312         mem = get_Call_mem(call);
313
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);
317         return 1;
318 }
319
320 int i_mapper_cbrt(ir_node *call, void *ctx)
321 {
322         ir_node   *mem;
323         ir_tarval *tv;
324         ir_node   *op = get_Call_param(call, 0);
325         (void) ctx;
326
327         if (!is_Const(op))
328                 return 0;
329
330         tv = get_Const_tarval(op);
331         if (! tarval_is_null(tv) && !tarval_is_one(tv) && !tarval_is_minus_one(tv))
332                 return 0;
333
334         mem = get_Call_mem(call);
335
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);
339         return 1;
340 }
341
342 int i_mapper_pow(ir_node *call, void *ctx)
343 {
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;
350         ir_node  *irn;
351         dbg_info *dbg;
352         ir_node  *mem;
353         (void) ctx;
354
355         if (is_Const(left) && is_Const_one(left)) {
356                 /* pow (1.0, x) = 1.0 */
357                 irn = left;
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 */
366                         irn = left;
367                 } else if (tarval_is_minus_one(tv)) {
368                         /* pow(x, -1.0) = 1/x */
369                         irn = NULL;
370                 } else
371                         return 0;
372         } else {
373                 return 0;
374         }
375
376         mem = get_Call_mem(call);
377         dbg = get_irn_dbg_info(call);
378
379         if (irn == NULL) {
380                 ir_mode *result_mode = get_irn_mode(left);
381                 ir_node *div;
382
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;
388                 }
389
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);
398                 }
399                 if (result_mode != mode) {
400                         irn = new_r_Conv(block, irn, result_mode);
401                 }
402         }
403         DBG_OPT_ALGSIM0(call, irn, FS_OPT_RTS_POW);
404         replace_call(irn, call, mem, reg_jmp, exc_jmp);
405         return 1;
406 }
407
408 int i_mapper_exp(ir_node *call, void *ctx)
409 {
410         ir_node *val  = get_Call_param(call, 0);
411         (void) ctx;
412
413         if (is_Const(val) && is_Const_null(val)) {
414                 /* exp(0.0) = 1.0 */
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);
421                 return 1;
422         }
423         return 0;
424 }
425
426 int i_mapper_exp2(ir_node *call, void *ctx)
427 {
428         return i_mapper_exp(call, ctx);
429 }
430
431 int i_mapper_exp10(ir_node *call, void *ctx)
432 {
433         return i_mapper_exp(call, ctx);
434 }
435
436 /**
437  * A mapper for mapping f(0.0) to 0.0.
438  */
439 static int i_mapper_zero_to_zero(ir_node *call, void *ctx, int reason)
440 {
441         ir_node *val  = get_Call_param(call, 0);
442         (void) ctx;
443
444         if (is_Const(val) && is_Const_null(val)) {
445                 /* f(0.0) = 0.0 */
446                 ir_node *mem = get_Call_mem(call);
447                 DBG_OPT_ALGSIM0(call, val, reason);
448                 replace_call(val, call, mem, NULL, NULL);
449                 return 1;
450         }
451         return 0;
452 }
453
454 /**
455  * A mapper for mapping f(1.0) to 0.0.
456  */
457 static int i_mapper_one_to_zero(ir_node *call, void *ctx, int reason)
458 {
459         ir_node *val  = get_Call_param(call, 0);
460         (void) ctx;
461
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);
470                 return 1;
471         }
472         return 0;
473 }
474
475 /**
476  * A mapper for mapping a functions with the following characteristics:
477  * f(-x)  = f(x).
478  * f(0.0) = 1.0
479  */
480 static int i_mapper_symmetric_zero_to_one(ir_node *call, void *ctx, int reason)
481 {
482         int      changed = 0;
483         ir_node *val     = get_Call_param(call, 0);
484         (void) ctx;
485
486         if (is_Conv(val)) {
487                 ir_node *op = get_Conv_op(val);
488                 if (is_Minus(op)) {
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);
493
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);
498                         changed = 1;
499                 }
500         } else if (is_Minus(val)) {
501                 /* f(-x) = f(x) */
502                 val = get_Minus_op(val);
503                 DBG_OPT_ALGSIM2(call, val, call, FS_OPT_RTS_SYMMETRIC);
504                 set_Call_param(call, 0, val);
505                 changed = 1;
506         }
507
508         if (is_Const(val) && is_Const_null(val)) {
509                 /* f(0.0) = 1.0 */
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);
516                 changed = 1;
517         }
518         return changed;
519 }
520
521 int i_mapper_log(ir_node *call, void *ctx)
522 {
523         /* log(1.0) = 0.0 */
524         return i_mapper_one_to_zero(call, ctx, FS_OPT_RTS_LOG);
525 }
526
527 int i_mapper_log2(ir_node *call, void *ctx)
528 {
529         /* log2(1.0) = 0.0 */
530         return i_mapper_one_to_zero(call, ctx, FS_OPT_RTS_LOG);
531 }
532
533 int i_mapper_log10(ir_node *call, void *ctx)
534 {
535         /* log10(1.0) = 0.0 */
536         return i_mapper_one_to_zero(call, ctx, FS_OPT_RTS_LOG);
537 }
538
539 int i_mapper_sin(ir_node *call, void *ctx)
540 {
541         /* sin(0.0) = 0.0 */
542         return i_mapper_zero_to_zero(call, ctx, FS_OPT_RTS_SIN);
543 }
544
545 int i_mapper_cos(ir_node *call, void *ctx)
546 {
547         /* cos(0.0) = 1.0, cos(-x) = x */
548         return i_mapper_symmetric_zero_to_one(call, ctx, FS_OPT_RTS_COS);
549 }
550
551 int i_mapper_tan(ir_node *call, void *ctx)
552 {
553         /* tan(0.0) = 0.0 */
554         return i_mapper_zero_to_zero(call, ctx, FS_OPT_RTS_TAN);
555 }
556
557 int i_mapper_asin(ir_node *call, void *ctx)
558 {
559         /* asin(0.0) = 0.0 */
560         return i_mapper_zero_to_zero(call, ctx, FS_OPT_RTS_ASIN);
561 }
562
563 int i_mapper_acos(ir_node *call, void *ctx)
564 {
565         /* acos(1.0) = 0.0 */
566         return i_mapper_one_to_zero(call, ctx, FS_OPT_RTS_ACOS);
567 }
568
569 int i_mapper_atan(ir_node *call, void *ctx)
570 {
571         /* atan(0.0) = 0.0 */
572         return i_mapper_zero_to_zero(call, ctx, FS_OPT_RTS_ATAN);
573 }
574
575 int i_mapper_sinh(ir_node *call, void *ctx)
576 {
577         /* sinh(0.0) = 0.0 */
578         return i_mapper_zero_to_zero(call, ctx, FS_OPT_RTS_SINH);
579 }
580
581 int i_mapper_cosh(ir_node *call, void *ctx)
582 {
583         /* cosh(0.0) = 1.0, cosh(-x) = x */
584         return i_mapper_symmetric_zero_to_one(call, ctx, FS_OPT_RTS_COSH);
585 }
586
587 int i_mapper_tanh(ir_node *call, void *ctx)
588 {
589         /* tanh(0.0) = 0.0 */
590         return i_mapper_zero_to_zero(call, ctx, FS_OPT_RTS_TANH);
591 }
592
593 /**
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).
596  *
597  * @param ptr  the pointer
598  */
599 static ir_entity *get_const_entity(ir_node *ptr)
600 {
601         if (is_SymConst_addr_ent(ptr)) {
602                 ir_entity *ent = get_SymConst_entity(ptr);
603
604                 if (get_entity_linkage(ent) & IR_LINKAGE_CONSTANT) {
605                         /* a constant entity */
606                         return ent;
607                 }
608         }
609         return NULL;
610 }
611
612 static ir_tarval *get_initializer_value(ir_initializer_t *const init, ir_mode *const mode)
613 {
614         switch (get_initializer_kind(init)) {
615         case IR_INITIALIZER_NULL:
616                 return get_mode_null(mode);
617
618         case IR_INITIALIZER_TARVAL:
619                 return get_initializer_tarval_value(init);
620
621         case IR_INITIALIZER_CONST: {
622                 ir_node *const irn = get_initializer_const_value(init);
623                 if (is_Const(irn))
624                         return get_Const_tarval(irn);
625                 break;
626         }
627
628         case IR_INITIALIZER_COMPOUND:
629                 break;
630         }
631
632         return get_tarval_undefined();
633 }
634
635 static bool initializer_val_is_null(ir_initializer_t *init)
636 {
637         ir_tarval *tv;
638
639         if (get_initializer_kind(init) == IR_INITIALIZER_NULL)
640                 return true;
641
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);
646                 if (!is_Const(irn))
647                         return false;
648                 tv = get_Const_tarval(irn);
649         } else {
650                 return false;
651         }
652
653         return tarval_is_null(tv);
654 }
655
656 /**
657  * Calculate the value of strlen if possible.
658  *
659  * @param ent     the entity
660  * @param res_tp  the result type
661  *
662  * @return a Const node containing the strlen() result or NULL
663  *         if the evaluation fails
664  */
665 static ir_node *eval_strlen(ir_graph *irg, ir_entity *ent, ir_type *res_tp)
666 {
667         ir_type *tp = get_entity_type(ent);
668         ir_mode *mode;
669         ir_initializer_t *initializer;
670         size_t            size;
671         size_t            i;
672
673         if (! is_Array_type(tp))
674                 return NULL;
675         tp = get_array_element_type(tp);
676         if (! is_Primitive_type(tp))
677                 return NULL;
678         mode = get_type_mode(tp);
679
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)
682                 return NULL;
683
684         initializer = get_entity_initializer(ent);
685         if (get_initializer_kind(initializer) != IR_INITIALIZER_COMPOUND)
686                 return NULL;
687
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);
694                 }
695         }
696
697         return NULL;
698 }
699
700 int i_mapper_strlen(ir_node *call, void *ctx)
701 {
702         ir_node *s     = get_Call_param(call, 0);
703         ir_entity *ent = get_const_entity(s);
704
705         (void) ctx;
706
707         /* FIXME: this cannot handle constant strings inside struct initializers yet */
708         if (ent != NULL) {
709                 /* a constant entity */
710                 ir_type *tp = get_Call_type(call);
711                 ir_node *irn;
712
713                 tp  = get_method_res_type(tp, 0);
714                 irn = eval_strlen(get_irn_irg(call), ent, tp);
715
716                 if (irn) {
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);
720                         return 1;
721                 }
722         }
723         return 0;
724 }
725
726 /**
727  * Calculate the value of strlen if possible.
728  *
729  * @param left    the left entity
730  * @param right   the right entity
731  * @param res_tp  the result type
732  *
733  * @return a Const node containing the strcmp() result or NULL
734  *         if the evaluation fails
735  */
736 static ir_node *eval_strcmp(ir_graph *irg, ir_entity *left, ir_entity *right,
737                             ir_type *res_tp)
738 {
739         ir_type          *tp;
740         ir_mode          *mode;
741         ir_initializer_t *init_l;
742         ir_initializer_t *init_r;
743         size_t            size_l;
744         size_t            size_r;
745         size_t            size;
746         size_t            i;
747
748         tp = get_entity_type(left);
749         if (! is_Array_type(tp))
750                 return NULL;
751         tp = get_array_element_type(tp);
752         if (! is_Primitive_type(tp))
753                 return NULL;
754         mode = get_type_mode(tp);
755
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)
758                 return NULL;
759
760         tp = get_entity_type(right);
761         if (! is_Array_type(tp))
762                 return NULL;
763         tp = get_array_element_type(tp);
764         if (! is_Primitive_type(tp))
765                 return NULL;
766         mode = get_type_mode(tp);
767
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)
770                 return NULL;
771
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)
776                 return NULL;
777
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;
781
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);
787
788                 if (!tarval_is_constant(tv_l) || !tarval_is_constant(tv_r))
789                         return NULL;
790
791                 if (tv_l != 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);
797                 }
798
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);
802                 }
803         }
804
805         return NULL;
806 }
807
808 /**
809  * Checks if an entity represents the empty string.
810  *
811  * @param ent     the entity
812  *
813  * @return non-zero if ent represents the empty string
814  */
815 static int is_empty_string(ir_entity *ent)
816 {
817         ir_type          *tp = get_entity_type(ent);
818         ir_mode          *mode;
819         ir_initializer_t *initializer;
820         ir_initializer_t *init0;
821
822         if (! is_Array_type(tp))
823                 return 0;
824         tp = get_array_element_type(tp);
825         if (! is_Primitive_type(tp))
826                 return 0;
827         mode = get_type_mode(tp);
828
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)
831                 return 0;
832
833         initializer = get_entity_initializer(ent);
834         if (get_initializer_kind(initializer) != IR_INITIALIZER_COMPOUND)
835                 return 0;
836
837         if (get_initializer_compound_n_entries(initializer) < 1)
838                 return 0;
839
840         init0 = get_initializer_compound_value(initializer, 0);
841         return initializer_val_is_null(init0);
842 }
843
844 int i_mapper_strcmp(ir_node *call, void *ctx)
845 {
846         ir_node   *left    = get_Call_param(call, 0);
847         ir_node   *right   = get_Call_param(call, 1);
848         ir_node   *irn     = NULL;
849         ir_node   *exc     = NULL;
850         ir_node   *reg     = NULL;
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;
854         ir_type   *char_tp;
855         ir_node   *v;
856
857         (void) ctx;
858
859         /* do some type checks first */
860         if (! is_Primitive_type(res_tp))
861                 return 0;
862         char_tp = get_method_param_type(call_tp, 0);
863         if (char_tp != get_method_param_type(call_tp, 1))
864                 return 0;
865         if (! is_Pointer_type(char_tp))
866                 return 0;
867         char_tp = get_pointer_points_to_type(char_tp);
868
869         ir_node *mem = get_Call_mem(call);
870         if (left == right) {
871                 /* a strcmp(s, s) ==> 0 */
872                 ir_graph *irg = get_irn_irg(call);
873                 ir_mode *mode = get_type_mode(res_tp);
874
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);
878                 return 1;
879         }
880         ent_l = get_const_entity(left);
881         ent_r = get_const_entity(right);
882
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)*/
889                         v = right;
890                         goto replace_by_call;
891                 }
892         } else if (ent_r != NULL) {
893                 if (is_empty_string(ent_r)) {
894                         /* s strcmp(s, "") ==> (*s) */
895                         ir_node  *block;
896                         dbg_info *dbg;
897                         ir_mode  *mode;
898
899                         v = left;
900 replace_by_call:
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);
905
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);
914                         } else {
915                                 exc = NULL;
916                                 reg = NULL;
917                         }
918
919                         /* conv to the result mode */
920                         mode = get_type_mode(res_tp);
921                         irn  = new_rd_Conv(dbg, block, irn, mode);
922
923                         if (v == right) {
924                                 /* negate in the ("", s) case */
925                                 irn = new_rd_Minus(dbg, block, irn, mode);
926                         }
927                 }
928         }
929
930         if (irn != NULL) {
931                 DBG_OPT_ALGSIM0(call, irn, FS_OPT_RTS_STRCMP);
932                 replace_call(irn, call, mem, reg, exc);
933                 return 1;
934         }
935
936         return 0;
937 }
938
939 int i_mapper_strncmp(ir_node *call, void *ctx)
940 {
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);
944         ir_node *irn;
945         (void) ctx;
946
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);
957
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);
961                 return 1;
962         }
963         return 0;
964 }
965
966 int i_mapper_strcpy(ir_node *call, void *ctx)
967 {
968         ir_node *dst = get_Call_param(call, 0);
969         ir_node *src = get_Call_param(call, 1);
970         (void) ctx;
971
972         if (dst == src) {
973                 /* a strcpy(d, s) ==> d */
974                 ir_node *mem = get_Call_mem(call);
975                 ir_node *dst = get_Call_param(call, 0);
976
977                 DBG_OPT_ALGSIM0(call, dst, FS_OPT_RTS_STRCPY);
978                 replace_call(dst, call, mem, NULL, NULL);
979                 return 1;
980         }
981         return 0;
982 }
983
984 int i_mapper_memcpy(ir_node *call, void *ctx)
985 {
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);
989         (void) ctx;
990
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);
995
996                 DBG_OPT_ALGSIM0(call, dst, FS_OPT_RTS_MEMCPY);
997                 replace_call(dst, call, mem, NULL, NULL);
998                 return 1;
999         }
1000         return 0;
1001 }
1002
1003 int i_mapper_mempcpy(ir_node *call, void *ctx)
1004 {
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);
1008         (void) ctx;
1009
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);
1018
1019                 DBG_OPT_ALGSIM0(call, res, FS_OPT_RTS_MEMPCPY);
1020                 replace_call(res, call, mem, NULL, NULL);
1021                 return 1;
1022         }
1023         return 0;
1024 }
1025
1026 int i_mapper_memmove(ir_node *call, void *ctx)
1027 {
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);
1031         (void) ctx;
1032
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);
1037
1038                 DBG_OPT_ALGSIM0(call, dst, FS_OPT_RTS_MEMMOVE);
1039                 replace_call(dst, call, mem, NULL, NULL);
1040                 return 1;
1041         }
1042         return 0;
1043 }
1044
1045 int i_mapper_memset(ir_node *call, void *ctx)
1046 {
1047         ir_node *len = get_Call_param(call, 2);
1048         (void) ctx;
1049
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);
1054
1055                 DBG_OPT_ALGSIM0(call, dst, FS_OPT_RTS_MEMSET);
1056                 replace_call(dst, call, mem, NULL, NULL);
1057                 return 1;
1058         }
1059         return 0;
1060 }
1061
1062 int i_mapper_memcmp(ir_node *call, void *ctx)
1063 {
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);
1067         ir_node *irn;
1068         (void) ctx;
1069
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);
1080
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);
1084                 return 1;
1085         }
1086         return 0;
1087 }
1088
1089 /**
1090  * Returns the result mode of a node.
1091  */
1092 static ir_mode *get_irn_res_mode(ir_node *node)
1093 {
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;
1099         }
1100 }
1101
1102 int i_mapper_RuntimeCall(ir_node *node, runtime_rt *rt)
1103 {
1104         int i, j, arity, first, n_param, n_res;
1105         long n_proj;
1106         ir_type *mtp;
1107         ir_node *mem, *call, *addr, *res_proj;
1108         ir_node **in;
1109         bool     throws_exception;
1110         ir_op   *op;
1111         symconst_symbol sym;
1112         ir_mode *mode = get_irn_mode(node);
1113
1114         if (mode != rt->mode)
1115                 return 0;
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)
1120                         return 0;
1121         }
1122
1123         arity = get_irn_arity(node);
1124         if (arity <= 0)
1125                 return 0;
1126
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);
1131
1132         mem = get_irn_n(node, 0);
1133         if (get_irn_mode(mem) != mode_M) {
1134                 mem = new_r_NoMem(irg);
1135                 first = 0;
1136         } else
1137                 first = 1;
1138
1139         /* check if the modes of the predecessors match the parameter modes */
1140         if (arity - first != n_param)
1141                 return 0;
1142
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);
1146
1147                 if (get_type_mode(param_tp) != get_irn_mode(pred))
1148                         return 0;
1149         }
1150
1151         n_res            = get_method_n_ress(mtp);
1152         throws_exception = ir_throws_exception(node);
1153
1154         /* step 0: calculate the number of needed Proj's */
1155         n_proj = 0;
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);
1161         }
1162
1163         if (n_proj > 0) {
1164                 if (rt->mode != mode_T) /* must be mode_T */
1165                         return 0;
1166         } else {
1167                 if (n_res > 0)
1168                         /* must match */
1169                         if (get_type_mode(get_method_res_type(mtp, 0)) != rt->mode)
1170                                 return 0;
1171         }
1172
1173         /* ok, when we are here, the number of predecessors match as well as the parameter modes */
1174         op = get_irn_op(node);
1175
1176         in = NULL;
1177         if (n_param > 0) {
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);
1181         }
1182
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));
1188
1189         if (n_res > 0)
1190                 res_proj = new_r_Proj(call, mode_T, pn_Call_T_result);
1191         else
1192                 res_proj = NULL;
1193
1194         if (n_proj > 0) {
1195                 n_proj += n_res - 1;
1196
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) {
1200                         in[i] = bad;
1201                 }
1202
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);
1208                 }
1209
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;
1215                         }
1216                 }
1217
1218                 turn_into_tuple(node, n_proj, in);
1219                 return 1;
1220         } else {
1221                 /* only one return value supported */
1222                 if (n_res > 0) {
1223                         ir_mode *mode = get_type_mode(get_method_res_type(mtp, 0));
1224
1225                         res_proj = new_r_Proj(call, mode_T, pn_Call_T_result);
1226                         res_proj = new_r_Proj(res_proj, mode, 0);
1227
1228                         exchange(node, res_proj);
1229                         return 1;
1230                 }
1231         }
1232         /* should not happen */
1233         return 0;
1234 }