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