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