3bac3891f4241b4f4afec87221cf395d68a8ff42
[libfirm] / ir / be / bearch.h
1 /*
2  * Copyright (C) 1995-2007 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 #ifndef _FIRM_BEARCH_H_
21 #define _FIRM_BEARCH_H_
22
23 #include "firm_types.h"
24 #include "bitset.h"
25 #include "be.h"
26
27 typedef struct arch_register_class_t     arch_register_class_t;
28 typedef struct arch_register_req_t       arch_register_req_t;
29 typedef struct arch_register_t           arch_register_t;
30 typedef struct arch_flag_t               arch_flag_t;
31 typedef struct arch_inverse_t            arch_inverse_t;
32 typedef struct arch_isa_if_t             arch_isa_if_t;
33 typedef struct arch_isa_t                arch_isa_t;
34 typedef struct arch_env_t                arch_env_t;
35 typedef struct arch_irn_ops_if_t         arch_irn_ops_if_t;
36 typedef struct arch_irn_ops_t            arch_irn_ops_t;
37 typedef struct arch_irn_handler_t        arch_irn_handler_t;
38 typedef struct arch_code_generator_t     arch_code_generator_t;
39 typedef struct arch_code_generator_if_t  arch_code_generator_if_t;
40
41 typedef enum arch_register_type_t {
42         arch_register_type_none         = 0,
43         arch_register_type_caller_save  = 1,  /**< The register must be saved by the caller
44                                              upon a function call. It thus can be overwritten
45                                              in the called function. */
46         arch_register_type_callee_save  = 2,  /**< The register must be saved by the caller
47                                              upon a function call. It thus can be overwritten
48                                              in the called function. */
49         arch_register_type_ignore       = 4,  /**< Do not consider this register when allocating. */
50         arch_register_type_joker        = 8,  /**< The emitter can choose an arbitrary register */
51         arch_register_type_virtual      = 16, /**< This is just a virtual register  */
52         arch_register_type_state        = 32,
53 } arch_register_type_t;
54
55 /**
56  * Put all registers in a class into a bitset.
57  * @param cls The class.
58  * @param bs The bitset. May be NULL.
59  * @return The number of registers in the class.
60  */
61 extern int arch_register_class_put(const arch_register_class_t *cls, bitset_t *bs);
62
63 typedef enum arch_operand_type_t {
64         arch_operand_type_invalid,
65         arch_operand_type_memory,
66         arch_operand_type_register,
67         arch_operand_type_immediate,
68         arch_operand_type_symconst,
69         arch_operand_type_last
70 } arch_operand_type_t;
71
72 /**
73  * Different types of register allocation requirements.
74  */
75 typedef enum arch_register_req_type_t {
76         arch_register_req_type_none                         = 0,  /**< No register requirement. */
77         arch_register_req_type_normal                       = 1,  /**< All registers in the class are allowed. */
78         arch_register_req_type_limited                      = 2,  /**< Only a real subset of the class is allowed. */
79         arch_register_req_type_should_be_same               = 4,  /**< The register should be equal another one at the node. */
80         arch_register_req_type_should_be_different          = 8,  /**< The register must be unequal to some other at the node. */
81         arch_register_req_type_should_be_different_from_all = 16, /**< The register must be different from all in's at the node */
82 } arch_register_req_type_t;
83
84 extern const arch_register_req_t *arch_no_register_req;
85
86 /**
87  * Format a register requirements information into a string.
88  * @param buf The string where to put it to.
89  * @param len The size of @p buf.
90  * @param req The requirements structure to format.
91  * @return    A pointer to buf.
92  */
93 extern char *arch_register_req_format(char *buf, size_t len, const arch_register_req_t *req, const ir_node *node);
94
95 /**
96  * Certain node classes which are relevant for the register allocator.
97  */
98 typedef enum arch_irn_class_t {
99         arch_irn_class_normal     = 1 << 0,
100         arch_irn_class_spill      = 1 << 1,
101         arch_irn_class_reload     = 1 << 2,
102         arch_irn_class_copy       = 1 << 3,
103         arch_irn_class_perm       = 1 << 4,
104         arch_irn_class_branch     = 1 << 5,
105         arch_irn_class_call       = 1 << 6,
106         arch_irn_class_const      = 1 << 7,
107         arch_irn_class_load       = 1 << 8,
108         arch_irn_class_store      = 1 << 9,
109         arch_irn_class_stackparam = 1 << 10,
110 } arch_irn_class_t;
111
112 /**
113  * Some flags describing a node in more detail.
114  */
115 typedef enum arch_irn_flags_t {
116         arch_irn_flags_none             = 0, /**< Node flags. */
117         arch_irn_flags_dont_spill       = 1, /**< This must not be spilled. */
118         arch_irn_flags_rematerializable = 2, /**< This can be replicated instead of spilled/reloaded. */
119         arch_irn_flags_ignore           = 4, /**< Ignore node during register allocation. */
120         arch_irn_flags_modify_sp        = 8, /**< I modify the stack pointer. */
121         arch_irn_flags_last             = arch_irn_flags_modify_sp
122 } arch_irn_flags_t;
123
124 /**
125  * Get the string representation of a flag.
126  * This functions does not handle or'ed bitmasks of flags.
127  * @param flag The flag.
128  * @return The flag as a string.
129  */
130 extern const char *arch_irn_flag_str(arch_irn_flags_t flag);
131
132 extern const arch_irn_ops_t *arch_get_irn_ops(const arch_env_t *env,
133                                               const ir_node *irn);
134
135 extern void arch_set_frame_offset(const arch_env_t *env, ir_node *irn, int bias);
136
137 extern ir_entity *arch_get_frame_entity(const arch_env_t *env, ir_node *irn);
138 extern void arch_set_frame_entity(const arch_env_t *env, ir_node *irn, ir_entity *ent);
139 extern int arch_get_sp_bias(const arch_env_t *env, ir_node *irn);
140
141 extern int arch_get_op_estimated_cost(const arch_env_t *env, const ir_node *irn);
142 extern arch_inverse_t *arch_get_inverse(const arch_env_t *env, const ir_node *irn, int i, arch_inverse_t *inverse, struct obstack *obstack);
143 extern int arch_possible_memory_operand(const arch_env_t *env, const ir_node *irn, unsigned int i);
144 extern void arch_perform_memory_operand(const arch_env_t *env, ir_node *irn, ir_node *spill, unsigned int i);
145
146 /**
147  * Get the register requirements for a node.
148  * @param env The architecture environment.
149  * @param req A pointer to a requirements structure, where the data can
150  *            be put into.
151  * @param irn The node.
152  * @param pos The position of the operand you're interested in.
153  * @return    A pointer to the register requirements which may <b>not</b>
154  *            neccessarily be equal to @p req. If NULL is returned, the
155  *            operand was no register operand.
156  */
157 extern const arch_register_req_t *
158 arch_get_register_req(const arch_env_t *env, const ir_node *irn, int pos);
159
160 /**
161  * Check if an operand is a register operand.
162  * @param env The environment.
163  * @param irn The node.
164  * @param pos The position of the operand.
165  * @return 1, if the operand is significant for register allocation, 0
166  * if not.
167  */
168 extern int arch_is_register_operand(const arch_env_t *env,
169     const ir_node *irn, int pos);
170
171 /**
172  * Get the number of allocatable registers concerning
173  * a register class for an operand of a node.
174  * @param env The environment.
175  * @param irn The node.
176  * @param pos The postition of the node's operand.
177  * @param bs  The bitset all allocatable registers shall be put into.
178  *            Note, that you can also pass NULL here. If you don't,
179  *            make sure, the bitset is as large as the register class
180  *            has registers.
181  * @return    The amount of registers allocatable for that operand.
182  */
183 extern int arch_get_allocatable_regs(const arch_env_t *env, const ir_node *irn, int pos, bitset_t *bs);
184
185 /**
186  * Put all registers which shall not be ignored by the register
187  * allocator in a bit set.
188  * @param env The arch env.
189  * @param cls The register class to consider.
190  * @param bs  The bit set to put the registers to.
191  */
192 extern void arch_put_non_ignore_regs(const arch_env_t *env, const arch_register_class_t *cls, bitset_t *bs);
193
194 /**
195  * Check, if a register is assignable to an operand of a node.
196  * @param env The architecture environment.
197  * @param irn The node.
198  * @param pos The position of the operand.
199  * @param reg The register.
200  * @return    1, if the register might be allocated to the operand 0 if not.
201  */
202 extern int arch_reg_is_allocatable(const arch_env_t *env,
203     const ir_node *irn, int pos, const arch_register_t *reg);
204
205 /**
206  * Get the register class of an operand of a node.
207  * @param env The architecture environment.
208  * @param irn The node.
209  * @param pos The position of the operand, -1 for the output.
210  * @return    The register class of the operand or NULL, if
211  *            operand is a non-register operand.
212  */
213 extern const arch_register_class_t *
214 arch_get_irn_reg_class(const arch_env_t *env, const ir_node *irn, int pos);
215
216 /**
217  * Get the register allocated at a certain output operand of a node.
218  * @param env The arch environment.
219  * @param irn The node.
220  * @return    The register allocated for this operand
221  */
222 extern const arch_register_t *
223 arch_get_irn_register(const arch_env_t *env, const ir_node *irn);
224
225 /**
226  * Set the register for a certain output operand.
227  * @param env The architecture environment.
228  * @param irn The node.
229  * @param idx The index of the output operand.
230  * @param reg The register.
231  */
232 extern void arch_set_irn_register(const arch_env_t *env, ir_node *irn,
233                 const arch_register_t *reg);
234
235 /**
236  * Classify a node.
237  * @param env The architecture environment.
238  * @param irn The node.
239  * @return A classification of the node.
240  */
241 extern arch_irn_class_t arch_irn_classify(const arch_env_t *env, const ir_node *irn);
242
243 #define arch_irn_class_is(env, irn, irn_class) ((arch_irn_classify(env, irn) & arch_irn_class_ ## irn_class) != 0)
244
245 /**
246  * Get the flags of a node.
247  * @param env The architecture environment.
248  * @param irn The node.
249  * @return The flags.
250  */
251 extern arch_irn_flags_t arch_irn_get_flags(const arch_env_t *env, const ir_node *irn);
252
253 #define arch_irn_is(env, irn, flag) ((arch_irn_get_flags(env, irn) & arch_irn_flags_ ## flag) != 0)
254
255 #define arch_irn_has_reg_class(env, irn, pos, cls) \
256         ((cls) == arch_get_irn_reg_class(env, irn, pos))
257
258 #define arch_irn_consider_in_reg_alloc(env, cls, irn) \
259         (arch_irn_has_reg_class(env, irn, -1, cls) && !arch_irn_is(env, irn, ignore))
260
261 /**
262  * Initialize the architecture environment struct.
263  * @param isa           The isa which shall be put into the environment.
264  * @param file_handle   The file handle
265  * @return The environment.
266  */
267 extern arch_env_t *arch_env_init(arch_env_t *env, const arch_isa_if_t *isa,
268                                  FILE *file_handle, be_main_env_t *main_env);
269
270 /**
271  * Add a node handler to the environment.
272  * @param env The environment.
273  * @param handler A node handler.
274  * @return The environment itself.
275  */
276 extern arch_env_t *arch_env_push_irn_handler(arch_env_t *env, const arch_irn_handler_t *handler);
277
278 /**
279  * Remove a node handler from the handler stack.
280  * @param env The architecture environment.
281  * @return The popped handler.
282  */
283 extern const arch_irn_handler_t *arch_env_pop_irn_handler(arch_env_t *env);
284
285 /**
286  * Register an instruction set architecture
287  */
288 void be_register_isa_if(const char *name, const arch_isa_if_t *isa);
289
290 #endif /* _FIRM_BEARCH_H_ */