8e9125a356d1d8ad8f185bf2ad91833f2aef5e69
[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   irg->frame_type = ftp;
400 }
401
402
403 /* To test for a frame type */
404 int
405 is_frame_type(type *ftp) {
406   return ((is_class_type(ftp) || is_struct_type(ftp)) &&
407           id_is_suffix(id_from_str(FRAME_TP_SUFFIX, strlen(FRAME_TP_SUFFIX)),
408                        get_type_ident(ftp)));
409 }
410
411 int
412 get_irg_n_locs (ir_graph *irg)
413 {
414 #if PRECISE_EXC_CONTEXT
415   return irg->n_loc - 1 - 1;
416 #else
417   return irg->n_loc - 1;
418 #endif
419 }
420
421 void
422 set_irg_n_loc (ir_graph *irg, int n_loc)
423 {
424 #if PRECISE_EXC_CONTEXT
425   irg->n_loc = n_loc + 1 + 1;
426 #else
427   irg->n_loc = n_loc + 1;
428 #endif
429 }
430
431
432
433 /* Returns the obstack associated with the graph. */
434 struct obstack *get_irg_obstack(ir_graph *irg) {
435   return irg->obst;
436 }
437
438 /*
439  * Returns true if the node n is allocated on the storage of graph irg.
440  *
441  * Implementation is GLIBC specific as is uses the internal _obstack_chunk implementation.
442  */
443 int node_is_in_irgs_storage(ir_graph *irg, ir_node *n)
444 {
445   struct _obstack_chunk *p;
446
447   /*
448    * checks wheater the ir_node pointer i on the obstack.
449    * A more sophisticated chaeck would test the "whole" ir_node
450    */
451   for (p = irg->obst->chunk; p; p = p->prev) {
452     if (((char *)p->contents <= (char *)n) && ((char *)n < (char *)p->limit))
453       return 1;
454   }
455
456   return 0;
457 }
458
459 irg_phase_state
460 get_irg_phase_state (ir_graph *irg) {
461   return irg->phase_state;
462 }
463
464 void
465 set_irg_phase_low(ir_graph *irg) {
466   irg->phase_state = phase_low;
467 }
468
469 op_pinned
470 get_irg_pinned (ir_graph *irg) {
471   return irg->pinned;
472 }
473
474 irg_outs_state
475 get_irg_outs_state(ir_graph *irg) {
476   return irg->outs_state;
477 }
478
479 void
480 set_irg_outs_inconsistent(ir_graph *irg) {
481   irg->outs_state = outs_inconsistent;
482 }
483
484 irg_dom_state
485 get_irg_dom_state(ir_graph *irg) {
486   return irg->dom_state;
487 }
488
489 void
490 set_irg_dom_inconsistent(ir_graph *irg) {
491   irg->dom_state = dom_inconsistent;
492 }
493
494 INLINE void
495 set_irg_pinned (ir_graph *irg, op_pinned p) {
496   irg->pinned = p;
497 }
498
499 INLINE void
500 set_irg_link (ir_graph *irg, void *thing) {
501   irg->link = thing;
502 }
503
504 INLINE void *
505 get_irg_link (ir_graph *irg) {
506   return irg->link;
507 }
508
509 /* maximum visited flag content of all ir_graph visited fields. */
510 static int max_irg_visited = 0;
511
512 unsigned long
513 get_irg_visited (ir_graph *irg)
514 {
515   return irg->visited;
516 }
517
518 void
519 set_irg_visited (ir_graph *irg, unsigned long visited)
520 {
521   irg->visited = visited;
522   if (irg->visited > max_irg_visited) {
523     max_irg_visited = irg->visited;
524   }
525 }
526
527 void
528 inc_irg_visited (ir_graph *irg)
529 {
530   if (++irg->visited > max_irg_visited) {
531     max_irg_visited = irg->visited;
532   }
533 }
534
535 unsigned long
536 get_max_irg_visited(void)
537 {
538   /*
539   int i;
540   for(i = 0; i < get_irp_n_irgs(); i++)
541   assert(max_irg_visited >= get_irg_visited(get_irp_irg(i)));
542    */
543   return max_irg_visited;
544 }
545
546 void set_max_irg_visited(int val) {
547   max_irg_visited = val;
548 }
549
550 unsigned long
551 inc_max_irg_visited(void)
552 {
553   /*
554   int i;
555   for(i = 0; i < get_irp_n_irgs(); i++)
556   assert(max_irg_visited >= get_irg_visited(get_irp_irg(i)));
557   */
558   max_irg_visited++;
559   return max_irg_visited;
560 }
561
562 unsigned long
563 get_irg_block_visited (ir_graph *irg)
564 {
565   return irg->block_visited;
566 }
567
568 void
569 set_irg_block_visited (ir_graph *irg, unsigned long visited)
570 {
571   irg->block_visited = visited;
572 }
573
574 void
575 inc_irg_block_visited (ir_graph *irg)
576 {
577   ++irg->block_visited;
578 }