differentiate between Bad and Deleted (because of exchange) nodes, this avoid some...
[libfirm] / ir / ir / irnode.c
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.
23  * @author  Martin Trapp, Christian Schaefer, Goetz Lindenmaier, Michael Beck
24  * @version $Id$
25  */
26 #include "config.h"
27
28 #include <string.h>
29
30 #include "pset_new.h"
31 #include "ident.h"
32 #include "irnode_t.h"
33 #include "irgraph_t.h"
34 #include "irmode_t.h"
35 #include "irbackedge_t.h"
36 #include "irdump.h"
37 #include "irop_t.h"
38 #include "irprog_t.h"
39 #include "iredgekinds.h"
40 #include "iredges_t.h"
41 #include "ircons.h"
42 #include "error.h"
43
44 #include "irhooks.h"
45 #include "irtools.h"
46
47 #include "beinfo.h"
48
49 /* some constants fixing the positions of nodes predecessors
50    in the in array */
51 #define CALL_PARAM_OFFSET     2
52 #define BUILDIN_PARAM_OFFSET  1
53 #define SEL_INDEX_OFFSET      2
54 #define RETURN_RESULT_OFFSET  1  /* mem is not a result */
55 #define END_KEEPALIVE_OFFSET  0
56
57 static const char *pnc_name_arr [] = {
58         "pn_Cmp_False", "pn_Cmp_Eq", "pn_Cmp_Lt", "pn_Cmp_Le",
59         "pn_Cmp_Gt", "pn_Cmp_Ge", "pn_Cmp_Lg", "pn_Cmp_Leg",
60         "pn_Cmp_Uo", "pn_Cmp_Ue", "pn_Cmp_Ul", "pn_Cmp_Ule",
61         "pn_Cmp_Ug", "pn_Cmp_Uge", "pn_Cmp_Ne", "pn_Cmp_True"
62 };
63
64 /**
65  * returns the pnc name from an pnc constant
66  */
67 const char *get_pnc_string(int pnc)
68 {
69         assert(pnc >= 0 && pnc <
70                         (int) (sizeof(pnc_name_arr)/sizeof(pnc_name_arr[0])));
71         return pnc_name_arr[pnc];
72 }
73
74 /*
75  * Calculates the negated (Complement(R)) pnc condition.
76  */
77 pn_Cmp get_negated_pnc(long pnc, ir_mode *mode)
78 {
79         pnc ^= pn_Cmp_True;
80
81         /* do NOT add the Uo bit for non-floating point values */
82         if (! mode_is_float(mode))
83                 pnc &= ~pn_Cmp_Uo;
84
85         return (pn_Cmp) pnc;
86 }
87
88 /* Calculates the inversed (R^-1) pnc condition, i.e., "<" --> ">" */
89 pn_Cmp get_inversed_pnc(long pnc)
90 {
91         long code    = pnc & ~(pn_Cmp_Lt|pn_Cmp_Gt);
92         long lesser  = pnc & pn_Cmp_Lt;
93         long greater = pnc & pn_Cmp_Gt;
94
95         code |= (lesser ? pn_Cmp_Gt : 0) | (greater ? pn_Cmp_Lt : 0);
96
97         return (pn_Cmp) code;
98 }
99
100 /**
101  * Indicates, whether additional data can be registered to ir nodes.
102  * If set to 1, this is not possible anymore.
103  */
104 static int forbid_new_data = 0;
105
106 /**
107  * The amount of additional space for custom data to be allocated upon
108  * creating a new node.
109  */
110 unsigned firm_add_node_size = 0;
111
112
113 /* register new space for every node */
114 unsigned firm_register_additional_node_data(unsigned size)
115 {
116         assert(!forbid_new_data && "Too late to register additional node data");
117
118         if (forbid_new_data)
119                 return 0;
120
121         return firm_add_node_size += size;
122 }
123
124
125 void init_irnode(void)
126 {
127         /* Forbid the addition of new data to an ir node. */
128         forbid_new_data = 1;
129 }
130
131 struct struct_align {
132         char c;
133         struct s {
134                 int i;
135                 float f;
136                 double d;
137         } s;
138 };
139
140 /*
141  * irnode constructor.
142  * Create a new irnode in irg, with an op, mode, arity and
143  * some incoming irnodes.
144  * If arity is negative, a node with a dynamic array is created.
145  */
146 ir_node *new_ir_node(dbg_info *db, ir_graph *irg, ir_node *block, ir_op *op,
147                      ir_mode *mode, int arity, ir_node **in)
148 {
149         ir_node *res;
150         unsigned align = offsetof(struct struct_align, s) - 1;
151         unsigned add_node_size = (firm_add_node_size + align) & ~align;
152         size_t node_size = offsetof(ir_node, attr) + op->attr_size + add_node_size;
153         char *p;
154         int i;
155
156         assert(irg);
157         assert(op);
158         assert(mode);
159         p = obstack_alloc(irg->obst, node_size);
160         memset(p, 0, node_size);
161         res = (ir_node *)(p + add_node_size);
162
163         res->kind     = k_ir_node;
164         res->op       = op;
165         res->mode     = mode;
166         res->visited  = 0;
167         res->node_idx = irg_register_node_idx(irg, res);
168         res->link     = NULL;
169         res->deps     = NULL;
170
171         if (arity < 0) {
172                 res->in = NEW_ARR_F(ir_node *, 1);  /* 1: space for block */
173         } else {
174                 /* not nice but necessary: End and Sync must always have a flexible array */
175                 if (op == op_End || op == op_Sync)
176                         res->in = NEW_ARR_F(ir_node *, (arity+1));
177                 else
178                         res->in = NEW_ARR_D(ir_node *, irg->obst, (arity+1));
179                 memcpy(&res->in[1], in, sizeof(ir_node *) * arity);
180         }
181
182         res->in[0]   = block;
183         set_irn_dbg_info(res, db);
184         res->out     = NULL;
185         res->node_nr = get_irp_new_node_nr();
186
187         for (i = 0; i < EDGE_KIND_LAST; ++i) {
188                 INIT_LIST_HEAD(&res->edge_info[i].outs_head);
189                 /* edges will be build immediately */
190                 res->edge_info[i].edges_built = 1;
191                 res->edge_info[i].out_count = 0;
192         }
193
194         /* don't put this into the for loop, arity is -1 for some nodes! */
195         edges_notify_edge(res, -1, res->in[0], NULL, irg);
196         for (i = 1; i <= arity; ++i)
197                 edges_notify_edge(res, i - 1, res->in[i], NULL, irg);
198
199         hook_new_node(irg, res);
200         if (get_irg_phase_state(irg) == phase_backend) {
201                 be_info_new_node(res);
202         }
203
204         return res;
205 }
206
207 /*-- getting some parameters from ir_nodes --*/
208
209 int (is_ir_node)(const void *thing)
210 {
211         return _is_ir_node(thing);
212 }
213
214 int (get_irn_arity)(const ir_node *node)
215 {
216         return _get_irn_arity(node);
217 }
218
219 /* Returns the array with ins. This array is shifted with respect to the
220    array accessed by get_irn_n: The block operand is at position 0 not -1.
221    (@@@ This should be changed.)
222    The order of the predecessors in this array is not guaranteed, except that
223    lists of operands as predecessors of Block or arguments of a Call are
224    consecutive. */
225 ir_node **get_irn_in(const ir_node *node)
226 {
227         return node->in;
228 }
229
230 void set_irn_in(ir_node *node, int arity, ir_node **in)
231 {
232         int i;
233         ir_node *** pOld_in;
234         ir_graph *irg = get_irn_irg(node);
235
236         pOld_in = &node->in;
237
238
239         for (i = 0; i < arity; i++) {
240                 if (i < ARR_LEN(*pOld_in)-1)
241                         edges_notify_edge(node, i, in[i], (*pOld_in)[i+1], irg);
242                 else
243                         edges_notify_edge(node, i, in[i], NULL,            irg);
244         }
245         for (;i < ARR_LEN(*pOld_in)-1; i++) {
246                 edges_notify_edge(node, i, NULL, (*pOld_in)[i+1], irg);
247         }
248
249         if (arity != ARR_LEN(*pOld_in) - 1) {
250                 ir_node * block = (*pOld_in)[0];
251                 *pOld_in = NEW_ARR_D(ir_node *, irg->obst, arity + 1);
252                 (*pOld_in)[0] = block;
253         }
254         fix_backedges(irg->obst, node);
255
256         memcpy((*pOld_in) + 1, in, sizeof(ir_node *) * arity);
257 }
258
259 ir_node *(get_irn_n)(const ir_node *node, int n)
260 {
261         return _get_irn_n(node, n);
262 }
263
264 void set_irn_n(ir_node *node, int n, ir_node *in)
265 {
266         ir_graph *irg = get_irn_irg(node);
267         assert(node && node->kind == k_ir_node);
268         assert(-1 <= n);
269         assert(n < get_irn_arity(node));
270         assert(in && in->kind == k_ir_node);
271
272         /* Call the hook */
273         hook_set_irn_n(node, n, in, node->in[n + 1]);
274
275         /* Here, we rely on src and tgt being in the current ir graph */
276         edges_notify_edge(node, n, in, node->in[n + 1], irg);
277
278         node->in[n + 1] = in;
279 }
280
281 int add_irn_n(ir_node *node, ir_node *in)
282 {
283         int pos;
284         ir_graph *irg = get_irn_irg(node);
285
286         assert(node->op->opar == oparity_dynamic);
287         pos = ARR_LEN(node->in) - 1;
288         ARR_APP1(ir_node *, node->in, in);
289         edges_notify_edge(node, pos, node->in[pos + 1], NULL, irg);
290
291         /* Call the hook */
292         hook_set_irn_n(node, pos, node->in[pos + 1], NULL);
293
294         return pos;
295 }
296
297 void del_Sync_n(ir_node *n, int i)
298 {
299         int      arity     = get_Sync_n_preds(n);
300         ir_node *last_pred = get_Sync_pred(n, arity - 1);
301         set_Sync_pred(n, i, last_pred);
302         edges_notify_edge(n, arity - 1, NULL, last_pred, get_irn_irg(n));
303         ARR_SHRINKLEN(get_irn_in(n), arity);
304 }
305
306 int (get_irn_deps)(const ir_node *node)
307 {
308         return _get_irn_deps(node);
309 }
310
311 ir_node *(get_irn_dep)(const ir_node *node, int pos)
312 {
313         return _get_irn_dep(node, pos);
314 }
315
316 void (set_irn_dep)(ir_node *node, int pos, ir_node *dep)
317 {
318         _set_irn_dep(node, pos, dep);
319 }
320
321 int add_irn_dep(ir_node *node, ir_node *dep)
322 {
323         int res = 0;
324
325         /* DEP edges are only allowed in backend phase */
326         assert(get_irg_phase_state(get_irn_irg(node)) == phase_backend);
327         if (node->deps == NULL) {
328                 node->deps = NEW_ARR_F(ir_node *, 1);
329                 node->deps[0] = dep;
330         } else {
331                 int i, n;
332                 int first_zero = -1;
333
334                 for (i = 0, n = ARR_LEN(node->deps); i < n; ++i) {
335                         if (node->deps[i] == NULL)
336                                 first_zero = i;
337
338                         if (node->deps[i] == dep)
339                                 return i;
340                 }
341
342                 if (first_zero >= 0) {
343                         node->deps[first_zero] = dep;
344                         res = first_zero;
345                 } else {
346                         ARR_APP1(ir_node *, node->deps, dep);
347                         res = n;
348                 }
349         }
350
351         edges_notify_edge_kind(node, res, dep, NULL, EDGE_KIND_DEP, get_irn_irg(node));
352
353         return res;
354 }
355
356 void add_irn_deps(ir_node *tgt, ir_node *src)
357 {
358         int i, n;
359
360         for (i = 0, n = get_irn_deps(src); i < n; ++i)
361                 add_irn_dep(tgt, get_irn_dep(src, i));
362 }
363
364
365 ir_mode *(get_irn_mode)(const ir_node *node)
366 {
367         return _get_irn_mode(node);
368 }
369
370 void (set_irn_mode)(ir_node *node, ir_mode *mode)
371 {
372         _set_irn_mode(node, mode);
373 }
374
375 /** Gets the string representation of the mode .*/
376 const char *get_irn_modename(const ir_node *node)
377 {
378         assert(node);
379         return get_mode_name(node->mode);
380 }
381
382 ident *get_irn_modeident(const ir_node *node)
383 {
384         assert(node);
385         return get_mode_ident(node->mode);
386 }
387
388 ir_op *(get_irn_op)(const ir_node *node)
389 {
390         return _get_irn_op(node);
391 }
392
393 /* should be private to the library: */
394 void (set_irn_op)(ir_node *node, ir_op *op)
395 {
396         _set_irn_op(node, op);
397 }
398
399 unsigned (get_irn_opcode)(const ir_node *node)
400 {
401         return _get_irn_opcode(node);
402 }
403
404 const char *get_irn_opname(const ir_node *node)
405 {
406         assert(node);
407         if (is_Phi0(node)) return "Phi0";
408         return get_id_str(node->op->name);
409 }
410
411 ident *get_irn_opident(const ir_node *node)
412 {
413         assert(node);
414         return node->op->name;
415 }
416
417 ir_visited_t (get_irn_visited)(const ir_node *node)
418 {
419         return _get_irn_visited(node);
420 }
421
422 void (set_irn_visited)(ir_node *node, ir_visited_t visited)
423 {
424         _set_irn_visited(node, visited);
425 }
426
427 void (mark_irn_visited)(ir_node *node)
428 {
429         _mark_irn_visited(node);
430 }
431
432 int (irn_visited)(const ir_node *node)
433 {
434         return _irn_visited(node);
435 }
436
437 int (irn_visited_else_mark)(ir_node *node)
438 {
439         return _irn_visited_else_mark(node);
440 }
441
442 void (set_irn_link)(ir_node *node, void *link)
443 {
444         _set_irn_link(node, link);
445 }
446
447 void *(get_irn_link)(const ir_node *node)
448 {
449         return _get_irn_link(node);
450 }
451
452 op_pin_state (get_irn_pinned)(const ir_node *node)
453 {
454         return _get_irn_pinned(node);
455 }
456
457 op_pin_state (is_irn_pinned_in_irg) (const ir_node *node)
458 {
459         return _is_irn_pinned_in_irg(node);
460 }
461
462 void set_irn_pinned(ir_node *node, op_pin_state state)
463 {
464         /* due to optimization an opt may be turned into a Tuple */
465         if (is_Tuple(node))
466                 return;
467
468         assert(node && get_op_pinned(get_irn_op(node)) >= op_pin_state_exc_pinned);
469         assert(state == op_pin_state_pinned || state == op_pin_state_floats);
470
471         node->attr.except.pin_state = state;
472 }
473
474 /* Outputs a unique number for this node */
475 long get_irn_node_nr(const ir_node *node)
476 {
477         assert(node);
478         return node->node_nr;
479 }
480
481 void *(get_irn_generic_attr)(ir_node *node)
482 {
483         assert(is_ir_node(node));
484         return _get_irn_generic_attr(node);
485 }
486
487 const void *(get_irn_generic_attr_const)(const ir_node *node)
488 {
489         assert(is_ir_node(node));
490         return _get_irn_generic_attr_const(node);
491 }
492
493 unsigned (get_irn_idx)(const ir_node *node)
494 {
495         assert(is_ir_node(node));
496         return _get_irn_idx(node);
497 }
498
499 int get_irn_pred_pos(ir_node *node, ir_node *arg)
500 {
501         int i;
502         for (i = get_irn_arity(node) - 1; i >= 0; i--) {
503                 if (get_irn_n(node, i) == arg)
504                         return i;
505         }
506         return -1;
507 }
508
509 /** manipulate fields of individual nodes **/
510
511 ir_node *(get_nodes_block)(const ir_node *node)
512 {
513         return _get_nodes_block(node);
514 }
515
516 void set_nodes_block(ir_node *node, ir_node *block)
517 {
518         assert(node->op != op_Block);
519         set_irn_n(node, -1, block);
520 }
521
522 /* this works for all except Block */
523 ir_node *get_nodes_MacroBlock(const ir_node *node)
524 {
525         assert(node->op != op_Block);
526         return get_Block_MacroBlock(get_irn_n(node, -1));
527 }
528
529 /* Test whether arbitrary node is frame pointer, i.e. Proj(pn_Start_P_frame_base)
530  * from Start.  If so returns frame type, else Null. */
531 ir_type *is_frame_pointer(const ir_node *n)
532 {
533         if (is_Proj(n) && (get_Proj_proj(n) == pn_Start_P_frame_base)) {
534                 ir_node *start = get_Proj_pred(n);
535                 if (is_Start(start)) {
536                         return get_irg_frame_type(get_irn_irg(start));
537                 }
538         }
539         return NULL;
540 }
541
542 /* Test whether arbitrary node is tls pointer, i.e. Proj(pn_Start_P_tls)
543  * from Start.  If so returns tls type, else Null. */
544 ir_type *is_tls_pointer(const ir_node *n)
545 {
546         if (is_Proj(n) && (get_Proj_proj(n) == pn_Start_P_tls)) {
547                 ir_node *start = get_Proj_pred(n);
548                 if (is_Start(start)) {
549                         return get_tls_type();
550                 }
551         }
552         return NULL;
553 }
554
555 ir_node **get_Block_cfgpred_arr(ir_node *node)
556 {
557         assert(is_Block(node));
558         return (ir_node **)&(get_irn_in(node)[1]);
559 }
560
561 int (get_Block_n_cfgpreds)(const ir_node *node)
562 {
563         return _get_Block_n_cfgpreds(node);
564 }
565
566 ir_node *(get_Block_cfgpred)(const ir_node *node, int pos)
567 {
568         return _get_Block_cfgpred(node, pos);
569 }
570
571 void set_Block_cfgpred(ir_node *node, int pos, ir_node *pred)
572 {
573         assert(is_Block(node));
574         set_irn_n(node, pos, pred);
575 }
576
577 int get_Block_cfgpred_pos(const ir_node *block, const ir_node *pred)
578 {
579         int i;
580
581         for (i = get_Block_n_cfgpreds(block) - 1; i >= 0; --i) {
582                 if (get_Block_cfgpred_block(block, i) == pred)
583                         return i;
584         }
585         return -1;
586 }
587
588 ir_node *(get_Block_cfgpred_block)(const ir_node *node, int pos)
589 {
590         return _get_Block_cfgpred_block(node, pos);
591 }
592
593 int get_Block_matured(const ir_node *node)
594 {
595         assert(is_Block(node));
596         return (int)node->attr.block.is_matured;
597 }
598
599 void set_Block_matured(ir_node *node, int matured)
600 {
601         assert(is_Block(node));
602         node->attr.block.is_matured = matured;
603 }
604
605 ir_visited_t (get_Block_block_visited)(const ir_node *node)
606 {
607         return _get_Block_block_visited(node);
608 }
609
610 void (set_Block_block_visited)(ir_node *node, ir_visited_t visit)
611 {
612         _set_Block_block_visited(node, visit);
613 }
614
615 void (mark_Block_block_visited)(ir_node *node)
616 {
617         _mark_Block_block_visited(node);
618 }
619
620 int (Block_block_visited)(const ir_node *node)
621 {
622         return _Block_block_visited(node);
623 }
624
625 ir_node *(set_Block_dead)(ir_node *block)
626 {
627         return _set_Block_dead(block);
628 }
629
630 int (is_Block_dead)(const ir_node *block)
631 {
632         return _is_Block_dead(block);
633 }
634
635 ir_extblk *get_Block_extbb(const ir_node *block)
636 {
637         ir_extblk *res;
638         assert(is_Block(block));
639         res = block->attr.block.extblk;
640         assert(res == NULL || is_ir_extbb(res));
641         return res;
642 }
643
644 void set_Block_extbb(ir_node *block, ir_extblk *extblk)
645 {
646         assert(is_Block(block));
647         assert(extblk == NULL || is_ir_extbb(extblk));
648         block->attr.block.extblk = extblk;
649 }
650
651 /* Returns the macro block header of a block.*/
652 ir_node *get_Block_MacroBlock(const ir_node *block)
653 {
654         ir_node *mbh;
655         assert(is_Block(block));
656         mbh = get_irn_n(block, -1);
657         /* once macro block header is respected by all optimizations,
658            this assert can be removed */
659         assert(mbh != NULL);
660         return mbh;
661 }
662
663 /* Sets the macro block header of a block. */
664 void set_Block_MacroBlock(ir_node *block, ir_node *mbh)
665 {
666         assert(is_Block(block));
667         mbh = skip_Id(mbh);
668         assert(is_Block(mbh));
669         set_irn_n(block, -1, mbh);
670 }
671
672 /* returns the macro block header of a node. */
673 ir_node *get_irn_MacroBlock(const ir_node *n)
674 {
675         if (! is_Block(n)) {
676                 n = get_nodes_block(n);
677                 /* if the Block is Bad, do NOT try to get it's MB, it will fail. */
678                 if (is_Bad(n))
679                         return (ir_node *)n;
680         }
681         return get_Block_MacroBlock(n);
682 }
683
684 /* returns the graph of a Block. */
685 ir_graph *(get_Block_irg)(const ir_node *block)
686 {
687         return _get_Block_irg(block);
688 }
689
690 ir_entity *create_Block_entity(ir_node *block)
691 {
692         ir_entity *entity;
693         assert(is_Block(block));
694
695         entity = block->attr.block.entity;
696         if (entity == NULL) {
697                 ir_label_t  nr;
698                 ir_type   *glob;
699
700                 glob = get_glob_type();
701                 entity = new_entity(glob, id_unique("block_%u"), get_code_type());
702                 set_entity_visibility(entity, ir_visibility_local);
703                 set_entity_linkage(entity, IR_LINKAGE_CONSTANT);
704                 nr = get_irp_next_label_nr();
705                 set_entity_label(entity, nr);
706                 set_entity_compiler_generated(entity, 1);
707
708                 block->attr.block.entity = entity;
709         }
710         return entity;
711 }
712
713 ir_entity *get_Block_entity(const ir_node *block)
714 {
715         assert(is_Block(block));
716         return block->attr.block.entity;
717 }
718
719 void set_Block_entity(ir_node *block, ir_entity *entity)
720 {
721         assert(is_Block(block));
722         assert(get_entity_type(entity) == get_code_type());
723         block->attr.block.entity = entity;
724 }
725
726 int has_Block_entity(const ir_node *block)
727 {
728         return block->attr.block.entity != NULL;
729 }
730
731 ir_node *(get_Block_phis)(const ir_node *block)
732 {
733         return _get_Block_phis(block);
734 }
735
736 void (set_Block_phis)(ir_node *block, ir_node *phi)
737 {
738         _set_Block_phis(block, phi);
739 }
740
741 void (add_Block_phi)(ir_node *block, ir_node *phi)
742 {
743         _add_Block_phi(block, phi);
744 }
745
746 /* Get the Block mark (single bit). */
747 unsigned (get_Block_mark)(const ir_node *block)
748 {
749         return _get_Block_mark(block);
750 }
751
752 /* Set the Block mark (single bit). */
753 void (set_Block_mark)(ir_node *block, unsigned mark)
754 {
755         _set_Block_mark(block, mark);
756 }
757
758 int get_End_n_keepalives(const ir_node *end)
759 {
760         assert(is_End(end));
761         return (get_irn_arity(end) - END_KEEPALIVE_OFFSET);
762 }
763
764 ir_node *get_End_keepalive(const ir_node *end, int pos)
765 {
766         assert(is_End(end));
767         return get_irn_n(end, pos + END_KEEPALIVE_OFFSET);
768 }
769
770 void add_End_keepalive(ir_node *end, ir_node *ka)
771 {
772         assert(is_End(end));
773         add_irn_n(end, ka);
774 }
775
776 void set_End_keepalive(ir_node *end, int pos, ir_node *ka)
777 {
778         assert(is_End(end));
779         set_irn_n(end, pos + END_KEEPALIVE_OFFSET, ka);
780 }
781
782 /* Set new keep-alives */
783 void set_End_keepalives(ir_node *end, int n, ir_node *in[])
784 {
785         int i;
786         ir_graph *irg = get_irn_irg(end);
787
788         /* notify that edges are deleted */
789         for (i = END_KEEPALIVE_OFFSET; i < ARR_LEN(end->in) - 1; ++i) {
790                 edges_notify_edge(end, i, NULL, end->in[i + 1], irg);
791         }
792         ARR_RESIZE(ir_node *, end->in, n + 1 + END_KEEPALIVE_OFFSET);
793
794         for (i = 0; i < n; ++i) {
795                 end->in[1 + END_KEEPALIVE_OFFSET + i] = in[i];
796                 edges_notify_edge(end, END_KEEPALIVE_OFFSET + i, end->in[1 + END_KEEPALIVE_OFFSET + i], NULL, irg);
797         }
798 }
799
800 /* Set new keep-alives from old keep-alives, skipping irn */
801 void remove_End_keepalive(ir_node *end, ir_node *irn)
802 {
803         int      n = get_End_n_keepalives(end);
804         int      i, idx;
805         ir_graph *irg;
806
807         idx = -1;
808         for (i = n -1; i >= 0; --i) {
809                 ir_node *old_ka = end->in[1 + END_KEEPALIVE_OFFSET + i];
810
811                 /* find irn */
812                 if (old_ka == irn) {
813                         idx = i;
814                         goto found;
815                 }
816         }
817         return;
818 found:
819         irg = get_irn_irg(end);
820
821         /* remove the edge */
822         edges_notify_edge(end, idx, NULL, irn, irg);
823
824         if (idx != n - 1) {
825                 /* exchange with the last one */
826                 ir_node *old = end->in[1 + END_KEEPALIVE_OFFSET + n - 1];
827                 edges_notify_edge(end, n - 1, NULL, old, irg);
828                 end->in[1 + END_KEEPALIVE_OFFSET + idx] = old;
829                 edges_notify_edge(end, idx, old, NULL, irg);
830         }
831         /* now n - 1 keeps, 1 block input */
832         ARR_RESIZE(ir_node *, end->in, (n - 1) + 1 + END_KEEPALIVE_OFFSET);
833 }
834
835 /* remove Bads, NoMems and doublets from the keep-alive set */
836 void remove_End_Bads_and_doublets(ir_node *end)
837 {
838         pset_new_t keeps;
839         int        idx, n = get_End_n_keepalives(end);
840         ir_graph   *irg;
841
842         if (n <= 0)
843                 return;
844
845         irg = get_irn_irg(end);
846         pset_new_init(&keeps);
847
848         for (idx = n - 1; idx >= 0; --idx) {
849                 ir_node *ka = get_End_keepalive(end, idx);
850
851                 if (is_Bad(ka) || is_NoMem(ka) || pset_new_contains(&keeps, ka)) {
852                         /* remove the edge */
853                         edges_notify_edge(end, idx, NULL, ka, irg);
854
855                         if (idx != n - 1) {
856                                 /* exchange with the last one */
857                                 ir_node *old = end->in[1 + END_KEEPALIVE_OFFSET + n - 1];
858                                 edges_notify_edge(end, n - 1, NULL, old, irg);
859                                 end->in[1 + END_KEEPALIVE_OFFSET + idx] = old;
860                                 edges_notify_edge(end, idx, old, NULL, irg);
861                         }
862                         --n;
863                 } else {
864                         pset_new_insert(&keeps, ka);
865                 }
866         }
867         /* n keeps, 1 block input */
868         ARR_RESIZE(ir_node *, end->in, n + 1 + END_KEEPALIVE_OFFSET);
869
870         pset_new_destroy(&keeps);
871 }
872
873 void free_End(ir_node *end)
874 {
875         assert(is_End(end));
876         end->kind = k_BAD;
877         DEL_ARR_F(end->in);
878         end->in = NULL;   /* @@@ make sure we get an error if we use the
879                              in array afterwards ... */
880 }
881
882 int get_Return_n_ress(const ir_node *node)
883 {
884         assert(is_Return(node));
885         return (get_irn_arity(node) - RETURN_RESULT_OFFSET);
886 }
887
888 ir_node **get_Return_res_arr(ir_node *node)
889 {
890         assert(is_Return(node));
891         if (get_Return_n_ress(node) > 0)
892                 return (ir_node **)&(get_irn_in(node)[1 + RETURN_RESULT_OFFSET]);
893         else
894                 return NULL;
895 }
896
897 /*
898 void set_Return_n_res(ir_node *node, int results)
899 {
900         assert(is_Return(node));
901 }
902 */
903
904 ir_node *get_Return_res(const ir_node *node, int pos)
905 {
906         assert(is_Return(node));
907         assert(get_Return_n_ress(node) > pos);
908         return get_irn_n(node, pos + RETURN_RESULT_OFFSET);
909 }
910
911 void set_Return_res(ir_node *node, int pos, ir_node *res)
912 {
913         assert(is_Return(node));
914         set_irn_n(node, pos + RETURN_RESULT_OFFSET, res);
915 }
916
917 int (is_Const_null)(const ir_node *node)
918 {
919         return _is_Const_null(node);
920 }
921
922 int (is_Const_one)(const ir_node *node)
923 {
924         return _is_Const_one(node);
925 }
926
927 int (is_Const_all_one)(const ir_node *node)
928 {
929         return _is_Const_all_one(node);
930 }
931
932
933 /* The source language type.  Must be an atomic type.  Mode of type must
934    be mode of node. For tarvals from entities type must be pointer to
935    entity type. */
936 ir_type *get_Const_type(const ir_node *node)
937 {
938         assert(is_Const(node));
939         return node->attr.con.tp;
940 }
941
942 void set_Const_type(ir_node *node, ir_type *tp)
943 {
944         assert(is_Const(node));
945         if (tp != firm_unknown_type) {
946                 assert(is_atomic_type(tp));
947                 assert(get_type_mode(tp) == get_irn_mode(node));
948         }
949         node->attr.con.tp = tp;
950 }
951
952
953 symconst_kind get_SymConst_kind(const ir_node *node)
954 {
955         assert(is_SymConst(node));
956         return node->attr.symc.kind;
957 }
958
959 void set_SymConst_kind(ir_node *node, symconst_kind kind)
960 {
961         assert(is_SymConst(node));
962         node->attr.symc.kind = kind;
963 }
964
965 ir_type *get_SymConst_type(const ir_node *node)
966 {
967         /* the cast here is annoying, but we have to compensate for
968            the skip_tip() */
969         ir_node *irn = (ir_node *)node;
970         assert(is_SymConst(node) &&
971                (SYMCONST_HAS_TYPE(get_SymConst_kind(node))));
972         return irn->attr.symc.sym.type_p;
973 }
974
975 void set_SymConst_type(ir_node *node, ir_type *tp)
976 {
977         assert(is_SymConst(node) &&
978                (SYMCONST_HAS_TYPE(get_SymConst_kind(node))));
979         node->attr.symc.sym.type_p = tp;
980 }
981
982
983 /* Only to access SymConst of kind symconst_addr_ent.  Else assertion: */
984 ir_entity *get_SymConst_entity(const ir_node *node)
985 {
986         assert(is_SymConst(node) && SYMCONST_HAS_ENT(get_SymConst_kind(node)));
987         return node->attr.symc.sym.entity_p;
988 }
989
990 void set_SymConst_entity(ir_node *node, ir_entity *ent)
991 {
992         assert(is_SymConst(node) && SYMCONST_HAS_ENT(get_SymConst_kind(node)));
993         node->attr.symc.sym.entity_p  = ent;
994 }
995
996 ir_enum_const *get_SymConst_enum(const ir_node *node)
997 {
998         assert(is_SymConst(node) && SYMCONST_HAS_ENUM(get_SymConst_kind(node)));
999         return node->attr.symc.sym.enum_p;
1000 }
1001
1002 void set_SymConst_enum(ir_node *node, ir_enum_const *ec)
1003 {
1004         assert(is_SymConst(node) && SYMCONST_HAS_ENUM(get_SymConst_kind(node)));
1005         node->attr.symc.sym.enum_p  = ec;
1006 }
1007
1008 union symconst_symbol
1009 get_SymConst_symbol(const ir_node *node)
1010 {
1011         assert(is_SymConst(node));
1012         return node->attr.symc.sym;
1013 }
1014
1015 void set_SymConst_symbol(ir_node *node, union symconst_symbol sym)
1016 {
1017         assert(is_SymConst(node));
1018         node->attr.symc.sym = sym;
1019 }
1020
1021 ir_type *get_SymConst_value_type(const ir_node *node)
1022 {
1023         assert(is_SymConst(node));
1024         return node->attr.symc.tp;
1025 }
1026
1027 void set_SymConst_value_type(ir_node *node, ir_type *tp)
1028 {
1029         assert(is_SymConst(node));
1030         node->attr.symc.tp = tp;
1031 }
1032
1033 int get_Sel_n_indexs(const ir_node *node)
1034 {
1035         assert(is_Sel(node));
1036         return (get_irn_arity(node) - SEL_INDEX_OFFSET);
1037 }
1038
1039 ir_node **get_Sel_index_arr(ir_node *node)
1040 {
1041         assert(is_Sel(node));
1042         if (get_Sel_n_indexs(node) > 0)
1043                 return (ir_node **)& get_irn_in(node)[SEL_INDEX_OFFSET + 1];
1044         else
1045                 return NULL;
1046 }
1047
1048 ir_node *get_Sel_index(const ir_node *node, int pos)
1049 {
1050         assert(is_Sel(node));
1051         return get_irn_n(node, pos + SEL_INDEX_OFFSET);
1052 }
1053
1054 void set_Sel_index(ir_node *node, int pos, ir_node *index)
1055 {
1056         assert(is_Sel(node));
1057         set_irn_n(node, pos + SEL_INDEX_OFFSET, index);
1058 }
1059
1060
1061 /* For unary and binary arithmetic operations the access to the
1062    operands can be factored out.  Left is the first, right the
1063    second arithmetic value  as listed in tech report 0999-33.
1064    unops are: Minus, Abs, Not, Conv, Cast
1065    binops are: Add, Sub, Mul, Quot, DivMod, Div, Mod, And, Or, Eor, Shl,
1066    Shr, Shrs, Rotate, Cmp */
1067
1068
1069 ir_node **get_Call_param_arr(ir_node *node)
1070 {
1071         assert(is_Call(node));
1072         return &get_irn_in(node)[CALL_PARAM_OFFSET + 1];
1073 }
1074
1075 int get_Call_n_params(const ir_node *node)
1076 {
1077         assert(is_Call(node));
1078         return (get_irn_arity(node) - CALL_PARAM_OFFSET);
1079 }
1080
1081 ir_node *get_Call_param(const ir_node *node, int pos)
1082 {
1083         assert(is_Call(node));
1084         return get_irn_n(node, pos + CALL_PARAM_OFFSET);
1085 }
1086
1087 void set_Call_param(ir_node *node, int pos, ir_node *param)
1088 {
1089         assert(is_Call(node));
1090         set_irn_n(node, pos + CALL_PARAM_OFFSET, param);
1091 }
1092
1093 ir_node **get_Builtin_param_arr(ir_node *node)
1094 {
1095         assert(is_Builtin(node));
1096         return &get_irn_in(node)[BUILDIN_PARAM_OFFSET + 1];
1097 }
1098
1099 int get_Builtin_n_params(const ir_node *node)
1100 {
1101         assert(is_Builtin(node));
1102         return (get_irn_arity(node) - BUILDIN_PARAM_OFFSET);
1103 }
1104
1105 ir_node *get_Builtin_param(const ir_node *node, int pos)
1106 {
1107         assert(is_Builtin(node));
1108         return get_irn_n(node, pos + BUILDIN_PARAM_OFFSET);
1109 }
1110
1111 void set_Builtin_param(ir_node *node, int pos, ir_node *param)
1112 {
1113         assert(is_Builtin(node));
1114         set_irn_n(node, pos + BUILDIN_PARAM_OFFSET, param);
1115 }
1116
1117 /* Returns a human readable string for the ir_builtin_kind. */
1118 const char *get_builtin_kind_name(ir_builtin_kind kind)
1119 {
1120 #define X(a)    case a: return #a
1121         switch (kind) {
1122                 X(ir_bk_trap);
1123                 X(ir_bk_debugbreak);
1124                 X(ir_bk_return_address);
1125                 X(ir_bk_frame_address);
1126                 X(ir_bk_prefetch);
1127                 X(ir_bk_ffs);
1128                 X(ir_bk_clz);
1129                 X(ir_bk_ctz);
1130                 X(ir_bk_popcount);
1131                 X(ir_bk_parity);
1132                 X(ir_bk_bswap);
1133                 X(ir_bk_inport);
1134                 X(ir_bk_outport);
1135                 X(ir_bk_inner_trampoline);
1136         }
1137         return "<unknown>";
1138 #undef X
1139 }
1140
1141
1142 int Call_has_callees(const ir_node *node)
1143 {
1144         assert(is_Call(node));
1145         return ((get_irg_callee_info_state(get_irn_irg(node)) != irg_callee_info_none) &&
1146                 (node->attr.call.callee_arr != NULL));
1147 }
1148
1149 int get_Call_n_callees(const ir_node *node)
1150 {
1151   assert(is_Call(node) && node->attr.call.callee_arr);
1152   return ARR_LEN(node->attr.call.callee_arr);
1153 }
1154
1155 ir_entity *get_Call_callee(const ir_node *node, int pos)
1156 {
1157         assert(pos >= 0 && pos < get_Call_n_callees(node));
1158         return node->attr.call.callee_arr[pos];
1159 }
1160
1161 void set_Call_callee_arr(ir_node *node, const int n, ir_entity ** arr)
1162 {
1163         ir_graph *irg = get_irn_irg(node);
1164
1165         assert(is_Call(node));
1166         if (node->attr.call.callee_arr == NULL || get_Call_n_callees(node) != n) {
1167                 node->attr.call.callee_arr = NEW_ARR_D(ir_entity *, irg->obst, n);
1168         }
1169         memcpy(node->attr.call.callee_arr, arr, n * sizeof(ir_entity *));
1170 }
1171
1172 void remove_Call_callee_arr(ir_node *node)
1173 {
1174         assert(is_Call(node));
1175         node->attr.call.callee_arr = NULL;
1176 }
1177
1178 /*
1179  * Returns non-zero if a Call is surely a self-recursive Call.
1180  * Beware: if this functions returns 0, the call might be self-recursive!
1181  */
1182 int is_self_recursive_Call(const ir_node *call)
1183 {
1184         const ir_node *callee = get_Call_ptr(call);
1185
1186         if (is_SymConst_addr_ent(callee)) {
1187                 const ir_entity *ent = get_SymConst_entity(callee);
1188                 const ir_graph  *irg = get_entity_irg(ent);
1189                 if (irg == get_irn_irg(call))
1190                         return 1;
1191         }
1192         return 0;
1193 }
1194
1195 /* Checks for upcast.
1196  *
1197  * Returns true if the Cast node casts a class type to a super type.
1198  */
1199 int is_Cast_upcast(ir_node *node)
1200 {
1201         ir_type *totype   = get_Cast_type(node);
1202         ir_type *fromtype = get_irn_typeinfo_type(get_Cast_op(node));
1203
1204         assert(get_irg_typeinfo_state(get_irn_irg(node)) == ir_typeinfo_consistent);
1205         assert(fromtype);
1206
1207         while (is_Pointer_type(totype) && is_Pointer_type(fromtype)) {
1208                 totype   = get_pointer_points_to_type(totype);
1209                 fromtype = get_pointer_points_to_type(fromtype);
1210         }
1211
1212         assert(fromtype);
1213
1214         if (!is_Class_type(totype)) return 0;
1215         return is_SubClass_of(fromtype, totype);
1216 }
1217
1218 /* Checks for downcast.
1219  *
1220  * Returns true if the Cast node casts a class type to a sub type.
1221  */
1222 int is_Cast_downcast(ir_node *node)
1223 {
1224         ir_type *totype   = get_Cast_type(node);
1225         ir_type *fromtype = get_irn_typeinfo_type(get_Cast_op(node));
1226
1227         assert(get_irg_typeinfo_state(get_irn_irg(node)) == ir_typeinfo_consistent);
1228         assert(fromtype);
1229
1230         while (is_Pointer_type(totype) && is_Pointer_type(fromtype)) {
1231                 totype   = get_pointer_points_to_type(totype);
1232                 fromtype = get_pointer_points_to_type(fromtype);
1233         }
1234
1235         assert(fromtype);
1236
1237         if (!is_Class_type(totype)) return 0;
1238         return is_SubClass_of(totype, fromtype);
1239 }
1240
1241 int (is_unop)(const ir_node *node)
1242 {
1243         return _is_unop(node);
1244 }
1245
1246 ir_node *get_unop_op(const ir_node *node)
1247 {
1248         if (node->op->opar == oparity_unary)
1249                 return get_irn_n(node, node->op->op_index);
1250
1251         assert(node->op->opar == oparity_unary);
1252         return NULL;
1253 }
1254
1255 void set_unop_op(ir_node *node, ir_node *op)
1256 {
1257         if (node->op->opar == oparity_unary)
1258                 set_irn_n(node, node->op->op_index, op);
1259
1260         assert(node->op->opar == oparity_unary);
1261 }
1262
1263 int (is_binop)(const ir_node *node)
1264 {
1265         return _is_binop(node);
1266 }
1267
1268 ir_node *get_binop_left(const ir_node *node)
1269 {
1270         assert(node->op->opar == oparity_binary);
1271         return get_irn_n(node, node->op->op_index);
1272 }
1273
1274 void set_binop_left(ir_node *node, ir_node *left)
1275 {
1276         assert(node->op->opar == oparity_binary);
1277         set_irn_n(node, node->op->op_index, left);
1278 }
1279
1280 ir_node *get_binop_right(const ir_node *node)
1281 {
1282         assert(node->op->opar == oparity_binary);
1283         return get_irn_n(node, node->op->op_index + 1);
1284 }
1285
1286 void set_binop_right(ir_node *node, ir_node *right)
1287 {
1288         assert(node->op->opar == oparity_binary);
1289         set_irn_n(node, node->op->op_index + 1, right);
1290 }
1291
1292 int is_Phi0(const ir_node *n)
1293 {
1294         assert(n);
1295
1296         return ((get_irn_op(n) == op_Phi) &&
1297                 (get_irn_arity(n) == 0) &&
1298                 (get_irg_phase_state(get_irn_irg(n)) ==  phase_building));
1299 }
1300
1301 ir_node **get_Phi_preds_arr(ir_node *node)
1302 {
1303   assert(is_Phi(node));
1304   return (ir_node **)&(get_irn_in(node)[1]);
1305 }
1306
1307 int get_Phi_n_preds(const ir_node *node)
1308 {
1309         assert(is_Phi(node) || is_Phi0(node));
1310         return (get_irn_arity(node));
1311 }
1312
1313 ir_node *get_Phi_pred(const ir_node *node, int pos)
1314 {
1315         assert(is_Phi(node) || is_Phi0(node));
1316         return get_irn_n(node, pos);
1317 }
1318
1319 void set_Phi_pred(ir_node *node, int pos, ir_node *pred)
1320 {
1321         assert(is_Phi(node) || is_Phi0(node));
1322         set_irn_n(node, pos, pred);
1323 }
1324
1325 ir_node *(get_Phi_next)(const ir_node *phi)
1326 {
1327         return _get_Phi_next(phi);
1328 }
1329
1330 void (set_Phi_next)(ir_node *phi, ir_node *next)
1331 {
1332         _set_Phi_next(phi, next);
1333 }
1334
1335 int is_memop(const ir_node *node)
1336 {
1337         ir_opcode code = get_irn_opcode(node);
1338         return (code == iro_Load || code == iro_Store);
1339 }
1340
1341 ir_node *get_memop_mem(const ir_node *node)
1342 {
1343         assert(is_memop(node));
1344         return get_irn_n(node, 0);
1345 }
1346
1347 void set_memop_mem(ir_node *node, ir_node *mem)
1348 {
1349         assert(is_memop(node));
1350         set_irn_n(node, 0, mem);
1351 }
1352
1353 ir_node *get_memop_ptr(const ir_node *node)
1354 {
1355         assert(is_memop(node));
1356         return get_irn_n(node, 1);
1357 }
1358
1359 void set_memop_ptr(ir_node *node, ir_node *ptr)
1360 {
1361         assert(is_memop(node));
1362         set_irn_n(node, 1, ptr);
1363 }
1364
1365 ir_volatility get_Load_volatility(const ir_node *node)
1366 {
1367         assert(is_Load(node));
1368         return node->attr.load.volatility;
1369 }
1370
1371 void set_Load_volatility(ir_node *node, ir_volatility volatility)
1372 {
1373         assert(is_Load(node));
1374         node->attr.load.volatility = volatility;
1375 }
1376
1377 ir_align get_Load_align(const ir_node *node)
1378 {
1379         assert(is_Load(node));
1380         return node->attr.load.aligned;
1381 }
1382
1383 void set_Load_align(ir_node *node, ir_align align)
1384 {
1385         assert(is_Load(node));
1386         node->attr.load.aligned = align;
1387 }
1388
1389
1390 ir_volatility get_Store_volatility(const ir_node *node)
1391 {
1392         assert(is_Store(node));
1393         return node->attr.store.volatility;
1394 }
1395
1396 void set_Store_volatility(ir_node *node, ir_volatility volatility)
1397 {
1398         assert(is_Store(node));
1399         node->attr.store.volatility = volatility;
1400 }
1401
1402 ir_align get_Store_align(const ir_node *node)
1403 {
1404         assert(is_Store(node));
1405         return node->attr.store.aligned;
1406 }
1407
1408 void set_Store_align(ir_node *node, ir_align align)
1409 {
1410         assert(is_Store(node));
1411         node->attr.store.aligned = align;
1412 }
1413
1414
1415 ir_node **get_Sync_preds_arr(ir_node *node)
1416 {
1417         assert(is_Sync(node));
1418         return (ir_node **)&(get_irn_in(node)[1]);
1419 }
1420
1421 int get_Sync_n_preds(const ir_node *node)
1422 {
1423         assert(is_Sync(node));
1424         return (get_irn_arity(node));
1425 }
1426
1427 /*
1428 void set_Sync_n_preds(ir_node *node, int n_preds)
1429 {
1430         assert(is_Sync(node));
1431 }
1432 */
1433
1434 ir_node *get_Sync_pred(const ir_node *node, int pos)
1435 {
1436         assert(is_Sync(node));
1437         return get_irn_n(node, pos);
1438 }
1439
1440 void set_Sync_pred(ir_node *node, int pos, ir_node *pred)
1441 {
1442         assert(is_Sync(node));
1443         set_irn_n(node, pos, pred);
1444 }
1445
1446 /* Add a new Sync predecessor */
1447 void add_Sync_pred(ir_node *node, ir_node *pred)
1448 {
1449         assert(is_Sync(node));
1450         add_irn_n(node, pred);
1451 }
1452
1453 /* Returns the source language type of a Proj node. */
1454 ir_type *get_Proj_type(const ir_node *n)
1455 {
1456         ir_type *tp   = firm_unknown_type;
1457         ir_node *pred = get_Proj_pred(n);
1458
1459         switch (get_irn_opcode(pred)) {
1460         case iro_Proj: {
1461                 ir_node *pred_pred;
1462                 /* Deal with Start / Call here: we need to know the Proj Nr. */
1463                 assert(get_irn_mode(pred) == mode_T);
1464                 pred_pred = get_Proj_pred(pred);
1465
1466                 if (is_Start(pred_pred))  {
1467                         ir_type *mtp = get_entity_type(get_irg_entity(get_irn_irg(pred_pred)));
1468                         tp = get_method_param_type(mtp, get_Proj_proj(n));
1469                 } else if (is_Call(pred_pred)) {
1470                         ir_type *mtp = get_Call_type(pred_pred);
1471                         tp = get_method_res_type(mtp, get_Proj_proj(n));
1472                 }
1473         } break;
1474         case iro_Start: break;
1475         case iro_Call: break;
1476         case iro_Load: {
1477                 ir_node *a = get_Load_ptr(pred);
1478                 if (is_Sel(a))
1479                         tp = get_entity_type(get_Sel_entity(a));
1480         } break;
1481         default:
1482                 break;
1483         }
1484         return tp;
1485 }
1486
1487 long get_Proj_proj(const ir_node *node)
1488 {
1489         assert(is_Proj(node));
1490         return node->attr.proj;
1491 }
1492
1493 void set_Proj_proj(ir_node *node, long proj)
1494 {
1495         assert(is_Proj(node));
1496         node->attr.proj = proj;
1497 }
1498
1499 int (is_arg_Proj)(const ir_node *node)
1500 {
1501         return _is_arg_Proj(node);
1502 }
1503
1504 ir_node **get_Tuple_preds_arr(ir_node *node)
1505 {
1506         assert(is_Tuple(node));
1507         return (ir_node **)&(get_irn_in(node)[1]);
1508 }
1509
1510 int get_Tuple_n_preds(const ir_node *node)
1511 {
1512         assert(is_Tuple(node));
1513         return get_irn_arity(node);
1514 }
1515
1516 ir_node *get_Tuple_pred(const ir_node *node, int pos)
1517 {
1518   assert(is_Tuple(node));
1519   return get_irn_n(node, pos);
1520 }
1521
1522 void set_Tuple_pred(ir_node *node, int pos, ir_node *pred)
1523 {
1524         assert(is_Tuple(node));
1525         set_irn_n(node, pos, pred);
1526 }
1527
1528 int get_ASM_n_input_constraints(const ir_node *node)
1529 {
1530         assert(is_ASM(node));
1531         return ARR_LEN(node->attr.assem.input_constraints);
1532 }
1533
1534 int get_ASM_n_output_constraints(const ir_node *node)
1535 {
1536         assert(is_ASM(node));
1537         return ARR_LEN(node->attr.assem.output_constraints);
1538 }
1539
1540 int get_ASM_n_clobbers(const ir_node *node)
1541 {
1542         assert(is_ASM(node));
1543         return ARR_LEN(node->attr.assem.clobbers);
1544 }
1545
1546 /* returns the graph of a node */
1547 ir_graph *(get_irn_irg)(const ir_node *node)
1548 {
1549         return _get_irn_irg(node);
1550 }
1551
1552
1553 /*----------------------------------------------------------------*/
1554 /*  Auxiliary routines                                            */
1555 /*----------------------------------------------------------------*/
1556
1557 ir_node *skip_Proj(ir_node *node)
1558 {
1559         /* don't assert node !!! */
1560         if (node == NULL)
1561                 return NULL;
1562
1563         if (is_Proj(node))
1564                 node = get_Proj_pred(node);
1565
1566         return node;
1567 }
1568
1569 const ir_node *
1570 skip_Proj_const(const ir_node *node)
1571 {
1572         /* don't assert node !!! */
1573         if (node == NULL)
1574                 return NULL;
1575
1576         if (is_Proj(node))
1577                 node = get_Proj_pred(node);
1578
1579         return node;
1580 }
1581
1582 ir_node *skip_Tuple(ir_node *node)
1583 {
1584   ir_node *pred;
1585
1586 restart:
1587         if (is_Proj(node)) {
1588             pred = get_Proj_pred(node);
1589
1590                 if (is_Proj(pred)) { /* nested Tuple ? */
1591                     pred = skip_Tuple(pred);
1592
1593                         if (is_Tuple(pred)) {
1594                                 node = get_Tuple_pred(pred, get_Proj_proj(node));
1595                                 goto restart;
1596                         }
1597                 } else if (is_Tuple(pred)) {
1598                         node = get_Tuple_pred(pred, get_Proj_proj(node));
1599                         goto restart;
1600                 }
1601         }
1602         return node;
1603 }
1604
1605 /* returns operand of node if node is a Cast */
1606 ir_node *skip_Cast(ir_node *node)
1607 {
1608         if (is_Cast(node))
1609                 return get_Cast_op(node);
1610         return node;
1611 }
1612
1613 /* returns operand of node if node is a Cast */
1614 const ir_node *skip_Cast_const(const ir_node *node)
1615 {
1616         if (is_Cast(node))
1617                 return get_Cast_op(node);
1618         return node;
1619 }
1620
1621 /* returns operand of node if node is a Pin */
1622 ir_node *skip_Pin(ir_node *node)
1623 {
1624         if (is_Pin(node))
1625                 return get_Pin_op(node);
1626         return node;
1627 }
1628
1629 /* returns operand of node if node is a Confirm */
1630 ir_node *skip_Confirm(ir_node *node)
1631 {
1632         if (is_Confirm(node))
1633                 return get_Confirm_value(node);
1634         return node;
1635 }
1636
1637 /* skip all high-level ops */
1638 ir_node *skip_HighLevel_ops(ir_node *node)
1639 {
1640         while (is_op_highlevel(get_irn_op(node))) {
1641                 node = get_irn_n(node, 0);
1642         }
1643         return node;
1644 }
1645
1646
1647 /* This should compact Id-cycles to self-cycles. It has the same (or less?) complexity
1648  * than any other approach, as Id chains are resolved and all point to the real node, or
1649  * all id's are self loops.
1650  *
1651  * Note: This function takes 10% of mostly ANY the compiler run, so it's
1652  * a little bit "hand optimized".
1653  *
1654  * Moreover, it CANNOT be switched off using get_opt_normalize() ...
1655  */
1656 ir_node *skip_Id(ir_node *node)
1657 {
1658         ir_node *pred;
1659         /* don't assert node !!! */
1660
1661         if (!node || (node->op != op_Id)) return node;
1662
1663         /* Don't use get_Id_pred():  We get into an endless loop for
1664            self-referencing Ids. */
1665         pred = node->in[0+1];
1666
1667         if (pred->op != op_Id) return pred;
1668
1669         if (node != pred) {  /* not a self referencing Id. Resolve Id chain. */
1670                 ir_node *rem_pred, *res;
1671
1672                 if (pred->op != op_Id) return pred; /* shortcut */
1673                 rem_pred = pred;
1674
1675                 assert(get_irn_arity (node) > 0);
1676
1677                 node->in[0+1] = node;   /* turn us into a self referencing Id:  shorten Id cycles. */
1678                 res = skip_Id(rem_pred);
1679                 if (is_Id(res)) /* self-loop */ return node;
1680
1681                 node->in[0+1] = res;    /* Turn Id chain into Ids all referencing the chain end. */
1682                 return res;
1683         } else {
1684                 return node;
1685         }
1686 }
1687
1688 int (is_strictConv)(const ir_node *node)
1689 {
1690         return _is_strictConv(node);
1691 }
1692
1693 /* Returns true if node is a SymConst node with kind symconst_addr_ent. */
1694 int (is_SymConst_addr_ent)(const ir_node *node)
1695 {
1696         return _is_SymConst_addr_ent(node);
1697 }
1698
1699 /* Returns true if the operation manipulates control flow. */
1700 int is_cfop(const ir_node *node)
1701 {
1702         return is_op_cfopcode(get_irn_op(node));
1703 }
1704
1705 /* Returns true if the operation can change the control flow because
1706    of an exception. */
1707 int is_fragile_op(const ir_node *node)
1708 {
1709         return is_op_fragile(get_irn_op(node));
1710 }
1711
1712 /* Returns the memory operand of fragile operations. */
1713 ir_node *get_fragile_op_mem(ir_node *node)
1714 {
1715         assert(node && is_fragile_op(node));
1716
1717         switch (get_irn_opcode(node)) {
1718         case iro_Call  :
1719         case iro_Quot  :
1720         case iro_DivMod:
1721         case iro_Div   :
1722         case iro_Mod   :
1723         case iro_Load  :
1724         case iro_Store :
1725         case iro_Alloc :
1726         case iro_Bound :
1727         case iro_CopyB :
1728                 return get_irn_n(node, pn_Generic_M);
1729         case iro_Bad   :
1730         case iro_Unknown:
1731                 return node;
1732         default:
1733                 panic("should not be reached");
1734         }
1735 }
1736
1737 /* Returns the result mode of a Div operation. */
1738 ir_mode *get_divop_resmod(const ir_node *node)
1739 {
1740         switch (get_irn_opcode(node)) {
1741         case iro_Quot  : return get_Quot_resmode(node);
1742         case iro_DivMod: return get_DivMod_resmode(node);
1743         case iro_Div   : return get_Div_resmode(node);
1744         case iro_Mod   : return get_Mod_resmode(node);
1745         default:
1746                 panic("should not be reached");
1747         }
1748 }
1749
1750 /* Returns true if the operation is a forking control flow operation. */
1751 int (is_irn_forking)(const ir_node *node)
1752 {
1753         return _is_irn_forking(node);
1754 }
1755
1756 void (copy_node_attr)(ir_graph *irg, const ir_node *old_node, ir_node *new_node)
1757 {
1758         _copy_node_attr(irg, old_node, new_node);
1759 }
1760
1761 /* Return the type associated with the value produced by n
1762  * if the node remarks this type as it is the case for
1763  * Cast, Const, SymConst and some Proj nodes. */
1764 ir_type *(get_irn_type)(ir_node *node)
1765 {
1766         return _get_irn_type(node);
1767 }
1768
1769 /* Return the type attribute of a node n (SymConst, Call, Alloc, Free,
1770    Cast) or NULL.*/
1771 ir_type *(get_irn_type_attr)(ir_node *node)
1772 {
1773         return _get_irn_type_attr(node);
1774 }
1775
1776 /* Return the entity attribute of a node n (SymConst, Sel) or NULL. */
1777 ir_entity *(get_irn_entity_attr)(ir_node *node)
1778 {
1779         return _get_irn_entity_attr(node);
1780 }
1781
1782 /* Returns non-zero for constant-like nodes. */
1783 int (is_irn_constlike)(const ir_node *node)
1784 {
1785         return _is_irn_constlike(node);
1786 }
1787
1788 /*
1789  * Returns non-zero for nodes that are allowed to have keep-alives and
1790  * are neither Block nor PhiM.
1791  */
1792 int (is_irn_keep)(const ir_node *node)
1793 {
1794         return _is_irn_keep(node);
1795 }
1796
1797 /*
1798  * Returns non-zero for nodes that are always placed in the start block.
1799  */
1800 int (is_irn_start_block_placed)(const ir_node *node)
1801 {
1802         return _is_irn_start_block_placed(node);
1803 }
1804
1805 /* Returns non-zero for nodes that are machine operations. */
1806 int (is_irn_machine_op)(const ir_node *node)
1807 {
1808         return _is_irn_machine_op(node);
1809 }
1810
1811 /* Returns non-zero for nodes that are machine operands. */
1812 int (is_irn_machine_operand)(const ir_node *node)
1813 {
1814         return _is_irn_machine_operand(node);
1815 }
1816
1817 /* Returns non-zero for nodes that have the n'th user machine flag set. */
1818 int (is_irn_machine_user)(const ir_node *node, unsigned n)
1819 {
1820         return _is_irn_machine_user(node, n);
1821 }
1822
1823 /* Returns non-zero for nodes that are CSE neutral to its users. */
1824 int (is_irn_cse_neutral)(const ir_node *node)
1825 {
1826         return _is_irn_cse_neutral(node);
1827 }
1828
1829 /* Gets the string representation of the jump prediction .*/
1830 const char *get_cond_jmp_predicate_name(cond_jmp_predicate pred)
1831 {
1832 #define X(a)    case a: return #a
1833         switch (pred) {
1834                 X(COND_JMP_PRED_NONE);
1835                 X(COND_JMP_PRED_TRUE);
1836                 X(COND_JMP_PRED_FALSE);
1837         }
1838         return "<unknown>";
1839 #undef X
1840 }
1841
1842 /** the get_type operation must be always implemented and return a firm type */
1843 static ir_type *get_Default_type(const ir_node *n)
1844 {
1845         (void) n;
1846         return get_unknown_type();
1847 }
1848
1849 /* Sets the get_type operation for an ir_op_ops. */
1850 ir_op_ops *firm_set_default_get_type(ir_opcode code, ir_op_ops *ops)
1851 {
1852         switch (code) {
1853         case iro_Const:    ops->get_type = get_Const_type; break;
1854         case iro_SymConst: ops->get_type = get_SymConst_value_type; break;
1855         case iro_Cast:     ops->get_type = get_Cast_type; break;
1856         case iro_Proj:     ops->get_type = get_Proj_type; break;
1857         default:
1858                 /* not allowed to be NULL */
1859                 if (! ops->get_type)
1860                         ops->get_type = get_Default_type;
1861                 break;
1862         }
1863         return ops;
1864 }
1865
1866 /** Return the attribute type of a SymConst node if exists */
1867 static ir_type *get_SymConst_attr_type(const ir_node *self)
1868 {
1869         symconst_kind kind = get_SymConst_kind(self);
1870         if (SYMCONST_HAS_TYPE(kind))
1871                 return get_SymConst_type(self);
1872         return NULL;
1873 }
1874
1875 /** Return the attribute entity of a SymConst node if exists */
1876 static ir_entity *get_SymConst_attr_entity(const ir_node *self)
1877 {
1878         symconst_kind kind = get_SymConst_kind(self);
1879         if (SYMCONST_HAS_ENT(kind))
1880                 return get_SymConst_entity(self);
1881         return NULL;
1882 }
1883
1884 /** the get_type_attr operation must be always implemented */
1885 static ir_type *get_Null_type(const ir_node *n)
1886 {
1887         (void) n;
1888         return firm_unknown_type;
1889 }
1890
1891 /* Sets the get_type operation for an ir_op_ops. */
1892 ir_op_ops *firm_set_default_get_type_attr(ir_opcode code, ir_op_ops *ops)
1893 {
1894         switch (code) {
1895         case iro_SymConst: ops->get_type_attr = get_SymConst_attr_type; break;
1896         case iro_Call:     ops->get_type_attr = get_Call_type; break;
1897         case iro_Alloc:    ops->get_type_attr = get_Alloc_type; break;
1898         case iro_Free:     ops->get_type_attr = get_Free_type; break;
1899         case iro_Cast:     ops->get_type_attr = get_Cast_type; break;
1900         default:
1901                 /* not allowed to be NULL */
1902                 if (! ops->get_type_attr)
1903                         ops->get_type_attr = get_Null_type;
1904                 break;
1905         }
1906         return ops;
1907 }
1908
1909 /** the get_entity_attr operation must be always implemented */
1910 static ir_entity *get_Null_ent(const ir_node *n)
1911 {
1912         (void) n;
1913         return NULL;
1914 }
1915
1916 /* Sets the get_type operation for an ir_op_ops. */
1917 ir_op_ops *firm_set_default_get_entity_attr(ir_opcode code, ir_op_ops *ops)
1918 {
1919         switch (code) {
1920         case iro_SymConst: ops->get_entity_attr = get_SymConst_attr_entity; break;
1921         case iro_Sel:      ops->get_entity_attr = get_Sel_entity; break;
1922         default:
1923                 /* not allowed to be NULL */
1924                 if (! ops->get_entity_attr)
1925                         ops->get_entity_attr = get_Null_ent;
1926                 break;
1927         }
1928         return ops;
1929 }
1930
1931 /* Sets the debug information of a node. */
1932 void (set_irn_dbg_info)(ir_node *n, dbg_info *db)
1933 {
1934         _set_irn_dbg_info(n, db);
1935 }
1936
1937 /**
1938  * Returns the debug information of an node.
1939  *
1940  * @param n   The node.
1941  */
1942 dbg_info *(get_irn_dbg_info)(const ir_node *n)
1943 {
1944         return _get_irn_dbg_info(n);
1945 }
1946
1947 /* checks whether a node represents a global address */
1948 int is_Global(const ir_node *node)
1949 {
1950         return is_SymConst_addr_ent(node);
1951 }
1952
1953 /* returns the entity of a global address */
1954 ir_entity *get_Global_entity(const ir_node *node)
1955 {
1956         return get_SymConst_entity(node);
1957 }
1958
1959 /*
1960  * Calculate a hash value of a node.
1961  */
1962 unsigned firm_default_hash(const ir_node *node)
1963 {
1964         unsigned h;
1965         int i, irn_arity;
1966
1967         /* hash table value = 9*(9*(9*(9*(9*arity+in[0])+in[1])+ ...)+mode)+code */
1968         h = irn_arity = get_irn_arity(node);
1969
1970         /* consider all in nodes... except the block if not a control flow. */
1971         for (i = is_cfop(node) ? -1 : 0;  i < irn_arity;  ++i) {
1972                 ir_node *pred = get_irn_n(node, i);
1973                 if (is_irn_cse_neutral(pred))
1974                         h *= 9;
1975                 else
1976                         h = 9*h + HASH_PTR(pred);
1977         }
1978
1979         /* ...mode,... */
1980         h = 9*h + HASH_PTR(get_irn_mode(node));
1981         /* ...and code */
1982         h = 9*h + HASH_PTR(get_irn_op(node));
1983
1984         return h;
1985 }  /* firm_default_hash */
1986
1987 /* include generated code */
1988 #include "gen_irnode.c.inl"