state handling for loops
[libfirm] / ir / ir / irgraph.c
1 /*
2  * Project:     libFIRM
3  * File name:   ir/ir/irgraph.c
4  * Purpose:     Entry point to the representation of procedure code.
5  * Author:      Martin Trapp, Christian Schaefer
6  * Modified by: Goetz Lindenmaier
7  * Created:
8  * CVS-ID:      $Id$
9  * Copyright:   (c) 1998-2003 Universität Karlsruhe
10  * Licence:     This file protected by GPL -  GNU GENERAL PUBLIC LICENSE.
11  */
12
13 #ifdef HAVE_CONFIG_H
14 # include <config.h>
15 #endif
16
17 # include <string.h>
18
19 # include "ircons.h"
20 # include "irgraph_t.h"
21 # include "irprog_t.h"
22 # include "iropt_t.h"
23 # include "array.h"
24 # include "irgmod.h"
25 # include "mangle.h"
26 # include "irouts.h"
27
28 ir_graph *current_ir_graph;
29 INLINE ir_graph *get_current_ir_graph(void) {
30   return current_ir_graph;
31 }
32 INLINE void set_current_ir_graph(ir_graph *graph) {
33   current_ir_graph = graph;
34 }
35
36
37 bool interprocedural_view = false;
38 INLINE bool get_interprocedural_view(void) {
39   return interprocedural_view;
40 }
41 INLINE void set_interprocedural_view(bool state) {
42   interprocedural_view = state;
43 }
44
45 static ident* frame_type_suffix = NULL;
46 void init_irgraph(void) {
47   frame_type_suffix = id_from_str(FRAME_TP_SUFFIX, strlen(FRAME_TP_SUFFIX));
48 }
49
50 #if USE_EXPLICIT_PHI_IN_STACK
51 /* really defined in ircons.c */
52 typedef struct Phi_in_stack Phi_in_stack;
53 Phi_in_stack *new_Phi_in_stack();
54 void free_Phi_in_stack(Phi_in_stack *s);
55 #endif
56
57 /* Allocates a list of nodes:
58     - The start block containing a start node and Proj nodes for it's four
59       results (X, M, P, Tuple).
60     - The end block containing an end node. This block is not matured after
61       new_ir_graph as predecessors need to be added to it.
62     - The current block, which is empty and also not matured.
63    Further it allocates several datastructures needed for graph construction
64    and optimization.
65 */
66 ir_graph *
67 new_ir_graph (entity *ent, int n_loc)
68 {
69   ir_graph *res;
70   ir_node *first_block;
71   ir_node *projX;
72
73   res = (ir_graph *) malloc (sizeof (ir_graph));
74   memset(res, 0, sizeof (ir_graph));
75   res->kind=k_ir_graph;
76
77   current_ir_graph = res;
78   add_irp_irg(res);          /* remember this graph global. */
79
80 /**
81   * initialized for each graph. **/
82 #if PRECISE_EXC_CONTEXT
83   res->n_loc = n_loc + 1 + 1; /* number of local variables that are never
84                                  dereferenced in this graph plus one for
85                                  the store plus one for links to fragile
86                                  operations.  n_loc is not the number of
87                                  parameters to the procedure!  */
88 #else
89   res->n_loc = n_loc + 1;  /* number of local variables that are never
90                               dereferenced in this graph plus one for
91                               the store. This is not the number of parameters
92                               to the procedure!  */
93 #endif
94
95   res->visited = 0;     /* visited flag, for the ir walker */
96   res->block_visited=0; /* visited flag, for the 'block'-walker */
97
98 #if USE_EXPLICIT_PHI_IN_STACK
99   res->Phi_in_stack = new_Phi_in_stack();  /* A stack needed for automatic Phi
100                                 generation */
101 #endif
102   res->kind = k_ir_graph;
103   res->obst      = (struct obstack *) xmalloc (sizeof (struct obstack));
104   obstack_init (res->obst);
105   res->value_table = new_identities (); /* value table for global value
106                                            numbering for optimizing use in
107                                            iropt.c */
108   res->outs = NULL;
109
110   res->phase_state = phase_building;
111   res->pinned = pinned;
112   res->outs_state = no_outs;
113   res->dom_state = no_dom;
114   res->typeinfo_state = irg_typeinfo_none;
115   res->loopinfo_state = loopinfo_none;
116
117   /** Type information for the procedure of the graph **/
118   res->ent = ent;
119   set_entity_irg(ent, res);
120
121 /**
122       contain "inner" methods as in Pascal. **/
123   res->frame_type = new_type_class(mangle(get_entity_ident(ent), frame_type_suffix));
124   /* Remove type from type list.  Must be treated differently than other types. */
125   remove_irp_type_from_list(res->frame_type);
126
127   /** Nodes needed in every graph **/
128   res->end_block = new_immBlock ();
129   res->end       = new_End ();
130
131   res->start_block = new_immBlock ();
132   res->start   = new_Start ();
133   res->bad     = new_ir_node (NULL, res, res->start_block, op_Bad, mode_T, 0, NULL);
134   //res->unknown = new_ir_node (NULL, res, res->start_block, op_Unknown, mode_T, 0, NULL);
135
136   /* Proj results of start node */
137   projX        = new_Proj (res->start, mode_X, pns_initial_exec);
138   set_store (new_Proj (res->start, mode_M, pns_global_store));
139   res->frame   = new_Proj (res->start, mode_P_mach, pns_frame_base);
140   res->globals = new_Proj (res->start, mode_P_mach, pns_globals);
141   res->args    = new_Proj (res->start, mode_T, pns_args);
142 #ifdef DEBUG_libfirm
143   res->graph_nr = get_irp_new_node_nr();
144 #endif
145
146
147   add_in_edge(res->start_block, projX);
148   /*
149    * The code generation needs it. leave it in now.
150    * Use of this edge is matter of discussion, unresolved. Also possible:
151    * add_in_edge(res->start_block, res->start_block), but invalid typed.
152    */
153   mature_block (res->current_block);
154
155   /** Make a block to start with **/
156   first_block = new_immBlock ();
157   add_in_edge (first_block, projX);
158
159   return res;
160 }
161
162
163 /* Make a rudimentary ir graph for the constant code.
164    Must look like a correct irg, spare everything else. */
165 ir_graph *new_const_code_irg(void) {
166   ir_graph *res;
167   ir_node *projX;
168
169   res = (ir_graph *) malloc (sizeof(*res));
170   memset(res, 0, sizeof(*res));
171
172   current_ir_graph = res;
173   res->n_loc = 1;      /* Only the memory. */
174   res->visited = 0;     /* visited flag, for the ir walker */
175   res->block_visited=0; /* visited flag, for the 'block'-walker */
176 #if USE_EXPLICIT_PHI_IN_STACK
177   res->Phi_in_stack = NULL;
178 #endif
179   res->kind = k_ir_graph;
180   res->obst      = (struct obstack *) xmalloc (sizeof (struct obstack));
181   obstack_init (res->obst);
182   res->phase_state = phase_building;
183   res->pinned = pinned;
184   res->value_table = new_identities (); /* value table for global value
185                                            numbering for optimizing use in
186                                            iropt.c */
187   res->ent = NULL;
188   res->frame_type = NULL;
189   res->start_block = new_immBlock ();
190   res->end_block = new_immBlock ();
191   res->end       = new_End ();
192   mature_block(get_cur_block());
193   res->bad = new_ir_node (NULL, res, res->start_block, op_Bad, mode_T, 0, NULL);
194   //res->unknown = new_ir_node (NULL, res, res->start_block, op_Unknown, mode_T, 0, NULL);
195   res->start   = new_Start ();
196
197   /* Proj results of start node */
198   projX        = new_Proj (res->start, mode_X, pns_initial_exec);
199   set_store (new_Proj (res->start, mode_M, pns_global_store));
200   add_in_edge(res->start_block, projX);
201   mature_block (res->current_block);
202   add_in_edge (new_immBlock (), projX);
203   mature_block(get_cur_block());
204   /* Set the visited flag high enough that the block will never be visited. */
205   set_irn_visited(get_cur_block(), -1);
206   set_Block_block_visited(get_cur_block(), -1);
207   set_Block_block_visited(res->start_block, -1);
208   set_irn_visited(res->start_block, -1);
209   set_irn_visited(res->bad, -1);
210   return res;
211 }
212
213 /* Defined in iropt.c */
214 void  del_identities (pset *value_table);
215
216 /* Frees the passed irgraph.
217    Deallocates all nodes in this graph and the ir_graph structure.
218    Sets the field irgraph in the corresponding entity to NULL.
219    Does not remove the irgraph from the list in irprog (requires
220    inefficient search, call remove_irp_irg by hand).
221    Does not free types, entities or modes that are used only by this
222    graph, nor the entity standing for this graph. */
223 void free_ir_graph (ir_graph *irg) {
224   if (irg->ent) set_entity_irg(irg->ent, NULL);  /* not set in const code irg */
225   free_End(irg->end);
226   if (irg->frame_type)  free_type(irg->frame_type);
227   if (irg->value_table) del_identities(irg->value_table);
228   if (irg->outs_state != no_outs) free_outs(irg);
229   obstack_free(irg->obst,NULL);
230   free(irg->obst);
231 #if USE_EXPLICIT_PHI_IN_STACK
232   free_Phi_in_stack(irg->Phi_in_stack);
233 #endif
234   irg->kind = k_BAD;
235   free(irg);
236 }
237
238 /* access routines for all ir_graph attributes:
239    templates:
240    {attr type} get_irg_{attribute name} (ir_graph *irg);
241    void set_irg_{attr name} (ir_graph *irg, {attr type} {attr}); */
242
243 int
244 is_ir_graph(void *thing) {
245   assert(thing);
246   if (get_kind(thing) == k_ir_graph)
247     return 1;
248   else
249     return 0;
250 }
251
252 /* Outputs a unique number for this node */
253
254 INLINE long
255 get_irg_graph_nr(ir_graph *irg) {
256   assert(irg);
257 #ifdef DEBUG_libfirm
258   return irg->graph_nr;
259 #else
260   return 0;
261 #endif
262 }
263
264 ir_node *
265 get_irg_start_block (ir_graph *irg)
266 {
267   return irg->start_block;
268 }
269
270 void
271 set_irg_start_block (ir_graph *irg, ir_node *node)
272 {
273   irg->start_block = node;
274 }
275
276 ir_node *
277 get_irg_start (ir_graph *irg)
278 {
279   return irg->start;
280 }
281
282 void
283 set_irg_start(ir_graph *irg, ir_node *node)
284 {
285   irg->start = node;
286 }
287
288 ir_node *
289 get_irg_end_block (ir_graph *irg)
290 {
291   return irg->end_block;
292 }
293
294 void
295 set_irg_end_block (ir_graph *irg, ir_node *node)
296 {
297   irg->end_block = node;
298 }
299
300 ir_node *
301 get_irg_end (ir_graph *irg)
302 {
303   return irg->end;
304 }
305
306 void
307 set_irg_end (ir_graph *irg, ir_node *node)
308 {
309   irg->end = node;
310 }
311
312 ir_node *
313 get_irg_cstore (ir_graph *irg)
314 {
315   return irg->cstore;
316 }
317
318 void
319 set_irg_cstore (ir_graph *irg, ir_node *node)
320 {
321   irg->cstore = node;
322 }
323
324 ir_node *
325 get_irg_frame (ir_graph *irg)
326 {
327   return irg->frame;
328 }
329
330 void
331 set_irg_frame (ir_graph *irg, ir_node *node)
332 {
333   irg->frame = node;
334 }
335
336 ir_node *
337 get_irg_globals (ir_graph *irg)
338 {
339   return irg->globals;
340 }
341
342 void
343 set_irg_globals (ir_graph *irg, ir_node *node)
344 {
345   irg->globals = node;
346 }
347
348 ir_node *
349 get_irg_args (ir_graph *irg)
350 {
351   return irg->args;
352 }
353
354 void
355 set_irg_args (ir_graph *irg, ir_node *node)
356 {
357   irg->args = node;
358 }
359
360 ir_node *
361 get_irg_bad (ir_graph *irg)
362 {
363   return irg->bad;
364 }
365
366 void
367 set_irg_bad (ir_graph *irg, ir_node *node)
368 {
369   irg->bad = node;
370 }
371
372 /* GL removed: we need unknown with mode for analyses.
373 ir_node *
374 get_irg_unknown (ir_graph *irg)
375 {
376   return irg->unknown;
377 }
378
379 void
380 set_irg_unknown (ir_graph *irg, ir_node *node)
381 {
382   irg->unknown = node;
383 }
384 */
385
386 ir_node *
387 get_irg_current_block (ir_graph *irg)
388 {
389   return irg->current_block;
390 }
391
392 void
393 set_irg_current_block (ir_graph *irg, ir_node *node)
394 {
395   irg->current_block = node;
396 }
397
398 entity *
399 get_irg_ent (ir_graph *irg)
400 {
401   assert(irg && irg->ent);
402   return irg->ent;
403 }
404
405 void
406 set_irg_ent (ir_graph *irg, entity *ent)
407 {
408   irg->ent = ent;
409 }
410
411 type *
412 get_irg_frame_type (ir_graph *irg)
413 {
414   assert(irg && irg->frame_type);
415   return irg->frame_type;
416 }
417
418 void
419 set_irg_frame_type (ir_graph *irg, type *ftp)
420 {
421   assert(is_class_type(ftp));
422   irg->frame_type = ftp;
423 }
424
425
426 /* To test for a frame type */
427 int
428 is_frame_type(type *ftp) {
429   int i;
430   if (is_class_type(ftp)) {
431     for (i = 0; i < get_irp_n_irgs(); i++) {
432       type *frame_tp = get_irg_frame_type(get_irp_irg(i));
433       if (ftp == frame_tp) return true;
434     }
435   }
436   return false;
437 }
438
439 int
440 get_irg_n_locs (ir_graph *irg)
441 {
442 #if PRECISE_EXC_CONTEXT
443   return irg->n_loc - 1 - 1;
444 #else
445   return irg->n_loc - 1;
446 #endif
447 }
448
449 void
450 set_irg_n_loc (ir_graph *irg, int n_loc)
451 {
452 #if PRECISE_EXC_CONTEXT
453   irg->n_loc = n_loc + 1 + 1;
454 #else
455   irg->n_loc = n_loc + 1;
456 #endif
457 }
458
459
460
461 /* Returns the obstack associated with the graph. */
462 struct obstack *get_irg_obstack(ir_graph *irg) {
463   return irg->obst;
464 }
465
466 /*
467  * Returns true if the node n is allocated on the storage of graph irg.
468  *
469  * Implementation is GLIBC specific as is uses the internal _obstack_chunk implementation.
470  */
471 int node_is_in_irgs_storage(ir_graph *irg, ir_node *n)
472 {
473   struct _obstack_chunk *p;
474
475   /*
476    * checks wheater the ir_node pointer i on the obstack.
477    * A more sophisticated check would test the "whole" ir_node
478    */
479   for (p = irg->obst->chunk; p; p = p->prev) {
480     if (((char *)p->contents <= (char *)n) && ((char *)n < (char *)p->limit))
481       return 1;
482   }
483
484   return 0;
485 }
486
487 irg_phase_state
488 get_irg_phase_state (ir_graph *irg) {
489   return irg->phase_state;
490 }
491
492 void
493 set_irg_phase_low(ir_graph *irg) {
494   irg->phase_state = phase_low;
495 }
496
497 op_pinned
498 get_irg_pinned (ir_graph *irg) {
499   return irg->pinned;
500 }
501
502 irg_outs_state
503 get_irg_outs_state(ir_graph *irg) {
504   return irg->outs_state;
505 }
506
507 void
508 set_irg_outs_inconsistent(ir_graph *irg) {
509   irg->outs_state = outs_inconsistent;
510 }
511
512 irg_dom_state
513 get_irg_dom_state(ir_graph *irg) {
514   return irg->dom_state;
515 }
516
517 void
518 set_irg_dom_inconsistent(ir_graph *irg) {
519   irg->dom_state = dom_inconsistent;
520 }
521
522 irg_loopinfo_state
523 get_irg_loopinfo_state(ir_graph *irg) {
524   return irg->loopinfo_state;
525 }
526
527 void set_irg_loopinfo_state(ir_graph *irg, irg_loopinfo_state s) {
528   irg->loopinfo_state = s;
529 }
530
531 void
532 set_irg_loopinfo_inconsistent(ir_graph *irg) {
533   if (irg->loopinfo_state == loopinfo_ip_consistent)
534     irg->loopinfo_state = loopinfo_ip_inconsistent;
535   else
536     irg->loopinfo_state = loopinfo_inconsistent;
537 }
538
539 INLINE void
540 set_irg_pinned (ir_graph *irg, op_pinned p) {
541   irg->pinned = p;
542 }
543
544
545 irg_callee_info_state get_irg_callee_info_state(ir_graph *irg) {
546   return irg->callee_info_state;
547 }
548
549 void set_irg_callee_info_state(ir_graph *irg, irg_callee_info_state s) {
550   irg->callee_info_state = s;
551 }
552
553 irg_inline_property get_irg_inline_property(ir_graph *irg) {
554   return irg->inline_property;
555 }
556 void set_irg_inline_property(ir_graph *irg, irg_inline_property s) {
557   irg->inline_property = s;
558 }
559
560
561 INLINE void
562 set_irg_link (ir_graph *irg, void *thing) {
563   irg->link = thing;
564 }
565
566 INLINE void *
567 get_irg_link (ir_graph *irg) {
568   return irg->link;
569 }
570
571 /* maximum visited flag content of all ir_graph visited fields. */
572 static int max_irg_visited = 0;
573
574 unsigned long
575 get_irg_visited (ir_graph *irg)
576 {
577   return irg->visited;
578 }
579
580 void
581 set_irg_visited (ir_graph *irg, unsigned long visited)
582 {
583   irg->visited = visited;
584   if (irg->visited > max_irg_visited) {
585     max_irg_visited = irg->visited;
586   }
587 }
588
589 void
590 inc_irg_visited (ir_graph *irg)
591 {
592   if (++irg->visited > max_irg_visited) {
593     max_irg_visited = irg->visited;
594   }
595 }
596
597 unsigned long
598 get_max_irg_visited(void)
599 {
600   /*
601   int i;
602   for(i = 0; i < get_irp_n_irgs(); i++)
603   assert(max_irg_visited >= get_irg_visited(get_irp_irg(i)));
604    */
605   return max_irg_visited;
606 }
607
608 void set_max_irg_visited(int val) {
609   max_irg_visited = val;
610 }
611
612 unsigned long
613 inc_max_irg_visited(void)
614 {
615   /*
616   int i;
617   for(i = 0; i < get_irp_n_irgs(); i++)
618   assert(max_irg_visited >= get_irg_visited(get_irp_irg(i)));
619   */
620   max_irg_visited++;
621   return max_irg_visited;
622 }
623
624 unsigned long
625 get_irg_block_visited (ir_graph *irg)
626 {
627   return irg->block_visited;
628 }
629
630 void
631 set_irg_block_visited (ir_graph *irg, unsigned long visited)
632 {
633   irg->block_visited = visited;
634 }
635
636 void
637 inc_irg_block_visited (ir_graph *irg)
638 {
639   ++irg->block_visited;
640 }