1bb8507e6SMatthias Braun //===-- LLVMTargetMachine.cpp - Implement the LLVMTargetMachine class -----===// 2bb8507e6SMatthias Braun // 3bb8507e6SMatthias Braun // The LLVM Compiler Infrastructure 4bb8507e6SMatthias Braun // 5bb8507e6SMatthias Braun // This file is distributed under the University of Illinois Open Source 6bb8507e6SMatthias Braun // License. See LICENSE.TXT for details. 7bb8507e6SMatthias Braun // 8bb8507e6SMatthias Braun //===----------------------------------------------------------------------===// 9bb8507e6SMatthias Braun // 10bb8507e6SMatthias Braun // This file implements the LLVMTargetMachine class. 11bb8507e6SMatthias Braun // 12bb8507e6SMatthias Braun //===----------------------------------------------------------------------===// 13bb8507e6SMatthias Braun 14bb8507e6SMatthias Braun #include "llvm/Analysis/Passes.h" 15bb8507e6SMatthias Braun #include "llvm/CodeGen/AsmPrinter.h" 16bb8507e6SMatthias Braun #include "llvm/CodeGen/BasicTTIImpl.h" 17bb8507e6SMatthias Braun #include "llvm/CodeGen/MachineModuleInfo.h" 18bb8507e6SMatthias Braun #include "llvm/CodeGen/Passes.h" 19b3bde2eaSDavid Blaikie #include "llvm/CodeGen/TargetLoweringObjectFile.h" 20bb8507e6SMatthias Braun #include "llvm/CodeGen/TargetPassConfig.h" 21bb8507e6SMatthias Braun #include "llvm/IR/LegacyPassManager.h" 22bb8507e6SMatthias Braun #include "llvm/MC/MCAsmBackend.h" 23bb8507e6SMatthias Braun #include "llvm/MC/MCAsmInfo.h" 24bb8507e6SMatthias Braun #include "llvm/MC/MCCodeEmitter.h" 25bb8507e6SMatthias Braun #include "llvm/MC/MCContext.h" 26bb8507e6SMatthias Braun #include "llvm/MC/MCInstrInfo.h" 27bb8507e6SMatthias Braun #include "llvm/MC/MCStreamer.h" 28bb8507e6SMatthias Braun #include "llvm/MC/MCSubtargetInfo.h" 29bb8507e6SMatthias Braun #include "llvm/Support/CommandLine.h" 30bb8507e6SMatthias Braun #include "llvm/Support/ErrorHandling.h" 31bb8507e6SMatthias Braun #include "llvm/Support/FormattedStream.h" 32bb8507e6SMatthias Braun #include "llvm/Support/TargetRegistry.h" 33bb8507e6SMatthias Braun #include "llvm/Target/TargetMachine.h" 34bb8507e6SMatthias Braun #include "llvm/Target/TargetOptions.h" 35bb8507e6SMatthias Braun using namespace llvm; 36bb8507e6SMatthias Braun 37bb8507e6SMatthias Braun void LLVMTargetMachine::initAsmInfo() { 38bb8507e6SMatthias Braun MRI = TheTarget.createMCRegInfo(getTargetTriple().str()); 39bb8507e6SMatthias Braun MII = TheTarget.createMCInstrInfo(); 40bb8507e6SMatthias Braun // FIXME: Having an MCSubtargetInfo on the target machine is a hack due 41bb8507e6SMatthias Braun // to some backends having subtarget feature dependent module level 42bb8507e6SMatthias Braun // code generation. This is similar to the hack in the AsmPrinter for 43bb8507e6SMatthias Braun // module level assembly etc. 44bb8507e6SMatthias Braun STI = TheTarget.createMCSubtargetInfo(getTargetTriple().str(), getTargetCPU(), 45bb8507e6SMatthias Braun getTargetFeatureString()); 46bb8507e6SMatthias Braun 47bb8507e6SMatthias Braun MCAsmInfo *TmpAsmInfo = 48bb8507e6SMatthias Braun TheTarget.createMCAsmInfo(*MRI, getTargetTriple().str()); 49bb8507e6SMatthias Braun // TargetSelect.h moved to a different directory between LLVM 2.9 and 3.0, 50bb8507e6SMatthias Braun // and if the old one gets included then MCAsmInfo will be NULL and 51bb8507e6SMatthias Braun // we'll crash later. 52bb8507e6SMatthias Braun // Provide the user with a useful error message about what's wrong. 53bb8507e6SMatthias Braun assert(TmpAsmInfo && "MCAsmInfo not initialized. " 54bb8507e6SMatthias Braun "Make sure you include the correct TargetSelect.h" 55bb8507e6SMatthias Braun "and that InitializeAllTargetMCs() is being invoked!"); 56bb8507e6SMatthias Braun 57bb8507e6SMatthias Braun if (Options.DisableIntegratedAS) 58bb8507e6SMatthias Braun TmpAsmInfo->setUseIntegratedAssembler(false); 59bb8507e6SMatthias Braun 60bb8507e6SMatthias Braun TmpAsmInfo->setPreserveAsmComments(Options.MCOptions.PreserveAsmComments); 61bb8507e6SMatthias Braun 62bb8507e6SMatthias Braun TmpAsmInfo->setCompressDebugSections(Options.CompressDebugSections); 63bb8507e6SMatthias Braun 64bb8507e6SMatthias Braun TmpAsmInfo->setRelaxELFRelocations(Options.RelaxELFRelocations); 65bb8507e6SMatthias Braun 66bb8507e6SMatthias Braun if (Options.ExceptionModel != ExceptionHandling::None) 67bb8507e6SMatthias Braun TmpAsmInfo->setExceptionsType(Options.ExceptionModel); 68bb8507e6SMatthias Braun 69bb8507e6SMatthias Braun AsmInfo = TmpAsmInfo; 70bb8507e6SMatthias Braun } 71bb8507e6SMatthias Braun 72bb8507e6SMatthias Braun LLVMTargetMachine::LLVMTargetMachine(const Target &T, 73bb8507e6SMatthias Braun StringRef DataLayoutString, 74bb8507e6SMatthias Braun const Triple &TT, StringRef CPU, 75bb8507e6SMatthias Braun StringRef FS, const TargetOptions &Options, 76bb8507e6SMatthias Braun Reloc::Model RM, CodeModel::Model CM, 77bb8507e6SMatthias Braun CodeGenOpt::Level OL) 78bb8507e6SMatthias Braun : TargetMachine(T, DataLayoutString, TT, CPU, FS, Options) { 79bb8507e6SMatthias Braun this->RM = RM; 80bb8507e6SMatthias Braun this->CMModel = CM; 81bb8507e6SMatthias Braun this->OptLevel = OL; 82bb8507e6SMatthias Braun } 83bb8507e6SMatthias Braun 8426d11ca4SSanjoy Das TargetTransformInfo 8526d11ca4SSanjoy Das LLVMTargetMachine::getTargetTransformInfo(const Function &F) { 86bb8507e6SMatthias Braun return TargetTransformInfo(BasicTTIImpl(this, F)); 87bb8507e6SMatthias Braun } 88bb8507e6SMatthias Braun 89bb8507e6SMatthias Braun /// addPassesToX helper drives creation and initialization of TargetPassConfig. 90bb8507e6SMatthias Braun static MCContext * 91bb8507e6SMatthias Braun addPassesToGenerateCode(LLVMTargetMachine *TM, PassManagerBase &PM, 92bb8507e6SMatthias Braun bool DisableVerify, bool &WillCompleteCodeGenPipeline, 93bb8507e6SMatthias Braun raw_pwrite_stream &Out, MachineModuleInfo *MMI) { 94bb8507e6SMatthias Braun // Targets may override createPassConfig to provide a target-specific 95bb8507e6SMatthias Braun // subclass. 96bb8507e6SMatthias Braun TargetPassConfig *PassConfig = TM->createPassConfig(PM); 97bb8507e6SMatthias Braun // Set PassConfig options provided by TargetMachine. 98bb8507e6SMatthias Braun PassConfig->setDisableVerify(DisableVerify); 99bb8507e6SMatthias Braun WillCompleteCodeGenPipeline = PassConfig->willCompleteCodeGenPipeline(); 100bb8507e6SMatthias Braun PM.add(PassConfig); 101bb8507e6SMatthias Braun if (!MMI) 102bb8507e6SMatthias Braun MMI = new MachineModuleInfo(TM); 103bb8507e6SMatthias Braun PM.add(MMI); 104bb8507e6SMatthias Braun 105bb8507e6SMatthias Braun if (PassConfig->addISelPasses()) 106bb8507e6SMatthias Braun return nullptr; 107bb8507e6SMatthias Braun PassConfig->addMachinePasses(); 108bb8507e6SMatthias Braun PassConfig->setInitialized(); 109bb8507e6SMatthias Braun if (!WillCompleteCodeGenPipeline) 110bb8507e6SMatthias Braun PM.add(createPrintMIRPass(Out)); 111bb8507e6SMatthias Braun 112bb8507e6SMatthias Braun return &MMI->getContext(); 113bb8507e6SMatthias Braun } 114bb8507e6SMatthias Braun 115bb8507e6SMatthias Braun bool LLVMTargetMachine::addAsmPrinter(PassManagerBase &PM, 116bb8507e6SMatthias Braun raw_pwrite_stream &Out, CodeGenFileType FileType, 117bb8507e6SMatthias Braun MCContext &Context) { 118bb8507e6SMatthias Braun if (Options.MCOptions.MCSaveTempLabels) 119bb8507e6SMatthias Braun Context.setAllowTemporaryLabels(false); 120bb8507e6SMatthias Braun 121bb8507e6SMatthias Braun const MCSubtargetInfo &STI = *getMCSubtargetInfo(); 122bb8507e6SMatthias Braun const MCAsmInfo &MAI = *getMCAsmInfo(); 123bb8507e6SMatthias Braun const MCRegisterInfo &MRI = *getMCRegisterInfo(); 124bb8507e6SMatthias Braun const MCInstrInfo &MII = *getMCInstrInfo(); 125bb8507e6SMatthias Braun 126bb8507e6SMatthias Braun std::unique_ptr<MCStreamer> AsmStreamer; 127bb8507e6SMatthias Braun 128bb8507e6SMatthias Braun switch (FileType) { 129bb8507e6SMatthias Braun case CGFT_AssemblyFile: { 130bb8507e6SMatthias Braun MCInstPrinter *InstPrinter = getTarget().createMCInstPrinter( 131bb8507e6SMatthias Braun getTargetTriple(), MAI.getAssemblerDialect(), MAI, MII, MRI); 132bb8507e6SMatthias Braun 133bb8507e6SMatthias Braun // Create a code emitter if asked to show the encoding. 134bb8507e6SMatthias Braun MCCodeEmitter *MCE = nullptr; 135bb8507e6SMatthias Braun if (Options.MCOptions.ShowMCEncoding) 136bb8507e6SMatthias Braun MCE = getTarget().createMCCodeEmitter(MII, MRI, Context); 137bb8507e6SMatthias Braun 138bb8507e6SMatthias Braun MCAsmBackend *MAB = 139*b22f751fSAlex Bradbury getTarget().createMCAsmBackend(STI, MRI, Options.MCOptions); 140bb8507e6SMatthias Braun auto FOut = llvm::make_unique<formatted_raw_ostream>(Out); 141bb8507e6SMatthias Braun MCStreamer *S = getTarget().createAsmStreamer( 142bb8507e6SMatthias Braun Context, std::move(FOut), Options.MCOptions.AsmVerbose, 143bb8507e6SMatthias Braun Options.MCOptions.MCUseDwarfDirectory, InstPrinter, MCE, MAB, 144bb8507e6SMatthias Braun Options.MCOptions.ShowMCInst); 145bb8507e6SMatthias Braun AsmStreamer.reset(S); 146bb8507e6SMatthias Braun break; 147bb8507e6SMatthias Braun } 148bb8507e6SMatthias Braun case CGFT_ObjectFile: { 149bb8507e6SMatthias Braun // Create the code emitter for the target if it exists. If not, .o file 150bb8507e6SMatthias Braun // emission fails. 151bb8507e6SMatthias Braun MCCodeEmitter *MCE = getTarget().createMCCodeEmitter(MII, MRI, Context); 152bb8507e6SMatthias Braun MCAsmBackend *MAB = 153*b22f751fSAlex Bradbury getTarget().createMCAsmBackend(STI, MRI, Options.MCOptions); 154bb8507e6SMatthias Braun if (!MCE || !MAB) 155bb8507e6SMatthias Braun return true; 156bb8507e6SMatthias Braun 157bb8507e6SMatthias Braun // Don't waste memory on names of temp labels. 158bb8507e6SMatthias Braun Context.setUseNamesOnTempLabels(false); 159bb8507e6SMatthias Braun 160bb8507e6SMatthias Braun Triple T(getTargetTriple().str()); 161bb8507e6SMatthias Braun AsmStreamer.reset(getTarget().createMCObjectStreamer( 162bb8507e6SMatthias Braun T, Context, std::unique_ptr<MCAsmBackend>(MAB), Out, 163bb8507e6SMatthias Braun std::unique_ptr<MCCodeEmitter>(MCE), STI, Options.MCOptions.MCRelaxAll, 164bb8507e6SMatthias Braun Options.MCOptions.MCIncrementalLinkerCompatible, 165bb8507e6SMatthias Braun /*DWARFMustBeAtTheEnd*/ true)); 166bb8507e6SMatthias Braun break; 167bb8507e6SMatthias Braun } 168bb8507e6SMatthias Braun case CGFT_Null: 169bb8507e6SMatthias Braun // The Null output is intended for use for performance analysis and testing, 170bb8507e6SMatthias Braun // not real users. 171bb8507e6SMatthias Braun AsmStreamer.reset(getTarget().createNullStreamer(Context)); 172bb8507e6SMatthias Braun break; 173bb8507e6SMatthias Braun } 174bb8507e6SMatthias Braun 175bb8507e6SMatthias Braun // Create the AsmPrinter, which takes ownership of AsmStreamer if successful. 176bb8507e6SMatthias Braun FunctionPass *Printer = 177bb8507e6SMatthias Braun getTarget().createAsmPrinter(*this, std::move(AsmStreamer)); 178bb8507e6SMatthias Braun if (!Printer) 179bb8507e6SMatthias Braun return true; 180bb8507e6SMatthias Braun 181bb8507e6SMatthias Braun PM.add(Printer); 182bb8507e6SMatthias Braun return false; 183bb8507e6SMatthias Braun } 184bb8507e6SMatthias Braun 185bb8507e6SMatthias Braun bool LLVMTargetMachine::addPassesToEmitFile(PassManagerBase &PM, 186bb8507e6SMatthias Braun raw_pwrite_stream &Out, 187bb8507e6SMatthias Braun CodeGenFileType FileType, 188bb8507e6SMatthias Braun bool DisableVerify, 189bb8507e6SMatthias Braun MachineModuleInfo *MMI) { 190bb8507e6SMatthias Braun // Add common CodeGen passes. 191bb8507e6SMatthias Braun bool WillCompleteCodeGenPipeline = true; 192bb8507e6SMatthias Braun MCContext *Context = addPassesToGenerateCode( 193bb8507e6SMatthias Braun this, PM, DisableVerify, WillCompleteCodeGenPipeline, Out, MMI); 194bb8507e6SMatthias Braun if (!Context) 195bb8507e6SMatthias Braun return true; 196bb8507e6SMatthias Braun 197bb8507e6SMatthias Braun if (WillCompleteCodeGenPipeline && addAsmPrinter(PM, Out, FileType, *Context)) 198bb8507e6SMatthias Braun return true; 199bb8507e6SMatthias Braun 200bb8507e6SMatthias Braun PM.add(createFreeMachineFunctionPass()); 201bb8507e6SMatthias Braun return false; 202bb8507e6SMatthias Braun } 203bb8507e6SMatthias Braun 204bb8507e6SMatthias Braun /// addPassesToEmitMC - Add passes to the specified pass manager to get 205bb8507e6SMatthias Braun /// machine code emitted with the MCJIT. This method returns true if machine 206bb8507e6SMatthias Braun /// code is not supported. It fills the MCContext Ctx pointer which can be 207bb8507e6SMatthias Braun /// used to build custom MCStreamer. 208bb8507e6SMatthias Braun /// 209bb8507e6SMatthias Braun bool LLVMTargetMachine::addPassesToEmitMC(PassManagerBase &PM, MCContext *&Ctx, 210bb8507e6SMatthias Braun raw_pwrite_stream &Out, 211bb8507e6SMatthias Braun bool DisableVerify) { 212bb8507e6SMatthias Braun // Add common CodeGen passes. 213bb8507e6SMatthias Braun bool WillCompleteCodeGenPipeline = true; 214bb8507e6SMatthias Braun Ctx = addPassesToGenerateCode(this, PM, DisableVerify, 215bb8507e6SMatthias Braun WillCompleteCodeGenPipeline, Out, 216bb8507e6SMatthias Braun /*MachineModuleInfo*/ nullptr); 217bb8507e6SMatthias Braun if (!Ctx) 218bb8507e6SMatthias Braun return true; 219bb8507e6SMatthias Braun assert(WillCompleteCodeGenPipeline && "CodeGen pipeline has been altered"); 220bb8507e6SMatthias Braun 221bb8507e6SMatthias Braun if (Options.MCOptions.MCSaveTempLabels) 222bb8507e6SMatthias Braun Ctx->setAllowTemporaryLabels(false); 223bb8507e6SMatthias Braun 224bb8507e6SMatthias Braun // Create the code emitter for the target if it exists. If not, .o file 225bb8507e6SMatthias Braun // emission fails. 226*b22f751fSAlex Bradbury const MCSubtargetInfo &STI = *getMCSubtargetInfo(); 227bb8507e6SMatthias Braun const MCRegisterInfo &MRI = *getMCRegisterInfo(); 228bb8507e6SMatthias Braun MCCodeEmitter *MCE = 229bb8507e6SMatthias Braun getTarget().createMCCodeEmitter(*getMCInstrInfo(), MRI, *Ctx); 230bb8507e6SMatthias Braun MCAsmBackend *MAB = 231*b22f751fSAlex Bradbury getTarget().createMCAsmBackend(STI, MRI, Options.MCOptions); 232bb8507e6SMatthias Braun if (!MCE || !MAB) 233bb8507e6SMatthias Braun return true; 234bb8507e6SMatthias Braun 235bb8507e6SMatthias Braun const Triple &T = getTargetTriple(); 236bb8507e6SMatthias Braun std::unique_ptr<MCStreamer> AsmStreamer(getTarget().createMCObjectStreamer( 237bb8507e6SMatthias Braun T, *Ctx, std::unique_ptr<MCAsmBackend>(MAB), Out, 238bb8507e6SMatthias Braun std::unique_ptr<MCCodeEmitter>(MCE), STI, Options.MCOptions.MCRelaxAll, 239bb8507e6SMatthias Braun Options.MCOptions.MCIncrementalLinkerCompatible, 240bb8507e6SMatthias Braun /*DWARFMustBeAtTheEnd*/ true)); 241bb8507e6SMatthias Braun 242bb8507e6SMatthias Braun // Create the AsmPrinter, which takes ownership of AsmStreamer if successful. 243bb8507e6SMatthias Braun FunctionPass *Printer = 244bb8507e6SMatthias Braun getTarget().createAsmPrinter(*this, std::move(AsmStreamer)); 245bb8507e6SMatthias Braun if (!Printer) 246bb8507e6SMatthias Braun return true; 247bb8507e6SMatthias Braun 248bb8507e6SMatthias Braun PM.add(Printer); 249bb8507e6SMatthias Braun PM.add(createFreeMachineFunctionPass()); 250bb8507e6SMatthias Braun 251bb8507e6SMatthias Braun return false; // success! 252bb8507e6SMatthias Braun } 253