clang  6.0.0svn
TargetInfo.h
Go to the documentation of this file.
1 //===---- 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 
15 #ifndef LLVM_CLANG_LIB_CODEGEN_TARGETINFO_H
16 #define LLVM_CLANG_LIB_CODEGEN_TARGETINFO_H
17 
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 CGFunctionInfo;
42 
43 /// TargetCodeGenInfo - This class organizes various target-specific
44 /// codegeneration issues, like target-specific attributes, builtins and so
45 /// on.
47  ABIInfo *Info;
48 
49 public:
50  // WARNING: Acquires the ownership of ABIInfo.
51  TargetCodeGenInfo(ABIInfo *info = nullptr) : Info(info) {}
52  virtual ~TargetCodeGenInfo();
53 
54  /// getABIInfo() - Returns ABI info helper for the target.
55  const ABIInfo &getABIInfo() const { return *Info; }
56 
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,
61  ForDefinition_t IsForDefinition) const {}
62 
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 {}
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.
92  return -1;
93  }
94 
95  /// 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.
100  llvm::Value *Address) const {
101  return true;
102  }
103 
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.
107  ///
108  /// Used by __builtin_extract_return_addr().
110  llvm::Value *Address) const {
111  return Address;
112  }
113 
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.
117  ///
118  /// Used by __builtin_frob_return_addr().
120  llvm::Value *Address) const {
121  return Address;
122  }
123 
124  /// 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.
130  StringRef Constraint,
131  llvm::Type *Ty) const {
132  return Ty;
133  }
134 
135  /// Adds constraints and types for result registers.
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 
143  /// doesReturnSlotInterfereWithArgs - Return true if the target uses an
144  /// argument slot for an 'sret' type.
145  virtual bool doesReturnSlotInterfereWithArgs() const { return true; }
146 
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 {
158  return "";
159  }
160 
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 *
165  return nullptr;
166  }
167 
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.
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;
212 
213  /// 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;
217 
218  /// 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 {}
223 
224  /// Get LLVM calling convention for OpenCL kernel.
225  virtual unsigned getOpenCLKernelCallingConv() const;
226 
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 
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;
241 
242  /// Get the AST address space for alloca.
243  virtual LangAS getASTAllocaAddressSpace() const { return LangAS::Default; }
244 
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;
255 
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,
262  llvm::Constant *V,
263  LangAS SrcAddr, LangAS DestAddr,
264  llvm::Type *DestTy) const;
265 
266  /// Get the syncscope used in LLVM IR.
267  virtual llvm::SyncScope::ID getLLVMSyncScopeID(SyncScope S,
268  llvm::LLVMContext &C) const;
269 
270  /// Inteface class for filling custom fields of a block literal for OpenCL.
272  public:
273  typedef std::pair<llvm::Value *, StringRef> ValueTy;
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.
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.
285  getCustomFieldValues(CodeGenModule &CGM, const CGBlockInfo &Info) = 0;
286  };
288  return nullptr;
289  }
290 
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;
300 };
301 
302 } // namespace CodeGen
303 } // namespace clang
304 
305 #endif // LLVM_CLANG_LIB_CODEGEN_TARGETINFO_H
A (possibly-)qualified type.
Definition: Type.h:653
DominatorTree GraphTraits specialization so the DominatorTree can be iterable by generic graph iterat...
Definition: Dominators.h:26
C Language Family Type Representation.
Decl - This represents one declaration (or definition), e.g.
Definition: DeclBase.h:86
const ABIInfo & getABIInfo() const
getABIInfo() - Returns ABI info helper for the target.
Definition: TargetInfo.h:55
The l-value was an access to a declared entity or something equivalently strong, like the address of ...
virtual bool extendPointerWithSExt() const
Controls whether __builtin_extend_pointer should sign-extend pointers to uint64_t or zero-extend them...
Definition: TargetInfo.h:85
VarDecl - An instance of this class is created to represent a variable declaration or definition...
Definition: Decl.h:807
virtual StringRef getARCRetainAutoreleasedReturnValueMarker() const
Retrieve the address of a function to call immediately before calling objc_retainAutoreleasedReturnVa...
Definition: TargetInfo.h:157
LangAS
Defines the address space values used by the address space qualifier of QualType. ...
Definition: AddressSpaces.h:26
std::pair< llvm::Value *, StringRef > ValueTy
Definition: TargetInfo.h:273
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:164
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:220
CodeGenFunction - This class organizes the per-function state that is used while generating LLVM code...
Forward-declares and imports various common LLVM datatypes that clang wants to use unqualified...
virtual TargetOpenCLBlockHelper * getTargetOpenCLBlockHelper() const
Definition: TargetInfo.h:287
Represents a K&R-style &#39;int foo()&#39; function, which has no information available about its arguments...
Definition: Type.h:3235
Inteface class for filling custom fields of a block literal for OpenCL.
Definition: TargetInfo.h:271
CGBlockInfo - Information to generate a block literal.
Definition: CGBlocks.h:149
virtual void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &M, ForDefinition_t IsForDefinition) const
setTargetAttributes - Provides a convenient hook to handle extra target-specific attributes for the g...
Definition: TargetInfo.h:59
TargetCodeGenInfo(ABIInfo *info=nullptr)
Definition: TargetInfo.h:51
const FunctionProtoType * T
virtual bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, llvm::Value *Address) const
Initializes the given DWARF EH register-size table, a char*.
Definition: TargetInfo.h:99
virtual bool doesReturnSlotInterfereWithArgs() const
doesReturnSlotInterfereWithArgs - Return true if the target uses an argument slot for an &#39;sret&#39; type...
Definition: TargetInfo.h:145
The l-value was considered opaque, so the alignment was determined from a type.
SyncScope
Defines synch scope values used internally by clang.
Definition: SyncScope.h:43
An aligned address.
Definition: Address.h:25
TargetCodeGenInfo - This class organizes various target-specific codegeneration issues, like target-specific attributes, builtins and so on.
Definition: TargetInfo.h:46
This class organizes the cross-function state that is used while generating LLVM code.
Dataflow Directional Tag Classes.
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:65
virtual LangAS getASTAllocaAddressSpace() const
Get the AST address space for alloca.
Definition: TargetInfo.h:243
Provides definitions for the atomic synchronization scopes.
virtual int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const
Determines the DWARF register number for the stack pointer, for exception-handling purposes...
Definition: TargetInfo.h:91
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:129
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:136
ABIInfo - Target specific hooks for defining how a type should be passed or returned from functions...
Definition: ABIInfo.h:51
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:109
LValue - This represents an lvalue references.
Definition: CGValue.h:167
CallArgList - Type for representing both the value and type of arguments in a call.
Definition: CGCall.h:182
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:119