added config.h include
[libfirm] / ir / be / beutil.c
1 #ifdef HAVE_CONFIG_H
2 #include "config.h"
3 #endif
4
5 #include <stdio.h>
6
7 #include "irgraph.h"
8 #include "irgwalk.h"
9 #include "ircons.h"
10 #include "iropt.h"
11 #include "irgopt.h"
12 #include "irprintf.h"
13
14 #include "beutil.h"
15 #include "besched_t.h"
16 #include "bera_t.h"
17
18 static void dump_allocated_block(ir_node *block, void *env)
19 {
20         int i, n;
21         const ir_node *irn;
22         FILE *f = env;
23
24         ir_fprintf(f, "node:{title:\"b%N\"\nlabel:\"%n\n", block, block);
25         sched_foreach(block, irn) {
26                 const char *prefix = "";
27
28                 ir_fprintf(f, "\n");
29                 if(is_color(get_irn_color(irn)))
30                         ir_fprintf(f, "r%d = ", get_irn_color(irn));
31                 ir_fprintf(f, "%n(", irn);
32
33                 if(block != get_irg_start_block(get_irn_irg(block))) {
34                         for(i = 0, n = get_irn_arity(irn); i < n; ++i) {
35                                 ir_node *op = get_irn_n(irn, i);
36                                 if(is_allocatable_irn(op)) {
37                                         ir_fprintf(f, "%sr%d", prefix, get_irn_color(op));
38                                         prefix = ", ";
39                                 }
40                         }
41                 }
42
43                 ir_fprintf(f, ")");
44         }
45         ir_fprintf(f, "\"}\n");
46
47         if(get_irg_start_block(get_irn_irg(block)) != block) {
48                 for(i = 0, n = get_irn_arity(block); i < n; ++i) {
49                         ir_node *pred_bl = get_nodes_block(get_irn_n(block, i));
50                         ir_fprintf(f, "edge:{sourcename:\"b%N\" targetname:\"b%N\"}\n", pred_bl, block);
51                 }
52         }
53 }
54
55 void dump_allocated_irg(ir_graph *irg, char *suffix)
56 {
57         char buf[1024];
58         FILE *f;
59
60         snprintf(buf, sizeof(buf), "%s-alloc%s.vcg", get_entity_name(get_irg_entity(irg)), suffix);
61
62         if((f = fopen(buf, "wt")) != NULL) {
63                 fprintf(f, "graph:{title:\"prg\"\n");
64                 irg_block_walk_graph(irg, dump_allocated_block, NULL, f);
65                 fprintf(f, "}\n");
66                 fclose(f);
67         }
68 }
69
70 static void localize_const_walker(ir_node *irn, void *data)
71 {
72         if(!is_Block(irn)) {
73                 int i, n;
74
75                 for(i = 0, n = get_irn_arity(irn); i < n; ++i) {
76                         ir_node *op = get_irn_n(irn, i);
77                         if(get_irn_opcode(op) == iro_Const) {
78                                 ir_node *tgt_block, *cnst;
79
80                                 /* Special treatment for phi nodes, because phi-usage is different */
81                                 tgt_block = get_nodes_block(irn);
82                                 if(is_Phi(irn))
83                                         tgt_block = get_nodes_block(get_irn_n(tgt_block, i));
84
85                                 /*
86                                  * We have to create the const node by ourselves, since the
87                                  * firmcons implementation always places it in the start block.
88                                  */
89                                 cnst = new_ir_node(NULL, get_irn_irg(irn),
90                                                 tgt_block, op_Const, get_irn_mode(op), 0, NULL);
91                                 cnst->attr.con.tv = get_Const_tarval(op);
92                                 set_irn_n(irn, i, cnst);
93                         }
94                 }
95         }
96 }
97
98 void localize_consts(ir_graph *irg)
99 {
100         irg_walk_graph(irg, localize_const_walker, NULL, NULL);
101         dead_node_elimination(irg);
102 }