1 //===- ExecutionEngine.h - Abstract Execution Engine Interface --*- 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 abstract interface that implements execution support
11 // for LLVM.
12 //
13 //===----------------------------------------------------------------------===//
14 
15 #ifndef LLVM_EXECUTIONENGINE_EXECUTIONENGINE_H
16 #define LLVM_EXECUTIONENGINE_EXECUTIONENGINE_H
17 
18 #include "RuntimeDyld.h"
19 #include "llvm-c/ExecutionEngine.h"
20 #include "llvm/ADT/SmallVector.h"
21 #include "llvm/ADT/StringRef.h"
22 #include "llvm/IR/Module.h"
23 #include "llvm/IR/ValueHandle.h"
24 #include "llvm/IR/ValueMap.h"
25 #include "llvm/MC/MCCodeGenInfo.h"
26 #include "llvm/Object/Binary.h"
27 #include "llvm/Support/ErrorHandling.h"
28 #include "llvm/Support/Mutex.h"
29 #include "llvm/Target/TargetMachine.h"
30 #include "llvm/Target/TargetOptions.h"
31 #include <map>
32 #include <string>
33 #include <vector>
34 #include <functional>
35 
36 namespace llvm {
37 
38 struct GenericValue;
39 class Constant;
40 class DataLayout;
41 class ExecutionEngine;
42 class Function;
43 class GlobalVariable;
44 class GlobalValue;
45 class JITEventListener;
46 class MachineCodeInfo;
47 class MCJITMemoryManager;
48 class MutexGuard;
49 class ObjectCache;
50 class RTDyldMemoryManager;
51 class Triple;
52 class Type;
53 
54 namespace object {
55   class Archive;
56   class ObjectFile;
57 }
58 
59 /// \brief Helper class for helping synchronize access to the global address map
60 /// table.  Access to this class should be serialized under a mutex.
61 class ExecutionEngineState {
62 public:
63   typedef StringMap<uint64_t> GlobalAddressMapTy;
64 
65 private:
66 
67   /// GlobalAddressMap - A mapping between LLVM global symbol names values and
68   /// their actualized version...
69   GlobalAddressMapTy GlobalAddressMap;
70 
71   /// GlobalAddressReverseMap - This is the reverse mapping of GlobalAddressMap,
72   /// used to convert raw addresses into the LLVM global value that is emitted
73   /// at the address.  This map is not computed unless getGlobalValueAtAddress
74   /// is called at some point.
75   std::map<uint64_t, std::string> GlobalAddressReverseMap;
76 
77 public:
78 
getGlobalAddressMap()79   GlobalAddressMapTy &getGlobalAddressMap() {
80     return GlobalAddressMap;
81   }
82 
getGlobalAddressReverseMap()83   std::map<uint64_t, std::string> &getGlobalAddressReverseMap() {
84     return GlobalAddressReverseMap;
85   }
86 
87   /// \brief Erase an entry from the mapping table.
88   ///
89   /// \returns The address that \p ToUnmap was happed to.
90   uint64_t RemoveMapping(StringRef Name);
91 };
92 
93 using FunctionCreator = std::function<void *(const std::string &)>;
94 
95 /// \brief Abstract interface for implementation execution of LLVM modules,
96 /// designed to support both interpreter and just-in-time (JIT) compiler
97 /// implementations.
98 class ExecutionEngine {
99   /// The state object holding the global address mapping, which must be
100   /// accessed synchronously.
101   //
102   // FIXME: There is no particular need the entire map needs to be
103   // synchronized.  Wouldn't a reader-writer design be better here?
104   ExecutionEngineState EEState;
105 
106   /// The target data for the platform for which execution is being performed.
107   ///
108   /// Note: the DataLayout is LLVMContext specific because it has an
109   /// internal cache based on type pointers. It makes unsafe to reuse the
110   /// ExecutionEngine across context, we don't enforce this rule but undefined
111   /// behavior can occurs if the user tries to do it.
112   const DataLayout DL;
113 
114   /// Whether lazy JIT compilation is enabled.
115   bool CompilingLazily;
116 
117   /// Whether JIT compilation of external global variables is allowed.
118   bool GVCompilationDisabled;
119 
120   /// Whether the JIT should perform lookups of external symbols (e.g.,
121   /// using dlsym).
122   bool SymbolSearchingDisabled;
123 
124   /// Whether the JIT should verify IR modules during compilation.
125   bool VerifyModules;
126 
127   friend class EngineBuilder;  // To allow access to JITCtor and InterpCtor.
128 
129 protected:
130   /// The list of Modules that we are JIT'ing from.  We use a SmallVector to
131   /// optimize for the case where there is only one module.
132   SmallVector<std::unique_ptr<Module>, 1> Modules;
133 
134   /// getMemoryforGV - Allocate memory for a global variable.
135   virtual char *getMemoryForGV(const GlobalVariable *GV);
136 
137   static ExecutionEngine *(*MCJITCtor)(
138                                 std::unique_ptr<Module> M,
139                                 std::string *ErrorStr,
140                                 std::shared_ptr<MCJITMemoryManager> MM,
141                                 std::shared_ptr<RuntimeDyld::SymbolResolver> SR,
142                                 std::unique_ptr<TargetMachine> TM);
143 
144   static ExecutionEngine *(*OrcMCJITReplacementCtor)(
145                                 std::string *ErrorStr,
146                                 std::shared_ptr<MCJITMemoryManager> MM,
147                                 std::shared_ptr<RuntimeDyld::SymbolResolver> SR,
148                                 std::unique_ptr<TargetMachine> TM);
149 
150   static ExecutionEngine *(*InterpCtor)(std::unique_ptr<Module> M,
151                                         std::string *ErrorStr);
152 
153   /// LazyFunctionCreator - If an unknown function is needed, this function
154   /// pointer is invoked to create it.  If this returns null, the JIT will
155   /// abort.
156   FunctionCreator LazyFunctionCreator;
157 
158   /// getMangledName - Get mangled name.
159   std::string getMangledName(const GlobalValue *GV);
160 
161 public:
162   /// lock - This lock protects the ExecutionEngine and MCJIT classes. It must
163   /// be held while changing the internal state of any of those classes.
164   sys::Mutex lock;
165 
166   //===--------------------------------------------------------------------===//
167   //  ExecutionEngine Startup
168   //===--------------------------------------------------------------------===//
169 
170   virtual ~ExecutionEngine();
171 
172   /// Add a Module to the list of modules that we can JIT from.
addModule(std::unique_ptr<Module> M)173   virtual void addModule(std::unique_ptr<Module> M) {
174     Modules.push_back(std::move(M));
175   }
176 
177   /// addObjectFile - Add an ObjectFile to the execution engine.
178   ///
179   /// This method is only supported by MCJIT.  MCJIT will immediately load the
180   /// object into memory and adds its symbols to the list used to resolve
181   /// external symbols while preparing other objects for execution.
182   ///
183   /// Objects added using this function will not be made executable until
184   /// needed by another object.
185   ///
186   /// MCJIT will take ownership of the ObjectFile.
187   virtual void addObjectFile(std::unique_ptr<object::ObjectFile> O);
188   virtual void addObjectFile(object::OwningBinary<object::ObjectFile> O);
189 
190   /// addArchive - Add an Archive to the execution engine.
191   ///
192   /// This method is only supported by MCJIT.  MCJIT will use the archive to
193   /// resolve external symbols in objects it is loading.  If a symbol is found
194   /// in the Archive the contained object file will be extracted (in memory)
195   /// and loaded for possible execution.
196   virtual void addArchive(object::OwningBinary<object::Archive> A);
197 
198   //===--------------------------------------------------------------------===//
199 
getDataLayout()200   const DataLayout &getDataLayout() const { return DL; }
201 
202   /// removeModule - Remove a Module from the list of modules.  Returns true if
203   /// M is found.
204   virtual bool removeModule(Module *M);
205 
206   /// FindFunctionNamed - Search all of the active modules to find the function that
207   /// defines FnName.  This is very slow operation and shouldn't be used for
208   /// general code.
209   virtual Function *FindFunctionNamed(const char *FnName);
210 
211   /// FindGlobalVariableNamed - Search all of the active modules to find the global variable
212   /// that defines Name.  This is very slow operation and shouldn't be used for
213   /// general code.
214   virtual GlobalVariable *FindGlobalVariableNamed(const char *Name, bool AllowInternal = false);
215 
216   /// runFunction - Execute the specified function with the specified arguments,
217   /// and return the result.
218   virtual GenericValue runFunction(Function *F,
219                                    ArrayRef<GenericValue> ArgValues) = 0;
220 
221   /// getPointerToNamedFunction - This method returns the address of the
222   /// specified function by using the dlsym function call.  As such it is only
223   /// useful for resolving library symbols, not code generated symbols.
224   ///
225   /// If AbortOnFailure is false and no function with the given name is
226   /// found, this function silently returns a null pointer. Otherwise,
227   /// it prints a message to stderr and aborts.
228   ///
229   /// This function is deprecated for the MCJIT execution engine.
230   virtual void *getPointerToNamedFunction(StringRef Name,
231                                           bool AbortOnFailure = true) = 0;
232 
233   /// mapSectionAddress - map a section to its target address space value.
234   /// Map the address of a JIT section as returned from the memory manager
235   /// to the address in the target process as the running code will see it.
236   /// This is the address which will be used for relocation resolution.
mapSectionAddress(const void * LocalAddress,uint64_t TargetAddress)237   virtual void mapSectionAddress(const void *LocalAddress,
238                                  uint64_t TargetAddress) {
239     llvm_unreachable("Re-mapping of section addresses not supported with this "
240                      "EE!");
241   }
242 
243   /// generateCodeForModule - Run code generation for the specified module and
244   /// load it into memory.
245   ///
246   /// When this function has completed, all code and data for the specified
247   /// module, and any module on which this module depends, will be generated
248   /// and loaded into memory, but relocations will not yet have been applied
249   /// and all memory will be readable and writable but not executable.
250   ///
251   /// This function is primarily useful when generating code for an external
252   /// target, allowing the client an opportunity to remap section addresses
253   /// before relocations are applied.  Clients that intend to execute code
254   /// locally can use the getFunctionAddress call, which will generate code
255   /// and apply final preparations all in one step.
256   ///
257   /// This method has no effect for the interpeter.
generateCodeForModule(Module * M)258   virtual void generateCodeForModule(Module *M) {}
259 
260   /// finalizeObject - ensure the module is fully processed and is usable.
261   ///
262   /// It is the user-level function for completing the process of making the
263   /// object usable for execution.  It should be called after sections within an
264   /// object have been relocated using mapSectionAddress.  When this method is
265   /// called the MCJIT execution engine will reapply relocations for a loaded
266   /// object.  This method has no effect for the interpeter.
finalizeObject()267   virtual void finalizeObject() {}
268 
269   /// runStaticConstructorsDestructors - This method is used to execute all of
270   /// the static constructors or destructors for a program.
271   ///
272   /// \param isDtors - Run the destructors instead of constructors.
273   virtual void runStaticConstructorsDestructors(bool isDtors);
274 
275   /// This method is used to execute all of the static constructors or
276   /// destructors for a particular module.
277   ///
278   /// \param isDtors - Run the destructors instead of constructors.
279   void runStaticConstructorsDestructors(Module &module, bool isDtors);
280 
281 
282   /// runFunctionAsMain - This is a helper function which wraps runFunction to
283   /// handle the common task of starting up main with the specified argc, argv,
284   /// and envp parameters.
285   int runFunctionAsMain(Function *Fn, const std::vector<std::string> &argv,
286                         const char * const * envp);
287 
288 
289   /// addGlobalMapping - Tell the execution engine that the specified global is
290   /// at the specified location.  This is used internally as functions are JIT'd
291   /// and as global variables are laid out in memory.  It can and should also be
292   /// used by clients of the EE that want to have an LLVM global overlay
293   /// existing data in memory.  Mappings are automatically removed when their
294   /// GlobalValue is destroyed.
295   void addGlobalMapping(const GlobalValue *GV, void *Addr);
296   void addGlobalMapping(StringRef Name, uint64_t Addr);
297 
298   /// clearAllGlobalMappings - Clear all global mappings and start over again,
299   /// for use in dynamic compilation scenarios to move globals.
300   void clearAllGlobalMappings();
301 
302   /// clearGlobalMappingsFromModule - Clear all global mappings that came from a
303   /// particular module, because it has been removed from the JIT.
304   void clearGlobalMappingsFromModule(Module *M);
305 
306   /// updateGlobalMapping - Replace an existing mapping for GV with a new
307   /// address.  This updates both maps as required.  If "Addr" is null, the
308   /// entry for the global is removed from the mappings.  This returns the old
309   /// value of the pointer, or null if it was not in the map.
310   uint64_t updateGlobalMapping(const GlobalValue *GV, void *Addr);
311   uint64_t updateGlobalMapping(StringRef Name, uint64_t Addr);
312 
313   /// getAddressToGlobalIfAvailable - This returns the address of the specified
314   /// global symbol.
315   uint64_t getAddressToGlobalIfAvailable(StringRef S);
316 
317   /// getPointerToGlobalIfAvailable - This returns the address of the specified
318   /// global value if it is has already been codegen'd, otherwise it returns
319   /// null.
320   void *getPointerToGlobalIfAvailable(StringRef S);
321   void *getPointerToGlobalIfAvailable(const GlobalValue *GV);
322 
323   /// getPointerToGlobal - This returns the address of the specified global
324   /// value. This may involve code generation if it's a function.
325   ///
326   /// This function is deprecated for the MCJIT execution engine.  Use
327   /// getGlobalValueAddress instead.
328   void *getPointerToGlobal(const GlobalValue *GV);
329 
330   /// getPointerToFunction - The different EE's represent function bodies in
331   /// different ways.  They should each implement this to say what a function
332   /// pointer should look like.  When F is destroyed, the ExecutionEngine will
333   /// remove its global mapping and free any machine code.  Be sure no threads
334   /// are running inside F when that happens.
335   ///
336   /// This function is deprecated for the MCJIT execution engine.  Use
337   /// getFunctionAddress instead.
338   virtual void *getPointerToFunction(Function *F) = 0;
339 
340   /// getPointerToFunctionOrStub - If the specified function has been
341   /// code-gen'd, return a pointer to the function.  If not, compile it, or use
342   /// a stub to implement lazy compilation if available.  See
343   /// getPointerToFunction for the requirements on destroying F.
344   ///
345   /// This function is deprecated for the MCJIT execution engine.  Use
346   /// getFunctionAddress instead.
getPointerToFunctionOrStub(Function * F)347   virtual void *getPointerToFunctionOrStub(Function *F) {
348     // Default implementation, just codegen the function.
349     return getPointerToFunction(F);
350   }
351 
352   /// getGlobalValueAddress - Return the address of the specified global
353   /// value. This may involve code generation.
354   ///
355   /// This function should not be called with the interpreter engine.
getGlobalValueAddress(const std::string & Name)356   virtual uint64_t getGlobalValueAddress(const std::string &Name) {
357     // Default implementation for the interpreter.  MCJIT will override this.
358     // JIT and interpreter clients should use getPointerToGlobal instead.
359     return 0;
360   }
361 
362   /// getFunctionAddress - Return the address of the specified function.
363   /// This may involve code generation.
getFunctionAddress(const std::string & Name)364   virtual uint64_t getFunctionAddress(const std::string &Name) {
365     // Default implementation for the interpreter.  MCJIT will override this.
366     // Interpreter clients should use getPointerToFunction instead.
367     return 0;
368   }
369 
370   /// getGlobalValueAtAddress - Return the LLVM global value object that starts
371   /// at the specified address.
372   ///
373   const GlobalValue *getGlobalValueAtAddress(void *Addr);
374 
375   /// StoreValueToMemory - Stores the data in Val of type Ty at address Ptr.
376   /// Ptr is the address of the memory at which to store Val, cast to
377   /// GenericValue *.  It is not a pointer to a GenericValue containing the
378   /// address at which to store Val.
379   void StoreValueToMemory(const GenericValue &Val, GenericValue *Ptr,
380                           Type *Ty);
381 
382   void InitializeMemory(const Constant *Init, void *Addr);
383 
384   /// getOrEmitGlobalVariable - Return the address of the specified global
385   /// variable, possibly emitting it to memory if needed.  This is used by the
386   /// Emitter.
387   ///
388   /// This function is deprecated for the MCJIT execution engine.  Use
389   /// getGlobalValueAddress instead.
getOrEmitGlobalVariable(const GlobalVariable * GV)390   virtual void *getOrEmitGlobalVariable(const GlobalVariable *GV) {
391     return getPointerToGlobal((const GlobalValue *)GV);
392   }
393 
394   /// Registers a listener to be called back on various events within
395   /// the JIT.  See JITEventListener.h for more details.  Does not
396   /// take ownership of the argument.  The argument may be NULL, in
397   /// which case these functions do nothing.
RegisterJITEventListener(JITEventListener *)398   virtual void RegisterJITEventListener(JITEventListener *) {}
UnregisterJITEventListener(JITEventListener *)399   virtual void UnregisterJITEventListener(JITEventListener *) {}
400 
401   /// Sets the pre-compiled object cache.  The ownership of the ObjectCache is
402   /// not changed.  Supported by MCJIT but not the interpreter.
setObjectCache(ObjectCache *)403   virtual void setObjectCache(ObjectCache *) {
404     llvm_unreachable("No support for an object cache");
405   }
406 
407   /// setProcessAllSections (MCJIT Only): By default, only sections that are
408   /// "required for execution" are passed to the RTDyldMemoryManager, and other
409   /// sections are discarded. Passing 'true' to this method will cause
410   /// RuntimeDyld to pass all sections to its RTDyldMemoryManager regardless
411   /// of whether they are "required to execute" in the usual sense.
412   ///
413   /// Rationale: Some MCJIT clients want to be able to inspect metadata
414   /// sections (e.g. Dwarf, Stack-maps) to enable functionality or analyze
415   /// performance. Passing these sections to the memory manager allows the
416   /// client to make policy about the relevant sections, rather than having
417   /// MCJIT do it.
setProcessAllSections(bool ProcessAllSections)418   virtual void setProcessAllSections(bool ProcessAllSections) {
419     llvm_unreachable("No support for ProcessAllSections option");
420   }
421 
422   /// Return the target machine (if available).
getTargetMachine()423   virtual TargetMachine *getTargetMachine() { return nullptr; }
424 
425   /// DisableLazyCompilation - When lazy compilation is off (the default), the
426   /// JIT will eagerly compile every function reachable from the argument to
427   /// getPointerToFunction.  If lazy compilation is turned on, the JIT will only
428   /// compile the one function and emit stubs to compile the rest when they're
429   /// first called.  If lazy compilation is turned off again while some lazy
430   /// stubs are still around, and one of those stubs is called, the program will
431   /// abort.
432   ///
433   /// In order to safely compile lazily in a threaded program, the user must
434   /// ensure that 1) only one thread at a time can call any particular lazy
435   /// stub, and 2) any thread modifying LLVM IR must hold the JIT's lock
436   /// (ExecutionEngine::lock) or otherwise ensure that no other thread calls a
437   /// lazy stub.  See http://llvm.org/PR5184 for details.
438   void DisableLazyCompilation(bool Disabled = true) {
439     CompilingLazily = !Disabled;
440   }
isCompilingLazily()441   bool isCompilingLazily() const {
442     return CompilingLazily;
443   }
444 
445   /// DisableGVCompilation - If called, the JIT will abort if it's asked to
446   /// allocate space and populate a GlobalVariable that is not internal to
447   /// the module.
448   void DisableGVCompilation(bool Disabled = true) {
449     GVCompilationDisabled = Disabled;
450   }
isGVCompilationDisabled()451   bool isGVCompilationDisabled() const {
452     return GVCompilationDisabled;
453   }
454 
455   /// DisableSymbolSearching - If called, the JIT will not try to lookup unknown
456   /// symbols with dlsym.  A client can still use InstallLazyFunctionCreator to
457   /// resolve symbols in a custom way.
458   void DisableSymbolSearching(bool Disabled = true) {
459     SymbolSearchingDisabled = Disabled;
460   }
isSymbolSearchingDisabled()461   bool isSymbolSearchingDisabled() const {
462     return SymbolSearchingDisabled;
463   }
464 
465   /// Enable/Disable IR module verification.
466   ///
467   /// Note: Module verification is enabled by default in Debug builds, and
468   /// disabled by default in Release. Use this method to override the default.
setVerifyModules(bool Verify)469   void setVerifyModules(bool Verify) {
470     VerifyModules = Verify;
471   }
getVerifyModules()472   bool getVerifyModules() const {
473     return VerifyModules;
474   }
475 
476   /// InstallLazyFunctionCreator - If an unknown function is needed, the
477   /// specified function pointer is invoked to create it.  If it returns null,
478   /// the JIT will abort.
InstallLazyFunctionCreator(FunctionCreator C)479   void InstallLazyFunctionCreator(FunctionCreator C) {
480     LazyFunctionCreator = C;
481   }
482 
483 protected:
ExecutionEngine(const DataLayout DL)484   ExecutionEngine(const DataLayout DL) : DL(std::move(DL)){}
485   explicit ExecutionEngine(DataLayout DL, std::unique_ptr<Module> M);
486   explicit ExecutionEngine(std::unique_ptr<Module> M);
487 
488   void emitGlobals();
489 
490   void EmitGlobalVariable(const GlobalVariable *GV);
491 
492   GenericValue getConstantValue(const Constant *C);
493   void LoadValueFromMemory(GenericValue &Result, GenericValue *Ptr,
494                            Type *Ty);
495 
496 private:
497   void Init(std::unique_ptr<Module> M);
498 };
499 
500 namespace EngineKind {
501   // These are actually bitmasks that get or-ed together.
502   enum Kind {
503     JIT         = 0x1,
504     Interpreter = 0x2
505   };
506   const static Kind Either = (Kind)(JIT | Interpreter);
507 }
508 
509 /// Builder class for ExecutionEngines. Use this by stack-allocating a builder,
510 /// chaining the various set* methods, and terminating it with a .create()
511 /// call.
512 class EngineBuilder {
513 private:
514   std::unique_ptr<Module> M;
515   EngineKind::Kind WhichEngine;
516   std::string *ErrorStr;
517   CodeGenOpt::Level OptLevel;
518   std::shared_ptr<MCJITMemoryManager> MemMgr;
519   std::shared_ptr<RuntimeDyld::SymbolResolver> Resolver;
520   TargetOptions Options;
521   Reloc::Model RelocModel;
522   CodeModel::Model CMModel;
523   std::string MArch;
524   std::string MCPU;
525   SmallVector<std::string, 4> MAttrs;
526   bool VerifyModules;
527   bool UseOrcMCJITReplacement;
528 
529 public:
530   /// Default constructor for EngineBuilder.
531   EngineBuilder();
532 
533   /// Constructor for EngineBuilder.
534   EngineBuilder(std::unique_ptr<Module> M);
535 
536   // Out-of-line since we don't have the def'n of RTDyldMemoryManager here.
537   ~EngineBuilder();
538 
539   /// setEngineKind - Controls whether the user wants the interpreter, the JIT,
540   /// or whichever engine works.  This option defaults to EngineKind::Either.
setEngineKind(EngineKind::Kind w)541   EngineBuilder &setEngineKind(EngineKind::Kind w) {
542     WhichEngine = w;
543     return *this;
544   }
545 
546   /// setMCJITMemoryManager - Sets the MCJIT memory manager to use. This allows
547   /// clients to customize their memory allocation policies for the MCJIT. This
548   /// is only appropriate for the MCJIT; setting this and configuring the builder
549   /// to create anything other than MCJIT will cause a runtime error. If create()
550   /// is called and is successful, the created engine takes ownership of the
551   /// memory manager. This option defaults to NULL.
552   EngineBuilder &setMCJITMemoryManager(std::unique_ptr<RTDyldMemoryManager> mcjmm);
553 
554   EngineBuilder&
555   setMemoryManager(std::unique_ptr<MCJITMemoryManager> MM);
556 
557   EngineBuilder&
558   setSymbolResolver(std::unique_ptr<RuntimeDyld::SymbolResolver> SR);
559 
560   /// setErrorStr - Set the error string to write to on error.  This option
561   /// defaults to NULL.
setErrorStr(std::string * e)562   EngineBuilder &setErrorStr(std::string *e) {
563     ErrorStr = e;
564     return *this;
565   }
566 
567   /// setOptLevel - Set the optimization level for the JIT.  This option
568   /// defaults to CodeGenOpt::Default.
setOptLevel(CodeGenOpt::Level l)569   EngineBuilder &setOptLevel(CodeGenOpt::Level l) {
570     OptLevel = l;
571     return *this;
572   }
573 
574   /// setTargetOptions - Set the target options that the ExecutionEngine
575   /// target is using. Defaults to TargetOptions().
setTargetOptions(const TargetOptions & Opts)576   EngineBuilder &setTargetOptions(const TargetOptions &Opts) {
577     Options = Opts;
578     return *this;
579   }
580 
581   /// setRelocationModel - Set the relocation model that the ExecutionEngine
582   /// target is using. Defaults to target specific default "Reloc::Default".
setRelocationModel(Reloc::Model RM)583   EngineBuilder &setRelocationModel(Reloc::Model RM) {
584     RelocModel = RM;
585     return *this;
586   }
587 
588   /// setCodeModel - Set the CodeModel that the ExecutionEngine target
589   /// data is using. Defaults to target specific default
590   /// "CodeModel::JITDefault".
setCodeModel(CodeModel::Model M)591   EngineBuilder &setCodeModel(CodeModel::Model M) {
592     CMModel = M;
593     return *this;
594   }
595 
596   /// setMArch - Override the architecture set by the Module's triple.
setMArch(StringRef march)597   EngineBuilder &setMArch(StringRef march) {
598     MArch.assign(march.begin(), march.end());
599     return *this;
600   }
601 
602   /// setMCPU - Target a specific cpu type.
setMCPU(StringRef mcpu)603   EngineBuilder &setMCPU(StringRef mcpu) {
604     MCPU.assign(mcpu.begin(), mcpu.end());
605     return *this;
606   }
607 
608   /// setVerifyModules - Set whether the JIT implementation should verify
609   /// IR modules during compilation.
setVerifyModules(bool Verify)610   EngineBuilder &setVerifyModules(bool Verify) {
611     VerifyModules = Verify;
612     return *this;
613   }
614 
615   /// setMAttrs - Set cpu-specific attributes.
616   template<typename StringSequence>
setMAttrs(const StringSequence & mattrs)617   EngineBuilder &setMAttrs(const StringSequence &mattrs) {
618     MAttrs.clear();
619     MAttrs.append(mattrs.begin(), mattrs.end());
620     return *this;
621   }
622 
623   // \brief Use OrcMCJITReplacement instead of MCJIT. Off by default.
setUseOrcMCJITReplacement(bool UseOrcMCJITReplacement)624   void setUseOrcMCJITReplacement(bool UseOrcMCJITReplacement) {
625     this->UseOrcMCJITReplacement = UseOrcMCJITReplacement;
626   }
627 
628   TargetMachine *selectTarget();
629 
630   /// selectTarget - Pick a target either via -march or by guessing the native
631   /// arch.  Add any CPU features specified via -mcpu or -mattr.
632   TargetMachine *selectTarget(const Triple &TargetTriple,
633                               StringRef MArch,
634                               StringRef MCPU,
635                               const SmallVectorImpl<std::string>& MAttrs);
636 
create()637   ExecutionEngine *create() {
638     return create(selectTarget());
639   }
640 
641   ExecutionEngine *create(TargetMachine *TM);
642 };
643 
644 // Create wrappers for C Binding types (see CBindingWrapping.h).
645 DEFINE_SIMPLE_CONVERSION_FUNCTIONS(ExecutionEngine, LLVMExecutionEngineRef)
646 
647 } // End llvm namespace
648 
649 #endif
650