1//===-- TargetSelect.cpp - Target Chooser Code ----------------------------===//
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This just asks the TargetRegistry for the appropriate target to use, and
11// allows the user to specify a specific one on the commandline with -march=x,
12// -mcpu=y, and -mattr=a,-b,+c. Clients should initialize targets prior to
13// calling selectTarget().
14//
15//===----------------------------------------------------------------------===//
16
17#include "llvm/ExecutionEngine/ExecutionEngine.h"
18#include "llvm/Module.h"
19#include "llvm/ADT/Triple.h"
20#include "llvm/MC/SubtargetFeature.h"
21#include "llvm/Target/TargetMachine.h"
22#include "llvm/Support/CommandLine.h"
23#include "llvm/Support/Host.h"
24#include "llvm/Support/TargetRegistry.h"
25
26using namespace llvm;
27
28TargetMachine *EngineBuilder::selectTarget() {
29  Triple TT(LLVM_HOSTTRIPLE);
30#if defined(__APPLE__)
31#if defined(__LP64__)
32  if (TT.isArch32Bit())
33    TT = TT.get64BitArchVariant();
34#else
35  if (TT.isArch64Bit())
36    TT = TT.get32BitArchVariant();
37#endif
38#endif // APPLE
39  return selectTarget(TT, MArch, MCPU, MAttrs);
40}
41
42/// selectTarget - Pick a target either via -march or by guessing the native
43/// arch.  Add any CPU features specified via -mcpu or -mattr.
44TargetMachine *EngineBuilder::selectTarget(const Triple &TargetTriple,
45                              StringRef MArch,
46                              StringRef MCPU,
47                              const SmallVectorImpl<std::string>& MAttrs) {
48  Triple TheTriple(TargetTriple);
49  if (TheTriple.getTriple().empty())
50    TheTriple.setTriple(sys::getDefaultTargetTriple());
51
52  // Adjust the triple to match what the user requested.
53  const Target *TheTarget = 0;
54  if (!MArch.empty()) {
55    for (TargetRegistry::iterator it = TargetRegistry::begin(),
56           ie = TargetRegistry::end(); it != ie; ++it) {
57      if (MArch == it->getName()) {
58        TheTarget = &*it;
59        break;
60      }
61    }
62
63    if (!TheTarget) {
64      if (ErrorStr)
65        *ErrorStr = "No available targets are compatible with this -march, "
66                    "see -version for the available targets.\n";
67      return 0;
68    }
69
70    // Adjust the triple to match (if known), otherwise stick with the
71    // requested/host triple.
72    Triple::ArchType Type = Triple::getArchTypeForLLVMName(MArch);
73    if (Type != Triple::UnknownArch)
74      TheTriple.setArch(Type);
75  } else {
76    std::string Error;
77    TheTarget = TargetRegistry::lookupTarget(TheTriple.getTriple(), Error);
78    if (TheTarget == 0) {
79      if (ErrorStr)
80        *ErrorStr = Error;
81      return 0;
82    }
83  }
84
85  // Package up features to be passed to target/subtarget
86  std::string FeaturesStr;
87  if (!MAttrs.empty()) {
88    SubtargetFeatures Features;
89    for (unsigned i = 0; i != MAttrs.size(); ++i)
90      Features.AddFeature(MAttrs[i]);
91    FeaturesStr = Features.getString();
92  }
93
94  // Allocate a target...
95  TargetMachine *Target = TheTarget->createTargetMachine(TheTriple.getTriple(),
96                                                         MCPU, FeaturesStr,
97                                                         Options,
98                                                         RelocModel, CMModel,
99                                                         OptLevel);
100  assert(Target && "Could not allocate target machine!");
101  return Target;
102}
103