]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/include/llvm/ExecutionEngine/Orc/IRTransformLayer.h
Merge llvm, clang, lld, lldb, compiler-rt and libc++ r306325, and update
[FreeBSD/FreeBSD.git] / contrib / llvm / include / llvm / ExecutionEngine / Orc / IRTransformLayer.h
1 //===- IRTransformLayer.h - Run all IR through a functor --------*- 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 // Run all IR passed in through a user supplied functor.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #ifndef LLVM_EXECUTIONENGINE_ORC_IRTRANSFORMLAYER_H
15 #define LLVM_EXECUTIONENGINE_ORC_IRTRANSFORMLAYER_H
16
17 #include "llvm/ExecutionEngine/JITSymbol.h"
18 #include <string>
19
20 namespace llvm {
21 namespace orc {
22
23 /// @brief IR mutating layer.
24 ///
25 ///   This layer applies a user supplied transform to each module that is added,
26 /// then adds the transformed module to the layer below.
27 template <typename BaseLayerT, typename TransformFtor>
28 class IRTransformLayer {
29 public:
30
31   /// @brief Handle to a set of added modules.
32   using ModuleHandleT = typename BaseLayerT::ModuleHandleT;
33
34   /// @brief Construct an IRTransformLayer with the given BaseLayer
35   IRTransformLayer(BaseLayerT &BaseLayer,
36                    TransformFtor Transform = TransformFtor())
37     : BaseLayer(BaseLayer), Transform(std::move(Transform)) {}
38
39   /// @brief Apply the transform functor to the module, then add the module to
40   ///        the layer below, along with the memory manager and symbol resolver.
41   ///
42   /// @return A handle for the added modules.
43   template <typename MemoryManagerPtrT, typename SymbolResolverPtrT>
44   ModuleHandleT addModule(std::shared_ptr<Module> M,
45                           MemoryManagerPtrT MemMgr,
46                           SymbolResolverPtrT Resolver) {
47     return BaseLayer.addModule(Transform(std::move(M)), std::move(MemMgr),
48                                std::move(Resolver));
49   }
50
51   /// @brief Remove the module associated with the handle H.
52   void removeModule(ModuleHandleT H) { BaseLayer.removeModule(H); }
53
54   /// @brief Search for the given named symbol.
55   /// @param Name The name of the symbol to search for.
56   /// @param ExportedSymbolsOnly If true, search only for exported symbols.
57   /// @return A handle for the given named symbol, if it exists.
58   JITSymbol findSymbol(const std::string &Name, bool ExportedSymbolsOnly) {
59     return BaseLayer.findSymbol(Name, ExportedSymbolsOnly);
60   }
61
62   /// @brief Get the address of the given symbol in the context of the module
63   ///        represented by the handle H. This call is forwarded to the base
64   ///        layer's implementation.
65   /// @param H The handle for the module to search in.
66   /// @param Name The name of the symbol to search for.
67   /// @param ExportedSymbolsOnly If true, search only for exported symbols.
68   /// @return A handle for the given named symbol, if it is found in the
69   ///         given module.
70   JITSymbol findSymbolIn(ModuleHandleT H, const std::string &Name,
71                          bool ExportedSymbolsOnly) {
72     return BaseLayer.findSymbolIn(H, Name, ExportedSymbolsOnly);
73   }
74
75   /// @brief Immediately emit and finalize the module represented by the given
76   ///        handle.
77   /// @param H Handle for module to emit/finalize.
78   void emitAndFinalize(ModuleHandleT H) {
79     BaseLayer.emitAndFinalize(H);
80   }
81
82   /// @brief Access the transform functor directly.
83   TransformFtor& getTransform() { return Transform; }
84
85   /// @brief Access the mumate functor directly.
86   const TransformFtor& getTransform() const { return Transform; }
87
88 private:
89   BaseLayerT &BaseLayer;
90   TransformFtor Transform;
91 };
92
93 } // end namespace orc
94 } // end namespace llvm
95
96 #endif // LLVM_EXECUTIONENGINE_ORC_IRTRANSFORMLAYER_H