LCOV - code coverage report
Current view: top level - lib/ExecutionEngine - TargetSelect.cpp (source / functions) Hit Total Coverage
Test: llvm-toolchain.info Lines: 28 39 71.8 %
Date: 2017-09-14 15:23:50 Functions: 2 3 66.7 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : //===-- TargetSelect.cpp - Target Chooser Code ----------------------------===//
       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 just asks the TargetRegistry for the appropriate target to use, and
      11             : // allows the user to specify a specific one on the commandline with -march=x,
      12             : // -mcpu=y, and -mattr=a,-b,+c. Clients should initialize targets prior to
      13             : // calling selectTarget().
      14             : //
      15             : //===----------------------------------------------------------------------===//
      16             : 
      17             : #include "llvm/ADT/Triple.h"
      18             : #include "llvm/ExecutionEngine/ExecutionEngine.h"
      19             : #include "llvm/IR/Module.h"
      20             : #include "llvm/MC/SubtargetFeature.h"
      21             : #include "llvm/Support/Host.h"
      22             : #include "llvm/Support/TargetRegistry.h"
      23             : #include "llvm/Target/TargetMachine.h"
      24             : 
      25             : using namespace llvm;
      26             : 
      27         240 : TargetMachine *EngineBuilder::selectTarget() {
      28         480 :   Triple TT;
      29             : 
      30             :   // MCJIT can generate code for remote targets, but the old JIT and Interpreter
      31             :   // must use the host architecture.
      32         463 :   if (WhichEngine != EngineKind::Interpreter && M)
      33         812 :     TT.setTriple(M->getTargetTriple());
      34             : 
      35         960 :   return selectTarget(TT, MArch, MCPU, MAttrs);
      36             : }
      37             : 
      38             : /// selectTarget - Pick a target either via -march or by guessing the native
      39             : /// arch.  Add any CPU features specified via -mcpu or -mattr.
      40         253 : TargetMachine *EngineBuilder::selectTarget(const Triple &TargetTriple,
      41             :                               StringRef MArch,
      42             :                               StringRef MCPU,
      43             :                               const SmallVectorImpl<std::string>& MAttrs) {
      44         253 :   Triple TheTriple(TargetTriple);
      45         253 :   if (TheTriple.getTriple().empty())
      46         612 :     TheTriple.setTriple(sys::getProcessTriple());
      47             : 
      48             :   // Adjust the triple to match what the user requested.
      49         253 :   const Target *TheTarget = nullptr;
      50         253 :   if (!MArch.empty()) {
      51           0 :     auto I = find_if(TargetRegistry::targets(),
      52           0 :                      [&](const Target &T) { return MArch == T.getName(); });
      53             : 
      54           0 :     if (I == TargetRegistry::targets().end()) {
      55           0 :       if (ErrorStr)
      56           0 :         *ErrorStr = "No available targets are compatible with this -march, "
      57             :                     "see -version for the available targets.\n";
      58             :       return nullptr;
      59             :     }
      60             : 
      61           0 :     TheTarget = &*I;
      62             : 
      63             :     // Adjust the triple to match (if known), otherwise stick with the
      64             :     // requested/host triple.
      65           0 :     Triple::ArchType Type = Triple::getArchTypeForLLVMName(MArch);
      66           0 :     if (Type != Triple::UnknownArch)
      67           0 :       TheTriple.setArch(Type);
      68             :   } else {
      69         501 :     std::string Error;
      70         253 :     TheTarget = TargetRegistry::lookupTarget(TheTriple.getTriple(), Error);
      71         253 :     if (!TheTarget) {
      72           5 :       if (ErrorStr)
      73           5 :         *ErrorStr = Error;
      74          10 :       return nullptr;
      75             :     }
      76             :   }
      77             : 
      78             :   // Package up features to be passed to target/subtarget
      79         248 :   std::string FeaturesStr;
      80         248 :   if (!MAttrs.empty()) {
      81          24 :     SubtargetFeatures Features;
      82         120 :     for (unsigned i = 0; i != MAttrs.size(); ++i)
      83         144 :       Features.AddFeature(MAttrs[i]);
      84          24 :     FeaturesStr = Features.getString();
      85             :   }
      86             : 
      87             :   // FIXME: non-iOS ARM FastISel is broken with MCJIT.
      88         248 :   if (TheTriple.getArch() == Triple::arm &&
      89         248 :       !TheTriple.isiOS() &&
      90           0 :       OptLevel == CodeGenOpt::None) {
      91           0 :     OptLevel = CodeGenOpt::Less;
      92             :   }
      93             : 
      94             :   // Allocate a target...
      95             :   TargetMachine *Target =
      96        1488 :       TheTarget->createTargetMachine(TheTriple.getTriple(), MCPU, FeaturesStr,
      97             :                                      Options, RelocModel, CMModel, OptLevel,
      98         248 :                                      /*JIT*/ true);
      99             :   assert(Target && "Could not allocate target machine!");
     100         248 :   return Target;
     101             : }

Generated by: LCOV version 1.13