expose some critical bearch functions for inlining
[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 int arch_possible_memory_operand(const ir_node *irn, unsigned int i)
97 {
98         const arch_irn_ops_t *ops = get_irn_ops(irn);
99
100         if (ops->possible_memory_operand) {
101                 return ops->possible_memory_operand(irn, i);
102         } else {
103                 return 0;
104         }
105 }
106
107 void arch_perform_memory_operand(ir_node *irn, ir_node *spill, unsigned int i)
108 {
109         const arch_irn_ops_t *ops = get_irn_ops(irn);
110
111         if (ops->perform_memory_operand) {
112                 ops->perform_memory_operand(irn, spill, i);
113         } else {
114                 return;
115         }
116 }
117
118 int arch_get_op_estimated_cost(const ir_node *irn)
119 {
120         const arch_irn_ops_t *ops = get_irn_ops(irn);
121
122         if (ops->get_op_estimated_cost) {
123                 return ops->get_op_estimated_cost(irn);
124         } else {
125                 return 1;
126         }
127 }
128
129 static reg_out_info_t *get_out_info_n(const ir_node *node, unsigned pos)
130 {
131         const backend_info_t *info = be_get_info(node);
132         assert(!is_Proj(node));
133         assert(pos < (unsigned)ARR_LEN(info->out_infos));
134         return &info->out_infos[pos];
135 }
136
137
138 const arch_register_t *arch_get_irn_register(const ir_node *node)
139 {
140         const reg_out_info_t *out = get_out_info(node);
141         return out->reg;
142 }
143
144 const arch_register_t *arch_get_irn_register_out(const ir_node *node,
145                                                  unsigned pos)
146 {
147         const reg_out_info_t *out = get_out_info_n(node, pos);
148         return out->reg;
149 }
150
151 const arch_register_t *arch_get_irn_register_in(const ir_node *node, int pos)
152 {
153         ir_node *op = get_irn_n(node, pos);
154         return arch_get_irn_register(op);
155 }
156
157 void arch_set_irn_register_out(ir_node *node, unsigned pos,
158                                const arch_register_t *reg)
159 {
160         reg_out_info_t *out = get_out_info_n(node, pos);
161         out->reg            = reg;
162 }
163
164 void arch_set_irn_register(ir_node *node, const arch_register_t *reg)
165 {
166         reg_out_info_t *out = get_out_info(node);
167         out->reg = reg;
168 }
169
170 void arch_set_irn_flags(ir_node *node, arch_irn_flags_t flags)
171 {
172         backend_info_t *info;
173
174         /* setting flags is only supported for instructions currently.
175          * (mainly because we found no use for it yet and saved the space for
176          * be_infos for them */
177         assert(!is_Proj(node));
178         info = be_get_info(node);
179         info->flags = flags;
180 }
181
182 void arch_add_irn_flags(ir_node *node, arch_irn_flags_t flags)
183 {
184         backend_info_t *info;
185         assert(!is_Proj(node));
186         info = be_get_info(node);
187         info->flags |= flags;
188 }
189
190 bool arch_reg_is_allocatable(const arch_register_req_t *req,
191                              const arch_register_t *reg)
192 {
193         assert(req->type != arch_register_req_type_none);
194         if (req->cls != reg->reg_class)
195                 return false;
196         if (reg->type & arch_register_type_virtual)
197                 return true;
198         if (req->type & arch_register_req_type_limited)
199                 return rbitset_is_set(req->limited, reg->index);
200         return true;
201 }
202
203 void arch_dump_register_req(FILE *F, const arch_register_req_t *req,
204                             const ir_node *node)
205 {
206         if (req == NULL || req->type == arch_register_req_type_none) {
207                 fprintf(F, "n/a");
208                 return;
209         }
210
211         fprintf(F, "%s", req->cls->name);
212
213         if (arch_register_req_is(req, limited)) {
214                 unsigned n_regs = req->cls->n_regs;
215                 unsigned i;
216
217                 fprintf(F, " limited to");
218                 for (i = 0; i < n_regs; ++i) {
219                         if (rbitset_is_set(req->limited, i)) {
220                                 const arch_register_t *reg = &req->cls->regs[i];
221                                 fprintf(F, " %s", reg->name);
222                         }
223                 }
224         }
225
226         if (arch_register_req_is(req, should_be_same)) {
227                 const unsigned other = req->other_same;
228                 int i;
229
230                 fprintf(F, " same as");
231                 for (i = 0; 1U << i <= other; ++i) {
232                         if (other & (1U << i)) {
233                                 ir_fprintf(F, " %+F", get_irn_n(skip_Proj_const(node), i));
234                         }
235                 }
236         }
237
238         if (arch_register_req_is(req, must_be_different)) {
239                 const unsigned other = req->other_different;
240                 int i;
241
242                 fprintf(F, " different from");
243                 for (i = 0; 1U << i <= other; ++i) {
244                         if (other & (1U << i)) {
245                                 ir_fprintf(F, " %+F", get_irn_n(skip_Proj_const(node), i));
246                         }
247                 }
248         }
249
250         if (req->width != 1) {
251                 fprintf(F, " width:%d", req->width);
252         }
253         if (arch_register_req_is(req, aligned)) {
254                 fprintf(F, " aligned");
255         }
256         if (arch_register_req_is(req, ignore)) {
257                 fprintf(F, " ignore");
258         }
259         if (arch_register_req_is(req, produces_sp)) {
260                 fprintf(F, " produces_sp");
261         }
262 }
263
264 void arch_dump_reqs_and_registers(FILE *F, const ir_node *node)
265 {
266         int n_ins  = get_irn_arity(node);
267         for (int i = 0; i < n_ins; ++i) {
268                 const arch_register_req_t *req = arch_get_irn_register_req_in(node, i);
269                 fprintf(F, "inreq #%d = ", i);
270                 arch_dump_register_req(F, req, node);
271                 fputs("\n", F);
272         }
273         unsigned n_outs = arch_get_irn_n_outs(node);
274         for (unsigned o = 0; o < n_outs; ++o) {
275                 const arch_register_req_t *req = arch_get_irn_register_req_out(node, o);
276                 fprintf(F, "outreq #%u = ", o);
277                 arch_dump_register_req(F, req, node);
278                 fputs("\n", F);
279         }
280         for (unsigned o = 0; o < n_outs; ++o) {
281                 const arch_register_t     *reg = arch_get_irn_register_out(node, o);
282                 const arch_register_req_t *req = arch_get_irn_register_req_out(node, o);
283                 if (req->cls == NULL)
284                         continue;
285                 fprintf(F, "reg #%u = %s\n", o, reg != NULL ? reg->name : "n/a");
286         }
287
288         fprintf(F, "flags =");
289         arch_irn_flags_t flags = arch_get_irn_flags(node);
290         if (flags == arch_irn_flags_none) {
291                 fprintf(F, " none");
292         } else {
293                 if (flags & arch_irn_flags_dont_spill) {
294                         fprintf(F, " unspillable");
295                 }
296                 if (flags & arch_irn_flags_rematerializable) {
297                         fprintf(F, " remat");
298                 }
299                 if (flags & arch_irn_flags_modify_flags) {
300                         fprintf(F, " modify_flags");
301                 }
302                 if (flags & arch_irn_flags_simple_jump) {
303                         fprintf(F, " simple_jump");
304                 }
305                 if (flags & arch_irn_flags_not_scheduled) {
306                         fprintf(F, " not_scheduled");
307                 }
308         }
309         fprintf(F, " (0x%x)\n", (unsigned)flags);
310 }