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