newest biset from libcore
[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 initialization variable
62  */
63 static uninitialized_local_variable_func_t *default_initialize_local_variable = NULL;
64
65 /* --------------------------------------------- */
66 /* private interfaces, for professional use only */
67 /* --------------------------------------------- */
68
69 /* Constructs a Block with a fixed number of predecessors.
70    Does not set current_block.  Can not be used with automatic
71    Phi node construction. */
72 ir_node *
73 new_rd_Block (dbg_info* db, ir_graph *irg,  int arity, ir_node **in)
74 {
75   ir_node *res;
76
77   res = new_ir_node (db, irg, NULL, op_Block, mode_BB, arity, in);
78   set_Block_matured(res, 1);
79   set_Block_block_visited(res, 0);
80
81   /* res->attr.block.exc = exc_normal; */
82   /* res->attr.block.handler_entry = 0; */
83   res->attr.block.dead        = 0;
84   res->attr.block.irg         = irg;
85   res->attr.block.backedge    = new_backedge_arr(irg->obst, arity);
86   res->attr.block.in_cg       = NULL;
87   res->attr.block.cg_backedge = NULL;
88   res->attr.block.extblk      = NULL;
89
90   IRN_VRFY_IRG(res, irg);
91   return res;
92 }
93
94 ir_node *
95 new_rd_Start (dbg_info* db, ir_graph *irg, ir_node *block)
96 {
97   ir_node *res;
98
99   res = new_ir_node(db, irg, block, op_Start, mode_T, 0, NULL);
100   /* res->attr.start.irg = irg; */
101
102   IRN_VRFY_IRG(res, irg);
103   return res;
104 }
105
106 ir_node *
107 new_rd_End (dbg_info* db, ir_graph *irg, ir_node *block)
108 {
109   ir_node *res;
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 ir_node *
120 new_rd_Phi (dbg_info* db, ir_graph *irg, ir_node *block, int arity, ir_node **in, ir_mode *mode)
121 {
122   ir_node *res;
123   int i;
124   bool has_unknown = false;
125
126   /* Don't assert that block matured: the use of this constructor is strongly
127      restricted ... */
128   if ( get_Block_matured(block) )
129     assert( get_irn_arity(block) == arity );
130
131   res = new_ir_node(db, irg, block, op_Phi, mode, arity, in);
132
133   res->attr.phi_backedge = new_backedge_arr(irg->obst, arity);
134
135   for (i = arity-1; i >= 0; i--)
136     if (get_irn_op(in[i]) == op_Unknown) {
137       has_unknown = true;
138       break;
139     }
140
141   if (!has_unknown) res = optimize_node (res);
142   IRN_VRFY_IRG(res, irg);
143
144   /* Memory Phis in endless loops must be kept alive.
145      As we can't distinguish these easily we keep all of them alive. */
146   if ((res->op == op_Phi) && (mode == mode_M))
147     add_End_keepalive(irg->end, res);
148   return res;
149 }
150
151 ir_node *
152 new_rd_Const_type (dbg_info* db, ir_graph *irg, ir_node *block, ir_mode *mode, tarval *con, type *tp)
153 {
154   ir_node *res;
155
156   res = new_ir_node (db, irg, irg->start_block, op_Const, mode, 0, NULL);
157   res->attr.con.tv = con;
158   set_Const_type(res, tp);  /* Call method because of complex assertion. */
159   res = optimize_node (res);
160   assert(get_Const_type(res) == tp);
161   IRN_VRFY_IRG(res, irg);
162
163   return res;
164 }
165
166 ir_node *
167 new_rd_Const (dbg_info* db, ir_graph *irg, ir_node *block, ir_mode *mode, tarval *con)
168 {
169   return new_rd_Const_type (db, irg, block, mode, con, firm_unknown_type);
170 }
171
172 ir_node *
173 new_rd_Const_long (dbg_info* db, ir_graph *irg, ir_node *block, ir_mode *mode, long value)
174 {
175     return new_rd_Const(db, irg, block, mode, new_tarval_from_long(value, mode));
176 }
177
178 ir_node *
179 new_rd_Id (dbg_info* db, ir_graph *irg, ir_node *block, ir_node *val, ir_mode *mode)
180 {
181   ir_node *res;
182
183   res = new_ir_node(db, irg, block, op_Id, mode, 1, &val);
184   res = optimize_node(res);
185   IRN_VRFY_IRG(res, irg);
186   return res;
187 }
188
189 ir_node *
190 new_rd_Proj (dbg_info* db, ir_graph *irg, ir_node *block, ir_node *arg, ir_mode *mode,
191         long proj)
192 {
193   ir_node *res;
194
195   res = new_ir_node (db, irg, block, op_Proj, mode, 1, &arg);
196   res->attr.proj = proj;
197
198   assert(res);
199   assert(get_Proj_pred(res));
200   assert(get_nodes_block(get_Proj_pred(res)));
201
202   res = optimize_node(res);
203
204   IRN_VRFY_IRG(res, irg);
205   return res;
206
207 }
208
209 ir_node *
210 new_rd_defaultProj (dbg_info* db, ir_graph *irg, ir_node *block, ir_node *arg,
211            long max_proj)
212 {
213   ir_node *res;
214   assert(arg->op == op_Cond);
215   arg->attr.c.kind = fragmentary;
216   arg->attr.c.default_proj = max_proj;
217   res = new_rd_Proj (db, irg, block, arg, mode_X, max_proj);
218   return res;
219 }
220
221 ir_node *
222 new_rd_Conv (dbg_info* db, ir_graph *irg, ir_node *block, ir_node *op, ir_mode *mode)
223 {
224   ir_node *res;
225
226   res = new_ir_node(db, irg, block, op_Conv, mode, 1, &op);
227   res = optimize_node(res);
228   IRN_VRFY_IRG(res, irg);
229   return res;
230 }
231
232 ir_node *
233 new_rd_Cast (dbg_info* db, ir_graph *irg, ir_node *block, ir_node *op, type *to_tp)
234 {
235   ir_node *res;
236
237   assert(is_atomic_type(to_tp));
238
239   res = new_ir_node(db, irg, block, op_Cast, get_irn_mode(op), 1, &op);
240   res->attr.cast.totype = to_tp;
241   res = optimize_node(res);
242   IRN_VRFY_IRG(res, irg);
243   return res;
244 }
245
246 ir_node *
247 new_rd_Tuple (dbg_info* db, ir_graph *irg, ir_node *block, int arity, ir_node **in)
248 {
249   ir_node *res;
250
251   res = new_ir_node(db, irg, block, op_Tuple, mode_T, arity, in);
252   res = optimize_node (res);
253   IRN_VRFY_IRG(res, irg);
254   return res;
255 }
256
257 ir_node *
258 new_rd_Add (dbg_info* db, ir_graph *irg, ir_node *block,
259        ir_node *op1, ir_node *op2, ir_mode *mode)
260 {
261   ir_node *in[2];
262   ir_node *res;
263
264   in[0] = op1;
265   in[1] = op2;
266   res = new_ir_node(db, irg, block, op_Add, mode, 2, in);
267   res = optimize_node(res);
268   IRN_VRFY_IRG(res, irg);
269   return res;
270 }
271
272 ir_node *
273 new_rd_Sub (dbg_info* db, ir_graph *irg, ir_node *block,
274        ir_node *op1, ir_node *op2, ir_mode *mode)
275 {
276   ir_node *in[2];
277   ir_node *res;
278
279   in[0] = op1;
280   in[1] = op2;
281   res = new_ir_node (db, irg, block, op_Sub, mode, 2, in);
282   res = optimize_node (res);
283   IRN_VRFY_IRG(res, irg);
284   return res;
285 }
286
287 ir_node *
288 new_rd_Minus (dbg_info* db, ir_graph *irg, ir_node *block,
289               ir_node *op, ir_mode *mode)
290 {
291   ir_node *res;
292
293   res = new_ir_node(db, irg, block, op_Minus, mode, 1, &op);
294   res = optimize_node(res);
295   IRN_VRFY_IRG(res, irg);
296   return res;
297 }
298
299 ir_node *
300 new_rd_Mul (dbg_info* db, ir_graph *irg, ir_node *block,
301        ir_node *op1, ir_node *op2, ir_mode *mode)
302 {
303   ir_node *in[2];
304   ir_node *res;
305
306   in[0] = op1;
307   in[1] = op2;
308   res = new_ir_node(db, irg, block, op_Mul, mode, 2, in);
309   res = optimize_node(res);
310   IRN_VRFY_IRG(res, irg);
311   return res;
312 }
313
314 ir_node *
315 new_rd_Quot (dbg_info* db, ir_graph *irg, ir_node *block,
316             ir_node *memop, ir_node *op1, ir_node *op2)
317 {
318   ir_node *in[3];
319   ir_node *res;
320
321   in[0] = memop;
322   in[1] = op1;
323   in[2] = op2;
324   res = new_ir_node(db, irg, block, op_Quot, mode_T, 3, in);
325   res = optimize_node(res);
326   IRN_VRFY_IRG(res, irg);
327   return res;
328 }
329
330 ir_node *
331 new_rd_DivMod (dbg_info* db, ir_graph *irg, ir_node *block,
332           ir_node *memop, ir_node *op1, ir_node *op2)
333 {
334   ir_node *in[3];
335   ir_node *res;
336
337   in[0] = memop;
338   in[1] = op1;
339   in[2] = op2;
340   res = new_ir_node(db, irg, block, op_DivMod, mode_T, 3, in);
341   res = optimize_node(res);
342   IRN_VRFY_IRG(res, irg);
343   return res;
344 }
345
346 ir_node *
347 new_rd_Div (dbg_info* db, ir_graph *irg, ir_node *block,
348            ir_node *memop, ir_node *op1, ir_node *op2)
349 {
350   ir_node *in[3];
351   ir_node *res;
352
353   in[0] = memop;
354   in[1] = op1;
355   in[2] = op2;
356   res = new_ir_node(db, irg, block, op_Div, mode_T, 3, in);
357   res = optimize_node(res);
358   IRN_VRFY_IRG(res, irg);
359   return res;
360 }
361
362 ir_node *
363 new_rd_Mod (dbg_info* db, ir_graph *irg, ir_node *block,
364            ir_node *memop, ir_node *op1, ir_node *op2)
365 {
366   ir_node *in[3];
367   ir_node *res;
368
369   in[0] = memop;
370   in[1] = op1;
371   in[2] = op2;
372   res = new_ir_node(db, irg, block, op_Mod, mode_T, 3, in);
373   res = optimize_node(res);
374   IRN_VRFY_IRG(res, irg);
375   return res;
376 }
377
378 ir_node *
379 new_rd_And (dbg_info* db, ir_graph *irg, ir_node *block,
380            ir_node *op1, ir_node *op2, ir_mode *mode)
381 {
382   ir_node *in[2];
383   ir_node *res;
384
385   in[0] = op1;
386   in[1] = op2;
387   res = new_ir_node(db, irg, block, op_And, mode, 2, in);
388   res = optimize_node(res);
389   IRN_VRFY_IRG(res, irg);
390   return res;
391 }
392
393 ir_node *
394 new_rd_Or (dbg_info* db, ir_graph *irg, ir_node *block,
395           ir_node *op1, ir_node *op2, ir_mode *mode)
396 {
397   ir_node *in[2];
398   ir_node *res;
399
400   in[0] = op1;
401   in[1] = op2;
402   res = new_ir_node(db, irg, block, op_Or, mode, 2, in);
403   res = optimize_node(res);
404   IRN_VRFY_IRG(res, irg);
405   return res;
406 }
407
408 ir_node *
409 new_rd_Eor (dbg_info* db, ir_graph *irg, ir_node *block,
410           ir_node *op1, ir_node *op2, ir_mode *mode)
411 {
412   ir_node *in[2];
413   ir_node *res;
414
415   in[0] = op1;
416   in[1] = op2;
417   res = new_ir_node (db, irg, block, op_Eor, mode, 2, in);
418   res = optimize_node (res);
419   IRN_VRFY_IRG(res, irg);
420   return res;
421 }
422
423 ir_node *
424 new_rd_Not    (dbg_info* db, ir_graph *irg, ir_node *block,
425           ir_node *op, ir_mode *mode)
426 {
427   ir_node *res;
428
429   res = new_ir_node(db, irg, block, op_Not, mode, 1, &op);
430   res = optimize_node(res);
431   IRN_VRFY_IRG(res, irg);
432   return res;
433 }
434
435 ir_node *
436 new_rd_Shl (dbg_info* db, ir_graph *irg, ir_node *block,
437           ir_node *op, ir_node *k, ir_mode *mode)
438 {
439   ir_node *in[2];
440   ir_node *res;
441
442   in[0] = op;
443   in[1] = k;
444   res = new_ir_node(db, irg, block, op_Shl, mode, 2, in);
445   res = optimize_node(res);
446   IRN_VRFY_IRG(res, irg);
447   return res;
448 }
449
450 ir_node *
451 new_rd_Shr (dbg_info* db, ir_graph *irg, ir_node *block,
452        ir_node *op, ir_node *k, ir_mode *mode)
453 {
454   ir_node *in[2];
455   ir_node *res;
456
457   in[0] = op;
458   in[1] = k;
459   res = new_ir_node(db, irg, block, op_Shr, mode, 2, in);
460   res = optimize_node(res);
461   IRN_VRFY_IRG(res, irg);
462   return res;
463 }
464
465 ir_node *
466 new_rd_Shrs (dbg_info* db, ir_graph *irg, ir_node *block,
467        ir_node *op, ir_node *k, ir_mode *mode)
468 {
469   ir_node *in[2];
470   ir_node *res;
471
472   in[0] = op;
473   in[1] = k;
474   res = new_ir_node(db, irg, block, op_Shrs, mode, 2, in);
475   res = optimize_node(res);
476   IRN_VRFY_IRG(res, irg);
477   return res;
478 }
479
480 ir_node *
481 new_rd_Rot (dbg_info* db, ir_graph *irg, ir_node *block,
482        ir_node *op, ir_node *k, ir_mode *mode)
483 {
484   ir_node *in[2];
485   ir_node *res;
486
487   in[0] = op;
488   in[1] = k;
489   res = new_ir_node(db, irg, block, op_Rot, mode, 2, in);
490   res = optimize_node(res);
491   IRN_VRFY_IRG(res, irg);
492   return res;
493 }
494
495 ir_node *
496 new_rd_Abs (dbg_info* db, ir_graph *irg, ir_node *block,
497        ir_node *op, ir_mode *mode)
498 {
499   ir_node *res;
500
501   res = new_ir_node(db, irg, block, op_Abs, mode, 1, &op);
502   res = optimize_node (res);
503   IRN_VRFY_IRG(res, irg);
504   return res;
505 }
506
507 ir_node *
508 new_rd_Cmp (dbg_info* db, ir_graph *irg, ir_node *block,
509        ir_node *op1, ir_node *op2)
510 {
511   ir_node *in[2];
512   ir_node *res;
513   in[0] = op1;
514   in[1] = op2;
515
516   res = new_ir_node(db, irg, block, op_Cmp, mode_T, 2, in);
517   res = optimize_node(res);
518   IRN_VRFY_IRG(res, irg);
519   return res;
520 }
521
522 ir_node *
523 new_rd_Jmp (dbg_info* db, ir_graph *irg, ir_node *block)
524 {
525   ir_node *res;
526
527   res = new_ir_node (db, irg, block, op_Jmp, mode_X, 0, NULL);
528   res = optimize_node (res);
529   IRN_VRFY_IRG (res, irg);
530   return res;
531 }
532
533 ir_node *
534 new_rd_IJmp (dbg_info* db, ir_graph *irg, ir_node *block, ir_node *tgt)
535 {
536   ir_node *res;
537
538   res = new_ir_node (db, irg, block, op_IJmp, mode_X, 1, &tgt);
539   res = optimize_node (res);
540   IRN_VRFY_IRG (res, irg);
541
542   if (get_irn_op(res) == op_IJmp) /* still an IJmp */
543     keep_alive(res);
544   return res;
545 }
546
547 ir_node *
548 new_rd_Cond (dbg_info* db, ir_graph *irg, ir_node *block, ir_node *c)
549 {
550   ir_node *res;
551
552   res = new_ir_node (db, irg, block, op_Cond, mode_T, 1, &c);
553   res->attr.c.kind         = dense;
554   res->attr.c.default_proj = 0;
555   res = optimize_node (res);
556   IRN_VRFY_IRG(res, irg);
557   return res;
558 }
559
560 ir_node *
561 new_rd_Call (dbg_info* db, ir_graph *irg, ir_node *block, ir_node *store,
562         ir_node *callee, int arity, ir_node **in, type *tp)
563 {
564   ir_node **r_in;
565   ir_node *res;
566   int r_arity;
567
568   r_arity = arity+2;
569   NEW_ARR_A(ir_node *, r_in, r_arity);
570   r_in[0] = store;
571   r_in[1] = callee;
572   memcpy(&r_in[2], in, sizeof(ir_node *) * arity);
573
574   res = new_ir_node(db, irg, block, op_Call, mode_T, r_arity, r_in);
575
576   assert((get_unknown_type() == tp) || is_Method_type(tp));
577   set_Call_type(res, tp);
578   res->attr.call.exc.pin_state = op_pin_state_pinned;
579   res->attr.call.callee_arr    = NULL;
580   res = optimize_node(res);
581   IRN_VRFY_IRG(res, irg);
582   return res;
583 }
584
585 ir_node *
586 new_rd_Return (dbg_info* db, ir_graph *irg, ir_node *block,
587               ir_node *store, int arity, ir_node **in)
588 {
589   ir_node **r_in;
590   ir_node *res;
591   int r_arity;
592
593   r_arity = arity+1;
594   NEW_ARR_A (ir_node *, r_in, r_arity);
595   r_in[0] = store;
596   memcpy(&r_in[1], in, sizeof(ir_node *) * arity);
597   res = new_ir_node(db, irg, block, op_Return, mode_X, r_arity, r_in);
598   res = optimize_node(res);
599   IRN_VRFY_IRG(res, irg);
600   return res;
601 }
602
603 ir_node *
604 new_rd_Raise (dbg_info* db, ir_graph *irg, ir_node *block, ir_node *store, ir_node *obj)
605 {
606   ir_node *in[2];
607   ir_node *res;
608
609   in[0] = store;
610   in[1] = obj;
611   res = new_ir_node(db, irg, block, op_Raise, mode_T, 2, in);
612   res = optimize_node(res);
613   IRN_VRFY_IRG(res, irg);
614   return res;
615 }
616
617 ir_node *
618 new_rd_Load (dbg_info* db, ir_graph *irg, ir_node *block,
619         ir_node *store, ir_node *adr, ir_mode *mode)
620 {
621   ir_node *in[2];
622   ir_node *res;
623
624   in[0] = store;
625   in[1] = adr;
626   res = new_ir_node(db, irg, block, op_Load, mode_T, 2, in);
627   res->attr.load.exc.pin_state = op_pin_state_pinned;
628   res->attr.load.load_mode     = mode;
629   res->attr.load.volatility    = volatility_non_volatile;
630   res = optimize_node(res);
631   IRN_VRFY_IRG(res, irg);
632   return res;
633 }
634
635 ir_node *
636 new_rd_Store (dbg_info* db, ir_graph *irg, ir_node *block,
637          ir_node *store, ir_node *adr, ir_node *val)
638 {
639   ir_node *in[3];
640   ir_node *res;
641
642   in[0] = store;
643   in[1] = adr;
644   in[2] = val;
645   res = new_ir_node(db, irg, block, op_Store, mode_T, 3, in);
646   res->attr.store.exc.pin_state = op_pin_state_pinned;
647   res->attr.store.volatility    = volatility_non_volatile;
648   res = optimize_node(res);
649   IRN_VRFY_IRG(res, irg);
650   return res;
651 }
652
653 ir_node *
654 new_rd_Alloc (dbg_info* db, ir_graph *irg, ir_node *block, ir_node *store,
655         ir_node *size, type *alloc_type, where_alloc where)
656 {
657   ir_node *in[2];
658   ir_node *res;
659
660   in[0] = store;
661   in[1] = size;
662   res = new_ir_node(db, irg, block, op_Alloc, mode_T, 2, in);
663   res->attr.a.exc.pin_state = op_pin_state_pinned;
664   res->attr.a.where         = where;
665   res->attr.a.type          = alloc_type;
666   res = optimize_node(res);
667   IRN_VRFY_IRG(res, irg);
668   return res;
669 }
670
671 ir_node *
672 new_rd_Free (dbg_info* db, ir_graph *irg, ir_node *block, ir_node *store,
673         ir_node *ptr, ir_node *size, type *free_type, where_alloc where)
674 {
675   ir_node *in[3];
676   ir_node *res;
677
678   in[0] = store;
679   in[1] = ptr;
680   in[2] = size;
681   res = new_ir_node (db, irg, block, op_Free, mode_M, 3, in);
682   res->attr.f.where = where;
683   res->attr.f.type  = free_type;
684   res = optimize_node(res);
685   IRN_VRFY_IRG(res, irg);
686   return res;
687 }
688
689 ir_node *
690 new_rd_Sel (dbg_info* db, ir_graph *irg, ir_node *block, ir_node *store, ir_node *objptr,
691            int arity, ir_node **in, entity *ent)
692 {
693   ir_node **r_in;
694   ir_node *res;
695   int r_arity;
696
697   assert(ent != NULL && is_entity(ent) && "entity expected in Sel construction");
698
699   r_arity = arity + 2;
700   NEW_ARR_A(ir_node *, r_in, r_arity);  /* uses alloca */
701   r_in[0] = store;
702   r_in[1] = objptr;
703   memcpy(&r_in[2], in, sizeof(ir_node *) * arity);
704   res = new_ir_node(db, irg, block, op_Sel, mode_P_mach, r_arity, r_in);
705   res->attr.s.ent = ent;
706   res = optimize_node(res);
707   IRN_VRFY_IRG(res, irg);
708   return res;
709 }
710
711 ir_node *
712 new_rd_InstOf (dbg_info *db, ir_graph *irg, ir_node *block, ir_node *store,
713            ir_node *objptr, type *ent)
714 {
715   ir_node **r_in;
716   ir_node *res;
717   int r_arity;
718
719   r_arity = 2;
720   NEW_ARR_A(ir_node *, r_in, r_arity);
721   r_in[0] = store;
722   r_in[1] = objptr;
723
724   res = new_ir_node(db, irg, block, op_Sel, mode_T, r_arity, r_in);
725   res->attr.io.ent = ent;
726
727   /* res = optimize(res); */
728   IRN_VRFY_IRG(res, irg);
729   return res;
730 }
731
732 ir_node *
733 new_rd_SymConst_type (dbg_info* db, ir_graph *irg, ir_node *block, symconst_symbol value,
734               symconst_kind symkind, type *tp) {
735   ir_node *res;
736   ir_mode *mode;
737
738   if ((symkind == symconst_addr_name) || (symkind == symconst_addr_ent))
739     mode = mode_P_mach;
740   else
741     mode = mode_Iu;
742
743   res = new_ir_node(db, irg, block, op_SymConst, mode, 0, NULL);
744
745   res->attr.i.num = symkind;
746   res->attr.i.sym = value;
747   res->attr.i.tp  = tp;
748
749   res = optimize_node(res);
750   IRN_VRFY_IRG(res, irg);
751   return res;
752 }
753
754 ir_node *
755 new_rd_SymConst (dbg_info* db, ir_graph *irg, ir_node *block, symconst_symbol value,
756          symconst_kind symkind)
757 {
758   ir_node *res = new_rd_SymConst_type(db, irg, block, value, symkind, firm_unknown_type);
759   return res;
760 }
761
762 ir_node *new_rd_SymConst_addr_ent (dbg_info *db, ir_graph *irg, entity *symbol, type *tp) {
763   symconst_symbol sym = {(type *)symbol};
764   return new_rd_SymConst_type(db, irg, irg->start_block, sym, symconst_addr_ent, tp);
765 }
766
767 ir_node *new_rd_SymConst_addr_name (dbg_info *db, ir_graph *irg, ident *symbol, type *tp) {
768   symconst_symbol sym = {(type *)symbol};
769   return new_rd_SymConst_type(db, irg, irg->start_block, sym, symconst_addr_name, tp);
770 }
771
772 ir_node *new_rd_SymConst_type_tag (dbg_info *db, ir_graph *irg, type *symbol, type *tp) {
773   symconst_symbol sym = {symbol};
774   return new_rd_SymConst_type(db, irg, irg->start_block, sym, symconst_type_tag, tp);
775 }
776
777 ir_node *new_rd_SymConst_size (dbg_info *db, ir_graph *irg, type *symbol, type *tp) {
778   symconst_symbol sym = {symbol};
779   return new_rd_SymConst_type(db, irg, irg->start_block, sym, symconst_size, tp);
780 }
781
782 ir_node *
783 new_rd_Sync (dbg_info* db, ir_graph *irg, ir_node *block, int arity, ir_node **in)
784 {
785   ir_node *res;
786
787   res = new_ir_node(db, irg, block, op_Sync, mode_M, arity, in);
788   res = optimize_node(res);
789   IRN_VRFY_IRG(res, irg);
790   return res;
791 }
792
793 ir_node *
794 new_rd_Bad (ir_graph *irg)
795 {
796   return irg->bad;
797 }
798
799 ir_node *
800 new_rd_Confirm (dbg_info *db, ir_graph *irg, ir_node *block, ir_node *val, ir_node *bound, pn_Cmp cmp)
801 {
802   ir_node *in[2], *res;
803
804   in[0] = val;
805   in[1] = bound;
806   res = new_ir_node (db, irg, block, op_Confirm, get_irn_mode(val), 2, in);
807   res->attr.confirm_cmp = cmp;
808   res = optimize_node (res);
809   IRN_VRFY_IRG(res, irg);
810   return res;
811 }
812
813 ir_node *
814 new_rd_Unknown (ir_graph *irg, ir_mode *m)
815 {
816   return new_ir_node(NULL, irg, irg->start_block, op_Unknown, m, 0, NULL);
817 }
818
819 ir_node *
820 new_rd_CallBegin (dbg_info *db, ir_graph *irg, ir_node *block, ir_node *call)
821 {
822   ir_node *in[1];
823   ir_node *res;
824
825   in[0] = get_Call_ptr(call);
826   res = new_ir_node(db, irg, block, op_CallBegin, mode_T, 1, in);
827   /* res->attr.callbegin.irg = irg; */
828   res->attr.callbegin.call = call;
829   res = optimize_node(res);
830   IRN_VRFY_IRG(res, irg);
831   return res;
832 }
833
834 ir_node *
835 new_rd_EndReg (dbg_info *db, ir_graph *irg, ir_node *block)
836 {
837   ir_node *res;
838
839   res = new_ir_node(db, irg, block, op_EndReg, mode_T, -1, NULL);
840   irg->end_reg = res;
841   IRN_VRFY_IRG(res, irg);
842   return res;
843 }
844
845 ir_node *
846 new_rd_EndExcept (dbg_info *db, ir_graph *irg, ir_node *block)
847 {
848   ir_node *res;
849
850   res = new_ir_node(db, irg, block, op_EndExcept, mode_T, -1, NULL);
851   irg->end_except = res;
852   IRN_VRFY_IRG (res, irg);
853   return res;
854 }
855
856 ir_node *
857 new_rd_Break (dbg_info *db, ir_graph *irg, ir_node *block)
858 {
859   ir_node *res;
860
861   res = new_ir_node(db, irg, block, op_Break, mode_X, 0, NULL);
862   res = optimize_node(res);
863   IRN_VRFY_IRG(res, irg);
864   return res;
865 }
866
867 ir_node *
868 new_rd_Filter (dbg_info *db, ir_graph *irg, ir_node *block, ir_node *arg, ir_mode *mode,
869            long proj)
870 {
871   ir_node *res;
872
873   res = new_ir_node(db, irg, block, op_Filter, mode, 1, &arg);
874   res->attr.filter.proj = proj;
875   res->attr.filter.in_cg = NULL;
876   res->attr.filter.backedge = NULL;
877
878   assert(res);
879   assert(get_Proj_pred(res));
880   assert(get_nodes_block(get_Proj_pred(res)));
881
882   res = optimize_node(res);
883   IRN_VRFY_IRG(res, irg);
884   return res;
885 }
886
887 ir_node *
888 new_rd_NoMem (ir_graph *irg) {
889   return irg->no_mem;
890 }
891
892 ir_node *
893 new_rd_Mux  (dbg_info *db, ir_graph *irg, ir_node *block,
894     ir_node *sel, ir_node *ir_false, ir_node *ir_true, ir_mode *mode)
895 {
896   ir_node *in[3];
897   ir_node *res;
898
899   in[0] = sel;
900   in[1] = ir_false;
901   in[2] = ir_true;
902
903   res = new_ir_node(db, irg, block, op_Mux, mode, 3, in);
904   assert(res);
905
906   res = optimize_node(res);
907   IRN_VRFY_IRG(res, irg);
908   return res;
909 }
910
911
912 ir_node *new_r_Block  (ir_graph *irg,  int arity, ir_node **in) {
913   return new_rd_Block(NULL, irg, arity, in);
914 }
915 ir_node *new_r_Start  (ir_graph *irg, ir_node *block) {
916   return new_rd_Start(NULL, irg, block);
917 }
918 ir_node *new_r_End    (ir_graph *irg, ir_node *block) {
919   return new_rd_End(NULL, irg, block);
920 }
921 ir_node *new_r_Jmp    (ir_graph *irg, ir_node *block) {
922   return new_rd_Jmp(NULL, irg, block);
923 }
924 ir_node *new_r_IJmp   (ir_graph *irg, ir_node *block, ir_node *tgt) {
925   return new_rd_IJmp(NULL, irg, block, tgt);
926 }
927 ir_node *new_r_Cond   (ir_graph *irg, ir_node *block, ir_node *c) {
928   return new_rd_Cond(NULL, irg, block, c);
929 }
930 ir_node *new_r_Return (ir_graph *irg, ir_node *block,
931                ir_node *store, int arity, ir_node **in) {
932   return new_rd_Return(NULL, irg, block, store, arity, in);
933 }
934 ir_node *new_r_Raise  (ir_graph *irg, ir_node *block,
935                ir_node *store, ir_node *obj) {
936   return new_rd_Raise(NULL, irg, block, store, obj);
937 }
938 ir_node *new_r_Const  (ir_graph *irg, ir_node *block,
939                ir_mode *mode, tarval *con) {
940   return new_rd_Const(NULL, irg, block, mode, con);
941 }
942
943 ir_node *new_r_Const_long(ir_graph *irg, ir_node *block,
944                ir_mode *mode, long value) {
945   return new_rd_Const_long(NULL, irg, block, mode, value);
946 }
947
948 ir_node *new_r_Const_type(ir_graph *irg, ir_node *block,
949                ir_mode *mode, tarval *con, type *tp) {
950   return new_rd_Const_type(NULL, irg, block, mode, con, tp);
951 }
952
953 ir_node *new_r_SymConst (ir_graph *irg, ir_node *block,
954                        symconst_symbol value, symconst_kind symkind) {
955   return new_rd_SymConst(NULL, irg, block, value, symkind);
956 }
957 ir_node *new_r_Sel    (ir_graph *irg, ir_node *block, ir_node *store,
958                   ir_node *objptr, int n_index, ir_node **index,
959                   entity *ent) {
960   return new_rd_Sel(NULL, irg, block, store, objptr, n_index, index, ent);
961 }
962 ir_node *new_r_InstOf (ir_graph *irg, ir_node *block, ir_node *store, ir_node *objptr,
963                   type *ent) {
964   return (new_rd_InstOf (NULL, irg, block, store, objptr, ent));
965 }
966 ir_node *new_r_Call   (ir_graph *irg, ir_node *block, ir_node *store,
967                   ir_node *callee, int arity, ir_node **in,
968                   type *tp) {
969   return new_rd_Call(NULL, irg, block, store, callee, arity, in, tp);
970 }
971 ir_node *new_r_Add    (ir_graph *irg, ir_node *block,
972                   ir_node *op1, ir_node *op2, ir_mode *mode) {
973   return new_rd_Add(NULL, irg, block, op1, op2, mode);
974 }
975 ir_node *new_r_Sub    (ir_graph *irg, ir_node *block,
976                   ir_node *op1, ir_node *op2, ir_mode *mode) {
977   return new_rd_Sub(NULL, irg, block, op1, op2, mode);
978 }
979 ir_node *new_r_Minus  (ir_graph *irg, ir_node *block,
980                   ir_node *op,  ir_mode *mode) {
981   return new_rd_Minus(NULL, irg, block,  op, mode);
982 }
983 ir_node *new_r_Mul    (ir_graph *irg, ir_node *block,
984                   ir_node *op1, ir_node *op2, ir_mode *mode) {
985   return new_rd_Mul(NULL, irg, block, op1, op2, mode);
986 }
987 ir_node *new_r_Quot   (ir_graph *irg, ir_node *block,
988                   ir_node *memop, ir_node *op1, ir_node *op2) {
989   return new_rd_Quot(NULL, irg, block, memop, op1, op2);
990 }
991 ir_node *new_r_DivMod (ir_graph *irg, ir_node *block,
992                   ir_node *memop, ir_node *op1, ir_node *op2) {
993   return new_rd_DivMod(NULL, irg, block, memop, op1, op2);
994 }
995 ir_node *new_r_Div    (ir_graph *irg, ir_node *block,
996                   ir_node *memop, ir_node *op1, ir_node *op2) {
997   return new_rd_Div(NULL, irg, block, memop, op1, op2);
998 }
999 ir_node *new_r_Mod    (ir_graph *irg, ir_node *block,
1000                   ir_node *memop, ir_node *op1, ir_node *op2) {
1001   return new_rd_Mod(NULL, irg, block, memop, op1, op2);
1002 }
1003 ir_node *new_r_Abs    (ir_graph *irg, ir_node *block,
1004                   ir_node *op, ir_mode *mode) {
1005   return new_rd_Abs(NULL, irg, block, op, mode);
1006 }
1007 ir_node *new_r_And    (ir_graph *irg, ir_node *block,
1008                   ir_node *op1, ir_node *op2, ir_mode *mode) {
1009   return new_rd_And(NULL, irg, block,  op1, op2, mode);
1010 }
1011 ir_node *new_r_Or     (ir_graph *irg, ir_node *block,
1012                   ir_node *op1, ir_node *op2, ir_mode *mode) {
1013   return new_rd_Or(NULL, irg, block,  op1, op2, mode);
1014 }
1015 ir_node *new_r_Eor    (ir_graph *irg, ir_node *block,
1016                   ir_node *op1, ir_node *op2, ir_mode *mode) {
1017   return new_rd_Eor(NULL, irg, block,  op1, op2, mode);
1018 }
1019 ir_node *new_r_Not    (ir_graph *irg, ir_node *block,
1020                ir_node *op, ir_mode *mode) {
1021   return new_rd_Not(NULL, irg, block, op, mode);
1022 }
1023 ir_node *new_r_Cmp    (ir_graph *irg, ir_node *block,
1024                ir_node *op1, ir_node *op2) {
1025   return new_rd_Cmp(NULL, irg, block, op1, op2);
1026 }
1027 ir_node *new_r_Shl    (ir_graph *irg, ir_node *block,
1028                ir_node *op, ir_node *k, ir_mode *mode) {
1029   return new_rd_Shl(NULL, irg, block, op, k, mode);
1030 }
1031 ir_node *new_r_Shr    (ir_graph *irg, ir_node *block,
1032                ir_node *op, ir_node *k, ir_mode *mode) {
1033   return new_rd_Shr(NULL, irg, block, op, k, mode);
1034 }
1035 ir_node *new_r_Shrs   (ir_graph *irg, ir_node *block,
1036                ir_node *op, ir_node *k, ir_mode *mode) {
1037   return new_rd_Shrs(NULL, irg, block, op, k, mode);
1038 }
1039 ir_node *new_r_Rot    (ir_graph *irg, ir_node *block,
1040                ir_node *op, ir_node *k, ir_mode *mode) {
1041   return new_rd_Rot(NULL, irg, block, op, k, mode);
1042 }
1043 ir_node *new_r_Conv   (ir_graph *irg, ir_node *block,
1044                ir_node *op, ir_mode *mode) {
1045   return new_rd_Conv(NULL, irg, block, op, mode);
1046 }
1047 ir_node *new_r_Cast   (ir_graph *irg, ir_node *block, ir_node *op, type *to_tp) {
1048   return new_rd_Cast(NULL, irg, block, op, to_tp);
1049 }
1050 ir_node *new_r_Phi    (ir_graph *irg, ir_node *block, int arity,
1051                ir_node **in, ir_mode *mode) {
1052   return new_rd_Phi(NULL, irg, block, arity, in, mode);
1053 }
1054 ir_node *new_r_Load   (ir_graph *irg, ir_node *block,
1055                ir_node *store, ir_node *adr, ir_mode *mode) {
1056   return new_rd_Load(NULL, irg, block, store, adr, mode);
1057 }
1058 ir_node *new_r_Store  (ir_graph *irg, ir_node *block,
1059                ir_node *store, ir_node *adr, ir_node *val) {
1060   return new_rd_Store(NULL, irg, block, store, adr, val);
1061 }
1062 ir_node *new_r_Alloc  (ir_graph *irg, ir_node *block, ir_node *store,
1063                ir_node *size, type *alloc_type, where_alloc where) {
1064   return new_rd_Alloc(NULL, irg, block, store, size, alloc_type, where);
1065 }
1066 ir_node *new_r_Free   (ir_graph *irg, ir_node *block, ir_node *store,
1067                ir_node *ptr, ir_node *size, type *free_type, where_alloc where) {
1068   return new_rd_Free(NULL, irg, block, store, ptr, size, free_type, where);
1069 }
1070 ir_node *new_r_Sync   (ir_graph *irg, ir_node *block, int arity, ir_node **in) {
1071   return new_rd_Sync(NULL, irg, block, arity, in);
1072 }
1073 ir_node *new_r_Proj   (ir_graph *irg, ir_node *block, ir_node *arg,
1074                ir_mode *mode, long proj) {
1075   return new_rd_Proj(NULL, irg, block, arg, mode, proj);
1076 }
1077 ir_node *new_r_defaultProj (ir_graph *irg, ir_node *block, ir_node *arg,
1078                 long max_proj) {
1079   return new_rd_defaultProj(NULL, irg, block, arg, max_proj);
1080 }
1081 ir_node *new_r_Tuple  (ir_graph *irg, ir_node *block,
1082                int arity, ir_node **in) {
1083   return new_rd_Tuple(NULL, irg, block, arity, in );
1084 }
1085 ir_node *new_r_Id     (ir_graph *irg, ir_node *block,
1086                ir_node *val, ir_mode *mode) {
1087   return new_rd_Id(NULL, irg, block, val, mode);
1088 }
1089 ir_node *new_r_Bad    (ir_graph *irg) {
1090   return new_rd_Bad(irg);
1091 }
1092 ir_node *new_r_Confirm (ir_graph *irg, ir_node *block, ir_node *val, ir_node *bound, pn_Cmp cmp) {
1093   return new_rd_Confirm (NULL, irg, block, val, bound, cmp);
1094 }
1095 ir_node *new_r_Unknown (ir_graph *irg, ir_mode *m) {
1096   return new_rd_Unknown(irg, m);
1097 }
1098 ir_node *new_r_CallBegin (ir_graph *irg, ir_node *block, ir_node *callee) {
1099   return new_rd_CallBegin(NULL, irg, block, callee);
1100 }
1101 ir_node *new_r_EndReg (ir_graph *irg, ir_node *block) {
1102   return new_rd_EndReg(NULL, irg, block);
1103 }
1104 ir_node *new_r_EndExcept (ir_graph *irg, ir_node *block) {
1105   return new_rd_EndExcept(NULL, irg, block);
1106 }
1107 ir_node *new_r_Break  (ir_graph *irg, ir_node *block) {
1108   return new_rd_Break(NULL, irg, block);
1109 }
1110 ir_node *new_r_Filter (ir_graph *irg, ir_node *block, ir_node *arg,
1111                ir_mode *mode, long proj) {
1112   return new_rd_Filter(NULL, irg, block, arg, mode, proj);
1113 }
1114 ir_node *new_r_NoMem  (ir_graph *irg) {
1115   return new_rd_NoMem(irg);
1116 }
1117 ir_node *new_r_Mux (ir_graph *irg, ir_node *block,
1118     ir_node *sel, ir_node *ir_false, ir_node *ir_true, ir_mode *mode) {
1119   return new_rd_Mux(NULL, irg, block, sel, ir_false, ir_true, mode);
1120 }
1121
1122
1123 /** ********************/
1124 /** public interfaces  */
1125 /** construction tools */
1126
1127 /**
1128  *
1129  *   - create a new Start node in the current block
1130  *
1131  *   @return s - pointer to the created Start node
1132  *
1133  *
1134  */
1135 ir_node *
1136 new_d_Start (dbg_info* db)
1137 {
1138   ir_node *res;
1139
1140   res = new_ir_node (db, current_ir_graph, current_ir_graph->current_block,
1141              op_Start, mode_T, 0, NULL);
1142   /* res->attr.start.irg = current_ir_graph; */
1143
1144   res = optimize_node(res);
1145   IRN_VRFY_IRG(res, current_ir_graph);
1146   return res;
1147 }
1148
1149 ir_node *
1150 new_d_End (dbg_info* db)
1151 {
1152   ir_node *res;
1153   res = new_ir_node(db, current_ir_graph,  current_ir_graph->current_block,
1154              op_End, mode_X, -1, NULL);
1155   res = optimize_node(res);
1156   IRN_VRFY_IRG(res, current_ir_graph);
1157
1158   return res;
1159 }
1160
1161 /* Constructs a Block with a fixed number of predecessors.
1162    Does set current_block.  Can be used with automatic Phi
1163    node construction. */
1164 ir_node *
1165 new_d_Block (dbg_info* db, int arity, ir_node **in)
1166 {
1167   ir_node *res;
1168   int i;
1169   bool has_unknown = false;
1170
1171   res = new_rd_Block(db, current_ir_graph, arity, in);
1172
1173   /* Create and initialize array for Phi-node construction. */
1174   if (get_irg_phase_state(current_ir_graph) == phase_building) {
1175     res->attr.block.graph_arr = NEW_ARR_D(ir_node *, current_ir_graph->obst,
1176                       current_ir_graph->n_loc);
1177     memset(res->attr.block.graph_arr, 0, sizeof(ir_node *)*current_ir_graph->n_loc);
1178   }
1179
1180   for (i = arity-1; i >= 0; i--)
1181     if (get_irn_op(in[i]) == op_Unknown) {
1182       has_unknown = true;
1183       break;
1184     }
1185
1186   if (!has_unknown) res = optimize_node(res);
1187   current_ir_graph->current_block = res;
1188
1189   IRN_VRFY_IRG(res, current_ir_graph);
1190
1191   return res;
1192 }
1193
1194 /* ***********************************************************************/
1195 /* Methods necessary for automatic Phi node creation                     */
1196 /*
1197   ir_node *phi_merge            (ir_node *block, int pos, ir_mode *mode, ir_node **nin, int ins)
1198   ir_node *get_r_value_internal (ir_node *block, int pos, ir_mode *mode);
1199   ir_node *new_rd_Phi0           (ir_graph *irg, ir_node *block, ir_mode *mode)
1200   ir_node *new_rd_Phi_in         (ir_graph *irg, ir_node *block, ir_mode *mode,  ir_node **in, int ins)
1201
1202   Call Graph:   ( A ---> B == A "calls" B)
1203
1204        get_value         mature_immBlock
1205           |                   |
1206           |                   |
1207           |                   |
1208           |          ---> phi_merge
1209           |         /       /   \
1210           |        /       /     \
1211          \|/      /      |/_      \
1212        get_r_value_internal        |
1213                 |                  |
1214             |                  |
1215            \|/                \|/
1216         new_rd_Phi0          new_rd_Phi_in
1217
1218 * *************************************************************************** */
1219
1220 /** Creates a Phi node with 0 predecessors */
1221 static INLINE ir_node *
1222 new_rd_Phi0 (ir_graph *irg, ir_node *block, ir_mode *mode)
1223 {
1224   ir_node *res;
1225
1226   res = new_ir_node(NULL, irg, block, op_Phi, mode, 0, NULL);
1227   IRN_VRFY_IRG(res, irg);
1228   return res;
1229 }
1230
1231 /* There are two implementations of the Phi node construction.  The first
1232    is faster, but does not work for blocks with more than 2 predecessors.
1233    The second works always but is slower and causes more unnecessary Phi
1234    nodes.
1235    Select the implementations by the following preprocessor flag set in
1236    common/common.h: */
1237 #if USE_FAST_PHI_CONSTRUCTION
1238
1239 /* This is a stack used for allocating and deallocating nodes in
1240    new_rd_Phi_in.  The original implementation used the obstack
1241    to model this stack, now it is explicit.  This reduces side effects.
1242 */
1243 #if USE_EXPLICIT_PHI_IN_STACK
1244 Phi_in_stack *
1245 new_Phi_in_stack(void) {
1246   Phi_in_stack *res;
1247
1248   res = (Phi_in_stack *) malloc ( sizeof (Phi_in_stack));
1249
1250   res->stack = NEW_ARR_F (ir_node *, 0);
1251   res->pos = 0;
1252
1253   return res;
1254 }
1255
1256 void
1257 free_Phi_in_stack(Phi_in_stack *s) {
1258   DEL_ARR_F(s->stack);
1259   free(s);
1260 }
1261 static INLINE void
1262 free_to_Phi_in_stack(ir_node *phi) {
1263   if (ARR_LEN(current_ir_graph->Phi_in_stack->stack) ==
1264       current_ir_graph->Phi_in_stack->pos)
1265     ARR_APP1 (ir_node *, current_ir_graph->Phi_in_stack->stack, phi);
1266   else
1267     current_ir_graph->Phi_in_stack->stack[current_ir_graph->Phi_in_stack->pos] = phi;
1268
1269   (current_ir_graph->Phi_in_stack->pos)++;
1270 }
1271
1272 static INLINE ir_node *
1273 alloc_or_pop_from_Phi_in_stack(ir_graph *irg, ir_node *block, ir_mode *mode,
1274          int arity, ir_node **in) {
1275   ir_node *res;
1276   ir_node **stack = current_ir_graph->Phi_in_stack->stack;
1277   int pos = current_ir_graph->Phi_in_stack->pos;
1278
1279
1280   if (pos == 0) {
1281     /* We need to allocate a new node */
1282     res = new_ir_node (db, irg, block, op_Phi, mode, arity, in);
1283     res->attr.phi_backedge = new_backedge_arr(irg->obst, arity);
1284   } else {
1285     /* reuse the old node and initialize it again. */
1286     res = stack[pos-1];
1287
1288     assert (res->kind == k_ir_node);
1289     assert (res->op == op_Phi);
1290     res->mode = mode;
1291     res->visited = 0;
1292     res->link = NULL;
1293     assert (arity >= 0);
1294     /* ???!!! How to free the old in array??  Not at all: on obstack ?!! */
1295     res->in = NEW_ARR_D (ir_node *, irg->obst, (arity+1));
1296     res->in[0] = block;
1297     memcpy (&res->in[1], in, sizeof (ir_node *) * arity);
1298
1299     (current_ir_graph->Phi_in_stack->pos)--;
1300   }
1301   return res;
1302 }
1303 #endif /* USE_EXPLICIT_PHI_IN_STACK */
1304
1305 /* Creates a Phi node with a given, fixed array **in of predecessors.
1306    If the Phi node is unnecessary, as the same value reaches the block
1307    through all control flow paths, it is eliminated and the value
1308    returned directly.  This constructor is only intended for use in
1309    the automatic Phi node generation triggered by get_value or mature.
1310    The implementation is quite tricky and depends on the fact, that
1311    the nodes are allocated on a stack:
1312    The in array contains predecessors and NULLs.  The NULLs appear,
1313    if get_r_value_internal, that computed the predecessors, reached
1314    the same block on two paths.  In this case the same value reaches
1315    this block on both paths, there is no definition in between.  We need
1316    not allocate a Phi where these path's merge, but we have to communicate
1317    this fact to the caller.  This happens by returning a pointer to the
1318    node the caller _will_ allocate.  (Yes, we predict the address. We can
1319    do so because the nodes are allocated on the obstack.)  The caller then
1320    finds a pointer to itself and, when this routine is called again,
1321    eliminates itself.
1322    */
1323 static INLINE ir_node *
1324 new_rd_Phi_in (ir_graph *irg, ir_node *block, ir_mode *mode, ir_node **in, int ins)
1325 {
1326   int i;
1327   ir_node *res, *known;
1328
1329   /* Allocate a new node on the obstack.  This can return a node to
1330      which some of the pointers in the in-array already point.
1331      Attention: the constructor copies the in array, i.e., the later
1332      changes to the array in this routine do not affect the
1333      constructed node!  If the in array contains NULLs, there will be
1334      missing predecessors in the returned node.  Is this a possible
1335      internal state of the Phi node generation? */
1336 #if USE_EXPLICIT_PHI_IN_STACK
1337   res = known = alloc_or_pop_from_Phi_in_stack(irg, block, mode, ins, in);
1338 #else
1339   res = known = new_ir_node (NULL, irg, block, op_Phi, mode, ins, in);
1340   res->attr.phi_backedge = new_backedge_arr(irg->obst, ins);
1341 #endif
1342
1343   /* The in-array can contain NULLs.  These were returned by
1344      get_r_value_internal if it reached the same block/definition on a
1345      second path.  The NULLs are replaced by the node itself to
1346      simplify the test in the next loop. */
1347   for (i = 0;  i < ins;  ++i) {
1348     if (in[i] == NULL)
1349       in[i] = res;
1350   }
1351
1352   /* This loop checks whether the Phi has more than one predecessor.
1353      If so, it is a real Phi node and we break the loop.  Else the Phi
1354      node merges the same definition on several paths and therefore is
1355      not needed. */
1356   for (i = 0;  i < ins;  ++i) {
1357     if (in[i] == res || in[i] == known)
1358       continue;
1359
1360     if (known == res)
1361       known = in[i];
1362     else
1363       break;
1364   }
1365
1366   /* i==ins: there is at most one predecessor, we don't need a phi node. */
1367   if (i==ins) {
1368 #if USE_EXPLICIT_PHI_IN_STACK
1369     free_to_Phi_in_stack(res);
1370 #else
1371     edges_node_deleted(res, current_ir_graph);
1372     obstack_free(current_ir_graph->obst, res);
1373 #endif
1374     res = known;
1375   } else {
1376     res = optimize_node (res);
1377     IRN_VRFY_IRG(res, irg);
1378   }
1379
1380   /* return the pointer to the Phi node.  This node might be deallocated! */
1381   return res;
1382 }
1383
1384 static ir_node *
1385 get_r_value_internal (ir_node *block, int pos, ir_mode *mode);
1386
1387 /**
1388     allocates and returns this node.  The routine called to allocate the
1389     node might optimize it away and return a real value, or even a pointer
1390     to a deallocated Phi node on top of the obstack!
1391     This function is called with an in-array of proper size. **/
1392 static ir_node *
1393 phi_merge (ir_node *block, int pos, ir_mode *mode, ir_node **nin, int ins)
1394 {
1395   ir_node *prevBlock, *res;
1396   int i;
1397
1398   /* This loop goes to all predecessor blocks of the block the Phi node is in
1399      and there finds the operands of the Phi node by calling
1400      get_r_value_internal. */
1401   for (i = 1;  i <= ins;  ++i) {
1402     assert (block->in[i]);
1403     prevBlock = block->in[i]->in[0]; /* go past control flow op to prev block */
1404     assert (prevBlock);
1405     nin[i-1] = get_r_value_internal (prevBlock, pos, mode);
1406   }
1407
1408   /* After collecting all predecessors into the array nin a new Phi node
1409      with these predecessors is created.  This constructor contains an
1410      optimization: If all predecessors of the Phi node are identical it
1411      returns the only operand instead of a new Phi node.  If the value
1412      passes two different control flow edges without being defined, and
1413      this is the second path treated, a pointer to the node that will be
1414      allocated for the first path (recursion) is returned.  We already
1415      know the address of this node, as it is the next node to be allocated
1416      and will be placed on top of the obstack. (The obstack is a _stack_!) */
1417   res = new_rd_Phi_in (current_ir_graph, block, mode, nin, ins);
1418
1419   /* Now we now the value for "pos" and can enter it in the array with
1420      all known local variables.  Attention: this might be a pointer to
1421      a node, that later will be allocated!!! See new_rd_Phi_in.
1422      If this is called in mature, after some set_value in the same block,
1423      the proper value must not be overwritten:
1424      The call order
1425        get_value    (makes Phi0, put's it into graph_arr)
1426        set_value    (overwrites Phi0 in graph_arr)
1427        mature_immBlock (upgrades Phi0, puts it again into graph_arr, overwriting
1428                      the proper value.)
1429      fails. */
1430   if (!block->attr.block.graph_arr[pos]) {
1431     block->attr.block.graph_arr[pos] = res;
1432   } else {
1433     /*  printf(" value already computed by %s\n",
1434         get_id_str(block->attr.block.graph_arr[pos]->op->name));  */
1435   }
1436
1437   return res;
1438 }
1439
1440 /* This function returns the last definition of a variable.  In case
1441    this variable was last defined in a previous block, Phi nodes are
1442    inserted.  If the part of the firm graph containing the definition
1443    is not yet constructed, a dummy Phi node is returned. */
1444 static ir_node *
1445 get_r_value_internal (ir_node *block, int pos, ir_mode *mode)
1446 {
1447   ir_node *res;
1448   /* There are 4 cases to treat.
1449
1450      1. The block is not mature and we visit it the first time.  We can not
1451         create a proper Phi node, therefore a Phi0, i.e., a Phi without
1452         predecessors is returned.  This node is added to the linked list (field
1453         "link") of the containing block to be completed when this block is
1454         matured. (Completion will add a new Phi and turn the Phi0 into an Id
1455         node.)
1456
1457      2. The value is already known in this block, graph_arr[pos] is set and we
1458         visit the block the first time.  We can return the value without
1459         creating any new nodes.
1460
1461      3. The block is mature and we visit it the first time.  A Phi node needs
1462         to be created (phi_merge).  If the Phi is not needed, as all it's
1463         operands are the same value reaching the block through different
1464         paths, it's optimized away and the value itself is returned.
1465
1466      4. The block is mature, and we visit it the second time.  Now two
1467         subcases are possible:
1468         * The value was computed completely the last time we were here. This
1469           is the case if there is no loop.  We can return the proper value.
1470         * The recursion that visited this node and set the flag did not
1471           return yet.  We are computing a value in a loop and need to
1472           break the recursion without knowing the result yet.
1473       @@@ strange case.  Straight forward we would create a Phi before
1474       starting the computation of it's predecessors.  In this case we will
1475       find a Phi here in any case.  The problem is that this implementation
1476       only creates a Phi after computing the predecessors, so that it is
1477       hard to compute self references of this Phi.  @@@
1478         There is no simple check for the second subcase.  Therefore we check
1479         for a second visit and treat all such cases as the second subcase.
1480         Anyways, the basic situation is the same:  we reached a block
1481         on two paths without finding a definition of the value:  No Phi
1482         nodes are needed on both paths.
1483         We return this information "Two paths, no Phi needed" by a very tricky
1484         implementation that relies on the fact that an obstack is a stack and
1485         will return a node with the same address on different allocations.
1486         Look also at phi_merge and new_rd_phi_in to understand this.
1487     @@@ Unfortunately this does not work, see testprogram
1488     three_cfpred_example.
1489
1490   */
1491
1492   /* case 4 -- already visited. */
1493   if (get_irn_visited(block) == get_irg_visited(current_ir_graph)) return NULL;
1494
1495   /* visited the first time */
1496   set_irn_visited(block, get_irg_visited(current_ir_graph));
1497
1498   /* Get the local valid value */
1499   res = block->attr.block.graph_arr[pos];
1500
1501   /* case 2 -- If the value is actually computed, return it. */
1502   if (res) return res;
1503
1504   if (block->attr.block.matured) { /* case 3 */
1505
1506     /* The Phi has the same amount of ins as the corresponding block. */
1507     int ins = get_irn_arity(block);
1508     ir_node **nin;
1509     NEW_ARR_A (ir_node *, nin, ins);
1510
1511     /* Phi merge collects the predecessors and then creates a node. */
1512     res = phi_merge (block, pos, mode, nin, ins);
1513
1514   } else {  /* case 1 */
1515     /* The block is not mature, we don't know how many in's are needed.  A Phi
1516        with zero predecessors is created.  Such a Phi node is called Phi0
1517        node.  (There is also an obsolete Phi0 opcode.) The Phi0 is then added
1518        to the list of Phi0 nodes in this block to be matured by mature_immBlock
1519        later.
1520        The Phi0 has to remember the pos of it's internal value.  If the real
1521        Phi is computed, pos is used to update the array with the local
1522        values. */
1523
1524     res = new_rd_Phi0 (current_ir_graph, block, mode);
1525     res->attr.phi0_pos = pos;
1526     res->link = block->link;
1527     block->link = res;
1528   }
1529
1530   /* If we get here, the frontend missed a use-before-definition error */
1531   if (!res) {
1532     /* Error Message */
1533     printf("Error: no value set.  Use of undefined variable.  Initializing to zero.\n");
1534     assert (mode->code >= irm_F && mode->code <= irm_P);
1535     res = new_rd_Const (NULL, current_ir_graph, block, mode,
1536                tarval_mode_null[mode->code]);
1537   }
1538
1539   /* The local valid value is available now. */
1540   block->attr.block.graph_arr[pos] = res;
1541
1542   return res;
1543 }
1544
1545 #else /* if 0 */
1546
1547 /**
1548     it starts the recursion.  This causes an Id at the entry of
1549     every block that has no definition of the value! **/
1550
1551 #if USE_EXPLICIT_PHI_IN_STACK
1552 /* Just dummies */
1553 Phi_in_stack * new_Phi_in_stack() {  return NULL; }
1554 void free_Phi_in_stack(Phi_in_stack *s) { }
1555 #endif
1556
1557 static INLINE ir_node *
1558 new_rd_Phi_in (ir_graph *irg, ir_node *block, ir_mode *mode,
1559            ir_node **in, int ins, ir_node *phi0)
1560 {
1561   int i;
1562   ir_node *res, *known;
1563
1564   /* Allocate a new node on the obstack.  The allocation copies the in
1565      array. */
1566   res = new_ir_node (NULL, irg, block, op_Phi, mode, ins, in);
1567   res->attr.phi_backedge = new_backedge_arr(irg->obst, ins);
1568
1569   /* This loop checks whether the Phi has more than one predecessor.
1570      If so, it is a real Phi node and we break the loop.  Else the
1571      Phi node merges the same definition on several paths and therefore
1572      is not needed. Don't consider Bad nodes! */
1573   known = res;
1574   for (i=0;  i < ins;  ++i)
1575   {
1576     assert(in[i]);
1577
1578     in[i] = skip_Id(in[i]);  /* increasses the number of freed Phis. */
1579
1580     /* Optimize self referencing Phis:  We can't detect them yet properly, as
1581        they still refer to the Phi0 they will replace.  So replace right now. */
1582     if (phi0 && in[i] == phi0) in[i] = res;
1583
1584     if (in[i]==res || in[i]==known || is_Bad(in[i])) continue;
1585
1586     if (known==res)
1587       known = in[i];
1588     else
1589       break;
1590   }
1591
1592   /* i==ins: there is at most one predecessor, we don't need a phi node. */
1593   if (i == ins) {
1594     if (res != known) {
1595       edges_node_deleted(res, current_ir_graph);
1596       obstack_free (current_ir_graph->obst, res);
1597       if (is_Phi(known)) {
1598         /* If pred is a phi node we want to optimize it: If loops are matured in a bad
1599            order, an enclosing Phi know may get superfluous. */
1600         res = optimize_in_place_2(known);
1601         if (res != known)
1602           exchange(known, res);
1603
1604       }
1605       else
1606         res = known;
1607     } else {
1608       /* A undefined value, e.g., in unreachable code. */
1609       res = new_Bad();
1610     }
1611   } else {
1612     res = optimize_node (res);  /* This is necessary to add the node to the hash table for cse. */
1613     IRN_VRFY_IRG(res, irg);
1614     /* Memory Phis in endless loops must be kept alive.
1615        As we can't distinguish these easily we keep all of them alive. */
1616     if ((res->op == op_Phi) && (mode == mode_M))
1617       add_End_keepalive(irg->end, res);
1618   }
1619
1620   return res;
1621 }
1622
1623 static ir_node *
1624 get_r_value_internal (ir_node *block, int pos, ir_mode *mode);
1625
1626 #if PRECISE_EXC_CONTEXT
1627 static ir_node *
1628 phi_merge (ir_node *block, int pos, ir_mode *mode, ir_node **nin, int ins);
1629
1630 /* Construct a new frag_array for node n.
1631    Copy the content from the current graph_arr of the corresponding block:
1632    this is the current state.
1633    Set ProjM(n) as current memory state.
1634    Further the last entry in frag_arr of current block points to n.  This
1635    constructs a chain block->last_frag_op-> ... first_frag_op of all frag ops in the block.
1636  */
1637 static INLINE ir_node ** new_frag_arr (ir_node *n)
1638 {
1639   ir_node **arr;
1640   int opt;
1641
1642   arr = NEW_ARR_D (ir_node *, current_ir_graph->obst, current_ir_graph->n_loc);
1643   memcpy(arr, current_ir_graph->current_block->attr.block.graph_arr,
1644      sizeof(ir_node *)*current_ir_graph->n_loc);
1645
1646   /* turn off optimization before allocating Proj nodes, as res isn't
1647      finished yet. */
1648   opt = get_opt_optimize(); set_optimize(0);
1649   /* Here we rely on the fact that all frag ops have Memory as first result! */
1650   if (get_irn_op(n) == op_Call)
1651     arr[0] = new_Proj(n, mode_M, pn_Call_M_except);
1652   else {
1653     assert((pn_Quot_M == pn_DivMod_M) &&
1654        (pn_Quot_M == pn_Div_M)    &&
1655        (pn_Quot_M == pn_Mod_M)    &&
1656        (pn_Quot_M == pn_Load_M)   &&
1657        (pn_Quot_M == pn_Store_M)  &&
1658        (pn_Quot_M == pn_Alloc_M)    );
1659     arr[0] = new_Proj(n, mode_M, pn_Alloc_M);
1660   }
1661   set_optimize(opt);
1662
1663   current_ir_graph->current_block->attr.block.graph_arr[current_ir_graph->n_loc-1] = n;
1664   return arr;
1665 }
1666
1667 /**
1668  * returns the frag_arr from a node
1669  */
1670 static INLINE ir_node **
1671 get_frag_arr (ir_node *n) {
1672   switch (get_irn_opcode(n)) {
1673   case iro_Call:
1674     return n->attr.call.exc.frag_arr;
1675   case iro_Alloc:
1676     return n->attr.a.exc.frag_arr;
1677   case iro_Load:
1678     return n->attr.load.exc.frag_arr;
1679   case iro_Store:
1680     return n->attr.store.exc.frag_arr;
1681   default:
1682     return n->attr.except.frag_arr;
1683   }
1684 }
1685
1686 static void
1687 set_frag_value(ir_node **frag_arr, int pos, ir_node *val) {
1688 #if 0
1689   if (!frag_arr[pos]) frag_arr[pos] = val;
1690   if (frag_arr[current_ir_graph->n_loc - 1]) {
1691     ir_node **arr = get_frag_arr(frag_arr[current_ir_graph->n_loc - 1]);
1692     assert(arr != frag_arr && "Endless recursion detected");
1693     set_frag_value(arr, pos, val);
1694   }
1695 #else
1696   int i;
1697
1698   for (i = 0; i < 1000; ++i) {
1699     if (!frag_arr[pos]) {
1700       frag_arr[pos] = val;
1701     }
1702     if (frag_arr[current_ir_graph->n_loc - 1]) {
1703       ir_node **arr = get_frag_arr(frag_arr[current_ir_graph->n_loc - 1]);
1704       frag_arr = arr;
1705     }
1706     else
1707       return;
1708   }
1709   assert(0 && "potential endless recursion");
1710 #endif
1711 }
1712
1713 static ir_node *
1714 get_r_frag_value_internal (ir_node *block, ir_node *cfOp, int pos, ir_mode *mode) {
1715   ir_node *res;
1716   ir_node **frag_arr;
1717
1718   assert(is_fragile_op(cfOp) && (get_irn_op(cfOp) != op_Bad));
1719
1720   frag_arr = get_frag_arr(cfOp);
1721   res = frag_arr[pos];
1722   if (!res) {
1723     if (block->attr.block.graph_arr[pos]) {
1724       /* There was a set_value after the cfOp and no get_value before that
1725          set_value.  We must build a Phi node now. */
1726       if (block->attr.block.matured) {
1727         int ins = get_irn_arity(block);
1728         ir_node **nin;
1729         NEW_ARR_A (ir_node *, nin, ins);
1730         res = phi_merge(block, pos, mode, nin, ins);
1731       } else {
1732         res = new_rd_Phi0 (current_ir_graph, block, mode);
1733         res->attr.phi0_pos = pos;
1734         res->link = block->link;
1735         block->link = res;
1736       }
1737       assert(res);
1738       /* @@@ tested by Flo: set_frag_value(frag_arr, pos, res);
1739          but this should be better: (remove comment if this works) */
1740       /* It's a Phi, we can write this into all graph_arrs with NULL */
1741       set_frag_value(block->attr.block.graph_arr, pos, res);
1742     } else {
1743       res = get_r_value_internal(block, pos, mode);
1744       set_frag_value(block->attr.block.graph_arr, pos, res);
1745     }
1746   }
1747   return res;
1748 }
1749 #endif
1750
1751 /**
1752     computes the predecessors for the real phi node, and then
1753     allocates and returns this node.  The routine called to allocate the
1754     node might optimize it away and return a real value.
1755     This function must be called with an in-array of proper size. **/
1756 static ir_node *
1757 phi_merge (ir_node *block, int pos, ir_mode *mode, ir_node **nin, int ins)
1758 {
1759   ir_node *prevBlock, *prevCfOp, *res, *phi0, *phi0_all;
1760   int i;
1761
1762   /* If this block has no value at pos create a Phi0 and remember it
1763      in graph_arr to break recursions.
1764      Else we may not set graph_arr as there a later value is remembered. */
1765   phi0 = NULL;
1766   if (!block->attr.block.graph_arr[pos]) {
1767     if (block == get_irg_start_block(current_ir_graph)) {
1768       /* Collapsing to Bad tarvals is no good idea.
1769          So we call a user-supplied routine here that deals with this case as
1770          appropriate for the given language. Sorryly the only help we can give
1771          here is the position.
1772
1773          Even if all variables are defined before use, it can happen that
1774          we get to the start block, if a cond has been replaced by a tuple
1775          (bad, jmp).  In this case we call the function needlessly, eventually
1776          generating an non existant error.
1777          However, this SHOULD NOT HAPPEN, as bad control flow nodes are intercepted
1778          before recuring.
1779       */
1780       if (default_initialize_local_variable)
1781         block->attr.block.graph_arr[pos] = default_initialize_local_variable(current_ir_graph, mode, pos - 1);
1782       else
1783         block->attr.block.graph_arr[pos] = new_Const(mode, tarval_bad);
1784       /* We don't need to care about exception ops in the start block.
1785      There are none by definition. */
1786       return block->attr.block.graph_arr[pos];
1787     } else {
1788       phi0 = new_rd_Phi0(current_ir_graph, block, mode);
1789       block->attr.block.graph_arr[pos] = phi0;
1790 #if PRECISE_EXC_CONTEXT
1791       if (get_opt_precise_exc_context()) {
1792     /* Set graph_arr for fragile ops.  Also here we should break recursion.
1793        We could choose a cyclic path through an cfop.  But the recursion would
1794        break at some point. */
1795     set_frag_value(block->attr.block.graph_arr, pos, phi0);
1796       }
1797 #endif
1798     }
1799   }
1800
1801   /* This loop goes to all predecessor blocks of the block the Phi node
1802      is in and there finds the operands of the Phi node by calling
1803      get_r_value_internal.  */
1804   for (i = 1;  i <= ins;  ++i) {
1805     prevCfOp = skip_Proj(block->in[i]);
1806     assert (prevCfOp);
1807     if (is_Bad(prevCfOp)) {
1808       /* In case a Cond has been optimized we would get right to the start block
1809      with an invalid definition. */
1810       nin[i-1] = new_Bad();
1811       continue;
1812     }
1813     prevBlock = block->in[i]->in[0]; /* go past control flow op to prev block */
1814     assert (prevBlock);
1815     if (!is_Bad(prevBlock)) {
1816 #if PRECISE_EXC_CONTEXT
1817       if (get_opt_precise_exc_context() &&
1818       is_fragile_op(prevCfOp) && (get_irn_op (prevCfOp) != op_Bad)) {
1819     assert(get_r_frag_value_internal (prevBlock, prevCfOp, pos, mode));
1820     nin[i-1] = get_r_frag_value_internal (prevBlock, prevCfOp, pos, mode);
1821       } else
1822 #endif
1823       nin[i-1] = get_r_value_internal (prevBlock, pos, mode);
1824     } else {
1825       nin[i-1] = new_Bad();
1826     }
1827   }
1828
1829   /* We want to pass the Phi0 node to the constructor: this finds additional
1830      optimization possibilities.
1831      The Phi0 node either is allocated in this function, or it comes from
1832      a former call to get_r_value_internal. In this case we may not yet
1833      exchange phi0, as this is done in mature_immBlock. */
1834   if (!phi0) {
1835     phi0_all = block->attr.block.graph_arr[pos];
1836     if (!((get_irn_op(phi0_all) == op_Phi) &&
1837       (get_irn_arity(phi0_all) == 0)   &&
1838       (get_nodes_block(phi0_all) == block)))
1839       phi0_all = NULL;
1840   } else {
1841     phi0_all = phi0;
1842   }
1843
1844   /* After collecting all predecessors into the array nin a new Phi node
1845      with these predecessors is created.  This constructor contains an
1846      optimization: If all predecessors of the Phi node are identical it
1847      returns the only operand instead of a new Phi node.  */
1848   res = new_rd_Phi_in (current_ir_graph, block, mode, nin, ins, phi0_all);
1849
1850   /* In case we allocated a Phi0 node at the beginning of this procedure,
1851      we need to exchange this Phi0 with the real Phi. */
1852   if (phi0) {
1853     exchange(phi0, res);
1854     block->attr.block.graph_arr[pos] = res;
1855     /* Don't set_frag_value as it does not overwrite.  Doesn't matter, is
1856        only an optimization. */
1857   }
1858
1859   return res;
1860 }
1861
1862 /* This function returns the last definition of a variable.  In case
1863    this variable was last defined in a previous block, Phi nodes are
1864    inserted.  If the part of the firm graph containing the definition
1865    is not yet constructed, a dummy Phi node is returned. */
1866 static ir_node *
1867 get_r_value_internal (ir_node *block, int pos, ir_mode *mode)
1868 {
1869   ir_node *res;
1870   /* There are 4 cases to treat.
1871
1872      1. The block is not mature and we visit it the first time.  We can not
1873         create a proper Phi node, therefore a Phi0, i.e., a Phi without
1874         predecessors is returned.  This node is added to the linked list (field
1875         "link") of the containing block to be completed when this block is
1876         matured. (Comlpletion will add a new Phi and turn the Phi0 into an Id
1877         node.)
1878
1879      2. The value is already known in this block, graph_arr[pos] is set and we
1880         visit the block the first time.  We can return the value without
1881         creating any new nodes.
1882
1883      3. The block is mature and we visit it the first time.  A Phi node needs
1884         to be created (phi_merge).  If the Phi is not needed, as all it's
1885         operands are the same value reaching the block through different
1886         paths, it's optimized away and the value itself is returned.
1887
1888      4. The block is mature, and we visit it the second time.  Now two
1889         subcases are possible:
1890         * The value was computed completely the last time we were here. This
1891           is the case if there is no loop.  We can return the proper value.
1892         * The recursion that visited this node and set the flag did not
1893           return yet.  We are computing a value in a loop and need to
1894           break the recursion.  This case only happens if we visited
1895       the same block with phi_merge before, which inserted a Phi0.
1896       So we return the Phi0.
1897   */
1898
1899   /* case 4 -- already visited. */
1900   if (get_irn_visited(block) == get_irg_visited(current_ir_graph)) {
1901     /* As phi_merge allocates a Phi0 this value is always defined. Here
1902      is the critical difference of the two algorithms. */
1903     assert(block->attr.block.graph_arr[pos]);
1904     return block->attr.block.graph_arr[pos];
1905   }
1906
1907   /* visited the first time */
1908   set_irn_visited(block, get_irg_visited(current_ir_graph));
1909
1910   /* Get the local valid value */
1911   res = block->attr.block.graph_arr[pos];
1912
1913   /* case 2 -- If the value is actually computed, return it. */
1914   if (res) { return res; };
1915
1916   if (block->attr.block.matured) { /* case 3 */
1917
1918     /* The Phi has the same amount of ins as the corresponding block. */
1919     int ins = get_irn_arity(block);
1920     ir_node **nin;
1921     NEW_ARR_A (ir_node *, nin, ins);
1922
1923     /* Phi merge collects the predecessors and then creates a node. */
1924     res = phi_merge (block, pos, mode, nin, ins);
1925
1926   } else {  /* case 1 */
1927     /* The block is not mature, we don't know how many in's are needed.  A Phi
1928        with zero predecessors is created.  Such a Phi node is called Phi0
1929        node.  The Phi0 is then added to the list of Phi0 nodes in this block
1930        to be matured by mature_immBlock later.
1931        The Phi0 has to remember the pos of it's internal value.  If the real
1932        Phi is computed, pos is used to update the array with the local
1933        values. */
1934     res = new_rd_Phi0 (current_ir_graph, block, mode);
1935     res->attr.phi0_pos = pos;
1936     res->link = block->link;
1937     block->link = res;
1938   }
1939
1940   /* If we get here, the frontend missed a use-before-definition error */
1941   if (!res) {
1942     /* Error Message */
1943     printf("Error: no value set.  Use of undefined variable.  Initializing to zero.\n");
1944     assert (mode->code >= irm_F && mode->code <= irm_P);
1945     res = new_rd_Const (NULL, current_ir_graph, block, mode,
1946             get_mode_null(mode));
1947   }
1948
1949   /* The local valid value is available now. */
1950   block->attr.block.graph_arr[pos] = res;
1951
1952   return res;
1953 }
1954
1955 #endif /* USE_FAST_PHI_CONSTRUCTION */
1956
1957 /* ************************************************************************** */
1958
1959 /** Finalize a Block node, when all control flows are known.  */
1960 /** Acceptable parameters are only Block nodes.               */
1961 void
1962 mature_immBlock (ir_node *block)
1963 {
1964
1965   int ins;
1966   ir_node *n, **nin;
1967   ir_node *next;
1968
1969   assert (get_irn_opcode(block) == iro_Block);
1970   /* @@@ should be commented in
1971      assert (!get_Block_matured(block) && "Block already matured"); */
1972
1973   if (!get_Block_matured(block)) {
1974     ins = ARR_LEN (block->in)-1;
1975     /* Fix block parameters */
1976     block->attr.block.backedge = new_backedge_arr(current_ir_graph->obst, ins);
1977
1978     /* An array for building the Phi nodes. */
1979     NEW_ARR_A (ir_node *, nin, ins);
1980
1981     /* Traverse a chain of Phi nodes attached to this block and mature
1982        these, too. **/
1983     for (n = block->link;  n;  n=next) {
1984       inc_irg_visited(current_ir_graph);
1985       next = n->link;
1986       exchange (n, phi_merge (block, n->attr.phi0_pos, n->mode, nin, ins));
1987     }
1988
1989     block->attr.block.matured = 1;
1990
1991     /* Now, as the block is a finished firm node, we can optimize it.
1992        Since other nodes have been allocated since the block was created
1993        we can not free the node on the obstack.  Therefore we have to call
1994        optimize_in_place.
1995        Unfortunately the optimization does not change a lot, as all allocated
1996        nodes refer to the unoptimized node.
1997        We can call _2, as global cse has no effect on blocks. */
1998     block = optimize_in_place_2(block);
1999     IRN_VRFY_IRG(block, current_ir_graph);
2000   }
2001 }
2002
2003 ir_node *
2004 new_d_Phi (dbg_info* db, int arity, ir_node **in, ir_mode *mode)
2005 {
2006   return new_rd_Phi(db, current_ir_graph, current_ir_graph->current_block,
2007             arity, in, mode);
2008 }
2009
2010 ir_node *
2011 new_d_Const (dbg_info* db, ir_mode *mode, tarval *con)
2012 {
2013   return new_rd_Const(db, current_ir_graph, current_ir_graph->start_block,
2014               mode, con);
2015 }
2016
2017 ir_node *
2018 new_d_Const_long(dbg_info* db, ir_mode *mode, long value)
2019 {
2020   return new_rd_Const_long(db, current_ir_graph, current_ir_graph->start_block, mode, value);
2021 }
2022
2023 ir_node *
2024 new_d_Const_type (dbg_info* db, ir_mode *mode, tarval *con, type *tp)
2025 {
2026   return new_rd_Const_type(db, current_ir_graph, current_ir_graph->start_block,
2027                 mode, con, tp);
2028 }
2029
2030
2031 ir_node *
2032 new_d_Id (dbg_info* db, ir_node *val, ir_mode *mode)
2033 {
2034   return new_rd_Id(db, current_ir_graph, current_ir_graph->current_block,
2035            val, mode);
2036 }
2037
2038 ir_node *
2039 new_d_Proj (dbg_info* db, ir_node *arg, ir_mode *mode, long proj)
2040 {
2041   return new_rd_Proj(db, current_ir_graph, current_ir_graph->current_block,
2042              arg, mode, proj);
2043 }
2044
2045 ir_node *
2046 new_d_defaultProj (dbg_info* db, ir_node *arg, long max_proj)
2047 {
2048   ir_node *res;
2049   assert(arg->op == op_Cond);
2050   arg->attr.c.kind = fragmentary;
2051   arg->attr.c.default_proj = max_proj;
2052   res = new_Proj (arg, mode_X, max_proj);
2053   return res;
2054 }
2055
2056 ir_node *
2057 new_d_Conv (dbg_info* db, ir_node *op, ir_mode *mode)
2058 {
2059   return new_rd_Conv(db, current_ir_graph, current_ir_graph->current_block,
2060              op, mode);
2061 }
2062
2063 ir_node *
2064 new_d_Cast (dbg_info* db, ir_node *op, type *to_tp)
2065 {
2066   return new_rd_Cast(db, current_ir_graph, current_ir_graph->current_block, op, to_tp);
2067 }
2068
2069 ir_node *
2070 new_d_Tuple (dbg_info* db, int arity, ir_node **in)
2071 {
2072   return new_rd_Tuple(db, current_ir_graph, current_ir_graph->current_block,
2073               arity, in);
2074 }
2075
2076 ir_node *
2077 new_d_Add (dbg_info* db, ir_node *op1, ir_node *op2, ir_mode *mode)
2078 {
2079   return new_rd_Add(db, current_ir_graph, current_ir_graph->current_block,
2080             op1, op2, mode);
2081 }
2082
2083 ir_node *
2084 new_d_Sub (dbg_info* db, ir_node *op1, ir_node *op2, ir_mode *mode)
2085 {
2086   return new_rd_Sub(db, current_ir_graph, current_ir_graph->current_block,
2087             op1, op2, mode);
2088 }
2089
2090
2091 ir_node *
2092 new_d_Minus (dbg_info* db, ir_node *op,  ir_mode *mode)
2093 {
2094   return new_rd_Minus(db, current_ir_graph, current_ir_graph->current_block,
2095               op, mode);
2096 }
2097
2098 ir_node *
2099 new_d_Mul (dbg_info* db, ir_node *op1, ir_node *op2, ir_mode *mode)
2100 {
2101   return new_rd_Mul(db, current_ir_graph, current_ir_graph->current_block,
2102             op1, op2, mode);
2103 }
2104
2105 /**
2106  * allocate the frag array
2107  */
2108 static void allocate_frag_arr(ir_node *res, ir_op *op, ir_node ***frag_store) {
2109   if (get_opt_precise_exc_context()) {
2110     if ((current_ir_graph->phase_state == phase_building) &&
2111     (get_irn_op(res) == op) && /* Could be optimized away. */
2112     !*frag_store)    /* Could be a cse where the arr is already set. */ {
2113       *frag_store = new_frag_arr(res);
2114     }
2115   }
2116 }
2117
2118
2119 ir_node *
2120 new_d_Quot (dbg_info* db, ir_node *memop, ir_node *op1, ir_node *op2)
2121 {
2122   ir_node *res;
2123   res = new_rd_Quot (db, current_ir_graph, current_ir_graph->current_block,
2124              memop, op1, op2);
2125   res->attr.except.pin_state = op_pin_state_pinned;
2126 #if PRECISE_EXC_CONTEXT
2127   allocate_frag_arr(res, op_Quot, &res->attr.except.frag_arr);  /* Could be optimized away. */
2128 #endif
2129
2130   return res;
2131 }
2132
2133 ir_node *
2134 new_d_DivMod (dbg_info* db, ir_node *memop, ir_node *op1, ir_node *op2)
2135 {
2136   ir_node *res;
2137   res = new_rd_DivMod (db, current_ir_graph, current_ir_graph->current_block,
2138                memop, op1, op2);
2139   res->attr.except.pin_state = op_pin_state_pinned;
2140 #if PRECISE_EXC_CONTEXT
2141   allocate_frag_arr(res, op_DivMod, &res->attr.except.frag_arr);  /* Could be optimized away. */
2142 #endif
2143
2144   return res;
2145 }
2146
2147 ir_node *
2148 new_d_Div (dbg_info* db, ir_node *memop, ir_node *op1, ir_node *op2)
2149 {
2150   ir_node *res;
2151   res = new_rd_Div (db, current_ir_graph, current_ir_graph->current_block,
2152             memop, op1, op2);
2153   res->attr.except.pin_state = op_pin_state_pinned;
2154 #if PRECISE_EXC_CONTEXT
2155   allocate_frag_arr(res, op_Div, &res->attr.except.frag_arr);  /* Could be optimized away. */
2156 #endif
2157
2158   return res;
2159 }
2160
2161 ir_node *
2162 new_d_Mod (dbg_info* db, ir_node *memop, ir_node *op1, ir_node *op2)
2163 {
2164   ir_node *res;
2165   res = new_rd_Mod (db, current_ir_graph, current_ir_graph->current_block,
2166             memop, op1, op2);
2167   res->attr.except.pin_state = op_pin_state_pinned;
2168 #if PRECISE_EXC_CONTEXT
2169   allocate_frag_arr(res, op_Mod, &res->attr.except.frag_arr);  /* Could be optimized away. */
2170 #endif
2171
2172   return res;
2173 }
2174
2175 ir_node *
2176 new_d_And (dbg_info* db, ir_node *op1, ir_node *op2, ir_mode *mode)
2177 {
2178   return new_rd_And (db, current_ir_graph, current_ir_graph->current_block,
2179             op1, op2, mode);
2180 }
2181
2182 ir_node *
2183 new_d_Or (dbg_info* db, ir_node *op1, ir_node *op2, ir_mode *mode)
2184 {
2185   return new_rd_Or (db, current_ir_graph, current_ir_graph->current_block,
2186            op1, op2, mode);
2187 }
2188
2189 ir_node *
2190 new_d_Eor (dbg_info* db, ir_node *op1, ir_node *op2, ir_mode *mode)
2191 {
2192   return new_rd_Eor (db, current_ir_graph, current_ir_graph->current_block,
2193             op1, op2, mode);
2194 }
2195
2196 ir_node *
2197 new_d_Not (dbg_info* db, ir_node *op, ir_mode *mode)
2198 {
2199   return new_rd_Not (db, current_ir_graph, current_ir_graph->current_block,
2200             op, mode);
2201 }
2202
2203 ir_node *
2204 new_d_Shl (dbg_info* db, ir_node *op, ir_node *k, ir_mode *mode)
2205 {
2206   return new_rd_Shl (db, current_ir_graph, current_ir_graph->current_block,
2207             op, k, mode);
2208 }
2209
2210 ir_node *
2211 new_d_Shr (dbg_info* db, ir_node *op, ir_node *k, ir_mode *mode)
2212 {
2213   return new_rd_Shr (db, current_ir_graph, current_ir_graph->current_block,
2214             op, k, mode);
2215 }
2216
2217 ir_node *
2218 new_d_Shrs (dbg_info* db, ir_node *op, ir_node *k, ir_mode *mode)
2219 {
2220   return new_rd_Shrs (db, current_ir_graph, current_ir_graph->current_block,
2221              op, k, mode);
2222 }
2223
2224 ir_node *
2225 new_d_Rot (dbg_info* db, ir_node *op, ir_node *k, ir_mode *mode)
2226 {
2227   return new_rd_Rot (db, current_ir_graph, current_ir_graph->current_block,
2228              op, k, mode);
2229 }
2230
2231 ir_node *
2232 new_d_Abs (dbg_info* db, ir_node *op, ir_mode *mode)
2233 {
2234   return new_rd_Abs (db, current_ir_graph, current_ir_graph->current_block,
2235             op, mode);
2236 }
2237
2238 ir_node *
2239 new_d_Cmp (dbg_info* db, ir_node *op1, ir_node *op2)
2240 {
2241   return new_rd_Cmp (db, current_ir_graph, current_ir_graph->current_block,
2242             op1, op2);
2243 }
2244
2245 ir_node *
2246 new_d_Jmp (dbg_info* db)
2247 {
2248   return new_rd_Jmp (db, current_ir_graph, current_ir_graph->current_block);
2249 }
2250
2251 ir_node *
2252 new_d_IJmp (dbg_info* db, ir_node *tgt)
2253 {
2254   return new_rd_IJmp (db, current_ir_graph, current_ir_graph->current_block, tgt);
2255 }
2256
2257 ir_node *
2258 new_d_Cond (dbg_info* db, ir_node *c)
2259 {
2260   return new_rd_Cond (db, current_ir_graph, current_ir_graph->current_block, c);
2261 }
2262
2263 ir_node *
2264 new_d_Call (dbg_info* db, ir_node *store, ir_node *callee, int arity, ir_node **in,
2265       type *tp)
2266 {
2267   ir_node *res;
2268   res = new_rd_Call (db, current_ir_graph, current_ir_graph->current_block,
2269              store, callee, arity, in, tp);
2270 #if PRECISE_EXC_CONTEXT
2271   allocate_frag_arr(res, op_Call, &res->attr.call.exc.frag_arr);  /* Could be optimized away. */
2272 #endif
2273
2274   return res;
2275 }
2276
2277 ir_node *
2278 new_d_Return (dbg_info* db, ir_node* store, int arity, ir_node **in)
2279 {
2280   return new_rd_Return (db, current_ir_graph, current_ir_graph->current_block,
2281                store, arity, in);
2282 }
2283
2284 ir_node *
2285 new_d_Raise (dbg_info* db, ir_node *store, ir_node *obj)
2286 {
2287   return new_rd_Raise (db, current_ir_graph, current_ir_graph->current_block,
2288               store, obj);
2289 }
2290
2291 ir_node *
2292 new_d_Load (dbg_info* db, ir_node *store, ir_node *addr, ir_mode *mode)
2293 {
2294   ir_node *res;
2295   res = new_rd_Load (db, current_ir_graph, current_ir_graph->current_block,
2296              store, addr, mode);
2297 #if PRECISE_EXC_CONTEXT
2298   allocate_frag_arr(res, op_Load, &res->attr.load.exc.frag_arr);  /* Could be optimized away. */
2299 #endif
2300
2301   return res;
2302 }
2303
2304 ir_node *
2305 new_d_Store (dbg_info* db, ir_node *store, ir_node *addr, ir_node *val)
2306 {
2307   ir_node *res;
2308   res = new_rd_Store (db, current_ir_graph, current_ir_graph->current_block,
2309               store, addr, val);
2310 #if PRECISE_EXC_CONTEXT
2311   allocate_frag_arr(res, op_Store, &res->attr.store.exc.frag_arr);  /* Could be optimized away. */
2312 #endif
2313
2314   return res;
2315 }
2316
2317 ir_node *
2318 new_d_Alloc (dbg_info* db, ir_node *store, ir_node *size, type *alloc_type,
2319            where_alloc where)
2320 {
2321   ir_node *res;
2322   res = new_rd_Alloc (db, current_ir_graph, current_ir_graph->current_block,
2323               store, size, alloc_type, where);
2324 #if PRECISE_EXC_CONTEXT
2325   allocate_frag_arr(res, op_Alloc, &res->attr.a.exc.frag_arr);  /* Could be optimized away. */
2326 #endif
2327
2328   return res;
2329 }
2330
2331 ir_node *
2332 new_d_Free (dbg_info* db, ir_node *store, ir_node *ptr,
2333     ir_node *size, type *free_type, where_alloc where)
2334 {
2335   return new_rd_Free (db, current_ir_graph, current_ir_graph->current_block,
2336              store, ptr, size, free_type, where);
2337 }
2338
2339 ir_node *
2340 new_d_simpleSel (dbg_info* db, ir_node *store, ir_node *objptr, entity *ent)
2341 /* GL: objptr was called frame before.  Frame was a bad choice for the name
2342    as the operand could as well be a pointer to a dynamic object. */
2343 {
2344   return new_rd_Sel (db, current_ir_graph, current_ir_graph->current_block,
2345             store, objptr, 0, NULL, ent);
2346 }
2347
2348 ir_node *
2349 new_d_Sel (dbg_info* db, ir_node *store, ir_node *objptr, int n_index, ir_node **index, entity *sel)
2350 {
2351   return new_rd_Sel (db, current_ir_graph, current_ir_graph->current_block,
2352             store, objptr, n_index, index, sel);
2353 }
2354
2355 ir_node *
2356 new_d_InstOf (dbg_info *db, ir_node *store, ir_node *objptr, type *ent)
2357 {
2358   return (new_rd_InstOf (db, current_ir_graph, current_ir_graph->current_block,
2359                          store, objptr, ent));
2360 }
2361
2362 ir_node *
2363 new_d_SymConst_type (dbg_info* db, symconst_symbol value, symconst_kind kind, type *tp)
2364 {
2365   return new_rd_SymConst_type (db, current_ir_graph, current_ir_graph->start_block,
2366                          value, kind, tp);
2367 }
2368
2369 ir_node *
2370 new_d_SymConst (dbg_info* db, symconst_symbol value, symconst_kind kind)
2371 {
2372   return new_rd_SymConst (db, current_ir_graph, current_ir_graph->start_block,
2373                          value, kind);
2374 }
2375
2376 ir_node *
2377 new_d_Sync (dbg_info* db, int arity, ir_node** in)
2378 {
2379   return new_rd_Sync (db, current_ir_graph, current_ir_graph->current_block,
2380               arity, in);
2381 }
2382
2383
2384 ir_node *
2385 (new_d_Bad)(void)
2386 {
2387   return _new_d_Bad();
2388 }
2389
2390 ir_node *
2391 new_d_Confirm (dbg_info *db, ir_node *val, ir_node *bound, pn_Cmp cmp)
2392 {
2393   return new_rd_Confirm (db, current_ir_graph, current_ir_graph->current_block,
2394              val, bound, cmp);
2395 }
2396
2397 ir_node *
2398 new_d_Unknown (ir_mode *m)
2399 {
2400   return new_rd_Unknown(current_ir_graph, m);
2401 }
2402
2403 ir_node *
2404 new_d_CallBegin (dbg_info *db, ir_node *call)
2405 {
2406   ir_node *res;
2407   res = new_rd_CallBegin (db, current_ir_graph, current_ir_graph->current_block, call);
2408   return res;
2409 }
2410
2411 ir_node *
2412 new_d_EndReg (dbg_info *db)
2413 {
2414   ir_node *res;
2415   res = new_rd_EndReg(db, current_ir_graph, current_ir_graph->current_block);
2416   return res;
2417 }
2418
2419 ir_node *
2420 new_d_EndExcept (dbg_info *db)
2421 {
2422   ir_node *res;
2423   res = new_rd_EndExcept(db, current_ir_graph, current_ir_graph->current_block);
2424   return res;
2425 }
2426
2427 ir_node *
2428 new_d_Break (dbg_info *db)
2429 {
2430   return new_rd_Break (db, current_ir_graph, current_ir_graph->current_block);
2431 }
2432
2433 ir_node *
2434 new_d_Filter (dbg_info *db, ir_node *arg, ir_mode *mode, long proj)
2435 {
2436   return new_rd_Filter (db, current_ir_graph, current_ir_graph->current_block,
2437             arg, mode, proj);
2438 }
2439
2440 ir_node *
2441 (new_d_NoMem)(void)
2442 {
2443   return _new_d_NoMem();
2444 }
2445
2446 ir_node *
2447 new_d_Mux (dbg_info *db, ir_node *sel, ir_node *ir_false,
2448     ir_node *ir_true, ir_mode *mode) {
2449   return new_rd_Mux (db, current_ir_graph, current_ir_graph->current_block,
2450       sel, ir_false, ir_true, mode);
2451 }
2452
2453 /* ********************************************************************* */
2454 /* Comfortable interface with automatic Phi node construction.           */
2455 /* (Uses also constructors of ?? interface, except new_Block.            */
2456 /* ********************************************************************* */
2457
2458 /* * Block construction **/
2459 /* immature Block without predecessors */
2460 ir_node *new_d_immBlock (dbg_info* db) {
2461   ir_node *res;
2462
2463   assert(get_irg_phase_state (current_ir_graph) == phase_building);
2464   /* creates a new dynamic in-array as length of in is -1 */
2465   res = new_ir_node (db, current_ir_graph, NULL, op_Block, mode_BB, -1, NULL);
2466   current_ir_graph->current_block = res;
2467   res->attr.block.matured     = 0;
2468   res->attr.block.dead        = 0;
2469   /* res->attr.block.exc = exc_normal; */
2470   /* res->attr.block.handler_entry = 0; */
2471   res->attr.block.irg         = current_ir_graph;
2472   res->attr.block.backedge    = NULL;
2473   res->attr.block.in_cg       = NULL;
2474   res->attr.block.cg_backedge = NULL;
2475   set_Block_block_visited(res, 0);
2476
2477   /* Create and initialize array for Phi-node construction. */
2478   res->attr.block.graph_arr = NEW_ARR_D (ir_node *, current_ir_graph->obst,
2479                                          current_ir_graph->n_loc);
2480   memset(res->attr.block.graph_arr, 0, sizeof(ir_node *)*current_ir_graph->n_loc);
2481
2482   /* Immature block may not be optimized! */
2483   IRN_VRFY_IRG(res, current_ir_graph);
2484
2485   return res;
2486 }
2487
2488 ir_node *
2489 new_immBlock (void) {
2490   return new_d_immBlock(NULL);
2491 }
2492
2493 /* add an edge to a jmp/control flow node */
2494 void
2495 add_immBlock_pred (ir_node *block, ir_node *jmp)
2496 {
2497   if (block->attr.block.matured) {
2498     assert(0 && "Error: Block already matured!\n");
2499   }
2500   else {
2501     assert(jmp != NULL);
2502     ARR_APP1(ir_node *, block->in, jmp);
2503   }
2504 }
2505
2506 /* changing the current block */
2507 void
2508 set_cur_block (ir_node *target)
2509 {
2510   current_ir_graph->current_block = target;
2511 }
2512
2513 /* ************************ */
2514 /* parameter administration */
2515
2516 /* get a value from the parameter array from the current block by its index */
2517 ir_node *
2518 get_d_value (dbg_info* db, int pos, ir_mode *mode)
2519 {
2520   assert(get_irg_phase_state (current_ir_graph) == phase_building);
2521   inc_irg_visited(current_ir_graph);
2522
2523   return get_r_value_internal (current_ir_graph->current_block, pos + 1, mode);
2524 }
2525 /* get a value from the parameter array from the current block by its index */
2526 ir_node *
2527 get_value (int pos, ir_mode *mode)
2528 {
2529   return get_d_value(NULL, pos, mode);
2530 }
2531
2532 /* set a value at position pos in the parameter array from the current block */
2533 void
2534 set_value (int pos, ir_node *value)
2535 {
2536   assert(get_irg_phase_state (current_ir_graph) == phase_building);
2537   assert(pos+1 < current_ir_graph->n_loc);
2538   current_ir_graph->current_block->attr.block.graph_arr[pos + 1] = value;
2539 }
2540
2541 /* get the current store */
2542 ir_node *
2543 get_store (void)
2544 {
2545   assert(get_irg_phase_state (current_ir_graph) == phase_building);
2546   /* GL: one could call get_value instead */
2547   inc_irg_visited(current_ir_graph);
2548   return get_r_value_internal (current_ir_graph->current_block, 0, mode_M);
2549 }
2550
2551 /* set the current store */
2552 void
2553 set_store (ir_node *store)
2554 {
2555   /* GL: one could call set_value instead */
2556   assert(get_irg_phase_state (current_ir_graph) == phase_building);
2557   current_ir_graph->current_block->attr.block.graph_arr[0] = store;
2558 }
2559
2560 void
2561 keep_alive (ir_node *ka) {
2562   add_End_keepalive(current_ir_graph->end, ka);
2563 }
2564
2565 /** Useful access routines **/
2566 /* Returns the current block of the current graph.  To set the current
2567    block use set_cur_block. */
2568 ir_node *get_cur_block() {
2569   return get_irg_current_block(current_ir_graph);
2570 }
2571
2572 /* Returns the frame type of the current graph */
2573 type *get_cur_frame_type() {
2574   return get_irg_frame_type(current_ir_graph);
2575 }
2576
2577
2578 /* ********************************************************************* */
2579 /* initialize */
2580
2581 /* call once for each run of the library */
2582 void
2583 init_cons(uninitialized_local_variable_func_t *func)
2584 {
2585   default_initialize_local_variable = func;
2586 }
2587
2588 /* call for each graph */
2589 void
2590 irg_finalize_cons (ir_graph *irg) {
2591   irg->phase_state = phase_high;
2592 }
2593
2594 void
2595 irp_finalize_cons (void) {
2596   int i, n_irgs = get_irp_n_irgs();
2597   for (i = 0; i < n_irgs; i++) {
2598     irg_finalize_cons(get_irp_irg(i));
2599   }
2600   irp->phase_state = phase_high;\
2601 }
2602
2603
2604
2605
2606 ir_node *new_Block(int arity, ir_node **in) {
2607   return new_d_Block(NULL, arity, in);
2608 }
2609 ir_node *new_Start  (void) {
2610   return new_d_Start(NULL);
2611 }
2612 ir_node *new_End    (void) {
2613   return new_d_End(NULL);
2614 }
2615 ir_node *new_Jmp    (void) {
2616   return new_d_Jmp(NULL);
2617 }
2618 ir_node *new_IJmp   (ir_node *tgt) {
2619   return new_d_IJmp(NULL, tgt);
2620 }
2621 ir_node *new_Cond   (ir_node *c) {
2622   return new_d_Cond(NULL, c);
2623 }
2624 ir_node *new_Return (ir_node *store, int arity, ir_node *in[]) {
2625   return new_d_Return(NULL, store, arity, in);
2626 }
2627 ir_node *new_Raise  (ir_node *store, ir_node *obj) {
2628   return new_d_Raise(NULL, store, obj);
2629 }
2630 ir_node *new_Const  (ir_mode *mode, tarval *con) {
2631   return new_d_Const(NULL, mode, con);
2632 }
2633
2634 ir_node *new_Const_long(ir_mode *mode, long value)
2635 {
2636     return new_d_Const_long(NULL, mode, value);
2637 }
2638
2639 ir_node *new_Const_type(tarval *con, type *tp) {
2640   return new_d_Const_type(NULL, get_type_mode(tp), con, tp);
2641 }
2642
2643 ir_node *new_SymConst (symconst_symbol value, symconst_kind kind) {
2644   return new_d_SymConst(NULL, value, kind);
2645 }
2646 ir_node *new_simpleSel(ir_node *store, ir_node *objptr, entity *ent) {
2647   return new_d_simpleSel(NULL, store, objptr, ent);
2648 }
2649 ir_node *new_Sel    (ir_node *store, ir_node *objptr, int arity, ir_node **in,
2650                      entity *ent) {
2651   return new_d_Sel(NULL, store, objptr, arity, in, ent);
2652 }
2653 ir_node *new_InstOf (ir_node *store, ir_node *objptr, type *ent) {
2654   return new_d_InstOf (NULL, store, objptr, ent);
2655 }
2656 ir_node *new_Call   (ir_node *store, ir_node *callee, int arity, ir_node **in,
2657              type *tp) {
2658   return new_d_Call(NULL, store, callee, arity, in, tp);
2659 }
2660 ir_node *new_Add    (ir_node *op1, ir_node *op2, ir_mode *mode) {
2661   return new_d_Add(NULL, op1, op2, mode);
2662 }
2663 ir_node *new_Sub    (ir_node *op1, ir_node *op2, ir_mode *mode) {
2664   return new_d_Sub(NULL, op1, op2, mode);
2665 }
2666 ir_node *new_Minus  (ir_node *op,  ir_mode *mode) {
2667   return new_d_Minus(NULL, op, mode);
2668 }
2669 ir_node *new_Mul    (ir_node *op1, ir_node *op2, ir_mode *mode) {
2670   return new_d_Mul(NULL, op1, op2, mode);
2671 }
2672 ir_node *new_Quot   (ir_node *memop, ir_node *op1, ir_node *op2) {
2673   return new_d_Quot(NULL, memop, op1, op2);
2674 }
2675 ir_node *new_DivMod (ir_node *memop, ir_node *op1, ir_node *op2) {
2676   return new_d_DivMod(NULL, memop, op1, op2);
2677 }
2678 ir_node *new_Div    (ir_node *memop, ir_node *op1, ir_node *op2) {
2679   return new_d_Div(NULL, memop, op1, op2);
2680 }
2681 ir_node *new_Mod    (ir_node *memop, ir_node *op1, ir_node *op2) {
2682   return new_d_Mod(NULL, memop, op1, op2);
2683 }
2684 ir_node *new_Abs    (ir_node *op, ir_mode *mode) {
2685   return new_d_Abs(NULL, op, mode);
2686 }
2687 ir_node *new_And    (ir_node *op1, ir_node *op2, ir_mode *mode) {
2688   return new_d_And(NULL, op1, op2, mode);
2689 }
2690 ir_node *new_Or     (ir_node *op1, ir_node *op2, ir_mode *mode) {
2691   return new_d_Or(NULL, op1, op2, mode);
2692 }
2693 ir_node *new_Eor    (ir_node *op1, ir_node *op2, ir_mode *mode) {
2694   return new_d_Eor(NULL, op1, op2, mode);
2695 }
2696 ir_node *new_Not    (ir_node *op,                ir_mode *mode) {
2697   return new_d_Not(NULL, op, mode);
2698 }
2699 ir_node *new_Shl    (ir_node *op,  ir_node *k,   ir_mode *mode) {
2700   return new_d_Shl(NULL, op, k, mode);
2701 }
2702 ir_node *new_Shr    (ir_node *op,  ir_node *k,   ir_mode *mode) {
2703   return new_d_Shr(NULL, op, k, mode);
2704 }
2705 ir_node *new_Shrs   (ir_node *op,  ir_node *k,   ir_mode *mode) {
2706   return new_d_Shrs(NULL, op, k, mode);
2707 }
2708 #define new_Rotate new_Rot
2709 ir_node *new_Rot    (ir_node *op,  ir_node *k,   ir_mode *mode) {
2710   return new_d_Rot(NULL, op, k, mode);
2711 }
2712 ir_node *new_Cmp    (ir_node *op1, ir_node *op2) {
2713   return new_d_Cmp(NULL, op1, op2);
2714 }
2715 ir_node *new_Conv   (ir_node *op, ir_mode *mode) {
2716   return new_d_Conv(NULL, op, mode);
2717 }
2718 ir_node *new_Cast   (ir_node *op, type *to_tp) {
2719   return new_d_Cast(NULL, op, to_tp);
2720 }
2721 ir_node *new_Phi    (int arity, ir_node **in, ir_mode *mode) {
2722   return new_d_Phi(NULL, arity, in, mode);
2723 }
2724 ir_node *new_Load   (ir_node *store, ir_node *addr, ir_mode *mode) {
2725   return new_d_Load(NULL, store, addr, mode);
2726 }
2727 ir_node *new_Store  (ir_node *store, ir_node *addr, ir_node *val) {
2728   return new_d_Store(NULL, store, addr, val);
2729 }
2730 ir_node *new_Alloc  (ir_node *store, ir_node *size, type *alloc_type,
2731                      where_alloc where) {
2732   return new_d_Alloc(NULL, store, size, alloc_type, where);
2733 }
2734 ir_node *new_Free   (ir_node *store, ir_node *ptr, ir_node *size,
2735              type *free_type, where_alloc where) {
2736   return new_d_Free(NULL, store, ptr, size, free_type, where);
2737 }
2738 ir_node *new_Sync   (int arity, ir_node **in) {
2739   return new_d_Sync(NULL, arity, in);
2740 }
2741 ir_node *new_Proj   (ir_node *arg, ir_mode *mode, long proj) {
2742   return new_d_Proj(NULL, arg, mode, proj);
2743 }
2744 ir_node *new_defaultProj (ir_node *arg, long max_proj) {
2745   return new_d_defaultProj(NULL, arg, max_proj);
2746 }
2747 ir_node *new_Tuple  (int arity, ir_node **in) {
2748   return new_d_Tuple(NULL, arity, in);
2749 }
2750 ir_node *new_Id     (ir_node *val, ir_mode *mode) {
2751   return new_d_Id(NULL, val, mode);
2752 }
2753 ir_node *new_Bad    (void) {
2754   return new_d_Bad();
2755 }
2756 ir_node *new_Confirm (ir_node *val, ir_node *bound, pn_Cmp cmp) {
2757   return new_d_Confirm (NULL, val, bound, cmp);
2758 }
2759 ir_node *new_Unknown(ir_mode *m) {
2760   return new_d_Unknown(m);
2761 }
2762 ir_node *new_CallBegin (ir_node *callee) {
2763   return new_d_CallBegin(NULL, callee);
2764 }
2765 ir_node *new_EndReg (void) {
2766   return new_d_EndReg(NULL);
2767 }
2768 ir_node *new_EndExcept (void) {
2769   return new_d_EndExcept(NULL);
2770 }
2771 ir_node *new_Break  (void) {
2772   return new_d_Break(NULL);
2773 }
2774 ir_node *new_Filter (ir_node *arg, ir_mode *mode, long proj) {
2775   return new_d_Filter(NULL, arg, mode, proj);
2776 }
2777 ir_node *new_NoMem  (void) {
2778   return new_d_NoMem();
2779 }
2780 ir_node *new_Mux (ir_node *sel, ir_node *ir_false, ir_node *ir_true, ir_mode *mode) {
2781   return new_d_Mux(NULL, sel, ir_false, ir_true, mode);
2782 }