From 1557c7772f4463a1c815d66272963c6dba38f3ef Mon Sep 17 00:00:00 2001 From: Simon Dardis Date: Thu, 3 Aug 2017 14:01:17 +0000 Subject: [PATCH] [mips] Implement -muninit-const-in-rodata This option when combined with -mgpopt and -membedded-data places all uninitialized constant variables in the read-only section. Reviewers: atanasyan, nitesh.jain Differential Revision: https://reviews.llvm.org/D35917 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@309940 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/clang/Driver/Options.td | 8 ++++++++ include/clang/Frontend/CodeGenOptions.def | 2 ++ lib/CodeGen/TargetInfo.cpp | 14 ++++++++++++++ lib/Driver/ToolChains/Clang.cpp | 8 ++++++++ lib/Frontend/CompilerInvocation.cpp | 2 ++ test/CodeGen/mips-uninit-const-in-ro.c | 10 ++++++++++ 6 files changed, 44 insertions(+) create mode 100644 test/CodeGen/mips-uninit-const-in-ro.c diff --git a/include/clang/Driver/Options.td b/include/clang/Driver/Options.td index 571d52fc0a..a3308733db 100644 --- a/include/clang/Driver/Options.td +++ b/include/clang/Driver/Options.td @@ -2057,6 +2057,14 @@ def membedded_data : Flag<["-"], "membedded-data">, Group, def mno_embedded_data : Flag<["-"], "mno-embedded-data">, Group, HelpText<"Do not place constants in the .rodata section instead of the " ".sdata if they meet the -G threshold (MIPS)">; +def muninit_const_in_rodata : Flag<["-"], "muninit-const-in-rodata">, + Group, Flags<[DriverOption,CC1Option]>, HelpText<"Place " + "uninitialized constants in the read-only data section instead of" + " the common section (MIPS)">; +def mno_uninit_const_in_rodata : Flag<["-"], "mno-uninit-const-in-rodata">, + Group, HelpText<"Do not place uninitialized constants in the " + "read-only data section instead of the common" + " section (MIPS)">; def mnan_EQ : Joined<["-"], "mnan=">, Group; def mabicalls : Flag<["-"], "mabicalls">, Group, HelpText<"Enable SVR4-style position-independent code (Mips only)">; diff --git a/include/clang/Frontend/CodeGenOptions.def b/include/clang/Frontend/CodeGenOptions.def index ec40ca11ed..a5a26018a1 100644 --- a/include/clang/Frontend/CodeGenOptions.def +++ b/include/clang/Frontend/CodeGenOptions.def @@ -101,6 +101,8 @@ CODEGENOPT(MergeAllConstants , 1, 1) ///< Merge identical constants. CODEGENOPT(MergeFunctions , 1, 0) ///< Set when -fmerge-functions is enabled. CODEGENOPT(MSVolatile , 1, 0) ///< Set when /volatile:ms is enabled. CODEGENOPT(NoCommon , 1, 0) ///< Set when -fno-common or C++ is enabled. +CODEGENOPT(UInitCstDataInROData, 1, 0) ///< Set when -mgpopt & -membedded-data + ///< & -muinit-const-in-rodata is set CODEGENOPT(NoDwarfDirectoryAsm , 1, 0) ///< Set when -fno-dwarf-directory-asm is ///< enabled. CODEGENOPT(NoExecStack , 1, 0) ///< Set when -Wa,--noexecstack is enabled. diff --git a/lib/CodeGen/TargetInfo.cpp b/lib/CodeGen/TargetInfo.cpp index 12341431ac..473b312b42 100644 --- a/lib/CodeGen/TargetInfo.cpp +++ b/lib/CodeGen/TargetInfo.cpp @@ -6656,6 +6656,20 @@ public: void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &CGM, ForDefinition_t IsForDefinition) const override { + + if (const VarDecl *VD = dyn_cast_or_null(D)) { + if (CGM.getCodeGenOpts().UInitCstDataInROData && + VD->getType().isConstQualified() && !VD->hasInit()) { + llvm::GlobalVariable *GVar = dyn_cast_or_null(GV); + if (GVar && !GVar->hasSection()) { + GVar->setLinkage(llvm::GlobalValue::ExternalLinkage); + GVar->setSection("rodata"); + } + } + + return; + } + const FunctionDecl *FD = dyn_cast_or_null(D); if (!FD) return; llvm::Function *Fn = cast(GV); diff --git a/lib/Driver/ToolChains/Clang.cpp b/lib/Driver/ToolChains/Clang.cpp index 80fc9923d3..896561b4d5 100644 --- a/lib/Driver/ToolChains/Clang.cpp +++ b/lib/Driver/ToolChains/Clang.cpp @@ -1515,6 +1515,14 @@ void Clang::AddMIPSTargetArgs(const ArgList &Args, CmdArgs.push_back("-membedded-data=0"); } EmbeddedData->claim(); + + if (Arg *A = Args.getLastArg(options::OPT_muninit_const_in_rodata, + options::OPT_mno_uninit_const_in_rodata)) { + if (A->getOption().matches(options::OPT_muninit_const_in_rodata)) { + CmdArgs.push_back("-muninit-const-in-rodata"); + A->claim(); + } + } } } else if ((!ABICalls || (!NoABICalls && ABICalls)) && WantGPOpt) diff --git a/lib/Frontend/CompilerInvocation.cpp b/lib/Frontend/CompilerInvocation.cpp index 003ea55471..cdc3b4b04a 100644 --- a/lib/Frontend/CompilerInvocation.cpp +++ b/lib/Frontend/CompilerInvocation.cpp @@ -950,6 +950,8 @@ static bool ParseCodeGenArgs(CodeGenOptions &Opts, ArgList &Args, InputKind IK, Opts.Backchain = Args.hasArg(OPT_mbackchain); + Opts.UInitCstDataInROData = Args.hasArg(OPT_muninit_const_in_rodata); + Opts.EmitCheckPathComponentsToStrip = getLastArgIntValue( Args, OPT_fsanitize_undefined_strip_path_components_EQ, 0, Diags); diff --git a/test/CodeGen/mips-uninit-const-in-ro.c b/test/CodeGen/mips-uninit-const-in-ro.c new file mode 100644 index 0000000000..62dc68637d --- /dev/null +++ b/test/CodeGen/mips-uninit-const-in-ro.c @@ -0,0 +1,10 @@ +// RUN: %clang_cc1 -triple mips-mti--elf -emit-llvm -mrelocation-model static \ +// RUN: -target-feature +noabicalls -mllvm -mgpopt -mllvm \ +// RUN: -membedded-data=1 -muninit-const-in-rodata -o - %s | \ +// RUN: FileCheck %s + +// Test that -muninit-const-in-rodata places constant uninitialized structures +// in the .rodata section rather than the commeon section. + +// CHECK: @a = global [8 x i32] zeroinitializer, section "rodata", align 4 +const int a[8]; -- 2.40.0