This is a follow-up to https://gcc.gnu.org/pipermail/gcc-patches/2026-June/721898.html with the following changes:
- Clarification w.r.t. to overlapping register asm output operands: Since we already error out during expand in such cases (independent whether operands are global or local register asm), I kept this but moved it to gimplify_asm_expr() in order to match similar logic for input operands which brings me to - Diagnose during gimplify_asm_expr() if input operands have overlapping register sets (independent whether operands are global or local register asm). In case of -fstrict-extended-asm an error is emitted and otherwise a warning. I went for a warning for the latter case since previously we didn't diagnose such cases at all. - Added more tests to asm-hard-reg-strict-3.c in order to demonstrate what is invalid. - Added diagnostics when a local register asm object is declared in an outer function and used in a nested one (see new test asm-hard-reg-strict-7.c). We should probably do the same for C++ lambdas, however, so far I haven't decided how to test for this during gimplification and whether this shouldn't rather be done by the FE in the first place? - Added an overload for decode_reg_name(). Bootstrap and regtest are still running. -- >8 -- From: Stefan Schulze Frielinghaus <[email protected]> Currently local register asm assignments materialize during expand into assignments utilizing hard registers. Since prior register allocation, hard registers or more precisely objects residing in hard registers are not tracked individually, those are subject to be clobbered. Well known and documented are function calls which may clobber hard registers used for register asm objects. For example, compiling on aarch64 register int x asm ("x0") = 0x123; register int y asm ("x1") = *ptr; using address sanitizers results after expand in x0:SI=0x123 x0:DI=r104:DI call [`__asan_load4'] argc:0 x1:SI=[r104:DI] The implicit function call added by the address sanitizer clobbers argument register x0 which was previously set for the register asm object. With the advent of hard register constraints, this can be overcome. Instead of expanding a register asm assignment directly into a hard register assignment, keep the register asm object in a pseudo for as long as possible and use a hard register constraint in Extended Asm statements which ensures that the object is finally allocated the respective hard register. Since local register asm is supposed to have an effect only for Extended Asm statements, this coincides with hard register constraints which materialize for the respective insn. This patch adds the feature of rewriting local register asm into code which exploits hard register constraints. For example register int global asm ("r3"); int foo (int x0) { register int x asm ("r4") = x0; register int y asm ("r5"); asm ("bar\t%0,%1,%2" : "=r" (x) : "0" (x), "r" (global)); x += 42; asm ("baz\t%0,%1" : "=r" (y) : "r" (x)); return y; } is rewritten during gimplification into register int global asm ("r3"); int foo (int x0) { int x = x0; int y; asm ("bar\t%0,%1,%2" : "={r4}" (x) : "0" (x), "r" (global)); x += 42; asm ("baz\t%0,%1" : "={r5}" (y) : "{r4}" (x)); return y; } The resulting code solely relies on hard register constraints modulo global register asm. Thus, for GIMPLE we fall back to ordinary SSA_NAMES and finally for RTL to pseudos. Note, since hard register constraints are more strict in order to prevent subtle bugs, this in turn means that certain programs are not valid after register asm demotion anymore. For example, register int x asm ("r5") = 42; asm ("" : "+r" (x) : "r" (x)); is rewritten into int x = 42; asm ("" : "={r5}" (x) : "0" (x), "{r5}" (x)); Two inputs refer to the very same register which is invalid when using hard register constraints. Therefore, currently the transformation is hidden behind new flag -fstrict-extended-asm which is disabled by default. This patch also adds the new warning -Wstrict-extended-asm which is enabled by -Wall in order to diagnose cases for which -fstrict-extended-asm errors out. Another incompatibility which is worthwhile to mention are usages of uninitialized register asm input operands which is undefined for -fstrict-extended-asm. In case -W{maybe-,}uninitialized is specified a diagnostics may be emitted. In order to automatically rewrite register asm into hard register constraints, it is crucial that the register referred to by the register asm operand is entailed in the register class of the corresponding constraint of the operand. If this is not the case, then error out in case of -fstrict-extended-asm, or in case of -fno-strict-extended-asm -Wstrict-extended-asm emit a diagnostics. gcc/ChangeLog: * cfgexpand.cc (expand_asm_stmt): Verify that register asm output operands have no overlapping registers during gimplification. * common.opt (Wstrict-extended-asm): New warning. (fstrict-extended-asm) New flag. * doc/invoke.texi: Document new warning and flag. * gimplify.cc (gimplify_demote_register_asm): Helper for gimplify_asm_expr in order to prepare demotion of a register asm object into an ordinary one by rewriting constraints. (gimplify_asm_expr): Add diagnostics logic for new warning. (gimplify_body): Cleanup helper hash set. * output.h (decode_reg_name): New function. * stmt.cc (parse_output_constraint): Utilize new function. Add diagnostics logic. * varasm.cc (decode_reg_name): New function. gcc/testsuite/ChangeLog: * gcc.dg/asm-hard-reg-strict-1.c: New test. * gcc.dg/asm-hard-reg-strict-2.c: New test. * gcc.dg/asm-hard-reg-strict-3.c: New test. * gcc.dg/asm-hard-reg-strict-4.c: New test. * gcc.dg/asm-hard-reg-strict-5.c: New test. * gcc.dg/asm-hard-reg-strict-6.c: New test. * gcc.dg/asm-hard-reg-strict-7.c: New test. --- gcc/cfgexpand.cc | 12 - gcc/common.opt | 8 + gcc/doc/invoke.texi | 63 +++- gcc/gimplify.cc | 299 ++++++++++++++++--- gcc/output.h | 4 + gcc/stmt.cc | 48 ++- gcc/testsuite/gcc.dg/asm-hard-reg-strict-1.c | 53 ++++ gcc/testsuite/gcc.dg/asm-hard-reg-strict-2.c | 54 ++++ gcc/testsuite/gcc.dg/asm-hard-reg-strict-3.c | 70 +++++ gcc/testsuite/gcc.dg/asm-hard-reg-strict-4.c | 72 +++++ gcc/testsuite/gcc.dg/asm-hard-reg-strict-5.c | 65 ++++ gcc/testsuite/gcc.dg/asm-hard-reg-strict-6.c | 13 + gcc/testsuite/gcc.dg/asm-hard-reg-strict-7.c | 41 +++ gcc/varasm.cc | 16 + 14 files changed, 750 insertions(+), 68 deletions(-) create mode 100644 gcc/testsuite/gcc.dg/asm-hard-reg-strict-1.c create mode 100644 gcc/testsuite/gcc.dg/asm-hard-reg-strict-2.c create mode 100644 gcc/testsuite/gcc.dg/asm-hard-reg-strict-3.c create mode 100644 gcc/testsuite/gcc.dg/asm-hard-reg-strict-4.c create mode 100644 gcc/testsuite/gcc.dg/asm-hard-reg-strict-5.c create mode 100644 gcc/testsuite/gcc.dg/asm-hard-reg-strict-6.c create mode 100644 gcc/testsuite/gcc.dg/asm-hard-reg-strict-7.c diff --git a/gcc/cfgexpand.cc b/gcc/cfgexpand.cc index 7517e8d5788..9bf0765b4e6 100644 --- a/gcc/cfgexpand.cc +++ b/gcc/cfgexpand.cc @@ -3528,18 +3528,6 @@ expand_asm_stmt (gasm *stmt) bool early_clobber_p = strchr (constraints[i], '&') != NULL; unsigned long match; - /* Verify the other outputs do not use the same hard register. */ - for (j = i + 1; j < noutputs; ++j) - if (DECL_P (output_tvec[j]) - && REG_P (DECL_RTL (output_tvec[j])) - && HARD_REGISTER_P (DECL_RTL (output_tvec[j])) - && output_hregno == REGNO (DECL_RTL (output_tvec[j]))) - { - error_at (locus, "invalid hard register usage between output " - "operands"); - error_seen = true; - } - /* Verify matching constraint operands use the same hard register and that the non-matching constraint operands do not use the same hard register if the output is an early clobber operand. */ diff --git a/gcc/common.opt b/gcc/common.opt index 3ad1444cc88..933f791aeb4 100644 --- a/gcc/common.opt +++ b/gcc/common.opt @@ -936,6 +936,10 @@ Wzero-init-padding-bits= Common Joined RejectNegative Enum(zero_init_padding_bits_kind) Var(warn_zero_init_padding_bits) Init(ZERO_INIT_PADDING_BITS_STANDARD) Warning -Wzero-init-padding-bits=[standard|unions|all] Warn about initializers that might not zero padding bits. +Wstrict-extended-asm +Common Var(warn_strict_extended_asm) Warning LangEnabledBy(C C++,Wall) +Warn about constructs for which -fstrict-extended-asm fails. + Xassembler Driver Separate @@ -3588,6 +3592,10 @@ fverbose-asm Common Var(flag_verbose_asm) Add extra commentary to assembler output. +fstrict-extended-asm +Common Var(flag_strict_extended_asm) Init(0) +Apply strict rules for extended asm comprising hard register constraint rules. + fvisibility= Common Joined RejectNegative Enum(symbol_visibility) Var(default_visibility) Init(VISIBILITY_DEFAULT) -fvisibility=[default|internal|hidden|protected] Set the default symbol visibility. diff --git a/gcc/doc/invoke.texi b/gcc/doc/invoke.texi index 6d1da9f610e..87844e66a54 100644 --- a/gcc/doc/invoke.texi +++ b/gcc/doc/invoke.texi @@ -210,7 +210,7 @@ in the following sections. -fpermitted-flt-eval-methods=@var{standard} -fplan9-extensions -fsigned-bitfields -funsigned-bitfields -fsigned-char -funsigned-char -fstrict-flex-arrays[=@var{n}] --fsso-struct=@var{endianness} --ansi} +-fstrict-extended-asm -fsso-struct=@var{endianness} --ansi} @item C++ Language Options @xref{C++ Dialect Options,,Options Controlling C++ Dialect}. @@ -448,7 +448,7 @@ Objective-C and Objective-C++ Dialects}. -Wstrict-aliasing=@var{n} -Wstrict-overflow -Wstrict-overflow=@var{n} -Wstring-compare -Wno-stringop-overflow -Wno-stringop-overread --Wno-stringop-truncation -Wstrict-flex-arrays +-Wno-stringop-truncation -Wstrict-flex-arrays -Wstrict-extended-asm -Wsuggest-attribute=@var{attribute-name} -Wswitch -Wno-switch-bool -Wswitch-default -Wswitch-enum -Wno-switch-outside-range -Wno-switch-unreachable -Wsync-nand @@ -2976,6 +2976,17 @@ The @option{-fstrict_flex_arrays} option interacts with the @option{-Wstrict-flex-arrays} option. @xref{Warning Options}, for more information. +@opindex fstrict-extended-asm +@opindex fno-strict-extended-asm +@item -fstrict-extended-asm +Apply strict rules for extended @code{asm} (@pxref{Extended Asm}). In +particular this means that register @code{asm} operands must follow the rules +for hard register constraints. For example, don't allow multiple input +operands to refer to the same register. Note, any use of an uninitialized +local register asm input operand is undefined. See also +@option{-Wstrict-extended-asm}. This flag is currently disabled by default and +will eventually be enabled by default in a future release. + @opindex fsso-struct @item -fsso-struct=@var{endianness} Set the default scalar storage order of structures and unions to the @@ -8990,6 +9001,54 @@ This option is more effective when @option{-ftree-vrp} is active (the default for @option{-O2} and above) but some warnings may be diagnosed even without optimization. +@opindex Wstrict-extended-asm +@opindex Wno-strict-extended-asm +@item -Wstrict-extended-asm +Warn about extended @code{asm} (@pxref{Extended Asm}) usages which could lead +to subtle bugs and for which @option{-fstrict-extended-asm} errors out. For +example, warn if an lvalue is used by multiple output operands: + +@smallexample +int x; +asm ("..." : "=r" (x), "=r" (x)); +@end smallexample + +Both output operands get a different register assigned but outside of the +extended @code{asm} only one is bound to @code{x}. + +Furthermore, warn about register @code{asm} usages, if multiple operands refer +to the same register (including overlaps in case of register pairs). + +@smallexample +register int x asm ("r5") = 42; +register int y asm ("r5") = 24; +asm ("..." : "=r" (x) : "r" (x), "r" (y)); +@end smallexample + +Here @code{x} and @code{y} refer to the same register @code{r5}. Note, this also +includes more subtle cases as for example: + +@smallexample +register int x asm ("r5") = 42; +asm ("..." : "+r" (x) : "r" (x)); +@end smallexample + +After multiplying out the in-out operand we are faced with two inputs which +refer to the same register. One time indirectly via @code{"0" (x)} and the +other one directly via @code{"r" (x)}. + +Also diagnose if a register @code{asm} operand does not coincide with its +corresponding constraint. Assume in the following that @code{f5} is a +floating-point register which is not entailed in the register class associated +with constraint @code{r}. + +@smallexample +register float x asm ("f5"); +asm ("..." : "=r" (x)); +@end smallexample + +This indicates a subtle bug which is diagnosed. + @opindex Wsuggest-attribute= @opindex Wno-suggest-attribute= @item -Wsuggest-attribute=@var{attribute-name} diff --git a/gcc/gimplify.cc b/gcc/gimplify.cc index e4db4b1d9bd..3b5fa4f3d4d 100644 --- a/gcc/gimplify.cc +++ b/gcc/gimplify.cc @@ -7976,6 +7976,71 @@ num_alternatives (const_tree link) return num + 1; } +/* Keep track of all register asm which have been replaced by hard register + constraints. After all asm statements of a function have been processed, + demote those to ordinary objects. */ +static hash_set<tree> demote_register_asm; + +/* If -fstrict-extended-asm is specified, rewrite constraints of Extended Asm + operands which refer to local register asm objects into hard register + constraints. Also mark those objects to be demoted from register asm + objects to ordinary objects which is done basically after gimplification of + the function body. */ + +static void +gimplify_demote_register_asm (tree link) +{ + tree op = TREE_VALUE (link); + if (!VAR_P (op) || !DECL_HARD_REGISTER (op) || is_global_var (op)) + return; + tree id = DECL_ASSEMBLER_NAME (op); + const char *regname = IDENTIFIER_POINTER (id); + ++regname; + int regno = decode_reg_name (regname); + if (regno < 0) + /* This indicates an error and we error out later on. */ + return; + /* Currently, fixed registers cannot be used for hard register constraints + which is why we skip those for the moment. */ + if (fixed_regs[regno]) + return; + const char *constraint + = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link))); + auto_vec<char, 64> constraint_new; + for (const char *p = constraint; *p; ) + { + bool changed_p = false; + enum constraint_num cn = lookup_constraint (p); + enum reg_class rclass = reg_class_for_constraint (cn); + if (rclass != NO_REGS) + { + /* During parse_{input,output}_constraint() we ensured that rclass + entails all registers required by the register asm operand. + Therefore, rewrite the constraint into a corresponding hard + register constraint. */ + constraint_new.safe_push ('{'); + size_t len = strlen (regname); + for (size_t i = 0; i < len; ++i) + constraint_new.safe_push (regname[i]); + constraint_new.safe_push ('}'); + changed_p = true; + } + + for (size_t len = CONSTRAINT_LEN (*p, p); len; len--, p++) + { + if (!changed_p) + constraint_new.safe_push (*p); + if (*p == '\0') + break; + } + } + constraint_new.safe_push ('\0'); + unsigned int len = constraint_new.length (); + tree str = build_string (len, constraint_new.address ()); + TREE_VALUE (TREE_PURPOSE (link)) = str; + demote_register_asm.add (op); +} + /* Gimplify the operands of an ASM_EXPR. Input operands should be a gimple value; output operands should be a gimple lvalue. */ @@ -8063,9 +8128,113 @@ gimplify_asm_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p) is_inout = false; } + /* In case of -fstrict-extended-asm or for hard register constraints, + error out if an lvalue is used for more than one output operand. + Otherwise emit at most a warning. For example, error out for + + asm ("" : "={0}" (x), "={1}" (x)); + asm ("" : "=r" (x), "={1}" (x)); + + and emit a warning for + + int x; + asm ("" : "=r" (x), "=r" (x)); + + if -fno-strict-extended-asm -Wstrict-register-asm is given. */ + + for (tree i = link_next; i; i = TREE_CHAIN (i)) + { + tree op1 = TREE_VALUE (link); + tree op2 = TREE_VALUE (i); + if (op1 != op2) + continue; + const char *constraint2 + = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (i))); + if (flag_strict_extended_asm + || strchr (constraint, '{') != nullptr + || strchr (constraint2, '{') != nullptr + /* We already diagnose global/local register asm cases during + expand as errors. Therefore, do here, too. */ + || (VAR_P (op2) && DECL_HARD_REGISTER (op2))) + { + error ("multiple outputs to lvalue %qE", op2); + return GS_ERROR; + } + else + warning (OPT_Wstrict_extended_asm, "multiple outputs to lvalue %qE", + op2); + } + + tree outputv = TREE_VALUE (link); + tree outtype = TREE_TYPE (outputv); + + /* There shouldn't be multiple register asm output operands referring to + the same register. For example: + + register int x asm ("5"); + register int y asm ("5"); + asm ("" : "=r" (x) : "=r" (y)); + + is error-prone. Note, this also includes cases when operands reside + in register pairs and overlap only partially (or in general for + multi-register operands). + + Previously this was diagnosed for local as well as global register asm + during expand with an error which is why we error out here, too. */ + if (VAR_P (outputv) + && DECL_HARD_REGISTER (outputv)) + { + int regno1 = decode_reg_name (outputv); + if (regno1 >= 0) + { + HARD_REG_SET hreg_set1; + CLEAR_HARD_REG_SET (hreg_set1); + add_to_hard_reg_set (&hreg_set1, TYPE_MODE (outtype), regno1); + for (tree i = link_next; i; i = TREE_CHAIN (i)) + { + tree outputv2 = TREE_VALUE (i); + if (VAR_P (outputv2) + && DECL_HARD_REGISTER (outputv2)) + { + int regno2 = decode_reg_name (outputv2); + if (regno2 < 0) + continue; + tree outtype2 = TREE_TYPE (outputv2); + int nregs2 + = hard_regno_nregs (regno2, TYPE_MODE (outtype2)); + for (int j = regno2; j < regno2 + nregs2; ++j) + { + if (!TEST_HARD_REG_BIT (hreg_set1, j)) + continue; + error ("multiple outputs to hard register: %s", + reg_names[j]); + return GS_ERROR; + } + } + } + } + + /* Furthermore, accessing a local register asm object declared in an + outer function is undefined. */ + if (!is_global_var (outputv) + && DECL_CONTEXT (outputv) + && DECL_CONTEXT (outputv) != current_function_decl) + { + const char *msg + = "accessing local register %<asm%> object %qE which is " + "declared in an outer function is undefined"; + if (flag_strict_extended_asm) + { + error (msg, outputv); + return GS_ERROR; + } + else + warning (OPT_Wstrict_extended_asm, msg, outputv); + } + } + /* If we can't make copies, we can only accept memory. Similarly for VLAs. */ - tree outtype = TREE_TYPE (TREE_VALUE (link)); if (TREE_ADDRESSABLE (outtype) || !COMPLETE_TYPE_P (outtype) || !tree_fits_poly_uint64_p (TYPE_SIZE_UNIT (outtype))) @@ -8226,45 +8395,6 @@ gimplify_asm_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p) } } - /* After all output operands have been gimplified, verify that each output - operand is used at most once in case of hard register constraints. Thus, - error out in cases like - asm ("" : "={0}" (x), "={1}" (x)); - or even for - asm ("" : "=r" (x), "={1}" (x)); - - FIXME: Ideally we would also error out for cases like - int x; - asm ("" : "=r" (x), "=r" (x)); - However, since code like that was previously accepted, erroring out now might - break existing code. On the other hand, we already error out for register - asm like - register int x asm ("0"); - asm ("" : "=r" (x), "=r" (x)); - Thus, maybe it wouldn't be too bad to also error out in the former - non-register-asm case. - */ - for (unsigned i = 0; i < vec_safe_length (outputs); ++i) - { - tree link = (*outputs)[i]; - tree op1 = TREE_VALUE (link); - const char *constraint - = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link))); - if (strchr (constraint, '{') != nullptr) - for (unsigned j = 0; j < vec_safe_length (outputs); ++j) - { - if (i == j) - continue; - tree link2 = (*outputs)[j]; - tree op2 = TREE_VALUE (link2); - if (op1 == op2) - { - error ("multiple outputs to lvalue %qE", op2); - return GS_ERROR; - } - } - } - link_next = NULL_TREE; int input_num = 0; for (link = ASM_INPUTS (expr); link; ++input_num, ++i, link = link_next) @@ -8283,8 +8413,10 @@ gimplify_asm_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p) is_inout = false; } + tree inputv = TREE_VALUE (link); + tree intype = TREE_TYPE (inputv); + /* If we can't make copies, we can only accept memory. */ - tree intype = TREE_TYPE (TREE_VALUE (link)); if (TREE_ADDRESSABLE (intype) || !COMPLETE_TYPE_P (intype) || !tree_fits_poly_uint64_p (TYPE_SIZE_UNIT (intype))) @@ -8299,10 +8431,72 @@ gimplify_asm_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p) } } + /* Similarly as for outputs there shouldn't be multiple register asm + input operands referring to the same register. In contrast to output + operands we didn't diagnose those so far. Therefore, error out only + in case of -fstrict-register-asm. */ + if (VAR_P (inputv) + && DECL_HARD_REGISTER (inputv)) + { + int regno1 = decode_reg_name (inputv); + if (regno1 >= 0) + { + HARD_REG_SET hreg_set1; + CLEAR_HARD_REG_SET (hreg_set1); + add_to_hard_reg_set (&hreg_set1, TYPE_MODE (intype), regno1); + for (tree i = link_next; i; i = TREE_CHAIN (i)) + { + tree inputv2 = TREE_VALUE (i); + if (VAR_P (inputv2) + && DECL_HARD_REGISTER (inputv2)) + { + int regno2 = decode_reg_name (inputv2); + if (regno2 < 0) + continue; + tree intype2 = TREE_TYPE (inputv2); + int nregs2 + = hard_regno_nregs (regno2, TYPE_MODE (intype2)); + for (int j = regno2; j < regno2 + nregs2; ++j) + { + if (!TEST_HARD_REG_BIT (hreg_set1, j)) + continue; + if (flag_strict_extended_asm) + { + error ("multiple inputs to hard register: %s", + reg_names[j]); + return GS_ERROR; + } + else + warning (OPT_Wstrict_extended_asm, + "multiple inputs to hard register: %s", + reg_names[j]); + } + } + } + } + + /* Furthermore, accessing a local register asm object declared in an + outer function is undefined. */ + if (!is_global_var (inputv) + && DECL_CONTEXT (inputv) + && DECL_CONTEXT (inputv) != current_function_decl) + { + const char *msg + = "accessing local register %<asm%> object %qE which is " + "declared in an outer function is undefined"; + if (flag_strict_extended_asm) + { + error (msg, inputv); + return GS_ERROR; + } + else + warning (OPT_Wstrict_extended_asm, msg, inputv); + } + } + /* If the operand is a memory input, it should be an lvalue. */ if (!allows_reg && allows_mem) { - tree inputv = TREE_VALUE (link); STRIP_NOPS (inputv); if (TREE_CODE (inputv) == PREDECREMENT_EXPR || TREE_CODE (inputv) == PREINCREMENT_EXPR @@ -8372,6 +8566,20 @@ gimplify_asm_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p) /* Do not add ASMs with errors to the gimple IL stream. */ if (ret != GS_ERROR) { + if (flag_strict_extended_asm) + { + for (unsigned i = 0; i < vec_safe_length (outputs); ++i) + { + tree link = (*outputs)[i]; + gimplify_demote_register_asm (link); + } + for (unsigned i = 0; i < vec_safe_length (inputs); ++i) + { + tree link = (*inputs)[i]; + gimplify_demote_register_asm (link); + } + } + stmt = gimple_build_asm_vec (TREE_STRING_POINTER (ASM_STRING (expr)), inputs, outputs, clobbers, labels); @@ -21874,6 +22082,13 @@ gimplify_body (tree fndecl, bool do_parms) } } + for (auto op : demote_register_asm) + { + DECL_REGISTER (op) = 0; + DECL_HARD_REGISTER (op) = 0; + } + demote_register_asm.empty (); + if ((flag_openacc || flag_openmp || flag_openmp_simd) && gimplify_omp_ctxp) { diff --git a/gcc/output.h b/gcc/output.h index 1f628ea2371..26f0d15d3af 100644 --- a/gcc/output.h +++ b/gcc/output.h @@ -161,6 +161,10 @@ extern void weak_finish (void); Prefixes such as % are optional. */ extern int decode_reg_name (const char *); +/* An overload which takes a tree operand which is expected to be a + DECL_HARD_REGISTER. */ +extern int decode_reg_name (tree); + /* Similar to decode_reg_name, but takes an extra parameter that is a pointer to the number of (internal) registers described by the external name. */ diff --git a/gcc/stmt.cc b/gcc/stmt.cc index 382a2d75c07..f1981cb423c 100644 --- a/gcc/stmt.cc +++ b/gcc/stmt.cc @@ -439,9 +439,7 @@ parse_output_constraint (const char **constraint_p, int operand_num, if (VAR_P (reg_info->operand) && DECL_HARD_REGISTER (reg_info->operand)) { - tree id = DECL_ASSEMBLER_NAME (reg_info->operand); - const char *asmspec = IDENTIFIER_POINTER (id) + 1; - int regno_op = decode_reg_name (asmspec); + int regno_op = decode_reg_name (reg_info->operand); if (regno != regno_op) { error ("constraint and register %<asm%> for output " @@ -482,9 +480,7 @@ parse_output_constraint (const char **constraint_p, int operand_num, && VAR_P (reg_info->operand) && DECL_HARD_REGISTER (reg_info->operand)) { - tree id = DECL_ASSEMBLER_NAME (reg_info->operand); - const char *asmspec = IDENTIFIER_POINTER (id) + 1; - int regno = decode_reg_name (asmspec); + int regno = decode_reg_name (reg_info->operand); if (regno < 0) { location_t loc = DECL_SOURCE_LOCATION (reg_info->operand); @@ -499,6 +495,23 @@ parse_output_constraint (const char **constraint_p, int operand_num, reg_names[overlap_regno]); return false; } + enum reg_class rclass = reg_class_for_constraint (cn); + machine_mode mode = TYPE_MODE (TREE_TYPE (reg_info->operand)); + if (rclass != NO_REGS + && !in_hard_reg_set_p (reg_class_contents[rclass], mode, + regno)) + { + if (flag_strict_extended_asm) + { + error + ("constraint and register %<asm%> do not coincide"); + return false; + } + else + warning + (OPT_Wstrict_extended_asm, + "constraint and register %<asm%> do not coincide"); + } reg_info->set_reg_asm_output (regno); if (early_clobbered) reg_info->set_early_clobbered (alt, operand_num, regno); @@ -691,9 +704,7 @@ repeat: if (VAR_P (reg_info->operand) && DECL_HARD_REGISTER (reg_info->operand)) { - tree id = DECL_ASSEMBLER_NAME (reg_info->operand); - const char *asmspec = IDENTIFIER_POINTER (id) + 1; - int regno_op = decode_reg_name (asmspec); + int regno_op = decode_reg_name (reg_info->operand); if (regno != regno_op) { error ("constraint and register %<asm%> for input " @@ -739,9 +750,7 @@ repeat: && VAR_P (reg_info->operand) && DECL_HARD_REGISTER (reg_info->operand)) { - tree id = DECL_ASSEMBLER_NAME (reg_info->operand); - const char *asmspec = IDENTIFIER_POINTER (id) + 1; - int regno = decode_reg_name (asmspec); + int regno = decode_reg_name (reg_info->operand); if (regno < 0) { location_t loc = DECL_SOURCE_LOCATION (reg_info->operand); @@ -756,6 +765,21 @@ repeat: reg_names[overlap_regno]); return false; } + enum reg_class rclass = reg_class_for_constraint (cn); + machine_mode mode = TYPE_MODE (TREE_TYPE (reg_info->operand)); + if (rclass != NO_REGS + && !in_hard_reg_set_p (reg_class_contents[rclass], mode, + regno)) + { + if (flag_strict_extended_asm) + { + error ("constraint and register %<asm%> do not coincide"); + return false; + } + else + warning (OPT_Wstrict_extended_asm, + "constraint and register %<asm%> do not coincide"); + } reg_info->set_reg_asm_input (regno); if ((constraint == orig_constraint && reg_info->test_early_clobbered_alt (alt, regno)) diff --git a/gcc/testsuite/gcc.dg/asm-hard-reg-strict-1.c b/gcc/testsuite/gcc.dg/asm-hard-reg-strict-1.c new file mode 100644 index 00000000000..235f0810d3d --- /dev/null +++ b/gcc/testsuite/gcc.dg/asm-hard-reg-strict-1.c @@ -0,0 +1,53 @@ +/* { dg-do compile { target aarch64*-*-* s390x-*-* x86_64-*-* } } */ +/* { dg-additional-options "-fstrict-extended-asm" } */ +/* { dg-additional-options "-msse2" { target x86_64-*-* } } */ + +/* Keep this file in sync with asm-hard-reg-strict-2.c. */ + +#if __aarch64__ +# define GPR "r5" +# define FPR "d5" +# define CSTR_GPR "r" +# define CSTR_FPR "w" +#elif __s390x__ +# define GPR "r5" +# define FPR "f5" +# define CSTR_GPR "r" +# define CSTR_FPR "f" +#elif __x86_64__ +# define GPR "cx" +# define FPR "xmm5" +# define CSTR_GPR "r" +# define CSTR_FPR "x" +#else +# error unsupported target +#endif + +int +test () +{ + register int x __asm__ (GPR) = 42; + register float y __asm__ (FPR) = 42; + + __asm__ ("" : "="CSTR_FPR (x)); /* { dg-error "constraint and register 'asm' do not coincide" } */ + __asm__ ("" : "="CSTR_FPR","CSTR_FPR (x)); /* { dg-error "constraint and register 'asm' do not coincide" } */ + __asm__ ("" : "="CSTR_GPR","CSTR_FPR (x)); /* { dg-error "constraint and register 'asm' do not coincide" } */ + __asm__ ("" : "="CSTR_FPR","CSTR_GPR (x)); /* { dg-error "constraint and register 'asm' do not coincide" } */ + + __asm__ __volatile ("" :: CSTR_FPR (x)); /* { dg-error "constraint and register 'asm' do not coincide" } */ + __asm__ __volatile ("" :: CSTR_FPR","CSTR_FPR (x)); /* { dg-error "constraint and register 'asm' do not coincide" } */ + __asm__ __volatile ("" :: CSTR_GPR","CSTR_FPR (x)); /* { dg-error "constraint and register 'asm' do not coincide" } */ + __asm__ __volatile ("" :: CSTR_FPR","CSTR_GPR (x)); /* { dg-error "constraint and register 'asm' do not coincide" } */ + + __asm__ ("" : "="CSTR_GPR (y)); /* { dg-error "constraint and register 'asm' do not coincide" } */ + __asm__ ("" : "="CSTR_GPR","CSTR_GPR (y)); /* { dg-error "constraint and register 'asm' do not coincide" } */ + __asm__ ("" : "="CSTR_FPR","CSTR_GPR (y)); /* { dg-error "constraint and register 'asm' do not coincide" } */ + __asm__ ("" : "="CSTR_GPR","CSTR_FPR (y)); /* { dg-error "constraint and register 'asm' do not coincide" } */ + + __asm__ __volatile ("" :: CSTR_GPR (y)); /* { dg-error "constraint and register 'asm' do not coincide" } */ + __asm__ __volatile ("" :: CSTR_GPR","CSTR_GPR (y)); /* { dg-error "constraint and register 'asm' do not coincide" } */ + __asm__ __volatile ("" :: CSTR_FPR","CSTR_GPR (y)); /* { dg-error "constraint and register 'asm' do not coincide" } */ + __asm__ __volatile ("" :: CSTR_GPR","CSTR_FPR (y)); /* { dg-error "constraint and register 'asm' do not coincide" } */ + + return x + y; +} diff --git a/gcc/testsuite/gcc.dg/asm-hard-reg-strict-2.c b/gcc/testsuite/gcc.dg/asm-hard-reg-strict-2.c new file mode 100644 index 00000000000..3671171edf0 --- /dev/null +++ b/gcc/testsuite/gcc.dg/asm-hard-reg-strict-2.c @@ -0,0 +1,54 @@ +/* { dg-do compile { target aarch64*-*-* s390x-*-* x86_64-*-* } } */ +/* { dg-additional-options "-fno-strict-extended-asm -Wstrict-extended-asm" } */ +/* { dg-additional-options "-msse2" { target x86_64-*-* } } */ + +/* This is a copy of asm-hard-reg-strict-1.c for -fno-strict-register-asm where + we expect warnings instead of errors. */ + +#if __aarch64__ +# define GPR "r5" +# define FPR "d5" +# define CSTR_GPR "r" +# define CSTR_FPR "w" +#elif __s390x__ +# define GPR "r5" +# define FPR "f5" +# define CSTR_GPR "r" +# define CSTR_FPR "f" +#elif __x86_64__ +# define GPR "cx" +# define FPR "xmm5" +# define CSTR_GPR "r" +# define CSTR_FPR "x" +#else +# error unsupported target +#endif + +int +test () +{ + register int x __asm__ (GPR) = 42; + register float y __asm__ (FPR) = 42; + + __asm__ ("" : "="CSTR_FPR (x)); /* { dg-warning "constraint and register 'asm' do not coincide" } */ + __asm__ ("" : "="CSTR_FPR","CSTR_FPR (x)); /* { dg-warning "constraint and register 'asm' do not coincide" } */ + __asm__ ("" : "="CSTR_GPR","CSTR_FPR (x)); /* { dg-warning "constraint and register 'asm' do not coincide" } */ + __asm__ ("" : "="CSTR_FPR","CSTR_GPR (x)); /* { dg-warning "constraint and register 'asm' do not coincide" } */ + + __asm__ __volatile ("" :: CSTR_FPR (x)); /* { dg-warning "constraint and register 'asm' do not coincide" } */ + __asm__ __volatile ("" :: CSTR_FPR","CSTR_FPR (x)); /* { dg-warning "constraint and register 'asm' do not coincide" } */ + __asm__ __volatile ("" :: CSTR_GPR","CSTR_FPR (x)); /* { dg-warning "constraint and register 'asm' do not coincide" } */ + __asm__ __volatile ("" :: CSTR_FPR","CSTR_GPR (x)); /* { dg-warning "constraint and register 'asm' do not coincide" } */ + + __asm__ ("" : "="CSTR_GPR (y)); /* { dg-warning "constraint and register 'asm' do not coincide" } */ + __asm__ ("" : "="CSTR_GPR","CSTR_GPR (y)); /* { dg-warning "constraint and register 'asm' do not coincide" } */ + __asm__ ("" : "="CSTR_FPR","CSTR_GPR (y)); /* { dg-warning "constraint and register 'asm' do not coincide" } */ + __asm__ ("" : "="CSTR_GPR","CSTR_FPR (y)); /* { dg-warning "constraint and register 'asm' do not coincide" } */ + + __asm__ __volatile ("" :: CSTR_GPR (y)); /* { dg-warning "constraint and register 'asm' do not coincide" } */ + __asm__ __volatile ("" :: CSTR_GPR","CSTR_GPR (y)); /* { dg-warning "constraint and register 'asm' do not coincide" } */ + __asm__ __volatile ("" :: CSTR_FPR","CSTR_GPR (y)); /* { dg-warning "constraint and register 'asm' do not coincide" } */ + __asm__ __volatile ("" :: CSTR_GPR","CSTR_FPR (y)); /* { dg-warning "constraint and register 'asm' do not coincide" } */ + + return x + y; +} diff --git a/gcc/testsuite/gcc.dg/asm-hard-reg-strict-3.c b/gcc/testsuite/gcc.dg/asm-hard-reg-strict-3.c new file mode 100644 index 00000000000..b8ea3db2a45 --- /dev/null +++ b/gcc/testsuite/gcc.dg/asm-hard-reg-strict-3.c @@ -0,0 +1,70 @@ +/* { dg-do compile { target aarch64*-*-* s390x-*-* x86_64-*-* } } */ +/* { dg-options "-fstrict-extended-asm" } */ + +/* Keep this file in sync with asm-hard-reg-strict-4.c. */ + +#if defined __aarch64__ +# define GPR "x19" +# define GPR_PAIR_A "x0" +# define GPR_PAIR_B "x1" +#elif defined __s390x__ +# define GPR "r7" +# define GPR_PAIR_A "r4" +# define GPR_PAIR_B "r5" +#elif defined __x86_64__ +# define GPR "rbx" +# define GPR_PAIR_A "rax" +# define GPR_PAIR_B "rdx" +#else +# error unsupported target +#endif + +register int g __asm__ (GPR); +register int g2 __asm__ (GPR); /* { dg-warning "register of 'g2' used for multiple global register variables" } */ + +void +test () +{ + register int x __asm__ (GPR) = 42; + register int y __asm__ (GPR) = 24; + int z; + register __int128 a __asm__ (GPR_PAIR_A) = 42; + register int b __asm__ (GPR_PAIR_B) = 24; + + /* Overlapping single register asm input operands. */ + __asm__ __volatile__ ("" : "+r" (x) : "r" (x)); /* { dg-error "multiple inputs to hard register" } */ + __asm__ __volatile__ ("" : "=r" (x) : "0" (x), "r" (x)); /* { dg-error "multiple inputs to hard register" } */ + __asm__ __volatile__ ("" : : "r" (x), "r" (x)); /* { dg-error "multiple inputs to hard register" } */ + __asm__ __volatile__ ("" : : "r" (x), "r" (y)); /* { dg-error "multiple inputs to hard register" } */ + + /* Early clobber is somewhat related if input and output operand registers + overlap but still distinct. */ + __asm__ __volatile__ ("" : "=&r" (x) : "r" (x)); /* { dg-error "invalid hard register usage between earlyclobber operand and input operand" } */ + __asm__ __volatile__ ("" : "=&r" (x) : "r" (y)); /* { dg-error "invalid hard register usage between earlyclobber operand and input operand" } */ + __asm__ __volatile__ ("" : "=&r" (a) : "r" (b)); /* { dg-error "invalid hard register usage between earlyclobber operand and input operand" } */ + + /* Overlapping register-pair asm input operands. */ + __asm__ __volatile__ ("" : "+r" (a) : "r" (b)); /* { dg-error "multiple inputs to hard register" } */ + __asm__ __volatile__ ("" : "=r" (a) : "0" (a), "r" (b)); /* { dg-error "multiple inputs to hard register" } */ + __asm__ __volatile__ ("" : : "r" (a), "r" (b)); /* { dg-error "multiple inputs to hard register" } */ + + /* Overlapping global register asm input operands. */ + __asm__ __volatile__ ("" : "+r" (g) : "r" (g)); /* { dg-error "multiple inputs to hard register" } */ + __asm__ __volatile__ ("" : "=r" (g) : "0" (g), "r" (g)); /* { dg-error "multiple inputs to hard register" } */ + __asm__ __volatile__ ("" : : "r" (g), "r" (g)); /* { dg-error "multiple inputs to hard register" } */ + __asm__ __volatile__ ("" : : "r" (x), "r" (g)); /* { dg-error "multiple inputs to hard register" } */ + + /* Overlapping register asm output operands. */ + __asm__ __volatile__ ("" : "=r" (x), "=r" (y)); /* { dg-error "multiple outputs to hard register" } */ + __asm__ __volatile__ ("" : "=r" (x), "=r" (g)); /* { dg-error "multiple outputs to hard register" } */ + __asm__ __volatile__ ("" : "=r" (g), "=r" (g2)); /* { dg-error "multiple outputs to hard register" } */ + __asm__ __volatile__ ("" : "=r" (a), "=r" (b)); /* { dg-error "multiple outputs to hard register" } */ + + /* Same lvalue. */ + __asm__ __volatile__ ("" : "=r" (x), "=r" (x)); /* { dg-error "multiple outputs to lvalue 'x'" } */ + __asm__ __volatile__ ("" : "=r" (g), "=r" (g)); /* { dg-error "multiple outputs to lvalue 'g'" } */ + __asm__ __volatile__ ("" : "=r" (z), "=r" (z)); /* { dg-error "multiple outputs to lvalue 'z'" } */ + __asm__ __volatile__ ("" : "=m" (z), "=r" (z)); /* { dg-error "multiple outputs to lvalue 'z'" } */ + __asm__ __volatile__ ("" : "=r" (z), "=m" (z)); /* { dg-error "multiple outputs to lvalue 'z'" } */ + __asm__ __volatile__ ("" : "=m" (z), "=m" (z)); /* { dg-error "multiple outputs to lvalue 'z'" } */ +} diff --git a/gcc/testsuite/gcc.dg/asm-hard-reg-strict-4.c b/gcc/testsuite/gcc.dg/asm-hard-reg-strict-4.c new file mode 100644 index 00000000000..269145e62ea --- /dev/null +++ b/gcc/testsuite/gcc.dg/asm-hard-reg-strict-4.c @@ -0,0 +1,72 @@ +/* { dg-do compile { target aarch64*-*-* s390x-*-* x86_64-*-* } } */ +/* { dg-options "-fno-strict-extended-asm -Wstrict-extended-asm" } */ + +/* This is a copy of asm-hard-reg-strict-3.c for -fno-strict-register-asm where + we expect warnings instead of errors (except for a few cases where we + errored out even in non-strict extended asm. */ + +#if defined __aarch64__ +# define GPR "x19" +# define GPR_PAIR_A "x0" +# define GPR_PAIR_B "x1" +#elif defined __s390x__ +# define GPR "r7" +# define GPR_PAIR_A "r4" +# define GPR_PAIR_B "r5" +#elif defined __x86_64__ +# define GPR "rbx" +# define GPR_PAIR_A "rax" +# define GPR_PAIR_B "rdx" +#else +# error unsupported target +#endif + +register int g __asm__ (GPR); +register int g2 __asm__ (GPR); /* { dg-warning "register of 'g2' used for multiple global register variables" } */ + +void +test () +{ + register int x __asm__ (GPR) = 42; + register int y __asm__ (GPR) = 24; + int z; + register __int128 a __asm__ (GPR_PAIR_A) = 42; + register int b __asm__ (GPR_PAIR_B) = 24; + + /* Overlapping single register asm input operands. */ + __asm__ __volatile__ ("" : "+r" (x) : "r" (x)); /* { dg-warning "multiple inputs to hard register" } */ + __asm__ __volatile__ ("" : "=r" (x) : "0" (x), "r" (x)); /* { dg-warning "multiple inputs to hard register" } */ + __asm__ __volatile__ ("" : : "r" (x), "r" (x)); /* { dg-warning "multiple inputs to hard register" } */ + __asm__ __volatile__ ("" : : "r" (x), "r" (y)); /* { dg-warning "multiple inputs to hard register" } */ + + /* Early clobber is somewhat related if input and output operand registers + overlap but still distinct. */ + __asm__ __volatile__ ("" : "=&r" (x) : "r" (x)); /* { dg-error "invalid hard register usage between earlyclobber operand and input operand" } */ + __asm__ __volatile__ ("" : "=&r" (x) : "r" (y)); /* { dg-error "invalid hard register usage between earlyclobber operand and input operand" } */ + __asm__ __volatile__ ("" : "=&r" (a) : "r" (b)); /* { dg-error "invalid hard register usage between earlyclobber operand and input operand" } */ + + /* Overlapping register-pair asm input operands. */ + __asm__ __volatile__ ("" : "+r" (a) : "r" (b)); /* { dg-warning "multiple inputs to hard register" } */ + __asm__ __volatile__ ("" : "=r" (a) : "0" (a), "r" (b)); /* { dg-warning "multiple inputs to hard register" } */ + __asm__ __volatile__ ("" : : "r" (a), "r" (b)); /* { dg-warning "multiple inputs to hard register" } */ + + /* Overlapping global register asm input operands. */ + __asm__ __volatile__ ("" : "+r" (g) : "r" (g)); /* { dg-warning "multiple inputs to hard register" } */ + __asm__ __volatile__ ("" : "=r" (g) : "0" (g), "r" (g)); /* { dg-warning "multiple inputs to hard register" } */ + __asm__ __volatile__ ("" : : "r" (g), "r" (g)); /* { dg-warning "multiple inputs to hard register" } */ + __asm__ __volatile__ ("" : : "r" (x), "r" (g)); /* { dg-warning "multiple inputs to hard register" } */ + + /* Overlapping register asm output operands. */ + __asm__ __volatile__ ("" : "=r" (x), "=r" (y)); /* { dg-error "multiple outputs to hard register" } */ + __asm__ __volatile__ ("" : "=r" (x), "=r" (g)); /* { dg-error "multiple outputs to hard register" } */ + __asm__ __volatile__ ("" : "=r" (g), "=r" (g2)); /* { dg-error "multiple outputs to hard register" } */ + __asm__ __volatile__ ("" : "=r" (a), "=r" (b)); /* { dg-error "multiple outputs to hard register" } */ + + /* Same lvalue. */ + __asm__ __volatile__ ("" : "=r" (x), "=r" (x)); /* { dg-error "multiple outputs to lvalue 'x'" } */ + __asm__ __volatile__ ("" : "=r" (g), "=r" (g)); /* { dg-error "multiple outputs to lvalue 'g'" } */ + __asm__ __volatile__ ("" : "=r" (z), "=r" (z)); /* { dg-warning "multiple outputs to lvalue 'z'" } */ + __asm__ __volatile__ ("" : "=m" (z), "=r" (z)); /* { dg-warning "multiple outputs to lvalue 'z'" } */ + __asm__ __volatile__ ("" : "=r" (z), "=m" (z)); /* { dg-warning "multiple outputs to lvalue 'z'" } */ + __asm__ __volatile__ ("" : "=m" (z), "=m" (z)); /* { dg-warning "multiple outputs to lvalue 'z'" } */ +} diff --git a/gcc/testsuite/gcc.dg/asm-hard-reg-strict-5.c b/gcc/testsuite/gcc.dg/asm-hard-reg-strict-5.c new file mode 100644 index 00000000000..fae95ffe75c --- /dev/null +++ b/gcc/testsuite/gcc.dg/asm-hard-reg-strict-5.c @@ -0,0 +1,65 @@ +/* { dg-do compile { target aarch64*-*-* s390x-*-* x86_64-*-* } } */ +/* { dg-additional-options "-fstrict-extended-asm -fdump-tree-gimple" } */ +/* { dg-additional-options "-msse2" { target x86_64-*-* } } */ + +/* Test rewriting constraints into hard register constraints and demote + register asm objects into ordinary objects. */ + +#if __aarch64__ +# define GPR "r5" +# define FPR "d5" +# define CSTR_GPR "r" +# define CSTR_FPR "w" +/* { dg-final { scan-tree-dump-times "__asm__\\(\"\" : \"=\{r5\}\" x0\\);" 1 "gimple" { target aarch64-*-* } } } */ +/* { dg-final { scan-tree-dump-times "__asm__\\(\"\" : \"=g,!\{r5\}\" x0\\);" 1 "gimple" { target aarch64-*-* } } } */ +/* { dg-final { scan-tree-dump-times "__asm__\\(\"\" : \"=!\{r5\},g\" x0\\);" 1 "gimple" { target aarch64-*-* } } } */ +/* { dg-final { scan-tree-dump-times "__asm__\\(\"\" : \"=\{d5\}\" x1\\);" 1 "gimple" { target aarch64-*-* } } } */ +/* { dg-final { scan-tree-dump-times "__asm__\\(\"\" : \"=g,!\{d5\}\" x1\\);" 1 "gimple" { target aarch64-*-* } } } */ +/* { dg-final { scan-tree-dump-times "__asm__\\(\"\" : \"=!\{d5\},g\" x1\\);" 1 "gimple" { target aarch64-*-* } } } */ +#elif __s390x__ +# define GPR "r5" +# define FPR "f5" +# define CSTR_GPR "r" +# define CSTR_FPR "f" +/* { dg-final { scan-tree-dump-times "__asm__\\(\"\" : \"=\{r5\}\" x0\\);" 1 "gimple" { target s390x-*-* } } } */ +/* { dg-final { scan-tree-dump-times "__asm__\\(\"\" : \"=g,!\{r5\}\" x0\\);" 1 "gimple" { target s390x-*-* } } } */ +/* { dg-final { scan-tree-dump-times "__asm__\\(\"\" : \"=!\{r5\},g\" x0\\);" 1 "gimple" { target s390x-*-* } } } */ +/* { dg-final { scan-tree-dump-times "__asm__\\(\"\" : \"=\{f5\}\" x1\\);" 1 "gimple" { target s390x-*-* } } } */ +/* { dg-final { scan-tree-dump-times "__asm__\\(\"\" : \"=g,!\{f5\}\" x1\\);" 1 "gimple" { target s390x-*-* } } } */ +/* { dg-final { scan-tree-dump-times "__asm__\\(\"\" : \"=!\{f5\},g\" x1\\);" 1 "gimple" { target s390x-*-* } } } */ +#elif __x86_64__ +# define GPR "cx" +# define FPR "xmm5" +# define CSTR_GPR "r" +# define CSTR_FPR "x" +/* { dg-final { scan-tree-dump-times "__asm__\\(\"\" : \"=\{cx\}\" x0\\);" 1 "gimple" { target x86_64-*-* } } } */ +/* { dg-final { scan-tree-dump-times "__asm__\\(\"\" : \"=g,!\{cx\}\" x0\\);" 1 "gimple" { target x86_64-*-* } } } */ +/* { dg-final { scan-tree-dump-times "__asm__\\(\"\" : \"=!\{cx\},g\" x0\\);" 1 "gimple" { target x86_64-*-* } } } */ +/* { dg-final { scan-tree-dump-times "__asm__\\(\"\" : \"=\{xmm5\}\" x1\\);" 1 "gimple" { target x86_64-*-* } } } */ +/* { dg-final { scan-tree-dump-times "__asm__\\(\"\" : \"=g,!\{xmm5\}\" x1\\);" 1 "gimple" { target x86_64-*-* } } } */ +/* { dg-final { scan-tree-dump-times "__asm__\\(\"\" : \"=!\{xmm5\},g\" x1\\);" 1 "gimple" { target x86_64-*-* } } } */ +#else +# error unsupported target +#endif + +int +test_gpr (void) +{ +/* { dg-final { scan-tree-dump-times "int x0;" 1 "gimple" } } */ + register int x0 __asm__ (GPR); + __asm__ ("" : "="CSTR_GPR (x0)); + __asm__ ("" : "=g,!"CSTR_GPR (x0)); + __asm__ ("" : "=!"CSTR_GPR",g" (x0)); + return x0; +} + +float +test_fpr (void) +{ +/* { dg-final { scan-tree-dump-times "float x1;" 1 "gimple" } } */ + register float x1 __asm__ (FPR); + __asm__ ("" : "="CSTR_FPR (x1)); + __asm__ ("" : "=g,!"CSTR_FPR (x1)); + __asm__ ("" : "=!"CSTR_FPR",g" (x1)); + return x1; +} diff --git a/gcc/testsuite/gcc.dg/asm-hard-reg-strict-6.c b/gcc/testsuite/gcc.dg/asm-hard-reg-strict-6.c new file mode 100644 index 00000000000..430a8aac612 --- /dev/null +++ b/gcc/testsuite/gcc.dg/asm-hard-reg-strict-6.c @@ -0,0 +1,13 @@ +/* { dg-do compile { target aarch64*-*-* s390x-*-* x86_64-*-* } } */ +/* { dg-additional-options "-fstrict-extended-asm -Wuninitialized" } */ + +/* Since for -fstrict-extended-asm usage of uninitialized register asm input + operands is undefined, test for uninit warnings. */ + +int +test (void) +{ + register int x __asm__ ("5"); + __asm__ ("" : "+r" (x)); /* { dg-warning "is used uninitialized" } */ + return x; +} diff --git a/gcc/testsuite/gcc.dg/asm-hard-reg-strict-7.c b/gcc/testsuite/gcc.dg/asm-hard-reg-strict-7.c new file mode 100644 index 00000000000..f1e5c770d3e --- /dev/null +++ b/gcc/testsuite/gcc.dg/asm-hard-reg-strict-7.c @@ -0,0 +1,41 @@ +/* { dg-do compile { target aarch64*-*-* s390x-*-* x86_64-*-* } } */ +/* { dg-options "-Wstrict-extended-asm" } */ + +/* Declaring and using a local register asm variable inside a nested function + is accepted. Whereas a local register asm variable declared in an outer + function and used in a nested function is invalid. */ + +#if __aarch64__ +# define GLOBAL_GPR "x19" +#elif __s390x__ +# define GLOBAL_GPR "r7" +#elif __x86_64__ +# define GLOBAL_GPR "rbx" +#else +# error unsupported target +#endif + +register int g __asm__ (GLOBAL_GPR); + +int foo () +{ + int bar () + { + __asm__ ("" : "+r" (g)); + register int x __asm__ ("5") = 42; + __asm__ ("" : "+r" (x)); + return x; + } + + register int y __asm__ ("5") = 42; + + int baz () + { + __asm__ ("" : "+r" (g)); + __asm__ ("" : "=r" (y)); /* { dg-warning "accessing local register 'asm' object 'y' which is declared in an outer function is undefined" } */ + __asm__ ("" :: "r" (y)); /* { dg-warning "accessing local register 'asm' object 'y' which is declared in an outer function is undefined" } */ + return y; + } + + return bar () + baz (); +} diff --git a/gcc/varasm.cc b/gcc/varasm.cc index 5999f1e5420..8aff66a6dc3 100644 --- a/gcc/varasm.cc +++ b/gcc/varasm.cc @@ -1100,6 +1100,22 @@ decode_reg_name (const char *name) return decode_reg_name_and_count (name, &count); } +/* Decode and return the register number referred to by a register asm. The + return value is negative in case the argument is not a register asm or in + cases described in the comment for decode_reg_name_and_count. */ + +int +decode_reg_name (tree x) +{ + if (!VAR_P (x) || !DECL_HARD_REGISTER (x)) + return -1; + tree id = DECL_ASSEMBLER_NAME (x); + const char *asmspec = IDENTIFIER_POINTER (id); + /* Skip asterisk marker. */ + ++asmspec; + return decode_reg_name (asmspec); +} + /* Return true if DECL's initializer is suitable for a BSS section. */ -- 2.54.0
