Initial commit of morgans spilling algorithm (spill unused values that live through
[libfirm] / ir / be / bearch.h
1 #ifndef _FIRM_BEARCH_H
2 #define _FIRM_BEARCH_H
3
4 #include "firm_config.h"
5
6 #ifdef WITH_LIBCORE
7 #include <libcore/lc_opts.h>
8 #endif
9
10 #include "firm_types.h"
11
12 #include "bitset.h"
13
14 #include "belistsched.h"
15 #include "beabi_t.h"
16 #include "bearch_t.h"
17 #include "be_t.h"
18
19 struct _be_node_factory_t;
20
21 typedef enum _arch_register_type_t {
22   arch_register_type_none         = 0,
23   arch_register_type_caller_save  = 1, /**< The register must be saved by the caller
24                                             upon a function call. It thus can be overwritten
25                                             in the called function. */
26   arch_register_type_callee_save  = 2, /**< The register must be saved by the caller
27                                             upon a function call. It thus can be overwritten
28                                             in the called function. */
29   arch_register_type_ignore       = 4, /**< Do not consider this register when allocating. */
30 } arch_register_type_t;
31
32 /**
33  * Convenience macro to check for register type.
34  * @param req   A pointer to register.
35  * @param kind  The kind of type to check for (see arch_register_type_t).
36  * @return      1, If register is of given kind, 0 if not.
37  */
38 #define arch_register_type_is(reg, kind) \
39   (((reg)->type & arch_register_type_ ## kind) != 0)
40
41 /**
42  * A register.
43  */
44 struct _arch_register_t {
45   const char *name;                         /**< The name of the register. */
46   const arch_register_class_t *reg_class;   /**< The class the register belongs to. */
47   int index;                                /**< The index of the register in the class. */
48   arch_register_type_t type;                /**< The type of the register. */
49   void *data;                               /**< Custom data. */
50 };
51
52 static INLINE const arch_register_class_t *
53 _arch_register_get_class(const arch_register_t *reg)
54 {
55   return reg->reg_class;
56 }
57
58 static INLINE int _arch_register_get_index(const arch_register_t *reg)
59 {
60   return reg->index;
61 }
62
63 #define arch_register_get_class(reg)      _arch_register_get_class(reg)
64 #define arch_register_get_index(reg)      _arch_register_get_index(reg)
65 #define arch_register_get_name(reg)       ((reg)->name)
66
67 /**
68  * A class of registers.
69  * Like general purpose or floating point.
70  */
71 struct _arch_register_class_t {
72   const char *name;               /**< The name of the register class. */
73   int n_regs;                     /**< Number of registers in this class. */
74   ir_mode *mode;                  /**< The mode of the register class. */
75   const arch_register_t *regs;    /**< The array of registers. */
76 };
77
78 /** return the number of registers in this register class */
79 #define arch_register_class_n_regs(cls) ((cls)->n_regs)
80
81 /** return the largest mode of this register class */
82 #define arch_register_class_mode(cls) ((cls)->mode)
83
84 /**
85  * Put all registers in a class into a bitset.
86  * @param cls The class.
87  * @param bs The bitset. May be NULL.
88  * @return The number of registers in the class.
89  */
90 extern int arch_register_class_put(const arch_register_class_t *cls, bitset_t *bs);
91
92 static INLINE const arch_register_t *
93 _arch_register_for_index(const arch_register_class_t *cls, int idx)
94 {
95   assert(0 <= idx && idx < cls->n_regs);
96   return &cls->regs[idx];
97 }
98
99 #define arch_register_for_index(cls, idx) \
100   _arch_register_for_index(cls, idx)
101
102 typedef enum _arch_operand_type_t {
103   arch_operand_type_invalid,
104   arch_operand_type_memory,
105   arch_operand_type_register,
106   arch_operand_type_immediate,
107   arch_operand_type_symconst,
108   arch_operand_type_last
109 } arch_operand_type_t;
110
111 /**
112  * Different types of register allocation requirements.
113  */
114 typedef enum _arch_register_req_type_t {
115   arch_register_req_type_none = 0,        /**< No register requirement. */
116
117   arch_register_req_type_normal = 1,      /**< All registers in the class
118                                                are allowed. */
119
120   arch_register_req_type_limited = 2,     /**< Only a real subset of
121                                                the class is allowed. */
122
123   arch_register_req_type_should_be_same = 4,       /**< The register should be equal
124                                                         another one at the node. */
125
126   arch_register_req_type_should_be_different = 8,  /**< The register must be unequal
127                                                         to some other at the node. */
128
129   arch_register_req_type_should_be_different_from_all = 16, /**< The register must be different from
130                                                         all in's at the node */
131 } arch_register_req_type_t;
132
133 /**
134  * Convenience macro to check for set constraints.
135  * @param req   A pointer to register requirements.
136  * @param kind  The kind of constraint to check for (see arch_register_req_type_t).
137  * @return      1, If the kind of constraint is present, 0 if not.
138  */
139 #define arch_register_req_is(req, kind) \
140         (((req)->type & (arch_register_req_type_ ## kind)) != 0)
141
142 /**
143  * Expresses requirements to register allocation for an operand.
144  */
145 typedef struct _arch_register_req_t {
146         arch_register_req_type_t type;          /**< The type of the constraint. */
147         const arch_register_class_t *cls;       /**< The register class this constraint belongs to. */
148
149         void (*limited)(void *limited_env, bitset_t *bs);
150                                           /**< In case of the 'limited'
151                                             constraint, this function
152                                             must put all allowable
153                                             registers in the bitset and
154                                             return the number of registers
155                                             in the bitset. */
156
157         void *limited_env;                    /**< This must passed to limited. */
158
159         ir_node *other_same;                      /**< The other which shall have the same reg
160                                                                                     as this one. (for case should_be_same). */
161
162         ir_node *other_different;             /**< The other node from which this one's register
163                                                                                     must be different (case must_be_different). */
164 } arch_register_req_t;
165
166 /**
167  * Format a register requirements information into a string.
168  * @param buf The string where to put it to.
169  * @param len The size of @p buf.
170  * @param req The requirements structure to format.
171  * @return    A pointer to buf.
172  */
173 extern char *arch_register_req_format(char *buf, size_t len, const arch_register_req_t *req);
174
175
176 /**
177  * Certain node classes which are relevant for the register allocator.
178  */
179 typedef enum _arch_irn_class_t {
180   arch_irn_class_normal,
181   arch_irn_class_spill,
182   arch_irn_class_reload,
183   arch_irn_class_copy,
184   arch_irn_class_perm,
185   arch_irn_class_branch,
186   arch_irn_class_call,
187   arch_irn_class_const,
188   arch_irn_class_load,
189   arch_irn_class_store,
190   arch_irn_class_stackparam,
191 } arch_irn_class_t;
192
193 /**
194  * An inverse operation returned by the backend
195  */
196 typedef struct _arch_inverse_t {
197         int      n;       /**< count of nodes returned in nodes array */
198         int      costs;   /**< costs of this remat */
199
200         /**< nodes for this inverse operation. shall be in
201          *  schedule order. last element is the target value
202          */
203         ir_node  **nodes;
204 } arch_inverse_t;
205
206 /**
207  * Some flags describing a node in more detail.
208  */
209 typedef enum _arch_irn_flags_t {
210         arch_irn_flags_none             = 0, /**< Node flags. */
211         arch_irn_flags_dont_spill       = 1, /**< This must not be spilled. */
212         arch_irn_flags_rematerializable = 2, /**< This should be replicated instead of spilled/reloaded. */
213         arch_irn_flags_ignore           = 4, /**< Ignore node during register allocation. */
214         arch_irn_flags_modify_sp        = 8, /**< I modify the stack pointer. */
215         arch_irn_flags_last             = arch_irn_flags_modify_sp
216 } arch_irn_flags_t;
217
218 /**
219  * Get the string representation of a flag.
220  * This functions does not handle or'ed bitmasks of flags.
221  * @param flag The flag.
222  * @return The flag as a string.
223  */
224 extern const char *arch_irn_flag_str(arch_irn_flags_t flag);
225
226 struct _arch_irn_ops_if_t {
227
228   /**
229    * Get the register requirements for a given operand.
230    * @param self The self pointer.
231    * @param irn The node.
232    * @param pos The operand's position
233          *                                              (-1 for the result of the node, 0..n for the input
234          *                                              operands).
235    * @return    The register requirements for the selected operand.
236    *            The pointer returned is never NULL.
237    */
238   const arch_register_req_t *(*get_irn_reg_req)(const void *self,
239       arch_register_req_t *req, const ir_node *irn, int pos);
240
241   /**
242    * Set the register for an output operand.
243    * @param irn The node.
244    * @param reg The register allocated to that operand.
245    * @note      If the operand is not a register operand,
246    *            the call is ignored.
247    */
248   void (*set_irn_reg)(const void *self, ir_node *irn, const arch_register_t *reg);
249
250   /**
251    * Get the register allocated for an output operand.
252    * @param irn The node.
253    * @return    The register allocated at that operand. NULL, if
254    *            the operand was no register operand or
255    *            @c arch_register_invalid, if no register has yet been
256    *            allocated for this node.
257    */
258   const arch_register_t *(*get_irn_reg)(const void *self, const ir_node *irn);
259
260   /**
261    * Classify the node.
262    * @param irn The node.
263    * @return A classification.
264    */
265   arch_irn_class_t (*classify)(const void *self, const ir_node *irn);
266
267   /**
268    * Get the flags of a node.
269    * @param self The irn ops themselves.
270    * @param irn The node.
271    * @return A set of flags.
272    */
273   arch_irn_flags_t (*get_flags)(const void *self, const ir_node *irn);
274
275   /**
276    * Get the entity on the stack frame this node depends on.
277    * @param self The this pointer.
278    * @param irn  The node in question.
279    * @return The entity on the stack frame or NULL, if the node does not has a stack frame entity.
280    */
281   entity *(*get_frame_entity)(const void *self, const ir_node *irn);
282
283   /**
284    * Set the offset of a node carrying an entity on the stack frame.
285    * @param self The this pointer.
286    * @param irn  The node.
287    * @param offset The offset of the node's stack frame entity.
288    */
289   void (*set_frame_offset)(const void *self, ir_node *irn, int offset);
290
291   /**
292    * Returns an inverse operation which yields the i-th argument
293    * of the given node as result.
294    *
295    * @param self      The this pointer.
296    * @param irn       The original operation
297    * @param i         Index of the argument we want the inverse operation to yield
298    * @param inverse   struct to be filled with the resulting inverse op
299    * @param obstack   The obstack to use for allocation of the returned nodes array
300    * @return          The inverse operation or NULL if operation invertible
301    */
302   arch_inverse_t *(*get_inverse)(const void *self, const ir_node *irn, int i, arch_inverse_t *inverse, struct obstack *obstack);
303
304   /**
305    * Get the estimated cycle count for @p irn.
306    *
307    * @param self The this pointer.
308    * @param irn  The node.
309    *
310    * @return     The estimated cycle count for this operation
311    */
312   int (*get_op_estimated_cost)(const void *self, const ir_node *irn);
313
314   /**
315    * Asks the backend whether operand @p i of @p irn can be loaded form memory internally
316    *
317    * @param self The this pointer.
318    * @param irn  The node.
319    * @param i    Index of the argument we would like to know whether @p irn can load it form memory internally
320    *
321    * @return     nonzero if argument can be loaded or zero otherwise
322    */
323   int (*possible_memory_operand)(const void *self, const ir_node *irn, unsigned int i);
324
325 };
326
327 /**
328  * irn_ops base class.
329  */
330 struct _arch_irn_ops_t {
331         const arch_irn_ops_if_t *impl;
332 };
333
334 extern void arch_set_frame_offset(const arch_env_t *env, ir_node *irn, int bias);
335
336 extern entity *arch_get_frame_entity(const arch_env_t *env, ir_node *irn);
337
338 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);
339
340 /**
341  * Get the register requirements for a node.
342  * @param env The architecture environment.
343  * @param req A pointer to a requirements structure, where the data can
344  *            be put into.
345  * @param irn The node.
346  * @param pos The position of the operand you're interested in.
347  * @return    A pointer to the register requirements which may <b>not</b>
348  *            neccessarily be equal to @p req. If NULL is returned, the
349  *            operand was no register operand.
350  */
351 extern const arch_register_req_t *
352 arch_get_register_req(const arch_env_t *env, arch_register_req_t *req,
353     const ir_node *irn, int pos);
354
355 /**
356  * Check if an operand is a register operand.
357  * @param env The environment.
358  * @param irn The node.
359  * @param pos The position of the operand.
360  * @return 1, if the operand is significant for register allocation, 0
361  * if not.
362  */
363 extern int arch_is_register_operand(const arch_env_t *env,
364     const ir_node *irn, int pos);
365
366 /**
367  * Get the number of allocatable registers concerning
368  * a register class for an operand of a node.
369  * @param env The environment.
370  * @param irn The node.
371  * @param pos The postition of the node's operand.
372  * @param bs  The bitset all allocatable registers shall be put into.
373  *            Note, that you can also pass NULL here. If you don't,
374  *            make sure, the bitset is as large as the register class
375  *            has registers.
376  * @return    The amount of registers allocatable for that operand.
377  */
378 extern int arch_get_allocatable_regs(const arch_env_t *env, const ir_node *irn, int pos, bitset_t *bs);
379
380 /**
381  * Put all registers which shall not be ignored by the register
382  * allocator in a bit set.
383  * @param env The arch env.
384  * @param cls The register class to consider.
385  * @param bs  The bit set to put the registers to.
386  */
387 extern void arch_put_non_ignore_regs(const arch_env_t *env, const arch_register_class_t *cls, bitset_t *bs);
388
389 /**
390  * Return the number of registers in a register class which should not be
391  * ignored by the register allocator.
392  * @param env The architecture environment.
393  * @param cls The register class to consider
394  * @return        The number of non-ignore registers in the register class
395  */
396 extern int arch_count_non_ignore_regs(const arch_env_t *env, const arch_register_class_t *cls);
397
398 /**
399  * Check, if a register is assignable to an operand of a node.
400  * @param env The architecture environment.
401  * @param irn The node.
402  * @param pos The position of the operand.
403  * @param reg The register.
404  * @return    1, if the register might be allocated to the operand 0 if not.
405  */
406 extern int arch_reg_is_allocatable(const arch_env_t *env,
407     const ir_node *irn, int pos, const arch_register_t *reg);
408
409 /**
410  * Get the register class of an operand of a node.
411  * @param env The architecture environment.
412  * @param irn The node.
413  * @param pos The position of the operand, -1 for the output.
414  * @return    The register class of the operand or NULL, if
415  *            operand is a non-register operand.
416  */
417 extern const arch_register_class_t *
418 arch_get_irn_reg_class(const arch_env_t *env, const ir_node *irn, int pos);
419
420 /**
421  * Get the register allocated at a certain output operand of a node.
422  * @param env The arch environment.
423  * @param irn The node.
424  * @return    The register allocated for this operand
425  */
426 extern const arch_register_t *
427 arch_get_irn_register(const arch_env_t *env, const ir_node *irn);
428
429 /**
430  * Set the register for a certain output operand.
431  * @param env The architecture environment.
432  * @param irn The node.
433  * @param idx The index of the output operand.
434  * @param reg The register.
435  */
436 extern void arch_set_irn_register(const arch_env_t *env, ir_node *irn,
437                 const arch_register_t *reg);
438
439 /**
440  * Classify a node.
441  * @param env The architecture environment.
442  * @param irn The node.
443  * @return A classification of the node.
444  */
445 extern arch_irn_class_t arch_irn_classify(const arch_env_t *env, const ir_node *irn);
446
447 /**
448  * Get the flags of a node.
449  * @param env The architecture environment.
450  * @param irn The node.
451  * @return The flags.
452  */
453 extern arch_irn_flags_t arch_irn_get_flags(const arch_env_t *env, const ir_node *irn);
454
455 #define arch_irn_is(env, irn, flag) ((arch_irn_get_flags(env, irn) & arch_irn_flags_ ## flag) != 0)
456
457 #define arch_irn_has_reg_class(env, irn, pos, cls) \
458   ((cls) == arch_get_irn_reg_class(env, irn, pos))
459
460 #define arch_irn_consider_in_reg_alloc(env, cls, irn) \
461         (arch_irn_has_reg_class(env, irn, -1, cls) && !arch_irn_is(env, irn, ignore))
462
463 /**
464  * Somebody who can be asked about IR nodes.
465  */
466 struct _arch_irn_handler_t {
467
468   /**
469     * Get the operations of an irn.
470     * @param self The handler from which the method is invoked.
471     * @param irn Some node.
472     * @return Operations for that irn.
473     */
474   const void *(*get_irn_ops)(const arch_irn_handler_t *handler,
475       const ir_node *irn);
476 };
477
478 /**
479  * The code generator interface.
480  */
481 struct _arch_code_generator_if_t {
482         /**
483          * Initialize the code generator.
484          * @param birg A backend IRG session.
485          * @return     A newly created code generator.
486          */
487         void *(*init)(const be_irg_t *birg);
488
489         /**
490          * Called before abi introduce.
491          */
492         void (*before_abi)(void *self);
493
494         /**
495          * Called, when the graph is being normalized.
496          */
497         void (*prepare_graph)(void *self);
498
499         /**
500          * Called before scheduling.
501          */
502         void (*before_sched)(void *self);
503
504         /**
505          * Called before register allocation.
506          */
507         void (*before_ra)(void *self);
508
509         /**
510          * Called after register allocation.
511          */
512         void (*after_ra)(void *self);
513
514         /**
515          * Called after everything happened.
516          * The code generator must also be de-allocated here.
517          */
518         void (*done)(void *self);
519 };
520
521 /**
522  * helper macro: call function func from the code generator
523  * if it's implemented.
524  */
525 #define _arch_cg_call(cg, func) \
526 do { \
527         if((cg)->impl->func) \
528                 (cg)->impl->func(cg); \
529 } while(0)
530
531 #define arch_code_generator_before_abi(cg)      _arch_cg_call(cg, before_abi)
532 #define arch_code_generator_prepare_graph(cg)   _arch_cg_call(cg, prepare_graph)
533 #define arch_code_generator_before_sched(cg)    _arch_cg_call(cg, before_sched)
534 #define arch_code_generator_before_ra(cg)       _arch_cg_call(cg, before_ra)
535 #define arch_code_generator_after_ra(cg)        _arch_cg_call(cg, after_ra)
536 #define arch_code_generator_done(cg)            _arch_cg_call(cg, done)
537
538 /**
539  * Code generator base class.
540  */
541 struct _arch_code_generator_t {
542         const arch_code_generator_if_t *impl;
543 };
544
545 /**
546  * ISA base class.
547  */
548 struct _arch_isa_t {
549         const arch_isa_if_t *impl;
550         const arch_register_t *sp;  /** The stack pointer register. */
551         const arch_register_t *bp;  /** The base pointer register. */
552         const int stack_dir;        /** -1 for decreasing, 1 for increasing. */
553 };
554
555 #define arch_isa_stack_dir(isa)  ((isa)->stack_dir)
556 #define arch_isa_sp(isa)         ((isa)->sp)
557 #define arch_isa_bp(isa)         ((isa)->bp)
558
559 /**
560  * Architecture interface.
561  */
562 struct _arch_isa_if_t {
563
564   /**
565    * Initialize the isa interface.
566          * @param file_handle  the file handle to write the output to
567          * @return a new isa instance
568    */
569   void *(*init)(FILE *file_handle);
570
571   /**
572    * Free the isa instance.
573    */
574   void (*done)(void *self);
575
576   /**
577    * Get the the number of register classes in the isa.
578    * @return The number of register classes.
579    */
580   int (*get_n_reg_class)(const void *self);
581
582   /**
583    * Get the i-th register class.
584    * @param i The number of the register class.
585    * @return The register class.
586    */
587   const arch_register_class_t *(*get_reg_class)(const void *self, int i);
588
589   /**
590    * Get the register class which shall be used to store a value of a given mode.
591    * @param self The this pointer.
592    * @param mode The mode in question.
593    * @return A register class which can hold values of the given mode.
594    */
595   const arch_register_class_t *(*get_reg_class_for_mode)(const void *self, const ir_mode *mode);
596
597   /**
598    * Get the ABI restrictions for procedure calls.
599    * @param self        The this pointer.
600    * @param method_type The type of the method (procedure) in question.
601    * @param p           The array of parameter locations to be filled.
602    */
603   void (*get_call_abi)(const void *self, ir_type *method_type, be_abi_call_t *abi);
604
605   /**
606    * The irn handler for this architecture.
607    * The irn handler is registered by the Firm back end
608    * when the architecture is initialized.
609    * (May be NULL).
610    */
611   const arch_irn_handler_t *(*get_irn_handler)(const void *self);
612
613   /**
614    * Get the code generator interface.
615    * @param self The this pointer.
616    * @return     Some code generator interface.
617    */
618   const arch_code_generator_if_t *(*get_code_generator_if)(void *self);
619
620   /**
621    * Get the list scheduler to use.
622    * @param self  The isa object.
623    * @return      The list scheduler selector.
624    */
625   const list_sched_selector_t *(*get_list_sched_selector)(const void *self);
626
627   /**
628    * Get the necessary alignment for storing a register of given class.
629    * @param self  The isa object.
630    * @param cls   The register class.
631    * @return      The alignment in bytes.
632    */
633   int (*get_reg_class_alignment)(const void *self, const arch_register_class_t *cls);
634
635   /**
636    * A "static" function, returns the frontend settings
637    * needed for this backend.
638    */
639   const backend_params *(*get_params)(void);
640
641 #ifdef WITH_LIBCORE
642   /**
643    * Register command line options for this backend.
644    * @param grp  The root group.
645    */
646   void (*register_options)(lc_opt_entry_t *grp);
647 #endif
648 };
649
650 #define arch_isa_get_n_reg_class(isa)                ((isa)->impl->get_n_reg_class(isa))
651 #define arch_isa_get_reg_class(isa,i)                ((isa)->impl->get_reg_class(isa, i))
652 #define arch_isa_get_irn_handler(isa)                ((isa)->impl->get_irn_handler(isa))
653 #define arch_isa_get_call_abi(isa,tp,abi)            ((isa)->impl->get_call_abi((isa), (tp), (abi)))
654 #define arch_isa_get_reg_class_for_mode(isa,mode)    ((isa)->impl->get_reg_class_for_mode((isa), (mode)))
655 #define arch_isa_make_code_generator(isa,irg)        ((isa)->impl->make_code_generator((isa), (irg)))
656 #define arch_isa_get_reg_class_alignment(isa, cls)   ((isa)->impl->get_reg_class_alignment((isa), (cls)))
657
658 #define ARCH_MAX_HANDLERS         8
659
660 /**
661  * Environment for the architecture infrastructure.
662  * Keep this everywhere you're going.
663  */
664 struct _arch_env_t {
665   const struct _be_node_factory_t *node_factory;  /**< The node factory for be nodes. */
666   arch_isa_t *isa;                                /**< The isa about which everything is. */
667
668   arch_irn_handler_t const *handlers[ARCH_MAX_HANDLERS]; /**< The handlers are organized as
669                                                            a stack. */
670
671   int handlers_tos;                                   /**< The stack pointer of the handler
672                                                         stack. */
673 };
674
675 /**
676  * Get the isa of an arch environment.
677  * @param env The environment.
678  * @return The isa with which the env was initialized with.
679  */
680 #define arch_env_get_isa(env)   ((env)->isa)
681
682 /**
683  * Initialize the architecture environment struct.
684  * @param isa           The isa which shall be put into the environment.
685  * @param file_handle   The file handle
686  * @return The environment.
687  */
688 extern arch_env_t *arch_env_init(arch_env_t *env, const arch_isa_if_t *isa, FILE *file_handle);
689
690 /**
691  * Add a node handler to the environment.
692  * @param env The environment.
693  * @param handler A node handler.
694  * @return The environment itself.
695  */
696 extern arch_env_t *arch_env_push_irn_handler(arch_env_t *env, const arch_irn_handler_t *handler);
697
698 /**
699  * Remove a node handler from the handler stack.
700  * @param env The architecture environment.
701  * @return The popped handler.
702  */
703 extern const arch_irn_handler_t *arch_env_pop_irn_handler(arch_env_t *env);
704
705 #endif /* _FIRM_BEARCH_H */