Refactor TargetOptions initialization into a single place.

The same code (~20 lines) for initializing a TargetOptions object from CodeGen
cmdline flags is duplicated 4 times in 4 different tools. This patch moves it
into a utility function.

Since the CodeGen/CommandFlags.h file defines cl::opt flags in a header, it's
a bit of a touchy situation because we should only link them into tools. So this
patch puts the init function in the header.



git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@201699 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Eli Bendersky
2014-02-19 17:09:35 +00:00
parent eb5fe7002a
commit cf42174647
5 changed files with 37 additions and 98 deletions

View File

@@ -19,6 +19,7 @@
#include "llvm/Support/CodeGen.h" #include "llvm/Support/CodeGen.h"
#include "llvm/Support/CommandLine.h" #include "llvm/Support/CommandLine.h"
#include "llvm/Target/TargetMachine.h" #include "llvm/Target/TargetMachine.h"
#include "llvm/Target/TargetOptions.h"
#include <string> #include <string>
using namespace llvm; using namespace llvm;
@@ -207,4 +208,30 @@ cl::opt<std::string> StartAfter("start-after",
cl::value_desc("pass-name"), cl::value_desc("pass-name"),
cl::init("")); cl::init(""));
// Common utility function tightly tied to the options listed here. Initializes
// a TargetOptions object with CodeGen flags and returns it.
static inline TargetOptions InitTargetOptionsFromCodeGenFlags() {
TargetOptions Options;
Options.LessPreciseFPMADOption = EnableFPMAD;
Options.NoFramePointerElim = DisableFPElim;
Options.AllowFPOpFusion = FuseFPOps;
Options.UnsafeFPMath = EnableUnsafeFPMath;
Options.NoInfsFPMath = EnableNoInfsFPMath;
Options.NoNaNsFPMath = EnableNoNaNsFPMath;
Options.HonorSignDependentRoundingFPMathOption =
EnableHonorSignDependentRoundingFPMath;
Options.UseSoftFloat = GenerateSoftFloatCalls;
if (FloatABIForCalls != FloatABI::Default)
Options.FloatABIType = FloatABIForCalls;
Options.NoZerosInBSS = DontPlaceZerosInBSS;
Options.GuaranteedTailCallOpt = EnableGuaranteedTailCallOpt;
Options.DisableTailCalls = DisableTailCalls;
Options.StackAlignmentOverride = OverrideStackAlignment;
Options.TrapFuncName = TrapFuncName;
Options.PositionIndependentExecutable = EnablePIE;
Options.EnableSegmentedStacks = SegmentedStacks;
Options.UseInitArray = UseInitArray;
return Options;
}
#endif #endif

View File

@@ -259,26 +259,7 @@ static int compileModule(char **argv, LLVMContext &Context) {
case '3': OLvl = CodeGenOpt::Aggressive; break; case '3': OLvl = CodeGenOpt::Aggressive; break;
} }
TargetOptions Options; TargetOptions Options = InitTargetOptionsFromCodeGenFlags();
Options.LessPreciseFPMADOption = EnableFPMAD;
Options.NoFramePointerElim = DisableFPElim;
Options.AllowFPOpFusion = FuseFPOps;
Options.UnsafeFPMath = EnableUnsafeFPMath;
Options.NoInfsFPMath = EnableNoInfsFPMath;
Options.NoNaNsFPMath = EnableNoNaNsFPMath;
Options.HonorSignDependentRoundingFPMathOption =
EnableHonorSignDependentRoundingFPMath;
Options.UseSoftFloat = GenerateSoftFloatCalls;
if (FloatABIForCalls != FloatABI::Default)
Options.FloatABIType = FloatABIForCalls;
Options.NoZerosInBSS = DontPlaceZerosInBSS;
Options.GuaranteedTailCallOpt = EnableGuaranteedTailCallOpt;
Options.DisableTailCalls = DisableTailCalls;
Options.StackAlignmentOverride = OverrideStackAlignment;
Options.TrapFuncName = TrapFuncName;
Options.PositionIndependentExecutable = EnablePIE;
Options.EnableSegmentedStacks = SegmentedStacks;
Options.UseInitArray = UseInitArray;
OwningPtr<TargetMachine> OwningPtr<TargetMachine>
target(TheTarget->createTargetMachine(TheTriple.getTriple(), target(TheTarget->createTargetMachine(TheTriple.getTriple(),

View File

@@ -77,26 +77,7 @@ int main(int argc, char **argv) {
InitializeAllAsmParsers(); InitializeAllAsmParsers();
// set up the TargetOptions for the machine // set up the TargetOptions for the machine
TargetOptions Options; TargetOptions Options = InitTargetOptionsFromCodeGenFlags();
Options.LessPreciseFPMADOption = EnableFPMAD;
Options.NoFramePointerElim = DisableFPElim;
Options.AllowFPOpFusion = FuseFPOps;
Options.UnsafeFPMath = EnableUnsafeFPMath;
Options.NoInfsFPMath = EnableNoInfsFPMath;
Options.NoNaNsFPMath = EnableNoNaNsFPMath;
Options.HonorSignDependentRoundingFPMathOption =
EnableHonorSignDependentRoundingFPMath;
Options.UseSoftFloat = GenerateSoftFloatCalls;
if (FloatABIForCalls != FloatABI::Default)
Options.FloatABIType = FloatABIForCalls;
Options.NoZerosInBSS = DontPlaceZerosInBSS;
Options.GuaranteedTailCallOpt = EnableGuaranteedTailCallOpt;
Options.DisableTailCalls = DisableTailCalls;
Options.StackAlignmentOverride = OverrideStackAlignment;
Options.TrapFuncName = TrapFuncName;
Options.PositionIndependentExecutable = EnablePIE;
Options.EnableSegmentedStacks = SegmentedStacks;
Options.UseInitArray = UseInitArray;
unsigned BaseArg = 0; unsigned BaseArg = 0;

View File

@@ -56,28 +56,6 @@ static void lto_initialize() {
} }
} }
static void lto_set_target_options(llvm::TargetOptions &Options) {
Options.LessPreciseFPMADOption = EnableFPMAD;
Options.NoFramePointerElim = DisableFPElim;
Options.AllowFPOpFusion = FuseFPOps;
Options.UnsafeFPMath = EnableUnsafeFPMath;
Options.NoInfsFPMath = EnableNoInfsFPMath;
Options.NoNaNsFPMath = EnableNoNaNsFPMath;
Options.HonorSignDependentRoundingFPMathOption =
EnableHonorSignDependentRoundingFPMath;
Options.UseSoftFloat = GenerateSoftFloatCalls;
if (FloatABIForCalls != llvm::FloatABI::Default)
Options.FloatABIType = FloatABIForCalls;
Options.NoZerosInBSS = DontPlaceZerosInBSS;
Options.GuaranteedTailCallOpt = EnableGuaranteedTailCallOpt;
Options.DisableTailCalls = DisableTailCalls;
Options.StackAlignmentOverride = OverrideStackAlignment;
Options.TrapFuncName = TrapFuncName;
Options.PositionIndependentExecutable = EnablePIE;
Options.EnableSegmentedStacks = SegmentedStacks;
Options.UseInitArray = UseInitArray;
}
/// lto_get_version - Returns a printable string. /// lto_get_version - Returns a printable string.
extern const char* lto_get_version() { extern const char* lto_get_version() {
return LTOCodeGenerator::getVersionString(); return LTOCodeGenerator::getVersionString();
@@ -120,8 +98,7 @@ lto_module_is_object_file_in_memory_for_target(const void* mem,
/// (check lto_get_error_message() for details). /// (check lto_get_error_message() for details).
lto_module_t lto_module_create(const char* path) { lto_module_t lto_module_create(const char* path) {
lto_initialize(); lto_initialize();
llvm::TargetOptions Options; llvm::TargetOptions Options = InitTargetOptionsFromCodeGenFlags();
lto_set_target_options(Options);
return LTOModule::makeLTOModule(path, Options, sLastErrorString); return LTOModule::makeLTOModule(path, Options, sLastErrorString);
} }
@@ -129,8 +106,7 @@ lto_module_t lto_module_create(const char* path) {
/// error (check lto_get_error_message() for details). /// error (check lto_get_error_message() for details).
lto_module_t lto_module_create_from_fd(int fd, const char *path, size_t size) { lto_module_t lto_module_create_from_fd(int fd, const char *path, size_t size) {
lto_initialize(); lto_initialize();
llvm::TargetOptions Options; llvm::TargetOptions Options = InitTargetOptionsFromCodeGenFlags();
lto_set_target_options(Options);
return LTOModule::makeLTOModule(fd, path, size, Options, sLastErrorString); return LTOModule::makeLTOModule(fd, path, size, Options, sLastErrorString);
} }
@@ -141,8 +117,7 @@ lto_module_t lto_module_create_from_fd_at_offset(int fd, const char *path,
size_t map_size, size_t map_size,
off_t offset) { off_t offset) {
lto_initialize(); lto_initialize();
llvm::TargetOptions Options; llvm::TargetOptions Options = InitTargetOptionsFromCodeGenFlags();
lto_set_target_options(Options);
return LTOModule::makeLTOModule(fd, path, map_size, offset, Options, return LTOModule::makeLTOModule(fd, path, map_size, offset, Options,
sLastErrorString); sLastErrorString);
} }
@@ -151,8 +126,7 @@ lto_module_t lto_module_create_from_fd_at_offset(int fd, const char *path,
/// NULL on error (check lto_get_error_message() for details). /// NULL on error (check lto_get_error_message() for details).
lto_module_t lto_module_create_from_memory(const void* mem, size_t length) { lto_module_t lto_module_create_from_memory(const void* mem, size_t length) {
lto_initialize(); lto_initialize();
llvm::TargetOptions Options; llvm::TargetOptions Options = InitTargetOptionsFromCodeGenFlags();
lto_set_target_options(Options);
return LTOModule::makeLTOModule(mem, length, Options, sLastErrorString); return LTOModule::makeLTOModule(mem, length, Options, sLastErrorString);
} }
@@ -162,8 +136,7 @@ lto_module_t lto_module_create_from_memory_with_path(const void* mem,
size_t length, size_t length,
const char *path) { const char *path) {
lto_initialize(); lto_initialize();
llvm::TargetOptions Options; llvm::TargetOptions Options = InitTargetOptionsFromCodeGenFlags();
lto_set_target_options(Options);
return LTOModule::makeLTOModule(mem, length, Options, sLastErrorString, path); return LTOModule::makeLTOModule(mem, length, Options, sLastErrorString, path);
} }
@@ -238,8 +211,7 @@ void lto_codegen_set_diagnostic_handler(lto_code_gen_t cg,
lto_code_gen_t lto_codegen_create(void) { lto_code_gen_t lto_codegen_create(void) {
lto_initialize(); lto_initialize();
TargetOptions Options; TargetOptions Options = InitTargetOptionsFromCodeGenFlags();
lto_set_target_options(Options);
LTOCodeGenerator *CodeGen = new LTOCodeGenerator(); LTOCodeGenerator *CodeGen = new LTOCodeGenerator();
if (CodeGen) if (CodeGen)

View File

@@ -273,29 +273,6 @@ static void AddStandardLinkPasses(PassManagerBase &PM) {
//===----------------------------------------------------------------------===// //===----------------------------------------------------------------------===//
// CodeGen-related helper functions. // CodeGen-related helper functions.
// //
static TargetOptions GetTargetOptions() {
TargetOptions Options;
Options.LessPreciseFPMADOption = EnableFPMAD;
Options.NoFramePointerElim = DisableFPElim;
Options.AllowFPOpFusion = FuseFPOps;
Options.UnsafeFPMath = EnableUnsafeFPMath;
Options.NoInfsFPMath = EnableNoInfsFPMath;
Options.NoNaNsFPMath = EnableNoNaNsFPMath;
Options.HonorSignDependentRoundingFPMathOption =
EnableHonorSignDependentRoundingFPMath;
Options.UseSoftFloat = GenerateSoftFloatCalls;
if (FloatABIForCalls != FloatABI::Default)
Options.FloatABIType = FloatABIForCalls;
Options.NoZerosInBSS = DontPlaceZerosInBSS;
Options.GuaranteedTailCallOpt = EnableGuaranteedTailCallOpt;
Options.DisableTailCalls = DisableTailCalls;
Options.StackAlignmentOverride = OverrideStackAlignment;
Options.TrapFuncName = TrapFuncName;
Options.PositionIndependentExecutable = EnablePIE;
Options.EnableSegmentedStacks = SegmentedStacks;
Options.UseInitArray = UseInitArray;
return Options;
}
CodeGenOpt::Level GetCodeGenOptLevel() { CodeGenOpt::Level GetCodeGenOptLevel() {
if (OptLevelO1) if (OptLevelO1)
@@ -327,7 +304,8 @@ static TargetMachine* GetTargetMachine(Triple TheTriple) {
} }
return TheTarget->createTargetMachine(TheTriple.getTriple(), return TheTarget->createTargetMachine(TheTriple.getTriple(),
MCPU, FeaturesStr, GetTargetOptions(), MCPU, FeaturesStr,
InitTargetOptionsFromCodeGenFlags(),
RelocModel, CMModel, RelocModel, CMModel,
GetCodeGenOptLevel()); GetCodeGenOptLevel());
} }