TargetInfo.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  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. #ifndef LLVM_CLANG_LIB_CODEGEN_TARGETINFO_H
  14. #define LLVM_CLANG_LIB_CODEGEN_TARGETINFO_H
  15. #include "CodeGenModule.h"
  16. #include "CGValue.h"
  17. #include "clang/AST/Type.h"
  18. #include "clang/Basic/LLVM.h"
  19. #include "clang/Basic/SyncScope.h"
  20. #include "llvm/ADT/SmallString.h"
  21. #include "llvm/ADT/StringRef.h"
  22. namespace llvm {
  23. class Constant;
  24. class GlobalValue;
  25. class Type;
  26. class Value;
  27. }
  28. namespace clang {
  29. class Decl;
  30. namespace CodeGen {
  31. class ABIInfo;
  32. class CallArgList;
  33. class CodeGenFunction;
  34. class CGBlockInfo;
  35. class CGFunctionInfo;
  36. /// TargetCodeGenInfo - This class organizes various target-specific
  37. /// codegeneration issues, like target-specific attributes, builtins and so
  38. /// on.
  39. class TargetCodeGenInfo {
  40. ABIInfo *Info;
  41. public:
  42. // WARNING: Acquires the ownership of ABIInfo.
  43. TargetCodeGenInfo(ABIInfo *info = nullptr) : Info(info) {}
  44. virtual ~TargetCodeGenInfo();
  45. /// getABIInfo() - Returns ABI info helper for the target.
  46. const ABIInfo &getABIInfo() const { return *Info; }
  47. /// setTargetAttributes - Provides a convenient hook to handle extra
  48. /// target-specific attributes for the given global.
  49. virtual void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
  50. CodeGen::CodeGenModule &M) const {}
  51. /// emitTargetMD - Provides a convenient hook to handle extra
  52. /// target-specific metadata for the given global.
  53. virtual void emitTargetMD(const Decl *D, llvm::GlobalValue *GV,
  54. CodeGen::CodeGenModule &M) const {}
  55. /// Determines the size of struct _Unwind_Exception on this platform,
  56. /// in 8-bit units. The Itanium ABI defines this as:
  57. /// struct _Unwind_Exception {
  58. /// uint64 exception_class;
  59. /// _Unwind_Exception_Cleanup_Fn exception_cleanup;
  60. /// uint64 private_1;
  61. /// uint64 private_2;
  62. /// };
  63. virtual unsigned getSizeOfUnwindException() const;
  64. /// Controls whether __builtin_extend_pointer should sign-extend
  65. /// pointers to uint64_t or zero-extend them (the default). Has
  66. /// no effect for targets:
  67. /// - that have 64-bit pointers, or
  68. /// - that cannot address through registers larger than pointers, or
  69. /// - that implicitly ignore/truncate the top bits when addressing
  70. /// through such registers.
  71. virtual bool extendPointerWithSExt() const { return false; }
  72. /// Determines the DWARF register number for the stack pointer, for
  73. /// exception-handling purposes. Implements __builtin_dwarf_sp_column.
  74. ///
  75. /// Returns -1 if the operation is unsupported by this target.
  76. virtual int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const {
  77. return -1;
  78. }
  79. /// Initializes the given DWARF EH register-size table, a char*.
  80. /// Implements __builtin_init_dwarf_reg_size_table.
  81. ///
  82. /// Returns true if the operation is unsupported by this target.
  83. virtual bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
  84. llvm::Value *Address) const {
  85. return true;
  86. }
  87. /// Performs the code-generation required to convert a return
  88. /// address as stored by the system into the actual address of the
  89. /// next instruction that will be executed.
  90. ///
  91. /// Used by __builtin_extract_return_addr().
  92. virtual llvm::Value *decodeReturnAddress(CodeGen::CodeGenFunction &CGF,
  93. llvm::Value *Address) const {
  94. return Address;
  95. }
  96. /// Performs the code-generation required to convert the address
  97. /// of an instruction into a return address suitable for storage
  98. /// by the system in a return slot.
  99. ///
  100. /// Used by __builtin_frob_return_addr().
  101. virtual llvm::Value *encodeReturnAddress(CodeGen::CodeGenFunction &CGF,
  102. llvm::Value *Address) const {
  103. return Address;
  104. }
  105. /// Corrects the low-level LLVM type for a given constraint and "usual"
  106. /// type.
  107. ///
  108. /// \returns A pointer to a new LLVM type, possibly the same as the original
  109. /// on success; 0 on failure.
  110. virtual llvm::Type *adjustInlineAsmType(CodeGen::CodeGenFunction &CGF,
  111. StringRef Constraint,
  112. llvm::Type *Ty) const {
  113. return Ty;
  114. }
  115. /// Adds constraints and types for result registers.
  116. virtual void addReturnRegisterOutputs(
  117. CodeGen::CodeGenFunction &CGF, CodeGen::LValue ReturnValue,
  118. std::string &Constraints, std::vector<llvm::Type *> &ResultRegTypes,
  119. std::vector<llvm::Type *> &ResultTruncRegTypes,
  120. std::vector<CodeGen::LValue> &ResultRegDests, std::string &AsmString,
  121. unsigned NumOutputs) const {}
  122. /// doesReturnSlotInterfereWithArgs - Return true if the target uses an
  123. /// argument slot for an 'sret' type.
  124. virtual bool doesReturnSlotInterfereWithArgs() const { return true; }
  125. /// Retrieve the address of a function to call immediately before
  126. /// calling objc_retainAutoreleasedReturnValue. The
  127. /// implementation of objc_autoreleaseReturnValue sniffs the
  128. /// instruction stream following its return address to decide
  129. /// whether it's a call to objc_retainAutoreleasedReturnValue.
  130. /// This can be prohibitively expensive, depending on the
  131. /// relocation model, and so on some targets it instead sniffs for
  132. /// a particular instruction sequence. This functions returns
  133. /// that instruction sequence in inline assembly, which will be
  134. /// empty if none is required.
  135. virtual StringRef getARCRetainAutoreleasedReturnValueMarker() const {
  136. return "";
  137. }
  138. /// Determine whether a call to objc_retainAutoreleasedReturnValue should be
  139. /// marked as 'notail'.
  140. virtual bool shouldSuppressTailCallsOfRetainAutoreleasedReturnValue() const {
  141. return false;
  142. }
  143. /// Return a constant used by UBSan as a signature to identify functions
  144. /// possessing type information, or 0 if the platform is unsupported.
  145. virtual llvm::Constant *
  146. getUBSanFunctionSignature(CodeGen::CodeGenModule &CGM) const {
  147. return nullptr;
  148. }
  149. /// Determine whether a call to an unprototyped functions under
  150. /// the given calling convention should use the variadic
  151. /// convention or the non-variadic convention.
  152. ///
  153. /// There's a good reason to make a platform's variadic calling
  154. /// convention be different from its non-variadic calling
  155. /// convention: the non-variadic arguments can be passed in
  156. /// registers (better for performance), and the variadic arguments
  157. /// can be passed on the stack (also better for performance). If
  158. /// this is done, however, unprototyped functions *must* use the
  159. /// non-variadic convention, because C99 states that a call
  160. /// through an unprototyped function type must succeed if the
  161. /// function was defined with a non-variadic prototype with
  162. /// compatible parameters. Therefore, splitting the conventions
  163. /// makes it impossible to call a variadic function through an
  164. /// unprototyped type. Since function prototypes came out in the
  165. /// late 1970s, this is probably an acceptable trade-off.
  166. /// Nonetheless, not all platforms are willing to make it, and in
  167. /// particularly x86-64 bends over backwards to make the
  168. /// conventions compatible.
  169. ///
  170. /// The default is false. This is correct whenever:
  171. /// - the conventions are exactly the same, because it does not
  172. /// matter and the resulting IR will be somewhat prettier in
  173. /// certain cases; or
  174. /// - the conventions are substantively different in how they pass
  175. /// arguments, because in this case using the variadic convention
  176. /// will lead to C99 violations.
  177. ///
  178. /// However, some platforms make the conventions identical except
  179. /// for passing additional out-of-band information to a variadic
  180. /// function: for example, x86-64 passes the number of SSE
  181. /// arguments in %al. On these platforms, it is desirable to
  182. /// call unprototyped functions using the variadic convention so
  183. /// that unprototyped calls to varargs functions still succeed.
  184. ///
  185. /// Relatedly, platforms which pass the fixed arguments to this:
  186. /// A foo(B, C, D);
  187. /// differently than they would pass them to this:
  188. /// A foo(B, C, D, ...);
  189. /// may need to adjust the debugger-support code in Sema to do the
  190. /// right thing when calling a function with no know signature.
  191. virtual bool isNoProtoCallVariadic(const CodeGen::CallArgList &args,
  192. const FunctionNoProtoType *fnType) const;
  193. /// Gets the linker options necessary to link a dependent library on this
  194. /// platform.
  195. virtual void getDependentLibraryOption(llvm::StringRef Lib,
  196. llvm::SmallString<24> &Opt) const;
  197. /// Gets the linker options necessary to detect object file mismatches on
  198. /// this platform.
  199. virtual void getDetectMismatchOption(llvm::StringRef Name,
  200. llvm::StringRef Value,
  201. llvm::SmallString<32> &Opt) const {}
  202. /// Get LLVM calling convention for OpenCL kernel.
  203. virtual unsigned getOpenCLKernelCallingConv() const;
  204. /// Get target specific null pointer.
  205. /// \param T is the LLVM type of the null pointer.
  206. /// \param QT is the clang QualType of the null pointer.
  207. /// \return ConstantPointerNull with the given type \p T.
  208. /// Each target can override it to return its own desired constant value.
  209. virtual llvm::Constant *getNullPointer(const CodeGen::CodeGenModule &CGM,
  210. llvm::PointerType *T, QualType QT) const;
  211. /// Get target favored AST address space of a global variable for languages
  212. /// other than OpenCL and CUDA.
  213. /// If \p D is nullptr, returns the default target favored address space
  214. /// for global variable.
  215. virtual LangAS getGlobalVarAddressSpace(CodeGenModule &CGM,
  216. const VarDecl *D) const;
  217. /// Get the AST address space for alloca.
  218. virtual LangAS getASTAllocaAddressSpace() const { return LangAS::Default; }
  219. /// Perform address space cast of an expression of pointer type.
  220. /// \param V is the LLVM value to be casted to another address space.
  221. /// \param SrcAddr is the language address space of \p V.
  222. /// \param DestAddr is the targeted language address space.
  223. /// \param DestTy is the destination LLVM pointer type.
  224. /// \param IsNonNull is the flag indicating \p V is known to be non null.
  225. virtual llvm::Value *performAddrSpaceCast(CodeGen::CodeGenFunction &CGF,
  226. llvm::Value *V, LangAS SrcAddr,
  227. LangAS DestAddr, llvm::Type *DestTy,
  228. bool IsNonNull = false) const;
  229. /// Perform address space cast of a constant expression of pointer type.
  230. /// \param V is the LLVM constant to be casted to another address space.
  231. /// \param SrcAddr is the language address space of \p V.
  232. /// \param DestAddr is the targeted language address space.
  233. /// \param DestTy is the destination LLVM pointer type.
  234. virtual llvm::Constant *performAddrSpaceCast(CodeGenModule &CGM,
  235. llvm::Constant *V,
  236. LangAS SrcAddr, LangAS DestAddr,
  237. llvm::Type *DestTy) const;
  238. /// Get address space of pointer parameter for __cxa_atexit.
  239. virtual LangAS getAddrSpaceOfCxaAtexitPtrParam() const {
  240. return LangAS::Default;
  241. }
  242. /// Get the syncscope used in LLVM IR.
  243. virtual llvm::SyncScope::ID getLLVMSyncScopeID(const LangOptions &LangOpts,
  244. SyncScope Scope,
  245. llvm::AtomicOrdering Ordering,
  246. llvm::LLVMContext &Ctx) const;
  247. /// Interface class for filling custom fields of a block literal for OpenCL.
  248. class TargetOpenCLBlockHelper {
  249. public:
  250. typedef std::pair<llvm::Value *, StringRef> ValueTy;
  251. TargetOpenCLBlockHelper() {}
  252. virtual ~TargetOpenCLBlockHelper() {}
  253. /// Get the custom field types for OpenCL blocks.
  254. virtual llvm::SmallVector<llvm::Type *, 1> getCustomFieldTypes() = 0;
  255. /// Get the custom field values for OpenCL blocks.
  256. virtual llvm::SmallVector<ValueTy, 1>
  257. getCustomFieldValues(CodeGenFunction &CGF, const CGBlockInfo &Info) = 0;
  258. virtual bool areAllCustomFieldValuesConstant(const CGBlockInfo &Info) = 0;
  259. /// Get the custom field values for OpenCL blocks if all values are LLVM
  260. /// constants.
  261. virtual llvm::SmallVector<llvm::Constant *, 1>
  262. getCustomFieldValues(CodeGenModule &CGM, const CGBlockInfo &Info) = 0;
  263. };
  264. virtual TargetOpenCLBlockHelper *getTargetOpenCLBlockHelper() const {
  265. return nullptr;
  266. }
  267. /// Create an OpenCL kernel for an enqueued block. The kernel function is
  268. /// a wrapper for the block invoke function with target-specific calling
  269. /// convention and ABI as an OpenCL kernel. The wrapper function accepts
  270. /// block context and block arguments in target-specific way and calls
  271. /// the original block invoke function.
  272. virtual llvm::Function *
  273. createEnqueuedBlockKernel(CodeGenFunction &CGF,
  274. llvm::Function *BlockInvokeFunc,
  275. llvm::Value *BlockLiteral) const;
  276. /// \return true if the target supports alias from the unmangled name to the
  277. /// mangled name of functions declared within an extern "C" region and marked
  278. /// as 'used', and having internal linkage.
  279. virtual bool shouldEmitStaticExternCAliases() const { return true; }
  280. virtual void setCUDAKernelCallingConvention(const FunctionType *&FT) const {}
  281. };
  282. } // namespace CodeGen
  283. } // namespace clang
  284. #endif // LLVM_CLANG_LIB_CODEGEN_TARGETINFO_H