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