LLVM API Documentation

Target/TargetMachine.h
Go to the documentation of this file.
00001 //===-- llvm/Target/TargetMachine.h - Target Information --------*- 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 // This file defines the TargetMachine and LLVMTargetMachine classes.
00011 //
00012 //===----------------------------------------------------------------------===//
00013 
00014 #ifndef LLVM_TARGET_TARGETMACHINE_H
00015 #define LLVM_TARGET_TARGETMACHINE_H
00016 
00017 #include "llvm/ADT/StringRef.h"
00018 #include "llvm/Pass.h"
00019 #include "llvm/Support/CodeGen.h"
00020 #include "llvm/Target/TargetOptions.h"
00021 #include <cassert>
00022 #include <string>
00023 
00024 namespace llvm {
00025 
00026 class InstrItineraryData;
00027 class JITCodeEmitter;
00028 class GlobalValue;
00029 class MCAsmInfo;
00030 class MCCodeGenInfo;
00031 class MCContext;
00032 class PassManagerBase;
00033 class Target;
00034 class DataLayout;
00035 class TargetLibraryInfo;
00036 class TargetFrameLowering;
00037 class TargetInstrInfo;
00038 class TargetIntrinsicInfo;
00039 class TargetJITInfo;
00040 class TargetLowering;
00041 class TargetPassConfig;
00042 class TargetRegisterInfo;
00043 class TargetSelectionDAGInfo;
00044 class TargetSubtargetInfo;
00045 class ScalarTargetTransformInfo;
00046 class VectorTargetTransformInfo;
00047 class formatted_raw_ostream;
00048 class raw_ostream;
00049 
00050 //===----------------------------------------------------------------------===//
00051 ///
00052 /// TargetMachine - Primary interface to the complete machine description for
00053 /// the target machine.  All target-specific information should be accessible
00054 /// through this interface.
00055 ///
00056 class TargetMachine {
00057   TargetMachine(const TargetMachine &) LLVM_DELETED_FUNCTION;
00058   void operator=(const TargetMachine &) LLVM_DELETED_FUNCTION;
00059 protected: // Can only create subclasses.
00060   TargetMachine(const Target &T, StringRef TargetTriple,
00061                 StringRef CPU, StringRef FS, const TargetOptions &Options);
00062 
00063   /// TheTarget - The Target that this machine was created for.
00064   const Target &TheTarget;
00065 
00066   /// TargetTriple, TargetCPU, TargetFS - Triple string, CPU name, and target
00067   /// feature strings the TargetMachine instance is created with.
00068   std::string TargetTriple;
00069   std::string TargetCPU;
00070   std::string TargetFS;
00071 
00072   /// CodeGenInfo - Low level target information such as relocation model.
00073   const MCCodeGenInfo *CodeGenInfo;
00074 
00075   /// AsmInfo - Contains target specific asm information.
00076   ///
00077   const MCAsmInfo *AsmInfo;
00078 
00079   unsigned MCRelaxAll : 1;
00080   unsigned MCNoExecStack : 1;
00081   unsigned MCSaveTempLabels : 1;
00082   unsigned MCUseLoc : 1;
00083   unsigned MCUseCFI : 1;
00084   unsigned MCUseDwarfDirectory : 1;
00085 
00086 public:
00087   virtual ~TargetMachine();
00088 
00089   const Target &getTarget() const { return TheTarget; }
00090 
00091   const StringRef getTargetTriple() const { return TargetTriple; }
00092   const StringRef getTargetCPU() const { return TargetCPU; }
00093   const StringRef getTargetFeatureString() const { return TargetFS; }
00094 
00095   /// getSubtargetImpl - virtual method implemented by subclasses that returns
00096   /// a reference to that target's TargetSubtargetInfo-derived member variable.
00097   virtual const TargetSubtargetInfo *getSubtargetImpl() const { return 0; }
00098 
00099   mutable TargetOptions Options;
00100 
00101   /// \brief Reset the target options based on the function's attributes.
00102   void resetTargetOptions(const MachineFunction *MF) const;
00103 
00104   // Interfaces to the major aspects of target machine information:
00105   // -- Instruction opcode and operand information
00106   // -- Pipelines and scheduling information
00107   // -- Stack frame information
00108   // -- Selection DAG lowering information
00109   //
00110   virtual const TargetInstrInfo         *getInstrInfo() const { return 0; }
00111   virtual const TargetFrameLowering *getFrameLowering() const { return 0; }
00112   virtual const TargetLowering    *getTargetLowering() const { return 0; }
00113   virtual const TargetSelectionDAGInfo *getSelectionDAGInfo() const{ return 0; }
00114   virtual const DataLayout             *getDataLayout() const { return 0; }
00115 
00116   /// getMCAsmInfo - Return target specific asm information.
00117   ///
00118   const MCAsmInfo *getMCAsmInfo() const { return AsmInfo; }
00119 
00120   /// getSubtarget - This method returns a pointer to the specified type of
00121   /// TargetSubtargetInfo.  In debug builds, it verifies that the object being
00122   /// returned is of the correct type.
00123   template<typename STC> const STC &getSubtarget() const {
00124     return *static_cast<const STC*>(getSubtargetImpl());
00125   }
00126 
00127   /// getRegisterInfo - If register information is available, return it.  If
00128   /// not, return null.  This is kept separate from RegInfo until RegInfo has
00129   /// details of graph coloring register allocation removed from it.
00130   ///
00131   virtual const TargetRegisterInfo *getRegisterInfo() const { return 0; }
00132 
00133   /// getIntrinsicInfo - If intrinsic information is available, return it.  If
00134   /// not, return null.
00135   ///
00136   virtual const TargetIntrinsicInfo *getIntrinsicInfo() const { return 0; }
00137 
00138   /// getJITInfo - If this target supports a JIT, return information for it,
00139   /// otherwise return null.
00140   ///
00141   virtual TargetJITInfo *getJITInfo() { return 0; }
00142 
00143   /// getInstrItineraryData - Returns instruction itinerary data for the target
00144   /// or specific subtarget.
00145   ///
00146   virtual const InstrItineraryData *getInstrItineraryData() const {
00147     return 0;
00148   }
00149 
00150   /// hasMCRelaxAll - Check whether all machine code instructions should be
00151   /// relaxed.
00152   bool hasMCRelaxAll() const { return MCRelaxAll; }
00153 
00154   /// setMCRelaxAll - Set whether all machine code instructions should be
00155   /// relaxed.
00156   void setMCRelaxAll(bool Value) { MCRelaxAll = Value; }
00157 
00158   /// hasMCSaveTempLabels - Check whether temporary labels will be preserved
00159   /// (i.e., not treated as temporary).
00160   bool hasMCSaveTempLabels() const { return MCSaveTempLabels; }
00161 
00162   /// setMCSaveTempLabels - Set whether temporary labels will be preserved
00163   /// (i.e., not treated as temporary).
00164   void setMCSaveTempLabels(bool Value) { MCSaveTempLabels = Value; }
00165 
00166   /// hasMCNoExecStack - Check whether an executable stack is not needed.
00167   bool hasMCNoExecStack() const { return MCNoExecStack; }
00168 
00169   /// setMCNoExecStack - Set whether an executabel stack is not needed.
00170   void setMCNoExecStack(bool Value) { MCNoExecStack = Value; }
00171 
00172   /// hasMCUseLoc - Check whether we should use dwarf's .loc directive.
00173   bool hasMCUseLoc() const { return MCUseLoc; }
00174 
00175   /// setMCUseLoc - Set whether all we should use dwarf's .loc directive.
00176   void setMCUseLoc(bool Value) { MCUseLoc = Value; }
00177 
00178   /// hasMCUseCFI - Check whether we should use dwarf's .cfi_* directives.
00179   bool hasMCUseCFI() const { return MCUseCFI; }
00180 
00181   /// setMCUseCFI - Set whether all we should use dwarf's .cfi_* directives.
00182   void setMCUseCFI(bool Value) { MCUseCFI = Value; }
00183 
00184   /// hasMCUseDwarfDirectory - Check whether we should use .file directives with
00185   /// explicit directories.
00186   bool hasMCUseDwarfDirectory() const { return MCUseDwarfDirectory; }
00187 
00188   /// setMCUseDwarfDirectory - Set whether all we should use .file directives
00189   /// with explicit directories.
00190   void setMCUseDwarfDirectory(bool Value) { MCUseDwarfDirectory = Value; }
00191 
00192   /// getRelocationModel - Returns the code generation relocation model. The
00193   /// choices are static, PIC, and dynamic-no-pic, and target default.
00194   Reloc::Model getRelocationModel() const;
00195 
00196   /// getCodeModel - Returns the code model. The choices are small, kernel,
00197   /// medium, large, and target default.
00198   CodeModel::Model getCodeModel() const;
00199 
00200   /// getTLSModel - Returns the TLS model which should be used for the given
00201   /// global variable.
00202   TLSModel::Model getTLSModel(const GlobalValue *GV) const;
00203 
00204   /// getOptLevel - Returns the optimization level: None, Less,
00205   /// Default, or Aggressive.
00206   CodeGenOpt::Level getOptLevel() const;
00207 
00208   void setFastISel(bool Enable) { Options.EnableFastISel = Enable; }
00209 
00210   bool shouldPrintMachineCode() const { return Options.PrintMachineCode; }
00211 
00212   /// getAsmVerbosityDefault - Returns the default value of asm verbosity.
00213   ///
00214   static bool getAsmVerbosityDefault();
00215 
00216   /// setAsmVerbosityDefault - Set the default value of asm verbosity. Default
00217   /// is false.
00218   static void setAsmVerbosityDefault(bool);
00219 
00220   /// getDataSections - Return true if data objects should be emitted into their
00221   /// own section, corresponds to -fdata-sections.
00222   static bool getDataSections();
00223 
00224   /// getFunctionSections - Return true if functions should be emitted into
00225   /// their own section, corresponding to -ffunction-sections.
00226   static bool getFunctionSections();
00227 
00228   /// setDataSections - Set if the data are emit into separate sections.
00229   static void setDataSections(bool);
00230 
00231   /// setFunctionSections - Set if the functions are emit into separate
00232   /// sections.
00233   static void setFunctionSections(bool);
00234 
00235   /// \brief Register analysis passes for this target with a pass manager.
00236   virtual void addAnalysisPasses(PassManagerBase &) {}
00237 
00238   /// CodeGenFileType - These enums are meant to be passed into
00239   /// addPassesToEmitFile to indicate what type of file to emit, and returned by
00240   /// it to indicate what type of file could actually be made.
00241   enum CodeGenFileType {
00242     CGFT_AssemblyFile,
00243     CGFT_ObjectFile,
00244     CGFT_Null         // Do not emit any output.
00245   };
00246 
00247   /// addPassesToEmitFile - Add passes to the specified pass manager to get the
00248   /// specified file emitted.  Typically this will involve several steps of code
00249   /// generation.  This method should return true if emission of this file type
00250   /// is not supported, or false on success.
00251   virtual bool addPassesToEmitFile(PassManagerBase &,
00252                                    formatted_raw_ostream &,
00253                                    CodeGenFileType,
00254                                    bool /*DisableVerify*/ = true,
00255                                    AnalysisID StartAfter = 0,
00256                                    AnalysisID StopAfter = 0) {
00257     return true;
00258   }
00259 
00260   /// addPassesToEmitMachineCode - Add passes to the specified pass manager to
00261   /// get machine code emitted.  This uses a JITCodeEmitter object to handle
00262   /// actually outputting the machine code and resolving things like the address
00263   /// of functions.  This method returns true if machine code emission is
00264   /// not supported.
00265   ///
00266   virtual bool addPassesToEmitMachineCode(PassManagerBase &,
00267                                           JITCodeEmitter &,
00268                                           bool /*DisableVerify*/ = true) {
00269     return true;
00270   }
00271 
00272   /// addPassesToEmitMC - Add passes to the specified pass manager to get
00273   /// machine code emitted with the MCJIT. This method returns true if machine
00274   /// code is not supported. It fills the MCContext Ctx pointer which can be
00275   /// used to build custom MCStreamer.
00276   ///
00277   virtual bool addPassesToEmitMC(PassManagerBase &,
00278                                  MCContext *&,
00279                                  raw_ostream &,
00280                                  bool /*DisableVerify*/ = true) {
00281     return true;
00282   }
00283 };
00284 
00285 /// LLVMTargetMachine - This class describes a target machine that is
00286 /// implemented with the LLVM target-independent code generator.
00287 ///
00288 class LLVMTargetMachine : public TargetMachine {
00289 protected: // Can only create subclasses.
00290   LLVMTargetMachine(const Target &T, StringRef TargetTriple,
00291                     StringRef CPU, StringRef FS, TargetOptions Options,
00292                     Reloc::Model RM, CodeModel::Model CM,
00293                     CodeGenOpt::Level OL);
00294 
00295   void initAsmInfo();
00296 public:
00297   /// \brief Register analysis passes for this target with a pass manager.
00298   ///
00299   /// This registers target independent analysis passes.
00300   virtual void addAnalysisPasses(PassManagerBase &PM);
00301 
00302   /// createPassConfig - Create a pass configuration object to be used by
00303   /// addPassToEmitX methods for generating a pipeline of CodeGen passes.
00304   virtual TargetPassConfig *createPassConfig(PassManagerBase &PM);
00305 
00306   /// addPassesToEmitFile - Add passes to the specified pass manager to get the
00307   /// specified file emitted.  Typically this will involve several steps of code
00308   /// generation.
00309   virtual bool addPassesToEmitFile(PassManagerBase &PM,
00310                                    formatted_raw_ostream &Out,
00311                                    CodeGenFileType FileType,
00312                                    bool DisableVerify = true,
00313                                    AnalysisID StartAfter = 0,
00314                                    AnalysisID StopAfter = 0);
00315 
00316   /// addPassesToEmitMachineCode - Add passes to the specified pass manager to
00317   /// get machine code emitted.  This uses a JITCodeEmitter object to handle
00318   /// actually outputting the machine code and resolving things like the address
00319   /// of functions.  This method returns true if machine code emission is
00320   /// not supported.
00321   ///
00322   virtual bool addPassesToEmitMachineCode(PassManagerBase &PM,
00323                                           JITCodeEmitter &MCE,
00324                                           bool DisableVerify = true);
00325 
00326   /// addPassesToEmitMC - Add passes to the specified pass manager to get
00327   /// machine code emitted with the MCJIT. This method returns true if machine
00328   /// code is not supported. It fills the MCContext Ctx pointer which can be
00329   /// used to build custom MCStreamer.
00330   ///
00331   virtual bool addPassesToEmitMC(PassManagerBase &PM,
00332                                  MCContext *&Ctx,
00333                                  raw_ostream &OS,
00334                                  bool DisableVerify = true);
00335 
00336   /// addCodeEmitter - This pass should be overridden by the target to add a
00337   /// code emitter, if supported.  If this is not supported, 'true' should be
00338   /// returned.
00339   virtual bool addCodeEmitter(PassManagerBase &,
00340                               JITCodeEmitter &) {
00341     return true;
00342   }
00343 };
00344 
00345 } // End llvm namespace
00346 
00347 #endif