bearch.c 9.73 KB
Newer Older
Christian Würdig's avatar
Christian Würdig committed
1
/*
Michael Beck's avatar
Michael Beck committed
2
 * Copyright (C) 1995-2008 University of Karlsruhe.  All right reserved.
Christian Würdig's avatar
Christian Würdig committed
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
 *
 * This file is part of libFirm.
 *
 * This file may be distributed and/or modified under the terms of the
 * GNU General Public License version 2 as published by the Free Software
 * Foundation and appearing in the file LICENSE.GPL included in the
 * packaging of this file.
 *
 * Licensees holding valid libFirm Professional Edition licenses may use
 * this file in accordance with the libFirm Commercial License.
 * Agreement provided with the Software.
 *
 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
 * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE.
 */

20
/**
Christian Würdig's avatar
Christian Würdig committed
21
22
23
24
 * @file
 * @brief       Processor architecture specification.
 * @author      Sebastian Hack
 * @version     $Id$
25
 */
Michael Beck's avatar
Michael Beck committed
26
#ifdef HAVE_CONFIG_H
27
#include "config.h"
Michael Beck's avatar
Michael Beck committed
28
29
#endif

30
31
#include <string.h>

32
#include "bearch_t.h"
Sebastian Hack's avatar
Sebastian Hack committed
33
#include "ircons_t.h"
Daniel Grund's avatar
Daniel Grund committed
34
#include "irnode_t.h"
35
#include "xmalloc.h"
36

Sebastian Hack's avatar
Sebastian Hack committed
37
#include "bitset.h"
Sebastian Hack's avatar
Sebastian Hack committed
38
#include "pset.h"
Matthias Braun's avatar
Matthias Braun committed
39
#include "raw_bitset.h"
40

Sebastian Hack's avatar
Sebastian Hack committed
41
42
#include "irprintf.h"

43
/* Initialize the architecture environment struct. */
Christian Würdig's avatar
Christian Würdig committed
44
arch_env_t *arch_env_init(arch_env_t *env, const arch_isa_if_t *isa_if, FILE *file_handle, be_main_env_t *main_env)
45
{
Matthias Braun's avatar
Matthias Braun committed
46
47
48
49
	memset(env, 0, sizeof(*env));
	env->isa                  = isa_if->init(file_handle);
	env->isa->main_env        = main_env;
	return env;
Sebastian Hack's avatar
Sebastian Hack committed
50
51
}

52
arch_env_t *arch_env_push_irn_handler(arch_env_t *env,
53
                                      arch_get_irn_ops_t *handler)
Sebastian Hack's avatar
Sebastian Hack committed
54
{
Matthias Braun's avatar
Matthias Braun committed
55
56
57
	assert(env->handlers_tos < ARCH_MAX_HANDLERS);
	env->handlers[env->handlers_tos++] = handler;
	return env;
58
59
}

60
arch_get_irn_ops_t *arch_env_pop_irn_handler(arch_env_t *env)
61
{
Matthias Braun's avatar
Matthias Braun committed
62
63
   	assert(env->handlers_tos > 0 && env->handlers_tos <= ARCH_MAX_HANDLERS);
	return env->handlers[--env->handlers_tos];
64
65
}

Sebastian Hack's avatar
Sebastian Hack committed
66
static const arch_irn_ops_t *fallback_irn_ops = NULL;
Sebastian Hack's avatar
Sebastian Hack committed
67

Sebastian Hack's avatar
Sebastian Hack committed
68
int arch_register_class_put(const arch_register_class_t *cls, bitset_t *bs)
69
{
Matthias Braun's avatar
Matthias Braun committed
70
71
72
73
74
	if(bs) {
		int i, n;
		for(i = 0, n = cls->n_regs; i < n; ++i)
			bitset_set(bs, i);
	}
75

Matthias Braun's avatar
Matthias Braun committed
76
	return cls->n_regs;
77
78
}

Sebastian Hack's avatar
Sebastian Hack committed
79
/**
Sebastian Hack's avatar
Sebastian Hack committed
80
81
82
83
 * Get the isa responsible for a node.
 * @param env The arch environment with the isa stack.
 * @param irn The node to get the responsible isa for.
 * @return The irn operations given by the responsible isa.
Sebastian Hack's avatar
Sebastian Hack committed
84
 */
Sebastian Hack's avatar
Sebastian Hack committed
85
86
static INLINE const arch_irn_ops_t *
get_irn_ops(const arch_env_t *env, const ir_node *irn)
87
{
88
#if 1
Matthias Braun's avatar
Matthias Braun committed
89
	int i;
90

Matthias Braun's avatar
Matthias Braun committed
91
	for(i = env->handlers_tos - 1; i >= 0; --i) {
92
93
		arch_get_irn_ops_t *get_irn_ops = env->handlers[i];
		const arch_irn_ops_t *ops = get_irn_ops(irn);
Sebastian Hack's avatar
Sebastian Hack committed
94

Matthias Braun's avatar
Matthias Braun committed
95
96
97
		if(ops)
			return ops;
	}
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#else
	if (is_Phi(irn) && !mode_is_datab(get_irn_mode(irn))) {
		const phi_handler_t *h;
		return &h->irn_ops;
	}
	if (is_Proj(irn)) {
		irn = get_Proj_pred(irn);
		if (is_Proj(irn)) {
			assert(get_irn_mode(irn) == mode_T);
			irn = get_Proj_pred(irn);
		}
	}
	if (is_be_node(irn))
		return &be_node_irn_ops;
Sebastian Hack's avatar
Sebastian Hack committed
112

113
#endif
Matthias Braun's avatar
Matthias Braun committed
114
	return fallback_irn_ops;
115
116
}

117
118
119
120
const arch_irn_ops_t *arch_get_irn_ops(const arch_env_t *env, const ir_node *irn) {
	return get_irn_ops(env, irn);
}

Sebastian Hack's avatar
Sebastian Hack committed
121
const arch_register_req_t *arch_get_register_req(const arch_env_t *env,
Matthias Braun's avatar
Matthias Braun committed
122
                                                 const ir_node *irn, int pos)
Sebastian Hack's avatar
Sebastian Hack committed
123
{
Matthias Braun's avatar
Matthias Braun committed
124
125
	const arch_irn_ops_t *ops = get_irn_ops(env, irn);
	return ops->impl->get_irn_reg_req(ops, irn, pos);
Sebastian Hack's avatar
Sebastian Hack committed
126
127
}

128
void arch_set_frame_offset(const arch_env_t *env, ir_node *irn, int offset)
Sebastian Hack's avatar
Sebastian Hack committed
129
{
Matthias Braun's avatar
Matthias Braun committed
130
131
	const arch_irn_ops_t *ops = get_irn_ops(env, irn);
	ops->impl->set_frame_offset(ops, irn, offset);
Sebastian Hack's avatar
Sebastian Hack committed
132
133
}

134
ir_entity *arch_get_frame_entity(const arch_env_t *env, const ir_node *irn)
135
{
Matthias Braun's avatar
Matthias Braun committed
136
137
	const arch_irn_ops_t *ops = get_irn_ops(env, irn);
	return ops->impl->get_frame_entity(ops, irn);
138
139
}

140
void arch_set_frame_entity(const arch_env_t *env, ir_node *irn, ir_entity *ent)
141
142
143
144
145
{
	const arch_irn_ops_t *ops = get_irn_ops(env, irn);
	ops->impl->set_frame_entity(ops, irn, ent);
}

146
147
148
149
150
151
int arch_get_sp_bias(const arch_env_t *env, ir_node *irn)
{
	const arch_irn_ops_t *ops = get_irn_ops(env, irn);
	return ops->impl->get_sp_bias(ops, irn);
}

152
153
arch_inverse_t *arch_get_inverse(const arch_env_t *env, const ir_node *irn, int i, arch_inverse_t *inverse, struct obstack *obstack)
{
Matthias Braun's avatar
Matthias Braun committed
154
155
156
157
158
159
160
	const arch_irn_ops_t *ops = get_irn_ops(env, irn);

	if(ops->impl->get_inverse) {
		return ops->impl->get_inverse(ops, irn, i, inverse, obstack);
	} else {
		return NULL;
	}
161
162
}

163
164
int arch_possible_memory_operand(const arch_env_t *env, const ir_node *irn, unsigned int i) {
	const arch_irn_ops_t *ops = get_irn_ops(env, irn);
Matthias Braun's avatar
Matthias Braun committed
165

166
167
168
169
170
171
172
	if(ops->impl->possible_memory_operand) {
		return ops->impl->possible_memory_operand(ops, irn, i);
	} else {
		return 0;
	}
}

Christian Würdig's avatar
Christian Würdig committed
173
void arch_perform_memory_operand(const arch_env_t *env, ir_node *irn, ir_node *spill, unsigned int i) {
174
	const arch_irn_ops_t *ops = get_irn_ops(env, irn);
Matthias Braun's avatar
Matthias Braun committed
175

176
	if(ops->impl->perform_memory_operand) {
177
		ops->impl->perform_memory_operand(ops, irn, spill, i);
178
179
180
181
182
	} else {
		return;
	}
}

183
184
int arch_get_op_estimated_cost(const arch_env_t *env, const ir_node *irn)
{
Matthias Braun's avatar
Matthias Braun committed
185
186
187
188
189
190
191
	const arch_irn_ops_t *ops = get_irn_ops(env, irn);

	if(ops->impl->get_op_estimated_cost) {
		return ops->impl->get_op_estimated_cost(ops, irn);
	} else {
		return 1;
	}
192
193
194
195
}

int arch_is_possible_memory_operand(const arch_env_t *env, const ir_node *irn, int i)
{
Matthias Braun's avatar
Matthias Braun committed
196
197
198
199
200
201
202
   	const arch_irn_ops_t *ops = get_irn_ops(env, irn);

	if(ops->impl->possible_memory_operand) {
	   	return ops->impl->possible_memory_operand(ops, irn, i);
	} else {
		return 0;
	}
203
}
Sebastian Hack's avatar
Sebastian Hack committed
204

Sebastian Hack's avatar
Sebastian Hack committed
205
int arch_get_allocatable_regs(const arch_env_t *env, const ir_node *irn, int pos, bitset_t *bs)
Sebastian Hack's avatar
Sebastian Hack committed
206
{
Matthias Braun's avatar
Matthias Braun committed
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
	const arch_irn_ops_t *ops = get_irn_ops(env, irn);
	const arch_register_req_t *req = ops->impl->get_irn_reg_req(ops, irn, pos);

	if(req->type == arch_register_req_type_none) {
		bitset_clear_all(bs);
		return 0;
	}

	if(arch_register_req_is(req, limited)) {
		rbitset_copy_to_bitset(req->limited, bs);
		return bitset_popcnt(bs);
	}

	arch_register_class_put(req->cls, bs);
	return req->cls->n_regs;
Sebastian Hack's avatar
Sebastian Hack committed
222
223
}

Matthias Braun's avatar
Matthias Braun committed
224
225
void arch_put_non_ignore_regs(const arch_env_t *env,
                              const arch_register_class_t *cls, bitset_t *bs)
Sebastian Hack's avatar
Sebastian Hack committed
226
{
227
	unsigned i;
Matthias Braun's avatar
Matthias Braun committed
228
	(void) env;
Sebastian Hack's avatar
Sebastian Hack committed
229
230
231
232
233
234
235

	for(i = 0; i < cls->n_regs; ++i) {
		if(!arch_register_type_is(&cls->regs[i], ignore))
			bitset_set(bs, i);
	}
}

Matthias Braun's avatar
Matthias Braun committed
236
237
int arch_count_non_ignore_regs(const arch_env_t *env,
                               const arch_register_class_t *cls)
238
{
239
	unsigned i;
240
	int result = 0;
Matthias Braun's avatar
Matthias Braun committed
241
	(void) env;
242
243
244
245
246
247
248
249
250

	for(i = 0; i < cls->n_regs; ++i) {
		if(!arch_register_type_is(&cls->regs[i], ignore))
			result++;
	}

	return result;
}

251
252
253
int arch_is_register_operand(const arch_env_t *env,
    const ir_node *irn, int pos)
{
Sebastian Hack's avatar
Sebastian Hack committed
254
	const arch_irn_ops_t *ops = get_irn_ops(env, irn);
Matthias Braun's avatar
Matthias Braun committed
255
256
	const arch_register_req_t *req = ops->impl->get_irn_reg_req(ops, irn, pos);

Sebastian Hack's avatar
Sebastian Hack committed
257
	return req != NULL;
258
259
}

Sebastian Hack's avatar
Sebastian Hack committed
260
261
int arch_reg_is_allocatable(const arch_env_t *env, const ir_node *irn,
    int pos, const arch_register_t *reg)
Sebastian Hack's avatar
Sebastian Hack committed
262
{
Matthias Braun's avatar
Matthias Braun committed
263
	const arch_register_req_t *req;
264

Matthias Braun's avatar
Matthias Braun committed
265
	req = arch_get_register_req(env, irn, pos);
Sebastian Hack's avatar
Sebastian Hack committed
266

Matthias Braun's avatar
Matthias Braun committed
267
	if(req->type == arch_register_req_type_none)
Sebastian Hack's avatar
Sebastian Hack committed
268
269
		return 0;

Matthias Braun's avatar
Matthias Braun committed
270
271
272
	if(arch_register_req_is(req, limited)) {
		assert(arch_register_get_class(reg) == req->cls);
		return rbitset_is_set(req->limited, arch_register_get_index(reg));
273
274
	}

Matthias Braun's avatar
Matthias Braun committed
275
	return req->cls == reg->reg_class;
Sebastian Hack's avatar
Sebastian Hack committed
276
277
}

Sebastian Hack's avatar
Sebastian Hack committed
278
279
const arch_register_class_t *
arch_get_irn_reg_class(const arch_env_t *env, const ir_node *irn, int pos)
Sebastian Hack's avatar
Sebastian Hack committed
280
{
Matthias Braun's avatar
Matthias Braun committed
281
282
283
284
285
286
	const arch_irn_ops_t *ops = get_irn_ops(env, irn);
	const arch_register_req_t *req = ops->impl->get_irn_reg_req(ops, irn, pos);

	assert(req->type != arch_register_req_type_none || req->cls == NULL);

	return req->cls;
Sebastian Hack's avatar
Sebastian Hack committed
287
288
}

Sebastian Hack's avatar
Sebastian Hack committed
289
extern const arch_register_t *
Sebastian Hack's avatar
Sebastian Hack committed
290
arch_get_irn_register(const arch_env_t *env, const ir_node *irn)
291
{
Matthias Braun's avatar
Matthias Braun committed
292
293
	const arch_irn_ops_t *ops = get_irn_ops(env, irn);
	return ops->impl->get_irn_reg(ops, irn);
294
}
295

Sebastian Hack's avatar
Sebastian Hack committed
296
extern void arch_set_irn_register(const arch_env_t *env,
Sebastian Hack's avatar
Sebastian Hack committed
297
    ir_node *irn, const arch_register_t *reg)
298
{
Matthias Braun's avatar
Matthias Braun committed
299
300
	const arch_irn_ops_t *ops = get_irn_ops(env, irn);
	ops->impl->set_irn_reg(ops, irn, reg);
301
}
302
303
304

extern arch_irn_class_t arch_irn_classify(const arch_env_t *env, const ir_node *irn)
{
Matthias Braun's avatar
Matthias Braun committed
305
306
	const arch_irn_ops_t *ops = get_irn_ops(env, irn);
	return ops->impl->classify(ops, irn);
307
}
Sebastian Hack's avatar
Sebastian Hack committed
308
309
310

extern arch_irn_flags_t arch_irn_get_flags(const arch_env_t *env, const ir_node *irn)
{
Matthias Braun's avatar
Matthias Braun committed
311
312
	const arch_irn_ops_t *ops = get_irn_ops(env, irn);
	return ops->impl->get_flags(ops, irn);
Sebastian Hack's avatar
Sebastian Hack committed
313
}
Sebastian Hack's avatar
Sebastian Hack committed
314
315
316
317
318
319
320
321

extern const char *arch_irn_flag_str(arch_irn_flags_t fl)
{
	switch(fl) {
#define XXX(x) case arch_irn_flags_ ## x: return #x;
		XXX(dont_spill);
		XXX(ignore);
		XXX(rematerializable);
Sebastian Hack's avatar
Sebastian Hack committed
322
		XXX(modify_sp);
Matthias Braun's avatar
Matthias Braun committed
323
		XXX(modify_flags);
Sebastian Hack's avatar
Sebastian Hack committed
324
325
326
327
328
329
		XXX(none);
#undef XXX
	}
	return "n/a";
}

Matthias Braun's avatar
Matthias Braun committed
330
331
332
extern char *arch_register_req_format(char *buf, size_t len,
                                      const arch_register_req_t *req,
                                      const ir_node *node)
Sebastian Hack's avatar
Sebastian Hack committed
333
334
335
336
337
{
	char tmp[128];
	snprintf(buf, len, "class: %s", req->cls->name);

	if(arch_register_req_is(req, limited)) {
Matthias Braun's avatar
Matthias Braun committed
338
339
340
		unsigned n_regs = req->cls->n_regs;
		unsigned i;

Sebastian Hack's avatar
Sebastian Hack committed
341
		strncat(buf, " limited:", len);
Matthias Braun's avatar
Matthias Braun committed
342
343
344
345
346
347
		for(i = 0; i < n_regs; ++i) {
			if(rbitset_is_set(req->limited, i)) {
				const arch_register_t *reg = &req->cls->regs[i];
				strncat(buf, " ", len);
				strncat(buf, reg->name, len);
			}
Sebastian Hack's avatar
Sebastian Hack committed
348
349
350
351
		}
	}

	if(arch_register_req_is(req, should_be_same)) {
352
353
354
355
356
357
358
359
360
		const unsigned other = req->other_same;
		int i;

		ir_snprintf(tmp, sizeof(tmp), " same to:");
		for (i = 0; 1U << i <= other; ++i) {
			if (other & (1U << i)) {
				ir_snprintf(tmp, sizeof(tmp), " %+F", get_irn_n(skip_Proj_const(node), i));
				strncat(buf, tmp, len);
			}
361
		}
Sebastian Hack's avatar
Sebastian Hack committed
362
363
364
	}

	if(arch_register_req_is(req, should_be_different)) {
365
366
367
368
369
370
371
372
373
374
		const unsigned other = req->other_different;
		int i;

		ir_snprintf(tmp, sizeof(tmp), " different from:");
		for (i = 0; 1U << i <= other; ++i) {
			if (other & (1U << i)) {
				ir_snprintf(tmp, sizeof(tmp), " %+F", get_irn_n(skip_Proj_const(node), i));
				strncat(buf, tmp, len);
			}
		}
Sebastian Hack's avatar
Sebastian Hack committed
375
376
377
378
	}

	return buf;
}
Matthias Braun's avatar
Matthias Braun committed
379
380
381
382
383

static const arch_register_req_t no_requirement = {
	arch_register_req_type_none,
	NULL,
	NULL,
384
385
	0,
	0
Matthias Braun's avatar
Matthias Braun committed
386
387
};
const arch_register_req_t *arch_no_register_req = &no_requirement;