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