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