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, 99e8f717aeSMatthias Braun bool DisableVerify, MachineModuleInfo &MMI) { 100bb8507e6SMatthias Braun // Targets may override createPassConfig to provide a target-specific 101bb8507e6SMatthias Braun // subclass. 102e8f717aeSMatthias Braun TargetPassConfig *PassConfig = TM.createPassConfig(PM); 103bb8507e6SMatthias Braun // Set PassConfig options provided by TargetMachine. 104bb8507e6SMatthias Braun PassConfig->setDisableVerify(DisableVerify); 105bb8507e6SMatthias Braun PM.add(PassConfig); 106e8f717aeSMatthias Braun PM.add(&MMI); 107bb8507e6SMatthias Braun 108bb8507e6SMatthias Braun if (PassConfig->addISelPasses()) 109bb8507e6SMatthias Braun return nullptr; 110bb8507e6SMatthias Braun PassConfig->addMachinePasses(); 111bb8507e6SMatthias Braun PassConfig->setInitialized(); 112e8f717aeSMatthias Braun return PassConfig; 113bb8507e6SMatthias Braun } 114bb8507e6SMatthias Braun 115bb8507e6SMatthias Braun bool LLVMTargetMachine::addAsmPrinter(PassManagerBase &PM, 1169a45114bSPeter Collingbourne raw_pwrite_stream &Out, 1179a45114bSPeter Collingbourne raw_pwrite_stream *DwoOut, 1189a45114bSPeter Collingbourne CodeGenFileType FileType, 119bb8507e6SMatthias Braun MCContext &Context) { 120bb8507e6SMatthias Braun if (Options.MCOptions.MCSaveTempLabels) 121bb8507e6SMatthias Braun Context.setAllowTemporaryLabels(false); 122bb8507e6SMatthias Braun 123bb8507e6SMatthias Braun const MCSubtargetInfo &STI = *getMCSubtargetInfo(); 124bb8507e6SMatthias Braun const MCAsmInfo &MAI = *getMCAsmInfo(); 125bb8507e6SMatthias Braun const MCRegisterInfo &MRI = *getMCRegisterInfo(); 126bb8507e6SMatthias Braun const MCInstrInfo &MII = *getMCInstrInfo(); 127bb8507e6SMatthias Braun 128bb8507e6SMatthias Braun std::unique_ptr<MCStreamer> AsmStreamer; 129bb8507e6SMatthias Braun 130bb8507e6SMatthias Braun switch (FileType) { 131bb8507e6SMatthias Braun case CGFT_AssemblyFile: { 132bb8507e6SMatthias Braun MCInstPrinter *InstPrinter = getTarget().createMCInstPrinter( 133bb8507e6SMatthias Braun getTargetTriple(), MAI.getAssemblerDialect(), MAI, MII, MRI); 134bb8507e6SMatthias Braun 135bb8507e6SMatthias Braun // Create a code emitter if asked to show the encoding. 1361b5533c9SNirav Dave std::unique_ptr<MCCodeEmitter> MCE; 137bb8507e6SMatthias Braun if (Options.MCOptions.ShowMCEncoding) 1381b5533c9SNirav Dave MCE.reset(getTarget().createMCCodeEmitter(MII, MRI, Context)); 139bb8507e6SMatthias Braun 1401b5533c9SNirav Dave std::unique_ptr<MCAsmBackend> MAB( 1411b5533c9SNirav Dave getTarget().createMCAsmBackend(STI, MRI, Options.MCOptions)); 142*0eaee545SJonas Devlieghere auto FOut = std::make_unique<formatted_raw_ostream>(Out); 143bb8507e6SMatthias Braun MCStreamer *S = getTarget().createAsmStreamer( 144bb8507e6SMatthias Braun Context, std::move(FOut), Options.MCOptions.AsmVerbose, 1451b5533c9SNirav Dave Options.MCOptions.MCUseDwarfDirectory, InstPrinter, std::move(MCE), 1461b5533c9SNirav Dave std::move(MAB), Options.MCOptions.ShowMCInst); 147bb8507e6SMatthias Braun AsmStreamer.reset(S); 148bb8507e6SMatthias Braun break; 149bb8507e6SMatthias Braun } 150bb8507e6SMatthias Braun case CGFT_ObjectFile: { 151bb8507e6SMatthias Braun // Create the code emitter for the target if it exists. If not, .o file 152bb8507e6SMatthias Braun // emission fails. 153bb8507e6SMatthias Braun MCCodeEmitter *MCE = getTarget().createMCCodeEmitter(MII, MRI, Context); 154bb8507e6SMatthias Braun MCAsmBackend *MAB = 155b22f751fSAlex Bradbury getTarget().createMCAsmBackend(STI, MRI, 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( 164f7b81db7SPeter Collingbourne T, Context, std::unique_ptr<MCAsmBackend>(MAB), 1659a45114bSPeter Collingbourne DwoOut ? MAB->createDwoObjectWriter(Out, *DwoOut) 1669a45114bSPeter Collingbourne : MAB->createObjectWriter(Out), 1679a45114bSPeter Collingbourne std::unique_ptr<MCCodeEmitter>(MCE), STI, Options.MCOptions.MCRelaxAll, 168bb8507e6SMatthias Braun Options.MCOptions.MCIncrementalLinkerCompatible, 169bb8507e6SMatthias Braun /*DWARFMustBeAtTheEnd*/ true)); 170bb8507e6SMatthias Braun break; 171bb8507e6SMatthias Braun } 172bb8507e6SMatthias Braun case CGFT_Null: 173bb8507e6SMatthias Braun // The Null output is intended for use for performance analysis and testing, 174bb8507e6SMatthias Braun // not real users. 175bb8507e6SMatthias Braun AsmStreamer.reset(getTarget().createNullStreamer(Context)); 176bb8507e6SMatthias Braun break; 177bb8507e6SMatthias Braun } 178bb8507e6SMatthias Braun 179bb8507e6SMatthias Braun // Create the AsmPrinter, which takes ownership of AsmStreamer if successful. 180bb8507e6SMatthias Braun FunctionPass *Printer = 181bb8507e6SMatthias Braun getTarget().createAsmPrinter(*this, std::move(AsmStreamer)); 182bb8507e6SMatthias Braun if (!Printer) 183bb8507e6SMatthias Braun return true; 184bb8507e6SMatthias Braun 185bb8507e6SMatthias Braun PM.add(Printer); 186bb8507e6SMatthias Braun return false; 187bb8507e6SMatthias Braun } 188bb8507e6SMatthias Braun 189bb8507e6SMatthias Braun bool LLVMTargetMachine::addPassesToEmitFile(PassManagerBase &PM, 190bb8507e6SMatthias Braun raw_pwrite_stream &Out, 1919a45114bSPeter Collingbourne raw_pwrite_stream *DwoOut, 192bb8507e6SMatthias Braun CodeGenFileType FileType, 193bb8507e6SMatthias Braun bool DisableVerify, 194bb8507e6SMatthias Braun MachineModuleInfo *MMI) { 195bb8507e6SMatthias Braun // Add common CodeGen passes. 196e8f717aeSMatthias Braun if (!MMI) 197e8f717aeSMatthias Braun MMI = new MachineModuleInfo(this); 198e8f717aeSMatthias Braun TargetPassConfig *PassConfig = 199e8f717aeSMatthias Braun addPassesToGenerateCode(*this, PM, DisableVerify, *MMI); 200e8f717aeSMatthias Braun if (!PassConfig) 201bb8507e6SMatthias Braun return true; 202bb8507e6SMatthias Braun 203e8f717aeSMatthias Braun if (!TargetPassConfig::willCompleteCodeGenPipeline()) { 20460ec2481SJason Liu if (this->getTargetTriple().isOSAIX()) { 20560ec2481SJason Liu // On AIX, we might manifest MCSymbols during SDAG lowering. For MIR 20660ec2481SJason Liu // testing to be meaningful, we need to ensure that the symbols created 20760ec2481SJason Liu // are MCSymbolXCOFF variants, which requires that 20860ec2481SJason Liu // the TargetLoweringObjectFile instance has been initialized. 20960ec2481SJason Liu MCContext &Ctx = MMI->getContext(); 21060ec2481SJason Liu const_cast<TargetLoweringObjectFile &>(*this->getObjFileLowering()) 21160ec2481SJason Liu .Initialize(Ctx, *this); 21260ec2481SJason Liu } 213e8f717aeSMatthias Braun PM.add(createPrintMIRPass(Out)); 214e8f717aeSMatthias Braun } else if (addAsmPrinter(PM, Out, DwoOut, FileType, MMI->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. 230e8f717aeSMatthias Braun MachineModuleInfo *MMI = new MachineModuleInfo(this); 231e8f717aeSMatthias Braun TargetPassConfig *PassConfig = 232e8f717aeSMatthias Braun addPassesToGenerateCode(*this, PM, DisableVerify, *MMI); 233e8f717aeSMatthias Braun if (!PassConfig) 234bb8507e6SMatthias Braun return true; 235e8f717aeSMatthias Braun assert(TargetPassConfig::willCompleteCodeGenPipeline() && 236e8f717aeSMatthias Braun "Cannot emit MC with limited codegen pipeline"); 237bb8507e6SMatthias Braun 238e8f717aeSMatthias Braun Ctx = &MMI->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