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