new_rd_Unknown sets the current_ir_graph right.
[libfirm] / ir / ir / ircons.c
1 /*
2  * Project:     libFIRM
3  * File name:   ir/ir/ircons.c
4  * Purpose:     Various irnode constructors.  Automatic construction
5  *              of SSA representation.
6  * Author:      Martin Trapp, Christian Schaefer
7  * Modified by: Goetz Lindenmaier, Boris Boesler
8  * Created:
9  * CVS-ID:      $Id$
10  * Copyright:   (c) 1998-2003 Universität Karlsruhe
11  * Licence:     This file protected by GPL -  GNU GENERAL PUBLIC LICENSE.
12  */
13
14 #ifdef HAVE_CONFIG_H
15 # include "config.h"
16 #endif
17
18 #ifdef HAVE_ALLOCA_H
19 #include <alloca.h>
20 #endif
21 #ifdef HAVE_MALLOC_H
22 #include <malloc.h>
23 #endif
24 #ifdef HAVE_STRING_H
25 #include <string.h>
26 #endif
27
28 # include "irprog_t.h"
29 # include "irgraph_t.h"
30 # include "irnode_t.h"
31 # include "irmode_t.h"
32 # include "ircons_t.h"
33 # include "firm_common_t.h"
34 # include "irvrfy.h"
35 # include "irop_t.h"
36 # include "iropt_t.h"
37 # include "irgmod.h"
38 # include "array.h"
39 # include "irbackedge_t.h"
40 # include "irflag_t.h"
41 # include "iredges_t.h"
42
43 #if USE_EXPLICIT_PHI_IN_STACK
44 /* A stack needed for the automatic Phi node construction in constructor
45    Phi_in. Redefinition in irgraph.c!! */
46 struct Phi_in_stack {
47   ir_node **stack;
48   int       pos;
49 };
50 typedef struct Phi_in_stack Phi_in_stack;
51 #endif
52
53 /* when we need verifying */
54 #ifdef NDEBUG
55 # define IRN_VRFY_IRG(res, irg)
56 #else
57 # define IRN_VRFY_IRG(res, irg)  irn_vrfy_irg(res, irg)
58 #endif
59
60 /**
61  * Language dependent variable initialization callback.
62  */
63 static uninitialized_local_variable_func_t *default_initialize_local_variable = NULL;
64
65 /* --------------------------------------------- */
66 /* private interfaces, for professional use only */
67 /* --------------------------------------------- */
68
69 /* Constructs a Block with a fixed number of predecessors.
70    Does not set current_block.  Can not be used with automatic
71    Phi node construction. */
72 ir_node *
73 new_rd_Block (dbg_info* db, ir_graph *irg,  int arity, ir_node **in)
74 {
75   ir_node *res;
76
77   res = new_ir_node (db, irg, NULL, op_Block, mode_BB, arity, in);
78   set_Block_matured(res, 1);
79   set_Block_block_visited(res, 0);
80
81   /* res->attr.block.exc = exc_normal; */
82   /* res->attr.block.handler_entry = 0; */
83   res->attr.block.dead        = 0;
84   res->attr.block.irg         = irg;
85   res->attr.block.backedge    = new_backedge_arr(irg->obst, arity);
86   res->attr.block.in_cg       = NULL;
87   res->attr.block.cg_backedge = NULL;
88   res->attr.block.extblk      = NULL;
89
90   IRN_VRFY_IRG(res, irg);
91   return res;
92 }
93
94 ir_node *
95 new_rd_Start (dbg_info* db, ir_graph *irg, ir_node *block)
96 {
97   ir_node *res;
98
99   res = new_ir_node(db, irg, block, op_Start, mode_T, 0, NULL);
100   /* res->attr.start.irg = irg; */
101
102   IRN_VRFY_IRG(res, irg);
103   return res;
104 }
105
106 ir_node *
107 new_rd_End (dbg_info* db, ir_graph *irg, ir_node *block)
108 {
109   ir_node *res;
110
111   res = new_ir_node(db, irg, block, op_End, mode_X, -1, NULL);
112
113   IRN_VRFY_IRG(res, irg);
114   return res;
115 }
116
117 /* Creates a Phi node with all predecessors.  Calling this constructor
118    is only allowed if the corresponding block is mature.  */
119 ir_node *
120 new_rd_Phi (dbg_info* db, ir_graph *irg, ir_node *block, int arity, ir_node **in, ir_mode *mode)
121 {
122   ir_node *res;
123   int i;
124   bool has_unknown = false;
125
126   /* Don't assert that block matured: the use of this constructor is strongly
127      restricted ... */
128   if ( get_Block_matured(block) )
129     assert( get_irn_arity(block) == arity );
130
131   res = new_ir_node(db, irg, block, op_Phi, mode, arity, in);
132
133   res->attr.phi_backedge = new_backedge_arr(irg->obst, arity);
134
135   for (i = arity-1; i >= 0; i--)
136     if (get_irn_op(in[i]) == op_Unknown) {
137       has_unknown = true;
138       break;
139     }
140
141   if (!has_unknown) res = optimize_node (res);
142   IRN_VRFY_IRG(res, irg);
143
144   /* Memory Phis in endless loops must be kept alive.
145      As we can't distinguish these easily we keep all of them alive. */
146   if ((res->op == op_Phi) && (mode == mode_M))
147     add_End_keepalive(irg->end, res);
148   return res;
149 }
150
151 ir_node *
152 new_rd_Const_type (dbg_info* db, ir_graph *irg, ir_node *block, ir_mode *mode, tarval *con, type *tp)
153 {
154   ir_node *res;
155
156   res = new_ir_node (db, irg, irg->start_block, op_Const, mode, 0, NULL);
157   res->attr.con.tv = con;
158   set_Const_type(res, tp);  /* Call method because of complex assertion. */
159   res = optimize_node (res);
160   assert(get_Const_type(res) == tp);
161   IRN_VRFY_IRG(res, irg);
162
163   return res;
164 }
165
166 ir_node *
167 new_rd_Const (dbg_info* db, ir_graph *irg, ir_node *block, ir_mode *mode, tarval *con)
168 {
169   return new_rd_Const_type (db, irg, block, mode, con, firm_unknown_type);
170 }
171
172 ir_node *
173 new_rd_Const_long (dbg_info* db, ir_graph *irg, ir_node *block, ir_mode *mode, long value)
174 {
175     return new_rd_Const(db, irg, block, mode, new_tarval_from_long(value, mode));
176 }
177
178 ir_node *
179 new_rd_Id (dbg_info* db, ir_graph *irg, ir_node *block, ir_node *val, ir_mode *mode)
180 {
181   ir_node *res;
182
183   res = new_ir_node(db, irg, block, op_Id, mode, 1, &val);
184   res = optimize_node(res);
185   IRN_VRFY_IRG(res, irg);
186   return res;
187 }
188
189 ir_node *
190 new_rd_Proj (dbg_info* db, ir_graph *irg, ir_node *block, ir_node *arg, ir_mode *mode,
191         long proj)
192 {
193   ir_node *res;
194
195   res = new_ir_node (db, irg, block, op_Proj, mode, 1, &arg);
196   res->attr.proj = proj;
197
198   assert(res);
199   assert(get_Proj_pred(res));
200   assert(get_nodes_block(get_Proj_pred(res)));
201
202   res = optimize_node(res);
203
204   IRN_VRFY_IRG(res, irg);
205   return res;
206
207 }
208
209 ir_node *
210 new_rd_defaultProj (dbg_info* db, ir_graph *irg, ir_node *block, ir_node *arg,
211            long max_proj)
212 {
213   ir_node *res;
214   assert(arg->op == op_Cond);
215   arg->attr.c.kind = fragmentary;
216   arg->attr.c.default_proj = max_proj;
217   res = new_rd_Proj (db, irg, block, arg, mode_X, max_proj);
218   return res;
219 }
220
221 ir_node *
222 new_rd_Conv (dbg_info* db, ir_graph *irg, ir_node *block, ir_node *op, ir_mode *mode)
223 {
224   ir_node *res;
225
226   res = new_ir_node(db, irg, block, op_Conv, mode, 1, &op);
227   res = optimize_node(res);
228   IRN_VRFY_IRG(res, irg);
229   return res;
230 }
231
232 ir_node *
233 new_rd_Cast (dbg_info* db, ir_graph *irg, ir_node *block, ir_node *op, type *to_tp)
234 {
235   ir_node *res;
236
237   assert(is_atomic_type(to_tp));
238
239   res = new_ir_node(db, irg, block, op_Cast, get_irn_mode(op), 1, &op);
240   res->attr.cast.totype = to_tp;
241   res = optimize_node(res);
242   IRN_VRFY_IRG(res, irg);
243   return res;
244 }
245
246 ir_node *
247 new_rd_Tuple (dbg_info* db, ir_graph *irg, ir_node *block, int arity, ir_node **in)
248 {
249   ir_node *res;
250
251   res = new_ir_node(db, irg, block, op_Tuple, mode_T, arity, in);
252   res = optimize_node (res);
253   IRN_VRFY_IRG(res, irg);
254   return res;
255 }
256
257 ir_node *
258 new_rd_Add (dbg_info* db, ir_graph *irg, ir_node *block,
259        ir_node *op1, ir_node *op2, ir_mode *mode)
260 {
261   ir_node *in[2];
262   ir_node *res;
263
264   in[0] = op1;
265   in[1] = op2;
266   res = new_ir_node(db, irg, block, op_Add, mode, 2, in);
267   res = optimize_node(res);
268   IRN_VRFY_IRG(res, irg);
269   return res;
270 }
271
272 ir_node *
273 new_rd_Sub (dbg_info* db, ir_graph *irg, ir_node *block,
274        ir_node *op1, ir_node *op2, ir_mode *mode)
275 {
276   ir_node *in[2];
277   ir_node *res;
278
279   in[0] = op1;
280   in[1] = op2;
281   res = new_ir_node (db, irg, block, op_Sub, mode, 2, in);
282   res = optimize_node (res);
283   IRN_VRFY_IRG(res, irg);
284   return res;
285 }
286
287 ir_node *
288 new_rd_Minus (dbg_info* db, ir_graph *irg, ir_node *block,
289               ir_node *op, ir_mode *mode)
290 {
291   ir_node *res;
292
293   res = new_ir_node(db, irg, block, op_Minus, mode, 1, &op);
294   res = optimize_node(res);
295   IRN_VRFY_IRG(res, irg);
296   return res;
297 }
298
299 ir_node *
300 new_rd_Mul (dbg_info* db, ir_graph *irg, ir_node *block,
301        ir_node *op1, ir_node *op2, ir_mode *mode)
302 {
303   ir_node *in[2];
304   ir_node *res;
305
306   in[0] = op1;
307   in[1] = op2;
308   res = new_ir_node(db, irg, block, op_Mul, mode, 2, in);
309   res = optimize_node(res);
310   IRN_VRFY_IRG(res, irg);
311   return res;
312 }
313
314 ir_node *
315 new_rd_Quot (dbg_info* db, ir_graph *irg, ir_node *block,
316             ir_node *memop, ir_node *op1, ir_node *op2)
317 {
318   ir_node *in[3];
319   ir_node *res;
320
321   in[0] = memop;
322   in[1] = op1;
323   in[2] = op2;
324   res = new_ir_node(db, irg, block, op_Quot, mode_T, 3, in);
325   res = optimize_node(res);
326   IRN_VRFY_IRG(res, irg);
327   return res;
328 }
329
330 ir_node *
331 new_rd_DivMod (dbg_info* db, ir_graph *irg, ir_node *block,
332           ir_node *memop, ir_node *op1, ir_node *op2)
333 {
334   ir_node *in[3];
335   ir_node *res;
336
337   in[0] = memop;
338   in[1] = op1;
339   in[2] = op2;
340   res = new_ir_node(db, irg, block, op_DivMod, mode_T, 3, in);
341   res = optimize_node(res);
342   IRN_VRFY_IRG(res, irg);
343   return res;
344 }
345
346 ir_node *
347 new_rd_Div (dbg_info* db, ir_graph *irg, ir_node *block,
348            ir_node *memop, ir_node *op1, ir_node *op2)
349 {
350   ir_node *in[3];
351   ir_node *res;
352
353   in[0] = memop;
354   in[1] = op1;
355   in[2] = op2;
356   res = new_ir_node(db, irg, block, op_Div, mode_T, 3, in);
357   res = optimize_node(res);
358   IRN_VRFY_IRG(res, irg);
359   return res;
360 }
361
362 ir_node *
363 new_rd_Mod (dbg_info* db, ir_graph *irg, ir_node *block,
364            ir_node *memop, ir_node *op1, ir_node *op2)
365 {
366   ir_node *in[3];
367   ir_node *res;
368
369   in[0] = memop;
370   in[1] = op1;
371   in[2] = op2;
372   res = new_ir_node(db, irg, block, op_Mod, mode_T, 3, in);
373   res = optimize_node(res);
374   IRN_VRFY_IRG(res, irg);
375   return res;
376 }
377
378 ir_node *
379 new_rd_And (dbg_info* db, ir_graph *irg, ir_node *block,
380            ir_node *op1, ir_node *op2, ir_mode *mode)
381 {
382   ir_node *in[2];
383   ir_node *res;
384
385   in[0] = op1;
386   in[1] = op2;
387   res = new_ir_node(db, irg, block, op_And, mode, 2, in);
388   res = optimize_node(res);
389   IRN_VRFY_IRG(res, irg);
390   return res;
391 }
392
393 ir_node *
394 new_rd_Or (dbg_info* db, ir_graph *irg, ir_node *block,
395           ir_node *op1, ir_node *op2, ir_mode *mode)
396 {
397   ir_node *in[2];
398   ir_node *res;
399
400   in[0] = op1;
401   in[1] = op2;
402   res = new_ir_node(db, irg, block, op_Or, mode, 2, in);
403   res = optimize_node(res);
404   IRN_VRFY_IRG(res, irg);
405   return res;
406 }
407
408 ir_node *
409 new_rd_Eor (dbg_info* db, ir_graph *irg, ir_node *block,
410           ir_node *op1, ir_node *op2, ir_mode *mode)
411 {
412   ir_node *in[2];
413   ir_node *res;
414
415   in[0] = op1;
416   in[1] = op2;
417   res = new_ir_node (db, irg, block, op_Eor, mode, 2, in);
418   res = optimize_node (res);
419   IRN_VRFY_IRG(res, irg);
420   return res;
421 }
422
423 ir_node *
424 new_rd_Not    (dbg_info* db, ir_graph *irg, ir_node *block,
425           ir_node *op, ir_mode *mode)
426 {
427   ir_node *res;
428
429   res = new_ir_node(db, irg, block, op_Not, mode, 1, &op);
430   res = optimize_node(res);
431   IRN_VRFY_IRG(res, irg);
432   return res;
433 }
434
435 ir_node *
436 new_rd_Shl (dbg_info* db, ir_graph *irg, ir_node *block,
437           ir_node *op, ir_node *k, ir_mode *mode)
438 {
439   ir_node *in[2];
440   ir_node *res;
441
442   in[0] = op;
443   in[1] = k;
444   res = new_ir_node(db, irg, block, op_Shl, mode, 2, in);
445   res = optimize_node(res);
446   IRN_VRFY_IRG(res, irg);
447   return res;
448 }
449
450 ir_node *
451 new_rd_Shr (dbg_info* db, ir_graph *irg, ir_node *block,
452        ir_node *op, ir_node *k, ir_mode *mode)
453 {
454   ir_node *in[2];
455   ir_node *res;
456
457   in[0] = op;
458   in[1] = k;
459   res = new_ir_node(db, irg, block, op_Shr, mode, 2, in);
460   res = optimize_node(res);
461   IRN_VRFY_IRG(res, irg);
462   return res;
463 }
464
465 ir_node *
466 new_rd_Shrs (dbg_info* db, ir_graph *irg, ir_node *block,
467        ir_node *op, ir_node *k, ir_mode *mode)
468 {
469   ir_node *in[2];
470   ir_node *res;
471
472   in[0] = op;
473   in[1] = k;
474   res = new_ir_node(db, irg, block, op_Shrs, mode, 2, in);
475   res = optimize_node(res);
476   IRN_VRFY_IRG(res, irg);
477   return res;
478 }
479
480 ir_node *
481 new_rd_Rot (dbg_info* db, ir_graph *irg, ir_node *block,
482        ir_node *op, ir_node *k, ir_mode *mode)
483 {
484   ir_node *in[2];
485   ir_node *res;
486
487   in[0] = op;
488   in[1] = k;
489   res = new_ir_node(db, irg, block, op_Rot, mode, 2, in);
490   res = optimize_node(res);
491   IRN_VRFY_IRG(res, irg);
492   return res;
493 }
494
495 ir_node *
496 new_rd_Abs (dbg_info* db, ir_graph *irg, ir_node *block,
497        ir_node *op, ir_mode *mode)
498 {
499   ir_node *res;
500
501   res = new_ir_node(db, irg, block, op_Abs, mode, 1, &op);
502   res = optimize_node (res);
503   IRN_VRFY_IRG(res, irg);
504   return res;
505 }
506
507 ir_node *
508 new_rd_Cmp (dbg_info* db, ir_graph *irg, ir_node *block,
509        ir_node *op1, ir_node *op2)
510 {
511   ir_node *in[2];
512   ir_node *res;
513   in[0] = op1;
514   in[1] = op2;
515
516   res = new_ir_node(db, irg, block, op_Cmp, mode_T, 2, in);
517   res = optimize_node(res);
518   IRN_VRFY_IRG(res, irg);
519   return res;
520 }
521
522 ir_node *
523 new_rd_Jmp (dbg_info* db, ir_graph *irg, ir_node *block)
524 {
525   ir_node *res;
526
527   res = new_ir_node (db, irg, block, op_Jmp, mode_X, 0, NULL);
528   res = optimize_node (res);
529   IRN_VRFY_IRG (res, irg);
530   return res;
531 }
532
533 ir_node *
534 new_rd_IJmp (dbg_info* db, ir_graph *irg, ir_node *block, ir_node *tgt)
535 {
536   ir_node *res;
537
538   res = new_ir_node (db, irg, block, op_IJmp, mode_X, 1, &tgt);
539   res = optimize_node (res);
540   IRN_VRFY_IRG (res, irg);
541
542   if (get_irn_op(res) == op_IJmp) /* still an IJmp */
543     keep_alive(res);
544   return res;
545 }
546
547 ir_node *
548 new_rd_Cond (dbg_info* db, ir_graph *irg, ir_node *block, ir_node *c)
549 {
550   ir_node *res;
551
552   res = new_ir_node (db, irg, block, op_Cond, mode_T, 1, &c);
553   res->attr.c.kind         = dense;
554   res->attr.c.default_proj = 0;
555   res = optimize_node (res);
556   IRN_VRFY_IRG(res, irg);
557   return res;
558 }
559
560 ir_node *
561 new_rd_Call (dbg_info* db, ir_graph *irg, ir_node *block, ir_node *store,
562         ir_node *callee, int arity, ir_node **in, type *tp)
563 {
564   ir_node **r_in;
565   ir_node *res;
566   int r_arity;
567
568   r_arity = arity+2;
569   NEW_ARR_A(ir_node *, r_in, r_arity);
570   r_in[0] = store;
571   r_in[1] = callee;
572   memcpy(&r_in[2], in, sizeof(ir_node *) * arity);
573
574   res = new_ir_node(db, irg, block, op_Call, mode_T, r_arity, r_in);
575
576   assert((get_unknown_type() == tp) || is_Method_type(tp));
577   set_Call_type(res, tp);
578   res->attr.call.exc.pin_state = op_pin_state_pinned;
579   res->attr.call.callee_arr    = NULL;
580   res = optimize_node(res);
581   IRN_VRFY_IRG(res, irg);
582   return res;
583 }
584
585 ir_node *
586 new_rd_Return (dbg_info* db, ir_graph *irg, ir_node *block,
587               ir_node *store, int arity, ir_node **in)
588 {
589   ir_node **r_in;
590   ir_node *res;
591   int r_arity;
592
593   r_arity = arity+1;
594   NEW_ARR_A (ir_node *, r_in, r_arity);
595   r_in[0] = store;
596   memcpy(&r_in[1], in, sizeof(ir_node *) * arity);
597   res = new_ir_node(db, irg, block, op_Return, mode_X, r_arity, r_in);
598   res = optimize_node(res);
599   IRN_VRFY_IRG(res, irg);
600   return res;
601 }
602
603 ir_node *
604 new_rd_Raise (dbg_info* db, ir_graph *irg, ir_node *block, ir_node *store, ir_node *obj)
605 {
606   ir_node *in[2];
607   ir_node *res;
608
609   in[0] = store;
610   in[1] = obj;
611   res = new_ir_node(db, irg, block, op_Raise, mode_T, 2, in);
612   res = optimize_node(res);
613   IRN_VRFY_IRG(res, irg);
614   return res;
615 }
616
617 ir_node *
618 new_rd_Load (dbg_info* db, ir_graph *irg, ir_node *block,
619         ir_node *store, ir_node *adr, ir_mode *mode)
620 {
621   ir_node *in[2];
622   ir_node *res;
623
624   in[0] = store;
625   in[1] = adr;
626   res = new_ir_node(db, irg, block, op_Load, mode_T, 2, in);
627   res->attr.load.exc.pin_state = op_pin_state_pinned;
628   res->attr.load.load_mode     = mode;
629   res->attr.load.volatility    = volatility_non_volatile;
630   res = optimize_node(res);
631   IRN_VRFY_IRG(res, irg);
632   return res;
633 }
634
635 ir_node *
636 new_rd_Store (dbg_info* db, ir_graph *irg, ir_node *block,
637          ir_node *store, ir_node *adr, ir_node *val)
638 {
639   ir_node *in[3];
640   ir_node *res;
641
642   in[0] = store;
643   in[1] = adr;
644   in[2] = val;
645   res = new_ir_node(db, irg, block, op_Store, mode_T, 3, in);
646   res->attr.store.exc.pin_state = op_pin_state_pinned;
647   res->attr.store.volatility    = volatility_non_volatile;
648   res = optimize_node(res);
649   IRN_VRFY_IRG(res, irg);
650   return res;
651 }
652
653 ir_node *
654 new_rd_Alloc (dbg_info* db, ir_graph *irg, ir_node *block, ir_node *store,
655         ir_node *size, type *alloc_type, where_alloc where)
656 {
657   ir_node *in[2];
658   ir_node *res;
659
660   in[0] = store;
661   in[1] = size;
662   res = new_ir_node(db, irg, block, op_Alloc, mode_T, 2, in);
663   res->attr.a.exc.pin_state = op_pin_state_pinned;
664   res->attr.a.where         = where;
665   res->attr.a.type          = alloc_type;
666   res = optimize_node(res);
667   IRN_VRFY_IRG(res, irg);
668   return res;
669 }
670
671 ir_node *
672 new_rd_Free (dbg_info* db, ir_graph *irg, ir_node *block, ir_node *store,
673         ir_node *ptr, ir_node *size, type *free_type, where_alloc where)
674 {
675   ir_node *in[3];
676   ir_node *res;
677
678   in[0] = store;
679   in[1] = ptr;
680   in[2] = size;
681   res = new_ir_node (db, irg, block, op_Free, mode_M, 3, in);
682   res->attr.f.where = where;
683   res->attr.f.type  = free_type;
684   res = optimize_node(res);
685   IRN_VRFY_IRG(res, irg);
686   return res;
687 }
688
689 ir_node *
690 new_rd_Sel (dbg_info* db, ir_graph *irg, ir_node *block, ir_node *store, ir_node *objptr,
691            int arity, ir_node **in, entity *ent)
692 {
693   ir_node **r_in;
694   ir_node *res;
695   int r_arity;
696
697   assert(ent != NULL && is_entity(ent) && "entity expected in Sel construction");
698
699   r_arity = arity + 2;
700   NEW_ARR_A(ir_node *, r_in, r_arity);  /* uses alloca */
701   r_in[0] = store;
702   r_in[1] = objptr;
703   memcpy(&r_in[2], in, sizeof(ir_node *) * arity);
704   /*
705    * FIXM: Sel's can select functions which should be of mode mode_P_code.
706    */
707   res = new_ir_node(db, irg, block, op_Sel, mode_P_data, r_arity, r_in);
708   res->attr.s.ent = ent;
709   res = optimize_node(res);
710   IRN_VRFY_IRG(res, irg);
711   return res;
712 }
713
714 ir_node *
715 new_rd_InstOf (dbg_info *db, ir_graph *irg, ir_node *block, ir_node *store,
716            ir_node *objptr, type *ent)
717 {
718   ir_node **r_in;
719   ir_node *res;
720   int r_arity;
721
722   r_arity = 2;
723   NEW_ARR_A(ir_node *, r_in, r_arity);
724   r_in[0] = store;
725   r_in[1] = objptr;
726
727   res = new_ir_node(db, irg, block, op_Sel, mode_T, r_arity, r_in);
728   res->attr.io.ent = ent;
729
730   /* res = optimize(res); */
731   IRN_VRFY_IRG(res, irg);
732   return res;
733 }
734
735 ir_node *
736 new_rd_SymConst_type (dbg_info* db, ir_graph *irg, ir_node *block, symconst_symbol value,
737               symconst_kind symkind, type *tp) {
738   ir_node *res;
739   ir_mode *mode;
740
741   if ((symkind == symconst_addr_name) || (symkind == symconst_addr_ent))
742     mode = mode_P_data;   /* FIXME: can be mode_P_code */
743   else
744     mode = mode_Iu;
745
746   res = new_ir_node(db, irg, block, op_SymConst, mode, 0, NULL);
747
748   res->attr.i.num = symkind;
749   res->attr.i.sym = value;
750   res->attr.i.tp  = tp;
751
752   res = optimize_node(res);
753   IRN_VRFY_IRG(res, irg);
754   return res;
755 }
756
757 ir_node *
758 new_rd_SymConst (dbg_info* db, ir_graph *irg, ir_node *block, symconst_symbol value,
759          symconst_kind symkind)
760 {
761   ir_node *res = new_rd_SymConst_type(db, irg, block, value, symkind, firm_unknown_type);
762   return res;
763 }
764
765 ir_node *new_rd_SymConst_addr_ent (dbg_info *db, ir_graph *irg, entity *symbol, type *tp) {
766   symconst_symbol sym = {(type *)symbol};
767   return new_rd_SymConst_type(db, irg, irg->start_block, sym, symconst_addr_ent, tp);
768 }
769
770 ir_node *new_rd_SymConst_addr_name (dbg_info *db, ir_graph *irg, ident *symbol, type *tp) {
771   symconst_symbol sym = {(type *)symbol};
772   return new_rd_SymConst_type(db, irg, irg->start_block, sym, symconst_addr_name, tp);
773 }
774
775 ir_node *new_rd_SymConst_type_tag (dbg_info *db, ir_graph *irg, type *symbol, type *tp) {
776   symconst_symbol sym = {symbol};
777   return new_rd_SymConst_type(db, irg, irg->start_block, sym, symconst_type_tag, tp);
778 }
779
780 ir_node *new_rd_SymConst_size (dbg_info *db, ir_graph *irg, type *symbol, type *tp) {
781   symconst_symbol sym = {symbol};
782   return new_rd_SymConst_type(db, irg, irg->start_block, sym, symconst_size, tp);
783 }
784
785 ir_node *
786 new_rd_Sync (dbg_info* db, ir_graph *irg, ir_node *block, int arity, ir_node **in)
787 {
788   ir_node *res;
789
790   res = new_ir_node(db, irg, block, op_Sync, mode_M, arity, in);
791   res = optimize_node(res);
792   IRN_VRFY_IRG(res, irg);
793   return res;
794 }
795
796 ir_node *
797 new_rd_Bad (ir_graph *irg)
798 {
799   return irg->bad;
800 }
801
802 ir_node *
803 new_rd_Confirm (dbg_info *db, ir_graph *irg, ir_node *block, ir_node *val, ir_node *bound, pn_Cmp cmp)
804 {
805   ir_node *in[2], *res;
806
807   in[0] = val;
808   in[1] = bound;
809   res = new_ir_node (db, irg, block, op_Confirm, get_irn_mode(val), 2, in);
810   res->attr.confirm_cmp = cmp;
811   res = optimize_node (res);
812   IRN_VRFY_IRG(res, irg);
813   return res;
814 }
815
816 /* this function is often called with current_ir_graph unset */
817 ir_node *
818 new_rd_Unknown (ir_graph *irg, ir_mode *m)
819 {
820   ir_graph *rem = current_ir_graph;
821   ir_node *res;
822
823   current_ir_graph = irg;
824   res = new_ir_node(NULL, irg, irg->start_block, op_Unknown, m, 0, NULL);
825   res = optimize_node(res);
826   current_ir_graph = rem;
827   return res;
828 }
829
830 ir_node *
831 new_rd_CallBegin (dbg_info *db, ir_graph *irg, ir_node *block, ir_node *call)
832 {
833   ir_node *in[1];
834   ir_node *res;
835
836   in[0] = get_Call_ptr(call);
837   res = new_ir_node(db, irg, block, op_CallBegin, mode_T, 1, in);
838   /* res->attr.callbegin.irg = irg; */
839   res->attr.callbegin.call = call;
840   res = optimize_node(res);
841   IRN_VRFY_IRG(res, irg);
842   return res;
843 }
844
845 ir_node *
846 new_rd_EndReg (dbg_info *db, ir_graph *irg, ir_node *block)
847 {
848   ir_node *res;
849
850   res = new_ir_node(db, irg, block, op_EndReg, mode_T, -1, NULL);
851   irg->end_reg = res;
852   IRN_VRFY_IRG(res, irg);
853   return res;
854 }
855
856 ir_node *
857 new_rd_EndExcept (dbg_info *db, ir_graph *irg, ir_node *block)
858 {
859   ir_node *res;
860
861   res = new_ir_node(db, irg, block, op_EndExcept, mode_T, -1, NULL);
862   irg->end_except = res;
863   IRN_VRFY_IRG (res, irg);
864   return res;
865 }
866
867 ir_node *
868 new_rd_Break (dbg_info *db, ir_graph *irg, ir_node *block)
869 {
870   ir_node *res;
871
872   res = new_ir_node(db, irg, block, op_Break, mode_X, 0, NULL);
873   res = optimize_node(res);
874   IRN_VRFY_IRG(res, irg);
875   return res;
876 }
877
878 ir_node *
879 new_rd_Filter (dbg_info *db, ir_graph *irg, ir_node *block, ir_node *arg, ir_mode *mode,
880            long proj)
881 {
882   ir_node *res;
883
884   res = new_ir_node(db, irg, block, op_Filter, mode, 1, &arg);
885   res->attr.filter.proj = proj;
886   res->attr.filter.in_cg = NULL;
887   res->attr.filter.backedge = NULL;
888
889   assert(res);
890   assert(get_Proj_pred(res));
891   assert(get_nodes_block(get_Proj_pred(res)));
892
893   res = optimize_node(res);
894   IRN_VRFY_IRG(res, irg);
895   return res;
896 }
897
898 ir_node *
899 new_rd_NoMem (ir_graph *irg) {
900   return irg->no_mem;
901 }
902
903 ir_node *
904 new_rd_Mux  (dbg_info *db, ir_graph *irg, ir_node *block,
905     ir_node *sel, ir_node *ir_false, ir_node *ir_true, ir_mode *mode)
906 {
907   ir_node *in[3];
908   ir_node *res;
909
910   in[0] = sel;
911   in[1] = ir_false;
912   in[2] = ir_true;
913
914   res = new_ir_node(db, irg, block, op_Mux, mode, 3, in);
915   assert(res);
916
917   res = optimize_node(res);
918   IRN_VRFY_IRG(res, irg);
919   return res;
920 }
921
922
923 ir_node *new_r_Block  (ir_graph *irg,  int arity, ir_node **in) {
924   return new_rd_Block(NULL, irg, arity, in);
925 }
926 ir_node *new_r_Start  (ir_graph *irg, ir_node *block) {
927   return new_rd_Start(NULL, irg, block);
928 }
929 ir_node *new_r_End    (ir_graph *irg, ir_node *block) {
930   return new_rd_End(NULL, irg, block);
931 }
932 ir_node *new_r_Jmp    (ir_graph *irg, ir_node *block) {
933   return new_rd_Jmp(NULL, irg, block);
934 }
935 ir_node *new_r_IJmp   (ir_graph *irg, ir_node *block, ir_node *tgt) {
936   return new_rd_IJmp(NULL, irg, block, tgt);
937 }
938 ir_node *new_r_Cond   (ir_graph *irg, ir_node *block, ir_node *c) {
939   return new_rd_Cond(NULL, irg, block, c);
940 }
941 ir_node *new_r_Return (ir_graph *irg, ir_node *block,
942                ir_node *store, int arity, ir_node **in) {
943   return new_rd_Return(NULL, irg, block, store, arity, in);
944 }
945 ir_node *new_r_Raise  (ir_graph *irg, ir_node *block,
946                ir_node *store, ir_node *obj) {
947   return new_rd_Raise(NULL, irg, block, store, obj);
948 }
949 ir_node *new_r_Const  (ir_graph *irg, ir_node *block,
950                ir_mode *mode, tarval *con) {
951   return new_rd_Const(NULL, irg, block, mode, con);
952 }
953
954 ir_node *new_r_Const_long(ir_graph *irg, ir_node *block,
955                ir_mode *mode, long value) {
956   return new_rd_Const_long(NULL, irg, block, mode, value);
957 }
958
959 ir_node *new_r_Const_type(ir_graph *irg, ir_node *block,
960                ir_mode *mode, tarval *con, type *tp) {
961   return new_rd_Const_type(NULL, irg, block, mode, con, tp);
962 }
963
964 ir_node *new_r_SymConst (ir_graph *irg, ir_node *block,
965                        symconst_symbol value, symconst_kind symkind) {
966   return new_rd_SymConst(NULL, irg, block, value, symkind);
967 }
968 ir_node *new_r_Sel    (ir_graph *irg, ir_node *block, ir_node *store,
969                   ir_node *objptr, int n_index, ir_node **index,
970                   entity *ent) {
971   return new_rd_Sel(NULL, irg, block, store, objptr, n_index, index, ent);
972 }
973 ir_node *new_r_InstOf (ir_graph *irg, ir_node *block, ir_node *store, ir_node *objptr,
974                   type *ent) {
975   return (new_rd_InstOf (NULL, irg, block, store, objptr, ent));
976 }
977 ir_node *new_r_Call   (ir_graph *irg, ir_node *block, ir_node *store,
978                   ir_node *callee, int arity, ir_node **in,
979                   type *tp) {
980   return new_rd_Call(NULL, irg, block, store, callee, arity, in, tp);
981 }
982 ir_node *new_r_Add    (ir_graph *irg, ir_node *block,
983                   ir_node *op1, ir_node *op2, ir_mode *mode) {
984   return new_rd_Add(NULL, irg, block, op1, op2, mode);
985 }
986 ir_node *new_r_Sub    (ir_graph *irg, ir_node *block,
987                   ir_node *op1, ir_node *op2, ir_mode *mode) {
988   return new_rd_Sub(NULL, irg, block, op1, op2, mode);
989 }
990 ir_node *new_r_Minus  (ir_graph *irg, ir_node *block,
991                   ir_node *op,  ir_mode *mode) {
992   return new_rd_Minus(NULL, irg, block,  op, mode);
993 }
994 ir_node *new_r_Mul    (ir_graph *irg, ir_node *block,
995                   ir_node *op1, ir_node *op2, ir_mode *mode) {
996   return new_rd_Mul(NULL, irg, block, op1, op2, mode);
997 }
998 ir_node *new_r_Quot   (ir_graph *irg, ir_node *block,
999                   ir_node *memop, ir_node *op1, ir_node *op2) {
1000   return new_rd_Quot(NULL, irg, block, memop, op1, op2);
1001 }
1002 ir_node *new_r_DivMod (ir_graph *irg, ir_node *block,
1003                   ir_node *memop, ir_node *op1, ir_node *op2) {
1004   return new_rd_DivMod(NULL, irg, block, memop, op1, op2);
1005 }
1006 ir_node *new_r_Div    (ir_graph *irg, ir_node *block,
1007                   ir_node *memop, ir_node *op1, ir_node *op2) {
1008   return new_rd_Div(NULL, irg, block, memop, op1, op2);
1009 }
1010 ir_node *new_r_Mod    (ir_graph *irg, ir_node *block,
1011                   ir_node *memop, ir_node *op1, ir_node *op2) {
1012   return new_rd_Mod(NULL, irg, block, memop, op1, op2);
1013 }
1014 ir_node *new_r_Abs    (ir_graph *irg, ir_node *block,
1015                   ir_node *op, ir_mode *mode) {
1016   return new_rd_Abs(NULL, irg, block, op, mode);
1017 }
1018 ir_node *new_r_And    (ir_graph *irg, ir_node *block,
1019                   ir_node *op1, ir_node *op2, ir_mode *mode) {
1020   return new_rd_And(NULL, irg, block,  op1, op2, mode);
1021 }
1022 ir_node *new_r_Or     (ir_graph *irg, ir_node *block,
1023                   ir_node *op1, ir_node *op2, ir_mode *mode) {
1024   return new_rd_Or(NULL, irg, block,  op1, op2, mode);
1025 }
1026 ir_node *new_r_Eor    (ir_graph *irg, ir_node *block,
1027                   ir_node *op1, ir_node *op2, ir_mode *mode) {
1028   return new_rd_Eor(NULL, irg, block,  op1, op2, mode);
1029 }
1030 ir_node *new_r_Not    (ir_graph *irg, ir_node *block,
1031                ir_node *op, ir_mode *mode) {
1032   return new_rd_Not(NULL, irg, block, op, mode);
1033 }
1034 ir_node *new_r_Cmp    (ir_graph *irg, ir_node *block,
1035                ir_node *op1, ir_node *op2) {
1036   return new_rd_Cmp(NULL, irg, block, op1, op2);
1037 }
1038 ir_node *new_r_Shl    (ir_graph *irg, ir_node *block,
1039                ir_node *op, ir_node *k, ir_mode *mode) {
1040   return new_rd_Shl(NULL, irg, block, op, k, mode);
1041 }
1042 ir_node *new_r_Shr    (ir_graph *irg, ir_node *block,
1043                ir_node *op, ir_node *k, ir_mode *mode) {
1044   return new_rd_Shr(NULL, irg, block, op, k, mode);
1045 }
1046 ir_node *new_r_Shrs   (ir_graph *irg, ir_node *block,
1047                ir_node *op, ir_node *k, ir_mode *mode) {
1048   return new_rd_Shrs(NULL, irg, block, op, k, mode);
1049 }
1050 ir_node *new_r_Rot    (ir_graph *irg, ir_node *block,
1051                ir_node *op, ir_node *k, ir_mode *mode) {
1052   return new_rd_Rot(NULL, irg, block, op, k, mode);
1053 }
1054 ir_node *new_r_Conv   (ir_graph *irg, ir_node *block,
1055                ir_node *op, ir_mode *mode) {
1056   return new_rd_Conv(NULL, irg, block, op, mode);
1057 }
1058 ir_node *new_r_Cast   (ir_graph *irg, ir_node *block, ir_node *op, type *to_tp) {
1059   return new_rd_Cast(NULL, irg, block, op, to_tp);
1060 }
1061 ir_node *new_r_Phi    (ir_graph *irg, ir_node *block, int arity,
1062                ir_node **in, ir_mode *mode) {
1063   return new_rd_Phi(NULL, irg, block, arity, in, mode);
1064 }
1065 ir_node *new_r_Load   (ir_graph *irg, ir_node *block,
1066                ir_node *store, ir_node *adr, ir_mode *mode) {
1067   return new_rd_Load(NULL, irg, block, store, adr, mode);
1068 }
1069 ir_node *new_r_Store  (ir_graph *irg, ir_node *block,
1070                ir_node *store, ir_node *adr, ir_node *val) {
1071   return new_rd_Store(NULL, irg, block, store, adr, val);
1072 }
1073 ir_node *new_r_Alloc  (ir_graph *irg, ir_node *block, ir_node *store,
1074                ir_node *size, type *alloc_type, where_alloc where) {
1075   return new_rd_Alloc(NULL, irg, block, store, size, alloc_type, where);
1076 }
1077 ir_node *new_r_Free   (ir_graph *irg, ir_node *block, ir_node *store,
1078                ir_node *ptr, ir_node *size, type *free_type, where_alloc where) {
1079   return new_rd_Free(NULL, irg, block, store, ptr, size, free_type, where);
1080 }
1081 ir_node *new_r_Sync   (ir_graph *irg, ir_node *block, int arity, ir_node **in) {
1082   return new_rd_Sync(NULL, irg, block, arity, in);
1083 }
1084 ir_node *new_r_Proj   (ir_graph *irg, ir_node *block, ir_node *arg,
1085                ir_mode *mode, long proj) {
1086   return new_rd_Proj(NULL, irg, block, arg, mode, proj);
1087 }
1088 ir_node *new_r_defaultProj (ir_graph *irg, ir_node *block, ir_node *arg,
1089                 long max_proj) {
1090   return new_rd_defaultProj(NULL, irg, block, arg, max_proj);
1091 }
1092 ir_node *new_r_Tuple  (ir_graph *irg, ir_node *block,
1093                int arity, ir_node **in) {
1094   return new_rd_Tuple(NULL, irg, block, arity, in );
1095 }
1096 ir_node *new_r_Id     (ir_graph *irg, ir_node *block,
1097                ir_node *val, ir_mode *mode) {
1098   return new_rd_Id(NULL, irg, block, val, mode);
1099 }
1100 ir_node *new_r_Bad    (ir_graph *irg) {
1101   return new_rd_Bad(irg);
1102 }
1103 ir_node *new_r_Confirm (ir_graph *irg, ir_node *block, ir_node *val, ir_node *bound, pn_Cmp cmp) {
1104   return new_rd_Confirm (NULL, irg, block, val, bound, cmp);
1105 }
1106 ir_node *new_r_Unknown (ir_graph *irg, ir_mode *m) {
1107   return new_rd_Unknown(irg, m);
1108 }
1109 ir_node *new_r_CallBegin (ir_graph *irg, ir_node *block, ir_node *callee) {
1110   return new_rd_CallBegin(NULL, irg, block, callee);
1111 }
1112 ir_node *new_r_EndReg (ir_graph *irg, ir_node *block) {
1113   return new_rd_EndReg(NULL, irg, block);
1114 }
1115 ir_node *new_r_EndExcept (ir_graph *irg, ir_node *block) {
1116   return new_rd_EndExcept(NULL, irg, block);
1117 }
1118 ir_node *new_r_Break  (ir_graph *irg, ir_node *block) {
1119   return new_rd_Break(NULL, irg, block);
1120 }
1121 ir_node *new_r_Filter (ir_graph *irg, ir_node *block, ir_node *arg,
1122                ir_mode *mode, long proj) {
1123   return new_rd_Filter(NULL, irg, block, arg, mode, proj);
1124 }
1125 ir_node *new_r_NoMem  (ir_graph *irg) {
1126   return new_rd_NoMem(irg);
1127 }
1128 ir_node *new_r_Mux (ir_graph *irg, ir_node *block,
1129     ir_node *sel, ir_node *ir_false, ir_node *ir_true, ir_mode *mode) {
1130   return new_rd_Mux(NULL, irg, block, sel, ir_false, ir_true, mode);
1131 }
1132
1133
1134 /** ********************/
1135 /** public interfaces  */
1136 /** construction tools */
1137
1138 /**
1139  *
1140  *   - create a new Start node in the current block
1141  *
1142  *   @return s - pointer to the created Start node
1143  *
1144  *
1145  */
1146 ir_node *
1147 new_d_Start (dbg_info* db)
1148 {
1149   ir_node *res;
1150
1151   res = new_ir_node (db, current_ir_graph, current_ir_graph->current_block,
1152              op_Start, mode_T, 0, NULL);
1153   /* res->attr.start.irg = current_ir_graph; */
1154
1155   res = optimize_node(res);
1156   IRN_VRFY_IRG(res, current_ir_graph);
1157   return res;
1158 }
1159
1160 ir_node *
1161 new_d_End (dbg_info* db)
1162 {
1163   ir_node *res;
1164   res = new_ir_node(db, current_ir_graph,  current_ir_graph->current_block,
1165              op_End, mode_X, -1, NULL);
1166   res = optimize_node(res);
1167   IRN_VRFY_IRG(res, current_ir_graph);
1168
1169   return res;
1170 }
1171
1172 /* Constructs a Block with a fixed number of predecessors.
1173    Does set current_block.  Can be used with automatic Phi
1174    node construction. */
1175 ir_node *
1176 new_d_Block (dbg_info* db, int arity, ir_node **in)
1177 {
1178   ir_node *res;
1179   int i;
1180   bool has_unknown = false;
1181
1182   res = new_rd_Block(db, current_ir_graph, arity, in);
1183
1184   /* Create and initialize array for Phi-node construction. */
1185   if (get_irg_phase_state(current_ir_graph) == phase_building) {
1186     res->attr.block.graph_arr = NEW_ARR_D(ir_node *, current_ir_graph->obst,
1187                       current_ir_graph->n_loc);
1188     memset(res->attr.block.graph_arr, 0, sizeof(ir_node *)*current_ir_graph->n_loc);
1189   }
1190
1191   for (i = arity-1; i >= 0; i--)
1192     if (get_irn_op(in[i]) == op_Unknown) {
1193       has_unknown = true;
1194       break;
1195     }
1196
1197   if (!has_unknown) res = optimize_node(res);
1198   current_ir_graph->current_block = res;
1199
1200   IRN_VRFY_IRG(res, current_ir_graph);
1201
1202   return res;
1203 }
1204
1205 /* ***********************************************************************/
1206 /* Methods necessary for automatic Phi node creation                     */
1207 /*
1208   ir_node *phi_merge            (ir_node *block, int pos, ir_mode *mode, ir_node **nin, int ins)
1209   ir_node *get_r_value_internal (ir_node *block, int pos, ir_mode *mode);
1210   ir_node *new_rd_Phi0          (ir_graph *irg, ir_node *block, ir_mode *mode)
1211   ir_node *new_rd_Phi_in        (ir_graph *irg, ir_node *block, ir_mode *mode, ir_node **in, int ins)
1212
1213   Call Graph:   ( A ---> B == A "calls" B)
1214
1215        get_value         mature_immBlock
1216           |                   |
1217           |                   |
1218           |                   |
1219           |          ---> phi_merge
1220           |         /       /   \
1221           |        /       /     \
1222          \|/      /      |/_      \
1223        get_r_value_internal        |
1224                 |                  |
1225             |                  |
1226            \|/                \|/
1227         new_rd_Phi0          new_rd_Phi_in
1228
1229 * *************************************************************************** */
1230
1231 /** Creates a Phi node with 0 predecessors */
1232 static INLINE ir_node *
1233 new_rd_Phi0 (ir_graph *irg, ir_node *block, ir_mode *mode)
1234 {
1235   ir_node *res;
1236
1237   res = new_ir_node(NULL, irg, block, op_Phi, mode, 0, NULL);
1238   IRN_VRFY_IRG(res, irg);
1239   return res;
1240 }
1241
1242 /* There are two implementations of the Phi node construction.  The first
1243    is faster, but does not work for blocks with more than 2 predecessors.
1244    The second works always but is slower and causes more unnecessary Phi
1245    nodes.
1246    Select the implementations by the following preprocessor flag set in
1247    common/common.h: */
1248 #if USE_FAST_PHI_CONSTRUCTION
1249
1250 /* This is a stack used for allocating and deallocating nodes in
1251    new_rd_Phi_in.  The original implementation used the obstack
1252    to model this stack, now it is explicit.  This reduces side effects.
1253 */
1254 #if USE_EXPLICIT_PHI_IN_STACK
1255 Phi_in_stack *
1256 new_Phi_in_stack(void) {
1257   Phi_in_stack *res;
1258
1259   res = (Phi_in_stack *) malloc ( sizeof (Phi_in_stack));
1260
1261   res->stack = NEW_ARR_F (ir_node *, 0);
1262   res->pos = 0;
1263
1264   return res;
1265 }
1266
1267 void
1268 free_Phi_in_stack(Phi_in_stack *s) {
1269   DEL_ARR_F(s->stack);
1270   free(s);
1271 }
1272 static INLINE void
1273 free_to_Phi_in_stack(ir_node *phi) {
1274   if (ARR_LEN(current_ir_graph->Phi_in_stack->stack) ==
1275       current_ir_graph->Phi_in_stack->pos)
1276     ARR_APP1 (ir_node *, current_ir_graph->Phi_in_stack->stack, phi);
1277   else
1278     current_ir_graph->Phi_in_stack->stack[current_ir_graph->Phi_in_stack->pos] = phi;
1279
1280   (current_ir_graph->Phi_in_stack->pos)++;
1281 }
1282
1283 static INLINE ir_node *
1284 alloc_or_pop_from_Phi_in_stack(ir_graph *irg, ir_node *block, ir_mode *mode,
1285          int arity, ir_node **in) {
1286   ir_node *res;
1287   ir_node **stack = current_ir_graph->Phi_in_stack->stack;
1288   int pos = current_ir_graph->Phi_in_stack->pos;
1289
1290
1291   if (pos == 0) {
1292     /* We need to allocate a new node */
1293     res = new_ir_node (db, irg, block, op_Phi, mode, arity, in);
1294     res->attr.phi_backedge = new_backedge_arr(irg->obst, arity);
1295   } else {
1296     /* reuse the old node and initialize it again. */
1297     res = stack[pos-1];
1298
1299     assert (res->kind == k_ir_node);
1300     assert (res->op == op_Phi);
1301     res->mode = mode;
1302     res->visited = 0;
1303     res->link = NULL;
1304     assert (arity >= 0);
1305     /* ???!!! How to free the old in array??  Not at all: on obstack ?!! */
1306     res->in = NEW_ARR_D (ir_node *, irg->obst, (arity+1));
1307     res->in[0] = block;
1308     memcpy (&res->in[1], in, sizeof (ir_node *) * arity);
1309
1310     (current_ir_graph->Phi_in_stack->pos)--;
1311   }
1312   return res;
1313 }
1314 #endif /* USE_EXPLICIT_PHI_IN_STACK */
1315
1316 /* Creates a Phi node with a given, fixed array **in of predecessors.
1317    If the Phi node is unnecessary, as the same value reaches the block
1318    through all control flow paths, it is eliminated and the value
1319    returned directly.  This constructor is only intended for use in
1320    the automatic Phi node generation triggered by get_value or mature.
1321    The implementation is quite tricky and depends on the fact, that
1322    the nodes are allocated on a stack:
1323    The in array contains predecessors and NULLs.  The NULLs appear,
1324    if get_r_value_internal, that computed the predecessors, reached
1325    the same block on two paths.  In this case the same value reaches
1326    this block on both paths, there is no definition in between.  We need
1327    not allocate a Phi where these path's merge, but we have to communicate
1328    this fact to the caller.  This happens by returning a pointer to the
1329    node the caller _will_ allocate.  (Yes, we predict the address. We can
1330    do so because the nodes are allocated on the obstack.)  The caller then
1331    finds a pointer to itself and, when this routine is called again,
1332    eliminates itself.
1333    */
1334 static INLINE ir_node *
1335 new_rd_Phi_in (ir_graph *irg, ir_node *block, ir_mode *mode, ir_node **in, int ins)
1336 {
1337   int i;
1338   ir_node *res, *known;
1339
1340   /* Allocate a new node on the obstack.  This can return a node to
1341      which some of the pointers in the in-array already point.
1342      Attention: the constructor copies the in array, i.e., the later
1343      changes to the array in this routine do not affect the
1344      constructed node!  If the in array contains NULLs, there will be
1345      missing predecessors in the returned node.  Is this a possible
1346      internal state of the Phi node generation? */
1347 #if USE_EXPLICIT_PHI_IN_STACK
1348   res = known = alloc_or_pop_from_Phi_in_stack(irg, block, mode, ins, in);
1349 #else
1350   res = known = new_ir_node (NULL, irg, block, op_Phi, mode, ins, in);
1351   res->attr.phi_backedge = new_backedge_arr(irg->obst, ins);
1352 #endif
1353
1354   /* The in-array can contain NULLs.  These were returned by
1355      get_r_value_internal if it reached the same block/definition on a
1356      second path.  The NULLs are replaced by the node itself to
1357      simplify the test in the next loop. */
1358   for (i = 0;  i < ins;  ++i) {
1359     if (in[i] == NULL)
1360       in[i] = res;
1361   }
1362
1363   /* This loop checks whether the Phi has more than one predecessor.
1364      If so, it is a real Phi node and we break the loop.  Else the Phi
1365      node merges the same definition on several paths and therefore is
1366      not needed. */
1367   for (i = 0;  i < ins;  ++i) {
1368     if (in[i] == res || in[i] == known)
1369       continue;
1370
1371     if (known == res)
1372       known = in[i];
1373     else
1374       break;
1375   }
1376
1377   /* i==ins: there is at most one predecessor, we don't need a phi node. */
1378   if (i==ins) {
1379 #if USE_EXPLICIT_PHI_IN_STACK
1380     free_to_Phi_in_stack(res);
1381 #else
1382     edges_node_deleted(res, current_ir_graph);
1383     obstack_free(current_ir_graph->obst, res);
1384 #endif
1385     res = known;
1386   } else {
1387     res = optimize_node (res);
1388     IRN_VRFY_IRG(res, irg);
1389   }
1390
1391   /* return the pointer to the Phi node.  This node might be deallocated! */
1392   return res;
1393 }
1394
1395 static ir_node *
1396 get_r_value_internal (ir_node *block, int pos, ir_mode *mode);
1397
1398 /**
1399     allocates and returns this node.  The routine called to allocate the
1400     node might optimize it away and return a real value, or even a pointer
1401     to a deallocated Phi node on top of the obstack!
1402     This function is called with an in-array of proper size. **/
1403 static ir_node *
1404 phi_merge (ir_node *block, int pos, ir_mode *mode, ir_node **nin, int ins)
1405 {
1406   ir_node *prevBlock, *res;
1407   int i;
1408
1409   /* This loop goes to all predecessor blocks of the block the Phi node is in
1410      and there finds the operands of the Phi node by calling
1411      get_r_value_internal. */
1412   for (i = 1;  i <= ins;  ++i) {
1413     assert (block->in[i]);
1414     prevBlock = block->in[i]->in[0]; /* go past control flow op to prev block */
1415     assert (prevBlock);
1416     nin[i-1] = get_r_value_internal (prevBlock, pos, mode);
1417   }
1418
1419   /* After collecting all predecessors into the array nin a new Phi node
1420      with these predecessors is created.  This constructor contains an
1421      optimization: If all predecessors of the Phi node are identical it
1422      returns the only operand instead of a new Phi node.  If the value
1423      passes two different control flow edges without being defined, and
1424      this is the second path treated, a pointer to the node that will be
1425      allocated for the first path (recursion) is returned.  We already
1426      know the address of this node, as it is the next node to be allocated
1427      and will be placed on top of the obstack. (The obstack is a _stack_!) */
1428   res = new_rd_Phi_in (current_ir_graph, block, mode, nin, ins);
1429
1430   /* Now we now the value for "pos" and can enter it in the array with
1431      all known local variables.  Attention: this might be a pointer to
1432      a node, that later will be allocated!!! See new_rd_Phi_in.
1433      If this is called in mature, after some set_value in the same block,
1434      the proper value must not be overwritten:
1435      The call order
1436        get_value    (makes Phi0, put's it into graph_arr)
1437        set_value    (overwrites Phi0 in graph_arr)
1438        mature_immBlock (upgrades Phi0, puts it again into graph_arr, overwriting
1439                      the proper value.)
1440      fails. */
1441   if (!block->attr.block.graph_arr[pos]) {
1442     block->attr.block.graph_arr[pos] = res;
1443   } else {
1444     /*  printf(" value already computed by %s\n",
1445         get_id_str(block->attr.block.graph_arr[pos]->op->name));  */
1446   }
1447
1448   return res;
1449 }
1450
1451 /* This function returns the last definition of a variable.  In case
1452    this variable was last defined in a previous block, Phi nodes are
1453    inserted.  If the part of the firm graph containing the definition
1454    is not yet constructed, a dummy Phi node is returned. */
1455 static ir_node *
1456 get_r_value_internal (ir_node *block, int pos, ir_mode *mode)
1457 {
1458   ir_node *res;
1459   /* There are 4 cases to treat.
1460
1461      1. The block is not mature and we visit it the first time.  We can not
1462         create a proper Phi node, therefore a Phi0, i.e., a Phi without
1463         predecessors is returned.  This node is added to the linked list (field
1464         "link") of the containing block to be completed when this block is
1465         matured. (Completion will add a new Phi and turn the Phi0 into an Id
1466         node.)
1467
1468      2. The value is already known in this block, graph_arr[pos] is set and we
1469         visit the block the first time.  We can return the value without
1470         creating any new nodes.
1471
1472      3. The block is mature and we visit it the first time.  A Phi node needs
1473         to be created (phi_merge).  If the Phi is not needed, as all it's
1474         operands are the same value reaching the block through different
1475         paths, it's optimized away and the value itself is returned.
1476
1477      4. The block is mature, and we visit it the second time.  Now two
1478         subcases are possible:
1479         * The value was computed completely the last time we were here. This
1480           is the case if there is no loop.  We can return the proper value.
1481         * The recursion that visited this node and set the flag did not
1482           return yet.  We are computing a value in a loop and need to
1483           break the recursion without knowing the result yet.
1484       @@@ strange case.  Straight forward we would create a Phi before
1485       starting the computation of it's predecessors.  In this case we will
1486       find a Phi here in any case.  The problem is that this implementation
1487       only creates a Phi after computing the predecessors, so that it is
1488       hard to compute self references of this Phi.  @@@
1489         There is no simple check for the second subcase.  Therefore we check
1490         for a second visit and treat all such cases as the second subcase.
1491         Anyways, the basic situation is the same:  we reached a block
1492         on two paths without finding a definition of the value:  No Phi
1493         nodes are needed on both paths.
1494         We return this information "Two paths, no Phi needed" by a very tricky
1495         implementation that relies on the fact that an obstack is a stack and
1496         will return a node with the same address on different allocations.
1497         Look also at phi_merge and new_rd_phi_in to understand this.
1498     @@@ Unfortunately this does not work, see testprogram
1499     three_cfpred_example.
1500
1501   */
1502
1503   /* case 4 -- already visited. */
1504   if (get_irn_visited(block) == get_irg_visited(current_ir_graph)) return NULL;
1505
1506   /* visited the first time */
1507   set_irn_visited(block, get_irg_visited(current_ir_graph));
1508
1509   /* Get the local valid value */
1510   res = block->attr.block.graph_arr[pos];
1511
1512   /* case 2 -- If the value is actually computed, return it. */
1513   if (res) return res;
1514
1515   if (block->attr.block.matured) { /* case 3 */
1516
1517     /* The Phi has the same amount of ins as the corresponding block. */
1518     int ins = get_irn_arity(block);
1519     ir_node **nin;
1520     NEW_ARR_A (ir_node *, nin, ins);
1521
1522     /* Phi merge collects the predecessors and then creates a node. */
1523     res = phi_merge (block, pos, mode, nin, ins);
1524
1525   } else {  /* case 1 */
1526     /* The block is not mature, we don't know how many in's are needed.  A Phi
1527        with zero predecessors is created.  Such a Phi node is called Phi0
1528        node.  (There is also an obsolete Phi0 opcode.) The Phi0 is then added
1529        to the list of Phi0 nodes in this block to be matured by mature_immBlock
1530        later.
1531        The Phi0 has to remember the pos of it's internal value.  If the real
1532        Phi is computed, pos is used to update the array with the local
1533        values. */
1534
1535     res = new_rd_Phi0 (current_ir_graph, block, mode);
1536     res->attr.phi0_pos = pos;
1537     res->link = block->link;
1538     block->link = res;
1539   }
1540
1541   /* If we get here, the frontend missed a use-before-definition error */
1542   if (!res) {
1543     /* Error Message */
1544     printf("Error: no value set.  Use of undefined variable.  Initializing to zero.\n");
1545     assert (mode->code >= irm_F && mode->code <= irm_P);
1546     res = new_rd_Const (NULL, current_ir_graph, block, mode,
1547                tarval_mode_null[mode->code]);
1548   }
1549
1550   /* The local valid value is available now. */
1551   block->attr.block.graph_arr[pos] = res;
1552
1553   return res;
1554 }
1555
1556 #else /* if 0 */
1557
1558 /**
1559     it starts the recursion.  This causes an Id at the entry of
1560     every block that has no definition of the value! **/
1561
1562 #if USE_EXPLICIT_PHI_IN_STACK
1563 /* Just dummies */
1564 Phi_in_stack * new_Phi_in_stack() {  return NULL; }
1565 void free_Phi_in_stack(Phi_in_stack *s) { }
1566 #endif
1567
1568 static INLINE ir_node *
1569 new_rd_Phi_in (ir_graph *irg, ir_node *block, ir_mode *mode,
1570            ir_node **in, int ins, ir_node *phi0)
1571 {
1572   int i;
1573   ir_node *res, *known;
1574
1575   /* Allocate a new node on the obstack.  The allocation copies the in
1576      array. */
1577   res = new_ir_node (NULL, irg, block, op_Phi, mode, ins, in);
1578   res->attr.phi_backedge = new_backedge_arr(irg->obst, ins);
1579
1580   /* This loop checks whether the Phi has more than one predecessor.
1581      If so, it is a real Phi node and we break the loop.  Else the
1582      Phi node merges the same definition on several paths and therefore
1583      is not needed. Don't consider Bad nodes! */
1584   known = res;
1585   for (i=0;  i < ins;  ++i)
1586   {
1587     assert(in[i]);
1588
1589     in[i] = skip_Id(in[i]);  /* increases the number of freed Phis. */
1590
1591     /* Optimize self referencing Phis:  We can't detect them yet properly, as
1592        they still refer to the Phi0 they will replace.  So replace right now. */
1593     if (phi0 && in[i] == phi0) in[i] = res;
1594
1595     if (in[i]==res || in[i]==known || is_Bad(in[i])) continue;
1596
1597     if (known==res)
1598       known = in[i];
1599     else
1600       break;
1601   }
1602
1603   /* i==ins: there is at most one predecessor, we don't need a phi node. */
1604   if (i == ins) {
1605     if (res != known) {
1606       edges_node_deleted(res, current_ir_graph);
1607       obstack_free (current_ir_graph->obst, res);
1608       if (is_Phi(known)) {
1609         /* If pred is a phi node we want to optimize it: If loops are matured in a bad
1610            order, an enclosing Phi know may get superfluous. */
1611         res = optimize_in_place_2(known);
1612         if (res != known)
1613           exchange(known, res);
1614
1615       }
1616       else
1617         res = known;
1618     } else {
1619       /* A undefined value, e.g., in unreachable code. */
1620       res = new_Bad();
1621     }
1622   } else {
1623     res = optimize_node (res);  /* This is necessary to add the node to the hash table for cse. */
1624     IRN_VRFY_IRG(res, irg);
1625     /* Memory Phis in endless loops must be kept alive.
1626        As we can't distinguish these easily we keep all of them alive. */
1627     if ((res->op == op_Phi) && (mode == mode_M))
1628       add_End_keepalive(irg->end, res);
1629   }
1630
1631   return res;
1632 }
1633
1634 static ir_node *
1635 get_r_value_internal (ir_node *block, int pos, ir_mode *mode);
1636
1637 #if PRECISE_EXC_CONTEXT
1638 static ir_node *
1639 phi_merge (ir_node *block, int pos, ir_mode *mode, ir_node **nin, int ins);
1640
1641 /* Construct a new frag_array for node n.
1642    Copy the content from the current graph_arr of the corresponding block:
1643    this is the current state.
1644    Set ProjM(n) as current memory state.
1645    Further the last entry in frag_arr of current block points to n.  This
1646    constructs a chain block->last_frag_op-> ... first_frag_op of all frag ops in the block.
1647  */
1648 static INLINE ir_node ** new_frag_arr (ir_node *n)
1649 {
1650   ir_node **arr;
1651   int opt;
1652
1653   arr = NEW_ARR_D (ir_node *, current_ir_graph->obst, current_ir_graph->n_loc);
1654   memcpy(arr, current_ir_graph->current_block->attr.block.graph_arr,
1655      sizeof(ir_node *)*current_ir_graph->n_loc);
1656
1657   /* turn off optimization before allocating Proj nodes, as res isn't
1658      finished yet. */
1659   opt = get_opt_optimize(); set_optimize(0);
1660   /* Here we rely on the fact that all frag ops have Memory as first result! */
1661   if (get_irn_op(n) == op_Call)
1662     arr[0] = new_Proj(n, mode_M, pn_Call_M_except);
1663   else {
1664     assert((pn_Quot_M == pn_DivMod_M) &&
1665        (pn_Quot_M == pn_Div_M)    &&
1666        (pn_Quot_M == pn_Mod_M)    &&
1667        (pn_Quot_M == pn_Load_M)   &&
1668        (pn_Quot_M == pn_Store_M)  &&
1669        (pn_Quot_M == pn_Alloc_M)    );
1670     arr[0] = new_Proj(n, mode_M, pn_Alloc_M);
1671   }
1672   set_optimize(opt);
1673
1674   current_ir_graph->current_block->attr.block.graph_arr[current_ir_graph->n_loc-1] = n;
1675   return arr;
1676 }
1677
1678 /**
1679  * returns the frag_arr from a node
1680  */
1681 static INLINE ir_node **
1682 get_frag_arr (ir_node *n) {
1683   switch (get_irn_opcode(n)) {
1684   case iro_Call:
1685     return n->attr.call.exc.frag_arr;
1686   case iro_Alloc:
1687     return n->attr.a.exc.frag_arr;
1688   case iro_Load:
1689     return n->attr.load.exc.frag_arr;
1690   case iro_Store:
1691     return n->attr.store.exc.frag_arr;
1692   default:
1693     return n->attr.except.frag_arr;
1694   }
1695 }
1696
1697 static void
1698 set_frag_value(ir_node **frag_arr, int pos, ir_node *val) {
1699 #if 0
1700   if (!frag_arr[pos]) frag_arr[pos] = val;
1701   if (frag_arr[current_ir_graph->n_loc - 1]) {
1702     ir_node **arr = get_frag_arr(frag_arr[current_ir_graph->n_loc - 1]);
1703     assert(arr != frag_arr && "Endless recursion detected");
1704     set_frag_value(arr, pos, val);
1705   }
1706 #else
1707   int i;
1708
1709   for (i = 0; i < 1000; ++i) {
1710     if (!frag_arr[pos]) {
1711       frag_arr[pos] = val;
1712     }
1713     if (frag_arr[current_ir_graph->n_loc - 1]) {
1714       ir_node **arr = get_frag_arr(frag_arr[current_ir_graph->n_loc - 1]);
1715       frag_arr = arr;
1716     }
1717     else
1718       return;
1719   }
1720   assert(0 && "potential endless recursion");
1721 #endif
1722 }
1723
1724 static ir_node *
1725 get_r_frag_value_internal (ir_node *block, ir_node *cfOp, int pos, ir_mode *mode) {
1726   ir_node *res;
1727   ir_node **frag_arr;
1728
1729   assert(is_fragile_op(cfOp) && (get_irn_op(cfOp) != op_Bad));
1730
1731   frag_arr = get_frag_arr(cfOp);
1732   res = frag_arr[pos];
1733   if (!res) {
1734     if (block->attr.block.graph_arr[pos]) {
1735       /* There was a set_value after the cfOp and no get_value before that
1736          set_value.  We must build a Phi node now. */
1737       if (block->attr.block.matured) {
1738         int ins = get_irn_arity(block);
1739         ir_node **nin;
1740         NEW_ARR_A (ir_node *, nin, ins);
1741         res = phi_merge(block, pos, mode, nin, ins);
1742       } else {
1743         res = new_rd_Phi0 (current_ir_graph, block, mode);
1744         res->attr.phi0_pos = pos;
1745         res->link = block->link;
1746         block->link = res;
1747       }
1748       assert(res);
1749       /* @@@ tested by Flo: set_frag_value(frag_arr, pos, res);
1750          but this should be better: (remove comment if this works) */
1751       /* It's a Phi, we can write this into all graph_arrs with NULL */
1752       set_frag_value(block->attr.block.graph_arr, pos, res);
1753     } else {
1754       res = get_r_value_internal(block, pos, mode);
1755       set_frag_value(block->attr.block.graph_arr, pos, res);
1756     }
1757   }
1758   return res;
1759 }
1760 #endif
1761
1762 /**
1763     computes the predecessors for the real phi node, and then
1764     allocates and returns this node.  The routine called to allocate the
1765     node might optimize it away and return a real value.
1766     This function must be called with an in-array of proper size. **/
1767 static ir_node *
1768 phi_merge (ir_node *block, int pos, ir_mode *mode, ir_node **nin, int ins)
1769 {
1770   ir_node *prevBlock, *prevCfOp, *res, *phi0, *phi0_all;
1771   int i;
1772
1773   /* If this block has no value at pos create a Phi0 and remember it
1774      in graph_arr to break recursions.
1775      Else we may not set graph_arr as there a later value is remembered. */
1776   phi0 = NULL;
1777   if (!block->attr.block.graph_arr[pos]) {
1778     if (block == get_irg_start_block(current_ir_graph)) {
1779       /* Collapsing to Bad tarvals is no good idea.
1780          So we call a user-supplied routine here that deals with this case as
1781          appropriate for the given language. Sorrily the only help we can give
1782          here is the position.
1783
1784          Even if all variables are defined before use, it can happen that
1785          we get to the start block, if a Cond has been replaced by a tuple
1786          (bad, jmp).  In this case we call the function needlessly, eventually
1787          generating an non existent error.
1788          However, this SHOULD NOT HAPPEN, as bad control flow nodes are intercepted
1789          before recuring.
1790       */
1791       if (default_initialize_local_variable)
1792         block->attr.block.graph_arr[pos] = default_initialize_local_variable(current_ir_graph, mode, pos - 1);
1793       else
1794         block->attr.block.graph_arr[pos] = new_Const(mode, tarval_bad);
1795       /* We don't need to care about exception ops in the start block.
1796          There are none by definition. */
1797       return block->attr.block.graph_arr[pos];
1798     } else {
1799       phi0 = new_rd_Phi0(current_ir_graph, block, mode);
1800       block->attr.block.graph_arr[pos] = phi0;
1801 #if PRECISE_EXC_CONTEXT
1802       if (get_opt_precise_exc_context()) {
1803         /* Set graph_arr for fragile ops.  Also here we should break recursion.
1804            We could choose a cyclic path through an cfop.  But the recursion would
1805            break at some point. */
1806         set_frag_value(block->attr.block.graph_arr, pos, phi0);
1807       }
1808 #endif
1809     }
1810   }
1811
1812   /* This loop goes to all predecessor blocks of the block the Phi node
1813      is in and there finds the operands of the Phi node by calling
1814      get_r_value_internal.  */
1815   for (i = 1;  i <= ins;  ++i) {
1816     prevCfOp = skip_Proj(block->in[i]);
1817     assert (prevCfOp);
1818     if (is_Bad(prevCfOp)) {
1819       /* In case a Cond has been optimized we would get right to the start block
1820          with an invalid definition. */
1821       nin[i-1] = new_Bad();
1822       continue;
1823     }
1824     prevBlock = block->in[i]->in[0]; /* go past control flow op to prev block */
1825     assert (prevBlock);
1826     if (!is_Bad(prevBlock)) {
1827 #if PRECISE_EXC_CONTEXT
1828       if (get_opt_precise_exc_context() &&
1829           is_fragile_op(prevCfOp) && (get_irn_op (prevCfOp) != op_Bad)) {
1830         assert(get_r_frag_value_internal (prevBlock, prevCfOp, pos, mode));
1831         nin[i-1] = get_r_frag_value_internal (prevBlock, prevCfOp, pos, mode);
1832       } else
1833 #endif
1834       nin[i-1] = get_r_value_internal (prevBlock, pos, mode);
1835     } else {
1836       nin[i-1] = new_Bad();
1837     }
1838   }
1839
1840   /* We want to pass the Phi0 node to the constructor: this finds additional
1841      optimization possibilities.
1842      The Phi0 node either is allocated in this function, or it comes from
1843      a former call to get_r_value_internal. In this case we may not yet
1844      exchange phi0, as this is done in mature_immBlock. */
1845   if (!phi0) {
1846     phi0_all = block->attr.block.graph_arr[pos];
1847     if (!((get_irn_op(phi0_all) == op_Phi) &&
1848       (get_irn_arity(phi0_all) == 0)   &&
1849       (get_nodes_block(phi0_all) == block)))
1850       phi0_all = NULL;
1851   } else {
1852     phi0_all = phi0;
1853   }
1854
1855   /* After collecting all predecessors into the array nin a new Phi node
1856      with these predecessors is created.  This constructor contains an
1857      optimization: If all predecessors of the Phi node are identical it
1858      returns the only operand instead of a new Phi node.  */
1859   res = new_rd_Phi_in (current_ir_graph, block, mode, nin, ins, phi0_all);
1860
1861   /* In case we allocated a Phi0 node at the beginning of this procedure,
1862      we need to exchange this Phi0 with the real Phi. */
1863   if (phi0) {
1864     exchange(phi0, res);
1865     block->attr.block.graph_arr[pos] = res;
1866     /* Don't set_frag_value as it does not overwrite.  Doesn't matter, is
1867        only an optimization. */
1868   }
1869
1870   return res;
1871 }
1872
1873 /* This function returns the last definition of a variable.  In case
1874    this variable was last defined in a previous block, Phi nodes are
1875    inserted.  If the part of the firm graph containing the definition
1876    is not yet constructed, a dummy Phi node is returned. */
1877 static ir_node *
1878 get_r_value_internal (ir_node *block, int pos, ir_mode *mode)
1879 {
1880   ir_node *res;
1881   /* There are 4 cases to treat.
1882
1883      1. The block is not mature and we visit it the first time.  We can not
1884         create a proper Phi node, therefore a Phi0, i.e., a Phi without
1885         predecessors is returned.  This node is added to the linked list (field
1886         "link") of the containing block to be completed when this block is
1887         matured. (Completion will add a new Phi and turn the Phi0 into an Id
1888         node.)
1889
1890      2. The value is already known in this block, graph_arr[pos] is set and we
1891         visit the block the first time.  We can return the value without
1892         creating any new nodes.
1893
1894      3. The block is mature and we visit it the first time.  A Phi node needs
1895         to be created (phi_merge).  If the Phi is not needed, as all it's
1896         operands are the same value reaching the block through different
1897         paths, it's optimized away and the value itself is returned.
1898
1899      4. The block is mature, and we visit it the second time.  Now two
1900         subcases are possible:
1901         * The value was computed completely the last time we were here. This
1902           is the case if there is no loop.  We can return the proper value.
1903         * The recursion that visited this node and set the flag did not
1904           return yet.  We are computing a value in a loop and need to
1905           break the recursion.  This case only happens if we visited
1906       the same block with phi_merge before, which inserted a Phi0.
1907       So we return the Phi0.
1908   */
1909
1910   /* case 4 -- already visited. */
1911   if (get_irn_visited(block) == get_irg_visited(current_ir_graph)) {
1912     /* As phi_merge allocates a Phi0 this value is always defined. Here
1913      is the critical difference of the two algorithms. */
1914     assert(block->attr.block.graph_arr[pos]);
1915     return block->attr.block.graph_arr[pos];
1916   }
1917
1918   /* visited the first time */
1919   set_irn_visited(block, get_irg_visited(current_ir_graph));
1920
1921   /* Get the local valid value */
1922   res = block->attr.block.graph_arr[pos];
1923
1924   /* case 2 -- If the value is actually computed, return it. */
1925   if (res) { return res; };
1926
1927   if (block->attr.block.matured) { /* case 3 */
1928
1929     /* The Phi has the same amount of ins as the corresponding block. */
1930     int ins = get_irn_arity(block);
1931     ir_node **nin;
1932     NEW_ARR_A (ir_node *, nin, ins);
1933
1934     /* Phi merge collects the predecessors and then creates a node. */
1935     res = phi_merge (block, pos, mode, nin, ins);
1936
1937   } else {  /* case 1 */
1938     /* The block is not mature, we don't know how many in's are needed.  A Phi
1939        with zero predecessors is created.  Such a Phi node is called Phi0
1940        node.  The Phi0 is then added to the list of Phi0 nodes in this block
1941        to be matured by mature_immBlock later.
1942        The Phi0 has to remember the pos of it's internal value.  If the real
1943        Phi is computed, pos is used to update the array with the local
1944        values. */
1945     res = new_rd_Phi0 (current_ir_graph, block, mode);
1946     res->attr.phi0_pos = pos;
1947     res->link = block->link;
1948     block->link = res;
1949   }
1950
1951   /* If we get here, the frontend missed a use-before-definition error */
1952   if (!res) {
1953     /* Error Message */
1954     printf("Error: no value set.  Use of undefined variable.  Initializing to zero.\n");
1955     assert (mode->code >= irm_F && mode->code <= irm_P);
1956     res = new_rd_Const (NULL, current_ir_graph, block, mode,
1957             get_mode_null(mode));
1958   }
1959
1960   /* The local valid value is available now. */
1961   block->attr.block.graph_arr[pos] = res;
1962
1963   return res;
1964 }
1965
1966 #endif /* USE_FAST_PHI_CONSTRUCTION */
1967
1968 /* ************************************************************************** */
1969
1970 /*
1971  * Finalize a Block node, when all control flows are known.
1972  * Acceptable parameters are only Block nodes.
1973  */
1974 void
1975 mature_immBlock (ir_node *block)
1976 {
1977   int ins;
1978   ir_node *n, **nin;
1979   ir_node *next;
1980
1981   assert (get_irn_opcode(block) == iro_Block);
1982   /* @@@ should be commented in
1983      assert (!get_Block_matured(block) && "Block already matured"); */
1984
1985   if (!get_Block_matured(block)) {
1986     ins = ARR_LEN (block->in)-1;
1987     /* Fix block parameters */
1988     block->attr.block.backedge = new_backedge_arr(current_ir_graph->obst, ins);
1989
1990     /* An array for building the Phi nodes. */
1991     NEW_ARR_A (ir_node *, nin, ins);
1992
1993     /* Traverse a chain of Phi nodes attached to this block and mature
1994        these, too. **/
1995     for (n = block->link;  n;  n=next) {
1996       inc_irg_visited(current_ir_graph);
1997       next = n->link;
1998       exchange (n, phi_merge (block, n->attr.phi0_pos, n->mode, nin, ins));
1999     }
2000
2001     block->attr.block.matured = 1;
2002
2003     /* Now, as the block is a finished firm node, we can optimize it.
2004        Since other nodes have been allocated since the block was created
2005        we can not free the node on the obstack.  Therefore we have to call
2006        optimize_in_place.
2007        Unfortunately the optimization does not change a lot, as all allocated
2008        nodes refer to the unoptimized node.
2009        We can call _2, as global cse has no effect on blocks. */
2010     block = optimize_in_place_2(block);
2011     IRN_VRFY_IRG(block, current_ir_graph);
2012   }
2013 }
2014
2015 ir_node *
2016 new_d_Phi (dbg_info* db, int arity, ir_node **in, ir_mode *mode)
2017 {
2018   return new_rd_Phi(db, current_ir_graph, current_ir_graph->current_block,
2019             arity, in, mode);
2020 }
2021
2022 ir_node *
2023 new_d_Const (dbg_info* db, ir_mode *mode, tarval *con)
2024 {
2025   return new_rd_Const(db, current_ir_graph, current_ir_graph->start_block,
2026               mode, con);
2027 }
2028
2029 ir_node *
2030 new_d_Const_long(dbg_info* db, ir_mode *mode, long value)
2031 {
2032   return new_rd_Const_long(db, current_ir_graph, current_ir_graph->start_block, mode, value);
2033 }
2034
2035 ir_node *
2036 new_d_Const_type (dbg_info* db, ir_mode *mode, tarval *con, type *tp)
2037 {
2038   return new_rd_Const_type(db, current_ir_graph, current_ir_graph->start_block,
2039                 mode, con, tp);
2040 }
2041
2042
2043 ir_node *
2044 new_d_Id (dbg_info* db, ir_node *val, ir_mode *mode)
2045 {
2046   return new_rd_Id(db, current_ir_graph, current_ir_graph->current_block,
2047            val, mode);
2048 }
2049
2050 ir_node *
2051 new_d_Proj (dbg_info* db, ir_node *arg, ir_mode *mode, long proj)
2052 {
2053   return new_rd_Proj(db, current_ir_graph, current_ir_graph->current_block,
2054              arg, mode, proj);
2055 }
2056
2057 ir_node *
2058 new_d_defaultProj (dbg_info* db, ir_node *arg, long max_proj)
2059 {
2060   ir_node *res;
2061   assert(arg->op == op_Cond);
2062   arg->attr.c.kind = fragmentary;
2063   arg->attr.c.default_proj = max_proj;
2064   res = new_Proj (arg, mode_X, max_proj);
2065   return res;
2066 }
2067
2068 ir_node *
2069 new_d_Conv (dbg_info* db, ir_node *op, ir_mode *mode)
2070 {
2071   return new_rd_Conv(db, current_ir_graph, current_ir_graph->current_block,
2072              op, mode);
2073 }
2074
2075 ir_node *
2076 new_d_Cast (dbg_info* db, ir_node *op, type *to_tp)
2077 {
2078   return new_rd_Cast(db, current_ir_graph, current_ir_graph->current_block, op, to_tp);
2079 }
2080
2081 ir_node *
2082 new_d_Tuple (dbg_info* db, int arity, ir_node **in)
2083 {
2084   return new_rd_Tuple(db, current_ir_graph, current_ir_graph->current_block,
2085               arity, in);
2086 }
2087
2088 ir_node *
2089 new_d_Add (dbg_info* db, ir_node *op1, ir_node *op2, ir_mode *mode)
2090 {
2091   return new_rd_Add(db, current_ir_graph, current_ir_graph->current_block,
2092             op1, op2, mode);
2093 }
2094
2095 ir_node *
2096 new_d_Sub (dbg_info* db, ir_node *op1, ir_node *op2, ir_mode *mode)
2097 {
2098   return new_rd_Sub(db, current_ir_graph, current_ir_graph->current_block,
2099             op1, op2, mode);
2100 }
2101
2102
2103 ir_node *
2104 new_d_Minus (dbg_info* db, ir_node *op,  ir_mode *mode)
2105 {
2106   return new_rd_Minus(db, current_ir_graph, current_ir_graph->current_block,
2107               op, mode);
2108 }
2109
2110 ir_node *
2111 new_d_Mul (dbg_info* db, ir_node *op1, ir_node *op2, ir_mode *mode)
2112 {
2113   return new_rd_Mul(db, current_ir_graph, current_ir_graph->current_block,
2114             op1, op2, mode);
2115 }
2116
2117 /**
2118  * allocate the frag array
2119  */
2120 static void allocate_frag_arr(ir_node *res, ir_op *op, ir_node ***frag_store) {
2121   if (get_opt_precise_exc_context()) {
2122     if ((current_ir_graph->phase_state == phase_building) &&
2123         (get_irn_op(res) == op) && /* Could be optimized away. */
2124         !*frag_store)    /* Could be a cse where the arr is already set. */ {
2125       *frag_store = new_frag_arr(res);
2126     }
2127   }
2128 }
2129
2130
2131 ir_node *
2132 new_d_Quot (dbg_info* db, ir_node *memop, ir_node *op1, ir_node *op2)
2133 {
2134   ir_node *res;
2135   res = new_rd_Quot (db, current_ir_graph, current_ir_graph->current_block,
2136              memop, op1, op2);
2137   res->attr.except.pin_state = op_pin_state_pinned;
2138 #if PRECISE_EXC_CONTEXT
2139   allocate_frag_arr(res, op_Quot, &res->attr.except.frag_arr);  /* Could be optimized away. */
2140 #endif
2141
2142   return res;
2143 }
2144
2145 ir_node *
2146 new_d_DivMod (dbg_info* db, ir_node *memop, ir_node *op1, ir_node *op2)
2147 {
2148   ir_node *res;
2149   res = new_rd_DivMod (db, current_ir_graph, current_ir_graph->current_block,
2150                memop, op1, op2);
2151   res->attr.except.pin_state = op_pin_state_pinned;
2152 #if PRECISE_EXC_CONTEXT
2153   allocate_frag_arr(res, op_DivMod, &res->attr.except.frag_arr);  /* Could be optimized away. */
2154 #endif
2155
2156   return res;
2157 }
2158
2159 ir_node *
2160 new_d_Div (dbg_info* db, ir_node *memop, ir_node *op1, ir_node *op2)
2161 {
2162   ir_node *res;
2163   res = new_rd_Div (db, current_ir_graph, current_ir_graph->current_block,
2164             memop, op1, op2);
2165   res->attr.except.pin_state = op_pin_state_pinned;
2166 #if PRECISE_EXC_CONTEXT
2167   allocate_frag_arr(res, op_Div, &res->attr.except.frag_arr);  /* Could be optimized away. */
2168 #endif
2169
2170   return res;
2171 }
2172
2173 ir_node *
2174 new_d_Mod (dbg_info* db, ir_node *memop, ir_node *op1, ir_node *op2)
2175 {
2176   ir_node *res;
2177   res = new_rd_Mod (db, current_ir_graph, current_ir_graph->current_block,
2178             memop, op1, op2);
2179   res->attr.except.pin_state = op_pin_state_pinned;
2180 #if PRECISE_EXC_CONTEXT
2181   allocate_frag_arr(res, op_Mod, &res->attr.except.frag_arr);  /* Could be optimized away. */
2182 #endif
2183
2184   return res;
2185 }
2186
2187 ir_node *
2188 new_d_And (dbg_info* db, ir_node *op1, ir_node *op2, ir_mode *mode)
2189 {
2190   return new_rd_And (db, current_ir_graph, current_ir_graph->current_block,
2191             op1, op2, mode);
2192 }
2193
2194 ir_node *
2195 new_d_Or (dbg_info* db, ir_node *op1, ir_node *op2, ir_mode *mode)
2196 {
2197   return new_rd_Or (db, current_ir_graph, current_ir_graph->current_block,
2198            op1, op2, mode);
2199 }
2200
2201 ir_node *
2202 new_d_Eor (dbg_info* db, ir_node *op1, ir_node *op2, ir_mode *mode)
2203 {
2204   return new_rd_Eor (db, current_ir_graph, current_ir_graph->current_block,
2205             op1, op2, mode);
2206 }
2207
2208 ir_node *
2209 new_d_Not (dbg_info* db, ir_node *op, ir_mode *mode)
2210 {
2211   return new_rd_Not (db, current_ir_graph, current_ir_graph->current_block,
2212             op, mode);
2213 }
2214
2215 ir_node *
2216 new_d_Shl (dbg_info* db, ir_node *op, ir_node *k, ir_mode *mode)
2217 {
2218   return new_rd_Shl (db, current_ir_graph, current_ir_graph->current_block,
2219             op, k, mode);
2220 }
2221
2222 ir_node *
2223 new_d_Shr (dbg_info* db, ir_node *op, ir_node *k, ir_mode *mode)
2224 {
2225   return new_rd_Shr (db, current_ir_graph, current_ir_graph->current_block,
2226             op, k, mode);
2227 }
2228
2229 ir_node *
2230 new_d_Shrs (dbg_info* db, ir_node *op, ir_node *k, ir_mode *mode)
2231 {
2232   return new_rd_Shrs (db, current_ir_graph, current_ir_graph->current_block,
2233              op, k, mode);
2234 }
2235
2236 ir_node *
2237 new_d_Rot (dbg_info* db, ir_node *op, ir_node *k, ir_mode *mode)
2238 {
2239   return new_rd_Rot (db, current_ir_graph, current_ir_graph->current_block,
2240              op, k, mode);
2241 }
2242
2243 ir_node *
2244 new_d_Abs (dbg_info* db, ir_node *op, ir_mode *mode)
2245 {
2246   return new_rd_Abs (db, current_ir_graph, current_ir_graph->current_block,
2247             op, mode);
2248 }
2249
2250 ir_node *
2251 new_d_Cmp (dbg_info* db, ir_node *op1, ir_node *op2)
2252 {
2253   return new_rd_Cmp (db, current_ir_graph, current_ir_graph->current_block,
2254             op1, op2);
2255 }
2256
2257 ir_node *
2258 new_d_Jmp (dbg_info* db)
2259 {
2260   return new_rd_Jmp (db, current_ir_graph, current_ir_graph->current_block);
2261 }
2262
2263 ir_node *
2264 new_d_IJmp (dbg_info* db, ir_node *tgt)
2265 {
2266   return new_rd_IJmp (db, current_ir_graph, current_ir_graph->current_block, tgt);
2267 }
2268
2269 ir_node *
2270 new_d_Cond (dbg_info* db, ir_node *c)
2271 {
2272   return new_rd_Cond (db, current_ir_graph, current_ir_graph->current_block, c);
2273 }
2274
2275 ir_node *
2276 new_d_Call (dbg_info* db, ir_node *store, ir_node *callee, int arity, ir_node **in,
2277       type *tp)
2278 {
2279   ir_node *res;
2280   res = new_rd_Call (db, current_ir_graph, current_ir_graph->current_block,
2281              store, callee, arity, in, tp);
2282 #if PRECISE_EXC_CONTEXT
2283   allocate_frag_arr(res, op_Call, &res->attr.call.exc.frag_arr);  /* Could be optimized away. */
2284 #endif
2285
2286   return res;
2287 }
2288
2289 ir_node *
2290 new_d_Return (dbg_info* db, ir_node* store, int arity, ir_node **in)
2291 {
2292   return new_rd_Return (db, current_ir_graph, current_ir_graph->current_block,
2293                store, arity, in);
2294 }
2295
2296 ir_node *
2297 new_d_Raise (dbg_info* db, ir_node *store, ir_node *obj)
2298 {
2299   return new_rd_Raise (db, current_ir_graph, current_ir_graph->current_block,
2300               store, obj);
2301 }
2302
2303 ir_node *
2304 new_d_Load (dbg_info* db, ir_node *store, ir_node *addr, ir_mode *mode)
2305 {
2306   ir_node *res;
2307   res = new_rd_Load (db, current_ir_graph, current_ir_graph->current_block,
2308              store, addr, mode);
2309 #if PRECISE_EXC_CONTEXT
2310   allocate_frag_arr(res, op_Load, &res->attr.load.exc.frag_arr);  /* Could be optimized away. */
2311 #endif
2312
2313   return res;
2314 }
2315
2316 ir_node *
2317 new_d_Store (dbg_info* db, ir_node *store, ir_node *addr, ir_node *val)
2318 {
2319   ir_node *res;
2320   res = new_rd_Store (db, current_ir_graph, current_ir_graph->current_block,
2321               store, addr, val);
2322 #if PRECISE_EXC_CONTEXT
2323   allocate_frag_arr(res, op_Store, &res->attr.store.exc.frag_arr);  /* Could be optimized away. */
2324 #endif
2325
2326   return res;
2327 }
2328
2329 ir_node *
2330 new_d_Alloc (dbg_info* db, ir_node *store, ir_node *size, type *alloc_type,
2331            where_alloc where)
2332 {
2333   ir_node *res;
2334   res = new_rd_Alloc (db, current_ir_graph, current_ir_graph->current_block,
2335               store, size, alloc_type, where);
2336 #if PRECISE_EXC_CONTEXT
2337   allocate_frag_arr(res, op_Alloc, &res->attr.a.exc.frag_arr);  /* Could be optimized away. */
2338 #endif
2339
2340   return res;
2341 }
2342
2343 ir_node *
2344 new_d_Free (dbg_info* db, ir_node *store, ir_node *ptr,
2345     ir_node *size, type *free_type, where_alloc where)
2346 {
2347   return new_rd_Free (db, current_ir_graph, current_ir_graph->current_block,
2348              store, ptr, size, free_type, where);
2349 }
2350
2351 ir_node *
2352 new_d_simpleSel (dbg_info* db, ir_node *store, ir_node *objptr, entity *ent)
2353 /* GL: objptr was called frame before.  Frame was a bad choice for the name
2354    as the operand could as well be a pointer to a dynamic object. */
2355 {
2356   return new_rd_Sel (db, current_ir_graph, current_ir_graph->current_block,
2357             store, objptr, 0, NULL, ent);
2358 }
2359
2360 ir_node *
2361 new_d_Sel (dbg_info* db, ir_node *store, ir_node *objptr, int n_index, ir_node **index, entity *sel)
2362 {
2363   return new_rd_Sel (db, current_ir_graph, current_ir_graph->current_block,
2364             store, objptr, n_index, index, sel);
2365 }
2366
2367 ir_node *
2368 new_d_InstOf (dbg_info *db, ir_node *store, ir_node *objptr, type *ent)
2369 {
2370   return (new_rd_InstOf (db, current_ir_graph, current_ir_graph->current_block,
2371                          store, objptr, ent));
2372 }
2373
2374 ir_node *
2375 new_d_SymConst_type (dbg_info* db, symconst_symbol value, symconst_kind kind, type *tp)
2376 {
2377   return new_rd_SymConst_type (db, current_ir_graph, current_ir_graph->start_block,
2378                          value, kind, tp);
2379 }
2380
2381 ir_node *
2382 new_d_SymConst (dbg_info* db, symconst_symbol value, symconst_kind kind)
2383 {
2384   return new_rd_SymConst (db, current_ir_graph, current_ir_graph->start_block,
2385                          value, kind);
2386 }
2387
2388 ir_node *
2389 new_d_Sync (dbg_info* db, int arity, ir_node** in)
2390 {
2391   return new_rd_Sync (db, current_ir_graph, current_ir_graph->current_block,
2392               arity, in);
2393 }
2394
2395
2396 ir_node *
2397 (new_d_Bad)(void) {
2398   return _new_d_Bad();
2399 }
2400
2401 ir_node *
2402 new_d_Confirm (dbg_info *db, ir_node *val, ir_node *bound, pn_Cmp cmp)
2403 {
2404   return new_rd_Confirm (db, current_ir_graph, current_ir_graph->current_block,
2405              val, bound, cmp);
2406 }
2407
2408 ir_node *
2409 new_d_Unknown (ir_mode *m)
2410 {
2411   return new_rd_Unknown(current_ir_graph, m);
2412 }
2413
2414 ir_node *
2415 new_d_CallBegin (dbg_info *db, ir_node *call)
2416 {
2417   ir_node *res;
2418   res = new_rd_CallBegin (db, current_ir_graph, current_ir_graph->current_block, call);
2419   return res;
2420 }
2421
2422 ir_node *
2423 new_d_EndReg (dbg_info *db)
2424 {
2425   ir_node *res;
2426   res = new_rd_EndReg(db, current_ir_graph, current_ir_graph->current_block);
2427   return res;
2428 }
2429
2430 ir_node *
2431 new_d_EndExcept (dbg_info *db)
2432 {
2433   ir_node *res;
2434   res = new_rd_EndExcept(db, current_ir_graph, current_ir_graph->current_block);
2435   return res;
2436 }
2437
2438 ir_node *
2439 new_d_Break (dbg_info *db)
2440 {
2441   return new_rd_Break (db, current_ir_graph, current_ir_graph->current_block);
2442 }
2443
2444 ir_node *
2445 new_d_Filter (dbg_info *db, ir_node *arg, ir_mode *mode, long proj)
2446 {
2447   return new_rd_Filter (db, current_ir_graph, current_ir_graph->current_block,
2448             arg, mode, proj);
2449 }
2450
2451 ir_node *
2452 (new_d_NoMem)(void)
2453 {
2454   return _new_d_NoMem();
2455 }
2456
2457 ir_node *
2458 new_d_Mux (dbg_info *db, ir_node *sel, ir_node *ir_false,
2459     ir_node *ir_true, ir_mode *mode) {
2460   return new_rd_Mux (db, current_ir_graph, current_ir_graph->current_block,
2461       sel, ir_false, ir_true, mode);
2462 }
2463
2464 /* ********************************************************************* */
2465 /* Comfortable interface with automatic Phi node construction.           */
2466 /* (Uses also constructors of ?? interface, except new_Block.            */
2467 /* ********************************************************************* */
2468
2469 /*  Block construction */
2470 /* immature Block without predecessors */
2471 ir_node *new_d_immBlock (dbg_info* db) {
2472   ir_node *res;
2473
2474   assert(get_irg_phase_state (current_ir_graph) == phase_building);
2475   /* creates a new dynamic in-array as length of in is -1 */
2476   res = new_ir_node (db, current_ir_graph, NULL, op_Block, mode_BB, -1, NULL);
2477   current_ir_graph->current_block = res;
2478   res->attr.block.matured     = 0;
2479   res->attr.block.dead        = 0;
2480   /* res->attr.block.exc = exc_normal; */
2481   /* res->attr.block.handler_entry = 0; */
2482   res->attr.block.irg         = current_ir_graph;
2483   res->attr.block.backedge    = NULL;
2484   res->attr.block.in_cg       = NULL;
2485   res->attr.block.cg_backedge = NULL;
2486   set_Block_block_visited(res, 0);
2487
2488   /* Create and initialize array for Phi-node construction. */
2489   res->attr.block.graph_arr = NEW_ARR_D (ir_node *, current_ir_graph->obst,
2490                                          current_ir_graph->n_loc);
2491   memset(res->attr.block.graph_arr, 0, sizeof(ir_node *)*current_ir_graph->n_loc);
2492
2493   /* Immature block may not be optimized! */
2494   IRN_VRFY_IRG(res, current_ir_graph);
2495
2496   return res;
2497 }
2498
2499 ir_node *
2500 new_immBlock (void) {
2501   return new_d_immBlock(NULL);
2502 }
2503
2504 /* add an edge to a jmp/control flow node */
2505 void
2506 add_immBlock_pred (ir_node *block, ir_node *jmp)
2507 {
2508   if (block->attr.block.matured) {
2509     assert(0 && "Error: Block already matured!\n");
2510   }
2511   else {
2512     assert(jmp != NULL);
2513     ARR_APP1(ir_node *, block->in, jmp);
2514   }
2515 }
2516
2517 /* changing the current block */
2518 void
2519 set_cur_block (ir_node *target) {
2520   current_ir_graph->current_block = target;
2521 }
2522
2523 /* ************************ */
2524 /* parameter administration */
2525
2526 /* get a value from the parameter array from the current block by its index */
2527 ir_node *
2528 get_d_value (dbg_info* db, int pos, ir_mode *mode)
2529 {
2530   assert(get_irg_phase_state (current_ir_graph) == phase_building);
2531   inc_irg_visited(current_ir_graph);
2532
2533   return get_r_value_internal (current_ir_graph->current_block, pos + 1, mode);
2534 }
2535 /* get a value from the parameter array from the current block by its index */
2536 ir_node *
2537 get_value (int pos, ir_mode *mode)
2538 {
2539   return get_d_value(NULL, pos, mode);
2540 }
2541
2542 /* set a value at position pos in the parameter array from the current block */
2543 void
2544 set_value (int pos, ir_node *value)
2545 {
2546   assert(get_irg_phase_state (current_ir_graph) == phase_building);
2547   assert(pos+1 < current_ir_graph->n_loc);
2548   current_ir_graph->current_block->attr.block.graph_arr[pos + 1] = value;
2549 }
2550
2551 /* get the current store */
2552 ir_node *
2553 get_store (void)
2554 {
2555   assert(get_irg_phase_state (current_ir_graph) == phase_building);
2556   /* GL: one could call get_value instead */
2557   inc_irg_visited(current_ir_graph);
2558   return get_r_value_internal (current_ir_graph->current_block, 0, mode_M);
2559 }
2560
2561 /* set the current store */
2562 void
2563 set_store (ir_node *store)
2564 {
2565   /* GL: one could call set_value instead */
2566   assert(get_irg_phase_state (current_ir_graph) == phase_building);
2567   current_ir_graph->current_block->attr.block.graph_arr[0] = store;
2568 }
2569
2570 void
2571 keep_alive (ir_node *ka) {
2572   add_End_keepalive(current_ir_graph->end, ka);
2573 }
2574
2575 /* --- Useful access routines --- */
2576 /* Returns the current block of the current graph.  To set the current
2577    block use set_cur_block. */
2578 ir_node *get_cur_block(void) {
2579   return get_irg_current_block(current_ir_graph);
2580 }
2581
2582 /* Returns the frame type of the current graph */
2583 type *get_cur_frame_type(void) {
2584   return get_irg_frame_type(current_ir_graph);
2585 }
2586
2587
2588 /* ********************************************************************* */
2589 /* initialize */
2590
2591 /* call once for each run of the library */
2592 void
2593 init_cons(uninitialized_local_variable_func_t *func)
2594 {
2595   default_initialize_local_variable = func;
2596 }
2597
2598 /* call for each graph */
2599 void
2600 irg_finalize_cons (ir_graph *irg) {
2601   irg->phase_state = phase_high;
2602 }
2603
2604 void
2605 irp_finalize_cons (void) {
2606   int i, n_irgs = get_irp_n_irgs();
2607   for (i = 0; i < n_irgs; i++) {
2608     irg_finalize_cons(get_irp_irg(i));
2609   }
2610   irp->phase_state = phase_high;\
2611 }
2612
2613
2614
2615
2616 ir_node *new_Block(int arity, ir_node **in) {
2617   return new_d_Block(NULL, arity, in);
2618 }
2619 ir_node *new_Start  (void) {
2620   return new_d_Start(NULL);
2621 }
2622 ir_node *new_End    (void) {
2623   return new_d_End(NULL);
2624 }
2625 ir_node *new_Jmp    (void) {
2626   return new_d_Jmp(NULL);
2627 }
2628 ir_node *new_IJmp   (ir_node *tgt) {
2629   return new_d_IJmp(NULL, tgt);
2630 }
2631 ir_node *new_Cond   (ir_node *c) {
2632   return new_d_Cond(NULL, c);
2633 }
2634 ir_node *new_Return (ir_node *store, int arity, ir_node *in[]) {
2635   return new_d_Return(NULL, store, arity, in);
2636 }
2637 ir_node *new_Raise  (ir_node *store, ir_node *obj) {
2638   return new_d_Raise(NULL, store, obj);
2639 }
2640 ir_node *new_Const  (ir_mode *mode, tarval *con) {
2641   return new_d_Const(NULL, mode, con);
2642 }
2643
2644 ir_node *new_Const_long(ir_mode *mode, long value)
2645 {
2646     return new_d_Const_long(NULL, mode, value);
2647 }
2648
2649 ir_node *new_Const_type(tarval *con, type *tp) {
2650   return new_d_Const_type(NULL, get_type_mode(tp), con, tp);
2651 }
2652
2653 ir_node *new_SymConst (symconst_symbol value, symconst_kind kind) {
2654   return new_d_SymConst(NULL, value, kind);
2655 }
2656 ir_node *new_simpleSel(ir_node *store, ir_node *objptr, entity *ent) {
2657   return new_d_simpleSel(NULL, store, objptr, ent);
2658 }
2659 ir_node *new_Sel    (ir_node *store, ir_node *objptr, int arity, ir_node **in,
2660                      entity *ent) {
2661   return new_d_Sel(NULL, store, objptr, arity, in, ent);
2662 }
2663 ir_node *new_InstOf (ir_node *store, ir_node *objptr, type *ent) {
2664   return new_d_InstOf (NULL, store, objptr, ent);
2665 }
2666 ir_node *new_Call   (ir_node *store, ir_node *callee, int arity, ir_node **in,
2667              type *tp) {
2668   return new_d_Call(NULL, store, callee, arity, in, tp);
2669 }
2670 ir_node *new_Add    (ir_node *op1, ir_node *op2, ir_mode *mode) {
2671   return new_d_Add(NULL, op1, op2, mode);
2672 }
2673 ir_node *new_Sub    (ir_node *op1, ir_node *op2, ir_mode *mode) {
2674   return new_d_Sub(NULL, op1, op2, mode);
2675 }
2676 ir_node *new_Minus  (ir_node *op,  ir_mode *mode) {
2677   return new_d_Minus(NULL, op, mode);
2678 }
2679 ir_node *new_Mul    (ir_node *op1, ir_node *op2, ir_mode *mode) {
2680   return new_d_Mul(NULL, op1, op2, mode);
2681 }
2682 ir_node *new_Quot   (ir_node *memop, ir_node *op1, ir_node *op2) {
2683   return new_d_Quot(NULL, memop, op1, op2);
2684 }
2685 ir_node *new_DivMod (ir_node *memop, ir_node *op1, ir_node *op2) {
2686   return new_d_DivMod(NULL, memop, op1, op2);
2687 }
2688 ir_node *new_Div    (ir_node *memop, ir_node *op1, ir_node *op2) {
2689   return new_d_Div(NULL, memop, op1, op2);
2690 }
2691 ir_node *new_Mod    (ir_node *memop, ir_node *op1, ir_node *op2) {
2692   return new_d_Mod(NULL, memop, op1, op2);
2693 }
2694 ir_node *new_Abs    (ir_node *op, ir_mode *mode) {
2695   return new_d_Abs(NULL, op, mode);
2696 }
2697 ir_node *new_And    (ir_node *op1, ir_node *op2, ir_mode *mode) {
2698   return new_d_And(NULL, op1, op2, mode);
2699 }
2700 ir_node *new_Or     (ir_node *op1, ir_node *op2, ir_mode *mode) {
2701   return new_d_Or(NULL, op1, op2, mode);
2702 }
2703 ir_node *new_Eor    (ir_node *op1, ir_node *op2, ir_mode *mode) {
2704   return new_d_Eor(NULL, op1, op2, mode);
2705 }
2706 ir_node *new_Not    (ir_node *op,                ir_mode *mode) {
2707   return new_d_Not(NULL, op, mode);
2708 }
2709 ir_node *new_Shl    (ir_node *op,  ir_node *k,   ir_mode *mode) {
2710   return new_d_Shl(NULL, op, k, mode);
2711 }
2712 ir_node *new_Shr    (ir_node *op,  ir_node *k,   ir_mode *mode) {
2713   return new_d_Shr(NULL, op, k, mode);
2714 }
2715 ir_node *new_Shrs   (ir_node *op,  ir_node *k,   ir_mode *mode) {
2716   return new_d_Shrs(NULL, op, k, mode);
2717 }
2718 ir_node *new_Rot    (ir_node *op,  ir_node *k,   ir_mode *mode) {
2719   return new_d_Rot(NULL, op, k, mode);
2720 }
2721 ir_node *new_Cmp    (ir_node *op1, ir_node *op2) {
2722   return new_d_Cmp(NULL, op1, op2);
2723 }
2724 ir_node *new_Conv   (ir_node *op, ir_mode *mode) {
2725   return new_d_Conv(NULL, op, mode);
2726 }
2727 ir_node *new_Cast   (ir_node *op, type *to_tp) {
2728   return new_d_Cast(NULL, op, to_tp);
2729 }
2730 ir_node *new_Phi    (int arity, ir_node **in, ir_mode *mode) {
2731   return new_d_Phi(NULL, arity, in, mode);
2732 }
2733 ir_node *new_Load   (ir_node *store, ir_node *addr, ir_mode *mode) {
2734   return new_d_Load(NULL, store, addr, mode);
2735 }
2736 ir_node *new_Store  (ir_node *store, ir_node *addr, ir_node *val) {
2737   return new_d_Store(NULL, store, addr, val);
2738 }
2739 ir_node *new_Alloc  (ir_node *store, ir_node *size, type *alloc_type,
2740                      where_alloc where) {
2741   return new_d_Alloc(NULL, store, size, alloc_type, where);
2742 }
2743 ir_node *new_Free   (ir_node *store, ir_node *ptr, ir_node *size,
2744              type *free_type, where_alloc where) {
2745   return new_d_Free(NULL, store, ptr, size, free_type, where);
2746 }
2747 ir_node *new_Sync   (int arity, ir_node **in) {
2748   return new_d_Sync(NULL, arity, in);
2749 }
2750 ir_node *new_Proj   (ir_node *arg, ir_mode *mode, long proj) {
2751   return new_d_Proj(NULL, arg, mode, proj);
2752 }
2753 ir_node *new_defaultProj (ir_node *arg, long max_proj) {
2754   return new_d_defaultProj(NULL, arg, max_proj);
2755 }
2756 ir_node *new_Tuple  (int arity, ir_node **in) {
2757   return new_d_Tuple(NULL, arity, in);
2758 }
2759 ir_node *new_Id     (ir_node *val, ir_mode *mode) {
2760   return new_d_Id(NULL, val, mode);
2761 }
2762 ir_node *new_Bad    (void) {
2763   return new_d_Bad();
2764 }
2765 ir_node *new_Confirm (ir_node *val, ir_node *bound, pn_Cmp cmp) {
2766   return new_d_Confirm (NULL, val, bound, cmp);
2767 }
2768 ir_node *new_Unknown(ir_mode *m) {
2769   return new_d_Unknown(m);
2770 }
2771 ir_node *new_CallBegin (ir_node *callee) {
2772   return new_d_CallBegin(NULL, callee);
2773 }
2774 ir_node *new_EndReg (void) {
2775   return new_d_EndReg(NULL);
2776 }
2777 ir_node *new_EndExcept (void) {
2778   return new_d_EndExcept(NULL);
2779 }
2780 ir_node *new_Break  (void) {
2781   return new_d_Break(NULL);
2782 }
2783 ir_node *new_Filter (ir_node *arg, ir_mode *mode, long proj) {
2784   return new_d_Filter(NULL, arg, mode, proj);
2785 }
2786 ir_node *new_NoMem  (void) {
2787   return new_d_NoMem();
2788 }
2789 ir_node *new_Mux (ir_node *sel, ir_node *ir_false, ir_node *ir_true, ir_mode *mode) {
2790   return new_d_Mux(NULL, sel, ir_false, ir_true, mode);
2791 }