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