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