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