Home | History | Annotate | Download | only in Target
      1 //===-- llvm/Target/TargetMachine.h - Target Information --------*- 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 // This file defines the TargetMachine and LLVMTargetMachine classes.
     11 //
     12 //===----------------------------------------------------------------------===//
     13 
     14 #ifndef LLVM_TARGET_TARGETMACHINE_H
     15 #define LLVM_TARGET_TARGETMACHINE_H
     16 
     17 #include "llvm/ADT/StringRef.h"
     18 #include "llvm/IR/DataLayout.h"
     19 #include "llvm/Pass.h"
     20 #include "llvm/Support/CodeGen.h"
     21 #include "llvm/Target/TargetOptions.h"
     22 #include <cassert>
     23 #include <string>
     24 
     25 namespace llvm {
     26 
     27 class InstrItineraryData;
     28 class GlobalValue;
     29 class Mangler;
     30 class MCAsmInfo;
     31 class MCCodeGenInfo;
     32 class MCContext;
     33 class MCInstrInfo;
     34 class MCRegisterInfo;
     35 class MCSubtargetInfo;
     36 class MCSymbol;
     37 class Target;
     38 class DataLayout;
     39 class TargetLibraryInfo;
     40 class TargetFrameLowering;
     41 class TargetIRAnalysis;
     42 class TargetIntrinsicInfo;
     43 class TargetLowering;
     44 class TargetPassConfig;
     45 class TargetRegisterInfo;
     46 class TargetSelectionDAGInfo;
     47 class TargetSubtargetInfo;
     48 class TargetTransformInfo;
     49 class formatted_raw_ostream;
     50 class raw_ostream;
     51 class raw_pwrite_stream;
     52 class TargetLoweringObjectFile;
     53 
     54 // The old pass manager infrastructure is hidden in a legacy namespace now.
     55 namespace legacy {
     56 class PassManagerBase;
     57 }
     58 using legacy::PassManagerBase;
     59 
     60 //===----------------------------------------------------------------------===//
     61 ///
     62 /// Primary interface to the complete machine description for the target
     63 /// machine.  All target-specific information should be accessible through this
     64 /// interface.
     65 ///
     66 class TargetMachine {
     67   TargetMachine(const TargetMachine &) = delete;
     68   void operator=(const TargetMachine &) = delete;
     69 protected: // Can only create subclasses.
     70   TargetMachine(const Target &T, StringRef DataLayoutString,
     71                 StringRef TargetTriple, StringRef CPU, StringRef FS,
     72                 const TargetOptions &Options);
     73 
     74   /// The Target that this machine was created for.
     75   const Target &TheTarget;
     76 
     77   /// For ABI type size and alignment.
     78   const DataLayout DL;
     79 
     80   /// Triple string, CPU name, and target feature strings the TargetMachine
     81   /// instance is created with.
     82   std::string TargetTriple;
     83   std::string TargetCPU;
     84   std::string TargetFS;
     85 
     86   /// Low level target information such as relocation model. Non-const to
     87   /// allow resetting optimization level per-function.
     88   MCCodeGenInfo *CodeGenInfo;
     89 
     90   /// Contains target specific asm information.
     91   const MCAsmInfo *AsmInfo;
     92 
     93   const MCRegisterInfo *MRI;
     94   const MCInstrInfo *MII;
     95   const MCSubtargetInfo *STI;
     96 
     97   unsigned RequireStructuredCFG : 1;
     98 
     99 public:
    100   mutable TargetOptions Options;
    101 
    102   virtual ~TargetMachine();
    103 
    104   const Target &getTarget() const { return TheTarget; }
    105 
    106   StringRef getTargetTriple() const { return TargetTriple; }
    107   StringRef getTargetCPU() const { return TargetCPU; }
    108   StringRef getTargetFeatureString() const { return TargetFS; }
    109 
    110   /// Virtual method implemented by subclasses that returns a reference to that
    111   /// target's TargetSubtargetInfo-derived member variable.
    112   virtual const TargetSubtargetInfo *getSubtargetImpl(const Function &) const {
    113     return nullptr;
    114   }
    115   virtual TargetLoweringObjectFile *getObjFileLowering() const {
    116     return nullptr;
    117   }
    118 
    119   /// This method returns a pointer to the specified type of
    120   /// TargetSubtargetInfo.  In debug builds, it verifies that the object being
    121   /// returned is of the correct type.
    122   template <typename STC> const STC &getSubtarget(const Function &F) const {
    123     return *static_cast<const STC*>(getSubtargetImpl(F));
    124   }
    125 
    126   /// This method returns a pointer to the DataLayout for the target. It should
    127   /// be unchanging for every subtarget.
    128   const DataLayout *getDataLayout() const { return &DL; }
    129 
    130   /// \brief Reset the target options based on the function's attributes.
    131   // FIXME: Remove TargetOptions that affect per-function code generation
    132   // from TargetMachine.
    133   void resetTargetOptions(const Function &F) const;
    134 
    135   /// Return target specific asm information.
    136   const MCAsmInfo *getMCAsmInfo() const { return AsmInfo; }
    137 
    138   const MCRegisterInfo *getMCRegisterInfo() const { return MRI; }
    139   const MCInstrInfo *getMCInstrInfo() const { return MII; }
    140   const MCSubtargetInfo *getMCSubtargetInfo() const { return STI; }
    141 
    142   /// If intrinsic information is available, return it.  If not, return null.
    143   virtual const TargetIntrinsicInfo *getIntrinsicInfo() const {
    144     return nullptr;
    145   }
    146 
    147   bool requiresStructuredCFG() const { return RequireStructuredCFG; }
    148   void setRequiresStructuredCFG(bool Value) { RequireStructuredCFG = Value; }
    149 
    150   /// Returns the code generation relocation model. The choices are static, PIC,
    151   /// and dynamic-no-pic, and target default.
    152   Reloc::Model getRelocationModel() const;
    153 
    154   /// Returns the code model. The choices are small, kernel, medium, large, and
    155   /// target default.
    156   CodeModel::Model getCodeModel() const;
    157 
    158   /// Returns the TLS model which should be used for the given global variable.
    159   TLSModel::Model getTLSModel(const GlobalValue *GV) const;
    160 
    161   /// Returns the optimization level: None, Less, Default, or Aggressive.
    162   CodeGenOpt::Level getOptLevel() const;
    163 
    164   /// \brief Overrides the optimization level.
    165   void setOptLevel(CodeGenOpt::Level Level) const;
    166 
    167   void setFastISel(bool Enable) { Options.EnableFastISel = Enable; }
    168 
    169   bool shouldPrintMachineCode() const { return Options.PrintMachineCode; }
    170 
    171   /// Returns the default value of asm verbosity.
    172   ///
    173   bool getAsmVerbosityDefault() const {
    174     return Options.MCOptions.AsmVerbose;
    175   }
    176 
    177   bool getUniqueSectionNames() const { return Options.UniqueSectionNames; }
    178 
    179   /// Return true if data objects should be emitted into their own section,
    180   /// corresponds to -fdata-sections.
    181   bool getDataSections() const {
    182     return Options.DataSections;
    183   }
    184 
    185   /// Return true if functions should be emitted into their own section,
    186   /// corresponding to -ffunction-sections.
    187   bool getFunctionSections() const {
    188     return Options.FunctionSections;
    189   }
    190 
    191   /// \brief Get a \c TargetIRAnalysis appropriate for the target.
    192   ///
    193   /// This is used to construct the new pass manager's target IR analysis pass,
    194   /// set up appropriately for this target machine. Even the old pass manager
    195   /// uses this to answer queries about the IR.
    196   virtual TargetIRAnalysis getTargetIRAnalysis();
    197 
    198   /// These enums are meant to be passed into addPassesToEmitFile to indicate
    199   /// what type of file to emit, and returned by it to indicate what type of
    200   /// file could actually be made.
    201   enum CodeGenFileType {
    202     CGFT_AssemblyFile,
    203     CGFT_ObjectFile,
    204     CGFT_Null         // Do not emit any output.
    205   };
    206 
    207   /// Add passes to the specified pass manager to get the specified file
    208   /// emitted.  Typically this will involve several steps of code generation.
    209   /// This method should return true if emission of this file type is not
    210   /// supported, or false on success.
    211   virtual bool addPassesToEmitFile(PassManagerBase &, raw_pwrite_stream &,
    212                                    CodeGenFileType,
    213                                    bool /*DisableVerify*/ = true,
    214                                    AnalysisID /*StartAfter*/ = nullptr,
    215                                    AnalysisID /*StopAfter*/ = nullptr) {
    216     return true;
    217   }
    218 
    219   /// Add passes to the specified pass manager to get machine code emitted with
    220   /// the MCJIT. This method returns true if machine code is not supported. It
    221   /// fills the MCContext Ctx pointer which can be used to build custom
    222   /// MCStreamer.
    223   ///
    224   virtual bool addPassesToEmitMC(PassManagerBase &, MCContext *&,
    225                                  raw_pwrite_stream &,
    226                                  bool /*DisableVerify*/ = true) {
    227     return true;
    228   }
    229 
    230   void getNameWithPrefix(SmallVectorImpl<char> &Name, const GlobalValue *GV,
    231                          Mangler &Mang, bool MayAlwaysUsePrivate = false) const;
    232   MCSymbol *getSymbol(const GlobalValue *GV, Mangler &Mang) const;
    233 };
    234 
    235 /// This class describes a target machine that is implemented with the LLVM
    236 /// target-independent code generator.
    237 ///
    238 class LLVMTargetMachine : public TargetMachine {
    239 protected: // Can only create subclasses.
    240   LLVMTargetMachine(const Target &T, StringRef DataLayoutString,
    241                     StringRef TargetTriple, StringRef CPU, StringRef FS,
    242                     TargetOptions Options, Reloc::Model RM, CodeModel::Model CM,
    243                     CodeGenOpt::Level OL);
    244 
    245   void initAsmInfo();
    246 public:
    247   /// \brief Get a TargetIRAnalysis implementation for the target.
    248   ///
    249   /// This analysis will produce a TTI result which uses the common code
    250   /// generator to answer queries about the IR.
    251   TargetIRAnalysis getTargetIRAnalysis() override;
    252 
    253   /// Create a pass configuration object to be used by addPassToEmitX methods
    254   /// for generating a pipeline of CodeGen passes.
    255   virtual TargetPassConfig *createPassConfig(PassManagerBase &PM);
    256 
    257   /// Add passes to the specified pass manager to get the specified file
    258   /// emitted.  Typically this will involve several steps of code generation.
    259   bool addPassesToEmitFile(PassManagerBase &PM, raw_pwrite_stream &Out,
    260                            CodeGenFileType FileType, bool DisableVerify = true,
    261                            AnalysisID StartAfter = nullptr,
    262                            AnalysisID StopAfter = nullptr) override;
    263 
    264   /// Add passes to the specified pass manager to get machine code emitted with
    265   /// the MCJIT. This method returns true if machine code is not supported. It
    266   /// fills the MCContext Ctx pointer which can be used to build custom
    267   /// MCStreamer.
    268   bool addPassesToEmitMC(PassManagerBase &PM, MCContext *&Ctx,
    269                          raw_pwrite_stream &OS,
    270                          bool DisableVerify = true) override;
    271 };
    272 
    273 } // End llvm namespace
    274 
    275 #endif
    276