bearch.h 13 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

Matthias Braun's avatar
Matthias Braun committed
23
24
typedef enum arch_register_class_flags_t {
	arch_register_class_flag_none      = 0,
Matthias Braun's avatar
Matthias Braun committed
25
	/** don't do automatic register allocation for this class */
26
	arch_register_class_flag_manual_ra = 1U << 0,
Matthias Braun's avatar
Matthias Braun committed
27
} arch_register_class_flags_t;
28
ENUM_BITSET(arch_register_class_flags_t)
Matthias Braun's avatar
Matthias Braun committed
29

30
typedef enum arch_register_type_t {
31
	arch_register_type_none    = 0,
32
33
34
	/** This is just a virtual register. Virtual registers fulfill any register
	 * constraints as long as the register class matches. It is a allowed to
	 * have multiple definitions for the same virtual register at a point */
35
	arch_register_type_virtual = 1U << 0,
Sebastian Hack's avatar
Sebastian Hack committed
36
} arch_register_type_t;
37
ENUM_BITSET(arch_register_type_t)
38

Sebastian Hack's avatar
Sebastian Hack committed
39
40
41
/**
 * Different types of register allocation requirements.
 */
42
typedef enum arch_register_req_type_t {
43
	/** All registers in the class are allowed. */
44
	arch_register_req_type_none              = 0,
45
	/** Only a real subset of the class is allowed. */
46
	arch_register_req_type_limited           = 1U << 0,
47
	/** The register should be equal to another one at the node. */
48
	arch_register_req_type_should_be_same    = 1U << 1,
49
	/** The register must be unequal from some other at the node. */
50
	arch_register_req_type_must_be_different = 1U << 2,
51
	/** The registernumber should be aligned (in case of multiregister values)*/
52
	arch_register_req_type_aligned           = 1U << 3,
53
	/** ignore while allocating registers */
54
	arch_register_req_type_ignore            = 1U << 4,
55
56
57
	/** the output produces a new value for the stack pointer
	 * (this is not really a constraint but a marker to guide the stackpointer
	 * rewiring logic) */
58
	arch_register_req_type_produces_sp       = 1U << 5,
Sebastian Hack's avatar
Sebastian Hack committed
59
} arch_register_req_type_t;
60
ENUM_BITSET(arch_register_req_type_t)
61

62
63
extern arch_register_req_t const arch_no_requirement;
#define arch_no_register_req (&arch_no_requirement)
Matthias Braun's avatar
Matthias Braun committed
64

65
66
int arch_get_op_estimated_cost(const ir_node *irn);

Sebastian Hack's avatar
Sebastian Hack committed
67
/**
68
 * Get the register allocated for a value.
Sebastian Hack's avatar
Sebastian Hack committed
69
 */
70
const arch_register_t *arch_get_irn_register(const ir_node *irn);
71

Sebastian Hack's avatar
Sebastian Hack committed
72
/**
73
 * Assign register to a value
Sebastian Hack's avatar
Sebastian Hack committed
74
 */
75
void arch_set_irn_register(ir_node *irn, const arch_register_t *reg);
76

Sebastian Hack's avatar
Sebastian Hack committed
77
/**
78
 * Set the register for a certain output operand.
Sebastian Hack's avatar
Sebastian Hack committed
79
 */
80
void arch_set_irn_register_out(ir_node *irn, unsigned pos, const arch_register_t *r);
Sebastian Hack's avatar
Sebastian Hack committed
81

82
const arch_register_t *arch_get_irn_register_out(const ir_node *irn, unsigned pos);
83
const arch_register_t *arch_get_irn_register_in(const ir_node *irn, int pos);
84

Sebastian Hack's avatar
Sebastian Hack committed
85
/**
86
 * Get register constraints for an operand at position @p
Sebastian Hack's avatar
Sebastian Hack committed
87
 */
88
89
90
91
92
93
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
94
95

/**
96
 * Get register constraint for a produced result (the @p pos result)
Sebastian Hack's avatar
Sebastian Hack committed
97
 */
98
static inline const arch_register_req_t *arch_get_irn_register_req_out(
99
		const ir_node *node, unsigned pos)
100
101
102
103
{
	const backend_info_t *info = be_get_info(node);
	return info->out_infos[pos].req;
}
Sebastian Hack's avatar
Sebastian Hack committed
104

105
static inline void arch_set_irn_register_req_out(ir_node *node, unsigned pos,
106
107
108
		const arch_register_req_t *req)
{
	backend_info_t *info = be_get_info(node);
109
	assert(pos < (unsigned)ARR_LEN(info->out_infos));
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
	info->out_infos[pos].req = req;
}

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;
}

127
128
129
static inline reg_out_info_t *get_out_info(const ir_node *node)
{
	assert(get_irn_mode(node) != mode_T);
130
	size_t pos = 0;
131
	if (is_Proj(node)) {
132
		pos  = get_Proj_num(node);
133
134
135
		node = get_Proj_pred(node);
	}

136
	const backend_info_t *info = be_get_info(node);
137
138
139
140
141
142
143
144
145
	assert(pos < ARR_LEN(info->out_infos));
	return &info->out_infos[pos];
}

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;
}
146

Sebastian Hack's avatar
Sebastian Hack committed
147
148
149
150
151
/**
 * Get the flags of a node.
 * @param irn The node.
 * @return The flags.
 */
152
153
static inline arch_irn_flags_t arch_get_irn_flags(const ir_node *node)
{
154
	backend_info_t const *const info = be_get_info(node);
155
156
	return info->flags;
}
Sebastian Hack's avatar
Sebastian Hack committed
157

158
159
160
161
162
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
163

164
165
166
167
168
169
170
171
172
173
/**
 * Returns true if the given node should not be scheduled (has
 * arch_irn_flag_not_scheduled flag seet)
 */
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);
}

174
#define arch_irn_is(irn, flag) ((arch_get_irn_flags(irn) & arch_irn_flag_ ## flag) != 0)
175
176
177

static inline unsigned arch_get_irn_n_outs(const ir_node *node)
{
178
	backend_info_t *const info = be_get_info(node);
179
180
181
	return (unsigned)ARR_LEN(info->out_infos);
}

182
183
184
#define be_foreach_out(node, i) \
	for (unsigned i = 0, i##__n = arch_get_irn_n_outs(node); i != i##__n; ++i)

185
186
187
188
189
/**
 * Register an instruction set architecture
 */
void be_register_isa_if(const char *name, const arch_isa_if_t *isa);

190
191
192
193
/**
 * A register.
 */
struct arch_register_t {
Michael Beck's avatar
Michael Beck committed
194
	const char                  *name;         /**< The name of the register. */
195
	arch_register_class_t const *cls;          /**< The class of the register */
196
197
198
	/** register constraint allowing just this register */
	const arch_register_req_t   *single_req;
	arch_register_type_t         type;         /**< The type of the register. */
Michael Beck's avatar
Michael Beck committed
199
200
	unsigned short               index;        /**< The index of the register in
	                                                the class. */
201
202
203
204
	unsigned short               global_index; /**< The global index this
												    register in the architecture. */
	/** register number in dwarf debugging format */
	unsigned short               dwarf_number;
205
206
	/** register number in instruction encoding */
	unsigned short               encoding;
207
208
209
210
211
212
213
};

/**
 * A class of registers.
 * Like general purpose or floating point.
 */
struct arch_register_class_t {
214
215
216
	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. */
217
	const arch_register_req_t   *class_req;
218
219
220
221
	unsigned                     index;  /**< index of this register class */
	unsigned                     n_regs; /**< Number of registers in this
	                                          class. */
	arch_register_class_flags_t  flags;  /**< register class flags. */
222
223
};

224
static inline const arch_register_t *arch_register_for_index(
225
		const arch_register_class_t *cls, unsigned idx)
226
227
228
229
230
231
232
233
{
	assert(idx < cls->n_regs);
	return &cls->regs[idx];
}

/**
 * Convenience macro to check for set constraints.
 * @param req   A pointer to register requirements.
234
235
 * @param kind  The kind of constraint to check for
 *              (see arch_register_req_type_t).
236
237
238
239
240
241
242
243
244
 * @return      1, If the kind of constraint is present, 0 if not.
 */
#define arch_register_req_is(req, kind) \
	(((req)->type & (arch_register_req_type_ ## kind)) != 0)

/**
 * Expresses requirements to register allocation for an operand.
 */
struct arch_register_req_t {
245
246
247
248
249
	/** 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
	 * first register) */
	const unsigned              *limited;
250
	arch_register_req_type_t     type; /**< The type of the constraint. */
251
252
253
254
255
256
	/** Bitmask of ins which should use the same register (should_be_same). */
	unsigned                     other_same;
	/** Bitmask of ins which shall use a different register (must_be_different) */
	unsigned                     other_different;
	/** Specifies how many sequential registers are required */
	unsigned char                width;
257
258
};

259
260
static inline bool reg_reqs_equal(const arch_register_req_t *req1,
                                  const arch_register_req_t *req2)
261
262
{
	if (req1 == req2)
263
		return true;
264

Christoph Mallon's avatar
Christoph Mallon committed
265
266
267
268
269
	if (req1->type              != req2->type            ||
	    req1->cls               != req2->cls             ||
	    req1->other_same        != req2->other_same      ||
	    req1->other_different   != req2->other_different ||
	    (req1->limited != NULL) != (req2->limited != NULL))
270
		return false;
271
272

	if (req1->limited != NULL) {
273
		if (!rbitsets_equal(req1->limited, req2->limited, req1->cls->n_regs))
274
			return false;
275
276
	}

277
	return true;
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
}

struct arch_irn_ops_t {
	/**
	 * Get the estimated cycle count for @p irn.
	 *
	 * @param irn  The node.
	 * @return     The estimated cycle count for this operation
	 */
	int (*get_op_estimated_cost)(const ir_node *irn);
};

/**
 * Architecture interface.
 */
struct arch_isa_if_t {
294
295
296
297
298
	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 */

299
	/**
300
301
302
303
304
	 * 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
305
306
307
308
309
	/**
	 * Fress resources allocated by this isa interface.
	 */
	void (*finish)(void);

310
311
	/**
	 * Returns the frontend settings needed for this backend.
312
	 */
313
	const backend_params *(*get_params)(void);
314

315
316
317
318
319
	/**
	 * Generate code for the current firm program.
	 */
	void (*generate_code)(FILE *output, const char *cup_name);

320
321
322
323
324
325
	/**
	 * lowers current program for target. See the documentation for
	 * be_lower_for_target() for details.
	 */
	void (*lower_for_target)(void);

326
327
328
329
330
331
	/**
	 * returns true if the string is a valid clobbered (register) in this
	 * backend
	 */
	int (*is_valid_clobber)(const char *clobber);

332
	/**
333
334
	 * Called directly after initialization. Backend should handle all
	 * intrinsics here.
335
	 */
Matthias Braun's avatar
Matthias Braun committed
336
	void (*handle_intrinsics)(ir_graph *irg);
337
338
339
340
};

static inline bool arch_irn_is_ignore(const ir_node *irn)
{
341
	const arch_register_req_t *req = arch_get_irn_register_req(irn);
342
	return arch_register_req_is(req, ignore);
343
344
}

345
346
static inline bool arch_irn_consider_in_reg_alloc(
		const arch_register_class_t *cls, const ir_node *node)
347
{
348
	const arch_register_req_t *req = arch_get_irn_register_req(node);
349
	return req->cls == cls && !arch_register_req_is(req, ignore);
350
351
}

352
arch_register_t const *arch_find_register(char const *name);
353

354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
#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)

369
#define be_foreach_definition_(node, ccls, value, req, code) \
370
371
372
373
374
375
	be_foreach_value(node, value, \
		arch_register_req_t const *const req = arch_get_irn_register_req(value); \
		if (req->cls != ccls) \
			continue; \
		code \
	)
376

377
378
379
380
381
382
/**
 * 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.
 */
383
384
385
#define be_foreach_definition(node, ccls, value, req, code) \
	be_foreach_definition_(node, ccls, value, req, \
		if (arch_register_req_is(req, ignore)) \
386
387
			continue; \
		code \
388
389
	)

390
391
392
393
394
395
396
397
398
399
400
401
402
403
#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); \
		if (value_req->type & arch_register_req_type_ignore)                 \
			continue;                                                        \
		code                                                                 \
	}                                                                        \
	} while (0)

404
405
406
bool arch_reg_is_allocatable(const arch_register_req_t *req,
                             const arch_register_t *reg);

407
408
409
410
411
412
413
414
415
416
417
typedef struct be_start_info_t {
	unsigned pos;
	ir_node *irn;
} be_start_info_t;

void be_make_start_mem(be_start_info_t *info, ir_node *start, unsigned pos);

void be_make_start_out(be_start_info_t *info, struct obstack *obst, ir_node *start, unsigned pos, arch_register_t const *reg, arch_register_req_type_t flags);

ir_node *be_get_start_proj(ir_graph *irg, be_start_info_t *info);

418
419
void arch_copy_irn_out_info(ir_node *dst, unsigned dst_pos, ir_node const *src);

420
#endif