LLVM API Documentation
00001 //===-- TargetSelect.cpp - Target Chooser Code ----------------------------===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This just asks the TargetRegistry for the appropriate JIT to use, and allows 00011 // the user to specify a specific one on the commandline with -march=x. Clients 00012 // should initialize targets prior to calling createJIT. 00013 // 00014 //===----------------------------------------------------------------------===// 00015 00016 #include "JIT.h" 00017 #include "llvm/Module.h" 00018 #include "llvm/ModuleProvider.h" 00019 #include "llvm/ADT/Triple.h" 00020 #include "llvm/Support/CommandLine.h" 00021 #include "llvm/Support/raw_ostream.h" 00022 #include "llvm/System/Host.h" 00023 #include "llvm/Target/SubtargetFeature.h" 00024 #include "llvm/Target/TargetMachine.h" 00025 #include "llvm/Target/TargetRegistry.h" 00026 using namespace llvm; 00027 00028 static cl::opt<std::string> 00029 MArch("march", cl::desc("Architecture to generate assembly for (see --version)")); 00030 00031 static cl::opt<std::string> 00032 MCPU("mcpu", 00033 cl::desc("Target a specific cpu type (-mcpu=help for details)"), 00034 cl::value_desc("cpu-name"), 00035 cl::init("")); 00036 00037 static cl::list<std::string> 00038 MAttrs("mattr", 00039 cl::CommaSeparated, 00040 cl::desc("Target specific attributes (-mattr=help for details)"), 00041 cl::value_desc("a1,+a2,-a3,...")); 00042 00043 /// selectTarget - Pick a target either via -march or by guessing the native 00044 /// arch. Add any CPU features specified via -mcpu or -mattr. 00045 TargetMachine *JIT::selectTarget(ModuleProvider *MP, std::string *ErrorStr) { 00046 Module &Mod = *MP->getModule(); 00047 00048 Triple TheTriple(Mod.getTargetTriple()); 00049 if (TheTriple.getTriple().empty()) 00050 TheTriple.setTriple(sys::getHostTriple()); 00051 00052 // Adjust the triple to match what the user requested. 00053 const Target *TheTarget = 0; 00054 if (!MArch.empty()) { 00055 for (TargetRegistry::iterator it = TargetRegistry::begin(), 00056 ie = TargetRegistry::end(); it != ie; ++it) { 00057 if (MArch == it->getName()) { 00058 TheTarget = &*it; 00059 break; 00060 } 00061 } 00062 00063 if (!TheTarget) { 00064 errs() << "JIT: error: invalid target '" << MArch << "'.\n"; 00065 return 0; 00066 } 00067 00068 // Adjust the triple to match (if known), otherwise stick with the 00069 // module/host triple. 00070 Triple::ArchType Type = Triple::getArchTypeForLLVMName(MArch); 00071 if (Type != Triple::UnknownArch) 00072 TheTriple.setArch(Type); 00073 } else { 00074 std::string Error; 00075 TheTarget = TargetRegistry::lookupTarget(TheTriple.getTriple(), Error); 00076 if (TheTarget == 0) { 00077 if (ErrorStr) 00078 *ErrorStr = Error; 00079 return 0; 00080 } 00081 } 00082 00083 if (!TheTarget->hasJIT()) { 00084 errs() << "WARNING: This target JIT is not designed for the host you are" 00085 << " running. If bad things happen, please choose a different " 00086 << "-march switch.\n"; 00087 } 00088 00089 // Package up features to be passed to target/subtarget 00090 std::string FeaturesStr; 00091 if (!MCPU.empty() || !MAttrs.empty()) { 00092 SubtargetFeatures Features; 00093 Features.setCPU(MCPU); 00094 for (unsigned i = 0; i != MAttrs.size(); ++i) 00095 Features.AddFeature(MAttrs[i]); 00096 FeaturesStr = Features.getString(); 00097 } 00098 00099 // Allocate a target... 00100 TargetMachine *Target = 00101 TheTarget->createTargetMachine(TheTriple.getTriple(), FeaturesStr); 00102 assert(Target && "Could not allocate target machine!"); 00103 return Target; 00104 }