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