typo fixed
[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 "bearch.h"
17
18 struct dump_env {
19   FILE *f;
20   arch_env_t *env;
21 };
22
23 static void dump_allocated_block(ir_node *block, void *data)
24 {
25         int i, n;
26         const ir_node *irn;
27   struct dump_env *dump_env = data;
28         FILE *f = dump_env->f;
29   arch_env_t *env = dump_env->env;
30
31         ir_fprintf(f, "node:{title:\"b%N\"\nlabel:\"%n\n", block, block);
32         sched_foreach(block, irn) {
33                 const char *prefix = "";
34     const arch_register_t *reg = arch_get_irn_register(env, irn, 0);
35
36                 ir_fprintf(f, "\n");
37     if(reg)
38       ir_fprintf(f, "%s = ", arch_register_get_name(reg));
39                 ir_fprintf(f, "%n(", irn);
40
41                 if(block != get_irg_start_block(get_irn_irg(block))) {
42                         for(i = 0, n = get_irn_arity(irn); i < n; ++i) {
43                                 ir_node *op = get_irn_n(irn, i);
44         if(arch_is_register_operand(dump_env->env, op, arch_pos_make_out(0))) {
45                                         ir_fprintf(f, "%s%s", prefix,
46               arch_register_get_name(arch_get_irn_register(env, op, 0)));
47                                         prefix = ", ";
48                                 }
49                         }
50                 }
51
52                 ir_fprintf(f, ")");
53         }
54         ir_fprintf(f, "\"}\n");
55
56         if(get_irg_start_block(get_irn_irg(block)) != block) {
57                 for(i = 0, n = get_irn_arity(block); i < n; ++i) {
58                         ir_node *pred_bl = get_nodes_block(get_irn_n(block, i));
59                         ir_fprintf(f, "edge:{sourcename:\"b%N\" targetname:\"b%N\"}\n", pred_bl, block);
60                 }
61         }
62 }
63
64 void dump_allocated_irg(arch_env_t *arch_env, ir_graph *irg, char *suffix)
65 {
66         char buf[1024];
67   struct dump_env env;
68
69   env.env = arch_env;
70
71         ir_snprintf(buf, sizeof(buf), "%F-alloc%s.vcg", irg, suffix);
72
73         if((env.f = fopen(buf, "wt")) != NULL) {
74                 fprintf(env.f, "graph:{title:\"prg\"\n");
75                 irg_block_walk_graph(irg, dump_allocated_block, NULL, &env);
76                 fprintf(env.f, "}\n");
77                 fclose(env.f);
78         }
79 }
80
81 static void localize_const_walker(ir_node *irn, void *data)
82 {
83         if(!is_Block(irn)) {
84                 int i, n;
85
86                 for(i = 0, n = get_irn_arity(irn); i < n; ++i) {
87                         ir_node *op = get_irn_n(irn, i);
88                         if(get_irn_opcode(op) == iro_Const) {
89                                 ir_node *tgt_block, *cnst;
90
91                                 /* Special treatment for phi nodes, because phi-usage is different */
92                                 tgt_block = get_nodes_block(irn);
93                                 if(is_Phi(irn))
94                                         tgt_block = get_nodes_block(get_irn_n(tgt_block, i));
95
96                                 /*
97                                  * We have to create the const node by ourselves, since the
98                                  * firmcons implementation always places it in the start block.
99                                  */
100                                 cnst = new_ir_node(NULL, get_irn_irg(irn),
101                                                 tgt_block, op_Const, get_irn_mode(op), 0, NULL);
102                                 cnst->attr.con.tv = get_Const_tarval(op);
103                                 set_irn_n(irn, i, cnst);
104                         }
105                 }
106         }
107 }
108
109 void localize_consts(ir_graph *irg)
110 {
111         irg_walk_graph(irg, localize_const_walker, NULL, NULL);
112         dead_node_elimination(irg);
113 }