TargetSelect.cpp revision 34ad6db8b958fdc0d38e122edf753b5326e69b03
1e6297010a6235f3a558f1441abe79cddcb05dc24Dylan Noblesmith//===-- TargetSelect.cpp - Target Chooser Code ----------------------------===// 2e6297010a6235f3a558f1441abe79cddcb05dc24Dylan Noblesmith// 3e6297010a6235f3a558f1441abe79cddcb05dc24Dylan Noblesmith// The LLVM Compiler Infrastructure 4e6297010a6235f3a558f1441abe79cddcb05dc24Dylan Noblesmith// 5e6297010a6235f3a558f1441abe79cddcb05dc24Dylan Noblesmith// This file is distributed under the University of Illinois Open Source 6e6297010a6235f3a558f1441abe79cddcb05dc24Dylan Noblesmith// License. See LICENSE.TXT for details. 7e6297010a6235f3a558f1441abe79cddcb05dc24Dylan Noblesmith// 8e6297010a6235f3a558f1441abe79cddcb05dc24Dylan Noblesmith//===----------------------------------------------------------------------===// 9e6297010a6235f3a558f1441abe79cddcb05dc24Dylan Noblesmith// 10e6297010a6235f3a558f1441abe79cddcb05dc24Dylan Noblesmith// This just asks the TargetRegistry for the appropriate JIT to use, and allows 11e6297010a6235f3a558f1441abe79cddcb05dc24Dylan Noblesmith// the user to specify a specific one on the commandline with -march=x. Clients 12e6297010a6235f3a558f1441abe79cddcb05dc24Dylan Noblesmith// should initialize targets prior to calling createJIT. 13e6297010a6235f3a558f1441abe79cddcb05dc24Dylan Noblesmith// 14e6297010a6235f3a558f1441abe79cddcb05dc24Dylan Noblesmith//===----------------------------------------------------------------------===// 15e6297010a6235f3a558f1441abe79cddcb05dc24Dylan Noblesmith 162ea29ba2a8ddd7ba4b946eb754f1a39304d9fc09Dylan Noblesmith#include "llvm/ExecutionEngine/ExecutionEngine.h" 17e6297010a6235f3a558f1441abe79cddcb05dc24Dylan Noblesmith#include "llvm/Module.h" 18e6297010a6235f3a558f1441abe79cddcb05dc24Dylan Noblesmith#include "llvm/ADT/Triple.h" 19ab8be96fd30ca9396e6b84fdddf1ac6208984cadEvan Cheng#include "llvm/MC/SubtargetFeature.h" 20e6297010a6235f3a558f1441abe79cddcb05dc24Dylan Noblesmith#include "llvm/Support/CommandLine.h" 21e6297010a6235f3a558f1441abe79cddcb05dc24Dylan Noblesmith#include "llvm/Support/raw_ostream.h" 22e6297010a6235f3a558f1441abe79cddcb05dc24Dylan Noblesmith#include "llvm/Support/Host.h" 23e6297010a6235f3a558f1441abe79cddcb05dc24Dylan Noblesmith#include "llvm/Target/TargetMachine.h" 24e6297010a6235f3a558f1441abe79cddcb05dc24Dylan Noblesmith#include "llvm/Target/TargetRegistry.h" 25e6297010a6235f3a558f1441abe79cddcb05dc24Dylan Noblesmithusing namespace llvm; 26e6297010a6235f3a558f1441abe79cddcb05dc24Dylan Noblesmith 27e6297010a6235f3a558f1441abe79cddcb05dc24Dylan Noblesmith/// selectTarget - Pick a target either via -march or by guessing the native 28e6297010a6235f3a558f1441abe79cddcb05dc24Dylan Noblesmith/// arch. Add any CPU features specified via -mcpu or -mattr. 292ea29ba2a8ddd7ba4b946eb754f1a39304d9fc09Dylan NoblesmithTargetMachine *EngineBuilder::selectTarget(Module *Mod, 302ea29ba2a8ddd7ba4b946eb754f1a39304d9fc09Dylan Noblesmith StringRef MArch, 312ea29ba2a8ddd7ba4b946eb754f1a39304d9fc09Dylan Noblesmith StringRef MCPU, 322ea29ba2a8ddd7ba4b946eb754f1a39304d9fc09Dylan Noblesmith const SmallVectorImpl<std::string>& MAttrs, 33439661395fd2a2a832dba01c65bc88718528313cEvan Cheng Reloc::Model RM, 3434ad6db8b958fdc0d38e122edf753b5326e69b03Evan Cheng CodeModel::Model CM, 352ea29ba2a8ddd7ba4b946eb754f1a39304d9fc09Dylan Noblesmith std::string *ErrorStr) { 36e6297010a6235f3a558f1441abe79cddcb05dc24Dylan Noblesmith Triple TheTriple(Mod->getTargetTriple()); 37e6297010a6235f3a558f1441abe79cddcb05dc24Dylan Noblesmith if (TheTriple.getTriple().empty()) 38e6297010a6235f3a558f1441abe79cddcb05dc24Dylan Noblesmith TheTriple.setTriple(sys::getHostTriple()); 39e6297010a6235f3a558f1441abe79cddcb05dc24Dylan Noblesmith 40e6297010a6235f3a558f1441abe79cddcb05dc24Dylan Noblesmith // Adjust the triple to match what the user requested. 41e6297010a6235f3a558f1441abe79cddcb05dc24Dylan Noblesmith const Target *TheTarget = 0; 42e6297010a6235f3a558f1441abe79cddcb05dc24Dylan Noblesmith if (!MArch.empty()) { 43e6297010a6235f3a558f1441abe79cddcb05dc24Dylan Noblesmith for (TargetRegistry::iterator it = TargetRegistry::begin(), 44e6297010a6235f3a558f1441abe79cddcb05dc24Dylan Noblesmith ie = TargetRegistry::end(); it != ie; ++it) { 45e6297010a6235f3a558f1441abe79cddcb05dc24Dylan Noblesmith if (MArch == it->getName()) { 46e6297010a6235f3a558f1441abe79cddcb05dc24Dylan Noblesmith TheTarget = &*it; 47e6297010a6235f3a558f1441abe79cddcb05dc24Dylan Noblesmith break; 48e6297010a6235f3a558f1441abe79cddcb05dc24Dylan Noblesmith } 49e6297010a6235f3a558f1441abe79cddcb05dc24Dylan Noblesmith } 50e6297010a6235f3a558f1441abe79cddcb05dc24Dylan Noblesmith 51e6297010a6235f3a558f1441abe79cddcb05dc24Dylan Noblesmith if (!TheTarget) { 52e6297010a6235f3a558f1441abe79cddcb05dc24Dylan Noblesmith *ErrorStr = "No available targets are compatible with this -march, " 53e6297010a6235f3a558f1441abe79cddcb05dc24Dylan Noblesmith "see -version for the available targets.\n"; 54e6297010a6235f3a558f1441abe79cddcb05dc24Dylan Noblesmith return 0; 55e6297010a6235f3a558f1441abe79cddcb05dc24Dylan Noblesmith } 56e6297010a6235f3a558f1441abe79cddcb05dc24Dylan Noblesmith 57e6297010a6235f3a558f1441abe79cddcb05dc24Dylan Noblesmith // Adjust the triple to match (if known), otherwise stick with the 58e6297010a6235f3a558f1441abe79cddcb05dc24Dylan Noblesmith // module/host triple. 59e6297010a6235f3a558f1441abe79cddcb05dc24Dylan Noblesmith Triple::ArchType Type = Triple::getArchTypeForLLVMName(MArch); 60e6297010a6235f3a558f1441abe79cddcb05dc24Dylan Noblesmith if (Type != Triple::UnknownArch) 61e6297010a6235f3a558f1441abe79cddcb05dc24Dylan Noblesmith TheTriple.setArch(Type); 62e6297010a6235f3a558f1441abe79cddcb05dc24Dylan Noblesmith } else { 63e6297010a6235f3a558f1441abe79cddcb05dc24Dylan Noblesmith std::string Error; 64e6297010a6235f3a558f1441abe79cddcb05dc24Dylan Noblesmith TheTarget = TargetRegistry::lookupTarget(TheTriple.getTriple(), Error); 65e6297010a6235f3a558f1441abe79cddcb05dc24Dylan Noblesmith if (TheTarget == 0) { 66e6297010a6235f3a558f1441abe79cddcb05dc24Dylan Noblesmith if (ErrorStr) 67e6297010a6235f3a558f1441abe79cddcb05dc24Dylan Noblesmith *ErrorStr = Error; 68e6297010a6235f3a558f1441abe79cddcb05dc24Dylan Noblesmith return 0; 69e6297010a6235f3a558f1441abe79cddcb05dc24Dylan Noblesmith } 70e6297010a6235f3a558f1441abe79cddcb05dc24Dylan Noblesmith } 71e6297010a6235f3a558f1441abe79cddcb05dc24Dylan Noblesmith 72e6297010a6235f3a558f1441abe79cddcb05dc24Dylan Noblesmith if (!TheTarget->hasJIT()) { 73e6297010a6235f3a558f1441abe79cddcb05dc24Dylan Noblesmith errs() << "WARNING: This target JIT is not designed for the host you are" 74e6297010a6235f3a558f1441abe79cddcb05dc24Dylan Noblesmith << " running. If bad things happen, please choose a different " 75e6297010a6235f3a558f1441abe79cddcb05dc24Dylan Noblesmith << "-march switch.\n"; 76e6297010a6235f3a558f1441abe79cddcb05dc24Dylan Noblesmith } 77e6297010a6235f3a558f1441abe79cddcb05dc24Dylan Noblesmith 78e6297010a6235f3a558f1441abe79cddcb05dc24Dylan Noblesmith // Package up features to be passed to target/subtarget 79e6297010a6235f3a558f1441abe79cddcb05dc24Dylan Noblesmith std::string FeaturesStr; 80276365dd4bc0c2160f91fd8062ae1fc90c86c324Evan Cheng if (!MAttrs.empty()) { 81e6297010a6235f3a558f1441abe79cddcb05dc24Dylan Noblesmith SubtargetFeatures Features; 82e6297010a6235f3a558f1441abe79cddcb05dc24Dylan Noblesmith for (unsigned i = 0; i != MAttrs.size(); ++i) 83e6297010a6235f3a558f1441abe79cddcb05dc24Dylan Noblesmith Features.AddFeature(MAttrs[i]); 84e6297010a6235f3a558f1441abe79cddcb05dc24Dylan Noblesmith FeaturesStr = Features.getString(); 85e6297010a6235f3a558f1441abe79cddcb05dc24Dylan Noblesmith } 86e6297010a6235f3a558f1441abe79cddcb05dc24Dylan Noblesmith 87e6297010a6235f3a558f1441abe79cddcb05dc24Dylan Noblesmith // Allocate a target... 88439661395fd2a2a832dba01c65bc88718528313cEvan Cheng TargetMachine *Target = TheTarget->createTargetMachine(TheTriple.getTriple(), 8934ad6db8b958fdc0d38e122edf753b5326e69b03Evan Cheng MCPU, FeaturesStr, 9034ad6db8b958fdc0d38e122edf753b5326e69b03Evan Cheng RM, CM); 91e6297010a6235f3a558f1441abe79cddcb05dc24Dylan Noblesmith assert(Target && "Could not allocate target machine!"); 92e6297010a6235f3a558f1441abe79cddcb05dc24Dylan Noblesmith return Target; 93e6297010a6235f3a558f1441abe79cddcb05dc24Dylan Noblesmith} 94