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 /// selectTarget - Pick a target either via -march or by guessing the native
42 /// arch. Add any CPU features specified via -mcpu or -mattr.
43 TargetMachine *JIT::selectTarget(ModuleProvider *MP, std::string *ErrorStr) {
44 const Target *TheTarget = 0;
47 TheTarget = TargetRegistry::getClosestTargetForJIT(Error);
54 for (TargetRegistry::iterator it = TargetRegistry::begin(),
55 ie = TargetRegistry::end(); it != ie; ++it) {
56 if (MArch == it->getName()) {
64 *ErrorStr = std::string("invalid target '" + MArch + "'.\n");
68 if (TheTarget->getJITMatchQuality() == 0) {
69 cerr << "WARNING: This target JIT is not designed for the host you are"
70 << " running. If bad things happen, please choose a different "
71 << "-march switch.\n";
75 // Package up features to be passed to target/subtarget
76 std::string FeaturesStr;
77 if (!MCPU.empty() || !MAttrs.empty()) {
78 SubtargetFeatures Features;
79 Features.setCPU(MCPU);
80 for (unsigned i = 0; i != MAttrs.size(); ++i)
81 Features.AddFeature(MAttrs[i]);
82 FeaturesStr = Features.getString();
85 // Allocate a target...
86 TargetMachine *Target =
87 TheTarget->createTargetMachine(*MP->getModule(), FeaturesStr);
88 assert(Target && "Could not allocate target machine!");