Home | History | Annotate | Download | only in ExecutionEngine
      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 TargetMachine *EngineBuilder::selectTarget() {
     28   Triple TT;
     29 
     30   // MCJIT can generate code for remote targets, but the old JIT and Interpreter
     31   // must use the host architecture.
     32   if (WhichEngine != EngineKind::Interpreter && M)
     33     TT.setTriple(M->getTargetTriple());
     34 
     35   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 TargetMachine *EngineBuilder::selectTarget(const Triple &TargetTriple,
     41                               StringRef MArch,
     42                               StringRef MCPU,
     43                               const SmallVectorImpl<std::string>& MAttrs) {
     44   Triple TheTriple(TargetTriple);
     45   if (TheTriple.getTriple().empty())
     46     TheTriple.setTriple(sys::getProcessTriple());
     47 
     48   // Adjust the triple to match what the user requested.
     49   const Target *TheTarget = nullptr;
     50   if (!MArch.empty()) {
     51     auto I = find_if(TargetRegistry::targets(),
     52                      [&](const Target &T) { return MArch == T.getName(); });
     53 
     54     if (I == TargetRegistry::targets().end()) {
     55       if (ErrorStr)
     56         *ErrorStr = "No available targets are compatible with this -march, "
     57                     "see -version for the available targets.\n";
     58       return nullptr;
     59     }
     60 
     61     TheTarget = &*I;
     62 
     63     // Adjust the triple to match (if known), otherwise stick with the
     64     // requested/host triple.
     65     Triple::ArchType Type = Triple::getArchTypeForLLVMName(MArch);
     66     if (Type != Triple::UnknownArch)
     67       TheTriple.setArch(Type);
     68   } else {
     69     std::string Error;
     70     TheTarget = TargetRegistry::lookupTarget(TheTriple.getTriple(), Error);
     71     if (!TheTarget) {
     72       if (ErrorStr)
     73         *ErrorStr = Error;
     74       return nullptr;
     75     }
     76   }
     77 
     78   // Package up features to be passed to target/subtarget
     79   std::string FeaturesStr;
     80   if (!MAttrs.empty()) {
     81     SubtargetFeatures Features;
     82     for (unsigned i = 0; i != MAttrs.size(); ++i)
     83       Features.AddFeature(MAttrs[i]);
     84     FeaturesStr = Features.getString();
     85   }
     86 
     87   // FIXME: non-iOS ARM FastISel is broken with MCJIT.
     88   if (TheTriple.getArch() == Triple::arm &&
     89       !TheTriple.isiOS() &&
     90       OptLevel == CodeGenOpt::None) {
     91     OptLevel = CodeGenOpt::Less;
     92   }
     93 
     94   // Allocate a target...
     95   TargetMachine *Target =
     96       TheTarget->createTargetMachine(TheTriple.getTriple(), MCPU, FeaturesStr,
     97                                      Options, RelocModel, CMModel, OptLevel,
     98 				     /*JIT*/ true);
     99   Target->Options.EmulatedTLS = EmulatedTLS;
    100   Target->Options.ExplicitEmulatedTLS = true;
    101 
    102   assert(Target && "Could not allocate target machine!");
    103   return Target;
    104 }
    105