TargetInfo.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  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. /// TargetCodeGenInfo - This class organizes various target-specific
  37. /// codegeneration issues, like target-specific attributes, builtins and so
  38. /// on.
  39. class TargetCodeGenInfo {
  40. std::unique_ptr<ABIInfo> Info = nullptr;
  41. public:
  42. TargetCodeGenInfo(std::unique_ptr<ABIInfo> Info) : Info(std::move(Info)) {}
  43. virtual ~TargetCodeGenInfo();
  44. /// getABIInfo() - Returns ABI info helper for the target.
  45. const ABIInfo &getABIInfo() const { return *Info; }
  46. /// setTargetAttributes - Provides a convenient hook to handle extra
  47. /// target-specific attributes for the given global.
  48. virtual void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
  49. CodeGen::CodeGenModule &M) const {}
  50. /// emitTargetMetadata - Provides a convenient hook to handle extra
  51. /// target-specific metadata for the given globals.
  52. virtual void emitTargetMetadata(
  53. CodeGen::CodeGenModule &CGM,
  54. const llvm::MapVector<GlobalDecl, StringRef> &MangledDeclNames) const {}
  55. /// Any further codegen related checks that need to be done on a function call
  56. /// in a target specific manner.
  57. virtual void checkFunctionCallABI(CodeGenModule &CGM, SourceLocation CallLoc,
  58. const FunctionDecl *Caller,
  59. const FunctionDecl *Callee,
  60. const CallArgList &Args) const {}
  61. /// Determines the size of struct _Unwind_Exception on this platform,
  62. /// in 8-bit units. The Itanium ABI defines this as:
  63. /// struct _Unwind_Exception {
  64. /// uint64 exception_class;
  65. /// _Unwind_Exception_Cleanup_Fn exception_cleanup;
  66. /// uint64 private_1;
  67. /// uint64 private_2;
  68. /// };
  69. virtual unsigned getSizeOfUnwindException() const;
  70. /// Controls whether __builtin_extend_pointer should sign-extend
  71. /// pointers to uint64_t or zero-extend them (the default). Has
  72. /// no effect for targets:
  73. /// - that have 64-bit pointers, or
  74. /// - that cannot address through registers larger than pointers, or
  75. /// - that implicitly ignore/truncate the top bits when addressing
  76. /// through such registers.
  77. virtual bool extendPointerWithSExt() const { return false; }
  78. /// Determines the DWARF register number for the stack pointer, for
  79. /// exception-handling purposes. Implements __builtin_dwarf_sp_column.
  80. ///
  81. /// Returns -1 if the operation is unsupported by this target.
  82. virtual int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const {
  83. return -1;
  84. }
  85. /// Initializes the given DWARF EH register-size table, a char*.
  86. /// Implements __builtin_init_dwarf_reg_size_table.
  87. ///
  88. /// Returns true if the operation is unsupported by this target.
  89. virtual bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
  90. llvm::Value *Address) const {
  91. return true;
  92. }
  93. /// Performs the code-generation required to convert a return
  94. /// address as stored by the system into the actual address of the
  95. /// next instruction that will be executed.
  96. ///
  97. /// Used by __builtin_extract_return_addr().
  98. virtual llvm::Value *decodeReturnAddress(CodeGen::CodeGenFunction &CGF,
  99. llvm::Value *Address) const {
  100. return Address;
  101. }
  102. /// Performs the code-generation required to convert the address
  103. /// of an instruction into a return address suitable for storage
  104. /// by the system in a return slot.
  105. ///
  106. /// Used by __builtin_frob_return_addr().
  107. virtual llvm::Value *encodeReturnAddress(CodeGen::CodeGenFunction &CGF,
  108. llvm::Value *Address) const {
  109. return Address;
  110. }
  111. /// Performs a target specific test of a floating point value for things
  112. /// like IsNaN, Infinity, ... Nullptr is returned if no implementation
  113. /// exists.
  114. virtual llvm::Value *
  115. testFPKind(llvm::Value *V, unsigned BuiltinID, CGBuilderTy &Builder,
  116. CodeGenModule &CGM) const {
  117. assert(V->getType()->isFloatingPointTy() && "V should have an FP type.");
  118. return nullptr;
  119. }
  120. /// Corrects the low-level LLVM type for a given constraint and "usual"
  121. /// type.
  122. ///
  123. /// \returns A pointer to a new LLVM type, possibly the same as the original
  124. /// on success; 0 on failure.
  125. virtual llvm::Type *adjustInlineAsmType(CodeGen::CodeGenFunction &CGF,
  126. StringRef Constraint,
  127. llvm::Type *Ty) const {
  128. return Ty;
  129. }
  130. /// Target hook to decide whether an inline asm operand can be passed
  131. /// by value.
  132. virtual bool isScalarizableAsmOperand(CodeGen::CodeGenFunction &CGF,
  133. llvm::Type *Ty) const {
  134. return false;
  135. }
  136. /// Adds constraints and types for result registers.
  137. virtual void addReturnRegisterOutputs(
  138. CodeGen::CodeGenFunction &CGF, CodeGen::LValue ReturnValue,
  139. std::string &Constraints, std::vector<llvm::Type *> &ResultRegTypes,
  140. std::vector<llvm::Type *> &ResultTruncRegTypes,
  141. std::vector<CodeGen::LValue> &ResultRegDests, std::string &AsmString,
  142. unsigned NumOutputs) const {}
  143. /// doesReturnSlotInterfereWithArgs - Return true if the target uses an
  144. /// argument slot for an 'sret' type.
  145. virtual bool doesReturnSlotInterfereWithArgs() const { return true; }
  146. /// Retrieve the address of a function to call immediately before
  147. /// calling objc_retainAutoreleasedReturnValue. The
  148. /// implementation of objc_autoreleaseReturnValue sniffs the
  149. /// instruction stream following its return address to decide
  150. /// whether it's a call to objc_retainAutoreleasedReturnValue.
  151. /// This can be prohibitively expensive, depending on the
  152. /// relocation model, and so on some targets it instead sniffs for
  153. /// a particular instruction sequence. This functions returns
  154. /// that instruction sequence in inline assembly, which will be
  155. /// empty if none is required.
  156. virtual StringRef getARCRetainAutoreleasedReturnValueMarker() const {
  157. return "";
  158. }
  159. /// Determine whether a call to objc_retainAutoreleasedReturnValue or
  160. /// objc_unsafeClaimAutoreleasedReturnValue should be marked as 'notail'.
  161. virtual bool markARCOptimizedReturnCallsAsNoTail() const { return false; }
  162. /// Return a constant used by UBSan as a signature to identify functions
  163. /// possessing type information, or 0 if the platform is unsupported.
  164. virtual llvm::Constant *
  165. getUBSanFunctionSignature(CodeGen::CodeGenModule &CGM) const {
  166. return nullptr;
  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. /// Gets the linker options necessary to link a dependent library on this
  213. /// platform.
  214. virtual void getDependentLibraryOption(llvm::StringRef Lib,
  215. llvm::SmallString<24> &Opt) const;
  216. /// Gets the linker options necessary to detect object file mismatches on
  217. /// this platform.
  218. virtual void getDetectMismatchOption(llvm::StringRef Name,
  219. llvm::StringRef Value,
  220. llvm::SmallString<32> &Opt) const {}
  221. /// Get LLVM calling convention for OpenCL kernel.
  222. virtual unsigned getOpenCLKernelCallingConv() const;
  223. /// Get target specific null pointer.
  224. /// \param T is the LLVM type of the null pointer.
  225. /// \param QT is the clang QualType of the null pointer.
  226. /// \return ConstantPointerNull with the given type \p T.
  227. /// Each target can override it to return its own desired constant value.
  228. virtual llvm::Constant *getNullPointer(const CodeGen::CodeGenModule &CGM,
  229. llvm::PointerType *T, QualType QT) const;
  230. /// Get target favored AST address space of a global variable for languages
  231. /// other than OpenCL and CUDA.
  232. /// If \p D is nullptr, returns the default target favored address space
  233. /// for global variable.
  234. virtual LangAS getGlobalVarAddressSpace(CodeGenModule &CGM,
  235. const VarDecl *D) const;
  236. /// Get the AST address space for alloca.
  237. virtual LangAS getASTAllocaAddressSpace() const { return LangAS::Default; }
  238. /// Perform address space cast of an expression of pointer type.
  239. /// \param V is the LLVM value to be casted to another address space.
  240. /// \param SrcAddr is the language address space of \p V.
  241. /// \param DestAddr is the targeted language address space.
  242. /// \param DestTy is the destination LLVM pointer type.
  243. /// \param IsNonNull is the flag indicating \p V is known to be non null.
  244. virtual llvm::Value *performAddrSpaceCast(CodeGen::CodeGenFunction &CGF,
  245. llvm::Value *V, LangAS SrcAddr,
  246. LangAS DestAddr, llvm::Type *DestTy,
  247. bool IsNonNull = false) const;
  248. /// Perform address space cast of a constant expression of pointer type.
  249. /// \param V is the LLVM constant to be casted to another address space.
  250. /// \param SrcAddr is the language address space of \p V.
  251. /// \param DestAddr is the targeted language address space.
  252. /// \param DestTy is the destination LLVM pointer type.
  253. virtual llvm::Constant *performAddrSpaceCast(CodeGenModule &CGM,
  254. llvm::Constant *V,
  255. LangAS SrcAddr, LangAS DestAddr,
  256. llvm::Type *DestTy) const;
  257. /// Get address space of pointer parameter for __cxa_atexit.
  258. virtual LangAS getAddrSpaceOfCxaAtexitPtrParam() const {
  259. return LangAS::Default;
  260. }
  261. /// Get the syncscope used in LLVM IR.
  262. virtual llvm::SyncScope::ID getLLVMSyncScopeID(const LangOptions &LangOpts,
  263. SyncScope Scope,
  264. llvm::AtomicOrdering Ordering,
  265. llvm::LLVMContext &Ctx) const;
  266. /// Interface class for filling custom fields of a block literal for OpenCL.
  267. class TargetOpenCLBlockHelper {
  268. public:
  269. typedef std::pair<llvm::Value *, StringRef> ValueTy;
  270. TargetOpenCLBlockHelper() {}
  271. virtual ~TargetOpenCLBlockHelper() {}
  272. /// Get the custom field types for OpenCL blocks.
  273. virtual llvm::SmallVector<llvm::Type *, 1> getCustomFieldTypes() = 0;
  274. /// Get the custom field values for OpenCL blocks.
  275. virtual llvm::SmallVector<ValueTy, 1>
  276. getCustomFieldValues(CodeGenFunction &CGF, const CGBlockInfo &Info) = 0;
  277. virtual bool areAllCustomFieldValuesConstant(const CGBlockInfo &Info) = 0;
  278. /// Get the custom field values for OpenCL blocks if all values are LLVM
  279. /// constants.
  280. virtual llvm::SmallVector<llvm::Constant *, 1>
  281. getCustomFieldValues(CodeGenModule &CGM, const CGBlockInfo &Info) = 0;
  282. };
  283. virtual TargetOpenCLBlockHelper *getTargetOpenCLBlockHelper() const {
  284. return nullptr;
  285. }
  286. /// Create an OpenCL kernel for an enqueued block. The kernel function is
  287. /// a wrapper for the block invoke function with target-specific calling
  288. /// convention and ABI as an OpenCL kernel. The wrapper function accepts
  289. /// block context and block arguments in target-specific way and calls
  290. /// the original block invoke function.
  291. virtual llvm::Function *
  292. createEnqueuedBlockKernel(CodeGenFunction &CGF,
  293. llvm::Function *BlockInvokeFunc,
  294. llvm::Value *BlockLiteral) const;
  295. /// \return true if the target supports alias from the unmangled name to the
  296. /// mangled name of functions declared within an extern "C" region and marked
  297. /// as 'used', and having internal linkage.
  298. virtual bool shouldEmitStaticExternCAliases() const { return true; }
  299. virtual void setCUDAKernelCallingConvention(const FunctionType *&FT) const {}
  300. /// Return the device-side type for the CUDA device builtin surface type.
  301. virtual llvm::Type *getCUDADeviceBuiltinSurfaceDeviceType() const {
  302. // By default, no change from the original one.
  303. return nullptr;
  304. }
  305. /// Return the device-side type for the CUDA device builtin texture type.
  306. virtual llvm::Type *getCUDADeviceBuiltinTextureDeviceType() const {
  307. // By default, no change from the original one.
  308. return nullptr;
  309. }
  310. /// Emit the device-side copy of the builtin surface type.
  311. virtual bool emitCUDADeviceBuiltinSurfaceDeviceCopy(CodeGenFunction &CGF,
  312. LValue Dst,
  313. LValue Src) const {
  314. // DO NOTHING by default.
  315. return false;
  316. }
  317. /// Emit the device-side copy of the builtin texture type.
  318. virtual bool emitCUDADeviceBuiltinTextureDeviceCopy(CodeGenFunction &CGF,
  319. LValue Dst,
  320. LValue Src) const {
  321. // DO NOTHING by default.
  322. return false;
  323. }
  324. };
  325. } // namespace CodeGen
  326. } // namespace clang
  327. #endif // LLVM_CLANG_LIB_CODEGEN_TARGETINFO_H