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 */
25 #include "config.h"
26
27 #include <string.h>
28 #include <stddef.h>
29
30 #include "xmalloc.h"
31 #include "ircons_t.h"
32 #include "irgraph_t.h"
33 #include "irprog_t.h"
34 #include "irgraph_t.h"
35 #include "irnode_t.h"
36 #include "iropt_t.h"
37 #include "irflag_t.h"
38 #include "array.h"
39 #include "irgmod.h"
40 #include "irouts.h"
41 #include "irhooks.h"
42 #include "irtools.h"
43 #include "util.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 "iroptimize.h"
50 #include "irgopt.h"
51
52 #define INITIAL_IDX_IRN_MAP_SIZE 1024
53
54 /**
55 * Indicates, whether additional data can be registered to graphs.
56 * If set to 1, this is not possible anymore.
57 */
58 static int forbid_new_data = 0;
59
60 /**
61 * The amount of additional space for custom data to be allocated upon
62 * creating a new graph.
63 */
64 static size_t additional_graph_data_size = 0;
65
66 ir_graph *current_ir_graph;
get_current_ir_graph(void)67 ir_graph *get_current_ir_graph(void)
68 {
69 return current_ir_graph;
70 }
71
set_current_ir_graph(ir_graph * graph)72 void set_current_ir_graph(ir_graph *graph)
73 {
74 current_ir_graph = graph;
75 }
76
77 /** contains the suffix for frame type names */
78 static ident *frame_type_suffix = NULL;
79
firm_init_irgraph(void)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 */
alloc_graph(void)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 */
free_graph(ir_graph * irg)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
irg_set_nloc(ir_graph * res,int n_loc)123 void irg_set_nloc(ir_graph *res, int n_loc)
124 {
125 assert(irg_is_constrained(res, IR_GRAPH_CONSTRAINT_CONSTRUCTION));
126
127 res->n_loc = n_loc + 1; /* number of local variables that are never
128 dereferenced in this graph plus one for
129 the store. This is not the number of
130 parameters to the procedure! */
131
132 if (res->loc_descriptions) {
133 xfree(res->loc_descriptions);
134 res->loc_descriptions = NULL;
135 }
136 }
137
new_r_ir_graph(ir_entity * ent,int n_loc)138 ir_graph *new_r_ir_graph(ir_entity *ent, int n_loc)
139 {
140 ir_graph *res;
141 ir_node *first_block;
142 ir_node *start, *start_block, *initial_mem, *projX;
143
144 res = alloc_graph();
145
146 /* inform statistics here, as blocks will be already build on this graph */
147 hook_new_graph(res, ent);
148
149 /*-- initialized for each graph. --*/
150 res->kind = k_ir_graph;
151 res->obst = XMALLOC(struct obstack);
152 obstack_init(res->obst);
153
154 /* graphs are in construction mode by default */
155 add_irg_constraints(res, IR_GRAPH_CONSTRAINT_CONSTRUCTION);
156 irg_set_nloc(res, n_loc);
157
158 /* descriptions will be allocated on demand */
159 res->loc_descriptions = NULL;
160
161 res->visited = 0; /* visited flag, for the ir walker */
162 res->block_visited = 0; /* visited flag, for the 'block'-walker */
163
164 res->last_node_idx = 0;
165
166 new_identities(res);
167
168 res->irg_pinned_state = op_pin_state_pinned;
169 res->typeinfo_state = ir_typeinfo_none;
170 set_irp_typeinfo_inconsistent(); /* there is a new graph with typeinfo_none. */
171 res->callee_info_state = irg_callee_info_none;
172 res->class_cast_state = ir_class_casts_transitive;
173 res->fp_model = fp_model_precise;
174 res->mem_disambig_opt = aa_opt_inherited;
175
176 /*-- Type information for the procedure of the graph --*/
177 res->ent = ent;
178 set_entity_irg(ent, res);
179
180 /*-- a class type so that it can contain "inner" methods as in Pascal. --*/
181 res->frame_type = new_type_frame();
182
183 /* the Anchor node must be created first */
184 res->anchor = new_r_Anchor(res);
185
186 /*-- Nodes needed in every graph --*/
187 set_irg_end_block(res, new_r_immBlock(res));
188 set_irg_end(res, new_r_End(res, 0, NULL));
189
190 start_block = new_r_Block_noopt(res, 0, NULL);
191 set_irg_start_block(res, start_block);
192 set_irg_no_mem (res, new_r_NoMem(res));
193 start = new_r_Start(res);
194 set_irg_start (res, start);
195
196 /* Proj results of start node */
197 projX = new_r_Proj(start, mode_X, pn_Start_X_initial_exec);
198 set_irg_initial_exec (res, projX);
199 set_irg_frame (res, new_r_Proj(start, mode_P_data, pn_Start_P_frame_base));
200 set_irg_args (res, new_r_Proj(start, mode_T, pn_Start_T_args));
201 initial_mem = new_r_Proj(start, mode_M, pn_Start_M);
202 set_irg_initial_mem(res, initial_mem);
203
204 res->index = get_irp_new_irg_idx();
205 #ifdef DEBUG_libfirm
206 res->graph_nr = get_irp_new_node_nr();
207 #endif
208
209 set_r_cur_block(res, start_block);
210 set_r_store(res, initial_mem);
211
212 /*-- Make a block to start with --*/
213 first_block = new_r_Block(res, 1, &projX);
214 set_r_cur_block(res, first_block);
215
216 res->method_execution_frequency = -1.0;
217 res->estimated_node_count = 0;
218
219 return res;
220 }
221
new_ir_graph(ir_entity * ent,int n_loc)222 ir_graph *new_ir_graph(ir_entity *ent, int n_loc)
223 {
224 ir_graph *res = new_r_ir_graph(ent, n_loc);
225 add_irp_irg(res); /* remember this graph global. */
226 return res;
227 }
228
new_const_code_irg(void)229 ir_graph *new_const_code_irg(void)
230 {
231 ir_graph *res = alloc_graph();
232 ir_node *body_block;
233 ir_node *end;
234 ir_node *end_block;
235 ir_node *no_mem;
236 ir_node *projX;
237 ir_node *start_block;
238 ir_node *start;
239
240 /* inform statistics here, as blocks will be already build on this graph */
241 hook_new_graph(res, NULL);
242
243 res->n_loc = 1; /* Only the memory. */
244 res->visited = 0; /* visited flag, for the ir walker */
245 res->block_visited = 0; /* visited flag, for the 'block'-walker */
246 res->obst = XMALLOC(struct obstack);
247 obstack_init(res->obst);
248
249 res->last_node_idx = 0;
250
251 res->irg_pinned_state = op_pin_state_pinned;
252 res->fp_model = fp_model_precise;
253
254 /* value table for global value numbering for optimizing use in iropt.c */
255 new_identities(res);
256 res->ent = NULL;
257 res->frame_type = NULL;
258
259 add_irg_constraints(res, IR_GRAPH_CONSTRAINT_CONSTRUCTION);
260
261 /* the Anchor node must be created first */
262 res->anchor = new_r_Anchor(res);
263
264 /* -- The end block -- */
265 end_block = new_r_Block_noopt(res, 0, NULL);
266 set_irg_end_block(res, end_block);
267 end = new_r_End(res, 0, NULL);
268 set_irg_end(res, end);
269
270 /* -- The start block -- */
271 start_block = new_r_Block_noopt(res, 0, NULL);
272 set_irg_start_block(res, start_block);
273 no_mem = new_r_NoMem(res);
274 set_irg_no_mem(res, no_mem);
275 start = new_r_Start(res);
276 set_irg_start(res, start);
277
278 /* Proj results of start node */
279 set_irg_initial_mem(res, new_r_Proj(start, mode_M, pn_Start_M));
280 projX = new_r_Proj(start, mode_X, pn_Start_X_initial_exec);
281
282 body_block = new_r_Block(res, 1, &projX);
283
284 set_r_cur_block(res, body_block);
285
286 /* Set the visited flag high enough that the blocks will never be visited. */
287 set_irn_visited(body_block, -1);
288 set_Block_block_visited(body_block, -1);
289 set_Block_block_visited(start_block, -1);
290 set_irn_visited(start_block, -1);
291
292 return res;
293 }
294
295 /**
296 * Pre-Walker: Copies blocks and nodes from the original method graph
297 * to the copied graph.
298 *
299 * @param n A node from the original method graph.
300 * @param env The copied graph.
301 */
copy_all_nodes(ir_node * node,void * env)302 static void copy_all_nodes(ir_node *node, void *env)
303 {
304 ir_graph *irg = (ir_graph*)env;
305 ir_node *new_node = irn_copy_into_irg(node, irg);
306
307 set_irn_link(node, new_node);
308
309 /* fix access to entities on the stack frame */
310 if (is_Sel(new_node)) {
311 ir_entity *ent = get_Sel_entity(new_node);
312 ir_type *tp = get_entity_owner(ent);
313
314 if (is_frame_type(tp)) {
315 /* replace by the copied entity */
316 ent = (ir_entity*)get_entity_link(ent);
317
318 assert(is_entity(ent));
319 assert(get_entity_owner(ent) == get_irg_frame_type(irg));
320 set_Sel_entity(new_node, ent);
321 }
322 }
323 }
324
325 /**
326 * Post-walker: Set the predecessors of the copied nodes.
327 * The copied nodes are set as link of their original nodes. The links of
328 * "irn" predecessors are the predecessors of copied node.
329 */
rewire(ir_node * irn,void * env)330 static void rewire(ir_node *irn, void *env)
331 {
332 (void) env;
333 irn_rewire_inputs(irn);
334 }
335
get_new_node(const ir_node * old_node)336 static ir_node *get_new_node(const ir_node *old_node)
337 {
338 return (ir_node*) get_irn_link(old_node);
339 }
340
create_irg_copy(ir_graph * irg)341 ir_graph *create_irg_copy(ir_graph *irg)
342 {
343 ir_graph *res;
344
345 res = alloc_graph();
346
347 res->n_loc = 0;
348 res->visited = 0; /* visited flag, for the ir walker */
349 res->block_visited = 0; /* visited flag, for the 'block'-walker */
350 res->obst = XMALLOC(struct obstack);
351 obstack_init(res->obst);
352
353 res->last_node_idx = 0;
354
355 res->irg_pinned_state = irg->irg_pinned_state;
356 res->fp_model = irg->fp_model;
357
358 new_identities(res);
359
360 /* clone the frame type here for safety */
361 irp_reserve_resources(irp, IRP_RESOURCE_ENTITY_LINK);
362 res->frame_type = clone_frame_type(irg->frame_type);
363
364 ir_reserve_resources(irg, IR_RESOURCE_IRN_LINK);
365
366 /* copy all nodes from the graph irg to the new graph res */
367 irg_walk_anchors(irg, copy_all_nodes, rewire, res);
368
369 /* copy the Anchor node */
370 res->anchor = get_new_node(irg->anchor);
371
372 /* -- The end block -- */
373 set_irg_end_block (res, get_new_node(get_irg_end_block(irg)));
374 set_irg_end (res, get_new_node(get_irg_end(irg)));
375
376 /* -- The start block -- */
377 set_irg_start_block(res, get_new_node(get_irg_start_block(irg)));
378 set_irg_no_mem (res, get_new_node(get_irg_no_mem(irg)));
379 set_irg_start (res, get_new_node(get_irg_start(irg)));
380
381 /* Proj results of start node */
382 set_irg_initial_mem(res, get_new_node(get_irg_initial_mem(irg)));
383
384 /* Copy the node count estimation. Would be strange if this
385 is different from the original one. */
386 res->estimated_node_count = irg->estimated_node_count;
387
388 ir_free_resources(irg, IR_RESOURCE_IRN_LINK);
389 irp_free_resources(irp, IRP_RESOURCE_ENTITY_LINK);
390
391 return res;
392 }
393
free_ir_graph(ir_graph * irg)394 void free_ir_graph(ir_graph *irg)
395 {
396 assert(is_ir_graph(irg));
397
398 remove_irp_irg(irg);
399 confirm_irg_properties(irg, IR_GRAPH_PROPERTIES_NONE);
400
401 hook_free_graph(irg);
402 free_irg_outs(irg);
403 del_identities(irg);
404 if (irg->ent) {
405 set_entity_irg(irg->ent, NULL); /* not set in const code irg */
406 }
407
408 free_End(get_irg_end(irg));
409 obstack_free(irg->obst, NULL);
410 free(irg->obst);
411 if (irg->loc_descriptions)
412 free(irg->loc_descriptions);
413 irg->kind = k_BAD;
414 free_graph(irg);
415 }
416
417 int (is_ir_graph)(const void *thing)
418 {
419 return is_ir_graph_(thing);
420 }
421
422 #ifdef DEBUG_libfirm
get_irg_graph_nr(const ir_graph * irg)423 long get_irg_graph_nr(const ir_graph *irg)
424 {
425 return irg->graph_nr;
426 }
427 #else
get_irg_graph_nr(const ir_graph * irg)428 long get_irg_graph_nr(const ir_graph *irg)
429 {
430 return PTR_TO_INT(irg);
431 }
432 #endif
433
get_irg_idx(const ir_graph * irg)434 size_t get_irg_idx(const ir_graph *irg)
435 {
436 return irg->index;
437 }
438
439 ir_node *(get_idx_irn)(const ir_graph *irg, unsigned idx)
440 {
441 return get_idx_irn_(irg, idx);
442 }
443
444 ir_node *(get_irg_start_block)(const ir_graph *irg)
445 {
446 return get_irg_start_block_(irg);
447 }
448
449 void (set_irg_start_block)(ir_graph *irg, ir_node *node)
450 {
451 set_irg_start_block_(irg, node);
452 }
453
454 ir_node *(get_irg_start)(const ir_graph *irg)
455 {
456 return get_irg_start_(irg);
457 }
458
459 void (set_irg_start)(ir_graph *irg, ir_node *node)
460 {
461 set_irg_start_(irg, node);
462 }
463
464 ir_node *(get_irg_end_block)(const ir_graph *irg)
465 {
466 return get_irg_end_block_(irg);
467 }
468
469 void (set_irg_end_block)(ir_graph *irg, ir_node *node)
470 {
471 set_irg_end_block_(irg, node);
472 }
473
474 ir_node *(get_irg_end)(const ir_graph *irg)
475 {
476 return get_irg_end_(irg);
477 }
478
479 void (set_irg_end)(ir_graph *irg, ir_node *node)
480 {
481 set_irg_end_(irg, node);
482 }
483
484 ir_node *(get_irg_initial_exec)(const ir_graph *irg)
485 {
486 return get_irg_initial_exec_(irg);
487 }
488
489 void (set_irg_initial_exec)(ir_graph *irg, ir_node *node)
490 {
491 set_irg_initial_exec_(irg, node);
492 }
493
494 ir_node *(get_irg_frame)(const ir_graph *irg)
495 {
496 return get_irg_frame_(irg);
497 }
498
499 void (set_irg_frame)(ir_graph *irg, ir_node *node)
500 {
501 set_irg_frame_(irg, node);
502 }
503
504 ir_node *(get_irg_initial_mem)(const ir_graph *irg)
505 {
506 return get_irg_initial_mem_(irg);
507 }
508
509 void (set_irg_initial_mem)(ir_graph *irg, ir_node *node)
510 {
511 set_irg_initial_mem_(irg, node);
512 }
513
514 ir_node *(get_irg_args)(const ir_graph *irg)
515 {
516 return get_irg_args_(irg);
517 }
518
519 void (set_irg_args)(ir_graph *irg, ir_node *node)
520 {
521 set_irg_args_(irg, node);
522 }
523
524 ir_node *(get_irg_no_mem)(const ir_graph *irg)
525 {
526 return get_irg_no_mem_(irg);
527 }
528
529 void (set_irg_no_mem)(ir_graph *irg, ir_node *node)
530 {
531 set_irg_no_mem_(irg, node);
532 }
533
534 ir_entity *(get_irg_entity)(const ir_graph *irg)
535 {
536 return get_irg_entity_(irg);
537 }
538
539 void (set_irg_entity)(ir_graph *irg, ir_entity *ent)
540 {
541 set_irg_entity_(irg, ent);
542 }
543
544 ir_type *(get_irg_frame_type)(ir_graph *irg)
545 {
546 return get_irg_frame_type_(irg);
547 }
548
549 void (set_irg_frame_type)(ir_graph *irg, ir_type *ftp)
550 {
551 set_irg_frame_type_(irg, ftp);
552 }
553
get_irg_n_locs(ir_graph * irg)554 int get_irg_n_locs(ir_graph *irg)
555 {
556 return irg->n_loc - 1;
557 }
558
559 struct obstack *(get_irg_obstack)(const ir_graph *irg)
560 {
561 return get_irg_obstack_(irg);
562 }
563
node_is_in_irgs_storage(const ir_graph * irg,const ir_node * n)564 int node_is_in_irgs_storage(const ir_graph *irg, const ir_node *n)
565 {
566 struct _obstack_chunk *p;
567
568 /*
569 * checks weather the ir_node pointer is on the obstack.
570 * A more sophisticated check would test the "whole" ir_node
571 */
572 for (p = irg->obst->chunk; p; p = p->prev) {
573 if (((char *)p->contents <= (char *)n) && ((char *)n < (char *)p->limit))
574 return 1;
575 }
576
577 return 0;
578 }
579
op_pin_state(get_irg_pinned)580 op_pin_state (get_irg_pinned)(const ir_graph *irg)
581 {
582 return get_irg_pinned_(irg);
583 }
584
585 void (set_irg_pinned)(ir_graph *irg, op_pin_state p)
586 {
587 set_irg_pinned_(irg, p);
588 }
589
irg_callee_info_state(get_irg_callee_info_state)590 irg_callee_info_state (get_irg_callee_info_state)(const ir_graph *irg)
591 {
592 return get_irg_callee_info_state_(irg);
593 }
594
595 void (set_irg_callee_info_state)(ir_graph *irg, irg_callee_info_state s)
596 {
597 set_irg_callee_info_state_(irg, s);
598 }
599
600 void (set_irg_link)(ir_graph *irg, void *thing)
601 {
602 set_irg_link_(irg, thing);
603 }
604
605 void *(get_irg_link)(const ir_graph *irg)
606 {
607 return get_irg_link_(irg);
608 }
609
ir_visited_t(get_irg_visited)610 ir_visited_t (get_irg_visited)(const ir_graph *irg)
611 {
612 return get_irg_visited_(irg);
613 }
614
615 /** maximum visited flag content of all ir_graph visited fields. */
616 static ir_visited_t max_irg_visited = 0;
617
set_irg_visited(ir_graph * irg,ir_visited_t visited)618 void set_irg_visited(ir_graph *irg, ir_visited_t visited)
619 {
620 irg->visited = visited;
621 if (irg->visited > max_irg_visited) {
622 max_irg_visited = irg->visited;
623 }
624 }
625
inc_irg_visited(ir_graph * irg)626 void inc_irg_visited(ir_graph *irg)
627 {
628 ++irg->visited;
629 if (irg->visited > max_irg_visited) {
630 max_irg_visited = irg->visited;
631 }
632 }
633
get_max_irg_visited(void)634 ir_visited_t get_max_irg_visited(void)
635 {
636 return max_irg_visited;
637 }
638
set_max_irg_visited(int val)639 void set_max_irg_visited(int val)
640 {
641 max_irg_visited = val;
642 }
643
inc_max_irg_visited(void)644 ir_visited_t inc_max_irg_visited(void)
645 {
646 #ifndef NDEBUG
647 size_t i;
648 for (i = 0; i < get_irp_n_irgs(); i++)
649 assert(max_irg_visited >= get_irg_visited(get_irp_irg(i)));
650 #endif
651 return ++max_irg_visited;
652 }
653
ir_visited_t(get_irg_block_visited)654 ir_visited_t (get_irg_block_visited)(const ir_graph *irg)
655 {
656 return get_irg_block_visited_(irg);
657 }
658
659 void (set_irg_block_visited)(ir_graph *irg, ir_visited_t visited)
660 {
661 set_irg_block_visited_(irg, visited);
662 }
663
664 void (inc_irg_block_visited)(ir_graph *irg)
665 {
666 inc_irg_block_visited_(irg);
667 }
668
669 unsigned (get_irg_fp_model)(const ir_graph *irg)
670 {
671 return get_irg_fp_model_(irg);
672 }
673
set_irg_fp_model(ir_graph * irg,unsigned model)674 void set_irg_fp_model(ir_graph *irg, unsigned model)
675 {
676 irg->fp_model = model;
677 }
678
set_irg_loc_description(ir_graph * irg,int n,void * description)679 void set_irg_loc_description(ir_graph *irg, int n, void *description)
680 {
681 assert(0 <= n && n < irg->n_loc);
682
683 if (! irg->loc_descriptions)
684 irg->loc_descriptions = XMALLOCNZ(void*, irg->n_loc);
685
686 irg->loc_descriptions[n] = description;
687 }
688
get_irg_loc_description(ir_graph * irg,int n)689 void *get_irg_loc_description(ir_graph *irg, int n)
690 {
691 assert(0 <= n && n < irg->n_loc);
692 return irg->loc_descriptions ? irg->loc_descriptions[n] : NULL;
693 }
694
695 #ifndef NDEBUG
ir_reserve_resources(ir_graph * irg,ir_resources_t resources)696 void ir_reserve_resources(ir_graph *irg, ir_resources_t resources)
697 {
698 assert((irg->reserved_resources & resources) == 0);
699 irg->reserved_resources |= resources;
700 }
701
ir_free_resources(ir_graph * irg,ir_resources_t resources)702 void ir_free_resources(ir_graph *irg, ir_resources_t resources)
703 {
704 assert((irg->reserved_resources & resources) == resources);
705 irg->reserved_resources &= ~resources;
706 }
707
ir_resources_reserved(const ir_graph * irg)708 ir_resources_t ir_resources_reserved(const ir_graph *irg)
709 {
710 return irg->reserved_resources;
711 }
712 #endif
713
714 unsigned (get_irg_estimated_node_cnt)(const ir_graph *irg)
715 {
716 return get_irg_estimated_node_cnt_(irg);
717 }
718
get_irg_last_idx(const ir_graph * irg)719 unsigned get_irg_last_idx(const ir_graph *irg)
720 {
721 return irg->last_node_idx;
722 }
723
register_additional_graph_data(size_t size)724 size_t register_additional_graph_data(size_t size)
725 {
726 assert(!forbid_new_data && "Too late to register additional node data");
727
728 if (forbid_new_data)
729 return 0;
730
731 return additional_graph_data_size += size;
732 }
733
add_irg_constraints(ir_graph * irg,ir_graph_constraints_t constraints)734 void add_irg_constraints(ir_graph *irg, ir_graph_constraints_t constraints)
735 {
736 irg->constraints |= constraints;
737 }
738
clear_irg_constraints(ir_graph * irg,ir_graph_constraints_t constraints)739 void clear_irg_constraints(ir_graph *irg, ir_graph_constraints_t constraints)
740 {
741 irg->constraints &= ~constraints;
742 }
743
744 int (irg_is_constrained)(const ir_graph *irg, ir_graph_constraints_t constraints)
745 {
746 return irg_is_constrained_(irg, constraints);
747 }
748
749 void (add_irg_properties)(ir_graph *irg, ir_graph_properties_t props)
750 {
751 add_irg_properties_(irg, props);
752 }
753
754 void (clear_irg_properties)(ir_graph *irg, ir_graph_properties_t props)
755 {
756 clear_irg_properties_(irg, props);
757 }
758
759 int (irg_has_properties)(const ir_graph *irg, ir_graph_properties_t props)
760 {
761 return irg_has_properties_(irg, props);
762 }
763
764 typedef void (*assure_property_func)(ir_graph *irg);
765
assure_irg_properties(ir_graph * irg,ir_graph_properties_t props)766 void assure_irg_properties(ir_graph *irg, ir_graph_properties_t props)
767 {
768 static struct {
769 ir_graph_properties_t property;
770 assure_property_func func;
771 } property_functions[] = {
772 { IR_GRAPH_PROPERTY_ONE_RETURN, normalize_one_return },
773 { IR_GRAPH_PROPERTY_MANY_RETURNS, normalize_n_returns },
774 { IR_GRAPH_PROPERTY_NO_CRITICAL_EDGES, remove_critical_cf_edges },
775 { IR_GRAPH_PROPERTY_NO_UNREACHABLE_CODE, remove_unreachable_code },
776 { IR_GRAPH_PROPERTY_NO_BADS, remove_bads },
777 { IR_GRAPH_PROPERTY_NO_TUPLES, remove_tuples },
778 { IR_GRAPH_PROPERTY_CONSISTENT_DOMINANCE, assure_doms },
779 { IR_GRAPH_PROPERTY_CONSISTENT_POSTDOMINANCE, assure_postdoms },
780 { IR_GRAPH_PROPERTY_CONSISTENT_OUT_EDGES, assure_edges },
781 { IR_GRAPH_PROPERTY_CONSISTENT_OUTS, assure_irg_outs },
782 { IR_GRAPH_PROPERTY_CONSISTENT_LOOPINFO, assure_loopinfo },
783 { IR_GRAPH_PROPERTY_CONSISTENT_ENTITY_USAGE, assure_irg_entity_usage_computed },
784 { IR_GRAPH_PROPERTY_CONSISTENT_DOMINANCE_FRONTIERS, ir_compute_dominance_frontiers },
785 };
786 size_t i;
787 for (i = 0; i < ARRAY_SIZE(property_functions); ++i) {
788 ir_graph_properties_t missing = props & ~irg->properties;
789 if (missing & property_functions[i].property)
790 property_functions[i].func(irg);
791 }
792 assert((props & ~irg->properties) == IR_GRAPH_PROPERTIES_NONE);
793 }
794
confirm_irg_properties(ir_graph * irg,ir_graph_properties_t props)795 void confirm_irg_properties(ir_graph *irg, ir_graph_properties_t props)
796 {
797 clear_irg_properties(irg, ~props);
798 if (! (props & IR_GRAPH_PROPERTY_CONSISTENT_OUT_EDGES))
799 edges_deactivate(irg);
800 if (! (props & IR_GRAPH_PROPERTY_CONSISTENT_OUTS)
801 && (irg->properties & IR_GRAPH_PROPERTY_CONSISTENT_OUTS))
802 free_irg_outs(irg);
803 if (! (props & IR_GRAPH_PROPERTY_CONSISTENT_DOMINANCE_FRONTIERS))
804 ir_free_dominance_frontiers(irg);
805 }
806