17b529640ae148a735e379e8ce73456f9c678a2c
[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 {
54         arch_env_t *arch_env = isa_if->begin_codegeneration();
55         return arch_env;
56 }
57
58 /**
59  * Get the isa responsible for a node.
60  * @param irn The node to get the responsible isa for.
61  * @return The irn operations given by the responsible isa.
62  */
63 static const arch_irn_ops_t *get_irn_ops(const ir_node *irn)
64 {
65         if (is_Proj(irn)) {
66                 irn = get_Proj_pred(irn);
67                 assert(!is_Proj(irn));
68         }
69
70         ir_op                *ops    = get_irn_op(irn);
71         const arch_irn_ops_t *be_ops = get_op_ops(ops)->be_ops;
72
73         return be_ops;
74 }
75
76 void arch_set_frame_offset(ir_node *irn, int offset)
77 {
78         const arch_irn_ops_t *ops = get_irn_ops(irn);
79         ops->set_frame_offset(irn, offset);
80 }
81
82 ir_entity *arch_get_frame_entity(const ir_node *irn)
83 {
84         const arch_irn_ops_t *ops = get_irn_ops(irn);
85         return ops->get_frame_entity(irn);
86 }
87
88 int arch_get_sp_bias(ir_node *irn)
89 {
90         const arch_irn_ops_t *ops = get_irn_ops(irn);
91         return ops->get_sp_bias(irn);
92 }
93
94 int arch_possible_memory_operand(const ir_node *irn, unsigned int i)
95 {
96         const arch_irn_ops_t *ops = get_irn_ops(irn);
97
98         if (ops->possible_memory_operand) {
99                 return ops->possible_memory_operand(irn, i);
100         } else {
101                 return 0;
102         }
103 }
104
105 void arch_perform_memory_operand(ir_node *irn, ir_node *spill, unsigned int i)
106 {
107         const arch_irn_ops_t *ops = get_irn_ops(irn);
108
109         if (ops->perform_memory_operand) {
110                 ops->perform_memory_operand(irn, spill, i);
111         } else {
112                 return;
113         }
114 }
115
116 int arch_get_op_estimated_cost(const ir_node *irn)
117 {
118         const arch_irn_ops_t *ops = get_irn_ops(irn);
119
120         if (ops->get_op_estimated_cost) {
121                 return ops->get_op_estimated_cost(irn);
122         } else {
123                 return 1;
124         }
125 }
126
127 static reg_out_info_t *get_out_info_n(const ir_node *node, unsigned pos)
128 {
129         const backend_info_t *info = be_get_info(node);
130         assert(pos < (unsigned)ARR_LEN(info->out_infos));
131         return &info->out_infos[pos];
132 }
133
134
135 const arch_register_t *arch_get_irn_register(const ir_node *node)
136 {
137         const reg_out_info_t *out = get_out_info(node);
138         return out->reg;
139 }
140
141 const arch_register_t *arch_get_irn_register_out(const ir_node *node,
142                                                  unsigned pos)
143 {
144         const reg_out_info_t *out = get_out_info_n(node, pos);
145         return out->reg;
146 }
147
148 const arch_register_t *arch_get_irn_register_in(const ir_node *node, int pos)
149 {
150         ir_node *op = get_irn_n(node, pos);
151         return arch_get_irn_register(op);
152 }
153
154 void arch_set_irn_register_out(ir_node *node, unsigned pos,
155                                const arch_register_t *reg)
156 {
157         reg_out_info_t *out = get_out_info_n(node, pos);
158         out->reg            = reg;
159 }
160
161 void arch_set_irn_register(ir_node *node, const arch_register_t *reg)
162 {
163         reg_out_info_t *out = get_out_info(node);
164         out->reg = reg;
165 }
166
167 void arch_set_irn_flags(ir_node *node, arch_irn_flags_t flags)
168 {
169         backend_info_t *const info = be_get_info(node);
170         info->flags = flags;
171 }
172
173 void arch_add_irn_flags(ir_node *node, arch_irn_flags_t flags)
174 {
175         backend_info_t *const info = be_get_info(node);
176         info->flags |= flags;
177 }
178
179 bool arch_reg_is_allocatable(const arch_register_req_t *req,
180                              const arch_register_t *reg)
181 {
182         assert(req->type != arch_register_req_type_none);
183         if (req->cls != reg->reg_class)
184                 return false;
185         if (reg->type & arch_register_type_virtual)
186                 return true;
187         if (arch_register_req_is(req, limited))
188                 return rbitset_is_set(req->limited, reg->index);
189         return true;
190 }
191
192 /**
193  * Print information about a register requirement in human readable form
194  * @param F   output stream/file
195  * @param req The requirements structure to format.
196  */
197 static void arch_dump_register_req(FILE *F, const arch_register_req_t *req,
198                             const ir_node *node)
199 {
200         if (req == NULL || req->type == arch_register_req_type_none) {
201                 fprintf(F, "n/a");
202                 return;
203         }
204
205         fprintf(F, "%s", req->cls->name);
206
207         if (arch_register_req_is(req, limited)) {
208                 unsigned n_regs = req->cls->n_regs;
209                 unsigned i;
210
211                 fprintf(F, " limited to");
212                 for (i = 0; i < n_regs; ++i) {
213                         if (rbitset_is_set(req->limited, i)) {
214                                 const arch_register_t *reg = &req->cls->regs[i];
215                                 fprintf(F, " %s", reg->name);
216                         }
217                 }
218         }
219
220         if (arch_register_req_is(req, should_be_same)) {
221                 const unsigned other = req->other_same;
222                 int i;
223
224                 fprintf(F, " same as");
225                 for (i = 0; 1U << i <= other; ++i) {
226                         if (other & (1U << i)) {
227                                 ir_fprintf(F, " #%d (%+F)", i, get_irn_n(skip_Proj_const(node), i));
228                         }
229                 }
230         }
231
232         if (arch_register_req_is(req, must_be_different)) {
233                 const unsigned other = req->other_different;
234                 int i;
235
236                 fprintf(F, " different from");
237                 for (i = 0; 1U << i <= other; ++i) {
238                         if (other & (1U << i)) {
239                                 ir_fprintf(F, " #%d (%+F)", i, get_irn_n(skip_Proj_const(node), i));
240                         }
241                 }
242         }
243
244         if (req->width != 1) {
245                 fprintf(F, " width:%d", req->width);
246         }
247         if (arch_register_req_is(req, aligned)) {
248                 fprintf(F, " aligned");
249         }
250         if (arch_register_req_is(req, ignore)) {
251                 fprintf(F, " ignore");
252         }
253         if (arch_register_req_is(req, produces_sp)) {
254                 fprintf(F, " produces_sp");
255         }
256 }
257
258 void arch_dump_reqs_and_registers(FILE *F, const ir_node *node)
259 {
260         backend_info_t *info = be_get_info(node);
261         /* don't fail on invalid graphs */
262         if (info == NULL || info->in_reqs == NULL || info->out_infos == NULL) {
263                 fprintf(F, "invalid register requirements!!!\n");
264                 return;
265         }
266
267         int n_ins  = get_irn_arity(node);
268         for (int i = 0; i < n_ins; ++i) {
269                 const arch_register_req_t *req = arch_get_irn_register_req_in(node, i);
270                 fprintf(F, "inreq #%d = ", i);
271                 arch_dump_register_req(F, req, node);
272                 fputs("\n", F);
273         }
274         be_foreach_out(node, 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                 const arch_register_t *reg = arch_get_irn_register_out(node, o);
279                 fprintf(F, " [%s]\n", reg != NULL ? reg->name : "n/a");
280         }
281
282         fprintf(F, "flags =");
283         arch_irn_flags_t flags = arch_get_irn_flags(node);
284         if (flags == arch_irn_flags_none) {
285                 fprintf(F, " none");
286         } else {
287                 if (flags & arch_irn_flags_dont_spill) {
288                         fprintf(F, " unspillable");
289                 }
290                 if (flags & arch_irn_flags_rematerializable) {
291                         fprintf(F, " remat");
292                 }
293                 if (flags & arch_irn_flags_modify_flags) {
294                         fprintf(F, " modify_flags");
295                 }
296                 if (flags & arch_irn_flags_simple_jump) {
297                         fprintf(F, " simple_jump");
298                 }
299                 if (flags & arch_irn_flags_not_scheduled) {
300                         fprintf(F, " not_scheduled");
301                 }
302         }
303         fprintf(F, " (0x%x)\n", (unsigned)flags);
304 }