perform_memory operand now takes a spill instead of a reload
[libfirm] / ir / be / bearch.c
1 /**
2  * Processor architecture specification.
3  * @author Sebastian Hack
4  * @date 11.2.2005
5  *
6  * $Id$
7  */
8
9 #ifdef HAVE_CONFIG_H
10 #include "config.h"
11 #endif
12
13 #ifdef HAVE_ALLOCA_H
14 #include <alloca.h>
15 #endif
16
17 #ifdef HAVE_MALLOC_H
18 #include <malloc.h>
19 #endif
20
21 #include <string.h>
22
23 #include "bearch.h"
24 #include "ircons_t.h"
25 #include "irnode_t.h"
26
27 #include "bitset.h"
28 #include "pset.h"
29 #include "entity.h"
30
31 #include "irprintf.h"
32
33 /* Initialize the architecture environment struct. */
34 arch_env_t *arch_env_init(arch_env_t *env, const arch_isa_if_t *isa_if, FILE *file_handle)
35 {
36   memset(env, 0, sizeof(*env));
37   env->isa = isa_if->init(file_handle);
38   return env;
39 }
40
41 arch_env_t *arch_env_push_irn_handler(arch_env_t *env,
42     const arch_irn_handler_t *handler)
43 {
44   assert(env->handlers_tos < ARCH_MAX_HANDLERS);
45   env->handlers[env->handlers_tos++] = handler;
46   return env;
47 }
48
49 const arch_irn_handler_t *arch_env_pop_irn_handler(arch_env_t *env)
50 {
51   assert(env->handlers_tos > 0 && env->handlers_tos <= ARCH_MAX_HANDLERS);
52   return env->handlers[--env->handlers_tos];
53 }
54
55 static const arch_irn_ops_t *fallback_irn_ops = NULL;
56
57 int arch_register_class_put(const arch_register_class_t *cls, bitset_t *bs)
58 {
59   if(bs) {
60     int i, n;
61     for(i = 0, n = cls->n_regs; i < n; ++i)
62       bitset_set(bs, i);
63   }
64
65   return cls->n_regs;
66 }
67
68 /**
69  * Get the isa responsible for a node.
70  * @param env The arch environment with the isa stack.
71  * @param irn The node to get the responsible isa for.
72  * @return The irn operations given by the responsible isa.
73  */
74 static INLINE const arch_irn_ops_t *
75 get_irn_ops(const arch_env_t *env, const ir_node *irn)
76 {
77   int i;
78
79   for(i = env->handlers_tos - 1; i >= 0; --i) {
80     const arch_irn_handler_t *handler = env->handlers[i];
81     const arch_irn_ops_t *ops = handler->get_irn_ops(handler, irn);
82
83     if(ops)
84       return ops;
85   }
86
87   return fallback_irn_ops;
88 }
89
90 const arch_irn_ops_t *arch_get_irn_ops(const arch_env_t *env, const ir_node *irn) {
91         return get_irn_ops(env, irn);
92 }
93
94 const arch_register_req_t *arch_get_register_req(const arch_env_t *env,
95     arch_register_req_t *req, const ir_node *irn, int pos)
96 {
97   const arch_irn_ops_t *ops = get_irn_ops(env, irn);
98   req->type = arch_register_req_type_none;
99   return ops->impl->get_irn_reg_req(ops, req, irn, pos);
100 }
101
102 void arch_set_frame_offset(const arch_env_t *env, ir_node *irn, int offset)
103 {
104   const arch_irn_ops_t *ops = get_irn_ops(env, irn);
105   ops->impl->set_frame_offset(ops, irn, offset);
106 }
107
108 entity *arch_get_frame_entity(const arch_env_t *env, ir_node *irn)
109 {
110   const arch_irn_ops_t *ops = get_irn_ops(env, irn);
111   return ops->impl->get_frame_entity(ops, irn);
112 }
113
114 void arch_set_frame_entity(const arch_env_t *env, ir_node *irn, entity *ent)
115 {
116         const arch_irn_ops_t *ops = get_irn_ops(env, irn);
117         ops->impl->set_frame_entity(ops, irn, ent);
118 }
119
120 int arch_get_sp_bias(const arch_env_t *env, ir_node *irn)
121 {
122         const arch_irn_ops_t *ops = get_irn_ops(env, irn);
123         return ops->impl->get_sp_bias(ops, irn);
124 }
125
126 arch_inverse_t *arch_get_inverse(const arch_env_t *env, const ir_node *irn, int i, arch_inverse_t *inverse, struct obstack *obstack)
127 {
128   const arch_irn_ops_t *ops = get_irn_ops(env, irn);
129   if(ops->impl->get_inverse) {
130     return ops->impl->get_inverse(ops, irn, i, inverse, obstack);
131   } else {
132     return NULL;
133   }
134 }
135
136 int arch_possible_memory_operand(const arch_env_t *env, const ir_node *irn, unsigned int i) {
137         const arch_irn_ops_t *ops = get_irn_ops(env, irn);
138         if(ops->impl->possible_memory_operand) {
139                 return ops->impl->possible_memory_operand(ops, irn, i);
140         } else {
141                 return 0;
142         }
143 }
144
145 extern void arch_perform_memory_operand(const arch_env_t *env, ir_node *irn, ir_node *spill, unsigned int i) {
146         const arch_irn_ops_t *ops = get_irn_ops(env, irn);
147         if(ops->impl->perform_memory_operand) {
148                 ops->impl->perform_memory_operand(ops, irn, spill, i);
149         } else {
150                 return;
151         }
152 }
153
154 int arch_get_op_estimated_cost(const arch_env_t *env, const ir_node *irn)
155 {
156   const arch_irn_ops_t *ops = get_irn_ops(env, irn);
157   if(ops->impl->get_op_estimated_cost) {
158     return ops->impl->get_op_estimated_cost(ops, irn);
159   } else {
160     return 1;
161   }
162 }
163
164 int arch_is_possible_memory_operand(const arch_env_t *env, const ir_node *irn, int i)
165 {
166   const arch_irn_ops_t *ops = get_irn_ops(env, irn);
167   if(ops->impl->possible_memory_operand) {
168     return ops->impl->possible_memory_operand(ops, irn, i);
169   } else {
170     return 0;
171   }
172 }
173
174 int arch_get_allocatable_regs(const arch_env_t *env, const ir_node *irn, int pos, bitset_t *bs)
175 {
176   arch_register_req_t local_req;
177   const arch_irn_ops_t *ops = get_irn_ops(env, irn);
178   const arch_register_req_t *req = ops->impl->get_irn_reg_req(ops, &local_req, irn, pos);
179
180   if(req->type == arch_register_req_type_none) {
181           bitset_clear_all(bs);
182           return 0;
183   }
184
185   if(arch_register_req_is(req, limited)) {
186           req->limited(req->limited_env, bs);
187           return bitset_popcnt(bs);
188   }
189
190   arch_register_class_put(req->cls, bs);
191   return req->cls->n_regs;
192 }
193
194 void arch_put_non_ignore_regs(const arch_env_t *env, const arch_register_class_t *cls, bitset_t *bs)
195 {
196         int i;
197
198         for(i = 0; i < cls->n_regs; ++i) {
199                 if(!arch_register_type_is(&cls->regs[i], ignore))
200                         bitset_set(bs, i);
201         }
202 }
203
204 int arch_count_non_ignore_regs(const arch_env_t *env, const arch_register_class_t *cls)
205 {
206         int i;
207         int result = 0;
208
209         for(i = 0; i < cls->n_regs; ++i) {
210                 if(!arch_register_type_is(&cls->regs[i], ignore))
211                         result++;
212         }
213
214         return result;
215 }
216
217 int arch_is_register_operand(const arch_env_t *env,
218     const ir_node *irn, int pos)
219 {
220         arch_register_req_t local_req;
221         const arch_irn_ops_t *ops = get_irn_ops(env, irn);
222         const arch_register_req_t *req = ops->impl->get_irn_reg_req(ops, &local_req, irn, pos);
223         return req != NULL;
224 }
225
226 int arch_reg_is_allocatable(const arch_env_t *env, const ir_node *irn,
227     int pos, const arch_register_t *reg)
228 {
229         arch_register_req_t req;
230
231         arch_get_register_req(env, &req, irn, pos);
232
233         if(req.type == arch_register_req_type_none)
234                 return 0;
235
236         if(arch_register_req_is(&req, limited)) {
237                 bitset_t *bs = bitset_alloca(req.cls->n_regs);
238                 req.limited(req.limited_env, bs);
239                 return bitset_is_set(bs, arch_register_get_index(reg));
240         }
241
242         return req.cls == reg->reg_class;
243 }
244
245 const arch_register_class_t *
246 arch_get_irn_reg_class(const arch_env_t *env, const ir_node *irn, int pos)
247 {
248   arch_register_req_t local_req;
249   const arch_irn_ops_t *ops = get_irn_ops(env, irn);
250   const arch_register_req_t *req = ops->impl->get_irn_reg_req(ops, &local_req, irn, pos);
251   return req ? req->cls : NULL;
252 }
253
254 extern const arch_register_t *
255 arch_get_irn_register(const arch_env_t *env, const ir_node *irn)
256 {
257   const arch_irn_ops_t *ops = get_irn_ops(env, irn);
258   return ops->impl->get_irn_reg(ops, irn);
259 }
260
261 extern void arch_set_irn_register(const arch_env_t *env,
262     ir_node *irn, const arch_register_t *reg)
263 {
264   const arch_irn_ops_t *ops = get_irn_ops(env, irn);
265   ops->impl->set_irn_reg(ops, irn, reg);
266 }
267
268 extern arch_irn_class_t arch_irn_classify(const arch_env_t *env, const ir_node *irn)
269 {
270   const arch_irn_ops_t *ops = get_irn_ops(env, irn);
271   return ops->impl->classify(ops, irn);
272 }
273
274 extern arch_irn_flags_t arch_irn_get_flags(const arch_env_t *env, const ir_node *irn)
275 {
276   const arch_irn_ops_t *ops = get_irn_ops(env, irn);
277   return ops->impl->get_flags(ops, irn);
278 }
279
280 extern const char *arch_irn_flag_str(arch_irn_flags_t fl)
281 {
282         switch(fl) {
283 #define XXX(x) case arch_irn_flags_ ## x: return #x;
284                 XXX(dont_spill);
285                 XXX(ignore);
286                 XXX(rematerializable);
287                 XXX(modify_sp);
288                 XXX(none);
289 #undef XXX
290         }
291         return "n/a";
292 }
293
294 extern char *arch_register_req_format(char *buf, size_t len, const arch_register_req_t *req)
295 {
296         char tmp[128];
297         snprintf(buf, len, "class: %s", req->cls->name);
298
299         if(arch_register_req_is(req, limited)) {
300                 bitset_pos_t elm;
301                 bitset_t *bs = bitset_alloca(req->cls->n_regs);
302                 req->limited(req->limited_env, bs);
303                 strncat(buf, " limited:", len);
304                 bitset_foreach(bs, elm) {
305                         strncat(buf, " ", len);
306                         strncat(buf, req->cls->regs[elm].name, len);
307                 }
308         }
309
310         if(arch_register_req_is(req, should_be_same)) {
311                 ir_snprintf(tmp, sizeof(tmp), " same to: %+F", req->other_different);
312                 strncat(buf, tmp, len);
313         }
314
315         if(arch_register_req_is(req, should_be_different)) {
316                 ir_snprintf(tmp, sizeof(tmp), " different to: %+F", req->other_different);
317                 strncat(buf, tmp, len);
318         }
319
320         return buf;
321 }