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