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 84*26d11ca4SSanjoy Das TargetTransformInfo 85*26d11ca4SSanjoy 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 = 139bb8507e6SMatthias Braun getTarget().createMCAsmBackend(MRI, getTargetTriple().str(), TargetCPU, 140bb8507e6SMatthias Braun Options.MCOptions); 141bb8507e6SMatthias Braun auto FOut = llvm::make_unique<formatted_raw_ostream>(Out); 142bb8507e6SMatthias Braun MCStreamer *S = getTarget().createAsmStreamer( 143bb8507e6SMatthias Braun Context, std::move(FOut), Options.MCOptions.AsmVerbose, 144bb8507e6SMatthias Braun Options.MCOptions.MCUseDwarfDirectory, InstPrinter, MCE, MAB, 145bb8507e6SMatthias Braun Options.MCOptions.ShowMCInst); 146bb8507e6SMatthias Braun AsmStreamer.reset(S); 147bb8507e6SMatthias Braun break; 148bb8507e6SMatthias Braun } 149bb8507e6SMatthias Braun case CGFT_ObjectFile: { 150bb8507e6SMatthias Braun // Create the code emitter for the target if it exists. If not, .o file 151bb8507e6SMatthias Braun // emission fails. 152bb8507e6SMatthias Braun MCCodeEmitter *MCE = getTarget().createMCCodeEmitter(MII, MRI, Context); 153bb8507e6SMatthias Braun MCAsmBackend *MAB = 154bb8507e6SMatthias Braun getTarget().createMCAsmBackend(MRI, getTargetTriple().str(), TargetCPU, 155bb8507e6SMatthias Braun Options.MCOptions); 156bb8507e6SMatthias Braun if (!MCE || !MAB) 157bb8507e6SMatthias Braun return true; 158bb8507e6SMatthias Braun 159bb8507e6SMatthias Braun // Don't waste memory on names of temp labels. 160bb8507e6SMatthias Braun Context.setUseNamesOnTempLabels(false); 161bb8507e6SMatthias Braun 162bb8507e6SMatthias Braun Triple T(getTargetTriple().str()); 163bb8507e6SMatthias Braun AsmStreamer.reset(getTarget().createMCObjectStreamer( 164bb8507e6SMatthias Braun T, Context, std::unique_ptr<MCAsmBackend>(MAB), Out, 165bb8507e6SMatthias Braun std::unique_ptr<MCCodeEmitter>(MCE), STI, Options.MCOptions.MCRelaxAll, 166bb8507e6SMatthias Braun Options.MCOptions.MCIncrementalLinkerCompatible, 167bb8507e6SMatthias Braun /*DWARFMustBeAtTheEnd*/ true)); 168bb8507e6SMatthias Braun break; 169bb8507e6SMatthias Braun } 170bb8507e6SMatthias Braun case CGFT_Null: 171bb8507e6SMatthias Braun // The Null output is intended for use for performance analysis and testing, 172bb8507e6SMatthias Braun // not real users. 173bb8507e6SMatthias Braun AsmStreamer.reset(getTarget().createNullStreamer(Context)); 174bb8507e6SMatthias Braun break; 175bb8507e6SMatthias Braun } 176bb8507e6SMatthias Braun 177bb8507e6SMatthias Braun // Create the AsmPrinter, which takes ownership of AsmStreamer if successful. 178bb8507e6SMatthias Braun FunctionPass *Printer = 179bb8507e6SMatthias Braun getTarget().createAsmPrinter(*this, std::move(AsmStreamer)); 180bb8507e6SMatthias Braun if (!Printer) 181bb8507e6SMatthias Braun return true; 182bb8507e6SMatthias Braun 183bb8507e6SMatthias Braun PM.add(Printer); 184bb8507e6SMatthias Braun return false; 185bb8507e6SMatthias Braun } 186bb8507e6SMatthias Braun 187bb8507e6SMatthias Braun bool LLVMTargetMachine::addPassesToEmitFile(PassManagerBase &PM, 188bb8507e6SMatthias Braun raw_pwrite_stream &Out, 189bb8507e6SMatthias Braun CodeGenFileType FileType, 190bb8507e6SMatthias Braun bool DisableVerify, 191bb8507e6SMatthias Braun MachineModuleInfo *MMI) { 192bb8507e6SMatthias Braun // Add common CodeGen passes. 193bb8507e6SMatthias Braun bool WillCompleteCodeGenPipeline = true; 194bb8507e6SMatthias Braun MCContext *Context = addPassesToGenerateCode( 195bb8507e6SMatthias Braun this, PM, DisableVerify, WillCompleteCodeGenPipeline, Out, MMI); 196bb8507e6SMatthias Braun if (!Context) 197bb8507e6SMatthias Braun return true; 198bb8507e6SMatthias Braun 199bb8507e6SMatthias Braun if (WillCompleteCodeGenPipeline && addAsmPrinter(PM, Out, FileType, *Context)) 200bb8507e6SMatthias Braun return true; 201bb8507e6SMatthias Braun 202bb8507e6SMatthias Braun PM.add(createFreeMachineFunctionPass()); 203bb8507e6SMatthias Braun return false; 204bb8507e6SMatthias Braun } 205bb8507e6SMatthias Braun 206bb8507e6SMatthias Braun /// addPassesToEmitMC - Add passes to the specified pass manager to get 207bb8507e6SMatthias Braun /// machine code emitted with the MCJIT. This method returns true if machine 208bb8507e6SMatthias Braun /// code is not supported. It fills the MCContext Ctx pointer which can be 209bb8507e6SMatthias Braun /// used to build custom MCStreamer. 210bb8507e6SMatthias Braun /// 211bb8507e6SMatthias Braun bool LLVMTargetMachine::addPassesToEmitMC(PassManagerBase &PM, MCContext *&Ctx, 212bb8507e6SMatthias Braun raw_pwrite_stream &Out, 213bb8507e6SMatthias Braun bool DisableVerify) { 214bb8507e6SMatthias Braun // Add common CodeGen passes. 215bb8507e6SMatthias Braun bool WillCompleteCodeGenPipeline = true; 216bb8507e6SMatthias Braun Ctx = addPassesToGenerateCode(this, PM, DisableVerify, 217bb8507e6SMatthias Braun WillCompleteCodeGenPipeline, Out, 218bb8507e6SMatthias Braun /*MachineModuleInfo*/ nullptr); 219bb8507e6SMatthias Braun if (!Ctx) 220bb8507e6SMatthias Braun return true; 221bb8507e6SMatthias Braun assert(WillCompleteCodeGenPipeline && "CodeGen pipeline has been altered"); 222bb8507e6SMatthias Braun 223bb8507e6SMatthias Braun if (Options.MCOptions.MCSaveTempLabels) 224bb8507e6SMatthias Braun Ctx->setAllowTemporaryLabels(false); 225bb8507e6SMatthias Braun 226bb8507e6SMatthias Braun // Create the code emitter for the target if it exists. If not, .o file 227bb8507e6SMatthias Braun // emission fails. 228bb8507e6SMatthias Braun const MCRegisterInfo &MRI = *getMCRegisterInfo(); 229bb8507e6SMatthias Braun MCCodeEmitter *MCE = 230bb8507e6SMatthias Braun getTarget().createMCCodeEmitter(*getMCInstrInfo(), MRI, *Ctx); 231bb8507e6SMatthias Braun MCAsmBackend *MAB = 232bb8507e6SMatthias Braun getTarget().createMCAsmBackend(MRI, getTargetTriple().str(), TargetCPU, 233bb8507e6SMatthias Braun Options.MCOptions); 234bb8507e6SMatthias Braun if (!MCE || !MAB) 235bb8507e6SMatthias Braun return true; 236bb8507e6SMatthias Braun 237bb8507e6SMatthias Braun const Triple &T = getTargetTriple(); 238bb8507e6SMatthias Braun const MCSubtargetInfo &STI = *getMCSubtargetInfo(); 239bb8507e6SMatthias Braun std::unique_ptr<MCStreamer> AsmStreamer(getTarget().createMCObjectStreamer( 240bb8507e6SMatthias Braun T, *Ctx, std::unique_ptr<MCAsmBackend>(MAB), Out, 241bb8507e6SMatthias Braun std::unique_ptr<MCCodeEmitter>(MCE), STI, Options.MCOptions.MCRelaxAll, 242bb8507e6SMatthias Braun Options.MCOptions.MCIncrementalLinkerCompatible, 243bb8507e6SMatthias Braun /*DWARFMustBeAtTheEnd*/ true)); 244bb8507e6SMatthias Braun 245bb8507e6SMatthias Braun // Create the AsmPrinter, which takes ownership of AsmStreamer if successful. 246bb8507e6SMatthias Braun FunctionPass *Printer = 247bb8507e6SMatthias Braun getTarget().createAsmPrinter(*this, std::move(AsmStreamer)); 248bb8507e6SMatthias Braun if (!Printer) 249bb8507e6SMatthias Braun return true; 250bb8507e6SMatthias Braun 251bb8507e6SMatthias Braun PM.add(Printer); 252bb8507e6SMatthias Braun PM.add(createFreeMachineFunctionPass()); 253bb8507e6SMatthias Braun 254bb8507e6SMatthias Braun return false; // success! 255bb8507e6SMatthias Braun } 256