c84aff788b7ee6c491f0b9f7a9eed7802bbc85da
[libfirm] / ir / ir / irnode_t.h
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 an intermediate operation -- private header.
23  * @author  Martin Trapp, Christian Schaefer, Goetz Lindenmaier, Michael Beck
24  */
25 #ifndef FIRM_IR_IRNODE_T_H
26 #define FIRM_IR_IRNODE_T_H
27
28 #include "irtypes.h"
29 #include "irnode.h"
30 #include "irop_t.h"
31 #include "irgraph_t.h"
32 #include "irflag_t.h"
33 #include "array.h"
34 #include "iredges_t.h"
35
36 /**
37  * Returns the array with the ins.  The content of the array may not be
38  * changed.
39  * Note that this function returns the whole in array including the
40  * block predecessor. So, it is NOT symmetric with set_irn_in().
41  */
42 ir_node **get_irn_in(const ir_node *node);
43
44 /**
45  * The amount of additional space for custom data to be allocated upon creating a new node.
46  */
47 extern unsigned firm_add_node_size;
48
49 /**
50  * Returns an array with the predecessors of the Block. Depending on
51  * the implementation of the graph data structure this can be a copy of
52  * the internal representation of predecessors as well as the internal
53  * array itself. Therefore writing to this array might obstruct the IR.
54  */
55 ir_node **get_Block_cfgpred_arr(ir_node *node);
56
57 /*-------------------------------------------------------------------*/
58 /*  These function are most used in libfirm.  Give them as static    */
59 /*  functions so they can be inlined.                                */
60 /*-------------------------------------------------------------------*/
61
62 /**
63  * Checks whether a pointer points to a ir node.
64  * Intern version for libFirm.
65  */
66 static inline int is_ir_node_(const void *thing)
67 {
68         return (get_kind(thing) == k_ir_node);
69 }
70
71 /**
72  * Gets the op of a node.
73  * Intern version for libFirm.
74  */
75 static inline ir_op *get_irn_op_(const ir_node *node)
76 {
77         assert(node);
78         return node->op;
79 }
80
81 static inline void set_irn_op_(ir_node *node, ir_op *op)
82 {
83         assert(node);
84         node->op = op;
85 }
86
87 /** Copies all attributes stored in the old node  to the new node.
88     Assumes both have the same opcode and sufficient size. */
89 static inline void copy_node_attr_(ir_graph *irg, const ir_node *old_node,
90                                    ir_node *new_node)
91 {
92         ir_op *op = get_irn_op_(old_node);
93
94         /* must always exist */
95         op->ops.copy_attr(irg, old_node, new_node);
96 }
97
98 /**
99  * Gets the opcode of a node.
100  * Intern version for libFirm.
101  */
102 static inline unsigned get_irn_opcode_(const ir_node *node)
103 {
104         assert(k_ir_node == get_kind(node));
105         assert(node->op);
106         return node->op->code;
107 }
108
109 /**
110  * Returns the number of predecessors without the block predecessor.
111  * Intern version for libFirm.
112  */
113 static inline int get_irn_arity_(const ir_node *node)
114 {
115         return (int)(ARR_LEN(node->in) - 1);
116 }
117
118 /**
119  * Intern version for libFirm.
120  */
121 static inline ir_node *get_irn_n_(const ir_node *node, int n)
122 {
123         ir_node *nn;
124
125         assert(-1 <= n && n < get_irn_arity_(node));
126
127         nn = node->in[n + 1];
128         if (nn->op != op_Id) return nn;
129
130         return (node->in[n + 1] = skip_Id(nn));
131 }
132
133 /**
134  * returns a hash value for a node
135  */
136 static inline unsigned hash_irn(const ir_node *node)
137 {
138         return (unsigned) get_irn_idx(node);
139 }
140
141 static inline int get_irn_deps_(const ir_node *node)
142 {
143         return node->deps ? (int)ARR_LEN(node->deps) : 0;
144 }
145
146 static inline ir_node *get_irn_dep_(const ir_node *node, int pos)
147 {
148         assert(node->deps && "dependency array node yet allocated. use add_irn_dep()");
149         assert(pos >= 0 && pos < (int)ARR_LEN(node->deps) && "dependency index out of range");
150         return node->deps[pos];
151 }
152
153 /* forward declaration outside iredges_t.h to avoid circular include problems */
154 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);
155
156 static inline void set_irn_dep_(ir_node *node, int pos, ir_node *dep)
157 {
158         ir_node *old;
159
160         assert(node->deps && "dependency array node yet allocated. use add_irn_dep()");
161         assert(pos >= 0 && pos < (int)ARR_LEN(node->deps) && "dependency index out of range");
162         assert(dep != NULL);
163         old = node->deps[pos];
164         node->deps[pos] = dep;
165         edges_notify_edge_kind(node, pos, dep, old, EDGE_KIND_DEP, get_irn_irg(node));
166 }
167
168
169 static inline int get_irn_ins_or_deps_(const ir_node *irn)
170 {
171         return get_irn_deps_(irn) + get_irn_arity_(irn);
172 }
173
174 static inline ir_node *get_irn_in_or_dep_(const ir_node *irn, int pos)
175 {
176         int n_in = get_irn_arity(irn);
177         return pos < n_in ? get_irn_n(irn, pos) : get_irn_dep(irn, pos - n_in);
178 }
179
180 /**
181  * Gets the mode of a node.
182  * Intern version for libFirm.
183  */
184 static inline ir_mode *get_irn_mode_(const ir_node *node)
185 {
186         assert(node);
187         return node->mode;
188 }
189
190 /**
191  * Sets the mode of a node.
192  * Intern version of libFirm.
193  */
194 static inline void set_irn_mode_(ir_node *node, ir_mode *mode)
195 {
196         assert(node);
197         node->mode = mode;
198 }
199
200 static inline int ir_has_irg_ref(const ir_node *node)
201 {
202         return is_Block(node) || is_Bad(node) || is_Anchor(node);
203 }
204
205 static inline ir_graph *get_irn_irg_(const ir_node *node)
206 {
207         if (! is_Block(node))
208                 node = get_nodes_block(node);
209         assert(ir_has_irg_ref(node));
210         return node->attr.irg.irg;
211 }
212
213 static inline ir_node *get_nodes_block_(const ir_node *node)
214 {
215         assert(!is_Block(node));
216         return get_irn_n(node, -1);
217 }
218
219 /**
220  * Gets the visited counter of a node.
221  * Intern version for libFirm.
222  */
223 static inline ir_visited_t get_irn_visited_(const ir_node *node)
224 {
225         assert(node);
226         return node->visited;
227 }
228
229 /**
230  * Sets the visited counter of a node.
231  * Intern version for libFirm.
232  */
233 static inline void set_irn_visited_(ir_node *node, ir_visited_t visited)
234 {
235         assert(node);
236         node->visited = visited;
237 }
238
239 /**
240  * Mark a node as visited in a graph.
241  * Intern version for libFirm.
242  */
243 static inline void mark_irn_visited_(ir_node *node)
244 {
245         node->visited = get_irn_irg(node)->visited;
246 }
247
248 /**
249  * Returns non-zero if a node of was visited.
250  * Intern version for libFirm.
251  */
252 static inline int irn_visited_(const ir_node *node)
253 {
254         ir_graph *irg = get_irn_irg(node);
255         return node->visited >= irg->visited;
256 }
257
258 static inline int irn_visited_else_mark_(ir_node *node)
259 {
260         if (irn_visited_(node))
261                 return 1;
262         mark_irn_visited_(node);
263         return 0;
264 }
265
266 /**
267  * Sets the link of a node.
268  * Intern version of libFirm.
269  */
270 static inline void set_irn_link_(ir_node *node, void *link)
271 {
272         assert(node);
273         node->link = link;
274 }
275
276 /**
277  * Returns the link of a node.
278  * Intern version of libFirm.
279  */
280 static inline void *get_irn_link_(const ir_node *node)
281 {
282         assert(node && is_ir_node_(node));
283         return node->link;
284 }
285
286 /**
287  * Returns whether the node _always_ must be pinned.
288  * I.e., the node is not floating after global cse.
289  *
290  * Intern version of libFirm.
291  */
292 static inline op_pin_state get_irn_pinned_(const ir_node *node)
293 {
294         op_pin_state state;
295         assert(node && is_ir_node_(node));
296         /* Check opcode */
297         state = get_op_pinned_(get_irn_op_(node));
298
299         if (state >= op_pin_state_exc_pinned)
300                 return (op_pin_state)node->attr.except.pin_state;
301
302         return state;
303 }
304
305 static inline op_pin_state is_irn_pinned_in_irg_(const ir_node *node)
306 {
307         if (get_irg_pinned(get_irn_irg(node)) == op_pin_state_floats)
308                 return get_irn_pinned(node);
309         return op_pin_state_pinned;
310 }
311
312 /* include generated code */
313 #include "gen_irnode.h"
314
315 static inline int is_unop_(const ir_node *node)
316 {
317         assert(node && is_ir_node_(node));
318         return (node->op->opar == oparity_unary);
319 }
320
321 static inline int is_binop_(const ir_node *node)
322 {
323         assert(node && is_ir_node_(node));
324         return (node->op->opar == oparity_binary);
325 }
326
327 static inline int is_strictConv_(const ir_node *node)
328 {
329         return is_Conv_(node) && get_Conv_strict(node);
330 }
331
332 static inline int is_SymConst_addr_ent_(const ir_node *node)
333 {
334         return is_SymConst(node) && get_SymConst_kind(node) == symconst_addr_ent;
335 }
336
337 static inline int get_Block_n_cfgpreds_(const ir_node *node)
338 {
339         assert(is_Block_(node));
340         return get_irn_arity_(node);
341 }
342
343 static inline ir_node *get_Block_cfgpred_(const ir_node *node, int pos)
344 {
345         assert(is_Block_(node));
346         return get_irn_n_(node, pos);
347 }
348
349 /* Get the predecessor block.
350  *
351  *  Returns the block corresponding to the predecessor pos.
352  *
353  *  There are several ambiguities we resolve with this function:
354  *  - The direct predecessor can be a Proj, which is not pinned.
355  *    We walk from the predecessor to the next pinned node
356  *    (skip_Proj) and return the block that node is in.
357  *  - If we encounter the Bad node, this function does not return
358  *    the Start block, but the Bad node.
359  */
360 static inline ir_node *get_Block_cfgpred_block_(const ir_node *node, int pos)
361 {
362         ir_node *res = get_Block_cfgpred(node, pos);
363         if (is_Bad(res)) {
364                 /* must return a Bad with mode_BB! */
365                 ir_graph *irg = get_irn_irg(node);
366                 return new_r_Bad(irg, mode_BB);
367         } else {
368                 return get_nodes_block(skip_Proj(res));
369         }
370 }
371
372 static inline ir_visited_t get_Block_block_visited_(const ir_node *node)
373 {
374         assert(is_Block(node));
375         return node->attr.block.block_visited;
376 }
377
378 static inline void set_Block_block_visited_(ir_node *node, ir_visited_t visit)
379 {
380         assert(is_Block(node));
381         node->attr.block.block_visited = visit;
382 }
383
384 static inline void mark_Block_block_visited_(ir_node *node)
385 {
386         ir_graph *irg = get_Block_irg(node);
387         node->attr.block.block_visited = get_irg_block_visited(irg);
388 }
389
390 static inline int Block_block_visited_(const ir_node *node)
391 {
392         ir_graph *irg = get_Block_irg(node);
393         return node->attr.block.block_visited >= get_irg_block_visited(irg);
394 }
395
396 static inline ir_graph *get_Block_irg_(const ir_node *block)
397 {
398         assert(is_Block(block));
399         return block->attr.irg.irg;
400 }
401
402 static inline ir_tarval *get_Const_tarval_(const ir_node *node)
403 {
404         assert(get_irn_op_(node) == op_Const);
405         return node->attr.con.tarval;
406 }
407
408 static inline int is_Const_null_(const ir_node *node)
409 {
410         return tarval_is_null(get_Const_tarval_(node));
411 }
412
413 static inline int is_Const_one_(const ir_node *node)
414 {
415         return tarval_is_one(get_Const_tarval_(node));
416 }
417
418 static inline int is_Const_all_one_(const ir_node *node)
419 {
420         return tarval_is_all_one(get_Const_tarval_(node));
421 }
422
423 static inline int is_irn_forking_(const ir_node *node)
424 {
425         return is_op_forking(get_irn_op_(node));
426 }
427
428 static inline ir_type *get_irn_type_attr_(ir_node *node)
429 {
430         return get_irn_op_(node)->ops.get_type_attr(node);
431 }
432
433 static inline ir_entity *get_irn_entity_attr_(ir_node *node)
434 {
435         return get_irn_op_(node)->ops.get_entity_attr(node);
436 }
437
438 static inline int is_irn_constlike_(const ir_node *node)
439 {
440         return is_op_constlike(get_irn_op_(node));
441 }
442
443 static inline int is_irn_keep_(const ir_node *node)
444 {
445         return is_op_keep(get_irn_op_(node));
446 }
447
448 static inline int is_irn_start_block_placed_(const ir_node *node)
449 {
450         return is_op_start_block_placed(get_irn_op_(node));
451 }
452
453 static inline int is_irn_cse_neutral_(const ir_node *node)
454 {
455         return is_op_cse_neutral(get_irn_op_(node));
456 }
457
458 static inline cond_jmp_predicate get_Cond_jmp_pred_(const ir_node *node)
459 {
460         assert(get_irn_op_(node) == op_Cond);
461         return node->attr.cond.jmp_pred;
462 }
463
464 static inline void set_Cond_jmp_pred_(ir_node *node, cond_jmp_predicate pred)
465 {
466         assert(get_irn_op_(node) == op_Cond);
467         node->attr.cond.jmp_pred = pred;
468 }
469
470 static inline void *get_irn_generic_attr_(ir_node *node)
471 {
472         return &node->attr;
473 }
474
475 static inline const void *get_irn_generic_attr_const_(const ir_node *node)
476 {
477         return &node->attr;
478 }
479
480 static inline unsigned get_irn_idx_(const ir_node *node)
481 {
482         return node->node_idx;
483 }
484
485 static inline dbg_info *get_irn_dbg_info_(const ir_node *n)
486 {
487         return n->dbi;
488 }
489
490 static inline void set_irn_dbg_info_(ir_node *n, dbg_info *db)
491 {
492         n->dbi = db;
493 }
494
495 /**
496  * Sets the Phi list of a block.
497  */
498 static inline void set_Block_phis_(ir_node *block, ir_node *phi)
499 {
500         assert(is_Block_(block));
501         assert(phi == NULL || is_Phi_(phi));
502         block->attr.block.phis = phi;
503 }
504
505 /**
506  * Returns the link of a node.
507  * Intern version of libFirm.
508  */
509 static inline ir_node *get_Block_phis_(const ir_node *block)
510 {
511         assert(is_Block_(block));
512         return block->attr.block.phis;
513 }
514
515 static inline void set_Phi_next_(ir_node *phi, ir_node *next)
516 {
517         assert(is_Phi_(phi));
518         phi->attr.phi.next = next;
519 }
520
521 static inline ir_node *get_Phi_next_(const ir_node *phi)
522 {
523         assert(is_Phi_(phi));
524         return phi->attr.phi.next;
525 }
526
527 /** Add a Phi node to the list of Block Phi's. */
528 static inline void add_Block_phi_(ir_node *block, ir_node *phi)
529 {
530         assert(is_Block_(block));
531         set_Phi_next_(phi, get_Block_phis_(block));
532         set_Block_phis_(block, phi);
533 }
534
535 /** Get the Block mark (single bit). */
536 static inline unsigned get_Block_mark_(const ir_node *block)
537 {
538         assert(is_Block_(block));
539         return block->attr.block.marked;
540 }
541
542 /** Set the Block mark (single bit). */
543 static inline void set_Block_mark_(ir_node *block, unsigned mark)
544 {
545         assert(is_Block_(block));
546         block->attr.block.marked = mark;
547 }
548
549 /** Returns non-zero if a node is a routine parameter. */
550 static inline int is_arg_Proj_(const ir_node *node)
551 {
552         if (! is_Proj(node))
553                 return 0;
554         node = get_Proj_pred(node);
555         if (! is_Proj(node))
556                 return 0;
557         return pn_Start_T_args == get_Proj_proj(node) && is_Start(get_Proj_pred(node));
558 }
559
560 static inline size_t ir_switch_table_get_n_entries_(const ir_switch_table *table)
561 {
562         return table->n_entries;
563 }
564
565 static inline ir_switch_table_entry *ir_switch_table_get_entry(
566                 ir_switch_table *table, size_t entry)
567 {
568         assert(entry < table->n_entries);
569         return &table->entries[entry];
570 }
571
572 static inline const ir_switch_table_entry *ir_switch_table_get_entry_const(
573                 const ir_switch_table *table, size_t entry)
574 {
575         assert(entry < table->n_entries);
576         return &table->entries[entry];
577 }
578
579 void ir_register_getter_ops(void);
580
581 /** initialize ir_node module */
582 void init_irnode(void);
583
584 /* this section MUST contain all inline functions */
585 #define is_ir_node(thing)                     is_ir_node_(thing)
586 #define get_irn_arity(node)                   get_irn_arity_(node)
587 #define get_irn_n(node, n)                    get_irn_n_(node, n)
588 #define get_irn_mode(node)                    get_irn_mode_(node)
589 #define set_irn_mode(node, mode)              set_irn_mode_(node, mode)
590 #define get_irn_irg(node)                     get_irn_irg_(node)
591 #define get_nodes_block(node)                 get_nodes_block_(node)
592 #define get_irn_op(node)                      get_irn_op_(node)
593 #define set_irn_op(node, op)                  set_irn_op_(node, op)
594 #define get_irn_opcode(node)                  get_irn_opcode_(node)
595 #define get_irn_visited(node)                 get_irn_visited_(node)
596 #define set_irn_visited(node, v)              set_irn_visited_(node, v)
597 #define mark_irn_visited(node)                mark_irn_visited_(node)
598 #define irn_visited(node)                     irn_visited_(node)
599 #define irn_visited_else_mark(node)           irn_visited_else_mark_(node)
600 #define set_irn_link(node, link)              set_irn_link_(node, link)
601 #define get_irn_link(node)                    get_irn_link_(node)
602 #define get_irn_pinned(node)                  get_irn_pinned_(node)
603 #define is_irn_pinned_in_irg(node)            is_irn_pinned_in_irg_(node)
604 #define is_unop(node)                         is_unop_(node)
605 #define is_binop(node)                        is_binop_(node)
606 #define is_Proj(node)                         is_Proj_(node)
607 #define is_Phi(node)                          is_Phi_(node)
608 #define is_strictConv(node)                   is_strictConv_(node)
609 #define is_SymConst_addr_ent(node)            is_SymConst_addr_ent_(node)
610 #define get_Block_n_cfgpreds(node)            get_Block_n_cfgpreds_(node)
611 #define get_Block_cfgpred(node, pos)          get_Block_cfgpred_(node, pos)
612 #define get_Block_cfgpred_block(node, pos)    get_Block_cfgpred_block_(node, pos)
613 #define get_Block_block_visited(node)         get_Block_block_visited_(node)
614 #define set_Block_block_visited(node, visit)  set_Block_block_visited_(node, visit)
615 #define mark_Block_block_visited(node)        mark_Block_block_visited_(node)
616 #define Block_block_visited(node)             Block_block_visited_(node)
617 #define get_Block_irg(block)                  get_Block_irg_(block)
618 #define get_Const_tarval(node)                get_Const_tarval_(node)
619 #define is_Const_null(node)                   is_Const_null_(node)
620 #define is_Const_one(node)                    is_Const_one_(node)
621 #define is_Const_all_one(node)                is_Const_all_one_(node)
622 #define is_irn_forking(node)                  is_irn_forking_(node)
623 #define copy_node_attr(irg,oldn,newn)         copy_node_attr_(irg,oldn,newn)
624 #define get_irn_type(node)                    get_irn_type_(node)
625 #define get_irn_type_attr(node)               get_irn_type_attr_(node)
626 #define get_irn_entity_attr(node)             get_irn_entity_attr_(node)
627 #define is_irn_constlike(node)                is_irn_constlike_(node)
628 #define is_irn_keep(node)                     is_irn_keep_(node)
629 #define is_irn_start_block_placed(node)       is_irn_start_block_placed_(node)
630 #define is_irn_cse_neutral(node)              is_irn_cse_neutral_(node)
631 #define get_Cond_jmp_pred(node)               get_Cond_jmp_pred_(node)
632 #define set_Cond_jmp_pred(node, pred)         set_Cond_jmp_pred_(node, pred)
633 #define get_irn_generic_attr(node)            get_irn_generic_attr_(node)
634 #define get_irn_generic_attr_const(node)      get_irn_generic_attr_const_(node)
635 #define get_irn_idx(node)                     get_irn_idx_(node)
636
637 #define get_irn_deps(node)                    get_irn_deps_(node)
638 #define set_irn_dep(node, pos, dep)           set_irn_dep_(node, pos, dep)
639 #define get_irn_dep(node, pos)                get_irn_dep_(node, pos)
640
641 #define get_irn_ins_or_deps(node)             get_irn_ins_or_deps_(node)
642 #define get_irn_in_or_dep(node, pos)          get_irn_in_or_dep_(node, pos)
643
644 #define get_irn_dbg_info(node)                get_irn_dbg_info_(node)
645 #define set_irn_dbg_info(node, db)            set_irn_dbg_info_(node, db)
646
647 #define set_Block_phis(block, phi)            set_Block_phis_(block, phi)
648 #define get_Block_phis(block)                 get_Block_phis_(block)
649 #define add_Block_phi(block, phi)             add_Block_phi_(block, phi)
650 #define get_Block_mark(block)                 get_Block_mark_(block)
651 #define set_Block_mark(block, mark)           set_Block_mark_(block, mark)
652
653 #define set_Phi_next(node, phi)               set_Phi_next_(node, phi)
654 #define get_Phi_next(node)                    get_Phi_next_(node)
655
656 #define is_arg_Proj(node)                     is_arg_Proj_(node)
657 #define ir_switch_table_get_n_entries(table)  ir_switch_table_get_n_entries_(table)
658
659 #endif