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