1 //===-- TargetSelect.cpp - Target Chooser Code ----------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This just asks the TargetRegistry for the appropriate JIT to use, and allows
11 // the user to specify a specific one on the commandline with -march=x. Clients
12 // should initialize targets prior to calling createJIT.
14 //===----------------------------------------------------------------------===//
17 #include "llvm/Module.h"
18 #include "llvm/ModuleProvider.h"
19 #include "llvm/Support/CommandLine.h"
20 #include "llvm/Support/Streams.h"
21 #include "llvm/Target/SubtargetFeature.h"
22 #include "llvm/Target/TargetMachine.h"
23 #include "llvm/Target/TargetRegistry.h"
26 static cl::opt<std::string>
27 MArch("march", cl::desc("Architecture to generate assembly for (see --version)"));
29 static cl::opt<std::string>
31 cl::desc("Target a specific cpu type (-mcpu=help for details)"),
32 cl::value_desc("cpu-name"),
35 static cl::list<std::string>
38 cl::desc("Target specific attributes (-mattr=help for details)"),
39 cl::value_desc("a1,+a2,-a3,..."));
41 /// createInternal - Create an return a new JIT compiler if there is one
42 /// available for the current target. Otherwise, return null.
44 ExecutionEngine *JIT::createJIT(ModuleProvider *MP, std::string *ErrorStr,
45 JITMemoryManager *JMM,
46 CodeGenOpt::Level OptLevel,
47 bool AllocateGVsWithCode) {
48 const Target *TheTarget = 0;
51 TheTarget = TargetRegistry::getClosestTargetForJIT(Error);
58 for (TargetRegistry::iterator it = TargetRegistry::begin(),
59 ie = TargetRegistry::end(); it != ie; ++it) {
60 if (MArch == it->getName()) {
68 *ErrorStr = std::string("invalid target '" + MArch + "'.\n");
72 if (TheTarget->getJITMatchQuality() == 0) {
73 cerr << "WARNING: This target JIT is not designed for the host you are"
74 << " running. If bad things happen, please choose a different "
75 << "-march switch.\n";
79 // Package up features to be passed to target/subtarget
80 std::string FeaturesStr;
81 if (!MCPU.empty() || !MAttrs.empty()) {
82 SubtargetFeatures Features;
83 Features.setCPU(MCPU);
84 for (unsigned i = 0; i != MAttrs.size(); ++i)
85 Features.AddFeature(MAttrs[i]);
86 FeaturesStr = Features.getString();
89 // Allocate a target...
90 TargetMachine *Target =
91 TheTarget->createTargetMachine(*MP->getModule(), FeaturesStr);
92 assert(Target && "Could not allocate target machine!");
94 // If the target supports JIT code generation, return a new JIT now.
95 if (TargetJITInfo *TJ = Target->getJITInfo())
96 return new JIT(MP, *Target, *TJ, JMM, OptLevel, AllocateGVsWithCode);
99 *ErrorStr = "target does not support JIT code generation";