Add missing '|' as wrong patch was applied.
[official-gcc.git] / gcc / config / rs6000 / driver-rs6000.c
blob972099a47ee81f31d8b9a0eeb53fb782de1a6e77
1 /* Subroutines for the gcc driver.
2 Copyright (C) 2007-2018 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3, or (at your option)
9 any later version.
11 GCC is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
20 #define IN_TARGET_CODE 1
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "tm.h"
26 #include "diagnostic.h"
27 #include "opts.h"
28 #include <stdlib.h>
30 #ifdef _AIX
31 # include <sys/systemcfg.h>
32 #endif
34 #ifdef __linux__
35 # include <link.h>
36 #endif
38 #if defined (__APPLE__) || (__FreeBSD__)
39 # include <sys/types.h>
40 # include <sys/sysctl.h>
41 #endif
43 #ifdef __linux__
44 /* Canonical GCC cpu name table. */
45 static const char *rs6000_supported_cpu_names[] =
47 #define RS6000_CPU(NAME, CPU, FLAGS) NAME,
48 #include "rs6000-cpus.def"
49 #undef RS6000_CPU
52 /* This table holds a list of cpus where their Linux AT_PLATFORM name differs
53 from their GCC canonical name. The first column in a row contains the GCC
54 canonical cpu name and the other columns in that row contain AT_PLATFORM
55 names that should be mapped to the canonical name. */
57 static const char *linux_cpu_translation_table[][4] = {
58 { "403", "ppc403", NULL },
59 { "405", "ppc405", NULL },
60 { "440", "ppc440", "ppc440gp", NULL },
61 { "476", "ppc470", NULL },
62 { "601", "ppc601", NULL },
63 { "603", "ppc603", NULL },
64 { "604", "ppc604", NULL },
65 { "7400", "ppc7400", NULL },
66 { "7450", "ppc7450", NULL },
67 { "750", "ppc750", NULL },
68 { "823", "ppc823", NULL },
69 { "8540", "ppc8540", NULL },
70 { "8548", "ppc8548", NULL },
71 { "970", "ppc970", NULL },
72 { "cell", "ppc-cell-be", NULL },
73 { "e500mc", "ppce500mc", NULL },
74 { "e5500", "ppce5500", NULL },
75 { "e6500", "ppce6500", NULL },
76 { "power7", "power7+", NULL },
77 { NULL } /* End of table sentinel. */
79 #endif
81 const char *host_detect_local_cpu (int argc, const char **argv);
83 #if GCC_VERSION >= 0
85 /* Returns parameters that describe L1_ASSOC associative cache of size
86 L1_SIZEKB with lines of size L1_LINE, and L2_SIZEKB. */
88 static char *
89 describe_cache (unsigned l1_sizekb, unsigned l1_line,
90 unsigned l1_assoc ATTRIBUTE_UNUSED, unsigned l2_sizekb)
92 char l1size[1000], line[1000], l2size[1000];
94 /* At the moment, gcc middle-end does not use the information about the
95 associativity of the cache. */
97 sprintf (l1size, "--param l1-cache-size=%u", l1_sizekb);
98 sprintf (line, "--param l1-cache-line-size=%u", l1_line);
99 sprintf (l2size, "--param l2-cache-size=%u", l2_sizekb);
101 return concat (l1size, " ", line, " ", l2size, " ", NULL);
104 #ifdef __APPLE__
106 /* Returns the description of caches on Darwin. */
108 static char *
109 detect_caches_darwin (void)
111 unsigned l1_sizekb, l1_line, l1_assoc, l2_sizekb;
112 size_t len = 4;
113 static int l1_size_name[2] = { CTL_HW, HW_L1DCACHESIZE };
114 static int l1_line_name[2] = { CTL_HW, HW_CACHELINE };
115 static int l2_size_name[2] = { CTL_HW, HW_L2CACHESIZE };
117 sysctl (l1_size_name, 2, &l1_sizekb, &len, NULL, 0);
118 sysctl (l1_line_name, 2, &l1_line, &len, NULL, 0);
119 sysctl (l2_size_name, 2, &l2_sizekb, &len, NULL, 0);
120 l1_assoc = 0;
122 return describe_cache (l1_sizekb / 1024, l1_line, l1_assoc,
123 l2_sizekb / 1024);
126 static const char *
127 detect_processor_darwin (void)
129 unsigned int proc;
130 size_t len = 4;
132 sysctlbyname ("hw.cpusubtype", &proc, &len, NULL, 0);
134 if (len > 0)
135 switch (proc)
137 case 1:
138 return "601";
139 case 2:
140 return "602";
141 case 3:
142 return "603";
143 case 4:
144 case 5:
145 return "603e";
146 case 6:
147 return "604";
148 case 7:
149 return "604e";
150 case 8:
151 return "620";
152 case 9:
153 return "750";
154 case 10:
155 return "7400";
156 case 11:
157 return "7450";
158 case 100:
159 return "970";
160 default:
161 return "powerpc";
164 return "powerpc";
167 #endif /* __APPLE__ */
169 #ifdef __FreeBSD__
171 /* Returns the description of caches on FreeBSD PPC. */
173 static char *
174 detect_caches_freebsd (void)
176 unsigned l1_sizekb, l1_line, l1_assoc, l2_sizekb;
177 size_t len = 4;
179 /* Currently, as of FreeBSD-7.0, there is only the cacheline_size
180 available via sysctl. */
181 sysctlbyname ("machdep.cacheline_size", &l1_line, &len, NULL, 0);
183 l1_sizekb = 32;
184 l1_assoc = 0;
185 l2_sizekb = 512;
187 return describe_cache (l1_sizekb, l1_line, l1_assoc, l2_sizekb);
190 /* Currently returns default powerpc. */
191 static const char *
192 detect_processor_freebsd (void)
194 return "powerpc";
197 #endif /* __FreeBSD__ */
199 #ifdef __linux__
201 /* Returns the canonical AT_PLATFORM if present, otherwise NULL. */
203 static const char *
204 elf_platform (void)
206 /* Used to cache the result we determine below. */
207 static const char *cpu = NULL;
209 /* Use the cached AT_PLATFORM cpu name if we've already determined it. */
210 if (cpu != NULL)
211 return cpu;
213 int fd = open ("/proc/self/auxv", O_RDONLY);
215 if (fd != -1)
217 char buf[1024];
218 ElfW(auxv_t) *av;
219 ssize_t n;
221 n = read (fd, buf, sizeof (buf));
222 close (fd);
224 if (n > 0)
226 for (av = (ElfW(auxv_t) *) buf; av->a_type != AT_NULL; ++av)
227 if (av->a_type == AT_PLATFORM)
229 /* Cache the result. */
230 cpu = (const char *) av->a_un.a_val;
231 break;
235 /* Verify that CPU is either a valid -mcpu=<cpu> option name, or is a
236 valid alternative name. If it is a valid alternative name, then use
237 the canonical name. */
238 if (cpu != NULL)
240 size_t i, j;
241 char *s;
243 /* Check if AT_PLATFORM is a GCC canonical cpu name. */
244 for (i = 0; i < ARRAY_SIZE (rs6000_supported_cpu_names); i++)
245 if (!strcmp (cpu, rs6000_supported_cpu_names[i]))
246 return cpu;
248 /* Check if AT_PLATFORM can be translated to a canonical cpu name. */
249 for (i = 0; linux_cpu_translation_table[i][0] != NULL; i++)
251 const char *canonical = linux_cpu_translation_table[i][0];
252 for (j = 1; linux_cpu_translation_table[i][j] != NULL; j++)
253 if (!strcmp (cpu, linux_cpu_translation_table[i][j]))
255 /* Cache the result. */
256 cpu = canonical;
257 return cpu;
261 /* The kernel returned an AT_PLATFORM name we do not support. */
262 auto_vec <const char *> candidates;
263 for (i = 0; i < ARRAY_SIZE (rs6000_supported_cpu_names); i++)
264 candidates.safe_push (rs6000_supported_cpu_names[i]);
265 candidates_list_and_hint (cpu, s, candidates);
266 fatal_error (
267 input_location,
268 "Unsupported cpu name returned from kernel for -mcpu=native: %s\n"
269 "Please use an explicit cpu name. Valid cpu names are: %s",
270 cpu, s);
273 return NULL;
276 /* Returns AT_DCACHEBSIZE if present, otherwise generic 32. */
278 static int
279 elf_dcachebsize (void)
281 int fd;
283 fd = open ("/proc/self/auxv", O_RDONLY);
285 if (fd != -1)
287 char buf[1024];
288 ElfW(auxv_t) *av;
289 ssize_t n;
291 n = read (fd, buf, sizeof (buf));
292 close (fd);
294 if (n > 0)
296 for (av = (ElfW(auxv_t) *) buf; av->a_type != AT_NULL; ++av)
297 switch (av->a_type)
299 case AT_DCACHEBSIZE:
300 return av->a_un.a_val;
302 default:
303 break;
307 return 32;
310 /* Returns the description of caches on Linux. */
312 static char *
313 detect_caches_linux (void)
315 unsigned l1_sizekb, l1_line, l1_assoc, l2_sizekb;
316 const char *platform;
318 platform = elf_platform ();
320 if (platform != NULL)
322 l1_line = 128;
324 if (platform[5] == '6')
325 /* POWER6 and POWER6x */
326 l1_sizekb = 64;
327 else
328 l1_sizekb = 32;
330 else
332 l1_line = elf_dcachebsize ();
333 l1_sizekb = 32;
336 l1_assoc = 0;
337 l2_sizekb = 512;
339 return describe_cache (l1_sizekb, l1_line, l1_assoc, l2_sizekb);
342 static const char *
343 detect_processor_linux (void)
345 const char *platform;
347 platform = elf_platform ();
349 if (platform != NULL)
350 return platform;
351 else
352 return "powerpc";
355 #endif /* __linux__ */
357 #ifdef _AIX
358 /* Returns the description of caches on AIX. */
360 static char *
361 detect_caches_aix (void)
363 unsigned l1_sizekb, l1_line, l1_assoc, l2_sizekb;
365 l1_sizekb = _system_configuration.dcache_size / 1024;
366 l1_line = _system_configuration.dcache_line;
367 l1_assoc = _system_configuration.dcache_asc;
368 l2_sizekb = _system_configuration.L2_cache_size / 1024;
370 return describe_cache (l1_sizekb, l1_line, l1_assoc, l2_sizekb);
374 /* Returns the processor implementation on AIX. */
376 static const char *
377 detect_processor_aix (void)
379 switch (_system_configuration.implementation)
381 case 0x0008:
382 return "601";
384 case 0x0020:
385 return "603";
387 case 0x0010:
388 return "604";
390 case 0x0040:
391 return "620";
393 case 0x0080:
394 return "630";
396 case 0x0100:
397 case 0x0200:
398 case 0x0400:
399 return "rs64";
401 case 0x0800:
402 return "power4";
404 case 0x2000:
405 if (_system_configuration.version == 0x0F0000)
406 return "power5";
407 else
408 return "power5+";
410 case 0x4000:
411 return "power6";
413 case 0x8000:
414 return "power7";
416 case 0x10000:
417 return "power8";
419 case 0x20000:
420 return "power9";
422 default:
423 return "powerpc";
426 #endif /* _AIX */
430 * Array to map -mcpu=native names to the switches passed to the assembler.
431 * This list mirrors the specs in ASM_CPU_SPEC, and any changes made here
432 * should be made there as well.
435 struct asm_name {
436 const char *cpu;
437 const char *asm_sw;
440 static const struct asm_name asm_names[] = {
441 #if defined (_AIX)
442 { "power3", "-m620" },
443 { "power4", "-mpwr4" },
444 { "power5", "-mpwr5" },
445 { "power5+", "-mpwr5x" },
446 { "power6", "-mpwr6" },
447 { "power6x", "-mpwr6" },
448 { "power7", "-mpwr7" },
449 { "power8", "-mpwr8" },
450 { "power9", "-mpwr9" },
451 { "powerpc", "-mppc" },
452 { "rs64a", "-mppc" },
453 { "603", "-m603" },
454 { "603e", "-m603" },
455 { "604", "-m604" },
456 { "604e", "-m604" },
457 { "620", "-m620" },
458 { "630", "-m620" },
459 { "970", "-m970" },
460 { "G5", "-m970" },
461 { NULL, "\
462 %{!maix64: \
463 %{mpowerpc64: -mppc64} \
464 %{maltivec: -m970} \
465 %{!maltivec: %{!mpowerpc64: %(asm_default)}}}" },
467 #else
468 { "cell", "-mcell" },
469 { "power3", "-mppc64" },
470 { "power4", "-mpower4" },
471 { "power5", "%(asm_cpu_power5)" },
472 { "power5+", "%(asm_cpu_power5)" },
473 { "power6", "%(asm_cpu_power6) -maltivec" },
474 { "power6x", "%(asm_cpu_power6) -maltivec" },
475 { "power7", "%(asm_cpu_power7)" },
476 { "power8", "%(asm_cpu_power8)" },
477 { "power9", "%(asm_cpu_power9)" },
478 { "powerpc", "-mppc" },
479 { "rs64a", "-mppc64" },
480 { "401", "-mppc" },
481 { "403", "-m403" },
482 { "405", "-m405" },
483 { "405fp", "-m405" },
484 { "440", "-m440" },
485 { "440fp", "-m440" },
486 { "464", "-m440" },
487 { "464fp", "-m440" },
488 { "505", "-mppc" },
489 { "601", "-m601" },
490 { "602", "-mppc" },
491 { "603", "-mppc" },
492 { "603e", "-mppc" },
493 { "ec603e", "-mppc" },
494 { "604", "-mppc" },
495 { "604e", "-mppc" },
496 { "620", "-mppc64" },
497 { "630", "-mppc64" },
498 { "740", "-mppc" },
499 { "750", "-mppc" },
500 { "G3", "-mppc" },
501 { "7400", "-mppc -maltivec" },
502 { "7450", "-mppc -maltivec" },
503 { "G4", "-mppc -maltivec" },
504 { "801", "-mppc" },
505 { "821", "-mppc" },
506 { "823", "-mppc" },
507 { "860", "-mppc" },
508 { "970", "-mpower4 -maltivec" },
509 { "G5", "-mpower4 -maltivec" },
510 { "8540", "-me500" },
511 { "8548", "-me500" },
512 { "e300c2", "-me300" },
513 { "e300c3", "-me300" },
514 { "e500mc", "-me500mc" },
515 { NULL, "\
516 %{mpowerpc64*: -mppc64} \
517 %{!mpowerpc64*: %(asm_default)}" },
518 #endif
521 /* This will be called by the spec parser in gcc.c when it sees
522 a %:local_cpu_detect(args) construct. Currently it will be called
523 with either "arch" or "tune" as argument depending on if -march=native
524 or -mtune=native is to be substituted.
526 Additionally it will be called with "asm" to select the appropriate flags
527 for the assembler.
529 It returns a string containing new command line parameters to be
530 put at the place of the above two options, depending on what CPU
531 this is executed.
533 ARGC and ARGV are set depending on the actual arguments given
534 in the spec. */
535 const char *
536 host_detect_local_cpu (int argc, const char **argv)
538 const char *cpu = NULL;
539 const char *cache = "";
540 const char *options = "";
541 bool arch;
542 bool assembler;
543 size_t i;
545 if (argc < 1)
546 return NULL;
548 arch = strcmp (argv[0], "cpu") == 0;
549 assembler = (!arch && strcmp (argv[0], "asm") == 0);
550 if (!arch && !assembler && strcmp (argv[0], "tune"))
551 return NULL;
553 if (! assembler)
555 #if defined (_AIX)
556 cache = detect_caches_aix ();
557 #elif defined (__APPLE__)
558 cache = detect_caches_darwin ();
559 #elif defined (__FreeBSD__)
560 cache = detect_caches_freebsd ();
561 /* FreeBSD PPC does not provide any cache information yet. */
562 cache = "";
563 #elif defined (__linux__)
564 cache = detect_caches_linux ();
565 /* PPC Linux does not provide any cache information yet. */
566 cache = "";
567 #else
568 cache = "";
569 #endif
572 #if defined (_AIX)
573 cpu = detect_processor_aix ();
574 #elif defined (__APPLE__)
575 cpu = detect_processor_darwin ();
576 #elif defined (__FreeBSD__)
577 cpu = detect_processor_freebsd ();
578 #elif defined (__linux__)
579 cpu = detect_processor_linux ();
580 #else
581 cpu = "powerpc";
582 #endif
584 if (assembler)
586 for (i = 0; i < sizeof (asm_names) / sizeof (asm_names[0]); i++)
588 if (!asm_names[i].cpu || !strcmp (asm_names[i].cpu, cpu))
589 return asm_names[i].asm_sw;
592 return NULL;
595 return concat (cache, "-m", argv[0], "=", cpu, " ", options, NULL);
598 #else /* GCC_VERSION */
600 /* If we aren't compiling with GCC we just provide a minimal
601 default value. */
602 const char *
603 host_detect_local_cpu (int argc, const char **argv)
605 const char *cpu;
606 bool arch;
608 if (argc < 1)
609 return NULL;
611 arch = strcmp (argv[0], "cpu") == 0;
612 if (!arch && strcmp (argv[0], "tune"))
613 return NULL;
615 if (arch)
616 cpu = "powerpc";
618 return concat ("-m", argv[0], "=", cpu, NULL);
621 #endif /* GCC_VERSION */