Index: docs/ClangCommandLineReference.rst =================================================================== --- docs/ClangCommandLineReference.rst +++ docs/ClangCommandLineReference.rst @@ -2402,10 +2402,34 @@ ARM --- +.. option:: -ffixed-r5 + +Reserve the r5 register (ARM only) + +.. option:: -ffixed-r6 + +Reserve the r6 register (ARM only) + +.. option:: -ffixed-r7 + +Reserve the r7 register (ARM only) + +.. option:: -ffixed-r8 + +Reserve the r8 register (ARM only) + .. option:: -ffixed-r9 Reserve the r9 register (ARM only) +.. option:: -ffixed-r10 + +Reserve the r10 register (ARM only) + +.. option:: -ffixed-r11 + +Reserve the r11 register (ARM only) + .. option:: -mexecute-only, -mno-execute-only, -mpure-code Disallow generation of data access to code sections (ARM only) Index: include/clang/Basic/DiagnosticDriverKinds.td =================================================================== --- include/clang/Basic/DiagnosticDriverKinds.td +++ include/clang/Basic/DiagnosticDriverKinds.td @@ -424,4 +424,9 @@ "specify a MSP430 device, or -mhwmult to set hardware multiply type " "explicitly.">, InGroup; +// Frame pointer reservation. +def warn_reserved_frame_pointer : Warning< + "'%0' has been specified but '%1' may still be used as a frame pointer">, + InGroup; + } Index: include/clang/Basic/DiagnosticGroups.td =================================================================== --- include/clang/Basic/DiagnosticGroups.td +++ include/clang/Basic/DiagnosticGroups.td @@ -1052,3 +1052,6 @@ def CrossTU : DiagGroup<"ctu">; def CTADMaybeUnsupported : DiagGroup<"ctad-maybe-unsupported">; + +// Register reservation. +def FixedRegs : DiagGroup<"fixed-registers">; \ No newline at end of file Index: include/clang/Driver/Options.td =================================================================== --- include/clang/Driver/Options.td +++ include/clang/Driver/Options.td @@ -2115,8 +2115,9 @@ def mno_restrict_it: Flag<["-"], "mno-restrict-it">, Group, HelpText<"Allow generation of deprecated IT blocks for ARMv8. It is off by default for ARMv8 Thumb mode">; def marm : Flag<["-"], "marm">, Alias; -def ffixed_r9 : Flag<["-"], "ffixed-r9">, Group, - HelpText<"Reserve the r9 register (ARM only)">; +foreach i = {5-11} in + def ffixed_r#i : Flag<["-"], "ffixed-r"#i>, Group, + HelpText<"Reserve the r"#i#" register (ARM only)">; def mno_movt : Flag<["-"], "mno-movt">, Group, HelpText<"Disallow use of movt/movw pairs (ARM only)">; def mcrc : Flag<["-"], "mcrc">, Group, Index: lib/Basic/Targets/ARM.h =================================================================== --- lib/Basic/Targets/ARM.h +++ lib/Basic/Targets/ARM.h @@ -147,6 +147,9 @@ ArrayRef getGCCRegNames() const override; ArrayRef getGCCRegAliases() const override; + bool validateGlobalRegisterVariable(StringRef RegName, + unsigned RegSize, + bool &HasSizeMismatch) const override; bool validateAsmConstraint(const char *&Name, TargetInfo::ConstraintInfo &Info) const override; std::string convertConstraint(const char *&Constraint) const override; Index: lib/Basic/Targets/ARM.cpp =================================================================== --- lib/Basic/Targets/ARM.cpp +++ lib/Basic/Targets/ARM.cpp @@ -823,6 +823,22 @@ return llvm::makeArrayRef(GCCRegAliases); } +bool ARMTargetInfo::validateGlobalRegisterVariable( + StringRef RegName, unsigned RegSize, bool &HasSizeMismatch) const { + if (RegName.equals("r5") || + RegName.equals("r6") || + RegName.equals("r7") || + RegName.equals("r8") || + RegName.equals("r9") || + RegName.equals("r10") || + RegName.equals("r11") || + RegName.equals("sp")) { + HasSizeMismatch = false; + return true; + } + return false; +} + bool ARMTargetInfo::validateAsmConstraint( const char *&Name, TargetInfo::ConstraintInfo &Info) const { switch (*Name) { Index: lib/Driver/ToolChains/Arch/ARM.cpp =================================================================== --- lib/Driver/ToolChains/Arch/ARM.cpp +++ lib/Driver/ToolChains/Arch/ARM.cpp @@ -547,11 +547,37 @@ Features.push_back("+strict-align"); } - // llvm does not support reserving registers in general. There is support - // for reserving r9 on ARM though (defined as a platform-specific register - // in ARM EABI). - if (Args.hasArg(options::OPT_ffixed_r9)) - Features.push_back("+reserve-r9"); + // Do not allow r9 reservation with -frwpi. + if (Args.hasArg(options::OPT_ffixed_r9) && Args.hasArg(options::OPT_frwpi)) { + Arg *A = Args.getLastArg(options::OPT_ffixed_r9); + Arg *B = Args.getLastArg(options::OPT_frwpi); + D.Diag(diag::err_opt_not_valid_with_opt) << A->getAsString(Args) << B->getAsString(Args); + } + + // The compiler can still use a FP in certain circumstances, + // even when frame pointer elimination is enabled. Thus we should + // give a warning when trying to reserve a target's FP register. + const llvm::opt::OptSpecifier RestrictFPOpt = + (Triple.isOSDarwin() || (!Triple.isOSWindows() && Triple.isThumb())) + ? options::OPT_ffixed_r7 + : options::OPT_ffixed_r11; + if (Args.hasArg(RestrictFPOpt)) { + const std::string OptStr = Args.getLastArg(RestrictFPOpt)->getAsString(Args); + const unsigned int SubStrIndex = strlen("ffixed-r"); + D.Diag(diag::warn_reserved_frame_pointer) << OptStr << OptStr.substr(SubStrIndex); + } + +// Reservation of general purpose registers. +#define HANDLE_FFIXED_R(n) \ + if (Args.hasArg(options::OPT_ffixed_r##n)) \ + Features.push_back("+reserve-r" #n) + HANDLE_FFIXED_R(5); + HANDLE_FFIXED_R(6); + HANDLE_FFIXED_R(7); + HANDLE_FFIXED_R(8); + HANDLE_FFIXED_R(9); + HANDLE_FFIXED_R(10); + HANDLE_FFIXED_R(11); // The kext linker doesn't know how to deal with movw/movt. if (KernelOrKext || Args.hasArg(options::OPT_mno_movt)) Index: test/Driver/arm-reserved-reg-options.c =================================================================== --- /dev/null +++ test/Driver/arm-reserved-reg-options.c @@ -0,0 +1,35 @@ +// ## FP ARM + Thumb +// RUN: %clang -target arm-arm-none-eabi -### -ffixed-r11 -c %s 2>&1 | FileCheck -check-prefix=CHECK-WARNING-R11 %s +// RUN: %clang -target arm-arm-none-eabi -### -ffixed-r11 -Wno-fixed-registers -c %s 2>&1 | FileCheck -check-prefix=CHECK-NO-WARNING %s + +// RUN: %clang -target arm-arm-none-eabi -### -ffixed-r7 -mthumb -c %s 2>&1 | FileCheck -check-prefix=CHECK-WARNING-R7 %s +// RUN: %clang -target arm-arm-none-eabi -### -ffixed-r7 -mthumb -Wno-fixed-registers -c %s 2>&1 | FileCheck -check-prefix=CHECK-NO-WARNING %s + +// RUN: %clang -target thumbv6m-none-eabi -### -ffixed-r7 -c %s 2>&1 | FileCheck -check-prefix=CHECK-WARNING-R7 %s +// RUN: %clang -target thumbv6m-none-eabi -### -ffixed-r7 -Wno-fixed-registers -c %s 2>&1 | FileCheck -check-prefix=CHECK-NO-WARNING %s + +// ## FP Darwin (R7) +// RUN: %clang -target armv6-apple-darwin9 -### -ffixed-r7 -c %s 2>&1 | FileCheck -check-prefix=CHECK-WARNING-R7 %s +// RUN: %clang -target armv6-apple-darwin9 -### -ffixed-r7 -Wno-fixed-registers -c %s 2>&1 | FileCheck -check-prefix=CHECK-NO-WARNING %s + +// RUN: %clang -target armv6-apple-ios3 -### -ffixed-r7 -c %s 2>&1 | FileCheck -check-prefix=CHECK-WARNING-R7 %s +// RUN: %clang -target armv6-apple-ios3 -### -ffixed-r7 -Wno-fixed-registers -c %s 2>&1 | FileCheck -check-prefix=CHECK-NO-WARNING %s + +// RUN: %clang -target armv7s-apple-darwin10 -### -ffixed-r7 -c %s 2>&1 | FileCheck -check-prefix=CHECK-WARNING-R7 %s +// RUN: %clang -target armv7s-apple-darwin10 -### -ffixed-r7 -Wno-fixed-registers -c %s 2>&1 | FileCheck -check-prefix=CHECK-NO-WARNING %s + +// ## FP Windows (R11) +// RUN: %clang -target armv7-windows -### -ffixed-r11 -c %s 2>&1 | FileCheck -check-prefix=CHECK-WARNING-R11 %s +// RUN: %clang -target armv7-windows -### -ffixed-r11 -Wno-fixed-registers -c %s 2>&1 | FileCheck -check-prefix=CHECK-NO-WARNING %s + +// ## FRWPI (R9) +// RUN: %clang -target arm-arm-none-eabi -### -frwpi -ffixed-r9 -c %s 2>&1 | FileCheck -check-prefix=CHECK-RESERVED-FRWPI-CONFLICT %s +// RUN: %clang -target arm-arm-none-eabi -### -ffixed-r9 -c %s 2>&1 | FileCheck -check-prefix=CHECK-RESERVED-FRWPI-VALID %s +// RUN: %clang -target arm-arm-none-eabi -### -frwpi -c %s 2>&1 | FileCheck -check-prefix=CHECK-RESERVED-FRWPI-VALID %s + +// CHECK-WARNING-R11: warning: '-ffixed-r11' has been specified but 'r11' may still be used as a frame pointer [-Wfixed-registers] +// CHECK-WARNING-R7: warning: '-ffixed-r7' has been specified but 'r7' may still be used as a frame pointer [-Wfixed-registers] +// CHECK-NO-WARNING-NOT: may still be used as a frame pointer [-Wfixed-registers] + +// CHECK-RESERVED-FRWPI-CONFLICT: option '-ffixed-r9' cannot be specified with '-frwpi' +// CHECK-RESERVED-FRWPI-VALID-NOT: option '-ffixed-r9' cannot be specified with '-frwpi' \ No newline at end of file Index: test/Sema/arm-global-regs.c =================================================================== --- /dev/null +++ test/Sema/arm-global-regs.c @@ -0,0 +1,13 @@ +// RUN: %clang_cc1 -ffreestanding -fsyntax-only -verify -triple arm %s + +// Check a small subset of valid and invalid global register variable declarations. + +register unsigned arm_r3 __asm("r3"); //expected-error {{register 'r3' unsuitable for global register variables on this target}} + +register unsigned arm_r12 __asm("r12"); //expected-error {{register 'r12' unsuitable for global register variables on this target}} + +register unsigned arm_r5 __asm("r5"); + +register unsigned arm_r9 __asm("r9"); + +register unsigned arm_sp __asm("sp"); \ No newline at end of file