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