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/ADT/Triple.h" 18b550cb17SMehdi Amini #include "llvm/ExecutionEngine/ExecutionEngine.h" 199fb823bbSChandler Carruth #include "llvm/IR/Module.h" 208264e272SEvan Cheng #include "llvm/MC/SubtargetFeature.h" 212bb40357SEvan Cheng #include "llvm/Support/Host.h" 222bb40357SEvan Cheng #include "llvm/Support/TargetRegistry.h" 23ed0881b2SChandler Carruth #include "llvm/Target/TargetMachine.h" 247f26246aSDylan Noblesmith 251756faa4SDylan Noblesmith using namespace llvm; 261756faa4SDylan Noblesmith 27add6f1d2SOwen Anderson TargetMachine *EngineBuilder::selectTarget() { 28feb805fcSAndrew Kaylor Triple TT; 29feb805fcSAndrew Kaylor 30feb805fcSAndrew Kaylor // MCJIT can generate code for remote targets, but the old JIT and Interpreter 31feb805fcSAndrew Kaylor // must use the host architecture. 3279cc1e3aSEric Christopher if (WhichEngine != EngineKind::Interpreter && M) 33feb805fcSAndrew Kaylor TT.setTriple(M->getTargetTriple()); 34a51c6ed6SPeter Collingbourne 35add6f1d2SOwen Anderson return selectTarget(TT, MArch, MCPU, MAttrs); 36add6f1d2SOwen Anderson } 37add6f1d2SOwen Anderson 381756faa4SDylan Noblesmith /// selectTarget - Pick a target either via -march or by guessing the native 391756faa4SDylan Noblesmith /// arch. Add any CPU features specified via -mcpu or -mattr. 407f26246aSDylan Noblesmith TargetMachine *EngineBuilder::selectTarget(const Triple &TargetTriple, 411756faa4SDylan Noblesmith StringRef MArch, 421756faa4SDylan Noblesmith StringRef MCPU, 43add6f1d2SOwen Anderson const SmallVectorImpl<std::string>& MAttrs) { 447f26246aSDylan Noblesmith Triple TheTriple(TargetTriple); 451756faa4SDylan Noblesmith if (TheTriple.getTriple().empty()) 46a51c6ed6SPeter Collingbourne TheTriple.setTriple(sys::getProcessTriple()); 471756faa4SDylan Noblesmith 481756faa4SDylan Noblesmith // Adjust the triple to match what the user requested. 492617dcceSCraig Topper const Target *TheTarget = nullptr; 501756faa4SDylan Noblesmith if (!MArch.empty()) { 5142531260SDavid Majnemer auto I = find_if(TargetRegistry::targets(), 5246c561c1SDavid Blaikie [&](const Target &T) { return MArch == T.getName(); }); 531756faa4SDylan Noblesmith 5446c561c1SDavid Blaikie if (I == TargetRegistry::targets().end()) { 551ace8b6aSJim Grosbach if (ErrorStr) 561756faa4SDylan Noblesmith *ErrorStr = "No available targets are compatible with this -march, " 571756faa4SDylan Noblesmith "see -version for the available targets.\n"; 582617dcceSCraig Topper return nullptr; 591756faa4SDylan Noblesmith } 601756faa4SDylan Noblesmith 6146c561c1SDavid Blaikie TheTarget = &*I; 6246c561c1SDavid Blaikie 631756faa4SDylan Noblesmith // Adjust the triple to match (if known), otherwise stick with the 647f26246aSDylan Noblesmith // requested/host triple. 651756faa4SDylan Noblesmith Triple::ArchType Type = Triple::getArchTypeForLLVMName(MArch); 661756faa4SDylan Noblesmith if (Type != Triple::UnknownArch) 671756faa4SDylan Noblesmith TheTriple.setArch(Type); 681756faa4SDylan Noblesmith } else { 691756faa4SDylan Noblesmith std::string Error; 701756faa4SDylan Noblesmith TheTarget = TargetRegistry::lookupTarget(TheTriple.getTriple(), Error); 712617dcceSCraig Topper if (!TheTarget) { 721756faa4SDylan Noblesmith if (ErrorStr) 731756faa4SDylan Noblesmith *ErrorStr = Error; 742617dcceSCraig Topper return nullptr; 751756faa4SDylan Noblesmith } 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 8718db1f2fSJF Bastien // FIXME: non-iOS ARM FastISel is broken with MCJIT. 8879cc1e3aSEric Christopher if (TheTriple.getArch() == Triple::arm && 89943908b7SCameron Esfahani !TheTriple.isiOS() && 9018db1f2fSJF Bastien OptLevel == CodeGenOpt::None) { 9118db1f2fSJF Bastien OptLevel = CodeGenOpt::Less; 9218db1f2fSJF Bastien } 9318db1f2fSJF Bastien 941756faa4SDylan Noblesmith // Allocate a target... 9579e238afSRafael Espindola TargetMachine *Target = 9679e238afSRafael Espindola TheTarget->createTargetMachine(TheTriple.getTriple(), MCPU, FeaturesStr, 9779e238afSRafael Espindola Options, RelocModel, CMModel, OptLevel, 9879e238afSRafael Espindola /*JIT*/ true); 99*8eec91e9SLang Hames Target->Options.EmulatedTLS = EmulatedTLS; 1001756faa4SDylan Noblesmith assert(Target && "Could not allocate target machine!"); 1011756faa4SDylan Noblesmith return Target; 1021756faa4SDylan Noblesmith } 103