bearch: Remove unnecessary indirection to access arch_no_register_req.
[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  */
25 #include "config.h"
26
27 #include <string.h>
28
29 #include "bearch.h"
30 #include "benode.h"
31 #include "beinfo.h"
32 #include "ircons_t.h"
33 #include "irnode_t.h"
34 #include "irop_t.h"
35
36 #include "bitset.h"
37 #include "pset.h"
38 #include "raw_bitset.h"
39
40 #include "irprintf.h"
41
42 arch_register_req_t const arch_no_requirement = {
43         arch_register_req_type_none,
44         NULL,
45         NULL,
46         0,
47         0,
48         0
49 };
50
51 /* Initialize the architecture environment struct. */
52 arch_env_t *arch_env_begin_codegeneration(const arch_isa_if_t *isa_if,
53                                           be_main_env_t *main_env)
54 {
55         arch_env_t *arch_env = isa_if->begin_codegeneration(main_env);
56         arch_env->main_env   = main_env;
57         return arch_env;
58 }
59
60 /**
61  * Get the isa responsible for a node.
62  * @param irn The node to get the responsible isa for.
63  * @return The irn operations given by the responsible isa.
64  */
65 static const arch_irn_ops_t *get_irn_ops(const ir_node *irn)
66 {
67         if (is_Proj(irn)) {
68                 irn = get_Proj_pred(irn);
69                 assert(!is_Proj(irn));
70         }
71
72         ir_op                *ops    = get_irn_op(irn);
73         const arch_irn_ops_t *be_ops = get_op_ops(ops)->be_ops;
74
75         return be_ops;
76 }
77
78 void arch_set_frame_offset(ir_node *irn, int offset)
79 {
80         const arch_irn_ops_t *ops = get_irn_ops(irn);
81         ops->set_frame_offset(irn, offset);
82 }
83
84 ir_entity *arch_get_frame_entity(const ir_node *irn)
85 {
86         const arch_irn_ops_t *ops = get_irn_ops(irn);
87         return ops->get_frame_entity(irn);
88 }
89
90 int arch_get_sp_bias(ir_node *irn)
91 {
92         const arch_irn_ops_t *ops = get_irn_ops(irn);
93         return ops->get_sp_bias(irn);
94 }
95
96 arch_inverse_t *arch_get_inverse(const ir_node *irn, int i, arch_inverse_t *inverse, struct obstack *obstack)
97 {
98         const arch_irn_ops_t *ops = get_irn_ops(irn);
99
100         if (ops->get_inverse) {
101                 return ops->get_inverse(irn, i, inverse, obstack);
102         } else {
103                 return NULL;
104         }
105 }
106
107 int arch_possible_memory_operand(const ir_node *irn, unsigned int i)
108 {
109         const arch_irn_ops_t *ops = get_irn_ops(irn);
110
111         if (ops->possible_memory_operand) {
112                 return ops->possible_memory_operand(irn, i);
113         } else {
114                 return 0;
115         }
116 }
117
118 void arch_perform_memory_operand(ir_node *irn, ir_node *spill, unsigned int i)
119 {
120         const arch_irn_ops_t *ops = get_irn_ops(irn);
121
122         if (ops->perform_memory_operand) {
123                 ops->perform_memory_operand(irn, spill, i);
124         } else {
125                 return;
126         }
127 }
128
129 int arch_get_op_estimated_cost(const ir_node *irn)
130 {
131         const arch_irn_ops_t *ops = get_irn_ops(irn);
132
133         if (ops->get_op_estimated_cost) {
134                 return ops->get_op_estimated_cost(irn);
135         } else {
136                 return 1;
137         }
138 }
139
140 static reg_out_info_t *get_out_info(const ir_node *node)
141 {
142         size_t                pos = 0;
143         const backend_info_t *info;
144         assert(get_irn_mode(node) != mode_T);
145         if (is_Proj(node)) {
146                 pos  = get_Proj_proj(node);
147                 node = get_Proj_pred(node);
148         }
149
150         info = be_get_info(node);
151         assert(pos < ARR_LEN(info->out_infos));
152         return &info->out_infos[pos];
153 }
154
155 static reg_out_info_t *get_out_info_n(const ir_node *node, unsigned pos)
156 {
157         const backend_info_t *info = be_get_info(node);
158         assert(!is_Proj(node));
159         assert(pos < (unsigned)ARR_LEN(info->out_infos));
160         return &info->out_infos[pos];
161 }
162
163
164 const arch_register_t *arch_get_irn_register(const ir_node *node)
165 {
166         const reg_out_info_t *out = get_out_info(node);
167         return out->reg;
168 }
169
170 const arch_register_t *arch_get_irn_register_out(const ir_node *node,
171                                                  unsigned pos)
172 {
173         const reg_out_info_t *out = get_out_info_n(node, pos);
174         return out->reg;
175 }
176
177 const arch_register_t *arch_get_irn_register_in(const ir_node *node, int pos)
178 {
179         ir_node *op = get_irn_n(node, pos);
180         return arch_get_irn_register(op);
181 }
182
183 void arch_set_irn_register_out(ir_node *node, unsigned pos,
184                                const arch_register_t *reg)
185 {
186         reg_out_info_t *out = get_out_info_n(node, pos);
187         out->reg            = reg;
188 }
189
190 void arch_set_irn_register(ir_node *node, const arch_register_t *reg)
191 {
192         reg_out_info_t *out = get_out_info(node);
193         out->reg = reg;
194 }
195
196 const arch_register_req_t *arch_get_irn_register_req(const ir_node *node)
197 {
198         reg_out_info_t *out = get_out_info(node);
199         return out->req;
200 }
201
202 arch_irn_flags_t arch_get_irn_flags(const ir_node *node)
203 {
204         backend_info_t *info;
205         if (is_Proj(node))
206                 return arch_irn_flags_not_scheduled;
207
208         info = be_get_info(node);
209         return info->flags;
210 }
211
212 void arch_set_irn_flags(ir_node *node, arch_irn_flags_t flags)
213 {
214         backend_info_t *info;
215
216         /* setting flags is only supported for instructions currently.
217          * (mainly because we found no use for it yet and saved the space for
218          * be_infos for them */
219         assert(!is_Proj(node));
220         info = be_get_info(node);
221         info->flags = flags;
222 }
223
224 void arch_add_irn_flags(ir_node *node, arch_irn_flags_t flags)
225 {
226         backend_info_t *info;
227         assert(!is_Proj(node));
228         info = be_get_info(node);
229         info->flags |= flags;
230 }
231
232 bool arch_reg_is_allocatable(const arch_register_req_t *req,
233                              const arch_register_t *reg)
234 {
235         if (reg->type & arch_register_type_joker)
236                 return true;
237         if (req->type == arch_register_req_type_none)
238                 return false;
239         if (req->type & arch_register_req_type_limited) {
240                 if (reg->reg_class != req->cls)
241                         return false;
242                 return rbitset_is_set(req->limited, reg->index);
243         }
244         return req->cls == reg->reg_class;
245 }
246
247 void arch_dump_register_req(FILE *F, const arch_register_req_t *req,
248                             const ir_node *node)
249 {
250         if (req == NULL || req->type == arch_register_req_type_none) {
251                 fprintf(F, "n/a");
252                 return;
253         }
254
255         fprintf(F, "%s", req->cls->name);
256
257         if (arch_register_req_is(req, limited)) {
258                 unsigned n_regs = req->cls->n_regs;
259                 unsigned i;
260
261                 fprintf(F, " limited to");
262                 for (i = 0; i < n_regs; ++i) {
263                         if (rbitset_is_set(req->limited, i)) {
264                                 const arch_register_t *reg = &req->cls->regs[i];
265                                 fprintf(F, " %s", reg->name);
266                         }
267                 }
268         }
269
270         if (arch_register_req_is(req, should_be_same)) {
271                 const unsigned other = req->other_same;
272                 int i;
273
274                 fprintf(F, " same as");
275                 for (i = 0; 1U << i <= other; ++i) {
276                         if (other & (1U << i)) {
277                                 ir_fprintf(F, " %+F", get_irn_n(skip_Proj_const(node), i));
278                         }
279                 }
280         }
281
282         if (arch_register_req_is(req, must_be_different)) {
283                 const unsigned other = req->other_different;
284                 int i;
285
286                 fprintf(F, " different from");
287                 for (i = 0; 1U << i <= other; ++i) {
288                         if (other & (1U << i)) {
289                                 ir_fprintf(F, " %+F", get_irn_n(skip_Proj_const(node), i));
290                         }
291                 }
292         }
293
294         if (req->width != 1) {
295                 fprintf(F, " width:%d", req->width);
296         }
297         if (arch_register_req_is(req, aligned)) {
298                 fprintf(F, " aligned");
299         }
300         if (arch_register_req_is(req, ignore)) {
301                 fprintf(F, " ignore");
302         }
303         if (arch_register_req_is(req, produces_sp)) {
304                 fprintf(F, " produces_sp");
305         }
306 }
307
308 void arch_dump_reqs_and_registers(FILE *F, const ir_node *node)
309 {
310         int n_ins  = get_irn_arity(node);
311         for (int i = 0; i < n_ins; ++i) {
312                 const arch_register_req_t *req = arch_get_irn_register_req_in(node, i);
313                 fprintf(F, "inreq #%d = ", i);
314                 arch_dump_register_req(F, req, node);
315                 fputs("\n", F);
316         }
317         unsigned n_outs = arch_get_irn_n_outs(node);
318         for (unsigned o = 0; o < n_outs; ++o) {
319                 const arch_register_req_t *req = arch_get_irn_register_req_out(node, o);
320                 fprintf(F, "outreq #%u = ", o);
321                 arch_dump_register_req(F, req, node);
322                 fputs("\n", F);
323         }
324         for (unsigned o = 0; o < n_outs; ++o) {
325                 const arch_register_t     *reg = arch_get_irn_register_out(node, o);
326                 const arch_register_req_t *req = arch_get_irn_register_req_out(node, o);
327                 if (req->cls == NULL)
328                         continue;
329                 fprintf(F, "reg #%u = %s\n", o, reg != NULL ? reg->name : "n/a");
330         }
331
332         fprintf(F, "flags =");
333         arch_irn_flags_t flags = arch_get_irn_flags(node);
334         if (flags == arch_irn_flags_none) {
335                 fprintf(F, " none");
336         } else {
337                 if (flags & arch_irn_flags_dont_spill) {
338                         fprintf(F, " unspillable");
339                 }
340                 if (flags & arch_irn_flags_rematerializable) {
341                         fprintf(F, " remat");
342                 }
343                 if (flags & arch_irn_flags_modify_flags) {
344                         fprintf(F, " modify_flags");
345                 }
346                 if (flags & arch_irn_flags_simple_jump) {
347                         fprintf(F, " simple_jump");
348                 }
349                 if (flags & arch_irn_flags_not_scheduled) {
350                         fprintf(F, " not_scheduled");
351                 }
352         }
353         fprintf(F, " (0x%x)\n", (unsigned)flags);
354 }