clang API Documentation

lib/CodeGen/TargetInfo.h
Go to the documentation of this file.
00001 //===---- TargetInfo.h - Encapsulate target details -------------*- C++ -*-===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // These classes wrap the information about a call or function
00011 // definition used to handle ABI compliancy.
00012 //
00013 //===----------------------------------------------------------------------===//
00014 
00015 #ifndef LLVM_CLANG_LIB_CODEGEN_TARGETINFO_H
00016 #define LLVM_CLANG_LIB_CODEGEN_TARGETINFO_H
00017 
00018 #include "CGValue.h"
00019 #include "clang/AST/Type.h"
00020 #include "clang/Basic/LLVM.h"
00021 #include "llvm/ADT/SmallString.h"
00022 #include "llvm/ADT/StringRef.h"
00023 
00024 namespace llvm {
00025 class Constant;
00026 class GlobalValue;
00027 class Type;
00028 class Value;
00029 }
00030 
00031 namespace clang {
00032 class ABIInfo;
00033 class Decl;
00034 
00035 namespace CodeGen {
00036 class CallArgList;
00037 class CodeGenModule;
00038 class CodeGenFunction;
00039 class CGFunctionInfo;
00040 }
00041 
00042 /// TargetCodeGenInfo - This class organizes various target-specific
00043 /// codegeneration issues, like target-specific attributes, builtins and so
00044 /// on.
00045 class TargetCodeGenInfo {
00046   ABIInfo *Info;
00047 
00048 public:
00049   // WARNING: Acquires the ownership of ABIInfo.
00050   TargetCodeGenInfo(ABIInfo *info = 0) : Info(info) {}
00051   virtual ~TargetCodeGenInfo();
00052 
00053   /// getABIInfo() - Returns ABI info helper for the target.
00054   const ABIInfo &getABIInfo() const { return *Info; }
00055 
00056   /// SetTargetAttributes - Provides a convenient hook to handle extra
00057   /// target-specific attributes for the given global.
00058   virtual void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
00059                                    CodeGen::CodeGenModule &M) const {}
00060 
00061   /// EmitTargetMD - Provides a convenient hook to handle extra
00062   /// target-specific metadata for the given global.
00063   virtual void emitTargetMD(const Decl *D, llvm::GlobalValue *GV,
00064                             CodeGen::CodeGenModule &M) const {}
00065 
00066   /// Determines the size of struct _Unwind_Exception on this platform,
00067   /// in 8-bit units.  The Itanium ABI defines this as:
00068   ///   struct _Unwind_Exception {
00069   ///     uint64 exception_class;
00070   ///     _Unwind_Exception_Cleanup_Fn exception_cleanup;
00071   ///     uint64 private_1;
00072   ///     uint64 private_2;
00073   ///   };
00074   virtual unsigned getSizeOfUnwindException() const;
00075 
00076   /// Controls whether __builtin_extend_pointer should sign-extend
00077   /// pointers to uint64_t or zero-extend them (the default).  Has
00078   /// no effect for targets:
00079   ///   - that have 64-bit pointers, or
00080   ///   - that cannot address through registers larger than pointers, or
00081   ///   - that implicitly ignore/truncate the top bits when addressing
00082   ///     through such registers.
00083   virtual bool extendPointerWithSExt() const { return false; }
00084 
00085   /// Determines the DWARF register number for the stack pointer, for
00086   /// exception-handling purposes.  Implements __builtin_dwarf_sp_column.
00087   ///
00088   /// Returns -1 if the operation is unsupported by this target.
00089   virtual int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const {
00090     return -1;
00091   }
00092 
00093   /// Initializes the given DWARF EH register-size table, a char*.
00094   /// Implements __builtin_init_dwarf_reg_size_table.
00095   ///
00096   /// Returns true if the operation is unsupported by this target.
00097   virtual bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
00098                                        llvm::Value *Address) const {
00099     return true;
00100   }
00101 
00102   /// Performs the code-generation required to convert a return
00103   /// address as stored by the system into the actual address of the
00104   /// next instruction that will be executed.
00105   ///
00106   /// Used by __builtin_extract_return_addr().
00107   virtual llvm::Value *decodeReturnAddress(CodeGen::CodeGenFunction &CGF,
00108                                            llvm::Value *Address) const {
00109     return Address;
00110   }
00111 
00112   /// Performs the code-generation required to convert the address
00113   /// of an instruction into a return address suitable for storage
00114   /// by the system in a return slot.
00115   ///
00116   /// Used by __builtin_frob_return_addr().
00117   virtual llvm::Value *encodeReturnAddress(CodeGen::CodeGenFunction &CGF,
00118                                            llvm::Value *Address) const {
00119     return Address;
00120   }
00121 
00122   /// Corrects the low-level LLVM type for a given constraint and "usual"
00123   /// type.
00124   ///
00125   /// \returns A pointer to a new LLVM type, possibly the same as the original
00126   /// on success; 0 on failure.
00127   virtual llvm::Type *adjustInlineAsmType(CodeGen::CodeGenFunction &CGF,
00128                                           StringRef Constraint,
00129                                           llvm::Type *Ty) const {
00130     return Ty;
00131   }
00132 
00133   /// Adds constraints and types for result registers.
00134   virtual void addReturnRegisterOutputs(
00135       CodeGen::CodeGenFunction &CGF, CodeGen::LValue ReturnValue,
00136       std::string &Constraints, std::vector<llvm::Type *> &ResultRegTypes,
00137       std::vector<llvm::Type *> &ResultTruncRegTypes,
00138       std::vector<CodeGen::LValue> &ResultRegDests, std::string &AsmString,
00139       unsigned NumOutputs) const {}
00140 
00141   /// doesReturnSlotInterfereWithArgs - Return true if the target uses an
00142   /// argument slot for an 'sret' type.
00143   virtual bool doesReturnSlotInterfereWithArgs() const { return true; }
00144 
00145   /// Retrieve the address of a function to call immediately before
00146   /// calling objc_retainAutoreleasedReturnValue.  The
00147   /// implementation of objc_autoreleaseReturnValue sniffs the
00148   /// instruction stream following its return address to decide
00149   /// whether it's a call to objc_retainAutoreleasedReturnValue.
00150   /// This can be prohibitively expensive, depending on the
00151   /// relocation model, and so on some targets it instead sniffs for
00152   /// a particular instruction sequence.  This functions returns
00153   /// that instruction sequence in inline assembly, which will be
00154   /// empty if none is required.
00155   virtual StringRef getARCRetainAutoreleasedReturnValueMarker() const {
00156     return "";
00157   }
00158 
00159   /// Return a constant used by UBSan as a signature to identify functions
00160   /// possessing type information, or 0 if the platform is unsupported.
00161   virtual llvm::Constant *
00162   getUBSanFunctionSignature(CodeGen::CodeGenModule &CGM) const {
00163     return nullptr;
00164   }
00165 
00166   /// Determine whether a call to an unprototyped functions under
00167   /// the given calling convention should use the variadic
00168   /// convention or the non-variadic convention.
00169   ///
00170   /// There's a good reason to make a platform's variadic calling
00171   /// convention be different from its non-variadic calling
00172   /// convention: the non-variadic arguments can be passed in
00173   /// registers (better for performance), and the variadic arguments
00174   /// can be passed on the stack (also better for performance).  If
00175   /// this is done, however, unprototyped functions *must* use the
00176   /// non-variadic convention, because C99 states that a call
00177   /// through an unprototyped function type must succeed if the
00178   /// function was defined with a non-variadic prototype with
00179   /// compatible parameters.  Therefore, splitting the conventions
00180   /// makes it impossible to call a variadic function through an
00181   /// unprototyped type.  Since function prototypes came out in the
00182   /// late 1970s, this is probably an acceptable trade-off.
00183   /// Nonetheless, not all platforms are willing to make it, and in
00184   /// particularly x86-64 bends over backwards to make the
00185   /// conventions compatible.
00186   ///
00187   /// The default is false.  This is correct whenever:
00188   ///   - the conventions are exactly the same, because it does not
00189   ///     matter and the resulting IR will be somewhat prettier in
00190   ///     certain cases; or
00191   ///   - the conventions are substantively different in how they pass
00192   ///     arguments, because in this case using the variadic convention
00193   ///     will lead to C99 violations.
00194   ///
00195   /// However, some platforms make the conventions identical except
00196   /// for passing additional out-of-band information to a variadic
00197   /// function: for example, x86-64 passes the number of SSE
00198   /// arguments in %al.  On these platforms, it is desirable to
00199   /// call unprototyped functions using the variadic convention so
00200   /// that unprototyped calls to varargs functions still succeed.
00201   ///
00202   /// Relatedly, platforms which pass the fixed arguments to this:
00203   ///   A foo(B, C, D);
00204   /// differently than they would pass them to this:
00205   ///   A foo(B, C, D, ...);
00206   /// may need to adjust the debugger-support code in Sema to do the
00207   /// right thing when calling a function with no know signature.
00208   virtual bool isNoProtoCallVariadic(const CodeGen::CallArgList &args,
00209                                      const FunctionNoProtoType *fnType) const;
00210 
00211   /// Gets the linker options necessary to link a dependent library on this
00212   /// platform.
00213   virtual void getDependentLibraryOption(llvm::StringRef Lib,
00214                                          llvm::SmallString<24> &Opt) const;
00215 
00216   /// Gets the linker options necessary to detect object file mismatches on
00217   /// this platform.
00218   virtual void getDetectMismatchOption(llvm::StringRef Name,
00219                                        llvm::StringRef Value,
00220                                        llvm::SmallString<32> &Opt) const {}
00221 
00222   /// Gets the target-specific default alignment used when an 'aligned' clause
00223   /// is used with a 'simd' OpenMP directive without specifying a specific
00224   /// alignment.
00225   virtual unsigned getOpenMPSimdDefaultAlignment(QualType Type) const {
00226     return 0;
00227   }
00228 };
00229 }
00230 
00231 #endif