ia32: Clean up ia32_get_op_estimated_cost().
[libfirm] / ir / be / ia32 / ia32_nodes_attr.h
1 /*
2  * This file is part of libFirm.
3  * Copyright (C) 2012 University of Karlsruhe.
4  */
5
6 /**
7  * @file
8  * @brief       Type definitions for ia32 node attributes.
9  * @author      Christian Wuerdig
10  */
11 #ifndef FIRM_BE_IA32_IA32_NODES_ATTR_H
12 #define FIRM_BE_IA32_IA32_NODES_ATTR_H
13
14 #include "firm_types.h"
15 #include "bearch.h"
16 #include "irnode_t.h"
17
18 /** ia32 condition codes (the numbers correspond to the real encoding order) */
19 typedef enum ia32_condition_code_t {
20         ia32_cc_negated       = 0x01, /**< negates condition */
21
22         ia32_cc_overflow      = 0x00,                                /**< OF=1 */
23         ia32_cc_below         = 0x02,                                /**< CF=1 */
24         ia32_cc_equal         = 0x04,                                /**< ZF=1 */
25         ia32_cc_below_equal   = 0x06,                                /**< ZF=1 or CF=1 */
26         ia32_cc_sign          = 0x08,                                /**< SF=1 */
27         ia32_cc_parity        = 0x0A,                                /**< PF=1 */
28         ia32_cc_less          = 0x0C,                                /**< SF!=OF */
29         ia32_cc_less_equal    = 0x0E,                                /**< ZF=1 or SF!=OF */
30         ia32_cc_not_overflow  = ia32_cc_negated|ia32_cc_overflow,    /**< OF=0 */
31         ia32_cc_above_equal   = ia32_cc_negated|ia32_cc_below,       /**< CF=0 */
32         ia32_cc_not_equal     = ia32_cc_negated|ia32_cc_equal,       /**< ZF=0 */
33         ia32_cc_above         = ia32_cc_negated|ia32_cc_below_equal, /**< ZF=0 and CF=0 */
34         ia32_cc_not_sign      = ia32_cc_negated|ia32_cc_sign,        /**< SF=0 */
35         ia32_cc_not_parity    = ia32_cc_negated|ia32_cc_parity,      /**< PF=0 */
36         ia32_cc_greater_equal = ia32_cc_negated|ia32_cc_less,        /**< SF=OF */
37         ia32_cc_greater       = ia32_cc_negated|ia32_cc_less_equal,  /**< ZF=0 and SF=OF */
38
39         /* the following codes are (unfortunately) NOT real hardware codes but
40          * simplify our backend as you need these combinations for some
41          * floatingpoint compares (the emitter will split them into multiple
42          * instructions) */
43         ia32_cc_float_parity_cases = 0x20,
44         /* we need even more cases as inversing the cc is different for float
45          * comparisons (though for the following we need no special
46          * parity+x combinations) */
47         ia32_cc_additional_float_cases = 0x10,
48
49         /* make sure that the lower 4 bit correspond to the real encoding
50          * (of the comparison not involving the parity special) */
51         ia32_cc_float_equal        = 0x34,                                /**< PF=0 and ZF=1 */
52         ia32_cc_float_below        = 0x32,                                /**< PF=0 and CF=1 */
53         ia32_cc_float_below_equal  = 0x36,                                /**< PF=0 and (ZF=1 or CF=1) */
54         ia32_cc_float_not_equal    = ia32_cc_negated|ia32_cc_float_equal, /**< PF=1 or ZF=0 */
55         ia32_cc_float_unordered_above_equal
56                 = ia32_cc_negated|ia32_cc_float_below,                        /**< PF=1 or CF=0 */
57         ia32_cc_float_unordered_above
58                 = ia32_cc_negated|ia32_cc_float_below_equal,                  /**< PF=1 or (ZF=0 and CF=0) */
59
60         ia32_cc_float_unordered_below_equal = 0x16,                       /**< ZF=1 or CF=1 */
61         ia32_cc_float_unordered_below       = 0x12,                       /**< CF=1 */
62         ia32_cc_float_above        =
63                 ia32_cc_negated|ia32_cc_float_unordered_below_equal,          /**< ZF=0 and CF=0 */
64         ia32_cc_float_above_equal
65                 = ia32_cc_negated|ia32_cc_float_unordered_below,              /**< CF=0 */
66 } ia32_condition_code_t;
67 ENUM_BITSET(ia32_condition_code_t)
68
69 static inline ia32_condition_code_t ia32_negate_condition_code(
70                 ia32_condition_code_t code)
71 {
72         return code ^ ia32_cc_negated;
73 }
74
75 static inline ia32_condition_code_t ia32_invert_condition_code(
76                 ia32_condition_code_t code)
77 {
78         /* doesn't appear to have any systematic, so use a table */
79         switch (code) {
80         case ia32_cc_below:              return ia32_cc_above;
81         case ia32_cc_below_equal:        return ia32_cc_above_equal;
82         case ia32_cc_above:              return ia32_cc_below;
83         case ia32_cc_above_equal:        return ia32_cc_below_equal;
84         case ia32_cc_less:               return ia32_cc_greater;
85         case ia32_cc_less_equal:         return ia32_cc_greater_equal;
86         case ia32_cc_greater:            return ia32_cc_less;
87         case ia32_cc_greater_equal:      return ia32_cc_less_equal;
88         case ia32_cc_float_below:        return ia32_cc_float_above;
89         case ia32_cc_float_below_equal:  return ia32_cc_float_above_equal;
90         case ia32_cc_float_above:        return ia32_cc_float_below;
91         case ia32_cc_float_above_equal:  return ia32_cc_float_below_equal;
92         case ia32_cc_float_unordered_below:       return ia32_cc_float_unordered_above;
93         case ia32_cc_float_unordered_below_equal: return ia32_cc_float_unordered_above_equal;
94         case ia32_cc_float_unordered_above:       return ia32_cc_float_unordered_below;
95         case ia32_cc_float_unordered_above_equal: return ia32_cc_float_unordered_below_equal;
96         default:                         return code;
97         }
98 }
99
100 typedef enum {
101         ia32_Normal,
102         ia32_AddrModeD,
103         ia32_AddrModeS
104 } ia32_op_type_t;
105
106 typedef enum {
107         ia32_am_none   = 0,
108         ia32_am_unary  = 1,
109         ia32_am_binary = 2
110 } ia32_am_type_t;
111
112 typedef enum {
113         match_commutative       = 1 << 0, /**< inputs are commutative */
114         match_am_and_immediates = 1 << 1, /**< node supports AM and immediate at
115                                                the same time */
116         match_am                = 1 << 2, /**< node supports (32bit) source AM */
117         match_8bit_am           = 1 << 3, /**< node supports 8bit source AM */
118         match_16bit_am          = 1 << 4, /**< node supports 16bit source AM */
119         match_immediate         = 1 << 5, /**< node supports immediates */
120         /** for 8/16 bit modes, mode_neutral operations can be emulated by their
121          * 32bit equivalents, they just don't care about the upper bits (they can be
122          * arbitrary before the insn and are unknown after the instruction). */
123         match_mode_neutral      = 1 << 6,
124         /** for 8/16 bit modes, zero_ext operations can be emulated by their
125          * 32bit equivalents, however the upper bits must be zero extended. */
126         match_zero_ext          = 1 << 7,
127         /** for 8/16 bit modes, upconv operations can be emulated by their
128          * 32bit equivalents, however the upper bits have to sign/zero extended
129          * based on the operations mode. */
130         match_upconv            = 1 << 8,
131         match_try_am            = 1 << 9, /**< only try to produce AM node, don't
132                                                do anything if AM isn't possible */
133         match_two_users         = 1 << 10,/**< the instruction uses a load two times ... */
134 } match_flags_t;
135 ENUM_BITSET(match_flags_t)
136
137 typedef struct ia32_op_attr_t ia32_op_attr_t;
138 struct ia32_op_attr_t {
139         //match_flags_t  flags;
140         unsigned       latency;
141 };
142
143 #ifndef NDEBUG
144 typedef enum {
145         IA32_ATTR_INVALID                = 0,
146         IA32_ATTR_ia32_attr_t            = 1 << 0,
147         IA32_ATTR_ia32_x87_attr_t        = 1 << 1,
148         IA32_ATTR_ia32_asm_attr_t        = 1 << 2,
149         IA32_ATTR_ia32_immediate_attr_t  = 1 << 3,
150         IA32_ATTR_ia32_condcode_attr_t   = 1 << 4,
151         IA32_ATTR_ia32_copyb_attr_t      = 1 << 5,
152         IA32_ATTR_ia32_call_attr_t       = 1 << 6,
153         IA32_ATTR_ia32_climbframe_attr_t = 1 << 7,
154         IA32_ATTR_ia32_switch_attr_t     = 1 << 8,
155 } ia32_attr_type_t;
156 #endif
157
158 /**
159  * The generic ia32 attributes. Every node has them.
160  */
161 typedef struct ia32_attr_t ia32_attr_t;
162 struct ia32_attr_t {
163         except_attr  exc;               /**< the exception attribute. MUST be the first one. */
164         struct ia32_attr_data_bitfield {
165                 unsigned tp:3;                  /**< ia32 node type. */
166                 unsigned am_arity:2;            /**< Indicates the address mode type supported by this node. */
167                 unsigned am_scale:2;            /**< The address mode scale for index register. */
168                 unsigned am_sc_sign:1;          /**< The sign bit of the address mode symconst. */
169
170                 unsigned am_sc_no_pic_adjust : 1;/**< AM symconst can be relative to EIP */
171                 unsigned am_tls_segment:1;       /**< addresses are relative to TLS */
172                 unsigned use_frame:1;           /**< Indicates whether the operation uses the frame pointer or not. */
173                 unsigned has_except_label:1;        /**< Set if this node needs a label because of possible exception. */
174
175                 unsigned is_commutative:1;      /**< Indicates whether op is commutative or not. */
176
177                 unsigned need_stackent:1;       /**< Set to 1 if node need space on stack. */
178                 unsigned need_64bit_stackent:1; /**< needs a 64bit stack entity (see double->unsigned int conv) */
179                 unsigned need_32bit_stackent:1; /**< needs a 32bit stack entity */
180                 unsigned ins_permuted : 1;      /**< inputs of node have been permuted
181                                                      (for commutative nodes) */
182                 unsigned is_reload : 1;         /**< node performs a reload */
183                 unsigned is_spill : 1;
184                 unsigned is_remat : 1;
185         } data;
186
187         int        am_offs;       /**< offsets for AddrMode */
188         ir_entity *am_sc;         /**< SymConst for AddrMode */
189
190         ir_mode   *ls_mode;       /**< Load/Store mode: This is the mode of the
191                                        value that is manipulated by this node. */
192
193         ir_entity *frame_ent; /**< the frame entity attached to this node */
194
195         ir_label_t        exc_label;       /**< the exception label iff this instruction can throw an exception */
196
197 #ifndef NDEBUG
198         const char       *orig_node;      /**< holds the name of the original ir node */
199         unsigned          attr_type;      /**< bitfield indicating the attribute type */
200 #endif
201 };
202
203 /**
204  * The attributes for a Call node.
205  */
206 typedef struct ia32_call_attr_t ia32_call_attr_t;
207 struct ia32_call_attr_t {
208         ia32_attr_t  attr;    /**< generic attribute */
209         unsigned     pop;     /**< number of bytes that get popped by the callee */
210         ir_type     *call_tp; /**< The call type, copied from the original Call node. */
211 };
212
213 /**
214  * The attributes for nodes with condition code.
215  */
216 typedef struct ia32_condcode_attr_t ia32_condcode_attr_t;
217 struct ia32_condcode_attr_t {
218         ia32_attr_t           attr;           /**< generic attribute */
219         ia32_condition_code_t condition_code; /**< condition code*/
220 };
221
222 /**
223  * The attributes for Switches
224  */
225 typedef struct ia32_switch_attr_t ia32_switch_attr_t;
226 struct ia32_switch_attr_t {
227         ia32_attr_t            attr;        /**< generic attribute */
228         const ir_switch_table *table;
229         ir_entity             *jump_table;
230 };
231
232 /**
233  * The attributes for CopyB code.
234  */
235 typedef struct ia32_copyb_attr_t ia32_copyb_attr_t;
236 struct ia32_copyb_attr_t {
237         ia32_attr_t  attr;      /**< generic attribute */
238         unsigned     size;      /**< size of copied block */
239 };
240
241 /**
242  * The attributes for immediates.
243  */
244 typedef struct ia32_immediate_attr_t ia32_immediate_attr_t;
245 struct ia32_immediate_attr_t {
246         ia32_attr_t  attr;              /**< generic attribute */
247         ir_entity   *symconst;          /**< An entity if any. */
248         long         offset;            /**< An offset if any. */
249         unsigned     sc_sign : 1;       /**< The sign bit of the symconst. */
250         unsigned     no_pic_adjust : 1; /**< constant can be relative to EIP */
251 };
252
253 /**
254  * The attributes for x87 nodes.
255  */
256 typedef struct ia32_x87_attr_t ia32_x87_attr_t;
257 struct ia32_x87_attr_t {
258         ia32_attr_t            attr;       /**< the generic attribute */
259         arch_register_t const *reg;        /**< The explicit register operand. */
260         bool                   res_in_reg; /**< True if the result is in the explicit register operand, %st0 otherwise. */
261         bool                   pop;        /**< Emit a pop suffix. */
262 };
263
264 typedef struct ia32_asm_reg_t ia32_asm_reg_t;
265 struct ia32_asm_reg_t {
266         unsigned                   use_input  : 1; /* use input or output pos */
267         unsigned                   valid      : 1;
268         unsigned                   memory     : 1;
269         unsigned                   dummy_fill : 13;
270         unsigned                   inout_pos  : 16; /* in/out pos where the
271                                                        register is assigned */
272         const ir_mode             *mode;
273 };
274
275 /**
276  * The attributes for ASM nodes.
277  */
278 typedef struct ia32_asm_attr_t ia32_asm_attr_t;
279 struct ia32_asm_attr_t {
280         ia32_attr_t           attr;         /**< the generic attribute */
281         ident                *asm_text;
282         const ia32_asm_reg_t *register_map;
283 };
284
285 /**
286  * The attributes for the ClimbFrame node.
287  */
288 typedef struct ia32_climbframe_attr_t ia32_climbframe_attr_t;
289 struct ia32_climbframe_attr_t {
290         ia32_attr_t attr;      /**< generic attribute */
291         unsigned    count;     /**< number of frames to climb up */
292 };
293
294 /* the following union is necessary to indicate to the compiler that we might want to cast
295  * the structs (we use them to simulate OO-inheritance) */
296 union allow_casts_attr_t_ {
297         ia32_attr_t            attr;
298         ia32_call_attr_t       call_attr;
299         ia32_condcode_attr_t   cc_attr;
300         ia32_copyb_attr_t      cpy_attr;
301         ia32_x87_attr_t        x87_attr;
302         ia32_asm_attr_t        asm_attr;
303         ia32_immediate_attr_t  immediate_attr;
304         ia32_climbframe_attr_t climbframe_attr;
305         ia32_switch_attr_t     switch_attr;
306 };
307
308 #ifndef NDEBUG
309 #define CAST_IA32_ATTR(type,ptr)        (assert( ((const ia32_attr_t*)(ptr))->attr_type & IA32_ATTR_ ## type ), (type*) (ptr))
310 #define CONST_CAST_IA32_ATTR(type,ptr)  (assert( ((const ia32_attr_t*)(ptr))->attr_type & IA32_ATTR_ ## type ), (const type*) (ptr))
311 #else
312 #define CAST_IA32_ATTR(type,ptr)        ((type*) (ptr))
313 #define CONST_CAST_IA32_ATTR(type,ptr)  ((const type*) (ptr))
314 #endif
315
316 #endif