put register classes into arch_env struct, no need for complicated callbacks
[libfirm] / ir / be / beinfo.c
1 /*
2  * Copyright (C) 1995-2008 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         /* Hack! We still have middle end nodes in the backend (which was probably
56            a bad decision back then), which have no register constraints.
57            Set some none_requirements here.
58          */
59         if (get_irn_mode(node) != mode_T
60                         && get_irn_opcode(node) <= iro_Last) {
61                 info->out_infos = NEW_ARR_D(reg_out_info_t, obst, 1);
62                 memset(info->out_infos, 0, 1 * sizeof(info->out_infos[0]));
63                 info->out_infos[0].req = arch_no_register_req;
64         }
65 }
66
67 static void new_phi_copy_attr(ir_graph *irg, const ir_node *old_node,
68                               ir_node *new_node)
69 {
70         backend_info_t *old_info = be_get_info(old_node);
71         backend_info_t *new_info = be_get_info(new_node);
72
73         *new_info = *old_info;
74
75         old_phi_copy_attr(irg, old_node, new_node);
76 }
77
78 int be_nodes_equal(ir_node *node1, ir_node *node2)
79 {
80         const backend_info_t *info1 = be_get_info(node1);
81         const backend_info_t *info2 = be_get_info(node2);
82         int                   len   = ARR_LEN(info1->out_infos);
83         int                   arity = get_irn_arity(node1);
84         int                   i;
85
86         if (ARR_LEN(info2->out_infos) != len)
87                 return false;
88
89         assert(arity == get_irn_arity(node2));
90
91         for (i = 0; i < arity; ++i) {
92                 if (info1->in_reqs[i] != info2->in_reqs[i])
93                         return false;
94         }
95
96         for (i = 0; i < len; ++i) {
97                 const reg_out_info_t *out1 = &info1->out_infos[i];
98                 const reg_out_info_t *out2 = &info2->out_infos[i];
99                 if (out1->reg != out2->reg)
100                         return false;
101                 if (!reg_reqs_equal(out1->req, out2->req))
102                         return false;
103         }
104
105         return true;
106 }
107
108 static void init_walker(ir_node *node, void *data)
109 {
110         (void) data;
111         be_info_new_node(node);
112 }
113
114 static bool initialized = false;
115
116 void be_info_init(void)
117 {
118         if (initialized)
119                 panic("double initialization of be_info");
120
121         old_phi_copy_attr = op_Phi->ops.copy_attr;
122         op_Phi->ops.copy_attr = new_phi_copy_attr;
123         initialized = true;
124
125         /* phis have register and register requirements now which we want to dump */
126         assert(op_Phi->ops.dump_node == NULL);
127         op_Phi->ops.dump_node = be_dump_phi_reg_reqs;
128 }
129
130 /**
131  * Edge hook to dump the schedule edges.
132  */
133 static void sched_edge_hook(FILE *F, ir_node *irn)
134 {
135         if (is_Proj(irn))
136                 return;
137         if (get_irn_irg(irn)->be_data == NULL)
138                 return;
139
140         if (sched_is_scheduled(irn) && sched_has_prev(irn)) {
141                 ir_node *prev = sched_prev(irn);
142                 fprintf(F, "edge:{sourcename:\"");
143                 PRINT_NODEID(irn);
144                 fprintf(F, "\" targetname:\"");
145                 PRINT_NODEID(prev);
146                 fprintf(F, "\" color:magenta}\n");
147         }
148 }
149
150 void be_info_init_irg(ir_graph *irg)
151 {
152         irg_walk_anchors(irg, init_walker, NULL, NULL);
153
154         set_dump_node_edge_hook(sched_edge_hook);
155 }
156
157 void be_info_free(void)
158 {
159         if (!initialized)
160                 panic("be_info_free called without prior init");
161
162         assert(op_Phi->ops.copy_attr == new_phi_copy_attr);
163         op_Phi->ops.copy_attr = old_phi_copy_attr;
164         initialized = false;
165
166         assert(op_Phi->ops.dump_node == be_dump_phi_reg_reqs);
167         op_Phi->ops.dump_node = NULL;
168 }
169
170 int be_info_initialized(const ir_graph *irg)
171 {
172         (void) irg;
173         return initialized;
174 }