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