cleanup: Remove code duplication for implementing get_irg_graph_nr().
[libfirm] / ir / ir / irgraph.c
1 /*
2  * This file is part of libFirm.
3  * Copyright (C) 2012 University of Karlsruhe.
4  */
5
6 /**
7  * @file
8  * @brief    Entry point to the representation of procedure code.
9  * @author   Martin Trapp, Christian Schaefer, Goetz Lindenmaier, Michael Beck
10  */
11 #include "config.h"
12
13 #include <string.h>
14 #include <stddef.h>
15
16 #include "xmalloc.h"
17 #include "ircons_t.h"
18 #include "irgraph_t.h"
19 #include "irprog_t.h"
20 #include "irgraph_t.h"
21 #include "irnode_t.h"
22 #include "iropt_t.h"
23 #include "irflag_t.h"
24 #include "array.h"
25 #include "irgmod.h"
26 #include "irouts.h"
27 #include "irhooks.h"
28 #include "irtools.h"
29 #include "util.h"
30 #include "irgwalk.h"
31 #include "irbackedge_t.h"
32 #include "iredges_t.h"
33 #include "type_t.h"
34 #include "irmemory.h"
35 #include "iroptimize.h"
36 #include "irgopt.h"
37
38 #define INITIAL_IDX_IRN_MAP_SIZE 1024
39 /** Suffix that is added to every frame type. */
40 #define FRAME_TP_SUFFIX "frame_tp"
41
42 ir_graph *current_ir_graph;
43 ir_graph *get_current_ir_graph(void)
44 {
45         return current_ir_graph;
46 }
47
48 void set_current_ir_graph(ir_graph *graph)
49 {
50         current_ir_graph = graph;
51 }
52
53 /** contains the suffix for frame type names */
54 static ident *frame_type_suffix = NULL;
55
56 void firm_init_irgraph(void)
57 {
58         frame_type_suffix = new_id_from_str(FRAME_TP_SUFFIX);
59 }
60
61 /**
62  * Allocate a new IR graph.
63  * This function respects the registered graph data. The only reason for
64  * this function is, that there are two locations, where graphs are
65  * allocated (new_r_ir_graph, new_const_code_irg).
66  * @return Memory for a new graph.
67  */
68 static ir_graph *alloc_graph(void)
69 {
70         ir_graph *const res = XMALLOCZ(ir_graph);
71         res->kind = k_ir_graph;
72
73         /* initialize the idx->node map. */
74         res->idx_irn_map = NEW_ARR_FZ(ir_node*, INITIAL_IDX_IRN_MAP_SIZE);
75
76         obstack_init(&res->obst);
77
78         /* value table for global value numbering for optimizing use in iropt.c */
79         new_identities(res);
80
81         return res;
82 }
83
84 /**
85  * Frees an allocated IR graph
86  */
87 static void free_graph(ir_graph *irg)
88 {
89         for (ir_edge_kind_t i = EDGE_KIND_FIRST; i < EDGE_KIND_LAST; ++i)
90                 edges_deactivate_kind(irg, i);
91         DEL_ARR_F(irg->idx_irn_map);
92         free(irg);
93 }
94
95 void irg_set_nloc(ir_graph *res, int n_loc)
96 {
97         assert(irg_is_constrained(res, IR_GRAPH_CONSTRAINT_CONSTRUCTION));
98
99         res->n_loc = n_loc + 1;     /* number of local variables that are never
100                                        dereferenced in this graph plus one for
101                                        the store. This is not the number of
102                                        parameters to the procedure!  */
103
104         if (res->loc_descriptions) {
105                 xfree(res->loc_descriptions);
106                 res->loc_descriptions = NULL;
107         }
108 }
109
110 ir_graph *new_r_ir_graph(ir_entity *ent, int n_loc)
111 {
112         ir_graph *res;
113         ir_node  *first_block;
114         ir_node  *start, *start_block, *initial_mem, *projX;
115
116         res = alloc_graph();
117
118         /* inform statistics here, as blocks will be already build on this graph */
119         hook_new_graph(res, ent);
120
121         /* graphs are in construction mode by default */
122         add_irg_constraints(res, IR_GRAPH_CONSTRAINT_CONSTRUCTION);
123         irg_set_nloc(res, n_loc);
124
125         res->irg_pinned_state    = op_pin_state_pinned;
126         res->typeinfo_state      = ir_typeinfo_none;
127         set_irp_typeinfo_inconsistent();           /* there is a new graph with typeinfo_none. */
128         res->callee_info_state   = irg_callee_info_none;
129         res->fp_model            = fp_model_precise;
130         res->mem_disambig_opt    = aa_opt_inherited;
131
132         /*-- Type information for the procedure of the graph --*/
133         res->ent = ent;
134         set_entity_irg(ent, res);
135
136         /*--  a class type so that it can contain "inner" methods as in Pascal. --*/
137         res->frame_type = new_type_frame();
138
139         /* the Anchor node must be created first */
140         res->anchor = new_r_Anchor(res);
141
142         /*-- Nodes needed in every graph --*/
143         set_irg_end_block(res, new_r_immBlock(res));
144         set_irg_end(res, new_r_End(res, 0, NULL));
145
146         start_block = new_r_Block_noopt(res, 0, NULL);
147         set_irg_start_block(res, start_block);
148         set_irg_no_mem     (res, new_r_NoMem(res));
149         start = new_r_Start(res);
150         set_irg_start      (res, start);
151
152         /* Proj results of start node */
153         projX                   = new_r_Proj(start, mode_X, pn_Start_X_initial_exec);
154         set_irg_initial_exec    (res, projX);
155         set_irg_frame           (res, new_r_Proj(start, mode_P_data, pn_Start_P_frame_base));
156         set_irg_args            (res, new_r_Proj(start, mode_T,      pn_Start_T_args));
157         initial_mem             = new_r_Proj(start, mode_M, pn_Start_M);
158         set_irg_initial_mem(res, initial_mem);
159
160         res->index       = get_irp_new_irg_idx();
161 #ifdef DEBUG_libfirm
162         res->graph_nr    = get_irp_new_node_nr();
163 #endif
164
165         set_r_cur_block(res, start_block);
166         set_r_store(res, initial_mem);
167
168         /*-- Make a block to start with --*/
169         first_block = new_r_Block(res, 1, &projX);
170         set_r_cur_block(res, first_block);
171
172         res->method_execution_frequency = -1.0;
173
174         return res;
175 }
176
177 ir_graph *new_ir_graph(ir_entity *ent, int n_loc)
178 {
179         ir_graph *res = new_r_ir_graph(ent, n_loc);
180         add_irp_irg(res);          /* remember this graph global. */
181         return res;
182 }
183
184 ir_graph *new_const_code_irg(void)
185 {
186         ir_graph *res = alloc_graph();
187         ir_node  *body_block;
188         ir_node  *end;
189         ir_node  *end_block;
190         ir_node  *no_mem;
191         ir_node  *projX;
192         ir_node  *start_block;
193         ir_node  *start;
194
195         /* inform statistics here, as blocks will be already build on this graph */
196         hook_new_graph(res, NULL);
197
198         res->n_loc            = 1; /* Only the memory. */
199         res->irg_pinned_state = op_pin_state_pinned;
200         res->fp_model         = fp_model_precise;
201
202         add_irg_constraints(res, IR_GRAPH_CONSTRAINT_CONSTRUCTION);
203
204         /* the Anchor node must be created first */
205         res->anchor = new_r_Anchor(res);
206
207         /* -- The end block -- */
208         end_block = new_r_Block_noopt(res, 0, NULL);
209         set_irg_end_block(res, end_block);
210         end = new_r_End(res, 0, NULL);
211         set_irg_end(res, end);
212
213         /* -- The start block -- */
214         start_block = new_r_Block_noopt(res, 0, NULL);
215         set_irg_start_block(res, start_block);
216         no_mem = new_r_NoMem(res);
217         set_irg_no_mem(res, no_mem);
218         start = new_r_Start(res);
219         set_irg_start(res, start);
220
221         /* Proj results of start node */
222         set_irg_initial_mem(res, new_r_Proj(start, mode_M, pn_Start_M));
223         projX = new_r_Proj(start, mode_X, pn_Start_X_initial_exec);
224
225         body_block = new_r_Block(res, 1, &projX);
226
227         set_r_cur_block(res, body_block);
228
229         /* Set the visited flag high enough that the blocks will never be visited. */
230         set_irn_visited(body_block, -1);
231         set_Block_block_visited(body_block, -1);
232         set_Block_block_visited(start_block, -1);
233         set_irn_visited(start_block, -1);
234
235         return res;
236 }
237
238 /**
239  * Pre-Walker: Copies blocks and nodes from the original method graph
240  * to the copied graph.
241  *
242  * @param n    A node from the original method graph.
243  * @param env  The copied graph.
244  */
245 static void copy_all_nodes(ir_node *node, void *env)
246 {
247         ir_graph *irg      = (ir_graph*)env;
248         ir_node  *new_node = irn_copy_into_irg(node, irg);
249
250         set_irn_link(node, new_node);
251
252         /* fix access to entities on the stack frame */
253         if (is_Sel(new_node)) {
254                 ir_entity *ent = get_Sel_entity(new_node);
255                 ir_type   *tp  = get_entity_owner(ent);
256
257                 if (is_frame_type(tp)) {
258                         /* replace by the copied entity */
259                         ent = (ir_entity*)get_entity_link(ent);
260
261                         assert(is_entity(ent));
262                         assert(get_entity_owner(ent) == get_irg_frame_type(irg));
263                         set_Sel_entity(new_node, ent);
264                 }
265         }
266 }
267
268 /**
269  * Post-walker: Set the predecessors of the copied nodes.
270  * The copied nodes are set as link of their original nodes. The links of
271  * "irn" predecessors are the predecessors of copied node.
272  */
273 static void rewire(ir_node *irn, void *env)
274 {
275         (void) env;
276         irn_rewire_inputs(irn);
277 }
278
279 static ir_node *get_new_node(const ir_node *old_node)
280 {
281         return (ir_node*) get_irn_link(old_node);
282 }
283
284 ir_graph *create_irg_copy(ir_graph *irg)
285 {
286         ir_graph *res;
287
288         res = alloc_graph();
289
290         res->irg_pinned_state = irg->irg_pinned_state;
291         res->fp_model         = irg->fp_model;
292
293         /* clone the frame type here for safety */
294         irp_reserve_resources(irp, IRP_RESOURCE_ENTITY_LINK);
295         res->frame_type  = clone_frame_type(irg->frame_type);
296
297         ir_reserve_resources(irg, IR_RESOURCE_IRN_LINK);
298
299         /* copy all nodes from the graph irg to the new graph res */
300         irg_walk_anchors(irg, copy_all_nodes, rewire, res);
301
302         /* copy the Anchor node */
303         res->anchor = get_new_node(irg->anchor);
304
305         /* -- The end block -- */
306         set_irg_end_block (res, get_new_node(get_irg_end_block(irg)));
307         set_irg_end       (res, get_new_node(get_irg_end(irg)));
308
309         /* -- The start block -- */
310         set_irg_start_block(res, get_new_node(get_irg_start_block(irg)));
311         set_irg_no_mem     (res, get_new_node(get_irg_no_mem(irg)));
312         set_irg_start      (res, get_new_node(get_irg_start(irg)));
313
314         /* Proj results of start node */
315         set_irg_initial_mem(res, get_new_node(get_irg_initial_mem(irg)));
316
317         ir_free_resources(irg, IR_RESOURCE_IRN_LINK);
318         irp_free_resources(irp, IRP_RESOURCE_ENTITY_LINK);
319
320         return res;
321 }
322
323 void free_ir_graph(ir_graph *irg)
324 {
325         assert(is_ir_graph(irg));
326
327         remove_irp_irg(irg);
328         confirm_irg_properties(irg, IR_GRAPH_PROPERTIES_NONE);
329
330         hook_free_graph(irg);
331         free_irg_outs(irg);
332         del_identities(irg);
333         if (irg->ent) {
334                 set_entity_irg(irg->ent, NULL);  /* not set in const code irg */
335         }
336
337         free_End(get_irg_end(irg));
338         obstack_free(&irg->obst, NULL);
339         if (irg->loc_descriptions)
340                 free(irg->loc_descriptions);
341         irg->kind = k_BAD;
342         free_graph(irg);
343 }
344
345 int (is_ir_graph)(const void *thing)
346 {
347         return is_ir_graph_(thing);
348 }
349
350 long get_irg_graph_nr(const ir_graph *irg)
351 {
352 #ifdef DEBUG_libfirm
353         return irg->graph_nr;
354 #else
355         return PTR_TO_INT(irg);
356 #endif
357 }
358
359 size_t get_irg_idx(const ir_graph *irg)
360 {
361         return irg->index;
362 }
363
364 ir_node *(get_idx_irn)(const ir_graph *irg, unsigned idx)
365 {
366         return get_idx_irn_(irg, idx);
367 }
368
369 ir_node *(get_irg_start_block)(const ir_graph *irg)
370 {
371         return get_irg_start_block_(irg);
372 }
373
374 void (set_irg_start_block)(ir_graph *irg, ir_node *node)
375 {
376         set_irg_start_block_(irg, node);
377 }
378
379 ir_node *(get_irg_start)(const ir_graph *irg)
380 {
381         return get_irg_start_(irg);
382 }
383
384 void (set_irg_start)(ir_graph *irg, ir_node *node)
385 {
386         set_irg_start_(irg, node);
387 }
388
389 ir_node *(get_irg_end_block)(const ir_graph *irg)
390 {
391         return get_irg_end_block_(irg);
392 }
393
394 void (set_irg_end_block)(ir_graph *irg, ir_node *node)
395 {
396         set_irg_end_block_(irg, node);
397 }
398
399 ir_node *(get_irg_end)(const ir_graph *irg)
400 {
401         return get_irg_end_(irg);
402 }
403
404 void (set_irg_end)(ir_graph *irg, ir_node *node)
405 {
406         set_irg_end_(irg, node);
407 }
408
409 ir_node *(get_irg_initial_exec)(const ir_graph *irg)
410 {
411         return get_irg_initial_exec_(irg);
412 }
413
414 void (set_irg_initial_exec)(ir_graph *irg, ir_node *node)
415 {
416         set_irg_initial_exec_(irg, node);
417 }
418
419 ir_node *(get_irg_frame)(const ir_graph *irg)
420 {
421         return get_irg_frame_(irg);
422 }
423
424 void (set_irg_frame)(ir_graph *irg, ir_node *node)
425 {
426         set_irg_frame_(irg, node);
427 }
428
429 ir_node *(get_irg_initial_mem)(const ir_graph *irg)
430 {
431         return get_irg_initial_mem_(irg);
432 }
433
434 void (set_irg_initial_mem)(ir_graph *irg, ir_node *node)
435 {
436         set_irg_initial_mem_(irg, node);
437 }
438
439 ir_node *(get_irg_args)(const ir_graph *irg)
440 {
441         return get_irg_args_(irg);
442 }
443
444 void (set_irg_args)(ir_graph *irg, ir_node *node)
445 {
446         set_irg_args_(irg, node);
447 }
448
449 ir_node *(get_irg_no_mem)(const ir_graph *irg)
450 {
451         return get_irg_no_mem_(irg);
452 }
453
454 void (set_irg_no_mem)(ir_graph *irg, ir_node *node)
455 {
456         set_irg_no_mem_(irg, node);
457 }
458
459 ir_entity *(get_irg_entity)(const ir_graph *irg)
460 {
461         return get_irg_entity_(irg);
462 }
463
464 void (set_irg_entity)(ir_graph *irg, ir_entity *ent)
465 {
466         set_irg_entity_(irg, ent);
467 }
468
469 ir_type *(get_irg_frame_type)(ir_graph *irg)
470 {
471         return get_irg_frame_type_(irg);
472 }
473
474 void (set_irg_frame_type)(ir_graph *irg, ir_type *ftp)
475 {
476         set_irg_frame_type_(irg, ftp);
477 }
478
479 int get_irg_n_locs(ir_graph *irg)
480 {
481         return irg->n_loc - 1;
482 }
483
484 int node_is_in_irgs_storage(const ir_graph *irg, const ir_node *n)
485 {
486         /* Check whether the ir_node pointer is on the obstack.
487          * A more sophisticated check would test the "whole" ir_node. */
488         for (struct _obstack_chunk const *p = irg->obst.chunk; p; p = p->prev) {
489                 if (((char *)p->contents <= (char *)n) && ((char *)n < (char *)p->limit))
490                         return 1;
491         }
492
493         return 0;
494 }
495
496 op_pin_state (get_irg_pinned)(const ir_graph *irg)
497 {
498         return get_irg_pinned_(irg);
499 }
500
501 irg_callee_info_state (get_irg_callee_info_state)(const ir_graph *irg)
502 {
503         return get_irg_callee_info_state_(irg);
504 }
505
506 void (set_irg_callee_info_state)(ir_graph *irg, irg_callee_info_state s)
507 {
508         set_irg_callee_info_state_(irg, s);
509 }
510
511 void (set_irg_link)(ir_graph *irg, void *thing)
512 {
513         set_irg_link_(irg, thing);
514 }
515
516 void *(get_irg_link)(const ir_graph *irg)
517 {
518         return get_irg_link_(irg);
519 }
520
521 ir_visited_t (get_irg_visited)(const ir_graph *irg)
522 {
523         return get_irg_visited_(irg);
524 }
525
526 /** maximum visited flag content of all ir_graph visited fields. */
527 static ir_visited_t max_irg_visited = 0;
528
529 void set_irg_visited(ir_graph *irg, ir_visited_t visited)
530 {
531         irg->visited = visited;
532         if (irg->visited > max_irg_visited) {
533                 max_irg_visited = irg->visited;
534         }
535 }
536
537 void inc_irg_visited(ir_graph *irg)
538 {
539         ++irg->visited;
540         if (irg->visited > max_irg_visited) {
541                 max_irg_visited = irg->visited;
542         }
543 }
544
545 ir_visited_t get_max_irg_visited(void)
546 {
547         return max_irg_visited;
548 }
549
550 void set_max_irg_visited(int val)
551 {
552         max_irg_visited = val;
553 }
554
555 ir_visited_t inc_max_irg_visited(void)
556 {
557 #ifndef NDEBUG
558         size_t i;
559         for (i = 0; i < get_irp_n_irgs(); i++)
560                 assert(max_irg_visited >= get_irg_visited(get_irp_irg(i)));
561 #endif
562         return ++max_irg_visited;
563 }
564
565 ir_visited_t (get_irg_block_visited)(const ir_graph *irg)
566 {
567         return get_irg_block_visited_(irg);
568 }
569
570 void (set_irg_block_visited)(ir_graph *irg, ir_visited_t visited)
571 {
572         set_irg_block_visited_(irg, visited);
573 }
574
575 void (inc_irg_block_visited)(ir_graph *irg)
576 {
577   inc_irg_block_visited_(irg);
578 }
579
580 unsigned (get_irg_fp_model)(const ir_graph *irg)
581 {
582         return get_irg_fp_model_(irg);
583 }
584
585 void set_irg_fp_model(ir_graph *irg, unsigned model)
586 {
587         irg->fp_model = model;
588 }
589
590 void set_irg_loc_description(ir_graph *irg, int n, void *description)
591 {
592         assert(0 <= n && n < irg->n_loc);
593
594         if (! irg->loc_descriptions)
595                 irg->loc_descriptions = XMALLOCNZ(void*, irg->n_loc);
596
597         irg->loc_descriptions[n] = description;
598 }
599
600 void *get_irg_loc_description(ir_graph *irg, int n)
601 {
602         assert(0 <= n && n < irg->n_loc);
603         return irg->loc_descriptions ? irg->loc_descriptions[n] : NULL;
604 }
605
606 #ifndef NDEBUG
607 void ir_reserve_resources(ir_graph *irg, ir_resources_t resources)
608 {
609         assert((irg->reserved_resources & resources) == 0);
610         irg->reserved_resources |= resources;
611 }
612
613 void ir_free_resources(ir_graph *irg, ir_resources_t resources)
614 {
615         assert((irg->reserved_resources & resources) == resources);
616         irg->reserved_resources &= ~resources;
617 }
618
619 ir_resources_t ir_resources_reserved(const ir_graph *irg)
620 {
621         return irg->reserved_resources;
622 }
623 #endif
624
625 unsigned get_irg_last_idx(const ir_graph *irg)
626 {
627         return irg->last_node_idx;
628 }
629
630 void add_irg_constraints(ir_graph *irg, ir_graph_constraints_t constraints)
631 {
632         irg->constraints |= constraints;
633 }
634
635 void clear_irg_constraints(ir_graph *irg, ir_graph_constraints_t constraints)
636 {
637         irg->constraints &= ~constraints;
638 }
639
640 int (irg_is_constrained)(const ir_graph *irg, ir_graph_constraints_t constraints)
641 {
642         return irg_is_constrained_(irg, constraints);
643 }
644
645 void (add_irg_properties)(ir_graph *irg, ir_graph_properties_t props)
646 {
647         add_irg_properties_(irg, props);
648 }
649
650 void (clear_irg_properties)(ir_graph *irg, ir_graph_properties_t props)
651 {
652         clear_irg_properties_(irg, props);
653 }
654
655 int (irg_has_properties)(const ir_graph *irg, ir_graph_properties_t props)
656 {
657         return irg_has_properties_(irg, props);
658 }
659
660 typedef void (*assure_property_func)(ir_graph *irg);
661
662 void assure_irg_properties(ir_graph *irg, ir_graph_properties_t props)
663 {
664         static struct {
665                 ir_graph_properties_t property;
666                 assure_property_func  func;
667         } property_functions[] = {
668                 { IR_GRAPH_PROPERTY_ONE_RETURN,               normalize_one_return },
669                 { IR_GRAPH_PROPERTY_MANY_RETURNS,             normalize_n_returns },
670                 { IR_GRAPH_PROPERTY_NO_CRITICAL_EDGES,        remove_critical_cf_edges },
671                 { IR_GRAPH_PROPERTY_NO_UNREACHABLE_CODE,      remove_unreachable_code },
672                 { IR_GRAPH_PROPERTY_NO_BADS,                  remove_bads },
673                 { IR_GRAPH_PROPERTY_NO_TUPLES,                remove_tuples },
674                 { IR_GRAPH_PROPERTY_CONSISTENT_DOMINANCE,     assure_doms },
675                 { IR_GRAPH_PROPERTY_CONSISTENT_POSTDOMINANCE, assure_postdoms },
676                 { IR_GRAPH_PROPERTY_CONSISTENT_OUT_EDGES,     assure_edges },
677                 { IR_GRAPH_PROPERTY_CONSISTENT_OUTS,          assure_irg_outs },
678                 { IR_GRAPH_PROPERTY_CONSISTENT_LOOPINFO,      assure_loopinfo },
679                 { IR_GRAPH_PROPERTY_CONSISTENT_ENTITY_USAGE,  assure_irg_entity_usage_computed },
680                 { IR_GRAPH_PROPERTY_CONSISTENT_DOMINANCE_FRONTIERS, ir_compute_dominance_frontiers },
681         };
682         size_t i;
683         for (i = 0; i < ARRAY_SIZE(property_functions); ++i) {
684                 ir_graph_properties_t missing = props & ~irg->properties;
685                 if (missing & property_functions[i].property)
686                         property_functions[i].func(irg);
687         }
688         assert((props & ~irg->properties) == IR_GRAPH_PROPERTIES_NONE);
689 }
690
691 void confirm_irg_properties(ir_graph *irg, ir_graph_properties_t props)
692 {
693         clear_irg_properties(irg, ~props);
694         if (! (props & IR_GRAPH_PROPERTY_CONSISTENT_OUT_EDGES))
695                 edges_deactivate(irg);
696         if (! (props & IR_GRAPH_PROPERTY_CONSISTENT_OUTS)
697             && (irg->properties & IR_GRAPH_PROPERTY_CONSISTENT_OUTS))
698             free_irg_outs(irg);
699         if (! (props & IR_GRAPH_PROPERTY_CONSISTENT_DOMINANCE_FRONTIERS))
700                 ir_free_dominance_frontiers(irg);
701 }