blob: d745e420c4a563e421a5657b3e14b32e92e7bb80 [file] [log] [blame]
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00001//===---- TargetInfo.h - Encapsulate target details -------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// These classes wrap the information about a call or function
11// definition used to handle ABI compliancy.
12//
13//===----------------------------------------------------------------------===//
14
Benjamin Kramer2f5db8b2014-08-13 16:25:19 +000015#ifndef LLVM_CLANG_LIB_CODEGEN_TARGETINFO_H
16#define LLVM_CLANG_LIB_CODEGEN_TARGETINFO_H
Anton Korobeynikov55bcea12010-01-10 12:58:08 +000017
Simon Atanasyan1a116db2017-07-20 20:34:18 +000018#include "CodeGenModule.h"
Reid Kleckner9b3e3df2014-09-04 20:04:38 +000019#include "CGValue.h"
John McCallcbc038a2011-09-21 08:08:30 +000020#include "clang/AST/Type.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000021#include "clang/Basic/LLVM.h"
Yaxun Liu39195062017-08-04 18:16:31 +000022#include "clang/Basic/SyncScope.h"
Reid Klecknere43f0fe2013-05-08 13:44:39 +000023#include "llvm/ADT/SmallString.h"
Chandler Carruth5553d0d2014-01-07 11:51:46 +000024#include "llvm/ADT/StringRef.h"
Peter Collingbourne8f5cf742011-02-19 23:03:58 +000025
Anton Korobeynikov55bcea12010-01-10 12:58:08 +000026namespace llvm {
Rafael Espindola14048092014-05-09 00:26:20 +000027class Constant;
28class GlobalValue;
29class Type;
30class Value;
Anton Korobeynikov55bcea12010-01-10 12:58:08 +000031}
32
33namespace clang {
Rafael Espindola14048092014-05-09 00:26:20 +000034class Decl;
Anton Korobeynikov55bcea12010-01-10 12:58:08 +000035
Rafael Espindola14048092014-05-09 00:26:20 +000036namespace CodeGen {
John McCall12f23522016-04-04 18:33:08 +000037class ABIInfo;
Rafael Espindola14048092014-05-09 00:26:20 +000038class CallArgList;
Rafael Espindola14048092014-05-09 00:26:20 +000039class CodeGenFunction;
Yaxun Liu10712d92017-10-04 20:32:17 +000040class CGBlockInfo;
Rafael Espindola14048092014-05-09 00:26:20 +000041class CGFunctionInfo;
Rafael Espindola14048092014-05-09 00:26:20 +000042
43/// TargetCodeGenInfo - This class organizes various target-specific
44/// codegeneration issues, like target-specific attributes, builtins and so
45/// on.
46class TargetCodeGenInfo {
47 ABIInfo *Info;
48
49public:
50 // WARNING: Acquires the ownership of ABIInfo.
Hans Wennborg59dbe862015-09-29 20:56:43 +000051 TargetCodeGenInfo(ABIInfo *info = nullptr) : Info(info) {}
Rafael Espindola14048092014-05-09 00:26:20 +000052 virtual ~TargetCodeGenInfo();
53
54 /// getABIInfo() - Returns ABI info helper for the target.
55 const ABIInfo &getABIInfo() const { return *Info; }
56
Eric Christopher162c91c2015-06-05 22:03:00 +000057 /// setTargetAttributes - Provides a convenient hook to handle extra
Rafael Espindola14048092014-05-09 00:26:20 +000058 /// target-specific attributes for the given global.
Eric Christopher162c91c2015-06-05 22:03:00 +000059 virtual void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
Simon Atanasyan1a116db2017-07-20 20:34:18 +000060 CodeGen::CodeGenModule &M,
61 ForDefinition_t IsForDefinition) const {}
Rafael Espindola14048092014-05-09 00:26:20 +000062
Eric Christopher56df4352015-06-05 22:03:01 +000063 /// emitTargetMD - Provides a convenient hook to handle extra
Rafael Espindola14048092014-05-09 00:26:20 +000064 /// target-specific metadata for the given global.
65 virtual void emitTargetMD(const Decl *D, llvm::GlobalValue *GV,
66 CodeGen::CodeGenModule &M) const {}
67
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;
73 /// uint64 private_1;
74 /// uint64 private_2;
75 /// };
76 virtual unsigned getSizeOfUnwindException() const;
77
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; }
86
87 /// Determines the DWARF register number for the stack pointer, for
88 /// exception-handling purposes. Implements __builtin_dwarf_sp_column.
89 ///
90 /// Returns -1 if the operation is unsupported by this target.
91 virtual int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const {
92 return -1;
Anton Korobeynikov55bcea12010-01-10 12:58:08 +000093 }
94
Rafael Espindola14048092014-05-09 00:26:20 +000095 /// Initializes the given DWARF EH register-size table, a char*.
96 /// Implements __builtin_init_dwarf_reg_size_table.
97 ///
98 /// Returns true if the operation is unsupported by this target.
99 virtual bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
100 llvm::Value *Address) const {
101 return true;
102 }
Anton Korobeynikov55bcea12010-01-10 12:58:08 +0000103
Rafael Espindola14048092014-05-09 00:26:20 +0000104 /// 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.
107 ///
108 /// Used by __builtin_extract_return_addr().
109 virtual llvm::Value *decodeReturnAddress(CodeGen::CodeGenFunction &CGF,
110 llvm::Value *Address) const {
111 return Address;
112 }
Anton Korobeynikov55bcea12010-01-10 12:58:08 +0000113
Rafael Espindola14048092014-05-09 00:26:20 +0000114 /// 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.
117 ///
118 /// Used by __builtin_frob_return_addr().
119 virtual llvm::Value *encodeReturnAddress(CodeGen::CodeGenFunction &CGF,
120 llvm::Value *Address) const {
121 return Address;
122 }
John McCallb6cc2c042010-03-02 03:50:12 +0000123
Rafael Espindola14048092014-05-09 00:26:20 +0000124 /// Corrects the low-level LLVM type for a given constraint and "usual"
125 /// type.
126 ///
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 {
132 return Ty;
133 }
Robert Lytton844aeeb2014-05-02 09:33:20 +0000134
Reid Kleckner9b3e3df2014-09-04 20:04:38 +0000135 /// 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 {}
142
Rafael Espindola14048092014-05-09 00:26:20 +0000143 /// doesReturnSlotInterfereWithArgs - Return true if the target uses an
144 /// argument slot for an 'sret' type.
145 virtual bool doesReturnSlotInterfereWithArgs() const { return true; }
John McCall5add20c2010-07-20 22:17:55 +0000146
Rafael Espindola14048092014-05-09 00:26:20 +0000147 /// 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 {
158 return "";
159 }
John McCalld4f4b7f2010-03-03 04:15:11 +0000160
Rafael Espindola14048092014-05-09 00:26:20 +0000161 /// 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 {
Craig Topper8a13c412014-05-21 05:09:00 +0000165 return nullptr;
Rafael Espindola14048092014-05-09 00:26:20 +0000166 }
John McCallbeec5a02010-03-06 00:35:14 +0000167
Rafael Espindola14048092014-05-09 00:26:20 +0000168 /// 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.
171 ///
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.
188 ///
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.
196 ///
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.
203 ///
204 /// Relatedly, platforms which pass the fixed arguments to this:
205 /// A foo(B, C, D);
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;
John McCallbeec5a02010-03-06 00:35:14 +0000212
Rafael Espindola14048092014-05-09 00:26:20 +0000213 /// Gets the linker options necessary to link a dependent library on this
214 /// platform.
215 virtual void getDependentLibraryOption(llvm::StringRef Lib,
216 llvm::SmallString<24> &Opt) const;
John McCalld4f4b7f2010-03-03 04:15:11 +0000217
Rafael Espindola14048092014-05-09 00:26:20 +0000218 /// Gets the linker options necessary to detect object file mismatches on
219 /// this platform.
220 virtual void getDetectMismatchOption(llvm::StringRef Name,
221 llvm::StringRef Value,
222 llvm::SmallString<32> &Opt) const {}
Nikolay Haustov8c6538b2016-06-30 09:06:33 +0000223
224 /// Get LLVM calling convention for OpenCL kernel.
225 virtual unsigned getOpenCLKernelCallingConv() const;
Yaxun Liu402804b2016-12-15 08:09:08 +0000226
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;
234
Yaxun Liucbf647c2017-07-08 13:24:52 +0000235 /// 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.
Alexander Richardson6d989432017-10-15 18:48:14 +0000239 virtual LangAS getGlobalVarAddressSpace(CodeGenModule &CGM,
240 const VarDecl *D) const;
Yaxun Liucbf647c2017-07-08 13:24:52 +0000241
Yaxun Liu6d96f1632017-05-18 18:51:09 +0000242 /// Get the AST address space for alloca.
Alexander Richardson6d989432017-10-15 18:48:14 +0000243 virtual LangAS getASTAllocaAddressSpace() const { return LangAS::Default; }
Yaxun Liu6d96f1632017-05-18 18:51:09 +0000244
Yaxun Liu402804b2016-12-15 08:09:08 +0000245 /// Perform address space cast of an expression of pointer type.
246 /// \param V is the LLVM value to be casted to another address space.
Yaxun Liu6d96f1632017-05-18 18:51:09 +0000247 /// \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.
Yaxun Liu402804b2016-12-15 08:09:08 +0000251 virtual llvm::Value *performAddrSpaceCast(CodeGen::CodeGenFunction &CGF,
Alexander Richardson6d989432017-10-15 18:48:14 +0000252 llvm::Value *V, LangAS SrcAddr,
253 LangAS DestAddr, llvm::Type *DestTy,
Yaxun Liu6d96f1632017-05-18 18:51:09 +0000254 bool IsNonNull = false) const;
Yaxun Liucbf647c2017-07-08 13:24:52 +0000255
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.
Alexander Richardson6d989432017-10-15 18:48:14 +0000261 virtual llvm::Constant *performAddrSpaceCast(CodeGenModule &CGM,
262 llvm::Constant *V,
263 LangAS SrcAddr, LangAS DestAddr,
264 llvm::Type *DestTy) const;
Yaxun Liu39195062017-08-04 18:16:31 +0000265
266 /// Get the syncscope used in LLVM IR.
267 virtual llvm::SyncScope::ID getLLVMSyncScopeID(SyncScope S,
268 llvm::LLVMContext &C) const;
Yaxun Liu10712d92017-10-04 20:32:17 +0000269
270 /// Inteface class for filling custom fields of a block literal for OpenCL.
271 class TargetOpenCLBlockHelper {
272 public:
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
283 /// constants.
284 virtual llvm::SmallVector<llvm::Constant *, 1>
285 getCustomFieldValues(CodeGenModule &CGM, const CGBlockInfo &Info) = 0;
286 };
287 virtual TargetOpenCLBlockHelper *getTargetOpenCLBlockHelper() const {
288 return nullptr;
289 }
Yaxun Liuc2a87a02017-10-14 12:23:50 +0000290
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;
Rafael Espindola14048092014-05-09 00:26:20 +0000300};
John McCall12f23522016-04-04 18:33:08 +0000301
302} // namespace CodeGen
Hans Wennborg59dbe862015-09-29 20:56:43 +0000303} // namespace clang
Anton Korobeynikov55bcea12010-01-10 12:58:08 +0000304
Hans Wennborg59dbe862015-09-29 20:56:43 +0000305#endif // LLVM_CLANG_LIB_CODEGEN_TARGETINFO_H