TargetInfo.h 17 KB

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