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