- removed useless be_req_t which was a wrapper around an arch_register_req_t
[libfirm] / ir / be / bearch_t.h
1 /*
2  * Copyright (C) 1995-2008 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       Processor architecture specification - internal data structures.
23  * @author      Sebastian Hack
24  * @version     $Id$
25  */
26 #ifndef FIRM_BE_BEARCH_T_H
27 #define FIRM_BE_BEARCH_T_H
28
29 #include "bearch.h"
30
31 #include "belistsched.h"
32 #include "beilpsched.h"
33 #include "bemachine.h"
34 #include "beirg.h"
35 #include "beinfo.h"
36 #include "beabi.h"
37 #include "raw_bitset.h"
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         unsigned                    index;        /**< The index of the register in the class. */
46         arch_register_type_t        type;         /**< The type of the register. */
47 };
48
49 static inline const arch_register_class_t *
50 _arch_register_get_class(const arch_register_t *reg)
51 {
52         return reg->reg_class;
53 }
54
55 static inline
56 unsigned _arch_register_get_index(const arch_register_t *reg)
57 {
58         return reg->index;
59 }
60
61 static inline
62 const char *_arch_register_get_name(const arch_register_t *reg)
63 {
64         return reg->name;
65 }
66
67 #define arch_register_get_class(reg)      _arch_register_get_class(reg)
68 #define arch_register_get_index(reg)      _arch_register_get_index(reg)
69 #define arch_register_get_name(reg)       _arch_register_get_name(reg)
70
71 /**
72  * Convenience macro to check for register type.
73  * @param req   A pointer to register.
74  * @param kind  The kind of type to check for (see arch_register_type_t).
75  * @return      1, If register is of given kind, 0 if not.
76  */
77 #define arch_register_type_is(reg, kind) \
78   (((reg)->type & arch_register_type_ ## kind) != 0)
79
80 /**
81  * A class of registers.
82  * Like general purpose or floating point.
83  */
84 struct arch_register_class_t {
85         unsigned                     index;  /**< index of this register class */
86         const char                  *name;   /**< The name of the register class.*/
87         unsigned                     n_regs; /**< Number of registers in this
88                                                   class. */
89         ir_mode                     *mode;   /**< The mode of the register class.*/
90         const arch_register_t       *regs;   /**< The array of registers. */
91         arch_register_class_flags_t  flags;  /**< register class flags. */
92 };
93
94 /** return the number of registers in this register class */
95 #define arch_register_class_n_regs(cls) ((cls)->n_regs)
96
97 /** return the largest mode of this register class */
98 #define arch_register_class_mode(cls) ((cls)->mode)
99
100 /** return the name of this register class */
101 #define arch_register_class_name(cls) ((cls)->name)
102
103 /** return the index of this register class */
104 #define arch_register_class_index(cls)  ((cls)->index)
105
106 /** return the register class flags */
107 #define arch_register_class_flags(cls) ((cls)->flags)
108
109 static inline const arch_register_t *
110 _arch_register_for_index(const arch_register_class_t *cls, unsigned idx)
111 {
112         assert(idx < cls->n_regs);
113         return &cls->regs[idx];
114 }
115
116 #define arch_register_for_index(cls, idx)   _arch_register_for_index(cls, idx)
117
118 /**
119  * Convenience macro to check for set constraints.
120  * @param req   A pointer to register requirements.
121  * @param kind  The kind of constraint to check for (see arch_register_req_type_t).
122  * @return      1, If the kind of constraint is present, 0 if not.
123  */
124 #define arch_register_req_is(req, kind) \
125         (((req)->type & (arch_register_req_type_ ## kind)) != 0)
126
127 /**
128  * Expresses requirements to register allocation for an operand.
129  */
130 struct arch_register_req_t {
131         arch_register_req_type_t type;      /**< The type of the constraint. */
132         const arch_register_class_t *cls;   /**< The register class this constraint belongs to. */
133
134         const unsigned *limited;            /**< allowed register bitset */
135
136         unsigned other_same;                /**< Bitmask of ins which should use the
137                                                  same register (should_be_same). */
138         unsigned other_different;           /**< Bitmask of ins which shall use a
139                                                  different register
140                                                  (must_be_different) */
141 };
142
143 static inline int reg_reqs_equal(const arch_register_req_t *req1,
144                                  const arch_register_req_t *req2)
145 {
146         if (req1 == req2)
147                 return 1;
148
149         if (req1->type != req2->type
150                         || req1->cls != req2->cls
151                         || req1->other_same != req2->other_same
152                         || req1->other_different != req2->other_different)
153                 return 0;
154
155         if (req1->limited != NULL) {
156                 size_t n_regs;
157
158                 if (req2->limited == NULL)
159                         return 0;
160
161                 n_regs = arch_register_class_n_regs(req1->cls);
162                 if (!rbitset_equal(req1->limited, req2->limited, n_regs))
163                         return 0;
164         }
165
166         return 1;
167 }
168
169 /**
170  * An inverse operation returned by the backend
171  */
172 struct arch_inverse_t {
173         int      n;       /**< count of nodes returned in nodes array */
174         int      costs;   /**< costs of this remat */
175
176         /**< nodes for this inverse operation. shall be in
177          *  schedule order. last element is the target value
178          */
179         ir_node  **nodes;
180 };
181
182 struct arch_irn_ops_t {
183
184         /**
185          * Get the register requirements for a given operand.
186          * @param self The self pointer.
187          * @param irn The node.
188          * @param pos The operand's position (0..n for the input operands).
189          * @return    The register requirements for the selected operand.
190          *            The pointer returned is never NULL.
191          */
192         const arch_register_req_t *(*get_irn_reg_req)(const ir_node *irn, int pos);
193
194         /**
195          * Classify the node.
196          * @param irn The node.
197          * @return A classification.
198          */
199         arch_irn_class_t (*classify)(const ir_node *irn);
200
201         /**
202          * Get the entity on the stack frame this node depends on.
203          * @param self The this pointer.
204          * @param irn  The node in question.
205          * @return The entity on the stack frame or NULL, if the node does not have a
206          *         stack frame entity.
207          */
208         ir_entity *(*get_frame_entity)(const ir_node *irn);
209
210         /**
211          * Set the entity on the stack frame this node depends on.
212          * @param self The this pointer.
213          * @param irn  The node in question.
214          * @param ent  The entity to set
215          */
216         void (*set_frame_entity)(ir_node *irn, ir_entity *ent);
217
218         /**
219          * Set the offset of a node carrying an entity on the stack frame.
220          * @param self The this pointer.
221          * @param irn  The node.
222          * @param offset The offset of the node's stack frame entity.
223          */
224         void (*set_frame_offset)(ir_node *irn, int offset);
225
226         /**
227          * Returns the delta of the stackpointer for nodes that increment or
228          * decrement the stackpointer with a constant value. (push, pop
229          * nodes on most architectures).
230          * A positive value stands for an expanding stack area, a negative value for
231          * a shrinking one.
232          *
233          * @param self      The this pointer
234          * @param irn       The node
235          * @return          0 if the stackpointer is not modified with a constant
236          *                  value, otherwise the increment/decrement value
237          */
238         int (*get_sp_bias)(const ir_node *irn);
239
240         /**
241          * Returns an inverse operation which yields the i-th argument
242          * of the given node as result.
243          *
244          * @param self      The this pointer.
245          * @param irn       The original operation
246          * @param i         Index of the argument we want the inverse operation to yield
247          * @param inverse   struct to be filled with the resulting inverse op
248          * @param obstack   The obstack to use for allocation of the returned nodes array
249          * @return          The inverse operation or NULL if operation invertible
250          */
251         arch_inverse_t *(*get_inverse)(const ir_node *irn, int i, arch_inverse_t *inverse, struct obstack *obstack);
252
253         /**
254          * Get the estimated cycle count for @p irn.
255          *
256          * @param self The this pointer.
257          * @param irn  The node.
258          *
259          * @return     The estimated cycle count for this operation
260          */
261         int (*get_op_estimated_cost)(const ir_node *irn);
262
263         /**
264          * Asks the backend whether operand @p i of @p irn can be loaded form memory internally
265          *
266          * @param self The this pointer.
267          * @param irn  The node.
268          * @param i    Index of the argument we would like to know whether @p irn can load it form memory internally
269          *
270          * @return     nonzero if argument can be loaded or zero otherwise
271          */
272         int (*possible_memory_operand)(const ir_node *irn, unsigned int i);
273
274         /**
275          * Ask the backend to assimilate @p reload of operand @p i into @p irn.
276          *
277          * @param self   The this pointer.
278          * @param irn    The node.
279          * @param spill  The spill.
280          * @param i      The position of the reload.
281          */
282         void (*perform_memory_operand)(ir_node *irn, ir_node *spill, unsigned int i);
283 };
284
285 /**
286  * The code generator interface.
287  */
288 struct arch_code_generator_if_t {
289         /**
290          * Initialize the code generator.
291          * @param birg A backend IRG session.
292          * @return     A newly created code generator.
293          */
294         void *(*init)(be_irg_t *birg);
295
296         /**
297          * return node used as base in pic code addresses
298          */
299         ir_node* (*get_pic_base)(void *self);
300
301         /**
302          * Called before abi introduce.
303          */
304         void (*before_abi)(void *self);
305
306         /**
307          * Called, when the graph is being normalized.
308          */
309         void (*prepare_graph)(void *self);
310
311         /**
312          * Backend may provide an own spiller.
313          * This spiller needs to spill all register classes.
314          */
315         void (*spill)(void *self, be_irg_t *birg);
316
317         /**
318          * Called before register allocation.
319          */
320         void (*before_ra)(void *self);
321
322         /**
323          * Called after register allocation.
324          */
325         void (*after_ra)(void *self);
326
327         /**
328          * Called directly before done is called. This should be the last place
329          * where the irg is modified.
330          */
331         void (*finish)(void *self);
332
333         /**
334          * Called after everything happened. This call should emit the final
335          * assembly code but avoid changing the irg.
336          * The code generator must also be de-allocated here.
337          */
338         void (*done)(void *self);
339 };
340
341 /**
342  * helper macro: call function func from the code generator
343  * if it's implemented.
344  */
345 #define _arch_cg_call(cg, func) \
346 do { \
347         if((cg)->impl->func) \
348                 (cg)->impl->func(cg); \
349 } while(0)
350
351 #define _arch_cg_call_env(cg, env, func) \
352 do { \
353         if((cg)->impl->func) \
354                 (cg)->impl->func(cg, env); \
355 } while(0)
356
357 #define arch_code_generator_before_abi(cg)      _arch_cg_call(cg, before_abi)
358 #define arch_code_generator_prepare_graph(cg)   _arch_cg_call(cg, prepare_graph)
359 #define arch_code_generator_before_ra(cg)       _arch_cg_call(cg, before_ra)
360 #define arch_code_generator_after_ra(cg)        _arch_cg_call(cg, after_ra)
361 #define arch_code_generator_finish(cg)          _arch_cg_call(cg, finish)
362 #define arch_code_generator_done(cg)            _arch_cg_call(cg, done)
363 #define arch_code_generator_spill(cg, birg)     _arch_cg_call_env(cg, birg, spill)
364 #define arch_code_generator_has_spiller(cg)     ((cg)->impl->spill != NULL)
365 #define arch_code_generator_get_pic_base(cg)    \
366         ((cg)->impl->get_pic_base != NULL ? (cg)->impl->get_pic_base(cg) : NULL)
367
368 /**
369  * Code generator base class.
370  */
371 struct arch_code_generator_t {
372         const arch_code_generator_if_t *impl;
373 };
374
375 /**
376  * Architecture interface.
377  */
378 struct arch_isa_if_t {
379         /**
380          * Initialize the isa interface.
381          * @param file_handle  the file handle to write the output to
382          * @return a new isa instance
383          */
384         arch_env_t *(*init)(FILE *file_handle);
385
386         /**
387          * Free the isa instance.
388          */
389         void (*done)(void *self);
390
391         /**
392          * Called directly after initialization. Backend should handle all
393          * intrinsics here.
394          */
395         void (*handle_intrinsics)(void);
396
397         /**
398          * Get the the number of register classes in the isa.
399          * @return The number of register classes.
400          */
401         unsigned (*get_n_reg_class)(const void *self);
402
403         /**
404          * Get the i-th register class.
405          * @param i The number of the register class.
406          * @return The register class.
407          */
408         const arch_register_class_t *(*get_reg_class)(const void *self, unsigned i);
409
410         /**
411          * Get the register class which shall be used to store a value of a given mode.
412          * @param self The this pointer.
413          * @param mode The mode in question.
414          * @return A register class which can hold values of the given mode.
415          */
416         const arch_register_class_t *(*get_reg_class_for_mode)(const void *self, const ir_mode *mode);
417
418         /**
419          * Get the ABI restrictions for procedure calls.
420          * @param self        The this pointer.
421          * @param call_type   The call type of the method (procedure) in question.
422          * @param p           The array of parameter locations to be filled.
423          */
424         void (*get_call_abi)(const void *self, ir_type *call_type, be_abi_call_t *abi);
425
426         /**
427          * Get the code generator interface.
428          * @param self The this pointer.
429          * @return     Some code generator interface.
430          */
431         const arch_code_generator_if_t *(*get_code_generator_if)(void *self);
432
433         /**
434          * Get the list scheduler to use. There is already a selector given, the
435          * backend is free to modify and/or ignore it.
436          *
437          * @param self     The isa object.
438          * @param selector The selector given by options.
439          * @return         The list scheduler selector.
440          */
441         const list_sched_selector_t *(*get_list_sched_selector)(const void *self, list_sched_selector_t *selector);
442
443         /**
444          * Get the ILP scheduler to use.
445          * @param self  The isa object.
446          * @return      The ILP scheduler selector
447          */
448         const ilp_sched_selector_t *(*get_ilp_sched_selector)(const void *self);
449
450         /**
451          * Get the necessary alignment for storing a register of given class.
452          * @param self  The isa object.
453          * @param cls   The register class.
454          * @return      The alignment in bytes.
455          */
456         int (*get_reg_class_alignment)(const void *self, const arch_register_class_t *cls);
457
458         /**
459          * A "static" function, returns the frontend settings
460          * needed for this backend.
461          */
462         const backend_params *(*get_params)(void);
463
464         /**
465          * Returns an 2-dim array of execution units, @p irn can be executed on.
466          * The first dimension is the type, the second the allowed units of this
467          * type.
468          * Each dimension is a NULL terminated list.
469          * @param self  The isa object.
470          * @param irn   The node.
471          * @return An array of allowed execution units.
472          *         exec_unit = {
473          *                       { unit1_of_tp1, ..., unitX1_of_tp1, NULL },
474          *                       ...,
475          *                       { unit1_of_tpY, ..., unitXn_of_tpY, NULL },
476          *                       NULL
477          *                     };
478          */
479         const be_execution_unit_t ***(*get_allowed_execution_units)(const void *self, const ir_node *irn);
480
481         /**
482          * Return the abstract machine for this isa.
483          * @param self  The isa object.
484          */
485         const be_machine_t *(*get_machine)(const void *self);
486
487         /**
488          * Return an ordered list of irgs where code should be generated for.
489          * If NULL is returned, all irg will be taken into account and they will be
490          * generated in an arbitrary order.
491          * @param self   The isa object.
492          * @param irgs   A flexible array ARR_F of length 0 where the backend can append the desired irgs.
493          * @return A flexible array ARR_F containing all desired irgs in the desired order.
494          */
495         ir_graph **(*get_backend_irg_list)(const void *self, ir_graph ***irgs);
496
497         /**
498          * mark node as rematerialized
499          */
500         void (*mark_remat)(const void *self, ir_node *node);
501
502         /**
503          * parse an assembler constraint part and set flags according to its nature
504          * advances the *c pointer to point to the last parsed character (so if you
505          * parse a single character don't advance c)
506          */
507         asm_constraint_flags_t (*parse_asm_constraint)(const void *self, const char **c);
508
509         /**
510          * returns true if the string is a valid clobbered (register) in this
511          * backend
512          */
513         int (*is_valid_clobber)(const void *self, const char *clobber);
514 };
515
516 #define arch_env_done(env)                             ((env)->impl->done(env))
517 #define arch_env_handle_intrinsics(env)                \
518         do { if((env)->impl->handle_intrinsics != NULL) (env)->impl->handle_intrinsics(); } while(0)
519 #define arch_env_get_n_reg_class(env)                  ((env)->impl->get_n_reg_class(env))
520 #define arch_env_get_reg_class(env,i)                  ((env)->impl->get_reg_class(env, i))
521 #define arch_env_get_reg_class_for_mode(env,mode)      ((env)->impl->get_reg_class_for_mode((env), (mode)))
522 #define arch_env_get_call_abi(env,tp,abi)              ((env)->impl->get_call_abi((env), (tp), (abi)))
523 #define arch_env_get_code_generator_if(env)            ((env)->impl->get_code_generator_if((env)))
524 #define arch_env_get_list_sched_selector(env,selector) ((env)->impl->get_list_sched_selector((env), (selector)))
525 #define arch_env_get_ilp_sched_selector(env)           ((env)->impl->get_ilp_sched_selector(env))
526 #define arch_env_get_reg_class_alignment(env,cls)      ((env)->impl->get_reg_class_alignment((env), (cls)))
527 #define arch_env_get_params(env)                       ((env)->impl->get_params())
528 #define arch_env_get_allowed_execution_units(env,irn)  ((env)->impl->get_allowed_execution_units((env), (irn)))
529 #define arch_env_get_machine(env)                      ((env)->impl->get_machine(env))
530 #define arch_env_get_backend_irg_list(env,irgs)        ((env)->impl->get_backend_irg_list((env), (irgs)))
531 #define arch_env_parse_asm_constraint(env,c)           ((env)->impl->parse_asm_constraint((env), (c))
532 #define arch_env_is_valid_clobber(env,clobber)         ((env)->impl->is_valid_clobber((env), (clobber))
533 #define arch_env_mark_remat(env,node) \
534         do { if ((env)->impl->mark_remat != NULL) (env)->impl->mark_remat((env), (node)); } while(0)
535
536 /**
537  * ISA base class.
538  */
539 struct arch_env_t {
540         const arch_isa_if_t   *impl;
541         const arch_register_t *sp;              /** The stack pointer register. */
542         const arch_register_t *bp;              /** The base pointer register. */
543         int                    stack_dir;       /** -1 for decreasing, 1 for increasing. */
544         int                    stack_alignment; /** power of 2 stack alignment */
545         const be_main_env_t   *main_env;        /** the be main environment */
546         int                    spill_cost;      /** cost for a be_Spill node */
547         int                    reload_cost;     /** cost for a be_Reload node */
548 };
549
550 #define arch_env_stack_dir(env)  ((env)->stack_dir)
551 #define arch_env_sp(env)         ((env)->sp)
552 #define arch_env_bp(env)         ((env)->bp)
553
554 static inline unsigned arch_irn_get_n_outs(const ir_node *node)
555 {
556         backend_info_t *info = be_get_info(node);
557         return ARR_LEN(info->out_infos);
558 }
559
560 static inline bool arch_irn_consider_in_reg_alloc(
561                 const arch_register_class_t *cls, const ir_node *node)
562 {
563         const arch_register_req_t *req = arch_get_register_req_out(node);
564         return
565                 req->cls == cls &&
566                 !(req->type & arch_register_req_type_ignore);
567 }
568
569 static inline bool arch_irn_is_ignore(const ir_node *irn)
570 {
571         const arch_register_req_t *req = arch_get_register_req_out(irn);
572         return !!(req->type & arch_register_req_type_ignore);
573 }
574
575 #endif