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