1 //===---- TargetInfo.h - Encapsulate target details -------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // These classes wrap the information about a call or function
11 // definition used to handle ABI compliancy.
13 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_CLANG_LIB_CODEGEN_TARGETINFO_H
16 #define LLVM_CLANG_LIB_CODEGEN_TARGETINFO_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 {
50 // WARNING: Acquires the ownership of ABIInfo.
51 TargetCodeGenInfo(ABIInfo *info = nullptr) : Info(info) {}
52 virtual ~TargetCodeGenInfo();
54 /// getABIInfo() - Returns ABI info helper for the target.
55 const ABIInfo &getABIInfo() const { return *Info; }
57 /// setTargetAttributes - Provides a convenient hook to handle extra
58 /// target-specific attributes for the given global.
59 virtual void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
60 CodeGen::CodeGenModule &M,
61 ForDefinition_t IsForDefinition) const {}
63 /// emitTargetMD - Provides a convenient hook to handle extra
64 /// target-specific metadata for the given global.
65 virtual void emitTargetMD(const Decl *D, llvm::GlobalValue *GV,
66 CodeGen::CodeGenModule &M) const {}
68 /// Determines the size of struct _Unwind_Exception on this platform,
69 /// in 8-bit units. The Itanium ABI defines this as:
70 /// struct _Unwind_Exception {
71 /// uint64 exception_class;
72 /// _Unwind_Exception_Cleanup_Fn exception_cleanup;
76 virtual unsigned getSizeOfUnwindException() const;
78 /// Controls whether __builtin_extend_pointer should sign-extend
79 /// pointers to uint64_t or zero-extend them (the default). Has
80 /// no effect for targets:
81 /// - that have 64-bit pointers, or
82 /// - that cannot address through registers larger than pointers, or
83 /// - that implicitly ignore/truncate the top bits when addressing
84 /// through such registers.
85 virtual bool extendPointerWithSExt() const { return false; }
87 /// Determines the DWARF register number for the stack pointer, for
88 /// exception-handling purposes. Implements __builtin_dwarf_sp_column.
90 /// Returns -1 if the operation is unsupported by this target.
91 virtual int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const {
95 /// Initializes the given DWARF EH register-size table, a char*.
96 /// Implements __builtin_init_dwarf_reg_size_table.
98 /// Returns true if the operation is unsupported by this target.
99 virtual bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
100 llvm::Value *Address) const {
104 /// Performs the code-generation required to convert a return
105 /// address as stored by the system into the actual address of the
106 /// next instruction that will be executed.
108 /// Used by __builtin_extract_return_addr().
109 virtual llvm::Value *decodeReturnAddress(CodeGen::CodeGenFunction &CGF,
110 llvm::Value *Address) const {
114 /// Performs the code-generation required to convert the address
115 /// of an instruction into a return address suitable for storage
116 /// by the system in a return slot.
118 /// Used by __builtin_frob_return_addr().
119 virtual llvm::Value *encodeReturnAddress(CodeGen::CodeGenFunction &CGF,
120 llvm::Value *Address) const {
124 /// Corrects the low-level LLVM type for a given constraint and "usual"
127 /// \returns A pointer to a new LLVM type, possibly the same as the original
128 /// on success; 0 on failure.
129 virtual llvm::Type *adjustInlineAsmType(CodeGen::CodeGenFunction &CGF,
130 StringRef Constraint,
131 llvm::Type *Ty) const {
135 /// Adds constraints and types for result registers.
136 virtual void addReturnRegisterOutputs(
137 CodeGen::CodeGenFunction &CGF, CodeGen::LValue ReturnValue,
138 std::string &Constraints, std::vector<llvm::Type *> &ResultRegTypes,
139 std::vector<llvm::Type *> &ResultTruncRegTypes,
140 std::vector<CodeGen::LValue> &ResultRegDests, std::string &AsmString,
141 unsigned NumOutputs) const {}
143 /// doesReturnSlotInterfereWithArgs - Return true if the target uses an
144 /// argument slot for an 'sret' type.
145 virtual bool doesReturnSlotInterfereWithArgs() const { return true; }
147 /// Retrieve the address of a function to call immediately before
148 /// calling objc_retainAutoreleasedReturnValue. The
149 /// implementation of objc_autoreleaseReturnValue sniffs the
150 /// instruction stream following its return address to decide
151 /// whether it's a call to objc_retainAutoreleasedReturnValue.
152 /// This can be prohibitively expensive, depending on the
153 /// relocation model, and so on some targets it instead sniffs for
154 /// a particular instruction sequence. This functions returns
155 /// that instruction sequence in inline assembly, which will be
156 /// empty if none is required.
157 virtual StringRef getARCRetainAutoreleasedReturnValueMarker() const {
161 /// Return a constant used by UBSan as a signature to identify functions
162 /// possessing type information, or 0 if the platform is unsupported.
163 virtual llvm::Constant *
164 getUBSanFunctionSignature(CodeGen::CodeGenModule &CGM) const {
168 /// Determine whether a call to an unprototyped functions under
169 /// the given calling convention should use the variadic
170 /// convention or the non-variadic convention.
172 /// There's a good reason to make a platform's variadic calling
173 /// convention be different from its non-variadic calling
174 /// convention: the non-variadic arguments can be passed in
175 /// registers (better for performance), and the variadic arguments
176 /// can be passed on the stack (also better for performance). If
177 /// this is done, however, unprototyped functions *must* use the
178 /// non-variadic convention, because C99 states that a call
179 /// through an unprototyped function type must succeed if the
180 /// function was defined with a non-variadic prototype with
181 /// compatible parameters. Therefore, splitting the conventions
182 /// makes it impossible to call a variadic function through an
183 /// unprototyped type. Since function prototypes came out in the
184 /// late 1970s, this is probably an acceptable trade-off.
185 /// Nonetheless, not all platforms are willing to make it, and in
186 /// particularly x86-64 bends over backwards to make the
187 /// conventions compatible.
189 /// The default is false. This is correct whenever:
190 /// - the conventions are exactly the same, because it does not
191 /// matter and the resulting IR will be somewhat prettier in
192 /// certain cases; or
193 /// - the conventions are substantively different in how they pass
194 /// arguments, because in this case using the variadic convention
195 /// will lead to C99 violations.
197 /// However, some platforms make the conventions identical except
198 /// for passing additional out-of-band information to a variadic
199 /// function: for example, x86-64 passes the number of SSE
200 /// arguments in %al. On these platforms, it is desirable to
201 /// call unprototyped functions using the variadic convention so
202 /// that unprototyped calls to varargs functions still succeed.
204 /// Relatedly, platforms which pass the fixed arguments to this:
206 /// differently than they would pass them to this:
207 /// A foo(B, C, D, ...);
208 /// may need to adjust the debugger-support code in Sema to do the
209 /// right thing when calling a function with no know signature.
210 virtual bool isNoProtoCallVariadic(const CodeGen::CallArgList &args,
211 const FunctionNoProtoType *fnType) const;
213 /// Gets the linker options necessary to link a dependent library on this
215 virtual void getDependentLibraryOption(llvm::StringRef Lib,
216 llvm::SmallString<24> &Opt) const;
218 /// Gets the linker options necessary to detect object file mismatches on
220 virtual void getDetectMismatchOption(llvm::StringRef Name,
221 llvm::StringRef Value,
222 llvm::SmallString<32> &Opt) const {}
224 /// Get LLVM calling convention for OpenCL kernel.
225 virtual unsigned getOpenCLKernelCallingConv() const;
227 /// Get target specific null pointer.
228 /// \param T is the LLVM type of the null pointer.
229 /// \param QT is the clang QualType of the null pointer.
230 /// \return ConstantPointerNull with the given type \p T.
231 /// Each target can override it to return its own desired constant value.
232 virtual llvm::Constant *getNullPointer(const CodeGen::CodeGenModule &CGM,
233 llvm::PointerType *T, QualType QT) const;
235 /// Get target favored AST address space of a global variable for languages
236 /// other than OpenCL and CUDA.
237 /// If \p D is nullptr, returns the default target favored address space
238 /// for global variable.
239 virtual LangAS getGlobalVarAddressSpace(CodeGenModule &CGM,
240 const VarDecl *D) const;
242 /// Get the AST address space for alloca.
243 virtual LangAS getASTAllocaAddressSpace() const { return LangAS::Default; }
245 /// Perform address space cast of an expression of pointer type.
246 /// \param V is the LLVM value to be casted to another address space.
247 /// \param SrcAddr is the language address space of \p V.
248 /// \param DestAddr is the targeted language address space.
249 /// \param DestTy is the destination LLVM pointer type.
250 /// \param IsNonNull is the flag indicating \p V is known to be non null.
251 virtual llvm::Value *performAddrSpaceCast(CodeGen::CodeGenFunction &CGF,
252 llvm::Value *V, LangAS SrcAddr,
253 LangAS DestAddr, llvm::Type *DestTy,
254 bool IsNonNull = false) const;
256 /// Perform address space cast of a constant expression of pointer type.
257 /// \param V is the LLVM constant 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 virtual llvm::Constant *performAddrSpaceCast(CodeGenModule &CGM,
263 LangAS SrcAddr, LangAS DestAddr,
264 llvm::Type *DestTy) const;
266 /// Get the syncscope used in LLVM IR.
267 virtual llvm::SyncScope::ID getLLVMSyncScopeID(SyncScope S,
268 llvm::LLVMContext &C) const;
270 /// Inteface class for filling custom fields of a block literal for OpenCL.
271 class TargetOpenCLBlockHelper {
273 typedef std::pair<llvm::Value *, StringRef> ValueTy;
274 TargetOpenCLBlockHelper() {}
275 virtual ~TargetOpenCLBlockHelper() {}
276 /// Get the custom field types for OpenCL blocks.
277 virtual llvm::SmallVector<llvm::Type *, 1> getCustomFieldTypes() = 0;
278 /// Get the custom field values for OpenCL blocks.
279 virtual llvm::SmallVector<ValueTy, 1>
280 getCustomFieldValues(CodeGenFunction &CGF, const CGBlockInfo &Info) = 0;
281 virtual bool areAllCustomFieldValuesConstant(const CGBlockInfo &Info) = 0;
282 /// Get the custom field values for OpenCL blocks if all values are LLVM
284 virtual llvm::SmallVector<llvm::Constant *, 1>
285 getCustomFieldValues(CodeGenModule &CGM, const CGBlockInfo &Info) = 0;
287 virtual TargetOpenCLBlockHelper *getTargetOpenCLBlockHelper() const {
291 /// Create an OpenCL kernel for an enqueued block. The kernel function is
292 /// a wrapper for the block invoke function with target-specific calling
293 /// convention and ABI as an OpenCL kernel. The wrapper function accepts
294 /// block context and block arguments in target-specific way and calls
295 /// the original block invoke function.
296 virtual llvm::Function *
297 createEnqueuedBlockKernel(CodeGenFunction &CGF,
298 llvm::Function *BlockInvokeFunc,
299 llvm::Value *BlockLiteral) const;
302 } // namespace CodeGen
305 #endif // LLVM_CLANG_LIB_CODEGEN_TARGETINFO_H