blob: 3937fe55c24d07183bd57238e8108f6636b9bdf4 [file] [log] [blame]
Dylan Noblesmithe6297012011-05-06 22:20:09 +00001//===-- 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//
Dylan Noblesmith9ea47172011-12-12 04:20:36 +000010// 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().
Dylan Noblesmithe6297012011-05-06 22:20:09 +000014//
15//===----------------------------------------------------------------------===//
16
Dylan Noblesmith2ea29ba2011-05-13 21:36:16 +000017#include "llvm/ExecutionEngine/ExecutionEngine.h"
Dylan Noblesmithe6297012011-05-06 22:20:09 +000018#include "llvm/ADT/Triple.h"
Evan Chengab8be962011-06-29 01:14:12 +000019#include "llvm/MC/SubtargetFeature.h"
Dylan Noblesmithe6297012011-05-06 22:20:09 +000020#include "llvm/Target/TargetMachine.h"
Evan Cheng3e74d6f2011-08-24 18:08:43 +000021#include "llvm/Support/CommandLine.h"
22#include "llvm/Support/Host.h"
23#include "llvm/Support/TargetRegistry.h"
Dylan Noblesmith9ea47172011-12-12 04:20:36 +000024
Dylan Noblesmithe6297012011-05-06 22:20:09 +000025using namespace llvm;
26
27/// selectTarget - Pick a target either via -march or by guessing the native
28/// arch. Add any CPU features specified via -mcpu or -mattr.
Dylan Noblesmith9ea47172011-12-12 04:20:36 +000029TargetMachine *EngineBuilder::selectTarget(const Triple &TargetTriple,
Dylan Noblesmith2ea29ba2011-05-13 21:36:16 +000030 StringRef MArch,
31 StringRef MCPU,
32 const SmallVectorImpl<std::string>& MAttrs,
Peter Collingbourned40e1032011-12-07 23:58:57 +000033 const TargetOptions &Options,
Evan Cheng43966132011-07-19 06:37:02 +000034 Reloc::Model RM,
Evan Cheng34ad6db2011-07-20 07:51:56 +000035 CodeModel::Model CM,
Dylan Noblesmithd95e67d2011-12-01 21:49:21 +000036 CodeGenOpt::Level OL,
Dylan Noblesmith2ea29ba2011-05-13 21:36:16 +000037 std::string *ErrorStr) {
Dylan Noblesmith9ea47172011-12-12 04:20:36 +000038 Triple TheTriple(TargetTriple);
Dylan Noblesmithe6297012011-05-06 22:20:09 +000039 if (TheTriple.getTriple().empty())
Sebastian Pop01738642011-11-01 21:32:20 +000040 TheTriple.setTriple(sys::getDefaultTargetTriple());
Dylan Noblesmithe6297012011-05-06 22:20:09 +000041
42 // Adjust the triple to match what the user requested.
43 const Target *TheTarget = 0;
44 if (!MArch.empty()) {
45 for (TargetRegistry::iterator it = TargetRegistry::begin(),
46 ie = TargetRegistry::end(); it != ie; ++it) {
47 if (MArch == it->getName()) {
48 TheTarget = &*it;
49 break;
50 }
51 }
52
53 if (!TheTarget) {
54 *ErrorStr = "No available targets are compatible with this -march, "
55 "see -version for the available targets.\n";
56 return 0;
57 }
58
59 // Adjust the triple to match (if known), otherwise stick with the
Dylan Noblesmith9ea47172011-12-12 04:20:36 +000060 // requested/host triple.
Dylan Noblesmithe6297012011-05-06 22:20:09 +000061 Triple::ArchType Type = Triple::getArchTypeForLLVMName(MArch);
62 if (Type != Triple::UnknownArch)
63 TheTriple.setArch(Type);
64 } else {
65 std::string Error;
66 TheTarget = TargetRegistry::lookupTarget(TheTriple.getTriple(), Error);
67 if (TheTarget == 0) {
68 if (ErrorStr)
69 *ErrorStr = Error;
70 return 0;
71 }
72 }
73
Dylan Noblesmithe6297012011-05-06 22:20:09 +000074 // Package up features to be passed to target/subtarget
75 std::string FeaturesStr;
Evan Cheng276365d2011-06-30 01:53:36 +000076 if (!MAttrs.empty()) {
Dylan Noblesmithe6297012011-05-06 22:20:09 +000077 SubtargetFeatures Features;
Dylan Noblesmithe6297012011-05-06 22:20:09 +000078 for (unsigned i = 0; i != MAttrs.size(); ++i)
79 Features.AddFeature(MAttrs[i]);
80 FeaturesStr = Features.getString();
81 }
82
83 // Allocate a target...
Evan Cheng43966132011-07-19 06:37:02 +000084 TargetMachine *Target = TheTarget->createTargetMachine(TheTriple.getTriple(),
Evan Cheng34ad6db2011-07-20 07:51:56 +000085 MCPU, FeaturesStr,
Nick Lewycky8a8d4792011-12-02 22:16:29 +000086 Options,
Dylan Noblesmithd95e67d2011-12-01 21:49:21 +000087 RM, CM, OL);
Dylan Noblesmithe6297012011-05-06 22:20:09 +000088 assert(Target && "Could not allocate target machine!");
89 return Target;
90}