Home | History | Annotate | Download | only in CodeGen
      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 CLANG_CODEGEN_TARGETINFO_H
     16 #define CLANG_CODEGEN_TARGETINFO_H
     17 
     18 #include "clang/AST/Type.h"
     19 #include "clang/Basic/LLVM.h"
     20 #include "llvm/ADT/SmallString.h"
     21 #include "llvm/ADT/StringRef.h"
     22 
     23 namespace llvm {
     24 class Constant;
     25 class GlobalValue;
     26 class Type;
     27 class Value;
     28 }
     29 
     30 namespace clang {
     31 class ABIInfo;
     32 class Decl;
     33 
     34 namespace CodeGen {
     35 class CallArgList;
     36 class CodeGenModule;
     37 class CodeGenFunction;
     38 class CGFunctionInfo;
     39 }
     40 
     41 /// TargetCodeGenInfo - This class organizes various target-specific
     42 /// codegeneration issues, like target-specific attributes, builtins and so
     43 /// on.
     44 class TargetCodeGenInfo {
     45   ABIInfo *Info;
     46 
     47 public:
     48   // WARNING: Acquires the ownership of ABIInfo.
     49   TargetCodeGenInfo(ABIInfo *info = 0) : Info(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   /// doesReturnSlotInterfereWithArgs - Return true if the target uses an
    133   /// argument slot for an 'sret' type.
    134   virtual bool doesReturnSlotInterfereWithArgs() const { return true; }
    135 
    136   /// Retrieve the address of a function to call immediately before
    137   /// calling objc_retainAutoreleasedReturnValue.  The
    138   /// implementation of objc_autoreleaseReturnValue sniffs the
    139   /// instruction stream following its return address to decide
    140   /// whether it's a call to objc_retainAutoreleasedReturnValue.
    141   /// This can be prohibitively expensive, depending on the
    142   /// relocation model, and so on some targets it instead sniffs for
    143   /// a particular instruction sequence.  This functions returns
    144   /// that instruction sequence in inline assembly, which will be
    145   /// empty if none is required.
    146   virtual StringRef getARCRetainAutoreleasedReturnValueMarker() const {
    147     return "";
    148   }
    149 
    150   /// Return a constant used by UBSan as a signature to identify functions
    151   /// possessing type information, or 0 if the platform is unsupported.
    152   virtual llvm::Constant *
    153   getUBSanFunctionSignature(CodeGen::CodeGenModule &CGM) const {
    154     return nullptr;
    155   }
    156 
    157   /// Determine whether a call to an unprototyped functions under
    158   /// the given calling convention should use the variadic
    159   /// convention or the non-variadic convention.
    160   ///
    161   /// There's a good reason to make a platform's variadic calling
    162   /// convention be different from its non-variadic calling
    163   /// convention: the non-variadic arguments can be passed in
    164   /// registers (better for performance), and the variadic arguments
    165   /// can be passed on the stack (also better for performance).  If
    166   /// this is done, however, unprototyped functions *must* use the
    167   /// non-variadic convention, because C99 states that a call
    168   /// through an unprototyped function type must succeed if the
    169   /// function was defined with a non-variadic prototype with
    170   /// compatible parameters.  Therefore, splitting the conventions
    171   /// makes it impossible to call a variadic function through an
    172   /// unprototyped type.  Since function prototypes came out in the
    173   /// late 1970s, this is probably an acceptable trade-off.
    174   /// Nonetheless, not all platforms are willing to make it, and in
    175   /// particularly x86-64 bends over backwards to make the
    176   /// conventions compatible.
    177   ///
    178   /// The default is false.  This is correct whenever:
    179   ///   - the conventions are exactly the same, because it does not
    180   ///     matter and the resulting IR will be somewhat prettier in
    181   ///     certain cases; or
    182   ///   - the conventions are substantively different in how they pass
    183   ///     arguments, because in this case using the variadic convention
    184   ///     will lead to C99 violations.
    185   ///
    186   /// However, some platforms make the conventions identical except
    187   /// for passing additional out-of-band information to a variadic
    188   /// function: for example, x86-64 passes the number of SSE
    189   /// arguments in %al.  On these platforms, it is desirable to
    190   /// call unprototyped functions using the variadic convention so
    191   /// that unprototyped calls to varargs functions still succeed.
    192   ///
    193   /// Relatedly, platforms which pass the fixed arguments to this:
    194   ///   A foo(B, C, D);
    195   /// differently than they would pass them to this:
    196   ///   A foo(B, C, D, ...);
    197   /// may need to adjust the debugger-support code in Sema to do the
    198   /// right thing when calling a function with no know signature.
    199   virtual bool isNoProtoCallVariadic(const CodeGen::CallArgList &args,
    200                                      const FunctionNoProtoType *fnType) const;
    201 
    202   /// Gets the linker options necessary to link a dependent library on this
    203   /// platform.
    204   virtual void getDependentLibraryOption(llvm::StringRef Lib,
    205                                          llvm::SmallString<24> &Opt) const;
    206 
    207   /// Gets the linker options necessary to detect object file mismatches on
    208   /// this platform.
    209   virtual void getDetectMismatchOption(llvm::StringRef Name,
    210                                        llvm::StringRef Value,
    211                                        llvm::SmallString<32> &Opt) const {}
    212 };
    213 }
    214 
    215 #endif // CLANG_CODEGEN_TARGETINFO_H
    216