Eliminate IntrinsicLowering from TargetMachine.
[oota-llvm.git] / include / llvm / Target / TargetMachineRegistry.h
1 //===-- Target/TargetMachineRegistry.h - Target Registration ----*- C++ -*-===//
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 file exposes two classes: the TargetMachineRegistry class, which allows
11 // tools to inspect all of registered targets, and the RegisterTarget class,
12 // which TargetMachine implementations should use to register themselves with
13 // the system.
14 //
15 //===----------------------------------------------------------------------===//
16
17 #ifndef LLVM_TARGET_TARGETMACHINEREGISTRY_H
18 #define LLVM_TARGET_TARGETMACHINEREGISTRY_H
19
20 #include "llvm/Support/CommandLine.h"
21
22 namespace llvm {
23   class Module;
24   class TargetMachine;
25
26   struct TargetMachineRegistry {
27     struct Entry;
28
29     /// TargetMachineRegistry::getList - This static method returns the list of
30     /// target machines that are registered with the system.
31     static const Entry *getList() { return List; }
32
33     /// getClosestStaticTargetForModule - Given an LLVM module, pick the best
34     /// target that is compatible with the module.  If no close target can be
35     /// found, this returns null and sets the Error string to a reason.
36     static const Entry *getClosestStaticTargetForModule(const Module &M,
37                                                         std::string &Error);
38
39     /// getClosestTargetForJIT - Given an LLVM module, pick the best target that
40     /// is compatible with the current host and the specified module.  If no
41     /// close target can be found, this returns null and sets the Error string
42     /// to a reason.
43     static const Entry *getClosestTargetForJIT(std::string &Error);
44
45
46     /// Entry - One instance of this struct is created for each target that is
47     /// registered.
48     struct Entry {
49       const char *Name;
50       const char *ShortDesc;
51       TargetMachine *(*CtorFn)(const Module &, const std::string &);
52       unsigned (*ModuleMatchQualityFn)(const Module &M);
53       unsigned (*JITMatchQualityFn)();
54
55       const Entry *getNext() const { return Next; }
56
57     protected:
58       Entry(const char *N, const char *SD,
59             TargetMachine *(*CF)(const Module &, const std::string &),
60             unsigned (*MMF)(const Module &M), unsigned (*JMF)());
61     private:
62       const Entry *Next;  // Next entry in the linked list.
63     };
64
65   private:
66     static const Entry *List;
67   };
68
69   //===--------------------------------------------------------------------===//
70   /// RegisterTarget - This class is used to make targets automatically register
71   /// themselves with the tool they are linked.  Targets should define an
72   /// instance of this and implement the static methods described in the
73   /// TargetMachine comments..
74   template<class TargetMachineImpl>
75   struct RegisterTarget : public TargetMachineRegistry::Entry {
76     RegisterTarget(const char *Name, const char *ShortDesc) :
77       TargetMachineRegistry::Entry(Name, ShortDesc, &Allocator,
78                                    &TargetMachineImpl::getModuleMatchQuality,
79                                    &TargetMachineImpl::getJITMatchQuality) {
80     }
81   private:
82     static TargetMachine *Allocator(const Module &M, const std::string &FS) {
83       return new TargetMachineImpl(M, FS);
84     }
85   };
86
87   /// TargetRegistrationListener - This class allows code to listen for targets
88   /// that are dynamically registered, and be notified of it when they are.
89   class TargetRegistrationListener {
90     TargetRegistrationListener **Prev, *Next;
91   public:
92     TargetRegistrationListener();
93     virtual ~TargetRegistrationListener();
94
95     TargetRegistrationListener *getNext() const { return Next; }
96
97     virtual void targetRegistered(const TargetMachineRegistry::Entry *E) = 0;
98   };
99
100
101   //===--------------------------------------------------------------------===//
102   /// TargetNameParser - This option can be used to provide a command line
103   /// option to choose among the various registered targets (commonly -march).
104   class TargetNameParser : public TargetRegistrationListener,
105     public cl::parser<const TargetMachineRegistry::Entry*> {
106   public:
107     void initialize(cl::Option &O) {
108       for (const TargetMachineRegistry::Entry *E =
109              TargetMachineRegistry::getList(); E; E = E->getNext())
110         Values.push_back(std::make_pair(E->Name,
111                                         std::make_pair(E, E->ShortDesc)));
112       cl::parser<const TargetMachineRegistry::Entry*>::initialize(O);
113     }
114
115     virtual void targetRegistered(const TargetMachineRegistry::Entry *E) {
116       Values.push_back(std::make_pair(E->Name,
117                                       std::make_pair(E, E->ShortDesc)));
118     }
119   };
120 }
121
122 #endif