1 //===-- llvm/Target/TargetOptions.h - Target Options ------------*- 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 defines command line option flags that are shared across various
10 // targets.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #ifndef LLVM_TARGET_TARGETOPTIONS_H
15 #define LLVM_TARGET_TARGETOPTIONS_H
16 
17 #include "llvm/ADT/FloatingPointMode.h"
18 #include "llvm/MC/MCTargetOptions.h"
19 
20 #include <memory>
21 
22 namespace llvm {
23   struct fltSemantics;
24   class MachineFunction;
25   class MemoryBuffer;
26 
27   namespace FloatABI {
28     enum ABIType {
29       Default, // Target-specific (either soft or hard depending on triple, etc).
30       Soft,    // Soft float.
31       Hard     // Hard float.
32     };
33   }
34 
35   namespace FPOpFusion {
36     enum FPOpFusionMode {
37       Fast,     // Enable fusion of FP ops wherever it's profitable.
38       Standard, // Only allow fusion of 'blessed' ops (currently just fmuladd).
39       Strict    // Never fuse FP-ops.
40     };
41   }
42 
43   namespace JumpTable {
44     enum JumpTableType {
45       Single,          // Use a single table for all indirect jumptable calls.
46       Arity,           // Use one table per number of function parameters.
47       Simplified,      // Use one table per function type, with types projected
48                        // into 4 types: pointer to non-function, struct,
49                        // primitive, and function pointer.
50       Full             // Use one table per unique function type
51     };
52   }
53 
54   namespace ThreadModel {
55     enum Model {
56       POSIX,  // POSIX Threads
57       Single  // Single Threaded Environment
58     };
59   }
60 
61   enum class BasicBlockSection {
62     All,    // Use Basic Block Sections for all basic blocks.  A section
63             // for every basic block can significantly bloat object file sizes.
64     List,   // Get list of functions & BBs from a file. Selectively enables
65             // basic block sections for a subset of basic blocks which can be
66             // used to control object size bloats from creating sections.
67     Labels, // Do not use Basic Block Sections but label basic blocks.  This
68             // is useful when associating profile counts from virtual addresses
69             // to basic blocks.
70     Preset, // Similar to list but the blocks are identified by passes which
71             // seek to use Basic Block Sections, e.g. MachineFunctionSplitter.
72             // This option cannot be set via the command line.
73     None    // Do not use Basic Block Sections.
74   };
75 
76   enum class EABI {
77     Unknown,
78     Default, // Default means not specified
79     EABI4,   // Target-specific (either 4, 5 or gnu depending on triple).
80     EABI5,
81     GNU
82   };
83 
84   /// Identify a debugger for "tuning" the debug info.
85   ///
86   /// The "debugger tuning" concept allows us to present a more intuitive
87   /// interface that unpacks into different sets of defaults for the various
88   /// individual feature-flag settings, that suit the preferences of the
89   /// various debuggers.  However, it's worth remembering that debuggers are
90   /// not the only consumers of debug info, and some variations in DWARF might
91   /// better be treated as target/platform issues. Fundamentally,
92   /// o if the feature is useful (or not) to a particular debugger, regardless
93   ///   of the target, that's a tuning decision;
94   /// o if the feature is useful (or not) on a particular platform, regardless
95   ///   of the debugger, that's a target decision.
96   /// It's not impossible to see both factors in some specific case.
97   enum class DebuggerKind {
98     Default, ///< No specific tuning requested.
99     GDB,     ///< Tune debug info for gdb.
100     LLDB,    ///< Tune debug info for lldb.
101     SCE,     ///< Tune debug info for SCE targets (e.g. PS4).
102     DBX      ///< Tune debug info for dbx.
103   };
104 
105   /// Enable abort calls when global instruction selection fails to lower/select
106   /// an instruction.
107   enum class GlobalISelAbortMode {
108     Disable,        // Disable the abort.
109     Enable,         // Enable the abort.
110     DisableWithDiag // Disable the abort but emit a diagnostic on failure.
111   };
112 
113   /// Indicates when and how the Swift async frame pointer bit should be set.
114   enum class SwiftAsyncFramePointerMode {
115     /// Determine whether to set the bit statically or dynamically based
116     /// on the deployment target.
117     DeploymentBased,
118     /// Always set the bit.
119     Always,
120     /// Never set the bit.
121     Never,
122   };
123 
124   class TargetOptions {
125   public:
126     TargetOptions()
127         : UnsafeFPMath(false), NoInfsFPMath(false), NoNaNsFPMath(false),
128           NoTrappingFPMath(true), NoSignedZerosFPMath(false),
129           ApproxFuncFPMath(false), EnableAIXExtendedAltivecABI(false),
130           HonorSignDependentRoundingFPMathOption(false), NoZerosInBSS(false),
131           GuaranteedTailCallOpt(false), StackSymbolOrdering(true),
132           EnableFastISel(false), EnableGlobalISel(false), UseInitArray(false),
133           LowerGlobalDtorsViaCxaAtExit(false), DisableIntegratedAS(false),
134           RelaxELFRelocations(true), FunctionSections(false),
135           DataSections(false), IgnoreXCOFFVisibility(false),
136           XCOFFTracebackTable(true), UniqueSectionNames(true),
137           UniqueBasicBlockSectionNames(false), TrapUnreachable(false),
138           NoTrapAfterNoreturn(false), TLSSize(0), EmulatedTLS(false),
139           ExplicitEmulatedTLS(false), EnableIPRA(false),
140           EmitStackSizeSection(false), EnableMachineOutliner(false),
141           EnableMachineFunctionSplitter(false), SupportsDefaultOutlining(false),
142           EmitAddrsig(false), EmitCallSiteInfo(false),
143           SupportsDebugEntryValues(false), EnableDebugEntryValues(false),
144           ValueTrackingVariableLocations(false), ForceDwarfFrameSection(false),
145           XRayOmitFunctionIndex(false), DebugStrictDwarf(false),
146           Hotpatch(false), PPCGenScalarMASSEntries(false), JMCInstrument(false),
147           EnableCFIFixup(false), MisExpect(false),
148           FPDenormalMode(DenormalMode::IEEE, DenormalMode::IEEE) {}
149 
150     /// DisableFramePointerElim - This returns true if frame pointer elimination
151     /// optimization should be disabled for the given machine function.
152     bool DisableFramePointerElim(const MachineFunction &MF) const;
153 
154     /// If greater than 0, override the default value of
155     /// MCAsmInfo::BinutilsVersion.
156     std::pair<int, int> BinutilsVersion{0, 0};
157 
158     /// UnsafeFPMath - This flag is enabled when the
159     /// -enable-unsafe-fp-math flag is specified on the command line.  When
160     /// this flag is off (the default), the code generator is not allowed to
161     /// produce results that are "less precise" than IEEE allows.  This includes
162     /// use of X86 instructions like FSIN and FCOS instead of libcalls.
163     unsigned UnsafeFPMath : 1;
164 
165     /// NoInfsFPMath - This flag is enabled when the
166     /// -enable-no-infs-fp-math flag is specified on the command line. When
167     /// this flag is off (the default), the code generator is not allowed to
168     /// assume the FP arithmetic arguments and results are never +-Infs.
169     unsigned NoInfsFPMath : 1;
170 
171     /// NoNaNsFPMath - This flag is enabled when the
172     /// -enable-no-nans-fp-math flag is specified on the command line. When
173     /// this flag is off (the default), the code generator is not allowed to
174     /// assume the FP arithmetic arguments and results are never NaNs.
175     unsigned NoNaNsFPMath : 1;
176 
177     /// NoTrappingFPMath - This flag is enabled when the
178     /// -enable-no-trapping-fp-math is specified on the command line. This
179     /// specifies that there are no trap handlers to handle exceptions.
180     unsigned NoTrappingFPMath : 1;
181 
182     /// NoSignedZerosFPMath - This flag is enabled when the
183     /// -enable-no-signed-zeros-fp-math is specified on the command line. This
184     /// specifies that optimizations are allowed to treat the sign of a zero
185     /// argument or result as insignificant.
186     unsigned NoSignedZerosFPMath : 1;
187 
188     /// ApproxFuncFPMath - This flag is enabled when the
189     /// -enable-approx-func-fp-math is specified on the command line. This
190     /// specifies that optimizations are allowed to substitute math functions
191     /// with approximate calculations
192     unsigned ApproxFuncFPMath : 1;
193 
194     /// EnableAIXExtendedAltivecABI - This flag returns true when -vec-extabi is
195     /// specified. The code generator is then able to use both volatile and
196     /// nonvolitle vector registers. When false, the code generator only uses
197     /// volatile vector registers which is the default setting on AIX.
198     unsigned EnableAIXExtendedAltivecABI : 1;
199 
200     /// HonorSignDependentRoundingFPMath - This returns true when the
201     /// -enable-sign-dependent-rounding-fp-math is specified.  If this returns
202     /// false (the default), the code generator is allowed to assume that the
203     /// rounding behavior is the default (round-to-zero for all floating point
204     /// to integer conversions, and round-to-nearest for all other arithmetic
205     /// truncations).  If this is enabled (set to true), the code generator must
206     /// assume that the rounding mode may dynamically change.
207     unsigned HonorSignDependentRoundingFPMathOption : 1;
208     bool HonorSignDependentRoundingFPMath() const;
209 
210     /// NoZerosInBSS - By default some codegens place zero-initialized data to
211     /// .bss section. This flag disables such behaviour (necessary, e.g. for
212     /// crt*.o compiling).
213     unsigned NoZerosInBSS : 1;
214 
215     /// GuaranteedTailCallOpt - This flag is enabled when -tailcallopt is
216     /// specified on the commandline. When the flag is on, participating targets
217     /// will perform tail call optimization on all calls which use the fastcc
218     /// calling convention and which satisfy certain target-independent
219     /// criteria (being at the end of a function, having the same return type
220     /// as their parent function, etc.), using an alternate ABI if necessary.
221     unsigned GuaranteedTailCallOpt : 1;
222 
223     /// StackSymbolOrdering - When true, this will allow CodeGen to order
224     /// the local stack symbols (for code size, code locality, or any other
225     /// heuristics). When false, the local symbols are left in whatever order
226     /// they were generated. Default is true.
227     unsigned StackSymbolOrdering : 1;
228 
229     /// EnableFastISel - This flag enables fast-path instruction selection
230     /// which trades away generated code quality in favor of reducing
231     /// compile time.
232     unsigned EnableFastISel : 1;
233 
234     /// EnableGlobalISel - This flag enables global instruction selection.
235     unsigned EnableGlobalISel : 1;
236 
237     /// EnableGlobalISelAbort - Control abort behaviour when global instruction
238     /// selection fails to lower/select an instruction.
239     GlobalISelAbortMode GlobalISelAbort = GlobalISelAbortMode::Enable;
240 
241     /// Control when and how the Swift async frame pointer bit should
242     /// be set.
243     SwiftAsyncFramePointerMode SwiftAsyncFramePointer =
244         SwiftAsyncFramePointerMode::Always;
245 
246     /// UseInitArray - Use .init_array instead of .ctors for static
247     /// constructors.
248     unsigned UseInitArray : 1;
249 
250     /// Use __cxa_atexit to register global destructors; determines how
251     /// llvm.global_dtors is lowered.
252     unsigned LowerGlobalDtorsViaCxaAtExit : 1;
253 
254     /// Disable the integrated assembler.
255     unsigned DisableIntegratedAS : 1;
256 
257     /// Compress DWARF debug sections.
258     DebugCompressionType CompressDebugSections = DebugCompressionType::None;
259 
260     unsigned RelaxELFRelocations : 1;
261 
262     /// Emit functions into separate sections.
263     unsigned FunctionSections : 1;
264 
265     /// Emit data into separate sections.
266     unsigned DataSections : 1;
267 
268     /// Do not emit visibility attribute for xcoff.
269     unsigned IgnoreXCOFFVisibility : 1;
270 
271     /// Emit XCOFF traceback table.
272     unsigned XCOFFTracebackTable : 1;
273 
274     unsigned UniqueSectionNames : 1;
275 
276     /// Use unique names for basic block sections.
277     unsigned UniqueBasicBlockSectionNames : 1;
278 
279     /// Emit target-specific trap instruction for 'unreachable' IR instructions.
280     unsigned TrapUnreachable : 1;
281 
282     /// Do not emit a trap instruction for 'unreachable' IR instructions behind
283     /// noreturn calls, even if TrapUnreachable is true.
284     unsigned NoTrapAfterNoreturn : 1;
285 
286     /// Bit size of immediate TLS offsets (0 == use the default).
287     unsigned TLSSize : 8;
288 
289     /// EmulatedTLS - This flag enables emulated TLS model, using emutls
290     /// function in the runtime library..
291     unsigned EmulatedTLS : 1;
292 
293     /// Whether -emulated-tls or -no-emulated-tls is set.
294     unsigned ExplicitEmulatedTLS : 1;
295 
296     /// This flag enables InterProcedural Register Allocation (IPRA).
297     unsigned EnableIPRA : 1;
298 
299     /// Emit section containing metadata on function stack sizes.
300     unsigned EmitStackSizeSection : 1;
301 
302     /// Enables the MachineOutliner pass.
303     unsigned EnableMachineOutliner : 1;
304 
305     /// Enables the MachineFunctionSplitter pass.
306     unsigned EnableMachineFunctionSplitter : 1;
307 
308     /// Set if the target supports default outlining behaviour.
309     unsigned SupportsDefaultOutlining : 1;
310 
311     /// Emit address-significance table.
312     unsigned EmitAddrsig : 1;
313 
314     /// Emit basic blocks into separate sections.
315     BasicBlockSection BBSections = BasicBlockSection::None;
316 
317     /// Memory Buffer that contains information on sampled basic blocks and used
318     /// to selectively generate basic block sections.
319     std::shared_ptr<MemoryBuffer> BBSectionsFuncListBuf;
320 
321     /// The flag enables call site info production. It is used only for debug
322     /// info, and it is restricted only to optimized code. This can be used for
323     /// something else, so that should be controlled in the frontend.
324     unsigned EmitCallSiteInfo : 1;
325     /// Set if the target supports the debug entry values by default.
326     unsigned SupportsDebugEntryValues : 1;
327     /// When set to true, the EnableDebugEntryValues option forces production
328     /// of debug entry values even if the target does not officially support
329     /// it. Useful for testing purposes only. This flag should never be checked
330     /// directly, always use \ref ShouldEmitDebugEntryValues instead.
331      unsigned EnableDebugEntryValues : 1;
332     /// NOTE: There are targets that still do not support the debug entry values
333     /// production.
334     bool ShouldEmitDebugEntryValues() const;
335 
336     // When set to true, use experimental new debug variable location tracking,
337     // which seeks to follow the values of variables rather than their location,
338     // post isel.
339     unsigned ValueTrackingVariableLocations : 1;
340 
341     /// Emit DWARF debug frame section.
342     unsigned ForceDwarfFrameSection : 1;
343 
344     /// Emit XRay Function Index section
345     unsigned XRayOmitFunctionIndex : 1;
346 
347     /// When set to true, don't use DWARF extensions in later DWARF versions.
348     /// By default, it is set to false.
349     unsigned DebugStrictDwarf : 1;
350 
351     /// Emit the hotpatch flag in CodeView debug.
352     unsigned Hotpatch : 1;
353 
354     /// Enables scalar MASS conversions
355     unsigned PPCGenScalarMASSEntries : 1;
356 
357     /// Enable JustMyCode instrumentation.
358     unsigned JMCInstrument : 1;
359 
360     /// Enable the CFIFixup pass.
361     unsigned EnableCFIFixup : 1;
362 
363     /// When set to true, enable MisExpect Diagnostics
364     /// By default, it is set to false
365     unsigned MisExpect : 1;
366 
367     /// Name of the stack usage file (i.e., .su file) if user passes
368     /// -fstack-usage. If empty, it can be implied that -fstack-usage is not
369     /// passed on the command line.
370     std::string StackUsageOutput;
371 
372     /// If greater than 0, override TargetLoweringBase::PrefLoopAlignment.
373     unsigned LoopAlignment = 0;
374 
375     /// FloatABIType - This setting is set by -float-abi=xxx option is specfied
376     /// on the command line. This setting may either be Default, Soft, or Hard.
377     /// Default selects the target's default behavior. Soft selects the ABI for
378     /// software floating point, but does not indicate that FP hardware may not
379     /// be used. Such a combination is unfortunately popular (e.g.
380     /// arm-apple-darwin). Hard presumes that the normal FP ABI is used.
381     FloatABI::ABIType FloatABIType = FloatABI::Default;
382 
383     /// AllowFPOpFusion - This flag is set by the -fp-contract=xxx option.
384     /// This controls the creation of fused FP ops that store intermediate
385     /// results in higher precision than IEEE allows (E.g. FMAs).
386     ///
387     /// Fast mode - allows formation of fused FP ops whenever they're
388     /// profitable.
389     /// Standard mode - allow fusion only for 'blessed' FP ops. At present the
390     /// only blessed op is the fmuladd intrinsic. In the future more blessed ops
391     /// may be added.
392     /// Strict mode - allow fusion only if/when it can be proven that the excess
393     /// precision won't effect the result.
394     ///
395     /// Note: This option only controls formation of fused ops by the
396     /// optimizers.  Fused operations that are explicitly specified (e.g. FMA
397     /// via the llvm.fma.* intrinsic) will always be honored, regardless of
398     /// the value of this option.
399     FPOpFusion::FPOpFusionMode AllowFPOpFusion = FPOpFusion::Standard;
400 
401     /// ThreadModel - This flag specifies the type of threading model to assume
402     /// for things like atomics
403     ThreadModel::Model ThreadModel = ThreadModel::POSIX;
404 
405     /// EABIVersion - This flag specifies the EABI version
406     EABI EABIVersion = EABI::Default;
407 
408     /// Which debugger to tune for.
409     DebuggerKind DebuggerTuning = DebuggerKind::Default;
410 
411   private:
412     /// Flushing mode to assume in default FP environment.
413     DenormalMode FPDenormalMode;
414 
415     /// Flushing mode to assume in default FP environment, for float/vector of
416     /// float.
417     DenormalMode FP32DenormalMode;
418 
419   public:
420     void setFPDenormalMode(DenormalMode Mode) {
421       FPDenormalMode = Mode;
422     }
423 
424     void setFP32DenormalMode(DenormalMode Mode) {
425       FP32DenormalMode = Mode;
426     }
427 
428     DenormalMode getRawFPDenormalMode() const {
429       return FPDenormalMode;
430     }
431 
432     DenormalMode getRawFP32DenormalMode() const {
433       return FP32DenormalMode;
434     }
435 
436     DenormalMode getDenormalMode(const fltSemantics &FPType) const;
437 
438     /// What exception model to use
439     ExceptionHandling ExceptionModel = ExceptionHandling::None;
440 
441     /// Machine level options.
442     MCTargetOptions MCOptions;
443 
444     /// Stores the filename/path of the final .o/.obj file, to be written in the
445     /// debug information. This is used for emitting the CodeView S_OBJNAME
446     /// record.
447     std::string ObjectFilenameForDebug;
448   };
449 
450 } // End llvm namespace
451 
452 #endif
453