Fixed some warning about unused variables.
[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  * @version     $Id$
24  */
25 #include "config.h"
26
27 #include <stdbool.h>
28
29 #include "beinfo.h"
30 #include "bearch.h"
31 #include "benode.h"
32 #include "besched.h"
33 #include "irgwalk.h"
34 #include "irnode_t.h"
35 #include "irdump_t.h"
36 #include "error.h"
37
38 static copy_attr_func old_phi_copy_attr;
39
40 void be_info_new_node(ir_node *node)
41 {
42         struct obstack *obst;
43         backend_info_t *info;
44
45         /* Projs need no be info, their tuple holds all information */
46         if (is_Proj(node))
47                 return;
48
49         obst = be_get_be_obst(current_ir_graph);
50         info = OALLOCZ(obst, backend_info_t);
51
52         assert(node->backend_info == NULL);
53         node->backend_info = info;
54
55         /*
56          * Set backend info for some middleend nodes which still appear in
57          * backend graphs
58          */
59         switch (get_irn_opcode(node)) {
60         case iro_Bad:
61         case iro_Block:
62         case iro_Dummy:
63         case iro_End:
64         case iro_Unknown:
65                 info->flags |= arch_irn_flags_not_scheduled;
66                 break;
67         case iro_NoMem:
68         case iro_Anchor:
69         case iro_Pin:
70         case iro_Sync:
71                 info->flags |= arch_irn_flags_not_scheduled;
72                 info->out_infos = NEW_ARR_D(reg_out_info_t, obst, 1);
73                 memset(info->out_infos, 0, 1 * sizeof(info->out_infos[0]));
74                 info->out_infos[0].req = arch_no_register_req;
75                 break;
76         case iro_Phi:
77                 info->out_infos = NEW_ARR_D(reg_out_info_t, obst, 1);
78                 memset(info->out_infos, 0, 1 * sizeof(info->out_infos[0]));
79                 info->out_infos[0].req = arch_no_register_req;
80                 break;
81         default:
82                 break;
83         }
84 }
85
86 static void new_phi_copy_attr(ir_graph *irg, const ir_node *old_node,
87                               ir_node *new_node)
88 {
89         backend_info_t *old_info = be_get_info(old_node);
90         backend_info_t *new_info = be_get_info(new_node);
91
92         *new_info = *old_info;
93
94         old_phi_copy_attr(irg, old_node, new_node);
95 }
96
97 int be_nodes_equal(const ir_node *node1, const ir_node *node2)
98 {
99         const backend_info_t *info1 = be_get_info(node1);
100         const backend_info_t *info2 = be_get_info(node2);
101         size_t                len   = ARR_LEN(info1->out_infos);
102         int                   arity = get_irn_arity(node1);
103         int                   in;
104         size_t                i;
105
106         if (ARR_LEN(info2->out_infos) != len)
107                 return false;
108
109         assert(arity == get_irn_arity(node2));
110
111         for (in = 0; in < arity; ++in) {
112                 if (info1->in_reqs[in] != info2->in_reqs[in])
113                         return false;
114         }
115
116         for (i = 0; i < len; ++i) {
117                 const reg_out_info_t *out1 = &info1->out_infos[i];
118                 const reg_out_info_t *out2 = &info2->out_infos[i];
119                 if (out1->reg != out2->reg)
120                         return false;
121                 if (!reg_reqs_equal(out1->req, out2->req))
122                         return false;
123         }
124
125         return true;
126 }
127
128 static void init_walker(ir_node *node, void *data)
129 {
130         (void) data;
131         be_info_new_node(node);
132 }
133
134 static bool initialized = false;
135
136 void be_info_init(void)
137 {
138         if (initialized)
139                 panic("double initialization of be_info");
140
141         old_phi_copy_attr = op_Phi->ops.copy_attr;
142         op_Phi->ops.copy_attr = new_phi_copy_attr;
143         initialized = true;
144
145         /* phis have register and register requirements now which we want to dump */
146         assert(op_Phi->ops.dump_node == NULL);
147         op_Phi->ops.dump_node = be_dump_phi_reg_reqs;
148 }
149
150 /**
151  * Edge hook to dump the schedule edges.
152  */
153 static void sched_edge_hook(FILE *F, ir_node *irn)
154 {
155         if (is_Proj(irn))
156                 return;
157         if (get_irn_irg(irn)->be_data == NULL)
158                 return;
159
160         if (sched_is_scheduled(irn) && sched_has_prev(irn)) {
161                 ir_node *prev = sched_prev(irn);
162                 fprintf(F, "edge:{sourcename:\"");
163                 PRINT_NODEID(irn);
164                 fprintf(F, "\" targetname:\"");
165                 PRINT_NODEID(prev);
166                 fprintf(F, "\" color:magenta}\n");
167         }
168 }
169
170 void be_info_init_irg(ir_graph *irg)
171 {
172         irg_walk_anchors(irg, init_walker, NULL, NULL);
173
174         set_dump_node_edge_hook(sched_edge_hook);
175 }
176
177 void be_info_free(void)
178 {
179         if (!initialized)
180                 panic("be_info_free called without prior init");
181
182         assert(op_Phi->ops.copy_attr == new_phi_copy_attr);
183         op_Phi->ops.copy_attr = old_phi_copy_attr;
184         initialized = false;
185
186         assert(op_Phi->ops.dump_node == be_dump_phi_reg_reqs);
187         op_Phi->ops.dump_node = NULL;
188 }
189
190 int be_info_initialized(const ir_graph *irg)
191 {
192         (void) irg;
193         return initialized;
194 }