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