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