Don't codegen available_externally functions. Fixes http://llvm.org/PR5735.
[oota-llvm.git] / lib / ExecutionEngine / JIT / JIT.h
1 //===-- JIT.h - Class definition for the JIT --------------------*- C++ -*-===//
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 file defines the top-level JIT data structure.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #ifndef JIT_H
15 #define JIT_H
16
17 #include "llvm/ExecutionEngine/ExecutionEngine.h"
18 #include "llvm/PassManager.h"
19 #include "llvm/Support/ValueHandle.h"
20
21 namespace llvm {
22
23 class Function;
24 struct JITEvent_EmittedFunctionDetails;
25 class MachineCodeEmitter;
26 class MachineCodeInfo;
27 class TargetJITInfo;
28 class TargetMachine;
29
30 class JITState {
31 private:
32   FunctionPassManager PM;  // Passes to compile a function
33   ModuleProvider *MP;      // ModuleProvider used to create the PM
34
35   /// PendingFunctions - Functions which have not been code generated yet, but
36   /// were called from a function being code generated.
37   std::vector<AssertingVH<Function> > PendingFunctions;
38
39 public:
40   explicit JITState(ModuleProvider *MP) : PM(MP), MP(MP) {}
41
42   FunctionPassManager &getPM(const MutexGuard &L) {
43     return PM;
44   }
45   
46   ModuleProvider *getMP() const { return MP; }
47   std::vector<AssertingVH<Function> > &getPendingFunctions(const MutexGuard &L){
48     return PendingFunctions;
49   }
50 };
51
52
53 class JIT : public ExecutionEngine {
54   TargetMachine &TM;       // The current target we are compiling to
55   TargetJITInfo &TJI;      // The JITInfo for the target we are compiling to
56   JITCodeEmitter *JCE;     // JCE object
57   std::vector<JITEventListener*> EventListeners;
58
59   /// AllocateGVsWithCode - Some applications require that global variables and
60   /// code be allocated into the same region of memory, in which case this flag
61   /// should be set to true.  Doing so breaks freeMachineCodeForFunction.
62   bool AllocateGVsWithCode;
63
64   JITState *jitstate;
65
66   JIT(ModuleProvider *MP, TargetMachine &tm, TargetJITInfo &tji,
67       JITMemoryManager *JMM, CodeGenOpt::Level OptLevel,
68       bool AllocateGVsWithCode);
69 public:
70   ~JIT();
71
72   static void Register() {
73     JITCtor = create;
74   }
75   
76   /// getJITInfo - Return the target JIT information structure.
77   ///
78   TargetJITInfo &getJITInfo() const { return TJI; }
79
80   /// create - Create an return a new JIT compiler if there is one available
81   /// for the current target.  Otherwise, return null.
82   ///
83   static ExecutionEngine *create(ModuleProvider *MP,
84                                  std::string *Err,
85                                  JITMemoryManager *JMM,
86                                  CodeGenOpt::Level OptLevel =
87                                    CodeGenOpt::Default,
88                                  bool GVsWithCode = true,
89                                  CodeModel::Model CMM = CodeModel::Default) {
90     return ExecutionEngine::createJIT(MP, Err, JMM, OptLevel, GVsWithCode,
91                                       CMM);
92   }
93
94   virtual void addModuleProvider(ModuleProvider *MP);
95   
96   /// removeModuleProvider - Remove a ModuleProvider from the list of modules.
97   /// Relases the Module from the ModuleProvider, materializing it in the
98   /// process, and returns the materialized Module.
99   virtual Module *removeModuleProvider(ModuleProvider *MP,
100                                        std::string *ErrInfo = 0);
101
102   /// deleteModuleProvider - Remove a ModuleProvider from the list of modules,
103   /// and deletes the ModuleProvider and owned Module.  Avoids materializing 
104   /// the underlying module.
105   virtual void deleteModuleProvider(ModuleProvider *P,std::string *ErrInfo = 0);
106
107   /// materializeFunction - make sure the given function is fully read.  If the
108   /// module is corrupt, this returns true and fills in the optional string with
109   /// information about the problem.  If successful, this returns false.
110   ///
111   bool materializeFunction(Function *F, std::string *ErrInfo = 0);
112
113   /// runFunction - Start execution with the specified function and arguments.
114   ///
115   virtual GenericValue runFunction(Function *F,
116                                    const std::vector<GenericValue> &ArgValues);
117
118   /// getPointerToNamedFunction - This method returns the address of the
119   /// specified function by using the dlsym function call.  As such it is only
120   /// useful for resolving library symbols, not code generated symbols.
121   ///
122   /// If AbortOnFailure is false and no function with the given name is
123   /// found, this function silently returns a null pointer. Otherwise,
124   /// it prints a message to stderr and aborts.
125   ///
126   void *getPointerToNamedFunction(const std::string &Name,
127                                   bool AbortOnFailure = true);
128
129   // CompilationCallback - Invoked the first time that a call site is found,
130   // which causes lazy compilation of the target function.
131   //
132   static void CompilationCallback();
133
134   /// getPointerToFunction - This returns the address of the specified function,
135   /// compiling it if necessary.
136   ///
137   void *getPointerToFunction(Function *F);
138
139   void *getPointerToBasicBlock(BasicBlock *BB) {
140     assert(0 && "JIT does not support address-of-label yet!");
141     return 0;
142   }
143   
144   /// getOrEmitGlobalVariable - Return the address of the specified global
145   /// variable, possibly emitting it to memory if needed.  This is used by the
146   /// Emitter.
147   void *getOrEmitGlobalVariable(const GlobalVariable *GV);
148
149   /// getPointerToFunctionOrStub - If the specified function has been
150   /// code-gen'd, return a pointer to the function.  If not, compile it, or use
151   /// a stub to implement lazy compilation if available.
152   ///
153   void *getPointerToFunctionOrStub(Function *F);
154
155   /// recompileAndRelinkFunction - This method is used to force a function
156   /// which has already been compiled, to be compiled again, possibly
157   /// after it has been modified. Then the entry to the old copy is overwritten
158   /// with a branch to the new copy. If there was no old copy, this acts
159   /// just like JIT::getPointerToFunction().
160   ///
161   void *recompileAndRelinkFunction(Function *F);
162
163   /// freeMachineCodeForFunction - deallocate memory used to code-generate this
164   /// Function.
165   ///
166   void freeMachineCodeForFunction(Function *F);
167
168   /// addPendingFunction - while jitting non-lazily, a called but non-codegen'd
169   /// function was encountered.  Add it to a pending list to be processed after 
170   /// the current function.
171   ///
172   void addPendingFunction(Function *F);
173
174   /// getCodeEmitter - Return the code emitter this JIT is emitting into.
175   ///
176   JITCodeEmitter *getCodeEmitter() const { return JCE; }
177
178   /// selectTarget - Pick a target either via -march or by guessing the native
179   /// arch.  Add any CPU features specified via -mcpu or -mattr.
180   static TargetMachine *selectTarget(ModuleProvider *MP, std::string *Err);
181
182   static ExecutionEngine *createJIT(ModuleProvider *MP,
183                                     std::string *ErrorStr,
184                                     JITMemoryManager *JMM,
185                                     CodeGenOpt::Level OptLevel,
186                                     bool GVsWithCode,
187                                     CodeModel::Model CMM);
188
189   // Run the JIT on F and return information about the generated code
190   void runJITOnFunction(Function *F, MachineCodeInfo *MCI = 0);
191
192   virtual void RegisterJITEventListener(JITEventListener *L);
193   virtual void UnregisterJITEventListener(JITEventListener *L);
194   /// These functions correspond to the methods on JITEventListener.  They
195   /// iterate over the registered listeners and call the corresponding method on
196   /// each.
197   void NotifyFunctionEmitted(
198       const Function &F, void *Code, size_t Size,
199       const JITEvent_EmittedFunctionDetails &Details);
200   void NotifyFreeingMachineCode(void *OldPtr);
201
202 private:
203   static JITCodeEmitter *createEmitter(JIT &J, JITMemoryManager *JMM,
204                                        TargetMachine &tm);
205   void runJITOnFunctionUnlocked(Function *F, const MutexGuard &locked);
206   void updateFunctionStub(Function *F);
207
208 protected:
209
210   /// getMemoryforGV - Allocate memory for a global variable.
211   virtual char* getMemoryForGV(const GlobalVariable* GV);
212
213 };
214
215 } // End llvm namespace
216
217 #endif