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"
18 #include "CodeGenModule.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"
39 class CodeGenFunction
;
43 /// TargetCodeGenInfo - This class organizes various target-specific
44 /// codegeneration issues, like target-specific attributes, builtins and so
46 class TargetCodeGenInfo
{
47 std::unique_ptr
<ABIInfo
> Info
;
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
);
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");
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 /// Any further codegen related checks that need to be done on a function call
85 /// in a target specific manner.
86 virtual void checkFunctionCallABI(CodeGenModule
&CGM
, SourceLocation CallLoc
,
87 const FunctionDecl
*Caller
,
88 const FunctionDecl
*Callee
,
89 const CallArgList
&Args
) const {}
91 /// Determines the size of struct _Unwind_Exception on this platform,
92 /// in 8-bit units. The Itanium ABI defines this as:
93 /// struct _Unwind_Exception {
94 /// uint64 exception_class;
95 /// _Unwind_Exception_Cleanup_Fn exception_cleanup;
99 virtual unsigned getSizeOfUnwindException() const;
101 /// Controls whether __builtin_extend_pointer should sign-extend
102 /// pointers to uint64_t or zero-extend them (the default). Has
103 /// no effect for targets:
104 /// - that have 64-bit pointers, or
105 /// - that cannot address through registers larger than pointers, or
106 /// - that implicitly ignore/truncate the top bits when addressing
107 /// through such registers.
108 virtual bool extendPointerWithSExt() const { return false; }
110 /// Determines the DWARF register number for the stack pointer, for
111 /// exception-handling purposes. Implements __builtin_dwarf_sp_column.
113 /// Returns -1 if the operation is unsupported by this target.
114 virtual int getDwarfEHStackPointer(CodeGen::CodeGenModule
&M
) const {
118 /// Initializes the given DWARF EH register-size table, a char*.
119 /// Implements __builtin_init_dwarf_reg_size_table.
121 /// Returns true if the operation is unsupported by this target.
122 virtual bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction
&CGF
,
123 llvm::Value
*Address
) const {
127 /// Performs the code-generation required to convert a return
128 /// address as stored by the system into the actual address of the
129 /// next instruction that will be executed.
131 /// Used by __builtin_extract_return_addr().
132 virtual llvm::Value
*decodeReturnAddress(CodeGen::CodeGenFunction
&CGF
,
133 llvm::Value
*Address
) const {
137 /// Performs the code-generation required to convert the address
138 /// of an instruction into a return address suitable for storage
139 /// by the system in a return slot.
141 /// Used by __builtin_frob_return_addr().
142 virtual llvm::Value
*encodeReturnAddress(CodeGen::CodeGenFunction
&CGF
,
143 llvm::Value
*Address
) const {
147 /// Performs a target specific test of a floating point value for things
148 /// like IsNaN, Infinity, ... Nullptr is returned if no implementation
150 virtual llvm::Value
*
151 testFPKind(llvm::Value
*V
, unsigned BuiltinID
, CGBuilderTy
&Builder
,
152 CodeGenModule
&CGM
) const {
153 assert(V
->getType()->isFloatingPointTy() && "V should have an FP type.");
157 /// Corrects the low-level LLVM type for a given constraint and "usual"
160 /// \returns A pointer to a new LLVM type, possibly the same as the original
161 /// on success; 0 on failure.
162 virtual llvm::Type
*adjustInlineAsmType(CodeGen::CodeGenFunction
&CGF
,
163 StringRef Constraint
,
164 llvm::Type
*Ty
) const {
168 /// Target hook to decide whether an inline asm operand can be passed
170 virtual bool isScalarizableAsmOperand(CodeGen::CodeGenFunction
&CGF
,
171 llvm::Type
*Ty
) const {
175 /// Adds constraints and types for result registers.
176 virtual void addReturnRegisterOutputs(
177 CodeGen::CodeGenFunction
&CGF
, CodeGen::LValue ReturnValue
,
178 std::string
&Constraints
, std::vector
<llvm::Type
*> &ResultRegTypes
,
179 std::vector
<llvm::Type
*> &ResultTruncRegTypes
,
180 std::vector
<CodeGen::LValue
> &ResultRegDests
, std::string
&AsmString
,
181 unsigned NumOutputs
) const {}
183 /// doesReturnSlotInterfereWithArgs - Return true if the target uses an
184 /// argument slot for an 'sret' type.
185 virtual bool doesReturnSlotInterfereWithArgs() const { return true; }
187 /// Retrieve the address of a function to call immediately before
188 /// calling objc_retainAutoreleasedReturnValue. The
189 /// implementation of objc_autoreleaseReturnValue sniffs the
190 /// instruction stream following its return address to decide
191 /// whether it's a call to objc_retainAutoreleasedReturnValue.
192 /// This can be prohibitively expensive, depending on the
193 /// relocation model, and so on some targets it instead sniffs for
194 /// a particular instruction sequence. This functions returns
195 /// that instruction sequence in inline assembly, which will be
196 /// empty if none is required.
197 virtual StringRef
getARCRetainAutoreleasedReturnValueMarker() const {
201 /// Determine whether a call to objc_retainAutoreleasedReturnValue or
202 /// objc_unsafeClaimAutoreleasedReturnValue should be marked as 'notail'.
203 virtual bool markARCOptimizedReturnCallsAsNoTail() const { return false; }
205 /// Return a constant used by UBSan as a signature to identify functions
206 /// possessing type information, or 0 if the platform is unsupported.
207 /// This magic number is invalid instruction encoding in many targets.
208 virtual llvm::Constant
*
209 getUBSanFunctionSignature(CodeGen::CodeGenModule
&CGM
) const {
210 return llvm::ConstantInt::get(CGM
.Int32Ty
, 0xc105cafe);
213 /// Determine whether a call to an unprototyped functions under
214 /// the given calling convention should use the variadic
215 /// convention or the non-variadic convention.
217 /// There's a good reason to make a platform's variadic calling
218 /// convention be different from its non-variadic calling
219 /// convention: the non-variadic arguments can be passed in
220 /// registers (better for performance), and the variadic arguments
221 /// can be passed on the stack (also better for performance). If
222 /// this is done, however, unprototyped functions *must* use the
223 /// non-variadic convention, because C99 states that a call
224 /// through an unprototyped function type must succeed if the
225 /// function was defined with a non-variadic prototype with
226 /// compatible parameters. Therefore, splitting the conventions
227 /// makes it impossible to call a variadic function through an
228 /// unprototyped type. Since function prototypes came out in the
229 /// late 1970s, this is probably an acceptable trade-off.
230 /// Nonetheless, not all platforms are willing to make it, and in
231 /// particularly x86-64 bends over backwards to make the
232 /// conventions compatible.
234 /// The default is false. This is correct whenever:
235 /// - the conventions are exactly the same, because it does not
236 /// matter and the resulting IR will be somewhat prettier in
237 /// certain cases; or
238 /// - the conventions are substantively different in how they pass
239 /// arguments, because in this case using the variadic convention
240 /// will lead to C99 violations.
242 /// However, some platforms make the conventions identical except
243 /// for passing additional out-of-band information to a variadic
244 /// function: for example, x86-64 passes the number of SSE
245 /// arguments in %al. On these platforms, it is desirable to
246 /// call unprototyped functions using the variadic convention so
247 /// that unprototyped calls to varargs functions still succeed.
249 /// Relatedly, platforms which pass the fixed arguments to this:
251 /// differently than they would pass them to this:
252 /// A foo(B, C, D, ...);
253 /// may need to adjust the debugger-support code in Sema to do the
254 /// right thing when calling a function with no know signature.
255 virtual bool isNoProtoCallVariadic(const CodeGen::CallArgList
&args
,
256 const FunctionNoProtoType
*fnType
) const;
258 /// Gets the linker options necessary to link a dependent library on this
260 virtual void getDependentLibraryOption(llvm::StringRef Lib
,
261 llvm::SmallString
<24> &Opt
) const;
263 /// Gets the linker options necessary to detect object file mismatches on
265 virtual void getDetectMismatchOption(llvm::StringRef Name
,
266 llvm::StringRef Value
,
267 llvm::SmallString
<32> &Opt
) const {}
269 /// Get LLVM calling convention for OpenCL kernel.
270 virtual unsigned getOpenCLKernelCallingConv() const;
272 /// Get target specific null pointer.
273 /// \param T is the LLVM type of the null pointer.
274 /// \param QT is the clang QualType of the null pointer.
275 /// \return ConstantPointerNull with the given type \p T.
276 /// Each target can override it to return its own desired constant value.
277 virtual llvm::Constant
*getNullPointer(const CodeGen::CodeGenModule
&CGM
,
278 llvm::PointerType
*T
, QualType QT
) const;
280 /// Get target favored AST address space of a global variable for languages
281 /// other than OpenCL and CUDA.
282 /// If \p D is nullptr, returns the default target favored address space
283 /// for global variable.
284 virtual LangAS
getGlobalVarAddressSpace(CodeGenModule
&CGM
,
285 const VarDecl
*D
) const;
287 /// Get the AST address space for alloca.
288 virtual LangAS
getASTAllocaAddressSpace() const { return LangAS::Default
; }
290 /// Perform address space cast of an expression of pointer type.
291 /// \param V is the LLVM value to be casted to another address space.
292 /// \param SrcAddr is the language address space of \p V.
293 /// \param DestAddr is the targeted language address space.
294 /// \param DestTy is the destination LLVM pointer type.
295 /// \param IsNonNull is the flag indicating \p V is known to be non null.
296 virtual llvm::Value
*performAddrSpaceCast(CodeGen::CodeGenFunction
&CGF
,
297 llvm::Value
*V
, LangAS SrcAddr
,
298 LangAS DestAddr
, llvm::Type
*DestTy
,
299 bool IsNonNull
= false) const;
301 /// Perform address space cast of a constant expression of pointer type.
302 /// \param V is the LLVM constant to be casted to another address space.
303 /// \param SrcAddr is the language address space of \p V.
304 /// \param DestAddr is the targeted language address space.
305 /// \param DestTy is the destination LLVM pointer type.
306 virtual llvm::Constant
*performAddrSpaceCast(CodeGenModule
&CGM
,
308 LangAS SrcAddr
, LangAS DestAddr
,
309 llvm::Type
*DestTy
) const;
311 /// Get address space of pointer parameter for __cxa_atexit.
312 virtual LangAS
getAddrSpaceOfCxaAtexitPtrParam() const {
313 return LangAS::Default
;
316 /// Get the syncscope used in LLVM IR.
317 virtual llvm::SyncScope::ID
getLLVMSyncScopeID(const LangOptions
&LangOpts
,
319 llvm::AtomicOrdering Ordering
,
320 llvm::LLVMContext
&Ctx
) const;
322 /// Interface class for filling custom fields of a block literal for OpenCL.
323 class TargetOpenCLBlockHelper
{
325 typedef std::pair
<llvm::Value
*, StringRef
> ValueTy
;
326 TargetOpenCLBlockHelper() {}
327 virtual ~TargetOpenCLBlockHelper() {}
328 /// Get the custom field types for OpenCL blocks.
329 virtual llvm::SmallVector
<llvm::Type
*, 1> getCustomFieldTypes() = 0;
330 /// Get the custom field values for OpenCL blocks.
331 virtual llvm::SmallVector
<ValueTy
, 1>
332 getCustomFieldValues(CodeGenFunction
&CGF
, const CGBlockInfo
&Info
) = 0;
333 virtual bool areAllCustomFieldValuesConstant(const CGBlockInfo
&Info
) = 0;
334 /// Get the custom field values for OpenCL blocks if all values are LLVM
336 virtual llvm::SmallVector
<llvm::Constant
*, 1>
337 getCustomFieldValues(CodeGenModule
&CGM
, const CGBlockInfo
&Info
) = 0;
339 virtual TargetOpenCLBlockHelper
*getTargetOpenCLBlockHelper() const {
343 /// Create an OpenCL kernel for an enqueued block. The kernel function is
344 /// a wrapper for the block invoke function with target-specific calling
345 /// convention and ABI as an OpenCL kernel. The wrapper function accepts
346 /// block context and block arguments in target-specific way and calls
347 /// the original block invoke function.
348 virtual llvm::Value
*
349 createEnqueuedBlockKernel(CodeGenFunction
&CGF
,
350 llvm::Function
*BlockInvokeFunc
,
351 llvm::Type
*BlockTy
) const;
353 /// \return true if the target supports alias from the unmangled name to the
354 /// mangled name of functions declared within an extern "C" region and marked
355 /// as 'used', and having internal linkage.
356 virtual bool shouldEmitStaticExternCAliases() const { return true; }
358 /// \return true if annonymous zero-sized bitfields should be emitted to
359 /// correctly distinguish between struct types whose memory layout is the
360 /// same, but whose layout may differ when used as argument passed by value
361 virtual bool shouldEmitDWARFBitFieldSeparators() const { return false; }
363 virtual void setCUDAKernelCallingConvention(const FunctionType
*&FT
) const {}
365 /// Return the device-side type for the CUDA device builtin surface type.
366 virtual llvm::Type
*getCUDADeviceBuiltinSurfaceDeviceType() const {
367 // By default, no change from the original one.
370 /// Return the device-side type for the CUDA device builtin texture type.
371 virtual llvm::Type
*getCUDADeviceBuiltinTextureDeviceType() const {
372 // By default, no change from the original one.
376 /// Return the WebAssembly externref reference type.
377 virtual llvm::Type
*getWasmExternrefReferenceType() const { return nullptr; }
379 /// Return the WebAssembly funcref reference type.
380 virtual llvm::Type
*getWasmFuncrefReferenceType() const { return nullptr; }
382 /// Emit the device-side copy of the builtin surface type.
383 virtual bool emitCUDADeviceBuiltinSurfaceDeviceCopy(CodeGenFunction
&CGF
,
386 // DO NOTHING by default.
389 /// Emit the device-side copy of the builtin texture type.
390 virtual bool emitCUDADeviceBuiltinTextureDeviceCopy(CodeGenFunction
&CGF
,
393 // DO NOTHING by default.
397 /// Return an LLVM type that corresponds to an OpenCL type.
398 virtual llvm::Type
*getOpenCLType(CodeGenModule
&CGM
, const Type
*T
) const {
403 static std::string
qualifyWindowsLibrary(StringRef Lib
);
405 void addStackProbeTargetAttributes(const Decl
*D
, llvm::GlobalValue
*GV
,
406 CodeGen::CodeGenModule
&CGM
) const;
409 } // namespace CodeGen
412 #endif // LLVM_CLANG_LIB_CODEGEN_TARGETINFO_H