remove the concept of a strictconv
[libfirm] / ir / ir / irop.c
1 /*
2  * Copyright (C) 1995-2008 University of Karlsruhe.  All right reserved.
3  *
4  * This file is part of libFirm.
5  *
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.
10  *
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.
14  *
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
17  * PURPOSE.
18  */
19
20 /**
21  * @file
22  * @brief   Representation of opcode of intermediate operation.
23  * @author  Christian Schaefer, Goetz Lindenmaier, Michael Beck
24  */
25 #include "config.h"
26
27 #include <string.h>
28
29 #include "irop_t.h"
30 #include "irnode_t.h"
31 #include "irhooks.h"
32 #include "irbackedge_t.h"
33
34 #include "iropt_t.h"
35 #include "irverify_t.h"
36 #include "reassoc_t.h"
37
38 #include "xmalloc.h"
39 #include "benode.h"
40
41 static ir_op **opcodes;
42 /** the available next opcode */
43 static unsigned next_iro = iro_MaxOpcode;
44
45 static ir_type *default_get_type_attr(const ir_node *node);
46 static ir_entity *default_get_entity_attr(const ir_node *node);
47 static unsigned default_hash_node(const ir_node *node);
48 static void default_copy_attr(ir_graph *irg, const ir_node *old_node,
49                               ir_node *new_node);
50
51 ir_op *new_ir_op(unsigned code, const char *name, op_pin_state p,
52                  irop_flags flags, op_arity opar, int op_index,
53                  size_t attr_size)
54 {
55         ir_op *res = XMALLOCZ(ir_op);
56
57         res->code      = code;
58         res->name      = new_id_from_chars(name, strlen(name));
59         res->pin_state = p;
60         res->attr_size = attr_size;
61         res->flags     = flags;
62         res->opar      = opar;
63         res->op_index  = op_index;
64         res->tag       = 0;
65
66         memset(&res->ops, 0, sizeof(res->ops));
67         res->ops.hash            = default_hash_node;
68         res->ops.copy_attr       = default_copy_attr;
69         res->ops.get_type_attr   = default_get_type_attr;
70         res->ops.get_entity_attr = default_get_entity_attr;
71
72         {
73                 size_t len = ARR_LEN(opcodes);
74                 if ((size_t)code >= len) {
75                         ARR_RESIZE(ir_op*, opcodes, (size_t)code+1);
76                         memset(&opcodes[len], 0, (code-len+1) * sizeof(opcodes[0]));
77                 }
78                 if (opcodes[code] != NULL)
79                         panic("opcode registered twice");
80                 opcodes[code] = res;
81         }
82
83         hook_new_ir_op(res);
84         return res;
85 }
86
87 void free_ir_op(ir_op *code)
88 {
89         hook_free_ir_op(code);
90
91         assert(opcodes[code->code] == code);
92         opcodes[code->code] = NULL;
93
94         free(code);
95 }
96
97 unsigned ir_get_n_opcodes(void)
98 {
99         return ARR_LEN(opcodes);
100 }
101
102 ir_op *ir_get_opcode(unsigned code)
103 {
104         assert((size_t)code < ARR_LEN(opcodes));
105         return opcodes[code];
106 }
107
108 void ir_clear_opcodes_generic_func(void)
109 {
110         size_t n = ir_get_n_opcodes();
111         size_t i;
112
113         for (i = 0; i < n; ++i) {
114                 ir_op *op = ir_get_opcode(i);
115                 if (op != NULL)
116                         op->ops.generic = (op_func)NULL;
117         }
118 }
119
120 void ir_op_set_memory_index(ir_op *op, int memory_index)
121 {
122         assert(op->flags & irop_flag_uses_memory);
123         op->memory_index = memory_index;
124 }
125
126 void ir_op_set_fragile_indices(ir_op *op, int pn_x_regular, int pn_x_except)
127 {
128         assert(op->flags & irop_flag_fragile);
129         op->pn_x_regular = pn_x_regular;
130         op->pn_x_except = pn_x_except;
131 }
132
133 const char *get_op_name (const ir_op *op)
134 {
135         return get_id_str(op->name);
136 }
137
138 unsigned (get_op_code)(const ir_op *op)
139 {
140   return get_op_code_(op);
141 }
142
143 ident *(get_op_ident)(const ir_op *op)
144 {
145   return get_op_ident_(op);
146 }
147
148 const char *get_op_pin_state_name(op_pin_state s)
149 {
150         switch (s) {
151 #define XXX(s) case s: return #s
152         XXX(op_pin_state_floats);
153         XXX(op_pin_state_pinned);
154         XXX(op_pin_state_exc_pinned);
155         XXX(op_pin_state_mem_pinned);
156 #undef XXX
157         }
158         return "<none>";
159 }
160
161 op_pin_state (get_op_pinned)(const ir_op *op)
162 {
163         return get_op_pinned_(op);
164 }
165
166 void set_op_pinned(ir_op *op, op_pin_state pinned)
167 {
168         if (op == op_Block || op == op_Phi || is_op_cfopcode(op)) return;
169         op->pin_state = pinned;
170 }
171
172 unsigned get_next_ir_opcode(void)
173 {
174         return next_iro++;
175 }
176
177 unsigned get_next_ir_opcodes(unsigned num)
178 {
179         unsigned base = next_iro;
180         next_iro += num;
181         return base;
182 }
183
184 op_func (get_generic_function_ptr)(const ir_op *op)
185 {
186         return get_generic_function_ptr_(op);
187 }
188
189 void (set_generic_function_ptr)(ir_op *op, op_func func)
190 {
191         set_generic_function_ptr_(op, func);
192 }
193
194 ir_op_ops *(get_op_ops)(ir_op *op)
195 {
196         return get_op_ops_(op);
197 }
198
199 irop_flags get_op_flags(const ir_op *op)
200 {
201         return (irop_flags)op->flags;
202 }
203
204 static ir_type *default_get_type_attr(const ir_node *node)
205 {
206         (void)node;
207         return get_unknown_type();
208 }
209
210 static ir_entity *default_get_entity_attr(const ir_node *node)
211 {
212         (void)node;
213         return NULL;
214 }
215
216 static unsigned default_hash_node(const ir_node *node)
217 {
218         unsigned h;
219         int i, irn_arity;
220
221         /* hash table value = 9*(9*(9*(9*(9*arity+in[0])+in[1])+ ...)+mode)+code */
222         h = irn_arity = get_irn_arity(node);
223
224         /* consider all in nodes... except the block if not a control flow. */
225         for (i = is_cfop(node) ? -1 : 0;  i < irn_arity;  ++i) {
226                 ir_node *pred = get_irn_n(node, i);
227                 if (is_irn_cse_neutral(pred))
228                         h *= 9;
229                 else
230                         h = 9*h + hash_ptr(pred);
231         }
232
233         /* ...mode,... */
234         h = 9*h + hash_ptr(get_irn_mode(node));
235         /* ...and code */
236         h = 9*h + hash_ptr(get_irn_op(node));
237
238         return h;
239 }
240
241 /**
242  * Calculate a hash value of a Const node.
243  */
244 static unsigned hash_Const(const ir_node *node)
245 {
246         unsigned h;
247
248         /* special value for const, as they only differ in their tarval. */
249         h = hash_ptr(node->attr.con.tarval);
250
251         return h;
252 }
253
254 /**
255  * Calculate a hash value of a SymConst node.
256  */
257 static unsigned hash_SymConst(const ir_node *node)
258 {
259         unsigned h;
260
261         /* all others are pointers */
262         h = hash_ptr(node->attr.symc.sym.type_p);
263
264         return h;
265 }
266
267 /** Compares two exception attributes */
268 static int node_cmp_exception(const ir_node *a, const ir_node *b)
269 {
270         const except_attr *ea = &a->attr.except;
271         const except_attr *eb = &b->attr.except;
272         return ea->pin_state != eb->pin_state;
273 }
274
275 /** Compares the attributes of two Const nodes. */
276 static int node_cmp_attr_Const(const ir_node *a, const ir_node *b)
277 {
278         return get_Const_tarval(a) != get_Const_tarval(b);
279 }
280
281 /** Compares the attributes of two Proj nodes. */
282 static int node_cmp_attr_Proj(const ir_node *a, const ir_node *b)
283 {
284         return a->attr.proj.proj != b->attr.proj.proj;
285 }
286
287 /** Compares the attributes of two Alloc nodes. */
288 static int node_cmp_attr_Alloc(const ir_node *a, const ir_node *b)
289 {
290         const alloc_attr *pa = &a->attr.alloc;
291         const alloc_attr *pb = &b->attr.alloc;
292         if (pa->where != pb->where || pa->type != pb->type)
293                 return 1;
294         return node_cmp_exception(a, b);
295 }
296
297 /** Compares the attributes of two Free nodes. */
298 static int node_cmp_attr_Free(const ir_node *a, const ir_node *b)
299 {
300         const free_attr *pa = &a->attr.free;
301         const free_attr *pb = &b->attr.free;
302         return (pa->where != pb->where) || (pa->type != pb->type);
303 }
304
305 /** Compares the attributes of two SymConst nodes. */
306 static int node_cmp_attr_SymConst(const ir_node *a, const ir_node *b)
307 {
308         const symconst_attr *pa = &a->attr.symc;
309         const symconst_attr *pb = &b->attr.symc;
310         return (pa->kind       != pb->kind)
311             || (pa->sym.type_p != pb->sym.type_p);
312 }
313
314 /** Compares the attributes of two Call nodes. */
315 static int node_cmp_attr_Call(const ir_node *a, const ir_node *b)
316 {
317         const call_attr *pa = &a->attr.call;
318         const call_attr *pb = &b->attr.call;
319         if (pa->type != pb->type)
320                 return 1;
321         return node_cmp_exception(a, b);
322 }
323
324 /** Compares the attributes of two Sel nodes. */
325 static int node_cmp_attr_Sel(const ir_node *a, const ir_node *b)
326 {
327         const ir_entity *a_ent = get_Sel_entity(a);
328         const ir_entity *b_ent = get_Sel_entity(b);
329         return a_ent != b_ent;
330 }
331
332 /** Compares the attributes of two Phi nodes. */
333 static int node_cmp_attr_Phi(const ir_node *a, const ir_node *b)
334 {
335         (void) b;
336         /* do not CSE Phi-nodes without any inputs when building new graphs */
337         if (get_irn_arity(a) == 0 &&
338             get_irg_phase_state(get_irn_irg(a)) == phase_building) {
339             return 1;
340         }
341         return 0;
342 }
343
344 /** Compares the attributes of two Cast nodes. */
345 static int node_cmp_attr_Cast(const ir_node *a, const ir_node *b)
346 {
347         return get_Cast_type(a) != get_Cast_type(b);
348 }
349
350 /** Compares the attributes of two Load nodes. */
351 static int node_cmp_attr_Load(const ir_node *a, const ir_node *b)
352 {
353         if (get_Load_volatility(a) == volatility_is_volatile ||
354             get_Load_volatility(b) == volatility_is_volatile)
355                 /* NEVER do CSE on volatile Loads */
356                 return 1;
357         /* do not CSE Loads with different alignment. Be conservative. */
358         if (get_Load_unaligned(a) != get_Load_unaligned(b))
359                 return 1;
360         if (get_Load_mode(a) != get_Load_mode(b))
361                 return 1;
362         return node_cmp_exception(a, b);
363 }
364
365 /** Compares the attributes of two Store nodes. */
366 static int node_cmp_attr_Store(const ir_node *a, const ir_node *b)
367 {
368         /* do not CSE Stores with different alignment. Be conservative. */
369         if (get_Store_unaligned(a) != get_Store_unaligned(b))
370                 return 1;
371         /* NEVER do CSE on volatile Stores */
372         if (get_Store_volatility(a) == volatility_is_volatile ||
373             get_Store_volatility(b) == volatility_is_volatile)
374                 return 1;
375         return node_cmp_exception(a, b);
376 }
377
378 static int node_cmp_attr_CopyB(const ir_node *a, const ir_node *b)
379 {
380         if (get_CopyB_type(a) != get_CopyB_type(b))
381                 return 1;
382
383         return node_cmp_exception(a, b);
384 }
385
386 static int node_cmp_attr_Bound(const ir_node *a, const ir_node *b)
387 {
388         return node_cmp_exception(a, b);
389 }
390
391 /** Compares the attributes of two Div nodes. */
392 static int node_cmp_attr_Div(const ir_node *a, const ir_node *b)
393 {
394         const div_attr *ma = &a->attr.div;
395         const div_attr *mb = &b->attr.div;
396         if (ma->resmode != mb->resmode || ma->no_remainder  != mb->no_remainder)
397                 return 1;
398         return node_cmp_exception(a, b);
399 }
400
401 /** Compares the attributes of two Mod nodes. */
402 static int node_cmp_attr_Mod(const ir_node *a, const ir_node *b)
403 {
404         const mod_attr *ma = &a->attr.mod;
405         const mod_attr *mb = &b->attr.mod;
406         if (ma->resmode != mb->resmode)
407                 return 1;
408         return node_cmp_exception(a, b);
409 }
410
411 static int node_cmp_attr_Cmp(const ir_node *a, const ir_node *b)
412 {
413         const cmp_attr *ma = &a->attr.cmp;
414         const cmp_attr *mb = &b->attr.cmp;
415         return ma->relation != mb->relation;
416 }
417
418 /** Compares the attributes of two Confirm nodes. */
419 static int node_cmp_attr_Confirm(const ir_node *a, const ir_node *b)
420 {
421         const confirm_attr *ma = &a->attr.confirm;
422         const confirm_attr *mb = &b->attr.confirm;
423         return ma->relation != mb->relation;
424 }
425
426 /** Compares the attributes of two Builtin nodes. */
427 static int node_cmp_attr_Builtin(const ir_node *a, const ir_node *b)
428 {
429         if (get_Builtin_kind(a) != get_Builtin_kind(b))
430                 return 1;
431         if (get_Builtin_type(a) != get_Builtin_type(b))
432                 return 1;
433         return node_cmp_exception(a, b);
434 }
435
436 /** Compares the attributes of two ASM nodes. */
437 static int node_cmp_attr_ASM(const ir_node *a, const ir_node *b)
438 {
439         if (get_ASM_text(a) != get_ASM_text(b))
440                 return 1;
441
442         int n_inputs = get_ASM_n_inputs(a);
443         if (n_inputs != get_ASM_n_inputs(b))
444                 return 1;
445
446         const ir_asm_constraint *in_a = get_ASM_input_constraints(a);
447         const ir_asm_constraint *in_b = get_ASM_input_constraints(b);
448         for (int i = 0; i < n_inputs; ++i) {
449                 if (in_a[i].pos != in_b[i].pos
450                     || in_a[i].constraint != in_b[i].constraint
451                     || in_a[i].mode != in_b[i].mode)
452                         return 1;
453         }
454
455         size_t n_outputs = get_ASM_n_output_constraints(a);
456         if (n_outputs != get_ASM_n_output_constraints(b))
457                 return 1;
458
459         const ir_asm_constraint *out_a = get_ASM_output_constraints(a);
460         const ir_asm_constraint *out_b = get_ASM_output_constraints(b);
461         for (size_t i = 0; i < n_outputs; ++i) {
462                 if (out_a[i].pos != out_b[i].pos
463                     || out_a[i].constraint != out_b[i].constraint
464                     || out_a[i].mode != out_b[i].mode)
465                         return 1;
466         }
467
468         size_t n_clobbers = get_ASM_n_clobbers(a);
469         if (n_clobbers != get_ASM_n_clobbers(b))
470                 return 1;
471
472         ident **cla = get_ASM_clobbers(a);
473         ident **clb = get_ASM_clobbers(b);
474         for (size_t i = 0; i < n_clobbers; ++i) {
475                 if (cla[i] != clb[i])
476                         return 1;
477         }
478
479         return node_cmp_exception(a, b);
480 }
481
482 /** Compares the inexistent attributes of two Dummy nodes. */
483 static int node_cmp_attr_Dummy(const ir_node *a, const ir_node *b)
484 {
485         (void) a;
486         (void) b;
487         /* Dummy nodes never equal by definition */
488         return 1;
489 }
490
491 static int node_cmp_attr_InstOf(const ir_node *a, const ir_node *b)
492 {
493         if (get_InstOf_type(a) != get_InstOf_type(b))
494                 return 1;
495         return node_cmp_exception(a, b);
496 }
497
498 static void default_copy_attr(ir_graph *irg, const ir_node *old_node,
499                               ir_node *new_node)
500 {
501         unsigned size = firm_add_node_size;
502         (void) irg;
503
504         assert(get_irn_op(old_node) == get_irn_op(new_node));
505         memcpy(&new_node->attr, &old_node->attr, get_op_attr_size(get_irn_op(old_node)));
506
507         if (size > 0) {
508                 /* copy additional node data */
509                 memcpy(get_irn_data(new_node, void, size), get_irn_data(old_node, void, size), size);
510         }
511 }
512
513 /**
514  * Copies all Call attributes stored in the old node to the new node.
515  */
516 static void call_copy_attr(ir_graph *irg, const ir_node *old_node,
517                            ir_node *new_node)
518 {
519         default_copy_attr(irg, old_node, new_node);
520         remove_Call_callee_arr(new_node);
521 }
522
523 /**
524  * Copies all Block attributes stored in the old node to the new node.
525  */
526 static void block_copy_attr(ir_graph *irg, const ir_node *old_node,
527                             ir_node *new_node)
528 {
529         default_copy_attr(irg, old_node, new_node);
530         new_node->attr.block.irg.irg       = irg;
531         new_node->attr.block.phis          = NULL;
532         new_node->attr.block.backedge      = new_backedge_arr(irg->obst, get_irn_arity(new_node));
533         new_node->attr.block.block_visited = 0;
534         memset(&new_node->attr.block.dom, 0, sizeof(new_node->attr.block.dom));
535         memset(&new_node->attr.block.pdom, 0, sizeof(new_node->attr.block.pdom));
536         /* It should be safe to copy the entity here, as it has no back-link to the old block.
537          * It serves just as a label number, so copying a labeled block results in an exact copy.
538          * This is at least what we need for DCE to work. */
539         new_node->attr.block.entity         = old_node->attr.block.entity;
540         new_node->attr.block.phis           = NULL;
541 }
542
543 /**
544  * Copies all phi attributes stored in old node to the new node
545  */
546 static void phi_copy_attr(ir_graph *irg, const ir_node *old_node,
547                           ir_node *new_node)
548 {
549         default_copy_attr(irg, old_node, new_node);
550         new_node->attr.phi.next       = NULL;
551         new_node->attr.phi.u.backedge = new_backedge_arr(irg->obst, get_irn_arity(new_node));
552 }
553
554 /**
555  * Copies all ASM attributes stored in old node to the new node
556  */
557 static void ASM_copy_attr(ir_graph *irg, const ir_node *old_node,
558                           ir_node *new_node)
559 {
560         default_copy_attr(irg, old_node, new_node);
561         new_node->attr.assem.input_constraints  = DUP_ARR_D(ir_asm_constraint, irg->obst, old_node->attr.assem.input_constraints);
562         new_node->attr.assem.output_constraints = DUP_ARR_D(ir_asm_constraint, irg->obst, old_node->attr.assem.output_constraints);
563         new_node->attr.assem.clobbers = DUP_ARR_D(ident*, irg->obst, old_node->attr.assem.clobbers);
564 }
565
566 static void switch_copy_attr(ir_graph *irg, const ir_node *old_node,
567                              ir_node *new_node)
568 {
569         const ir_switch_table *table = get_Switch_table(old_node);
570         new_node->attr.switcha.table = ir_switch_table_duplicate(irg, table);
571         new_node->attr.switcha.n_outs = old_node->attr.switcha.n_outs;
572 }
573
574 static void register_node_cmp_func(ir_op *op, node_cmp_attr_func func)
575 {
576         op->ops.node_cmp_attr = func;
577 }
578
579 static void register_node_hash_func(ir_op *op, hash_func func)
580 {
581         op->ops.hash = func;
582 }
583
584 static void register_node_copy_attr_func(ir_op *op, copy_attr_func func)
585 {
586         op->ops.copy_attr = func;
587 }
588
589 static void generated_init_op(void);
590 static void generated_finish_op(void);
591
592 void firm_init_op(void)
593 {
594         opcodes = NEW_ARR_F(ir_op*, 0);
595         generated_init_op();
596         be_init_op();
597
598         register_node_cmp_func(op_ASM,      node_cmp_attr_ASM);
599         register_node_cmp_func(op_Alloc,    node_cmp_attr_Alloc);
600         register_node_cmp_func(op_Bound,    node_cmp_attr_Bound);
601         register_node_cmp_func(op_Builtin,  node_cmp_attr_Builtin);
602         register_node_cmp_func(op_Call,     node_cmp_attr_Call);
603         register_node_cmp_func(op_Cast,     node_cmp_attr_Cast);
604         register_node_cmp_func(op_Cmp,      node_cmp_attr_Cmp);
605         register_node_cmp_func(op_Confirm,  node_cmp_attr_Confirm);
606         register_node_cmp_func(op_Const,    node_cmp_attr_Const);
607         register_node_cmp_func(op_CopyB,    node_cmp_attr_CopyB);
608         register_node_cmp_func(op_Div,      node_cmp_attr_Div);
609         register_node_cmp_func(op_Dummy,    node_cmp_attr_Dummy);
610         register_node_cmp_func(op_Free,     node_cmp_attr_Free);
611         register_node_cmp_func(op_InstOf,   node_cmp_attr_InstOf);
612         register_node_cmp_func(op_Load,     node_cmp_attr_Load);
613         register_node_cmp_func(op_Mod,      node_cmp_attr_Mod);
614         register_node_cmp_func(op_Phi,      node_cmp_attr_Phi);
615         register_node_cmp_func(op_Proj,     node_cmp_attr_Proj);
616         register_node_cmp_func(op_Sel,      node_cmp_attr_Sel);
617         register_node_cmp_func(op_Store,    node_cmp_attr_Store);
618         register_node_cmp_func(op_SymConst, node_cmp_attr_SymConst);
619
620         register_node_hash_func(op_Const,    hash_Const);
621         register_node_hash_func(op_SymConst, hash_SymConst);
622
623         register_node_copy_attr_func(op_Call,   call_copy_attr);
624         register_node_copy_attr_func(op_Block,  block_copy_attr);
625         register_node_copy_attr_func(op_Phi,    phi_copy_attr);
626         register_node_copy_attr_func(op_ASM,    ASM_copy_attr);
627         register_node_copy_attr_func(op_Switch, switch_copy_attr);
628
629         ir_register_opt_node_ops();
630         ir_register_reassoc_node_ops();
631         ir_register_verify_node_ops();
632 }
633
634 void firm_finish_op(void)
635 {
636         be_finish_op();
637         generated_finish_op();
638         DEL_ARR_F(opcodes);
639         opcodes = NULL;
640 }
641
642 #include "gen_irop.c.inl"