5e309661dae10f6e5d45719849602103a45624a6
[libfirm] / ir / be / bearch.h
1
2 #ifndef _FIRM_BEARCH_H
3 #define _FIRM_BEARCH_H
4
5 #include "firm_config.h"
6
7 #include "irnode.h"
8 #include "irmode.h"
9
10 #include "hashptr.h"
11 #include "fourcc.h"
12 #include "set.h"
13 #include "list.h"
14 #include "ident.h"
15
16 struct _bitset_t;
17
18 typedef struct _arch_register_class_t   arch_register_class_t;
19 typedef struct _arch_register_t         arch_register_t;
20 typedef struct _arch_enum_t             arch_enum_t;
21 typedef struct _arch_enum_member_t      arch_enum_member_t;
22 typedef struct _arch_isa_if_t           arch_isa_if_t;
23 typedef struct _arch_env_t              arch_env_t;
24 typedef struct _arch_irn_ops_t          arch_irn_ops_t;
25 typedef struct _arch_irn_handler_t      arch_irn_handler_t;
26
27 struct _be_node_factory_t;
28
29 typedef enum _arch_register_type_t {
30         arch_register_type_none = 0,
31   arch_register_type_write_invariant,
32         arch_register_type_caller_saved,    /**< The register must be saved by the caller
33                                         upon a function call. It thus can be overwritten
34                                                                                                                                                                                                 in the called function. */
35         arch_register_type_callee_saved,    /**< The register must be saved by the called function,
36                                         it thus survives a function call. */
37         arch_register_type_ignore           /**< Do not consider this register when allocating. */
38 } arch_register_type_t;
39
40 /**
41  * A register.
42  */
43 struct _arch_register_t {
44   const char *name;                         /**< The name of the register. */
45         const arch_register_class_t *reg_class;   /**< The class the register belongs to. */
46         int index;                                                                                                                              /**< The index of the register in the class. */
47         arch_register_type_t type;                /**< The type of the register. */
48   void *data;                               /**< Custom data. */
49 };
50
51 static INLINE const arch_register_class_t *
52 _arch_register_get_class(const arch_register_t *reg)
53 {
54   return reg->reg_class;
55 }
56
57 static INLINE int _arch_register_get_index(const arch_register_t *reg)
58 {
59   return reg->index;
60 }
61
62 #define arch_register_get_class(reg)      _arch_register_get_class(reg)
63 #define arch_register_get_index(reg)      _arch_register_get_index(reg)
64 #define arch_register_get_name(reg)       ((reg)->name)
65
66 /**
67  * A class of registers.
68  * Like general purpose or floating point.
69  */
70 struct _arch_register_class_t {
71   const char *name;               /**< The name of the register. */
72         int n_regs;                                                                   /**< Number of registers in this class. */
73         const arch_register_t *regs;    /**< The array of registers. */
74 };
75
76 #define arch_register_class_n_regs(cls) ((cls)->n_regs)
77
78 /**
79  * Put all registers in a class into a bitset.
80  * @param cls The class.
81  * @param bs The bitset. May be NULL.
82  * @return The number of registers in the class.
83  */
84 extern int arch_register_class_put(const arch_register_class_t *cls,
85     struct _bitset_t *bs);
86
87 static INLINE const arch_register_t *
88 _arch_register_for_index(const arch_register_class_t *cls, int idx)
89 {
90         assert(0 <= idx && idx < cls->n_regs);
91         return &cls->regs[idx];
92 }
93
94 #define arch_register_for_index(cls, idx) \
95   _arch_register_for_index(cls, idx)
96
97 /**
98  * Get the register set for a register class.
99  * @param cls The register class.
100  * @return The set containing all registers in the class.
101  */
102 #define arch_get_register_set_for_class(cls) ((cls)->set)
103
104 /**
105  * An immediate.
106  */
107 struct _arch_immediate_t {
108   const char *name;         /**< The name of the immediate. */
109         ir_mode *mode;                                          /**< The mode of the immediate. */
110 };
111
112 /**
113  * The member of an enum.
114  */
115 struct _arch_enum_member_t {
116         arch_enum_t *enm;                                       /**< The enum, this member belongs to. */
117 };
118
119 /**
120  * An enumeration operand type.
121  *
122  * Enumeration operand types can be used to describe the variants
123  * of an instruction, like giving the cases for a compare (gt, lt,
124  * eq, ...) some other special attributes of an instruction.
125  */
126 struct _arch_enum_t {
127         int n_members;                                                                          /**< The number of members in this enum. */
128         arch_enum_member_t *members[1];         /**< The array of members. */
129 };
130
131 typedef enum _arch_operand_type_t {
132   arch_operand_type_invalid,
133   arch_operand_type_memory,
134   arch_operand_type_register,
135   arch_operand_type_immediate,
136   arch_operand_type_symconst,
137         arch_operand_type_last
138 } arch_operand_type_t;
139
140 /**
141  * Different types of register allocation requirements.
142  */
143 typedef enum _arch_register_req_type_t {
144   arch_register_req_type_none = 0,        /** No register requirement. */
145
146   arch_register_req_type_normal = 1,      /** All registers in the class
147                                             are allowed. */
148
149   arch_register_req_type_limited = 2,     /** Only a real subset of
150                                             the class is allowed. */
151
152   arch_register_req_type_equal = 4,       /** The register must equal
153                                             another one at the node. */
154
155   arch_register_req_type_unequal = 8,     /** The register must be unequal
156                                             to some other at the node. */
157
158   arch_register_req_type_pair = 16        /** The register is part of a
159                                             register pair. */
160 } arch_register_req_type_t;
161
162 #define arch_register_req_is_constr(x) \
163   ((x)->type & (arch_register_req_type_pair + arch_register_req_type_limited - 1) != 0)
164
165 /**
166  * Expresses requirements to register allocation for an operand.
167  */
168 typedef struct _arch_register_req_t {
169   arch_register_req_type_t type;          /** The type of the constraint. */
170   const arch_register_class_t *cls;       /** The register class this
171                                             constraint belongs to. */
172   union {
173     int (*limited)(const ir_node *irn, int pos, struct _bitset_t *bs);
174                                           /** In case of the 'limited'
175                                             constraint, this function
176                                             must put all allowable
177                                             registers in the bitset and
178                                             return the number of registers
179                                             in the bitset. */
180
181     int pos;                             /** In case of the equal constraint,
182                                             this gives the position of the
183                                             operand to which the register of
184                                             this should be equal to. Same for
185                                             unequal. */
186   } data;
187 } arch_register_req_t;
188
189 /**
190  * Certain node classes which are relevent for the register allocator.
191  */
192 typedef enum _arch_irn_class_t {
193   arch_irn_class_normal,
194   arch_irn_class_spill,
195   arch_irn_class_reload,
196   arch_irn_class_copy,
197   arch_irn_class_perm
198 } arch_irn_class_t;
199
200 /*
201  * Some words about positions and indices:
202  *
203  * Firm has the policy "One node per value", that's why there are
204  * Proj nodes. This view has its advantages, but in a backend
205  * setting where we talk about instructions (which can also have
206  * multiple results and not a single Tuple value) this is sometimes
207  * hard.
208  *
209  * Each node representing an instruction must provide information
210  * about the kind of its operands (where operands mean both input
211  * and output operands). Such an operand is addressed with a position
212  * which is infact a tuple {in, out} x N. The fact that a position
213  * is an input/output operand is encoded in the sign, so input operands
214  * go from 0..n-1 and output operands from -1..-m if the
215  * instruction has n input and m output operands.
216  */
217
218 #define _BEARCH_TRANSFORM_INDEX(cmp, index) ((index) cmp 0 ? -((index) + 1) : (index))
219
220 /**
221  * Make an in position from an index.
222  * @param index The index.
223  * @return The position representing the index as an in operand.
224  */
225 #define arch_pos_make_in(index)   _BEARCH_TRANSFORM_INDEX(<, index)
226
227 /**
228  * Make an out position from an index.
229  * @param index The index.
230  * @return The position representing the index as an out operand.
231  */
232 #define arch_pos_make_out(index)  _BEARCH_TRANSFORM_INDEX(>=, index)
233
234 /**
235  * Check, if a position denotes an input operand.
236  * @param pos The position.
237  * @return 1, if the position denotes an input operand 0 if not.
238  */
239 #define arch_pos_is_in(pos)       ((pos) >= 0)
240
241 /**
242  * Check, if a position denotes an output operand.
243  * @param pos The position.
244  * @return 1, if the position denotes an output operand 0 if not.
245  */
246 #define arch_pos_is_out(pos)      (!arch_pos_is_in(pos))
247
248 /**
249  * Get the index of a position.
250  * @param pos The position.
251  * @return The index of the position.
252  */
253 #define arch_pos_get_index(pos)   _BEARCH_TRANSFORM_INDEX(<, pos)
254
255 struct _arch_irn_ops_t {
256
257   /**
258    * Get the register requirements for a given operand.
259    * @param self The self pointer.
260    * @param irn The node.
261    * @param pos The operand's position.
262    * @return    The register requirements for the selected operand.
263    *            The pointer returned is never NULL.
264    */
265   const arch_register_req_t *(*get_irn_reg_req)(const arch_irn_ops_t *self,
266       arch_register_req_t *req,
267       const ir_node *irn, int pos);
268
269   /**
270    * Get the number of operands of a node.
271    * @param irn     The node.
272    * @param in_out  Denotes wither input (a number >= 0) or
273    *                output (a number < 0).
274    * @return        The number of operands for either in, or output.
275    */
276   int (*get_n_operands)(const arch_irn_ops_t *self, const ir_node *irn, int in_out);
277
278   /**
279    * Set the register for an output operand.
280    * @param irn The node.
281    * @param pos The position of the output operand.
282    * @param reg The register allocated to that operand.
283    * @note      If the operand is not a register operand,
284    *            the call is ignored.
285    */
286   void (*set_irn_reg)(const arch_irn_ops_t *self, ir_node *irn,
287       int idx, const arch_register_t *reg);
288
289   /**
290    * Get the register allocated for an output operand.
291    * @param irn The node.
292    * @param pos The index of the output operand.
293    * @return    The register allocated at that operand. NULL, if
294    *            the operand was no register operand or
295    *            @c arch_register_invalid, if no register has yet been
296    *            allocated for this node.
297    */
298   const arch_register_t *(*get_irn_reg)(const arch_irn_ops_t *self,
299       const ir_node *irn, int idx);
300
301   /**
302    * Classify the node.
303    * @param irn The node.
304    * @return A classification.
305    */
306   arch_irn_class_t (*classify)(const arch_irn_ops_t *self, const ir_node *irn);
307
308 };
309
310 /**
311  * Get the register requirements for a node.
312  * @param env The architecture environment.
313  * @param req A pointer to a requirements structure, where the data can
314  *            be put into.
315  * @param irn The node.
316  * @param pos The position of the operand you're interested in.
317  * @return    A pointer to the register requirements which may <b>not</b>
318  *            neccessarily be equal to @p req. If NULL is returned, the
319  *            operand was no register operand.
320  */
321 extern const arch_register_req_t *
322 arch_get_register_req(const arch_env_t *env, arch_register_req_t *req,
323     const ir_node *irn, int pos);
324
325 /**
326  * Check if an operand is a register operand.
327  * @param env The environment.
328  * @param irn The node.
329  * @param pos The position of the operand.
330  * @return 1, if the operand is significant for register allocation, 0
331  * if not.
332  */
333 extern int arch_is_register_operand(const arch_env_t *env,
334     const ir_node *irn, int pos);
335
336 /**
337  * Get the number of allocatable registers concerning
338  * a register class for an operand of a node.
339  * @param env The environment.
340  * @param irn The node.
341  * @param pos The postition of the node's operand.
342  * @param cls The register class.
343  * @param bs  The bitset all allocatable registers shall be put into.
344  *            Note, that you can also pass NULL here. If you don't,
345  *            make sure, the bitset is as large as the register class
346  *            has registers.
347  * @return    The amount of registers allocatable for that operand.
348  */
349 extern int arch_get_allocatable_regs(const arch_env_t *env, const ir_node *irn,
350     int pos, const arch_register_class_t *cls, struct _bitset_t *bs);
351
352 /**
353  * Check, if a register is assignable to an operand of a node.
354  * @param env The architecture environment.
355  * @param irn The node.
356  * @param pos The position of the operand.
357  * @param reg The register.
358  * @return    1, if the register might be allocated to the operand 0 if not.
359  */
360 extern int arch_reg_is_allocatable(const arch_env_t *env,
361     const ir_node *irn, int pos, const arch_register_t *reg);
362
363 /**
364  * Get the register class of an operand of a node.
365  * @param env The architecture environment.
366  * @param irn The node.
367  * @param idx The position of the operand.
368  * @return    The register class of the operand or NULL, if
369  *            operand is a non-register operand.
370  */
371 extern const arch_register_class_t *
372 arch_get_irn_reg_class(const arch_env_t *env, const ir_node *irn, int pos);
373
374 /**
375  * Get the register allocated at a certain output operand of a node.
376  * @param env The arch nvironment.
377  * @param irn The node.
378  * @param idx The index of the output operand.
379  * @return    The register allocated for this operand
380  */
381 extern const arch_register_t *
382 arch_get_irn_register(const arch_env_t *env, const ir_node *irn, int idx);
383
384 /**
385  * Set the register for a certain output operand.
386  * @param env The architecture environment.
387  * @param irn The node.
388  * @param idx The index of the output operand.
389  * @param reg The register.
390  */
391 extern void arch_set_irn_register(const arch_env_t *env,
392     ir_node *irn, int idx, const arch_register_t *reg);
393
394 #define arch_irn_has_reg_class(env, irn, pos, cls) \
395   ((cls) == arch_get_irn_reg_class(env, irn, pos))
396
397 /**
398  * Somebody who can be asked about nodes.
399  */
400 struct _arch_irn_handler_t {
401
402   /**
403     * Get the operations of an irn.
404     * @param self The handler from which the method is invoked.
405     * @param irn Some node.
406     * @return Operations for that irn.
407     */
408   const arch_irn_ops_t *(*get_irn_ops)(const arch_irn_handler_t *handler,
409       const ir_node *irn);
410
411 };
412
413 /**
414  * Architecture interface.
415  */
416 struct _arch_isa_if_t {
417
418   /**
419    * Initialize the isa interface.
420    */
421   void (*init)(void);
422
423   /**
424    * Get the the number of register classes in the isa.
425    * @return The number of register classes.
426    */
427   int (*get_n_reg_class)(void);
428
429   /**
430    * Get the i-th register class.
431    * @param i The number of the register class.
432    * @return The register class.
433    */
434   const arch_register_class_t *(*get_reg_class)(int i);
435
436 };
437
438 #define ARCH_MAX_HANDLERS         8
439
440 /**
441  * Environment for the architecture infrastructure.
442  * Keep this everywhere you're going.
443  */
444 struct _arch_env_t {
445   const struct _be_node_factory_t *node_factory;  /**< The node factory for be nodes. */
446   const arch_isa_if_t *isa;               /**< The isa about which everything is. */
447
448   arch_irn_handler_t const *handlers[ARCH_MAX_HANDLERS]; /**< The handlers are organized as
449                                                            a stack. */
450
451   int handlers_tos;                                   /**< The stack pointer of the handler
452                                                         stack. */
453 };
454
455 /**
456  * Get the isa of an arch environment.
457  * @param env The environment.
458  * @return The isa with which the env was initialized with.
459  */
460 #define arch_env_get_isa(env)   ((env)->isa)
461
462 /**
463  * Initialize the architecture environment struct.
464  * @param isa The isa which shall be put into the environment.
465  * @return The environment.
466  */
467 extern arch_env_t *arch_env_init(arch_env_t *env, const arch_isa_if_t *isa);
468
469 /**
470  * Add a node handler to the environment.
471  * @param env The environment.
472  * @param handler A node handler.
473  * @return The environment itself.
474  */
475 extern arch_env_t *arch_env_add_irn_handler(arch_env_t *env,
476     const arch_irn_handler_t *handler);
477
478 #endif /* _FIRM_BEARCH_H */