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