LLVM 24.0.0git
CodeGen.h
Go to the documentation of this file.
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
27 }
28
29 // Code model types.
30 namespace CodeModel {
31 // Sync changes with CodeGenCWrappers.h.
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.
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 /// "".
72 switch (EH) {
74 return "none";
76 return "dwarf";
78 return "sjlj";
80 return "arm";
82 return "wineh";
84 return "wasm";
86 return "emscripten";
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")
101 if (Name == "dwarf")
103 if (Name == "sjlj")
105 if (Name == "arm")
107 if (Name == "wineh")
109 if (Name == "wasm")
111 if (Name == "emscripten")
113 return std::nullopt;
114 }
115
116 /// The floating-point format used for the target's "long double" type.
124
125 /// Returns the IR floating-point type name for a LongDoubleFormat.
127 switch (Format) {
129 return "float";
131 return "double";
133 return "x86_fp80";
135 return "fp128";
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")
147 if (Name == "double")
149 if (Name == "x86_fp80")
151 if (Name == "fp128")
153 if (Name == "ppc_fp128")
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.
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.
209 switch (TM) {
211 return "posix";
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.
256 switch (Tuning) {
258 return "gdb";
260 return "lldb";
262 return "sce";
264 return "dbx";
266 break;
267 }
268 return "";
269 }
270
271 enum class EABI {
273 Default, // Default means not specified
274 EABI4, // Target-specific (either 4, 5 or gnu depending on triple).
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(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 {
312 Null // Do not emit any output.
313 };
314
315 /// Specify where the machine verifier runs in the codegen pipeline.
317 /// Never run the machine verifier.
319 /// Run the machine verifier once, at the end of the pipeline.
321 /// Run the machine verifier before and after every machine pass.
323 };
324
325 // Specify what functions should keep the frame pointer.
333
334 // Specify what type of zeroing callee-used registers.
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.
345 // Only zeros call-used GPRs used in the fn.
347 // Only zeros call-used regs used in the fn and pass args.
349 // Only zeros call-used regs used in the fn.
351 // Zeros all call-used GPRs that pass args.
353 // Zeros all call-used GPRs.
355 // Zeros all call-used regs that pass args.
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)
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
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
386 // Don't enable Control Flow Guard.
388 // Emit the Control Flow Guard tables in the binary, but don't emit any
389 // checks.
391 // Enable Control Flow Guard checks and emit the tables.
393 };
394
396 // Choose the mechanism automatically based on the target.
398 Check = 1,
400 };
401
402 } // namespace llvm
403
404#endif
static GCRegistry::Add< ShadowStackGC > C("shadow-stack", "Very portable GC for uncooperative code generators")
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
This class is the base class for all object file types.
Definition ObjectFile.h:231
Define some predicates that are used for node matching.
Definition ARMEHABI.h:25
std::optional< CodeGenOptLevel > getLevel(int OL)
Get the Level identified by the integer OL.
Definition CodeGen.h:291
std::optional< CodeGenOptLevel > parseLevel(char C)
Parse C as a single digit integer and get matching CodeGenLevel.
Definition CodeGen.h:299
std::optional< ABIType > parseABIType(StringRef S)
Parse the string spelling used by the "float-abi" IR module flag into an ABIType.
Definition CodeGen.h:167
StringRef getABITypeName(ABIType ABI)
Returns the string spelling used by the "float-abi" IR module flag for a Soft or Hard ABIType.
Definition CodeGen.h:177
@ DynamicNoPIC
Definition CodeGen.h:26
const unsigned ONLY_GPR
Definition CodeGen.h:337
const unsigned ONLY_USED
Definition CodeGen.h:336
const unsigned ONLY_ARG
Definition CodeGen.h:338
This is an optimization pass for GlobalISel generic memory operations.
std::optional< LongDoubleFormat > parseLongDoubleFormat(StringRef Name)
Parses an IR floating-point type name into a LongDoubleFormat, returning std::nullopt if it does not ...
Definition CodeGen.h:144
FramePointerKind
Definition CodeGen.h:326
@ Unknown
Not known to have no common set bits.
StringRef getLongDoubleFormatName(LongDoubleFormat Format)
Returns the IR floating-point type name for a LongDoubleFormat.
Definition CodeGen.h:126
LongDoubleFormat
The floating-point format used for the target's "long double" type.
Definition CodeGen.h:117
StringRef getDebuggerTuningName(DebuggerKind Tuning)
Returns the string spelling used by the "debugger-tuning" IR module flag for a DebuggerKind.
Definition CodeGen.h:255
std::optional< ExceptionHandling > parseExceptionModel(StringRef Name)
Parses the string spelling used by the "exception-model" IR module flag into an ExceptionHandling val...
Definition CodeGen.h:98
ControlFlowGuardMode
Definition CodeGen.h:385
WinX64EHUnwindMode
Definition CodeGen.h:375
StringRef getExceptionModelName(ExceptionHandling EH)
Returns the "exception-model" module flag spelling for an ExceptionHandling value.
Definition CodeGen.h:71
UWTableKind
Definition CodeGen.h:362
@ Async
"Asynchronous" unwind tables (instr precise)
Definition CodeGen.h:365
@ Sync
"Synchronous" unwind tables
Definition CodeGen.h:364
CodeGenFileType
These enums are meant to be passed into addPassesToEmitFile to indicate what type of file to emit,...
Definition CodeGen.h:309
CodeGenOptLevel
Code generation optimization level.
Definition CodeGen.h:280
ThreadModel
The threading model to assume for lowering, e.g. of atomics.
Definition CodeGen.h:191
std::optional< ThreadModel > parseThreadModel(StringRef S)
Parse the string spelling used by the "thread-model" IR module flag into a ThreadModel.
Definition CodeGen.h:198
ExceptionHandling
Definition CodeGen.h:54
@ SjLj
setjmp/longjmp based exceptions
Definition CodeGen.h:58
@ ZOS
z/OS MVS Exception Handling.
Definition CodeGen.h:64
@ Emscripten
Emscripten JavaScript-based exception handling.
Definition CodeGen.h:62
@ None
No exception support.
Definition CodeGen.h:56
@ Default
Not specified; resolve to the target's default model.
Definition CodeGen.h:55
@ AIX
AIX Exception Handling.
Definition CodeGen.h:63
@ DwarfCFI
DWARF-like instruction based exceptions.
Definition CodeGen.h:57
@ WinEH
Windows Exception Handling.
Definition CodeGen.h:60
@ Wasm
WebAssembly Exception Handling.
Definition CodeGen.h:61
std::optional< DebuggerKind > parseDebuggerTuning(StringRef S)
Parse the string spelling used by the "debugger-tuning" IR module flag into a DebuggerKind.
Definition CodeGen.h:241
ControlFlowGuardMechanism
Definition CodeGen.h:395
StringRef getThreadModelName(ThreadModel TM)
Returns the string spelling used by the "thread-model" IR module flag for a ThreadModel.
Definition CodeGen.h:208
DebuggerKind
Identify a debugger for "tuning" the debug info.
Definition CodeGen.h:231
@ SCE
Tune debug info for SCE targets (e.g. PS4).
Definition CodeGen.h:235
@ DBX
Tune debug info for dbx.
Definition CodeGen.h:236
@ Default
No specific tuning requested.
Definition CodeGen.h:232
@ GDB
Tune debug info for gdb.
Definition CodeGen.h:233
@ LLDB
Tune debug info for lldb.
Definition CodeGen.h:234
@ Enabled
Convert any .debug_str_offsets tables to DWARF64 if needed.
Definition DWP.h:31
@ Disabled
Don't do any conversion of .debug_str_offsets tables.
Definition DWP.h:30
MachineVerifierMode
Specify where the machine verifier runs in the codegen pipeline.
Definition CodeGen.h:316
@ Each
Run the machine verifier before and after every machine pass.
Definition CodeGen.h:322
@ End
Run the machine verifier once, at the end of the pipeline.
Definition CodeGen.h:320
FunctionReturnThunksKind
Definition CodeGen.h:369
@ Keep
No function return thunk.
Definition CodeGen.h:370
@ Extern
Replace returns with jump to thunk, don't emit thunk.
Definition CodeGen.h:371