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