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