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,
33*2129f596SEvan Cheng                               Reloc::Model RM,
341756faa4SDylan Noblesmith                               std::string *ErrorStr) {
351756faa4SDylan Noblesmith   Triple TheTriple(Mod->getTargetTriple());
361756faa4SDylan Noblesmith   if (TheTriple.getTriple().empty())
371756faa4SDylan Noblesmith     TheTriple.setTriple(sys::getHostTriple());
381756faa4SDylan Noblesmith 
391756faa4SDylan Noblesmith   // Adjust the triple to match what the user requested.
401756faa4SDylan Noblesmith   const Target *TheTarget = 0;
411756faa4SDylan Noblesmith   if (!MArch.empty()) {
421756faa4SDylan Noblesmith     for (TargetRegistry::iterator it = TargetRegistry::begin(),
431756faa4SDylan Noblesmith            ie = TargetRegistry::end(); it != ie; ++it) {
441756faa4SDylan Noblesmith       if (MArch == it->getName()) {
451756faa4SDylan Noblesmith         TheTarget = &*it;
461756faa4SDylan Noblesmith         break;
471756faa4SDylan Noblesmith       }
481756faa4SDylan Noblesmith     }
491756faa4SDylan Noblesmith 
501756faa4SDylan Noblesmith     if (!TheTarget) {
511756faa4SDylan Noblesmith       *ErrorStr = "No available targets are compatible with this -march, "
521756faa4SDylan Noblesmith         "see -version for the available targets.\n";
531756faa4SDylan Noblesmith       return 0;
541756faa4SDylan Noblesmith     }
551756faa4SDylan Noblesmith 
561756faa4SDylan Noblesmith     // Adjust the triple to match (if known), otherwise stick with the
571756faa4SDylan Noblesmith     // module/host triple.
581756faa4SDylan Noblesmith     Triple::ArchType Type = Triple::getArchTypeForLLVMName(MArch);
591756faa4SDylan Noblesmith     if (Type != Triple::UnknownArch)
601756faa4SDylan Noblesmith       TheTriple.setArch(Type);
611756faa4SDylan Noblesmith   } else {
621756faa4SDylan Noblesmith     std::string Error;
631756faa4SDylan Noblesmith     TheTarget = TargetRegistry::lookupTarget(TheTriple.getTriple(), Error);
641756faa4SDylan Noblesmith     if (TheTarget == 0) {
651756faa4SDylan Noblesmith       if (ErrorStr)
661756faa4SDylan Noblesmith         *ErrorStr = Error;
671756faa4SDylan Noblesmith       return 0;
681756faa4SDylan Noblesmith     }
691756faa4SDylan Noblesmith   }
701756faa4SDylan Noblesmith 
711756faa4SDylan Noblesmith   if (!TheTarget->hasJIT()) {
721756faa4SDylan Noblesmith     errs() << "WARNING: This target JIT is not designed for the host you are"
731756faa4SDylan Noblesmith            << " running.  If bad things happen, please choose a different "
741756faa4SDylan Noblesmith            << "-march switch.\n";
751756faa4SDylan Noblesmith   }
761756faa4SDylan Noblesmith 
771756faa4SDylan Noblesmith   // Package up features to be passed to target/subtarget
781756faa4SDylan Noblesmith   std::string FeaturesStr;
79fe6e405eSEvan Cheng   if (!MAttrs.empty()) {
801756faa4SDylan Noblesmith     SubtargetFeatures Features;
811756faa4SDylan Noblesmith     for (unsigned i = 0; i != MAttrs.size(); ++i)
821756faa4SDylan Noblesmith       Features.AddFeature(MAttrs[i]);
831756faa4SDylan Noblesmith     FeaturesStr = Features.getString();
841756faa4SDylan Noblesmith   }
851756faa4SDylan Noblesmith 
861756faa4SDylan Noblesmith   // Allocate a target...
87*2129f596SEvan Cheng   TargetMachine *Target = TheTarget->createTargetMachine(TheTriple.getTriple(),
88*2129f596SEvan Cheng                                                          MCPU, FeaturesStr, RM);
891756faa4SDylan Noblesmith   assert(Target && "Could not allocate target machine!");
901756faa4SDylan Noblesmith   return Target;
911756faa4SDylan Noblesmith }
92