b769ef9c92a1ed52df6180d17576741df05dfc50
[libfirm] / ir / be / bearch.h
1 /*
2  * Copyright (C) 1995-2011 University of Karlsruhe.  All right reserved.
3  *
4  * This file is part of libFirm.
5  *
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.
10  *
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.
14  *
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
17  * PURPOSE.
18  */
19
20 /**
21  * @file
22  * @brief       Processor architecture specification.
23  * @author      Sebastian Hack
24  */
25 #ifndef FIRM_BE_BEARCH_H
26 #define FIRM_BE_BEARCH_H
27
28 #include <stdbool.h>
29
30 #include "firm_types.h"
31 #include "bitset.h"
32 #include "obst.h"
33 #include "raw_bitset.h"
34 #include "irop_t.h"
35
36 #include "be_types.h"
37 #include "beinfo.h"
38 #include "be.h"
39 #include "beirg.h"
40 #include "error.h"
41
42 /**
43  * this constant is returned by the get_sp_bias functions if the stack
44  * is reset (usually because the frame pointer is copied to the stack
45  * pointer
46  */
47 #define SP_BIAS_RESET      INT_MIN
48
49 typedef enum arch_register_class_flags_t {
50         arch_register_class_flag_none      = 0,
51         /** don't do automatic register allocation for this class */
52         arch_register_class_flag_manual_ra = 1U << 0,
53         /** the register models an abstract state (example: fpu rounding mode) */
54         arch_register_class_flag_state     = 1U << 1
55 } arch_register_class_flags_t;
56 ENUM_BITSET(arch_register_class_flags_t)
57
58 typedef enum arch_register_type_t {
59         arch_register_type_none         = 0,
60         /** Do not consider this register when allocating. */
61         arch_register_type_ignore       = 1U << 0,
62         /** The emitter can choose an arbitrary register. The register fulfills any
63          * register constraints as long as the register class matches */
64         arch_register_type_joker        = 1U << 1,
65         /** This is just a virtual register. Virtual registers fulfill any register
66          * constraints as long as the register class matches. It is a allowed to
67          * have multiple definitions for the same virtual register at a point */
68         arch_register_type_virtual      = 1U << 2,
69         /** The register represents a state that should be handled by bestate
70          * code */
71         arch_register_type_state        = 1U << 3,
72 } arch_register_type_t;
73 ENUM_BITSET(arch_register_type_t)
74
75 /**
76  * Different types of register allocation requirements.
77  */
78 typedef enum arch_register_req_type_t {
79         /** No register requirement. */
80         arch_register_req_type_none              = 0,
81         /** All registers in the class are allowed. */
82         arch_register_req_type_normal            = 1U << 0,
83         /** Only a real subset of the class is allowed. */
84         arch_register_req_type_limited           = 1U << 1,
85         /** The register should be equal to another one at the node. */
86         arch_register_req_type_should_be_same    = 1U << 2,
87         /** The register must be unequal from some other at the node. */
88         arch_register_req_type_must_be_different = 1U << 3,
89         /** The registernumber should be aligned (in case of multiregister values)*/
90         arch_register_req_type_aligned           = 1U << 4,
91         /** ignore while allocating registers */
92         arch_register_req_type_ignore            = 1U << 5,
93         /** the output produces a new value for the stack pointer
94          * (this is not really a constraint but a marker to guide the stackpointer
95          * rewiring logic) */
96         arch_register_req_type_produces_sp       = 1U << 6,
97 } arch_register_req_type_t;
98 ENUM_BITSET(arch_register_req_type_t)
99
100 extern const arch_register_req_t *arch_no_register_req;
101
102 /**
103  * Print information about a register requirement in human readable form
104  * @param F   output stream/file
105  * @param req The requirements structure to format.
106  */
107 void arch_dump_register_req(FILE *F, const arch_register_req_t *req,
108                             const ir_node *node);
109
110 void arch_dump_register_reqs(FILE *F, const ir_node *node);
111 void arch_dump_reqs_and_registers(FILE *F, const ir_node *node);
112
113 void arch_set_frame_offset(ir_node *irn, int bias);
114
115 ir_entity *arch_get_frame_entity(const ir_node *irn);
116 int        arch_get_sp_bias(ir_node *irn);
117
118 int             arch_get_op_estimated_cost(const ir_node *irn);
119 arch_inverse_t *arch_get_inverse(const ir_node *irn, int i,
120                                  arch_inverse_t *inverse,
121                                  struct obstack *obstack);
122 int             arch_possible_memory_operand(const ir_node *irn,
123                                              unsigned int i);
124 void            arch_perform_memory_operand(ir_node *irn, ir_node *spill,
125                                             unsigned int i);
126
127 /**
128  * Get the register allocated for a value.
129  */
130 const arch_register_t *arch_get_irn_register(const ir_node *irn);
131
132 /**
133  * Assign register to a value
134  */
135 void arch_set_irn_register(ir_node *irn, const arch_register_t *reg);
136
137 /**
138  * Set the register for a certain output operand.
139  */
140 void arch_set_irn_register_out(ir_node *irn, int pos, const arch_register_t *r);
141
142 const arch_register_t *arch_get_irn_register_out(const ir_node *irn, int pos);
143 const arch_register_t *arch_get_irn_register_in(const ir_node *irn, int pos);
144
145 /**
146  * Get register constraints for an operand at position @p
147  */
148 static inline const arch_register_req_t *arch_get_irn_register_req_in(
149                 const ir_node *node, int pos)
150 {
151         const backend_info_t *info = be_get_info(node);
152         if (info->in_reqs == NULL)
153                 return arch_no_register_req;
154         return info->in_reqs[pos];
155 }
156
157 /**
158  * Get register constraint for a produced result (the @p pos result)
159  */
160 static inline const arch_register_req_t *arch_get_irn_register_req_out(
161                 const ir_node *node, int pos)
162 {
163         const backend_info_t *info = be_get_info(node);
164         if (info->out_infos == NULL)
165                 return arch_no_register_req;
166         return info->out_infos[pos].req;
167 }
168
169 static inline void arch_set_irn_register_req_out(ir_node *node, int pos,
170                 const arch_register_req_t *req)
171 {
172         backend_info_t *info = be_get_info(node);
173         assert(pos < (int)ARR_LEN(info->out_infos));
174         info->out_infos[pos].req = req;
175 }
176
177 static inline void arch_set_irn_register_reqs_in(ir_node *node,
178                 const arch_register_req_t **reqs)
179 {
180         backend_info_t *info = be_get_info(node);
181         info->in_reqs = reqs;
182 }
183
184 static inline const arch_register_req_t **arch_get_irn_register_reqs_in(
185                 const ir_node *node)
186 {
187         backend_info_t *info = be_get_info(node);
188         return info->in_reqs;
189 }
190
191 const arch_register_req_t *arch_get_irn_register_req(const ir_node *node);
192
193 /**
194  * Get the flags of a node.
195  * @param irn The node.
196  * @return The flags.
197  */
198 arch_irn_flags_t arch_get_irn_flags(const ir_node *irn);
199
200 void arch_set_irn_flags(ir_node *node, arch_irn_flags_t flags);
201 void arch_add_irn_flags(ir_node *node, arch_irn_flags_t flags);
202
203 #define arch_irn_is(irn, flag) ((arch_get_irn_flags(irn) & arch_irn_flags_ ## flag) != 0)
204
205 static inline unsigned arch_get_irn_n_outs(const ir_node *node)
206 {
207         backend_info_t *info = be_get_info(node);
208         if (info->out_infos == NULL)
209                 return 0;
210
211         return (unsigned)ARR_LEN(info->out_infos);
212 }
213
214 /**
215  * Start codegeneration
216  */
217 arch_env_t *arch_env_begin_codegeneration(const arch_isa_if_t *isa,
218                                           be_main_env_t *main_env);
219
220 /**
221  * Register an instruction set architecture
222  */
223 void be_register_isa_if(const char *name, const arch_isa_if_t *isa);
224
225 /**
226  * A register.
227  */
228 struct arch_register_t {
229         const char                  *name;         /**< The name of the register. */
230         const arch_register_class_t *reg_class;    /**< The class of the register */
231         unsigned short               index;        /**< The index of the register in
232                                                         the class. */
233         unsigned short               global_index; /**< The global index this
234                                                                                                     register in the architecture. */
235         arch_register_type_t         type;         /**< The type of the register. */
236         /** register constraint allowing just this register */
237         const arch_register_req_t   *single_req;
238         /** register number in dwarf debugging format */
239         unsigned short               dwarf_number;
240 };
241
242 static inline const arch_register_class_t *arch_register_get_class(
243                 const arch_register_t *reg)
244 {
245         return reg->reg_class;
246 }
247
248 static inline unsigned arch_register_get_index(const arch_register_t *reg)
249 {
250         return reg->index;
251 }
252
253 static inline const char *arch_register_get_name(const arch_register_t *reg)
254 {
255         return reg->name;
256 }
257
258 /**
259  * A class of registers.
260  * Like general purpose or floating point.
261  */
262 struct arch_register_class_t {
263         unsigned                     index;   /**< index of this register class */
264         const char                  *name;    /**< The name of the register class.*/
265         unsigned                     n_regs;  /**< Number of registers in this
266                                                    class. */
267         ir_mode                     *mode;    /**< The mode of the register class.*/
268         const arch_register_t       *regs;    /**< The array of registers. */
269         arch_register_class_flags_t  flags;   /**< register class flags. */
270         const arch_register_req_t   *class_req;
271 };
272
273 /** return the number of registers in this register class */
274 #define arch_register_class_n_regs(cls) ((cls)->n_regs)
275
276 /** return the largest mode of this register class */
277 #define arch_register_class_mode(cls) ((cls)->mode)
278
279 /** return the name of this register class */
280 #define arch_register_class_name(cls) ((cls)->name)
281
282 /** return the index of this register class */
283 #define arch_register_class_index(cls)  ((cls)->index)
284
285 /** return the register class flags */
286 #define arch_register_class_flags(cls) ((cls)->flags)
287
288 static inline const arch_register_t *arch_register_for_index(
289                 const arch_register_class_t *cls, unsigned idx)
290 {
291         assert(idx < cls->n_regs);
292         return &cls->regs[idx];
293 }
294
295 /**
296  * Convenience macro to check for set constraints.
297  * @param req   A pointer to register requirements.
298  * @param kind  The kind of constraint to check for
299  *              (see arch_register_req_type_t).
300  * @return      1, If the kind of constraint is present, 0 if not.
301  */
302 #define arch_register_req_is(req, kind) \
303         (((req)->type & (arch_register_req_type_ ## kind)) != 0)
304
305 /**
306  * Expresses requirements to register allocation for an operand.
307  */
308 struct arch_register_req_t {
309         arch_register_req_type_t     type; /**< The type of the constraint. */
310         const arch_register_class_t *cls;  /**< The register class this constraint
311                                                 belongs to. */
312         const unsigned *limited;           /**< allowed register bitset
313                                                 (in case of wide-values this is
314                                                  only about the first register) */
315         unsigned other_same;               /**< Bitmask of ins which should use the
316                                                 same register (should_be_same). */
317         unsigned other_different;          /**< Bitmask of ins which shall use a
318                                                 different register
319                                                 (must_be_different) */
320         unsigned char width;               /**< specifies how many sequential
321                                                 registers are required */
322 };
323
324 static inline bool reg_reqs_equal(const arch_register_req_t *req1,
325                                   const arch_register_req_t *req2)
326 {
327         if (req1 == req2)
328                 return true;
329
330         if (req1->type              != req2->type            ||
331             req1->cls               != req2->cls             ||
332             req1->other_same        != req2->other_same      ||
333             req1->other_different   != req2->other_different ||
334             (req1->limited != NULL) != (req2->limited != NULL))
335                 return false;
336
337         if (req1->limited != NULL) {
338                 size_t const n_regs = arch_register_class_n_regs(req1->cls);
339                 if (!rbitsets_equal(req1->limited, req2->limited, n_regs))
340                         return false;
341         }
342
343         return true;
344 }
345
346 /**
347  * An inverse operation returned by the backend
348  */
349 struct arch_inverse_t {
350         int      n;       /**< count of nodes returned in nodes array */
351         int      costs;   /**< costs of this remat */
352
353         /** nodes for this inverse operation. shall be in schedule order.
354          * last element is the target value */
355         ir_node  **nodes;
356 };
357
358 struct arch_irn_ops_t {
359
360         /**
361          * Get the entity on the stack frame this node depends on.
362          * @param irn  The node in question.
363          * @return The entity on the stack frame or NULL, if the node does not have
364          *         a stack frame entity.
365          */
366         ir_entity *(*get_frame_entity)(const ir_node *irn);
367
368         /**
369          * Set the offset of a node carrying an entity on the stack frame.
370          * @param irn  The node.
371          * @param offset The offset of the node's stack frame entity.
372          */
373         void (*set_frame_offset)(ir_node *irn, int offset);
374
375         /**
376          * Returns the delta of the stackpointer for nodes that increment or
377          * decrement the stackpointer with a constant value. (push, pop
378          * nodes on most architectures).
379          * A positive value stands for an expanding stack area, a negative value for
380          * a shrinking one.
381          *
382          * @param irn       The node
383          * @return          0 if the stackpointer is not modified with a constant
384          *                  value, otherwise the increment/decrement value
385          */
386         int (*get_sp_bias)(const ir_node *irn);
387
388         /**
389          * Returns an inverse operation which yields the i-th argument
390          * of the given node as result.
391          *
392          * @param irn       The original operation
393          * @param i         Index of the argument we want the inverse operation to
394          *                  yield
395          * @param inverse   struct to be filled with the resulting inverse op
396          * @param obstack   The obstack to use for allocation of the returned nodes
397          *                  array
398          * @return          The inverse operation or NULL if operation invertible
399          */
400         arch_inverse_t *(*get_inverse)(const ir_node *irn, int i,
401                                        arch_inverse_t *inverse,
402                                        struct obstack *obstack);
403
404         /**
405          * Get the estimated cycle count for @p irn.
406          *
407          * @param irn  The node.
408          * @return     The estimated cycle count for this operation
409          */
410         int (*get_op_estimated_cost)(const ir_node *irn);
411
412         /**
413          * Asks the backend whether operand @p i of @p irn can be loaded form memory
414          * internally
415          *
416          * @param irn  The node.
417          * @param i    Index of the argument we would like to know whether @p irn
418          *             can load it form memory internally
419          * @return     nonzero if argument can be loaded or zero otherwise
420          */
421         int (*possible_memory_operand)(const ir_node *irn, unsigned int i);
422
423         /**
424          * Ask the backend to assimilate @p reload of operand @p i into @p irn.
425          *
426          * @param irn    The node.
427          * @param spill  The spill.
428          * @param i      The position of the reload.
429          */
430         void (*perform_memory_operand)(ir_node *irn, ir_node *spill,
431                                        unsigned int i);
432 };
433
434 /**
435  * Architecture interface.
436  */
437 struct arch_isa_if_t {
438         /**
439          * Initializes the isa interface. This is necessary before calling any
440          * other functions from this interface.
441          */
442         void (*init)(void);
443
444         /**
445          * Fress resources allocated by this isa interface.
446          */
447         void (*finish)(void);
448
449         /**
450          * Returns the frontend settings needed for this backend.
451          */
452         const backend_params *(*get_params)(void);
453
454         /**
455          * lowers current program for target. See the documentation for
456          * be_lower_for_target() for details.
457          */
458         void (*lower_for_target)(void);
459
460         /**
461          * parse an assembler constraint part and set flags according to its nature
462          * advances the *c pointer to point to the last parsed character (so if you
463          * parse a single character don't advance c)
464          */
465         asm_constraint_flags_t (*parse_asm_constraint)(const char **c);
466
467         /**
468          * returns true if the string is a valid clobbered (register) in this
469          * backend
470          */
471         int (*is_valid_clobber)(const char *clobber);
472
473         /**
474          * Start codegeneration
475          * @return a new isa instance
476          */
477         arch_env_t *(*begin_codegeneration)(const be_main_env_t *env);
478
479         /**
480          * Free the isa instance.
481          */
482         void (*end_codegeneration)(void *self);
483
484         /**
485          * Initialize the code generator for a graph
486          * @param irg  A graph
487          */
488         void (*init_graph)(ir_graph *irg);
489
490         /**
491          * Get the ABI restrictions for procedure calls.
492          * @param call_type   The call type of the method (procedure) in question.
493          * @param p           The array of parameter locations to be filled.
494          */
495         void (*get_call_abi)(ir_type *call_type, be_abi_call_t *abi);
496
497         /**
498          * mark node as rematerialized
499          */
500         void (*mark_remat)(ir_node *node);
501
502         /**
503          * return node used as base in pic code addresses
504          */
505         ir_node* (*get_pic_base)(ir_graph *irg);
506
507         /**
508          * Create a spill instruction. We assume that spill instructions
509          * do not need any additional registers and do not affect cpu-flags in any
510          * way.
511          * Construct a sequence of instructions after @p after (the resulting nodes
512          * are already scheduled).
513          * Returns a mode_M value which is used as input for a reload instruction.
514          */
515         ir_node *(*new_spill)(ir_node *value, ir_node *after);
516
517         /**
518          * Create a reload instruction. We assume that reload instructions do not
519          * need any additional registers and do not affect cpu-flags in any way.
520          * Constructs a sequence of instruction before @p before (the resulting
521          * nodes are already scheduled). A rewiring of users is not performed in
522          * this function.
523          * Returns a value representing the restored value.
524          */
525         ir_node *(*new_reload)(ir_node *value, ir_node *spilled_value,
526                                ir_node *before);
527
528         /**
529          * Checks if the given register is callee/caller saved.
530          * @deprecated, only necessary if backend still uses beabi functions
531          */
532         int (*register_saved_by)(const arch_register_t *reg, int callee);
533
534         /**
535          * Called directly after initialization. Backend should handle all
536          * intrinsics here.
537          */
538         void (*handle_intrinsics)(void);
539
540         /**
541          * Called before abi introduce.
542          */
543         void (*before_abi)(ir_graph *irg);
544
545         /**
546          * Called, when the graph is being normalized.
547          */
548         void (*prepare_graph)(ir_graph *irg);
549
550         /**
551          * Called before register allocation.
552          */
553         void (*before_ra)(ir_graph *irg);
554
555         /**
556          * Called directly before done is called. This should be the last place
557          * where the irg is modified.
558          */
559         void (*finish_graph)(ir_graph *irg);
560
561         /**
562          * Called after everything happened. This call should emit the final
563          * assembly code but avoid changing the irg.
564          */
565         void (*emit)(ir_graph *irg);
566 };
567
568 #define arch_env_end_codegeneration(env)               ((env)->impl->end_codegeneration(env))
569 #define arch_env_handle_intrinsics(env)                \
570         do { if((env)->impl->handle_intrinsics != NULL) (env)->impl->handle_intrinsics(); } while(0)
571 #define arch_env_get_call_abi(env,tp,abi)              ((env)->impl->get_call_abi((tp), (abi)))
572 #define arch_env_get_params(env)                       ((env)->impl->get_params())
573 #define arch_env_parse_asm_constraint(env,c)           ((env)->impl->parse_asm_constraint((c))
574 #define arch_env_is_valid_clobber(env,clobber)         ((env)->impl->is_valid_clobber((clobber))
575 #define arch_env_mark_remat(env,node) \
576         do { if ((env)->impl->mark_remat != NULL) (env)->impl->mark_remat((node)); } while(0)
577
578 #define arch_env_new_spill(env,value,after)            ((env)->impl->new_spill(value, after))
579 #define arch_env_new_reload(env,value,spilled,before)  ((env)->impl->new_reload(value, spilled, before))
580
581 /**
582  * ISA base class.
583  */
584 struct arch_env_t {
585         const arch_isa_if_t   *impl;
586         unsigned               n_registers;      /**< number of registers */
587         const arch_register_t *registers;        /**< register array */
588         unsigned               n_register_classes; /**< number of register classes*/
589         const arch_register_class_t *register_classes; /**< register classes */
590         const arch_register_t *sp;               /**< The stack pointer register. */
591         const arch_register_t *bp;               /**< The base pointer register. */
592         const arch_register_class_t *link_class; /**< The static link pointer
593                                                       register class. */
594         int                    stack_alignment;  /**< power of 2 stack alignment */
595         const be_main_env_t   *main_env;         /**< the be main environment */
596         int                    spill_cost;       /**< cost for a be_Spill node */
597         int                    reload_cost;      /**< cost for a be_Reload node */
598         bool                   custom_abi : 1;   /**< backend does all abi handling
599                                                       and does not need the generic
600                                                       stuff from beabi.h/.c */
601 };
602
603 static inline bool arch_irn_is_ignore(const ir_node *irn)
604 {
605         const arch_register_req_t *req = arch_get_irn_register_req(irn);
606         return req->type & arch_register_req_type_ignore;
607 }
608
609 static inline bool arch_irn_consider_in_reg_alloc(
610                 const arch_register_class_t *cls, const ir_node *node)
611 {
612         const arch_register_req_t *req = arch_get_irn_register_req(node);
613         return
614                 req->cls == cls &&
615                 !(req->type & arch_register_req_type_ignore);
616 }
617
618 /**
619  * Iterate over all values defined by an instruction.
620  * Only looks at values in a certain register class where the requirements
621  * are not marked as ignore.
622  * Executes @p code for each definition.
623  */
624 #define be_foreach_definition_(node, cls, value, code)                     \
625         do {                                                                   \
626         if (get_irn_mode(node) == mode_T) {                                    \
627                 foreach_out_edge(node, edge_) {                                    \
628                         const arch_register_req_t *req_;                               \
629                         value = get_edge_src_irn(edge_);                               \
630                         req_  = arch_get_irn_register_req(value);                      \
631                         if (req_->cls != cls)                                          \
632                                 continue;                                                  \
633                         code                                                           \
634                 }                                                                  \
635         } else {                                                               \
636                 const arch_register_req_t *req_ = arch_get_irn_register_req(node); \
637                 value = node;                                                      \
638                 if (req_->cls == cls) {                                            \
639                         code                                                           \
640                 }                                                                  \
641         }                                                                      \
642         } while (0)
643
644 #define be_foreach_definition(node, cls, value, code)                      \
645         be_foreach_definition_(node, cls, value,                               \
646                 if (req_->type & arch_register_req_type_ignore)                    \
647                         continue;                                                      \
648                 code                                                               \
649         )
650
651 static inline const arch_register_class_t *arch_get_irn_reg_class(
652                 const ir_node *node)
653 {
654         const arch_register_req_t *req = arch_get_irn_register_req(node);
655         return req->cls;
656 }
657
658 bool arch_reg_is_allocatable(const arch_register_req_t *req,
659                              const arch_register_t *reg);
660
661 #endif