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"
19*9fb823bbSChandler 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());
3550a62525SBob Wilson   else {
36feb805fcSAndrew Kaylor     TT.setTriple(LLVM_HOSTTRIPLE);
3750a62525SBob Wilson #if defined(__APPLE__)
3850a62525SBob Wilson #if defined(__LP64__)
3950a62525SBob Wilson     if (TT.isArch32Bit())
4050a62525SBob Wilson       TT = TT.get64BitArchVariant();
4150a62525SBob Wilson #else
4250a62525SBob Wilson     if (TT.isArch64Bit())
4350a62525SBob Wilson       TT = TT.get32BitArchVariant();
4450a62525SBob Wilson #endif
4550a62525SBob Wilson #endif // APPLE
4650a62525SBob Wilson   }
47add6f1d2SOwen Anderson   return selectTarget(TT, MArch, MCPU, MAttrs);
48add6f1d2SOwen Anderson }
49add6f1d2SOwen Anderson 
501756faa4SDylan Noblesmith /// selectTarget - Pick a target either via -march or by guessing the native
511756faa4SDylan Noblesmith /// arch.  Add any CPU features specified via -mcpu or -mattr.
527f26246aSDylan Noblesmith TargetMachine *EngineBuilder::selectTarget(const Triple &TargetTriple,
531756faa4SDylan Noblesmith                               StringRef MArch,
541756faa4SDylan Noblesmith                               StringRef MCPU,
55add6f1d2SOwen Anderson                               const SmallVectorImpl<std::string>& MAttrs) {
567f26246aSDylan Noblesmith   Triple TheTriple(TargetTriple);
571756faa4SDylan Noblesmith   if (TheTriple.getTriple().empty())
5894441fbaSSebastian Pop     TheTriple.setTriple(sys::getDefaultTargetTriple());
591756faa4SDylan Noblesmith 
601756faa4SDylan Noblesmith   // Adjust the triple to match what the user requested.
611756faa4SDylan Noblesmith   const Target *TheTarget = 0;
621756faa4SDylan Noblesmith   if (!MArch.empty()) {
631756faa4SDylan Noblesmith     for (TargetRegistry::iterator it = TargetRegistry::begin(),
641756faa4SDylan Noblesmith            ie = TargetRegistry::end(); it != ie; ++it) {
651756faa4SDylan Noblesmith       if (MArch == it->getName()) {
661756faa4SDylan Noblesmith         TheTarget = &*it;
671756faa4SDylan Noblesmith         break;
681756faa4SDylan Noblesmith       }
691756faa4SDylan Noblesmith     }
701756faa4SDylan Noblesmith 
711756faa4SDylan Noblesmith     if (!TheTarget) {
721ace8b6aSJim Grosbach       if (ErrorStr)
731756faa4SDylan Noblesmith         *ErrorStr = "No available targets are compatible with this -march, "
741756faa4SDylan Noblesmith                     "see -version for the available targets.\n";
751756faa4SDylan Noblesmith       return 0;
761756faa4SDylan Noblesmith     }
771756faa4SDylan Noblesmith 
781756faa4SDylan Noblesmith     // Adjust the triple to match (if known), otherwise stick with the
797f26246aSDylan Noblesmith     // requested/host triple.
801756faa4SDylan Noblesmith     Triple::ArchType Type = Triple::getArchTypeForLLVMName(MArch);
811756faa4SDylan Noblesmith     if (Type != Triple::UnknownArch)
821756faa4SDylan Noblesmith       TheTriple.setArch(Type);
831756faa4SDylan Noblesmith   } else {
841756faa4SDylan Noblesmith     std::string Error;
851756faa4SDylan Noblesmith     TheTarget = TargetRegistry::lookupTarget(TheTriple.getTriple(), Error);
861756faa4SDylan Noblesmith     if (TheTarget == 0) {
871756faa4SDylan Noblesmith       if (ErrorStr)
881756faa4SDylan Noblesmith         *ErrorStr = Error;
891756faa4SDylan Noblesmith       return 0;
901756faa4SDylan Noblesmith     }
911756faa4SDylan Noblesmith   }
921756faa4SDylan Noblesmith 
931756faa4SDylan Noblesmith   // Package up features to be passed to target/subtarget
941756faa4SDylan Noblesmith   std::string FeaturesStr;
95fe6e405eSEvan Cheng   if (!MAttrs.empty()) {
961756faa4SDylan Noblesmith     SubtargetFeatures Features;
971756faa4SDylan Noblesmith     for (unsigned i = 0; i != MAttrs.size(); ++i)
981756faa4SDylan Noblesmith       Features.AddFeature(MAttrs[i]);
991756faa4SDylan Noblesmith     FeaturesStr = Features.getString();
1001756faa4SDylan Noblesmith   }
1011756faa4SDylan Noblesmith 
1021756faa4SDylan Noblesmith   // Allocate a target...
1032129f596SEvan Cheng   TargetMachine *Target = TheTarget->createTargetMachine(TheTriple.getTriple(),
104efd9b424SEvan Cheng                                                          MCPU, FeaturesStr,
10550f02cb2SNick Lewycky                                                          Options,
106add6f1d2SOwen Anderson                                                          RelocModel, CMModel,
107add6f1d2SOwen Anderson                                                          OptLevel);
1081756faa4SDylan Noblesmith   assert(Target && "Could not allocate target machine!");
1091756faa4SDylan Noblesmith   return Target;
1101756faa4SDylan Noblesmith }
111