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