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