LCOV - code coverage report
Current view: top level - lib/CodeGen - LLVMTargetMachine.cpp (source / functions) Hit Total Coverage
Test: llvm-toolchain.info Lines: 106 108 98.1 %
Date: 2017-09-14 15:23:50 Functions: 8 8 100.0 %
Legend: Lines: hit not hit

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

Generated by: LCOV version 1.13