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