bugfix
[libfirm] / ir / ir / irgraph.c
1 /*
2  * Project:     libFIRM
3  * File name:   ir/ir/irgraph.c
4  * Purpose:     Entry point to the representation of procedure code.
5  * Author:      Martin Trapp, Christian Schaefer
6  * Modified by: Goetz Lindenmaier
7  * Created:
8  * CVS-ID:      $Id$
9  * Copyright:   (c) 1998-2003 Universität Karlsruhe
10  * Licence:     This file protected by GPL -  GNU GENERAL PUBLIC LICENSE.
11  */
12
13 #ifdef HAVE_CONFIG_H
14 # include "config.h"
15 #endif
16
17 #ifdef HAVE_STRING_H
18 # include <string.h>
19 #endif
20
21 # include "xmalloc.h"
22 # include "ircons.h"
23 # include "irgraph_t.h"
24 # include "irprog_t.h"
25 # include "irnode_t.h"
26 # include "iropt_t.h"
27 # include "irflag_t.h"
28 # include "array.h"
29 # include "irgmod.h"
30 # include "mangle.h"
31 # include "irouts.h"
32 # include "firmstat.h"
33 # include "irgwalk.h"
34
35 ir_graph *current_ir_graph;
36 INLINE ir_graph *get_current_ir_graph(void) {
37   return current_ir_graph;
38 }
39 INLINE void set_current_ir_graph(ir_graph *graph) {
40   current_ir_graph = graph;
41 }
42
43
44 int __interprocedural_view = false;
45
46 int (get_interprocedural_view)(void) {
47   return __get_interprocedural_view();
48 }
49
50 void (set_interprocedural_view)(int state) {
51   __interprocedural_view = state;
52
53   /* set function vectors for faster access */
54   if (state) {
55     __get_irn_arity = __get_irn_inter_arity;
56     __get_irn_n     = __get_irn_inter_n;
57   }
58   else {
59     __get_irn_arity = __get_irn_intra_arity;
60     __get_irn_n     = __get_irn_intra_n;
61   }
62 }
63
64 static ident* frame_type_suffix = NULL;
65 void init_irgraph(void) {
66   frame_type_suffix = new_id_from_chars(FRAME_TP_SUFFIX, strlen(FRAME_TP_SUFFIX));
67 }
68
69 #if USE_EXPLICIT_PHI_IN_STACK
70 /* really defined in ircons.c */
71 typedef struct Phi_in_stack Phi_in_stack;
72 Phi_in_stack *new_Phi_in_stack();
73 void free_Phi_in_stack(Phi_in_stack *s);
74 #endif
75
76 /* Allocates a list of nodes:
77     - The start block containing a start node and Proj nodes for it's four
78       results (X, M, P, Tuple).
79     - The end block containing an end node. This block is not matured after
80       new_ir_graph as predecessors need to be added to it.
81     - The current block, which is empty and also not matured.
82    Further it allocates several datastructures needed for graph construction
83    and optimization.
84 */
85 ir_graph *
86 new_r_ir_graph (entity *ent, int n_loc)
87 {
88   ir_graph *res;
89   ir_node *first_block;
90   ir_node *projX;
91
92   res = (ir_graph *) xmalloc (sizeof (ir_graph));
93   memset(res, 0, sizeof (ir_graph));
94   res->kind = k_ir_graph;
95
96   /* inform statistics here, as blocks will be already build on this graph */
97   stat_new_graph(res, ent);
98
99   current_ir_graph = res;
100
101   /*-- initialized for each graph. --*/
102   if (get_opt_precise_exc_context()) {
103     res->n_loc = n_loc + 1 + 1; /* number of local variables that are never
104                    dereferenced in this graph plus one for
105                    the store plus one for links to fragile
106                    operations.  n_loc is not the number of
107                    parameters to the procedure!  */
108   }
109   else {
110     res->n_loc = n_loc + 1;  /* number of local variables that are never
111                 dereferenced in this graph plus one for
112                 the store. This is not the number of parameters
113                 to the procedure!  */
114   }
115
116   res->visited = 0;     /* visited flag, for the ir walker */
117   res->block_visited = 0; /* visited flag, for the 'block'-walker */
118
119 #if USE_EXPLICIT_PHI_IN_STACK
120   res->Phi_in_stack = new_Phi_in_stack();  /* A stack needed for automatic Phi
121                                 generation */
122 #endif
123   res->kind = k_ir_graph;
124   res->obst      = (struct obstack *) xmalloc (sizeof (struct obstack));
125   obstack_init (res->obst);
126   res->value_table = new_identities (); /* value table for global value
127                        numbering for optimizing use in
128                        iropt.c */
129   res->outs = NULL;
130
131   res->phase_state    = phase_building;
132   res->op_pin_state_pinned = op_pin_state_pinned;
133   res->outs_state     = outs_none;
134   res->dom_state      = dom_none;
135   res->typeinfo_state = irg_typeinfo_none;
136   res->loopinfo_state = loopinfo_none;
137
138   /*-- Type information for the procedure of the graph --*/
139   res->ent = ent;
140   set_entity_irg(ent, res);
141
142   /*--  a class type so that it can contain "inner" methods as in Pascal. --*/
143   res->frame_type = new_type_class(mangle(get_entity_ident(ent), frame_type_suffix));
144
145   /* Remove type from type list.  Must be treated differently than other types. */
146   remove_irp_type_from_list(res->frame_type);
147
148   /*-- Nodes needed in every graph --*/
149   res->end_block  = new_immBlock();
150   res->end        = new_End();
151   res->end_reg    = res->end;
152   res->end_except = res->end;
153
154   res->start_block = new_immBlock();
155   res->start   = new_Start();
156   res->bad     = new_ir_node(NULL, res, res->start_block, op_Bad, mode_T, 0, NULL);
157   res->no_mem  = new_ir_node(NULL, res, res->start_block, op_NoMem, mode_M, 0, NULL);
158
159   /* Proj results of start node */
160   projX            = new_Proj (res->start, mode_X, pn_Start_X_initial_exec);
161   res->frame       = new_Proj (res->start, mode_P_mach, pn_Start_P_frame_base);
162   res->globals     = new_Proj (res->start, mode_P_mach, pn_Start_P_globals);
163   res->initial_mem = new_Proj (res->start, mode_M, pn_Start_M);
164   res->args        = new_Proj (res->start, mode_T, pn_Start_T_args);
165 #ifdef DEBUG_libfirm
166   res->graph_nr    = get_irp_new_node_nr();
167 #endif
168
169   set_store(res->initial_mem);
170
171   add_immBlock_pred(res->start_block, projX);
172   /*
173    * The code generation needs it. leave it in now.
174    * Use of this edge is matter of discussion, unresolved. Also possible:
175    * add_immBlock_pred(res->start_block, res->start_block), but invalid typed.
176    */
177   mature_immBlock (res->current_block);
178
179   /*-- Make a block to start with --*/
180   first_block = new_immBlock();
181   add_immBlock_pred (first_block, projX);
182
183   return res;
184 }
185
186
187 ir_graph *
188 new_ir_graph (entity *ent, int n_loc)
189 {
190   ir_graph *res = new_r_ir_graph (ent, n_loc);
191   add_irp_irg(res);          /* remember this graph global. */
192   return res;
193 }
194
195 /* Make a rudimentary ir graph for the constant code.
196    Must look like a correct irg, spare everything else. */
197 ir_graph *new_const_code_irg(void) {
198   ir_graph *res;
199   ir_node *projX;
200
201   res = (ir_graph *) xmalloc (sizeof(*res));
202   memset(res, 0, sizeof(*res));
203
204   /* inform statistics here, as blocks will be already build on this graph */
205   stat_new_graph(res, NULL);
206
207   current_ir_graph = res;
208   res->n_loc = 1;       /* Only the memory. */
209   res->visited = 0;     /* visited flag, for the ir walker */
210   res->block_visited=0; /* visited flag, for the 'block'-walker */
211 #if USE_EXPLICIT_PHI_IN_STACK
212   res->Phi_in_stack = NULL;
213 #endif
214   res->kind = k_ir_graph;
215   res->obst      = (struct obstack *) xmalloc (sizeof (struct obstack));
216   obstack_init (res->obst);
217   res->phase_state = phase_building;
218   res->op_pin_state_pinned = op_pin_state_pinned;
219   res->value_table = new_identities (); /* value table for global value
220                        numbering for optimizing use in
221                        iropt.c */
222   res->ent = NULL;
223   res->frame_type  = NULL;
224
225   /* -- The end block -- */
226   res->end_block  = new_immBlock ();
227   res->end        = new_End ();
228   res->end_reg    = res->end;
229   res->end_except = res->end;
230   mature_immBlock(get_cur_block());  /* mature the end block */
231
232   /* -- The start block -- */
233   res->start_block = new_immBlock ();
234   res->bad     = new_ir_node (NULL, res, res->start_block, op_Bad, mode_T, 0, NULL);
235   res->no_mem  = new_ir_node (NULL, res, res->start_block, op_NoMem, mode_M, 0, NULL);
236   res->start   = new_Start ();
237   /* Proj results of start node */
238   res->initial_mem = new_Proj (res->start, mode_M, pn_Start_M);
239   projX            = new_Proj (res->start, mode_X, pn_Start_X_initial_exec);
240   add_immBlock_pred (res->start_block, projX);
241   mature_immBlock   (res->start_block);  /* mature the start block */
242
243   add_immBlock_pred (new_immBlock (), projX);
244   mature_immBlock   (get_cur_block());   /* mature the 'body' block for expressions */
245
246   /* Set the visited flag high enough that the blocks will never be visited. */
247   set_irn_visited(get_cur_block(), -1);
248   set_Block_block_visited(get_cur_block(), -1);
249   set_Block_block_visited(res->start_block, -1);
250   set_irn_visited(res->start_block, -1);
251   set_irn_visited(res->bad, -1);
252   set_irn_visited(res->no_mem, -1);
253
254   res->phase_state = phase_high;
255   return res;
256 }
257
258 /* Defined in iropt.c */
259 void  del_identities (pset *value_table);
260
261 /* Frees the passed irgraph.
262    Deallocates all nodes in this graph and the ir_graph structure.
263    Sets the field irgraph in the corresponding entity to NULL.
264    Does not remove the irgraph from the list in irprog (requires
265    inefficient search, call remove_irp_irg by hand).
266    Does not free types, entities or modes that are used only by this
267    graph, nor the entity standing for this graph. */
268 void free_ir_graph (ir_graph *irg) {
269
270   stat_free_graph(irg);
271   if (irg->outs_state != outs_none) free_outs(irg);
272   if (irg->frame_type)  free_type(irg->frame_type);
273   if (irg->value_table) del_identities(irg->value_table);
274   if (irg->ent) {
275     peculiarity pec = get_entity_peculiarity (irg->ent);
276     set_entity_peculiarity (irg->ent, peculiarity_description);
277     set_entity_irg(irg->ent, NULL);  /* not set in const code irg */
278     set_entity_peculiarity (irg->ent, pec);
279   }
280
281   free_End(irg->end);
282   obstack_free(irg->obst,NULL);
283   free(irg->obst);
284 #if USE_EXPLICIT_PHI_IN_STACK
285   free_Phi_in_stack(irg->Phi_in_stack);
286 #endif
287   irg->kind = k_BAD;
288   free(irg);
289 }
290
291 /* access routines for all ir_graph attributes:
292    templates:
293    {attr type} get_irg_{attribute name} (ir_graph *irg);
294    void set_irg_{attr name} (ir_graph *irg, {attr type} {attr}); */
295
296 int
297 (is_ir_graph)(const void *thing) {
298   return __is_ir_graph(thing);
299 }
300
301 /* Outputs a unique number for this node */
302
303 INLINE long
304 get_irg_graph_nr(ir_graph *irg) {
305   assert(irg);
306 #ifdef DEBUG_libfirm
307   return irg->graph_nr;
308 #else
309   return (long)irg;
310 #endif
311 }
312
313 ir_node *
314 (get_irg_start_block)(const ir_graph *irg) {
315   return __get_irg_start_block(irg);
316 }
317
318 void
319 (set_irg_start_block)(ir_graph *irg, ir_node *node) {
320   __set_irg_start_block(irg, node);
321 }
322
323 ir_node *
324 (get_irg_start)(const ir_graph *irg) {
325   return __get_irg_start(irg);
326 }
327
328 void
329 (set_irg_start)(ir_graph *irg, ir_node *node) {
330   __set_irg_start(irg, node);
331 }
332
333 ir_node *
334 (get_irg_end_block)(const ir_graph *irg) {
335   return __get_irg_end_block(irg);
336 }
337
338 void
339 (set_irg_end_block)(ir_graph *irg, ir_node *node) {
340   __set_irg_end_block(irg, node);
341 }
342
343 ir_node *
344 (get_irg_end)(const ir_graph *irg) {
345   return __get_irg_end(irg);
346 }
347
348 void
349 (set_irg_end)(ir_graph *irg, ir_node *node) {
350   __set_irg_end(irg, node);
351 }
352
353 ir_node *
354 (get_irg_end_reg)(const ir_graph *irg) {
355   return __get_irg_end_reg(irg);
356 }
357
358 void     set_irg_end_reg (ir_graph *irg, ir_node *node) {
359   assert(get_irn_op(node) == op_EndReg || get_irn_op(node) == op_End);
360   irg->end_reg = node;
361 }
362
363 ir_node *
364 (get_irg_end_except)(const ir_graph *irg) {
365   return __get_irg_end_except(irg);
366 }
367
368 void     set_irg_end_except (ir_graph *irg, ir_node *node) {
369   assert(get_irn_op(node) == op_EndExcept || get_irn_op(node) == op_End);
370   irg->end_except = node;
371 }
372
373 ir_node *
374 (get_irg_cstore)(const ir_graph *irg) {
375   return __get_irg_cstore(irg);
376 }
377
378 void
379 (set_irg_cstore)(ir_graph *irg, ir_node *node) {
380   __set_irg_cstore(irg, node);
381 }
382
383 ir_node *
384 (get_irg_frame)(const ir_graph *irg) {
385   return __get_irg_frame(irg);
386 }
387
388 void
389 (set_irg_frame)(ir_graph *irg, ir_node *node) {
390   __set_irg_frame(irg, node);
391 }
392
393 ir_node *
394 (get_irg_globals)(const ir_graph *irg) {
395   return __get_irg_globals(irg);
396 }
397
398 void
399 (set_irg_globals)(ir_graph *irg, ir_node *node) {
400   __set_irg_globals(irg, node);
401 }
402
403 ir_node *
404 (get_irg_initial_mem)(const ir_graph *irg) {
405   return __get_irg_initial_mem(irg);
406 }
407
408 void
409 (set_irg_initial_mem)(ir_graph *irg, ir_node *node) {
410   __set_irg_initial_mem(irg, node);
411 }
412
413 ir_node *
414 (get_irg_args)(const ir_graph *irg) {
415   return __get_irg_args(irg);
416 }
417
418 void
419 (set_irg_args)(ir_graph *irg, ir_node *node) {
420   __set_irg_args(irg, node);
421 }
422
423 ir_node *
424 (get_irg_bad)(const ir_graph *irg) {
425   return __get_irg_bad(irg);
426 }
427
428 void
429 (set_irg_bad)(ir_graph *irg, ir_node *node) {
430   __set_irg_bad(irg, node);
431 }
432
433 ir_node *
434 (get_irg_no_mem)(const ir_graph *irg) {
435   return __get_irg_no_mem(irg);
436 }
437
438 void
439 (set_irg_no_mem)(ir_graph *irg, ir_node *node) {
440   __set_irg_no_mem(irg, node);
441 }
442
443 ir_node *
444 (get_irg_current_block)(const ir_graph *irg) {
445   return __get_irg_current_block(irg);
446 }
447
448 void
449 (set_irg_current_block)(ir_graph *irg, ir_node *node) {
450   __set_irg_current_block(irg, node);
451 }
452
453 entity *
454 (get_irg_entity)(const ir_graph *irg) {
455   return __get_irg_entity(irg);
456 }
457
458 void
459 (set_irg_entity)(ir_graph *irg, entity *ent) {
460   __set_irg_entity(irg, ent);
461 }
462
463 type *
464 (get_irg_frame_type)(const ir_graph *irg) {
465   return __get_irg_frame_type(irg);
466 }
467
468 void
469 (set_irg_frame_type)(ir_graph *irg, type *ftp) {
470   __set_irg_frame_type(irg, ftp);
471 }
472
473
474 /* To test for a frame type */
475 int
476 is_frame_type(const type *ftp) {
477   int i;
478   if (is_class_type(ftp)) {
479     for (i = 0; i < get_irp_n_irgs(); i++) {
480       const type *frame_tp = get_irg_frame_type(get_irp_irg(i));
481       if (ftp == frame_tp) return true;
482     }
483   }
484   return false;
485 }
486
487 int
488 get_irg_n_locs (ir_graph *irg)
489 {
490   if (get_opt_precise_exc_context())
491     return irg->n_loc - 1 - 1;
492   else
493     return irg->n_loc - 1;
494 }
495
496 void
497 set_irg_n_loc (ir_graph *irg, int n_loc)
498 {
499   if (get_opt_precise_exc_context())
500     irg->n_loc = n_loc + 1 + 1;
501   else
502     irg->n_loc = n_loc + 1;
503 }
504
505
506
507 /* Returns the obstack associated with the graph. */
508 struct obstack *
509 (get_irg_obstack)(const ir_graph *irg) {
510   return __get_irg_obstack(irg);
511 }
512
513 /*
514  * Returns true if the node n is allocated on the storage of graph irg.
515  *
516  * Implementation is GLIBC specific as is uses the internal _obstack_chunk implementation.
517  */
518 int node_is_in_irgs_storage(ir_graph *irg, ir_node *n)
519 {
520   struct _obstack_chunk *p;
521
522   /*
523    * checks wheater the ir_node pointer i on the obstack.
524    * A more sophisticated check would test the "whole" ir_node
525    */
526   for (p = irg->obst->chunk; p; p = p->prev) {
527     if (((char *)p->contents <= (char *)n) && ((char *)n < (char *)p->limit))
528       return 1;
529   }
530
531   return 0;
532 }
533
534 irg_phase_state
535 (get_irg_phase_state)(const ir_graph *irg) {
536   return __get_irg_phase_state(irg);
537 }
538
539 void
540 (set_irg_phase_low)(ir_graph *irg) {
541   __set_irg_phase_low(irg);
542 }
543
544 op_pin_state
545 (get_irg_pinned)(const ir_graph *irg) {
546   return __get_irg_pinned(irg);
547 }
548
549 irg_outs_state
550 (get_irg_outs_state)(const ir_graph *irg) {
551   return __get_irg_outs_state(irg);
552 }
553
554 void
555 (set_irg_outs_inconsistent)(ir_graph *irg) {
556   __set_irg_outs_inconsistent(irg);
557 }
558
559 irg_dom_state
560 (get_irg_dom_state)(const ir_graph *irg) {
561   return __get_irg_dom_state(irg);
562 }
563
564 void
565 (set_irg_dom_inconsistent)(ir_graph *irg) {
566   __set_irg_dom_inconsistent(irg);
567 }
568
569 irg_loopinfo_state
570 (get_irg_loopinfo_state)(const ir_graph *irg) {
571   return __get_irg_loopinfo_state(irg);
572 }
573
574 void
575 (set_irg_loopinfo_state)(ir_graph *irg, irg_loopinfo_state s) {
576   __set_irg_loopinfo_state(irg, s);
577 }
578
579 void
580 set_irg_loopinfo_inconsistent(ir_graph *irg) {
581   if (irg->loopinfo_state == loopinfo_ip_consistent)
582     irg->loopinfo_state = loopinfo_ip_inconsistent;
583
584   else if (irg->loopinfo_state == loopinfo_consistent)
585     irg->loopinfo_state = loopinfo_inconsistent;
586
587   else if (irg->loopinfo_state == loopinfo_cf_ip_consistent)
588     irg->loopinfo_state = loopinfo_cf_ip_inconsistent;
589
590   else if (irg->loopinfo_state == loopinfo_cf_consistent)
591     irg->loopinfo_state = loopinfo_cf_inconsistent;
592 }
593
594 void
595 (set_irg_pinned)(ir_graph *irg, op_pin_state p) {
596   __set_irg_pinned(irg, p);
597 }
598
599 irg_callee_info_state
600 (get_irg_callee_info_state)(const ir_graph *irg) {
601   return __get_irg_callee_info_state(irg);
602 }
603
604 void
605 (set_irg_callee_info_state)(ir_graph *irg, irg_callee_info_state s) {
606   __set_irg_callee_info_state(irg, s);
607 }
608
609 irg_inline_property
610 (get_irg_inline_property)(const ir_graph *irg) {
611   return __get_irg_inline_property(irg);
612 }
613
614 void
615 (set_irg_inline_property)(ir_graph *irg, irg_inline_property s) {
616   __set_irg_inline_property(irg, s);
617 }
618
619 void
620 (set_irg_link)(ir_graph *irg, void *thing) {
621   __set_irg_link(irg, thing);
622 }
623
624 void *
625 (get_irg_link)(const ir_graph *irg) {
626   return __get_irg_link(irg);
627 }
628
629 /* maximum visited flag content of all ir_graph visited fields. */
630 static int max_irg_visited = 0;
631
632 unsigned long
633 (get_irg_visited)(const ir_graph *irg) {
634   return __get_irg_visited(irg);
635 }
636
637 void
638 set_irg_visited (ir_graph *irg, unsigned long visited)
639 {
640   irg->visited = visited;
641   if (irg->visited > max_irg_visited) {
642     max_irg_visited = irg->visited;
643   }
644 }
645
646 void
647 inc_irg_visited (ir_graph *irg)
648 {
649   if (++irg->visited > max_irg_visited) {
650     max_irg_visited = irg->visited;
651   }
652 }
653
654 unsigned long
655 get_max_irg_visited(void)
656 {
657   /*
658   int i;
659   for(i = 0; i < get_irp_n_irgs(); i++)
660   assert(max_irg_visited >= get_irg_visited(get_irp_irg(i)));
661    */
662   return max_irg_visited;
663 }
664
665 void set_max_irg_visited(int val) {
666   max_irg_visited = val;
667 }
668
669 unsigned long
670 inc_max_irg_visited(void)
671 {
672   /*
673   int i;
674   for(i = 0; i < get_irp_n_irgs(); i++)
675   assert(max_irg_visited >= get_irg_visited(get_irp_irg(i)));
676   */
677   max_irg_visited++;
678   return max_irg_visited;
679 }
680
681 unsigned long
682 (get_irg_block_visited)(const ir_graph *irg) {
683   return __get_irg_block_visited(irg);
684 }
685
686 void
687 (set_irg_block_visited)(ir_graph *irg, unsigned long visited) {
688   __set_irg_block_visited(irg, visited);
689 }
690
691 void
692 (inc_irg_block_visited)(ir_graph *irg) {
693   __inc_irg_block_visited(irg);
694 }
695
696
697 /**
698  * walker Start->End: places Proj nodes into the same block
699  * as it's predecessors
700  *
701  * @param n    the node
702  * @param env  ignored
703  */
704 static void normalize_proj_walker(ir_node *n, void *env)
705 {
706   if (is_Proj(n)) {
707     ir_node *pred  = get_Proj_pred(n);
708     ir_node *block = get_nodes_block(pred);
709
710     set_nodes_block(n, block);
711   }
712 }
713
714 /* put the proj's into the same block as its predecessors */
715 void normalize_proj_nodes(ir_graph *irg)
716 {
717   irg_walk_graph(irg, NULL, normalize_proj_walker, NULL);
718         set_irg_outs_inconsistent(irg);
719 }