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