Remove outs_state attribute
[libfirm] / ir / ir / irgraph.c
1 /*
2  * Copyright (C) 1995-2011 University of Karlsruhe.  All right reserved.
3  *
4  * This file is part of libFirm.
5  *
6  * This file may be distributed and/or modified under the terms of the
7  * GNU General Public License version 2 as published by the Free Software
8  * Foundation and appearing in the file LICENSE.GPL included in the
9  * packaging of this file.
10  *
11  * Licensees holding valid libFirm Professional Edition licenses may use
12  * this file in accordance with the libFirm Commercial License.
13  * Agreement provided with the Software.
14  *
15  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
16  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17  * PURPOSE.
18  */
19
20 /**
21  * @file
22  * @brief    Entry point to the representation of procedure code.
23  * @author   Martin Trapp, Christian Schaefer, Goetz Lindenmaier, Michael Beck
24  * @version  $Id$
25  */
26 #include "config.h"
27
28 #include <string.h>
29 #include <stddef.h>
30
31 #include "xmalloc.h"
32 #include "ircons_t.h"
33 #include "irgraph_t.h"
34 #include "irprog_t.h"
35 #include "irgraph_t.h"
36 #include "irnode_t.h"
37 #include "iropt_t.h"
38 #include "irflag_t.h"
39 #include "array.h"
40 #include "irgmod.h"
41 #include "irouts.h"
42 #include "irhooks.h"
43 #include "irtools.h"
44 #include "irgwalk.h"
45 #include "irbackedge_t.h"
46 #include "iredges_t.h"
47 #include "type_t.h"
48 #include "irmemory.h"
49 #include "irphase.h"
50
51 #define INITIAL_IDX_IRN_MAP_SIZE 1024
52
53 /**
54  * Indicates, whether additional data can be registered to graphs.
55  * If set to 1, this is not possible anymore.
56  */
57 static int forbid_new_data = 0;
58
59 /**
60  * The amount of additional space for custom data to be allocated upon
61  * creating a new graph.
62  */
63 static size_t additional_graph_data_size = 0;
64
65 ir_graph *current_ir_graph;
66 ir_graph *get_current_ir_graph(void)
67 {
68         return current_ir_graph;
69 }
70
71 void set_current_ir_graph(ir_graph *graph)
72 {
73         current_ir_graph = graph;
74 }
75
76 /** contains the suffix for frame type names */
77 static ident *frame_type_suffix = NULL;
78
79 /* initialize the IR graph module */
80 void firm_init_irgraph(void)
81 {
82         frame_type_suffix = new_id_from_str(FRAME_TP_SUFFIX);
83         forbid_new_data   = 1;
84 }
85
86 /**
87  * Allocate a new IR graph.
88  * This function respects the registered graph data. The only reason for
89  * this function is, that there are two locations, where graphs are
90  * allocated (new_r_ir_graph, new_const_code_irg).
91  * @return Memory for a new graph.
92  */
93 static ir_graph *alloc_graph(void)
94 {
95         ir_graph *res;
96         size_t   size = sizeof(ir_graph) + additional_graph_data_size;
97         char     *ptr = XMALLOCNZ(char, size);
98
99         res = (ir_graph *)(ptr + additional_graph_data_size);
100         res->kind = k_ir_graph;
101
102         /* initialize the idx->node map. */
103         res->idx_irn_map = NEW_ARR_F(ir_node *, INITIAL_IDX_IRN_MAP_SIZE);
104         memset(res->idx_irn_map, 0, INITIAL_IDX_IRN_MAP_SIZE * sizeof(res->idx_irn_map[0]));
105
106         return res;
107 }
108
109 /**
110  * Frees an allocated IR graph
111  */
112 static void free_graph(ir_graph *irg)
113 {
114         char           *ptr = (char *)irg;
115         ir_edge_kind_t  i;
116
117         for (i = EDGE_KIND_FIRST; i < EDGE_KIND_LAST; ++i)
118                 edges_deactivate_kind(irg, i);
119         DEL_ARR_F(irg->idx_irn_map);
120         free(ptr - additional_graph_data_size);
121 }
122
123 /**
124  * Set the number of locals for a given graph.
125  *
126  * @param irg    the graph
127  * @param n_loc  number of locals
128  */
129 void irg_set_nloc(ir_graph *res, int n_loc)
130 {
131         assert(res->phase_state == phase_building);
132
133         res->n_loc = n_loc + 1;     /* number of local variables that are never
134                                        dereferenced in this graph plus one for
135                                        the store. This is not the number of
136                                        parameters to the procedure!  */
137
138         if (res->loc_descriptions) {
139                 xfree(res->loc_descriptions);
140                 res->loc_descriptions = NULL;
141         }
142 }
143
144 /* Allocates a list of nodes:
145     - The start block containing a start node and Proj nodes for its four
146       results (X, M, P, Tuple).
147     - The end block containing an end node. This block is not matured after
148       new_ir_graph as predecessors need to be added to it.
149     - The current block, which is empty and also not matured.
150    Further it allocates several datastructures needed for graph construction
151    and optimization.
152 */
153 ir_graph *new_r_ir_graph(ir_entity *ent, int n_loc)
154 {
155         ir_graph *res;
156         ir_node  *first_block;
157         ir_node  *start, *start_block, *initial_mem, *projX;
158
159         res = alloc_graph();
160
161         /* inform statistics here, as blocks will be already build on this graph */
162         hook_new_graph(res, ent);
163
164         /*-- initialized for each graph. --*/
165         res->kind = k_ir_graph;
166         res->obst = XMALLOC(struct obstack);
167         obstack_init(res->obst);
168
169         res->phase_state = phase_building;
170         irg_set_nloc(res, n_loc);
171
172         /* descriptions will be allocated on demand */
173         res->loc_descriptions = NULL;
174
175         res->visited       = 0; /* visited flag, for the ir walker */
176         res->block_visited = 0; /* visited flag, for the 'block'-walker */
177
178         res->extbb_obst = NULL;
179
180         res->last_node_idx = 0;
181
182         new_identities(res);
183         res->outs = NULL;
184
185         res->inline_property       = irg_inline_any;
186         res->additional_properties = mtp_property_inherited;  /* inherited from type */
187
188         res->irg_pinned_state    = op_pin_state_pinned;
189         res->dom_state           = dom_none;
190         res->pdom_state          = dom_none;
191         res->typeinfo_state      = ir_typeinfo_none;
192         set_irp_typeinfo_inconsistent();           /* there is a new graph with typeinfo_none. */
193         res->callee_info_state   = irg_callee_info_none;
194         res->loopinfo_state      = loopinfo_none;
195         res->class_cast_state    = ir_class_casts_transitive;
196         res->extblk_state        = ir_extblk_info_none;
197         res->execfreq_state      = exec_freq_none;
198         res->fp_model            = fp_model_precise;
199         res->entity_usage_state  = ir_entity_usage_not_computed;
200         res->mem_disambig_opt    = aa_opt_inherited;
201
202         /*-- Type information for the procedure of the graph --*/
203         res->ent = ent;
204         set_entity_irg(ent, res);
205
206         /*--  a class type so that it can contain "inner" methods as in Pascal. --*/
207         res->frame_type = new_type_frame();
208
209         /* the Anchor node must be created first */
210         res->anchor = new_r_Anchor(res);
211
212         /*-- Nodes needed in every graph --*/
213         set_irg_end_block (res, new_r_immBlock(res));
214         set_irg_end(res, new_r_End(res, 0, NULL));
215
216         start_block = new_r_Block_noopt(res, 0, NULL);
217         set_irg_start_block(res, start_block);
218         set_irg_no_mem     (res, new_r_NoMem(res));
219         start = new_r_Start(res);
220         set_irg_start      (res, start);
221
222         /* Proj results of start node */
223         projX                   = new_r_Proj(start, mode_X, pn_Start_X_initial_exec);
224         set_irg_initial_exec    (res, projX);
225         set_irg_frame           (res, new_r_Proj(start, mode_P_data, pn_Start_P_frame_base));
226         set_irg_args            (res, new_r_Proj(start, mode_T,      pn_Start_T_args));
227         initial_mem             = new_r_Proj(start, mode_M, pn_Start_M);
228         set_irg_initial_mem(res, initial_mem);
229
230         res->index       = get_irp_new_irg_idx();
231 #ifdef DEBUG_libfirm
232         res->graph_nr    = get_irp_new_node_nr();
233 #endif
234
235         set_r_cur_block(res, start_block);
236         set_r_store(res, initial_mem);
237
238         /*-- Make a block to start with --*/
239         first_block = new_r_immBlock(res);
240         set_r_cur_block(res, first_block);
241         add_immBlock_pred(first_block, projX);
242
243         res->method_execution_frequency = -1.0;
244         res->estimated_node_count       = 0;
245
246         return res;
247 }
248
249 ir_graph *new_ir_graph(ir_entity *ent, int n_loc)
250 {
251         ir_graph *res = new_r_ir_graph(ent, n_loc);
252         add_irp_irg(res);          /* remember this graph global. */
253         return res;
254 }
255
256 /* Make a rudimentary IR graph for the constant code.
257    Must look like a correct irg, spare everything else. */
258 ir_graph *new_const_code_irg(void)
259 {
260         ir_graph *res = alloc_graph();
261         ir_node  *body_block;
262         ir_node  *end;
263         ir_node  *end_block;
264         ir_node  *no_mem;
265         ir_node  *projX;
266         ir_node  *start_block;
267         ir_node  *start;
268
269         /* inform statistics here, as blocks will be already build on this graph */
270         hook_new_graph(res, NULL);
271
272         res->n_loc         = 1; /* Only the memory. */
273         res->visited       = 0; /* visited flag, for the ir walker */
274         res->block_visited = 0; /* visited flag, for the 'block'-walker */
275         res->obst          = XMALLOC(struct obstack);
276         obstack_init(res->obst);
277         res->extbb_obst = NULL;
278
279         res->last_node_idx = 0;
280
281         res->phase_state      = phase_building;
282         res->irg_pinned_state = op_pin_state_pinned;
283         res->extblk_state     = ir_extblk_info_none;
284         res->fp_model         = fp_model_precise;
285
286         /* value table for global value numbering for optimizing use in iropt.c */
287         new_identities(res);
288         res->ent         = NULL;
289         res->frame_type  = NULL;
290
291         /* the Anchor node must be created first */
292         res->anchor = new_r_Anchor(res);
293
294         /* -- The end block -- */
295         end_block = new_r_Block_noopt(res, 0, NULL);
296         set_irg_end_block(res, end_block);
297         end = new_r_End(res, 0, NULL);
298         set_irg_end(res, end);
299
300         /* -- The start block -- */
301         start_block = new_r_Block_noopt(res, 0, NULL);
302         set_irg_start_block(res, start_block);
303         no_mem = new_r_NoMem(res);
304         set_irg_no_mem(res, no_mem);
305         start = new_r_Start(res);
306         set_irg_start(res, start);
307
308         /* Proj results of start node */
309         set_irg_initial_mem(res, new_r_Proj(start, mode_M, pn_Start_M));
310         projX = new_r_Proj(start, mode_X, pn_Start_X_initial_exec);
311
312         body_block = new_r_Block(res, 1, &projX);
313
314         set_r_cur_block(res, body_block);
315
316         /* Set the visited flag high enough that the blocks will never be visited. */
317         set_irn_visited(body_block, -1);
318         set_Block_block_visited(body_block, -1);
319         set_Block_block_visited(start_block, -1);
320         set_irn_visited(start_block, -1);
321         set_irn_visited(no_mem, -1);
322
323         return res;
324 }
325
326 /**
327  * Pre-Walker: Copies blocks and nodes from the original method graph
328  * to the copied graph.
329  *
330  * @param n    A node from the original method graph.
331  * @param env  The copied graph.
332  */
333 static void copy_all_nodes(ir_node *node, void *env)
334 {
335         ir_graph *irg      = (ir_graph*)env;
336         ir_node  *new_node = irn_copy_into_irg(node, irg);
337
338         set_irn_link(node, new_node);
339
340         /* fix access to entities on the stack frame */
341         if (is_Sel(new_node)) {
342                 ir_entity *ent = get_Sel_entity(new_node);
343                 ir_type   *tp  = get_entity_owner(ent);
344
345                 if (is_frame_type(tp)) {
346                         /* replace by the copied entity */
347                         ent = (ir_entity*)get_entity_link(ent);
348
349                         assert(is_entity(ent));
350                         assert(get_entity_owner(ent) == get_irg_frame_type(irg));
351                         set_Sel_entity(new_node, ent);
352                 }
353         }
354 }
355
356 /**
357  * Post-walker: Set the predecessors of the copied nodes.
358  * The copied nodes are set as link of their original nodes. The links of
359  * "irn" predecessors are the predecessors of copied node.
360  */
361 static void rewire(ir_node *irn, void *env)
362 {
363         (void) env;
364         irn_rewire_inputs(irn);
365 }
366
367 static ir_node *get_new_node(const ir_node *old_node)
368 {
369         return (ir_node*) get_irn_link(old_node);
370 }
371
372 /*
373  * Create a new graph that is a copy of a given one.
374  */
375 ir_graph *create_irg_copy(ir_graph *irg)
376 {
377         ir_graph *res;
378
379         res = alloc_graph();
380
381         res->n_loc = 0;
382         res->visited = 0;       /* visited flag, for the ir walker */
383         res->block_visited = 0; /* visited flag, for the 'block'-walker */
384         res->obst       = XMALLOC(struct obstack);
385         obstack_init(res->obst);
386         res->extbb_obst = NULL;
387
388         res->last_node_idx = 0;
389
390         res->phase_state      = irg->phase_state;
391         res->irg_pinned_state = irg->irg_pinned_state;
392         res->extblk_state     = ir_extblk_info_none;
393         res->fp_model         = irg->fp_model;
394
395         new_identities(res);
396
397         /* clone the frame type here for safety */
398         irp_reserve_resources(irp, IRP_RESOURCE_ENTITY_LINK);
399         res->frame_type  = clone_frame_type(irg->frame_type);
400
401         res->phase_state = irg->phase_state;
402
403         ir_reserve_resources(irg, IR_RESOURCE_IRN_LINK);
404
405         /* copy all nodes from the graph irg to the new graph res */
406         irg_walk_anchors(irg, copy_all_nodes, rewire, res);
407
408         /* copy the Anchor node */
409         res->anchor = get_new_node(irg->anchor);
410
411         /* -- The end block -- */
412         set_irg_end_block (res, get_new_node(get_irg_end_block(irg)));
413         set_irg_end       (res, get_new_node(get_irg_end(irg)));
414
415         /* -- The start block -- */
416         set_irg_start_block(res, get_new_node(get_irg_start_block(irg)));
417         set_irg_no_mem     (res, get_new_node(get_irg_no_mem(irg)));
418         set_irg_start      (res, get_new_node(get_irg_start(irg)));
419
420         /* Proj results of start node */
421         set_irg_initial_mem(res, get_new_node(get_irg_initial_mem(irg)));
422
423         /* Copy the node count estimation. Would be strange if this
424            is different from the original one. */
425         res->estimated_node_count = irg->estimated_node_count;
426
427         ir_free_resources(irg, IR_RESOURCE_IRN_LINK);
428         irp_free_resources(irp, IRP_RESOURCE_ENTITY_LINK);
429
430         return res;
431 }
432
433 /* Frees the passed irgraph.
434    Deallocates all nodes in this graph and the ir_graph structure.
435    Sets the field irgraph in the corresponding entity to NULL.
436    Does not remove the irgraph from the list in irprog (requires
437    inefficient search, call remove_irp_irg by hand).
438    Does not free types, entities or modes that are used only by this
439    graph, nor the entity standing for this graph. */
440 void free_ir_graph(ir_graph *irg)
441 {
442         assert(is_ir_graph(irg));
443
444         edges_deactivate(irg);
445
446         hook_free_graph(irg);
447         free_irg_outs(irg);
448         if (irg->frame_type)
449                 free_type(irg->frame_type);
450         del_identities(irg);
451         if (irg->ent) {
452                 set_entity_irg(irg->ent, NULL);  /* not set in const code irg */
453         }
454
455         free_End(get_irg_end(irg));
456         obstack_free(irg->obst,NULL);
457         free(irg->obst);
458         if (irg->loc_descriptions)
459                 free(irg->loc_descriptions);
460         irg->kind = k_BAD;
461         free_graph(irg);
462 }
463
464 /* access routines for all ir_graph attributes:
465    templates:
466    {attr type} get_irg_{attribute name} (ir_graph *irg);
467    void set_irg_{attr name} (ir_graph *irg, {attr type} {attr}); */
468
469 int (is_ir_graph)(const void *thing)
470 {
471         return _is_ir_graph(thing);
472 }
473
474 #ifdef DEBUG_libfirm
475 /* Outputs a unique number for this node */
476 long get_irg_graph_nr(const ir_graph *irg)
477 {
478         return irg->graph_nr;
479 }
480 #else
481 long get_irg_graph_nr(const ir_graph *irg)
482 {
483         return PTR_TO_INT(irg);
484 }
485 #endif
486
487 size_t get_irg_idx(const ir_graph *irg)
488 {
489         return irg->index;
490 }
491
492 ir_node *(get_idx_irn)(const ir_graph *irg, unsigned idx)
493 {
494         return _get_idx_irn(irg, idx);
495 }
496
497 ir_node *(get_irg_start_block)(const ir_graph *irg)
498 {
499         return _get_irg_start_block(irg);
500 }
501
502 void (set_irg_start_block)(ir_graph *irg, ir_node *node)
503 {
504         _set_irg_start_block(irg, node);
505 }
506
507 ir_node *(get_irg_start)(const ir_graph *irg)
508 {
509         return _get_irg_start(irg);
510 }
511
512 void (set_irg_start)(ir_graph *irg, ir_node *node)
513 {
514         _set_irg_start(irg, node);
515 }
516
517 ir_node *(get_irg_end_block)(const ir_graph *irg)
518 {
519         return _get_irg_end_block(irg);
520 }
521
522 void (set_irg_end_block)(ir_graph *irg, ir_node *node)
523 {
524   _set_irg_end_block(irg, node);
525 }
526
527 ir_node *(get_irg_end)(const ir_graph *irg)
528 {
529         return _get_irg_end(irg);
530 }
531
532 void (set_irg_end)(ir_graph *irg, ir_node *node)
533 {
534         _set_irg_end(irg, node);
535 }
536
537 ir_node *(get_irg_initial_exec)(const ir_graph *irg)
538 {
539         return _get_irg_initial_exec(irg);
540 }
541
542 void (set_irg_initial_exec)(ir_graph *irg, ir_node *node)
543 {
544         _set_irg_initial_exec(irg, node);
545 }
546
547 ir_node *(get_irg_frame)(const ir_graph *irg)
548 {
549         return _get_irg_frame(irg);
550 }
551
552 void (set_irg_frame)(ir_graph *irg, ir_node *node)
553 {
554         _set_irg_frame(irg, node);
555 }
556
557 ir_node *(get_irg_initial_mem)(const ir_graph *irg)
558 {
559         return _get_irg_initial_mem(irg);
560 }
561
562 void (set_irg_initial_mem)(ir_graph *irg, ir_node *node)
563 {
564         _set_irg_initial_mem(irg, node);
565 }
566
567 ir_node *(get_irg_args)(const ir_graph *irg)
568 {
569         return _get_irg_args(irg);
570 }
571
572 void (set_irg_args)(ir_graph *irg, ir_node *node)
573 {
574         _set_irg_args(irg, node);
575 }
576
577 ir_node *(get_irg_no_mem)(const ir_graph *irg)
578 {
579         return _get_irg_no_mem(irg);
580 }
581
582 void (set_irg_no_mem)(ir_graph *irg, ir_node *node)
583 {
584         _set_irg_no_mem(irg, node);
585 }
586
587 ir_entity *(get_irg_entity)(const ir_graph *irg)
588 {
589         return _get_irg_entity(irg);
590 }
591
592 void (set_irg_entity)(ir_graph *irg, ir_entity *ent)
593 {
594         _set_irg_entity(irg, ent);
595 }
596
597 ir_type *(get_irg_frame_type)(ir_graph *irg)
598 {
599         return _get_irg_frame_type(irg);
600 }
601
602 void (set_irg_frame_type)(ir_graph *irg, ir_type *ftp)
603 {
604         _set_irg_frame_type(irg, ftp);
605 }
606
607 int get_irg_n_locs(ir_graph *irg)
608 {
609         return irg->n_loc - 1;
610 }
611
612 /* Returns the obstack associated with the graph. */
613 struct obstack *
614 (get_irg_obstack)(const ir_graph *irg) {
615         return _get_irg_obstack(irg);
616 }
617
618 /*
619  * Returns true if the node n is allocated on the storage of graph irg.
620  *
621  * Implementation is GLIBC specific as is uses the internal _obstack_chunk implementation.
622  */
623 int node_is_in_irgs_storage(const ir_graph *irg, const ir_node *n)
624 {
625         struct _obstack_chunk *p;
626
627         /*
628          * checks weather the ir_node pointer is on the obstack.
629          * A more sophisticated check would test the "whole" ir_node
630          */
631         for (p = irg->obst->chunk; p; p = p->prev) {
632                 if (((char *)p->contents <= (char *)n) && ((char *)n < (char *)p->limit))
633                         return 1;
634         }
635
636         return 0;
637 }
638
639 irg_phase_state (get_irg_phase_state)(const ir_graph *irg)
640 {
641         return _get_irg_phase_state(irg);
642 }
643
644 void (set_irg_phase_state)(ir_graph *irg, irg_phase_state state)
645 {
646         _set_irg_phase_state(irg, state);
647 }
648
649 op_pin_state (get_irg_pinned)(const ir_graph *irg)
650 {
651         return _get_irg_pinned(irg);
652 }
653
654 irg_extblk_info_state (get_irg_extblk_state)(const ir_graph *irg)
655 {
656         return _get_irg_extblk_state(irg);
657 }
658
659 void (set_irg_extblk_inconsistent)(ir_graph *irg)
660 {
661         _set_irg_extblk_inconsistent(irg);
662 }
663
664 irg_dom_state (get_irg_dom_state)(const ir_graph *irg)
665 {
666         return _get_irg_dom_state(irg);
667 }
668
669 irg_dom_state (get_irg_postdom_state)(const ir_graph *irg)
670 {
671         return _get_irg_postdom_state(irg);
672 }
673
674 void (set_irg_doms_inconsistent)(ir_graph *irg)
675 {
676         _set_irg_doms_inconsistent(irg);
677 }
678
679 irg_loopinfo_state (get_irg_loopinfo_state)(const ir_graph *irg)
680 {
681         return _get_irg_loopinfo_state(irg);
682 }
683
684 void (set_irg_loopinfo_state)(ir_graph *irg, irg_loopinfo_state s)
685 {
686         _set_irg_loopinfo_state(irg, s);
687 }
688
689 void (set_irg_loopinfo_inconsistent)(ir_graph *irg)
690 {
691         _set_irg_loopinfo_inconsistent(irg);
692 }
693
694 void set_irp_loopinfo_inconsistent(void)
695 {
696         size_t i, n;
697         for (i = 0, n = get_irp_n_irgs(); i < n; ++i) {
698                 set_irg_loopinfo_inconsistent(get_irp_irg(i));
699         }
700 }
701
702
703
704 void (set_irg_pinned)(ir_graph *irg, op_pin_state p)
705 {
706         _set_irg_pinned(irg, p);
707 }
708
709 irg_callee_info_state (get_irg_callee_info_state)(const ir_graph *irg)
710 {
711         return _get_irg_callee_info_state(irg);
712 }
713
714 void (set_irg_callee_info_state)(ir_graph *irg, irg_callee_info_state s)
715 {
716         _set_irg_callee_info_state(irg, s);
717 }
718
719 irg_inline_property (get_irg_inline_property)(const ir_graph *irg)
720 {
721         return _get_irg_inline_property(irg);
722 }
723
724 void (set_irg_inline_property)(ir_graph *irg, irg_inline_property s)
725 {
726         _set_irg_inline_property(irg, s);
727 }
728
729 mtp_additional_properties (get_irg_additional_properties)(const ir_graph *irg)
730 {
731         return _get_irg_additional_properties(irg);
732 }
733
734 void (set_irg_additional_properties)(ir_graph *irg, mtp_additional_properties property_mask)
735 {
736         _set_irg_additional_properties(irg, property_mask);
737 }
738
739 void (add_irg_additional_properties)(ir_graph *irg, mtp_additional_properties flag)
740 {
741         _add_irg_additional_properties(irg, flag);
742 }
743
744 void (set_irg_link)(ir_graph *irg, void *thing)
745 {
746         _set_irg_link(irg, thing);
747 }
748
749 void *(get_irg_link)(const ir_graph *irg)
750 {
751         return _get_irg_link(irg);
752 }
753
754 ir_visited_t (get_irg_visited)(const ir_graph *irg)
755 {
756         return _get_irg_visited(irg);
757 }
758
759 /** maximum visited flag content of all ir_graph visited fields. */
760 static ir_visited_t max_irg_visited = 0;
761
762 void set_irg_visited(ir_graph *irg, ir_visited_t visited)
763 {
764         irg->visited = visited;
765         if (irg->visited > max_irg_visited) {
766                 max_irg_visited = irg->visited;
767         }
768 }
769
770 void inc_irg_visited(ir_graph *irg)
771 {
772         ++irg->visited;
773         if (irg->visited > max_irg_visited) {
774                 max_irg_visited = irg->visited;
775         }
776 }
777
778 ir_visited_t get_max_irg_visited(void)
779 {
780         return max_irg_visited;
781 }
782
783 void set_max_irg_visited(int val)
784 {
785         max_irg_visited = val;
786 }
787
788 ir_visited_t inc_max_irg_visited(void)
789 {
790 #ifndef NDEBUG
791         size_t i;
792         for (i = 0; i < get_irp_n_irgs(); i++)
793                 assert(max_irg_visited >= get_irg_visited(get_irp_irg(i)));
794 #endif
795         return ++max_irg_visited;
796 }
797
798 ir_visited_t (get_irg_block_visited)(const ir_graph *irg)
799 {
800         return _get_irg_block_visited(irg);
801 }
802
803 void (set_irg_block_visited)(ir_graph *irg, ir_visited_t visited)
804 {
805         _set_irg_block_visited(irg, visited);
806 }
807
808 void (inc_irg_block_visited)(ir_graph *irg)
809 {
810   _inc_irg_block_visited(irg);
811 }
812
813 /* Return the floating point model of this graph. */
814 unsigned (get_irg_fp_model)(const ir_graph *irg)
815 {
816         return _get_irg_fp_model(irg);
817 }
818
819 /* Sets the floating point model for this graph. */
820 void set_irg_fp_model(ir_graph *irg, unsigned model)
821 {
822         irg->fp_model = model;
823 }
824
825 /* set a description for local value n */
826 void set_irg_loc_description(ir_graph *irg, int n, void *description)
827 {
828         assert(0 <= n && n < irg->n_loc);
829
830         if (! irg->loc_descriptions)
831                 irg->loc_descriptions = XMALLOCNZ(void*, irg->n_loc);
832
833         irg->loc_descriptions[n] = description;
834 }
835
836 /* get the description for local value n */
837 void *get_irg_loc_description(ir_graph *irg, int n)
838 {
839         assert(0 <= n && n < irg->n_loc);
840         return irg->loc_descriptions ? irg->loc_descriptions[n] : NULL;
841 }
842
843 void irg_register_phase(ir_graph *irg, ir_phase_id id, ir_phase *phase)
844 {
845         assert(id <= PHASE_LAST);
846         assert(irg->phases[id] == NULL);
847         irg->phases[id] = phase;
848 }
849
850 void irg_invalidate_phases(ir_graph *irg)
851 {
852         int p;
853         for (p = 0; p <= PHASE_LAST; ++p) {
854                 ir_phase *phase = irg->phases[p];
855                 if (phase == NULL)
856                         continue;
857
858                 phase_free(phase);
859                 irg->phases[p] = NULL;
860         }
861 }
862
863 #ifndef NDEBUG
864 void ir_reserve_resources(ir_graph *irg, ir_resources_t resources)
865 {
866         assert((irg->reserved_resources & resources) == 0);
867         irg->reserved_resources |= resources;
868 }
869
870 void ir_free_resources(ir_graph *irg, ir_resources_t resources)
871 {
872         assert((irg->reserved_resources & resources) == resources);
873         irg->reserved_resources &= ~resources;
874 }
875
876 ir_resources_t ir_resources_reserved(const ir_graph *irg)
877 {
878         return irg->reserved_resources;
879 }
880 #endif /* NDEBUG */
881
882 /* Returns a estimated node count of the irg. */
883 unsigned (get_irg_estimated_node_cnt)(const ir_graph *irg)
884 {
885         return _get_irg_estimated_node_cnt(irg);
886 }
887
888 /* Returns the last irn index for this graph. */
889 unsigned get_irg_last_idx(const ir_graph *irg)
890 {
891         return irg->last_node_idx;
892 }
893
894 /* register additional space in an IR graph */
895 size_t register_additional_graph_data(size_t size)
896 {
897         assert(!forbid_new_data && "Too late to register additional node data");
898
899         if (forbid_new_data)
900                 return 0;
901
902         return additional_graph_data_size += size;
903 }
904
905 void (set_irg_state)(ir_graph *irg, ir_graph_state_t state)
906 {
907         _set_irg_state(irg, state);
908 }
909
910 void (clear_irg_state)(ir_graph *irg, ir_graph_state_t state)
911 {
912         _clear_irg_state(irg, state);
913 }
914
915 int (is_irg_state)(const ir_graph *irg, ir_graph_state_t state)
916 {
917         return _is_irg_state(irg, state);
918 }