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