type fixed
[libfirm] / ir / ir / irgraph.c
1 /* Copyright (C) 1998 - 2000 by Universitaet Karlsruhe
2 * All rights reserved.
3 *
4 * Authors: Martin Trapp, Christian Schaefer
5 *
6 *
7 */
8
9 /* $Id$ */
10
11 #ifdef HAVE_CONFIG_H
12 # include <config.h>
13 #endif
14
15 # include <string.h>
16
17 # include "ircons.h"
18 # include "irgraph_t.h"
19 # include "irprog_t.h"
20 # include "iropt_t.h"
21 # include "array.h"
22 # include "irgmod.h"
23 # include "mangle.h"
24
25 ir_graph *current_ir_graph;
26 INLINE ir_graph *get_current_ir_graph(void) {
27   return current_ir_graph;
28 }
29 INLINE void set_current_ir_graph(ir_graph *graph) {
30   current_ir_graph = graph;
31 }
32
33
34 bool interprocedural_view = false;
35 INLINE bool get_interprocedural_view(void) {
36   return interprocedural_view;
37 }
38 INLINE void set_interprocedural_view(bool state) {
39   interprocedural_view = state;
40 }
41
42 static ident* frame_type_suffix = NULL;
43 void init_irgraph(void) {
44   frame_type_suffix = id_from_str(FRAME_TP_SUFFIX, strlen(FRAME_TP_SUFFIX));
45 }
46
47 #if USE_EXPLICIT_PHI_IN_STACK
48 /* really defined in ircons.c */
49 typedef struct Phi_in_stack Phi_in_stack;
50 Phi_in_stack *new_Phi_in_stack();
51 void free_Phi_in_stack(Phi_in_stack *s);
52 #endif
53
54 /* Allocates a list of nodes:
55     - The start block containing a start node and Proj nodes for it's four
56       results (X, M, P, Tuple).
57     - The end block containing an end node. This block is not matured after
58       new_ir_graph as predecessors need to be added to it.
59     - The current block, which is empty and also not matured.
60    Further it allocates several datastructures needed for graph construction
61    and optimization.
62 */
63 ir_graph *
64 new_ir_graph (entity *ent, int n_loc)
65 {
66   ir_graph *res;
67   ir_node *first_block;
68   ir_node *projX;
69
70   res = (ir_graph *) malloc (sizeof (ir_graph));
71   res->kind=k_ir_graph;
72
73   current_ir_graph = res;
74   add_irp_irg(res);          /* remember this graph global. */
75
76 /**
77   * initialized for each graph. **/
78 #if PRECISE_EXC_CONTEXT
79   res->n_loc = n_loc + 1 + 1; /* number of local variables that are never
80                                  dereferenced in this graph plus one for
81                                  the store plus one for links to fragile
82                                  operations.  n_loc is not the number of
83                                  parameters to the procedure!  */
84 #else
85   res->n_loc = n_loc + 1;  /* number of local variables that are never
86                               dereferenced in this graph plus one for
87                               the store. This is not the number of parameters
88                               to the procedure!  */
89 #endif
90
91   res->visited = 0;     /* visited flag, for the ir walker */
92   res->block_visited=0; /* visited flag, for the 'block'-walker */
93
94 #if USE_EXPLICIT_PHI_IN_STACK
95   res->Phi_in_stack = new_Phi_in_stack();  /* A stack needed for automatic Phi
96                                 generation */
97 #endif
98   res->obst      = (struct obstack *) xmalloc (sizeof (struct obstack));
99   obstack_init (res->obst);
100   res->value_table = new_identities (); /* value table for global value
101                                            numbering for optimizing use in
102                                            iropt.c */
103   res->outs = NULL;
104
105   res->phase_state = phase_building;
106   res->pinned = pinned;
107   res->outs_state = no_outs;
108   res->dom_state = no_dom;
109
110   /** Type information for the procedure of the graph **/
111   res->ent = ent;
112   set_entity_irg(ent, res);
113
114 /**
115       contain "inner" methods as in Pascal. **/
116   res->frame_type = new_type_class(mangle(get_entity_ident(ent), frame_type_suffix));
117   /* Remove type from type list.  Must be treated differently than other types. */
118   remove_irp_type_from_list(res->frame_type);
119
120   /** Nodes needed in every graph **/
121   res->end_block = new_immBlock ();
122   res->end       = new_End ();
123
124   res->start_block = new_immBlock ();
125   res->start   = new_Start ();
126   res->bad     = new_ir_node (NULL, res, res->start_block, op_Bad, mode_T, 0, NULL);
127   res->unknown = new_ir_node (NULL, res, res->start_block, op_Unknown, mode_T, 0, NULL);
128
129   /* Proj results of start node */
130   projX        = new_Proj (res->start, mode_X, pns_initial_exec);
131   set_store (new_Proj (res->start, mode_M, pns_global_store));
132   res->frame   = new_Proj (res->start, mode_P, pns_frame_base);
133   res->globals = new_Proj (res->start, mode_P, pns_globals);
134   res->args    = new_Proj (res->start, mode_T, pns_args);
135 #ifdef DEBUG_libfirm
136   res->graph_nr = get_irp_new_node_nr();
137 #endif
138
139
140   add_in_edge(res->start_block, projX);
141   /*
142    * The code generation needs it. leave it in now.
143    * Use of this edge is matter of discussion, unresolved. Also possible:
144    * add_in_edge(res->start_block, res->start_block), but invalid typed.
145    */
146   mature_block (res->current_block);
147
148   /** Make a block to start with **/
149   first_block = new_immBlock ();
150   add_in_edge (first_block, projX);
151
152   return res;
153 }
154
155
156 /* Make a rudimentary ir graph for the constant code.
157    Must look like a correct irg, spare everything else. */
158 ir_graph *new_const_code_irg() {
159   ir_graph *res;
160   ir_node *projX;
161
162   res = (ir_graph *) malloc (sizeof (ir_graph));
163   current_ir_graph = res;
164   res->n_loc = 1;      /* Only the memory. */
165   res->visited = 0;     /* visited flag, for the ir walker */
166   res->block_visited=0; /* visited flag, for the 'block'-walker */
167 #if USE_EXPLICIT_PHI_IN_STACK
168   res->Phi_in_stack = NULL;
169 #endif
170   res->obst      = (struct obstack *) xmalloc (sizeof (struct obstack));
171   obstack_init (res->obst);
172   res->phase_state = phase_building;
173   res->pinned = pinned;
174   res->value_table = new_identities (); /* value table for global value
175                                            numbering for optimizing use in
176                                            iropt.c */
177   res->ent = NULL;
178   res->frame_type = NULL;
179   res->start_block = new_immBlock ();
180   res->end_block = new_immBlock ();
181   res->end       = new_End ();
182   mature_block(get_cur_block());
183   res->bad = new_ir_node (NULL, res, res->start_block, op_Bad, mode_T, 0, NULL);
184   res->unknown = new_ir_node (NULL, res, res->start_block, op_Unknown, mode_T, 0, NULL);
185   res->start   = new_Start ();
186   /* Proj results of start node */
187   projX        = new_Proj (res->start, mode_X, pns_initial_exec);
188   set_store (new_Proj (res->start, mode_M, pns_global_store));
189   add_in_edge(res->start_block, projX);
190   mature_block (res->current_block);
191   add_in_edge (new_immBlock (), projX);
192   mature_block(get_cur_block());
193   /* Set the visited flag high enough that the block will never be visited. */
194   set_irn_visited(get_cur_block(), -1);
195   set_Block_block_visited(get_cur_block(), -1);
196   set_Block_block_visited(res->start_block, -1);
197   return res;
198 }
199
200 /* Frees the passed irgraph.
201    Deallocates all nodes in this graph and the ir_graph structure.
202    Sets the field irgraph in the corresponding entity to NULL.
203    Does not remove the irgraph from the list in irprog (requires
204    inefficient search, call remove_irp_irg by hand).
205    Does not free types, entities or modes that are used only by this
206    graph, nor the entity standing for this graph. */
207 void free_ir_graph (ir_graph *irg) {
208   set_entity_irg(irg->ent, NULL);
209   free(irg->obst);
210 #if USE_EXPLICIT_PHI_IN_STACK
211   free_Phi_in_stack(irg->Phi_in_stack);
212 #endif
213   free(irg);
214 }
215
216 /* access routines for all ir_graph attributes:
217    templates:
218    {attr type} get_irg_{attribute name} (ir_graph *irg);
219    void set_irg_{attr name} (ir_graph *irg, {attr type} {attr}); */
220
221 /* Outputs a unique number for this node */
222
223 INLINE long
224 get_irg_graph_nr(ir_graph *irg) {
225   assert(irg);
226 #ifdef DEBUG_libfirm
227   return irg->graph_nr;
228 #else
229   return 0;
230 #endif
231 }
232
233 ir_node *
234 get_irg_start_block (ir_graph *irg)
235 {
236   return irg->start_block;
237 }
238
239 void
240 set_irg_start_block (ir_graph *irg, ir_node *node)
241 {
242   irg->start_block = node;
243 }
244
245 ir_node *
246 get_irg_start (ir_graph *irg)
247 {
248   return irg->start;
249 }
250
251 void
252 set_irg_start(ir_graph *irg, ir_node *node)
253 {
254   irg->start = node;
255 }
256
257 ir_node *
258 get_irg_end_block (ir_graph *irg)
259 {
260   return irg->end_block;
261 }
262
263 void
264 set_irg_end_block (ir_graph *irg, ir_node *node)
265 {
266   irg->end_block = node;
267 }
268
269 ir_node *
270 get_irg_end (ir_graph *irg)
271 {
272   return irg->end;
273 }
274
275 void
276 set_irg_end (ir_graph *irg, ir_node *node)
277 {
278   irg->end = node;
279 }
280
281 ir_node *
282 get_irg_cstore (ir_graph *irg)
283 {
284   return irg->cstore;
285 }
286
287 void
288 set_irg_cstore (ir_graph *irg, ir_node *node)
289 {
290   irg->cstore = node;
291 }
292
293 ir_node *
294 get_irg_frame (ir_graph *irg)
295 {
296   return irg->frame;
297 }
298
299 void
300 set_irg_frame (ir_graph *irg, ir_node *node)
301 {
302   irg->frame = node;
303 }
304
305 ir_node *
306 get_irg_globals (ir_graph *irg)
307 {
308   return irg->globals;
309 }
310
311 void
312 set_irg_globals (ir_graph *irg, ir_node *node)
313 {
314   irg->globals = node;
315 }
316
317 ir_node *
318 get_irg_args (ir_graph *irg)
319 {
320   return irg->args;
321 }
322
323 void
324 set_irg_args (ir_graph *irg, ir_node *node)
325 {
326   irg->args = node;
327 }
328
329 ir_node *
330 get_irg_bad (ir_graph *irg)
331 {
332   return irg->bad;
333 }
334
335 void
336 set_irg_bad (ir_graph *irg, ir_node *node)
337 {
338   irg->bad = node;
339 }
340
341 ir_node *
342 get_irg_unknown (ir_graph *irg)
343 {
344   return irg->unknown;
345 }
346
347 void
348 set_irg_unknown (ir_graph *irg, ir_node *node)
349 {
350   irg->unknown = node;
351 }
352
353 ir_node *
354 get_irg_current_block (ir_graph *irg)
355 {
356   return irg->current_block;
357 }
358
359 void
360 set_irg_current_block (ir_graph *irg, ir_node *node)
361 {
362   irg->current_block = node;
363 }
364
365 entity *
366 get_irg_ent (ir_graph *irg)
367 {
368   assert(irg && irg->ent);
369   return irg->ent;
370 }
371
372 void
373 set_irg_ent (ir_graph *irg, entity *ent)
374 {
375   irg->ent = ent;
376 }
377
378 type *
379 get_irg_frame_type (ir_graph *irg)
380 {
381   assert(irg && irg->frame_type);
382   return irg->frame_type;
383 }
384
385 void
386 set_irg_frame_type (ir_graph *irg, type *ftp)
387 {
388   irg->frame_type = ftp;
389 }
390
391
392 /* To test for a frame type */
393 int
394 is_frame_type(type *ftp) {
395   return ((is_class_type(ftp) || is_struct_type(ftp)) &&
396           id_is_suffix(id_from_str(FRAME_TP_SUFFIX, strlen(FRAME_TP_SUFFIX)),
397                        get_type_ident(ftp)));
398 }
399
400 int
401 get_irg_n_locs (ir_graph *irg)
402 {
403 #if PRECISE_EXC_CONTEXT
404   return irg->n_loc - 1 - 1;
405 #else
406   return irg->n_loc - 1;
407 #endif
408 }
409
410 void
411 set_irg_n_loc (ir_graph *irg, int n_loc)
412 {
413 #if PRECISE_EXC_CONTEXT
414   irg->n_loc = n_loc + 1 + 1;
415 #else
416   irg->n_loc = n_loc + 1;
417 #endif
418 }
419
420
421
422 /* Returns the obstack associated with the graph. */
423 struct obstack *get_irg_obstack(ir_graph *irg) {
424   return irg->obst;
425 }
426
427 /*
428  * Returns true if the node n is allocated on the storage of graph irg.
429  *
430  * Implementation is GLIBC specific as is uses the internal _obstack_chunk implementation.
431  */
432 int node_is_in_irgs_storage(ir_graph *irg, ir_node *n)
433 {
434   struct _obstack_chunk *p;
435
436   /*
437    * checks wheater the ir_node pointer i on the obstack.
438    * A more sophisticated chaeck would test the "whole" ir_node
439    */
440   for (p = irg->obst->chunk; p; p = p->prev) {
441     if (((char *)p->contents <= (char *)n) && ((char *)n < (char *)p->limit))
442       return 1;
443   }
444
445   return 0;
446 }
447
448 irg_phase_state
449 get_irg_phase_state (ir_graph *irg) {
450   return irg->phase_state;
451 }
452
453 void
454 set_irg_phase_low(ir_graph *irg) {
455   irg->phase_state = phase_low;
456 }
457
458 op_pinned
459 get_irg_pinned (ir_graph *irg) {
460   return irg->pinned;
461 }
462
463 irg_outs_state
464 get_irg_outs_state(ir_graph *irg) {
465   return irg->outs_state;
466 }
467
468 void
469 set_irg_outs_inconsistent(ir_graph *irg) {
470   irg->outs_state = outs_inconsistent;
471 }
472
473 irg_dom_state
474 get_irg_dom_state(ir_graph *irg) {
475   return irg->dom_state;
476 }
477
478 void
479 set_irg_dom_inconsistent(ir_graph *irg) {
480   irg->dom_state = dom_inconsistent;
481 }
482
483 INLINE void
484 set_irg_pinned (ir_graph *irg, op_pinned p) {
485   irg->pinned = p;
486 }
487
488 INLINE void
489 set_irg_link (ir_graph *irg, void *thing) {
490   irg->link = thing;
491 }
492
493 INLINE void *
494 get_irg_link (ir_graph *irg) {
495   return irg->link;
496 }
497
498 /* maximum visited flag content of all ir_graph visited fields. */
499 static int max_irg_visited = 0;
500
501 unsigned long
502 get_irg_visited (ir_graph *irg)
503 {
504   return irg->visited;
505 }
506
507 void
508 set_irg_visited (ir_graph *irg, unsigned long visited)
509 {
510   irg->visited = visited;
511   if (irg->visited > max_irg_visited) {
512     max_irg_visited = irg->visited;
513   }
514 }
515
516 void
517 inc_irg_visited (ir_graph *irg)
518 {
519   if (++irg->visited > max_irg_visited) {
520     max_irg_visited = irg->visited;
521   }
522 }
523
524 unsigned long
525 get_max_irg_visited(void)
526 {
527   /*
528   int i;
529   for(i = 0; i < get_irp_n_irgs(); i++)
530   assert(max_irg_visited >= get_irg_visited(get_irp_irg(i)));
531    */
532   return max_irg_visited;
533 }
534
535 void set_max_irg_visited(int val) {
536   max_irg_visited = val;
537 }
538
539 unsigned long
540 inc_max_irg_visited(void)
541 {
542   /*
543   int i;
544   for(i = 0; i < get_irp_n_irgs(); i++)
545   assert(max_irg_visited >= get_irg_visited(get_irp_irg(i)));
546   */
547   max_irg_visited++;
548   return max_irg_visited;
549 }
550
551 unsigned long
552 get_irg_block_visited (ir_graph *irg)
553 {
554   return irg->block_visited;
555 }
556
557 void
558 set_irg_block_visited (ir_graph *irg, unsigned long visited)
559 {
560   irg->block_visited = visited;
561 }
562
563 void
564 inc_irg_block_visited (ir_graph *irg)
565 {
566   ++irg->block_visited;
567 }