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