2 * Copyright (C) 1995-2011 University of Karlsruhe. All right reserved.
4 * This file is part of libFirm.
6 * This file may be distributed and/or modified under the terms of the
7 * GNU General Public License version 2 as published by the Free Software
8 * Foundation and appearing in the file LICENSE.GPL included in the
9 * packaging of this file.
11 * Licensees holding valid libFirm Professional Edition licenses may use
12 * this file in accordance with the libFirm Commercial License.
13 * Agreement provided with the Software.
15 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
16 * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * @brief Processor architecture specification.
23 * @author Sebastian Hack
25 #ifndef FIRM_BE_BEARCH_H
26 #define FIRM_BE_BEARCH_H
30 #include "firm_types.h"
31 #include "raw_bitset.h"
38 * this constant is returned by the get_sp_bias functions if the stack
39 * is reset (usually because the frame pointer is copied to the stack
42 #define SP_BIAS_RESET INT_MIN
44 typedef enum arch_register_class_flags_t {
45 arch_register_class_flag_none = 0,
46 /** don't do automatic register allocation for this class */
47 arch_register_class_flag_manual_ra = 1U << 0,
48 /** the register models an abstract state (example: fpu rounding mode) */
49 arch_register_class_flag_state = 1U << 1
50 } arch_register_class_flags_t;
51 ENUM_BITSET(arch_register_class_flags_t)
53 typedef enum arch_register_type_t {
54 arch_register_type_none = 0,
55 /** Do not consider this register when allocating. */
56 arch_register_type_ignore = 1U << 0,
57 /** This is just a virtual register. Virtual registers fulfill any register
58 * constraints as long as the register class matches. It is a allowed to
59 * have multiple definitions for the same virtual register at a point */
60 arch_register_type_virtual = 1U << 1,
61 /** The register represents a state that should be handled by bestate
63 arch_register_type_state = 1U << 2,
64 } arch_register_type_t;
65 ENUM_BITSET(arch_register_type_t)
68 * Different types of register allocation requirements.
70 typedef enum arch_register_req_type_t {
71 /** No register requirement. */
72 arch_register_req_type_none = 0,
73 /** All registers in the class are allowed. */
74 arch_register_req_type_normal = 1U << 0,
75 /** Only a real subset of the class is allowed. */
76 arch_register_req_type_limited = 1U << 1,
77 /** The register should be equal to another one at the node. */
78 arch_register_req_type_should_be_same = 1U << 2,
79 /** The register must be unequal from some other at the node. */
80 arch_register_req_type_must_be_different = 1U << 3,
81 /** The registernumber should be aligned (in case of multiregister values)*/
82 arch_register_req_type_aligned = 1U << 4,
83 /** ignore while allocating registers */
84 arch_register_req_type_ignore = 1U << 5,
85 /** the output produces a new value for the stack pointer
86 * (this is not really a constraint but a marker to guide the stackpointer
88 arch_register_req_type_produces_sp = 1U << 6,
89 } arch_register_req_type_t;
90 ENUM_BITSET(arch_register_req_type_t)
92 extern arch_register_req_t const arch_no_requirement;
93 #define arch_no_register_req (&arch_no_requirement)
95 void arch_dump_register_reqs(FILE *F, const ir_node *node);
96 void arch_dump_reqs_and_registers(FILE *F, const ir_node *node);
98 void arch_set_frame_offset(ir_node *irn, int bias);
100 ir_entity *arch_get_frame_entity(const ir_node *irn);
101 int arch_get_sp_bias(ir_node *irn);
103 int arch_get_op_estimated_cost(const ir_node *irn);
104 int arch_possible_memory_operand(const ir_node *irn,
106 void arch_perform_memory_operand(ir_node *irn, ir_node *spill,
110 * Get the register allocated for a value.
112 const arch_register_t *arch_get_irn_register(const ir_node *irn);
115 * Assign register to a value
117 void arch_set_irn_register(ir_node *irn, const arch_register_t *reg);
120 * Set the register for a certain output operand.
122 void arch_set_irn_register_out(ir_node *irn, unsigned pos, const arch_register_t *r);
124 const arch_register_t *arch_get_irn_register_out(const ir_node *irn, unsigned pos);
125 const arch_register_t *arch_get_irn_register_in(const ir_node *irn, int pos);
128 * Get register constraints for an operand at position @p
130 static inline const arch_register_req_t *arch_get_irn_register_req_in(
131 const ir_node *node, int pos)
133 const backend_info_t *info = be_get_info(node);
134 return info->in_reqs[pos];
138 * Get register constraint for a produced result (the @p pos result)
140 static inline const arch_register_req_t *arch_get_irn_register_req_out(
141 const ir_node *node, unsigned pos)
143 const backend_info_t *info = be_get_info(node);
144 return info->out_infos[pos].req;
147 static inline void arch_set_irn_register_req_out(ir_node *node, unsigned pos,
148 const arch_register_req_t *req)
150 backend_info_t *info = be_get_info(node);
151 assert(pos < (unsigned)ARR_LEN(info->out_infos));
152 info->out_infos[pos].req = req;
155 static inline void arch_set_irn_register_reqs_in(ir_node *node,
156 const arch_register_req_t **reqs)
158 backend_info_t *info = be_get_info(node);
159 info->in_reqs = reqs;
162 static inline const arch_register_req_t **arch_get_irn_register_reqs_in(
165 backend_info_t *info = be_get_info(node);
166 return info->in_reqs;
169 static inline reg_out_info_t *get_out_info(const ir_node *node)
172 const backend_info_t *info;
173 assert(get_irn_mode(node) != mode_T);
175 pos = get_Proj_proj(node);
176 node = get_Proj_pred(node);
179 info = be_get_info(node);
180 assert(pos < ARR_LEN(info->out_infos));
181 return &info->out_infos[pos];
184 static inline const arch_register_req_t *arch_get_irn_register_req(const ir_node *node)
186 reg_out_info_t *out = get_out_info(node);
191 * Get the flags of a node.
192 * @param irn The node.
195 static inline arch_irn_flags_t arch_get_irn_flags(const ir_node *node)
197 backend_info_t const *const info = be_get_info(node);
201 void arch_set_irn_flags(ir_node *node, arch_irn_flags_t flags);
202 void arch_add_irn_flags(ir_node *node, arch_irn_flags_t flags);
204 #define arch_irn_is(irn, flag) ((arch_get_irn_flags(irn) & arch_irn_flags_ ## flag) != 0)
206 static inline unsigned arch_get_irn_n_outs(const ir_node *node)
208 backend_info_t *info = be_get_info(node);
209 if (info->out_infos == NULL)
212 return (unsigned)ARR_LEN(info->out_infos);
216 * Start codegeneration
218 arch_env_t *arch_env_begin_codegeneration(const arch_isa_if_t *isa,
219 be_main_env_t *main_env);
222 * Register an instruction set architecture
224 void be_register_isa_if(const char *name, const arch_isa_if_t *isa);
229 struct arch_register_t {
230 const char *name; /**< The name of the register. */
231 const arch_register_class_t *reg_class; /**< The class of the register */
232 unsigned short index; /**< The index of the register in
234 unsigned short global_index; /**< The global index this
235 register in the architecture. */
236 arch_register_type_t type; /**< The type of the register. */
237 /** register constraint allowing just this register */
238 const arch_register_req_t *single_req;
239 /** register number in dwarf debugging format */
240 unsigned short dwarf_number;
244 * A class of registers.
245 * Like general purpose or floating point.
247 struct arch_register_class_t {
248 unsigned index; /**< index of this register class */
249 const char *name; /**< The name of the register class.*/
250 unsigned n_regs; /**< Number of registers in this
252 ir_mode *mode; /**< The mode of the register class.*/
253 const arch_register_t *regs; /**< The array of registers. */
254 arch_register_class_flags_t flags; /**< register class flags. */
255 const arch_register_req_t *class_req;
258 /** return the number of registers in this register class */
259 #define arch_register_class_n_regs(cls) ((cls)->n_regs)
261 /** return the largest mode of this register class */
262 #define arch_register_class_mode(cls) ((cls)->mode)
264 /** return the name of this register class */
265 #define arch_register_class_name(cls) ((cls)->name)
267 /** return the index of this register class */
268 #define arch_register_class_index(cls) ((cls)->index)
270 /** return the register class flags */
271 #define arch_register_class_flags(cls) ((cls)->flags)
273 static inline const arch_register_t *arch_register_for_index(
274 const arch_register_class_t *cls, unsigned idx)
276 assert(idx < cls->n_regs);
277 return &cls->regs[idx];
281 * Convenience macro to check for set constraints.
282 * @param req A pointer to register requirements.
283 * @param kind The kind of constraint to check for
284 * (see arch_register_req_type_t).
285 * @return 1, If the kind of constraint is present, 0 if not.
287 #define arch_register_req_is(req, kind) \
288 (((req)->type & (arch_register_req_type_ ## kind)) != 0)
291 * Expresses requirements to register allocation for an operand.
293 struct arch_register_req_t {
294 arch_register_req_type_t type; /**< The type of the constraint. */
295 const arch_register_class_t *cls; /**< The register class this constraint
297 const unsigned *limited; /**< allowed register bitset
298 (in case of wide-values this is
299 only about the first register) */
300 unsigned other_same; /**< Bitmask of ins which should use the
301 same register (should_be_same). */
302 unsigned other_different; /**< Bitmask of ins which shall use a
304 (must_be_different) */
305 unsigned char width; /**< specifies how many sequential
306 registers are required */
309 static inline bool reg_reqs_equal(const arch_register_req_t *req1,
310 const arch_register_req_t *req2)
315 if (req1->type != req2->type ||
316 req1->cls != req2->cls ||
317 req1->other_same != req2->other_same ||
318 req1->other_different != req2->other_different ||
319 (req1->limited != NULL) != (req2->limited != NULL))
322 if (req1->limited != NULL) {
323 size_t const n_regs = arch_register_class_n_regs(req1->cls);
324 if (!rbitsets_equal(req1->limited, req2->limited, n_regs))
331 struct arch_irn_ops_t {
334 * Get the entity on the stack frame this node depends on.
335 * @param irn The node in question.
336 * @return The entity on the stack frame or NULL, if the node does not have
337 * a stack frame entity.
339 ir_entity *(*get_frame_entity)(const ir_node *irn);
342 * Set the offset of a node carrying an entity on the stack frame.
343 * @param irn The node.
344 * @param offset The offset of the node's stack frame entity.
346 void (*set_frame_offset)(ir_node *irn, int offset);
349 * Returns the delta of the stackpointer for nodes that increment or
350 * decrement the stackpointer with a constant value. (push, pop
351 * nodes on most architectures).
352 * A positive value stands for an expanding stack area, a negative value for
355 * @param irn The node
356 * @return 0 if the stackpointer is not modified with a constant
357 * value, otherwise the increment/decrement value
359 int (*get_sp_bias)(const ir_node *irn);
362 * Get the estimated cycle count for @p irn.
364 * @param irn The node.
365 * @return The estimated cycle count for this operation
367 int (*get_op_estimated_cost)(const ir_node *irn);
370 * Asks the backend whether operand @p i of @p irn can be loaded form memory
373 * @param irn The node.
374 * @param i Index of the argument we would like to know whether @p irn
375 * can load it form memory internally
376 * @return nonzero if argument can be loaded or zero otherwise
378 int (*possible_memory_operand)(const ir_node *irn, unsigned int i);
381 * Ask the backend to assimilate @p reload of operand @p i into @p irn.
383 * @param irn The node.
384 * @param spill The spill.
385 * @param i The position of the reload.
387 void (*perform_memory_operand)(ir_node *irn, ir_node *spill,
392 * Architecture interface.
394 struct arch_isa_if_t {
396 * Initializes the isa interface. This is necessary before calling any
397 * other functions from this interface.
402 * Fress resources allocated by this isa interface.
404 void (*finish)(void);
407 * Returns the frontend settings needed for this backend.
409 const backend_params *(*get_params)(void);
412 * lowers current program for target. See the documentation for
413 * be_lower_for_target() for details.
415 void (*lower_for_target)(void);
418 * parse an assembler constraint part and set flags according to its nature
419 * advances the *c pointer to point to the last parsed character (so if you
420 * parse a single character don't advance c)
422 asm_constraint_flags_t (*parse_asm_constraint)(const char **c);
425 * returns true if the string is a valid clobbered (register) in this
428 int (*is_valid_clobber)(const char *clobber);
431 * Start codegeneration
432 * @return a new isa instance
434 arch_env_t *(*begin_codegeneration)(const be_main_env_t *env);
437 * Free the isa instance.
439 void (*end_codegeneration)(void *self);
442 * Initialize the code generator for a graph
445 void (*init_graph)(ir_graph *irg);
448 * Get the ABI restrictions for procedure calls.
449 * @param call_type The call type of the method (procedure) in question.
450 * @param p The array of parameter locations to be filled.
452 void (*get_call_abi)(ir_type *call_type, be_abi_call_t *abi);
455 * mark node as rematerialized
457 void (*mark_remat)(ir_node *node);
460 * return node used as base in pic code addresses
462 ir_node* (*get_pic_base)(ir_graph *irg);
465 * Create a spill instruction. We assume that spill instructions
466 * do not need any additional registers and do not affect cpu-flags in any
468 * Construct a sequence of instructions after @p after (the resulting nodes
469 * are already scheduled).
470 * Returns a mode_M value which is used as input for a reload instruction.
472 ir_node *(*new_spill)(ir_node *value, ir_node *after);
475 * Create a reload instruction. We assume that reload instructions do not
476 * need any additional registers and do not affect cpu-flags in any way.
477 * Constructs a sequence of instruction before @p before (the resulting
478 * nodes are already scheduled). A rewiring of users is not performed in
480 * Returns a value representing the restored value.
482 ir_node *(*new_reload)(ir_node *value, ir_node *spilled_value,
486 * Checks if the given register is callee/caller saved.
487 * @deprecated, only necessary if backend still uses beabi functions
489 int (*register_saved_by)(const arch_register_t *reg, int callee);
492 * Called directly after initialization. Backend should handle all
495 void (*handle_intrinsics)(void);
498 * Called before abi introduce.
500 void (*before_abi)(ir_graph *irg);
503 * Called, when the graph is being normalized.
505 void (*prepare_graph)(ir_graph *irg);
508 * Called before register allocation.
510 void (*before_ra)(ir_graph *irg);
513 * Called directly before done is called. This should be the last place
514 * where the irg is modified.
516 void (*finish_graph)(ir_graph *irg);
519 * Called after everything happened. This call should emit the final
520 * assembly code but avoid changing the irg.
522 void (*emit)(ir_graph *irg);
525 #define arch_env_end_codegeneration(env) ((env)->impl->end_codegeneration(env))
526 #define arch_env_handle_intrinsics(env) \
527 do { if((env)->impl->handle_intrinsics != NULL) (env)->impl->handle_intrinsics(); } while(0)
528 #define arch_env_get_call_abi(env,tp,abi) ((env)->impl->get_call_abi((tp), (abi)))
529 #define arch_env_get_params(env) ((env)->impl->get_params())
530 #define arch_env_parse_asm_constraint(env,c) ((env)->impl->parse_asm_constraint((c))
531 #define arch_env_is_valid_clobber(env,clobber) ((env)->impl->is_valid_clobber((clobber))
532 #define arch_env_mark_remat(env,node) \
533 do { if ((env)->impl->mark_remat != NULL) (env)->impl->mark_remat((node)); } while(0)
535 #define arch_env_new_spill(env,value,after) ((env)->impl->new_spill(value, after))
536 #define arch_env_new_reload(env,value,spilled,before) ((env)->impl->new_reload(value, spilled, before))
542 const arch_isa_if_t *impl;
543 unsigned n_registers; /**< number of registers */
544 const arch_register_t *registers; /**< register array */
545 unsigned n_register_classes; /**< number of register classes*/
546 const arch_register_class_t *register_classes; /**< register classes */
547 const arch_register_t *sp; /**< The stack pointer register. */
548 const arch_register_t *bp; /**< The base pointer register. */
549 int stack_alignment; /**< power of 2 stack alignment */
550 const be_main_env_t *main_env; /**< the be main environment */
551 int spill_cost; /**< cost for a be_Spill node */
552 int reload_cost; /**< cost for a be_Reload node */
553 bool custom_abi : 1; /**< backend does all abi handling
554 and does not need the generic
555 stuff from beabi.h/.c */
558 static inline bool arch_irn_is_ignore(const ir_node *irn)
560 const arch_register_req_t *req = arch_get_irn_register_req(irn);
561 return arch_register_req_is(req, ignore);
564 static inline bool arch_irn_consider_in_reg_alloc(
565 const arch_register_class_t *cls, const ir_node *node)
567 const arch_register_req_t *req = arch_get_irn_register_req(node);
568 return req->cls == cls && !arch_register_req_is(req, ignore);
572 * Iterate over all values defined by an instruction.
573 * Only looks at values in a certain register class where the requirements
574 * are not marked as ignore.
575 * Executes @p code for each definition.
577 #define be_foreach_definition_(node, ccls, value, code) \
579 if (get_irn_mode(node) == mode_T) { \
580 foreach_out_edge(node, edge_) { \
581 ir_node *const value = get_edge_src_irn(edge_); \
582 arch_register_req_t const *const req_ = arch_get_irn_register_req(value); \
583 if (req_->cls != ccls) \
588 arch_register_req_t const *const req_ = arch_get_irn_register_req(node); \
589 ir_node *const value = node; \
590 if (req_->cls == ccls) { \
596 #define be_foreach_definition(node, ccls, value, code) \
597 be_foreach_definition_(node, ccls, value, \
598 if (arch_register_req_is(req_, ignore)) \
603 static inline const arch_register_class_t *arch_get_irn_reg_class(
606 const arch_register_req_t *req = arch_get_irn_register_req(node);
610 bool arch_reg_is_allocatable(const arch_register_req_t *req,
611 const arch_register_t *reg);