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