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 15 #ifndef LLVM_CLANG_LIB_CODEGEN_TARGETINFO_H 16 #define LLVM_CLANG_LIB_CODEGEN_TARGETINFO_H 17 18 #include "CodeGenModule.h" 19 #include "CGValue.h" 20 #include "clang/AST/Type.h" 21 #include "clang/Basic/LLVM.h" 22 #include "clang/Basic/SyncScope.h" 23 #include "llvm/ADT/SmallString.h" 24 #include "llvm/ADT/StringRef.h" 25 26 namespace llvm { 27 class Constant; 28 class GlobalValue; 29 class Type; 30 class Value; 31 } 32 33 namespace clang { 34 class Decl; 35 36 namespace CodeGen { 37 class ABIInfo; 38 class CallArgList; 39 class CodeGenFunction; 40 class CGBlockInfo; 41 class CGFunctionInfo; 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. 51 TargetCodeGenInfo(ABIInfo *info = nullptr) : Info(info) {} 52 virtual ~TargetCodeGenInfo(); 53 54 /// getABIInfo() - Returns ABI info helper for the target. 55 const ABIInfo &getABIInfo() const { return *Info; } 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, 61 ForDefinition_t IsForDefinition) const {} 62 63 /// emitTargetMD - Provides a convenient hook to handle extra 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; 93 } 94 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 } 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 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 } 123 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 } 134 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 143 /// doesReturnSlotInterfereWithArgs - Return true if the target uses an 144 /// argument slot for an 'sret' type. 145 virtual bool doesReturnSlotInterfereWithArgs() const { return true; } 146 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 } 160 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 { 165 return nullptr; 166 } 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 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; 217 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 {} 223 224 /// Get LLVM calling convention for OpenCL kernel. 225 virtual unsigned getOpenCLKernelCallingConv() const; 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 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. 239 virtual unsigned getGlobalVarAddressSpace(CodeGenModule &CGM, 240 const VarDecl *D) const; 241 242 /// Get the AST address space for alloca. 243 virtual unsigned getASTAllocaAddressSpace() const { return LangAS::Default; } 244 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. 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. 251 virtual llvm::Value *performAddrSpaceCast(CodeGen::CodeGenFunction &CGF, 252 llvm::Value *V, unsigned SrcAddr, 253 unsigned DestAddr, 254 llvm::Type *DestTy, 255 bool IsNonNull = false) const; 256 257 /// Perform address space cast of a constant expression of pointer type. 258 /// \param V is the LLVM constant to be casted to another address space. 259 /// \param SrcAddr is the language address space of \p V. 260 /// \param DestAddr is the targeted language address space. 261 /// \param DestTy is the destination LLVM pointer type. 262 virtual llvm::Constant * 263 performAddrSpaceCast(CodeGenModule &CGM, llvm::Constant *V, unsigned SrcAddr, 264 unsigned DestAddr, llvm::Type *DestTy) const; 265 266 /// Get the syncscope used in LLVM IR. 267 virtual llvm::SyncScope::ID getLLVMSyncScopeID(SyncScope S, 268 llvm::LLVMContext &C) const; 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 } 290 }; 291 292 } // namespace CodeGen 293 } // namespace clang 294 295 #endif // LLVM_CLANG_LIB_CODEGEN_TARGETINFO_H 296