| 1 | /* Common hooks for AArch64. |
| 2 | Copyright (C) 2012-2025 Free Software Foundation, Inc. |
| 3 | Contributed by ARM Ltd. |
| 4 | |
| 5 | This file is part of GCC. |
| 6 | |
| 7 | GCC is free software; you can redistribute it and/or modify it |
| 8 | under the terms of the GNU General Public License as published |
| 9 | by the Free Software Foundation; either version 3, or (at your |
| 10 | option) any later version. |
| 11 | |
| 12 | GCC is distributed in the hope that it will be useful, but WITHOUT |
| 13 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY |
| 14 | or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public |
| 15 | License for more details. |
| 16 | |
| 17 | You should have received a copy of the GNU General Public License |
| 18 | along with GCC; see the file COPYING3. If not see |
| 19 | <http://www.gnu.org/licenses/>. */ |
| 20 | |
| 21 | #include "config.h" |
| 22 | #define INCLUDE_STRING |
| 23 | #include "system.h" |
| 24 | #include "coretypes.h" |
| 25 | #include "tm.h" |
| 26 | #include "memmodel.h" |
| 27 | #include "tm_p.h" |
| 28 | #include "common/common-target.h" |
| 29 | #include "common/common-target-def.h" |
| 30 | #include "opts.h" |
| 31 | #include "flags.h" |
| 32 | #include "diagnostic.h" |
| 33 | #include "config/aarch64/aarch64-feature-deps.h" |
| 34 | #include "config/arm/aarch-common.h" |
| 35 | |
| 36 | #ifdef TARGET_BIG_ENDIAN_DEFAULT |
| 37 | #undef TARGET_DEFAULT_TARGET_FLAGS |
| 38 | #define TARGET_DEFAULT_TARGET_FLAGS (MASK_BIG_END) |
| 39 | #endif |
| 40 | |
| 41 | #undef TARGET_HANDLE_OPTION |
| 42 | #define TARGET_HANDLE_OPTION aarch64_handle_option |
| 43 | |
| 44 | #undef TARGET_OPTION_OPTIMIZATION_TABLE |
| 45 | #define TARGET_OPTION_OPTIMIZATION_TABLE aarch_option_optimization_table |
| 46 | |
| 47 | /* Set default optimization options. */ |
| 48 | static const struct default_options aarch_option_optimization_table[] = |
| 49 | { |
| 50 | /* Enable section anchors by default at -O1 or higher. */ |
| 51 | { OPT_LEVELS_1_PLUS, OPT_fsection_anchors, NULL, 1 }, |
| 52 | /* Disable fomit-frame-pointer by default. */ |
| 53 | { OPT_LEVELS_ALL, OPT_fomit_frame_pointer, NULL, 0 }, |
| 54 | /* Enable -fsched-pressure by default when optimizing. */ |
| 55 | { OPT_LEVELS_1_PLUS, OPT_fsched_pressure, NULL, 1 }, |
| 56 | /* Except for -O3 and higher, disable early scheduling due to high |
| 57 | compile-time overheads. */ |
| 58 | { OPT_LEVELS_ALL, OPT_fschedule_insns, NULL, 0 }, |
| 59 | { OPT_LEVELS_3_PLUS, OPT_fschedule_insns, NULL, 1 }, |
| 60 | /* Enable redundant extension instructions removal at -O2 and higher. */ |
| 61 | { OPT_LEVELS_2_PLUS, OPT_free, NULL, 1 }, |
| 62 | { OPT_LEVELS_2_PLUS, OPT_mearly_ra_, NULL, AARCH64_EARLY_RA_ALL }, |
| 63 | #if (TARGET_DEFAULT_ASYNC_UNWIND_TABLES == 1) |
| 64 | { OPT_LEVELS_ALL, OPT_fasynchronous_unwind_tables, NULL, 1 }, |
| 65 | { OPT_LEVELS_ALL, OPT_funwind_tables, NULL, 1}, |
| 66 | #endif |
| 67 | { OPT_LEVELS_ALL, OPT__param_stack_clash_protection_guard_size_, NULL, |
| 68 | DEFAULT_STK_CLASH_GUARD_SIZE == 0 ? 16 : DEFAULT_STK_CLASH_GUARD_SIZE }, |
| 69 | |
| 70 | { OPT_LEVELS_NONE, 0, NULL, 0 } |
| 71 | }; |
| 72 | |
| 73 | |
| 74 | /* Set OPTS->x_aarch64_asm_isa_flags_<0..n> to FLAGS and update |
| 75 | OPTS->x_aarch64_isa_flags_<0..n> accordingly. */ |
| 76 | void |
| 77 | aarch64_set_asm_isa_flags (gcc_options *opts, aarch64_feature_flags flags) |
| 78 | { |
| 79 | opts->x_aarch64_asm_isa_flags_0 = flags.val[0]; |
| 80 | opts->x_aarch64_asm_isa_flags_1 = flags.val[1]; |
| 81 | |
| 82 | if (opts->x_target_flags & MASK_GENERAL_REGS_ONLY) |
| 83 | flags &= ~feature_deps::get_flags_off (AARCH64_FL_FP); |
| 84 | |
| 85 | opts->x_aarch64_isa_flags_0 = flags.val[0]; |
| 86 | opts->x_aarch64_isa_flags_1 = flags.val[1]; |
| 87 | } |
| 88 | |
| 89 | /* Implement TARGET_HANDLE_OPTION. |
| 90 | This function handles the target specific options for CPU/target selection. |
| 91 | |
| 92 | -mcpu=CPU is shorthand for -march=ARCH_FOR_CPU, -mtune=CPU. |
| 93 | If either of -march or -mtune is given, they override their |
| 94 | respective component of -mcpu. This logic is implemented |
| 95 | in config/aarch64/aarch64.cc:aarch64_override_options. */ |
| 96 | |
| 97 | bool |
| 98 | aarch64_handle_option (struct gcc_options *opts, |
| 99 | struct gcc_options *opts_set ATTRIBUTE_UNUSED, |
| 100 | const struct cl_decoded_option *decoded, |
| 101 | location_t loc ATTRIBUTE_UNUSED) |
| 102 | { |
| 103 | size_t code = decoded->opt_index; |
| 104 | const char *arg = decoded->arg; |
| 105 | int val = decoded->value; |
| 106 | |
| 107 | switch (code) |
| 108 | { |
| 109 | case OPT_march_: |
| 110 | opts->x_aarch64_arch_string = arg; |
| 111 | return true; |
| 112 | |
| 113 | case OPT_mcpu_: |
| 114 | opts->x_aarch64_cpu_string = arg; |
| 115 | return true; |
| 116 | |
| 117 | case OPT_mtune_: |
| 118 | opts->x_aarch64_tune_string = arg; |
| 119 | return true; |
| 120 | |
| 121 | case OPT_mgeneral_regs_only: |
| 122 | opts->x_target_flags |= MASK_GENERAL_REGS_ONLY; |
| 123 | aarch64_set_asm_isa_flags (opts, aarch64_get_asm_isa_flags (opts)); |
| 124 | return true; |
| 125 | |
| 126 | case OPT_mfix_cortex_a53_835769: |
| 127 | opts->x_aarch64_fix_a53_err835769 = val; |
| 128 | return true; |
| 129 | |
| 130 | case OPT_mstrict_align: |
| 131 | if (val) |
| 132 | opts->x_target_flags |= MASK_STRICT_ALIGN; |
| 133 | else |
| 134 | opts->x_target_flags &= ~MASK_STRICT_ALIGN; |
| 135 | return true; |
| 136 | |
| 137 | case OPT_momit_leaf_frame_pointer: |
| 138 | opts->x_flag_omit_leaf_frame_pointer = val; |
| 139 | return true; |
| 140 | |
| 141 | case OPT_moutline_atomics: |
| 142 | opts->x_aarch64_flag_outline_atomics = val; |
| 143 | return true; |
| 144 | |
| 145 | case OPT_msimd_memops: |
| 146 | opts->x_aarch64_flag_simd_memops = val; |
| 147 | return true; |
| 148 | |
| 149 | default: |
| 150 | return true; |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | |
| 155 | /* An ISA extension in the co-processor and main instruction set space. */ |
| 156 | struct aarch64_extension_info |
| 157 | { |
| 158 | /* The extension name to pass on to the assembler. */ |
| 159 | const char *name; |
| 160 | /* The smallest set of feature bits to toggle to enable this option. */ |
| 161 | aarch64_feature_flags flag_canonical; |
| 162 | /* If this feature is turned on, these bits also need to be turned on. */ |
| 163 | aarch64_feature_flags flags_on; |
| 164 | /* If this feature is turned off, these bits also need to be turned off. */ |
| 165 | aarch64_feature_flags flags_off; |
| 166 | /* If this feature remains enabled, these bits must also remain enabled. */ |
| 167 | aarch64_feature_flags flags_required; |
| 168 | }; |
| 169 | |
| 170 | /* ISA extensions in AArch64. */ |
| 171 | static constexpr aarch64_extension_info all_extensions[] = |
| 172 | { |
| 173 | #define AARCH64_OPT_EXTENSION(NAME, IDENT, C, D, E, FEATURE_STRING) \ |
| 174 | {NAME, AARCH64_FL_##IDENT, feature_deps::IDENT ().explicit_on, \ |
| 175 | feature_deps::get_flags_off (feature_deps::root_off_##IDENT), \ |
| 176 | feature_deps::IDENT ().enable}, |
| 177 | #include "config/aarch64/aarch64-option-extensions.def" |
| 178 | {NULL, 0, 0, 0, 0} |
| 179 | }; |
| 180 | |
| 181 | struct aarch64_arch_info |
| 182 | { |
| 183 | const char *name; |
| 184 | aarch64_arch arch; |
| 185 | aarch64_feature_flags flags; |
| 186 | }; |
| 187 | |
| 188 | /* Map architecture revisions to their string representation. */ |
| 189 | static constexpr aarch64_arch_info all_architectures[] = |
| 190 | { |
| 191 | #define AARCH64_ARCH(NAME, B, ARCH_IDENT, D, E) \ |
| 192 | {NAME, AARCH64_ARCH_##ARCH_IDENT, feature_deps::ARCH_IDENT ().enable}, |
| 193 | #include "config/aarch64/aarch64-arches.def" |
| 194 | {NULL, aarch64_no_arch, 0} |
| 195 | }; |
| 196 | |
| 197 | struct aarch64_processor_info |
| 198 | { |
| 199 | const char *name; |
| 200 | aarch64_cpu processor; |
| 201 | aarch64_arch arch; |
| 202 | aarch64_feature_flags flags; |
| 203 | }; |
| 204 | |
| 205 | /* Map processor names to the architecture revision they implement and |
| 206 | the default set of architectural feature flags they support. */ |
| 207 | static constexpr aarch64_processor_info all_cores[] = |
| 208 | { |
| 209 | #define AARCH64_CORE(NAME, CORE_IDENT, C, ARCH_IDENT, E, F, G, H, I) \ |
| 210 | {NAME, AARCH64_CPU_##CORE_IDENT, AARCH64_ARCH_##ARCH_IDENT, \ |
| 211 | feature_deps::cpu_##CORE_IDENT}, |
| 212 | #include "config/aarch64/aarch64-cores.def" |
| 213 | {NULL, aarch64_no_cpu, aarch64_no_arch, 0} |
| 214 | }; |
| 215 | |
| 216 | /* Return the set of feature flags that are required to be enabled when the |
| 217 | features in FLAGS are enabled. */ |
| 218 | |
| 219 | aarch64_feature_flags |
| 220 | aarch64_get_required_features (aarch64_feature_flags flags) |
| 221 | { |
| 222 | const struct aarch64_extension_info *opt; |
| 223 | for (opt = all_extensions; opt->name != NULL; opt++) |
| 224 | if (flags & opt->flag_canonical) |
| 225 | flags |= opt->flags_required; |
| 226 | return flags; |
| 227 | } |
| 228 | |
| 229 | /* Print a list of CANDIDATES for an argument, and try to suggest a specific |
| 230 | close match. */ |
| 231 | |
| 232 | inline static void |
| 233 | aarch64_print_hint_candidates (const char *str, |
| 234 | const auto_vec<const char*> & candidates) |
| 235 | { |
| 236 | char *s; |
| 237 | const char *hint = candidates_list_and_hint (str, s, candidates); |
| 238 | if (hint) |
| 239 | inform (input_location, "valid arguments are: %s;" |
| 240 | " did you mean %qs?", s, hint); |
| 241 | else |
| 242 | inform (input_location, "valid arguments are: %s", s); |
| 243 | |
| 244 | XDELETEVEC (s); |
| 245 | } |
| 246 | |
| 247 | /* Print a hint with a suggestion for an extension name |
| 248 | that most closely resembles what the user passed in STR. */ |
| 249 | |
| 250 | void |
| 251 | aarch64_print_hint_for_extensions (const char *str) |
| 252 | { |
| 253 | auto_vec<const char *> candidates; |
| 254 | const struct aarch64_extension_info *opt; |
| 255 | for (opt = all_extensions; opt->name != NULL; opt++) |
| 256 | candidates.safe_push (opt->name); |
| 257 | |
| 258 | aarch64_print_hint_candidates (str, candidates); |
| 259 | } |
| 260 | |
| 261 | /* Print a hint with a suggestion for an architecture name that most closely |
| 262 | resembles what the user passed in STR. */ |
| 263 | |
| 264 | void |
| 265 | aarch64_print_hint_for_arch (const char *str) |
| 266 | { |
| 267 | auto_vec<const char *> candidates; |
| 268 | const struct aarch64_arch_info *entry = all_architectures; |
| 269 | for (; entry->name != NULL; entry++) |
| 270 | candidates.safe_push (entry->name); |
| 271 | |
| 272 | #ifdef HAVE_LOCAL_CPU_DETECT |
| 273 | /* Add also "native" as possible value. */ |
| 274 | candidates.safe_push ("native"); |
| 275 | #endif |
| 276 | |
| 277 | aarch64_print_hint_candidates (str, candidates); |
| 278 | } |
| 279 | |
| 280 | /* Print a hint with a suggestion for a core name that most closely resembles |
| 281 | what the user passed in STR. */ |
| 282 | |
| 283 | void |
| 284 | aarch64_print_hint_for_core (const char *str) |
| 285 | { |
| 286 | auto_vec<const char *> candidates; |
| 287 | const struct aarch64_processor_info *entry = all_cores; |
| 288 | for (; entry->name != NULL; entry++) |
| 289 | candidates.safe_push (entry->name); |
| 290 | aarch64_print_hint_candidates (str, candidates); |
| 291 | } |
| 292 | |
| 293 | |
| 294 | /* Parse the architecture extension string STR and update ISA_FLAGS |
| 295 | with the architecture features turned on or off. Return a |
| 296 | aarch_parse_opt_result describing the result. |
| 297 | When the STR string contains an invalid extension, |
| 298 | a copy of the string is created and stored to INVALID_EXTENSION. */ |
| 299 | |
| 300 | enum aarch_parse_opt_result |
| 301 | aarch64_parse_extension (const char *str, aarch64_feature_flags *isa_flags, |
| 302 | std::string *invalid_extension) |
| 303 | { |
| 304 | /* The extension string is parsed left to right. */ |
| 305 | const struct aarch64_extension_info *opt = NULL; |
| 306 | |
| 307 | /* Flag to say whether we are adding or removing an extension. */ |
| 308 | int adding_ext = -1; |
| 309 | |
| 310 | while (str != NULL && *str != 0) |
| 311 | { |
| 312 | const char *ext; |
| 313 | size_t len; |
| 314 | |
| 315 | str++; |
| 316 | ext = strchr (str, '+'); |
| 317 | |
| 318 | if (ext != NULL) |
| 319 | len = ext - str; |
| 320 | else |
| 321 | len = strlen (str); |
| 322 | |
| 323 | if (len >= 2 && startswith (str, "no")) |
| 324 | { |
| 325 | adding_ext = 0; |
| 326 | len -= 2; |
| 327 | str += 2; |
| 328 | } |
| 329 | else if (len > 0) |
| 330 | adding_ext = 1; |
| 331 | |
| 332 | if (len == 0) |
| 333 | return AARCH_PARSE_MISSING_ARG; |
| 334 | |
| 335 | |
| 336 | /* Scan over the extensions table trying to find an exact match. */ |
| 337 | for (opt = all_extensions; opt->name != NULL; opt++) |
| 338 | { |
| 339 | if (strlen (opt->name) == len && strncmp (opt->name, str, len) == 0) |
| 340 | { |
| 341 | /* Add or remove the extension. */ |
| 342 | if (adding_ext) |
| 343 | *isa_flags |= opt->flags_on; |
| 344 | else |
| 345 | *isa_flags &= ~opt->flags_off; |
| 346 | break; |
| 347 | } |
| 348 | } |
| 349 | |
| 350 | if (opt->name == NULL) |
| 351 | { |
| 352 | /* Extension not found in list. */ |
| 353 | if (invalid_extension) |
| 354 | *invalid_extension = std::string (str, len); |
| 355 | return AARCH_PARSE_INVALID_FEATURE; |
| 356 | } |
| 357 | |
| 358 | str = ext; |
| 359 | }; |
| 360 | |
| 361 | return AARCH_PARSE_OK; |
| 362 | } |
| 363 | |
| 364 | /* Parse the TO_PARSE string and put the architecture that it |
| 365 | selects into RES_ARCH and the architectural features into RES_FLAGS. |
| 366 | Return an aarch_parse_opt_result describing the parse result. |
| 367 | If there is an error parsing, RES_ARCH and RES_FLAGS are left unchanged. |
| 368 | When the TO_PARSE string contains an invalid extension, |
| 369 | a copy of the string is created and stored to INVALID_EXTENSION. */ |
| 370 | |
| 371 | enum aarch_parse_opt_result |
| 372 | aarch64_parse_arch (const char *to_parse, aarch64_arch *res_arch, |
| 373 | aarch64_feature_flags *res_flags, |
| 374 | std::string *invalid_extension) |
| 375 | { |
| 376 | const char *ext; |
| 377 | const struct aarch64_arch_info *arch; |
| 378 | size_t len; |
| 379 | |
| 380 | ext = strchr (to_parse, '+'); |
| 381 | |
| 382 | if (ext != NULL) |
| 383 | len = ext - to_parse; |
| 384 | else |
| 385 | len = strlen (to_parse); |
| 386 | |
| 387 | if (len == 0) |
| 388 | return AARCH_PARSE_MISSING_ARG; |
| 389 | |
| 390 | |
| 391 | /* Loop through the list of supported ARCHes to find a match. */ |
| 392 | for (arch = all_architectures; arch->name != NULL; arch++) |
| 393 | { |
| 394 | if (strlen (arch->name) == len |
| 395 | && strncmp (arch->name, to_parse, len) == 0) |
| 396 | { |
| 397 | auto isa_flags = arch->flags; |
| 398 | |
| 399 | if (ext != NULL) |
| 400 | { |
| 401 | /* TO_PARSE string contains at least one extension. */ |
| 402 | enum aarch_parse_opt_result ext_res |
| 403 | = aarch64_parse_extension (ext, &isa_flags, invalid_extension); |
| 404 | |
| 405 | if (ext_res != AARCH_PARSE_OK) |
| 406 | return ext_res; |
| 407 | } |
| 408 | /* Extension parsing was successful. Confirm the result |
| 409 | arch and ISA flags. */ |
| 410 | *res_arch = arch->arch; |
| 411 | *res_flags = isa_flags; |
| 412 | return AARCH_PARSE_OK; |
| 413 | } |
| 414 | } |
| 415 | |
| 416 | /* ARCH name not found in list. */ |
| 417 | return AARCH_PARSE_INVALID_ARG; |
| 418 | } |
| 419 | |
| 420 | /* Parse the TO_PARSE string and put the result tuning in RES_CPU and the |
| 421 | architecture flags in RES_FLAGS. Return an aarch_parse_opt_result |
| 422 | describing the parse result. If there is an error parsing, RES_CPU and |
| 423 | RES_FLAGS are left unchanged. |
| 424 | When the TO_PARSE string contains an invalid extension, |
| 425 | a copy of the string is created and stored to INVALID_EXTENSION. */ |
| 426 | |
| 427 | enum aarch_parse_opt_result |
| 428 | aarch64_parse_cpu (const char *to_parse, aarch64_cpu *res_cpu, |
| 429 | aarch64_feature_flags *res_flags, |
| 430 | std::string *invalid_extension) |
| 431 | { |
| 432 | const char *ext; |
| 433 | const struct aarch64_processor_info *cpu; |
| 434 | size_t len; |
| 435 | |
| 436 | ext = strchr (to_parse, '+'); |
| 437 | |
| 438 | if (ext != NULL) |
| 439 | len = ext - to_parse; |
| 440 | else |
| 441 | len = strlen (to_parse); |
| 442 | |
| 443 | if (len == 0) |
| 444 | return AARCH_PARSE_MISSING_ARG; |
| 445 | |
| 446 | |
| 447 | /* Loop through the list of supported CPUs to find a match. */ |
| 448 | for (cpu = all_cores; cpu->name != NULL; cpu++) |
| 449 | { |
| 450 | if (strlen (cpu->name) == len && strncmp (cpu->name, to_parse, len) == 0) |
| 451 | { |
| 452 | auto isa_flags = cpu->flags; |
| 453 | |
| 454 | if (ext != NULL) |
| 455 | { |
| 456 | /* TO_PARSE string contains at least one extension. */ |
| 457 | enum aarch_parse_opt_result ext_res |
| 458 | = aarch64_parse_extension (ext, &isa_flags, invalid_extension); |
| 459 | |
| 460 | if (ext_res != AARCH_PARSE_OK) |
| 461 | return ext_res; |
| 462 | } |
| 463 | /* Extension parsing was successfull. Confirm the result |
| 464 | cpu and ISA flags. */ |
| 465 | *res_cpu = cpu->processor; |
| 466 | *res_flags = isa_flags; |
| 467 | return AARCH_PARSE_OK; |
| 468 | } |
| 469 | } |
| 470 | |
| 471 | /* CPU name not found in list. */ |
| 472 | return AARCH_PARSE_INVALID_ARG; |
| 473 | } |
| 474 | |
| 475 | /* Parse the TO_PARSE string and put the cpu it selects into RES_CPU. |
| 476 | Return an aarch_parse_opt_result describing the parse result. |
| 477 | If the parsing fails then RES_CPU does not change. */ |
| 478 | |
| 479 | enum aarch_parse_opt_result |
| 480 | aarch64_parse_tune (const char *to_parse, aarch64_cpu *res_cpu) |
| 481 | { |
| 482 | const struct aarch64_processor_info *cpu; |
| 483 | |
| 484 | /* Loop through the list of supported CPUs to find a match. */ |
| 485 | for (cpu = all_cores; cpu->name != NULL; cpu++) |
| 486 | { |
| 487 | if (strcmp (cpu->name, to_parse) == 0) |
| 488 | { |
| 489 | *res_cpu = cpu->processor; |
| 490 | return AARCH_PARSE_OK; |
| 491 | } |
| 492 | } |
| 493 | |
| 494 | /* CPU name not found in list. */ |
| 495 | return AARCH_PARSE_INVALID_ARG; |
| 496 | } |
| 497 | |
| 498 | |
| 499 | /* Validate a command-line -march option. Parse the arch and extensions |
| 500 | (if any) specified in STR and throw errors if appropriate. Put the |
| 501 | results, if they are valid, in RES_ARCH and RES_FLAGS. Return whether the |
| 502 | option is valid. */ |
| 503 | |
| 504 | bool |
| 505 | aarch64_validate_march (const char *str, aarch64_arch *res_arch, |
| 506 | aarch64_feature_flags *res_flags) |
| 507 | { |
| 508 | std::string invalid_extension; |
| 509 | enum aarch_parse_opt_result parse_res |
| 510 | = aarch64_parse_arch (str, res_arch, res_flags, &invalid_extension); |
| 511 | |
| 512 | if (parse_res == AARCH_PARSE_OK) |
| 513 | return true; |
| 514 | |
| 515 | switch (parse_res) |
| 516 | { |
| 517 | case AARCH_PARSE_MISSING_ARG: |
| 518 | error ("missing arch name in %<-march=%s%>", str); |
| 519 | break; |
| 520 | case AARCH_PARSE_INVALID_ARG: |
| 521 | { |
| 522 | error ("unknown value %qs for %<-march%>", str); |
| 523 | aarch64_print_hint_for_arch (str); |
| 524 | /* A common user error is confusing -march and -mcpu. |
| 525 | If the -march string matches a known CPU suggest -mcpu. */ |
| 526 | aarch64_cpu temp_cpu; |
| 527 | aarch64_feature_flags temp_flags; |
| 528 | parse_res = aarch64_parse_cpu (str, &temp_cpu, &temp_flags, |
| 529 | &invalid_extension); |
| 530 | if (parse_res == AARCH_PARSE_OK) |
| 531 | inform (input_location, "did you mean %<-mcpu=%s%>?", str); |
| 532 | break; |
| 533 | } |
| 534 | case AARCH_PARSE_INVALID_FEATURE: |
| 535 | error ("invalid feature modifier %qs in %<-march=%s%>", |
| 536 | invalid_extension.c_str (), str); |
| 537 | aarch64_print_hint_for_extensions (invalid_extension.c_str ()); |
| 538 | break; |
| 539 | default: |
| 540 | gcc_unreachable (); |
| 541 | } |
| 542 | |
| 543 | return false; |
| 544 | } |
| 545 | |
| 546 | /* Validate a command-line -mcpu option. Parse the cpu and extensions (if any) |
| 547 | specified in STR and throw errors if appropriate. Put the results if |
| 548 | they are valid in RES_CPU and RES_FLAGS. Return whether the option is |
| 549 | valid. */ |
| 550 | |
| 551 | bool |
| 552 | aarch64_validate_mcpu (const char *str, aarch64_cpu *res_cpu, |
| 553 | aarch64_feature_flags *res_flags) |
| 554 | { |
| 555 | std::string invalid_extension; |
| 556 | enum aarch_parse_opt_result parse_res |
| 557 | = aarch64_parse_cpu (str, res_cpu, res_flags, &invalid_extension); |
| 558 | |
| 559 | if (parse_res == AARCH_PARSE_OK) |
| 560 | return true; |
| 561 | |
| 562 | switch (parse_res) |
| 563 | { |
| 564 | case AARCH_PARSE_MISSING_ARG: |
| 565 | error ("missing cpu name in %<-mcpu=%s%>", str); |
| 566 | break; |
| 567 | case AARCH_PARSE_INVALID_ARG: |
| 568 | { |
| 569 | error ("unknown value %qs for %<-mcpu%>", str); |
| 570 | aarch64_print_hint_for_core (str); |
| 571 | /* A common user error is confusing -march and -mcpu. |
| 572 | If the -mcpu string matches a known architecture then suggest |
| 573 | -march=. */ |
| 574 | aarch64_arch temp_arch; |
| 575 | aarch64_feature_flags temp_flags; |
| 576 | parse_res = aarch64_parse_arch (str, &temp_arch, &temp_flags, |
| 577 | &invalid_extension); |
| 578 | if (parse_res == AARCH_PARSE_OK) |
| 579 | inform (input_location, "did you mean %<-march=%s%>?", str); |
| 580 | break; |
| 581 | } |
| 582 | case AARCH_PARSE_INVALID_FEATURE: |
| 583 | error ("invalid feature modifier %qs in %<-mcpu=%s%>", |
| 584 | invalid_extension.c_str (), str); |
| 585 | aarch64_print_hint_for_extensions (invalid_extension.c_str ()); |
| 586 | break; |
| 587 | default: |
| 588 | gcc_unreachable (); |
| 589 | } |
| 590 | |
| 591 | return false; |
| 592 | } |
| 593 | |
| 594 | /* Validate a command-line -mtune option. Parse the cpu |
| 595 | specified in STR and throw errors if appropriate. Put the |
| 596 | result, if it is valid, in RES_CPU. Return whether the option is |
| 597 | valid. */ |
| 598 | |
| 599 | bool |
| 600 | aarch64_validate_mtune (const char *str, aarch64_cpu *res_cpu) |
| 601 | { |
| 602 | enum aarch_parse_opt_result parse_res |
| 603 | = aarch64_parse_tune (str, res_cpu); |
| 604 | |
| 605 | if (parse_res == AARCH_PARSE_OK) |
| 606 | return true; |
| 607 | |
| 608 | switch (parse_res) |
| 609 | { |
| 610 | case AARCH_PARSE_MISSING_ARG: |
| 611 | error ("missing cpu name in %<-mtune=%s%>", str); |
| 612 | break; |
| 613 | case AARCH_PARSE_INVALID_ARG: |
| 614 | error ("unknown value %qs for %<-mtune%>", str); |
| 615 | aarch64_print_hint_for_core (str); |
| 616 | break; |
| 617 | default: |
| 618 | gcc_unreachable (); |
| 619 | } |
| 620 | return false; |
| 621 | } |
| 622 | |
| 623 | |
| 624 | /* Return a string representation of ISA_FLAGS. DEFAULT_ARCH_FLAGS |
| 625 | gives the default set of flags which are implied by whatever -march |
| 626 | we'd put out. Our job is to figure out the minimal set of "+" and |
| 627 | "+no" feature flags to put out, and to put them out grouped such |
| 628 | that all the "+" flags come before the "+no" flags. */ |
| 629 | |
| 630 | std::string |
| 631 | aarch64_get_extension_string_for_isa_flags |
| 632 | (aarch64_feature_flags isa_flags, |
| 633 | aarch64_feature_flags default_arch_flags) |
| 634 | { |
| 635 | std::string outstr = ""; |
| 636 | |
| 637 | /* The CRYPTO bit should only be used to support the +crypto alias |
| 638 | during option processing, and should be cleared at all other times. |
| 639 | Verify this property for the supplied flags bitmask. */ |
| 640 | gcc_assert (!(AARCH64_FL_CRYPTO & aarch64_isa_flags)); |
| 641 | aarch64_feature_flags current_flags = default_arch_flags; |
| 642 | |
| 643 | /* As a special case, do not assume that the assembler will enable CRC |
| 644 | even if it is the default for the architecture. This is required |
| 645 | because some CPUs had an incorrect specification in older assemblers: |
| 646 | even though CRC should be the default for these cases the -mcpu |
| 647 | values would not turn it on. |
| 648 | |
| 649 | However, assemblers with Armv8-R AArch64 support should not have this |
| 650 | issue, so we don't need this fix when targeting Armv8-R. */ |
| 651 | auto explicit_flags = (!(current_flags & AARCH64_FL_V8R) |
| 652 | ? AARCH64_FL_CRC : 0); |
| 653 | |
| 654 | /* Add the features in isa_flags & ~current_flags using the smallest |
| 655 | possible number of extensions. We can do this by iterating over the |
| 656 | array in reverse order, since the array is sorted topologically. |
| 657 | But in order to make the output more readable, it seems better |
| 658 | to add the strings in definition order. */ |
| 659 | aarch64_feature_flags added = 0; |
| 660 | auto flags_crypto = AARCH64_FL_AES | AARCH64_FL_SHA2; |
| 661 | for (unsigned int i = ARRAY_SIZE (all_extensions); i-- > 0; ) |
| 662 | { |
| 663 | auto &opt = all_extensions[i]; |
| 664 | |
| 665 | /* As a special case, emit +crypto rather than +aes+sha2, |
| 666 | in order to support assemblers that predate the separate |
| 667 | per-feature crypto flags. */ |
| 668 | auto flags = opt.flag_canonical; |
| 669 | if (flags == AARCH64_FL_CRYPTO) |
| 670 | flags = flags_crypto; |
| 671 | |
| 672 | if ((flags & isa_flags & (explicit_flags | ~current_flags)) == flags) |
| 673 | { |
| 674 | current_flags |= opt.flags_on; |
| 675 | added |= opt.flag_canonical; |
| 676 | } |
| 677 | } |
| 678 | for (auto &opt : all_extensions) |
| 679 | if (added & opt.flag_canonical) |
| 680 | { |
| 681 | outstr += "+"; |
| 682 | outstr += opt.name; |
| 683 | } |
| 684 | |
| 685 | /* Remove the features in current_flags & ~isa_flags. If the feature does |
| 686 | not have an HWCAPs then it shouldn't be taken into account for feature |
| 687 | detection because one way or another we can't tell if it's available |
| 688 | or not. */ |
| 689 | |
| 690 | for (auto &opt : all_extensions) |
| 691 | { |
| 692 | auto flags = opt.flag_canonical; |
| 693 | /* As a special case, don't emit "+noaes" or "+nosha2" when we could emit |
| 694 | "+nocrypto" instead, in order to support assemblers that predate the |
| 695 | separate per-feature crypto flags. Only allow "+nocrypto" when "sm4" |
| 696 | is not already enabled (to avoid dependending on whether "+nocrypto" |
| 697 | also disables "sm4"). */ |
| 698 | if (flags & flags_crypto |
| 699 | && (flags_crypto & current_flags & ~isa_flags) == flags_crypto |
| 700 | && !(current_flags & AARCH64_FL_SM4)) |
| 701 | continue; |
| 702 | |
| 703 | if (flags == AARCH64_FL_CRYPTO) |
| 704 | /* If either crypto flag needs removing here, then both do. */ |
| 705 | flags = flags_crypto; |
| 706 | |
| 707 | if (flags & current_flags & ~isa_flags) |
| 708 | { |
| 709 | current_flags &= ~opt.flags_off; |
| 710 | outstr += "+no"; |
| 711 | outstr += opt.name; |
| 712 | } |
| 713 | } |
| 714 | |
| 715 | return outstr; |
| 716 | } |
| 717 | |
| 718 | /* Generate an arch string to be passed to the assembler. */ |
| 719 | |
| 720 | std::string |
| 721 | aarch64_get_arch_string_for_assembler (aarch64_arch arch, |
| 722 | aarch64_feature_flags flags) |
| 723 | { |
| 724 | const struct aarch64_arch_info *entry; |
| 725 | for (entry = all_architectures; entry->arch != aarch64_no_arch; entry++) |
| 726 | if (entry->arch == arch) |
| 727 | break; |
| 728 | |
| 729 | std::string outstr = entry->name |
| 730 | + aarch64_get_extension_string_for_isa_flags (flags, entry->flags); |
| 731 | |
| 732 | return outstr; |
| 733 | } |
| 734 | |
| 735 | /* Called by the driver to rewrite a name passed to the -march |
| 736 | argument in preparation to be passed to the assembler. The |
| 737 | names passed from the commend line will be in ARGV, we want |
| 738 | to use the right-most argument, which should be in |
| 739 | ARGV[ARGC - 1]. ARGC should always be greater than 0. */ |
| 740 | |
| 741 | const char * |
| 742 | aarch64_rewrite_march (int argc, const char **argv) |
| 743 | { |
| 744 | gcc_assert (argc); |
| 745 | const char *name = argv[argc - 1]; |
| 746 | aarch64_arch arch; |
| 747 | aarch64_feature_flags flags; |
| 748 | |
| 749 | aarch64_validate_march (name, &arch, &flags); |
| 750 | |
| 751 | std::string outstr = aarch64_get_arch_string_for_assembler (arch, flags); |
| 752 | |
| 753 | /* We are going to memory leak here, nobody elsewhere |
| 754 | in the callchain is going to clean up after us. The alternative is |
| 755 | to allocate a static buffer, and assert that it is big enough for our |
| 756 | modified string, which seems much worse! */ |
| 757 | return xstrdup (outstr.c_str ()); |
| 758 | } |
| 759 | |
| 760 | /* Called by the driver to rewrite a name passed to the -mcpu argument |
| 761 | to an equivalent -march value to be passed to the assembler. The |
| 762 | names passed from the commend line will be in ARGV, we want |
| 763 | to use the right-most argument, which should be in |
| 764 | ARGV[ARGC - 1]. ARGC should always be greater than 0. */ |
| 765 | |
| 766 | const char * |
| 767 | aarch64_rewrite_mcpu (int argc, const char **argv) |
| 768 | { |
| 769 | gcc_assert (argc); |
| 770 | const char *name = argv[argc - 1]; |
| 771 | aarch64_cpu cpu; |
| 772 | aarch64_feature_flags flags; |
| 773 | |
| 774 | aarch64_validate_mcpu (name, &cpu, &flags); |
| 775 | |
| 776 | const struct aarch64_processor_info *entry; |
| 777 | for (entry = all_cores; entry->processor != aarch64_no_cpu; entry++) |
| 778 | if (entry->processor == cpu) |
| 779 | break; |
| 780 | |
| 781 | std::string outstr = aarch64_get_arch_string_for_assembler (entry->arch, |
| 782 | flags); |
| 783 | |
| 784 | /* We are going to memory leak here, nobody elsewhere |
| 785 | in the callchain is going to clean up after us. The alternative is |
| 786 | to allocate a static buffer, and assert that it is big enough for our |
| 787 | modified string, which seems much worse! */ |
| 788 | return xstrdup (outstr.c_str ()); |
| 789 | } |
| 790 | |
| 791 | /* Checks to see if the host CPU may not be Cortex-A53 or an unknown Armv8-a |
| 792 | baseline CPU. */ |
| 793 | |
| 794 | const char * |
| 795 | is_host_cpu_not_armv8_base (int argc, const char **argv) |
| 796 | { |
| 797 | gcc_assert (argc); |
| 798 | |
| 799 | /* Default to not knowing what we are if unspecified. The SPEC file should |
| 800 | have already mapped configure time options to here through |
| 801 | OPTION_DEFAULT_SPECS so we don't need to check the configure variants |
| 802 | manually. */ |
| 803 | if (!argv[0]) |
| 804 | return NULL; |
| 805 | |
| 806 | const char *res = argv[0]; |
| 807 | |
| 808 | /* No SVE system is baseline Armv8-A. */ |
| 809 | if (strstr (res, "+sve")) |
| 810 | return ""; |
| 811 | |
| 812 | if (strstr (res, "cortex-a53") || strstr (res, "armv8-a")) |
| 813 | return NULL; |
| 814 | |
| 815 | return ""; |
| 816 | } |
| 817 | |
| 818 | struct gcc_targetm_common targetm_common = TARGETM_COMMON_INITIALIZER; |
| 819 | |
| 820 | #undef AARCH64_CPU_NAME_LENGTH |
| 821 |