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