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