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