clang 10.0.0git

TargetInfo.h
Go to the documentation of this file.
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 "CodeGenModule.h"
18 #include "CGValue.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"
24
25 namespace llvm {
26 class Constant;
27 class GlobalValue;
28 class Type;
29 class Value;
30 }
31
32 namespace clang {
33 class Decl;
34
35 namespace CodeGen {
36 class ABIInfo;
37 class CallArgList;
38 class CodeGenFunction;
39 class CGBlockInfo;
40 class CGFunctionInfo;
41
42 /// TargetCodeGenInfo - This class organizes various target-specific
43 /// codegeneration issues, like target-specific attributes, builtins and so
44 /// on.
45 class TargetCodeGenInfo {
46 ABIInfo *Info;
47
48 public:
49 // WARNING: Acquires the ownership of ABIInfo.
50 TargetCodeGenInfo(ABIInfo *info = nullptr) : Info(info) {}
51 virtual ~TargetCodeGenInfo();
52
53 /// getABIInfo() - Returns ABI info helper for the target.
54 const ABIInfo &getABIInfo() const { return *Info; }
55
56 /// setTargetAttributes - Provides a convenient hook to handle extra
57 /// target-specific attributes for the given global.
58 virtual void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
59 CodeGen::CodeGenModule &M) const {}
60
61 /// emitTargetMD - Provides a convenient hook to handle extra
62 /// 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;
91 }
92
93 /// 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 }
101
102 /// 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 }
111
112 /// 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 }
121
122 /// 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 }
132
133 /// 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
141 /// doesReturnSlotInterfereWithArgs - Return true if the target uses an
142 /// argument slot for an 'sret' type.
143 virtual bool doesReturnSlotInterfereWithArgs() const { return true; }
144
145 /// 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 }
158
159 /// Determine whether a call to objc_retainAutoreleasedReturnValue should be
160 /// marked as 'notail'.
161 virtual bool shouldSuppressTailCallsOfRetainAutoreleasedReturnValue() const {
162 return false;
163 }
164
165 /// 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 {
169 return nullptr;
170 }
171
172 /// 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;
216
217 /// 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;
221
222 /// 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 {}
227
228 /// Get LLVM calling convention for OpenCL kernel.
229 virtual unsigned getOpenCLKernelCallingConv() const;
230
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
239 /// 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.
243 virtual LangAS getGlobalVarAddressSpace(CodeGenModule &CGM,
244 const VarDecl *D) const;
245
246 /// Get the AST address space for alloca.
247 virtual LangAS getASTAllocaAddressSpace() const { return LangAS::Default; }
248
249 /// Perform address space cast of an expression of pointer type.
250 /// \param V is the LLVM value to be casted to another address space.
251 /// \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.
255 virtual llvm::Value *performAddrSpaceCast(CodeGen::CodeGenFunction &CGF,
256 llvm::Value *V, LangAS SrcAddr,
257 LangAS DestAddr, llvm::Type *DestTy,
258 bool IsNonNull = false) const;
259
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.
265 virtual llvm::Constant *performAddrSpaceCast(CodeGenModule &CGM,
266 llvm::Constant *V,
267 LangAS SrcAddr, LangAS DestAddr,
268 llvm::Type *DestTy) const;
269
270 /// Get address space of pointer parameter for __cxa_atexit.
271 virtual LangAS getAddrSpaceOfCxaAtexitPtrParam() const {
272 return LangAS::Default;
273 }
274
275 /// Get the syncscope used in LLVM IR.
276 virtual llvm::SyncScope::ID getLLVMSyncScopeID(const LangOptions &LangOpts,
277 SyncScope Scope,
278 llvm::AtomicOrdering Ordering,
279 llvm::LLVMContext &Ctx) const;
280
281 /// Interface class for filling custom fields of a block literal for OpenCL.
282 class TargetOpenCLBlockHelper {
283 public:
284 typedef std::pair<llvm::Value *, StringRef> ValueTy;
285 TargetOpenCLBlockHelper() {}
286 virtual ~TargetOpenCLBlockHelper() {}
287 /// Get the custom field types for OpenCL blocks.
288 virtual llvm::SmallVector<llvm::Type *, 1> getCustomFieldTypes() = 0;
289 /// Get the custom field values for OpenCL blocks.
290 virtual llvm::SmallVector<ValueTy, 1>
291 getCustomFieldValues(CodeGenFunction &CGF, const CGBlockInfo &Info) = 0;
292 virtual bool areAllCustomFieldValuesConstant(const CGBlockInfo &Info) = 0;
293 /// Get the custom field values for OpenCL blocks if all values are LLVM
294 /// constants.
295 virtual llvm::SmallVector<llvm::Constant *, 1>
296 getCustomFieldValues(CodeGenModule &CGM, const CGBlockInfo &Info) = 0;
297 };
298 virtual TargetOpenCLBlockHelper *getTargetOpenCLBlockHelper() const {
299 return nullptr;
300 }
301
302 /// Create an OpenCL kernel for an enqueued block. The kernel function is
303 /// a wrapper for the block invoke function with target-specific calling
304 /// convention and ABI as an OpenCL kernel. The wrapper function accepts
305 /// block context and block arguments in target-specific way and calls
306 /// the original block invoke function.
307 virtual llvm::Function *
308 createEnqueuedBlockKernel(CodeGenFunction &CGF,
309 llvm::Function *BlockInvokeFunc,
310 llvm::Value *BlockLiteral) const;
311
312 /// \return true if the target supports alias from the unmangled name to the
313 /// mangled name of functions declared within an extern "C" region and marked
314 /// as 'used', and having internal linkage.
315 virtual bool shouldEmitStaticExternCAliases() const { return true; }
316
317 virtual void setCUDAKernelCallingConvention(const FunctionType *&FT) const {}
318 };
319
320 } // namespace CodeGen
321 } // namespace clang
322
323 #endif // LLVM_CLANG_LIB_CODEGEN_TARGETINFO_H
clang::Builtin::ID
ID
Definition: Builtins.h:46
clang::QualType
A (possibly-)qualified type.
Definition: Type.h:654
llvm
Specialize PointerLikeTypeTraits to allow LazyGenerationalUpdatePtr to be placed into a PointerUnion...
Definition: Dominators.h:30
clang::FunctionType
FunctionType - C99 6.7.5.3 - Function Declarators.
Definition: Type.h:3422
Type.h
C Language Family Type Representation.
clang::Decl
Decl - This represents one declaration (or definition), e.g.
Definition: DeclBase.h:88
clang::CodeGen::TargetCodeGenInfo::getABIInfo
const ABIInfo & getABIInfo() const
getABIInfo() - Returns ABI info helper for the target.
Definition: TargetInfo.h:54
clang::CodeGen::AlignmentSource::Decl
The l-value was an access to a declared entity or something equivalently strong, like the address of ...
clang::CodeGen::TargetCodeGenInfo::extendPointerWithSExt
virtual bool extendPointerWithSExt() const
Controls whether __builtin_extend_pointer should sign-extend pointers to uint64_t or zero-extend them...
Definition: TargetInfo.h:83
Value
Value
Definition: UninitializedValues.cpp:101
clang::interp::ReturnValue
bool ReturnValue(const T &V, APValue &R)
Convers a value to an APValue.
Definition: Interp.h:41
clang::VarDecl
Represents a variable declaration or definition.
Definition: Decl.h:820
clang::CodeGen::TargetCodeGenInfo::getARCRetainAutoreleasedReturnValueMarker
virtual StringRef getARCRetainAutoreleasedReturnValueMarker() const
Retrieve the address of a function to call immediately before calling objc_retainAutoreleasedReturnVa...
Definition: TargetInfo.h:155
clang::LangAS
LangAS
Defines the address space values used by the address space qualifier of QualType. ...
Definition: AddressSpaces.h:25
clang::CodeGen::TargetCodeGenInfo::TargetOpenCLBlockHelper::ValueTy
std::pair< llvm::Value *, StringRef > ValueTy
Definition: TargetInfo.h:284
clang::CodeGen::TargetCodeGenInfo::getUBSanFunctionSignature
virtual llvm::Constant * getUBSanFunctionSignature(CodeGen::CodeGenModule &CGM) const
Return a constant used by UBSan as a signature to identify functions possessing type information...
Definition: TargetInfo.h:168
clang::CodeGen::TargetCodeGenInfo::getDetectMismatchOption
virtual void getDetectMismatchOption(llvm::StringRef Name, llvm::StringRef Value, llvm::SmallString< 32 > &Opt) const
Gets the linker options necessary to detect object file mismatches on this platform.
Definition: TargetInfo.h:224
clang::CodeGen::CodeGenFunction
CodeGenFunction - This class organizes the per-function state that is used while generating LLVM code...
Definition: CodeGenFunction.h:227
CGValue.h
clang::LangOptions
Keeps track of the various options that can be enabled, which controls the dialect of C or C++ that i...
Definition: LangOptions.h:53
LLVM.h
Forward-declares and imports various common LLVM datatypes that clang wants to use unqualified...
clang::CodeGen::TargetCodeGenInfo::getTargetOpenCLBlockHelper
virtual TargetOpenCLBlockHelper * getTargetOpenCLBlockHelper() const
Definition: TargetInfo.h:298
clang::CodeGen::TargetCodeGenInfo::shouldSuppressTailCallsOfRetainAutoreleasedReturnValue
virtual bool shouldSuppressTailCallsOfRetainAutoreleasedReturnValue() const
Determine whether a call to objc_retainAutoreleasedReturnValue should be marked as &#39;notail&#39;...
Definition: TargetInfo.h:161
clang::Scope
Scope - A scope is a transient data structure that is used while parsing the program.
Definition: Scope.h:40
llvm::SmallString
Definition: LLVM.h:34
clang::CodeGen::TargetCodeGenInfo::setCUDAKernelCallingConvention
virtual void setCUDAKernelCallingConvention(const FunctionType *&FT) const
Definition: TargetInfo.h:317
clang::FunctionNoProtoType
Represents a K&R-style &#39;int foo()&#39; function, which has no information available about its arguments...
Definition: Type.h:3717
clang::CodeGen::TargetCodeGenInfo::TargetOpenCLBlockHelper
Interface class for filling custom fields of a block literal for OpenCL.
Definition: TargetInfo.h:282
clang::CodeGen::CGBlockInfo
CGBlockInfo - Information to generate a block literal.
Definition: CGBlocks.h:152
clang::CodeGen::TargetCodeGenInfo::TargetCodeGenInfo
TargetCodeGenInfo(ABIInfo *info=nullptr)
Definition: TargetInfo.h:50
CodeGenModule.h
V
#define V(N, I)
Definition: ASTContext.h:2941
clang::CodeGen::TargetCodeGenInfo::shouldEmitStaticExternCAliases
virtual bool shouldEmitStaticExternCAliases() const
Definition: TargetInfo.h:315
clang::CodeGen::TargetCodeGenInfo::initDwarfEHRegSizeTable
virtual bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, llvm::Value *Address) const
Initializes the given DWARF EH register-size table, a char*.
Definition: TargetInfo.h:97
clang::CodeGen::TargetCodeGenInfo::doesReturnSlotInterfereWithArgs
virtual bool doesReturnSlotInterfereWithArgs() const
doesReturnSlotInterfereWithArgs - Return true if the target uses an argument slot for an &#39;sret&#39; type...
Definition: TargetInfo.h:143
clang::SyncScope
SyncScope
Defines synch scope values used internally by clang.
Definition: SyncScope.h:42
llvm::SmallVector
Definition: LLVM.h:35
clang::CodeGen::Address
An aligned address.
Definition: Address.h:24
clang::CodeGen::TargetCodeGenInfo
TargetCodeGenInfo - This class organizes various target-specific codegeneration issues, like target-specific attributes, builtins and so on.
Definition: TargetInfo.h:45
clang::CodeGen::CodeGenModule
This class organizes the cross-function state that is used while generating LLVM code.
Definition: CodeGenModule.h:280
Type
Optional< types::ID > Type
Definition: InterpolatingCompilationDatabase.cpp:127
clang
Dataflow Directional Tag Classes.
Definition: CFGReachabilityAnalysis.h:21
clang::CodeGen::TargetCodeGenInfo::TargetOpenCLBlockHelper::~TargetOpenCLBlockHelper
virtual ~TargetOpenCLBlockHelper()
Definition: TargetInfo.h:286
clang::CodeGen::TargetCodeGenInfo::setTargetAttributes
virtual void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &M) const
setTargetAttributes - Provides a convenient hook to handle extra target-specific attributes for the g...
Definition: TargetInfo.h:58
clang::CodeGen::TargetCodeGenInfo::emitTargetMD
virtual void emitTargetMD(const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &M) const
emitTargetMD - Provides a convenient hook to handle extra target-specific metadata for the given glob...
Definition: TargetInfo.h:63
clang::CodeGen::TargetCodeGenInfo::getASTAllocaAddressSpace
virtual LangAS getASTAllocaAddressSpace() const
Get the AST address space for alloca.
Definition: TargetInfo.h:247
SyncScope.h
Provides definitions for the atomic synchronization scopes.
clang::CodeGen::TargetCodeGenInfo::getDwarfEHStackPointer
virtual int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const
Determines the DWARF register number for the stack pointer, for exception-handling purposes...
Definition: TargetInfo.h:89
clang::CodeGen::TargetCodeGenInfo::adjustInlineAsmType
virtual llvm::Type * adjustInlineAsmType(CodeGen::CodeGenFunction &CGF, StringRef Constraint, llvm::Type *Ty) const
Corrects the low-level LLVM type for a given constraint and "usual" type.
Definition: TargetInfo.h:127
clang::CodeGen::TargetCodeGenInfo::TargetOpenCLBlockHelper::TargetOpenCLBlockHelper
TargetOpenCLBlockHelper()
Definition: TargetInfo.h:285
clang::CodeGen::TargetCodeGenInfo::addReturnRegisterOutputs
virtual void addReturnRegisterOutputs(CodeGen::CodeGenFunction &CGF, CodeGen::LValue ReturnValue, std::string &Constraints, std::vector< llvm::Type *> &ResultRegTypes, std::vector< llvm::Type *> &ResultTruncRegTypes, std::vector< CodeGen::LValue > &ResultRegDests, std::string &AsmString, unsigned NumOutputs) const
Adds constraints and types for result registers.
Definition: TargetInfo.h:134
clang::CodeGen::ABIInfo
ABIInfo - Target specific hooks for defining how a type should be passed or returned from functions...
Definition: ABIInfo.h:50
clang::CodeGen::TargetCodeGenInfo::getAddrSpaceOfCxaAtexitPtrParam
virtual LangAS getAddrSpaceOfCxaAtexitPtrParam() const
Get address space of pointer parameter for __cxa_atexit.
Definition: TargetInfo.h:271
clang::CodeGen::TargetCodeGenInfo::decodeReturnAddress
virtual llvm::Value * decodeReturnAddress(CodeGen::CodeGenFunction &CGF, llvm::Value *Address) const
Performs the code-generation required to convert a return address as stored by the system into the ac...
Definition: TargetInfo.h:107
clang::CodeGen::LValue
LValue - This represents an lvalue references.
Definition: CGValue.h:167
clang::CodeGen::CallArgList
CallArgList - Type for representing both the value and type of arguments in a call.
Definition: CGCall.h:261
clang::CodeGen::TargetCodeGenInfo::encodeReturnAddress
virtual llvm::Value * encodeReturnAddress(CodeGen::CodeGenFunction &CGF, llvm::Value *Address) const
Performs the code-generation required to convert the address of an instruction into a return address ...
Definition: TargetInfo.h:117