added registration facility for own data in an ir graph (like already
[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 #ifdef HAVE_STRING_H
18 # include <string.h>
19 #endif
20
21 #include <stddef.h>
22
23 # include "xmalloc.h"
24 # include "ircons.h"
25 # include "irgraph_t.h"
26 # include "irprog_t.h"
27 # include "irnode_t.h"
28 # include "iropt_t.h"
29 # include "irflag_t.h"
30 # include "array.h"
31 # include "irgmod.h"
32 # include "mangle.h"
33 # include "irouts.h"
34 # include "firmstat.h"
35 # include "irgwalk.h"
36
37 /**
38  * Indicates, whether additional data can be registered to graphs.
39  * If set to 1, this is not possible anymore.
40  */
41 static int forbid_new_data = 0;
42
43 /**
44  * The amount of additional space for custom data to be allocated upon
45  * creating a new graph.
46  */
47 static size_t additional_graph_data_size = 0;
48
49 ir_graph *current_ir_graph;
50 INLINE ir_graph *get_current_ir_graph(void) {
51   return current_ir_graph;
52 }
53 INLINE void set_current_ir_graph(ir_graph *graph) {
54   current_ir_graph = graph;
55 }
56
57
58 int __interprocedural_view = false;
59
60 int (get_interprocedural_view)(void) {
61   return __get_interprocedural_view();
62 }
63
64 void (set_interprocedural_view)(int state) {
65   __interprocedural_view = state;
66
67   /* set function vectors for faster access */
68   if (state) {
69     __get_irn_arity = __get_irn_inter_arity;
70     __get_irn_n     = __get_irn_inter_n;
71   }
72   else {
73     __get_irn_arity = __get_irn_intra_arity;
74     __get_irn_n     = __get_irn_intra_n;
75   }
76 }
77
78 static ident* frame_type_suffix = NULL;
79 void init_irgraph(void) {
80   frame_type_suffix = new_id_from_str(FRAME_TP_SUFFIX);
81         forbid_new_data = 1;
82 }
83
84 /**
85  * Allocate a new ir graph.
86  * This function respects the registered graph data. The only reason for
87  * this function is, that there are two locations, where graphs are
88  * allocated (new_r_ir_graph, new_const_code_irg).
89  * @return Memory for a new graph.
90  */
91 ir_graph *alloc_graph(void)
92 {
93         size_t size = sizeof(ir_graph) + additional_graph_data_size;
94         char *ptr = xmalloc(size);
95         memset(ptr, 0, size);
96
97         return (ir_graph *) (ptr + additional_graph_data_size);
98 }
99
100 #if USE_EXPLICIT_PHI_IN_STACK
101 /* really defined in ircons.c */
102 typedef struct Phi_in_stack Phi_in_stack;
103 Phi_in_stack *new_Phi_in_stack();
104 void free_Phi_in_stack(Phi_in_stack *s);
105 #endif
106
107 /* Allocates a list of nodes:
108     - The start block containing a start node and Proj nodes for it's four
109       results (X, M, P, Tuple).
110     - The end block containing an end node. This block is not matured after
111       new_ir_graph as predecessors need to be added to it.
112     - The current block, which is empty and also not matured.
113    Further it allocates several datastructures needed for graph construction
114    and optimization.
115 */
116 ir_graph *
117 new_r_ir_graph (entity *ent, int n_loc)
118 {
119   ir_graph *res;
120   ir_node *first_block;
121   ir_node *projX;
122
123   res = alloc_graph();
124   res->kind = k_ir_graph;
125
126   /* inform statistics here, as blocks will be already build on this graph */
127   stat_new_graph(res, ent);
128
129   current_ir_graph = res;
130
131   /*-- initialized for each graph. --*/
132   if (get_opt_precise_exc_context()) {
133     res->n_loc = n_loc + 1 + 1; /* number of local variables that are never
134                    dereferenced in this graph plus one for
135                    the store plus one for links to fragile
136                    operations.  n_loc is not the number of
137                    parameters to the procedure!  */
138   }
139   else {
140     res->n_loc = n_loc + 1;  /* number of local variables that are never
141                 dereferenced in this graph plus one for
142                 the store. This is not the number of parameters
143                 to the procedure!  */
144   }
145
146   res->visited = 0;     /* visited flag, for the ir walker */
147   res->block_visited = 0; /* visited flag, for the 'block'-walker */
148
149 #if USE_EXPLICIT_PHI_IN_STACK
150   res->Phi_in_stack = new_Phi_in_stack();  /* A stack needed for automatic Phi
151                                 generation */
152 #endif
153   res->kind = k_ir_graph;
154   res->obst      = (struct obstack *) xmalloc (sizeof (struct obstack));
155   obstack_init (res->obst);
156   res->value_table = new_identities (); /* value table for global value
157                        numbering for optimizing use in
158                        iropt.c */
159   res->outs = NULL;
160
161   res->phase_state    = phase_building;
162   res->op_pin_state_pinned = op_pin_state_pinned;
163   res->outs_state     = outs_none;
164   res->dom_state      = dom_none;
165   res->typeinfo_state = irg_typeinfo_none;
166   res->loopinfo_state = loopinfo_none;
167
168   /*-- Type information for the procedure of the graph --*/
169   res->ent = ent;
170   set_entity_irg(ent, res);
171
172   /*--  a class type so that it can contain "inner" methods as in Pascal. --*/
173   res->frame_type = new_type_class(mangle(get_entity_ident(ent), frame_type_suffix));
174
175   /* Remove type from type list.  Must be treated differently than other types. */
176   remove_irp_type_from_list(res->frame_type);
177
178   /*-- Nodes needed in every graph --*/
179   res->end_block  = new_immBlock();
180   res->end        = new_End();
181   res->end_reg    = res->end;
182   res->end_except = res->end;
183
184   res->start_block = new_immBlock();
185   res->start   = new_Start();
186   res->bad     = new_ir_node(NULL, res, res->start_block, op_Bad, mode_T, 0, NULL);
187   res->no_mem  = new_ir_node(NULL, res, res->start_block, op_NoMem, mode_M, 0, NULL);
188
189   /* Proj results of start node */
190   projX            = new_Proj (res->start, mode_X, pn_Start_X_initial_exec);
191   res->frame       = new_Proj (res->start, mode_P_mach, pn_Start_P_frame_base);
192   res->globals     = new_Proj (res->start, mode_P_mach, pn_Start_P_globals);
193   res->initial_mem = new_Proj (res->start, mode_M, pn_Start_M);
194   res->args        = new_Proj (res->start, mode_T, pn_Start_T_args);
195 #ifdef DEBUG_libfirm
196   res->graph_nr    = get_irp_new_node_nr();
197 #endif
198
199   set_store(res->initial_mem);
200
201   add_immBlock_pred(res->start_block, projX);
202   /*
203    * The code generation needs it. leave it in now.
204    * Use of this edge is matter of discussion, unresolved. Also possible:
205    * add_immBlock_pred(res->start_block, res->start_block), but invalid typed.
206    */
207   mature_immBlock (res->current_block);
208
209   /*-- Make a block to start with --*/
210   first_block = new_immBlock();
211   add_immBlock_pred (first_block, projX);
212
213   return res;
214 }
215
216
217 ir_graph *
218 new_ir_graph (entity *ent, int n_loc)
219 {
220   ir_graph *res = new_r_ir_graph (ent, n_loc);
221   add_irp_irg(res);          /* remember this graph global. */
222   return res;
223 }
224
225 /* Make a rudimentary ir graph for the constant code.
226    Must look like a correct irg, spare everything else. */
227 ir_graph *new_const_code_irg(void) {
228   ir_graph *res;
229   ir_node *projX;
230
231   res = alloc_graph();
232
233   /* inform statistics here, as blocks will be already build on this graph */
234   stat_new_graph(res, NULL);
235
236   current_ir_graph = res;
237   res->n_loc = 1;       /* Only the memory. */
238   res->visited = 0;     /* visited flag, for the ir walker */
239   res->block_visited=0; /* visited flag, for the 'block'-walker */
240 #if USE_EXPLICIT_PHI_IN_STACK
241   res->Phi_in_stack = NULL;
242 #endif
243   res->kind = k_ir_graph;
244   res->obst      = (struct obstack *) xmalloc (sizeof (struct obstack));
245   obstack_init (res->obst);
246   res->phase_state = phase_building;
247   res->op_pin_state_pinned = op_pin_state_pinned;
248   res->value_table = new_identities (); /* value table for global value
249                        numbering for optimizing use in
250                        iropt.c */
251   res->ent = NULL;
252   res->frame_type  = NULL;
253
254   /* -- The end block -- */
255   res->end_block  = new_immBlock ();
256   res->end        = new_End ();
257   res->end_reg    = res->end;
258   res->end_except = res->end;
259   mature_immBlock(get_cur_block());  /* mature the end block */
260
261   /* -- The start block -- */
262   res->start_block = new_immBlock ();
263   res->bad     = new_ir_node (NULL, res, res->start_block, op_Bad, mode_T, 0, NULL);
264   res->no_mem  = new_ir_node (NULL, res, res->start_block, op_NoMem, mode_M, 0, NULL);
265   res->start   = new_Start ();
266   /* Proj results of start node */
267   res->initial_mem = new_Proj (res->start, mode_M, pn_Start_M);
268   projX            = new_Proj (res->start, mode_X, pn_Start_X_initial_exec);
269   add_immBlock_pred (res->start_block, projX);
270   mature_immBlock   (res->start_block);  /* mature the start block */
271
272   add_immBlock_pred (new_immBlock (), projX);
273   mature_immBlock   (get_cur_block());   /* mature the 'body' block for expressions */
274
275   /* Set the visited flag high enough that the blocks will never be visited. */
276   set_irn_visited(get_cur_block(), -1);
277   set_Block_block_visited(get_cur_block(), -1);
278   set_Block_block_visited(res->start_block, -1);
279   set_irn_visited(res->start_block, -1);
280   set_irn_visited(res->bad, -1);
281   set_irn_visited(res->no_mem, -1);
282
283   res->phase_state = phase_high;
284   return res;
285 }
286
287 /* Defined in iropt.c */
288 void  del_identities (pset *value_table);
289
290 /* Frees the passed irgraph.
291    Deallocates all nodes in this graph and the ir_graph structure.
292    Sets the field irgraph in the corresponding entity to NULL.
293    Does not remove the irgraph from the list in irprog (requires
294    inefficient search, call remove_irp_irg by hand).
295    Does not free types, entities or modes that are used only by this
296    graph, nor the entity standing for this graph. */
297 void free_ir_graph (ir_graph *irg) {
298
299   stat_free_graph(irg);
300   if (irg->outs_state != outs_none) free_outs(irg);
301   if (irg->frame_type)  free_type(irg->frame_type);
302   if (irg->value_table) del_identities(irg->value_table);
303   if (irg->ent) {
304     peculiarity pec = get_entity_peculiarity (irg->ent);
305     set_entity_peculiarity (irg->ent, peculiarity_description);
306     set_entity_irg(irg->ent, NULL);  /* not set in const code irg */
307     set_entity_peculiarity (irg->ent, pec);
308   }
309
310   free_End(irg->end);
311   obstack_free(irg->obst,NULL);
312   free(irg->obst);
313 #if USE_EXPLICIT_PHI_IN_STACK
314   free_Phi_in_stack(irg->Phi_in_stack);
315 #endif
316   irg->kind = k_BAD;
317   free(irg);
318 }
319
320 /* access routines for all ir_graph attributes:
321    templates:
322    {attr type} get_irg_{attribute name} (ir_graph *irg);
323    void set_irg_{attr name} (ir_graph *irg, {attr type} {attr}); */
324
325 int
326 (is_ir_graph)(const void *thing) {
327   return __is_ir_graph(thing);
328 }
329
330 /* Outputs a unique number for this node */
331
332 INLINE long
333 get_irg_graph_nr(ir_graph *irg) {
334   assert(irg);
335 #ifdef DEBUG_libfirm
336   return irg->graph_nr;
337 #else
338   return (long)irg;
339 #endif
340 }
341
342 ir_node *
343 (get_irg_start_block)(const ir_graph *irg) {
344   return __get_irg_start_block(irg);
345 }
346
347 void
348 (set_irg_start_block)(ir_graph *irg, ir_node *node) {
349   __set_irg_start_block(irg, node);
350 }
351
352 ir_node *
353 (get_irg_start)(const ir_graph *irg) {
354   return __get_irg_start(irg);
355 }
356
357 void
358 (set_irg_start)(ir_graph *irg, ir_node *node) {
359   __set_irg_start(irg, node);
360 }
361
362 ir_node *
363 (get_irg_end_block)(const ir_graph *irg) {
364   return __get_irg_end_block(irg);
365 }
366
367 void
368 (set_irg_end_block)(ir_graph *irg, ir_node *node) {
369   __set_irg_end_block(irg, node);
370 }
371
372 ir_node *
373 (get_irg_end)(const ir_graph *irg) {
374   return __get_irg_end(irg);
375 }
376
377 void
378 (set_irg_end)(ir_graph *irg, ir_node *node) {
379   __set_irg_end(irg, node);
380 }
381
382 ir_node *
383 (get_irg_end_reg)(const ir_graph *irg) {
384   return __get_irg_end_reg(irg);
385 }
386
387 void     set_irg_end_reg (ir_graph *irg, ir_node *node) {
388   assert(get_irn_op(node) == op_EndReg || get_irn_op(node) == op_End);
389   irg->end_reg = node;
390 }
391
392 ir_node *
393 (get_irg_end_except)(const ir_graph *irg) {
394   return __get_irg_end_except(irg);
395 }
396
397 void     set_irg_end_except (ir_graph *irg, ir_node *node) {
398   assert(get_irn_op(node) == op_EndExcept || get_irn_op(node) == op_End);
399   irg->end_except = node;
400 }
401
402 ir_node *
403 (get_irg_cstore)(const ir_graph *irg) {
404   return __get_irg_cstore(irg);
405 }
406
407 void
408 (set_irg_cstore)(ir_graph *irg, ir_node *node) {
409   __set_irg_cstore(irg, node);
410 }
411
412 ir_node *
413 (get_irg_frame)(const ir_graph *irg) {
414   return __get_irg_frame(irg);
415 }
416
417 void
418 (set_irg_frame)(ir_graph *irg, ir_node *node) {
419   __set_irg_frame(irg, node);
420 }
421
422 ir_node *
423 (get_irg_globals)(const ir_graph *irg) {
424   return __get_irg_globals(irg);
425 }
426
427 void
428 (set_irg_globals)(ir_graph *irg, ir_node *node) {
429   __set_irg_globals(irg, node);
430 }
431
432 ir_node *
433 (get_irg_initial_mem)(const ir_graph *irg) {
434   return __get_irg_initial_mem(irg);
435 }
436
437 void
438 (set_irg_initial_mem)(ir_graph *irg, ir_node *node) {
439   __set_irg_initial_mem(irg, node);
440 }
441
442 ir_node *
443 (get_irg_args)(const ir_graph *irg) {
444   return __get_irg_args(irg);
445 }
446
447 void
448 (set_irg_args)(ir_graph *irg, ir_node *node) {
449   __set_irg_args(irg, node);
450 }
451
452 ir_node *
453 (get_irg_bad)(const ir_graph *irg) {
454   return __get_irg_bad(irg);
455 }
456
457 void
458 (set_irg_bad)(ir_graph *irg, ir_node *node) {
459   __set_irg_bad(irg, node);
460 }
461
462 ir_node *
463 (get_irg_no_mem)(const ir_graph *irg) {
464   return __get_irg_no_mem(irg);
465 }
466
467 void
468 (set_irg_no_mem)(ir_graph *irg, ir_node *node) {
469   __set_irg_no_mem(irg, node);
470 }
471
472 ir_node *
473 (get_irg_current_block)(const ir_graph *irg) {
474   return __get_irg_current_block(irg);
475 }
476
477 void
478 (set_irg_current_block)(ir_graph *irg, ir_node *node) {
479   __set_irg_current_block(irg, node);
480 }
481
482 entity *
483 (get_irg_entity)(const ir_graph *irg) {
484   return __get_irg_entity(irg);
485 }
486
487 void
488 (set_irg_entity)(ir_graph *irg, entity *ent) {
489   __set_irg_entity(irg, ent);
490 }
491
492 type *
493 (get_irg_frame_type)(const ir_graph *irg) {
494   return __get_irg_frame_type(irg);
495 }
496
497 void
498 (set_irg_frame_type)(ir_graph *irg, type *ftp) {
499   __set_irg_frame_type(irg, ftp);
500 }
501
502
503 /* To test for a frame type */
504 int
505 is_frame_type(const type *ftp) {
506   int i;
507   if (is_class_type(ftp)) {
508     for (i = 0; i < get_irp_n_irgs(); i++) {
509       const type *frame_tp = get_irg_frame_type(get_irp_irg(i));
510       if (ftp == frame_tp) return true;
511     }
512   }
513   return false;
514 }
515
516 int
517 get_irg_n_locs (ir_graph *irg)
518 {
519   if (get_opt_precise_exc_context())
520     return irg->n_loc - 1 - 1;
521   else
522     return irg->n_loc - 1;
523 }
524
525 void
526 set_irg_n_loc (ir_graph *irg, int n_loc)
527 {
528   if (get_opt_precise_exc_context())
529     irg->n_loc = n_loc + 1 + 1;
530   else
531     irg->n_loc = n_loc + 1;
532 }
533
534
535
536 /* Returns the obstack associated with the graph. */
537 struct obstack *
538 (get_irg_obstack)(const ir_graph *irg) {
539   return __get_irg_obstack(irg);
540 }
541
542 /*
543  * Returns true if the node n is allocated on the storage of graph irg.
544  *
545  * Implementation is GLIBC specific as is uses the internal _obstack_chunk implementation.
546  */
547 int node_is_in_irgs_storage(ir_graph *irg, ir_node *n)
548 {
549   struct _obstack_chunk *p;
550
551   /*
552    * checks wheater the ir_node pointer i on the obstack.
553    * A more sophisticated check would test the "whole" ir_node
554    */
555   for (p = irg->obst->chunk; p; p = p->prev) {
556     if (((char *)p->contents <= (char *)n) && ((char *)n < (char *)p->limit))
557       return 1;
558   }
559
560   return 0;
561 }
562
563 irg_phase_state
564 (get_irg_phase_state)(const ir_graph *irg) {
565   return __get_irg_phase_state(irg);
566 }
567
568 void
569 (set_irg_phase_low)(ir_graph *irg) {
570   __set_irg_phase_low(irg);
571 }
572
573 op_pin_state
574 (get_irg_pinned)(const ir_graph *irg) {
575   return __get_irg_pinned(irg);
576 }
577
578 irg_outs_state
579 (get_irg_outs_state)(const ir_graph *irg) {
580   return __get_irg_outs_state(irg);
581 }
582
583 void
584 (set_irg_outs_inconsistent)(ir_graph *irg) {
585   __set_irg_outs_inconsistent(irg);
586 }
587
588 irg_dom_state
589 (get_irg_dom_state)(const ir_graph *irg) {
590   return __get_irg_dom_state(irg);
591 }
592
593 void
594 (set_irg_dom_inconsistent)(ir_graph *irg) {
595   __set_irg_dom_inconsistent(irg);
596 }
597
598 irg_loopinfo_state
599 (get_irg_loopinfo_state)(const ir_graph *irg) {
600   return __get_irg_loopinfo_state(irg);
601 }
602
603 void
604 (set_irg_loopinfo_state)(ir_graph *irg, irg_loopinfo_state s) {
605   __set_irg_loopinfo_state(irg, s);
606 }
607
608 void
609 set_irg_loopinfo_inconsistent(ir_graph *irg) {
610   if (irg->loopinfo_state == loopinfo_ip_consistent)
611     irg->loopinfo_state = loopinfo_ip_inconsistent;
612
613   else if (irg->loopinfo_state == loopinfo_consistent)
614     irg->loopinfo_state = loopinfo_inconsistent;
615
616   else if (irg->loopinfo_state == loopinfo_cf_ip_consistent)
617     irg->loopinfo_state = loopinfo_cf_ip_inconsistent;
618
619   else if (irg->loopinfo_state == loopinfo_cf_consistent)
620     irg->loopinfo_state = loopinfo_cf_inconsistent;
621 }
622
623 void
624 (set_irg_pinned)(ir_graph *irg, op_pin_state p) {
625   __set_irg_pinned(irg, p);
626 }
627
628 irg_callee_info_state
629 (get_irg_callee_info_state)(const ir_graph *irg) {
630   return __get_irg_callee_info_state(irg);
631 }
632
633 void
634 (set_irg_callee_info_state)(ir_graph *irg, irg_callee_info_state s) {
635   __set_irg_callee_info_state(irg, s);
636 }
637
638 irg_inline_property
639 (get_irg_inline_property)(const ir_graph *irg) {
640   return __get_irg_inline_property(irg);
641 }
642
643 void
644 (set_irg_inline_property)(ir_graph *irg, irg_inline_property s) {
645   __set_irg_inline_property(irg, s);
646 }
647
648 void
649 (set_irg_link)(ir_graph *irg, void *thing) {
650   __set_irg_link(irg, thing);
651 }
652
653 void *
654 (get_irg_link)(const ir_graph *irg) {
655   return __get_irg_link(irg);
656 }
657
658 /* maximum visited flag content of all ir_graph visited fields. */
659 static int max_irg_visited = 0;
660
661 unsigned long
662 (get_irg_visited)(const ir_graph *irg) {
663   return __get_irg_visited(irg);
664 }
665
666 void
667 set_irg_visited (ir_graph *irg, unsigned long visited)
668 {
669   irg->visited = visited;
670   if (irg->visited > max_irg_visited) {
671     max_irg_visited = irg->visited;
672   }
673 }
674
675 void
676 inc_irg_visited (ir_graph *irg)
677 {
678   if (++irg->visited > max_irg_visited) {
679     max_irg_visited = irg->visited;
680   }
681 }
682
683 unsigned long
684 get_max_irg_visited(void)
685 {
686   /*
687   int i;
688   for(i = 0; i < get_irp_n_irgs(); i++)
689   assert(max_irg_visited >= get_irg_visited(get_irp_irg(i)));
690    */
691   return max_irg_visited;
692 }
693
694 void set_max_irg_visited(int val) {
695   max_irg_visited = val;
696 }
697
698 unsigned long
699 inc_max_irg_visited(void)
700 {
701   /*
702   int i;
703   for(i = 0; i < get_irp_n_irgs(); i++)
704   assert(max_irg_visited >= get_irg_visited(get_irp_irg(i)));
705   */
706   max_irg_visited++;
707   return max_irg_visited;
708 }
709
710 unsigned long
711 (get_irg_block_visited)(const ir_graph *irg) {
712   return __get_irg_block_visited(irg);
713 }
714
715 void
716 (set_irg_block_visited)(ir_graph *irg, unsigned long visited) {
717   __set_irg_block_visited(irg, visited);
718 }
719
720 void
721 (inc_irg_block_visited)(ir_graph *irg) {
722   __inc_irg_block_visited(irg);
723 }
724
725
726 /**
727  * walker Start->End: places Proj nodes into the same block
728  * as it's predecessors
729  *
730  * @param n    the node
731  * @param env  ignored
732  */
733 static void normalize_proj_walker(ir_node *n, void *env)
734 {
735   if (is_Proj(n)) {
736     ir_node *pred  = get_Proj_pred(n);
737     ir_node *block = get_nodes_block(pred);
738
739     set_nodes_block(n, block);
740   }
741 }
742
743 /* put the proj's into the same block as its predecessors */
744 void normalize_proj_nodes(ir_graph *irg)
745 {
746   irg_walk_graph(irg, NULL, normalize_proj_walker, NULL);
747         set_irg_outs_inconsistent(irg);
748 }
749
750 size_t register_additional_graph_data(size_t size)
751 {
752         assert(!forbid_new_data && "Too late to register additional node data");
753
754         if(forbid_new_data)
755                 return 0;
756
757         return additional_graph_data_size += size;
758 }