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/Target/TargetMachine.h" 212bb40357SEvan Cheng #include "llvm/Support/CommandLine.h" 222bb40357SEvan Cheng #include "llvm/Support/Host.h" 232bb40357SEvan Cheng #include "llvm/Support/TargetRegistry.h" 242bb40357SEvan Cheng #include "llvm/Support/raw_ostream.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, 332129f596SEvan Cheng Reloc::Model RM, 34efd9b424SEvan Cheng CodeModel::Model CM, 35*19a58df9SDylan Noblesmith CodeGenOpt::Level OL, 361756faa4SDylan Noblesmith std::string *ErrorStr) { 371756faa4SDylan Noblesmith Triple TheTriple(Mod->getTargetTriple()); 381756faa4SDylan Noblesmith if (TheTriple.getTriple().empty()) 3994441fbaSSebastian Pop TheTriple.setTriple(sys::getDefaultTargetTriple()); 401756faa4SDylan Noblesmith 411756faa4SDylan Noblesmith // Adjust the triple to match what the user requested. 421756faa4SDylan Noblesmith const Target *TheTarget = 0; 431756faa4SDylan Noblesmith if (!MArch.empty()) { 441756faa4SDylan Noblesmith for (TargetRegistry::iterator it = TargetRegistry::begin(), 451756faa4SDylan Noblesmith ie = TargetRegistry::end(); it != ie; ++it) { 461756faa4SDylan Noblesmith if (MArch == it->getName()) { 471756faa4SDylan Noblesmith TheTarget = &*it; 481756faa4SDylan Noblesmith break; 491756faa4SDylan Noblesmith } 501756faa4SDylan Noblesmith } 511756faa4SDylan Noblesmith 521756faa4SDylan Noblesmith if (!TheTarget) { 531756faa4SDylan Noblesmith *ErrorStr = "No available targets are compatible with this -march, " 541756faa4SDylan Noblesmith "see -version for the available targets.\n"; 551756faa4SDylan Noblesmith return 0; 561756faa4SDylan Noblesmith } 571756faa4SDylan Noblesmith 581756faa4SDylan Noblesmith // Adjust the triple to match (if known), otherwise stick with the 591756faa4SDylan Noblesmith // module/host triple. 601756faa4SDylan Noblesmith Triple::ArchType Type = Triple::getArchTypeForLLVMName(MArch); 611756faa4SDylan Noblesmith if (Type != Triple::UnknownArch) 621756faa4SDylan Noblesmith TheTriple.setArch(Type); 631756faa4SDylan Noblesmith } else { 641756faa4SDylan Noblesmith std::string Error; 651756faa4SDylan Noblesmith TheTarget = TargetRegistry::lookupTarget(TheTriple.getTriple(), Error); 661756faa4SDylan Noblesmith if (TheTarget == 0) { 671756faa4SDylan Noblesmith if (ErrorStr) 681756faa4SDylan Noblesmith *ErrorStr = Error; 691756faa4SDylan Noblesmith return 0; 701756faa4SDylan Noblesmith } 711756faa4SDylan Noblesmith } 721756faa4SDylan Noblesmith 731756faa4SDylan Noblesmith if (!TheTarget->hasJIT()) { 741756faa4SDylan Noblesmith errs() << "WARNING: This target JIT is not designed for the host you are" 751756faa4SDylan Noblesmith << " running. If bad things happen, please choose a different " 761756faa4SDylan Noblesmith << "-march switch.\n"; 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, 91*19a58df9SDylan Noblesmith RM, CM, OL); 921756faa4SDylan Noblesmith assert(Target && "Could not allocate target machine!"); 931756faa4SDylan Noblesmith return Target; 941756faa4SDylan Noblesmith } 95