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