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