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