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