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