Typo 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->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   /* Proj results of start node */
189   projX        = new_Proj (res->start, mode_X, pns_initial_exec);
190   set_store (new_Proj (res->start, mode_M, pns_global_store));
191   add_in_edge(res->start_block, projX);
192   mature_block (res->current_block);
193   add_in_edge (new_immBlock (), projX);
194   mature_block(get_cur_block());
195   /* Set the visited flag high enough that the block will never be visited. */
196   set_irn_visited(get_cur_block(), -1);
197   set_Block_block_visited(get_cur_block(), -1);
198   set_Block_block_visited(res->start_block, -1);
199   return res;
200 }
201
202 /* Frees the passed irgraph.
203    Deallocates all nodes in this graph and the ir_graph structure.
204    Sets the field irgraph in the corresponding entity to NULL.
205    Does not remove the irgraph from the list in irprog (requires
206    inefficient search, call remove_irp_irg by hand).
207    Does not free types, entities or modes that are used only by this
208    graph, nor the entity standing for this graph. */
209 void free_ir_graph (ir_graph *irg) {
210   set_entity_irg(irg->ent, NULL);
211   free(irg->obst);
212 #if USE_EXPLICIT_PHI_IN_STACK
213   free_Phi_in_stack(irg->Phi_in_stack);
214 #endif
215   free(irg);
216 }
217
218 /* access routines for all ir_graph attributes:
219    templates:
220    {attr type} get_irg_{attribute name} (ir_graph *irg);
221    void set_irg_{attr name} (ir_graph *irg, {attr type} {attr}); */
222
223 int
224 is_ir_graph(void *thing) {
225   assert(thing);
226   if (get_kind(thing) == k_ir_graph)
227     return 1;
228   else
229     return 0;
230 }
231
232 /* Outputs a unique number for this node */
233
234 INLINE long
235 get_irg_graph_nr(ir_graph *irg) {
236   assert(irg);
237 #ifdef DEBUG_libfirm
238   return irg->graph_nr;
239 #else
240   return 0;
241 #endif
242 }
243
244 ir_node *
245 get_irg_start_block (ir_graph *irg)
246 {
247   return irg->start_block;
248 }
249
250 void
251 set_irg_start_block (ir_graph *irg, ir_node *node)
252 {
253   irg->start_block = node;
254 }
255
256 ir_node *
257 get_irg_start (ir_graph *irg)
258 {
259   return irg->start;
260 }
261
262 void
263 set_irg_start(ir_graph *irg, ir_node *node)
264 {
265   irg->start = node;
266 }
267
268 ir_node *
269 get_irg_end_block (ir_graph *irg)
270 {
271   return irg->end_block;
272 }
273
274 void
275 set_irg_end_block (ir_graph *irg, ir_node *node)
276 {
277   irg->end_block = node;
278 }
279
280 ir_node *
281 get_irg_end (ir_graph *irg)
282 {
283   return irg->end;
284 }
285
286 void
287 set_irg_end (ir_graph *irg, ir_node *node)
288 {
289   irg->end = node;
290 }
291
292 ir_node *
293 get_irg_cstore (ir_graph *irg)
294 {
295   return irg->cstore;
296 }
297
298 void
299 set_irg_cstore (ir_graph *irg, ir_node *node)
300 {
301   irg->cstore = node;
302 }
303
304 ir_node *
305 get_irg_frame (ir_graph *irg)
306 {
307   return irg->frame;
308 }
309
310 void
311 set_irg_frame (ir_graph *irg, ir_node *node)
312 {
313   irg->frame = node;
314 }
315
316 ir_node *
317 get_irg_globals (ir_graph *irg)
318 {
319   return irg->globals;
320 }
321
322 void
323 set_irg_globals (ir_graph *irg, ir_node *node)
324 {
325   irg->globals = node;
326 }
327
328 ir_node *
329 get_irg_args (ir_graph *irg)
330 {
331   return irg->args;
332 }
333
334 void
335 set_irg_args (ir_graph *irg, ir_node *node)
336 {
337   irg->args = node;
338 }
339
340 ir_node *
341 get_irg_bad (ir_graph *irg)
342 {
343   return irg->bad;
344 }
345
346 void
347 set_irg_bad (ir_graph *irg, ir_node *node)
348 {
349   irg->bad = node;
350 }
351
352 ir_node *
353 get_irg_unknown (ir_graph *irg)
354 {
355   return irg->unknown;
356 }
357
358 void
359 set_irg_unknown (ir_graph *irg, ir_node *node)
360 {
361   irg->unknown = node;
362 }
363
364 ir_node *
365 get_irg_current_block (ir_graph *irg)
366 {
367   return irg->current_block;
368 }
369
370 void
371 set_irg_current_block (ir_graph *irg, ir_node *node)
372 {
373   irg->current_block = node;
374 }
375
376 entity *
377 get_irg_ent (ir_graph *irg)
378 {
379   assert(irg && irg->ent);
380   return irg->ent;
381 }
382
383 void
384 set_irg_ent (ir_graph *irg, entity *ent)
385 {
386   irg->ent = ent;
387 }
388
389 type *
390 get_irg_frame_type (ir_graph *irg)
391 {
392   assert(irg && irg->frame_type);
393   return irg->frame_type;
394 }
395
396 void
397 set_irg_frame_type (ir_graph *irg, type *ftp)
398 {
399   assert(is_class_type(ftp));
400   irg->frame_type = ftp;
401 }
402
403
404 /* To test for a frame type */
405 int
406 is_frame_type(type *ftp) {
407   int i;
408   if (is_class_type(ftp)) {
409     for (i = 0; i < get_irp_n_irgs(); i++) {
410       type *frame_tp = get_irg_frame_type(get_irp_irg(i));
411       if (ftp == frame_tp) return true;
412     }
413   }
414   return false;
415 }
416
417 int
418 get_irg_n_locs (ir_graph *irg)
419 {
420 #if PRECISE_EXC_CONTEXT
421   return irg->n_loc - 1 - 1;
422 #else
423   return irg->n_loc - 1;
424 #endif
425 }
426
427 void
428 set_irg_n_loc (ir_graph *irg, int n_loc)
429 {
430 #if PRECISE_EXC_CONTEXT
431   irg->n_loc = n_loc + 1 + 1;
432 #else
433   irg->n_loc = n_loc + 1;
434 #endif
435 }
436
437
438
439 /* Returns the obstack associated with the graph. */
440 struct obstack *get_irg_obstack(ir_graph *irg) {
441   return irg->obst;
442 }
443
444 /*
445  * Returns true if the node n is allocated on the storage of graph irg.
446  *
447  * Implementation is GLIBC specific as is uses the internal _obstack_chunk implementation.
448  */
449 int node_is_in_irgs_storage(ir_graph *irg, ir_node *n)
450 {
451   struct _obstack_chunk *p;
452
453   /*
454    * checks wheater the ir_node pointer i on the obstack.
455    * A more sophisticated chaeck would test the "whole" ir_node
456    */
457   for (p = irg->obst->chunk; p; p = p->prev) {
458     if (((char *)p->contents <= (char *)n) && ((char *)n < (char *)p->limit))
459       return 1;
460   }
461
462   return 0;
463 }
464
465 irg_phase_state
466 get_irg_phase_state (ir_graph *irg) {
467   return irg->phase_state;
468 }
469
470 void
471 set_irg_phase_low(ir_graph *irg) {
472   irg->phase_state = phase_low;
473 }
474
475 op_pinned
476 get_irg_pinned (ir_graph *irg) {
477   return irg->pinned;
478 }
479
480 irg_outs_state
481 get_irg_outs_state(ir_graph *irg) {
482   return irg->outs_state;
483 }
484
485 void
486 set_irg_outs_inconsistent(ir_graph *irg) {
487   irg->outs_state = outs_inconsistent;
488 }
489
490 irg_dom_state
491 get_irg_dom_state(ir_graph *irg) {
492   return irg->dom_state;
493 }
494
495 void
496 set_irg_dom_inconsistent(ir_graph *irg) {
497   irg->dom_state = dom_inconsistent;
498 }
499
500 INLINE void
501 set_irg_pinned (ir_graph *irg, op_pinned p) {
502   irg->pinned = p;
503 }
504
505 INLINE void
506 set_irg_link (ir_graph *irg, void *thing) {
507   irg->link = thing;
508 }
509
510 INLINE void *
511 get_irg_link (ir_graph *irg) {
512   return irg->link;
513 }
514
515 /* maximum visited flag content of all ir_graph visited fields. */
516 static int max_irg_visited = 0;
517
518 unsigned long
519 get_irg_visited (ir_graph *irg)
520 {
521   return irg->visited;
522 }
523
524 void
525 set_irg_visited (ir_graph *irg, unsigned long visited)
526 {
527   irg->visited = visited;
528   if (irg->visited > max_irg_visited) {
529     max_irg_visited = irg->visited;
530   }
531 }
532
533 void
534 inc_irg_visited (ir_graph *irg)
535 {
536   if (++irg->visited > max_irg_visited) {
537     max_irg_visited = irg->visited;
538   }
539 }
540
541 unsigned long
542 get_max_irg_visited(void)
543 {
544   /*
545   int i;
546   for(i = 0; i < get_irp_n_irgs(); i++)
547   assert(max_irg_visited >= get_irg_visited(get_irp_irg(i)));
548    */
549   return max_irg_visited;
550 }
551
552 void set_max_irg_visited(int val) {
553   max_irg_visited = val;
554 }
555
556 unsigned long
557 inc_max_irg_visited(void)
558 {
559   /*
560   int i;
561   for(i = 0; i < get_irp_n_irgs(); i++)
562   assert(max_irg_visited >= get_irg_visited(get_irp_irg(i)));
563   */
564   max_irg_visited++;
565   return max_irg_visited;
566 }
567
568 unsigned long
569 get_irg_block_visited (ir_graph *irg)
570 {
571   return irg->block_visited;
572 }
573
574 void
575 set_irg_block_visited (ir_graph *irg, unsigned long visited)
576 {
577   irg->block_visited = visited;
578 }
579
580 void
581 inc_irg_block_visited (ir_graph *irg)
582 {
583   ++irg->block_visited;
584 }