blob: d7e9eee9c5b3e14ef9f992da05cdfa2e84a6c95a [file] [log] [blame]
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00001//===---- TargetInfo.h - Encapsulate target details -------------*- C++ -*-===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00006//
7//===----------------------------------------------------------------------===//
8//
9// These classes wrap the information about a call or function
10// definition used to handle ABI compliancy.
11//
12//===----------------------------------------------------------------------===//
13
Benjamin Kramer2f5db8b2014-08-13 16:25:19 +000014#ifndef LLVM_CLANG_LIB_CODEGEN_TARGETINFO_H
15#define LLVM_CLANG_LIB_CODEGEN_TARGETINFO_H
Anton Korobeynikov55bcea12010-01-10 12:58:08 +000016
Simon Atanasyan1a116db2017-07-20 20:34:18 +000017#include "CodeGenModule.h"
Reid Kleckner9b3e3df2014-09-04 20:04:38 +000018#include "CGValue.h"
John McCallcbc038a2011-09-21 08:08:30 +000019#include "clang/AST/Type.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000020#include "clang/Basic/LLVM.h"
Yaxun Liu39195062017-08-04 18:16:31 +000021#include "clang/Basic/SyncScope.h"
Reid Klecknere43f0fe2013-05-08 13:44:39 +000022#include "llvm/ADT/SmallString.h"
Chandler Carruth5553d0d2014-01-07 11:51:46 +000023#include "llvm/ADT/StringRef.h"
Peter Collingbourne8f5cf742011-02-19 23:03:58 +000024
Anton Korobeynikov55bcea12010-01-10 12:58:08 +000025namespace llvm {
Rafael Espindola14048092014-05-09 00:26:20 +000026class Constant;
27class GlobalValue;
28class Type;
29class Value;
Anton Korobeynikov55bcea12010-01-10 12:58:08 +000030}
31
32namespace clang {
Rafael Espindola14048092014-05-09 00:26:20 +000033class Decl;
Anton Korobeynikov55bcea12010-01-10 12:58:08 +000034
Rafael Espindola14048092014-05-09 00:26:20 +000035namespace CodeGen {
John McCall12f23522016-04-04 18:33:08 +000036class ABIInfo;
Rafael Espindola14048092014-05-09 00:26:20 +000037class CallArgList;
Rafael Espindola14048092014-05-09 00:26:20 +000038class CodeGenFunction;
Yaxun Liu10712d92017-10-04 20:32:17 +000039class CGBlockInfo;
Rafael Espindola14048092014-05-09 00:26:20 +000040class CGFunctionInfo;
Rafael Espindola14048092014-05-09 00:26:20 +000041
42/// TargetCodeGenInfo - This class organizes various target-specific
43/// codegeneration issues, like target-specific attributes, builtins and so
44/// on.
45class TargetCodeGenInfo {
46 ABIInfo *Info;
47
48public:
49 // WARNING: Acquires the ownership of ABIInfo.
Hans Wennborg59dbe862015-09-29 20:56:43 +000050 TargetCodeGenInfo(ABIInfo *info = nullptr) : Info(info) {}
Rafael Espindola14048092014-05-09 00:26:20 +000051 virtual ~TargetCodeGenInfo();
52
53 /// getABIInfo() - Returns ABI info helper for the target.
54 const ABIInfo &getABIInfo() const { return *Info; }
55
Eric Christopher162c91c2015-06-05 22:03:00 +000056 /// setTargetAttributes - Provides a convenient hook to handle extra
Rafael Espindola14048092014-05-09 00:26:20 +000057 /// target-specific attributes for the given global.
Eric Christopher162c91c2015-06-05 22:03:00 +000058 virtual void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
Rafael Espindoladeb10be2018-02-07 19:04:41 +000059 CodeGen::CodeGenModule &M) const {}
Rafael Espindola14048092014-05-09 00:26:20 +000060
Eric Christopher56df4352015-06-05 22:03:01 +000061 /// emitTargetMD - Provides a convenient hook to handle extra
Rafael Espindola14048092014-05-09 00:26:20 +000062 /// target-specific metadata for the given global.
63 virtual void emitTargetMD(const Decl *D, llvm::GlobalValue *GV,
64 CodeGen::CodeGenModule &M) const {}
65
66 /// Determines the size of struct _Unwind_Exception on this platform,
67 /// in 8-bit units. The Itanium ABI defines this as:
68 /// struct _Unwind_Exception {
69 /// uint64 exception_class;
70 /// _Unwind_Exception_Cleanup_Fn exception_cleanup;
71 /// uint64 private_1;
72 /// uint64 private_2;
73 /// };
74 virtual unsigned getSizeOfUnwindException() const;
75
76 /// Controls whether __builtin_extend_pointer should sign-extend
77 /// pointers to uint64_t or zero-extend them (the default). Has
78 /// no effect for targets:
79 /// - that have 64-bit pointers, or
80 /// - that cannot address through registers larger than pointers, or
81 /// - that implicitly ignore/truncate the top bits when addressing
82 /// through such registers.
83 virtual bool extendPointerWithSExt() const { return false; }
84
85 /// Determines the DWARF register number for the stack pointer, for
86 /// exception-handling purposes. Implements __builtin_dwarf_sp_column.
87 ///
88 /// Returns -1 if the operation is unsupported by this target.
89 virtual int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const {
90 return -1;
Anton Korobeynikov55bcea12010-01-10 12:58:08 +000091 }
92
Rafael Espindola14048092014-05-09 00:26:20 +000093 /// Initializes the given DWARF EH register-size table, a char*.
94 /// Implements __builtin_init_dwarf_reg_size_table.
95 ///
96 /// Returns true if the operation is unsupported by this target.
97 virtual bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
98 llvm::Value *Address) const {
99 return true;
100 }
Anton Korobeynikov55bcea12010-01-10 12:58:08 +0000101
Rafael Espindola14048092014-05-09 00:26:20 +0000102 /// Performs the code-generation required to convert a return
103 /// address as stored by the system into the actual address of the
104 /// next instruction that will be executed.
105 ///
106 /// Used by __builtin_extract_return_addr().
107 virtual llvm::Value *decodeReturnAddress(CodeGen::CodeGenFunction &CGF,
108 llvm::Value *Address) const {
109 return Address;
110 }
Anton Korobeynikov55bcea12010-01-10 12:58:08 +0000111
Rafael Espindola14048092014-05-09 00:26:20 +0000112 /// Performs the code-generation required to convert the address
113 /// of an instruction into a return address suitable for storage
114 /// by the system in a return slot.
115 ///
116 /// Used by __builtin_frob_return_addr().
117 virtual llvm::Value *encodeReturnAddress(CodeGen::CodeGenFunction &CGF,
118 llvm::Value *Address) const {
119 return Address;
120 }
John McCallb6cc2c042010-03-02 03:50:12 +0000121
Rafael Espindola14048092014-05-09 00:26:20 +0000122 /// Corrects the low-level LLVM type for a given constraint and "usual"
123 /// type.
124 ///
125 /// \returns A pointer to a new LLVM type, possibly the same as the original
126 /// on success; 0 on failure.
127 virtual llvm::Type *adjustInlineAsmType(CodeGen::CodeGenFunction &CGF,
128 StringRef Constraint,
129 llvm::Type *Ty) const {
130 return Ty;
131 }
Robert Lytton844aeeb2014-05-02 09:33:20 +0000132
Reid Kleckner9b3e3df2014-09-04 20:04:38 +0000133 /// Adds constraints and types for result registers.
134 virtual void addReturnRegisterOutputs(
135 CodeGen::CodeGenFunction &CGF, CodeGen::LValue ReturnValue,
136 std::string &Constraints, std::vector<llvm::Type *> &ResultRegTypes,
137 std::vector<llvm::Type *> &ResultTruncRegTypes,
138 std::vector<CodeGen::LValue> &ResultRegDests, std::string &AsmString,
139 unsigned NumOutputs) const {}
140
Rafael Espindola14048092014-05-09 00:26:20 +0000141 /// doesReturnSlotInterfereWithArgs - Return true if the target uses an
142 /// argument slot for an 'sret' type.
143 virtual bool doesReturnSlotInterfereWithArgs() const { return true; }
John McCall5add20c2010-07-20 22:17:55 +0000144
Rafael Espindola14048092014-05-09 00:26:20 +0000145 /// Retrieve the address of a function to call immediately before
146 /// calling objc_retainAutoreleasedReturnValue. The
147 /// implementation of objc_autoreleaseReturnValue sniffs the
148 /// instruction stream following its return address to decide
149 /// whether it's a call to objc_retainAutoreleasedReturnValue.
150 /// This can be prohibitively expensive, depending on the
151 /// relocation model, and so on some targets it instead sniffs for
152 /// a particular instruction sequence. This functions returns
153 /// that instruction sequence in inline assembly, which will be
154 /// empty if none is required.
155 virtual StringRef getARCRetainAutoreleasedReturnValueMarker() const {
156 return "";
157 }
John McCalld4f4b7f2010-03-03 04:15:11 +0000158
Akira Hatanaka65bb3f92019-03-21 19:59:49 +0000159 /// Determine whether a call to objc_retainAutoreleasedReturnValue should be
160 /// marked as 'notail'.
161 virtual bool shouldSuppressTailCallsOfRetainAutoreleasedReturnValue() const {
162 return false;
163 }
164
Rafael Espindola14048092014-05-09 00:26:20 +0000165 /// Return a constant used by UBSan as a signature to identify functions
166 /// possessing type information, or 0 if the platform is unsupported.
167 virtual llvm::Constant *
168 getUBSanFunctionSignature(CodeGen::CodeGenModule &CGM) const {
Craig Topper8a13c412014-05-21 05:09:00 +0000169 return nullptr;
Rafael Espindola14048092014-05-09 00:26:20 +0000170 }
John McCallbeec5a02010-03-06 00:35:14 +0000171
Rafael Espindola14048092014-05-09 00:26:20 +0000172 /// Determine whether a call to an unprototyped functions under
173 /// the given calling convention should use the variadic
174 /// convention or the non-variadic convention.
175 ///
176 /// There's a good reason to make a platform's variadic calling
177 /// convention be different from its non-variadic calling
178 /// convention: the non-variadic arguments can be passed in
179 /// registers (better for performance), and the variadic arguments
180 /// can be passed on the stack (also better for performance). If
181 /// this is done, however, unprototyped functions *must* use the
182 /// non-variadic convention, because C99 states that a call
183 /// through an unprototyped function type must succeed if the
184 /// function was defined with a non-variadic prototype with
185 /// compatible parameters. Therefore, splitting the conventions
186 /// makes it impossible to call a variadic function through an
187 /// unprototyped type. Since function prototypes came out in the
188 /// late 1970s, this is probably an acceptable trade-off.
189 /// Nonetheless, not all platforms are willing to make it, and in
190 /// particularly x86-64 bends over backwards to make the
191 /// conventions compatible.
192 ///
193 /// The default is false. This is correct whenever:
194 /// - the conventions are exactly the same, because it does not
195 /// matter and the resulting IR will be somewhat prettier in
196 /// certain cases; or
197 /// - the conventions are substantively different in how they pass
198 /// arguments, because in this case using the variadic convention
199 /// will lead to C99 violations.
200 ///
201 /// However, some platforms make the conventions identical except
202 /// for passing additional out-of-band information to a variadic
203 /// function: for example, x86-64 passes the number of SSE
204 /// arguments in %al. On these platforms, it is desirable to
205 /// call unprototyped functions using the variadic convention so
206 /// that unprototyped calls to varargs functions still succeed.
207 ///
208 /// Relatedly, platforms which pass the fixed arguments to this:
209 /// A foo(B, C, D);
210 /// differently than they would pass them to this:
211 /// A foo(B, C, D, ...);
212 /// may need to adjust the debugger-support code in Sema to do the
213 /// right thing when calling a function with no know signature.
214 virtual bool isNoProtoCallVariadic(const CodeGen::CallArgList &args,
215 const FunctionNoProtoType *fnType) const;
John McCallbeec5a02010-03-06 00:35:14 +0000216
Rafael Espindola14048092014-05-09 00:26:20 +0000217 /// Gets the linker options necessary to link a dependent library on this
218 /// platform.
219 virtual void getDependentLibraryOption(llvm::StringRef Lib,
220 llvm::SmallString<24> &Opt) const;
John McCalld4f4b7f2010-03-03 04:15:11 +0000221
Rafael Espindola14048092014-05-09 00:26:20 +0000222 /// Gets the linker options necessary to detect object file mismatches on
223 /// this platform.
224 virtual void getDetectMismatchOption(llvm::StringRef Name,
225 llvm::StringRef Value,
226 llvm::SmallString<32> &Opt) const {}
Nikolay Haustov8c6538b2016-06-30 09:06:33 +0000227
228 /// Get LLVM calling convention for OpenCL kernel.
229 virtual unsigned getOpenCLKernelCallingConv() const;
Yaxun Liu402804b2016-12-15 08:09:08 +0000230
231 /// Get target specific null pointer.
232 /// \param T is the LLVM type of the null pointer.
233 /// \param QT is the clang QualType of the null pointer.
234 /// \return ConstantPointerNull with the given type \p T.
235 /// Each target can override it to return its own desired constant value.
236 virtual llvm::Constant *getNullPointer(const CodeGen::CodeGenModule &CGM,
237 llvm::PointerType *T, QualType QT) const;
238
Yaxun Liucbf647c2017-07-08 13:24:52 +0000239 /// Get target favored AST address space of a global variable for languages
240 /// other than OpenCL and CUDA.
241 /// If \p D is nullptr, returns the default target favored address space
242 /// for global variable.
Alexander Richardson6d989432017-10-15 18:48:14 +0000243 virtual LangAS getGlobalVarAddressSpace(CodeGenModule &CGM,
244 const VarDecl *D) const;
Yaxun Liucbf647c2017-07-08 13:24:52 +0000245
Yaxun Liu6d96f1632017-05-18 18:51:09 +0000246 /// Get the AST address space for alloca.
Alexander Richardson6d989432017-10-15 18:48:14 +0000247 virtual LangAS getASTAllocaAddressSpace() const { return LangAS::Default; }
Yaxun Liu6d96f1632017-05-18 18:51:09 +0000248
Yaxun Liu402804b2016-12-15 08:09:08 +0000249 /// Perform address space cast of an expression of pointer type.
250 /// \param V is the LLVM value to be casted to another address space.
Yaxun Liu6d96f1632017-05-18 18:51:09 +0000251 /// \param SrcAddr is the language address space of \p V.
252 /// \param DestAddr is the targeted language address space.
253 /// \param DestTy is the destination LLVM pointer type.
254 /// \param IsNonNull is the flag indicating \p V is known to be non null.
Yaxun Liu402804b2016-12-15 08:09:08 +0000255 virtual llvm::Value *performAddrSpaceCast(CodeGen::CodeGenFunction &CGF,
Alexander Richardson6d989432017-10-15 18:48:14 +0000256 llvm::Value *V, LangAS SrcAddr,
257 LangAS DestAddr, llvm::Type *DestTy,
Yaxun Liu6d96f1632017-05-18 18:51:09 +0000258 bool IsNonNull = false) const;
Yaxun Liucbf647c2017-07-08 13:24:52 +0000259
260 /// Perform address space cast of a constant expression of pointer type.
261 /// \param V is the LLVM constant to be casted to another address space.
262 /// \param SrcAddr is the language address space of \p V.
263 /// \param DestAddr is the targeted language address space.
264 /// \param DestTy is the destination LLVM pointer type.
Alexander Richardson6d989432017-10-15 18:48:14 +0000265 virtual llvm::Constant *performAddrSpaceCast(CodeGenModule &CGM,
266 llvm::Constant *V,
267 LangAS SrcAddr, LangAS DestAddr,
268 llvm::Type *DestTy) const;
Yaxun Liu39195062017-08-04 18:16:31 +0000269
270 /// Get the syncscope used in LLVM IR.
Konstantin Zhuravlyovec28a1d2019-03-25 20:54:00 +0000271 virtual llvm::SyncScope::ID getLLVMSyncScopeID(const LangOptions &LangOpts,
272 SyncScope Scope,
273 llvm::AtomicOrdering Ordering,
274 llvm::LLVMContext &Ctx) const;
Yaxun Liu10712d92017-10-04 20:32:17 +0000275
Alexander Kornienko2a8c18d2018-04-06 15:14:32 +0000276 /// Interface class for filling custom fields of a block literal for OpenCL.
Yaxun Liu10712d92017-10-04 20:32:17 +0000277 class TargetOpenCLBlockHelper {
278 public:
279 typedef std::pair<llvm::Value *, StringRef> ValueTy;
280 TargetOpenCLBlockHelper() {}
281 virtual ~TargetOpenCLBlockHelper() {}
282 /// Get the custom field types for OpenCL blocks.
283 virtual llvm::SmallVector<llvm::Type *, 1> getCustomFieldTypes() = 0;
284 /// Get the custom field values for OpenCL blocks.
285 virtual llvm::SmallVector<ValueTy, 1>
286 getCustomFieldValues(CodeGenFunction &CGF, const CGBlockInfo &Info) = 0;
287 virtual bool areAllCustomFieldValuesConstant(const CGBlockInfo &Info) = 0;
288 /// Get the custom field values for OpenCL blocks if all values are LLVM
289 /// constants.
290 virtual llvm::SmallVector<llvm::Constant *, 1>
291 getCustomFieldValues(CodeGenModule &CGM, const CGBlockInfo &Info) = 0;
292 };
293 virtual TargetOpenCLBlockHelper *getTargetOpenCLBlockHelper() const {
294 return nullptr;
295 }
Yaxun Liuc2a87a02017-10-14 12:23:50 +0000296
297 /// Create an OpenCL kernel for an enqueued block. The kernel function is
298 /// a wrapper for the block invoke function with target-specific calling
299 /// convention and ABI as an OpenCL kernel. The wrapper function accepts
300 /// block context and block arguments in target-specific way and calls
301 /// the original block invoke function.
302 virtual llvm::Function *
303 createEnqueuedBlockKernel(CodeGenFunction &CGF,
304 llvm::Function *BlockInvokeFunc,
305 llvm::Value *BlockLiteral) const;
Yaxun Liub0eee292018-03-29 14:50:00 +0000306
307 /// \return true if the target supports alias from the unmangled name to the
308 /// mangled name of functions declared within an extern "C" region and marked
309 /// as 'used', and having internal linkage.
310 virtual bool shouldEmitStaticExternCAliases() const { return true; }
Yaxun Liu4306f202018-04-20 17:01:03 +0000311
Yaxun Liu6c10a662018-06-12 00:16:33 +0000312 virtual void setCUDAKernelCallingConvention(const FunctionType *&FT) const {}
Rafael Espindola14048092014-05-09 00:26:20 +0000313};
John McCall12f23522016-04-04 18:33:08 +0000314
315} // namespace CodeGen
Hans Wennborg59dbe862015-09-29 20:56:43 +0000316} // namespace clang
Anton Korobeynikov55bcea12010-01-10 12:58:08 +0000317
Hans Wennborg59dbe862015-09-29 20:56:43 +0000318#endif // LLVM_CLANG_LIB_CODEGEN_TARGETINFO_H