1 //===-- llvm/LLVMContext.h - Class for managing "global" state --*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file declares LLVMContext, a container of "global" state in LLVM, such
11 // as the global type and constant uniquing tables.
13 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_IR_LLVMCONTEXT_H
16 #define LLVM_IR_LLVMCONTEXT_H
18 #include "llvm-c/Core.h"
19 #include "llvm/Support/CBindingWrapping.h"
20 #include "llvm/Support/Compiler.h"
29 class LLVMContextImpl;
32 struct PassRunListener;
33 template <typename T> class SmallVectorImpl;
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.
45 LLVMContextImpl *const pImpl;
49 // Pinned metadata names, which always have the same value. This is a
50 // compile-time performance optimization, not a correctness optimization.
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"
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;
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;
70 typedef void (*InlineAsmDiagHandlerTy)(const SMDiagnostic&, void *Context,
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);
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.
83 /// LLVMContext doesn't take ownership or interpret either of these
85 void setInlineAsmDiagnosticHandler(InlineAsmDiagHandlerTy DiagHandler,
86 void *DiagContext = nullptr);
88 /// getInlineAsmDiagnosticHandler - Return the diagnostic handler set by
89 /// setInlineAsmDiagnosticHandler.
90 InlineAsmDiagHandlerTy getInlineAsmDiagnosticHandler() const;
92 /// getInlineAsmDiagnosticContext - Return the diagnostic context set by
93 /// setInlineAsmDiagnosticHandler.
94 void *getInlineAsmDiagnosticContext() const;
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.
101 /// LLVMContext doesn't take ownership or interpret either of these
103 void setDiagnosticHandler(DiagnosticHandlerTy DiagHandler,
104 void *DiagContext = nullptr);
106 /// getDiagnosticHandler - Return the diagnostic handler set by
107 /// setDiagnosticHandler.
108 DiagnosticHandlerTy getDiagnosticHandler() const;
110 /// getDiagnosticContext - Return the diagnostic context set by
111 /// setDiagnosticContext.
112 void *getDiagnosticContext() const;
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);
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);
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);
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);
153 LLVMContext(LLVMContext&) LLVM_DELETED_FUNCTION;
154 void operator=(LLVMContext&) LLVM_DELETED_FUNCTION;
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*);
160 /// removeModule - Unregister a module from this context.
161 void removeModule(Module*);
163 // Module needs access to the add/removeModule methods.
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();
171 // Create wrappers for C Binding types (see CBindingWrapping.h).
172 DEFINE_SIMPLE_CONVERSION_FUNCTIONS(LLVMContext, LLVMContextRef)
174 /* Specialized opaque context conversions.
176 inline LLVMContext **unwrap(LLVMContextRef* Tys) {
177 return reinterpret_cast<LLVMContext**>(Tys);
180 inline LLVMContextRef *wrap(const LLVMContext **Tys) {
181 return reinterpret_cast<LLVMContextRef*>(const_cast<LLVMContext**>(Tys));