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 an intermediate operation -- private header.
23 * @author Martin Trapp, Christian Schaefer, Goetz Lindenmaier, Michael Beck
25 #ifndef FIRM_IR_IRNODE_T_H
26 #define FIRM_IR_IRNODE_T_H
31 #include "irgraph_t.h"
34 #include "iredges_t.h"
36 /* This section MUST come first, so the inline functions get used in this header. */
37 #define is_ir_node(thing) is_ir_node_(thing)
38 #define get_irn_arity(node) get_irn_arity_(node)
39 #define get_irn_n(node, n) get_irn_n_(node, n)
40 #define get_irn_mode(node) get_irn_mode_(node)
41 #define set_irn_mode(node, mode) set_irn_mode_(node, mode)
42 #define get_irn_irg(node) get_irn_irg_(node)
43 #define get_nodes_block(node) get_nodes_block_(node)
44 #define get_irn_op(node) get_irn_op_(node)
45 #define set_irn_op(node, op) set_irn_op_(node, op)
46 #define get_irn_opcode(node) get_irn_opcode_(node)
47 #define get_irn_visited(node) get_irn_visited_(node)
48 #define set_irn_visited(node, v) set_irn_visited_(node, v)
49 #define mark_irn_visited(node) mark_irn_visited_(node)
50 #define irn_visited(node) irn_visited_(node)
51 #define irn_visited_else_mark(node) irn_visited_else_mark_(node)
52 #define set_irn_link(node, link) set_irn_link_(node, link)
53 #define get_irn_link(node) get_irn_link_(node)
54 #define get_irn_pinned(node) get_irn_pinned_(node)
55 #define is_irn_pinned_in_irg(node) is_irn_pinned_in_irg_(node)
56 #define is_unop(node) is_unop_(node)
57 #define is_binop(node) is_binop_(node)
58 #define is_Proj(node) is_Proj_(node)
59 #define is_Phi(node) is_Phi_(node)
60 #define is_SymConst_addr_ent(node) is_SymConst_addr_ent_(node)
61 #define get_Block_n_cfgpreds(node) get_Block_n_cfgpreds_(node)
62 #define get_Block_cfgpred(node, pos) get_Block_cfgpred_(node, pos)
63 #define get_Block_cfgpred_block(node, pos) get_Block_cfgpred_block_(node, pos)
64 #define get_Block_block_visited(node) get_Block_block_visited_(node)
65 #define set_Block_block_visited(node, visit) set_Block_block_visited_(node, visit)
66 #define mark_Block_block_visited(node) mark_Block_block_visited_(node)
67 #define Block_block_visited(node) Block_block_visited_(node)
68 #define get_Block_irg(block) get_Block_irg_(block)
69 #define get_Const_tarval(node) get_Const_tarval_(node)
70 #define is_Const_null(node) is_Const_null_(node)
71 #define is_Const_one(node) is_Const_one_(node)
72 #define is_Const_all_one(node) is_Const_all_one_(node)
73 #define is_irn_forking(node) is_irn_forking_(node)
74 #define copy_node_attr(irg,oldn,newn) copy_node_attr_(irg,oldn,newn)
75 #define get_irn_type(node) get_irn_type_(node)
76 #define get_irn_type_attr(node) get_irn_type_attr_(node)
77 #define get_irn_entity_attr(node) get_irn_entity_attr_(node)
78 #define is_irn_constlike(node) is_irn_constlike_(node)
79 #define is_irn_keep(node) is_irn_keep_(node)
80 #define is_irn_start_block_placed(node) is_irn_start_block_placed_(node)
81 #define is_irn_cse_neutral(node) is_irn_cse_neutral_(node)
82 #define get_Cond_jmp_pred(node) get_Cond_jmp_pred_(node)
83 #define set_Cond_jmp_pred(node, pred) set_Cond_jmp_pred_(node, pred)
84 #define get_irn_generic_attr(node) get_irn_generic_attr_(node)
85 #define get_irn_generic_attr_const(node) get_irn_generic_attr_const_(node)
86 #define get_irn_idx(node) get_irn_idx_(node)
88 #define get_irn_deps(node) get_irn_deps_(node)
89 #define get_irn_dep(node, pos) get_irn_dep_(node, pos)
91 #define get_irn_ins_or_deps(node) get_irn_ins_or_deps_(node)
92 #define get_irn_in_or_dep(node, pos) get_irn_in_or_dep_(node, pos)
94 #define get_irn_dbg_info(node) get_irn_dbg_info_(node)
95 #define set_irn_dbg_info(node, db) set_irn_dbg_info_(node, db)
97 #define set_Block_phis(block, phi) set_Block_phis_(block, phi)
98 #define get_Block_phis(block) get_Block_phis_(block)
99 #define add_Block_phi(block, phi) add_Block_phi_(block, phi)
100 #define get_Block_mark(block) get_Block_mark_(block)
101 #define set_Block_mark(block, mark) set_Block_mark_(block, mark)
103 #define set_Phi_next(node, phi) set_Phi_next_(node, phi)
104 #define get_Phi_next(node) get_Phi_next_(node)
106 #define is_arg_Proj(node) is_arg_Proj_(node)
107 #define ir_switch_table_get_n_entries(table) ir_switch_table_get_n_entries_(table)
110 * Returns the array with the ins. The content of the array may not be
112 * Note that this function returns the whole in array including the
113 * block predecessor. So, it is NOT symmetric with set_irn_in().
115 ir_node **get_irn_in(const ir_node *node);
118 * Returns an array with the predecessors of the Block. Depending on
119 * the implementation of the graph data structure this can be a copy of
120 * the internal representation of predecessors as well as the internal
121 * array itself. Therefore writing to this array might obstruct the IR.
123 ir_node **get_Block_cfgpred_arr(ir_node *node);
125 /*-------------------------------------------------------------------*/
126 /* These function are most used in libfirm. Give them as static */
127 /* functions so they can be inlined. */
128 /*-------------------------------------------------------------------*/
131 * Checks whether a pointer points to a ir node.
132 * Intern version for libFirm.
134 static inline int is_ir_node_(const void *thing)
136 return (get_kind(thing) == k_ir_node);
139 static inline unsigned get_irn_idx_(const ir_node *node)
141 return node->node_idx;
145 * Gets the op of a node.
146 * Intern version for libFirm.
148 static inline ir_op *get_irn_op_(const ir_node *node)
153 /* include generated code */
154 #include "gen_irnode.h"
156 static inline void set_irn_op_(ir_node *node, ir_op *op)
161 /** Copies all attributes stored in the old node to the new node.
162 Assumes both have the same opcode and sufficient size. */
163 static inline void copy_node_attr_(ir_graph *irg, const ir_node *old_node,
166 ir_op *op = get_irn_op_(old_node);
168 /* must always exist */
169 op->ops.copy_attr(irg, old_node, new_node);
173 * Gets the opcode of a node.
174 * Intern version for libFirm.
176 static inline unsigned get_irn_opcode_(const ir_node *node)
178 assert(k_ir_node == get_kind(node));
179 return node->op->code;
183 * Returns the number of predecessors without the block predecessor.
184 * Intern version for libFirm.
186 static inline int get_irn_arity_(const ir_node *node)
188 return (int)(ARR_LEN(node->in) - 1);
192 * Intern version for libFirm.
194 static inline ir_node *get_irn_n_(const ir_node *node, int n)
198 assert(-1 <= n && n < get_irn_arity_(node));
200 nn = node->in[n + 1];
201 if (!is_Id(nn)) return nn;
203 return (node->in[n + 1] = skip_Id(nn));
207 * returns a hash value for a node
209 static inline unsigned hash_irn(const ir_node *node)
211 return (unsigned) get_irn_idx(node);
214 static inline int get_irn_deps_(const ir_node *node)
216 return node->deps ? (int)ARR_LEN(node->deps) : 0;
219 static inline ir_node *get_irn_dep_(const ir_node *node, int pos)
221 assert(pos >= 0 && pos < (int)ARR_LEN(node->deps) && "dependency index out of range");
222 return node->deps[pos];
225 /* forward declaration outside iredges_t.h to avoid circular include problems */
226 void edges_notify_edge_kind(ir_node *src, int pos, ir_node *tgt, ir_node *old_tgt, ir_edge_kind_t kind, ir_graph *irg);
228 static inline int get_irn_ins_or_deps_(const ir_node *irn)
230 return get_irn_deps_(irn) + get_irn_arity_(irn);
233 static inline ir_node *get_irn_in_or_dep_(const ir_node *irn, int pos)
235 int n_in = get_irn_arity(irn);
236 return pos < n_in ? get_irn_n(irn, pos) : get_irn_dep(irn, pos - n_in);
240 * Gets the mode of a node.
241 * Intern version for libFirm.
243 static inline ir_mode *get_irn_mode_(const ir_node *node)
249 * Sets the mode of a node.
250 * Intern version of libFirm.
252 static inline void set_irn_mode_(ir_node *node, ir_mode *mode)
257 static inline int ir_has_irg_ref(const ir_node *node)
259 return is_Block(node) || is_Bad(node) || is_Anchor(node);
262 static inline ir_node *get_nodes_block_(const ir_node *node)
264 assert(!is_Block(node));
265 return get_irn_n(node, -1);
268 static inline ir_graph *get_irn_irg_(const ir_node *node)
270 if (! is_Block(node))
271 node = get_nodes_block(node);
272 assert(ir_has_irg_ref(node));
273 return node->attr.irg.irg;
277 * Gets the visited counter of a node.
278 * Intern version for libFirm.
280 static inline ir_visited_t get_irn_visited_(const ir_node *node)
282 return node->visited;
286 * Sets the visited counter of a node.
287 * Intern version for libFirm.
289 static inline void set_irn_visited_(ir_node *node, ir_visited_t visited)
291 node->visited = visited;
295 * Mark a node as visited in a graph.
296 * Intern version for libFirm.
298 static inline void mark_irn_visited_(ir_node *node)
300 node->visited = get_irn_irg(node)->visited;
304 * Returns non-zero if a node of was visited.
305 * Intern version for libFirm.
307 static inline int irn_visited_(const ir_node *node)
309 ir_graph *irg = get_irn_irg(node);
310 return node->visited >= irg->visited;
313 static inline int irn_visited_else_mark_(ir_node *node)
315 if (irn_visited_(node))
317 mark_irn_visited_(node);
322 * Sets the link of a node.
323 * Intern version of libFirm.
325 static inline void set_irn_link_(ir_node *node, void *link)
331 * Returns the link of a node.
332 * Intern version of libFirm.
334 static inline void *get_irn_link_(const ir_node *node)
336 assert(is_ir_node_(node));
341 * Returns whether the node _always_ must be pinned.
342 * I.e., the node is not floating after global cse.
344 * Intern version of libFirm.
346 static inline op_pin_state get_irn_pinned_(const ir_node *node)
349 assert(is_ir_node_(node));
351 state = get_op_pinned_(get_irn_op_(node));
353 if (state >= op_pin_state_exc_pinned)
354 return (op_pin_state)node->attr.except.pin_state;
359 static inline op_pin_state is_irn_pinned_in_irg_(const ir_node *node)
361 if (get_irg_pinned(get_irn_irg(node)) == op_pin_state_floats)
362 return get_irn_pinned(node);
363 return op_pin_state_pinned;
366 static inline int is_unop_(const ir_node *node)
368 assert(is_ir_node_(node));
369 return (node->op->opar == oparity_unary);
372 static inline int is_binop_(const ir_node *node)
374 assert(is_ir_node_(node));
375 return (node->op->opar == oparity_binary);
378 static inline int is_SymConst_addr_ent_(const ir_node *node)
380 return is_SymConst(node) && get_SymConst_kind(node) == symconst_addr_ent;
383 static inline int get_Block_n_cfgpreds_(const ir_node *node)
385 assert(is_Block_(node));
386 return get_irn_arity_(node);
389 static inline ir_node *get_Block_cfgpred_(const ir_node *node, int pos)
391 assert(is_Block_(node));
392 return get_irn_n_(node, pos);
395 /* Get the predecessor block.
397 * Returns the block corresponding to the predecessor pos.
399 * There are several ambiguities we resolve with this function:
400 * - The direct predecessor can be a Proj, which is not pinned.
401 * We walk from the predecessor to the next pinned node
402 * (skip_Proj) and return the block that node is in.
403 * - If we encounter the Bad node, this function does not return
404 * the Start block, but the Bad node.
406 static inline ir_node *get_Block_cfgpred_block_(const ir_node *node, int pos)
408 ir_node *res = get_Block_cfgpred(node, pos);
410 /* must return a Bad with mode_BB! */
411 ir_graph *irg = get_irn_irg(node);
412 return new_r_Bad(irg, mode_BB);
414 return get_nodes_block(skip_Proj(res));
418 static inline ir_visited_t get_Block_block_visited_(const ir_node *node)
420 assert(is_Block(node));
421 return node->attr.block.block_visited;
424 static inline void set_Block_block_visited_(ir_node *node, ir_visited_t visit)
426 assert(is_Block(node));
427 node->attr.block.block_visited = visit;
430 static inline ir_graph *get_Block_irg_(const ir_node *block)
432 assert(is_Block(block));
433 return block->attr.irg.irg;
436 static inline void mark_Block_block_visited_(ir_node *node)
438 ir_graph *irg = get_Block_irg(node);
439 node->attr.block.block_visited = get_irg_block_visited(irg);
442 static inline int Block_block_visited_(const ir_node *node)
444 ir_graph *irg = get_Block_irg(node);
445 return node->attr.block.block_visited >= get_irg_block_visited(irg);
448 static inline ir_tarval *get_Const_tarval_(const ir_node *node)
450 assert(is_Const(node));
451 return node->attr.con.tarval;
454 static inline int is_Const_null_(const ir_node *node)
456 return tarval_is_null(get_Const_tarval_(node));
459 static inline int is_Const_one_(const ir_node *node)
461 return tarval_is_one(get_Const_tarval_(node));
464 static inline int is_Const_all_one_(const ir_node *node)
466 return tarval_is_all_one(get_Const_tarval_(node));
469 static inline int is_irn_forking_(const ir_node *node)
471 return is_op_forking(get_irn_op_(node));
474 static inline ir_type *get_irn_type_attr_(ir_node *node)
476 return get_irn_op_(node)->ops.get_type_attr(node);
479 static inline ir_entity *get_irn_entity_attr_(ir_node *node)
481 return get_irn_op_(node)->ops.get_entity_attr(node);
484 static inline int is_irn_constlike_(const ir_node *node)
486 return is_op_constlike(get_irn_op_(node));
489 static inline int is_irn_keep_(const ir_node *node)
491 return is_op_keep(get_irn_op_(node));
494 static inline int is_irn_start_block_placed_(const ir_node *node)
496 return is_op_start_block_placed(get_irn_op_(node));
499 static inline int is_irn_cse_neutral_(const ir_node *node)
501 return is_op_cse_neutral(get_irn_op_(node));
504 static inline cond_jmp_predicate get_Cond_jmp_pred_(const ir_node *node)
506 assert(is_Cond(node));
507 return node->attr.cond.jmp_pred;
510 static inline void set_Cond_jmp_pred_(ir_node *node, cond_jmp_predicate pred)
512 assert(is_Cond(node));
513 node->attr.cond.jmp_pred = pred;
516 static inline void *get_irn_generic_attr_(ir_node *node)
521 static inline const void *get_irn_generic_attr_const_(const ir_node *node)
526 static inline dbg_info *get_irn_dbg_info_(const ir_node *n)
531 static inline void set_irn_dbg_info_(ir_node *n, dbg_info *db)
537 * Sets the Phi list of a block.
539 static inline void set_Block_phis_(ir_node *block, ir_node *phi)
541 assert(is_Block_(block));
542 assert(phi == NULL || is_Phi_(phi));
543 block->attr.block.phis = phi;
547 * Returns the link of a node.
548 * Intern version of libFirm.
550 static inline ir_node *get_Block_phis_(const ir_node *block)
552 assert(is_Block_(block));
553 return block->attr.block.phis;
556 static inline void set_Phi_next_(ir_node *phi, ir_node *next)
558 assert(is_Phi_(phi));
559 phi->attr.phi.next = next;
562 static inline ir_node *get_Phi_next_(const ir_node *phi)
564 assert(is_Phi_(phi));
565 return phi->attr.phi.next;
568 /** Add a Phi node to the list of Block Phi's. */
569 static inline void add_Block_phi_(ir_node *block, ir_node *phi)
571 assert(is_Block_(block));
572 set_Phi_next_(phi, get_Block_phis_(block));
573 set_Block_phis_(block, phi);
576 /** Get the Block mark (single bit). */
577 static inline unsigned get_Block_mark_(const ir_node *block)
579 assert(is_Block_(block));
580 return block->attr.block.marked;
583 /** Set the Block mark (single bit). */
584 static inline void set_Block_mark_(ir_node *block, unsigned mark)
586 assert(is_Block_(block));
587 block->attr.block.marked = mark;
590 /** Returns non-zero if a node is a routine parameter. */
591 static inline int is_arg_Proj_(const ir_node *node)
595 node = get_Proj_pred(node);
598 return pn_Start_T_args == get_Proj_proj(node) && is_Start(get_Proj_pred(node));
601 static inline size_t ir_switch_table_get_n_entries_(const ir_switch_table *table)
603 return table->n_entries;
606 static inline ir_switch_table_entry *ir_switch_table_get_entry(
607 ir_switch_table *table, size_t entry)
609 assert(entry < table->n_entries);
610 return &table->entries[entry];
613 static inline const ir_switch_table_entry *ir_switch_table_get_entry_const(
614 const ir_switch_table *table, size_t entry)
616 assert(entry < table->n_entries);
617 return &table->entries[entry];
620 void ir_register_getter_ops(void);
623 * because firm keepalive edges are a broken concept, we have to make sure that
624 * nodes which are only held by a keepalive edges are never moved again.
625 * This function returns true in this case.
627 bool only_used_by_keepalive(const ir_node *node);