2 * Copyright (C) 1995-2008 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
26 #ifndef FIRM_BE_BEARCH_H
27 #define FIRM_BE_BEARCH_H
31 #include "firm_types.h"
34 #include "raw_bitset.h"
43 typedef enum arch_register_class_flags_t {
44 arch_register_class_flag_none = 0,
45 /** don't do automatic register allocation for this class */
46 arch_register_class_flag_manual_ra = 1U << 0,
47 /** the register models an abstract state (example: fpu rounding mode) */
48 arch_register_class_flag_state = 1U << 1
49 } arch_register_class_flags_t;
51 typedef enum arch_register_type_t {
52 arch_register_type_none = 0,
53 /** The register must be saved by the caller upon a function call. It thus
54 * can be overwritten in the called function. */
55 arch_register_type_caller_save = 1U << 0,
56 /** The register must be saved by the caller upon a function call. It thus
57 * can be overwritten in the called function. */
58 arch_register_type_callee_save = 1U << 1,
59 /** Do not consider this register when allocating. */
60 arch_register_type_ignore = 1U << 2,
61 /** The emitter can choose an arbitrary register. The register fulfills any
62 * register constraints as long as the register class matches */
63 arch_register_type_joker = 1U << 3,
64 /** This is just a virtual register. Virtual registers fulfill any register
65 * constraints as long as the register class matches. It is a allowed to
66 * have multiple definitions for the same virtual register at a point */
67 arch_register_type_virtual = 1U << 4,
68 /** The register represents a state that should be handled by bestate
70 arch_register_type_state = 1U << 5,
71 } arch_register_type_t;
74 * Different types of register allocation requirements.
76 typedef enum arch_register_req_type_t {
77 /** No register requirement. */
78 arch_register_req_type_none = 0,
79 /** All registers in the class are allowed. */
80 arch_register_req_type_normal = 1U << 0,
81 /** Only a real subset of the class is allowed. */
82 arch_register_req_type_limited = 1U << 1,
83 /** The register should be equal to another one at the node. */
84 arch_register_req_type_should_be_same = 1U << 2,
85 /** The register must be unequal from some other at the node. */
86 arch_register_req_type_must_be_different = 1U << 3,
87 /** The registernumber should be aligned (in case of multiregister values)*/
88 arch_register_req_type_aligned = 1U << 4,
89 /** ignore while allocating registers */
90 arch_register_req_type_ignore = 1U << 5,
91 /** the output produces a new value for the stack pointer
92 * (this is not really a constraint but a marker to guide the stackpointer
94 arch_register_req_type_produces_sp = 1U << 6,
95 } arch_register_req_type_t;
97 extern const arch_register_req_t *arch_no_register_req;
100 * Print information about a register requirement in human readable form
101 * @param F output stream/file
102 * @param req The requirements structure to format.
104 void arch_dump_register_req(FILE *F, const arch_register_req_t *req,
105 const ir_node *node);
107 void arch_dump_register_reqs(FILE *F, const ir_node *node);
108 void arch_dump_reqs_and_registers(FILE *F, const ir_node *node);
111 * Node classification. Used for statistics and for detecting reload nodes.
113 typedef enum arch_irn_class_t {
114 arch_irn_class_spill = 1 << 0,
115 arch_irn_class_reload = 1 << 1,
116 arch_irn_class_remat = 1 << 2,
117 arch_irn_class_copy = 1 << 3,
118 arch_irn_class_perm = 1 << 4
121 void arch_set_frame_offset(ir_node *irn, int bias);
123 ir_entity *arch_get_frame_entity(const ir_node *irn);
124 int arch_get_sp_bias(ir_node *irn);
126 int arch_get_op_estimated_cost(const ir_node *irn);
127 arch_inverse_t *arch_get_inverse(const ir_node *irn, int i,
128 arch_inverse_t *inverse,
129 struct obstack *obstack);
130 int arch_possible_memory_operand(const ir_node *irn,
132 void arch_perform_memory_operand(ir_node *irn, ir_node *spill,
136 * Get the register requirements for a node.
137 * @note Deprecated API! Preferably use
138 * arch_get_in_register_req and
139 * arch_get_out_register_req.
141 * @param irn The node.
142 * @param pos The position of the operand you're interested in.
143 * @return A pointer to the register requirements. If NULL is returned, the
144 * operand was no register operand.
146 const arch_register_req_t *arch_get_register_req(const ir_node *irn, int pos);
149 * Put all registers which shall not be ignored by the register
150 * allocator in a bit set.
151 * @param cls The register class to consider.
152 * @param bs The bit set to put the registers to.
154 extern void arch_put_non_ignore_regs(const arch_register_class_t *cls,
158 * Check, if a register is assignable to an operand of a node.
159 * @param irn The node.
160 * @param pos The position of the operand.
161 * @param reg The register.
162 * @return 1, if the register might be allocated to the operand 0 if not.
164 int arch_reg_is_allocatable(const ir_node *irn, int pos,
165 const arch_register_t *reg);
167 #define arch_reg_out_is_allocatable(irn, reg) arch_reg_is_allocatable(irn, -1, reg)
170 * Get the register class of an operand of a node.
171 * @param irn The node.
172 * @param pos The position of the operand, -1 for the output.
173 * @return The register class of the operand or NULL, if
174 * operand is a non-register operand.
176 const arch_register_class_t *arch_get_irn_reg_class(const ir_node *irn,
179 #define arch_get_irn_reg_class_out(irn) arch_get_irn_reg_class(irn, -1)
182 * Get the register allocated at a certain output operand of a node.
183 * @param irn The node.
184 * @return The register allocated for this operand
186 const arch_register_t *arch_get_irn_register(const ir_node *irn);
187 const arch_register_t *arch_irn_get_register(const ir_node *irn, int pos);
190 * Set the register for a certain output operand.
191 * @param irn The node.
192 * @param reg The register.
194 void arch_set_irn_register(ir_node *irn, const arch_register_t *reg);
195 void arch_irn_set_register(ir_node *irn, int pos, const arch_register_t *reg);
199 * @param irn The node.
200 * @return A classification of the node.
202 arch_irn_class_t arch_irn_classify(const ir_node *irn);
205 * Get the flags of a node.
206 * @param irn The node.
209 arch_irn_flags_t arch_irn_get_flags(const ir_node *irn);
211 void arch_irn_set_flags(ir_node *node, arch_irn_flags_t flags);
212 void arch_irn_add_flags(ir_node *node, arch_irn_flags_t flags);
214 #define arch_irn_is(irn, flag) ((arch_irn_get_flags(irn) & arch_irn_flags_ ## flag) != 0)
217 * Get the operations of an irn.
218 * @param self The handler from which the method is invoked.
219 * @param irn Some node.
220 * @return Operations for that irn.
222 typedef const void *(arch_get_irn_ops_t)(const ir_node *irn);
225 * Initialize the architecture environment struct.
226 * @param isa The isa which shall be put into the environment.
227 * @param file_handle The file handle
228 * @return The environment.
230 extern arch_env_t *arch_env_init(const arch_isa_if_t *isa,
231 FILE *file_handle, be_main_env_t *main_env);
234 * Register an instruction set architecture
236 void be_register_isa_if(const char *name, const arch_isa_if_t *isa);
241 struct arch_register_t {
242 const char *name; /**< The name of the register. */
243 const arch_register_class_t *reg_class; /**< The class of the register */
244 unsigned short index; /**< The index of the register in
246 unsigned short global_index;
247 arch_register_type_t type; /**< The type of the register. */
248 /** register constraint allowing just this register */
249 const arch_register_req_t *single_req;
252 static inline const arch_register_class_t *arch_register_get_class(
253 const arch_register_t *reg)
255 return reg->reg_class;
258 static inline unsigned arch_register_get_index(const arch_register_t *reg)
263 static inline const char *arch_register_get_name(const arch_register_t *reg)
269 * Convenience macro to check for register type.
270 * @param req A pointer to register.
271 * @param kind The kind of type to check for (see arch_register_type_t).
272 * @return 1, If register is of given kind, 0 if not.
274 #define arch_register_type_is(reg, kind) \
275 (((reg)->type & arch_register_type_ ## kind) != 0)
278 * A class of registers.
279 * Like general purpose or floating point.
281 struct arch_register_class_t {
282 unsigned index; /**< index of this register class */
283 const char *name; /**< The name of the register class.*/
284 unsigned n_regs; /**< Number of registers in this
286 ir_mode *mode; /**< The mode of the register class.*/
287 const arch_register_t *regs; /**< The array of registers. */
288 arch_register_class_flags_t flags; /**< register class flags. */
289 const arch_register_req_t *class_req;
292 /** return the number of registers in this register class */
293 #define arch_register_class_n_regs(cls) ((cls)->n_regs)
295 /** return the largest mode of this register class */
296 #define arch_register_class_mode(cls) ((cls)->mode)
298 /** return the name of this register class */
299 #define arch_register_class_name(cls) ((cls)->name)
301 /** return the index of this register class */
302 #define arch_register_class_index(cls) ((cls)->index)
304 /** return the register class flags */
305 #define arch_register_class_flags(cls) ((cls)->flags)
307 static inline const arch_register_t *arch_register_for_index(
308 const arch_register_class_t *cls, unsigned idx)
310 assert(idx < cls->n_regs);
311 return &cls->regs[idx];
315 * Convenience macro to check for set constraints.
316 * @param req A pointer to register requirements.
317 * @param kind The kind of constraint to check for
318 * (see arch_register_req_type_t).
319 * @return 1, If the kind of constraint is present, 0 if not.
321 #define arch_register_req_is(req, kind) \
322 (((req)->type & (arch_register_req_type_ ## kind)) != 0)
325 * Expresses requirements to register allocation for an operand.
327 struct arch_register_req_t {
328 arch_register_req_type_t type; /**< The type of the constraint. */
329 const arch_register_class_t *cls; /**< The register class this constraint
331 const unsigned *limited; /**< allowed register bitset */
332 unsigned other_same; /**< Bitmask of ins which should use the
333 same register (should_be_same). */
334 unsigned other_different; /**< Bitmask of ins which shall use a
336 (must_be_different) */
337 unsigned char width; /**< specifies how many sequential
338 registers are required */
341 static inline int reg_reqs_equal(const arch_register_req_t *req1,
342 const arch_register_req_t *req2)
347 if (req1->type != req2->type
348 || req1->cls != req2->cls
349 || req1->other_same != req2->other_same
350 || req1->other_different != req2->other_different)
353 if (req1->limited != NULL) {
356 if (req2->limited == NULL)
359 n_regs = arch_register_class_n_regs(req1->cls);
360 if (!rbitsets_equal(req1->limited, req2->limited, n_regs))
368 * An inverse operation returned by the backend
370 struct arch_inverse_t {
371 int n; /**< count of nodes returned in nodes array */
372 int costs; /**< costs of this remat */
374 /** nodes for this inverse operation. shall be in schedule order.
375 * last element is the target value */
379 struct arch_irn_ops_t {
383 * @param irn The node.
384 * @return A classification.
386 arch_irn_class_t (*classify)(const ir_node *irn);
389 * Get the entity on the stack frame this node depends on.
390 * @param irn The node in question.
391 * @return The entity on the stack frame or NULL, if the node does not have
392 * a stack frame entity.
394 ir_entity *(*get_frame_entity)(const ir_node *irn);
397 * Set the offset of a node carrying an entity on the stack frame.
398 * @param irn The node.
399 * @param offset The offset of the node's stack frame entity.
401 void (*set_frame_offset)(ir_node *irn, int offset);
404 * Returns the delta of the stackpointer for nodes that increment or
405 * decrement the stackpointer with a constant value. (push, pop
406 * nodes on most architectures).
407 * A positive value stands for an expanding stack area, a negative value for
410 * @param irn The node
411 * @return 0 if the stackpointer is not modified with a constant
412 * value, otherwise the increment/decrement value
414 int (*get_sp_bias)(const ir_node *irn);
417 * Returns an inverse operation which yields the i-th argument
418 * of the given node as result.
420 * @param irn The original operation
421 * @param i Index of the argument we want the inverse operation to
423 * @param inverse struct to be filled with the resulting inverse op
424 * @param obstack The obstack to use for allocation of the returned nodes
426 * @return The inverse operation or NULL if operation invertible
428 arch_inverse_t *(*get_inverse)(const ir_node *irn, int i,
429 arch_inverse_t *inverse,
430 struct obstack *obstack);
433 * Get the estimated cycle count for @p irn.
435 * @param irn The node.
436 * @return The estimated cycle count for this operation
438 int (*get_op_estimated_cost)(const ir_node *irn);
441 * Asks the backend whether operand @p i of @p irn can be loaded form memory
444 * @param irn The node.
445 * @param i Index of the argument we would like to know whether @p irn
446 * can load it form memory internally
447 * @return nonzero if argument can be loaded or zero otherwise
449 int (*possible_memory_operand)(const ir_node *irn, unsigned int i);
452 * Ask the backend to assimilate @p reload of operand @p i into @p irn.
454 * @param irn The node.
455 * @param spill The spill.
456 * @param i The position of the reload.
458 void (*perform_memory_operand)(ir_node *irn, ir_node *spill,
463 * Architecture interface.
465 struct arch_isa_if_t {
467 * Initialize the isa interface.
468 * @param file_handle the file handle to write the output to
469 * @return a new isa instance
471 arch_env_t *(*init)(FILE *file_handle);
474 * Free the isa instance.
476 void (*done)(void *self);
479 * Called directly after initialization. Backend should handle all
482 void (*handle_intrinsics)(void);
485 * Get the register class which shall be used to store a value of a given
487 * @param self The this pointer.
488 * @param mode The mode in question.
489 * @return A register class which can hold values of the given mode.
491 const arch_register_class_t *(*get_reg_class_for_mode)(const ir_mode *mode);
494 * Get the ABI restrictions for procedure calls.
495 * @param self The this pointer.
496 * @param call_type The call type of the method (procedure) in question.
497 * @param p The array of parameter locations to be filled.
499 void (*get_call_abi)(const void *self, ir_type *call_type,
503 * Get the necessary alignment for storing a register of given class.
504 * @param self The isa object.
505 * @param cls The register class.
506 * @return The alignment in bytes.
508 int (*get_reg_class_alignment)(const arch_register_class_t *cls);
511 * A "static" function, returns the frontend settings
512 * needed for this backend.
514 const backend_params *(*get_params)(void);
517 * Return an ordered list of irgs where code should be generated for.
518 * If NULL is returned, all irg will be taken into account and they will be
519 * generated in an arbitrary order.
520 * @param self The isa object.
521 * @param irgs A flexible array ARR_F of length 0 where the backend can
522 * append the desired irgs.
523 * @return A flexible array ARR_F containing all desired irgs in the
526 ir_graph **(*get_backend_irg_list)(const void *self, ir_graph ***irgs);
529 * mark node as rematerialized
531 void (*mark_remat)(ir_node *node);
534 * parse an assembler constraint part and set flags according to its nature
535 * advances the *c pointer to point to the last parsed character (so if you
536 * parse a single character don't advance c)
538 asm_constraint_flags_t (*parse_asm_constraint)(const char **c);
541 * returns true if the string is a valid clobbered (register) in this
544 int (*is_valid_clobber)(const char *clobber);
547 * Initialize the code generator.
549 * @return A newly created code generator.
551 void (*init_graph)(ir_graph *irg);
554 * return node used as base in pic code addresses
556 ir_node* (*get_pic_base)(ir_graph *irg);
559 * Called before abi introduce.
561 void (*before_abi)(ir_graph *irg);
564 * Called, when the graph is being normalized.
566 void (*prepare_graph)(ir_graph *irg);
569 * Called before register allocation.
571 void (*before_ra)(ir_graph *irg);
574 * Called after register allocation.
576 void (*after_ra)(ir_graph *irg);
579 * Called directly before done is called. This should be the last place
580 * where the irg is modified.
582 void (*finish)(ir_graph *irg);
585 * Called after everything happened. This call should emit the final
586 * assembly code but avoid changing the irg.
587 * The code generator must also be de-allocated here.
589 void (*emit)(ir_graph *irg);
592 #define arch_env_done(env) ((env)->impl->done(env))
593 #define arch_env_handle_intrinsics(env) \
594 do { if((env)->impl->handle_intrinsics != NULL) (env)->impl->handle_intrinsics(); } while(0)
595 #define arch_env_get_reg_class_for_mode(env,mode) ((env)->impl->get_reg_class_for_mode((mode)))
596 #define arch_env_get_call_abi(env,tp,abi) ((env)->impl->get_call_abi((env), (tp), (abi)))
597 #define arch_env_get_reg_class_alignment(env,cls) ((env)->impl->get_reg_class_alignment((cls)))
598 #define arch_env_get_params(env) ((env)->impl->get_params())
599 #define arch_env_get_allowed_execution_units(env,irn) ((env)->impl->get_allowed_execution_units((irn)))
600 #define arch_env_get_machine(env) ((env)->impl->get_machine(env))
601 #define arch_env_get_backend_irg_list(env,irgs) ((env)->impl->get_backend_irg_list((env), (irgs)))
602 #define arch_env_parse_asm_constraint(env,c) ((env)->impl->parse_asm_constraint((c))
603 #define arch_env_is_valid_clobber(env,clobber) ((env)->impl->is_valid_clobber((clobber))
604 #define arch_env_mark_remat(env,node) \
605 do { if ((env)->impl->mark_remat != NULL) (env)->impl->mark_remat((node)); } while(0)
611 const arch_isa_if_t *impl;
612 unsigned n_registers; /**< number of registers */
613 const arch_register_t *registers; /**< register array */
614 unsigned n_register_classes; /**< number of register classes*/
615 const arch_register_class_t *register_classes; /**< register classes */
616 const arch_register_t *sp; /**< The stack pointer register. */
617 const arch_register_t *bp; /**< The base pointer register. */
618 const arch_register_class_t *link_class; /**< The static link pointer
620 int stack_dir; /**< -1 for decreasing, 1 for
622 int stack_alignment; /**< power of 2 stack alignment */
623 const be_main_env_t *main_env; /**< the be main environment */
624 int spill_cost; /**< cost for a be_Spill node */
625 int reload_cost; /**< cost for a be_Reload node */
626 bool custom_abi : 1; /**< backend does all abi handling
627 and does not need the generic
628 stuff from beabi.h/.c */
631 static inline unsigned arch_irn_get_n_outs(const ir_node *node)
633 backend_info_t *info = be_get_info(node);
634 if (info->out_infos == NULL)
637 return ARR_LEN(info->out_infos);
640 static inline const arch_irn_ops_t *get_irn_ops_simple(const ir_node *node)
642 const ir_op *ops = get_irn_op(node);
643 const arch_irn_ops_t *be_ops = get_op_ops(ops)->be_ops;
644 assert(!is_Proj(node));
648 static inline const arch_register_req_t *arch_get_register_req_out(
652 backend_info_t *info;
654 /* you have to query the Proj nodes for the constraints (or use
655 * arch_get_out_register_req. Querying a mode_T node and expecting
656 * arch_no_register_req is a bug in your code! */
657 assert(get_irn_mode(irn) != mode_T);
660 pos = get_Proj_proj(irn);
661 irn = get_Proj_pred(irn);
664 info = be_get_info(irn);
665 if (info->out_infos == NULL)
666 return arch_no_register_req;
668 return info->out_infos[pos].req;
671 static inline bool arch_irn_is_ignore(const ir_node *irn)
673 const arch_register_req_t *req = arch_get_register_req_out(irn);
674 return !!(req->type & arch_register_req_type_ignore);
677 static inline bool arch_irn_consider_in_reg_alloc(
678 const arch_register_class_t *cls, const ir_node *node)
680 const arch_register_req_t *req = arch_get_register_req_out(node);
683 !(req->type & arch_register_req_type_ignore);
687 * Get register constraints for an operand at position @p
689 static inline const arch_register_req_t *arch_get_in_register_req(
690 const ir_node *node, int pos)
692 const backend_info_t *info = be_get_info(node);
693 if (info->in_reqs == NULL)
694 return arch_no_register_req;
695 return info->in_reqs[pos];
699 * Get register constraint for a produced result (the @p pos result)
701 static inline const arch_register_req_t *arch_get_out_register_req(
702 const ir_node *node, int pos)
704 const backend_info_t *info = be_get_info(node);
705 if (info->out_infos == NULL)
706 return arch_no_register_req;
707 return info->out_infos[pos].req;
710 static inline void arch_set_out_register_req(ir_node *node, int pos,
711 const arch_register_req_t *req)
713 backend_info_t *info = be_get_info(node);
714 assert(pos < (int) arch_irn_get_n_outs(node));
715 info->out_infos[pos].req = req;
718 static inline void arch_set_in_register_reqs(ir_node *node,
719 const arch_register_req_t **in_reqs)
721 backend_info_t *info = be_get_info(node);
722 info->in_reqs = in_reqs;
725 static inline const arch_register_req_t **arch_get_in_register_reqs(
728 backend_info_t *info = be_get_info(node);
729 return info->in_reqs;
733 * Iterate over all values defined by an instruction.
734 * Only looks at values in a certain register class where the requirements
735 * are not marked as ignore.
736 * Executes @p code for each definition.
738 #define be_foreach_definition_(node, cls, value, code) \
740 if (get_irn_mode(node) == mode_T) { \
741 const ir_edge_t *edge_; \
742 foreach_out_edge(node, edge_) { \
743 const arch_register_req_t *req_; \
744 value = get_edge_src_irn(edge_); \
745 req_ = arch_get_register_req_out(value); \
746 if (req_->cls != cls) \
751 const arch_register_req_t *req_ = arch_get_register_req_out(node); \
753 if (req_->cls == cls) { \
759 #define be_foreach_definition(node, cls, value, code) \
760 be_foreach_definition_(node, cls, value, \
761 if (req_->type & arch_register_req_type_ignore) \