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" 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" 326054e650SDavid 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() { 4210a21625SFangrui Song MRI.reset(TheTarget.createMCRegInfo(getTargetTriple().str())); 4310a21625SFangrui Song MII.reset(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. 4810a21625SFangrui Song STI.reset(TheTarget.createMCSubtargetInfo( 4910a21625SFangrui Song getTargetTriple().str(), getTargetCPU(), 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 7310a21625SFangrui Song AsmInfo.reset(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. 97e8f717aeSMatthias Braun static TargetPassConfig * 98e8f717aeSMatthias Braun addPassesToGenerateCode(LLVMTargetMachine &TM, PassManagerBase &PM, 99*cc382cf7SYuanfang Chen bool DisableVerify, 100*cc382cf7SYuanfang Chen MachineModuleInfoWrapperPass &MMIWP) { 101bb8507e6SMatthias Braun // Targets may override createPassConfig to provide a target-specific 102bb8507e6SMatthias Braun // subclass. 103e8f717aeSMatthias Braun TargetPassConfig *PassConfig = TM.createPassConfig(PM); 104bb8507e6SMatthias Braun // Set PassConfig options provided by TargetMachine. 105bb8507e6SMatthias Braun PassConfig->setDisableVerify(DisableVerify); 106bb8507e6SMatthias Braun PM.add(PassConfig); 107*cc382cf7SYuanfang Chen PM.add(&MMIWP); 108bb8507e6SMatthias Braun 109bb8507e6SMatthias Braun if (PassConfig->addISelPasses()) 110bb8507e6SMatthias Braun return nullptr; 111bb8507e6SMatthias Braun PassConfig->addMachinePasses(); 112bb8507e6SMatthias Braun PassConfig->setInitialized(); 113e8f717aeSMatthias Braun return PassConfig; 114bb8507e6SMatthias Braun } 115bb8507e6SMatthias Braun 116bb8507e6SMatthias Braun bool LLVMTargetMachine::addAsmPrinter(PassManagerBase &PM, 1179a45114bSPeter Collingbourne raw_pwrite_stream &Out, 1189a45114bSPeter Collingbourne raw_pwrite_stream *DwoOut, 1199a45114bSPeter Collingbourne CodeGenFileType FileType, 120bb8507e6SMatthias Braun MCContext &Context) { 121bb8507e6SMatthias Braun if (Options.MCOptions.MCSaveTempLabels) 122bb8507e6SMatthias Braun Context.setAllowTemporaryLabels(false); 123bb8507e6SMatthias Braun 124bb8507e6SMatthias Braun const MCSubtargetInfo &STI = *getMCSubtargetInfo(); 125bb8507e6SMatthias Braun const MCAsmInfo &MAI = *getMCAsmInfo(); 126bb8507e6SMatthias Braun const MCRegisterInfo &MRI = *getMCRegisterInfo(); 127bb8507e6SMatthias Braun const MCInstrInfo &MII = *getMCInstrInfo(); 128bb8507e6SMatthias Braun 129bb8507e6SMatthias Braun std::unique_ptr<MCStreamer> AsmStreamer; 130bb8507e6SMatthias Braun 131bb8507e6SMatthias Braun switch (FileType) { 132bb8507e6SMatthias Braun case CGFT_AssemblyFile: { 133bb8507e6SMatthias Braun MCInstPrinter *InstPrinter = getTarget().createMCInstPrinter( 134bb8507e6SMatthias Braun getTargetTriple(), MAI.getAssemblerDialect(), MAI, MII, MRI); 135bb8507e6SMatthias Braun 136bb8507e6SMatthias Braun // Create a code emitter if asked to show the encoding. 1371b5533c9SNirav Dave std::unique_ptr<MCCodeEmitter> MCE; 138bb8507e6SMatthias Braun if (Options.MCOptions.ShowMCEncoding) 1391b5533c9SNirav Dave MCE.reset(getTarget().createMCCodeEmitter(MII, MRI, Context)); 140bb8507e6SMatthias Braun 1411b5533c9SNirav Dave std::unique_ptr<MCAsmBackend> MAB( 1421b5533c9SNirav Dave getTarget().createMCAsmBackend(STI, MRI, Options.MCOptions)); 1430eaee545SJonas Devlieghere auto FOut = std::make_unique<formatted_raw_ostream>(Out); 144bb8507e6SMatthias Braun MCStreamer *S = getTarget().createAsmStreamer( 145bb8507e6SMatthias Braun Context, std::move(FOut), Options.MCOptions.AsmVerbose, 1461b5533c9SNirav Dave Options.MCOptions.MCUseDwarfDirectory, InstPrinter, std::move(MCE), 1471b5533c9SNirav Dave std::move(MAB), Options.MCOptions.ShowMCInst); 148bb8507e6SMatthias Braun AsmStreamer.reset(S); 149bb8507e6SMatthias Braun break; 150bb8507e6SMatthias Braun } 151bb8507e6SMatthias Braun case CGFT_ObjectFile: { 152bb8507e6SMatthias Braun // Create the code emitter for the target if it exists. If not, .o file 153bb8507e6SMatthias Braun // emission fails. 154bb8507e6SMatthias Braun MCCodeEmitter *MCE = getTarget().createMCCodeEmitter(MII, MRI, Context); 155bb8507e6SMatthias Braun MCAsmBackend *MAB = 156b22f751fSAlex Bradbury getTarget().createMCAsmBackend(STI, MRI, Options.MCOptions); 157bb8507e6SMatthias Braun if (!MCE || !MAB) 158bb8507e6SMatthias Braun return true; 159bb8507e6SMatthias Braun 160bb8507e6SMatthias Braun // Don't waste memory on names of temp labels. 161bb8507e6SMatthias Braun Context.setUseNamesOnTempLabels(false); 162bb8507e6SMatthias Braun 163bb8507e6SMatthias Braun Triple T(getTargetTriple().str()); 164bb8507e6SMatthias Braun AsmStreamer.reset(getTarget().createMCObjectStreamer( 165f7b81db7SPeter Collingbourne T, Context, std::unique_ptr<MCAsmBackend>(MAB), 1669a45114bSPeter Collingbourne DwoOut ? MAB->createDwoObjectWriter(Out, *DwoOut) 1679a45114bSPeter Collingbourne : MAB->createObjectWriter(Out), 1689a45114bSPeter Collingbourne std::unique_ptr<MCCodeEmitter>(MCE), STI, Options.MCOptions.MCRelaxAll, 169bb8507e6SMatthias Braun Options.MCOptions.MCIncrementalLinkerCompatible, 170bb8507e6SMatthias Braun /*DWARFMustBeAtTheEnd*/ true)); 171bb8507e6SMatthias Braun break; 172bb8507e6SMatthias Braun } 173bb8507e6SMatthias Braun case CGFT_Null: 174bb8507e6SMatthias Braun // The Null output is intended for use for performance analysis and testing, 175bb8507e6SMatthias Braun // not real users. 176bb8507e6SMatthias Braun AsmStreamer.reset(getTarget().createNullStreamer(Context)); 177bb8507e6SMatthias Braun break; 178bb8507e6SMatthias Braun } 179bb8507e6SMatthias Braun 180bb8507e6SMatthias Braun // Create the AsmPrinter, which takes ownership of AsmStreamer if successful. 181bb8507e6SMatthias Braun FunctionPass *Printer = 182bb8507e6SMatthias Braun getTarget().createAsmPrinter(*this, std::move(AsmStreamer)); 183bb8507e6SMatthias Braun if (!Printer) 184bb8507e6SMatthias Braun return true; 185bb8507e6SMatthias Braun 186bb8507e6SMatthias Braun PM.add(Printer); 187bb8507e6SMatthias Braun return false; 188bb8507e6SMatthias Braun } 189bb8507e6SMatthias Braun 190*cc382cf7SYuanfang Chen bool LLVMTargetMachine::addPassesToEmitFile( 191*cc382cf7SYuanfang Chen PassManagerBase &PM, raw_pwrite_stream &Out, raw_pwrite_stream *DwoOut, 192*cc382cf7SYuanfang Chen CodeGenFileType FileType, bool DisableVerify, 193*cc382cf7SYuanfang Chen MachineModuleInfoWrapperPass *MMIWP) { 194bb8507e6SMatthias Braun // Add common CodeGen passes. 195*cc382cf7SYuanfang Chen if (!MMIWP) 196*cc382cf7SYuanfang Chen MMIWP = new MachineModuleInfoWrapperPass(this); 197e8f717aeSMatthias Braun TargetPassConfig *PassConfig = 198*cc382cf7SYuanfang Chen addPassesToGenerateCode(*this, PM, DisableVerify, *MMIWP); 199e8f717aeSMatthias Braun if (!PassConfig) 200bb8507e6SMatthias Braun return true; 201bb8507e6SMatthias Braun 202e8f717aeSMatthias Braun if (!TargetPassConfig::willCompleteCodeGenPipeline()) { 20360ec2481SJason Liu if (this->getTargetTriple().isOSAIX()) { 20460ec2481SJason Liu // On AIX, we might manifest MCSymbols during SDAG lowering. For MIR 20560ec2481SJason Liu // testing to be meaningful, we need to ensure that the symbols created 20660ec2481SJason Liu // are MCSymbolXCOFF variants, which requires that 20760ec2481SJason Liu // the TargetLoweringObjectFile instance has been initialized. 208*cc382cf7SYuanfang Chen MCContext &Ctx = MMIWP->getMMI().getContext(); 20960ec2481SJason Liu const_cast<TargetLoweringObjectFile &>(*this->getObjFileLowering()) 21060ec2481SJason Liu .Initialize(Ctx, *this); 21160ec2481SJason Liu } 212e8f717aeSMatthias Braun PM.add(createPrintMIRPass(Out)); 213*cc382cf7SYuanfang Chen } else if (addAsmPrinter(PM, Out, DwoOut, FileType, 214*cc382cf7SYuanfang Chen MMIWP->getMMI().getContext())) 215bb8507e6SMatthias Braun return true; 216bb8507e6SMatthias Braun 217bb8507e6SMatthias Braun PM.add(createFreeMachineFunctionPass()); 218bb8507e6SMatthias Braun return false; 219bb8507e6SMatthias Braun } 220bb8507e6SMatthias Braun 221bb8507e6SMatthias Braun /// addPassesToEmitMC - Add passes to the specified pass manager to get 222bb8507e6SMatthias Braun /// machine code emitted with the MCJIT. This method returns true if machine 223bb8507e6SMatthias Braun /// code is not supported. It fills the MCContext Ctx pointer which can be 224bb8507e6SMatthias Braun /// used to build custom MCStreamer. 225bb8507e6SMatthias Braun /// 226bb8507e6SMatthias Braun bool LLVMTargetMachine::addPassesToEmitMC(PassManagerBase &PM, MCContext *&Ctx, 227bb8507e6SMatthias Braun raw_pwrite_stream &Out, 228bb8507e6SMatthias Braun bool DisableVerify) { 229bb8507e6SMatthias Braun // Add common CodeGen passes. 230*cc382cf7SYuanfang Chen MachineModuleInfoWrapperPass *MMIWP = new MachineModuleInfoWrapperPass(this); 231e8f717aeSMatthias Braun TargetPassConfig *PassConfig = 232*cc382cf7SYuanfang Chen addPassesToGenerateCode(*this, PM, DisableVerify, *MMIWP); 233e8f717aeSMatthias Braun if (!PassConfig) 234bb8507e6SMatthias Braun return true; 235e8f717aeSMatthias Braun assert(TargetPassConfig::willCompleteCodeGenPipeline() && 236e8f717aeSMatthias Braun "Cannot emit MC with limited codegen pipeline"); 237bb8507e6SMatthias Braun 238*cc382cf7SYuanfang Chen Ctx = &MMIWP->getMMI().getContext(); 239bb8507e6SMatthias Braun if (Options.MCOptions.MCSaveTempLabels) 240bb8507e6SMatthias Braun Ctx->setAllowTemporaryLabels(false); 241bb8507e6SMatthias Braun 242bb8507e6SMatthias Braun // Create the code emitter for the target if it exists. If not, .o file 243bb8507e6SMatthias Braun // emission fails. 244b22f751fSAlex Bradbury const MCSubtargetInfo &STI = *getMCSubtargetInfo(); 245bb8507e6SMatthias Braun const MCRegisterInfo &MRI = *getMCRegisterInfo(); 246bb8507e6SMatthias Braun MCCodeEmitter *MCE = 247bb8507e6SMatthias Braun getTarget().createMCCodeEmitter(*getMCInstrInfo(), MRI, *Ctx); 248bb8507e6SMatthias Braun MCAsmBackend *MAB = 249b22f751fSAlex Bradbury getTarget().createMCAsmBackend(STI, MRI, Options.MCOptions); 250bb8507e6SMatthias Braun if (!MCE || !MAB) 251bb8507e6SMatthias Braun return true; 252bb8507e6SMatthias Braun 253bb8507e6SMatthias Braun const Triple &T = getTargetTriple(); 254bb8507e6SMatthias Braun std::unique_ptr<MCStreamer> AsmStreamer(getTarget().createMCObjectStreamer( 255f7b81db7SPeter Collingbourne T, *Ctx, std::unique_ptr<MCAsmBackend>(MAB), MAB->createObjectWriter(Out), 256bb8507e6SMatthias Braun std::unique_ptr<MCCodeEmitter>(MCE), STI, Options.MCOptions.MCRelaxAll, 257bb8507e6SMatthias Braun Options.MCOptions.MCIncrementalLinkerCompatible, 258bb8507e6SMatthias Braun /*DWARFMustBeAtTheEnd*/ true)); 259bb8507e6SMatthias Braun 260bb8507e6SMatthias Braun // Create the AsmPrinter, which takes ownership of AsmStreamer if successful. 261bb8507e6SMatthias Braun FunctionPass *Printer = 262bb8507e6SMatthias Braun getTarget().createAsmPrinter(*this, std::move(AsmStreamer)); 263bb8507e6SMatthias Braun if (!Printer) 264bb8507e6SMatthias Braun return true; 265bb8507e6SMatthias Braun 266bb8507e6SMatthias Braun PM.add(Printer); 267bb8507e6SMatthias Braun PM.add(createFreeMachineFunctionPass()); 268bb8507e6SMatthias Braun 269bb8507e6SMatthias Braun return false; // success! 270bb8507e6SMatthias Braun } 271