Remove the unused function arch_is_register_operand().
[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  * @version     $Id$
25  */
26 #include "config.h"
27
28 #include <string.h>
29
30 #include "bearch_t.h"
31 #include "benode_t.h"
32 #include "ircons_t.h"
33 #include "irnode_t.h"
34
35 #include "bitset.h"
36 #include "pset.h"
37 #include "raw_bitset.h"
38
39 #include "irprintf.h"
40
41 /* Initialize the architecture environment struct. */
42 arch_env_t *arch_env_init(const arch_isa_if_t *isa_if, FILE *file_handle, be_main_env_t *main_env)
43 {
44         arch_env_t *arch_env = isa_if->init(file_handle);
45         arch_env->main_env   = main_env;
46         return arch_env;
47 }
48
49 /**
50  * Put all registers in a class into a bitset.
51  * @param cls The class.
52  * @param bs The bitset.
53  * @return The number of registers in the class.
54  */
55 static int arch_register_class_put(const arch_register_class_t *cls, bitset_t *bs)
56 {
57         int i, n = cls->n_regs;
58         for (i = n - 1; i >= 0; --i)
59                 bitset_set(bs, i);
60         return n;
61 }
62
63 /**
64  * Get the isa responsible for a node.
65  * @param irn The node to get the responsible isa for.
66  * @return The irn operations given by the responsible isa.
67  */
68 static INLINE const arch_irn_ops_t *get_irn_ops(const ir_node *irn)
69 {
70         const ir_op          *ops;
71         const arch_irn_ops_t *be_ops;
72
73         if (is_Proj(irn)) {
74                 irn = get_Proj_pred(irn);
75                 assert(!is_Proj(irn));
76         }
77
78         ops    = get_irn_op(irn);
79         be_ops = get_op_ops(ops)->be_ops;
80
81         assert(be_ops);
82         return be_ops;
83 }
84
85 const arch_register_req_t *arch_get_register_req(const ir_node *irn, int pos)
86 {
87         const arch_irn_ops_t *ops = get_irn_ops(irn);
88         return ops->get_irn_reg_req(irn, pos);
89 }
90
91 void arch_set_frame_offset(ir_node *irn, int offset)
92 {
93         const arch_irn_ops_t *ops = get_irn_ops(irn);
94         ops->set_frame_offset(irn, offset);
95 }
96
97 ir_entity *arch_get_frame_entity(const ir_node *irn)
98 {
99         const arch_irn_ops_t *ops = get_irn_ops(irn);
100         return ops->get_frame_entity(irn);
101 }
102
103 void arch_set_frame_entity(ir_node *irn, ir_entity *ent)
104 {
105         const arch_irn_ops_t *ops = get_irn_ops(irn);
106         ops->set_frame_entity(irn, ent);
107 }
108
109 int arch_get_sp_bias(ir_node *irn)
110 {
111         const arch_irn_ops_t *ops = get_irn_ops(irn);
112         return ops->get_sp_bias(irn);
113 }
114
115 arch_inverse_t *arch_get_inverse(const ir_node *irn, int i, arch_inverse_t *inverse, struct obstack *obstack)
116 {
117         const arch_irn_ops_t *ops = get_irn_ops(irn);
118
119         if(ops->get_inverse) {
120                 return ops->get_inverse(irn, i, inverse, obstack);
121         } else {
122                 return NULL;
123         }
124 }
125
126 int arch_possible_memory_operand(const ir_node *irn, unsigned int i)
127 {
128         const arch_irn_ops_t *ops = get_irn_ops(irn);
129
130         if(ops->possible_memory_operand) {
131                 return ops->possible_memory_operand(irn, i);
132         } else {
133                 return 0;
134         }
135 }
136
137 void arch_perform_memory_operand(ir_node *irn, ir_node *spill, unsigned int i)
138 {
139         const arch_irn_ops_t *ops = get_irn_ops(irn);
140
141         if(ops->perform_memory_operand) {
142                 ops->perform_memory_operand(irn, spill, i);
143         } else {
144                 return;
145         }
146 }
147
148 int arch_get_op_estimated_cost(const ir_node *irn)
149 {
150         const arch_irn_ops_t *ops = get_irn_ops(irn);
151
152         if(ops->get_op_estimated_cost) {
153                 return ops->get_op_estimated_cost(irn);
154         } else {
155                 return 1;
156         }
157 }
158
159 int arch_get_allocatable_regs(const ir_node *irn, int pos, bitset_t *bs)
160 {
161         const arch_irn_ops_t *ops = get_irn_ops(irn);
162         const arch_register_req_t *req = ops->get_irn_reg_req(irn, pos);
163
164         if(req->type == arch_register_req_type_none) {
165                 bitset_clear_all(bs);
166                 return 0;
167         }
168
169         if(arch_register_req_is(req, limited)) {
170                 rbitset_copy_to_bitset(req->limited, bs);
171                 return bitset_popcnt(bs);
172         }
173
174         arch_register_class_put(req->cls, bs);
175         return req->cls->n_regs;
176 }
177
178 void arch_put_non_ignore_regs(const arch_register_class_t *cls, bitset_t *bs)
179 {
180         unsigned i;
181
182         for(i = 0; i < cls->n_regs; ++i) {
183                 if(!arch_register_type_is(&cls->regs[i], ignore))
184                         bitset_set(bs, i);
185         }
186 }
187
188 int arch_reg_is_allocatable(const ir_node *irn, int pos, const arch_register_t *reg)
189 {
190         const arch_register_req_t *req = arch_get_register_req(irn, pos);
191
192         if(req->type == arch_register_req_type_none)
193                 return 0;
194
195         if(arch_register_req_is(req, limited)) {
196                 assert(arch_register_get_class(reg) == req->cls);
197                 return rbitset_is_set(req->limited, arch_register_get_index(reg));
198         }
199
200         return req->cls == reg->reg_class;
201 }
202
203 const arch_register_class_t *arch_get_irn_reg_class(const ir_node *irn, int pos)
204 {
205         const arch_irn_ops_t *ops = get_irn_ops(irn);
206         const arch_register_req_t *req = ops->get_irn_reg_req(irn, pos);
207
208         assert(req->type != arch_register_req_type_none || req->cls == NULL);
209
210         return req->cls;
211 }
212
213 const arch_register_t *arch_get_irn_register(const ir_node *irn)
214 {
215         const arch_irn_ops_t *ops = get_irn_ops(irn);
216         return ops->get_irn_reg(irn);
217 }
218
219 void arch_set_irn_register(ir_node *irn, const arch_register_t *reg)
220 {
221         const arch_irn_ops_t *ops = get_irn_ops(irn);
222         ops->set_irn_reg(irn, reg);
223 }
224
225 arch_irn_class_t arch_irn_classify(const ir_node *irn)
226 {
227         const arch_irn_ops_t *ops = get_irn_ops(irn);
228         return ops->classify(irn);
229 }
230
231 arch_irn_flags_t arch_irn_get_flags(const ir_node *irn)
232 {
233         const arch_irn_ops_t *ops = get_irn_ops(irn);
234         return ops->get_flags(irn);
235 }
236
237 extern char *arch_register_req_format(char *buf, size_t len,
238                                       const arch_register_req_t *req,
239                                       const ir_node *node)
240 {
241         char tmp[128];
242         snprintf(buf, len, "class: %s", req->cls->name);
243
244         if(arch_register_req_is(req, limited)) {
245                 unsigned n_regs = req->cls->n_regs;
246                 unsigned i;
247
248                 strncat(buf, " limited:", len);
249                 for(i = 0; i < n_regs; ++i) {
250                         if(rbitset_is_set(req->limited, i)) {
251                                 const arch_register_t *reg = &req->cls->regs[i];
252                                 strncat(buf, " ", len);
253                                 strncat(buf, reg->name, len);
254                         }
255                 }
256         }
257
258         if(arch_register_req_is(req, should_be_same)) {
259                 const unsigned other = req->other_same;
260                 int i;
261
262                 ir_snprintf(tmp, sizeof(tmp), " same to:");
263                 for (i = 0; 1U << i <= other; ++i) {
264                         if (other & (1U << i)) {
265                                 ir_snprintf(tmp, sizeof(tmp), " %+F", get_irn_n(skip_Proj_const(node), i));
266                                 strncat(buf, tmp, len);
267                         }
268                 }
269         }
270
271         if (arch_register_req_is(req, must_be_different)) {
272                 const unsigned other = req->other_different;
273                 int i;
274
275                 ir_snprintf(tmp, sizeof(tmp), " different from:");
276                 for (i = 0; 1U << i <= other; ++i) {
277                         if (other & (1U << i)) {
278                                 ir_snprintf(tmp, sizeof(tmp), " %+F", get_irn_n(skip_Proj_const(node), i));
279                                 strncat(buf, tmp, len);
280                         }
281                 }
282         }
283
284         return buf;
285 }
286
287 static const arch_register_req_t no_requirement = {
288         arch_register_req_type_none,
289         NULL,
290         NULL,
291         0,
292         0
293 };
294 const arch_register_req_t *arch_no_register_req = &no_requirement;