a50cb9dce306c583c690926e2d68070a0a19f7b1
[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)
53 {
54         char buf[1024];
55         FILE *f;
56
57         snprintf(buf, sizeof(buf), "%s-alloc.vcg", get_entity_name(get_irg_entity(irg)));
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                                 /*
76                                  * We have to create the const node by ourselves, since the
77                                  * firmcons implementation always places it in the start block.
78                                  */
79                                 ir_node *cnst = new_ir_node(NULL, get_irn_irg(irn),
80                                                 get_nodes_block(irn), op_Const, get_irn_mode(op), 0, NULL);
81                                 cnst->attr.con.tv = get_Const_tarval(op);
82                                 set_irn_n(irn, i, cnst);
83                         }
84                 }
85         }
86 }
87
88 void localize_consts(ir_graph *irg)
89 {
90         irg_walk_graph(irg, localize_const_walker, NULL, NULL);
91         dead_node_elimination(irg);
92 }