TargetSelect.cpp revision 224145
1223013Sdim//===-- TargetSelect.cpp - Target Chooser Code ----------------------------===//
2223013Sdim//
3223013Sdim//                     The LLVM Compiler Infrastructure
4223013Sdim//
5223013Sdim// This file is distributed under the University of Illinois Open Source
6223013Sdim// License. See LICENSE.TXT for details.
7223013Sdim//
8223013Sdim//===----------------------------------------------------------------------===//
9223013Sdim//
10223013Sdim// This just asks the TargetRegistry for the appropriate JIT to use, and allows
11223013Sdim// the user to specify a specific one on the commandline with -march=x. Clients
12223013Sdim// should initialize targets prior to calling createJIT.
13223013Sdim//
14223013Sdim//===----------------------------------------------------------------------===//
15223013Sdim
16223013Sdim#include "llvm/ExecutionEngine/ExecutionEngine.h"
17223013Sdim#include "llvm/Module.h"
18223013Sdim#include "llvm/ADT/Triple.h"
19224145Sdim#include "llvm/MC/SubtargetFeature.h"
20223013Sdim#include "llvm/Support/CommandLine.h"
21223013Sdim#include "llvm/Support/raw_ostream.h"
22223013Sdim#include "llvm/Support/Host.h"
23223013Sdim#include "llvm/Target/TargetMachine.h"
24223013Sdim#include "llvm/Target/TargetRegistry.h"
25223013Sdimusing namespace llvm;
26223013Sdim
27223013Sdim/// selectTarget - Pick a target either via -march or by guessing the native
28223013Sdim/// arch.  Add any CPU features specified via -mcpu or -mattr.
29223013SdimTargetMachine *EngineBuilder::selectTarget(Module *Mod,
30223013Sdim                              StringRef MArch,
31223013Sdim                              StringRef MCPU,
32223013Sdim                              const SmallVectorImpl<std::string>& MAttrs,
33223013Sdim                              std::string *ErrorStr) {
34223013Sdim  Triple TheTriple(Mod->getTargetTriple());
35223013Sdim  if (TheTriple.getTriple().empty())
36223013Sdim    TheTriple.setTriple(sys::getHostTriple());
37223013Sdim
38223013Sdim  // Adjust the triple to match what the user requested.
39223013Sdim  const Target *TheTarget = 0;
40223013Sdim  if (!MArch.empty()) {
41223013Sdim    for (TargetRegistry::iterator it = TargetRegistry::begin(),
42223013Sdim           ie = TargetRegistry::end(); it != ie; ++it) {
43223013Sdim      if (MArch == it->getName()) {
44223013Sdim        TheTarget = &*it;
45223013Sdim        break;
46223013Sdim      }
47223013Sdim    }
48223013Sdim
49223013Sdim    if (!TheTarget) {
50223013Sdim      *ErrorStr = "No available targets are compatible with this -march, "
51223013Sdim        "see -version for the available targets.\n";
52223013Sdim      return 0;
53223013Sdim    }
54223013Sdim
55223013Sdim    // Adjust the triple to match (if known), otherwise stick with the
56223013Sdim    // module/host triple.
57223013Sdim    Triple::ArchType Type = Triple::getArchTypeForLLVMName(MArch);
58223013Sdim    if (Type != Triple::UnknownArch)
59223013Sdim      TheTriple.setArch(Type);
60223013Sdim  } else {
61223013Sdim    std::string Error;
62223013Sdim    TheTarget = TargetRegistry::lookupTarget(TheTriple.getTriple(), Error);
63223013Sdim    if (TheTarget == 0) {
64223013Sdim      if (ErrorStr)
65223013Sdim        *ErrorStr = Error;
66223013Sdim      return 0;
67223013Sdim    }
68223013Sdim  }
69223013Sdim
70223013Sdim  if (!TheTarget->hasJIT()) {
71223013Sdim    errs() << "WARNING: This target JIT is not designed for the host you are"
72223013Sdim           << " running.  If bad things happen, please choose a different "
73223013Sdim           << "-march switch.\n";
74223013Sdim  }
75223013Sdim
76223013Sdim  // Package up features to be passed to target/subtarget
77223013Sdim  std::string FeaturesStr;
78224145Sdim  if (!MAttrs.empty()) {
79223013Sdim    SubtargetFeatures Features;
80223013Sdim    for (unsigned i = 0; i != MAttrs.size(); ++i)
81223013Sdim      Features.AddFeature(MAttrs[i]);
82223013Sdim    FeaturesStr = Features.getString();
83223013Sdim  }
84223013Sdim
85223013Sdim  // Allocate a target...
86223013Sdim  TargetMachine *Target =
87224145Sdim    TheTarget->createTargetMachine(TheTriple.getTriple(), MCPU, FeaturesStr);
88223013Sdim  assert(Target && "Could not allocate target machine!");
89223013Sdim  return Target;
90223013Sdim}
91