gcc has same costs for prescott and nocona
[libfirm] / ir / be / ia32 / ia32_architecture.c
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       ia32 architecture variants
23  * @author      Michael Beck, Matthias Braun
24  * @version     $Id: bearch_ia32_t.h 16363 2007-10-25 23:27:07Z beck $
25  */
26 #ifdef HAVE_CONFIG_H
27 #include "config.h"
28 #endif
29
30 #include "lc_opts.h"
31 #include "lc_opts_enum.h"
32
33 #include "irtools.h"
34
35 #include "bearch_ia32_t.h"
36 #include "ia32_architecture.h"
37
38 ia32_code_gen_config_t  ia32_cg_config;
39
40 /**
41  * CPU features.
42  */
43 enum cpu_arch_features {
44         arch_feature_intel    = 0x80000000,                      /**< Intel CPU */
45         arch_feature_amd      = 0x40000000,                      /**< AMD CPU */
46         arch_feature_p6       = 0x20000000,                      /**< P6 instructions */
47         arch_feature_mmx      = 0x10000000,                      /**< MMX instructions */
48         arch_feature_sse1     = 0x08000000 | arch_feature_mmx,   /**< SSE1 instructions, include MMX */
49         arch_feature_sse2     = 0x04000000 | arch_feature_sse1,  /**< SSE2 instructions, include SSE1 */
50         arch_feature_sse3     = 0x02000000 | arch_feature_sse2,  /**< SSE3 instructions, include SSE2 */
51         arch_feature_ssse3    = 0x01000000 | arch_feature_sse3,  /**< SSSE3 instructions, include SSE3 */
52         arch_feature_3DNow    = 0x00800000,                      /**< 3DNow! instructions */
53         arch_feature_3DNowE   = 0x00400000 | arch_feature_3DNow, /**< Enhanced 3DNow! instructions */
54         arch_feature_netburst = 0x00200000 | arch_feature_intel, /**< Netburst architecture */
55         arch_feature_64bit    = 0x00100000 | arch_feature_sse2,  /**< x86_64 support, include SSE2 */
56 };
57
58 /**
59  * Architectures.
60  */
61 enum cpu_support {
62         /* intel CPU's */
63         arch_generic          =  0,
64
65         arch_i386        =  1,
66         arch_i486        =  2,
67         arch_pentium     =  3 | arch_feature_intel,
68         arch_pentium_mmx =  4 | arch_feature_intel | arch_feature_mmx,
69         arch_pentium_pro =  5 | arch_feature_intel | arch_feature_p6,
70         arch_pentium_2   =  6 | arch_feature_intel | arch_feature_p6 | arch_feature_mmx,
71         arch_pentium_3   =  7 | arch_feature_intel | arch_feature_p6 | arch_feature_sse1,
72         arch_pentium_4   =  8 | arch_feature_netburst | arch_feature_p6 | arch_feature_sse2,
73         arch_prescott    =  9 | arch_feature_netburst | arch_feature_p6 | arch_feature_sse3,
74         arch_nocona      = 10 | arch_feature_netburst | arch_feature_p6 arch_feature_64bit | arch_feature_sse3,
75         arch_pentium_m   = 11 | arch_feature_intel | arch_feature_p6 | arch_feature_sse2,
76         arch_core        = 12 | arch_feature_intel | arch_feature_p6 | arch_feature_sse3,
77         arch_core2       = 13 | arch_feature_intel | arch_feature_p6 | arch_feature_64bit | arch_feature_ssse3,
78
79         /* AMD CPU's */
80         arch_k6          = 14 | arch_feature_amd | arch_feature_mmx,
81         arch_geode       = 15 | arch_feature_amd | arch_feature_mmx | arch_feature_3DNowE,
82         arch_k6_2        = 16 | arch_feature_amd | arch_feature_mmx | arch_feature_3DNow,
83         arch_k6_3        = 17 | arch_feature_amd | arch_feature_mmx | arch_feature_3DNow,
84         arch_athlon      = 18 | arch_feature_amd | arch_feature_mmx | arch_feature_3DNowE | arch_feature_p6,
85         arch_athlon_xp   = 19 | arch_feature_amd | arch_feature_sse1 | arch_feature_3DNowE | arch_feature_p6,
86         arch_opteron     = 20 | arch_feature_amd | arch_feature_64bit | arch_feature_3DNowE | arch_feature_p6,
87
88         /* other */
89         arch_winchip_c6  = 21 | arch_feature_mmx,
90         arch_winchip2    = 22 | arch_feature_mmx | arch_feature_3DNow,
91         arch_c3          = 23 | arch_feature_mmx | arch_feature_3DNow,
92         arch_c3_2        = 24 | arch_feature_sse1,  /* really no 3DNow! */
93 };
94
95 /** checks for l <= x <= h */
96 #define _IN_RANGE(x, l, h)  ((unsigned)((x) - (l)) <= (unsigned)((h) - (l)))
97
98 /** returns true if it's Intel architecture */
99 #define ARCH_INTEL(x)       (((x) & arch_feature_intel) != 0)
100
101 /** returns true if it's AMD architecture */
102 #define ARCH_AMD(x)         (((x) & arch_feature_amd) != 0)
103
104 /** return true if it's a Athlon/Opteron */
105 #define ARCH_ATHLON(x)      _IN_RANGE((x), arch_athlon, arch_opteron)
106
107 /** return true if the CPU has MMX support */
108 #define ARCH_MMX(x)         (((x) & arch_feature_mmx) != 0)
109
110 /** return true if the CPU has 3DNow! support */
111 #define ARCH_3DNow(x)       (((x) & arch_feature_3DNow) != 0)
112
113 /** return true if the CPU has P6 features (CMOV) */
114 #define IS_P6_ARCH(x)       (((x) & arch_feature_p6) != 0)
115
116 static cpu_support arch                 = arch_generic;
117 static cpu_support opt_arch             = arch_pentium_4;
118 static int         use_sse2             = 0;
119 static int         opt_cc               = 1;
120 static int         opt_unsafe_floatconv = 0;
121
122 /* instruction set architectures. */
123 static const lc_opt_enum_int_items_t arch_items[] = {
124         { "i386",       arch_i386, },
125         { "i486",       arch_i486, },
126         { "pentium",    arch_pentium, },
127         { "i586",       arch_pentium, },
128         { "pentiumpro", arch_pentium_pro, },
129         { "i686",       arch_pentium_pro, },
130         { "pentiummmx", arch_pentium_mmx, },
131         { "pentium2",   arch_pentium_2, },
132         { "p2",         arch_pentium_2, },
133         { "pentium3",   arch_pentium_3, },
134         { "p3",         arch_pentium_3, },
135         { "pentium4",   arch_pentium_4, },
136         { "p4",         arch_pentium_4, },
137         { "prescott",   arch_prescott, },
138         { "nocona",     arch_nocona, },
139         { "pentiumm",   arch_pentium_m, },
140         { "pm",         arch_pentium_m, },
141         /*
142          * core CPUs: Yonah
143          */
144         { "core",       arch_core, },
145         { "yonah",      arch_core, },
146         /*
147          * core2 CPUs: Conroe (XE, L), Allendale, Merom (XE),
148          * Kentsfield (XE), Yorkfield XE, Penryn, Wolfdale, Yorkfield
149          */
150         { "merom",      arch_core2, },
151         { "core2",      arch_core2, },
152         { "k6",         arch_k6, },
153         { "k6-2",       arch_k6_2, },
154         { "k6-3",       arch_k6_2, },
155         { "geode",      arch_geode, },
156         { "athlon",     arch_athlon, },
157         { "athlon-xp",  arch_athlon_xp, },
158         { "athlon-mp",  arch_athlon_xp, },
159         { "athlon-4",   arch_athlon_xp, },
160         { "athlon64",   arch_opteron, },
161         { "k8",         arch_opteron, },
162         { "k10",        arch_opteron, },
163         { "opteron",    arch_opteron, },
164         { "generic",    arch_generic, },
165         { NULL,         0 }
166 };
167
168 static lc_opt_enum_int_var_t arch_var = {
169         (int*) &arch, arch_items
170 };
171
172 static lc_opt_enum_int_var_t opt_arch_var = {
173         (int*) &opt_arch, arch_items
174 };
175
176 static const lc_opt_enum_int_items_t fp_unit_items[] = {
177         { "x87" ,    0 },
178         { "sse2",    1 },
179         { NULL,      0 }
180 };
181
182 static lc_opt_enum_int_var_t fp_unit_var = {
183         &use_sse2, fp_unit_items
184 };
185
186 static const lc_opt_table_entry_t ia32_architecture_options[] = {
187         LC_OPT_ENT_ENUM_INT("arch",        "select the instruction architecture",
188                             &arch_var),
189         LC_OPT_ENT_ENUM_INT("opt",         "optimize for instruction architecture",
190                             &opt_arch_var),
191         LC_OPT_ENT_ENUM_INT("fpunit",      "select the floating point unit",
192                             &fp_unit_var),
193         LC_OPT_ENT_NEGBIT("nooptcc",       "do not optimize calling convention",
194                           &opt_cc, 1),
195         LC_OPT_ENT_BIT("unsafe_floatconv", "do unsafe floating point controlword "
196                        "optimisations", &opt_unsafe_floatconv, 1),
197         LC_OPT_LAST
198 };
199
200 typedef struct insn_const {
201         int add_cost;       /**< cost of an add instruction */
202         int lea_cost;       /**< cost of a lea instruction */
203         int const_shf_cost; /**< cost of a constant shift instruction */
204         int cost_mul_start; /**< starting cost of a multiply instruction */
205         int cost_mul_bit;   /**< cost of multiply for every set bit */
206 } insn_const;
207
208 #define COSTS_INSNS(x)  (4 * (x))
209
210 /* costs for the i386 */
211 static const insn_const i386_cost = {
212         COSTS_INSNS(1),   /* cost of an add instruction */
213         COSTS_INSNS(1),   /* cost of a lea instruction */
214         COSTS_INSNS(3),   /* cost of a constant shift instruction */
215         COSTS_INSNS(9),   /* starting cost of a multiply instruction */
216         COSTS_INSNS(1)    /* cost of multiply for every set bit */
217 };
218
219 /* costs for the i486 */
220 static const insn_const i486_cost = {
221         COSTS_INSNS(1),   /* cost of an add instruction */
222         COSTS_INSNS(1),   /* cost of a lea instruction */
223         COSTS_INSNS(2),   /* cost of a constant shift instruction */
224         COSTS_INSNS(12),  /* starting cost of a multiply instruction */
225         COSTS_INSNS(1)    /* cost of multiply for every set bit */
226 };
227
228 /* costs for the Pentium */
229 static const insn_const pentium_cost = {
230         COSTS_INSNS(1),   /* cost of an add instruction */
231         COSTS_INSNS(1),   /* cost of a lea instruction */
232         COSTS_INSNS(1),   /* cost of a constant shift instruction */
233         COSTS_INSNS(11),  /* starting cost of a multiply instruction */
234         0    /* cost of multiply for every set bit */
235 };
236
237 /* costs for the Pentium Pro */
238 static const insn_const pentiumpro_cost = {
239         COSTS_INSNS(1),   /* cost of an add instruction */
240         COSTS_INSNS(1),   /* cost of a lea instruction */
241         COSTS_INSNS(1),   /* cost of a constant shift instruction */
242         COSTS_INSNS(4),   /* starting cost of a multiply instruction */
243         0    /* cost of multiply for every set bit */
244 };
245
246 /* costs for the Geode */
247 static const insn_const geode_cost = {
248         COSTS_INSNS(1),   /* cost of an add instruction */
249         COSTS_INSNS(1),   /* cost of a lea instruction */
250         COSTS_INSNS(1),   /* cost of a constant shift instruction */
251         COSTS_INSNS(7),   /* starting cost of a multiply instruction */
252         0    /* cost of multiply for every set bit */
253 };
254
255 /* costs for the K6 */
256 static const insn_const k6_cost = {
257         COSTS_INSNS(1),   /* cost of an add instruction */
258         COSTS_INSNS(2),   /* cost of a lea instruction */
259         COSTS_INSNS(1),   /* cost of a constant shift instruction */
260         COSTS_INSNS(3),   /* starting cost of a multiply instruction */
261         0    /* cost of multiply for every set bit */
262 };
263
264 /* costs for the Athlon */
265 static const insn_const athlon_cost = {
266         COSTS_INSNS(1),   /* cost of an add instruction */
267         COSTS_INSNS(2),   /* cost of a lea instruction */
268         COSTS_INSNS(1),   /* cost of a constant shift instruction */
269         COSTS_INSNS(5),   /* starting cost of a multiply instruction */
270         0    /* cost of multiply for every set bit */
271 };
272
273 /* costs for the K8 */
274 static const insn_const k8_cost = {
275         COSTS_INSNS(1),   /* cost of an add instruction */
276         COSTS_INSNS(2),   /* cost of a lea instruction */
277         COSTS_INSNS(1),   /* cost of a constant shift instruction */
278         COSTS_INSNS(3),   /* starting cost of a multiply instruction */
279         0    /* cost of multiply for every set bit */
280 };
281
282 /* costs for the Pentium 4 */
283 static const insn_const pentium4_cost = {
284         COSTS_INSNS(1),   /* cost of an add instruction */
285         COSTS_INSNS(3),   /* cost of a lea instruction */
286         COSTS_INSNS(4),   /* cost of a constant shift instruction */
287         COSTS_INSNS(15),  /* starting cost of a multiply instruction */
288         0    /* cost of multiply for every set bit */
289 };
290
291 /* costs for the Pentium 4 nocona, Core */
292 static const insn_const nocona_cost = {
293         COSTS_INSNS(1),   /* cost of an add instruction */
294         COSTS_INSNS(1),   /* cost of a lea instruction */
295         COSTS_INSNS(1),   /* cost of a constant shift instruction */
296         COSTS_INSNS(10),  /* starting cost of a multiply instruction */
297         0    /* cost of multiply for every set bit */
298 };
299
300 /* costs for the Core2 */
301 static const insn_const core2_cost = {
302         COSTS_INSNS(1),   /* cost of an add instruction */
303         COSTS_INSNS(1) + 1,   /* cost of a lea instruction */
304         COSTS_INSNS(1),   /* cost of a constant shift instruction */
305         COSTS_INSNS(3),   /* starting cost of a multiply instruction */
306         0    /* cost of multiply for every set bit */
307 };
308
309 /* costs for the generic */
310 static const insn_const generic_cost = {
311         COSTS_INSNS(1),   /* cost of an add instruction */
312         COSTS_INSNS(1) + 1,   /* cost of a lea instruction */
313         COSTS_INSNS(1),   /* cost of a constant shift instruction */
314         COSTS_INSNS(4),   /* starting cost of a multiply instruction */
315         0    /* cost of multiply for every set bit */
316 };
317
318 static const insn_const *arch_costs = &generic_cost;
319
320 static void set_arch_costs(void)
321 {
322         switch (opt_arch) {
323         case arch_i386:
324                 arch_costs = &i386_cost;
325                 break;
326         case arch_i486:
327                 arch_costs = &i486_cost;
328                 break;
329         case arch_pentium:
330         case arch_pentium_mmx:
331                 arch_costs = &pentium_cost;
332                 break;
333         case arch_pentium_pro:
334         case arch_pentium_2:
335         case arch_pentium_3:
336                 arch_costs = &pentiumpro_cost;
337                 break;
338         case arch_pentium_4:
339                 arch_costs = &pentium4_cost;
340                 break;
341         case arch_pentium_m:
342                 arch_costs = &pentiumpro_cost;
343                 break;
344         case arch_nocona:
345         case arch_prescott:
346         case arch_core:
347                 arch_costs = &nocona_cost;
348                 break;
349         case arch_core2:
350                 arch_costs = &core2_cost;
351                 break;
352         case arch_k6:
353         case arch_k6_2:
354                 arch_costs = &k6_cost;
355                 break;
356         case arch_geode:
357                 arch_costs = &geode_cost;
358                 break;
359         case arch_athlon:
360         case arch_athlon_xp:
361         case arch_opteron:
362                 arch_costs = &athlon_cost;
363                 break;
364         case arch_generic:
365         default:
366                 arch_costs = &generic_cost;
367         }
368 }
369
370 /**
371  * Evaluate a given simple instruction.
372  */
373 int ia32_evaluate_insn(insn_kind kind, tarval *tv) {
374         int cost;
375
376         switch (kind) {
377         case MUL:
378                 cost =  arch_costs->cost_mul_start;
379                 if (arch_costs->cost_mul_bit > 0) {
380                         char *bitstr = get_tarval_bitpattern(tv);
381                         int i;
382
383                         for (i = 0; bitstr[i] != '\0'; ++i) {
384                                 if (bitstr[i] == '1') {
385                                         cost += arch_costs->cost_mul_bit;
386                                 }
387                         }
388                         free(bitstr);
389                 }
390                 return cost;
391         case LEA:
392                 return arch_costs->lea_cost;
393         case ADD:
394         case SUB:
395                 return arch_costs->add_cost;
396         case SHIFT:
397                 return arch_costs->const_shf_cost;
398         case ZERO:
399                 return arch_costs->add_cost;
400         default:
401                 return COSTS_INSNS(1);
402         }
403 }
404
405
406
407 void ia32_setup_cg_config(void)
408 {
409         memset(&ia32_cg_config, 0, sizeof(ia32_cg_config));
410
411         /* on newer intel cpus mov, pop is often faster then leave although it has a
412          * longer opcode */
413         ia32_cg_config.use_leave            = !ARCH_INTEL(opt_arch)
414                                                || !IS_P6_ARCH(opt_arch);
415         /* P4s don't like inc/decs because they only partially write the flags
416            register which produces false dependencies */
417         ia32_cg_config.use_incdec           = !(opt_arch & arch_feature_netburst) && (opt_arch != arch_generic);
418         ia32_cg_config.use_sse2             = use_sse2;
419         ia32_cg_config.use_ffreep           = ARCH_ATHLON(opt_arch);
420         ia32_cg_config.use_ftst             = !IS_P6_ARCH(arch);
421         ia32_cg_config.use_femms            = ARCH_ATHLON(opt_arch)
422                                               && ARCH_MMX(arch) && ARCH_AMD(arch);
423         ia32_cg_config.use_fucomi           = IS_P6_ARCH(arch);
424         ia32_cg_config.use_cmov             = IS_P6_ARCH(arch);
425         ia32_cg_config.optimize_cc          = opt_cc;
426         ia32_cg_config.use_unsafe_floatconv = opt_unsafe_floatconv;
427
428         if(opt_arch == arch_i386) {
429                 ia32_cg_config.function_alignment = 2;
430         } else if(opt_arch == arch_i486) {
431                 ia32_cg_config.function_alignment = 4;
432         } else if(opt_arch == arch_k6) {
433                 ia32_cg_config.function_alignment = 5;
434                 ia32_cg_config.label_alignment    = 5;
435         } else {
436                 ia32_cg_config.function_alignment = 4;
437                 ia32_cg_config.label_alignment    = 4;
438         }
439
440         if(opt_arch == arch_i386 || opt_arch == arch_i486) {
441                 ia32_cg_config.label_alignment_factor = -1;
442         } else if(ARCH_AMD(opt_arch)) {
443                 ia32_cg_config.label_alignment_factor = 3;
444         } else {
445                 ia32_cg_config.label_alignment_factor = 2;
446         }
447
448         set_arch_costs();
449 }
450
451 void ia32_init_architecture(void)
452 {
453         lc_opt_entry_t *be_grp, *ia32_grp;
454
455         memset(&ia32_cg_config, 0, sizeof(ia32_cg_config));
456
457         be_grp   = lc_opt_get_grp(firm_opt_get_root(), "be");
458         ia32_grp = lc_opt_get_grp(be_grp, "ia32");
459
460         lc_opt_add_table(ia32_grp, ia32_architecture_options);
461 }