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