avoid putting interfering stuff into the same congruence class
[libfirm] / ir / be / bearch.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  * @brief       Processor architecture specification.
23  * @author      Sebastian Hack
24  * @version     $Id$
25  */
26 #include "config.h"
27
28 #include <string.h>
29
30 #include "bearch.h"
31 #include "benode_t.h"
32 #include "beinfo.h"
33 #include "ircons_t.h"
34 #include "irnode_t.h"
35 #include "irop_t.h"
36
37 #include "bitset.h"
38 #include "pset.h"
39 #include "raw_bitset.h"
40
41 #include "irprintf.h"
42
43 /* Initialize the architecture environment struct. */
44 arch_env_t *arch_env_init(const arch_isa_if_t *isa_if, FILE *file_handle, be_main_env_t *main_env)
45 {
46         arch_env_t *arch_env = isa_if->init(file_handle);
47         arch_env->main_env   = main_env;
48         return arch_env;
49 }
50
51 /**
52  * Get the isa responsible for a node.
53  * @param irn The node to get the responsible isa for.
54  * @return The irn operations given by the responsible isa.
55  */
56 static inline const arch_irn_ops_t *get_irn_ops(const ir_node *irn)
57 {
58         const ir_op          *ops;
59         const arch_irn_ops_t *be_ops;
60
61         if (is_Proj(irn)) {
62                 irn = get_Proj_pred(irn);
63                 assert(!is_Proj(irn));
64         }
65
66         ops    = get_irn_op(irn);
67         be_ops = get_op_ops(ops)->be_ops;
68
69         return be_ops;
70 }
71
72 const arch_register_req_t *arch_get_register_req(const ir_node *irn, int pos)
73 {
74         const arch_irn_ops_t *ops;
75
76         if (is_Proj(irn)) {
77                 assert(pos == -1);
78                 pos = -1-get_Proj_proj(irn);
79                 irn = get_Proj_pred(irn);
80         }
81         ops = get_irn_ops_simple(irn);
82         if (pos < 0) {
83                 return ops->get_irn_reg_req_out(irn, -pos-1);
84         } else {
85                 return ops->get_irn_reg_req_in(irn, pos);
86         }
87 }
88
89 const arch_register_req_t *arch_get_register_req_out(const ir_node *irn)
90 {
91         int                   pos = 0;
92         const arch_irn_ops_t *ops;
93
94         if (is_Proj(irn)) {
95                 pos = get_Proj_proj(irn);
96                 irn = get_Proj_pred(irn);
97         } else if (get_irn_mode(irn) == mode_T) {
98                 return arch_no_register_req;
99         }
100         ops = get_irn_ops(irn);
101         return ops->get_irn_reg_req_out(irn, pos);
102 }
103
104 void arch_set_frame_offset(ir_node *irn, int offset)
105 {
106         const arch_irn_ops_t *ops = get_irn_ops(irn);
107         ops->set_frame_offset(irn, offset);
108 }
109
110 ir_entity *arch_get_frame_entity(const ir_node *irn)
111 {
112         const arch_irn_ops_t *ops = get_irn_ops(irn);
113         return ops->get_frame_entity(irn);
114 }
115
116 void arch_set_frame_entity(ir_node *irn, ir_entity *ent)
117 {
118         const arch_irn_ops_t *ops = get_irn_ops(irn);
119         ops->set_frame_entity(irn, ent);
120 }
121
122 int arch_get_sp_bias(ir_node *irn)
123 {
124         const arch_irn_ops_t *ops = get_irn_ops(irn);
125         return ops->get_sp_bias(irn);
126 }
127
128 arch_inverse_t *arch_get_inverse(const ir_node *irn, int i, arch_inverse_t *inverse, struct obstack *obstack)
129 {
130         const arch_irn_ops_t *ops = get_irn_ops(irn);
131
132         if(ops->get_inverse) {
133                 return ops->get_inverse(irn, i, inverse, obstack);
134         } else {
135                 return NULL;
136         }
137 }
138
139 int arch_possible_memory_operand(const ir_node *irn, unsigned int i)
140 {
141         const arch_irn_ops_t *ops = get_irn_ops(irn);
142
143         if(ops->possible_memory_operand) {
144                 return ops->possible_memory_operand(irn, i);
145         } else {
146                 return 0;
147         }
148 }
149
150 void arch_perform_memory_operand(ir_node *irn, ir_node *spill, unsigned int i)
151 {
152         const arch_irn_ops_t *ops = get_irn_ops(irn);
153
154         if(ops->perform_memory_operand) {
155                 ops->perform_memory_operand(irn, spill, i);
156         } else {
157                 return;
158         }
159 }
160
161 int arch_get_op_estimated_cost(const ir_node *irn)
162 {
163         const arch_irn_ops_t *ops = get_irn_ops(irn);
164
165         if(ops->get_op_estimated_cost) {
166                 return ops->get_op_estimated_cost(irn);
167         } else {
168                 return 1;
169         }
170 }
171
172 void arch_put_non_ignore_regs(const arch_register_class_t *cls, bitset_t *bs)
173 {
174         unsigned i;
175
176         for(i = 0; i < cls->n_regs; ++i) {
177                 if(!arch_register_type_is(&cls->regs[i], ignore))
178                         bitset_set(bs, i);
179         }
180 }
181
182 int arch_reg_is_allocatable(const ir_node *irn, int pos,
183                             const arch_register_t *reg)
184 {
185         const arch_register_req_t *req = arch_get_register_req(irn, pos);
186
187         if(req->type == arch_register_req_type_none)
188                 return 0;
189
190         if(arch_register_req_is(req, limited)) {
191                 if (arch_register_get_class(reg) != req->cls)
192                         return 0;
193                 return rbitset_is_set(req->limited, arch_register_get_index(reg));
194         }
195
196         return req->cls == reg->reg_class;
197 }
198
199 const arch_register_class_t *arch_get_irn_reg_class(const ir_node *irn, int pos)
200 {
201         const arch_register_req_t *req = arch_get_register_req(irn, pos);
202
203         assert(req->type != arch_register_req_type_none || req->cls == NULL);
204
205         return req->cls;
206 }
207
208 static inline reg_out_info_t *get_out_info(const ir_node *node)
209 {
210         int                   pos  = 0;
211         const backend_info_t *info;
212
213         assert(get_irn_mode(node) != mode_T);
214         if (is_Proj(node)) {
215                 pos  = get_Proj_proj(node);
216                 node = get_Proj_pred(node);
217         }
218
219         info = be_get_info(node);
220         assert(pos >= 0 && pos < ARR_LEN(info->out_infos));
221         return &info->out_infos[pos];
222 }
223
224
225 static inline reg_out_info_t *get_out_info_n(const ir_node *node, int pos)
226 {
227         const backend_info_t *info = be_get_info(node);
228         assert(!is_Proj(node));
229         assert(pos >= 0 && pos < ARR_LEN(info->out_infos));
230         return &info->out_infos[pos];
231 }
232
233
234 const arch_register_t *arch_get_irn_register(const ir_node *node)
235 {
236         const reg_out_info_t *out = get_out_info(node);
237         return out->reg;
238 }
239
240 const arch_register_t *arch_irn_get_register(const ir_node *node, int pos)
241 {
242         const reg_out_info_t *out = get_out_info_n(node, pos);
243         return out->reg;
244 }
245
246 void arch_irn_set_register(ir_node *node, int pos, const arch_register_t *reg)
247 {
248         reg_out_info_t *out = get_out_info_n(node, pos);
249         out->reg            = reg;
250 }
251
252 void arch_set_irn_register(ir_node *node, const arch_register_t *reg)
253 {
254         reg_out_info_t *out = get_out_info(node);
255         out->reg = reg;
256 }
257
258 arch_irn_class_t arch_irn_classify(const ir_node *node)
259 {
260         const arch_irn_ops_t *ops = get_irn_ops(node);
261         return ops->classify(node);
262 }
263
264 arch_irn_flags_t arch_irn_get_flags(const ir_node *node)
265 {
266         backend_info_t *info = be_get_info(node);
267         return info->flags;
268 }
269
270 void arch_irn_set_flags(ir_node *node, arch_irn_flags_t flags)
271 {
272         backend_info_t *info = be_get_info(node);
273         info->flags = flags;
274 }
275
276 void arch_irn_add_flags(ir_node *node, arch_irn_flags_t flags)
277 {
278         backend_info_t *info = be_get_info(node);
279         info->flags |= flags;
280 }
281
282 void arch_dump_register_req(FILE *F, const arch_register_req_t *req,
283                             const ir_node *node)
284 {
285         if (req == NULL || req->type == arch_register_req_type_none) {
286                 fprintf(F, "n/a");
287                 return;
288         }
289
290         fprintf(F, "%s", req->cls->name);
291
292         if(arch_register_req_is(req, limited)) {
293                 unsigned n_regs = req->cls->n_regs;
294                 unsigned i;
295
296                 fprintf(F, " limited to");
297                 for(i = 0; i < n_regs; ++i) {
298                         if(rbitset_is_set(req->limited, i)) {
299                                 const arch_register_t *reg = &req->cls->regs[i];
300                                 fprintf(F, " %s", reg->name);
301                         }
302                 }
303         }
304
305         if(arch_register_req_is(req, should_be_same)) {
306                 const unsigned other = req->other_same;
307                 int i;
308
309                 fprintf(F, " same as");
310                 for (i = 0; 1U << i <= other; ++i) {
311                         if (other & (1U << i)) {
312                                 ir_fprintf(F, " %+F", get_irn_n(skip_Proj_const(node), i));
313                         }
314                 }
315         }
316
317         if (arch_register_req_is(req, must_be_different)) {
318                 const unsigned other = req->other_different;
319                 int i;
320
321                 fprintf(F, " different from");
322                 for (i = 0; 1U << i <= other; ++i) {
323                         if (other & (1U << i)) {
324                                 ir_fprintf(F, " %+F", get_irn_n(skip_Proj_const(node), i));
325                         }
326                 }
327         }
328
329         if (arch_register_req_is(req, ignore)) {
330                 fprintf(F, " ignore");
331         }
332         if (arch_register_req_is(req, produces_sp)) {
333                 fprintf(F, " produces_sp");
334         }
335 }
336
337 static const arch_register_req_t no_requirement = {
338         arch_register_req_type_none,
339         NULL,
340         NULL,
341         0,
342         0
343 };
344 const arch_register_req_t *arch_no_register_req = &no_requirement;