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 = std::find_if(
     52         TargetRegistry::targets().begin(), TargetRegistry::targets().end(),
     53         [&](const Target &T) { return MArch == T.getName(); });
     54 
     55     if (I == TargetRegistry::targets().end()) {
     56       if (ErrorStr)
     57         *ErrorStr = "No available targets are compatible with this -march, "
     58                     "see -version for the available targets.\n";
     59       return nullptr;
     60     }
     61 
     62     TheTarget = &*I;
     63 
     64     // Adjust the triple to match (if known), otherwise stick with the
     65     // requested/host triple.
     66     Triple::ArchType Type = Triple::getArchTypeForLLVMName(MArch);
     67     if (Type != Triple::UnknownArch)
     68       TheTriple.setArch(Type);
     69   } else {
     70     std::string Error;
     71     TheTarget = TargetRegistry::lookupTarget(TheTriple.getTriple(), Error);
     72     if (!TheTarget) {
     73       if (ErrorStr)
     74         *ErrorStr = Error;
     75       return nullptr;
     76     }
     77   }
     78 
     79   // Package up features to be passed to target/subtarget
     80   std::string FeaturesStr;
     81   if (!MAttrs.empty()) {
     82     SubtargetFeatures Features;
     83     for (unsigned i = 0; i != MAttrs.size(); ++i)
     84       Features.AddFeature(MAttrs[i]);
     85     FeaturesStr = Features.getString();
     86   }
     87 
     88   // FIXME: non-iOS ARM FastISel is broken with MCJIT.
     89   if (TheTriple.getArch() == Triple::arm &&
     90       !TheTriple.isiOS() &&
     91       OptLevel == CodeGenOpt::None) {
     92     OptLevel = CodeGenOpt::Less;
     93   }
     94 
     95   // Allocate a target...
     96   TargetMachine *Target = TheTarget->createTargetMachine(TheTriple.getTriple(),
     97                                                          MCPU, FeaturesStr,
     98                                                          Options,
     99                                                          RelocModel, CMModel,
    100                                                          OptLevel);
    101   assert(Target && "Could not allocate target machine!");
    102   return Target;
    103 }
    104