04e7e1b9f463bf8e9228df7fe5bc2c1114213c2b
[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 "irdump_t.h"
10 #include "ircons.h"
11 #include "iropt.h"
12 #include "irgopt.h"
13 #include "irprintf.h"
14
15 #include "beutil.h"
16 #include "besched_t.h"
17 #include "bearch.h"
18
19 struct dump_env {
20   FILE *f;
21   arch_env_t *env;
22 };
23
24 static void dump_allocated_block(ir_node *block, void *data)
25 {
26         int i, n;
27         const ir_node *irn;
28   struct dump_env *dump_env = data;
29         FILE *f = dump_env->f;
30   arch_env_t *env = dump_env->env;
31
32         ir_fprintf(f, "node:{title:\"b%N\"\nlabel:\"%n\n", block, block);
33         sched_foreach(block, irn) {
34                 const char *prefix = "";
35     const arch_register_t *reg = arch_get_irn_register(env, irn, 0);
36
37                 ir_fprintf(f, "\n");
38     if(reg)
39       ir_fprintf(f, "%s = ", arch_register_get_name(reg));
40                 ir_fprintf(f, "%n(", irn);
41
42                 if(block != get_irg_start_block(get_irn_irg(block))) {
43                         for(i = 0, n = get_irn_arity(irn); i < n; ++i) {
44                                 ir_node *op = get_irn_n(irn, i);
45         if(arch_is_register_operand(dump_env->env, op, arch_pos_make_out(0))) {
46                                         ir_fprintf(f, "%s%s", prefix,
47               arch_register_get_name(arch_get_irn_register(env, op, 0)));
48                                         prefix = ", ";
49                                 }
50                         }
51                 }
52
53                 ir_fprintf(f, ")");
54         }
55         ir_fprintf(f, "\"}\n");
56
57         if(get_irg_start_block(get_irn_irg(block)) != block) {
58                 for(i = 0, n = get_irn_arity(block); i < n; ++i) {
59                         ir_node *pred_bl = get_nodes_block(get_irn_n(block, i));
60                         ir_fprintf(f, "edge:{sourcename:\"b%N\" targetname:\"b%N\"}\n", pred_bl, block);
61                 }
62         }
63 }
64
65 void dump_allocated_irg(arch_env_t *arch_env, ir_graph *irg, char *suffix)
66 {
67         char buf[1024];
68   struct dump_env env;
69
70   env.env = arch_env;
71
72         ir_snprintf(buf, sizeof(buf), "%F-alloc%s.vcg", irg, suffix);
73
74         if((env.f = fopen(buf, "wt")) != NULL) {
75                 fprintf(env.f, "graph:{title:\"prg\"\n");
76                 irg_block_walk_graph(irg, dump_allocated_block, NULL, &env);
77                 fprintf(env.f, "}\n");
78                 fclose(env.f);
79         }
80 }
81
82 static void localize_const_walker(ir_node *irn, void *data)
83 {
84         if(!is_Block(irn)) {
85                 int i, n;
86
87                 for(i = 0, n = get_irn_arity(irn); i < n; ++i) {
88                         ir_node *op = get_irn_n(irn, i);
89                         if(get_irn_opcode(op) == iro_Const) {
90                                 ir_node *tgt_block, *cnst;
91
92                                 /* Special treatment for phi nodes, because phi-usage is different */
93                                 tgt_block = get_nodes_block(irn);
94                                 if(is_Phi(irn))
95                                         tgt_block = get_nodes_block(get_irn_n(tgt_block, i));
96
97                                 /*
98                                  * We have to create the const node by ourselves, since the
99                                  * firmcons implementation always places it in the start block.
100                                  */
101                                 cnst = new_ir_node(NULL, get_irn_irg(irn),
102                                                 tgt_block, op_Const, get_irn_mode(op), 0, NULL);
103                                 cnst->attr.con.tv = get_Const_tarval(op);
104                                 set_irn_n(irn, i, cnst);
105                         }
106                 }
107         }
108 }
109
110 void localize_consts(ir_graph *irg)
111 {
112         irg_walk_graph(irg, localize_const_walker, NULL, NULL);
113         dead_node_elimination(irg);
114 }
115
116 static int sched_edge_hook(FILE *F, ir_node *irn)
117 {
118     if(sched_is_scheduled(irn) && sched_has_prev(irn)) {
119         ir_node *prev = sched_prev(irn);
120         fprintf(F, "edge:{sourcename:\"");
121         PRINT_NODEID(irn);
122         fprintf(F, "\" targetname:\"");
123         PRINT_NODEID(prev);
124         fprintf(F, "\" color:magenta}\n");
125     }
126     return 1;
127 }
128
129 void dump_ir_block_graph_sched(ir_graph *irg, const char *suffix) {
130     DUMP_NODE_EDGE_FUNC old = get_dump_node_edge_hook();
131
132     set_dump_node_edge_hook(sched_edge_hook);
133     dump_ir_block_graph(irg, suffix);
134     set_dump_node_edge_hook(old);
135 }
136
137 static void clear_link(ir_node *irn, void *data)
138 {
139   set_irn_link(irn, NULL);
140 }
141
142 static void collect_phis(ir_node *irn, void *data)
143 {
144   if(is_Phi(irn)) {
145     ir_node *bl = get_nodes_block(irn);
146     set_irn_link(irn, get_irn_link(bl));
147     set_irn_link(bl, irn);
148   }
149 }
150
151 void be_clear_links(ir_graph *irg)
152 {
153         irg_walk_graph(irg, clear_link, NULL, NULL);
154 }
155
156 void be_collect_phis(ir_graph *irg)
157 {
158         irg_walk_graph(irg, collect_phis, NULL, NULL);
159 }