1//===-- CommandFlags.cpp - Command Line Flags Interface ---------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file contains codegen-specific flags that are shared between different
10// command line tools. The tools "llc" and "opt" both use this file to prevent
11// flag duplication.
12//
13//===----------------------------------------------------------------------===//
14
15#include "llvm/CodeGen/CommandFlags.h"
16#include "llvm/ADT/SmallString.h"
17#include "llvm/ADT/Statistic.h"
18#include "llvm/ADT/StringExtras.h"
19#include "llvm/ADT/StringRef.h"
20#include "llvm/IR/Instructions.h"
21#include "llvm/IR/Intrinsics.h"
22#include "llvm/IR/Module.h"
23#include "llvm/MC/MCTargetOptionsCommandFlags.h"
24#include "llvm/MC/TargetRegistry.h"
25#include "llvm/Support/CommandLine.h"
26#include "llvm/Support/FileSystem.h"
27#include "llvm/Support/MemoryBuffer.h"
28#include "llvm/Support/Path.h"
29#include "llvm/Support/WithColor.h"
30#include "llvm/Support/raw_ostream.h"
31#include "llvm/Target/TargetMachine.h"
32#include "llvm/TargetParser/Host.h"
33#include "llvm/TargetParser/SubtargetFeature.h"
34#include "llvm/TargetParser/Triple.h"
35#include <cassert>
36#include <memory>
37#include <optional>
38#include <system_error>
39
40using namespace llvm;
41
42#define CGOPT(TY, NAME) \
43 static cl::opt<TY> *NAME##View; \
44 TY codegen::get##NAME() { \
45 assert(NAME##View && "Flag not registered."); \
46 return *NAME##View; \
47 }
48
49#define CGLIST(TY, NAME) \
50 static cl::list<TY> *NAME##View; \
51 std::vector<TY> codegen::get##NAME() { \
52 assert(NAME##View && "Flag not registered."); \
53 return *NAME##View; \
54 }
55
56// Temporary macro for incremental transition to std::optional.
57#define CGOPT_EXP(TY, NAME) \
58 CGOPT(TY, NAME) \
59 std::optional<TY> codegen::getExplicit##NAME() { \
60 if (NAME##View->getNumOccurrences()) { \
61 TY res = *NAME##View; \
62 return res; \
63 } \
64 return std::nullopt; \
65 }
66
67CGOPT(std::string, MArch)
68CGOPT(std::string, MCPU)
69CGOPT(std::string, MTune)
70CGLIST(std::string, MAttrs)
71CGOPT_EXP(Reloc::Model, RelocModel)
72CGOPT(ThreadModel::Model, ThreadModel)
73CGOPT_EXP(CodeModel::Model, CodeModel)
74CGOPT_EXP(uint64_t, LargeDataThreshold)
75CGOPT(ExceptionHandling, ExceptionModel)
76CGOPT_EXP(CodeGenFileType, FileType)
77CGOPT(FramePointerKind, FramePointerUsage)
78CGOPT(bool, EnableNoTrappingFPMath)
79CGOPT(bool, EnableAIXExtendedAltivecABI)
80CGOPT(DenormalMode::DenormalModeKind, DenormalFPMath)
81CGOPT(DenormalMode::DenormalModeKind, DenormalFP32Math)
82CGOPT(bool, EnableHonorSignDependentRoundingFPMath)
83CGOPT(FloatABI::ABIType, FloatABIForCalls)
84CGOPT(FPOpFusion::FPOpFusionMode, FuseFPOps)
85CGOPT(SwiftAsyncFramePointerMode, SwiftAsyncFramePointer)
86CGOPT(bool, DontPlaceZerosInBSS)
87CGOPT(bool, EnableGuaranteedTailCallOpt)
88CGOPT(bool, DisableTailCalls)
89CGOPT(bool, StackSymbolOrdering)
90CGOPT(bool, StackRealign)
91CGOPT(std::string, TrapFuncName)
92CGOPT(bool, UseCtors)
93CGOPT(bool, DisableIntegratedAS)
94CGOPT_EXP(bool, DataSections)
95CGOPT_EXP(bool, FunctionSections)
96CGOPT(bool, IgnoreXCOFFVisibility)
97CGOPT(bool, XCOFFTracebackTable)
98CGOPT(bool, EnableBBAddrMap)
99CGOPT(std::string, BBSections)
100CGOPT(unsigned, TLSSize)
101CGOPT_EXP(bool, EmulatedTLS)
102CGOPT_EXP(bool, EnableTLSDESC)
103CGOPT(bool, UniqueSectionNames)
104CGOPT(bool, UniqueBasicBlockSectionNames)
105CGOPT(bool, SeparateNamedSections)
106CGOPT(EABI, EABIVersion)
107CGOPT(DebuggerKind, DebuggerTuningOpt)
108CGOPT(VectorLibrary, VectorLibrary)
109CGOPT(bool, EnableStackSizeSection)
110CGOPT(bool, EnableAddrsig)
111CGOPT(bool, EnableCallGraphSection)
112CGOPT(bool, EmitCallSiteInfo)
113CGOPT(bool, EnableMachineFunctionSplitter)
114CGOPT(bool, EnableStaticDataPartitioning)
115CGOPT(bool, EnableDebugEntryValues)
116CGOPT(bool, ForceDwarfFrameSection)
117CGOPT(bool, XRayFunctionIndex)
118CGOPT(bool, DebugStrictDwarf)
119CGOPT(unsigned, AlignLoops)
120CGOPT(bool, JMCInstrument)
121CGOPT(bool, XCOFFReadOnlyPointers)
122CGOPT(codegen::SaveStatsMode, SaveStats)
123
124#define CGBINDOPT(NAME) \
125 do { \
126 NAME##View = std::addressof(NAME); \
127 } while (0)
128
129codegen::RegisterCodeGenFlags::RegisterCodeGenFlags() {
130 static cl::opt<std::string> MArch(
131 "march", cl::desc("Architecture to generate code for (see --version)"));
132 CGBINDOPT(MArch);
133
134 static cl::opt<std::string> MCPU(
135 "mcpu", cl::desc("Target a specific cpu type (-mcpu=help for details)"),
136 cl::value_desc("cpu-name"), cl::init(Val: ""));
137 CGBINDOPT(MCPU);
138
139 static cl::list<std::string> MAttrs(
140 "mattr", cl::CommaSeparated,
141 cl::desc("Target specific attributes (-mattr=help for details)"),
142 cl::value_desc("a1,+a2,-a3,..."));
143 CGBINDOPT(MAttrs);
144
145 static cl::opt<Reloc::Model> RelocModel(
146 "relocation-model", cl::desc("Choose relocation model"),
147 cl::values(
148 clEnumValN(Reloc::Static, "static", "Non-relocatable code"),
149 clEnumValN(Reloc::PIC_, "pic",
150 "Fully relocatable, position independent code"),
151 clEnumValN(Reloc::DynamicNoPIC, "dynamic-no-pic",
152 "Relocatable external references, non-relocatable code"),
153 clEnumValN(
154 Reloc::ROPI, "ropi",
155 "Code and read-only data relocatable, accessed PC-relative"),
156 clEnumValN(
157 Reloc::RWPI, "rwpi",
158 "Read-write data relocatable, accessed relative to static base"),
159 clEnumValN(Reloc::ROPI_RWPI, "ropi-rwpi",
160 "Combination of ropi and rwpi")));
161 CGBINDOPT(RelocModel);
162
163 static cl::opt<ThreadModel::Model> ThreadModel(
164 "thread-model", cl::desc("Choose threading model"),
165 cl::init(Val: ThreadModel::POSIX),
166 cl::values(
167 clEnumValN(ThreadModel::POSIX, "posix", "POSIX thread model"),
168 clEnumValN(ThreadModel::Single, "single", "Single thread model")));
169 CGBINDOPT(ThreadModel);
170
171 static cl::opt<CodeModel::Model> CodeModel(
172 "code-model", cl::desc("Choose code model"),
173 cl::values(clEnumValN(CodeModel::Tiny, "tiny", "Tiny code model"),
174 clEnumValN(CodeModel::Small, "small", "Small code model"),
175 clEnumValN(CodeModel::Kernel, "kernel", "Kernel code model"),
176 clEnumValN(CodeModel::Medium, "medium", "Medium code model"),
177 clEnumValN(CodeModel::Large, "large", "Large code model")));
178 CGBINDOPT(CodeModel);
179
180 static cl::opt<uint64_t> LargeDataThreshold(
181 "large-data-threshold",
182 cl::desc("Choose large data threshold for x86_64 medium code model"),
183 cl::init(Val: 0));
184 CGBINDOPT(LargeDataThreshold);
185
186 static cl::opt<ExceptionHandling> ExceptionModel(
187 "exception-model", cl::desc("exception model"),
188 cl::init(Val: ExceptionHandling::None),
189 cl::values(
190 clEnumValN(ExceptionHandling::None, "default",
191 "default exception handling model"),
192 clEnumValN(ExceptionHandling::DwarfCFI, "dwarf",
193 "DWARF-like CFI based exception handling"),
194 clEnumValN(ExceptionHandling::SjLj, "sjlj",
195 "SjLj exception handling"),
196 clEnumValN(ExceptionHandling::ARM, "arm", "ARM EHABI exceptions"),
197 clEnumValN(ExceptionHandling::WinEH, "wineh",
198 "Windows exception model"),
199 clEnumValN(ExceptionHandling::Wasm, "wasm",
200 "WebAssembly exception handling")));
201 CGBINDOPT(ExceptionModel);
202
203 static cl::opt<CodeGenFileType> FileType(
204 "filetype", cl::init(Val: CodeGenFileType::AssemblyFile),
205 cl::desc(
206 "Choose a file type (not all types are supported by all targets):"),
207 cl::values(clEnumValN(CodeGenFileType::AssemblyFile, "asm",
208 "Emit an assembly ('.s') file"),
209 clEnumValN(CodeGenFileType::ObjectFile, "obj",
210 "Emit a native object ('.o') file"),
211 clEnumValN(CodeGenFileType::Null, "null",
212 "Emit nothing, for performance testing")));
213 CGBINDOPT(FileType);
214
215 static cl::opt<FramePointerKind> FramePointerUsage(
216 "frame-pointer",
217 cl::desc("Specify frame pointer elimination optimization"),
218 cl::init(Val: FramePointerKind::None),
219 cl::values(
220 clEnumValN(FramePointerKind::All, "all",
221 "Disable frame pointer elimination"),
222 clEnumValN(FramePointerKind::NonLeaf, "non-leaf",
223 "Disable frame pointer elimination for non-leaf frame but "
224 "reserve the register in leaf functions"),
225 clEnumValN(FramePointerKind::NonLeafNoReserve, "non-leaf-no-reserve",
226 "Disable frame pointer elimination for non-leaf frame"),
227 clEnumValN(FramePointerKind::Reserved, "reserved",
228 "Enable frame pointer elimination, but reserve the frame "
229 "pointer register"),
230 clEnumValN(FramePointerKind::None, "none",
231 "Enable frame pointer elimination")));
232 CGBINDOPT(FramePointerUsage);
233
234 static cl::opt<bool> EnableNoTrappingFPMath(
235 "enable-no-trapping-fp-math",
236 cl::desc("Enable setting the FP exceptions build "
237 "attribute not to use exceptions"),
238 cl::init(Val: false));
239 CGBINDOPT(EnableNoTrappingFPMath);
240
241 static const auto DenormFlagEnumOptions = cl::values(
242 clEnumValN(DenormalMode::IEEE, "ieee", "IEEE 754 denormal numbers"),
243 clEnumValN(DenormalMode::PreserveSign, "preserve-sign",
244 "the sign of a flushed-to-zero number is preserved "
245 "in the sign of 0"),
246 clEnumValN(DenormalMode::PositiveZero, "positive-zero",
247 "denormals are flushed to positive zero"),
248 clEnumValN(DenormalMode::Dynamic, "dynamic",
249 "denormals have unknown treatment"));
250
251 // FIXME: Doesn't have way to specify separate input and output modes.
252 static cl::opt<DenormalMode::DenormalModeKind> DenormalFPMath(
253 "denormal-fp-math",
254 cl::desc("Select which denormal numbers the code is permitted to require"),
255 cl::init(Val: DenormalMode::IEEE),
256 DenormFlagEnumOptions);
257 CGBINDOPT(DenormalFPMath);
258
259 static cl::opt<DenormalMode::DenormalModeKind> DenormalFP32Math(
260 "denormal-fp-math-f32",
261 cl::desc("Select which denormal numbers the code is permitted to require for float"),
262 cl::init(Val: DenormalMode::Invalid),
263 DenormFlagEnumOptions);
264 CGBINDOPT(DenormalFP32Math);
265
266 static cl::opt<bool> EnableHonorSignDependentRoundingFPMath(
267 "enable-sign-dependent-rounding-fp-math", cl::Hidden,
268 cl::desc("Force codegen to assume rounding mode can change dynamically"),
269 cl::init(Val: false));
270 CGBINDOPT(EnableHonorSignDependentRoundingFPMath);
271
272 static cl::opt<FloatABI::ABIType> FloatABIForCalls(
273 "float-abi", cl::desc("Choose float ABI type"),
274 cl::init(Val: FloatABI::Default),
275 cl::values(clEnumValN(FloatABI::Default, "default",
276 "Target default float ABI type"),
277 clEnumValN(FloatABI::Soft, "soft",
278 "Soft float ABI (implied by -soft-float)"),
279 clEnumValN(FloatABI::Hard, "hard",
280 "Hard float ABI (uses FP registers)")));
281 CGBINDOPT(FloatABIForCalls);
282
283 static cl::opt<FPOpFusion::FPOpFusionMode> FuseFPOps(
284 "fp-contract", cl::desc("Enable aggressive formation of fused FP ops"),
285 cl::init(Val: FPOpFusion::Standard),
286 cl::values(
287 clEnumValN(FPOpFusion::Fast, "fast",
288 "Fuse FP ops whenever profitable"),
289 clEnumValN(FPOpFusion::Standard, "on", "Only fuse 'blessed' FP ops."),
290 clEnumValN(FPOpFusion::Strict, "off",
291 "Only fuse FP ops when the result won't be affected.")));
292 CGBINDOPT(FuseFPOps);
293
294 static cl::opt<SwiftAsyncFramePointerMode> SwiftAsyncFramePointer(
295 "swift-async-fp",
296 cl::desc("Determine when the Swift async frame pointer should be set"),
297 cl::init(Val: SwiftAsyncFramePointerMode::Always),
298 cl::values(clEnumValN(SwiftAsyncFramePointerMode::DeploymentBased, "auto",
299 "Determine based on deployment target"),
300 clEnumValN(SwiftAsyncFramePointerMode::Always, "always",
301 "Always set the bit"),
302 clEnumValN(SwiftAsyncFramePointerMode::Never, "never",
303 "Never set the bit")));
304 CGBINDOPT(SwiftAsyncFramePointer);
305
306 static cl::opt<bool> DontPlaceZerosInBSS(
307 "nozero-initialized-in-bss",
308 cl::desc("Don't place zero-initialized symbols into bss section"),
309 cl::init(Val: false));
310 CGBINDOPT(DontPlaceZerosInBSS);
311
312 static cl::opt<bool> EnableAIXExtendedAltivecABI(
313 "vec-extabi", cl::desc("Enable the AIX Extended Altivec ABI."),
314 cl::init(Val: false));
315 CGBINDOPT(EnableAIXExtendedAltivecABI);
316
317 static cl::opt<bool> EnableGuaranteedTailCallOpt(
318 "tailcallopt",
319 cl::desc(
320 "Turn fastcc calls into tail calls by (potentially) changing ABI."),
321 cl::init(Val: false));
322 CGBINDOPT(EnableGuaranteedTailCallOpt);
323
324 static cl::opt<bool> DisableTailCalls(
325 "disable-tail-calls", cl::desc("Never emit tail calls"), cl::init(Val: false));
326 CGBINDOPT(DisableTailCalls);
327
328 static cl::opt<bool> StackSymbolOrdering(
329 "stack-symbol-ordering", cl::desc("Order local stack symbols."),
330 cl::init(Val: true));
331 CGBINDOPT(StackSymbolOrdering);
332
333 static cl::opt<bool> StackRealign(
334 "stackrealign",
335 cl::desc("Force align the stack to the minimum alignment"),
336 cl::init(Val: false));
337 CGBINDOPT(StackRealign);
338
339 static cl::opt<std::string> TrapFuncName(
340 "trap-func", cl::Hidden,
341 cl::desc("Emit a call to trap function rather than a trap instruction"),
342 cl::init(Val: ""));
343 CGBINDOPT(TrapFuncName);
344
345 static cl::opt<bool> UseCtors("use-ctors",
346 cl::desc("Use .ctors instead of .init_array."),
347 cl::init(Val: false));
348 CGBINDOPT(UseCtors);
349
350 static cl::opt<bool> DataSections(
351 "data-sections", cl::desc("Emit data into separate sections"),
352 cl::init(Val: false));
353 CGBINDOPT(DataSections);
354
355 static cl::opt<bool> FunctionSections(
356 "function-sections", cl::desc("Emit functions into separate sections"),
357 cl::init(Val: false));
358 CGBINDOPT(FunctionSections);
359
360 static cl::opt<bool> IgnoreXCOFFVisibility(
361 "ignore-xcoff-visibility",
362 cl::desc("Not emit the visibility attribute for asm in AIX OS or give "
363 "all symbols 'unspecified' visibility in XCOFF object file"),
364 cl::init(Val: false));
365 CGBINDOPT(IgnoreXCOFFVisibility);
366
367 static cl::opt<bool> XCOFFTracebackTable(
368 "xcoff-traceback-table", cl::desc("Emit the XCOFF traceback table"),
369 cl::init(Val: true));
370 CGBINDOPT(XCOFFTracebackTable);
371
372 static cl::opt<bool> EnableBBAddrMap(
373 "basic-block-address-map",
374 cl::desc("Emit the basic block address map section"), cl::init(Val: false));
375 CGBINDOPT(EnableBBAddrMap);
376
377 static cl::opt<std::string> BBSections(
378 "basic-block-sections",
379 cl::desc("Emit basic blocks into separate sections"),
380 cl::value_desc("all | <function list (file)> | labels | none"),
381 cl::init(Val: "none"));
382 CGBINDOPT(BBSections);
383
384 static cl::opt<unsigned> TLSSize(
385 "tls-size", cl::desc("Bit size of immediate TLS offsets"), cl::init(Val: 0));
386 CGBINDOPT(TLSSize);
387
388 static cl::opt<bool> EmulatedTLS(
389 "emulated-tls", cl::desc("Use emulated TLS model"), cl::init(Val: false));
390 CGBINDOPT(EmulatedTLS);
391
392 static cl::opt<bool> EnableTLSDESC(
393 "enable-tlsdesc", cl::desc("Enable the use of TLS Descriptors"),
394 cl::init(Val: false));
395 CGBINDOPT(EnableTLSDESC);
396
397 static cl::opt<bool> UniqueSectionNames(
398 "unique-section-names", cl::desc("Give unique names to every section"),
399 cl::init(Val: true));
400 CGBINDOPT(UniqueSectionNames);
401
402 static cl::opt<bool> UniqueBasicBlockSectionNames(
403 "unique-basic-block-section-names",
404 cl::desc("Give unique names to every basic block section"),
405 cl::init(Val: false));
406 CGBINDOPT(UniqueBasicBlockSectionNames);
407
408 static cl::opt<bool> SeparateNamedSections(
409 "separate-named-sections",
410 cl::desc("Use separate unique sections for named sections"),
411 cl::init(Val: false));
412 CGBINDOPT(SeparateNamedSections);
413
414 static cl::opt<EABI> EABIVersion(
415 "meabi", cl::desc("Set EABI type (default depends on triple):"),
416 cl::init(Val: EABI::Default),
417 cl::values(
418 clEnumValN(EABI::Default, "default", "Triple default EABI version"),
419 clEnumValN(EABI::EABI4, "4", "EABI version 4"),
420 clEnumValN(EABI::EABI5, "5", "EABI version 5"),
421 clEnumValN(EABI::GNU, "gnu", "EABI GNU")));
422 CGBINDOPT(EABIVersion);
423
424 static cl::opt<DebuggerKind> DebuggerTuningOpt(
425 "debugger-tune", cl::desc("Tune debug info for a particular debugger"),
426 cl::init(Val: DebuggerKind::Default),
427 cl::values(
428 clEnumValN(DebuggerKind::GDB, "gdb", "gdb"),
429 clEnumValN(DebuggerKind::LLDB, "lldb", "lldb"),
430 clEnumValN(DebuggerKind::DBX, "dbx", "dbx"),
431 clEnumValN(DebuggerKind::SCE, "sce", "SCE targets (e.g. PS4)")));
432 CGBINDOPT(DebuggerTuningOpt);
433
434 static cl::opt<VectorLibrary> VectorLibrary(
435 "vector-library", cl::Hidden, cl::desc("Vector functions library"),
436 cl::init(Val: VectorLibrary::NoLibrary),
437 cl::values(
438 clEnumValN(VectorLibrary::NoLibrary, "none",
439 "No vector functions library"),
440 clEnumValN(VectorLibrary::Accelerate, "Accelerate",
441 "Accelerate framework"),
442 clEnumValN(VectorLibrary::DarwinLibSystemM, "Darwin_libsystem_m",
443 "Darwin libsystem_m"),
444 clEnumValN(VectorLibrary::LIBMVEC, "LIBMVEC",
445 "GLIBC Vector Math library"),
446 clEnumValN(VectorLibrary::MASSV, "MASSV", "IBM MASS vector library"),
447 clEnumValN(VectorLibrary::SVML, "SVML", "Intel SVML library"),
448 clEnumValN(VectorLibrary::SLEEFGNUABI, "sleefgnuabi",
449 "SIMD Library for Evaluating Elementary Functions"),
450 clEnumValN(VectorLibrary::ArmPL, "ArmPL",
451 "Arm Performance Libraries"),
452 clEnumValN(VectorLibrary::AMDLIBM, "AMDLIBM",
453 "AMD vector math library")));
454 CGBINDOPT(VectorLibrary);
455
456 static cl::opt<bool> EnableStackSizeSection(
457 "stack-size-section",
458 cl::desc("Emit a section containing stack size metadata"),
459 cl::init(Val: false));
460 CGBINDOPT(EnableStackSizeSection);
461
462 static cl::opt<bool> EnableAddrsig(
463 "addrsig", cl::desc("Emit an address-significance table"),
464 cl::init(Val: false));
465 CGBINDOPT(EnableAddrsig);
466
467 static cl::opt<bool> EnableCallGraphSection(
468 "call-graph-section", cl::desc("Emit a call graph section"),
469 cl::init(Val: false));
470 CGBINDOPT(EnableCallGraphSection);
471
472 static cl::opt<bool> EmitCallSiteInfo(
473 "emit-call-site-info",
474 cl::desc(
475 "Emit call site debug information, if debug information is enabled."),
476 cl::init(Val: false));
477 CGBINDOPT(EmitCallSiteInfo);
478
479 static cl::opt<bool> EnableDebugEntryValues(
480 "debug-entry-values",
481 cl::desc("Enable debug info for the debug entry values."),
482 cl::init(Val: false));
483 CGBINDOPT(EnableDebugEntryValues);
484
485 static cl::opt<bool> EnableMachineFunctionSplitter(
486 "split-machine-functions",
487 cl::desc("Split out cold basic blocks from machine functions based on "
488 "profile information"),
489 cl::init(Val: false));
490 CGBINDOPT(EnableMachineFunctionSplitter);
491
492 static cl::opt<bool> EnableStaticDataPartitioning(
493 "partition-static-data-sections",
494 cl::desc("Partition data sections using profile information."),
495 cl::init(Val: false));
496 CGBINDOPT(EnableStaticDataPartitioning);
497
498 static cl::opt<bool> ForceDwarfFrameSection(
499 "force-dwarf-frame-section",
500 cl::desc("Always emit a debug frame section."), cl::init(Val: false));
501 CGBINDOPT(ForceDwarfFrameSection);
502
503 static cl::opt<bool> XRayFunctionIndex("xray-function-index",
504 cl::desc("Emit xray_fn_idx section"),
505 cl::init(Val: true));
506 CGBINDOPT(XRayFunctionIndex);
507
508 static cl::opt<bool> DebugStrictDwarf(
509 "strict-dwarf", cl::desc("use strict dwarf"), cl::init(Val: false));
510 CGBINDOPT(DebugStrictDwarf);
511
512 static cl::opt<unsigned> AlignLoops("align-loops",
513 cl::desc("Default alignment for loops"));
514 CGBINDOPT(AlignLoops);
515
516 static cl::opt<bool> JMCInstrument(
517 "enable-jmc-instrument",
518 cl::desc("Instrument functions with a call to __CheckForDebuggerJustMyCode"),
519 cl::init(Val: false));
520 CGBINDOPT(JMCInstrument);
521
522 static cl::opt<bool> XCOFFReadOnlyPointers(
523 "mxcoff-roptr",
524 cl::desc("When set to true, const objects with relocatable address "
525 "values are put into the RO data section."),
526 cl::init(Val: false));
527 CGBINDOPT(XCOFFReadOnlyPointers);
528
529 static cl::opt<bool> DisableIntegratedAS(
530 "no-integrated-as", cl::desc("Disable integrated assembler"),
531 cl::init(Val: false));
532 CGBINDOPT(DisableIntegratedAS);
533
534 mc::RegisterMCTargetOptionsFlags();
535}
536
537codegen::RegisterMTuneFlag::RegisterMTuneFlag() {
538 static cl::opt<std::string> MTune(
539 "mtune",
540 cl::desc("Tune for a specific CPU microarchitecture (-mtune=help for "
541 "details)"),
542 cl::value_desc("tune-cpu-name"), cl::init(Val: ""));
543 CGBINDOPT(MTune);
544}
545
546codegen::RegisterSaveStatsFlag::RegisterSaveStatsFlag() {
547 static cl::opt<SaveStatsMode> SaveStats(
548 "save-stats",
549 cl::desc(
550 "Save LLVM statistics to a file in the current directory"
551 "(`-save-stats`/`-save-stats=cwd`) or the directory of the output"
552 "file (`-save-stats=obj`). (default: cwd)"),
553 cl::values(clEnumValN(SaveStatsMode::Cwd, "cwd",
554 "Save to the current working directory"),
555 clEnumValN(SaveStatsMode::Cwd, "", ""),
556 clEnumValN(SaveStatsMode::Obj, "obj",
557 "Save to the output file directory")),
558 cl::init(Val: SaveStatsMode::None), cl::ValueOptional);
559 CGBINDOPT(SaveStats);
560}
561
562llvm::BasicBlockSection
563codegen::getBBSectionsMode(llvm::TargetOptions &Options) {
564 if (getBBSections() == "all")
565 return BasicBlockSection::All;
566 else if (getBBSections() == "none")
567 return BasicBlockSection::None;
568 else {
569 ErrorOr<std::unique_ptr<MemoryBuffer>> MBOrErr =
570 MemoryBuffer::getFile(Filename: getBBSections());
571 if (!MBOrErr) {
572 errs() << "Error loading basic block sections function list file: "
573 << MBOrErr.getError().message() << "\n";
574 } else {
575 Options.BBSectionsFuncListBuf = std::move(*MBOrErr);
576 }
577 return BasicBlockSection::List;
578 }
579}
580
581// Common utility function tightly tied to the options listed here. Initializes
582// a TargetOptions object with CodeGen flags and returns it.
583TargetOptions
584codegen::InitTargetOptionsFromCodeGenFlags(const Triple &TheTriple) {
585 TargetOptions Options;
586 Options.AllowFPOpFusion = getFuseFPOps();
587 Options.NoTrappingFPMath = getEnableNoTrappingFPMath();
588
589 Options.HonorSignDependentRoundingFPMathOption =
590 getEnableHonorSignDependentRoundingFPMath();
591 if (getFloatABIForCalls() != FloatABI::Default)
592 Options.FloatABIType = getFloatABIForCalls();
593 Options.EnableAIXExtendedAltivecABI = getEnableAIXExtendedAltivecABI();
594 Options.NoZerosInBSS = getDontPlaceZerosInBSS();
595 Options.GuaranteedTailCallOpt = getEnableGuaranteedTailCallOpt();
596 Options.StackSymbolOrdering = getStackSymbolOrdering();
597 Options.UseInitArray = !getUseCtors();
598 Options.DisableIntegratedAS = getDisableIntegratedAS();
599 Options.DataSections =
600 getExplicitDataSections().value_or(u: TheTriple.hasDefaultDataSections());
601 Options.FunctionSections = getFunctionSections();
602 Options.IgnoreXCOFFVisibility = getIgnoreXCOFFVisibility();
603 Options.XCOFFTracebackTable = getXCOFFTracebackTable();
604 Options.BBAddrMap = getEnableBBAddrMap();
605 Options.BBSections = getBBSectionsMode(Options);
606 Options.UniqueSectionNames = getUniqueSectionNames();
607 Options.UniqueBasicBlockSectionNames = getUniqueBasicBlockSectionNames();
608 Options.SeparateNamedSections = getSeparateNamedSections();
609 Options.TLSSize = getTLSSize();
610 Options.EmulatedTLS =
611 getExplicitEmulatedTLS().value_or(u: TheTriple.hasDefaultEmulatedTLS());
612 Options.EnableTLSDESC =
613 getExplicitEnableTLSDESC().value_or(u: TheTriple.hasDefaultTLSDESC());
614 Options.ExceptionModel = getExceptionModel();
615 Options.VecLib = getVectorLibrary();
616 Options.EmitStackSizeSection = getEnableStackSizeSection();
617 Options.EnableMachineFunctionSplitter = getEnableMachineFunctionSplitter();
618 Options.EnableStaticDataPartitioning = getEnableStaticDataPartitioning();
619 Options.EmitAddrsig = getEnableAddrsig();
620 Options.EmitCallGraphSection = getEnableCallGraphSection();
621 Options.EmitCallSiteInfo = getEmitCallSiteInfo();
622 Options.EnableDebugEntryValues = getEnableDebugEntryValues();
623 Options.ForceDwarfFrameSection = getForceDwarfFrameSection();
624 Options.XRayFunctionIndex = getXRayFunctionIndex();
625 Options.DebugStrictDwarf = getDebugStrictDwarf();
626 Options.LoopAlignment = getAlignLoops();
627 Options.JMCInstrument = getJMCInstrument();
628 Options.XCOFFReadOnlyPointers = getXCOFFReadOnlyPointers();
629
630 Options.MCOptions = mc::InitMCTargetOptionsFromFlags();
631
632 Options.ThreadModel = getThreadModel();
633 Options.EABIVersion = getEABIVersion();
634 Options.DebuggerTuning = getDebuggerTuningOpt();
635 Options.SwiftAsyncFramePointer = getSwiftAsyncFramePointer();
636 return Options;
637}
638
639std::string codegen::getCPUStr() {
640 std::string MCPU = getMCPU();
641
642 // If user asked for the 'native' CPU, autodetect here. If auto-detection
643 // fails, this will set the CPU to an empty string which tells the target to
644 // pick a basic default.
645 if (MCPU == "native")
646 return std::string(sys::getHostCPUName());
647
648 return MCPU;
649}
650
651std::string codegen::getTuneCPUStr() {
652 std::string TuneCPU = getMTune();
653
654 // If user asked for the 'native' tune CPU, autodetect here. If auto-detection
655 // fails, this will set the tune CPU to an empty string which tells the target
656 // to pick a basic default.
657 if (TuneCPU == "native")
658 return std::string(sys::getHostCPUName());
659
660 return TuneCPU;
661}
662
663std::string codegen::getFeaturesStr() {
664 SubtargetFeatures Features;
665
666 // If user asked for the 'native' CPU, we need to autodetect features.
667 // This is necessary for x86 where the CPU might not support all the
668 // features the autodetected CPU name lists in the target. For example,
669 // not all Sandybridge processors support AVX.
670 if (getMCPU() == "native")
671 for (const auto &[Feature, IsEnabled] : sys::getHostCPUFeatures())
672 Features.AddFeature(String: Feature, Enable: IsEnabled);
673
674 for (auto const &MAttr : getMAttrs())
675 Features.AddFeature(String: MAttr);
676
677 return Features.getString();
678}
679
680std::vector<std::string> codegen::getFeatureList() {
681 SubtargetFeatures Features;
682
683 // If user asked for the 'native' CPU, we need to autodetect features.
684 // This is necessary for x86 where the CPU might not support all the
685 // features the autodetected CPU name lists in the target. For example,
686 // not all Sandybridge processors support AVX.
687 if (getMCPU() == "native")
688 for (const auto &[Feature, IsEnabled] : sys::getHostCPUFeatures())
689 Features.AddFeature(String: Feature, Enable: IsEnabled);
690
691 for (auto const &MAttr : getMAttrs())
692 Features.AddFeature(String: MAttr);
693
694 return Features.getFeatures();
695}
696
697void codegen::renderBoolStringAttr(AttrBuilder &B, StringRef Name, bool Val) {
698 B.addAttribute(A: Name, V: Val ? "true" : "false");
699}
700
701#define HANDLE_BOOL_ATTR(CL, AttrName) \
702 do { \
703 if (CL->getNumOccurrences() > 0 && !F.hasFnAttribute(AttrName)) \
704 renderBoolStringAttr(NewAttrs, AttrName, *CL); \
705 } while (0)
706
707void codegen::setFunctionAttributes(Function &F, StringRef CPU,
708 StringRef Features, StringRef TuneCPU) {
709 auto &Ctx = F.getContext();
710 AttributeList Attrs = F.getAttributes();
711 AttrBuilder NewAttrs(Ctx);
712
713 if (!CPU.empty() && !F.hasFnAttribute(Kind: "target-cpu"))
714 NewAttrs.addAttribute(A: "target-cpu", V: CPU);
715 if (!TuneCPU.empty() && !F.hasFnAttribute(Kind: "tune-cpu"))
716 NewAttrs.addAttribute(A: "tune-cpu", V: TuneCPU);
717 if (!Features.empty()) {
718 // Append the command line features to any that are already on the function.
719 StringRef OldFeatures =
720 F.getFnAttribute(Kind: "target-features").getValueAsString();
721 if (OldFeatures.empty())
722 NewAttrs.addAttribute(A: "target-features", V: Features);
723 else {
724 SmallString<256> Appended(OldFeatures);
725 Appended.push_back(Elt: ',');
726 Appended.append(RHS: Features);
727 NewAttrs.addAttribute(A: "target-features", V: Appended);
728 }
729 }
730 if (FramePointerUsageView->getNumOccurrences() > 0 &&
731 !F.hasFnAttribute(Kind: "frame-pointer")) {
732 if (getFramePointerUsage() == FramePointerKind::All)
733 NewAttrs.addAttribute(A: "frame-pointer", V: "all");
734 else if (getFramePointerUsage() == FramePointerKind::NonLeaf)
735 NewAttrs.addAttribute(A: "frame-pointer", V: "non-leaf");
736 else if (getFramePointerUsage() == FramePointerKind::NonLeafNoReserve)
737 NewAttrs.addAttribute(A: "frame-pointer", V: "non-leaf-no-reserve");
738 else if (getFramePointerUsage() == FramePointerKind::Reserved)
739 NewAttrs.addAttribute(A: "frame-pointer", V: "reserved");
740 else if (getFramePointerUsage() == FramePointerKind::None)
741 NewAttrs.addAttribute(A: "frame-pointer", V: "none");
742 }
743 if (DisableTailCallsView->getNumOccurrences() > 0)
744 NewAttrs.addAttribute(A: "disable-tail-calls",
745 V: toStringRef(B: getDisableTailCalls()));
746 if (getStackRealign())
747 NewAttrs.addAttribute(A: "stackrealign");
748
749 if ((DenormalFPMathView->getNumOccurrences() > 0 ||
750 DenormalFP32MathView->getNumOccurrences() > 0) &&
751 !F.hasFnAttribute(Kind: Attribute::DenormalFPEnv)) {
752 DenormalMode::DenormalModeKind DenormKind = getDenormalFPMath();
753 DenormalMode::DenormalModeKind DenormKindF32 = getDenormalFP32Math();
754
755 DenormalFPEnv FPEnv(DenormalMode{DenormKind, DenormKind},
756 DenormalMode{DenormKindF32, DenormKindF32});
757 // FIXME: Command line flag should expose separate input/output modes.
758 NewAttrs.addDenormalFPEnvAttr(Mode: FPEnv);
759 }
760
761 if (TrapFuncNameView->getNumOccurrences() > 0)
762 for (auto &B : F)
763 for (auto &I : B)
764 if (auto *Call = dyn_cast<CallInst>(Val: &I))
765 if (const auto *F = Call->getCalledFunction())
766 if (F->getIntrinsicID() == Intrinsic::debugtrap ||
767 F->getIntrinsicID() == Intrinsic::trap)
768 Call->addFnAttr(
769 Attr: Attribute::get(Context&: Ctx, Kind: "trap-func-name", Val: getTrapFuncName()));
770
771 // Let NewAttrs override Attrs.
772 F.setAttributes(Attrs.addFnAttributes(C&: Ctx, B: NewAttrs));
773}
774
775void codegen::setFunctionAttributes(Module &M, StringRef CPU,
776 StringRef Features, StringRef TuneCPU) {
777 for (Function &F : M)
778 setFunctionAttributes(F, CPU, Features, TuneCPU);
779}
780
781Expected<std::unique_ptr<TargetMachine>>
782codegen::createTargetMachineForTriple(const Triple &TargetTriple,
783 CodeGenOptLevel OptLevel) {
784 // lookupTarget may mutate the triple, so we need a copy.
785 Triple TheTriple(TargetTriple);
786 std::string Error;
787 const auto *TheTarget =
788 TargetRegistry::lookupTarget(ArchName: codegen::getMArch(), TheTriple, Error);
789 if (!TheTarget)
790 return createStringError(EC: inconvertibleErrorCode(), S: Error);
791 auto *Target = TheTarget->createTargetMachine(
792 TT: TheTriple, CPU: codegen::getCPUStr(), Features: codegen::getFeaturesStr(),
793 Options: codegen::InitTargetOptionsFromCodeGenFlags(TheTriple),
794 RM: codegen::getExplicitRelocModel(), CM: codegen::getExplicitCodeModel(),
795 OL: OptLevel);
796 if (!Target)
797 return createStringError(EC: inconvertibleErrorCode(),
798 S: Twine("could not allocate target machine for ") +
799 TheTriple.str());
800 return std::unique_ptr<TargetMachine>(Target);
801}
802
803Expected<std::unique_ptr<TargetMachine>>
804codegen::createTargetMachineForTriple(StringRef TargetTriple,
805 CodeGenOptLevel OptLevel) {
806 return createTargetMachineForTriple(TargetTriple: Triple(TargetTriple), OptLevel);
807}
808
809void codegen::MaybeEnableStatistics() {
810 if (getSaveStats() == SaveStatsMode::None)
811 return;
812
813 llvm::EnableStatistics(DoPrintOnExit: false);
814}
815
816int codegen::MaybeSaveStatistics(StringRef OutputFilename, StringRef ToolName) {
817 auto SaveStatsValue = getSaveStats();
818 if (SaveStatsValue == codegen::SaveStatsMode::None)
819 return 0;
820
821 SmallString<128> StatsFilename;
822 if (SaveStatsValue == codegen::SaveStatsMode::Obj) {
823 StatsFilename = OutputFilename;
824 llvm::sys::path::remove_filename(path&: StatsFilename);
825 } else {
826 assert(SaveStatsValue == codegen::SaveStatsMode::Cwd &&
827 "Should have been a valid --save-stats value");
828 }
829
830 auto BaseName = llvm::sys::path::filename(path: OutputFilename);
831 llvm::sys::path::append(path&: StatsFilename, a: BaseName);
832 llvm::sys::path::replace_extension(path&: StatsFilename, extension: "stats");
833
834 auto FileFlags = llvm::sys::fs::OF_TextWithCRLF;
835 std::error_code EC;
836 auto StatsOS =
837 std::make_unique<llvm::raw_fd_ostream>(args&: StatsFilename, args&: EC, args&: FileFlags);
838 if (EC) {
839 WithColor::error(OS&: errs(), Prefix: ToolName)
840 << "Unable to open statistics file: " << EC.message() << "\n";
841 return 1;
842 }
843
844 llvm::PrintStatisticsJSON(OS&: *StatsOS);
845 return 0;
846}
847