besched: Change sched_foreach_from(sched_next(x), y) to sched_foreach_after(x, y).
[libfirm] / ir / be / beinfo.c
1 /*
2  * This file is part of libFirm.
3  * Copyright (C) 2012 University of Karlsruhe.
4  */
5
6 /**
7  * @file
8  * @author      Matthias Braun
9  */
10 #include "config.h"
11
12 #include <stdbool.h>
13
14 #include "beinfo.h"
15 #include "beirg.h"
16 #include "bearch.h"
17 #include "benode.h"
18 #include "besched.h"
19 #include "bedump.h"
20 #include "belive_t.h"
21 #include "irgwalk.h"
22 #include "irnode_t.h"
23 #include "irdump_t.h"
24 #include "irhooks.h"
25 #include "error.h"
26
27 static copy_attr_func old_phi_copy_attr;
28
29 void be_info_new_node(ir_graph *irg, ir_node *node)
30 {
31         struct obstack *obst;
32         backend_info_t *info;
33
34         /* Projs need no be info, all info is fetched from their predecessor */
35         if (is_Proj(node))
36                 return;
37
38         obst = be_get_be_obst(irg);
39         info = OALLOCZ(obst, backend_info_t);
40
41         assert(node->backend_info == NULL);
42         node->backend_info = info;
43
44         /*
45          * Set backend info for some middleend nodes which still appear in
46          * backend graphs
47          */
48         switch (get_irn_opcode(node)) {
49         case iro_Block:
50         case iro_Dummy:
51         case iro_NoMem:
52         case iro_Anchor:
53         case iro_Pin:
54         case iro_Sync:
55         case iro_Bad:
56         case iro_End:
57         case iro_Unknown:
58                 info->flags |= arch_irn_flags_not_scheduled;
59                 /* FALLTHROUGH */
60         case iro_Phi:
61                 info->out_infos        = NEW_ARR_DZ(reg_out_info_t, obst, 1);
62                 info->out_infos[0].req = arch_no_register_req;
63                 break;
64         default:
65                 break;
66         }
67 }
68
69 static void new_phi_copy_attr(ir_graph *irg, const ir_node *old_node,
70                               ir_node *new_node)
71 {
72         backend_info_t *old_info = be_get_info(old_node);
73         backend_info_t *new_info = be_get_info(new_node);
74
75         *new_info = *old_info;
76
77         old_phi_copy_attr(irg, old_node, new_node);
78 }
79
80 int be_nodes_equal(const ir_node *node1, const ir_node *node2)
81 {
82         const backend_info_t *info1 = be_get_info(node1);
83         const backend_info_t *info2 = be_get_info(node2);
84         size_t                len   = ARR_LEN(info1->out_infos);
85         int                   arity = get_irn_arity(node1);
86         int                   in;
87         size_t                i;
88
89         if (ARR_LEN(info2->out_infos) != len)
90                 return false;
91
92         assert(arity == get_irn_arity(node2));
93
94         for (in = 0; in < arity; ++in) {
95                 if (info1->in_reqs[in] != info2->in_reqs[in])
96                         return false;
97         }
98
99         for (i = 0; i < len; ++i) {
100                 const reg_out_info_t *out1 = &info1->out_infos[i];
101                 const reg_out_info_t *out2 = &info2->out_infos[i];
102                 if (out1->reg != out2->reg)
103                         return false;
104                 if (!reg_reqs_equal(out1->req, out2->req))
105                         return false;
106         }
107
108         return true;
109 }
110
111 static void init_walker(ir_node *node, void *data)
112 {
113         ir_graph *irg = get_irn_irg(node);
114         (void) data;
115         be_info_new_node(irg, node);
116 }
117
118 static bool         initialized = false;
119 static hook_entry_t hook_liveness_info;
120
121 static void dump_liveness_info_hook(void *context, FILE *F, const ir_node *node)
122 {
123         (void)context;
124         if (!is_Block(node))
125                 return;
126         ir_graph *irg = get_irn_irg(node);
127         if (!irg_is_constrained(irg, IR_GRAPH_CONSTRAINT_BACKEND))
128                 return;
129
130         be_lv_t *lv = be_get_irg_liveness(irg);
131         if (lv == NULL)
132                 return;
133         if (!lv->sets_valid)
134                 return;
135
136         be_dump_liveness_block(lv, F, node);
137 }
138
139 void be_info_init(void)
140 {
141         if (initialized)
142                 panic("double initialization of be_info");
143
144         old_phi_copy_attr = op_Phi->ops.copy_attr;
145         op_Phi->ops.copy_attr = new_phi_copy_attr;
146         initialized = true;
147
148         /* phis have register and register requirements now which we want to dump */
149         assert(op_Phi->ops.dump_node == NULL);
150         op_Phi->ops.dump_node = be_dump_phi_reg_reqs;
151
152         hook_liveness_info.hook._hook_node_info = dump_liveness_info_hook;
153         register_hook(hook_node_info, &hook_liveness_info);
154 }
155
156 /**
157  * Edge hook to dump the schedule edges.
158  */
159 static void sched_edge_hook(FILE *F, const ir_node *irn)
160 {
161         ir_graph *irg = get_irn_irg(irn);
162         if (!irg_is_constrained(irg, IR_GRAPH_CONSTRAINT_BACKEND))
163                 return;
164
165         if (is_Proj(irn) || is_Block(irn) || !sched_is_scheduled(irn))
166                 return;
167
168         ir_node *const prev = sched_prev(irn);
169         if (!sched_is_begin(prev)) {
170                 fprintf(F, "edge:{sourcename: ");
171                 print_nodeid(F, irn);
172                 fprintf(F, " targetname: ");
173                 print_nodeid(F, prev);
174                 fprintf(F, " color:magenta}\n");
175         }
176 }
177
178 void be_info_init_irg(ir_graph *irg)
179 {
180         add_irg_constraints(irg, IR_GRAPH_CONSTRAINT_BACKEND);
181         irg_walk_anchors(irg, init_walker, NULL, NULL);
182
183         set_dump_node_edge_hook(sched_edge_hook);
184 }
185
186 void be_info_free(void)
187 {
188         if (!initialized)
189                 panic("called without prior init");
190
191         assert(op_Phi->ops.copy_attr == new_phi_copy_attr);
192         op_Phi->ops.copy_attr = old_phi_copy_attr;
193         initialized = false;
194
195         assert(op_Phi->ops.dump_node == be_dump_phi_reg_reqs);
196         op_Phi->ops.dump_node = NULL;
197
198         unregister_hook(hook_node_info, &hook_liveness_info);
199 }