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 "CodeGenModule.h"
19 #include "clang/AST/Type.h"
20 #include "clang/Basic/LLVM.h"
21 #include "clang/Basic/SyncScope.h"
22 #include "llvm/ADT/SmallString.h"
23 #include "llvm/ADT/StringRef.h"
38 class CodeGenFunction;
42 /// TargetCodeGenInfo - This class organizes various target-specific
43 /// codegeneration issues, like target-specific attributes, builtins and so
45 class TargetCodeGenInfo {
46 std::unique_ptr<ABIInfo> Info = nullptr;
49 TargetCodeGenInfo(std::unique_ptr<ABIInfo> Info) : Info(std::move(Info)) {}
50 virtual ~TargetCodeGenInfo();
52 /// getABIInfo() - Returns ABI info helper for the target.
53 const ABIInfo &getABIInfo() const { return *Info; }
55 /// setTargetAttributes - Provides a convenient hook to handle extra
56 /// target-specific attributes for the given global.
57 virtual void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
58 CodeGen::CodeGenModule &M) const {}
60 /// emitTargetMetadata - Provides a convenient hook to handle extra
61 /// target-specific metadata for the given globals.
62 virtual void emitTargetMetadata(
63 CodeGen::CodeGenModule &CGM,
64 const llvm::MapVector<GlobalDecl, StringRef> &MangledDeclNames) const {}
66 /// Any further codegen related checks that need to be done on a function call
67 /// in a target specific manner.
68 virtual void checkFunctionCallABI(CodeGenModule &CGM, SourceLocation CallLoc,
69 const FunctionDecl *Caller,
70 const FunctionDecl *Callee,
71 const CallArgList &Args) const {}
73 /// Determines the size of struct _Unwind_Exception on this platform,
74 /// in 8-bit units. The Itanium ABI defines this as:
75 /// struct _Unwind_Exception {
76 /// uint64 exception_class;
77 /// _Unwind_Exception_Cleanup_Fn exception_cleanup;
81 virtual unsigned getSizeOfUnwindException() const;
83 /// Controls whether __builtin_extend_pointer should sign-extend
84 /// pointers to uint64_t or zero-extend them (the default). Has
85 /// no effect for targets:
86 /// - that have 64-bit pointers, or
87 /// - that cannot address through registers larger than pointers, or
88 /// - that implicitly ignore/truncate the top bits when addressing
89 /// through such registers.
90 virtual bool extendPointerWithSExt() const { return false; }
92 /// Determines the DWARF register number for the stack pointer, for
93 /// exception-handling purposes. Implements __builtin_dwarf_sp_column.
95 /// Returns -1 if the operation is unsupported by this target.
96 virtual int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const {
100 /// Initializes the given DWARF EH register-size table, a char*.
101 /// Implements __builtin_init_dwarf_reg_size_table.
103 /// Returns true if the operation is unsupported by this target.
104 virtual bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
105 llvm::Value *Address) const {
109 /// Performs the code-generation required to convert a return
110 /// address as stored by the system into the actual address of the
111 /// next instruction that will be executed.
113 /// Used by __builtin_extract_return_addr().
114 virtual llvm::Value *decodeReturnAddress(CodeGen::CodeGenFunction &CGF,
115 llvm::Value *Address) const {
119 /// Performs the code-generation required to convert the address
120 /// of an instruction into a return address suitable for storage
121 /// by the system in a return slot.
123 /// Used by __builtin_frob_return_addr().
124 virtual llvm::Value *encodeReturnAddress(CodeGen::CodeGenFunction &CGF,
125 llvm::Value *Address) const {
129 /// Corrects the low-level LLVM type for a given constraint and "usual"
132 /// \returns A pointer to a new LLVM type, possibly the same as the original
133 /// on success; 0 on failure.
134 virtual llvm::Type *adjustInlineAsmType(CodeGen::CodeGenFunction &CGF,
135 StringRef Constraint,
136 llvm::Type *Ty) const {
140 /// Adds constraints and types for result registers.
141 virtual void addReturnRegisterOutputs(
142 CodeGen::CodeGenFunction &CGF, CodeGen::LValue ReturnValue,
143 std::string &Constraints, std::vector<llvm::Type *> &ResultRegTypes,
144 std::vector<llvm::Type *> &ResultTruncRegTypes,
145 std::vector<CodeGen::LValue> &ResultRegDests, std::string &AsmString,
146 unsigned NumOutputs) const {}
148 /// doesReturnSlotInterfereWithArgs - Return true if the target uses an
149 /// argument slot for an 'sret' type.
150 virtual bool doesReturnSlotInterfereWithArgs() const { return true; }
152 /// Retrieve the address of a function to call immediately before
153 /// calling objc_retainAutoreleasedReturnValue. The
154 /// implementation of objc_autoreleaseReturnValue sniffs the
155 /// instruction stream following its return address to decide
156 /// whether it's a call to objc_retainAutoreleasedReturnValue.
157 /// This can be prohibitively expensive, depending on the
158 /// relocation model, and so on some targets it instead sniffs for
159 /// a particular instruction sequence. This functions returns
160 /// that instruction sequence in inline assembly, which will be
161 /// empty if none is required.
162 virtual StringRef getARCRetainAutoreleasedReturnValueMarker() const {
166 /// Determine whether a call to objc_retainAutoreleasedReturnValue should be
167 /// marked as 'notail'.
168 virtual bool shouldSuppressTailCallsOfRetainAutoreleasedReturnValue() const {
172 /// Return a constant used by UBSan as a signature to identify functions
173 /// possessing type information, or 0 if the platform is unsupported.
174 virtual llvm::Constant *
175 getUBSanFunctionSignature(CodeGen::CodeGenModule &CGM) const {
179 /// Determine whether a call to an unprototyped functions under
180 /// the given calling convention should use the variadic
181 /// convention or the non-variadic convention.
183 /// There's a good reason to make a platform's variadic calling
184 /// convention be different from its non-variadic calling
185 /// convention: the non-variadic arguments can be passed in
186 /// registers (better for performance), and the variadic arguments
187 /// can be passed on the stack (also better for performance). If
188 /// this is done, however, unprototyped functions *must* use the
189 /// non-variadic convention, because C99 states that a call
190 /// through an unprototyped function type must succeed if the
191 /// function was defined with a non-variadic prototype with
192 /// compatible parameters. Therefore, splitting the conventions
193 /// makes it impossible to call a variadic function through an
194 /// unprototyped type. Since function prototypes came out in the
195 /// late 1970s, this is probably an acceptable trade-off.
196 /// Nonetheless, not all platforms are willing to make it, and in
197 /// particularly x86-64 bends over backwards to make the
198 /// conventions compatible.
200 /// The default is false. This is correct whenever:
201 /// - the conventions are exactly the same, because it does not
202 /// matter and the resulting IR will be somewhat prettier in
203 /// certain cases; or
204 /// - the conventions are substantively different in how they pass
205 /// arguments, because in this case using the variadic convention
206 /// will lead to C99 violations.
208 /// However, some platforms make the conventions identical except
209 /// for passing additional out-of-band information to a variadic
210 /// function: for example, x86-64 passes the number of SSE
211 /// arguments in %al. On these platforms, it is desirable to
212 /// call unprototyped functions using the variadic convention so
213 /// that unprototyped calls to varargs functions still succeed.
215 /// Relatedly, platforms which pass the fixed arguments to this:
217 /// differently than they would pass them to this:
218 /// A foo(B, C, D, ...);
219 /// may need to adjust the debugger-support code in Sema to do the
220 /// right thing when calling a function with no know signature.
221 virtual bool isNoProtoCallVariadic(const CodeGen::CallArgList &args,
222 const FunctionNoProtoType *fnType) const;
224 /// Gets the linker options necessary to link a dependent library on this
226 virtual void getDependentLibraryOption(llvm::StringRef Lib,
227 llvm::SmallString<24> &Opt) const;
229 /// Gets the linker options necessary to detect object file mismatches on
231 virtual void getDetectMismatchOption(llvm::StringRef Name,
232 llvm::StringRef Value,
233 llvm::SmallString<32> &Opt) const {}
235 /// Get LLVM calling convention for OpenCL kernel.
236 virtual unsigned getOpenCLKernelCallingConv() const;
238 /// Get target specific null pointer.
239 /// \param T is the LLVM type of the null pointer.
240 /// \param QT is the clang QualType of the null pointer.
241 /// \return ConstantPointerNull with the given type \p T.
242 /// Each target can override it to return its own desired constant value.
243 virtual llvm::Constant *getNullPointer(const CodeGen::CodeGenModule &CGM,
244 llvm::PointerType *T, QualType QT) const;
246 /// Get target favored AST address space of a global variable for languages
247 /// other than OpenCL and CUDA.
248 /// If \p D is nullptr, returns the default target favored address space
249 /// for global variable.
250 virtual LangAS getGlobalVarAddressSpace(CodeGenModule &CGM,
251 const VarDecl *D) const;
253 /// Get the AST address space for alloca.
254 virtual LangAS getASTAllocaAddressSpace() const { return LangAS::Default; }
256 /// Perform address space cast of an expression of pointer type.
257 /// \param V is the LLVM value to be casted to another address space.
258 /// \param SrcAddr is the language address space of \p V.
259 /// \param DestAddr is the targeted language address space.
260 /// \param DestTy is the destination LLVM pointer type.
261 /// \param IsNonNull is the flag indicating \p V is known to be non null.
262 virtual llvm::Value *performAddrSpaceCast(CodeGen::CodeGenFunction &CGF,
263 llvm::Value *V, LangAS SrcAddr,
264 LangAS DestAddr, llvm::Type *DestTy,
265 bool IsNonNull = false) const;
267 /// Perform address space cast of a constant expression of pointer type.
268 /// \param V is the LLVM constant to be casted to another address space.
269 /// \param SrcAddr is the language address space of \p V.
270 /// \param DestAddr is the targeted language address space.
271 /// \param DestTy is the destination LLVM pointer type.
272 virtual llvm::Constant *performAddrSpaceCast(CodeGenModule &CGM,
274 LangAS SrcAddr, LangAS DestAddr,
275 llvm::Type *DestTy) const;
277 /// Get address space of pointer parameter for __cxa_atexit.
278 virtual LangAS getAddrSpaceOfCxaAtexitPtrParam() const {
279 return LangAS::Default;
282 /// Get the syncscope used in LLVM IR.
283 virtual llvm::SyncScope::ID getLLVMSyncScopeID(const LangOptions &LangOpts,
285 llvm::AtomicOrdering Ordering,
286 llvm::LLVMContext &Ctx) const;
288 /// Interface class for filling custom fields of a block literal for OpenCL.
289 class TargetOpenCLBlockHelper {
291 typedef std::pair<llvm::Value *, StringRef> ValueTy;
292 TargetOpenCLBlockHelper() {}
293 virtual ~TargetOpenCLBlockHelper() {}
294 /// Get the custom field types for OpenCL blocks.
295 virtual llvm::SmallVector<llvm::Type *, 1> getCustomFieldTypes() = 0;
296 /// Get the custom field values for OpenCL blocks.
297 virtual llvm::SmallVector<ValueTy, 1>
298 getCustomFieldValues(CodeGenFunction &CGF, const CGBlockInfo &Info) = 0;
299 virtual bool areAllCustomFieldValuesConstant(const CGBlockInfo &Info) = 0;
300 /// Get the custom field values for OpenCL blocks if all values are LLVM
302 virtual llvm::SmallVector<llvm::Constant *, 1>
303 getCustomFieldValues(CodeGenModule &CGM, const CGBlockInfo &Info) = 0;
305 virtual TargetOpenCLBlockHelper *getTargetOpenCLBlockHelper() const {
309 /// Create an OpenCL kernel for an enqueued block. The kernel function is
310 /// a wrapper for the block invoke function with target-specific calling
311 /// convention and ABI as an OpenCL kernel. The wrapper function accepts
312 /// block context and block arguments in target-specific way and calls
313 /// the original block invoke function.
314 virtual llvm::Function *
315 createEnqueuedBlockKernel(CodeGenFunction &CGF,
316 llvm::Function *BlockInvokeFunc,
317 llvm::Value *BlockLiteral) const;
319 /// \return true if the target supports alias from the unmangled name to the
320 /// mangled name of functions declared within an extern "C" region and marked
321 /// as 'used', and having internal linkage.
322 virtual bool shouldEmitStaticExternCAliases() const { return true; }
324 virtual void setCUDAKernelCallingConvention(const FunctionType *&FT) const {}
326 /// Return the device-side type for the CUDA device builtin surface type.
327 virtual llvm::Type *getCUDADeviceBuiltinSurfaceDeviceType() const {
328 // By default, no change from the original one.
331 /// Return the device-side type for the CUDA device builtin texture type.
332 virtual llvm::Type *getCUDADeviceBuiltinTextureDeviceType() const {
333 // By default, no change from the original one.
337 /// Emit the device-side copy of the builtin surface type.
338 virtual bool emitCUDADeviceBuiltinSurfaceDeviceCopy(CodeGenFunction &CGF,
341 // DO NOTHING by default.
344 /// Emit the device-side copy of the builtin texture type.
345 virtual bool emitCUDADeviceBuiltinTextureDeviceCopy(CodeGenFunction &CGF,
348 // DO NOTHING by default.
353 } // namespace CodeGen
356 #endif // LLVM_CLANG_LIB_CODEGEN_TARGETINFO_H