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