bearch.c 8.89 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"
Michael Beck's avatar
Michael Beck committed
33
#include "benode_t.h"
Sebastian Hack's avatar
Sebastian Hack committed
34
#include "ircons_t.h"
Daniel Grund's avatar
Daniel Grund committed
35
#include "irnode_t.h"
36
#include "xmalloc.h"
37

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

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

44
/* Initialize the architecture environment struct. */
45
arch_env_t *arch_env_init(const arch_isa_if_t *isa_if, FILE *file_handle, be_main_env_t *main_env)
46
{
47
48
49
	arch_env_t *arch_env = isa_if->init(file_handle);
	arch_env->main_env   = main_env;
	return arch_env;
Sebastian Hack's avatar
Sebastian Hack committed
50
51
}

52
53
54
55
56
57
58
/**
 * Put all registers in a class into a bitset.
 * @param cls The class.
 * @param bs The bitset. May be NULL.
 * @return The number of registers in the class.
 */
static int arch_register_class_put(const arch_register_class_t *cls, bitset_t *bs)
59
{
60
61
62
	int i, n;
	for(i = 0, n = cls->n_regs; i < n; ++i)
		bitset_set(bs, i);
Matthias Braun's avatar
Matthias Braun committed
63
	return cls->n_regs;
64
65
}

Sebastian Hack's avatar
Sebastian Hack committed
66
/**
Sebastian Hack's avatar
Sebastian Hack committed
67
68
69
70
 * 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
71
 */
Sebastian Hack's avatar
Sebastian Hack committed
72
73
static INLINE const arch_irn_ops_t *
get_irn_ops(const arch_env_t *env, const ir_node *irn)
74
{
Matthias Braun's avatar
Matthias Braun committed
75
76
77
78
79
80
81
82
	const ir_op          *ops;
	const arch_irn_ops_t *be_ops;
	(void) env;

	if (is_Proj(irn)) {
		irn = get_Proj_pred(irn);
		assert(!is_Proj(irn));
	}
83

Matthias Braun's avatar
Matthias Braun committed
84
85
	ops    = get_irn_op(irn);
	be_ops = get_op_ops(ops)->be_ops;
Sebastian Hack's avatar
Sebastian Hack committed
86

Matthias Braun's avatar
Matthias Braun committed
87
88
	assert(be_ops);
	return be_ops;
89
90
}

91
92
93
94
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
95
const arch_register_req_t *arch_get_register_req(const arch_env_t *env,
Matthias Braun's avatar
Matthias Braun committed
96
                                                 const ir_node *irn, int pos)
Sebastian Hack's avatar
Sebastian Hack committed
97
{
Matthias Braun's avatar
Matthias Braun committed
98
	const arch_irn_ops_t *ops = get_irn_ops(env, irn);
99
	return ops->get_irn_reg_req(irn, pos);
Sebastian Hack's avatar
Sebastian Hack committed
100
101
}

102
void arch_set_frame_offset(const arch_env_t *env, ir_node *irn, int offset)
Sebastian Hack's avatar
Sebastian Hack committed
103
{
Matthias Braun's avatar
Matthias Braun committed
104
	const arch_irn_ops_t *ops = get_irn_ops(env, irn);
105
	ops->set_frame_offset(irn, offset);
Sebastian Hack's avatar
Sebastian Hack committed
106
107
}

108
ir_entity *arch_get_frame_entity(const arch_env_t *env, const ir_node *irn)
109
{
Matthias Braun's avatar
Matthias Braun committed
110
	const arch_irn_ops_t *ops = get_irn_ops(env, irn);
111
	return ops->get_frame_entity(irn);
112
113
}

114
void arch_set_frame_entity(const arch_env_t *env, ir_node *irn, ir_entity *ent)
115
116
{
	const arch_irn_ops_t *ops = get_irn_ops(env, irn);
117
	ops->set_frame_entity(irn, ent);
118
119
}

120
121
122
int arch_get_sp_bias(const arch_env_t *env, ir_node *irn)
{
	const arch_irn_ops_t *ops = get_irn_ops(env, irn);
123
	return ops->get_sp_bias(irn);
124
125
}

126
127
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
128
129
	const arch_irn_ops_t *ops = get_irn_ops(env, irn);

Matthias Braun's avatar
Matthias Braun committed
130
	if(ops->get_inverse) {
131
		return ops->get_inverse(irn, i, inverse, obstack);
Matthias Braun's avatar
Matthias Braun committed
132
133
134
	} else {
		return NULL;
	}
135
136
}

137
138
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
139

Matthias Braun's avatar
Matthias Braun committed
140
	if(ops->possible_memory_operand) {
141
		return ops->possible_memory_operand(irn, i);
142
143
144
145
146
	} else {
		return 0;
	}
}

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

Matthias Braun's avatar
Matthias Braun committed
150
	if(ops->perform_memory_operand) {
151
		ops->perform_memory_operand(irn, spill, i);
152
153
154
155
156
	} else {
		return;
	}
}

157
158
int arch_get_op_estimated_cost(const arch_env_t *env, const ir_node *irn)
{
Matthias Braun's avatar
Matthias Braun committed
159
160
	const arch_irn_ops_t *ops = get_irn_ops(env, irn);

Matthias Braun's avatar
Matthias Braun committed
161
	if(ops->get_op_estimated_cost) {
162
		return ops->get_op_estimated_cost(irn);
Matthias Braun's avatar
Matthias Braun committed
163
164
165
	} else {
		return 1;
	}
166
167
168
169
}

int arch_is_possible_memory_operand(const arch_env_t *env, const ir_node *irn, int i)
{
Matthias Braun's avatar
Matthias Braun committed
170
171
   	const arch_irn_ops_t *ops = get_irn_ops(env, irn);

Matthias Braun's avatar
Matthias Braun committed
172
	if(ops->possible_memory_operand) {
173
	   	return ops->possible_memory_operand(irn, i);
Matthias Braun's avatar
Matthias Braun committed
174
175
176
	} else {
		return 0;
	}
177
}
Sebastian Hack's avatar
Sebastian Hack committed
178

Sebastian Hack's avatar
Sebastian Hack committed
179
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
180
{
Matthias Braun's avatar
Matthias Braun committed
181
	const arch_irn_ops_t *ops = get_irn_ops(env, irn);
182
	const arch_register_req_t *req = ops->get_irn_reg_req(irn, pos);
Matthias Braun's avatar
Matthias Braun committed
183
184
185
186
187
188
189
190
191
192
193
194
195

	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
196
197
}

Matthias Braun's avatar
Matthias Braun committed
198
199
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
200
{
201
	unsigned i;
Matthias Braun's avatar
Matthias Braun committed
202
	(void) env;
Sebastian Hack's avatar
Sebastian Hack committed
203
204
205
206
207
208
209

	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
210
211
int arch_count_non_ignore_regs(const arch_env_t *env,
                               const arch_register_class_t *cls)
212
{
213
	unsigned i;
214
	int result = 0;
Matthias Braun's avatar
Matthias Braun committed
215
	(void) env;
216
217
218
219
220
221
222
223
224

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

	return result;
}

225
226
227
int arch_is_register_operand(const arch_env_t *env,
    const ir_node *irn, int pos)
{
Sebastian Hack's avatar
Sebastian Hack committed
228
	const arch_irn_ops_t *ops = get_irn_ops(env, irn);
229
	const arch_register_req_t *req = ops->get_irn_reg_req(irn, pos);
Matthias Braun's avatar
Matthias Braun committed
230

Sebastian Hack's avatar
Sebastian Hack committed
231
	return req != NULL;
232
233
}

Sebastian Hack's avatar
Sebastian Hack committed
234
235
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
236
{
Matthias Braun's avatar
Matthias Braun committed
237
	const arch_register_req_t *req;
238

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

Matthias Braun's avatar
Matthias Braun committed
241
	if(req->type == arch_register_req_type_none)
Sebastian Hack's avatar
Sebastian Hack committed
242
243
		return 0;

Matthias Braun's avatar
Matthias Braun committed
244
245
246
	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));
247
248
	}

Matthias Braun's avatar
Matthias Braun committed
249
	return req->cls == reg->reg_class;
Sebastian Hack's avatar
Sebastian Hack committed
250
251
}

Sebastian Hack's avatar
Sebastian Hack committed
252
253
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
254
{
Matthias Braun's avatar
Matthias Braun committed
255
	const arch_irn_ops_t *ops = get_irn_ops(env, irn);
256
	const arch_register_req_t *req = ops->get_irn_reg_req(irn, pos);
Matthias Braun's avatar
Matthias Braun committed
257
258
259
260

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

	return req->cls;
Sebastian Hack's avatar
Sebastian Hack committed
261
262
}

Sebastian Hack's avatar
Sebastian Hack committed
263
extern const arch_register_t *
Sebastian Hack's avatar
Sebastian Hack committed
264
arch_get_irn_register(const arch_env_t *env, const ir_node *irn)
265
{
Matthias Braun's avatar
Matthias Braun committed
266
	const arch_irn_ops_t *ops = get_irn_ops(env, irn);
267
	return ops->get_irn_reg(irn);
268
}
269

Sebastian Hack's avatar
Sebastian Hack committed
270
extern void arch_set_irn_register(const arch_env_t *env,
Sebastian Hack's avatar
Sebastian Hack committed
271
    ir_node *irn, const arch_register_t *reg)
272
{
Matthias Braun's avatar
Matthias Braun committed
273
	const arch_irn_ops_t *ops = get_irn_ops(env, irn);
274
	ops->set_irn_reg(irn, reg);
275
}
276
277
278

extern arch_irn_class_t arch_irn_classify(const arch_env_t *env, const ir_node *irn)
{
Matthias Braun's avatar
Matthias Braun committed
279
	const arch_irn_ops_t *ops = get_irn_ops(env, irn);
280
	return ops->classify(irn);
281
}
Sebastian Hack's avatar
Sebastian Hack committed
282
283
284

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
285
	const arch_irn_ops_t *ops = get_irn_ops(env, irn);
286
	return ops->get_flags(irn);
Sebastian Hack's avatar
Sebastian Hack committed
287
}
Sebastian Hack's avatar
Sebastian Hack committed
288
289
290
291
292
293
294
295

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
296
		XXX(modify_sp);
Matthias Braun's avatar
Matthias Braun committed
297
		XXX(modify_flags);
Sebastian Hack's avatar
Sebastian Hack committed
298
299
300
301
302
303
		XXX(none);
#undef XXX
	}
	return "n/a";
}

Matthias Braun's avatar
Matthias Braun committed
304
305
306
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
307
308
309
310
311
{
	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
312
313
314
		unsigned n_regs = req->cls->n_regs;
		unsigned i;

Sebastian Hack's avatar
Sebastian Hack committed
315
		strncat(buf, " limited:", len);
Matthias Braun's avatar
Matthias Braun committed
316
317
318
319
320
321
		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
322
323
324
325
		}
	}

	if(arch_register_req_is(req, should_be_same)) {
326
327
328
329
330
331
332
333
334
		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);
			}
335
		}
Sebastian Hack's avatar
Sebastian Hack committed
336
337
	}

338
	if (arch_register_req_is(req, must_be_different)) {
339
340
341
342
343
344
345
346
347
348
		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
349
350
351
352
	}

	return buf;
}
Matthias Braun's avatar
Matthias Braun committed
353
354
355
356
357

static const arch_register_req_t no_requirement = {
	arch_register_req_type_none,
	NULL,
	NULL,
358
359
	0,
	0
Matthias Braun's avatar
Matthias Braun committed
360
361
};
const arch_register_req_t *arch_no_register_req = &no_requirement;