bearch.h 19.5 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
26
27
28
29
/**
 * this constant is returned by the get_sp_bias functions if the stack
 * is reset (usually because the frame pointer is copied to the stack
 * pointer
 */
#define SP_BIAS_RESET      INT_MIN

Matthias Braun's avatar
Matthias Braun committed
30
31
typedef enum arch_register_class_flags_t {
	arch_register_class_flag_none      = 0,
Matthias Braun's avatar
Matthias Braun committed
32
	/** don't do automatic register allocation for this class */
33
	arch_register_class_flag_manual_ra = 1U << 0,
Matthias Braun's avatar
Matthias Braun committed
34
	/** the register models an abstract state (example: fpu rounding mode) */
35
	arch_register_class_flag_state     = 1U << 1
Matthias Braun's avatar
Matthias Braun committed
36
} arch_register_class_flags_t;
37
ENUM_BITSET(arch_register_class_flags_t)
Matthias Braun's avatar
Matthias Braun committed
38

39
typedef enum arch_register_type_t {
Michael Beck's avatar
Michael Beck committed
40
	arch_register_type_none         = 0,
41
	/** Do not consider this register when allocating. */
42
	arch_register_type_ignore       = 1U << 0,
43
44
45
	/** 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 */
46
	arch_register_type_virtual      = 1U << 1,
47
48
	/** The register represents a state that should be handled by bestate
	 * code */
49
	arch_register_type_state        = 1U << 2,
Sebastian Hack's avatar
Sebastian Hack committed
50
} arch_register_type_t;
51
ENUM_BITSET(arch_register_type_t)
52

Sebastian Hack's avatar
Sebastian Hack committed
53
54
55
/**
 * Different types of register allocation requirements.
 */
56
typedef enum arch_register_req_type_t {
57
	/** No register requirement. */
58
	arch_register_req_type_none              = 0,
59
60
61
62
63
64
65
66
	/** All registers in the class are allowed. */
	arch_register_req_type_normal            = 1U << 0,
	/** Only a real subset of the class is allowed. */
	arch_register_req_type_limited           = 1U << 1,
	/** The register should be equal to another one at the node. */
	arch_register_req_type_should_be_same    = 1U << 2,
	/** The register must be unequal from some other at the node. */
	arch_register_req_type_must_be_different = 1U << 3,
67
	/** The registernumber should be aligned (in case of multiregister values)*/
68
	arch_register_req_type_aligned           = 1U << 4,
69
	/** ignore while allocating registers */
70
	arch_register_req_type_ignore            = 1U << 5,
71
72
73
	/** 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) */
74
	arch_register_req_type_produces_sp       = 1U << 6,
Sebastian Hack's avatar
Sebastian Hack committed
75
} arch_register_req_type_t;
76
ENUM_BITSET(arch_register_req_type_t)
77

78
79
extern arch_register_req_t const arch_no_requirement;
#define arch_no_register_req (&arch_no_requirement)
Matthias Braun's avatar
Matthias Braun committed
80

81
82
83
void arch_dump_register_reqs(FILE *F, const ir_node *node);
void arch_dump_reqs_and_registers(FILE *F, const ir_node *node);

84
void arch_set_frame_offset(ir_node *irn, int bias);
85

86
ir_entity *arch_get_frame_entity(const ir_node *irn);
87
int        arch_get_sp_bias(ir_node *irn);
Sebastian Hack's avatar
Sebastian Hack committed
88

89
int             arch_get_op_estimated_cost(const ir_node *irn);
90
91
92
93
int             arch_possible_memory_operand(const ir_node *irn,
                                             unsigned int i);
void            arch_perform_memory_operand(ir_node *irn, ir_node *spill,
                                            unsigned int i);
94

Sebastian Hack's avatar
Sebastian Hack committed
95
/**
96
 * Get the register allocated for a value.
Sebastian Hack's avatar
Sebastian Hack committed
97
 */
98
const arch_register_t *arch_get_irn_register(const ir_node *irn);
99

Sebastian Hack's avatar
Sebastian Hack committed
100
/**
101
 * Assign register to a value
Sebastian Hack's avatar
Sebastian Hack committed
102
 */
103
void arch_set_irn_register(ir_node *irn, const arch_register_t *reg);
104

Sebastian Hack's avatar
Sebastian Hack committed
105
/**
106
 * Set the register for a certain output operand.
Sebastian Hack's avatar
Sebastian Hack committed
107
 */
108
void arch_set_irn_register_out(ir_node *irn, unsigned pos, const arch_register_t *r);
Sebastian Hack's avatar
Sebastian Hack committed
109

110
const arch_register_t *arch_get_irn_register_out(const ir_node *irn, unsigned pos);
111
const arch_register_t *arch_get_irn_register_in(const ir_node *irn, int pos);
112

Sebastian Hack's avatar
Sebastian Hack committed
113
/**
114
 * Get register constraints for an operand at position @p
Sebastian Hack's avatar
Sebastian Hack committed
115
 */
116
117
118
119
120
121
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
122
123

/**
124
 * Get register constraint for a produced result (the @p pos result)
Sebastian Hack's avatar
Sebastian Hack committed
125
 */
126
static inline const arch_register_req_t *arch_get_irn_register_req_out(
127
		const ir_node *node, unsigned pos)
128
129
130
131
{
	const backend_info_t *info = be_get_info(node);
	return info->out_infos[pos].req;
}
Sebastian Hack's avatar
Sebastian Hack committed
132

133
static inline void arch_set_irn_register_req_out(ir_node *node, unsigned pos,
134
135
136
		const arch_register_req_t *req)
{
	backend_info_t *info = be_get_info(node);
137
	assert(pos < (unsigned)ARR_LEN(info->out_infos));
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
	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;
}

155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
static inline reg_out_info_t *get_out_info(const ir_node *node)
{
	size_t                pos = 0;
	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);
	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;
}
175

Sebastian Hack's avatar
Sebastian Hack committed
176
177
178
179
180
/**
 * Get the flags of a node.
 * @param irn The node.
 * @return The flags.
 */
181
182
static inline arch_irn_flags_t arch_get_irn_flags(const ir_node *node)
{
183
	backend_info_t const *const info = be_get_info(node);
184
185
	return info->flags;
}
Sebastian Hack's avatar
Sebastian Hack committed
186

187
188
void arch_set_irn_flags(ir_node *node, arch_irn_flags_t flags);
void arch_add_irn_flags(ir_node *node, arch_irn_flags_t flags);
Sebastian Hack's avatar
Sebastian Hack committed
189

190
191
192
193
#define arch_irn_is(irn, flag) ((arch_get_irn_flags(irn) & arch_irn_flags_ ## flag) != 0)

static inline unsigned arch_get_irn_n_outs(const ir_node *node)
{
194
	backend_info_t *const info = be_get_info(node);
195
196
197
	return (unsigned)ARR_LEN(info->out_infos);
}

198
199
200
#define be_foreach_out(node, i) \
	for (unsigned i = 0, i##__n = arch_get_irn_n_outs(node); i != i##__n; ++i)

201
202
203
204
205
/**
 * Register an instruction set architecture
 */
void be_register_isa_if(const char *name, const arch_isa_if_t *isa);

206
207
208
209
/**
 * A register.
 */
struct arch_register_t {
Michael Beck's avatar
Michael Beck committed
210
211
212
213
	const char                  *name;         /**< The name of the register. */
	const arch_register_class_t *reg_class;    /**< The class of the register */
	unsigned short               index;        /**< The index of the register in
	                                                the class. */
214
215
	unsigned short               global_index; /**< The global index this
												    register in the architecture. */
Michael Beck's avatar
Michael Beck committed
216
	arch_register_type_t         type;         /**< The type of the register. */
217
	/** register constraint allowing just this register */
218
	const arch_register_req_t   *single_req;
219
220
	/** register number in dwarf debugging format */
	unsigned short               dwarf_number;
221
222
223
224
225
226
227
};

/**
 * A class of registers.
 * Like general purpose or floating point.
 */
struct arch_register_class_t {
228
229
230
231
232
233
234
235
	unsigned                     index;   /**< index of this register class */
	const char                  *name;    /**< The name of the register class.*/
	unsigned                     n_regs;  /**< Number of registers in this
	                                           class. */
	ir_mode                     *mode;    /**< The mode of the register class.*/
	const arch_register_t       *regs;    /**< The array of registers. */
	arch_register_class_flags_t  flags;   /**< register class flags. */
	const arch_register_req_t   *class_req;
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
};

/** return the number of registers in this register class */
#define arch_register_class_n_regs(cls) ((cls)->n_regs)

/** return the largest mode of this register class */
#define arch_register_class_mode(cls) ((cls)->mode)

/** return the name of this register class */
#define arch_register_class_name(cls) ((cls)->name)

/** return the index of this register class */
#define arch_register_class_index(cls)  ((cls)->index)

/** return the register class flags */
#define arch_register_class_flags(cls) ((cls)->flags)

253
static inline const arch_register_t *arch_register_for_index(
254
		const arch_register_class_t *cls, unsigned idx)
255
256
257
258
259
260
261
262
{
	assert(idx < cls->n_regs);
	return &cls->regs[idx];
}

/**
 * Convenience macro to check for set constraints.
 * @param req   A pointer to register requirements.
263
264
 * @param kind  The kind of constraint to check for
 *              (see arch_register_req_type_t).
265
266
267
268
269
270
271
272
273
 * @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 {
274
275
276
	arch_register_req_type_t     type; /**< The type of the constraint. */
	const arch_register_class_t *cls;  /**< The register class this constraint
	                                        belongs to. */
277
278
279
280
281
282
283
284
285
286
	const unsigned *limited;           /**< allowed register bitset
	                                        (in case of wide-values this is
	                                         only about the first register) */
	unsigned other_same;               /**< Bitmask of ins which should use the
	                                        same register (should_be_same). */
	unsigned other_different;          /**< Bitmask of ins which shall use a
	                                        different register
	                                        (must_be_different) */
	unsigned char width;               /**< specifies how many sequential
	                                        registers are required */
287
288
};

289
290
static inline bool reg_reqs_equal(const arch_register_req_t *req1,
                                  const arch_register_req_t *req2)
291
292
{
	if (req1 == req2)
293
		return true;
294

Christoph Mallon's avatar
Christoph Mallon committed
295
296
297
298
299
	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))
300
		return false;
301
302

	if (req1->limited != NULL) {
Christoph Mallon's avatar
Christoph Mallon committed
303
		size_t const n_regs = arch_register_class_n_regs(req1->cls);
304
		if (!rbitsets_equal(req1->limited, req2->limited, n_regs))
305
			return false;
306
307
	}

308
	return true;
309
310
311
312
313
314
315
}

struct arch_irn_ops_t {

	/**
	 * Get the entity on the stack frame this node depends on.
	 * @param irn  The node in question.
316
317
	 * @return The entity on the stack frame or NULL, if the node does not have
	 *         a stack frame entity.
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
	 */
	ir_entity *(*get_frame_entity)(const ir_node *irn);

	/**
	 * Set the offset of a node carrying an entity on the stack frame.
	 * @param irn  The node.
	 * @param offset The offset of the node's stack frame entity.
	 */
	void (*set_frame_offset)(ir_node *irn, int offset);

	/**
	 * Returns the delta of the stackpointer for nodes that increment or
	 * decrement the stackpointer with a constant value. (push, pop
	 * nodes on most architectures).
	 * A positive value stands for an expanding stack area, a negative value for
	 * a shrinking one.
	 *
	 * @param irn       The node
	 * @return          0 if the stackpointer is not modified with a constant
	 *                  value, otherwise the increment/decrement value
	 */
	int (*get_sp_bias)(const ir_node *irn);

	/**
	 * 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);

	/**
350
351
	 * Asks the backend whether operand @p i of @p irn can be loaded form memory
	 * internally
352
353
	 *
	 * @param irn  The node.
354
355
	 * @param i    Index of the argument we would like to know whether @p irn
	 *             can load it form memory internally
356
357
358
359
360
361
362
363
364
365
366
	 * @return     nonzero if argument can be loaded or zero otherwise
	 */
	int (*possible_memory_operand)(const ir_node *irn, unsigned int i);

	/**
	 * Ask the backend to assimilate @p reload of operand @p i into @p irn.
	 *
	 * @param irn    The node.
	 * @param spill  The spill.
	 * @param i      The position of the reload.
	 */
367
368
	void (*perform_memory_operand)(ir_node *irn, ir_node *spill,
	                               unsigned int i);
369
370
371
372
373
374
375
};

/**
 * Architecture interface.
 */
struct arch_isa_if_t {
	/**
376
377
378
379
380
	 * 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
381
382
383
384
385
	/**
	 * Fress resources allocated by this isa interface.
	 */
	void (*finish)(void);

386
387
	/**
	 * Returns the frontend settings needed for this backend.
388
	 */
389
	const backend_params *(*get_params)(void);
390

391
392
393
394
395
396
	/**
	 * lowers current program for target. See the documentation for
	 * be_lower_for_target() for details.
	 */
	void (*lower_for_target)(void);

397
398
399
400
401
402
403
404
405
406
	/**
	 * returns true if the string is a valid clobbered (register) in this
	 * backend
	 */
	int (*is_valid_clobber)(const char *clobber);

	/**
	 * Start codegeneration
	 * @return a new isa instance
	 */
407
	arch_env_t *(*begin_codegeneration)(void);
408

409
410
411
	/**
	 * Free the isa instance.
	 */
412
	void (*end_codegeneration)(void *self);
413
414

	/**
415
416
	 * Initialize the code generator for a graph
	 * @param irg  A graph
417
	 */
418
	void (*init_graph)(ir_graph *irg);
419
420
421
422
423
424

	/**
	 * Get the ABI restrictions for procedure calls.
	 * @param call_type   The call type of the method (procedure) in question.
	 * @param p           The array of parameter locations to be filled.
	 */
425
	void (*get_call_abi)(ir_type *call_type, be_abi_call_t *abi);
426
427

	/**
428
	 * mark node as rematerialized
429
	 */
430
	void (*mark_remat)(ir_node *node);
431
432

	/**
433
	 * return node used as base in pic code addresses
434
	 */
435
	ir_node* (*get_pic_base)(ir_graph *irg);
436
437

	/**
438
439
440
441
442
443
	 * Create a spill instruction. We assume that spill instructions
	 * do not need any additional registers and do not affect cpu-flags in any
	 * way.
	 * Construct a sequence of instructions after @p after (the resulting nodes
	 * are already scheduled).
	 * Returns a mode_M value which is used as input for a reload instruction.
444
	 */
445
	ir_node *(*new_spill)(ir_node *value, ir_node *after);
446
447

	/**
448
449
450
451
452
453
	 * Create a reload instruction. We assume that reload instructions do not
	 * need any additional registers and do not affect cpu-flags in any way.
	 * Constructs a sequence of instruction before @p before (the resulting
	 * nodes are already scheduled). A rewiring of users is not performed in
	 * this function.
	 * Returns a value representing the restored value.
454
	 */
455
456
	ir_node *(*new_reload)(ir_node *value, ir_node *spilled_value,
	                       ir_node *before);
457
458

	/**
459
460
	 * Checks if the given register is callee/caller saved.
	 * @deprecated, only necessary if backend still uses beabi functions
461
	 */
462
	int (*register_saved_by)(const arch_register_t *reg, int callee);
463
464

	/**
465
466
	 * Called directly after initialization. Backend should handle all
	 * intrinsics here.
467
	 */
468
	void (*handle_intrinsics)(void);
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488

	/**
	 * Called before abi introduce.
	 */
	void (*before_abi)(ir_graph *irg);

	/**
	 * Called, when the graph is being normalized.
	 */
	void (*prepare_graph)(ir_graph *irg);

	/**
	 * Called before register allocation.
	 */
	void (*before_ra)(ir_graph *irg);

	/**
	 * Called directly before done is called. This should be the last place
	 * where the irg is modified.
	 */
Matthias Braun's avatar
Matthias Braun committed
489
	void (*finish_graph)(ir_graph *irg);
490
491
492
493
494
495

	/**
	 * Called after everything happened. This call should emit the final
	 * assembly code but avoid changing the irg.
	 */
	void (*emit)(ir_graph *irg);
496
497
};

498
#define arch_env_end_codegeneration(env)               ((env)->impl->end_codegeneration(env))
499
500
#define arch_env_handle_intrinsics(env)                \
	do { if((env)->impl->handle_intrinsics != NULL) (env)->impl->handle_intrinsics(); } while(0)
501
#define arch_env_get_call_abi(env,tp,abi)              ((env)->impl->get_call_abi((tp), (abi)))
502
#define arch_env_mark_remat(env,node) \
503
	do { if ((env)->impl->mark_remat != NULL) (env)->impl->mark_remat((node)); } while(0)
504

505
506
507
#define arch_env_new_spill(env,value,after)            ((env)->impl->new_spill(value, after))
#define arch_env_new_reload(env,value,spilled,before)  ((env)->impl->new_reload(value, spilled, before))

508
509
510
511
512
/**
 * ISA base class.
 */
struct arch_env_t {
	const arch_isa_if_t   *impl;
513
514
	unsigned               n_registers;      /**< number of registers */
	const arch_register_t *registers;        /**< register array */
515
516
	unsigned               n_register_classes; /**< number of register classes*/
	const arch_register_class_t *register_classes; /**< register classes */
517
518
519
520
521
	const arch_register_t *sp;               /**< The stack pointer register. */
	const arch_register_t *bp;               /**< The base pointer register. */
	int                    stack_alignment;  /**< power of 2 stack alignment */
	int                    spill_cost;       /**< cost for a be_Spill node */
	int                    reload_cost;      /**< cost for a be_Reload node */
522
	bool                   custom_abi : 1;   /**< backend does all abi handling
523
524
	                                              and does not need the generic
	                                              stuff from beabi.h/.c */
525
526
527
528
};

static inline bool arch_irn_is_ignore(const ir_node *irn)
{
529
	const arch_register_req_t *req = arch_get_irn_register_req(irn);
530
	return arch_register_req_is(req, ignore);
531
532
}

533
534
static inline bool arch_irn_consider_in_reg_alloc(
		const arch_register_class_t *cls, const ir_node *node)
535
{
536
	const arch_register_req_t *req = arch_get_irn_register_req(node);
537
	return req->cls == cls && !arch_register_req_is(req, ignore);
538
539
}

540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
#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)

555
556
557
558
559
560
/**
 * 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.
 */
561
#define be_foreach_definition_(node, ccls, value, req, code) \
562
563
564
565
566
567
	be_foreach_value(node, value, \
		arch_register_req_t const *const req = arch_get_irn_register_req(value); \
		if (req->cls != ccls) \
			continue; \
		code \
	)
568

569
570
571
#define be_foreach_definition(node, ccls, value, req, code) \
	be_foreach_definition_(node, ccls, value, req, \
		if (arch_register_req_is(req, ignore)) \
572
573
			continue; \
		code \
574
575
	)

576
577
578
579
580
581
582
583
584
585
586
587
588
589
#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)

590
591
592
593
594
595
596
597
598
599
static inline const arch_register_class_t *arch_get_irn_reg_class(
		const ir_node *node)
{
	const arch_register_req_t *req = arch_get_irn_register_req(node);
	return req->cls;
}

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

600
#endif