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