LLVM API Documentation
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 GlobalValue; 00028 class Mangler; 00029 class MCAsmInfo; 00030 class MCCodeGenInfo; 00031 class MCContext; 00032 class MCSymbol; 00033 class Target; 00034 class DataLayout; 00035 class TargetLibraryInfo; 00036 class TargetFrameLowering; 00037 class TargetIntrinsicInfo; 00038 class TargetLowering; 00039 class TargetPassConfig; 00040 class TargetRegisterInfo; 00041 class TargetSelectionDAGInfo; 00042 class TargetSubtargetInfo; 00043 class ScalarTargetTransformInfo; 00044 class VectorTargetTransformInfo; 00045 class formatted_raw_ostream; 00046 class raw_ostream; 00047 00048 // The old pass manager infrastructure is hidden in a legacy namespace now. 00049 namespace legacy { 00050 class PassManagerBase; 00051 } 00052 using legacy::PassManagerBase; 00053 00054 //===----------------------------------------------------------------------===// 00055 /// 00056 /// TargetMachine - Primary interface to the complete machine description for 00057 /// the target machine. All target-specific information should be accessible 00058 /// through this interface. 00059 /// 00060 class TargetMachine { 00061 TargetMachine(const TargetMachine &) LLVM_DELETED_FUNCTION; 00062 void operator=(const TargetMachine &) LLVM_DELETED_FUNCTION; 00063 protected: // Can only create subclasses. 00064 TargetMachine(const Target &T, StringRef TargetTriple, 00065 StringRef CPU, StringRef FS, const TargetOptions &Options); 00066 00067 /// TheTarget - The Target that this machine was created for. 00068 const Target &TheTarget; 00069 00070 /// TargetTriple, TargetCPU, TargetFS - Triple string, CPU name, and target 00071 /// feature strings the TargetMachine instance is created with. 00072 std::string TargetTriple; 00073 std::string TargetCPU; 00074 std::string TargetFS; 00075 00076 /// CodeGenInfo - Low level target information such as relocation model. 00077 /// Non-const to allow resetting optimization level per-function. 00078 MCCodeGenInfo *CodeGenInfo; 00079 00080 /// AsmInfo - Contains target specific asm information. 00081 /// 00082 const MCAsmInfo *AsmInfo; 00083 00084 unsigned RequireStructuredCFG : 1; 00085 00086 public: 00087 mutable TargetOptions Options; 00088 00089 virtual ~TargetMachine(); 00090 00091 const Target &getTarget() const { return TheTarget; } 00092 00093 StringRef getTargetTriple() const { return TargetTriple; } 00094 StringRef getTargetCPU() const { return TargetCPU; } 00095 StringRef getTargetFeatureString() const { return TargetFS; } 00096 00097 /// getSubtargetImpl - virtual method implemented by subclasses that returns 00098 /// a reference to that target's TargetSubtargetInfo-derived member variable. 00099 virtual const TargetSubtargetInfo *getSubtargetImpl() const { 00100 return nullptr; 00101 } 00102 virtual const TargetSubtargetInfo *getSubtargetImpl(const Function *) const { 00103 return getSubtargetImpl(); 00104 } 00105 00106 /// getSubtarget - This method returns a pointer to the specified type of 00107 /// TargetSubtargetInfo. In debug builds, it verifies that the object being 00108 /// returned is of the correct type. 00109 template<typename STC> const STC &getSubtarget() const { 00110 return *static_cast<const STC*>(getSubtargetImpl()); 00111 } 00112 template <typename STC> const STC &getSubtarget(const Function *) const { 00113 return *static_cast<const STC*>(getSubtargetImpl()); 00114 } 00115 00116 /// \brief Reset the target options based on the function's attributes. 00117 void resetTargetOptions(const MachineFunction *MF) const; 00118 00119 /// getMCAsmInfo - Return target specific asm information. 00120 /// 00121 const MCAsmInfo *getMCAsmInfo() const { return AsmInfo; } 00122 00123 /// getIntrinsicInfo - If intrinsic information is available, return it. If 00124 /// not, return null. 00125 /// 00126 virtual const TargetIntrinsicInfo *getIntrinsicInfo() const { 00127 return nullptr; 00128 } 00129 00130 bool requiresStructuredCFG() const { return RequireStructuredCFG; } 00131 void setRequiresStructuredCFG(bool Value) { RequireStructuredCFG = Value; } 00132 00133 /// getRelocationModel - Returns the code generation relocation model. The 00134 /// choices are static, PIC, and dynamic-no-pic, and target default. 00135 Reloc::Model getRelocationModel() const; 00136 00137 /// getCodeModel - Returns the code model. The choices are small, kernel, 00138 /// medium, large, and target default. 00139 CodeModel::Model getCodeModel() const; 00140 00141 /// getTLSModel - Returns the TLS model which should be used for the given 00142 /// global variable. 00143 TLSModel::Model getTLSModel(const GlobalValue *GV) const; 00144 00145 /// getOptLevel - Returns the optimization level: None, Less, 00146 /// Default, or Aggressive. 00147 CodeGenOpt::Level getOptLevel() const; 00148 00149 /// \brief Overrides the optimization level. 00150 void setOptLevel(CodeGenOpt::Level Level) const; 00151 00152 void setFastISel(bool Enable) { Options.EnableFastISel = Enable; } 00153 00154 bool shouldPrintMachineCode() const { return Options.PrintMachineCode; } 00155 00156 /// getAsmVerbosityDefault - Returns the default value of asm verbosity. 00157 /// 00158 bool getAsmVerbosityDefault() const ; 00159 00160 /// setAsmVerbosityDefault - Set the default value of asm verbosity. Default 00161 /// is false. 00162 void setAsmVerbosityDefault(bool); 00163 00164 /// getDataSections - Return true if data objects should be emitted into their 00165 /// own section, corresponds to -fdata-sections. 00166 bool getDataSections() const; 00167 00168 /// getFunctionSections - Return true if functions should be emitted into 00169 /// their own section, corresponding to -ffunction-sections. 00170 bool getFunctionSections() const; 00171 00172 /// setDataSections - Set if the data are emit into separate sections. 00173 void setDataSections(bool); 00174 00175 /// setFunctionSections - Set if the functions are emit into separate 00176 /// sections. 00177 void setFunctionSections(bool); 00178 00179 /// \brief Register analysis passes for this target with a pass manager. 00180 virtual void addAnalysisPasses(PassManagerBase &) {} 00181 00182 /// CodeGenFileType - These enums are meant to be passed into 00183 /// addPassesToEmitFile to indicate what type of file to emit, and returned by 00184 /// it to indicate what type of file could actually be made. 00185 enum CodeGenFileType { 00186 CGFT_AssemblyFile, 00187 CGFT_ObjectFile, 00188 CGFT_Null // Do not emit any output. 00189 }; 00190 00191 /// addPassesToEmitFile - Add passes to the specified pass manager to get the 00192 /// specified file emitted. Typically this will involve several steps of code 00193 /// generation. This method should return true if emission of this file type 00194 /// is not supported, or false on success. 00195 virtual bool addPassesToEmitFile(PassManagerBase &, 00196 formatted_raw_ostream &, 00197 CodeGenFileType, 00198 bool /*DisableVerify*/ = true, 00199 AnalysisID /*StartAfter*/ = nullptr, 00200 AnalysisID /*StopAfter*/ = nullptr) { 00201 return true; 00202 } 00203 00204 /// addPassesToEmitMC - Add passes to the specified pass manager to get 00205 /// machine code emitted with the MCJIT. This method returns true if machine 00206 /// code is not supported. It fills the MCContext Ctx pointer which can be 00207 /// used to build custom MCStreamer. 00208 /// 00209 virtual bool addPassesToEmitMC(PassManagerBase &, 00210 MCContext *&, 00211 raw_ostream &, 00212 bool /*DisableVerify*/ = true) { 00213 return true; 00214 } 00215 00216 void getNameWithPrefix(SmallVectorImpl<char> &Name, const GlobalValue *GV, 00217 Mangler &Mang, bool MayAlwaysUsePrivate = false) const; 00218 MCSymbol *getSymbol(const GlobalValue *GV, Mangler &Mang) const; 00219 }; 00220 00221 /// LLVMTargetMachine - This class describes a target machine that is 00222 /// implemented with the LLVM target-independent code generator. 00223 /// 00224 class LLVMTargetMachine : public TargetMachine { 00225 protected: // Can only create subclasses. 00226 LLVMTargetMachine(const Target &T, StringRef TargetTriple, 00227 StringRef CPU, StringRef FS, TargetOptions Options, 00228 Reloc::Model RM, CodeModel::Model CM, 00229 CodeGenOpt::Level OL); 00230 00231 void initAsmInfo(); 00232 public: 00233 /// \brief Register analysis passes for this target with a pass manager. 00234 /// 00235 /// This registers target independent analysis passes. 00236 void addAnalysisPasses(PassManagerBase &PM) override; 00237 00238 /// createPassConfig - Create a pass configuration object to be used by 00239 /// addPassToEmitX methods for generating a pipeline of CodeGen passes. 00240 virtual TargetPassConfig *createPassConfig(PassManagerBase &PM); 00241 00242 /// addPassesToEmitFile - Add passes to the specified pass manager to get the 00243 /// specified file emitted. Typically this will involve several steps of code 00244 /// generation. 00245 bool addPassesToEmitFile(PassManagerBase &PM, formatted_raw_ostream &Out, 00246 CodeGenFileType FileType, bool DisableVerify = true, 00247 AnalysisID StartAfter = nullptr, 00248 AnalysisID StopAfter = nullptr) override; 00249 00250 /// addPassesToEmitMC - Add passes to the specified pass manager to get 00251 /// machine code emitted with the MCJIT. This method returns true if machine 00252 /// code is not supported. It fills the MCContext Ctx pointer which can be 00253 /// used to build custom MCStreamer. 00254 /// 00255 bool addPassesToEmitMC(PassManagerBase &PM, MCContext *&Ctx, 00256 raw_ostream &OS, bool DisableVerify = true) override; 00257 }; 00258 00259 } // End llvm namespace 00260 00261 #endif