1//===-- llvm/Support/CodeGen.h - CodeGen Concepts ---------------*- 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 define some types which define code generation concepts. For
10// example, relocation model.
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef LLVM_SUPPORT_CODEGEN_H
15#define LLVM_SUPPORT_CODEGEN_H
16
17#include "llvm/ADT/StringRef.h"
18#include <cstdint>
19#include <optional>
20
21namespace llvm {
22
23 // Relocation model types.
24 namespace Reloc {
25 // Cannot be named PIC due to collision with -DPIC
26 enum Model { Static, PIC_, DynamicNoPIC, ROPI, RWPI, ROPI_RWPI };
27 }
28
29 // Code model types.
30 namespace CodeModel {
31 // Sync changes with CodeGenCWrappers.h.
32 enum Model { Tiny, Small, Kernel, Medium, Large };
33 }
34
35 namespace PICLevel {
36 // This is used to map -fpic/-fPIC.
37 enum Level { NotPIC=0, SmallPIC=1, BigPIC=2 };
38 }
39
40 namespace PIELevel {
41 enum Level { Default=0, Small=1, Large=2 };
42 }
43
44 // TLS models.
45 namespace TLSModel {
46 enum Model {
47 GeneralDynamic,
48 LocalDynamic,
49 InitialExec,
50 LocalExec
51 };
52 }
53
54 enum class ExceptionHandling : int {
55 Default, ///< Not specified; resolve to the target's default model
56 None, ///< No exception support
57 DwarfCFI, ///< DWARF-like instruction based exceptions
58 SjLj, ///< setjmp/longjmp based exceptions
59 ARM, ///< ARM EHABI
60 WinEH, ///< Windows Exception Handling
61 Wasm, ///< WebAssembly Exception Handling
62 Emscripten, ///< Emscripten JavaScript-based exception handling
63 AIX, ///< AIX Exception Handling
64 ZOS, ///< z/OS MVS Exception Handling. Very similar to DwarfCFI, but the
65 ///< PPA1 is used instead of an .eh_frame section.
66 };
67
68 /// Returns the "exception-model" module flag spelling for an
69 /// ExceptionHandling value. Default, AIX, and ZOS have no spelling and return
70 /// "".
71 inline StringRef getExceptionModelName(ExceptionHandling EH) {
72 switch (EH) {
73 case ExceptionHandling::None:
74 return "none";
75 case ExceptionHandling::DwarfCFI:
76 return "dwarf";
77 case ExceptionHandling::SjLj:
78 return "sjlj";
79 case ExceptionHandling::ARM:
80 return "arm";
81 case ExceptionHandling::WinEH:
82 return "wineh";
83 case ExceptionHandling::Wasm:
84 return "wasm";
85 case ExceptionHandling::Emscripten:
86 return "emscripten";
87 case ExceptionHandling::Default:
88 case ExceptionHandling::AIX:
89 case ExceptionHandling::ZOS:
90 break;
91 }
92 return "";
93 }
94
95 /// Parses the string spelling used by the "exception-model" IR module flag
96 /// into an ExceptionHandling value, returning std::nullopt if it does not
97 /// name a supported exception model.
98 inline std::optional<ExceptionHandling> parseExceptionModel(StringRef Name) {
99 if (Name == "none")
100 return ExceptionHandling::None;
101 if (Name == "dwarf")
102 return ExceptionHandling::DwarfCFI;
103 if (Name == "sjlj")
104 return ExceptionHandling::SjLj;
105 if (Name == "arm")
106 return ExceptionHandling::ARM;
107 if (Name == "wineh")
108 return ExceptionHandling::WinEH;
109 if (Name == "wasm")
110 return ExceptionHandling::Wasm;
111 if (Name == "emscripten")
112 return ExceptionHandling::Emscripten;
113 return std::nullopt;
114 }
115
116 /// The floating-point format used for the target's "long double" type.
117 enum class LongDoubleFormat {
118 IEEEsingle,
119 IEEEdouble,
120 X87DoubleExtended,
121 IEEEquad,
122 PPCDoubleDouble,
123 };
124
125 /// Returns the IR floating-point type name for a LongDoubleFormat.
126 inline StringRef getLongDoubleFormatName(LongDoubleFormat Format) {
127 switch (Format) {
128 case LongDoubleFormat::IEEEsingle:
129 return "float";
130 case LongDoubleFormat::IEEEdouble:
131 return "double";
132 case LongDoubleFormat::X87DoubleExtended:
133 return "x86_fp80";
134 case LongDoubleFormat::IEEEquad:
135 return "fp128";
136 case LongDoubleFormat::PPCDoubleDouble:
137 return "ppc_fp128";
138 }
139 return "";
140 }
141
142 /// Parses an IR floating-point type name into a LongDoubleFormat, returning
143 /// std::nullopt if it does not name a supported long double format.
144 inline std::optional<LongDoubleFormat> parseLongDoubleFormat(StringRef Name) {
145 if (Name == "float")
146 return LongDoubleFormat::IEEEsingle;
147 if (Name == "double")
148 return LongDoubleFormat::IEEEdouble;
149 if (Name == "x86_fp80")
150 return LongDoubleFormat::X87DoubleExtended;
151 if (Name == "fp128")
152 return LongDoubleFormat::IEEEquad;
153 if (Name == "ppc_fp128")
154 return LongDoubleFormat::PPCDoubleDouble;
155 return std::nullopt;
156 }
157
158 namespace FloatABI {
159 enum ABIType {
160 Default, // Target-specific (either soft or hard depending on triple, etc).
161 Soft, // Soft float.
162 Hard // Hard float.
163 };
164
165 /// Parse the string spelling used by the "float-abi" IR module flag into an
166 /// ABIType.
167 inline std::optional<ABIType> parseABIType(StringRef S) {
168 if (S == "soft")
169 return Soft;
170 if (S == "hard")
171 return Hard;
172 return std::nullopt;
173 }
174
175 /// Returns the string spelling used by the "float-abi" IR module flag for a
176 /// Soft or Hard ABIType. Default has no spelling.
177 inline StringRef getABITypeName(ABIType ABI) {
178 switch (ABI) {
179 case Soft:
180 return "soft";
181 case Hard:
182 return "hard";
183 case Default:
184 break;
185 }
186 return "";
187 }
188 } // namespace FloatABI
189
190 /// The threading model to assume for lowering, e.g. of atomics.
191 enum class ThreadModel {
192 POSIX, // POSIX Threads
193 Single, // Single Threaded Environment
194 };
195
196 /// Parse the string spelling used by the "thread-model" IR module flag into a
197 /// ThreadModel.
198 inline std::optional<ThreadModel> parseThreadModel(StringRef S) {
199 if (S == "posix")
200 return ThreadModel::POSIX;
201 if (S == "single")
202 return ThreadModel::Single;
203 return std::nullopt;
204 }
205
206 /// Returns the string spelling used by the "thread-model" IR module flag for
207 /// a ThreadModel.
208 inline StringRef getThreadModelName(ThreadModel TM) {
209 switch (TM) {
210 case ThreadModel::POSIX:
211 return "posix";
212 case ThreadModel::Single:
213 return "single";
214 }
215 return "";
216 }
217
218 /// Identify a debugger for "tuning" the debug info.
219 ///
220 /// The "debugger tuning" concept allows us to present a more intuitive
221 /// interface that unpacks into different sets of defaults for the various
222 /// individual feature-flag settings, that suit the preferences of the
223 /// various debuggers. However, it's worth remembering that debuggers are
224 /// not the only consumers of debug info, and some variations in DWARF might
225 /// better be treated as target/platform issues. Fundamentally,
226 /// o if the feature is useful (or not) to a particular debugger, regardless
227 /// of the target, that's a tuning decision;
228 /// o if the feature is useful (or not) on a particular platform, regardless
229 /// of the debugger, that's a target decision.
230 /// It's not impossible to see both factors in some specific case.
231 enum class DebuggerKind {
232 Default, ///< No specific tuning requested.
233 GDB, ///< Tune debug info for gdb.
234 LLDB, ///< Tune debug info for lldb.
235 SCE, ///< Tune debug info for SCE targets (e.g. PS4).
236 DBX ///< Tune debug info for dbx.
237 };
238
239 /// Parse the string spelling used by the "debugger-tuning" IR module flag
240 /// into a DebuggerKind.
241 inline std::optional<DebuggerKind> parseDebuggerTuning(StringRef S) {
242 if (S == "gdb")
243 return DebuggerKind::GDB;
244 if (S == "lldb")
245 return DebuggerKind::LLDB;
246 if (S == "sce")
247 return DebuggerKind::SCE;
248 if (S == "dbx")
249 return DebuggerKind::DBX;
250 return std::nullopt;
251 }
252
253 /// Returns the string spelling used by the "debugger-tuning" IR module flag
254 /// for a DebuggerKind.
255 inline StringRef getDebuggerTuningName(DebuggerKind Tuning) {
256 switch (Tuning) {
257 case DebuggerKind::GDB:
258 return "gdb";
259 case DebuggerKind::LLDB:
260 return "lldb";
261 case DebuggerKind::SCE:
262 return "sce";
263 case DebuggerKind::DBX:
264 return "dbx";
265 case DebuggerKind::Default:
266 break;
267 }
268 return "";
269 }
270
271 enum class EABI {
272 Unknown,
273 Default, // Default means not specified
274 EABI4, // Target-specific (either 4, 5 or gnu depending on triple).
275 EABI5,
276 GNU
277 };
278
279 /// Code generation optimization level.
280 enum class CodeGenOptLevel {
281 None = 0, ///< -O0
282 Less = 1, ///< -O1
283 Default = 2, ///< -O2, -Os, -Oz
284 Aggressive = 3 ///< -O3
285 };
286
287 namespace CodeGenOpt {
288 /// Get the \c Level identified by the integer \p OL.
289 ///
290 /// Returns std::nullopt if \p OL is invalid.
291 inline std::optional<CodeGenOptLevel> getLevel(int OL) {
292 if (OL < 0 || OL > 3)
293 return std::nullopt;
294 return static_cast<CodeGenOptLevel>(OL);
295 }
296 /// Parse \p C as a single digit integer and get matching \c CodeGenLevel.
297 ///
298 /// Returns std::nullopt if the input is not a valid optimization level.
299 inline std::optional<CodeGenOptLevel> parseLevel(char C) {
300 if (C < '0')
301 return std::nullopt;
302 return getLevel(OL: static_cast<int>(C - '0'));
303 }
304 } // namespace CodeGenOpt
305
306 /// These enums are meant to be passed into addPassesToEmitFile to indicate
307 /// what type of file to emit, and returned by it to indicate what type of
308 /// file could actually be made.
309 enum class CodeGenFileType {
310 AssemblyFile,
311 ObjectFile,
312 Null // Do not emit any output.
313 };
314
315 /// Specify where the machine verifier runs in the codegen pipeline.
316 enum class MachineVerifierMode {
317 /// Never run the machine verifier.
318 None,
319 /// Run the machine verifier once, at the end of the pipeline.
320 End,
321 /// Run the machine verifier before and after every machine pass.
322 Each
323 };
324
325 // Specify what functions should keep the frame pointer.
326 enum class FramePointerKind {
327 None,
328 NonLeaf,
329 All,
330 Reserved,
331 NonLeafNoReserve
332 };
333
334 // Specify what type of zeroing callee-used registers.
335 namespace ZeroCallUsedRegs {
336 const unsigned ONLY_USED = 1U << 1;
337 const unsigned ONLY_GPR = 1U << 2;
338 const unsigned ONLY_ARG = 1U << 3;
339
340 enum class ZeroCallUsedRegsKind : unsigned int {
341 // Don't zero any call-used regs.
342 Skip = 1U << 0,
343 // Only zeros call-used GPRs used in the fn and pass args.
344 UsedGPRArg = ONLY_USED | ONLY_GPR | ONLY_ARG,
345 // Only zeros call-used GPRs used in the fn.
346 UsedGPR = ONLY_USED | ONLY_GPR,
347 // Only zeros call-used regs used in the fn and pass args.
348 UsedArg = ONLY_USED | ONLY_ARG,
349 // Only zeros call-used regs used in the fn.
350 Used = ONLY_USED,
351 // Zeros all call-used GPRs that pass args.
352 AllGPRArg = ONLY_GPR | ONLY_ARG,
353 // Zeros all call-used GPRs.
354 AllGPR = ONLY_GPR,
355 // Zeros all call-used regs that pass args.
356 AllArg = ONLY_ARG,
357 // Zeros all call-used regs.
358 All = 0,
359 };
360 } // namespace ZeroCallUsedRegs
361
362 enum class UWTableKind {
363 None = 0, ///< No unwind table requested
364 Sync = 1, ///< "Synchronous" unwind tables
365 Async = 2, ///< "Asynchronous" unwind tables (instr precise)
366 Default = 2,
367 };
368
369 enum class FunctionReturnThunksKind : unsigned int {
370 Keep = 0, ///< No function return thunk.
371 Extern = 1, ///< Replace returns with jump to thunk, don't emit thunk.
372 Invalid = 2, ///< Not used.
373 };
374
375 enum class WinX64EHUnwindMode {
376 Default = 4, // Toolchain default/auto.
377 // Using '4' to avoid renumbering the existing values.
378
379 V1 = 0, // V1 unwind info.
380 V2BestEffort = 1, // V2 where possible, fall back to V1.
381 V2Required = 2, // V2 required — error if a function cannot use V2.
382 V3 = 3, // V3 unwind info.
383 };
384
385 enum class ControlFlowGuardMode {
386 // Don't enable Control Flow Guard.
387 Disabled = 0,
388 // Emit the Control Flow Guard tables in the binary, but don't emit any
389 // checks.
390 TableOnly = 1,
391 // Enable Control Flow Guard checks and emit the tables.
392 Enabled = 2,
393 };
394
395 enum class ControlFlowGuardMechanism {
396 // Choose the mechanism automatically based on the target.
397 Automatic = 0,
398 Check = 1,
399 Dispatch = 2,
400 };
401
402 } // namespace llvm
403
404#endif
405