[cmake] Add minor version to library SONAME (#79376)
[llvm-project.git] / clang / lib / CodeGen / TargetInfo.h
blob7682f197041c748a4daebc0e15fe19d73eeccaea
1 //===---- TargetInfo.h - Encapsulate target details -------------*- 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 // These classes wrap the information about a call or function
10 // definition used to handle ABI compliancy.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_CLANG_LIB_CODEGEN_TARGETINFO_H
15 #define LLVM_CLANG_LIB_CODEGEN_TARGETINFO_H
17 #include "CGBuilder.h"
18 #include "CodeGenModule.h"
19 #include "CGValue.h"
20 #include "clang/AST/Type.h"
21 #include "clang/Basic/LLVM.h"
22 #include "clang/Basic/SyncScope.h"
23 #include "llvm/ADT/SmallString.h"
24 #include "llvm/ADT/StringRef.h"
26 namespace llvm {
27 class Constant;
28 class GlobalValue;
29 class Type;
30 class Value;
33 namespace clang {
34 class Decl;
36 namespace CodeGen {
37 class ABIInfo;
38 class CallArgList;
39 class CodeGenFunction;
40 class CGBlockInfo;
41 class SwiftABIInfo;
43 /// TargetCodeGenInfo - This class organizes various target-specific
44 /// codegeneration issues, like target-specific attributes, builtins and so
45 /// on.
46 class TargetCodeGenInfo {
47 std::unique_ptr<ABIInfo> Info;
49 protected:
50 // Target hooks supporting Swift calling conventions. The target must
51 // initialize this field if it claims to support these calling conventions
52 // by returning true from TargetInfo::checkCallingConvention for them.
53 std::unique_ptr<SwiftABIInfo> SwiftInfo;
55 // Returns ABI info helper for the target. This is for use by derived classes.
56 template <typename T> const T &getABIInfo() const {
57 return static_cast<const T &>(*Info);
60 public:
61 TargetCodeGenInfo(std::unique_ptr<ABIInfo> Info);
62 virtual ~TargetCodeGenInfo();
64 /// getABIInfo() - Returns ABI info helper for the target.
65 const ABIInfo &getABIInfo() const { return *Info; }
67 /// Returns Swift ABI info helper for the target.
68 const SwiftABIInfo &getSwiftABIInfo() const {
69 assert(SwiftInfo && "Swift ABI info has not been initialized");
70 return *SwiftInfo;
73 /// setTargetAttributes - Provides a convenient hook to handle extra
74 /// target-specific attributes for the given global.
75 virtual void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
76 CodeGen::CodeGenModule &M) const {}
78 /// emitTargetMetadata - Provides a convenient hook to handle extra
79 /// target-specific metadata for the given globals.
80 virtual void emitTargetMetadata(
81 CodeGen::CodeGenModule &CGM,
82 const llvm::MapVector<GlobalDecl, StringRef> &MangledDeclNames) const {}
84 /// Provides a convenient hook to handle extra target-specific globals.
85 virtual void emitTargetGlobals(CodeGen::CodeGenModule &CGM) const {}
87 /// Any further codegen related checks that need to be done on a function call
88 /// in a target specific manner.
89 virtual void checkFunctionCallABI(CodeGenModule &CGM, SourceLocation CallLoc,
90 const FunctionDecl *Caller,
91 const FunctionDecl *Callee,
92 const CallArgList &Args) const {}
94 /// Determines the size of struct _Unwind_Exception on this platform,
95 /// in 8-bit units. The Itanium ABI defines this as:
96 /// struct _Unwind_Exception {
97 /// uint64 exception_class;
98 /// _Unwind_Exception_Cleanup_Fn exception_cleanup;
99 /// uint64 private_1;
100 /// uint64 private_2;
101 /// };
102 virtual unsigned getSizeOfUnwindException() const;
104 /// Controls whether __builtin_extend_pointer should sign-extend
105 /// pointers to uint64_t or zero-extend them (the default). Has
106 /// no effect for targets:
107 /// - that have 64-bit pointers, or
108 /// - that cannot address through registers larger than pointers, or
109 /// - that implicitly ignore/truncate the top bits when addressing
110 /// through such registers.
111 virtual bool extendPointerWithSExt() const { return false; }
113 /// Determines the DWARF register number for the stack pointer, for
114 /// exception-handling purposes. Implements __builtin_dwarf_sp_column.
116 /// Returns -1 if the operation is unsupported by this target.
117 virtual int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const {
118 return -1;
121 /// Initializes the given DWARF EH register-size table, a char*.
122 /// Implements __builtin_init_dwarf_reg_size_table.
124 /// Returns true if the operation is unsupported by this target.
125 virtual bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
126 llvm::Value *Address) const {
127 return true;
130 /// Performs the code-generation required to convert a return
131 /// address as stored by the system into the actual address of the
132 /// next instruction that will be executed.
134 /// Used by __builtin_extract_return_addr().
135 virtual llvm::Value *decodeReturnAddress(CodeGen::CodeGenFunction &CGF,
136 llvm::Value *Address) const {
137 return Address;
140 /// Performs the code-generation required to convert the address
141 /// of an instruction into a return address suitable for storage
142 /// by the system in a return slot.
144 /// Used by __builtin_frob_return_addr().
145 virtual llvm::Value *encodeReturnAddress(CodeGen::CodeGenFunction &CGF,
146 llvm::Value *Address) const {
147 return Address;
150 /// Performs a target specific test of a floating point value for things
151 /// like IsNaN, Infinity, ... Nullptr is returned if no implementation
152 /// exists.
153 virtual llvm::Value *
154 testFPKind(llvm::Value *V, unsigned BuiltinID, CGBuilderTy &Builder,
155 CodeGenModule &CGM) const {
156 assert(V->getType()->isFloatingPointTy() && "V should have an FP type.");
157 return nullptr;
160 /// Corrects the low-level LLVM type for a given constraint and "usual"
161 /// type.
163 /// \returns A pointer to a new LLVM type, possibly the same as the original
164 /// on success; 0 on failure.
165 virtual llvm::Type *adjustInlineAsmType(CodeGen::CodeGenFunction &CGF,
166 StringRef Constraint,
167 llvm::Type *Ty) const {
168 return Ty;
171 /// Target hook to decide whether an inline asm operand can be passed
172 /// by value.
173 virtual bool isScalarizableAsmOperand(CodeGen::CodeGenFunction &CGF,
174 llvm::Type *Ty) const {
175 return false;
178 /// Adds constraints and types for result registers.
179 virtual void addReturnRegisterOutputs(
180 CodeGen::CodeGenFunction &CGF, CodeGen::LValue ReturnValue,
181 std::string &Constraints, std::vector<llvm::Type *> &ResultRegTypes,
182 std::vector<llvm::Type *> &ResultTruncRegTypes,
183 std::vector<CodeGen::LValue> &ResultRegDests, std::string &AsmString,
184 unsigned NumOutputs) const {}
186 /// doesReturnSlotInterfereWithArgs - Return true if the target uses an
187 /// argument slot for an 'sret' type.
188 virtual bool doesReturnSlotInterfereWithArgs() const { return true; }
190 /// Retrieve the address of a function to call immediately before
191 /// calling objc_retainAutoreleasedReturnValue. The
192 /// implementation of objc_autoreleaseReturnValue sniffs the
193 /// instruction stream following its return address to decide
194 /// whether it's a call to objc_retainAutoreleasedReturnValue.
195 /// This can be prohibitively expensive, depending on the
196 /// relocation model, and so on some targets it instead sniffs for
197 /// a particular instruction sequence. This functions returns
198 /// that instruction sequence in inline assembly, which will be
199 /// empty if none is required.
200 virtual StringRef getARCRetainAutoreleasedReturnValueMarker() const {
201 return "";
204 /// Determine whether a call to objc_retainAutoreleasedReturnValue or
205 /// objc_unsafeClaimAutoreleasedReturnValue should be marked as 'notail'.
206 virtual bool markARCOptimizedReturnCallsAsNoTail() const { return false; }
208 /// Return a constant used by UBSan as a signature to identify functions
209 /// possessing type information, or 0 if the platform is unsupported.
210 /// This magic number is invalid instruction encoding in many targets.
211 virtual llvm::Constant *
212 getUBSanFunctionSignature(CodeGen::CodeGenModule &CGM) const {
213 return llvm::ConstantInt::get(CGM.Int32Ty, 0xc105cafe);
216 /// Determine whether a call to an unprototyped functions under
217 /// the given calling convention should use the variadic
218 /// convention or the non-variadic convention.
220 /// There's a good reason to make a platform's variadic calling
221 /// convention be different from its non-variadic calling
222 /// convention: the non-variadic arguments can be passed in
223 /// registers (better for performance), and the variadic arguments
224 /// can be passed on the stack (also better for performance). If
225 /// this is done, however, unprototyped functions *must* use the
226 /// non-variadic convention, because C99 states that a call
227 /// through an unprototyped function type must succeed if the
228 /// function was defined with a non-variadic prototype with
229 /// compatible parameters. Therefore, splitting the conventions
230 /// makes it impossible to call a variadic function through an
231 /// unprototyped type. Since function prototypes came out in the
232 /// late 1970s, this is probably an acceptable trade-off.
233 /// Nonetheless, not all platforms are willing to make it, and in
234 /// particularly x86-64 bends over backwards to make the
235 /// conventions compatible.
237 /// The default is false. This is correct whenever:
238 /// - the conventions are exactly the same, because it does not
239 /// matter and the resulting IR will be somewhat prettier in
240 /// certain cases; or
241 /// - the conventions are substantively different in how they pass
242 /// arguments, because in this case using the variadic convention
243 /// will lead to C99 violations.
245 /// However, some platforms make the conventions identical except
246 /// for passing additional out-of-band information to a variadic
247 /// function: for example, x86-64 passes the number of SSE
248 /// arguments in %al. On these platforms, it is desirable to
249 /// call unprototyped functions using the variadic convention so
250 /// that unprototyped calls to varargs functions still succeed.
252 /// Relatedly, platforms which pass the fixed arguments to this:
253 /// A foo(B, C, D);
254 /// differently than they would pass them to this:
255 /// A foo(B, C, D, ...);
256 /// may need to adjust the debugger-support code in Sema to do the
257 /// right thing when calling a function with no know signature.
258 virtual bool isNoProtoCallVariadic(const CodeGen::CallArgList &args,
259 const FunctionNoProtoType *fnType) const;
261 /// Gets the linker options necessary to link a dependent library on this
262 /// platform.
263 virtual void getDependentLibraryOption(llvm::StringRef Lib,
264 llvm::SmallString<24> &Opt) const;
266 /// Gets the linker options necessary to detect object file mismatches on
267 /// this platform.
268 virtual void getDetectMismatchOption(llvm::StringRef Name,
269 llvm::StringRef Value,
270 llvm::SmallString<32> &Opt) const {}
272 /// Get LLVM calling convention for OpenCL kernel.
273 virtual unsigned getOpenCLKernelCallingConv() const;
275 /// Get target specific null pointer.
276 /// \param T is the LLVM type of the null pointer.
277 /// \param QT is the clang QualType of the null pointer.
278 /// \return ConstantPointerNull with the given type \p T.
279 /// Each target can override it to return its own desired constant value.
280 virtual llvm::Constant *getNullPointer(const CodeGen::CodeGenModule &CGM,
281 llvm::PointerType *T, QualType QT) const;
283 /// Get target favored AST address space of a global variable for languages
284 /// other than OpenCL and CUDA.
285 /// If \p D is nullptr, returns the default target favored address space
286 /// for global variable.
287 virtual LangAS getGlobalVarAddressSpace(CodeGenModule &CGM,
288 const VarDecl *D) const;
290 /// Get the AST address space for alloca.
291 virtual LangAS getASTAllocaAddressSpace() const { return LangAS::Default; }
293 /// Perform address space cast of an expression of pointer type.
294 /// \param V is the LLVM value to be casted to another address space.
295 /// \param SrcAddr is the language address space of \p V.
296 /// \param DestAddr is the targeted language address space.
297 /// \param DestTy is the destination LLVM pointer type.
298 /// \param IsNonNull is the flag indicating \p V is known to be non null.
299 virtual llvm::Value *performAddrSpaceCast(CodeGen::CodeGenFunction &CGF,
300 llvm::Value *V, LangAS SrcAddr,
301 LangAS DestAddr, llvm::Type *DestTy,
302 bool IsNonNull = false) const;
304 /// Perform address space cast of a constant expression of pointer type.
305 /// \param V is the LLVM constant to be casted to another address space.
306 /// \param SrcAddr is the language address space of \p V.
307 /// \param DestAddr is the targeted language address space.
308 /// \param DestTy is the destination LLVM pointer type.
309 virtual llvm::Constant *performAddrSpaceCast(CodeGenModule &CGM,
310 llvm::Constant *V,
311 LangAS SrcAddr, LangAS DestAddr,
312 llvm::Type *DestTy) const;
314 /// Get address space of pointer parameter for __cxa_atexit.
315 virtual LangAS getAddrSpaceOfCxaAtexitPtrParam() const {
316 return LangAS::Default;
319 /// Get the syncscope used in LLVM IR.
320 virtual llvm::SyncScope::ID getLLVMSyncScopeID(const LangOptions &LangOpts,
321 SyncScope Scope,
322 llvm::AtomicOrdering Ordering,
323 llvm::LLVMContext &Ctx) const;
325 /// Interface class for filling custom fields of a block literal for OpenCL.
326 class TargetOpenCLBlockHelper {
327 public:
328 typedef std::pair<llvm::Value *, StringRef> ValueTy;
329 TargetOpenCLBlockHelper() {}
330 virtual ~TargetOpenCLBlockHelper() {}
331 /// Get the custom field types for OpenCL blocks.
332 virtual llvm::SmallVector<llvm::Type *, 1> getCustomFieldTypes() = 0;
333 /// Get the custom field values for OpenCL blocks.
334 virtual llvm::SmallVector<ValueTy, 1>
335 getCustomFieldValues(CodeGenFunction &CGF, const CGBlockInfo &Info) = 0;
336 virtual bool areAllCustomFieldValuesConstant(const CGBlockInfo &Info) = 0;
337 /// Get the custom field values for OpenCL blocks if all values are LLVM
338 /// constants.
339 virtual llvm::SmallVector<llvm::Constant *, 1>
340 getCustomFieldValues(CodeGenModule &CGM, const CGBlockInfo &Info) = 0;
342 virtual TargetOpenCLBlockHelper *getTargetOpenCLBlockHelper() const {
343 return nullptr;
346 /// Create an OpenCL kernel for an enqueued block. The kernel function is
347 /// a wrapper for the block invoke function with target-specific calling
348 /// convention and ABI as an OpenCL kernel. The wrapper function accepts
349 /// block context and block arguments in target-specific way and calls
350 /// the original block invoke function.
351 virtual llvm::Value *
352 createEnqueuedBlockKernel(CodeGenFunction &CGF,
353 llvm::Function *BlockInvokeFunc,
354 llvm::Type *BlockTy) const;
356 /// \return true if the target supports alias from the unmangled name to the
357 /// mangled name of functions declared within an extern "C" region and marked
358 /// as 'used', and having internal linkage.
359 virtual bool shouldEmitStaticExternCAliases() const { return true; }
361 /// \return true if annonymous zero-sized bitfields should be emitted to
362 /// correctly distinguish between struct types whose memory layout is the
363 /// same, but whose layout may differ when used as argument passed by value
364 virtual bool shouldEmitDWARFBitFieldSeparators() const { return false; }
366 virtual void setCUDAKernelCallingConvention(const FunctionType *&FT) const {}
368 /// Return the device-side type for the CUDA device builtin surface type.
369 virtual llvm::Type *getCUDADeviceBuiltinSurfaceDeviceType() const {
370 // By default, no change from the original one.
371 return nullptr;
373 /// Return the device-side type for the CUDA device builtin texture type.
374 virtual llvm::Type *getCUDADeviceBuiltinTextureDeviceType() const {
375 // By default, no change from the original one.
376 return nullptr;
379 /// Return the WebAssembly externref reference type.
380 virtual llvm::Type *getWasmExternrefReferenceType() const { return nullptr; }
382 /// Return the WebAssembly funcref reference type.
383 virtual llvm::Type *getWasmFuncrefReferenceType() const { return nullptr; }
385 /// Emit the device-side copy of the builtin surface type.
386 virtual bool emitCUDADeviceBuiltinSurfaceDeviceCopy(CodeGenFunction &CGF,
387 LValue Dst,
388 LValue Src) const {
389 // DO NOTHING by default.
390 return false;
392 /// Emit the device-side copy of the builtin texture type.
393 virtual bool emitCUDADeviceBuiltinTextureDeviceCopy(CodeGenFunction &CGF,
394 LValue Dst,
395 LValue Src) const {
396 // DO NOTHING by default.
397 return false;
400 /// Return an LLVM type that corresponds to an OpenCL type.
401 virtual llvm::Type *getOpenCLType(CodeGenModule &CGM, const Type *T) const {
402 return nullptr;
405 protected:
406 static std::string qualifyWindowsLibrary(StringRef Lib);
408 void addStackProbeTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
409 CodeGen::CodeGenModule &CGM) const;
412 std::unique_ptr<TargetCodeGenInfo>
413 createDefaultTargetCodeGenInfo(CodeGenModule &CGM);
415 enum class AArch64ABIKind {
416 AAPCS = 0,
417 DarwinPCS,
418 Win64,
421 std::unique_ptr<TargetCodeGenInfo>
422 createAArch64TargetCodeGenInfo(CodeGenModule &CGM, AArch64ABIKind Kind);
424 std::unique_ptr<TargetCodeGenInfo>
425 createWindowsAArch64TargetCodeGenInfo(CodeGenModule &CGM, AArch64ABIKind K);
427 std::unique_ptr<TargetCodeGenInfo>
428 createAMDGPUTargetCodeGenInfo(CodeGenModule &CGM);
430 std::unique_ptr<TargetCodeGenInfo>
431 createARCTargetCodeGenInfo(CodeGenModule &CGM);
433 enum class ARMABIKind {
434 APCS = 0,
435 AAPCS = 1,
436 AAPCS_VFP = 2,
437 AAPCS16_VFP = 3,
440 std::unique_ptr<TargetCodeGenInfo>
441 createARMTargetCodeGenInfo(CodeGenModule &CGM, ARMABIKind Kind);
443 std::unique_ptr<TargetCodeGenInfo>
444 createWindowsARMTargetCodeGenInfo(CodeGenModule &CGM, ARMABIKind K);
446 std::unique_ptr<TargetCodeGenInfo>
447 createAVRTargetCodeGenInfo(CodeGenModule &CGM, unsigned NPR, unsigned NRR);
449 std::unique_ptr<TargetCodeGenInfo>
450 createBPFTargetCodeGenInfo(CodeGenModule &CGM);
452 std::unique_ptr<TargetCodeGenInfo>
453 createCSKYTargetCodeGenInfo(CodeGenModule &CGM, unsigned FLen);
455 std::unique_ptr<TargetCodeGenInfo>
456 createHexagonTargetCodeGenInfo(CodeGenModule &CGM);
458 std::unique_ptr<TargetCodeGenInfo>
459 createLanaiTargetCodeGenInfo(CodeGenModule &CGM);
461 std::unique_ptr<TargetCodeGenInfo>
462 createLoongArchTargetCodeGenInfo(CodeGenModule &CGM, unsigned GRLen,
463 unsigned FLen);
465 std::unique_ptr<TargetCodeGenInfo>
466 createM68kTargetCodeGenInfo(CodeGenModule &CGM);
468 std::unique_ptr<TargetCodeGenInfo>
469 createMIPSTargetCodeGenInfo(CodeGenModule &CGM, bool IsOS32);
471 std::unique_ptr<TargetCodeGenInfo>
472 createMSP430TargetCodeGenInfo(CodeGenModule &CGM);
474 std::unique_ptr<TargetCodeGenInfo>
475 createNVPTXTargetCodeGenInfo(CodeGenModule &CGM);
477 std::unique_ptr<TargetCodeGenInfo>
478 createPNaClTargetCodeGenInfo(CodeGenModule &CGM);
480 enum class PPC64_SVR4_ABIKind {
481 ELFv1 = 0,
482 ELFv2,
485 std::unique_ptr<TargetCodeGenInfo>
486 createAIXTargetCodeGenInfo(CodeGenModule &CGM, bool Is64Bit);
488 std::unique_ptr<TargetCodeGenInfo>
489 createPPC32TargetCodeGenInfo(CodeGenModule &CGM, bool SoftFloatABI);
491 std::unique_ptr<TargetCodeGenInfo>
492 createPPC64TargetCodeGenInfo(CodeGenModule &CGM);
494 std::unique_ptr<TargetCodeGenInfo>
495 createPPC64_SVR4_TargetCodeGenInfo(CodeGenModule &CGM, PPC64_SVR4_ABIKind Kind,
496 bool SoftFloatABI);
498 std::unique_ptr<TargetCodeGenInfo>
499 createRISCVTargetCodeGenInfo(CodeGenModule &CGM, unsigned XLen, unsigned FLen,
500 bool EABI);
502 std::unique_ptr<TargetCodeGenInfo>
503 createCommonSPIRTargetCodeGenInfo(CodeGenModule &CGM);
505 std::unique_ptr<TargetCodeGenInfo>
506 createSPIRVTargetCodeGenInfo(CodeGenModule &CGM);
508 std::unique_ptr<TargetCodeGenInfo>
509 createSparcV8TargetCodeGenInfo(CodeGenModule &CGM);
511 std::unique_ptr<TargetCodeGenInfo>
512 createSparcV9TargetCodeGenInfo(CodeGenModule &CGM);
514 std::unique_ptr<TargetCodeGenInfo>
515 createSystemZTargetCodeGenInfo(CodeGenModule &CGM, bool HasVector,
516 bool SoftFloatABI);
518 std::unique_ptr<TargetCodeGenInfo>
519 createTCETargetCodeGenInfo(CodeGenModule &CGM);
521 std::unique_ptr<TargetCodeGenInfo>
522 createVETargetCodeGenInfo(CodeGenModule &CGM);
524 enum class WebAssemblyABIKind {
525 MVP = 0,
526 ExperimentalMV = 1,
529 std::unique_ptr<TargetCodeGenInfo>
530 createWebAssemblyTargetCodeGenInfo(CodeGenModule &CGM, WebAssemblyABIKind K);
532 /// The AVX ABI level for X86 targets.
533 enum class X86AVXABILevel {
534 None,
535 AVX,
536 AVX512,
539 std::unique_ptr<TargetCodeGenInfo> createX86_32TargetCodeGenInfo(
540 CodeGenModule &CGM, bool DarwinVectorABI, bool Win32StructABI,
541 unsigned NumRegisterParameters, bool SoftFloatABI);
543 std::unique_ptr<TargetCodeGenInfo>
544 createWinX86_32TargetCodeGenInfo(CodeGenModule &CGM, bool DarwinVectorABI,
545 bool Win32StructABI,
546 unsigned NumRegisterParameters);
548 std::unique_ptr<TargetCodeGenInfo>
549 createX86_64TargetCodeGenInfo(CodeGenModule &CGM, X86AVXABILevel AVXLevel);
551 std::unique_ptr<TargetCodeGenInfo>
552 createWinX86_64TargetCodeGenInfo(CodeGenModule &CGM, X86AVXABILevel AVXLevel);
554 std::unique_ptr<TargetCodeGenInfo>
555 createXCoreTargetCodeGenInfo(CodeGenModule &CGM);
557 } // namespace CodeGen
558 } // namespace clang
560 #endif // LLVM_CLANG_LIB_CODEGEN_TARGETINFO_H