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