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