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