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