1bb8507e6SMatthias Braun //===-- LLVMTargetMachine.cpp - Implement the LLVMTargetMachine class -----===// 2bb8507e6SMatthias Braun // 32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information. 52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6bb8507e6SMatthias Braun // 7bb8507e6SMatthias Braun //===----------------------------------------------------------------------===// 8bb8507e6SMatthias Braun // 9bb8507e6SMatthias Braun // This file implements the LLVMTargetMachine class. 10bb8507e6SMatthias Braun // 11bb8507e6SMatthias Braun //===----------------------------------------------------------------------===// 12bb8507e6SMatthias Braun 13bb8507e6SMatthias Braun #include "llvm/Analysis/Passes.h" 14bb8507e6SMatthias Braun #include "llvm/CodeGen/AsmPrinter.h" 15bb8507e6SMatthias Braun #include "llvm/CodeGen/BasicTTIImpl.h" 16bb8507e6SMatthias Braun #include "llvm/CodeGen/MachineModuleInfo.h" 17bb8507e6SMatthias Braun #include "llvm/CodeGen/Passes.h" 18bb8507e6SMatthias Braun #include "llvm/CodeGen/TargetPassConfig.h" 19bb8507e6SMatthias Braun #include "llvm/IR/LegacyPassManager.h" 20bb8507e6SMatthias Braun #include "llvm/MC/MCAsmBackend.h" 21bb8507e6SMatthias Braun #include "llvm/MC/MCAsmInfo.h" 22bb8507e6SMatthias Braun #include "llvm/MC/MCCodeEmitter.h" 23bb8507e6SMatthias Braun #include "llvm/MC/MCContext.h" 24bb8507e6SMatthias Braun #include "llvm/MC/MCInstrInfo.h" 25f7b81db7SPeter Collingbourne #include "llvm/MC/MCObjectWriter.h" 26*ef736a1cSserge-sans-paille #include "llvm/MC/MCRegisterInfo.h" 27bb8507e6SMatthias Braun #include "llvm/MC/MCStreamer.h" 28bb8507e6SMatthias Braun #include "llvm/MC/MCSubtargetInfo.h" 2989b57061SReid Kleckner #include "llvm/MC/TargetRegistry.h" 30bb8507e6SMatthias Braun #include "llvm/Support/CommandLine.h" 31bb8507e6SMatthias Braun #include "llvm/Support/ErrorHandling.h" 32bb8507e6SMatthias Braun #include "llvm/Support/FormattedStream.h" 336054e650SDavid Blaikie #include "llvm/Target/TargetLoweringObjectFile.h" 34bb8507e6SMatthias Braun #include "llvm/Target/TargetMachine.h" 35bb8507e6SMatthias Braun #include "llvm/Target/TargetOptions.h" 36bb8507e6SMatthias Braun using namespace llvm; 37bb8507e6SMatthias Braun 386d9f8c98SDavid Green static cl::opt<bool> EnableTrapUnreachable("trap-unreachable", 396d9f8c98SDavid Green cl::Hidden, cl::ZeroOrMore, cl::init(false), 406d9f8c98SDavid Green cl::desc("Enable generating trap for unreachable")); 416d9f8c98SDavid Green 42bb8507e6SMatthias Braun void LLVMTargetMachine::initAsmInfo() { 4310a21625SFangrui Song MRI.reset(TheTarget.createMCRegInfo(getTargetTriple().str())); 441756d679SElla Ma assert(MRI && "Unable to create reg info"); 4510a21625SFangrui Song MII.reset(TheTarget.createMCInstrInfo()); 461756d679SElla Ma assert(MII && "Unable to create instruction info"); 47bb8507e6SMatthias Braun // FIXME: Having an MCSubtargetInfo on the target machine is a hack due 48bb8507e6SMatthias Braun // to some backends having subtarget feature dependent module level 49bb8507e6SMatthias Braun // code generation. This is similar to the hack in the AsmPrinter for 50bb8507e6SMatthias Braun // module level assembly etc. 5110a21625SFangrui Song STI.reset(TheTarget.createMCSubtargetInfo( 5210a21625SFangrui Song getTargetTriple().str(), getTargetCPU(), getTargetFeatureString())); 531756d679SElla Ma assert(STI && "Unable to create subtarget info"); 54bb8507e6SMatthias Braun 554b63ca13SMirko Brkusanin MCAsmInfo *TmpAsmInfo = TheTarget.createMCAsmInfo( 564b63ca13SMirko Brkusanin *MRI, getTargetTriple().str(), Options.MCOptions); 57bb8507e6SMatthias Braun // TargetSelect.h moved to a different directory between LLVM 2.9 and 3.0, 58bb8507e6SMatthias Braun // and if the old one gets included then MCAsmInfo will be NULL and 59bb8507e6SMatthias Braun // we'll crash later. 60bb8507e6SMatthias Braun // Provide the user with a useful error message about what's wrong. 61bb8507e6SMatthias Braun assert(TmpAsmInfo && "MCAsmInfo not initialized. " 62bb8507e6SMatthias Braun "Make sure you include the correct TargetSelect.h" 63bb8507e6SMatthias Braun "and that InitializeAllTargetMCs() is being invoked!"); 64bb8507e6SMatthias Braun 6534b60d8aSFangrui Song if (Options.BinutilsVersion.first > 0) 6634b60d8aSFangrui Song TmpAsmInfo->setBinutilsVersion(Options.BinutilsVersion); 6734b60d8aSFangrui Song 6803e9dcfdSJinsong Ji if (Options.DisableIntegratedAS) { 69bb8507e6SMatthias Braun TmpAsmInfo->setUseIntegratedAssembler(false); 7003e9dcfdSJinsong Ji // If there is explict option disable integratedAS, we can't use it for 7103e9dcfdSJinsong Ji // inlineasm either. 7203e9dcfdSJinsong Ji TmpAsmInfo->setParseInlineAsmUsingAsmParser(false); 7303e9dcfdSJinsong Ji } 74bb8507e6SMatthias Braun 75bb8507e6SMatthias Braun TmpAsmInfo->setPreserveAsmComments(Options.MCOptions.PreserveAsmComments); 76bb8507e6SMatthias Braun 77bb8507e6SMatthias Braun TmpAsmInfo->setCompressDebugSections(Options.CompressDebugSections); 78bb8507e6SMatthias Braun 79bb8507e6SMatthias Braun TmpAsmInfo->setRelaxELFRelocations(Options.RelaxELFRelocations); 80bb8507e6SMatthias Braun 81bb8507e6SMatthias Braun if (Options.ExceptionModel != ExceptionHandling::None) 82bb8507e6SMatthias Braun TmpAsmInfo->setExceptionsType(Options.ExceptionModel); 83bb8507e6SMatthias Braun 8410a21625SFangrui Song AsmInfo.reset(TmpAsmInfo); 85bb8507e6SMatthias Braun } 86bb8507e6SMatthias Braun 87bb8507e6SMatthias Braun LLVMTargetMachine::LLVMTargetMachine(const Target &T, 88bb8507e6SMatthias Braun StringRef DataLayoutString, 89bb8507e6SMatthias Braun const Triple &TT, StringRef CPU, 90bb8507e6SMatthias Braun StringRef FS, const TargetOptions &Options, 91bb8507e6SMatthias Braun Reloc::Model RM, CodeModel::Model CM, 92bb8507e6SMatthias Braun CodeGenOpt::Level OL) 93bb8507e6SMatthias Braun : TargetMachine(T, DataLayoutString, TT, CPU, FS, Options) { 94bb8507e6SMatthias Braun this->RM = RM; 95bb8507e6SMatthias Braun this->CMModel = CM; 96bb8507e6SMatthias Braun this->OptLevel = OL; 976d9f8c98SDavid Green 986d9f8c98SDavid Green if (EnableTrapUnreachable) 996d9f8c98SDavid Green this->Options.TrapUnreachable = true; 100bb8507e6SMatthias Braun } 101bb8507e6SMatthias Braun 10226d11ca4SSanjoy Das TargetTransformInfo 10326d11ca4SSanjoy Das LLVMTargetMachine::getTargetTransformInfo(const Function &F) { 104bb8507e6SMatthias Braun return TargetTransformInfo(BasicTTIImpl(this, F)); 105bb8507e6SMatthias Braun } 106bb8507e6SMatthias Braun 107bb8507e6SMatthias Braun /// addPassesToX helper drives creation and initialization of TargetPassConfig. 108e8f717aeSMatthias Braun static TargetPassConfig * 109e8f717aeSMatthias Braun addPassesToGenerateCode(LLVMTargetMachine &TM, PassManagerBase &PM, 110cc382cf7SYuanfang Chen bool DisableVerify, 111cc382cf7SYuanfang Chen MachineModuleInfoWrapperPass &MMIWP) { 112bb8507e6SMatthias Braun // Targets may override createPassConfig to provide a target-specific 113bb8507e6SMatthias Braun // subclass. 114e8f717aeSMatthias Braun TargetPassConfig *PassConfig = TM.createPassConfig(PM); 115bb8507e6SMatthias Braun // Set PassConfig options provided by TargetMachine. 116bb8507e6SMatthias Braun PassConfig->setDisableVerify(DisableVerify); 117bb8507e6SMatthias Braun PM.add(PassConfig); 118cc382cf7SYuanfang Chen PM.add(&MMIWP); 119bb8507e6SMatthias Braun 120bb8507e6SMatthias Braun if (PassConfig->addISelPasses()) 121bb8507e6SMatthias Braun return nullptr; 122bb8507e6SMatthias Braun PassConfig->addMachinePasses(); 123bb8507e6SMatthias Braun PassConfig->setInitialized(); 124e8f717aeSMatthias Braun return PassConfig; 125bb8507e6SMatthias Braun } 126bb8507e6SMatthias Braun 127bb8507e6SMatthias Braun bool LLVMTargetMachine::addAsmPrinter(PassManagerBase &PM, 1289a45114bSPeter Collingbourne raw_pwrite_stream &Out, 1299a45114bSPeter Collingbourne raw_pwrite_stream *DwoOut, 1309a45114bSPeter Collingbourne CodeGenFileType FileType, 131bb8507e6SMatthias Braun MCContext &Context) { 132480936e7SYuanfang Chen Expected<std::unique_ptr<MCStreamer>> MCStreamerOrErr = 133480936e7SYuanfang Chen createMCStreamer(Out, DwoOut, FileType, Context); 134480936e7SYuanfang Chen if (auto Err = MCStreamerOrErr.takeError()) 135480936e7SYuanfang Chen return true; 136480936e7SYuanfang Chen 137480936e7SYuanfang Chen // Create the AsmPrinter, which takes ownership of AsmStreamer if successful. 138480936e7SYuanfang Chen FunctionPass *Printer = 139480936e7SYuanfang Chen getTarget().createAsmPrinter(*this, std::move(*MCStreamerOrErr)); 140480936e7SYuanfang Chen if (!Printer) 141480936e7SYuanfang Chen return true; 142480936e7SYuanfang Chen 143480936e7SYuanfang Chen PM.add(Printer); 144480936e7SYuanfang Chen return false; 145480936e7SYuanfang Chen } 146480936e7SYuanfang Chen 147480936e7SYuanfang Chen Expected<std::unique_ptr<MCStreamer>> LLVMTargetMachine::createMCStreamer( 148480936e7SYuanfang Chen raw_pwrite_stream &Out, raw_pwrite_stream *DwoOut, CodeGenFileType FileType, 149480936e7SYuanfang Chen MCContext &Context) { 150bb8507e6SMatthias Braun if (Options.MCOptions.MCSaveTempLabels) 151bb8507e6SMatthias Braun Context.setAllowTemporaryLabels(false); 152bb8507e6SMatthias Braun 153bb8507e6SMatthias Braun const MCSubtargetInfo &STI = *getMCSubtargetInfo(); 154bb8507e6SMatthias Braun const MCAsmInfo &MAI = *getMCAsmInfo(); 155bb8507e6SMatthias Braun const MCRegisterInfo &MRI = *getMCRegisterInfo(); 156bb8507e6SMatthias Braun const MCInstrInfo &MII = *getMCInstrInfo(); 157bb8507e6SMatthias Braun 158bb8507e6SMatthias Braun std::unique_ptr<MCStreamer> AsmStreamer; 159bb8507e6SMatthias Braun 160bb8507e6SMatthias Braun switch (FileType) { 161bb8507e6SMatthias Braun case CGFT_AssemblyFile: { 162bb8507e6SMatthias Braun MCInstPrinter *InstPrinter = getTarget().createMCInstPrinter( 163bb8507e6SMatthias Braun getTargetTriple(), MAI.getAssemblerDialect(), MAI, MII, MRI); 164bb8507e6SMatthias Braun 165bb8507e6SMatthias Braun // Create a code emitter if asked to show the encoding. 1661b5533c9SNirav Dave std::unique_ptr<MCCodeEmitter> MCE; 167bb8507e6SMatthias Braun if (Options.MCOptions.ShowMCEncoding) 1681b5533c9SNirav Dave MCE.reset(getTarget().createMCCodeEmitter(MII, MRI, Context)); 169bb8507e6SMatthias Braun 1701b5533c9SNirav Dave std::unique_ptr<MCAsmBackend> MAB( 1711b5533c9SNirav Dave getTarget().createMCAsmBackend(STI, MRI, Options.MCOptions)); 1720eaee545SJonas Devlieghere auto FOut = std::make_unique<formatted_raw_ostream>(Out); 173bb8507e6SMatthias Braun MCStreamer *S = getTarget().createAsmStreamer( 174bb8507e6SMatthias Braun Context, std::move(FOut), Options.MCOptions.AsmVerbose, 1751b5533c9SNirav Dave Options.MCOptions.MCUseDwarfDirectory, InstPrinter, std::move(MCE), 1761b5533c9SNirav Dave std::move(MAB), Options.MCOptions.ShowMCInst); 177bb8507e6SMatthias Braun AsmStreamer.reset(S); 178bb8507e6SMatthias Braun break; 179bb8507e6SMatthias Braun } 180bb8507e6SMatthias Braun case CGFT_ObjectFile: { 181bb8507e6SMatthias Braun // Create the code emitter for the target if it exists. If not, .o file 182bb8507e6SMatthias Braun // emission fails. 183bb8507e6SMatthias Braun MCCodeEmitter *MCE = getTarget().createMCCodeEmitter(MII, MRI, Context); 184480936e7SYuanfang Chen if (!MCE) 185480936e7SYuanfang Chen return make_error<StringError>("createMCCodeEmitter failed", 186480936e7SYuanfang Chen inconvertibleErrorCode()); 187bb8507e6SMatthias Braun MCAsmBackend *MAB = 188b22f751fSAlex Bradbury getTarget().createMCAsmBackend(STI, MRI, Options.MCOptions); 189480936e7SYuanfang Chen if (!MAB) 190480936e7SYuanfang Chen return make_error<StringError>("createMCAsmBackend failed", 191480936e7SYuanfang Chen inconvertibleErrorCode()); 192bb8507e6SMatthias Braun 193bb8507e6SMatthias Braun Triple T(getTargetTriple().str()); 194bb8507e6SMatthias Braun AsmStreamer.reset(getTarget().createMCObjectStreamer( 195f7b81db7SPeter Collingbourne T, Context, std::unique_ptr<MCAsmBackend>(MAB), 1969a45114bSPeter Collingbourne DwoOut ? MAB->createDwoObjectWriter(Out, *DwoOut) 1979a45114bSPeter Collingbourne : MAB->createObjectWriter(Out), 1989a45114bSPeter Collingbourne std::unique_ptr<MCCodeEmitter>(MCE), STI, Options.MCOptions.MCRelaxAll, 199bb8507e6SMatthias Braun Options.MCOptions.MCIncrementalLinkerCompatible, 200bb8507e6SMatthias Braun /*DWARFMustBeAtTheEnd*/ true)); 201bb8507e6SMatthias Braun break; 202bb8507e6SMatthias Braun } 203bb8507e6SMatthias Braun case CGFT_Null: 204bb8507e6SMatthias Braun // The Null output is intended for use for performance analysis and testing, 205bb8507e6SMatthias Braun // not real users. 206bb8507e6SMatthias Braun AsmStreamer.reset(getTarget().createNullStreamer(Context)); 207bb8507e6SMatthias Braun break; 208bb8507e6SMatthias Braun } 209bb8507e6SMatthias Braun 210480936e7SYuanfang Chen return std::move(AsmStreamer); 211bb8507e6SMatthias Braun } 212bb8507e6SMatthias Braun 213cc382cf7SYuanfang Chen bool LLVMTargetMachine::addPassesToEmitFile( 214cc382cf7SYuanfang Chen PassManagerBase &PM, raw_pwrite_stream &Out, raw_pwrite_stream *DwoOut, 215cc382cf7SYuanfang Chen CodeGenFileType FileType, bool DisableVerify, 216cc382cf7SYuanfang Chen MachineModuleInfoWrapperPass *MMIWP) { 217bb8507e6SMatthias Braun // Add common CodeGen passes. 218cc382cf7SYuanfang Chen if (!MMIWP) 219cc382cf7SYuanfang Chen MMIWP = new MachineModuleInfoWrapperPass(this); 220e8f717aeSMatthias Braun TargetPassConfig *PassConfig = 221cc382cf7SYuanfang Chen addPassesToGenerateCode(*this, PM, DisableVerify, *MMIWP); 222e8f717aeSMatthias Braun if (!PassConfig) 223bb8507e6SMatthias Braun return true; 224bb8507e6SMatthias Braun 2250c1381d7SJay Foad if (TargetPassConfig::willCompleteCodeGenPipeline()) { 2260c1381d7SJay Foad if (addAsmPrinter(PM, Out, DwoOut, FileType, MMIWP->getMMI().getContext())) 227bb8507e6SMatthias Braun return true; 2280c1381d7SJay Foad } else { 2290c1381d7SJay Foad // MIR printing is redundant with -filetype=null. 2300c1381d7SJay Foad if (FileType != CGFT_Null) 2310c1381d7SJay Foad PM.add(createPrintMIRPass(Out)); 2320c1381d7SJay Foad } 233bb8507e6SMatthias Braun 234bb8507e6SMatthias Braun PM.add(createFreeMachineFunctionPass()); 235bb8507e6SMatthias Braun return false; 236bb8507e6SMatthias Braun } 237bb8507e6SMatthias Braun 238bb8507e6SMatthias Braun /// addPassesToEmitMC - Add passes to the specified pass manager to get 239bb8507e6SMatthias Braun /// machine code emitted with the MCJIT. This method returns true if machine 240bb8507e6SMatthias Braun /// code is not supported. It fills the MCContext Ctx pointer which can be 241bb8507e6SMatthias Braun /// used to build custom MCStreamer. 242bb8507e6SMatthias Braun /// 243bb8507e6SMatthias Braun bool LLVMTargetMachine::addPassesToEmitMC(PassManagerBase &PM, MCContext *&Ctx, 244bb8507e6SMatthias Braun raw_pwrite_stream &Out, 245bb8507e6SMatthias Braun bool DisableVerify) { 246bb8507e6SMatthias Braun // Add common CodeGen passes. 247cc382cf7SYuanfang Chen MachineModuleInfoWrapperPass *MMIWP = new MachineModuleInfoWrapperPass(this); 248e8f717aeSMatthias Braun TargetPassConfig *PassConfig = 249cc382cf7SYuanfang Chen addPassesToGenerateCode(*this, PM, DisableVerify, *MMIWP); 250e8f717aeSMatthias Braun if (!PassConfig) 251bb8507e6SMatthias Braun return true; 252e8f717aeSMatthias Braun assert(TargetPassConfig::willCompleteCodeGenPipeline() && 253e8f717aeSMatthias Braun "Cannot emit MC with limited codegen pipeline"); 254bb8507e6SMatthias Braun 255cc382cf7SYuanfang Chen Ctx = &MMIWP->getMMI().getContext(); 256bb8507e6SMatthias Braun if (Options.MCOptions.MCSaveTempLabels) 257bb8507e6SMatthias Braun Ctx->setAllowTemporaryLabels(false); 258bb8507e6SMatthias Braun 259bb8507e6SMatthias Braun // Create the code emitter for the target if it exists. If not, .o file 260bb8507e6SMatthias Braun // emission fails. 261b22f751fSAlex Bradbury const MCSubtargetInfo &STI = *getMCSubtargetInfo(); 262bb8507e6SMatthias Braun const MCRegisterInfo &MRI = *getMCRegisterInfo(); 263bb8507e6SMatthias Braun MCCodeEmitter *MCE = 264bb8507e6SMatthias Braun getTarget().createMCCodeEmitter(*getMCInstrInfo(), MRI, *Ctx); 265bb8507e6SMatthias Braun MCAsmBackend *MAB = 266b22f751fSAlex Bradbury getTarget().createMCAsmBackend(STI, MRI, Options.MCOptions); 267bb8507e6SMatthias Braun if (!MCE || !MAB) 268bb8507e6SMatthias Braun return true; 269bb8507e6SMatthias Braun 270bb8507e6SMatthias Braun const Triple &T = getTargetTriple(); 271bb8507e6SMatthias Braun std::unique_ptr<MCStreamer> AsmStreamer(getTarget().createMCObjectStreamer( 272f7b81db7SPeter Collingbourne T, *Ctx, std::unique_ptr<MCAsmBackend>(MAB), MAB->createObjectWriter(Out), 273bb8507e6SMatthias Braun std::unique_ptr<MCCodeEmitter>(MCE), STI, Options.MCOptions.MCRelaxAll, 274bb8507e6SMatthias Braun Options.MCOptions.MCIncrementalLinkerCompatible, 275bb8507e6SMatthias Braun /*DWARFMustBeAtTheEnd*/ true)); 276bb8507e6SMatthias Braun 277bb8507e6SMatthias Braun // Create the AsmPrinter, which takes ownership of AsmStreamer if successful. 278bb8507e6SMatthias Braun FunctionPass *Printer = 279bb8507e6SMatthias Braun getTarget().createAsmPrinter(*this, std::move(AsmStreamer)); 280bb8507e6SMatthias Braun if (!Printer) 281bb8507e6SMatthias Braun return true; 282bb8507e6SMatthias Braun 283bb8507e6SMatthias Braun PM.add(Printer); 284bb8507e6SMatthias Braun PM.add(createFreeMachineFunctionPass()); 285bb8507e6SMatthias Braun 286bb8507e6SMatthias Braun return false; // success! 287bb8507e6SMatthias Braun } 288