Merge branch 'opt_manage'
[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->typeinfo_state      = ir_typeinfo_none;
190         set_irp_typeinfo_inconsistent();           /* there is a new graph with typeinfo_none. */
191         res->callee_info_state   = irg_callee_info_none;
192         res->loopinfo_state      = loopinfo_none;
193         res->class_cast_state    = ir_class_casts_transitive;
194         res->extblk_state        = ir_extblk_info_none;
195         res->execfreq_state      = exec_freq_none;
196         res->fp_model            = fp_model_precise;
197         res->mem_disambig_opt    = aa_opt_inherited;
198
199         /*-- Type information for the procedure of the graph --*/
200         res->ent = ent;
201         set_entity_irg(ent, res);
202
203         /*--  a class type so that it can contain "inner" methods as in Pascal. --*/
204         res->frame_type = new_type_frame();
205
206         /* the Anchor node must be created first */
207         res->anchor = new_r_Anchor(res);
208
209         /*-- Nodes needed in every graph --*/
210         set_irg_end_block (res, new_r_immBlock(res));
211         set_irg_end(res, new_r_End(res, 0, NULL));
212
213         start_block = new_r_Block_noopt(res, 0, NULL);
214         set_irg_start_block(res, start_block);
215         set_irg_no_mem     (res, new_r_NoMem(res));
216         start = new_r_Start(res);
217         set_irg_start      (res, start);
218
219         /* Proj results of start node */
220         projX                   = new_r_Proj(start, mode_X, pn_Start_X_initial_exec);
221         set_irg_initial_exec    (res, projX);
222         set_irg_frame           (res, new_r_Proj(start, mode_P_data, pn_Start_P_frame_base));
223         set_irg_args            (res, new_r_Proj(start, mode_T,      pn_Start_T_args));
224         initial_mem             = new_r_Proj(start, mode_M, pn_Start_M);
225         set_irg_initial_mem(res, initial_mem);
226
227         res->index       = get_irp_new_irg_idx();
228 #ifdef DEBUG_libfirm
229         res->graph_nr    = get_irp_new_node_nr();
230 #endif
231
232         set_r_cur_block(res, start_block);
233         set_r_store(res, initial_mem);
234
235         /*-- Make a block to start with --*/
236         first_block = new_r_immBlock(res);
237         set_r_cur_block(res, first_block);
238         add_immBlock_pred(first_block, projX);
239
240         res->method_execution_frequency = -1.0;
241         res->estimated_node_count       = 0;
242
243         return res;
244 }
245
246 ir_graph *new_ir_graph(ir_entity *ent, int n_loc)
247 {
248         ir_graph *res = new_r_ir_graph(ent, n_loc);
249         add_irp_irg(res);          /* remember this graph global. */
250         return res;
251 }
252
253 /* Make a rudimentary IR graph for the constant code.
254    Must look like a correct irg, spare everything else. */
255 ir_graph *new_const_code_irg(void)
256 {
257         ir_graph *res = alloc_graph();
258         ir_node  *body_block;
259         ir_node  *end;
260         ir_node  *end_block;
261         ir_node  *no_mem;
262         ir_node  *projX;
263         ir_node  *start_block;
264         ir_node  *start;
265
266         /* inform statistics here, as blocks will be already build on this graph */
267         hook_new_graph(res, NULL);
268
269         res->n_loc         = 1; /* Only the memory. */
270         res->visited       = 0; /* visited flag, for the ir walker */
271         res->block_visited = 0; /* visited flag, for the 'block'-walker */
272         res->obst          = XMALLOC(struct obstack);
273         obstack_init(res->obst);
274         res->extbb_obst = NULL;
275
276         res->last_node_idx = 0;
277
278         res->phase_state      = phase_building;
279         res->irg_pinned_state = op_pin_state_pinned;
280         res->extblk_state     = ir_extblk_info_none;
281         res->fp_model         = fp_model_precise;
282
283         /* value table for global value numbering for optimizing use in iropt.c */
284         new_identities(res);
285         res->ent         = NULL;
286         res->frame_type  = NULL;
287
288         /* the Anchor node must be created first */
289         res->anchor = new_r_Anchor(res);
290
291         /* -- The end block -- */
292         end_block = new_r_Block_noopt(res, 0, NULL);
293         set_irg_end_block(res, end_block);
294         end = new_r_End(res, 0, NULL);
295         set_irg_end(res, end);
296
297         /* -- The start block -- */
298         start_block = new_r_Block_noopt(res, 0, NULL);
299         set_irg_start_block(res, start_block);
300         no_mem = new_r_NoMem(res);
301         set_irg_no_mem(res, no_mem);
302         start = new_r_Start(res);
303         set_irg_start(res, start);
304
305         /* Proj results of start node */
306         set_irg_initial_mem(res, new_r_Proj(start, mode_M, pn_Start_M));
307         projX = new_r_Proj(start, mode_X, pn_Start_X_initial_exec);
308
309         body_block = new_r_Block(res, 1, &projX);
310
311         set_r_cur_block(res, body_block);
312
313         /* Set the visited flag high enough that the blocks will never be visited. */
314         set_irn_visited(body_block, -1);
315         set_Block_block_visited(body_block, -1);
316         set_Block_block_visited(start_block, -1);
317         set_irn_visited(start_block, -1);
318         set_irn_visited(no_mem, -1);
319
320         return res;
321 }
322
323 /**
324  * Pre-Walker: Copies blocks and nodes from the original method graph
325  * to the copied graph.
326  *
327  * @param n    A node from the original method graph.
328  * @param env  The copied graph.
329  */
330 static void copy_all_nodes(ir_node *node, void *env)
331 {
332         ir_graph *irg      = (ir_graph*)env;
333         ir_node  *new_node = irn_copy_into_irg(node, irg);
334
335         set_irn_link(node, new_node);
336
337         /* fix access to entities on the stack frame */
338         if (is_Sel(new_node)) {
339                 ir_entity *ent = get_Sel_entity(new_node);
340                 ir_type   *tp  = get_entity_owner(ent);
341
342                 if (is_frame_type(tp)) {
343                         /* replace by the copied entity */
344                         ent = (ir_entity*)get_entity_link(ent);
345
346                         assert(is_entity(ent));
347                         assert(get_entity_owner(ent) == get_irg_frame_type(irg));
348                         set_Sel_entity(new_node, ent);
349                 }
350         }
351 }
352
353 /**
354  * Post-walker: Set the predecessors of the copied nodes.
355  * The copied nodes are set as link of their original nodes. The links of
356  * "irn" predecessors are the predecessors of copied node.
357  */
358 static void rewire(ir_node *irn, void *env)
359 {
360         (void) env;
361         irn_rewire_inputs(irn);
362 }
363
364 static ir_node *get_new_node(const ir_node *old_node)
365 {
366         return (ir_node*) get_irn_link(old_node);
367 }
368
369 /*
370  * Create a new graph that is a copy of a given one.
371  */
372 ir_graph *create_irg_copy(ir_graph *irg)
373 {
374         ir_graph *res;
375
376         res = alloc_graph();
377
378         res->n_loc = 0;
379         res->visited = 0;       /* visited flag, for the ir walker */
380         res->block_visited = 0; /* visited flag, for the 'block'-walker */
381         res->obst       = XMALLOC(struct obstack);
382         obstack_init(res->obst);
383         res->extbb_obst = NULL;
384
385         res->last_node_idx = 0;
386
387         res->phase_state      = irg->phase_state;
388         res->irg_pinned_state = irg->irg_pinned_state;
389         res->extblk_state     = ir_extblk_info_none;
390         res->fp_model         = irg->fp_model;
391
392         new_identities(res);
393
394         /* clone the frame type here for safety */
395         irp_reserve_resources(irp, IRP_RESOURCE_ENTITY_LINK);
396         res->frame_type  = clone_frame_type(irg->frame_type);
397
398         res->phase_state = irg->phase_state;
399
400         ir_reserve_resources(irg, IR_RESOURCE_IRN_LINK);
401
402         /* copy all nodes from the graph irg to the new graph res */
403         irg_walk_anchors(irg, copy_all_nodes, rewire, res);
404
405         /* copy the Anchor node */
406         res->anchor = get_new_node(irg->anchor);
407
408         /* -- The end block -- */
409         set_irg_end_block (res, get_new_node(get_irg_end_block(irg)));
410         set_irg_end       (res, get_new_node(get_irg_end(irg)));
411
412         /* -- The start block -- */
413         set_irg_start_block(res, get_new_node(get_irg_start_block(irg)));
414         set_irg_no_mem     (res, get_new_node(get_irg_no_mem(irg)));
415         set_irg_start      (res, get_new_node(get_irg_start(irg)));
416
417         /* Proj results of start node */
418         set_irg_initial_mem(res, get_new_node(get_irg_initial_mem(irg)));
419
420         /* Copy the node count estimation. Would be strange if this
421            is different from the original one. */
422         res->estimated_node_count = irg->estimated_node_count;
423
424         ir_free_resources(irg, IR_RESOURCE_IRN_LINK);
425         irp_free_resources(irp, IRP_RESOURCE_ENTITY_LINK);
426
427         return res;
428 }
429
430 /* Frees the passed irgraph.
431    Deallocates all nodes in this graph and the ir_graph structure.
432    Sets the field irgraph in the corresponding entity to NULL.
433    Does not remove the irgraph from the list in irprog (requires
434    inefficient search, call remove_irp_irg by hand).
435    Does not free types, entities or modes that are used only by this
436    graph, nor the entity standing for this graph. */
437 void free_ir_graph(ir_graph *irg)
438 {
439         assert(is_ir_graph(irg));
440
441         edges_deactivate(irg);
442
443         hook_free_graph(irg);
444         free_irg_outs(irg);
445         if (irg->frame_type)
446                 free_type(irg->frame_type);
447         del_identities(irg);
448         if (irg->ent) {
449                 set_entity_irg(irg->ent, NULL);  /* not set in const code irg */
450         }
451
452         free_End(get_irg_end(irg));
453         obstack_free(irg->obst,NULL);
454         free(irg->obst);
455         if (irg->loc_descriptions)
456                 free(irg->loc_descriptions);
457         irg->kind = k_BAD;
458         free_graph(irg);
459 }
460
461 /* access routines for all ir_graph attributes:
462    templates:
463    {attr type} get_irg_{attribute name} (ir_graph *irg);
464    void set_irg_{attr name} (ir_graph *irg, {attr type} {attr}); */
465
466 int (is_ir_graph)(const void *thing)
467 {
468         return _is_ir_graph(thing);
469 }
470
471 #ifdef DEBUG_libfirm
472 /* Outputs a unique number for this node */
473 long get_irg_graph_nr(const ir_graph *irg)
474 {
475         return irg->graph_nr;
476 }
477 #else
478 long get_irg_graph_nr(const ir_graph *irg)
479 {
480         return PTR_TO_INT(irg);
481 }
482 #endif
483
484 size_t get_irg_idx(const ir_graph *irg)
485 {
486         return irg->index;
487 }
488
489 ir_node *(get_idx_irn)(const ir_graph *irg, unsigned idx)
490 {
491         return _get_idx_irn(irg, idx);
492 }
493
494 ir_node *(get_irg_start_block)(const ir_graph *irg)
495 {
496         return _get_irg_start_block(irg);
497 }
498
499 void (set_irg_start_block)(ir_graph *irg, ir_node *node)
500 {
501         _set_irg_start_block(irg, node);
502 }
503
504 ir_node *(get_irg_start)(const ir_graph *irg)
505 {
506         return _get_irg_start(irg);
507 }
508
509 void (set_irg_start)(ir_graph *irg, ir_node *node)
510 {
511         _set_irg_start(irg, node);
512 }
513
514 ir_node *(get_irg_end_block)(const ir_graph *irg)
515 {
516         return _get_irg_end_block(irg);
517 }
518
519 void (set_irg_end_block)(ir_graph *irg, ir_node *node)
520 {
521   _set_irg_end_block(irg, node);
522 }
523
524 ir_node *(get_irg_end)(const ir_graph *irg)
525 {
526         return _get_irg_end(irg);
527 }
528
529 void (set_irg_end)(ir_graph *irg, ir_node *node)
530 {
531         _set_irg_end(irg, node);
532 }
533
534 ir_node *(get_irg_initial_exec)(const ir_graph *irg)
535 {
536         return _get_irg_initial_exec(irg);
537 }
538
539 void (set_irg_initial_exec)(ir_graph *irg, ir_node *node)
540 {
541         _set_irg_initial_exec(irg, node);
542 }
543
544 ir_node *(get_irg_frame)(const ir_graph *irg)
545 {
546         return _get_irg_frame(irg);
547 }
548
549 void (set_irg_frame)(ir_graph *irg, ir_node *node)
550 {
551         _set_irg_frame(irg, node);
552 }
553
554 ir_node *(get_irg_initial_mem)(const ir_graph *irg)
555 {
556         return _get_irg_initial_mem(irg);
557 }
558
559 void (set_irg_initial_mem)(ir_graph *irg, ir_node *node)
560 {
561         _set_irg_initial_mem(irg, node);
562 }
563
564 ir_node *(get_irg_args)(const ir_graph *irg)
565 {
566         return _get_irg_args(irg);
567 }
568
569 void (set_irg_args)(ir_graph *irg, ir_node *node)
570 {
571         _set_irg_args(irg, node);
572 }
573
574 ir_node *(get_irg_no_mem)(const ir_graph *irg)
575 {
576         return _get_irg_no_mem(irg);
577 }
578
579 void (set_irg_no_mem)(ir_graph *irg, ir_node *node)
580 {
581         _set_irg_no_mem(irg, node);
582 }
583
584 ir_entity *(get_irg_entity)(const ir_graph *irg)
585 {
586         return _get_irg_entity(irg);
587 }
588
589 void (set_irg_entity)(ir_graph *irg, ir_entity *ent)
590 {
591         _set_irg_entity(irg, ent);
592 }
593
594 ir_type *(get_irg_frame_type)(ir_graph *irg)
595 {
596         return _get_irg_frame_type(irg);
597 }
598
599 void (set_irg_frame_type)(ir_graph *irg, ir_type *ftp)
600 {
601         _set_irg_frame_type(irg, ftp);
602 }
603
604 int get_irg_n_locs(ir_graph *irg)
605 {
606         return irg->n_loc - 1;
607 }
608
609 /* Returns the obstack associated with the graph. */
610 struct obstack *
611 (get_irg_obstack)(const ir_graph *irg) {
612         return _get_irg_obstack(irg);
613 }
614
615 /*
616  * Returns true if the node n is allocated on the storage of graph irg.
617  *
618  * Implementation is GLIBC specific as is uses the internal _obstack_chunk implementation.
619  */
620 int node_is_in_irgs_storage(const ir_graph *irg, const ir_node *n)
621 {
622         struct _obstack_chunk *p;
623
624         /*
625          * checks weather the ir_node pointer is on the obstack.
626          * A more sophisticated check would test the "whole" ir_node
627          */
628         for (p = irg->obst->chunk; p; p = p->prev) {
629                 if (((char *)p->contents <= (char *)n) && ((char *)n < (char *)p->limit))
630                         return 1;
631         }
632
633         return 0;
634 }
635
636 irg_phase_state (get_irg_phase_state)(const ir_graph *irg)
637 {
638         return _get_irg_phase_state(irg);
639 }
640
641 void (set_irg_phase_state)(ir_graph *irg, irg_phase_state state)
642 {
643         _set_irg_phase_state(irg, state);
644 }
645
646 op_pin_state (get_irg_pinned)(const ir_graph *irg)
647 {
648         return _get_irg_pinned(irg);
649 }
650
651 irg_extblk_info_state (get_irg_extblk_state)(const ir_graph *irg)
652 {
653         return _get_irg_extblk_state(irg);
654 }
655
656 void (set_irg_extblk_inconsistent)(ir_graph *irg)
657 {
658         _set_irg_extblk_inconsistent(irg);
659 }
660
661 irg_loopinfo_state (get_irg_loopinfo_state)(const ir_graph *irg)
662 {
663         return _get_irg_loopinfo_state(irg);
664 }
665
666 void (set_irg_loopinfo_state)(ir_graph *irg, irg_loopinfo_state s)
667 {
668         _set_irg_loopinfo_state(irg, s);
669 }
670
671 void (set_irg_loopinfo_inconsistent)(ir_graph *irg)
672 {
673         _set_irg_loopinfo_inconsistent(irg);
674 }
675
676 void set_irp_loopinfo_inconsistent(void)
677 {
678         size_t i, n;
679         for (i = 0, n = get_irp_n_irgs(); i < n; ++i) {
680                 set_irg_loopinfo_inconsistent(get_irp_irg(i));
681         }
682 }
683
684
685
686 void (set_irg_pinned)(ir_graph *irg, op_pin_state p)
687 {
688         _set_irg_pinned(irg, p);
689 }
690
691 irg_callee_info_state (get_irg_callee_info_state)(const ir_graph *irg)
692 {
693         return _get_irg_callee_info_state(irg);
694 }
695
696 void (set_irg_callee_info_state)(ir_graph *irg, irg_callee_info_state s)
697 {
698         _set_irg_callee_info_state(irg, s);
699 }
700
701 irg_inline_property (get_irg_inline_property)(const ir_graph *irg)
702 {
703         return _get_irg_inline_property(irg);
704 }
705
706 void (set_irg_inline_property)(ir_graph *irg, irg_inline_property s)
707 {
708         _set_irg_inline_property(irg, s);
709 }
710
711 mtp_additional_properties (get_irg_additional_properties)(const ir_graph *irg)
712 {
713         return _get_irg_additional_properties(irg);
714 }
715
716 void (set_irg_additional_properties)(ir_graph *irg, mtp_additional_properties property_mask)
717 {
718         _set_irg_additional_properties(irg, property_mask);
719 }
720
721 void (add_irg_additional_properties)(ir_graph *irg, mtp_additional_properties flag)
722 {
723         _add_irg_additional_properties(irg, flag);
724 }
725
726 void (set_irg_link)(ir_graph *irg, void *thing)
727 {
728         _set_irg_link(irg, thing);
729 }
730
731 void *(get_irg_link)(const ir_graph *irg)
732 {
733         return _get_irg_link(irg);
734 }
735
736 ir_visited_t (get_irg_visited)(const ir_graph *irg)
737 {
738         return _get_irg_visited(irg);
739 }
740
741 /** maximum visited flag content of all ir_graph visited fields. */
742 static ir_visited_t max_irg_visited = 0;
743
744 void set_irg_visited(ir_graph *irg, ir_visited_t visited)
745 {
746         irg->visited = visited;
747         if (irg->visited > max_irg_visited) {
748                 max_irg_visited = irg->visited;
749         }
750 }
751
752 void inc_irg_visited(ir_graph *irg)
753 {
754         ++irg->visited;
755         if (irg->visited > max_irg_visited) {
756                 max_irg_visited = irg->visited;
757         }
758 }
759
760 ir_visited_t get_max_irg_visited(void)
761 {
762         return max_irg_visited;
763 }
764
765 void set_max_irg_visited(int val)
766 {
767         max_irg_visited = val;
768 }
769
770 ir_visited_t inc_max_irg_visited(void)
771 {
772 #ifndef NDEBUG
773         size_t i;
774         for (i = 0; i < get_irp_n_irgs(); i++)
775                 assert(max_irg_visited >= get_irg_visited(get_irp_irg(i)));
776 #endif
777         return ++max_irg_visited;
778 }
779
780 ir_visited_t (get_irg_block_visited)(const ir_graph *irg)
781 {
782         return _get_irg_block_visited(irg);
783 }
784
785 void (set_irg_block_visited)(ir_graph *irg, ir_visited_t visited)
786 {
787         _set_irg_block_visited(irg, visited);
788 }
789
790 void (inc_irg_block_visited)(ir_graph *irg)
791 {
792   _inc_irg_block_visited(irg);
793 }
794
795 /* Return the floating point model of this graph. */
796 unsigned (get_irg_fp_model)(const ir_graph *irg)
797 {
798         return _get_irg_fp_model(irg);
799 }
800
801 /* Sets the floating point model for this graph. */
802 void set_irg_fp_model(ir_graph *irg, unsigned model)
803 {
804         irg->fp_model = model;
805 }
806
807 /* set a description for local value n */
808 void set_irg_loc_description(ir_graph *irg, int n, void *description)
809 {
810         assert(0 <= n && n < irg->n_loc);
811
812         if (! irg->loc_descriptions)
813                 irg->loc_descriptions = XMALLOCNZ(void*, irg->n_loc);
814
815         irg->loc_descriptions[n] = description;
816 }
817
818 /* get the description for local value n */
819 void *get_irg_loc_description(ir_graph *irg, int n)
820 {
821         assert(0 <= n && n < irg->n_loc);
822         return irg->loc_descriptions ? irg->loc_descriptions[n] : NULL;
823 }
824
825 void irg_register_phase(ir_graph *irg, ir_phase_id id, ir_phase *phase)
826 {
827         assert(id <= PHASE_LAST);
828         assert(irg->phases[id] == NULL);
829         irg->phases[id] = phase;
830 }
831
832 void irg_invalidate_phases(ir_graph *irg)
833 {
834         int p;
835         for (p = 0; p <= PHASE_LAST; ++p) {
836                 ir_phase *phase = irg->phases[p];
837                 if (phase == NULL)
838                         continue;
839
840                 phase_free(phase);
841                 irg->phases[p] = NULL;
842         }
843 }
844
845 #ifndef NDEBUG
846 void ir_reserve_resources(ir_graph *irg, ir_resources_t resources)
847 {
848         assert((irg->reserved_resources & resources) == 0);
849         irg->reserved_resources |= resources;
850 }
851
852 void ir_free_resources(ir_graph *irg, ir_resources_t resources)
853 {
854         assert((irg->reserved_resources & resources) == resources);
855         irg->reserved_resources &= ~resources;
856 }
857
858 ir_resources_t ir_resources_reserved(const ir_graph *irg)
859 {
860         return irg->reserved_resources;
861 }
862 #endif /* NDEBUG */
863
864 /* Returns a estimated node count of the irg. */
865 unsigned (get_irg_estimated_node_cnt)(const ir_graph *irg)
866 {
867         return _get_irg_estimated_node_cnt(irg);
868 }
869
870 /* Returns the last irn index for this graph. */
871 unsigned get_irg_last_idx(const ir_graph *irg)
872 {
873         return irg->last_node_idx;
874 }
875
876 /* register additional space in an IR graph */
877 size_t register_additional_graph_data(size_t size)
878 {
879         assert(!forbid_new_data && "Too late to register additional node data");
880
881         if (forbid_new_data)
882                 return 0;
883
884         return additional_graph_data_size += size;
885 }
886
887 void (set_irg_state)(ir_graph *irg, ir_graph_state_t state)
888 {
889         _set_irg_state(irg, state);
890 }
891
892 void (clear_irg_state)(ir_graph *irg, ir_graph_state_t state)
893 {
894         _clear_irg_state(irg, state);
895 }
896
897 int (is_irg_state)(const ir_graph *irg, ir_graph_state_t state)
898 {
899         return _is_irg_state(irg, state);
900 }