1 //===--- CodeGenOptions.h ---------------------------------------*- 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 the CodeGenOptions interface.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #ifndef LLVM_CLANG_BASIC_CODEGENOPTIONS_H
14 #define LLVM_CLANG_BASIC_CODEGENOPTIONS_H
15 
16 #include "clang/Basic/DebugInfoOptions.h"
17 #include "clang/Basic/Sanitizers.h"
18 #include "clang/Basic/XRayInstr.h"
19 #include "llvm/ADT/FloatingPointMode.h"
20 #include "llvm/Support/CodeGen.h"
21 #include "llvm/Support/Regex.h"
22 #include "llvm/Target/TargetOptions.h"
23 #include <map>
24 #include <memory>
25 #include <string>
26 #include <vector>
27 
28 namespace clang {
29 
30 /// Bitfields of CodeGenOptions, split out from CodeGenOptions to ensure
31 /// that this large collection of bitfields is a trivial class type.
32 class CodeGenOptionsBase {
33 public:
34 #define CODEGENOPT(Name, Bits, Default) unsigned Name : Bits;
35 #define ENUM_CODEGENOPT(Name, Type, Bits, Default)
36 #include "clang/Basic/CodeGenOptions.def"
37 
38 protected:
39 #define CODEGENOPT(Name, Bits, Default)
40 #define ENUM_CODEGENOPT(Name, Type, Bits, Default) unsigned Name : Bits;
41 #include "clang/Basic/CodeGenOptions.def"
42 };
43 
44 /// CodeGenOptions - Track various options which control how the code
45 /// is optimized and passed to the backend.
46 class CodeGenOptions : public CodeGenOptionsBase {
47 public:
48   enum InliningMethod {
49     NormalInlining,     // Use the standard function inlining pass.
50     OnlyHintInlining,   // Inline only (implicitly) hinted functions.
51     OnlyAlwaysInlining  // Only run the always inlining pass.
52   };
53 
54   enum VectorLibrary {
55     NoLibrary,  // Don't use any vector library.
56     Accelerate, // Use the Accelerate framework.
57     MASSV,      // IBM MASS vector library.
58     SVML        // Intel short vector math library.
59   };
60 
61 
62   enum ObjCDispatchMethodKind {
63     Legacy = 0,
64     NonLegacy = 1,
65     Mixed = 2
66   };
67 
68   enum TLSModel {
69     GeneralDynamicTLSModel,
70     LocalDynamicTLSModel,
71     InitialExecTLSModel,
72     LocalExecTLSModel
73   };
74 
75   /// Clang versions with different platform ABI conformance.
76   enum class ClangABI {
77     /// Attempt to be ABI-compatible with code generated by Clang 3.8.x
78     /// (SVN r257626). This causes <1 x long long> to be passed in an
79     /// integer register instead of an SSE register on x64_64.
80     Ver3_8,
81 
82     /// Attempt to be ABI-compatible with code generated by Clang 4.0.x
83     /// (SVN r291814). This causes move operations to be ignored when
84     /// determining whether a class type can be passed or returned directly.
85     Ver4,
86 
87     /// Conform to the underlying platform's C and C++ ABIs as closely
88     /// as we can.
89     Latest
90   };
91 
92   enum StructReturnConventionKind {
93     SRCK_Default,  // No special option was passed.
94     SRCK_OnStack,  // Small structs on the stack (-fpcc-struct-return).
95     SRCK_InRegs    // Small structs in registers (-freg-struct-return).
96   };
97 
98   enum ProfileInstrKind {
99     ProfileNone,       // Profile instrumentation is turned off.
100     ProfileClangInstr, // Clang instrumentation to generate execution counts
101                        // to use with PGO.
102     ProfileIRInstr,    // IR level PGO instrumentation in LLVM.
103     ProfileCSIRInstr, // IR level PGO context sensitive instrumentation in LLVM.
104   };
105 
106   enum EmbedBitcodeKind {
107     Embed_Off,      // No embedded bitcode.
108     Embed_All,      // Embed both bitcode and commandline in the output.
109     Embed_Bitcode,  // Embed just the bitcode in the output.
110     Embed_Marker    // Embed a marker as a placeholder for bitcode.
111   };
112 
113   // This field stores one of the allowed values for the option
114   // -fbasic-block-sections=.  The allowed values with this option are:
115   // {"labels", "all", "list=<file>", "none"}.
116   //
117   // "labels":      Only generate basic block symbols (labels) for all basic
118   //                blocks, do not generate unique sections for basic blocks.
119   //                Use the machine basic block id in the symbol name to
120   //                associate profile info from virtual address to machine
121   //                basic block.
122   // "all" :        Generate basic block sections for all basic blocks.
123   // "list=<file>": Generate basic block sections for a subset of basic blocks.
124   //                The functions and the machine basic block ids are specified
125   //                in the file.
126   // "none":        Disable sections/labels for basic blocks.
127   std::string BBSections;
128 
129   enum class FramePointerKind {
130     None,        // Omit all frame pointers.
131     NonLeaf,     // Keep non-leaf frame pointers.
132     All,         // Keep all frame pointers.
133   };
134 
135   /// The code model to use (-mcmodel).
136   std::string CodeModel;
137 
138   /// The filename with path we use for coverage data files. The runtime
139   /// allows further manipulation with the GCOV_PREFIX and GCOV_PREFIX_STRIP
140   /// environment variables.
141   std::string CoverageDataFile;
142 
143   /// The filename with path we use for coverage notes files.
144   std::string CoverageNotesFile;
145 
146   /// Regexes separated by a semi-colon to filter the files to instrument.
147   std::string ProfileFilterFiles;
148 
149   /// Regexes separated by a semi-colon to filter the files to not instrument.
150   std::string ProfileExcludeFiles;
151 
152   /// The version string to put into coverage files.
153   char CoverageVersion[4];
154 
155   /// Enable additional debugging information.
156   std::string DebugPass;
157 
158   /// The string to embed in debug information as the current working directory.
159   std::string DebugCompilationDir;
160 
161   /// The string to embed in the debug information for the compile unit, if
162   /// non-empty.
163   std::string DwarfDebugFlags;
164 
165   /// The string containing the commandline for the llvm.commandline metadata,
166   /// if non-empty.
167   std::string RecordCommandLine;
168 
169   std::map<std::string, std::string> DebugPrefixMap;
170 
171   /// The ABI to use for passing floating point arguments.
172   std::string FloatABI;
173 
174   /// The floating-point denormal mode to use.
175   llvm::DenormalMode FPDenormalMode = llvm::DenormalMode::getIEEE();
176 
177   /// The floating-point denormal mode to use, for float.
178   llvm::DenormalMode FP32DenormalMode = llvm::DenormalMode::getIEEE();
179 
180   /// The float precision limit to use, if non-empty.
181   std::string LimitFloatPrecision;
182 
183   struct BitcodeFileToLink {
184     /// The filename of the bitcode file to link in.
185     std::string Filename;
186     /// If true, we set attributes functions in the bitcode library according to
187     /// our CodeGenOptions, much as we set attrs on functions that we generate
188     /// ourselves.
189     bool PropagateAttrs = false;
190     /// If true, we use LLVM module internalizer.
191     bool Internalize = false;
192     /// Bitwise combination of llvm::Linker::Flags, passed to the LLVM linker.
193     unsigned LinkFlags = 0;
194   };
195 
196   /// The files specified here are linked in to the module before optimizations.
197   std::vector<BitcodeFileToLink> LinkBitcodeFiles;
198 
199   /// The user provided name for the "main file", if non-empty. This is useful
200   /// in situations where the input file name does not match the original input
201   /// file, for example with -save-temps.
202   std::string MainFileName;
203 
204   /// The name for the split debug info file used for the DW_AT_[GNU_]dwo_name
205   /// attribute in the skeleton CU.
206   std::string SplitDwarfFile;
207 
208   /// Output filename for the split debug info, not used in the skeleton CU.
209   std::string SplitDwarfOutput;
210 
211   /// The name of the relocation model to use.
212   llvm::Reloc::Model RelocationModel;
213 
214   /// The thread model to use
215   std::string ThreadModel;
216 
217   /// If not an empty string, trap intrinsics are lowered to calls to this
218   /// function instead of to trap instructions.
219   std::string TrapFuncName;
220 
221   /// A list of dependent libraries.
222   std::vector<std::string> DependentLibraries;
223 
224   /// A list of linker options to embed in the object file.
225   std::vector<std::string> LinkerOptions;
226 
227   /// Name of the profile file to use as output for -fprofile-instr-generate,
228   /// -fprofile-generate, and -fcs-profile-generate.
229   std::string InstrProfileOutput;
230 
231   /// Name of the profile file to use with -fprofile-sample-use.
232   std::string SampleProfileFile;
233 
234   /// Name of the profile file to use as input for -fprofile-instr-use
235   std::string ProfileInstrumentUsePath;
236 
237   /// Name of the profile remapping file to apply to the profile data supplied
238   /// by -fprofile-sample-use or -fprofile-instr-use.
239   std::string ProfileRemappingFile;
240 
241   /// Name of the function summary index file to use for ThinLTO function
242   /// importing.
243   std::string ThinLTOIndexFile;
244 
245   /// Name of a file that can optionally be written with minimized bitcode
246   /// to be used as input for the ThinLTO thin link step, which only needs
247   /// the summary and module symbol table (and not, e.g. any debug metadata).
248   std::string ThinLinkBitcodeFile;
249 
250   /// Prefix to use for -save-temps output.
251   std::string SaveTempsFilePrefix;
252 
253   /// The name of the file to which the backend should save CHERI-related
254   /// statistics (number of ptr->int casts, csetbounds info, etc..) as JSON.
255   /// Note: This file can be a shared between multiple compiler instances
256   /// since we will use a lock file global
257   std::string CHERIStatsFile;
258 
259   /// Name of file passed with -fcuda-include-gpubinary option to forward to
260   /// CUDA runtime back-end for incorporating them into host-side object file.
261   std::string CudaGpuBinaryFileName;
262 
263   /// The name of the file to which the backend should save YAML optimization
264   /// records.
265   std::string OptRecordFile;
266 
267   /// The regex that filters the passes that should be saved to the optimization
268   /// records.
269   std::string OptRecordPasses;
270 
271   /// The format used for serializing remarks (default: YAML)
272   std::string OptRecordFormat;
273 
274   /// The name of the partition that symbols are assigned to, specified with
275   /// -fsymbol-partition (see https://lld.llvm.org/Partitions.html).
276   std::string SymbolPartition;
277 
278   /// Regular expression to select optimizations for which we should enable
279   /// optimization remarks. Transformation passes whose name matches this
280   /// expression (and support this feature), will emit a diagnostic
281   /// whenever they perform a transformation. This is enabled by the
282   /// -Rpass=regexp flag.
283   std::shared_ptr<llvm::Regex> OptimizationRemarkPattern;
284 
285   /// Regular expression to select optimizations for which we should enable
286   /// missed optimization remarks. Transformation passes whose name matches this
287   /// expression (and support this feature), will emit a diagnostic
288   /// whenever they tried but failed to perform a transformation. This is
289   /// enabled by the -Rpass-missed=regexp flag.
290   std::shared_ptr<llvm::Regex> OptimizationRemarkMissedPattern;
291 
292   /// Regular expression to select optimizations for which we should enable
293   /// optimization analyses. Transformation passes whose name matches this
294   /// expression (and support this feature), will emit a diagnostic
295   /// whenever they want to explain why they decided to apply or not apply
296   /// a given transformation. This is enabled by the -Rpass-analysis=regexp
297   /// flag.
298   std::shared_ptr<llvm::Regex> OptimizationRemarkAnalysisPattern;
299 
300   /// Set of files defining the rules for the symbol rewriting.
301   std::vector<std::string> RewriteMapFiles;
302 
303   /// Set of sanitizer checks that are non-fatal (i.e. execution should be
304   /// continued when possible).
305   SanitizerSet SanitizeRecover;
306 
307   /// Set of sanitizer checks that trap rather than diagnose.
308   SanitizerSet SanitizeTrap;
309 
310   /// List of backend command-line options for -fembed-bitcode.
311   std::vector<uint8_t> CmdArgs;
312 
313   /// A list of all -fno-builtin-* function names (e.g., memset).
314   std::vector<std::string> NoBuiltinFuncs;
315 
316   std::vector<std::string> Reciprocals;
317 
318   /// The preferred width for auto-vectorization transforms. This is intended to
319   /// override default transforms based on the width of the architected vector
320   /// registers.
321   std::string PreferVectorWidth;
322 
323   /// Set of XRay instrumentation kinds to emit.
324   XRayInstrSet XRayInstrumentationBundle;
325 
326   std::vector<std::string> DefaultFunctionAttrs;
327 
328   /// List of dynamic shared object files to be loaded as pass plugins.
329   std::vector<std::string> PassPlugins;
330 
331   /// Path to allowlist file specifying which objects
332   /// (files, functions) should exclusively be instrumented
333   /// by sanitizer coverage pass.
334   std::vector<std::string> SanitizeCoverageAllowlistFiles;
335 
336   /// Path to blocklist file specifying which objects
337   /// (files, functions) listed for instrumentation by sanitizer
338   /// coverage pass should actually not be instrumented.
339   std::vector<std::string> SanitizeCoverageBlocklistFiles;
340 
341   /// Executable and command-line used to create a given CompilerInvocation.
342   /// Most of the time this will be the full -cc1 command.
343   const char *Argv0 = nullptr;
344   ArrayRef<const char *> CommandLineArgs;
345 
346 public:
347   // Define accessors/mutators for code generation options of enumeration type.
348 #define CODEGENOPT(Name, Bits, Default)
349 #define ENUM_CODEGENOPT(Name, Type, Bits, Default) \
350   Type get##Name() const { return static_cast<Type>(Name); } \
351   void set##Name(Type Value) { Name = static_cast<unsigned>(Value); }
352 #include "clang/Basic/CodeGenOptions.def"
353 
354   CodeGenOptions();
355 
356   /// Is this a libc/libm function that is no longer recognized as a
357   /// builtin because a -fno-builtin-* option has been specified?
358   bool isNoBuiltinFunc(const char *Name) const;
359 
getNoBuiltinFuncs()360   const std::vector<std::string> &getNoBuiltinFuncs() const {
361     return NoBuiltinFuncs;
362   }
363 
364   /// Check if Clang profile instrumenation is on.
hasProfileClangInstr()365   bool hasProfileClangInstr() const {
366     return getProfileInstr() == ProfileClangInstr;
367   }
368 
369   /// Check if IR level profile instrumentation is on.
hasProfileIRInstr()370   bool hasProfileIRInstr() const {
371     return getProfileInstr() == ProfileIRInstr;
372   }
373 
374   /// Check if CS IR level profile instrumentation is on.
hasProfileCSIRInstr()375   bool hasProfileCSIRInstr() const {
376     return getProfileInstr() == ProfileCSIRInstr;
377   }
378 
379   /// Check if Clang profile use is on.
hasProfileClangUse()380   bool hasProfileClangUse() const {
381     return getProfileUse() == ProfileClangInstr;
382   }
383 
384   /// Check if IR level profile use is on.
hasProfileIRUse()385   bool hasProfileIRUse() const {
386     return getProfileUse() == ProfileIRInstr ||
387            getProfileUse() == ProfileCSIRInstr;
388   }
389 
390   /// Check if CSIR profile use is on.
hasProfileCSIRUse()391   bool hasProfileCSIRUse() const { return getProfileUse() == ProfileCSIRInstr; }
392 
393   /// Check if type and variable info should be emitted.
hasReducedDebugInfo()394   bool hasReducedDebugInfo() const {
395     return getDebugInfo() >= codegenoptions::DebugInfoConstructor;
396   }
397 };
398 
399 }  // end namespace clang
400 
401 #endif
402