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