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