Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 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 | |
Benjamin Kramer | 2f5db8b | 2014-08-13 16:25:19 +0000 | [diff] [blame] | 15 | #ifndef LLVM_CLANG_LIB_CODEGEN_TARGETINFO_H |
| 16 | #define LLVM_CLANG_LIB_CODEGEN_TARGETINFO_H |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 17 | |
Simon Atanasyan | 1a116db | 2017-07-20 20:34:18 +0000 | [diff] [blame] | 18 | #include "CodeGenModule.h" |
Reid Kleckner | 9b3e3df | 2014-09-04 20:04:38 +0000 | [diff] [blame] | 19 | #include "CGValue.h" |
John McCall | cbc038a | 2011-09-21 08:08:30 +0000 | [diff] [blame] | 20 | #include "clang/AST/Type.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 21 | #include "clang/Basic/LLVM.h" |
Yaxun Liu | 3919506 | 2017-08-04 18:16:31 +0000 | [diff] [blame] | 22 | #include "clang/Basic/SyncScope.h" |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 23 | #include "llvm/ADT/SmallString.h" |
Chandler Carruth | 5553d0d | 2014-01-07 11:51:46 +0000 | [diff] [blame] | 24 | #include "llvm/ADT/StringRef.h" |
Peter Collingbourne | 8f5cf74 | 2011-02-19 23:03:58 +0000 | [diff] [blame] | 25 | |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 26 | namespace llvm { |
Rafael Espindola | 1404809 | 2014-05-09 00:26:20 +0000 | [diff] [blame] | 27 | class Constant; |
| 28 | class GlobalValue; |
| 29 | class Type; |
| 30 | class Value; |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 31 | } |
| 32 | |
| 33 | namespace clang { |
Rafael Espindola | 1404809 | 2014-05-09 00:26:20 +0000 | [diff] [blame] | 34 | class Decl; |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 35 | |
Rafael Espindola | 1404809 | 2014-05-09 00:26:20 +0000 | [diff] [blame] | 36 | namespace CodeGen { |
John McCall | 12f2352 | 2016-04-04 18:33:08 +0000 | [diff] [blame] | 37 | class ABIInfo; |
Rafael Espindola | 1404809 | 2014-05-09 00:26:20 +0000 | [diff] [blame] | 38 | class CallArgList; |
Rafael Espindola | 1404809 | 2014-05-09 00:26:20 +0000 | [diff] [blame] | 39 | class CodeGenFunction; |
Yaxun Liu | 10712d9 | 2017-10-04 20:32:17 +0000 | [diff] [blame] | 40 | class CGBlockInfo; |
Rafael Espindola | 1404809 | 2014-05-09 00:26:20 +0000 | [diff] [blame] | 41 | class CGFunctionInfo; |
Rafael Espindola | 1404809 | 2014-05-09 00:26:20 +0000 | [diff] [blame] | 42 | |
| 43 | /// TargetCodeGenInfo - This class organizes various target-specific |
| 44 | /// codegeneration issues, like target-specific attributes, builtins and so |
| 45 | /// on. |
| 46 | class TargetCodeGenInfo { |
| 47 | ABIInfo *Info; |
| 48 | |
| 49 | public: |
| 50 | // WARNING: Acquires the ownership of ABIInfo. |
Hans Wennborg | 59dbe86 | 2015-09-29 20:56:43 +0000 | [diff] [blame] | 51 | TargetCodeGenInfo(ABIInfo *info = nullptr) : Info(info) {} |
Rafael Espindola | 1404809 | 2014-05-09 00:26:20 +0000 | [diff] [blame] | 52 | virtual ~TargetCodeGenInfo(); |
| 53 | |
| 54 | /// getABIInfo() - Returns ABI info helper for the target. |
| 55 | const ABIInfo &getABIInfo() const { return *Info; } |
| 56 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 57 | /// setTargetAttributes - Provides a convenient hook to handle extra |
Rafael Espindola | 1404809 | 2014-05-09 00:26:20 +0000 | [diff] [blame] | 58 | /// target-specific attributes for the given global. |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 59 | virtual void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Simon Atanasyan | 1a116db | 2017-07-20 20:34:18 +0000 | [diff] [blame] | 60 | CodeGen::CodeGenModule &M, |
| 61 | ForDefinition_t IsForDefinition) const {} |
Rafael Espindola | 1404809 | 2014-05-09 00:26:20 +0000 | [diff] [blame] | 62 | |
Eric Christopher | 56df435 | 2015-06-05 22:03:01 +0000 | [diff] [blame] | 63 | /// emitTargetMD - Provides a convenient hook to handle extra |
Rafael Espindola | 1404809 | 2014-05-09 00:26:20 +0000 | [diff] [blame] | 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. |
| 91 | virtual int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const { |
| 92 | return -1; |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 93 | } |
| 94 | |
Rafael Espindola | 1404809 | 2014-05-09 00:26:20 +0000 | [diff] [blame] | 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. |
| 99 | virtual bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
| 100 | llvm::Value *Address) const { |
| 101 | return true; |
| 102 | } |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 103 | |
Rafael Espindola | 1404809 | 2014-05-09 00:26:20 +0000 | [diff] [blame] | 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 | } |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 113 | |
Rafael Espindola | 1404809 | 2014-05-09 00:26:20 +0000 | [diff] [blame] | 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(). |
| 119 | virtual llvm::Value *encodeReturnAddress(CodeGen::CodeGenFunction &CGF, |
| 120 | llvm::Value *Address) const { |
| 121 | return Address; |
| 122 | } |
John McCall | b6cc2c04 | 2010-03-02 03:50:12 +0000 | [diff] [blame] | 123 | |
Rafael Espindola | 1404809 | 2014-05-09 00:26:20 +0000 | [diff] [blame] | 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. |
| 129 | virtual llvm::Type *adjustInlineAsmType(CodeGen::CodeGenFunction &CGF, |
| 130 | StringRef Constraint, |
| 131 | llvm::Type *Ty) const { |
| 132 | return Ty; |
| 133 | } |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 134 | |
Reid Kleckner | 9b3e3df | 2014-09-04 20:04:38 +0000 | [diff] [blame] | 135 | /// Adds constraints and types for result registers. |
| 136 | virtual void addReturnRegisterOutputs( |
| 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 | |
Rafael Espindola | 1404809 | 2014-05-09 00:26:20 +0000 | [diff] [blame] | 143 | /// doesReturnSlotInterfereWithArgs - Return true if the target uses an |
| 144 | /// argument slot for an 'sret' type. |
| 145 | virtual bool doesReturnSlotInterfereWithArgs() const { return true; } |
John McCall | 5add20c | 2010-07-20 22:17:55 +0000 | [diff] [blame] | 146 | |
Rafael Espindola | 1404809 | 2014-05-09 00:26:20 +0000 | [diff] [blame] | 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 | } |
John McCall | d4f4b7f | 2010-03-03 04:15:11 +0000 | [diff] [blame] | 160 | |
Rafael Espindola | 1404809 | 2014-05-09 00:26:20 +0000 | [diff] [blame] | 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 * |
| 164 | getUBSanFunctionSignature(CodeGen::CodeGenModule &CGM) const { |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 165 | return nullptr; |
Rafael Espindola | 1404809 | 2014-05-09 00:26:20 +0000 | [diff] [blame] | 166 | } |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 167 | |
Rafael Espindola | 1404809 | 2014-05-09 00:26:20 +0000 | [diff] [blame] | 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; |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 212 | |
Rafael Espindola | 1404809 | 2014-05-09 00:26:20 +0000 | [diff] [blame] | 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; |
John McCall | d4f4b7f | 2010-03-03 04:15:11 +0000 | [diff] [blame] | 217 | |
Rafael Espindola | 1404809 | 2014-05-09 00:26:20 +0000 | [diff] [blame] | 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 {} |
Nikolay Haustov | 8c6538b | 2016-06-30 09:06:33 +0000 | [diff] [blame] | 223 | |
| 224 | /// Get LLVM calling convention for OpenCL kernel. |
| 225 | virtual unsigned getOpenCLKernelCallingConv() const; |
Yaxun Liu | 402804b | 2016-12-15 08:09:08 +0000 | [diff] [blame] | 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 | |
Yaxun Liu | cbf647c | 2017-07-08 13:24:52 +0000 | [diff] [blame] | 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. |
Alexander Richardson | 6d98943 | 2017-10-15 18:48:14 +0000 | [diff] [blame] | 239 | virtual LangAS getGlobalVarAddressSpace(CodeGenModule &CGM, |
| 240 | const VarDecl *D) const; |
Yaxun Liu | cbf647c | 2017-07-08 13:24:52 +0000 | [diff] [blame] | 241 | |
Yaxun Liu | 6d96f163 | 2017-05-18 18:51:09 +0000 | [diff] [blame] | 242 | /// Get the AST address space for alloca. |
Alexander Richardson | 6d98943 | 2017-10-15 18:48:14 +0000 | [diff] [blame] | 243 | virtual LangAS getASTAllocaAddressSpace() const { return LangAS::Default; } |
Yaxun Liu | 6d96f163 | 2017-05-18 18:51:09 +0000 | [diff] [blame] | 244 | |
Yaxun Liu | 402804b | 2016-12-15 08:09:08 +0000 | [diff] [blame] | 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. |
Yaxun Liu | 6d96f163 | 2017-05-18 18:51:09 +0000 | [diff] [blame] | 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. |
Yaxun Liu | 402804b | 2016-12-15 08:09:08 +0000 | [diff] [blame] | 251 | virtual llvm::Value *performAddrSpaceCast(CodeGen::CodeGenFunction &CGF, |
Alexander Richardson | 6d98943 | 2017-10-15 18:48:14 +0000 | [diff] [blame] | 252 | llvm::Value *V, LangAS SrcAddr, |
| 253 | LangAS DestAddr, llvm::Type *DestTy, |
Yaxun Liu | 6d96f163 | 2017-05-18 18:51:09 +0000 | [diff] [blame] | 254 | bool IsNonNull = false) const; |
Yaxun Liu | cbf647c | 2017-07-08 13:24:52 +0000 | [diff] [blame] | 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. |
Alexander Richardson | 6d98943 | 2017-10-15 18:48:14 +0000 | [diff] [blame] | 261 | virtual llvm::Constant *performAddrSpaceCast(CodeGenModule &CGM, |
| 262 | llvm::Constant *V, |
| 263 | LangAS SrcAddr, LangAS DestAddr, |
| 264 | llvm::Type *DestTy) const; |
Yaxun Liu | 3919506 | 2017-08-04 18:16:31 +0000 | [diff] [blame] | 265 | |
| 266 | /// Get the syncscope used in LLVM IR. |
| 267 | virtual llvm::SyncScope::ID getLLVMSyncScopeID(SyncScope S, |
| 268 | llvm::LLVMContext &C) const; |
Yaxun Liu | 10712d9 | 2017-10-04 20:32:17 +0000 | [diff] [blame] | 269 | |
| 270 | /// Inteface class for filling custom fields of a block literal for OpenCL. |
| 271 | class TargetOpenCLBlockHelper { |
| 272 | public: |
| 273 | typedef std::pair<llvm::Value *, StringRef> ValueTy; |
| 274 | TargetOpenCLBlockHelper() {} |
| 275 | virtual ~TargetOpenCLBlockHelper() {} |
| 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. |
| 279 | virtual llvm::SmallVector<ValueTy, 1> |
| 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. |
| 284 | virtual llvm::SmallVector<llvm::Constant *, 1> |
| 285 | getCustomFieldValues(CodeGenModule &CGM, const CGBlockInfo &Info) = 0; |
| 286 | }; |
| 287 | virtual TargetOpenCLBlockHelper *getTargetOpenCLBlockHelper() const { |
| 288 | return nullptr; |
| 289 | } |
Yaxun Liu | c2a87a0 | 2017-10-14 12:23:50 +0000 | [diff] [blame] | 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; |
Rafael Espindola | 1404809 | 2014-05-09 00:26:20 +0000 | [diff] [blame] | 300 | }; |
John McCall | 12f2352 | 2016-04-04 18:33:08 +0000 | [diff] [blame] | 301 | |
| 302 | } // namespace CodeGen |
Hans Wennborg | 59dbe86 | 2015-09-29 20:56:43 +0000 | [diff] [blame] | 303 | } // namespace clang |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 304 | |
Hans Wennborg | 59dbe86 | 2015-09-29 20:56:43 +0000 | [diff] [blame] | 305 | #endif // LLVM_CLANG_LIB_CODEGEN_TARGETINFO_H |