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