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