11756faa4SDylan Noblesmith //===-- TargetSelect.cpp - Target Chooser Code ----------------------------===// 21756faa4SDylan Noblesmith // 3*2946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4*2946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information. 5*2946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 61756faa4SDylan Noblesmith // 71756faa4SDylan Noblesmith //===----------------------------------------------------------------------===// 81756faa4SDylan Noblesmith // 97f26246aSDylan Noblesmith // This just asks the TargetRegistry for the appropriate target to use, and 107f26246aSDylan Noblesmith // allows the user to specify a specific one on the commandline with -march=x, 117f26246aSDylan Noblesmith // -mcpu=y, and -mattr=a,-b,+c. Clients should initialize targets prior to 127f26246aSDylan Noblesmith // calling selectTarget(). 131756faa4SDylan Noblesmith // 141756faa4SDylan Noblesmith //===----------------------------------------------------------------------===// 151756faa4SDylan Noblesmith 161756faa4SDylan Noblesmith #include "llvm/ADT/Triple.h" 17b550cb17SMehdi Amini #include "llvm/ExecutionEngine/ExecutionEngine.h" 189fb823bbSChandler Carruth #include "llvm/IR/Module.h" 198264e272SEvan Cheng #include "llvm/MC/SubtargetFeature.h" 202bb40357SEvan Cheng #include "llvm/Support/Host.h" 212bb40357SEvan Cheng #include "llvm/Support/TargetRegistry.h" 22ed0881b2SChandler Carruth #include "llvm/Target/TargetMachine.h" 237f26246aSDylan Noblesmith 241756faa4SDylan Noblesmith using namespace llvm; 251756faa4SDylan Noblesmith 26add6f1d2SOwen Anderson TargetMachine *EngineBuilder::selectTarget() { 27feb805fcSAndrew Kaylor Triple TT; 28feb805fcSAndrew Kaylor 29feb805fcSAndrew Kaylor // MCJIT can generate code for remote targets, but the old JIT and Interpreter 30feb805fcSAndrew Kaylor // must use the host architecture. 3179cc1e3aSEric Christopher if (WhichEngine != EngineKind::Interpreter && M) 32feb805fcSAndrew Kaylor TT.setTriple(M->getTargetTriple()); 33a51c6ed6SPeter Collingbourne 34add6f1d2SOwen Anderson return selectTarget(TT, MArch, MCPU, MAttrs); 35add6f1d2SOwen Anderson } 36add6f1d2SOwen Anderson 371756faa4SDylan Noblesmith /// selectTarget - Pick a target either via -march or by guessing the native 381756faa4SDylan Noblesmith /// arch. Add any CPU features specified via -mcpu or -mattr. 397f26246aSDylan Noblesmith TargetMachine *EngineBuilder::selectTarget(const Triple &TargetTriple, 401756faa4SDylan Noblesmith StringRef MArch, 411756faa4SDylan Noblesmith StringRef MCPU, 42add6f1d2SOwen Anderson const SmallVectorImpl<std::string>& MAttrs) { 437f26246aSDylan Noblesmith Triple TheTriple(TargetTriple); 441756faa4SDylan Noblesmith if (TheTriple.getTriple().empty()) 45a51c6ed6SPeter Collingbourne TheTriple.setTriple(sys::getProcessTriple()); 461756faa4SDylan Noblesmith 471756faa4SDylan Noblesmith // Adjust the triple to match what the user requested. 482617dcceSCraig Topper const Target *TheTarget = nullptr; 491756faa4SDylan Noblesmith if (!MArch.empty()) { 5042531260SDavid Majnemer auto I = find_if(TargetRegistry::targets(), 5146c561c1SDavid Blaikie [&](const Target &T) { return MArch == T.getName(); }); 521756faa4SDylan Noblesmith 5346c561c1SDavid Blaikie if (I == TargetRegistry::targets().end()) { 541ace8b6aSJim Grosbach if (ErrorStr) 551756faa4SDylan Noblesmith *ErrorStr = "No available targets are compatible with this -march, " 561756faa4SDylan Noblesmith "see -version for the available targets.\n"; 572617dcceSCraig Topper return nullptr; 581756faa4SDylan Noblesmith } 591756faa4SDylan Noblesmith 6046c561c1SDavid Blaikie TheTarget = &*I; 6146c561c1SDavid Blaikie 621756faa4SDylan Noblesmith // Adjust the triple to match (if known), otherwise stick with the 637f26246aSDylan Noblesmith // requested/host triple. 641756faa4SDylan Noblesmith Triple::ArchType Type = Triple::getArchTypeForLLVMName(MArch); 651756faa4SDylan Noblesmith if (Type != Triple::UnknownArch) 661756faa4SDylan Noblesmith TheTriple.setArch(Type); 671756faa4SDylan Noblesmith } else { 681756faa4SDylan Noblesmith std::string Error; 691756faa4SDylan Noblesmith TheTarget = TargetRegistry::lookupTarget(TheTriple.getTriple(), Error); 702617dcceSCraig Topper if (!TheTarget) { 711756faa4SDylan Noblesmith if (ErrorStr) 721756faa4SDylan Noblesmith *ErrorStr = Error; 732617dcceSCraig Topper return nullptr; 741756faa4SDylan Noblesmith } 751756faa4SDylan Noblesmith } 761756faa4SDylan Noblesmith 771756faa4SDylan Noblesmith // Package up features to be passed to target/subtarget 781756faa4SDylan Noblesmith std::string FeaturesStr; 79fe6e405eSEvan Cheng if (!MAttrs.empty()) { 801756faa4SDylan Noblesmith SubtargetFeatures Features; 811756faa4SDylan Noblesmith for (unsigned i = 0; i != MAttrs.size(); ++i) 821756faa4SDylan Noblesmith Features.AddFeature(MAttrs[i]); 831756faa4SDylan Noblesmith FeaturesStr = Features.getString(); 841756faa4SDylan Noblesmith } 851756faa4SDylan Noblesmith 8618db1f2fSJF Bastien // FIXME: non-iOS ARM FastISel is broken with MCJIT. 8779cc1e3aSEric Christopher if (TheTriple.getArch() == Triple::arm && 88943908b7SCameron Esfahani !TheTriple.isiOS() && 8918db1f2fSJF Bastien OptLevel == CodeGenOpt::None) { 9018db1f2fSJF Bastien OptLevel = CodeGenOpt::Less; 9118db1f2fSJF Bastien } 9218db1f2fSJF Bastien 931756faa4SDylan Noblesmith // Allocate a target... 9479e238afSRafael Espindola TargetMachine *Target = 9579e238afSRafael Espindola TheTarget->createTargetMachine(TheTriple.getTriple(), MCPU, FeaturesStr, 9679e238afSRafael Espindola Options, RelocModel, CMModel, OptLevel, 9779e238afSRafael Espindola /*JIT*/ true); 988eec91e9SLang Hames Target->Options.EmulatedTLS = EmulatedTLS; 999f9e4681SChih-Hung Hsieh Target->Options.ExplicitEmulatedTLS = true; 1009f9e4681SChih-Hung Hsieh 1011756faa4SDylan Noblesmith assert(Target && "Could not allocate target machine!"); 1021756faa4SDylan Noblesmith return Target; 1031756faa4SDylan Noblesmith } 104