11756faa4SDylan Noblesmith //===-- TargetSelect.cpp - Target Chooser Code ----------------------------===//
21756faa4SDylan Noblesmith //
31756faa4SDylan Noblesmith //                     The LLVM Compiler Infrastructure
41756faa4SDylan Noblesmith //
51756faa4SDylan Noblesmith // This file is distributed under the University of Illinois Open Source
61756faa4SDylan Noblesmith // License. See LICENSE.TXT for details.
71756faa4SDylan Noblesmith //
81756faa4SDylan Noblesmith //===----------------------------------------------------------------------===//
91756faa4SDylan Noblesmith //
101756faa4SDylan Noblesmith // This just asks the TargetRegistry for the appropriate JIT to use, and allows
111756faa4SDylan Noblesmith // the user to specify a specific one on the commandline with -march=x. Clients
121756faa4SDylan Noblesmith // should initialize targets prior to calling createJIT.
131756faa4SDylan Noblesmith //
141756faa4SDylan Noblesmith //===----------------------------------------------------------------------===//
151756faa4SDylan Noblesmith 
161756faa4SDylan Noblesmith #include "llvm/ExecutionEngine/ExecutionEngine.h"
171756faa4SDylan Noblesmith #include "llvm/Module.h"
181756faa4SDylan Noblesmith #include "llvm/ADT/Triple.h"
198264e272SEvan Cheng #include "llvm/MC/SubtargetFeature.h"
201756faa4SDylan Noblesmith #include "llvm/Support/CommandLine.h"
211756faa4SDylan Noblesmith #include "llvm/Support/raw_ostream.h"
221756faa4SDylan Noblesmith #include "llvm/Support/Host.h"
231756faa4SDylan Noblesmith #include "llvm/Target/TargetMachine.h"
241756faa4SDylan Noblesmith #include "llvm/Target/TargetRegistry.h"
251756faa4SDylan Noblesmith using namespace llvm;
261756faa4SDylan Noblesmith 
271756faa4SDylan Noblesmith /// selectTarget - Pick a target either via -march or by guessing the native
281756faa4SDylan Noblesmith /// arch.  Add any CPU features specified via -mcpu or -mattr.
291756faa4SDylan Noblesmith TargetMachine *EngineBuilder::selectTarget(Module *Mod,
301756faa4SDylan Noblesmith                               StringRef MArch,
311756faa4SDylan Noblesmith                               StringRef MCPU,
321756faa4SDylan Noblesmith                               const SmallVectorImpl<std::string>& MAttrs,
331756faa4SDylan Noblesmith                               std::string *ErrorStr) {
341756faa4SDylan Noblesmith   Triple TheTriple(Mod->getTargetTriple());
351756faa4SDylan Noblesmith   if (TheTriple.getTriple().empty())
361756faa4SDylan Noblesmith     TheTriple.setTriple(sys::getHostTriple());
371756faa4SDylan Noblesmith 
381756faa4SDylan Noblesmith   // Adjust the triple to match what the user requested.
391756faa4SDylan Noblesmith   const Target *TheTarget = 0;
401756faa4SDylan Noblesmith   if (!MArch.empty()) {
411756faa4SDylan Noblesmith     for (TargetRegistry::iterator it = TargetRegistry::begin(),
421756faa4SDylan Noblesmith            ie = TargetRegistry::end(); it != ie; ++it) {
431756faa4SDylan Noblesmith       if (MArch == it->getName()) {
441756faa4SDylan Noblesmith         TheTarget = &*it;
451756faa4SDylan Noblesmith         break;
461756faa4SDylan Noblesmith       }
471756faa4SDylan Noblesmith     }
481756faa4SDylan Noblesmith 
491756faa4SDylan Noblesmith     if (!TheTarget) {
501756faa4SDylan Noblesmith       *ErrorStr = "No available targets are compatible with this -march, "
511756faa4SDylan Noblesmith         "see -version for the available targets.\n";
521756faa4SDylan Noblesmith       return 0;
531756faa4SDylan Noblesmith     }
541756faa4SDylan Noblesmith 
551756faa4SDylan Noblesmith     // Adjust the triple to match (if known), otherwise stick with the
561756faa4SDylan Noblesmith     // module/host triple.
571756faa4SDylan Noblesmith     Triple::ArchType Type = Triple::getArchTypeForLLVMName(MArch);
581756faa4SDylan Noblesmith     if (Type != Triple::UnknownArch)
591756faa4SDylan Noblesmith       TheTriple.setArch(Type);
601756faa4SDylan Noblesmith   } else {
611756faa4SDylan Noblesmith     std::string Error;
621756faa4SDylan Noblesmith     TheTarget = TargetRegistry::lookupTarget(TheTriple.getTriple(), Error);
631756faa4SDylan Noblesmith     if (TheTarget == 0) {
641756faa4SDylan Noblesmith       if (ErrorStr)
651756faa4SDylan Noblesmith         *ErrorStr = Error;
661756faa4SDylan Noblesmith       return 0;
671756faa4SDylan Noblesmith     }
681756faa4SDylan Noblesmith   }
691756faa4SDylan Noblesmith 
701756faa4SDylan Noblesmith   if (!TheTarget->hasJIT()) {
711756faa4SDylan Noblesmith     errs() << "WARNING: This target JIT is not designed for the host you are"
721756faa4SDylan Noblesmith            << " running.  If bad things happen, please choose a different "
731756faa4SDylan Noblesmith            << "-march switch.\n";
741756faa4SDylan Noblesmith   }
751756faa4SDylan Noblesmith 
761756faa4SDylan Noblesmith   // Package up features to be passed to target/subtarget
771756faa4SDylan Noblesmith   std::string FeaturesStr;
78*fe6e405eSEvan Cheng   if (!MAttrs.empty()) {
791756faa4SDylan Noblesmith     SubtargetFeatures Features;
801756faa4SDylan Noblesmith     for (unsigned i = 0; i != MAttrs.size(); ++i)
811756faa4SDylan Noblesmith       Features.AddFeature(MAttrs[i]);
821756faa4SDylan Noblesmith     FeaturesStr = Features.getString();
831756faa4SDylan Noblesmith   }
841756faa4SDylan Noblesmith 
851756faa4SDylan Noblesmith   // Allocate a target...
861756faa4SDylan Noblesmith   TargetMachine *Target =
87*fe6e405eSEvan Cheng     TheTarget->createTargetMachine(TheTriple.getTriple(), MCPU, FeaturesStr);
881756faa4SDylan Noblesmith   assert(Target && "Could not allocate target machine!");
891756faa4SDylan Noblesmith   return Target;
901756faa4SDylan Noblesmith }
91