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