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