2 * Copyright (C) 1995-2008 University of Karlsruhe. All right reserved.
4 * This file is part of libFirm.
6 * This file may be distributed and/or modified under the terms of the
7 * GNU General Public License version 2 as published by the Free Software
8 * Foundation and appearing in the file LICENSE.GPL included in the
9 * packaging of this file.
11 * Licensees holding valid libFirm Professional Edition licenses may use
12 * this file in accordance with the libFirm Commercial License.
13 * Agreement provided with the Software.
15 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
16 * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * @brief Representation of opcode of intermediate operation.
23 * @author Christian Schaefer, Goetz Lindenmaier, Michael Beck
33 #include "irbackedge_t.h"
36 #include "irverify_t.h"
37 #include "reassoc_t.h"
42 static ir_op **opcodes;
43 /** the available next opcode */
44 static unsigned next_iro = iro_MaxOpcode;
46 static ir_type *default_get_type_attr(const ir_node *node);
47 static ir_entity *default_get_entity_attr(const ir_node *node);
48 static unsigned default_hash_node(const ir_node *node);
49 static void default_copy_attr(ir_graph *irg, const ir_node *old_node,
52 ir_op *new_ir_op(unsigned code, const char *name, op_pin_state p,
53 irop_flags flags, op_arity opar, int op_index,
56 ir_op *res = XMALLOCZ(ir_op);
59 res->name = new_id_from_chars(name, strlen(name));
61 res->attr_size = attr_size;
64 res->op_index = op_index;
67 memset(&res->ops, 0, sizeof(res->ops));
68 res->ops.hash = default_hash_node;
69 res->ops.copy_attr = default_copy_attr;
70 res->ops.get_type_attr = default_get_type_attr;
71 res->ops.get_entity_attr = default_get_entity_attr;
74 size_t len = ARR_LEN(opcodes);
75 if ((size_t)code >= len) {
76 ARR_RESIZE(ir_op*, opcodes, (size_t)code+1);
77 memset(&opcodes[len], 0, (code-len+1) * sizeof(opcodes[0]));
79 if (opcodes[code] != NULL)
80 panic("opcode registered twice");
88 void free_ir_op(ir_op *code)
90 hook_free_ir_op(code);
92 assert(opcodes[code->code] == code);
93 opcodes[code->code] = NULL;
98 unsigned ir_get_n_opcodes(void)
100 return ARR_LEN(opcodes);
103 ir_op *ir_get_opcode(unsigned code)
105 assert((size_t)code < ARR_LEN(opcodes));
106 return opcodes[code];
109 void ir_clear_opcodes_generic_func(void)
111 size_t n = ir_get_n_opcodes();
114 for (i = 0; i < n; ++i) {
115 ir_op *op = ir_get_opcode(i);
118 op->ops.generic = (op_func)NULL;
119 op->ops.generic1 = (op_func)NULL;
120 op->ops.generic2 = (op_func)NULL;
124 void ir_op_set_memory_index(ir_op *op, int memory_index)
126 assert(op->flags & irop_flag_uses_memory);
127 op->memory_index = memory_index;
130 void ir_op_set_fragile_indices(ir_op *op, int pn_x_regular, int pn_x_except)
132 assert(op->flags & irop_flag_fragile);
133 op->pn_x_regular = pn_x_regular;
134 op->pn_x_except = pn_x_except;
137 const char *get_op_name (const ir_op *op)
139 return get_id_str(op->name);
142 unsigned (get_op_code)(const ir_op *op)
144 return get_op_code_(op);
147 ident *(get_op_ident)(const ir_op *op)
149 return get_op_ident_(op);
152 const char *get_op_pin_state_name(op_pin_state s)
155 #define XXX(s) case s: return #s
156 XXX(op_pin_state_floats);
157 XXX(op_pin_state_pinned);
158 XXX(op_pin_state_exc_pinned);
159 XXX(op_pin_state_mem_pinned);
165 op_pin_state (get_op_pinned)(const ir_op *op)
167 return get_op_pinned_(op);
170 void set_op_pinned(ir_op *op, op_pin_state pinned)
172 if (op == op_Block || op == op_Phi || is_op_cfopcode(op)) return;
173 op->pin_state = pinned;
176 unsigned get_next_ir_opcode(void)
181 unsigned get_next_ir_opcodes(unsigned num)
183 unsigned base = next_iro;
188 op_func (get_generic_function_ptr)(const ir_op *op)
190 return get_generic_function_ptr_(op);
193 void (set_generic_function_ptr)(ir_op *op, op_func func)
195 set_generic_function_ptr_(op, func);
198 ir_op_ops *(get_op_ops)(ir_op *op)
200 return get_op_ops_(op);
203 irop_flags get_op_flags(const ir_op *op)
205 return (irop_flags)op->flags;
208 static ir_type *default_get_type_attr(const ir_node *node)
211 return get_unknown_type();
214 static ir_entity *default_get_entity_attr(const ir_node *node)
220 static unsigned default_hash_node(const ir_node *node)
225 /* hash table value = 9*(9*(9*(9*(9*arity+in[0])+in[1])+ ...)+mode)+code */
226 h = irn_arity = get_irn_arity(node);
228 /* consider all in nodes... except the block if not a control flow. */
229 for (i = is_cfop(node) ? -1 : 0; i < irn_arity; ++i) {
230 ir_node *pred = get_irn_n(node, i);
231 if (is_irn_cse_neutral(pred))
234 h = 9*h + hash_ptr(pred);
238 h = 9*h + hash_ptr(get_irn_mode(node));
240 h = 9*h + hash_ptr(get_irn_op(node));
246 * Calculate a hash value of a Const node.
248 static unsigned hash_Const(const ir_node *node)
252 /* special value for const, as they only differ in their tarval. */
253 h = hash_ptr(node->attr.con.tarval);
259 * Calculate a hash value of a SymConst node.
261 static unsigned hash_SymConst(const ir_node *node)
265 /* all others are pointers */
266 h = hash_ptr(node->attr.symc.sym.type_p);
271 /** Compares two exception attributes */
272 static int node_cmp_exception(const ir_node *a, const ir_node *b)
274 const except_attr *ea = &a->attr.except;
275 const except_attr *eb = &b->attr.except;
276 return ea->pin_state != eb->pin_state;
279 /** Compares the attributes of two Const nodes. */
280 static int node_cmp_attr_Const(const ir_node *a, const ir_node *b)
282 return get_Const_tarval(a) != get_Const_tarval(b);
285 /** Compares the attributes of two Proj nodes. */
286 static int node_cmp_attr_Proj(const ir_node *a, const ir_node *b)
288 return a->attr.proj.proj != b->attr.proj.proj;
291 /** Compares the attributes of two Alloc nodes. */
292 static int node_cmp_attr_Alloc(const ir_node *a, const ir_node *b)
294 const alloc_attr *pa = &a->attr.alloc;
295 const alloc_attr *pb = &b->attr.alloc;
296 if (pa->where != pb->where || pa->type != pb->type)
298 return node_cmp_exception(a, b);
301 /** Compares the attributes of two Free nodes. */
302 static int node_cmp_attr_Free(const ir_node *a, const ir_node *b)
304 const free_attr *pa = &a->attr.free;
305 const free_attr *pb = &b->attr.free;
306 return (pa->where != pb->where) || (pa->type != pb->type);
309 /** Compares the attributes of two SymConst nodes. */
310 static int node_cmp_attr_SymConst(const ir_node *a, const ir_node *b)
312 const symconst_attr *pa = &a->attr.symc;
313 const symconst_attr *pb = &b->attr.symc;
314 return (pa->kind != pb->kind)
315 || (pa->sym.type_p != pb->sym.type_p);
318 /** Compares the attributes of two Call nodes. */
319 static int node_cmp_attr_Call(const ir_node *a, const ir_node *b)
321 const call_attr *pa = &a->attr.call;
322 const call_attr *pb = &b->attr.call;
323 if (pa->type != pb->type)
325 return node_cmp_exception(a, b);
328 /** Compares the attributes of two Sel nodes. */
329 static int node_cmp_attr_Sel(const ir_node *a, const ir_node *b)
331 const ir_entity *a_ent = get_Sel_entity(a);
332 const ir_entity *b_ent = get_Sel_entity(b);
333 return a_ent != b_ent;
336 /** Compares the attributes of two Phi nodes. */
337 static int node_cmp_attr_Phi(const ir_node *a, const ir_node *b)
340 /* do not CSE Phi-nodes without any inputs when building new graphs */
341 if (get_irn_arity(a) == 0 &&
342 irg_is_constrained(get_irn_irg(a), IR_GRAPH_CONSTRAINT_CONSTRUCTION)) {
348 /** Compares the attributes of two Cast nodes. */
349 static int node_cmp_attr_Cast(const ir_node *a, const ir_node *b)
351 return get_Cast_type(a) != get_Cast_type(b);
354 /** Compares the attributes of two Load nodes. */
355 static int node_cmp_attr_Load(const ir_node *a, const ir_node *b)
357 if (get_Load_volatility(a) == volatility_is_volatile ||
358 get_Load_volatility(b) == volatility_is_volatile)
359 /* NEVER do CSE on volatile Loads */
361 /* do not CSE Loads with different alignment. Be conservative. */
362 if (get_Load_unaligned(a) != get_Load_unaligned(b))
364 if (get_Load_mode(a) != get_Load_mode(b))
366 return node_cmp_exception(a, b);
369 /** Compares the attributes of two Store nodes. */
370 static int node_cmp_attr_Store(const ir_node *a, const ir_node *b)
372 /* do not CSE Stores with different alignment. Be conservative. */
373 if (get_Store_unaligned(a) != get_Store_unaligned(b))
375 /* NEVER do CSE on volatile Stores */
376 if (get_Store_volatility(a) == volatility_is_volatile ||
377 get_Store_volatility(b) == volatility_is_volatile)
379 return node_cmp_exception(a, b);
382 static int node_cmp_attr_CopyB(const ir_node *a, const ir_node *b)
384 if (get_CopyB_type(a) != get_CopyB_type(b))
387 return node_cmp_exception(a, b);
390 static int node_cmp_attr_Bound(const ir_node *a, const ir_node *b)
392 return node_cmp_exception(a, b);
395 /** Compares the attributes of two Div nodes. */
396 static int node_cmp_attr_Div(const ir_node *a, const ir_node *b)
398 const div_attr *ma = &a->attr.div;
399 const div_attr *mb = &b->attr.div;
400 if (ma->resmode != mb->resmode || ma->no_remainder != mb->no_remainder)
402 return node_cmp_exception(a, b);
405 /** Compares the attributes of two Mod nodes. */
406 static int node_cmp_attr_Mod(const ir_node *a, const ir_node *b)
408 const mod_attr *ma = &a->attr.mod;
409 const mod_attr *mb = &b->attr.mod;
410 if (ma->resmode != mb->resmode)
412 return node_cmp_exception(a, b);
415 static int node_cmp_attr_Cmp(const ir_node *a, const ir_node *b)
417 const cmp_attr *ma = &a->attr.cmp;
418 const cmp_attr *mb = &b->attr.cmp;
419 return ma->relation != mb->relation;
422 /** Compares the attributes of two Confirm nodes. */
423 static int node_cmp_attr_Confirm(const ir_node *a, const ir_node *b)
425 const confirm_attr *ma = &a->attr.confirm;
426 const confirm_attr *mb = &b->attr.confirm;
427 return ma->relation != mb->relation;
430 /** Compares the attributes of two Builtin nodes. */
431 static int node_cmp_attr_Builtin(const ir_node *a, const ir_node *b)
433 if (get_Builtin_kind(a) != get_Builtin_kind(b))
435 if (get_Builtin_type(a) != get_Builtin_type(b))
437 return node_cmp_exception(a, b);
440 /** Compares the attributes of two ASM nodes. */
441 static int node_cmp_attr_ASM(const ir_node *a, const ir_node *b)
443 if (get_ASM_text(a) != get_ASM_text(b))
446 int n_inputs = get_ASM_n_inputs(a);
447 if (n_inputs != get_ASM_n_inputs(b))
450 const ir_asm_constraint *in_a = get_ASM_input_constraints(a);
451 const ir_asm_constraint *in_b = get_ASM_input_constraints(b);
452 for (int i = 0; i < n_inputs; ++i) {
453 if (in_a[i].pos != in_b[i].pos
454 || in_a[i].constraint != in_b[i].constraint
455 || in_a[i].mode != in_b[i].mode)
459 size_t n_outputs = get_ASM_n_output_constraints(a);
460 if (n_outputs != get_ASM_n_output_constraints(b))
463 const ir_asm_constraint *out_a = get_ASM_output_constraints(a);
464 const ir_asm_constraint *out_b = get_ASM_output_constraints(b);
465 for (size_t i = 0; i < n_outputs; ++i) {
466 if (out_a[i].pos != out_b[i].pos
467 || out_a[i].constraint != out_b[i].constraint
468 || out_a[i].mode != out_b[i].mode)
472 size_t n_clobbers = get_ASM_n_clobbers(a);
473 if (n_clobbers != get_ASM_n_clobbers(b))
476 ident **cla = get_ASM_clobbers(a);
477 ident **clb = get_ASM_clobbers(b);
478 for (size_t i = 0; i < n_clobbers; ++i) {
479 if (cla[i] != clb[i])
483 return node_cmp_exception(a, b);
486 /** Compares the inexistent attributes of two Dummy nodes. */
487 static int node_cmp_attr_Dummy(const ir_node *a, const ir_node *b)
491 /* Dummy nodes never equal by definition */
495 static int node_cmp_attr_InstOf(const ir_node *a, const ir_node *b)
497 if (get_InstOf_type(a) != get_InstOf_type(b))
499 return node_cmp_exception(a, b);
502 static void default_copy_attr(ir_graph *irg, const ir_node *old_node,
507 assert(get_irn_op(old_node) == get_irn_op(new_node));
508 memcpy(&new_node->attr, &old_node->attr, get_op_attr_size(get_irn_op(old_node)));
512 * Copies all Call attributes stored in the old node to the new node.
514 static void call_copy_attr(ir_graph *irg, const ir_node *old_node,
517 default_copy_attr(irg, old_node, new_node);
518 remove_Call_callee_arr(new_node);
522 * Copies all Block attributes stored in the old node to the new node.
524 static void block_copy_attr(ir_graph *irg, const ir_node *old_node,
527 default_copy_attr(irg, old_node, new_node);
528 new_node->attr.block.irg.irg = irg;
529 new_node->attr.block.phis = NULL;
530 new_node->attr.block.backedge = new_backedge_arr(irg->obst, get_irn_arity(new_node));
531 new_node->attr.block.block_visited = 0;
532 memset(&new_node->attr.block.dom, 0, sizeof(new_node->attr.block.dom));
533 memset(&new_node->attr.block.pdom, 0, sizeof(new_node->attr.block.pdom));
534 /* It should be safe to copy the entity here, as it has no back-link to the old block.
535 * It serves just as a label number, so copying a labeled block results in an exact copy.
536 * This is at least what we need for DCE to work. */
537 new_node->attr.block.entity = old_node->attr.block.entity;
538 new_node->attr.block.phis = NULL;
542 * Copies all phi attributes stored in old node to the new node
544 static void phi_copy_attr(ir_graph *irg, const ir_node *old_node,
547 default_copy_attr(irg, old_node, new_node);
548 new_node->attr.phi.next = NULL;
549 new_node->attr.phi.u.backedge = new_backedge_arr(irg->obst, get_irn_arity(new_node));
553 * Copies all ASM attributes stored in old node to the new node
555 static void ASM_copy_attr(ir_graph *irg, const ir_node *old_node,
558 default_copy_attr(irg, old_node, new_node);
559 new_node->attr.assem.input_constraints = DUP_ARR_D(ir_asm_constraint, irg->obst, old_node->attr.assem.input_constraints);
560 new_node->attr.assem.output_constraints = DUP_ARR_D(ir_asm_constraint, irg->obst, old_node->attr.assem.output_constraints);
561 new_node->attr.assem.clobbers = DUP_ARR_D(ident*, irg->obst, old_node->attr.assem.clobbers);
564 static void switch_copy_attr(ir_graph *irg, const ir_node *old_node,
567 const ir_switch_table *table = get_Switch_table(old_node);
568 new_node->attr.switcha.table = ir_switch_table_duplicate(irg, table);
569 new_node->attr.switcha.n_outs = old_node->attr.switcha.n_outs;
572 static void register_node_cmp_func(ir_op *op, node_cmp_attr_func func)
574 op->ops.node_cmp_attr = func;
577 static void register_node_hash_func(ir_op *op, hash_func func)
582 static void register_node_copy_attr_func(ir_op *op, copy_attr_func func)
584 op->ops.copy_attr = func;
587 static void generated_init_op(void);
588 static void generated_finish_op(void);
590 void firm_init_op(void)
592 opcodes = NEW_ARR_F(ir_op*, 0);
596 register_node_cmp_func(op_ASM, node_cmp_attr_ASM);
597 register_node_cmp_func(op_Alloc, node_cmp_attr_Alloc);
598 register_node_cmp_func(op_Bound, node_cmp_attr_Bound);
599 register_node_cmp_func(op_Builtin, node_cmp_attr_Builtin);
600 register_node_cmp_func(op_Call, node_cmp_attr_Call);
601 register_node_cmp_func(op_Cast, node_cmp_attr_Cast);
602 register_node_cmp_func(op_Cmp, node_cmp_attr_Cmp);
603 register_node_cmp_func(op_Confirm, node_cmp_attr_Confirm);
604 register_node_cmp_func(op_Const, node_cmp_attr_Const);
605 register_node_cmp_func(op_CopyB, node_cmp_attr_CopyB);
606 register_node_cmp_func(op_Div, node_cmp_attr_Div);
607 register_node_cmp_func(op_Dummy, node_cmp_attr_Dummy);
608 register_node_cmp_func(op_Free, node_cmp_attr_Free);
609 register_node_cmp_func(op_InstOf, node_cmp_attr_InstOf);
610 register_node_cmp_func(op_Load, node_cmp_attr_Load);
611 register_node_cmp_func(op_Mod, node_cmp_attr_Mod);
612 register_node_cmp_func(op_Phi, node_cmp_attr_Phi);
613 register_node_cmp_func(op_Proj, node_cmp_attr_Proj);
614 register_node_cmp_func(op_Sel, node_cmp_attr_Sel);
615 register_node_cmp_func(op_Store, node_cmp_attr_Store);
616 register_node_cmp_func(op_SymConst, node_cmp_attr_SymConst);
618 register_node_hash_func(op_Const, hash_Const);
619 register_node_hash_func(op_SymConst, hash_SymConst);
621 register_node_copy_attr_func(op_Call, call_copy_attr);
622 register_node_copy_attr_func(op_Block, block_copy_attr);
623 register_node_copy_attr_func(op_Phi, phi_copy_attr);
624 register_node_copy_attr_func(op_ASM, ASM_copy_attr);
625 register_node_copy_attr_func(op_Switch, switch_copy_attr);
627 ir_register_opt_node_ops();
628 ir_register_reassoc_node_ops();
629 ir_register_verify_node_ops();
632 void firm_finish_op(void)
635 generated_finish_op();
640 #include "gen_irop.c.inl"