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