2 * Copyright (C) 1995-2008 University of Karlsruhe. All right reserved.
4 * This file is part of libFirm.
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.
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.
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
22 * @brief Processor architecture specification.
23 * @author Sebastian Hack
38 #include "raw_bitset.h"
42 static const arch_register_req_t no_requirement = {
43 arch_register_req_type_none,
50 const arch_register_req_t *arch_no_register_req = &no_requirement;
52 static reg_out_info_t dummy_info = {
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)
61 arch_env_t *arch_env = isa_if->begin_codegeneration(main_env);
62 arch_env->main_env = main_env;
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.
71 static const arch_irn_ops_t *get_irn_ops(const ir_node *irn)
74 const arch_irn_ops_t *be_ops;
77 irn = get_Proj_pred(irn);
78 assert(!is_Proj(irn));
81 ops = get_irn_op(irn);
82 be_ops = get_op_ops(ops)->be_ops;
87 void arch_set_frame_offset(ir_node *irn, int offset)
89 const arch_irn_ops_t *ops = get_irn_ops(irn);
90 ops->set_frame_offset(irn, offset);
93 ir_entity *arch_get_frame_entity(const ir_node *irn)
95 const arch_irn_ops_t *ops = get_irn_ops(irn);
96 return ops->get_frame_entity(irn);
99 int arch_get_sp_bias(ir_node *irn)
101 const arch_irn_ops_t *ops = get_irn_ops(irn);
102 return ops->get_sp_bias(irn);
105 arch_inverse_t *arch_get_inverse(const ir_node *irn, int i, arch_inverse_t *inverse, struct obstack *obstack)
107 const arch_irn_ops_t *ops = get_irn_ops(irn);
109 if (ops->get_inverse) {
110 return ops->get_inverse(irn, i, inverse, obstack);
116 int arch_possible_memory_operand(const ir_node *irn, unsigned int i)
118 const arch_irn_ops_t *ops = get_irn_ops(irn);
120 if (ops->possible_memory_operand) {
121 return ops->possible_memory_operand(irn, i);
127 void arch_perform_memory_operand(ir_node *irn, ir_node *spill, unsigned int i)
129 const arch_irn_ops_t *ops = get_irn_ops(irn);
131 if (ops->perform_memory_operand) {
132 ops->perform_memory_operand(irn, spill, i);
138 int arch_get_op_estimated_cost(const ir_node *irn)
140 const arch_irn_ops_t *ops = get_irn_ops(irn);
142 if (ops->get_op_estimated_cost) {
143 return ops->get_op_estimated_cost(irn);
149 static reg_out_info_t *get_out_info(const ir_node *node)
152 const backend_info_t *info;
153 assert(get_irn_mode(node) != mode_T);
155 pos = get_Proj_proj(node);
156 node = get_Proj_pred(node);
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
164 if (info->out_infos == NULL)
166 assert(pos < ARR_LEN(info->out_infos));
167 return &info->out_infos[pos];
170 static reg_out_info_t *get_out_info_n(const ir_node *node, int pos)
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];
179 const arch_register_t *arch_get_irn_register(const ir_node *node)
181 const reg_out_info_t *out = get_out_info(node);
185 const arch_register_t *arch_get_irn_register_out(const ir_node *node, int pos)
187 const reg_out_info_t *out = get_out_info_n(node, pos);
191 const arch_register_t *arch_get_irn_register_in(const ir_node *node, int pos)
193 ir_node *op = get_irn_n(node, pos);
194 return arch_get_irn_register(op);
197 void arch_set_irn_register_out(ir_node *node, int pos,
198 const arch_register_t *reg)
200 reg_out_info_t *out = get_out_info_n(node, pos);
204 void arch_set_irn_register(ir_node *node, const arch_register_t *reg)
206 reg_out_info_t *out = get_out_info(node);
210 const arch_register_req_t *arch_get_irn_register_req(const ir_node *node)
212 reg_out_info_t *out = get_out_info(node);
216 arch_irn_flags_t arch_get_irn_flags(const ir_node *node)
218 backend_info_t *info;
220 return arch_irn_flags_not_scheduled;
222 info = be_get_info(node);
226 void arch_set_irn_flags(ir_node *node, arch_irn_flags_t flags)
228 backend_info_t *info;
230 /* setting flags is only supported for instructions currently.
231 * (mainly because we found no use for it yet and saved the space for
232 * be_infos for them */
233 assert(!is_Proj(node));
234 info = be_get_info(node);
238 void arch_add_irn_flags(ir_node *node, arch_irn_flags_t flags)
240 backend_info_t *info;
241 assert(!is_Proj(node));
242 info = be_get_info(node);
243 info->flags |= flags;
246 bool arch_reg_is_allocatable(const arch_register_req_t *req,
247 const arch_register_t *reg)
249 if (reg->type & arch_register_type_joker)
251 if (req->type == arch_register_req_type_none)
253 if (req->type & arch_register_req_type_limited) {
254 if (arch_register_get_class(reg) != req->cls)
256 return rbitset_is_set(req->limited, arch_register_get_index(reg));
258 return req->cls == arch_register_get_class(reg);
261 void arch_dump_register_req(FILE *F, const arch_register_req_t *req,
264 if (req == NULL || req->type == arch_register_req_type_none) {
269 fprintf(F, "%s", req->cls->name);
271 if (arch_register_req_is(req, limited)) {
272 unsigned n_regs = req->cls->n_regs;
275 fprintf(F, " limited to");
276 for (i = 0; i < n_regs; ++i) {
277 if (rbitset_is_set(req->limited, i)) {
278 const arch_register_t *reg = &req->cls->regs[i];
279 fprintf(F, " %s", reg->name);
284 if (arch_register_req_is(req, should_be_same)) {
285 const unsigned other = req->other_same;
288 fprintf(F, " same as");
289 for (i = 0; 1U << i <= other; ++i) {
290 if (other & (1U << i)) {
291 ir_fprintf(F, " %+F", get_irn_n(skip_Proj_const(node), i));
296 if (arch_register_req_is(req, must_be_different)) {
297 const unsigned other = req->other_different;
300 fprintf(F, " different from");
301 for (i = 0; 1U << i <= other; ++i) {
302 if (other & (1U << i)) {
303 ir_fprintf(F, " %+F", get_irn_n(skip_Proj_const(node), i));
308 if (req->width != 1) {
309 fprintf(F, " width:%d", req->width);
311 if (arch_register_req_is(req, aligned)) {
312 fprintf(F, " aligned");
314 if (arch_register_req_is(req, ignore)) {
315 fprintf(F, " ignore");
317 if (arch_register_req_is(req, produces_sp)) {
318 fprintf(F, " produces_sp");
322 void arch_dump_reqs_and_registers(FILE *F, const ir_node *node)
324 int n_ins = get_irn_arity(node);
325 int n_outs = arch_get_irn_n_outs(node);
326 arch_irn_flags_t flags = arch_get_irn_flags(node);
329 for (i = 0; i < n_ins; ++i) {
330 const arch_register_req_t *req = arch_get_irn_register_req_in(node, i);
331 fprintf(F, "inreq #%d = ", i);
332 arch_dump_register_req(F, req, node);
335 for (i = 0; i < n_outs; ++i) {
336 const arch_register_req_t *req = arch_get_irn_register_req_out(node, i);
337 fprintf(F, "outreq #%d = ", i);
338 arch_dump_register_req(F, req, node);
341 for (i = 0; i < n_outs; ++i) {
342 const arch_register_t *reg = arch_get_irn_register_out(node, i);
343 const arch_register_req_t *req = arch_get_irn_register_req_out(node, i);
344 if (req->cls == NULL)
346 fprintf(F, "reg #%d = %s\n", i, reg != NULL ? reg->name : "n/a");
349 fprintf(F, "flags =");
350 if (flags == arch_irn_flags_none) {
353 if (flags & arch_irn_flags_dont_spill) {
354 fprintf(F, " unspillable");
356 if (flags & arch_irn_flags_rematerializable) {
357 fprintf(F, " remat");
359 if (flags & arch_irn_flags_modify_flags) {
360 fprintf(F, " modify_flags");
362 if (flags & arch_irn_flags_simple_jump) {
363 fprintf(F, " simple_jump");
365 if (flags & arch_irn_flags_not_scheduled) {
366 fprintf(F, " not_scheduled");
369 fprintf(F, " (%d)\n", (int)flags);