Debian Codegraph @

llvm-project/clang/lib/CodeGen/TargetInfo.h

raw
1 //===---- TargetInfo.h - Encapsulate target details -------------*- C++ -*-===//
2 //
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
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // These classes wrap the information about a call or function
10 // definition used to handle ABI compliancy.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #ifndef LLVM_CLANG_LIB_CODEGEN_TARGETINFO_H
15 #define LLVM_CLANG_LIB_CODEGEN_TARGETINFO_H
16
17 #include "CGBuilder.h"
18 #include "CodeGenModule.h"
19 #include "CGValue.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"
25
26 namespace llvm {
27 class Constant;
28 class GlobalValue;
29 class Type;
30 class Value;
31 }
32
33 namespace clang {
34 class Decl;
35
36 namespace CodeGen {
37 class ABIInfo;
38 class CallArgList;
39 class CodeGenFunction;
40 class CGBlockInfo;
41 class SwiftABIInfo;
42
43 /// TargetCodeGenInfo - This class organizes various target-specific
44 /// codegeneration issues, like target-specific attributes, builtins and so
45 /// on.
46 class TargetCodeGenInfo {
47 std::unique_ptr<ABIInfo> Info;
48
49 protected:
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;
54
55 public:
56 TargetCodeGenInfo(std::unique_ptr<ABIInfo> Info);
57 virtual ~TargetCodeGenInfo();
58
59 /// getABIInfo() - Returns ABI info helper for the target.
60 const ABIInfo &getABIInfo() const { return *Info; }
61
62 /// Returns Swift ABI info helper for the target.
63 const SwiftABIInfo &getSwiftABIInfo() const {
64 assert(SwiftInfo && "Swift ABI info has not been initialized");
65 return *SwiftInfo;
66 }
67
68 /// setTargetAttributes - Provides a convenient hook to handle extra
69 /// target-specific attributes for the given global.
70 virtual void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
71 CodeGen::CodeGenModule &M) const {}
72
73 /// emitTargetMetadata - Provides a convenient hook to handle extra
74 /// target-specific metadata for the given globals.
75 virtual void emitTargetMetadata(
76 CodeGen::CodeGenModule &CGM,
77 const llvm::MapVector<GlobalDecl, StringRef> &MangledDeclNames) const {}
78
79 /// Any further codegen related checks that need to be done on a function call
80 /// in a target specific manner.
81 virtual void checkFunctionCallABI(CodeGenModule &CGM, SourceLocation CallLoc,
82 const FunctionDecl *Caller,
83 const FunctionDecl *Callee,
84 const CallArgList &Args) const {}
85
86 /// Determines the size of struct _Unwind_Exception on this platform,
87 /// in 8-bit units. The Itanium ABI defines this as:
88 /// struct _Unwind_Exception {
89 /// uint64 exception_class;
90 /// _Unwind_Exception_Cleanup_Fn exception_cleanup;
91 /// uint64 private_1;
92 /// uint64 private_2;
93 /// };
94 virtual unsigned getSizeOfUnwindException() const;
95
96 /// Controls whether __builtin_extend_pointer should sign-extend
97 /// pointers to uint64_t or zero-extend them (the default). Has
98 /// no effect for targets:
99 /// - that have 64-bit pointers, or
100 /// - that cannot address through registers larger than pointers, or
101 /// - that implicitly ignore/truncate the top bits when addressing
102 /// through such registers.
103 virtual bool extendPointerWithSExt() const { return false; }
104
105 /// Determines the DWARF register number for the stack pointer, for
106 /// exception-handling purposes. Implements __builtin_dwarf_sp_column.
107 ///
108 /// Returns -1 if the operation is unsupported by this target.
109 virtual int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const {
110 return -1;
111 }
112
113 /// Initializes the given DWARF EH register-size table, a char*.
114 /// Implements __builtin_init_dwarf_reg_size_table.
115 ///
116 /// Returns true if the operation is unsupported by this target.
117 virtual bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
118 llvm::Value *Address) const {
119 return true;
120 }
121
122 /// Performs the code-generation required to convert a return
123 /// address as stored by the system into the actual address of the
124 /// next instruction that will be executed.
125 ///
126 /// Used by __builtin_extract_return_addr().
127 virtual llvm::Value *decodeReturnAddress(CodeGen::CodeGenFunction &CGF,
128 llvm::Value *Address) const {
129 return Address;
130 }
131
132 /// Performs the code-generation required to convert the address
133 /// of an instruction into a return address suitable for storage
134 /// by the system in a return slot.
135 ///
136 /// Used by __builtin_frob_return_addr().
137 virtual llvm::Value *encodeReturnAddress(CodeGen::CodeGenFunction &CGF,
138 llvm::Value *Address) const {
139 return Address;
140 }
141
142 /// Performs a target specific test of a floating point value for things
143 /// like IsNaN, Infinity, ... Nullptr is returned if no implementation
144 /// exists.
145 virtual llvm::Value *
146 testFPKind(llvm::Value *V, unsigned BuiltinID, CGBuilderTy &Builder,
147 CodeGenModule &CGM) const {
148 assert(V->getType()->isFloatingPointTy() && "V should have an FP type.");
149 return nullptr;
150 }
151
152 /// Corrects the low-level LLVM type for a given constraint and "usual"
153 /// type.
154 ///
155 /// \returns A pointer to a new LLVM type, possibly the same as the original
156 /// on success; 0 on failure.
157 virtual llvm::Type *adjustInlineAsmType(CodeGen::CodeGenFunction &CGF,
158 StringRef Constraint,
159 llvm::Type *Ty) const {
160 return Ty;
161 }
162
163 /// Target hook to decide whether an inline asm operand can be passed
164 /// by value.
165 virtual bool isScalarizableAsmOperand(CodeGen::CodeGenFunction &CGF,
166 llvm::Type *Ty) const {
167 return false;
168 }
169
170 /// Adds constraints and types for result registers.
171 virtual void addReturnRegisterOutputs(
172 CodeGen::CodeGenFunction &CGF, CodeGen::LValue ReturnValue,
173 std::string &Constraints, std::vector<llvm::Type *> &ResultRegTypes,
174 std::vector<llvm::Type *> &ResultTruncRegTypes,
175 std::vector<CodeGen::LValue> &ResultRegDests, std::string &AsmString,
176 unsigned NumOutputs) const {}
177
178 /// doesReturnSlotInterfereWithArgs - Return true if the target uses an
179 /// argument slot for an 'sret' type.
180 virtual bool doesReturnSlotInterfereWithArgs() const { return true; }
181
182 /// Retrieve the address of a function to call immediately before
183 /// calling objc_retainAutoreleasedReturnValue. The
184 /// implementation of objc_autoreleaseReturnValue sniffs the
185 /// instruction stream following its return address to decide
186 /// whether it's a call to objc_retainAutoreleasedReturnValue.
187 /// This can be prohibitively expensive, depending on the
188 /// relocation model, and so on some targets it instead sniffs for
189 /// a particular instruction sequence. This functions returns
190 /// that instruction sequence in inline assembly, which will be
191 /// empty if none is required.
192 virtual StringRef getARCRetainAutoreleasedReturnValueMarker() const {
193 return "";
194 }
195
196 /// Determine whether a call to objc_retainAutoreleasedReturnValue or
197 /// objc_unsafeClaimAutoreleasedReturnValue should be marked as 'notail'.
198 virtual bool markARCOptimizedReturnCallsAsNoTail() const { return false; }
199
200 /// Return a constant used by UBSan as a signature to identify functions
201 /// possessing type information, or 0 if the platform is unsupported.
202 virtual llvm::Constant *
203 getUBSanFunctionSignature(CodeGen::CodeGenModule &CGM) const {
204 return nullptr;
205 }
206
207 /// Determine whether a call to an unprototyped functions under
208 /// the given calling convention should use the variadic
209 /// convention or the non-variadic convention.
210 ///
211 /// There's a good reason to make a platform's variadic calling
212 /// convention be different from its non-variadic calling
213 /// convention: the non-variadic arguments can be passed in
214 /// registers (better for performance), and the variadic arguments
215 /// can be passed on the stack (also better for performance). If
216 /// this is done, however, unprototyped functions *must* use the
217 /// non-variadic convention, because C99 states that a call
218 /// through an unprototyped function type must succeed if the
219 /// function was defined with a non-variadic prototype with
220 /// compatible parameters. Therefore, splitting the conventions
221 /// makes it impossible to call a variadic function through an
222 /// unprototyped type. Since function prototypes came out in the
223 /// late 1970s, this is probably an acceptable trade-off.
224 /// Nonetheless, not all platforms are willing to make it, and in
225 /// particularly x86-64 bends over backwards to make the
226 /// conventions compatible.
227 ///
228 /// The default is false. This is correct whenever:
229 /// - the conventions are exactly the same, because it does not
230 /// matter and the resulting IR will be somewhat prettier in
231 /// certain cases; or
232 /// - the conventions are substantively different in how they pass
233 /// arguments, because in this case using the variadic convention
234 /// will lead to C99 violations.
235 ///
236 /// However, some platforms make the conventions identical except
237 /// for passing additional out-of-band information to a variadic
238 /// function: for example, x86-64 passes the number of SSE
239 /// arguments in %al. On these platforms, it is desirable to
240 /// call unprototyped functions using the variadic convention so
241 /// that unprototyped calls to varargs functions still succeed.
242 ///
243 /// Relatedly, platforms which pass the fixed arguments to this:
244 /// A foo(B, C, D);
245 /// differently than they would pass them to this:
246 /// A foo(B, C, D, ...);
247 /// may need to adjust the debugger-support code in Sema to do the
248 /// right thing when calling a function with no know signature.
249 virtual bool isNoProtoCallVariadic(const CodeGen::CallArgList &args,
250 const FunctionNoProtoType *fnType) const;
251
252 /// Gets the linker options necessary to link a dependent library on this
253 /// platform.
254 virtual void getDependentLibraryOption(llvm::StringRef Lib,
255 llvm::SmallString<24> &Opt) const;
256
257 /// Gets the linker options necessary to detect object file mismatches on
258 /// this platform.
259 virtual void getDetectMismatchOption(llvm::StringRef Name,
260 llvm::StringRef Value,
261 llvm::SmallString<32> &Opt) const {}
262
263 /// Get LLVM calling convention for OpenCL kernel.
264 virtual unsigned getOpenCLKernelCallingConv() const;
265
266 /// Get target specific null pointer.
267 /// \param T is the LLVM type of the null pointer.
268 /// \param QT is the clang QualType of the null pointer.
269 /// \return ConstantPointerNull with the given type \p T.
270 /// Each target can override it to return its own desired constant value.
271 virtual llvm::Constant *getNullPointer(const CodeGen::CodeGenModule &CGM,
272 llvm::PointerType *T, QualType QT) const;
273
274 /// Get target favored AST address space of a global variable for languages
275 /// other than OpenCL and CUDA.
276 /// If \p D is nullptr, returns the default target favored address space
277 /// for global variable.
278 virtual LangAS getGlobalVarAddressSpace(CodeGenModule &CGM,
279 const VarDecl *D) const;
280
281 /// Get the AST address space for alloca.
282 virtual LangAS getASTAllocaAddressSpace() const { return LangAS::Default; }
283
284 /// Perform address space cast of an expression of pointer type.
285 /// \param V is the LLVM value to be casted to another address space.
286 /// \param SrcAddr is the language address space of \p V.
287 /// \param DestAddr is the targeted language address space.
288 /// \param DestTy is the destination LLVM pointer type.
289 /// \param IsNonNull is the flag indicating \p V is known to be non null.
290 virtual llvm::Value *performAddrSpaceCast(CodeGen::CodeGenFunction &CGF,
291 llvm::Value *V, LangAS SrcAddr,
292 LangAS DestAddr, llvm::Type *DestTy,
293 bool IsNonNull = false) const;
294
295 /// Perform address space cast of a constant expression of pointer type.
296 /// \param V is the LLVM constant to be casted to another address space.
297 /// \param SrcAddr is the language address space of \p V.
298 /// \param DestAddr is the targeted language address space.
299 /// \param DestTy is the destination LLVM pointer type.
300 virtual llvm::Constant *performAddrSpaceCast(CodeGenModule &CGM,
301 llvm::Constant *V,
302 LangAS SrcAddr, LangAS DestAddr,
303 llvm::Type *DestTy) const;
304
305 /// Get address space of pointer parameter for __cxa_atexit.
306 virtual LangAS getAddrSpaceOfCxaAtexitPtrParam() const {
307 return LangAS::Default;
308 }
309
310 /// Get the syncscope used in LLVM IR.
311 virtual llvm::SyncScope::ID getLLVMSyncScopeID(const LangOptions &LangOpts,
312 SyncScope Scope,
313 llvm::AtomicOrdering Ordering,
314 llvm::LLVMContext &Ctx) const;
315
316 /// Interface class for filling custom fields of a block literal for OpenCL.
317 class TargetOpenCLBlockHelper {
318 public:
319 typedef std::pair<llvm::Value *, StringRef> ValueTy;
320 TargetOpenCLBlockHelper() {}
321 virtual ~TargetOpenCLBlockHelper() {}
322 /// Get the custom field types for OpenCL blocks.
323 virtual llvm::SmallVector<llvm::Type *, 1> getCustomFieldTypes() = 0;
324 /// Get the custom field values for OpenCL blocks.
325 virtual llvm::SmallVector<ValueTy, 1>
326 getCustomFieldValues(CodeGenFunction &CGF, const CGBlockInfo &Info) = 0;
327 virtual bool areAllCustomFieldValuesConstant(const CGBlockInfo &Info) = 0;
328 /// Get the custom field values for OpenCL blocks if all values are LLVM
329 /// constants.
330 virtual llvm::SmallVector<llvm::Constant *, 1>
331 getCustomFieldValues(CodeGenModule &CGM, const CGBlockInfo &Info) = 0;
332 };
333 virtual TargetOpenCLBlockHelper *getTargetOpenCLBlockHelper() const {
334 return nullptr;
335 }
336
337 /// Create an OpenCL kernel for an enqueued block. The kernel function is
338 /// a wrapper for the block invoke function with target-specific calling
339 /// convention and ABI as an OpenCL kernel. The wrapper function accepts
340 /// block context and block arguments in target-specific way and calls
341 /// the original block invoke function.
342 virtual llvm::Function *
343 createEnqueuedBlockKernel(CodeGenFunction &CGF,
344 llvm::Function *BlockInvokeFunc,
345 llvm::Type *BlockTy) const;
346
347 /// \return true if the target supports alias from the unmangled name to the
348 /// mangled name of functions declared within an extern "C" region and marked
349 /// as 'used', and having internal linkage.
350 virtual bool shouldEmitStaticExternCAliases() const { return true; }
351
352 virtual void setCUDAKernelCallingConvention(const FunctionType *&FT) const {}
353
354 /// Return the device-side type for the CUDA device builtin surface type.
355 virtual llvm::Type *getCUDADeviceBuiltinSurfaceDeviceType() const {
356 // By default, no change from the original one.
357 return nullptr;
358 }
359 /// Return the device-side type for the CUDA device builtin texture type.
360 virtual llvm::Type *getCUDADeviceBuiltinTextureDeviceType() const {
361 // By default, no change from the original one.
362 return nullptr;
363 }
364
365 /// Emit the device-side copy of the builtin surface type.
366 virtual bool emitCUDADeviceBuiltinSurfaceDeviceCopy(CodeGenFunction &CGF,
367 LValue Dst,
368 LValue Src) const {
369 // DO NOTHING by default.
370 return false;
371 }
372 /// Emit the device-side copy of the builtin texture type.
373 virtual bool emitCUDADeviceBuiltinTextureDeviceCopy(CodeGenFunction &CGF,
374 LValue Dst,
375 LValue Src) const {
376 // DO NOTHING by default.
377 return false;
378 }
379 };
380
381 } // namespace CodeGen
382 } // namespace clang
383
384 #endif // LLVM_CLANG_LIB_CODEGEN_TARGETINFO_H