376a68e48503c78913683416a71c90e5edf1cab3
[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 == NULL) {
152                 /* only block and Anchor inputs are allowed to be NULL */
153                 assert((is_Anchor(node) || n == -1) && "NULL input of a node");
154                 return NULL;
155         }
156         if (nn->op != op_Id) return nn;
157
158         return (node->in[n + 1] = skip_Id(nn));
159 }
160
161 /**
162  * returns a hash value for a node
163  */
164 static inline unsigned hash_irn(const ir_node *node)
165 {
166         return (unsigned) get_irn_idx(node);
167 }
168
169 static inline int _get_irn_deps(const ir_node *node) {
170         return node->deps ? (int)ARR_LEN(node->deps) : 0;
171 }
172
173 static inline ir_node *_get_irn_dep(const ir_node *node, int pos) {
174         assert(node->deps && "dependency array node yet allocated. use add_irn_dep()");
175         assert(pos >= 0 && pos < (int)ARR_LEN(node->deps) && "dependency index out of range");
176         return node->deps[pos];
177 }
178
179 /* forward declaration outside iredges_t.h to avoid circular include problems */
180 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);
181
182 static inline void _set_irn_dep(ir_node *node, int pos, ir_node *dep)
183 {
184         ir_node *old;
185
186         assert(node->deps && "dependency array node yet allocated. use add_irn_dep()");
187         assert(pos >= 0 && pos < (int)ARR_LEN(node->deps) && "dependency index out of range");
188         old = node->deps[pos];
189         node->deps[pos] = dep;
190         edges_notify_edge_kind(node, pos, dep, old, EDGE_KIND_DEP, get_irn_irg(node));
191 }
192
193
194 static inline int _get_irn_ins_or_deps(const ir_node *irn)
195 {
196         return _get_irn_deps(irn) + _get_irn_arity(irn);
197 }
198
199 static inline ir_node *_get_irn_in_or_dep(const ir_node *irn, int pos)
200 {
201         int n_in = get_irn_arity(irn);
202         return pos < n_in ? get_irn_n(irn, pos) : get_irn_dep(irn, pos - n_in);
203 }
204
205 /**
206  * Gets the mode of a node.
207  * Intern version for libFirm.
208  */
209 static inline ir_mode *_get_irn_mode(const ir_node *node)
210 {
211         assert(node);
212         return node->mode;
213 }
214
215 /**
216  * Sets the mode of a node.
217  * Intern version of libFirm.
218  */
219 static inline void _set_irn_mode(ir_node *node, ir_mode *mode)
220 {
221         assert(node);
222         node->mode = mode;
223 }
224
225 static inline int ir_has_irg_ref(const ir_node *node)
226 {
227         return is_Block(node) || is_Bad(node) || is_Anchor(node);
228 }
229
230 static inline ir_graph *_get_irn_irg(const ir_node *node)
231 {
232         /*
233          * Do not use get_nodes_block() here, because this
234          * will check the pinned state.
235          * However even a 'wrong' block is always in the proper irg.
236          */
237         if (! is_Block(node))
238                 node = get_irn_n(node, -1);
239         assert(ir_has_irg_ref(node));
240         return node->attr.irg.irg;
241 }
242
243 static inline ir_node *_get_nodes_block(const ir_node *node)
244 {
245         assert(!is_Block(node));
246         return get_irn_n(node, -1);
247 }
248
249 /**
250  * Gets the visited counter of a node.
251  * Intern version for libFirm.
252  */
253 static inline ir_visited_t _get_irn_visited(const ir_node *node)
254 {
255         assert(node);
256         return node->visited;
257 }
258
259 /**
260  * Sets the visited counter of a node.
261  * Intern version for libFirm.
262  */
263 static inline void _set_irn_visited(ir_node *node, ir_visited_t visited)
264 {
265         assert(node);
266         node->visited = visited;
267 }
268
269 /**
270  * Mark a node as visited in a graph.
271  * Intern version for libFirm.
272  */
273 static inline void _mark_irn_visited(ir_node *node)
274 {
275         node->visited = get_irn_irg(node)->visited;
276 }
277
278 /**
279  * Returns non-zero if a node of was visited.
280  * Intern version for libFirm.
281  */
282 static inline int _irn_visited(const ir_node *node)
283 {
284         ir_graph *irg = get_irn_irg(node);
285         return node->visited >= irg->visited;
286 }
287
288 static inline int _irn_visited_else_mark(ir_node *node)
289 {
290         if (_irn_visited(node))
291                 return 1;
292         _mark_irn_visited(node);
293         return 0;
294 }
295
296 /**
297  * Sets the link of a node.
298  * Intern version of libFirm.
299  */
300 static inline void _set_irn_link(ir_node *node, void *link)
301 {
302         assert(node);
303         node->link = link;
304 }
305
306 /**
307  * Returns the link of a node.
308  * Intern version of libFirm.
309  */
310 static inline void *_get_irn_link(const ir_node *node)
311 {
312         assert(node && _is_ir_node(node));
313         return node->link;
314 }
315
316 /**
317  * Returns whether the node _always_ must be pinned.
318  * I.e., the node is not floating after global cse.
319  *
320  * Intern version of libFirm.
321  */
322 static inline op_pin_state _get_irn_pinned(const ir_node *node)
323 {
324         op_pin_state state;
325         assert(node && _is_ir_node(node));
326         /* Check opcode */
327         state = _get_op_pinned(_get_irn_op(node));
328
329         if (state >= op_pin_state_exc_pinned)
330                 return (op_pin_state)node->attr.except.pin_state;
331
332         return state;
333 }
334
335 static inline op_pin_state _is_irn_pinned_in_irg(const ir_node *node)
336 {
337         if (get_irg_pinned(get_irn_irg(node)) == op_pin_state_floats)
338                 return get_irn_pinned(node);
339         return op_pin_state_pinned;
340 }
341
342 /* include generated code */
343 #include "gen_irnode.h"
344
345 static inline int _is_unop(const ir_node *node)
346 {
347         assert(node && _is_ir_node(node));
348         return (node->op->opar == oparity_unary);
349 }
350
351 static inline int _is_binop(const ir_node *node)
352 {
353         assert(node && _is_ir_node(node));
354         return (node->op->opar == oparity_binary);
355 }
356
357 static inline int _is_strictConv(const ir_node *node)
358 {
359         return _is_Conv(node) && get_Conv_strict(node);
360 }
361
362 static inline int _is_SymConst_addr_ent(const ir_node *node)
363 {
364         return is_SymConst(node) && get_SymConst_kind(node) == symconst_addr_ent;
365 }
366
367 static inline int _get_Block_n_cfgpreds(const ir_node *node)
368 {
369         assert(_is_Block(node));
370         return _get_irn_arity(node);
371 }
372
373 static inline ir_node *_get_Block_cfgpred(const ir_node *node, int pos)
374 {
375         assert(0 <= pos && pos < get_irn_arity(node));
376         assert(_is_Block(node));
377         return _get_irn_n(node, pos);
378 }
379
380 /* Get the predecessor block.
381  *
382  *  Returns the block corresponding to the predecessor pos.
383  *
384  *  There are several ambiguities we resolve with this function:
385  *  - The direct predecessor can be a Proj, which is not pinned.
386  *    We walk from the predecessor to the next pinned node
387  *    (skip_Proj) and return the block that node is in.
388  *  - If we encounter the Bad node, this function does not return
389  *    the Start block, but the Bad node.
390  */
391 static inline ir_node  *_get_Block_cfgpred_block(const ir_node *node, int pos)
392 {
393         ir_node *res = get_Block_cfgpred(node, pos);
394         if (is_Bad(res)) {
395                 /* must return a Bad with mode_BB! */
396                 ir_graph *irg = get_irn_irg(node);
397                 return new_r_Bad(irg, mode_BB);
398         } else {
399                 return get_nodes_block(skip_Proj(res));
400         }
401 }
402
403 static inline ir_visited_t _get_Block_block_visited(const ir_node *node)
404 {
405         assert(is_Block(node));
406         return node->attr.block.block_visited;
407 }
408
409 static inline void _set_Block_block_visited(ir_node *node, ir_visited_t visit)
410 {
411         assert(is_Block(node));
412         node->attr.block.block_visited = visit;
413 }
414
415 static inline void _mark_Block_block_visited(ir_node *node)
416 {
417         ir_graph *irg = get_Block_irg(node);
418         node->attr.block.block_visited = get_irg_block_visited(irg);
419 }
420
421 static inline int _Block_block_visited(const ir_node *node)
422 {
423         ir_graph *irg = get_Block_irg(node);
424         return node->attr.block.block_visited >= get_irg_block_visited(irg);
425 }
426
427 static inline ir_graph *_get_Block_irg(const ir_node *block)
428 {
429         assert(is_Block(block));
430         return block->attr.irg.irg;
431 }
432
433 static inline ir_tarval *_get_Const_tarval(const ir_node *node) {
434         assert(_get_irn_op(node) == op_Const);
435         return node->attr.con.tarval;
436 }
437
438 static inline int _is_Const_null(const ir_node *node) {
439         return tarval_is_null(_get_Const_tarval(node));
440 }
441
442 static inline int _is_Const_one(const ir_node *node) {
443         return tarval_is_one(_get_Const_tarval(node));
444 }
445
446 static inline int _is_Const_all_one(const ir_node *node) {
447         return tarval_is_all_one(_get_Const_tarval(node));
448 }
449
450 static inline int _is_irn_forking(const ir_node *node) {
451         return is_op_forking(_get_irn_op(node));
452 }
453
454 static inline ir_type *_get_irn_type_attr(ir_node *node) {
455         return _get_irn_op(node)->ops.get_type_attr(node);
456 }
457
458 static inline ir_entity *_get_irn_entity_attr(ir_node *node) {
459   return _get_irn_op(node)->ops.get_entity_attr(node);
460 }
461
462 static inline int _is_irn_constlike(const ir_node *node) {
463         return is_op_constlike(_get_irn_op(node));
464 }
465
466 static inline int _is_irn_always_opt(const ir_node *node) {
467         return is_op_always_opt(_get_irn_op(node));
468 }
469
470 static inline int _is_irn_keep(const ir_node *node) {
471         return is_op_keep(_get_irn_op(node));
472 }
473
474 static inline int _is_irn_start_block_placed(const ir_node *node) {
475         return is_op_start_block_placed(_get_irn_op(node));
476 }
477
478 static inline int _is_irn_machine_op(const ir_node *node) {
479         return is_op_machine(_get_irn_op(node));
480 }
481
482 static inline int _is_irn_machine_operand(const ir_node *node) {
483         return is_op_machine_operand(_get_irn_op(node));
484 }
485
486 static inline int _is_irn_machine_user(const ir_node *node, unsigned n) {
487         return is_op_machine_user(_get_irn_op(node), n);
488 }
489
490 static inline int _is_irn_cse_neutral(const ir_node *node) {
491         return is_op_cse_neutral(_get_irn_op(node));
492 }
493
494 static inline cond_jmp_predicate _get_Cond_jmp_pred(const ir_node *node) {
495         assert(_get_irn_op(node) == op_Cond);
496         return node->attr.cond.jmp_pred;
497 }
498
499 static inline void _set_Cond_jmp_pred(ir_node *node, cond_jmp_predicate pred) {
500         assert(_get_irn_op(node) == op_Cond);
501         node->attr.cond.jmp_pred = pred;
502 }
503
504 static inline void *_get_irn_generic_attr(ir_node *node) {
505         return &node->attr;
506 }
507
508 static inline const void *_get_irn_generic_attr_const(const ir_node *node) {
509         return &node->attr;
510 }
511
512 static inline unsigned _get_irn_idx(const ir_node *node) {
513         return node->node_idx;
514 }
515
516 static inline dbg_info *_get_irn_dbg_info(const ir_node *n) {
517         return n->dbi;
518 }  /* get_irn_dbg_info */
519
520 static inline void _set_irn_dbg_info(ir_node *n, dbg_info *db) {
521         n->dbi = db;
522 }
523
524 /**
525  * Sets the Phi list of a block.
526  */
527 static inline void _set_Block_phis(ir_node *block, ir_node *phi)
528 {
529         assert(_is_Block(block));
530         assert(phi == NULL || _is_Phi(phi));
531         block->attr.block.phis = phi;
532 }
533
534 /**
535  * Returns the link of a node.
536  * Intern version of libFirm.
537  */
538 static inline ir_node *_get_Block_phis(const ir_node *block)
539 {
540         assert(_is_Block(block));
541         return block->attr.block.phis;
542 }
543
544 static inline void _set_Phi_next(ir_node *phi, ir_node *next)
545 {
546         assert(_is_Phi(phi));
547         phi->attr.phi.next = next;
548 }
549
550 static inline ir_node *_get_Phi_next(const ir_node *phi)
551 {
552         assert(_is_Phi(phi));
553         return phi->attr.phi.next;
554 }
555
556 /** Add a Phi node to the list of Block Phi's. */
557 static inline void _add_Block_phi(ir_node *block, ir_node *phi)
558 {
559         assert(_is_Block(block));
560         _set_Phi_next(phi, _get_Block_phis(block));
561         _set_Block_phis(block, phi);
562 }
563
564 /** Get the Block mark (single bit). */
565 static inline unsigned _get_Block_mark(const ir_node *block)
566 {
567         assert(_is_Block(block));
568         return block->attr.block.marked;
569 }
570
571 /** Set the Block mark (single bit). */
572 static inline void _set_Block_mark(ir_node *block, unsigned mark)
573 {
574         assert(_is_Block(block));
575         block->attr.block.marked = mark;
576 }
577
578 /** Returns non-zero if a node is a routine parameter. */
579 static inline int _is_arg_Proj(const ir_node *node)
580 {
581         if (! is_Proj(node))
582                 return 0;
583         node = get_Proj_pred(node);
584         if (! is_Proj(node))
585                 return 0;
586         return pn_Start_T_args == get_Proj_proj(node) && is_Start(get_Proj_pred(node));
587 }
588
589 /** initialize ir_node module */
590 void init_irnode(void);
591
592 /* this section MUST contain all inline functions */
593 #define is_ir_node(thing)                     _is_ir_node(thing)
594 #define get_irn_arity(node)                   _get_irn_arity(node)
595 #define get_irn_n(node, n)                    _get_irn_n(node, n)
596 #define get_irn_mode(node)                    _get_irn_mode(node)
597 #define set_irn_mode(node, mode)              _set_irn_mode(node, mode)
598 #define get_irn_irg(node)                     _get_irn_irg(node)
599 #define get_nodes_block(node)                 _get_nodes_block(node)
600 #define get_irn_op(node)                      _get_irn_op(node)
601 #define set_irn_op(node, op)                  _set_irn_op(node, op)
602 #define get_irn_opcode(node)                  _get_irn_opcode(node)
603 #define get_irn_visited(node)                 _get_irn_visited(node)
604 #define set_irn_visited(node, v)              _set_irn_visited(node, v)
605 #define mark_irn_visited(node)                _mark_irn_visited(node)
606 #define irn_visited(node)                     _irn_visited(node)
607 #define irn_visited_else_mark(node)           _irn_visited_else_mark(node)
608 #define set_irn_link(node, link)              _set_irn_link(node, link)
609 #define get_irn_link(node)                    _get_irn_link(node)
610 #define get_irn_pinned(node)                  _get_irn_pinned(node)
611 #define is_irn_pinned_in_irg(node)            _is_irn_pinned_in_irg(node)
612 #define is_unop(node)                         _is_unop(node)
613 #define is_binop(node)                        _is_binop(node)
614 #define is_Proj(node)                         _is_Proj(node)
615 #define is_Phi(node)                          _is_Phi(node)
616 #define is_strictConv(node)                   _is_strictConv(node)
617 #define is_SymConst_addr_ent(node)            _is_SymConst_addr_ent(node)
618 #define get_Block_n_cfgpreds(node)            _get_Block_n_cfgpreds(node)
619 #define get_Block_cfgpred(node, pos)          _get_Block_cfgpred(node, pos)
620 #define get_Block_cfgpred_block(node, pos)    _get_Block_cfgpred_block(node, pos)
621 #define get_Block_block_visited(node)         _get_Block_block_visited(node)
622 #define set_Block_block_visited(node, visit)  _set_Block_block_visited(node, visit)
623 #define mark_Block_block_visited(node)        _mark_Block_block_visited(node)
624 #define Block_block_visited(node)             _Block_block_visited(node)
625 #define get_Block_irg(block)                  _get_Block_irg(block)
626 #define get_Const_tarval(node)                _get_Const_tarval(node)
627 #define is_Const_null(node)                   _is_Const_null(node)
628 #define is_Const_one(node)                    _is_Const_one(node)
629 #define is_Const_all_one(node)                _is_Const_all_one(node)
630 #define is_irn_forking(node)                  _is_irn_forking(node)
631 #define copy_node_attr(irg,oldn,newn)         _copy_node_attr(irg,oldn,newn)
632 #define get_irn_type(node)                    _get_irn_type(node)
633 #define get_irn_type_attr(node)               _get_irn_type_attr(node)
634 #define get_irn_entity_attr(node)             _get_irn_entity_attr(node)
635 #define is_irn_constlike(node)                _is_irn_constlike(node)
636 #define is_irn_always_opt(node)               _is_irn_always_opt(node)
637 #define is_irn_keep(node)                     _is_irn_keep(node)
638 #define is_irn_start_block_placed(node)       _is_irn_start_block_placed(node)
639 #define is_irn_machine_op(node)               _is_irn_machine_op(node)
640 #define is_irn_machine_operand(node)          _is_irn_machine_operand(node)
641 #define is_irn_machine_user(node, n)          _is_irn_machine_user(node, n)
642 #define is_irn_cse_neutral(node)              _is_irn_cse_neutral(node)
643 #define get_Cond_jmp_pred(node)               _get_Cond_jmp_pred(node)
644 #define set_Cond_jmp_pred(node, pred)         _set_Cond_jmp_pred(node, pred)
645 #define get_irn_generic_attr(node)            _get_irn_generic_attr(node)
646 #define get_irn_generic_attr_const(node)      _get_irn_generic_attr_const(node)
647 #define get_irn_idx(node)                     _get_irn_idx(node)
648
649 #define get_irn_deps(node)                    _get_irn_deps(node)
650 #define set_irn_dep(node, pos, dep)           _set_irn_dep(node, pos, dep)
651 #define get_irn_dep(node, pos)                _get_irn_dep(node, pos)
652
653 #define get_irn_ins_or_deps(node)             _get_irn_ins_or_deps(node)
654 #define get_irn_in_or_dep(node, pos)          _get_irn_in_or_dep(node, pos)
655
656 #define get_irn_dbg_info(node)                _get_irn_dbg_info(node)
657 #define set_irn_dbg_info(node, db)            _set_irn_dbg_info(node, db)
658
659 #define set_Block_phis(block, phi)            _set_Block_phis(block, phi)
660 #define get_Block_phis(block)                 _get_Block_phis(block)
661 #define add_Block_phi(block, phi)             _add_Block_phi(block, phi)
662 #define get_Block_mark(block)                 _get_Block_mark(block)
663 #define set_Block_mark(block, mark)           _set_Block_mark(block, mark)
664
665 #define set_Phi_next(node, phi)               _set_Phi_next(node, phi)
666 #define get_Phi_next(node)                    _get_Phi_next(node)
667
668 #define is_arg_Proj(node)                     _is_arg_Proj(node)
669
670 #endif