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