summaryrefslogtreecommitdiffstats
path: root/dynamic-layers/clang-layer/recipes-devtools/clang/files/llvm10-Replace-MCTargetOptionsCommandFlags.inc-and-CommandF.patch
diff options
context:
space:
mode:
Diffstat (limited to 'dynamic-layers/clang-layer/recipes-devtools/clang/files/llvm10-Replace-MCTargetOptionsCommandFlags.inc-and-CommandF.patch')
-rw-r--r--dynamic-layers/clang-layer/recipes-devtools/clang/files/llvm10-Replace-MCTargetOptionsCommandFlags.inc-and-CommandF.patch2243
1 files changed, 0 insertions, 2243 deletions
diff --git a/dynamic-layers/clang-layer/recipes-devtools/clang/files/llvm10-Replace-MCTargetOptionsCommandFlags.inc-and-CommandF.patch b/dynamic-layers/clang-layer/recipes-devtools/clang/files/llvm10-Replace-MCTargetOptionsCommandFlags.inc-and-CommandF.patch
deleted file mode 100644
index 6b2db931..00000000
--- a/dynamic-layers/clang-layer/recipes-devtools/clang/files/llvm10-Replace-MCTargetOptionsCommandFlags.inc-and-CommandF.patch
+++ /dev/null
@@ -1,2243 +0,0 @@
1From fd7146658e3b4ce045dfb332c2edf216f76c1e1f Mon Sep 17 00:00:00 2001
2From: serge-sans-paille <sguelton@redhat.com>
3Date: Wed, 4 Mar 2020 00:47:43 +0100
4Subject: [PATCH] Replace MCTargetOptionsCommandFlags.inc and CommandFlags.inc
5 by runtime registration
6
7MCTargetOptionsCommandFlags.inc and CommandFlags.inc are headers which contain
8cl::opt with static storage.
9These headers are meant to be incuded by tools to make it easier to parametrize
10codegen/mc.
11
12However, these headers are also included in at least two libraries: lldCommon
13and handle-llvm. As a result, when creating DYLIB, clang-cpp holds a reference
14to the options, and lldCommon holds another reference. Linking the two in a
15single executable, as zig does[0], results in a double registration.
16
17This patch explores an other approach: the .inc files are moved to regular
18files, and the registration happens on-demand through static declaration of
19options in the constructor of a static object.
20
21[0] https://bugzilla.redhat.com/show_bug.cgi?id=1756977#c5
22
23Differential Revision: https://reviews.llvm.org/D75579
24
25Upstream-Status: Backport
26Signed-off-by: Anuj Mittal <anuj.mittal@intel.com>
27---
28 .../clang-fuzzer/handle-llvm/handle_llvm.cpp | 32 +-
29 lld/Common/TargetOptionsCommandFlags.cpp | 25 +-
30 llvm/include/llvm/CodeGen/CommandFlags.h | 149 +++++
31 .../llvm/MC/MCTargetOptionsCommandFlags.h | 54 ++
32 .../llvm/MC/MCTargetOptionsCommandFlags.inc | 65 --
33 llvm/include/llvm/module.modulemap | 3 -
34 llvm/lib/CodeGen/CMakeLists.txt | 1 +
35 llvm/lib/CodeGen/CommandFlags.cpp | 588 ++++++++++++++++++
36 llvm/lib/MC/CMakeLists.txt | 1 +
37 llvm/lib/MC/MCTargetOptionsCommandFlags.cpp | 105 ++++
38 llvm/tools/dsymutil/DwarfStreamer.cpp | 7 +-
39 llvm/tools/gold/gold-plugin.cpp | 16 +-
40 llvm/tools/llc/CMakeLists.txt | 1 +
41 llvm/tools/llc/llc.cpp | 43 +-
42 llvm/tools/lli/CMakeLists.txt | 1 +
43 llvm/tools/lli/lli.cpp | 41 +-
44 llvm/tools/llvm-dwp/llvm-dwp.cpp | 6 +-
45 .../llvm-isel-fuzzer/llvm-isel-fuzzer.cpp | 17 +-
46 llvm/tools/llvm-lto/CMakeLists.txt | 5 +-
47 llvm/tools/llvm-lto/llvm-lto.cpp | 28 +-
48 llvm/tools/llvm-lto2/CMakeLists.txt | 1 +
49 llvm/tools/llvm-lto2/llvm-lto2.cpp | 20 +-
50 .../llvm-mc-assemble-fuzzer/CMakeLists.txt | 1 +
51 .../llvm-mc-assemble-fuzzer.cpp | 12 +-
52 llvm/tools/llvm-mc/CMakeLists.txt | 1 +
53 llvm/tools/llvm-mc/llvm-mc.cpp | 6 +-
54 llvm/tools/llvm-mca/llvm-mca.cpp | 8 +-
55 .../tools/llvm-opt-fuzzer/llvm-opt-fuzzer.cpp | 16 +-
56 llvm/tools/lto/CMakeLists.txt | 4 +-
57 llvm/tools/lto/lto.cpp | 35 +-
58 llvm/tools/opt/opt.cpp | 23 +-
59 .../DebugInfo/DWARF/DwarfGenerator.cpp | 6 +-
60 32 files changed, 1088 insertions(+), 233 deletions(-)
61 create mode 100644 llvm/include/llvm/CodeGen/CommandFlags.h
62 create mode 100644 llvm/include/llvm/MC/MCTargetOptionsCommandFlags.h
63 delete mode 100644 llvm/include/llvm/MC/MCTargetOptionsCommandFlags.inc
64 create mode 100644 llvm/lib/CodeGen/CommandFlags.cpp
65 create mode 100644 llvm/lib/MC/MCTargetOptionsCommandFlags.cpp
66
67diff --git a/clang/tools/clang-fuzzer/handle-llvm/handle_llvm.cpp b/clang/tools/clang-fuzzer/handle-llvm/handle_llvm.cpp
68index d8ab14594185..aefb761cd70c 100644
69--- a/clang/tools/clang-fuzzer/handle-llvm/handle_llvm.cpp
70+++ b/clang/tools/clang-fuzzer/handle-llvm/handle_llvm.cpp
71@@ -19,7 +19,7 @@
72 #include "llvm/ADT/Triple.h"
73 #include "llvm/Analysis/TargetLibraryInfo.h"
74 #include "llvm/Analysis/TargetTransformInfo.h"
75-#include "llvm/CodeGen/CommandFlags.inc"
76+#include "llvm/CodeGen/CommandFlags.h"
77 #include "llvm/CodeGen/MachineModuleInfo.h"
78 #include "llvm/CodeGen/TargetPassConfig.h"
79 #include "llvm/ExecutionEngine/JITEventListener.h"
80@@ -29,9 +29,9 @@
81 #include "llvm/ExecutionEngine/RTDyldMemoryManager.h"
82 #include "llvm/ExecutionEngine/SectionMemoryManager.h"
83 #include "llvm/IR/IRPrintingPasses.h"
84+#include "llvm/IR/LLVMContext.h"
85 #include "llvm/IR/LegacyPassManager.h"
86 #include "llvm/IR/LegacyPassNameParser.h"
87-#include "llvm/IR/LLVMContext.h"
88 #include "llvm/IR/Module.h"
89 #include "llvm/IR/Verifier.h"
90 #include "llvm/IRReader/IRReader.h"
91@@ -42,12 +42,14 @@
92 #include "llvm/Support/TargetRegistry.h"
93 #include "llvm/Support/TargetSelect.h"
94 #include "llvm/Target/TargetMachine.h"
95-#include "llvm/Transforms/IPO/PassManagerBuilder.h"
96 #include "llvm/Transforms/IPO.h"
97+#include "llvm/Transforms/IPO/PassManagerBuilder.h"
98 #include "llvm/Transforms/Vectorize.h"
99
100 using namespace llvm;
101
102+static codegen::RegisterCodeGenFlags CGF;
103+
104 // Define a type for the functions that are compiled and executed
105 typedef void (*LLVMFunc)(int*, int*, int*, int);
106
107@@ -100,15 +102,17 @@ static std::string OptLLVM(const std::string &IR, CodeGenOpt::Level OLvl) {
108 ErrorAndExit("Could not parse IR");
109
110 Triple ModuleTriple(M->getTargetTriple());
111- const TargetOptions Options = InitTargetOptionsFromCodeGenFlags();
112+ const TargetOptions Options = codegen::InitTargetOptionsFromCodeGenFlags();
113 std::string E;
114- const Target *TheTarget = TargetRegistry::lookupTarget(MArch, ModuleTriple, E);
115- TargetMachine *Machine =
116- TheTarget->createTargetMachine(M->getTargetTriple(), getCPUStr(),
117- getFeaturesStr(), Options, getRelocModel(),
118- getCodeModel(), OLvl);
119+ const Target *TheTarget =
120+ TargetRegistry::lookupTarget(codegen::getMArch(), ModuleTriple, E);
121+ TargetMachine *Machine = TheTarget->createTargetMachine(
122+ M->getTargetTriple(), codegen::getCPUStr(), codegen::getFeaturesStr(),
123+ Options, codegen::getExplicitRelocModel(),
124+ codegen::getExplicitCodeModel(), OLvl);
125 std::unique_ptr<TargetMachine> TM(Machine);
126- setFunctionAttributes(getCPUStr(), getFeaturesStr(), *M);
127+ codegen::setFunctionAttributes(codegen::getCPUStr(),
128+ codegen::getFeaturesStr(), *M);
129
130 legacy::PassManager Passes;
131
132@@ -154,14 +158,14 @@ static void CreateAndRunJITFunc(const std::string &IR, CodeGenOpt::Level OLvl) {
133
134 std::string ErrorMsg;
135 EngineBuilder builder(std::move(M));
136- builder.setMArch(MArch);
137- builder.setMCPU(getCPUStr());
138- builder.setMAttrs(getFeatureList());
139+ builder.setMArch(codegen::getMArch());
140+ builder.setMCPU(codegen::getCPUStr());
141+ builder.setMAttrs(codegen::getFeatureList());
142 builder.setErrorStr(&ErrorMsg);
143 builder.setEngineKind(EngineKind::JIT);
144 builder.setMCJITMemoryManager(std::make_unique<SectionMemoryManager>());
145 builder.setOptLevel(OLvl);
146- builder.setTargetOptions(InitTargetOptionsFromCodeGenFlags());
147+ builder.setTargetOptions(codegen::InitTargetOptionsFromCodeGenFlags());
148
149 std::unique_ptr<ExecutionEngine> EE(builder.create());
150 if (!EE)
151diff --git a/lld/Common/TargetOptionsCommandFlags.cpp b/lld/Common/TargetOptionsCommandFlags.cpp
152index 0137feb63f37..9b166a3e130a 100644
153--- a/lld/Common/TargetOptionsCommandFlags.cpp
154+++ b/lld/Common/TargetOptionsCommandFlags.cpp
155@@ -5,35 +5,26 @@
156 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
157 //
158 //===----------------------------------------------------------------------===//
159-//
160-// This file exists as a place for global variables defined in LLVM's
161-// CodeGen/CommandFlags.inc. By putting the resulting object file in
162-// an archive and linking with it, the definitions will automatically be
163-// included when needed and skipped when already present.
164-//
165-//===----------------------------------------------------------------------===//
166
167 #include "lld/Common/TargetOptionsCommandFlags.h"
168
169-#include "llvm/CodeGen/CommandFlags.inc"
170+#include "llvm/CodeGen/CommandFlags.h"
171 #include "llvm/Target/TargetOptions.h"
172
173-// Define an externally visible version of
174-// initTargetOptionsFromCodeGenFlags, so that its functionality can be
175-// used without having to include llvm/CodeGen/CommandFlags.inc, which
176-// would lead to multiple definitions of the command line flags.
177+static llvm::codegen::RegisterCodeGenFlags CGF;
178+
179 llvm::TargetOptions lld::initTargetOptionsFromCodeGenFlags() {
180- return ::InitTargetOptionsFromCodeGenFlags();
181+ return llvm::codegen::InitTargetOptionsFromCodeGenFlags();
182 }
183
184 llvm::Optional<llvm::Reloc::Model> lld::getRelocModelFromCMModel() {
185- return getRelocModel();
186+ return llvm::codegen::getExplicitRelocModel();
187 }
188
189 llvm::Optional<llvm::CodeModel::Model> lld::getCodeModelFromCMModel() {
190- return getCodeModel();
191+ return llvm::codegen::getExplicitCodeModel();
192 }
193
194-std::string lld::getCPUStr() { return ::getCPUStr(); }
195+std::string lld::getCPUStr() { return llvm::codegen::getCPUStr(); }
196
197-std::vector<std::string> lld::getMAttrs() { return ::MAttrs; }
198+std::vector<std::string> lld::getMAttrs() { return llvm::codegen::getMAttrs(); }
199diff --git a/llvm/include/llvm/CodeGen/CommandFlags.h b/llvm/include/llvm/CodeGen/CommandFlags.h
200new file mode 100644
201index 000000000000..cdec26879f73
202--- /dev/null
203+++ b/llvm/include/llvm/CodeGen/CommandFlags.h
204@@ -0,0 +1,149 @@
205+//===-- CommandFlags.h - Command Line Flags Interface -----------*- C++ -*-===//
206+//
207+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
208+// See https://llvm.org/LICENSE.txt for license information.
209+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
210+//
211+//===----------------------------------------------------------------------===//
212+//
213+// This file contains codegen-specific flags that are shared between different
214+// command line tools. The tools "llc" and "opt" both use this file to prevent
215+// flag duplication.
216+//
217+//===----------------------------------------------------------------------===//
218+
219+#include "llvm/ADT/StringExtras.h"
220+#include "llvm/IR/Instructions.h"
221+#include "llvm/IR/Intrinsics.h"
222+#include "llvm/IR/Module.h"
223+#include "llvm/MC/MCTargetOptionsCommandFlags.h"
224+#include "llvm/MC/SubtargetFeature.h"
225+#include "llvm/Support/CodeGen.h"
226+#include "llvm/Support/CommandLine.h"
227+#include "llvm/Support/Host.h"
228+#include "llvm/Target/TargetMachine.h"
229+#include "llvm/Target/TargetOptions.h"
230+#include <string>
231+
232+namespace llvm {
233+
234+namespace codegen {
235+
236+std::string getMArch();
237+
238+std::string getMCPU();
239+
240+std::vector<std::string> getMAttrs();
241+
242+Reloc::Model getRelocModel();
243+Optional<Reloc::Model> getExplicitRelocModel();
244+
245+ThreadModel::Model getThreadModel();
246+
247+CodeModel::Model getCodeModel();
248+Optional<CodeModel::Model> getExplicitCodeModel();
249+
250+llvm::ExceptionHandling getExceptionModel();
251+
252+CodeGenFileType getFileType();
253+Optional<CodeGenFileType> getExplicitFileType();
254+
255+CodeGenFileType getFileType();
256+
257+llvm::FramePointer::FP getFramePointerUsage();
258+
259+bool getEnableUnsafeFPMath();
260+
261+bool getEnableNoInfsFPMath();
262+
263+bool getEnableNoNaNsFPMath();
264+
265+bool getEnableNoSignedZerosFPMath();
266+
267+bool getEnableNoTrappingFPMath();
268+
269+llvm::FPDenormal::DenormalMode getDenormalFPMath();
270+
271+bool getEnableHonorSignDependentRoundingFPMath();
272+
273+llvm::FloatABI::ABIType getFloatABIForCalls();
274+
275+llvm::FPOpFusion::FPOpFusionMode getFuseFPOps();
276+
277+bool getDontPlaceZerosInBSS();
278+
279+bool getEnableGuaranteedTailCallOpt();
280+
281+bool getDisableTailCalls();
282+
283+bool getStackSymbolOrdering();
284+
285+unsigned getOverrideStackAlignment();
286+
287+bool getStackRealign();
288+
289+std::string getTrapFuncName();
290+
291+bool getUseCtors();
292+
293+bool getRelaxELFRelocations();
294+
295+bool getDataSections();
296+Optional<bool> getExplicitDataSections();
297+
298+bool getFunctionSections();
299+Optional<bool> getExplicitFunctionSections();
300+
301+std::string getBBSections();
302+
303+unsigned getTLSSize();
304+
305+bool getEmulatedTLS();
306+
307+bool getUniqueSectionNames();
308+
309+bool getUniqueBBSectionNames();
310+
311+llvm::EABI getEABIVersion();
312+
313+llvm::DebuggerKind getDebuggerTuningOpt();
314+
315+bool getEnableStackSizeSection();
316+
317+bool getEnableAddrsig();
318+
319+bool getEmitCallSiteInfo();
320+
321+bool getEnableDebugEntryValues();
322+
323+bool getForceDwarfFrameSection();
324+
325+/// Create this object with static storage to register codegen-related command
326+/// line options.
327+struct RegisterCodeGenFlags {
328+ RegisterCodeGenFlags();
329+};
330+
331+llvm::BasicBlockSection getBBSectionsMode(llvm::TargetOptions &Options);
332+
333+// Common utility function tightly tied to the options listed here. Initializes
334+// a TargetOptions object with CodeGen flags and returns it.
335+TargetOptions InitTargetOptionsFromCodeGenFlags();
336+
337+std::string getCPUStr();
338+
339+std::string getFeaturesStr();
340+
341+std::vector<std::string> getFeatureList();
342+
343+void renderBoolStringAttr(AttrBuilder &B, StringRef Name, bool Val);
344+
345+/// Set function attributes of function \p F based on CPU, Features, and command
346+/// line flags.
347+void setFunctionAttributes(StringRef CPU, StringRef Features, Function &F);
348+
349+/// Set function attributes of functions in Module M based on CPU,
350+/// Features, and command line flags.
351+void setFunctionAttributes(StringRef CPU, StringRef Features, Module &M);
352+} // namespace codegen
353+} // namespace llvm
354diff --git a/llvm/include/llvm/MC/MCTargetOptionsCommandFlags.h b/llvm/include/llvm/MC/MCTargetOptionsCommandFlags.h
355new file mode 100644
356index 000000000000..7a5edf78fdcc
357--- /dev/null
358+++ b/llvm/include/llvm/MC/MCTargetOptionsCommandFlags.h
359@@ -0,0 +1,54 @@
360+//===-- MCTargetOptionsCommandFlags.h --------------------------*- C++ -*-===//
361+//
362+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
363+// See https://llvm.org/LICENSE.txt for license information.
364+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
365+//
366+//===----------------------------------------------------------------------===//
367+//
368+// This file contains machine code-specific flags that are shared between
369+// different command line tools.
370+//
371+//===----------------------------------------------------------------------===//
372+
373+#ifndef LLVM_MC_MCTARGETOPTIONSCOMMANDFLAGS_H
374+#define LLVM_MC_MCTARGETOPTIONSCOMMANDFLAGS_H
375+
376+#include "llvm/ADT/Optional.h"
377+#include "llvm/MC/MCTargetOptions.h"
378+#include "llvm/Support/CommandLine.h"
379+
380+namespace llvm {
381+
382+namespace mc {
383+
384+bool getRelaxAll();
385+Optional<bool> getExplicitRelaxAll();
386+
387+bool getIncrementalLinkerCompatible();
388+
389+int getDwarfVersion();
390+
391+bool getShowMCInst();
392+
393+bool getFatalWarnings();
394+
395+bool getNoWarn();
396+
397+bool getNoDeprecatedWarn();
398+
399+std::string getABIName();
400+
401+/// Create this object with static storage to register mc-related command
402+/// line options.
403+struct RegisterMCTargetOptionsFlags {
404+ RegisterMCTargetOptionsFlags();
405+};
406+
407+MCTargetOptions InitMCTargetOptionsFromFlags();
408+
409+} // namespace mc
410+
411+} // namespace llvm
412+
413+#endif
414diff --git a/llvm/include/llvm/MC/MCTargetOptionsCommandFlags.inc b/llvm/include/llvm/MC/MCTargetOptionsCommandFlags.inc
415deleted file mode 100644
416index 93e21b626eac..000000000000
417--- a/llvm/include/llvm/MC/MCTargetOptionsCommandFlags.inc
418+++ /dev/null
419@@ -1,65 +0,0 @@
420-//===-- MCTargetOptionsCommandFlags.h --------------------------*- C++ -*-===//
421-//
422-// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
423-// See https://llvm.org/LICENSE.txt for license information.
424-// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
425-//
426-//===----------------------------------------------------------------------===//
427-//
428-// This file contains machine code-specific flags that are shared between
429-// different command line tools.
430-//
431-//===----------------------------------------------------------------------===//
432-
433-#ifndef LLVM_MC_MCTARGETOPTIONSCOMMANDFLAGS_H
434-#define LLVM_MC_MCTARGETOPTIONSCOMMANDFLAGS_H
435-
436-#include "llvm/MC/MCTargetOptions.h"
437-#include "llvm/Support/CommandLine.h"
438-using namespace llvm;
439-
440-static cl::opt<bool> RelaxAll("mc-relax-all",
441- cl::desc("When used with filetype=obj, "
442- "relax all fixups in the emitted object file"));
443-
444-static cl::opt<bool> IncrementalLinkerCompatible(
445- "incremental-linker-compatible",
446- cl::desc(
447- "When used with filetype=obj, "
448- "emit an object file which can be used with an incremental linker"));
449-
450-static cl::opt<int> DwarfVersion("dwarf-version", cl::desc("Dwarf version"),
451- cl::init(0));
452-
453-static cl::opt<bool> ShowMCInst("asm-show-inst",
454- cl::desc("Emit internal instruction representation to "
455- "assembly file"));
456-
457-static cl::opt<bool> FatalWarnings("fatal-warnings",
458- cl::desc("Treat warnings as errors"));
459-
460-static cl::opt<bool> NoWarn("no-warn", cl::desc("Suppress all warnings"));
461-static cl::alias NoWarnW("W", cl::desc("Alias for --no-warn"), cl::aliasopt(NoWarn));
462-
463-static cl::opt<bool> NoDeprecatedWarn("no-deprecated-warn",
464- cl::desc("Suppress all deprecated warnings"));
465-
466-static cl::opt<std::string>
467-ABIName("target-abi", cl::Hidden,
468- cl::desc("The name of the ABI to be targeted from the backend."),
469- cl::init(""));
470-
471-static MCTargetOptions InitMCTargetOptionsFromFlags() {
472- MCTargetOptions Options;
473- Options.MCRelaxAll = RelaxAll;
474- Options.MCIncrementalLinkerCompatible = IncrementalLinkerCompatible;
475- Options.DwarfVersion = DwarfVersion;
476- Options.ShowMCInst = ShowMCInst;
477- Options.ABIName = ABIName;
478- Options.MCFatalWarnings = FatalWarnings;
479- Options.MCNoWarn = NoWarn;
480- Options.MCNoDeprecatedWarn = NoDeprecatedWarn;
481- return Options;
482-}
483-
484-#endif
485diff --git a/llvm/include/llvm/module.modulemap b/llvm/include/llvm/module.modulemap
486index d281682ae003..d176b3dfd4be 100644
487--- a/llvm/include/llvm/module.modulemap
488+++ b/llvm/include/llvm/module.modulemap
489@@ -29,7 +29,6 @@ module LLVM_Backend {
490 exclude header "CodeGen/LinkAllCodegenComponents.h"
491
492 // These are intended for (repeated) textual inclusion.
493- textual header "CodeGen/CommandFlags.inc"
494 textual header "CodeGen/DIEValue.def"
495 }
496 }
497@@ -308,8 +307,6 @@ module LLVM_MC {
498
499 umbrella "MC"
500 module * { export * }
501-
502- textual header "MC/MCTargetOptionsCommandFlags.inc"
503 }
504
505 // Used by llvm-tblgen
506diff --git a/llvm/lib/CodeGen/CMakeLists.txt b/llvm/lib/CodeGen/CMakeLists.txt
507index a3916b7c6242..c6be91247017 100644
508--- a/llvm/lib/CodeGen/CMakeLists.txt
509+++ b/llvm/lib/CodeGen/CMakeLists.txt
510@@ -14,6 +14,7 @@ add_llvm_component_library(LLVMCodeGen
511 CFIInstrInserter.cpp
512 CodeGen.cpp
513 CodeGenPrepare.cpp
514+ CommandFlags.cpp
515 CriticalAntiDepBreaker.cpp
516 DeadMachineInstructionElim.cpp
517 DetectDeadLanes.cpp
518diff --git a/llvm/lib/CodeGen/CommandFlags.cpp b/llvm/lib/CodeGen/CommandFlags.cpp
519new file mode 100644
520index 000000000000..7acb84df582f
521--- /dev/null
522+++ b/llvm/lib/CodeGen/CommandFlags.cpp
523@@ -0,0 +1,588 @@
524+//===-- CommandFlags.cpp - Command Line Flags Interface ---------*- C++ -*-===//
525+//
526+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
527+// See https://llvm.org/LICENSE.txt for license information.
528+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
529+//
530+//===----------------------------------------------------------------------===//
531+//
532+// This file contains codegen-specific flags that are shared between different
533+// command line tools. The tools "llc" and "opt" both use this file to prevent
534+// flag duplication.
535+//
536+//===----------------------------------------------------------------------===//
537+
538+#include "llvm/CodeGen/CommandFlags.h"
539+
540+using namespace llvm;
541+
542+#define CGOPT(TY, NAME) \
543+ static cl::opt<TY> *NAME##View; \
544+ TY codegen::get##NAME() { \
545+ assert(NAME##View && "RegisterCodeGenFlags not created."); \
546+ return *NAME##View; \
547+ }
548+
549+#define CGLIST(TY, NAME) \
550+ static cl::list<TY> *NAME##View; \
551+ std::vector<TY> codegen::get##NAME() { \
552+ assert(NAME##View && "RegisterCodeGenFlags not created."); \
553+ return *NAME##View; \
554+ }
555+
556+#define CGOPT_EXP(TY, NAME) \
557+ CGOPT(TY, NAME) \
558+ Optional<TY> codegen::getExplicit##NAME() { \
559+ if (NAME##View->getNumOccurrences()) { \
560+ TY res = *NAME##View; \
561+ return res; \
562+ } \
563+ return None; \
564+ }
565+
566+CGOPT(std::string, MArch)
567+CGOPT(std::string, MCPU)
568+CGLIST(std::string, MAttrs)
569+CGOPT_EXP(Reloc::Model, RelocModel)
570+CGOPT(ThreadModel::Model, ThreadModel)
571+CGOPT_EXP(CodeModel::Model, CodeModel)
572+CGOPT(ExceptionHandling, ExceptionModel)
573+CGOPT_EXP(CodeGenFileType, FileType)
574+CGOPT(FramePointer::FP, FramePointerUsage)
575+CGOPT(bool, EnableUnsafeFPMath)
576+CGOPT(bool, EnableNoInfsFPMath)
577+CGOPT(bool, EnableNoNaNsFPMath)
578+CGOPT(bool, EnableNoSignedZerosFPMath)
579+CGOPT(bool, EnableNoTrappingFPMath)
580+CGOPT(FPDenormal::DenormalMode, DenormalFPMath)
581+CGOPT(bool, EnableHonorSignDependentRoundingFPMath)
582+CGOPT(FloatABI::ABIType, FloatABIForCalls)
583+CGOPT(FPOpFusion::FPOpFusionMode, FuseFPOps)
584+CGOPT(bool, DontPlaceZerosInBSS)
585+CGOPT(bool, EnableGuaranteedTailCallOpt)
586+CGOPT(bool, DisableTailCalls)
587+CGOPT(bool, StackSymbolOrdering)
588+CGOPT(unsigned, OverrideStackAlignment)
589+CGOPT(bool, StackRealign)
590+CGOPT(std::string, TrapFuncName)
591+CGOPT(bool, UseCtors)
592+CGOPT(bool, RelaxELFRelocations)
593+CGOPT_EXP(bool, DataSections)
594+CGOPT_EXP(bool, FunctionSections)
595+CGOPT(std::string, BBSections)
596+CGOPT(unsigned, TLSSize)
597+CGOPT(bool, EmulatedTLS)
598+CGOPT(bool, UniqueSectionNames)
599+CGOPT(bool, UniqueBBSectionNames)
600+CGOPT(EABI, EABIVersion)
601+CGOPT(DebuggerKind, DebuggerTuningOpt)
602+CGOPT(bool, EnableStackSizeSection)
603+CGOPT(bool, EnableAddrsig)
604+CGOPT(bool, EmitCallSiteInfo)
605+CGOPT(bool, EnableDebugEntryValues)
606+CGOPT(bool, ForceDwarfFrameSection)
607+
608+codegen::RegisterCodeGenFlags::RegisterCodeGenFlags() {
609+#define CGBINDOPT(NAME) \
610+ do { \
611+ NAME##View = std::addressof(NAME); \
612+ } while (0)
613+
614+ static cl::opt<std::string> MArch(
615+ "march", cl::desc("Architecture to generate code for (see --version)"));
616+ CGBINDOPT(MArch);
617+
618+ static cl::opt<std::string> MCPU(
619+ "mcpu", cl::desc("Target a specific cpu type (-mcpu=help for details)"),
620+ cl::value_desc("cpu-name"), cl::init(""));
621+ CGBINDOPT(MCPU);
622+
623+ static cl::list<std::string> MAttrs(
624+ "mattr", cl::CommaSeparated,
625+ cl::desc("Target specific attributes (-mattr=help for details)"),
626+ cl::value_desc("a1,+a2,-a3,..."));
627+ CGBINDOPT(MAttrs);
628+
629+ static cl::opt<Reloc::Model> RelocModel(
630+ "relocation-model", cl::desc("Choose relocation model"),
631+ cl::values(
632+ clEnumValN(Reloc::Static, "static", "Non-relocatable code"),
633+ clEnumValN(Reloc::PIC_, "pic",
634+ "Fully relocatable, position independent code"),
635+ clEnumValN(Reloc::DynamicNoPIC, "dynamic-no-pic",
636+ "Relocatable external references, non-relocatable code"),
637+ clEnumValN(
638+ Reloc::ROPI, "ropi",
639+ "Code and read-only data relocatable, accessed PC-relative"),
640+ clEnumValN(
641+ Reloc::RWPI, "rwpi",
642+ "Read-write data relocatable, accessed relative to static base"),
643+ clEnumValN(Reloc::ROPI_RWPI, "ropi-rwpi",
644+ "Combination of ropi and rwpi")));
645+ CGBINDOPT(RelocModel);
646+
647+ static cl::opt<ThreadModel::Model> ThreadModel(
648+ "thread-model", cl::desc("Choose threading model"),
649+ cl::init(ThreadModel::POSIX),
650+ cl::values(
651+ clEnumValN(ThreadModel::POSIX, "posix", "POSIX thread model"),
652+ clEnumValN(ThreadModel::Single, "single", "Single thread model")));
653+ CGBINDOPT(ThreadModel);
654+
655+ static cl::opt<CodeModel::Model> CodeModel(
656+ "code-model", cl::desc("Choose code model"),
657+ cl::values(clEnumValN(CodeModel::Tiny, "tiny", "Tiny code model"),
658+ clEnumValN(CodeModel::Small, "small", "Small code model"),
659+ clEnumValN(CodeModel::Kernel, "kernel", "Kernel code model"),
660+ clEnumValN(CodeModel::Medium, "medium", "Medium code model"),
661+ clEnumValN(CodeModel::Large, "large", "Large code model")));
662+ CGBINDOPT(CodeModel);
663+
664+ static cl::opt<ExceptionHandling> ExceptionModel(
665+ "exception-model", cl::desc("exception model"),
666+ cl::init(ExceptionHandling::None),
667+ cl::values(
668+ clEnumValN(ExceptionHandling::None, "default",
669+ "default exception handling model"),
670+ clEnumValN(ExceptionHandling::DwarfCFI, "dwarf",
671+ "DWARF-like CFI based exception handling"),
672+ clEnumValN(ExceptionHandling::SjLj, "sjlj",
673+ "SjLj exception handling"),
674+ clEnumValN(ExceptionHandling::ARM, "arm", "ARM EHABI exceptions"),
675+ clEnumValN(ExceptionHandling::WinEH, "wineh",
676+ "Windows exception model"),
677+ clEnumValN(ExceptionHandling::Wasm, "wasm",
678+ "WebAssembly exception handling")));
679+ CGBINDOPT(ExceptionModel);
680+
681+ static cl::opt<CodeGenFileType> FileType(
682+ "filetype", cl::init(CGFT_AssemblyFile),
683+ cl::desc(
684+ "Choose a file type (not all types are supported by all targets):"),
685+ cl::values(
686+ clEnumValN(CGFT_AssemblyFile, "asm", "Emit an assembly ('.s') file"),
687+ clEnumValN(CGFT_ObjectFile, "obj",
688+ "Emit a native object ('.o') file"),
689+ clEnumValN(CGFT_Null, "null",
690+ "Emit nothing, for performance testing")));
691+ CGBINDOPT(FileType);
692+
693+ static cl::opt<FramePointer::FP> FramePointerUsage(
694+ "frame-pointer",
695+ cl::desc("Specify frame pointer elimination optimization"),
696+ cl::init(FramePointer::None),
697+ cl::values(
698+ clEnumValN(FramePointer::All, "all",
699+ "Disable frame pointer elimination"),
700+ clEnumValN(FramePointer::NonLeaf, "non-leaf",
701+ "Disable frame pointer elimination for non-leaf frame"),
702+ clEnumValN(FramePointer::None, "none",
703+ "Enable frame pointer elimination")));
704+ CGBINDOPT(FramePointerUsage);
705+
706+ static cl::opt<bool> EnableUnsafeFPMath(
707+ "enable-unsafe-fp-math",
708+ cl::desc("Enable optimizations that may decrease FP precision"),
709+ cl::init(false));
710+ CGBINDOPT(EnableUnsafeFPMath);
711+
712+ static cl::opt<bool> EnableNoInfsFPMath(
713+ "enable-no-infs-fp-math",
714+ cl::desc("Enable FP math optimizations that assume no +-Infs"),
715+ cl::init(false));
716+ CGBINDOPT(EnableNoInfsFPMath);
717+
718+ static cl::opt<bool> EnableNoNaNsFPMath(
719+ "enable-no-nans-fp-math",
720+ cl::desc("Enable FP math optimizations that assume no NaNs"),
721+ cl::init(false));
722+ CGBINDOPT(EnableNoNaNsFPMath);
723+
724+ static cl::opt<bool> EnableNoSignedZerosFPMath(
725+ "enable-no-signed-zeros-fp-math",
726+ cl::desc("Enable FP math optimizations that assume "
727+ "the sign of 0 is insignificant"),
728+ cl::init(false));
729+ CGBINDOPT(EnableNoSignedZerosFPMath);
730+
731+ static cl::opt<bool> EnableNoTrappingFPMath(
732+ "enable-no-trapping-fp-math",
733+ cl::desc("Enable setting the FP exceptions build "
734+ "attribute not to use exceptions"),
735+ cl::init(false));
736+ CGBINDOPT(EnableNoTrappingFPMath);
737+
738+ static cl::opt<FPDenormal::DenormalMode> DenormalFPMath(
739+ "denormal-fp-math",
740+ cl::desc(
741+ "Select which denormal numbers the code is permitted to require"),
742+ cl::init(FPDenormal::IEEE),
743+ cl::values(
744+ clEnumValN(FPDenormal::IEEE, "ieee", "IEEE 754 denormal numbers"),
745+ clEnumValN(FPDenormal::PreserveSign, "preserve-sign",
746+ "the sign of a flushed-to-zero number is preserved "
747+ "in the sign of 0"),
748+ clEnumValN(FPDenormal::PositiveZero, "positive-zero",
749+ "denormals are flushed to positive zero")));
750+ CGBINDOPT(DenormalFPMath);
751+
752+ static cl::opt<bool> EnableHonorSignDependentRoundingFPMath(
753+ "enable-sign-dependent-rounding-fp-math", cl::Hidden,
754+ cl::desc("Force codegen to assume rounding mode can change dynamically"),
755+ cl::init(false));
756+ CGBINDOPT(EnableHonorSignDependentRoundingFPMath);
757+
758+ static cl::opt<FloatABI::ABIType> FloatABIForCalls(
759+ "float-abi", cl::desc("Choose float ABI type"),
760+ cl::init(FloatABI::Default),
761+ cl::values(clEnumValN(FloatABI::Default, "default",
762+ "Target default float ABI type"),
763+ clEnumValN(FloatABI::Soft, "soft",
764+ "Soft float ABI (implied by -soft-float)"),
765+ clEnumValN(FloatABI::Hard, "hard",
766+ "Hard float ABI (uses FP registers)")));
767+ CGBINDOPT(FloatABIForCalls);
768+
769+ static cl::opt<FPOpFusion::FPOpFusionMode> FuseFPOps(
770+ "fp-contract", cl::desc("Enable aggressive formation of fused FP ops"),
771+ cl::init(FPOpFusion::Standard),
772+ cl::values(
773+ clEnumValN(FPOpFusion::Fast, "fast",
774+ "Fuse FP ops whenever profitable"),
775+ clEnumValN(FPOpFusion::Standard, "on", "Only fuse 'blessed' FP ops."),
776+ clEnumValN(FPOpFusion::Strict, "off",
777+ "Only fuse FP ops when the result won't be affected.")));
778+ CGBINDOPT(FuseFPOps);
779+
780+ static cl::opt<bool> DontPlaceZerosInBSS(
781+ "nozero-initialized-in-bss",
782+ cl::desc("Don't place zero-initialized symbols into bss section"),
783+ cl::init(false));
784+ CGBINDOPT(DontPlaceZerosInBSS);
785+
786+ static cl::opt<bool> EnableGuaranteedTailCallOpt(
787+ "tailcallopt",
788+ cl::desc(
789+ "Turn fastcc calls into tail calls by (potentially) changing ABI."),
790+ cl::init(false));
791+ CGBINDOPT(EnableGuaranteedTailCallOpt);
792+
793+ static cl::opt<bool> DisableTailCalls(
794+ "disable-tail-calls", cl::desc("Never emit tail calls"), cl::init(false));
795+ CGBINDOPT(DisableTailCalls);
796+
797+ static cl::opt<bool> StackSymbolOrdering(
798+ "stack-symbol-ordering", cl::desc("Order local stack symbols."),
799+ cl::init(true));
800+ CGBINDOPT(StackSymbolOrdering);
801+
802+ static cl::opt<unsigned> OverrideStackAlignment(
803+ "stack-alignment", cl::desc("Override default stack alignment"),
804+ cl::init(0));
805+ CGBINDOPT(OverrideStackAlignment);
806+
807+ static cl::opt<bool> StackRealign(
808+ "stackrealign",
809+ cl::desc("Force align the stack to the minimum alignment"),
810+ cl::init(false));
811+ CGBINDOPT(StackRealign);
812+
813+ static cl::opt<std::string> TrapFuncName(
814+ "trap-func", cl::Hidden,
815+ cl::desc("Emit a call to trap function rather than a trap instruction"),
816+ cl::init(""));
817+ CGBINDOPT(TrapFuncName);
818+
819+ static cl::opt<bool> UseCtors("use-ctors",
820+ cl::desc("Use .ctors instead of .init_array."),
821+ cl::init(false));
822+ CGBINDOPT(UseCtors);
823+
824+ static cl::opt<bool> RelaxELFRelocations(
825+ "relax-elf-relocations",
826+ cl::desc(
827+ "Emit GOTPCRELX/REX_GOTPCRELX instead of GOTPCREL on x86-64 ELF"),
828+ cl::init(false));
829+ CGBINDOPT(RelaxELFRelocations);
830+
831+ static cl::opt<bool> DataSections(
832+ "data-sections", cl::desc("Emit data into separate sections"),
833+ cl::init(false));
834+ CGBINDOPT(DataSections);
835+
836+ static cl::opt<bool> FunctionSections(
837+ "function-sections", cl::desc("Emit functions into separate sections"),
838+ cl::init(false));
839+ CGBINDOPT(FunctionSections);
840+
841+ static cl::opt<std::string> BBSections(
842+ "basicblock-sections",
843+ cl::desc("Emit basic blocks into separate sections"),
844+ cl::value_desc("all | <function list (file)> | labels | none"),
845+ cl::init("none"));
846+ CGBINDOPT(BBSections);
847+
848+ static cl::opt<unsigned> TLSSize(
849+ "tls-size", cl::desc("Bit size of immediate TLS offsets"), cl::init(0));
850+ CGBINDOPT(TLSSize);
851+
852+ static cl::opt<bool> EmulatedTLS(
853+ "emulated-tls", cl::desc("Use emulated TLS model"), cl::init(false));
854+ CGBINDOPT(EmulatedTLS);
855+
856+ static cl::opt<bool> UniqueSectionNames(
857+ "unique-section-names", cl::desc("Give unique names to every section"),
858+ cl::init(true));
859+ CGBINDOPT(UniqueSectionNames);
860+
861+ static cl::opt<bool> UniqueBBSectionNames(
862+ "unique-bb-section-names",
863+ cl::desc("Give unique names to every basic block section"),
864+ cl::init(false));
865+ CGBINDOPT(UniqueBBSectionNames);
866+
867+ static cl::opt<EABI> EABIVersion(
868+ "meabi", cl::desc("Set EABI type (default depends on triple):"),
869+ cl::init(EABI::Default),
870+ cl::values(
871+ clEnumValN(EABI::Default, "default", "Triple default EABI version"),
872+ clEnumValN(EABI::EABI4, "4", "EABI version 4"),
873+ clEnumValN(EABI::EABI5, "5", "EABI version 5"),
874+ clEnumValN(EABI::GNU, "gnu", "EABI GNU")));
875+ CGBINDOPT(EABIVersion);
876+
877+ static cl::opt<DebuggerKind> DebuggerTuningOpt(
878+ "debugger-tune", cl::desc("Tune debug info for a particular debugger"),
879+ cl::init(DebuggerKind::Default),
880+ cl::values(
881+ clEnumValN(DebuggerKind::GDB, "gdb", "gdb"),
882+ clEnumValN(DebuggerKind::LLDB, "lldb", "lldb"),
883+ clEnumValN(DebuggerKind::SCE, "sce", "SCE targets (e.g. PS4)")));
884+ CGBINDOPT(DebuggerTuningOpt);
885+
886+ static cl::opt<bool> EnableStackSizeSection(
887+ "stack-size-section",
888+ cl::desc("Emit a section containing stack size metadata"),
889+ cl::init(false));
890+ CGBINDOPT(EnableStackSizeSection);
891+
892+ static cl::opt<bool> EnableAddrsig(
893+ "addrsig", cl::desc("Emit an address-significance table"),
894+ cl::init(false));
895+ CGBINDOPT(EnableAddrsig);
896+
897+ static cl::opt<bool> EmitCallSiteInfo(
898+ "emit-call-site-info",
899+ cl::desc(
900+ "Emit call site debug information, if debug information is enabled."),
901+ cl::init(false));
902+ CGBINDOPT(EmitCallSiteInfo);
903+
904+ static cl::opt<bool> EnableDebugEntryValues(
905+ "debug-entry-values",
906+ cl::desc("Emit debug info about parameter's entry values"),
907+ cl::init(false));
908+ CGBINDOPT(EnableDebugEntryValues);
909+
910+ static cl::opt<bool> ForceDwarfFrameSection(
911+ "force-dwarf-frame-section",
912+ cl::desc("Always emit a debug frame section."), cl::init(false));
913+ CGBINDOPT(ForceDwarfFrameSection);
914+
915+#undef CGBINDOPT
916+
917+ mc::RegisterMCTargetOptionsFlags();
918+}
919+
920+llvm::BasicBlockSection
921+codegen::getBBSectionsMode(llvm::TargetOptions &Options) {
922+ if (getBBSections() == "all")
923+ return BasicBlockSection::All;
924+ else if (getBBSections() == "labels")
925+ return BasicBlockSection::Labels;
926+ else if (getBBSections() == "none")
927+ return BasicBlockSection::None;
928+ else {
929+ ErrorOr<std::unique_ptr<MemoryBuffer>> MBOrErr =
930+ MemoryBuffer::getFile(getBBSections());
931+ if (!MBOrErr) {
932+ errs() << "Error loading basic block sections function list file: "
933+ << MBOrErr.getError().message() << "\n";
934+ } else {
935+ Options.BBSectionsFuncListBuf = std::move(*MBOrErr);
936+ }
937+ return BasicBlockSection::List;
938+ }
939+}
940+
941+// Common utility function tightly tied to the options listed here. Initializes
942+// a TargetOptions object with CodeGen flags and returns it.
943+TargetOptions codegen::InitTargetOptionsFromCodeGenFlags() {
944+ TargetOptions Options;
945+ Options.AllowFPOpFusion = getFuseFPOps();
946+ Options.UnsafeFPMath = getEnableUnsafeFPMath();
947+ Options.NoInfsFPMath = getEnableNoInfsFPMath();
948+ Options.NoNaNsFPMath = getEnableNoNaNsFPMath();
949+ Options.NoSignedZerosFPMath = getEnableNoSignedZerosFPMath();
950+ Options.NoTrappingFPMath = getEnableNoTrappingFPMath();
951+ Options.FPDenormalMode = getDenormalFPMath();
952+ Options.HonorSignDependentRoundingFPMathOption =
953+ getEnableHonorSignDependentRoundingFPMath();
954+ if (getFloatABIForCalls() != FloatABI::Default)
955+ Options.FloatABIType = getFloatABIForCalls();
956+ Options.NoZerosInBSS = getDontPlaceZerosInBSS();
957+ Options.GuaranteedTailCallOpt = getEnableGuaranteedTailCallOpt();
958+ Options.StackAlignmentOverride = getOverrideStackAlignment();
959+ Options.StackSymbolOrdering = getStackSymbolOrdering();
960+ Options.UseInitArray = !getUseCtors();
961+ Options.RelaxELFRelocations = getRelaxELFRelocations();
962+ Options.DataSections = getDataSections();
963+ Options.FunctionSections = getFunctionSections();
964+ Options.BBSections = getBBSectionsMode(Options);
965+ Options.UniqueSectionNames = getUniqueSectionNames();
966+ Options.UniqueBBSectionNames = getUniqueBBSectionNames();
967+ Options.TLSSize = getTLSSize();
968+ Options.EmulatedTLS = getEmulatedTLS();
969+ Options.ExplicitEmulatedTLS = EmulatedTLSView->getNumOccurrences() > 0;
970+ Options.ExceptionModel = getExceptionModel();
971+ Options.EmitStackSizeSection = getEnableStackSizeSection();
972+ Options.EmitAddrsig = getEnableAddrsig();
973+ Options.EmitCallSiteInfo = getEmitCallSiteInfo();
974+ Options.EnableDebugEntryValues = getEnableDebugEntryValues();
975+ Options.ForceDwarfFrameSection = getForceDwarfFrameSection();
976+
977+ Options.MCOptions = mc::InitMCTargetOptionsFromFlags();
978+
979+ Options.ThreadModel = getThreadModel();
980+ Options.EABIVersion = getEABIVersion();
981+ Options.DebuggerTuning = getDebuggerTuningOpt();
982+
983+ return Options;
984+}
985+
986+std::string codegen::getCPUStr() {
987+ // If user asked for the 'native' CPU, autodetect here. If autodection fails,
988+ // this will set the CPU to an empty string which tells the target to
989+ // pick a basic default.
990+ if (getMCPU() == "native")
991+ return std::string(sys::getHostCPUName());
992+
993+ return getMCPU();
994+}
995+
996+std::string codegen::getFeaturesStr() {
997+ SubtargetFeatures Features;
998+
999+ // If user asked for the 'native' CPU, we need to autodetect features.
1000+ // This is necessary for x86 where the CPU might not support all the
1001+ // features the autodetected CPU name lists in the target. For example,
1002+ // not all Sandybridge processors support AVX.
1003+ if (getMCPU() == "native") {
1004+ StringMap<bool> HostFeatures;
1005+ if (sys::getHostCPUFeatures(HostFeatures))
1006+ for (auto &F : HostFeatures)
1007+ Features.AddFeature(F.first(), F.second);
1008+ }
1009+
1010+ for (auto const &MAttr : getMAttrs())
1011+ Features.AddFeature(MAttr);
1012+
1013+ return Features.getString();
1014+}
1015+
1016+std::vector<std::string> codegen::getFeatureList() {
1017+ SubtargetFeatures Features;
1018+
1019+ // If user asked for the 'native' CPU, we need to autodetect features.
1020+ // This is necessary for x86 where the CPU might not support all the
1021+ // features the autodetected CPU name lists in the target. For example,
1022+ // not all Sandybridge processors support AVX.
1023+ if (getMCPU() == "native") {
1024+ StringMap<bool> HostFeatures;
1025+ if (sys::getHostCPUFeatures(HostFeatures))
1026+ for (auto &F : HostFeatures)
1027+ Features.AddFeature(F.first(), F.second);
1028+ }
1029+
1030+ for (auto const &MAttr : getMAttrs())
1031+ Features.AddFeature(MAttr);
1032+
1033+ return Features.getFeatures();
1034+}
1035+
1036+void codegen::renderBoolStringAttr(AttrBuilder &B, StringRef Name, bool Val) {
1037+ B.addAttribute(Name, Val ? "true" : "false");
1038+}
1039+
1040+#define HANDLE_BOOL_ATTR(CL, AttrName) \
1041+ do { \
1042+ if (CL->getNumOccurrences() > 0 && !F.hasFnAttribute(AttrName)) \
1043+ renderBoolStringAttr(NewAttrs, AttrName, *CL); \
1044+ } while (0)
1045+
1046+/// Set function attributes of function \p F based on CPU, Features, and command
1047+/// line flags.
1048+void codegen::setFunctionAttributes(StringRef CPU, StringRef Features,
1049+ Function &F) {
1050+ auto &Ctx = F.getContext();
1051+ AttributeList Attrs = F.getAttributes();
1052+ AttrBuilder NewAttrs;
1053+
1054+ if (!CPU.empty() && !F.hasFnAttribute("target-cpu"))
1055+ NewAttrs.addAttribute("target-cpu", CPU);
1056+ if (!Features.empty()) {
1057+ // Append the command line features to any that are already on the function.
1058+ StringRef OldFeatures =
1059+ F.getFnAttribute("target-features").getValueAsString();
1060+ if (OldFeatures.empty())
1061+ NewAttrs.addAttribute("target-features", Features);
1062+ else {
1063+ SmallString<256> Appended(OldFeatures);
1064+ Appended.push_back(',');
1065+ Appended.append(Features);
1066+ NewAttrs.addAttribute("target-features", Appended);
1067+ }
1068+ }
1069+ if (FramePointerUsageView->getNumOccurrences() > 0 &&
1070+ !F.hasFnAttribute("frame-pointer")) {
1071+ if (getFramePointerUsage() == FramePointer::All)
1072+ NewAttrs.addAttribute("frame-pointer", "all");
1073+ else if (getFramePointerUsage() == FramePointer::NonLeaf)
1074+ NewAttrs.addAttribute("frame-pointer", "non-leaf");
1075+ else if (getFramePointerUsage() == FramePointer::None)
1076+ NewAttrs.addAttribute("frame-pointer", "none");
1077+ }
1078+ if (DisableTailCallsView->getNumOccurrences() > 0)
1079+ NewAttrs.addAttribute("disable-tail-calls",
1080+ toStringRef(getDisableTailCalls()));
1081+ if (getStackRealign())
1082+ NewAttrs.addAttribute("stackrealign");
1083+
1084+ HANDLE_BOOL_ATTR(EnableUnsafeFPMathView, "unsafe-fp-math");
1085+ HANDLE_BOOL_ATTR(EnableNoInfsFPMathView, "no-infs-fp-math");
1086+ HANDLE_BOOL_ATTR(EnableNoNaNsFPMathView, "no-nans-fp-math");
1087+ HANDLE_BOOL_ATTR(EnableNoSignedZerosFPMathView, "no-signed-zeros-fp-math");
1088+
1089+ if (TrapFuncNameView->getNumOccurrences() > 0)
1090+ for (auto &B : F)
1091+ for (auto &I : B)
1092+ if (auto *Call = dyn_cast<CallInst>(&I))
1093+ if (const auto *F = Call->getCalledFunction())
1094+ if (F->getIntrinsicID() == Intrinsic::debugtrap ||
1095+ F->getIntrinsicID() == Intrinsic::trap)
1096+ Call->addAttribute(
1097+ AttributeList::FunctionIndex,
1098+ Attribute::get(Ctx, "trap-func-name", getTrapFuncName()));
1099+
1100+ // Let NewAttrs override Attrs.
1101+ F.setAttributes(
1102+ Attrs.addAttributes(Ctx, AttributeList::FunctionIndex, NewAttrs));
1103+}
1104+
1105+/// Set function attributes of functions in Module M based on CPU,
1106+/// Features, and command line flags.
1107+void codegen::setFunctionAttributes(StringRef CPU, StringRef Features,
1108+ Module &M) {
1109+ for (Function &F : M)
1110+ setFunctionAttributes(CPU, Features, F);
1111+}
1112diff --git a/llvm/lib/MC/CMakeLists.txt b/llvm/lib/MC/CMakeLists.txt
1113index de2e47d8d9b2..ab809daf5273 100644
1114--- a/llvm/lib/MC/CMakeLists.txt
1115+++ b/llvm/lib/MC/CMakeLists.txt
1116@@ -44,6 +44,7 @@ add_llvm_component_library(LLVMMC
1117 MCSymbol.cpp
1118 MCSymbolELF.cpp
1119 MCTargetOptions.cpp
1120+ MCTargetOptionsCommandFlags.cpp
1121 MCValue.cpp
1122 MCWasmObjectTargetWriter.cpp
1123 MCWasmStreamer.cpp
1124diff --git a/llvm/lib/MC/MCTargetOptionsCommandFlags.cpp b/llvm/lib/MC/MCTargetOptionsCommandFlags.cpp
1125new file mode 100644
1126index 000000000000..3ca34061241a
1127--- /dev/null
1128+++ b/llvm/lib/MC/MCTargetOptionsCommandFlags.cpp
1129@@ -0,0 +1,105 @@
1130+//===-- MCTargetOptionsCommandFlags.cpp --------------------------*- C++
1131+//-*-===//
1132+//
1133+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
1134+// See https://llvm.org/LICENSE.txt for license information.
1135+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
1136+//
1137+//===----------------------------------------------------------------------===//
1138+//
1139+// This file contains machine code-specific flags that are shared between
1140+// different command line tools.
1141+//
1142+//===----------------------------------------------------------------------===//
1143+
1144+#include "llvm/MC/MCTargetOptionsCommandFlags.h"
1145+
1146+using namespace llvm;
1147+
1148+#define MCOPT(TY, NAME) \
1149+ static cl::opt<TY> *NAME##View; \
1150+ TY llvm::mc::get##NAME() { \
1151+ assert(NAME##View && "RegisterMCTargetOptionsFlags not created."); \
1152+ return *NAME##View; \
1153+ }
1154+
1155+#define MCOPT_EXP(TY, NAME) \
1156+ MCOPT(TY, NAME) \
1157+ Optional<TY> llvm::mc::getExplicit##NAME() { \
1158+ if (NAME##View->getNumOccurrences()) { \
1159+ TY res = *NAME##View; \
1160+ return res; \
1161+ } \
1162+ return None; \
1163+ }
1164+
1165+MCOPT_EXP(bool, RelaxAll)
1166+MCOPT(bool, IncrementalLinkerCompatible)
1167+MCOPT(int, DwarfVersion)
1168+MCOPT(bool, ShowMCInst)
1169+MCOPT(bool, FatalWarnings)
1170+MCOPT(bool, NoWarn)
1171+MCOPT(bool, NoDeprecatedWarn)
1172+MCOPT(std::string, ABIName)
1173+
1174+llvm::mc::RegisterMCTargetOptionsFlags::RegisterMCTargetOptionsFlags() {
1175+#define MCBINDOPT(NAME) \
1176+ do { \
1177+ NAME##View = std::addressof(NAME); \
1178+ } while (0)
1179+
1180+ static cl::opt<bool> RelaxAll(
1181+ "mc-relax-all", cl::desc("When used with filetype=obj, relax all fixups "
1182+ "in the emitted object file"));
1183+ MCBINDOPT(RelaxAll);
1184+
1185+ static cl::opt<bool> IncrementalLinkerCompatible(
1186+ "incremental-linker-compatible",
1187+ cl::desc(
1188+ "When used with filetype=obj, "
1189+ "emit an object file which can be used with an incremental linker"));
1190+ MCBINDOPT(IncrementalLinkerCompatible);
1191+
1192+ static cl::opt<int> DwarfVersion("dwarf-version", cl::desc("Dwarf version"),
1193+ cl::init(0));
1194+ MCBINDOPT(DwarfVersion);
1195+
1196+ static cl::opt<bool> ShowMCInst(
1197+ "asm-show-inst",
1198+ cl::desc("Emit internal instruction representation to assembly file"));
1199+ MCBINDOPT(ShowMCInst);
1200+
1201+ static cl::opt<bool> FatalWarnings("fatal-warnings",
1202+ cl::desc("Treat warnings as errors"));
1203+ MCBINDOPT(FatalWarnings);
1204+
1205+ static cl::opt<bool> NoWarn("no-warn", cl::desc("Suppress all warnings"));
1206+ static cl::alias NoWarnW("W", cl::desc("Alias for --no-warn"),
1207+ cl::aliasopt(NoWarn));
1208+ MCBINDOPT(NoWarn);
1209+
1210+ static cl::opt<bool> NoDeprecatedWarn(
1211+ "no-deprecated-warn", cl::desc("Suppress all deprecated warnings"));
1212+ MCBINDOPT(NoDeprecatedWarn);
1213+
1214+ static cl::opt<std::string> ABIName(
1215+ "target-abi", cl::Hidden,
1216+ cl::desc("The name of the ABI to be targeted from the backend."),
1217+ cl::init(""));
1218+ MCBINDOPT(ABIName);
1219+
1220+#undef MCBINDOPT
1221+}
1222+
1223+MCTargetOptions llvm::mc::InitMCTargetOptionsFromFlags() {
1224+ MCTargetOptions Options;
1225+ Options.MCRelaxAll = getRelaxAll();
1226+ Options.MCIncrementalLinkerCompatible = getIncrementalLinkerCompatible();
1227+ Options.DwarfVersion = getDwarfVersion();
1228+ Options.ShowMCInst = getShowMCInst();
1229+ Options.ABIName = getABIName();
1230+ Options.MCFatalWarnings = getFatalWarnings();
1231+ Options.MCNoWarn = getNoWarn();
1232+ Options.MCNoDeprecatedWarn = getNoDeprecatedWarn();
1233+ return Options;
1234+}
1235diff --git a/llvm/tools/dsymutil/DwarfStreamer.cpp b/llvm/tools/dsymutil/DwarfStreamer.cpp
1236index 3e132c29eada..eb068effbc71 100644
1237--- a/llvm/tools/dsymutil/DwarfStreamer.cpp
1238+++ b/llvm/tools/dsymutil/DwarfStreamer.cpp
1239@@ -13,13 +13,16 @@
1240 #include "llvm/DWARFLinker/DWARFLinkerCompileUnit.h"
1241 #include "llvm/DebugInfo/DWARF/DWARFContext.h"
1242 #include "llvm/MC/MCTargetOptions.h"
1243-#include "llvm/MC/MCTargetOptionsCommandFlags.inc"
1244+#include "llvm/MC/MCTargetOptionsCommandFlags.h"
1245 #include "llvm/Support/LEB128.h"
1246 #include "llvm/Support/TargetRegistry.h"
1247 #include "llvm/Target/TargetMachine.h"
1248 #include "llvm/Target/TargetOptions.h"
1249
1250 namespace llvm {
1251+
1252+static mc::RegisterMCTargetOptionsFlags MOF;
1253+
1254 namespace dsymutil {
1255
1256 /// Retrieve the section named \a SecName in \a Obj.
1257@@ -61,7 +64,7 @@ bool DwarfStreamer::init(Triple TheTriple) {
1258 if (!MRI)
1259 return error(Twine("no register info for target ") + TripleName, Context);
1260
1261- MCTargetOptions MCOptions = InitMCTargetOptionsFromFlags();
1262+ MCTargetOptions MCOptions = mc::InitMCTargetOptionsFromFlags();
1263 MAI.reset(TheTarget->createMCAsmInfo(*MRI, TripleName, MCOptions));
1264 if (!MAI)
1265 return error("no asm info for target " + TripleName, Context);
1266diff --git a/llvm/tools/gold/gold-plugin.cpp b/llvm/tools/gold/gold-plugin.cpp
1267index 406079dad307..95e72b861197 100644
1268--- a/llvm/tools/gold/gold-plugin.cpp
1269+++ b/llvm/tools/gold/gold-plugin.cpp
1270@@ -14,7 +14,7 @@
1271 #include "llvm/ADT/Statistic.h"
1272 #include "llvm/Bitcode/BitcodeReader.h"
1273 #include "llvm/Bitcode/BitcodeWriter.h"
1274-#include "llvm/CodeGen/CommandFlags.inc"
1275+#include "llvm/CodeGen/CommandFlags.h"
1276 #include "llvm/Config/config.h" // plugin-api.h requires HAVE_STDINT_H
1277 #include "llvm/IR/Constants.h"
1278 #include "llvm/IR/DiagnosticPrinter.h"
1279@@ -50,6 +50,8 @@
1280 using namespace llvm;
1281 using namespace lto;
1282
1283+static codegen::RegisterCodeGenFlags CodeGenFlags;
1284+
1285 // FIXME: Remove when binutils 2.31 (containing gold 1.16) is the minimum
1286 // required version.
1287 typedef enum ld_plugin_status (*ld_plugin_get_wrap_symbols)(
1288@@ -842,21 +844,21 @@ static std::unique_ptr<LTO> createLTO(IndexWriteCallback OnIndexWrite,
1289 ThinBackend Backend;
1290
1291 Conf.CPU = options::mcpu;
1292- Conf.Options = InitTargetOptionsFromCodeGenFlags();
1293+ Conf.Options = codegen::InitTargetOptionsFromCodeGenFlags();
1294
1295 // Disable the new X86 relax relocations since gold might not support them.
1296 // FIXME: Check the gold version or add a new option to enable them.
1297 Conf.Options.RelaxELFRelocations = false;
1298
1299 // Toggle function/data sections.
1300- if (FunctionSections.getNumOccurrences() == 0)
1301+ if (!codegen::getExplicitFunctionSections())
1302 Conf.Options.FunctionSections = SplitSections;
1303- if (DataSections.getNumOccurrences() == 0)
1304+ if (!codegen::getExplicitDataSections())
1305 Conf.Options.DataSections = SplitSections;
1306
1307- Conf.MAttrs = MAttrs;
1308- Conf.RelocModel = RelocationModel;
1309- Conf.CodeModel = getCodeModel();
1310+ Conf.MAttrs = codegen::getMAttrs();
1311+ Conf.RelocModel = codegen::getExplicitRelocModel();
1312+ Conf.CodeModel = codegen::getExplicitCodeModel();
1313 Conf.CGOptLevel = getCGOptLevel();
1314 Conf.DisableVerify = options::DisableVerify;
1315 Conf.OptLevel = options::OptLevel;
1316diff --git a/llvm/tools/llc/CMakeLists.txt b/llvm/tools/llc/CMakeLists.txt
1317index 880deefa539c..479bc6b55b27 100644
1318--- a/llvm/tools/llc/CMakeLists.txt
1319+++ b/llvm/tools/llc/CMakeLists.txt
1320@@ -26,4 +26,5 @@ add_llvm_tool(llc
1321 intrinsics_gen
1322 SUPPORT_PLUGINS
1323 )
1324+
1325 export_executable_symbols(llc)
1326diff --git a/llvm/tools/llc/llc.cpp b/llvm/tools/llc/llc.cpp
1327index b35f8e853c30..4c41ed292fdc 100644
1328--- a/llvm/tools/llc/llc.cpp
1329+++ b/llvm/tools/llc/llc.cpp
1330@@ -15,7 +15,7 @@
1331 #include "llvm/ADT/STLExtras.h"
1332 #include "llvm/ADT/Triple.h"
1333 #include "llvm/Analysis/TargetLibraryInfo.h"
1334-#include "llvm/CodeGen/CommandFlags.inc"
1335+#include "llvm/CodeGen/CommandFlags.h"
1336 #include "llvm/CodeGen/LinkAllAsmWriterComponents.h"
1337 #include "llvm/CodeGen/LinkAllCodegenComponents.h"
1338 #include "llvm/CodeGen/MIRParser/MIRParser.h"
1339@@ -55,6 +55,8 @@
1340 #include <memory>
1341 using namespace llvm;
1342
1343+static codegen::RegisterCodeGenFlags CGF;
1344+
1345 // General options for llc. Other pass-specific options are specified
1346 // within the corresponding llc passes, and target-specific options
1347 // and back-end code generation options are specified with the target machine.
1348@@ -202,7 +204,7 @@ static std::unique_ptr<ToolOutputFile> GetOutputStream(const char *TargetName,
1349 else
1350 OutputFilename = IFN;
1351
1352- switch (FileType) {
1353+ switch (codegen::getFileType()) {
1354 case CGFT_AssemblyFile:
1355 if (TargetName[0] == 'c') {
1356 if (TargetName[1] == 0)
1357@@ -229,7 +231,7 @@ static std::unique_ptr<ToolOutputFile> GetOutputStream(const char *TargetName,
1358
1359 // Decide if we need "binary" output.
1360 bool Binary = false;
1361- switch (FileType) {
1362+ switch (codegen::getFileType()) {
1363 case CGFT_AssemblyFile:
1364 break;
1365 case CGFT_ObjectFile:
1366@@ -395,14 +397,16 @@ static int compileModule(char **argv, LLVMContext &Context) {
1367 std::unique_ptr<Module> M;
1368 std::unique_ptr<MIRParser> MIR;
1369 Triple TheTriple;
1370- std::string CPUStr = getCPUStr(), FeaturesStr = getFeaturesStr();
1371+ std::string CPUStr = codegen::getCPUStr(),
1372+ FeaturesStr = codegen::getFeaturesStr();
1373
1374 // Set attributes on functions as loaded from MIR from command line arguments.
1375 auto setMIRFunctionAttributes = [&CPUStr, &FeaturesStr](Function &F) {
1376- setFunctionAttributes(CPUStr, FeaturesStr, F);
1377+ codegen::setFunctionAttributes(CPUStr, FeaturesStr, F);
1378 };
1379
1380- bool SkipModule = MCPU == "help" ||
1381+ auto MAttrs = codegen::getMAttrs();
1382+ bool SkipModule = codegen::getMCPU() == "help" ||
1383 (!MAttrs.empty() && MAttrs.front() == "help");
1384
1385 // If user just wants to list available options, skip module loading
1386@@ -433,8 +437,8 @@ static int compileModule(char **argv, LLVMContext &Context) {
1387
1388 // Get the target specific parser.
1389 std::string Error;
1390- const Target *TheTarget = TargetRegistry::lookupTarget(MArch, TheTriple,
1391- Error);
1392+ const Target *TheTarget =
1393+ TargetRegistry::lookupTarget(codegen::getMArch(), TheTriple, Error);
1394 if (!TheTarget) {
1395 WithColor::error(errs(), argv[0]) << Error;
1396 return 1;
1397@@ -452,7 +456,7 @@ static int compileModule(char **argv, LLVMContext &Context) {
1398 case '3': OLvl = CodeGenOpt::Aggressive; break;
1399 }
1400
1401- TargetOptions Options = InitTargetOptionsFromCodeGenFlags();
1402+ TargetOptions Options = codegen::InitTargetOptionsFromCodeGenFlags();
1403 Options.DisableIntegratedAS = NoIntegratedAssembler;
1404 Options.MCOptions.ShowMCEncoding = ShowMCEncoding;
1405 Options.MCOptions.MCUseDwarfDirectory = EnableDwarfDirectory;
1406@@ -462,8 +466,8 @@ static int compileModule(char **argv, LLVMContext &Context) {
1407 Options.MCOptions.SplitDwarfFile = SplitDwarfFile;
1408
1409 std::unique_ptr<TargetMachine> Target(TheTarget->createTargetMachine(
1410- TheTriple.getTriple(), CPUStr, FeaturesStr, Options, getRelocModel(),
1411- getCodeModel(), OLvl));
1412+ TheTriple.getTriple(), CPUStr, FeaturesStr, Options, codegen::getExplicitRelocModel(),
1413+ codegen::getExplicitCodeModel(), OLvl));
1414
1415 assert(Target && "Could not allocate target machine!");
1416
1417@@ -474,8 +478,8 @@ static int compileModule(char **argv, LLVMContext &Context) {
1418 return 0;
1419
1420 assert(M && "Should have exited if we didn't have a module!");
1421- if (FloatABIForCalls != FloatABI::Default)
1422- Options.FloatABIType = FloatABIForCalls;
1423+ if (codegen::getFloatABIForCalls() != FloatABI::Default)
1424+ Options.FloatABIType = codegen::getFloatABIForCalls();
1425
1426 // Figure out where we are going to send the output.
1427 std::unique_ptr<ToolOutputFile> Out =
1428@@ -522,10 +526,9 @@ static int compileModule(char **argv, LLVMContext &Context) {
1429
1430 // Override function attributes based on CPUStr, FeaturesStr, and command line
1431 // flags.
1432- setFunctionAttributes(CPUStr, FeaturesStr, *M);
1433+ codegen::setFunctionAttributes(CPUStr, FeaturesStr, *M);
1434
1435- if (RelaxAll.getNumOccurrences() > 0 &&
1436- FileType != CGFT_ObjectFile)
1437+ if (mc::getExplicitRelaxAll() && codegen::getFileType() != CGFT_ObjectFile)
1438 WithColor::warning(errs(), argv[0])
1439 << ": warning: ignoring -mc-relax-all because filetype != obj";
1440
1441@@ -536,7 +539,7 @@ static int compileModule(char **argv, LLVMContext &Context) {
1442 // so we can memcmp the contents in CompileTwice mode
1443 SmallVector<char, 0> Buffer;
1444 std::unique_ptr<raw_svector_ostream> BOS;
1445- if ((FileType != CGFT_AssemblyFile &&
1446+ if ((codegen::getFileType() != CGFT_AssemblyFile &&
1447 !Out->os().supportsSeeking()) ||
1448 CompileTwice) {
1449 BOS = std::make_unique<raw_svector_ostream>(Buffer);
1450@@ -575,9 +578,9 @@ static int compileModule(char **argv, LLVMContext &Context) {
1451 TPC.setInitialized();
1452 PM.add(createPrintMIRPass(*OS));
1453 PM.add(createFreeMachineFunctionPass());
1454- } else if (Target->addPassesToEmitFile(PM, *OS,
1455- DwoOut ? &DwoOut->os() : nullptr,
1456- FileType, NoVerify, MMIWP)) {
1457+ } else if (Target->addPassesToEmitFile(
1458+ PM, *OS, DwoOut ? &DwoOut->os() : nullptr,
1459+ codegen::getFileType(), NoVerify, MMIWP)) {
1460 WithColor::warning(errs(), argv[0])
1461 << "target does not support generation of this"
1462 << " file type!\n";
1463diff --git a/llvm/tools/lli/CMakeLists.txt b/llvm/tools/lli/CMakeLists.txt
1464index db163ad131e8..bc6ef213b8fd 100644
1465--- a/llvm/tools/lli/CMakeLists.txt
1466+++ b/llvm/tools/lli/CMakeLists.txt
1467@@ -53,4 +53,5 @@ add_llvm_tool(lli
1468 DEPENDS
1469 intrinsics_gen
1470 )
1471+
1472 export_executable_symbols(lli)
1473diff --git a/llvm/tools/lli/lli.cpp b/llvm/tools/lli/lli.cpp
1474index 0efd0df2c12b..b6a4e3f2833c 100644
1475--- a/llvm/tools/lli/lli.cpp
1476+++ b/llvm/tools/lli/lli.cpp
1477@@ -16,7 +16,7 @@
1478 #include "llvm/ADT/StringExtras.h"
1479 #include "llvm/ADT/Triple.h"
1480 #include "llvm/Bitcode/BitcodeReader.h"
1481-#include "llvm/CodeGen/CommandFlags.inc"
1482+#include "llvm/CodeGen/CommandFlags.h"
1483 #include "llvm/CodeGen/LinkAllCodegenComponents.h"
1484 #include "llvm/Config/llvm-config.h"
1485 #include "llvm/ExecutionEngine/GenericValue.h"
1486@@ -67,6 +67,8 @@
1487
1488 using namespace llvm;
1489
1490+static codegen::RegisterCodeGenFlags CGF;
1491+
1492 #define DEBUG_TYPE "lli"
1493
1494 namespace {
1495@@ -410,13 +412,13 @@ int main(int argc, char **argv, char * const *envp) {
1496
1497 std::string ErrorMsg;
1498 EngineBuilder builder(std::move(Owner));
1499- builder.setMArch(MArch);
1500- builder.setMCPU(getCPUStr());
1501- builder.setMAttrs(getFeatureList());
1502- if (RelocModel.getNumOccurrences())
1503- builder.setRelocationModel(RelocModel);
1504- if (CMModel.getNumOccurrences())
1505- builder.setCodeModel(CMModel);
1506+ builder.setMArch(codegen::getMArch());
1507+ builder.setMCPU(codegen::getCPUStr());
1508+ builder.setMAttrs(codegen::getFeatureList());
1509+ if (auto RM = codegen::getExplicitRelocModel())
1510+ builder.setRelocationModel(RM.getValue());
1511+ if (auto CM = codegen::getExplicitCodeModel())
1512+ builder.setCodeModel(CM.getValue());
1513 builder.setErrorStr(&ErrorMsg);
1514 builder.setEngineKind(ForceInterpreter
1515 ? EngineKind::Interpreter
1516@@ -448,9 +450,9 @@ int main(int argc, char **argv, char * const *envp) {
1517
1518 builder.setOptLevel(getOptLevel());
1519
1520- TargetOptions Options = InitTargetOptionsFromCodeGenFlags();
1521- if (FloatABIForCalls != FloatABI::Default)
1522- Options.FloatABIType = FloatABIForCalls;
1523+ TargetOptions Options = codegen::InitTargetOptionsFromCodeGenFlags();
1524+ if (codegen::getFloatABIForCalls() != FloatABI::Default)
1525+ Options.FloatABIType = codegen::getFloatABIForCalls();
1526
1527 builder.setTargetOptions(Options);
1528
1529@@ -762,18 +764,15 @@ int runOrcLazyJIT(const char *ProgName) {
1530 TT.empty() ? ExitOnErr(orc::JITTargetMachineBuilder::detectHost())
1531 : orc::JITTargetMachineBuilder(Triple(TT)));
1532
1533- if (!MArch.empty())
1534- Builder.getJITTargetMachineBuilder()->getTargetTriple().setArchName(MArch);
1535+ if (!codegen::getMArch().empty())
1536+ Builder.getJITTargetMachineBuilder()->getTargetTriple().setArchName(
1537+ codegen::getMArch());
1538
1539 Builder.getJITTargetMachineBuilder()
1540- ->setCPU(getCPUStr())
1541- .addFeatures(getFeatureList())
1542- .setRelocationModel(RelocModel.getNumOccurrences()
1543- ? Optional<Reloc::Model>(RelocModel)
1544- : None)
1545- .setCodeModel(CMModel.getNumOccurrences()
1546- ? Optional<CodeModel::Model>(CMModel)
1547- : None);
1548+ ->setCPU(codegen::getCPUStr())
1549+ .addFeatures(codegen::getFeatureList())
1550+ .setRelocationModel(codegen::getExplicitRelocModel())
1551+ .setCodeModel(codegen::getExplicitCodeModel());
1552
1553 Builder.setLazyCompileFailureAddr(
1554 pointerToJITTargetAddress(exitOnLazyCallThroughFailure));
1555diff --git a/llvm/tools/llvm-dwp/llvm-dwp.cpp b/llvm/tools/llvm-dwp/llvm-dwp.cpp
1556index 23513ef8fb4e..8cfd433d5da3 100644
1557--- a/llvm/tools/llvm-dwp/llvm-dwp.cpp
1558+++ b/llvm/tools/llvm-dwp/llvm-dwp.cpp
1559@@ -27,7 +27,7 @@
1560 #include "llvm/MC/MCObjectWriter.h"
1561 #include "llvm/MC/MCRegisterInfo.h"
1562 #include "llvm/MC/MCStreamer.h"
1563-#include "llvm/MC/MCTargetOptionsCommandFlags.inc"
1564+#include "llvm/MC/MCTargetOptionsCommandFlags.h"
1565 #include "llvm/Object/Decompressor.h"
1566 #include "llvm/Object/ObjectFile.h"
1567 #include "llvm/Support/DataExtractor.h"
1568@@ -46,6 +46,8 @@
1569 using namespace llvm;
1570 using namespace llvm::object;
1571
1572+static mc::RegisterMCTargetOptionsFlags MCTargetOptionsFlags;
1573+
1574 cl::OptionCategory DwpCategory("Specific Options");
1575 static cl::list<std::string> InputFiles(cl::Positional, cl::ZeroOrMore,
1576 cl::desc("<input files>"),
1577@@ -676,7 +678,7 @@ int main(int argc, char **argv) {
1578 if (!MRI)
1579 return error(Twine("no register info for target ") + TripleName, Context);
1580
1581- MCTargetOptions MCOptions = InitMCTargetOptionsFromFlags();
1582+ MCTargetOptions MCOptions = llvm::mc::InitMCTargetOptionsFromFlags();
1583 std::unique_ptr<MCAsmInfo> MAI(
1584 TheTarget->createMCAsmInfo(*MRI, TripleName, MCOptions));
1585 if (!MAI)
1586diff --git a/llvm/tools/llvm-isel-fuzzer/llvm-isel-fuzzer.cpp b/llvm/tools/llvm-isel-fuzzer/llvm-isel-fuzzer.cpp
1587index b71ed4a70566..627e9ab4c03f 100644
1588--- a/llvm/tools/llvm-isel-fuzzer/llvm-isel-fuzzer.cpp
1589+++ b/llvm/tools/llvm-isel-fuzzer/llvm-isel-fuzzer.cpp
1590@@ -14,7 +14,7 @@
1591 #include "llvm/Analysis/TargetLibraryInfo.h"
1592 #include "llvm/Bitcode/BitcodeReader.h"
1593 #include "llvm/Bitcode/BitcodeWriter.h"
1594-#include "llvm/CodeGen/CommandFlags.inc"
1595+#include "llvm/CodeGen/CommandFlags.h"
1596 #include "llvm/FuzzMutate/FuzzerCLI.h"
1597 #include "llvm/FuzzMutate/IRMutator.h"
1598 #include "llvm/FuzzMutate/Operations.h"
1599@@ -35,6 +35,8 @@
1600
1601 using namespace llvm;
1602
1603+static codegen::RegisterCodeGenFlags CGF;
1604+
1605 static cl::opt<char>
1606 OptLevel("O",
1607 cl::desc("Optimization level. [-O0, -O1, -O2, or -O3] "
1608@@ -133,14 +135,15 @@ extern "C" LLVM_ATTRIBUTE_USED int LLVMFuzzerInitialize(int *argc,
1609 // Get the target specific parser.
1610 std::string Error;
1611 const Target *TheTarget =
1612- TargetRegistry::lookupTarget(MArch, TheTriple, Error);
1613+ TargetRegistry::lookupTarget(codegen::getMArch(), TheTriple, Error);
1614 if (!TheTarget) {
1615 errs() << argv[0] << ": " << Error;
1616 return 1;
1617 }
1618
1619 // Set up the pipeline like llc does.
1620- std::string CPUStr = getCPUStr(), FeaturesStr = getFeaturesStr();
1621+ std::string CPUStr = codegen::getCPUStr(),
1622+ FeaturesStr = codegen::getFeaturesStr();
1623
1624 CodeGenOpt::Level OLvl = CodeGenOpt::Default;
1625 switch (OptLevel) {
1626@@ -154,10 +157,10 @@ extern "C" LLVM_ATTRIBUTE_USED int LLVMFuzzerInitialize(int *argc,
1627 case '3': OLvl = CodeGenOpt::Aggressive; break;
1628 }
1629
1630- TargetOptions Options = InitTargetOptionsFromCodeGenFlags();
1631- TM.reset(TheTarget->createTargetMachine(TheTriple.getTriple(), CPUStr,
1632- FeaturesStr, Options, getRelocModel(),
1633- getCodeModel(), OLvl));
1634+ TargetOptions Options = codegen::InitTargetOptionsFromCodeGenFlags();
1635+ TM.reset(TheTarget->createTargetMachine(
1636+ TheTriple.getTriple(), CPUStr, FeaturesStr, Options,
1637+ codegen::getExplicitRelocModel(), codegen::getExplicitCodeModel(), OLvl));
1638 assert(TM && "Could not allocate target machine!");
1639
1640 // Make sure we print the summary and the current unit when LLVM errors out.
1641diff --git a/llvm/tools/llvm-lto/CMakeLists.txt b/llvm/tools/llvm-lto/CMakeLists.txt
1642index 69868fb870c0..5128e713eecf 100644
1643--- a/llvm/tools/llvm-lto/CMakeLists.txt
1644+++ b/llvm/tools/llvm-lto/CMakeLists.txt
1645@@ -5,6 +5,7 @@ set(LLVM_LINK_COMPONENTS
1646 AllTargetsInfos
1647 BitReader
1648 BitWriter
1649+ CodeGen
1650 Core
1651 IRReader
1652 LTO
1653@@ -17,7 +18,5 @@ set(LLVM_LINK_COMPONENTS
1654 add_llvm_tool(llvm-lto
1655 llvm-lto.cpp
1656
1657- DEPENDS
1658- intrinsics_gen
1659+ DEPENDS intrinsics_gen
1660 )
1661-
1662diff --git a/llvm/tools/llvm-lto/llvm-lto.cpp b/llvm/tools/llvm-lto/llvm-lto.cpp
1663index b47e68e82850..7886426e8945 100644
1664--- a/llvm/tools/llvm-lto/llvm-lto.cpp
1665+++ b/llvm/tools/llvm-lto/llvm-lto.cpp
1666@@ -21,7 +21,7 @@
1667 #include "llvm/ADT/Twine.h"
1668 #include "llvm/Bitcode/BitcodeReader.h"
1669 #include "llvm/Bitcode/BitcodeWriter.h"
1670-#include "llvm/CodeGen/CommandFlags.inc"
1671+#include "llvm/CodeGen/CommandFlags.h"
1672 #include "llvm/IR/DiagnosticInfo.h"
1673 #include "llvm/IR/DiagnosticPrinter.h"
1674 #include "llvm/IR/LLVMContext.h"
1675@@ -62,6 +62,8 @@
1676
1677 using namespace llvm;
1678
1679+static codegen::RegisterCodeGenFlags CGF;
1680+
1681 static cl::opt<char>
1682 OptLevel("O", cl::desc("Optimization level. [-O0, -O1, -O2, or -O3] "
1683 "(default = '-O2')"),
1684@@ -521,7 +523,7 @@ public:
1685 ThinLTOCodeGenerator ThinGenerator;
1686
1687 ThinLTOProcessing(const TargetOptions &Options) {
1688- ThinGenerator.setCodePICModel(getRelocModel());
1689+ ThinGenerator.setCodePICModel(codegen::getExplicitRelocModel());
1690 ThinGenerator.setTargetOptions(Options);
1691 ThinGenerator.setCacheDir(ThinLTOCacheDir);
1692 ThinGenerator.setCachePruningInterval(ThinLTOCachePruningInterval);
1693@@ -873,7 +875,7 @@ int main(int argc, char **argv) {
1694 InitializeAllAsmParsers();
1695
1696 // set up the TargetOptions for the machine
1697- TargetOptions Options = InitTargetOptionsFromCodeGenFlags();
1698+ TargetOptions Options = codegen::InitTargetOptionsFromCodeGenFlags();
1699
1700 if (ListSymbolsOnly) {
1701 listSymbols(Options);
1702@@ -929,7 +931,7 @@ int main(int argc, char **argv) {
1703 if (UseDiagnosticHandler)
1704 CodeGen.setDiagnosticHandler(handleDiagnostics, nullptr);
1705
1706- CodeGen.setCodePICModel(getRelocModel());
1707+ CodeGen.setCodePICModel(codegen::getExplicitRelocModel());
1708 CodeGen.setFreestanding(EnableFreestanding);
1709
1710 CodeGen.setDebugInfo(LTO_DEBUG_MODEL_DWARF);
1711@@ -980,22 +982,18 @@ int main(int argc, char **argv) {
1712 CodeGen.addMustPreserveSymbol(KeptDSOSyms[i]);
1713
1714 // Set cpu and attrs strings for the default target/subtarget.
1715- CodeGen.setCpu(MCPU.c_str());
1716+ CodeGen.setCpu(codegen::getMCPU().c_str());
1717
1718 CodeGen.setOptLevel(OptLevel - '0');
1719
1720- std::string attrs;
1721- for (unsigned i = 0; i < MAttrs.size(); ++i) {
1722- if (i > 0)
1723- attrs.append(",");
1724- attrs.append(MAttrs[i]);
1725- }
1726-
1727- if (!attrs.empty())
1728+ auto MAttrs = codegen::getMAttrs();
1729+ if (!MAttrs.empty()) {
1730+ std::string attrs = join(MAttrs, ",");
1731 CodeGen.setAttr(attrs);
1732+ }
1733
1734- if (FileType.getNumOccurrences())
1735- CodeGen.setFileType(FileType);
1736+ if (auto FT = codegen::getExplicitFileType())
1737+ CodeGen.setFileType(FT.getValue());
1738
1739 if (!OutputFilename.empty()) {
1740 if (!CodeGen.optimize(DisableVerify, DisableInline, DisableGVNLoadPRE,
1741diff --git a/llvm/tools/llvm-lto2/CMakeLists.txt b/llvm/tools/llvm-lto2/CMakeLists.txt
1742index fa2d8624fd94..4d3364175b04 100644
1743--- a/llvm/tools/llvm-lto2/CMakeLists.txt
1744+++ b/llvm/tools/llvm-lto2/CMakeLists.txt
1745@@ -4,6 +4,7 @@ set(LLVM_LINK_COMPONENTS
1746 AllTargetsDescs
1747 AllTargetsInfos
1748 BitReader
1749+ CodeGen
1750 Core
1751 Linker
1752 LTO
1753diff --git a/llvm/tools/llvm-lto2/llvm-lto2.cpp b/llvm/tools/llvm-lto2/llvm-lto2.cpp
1754index 67a677dd45fb..142ba605aa2a 100644
1755--- a/llvm/tools/llvm-lto2/llvm-lto2.cpp
1756+++ b/llvm/tools/llvm-lto2/llvm-lto2.cpp
1757@@ -16,7 +16,7 @@
1758 //===----------------------------------------------------------------------===//
1759
1760 #include "llvm/Bitcode/BitcodeReader.h"
1761-#include "llvm/CodeGen/CommandFlags.inc"
1762+#include "llvm/CodeGen/CommandFlags.h"
1763 #include "llvm/IR/DiagnosticPrinter.h"
1764 #include "llvm/LTO/Caching.h"
1765 #include "llvm/LTO/LTO.h"
1766@@ -29,6 +29,8 @@
1767 using namespace llvm;
1768 using namespace lto;
1769
1770+static codegen::RegisterCodeGenFlags CGF;
1771+
1772 static cl::opt<char>
1773 OptLevel("O", cl::desc("Optimization level. [-O0, -O1, -O2, or -O3] "
1774 "(default = '-O2')"),
1775@@ -217,12 +219,12 @@ static int run(int argc, char **argv) {
1776 exit(1);
1777 };
1778
1779- Conf.CPU = MCPU;
1780- Conf.Options = InitTargetOptionsFromCodeGenFlags();
1781- Conf.MAttrs = MAttrs;
1782- if (auto RM = getRelocModel())
1783- Conf.RelocModel = *RM;
1784- Conf.CodeModel = getCodeModel();
1785+ Conf.CPU = codegen::getMCPU();
1786+ Conf.Options = codegen::InitTargetOptionsFromCodeGenFlags();
1787+ Conf.MAttrs = codegen::getMAttrs();
1788+ if (auto RM = codegen::getExplicitRelocModel())
1789+ Conf.RelocModel = RM.getValue();
1790+ Conf.CodeModel = codegen::getExplicitCodeModel();
1791
1792 Conf.DebugPassManager = DebugPassManager;
1793
1794@@ -264,8 +266,8 @@ static int run(int argc, char **argv) {
1795 return 1;
1796 }
1797
1798- if (FileType.getNumOccurrences())
1799- Conf.CGFileType = FileType;
1800+ if (auto FT = codegen::getExplicitFileType())
1801+ Conf.CGFileType = FT.getValue();
1802
1803 Conf.OverrideTriple = OverrideTriple;
1804 Conf.DefaultTriple = DefaultTriple;
1805diff --git a/llvm/tools/llvm-mc-assemble-fuzzer/CMakeLists.txt b/llvm/tools/llvm-mc-assemble-fuzzer/CMakeLists.txt
1806index fb6befd3c54a..6bbc502e2eee 100644
1807--- a/llvm/tools/llvm-mc-assemble-fuzzer/CMakeLists.txt
1808+++ b/llvm/tools/llvm-mc-assemble-fuzzer/CMakeLists.txt
1809@@ -7,6 +7,7 @@ set(LLVM_LINK_COMPONENTS
1810 MCParser
1811 Support
1812 )
1813+
1814 add_llvm_fuzzer(llvm-mc-assemble-fuzzer
1815 llvm-mc-assemble-fuzzer.cpp
1816 )
1817diff --git a/llvm/tools/llvm-mc-assemble-fuzzer/llvm-mc-assemble-fuzzer.cpp b/llvm/tools/llvm-mc-assemble-fuzzer/llvm-mc-assemble-fuzzer.cpp
1818index 6c5961f7027c..29699c634bfa 100644
1819--- a/llvm/tools/llvm-mc-assemble-fuzzer/llvm-mc-assemble-fuzzer.cpp
1820+++ b/llvm/tools/llvm-mc-assemble-fuzzer/llvm-mc-assemble-fuzzer.cpp
1821@@ -9,7 +9,6 @@
1822 //===----------------------------------------------------------------------===//
1823
1824 #include "llvm-c/Target.h"
1825-#include "llvm/MC/SubtargetFeature.h"
1826 #include "llvm/MC/MCAsmBackend.h"
1827 #include "llvm/MC/MCAsmInfo.h"
1828 #include "llvm/MC/MCCodeEmitter.h"
1829@@ -24,15 +23,16 @@
1830 #include "llvm/MC/MCSectionMachO.h"
1831 #include "llvm/MC/MCStreamer.h"
1832 #include "llvm/MC/MCSubtargetInfo.h"
1833-#include "llvm/MC/MCTargetOptionsCommandFlags.inc"
1834-#include "llvm/Support/MemoryBuffer.h"
1835+#include "llvm/MC/MCTargetOptionsCommandFlags.h"
1836+#include "llvm/MC/SubtargetFeature.h"
1837 #include "llvm/Support/CommandLine.h"
1838 #include "llvm/Support/FileUtilities.h"
1839-#include "llvm/Support/raw_ostream.h"
1840+#include "llvm/Support/MemoryBuffer.h"
1841 #include "llvm/Support/SourceMgr.h"
1842-#include "llvm/Support/TargetSelect.h"
1843 #include "llvm/Support/TargetRegistry.h"
1844+#include "llvm/Support/TargetSelect.h"
1845 #include "llvm/Support/ToolOutputFile.h"
1846+#include "llvm/Support/raw_ostream.h"
1847
1848 using namespace llvm;
1849
1850@@ -161,7 +161,7 @@ int AssembleOneInput(const uint8_t *Data, size_t Size) {
1851 abort();
1852 }
1853
1854- MCTargetOptions MCOptions = InitMCTargetOptionsFromFlags();
1855+ MCTargetOptions MCOptions = mc::InitMCTargetOptionsFromFlags();
1856 std::unique_ptr<MCAsmInfo> MAI(
1857 TheTarget->createMCAsmInfo(*MRI, TripleName, MCOptions));
1858 if (!MAI) {
1859diff --git a/llvm/tools/llvm-mc/CMakeLists.txt b/llvm/tools/llvm-mc/CMakeLists.txt
1860index 15c6dda1b258..94add70b6943 100644
1861--- a/llvm/tools/llvm-mc/CMakeLists.txt
1862+++ b/llvm/tools/llvm-mc/CMakeLists.txt
1863@@ -4,6 +4,7 @@ set(LLVM_LINK_COMPONENTS
1864 AllTargetsDescs
1865 AllTargetsDisassemblers
1866 AllTargetsInfos
1867+ CodeGen
1868 MC
1869 MCParser
1870 Support
1871diff --git a/llvm/tools/llvm-mc/llvm-mc.cpp b/llvm/tools/llvm-mc/llvm-mc.cpp
1872index 6aa347d98be2..8c1b3cf2cab0 100644
1873--- a/llvm/tools/llvm-mc/llvm-mc.cpp
1874+++ b/llvm/tools/llvm-mc/llvm-mc.cpp
1875@@ -25,7 +25,7 @@
1876 #include "llvm/MC/MCRegisterInfo.h"
1877 #include "llvm/MC/MCStreamer.h"
1878 #include "llvm/MC/MCSubtargetInfo.h"
1879-#include "llvm/MC/MCTargetOptionsCommandFlags.inc"
1880+#include "llvm/MC/MCTargetOptionsCommandFlags.h"
1881 #include "llvm/Support/CommandLine.h"
1882 #include "llvm/Support/Compression.h"
1883 #include "llvm/Support/FileUtilities.h"
1884@@ -41,6 +41,8 @@
1885
1886 using namespace llvm;
1887
1888+static mc::RegisterMCTargetOptionsFlags MOF;
1889+
1890 static cl::opt<std::string>
1891 InputFilename(cl::Positional, cl::desc("<input file>"), cl::init("-"));
1892
1893@@ -317,7 +319,7 @@ int main(int argc, char **argv) {
1894 cl::AddExtraVersionPrinter(TargetRegistry::printRegisteredTargetsForVersion);
1895
1896 cl::ParseCommandLineOptions(argc, argv, "llvm machine code playground\n");
1897- const MCTargetOptions MCOptions = InitMCTargetOptionsFromFlags();
1898+ const MCTargetOptions MCOptions = mc::InitMCTargetOptionsFromFlags();
1899 setDwarfDebugFlags(argc, argv);
1900
1901 setDwarfDebugProducer();
1902diff --git a/llvm/tools/llvm-mca/llvm-mca.cpp b/llvm/tools/llvm-mca/llvm-mca.cpp
1903index fff5906bb59b..eca86768aa5c 100644
1904--- a/llvm/tools/llvm-mca/llvm-mca.cpp
1905+++ b/llvm/tools/llvm-mca/llvm-mca.cpp
1906@@ -39,7 +39,7 @@
1907 #include "llvm/MC/MCObjectFileInfo.h"
1908 #include "llvm/MC/MCRegisterInfo.h"
1909 #include "llvm/MC/MCSubtargetInfo.h"
1910-#include "llvm/MC/MCTargetOptionsCommandFlags.inc"
1911+#include "llvm/MC/MCTargetOptionsCommandFlags.h"
1912 #include "llvm/MCA/CodeEmitter.h"
1913 #include "llvm/MCA/Context.h"
1914 #include "llvm/MCA/InstrBuilder.h"
1915@@ -62,6 +62,8 @@
1916
1917 using namespace llvm;
1918
1919+static mc::RegisterMCTargetOptionsFlags MOF;
1920+
1921 static cl::OptionCategory ToolOptions("Tool Options");
1922 static cl::OptionCategory ViewOptions("View Options");
1923
1924@@ -353,7 +355,7 @@ int main(int argc, char **argv) {
1925 std::unique_ptr<MCRegisterInfo> MRI(TheTarget->createMCRegInfo(TripleName));
1926 assert(MRI && "Unable to create target register info!");
1927
1928- MCTargetOptions MCOptions = InitMCTargetOptionsFromFlags();
1929+ MCTargetOptions MCOptions = mc::InitMCTargetOptionsFromFlags();
1930 std::unique_ptr<MCAsmInfo> MAI(
1931 TheTarget->createMCAsmInfo(*MRI, TripleName, MCOptions));
1932 assert(MAI && "Unable to create target asm info!");
1933@@ -443,7 +445,7 @@ int main(int argc, char **argv) {
1934 TheTarget->createMCCodeEmitter(*MCII, *MRI, Ctx));
1935
1936 std::unique_ptr<MCAsmBackend> MAB(TheTarget->createMCAsmBackend(
1937- *STI, *MRI, InitMCTargetOptionsFromFlags()));
1938+ *STI, *MRI, mc::InitMCTargetOptionsFromFlags()));
1939
1940 for (const std::unique_ptr<mca::CodeRegion> &Region : Regions) {
1941 // Skip empty code regions.
1942diff --git a/llvm/tools/llvm-opt-fuzzer/llvm-opt-fuzzer.cpp b/llvm/tools/llvm-opt-fuzzer/llvm-opt-fuzzer.cpp
1943index cd6c7d380cc6..e353e333f580 100644
1944--- a/llvm/tools/llvm-opt-fuzzer/llvm-opt-fuzzer.cpp
1945+++ b/llvm/tools/llvm-opt-fuzzer/llvm-opt-fuzzer.cpp
1946@@ -12,7 +12,7 @@
1947
1948 #include "llvm/Bitcode/BitcodeReader.h"
1949 #include "llvm/Bitcode/BitcodeWriter.h"
1950-#include "llvm/CodeGen/CommandFlags.inc"
1951+#include "llvm/CodeGen/CommandFlags.h"
1952 #include "llvm/FuzzMutate/FuzzerCLI.h"
1953 #include "llvm/FuzzMutate/IRMutator.h"
1954 #include "llvm/IR/Verifier.h"
1955@@ -24,6 +24,8 @@
1956
1957 using namespace llvm;
1958
1959+static codegen::RegisterCodeGenFlags CGF;
1960+
1961 static cl::opt<std::string>
1962 TargetTripleStr("mtriple", cl::desc("Override target triple for module"));
1963
1964@@ -124,7 +126,8 @@ extern "C" int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) {
1965
1966 M->setTargetTriple(TM->getTargetTriple().normalize());
1967 M->setDataLayout(TM->createDataLayout());
1968- setFunctionAttributes(TM->getTargetCPU(), TM->getTargetFeatureString(), *M);
1969+ codegen::setFunctionAttributes(TM->getTargetCPU(),
1970+ TM->getTargetFeatureString(), *M);
1971
1972 // Create pass pipeline
1973 //
1974@@ -214,16 +217,17 @@ extern "C" LLVM_ATTRIBUTE_USED int LLVMFuzzerInitialize(
1975
1976 std::string Error;
1977 const Target *TheTarget =
1978- TargetRegistry::lookupTarget(MArch, TargetTriple, Error);
1979+ TargetRegistry::lookupTarget(codegen::getMArch(), TargetTriple, Error);
1980 if (!TheTarget) {
1981 errs() << *argv[0] << ": " << Error;
1982 exit(1);
1983 }
1984
1985- TargetOptions Options = InitTargetOptionsFromCodeGenFlags();
1986+ TargetOptions Options = codegen::InitTargetOptionsFromCodeGenFlags();
1987 TM.reset(TheTarget->createTargetMachine(
1988- TargetTriple.getTriple(), getCPUStr(), getFeaturesStr(),
1989- Options, getRelocModel(), getCodeModel(), CodeGenOpt::Default));
1990+ TargetTriple.getTriple(), codegen::getCPUStr(), codegen::getFeaturesStr(),
1991+ Options, codegen::getExplicitRelocModel(),
1992+ codegen::getExplicitCodeModel(), CodeGenOpt::Default));
1993 assert(TM && "Could not allocate target machine!");
1994
1995 // Check that pass pipeline is specified and correct
1996diff --git a/llvm/tools/lto/CMakeLists.txt b/llvm/tools/lto/CMakeLists.txt
1997index b86e4abd01a7..2963f97cad88 100644
1998--- a/llvm/tools/lto/CMakeLists.txt
1999+++ b/llvm/tools/lto/CMakeLists.txt
2000@@ -6,6 +6,7 @@ set(LLVM_LINK_COMPONENTS
2001 AllTargetsInfos
2002 BitReader
2003 Core
2004+ CodeGen
2005 LTO
2006 MC
2007 MCDisassembler
2008@@ -20,7 +21,8 @@ set(SOURCES
2009
2010 set(LLVM_EXPORTED_SYMBOL_FILE ${CMAKE_CURRENT_SOURCE_DIR}/lto.exports)
2011
2012-add_llvm_library(LTO SHARED INSTALL_WITH_TOOLCHAIN ${SOURCES} DEPENDS intrinsics_gen)
2013+add_llvm_library(LTO SHARED INSTALL_WITH_TOOLCHAIN ${SOURCES} DEPENDS
2014+ intrinsics_gen)
2015
2016 install(FILES ${LLVM_MAIN_INCLUDE_DIR}/llvm-c/lto.h
2017 DESTINATION include/llvm-c
2018diff --git a/llvm/tools/lto/lto.cpp b/llvm/tools/lto/lto.cpp
2019index 9933af94de1e..6d207b76685f 100644
2020--- a/llvm/tools/lto/lto.cpp
2021+++ b/llvm/tools/lto/lto.cpp
2022@@ -15,7 +15,7 @@
2023 #include "llvm/ADT/STLExtras.h"
2024 #include "llvm/ADT/StringExtras.h"
2025 #include "llvm/Bitcode/BitcodeReader.h"
2026-#include "llvm/CodeGen/CommandFlags.inc"
2027+#include "llvm/CodeGen/CommandFlags.h"
2028 #include "llvm/IR/DiagnosticInfo.h"
2029 #include "llvm/IR/DiagnosticPrinter.h"
2030 #include "llvm/IR/LLVMContext.h"
2031@@ -28,6 +28,10 @@
2032 #include "llvm/Support/TargetSelect.h"
2033 #include "llvm/Support/raw_ostream.h"
2034
2035+using namespace llvm;
2036+
2037+static codegen::RegisterCodeGenFlags CGF;
2038+
2039 // extra command-line flags needed for LTOCodeGenerator
2040 static cl::opt<char>
2041 OptLevel("O",
2042@@ -154,14 +158,9 @@ DEFINE_SIMPLE_CONVERSION_FUNCTIONS(LTOModule, lto_module_t)
2043 // Convert the subtarget features into a string to pass to LTOCodeGenerator.
2044 static void lto_add_attrs(lto_code_gen_t cg) {
2045 LTOCodeGenerator *CG = unwrap(cg);
2046- if (MAttrs.size()) {
2047- std::string attrs;
2048- for (unsigned i = 0; i < MAttrs.size(); ++i) {
2049- if (i > 0)
2050- attrs.append(",");
2051- attrs.append(MAttrs[i]);
2052- }
2053-
2054+ auto MAttrs = codegen::getMAttrs();
2055+ if (!MAttrs.empty()) {
2056+ std::string attrs = join(MAttrs, ",");
2057 CG->setAttr(attrs);
2058 }
2059
2060@@ -219,7 +218,7 @@ lto_module_is_object_file_in_memory_for_target(const void* mem,
2061
2062 lto_module_t lto_module_create(const char* path) {
2063 lto_initialize();
2064- llvm::TargetOptions Options = InitTargetOptionsFromCodeGenFlags();
2065+ llvm::TargetOptions Options = codegen::InitTargetOptionsFromCodeGenFlags();
2066 ErrorOr<std::unique_ptr<LTOModule>> M =
2067 LTOModule::createFromFile(*LTOContext, StringRef(path), Options);
2068 if (!M)
2069@@ -229,7 +228,7 @@ lto_module_t lto_module_create(const char* path) {
2070
2071 lto_module_t lto_module_create_from_fd(int fd, const char *path, size_t size) {
2072 lto_initialize();
2073- llvm::TargetOptions Options = InitTargetOptionsFromCodeGenFlags();
2074+ llvm::TargetOptions Options = codegen::InitTargetOptionsFromCodeGenFlags();
2075 ErrorOr<std::unique_ptr<LTOModule>> M = LTOModule::createFromOpenFile(
2076 *LTOContext, fd, StringRef(path), size, Options);
2077 if (!M)
2078@@ -242,7 +241,7 @@ lto_module_t lto_module_create_from_fd_at_offset(int fd, const char *path,
2079 size_t map_size,
2080 off_t offset) {
2081 lto_initialize();
2082- llvm::TargetOptions Options = InitTargetOptionsFromCodeGenFlags();
2083+ llvm::TargetOptions Options = codegen::InitTargetOptionsFromCodeGenFlags();
2084 ErrorOr<std::unique_ptr<LTOModule>> M = LTOModule::createFromOpenFileSlice(
2085 *LTOContext, fd, StringRef(path), map_size, offset, Options);
2086 if (!M)
2087@@ -252,7 +251,7 @@ lto_module_t lto_module_create_from_fd_at_offset(int fd, const char *path,
2088
2089 lto_module_t lto_module_create_from_memory(const void* mem, size_t length) {
2090 lto_initialize();
2091- llvm::TargetOptions Options = InitTargetOptionsFromCodeGenFlags();
2092+ llvm::TargetOptions Options = codegen::InitTargetOptionsFromCodeGenFlags();
2093 ErrorOr<std::unique_ptr<LTOModule>> M =
2094 LTOModule::createFromBuffer(*LTOContext, mem, length, Options);
2095 if (!M)
2096@@ -264,7 +263,7 @@ lto_module_t lto_module_create_from_memory_with_path(const void* mem,
2097 size_t length,
2098 const char *path) {
2099 lto_initialize();
2100- llvm::TargetOptions Options = InitTargetOptionsFromCodeGenFlags();
2101+ llvm::TargetOptions Options = codegen::InitTargetOptionsFromCodeGenFlags();
2102 ErrorOr<std::unique_ptr<LTOModule>> M = LTOModule::createFromBuffer(
2103 *LTOContext, mem, length, Options, StringRef(path));
2104 if (!M)
2105@@ -275,7 +274,7 @@ lto_module_t lto_module_create_from_memory_with_path(const void* mem,
2106 lto_module_t lto_module_create_in_local_context(const void *mem, size_t length,
2107 const char *path) {
2108 lto_initialize();
2109- llvm::TargetOptions Options = InitTargetOptionsFromCodeGenFlags();
2110+ llvm::TargetOptions Options = codegen::InitTargetOptionsFromCodeGenFlags();
2111
2112 // Create a local context. Ownership will be transferred to LTOModule.
2113 std::unique_ptr<LLVMContext> Context = std::make_unique<LLVMContext>();
2114@@ -294,7 +293,7 @@ lto_module_t lto_module_create_in_codegen_context(const void *mem,
2115 const char *path,
2116 lto_code_gen_t cg) {
2117 lto_initialize();
2118- llvm::TargetOptions Options = InitTargetOptionsFromCodeGenFlags();
2119+ llvm::TargetOptions Options = codegen::InitTargetOptionsFromCodeGenFlags();
2120 ErrorOr<std::unique_ptr<LTOModule>> M = LTOModule::createFromBuffer(
2121 unwrap(cg)->getContext(), mem, length, Options, StringRef(path));
2122 return wrap(M->release());
2123@@ -336,7 +335,7 @@ void lto_codegen_set_diagnostic_handler(lto_code_gen_t cg,
2124 static lto_code_gen_t createCodeGen(bool InLocalContext) {
2125 lto_initialize();
2126
2127- TargetOptions Options = InitTargetOptionsFromCodeGenFlags();
2128+ TargetOptions Options = codegen::InitTargetOptionsFromCodeGenFlags();
2129
2130 LibLTOCodeGenerator *CodeGen =
2131 InLocalContext ? new LibLTOCodeGenerator(std::make_unique<LLVMContext>())
2132@@ -484,7 +483,7 @@ void lto_codegen_set_should_embed_uselists(lto_code_gen_t cg,
2133 thinlto_code_gen_t thinlto_create_codegen(void) {
2134 lto_initialize();
2135 ThinLTOCodeGenerator *CodeGen = new ThinLTOCodeGenerator();
2136- CodeGen->setTargetOptions(InitTargetOptionsFromCodeGenFlags());
2137+ CodeGen->setTargetOptions(codegen::InitTargetOptionsFromCodeGenFlags());
2138 CodeGen->setFreestanding(EnableFreestanding);
2139
2140 if (OptLevel.getNumOccurrences()) {
2141diff --git a/llvm/tools/opt/opt.cpp b/llvm/tools/opt/opt.cpp
2142index 75a6cdc3892b..8d619ef76b7a 100644
2143--- a/llvm/tools/opt/opt.cpp
2144+++ b/llvm/tools/opt/opt.cpp
2145@@ -22,7 +22,7 @@
2146 #include "llvm/Analysis/TargetLibraryInfo.h"
2147 #include "llvm/Analysis/TargetTransformInfo.h"
2148 #include "llvm/Bitcode/BitcodeWriterPass.h"
2149-#include "llvm/CodeGen/CommandFlags.inc"
2150+#include "llvm/CodeGen/CommandFlags.h"
2151 #include "llvm/CodeGen/TargetPassConfig.h"
2152 #include "llvm/Config/llvm-config.h"
2153 #include "llvm/IR/DataLayout.h"
2154@@ -61,6 +61,8 @@
2155 using namespace llvm;
2156 using namespace opt_tool;
2157
2158+static codegen::RegisterCodeGenFlags CFG;
2159+
2160 // The OptimizationList is automatically populated with registered Passes by the
2161 // PassNameParser.
2162 //
2163@@ -470,16 +472,17 @@ static TargetMachine* GetTargetMachine(Triple TheTriple, StringRef CPUStr,
2164 StringRef FeaturesStr,
2165 const TargetOptions &Options) {
2166 std::string Error;
2167- const Target *TheTarget = TargetRegistry::lookupTarget(MArch, TheTriple,
2168- Error);
2169+ const Target *TheTarget =
2170+ TargetRegistry::lookupTarget(codegen::getMArch(), TheTriple, Error);
2171 // Some modules don't specify a triple, and this is okay.
2172 if (!TheTarget) {
2173 return nullptr;
2174 }
2175
2176- return TheTarget->createTargetMachine(TheTriple.getTriple(), CPUStr,
2177- FeaturesStr, Options, getRelocModel(),
2178- getCodeModel(), GetCodeGenOptLevel());
2179+ return TheTarget->createTargetMachine(
2180+ TheTriple.getTriple(), codegen::getCPUStr(), codegen::getFeaturesStr(),
2181+ Options, codegen::getExplicitRelocModel(),
2182+ codegen::getExplicitCodeModel(), GetCodeGenOptLevel());
2183 }
2184
2185 #ifdef BUILD_EXAMPLES
2186@@ -659,11 +662,11 @@ int main(int argc, char **argv) {
2187 Triple ModuleTriple(M->getTargetTriple());
2188 std::string CPUStr, FeaturesStr;
2189 TargetMachine *Machine = nullptr;
2190- const TargetOptions Options = InitTargetOptionsFromCodeGenFlags();
2191+ const TargetOptions Options = codegen::InitTargetOptionsFromCodeGenFlags();
2192
2193 if (ModuleTriple.getArch()) {
2194- CPUStr = getCPUStr();
2195- FeaturesStr = getFeaturesStr();
2196+ CPUStr = codegen::getCPUStr();
2197+ FeaturesStr = codegen::getFeaturesStr();
2198 Machine = GetTargetMachine(ModuleTriple, CPUStr, FeaturesStr, Options);
2199 } else if (ModuleTriple.getArchName() != "unknown" &&
2200 ModuleTriple.getArchName() != "") {
2201@@ -676,7 +679,7 @@ int main(int argc, char **argv) {
2202
2203 // Override function attributes based on CPUStr, FeaturesStr, and command line
2204 // flags.
2205- setFunctionAttributes(CPUStr, FeaturesStr, *M);
2206+ codegen::setFunctionAttributes(CPUStr, FeaturesStr, *M);
2207
2208 // If the output is set to be emitted to standard out, and standard out is a
2209 // console, print out a warning message and refuse to do it. We don't
2210diff --git a/llvm/unittests/DebugInfo/DWARF/DwarfGenerator.cpp b/llvm/unittests/DebugInfo/DWARF/DwarfGenerator.cpp
2211index 472d4dd6ad1e..32d1d3c91ff2 100644
2212--- a/llvm/unittests/DebugInfo/DWARF/DwarfGenerator.cpp
2213+++ b/llvm/unittests/DebugInfo/DWARF/DwarfGenerator.cpp
2214@@ -25,7 +25,7 @@
2215 #include "llvm/MC/MCRegisterInfo.h"
2216 #include "llvm/MC/MCStreamer.h"
2217 #include "llvm/MC/MCSubtargetInfo.h"
2218-#include "llvm/MC/MCTargetOptionsCommandFlags.inc"
2219+#include "llvm/MC/MCTargetOptionsCommandFlags.h"
2220 #include "llvm/PassAnalysisSupport.h"
2221 #include "llvm/Support/TargetRegistry.h"
2222 #include "llvm/Support/raw_ostream.h"
2223@@ -36,6 +36,8 @@
2224 using namespace llvm;
2225 using namespace dwarf;
2226
2227+mc::RegisterMCTargetOptionsFlags MOF;
2228+
2229 namespace {} // end anonymous namespace
2230
2231 //===----------------------------------------------------------------------===//
2232@@ -410,7 +412,7 @@ llvm::Error dwarfgen::Generator::init(Triple TheTriple, uint16_t V) {
2233 TripleName,
2234 inconvertibleErrorCode());
2235
2236- MCTargetOptions MCOptions = InitMCTargetOptionsFromFlags();
2237+ MCTargetOptions MCOptions = mc::InitMCTargetOptionsFromFlags();
2238 MAI.reset(TheTarget->createMCAsmInfo(*MRI, TripleName, MCOptions));
2239 if (!MAI)
2240 return make_error<StringError>("no asm info for target " + TripleName,
2241--
22422.33.1
2243