1 //===---- TargetInfo.h - Encapsulate target details -------------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
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"
19 #include "CodeGenModule.h"
20 #include "clang/AST/Type.h"
21 #include "clang/Basic/LLVM.h"
22 #include "clang/Basic/SyncScope.h"
23 #include "clang/Basic/TargetInfo.h"
24 #include "llvm/ADT/SmallString.h"
25 #include "llvm/ADT/StringRef.h"
40 class CodeGenFunction
;
44 /// TargetCodeGenInfo - This class organizes various target-specific
45 /// codegeneration issues, like target-specific attributes, builtins and so
47 class TargetCodeGenInfo
{
48 std::unique_ptr
<ABIInfo
> Info
;
51 // Target hooks supporting Swift calling conventions. The target must
52 // initialize this field if it claims to support these calling conventions
53 // by returning true from TargetInfo::checkCallingConvention for them.
54 std::unique_ptr
<SwiftABIInfo
> SwiftInfo
;
56 // Returns ABI info helper for the target. This is for use by derived classes.
57 template <typename T
> const T
&getABIInfo() const {
58 return static_cast<const T
&>(*Info
);
62 TargetCodeGenInfo(std::unique_ptr
<ABIInfo
> Info
);
63 virtual ~TargetCodeGenInfo();
65 /// getABIInfo() - Returns ABI info helper for the target.
66 const ABIInfo
&getABIInfo() const { return *Info
; }
68 /// Returns Swift ABI info helper for the target.
69 const SwiftABIInfo
&getSwiftABIInfo() const {
70 assert(SwiftInfo
&& "Swift ABI info has not been initialized");
74 /// setTargetAttributes - Provides a convenient hook to handle extra
75 /// target-specific attributes for the given global.
76 virtual void setTargetAttributes(const Decl
*D
, llvm::GlobalValue
*GV
,
77 CodeGen::CodeGenModule
&M
) const {}
79 /// emitTargetMetadata - Provides a convenient hook to handle extra
80 /// target-specific metadata for the given globals.
81 virtual void emitTargetMetadata(
82 CodeGen::CodeGenModule
&CGM
,
83 const llvm::MapVector
<GlobalDecl
, StringRef
> &MangledDeclNames
) const {}
85 /// Provides a convenient hook to handle extra target-specific globals.
86 virtual void emitTargetGlobals(CodeGen::CodeGenModule
&CGM
) const {}
88 /// Any further codegen related checks that need to be done on a function
89 /// signature in a target specific manner.
90 virtual void checkFunctionABI(CodeGenModule
&CGM
,
91 const FunctionDecl
*Decl
) const {}
93 /// Any further codegen related checks that need to be done on a function call
94 /// in a target specific manner.
95 virtual void checkFunctionCallABI(CodeGenModule
&CGM
, SourceLocation CallLoc
,
96 const FunctionDecl
*Caller
,
97 const FunctionDecl
*Callee
,
98 const CallArgList
&Args
,
99 QualType ReturnType
) const {}
101 /// Determines the size of struct _Unwind_Exception on this platform,
102 /// in 8-bit units. The Itanium ABI defines this as:
103 /// struct _Unwind_Exception {
104 /// uint64 exception_class;
105 /// _Unwind_Exception_Cleanup_Fn exception_cleanup;
106 /// uint64 private_1;
107 /// uint64 private_2;
109 virtual unsigned getSizeOfUnwindException() const;
111 /// Controls whether __builtin_extend_pointer should sign-extend
112 /// pointers to uint64_t or zero-extend them (the default). Has
113 /// no effect for targets:
114 /// - that have 64-bit pointers, or
115 /// - that cannot address through registers larger than pointers, or
116 /// - that implicitly ignore/truncate the top bits when addressing
117 /// through such registers.
118 virtual bool extendPointerWithSExt() const { return false; }
120 /// Determines the DWARF register number for the stack pointer, for
121 /// exception-handling purposes. Implements __builtin_dwarf_sp_column.
123 /// Returns -1 if the operation is unsupported by this target.
124 virtual int getDwarfEHStackPointer(CodeGen::CodeGenModule
&M
) const {
128 /// Initializes the given DWARF EH register-size table, a char*.
129 /// Implements __builtin_init_dwarf_reg_size_table.
131 /// Returns true if the operation is unsupported by this target.
132 virtual bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction
&CGF
,
133 llvm::Value
*Address
) const {
137 /// Performs the code-generation required to convert a return
138 /// address as stored by the system into the actual address of the
139 /// next instruction that will be executed.
141 /// Used by __builtin_extract_return_addr().
142 virtual llvm::Value
*decodeReturnAddress(CodeGen::CodeGenFunction
&CGF
,
143 llvm::Value
*Address
) const {
147 /// Performs the code-generation required to convert the address
148 /// of an instruction into a return address suitable for storage
149 /// by the system in a return slot.
151 /// Used by __builtin_frob_return_addr().
152 virtual llvm::Value
*encodeReturnAddress(CodeGen::CodeGenFunction
&CGF
,
153 llvm::Value
*Address
) const {
157 /// Performs a target specific test of a floating point value for things
158 /// like IsNaN, Infinity, ... Nullptr is returned if no implementation
160 virtual llvm::Value
*
161 testFPKind(llvm::Value
*V
, unsigned BuiltinID
, CGBuilderTy
&Builder
,
162 CodeGenModule
&CGM
) const {
163 assert(V
->getType()->isFloatingPointTy() && "V should have an FP type.");
167 /// Corrects the low-level LLVM type for a given constraint and "usual"
170 /// \returns A pointer to a new LLVM type, possibly the same as the original
171 /// on success; 0 on failure.
172 virtual llvm::Type
*adjustInlineAsmType(CodeGen::CodeGenFunction
&CGF
,
173 StringRef Constraint
,
174 llvm::Type
*Ty
) const {
178 /// Target hook to decide whether an inline asm operand can be passed
180 virtual bool isScalarizableAsmOperand(CodeGen::CodeGenFunction
&CGF
,
181 llvm::Type
*Ty
) const {
185 /// Adds constraints and types for result registers.
186 virtual void addReturnRegisterOutputs(
187 CodeGen::CodeGenFunction
&CGF
, CodeGen::LValue ReturnValue
,
188 std::string
&Constraints
, std::vector
<llvm::Type
*> &ResultRegTypes
,
189 std::vector
<llvm::Type
*> &ResultTruncRegTypes
,
190 std::vector
<CodeGen::LValue
> &ResultRegDests
, std::string
&AsmString
,
191 unsigned NumOutputs
) const {}
193 /// doesReturnSlotInterfereWithArgs - Return true if the target uses an
194 /// argument slot for an 'sret' type.
195 virtual bool doesReturnSlotInterfereWithArgs() const { return true; }
197 /// Retrieve the address of a function to call immediately before
198 /// calling objc_retainAutoreleasedReturnValue. The
199 /// implementation of objc_autoreleaseReturnValue sniffs the
200 /// instruction stream following its return address to decide
201 /// whether it's a call to objc_retainAutoreleasedReturnValue.
202 /// This can be prohibitively expensive, depending on the
203 /// relocation model, and so on some targets it instead sniffs for
204 /// a particular instruction sequence. This functions returns
205 /// that instruction sequence in inline assembly, which will be
206 /// empty if none is required.
207 virtual StringRef
getARCRetainAutoreleasedReturnValueMarker() const {
211 /// Determine whether a call to objc_retainAutoreleasedReturnValue or
212 /// objc_unsafeClaimAutoreleasedReturnValue should be marked as 'notail'.
213 virtual bool markARCOptimizedReturnCallsAsNoTail() const { return false; }
215 /// Return a constant used by UBSan as a signature to identify functions
216 /// possessing type information, or 0 if the platform is unsupported.
217 /// This magic number is invalid instruction encoding in many targets.
218 virtual llvm::Constant
*
219 getUBSanFunctionSignature(CodeGen::CodeGenModule
&CGM
) const {
220 return llvm::ConstantInt::get(CGM
.Int32Ty
, 0xc105cafe);
223 /// Determine whether a call to an unprototyped functions under
224 /// the given calling convention should use the variadic
225 /// convention or the non-variadic convention.
227 /// There's a good reason to make a platform's variadic calling
228 /// convention be different from its non-variadic calling
229 /// convention: the non-variadic arguments can be passed in
230 /// registers (better for performance), and the variadic arguments
231 /// can be passed on the stack (also better for performance). If
232 /// this is done, however, unprototyped functions *must* use the
233 /// non-variadic convention, because C99 states that a call
234 /// through an unprototyped function type must succeed if the
235 /// function was defined with a non-variadic prototype with
236 /// compatible parameters. Therefore, splitting the conventions
237 /// makes it impossible to call a variadic function through an
238 /// unprototyped type. Since function prototypes came out in the
239 /// late 1970s, this is probably an acceptable trade-off.
240 /// Nonetheless, not all platforms are willing to make it, and in
241 /// particularly x86-64 bends over backwards to make the
242 /// conventions compatible.
244 /// The default is false. This is correct whenever:
245 /// - the conventions are exactly the same, because it does not
246 /// matter and the resulting IR will be somewhat prettier in
247 /// certain cases; or
248 /// - the conventions are substantively different in how they pass
249 /// arguments, because in this case using the variadic convention
250 /// will lead to C99 violations.
252 /// However, some platforms make the conventions identical except
253 /// for passing additional out-of-band information to a variadic
254 /// function: for example, x86-64 passes the number of SSE
255 /// arguments in %al. On these platforms, it is desirable to
256 /// call unprototyped functions using the variadic convention so
257 /// that unprototyped calls to varargs functions still succeed.
259 /// Relatedly, platforms which pass the fixed arguments to this:
261 /// differently than they would pass them to this:
262 /// A foo(B, C, D, ...);
263 /// may need to adjust the debugger-support code in Sema to do the
264 /// right thing when calling a function with no know signature.
265 virtual bool isNoProtoCallVariadic(const CodeGen::CallArgList
&args
,
266 const FunctionNoProtoType
*fnType
) const;
268 /// Gets the linker options necessary to link a dependent library on this
270 virtual void getDependentLibraryOption(llvm::StringRef Lib
,
271 llvm::SmallString
<24> &Opt
) const;
273 /// Gets the linker options necessary to detect object file mismatches on
275 virtual void getDetectMismatchOption(llvm::StringRef Name
,
276 llvm::StringRef Value
,
277 llvm::SmallString
<32> &Opt
) const {}
279 /// Get LLVM calling convention for OpenCL kernel.
280 virtual unsigned getOpenCLKernelCallingConv() const;
282 /// Get target specific null pointer.
283 /// \param T is the LLVM type of the null pointer.
284 /// \param QT is the clang QualType of the null pointer.
285 /// \return ConstantPointerNull with the given type \p T.
286 /// Each target can override it to return its own desired constant value.
287 virtual llvm::Constant
*getNullPointer(const CodeGen::CodeGenModule
&CGM
,
288 llvm::PointerType
*T
, QualType QT
) const;
290 /// Get target favored AST address space of a global variable for languages
291 /// other than OpenCL and CUDA.
292 /// If \p D is nullptr, returns the default target favored address space
293 /// for global variable.
294 virtual LangAS
getGlobalVarAddressSpace(CodeGenModule
&CGM
,
295 const VarDecl
*D
) const;
297 /// Get the AST address space for alloca.
298 virtual LangAS
getASTAllocaAddressSpace() const { return LangAS::Default
; }
300 Address
performAddrSpaceCast(CodeGen::CodeGenFunction
&CGF
, Address Addr
,
301 LangAS SrcAddr
, LangAS DestAddr
,
303 bool IsNonNull
= false) const;
305 /// Perform address space cast of an expression of pointer type.
306 /// \param V is the LLVM value to be casted to another address space.
307 /// \param SrcAddr is the language address space of \p V.
308 /// \param DestAddr is the targeted language address space.
309 /// \param DestTy is the destination LLVM pointer type.
310 /// \param IsNonNull is the flag indicating \p V is known to be non null.
311 virtual llvm::Value
*performAddrSpaceCast(CodeGen::CodeGenFunction
&CGF
,
312 llvm::Value
*V
, LangAS SrcAddr
,
313 LangAS DestAddr
, llvm::Type
*DestTy
,
314 bool IsNonNull
= false) const;
316 /// Perform address space cast of a constant expression of pointer type.
317 /// \param V is the LLVM constant to be casted to another address space.
318 /// \param SrcAddr is the language address space of \p V.
319 /// \param DestAddr is the targeted language address space.
320 /// \param DestTy is the destination LLVM pointer type.
321 virtual llvm::Constant
*performAddrSpaceCast(CodeGenModule
&CGM
,
323 LangAS SrcAddr
, LangAS DestAddr
,
324 llvm::Type
*DestTy
) const;
326 /// Get address space of pointer parameter for __cxa_atexit.
327 virtual LangAS
getAddrSpaceOfCxaAtexitPtrParam() const {
328 return LangAS::Default
;
331 /// Get the syncscope used in LLVM IR.
332 virtual llvm::SyncScope::ID
getLLVMSyncScopeID(const LangOptions
&LangOpts
,
334 llvm::AtomicOrdering Ordering
,
335 llvm::LLVMContext
&Ctx
) const;
337 /// Interface class for filling custom fields of a block literal for OpenCL.
338 class TargetOpenCLBlockHelper
{
340 typedef std::pair
<llvm::Value
*, StringRef
> ValueTy
;
341 TargetOpenCLBlockHelper() {}
342 virtual ~TargetOpenCLBlockHelper() {}
343 /// Get the custom field types for OpenCL blocks.
344 virtual llvm::SmallVector
<llvm::Type
*, 1> getCustomFieldTypes() = 0;
345 /// Get the custom field values for OpenCL blocks.
346 virtual llvm::SmallVector
<ValueTy
, 1>
347 getCustomFieldValues(CodeGenFunction
&CGF
, const CGBlockInfo
&Info
) = 0;
348 virtual bool areAllCustomFieldValuesConstant(const CGBlockInfo
&Info
) = 0;
349 /// Get the custom field values for OpenCL blocks if all values are LLVM
351 virtual llvm::SmallVector
<llvm::Constant
*, 1>
352 getCustomFieldValues(CodeGenModule
&CGM
, const CGBlockInfo
&Info
) = 0;
354 virtual TargetOpenCLBlockHelper
*getTargetOpenCLBlockHelper() const {
358 /// Create an OpenCL kernel for an enqueued block. The kernel function is
359 /// a wrapper for the block invoke function with target-specific calling
360 /// convention and ABI as an OpenCL kernel. The wrapper function accepts
361 /// block context and block arguments in target-specific way and calls
362 /// the original block invoke function.
363 virtual llvm::Value
*
364 createEnqueuedBlockKernel(CodeGenFunction
&CGF
,
365 llvm::Function
*BlockInvokeFunc
,
366 llvm::Type
*BlockTy
) const;
368 /// \return true if the target supports alias from the unmangled name to the
369 /// mangled name of functions declared within an extern "C" region and marked
370 /// as 'used', and having internal linkage.
371 virtual bool shouldEmitStaticExternCAliases() const { return true; }
373 /// \return true if annonymous zero-sized bitfields should be emitted to
374 /// correctly distinguish between struct types whose memory layout is the
375 /// same, but whose layout may differ when used as argument passed by value
376 virtual bool shouldEmitDWARFBitFieldSeparators() const { return false; }
378 virtual void setCUDAKernelCallingConvention(const FunctionType
*&FT
) const {}
380 /// Return the device-side type for the CUDA device builtin surface type.
381 virtual llvm::Type
*getCUDADeviceBuiltinSurfaceDeviceType() const {
382 // By default, no change from the original one.
385 /// Return the device-side type for the CUDA device builtin texture type.
386 virtual llvm::Type
*getCUDADeviceBuiltinTextureDeviceType() const {
387 // By default, no change from the original one.
391 /// Return the WebAssembly externref reference type.
392 virtual llvm::Type
*getWasmExternrefReferenceType() const { return nullptr; }
394 /// Return the WebAssembly funcref reference type.
395 virtual llvm::Type
*getWasmFuncrefReferenceType() const { return nullptr; }
397 /// Emit the device-side copy of the builtin surface type.
398 virtual bool emitCUDADeviceBuiltinSurfaceDeviceCopy(CodeGenFunction
&CGF
,
401 // DO NOTHING by default.
404 /// Emit the device-side copy of the builtin texture type.
405 virtual bool emitCUDADeviceBuiltinTextureDeviceCopy(CodeGenFunction
&CGF
,
408 // DO NOTHING by default.
412 /// Return an LLVM type that corresponds to an OpenCL type.
413 virtual llvm::Type
*getOpenCLType(CodeGenModule
&CGM
, const Type
*T
) const {
417 // Set the Branch Protection Attributes of the Function accordingly to the
418 // BPI. Remove attributes that contradict with current BPI.
420 setBranchProtectionFnAttributes(const TargetInfo::BranchProtectionInfo
&BPI
,
423 // Add the Branch Protection Attributes of the FuncAttrs.
425 initBranchProtectionFnAttributes(const TargetInfo::BranchProtectionInfo
&BPI
,
426 llvm::AttrBuilder
&FuncAttrs
);
429 static std::string
qualifyWindowsLibrary(StringRef Lib
);
431 void addStackProbeTargetAttributes(const Decl
*D
, llvm::GlobalValue
*GV
,
432 CodeGen::CodeGenModule
&CGM
) const;
435 std::unique_ptr
<TargetCodeGenInfo
>
436 createDefaultTargetCodeGenInfo(CodeGenModule
&CGM
);
438 enum class AArch64ABIKind
{
446 std::unique_ptr
<TargetCodeGenInfo
>
447 createAArch64TargetCodeGenInfo(CodeGenModule
&CGM
, AArch64ABIKind Kind
);
449 std::unique_ptr
<TargetCodeGenInfo
>
450 createWindowsAArch64TargetCodeGenInfo(CodeGenModule
&CGM
, AArch64ABIKind K
);
452 std::unique_ptr
<TargetCodeGenInfo
>
453 createAMDGPUTargetCodeGenInfo(CodeGenModule
&CGM
);
455 std::unique_ptr
<TargetCodeGenInfo
>
456 createARCTargetCodeGenInfo(CodeGenModule
&CGM
);
458 enum class ARMABIKind
{
465 std::unique_ptr
<TargetCodeGenInfo
>
466 createARMTargetCodeGenInfo(CodeGenModule
&CGM
, ARMABIKind Kind
);
468 std::unique_ptr
<TargetCodeGenInfo
>
469 createWindowsARMTargetCodeGenInfo(CodeGenModule
&CGM
, ARMABIKind K
);
471 std::unique_ptr
<TargetCodeGenInfo
>
472 createAVRTargetCodeGenInfo(CodeGenModule
&CGM
, unsigned NPR
, unsigned NRR
);
474 std::unique_ptr
<TargetCodeGenInfo
>
475 createBPFTargetCodeGenInfo(CodeGenModule
&CGM
);
477 std::unique_ptr
<TargetCodeGenInfo
>
478 createCSKYTargetCodeGenInfo(CodeGenModule
&CGM
, unsigned FLen
);
480 std::unique_ptr
<TargetCodeGenInfo
>
481 createHexagonTargetCodeGenInfo(CodeGenModule
&CGM
);
483 std::unique_ptr
<TargetCodeGenInfo
>
484 createLanaiTargetCodeGenInfo(CodeGenModule
&CGM
);
486 std::unique_ptr
<TargetCodeGenInfo
>
487 createLoongArchTargetCodeGenInfo(CodeGenModule
&CGM
, unsigned GRLen
,
490 std::unique_ptr
<TargetCodeGenInfo
>
491 createM68kTargetCodeGenInfo(CodeGenModule
&CGM
);
493 std::unique_ptr
<TargetCodeGenInfo
>
494 createMIPSTargetCodeGenInfo(CodeGenModule
&CGM
, bool IsOS32
);
496 std::unique_ptr
<TargetCodeGenInfo
>
497 createMSP430TargetCodeGenInfo(CodeGenModule
&CGM
);
499 std::unique_ptr
<TargetCodeGenInfo
>
500 createNVPTXTargetCodeGenInfo(CodeGenModule
&CGM
);
502 std::unique_ptr
<TargetCodeGenInfo
>
503 createPNaClTargetCodeGenInfo(CodeGenModule
&CGM
);
505 enum class PPC64_SVR4_ABIKind
{
510 std::unique_ptr
<TargetCodeGenInfo
>
511 createAIXTargetCodeGenInfo(CodeGenModule
&CGM
, bool Is64Bit
);
513 std::unique_ptr
<TargetCodeGenInfo
>
514 createPPC32TargetCodeGenInfo(CodeGenModule
&CGM
, bool SoftFloatABI
);
516 std::unique_ptr
<TargetCodeGenInfo
>
517 createPPC64TargetCodeGenInfo(CodeGenModule
&CGM
);
519 std::unique_ptr
<TargetCodeGenInfo
>
520 createPPC64_SVR4_TargetCodeGenInfo(CodeGenModule
&CGM
, PPC64_SVR4_ABIKind Kind
,
523 std::unique_ptr
<TargetCodeGenInfo
>
524 createRISCVTargetCodeGenInfo(CodeGenModule
&CGM
, unsigned XLen
, unsigned FLen
,
527 std::unique_ptr
<TargetCodeGenInfo
>
528 createCommonSPIRTargetCodeGenInfo(CodeGenModule
&CGM
);
530 std::unique_ptr
<TargetCodeGenInfo
>
531 createSPIRVTargetCodeGenInfo(CodeGenModule
&CGM
);
533 std::unique_ptr
<TargetCodeGenInfo
>
534 createSparcV8TargetCodeGenInfo(CodeGenModule
&CGM
);
536 std::unique_ptr
<TargetCodeGenInfo
>
537 createSparcV9TargetCodeGenInfo(CodeGenModule
&CGM
);
539 std::unique_ptr
<TargetCodeGenInfo
>
540 createSystemZTargetCodeGenInfo(CodeGenModule
&CGM
, bool HasVector
,
543 std::unique_ptr
<TargetCodeGenInfo
>
544 createTCETargetCodeGenInfo(CodeGenModule
&CGM
);
546 std::unique_ptr
<TargetCodeGenInfo
>
547 createVETargetCodeGenInfo(CodeGenModule
&CGM
);
549 enum class WebAssemblyABIKind
{
554 std::unique_ptr
<TargetCodeGenInfo
>
555 createWebAssemblyTargetCodeGenInfo(CodeGenModule
&CGM
, WebAssemblyABIKind K
);
557 /// The AVX ABI level for X86 targets.
558 enum class X86AVXABILevel
{
564 std::unique_ptr
<TargetCodeGenInfo
> createX86_32TargetCodeGenInfo(
565 CodeGenModule
&CGM
, bool DarwinVectorABI
, bool Win32StructABI
,
566 unsigned NumRegisterParameters
, bool SoftFloatABI
);
568 std::unique_ptr
<TargetCodeGenInfo
>
569 createWinX86_32TargetCodeGenInfo(CodeGenModule
&CGM
, bool DarwinVectorABI
,
571 unsigned NumRegisterParameters
);
573 std::unique_ptr
<TargetCodeGenInfo
>
574 createX86_64TargetCodeGenInfo(CodeGenModule
&CGM
, X86AVXABILevel AVXLevel
);
576 std::unique_ptr
<TargetCodeGenInfo
>
577 createWinX86_64TargetCodeGenInfo(CodeGenModule
&CGM
, X86AVXABILevel AVXLevel
);
579 std::unique_ptr
<TargetCodeGenInfo
>
580 createXCoreTargetCodeGenInfo(CodeGenModule
&CGM
);
582 } // namespace CodeGen
585 #endif // LLVM_CLANG_LIB_CODEGEN_TARGETINFO_H