bearch.c 8.93 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
 * @file
 * @brief       Processor architecture specification.
 * @author      Sebastian Hack
24
 */
25
#include "config.h"
Michael Beck's avatar
Michael Beck committed
26

27
28
#include <string.h>

29
#include "bearch.h"
30
#include "benode.h"
31
#include "beinfo.h"
Sebastian Hack's avatar
Sebastian Hack committed
32
#include "ircons_t.h"
Daniel Grund's avatar
Daniel Grund committed
33
#include "irnode_t.h"
34
#include "irop_t.h"
35

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

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

42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
static const arch_register_req_t no_requirement = {
	arch_register_req_type_none,
	NULL,
	NULL,
	0,
	0,
	0
};
const arch_register_req_t *arch_no_register_req = &no_requirement;

static reg_out_info_t dummy_info = {
	NULL,
	&no_requirement
};

57
/* Initialize the architecture environment struct. */
58
arch_env_t *arch_env_init(const arch_isa_if_t *isa_if, be_main_env_t *main_env)
59
{
60
	arch_env_t *arch_env = isa_if->init(main_env);
61
62
	arch_env->main_env   = main_env;
	return arch_env;
Sebastian Hack's avatar
Sebastian Hack committed
63
64
65
}

/**
Sebastian Hack's avatar
Sebastian Hack committed
66
67
68
 * Get the isa responsible for a node.
 * @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
69
 */
70
static const arch_irn_ops_t *get_irn_ops(const ir_node *irn)
71
{
Matthias Braun's avatar
Matthias Braun committed
72
73
74
75
76
77
78
	const ir_op          *ops;
	const arch_irn_ops_t *be_ops;

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

Matthias Braun's avatar
Matthias Braun committed
80
81
	ops    = get_irn_op(irn);
	be_ops = get_op_ops(ops)->be_ops;
Sebastian Hack's avatar
Sebastian Hack committed
82

Matthias Braun's avatar
Matthias Braun committed
83
	return be_ops;
84
85
}

86
void arch_set_frame_offset(ir_node *irn, int offset)
Sebastian Hack's avatar
Sebastian Hack committed
87
{
88
	const arch_irn_ops_t *ops = get_irn_ops(irn);
89
	ops->set_frame_offset(irn, offset);
Sebastian Hack's avatar
Sebastian Hack committed
90
91
}

92
ir_entity *arch_get_frame_entity(const ir_node *irn)
93
{
94
	const arch_irn_ops_t *ops = get_irn_ops(irn);
95
	return ops->get_frame_entity(irn);
96
97
}

98
int arch_get_sp_bias(ir_node *irn)
99
{
100
	const arch_irn_ops_t *ops = get_irn_ops(irn);
101
	return ops->get_sp_bias(irn);
102
103
}

104
arch_inverse_t *arch_get_inverse(const ir_node *irn, int i, arch_inverse_t *inverse, struct obstack *obstack)
105
{
106
	const arch_irn_ops_t *ops = get_irn_ops(irn);
Matthias Braun's avatar
Matthias Braun committed
107

108
	if (ops->get_inverse) {
109
		return ops->get_inverse(irn, i, inverse, obstack);
Matthias Braun's avatar
Matthias Braun committed
110
111
112
	} else {
		return NULL;
	}
113
114
}

115
116
int arch_possible_memory_operand(const ir_node *irn, unsigned int i)
{
117
	const arch_irn_ops_t *ops = get_irn_ops(irn);
Matthias Braun's avatar
Matthias Braun committed
118

119
	if (ops->possible_memory_operand) {
120
		return ops->possible_memory_operand(irn, i);
121
122
123
124
125
	} else {
		return 0;
	}
}

126
127
void arch_perform_memory_operand(ir_node *irn, ir_node *spill, unsigned int i)
{
128
	const arch_irn_ops_t *ops = get_irn_ops(irn);
Matthias Braun's avatar
Matthias Braun committed
129

130
	if (ops->perform_memory_operand) {
131
		ops->perform_memory_operand(irn, spill, i);
132
133
134
135
136
	} else {
		return;
	}
}

137
int arch_get_op_estimated_cost(const ir_node *irn)
138
{
139
	const arch_irn_ops_t *ops = get_irn_ops(irn);
Matthias Braun's avatar
Matthias Braun committed
140

141
	if (ops->get_op_estimated_cost) {
142
		return ops->get_op_estimated_cost(irn);
Matthias Braun's avatar
Matthias Braun committed
143
144
145
	} else {
		return 1;
	}
146
147
}

148
static reg_out_info_t *get_out_info(const ir_node *node)
Sebastian Hack's avatar
Sebastian Hack committed
149
{
150
	size_t                pos = 0;
151
152
153
154
155
156
157
158
	const backend_info_t *info;
	assert(get_irn_mode(node) != mode_T);
	if (is_Proj(node)) {
		pos  = get_Proj_proj(node);
		node = get_Proj_pred(node);
	}

	info = be_get_info(node);
159
160
161
162
163
164
	/* We have a problem with the switch-node where there can be arbitrary
	 * Proj-numbers, so we can't easily allocate an array big-enough to hold
	 * all of them. So until we rewrite Switch-nodes we need this special case
	 */
	if (info->out_infos == NULL)
		return &dummy_info;
165
	assert(pos < ARR_LEN(info->out_infos));
166
	return &info->out_infos[pos];
167
}
168

169
static reg_out_info_t *get_out_info_n(const ir_node *node, int pos)
170
{
171
172
	const backend_info_t *info = be_get_info(node);
	assert(!is_Proj(node));
173
	assert(pos >= 0 && pos < (int)ARR_LEN(info->out_infos));
174
	return &info->out_infos[pos];
175
}
176

177
178

const arch_register_t *arch_get_irn_register(const ir_node *node)
179
{
180
181
	const reg_out_info_t *out = get_out_info(node);
	return out->reg;
182
}
Sebastian Hack's avatar
Sebastian Hack committed
183

184
const arch_register_t *arch_get_irn_register_out(const ir_node *node, int pos)
Sebastian Hack's avatar
Sebastian Hack committed
185
{
186
187
188
189
	const reg_out_info_t *out = get_out_info_n(node, pos);
	return out->reg;
}

190
191
192
193
194
195
196
197
const arch_register_t *arch_get_irn_register_in(const ir_node *node, int pos)
{
	ir_node *op = get_irn_n(node, pos);
	return arch_get_irn_register(op);
}

void arch_set_irn_register_out(ir_node *node, int pos,
                               const arch_register_t *reg)
198
199
200
201
202
203
204
205
206
207
208
{
	reg_out_info_t *out = get_out_info_n(node, pos);
	out->reg            = reg;
}

void arch_set_irn_register(ir_node *node, const arch_register_t *reg)
{
	reg_out_info_t *out = get_out_info(node);
	out->reg = reg;
}

209
210
211
212
213
214
215
const arch_register_req_t *arch_get_irn_register_req(const ir_node *node)
{
	reg_out_info_t *out = get_out_info(node);
	return out->req;
}

arch_irn_flags_t arch_get_irn_flags(const ir_node *node)
216
{
217
218
219
220
221
	backend_info_t *info;
	if (is_Proj(node))
		return arch_irn_flags_not_scheduled;

	info = be_get_info(node);
222
223
224
	return info->flags;
}

225
void arch_set_irn_flags(ir_node *node, arch_irn_flags_t flags)
226
{
227
228
229
230
231
232
233
	backend_info_t *info;

	/* setting flags is only supported for instructions currently.
	 * (mainly because we found no use for it yet and saved the space for
	 * be_infos for them */
	assert(!is_Proj(node));
	info = be_get_info(node);
234
235
236
	info->flags = flags;
}

237
void arch_add_irn_flags(ir_node *node, arch_irn_flags_t flags)
238
{
239
240
241
	backend_info_t *info;
	assert(!is_Proj(node));
	info = be_get_info(node);
242
	info->flags |= flags;
Sebastian Hack's avatar
Sebastian Hack committed
243
}
Sebastian Hack's avatar
Sebastian Hack committed
244

245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
bool arch_reg_is_allocatable(const arch_register_req_t *req,
                             const arch_register_t *reg)
{
	if (reg->type & arch_register_type_joker)
		return true;
	if (req->type == arch_register_req_type_none)
		return false;
	if (req->type & arch_register_req_type_limited) {
		if (arch_register_get_class(reg) != req->cls)
			return false;
		return rbitset_is_set(req->limited, arch_register_get_index(reg));
	}
	return req->cls == arch_register_get_class(reg);
}

260
261
void arch_dump_register_req(FILE *F, const arch_register_req_t *req,
                            const ir_node *node)
Sebastian Hack's avatar
Sebastian Hack committed
262
{
263
264
265
266
267
268
	if (req == NULL || req->type == arch_register_req_type_none) {
		fprintf(F, "n/a");
		return;
	}

	fprintf(F, "%s", req->cls->name);
Sebastian Hack's avatar
Sebastian Hack committed
269

270
	if (arch_register_req_is(req, limited)) {
Matthias Braun's avatar
Matthias Braun committed
271
272
273
		unsigned n_regs = req->cls->n_regs;
		unsigned i;

274
		fprintf(F, " limited to");
275
276
		for (i = 0; i < n_regs; ++i) {
			if (rbitset_is_set(req->limited, i)) {
Matthias Braun's avatar
Matthias Braun committed
277
				const arch_register_t *reg = &req->cls->regs[i];
278
				fprintf(F, " %s", reg->name);
Matthias Braun's avatar
Matthias Braun committed
279
			}
Sebastian Hack's avatar
Sebastian Hack committed
280
281
282
		}
	}

283
	if (arch_register_req_is(req, should_be_same)) {
284
285
286
		const unsigned other = req->other_same;
		int i;

287
		fprintf(F, " same as");
288
289
		for (i = 0; 1U << i <= other; ++i) {
			if (other & (1U << i)) {
290
				ir_fprintf(F, " %+F", get_irn_n(skip_Proj_const(node), i));
291
			}
292
		}
Sebastian Hack's avatar
Sebastian Hack committed
293
294
	}

295
	if (arch_register_req_is(req, must_be_different)) {
296
297
298
		const unsigned other = req->other_different;
		int i;

299
		fprintf(F, " different from");
300
301
		for (i = 0; 1U << i <= other; ++i) {
			if (other & (1U << i)) {
302
				ir_fprintf(F, " %+F", get_irn_n(skip_Proj_const(node), i));
303
304
			}
		}
Sebastian Hack's avatar
Sebastian Hack committed
305
306
	}

307
	if (req->width != 1) {
Matthias Braun's avatar
Matthias Braun committed
308
		fprintf(F, " width:%d", req->width);
309
310
311
312
	}
	if (arch_register_req_is(req, aligned)) {
		fprintf(F, " aligned");
	}
313
	if (arch_register_req_is(req, ignore)) {
314
		fprintf(F, " ignore");
315
316
	}
	if (arch_register_req_is(req, produces_sp)) {
317
		fprintf(F, " produces_sp");
318
	}
Sebastian Hack's avatar
Sebastian Hack committed
319
}
Matthias Braun's avatar
Matthias Braun committed
320

321
322
323
void arch_dump_reqs_and_registers(FILE *F, const ir_node *node)
{
	int              n_ins  = get_irn_arity(node);
324
325
	int              n_outs = arch_get_irn_n_outs(node);
	arch_irn_flags_t flags  = arch_get_irn_flags(node);
326
327
328
	int              i;

	for (i = 0; i < n_ins; ++i) {
329
		const arch_register_req_t *req = arch_get_irn_register_req_in(node, i);
330
331
332
333
334
		fprintf(F, "inreq #%d = ", i);
		arch_dump_register_req(F, req, node);
		fputs("\n", F);
	}
	for (i = 0; i < n_outs; ++i) {
335
		const arch_register_req_t *req = arch_get_irn_register_req_out(node, i);
336
337
338
339
340
		fprintf(F, "outreq #%d = ", i);
		arch_dump_register_req(F, req, node);
		fputs("\n", F);
	}
	for (i = 0; i < n_outs; ++i) {
341
342
		const arch_register_t     *reg = arch_get_irn_register_out(node, i);
		const arch_register_req_t *req = arch_get_irn_register_req_out(node, i);
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
		if (req->cls == NULL)
			continue;
		fprintf(F, "reg #%d = %s\n", i, reg != NULL ? reg->name : "n/a");
	}

	fprintf(F, "flags =");
	if (flags == arch_irn_flags_none) {
		fprintf(F, " none");
	} else {
		if (flags & arch_irn_flags_dont_spill) {
			fprintf(F, " unspillable");
		}
		if (flags & arch_irn_flags_rematerializable) {
			fprintf(F, " remat");
		}
		if (flags & arch_irn_flags_modify_flags) {
			fprintf(F, " modify_flags");
		}
361
362
363
364
365
366
		if (flags & arch_irn_flags_simple_jump) {
			fprintf(F, " simple_jump");
		}
		if (flags & arch_irn_flags_not_scheduled) {
			fprintf(F, " not_scheduled");
		}
367
	}
Matthias Braun's avatar
Matthias Braun committed
368
	fprintf(F, " (%d)\n", (int)flags);
369
}