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 //
107f26246aSDylan Noblesmith // This just asks the TargetRegistry for the appropriate target to use, and
117f26246aSDylan Noblesmith // allows the user to specify a specific one on the commandline with -march=x,
127f26246aSDylan Noblesmith // -mcpu=y, and -mattr=a,-b,+c. Clients should initialize targets prior to
137f26246aSDylan Noblesmith // calling selectTarget().
141756faa4SDylan Noblesmith //
151756faa4SDylan Noblesmith //===----------------------------------------------------------------------===//
161756faa4SDylan Noblesmith 
171756faa4SDylan Noblesmith #include "llvm/ExecutionEngine/ExecutionEngine.h"
18*add6f1d2SOwen Anderson #include "llvm/Module.h"
191756faa4SDylan Noblesmith #include "llvm/ADT/Triple.h"
208264e272SEvan Cheng #include "llvm/MC/SubtargetFeature.h"
211756faa4SDylan Noblesmith #include "llvm/Target/TargetMachine.h"
222bb40357SEvan Cheng #include "llvm/Support/CommandLine.h"
232bb40357SEvan Cheng #include "llvm/Support/Host.h"
242bb40357SEvan Cheng #include "llvm/Support/TargetRegistry.h"
257f26246aSDylan Noblesmith 
261756faa4SDylan Noblesmith using namespace llvm;
271756faa4SDylan Noblesmith 
28*add6f1d2SOwen Anderson TargetMachine *EngineBuilder::selectTarget() {
29*add6f1d2SOwen Anderson   StringRef MArch = "";
30*add6f1d2SOwen Anderson   StringRef MCPU = "";
31*add6f1d2SOwen Anderson   SmallVector<std::string, 1> MAttrs;
32*add6f1d2SOwen Anderson   Triple TT(M->getTargetTriple());
33*add6f1d2SOwen Anderson 
34*add6f1d2SOwen Anderson   return selectTarget(TT, MArch, MCPU, MAttrs);
35*add6f1d2SOwen Anderson }
36*add6f1d2SOwen Anderson 
371756faa4SDylan Noblesmith /// selectTarget - Pick a target either via -march or by guessing the native
381756faa4SDylan Noblesmith /// arch.  Add any CPU features specified via -mcpu or -mattr.
397f26246aSDylan Noblesmith TargetMachine *EngineBuilder::selectTarget(const Triple &TargetTriple,
401756faa4SDylan Noblesmith                               StringRef MArch,
411756faa4SDylan Noblesmith                               StringRef MCPU,
42*add6f1d2SOwen Anderson                               const SmallVectorImpl<std::string>& MAttrs) {
437f26246aSDylan Noblesmith   Triple TheTriple(TargetTriple);
441756faa4SDylan Noblesmith   if (TheTriple.getTriple().empty())
4594441fbaSSebastian Pop     TheTriple.setTriple(sys::getDefaultTargetTriple());
461756faa4SDylan Noblesmith 
471756faa4SDylan Noblesmith   // Adjust the triple to match what the user requested.
481756faa4SDylan Noblesmith   const Target *TheTarget = 0;
491756faa4SDylan Noblesmith   if (!MArch.empty()) {
501756faa4SDylan Noblesmith     for (TargetRegistry::iterator it = TargetRegistry::begin(),
511756faa4SDylan Noblesmith            ie = TargetRegistry::end(); it != ie; ++it) {
521756faa4SDylan Noblesmith       if (MArch == it->getName()) {
531756faa4SDylan Noblesmith         TheTarget = &*it;
541756faa4SDylan Noblesmith         break;
551756faa4SDylan Noblesmith       }
561756faa4SDylan Noblesmith     }
571756faa4SDylan Noblesmith 
581756faa4SDylan Noblesmith     if (!TheTarget) {
591756faa4SDylan Noblesmith       *ErrorStr = "No available targets are compatible with this -march, "
601756faa4SDylan Noblesmith         "see -version for the available targets.\n";
611756faa4SDylan Noblesmith       return 0;
621756faa4SDylan Noblesmith     }
631756faa4SDylan Noblesmith 
641756faa4SDylan Noblesmith     // Adjust the triple to match (if known), otherwise stick with the
657f26246aSDylan Noblesmith     // requested/host triple.
661756faa4SDylan Noblesmith     Triple::ArchType Type = Triple::getArchTypeForLLVMName(MArch);
671756faa4SDylan Noblesmith     if (Type != Triple::UnknownArch)
681756faa4SDylan Noblesmith       TheTriple.setArch(Type);
691756faa4SDylan Noblesmith   } else {
701756faa4SDylan Noblesmith     std::string Error;
711756faa4SDylan Noblesmith     TheTarget = TargetRegistry::lookupTarget(TheTriple.getTriple(), Error);
721756faa4SDylan Noblesmith     if (TheTarget == 0) {
731756faa4SDylan Noblesmith       if (ErrorStr)
741756faa4SDylan Noblesmith         *ErrorStr = Error;
751756faa4SDylan Noblesmith       return 0;
761756faa4SDylan Noblesmith     }
771756faa4SDylan Noblesmith   }
781756faa4SDylan Noblesmith 
791756faa4SDylan Noblesmith   // Package up features to be passed to target/subtarget
801756faa4SDylan Noblesmith   std::string FeaturesStr;
81fe6e405eSEvan Cheng   if (!MAttrs.empty()) {
821756faa4SDylan Noblesmith     SubtargetFeatures Features;
831756faa4SDylan Noblesmith     for (unsigned i = 0; i != MAttrs.size(); ++i)
841756faa4SDylan Noblesmith       Features.AddFeature(MAttrs[i]);
851756faa4SDylan Noblesmith     FeaturesStr = Features.getString();
861756faa4SDylan Noblesmith   }
871756faa4SDylan Noblesmith 
881756faa4SDylan Noblesmith   // Allocate a target...
892129f596SEvan Cheng   TargetMachine *Target = TheTarget->createTargetMachine(TheTriple.getTriple(),
90efd9b424SEvan Cheng                                                          MCPU, FeaturesStr,
9150f02cb2SNick Lewycky                                                          Options,
92*add6f1d2SOwen Anderson                                                          RelocModel, CMModel,
93*add6f1d2SOwen Anderson                                                          OptLevel);
941756faa4SDylan Noblesmith   assert(Target && "Could not allocate target machine!");
951756faa4SDylan Noblesmith   return Target;
961756faa4SDylan Noblesmith }
97