bf968af4796b100e2cfdb57b55e76c8a9de747b7
[oota-llvm.git] / lib / ExecutionEngine / JIT / TargetSelect.cpp
1 //===-- TargetSelect.cpp - Target Chooser Code ----------------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This just asks the TargetMachineRegistry for the appropriate JIT to use, and
11 // allows the user to specify a specific one on the commandline with -march=x.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #include "JIT.h"
16 #include "llvm/Module.h"
17 #include "llvm/ModuleProvider.h"
18 #include "llvm/Target/SubtargetFeature.h"
19 #include "llvm/Target/TargetMachine.h"
20 #include "llvm/Target/TargetMachineRegistry.h"
21 using namespace llvm;
22
23 static cl::opt<const TargetMachineRegistry::Entry*, false, TargetNameParser>
24 MArch("march", cl::desc("Architecture to generate assembly for:"));
25
26 static cl::opt<std::string>
27 MCPU("mcpu", 
28   cl::desc("Target a specific cpu type (-mcpu=help for details)"),
29   cl::value_desc("cpu-name"),
30   cl::init(""));
31
32 static cl::list<std::string>
33 MAttrs("mattr", 
34   cl::CommaSeparated,
35   cl::desc("Target specific attributes (-mattr=help for details)"),
36   cl::value_desc("a1,+a2,-a3,..."));
37
38 /// create - Create an return a new JIT compiler if there is one available
39 /// for the current target.  Otherwise, return null.
40 ///
41 ExecutionEngine *JIT::create(ModuleProvider *MP, std::string *ErrorStr) {
42   const TargetMachineRegistry::Entry *TheArch = MArch;
43   if (TheArch == 0) {
44     std::string Error;
45     TheArch = TargetMachineRegistry::getClosestTargetForJIT(Error);
46     if (TheArch == 0) {
47       if (ErrorStr)
48         *ErrorStr = Error;
49       return 0;
50     }
51   } else if (TheArch->JITMatchQualityFn() == 0) {
52     cerr << "WARNING: This target JIT is not designed for the host you are"
53          << " running.  If bad things happen, please choose a different "
54          << "-march switch.\n";
55   }
56
57   // Package up features to be passed to target/subtarget
58   std::string FeaturesStr;
59   if (MCPU.size() || MAttrs.size()) {
60     SubtargetFeatures Features;
61     Features.setCPU(MCPU);
62     for (unsigned i = 0; i != MAttrs.size(); ++i)
63       Features.AddFeature(MAttrs[i]);
64     FeaturesStr = Features.getString();
65   }
66
67   // Allocate a target...
68   TargetMachine *Target = TheArch->CtorFn(*MP->getModule(), FeaturesStr);
69   assert(Target && "Could not allocate target machine!");
70
71   // If the target supports JIT code generation, return a new JIT now.
72   if (TargetJITInfo *TJ = Target->getJITInfo())
73     return new JIT(MP, *Target, *TJ);
74
75   if (ErrorStr)
76     *ErrorStr = "target does not support JIT code generation";
77   return 0;
78 }