inline arch_get_register_req_out to make the backend slightly faster
[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 void arch_set_frame_offset(ir_node *irn, int offset)
90 {
91         const arch_irn_ops_t *ops = get_irn_ops(irn);
92         ops->set_frame_offset(irn, offset);
93 }
94
95 ir_entity *arch_get_frame_entity(const ir_node *irn)
96 {
97         const arch_irn_ops_t *ops = get_irn_ops(irn);
98         return ops->get_frame_entity(irn);
99 }
100
101 void arch_set_frame_entity(ir_node *irn, ir_entity *ent)
102 {
103         const arch_irn_ops_t *ops = get_irn_ops(irn);
104         ops->set_frame_entity(irn, ent);
105 }
106
107 int arch_get_sp_bias(ir_node *irn)
108 {
109         const arch_irn_ops_t *ops = get_irn_ops(irn);
110         return ops->get_sp_bias(irn);
111 }
112
113 arch_inverse_t *arch_get_inverse(const ir_node *irn, int i, arch_inverse_t *inverse, struct obstack *obstack)
114 {
115         const arch_irn_ops_t *ops = get_irn_ops(irn);
116
117         if(ops->get_inverse) {
118                 return ops->get_inverse(irn, i, inverse, obstack);
119         } else {
120                 return NULL;
121         }
122 }
123
124 int arch_possible_memory_operand(const ir_node *irn, unsigned int i)
125 {
126         const arch_irn_ops_t *ops = get_irn_ops(irn);
127
128         if(ops->possible_memory_operand) {
129                 return ops->possible_memory_operand(irn, i);
130         } else {
131                 return 0;
132         }
133 }
134
135 void arch_perform_memory_operand(ir_node *irn, ir_node *spill, unsigned int i)
136 {
137         const arch_irn_ops_t *ops = get_irn_ops(irn);
138
139         if(ops->perform_memory_operand) {
140                 ops->perform_memory_operand(irn, spill, i);
141         } else {
142                 return;
143         }
144 }
145
146 int arch_get_op_estimated_cost(const ir_node *irn)
147 {
148         const arch_irn_ops_t *ops = get_irn_ops(irn);
149
150         if(ops->get_op_estimated_cost) {
151                 return ops->get_op_estimated_cost(irn);
152         } else {
153                 return 1;
154         }
155 }
156
157 void arch_put_non_ignore_regs(const arch_register_class_t *cls, bitset_t *bs)
158 {
159         unsigned i;
160
161         for(i = 0; i < cls->n_regs; ++i) {
162                 if(!arch_register_type_is(&cls->regs[i], ignore))
163                         bitset_set(bs, i);
164         }
165 }
166
167 int arch_reg_is_allocatable(const ir_node *irn, int pos,
168                             const arch_register_t *reg)
169 {
170         const arch_register_req_t *req = arch_get_register_req(irn, pos);
171
172         if(req->type == arch_register_req_type_none)
173                 return 0;
174
175         if(arch_register_req_is(req, limited)) {
176                 if (arch_register_get_class(reg) != req->cls)
177                         return 0;
178                 return rbitset_is_set(req->limited, arch_register_get_index(reg));
179         }
180
181         return req->cls == reg->reg_class;
182 }
183
184 const arch_register_class_t *arch_get_irn_reg_class(const ir_node *irn, int pos)
185 {
186         const arch_register_req_t *req = arch_get_register_req(irn, pos);
187
188         assert(req->type != arch_register_req_type_none || req->cls == NULL);
189
190         return req->cls;
191 }
192
193 static inline reg_out_info_t *get_out_info(const ir_node *node)
194 {
195         int                   pos  = 0;
196         const backend_info_t *info;
197
198         assert(get_irn_mode(node) != mode_T);
199         if (is_Proj(node)) {
200                 pos  = get_Proj_proj(node);
201                 node = get_Proj_pred(node);
202         }
203
204         info = be_get_info(node);
205         assert(pos >= 0 && pos < ARR_LEN(info->out_infos));
206         return &info->out_infos[pos];
207 }
208
209
210 static inline reg_out_info_t *get_out_info_n(const ir_node *node, int pos)
211 {
212         const backend_info_t *info = be_get_info(node);
213         assert(!is_Proj(node));
214         assert(pos >= 0 && pos < ARR_LEN(info->out_infos));
215         return &info->out_infos[pos];
216 }
217
218
219 const arch_register_t *arch_get_irn_register(const ir_node *node)
220 {
221         const reg_out_info_t *out = get_out_info(node);
222         return out->reg;
223 }
224
225 const arch_register_t *arch_irn_get_register(const ir_node *node, int pos)
226 {
227         const reg_out_info_t *out = get_out_info_n(node, pos);
228         return out->reg;
229 }
230
231 void arch_irn_set_register(ir_node *node, int pos, const arch_register_t *reg)
232 {
233         reg_out_info_t *out = get_out_info_n(node, pos);
234         out->reg            = reg;
235 }
236
237 void arch_set_irn_register(ir_node *node, const arch_register_t *reg)
238 {
239         reg_out_info_t *out = get_out_info(node);
240         out->reg = reg;
241 }
242
243 arch_irn_class_t arch_irn_classify(const ir_node *node)
244 {
245         const arch_irn_ops_t *ops = get_irn_ops(node);
246         return ops->classify(node);
247 }
248
249 arch_irn_flags_t arch_irn_get_flags(const ir_node *node)
250 {
251         backend_info_t *info = be_get_info(node);
252         return info->flags;
253 }
254
255 void arch_irn_set_flags(ir_node *node, arch_irn_flags_t flags)
256 {
257         backend_info_t *info = be_get_info(node);
258         info->flags = flags;
259 }
260
261 void arch_irn_add_flags(ir_node *node, arch_irn_flags_t flags)
262 {
263         backend_info_t *info = be_get_info(node);
264         info->flags |= flags;
265 }
266
267 void arch_dump_register_req(FILE *F, const arch_register_req_t *req,
268                             const ir_node *node)
269 {
270         if (req == NULL || req->type == arch_register_req_type_none) {
271                 fprintf(F, "n/a");
272                 return;
273         }
274
275         fprintf(F, "%s", req->cls->name);
276
277         if(arch_register_req_is(req, limited)) {
278                 unsigned n_regs = req->cls->n_regs;
279                 unsigned i;
280
281                 fprintf(F, " limited to");
282                 for(i = 0; i < n_regs; ++i) {
283                         if(rbitset_is_set(req->limited, i)) {
284                                 const arch_register_t *reg = &req->cls->regs[i];
285                                 fprintf(F, " %s", reg->name);
286                         }
287                 }
288         }
289
290         if(arch_register_req_is(req, should_be_same)) {
291                 const unsigned other = req->other_same;
292                 int i;
293
294                 fprintf(F, " same as");
295                 for (i = 0; 1U << i <= other; ++i) {
296                         if (other & (1U << i)) {
297                                 ir_fprintf(F, " %+F", get_irn_n(skip_Proj_const(node), i));
298                         }
299                 }
300         }
301
302         if (arch_register_req_is(req, must_be_different)) {
303                 const unsigned other = req->other_different;
304                 int i;
305
306                 fprintf(F, " different from");
307                 for (i = 0; 1U << i <= other; ++i) {
308                         if (other & (1U << i)) {
309                                 ir_fprintf(F, " %+F", get_irn_n(skip_Proj_const(node), i));
310                         }
311                 }
312         }
313
314         if (arch_register_req_is(req, ignore)) {
315                 fprintf(F, " ignore");
316         }
317         if (arch_register_req_is(req, produces_sp)) {
318                 fprintf(F, " produces_sp");
319         }
320 }
321
322 static const arch_register_req_t no_requirement = {
323         arch_register_req_type_none,
324         NULL,
325         NULL,
326         0,
327         0
328 };
329 const arch_register_req_t *arch_no_register_req = &no_requirement;