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