Added option to enable/disable scalar replacement
[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 initialization variable
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 ir_node *
817 new_rd_Unknown (ir_graph *irg, ir_mode *m)
818 {
819   return new_ir_node(NULL, irg, irg->start_block, op_Unknown, m, 0, NULL);
820 }
821
822 ir_node *
823 new_rd_CallBegin (dbg_info *db, ir_graph *irg, ir_node *block, ir_node *call)
824 {
825   ir_node *in[1];
826   ir_node *res;
827
828   in[0] = get_Call_ptr(call);
829   res = new_ir_node(db, irg, block, op_CallBegin, mode_T, 1, in);
830   /* res->attr.callbegin.irg = irg; */
831   res->attr.callbegin.call = call;
832   res = optimize_node(res);
833   IRN_VRFY_IRG(res, irg);
834   return res;
835 }
836
837 ir_node *
838 new_rd_EndReg (dbg_info *db, ir_graph *irg, ir_node *block)
839 {
840   ir_node *res;
841
842   res = new_ir_node(db, irg, block, op_EndReg, mode_T, -1, NULL);
843   irg->end_reg = res;
844   IRN_VRFY_IRG(res, irg);
845   return res;
846 }
847
848 ir_node *
849 new_rd_EndExcept (dbg_info *db, ir_graph *irg, ir_node *block)
850 {
851   ir_node *res;
852
853   res = new_ir_node(db, irg, block, op_EndExcept, mode_T, -1, NULL);
854   irg->end_except = res;
855   IRN_VRFY_IRG (res, irg);
856   return res;
857 }
858
859 ir_node *
860 new_rd_Break (dbg_info *db, ir_graph *irg, ir_node *block)
861 {
862   ir_node *res;
863
864   res = new_ir_node(db, irg, block, op_Break, mode_X, 0, NULL);
865   res = optimize_node(res);
866   IRN_VRFY_IRG(res, irg);
867   return res;
868 }
869
870 ir_node *
871 new_rd_Filter (dbg_info *db, ir_graph *irg, ir_node *block, ir_node *arg, ir_mode *mode,
872            long proj)
873 {
874   ir_node *res;
875
876   res = new_ir_node(db, irg, block, op_Filter, mode, 1, &arg);
877   res->attr.filter.proj = proj;
878   res->attr.filter.in_cg = NULL;
879   res->attr.filter.backedge = NULL;
880
881   assert(res);
882   assert(get_Proj_pred(res));
883   assert(get_nodes_block(get_Proj_pred(res)));
884
885   res = optimize_node(res);
886   IRN_VRFY_IRG(res, irg);
887   return res;
888 }
889
890 ir_node *
891 new_rd_NoMem (ir_graph *irg) {
892   return irg->no_mem;
893 }
894
895 ir_node *
896 new_rd_Mux  (dbg_info *db, ir_graph *irg, ir_node *block,
897     ir_node *sel, ir_node *ir_false, ir_node *ir_true, ir_mode *mode)
898 {
899   ir_node *in[3];
900   ir_node *res;
901
902   in[0] = sel;
903   in[1] = ir_false;
904   in[2] = ir_true;
905
906   res = new_ir_node(db, irg, block, op_Mux, mode, 3, in);
907   assert(res);
908
909   res = optimize_node(res);
910   IRN_VRFY_IRG(res, irg);
911   return res;
912 }
913
914
915 ir_node *new_r_Block  (ir_graph *irg,  int arity, ir_node **in) {
916   return new_rd_Block(NULL, irg, arity, in);
917 }
918 ir_node *new_r_Start  (ir_graph *irg, ir_node *block) {
919   return new_rd_Start(NULL, irg, block);
920 }
921 ir_node *new_r_End    (ir_graph *irg, ir_node *block) {
922   return new_rd_End(NULL, irg, block);
923 }
924 ir_node *new_r_Jmp    (ir_graph *irg, ir_node *block) {
925   return new_rd_Jmp(NULL, irg, block);
926 }
927 ir_node *new_r_IJmp   (ir_graph *irg, ir_node *block, ir_node *tgt) {
928   return new_rd_IJmp(NULL, irg, block, tgt);
929 }
930 ir_node *new_r_Cond   (ir_graph *irg, ir_node *block, ir_node *c) {
931   return new_rd_Cond(NULL, irg, block, c);
932 }
933 ir_node *new_r_Return (ir_graph *irg, ir_node *block,
934                ir_node *store, int arity, ir_node **in) {
935   return new_rd_Return(NULL, irg, block, store, arity, in);
936 }
937 ir_node *new_r_Raise  (ir_graph *irg, ir_node *block,
938                ir_node *store, ir_node *obj) {
939   return new_rd_Raise(NULL, irg, block, store, obj);
940 }
941 ir_node *new_r_Const  (ir_graph *irg, ir_node *block,
942                ir_mode *mode, tarval *con) {
943   return new_rd_Const(NULL, irg, block, mode, con);
944 }
945
946 ir_node *new_r_Const_long(ir_graph *irg, ir_node *block,
947                ir_mode *mode, long value) {
948   return new_rd_Const_long(NULL, irg, block, mode, value);
949 }
950
951 ir_node *new_r_Const_type(ir_graph *irg, ir_node *block,
952                ir_mode *mode, tarval *con, type *tp) {
953   return new_rd_Const_type(NULL, irg, block, mode, con, tp);
954 }
955
956 ir_node *new_r_SymConst (ir_graph *irg, ir_node *block,
957                        symconst_symbol value, symconst_kind symkind) {
958   return new_rd_SymConst(NULL, irg, block, value, symkind);
959 }
960 ir_node *new_r_Sel    (ir_graph *irg, ir_node *block, ir_node *store,
961                   ir_node *objptr, int n_index, ir_node **index,
962                   entity *ent) {
963   return new_rd_Sel(NULL, irg, block, store, objptr, n_index, index, ent);
964 }
965 ir_node *new_r_InstOf (ir_graph *irg, ir_node *block, ir_node *store, ir_node *objptr,
966                   type *ent) {
967   return (new_rd_InstOf (NULL, irg, block, store, objptr, ent));
968 }
969 ir_node *new_r_Call   (ir_graph *irg, ir_node *block, ir_node *store,
970                   ir_node *callee, int arity, ir_node **in,
971                   type *tp) {
972   return new_rd_Call(NULL, irg, block, store, callee, arity, in, tp);
973 }
974 ir_node *new_r_Add    (ir_graph *irg, ir_node *block,
975                   ir_node *op1, ir_node *op2, ir_mode *mode) {
976   return new_rd_Add(NULL, irg, block, op1, op2, mode);
977 }
978 ir_node *new_r_Sub    (ir_graph *irg, ir_node *block,
979                   ir_node *op1, ir_node *op2, ir_mode *mode) {
980   return new_rd_Sub(NULL, irg, block, op1, op2, mode);
981 }
982 ir_node *new_r_Minus  (ir_graph *irg, ir_node *block,
983                   ir_node *op,  ir_mode *mode) {
984   return new_rd_Minus(NULL, irg, block,  op, mode);
985 }
986 ir_node *new_r_Mul    (ir_graph *irg, ir_node *block,
987                   ir_node *op1, ir_node *op2, ir_mode *mode) {
988   return new_rd_Mul(NULL, irg, block, op1, op2, mode);
989 }
990 ir_node *new_r_Quot   (ir_graph *irg, ir_node *block,
991                   ir_node *memop, ir_node *op1, ir_node *op2) {
992   return new_rd_Quot(NULL, irg, block, memop, op1, op2);
993 }
994 ir_node *new_r_DivMod (ir_graph *irg, ir_node *block,
995                   ir_node *memop, ir_node *op1, ir_node *op2) {
996   return new_rd_DivMod(NULL, irg, block, memop, op1, op2);
997 }
998 ir_node *new_r_Div    (ir_graph *irg, ir_node *block,
999                   ir_node *memop, ir_node *op1, ir_node *op2) {
1000   return new_rd_Div(NULL, irg, block, memop, op1, op2);
1001 }
1002 ir_node *new_r_Mod    (ir_graph *irg, ir_node *block,
1003                   ir_node *memop, ir_node *op1, ir_node *op2) {
1004   return new_rd_Mod(NULL, irg, block, memop, op1, op2);
1005 }
1006 ir_node *new_r_Abs    (ir_graph *irg, ir_node *block,
1007                   ir_node *op, ir_mode *mode) {
1008   return new_rd_Abs(NULL, irg, block, op, mode);
1009 }
1010 ir_node *new_r_And    (ir_graph *irg, ir_node *block,
1011                   ir_node *op1, ir_node *op2, ir_mode *mode) {
1012   return new_rd_And(NULL, irg, block,  op1, op2, mode);
1013 }
1014 ir_node *new_r_Or     (ir_graph *irg, ir_node *block,
1015                   ir_node *op1, ir_node *op2, ir_mode *mode) {
1016   return new_rd_Or(NULL, irg, block,  op1, op2, mode);
1017 }
1018 ir_node *new_r_Eor    (ir_graph *irg, ir_node *block,
1019                   ir_node *op1, ir_node *op2, ir_mode *mode) {
1020   return new_rd_Eor(NULL, irg, block,  op1, op2, mode);
1021 }
1022 ir_node *new_r_Not    (ir_graph *irg, ir_node *block,
1023                ir_node *op, ir_mode *mode) {
1024   return new_rd_Not(NULL, irg, block, op, mode);
1025 }
1026 ir_node *new_r_Cmp    (ir_graph *irg, ir_node *block,
1027                ir_node *op1, ir_node *op2) {
1028   return new_rd_Cmp(NULL, irg, block, op1, op2);
1029 }
1030 ir_node *new_r_Shl    (ir_graph *irg, ir_node *block,
1031                ir_node *op, ir_node *k, ir_mode *mode) {
1032   return new_rd_Shl(NULL, irg, block, op, k, mode);
1033 }
1034 ir_node *new_r_Shr    (ir_graph *irg, ir_node *block,
1035                ir_node *op, ir_node *k, ir_mode *mode) {
1036   return new_rd_Shr(NULL, irg, block, op, k, mode);
1037 }
1038 ir_node *new_r_Shrs   (ir_graph *irg, ir_node *block,
1039                ir_node *op, ir_node *k, ir_mode *mode) {
1040   return new_rd_Shrs(NULL, irg, block, op, k, mode);
1041 }
1042 ir_node *new_r_Rot    (ir_graph *irg, ir_node *block,
1043                ir_node *op, ir_node *k, ir_mode *mode) {
1044   return new_rd_Rot(NULL, irg, block, op, k, mode);
1045 }
1046 ir_node *new_r_Conv   (ir_graph *irg, ir_node *block,
1047                ir_node *op, ir_mode *mode) {
1048   return new_rd_Conv(NULL, irg, block, op, mode);
1049 }
1050 ir_node *new_r_Cast   (ir_graph *irg, ir_node *block, ir_node *op, type *to_tp) {
1051   return new_rd_Cast(NULL, irg, block, op, to_tp);
1052 }
1053 ir_node *new_r_Phi    (ir_graph *irg, ir_node *block, int arity,
1054                ir_node **in, ir_mode *mode) {
1055   return new_rd_Phi(NULL, irg, block, arity, in, mode);
1056 }
1057 ir_node *new_r_Load   (ir_graph *irg, ir_node *block,
1058                ir_node *store, ir_node *adr, ir_mode *mode) {
1059   return new_rd_Load(NULL, irg, block, store, adr, mode);
1060 }
1061 ir_node *new_r_Store  (ir_graph *irg, ir_node *block,
1062                ir_node *store, ir_node *adr, ir_node *val) {
1063   return new_rd_Store(NULL, irg, block, store, adr, val);
1064 }
1065 ir_node *new_r_Alloc  (ir_graph *irg, ir_node *block, ir_node *store,
1066                ir_node *size, type *alloc_type, where_alloc where) {
1067   return new_rd_Alloc(NULL, irg, block, store, size, alloc_type, where);
1068 }
1069 ir_node *new_r_Free   (ir_graph *irg, ir_node *block, ir_node *store,
1070                ir_node *ptr, ir_node *size, type *free_type, where_alloc where) {
1071   return new_rd_Free(NULL, irg, block, store, ptr, size, free_type, where);
1072 }
1073 ir_node *new_r_Sync   (ir_graph *irg, ir_node *block, int arity, ir_node **in) {
1074   return new_rd_Sync(NULL, irg, block, arity, in);
1075 }
1076 ir_node *new_r_Proj   (ir_graph *irg, ir_node *block, ir_node *arg,
1077                ir_mode *mode, long proj) {
1078   return new_rd_Proj(NULL, irg, block, arg, mode, proj);
1079 }
1080 ir_node *new_r_defaultProj (ir_graph *irg, ir_node *block, ir_node *arg,
1081                 long max_proj) {
1082   return new_rd_defaultProj(NULL, irg, block, arg, max_proj);
1083 }
1084 ir_node *new_r_Tuple  (ir_graph *irg, ir_node *block,
1085                int arity, ir_node **in) {
1086   return new_rd_Tuple(NULL, irg, block, arity, in );
1087 }
1088 ir_node *new_r_Id     (ir_graph *irg, ir_node *block,
1089                ir_node *val, ir_mode *mode) {
1090   return new_rd_Id(NULL, irg, block, val, mode);
1091 }
1092 ir_node *new_r_Bad    (ir_graph *irg) {
1093   return new_rd_Bad(irg);
1094 }
1095 ir_node *new_r_Confirm (ir_graph *irg, ir_node *block, ir_node *val, ir_node *bound, pn_Cmp cmp) {
1096   return new_rd_Confirm (NULL, irg, block, val, bound, cmp);
1097 }
1098 ir_node *new_r_Unknown (ir_graph *irg, ir_mode *m) {
1099   return new_rd_Unknown(irg, m);
1100 }
1101 ir_node *new_r_CallBegin (ir_graph *irg, ir_node *block, ir_node *callee) {
1102   return new_rd_CallBegin(NULL, irg, block, callee);
1103 }
1104 ir_node *new_r_EndReg (ir_graph *irg, ir_node *block) {
1105   return new_rd_EndReg(NULL, irg, block);
1106 }
1107 ir_node *new_r_EndExcept (ir_graph *irg, ir_node *block) {
1108   return new_rd_EndExcept(NULL, irg, block);
1109 }
1110 ir_node *new_r_Break  (ir_graph *irg, ir_node *block) {
1111   return new_rd_Break(NULL, irg, block);
1112 }
1113 ir_node *new_r_Filter (ir_graph *irg, ir_node *block, ir_node *arg,
1114                ir_mode *mode, long proj) {
1115   return new_rd_Filter(NULL, irg, block, arg, mode, proj);
1116 }
1117 ir_node *new_r_NoMem  (ir_graph *irg) {
1118   return new_rd_NoMem(irg);
1119 }
1120 ir_node *new_r_Mux (ir_graph *irg, ir_node *block,
1121     ir_node *sel, ir_node *ir_false, ir_node *ir_true, ir_mode *mode) {
1122   return new_rd_Mux(NULL, irg, block, sel, ir_false, ir_true, mode);
1123 }
1124
1125
1126 /** ********************/
1127 /** public interfaces  */
1128 /** construction tools */
1129
1130 /**
1131  *
1132  *   - create a new Start node in the current block
1133  *
1134  *   @return s - pointer to the created Start node
1135  *
1136  *
1137  */
1138 ir_node *
1139 new_d_Start (dbg_info* db)
1140 {
1141   ir_node *res;
1142
1143   res = new_ir_node (db, current_ir_graph, current_ir_graph->current_block,
1144              op_Start, mode_T, 0, NULL);
1145   /* res->attr.start.irg = current_ir_graph; */
1146
1147   res = optimize_node(res);
1148   IRN_VRFY_IRG(res, current_ir_graph);
1149   return res;
1150 }
1151
1152 ir_node *
1153 new_d_End (dbg_info* db)
1154 {
1155   ir_node *res;
1156   res = new_ir_node(db, current_ir_graph,  current_ir_graph->current_block,
1157              op_End, mode_X, -1, NULL);
1158   res = optimize_node(res);
1159   IRN_VRFY_IRG(res, current_ir_graph);
1160
1161   return res;
1162 }
1163
1164 /* Constructs a Block with a fixed number of predecessors.
1165    Does set current_block.  Can be used with automatic Phi
1166    node construction. */
1167 ir_node *
1168 new_d_Block (dbg_info* db, int arity, ir_node **in)
1169 {
1170   ir_node *res;
1171   int i;
1172   bool has_unknown = false;
1173
1174   res = new_rd_Block(db, current_ir_graph, arity, in);
1175
1176   /* Create and initialize array for Phi-node construction. */
1177   if (get_irg_phase_state(current_ir_graph) == phase_building) {
1178     res->attr.block.graph_arr = NEW_ARR_D(ir_node *, current_ir_graph->obst,
1179                       current_ir_graph->n_loc);
1180     memset(res->attr.block.graph_arr, 0, sizeof(ir_node *)*current_ir_graph->n_loc);
1181   }
1182
1183   for (i = arity-1; i >= 0; i--)
1184     if (get_irn_op(in[i]) == op_Unknown) {
1185       has_unknown = true;
1186       break;
1187     }
1188
1189   if (!has_unknown) res = optimize_node(res);
1190   current_ir_graph->current_block = res;
1191
1192   IRN_VRFY_IRG(res, current_ir_graph);
1193
1194   return res;
1195 }
1196
1197 /* ***********************************************************************/
1198 /* Methods necessary for automatic Phi node creation                     */
1199 /*
1200   ir_node *phi_merge            (ir_node *block, int pos, ir_mode *mode, ir_node **nin, int ins)
1201   ir_node *get_r_value_internal (ir_node *block, int pos, ir_mode *mode);
1202   ir_node *new_rd_Phi0           (ir_graph *irg, ir_node *block, ir_mode *mode)
1203   ir_node *new_rd_Phi_in         (ir_graph *irg, ir_node *block, ir_mode *mode,  ir_node **in, int ins)
1204
1205   Call Graph:   ( A ---> B == A "calls" B)
1206
1207        get_value         mature_immBlock
1208           |                   |
1209           |                   |
1210           |                   |
1211           |          ---> phi_merge
1212           |         /       /   \
1213           |        /       /     \
1214          \|/      /      |/_      \
1215        get_r_value_internal        |
1216                 |                  |
1217             |                  |
1218            \|/                \|/
1219         new_rd_Phi0          new_rd_Phi_in
1220
1221 * *************************************************************************** */
1222
1223 /** Creates a Phi node with 0 predecessors */
1224 static INLINE ir_node *
1225 new_rd_Phi0 (ir_graph *irg, ir_node *block, ir_mode *mode)
1226 {
1227   ir_node *res;
1228
1229   res = new_ir_node(NULL, irg, block, op_Phi, mode, 0, NULL);
1230   IRN_VRFY_IRG(res, irg);
1231   return res;
1232 }
1233
1234 /* There are two implementations of the Phi node construction.  The first
1235    is faster, but does not work for blocks with more than 2 predecessors.
1236    The second works always but is slower and causes more unnecessary Phi
1237    nodes.
1238    Select the implementations by the following preprocessor flag set in
1239    common/common.h: */
1240 #if USE_FAST_PHI_CONSTRUCTION
1241
1242 /* This is a stack used for allocating and deallocating nodes in
1243    new_rd_Phi_in.  The original implementation used the obstack
1244    to model this stack, now it is explicit.  This reduces side effects.
1245 */
1246 #if USE_EXPLICIT_PHI_IN_STACK
1247 Phi_in_stack *
1248 new_Phi_in_stack(void) {
1249   Phi_in_stack *res;
1250
1251   res = (Phi_in_stack *) malloc ( sizeof (Phi_in_stack));
1252
1253   res->stack = NEW_ARR_F (ir_node *, 0);
1254   res->pos = 0;
1255
1256   return res;
1257 }
1258
1259 void
1260 free_Phi_in_stack(Phi_in_stack *s) {
1261   DEL_ARR_F(s->stack);
1262   free(s);
1263 }
1264 static INLINE void
1265 free_to_Phi_in_stack(ir_node *phi) {
1266   if (ARR_LEN(current_ir_graph->Phi_in_stack->stack) ==
1267       current_ir_graph->Phi_in_stack->pos)
1268     ARR_APP1 (ir_node *, current_ir_graph->Phi_in_stack->stack, phi);
1269   else
1270     current_ir_graph->Phi_in_stack->stack[current_ir_graph->Phi_in_stack->pos] = phi;
1271
1272   (current_ir_graph->Phi_in_stack->pos)++;
1273 }
1274
1275 static INLINE ir_node *
1276 alloc_or_pop_from_Phi_in_stack(ir_graph *irg, ir_node *block, ir_mode *mode,
1277          int arity, ir_node **in) {
1278   ir_node *res;
1279   ir_node **stack = current_ir_graph->Phi_in_stack->stack;
1280   int pos = current_ir_graph->Phi_in_stack->pos;
1281
1282
1283   if (pos == 0) {
1284     /* We need to allocate a new node */
1285     res = new_ir_node (db, irg, block, op_Phi, mode, arity, in);
1286     res->attr.phi_backedge = new_backedge_arr(irg->obst, arity);
1287   } else {
1288     /* reuse the old node and initialize it again. */
1289     res = stack[pos-1];
1290
1291     assert (res->kind == k_ir_node);
1292     assert (res->op == op_Phi);
1293     res->mode = mode;
1294     res->visited = 0;
1295     res->link = NULL;
1296     assert (arity >= 0);
1297     /* ???!!! How to free the old in array??  Not at all: on obstack ?!! */
1298     res->in = NEW_ARR_D (ir_node *, irg->obst, (arity+1));
1299     res->in[0] = block;
1300     memcpy (&res->in[1], in, sizeof (ir_node *) * arity);
1301
1302     (current_ir_graph->Phi_in_stack->pos)--;
1303   }
1304   return res;
1305 }
1306 #endif /* USE_EXPLICIT_PHI_IN_STACK */
1307
1308 /* Creates a Phi node with a given, fixed array **in of predecessors.
1309    If the Phi node is unnecessary, as the same value reaches the block
1310    through all control flow paths, it is eliminated and the value
1311    returned directly.  This constructor is only intended for use in
1312    the automatic Phi node generation triggered by get_value or mature.
1313    The implementation is quite tricky and depends on the fact, that
1314    the nodes are allocated on a stack:
1315    The in array contains predecessors and NULLs.  The NULLs appear,
1316    if get_r_value_internal, that computed the predecessors, reached
1317    the same block on two paths.  In this case the same value reaches
1318    this block on both paths, there is no definition in between.  We need
1319    not allocate a Phi where these path's merge, but we have to communicate
1320    this fact to the caller.  This happens by returning a pointer to the
1321    node the caller _will_ allocate.  (Yes, we predict the address. We can
1322    do so because the nodes are allocated on the obstack.)  The caller then
1323    finds a pointer to itself and, when this routine is called again,
1324    eliminates itself.
1325    */
1326 static INLINE ir_node *
1327 new_rd_Phi_in (ir_graph *irg, ir_node *block, ir_mode *mode, ir_node **in, int ins)
1328 {
1329   int i;
1330   ir_node *res, *known;
1331
1332   /* Allocate a new node on the obstack.  This can return a node to
1333      which some of the pointers in the in-array already point.
1334      Attention: the constructor copies the in array, i.e., the later
1335      changes to the array in this routine do not affect the
1336      constructed node!  If the in array contains NULLs, there will be
1337      missing predecessors in the returned node.  Is this a possible
1338      internal state of the Phi node generation? */
1339 #if USE_EXPLICIT_PHI_IN_STACK
1340   res = known = alloc_or_pop_from_Phi_in_stack(irg, block, mode, ins, in);
1341 #else
1342   res = known = new_ir_node (NULL, irg, block, op_Phi, mode, ins, in);
1343   res->attr.phi_backedge = new_backedge_arr(irg->obst, ins);
1344 #endif
1345
1346   /* The in-array can contain NULLs.  These were returned by
1347      get_r_value_internal if it reached the same block/definition on a
1348      second path.  The NULLs are replaced by the node itself to
1349      simplify the test in the next loop. */
1350   for (i = 0;  i < ins;  ++i) {
1351     if (in[i] == NULL)
1352       in[i] = res;
1353   }
1354
1355   /* This loop checks whether the Phi has more than one predecessor.
1356      If so, it is a real Phi node and we break the loop.  Else the Phi
1357      node merges the same definition on several paths and therefore is
1358      not needed. */
1359   for (i = 0;  i < ins;  ++i) {
1360     if (in[i] == res || in[i] == known)
1361       continue;
1362
1363     if (known == res)
1364       known = in[i];
1365     else
1366       break;
1367   }
1368
1369   /* i==ins: there is at most one predecessor, we don't need a phi node. */
1370   if (i==ins) {
1371 #if USE_EXPLICIT_PHI_IN_STACK
1372     free_to_Phi_in_stack(res);
1373 #else
1374     edges_node_deleted(res, current_ir_graph);
1375     obstack_free(current_ir_graph->obst, res);
1376 #endif
1377     res = known;
1378   } else {
1379     res = optimize_node (res);
1380     IRN_VRFY_IRG(res, irg);
1381   }
1382
1383   /* return the pointer to the Phi node.  This node might be deallocated! */
1384   return res;
1385 }
1386
1387 static ir_node *
1388 get_r_value_internal (ir_node *block, int pos, ir_mode *mode);
1389
1390 /**
1391     allocates and returns this node.  The routine called to allocate the
1392     node might optimize it away and return a real value, or even a pointer
1393     to a deallocated Phi node on top of the obstack!
1394     This function is called with an in-array of proper size. **/
1395 static ir_node *
1396 phi_merge (ir_node *block, int pos, ir_mode *mode, ir_node **nin, int ins)
1397 {
1398   ir_node *prevBlock, *res;
1399   int i;
1400
1401   /* This loop goes to all predecessor blocks of the block the Phi node is in
1402      and there finds the operands of the Phi node by calling
1403      get_r_value_internal. */
1404   for (i = 1;  i <= ins;  ++i) {
1405     assert (block->in[i]);
1406     prevBlock = block->in[i]->in[0]; /* go past control flow op to prev block */
1407     assert (prevBlock);
1408     nin[i-1] = get_r_value_internal (prevBlock, pos, mode);
1409   }
1410
1411   /* After collecting all predecessors into the array nin a new Phi node
1412      with these predecessors is created.  This constructor contains an
1413      optimization: If all predecessors of the Phi node are identical it
1414      returns the only operand instead of a new Phi node.  If the value
1415      passes two different control flow edges without being defined, and
1416      this is the second path treated, a pointer to the node that will be
1417      allocated for the first path (recursion) is returned.  We already
1418      know the address of this node, as it is the next node to be allocated
1419      and will be placed on top of the obstack. (The obstack is a _stack_!) */
1420   res = new_rd_Phi_in (current_ir_graph, block, mode, nin, ins);
1421
1422   /* Now we now the value for "pos" and can enter it in the array with
1423      all known local variables.  Attention: this might be a pointer to
1424      a node, that later will be allocated!!! See new_rd_Phi_in.
1425      If this is called in mature, after some set_value in the same block,
1426      the proper value must not be overwritten:
1427      The call order
1428        get_value    (makes Phi0, put's it into graph_arr)
1429        set_value    (overwrites Phi0 in graph_arr)
1430        mature_immBlock (upgrades Phi0, puts it again into graph_arr, overwriting
1431                      the proper value.)
1432      fails. */
1433   if (!block->attr.block.graph_arr[pos]) {
1434     block->attr.block.graph_arr[pos] = res;
1435   } else {
1436     /*  printf(" value already computed by %s\n",
1437         get_id_str(block->attr.block.graph_arr[pos]->op->name));  */
1438   }
1439
1440   return res;
1441 }
1442
1443 /* This function returns the last definition of a variable.  In case
1444    this variable was last defined in a previous block, Phi nodes are
1445    inserted.  If the part of the firm graph containing the definition
1446    is not yet constructed, a dummy Phi node is returned. */
1447 static ir_node *
1448 get_r_value_internal (ir_node *block, int pos, ir_mode *mode)
1449 {
1450   ir_node *res;
1451   /* There are 4 cases to treat.
1452
1453      1. The block is not mature and we visit it the first time.  We can not
1454         create a proper Phi node, therefore a Phi0, i.e., a Phi without
1455         predecessors is returned.  This node is added to the linked list (field
1456         "link") of the containing block to be completed when this block is
1457         matured. (Completion will add a new Phi and turn the Phi0 into an Id
1458         node.)
1459
1460      2. The value is already known in this block, graph_arr[pos] is set and we
1461         visit the block the first time.  We can return the value without
1462         creating any new nodes.
1463
1464      3. The block is mature and we visit it the first time.  A Phi node needs
1465         to be created (phi_merge).  If the Phi is not needed, as all it's
1466         operands are the same value reaching the block through different
1467         paths, it's optimized away and the value itself is returned.
1468
1469      4. The block is mature, and we visit it the second time.  Now two
1470         subcases are possible:
1471         * The value was computed completely the last time we were here. This
1472           is the case if there is no loop.  We can return the proper value.
1473         * The recursion that visited this node and set the flag did not
1474           return yet.  We are computing a value in a loop and need to
1475           break the recursion without knowing the result yet.
1476       @@@ strange case.  Straight forward we would create a Phi before
1477       starting the computation of it's predecessors.  In this case we will
1478       find a Phi here in any case.  The problem is that this implementation
1479       only creates a Phi after computing the predecessors, so that it is
1480       hard to compute self references of this Phi.  @@@
1481         There is no simple check for the second subcase.  Therefore we check
1482         for a second visit and treat all such cases as the second subcase.
1483         Anyways, the basic situation is the same:  we reached a block
1484         on two paths without finding a definition of the value:  No Phi
1485         nodes are needed on both paths.
1486         We return this information "Two paths, no Phi needed" by a very tricky
1487         implementation that relies on the fact that an obstack is a stack and
1488         will return a node with the same address on different allocations.
1489         Look also at phi_merge and new_rd_phi_in to understand this.
1490     @@@ Unfortunately this does not work, see testprogram
1491     three_cfpred_example.
1492
1493   */
1494
1495   /* case 4 -- already visited. */
1496   if (get_irn_visited(block) == get_irg_visited(current_ir_graph)) return NULL;
1497
1498   /* visited the first time */
1499   set_irn_visited(block, get_irg_visited(current_ir_graph));
1500
1501   /* Get the local valid value */
1502   res = block->attr.block.graph_arr[pos];
1503
1504   /* case 2 -- If the value is actually computed, return it. */
1505   if (res) return res;
1506
1507   if (block->attr.block.matured) { /* case 3 */
1508
1509     /* The Phi has the same amount of ins as the corresponding block. */
1510     int ins = get_irn_arity(block);
1511     ir_node **nin;
1512     NEW_ARR_A (ir_node *, nin, ins);
1513
1514     /* Phi merge collects the predecessors and then creates a node. */
1515     res = phi_merge (block, pos, mode, nin, ins);
1516
1517   } else {  /* case 1 */
1518     /* The block is not mature, we don't know how many in's are needed.  A Phi
1519        with zero predecessors is created.  Such a Phi node is called Phi0
1520        node.  (There is also an obsolete Phi0 opcode.) The Phi0 is then added
1521        to the list of Phi0 nodes in this block to be matured by mature_immBlock
1522        later.
1523        The Phi0 has to remember the pos of it's internal value.  If the real
1524        Phi is computed, pos is used to update the array with the local
1525        values. */
1526
1527     res = new_rd_Phi0 (current_ir_graph, block, mode);
1528     res->attr.phi0_pos = pos;
1529     res->link = block->link;
1530     block->link = res;
1531   }
1532
1533   /* If we get here, the frontend missed a use-before-definition error */
1534   if (!res) {
1535     /* Error Message */
1536     printf("Error: no value set.  Use of undefined variable.  Initializing to zero.\n");
1537     assert (mode->code >= irm_F && mode->code <= irm_P);
1538     res = new_rd_Const (NULL, current_ir_graph, block, mode,
1539                tarval_mode_null[mode->code]);
1540   }
1541
1542   /* The local valid value is available now. */
1543   block->attr.block.graph_arr[pos] = res;
1544
1545   return res;
1546 }
1547
1548 #else /* if 0 */
1549
1550 /**
1551     it starts the recursion.  This causes an Id at the entry of
1552     every block that has no definition of the value! **/
1553
1554 #if USE_EXPLICIT_PHI_IN_STACK
1555 /* Just dummies */
1556 Phi_in_stack * new_Phi_in_stack() {  return NULL; }
1557 void free_Phi_in_stack(Phi_in_stack *s) { }
1558 #endif
1559
1560 static INLINE ir_node *
1561 new_rd_Phi_in (ir_graph *irg, ir_node *block, ir_mode *mode,
1562            ir_node **in, int ins, ir_node *phi0)
1563 {
1564   int i;
1565   ir_node *res, *known;
1566
1567   /* Allocate a new node on the obstack.  The allocation copies the in
1568      array. */
1569   res = new_ir_node (NULL, irg, block, op_Phi, mode, ins, in);
1570   res->attr.phi_backedge = new_backedge_arr(irg->obst, ins);
1571
1572   /* This loop checks whether the Phi has more than one predecessor.
1573      If so, it is a real Phi node and we break the loop.  Else the
1574      Phi node merges the same definition on several paths and therefore
1575      is not needed. Don't consider Bad nodes! */
1576   known = res;
1577   for (i=0;  i < ins;  ++i)
1578   {
1579     assert(in[i]);
1580
1581     in[i] = skip_Id(in[i]);  /* increasses the number of freed Phis. */
1582
1583     /* Optimize self referencing Phis:  We can't detect them yet properly, as
1584        they still refer to the Phi0 they will replace.  So replace right now. */
1585     if (phi0 && in[i] == phi0) in[i] = res;
1586
1587     if (in[i]==res || in[i]==known || is_Bad(in[i])) continue;
1588
1589     if (known==res)
1590       known = in[i];
1591     else
1592       break;
1593   }
1594
1595   /* i==ins: there is at most one predecessor, we don't need a phi node. */
1596   if (i == ins) {
1597     if (res != known) {
1598       edges_node_deleted(res, current_ir_graph);
1599       obstack_free (current_ir_graph->obst, res);
1600       if (is_Phi(known)) {
1601         /* If pred is a phi node we want to optimize it: If loops are matured in a bad
1602            order, an enclosing Phi know may get superfluous. */
1603         res = optimize_in_place_2(known);
1604         if (res != known)
1605           exchange(known, res);
1606
1607       }
1608       else
1609         res = known;
1610     } else {
1611       /* A undefined value, e.g., in unreachable code. */
1612       res = new_Bad();
1613     }
1614   } else {
1615     res = optimize_node (res);  /* This is necessary to add the node to the hash table for cse. */
1616     IRN_VRFY_IRG(res, irg);
1617     /* Memory Phis in endless loops must be kept alive.
1618        As we can't distinguish these easily we keep all of them alive. */
1619     if ((res->op == op_Phi) && (mode == mode_M))
1620       add_End_keepalive(irg->end, res);
1621   }
1622
1623   return res;
1624 }
1625
1626 static ir_node *
1627 get_r_value_internal (ir_node *block, int pos, ir_mode *mode);
1628
1629 #if PRECISE_EXC_CONTEXT
1630 static ir_node *
1631 phi_merge (ir_node *block, int pos, ir_mode *mode, ir_node **nin, int ins);
1632
1633 /* Construct a new frag_array for node n.
1634    Copy the content from the current graph_arr of the corresponding block:
1635    this is the current state.
1636    Set ProjM(n) as current memory state.
1637    Further the last entry in frag_arr of current block points to n.  This
1638    constructs a chain block->last_frag_op-> ... first_frag_op of all frag ops in the block.
1639  */
1640 static INLINE ir_node ** new_frag_arr (ir_node *n)
1641 {
1642   ir_node **arr;
1643   int opt;
1644
1645   arr = NEW_ARR_D (ir_node *, current_ir_graph->obst, current_ir_graph->n_loc);
1646   memcpy(arr, current_ir_graph->current_block->attr.block.graph_arr,
1647      sizeof(ir_node *)*current_ir_graph->n_loc);
1648
1649   /* turn off optimization before allocating Proj nodes, as res isn't
1650      finished yet. */
1651   opt = get_opt_optimize(); set_optimize(0);
1652   /* Here we rely on the fact that all frag ops have Memory as first result! */
1653   if (get_irn_op(n) == op_Call)
1654     arr[0] = new_Proj(n, mode_M, pn_Call_M_except);
1655   else {
1656     assert((pn_Quot_M == pn_DivMod_M) &&
1657        (pn_Quot_M == pn_Div_M)    &&
1658        (pn_Quot_M == pn_Mod_M)    &&
1659        (pn_Quot_M == pn_Load_M)   &&
1660        (pn_Quot_M == pn_Store_M)  &&
1661        (pn_Quot_M == pn_Alloc_M)    );
1662     arr[0] = new_Proj(n, mode_M, pn_Alloc_M);
1663   }
1664   set_optimize(opt);
1665
1666   current_ir_graph->current_block->attr.block.graph_arr[current_ir_graph->n_loc-1] = n;
1667   return arr;
1668 }
1669
1670 /**
1671  * returns the frag_arr from a node
1672  */
1673 static INLINE ir_node **
1674 get_frag_arr (ir_node *n) {
1675   switch (get_irn_opcode(n)) {
1676   case iro_Call:
1677     return n->attr.call.exc.frag_arr;
1678   case iro_Alloc:
1679     return n->attr.a.exc.frag_arr;
1680   case iro_Load:
1681     return n->attr.load.exc.frag_arr;
1682   case iro_Store:
1683     return n->attr.store.exc.frag_arr;
1684   default:
1685     return n->attr.except.frag_arr;
1686   }
1687 }
1688
1689 static void
1690 set_frag_value(ir_node **frag_arr, int pos, ir_node *val) {
1691 #if 0
1692   if (!frag_arr[pos]) frag_arr[pos] = val;
1693   if (frag_arr[current_ir_graph->n_loc - 1]) {
1694     ir_node **arr = get_frag_arr(frag_arr[current_ir_graph->n_loc - 1]);
1695     assert(arr != frag_arr && "Endless recursion detected");
1696     set_frag_value(arr, pos, val);
1697   }
1698 #else
1699   int i;
1700
1701   for (i = 0; i < 1000; ++i) {
1702     if (!frag_arr[pos]) {
1703       frag_arr[pos] = val;
1704     }
1705     if (frag_arr[current_ir_graph->n_loc - 1]) {
1706       ir_node **arr = get_frag_arr(frag_arr[current_ir_graph->n_loc - 1]);
1707       frag_arr = arr;
1708     }
1709     else
1710       return;
1711   }
1712   assert(0 && "potential endless recursion");
1713 #endif
1714 }
1715
1716 static ir_node *
1717 get_r_frag_value_internal (ir_node *block, ir_node *cfOp, int pos, ir_mode *mode) {
1718   ir_node *res;
1719   ir_node **frag_arr;
1720
1721   assert(is_fragile_op(cfOp) && (get_irn_op(cfOp) != op_Bad));
1722
1723   frag_arr = get_frag_arr(cfOp);
1724   res = frag_arr[pos];
1725   if (!res) {
1726     if (block->attr.block.graph_arr[pos]) {
1727       /* There was a set_value after the cfOp and no get_value before that
1728          set_value.  We must build a Phi node now. */
1729       if (block->attr.block.matured) {
1730         int ins = get_irn_arity(block);
1731         ir_node **nin;
1732         NEW_ARR_A (ir_node *, nin, ins);
1733         res = phi_merge(block, pos, mode, nin, ins);
1734       } else {
1735         res = new_rd_Phi0 (current_ir_graph, block, mode);
1736         res->attr.phi0_pos = pos;
1737         res->link = block->link;
1738         block->link = res;
1739       }
1740       assert(res);
1741       /* @@@ tested by Flo: set_frag_value(frag_arr, pos, res);
1742          but this should be better: (remove comment if this works) */
1743       /* It's a Phi, we can write this into all graph_arrs with NULL */
1744       set_frag_value(block->attr.block.graph_arr, pos, res);
1745     } else {
1746       res = get_r_value_internal(block, pos, mode);
1747       set_frag_value(block->attr.block.graph_arr, pos, res);
1748     }
1749   }
1750   return res;
1751 }
1752 #endif
1753
1754 /**
1755     computes the predecessors for the real phi node, and then
1756     allocates and returns this node.  The routine called to allocate the
1757     node might optimize it away and return a real value.
1758     This function must be called with an in-array of proper size. **/
1759 static ir_node *
1760 phi_merge (ir_node *block, int pos, ir_mode *mode, ir_node **nin, int ins)
1761 {
1762   ir_node *prevBlock, *prevCfOp, *res, *phi0, *phi0_all;
1763   int i;
1764
1765   /* If this block has no value at pos create a Phi0 and remember it
1766      in graph_arr to break recursions.
1767      Else we may not set graph_arr as there a later value is remembered. */
1768   phi0 = NULL;
1769   if (!block->attr.block.graph_arr[pos]) {
1770     if (block == get_irg_start_block(current_ir_graph)) {
1771       /* Collapsing to Bad tarvals is no good idea.
1772          So we call a user-supplied routine here that deals with this case as
1773          appropriate for the given language. Sorryly the only help we can give
1774          here is the position.
1775
1776          Even if all variables are defined before use, it can happen that
1777          we get to the start block, if a cond has been replaced by a tuple
1778          (bad, jmp).  In this case we call the function needlessly, eventually
1779          generating an non existant error.
1780          However, this SHOULD NOT HAPPEN, as bad control flow nodes are intercepted
1781          before recuring.
1782       */
1783       if (default_initialize_local_variable)
1784         block->attr.block.graph_arr[pos] = default_initialize_local_variable(current_ir_graph, mode, pos - 1);
1785       else
1786         block->attr.block.graph_arr[pos] = new_Const(mode, tarval_bad);
1787       /* We don't need to care about exception ops in the start block.
1788      There are none by definition. */
1789       return block->attr.block.graph_arr[pos];
1790     } else {
1791       phi0 = new_rd_Phi0(current_ir_graph, block, mode);
1792       block->attr.block.graph_arr[pos] = phi0;
1793 #if PRECISE_EXC_CONTEXT
1794       if (get_opt_precise_exc_context()) {
1795     /* Set graph_arr for fragile ops.  Also here we should break recursion.
1796        We could choose a cyclic path through an cfop.  But the recursion would
1797        break at some point. */
1798     set_frag_value(block->attr.block.graph_arr, pos, phi0);
1799       }
1800 #endif
1801     }
1802   }
1803
1804   /* This loop goes to all predecessor blocks of the block the Phi node
1805      is in and there finds the operands of the Phi node by calling
1806      get_r_value_internal.  */
1807   for (i = 1;  i <= ins;  ++i) {
1808     prevCfOp = skip_Proj(block->in[i]);
1809     assert (prevCfOp);
1810     if (is_Bad(prevCfOp)) {
1811       /* In case a Cond has been optimized we would get right to the start block
1812      with an invalid definition. */
1813       nin[i-1] = new_Bad();
1814       continue;
1815     }
1816     prevBlock = block->in[i]->in[0]; /* go past control flow op to prev block */
1817     assert (prevBlock);
1818     if (!is_Bad(prevBlock)) {
1819 #if PRECISE_EXC_CONTEXT
1820       if (get_opt_precise_exc_context() &&
1821       is_fragile_op(prevCfOp) && (get_irn_op (prevCfOp) != op_Bad)) {
1822     assert(get_r_frag_value_internal (prevBlock, prevCfOp, pos, mode));
1823     nin[i-1] = get_r_frag_value_internal (prevBlock, prevCfOp, pos, mode);
1824       } else
1825 #endif
1826       nin[i-1] = get_r_value_internal (prevBlock, pos, mode);
1827     } else {
1828       nin[i-1] = new_Bad();
1829     }
1830   }
1831
1832   /* We want to pass the Phi0 node to the constructor: this finds additional
1833      optimization possibilities.
1834      The Phi0 node either is allocated in this function, or it comes from
1835      a former call to get_r_value_internal. In this case we may not yet
1836      exchange phi0, as this is done in mature_immBlock. */
1837   if (!phi0) {
1838     phi0_all = block->attr.block.graph_arr[pos];
1839     if (!((get_irn_op(phi0_all) == op_Phi) &&
1840       (get_irn_arity(phi0_all) == 0)   &&
1841       (get_nodes_block(phi0_all) == block)))
1842       phi0_all = NULL;
1843   } else {
1844     phi0_all = phi0;
1845   }
1846
1847   /* After collecting all predecessors into the array nin a new Phi node
1848      with these predecessors is created.  This constructor contains an
1849      optimization: If all predecessors of the Phi node are identical it
1850      returns the only operand instead of a new Phi node.  */
1851   res = new_rd_Phi_in (current_ir_graph, block, mode, nin, ins, phi0_all);
1852
1853   /* In case we allocated a Phi0 node at the beginning of this procedure,
1854      we need to exchange this Phi0 with the real Phi. */
1855   if (phi0) {
1856     exchange(phi0, res);
1857     block->attr.block.graph_arr[pos] = res;
1858     /* Don't set_frag_value as it does not overwrite.  Doesn't matter, is
1859        only an optimization. */
1860   }
1861
1862   return res;
1863 }
1864
1865 /* This function returns the last definition of a variable.  In case
1866    this variable was last defined in a previous block, Phi nodes are
1867    inserted.  If the part of the firm graph containing the definition
1868    is not yet constructed, a dummy Phi node is returned. */
1869 static ir_node *
1870 get_r_value_internal (ir_node *block, int pos, ir_mode *mode)
1871 {
1872   ir_node *res;
1873   /* There are 4 cases to treat.
1874
1875      1. The block is not mature and we visit it the first time.  We can not
1876         create a proper Phi node, therefore a Phi0, i.e., a Phi without
1877         predecessors is returned.  This node is added to the linked list (field
1878         "link") of the containing block to be completed when this block is
1879         matured. (Comlpletion will add a new Phi and turn the Phi0 into an Id
1880         node.)
1881
1882      2. The value is already known in this block, graph_arr[pos] is set and we
1883         visit the block the first time.  We can return the value without
1884         creating any new nodes.
1885
1886      3. The block is mature and we visit it the first time.  A Phi node needs
1887         to be created (phi_merge).  If the Phi is not needed, as all it's
1888         operands are the same value reaching the block through different
1889         paths, it's optimized away and the value itself is returned.
1890
1891      4. The block is mature, and we visit it the second time.  Now two
1892         subcases are possible:
1893         * The value was computed completely the last time we were here. This
1894           is the case if there is no loop.  We can return the proper value.
1895         * The recursion that visited this node and set the flag did not
1896           return yet.  We are computing a value in a loop and need to
1897           break the recursion.  This case only happens if we visited
1898       the same block with phi_merge before, which inserted a Phi0.
1899       So we return the Phi0.
1900   */
1901
1902   /* case 4 -- already visited. */
1903   if (get_irn_visited(block) == get_irg_visited(current_ir_graph)) {
1904     /* As phi_merge allocates a Phi0 this value is always defined. Here
1905      is the critical difference of the two algorithms. */
1906     assert(block->attr.block.graph_arr[pos]);
1907     return block->attr.block.graph_arr[pos];
1908   }
1909
1910   /* visited the first time */
1911   set_irn_visited(block, get_irg_visited(current_ir_graph));
1912
1913   /* Get the local valid value */
1914   res = block->attr.block.graph_arr[pos];
1915
1916   /* case 2 -- If the value is actually computed, return it. */
1917   if (res) { return res; };
1918
1919   if (block->attr.block.matured) { /* case 3 */
1920
1921     /* The Phi has the same amount of ins as the corresponding block. */
1922     int ins = get_irn_arity(block);
1923     ir_node **nin;
1924     NEW_ARR_A (ir_node *, nin, ins);
1925
1926     /* Phi merge collects the predecessors and then creates a node. */
1927     res = phi_merge (block, pos, mode, nin, ins);
1928
1929   } else {  /* case 1 */
1930     /* The block is not mature, we don't know how many in's are needed.  A Phi
1931        with zero predecessors is created.  Such a Phi node is called Phi0
1932        node.  The Phi0 is then added to the list of Phi0 nodes in this block
1933        to be matured by mature_immBlock later.
1934        The Phi0 has to remember the pos of it's internal value.  If the real
1935        Phi is computed, pos is used to update the array with the local
1936        values. */
1937     res = new_rd_Phi0 (current_ir_graph, block, mode);
1938     res->attr.phi0_pos = pos;
1939     res->link = block->link;
1940     block->link = res;
1941   }
1942
1943   /* If we get here, the frontend missed a use-before-definition error */
1944   if (!res) {
1945     /* Error Message */
1946     printf("Error: no value set.  Use of undefined variable.  Initializing to zero.\n");
1947     assert (mode->code >= irm_F && mode->code <= irm_P);
1948     res = new_rd_Const (NULL, current_ir_graph, block, mode,
1949             get_mode_null(mode));
1950   }
1951
1952   /* The local valid value is available now. */
1953   block->attr.block.graph_arr[pos] = res;
1954
1955   return res;
1956 }
1957
1958 #endif /* USE_FAST_PHI_CONSTRUCTION */
1959
1960 /* ************************************************************************** */
1961
1962 /** Finalize a Block node, when all control flows are known.  */
1963 /** Acceptable parameters are only Block nodes.               */
1964 void
1965 mature_immBlock (ir_node *block)
1966 {
1967
1968   int ins;
1969   ir_node *n, **nin;
1970   ir_node *next;
1971
1972   assert (get_irn_opcode(block) == iro_Block);
1973   /* @@@ should be commented in
1974      assert (!get_Block_matured(block) && "Block already matured"); */
1975
1976   if (!get_Block_matured(block)) {
1977     ins = ARR_LEN (block->in)-1;
1978     /* Fix block parameters */
1979     block->attr.block.backedge = new_backedge_arr(current_ir_graph->obst, ins);
1980
1981     /* An array for building the Phi nodes. */
1982     NEW_ARR_A (ir_node *, nin, ins);
1983
1984     /* Traverse a chain of Phi nodes attached to this block and mature
1985        these, too. **/
1986     for (n = block->link;  n;  n=next) {
1987       inc_irg_visited(current_ir_graph);
1988       next = n->link;
1989       exchange (n, phi_merge (block, n->attr.phi0_pos, n->mode, nin, ins));
1990     }
1991
1992     block->attr.block.matured = 1;
1993
1994     /* Now, as the block is a finished firm node, we can optimize it.
1995        Since other nodes have been allocated since the block was created
1996        we can not free the node on the obstack.  Therefore we have to call
1997        optimize_in_place.
1998        Unfortunately the optimization does not change a lot, as all allocated
1999        nodes refer to the unoptimized node.
2000        We can call _2, as global cse has no effect on blocks. */
2001     block = optimize_in_place_2(block);
2002     IRN_VRFY_IRG(block, current_ir_graph);
2003   }
2004 }
2005
2006 ir_node *
2007 new_d_Phi (dbg_info* db, int arity, ir_node **in, ir_mode *mode)
2008 {
2009   return new_rd_Phi(db, current_ir_graph, current_ir_graph->current_block,
2010             arity, in, mode);
2011 }
2012
2013 ir_node *
2014 new_d_Const (dbg_info* db, ir_mode *mode, tarval *con)
2015 {
2016   return new_rd_Const(db, current_ir_graph, current_ir_graph->start_block,
2017               mode, con);
2018 }
2019
2020 ir_node *
2021 new_d_Const_long(dbg_info* db, ir_mode *mode, long value)
2022 {
2023   return new_rd_Const_long(db, current_ir_graph, current_ir_graph->start_block, mode, value);
2024 }
2025
2026 ir_node *
2027 new_d_Const_type (dbg_info* db, ir_mode *mode, tarval *con, type *tp)
2028 {
2029   return new_rd_Const_type(db, current_ir_graph, current_ir_graph->start_block,
2030                 mode, con, tp);
2031 }
2032
2033
2034 ir_node *
2035 new_d_Id (dbg_info* db, ir_node *val, ir_mode *mode)
2036 {
2037   return new_rd_Id(db, current_ir_graph, current_ir_graph->current_block,
2038            val, mode);
2039 }
2040
2041 ir_node *
2042 new_d_Proj (dbg_info* db, ir_node *arg, ir_mode *mode, long proj)
2043 {
2044   return new_rd_Proj(db, current_ir_graph, current_ir_graph->current_block,
2045              arg, mode, proj);
2046 }
2047
2048 ir_node *
2049 new_d_defaultProj (dbg_info* db, ir_node *arg, long max_proj)
2050 {
2051   ir_node *res;
2052   assert(arg->op == op_Cond);
2053   arg->attr.c.kind = fragmentary;
2054   arg->attr.c.default_proj = max_proj;
2055   res = new_Proj (arg, mode_X, max_proj);
2056   return res;
2057 }
2058
2059 ir_node *
2060 new_d_Conv (dbg_info* db, ir_node *op, ir_mode *mode)
2061 {
2062   return new_rd_Conv(db, current_ir_graph, current_ir_graph->current_block,
2063              op, mode);
2064 }
2065
2066 ir_node *
2067 new_d_Cast (dbg_info* db, ir_node *op, type *to_tp)
2068 {
2069   return new_rd_Cast(db, current_ir_graph, current_ir_graph->current_block, op, to_tp);
2070 }
2071
2072 ir_node *
2073 new_d_Tuple (dbg_info* db, int arity, ir_node **in)
2074 {
2075   return new_rd_Tuple(db, current_ir_graph, current_ir_graph->current_block,
2076               arity, in);
2077 }
2078
2079 ir_node *
2080 new_d_Add (dbg_info* db, ir_node *op1, ir_node *op2, ir_mode *mode)
2081 {
2082   return new_rd_Add(db, current_ir_graph, current_ir_graph->current_block,
2083             op1, op2, mode);
2084 }
2085
2086 ir_node *
2087 new_d_Sub (dbg_info* db, ir_node *op1, ir_node *op2, ir_mode *mode)
2088 {
2089   return new_rd_Sub(db, current_ir_graph, current_ir_graph->current_block,
2090             op1, op2, mode);
2091 }
2092
2093
2094 ir_node *
2095 new_d_Minus (dbg_info* db, ir_node *op,  ir_mode *mode)
2096 {
2097   return new_rd_Minus(db, current_ir_graph, current_ir_graph->current_block,
2098               op, mode);
2099 }
2100
2101 ir_node *
2102 new_d_Mul (dbg_info* db, ir_node *op1, ir_node *op2, ir_mode *mode)
2103 {
2104   return new_rd_Mul(db, current_ir_graph, current_ir_graph->current_block,
2105             op1, op2, mode);
2106 }
2107
2108 /**
2109  * allocate the frag array
2110  */
2111 static void allocate_frag_arr(ir_node *res, ir_op *op, ir_node ***frag_store) {
2112   if (get_opt_precise_exc_context()) {
2113     if ((current_ir_graph->phase_state == phase_building) &&
2114     (get_irn_op(res) == op) && /* Could be optimized away. */
2115     !*frag_store)    /* Could be a cse where the arr is already set. */ {
2116       *frag_store = new_frag_arr(res);
2117     }
2118   }
2119 }
2120
2121
2122 ir_node *
2123 new_d_Quot (dbg_info* db, ir_node *memop, ir_node *op1, ir_node *op2)
2124 {
2125   ir_node *res;
2126   res = new_rd_Quot (db, current_ir_graph, current_ir_graph->current_block,
2127              memop, op1, op2);
2128   res->attr.except.pin_state = op_pin_state_pinned;
2129 #if PRECISE_EXC_CONTEXT
2130   allocate_frag_arr(res, op_Quot, &res->attr.except.frag_arr);  /* Could be optimized away. */
2131 #endif
2132
2133   return res;
2134 }
2135
2136 ir_node *
2137 new_d_DivMod (dbg_info* db, ir_node *memop, ir_node *op1, ir_node *op2)
2138 {
2139   ir_node *res;
2140   res = new_rd_DivMod (db, current_ir_graph, current_ir_graph->current_block,
2141                memop, op1, op2);
2142   res->attr.except.pin_state = op_pin_state_pinned;
2143 #if PRECISE_EXC_CONTEXT
2144   allocate_frag_arr(res, op_DivMod, &res->attr.except.frag_arr);  /* Could be optimized away. */
2145 #endif
2146
2147   return res;
2148 }
2149
2150 ir_node *
2151 new_d_Div (dbg_info* db, ir_node *memop, ir_node *op1, ir_node *op2)
2152 {
2153   ir_node *res;
2154   res = new_rd_Div (db, current_ir_graph, current_ir_graph->current_block,
2155             memop, op1, op2);
2156   res->attr.except.pin_state = op_pin_state_pinned;
2157 #if PRECISE_EXC_CONTEXT
2158   allocate_frag_arr(res, op_Div, &res->attr.except.frag_arr);  /* Could be optimized away. */
2159 #endif
2160
2161   return res;
2162 }
2163
2164 ir_node *
2165 new_d_Mod (dbg_info* db, ir_node *memop, ir_node *op1, ir_node *op2)
2166 {
2167   ir_node *res;
2168   res = new_rd_Mod (db, current_ir_graph, current_ir_graph->current_block,
2169             memop, op1, op2);
2170   res->attr.except.pin_state = op_pin_state_pinned;
2171 #if PRECISE_EXC_CONTEXT
2172   allocate_frag_arr(res, op_Mod, &res->attr.except.frag_arr);  /* Could be optimized away. */
2173 #endif
2174
2175   return res;
2176 }
2177
2178 ir_node *
2179 new_d_And (dbg_info* db, ir_node *op1, ir_node *op2, ir_mode *mode)
2180 {
2181   return new_rd_And (db, current_ir_graph, current_ir_graph->current_block,
2182             op1, op2, mode);
2183 }
2184
2185 ir_node *
2186 new_d_Or (dbg_info* db, ir_node *op1, ir_node *op2, ir_mode *mode)
2187 {
2188   return new_rd_Or (db, current_ir_graph, current_ir_graph->current_block,
2189            op1, op2, mode);
2190 }
2191
2192 ir_node *
2193 new_d_Eor (dbg_info* db, ir_node *op1, ir_node *op2, ir_mode *mode)
2194 {
2195   return new_rd_Eor (db, current_ir_graph, current_ir_graph->current_block,
2196             op1, op2, mode);
2197 }
2198
2199 ir_node *
2200 new_d_Not (dbg_info* db, ir_node *op, ir_mode *mode)
2201 {
2202   return new_rd_Not (db, current_ir_graph, current_ir_graph->current_block,
2203             op, mode);
2204 }
2205
2206 ir_node *
2207 new_d_Shl (dbg_info* db, ir_node *op, ir_node *k, ir_mode *mode)
2208 {
2209   return new_rd_Shl (db, current_ir_graph, current_ir_graph->current_block,
2210             op, k, mode);
2211 }
2212
2213 ir_node *
2214 new_d_Shr (dbg_info* db, ir_node *op, ir_node *k, ir_mode *mode)
2215 {
2216   return new_rd_Shr (db, current_ir_graph, current_ir_graph->current_block,
2217             op, k, mode);
2218 }
2219
2220 ir_node *
2221 new_d_Shrs (dbg_info* db, ir_node *op, ir_node *k, ir_mode *mode)
2222 {
2223   return new_rd_Shrs (db, current_ir_graph, current_ir_graph->current_block,
2224              op, k, mode);
2225 }
2226
2227 ir_node *
2228 new_d_Rot (dbg_info* db, ir_node *op, ir_node *k, ir_mode *mode)
2229 {
2230   return new_rd_Rot (db, current_ir_graph, current_ir_graph->current_block,
2231              op, k, mode);
2232 }
2233
2234 ir_node *
2235 new_d_Abs (dbg_info* db, ir_node *op, ir_mode *mode)
2236 {
2237   return new_rd_Abs (db, current_ir_graph, current_ir_graph->current_block,
2238             op, mode);
2239 }
2240
2241 ir_node *
2242 new_d_Cmp (dbg_info* db, ir_node *op1, ir_node *op2)
2243 {
2244   return new_rd_Cmp (db, current_ir_graph, current_ir_graph->current_block,
2245             op1, op2);
2246 }
2247
2248 ir_node *
2249 new_d_Jmp (dbg_info* db)
2250 {
2251   return new_rd_Jmp (db, current_ir_graph, current_ir_graph->current_block);
2252 }
2253
2254 ir_node *
2255 new_d_IJmp (dbg_info* db, ir_node *tgt)
2256 {
2257   return new_rd_IJmp (db, current_ir_graph, current_ir_graph->current_block, tgt);
2258 }
2259
2260 ir_node *
2261 new_d_Cond (dbg_info* db, ir_node *c)
2262 {
2263   return new_rd_Cond (db, current_ir_graph, current_ir_graph->current_block, c);
2264 }
2265
2266 ir_node *
2267 new_d_Call (dbg_info* db, ir_node *store, ir_node *callee, int arity, ir_node **in,
2268       type *tp)
2269 {
2270   ir_node *res;
2271   res = new_rd_Call (db, current_ir_graph, current_ir_graph->current_block,
2272              store, callee, arity, in, tp);
2273 #if PRECISE_EXC_CONTEXT
2274   allocate_frag_arr(res, op_Call, &res->attr.call.exc.frag_arr);  /* Could be optimized away. */
2275 #endif
2276
2277   return res;
2278 }
2279
2280 ir_node *
2281 new_d_Return (dbg_info* db, ir_node* store, int arity, ir_node **in)
2282 {
2283   return new_rd_Return (db, current_ir_graph, current_ir_graph->current_block,
2284                store, arity, in);
2285 }
2286
2287 ir_node *
2288 new_d_Raise (dbg_info* db, ir_node *store, ir_node *obj)
2289 {
2290   return new_rd_Raise (db, current_ir_graph, current_ir_graph->current_block,
2291               store, obj);
2292 }
2293
2294 ir_node *
2295 new_d_Load (dbg_info* db, ir_node *store, ir_node *addr, ir_mode *mode)
2296 {
2297   ir_node *res;
2298   res = new_rd_Load (db, current_ir_graph, current_ir_graph->current_block,
2299              store, addr, mode);
2300 #if PRECISE_EXC_CONTEXT
2301   allocate_frag_arr(res, op_Load, &res->attr.load.exc.frag_arr);  /* Could be optimized away. */
2302 #endif
2303
2304   return res;
2305 }
2306
2307 ir_node *
2308 new_d_Store (dbg_info* db, ir_node *store, ir_node *addr, ir_node *val)
2309 {
2310   ir_node *res;
2311   res = new_rd_Store (db, current_ir_graph, current_ir_graph->current_block,
2312               store, addr, val);
2313 #if PRECISE_EXC_CONTEXT
2314   allocate_frag_arr(res, op_Store, &res->attr.store.exc.frag_arr);  /* Could be optimized away. */
2315 #endif
2316
2317   return res;
2318 }
2319
2320 ir_node *
2321 new_d_Alloc (dbg_info* db, ir_node *store, ir_node *size, type *alloc_type,
2322            where_alloc where)
2323 {
2324   ir_node *res;
2325   res = new_rd_Alloc (db, current_ir_graph, current_ir_graph->current_block,
2326               store, size, alloc_type, where);
2327 #if PRECISE_EXC_CONTEXT
2328   allocate_frag_arr(res, op_Alloc, &res->attr.a.exc.frag_arr);  /* Could be optimized away. */
2329 #endif
2330
2331   return res;
2332 }
2333
2334 ir_node *
2335 new_d_Free (dbg_info* db, ir_node *store, ir_node *ptr,
2336     ir_node *size, type *free_type, where_alloc where)
2337 {
2338   return new_rd_Free (db, current_ir_graph, current_ir_graph->current_block,
2339              store, ptr, size, free_type, where);
2340 }
2341
2342 ir_node *
2343 new_d_simpleSel (dbg_info* db, ir_node *store, ir_node *objptr, entity *ent)
2344 /* GL: objptr was called frame before.  Frame was a bad choice for the name
2345    as the operand could as well be a pointer to a dynamic object. */
2346 {
2347   return new_rd_Sel (db, current_ir_graph, current_ir_graph->current_block,
2348             store, objptr, 0, NULL, ent);
2349 }
2350
2351 ir_node *
2352 new_d_Sel (dbg_info* db, ir_node *store, ir_node *objptr, int n_index, ir_node **index, entity *sel)
2353 {
2354   return new_rd_Sel (db, current_ir_graph, current_ir_graph->current_block,
2355             store, objptr, n_index, index, sel);
2356 }
2357
2358 ir_node *
2359 new_d_InstOf (dbg_info *db, ir_node *store, ir_node *objptr, type *ent)
2360 {
2361   return (new_rd_InstOf (db, current_ir_graph, current_ir_graph->current_block,
2362                          store, objptr, ent));
2363 }
2364
2365 ir_node *
2366 new_d_SymConst_type (dbg_info* db, symconst_symbol value, symconst_kind kind, type *tp)
2367 {
2368   return new_rd_SymConst_type (db, current_ir_graph, current_ir_graph->start_block,
2369                          value, kind, tp);
2370 }
2371
2372 ir_node *
2373 new_d_SymConst (dbg_info* db, symconst_symbol value, symconst_kind kind)
2374 {
2375   return new_rd_SymConst (db, current_ir_graph, current_ir_graph->start_block,
2376                          value, kind);
2377 }
2378
2379 ir_node *
2380 new_d_Sync (dbg_info* db, int arity, ir_node** in)
2381 {
2382   return new_rd_Sync (db, current_ir_graph, current_ir_graph->current_block,
2383               arity, in);
2384 }
2385
2386
2387 ir_node *
2388 (new_d_Bad)(void)
2389 {
2390   return _new_d_Bad();
2391 }
2392
2393 ir_node *
2394 new_d_Confirm (dbg_info *db, ir_node *val, ir_node *bound, pn_Cmp cmp)
2395 {
2396   return new_rd_Confirm (db, current_ir_graph, current_ir_graph->current_block,
2397              val, bound, cmp);
2398 }
2399
2400 ir_node *
2401 new_d_Unknown (ir_mode *m)
2402 {
2403   return new_rd_Unknown(current_ir_graph, m);
2404 }
2405
2406 ir_node *
2407 new_d_CallBegin (dbg_info *db, ir_node *call)
2408 {
2409   ir_node *res;
2410   res = new_rd_CallBegin (db, current_ir_graph, current_ir_graph->current_block, call);
2411   return res;
2412 }
2413
2414 ir_node *
2415 new_d_EndReg (dbg_info *db)
2416 {
2417   ir_node *res;
2418   res = new_rd_EndReg(db, current_ir_graph, current_ir_graph->current_block);
2419   return res;
2420 }
2421
2422 ir_node *
2423 new_d_EndExcept (dbg_info *db)
2424 {
2425   ir_node *res;
2426   res = new_rd_EndExcept(db, current_ir_graph, current_ir_graph->current_block);
2427   return res;
2428 }
2429
2430 ir_node *
2431 new_d_Break (dbg_info *db)
2432 {
2433   return new_rd_Break (db, current_ir_graph, current_ir_graph->current_block);
2434 }
2435
2436 ir_node *
2437 new_d_Filter (dbg_info *db, ir_node *arg, ir_mode *mode, long proj)
2438 {
2439   return new_rd_Filter (db, current_ir_graph, current_ir_graph->current_block,
2440             arg, mode, proj);
2441 }
2442
2443 ir_node *
2444 (new_d_NoMem)(void)
2445 {
2446   return _new_d_NoMem();
2447 }
2448
2449 ir_node *
2450 new_d_Mux (dbg_info *db, ir_node *sel, ir_node *ir_false,
2451     ir_node *ir_true, ir_mode *mode) {
2452   return new_rd_Mux (db, current_ir_graph, current_ir_graph->current_block,
2453       sel, ir_false, ir_true, mode);
2454 }
2455
2456 /* ********************************************************************* */
2457 /* Comfortable interface with automatic Phi node construction.           */
2458 /* (Uses also constructors of ?? interface, except new_Block.            */
2459 /* ********************************************************************* */
2460
2461 /* * Block construction **/
2462 /* immature Block without predecessors */
2463 ir_node *new_d_immBlock (dbg_info* db) {
2464   ir_node *res;
2465
2466   assert(get_irg_phase_state (current_ir_graph) == phase_building);
2467   /* creates a new dynamic in-array as length of in is -1 */
2468   res = new_ir_node (db, current_ir_graph, NULL, op_Block, mode_BB, -1, NULL);
2469   current_ir_graph->current_block = res;
2470   res->attr.block.matured     = 0;
2471   res->attr.block.dead        = 0;
2472   /* res->attr.block.exc = exc_normal; */
2473   /* res->attr.block.handler_entry = 0; */
2474   res->attr.block.irg         = current_ir_graph;
2475   res->attr.block.backedge    = NULL;
2476   res->attr.block.in_cg       = NULL;
2477   res->attr.block.cg_backedge = NULL;
2478   set_Block_block_visited(res, 0);
2479
2480   /* Create and initialize array for Phi-node construction. */
2481   res->attr.block.graph_arr = NEW_ARR_D (ir_node *, current_ir_graph->obst,
2482                                          current_ir_graph->n_loc);
2483   memset(res->attr.block.graph_arr, 0, sizeof(ir_node *)*current_ir_graph->n_loc);
2484
2485   /* Immature block may not be optimized! */
2486   IRN_VRFY_IRG(res, current_ir_graph);
2487
2488   return res;
2489 }
2490
2491 ir_node *
2492 new_immBlock (void) {
2493   return new_d_immBlock(NULL);
2494 }
2495
2496 /* add an edge to a jmp/control flow node */
2497 void
2498 add_immBlock_pred (ir_node *block, ir_node *jmp)
2499 {
2500   if (block->attr.block.matured) {
2501     assert(0 && "Error: Block already matured!\n");
2502   }
2503   else {
2504     assert(jmp != NULL);
2505     ARR_APP1(ir_node *, block->in, jmp);
2506   }
2507 }
2508
2509 /* changing the current block */
2510 void
2511 set_cur_block (ir_node *target)
2512 {
2513   current_ir_graph->current_block = target;
2514 }
2515
2516 /* ************************ */
2517 /* parameter administration */
2518
2519 /* get a value from the parameter array from the current block by its index */
2520 ir_node *
2521 get_d_value (dbg_info* db, int pos, ir_mode *mode)
2522 {
2523   assert(get_irg_phase_state (current_ir_graph) == phase_building);
2524   inc_irg_visited(current_ir_graph);
2525
2526   return get_r_value_internal (current_ir_graph->current_block, pos + 1, mode);
2527 }
2528 /* get a value from the parameter array from the current block by its index */
2529 ir_node *
2530 get_value (int pos, ir_mode *mode)
2531 {
2532   return get_d_value(NULL, pos, mode);
2533 }
2534
2535 /* set a value at position pos in the parameter array from the current block */
2536 void
2537 set_value (int pos, ir_node *value)
2538 {
2539   assert(get_irg_phase_state (current_ir_graph) == phase_building);
2540   assert(pos+1 < current_ir_graph->n_loc);
2541   current_ir_graph->current_block->attr.block.graph_arr[pos + 1] = value;
2542 }
2543
2544 /* get the current store */
2545 ir_node *
2546 get_store (void)
2547 {
2548   assert(get_irg_phase_state (current_ir_graph) == phase_building);
2549   /* GL: one could call get_value instead */
2550   inc_irg_visited(current_ir_graph);
2551   return get_r_value_internal (current_ir_graph->current_block, 0, mode_M);
2552 }
2553
2554 /* set the current store */
2555 void
2556 set_store (ir_node *store)
2557 {
2558   /* GL: one could call set_value instead */
2559   assert(get_irg_phase_state (current_ir_graph) == phase_building);
2560   current_ir_graph->current_block->attr.block.graph_arr[0] = store;
2561 }
2562
2563 void
2564 keep_alive (ir_node *ka) {
2565   add_End_keepalive(current_ir_graph->end, ka);
2566 }
2567
2568 /** Useful access routines **/
2569 /* Returns the current block of the current graph.  To set the current
2570    block use set_cur_block. */
2571 ir_node *get_cur_block() {
2572   return get_irg_current_block(current_ir_graph);
2573 }
2574
2575 /* Returns the frame type of the current graph */
2576 type *get_cur_frame_type() {
2577   return get_irg_frame_type(current_ir_graph);
2578 }
2579
2580
2581 /* ********************************************************************* */
2582 /* initialize */
2583
2584 /* call once for each run of the library */
2585 void
2586 init_cons(uninitialized_local_variable_func_t *func)
2587 {
2588   default_initialize_local_variable = func;
2589 }
2590
2591 /* call for each graph */
2592 void
2593 irg_finalize_cons (ir_graph *irg) {
2594   irg->phase_state = phase_high;
2595 }
2596
2597 void
2598 irp_finalize_cons (void) {
2599   int i, n_irgs = get_irp_n_irgs();
2600   for (i = 0; i < n_irgs; i++) {
2601     irg_finalize_cons(get_irp_irg(i));
2602   }
2603   irp->phase_state = phase_high;\
2604 }
2605
2606
2607
2608
2609 ir_node *new_Block(int arity, ir_node **in) {
2610   return new_d_Block(NULL, arity, in);
2611 }
2612 ir_node *new_Start  (void) {
2613   return new_d_Start(NULL);
2614 }
2615 ir_node *new_End    (void) {
2616   return new_d_End(NULL);
2617 }
2618 ir_node *new_Jmp    (void) {
2619   return new_d_Jmp(NULL);
2620 }
2621 ir_node *new_IJmp   (ir_node *tgt) {
2622   return new_d_IJmp(NULL, tgt);
2623 }
2624 ir_node *new_Cond   (ir_node *c) {
2625   return new_d_Cond(NULL, c);
2626 }
2627 ir_node *new_Return (ir_node *store, int arity, ir_node *in[]) {
2628   return new_d_Return(NULL, store, arity, in);
2629 }
2630 ir_node *new_Raise  (ir_node *store, ir_node *obj) {
2631   return new_d_Raise(NULL, store, obj);
2632 }
2633 ir_node *new_Const  (ir_mode *mode, tarval *con) {
2634   return new_d_Const(NULL, mode, con);
2635 }
2636
2637 ir_node *new_Const_long(ir_mode *mode, long value)
2638 {
2639     return new_d_Const_long(NULL, mode, value);
2640 }
2641
2642 ir_node *new_Const_type(tarval *con, type *tp) {
2643   return new_d_Const_type(NULL, get_type_mode(tp), con, tp);
2644 }
2645
2646 ir_node *new_SymConst (symconst_symbol value, symconst_kind kind) {
2647   return new_d_SymConst(NULL, value, kind);
2648 }
2649 ir_node *new_simpleSel(ir_node *store, ir_node *objptr, entity *ent) {
2650   return new_d_simpleSel(NULL, store, objptr, ent);
2651 }
2652 ir_node *new_Sel    (ir_node *store, ir_node *objptr, int arity, ir_node **in,
2653                      entity *ent) {
2654   return new_d_Sel(NULL, store, objptr, arity, in, ent);
2655 }
2656 ir_node *new_InstOf (ir_node *store, ir_node *objptr, type *ent) {
2657   return new_d_InstOf (NULL, store, objptr, ent);
2658 }
2659 ir_node *new_Call   (ir_node *store, ir_node *callee, int arity, ir_node **in,
2660              type *tp) {
2661   return new_d_Call(NULL, store, callee, arity, in, tp);
2662 }
2663 ir_node *new_Add    (ir_node *op1, ir_node *op2, ir_mode *mode) {
2664   return new_d_Add(NULL, op1, op2, mode);
2665 }
2666 ir_node *new_Sub    (ir_node *op1, ir_node *op2, ir_mode *mode) {
2667   return new_d_Sub(NULL, op1, op2, mode);
2668 }
2669 ir_node *new_Minus  (ir_node *op,  ir_mode *mode) {
2670   return new_d_Minus(NULL, op, mode);
2671 }
2672 ir_node *new_Mul    (ir_node *op1, ir_node *op2, ir_mode *mode) {
2673   return new_d_Mul(NULL, op1, op2, mode);
2674 }
2675 ir_node *new_Quot   (ir_node *memop, ir_node *op1, ir_node *op2) {
2676   return new_d_Quot(NULL, memop, op1, op2);
2677 }
2678 ir_node *new_DivMod (ir_node *memop, ir_node *op1, ir_node *op2) {
2679   return new_d_DivMod(NULL, memop, op1, op2);
2680 }
2681 ir_node *new_Div    (ir_node *memop, ir_node *op1, ir_node *op2) {
2682   return new_d_Div(NULL, memop, op1, op2);
2683 }
2684 ir_node *new_Mod    (ir_node *memop, ir_node *op1, ir_node *op2) {
2685   return new_d_Mod(NULL, memop, op1, op2);
2686 }
2687 ir_node *new_Abs    (ir_node *op, ir_mode *mode) {
2688   return new_d_Abs(NULL, op, mode);
2689 }
2690 ir_node *new_And    (ir_node *op1, ir_node *op2, ir_mode *mode) {
2691   return new_d_And(NULL, op1, op2, mode);
2692 }
2693 ir_node *new_Or     (ir_node *op1, ir_node *op2, ir_mode *mode) {
2694   return new_d_Or(NULL, op1, op2, mode);
2695 }
2696 ir_node *new_Eor    (ir_node *op1, ir_node *op2, ir_mode *mode) {
2697   return new_d_Eor(NULL, op1, op2, mode);
2698 }
2699 ir_node *new_Not    (ir_node *op,                ir_mode *mode) {
2700   return new_d_Not(NULL, op, mode);
2701 }
2702 ir_node *new_Shl    (ir_node *op,  ir_node *k,   ir_mode *mode) {
2703   return new_d_Shl(NULL, op, k, mode);
2704 }
2705 ir_node *new_Shr    (ir_node *op,  ir_node *k,   ir_mode *mode) {
2706   return new_d_Shr(NULL, op, k, mode);
2707 }
2708 ir_node *new_Shrs   (ir_node *op,  ir_node *k,   ir_mode *mode) {
2709   return new_d_Shrs(NULL, op, k, mode);
2710 }
2711 #define new_Rotate new_Rot
2712 ir_node *new_Rot    (ir_node *op,  ir_node *k,   ir_mode *mode) {
2713   return new_d_Rot(NULL, op, k, mode);
2714 }
2715 ir_node *new_Cmp    (ir_node *op1, ir_node *op2) {
2716   return new_d_Cmp(NULL, op1, op2);
2717 }
2718 ir_node *new_Conv   (ir_node *op, ir_mode *mode) {
2719   return new_d_Conv(NULL, op, mode);
2720 }
2721 ir_node *new_Cast   (ir_node *op, type *to_tp) {
2722   return new_d_Cast(NULL, op, to_tp);
2723 }
2724 ir_node *new_Phi    (int arity, ir_node **in, ir_mode *mode) {
2725   return new_d_Phi(NULL, arity, in, mode);
2726 }
2727 ir_node *new_Load   (ir_node *store, ir_node *addr, ir_mode *mode) {
2728   return new_d_Load(NULL, store, addr, mode);
2729 }
2730 ir_node *new_Store  (ir_node *store, ir_node *addr, ir_node *val) {
2731   return new_d_Store(NULL, store, addr, val);
2732 }
2733 ir_node *new_Alloc  (ir_node *store, ir_node *size, type *alloc_type,
2734                      where_alloc where) {
2735   return new_d_Alloc(NULL, store, size, alloc_type, where);
2736 }
2737 ir_node *new_Free   (ir_node *store, ir_node *ptr, ir_node *size,
2738              type *free_type, where_alloc where) {
2739   return new_d_Free(NULL, store, ptr, size, free_type, where);
2740 }
2741 ir_node *new_Sync   (int arity, ir_node **in) {
2742   return new_d_Sync(NULL, arity, in);
2743 }
2744 ir_node *new_Proj   (ir_node *arg, ir_mode *mode, long proj) {
2745   return new_d_Proj(NULL, arg, mode, proj);
2746 }
2747 ir_node *new_defaultProj (ir_node *arg, long max_proj) {
2748   return new_d_defaultProj(NULL, arg, max_proj);
2749 }
2750 ir_node *new_Tuple  (int arity, ir_node **in) {
2751   return new_d_Tuple(NULL, arity, in);
2752 }
2753 ir_node *new_Id     (ir_node *val, ir_mode *mode) {
2754   return new_d_Id(NULL, val, mode);
2755 }
2756 ir_node *new_Bad    (void) {
2757   return new_d_Bad();
2758 }
2759 ir_node *new_Confirm (ir_node *val, ir_node *bound, pn_Cmp cmp) {
2760   return new_d_Confirm (NULL, val, bound, cmp);
2761 }
2762 ir_node *new_Unknown(ir_mode *m) {
2763   return new_d_Unknown(m);
2764 }
2765 ir_node *new_CallBegin (ir_node *callee) {
2766   return new_d_CallBegin(NULL, callee);
2767 }
2768 ir_node *new_EndReg (void) {
2769   return new_d_EndReg(NULL);
2770 }
2771 ir_node *new_EndExcept (void) {
2772   return new_d_EndExcept(NULL);
2773 }
2774 ir_node *new_Break  (void) {
2775   return new_d_Break(NULL);
2776 }
2777 ir_node *new_Filter (ir_node *arg, ir_mode *mode, long proj) {
2778   return new_d_Filter(NULL, arg, mode, proj);
2779 }
2780 ir_node *new_NoMem  (void) {
2781   return new_d_NoMem();
2782 }
2783 ir_node *new_Mux (ir_node *sel, ir_node *ir_false, ir_node *ir_true, ir_mode *mode) {
2784   return new_d_Mux(NULL, sel, ir_false, ir_true, mode);
2785 }