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