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" 18add6f1d2SOwen 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 28add6f1d2SOwen Anderson TargetMachine *EngineBuilder::selectTarget() { 29*feb805fcSAndrew Kaylor Triple TT; 30*feb805fcSAndrew Kaylor 31*feb805fcSAndrew Kaylor // MCJIT can generate code for remote targets, but the old JIT and Interpreter 32*feb805fcSAndrew Kaylor // must use the host architecture. 33*feb805fcSAndrew Kaylor if (UseMCJIT && WhichEngine != EngineKind::Interpreter && M) 34*feb805fcSAndrew Kaylor TT.setTriple(M->getTargetTriple()); 35*feb805fcSAndrew Kaylor else 36*feb805fcSAndrew Kaylor TT.setTriple(LLVM_HOSTTRIPLE); 37add6f1d2SOwen Anderson return selectTarget(TT, MArch, MCPU, MAttrs); 38add6f1d2SOwen Anderson } 39add6f1d2SOwen Anderson 401756faa4SDylan Noblesmith /// selectTarget - Pick a target either via -march or by guessing the native 411756faa4SDylan Noblesmith /// arch. Add any CPU features specified via -mcpu or -mattr. 427f26246aSDylan Noblesmith TargetMachine *EngineBuilder::selectTarget(const Triple &TargetTriple, 431756faa4SDylan Noblesmith StringRef MArch, 441756faa4SDylan Noblesmith StringRef MCPU, 45add6f1d2SOwen Anderson const SmallVectorImpl<std::string>& MAttrs) { 467f26246aSDylan Noblesmith Triple TheTriple(TargetTriple); 471756faa4SDylan Noblesmith if (TheTriple.getTriple().empty()) 4894441fbaSSebastian Pop TheTriple.setTriple(sys::getDefaultTargetTriple()); 491756faa4SDylan Noblesmith 501756faa4SDylan Noblesmith // Adjust the triple to match what the user requested. 511756faa4SDylan Noblesmith const Target *TheTarget = 0; 521756faa4SDylan Noblesmith if (!MArch.empty()) { 531756faa4SDylan Noblesmith for (TargetRegistry::iterator it = TargetRegistry::begin(), 541756faa4SDylan Noblesmith ie = TargetRegistry::end(); it != ie; ++it) { 551756faa4SDylan Noblesmith if (MArch == it->getName()) { 561756faa4SDylan Noblesmith TheTarget = &*it; 571756faa4SDylan Noblesmith break; 581756faa4SDylan Noblesmith } 591756faa4SDylan Noblesmith } 601756faa4SDylan Noblesmith 611756faa4SDylan Noblesmith if (!TheTarget) { 621ace8b6aSJim Grosbach if (ErrorStr) 631756faa4SDylan Noblesmith *ErrorStr = "No available targets are compatible with this -march, " 641756faa4SDylan Noblesmith "see -version for the available targets.\n"; 651756faa4SDylan Noblesmith return 0; 661756faa4SDylan Noblesmith } 671756faa4SDylan Noblesmith 681756faa4SDylan Noblesmith // Adjust the triple to match (if known), otherwise stick with the 697f26246aSDylan Noblesmith // requested/host triple. 701756faa4SDylan Noblesmith Triple::ArchType Type = Triple::getArchTypeForLLVMName(MArch); 711756faa4SDylan Noblesmith if (Type != Triple::UnknownArch) 721756faa4SDylan Noblesmith TheTriple.setArch(Type); 731756faa4SDylan Noblesmith } else { 741756faa4SDylan Noblesmith std::string Error; 751756faa4SDylan Noblesmith TheTarget = TargetRegistry::lookupTarget(TheTriple.getTriple(), Error); 761756faa4SDylan Noblesmith if (TheTarget == 0) { 771756faa4SDylan Noblesmith if (ErrorStr) 781756faa4SDylan Noblesmith *ErrorStr = Error; 791756faa4SDylan Noblesmith return 0; 801756faa4SDylan Noblesmith } 811756faa4SDylan Noblesmith } 821756faa4SDylan Noblesmith 831756faa4SDylan Noblesmith // Package up features to be passed to target/subtarget 841756faa4SDylan Noblesmith std::string FeaturesStr; 85fe6e405eSEvan Cheng if (!MAttrs.empty()) { 861756faa4SDylan Noblesmith SubtargetFeatures Features; 871756faa4SDylan Noblesmith for (unsigned i = 0; i != MAttrs.size(); ++i) 881756faa4SDylan Noblesmith Features.AddFeature(MAttrs[i]); 891756faa4SDylan Noblesmith FeaturesStr = Features.getString(); 901756faa4SDylan Noblesmith } 911756faa4SDylan Noblesmith 921756faa4SDylan Noblesmith // Allocate a target... 932129f596SEvan Cheng TargetMachine *Target = TheTarget->createTargetMachine(TheTriple.getTriple(), 94efd9b424SEvan Cheng MCPU, FeaturesStr, 9550f02cb2SNick Lewycky Options, 96add6f1d2SOwen Anderson RelocModel, CMModel, 97add6f1d2SOwen Anderson OptLevel); 981756faa4SDylan Noblesmith assert(Target && "Could not allocate target machine!"); 991756faa4SDylan Noblesmith return Target; 1001756faa4SDylan Noblesmith } 101