Changed file names.
[libfirm] / ir / ir / ircons.c
1 /* Copyright (C) 1998 - 2000 by Universitaet Karlsruhe
2 ** All rights reserved.
3 **
4 ** Authors: Martin Trapp, Christian Schaefer
5 **
6 ** ircons.c: basic and more detailed irnode constructors
7 **           store, block and parameter administration.
8 ** Adapted to extended FIRM nodes (exceptions...) and commented
9 **   by Goetz Lindenmaier
10 */
11
12 /* $Id$ */
13
14 #ifdef HAVE_CONFIG_H
15 # include <config.h>
16 #endif
17
18 # include "irgraph_t.h"
19 # include "irnode_t.h"
20 # include "irmode_t.h"
21 # include "ircons.h"
22 # include "common_t.h"
23 # include "irvrfy.h"
24 # include "irop.h"
25 # include "iropt_t.h"
26 # include "irgmod.h"
27 # include "array.h"
28 /* memset belongs to string.h */
29 # include "string.h"
30
31 /* # include "exc.h" */
32
33 #if USE_EXPLICIT_PHI_IN_STACK
34 /* A stack needed for the automatic Phi node construction in constructor
35    Phi_in. Redefinition in irgraph.c!! */
36 struct Phi_in_stack {
37   ir_node **stack;
38   int       pos;
39 };
40 typedef struct Phi_in_stack Phi_in_stack;
41 #endif
42
43 /*** ******************************************** */
44 /** privat interfaces, for professional use only */
45
46 /* Constructs a Block with a fixed number of predecessors.
47    Does not set current_block.  Can not be used with automatic
48    Phi node construction. */
49 inline ir_node *
50 new_r_Block (ir_graph *irg,  int arity, ir_node **in)
51 {
52   ir_node *res;
53
54   res = new_ir_node (irg, NULL, op_Block, mode_R, arity, in);
55   set_Block_matured(res, 1);
56   set_Block_block_visited(res, 0);
57
58   res->attr.block.exc = exc_normal;
59
60   irn_vrfy (res);
61   return res;
62 }
63
64 ir_node *
65 new_r_Start (ir_graph *irg, ir_node *block)
66 {
67   ir_node *res;
68
69   res = new_ir_node (irg, block, op_Start, mode_T, 0, NULL);
70
71   irn_vrfy (res);
72   return res;
73 }
74
75 ir_node *
76 new_r_End (ir_graph *irg, ir_node *block)
77 {
78   ir_node *res;
79
80   res = new_ir_node (irg, block, op_End, mode_X, -1, NULL);
81
82   irn_vrfy (res);
83   return res;
84 }
85
86 /* Creates a Phi node with all predecessors.  Calling this constructor
87    is only allowed if the corresponding block is mature.  */
88 ir_node *
89 new_r_Phi (ir_graph *irg, ir_node *block, int arity, ir_node **in, ir_mode *mode)
90 {
91   ir_node *res;
92
93   assert( get_Block_matured(block) );
94   assert( get_irn_arity(block) == arity );
95
96   res = new_ir_node (irg, block, op_Phi, mode, arity, in);
97
98   res = optimize (res);
99   irn_vrfy (res);
100
101   /* Memory Phis in endless loops must be kept alive.
102      As we can't distinguish these easily we keep all of them alive. */
103   if ((res->op == op_Phi) && (mode == mode_M))
104     add_End_keepalive(irg->end, res);
105   return res;
106 }
107
108 ir_node *
109 new_r_Const (ir_graph *irg, ir_node *block, ir_mode *mode, tarval *con)
110 {
111   ir_node *res;
112   res = new_ir_node (irg, block, op_Const, mode, 0, NULL);
113   res->attr.con = con;
114   res = optimize (res);
115   irn_vrfy (res);
116
117 #if 0
118   res = local_optimize_newby (res);
119 # endif
120
121   return res;
122 }
123
124 ir_node *
125 new_r_Id (ir_graph *irg, ir_node *block, ir_node *val, ir_mode *mode)
126 {
127   ir_node *in[1] = {val};
128   ir_node *res;
129   res = new_ir_node (irg, block, op_Id, mode, 1, in);
130   res = optimize (res);
131   irn_vrfy (res);
132   return res;
133 }
134
135 ir_node *
136 new_r_Proj (ir_graph *irg, ir_node *block, ir_node *arg, ir_mode *mode,
137             long proj)
138 {
139   ir_node *in[1] = {arg};
140   ir_node *res;
141   res = new_ir_node (irg, block, op_Proj, mode, 1, in);
142   res->attr.proj = proj;
143
144   assert(res);
145   assert(get_Proj_pred(res));
146   assert(get_nodes_Block(get_Proj_pred(res)));
147
148   res = optimize (res);
149
150   irn_vrfy (res);
151   return res;
152
153 }
154
155 ir_node *
156 new_r_defaultProj (ir_graph *irg, ir_node *block, ir_node *arg,
157                    long max_proj)
158 {
159   ir_node *res;
160   assert((arg->op==op_Cond) && (get_irn_mode(arg->in[1]) == mode_I));
161   arg->attr.c.kind = fragmentary;
162   arg->attr.c.default_proj = max_proj;
163   res = new_r_Proj (irg, block, arg, mode_X, max_proj);
164   return res;
165 }
166
167 ir_node *
168 new_r_Conv (ir_graph *irg, ir_node *block, ir_node *op, ir_mode *mode)
169 {
170   ir_node *in[1] = {op};
171   ir_node *res;
172   res = new_ir_node (irg, block, op_Conv, mode, 1, in);
173   res = optimize (res);
174   irn_vrfy (res);
175   return res;
176
177 }
178
179 ir_node *
180 new_r_Tuple (ir_graph *irg, ir_node *block, int arity, ir_node **in)
181 {
182   ir_node *res;
183
184   res = new_ir_node (irg, block, op_Tuple, mode_T, arity, in);
185   res = optimize (res);
186   irn_vrfy (res);
187   return res;
188 }
189
190 inline ir_node *
191 new_r_Add (ir_graph *irg, ir_node *block,
192            ir_node *op1, ir_node *op2, ir_mode *mode)
193 {
194   ir_node *in[2] = {op1, op2};
195   ir_node *res;
196   res = new_ir_node (irg, block, op_Add, mode, 2, in);
197   res = optimize (res);
198   irn_vrfy (res);
199   return res;
200 }
201
202 inline ir_node *
203 new_r_Sub (ir_graph *irg, ir_node *block,
204            ir_node *op1, ir_node *op2, ir_mode *mode)
205 {
206   ir_node *in[2] = {op1, op2};
207   ir_node *res;
208   res = new_ir_node (irg, block, op_Sub, mode, 2, in);
209   res = optimize (res);
210   irn_vrfy (res);
211   return res;
212 }
213
214 inline ir_node *
215 new_r_Minus (ir_graph *irg, ir_node *block,
216              ir_node *op,  ir_mode *mode)
217 {
218   ir_node *in[1] = {op};
219   ir_node *res;
220   res = new_ir_node (irg, block, op_Minus, mode, 1, in);
221   res = optimize (res);
222   irn_vrfy (res);
223   return res;
224 }
225
226 inline ir_node *
227 new_r_Mul (ir_graph *irg, ir_node *block,
228            ir_node *op1, ir_node *op2, ir_mode *mode)
229 {
230   ir_node *in[2] = {op1, op2};
231   ir_node *res;
232   res = new_ir_node (irg, block, op_Mul, mode, 2, in);
233   res = optimize (res);
234   irn_vrfy (res);
235   return res;
236 }
237
238 inline ir_node *
239 new_r_Quot (ir_graph *irg, ir_node *block,
240             ir_node *memop, ir_node *op1, ir_node *op2)
241 {
242   ir_node *in[3] = {memop, op1, op2};
243   ir_node *res;
244   res = new_ir_node (irg, block, op_Quot, mode_T, 3, in);
245   res = optimize (res);
246   irn_vrfy (res);
247   return res;
248 }
249
250 inline ir_node *
251 new_r_DivMod (ir_graph *irg, ir_node *block,
252               ir_node *memop, ir_node *op1, ir_node *op2)
253 {
254   ir_node *in[3] = {memop, op1, op2};
255   ir_node *res;
256   res = new_ir_node (irg, block, op_DivMod, mode_T, 3, in);
257   res = optimize (res);
258   irn_vrfy (res);
259   return res;
260 }
261
262 inline ir_node *
263 new_r_Div (ir_graph *irg, ir_node *block,
264            ir_node *memop, ir_node *op1, ir_node *op2)
265 {
266   ir_node *in[3] = {memop, op1, op2};
267   ir_node *res;
268   res = new_ir_node (irg, block, op_Div, mode_T, 3, in);
269   res = optimize (res);
270   irn_vrfy (res);
271   return res;
272 }
273
274 inline ir_node *
275 new_r_Mod (ir_graph *irg, ir_node *block,
276            ir_node *memop, ir_node *op1, ir_node *op2)
277 {
278   ir_node *in[3] = {memop, op1, op2};
279   ir_node *res;
280   res = new_ir_node (irg, block, op_Mod, mode_T, 3, in);
281   res = optimize (res);
282   irn_vrfy (res);
283   return res;
284 }
285
286 inline ir_node *
287 new_r_And (ir_graph *irg, ir_node *block,
288            ir_node *op1, ir_node *op2, ir_mode *mode)
289 {
290   ir_node *in[2] = {op1, op2};
291   ir_node *res;
292   res = new_ir_node (irg, block, op_And, mode, 2, in);
293   res = optimize (res);
294   irn_vrfy (res);
295   return res;
296 }
297
298 inline ir_node *
299 new_r_Or (ir_graph *irg, ir_node *block,
300           ir_node *op1, ir_node *op2, ir_mode *mode)
301 {
302   ir_node *in[2] = {op1, op2};
303   ir_node *res;
304   res = new_ir_node (irg, block, op_Or, mode, 2, in);
305   res = optimize (res);
306   irn_vrfy (res);
307   return res;
308 }
309
310 inline ir_node *
311 new_r_Eor (ir_graph *irg, ir_node *block,
312           ir_node *op1, ir_node *op2, ir_mode *mode)
313 {
314   ir_node *in[2] = {op1, op2};
315   ir_node *res;
316   res = new_ir_node (irg, block, op_Eor, mode, 2, in);
317   res = optimize (res);
318   irn_vrfy (res);
319   return res;
320 }
321
322 inline ir_node *
323 new_r_Not    (ir_graph *irg, ir_node *block,
324               ir_node *op, ir_mode *mode)
325 {
326   ir_node *in[1] = {op};
327   ir_node *res;
328   res = new_ir_node (irg, block, op_Not, mode, 1, in);
329   res = optimize (res);
330   irn_vrfy (res);
331   return res;
332 }
333
334 inline ir_node *
335 new_r_Shl (ir_graph *irg, ir_node *block,
336           ir_node *op, ir_node *k, ir_mode *mode)
337 {
338   ir_node *in[2] = {op, k};
339   ir_node *res;
340   res = new_ir_node (irg, block, op_Shl, mode, 2, in);
341   res = optimize (res);
342   irn_vrfy (res);
343   return res;
344 }
345
346 inline ir_node *
347 new_r_Shr (ir_graph *irg, ir_node *block,
348            ir_node *op, ir_node *k, ir_mode *mode)
349 {
350   ir_node *in[2] = {op, k};
351   ir_node *res;
352   res = new_ir_node (irg, block, op_Shr, mode, 2, in);
353   res = optimize (res);
354   irn_vrfy (res);
355   return res;
356 }
357
358 inline ir_node *
359 new_r_Shrs (ir_graph *irg, ir_node *block,
360            ir_node *op, ir_node *k, ir_mode *mode)
361 {
362   ir_node *in[2] = {op, k};
363   ir_node *res;
364   res = new_ir_node (irg, block, op_Shrs, mode, 2, in);
365   res = optimize (res);
366   irn_vrfy (res);
367   return res;
368 }
369
370 inline ir_node *
371 new_r_Rot (ir_graph *irg, ir_node *block,
372            ir_node *op, ir_node *k, ir_mode *mode)
373 {
374   ir_node *in[2] = {op, k};
375   ir_node *res;
376   res = new_ir_node (irg, block, op_Rot, mode, 2, in);
377   res = optimize (res);
378   irn_vrfy (res);
379   return res;
380 }
381
382 inline ir_node *
383 new_r_Abs (ir_graph *irg, ir_node *block,
384            ir_node *op, ir_mode *mode)
385 {
386   ir_node *in[1] = {op};
387   ir_node *res;
388   res = new_ir_node (irg, block, op_Abs, mode, 1, in);
389   res = optimize (res);
390   irn_vrfy (res);
391   return res;
392 }
393
394 inline ir_node *
395 new_r_Cmp (ir_graph *irg, ir_node *block,
396            ir_node *op1, ir_node *op2)
397 {
398   ir_node *in[2] = {op1, op2};
399   ir_node *res;
400   res = new_ir_node (irg, block, op_Cmp, mode_T, 2, in);
401   res = optimize (res);
402   irn_vrfy (res);
403   return res;
404 }
405
406 inline ir_node *
407 new_r_Jmp (ir_graph *irg, ir_node *block)
408 {
409   ir_node *in[0] = {};
410   ir_node *res;
411   res = new_ir_node (irg, block, op_Jmp, mode_X, 0, in);
412   res = optimize (res);
413   irn_vrfy (res);
414   return res;
415 }
416
417 inline ir_node *
418 new_r_Cond (ir_graph *irg, ir_node *block, ir_node *c)
419 {
420   ir_node *in[1] = {c};
421   ir_node *res;
422   res = new_ir_node (irg, block, op_Cond, mode_T, 1, in);
423   res->attr.c.kind = dense;
424   res->attr.c.default_proj = 0;
425   res = optimize (res);
426   irn_vrfy (res);
427   return res;
428 }
429
430 ir_node *
431 new_r_Call (ir_graph *irg, ir_node *block, ir_node *store,
432             ir_node *callee, int arity, ir_node **in, type *type)
433 {
434   ir_node **r_in;
435   ir_node *res;
436   int r_arity;
437
438   r_arity = arity+2;
439   NEW_ARR_A (ir_node *, r_in, r_arity);
440   r_in[0] = store;
441   r_in[1] = callee;
442   memcpy (&r_in[2], in, sizeof (ir_node *) * arity);
443
444   res = new_ir_node (irg, block, op_Call, mode_T, r_arity, r_in);
445
446   assert(is_method_type(type));
447   set_Call_type(res, type);
448   res = optimize (res);
449   irn_vrfy (res);
450   return res;
451 }
452
453 ir_node *
454 new_r_Return (ir_graph *irg, ir_node *block,
455               ir_node *store, int arity, ir_node **in)
456 {
457   ir_node **r_in;
458   ir_node *res;
459   int r_arity;
460
461   r_arity = arity+1;
462   NEW_ARR_A (ir_node *, r_in, r_arity);
463   r_in[0] = store;
464   memcpy (&r_in[1], in, sizeof (ir_node *) * arity);
465   res = new_ir_node (irg, block, op_Return, mode_X, r_arity, r_in);
466   res = optimize (res);
467   irn_vrfy (res);
468   return res;
469 }
470
471 inline ir_node *
472 new_r_Raise (ir_graph *irg, ir_node *block, ir_node *store, ir_node *obj)
473 {
474   ir_node *in[2] = {store, obj};
475   ir_node *res;
476   res = new_ir_node (irg, block, op_Raise, mode_T, 2, in);
477   res = optimize (res);
478   irn_vrfy (res);
479   return res;
480 }
481
482 inline ir_node *
483 new_r_Load (ir_graph *irg, ir_node *block,
484             ir_node *store, ir_node *adr)
485 {
486   ir_node *in[2] = {store, adr};
487   ir_node *res;
488   res = new_ir_node (irg, block, op_Load, mode_T, 2, in);
489
490   res = optimize (res);
491   irn_vrfy (res);
492   return res;
493 }
494
495 inline ir_node *
496 new_r_Store (ir_graph *irg, ir_node *block,
497              ir_node *store, ir_node *adr, ir_node *val)
498 {
499   ir_node *in[3] = {store, adr, val};
500   ir_node *res;
501   res = new_ir_node (irg, block, op_Store, mode_T, 3, in);
502
503   res = optimize (res);
504
505   irn_vrfy (res);
506   return res;
507 }
508
509 inline ir_node *
510 new_r_Alloc (ir_graph *irg, ir_node *block, ir_node *store,
511             ir_node *size, type *alloc_type, where_alloc where)
512 {
513   ir_node *in[2] = {store, size};
514   ir_node *res;
515   res = new_ir_node (irg, block, op_Alloc, mode_T, 2, in);
516
517   res->attr.a.where = where;
518   res->attr.a.type = alloc_type;
519
520   res = optimize (res);
521   irn_vrfy (res);
522   return res;
523 }
524
525 inline ir_node *
526 new_r_Free (ir_graph *irg, ir_node *block, ir_node *store,
527             ir_node *ptr, ir_node *size, type *free_type)
528 {
529   ir_node *in[3] = {store, ptr, size};
530   ir_node *res;
531   res = new_ir_node (irg, block, op_Free, mode_T, 3, in);
532
533   res->attr.f = free_type;
534
535   res = optimize (res);
536   irn_vrfy (res);
537   return res;
538 }
539
540 inline ir_node *
541 new_r_Sel (ir_graph *irg, ir_node *block, ir_node *store, ir_node *objptr,
542            int arity, ir_node **in, entity *ent)
543 {
544   ir_node **r_in;
545   ir_node *res;
546   int r_arity;
547
548   r_arity = arity + 2;
549   NEW_ARR_A (ir_node *, r_in, r_arity);
550   r_in[0] = store;
551   r_in[1] = objptr;
552   memcpy (&r_in[2], in, sizeof (ir_node *) * arity);
553   res = new_ir_node (irg, block, op_Sel, mode_p, r_arity, r_in);
554
555   res->attr.s.ltyp = static_linkage;
556   res->attr.s.ent = ent;
557
558   res = optimize (res);
559   irn_vrfy (res);
560   return res;
561 }
562
563 inline ir_node *
564 new_r_SymConst (ir_graph *irg, ir_node *block, type_or_id_p value,
565                 symconst_kind symkind)
566 {
567   ir_node *in[0] = {};
568   ir_node *res;
569   ir_mode *mode;
570   if (symkind == linkage_ptr_info)
571     mode = mode_p;
572   else
573     mode = mode_I;
574   res = new_ir_node (irg, block, op_SymConst, mode, 0, in);
575
576   res->attr.i.num = symkind;
577   if (symkind == linkage_ptr_info) {
578     res->attr.i.tori.ptrinfo = (ident *)value;
579   } else {
580     assert (   (   (symkind == type_tag)
581                 || (symkind == size))
582             && (is_type(value)));
583     res->attr.i.tori.typ = (type *)value;
584   }
585   res = optimize (res);
586   irn_vrfy (res);
587   return res;
588 }
589
590 ir_node *
591 new_r_Sync (ir_graph *irg, ir_node *block, int arity, ir_node **in)
592 {
593   ir_node *res;
594
595   res = new_ir_node (irg, block, op_Sync, mode_M, arity, in);
596
597   res = optimize (res);
598   irn_vrfy (res);
599   return res;
600 }
601
602 ir_node *
603 new_r_Bad ()
604 {
605   return current_ir_graph->bad;
606 }
607
608 /** ********************/
609 /** public interfaces  */
610 /** construction tools */
611
612 /****f* ircons/new_Start
613  *
614  * NAME
615  *   new_Start -- create a new Start node in the current block
616  *
617  * SYNOPSIS
618  *   s = new_Start(void);
619  *   ir_node* new_Start(void);
620  *
621  * RESULT
622  *   s - pointer to the created Start node
623  *
624  ****
625  */
626 ir_node *
627 new_Start (void)
628 {
629   ir_node *res;
630
631   res = new_ir_node (current_ir_graph, current_ir_graph->current_block,
632                      op_Start, mode_T, 0, NULL);
633
634   res = optimize (res);
635   irn_vrfy (res);
636   return res;
637 }
638
639 ir_node *
640 new_End (void)
641 {
642   ir_node *res;
643   res = new_ir_node (current_ir_graph,  current_ir_graph->current_block,
644                      op_End, mode_X, -1, NULL);
645   res = optimize (res);
646   irn_vrfy (res);
647
648   return res;
649 }
650
651 /* Constructs a Block with a fixed number of predecessors.
652    Does set current_block.  Can be used with automatic Phi
653    node construction. */
654 ir_node *
655 new_Block (int arity, ir_node **in)
656 {
657   ir_node *res;
658
659   res = new_r_Block (current_ir_graph, arity, in);
660
661   /* Create and initialize array for Phi-node construction. */
662   res->attr.block.graph_arr = NEW_ARR_D (ir_node *, current_ir_graph->obst,
663                                          current_ir_graph->n_loc);
664   memset(res->attr.block.graph_arr, 0, sizeof(ir_node *)*current_ir_graph->n_loc);
665
666   res = optimize (res);
667   current_ir_graph->current_block = res;
668
669   irn_vrfy (res);
670
671   return res;
672 }
673
674 /* ***********************************************************************/
675 /* Methods necessary for automatic Phi node creation                     */
676 /*
677   ir_node *phi_merge            (ir_node *block, int pos, ir_mode *mode, ir_node **nin, int ins)
678   ir_node *get_r_value_internal (ir_node *block, int pos, ir_mode *mode);
679   ir_node *new_r_Phi0           (ir_graph *irg, ir_node *block, ir_mode *mode)
680   ir_node *new_r_Phi_in         (ir_graph *irg, ir_node *block, ir_mode *mode,  ir_node **in, int ins)
681
682   Call Graph:   ( A ---> B == A "calls" B)
683
684        get_value         mature_block
685           |                   |
686           |                   |
687           |                   |
688           |          ---> phi_merge
689           |         /       /   \
690           |        /       /     \
691          \|/      /      |/_      \
692        get_r_value_internal        |
693                 |                  |
694                 |                  |
695                \|/                \|/
696             new_r_Phi0          new_r_Phi_in
697
698 * *************************************************************************** */
699
700 /* Creates a Phi node with 0 predecessors */
701 inline ir_node *
702 new_r_Phi0 (ir_graph *irg, ir_node *block, ir_mode *mode)
703 {
704   ir_node *res;
705   res = new_ir_node (irg, block, op_Phi, mode, 0, NULL);
706   irn_vrfy (res);
707   return res;
708 }
709
710 /* There are two implementations of the Phi node construction.  The first
711    is faster, but does not work for blocks with more than 2 predecessors.
712    The second works always but is slower and causes more unnecessary Phi
713    nodes.
714    Select the implementations by the following preprocessor flag set in
715    common/common.h: */
716 #if USE_FAST_PHI_CONSTRUCTION
717
718 /* This is a stack used for allocating and deallocating nodes in
719    new_r_Phi_in.  The original implementation used the obstack
720    to model this stack, now it is explicit.  This reduces side effects.
721 */
722 #if USE_EXPLICIT_PHI_IN_STACK
723 Phi_in_stack *
724 new_Phi_in_stack() {
725   Phi_in_stack *res;
726
727   res = (Phi_in_stack *) malloc ( sizeof (Phi_in_stack));
728
729   res->stack = NEW_ARR_F (ir_node *, 1);
730   res->pos = 0;
731
732   return res;
733 }
734
735 void
736 free_Phi_in_stack(Phi_in_stack *s) {
737   DEL_ARR_F(s->stack);
738   free(s);
739 }
740
741 void free_to_Phi_in_stack(ir_node *phi) {
742   assert(get_irn_opcode(phi) == iro_Phi);
743
744   if (ARR_LEN(current_ir_graph->Phi_in_stack->stack) ==
745       current_ir_graph->Phi_in_stack->pos)
746     ARR_APP1 (ir_node *, current_ir_graph->Phi_in_stack->stack, phi);
747   else
748     current_ir_graph->Phi_in_stack->stack[current_ir_graph->Phi_in_stack->pos] = phi;
749
750   (current_ir_graph->Phi_in_stack->pos)++;
751 }
752
753 ir_node *
754 alloc_or_pop_from_Phi_in_stack(ir_graph *irg, ir_node *block, ir_mode *mode,
755              int arity, ir_node **in) {
756   ir_node *res;
757   ir_node **stack = current_ir_graph->Phi_in_stack->stack;
758   int pos = current_ir_graph->Phi_in_stack->pos;
759
760
761   if (pos == 0) {
762     /* We need to allocate a new node */
763     res = new_ir_node (irg, block, op_Phi, mode, arity, in);
764   } else {
765     /* reuse the old node and initialize it again. */
766     res = stack[pos-1];
767
768     assert (res->kind == k_ir_node);
769     assert (res->op == op_Phi);
770     res->mode = mode;
771     res->visited = 0;
772     res->link = NULL;
773     assert (arity >= 0);
774     /* ???!!! How to free the old in array??  */
775     res->in = NEW_ARR_D (ir_node *, irg->obst, (arity+1));
776     res->in[0] = block;
777     memcpy (&res->in[1], in, sizeof (ir_node *) * arity);
778
779     (current_ir_graph->Phi_in_stack->pos)--;
780   }
781   return res;
782 }
783 #endif /* USE_EXPLICIT_PHI_IN_STACK */
784
785 /* Creates a Phi node with a given, fixed array **in of predecessors.
786    If the Phi node is unnecessary, as the same value reaches the block
787    through all control flow paths, it is eliminated and the value
788    returned directly.  This constructor is only intended for use in
789    the automatic Phi node generation triggered by get_value or mature.
790    The implementation is quite tricky and depends on the fact, that
791    the nodes are allocated on a stack:
792    The in array contains predecessors and NULLs.  The NULLs appear,
793    if get_r_value_internal, that computed the predecessors, reached
794    the same block on two paths.  In this case the same value reaches
795    this block on both paths, there is no definition in between.  We need
796    not allocate a Phi where these path's merge, but we have to communicate
797    this fact to the caller.  This happens by returning a pointer to the
798    node the caller _will_ allocate.  (Yes, we predict the address. We can
799    do so because the nodes are allocated on the obstack.)  The caller then
800    finds a pointer to itself and, when this routine is called again,
801    eliminates itself.
802    */
803 inline ir_node *
804 new_r_Phi_in (ir_graph *irg, ir_node *block, ir_mode *mode,
805               ir_node **in, int ins)
806 {
807   int i;
808   ir_node *res, *known;
809
810   /* allocate a new node on the obstack.
811      This can return a node to which some of the pointers in the in-array
812      already point.
813      Attention: the constructor copies the in array, i.e., the later changes
814      to the array in this routine do not affect the constructed node!  If
815      the in array contains NULLs, there will be missing predecessors in the
816      returned node.
817      Is this a possible internal state of the Phi node generation? */
818 #if USE_EXPLICIT_PHI_IN_STACK
819   res = known = alloc_or_pop_from_Phi_in_stack(irg, block, mode, ins, in);
820 #else
821   res = known = new_ir_node (irg, block, op_Phi, mode, ins, in);
822 #endif
823   /* The in-array can contain NULLs.  These were returned by
824      get_r_value_internal if it reached the same block/definition on a
825      second path.
826      The NULLs are replaced by the node itself to simplify the test in the
827      next loop. */
828   for (i=0;  i < ins;  ++i)
829     if (in[i] == NULL) in[i] = res;
830
831   /* This loop checks whether the Phi has more than one predecessor.
832      If so, it is a real Phi node and we break the loop.  Else the
833      Phi node merges the same definition on several paths and therefore
834      is not needed. */
835   for (i=0;  i < ins;  ++i)
836   {
837     if (in[i]==res || in[i]==known) continue;
838
839     if (known==res)
840       known = in[i];
841     else
842       break;
843   }
844
845   /* i==ins: there is at most one predecessor, we don't need a phi node. */
846   if (i==ins) {
847 #if USE_EXPLICIT_PHI_IN_STACK
848     free_to_Phi_in_stack(res);
849 #else
850     obstack_free (current_ir_graph->obst, res);
851 #endif
852     res = known;
853   } else {
854     res = optimize (res);
855     irn_vrfy (res);
856   }
857
858   /* return the pointer to the Phi node.  This node might be deallocated! */
859   return res;
860 }
861
862 inline ir_node *
863 get_r_value_internal (ir_node *block, int pos, ir_mode *mode);
864
865 /** This function computes the predecessors for a real Phi node, and then
866     allocates and returns this node.  The routine called to allocate the
867     node might optimize it away and return a real value, or even a pointer
868     to a deallocated Phi node on top of the obstack!
869     This function is called with an in-array of proper size. **/
870 static inline ir_node *
871 phi_merge (ir_node *block, int pos, ir_mode *mode, ir_node **nin, int ins)
872 {
873   ir_node *prevBlock, *res;
874   int i;
875
876   /* This loop goes to all predecessor blocks of the block the Phi node is in
877      and there finds the operands of the Phi node by calling
878      get_r_value_internal. */
879   for (i = 1;  i <= ins;  ++i) {
880     assert (block->in[i]);
881     prevBlock = block->in[i]->in[0]; /* go past control flow op to prev block */
882     assert (prevBlock);
883     nin[i-1] = get_r_value_internal (prevBlock, pos, mode);
884   }
885
886   /* After collecting all predecessors into the array nin a new Phi node
887      with these predecessors is created.  This constructor contains an
888      optimization: If all predecessors of the Phi node are identical it
889      returns the only operand instead of a new Phi node.  If the value
890      passes two different control flow edges without being defined, and
891      this is the second path treated, a pointer to the node that will be
892      allocated for the first path (recursion) is returned.  We already
893      know the address of this node, as it is the next node to be allocated
894      and will be placed on top of the obstack. (The obstack is a _stack_!) */
895   res = new_r_Phi_in (current_ir_graph, block, mode, nin, ins);
896
897   /* Now we now the value for "pos" and can enter it in the array with
898      all known local variables.  Attention: this might be a pointer to
899      a node, that later will be allocated!!! See new_r_Phi_in.
900      If this is called in mature, after some set_value in the same block,
901      the proper value must not be overwritten:
902      The call order
903        get_value    (makes Phi0, put's it into graph_arr)
904        set_value    (overwrites Phi0 in graph_arr)
905        mature_block (upgrades Phi0, puts it again into graph_arr, overwriting
906                      the proper value.)
907      fails. */
908   if (!block->attr.block.graph_arr[pos]) {
909     block->attr.block.graph_arr[pos] = res;
910   } else {
911     /*  printf(" value already computed by %s\n",
912         id_to_str(block->attr.block.graph_arr[pos]->op->name));  */
913   }
914
915   return res;
916 }
917
918 /* This function returns the last definition of a variable.  In case
919    this variable was last defined in a previous block, Phi nodes are
920    inserted.  If the part of the firm graph containing the definition
921    is not yet constructed, a dummy Phi node is returned. */
922 inline ir_node *
923 get_r_value_internal (ir_node *block, int pos, ir_mode *mode)
924 {
925   ir_node *res;
926   /* There are 4 cases to treat.
927
928      1. The block is not mature and we visit it the first time.  We can not
929         create a proper Phi node, therefore a Phi0, i.e., a Phi without
930         predecessors is returned.  This node is added to the linked list (field
931         "link") of the containing block to be completed when this block is
932         matured. (Completion will add a new Phi and turn the Phi0 into an Id
933         node.)
934
935      2. The value is already known in this block, graph_arr[pos] is set and we
936         visit the block the first time.  We can return the value without
937         creating any new nodes.
938
939      3. The block is mature and we visit it the first time.  A Phi node needs
940         to be created (phi_merge).  If the Phi is not needed, as all it's
941         operands are the same value reaching the block through different
942         paths, it's optimized away and the value itself is returned.
943
944      4. The block is mature, and we visit it the second time.  Now two
945         subcases are possible:
946         * The value was computed completely the last time we were here. This
947           is the case if there is no loop.  We can return the proper value.
948         * The recursion that visited this node and set the flag did not
949           return yet.  We are computing a value in a loop and need to
950           break the recursion without knowing the result yet.
951           @@@ strange case.  Straight forward we would create a Phi before
952           starting the computation of it's predecessors.  In this case we will
953           find a Phi here in any case.  The problem is that this implementation
954           only creates a Phi after computing the predecessors, so that it is
955           hard to compute self references of this Phi.  @@@
956         There is no simple check for the second subcase.  Therefore we check
957         for a second visit and treat all such cases as the second subcase.
958         Anyways, the basic situation is the same:  we reached a block
959         on two paths without finding a definition of the value:  No Phi
960         nodes are needed on both paths.
961         We return this information "Two paths, no Phi needed" by a very tricky
962         implementation that relies on the fact that an obstack is a stack and
963         will return a node with the same address on different allocations.
964         Look also at phi_merge and new_r_phi_in to understand this.
965         @@@ Unfortunately this does not work, see testprogram
966         three_cfpred_example.
967
968   */
969
970   /* case 4 -- already visited. */
971   if (get_irn_visited(block) == get_irg_visited(current_ir_graph)) return NULL;
972
973   /* visited the first time */
974   set_irn_visited(block, get_irg_visited(current_ir_graph));
975
976   /* Get the local valid value */
977   res = block->attr.block.graph_arr[pos];
978
979   /* case 2 -- If the value is actually computed, return it. */
980   if (res) { return res;};
981
982   if (block->attr.block.matured) { /* case 3 */
983
984     /* The Phi has the same amount of ins as the corresponding block. */
985     int ins = get_irn_arity(block);
986     ir_node **nin;
987     NEW_ARR_A (ir_node *, nin, ins);
988
989     /* Phi merge collects the predecessors and then creates a node. */
990     res = phi_merge (block, pos, mode, nin, ins);
991
992   } else {  /* case 1 */
993     /* The block is not mature, we don't know how many in's are needed.  A Phi
994        with zero predecessors is created.  Such a Phi node is called Phi0
995        node.  (There is also an obsolete Phi0 opcode.) The Phi0 is then added
996        to the list of Phi0 nodes in this block to be matured by mature_block
997        later.
998        The Phi0 has to remember the pos of it's internal value.  If the real
999        Phi is computed, pos is used to update the array with the local
1000        values. */
1001
1002     res = new_r_Phi0 (current_ir_graph, block, mode);
1003     res->attr.phi0_pos = pos;
1004     res->link = block->link;
1005     block->link = res;
1006   }
1007
1008   /* If we get here, the frontend missed a use-before-definition error */
1009   if (!res) {
1010     /* Error Message */
1011     printf("Error: no value set.  Use of undefined variable.  Initializing
1012             to zero.\n");
1013     assert (mode->code >= irm_f && mode->code <= irm_p);
1014     res = new_r_Const (current_ir_graph, block, mode,
1015                        tarval_mode_null[mode->code]);
1016   }
1017
1018   /* The local valid value is available now. */
1019   block->attr.block.graph_arr[pos] = res;
1020
1021   return res;
1022 }
1023
1024 #else /* if 0 */
1025
1026 /** This is the simple algorithm.  If first generates a Phi0, then
1027     it starts the recursion.  This causes an Id at the entry of
1028     every block that has no definition of the value! **/
1029
1030 #if USE_EXPLICIT_PHI_IN_STACK
1031 /* Just dummies */
1032 Phi_in_stack * new_Phi_in_stack() {  return NULL; }
1033 void free_Phi_in_stack(Phi_in_stack *s) { }
1034 #endif
1035
1036 inline ir_node *
1037 new_r_Phi_in (ir_graph *irg, ir_node *block, ir_mode *mode,
1038               ir_node **in, int ins)
1039 {
1040   int i;
1041   ir_node *res, *known;
1042
1043   /* Allocate a new node on the obstack.  The allocation copies the in
1044      array. */
1045   res = new_ir_node (irg, block, op_Phi, mode, ins, in);
1046
1047   /* This loop checks whether the Phi has more than one predecessor.
1048      If so, it is a real Phi node and we break the loop.  Else the
1049      Phi node merges the same definition on several paths and therefore
1050      is not needed. Don't consider Bad nodes! */
1051   known = res;
1052   for (i=0;  i < ins;  ++i)
1053   {
1054         assert(in[i]);
1055
1056     if (in[i]==res || in[i]==known || is_Bad(in[i])) continue;
1057
1058     if (known==res)
1059       known = in[i];
1060     else
1061       break;
1062   }
1063
1064   /* i==ins: there is at most one predecessor, we don't need a phi node. */
1065   if (i==ins) {
1066     if (res != known) {
1067       obstack_free (current_ir_graph->obst, res);
1068       res = known;
1069     } else {
1070       /* A undefined value, e.g., in unreachable code. */
1071       res = new_Bad();
1072     }
1073   } else {
1074     res = optimize (res);
1075     irn_vrfy (res);
1076     /* Memory Phis in endless loops must be kept alive.
1077        As we can't distinguish these easily we keep all of the alive. */
1078     if ((res->op == op_Phi) && (mode == mode_M))
1079       add_End_keepalive(irg->end, res);
1080   }
1081
1082   return res;
1083 }
1084
1085 inline ir_node *
1086 get_r_value_internal (ir_node *block, int pos, ir_mode *mode);
1087
1088 #if PRECISE_EXC_CONTEXT
1089 static inline ir_node *
1090 phi_merge (ir_node *block, int pos, ir_mode *mode, ir_node **nin, int ins);
1091
1092 ir_node **
1093 new_frag_arr (ir_node *n) {
1094   ir_node **arr;
1095   int opt;
1096   arr = NEW_ARR_D (ir_node *, current_ir_graph->obst, current_ir_graph->n_loc);
1097   memcpy(arr, current_ir_graph->current_block->attr.block.graph_arr,
1098          sizeof(ir_node *)*current_ir_graph->n_loc);
1099   /* turn off optimization before allocating Proj nodes, as res isn't
1100      finished yet. */
1101   opt = get_optimize(); set_optimize(0);
1102   /* Here we rely on the fact that all frag ops have Memory as first result! */
1103   if (get_irn_op(n) == op_Call)
1104     arr[0] = new_Proj(n, mode_M, 3);
1105   else
1106     arr[0] = new_Proj(n, mode_M, 0);
1107   set_optimize(opt);
1108   current_ir_graph->current_block->attr.block.graph_arr[current_ir_graph->n_loc-1] = n;
1109   return arr;
1110 }
1111
1112 inline ir_node **
1113 get_frag_arr (ir_node *n) {
1114   if (get_irn_op(n) == op_Call) {
1115     return n->attr.call.frag_arr;
1116   } else if (get_irn_op(n) == op_Alloc) {
1117     return n->attr.a.frag_arr;
1118   } else {
1119     return n->attr.frag_arr;
1120   }
1121 }
1122
1123 inline ir_node *
1124 set_frag_value(ir_node **frag_arr, int pos, ir_node *val) {
1125   if (!frag_arr[pos]) frag_arr[pos] = val;
1126   if (frag_arr[current_ir_graph->n_loc - 1])
1127     set_frag_value (get_frag_arr(frag_arr[current_ir_graph->n_loc - 1]), pos, val);
1128 }
1129
1130 inline ir_node *
1131 get_r_frag_value_internal (ir_node *block, ir_node *cfOp, int pos, ir_mode *mode) {
1132   ir_node *res;
1133   ir_node **rem;
1134   ir_node **frag_arr;
1135
1136   assert(is_fragile_op(cfOp) && (get_irn_op(cfOp) != op_Bad));
1137
1138   frag_arr = get_frag_arr(cfOp);
1139   res = frag_arr[pos];
1140   if (!res) {
1141     if (block->attr.block.graph_arr[pos]) {
1142       /* There was a set_value after the cfOp and no get_value before that
1143                  set_value.  We must build a Phi node now. */
1144       if (block->attr.block.matured) {
1145                 int ins = get_irn_arity(block);
1146                 ir_node **nin;
1147                 NEW_ARR_A (ir_node *, nin, ins);
1148                 res = phi_merge(block, pos, mode, nin, ins);
1149       } else {
1150                 res = new_r_Phi0 (current_ir_graph, block, mode);
1151                 res->attr.phi0_pos = pos;
1152                 res->link = block->link;
1153                 block->link = res;
1154       }
1155       assert(res);
1156       /* @@@ tested by Flo: set_frag_value(frag_arr, pos, res);
1157                  but this should be better: (remove comment if this works) */
1158       /* It's a Phi, we can write this into all graph_arrs with NULL */
1159       set_frag_value(block->attr.block.graph_arr, pos, res);
1160     } else {
1161       res = get_r_value_internal(block, pos, mode);
1162       set_frag_value(block->attr.block.graph_arr, pos, res);
1163     }
1164   }
1165   return res;
1166 }
1167 #endif
1168
1169 /** This function allocates a dummy Phi node to break recursions,
1170     computes the predecessors for the real phi node, and then
1171     allocates and returns this node.  The routine called to allocate the
1172     node might optimize it away and return a real value.
1173     This function is called with an in-array of proper size. **/
1174 static inline ir_node *
1175 phi_merge (ir_node *block, int pos, ir_mode *mode, ir_node **nin, int ins)
1176 {
1177   ir_node *prevBlock, *prevCfOp, *res, *phi0;
1178   int i;
1179
1180   /* If this block has no value at pos create a Phi0 and remember it
1181      in graph_arr to break recursions.
1182      Else we may not set graph_arr as there a later value is remembered. */
1183   phi0 = NULL;
1184   if (!block->attr.block.graph_arr[pos]) {
1185     /* This is commented out as collapsing to Bads is no good idea.
1186        Either we need an assert here, or we need to call a routine
1187        that deals with this case as appropriate for the given language.
1188        Right now a self referencing Id is created which will crash irg_vrfy().
1189
1190        Even if all variables are defined before use, it can happen that
1191        we get to the start block, if a cond has been replaced by a tuple
1192        (bad, jmp).  As the start has a self referencing control flow edge,
1193        we get a self referencing Id, which is hard to optimize away.  We avoid
1194        this by defining the value as a Bad node.
1195        Returning a const with tarval_bad is a preliminary solution.  In some
1196        situations we might want a Warning or an Error. */
1197
1198     if (block == get_irg_start_block(current_ir_graph)) {
1199       block->attr.block.graph_arr[pos] = new_Const(mode, tarval_bad);
1200       /* We don't need to care about exception ops in the start block.
1201                  There are none by definition. */
1202       return block->attr.block.graph_arr[pos];
1203     } else {
1204       phi0 = new_r_Phi0(current_ir_graph, block, mode);
1205       block->attr.block.graph_arr[pos] = phi0;
1206 #if PRECISE_EXC_CONTEXT
1207       /* Set graph_arr for fragile ops.  Also here we should break recursion.
1208                  We could choose a cyclic path through an cfop.  But the recursion would
1209                  break at some point. */
1210       set_frag_value(block->attr.block.graph_arr, pos, phi0);
1211 #endif
1212     }
1213   }
1214
1215   /* This loop goes to all predecessor blocks of the block the Phi node
1216      is in and there finds the operands of the Phi node by calling
1217      get_r_value_internal.  */
1218   for (i = 1;  i <= ins;  ++i) {
1219     prevCfOp = skip_Proj(block->in[i]);
1220     assert (prevCfOp);
1221     if (is_Bad(prevCfOp)) {
1222       /* In case a Cond has been optimized we would get right to the start block
1223                  with an invalid definition. */
1224       nin[i-1] = new_Bad();
1225       continue;
1226     }
1227     prevBlock = block->in[i]->in[0]; /* go past control flow op to prev block */
1228     assert (prevBlock);
1229     if (!is_Bad(prevBlock)) {
1230 #if PRECISE_EXC_CONTEXT
1231       if (is_fragile_op(prevCfOp) && (get_irn_op (prevCfOp) != op_Bad)) {
1232                 assert(get_r_frag_value_internal (prevBlock, prevCfOp, pos, mode));
1233                 nin[i-1] = get_r_frag_value_internal (prevBlock, prevCfOp, pos, mode);
1234       } else
1235 #endif
1236                 nin[i-1] = get_r_value_internal (prevBlock, pos, mode);
1237     } else {
1238       nin[i-1] = new_Bad();
1239     }
1240   }
1241
1242   /* After collecting all predecessors into the array nin a new Phi node
1243      with these predecessors is created.  This constructor contains an
1244      optimization: If all predecessors of the Phi node are identical it
1245      returns the only operand instead of a new Phi node.  */
1246   res = new_r_Phi_in (current_ir_graph, block, mode, nin, ins);
1247
1248   /* In case we allocated a Phi0 node at the beginning of this procedure,
1249      we need to exchange this Phi0 with the real Phi. */
1250   if (phi0) {
1251     exchange(phi0, res);
1252     block->attr.block.graph_arr[pos] = res;
1253     /* Don't set_frag_value as it does not overwrite.  Doesn't matter, is
1254        only an optimization. */
1255   }
1256
1257   return res;
1258 }
1259
1260 /* This function returns the last definition of a variable.  In case
1261    this variable was last defined in a previous block, Phi nodes are
1262    inserted.  If the part of the firm graph containing the definition
1263    is not yet constructed, a dummy Phi node is returned. */
1264 inline ir_node *
1265 get_r_value_internal (ir_node *block, int pos, ir_mode *mode)
1266 {
1267   ir_node *res;
1268   /* There are 4 cases to treat.
1269
1270      1. The block is not mature and we visit it the first time.  We can not
1271         create a proper Phi node, therefore a Phi0, i.e., a Phi without
1272         predecessors is returned.  This node is added to the linked list (field
1273         "link") of the containing block to be completed when this block is
1274         matured. (Comlpletion will add a new Phi and turn the Phi0 into an Id
1275         node.)
1276
1277      2. The value is already known in this block, graph_arr[pos] is set and we
1278         visit the block the first time.  We can return the value without
1279         creating any new nodes.
1280
1281      3. The block is mature and we visit it the first time.  A Phi node needs
1282         to be created (phi_merge).  If the Phi is not needed, as all it's
1283         operands are the same value reaching the block through different
1284         paths, it's optimized away and the value itself is returned.
1285
1286      4. The block is mature, and we visit it the second time.  Now two
1287         subcases are possible:
1288         * The value was computed completely the last time we were here. This
1289           is the case if there is no loop.  We can return the proper value.
1290         * The recursion that visited this node and set the flag did not
1291           return yet.  We are computing a value in a loop and need to
1292           break the recursion.  This case only happens if we visited
1293           the same block with phi_merge before, which inserted a Phi0.
1294           So we return the Phi0.
1295   */
1296
1297   /* case 4 -- already visited. */
1298   if (get_irn_visited(block) == get_irg_visited(current_ir_graph)) {
1299     /* As phi_merge allocates a Phi0 this value is always defined. Here
1300      is the critical difference of the two algorithms. */
1301     assert(block->attr.block.graph_arr[pos]);
1302     return block->attr.block.graph_arr[pos];
1303   }
1304
1305   /* visited the first time */
1306   set_irn_visited(block, get_irg_visited(current_ir_graph));
1307
1308   /* Get the local valid value */
1309   res = block->attr.block.graph_arr[pos];
1310
1311   /* case 2 -- If the value is actually computed, return it. */
1312   if (res) { return res; };
1313
1314   if (block->attr.block.matured) { /* case 3 */
1315
1316     /* The Phi has the same amount of ins as the corresponding block. */
1317     int ins = get_irn_arity(block);
1318     ir_node **nin;
1319     NEW_ARR_A (ir_node *, nin, ins);
1320
1321     /* Phi merge collects the predecessors and then creates a node. */
1322     res = phi_merge (block, pos, mode, nin, ins);
1323
1324   } else {  /* case 1 */
1325     /* The block is not mature, we don't know how many in's are needed.  A Phi
1326        with zero predecessors is created.  Such a Phi node is called Phi0
1327        node.  The Phi0 is then added to the list of Phi0 nodes in this block
1328        to be matured by mature_block later.
1329        The Phi0 has to remember the pos of it's internal value.  If the real
1330        Phi is computed, pos is used to update the array with the local
1331        values. */
1332     res = new_r_Phi0 (current_ir_graph, block, mode);
1333     res->attr.phi0_pos = pos;
1334     res->link = block->link;
1335     block->link = res;
1336   }
1337
1338   /* If we get here, the frontend missed a use-before-definition error */
1339   if (!res) {
1340     /* Error Message */
1341     printf("Error: no value set.  Use of undefined variable.  Initializing
1342             to zero.\n");
1343     assert (mode->code >= irm_f && mode->code <= irm_p);
1344     res = new_r_Const (current_ir_graph, block, mode,
1345                        tarval_mode_null[mode->code]);
1346   }
1347
1348   /* The local valid value is available now. */
1349   block->attr.block.graph_arr[pos] = res;
1350
1351   return res;
1352 }
1353
1354 #endif /* USE_FAST_PHI_CONSTRUCTION */
1355
1356 /* ************************************************************************** */
1357
1358 /** Finalize a Block node, when all control flows are known.  */
1359 /** Acceptable parameters are only Block nodes.               */
1360 void
1361 mature_block (ir_node *block)
1362 {
1363
1364   int ins;
1365   ir_node *n, **nin;
1366   ir_node *next;
1367
1368   assert (get_irn_opcode(block) == iro_Block);
1369   // assert (!get_Block_matured(block) && "Block already matured");
1370
1371   if (!get_Block_matured(block)) {
1372
1373     /* An array for building the Phi nodes. */
1374     ins = ARR_LEN (block->in)-1;
1375     NEW_ARR_A (ir_node *, nin, ins);
1376     /* shouldn't we delete this array at the end of the procedure? @@@ memory leak? */
1377
1378     /* Traverse a chain of Phi nodes attached to this block and mature
1379        these, too. **/
1380     for (n = block->link;  n;  n=next) {
1381       inc_irg_visited(current_ir_graph);
1382       next = n->link;
1383       exchange (n, phi_merge (block, n->attr.phi0_pos, n->mode, nin, ins));
1384     }
1385
1386     block->attr.block.matured = 1;
1387
1388     /* Now, as the block is a finished firm node, we can optimize it.
1389        Since other nodes have been allocated since the block was created
1390        we can not free the node on the obstack.  Therefore we have to call
1391        optimize_in_place.
1392        Unfortunately the optimization does not change a lot, as all allocated
1393        nodes refer to the unoptimized node.
1394        We can call _2, as global cse has no effect on blocks. */
1395     block = optimize_in_place_2(block);
1396     irn_vrfy(block);
1397   }
1398 }
1399
1400 ir_node *
1401 new_Phi (int arity, ir_node **in, ir_mode *mode)
1402 {
1403   return new_r_Phi (current_ir_graph, current_ir_graph->current_block,
1404                     arity, in, mode);
1405 }
1406
1407 ir_node *
1408 new_Const (ir_mode *mode, tarval *con)
1409 {
1410   return new_r_Const (current_ir_graph, current_ir_graph->start_block,
1411                       mode, con);
1412 }
1413
1414 ir_node *
1415 new_Id (ir_node *val, ir_mode *mode)
1416 {
1417   return new_r_Id (current_ir_graph, current_ir_graph->current_block,
1418                    val, mode);
1419 }
1420
1421 ir_node *
1422 new_Proj (ir_node *arg, ir_mode *mode, long proj)
1423 {
1424   return new_r_Proj (current_ir_graph, current_ir_graph->current_block,
1425                      arg, mode, proj);
1426 }
1427
1428 ir_node *
1429 new_defaultProj (ir_node *arg, long max_proj)
1430 {
1431   ir_node *res;
1432   assert((arg->op==op_Cond) && (get_irn_mode(arg->in[1]) == mode_I));
1433   arg->attr.c.kind = fragmentary;
1434   arg->attr.c.default_proj = max_proj;
1435   res = new_Proj (arg, mode_X, max_proj);
1436   return res;
1437 }
1438
1439 ir_node *
1440 new_Conv (ir_node *op, ir_mode *mode)
1441 {
1442   return new_r_Conv (current_ir_graph, current_ir_graph->current_block,
1443                      op, mode);
1444 }
1445
1446 ir_node *
1447 new_Tuple (int arity, ir_node **in)
1448 {
1449   return new_r_Tuple (current_ir_graph, current_ir_graph->current_block,
1450                       arity, in);
1451 }
1452
1453 ir_node *
1454 new_Add (ir_node *op1, ir_node *op2, ir_mode *mode)
1455 {
1456   return new_r_Add (current_ir_graph, current_ir_graph->current_block,
1457                     op1, op2, mode);
1458 }
1459
1460 ir_node *
1461 new_Sub (ir_node *op1, ir_node *op2, ir_mode *mode)
1462 {
1463   return new_r_Sub (current_ir_graph, current_ir_graph->current_block,
1464                     op1, op2, mode);
1465 }
1466
1467
1468 ir_node *
1469 new_Minus  (ir_node *op,  ir_mode *mode)
1470 {
1471   return new_r_Minus (current_ir_graph, current_ir_graph->current_block,
1472                       op, mode);
1473 }
1474
1475 ir_node *
1476 new_Mul (ir_node *op1, ir_node *op2, ir_mode *mode)
1477 {
1478   return new_r_Mul (current_ir_graph, current_ir_graph->current_block,
1479                     op1, op2, mode);
1480 }
1481
1482 ir_node *
1483 new_Quot (ir_node *memop, ir_node *op1, ir_node *op2)
1484 {
1485   ir_node *res;
1486   res = new_r_Quot (current_ir_graph, current_ir_graph->current_block,
1487                      memop, op1, op2);
1488 #if PRECISE_EXC_CONTEXT
1489   if ((current_ir_graph->phase_state == phase_building) &&
1490       (get_irn_op(res) == op_Quot))  /* Could be optimized away. */
1491     res->attr.frag_arr = new_frag_arr(res);
1492 #endif
1493
1494   return res;
1495 }
1496
1497 ir_node *
1498 new_DivMod (ir_node *memop, ir_node *op1, ir_node *op2)
1499 {
1500   ir_node *res;
1501   res = new_r_DivMod (current_ir_graph, current_ir_graph->current_block,
1502                        memop, op1, op2);
1503 #if PRECISE_EXC_CONTEXT
1504   if ((current_ir_graph->phase_state == phase_building) &&
1505       (get_irn_op(res) == op_DivMod))   /* Could be optimized away. */
1506     res->attr.frag_arr = new_frag_arr(res);
1507 #endif
1508
1509   return res;
1510 }
1511
1512 ir_node *
1513 new_Div (ir_node *memop, ir_node *op1, ir_node *op2)
1514 {
1515   ir_node *res;
1516   res = new_r_Div (current_ir_graph, current_ir_graph->current_block,
1517                     memop, op1, op2);
1518 #if PRECISE_EXC_CONTEXT
1519   if ((current_ir_graph->phase_state == phase_building) &&
1520       (get_irn_op(res) == op_Div))  /* Could be optimized away. */
1521     res->attr.frag_arr = new_frag_arr(res);
1522 #endif
1523
1524   return res;
1525 }
1526
1527 ir_node *
1528 new_Mod (ir_node *memop, ir_node *op1, ir_node *op2)
1529 {
1530   ir_node *res;
1531   res = new_r_Mod (current_ir_graph, current_ir_graph->current_block,
1532                     memop, op1, op2);
1533 #if PRECISE_EXC_CONTEXT
1534   if ((current_ir_graph->phase_state == phase_building) &&
1535       (get_irn_op(res) == op_Mod))  /* Could be optimized away. */
1536     res->attr.frag_arr = new_frag_arr(res);
1537 #endif
1538
1539   return res;
1540 }
1541
1542 ir_node *
1543 new_And (ir_node *op1, ir_node *op2, ir_mode *mode)
1544 {
1545   return new_r_And (current_ir_graph, current_ir_graph->current_block,
1546                     op1, op2, mode);
1547 }
1548
1549 ir_node *
1550 new_Or (ir_node *op1, ir_node *op2, ir_mode *mode)
1551 {
1552   return new_r_Or (current_ir_graph, current_ir_graph->current_block,
1553                    op1, op2, mode);
1554 }
1555
1556 ir_node *
1557 new_Eor (ir_node *op1, ir_node *op2, ir_mode *mode)
1558 {
1559   return new_r_Eor (current_ir_graph, current_ir_graph->current_block,
1560                     op1, op2, mode);
1561 }
1562
1563 ir_node *
1564 new_Not (ir_node *op, ir_mode *mode)
1565 {
1566   return new_r_Not (current_ir_graph, current_ir_graph->current_block,
1567                     op, mode);
1568 }
1569
1570 ir_node *
1571 new_Shl (ir_node *op, ir_node *k, ir_mode *mode)
1572 {
1573   return new_r_Shl (current_ir_graph, current_ir_graph->current_block,
1574                     op, k, mode);
1575 }
1576
1577 ir_node *
1578 new_Shr (ir_node *op, ir_node *k, ir_mode *mode)
1579 {
1580   return new_r_Shr (current_ir_graph, current_ir_graph->current_block,
1581                     op, k, mode);
1582 }
1583
1584 ir_node *
1585 new_Shrs (ir_node *op, ir_node *k, ir_mode *mode)
1586 {
1587   return new_r_Shrs (current_ir_graph, current_ir_graph->current_block,
1588                      op, k, mode);
1589 }
1590
1591 ir_node *
1592 new_Rotate (ir_node *op, ir_node *k, ir_mode *mode)
1593 {
1594   return new_r_Rot (current_ir_graph, current_ir_graph->current_block,
1595                      op, k, mode);
1596 }
1597
1598 ir_node *
1599 new_Abs (ir_node *op, ir_mode *mode)
1600 {
1601   return new_r_Abs (current_ir_graph, current_ir_graph->current_block,
1602                     op, mode);
1603 }
1604
1605 ir_node *
1606 new_Cmp (ir_node *op1, ir_node *op2)
1607 {
1608   return new_r_Cmp (current_ir_graph, current_ir_graph->current_block,
1609                     op1, op2);
1610 }
1611
1612 ir_node *
1613 new_Jmp (void)
1614 {
1615   return new_r_Jmp (current_ir_graph, current_ir_graph->current_block);
1616 }
1617
1618 ir_node *
1619 new_Cond (ir_node *c)
1620 {
1621   return new_r_Cond (current_ir_graph, current_ir_graph->current_block, c);
1622 }
1623
1624 ir_node *
1625 new_Call (ir_node *store, ir_node *callee, int arity, ir_node **in,
1626           type *type)
1627 {
1628   ir_node *res;
1629   res = new_r_Call (current_ir_graph, current_ir_graph->current_block,
1630                      store, callee, arity, in, type);
1631 #if PRECISE_EXC_CONTEXT
1632   if ((current_ir_graph->phase_state == phase_building) &&
1633       (get_irn_op(res) == op_Call))  /* Could be optimized away. */
1634     res->attr.call.frag_arr = new_frag_arr(res);
1635 #endif
1636
1637   return res;
1638 }
1639
1640 ir_node *
1641 new_Return (ir_node* store, int arity, ir_node **in)
1642 {
1643   return new_r_Return (current_ir_graph, current_ir_graph->current_block,
1644                        store, arity, in);
1645 }
1646
1647 ir_node *
1648 new_Raise (ir_node *store, ir_node *obj)
1649 {
1650   return new_r_Raise (current_ir_graph, current_ir_graph->current_block,
1651                       store, obj);
1652 }
1653
1654 ir_node *
1655 new_Load (ir_node *store, ir_node *addr)
1656 {
1657   ir_node *res;
1658   res = new_r_Load (current_ir_graph, current_ir_graph->current_block,
1659                      store, addr);
1660 #if PRECISE_EXC_CONTEXT
1661   if ((current_ir_graph->phase_state == phase_building) &&
1662       (get_irn_op(res) == op_Load))  /* Could be optimized away. */
1663     res->attr.frag_arr = new_frag_arr(res);
1664 #endif
1665
1666   return res;
1667 }
1668
1669 ir_node *
1670 new_Store (ir_node *store, ir_node *addr, ir_node *val)
1671 {
1672   ir_node *res;
1673   res = new_r_Store (current_ir_graph, current_ir_graph->current_block,
1674                       store, addr, val);
1675 #if PRECISE_EXC_CONTEXT
1676   if ((current_ir_graph->phase_state == phase_building) &&
1677       (get_irn_op(res) == op_Store))  /* Could be optimized away. */
1678     res->attr.frag_arr = new_frag_arr(res);
1679 #endif
1680
1681   return res;
1682 }
1683
1684 ir_node *
1685 new_Alloc (ir_node *store, ir_node *size, type *alloc_type,
1686            where_alloc where)
1687 {
1688   ir_node *res;
1689   res = new_r_Alloc (current_ir_graph, current_ir_graph->current_block,
1690                       store, size, alloc_type, where);
1691 #if PRECISE_EXC_CONTEXT
1692   if ((current_ir_graph->phase_state == phase_building) &&
1693       (get_irn_op(res) == op_Alloc))  /* Could be optimized away. */
1694     res->attr.a.frag_arr = new_frag_arr(res);
1695 #endif
1696
1697   return res;
1698 }
1699
1700 ir_node *
1701 new_Free (ir_node *store, ir_node *ptr, ir_node *size, type *free_type)
1702 {
1703   return new_r_Free (current_ir_graph, current_ir_graph->current_block,
1704                      store, ptr, size, free_type);
1705 }
1706
1707 ir_node *
1708 new_simpleSel (ir_node *store, ir_node *objptr, entity *ent)
1709 /* GL: objptr was called frame before.  Frame was a bad choice for the name
1710    as the operand could as well be a pointer to a dynamic object. */
1711 {
1712   return new_r_Sel (current_ir_graph, current_ir_graph->current_block,
1713                     store, objptr, 0, NULL, ent);
1714 }
1715
1716 ir_node *
1717 new_Sel (ir_node *store, ir_node *objptr, int n_index, ir_node **index, entity *sel)
1718 {
1719   return new_r_Sel (current_ir_graph, current_ir_graph->current_block,
1720                     store, objptr, n_index, index, sel);
1721 }
1722
1723 ir_node *
1724 new_SymConst (type_or_id_p value, symconst_kind kind)
1725 {
1726   return new_r_SymConst (current_ir_graph, current_ir_graph->current_block,
1727                          value, kind);
1728 }
1729
1730 ir_node *
1731 new_Sync (int arity, ir_node** in)
1732 {
1733   return new_r_Sync (current_ir_graph, current_ir_graph->current_block,
1734                      arity, in);
1735 }
1736
1737
1738 ir_node *
1739 new_Bad (void)
1740 {
1741   return current_ir_graph->bad;
1742 }
1743
1744 /* ********************************************************************* */
1745 /* Comfortable interface with automatic Phi node construction.           */
1746 /* (Uses also constructors of ?? interface, except new_Block.            */
1747 /* ********************************************************************* */
1748
1749 /** Block construction **/
1750 /* immature Block without predecessors */
1751 ir_node *new_immBlock (void) {
1752   ir_node *res;
1753
1754   assert(get_irg_phase_state (current_ir_graph) == phase_building);
1755   /* creates a new dynamic in-array as length of in is -1 */
1756   res = new_ir_node (current_ir_graph, NULL, op_Block, mode_R, -1, NULL);
1757   current_ir_graph->current_block = res;
1758   res->attr.block.matured = 0;
1759   res->attr.block.exc = exc_normal;
1760   set_Block_block_visited(res, 0);
1761
1762   /* Create and initialize array for Phi-node construction. */
1763   res->attr.block.graph_arr = NEW_ARR_D (ir_node *, current_ir_graph->obst,
1764                                          current_ir_graph->n_loc);
1765   memset(res->attr.block.graph_arr, 0, sizeof(ir_node *)*current_ir_graph->n_loc);
1766
1767   /* Immature block may not be optimized! */
1768   irn_vrfy (res);
1769
1770   return res;
1771 }
1772
1773 /* add an adge to a jmp/control flow node */
1774 void
1775 add_in_edge (ir_node *block, ir_node *jmp)
1776 {
1777   if (block->attr.block.matured) {
1778     assert(0 && "Error: Block already matured!\n");
1779   }
1780   else {
1781     assert (jmp != NULL);
1782     ARR_APP1 (ir_node *, block->in, jmp);
1783   }
1784 }
1785
1786 /* changing the current block */
1787 void
1788 switch_block (ir_node *target)
1789 {
1790   current_ir_graph->current_block = target;
1791 }
1792
1793 /* ************************ */
1794 /* parameter administration */
1795
1796 /* get a value from the parameter array from the current block by its index */
1797 ir_node *
1798 get_value (int pos, ir_mode *mode)
1799 {
1800   assert(get_irg_phase_state (current_ir_graph) == phase_building);
1801   inc_irg_visited(current_ir_graph);
1802
1803   return get_r_value_internal (current_ir_graph->current_block, pos + 1, mode);
1804 }
1805
1806
1807 /* set a value at position pos in the parameter array from the current block */
1808 inline void
1809 set_value (int pos, ir_node *value)
1810 {
1811   assert(get_irg_phase_state (current_ir_graph) == phase_building);
1812   current_ir_graph->current_block->attr.block.graph_arr[pos + 1] = value;
1813 }
1814
1815 /* get the current store */
1816 inline ir_node *
1817 get_store (void)
1818 {
1819   assert(get_irg_phase_state (current_ir_graph) == phase_building);
1820   /* GL: one could call get_value instead */
1821   inc_irg_visited(current_ir_graph);
1822   return get_r_value_internal (current_ir_graph->current_block, 0, mode_M);
1823 }
1824
1825 /* set the current store */
1826 inline void
1827 set_store (ir_node *store)
1828 {
1829   assert(get_irg_phase_state (current_ir_graph) == phase_building);
1830   /* GL: one could call set_value instead */
1831   current_ir_graph->current_block->attr.block.graph_arr[0] = store;
1832 }
1833
1834 inline void
1835 keep_alive (ir_node *ka)
1836 {
1837   add_End_keepalive(current_ir_graph->end, ka);
1838 }
1839
1840 /** Useful access routines **/
1841 /* Returns the current block of the current graph.  To set the current
1842    block use switch_block(). */
1843 ir_node *get_cur_block() {
1844   return get_irg_current_block(current_ir_graph);
1845 }
1846
1847 /* Returns the frame type of the current graph */
1848 type *get_cur_frame_type() {
1849   return get_irg_frame_type(current_ir_graph);
1850 }
1851
1852
1853 /* ********************************************************************* */
1854 /* initialize */
1855
1856 /* call once for each run of the library */
1857 void
1858 init_cons (void)
1859 {
1860 }
1861
1862 /* call for each graph */
1863 void
1864 finalize_cons (ir_graph *irg) {
1865   irg->phase_state = phase_high;
1866 }