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"
181756faa4SDylan Noblesmith #include "llvm/ADT/Triple.h"
199fb823bbSChandler Carruth #include "llvm/IR/Module.h"
208264e272SEvan Cheng #include "llvm/MC/SubtargetFeature.h"
212bb40357SEvan Cheng #include "llvm/Support/CommandLine.h"
222bb40357SEvan Cheng #include "llvm/Support/Host.h"
232bb40357SEvan Cheng #include "llvm/Support/TargetRegistry.h"
24ed0881b2SChandler Carruth #include "llvm/Target/TargetMachine.h"
257f26246aSDylan Noblesmith 
261756faa4SDylan Noblesmith using namespace llvm;
271756faa4SDylan Noblesmith 
28add6f1d2SOwen Anderson TargetMachine *EngineBuilder::selectTarget() {
29feb805fcSAndrew Kaylor   Triple TT;
30feb805fcSAndrew Kaylor 
31feb805fcSAndrew Kaylor   // MCJIT can generate code for remote targets, but the old JIT and Interpreter
32feb805fcSAndrew Kaylor   // must use the host architecture.
33feb805fcSAndrew Kaylor   if (UseMCJIT && WhichEngine != EngineKind::Interpreter && M)
34feb805fcSAndrew Kaylor     TT.setTriple(M->getTargetTriple());
35a51c6ed6SPeter Collingbourne 
36add6f1d2SOwen Anderson   return selectTarget(TT, MArch, MCPU, MAttrs);
37add6f1d2SOwen Anderson }
38add6f1d2SOwen Anderson 
391756faa4SDylan Noblesmith /// selectTarget - Pick a target either via -march or by guessing the native
401756faa4SDylan Noblesmith /// arch.  Add any CPU features specified via -mcpu or -mattr.
417f26246aSDylan Noblesmith TargetMachine *EngineBuilder::selectTarget(const Triple &TargetTriple,
421756faa4SDylan Noblesmith                               StringRef MArch,
431756faa4SDylan Noblesmith                               StringRef MCPU,
44add6f1d2SOwen Anderson                               const SmallVectorImpl<std::string>& MAttrs) {
457f26246aSDylan Noblesmith   Triple TheTriple(TargetTriple);
461756faa4SDylan Noblesmith   if (TheTriple.getTriple().empty())
47a51c6ed6SPeter Collingbourne     TheTriple.setTriple(sys::getProcessTriple());
481756faa4SDylan Noblesmith 
491756faa4SDylan Noblesmith   // Adjust the triple to match what the user requested.
501756faa4SDylan Noblesmith   const Target *TheTarget = 0;
511756faa4SDylan Noblesmith   if (!MArch.empty()) {
521756faa4SDylan Noblesmith     for (TargetRegistry::iterator it = TargetRegistry::begin(),
531756faa4SDylan Noblesmith            ie = TargetRegistry::end(); it != ie; ++it) {
541756faa4SDylan Noblesmith       if (MArch == it->getName()) {
551756faa4SDylan Noblesmith         TheTarget = &*it;
561756faa4SDylan Noblesmith         break;
571756faa4SDylan Noblesmith       }
581756faa4SDylan Noblesmith     }
591756faa4SDylan Noblesmith 
601756faa4SDylan Noblesmith     if (!TheTarget) {
611ace8b6aSJim Grosbach       if (ErrorStr)
621756faa4SDylan Noblesmith         *ErrorStr = "No available targets are compatible with this -march, "
631756faa4SDylan Noblesmith                     "see -version for the available targets.\n";
641756faa4SDylan Noblesmith       return 0;
651756faa4SDylan Noblesmith     }
661756faa4SDylan Noblesmith 
671756faa4SDylan Noblesmith     // Adjust the triple to match (if known), otherwise stick with the
687f26246aSDylan Noblesmith     // requested/host triple.
691756faa4SDylan Noblesmith     Triple::ArchType Type = Triple::getArchTypeForLLVMName(MArch);
701756faa4SDylan Noblesmith     if (Type != Triple::UnknownArch)
711756faa4SDylan Noblesmith       TheTriple.setArch(Type);
721756faa4SDylan Noblesmith   } else {
731756faa4SDylan Noblesmith     std::string Error;
741756faa4SDylan Noblesmith     TheTarget = TargetRegistry::lookupTarget(TheTriple.getTriple(), Error);
751756faa4SDylan Noblesmith     if (TheTarget == 0) {
761756faa4SDylan Noblesmith       if (ErrorStr)
771756faa4SDylan Noblesmith         *ErrorStr = Error;
781756faa4SDylan Noblesmith       return 0;
791756faa4SDylan Noblesmith     }
801756faa4SDylan Noblesmith   }
811756faa4SDylan Noblesmith 
821756faa4SDylan Noblesmith   // Package up features to be passed to target/subtarget
831756faa4SDylan Noblesmith   std::string FeaturesStr;
84fe6e405eSEvan Cheng   if (!MAttrs.empty()) {
851756faa4SDylan Noblesmith     SubtargetFeatures Features;
861756faa4SDylan Noblesmith     for (unsigned i = 0; i != MAttrs.size(); ++i)
871756faa4SDylan Noblesmith       Features.AddFeature(MAttrs[i]);
881756faa4SDylan Noblesmith     FeaturesStr = Features.getString();
891756faa4SDylan Noblesmith   }
901756faa4SDylan Noblesmith 
9118db1f2fSJF Bastien   // FIXME: non-iOS ARM FastISel is broken with MCJIT.
9218db1f2fSJF Bastien   if (UseMCJIT &&
9318db1f2fSJF Bastien       TheTriple.getArch() == Triple::arm &&
94*943908b7SCameron Esfahani       !TheTriple.isiOS() &&
9518db1f2fSJF Bastien       OptLevel == CodeGenOpt::None) {
9618db1f2fSJF Bastien     OptLevel = CodeGenOpt::Less;
9718db1f2fSJF Bastien   }
9818db1f2fSJF Bastien 
991756faa4SDylan Noblesmith   // Allocate a target...
1002129f596SEvan Cheng   TargetMachine *Target = TheTarget->createTargetMachine(TheTriple.getTriple(),
101efd9b424SEvan Cheng                                                          MCPU, FeaturesStr,
10250f02cb2SNick Lewycky                                                          Options,
103add6f1d2SOwen Anderson                                                          RelocModel, CMModel,
104add6f1d2SOwen Anderson                                                          OptLevel);
1051756faa4SDylan Noblesmith   assert(Target && "Could not allocate target machine!");
1061756faa4SDylan Noblesmith   return Target;
1071756faa4SDylan Noblesmith }
108