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