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