ae58eb0d45721d9d7ccd7c5254576ec5493f4dbe
[libfirm] / ir / be / benode_t.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 /**
21  * @file
22  * @brief       Backend node support for generic backend nodes.
23  * @author      Sebastian Hack
24  * @date        17.05.2005
25  * @version     $Id$
26  *
27  * Backend node support for generic backend nodes.
28  * This file provides Perm, Copy, Spill and Reload nodes.
29  */
30 #ifndef FIRM_BE_BENODE_T_H
31 #define FIRM_BE_BENODE_T_H
32
33 #include <limits.h>
34
35 #include "firm_types.h"
36 #include "irnode.h"
37 #include "bearch.h"
38
39 #define BE_OUT_POS(p) (-((p) + 1))
40
41 /**
42  * The benode op's.  Must be available to register emitter function.
43  */
44 extern ir_op *op_be_Spill;
45 extern ir_op *op_be_Reload;
46 extern ir_op *op_be_Perm;
47 extern ir_op *op_be_MemPerm;
48 extern ir_op *op_be_Copy;
49 extern ir_op *op_be_Keep;
50 extern ir_op *op_be_CopyKeep;
51 extern ir_op *op_be_Call;
52 extern ir_op *op_be_Return;
53 extern ir_op *op_be_IncSP;
54 extern ir_op *op_be_AddSP;
55 extern ir_op *op_be_SubSP;
56 extern ir_op *op_be_SetSP;
57 extern ir_op *op_be_RegParams;
58 extern ir_op *op_be_StackParam;
59 extern ir_op *op_be_FrameAddr;
60 extern ir_op *op_be_FrameLoad;
61 extern ir_op *op_be_FrameStore;
62 extern ir_op *op_be_Barrier;
63
64 typedef enum {
65         beo_NoBeOp = -1,
66         beo_Spill,
67         beo_Reload,
68         beo_Perm,
69         beo_MemPerm,
70         beo_Copy,
71         beo_Keep,
72         beo_CopyKeep,
73         beo_Call,
74         beo_Return,
75         beo_AddSP,
76         beo_SubSP,
77         beo_IncSP,
78         beo_SetSP,
79         beo_RegParams,
80         beo_StackParam,
81         beo_FrameLoad,
82         beo_FrameStore,
83         beo_FrameAddr,
84         beo_Barrier,
85         beo_Last
86 } be_opcode_t;
87
88 /** Not used yet. */
89 typedef enum {
90         be_frame_flag_spill = 1,
91         be_frame_flag_local = 2,
92         be_frame_flag_arg   = 4
93 } be_frame_flag_t;
94
95 /**
96  * A "symbolic constant" for the size of the stack frame to use with IncSP nodes.
97  * It gets back-patched to the real size as soon it is known.
98  */
99 #define BE_STACK_FRAME_SIZE_EXPAND INT_MAX
100 #define BE_STACK_FRAME_SIZE_SHRINK INT_MIN
101
102 /**
103  * Determines if irn is a be_node.
104  */
105 int is_be_node(const ir_node *irn);
106
107 /**
108  * Create all BE specific opcodes.
109  */
110 void be_node_init(void);
111
112 /**
113  * Position numbers for the be_Spill inputs.
114  */
115 enum {
116         be_pos_Spill_frame = 0,
117         be_pos_Spill_val   = 1
118 };
119
120 /**
121  * Make a new Spill node.
122  */
123 ir_node *be_new_Spill(const arch_register_class_t *cls, const arch_register_class_t *cls_frame,
124         ir_graph *irg, ir_node *bl, ir_node *frame, ir_node *to_spill);
125
126 /**
127  * Position numbers for the be_Reload inputs.
128  */
129 enum {
130         be_pos_Reload_frame = 0,
131         be_pos_Reload_mem   = 1
132 };
133
134 /**
135  * Make a new Reload node.
136  */
137 ir_node *be_new_Reload(const arch_register_class_t *cls, const arch_register_class_t *cls_frame,
138         ir_graph *irg, ir_node *bl, ir_node *frame, ir_node *mem, ir_mode *mode);
139
140 /**
141  * Position numbers for the be_Copy inputs.
142  */
143 enum {
144         be_pos_Copy_op = 0
145 };
146
147 /**
148  * Make a new Copy node.
149  */
150 ir_node *be_new_Copy(const arch_register_class_t *cls, ir_graph *irg, ir_node *block, ir_node *in);
151 /** Returns the Copy Argument. */
152 ir_node *be_get_Copy_op(const ir_node *cpy);
153 /** Sets the Copy Argument. */
154 void be_set_Copy_op(ir_node *cpy, ir_node *op);
155
156 /**
157  * Make a new Perm node.
158  */
159 ir_node *be_new_Perm(const arch_register_class_t *cls, ir_graph *irg, ir_node *bl, int arity, ir_node *in[]);
160
161 /**
162  * Reduce a Perm.
163  * Basically, we provide a map to remap the Perm's arguments. If an entry in the
164  * map is -1, the argument gets deleted.
165  * This function takes care, that the register data and the input array reflects
166  * the changes described by the map.
167  * This is needed by the Perm optimization/movement in belower.c, see push_through_perm().
168  * @param perm     The perm node.
169  * @param new_size The new number of arguments (must be smaller or equal to the current one).
170  * @param map      A map assigning each operand a new index (or -1 to indicate deletion).
171  */
172 void be_Perm_reduce(ir_node *perm, int new_size, int *map);
173
174 /**
175  * Create a new MemPerm node.
176  */
177 ir_node *be_new_MemPerm(const arch_env_t *arch_env, ir_graph *irg, ir_node *bl, int n, ir_node *in[]);
178 ir_node *be_new_Keep(const arch_register_class_t *cls, ir_graph *irg, ir_node *bl, int arity, ir_node *in[]);
179
180 void be_Keep_add_node(ir_node *keep, const arch_register_class_t *cls, ir_node *node);
181
182 /**
183  * Position numbers for the be_FrameLoad inputs
184  */
185 enum {
186         be_pos_FrameLoad_mem = 0,
187         be_pos_FrameLoad_ptr = 1
188 };
189
190 ir_node *be_new_FrameLoad(const arch_register_class_t *cls_frame, const arch_register_class_t *cls_data,
191                                                   ir_graph *irg, ir_node *bl, ir_node *mem, ir_node *frame, ir_entity *ent);
192
193 /**
194  * Position numbers for the be_FrameStore inputs
195  */
196 enum {
197         be_pos_FrameStore_mem = 0,
198         be_pos_FrameStore_ptr = 1,
199         be_pos_FrameStore_val = 2
200 };
201
202 ir_node *be_new_FrameStore(const arch_register_class_t *cls_frame, const arch_register_class_t *cls_data,
203                                                    ir_graph *irg, ir_node *bl, ir_node *mem, ir_node *frame, ir_node *data, ir_entity *ent);
204
205 /**
206  * Position numbers for the be_FrameAddr inputs
207  */
208 enum {
209         be_pos_FrameAddr_ptr = 0
210 };
211
212 /** Create a new FrameAddr node. */
213 ir_node *be_new_FrameAddr(const arch_register_class_t *cls_frame, ir_graph *irg, ir_node *bl, ir_node *frame, ir_entity *ent);
214
215 /** Return the frame input of a FrameAddr node. */
216 ir_node *be_get_FrameAddr_frame(ir_node *node);
217
218 /**
219  * Position numbers for the be_AddSP inputs
220  */
221 enum {
222         be_pos_AddSP_old_sp = 0,
223         be_pos_AddSP_size   = 1,
224         be_pos_AddSP_last   = 2
225 };
226
227 enum {
228         pn_be_AddSP_res  = 0,
229         pn_be_AddSP_M    = 1,
230         pn_be_AddSP_last = 2
231 };
232
233 /**
234  * Make a new AddSP node.
235  * An AddSP node expresses an increase of the stack pointer in the direction the stack
236  * grows. In contrast to IncSP, the amount of bytes the stack pointer is grown, is not
237  * given by a constant but an ordinary Firm node.
238  * @param sp     The stack pointer register.
239  * @param irg    The graph.
240  * @param bl     The block.
241  * @param old_sp The node representing the old stack pointer value.
242  * @param sz     The node expressing the size by which the stack pointer shall be grown.
243  * @return       A new AddSP node.
244  */
245 ir_node *be_new_AddSP(const arch_register_t *sp, ir_graph *irg, ir_node *bl, ir_node *old_sp, ir_node *sz);
246
247 /**
248  * Position numbers for the be_SubSP inputs
249  */
250 enum {
251         be_pos_SubSP_old_sp = 0,
252         be_pos_SubSP_size   = 1,
253         be_pos_SubSP_last   = 2
254 };
255
256 enum {
257         pn_be_SubSP_res  = 0,
258         pn_be_SubSP_M    = 1,
259         pn_be_SubSP_last = 2
260 };
261
262 /**
263  * Make a new SubSP node.
264  * A SubSP node expresses a decrease of the stack pointer in the direction the stack
265  * grows. In contrast to IncSP, the amount of bytes the stack pointer is grown, is not
266  * given by a constant but an ordinary Firm node.
267  * @param sp     The stack pointer register.
268  * @param irg    The graph.
269  * @param bl     The block.
270  * @param old_sp The node representing the old stack pointer value.
271  * @param sz     The node expressing the size by which the stack pointer shall be grown.
272  * @return       A new DecSP node.
273  */
274 ir_node *be_new_SubSP(const arch_register_t *sp, ir_graph *irg, ir_node *bl, ir_node *old_sp, ir_node *sz);
275
276 ir_node *be_new_SetSP(const arch_register_t *sp, ir_graph *irg, ir_node *bl, ir_node *old_sp, ir_node *operand, ir_node *mem);
277
278 /**
279  * Make a stack pointer increase/decrease node.
280  * @param sp     The stack pointer register.
281  * @param irg    The graph to insert the node to.
282  * @param bl     The block to insert the node into.
283  * @param old_sp The node defining the former stack pointer.
284  * @param amount The mount of bytes the stack shall be expanded/shrinked (see set_IncSP_offset)
285  * @param dir    The direction in which the stack pointer shall be modified:
286  *               Along the stack's growing direction or against.
287  * @return       A new stack pointer increment/decrement node.
288  * @note         This node sets a register constraint to the @p sp register on its output.
289  */
290 ir_node *be_new_IncSP(const arch_register_t *sp, ir_graph *irg, ir_node *bl, ir_node *old_sp, int offset);
291
292 /** Returns the previous node that computes the stack pointer. */
293 ir_node *be_get_IncSP_pred(ir_node *incsp);
294
295 /** Sets the previous node that computes the stack pointer. */
296 void     be_set_IncSP_pred(ir_node *incsp, ir_node *pred);
297
298 /**
299  * Sets a new offset to a IncSP node.
300  * A positive offset means expanding the stack, a negative offset shrinking
301  * an offset is == BE_STACK_FRAME_SIZE will be replaced by the real size of the
302  * stackframe in the fix_stack_offsets phase.
303  */
304 void     be_set_IncSP_offset(ir_node *irn, int offset);
305
306 /** Gets the offset from a IncSP node. */
307 int be_get_IncSP_offset(const ir_node *irn);
308
309 /** Gets the call entity or NULL if this is no static call. */
310 ir_entity  *be_Call_get_entity(const ir_node *call);
311 /** Sets the call entity. */
312 void     be_Call_set_entity(ir_node *call, ir_entity *ent);
313 /** Gets the call type. */
314 ir_type *be_Call_get_type(ir_node *call);
315 /** Sets the call type. */
316 void     be_Call_set_type(ir_node *call, ir_type *call_tp);
317
318 /**
319  * Position numbers for the be_Call inputs.
320  */
321 enum {
322         be_pos_Call_mem       = 0,  /**< memory input of a be_Call node */
323         be_pos_Call_sp        = 1,  /**< stack pointer input of a be_Call node */
324         be_pos_Call_ptr       = 2,  /**< call pointer input of a be_Call node */
325         be_pos_Call_first_arg = 3   /**< first argument input of a be_Call node */
326 };
327
328 /**
329  * Projection numbers for result of be_Call node: use for Proj nodes!
330  */
331 typedef enum {
332         pn_be_Call_M_regular = pn_Call_M_regular,  /**< The memory result of a be_Call. */
333         pn_be_Call_first_res = pn_Call_max         /**< The first result proj number of a be_Call. */
334 } pn_be_Call;
335
336 /**
337  * Construct a new be_Call.
338  *
339  * @param dbg      debug info
340  * @param irg      the graph where the call is placed
341  * @param bl       the block where the call is placed
342  * @param mem      the memory input of the call
343  * @param sp       the stack pointer input of the call
344  * @param ptr      the address of the called function, if immediate call set to sp
345  * @param n_outs   the number of outcoming values from this call
346  * @param n        the number of (register) inputs of this call
347  * @param in       the (register) inputs of this call
348  * @param call_tp  the call type of this call
349  */
350 ir_node *be_new_Call(dbg_info *dbg, ir_graph *irg, ir_node *bl, ir_node *mem, ir_node *sp, ir_node *ptr,
351                      int n_outs, int n, ir_node *in[], ir_type *call_tp);
352
353 /**
354  * Position numbers for the be_Return inputs.
355  */
356 enum {
357         be_pos_Return_mem  = 0,     /**< memory input of a be_Return node */
358         be_pos_Return_sp   = 1,     /**< stack pointer input of a be_Return node */
359         be_pos_Return_val  = 2,     /**< first "real" return value if any */
360 };
361
362 /**
363  * Construct a new be_Return.
364  * @param irg    the graph where the new node will be placed
365  * @param bl     the block where the new node will be placed
366  * @param n_res  number of "real" results
367  * @param n      number of inputs
368  * @param in     input array
369  */
370 ir_node *be_new_Return(dbg_info *dbg, ir_graph *irg, ir_node *bl, int n_res, int n, ir_node *in[]);
371
372 /** Returns the number of real returns values */
373 int be_Return_get_n_rets(ir_node *ret);
374
375 /** appends a node to the return node, returns the position of the node */
376 int be_Return_append_node(ir_node *ret, ir_node *node);
377
378 /**
379  * StackParam input positions
380  */
381 enum {
382         be_pos_StackParam_ptr = 0
383 };
384
385 /**
386  * Construct a new Stack Parameter node.
387  */
388 ir_node *be_new_StackParam(const arch_register_class_t *cls, const arch_register_class_t *cls_frame, ir_graph *irg, ir_node *bl, ir_mode *mode, ir_node *frame_pointer, ir_entity *ent);
389 ir_node *be_new_RegParams(ir_graph *irg, ir_node *bl, int n_out);
390
391 ir_node *be_new_Barrier(ir_graph *irg, ir_node *bl, int n, ir_node *in[]);
392
393 /**
394  * Appends a node to a barrier, returns the result proj of the node
395  */
396 ir_node *be_Barrier_append_node(ir_node *barrier, ir_node *node);
397
398 /**
399  * Appends a register out requirement to a RegParams node
400  *
401  * @returns the proj node for the new register
402  */
403 ir_node *be_RegParams_append_out_reg(ir_node *regparams,
404                                      const arch_env_t *arch_env,
405                                      const arch_register_t *reg);
406
407 /**
408  * Make a spill node.
409  *
410  * @param arch_env  The architecture environment.
411  * @param irn       The node to be spilled.
412  * @param spill_ctx The context in which the spill is introduced (This is mostly == irn up to the case of Phis).
413  * @return          The new spill node.
414  */
415 ir_node *be_spill(const arch_env_t *arch_env, ir_node *irn);
416
417 /**
418  * Make a reload and insert it into the schedule.
419  *
420  * @param arch_env The architecture environment.
421  * @param cls      The register class of the reloaded value.
422  * @param insert   The node in the schedule in front of which the reload is inserted.
423  * @param mode     The mode of the original (spilled) value.
424  * @param spill    The spill node corresponding to this reload.
425  * @return         A freshly made reload.
426  */
427 ir_node *be_reload(const arch_env_t *arch_env, const arch_register_class_t *cls, ir_node *insert, ir_mode *mode, ir_node *spill);
428
429 enum {
430         be_pos_CopyKeep_op = 0
431 };
432 ir_node *be_new_CopyKeep(const arch_register_class_t *cls, ir_graph *irg, ir_node *bl, ir_node *src, int n, ir_node *in_keep[], ir_mode *mode);
433 ir_node *be_new_CopyKeep_single(const arch_register_class_t *cls, ir_graph *irg, ir_node *bl, ir_node *src, ir_node *keep, ir_mode *mode);
434 ir_node *be_get_CopyKeep_op(const ir_node *cpy);
435 void be_set_CopyKeep_op(ir_node *cpy, ir_node *op);
436
437 /**
438  * Get the backend opcode of a backend node.
439  * @param irn The node.
440  * @return The backend opcode.
441  */
442 be_opcode_t be_get_irn_opcode(const ir_node *irn);
443
444 int be_is_Spill(const ir_node *irn);
445 int be_is_Reload(const ir_node *irn);
446 int be_is_Copy(const ir_node *irn);
447 int be_is_Perm(const ir_node *irn);
448 int be_is_MemPerm(const ir_node *irn);
449 int be_is_Keep(const ir_node *irn);
450 int be_is_CopyKeep(const ir_node *irn);
451 int be_is_Call(const ir_node *irn);
452 int be_is_Return(const ir_node *irn);
453 int be_is_IncSP(const ir_node *irn);
454 int be_is_SetSP(const ir_node *irn);
455 int be_is_AddSP(const ir_node *irn);
456 int be_is_SubSP(const ir_node *irn);
457 int be_is_RegParams(const ir_node *irn);
458 int be_is_StackParam(const ir_node *irn);
459 int be_is_FrameAddr(const ir_node *irn);
460 int be_is_FrameLoad(const ir_node *irn);
461 int be_is_FrameStore(const ir_node *irn);
462 int be_is_Barrier(const ir_node *irn);
463
464 /**
465  * Try to avoid this function and better call arch_get_frame_entity!
466  *
467  * Returns the frame entity used by the be node
468  */
469 ir_entity *be_get_frame_entity(const ir_node *irn);
470
471 /**
472  * Returns the frame offset of this node.
473  */
474 int be_get_frame_offset(const ir_node *irn);
475
476 ir_node* be_get_Reload_mem(const ir_node *irn);
477 ir_node *be_get_Reload_frame(const ir_node *irn);
478 ir_node* be_get_Spill_val(const ir_node *irn);
479 ir_node *be_get_Spill_frame(const ir_node *irn);
480
481 void be_set_MemPerm_in_entity(const ir_node *irn, int n, ir_entity* ent);
482 ir_entity *be_get_MemPerm_in_entity(const ir_node *irn, int n);
483
484 void be_set_MemPerm_out_entity(const ir_node *irn, int n, ir_entity* ent);
485 ir_entity *be_get_MemPerm_out_entity(const ir_node *irn, int n);
486
487 int be_get_MemPerm_entity_arity(const ir_node *irn);
488
489 /**
490  * Impose a register constraint on a backend node.
491  * @param irn The node.
492  * @param pos The position of the argument/result. Results range from -1..-m and arguments form 0..n
493  * @param reg The register which is admissible for that node, argument/result and position.
494  */
495 void be_set_constr_single_reg(ir_node *irn, int pos, const arch_register_t *reg);
496
497 /**
498  * Impose register constraints on a backend node.
499  * The register subsets given by the limited function in @p req are copied to the backend node.
500  * This requires that the constraint type of the @p req is arch_register_req_type_limited.
501  * @param irn The backend node.
502  * @param pos The position (@see be_set_constr_single_reg()).
503  * @param req The register requirements which shall be transferred.
504  */
505 void be_set_constr_limited(ir_node *irn, int pos, const arch_register_req_t *req);
506
507 /**
508  * Set the flags of a node.
509  * @param irn The node itself.
510  * @param pos The position (0..n) for arguments, (-1..-m) for results.
511  * @param flags The flags to set for that node and position.
512  */
513 void be_node_set_flags(ir_node *irn, int pos, arch_irn_flags_t flags);
514
515 /**
516  * Set the register class of a node.
517  * @param irn The node itself.
518  * @param pos The position (0..n) for arguments, (-1..-m) for results.
519  * @param flags The register class to set for that node and position.
520  */
521 void be_node_set_reg_class(ir_node *irn, int pos, const arch_register_class_t *cls);
522
523 /**
524  * Set the register requirement type of a node.
525  * @param irn The node itself.
526  * @param pos The position (0..n) for arguments, (-1..-m) for results.
527  * @param flags The register requirement type to set for that node and position.
528  */
529 void be_node_set_req_type(ir_node *irn, int pos, arch_register_req_type_t type);
530
531 /**
532  * Make a new phi handler.
533  * @param env The architecture environment.
534  * @return A new phi handler.
535  */
536 arch_irn_handler_t *be_phi_handler_new(const arch_env_t *arch_env);
537
538 /**
539  * Free a phi handler.
540  * @param handler The handler to free.
541  */
542 void be_phi_handler_free(arch_irn_handler_t *handler);
543
544 /**
545  * Reset the register data in the phi handler.
546  * This should be called on each new graph and deletes the register information of the current graph.
547  */
548 void be_phi_handler_reset(arch_irn_handler_t *handler);
549
550 /**
551  * Set the register requirements for a phi node.
552  */
553 void be_set_phi_reg_req(const arch_env_t *arch_env, ir_node *phi,
554                         const arch_register_req_t *req);
555
556 /*
557  * Set flags for a phi node
558  */
559 void be_set_phi_flags(const arch_env_t *arch_env, ir_node *phi,
560                       arch_irn_flags_t flags);
561
562 /**
563  * irn handler for common be nodes.
564  */
565 extern const arch_irn_handler_t be_node_irn_handler;
566
567 #endif /* FIRM_BE_BENODE_T_H */