bearch.h 11.8 KB
Newer Older
Christian Würdig's avatar
Christian Würdig committed
1
2
/*
 * This file is part of libFirm.
3
 * Copyright (C) 2012 University of Karlsruhe.
Christian Würdig's avatar
Christian Würdig committed
4
5
 */

Christian Würdig's avatar
Christian Würdig committed
6
7
8
9
10
11
12
/**
 * @file
 * @brief       Processor architecture specification.
 * @author      Sebastian Hack
 */
#ifndef FIRM_BE_BEARCH_H
#define FIRM_BE_BEARCH_H
13

14
15
#include <stdbool.h>

16
#include "firm_types.h"
17
#include "raw_bitset.h"
18

19
20
21
#include "be_types.h"
#include "beinfo.h"
#include "be.h"
22

23
24
25
extern arch_register_req_t const arch_exec_requirement;
#define arch_exec_req (&arch_exec_requirement)

26
27
28
extern arch_register_req_t const arch_memory_requirement;
#define arch_memory_req (&arch_memory_requirement)

29
30
extern arch_register_req_t const arch_no_requirement;
#define arch_no_register_req (&arch_no_requirement)
Matthias Braun's avatar
Matthias Braun committed
31

Christoph Mallon's avatar
Christoph Mallon committed
32
33
34
35
36
37
38
static inline reg_out_info_t *get_out_info_n(ir_node const *const node, unsigned const pos)
{
	backend_info_t const *const info = be_get_info(node);
	assert(pos < ARR_LEN(info->out_infos));
	return &info->out_infos[pos];
}

Sebastian Hack's avatar
Sebastian Hack committed
39
/**
40
 * Get the register allocated for a value.
Sebastian Hack's avatar
Sebastian Hack committed
41
 */
42
const arch_register_t *arch_get_irn_register(const ir_node *irn);
43

Sebastian Hack's avatar
Sebastian Hack committed
44
/**
45
 * Assign register to a value
Sebastian Hack's avatar
Sebastian Hack committed
46
 */
47
void arch_set_irn_register(ir_node *irn, const arch_register_t *reg);
48

Sebastian Hack's avatar
Sebastian Hack committed
49
/**
50
 * Set the register for a certain output operand.
Sebastian Hack's avatar
Sebastian Hack committed
51
 */
52
void arch_set_irn_register_out(ir_node *irn, unsigned pos, const arch_register_t *r);
Sebastian Hack's avatar
Sebastian Hack committed
53

54
const arch_register_t *arch_get_irn_register_out(const ir_node *irn, unsigned pos);
55
const arch_register_t *arch_get_irn_register_in(const ir_node *irn, int pos);
56

Sebastian Hack's avatar
Sebastian Hack committed
57
/**
58
 * Get register constraints for an operand at position @p
Sebastian Hack's avatar
Sebastian Hack committed
59
 */
60
61
62
63
64
65
static inline const arch_register_req_t *arch_get_irn_register_req_in(
		const ir_node *node, int pos)
{
	const backend_info_t *info = be_get_info(node);
	return info->in_reqs[pos];
}
Sebastian Hack's avatar
Sebastian Hack committed
66
67

/**
68
 * Get register constraint for a produced result (the @p pos result)
Sebastian Hack's avatar
Sebastian Hack committed
69
 */
70
static inline const arch_register_req_t *arch_get_irn_register_req_out(
71
		const ir_node *node, unsigned pos)
72
{
Christoph Mallon's avatar
Christoph Mallon committed
73
74
	reg_out_info_t const *const out = get_out_info_n(node, pos);
	return out->req;
75
}
Sebastian Hack's avatar
Sebastian Hack committed
76

77
static inline void arch_set_irn_register_req_out(ir_node *node, unsigned pos,
78
79
		const arch_register_req_t *req)
{
Christoph Mallon's avatar
Christoph Mallon committed
80
81
	reg_out_info_t *const out = get_out_info_n(node, pos);
	out->req = req;
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
}

static inline void arch_set_irn_register_reqs_in(ir_node *node,
		const arch_register_req_t **reqs)
{
	backend_info_t *info = be_get_info(node);
	info->in_reqs = reqs;
}

static inline const arch_register_req_t **arch_get_irn_register_reqs_in(
		const ir_node *node)
{
	backend_info_t *info = be_get_info(node);
	return info->in_reqs;
}

98
99
100
static inline reg_out_info_t *get_out_info(const ir_node *node)
{
	assert(get_irn_mode(node) != mode_T);
101
	size_t pos = 0;
102
	if (is_Proj(node)) {
103
		pos  = get_Proj_num(node);
104
105
106
		node = get_Proj_pred(node);
	}

Christoph Mallon's avatar
Christoph Mallon committed
107
	return get_out_info_n(node, pos);
108
109
110
111
112
113
114
}

static inline 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;
}
115

Sebastian Hack's avatar
Sebastian Hack committed
116
117
118
119
120
/**
 * Get the flags of a node.
 * @param irn The node.
 * @return The flags.
 */
121
122
static inline arch_irn_flags_t arch_get_irn_flags(const ir_node *node)
{
123
	backend_info_t const *const info = be_get_info(node);
124
125
	return info->flags;
}
Sebastian Hack's avatar
Sebastian Hack committed
126

127
128
129
130
131
static inline void arch_add_irn_flags(ir_node *const node, arch_irn_flags_t const flags)
{
	backend_info_t *const info = be_get_info(node);
	info->flags |= flags;
}
Sebastian Hack's avatar
Sebastian Hack committed
132

133
134
135
136
137
138
void arch_set_additional_pressure(ir_node *node, arch_register_class_t const *,
                                  uint8_t pressure);

uint8_t arch_get_additional_pressure(ir_node const *node,
                                     arch_register_class_t const *cls);

139
140
/**
 * Returns true if the given node should not be scheduled (has
141
 * arch_irn_flag_not_scheduled flag set)
142
143
144
145
146
147
148
 */
static inline bool arch_is_irn_not_scheduled(const ir_node *node)
{
	return is_Proj(node)
	    || (arch_get_irn_flags(node) & arch_irn_flag_not_scheduled);
}

149
#define arch_irn_is(irn, flag) ((arch_get_irn_flags(irn) & arch_irn_flag_ ## flag) != 0)
150
151
152

static inline unsigned arch_get_irn_n_outs(const ir_node *node)
{
153
	backend_info_t *const info = be_get_info(node);
154
155
156
	return (unsigned)ARR_LEN(info->out_infos);
}

157
158
159
#define be_foreach_out(node, i) \
	for (unsigned i = 0, i##__n = arch_get_irn_n_outs(node); i != i##__n; ++i)

160
161
162
163
164
/**
 * Register an instruction set architecture
 */
void be_register_isa_if(const char *name, const arch_isa_if_t *isa);

165
166
167
168
/**
 * A register.
 */
struct arch_register_t {
Michael Beck's avatar
Michael Beck committed
169
	const char                  *name;         /**< The name of the register. */
170
	arch_register_class_t const *cls;          /**< The class of the register */
171
172
	/** register constraint allowing just this register */
	const arch_register_req_t   *single_req;
Michael Beck's avatar
Michael Beck committed
173
174
	unsigned short               index;        /**< The index of the register in
	                                                the class. */
175
176
177
178
	unsigned short               global_index; /**< The global index this
												    register in the architecture. */
	/** register number in dwarf debugging format */
	unsigned short               dwarf_number;
179
180
	/** register number in instruction encoding */
	unsigned short               encoding;
181
182
183
184
	/** This is just a virtual register. Virtual registers fulfill any register
	 * constraints as long as the register class matches. It is allowed to
	 * have multiple definitions for the same virtual register at a point */
	bool                         is_virtual : 1;
185
186
187
188
189
190
191
};

/**
 * A class of registers.
 * Like general purpose or floating point.
 */
struct arch_register_class_t {
192
193
194
195
196
197
198
199
200
	const char                *name;   /**< The name of the register class.*/
	ir_mode                   *mode;   /**< The mode of the register class.*/
	const arch_register_t     *regs;   /**< The array of registers. */
	const arch_register_req_t *class_req;
	unsigned                   index;  /**< index of this register class */
	unsigned                   n_regs; /**< Number of registers in this
	                                        class. */
	/** don't do register allocation for this class */
	bool                       manual_ra : 1;
201
202
};

203
static inline const arch_register_t *arch_register_for_index(
204
		const arch_register_class_t *cls, unsigned idx)
205
206
207
208
209
210
211
212
213
{
	assert(idx < cls->n_regs);
	return &cls->regs[idx];
}

/**
 * Expresses requirements to register allocation for an operand.
 */
struct arch_register_req_t {
214
215
216
	/** The register class this constraint belongs to. */
	const arch_register_class_t *cls;
	/** allowed register bitset (in case of wide-values this is only about the
217
	 * first register). NULL if all registers are allowed. */
218
	const unsigned              *limited;
219
220
	/** Bitmask of ins which should use the same register. */
	unsigned                     should_be_same;
221
	/** Bitmask of ins which shall use a different register (must_be_different) */
222
	unsigned                     must_be_different;
223
224
	/** Specifies how many sequential registers are required */
	unsigned char                width;
225
226
227
228
	/** ignore this input/output while allocating registers */
	bool                         ignore : 1;
	/** The registernumber should be aligned (in case of multiregister values)*/
	bool                         aligned : 1;
229
230
231
	/** The instructions modifies the value in the register in an unknown way,
	 * the value has to be copied if it is needed afterwards. */
	bool                         kills_value : 1;
232
233
};

234
235
static inline bool reg_reqs_equal(const arch_register_req_t *req1,
                                  const arch_register_req_t *req2)
236
237
{
	if (req1 == req2)
238
		return true;
239

240
241
	if (req1->cls               != req2->cls               ||
	    req1->should_be_same    != req2->should_be_same    ||
242
	    req1->must_be_different != req2->must_be_different ||
243
244
	    req1->ignore            != req2->ignore            ||
	    req1->aligned           != req2->aligned           ||
Christoph Mallon's avatar
Christoph Mallon committed
245
	    (req1->limited != NULL) != (req2->limited != NULL))
246
		return false;
247
248

	if (req1->limited != NULL) {
249
		if (!rbitsets_equal(req1->limited, req2->limited, req1->cls->n_regs))
250
			return false;
251
252
	}

253
	return true;
254
255
}

256
257
static inline bool reg_req_has_constraint(const arch_register_req_t *req)
{
258
	return req->limited || req->must_be_different != 0 || req->ignore || req->aligned;
259
260
}

261
262
263
264
/**
 * Architecture interface.
 */
struct arch_isa_if_t {
265
266
267
268
269
	unsigned                     n_registers;        /**< number of registers */
	arch_register_t       const *registers;          /**< register array */
	unsigned                     n_register_classes; /**< number of register classes */
	arch_register_class_t const *register_classes;   /**< register classes */

270
	/**
271
272
273
274
275
	 * Initializes the isa interface. This is necessary before calling any
	 * other functions from this interface.
	 */
	void (*init)(void);

Matthias Braun's avatar
Matthias Braun committed
276
277
278
279
280
	/**
	 * Fress resources allocated by this isa interface.
	 */
	void (*finish)(void);

281
282
	/**
	 * Returns the frontend settings needed for this backend.
283
	 */
284
	const backend_params *(*get_params)(void);
285

286
287
288
289
290
	/**
	 * Generate code for the current firm program.
	 */
	void (*generate_code)(FILE *output, const char *cup_name);

291
292
293
294
295
296
	/**
	 * lowers current program for target. See the documentation for
	 * be_lower_for_target() for details.
	 */
	void (*lower_for_target)(void);

297
298
299
300
301
302
	/**
	 * returns true if the string is a valid clobbered (register) in this
	 * backend
	 */
	int (*is_valid_clobber)(const char *clobber);

303
	/**
304
305
	 * Called directly after initialization. Backend should handle all
	 * intrinsics here.
306
	 */
Matthias Braun's avatar
Matthias Braun committed
307
	void (*handle_intrinsics)(ir_graph *irg);
308
309
310
311
312
313

	/**
	 * Get a cost estimation for node @p irn. The cost should be similar to the
	 * number of cycles necessary to execute the instruction.
	 */
	unsigned (*get_op_estimated_cost)(const ir_node *irn);
314
315
316
317
};

static inline bool arch_irn_is_ignore(const ir_node *irn)
{
318
	const arch_register_req_t *req = arch_get_irn_register_req(irn);
319
	return req->ignore;
320
321
}

322
323
static inline bool arch_irn_consider_in_reg_alloc(
		const arch_register_class_t *cls, const ir_node *node)
324
{
325
	const arch_register_req_t *req = arch_get_irn_register_req(node);
326
	return req->cls == cls && !req->ignore;
327
328
}

329
arch_register_t const *arch_find_register(char const *name);
330

331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
#define be_foreach_value(node, value, code) \
	do { \
		if (get_irn_mode(node) == mode_T) { \
			foreach_out_edge(node, node##__edge) { \
				ir_node *const value = get_edge_src_irn(node##__edge); \
				if (!is_Proj(value)) \
					continue; \
				code \
			} \
		} else { \
			ir_node *const value = node; \
			code \
		} \
	} while (0)

346
#define be_foreach_definition_(node, ccls, value, req, code) \
347
348
349
350
351
352
	be_foreach_value(node, value, \
		arch_register_req_t const *const req = arch_get_irn_register_req(value); \
		if (req->cls != ccls) \
			continue; \
		code \
	)
353

354
355
356
357
358
359
/**
 * Iterate over all values defined by an instruction.
 * Only looks at values in a certain register class where the requirements
 * are not marked as ignore.
 * Executes @p code for each definition.
 */
360
361
#define be_foreach_definition(node, ccls, value, req, code) \
	be_foreach_definition_(node, ccls, value, req, \
362
		if (req->ignore) \
363
364
			continue; \
		code \
365
366
	)

367
368
369
370
371
372
373
374
#define be_foreach_use(node, ccls, in_req, value, value_req, code)           \
	do {                                                                     \
	for (int i_ = 0, n_ = get_irn_arity(node); i_ < n_; ++i_) {              \
		const arch_register_req_t *in_req = arch_get_irn_register_req_in(node, i_); \
		if (in_req->cls != ccls)                                             \
			continue;                                                        \
		ir_node                   *value     = get_irn_n(node, i_);              \
		const arch_register_req_t *value_req = arch_get_irn_register_req(value); \
375
		if (value_req->ignore)                                               \
376
377
378
379
380
			continue;                                                        \
		code                                                                 \
	}                                                                        \
	} while (0)

381
382
383
bool arch_reg_is_allocatable(const arch_register_req_t *req,
                             const arch_register_t *reg);

384
385
void arch_copy_irn_out_info(ir_node *dst, unsigned dst_pos, ir_node const *src);

386
#endif