[PM] Add pass run listeners to the pass manager.
[oota-llvm.git] / include / llvm / IR / LLVMContext.h
1 //===-- llvm/LLVMContext.h - Class for managing "global" state --*- 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 declares LLVMContext, a container of "global" state in LLVM, such
11 // as the global type and constant uniquing tables.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #ifndef LLVM_IR_LLVMCONTEXT_H
16 #define LLVM_IR_LLVMCONTEXT_H
17
18 #include "llvm-c/Core.h"
19 #include "llvm/Support/CBindingWrapping.h"
20 #include "llvm/Support/Compiler.h"
21
22 namespace llvm {
23
24 class BasicBlock;
25 class DebugLoc;
26 class DiagnosticInfo;
27 class Function;
28 class Instruction;
29 class LLVMContextImpl;
30 class Module;
31 class Pass;
32 struct PassRunListener;
33 template <typename T> class SmallVectorImpl;
34 class SMDiagnostic;
35 class StringRef;
36 class Twine;
37
38 /// This is an important class for using LLVM in a threaded context.  It
39 /// (opaquely) owns and manages the core "global" data of LLVM's core
40 /// infrastructure, including the type and constant uniquing tables.
41 /// LLVMContext itself provides no locking guarantees, so you should be careful
42 /// to have one context per thread.
43 class LLVMContext {
44 public:
45   LLVMContextImpl *const pImpl;
46   LLVMContext();
47   ~LLVMContext();
48
49   // Pinned metadata names, which always have the same value.  This is a
50   // compile-time performance optimization, not a correctness optimization.
51   enum {
52     MD_dbg = 0,  // "dbg"
53     MD_tbaa = 1, // "tbaa"
54     MD_prof = 2,  // "prof"
55     MD_fpmath = 3,  // "fpmath"
56     MD_range = 4, // "range"
57     MD_tbaa_struct = 5, // "tbaa.struct"
58     MD_invariant_load = 6 // "invariant.load"
59   };
60
61   /// getMDKindID - Return a unique non-zero ID for the specified metadata kind.
62   /// This ID is uniqued across modules in the current LLVMContext.
63   unsigned getMDKindID(StringRef Name) const;
64
65   /// getMDKindNames - Populate client supplied SmallVector with the name for
66   /// custom metadata IDs registered in this LLVMContext.
67   void getMDKindNames(SmallVectorImpl<StringRef> &Result) const;
68
69
70   typedef void (*InlineAsmDiagHandlerTy)(const SMDiagnostic&, void *Context,
71                                          unsigned LocCookie);
72
73   /// Defines the type of a diagnostic handler.
74   /// \see LLVMContext::setDiagnosticHandler.
75   /// \see LLVMContext::diagnose.
76   typedef void (*DiagnosticHandlerTy)(const DiagnosticInfo &DI, void *Context);
77
78   /// setInlineAsmDiagnosticHandler - This method sets a handler that is invoked
79   /// when problems with inline asm are detected by the backend.  The first
80   /// argument is a function pointer and the second is a context pointer that
81   /// gets passed into the DiagHandler.
82   ///
83   /// LLVMContext doesn't take ownership or interpret either of these
84   /// pointers.
85   void setInlineAsmDiagnosticHandler(InlineAsmDiagHandlerTy DiagHandler,
86                                      void *DiagContext = nullptr);
87
88   /// getInlineAsmDiagnosticHandler - Return the diagnostic handler set by
89   /// setInlineAsmDiagnosticHandler.
90   InlineAsmDiagHandlerTy getInlineAsmDiagnosticHandler() const;
91
92   /// getInlineAsmDiagnosticContext - Return the diagnostic context set by
93   /// setInlineAsmDiagnosticHandler.
94   void *getInlineAsmDiagnosticContext() const;
95
96   /// setDiagnosticHandler - This method sets a handler that is invoked
97   /// when the backend needs to report anything to the user.  The first
98   /// argument is a function pointer and the second is a context pointer that
99   /// gets passed into the DiagHandler.
100   ///
101   /// LLVMContext doesn't take ownership or interpret either of these
102   /// pointers.
103   void setDiagnosticHandler(DiagnosticHandlerTy DiagHandler,
104                             void *DiagContext = nullptr);
105
106   /// getDiagnosticHandler - Return the diagnostic handler set by
107   /// setDiagnosticHandler.
108   DiagnosticHandlerTy getDiagnosticHandler() const;
109
110   /// getDiagnosticContext - Return the diagnostic context set by
111   /// setDiagnosticContext.
112   void *getDiagnosticContext() const;
113
114   /// diagnose - Report a message to the currently installed diagnostic handler.
115   /// This function returns, in particular in the case of error reporting
116   /// (DI.Severity == RS_Error), so the caller should leave the compilation
117   /// process in a self-consistent state, even though the generated code
118   /// need not be correct.
119   /// The diagnostic message will be implicitly prefixed with a severity
120   /// keyword according to \p DI.getSeverity(), i.e., "error: "
121   /// for RS_Error, "warning: " for RS_Warning, and "note: " for RS_Note.
122   void diagnose(const DiagnosticInfo &DI);
123
124   /// emitError - Emit an error message to the currently installed error handler
125   /// with optional location information.  This function returns, so code should
126   /// be prepared to drop the erroneous construct on the floor and "not crash".
127   /// The generated code need not be correct.  The error message will be
128   /// implicitly prefixed with "error: " and should not end with a ".".
129   void emitError(unsigned LocCookie, const Twine &ErrorStr);
130   void emitError(const Instruction *I, const Twine &ErrorStr);
131   void emitError(const Twine &ErrorStr);
132
133   /// emitOptimizationRemark - Emit an optimization remark message. \p PassName
134   /// is the name of the pass emitting the message. If -Rpass= is given
135   /// and \p PassName matches the regular expression in -Rpass, then the
136   /// remark will be emitted. \p Fn is the function triggering the remark,
137   /// \p DLoc is the debug location where the diagnostic is generated.
138   /// \p Msg is the message string to use.
139   void emitOptimizationRemark(const char *PassName, const Function &Fn,
140                               const DebugLoc &DLoc, const Twine &Msg);
141
142   /// \brief Notify that we finished running a pass.
143   void notifyPassRun(Pass *P, Module *M, Function *F = nullptr,
144                      BasicBlock *BB = nullptr);
145   /// \brief Register the given PassRunListener to receive notifyPassRun()
146   /// callbacks whenever a pass ran. The context will take ownership of the
147   /// listener and free it when the context is destroyed.
148   void addRunListener(PassRunListener *L);
149   /// \brief Unregister a PassRunListener so that it no longer receives
150   /// notifyPassRun() callbacks. Remove and free the listener from the context.
151   void removeRunListener(PassRunListener *L);
152 private:
153   LLVMContext(LLVMContext&) LLVM_DELETED_FUNCTION;
154   void operator=(LLVMContext&) LLVM_DELETED_FUNCTION;
155
156   /// addModule - Register a module as being instantiated in this context.  If
157   /// the context is deleted, the module will be deleted as well.
158   void addModule(Module*);
159
160   /// removeModule - Unregister a module from this context.
161   void removeModule(Module*);
162
163   // Module needs access to the add/removeModule methods.
164   friend class Module;
165 };
166
167 /// getGlobalContext - Returns a global context.  This is for LLVM clients that
168 /// only care about operating on a single thread.
169 extern LLVMContext &getGlobalContext();
170
171 // Create wrappers for C Binding types (see CBindingWrapping.h).
172 DEFINE_SIMPLE_CONVERSION_FUNCTIONS(LLVMContext, LLVMContextRef)
173
174 /* Specialized opaque context conversions.
175  */
176 inline LLVMContext **unwrap(LLVMContextRef* Tys) {
177   return reinterpret_cast<LLVMContext**>(Tys);
178 }
179
180 inline LLVMContextRef *wrap(const LLVMContext **Tys) {
181   return reinterpret_cast<LLVMContextRef*>(const_cast<LLVMContext**>(Tys));
182 }
183
184 }
185
186 #endif