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/Support/CBindingWrapping.h"
19 #include "llvm/Support/Compiler.h"
20 #include "llvm/Support/Options.h"
24 class LLVMContextImpl;
31 template <typename T> class SmallVectorImpl;
35 /// This is an important class for using LLVM in a threaded context. It
36 /// (opaquely) owns and manages the core "global" data of LLVM's core
37 /// infrastructure, including the type and constant uniquing tables.
38 /// LLVMContext itself provides no locking guarantees, so you should be careful
39 /// to have one context per thread.
42 LLVMContextImpl *const pImpl;
46 // Pinned metadata names, which always have the same value. This is a
47 // compile-time performance optimization, not a correctness optimization.
50 MD_tbaa = 1, // "tbaa"
51 MD_prof = 2, // "prof"
52 MD_fpmath = 3, // "fpmath"
53 MD_range = 4, // "range"
54 MD_tbaa_struct = 5, // "tbaa.struct"
55 MD_invariant_load = 6, // "invariant.load"
56 MD_alias_scope = 7, // "alias.scope"
57 MD_noalias = 8, // "noalias",
58 MD_nontemporal = 9, // "nontemporal"
59 MD_mem_parallel_loop_access = 10, // "llvm.mem.parallel_loop_access"
60 MD_nonnull = 11, // "nonnull"
61 MD_dereferenceable = 12, // "dereferenceable"
62 MD_dereferenceable_or_null = 13, // "dereferenceable_or_null"
63 MD_make_implicit = 14, // "make.implicit"
64 MD_unpredictable = 15, // "unpredictable"
65 MD_invariant_group = 16, // "invariant.group"
66 MD_align = 17 // "align"
69 /// Known operand bundle tag IDs, which always have the same value. All
70 /// operand bundle tags that LLVM has special knowledge of are listed here.
71 /// Additionally, this scheme allows LLVM to efficiently check for specific
72 /// operand bundle tags without comparing strings.
74 OB_deopt = 0, // "deopt"
75 OB_funclet = 1, // "funclet"
78 /// getMDKindID - Return a unique non-zero ID for the specified metadata kind.
79 /// This ID is uniqued across modules in the current LLVMContext.
80 unsigned getMDKindID(StringRef Name) const;
82 /// getMDKindNames - Populate client supplied SmallVector with the name for
83 /// custom metadata IDs registered in this LLVMContext.
84 void getMDKindNames(SmallVectorImpl<StringRef> &Result) const;
86 /// getOperandBundleTags - Populate client supplied SmallVector with the
87 /// bundle tags registered in this LLVMContext. The bundle tags are ordered
88 /// by increasing bundle IDs.
89 /// \see LLVMContext::getOperandBundleTagID
90 void getOperandBundleTags(SmallVectorImpl<StringRef> &Result) const;
92 /// getOperandBundleTagID - Maps a bundle tag to an integer ID. Every bundle
93 /// tag registered with an LLVMContext has an unique ID.
94 uint32_t getOperandBundleTagID(StringRef Tag) const;
97 /// Define the GC for a function
98 void setGC(const Function &Fn, std::string GCName);
100 /// Return the GC for a function
101 const std::string &getGC(const Function &Fn);
103 /// Remove the GC for a function
104 void deleteGC(const Function &Fn);
107 typedef void (*InlineAsmDiagHandlerTy)(const SMDiagnostic&, void *Context,
110 /// Defines the type of a diagnostic handler.
111 /// \see LLVMContext::setDiagnosticHandler.
112 /// \see LLVMContext::diagnose.
113 typedef void (*DiagnosticHandlerTy)(const DiagnosticInfo &DI, void *Context);
115 /// Defines the type of a yield callback.
116 /// \see LLVMContext::setYieldCallback.
117 typedef void (*YieldCallbackTy)(LLVMContext *Context, void *OpaqueHandle);
119 /// setInlineAsmDiagnosticHandler - This method sets a handler that is invoked
120 /// when problems with inline asm are detected by the backend. The first
121 /// argument is a function pointer and the second is a context pointer that
122 /// gets passed into the DiagHandler.
124 /// LLVMContext doesn't take ownership or interpret either of these
126 void setInlineAsmDiagnosticHandler(InlineAsmDiagHandlerTy DiagHandler,
127 void *DiagContext = nullptr);
129 /// getInlineAsmDiagnosticHandler - Return the diagnostic handler set by
130 /// setInlineAsmDiagnosticHandler.
131 InlineAsmDiagHandlerTy getInlineAsmDiagnosticHandler() const;
133 /// getInlineAsmDiagnosticContext - Return the diagnostic context set by
134 /// setInlineAsmDiagnosticHandler.
135 void *getInlineAsmDiagnosticContext() const;
137 /// setDiagnosticHandler - This method sets a handler that is invoked
138 /// when the backend needs to report anything to the user. The first
139 /// argument is a function pointer and the second is a context pointer that
140 /// gets passed into the DiagHandler. The third argument should be set to
141 /// true if the handler only expects enabled diagnostics.
143 /// LLVMContext doesn't take ownership or interpret either of these
145 void setDiagnosticHandler(DiagnosticHandlerTy DiagHandler,
146 void *DiagContext = nullptr,
147 bool RespectFilters = false);
149 /// getDiagnosticHandler - Return the diagnostic handler set by
150 /// setDiagnosticHandler.
151 DiagnosticHandlerTy getDiagnosticHandler() const;
153 /// getDiagnosticContext - Return the diagnostic context set by
154 /// setDiagnosticContext.
155 void *getDiagnosticContext() const;
157 /// \brief Report a message to the currently installed diagnostic handler.
159 /// This function returns, in particular in the case of error reporting
160 /// (DI.Severity == \a DS_Error), so the caller should leave the compilation
161 /// process in a self-consistent state, even though the generated code
162 /// need not be correct.
164 /// The diagnostic message will be implicitly prefixed with a severity keyword
165 /// according to \p DI.getSeverity(), i.e., "error: " for \a DS_Error,
166 /// "warning: " for \a DS_Warning, and "note: " for \a DS_Note.
167 void diagnose(const DiagnosticInfo &DI);
169 /// \brief Registers a yield callback with the given context.
171 /// The yield callback function may be called by LLVM to transfer control back
172 /// to the client that invoked the LLVM compilation. This can be used to yield
173 /// control of the thread, or perform periodic work needed by the client.
174 /// There is no guaranteed frequency at which callbacks must occur; in fact,
175 /// the client is not guaranteed to ever receive this callback. It is at the
176 /// sole discretion of LLVM to do so and only if it can guarantee that
177 /// suspending the thread won't block any forward progress in other LLVM
178 /// contexts in the same process.
180 /// At a suspend point, the state of the current LLVM context is intentionally
181 /// undefined. No assumptions about it can or should be made. Only LLVM
182 /// context API calls that explicitly state that they can be used during a
183 /// yield callback are allowed to be used. Any other API calls into the
184 /// context are not supported until the yield callback function returns
185 /// control to LLVM. Other LLVM contexts are unaffected by this restriction.
186 void setYieldCallback(YieldCallbackTy Callback, void *OpaqueHandle);
188 /// \brief Calls the yield callback (if applicable).
190 /// This transfers control of the current thread back to the client, which may
191 /// suspend the current thread. Only call this method when LLVM doesn't hold
192 /// any global mutex or cannot block the execution in another LLVM context.
195 /// emitError - Emit an error message to the currently installed error handler
196 /// with optional location information. This function returns, so code should
197 /// be prepared to drop the erroneous construct on the floor and "not crash".
198 /// The generated code need not be correct. The error message will be
199 /// implicitly prefixed with "error: " and should not end with a ".".
200 void emitError(unsigned LocCookie, const Twine &ErrorStr);
201 void emitError(const Instruction *I, const Twine &ErrorStr);
202 void emitError(const Twine &ErrorStr);
204 /// \brief Query for a debug option's value.
206 /// This function returns typed data populated from command line parsing.
207 template <typename ValT, typename Base, ValT(Base::*Mem)>
208 ValT getOption() const {
209 return OptionRegistry::instance().template get<ValT, Base, Mem>();
213 LLVMContext(LLVMContext&) = delete;
214 void operator=(LLVMContext&) = delete;
216 /// addModule - Register a module as being instantiated in this context. If
217 /// the context is deleted, the module will be deleted as well.
218 void addModule(Module*);
220 /// removeModule - Unregister a module from this context.
221 void removeModule(Module*);
223 // Module needs access to the add/removeModule methods.
227 /// getGlobalContext - Returns a global context. This is for LLVM clients that
228 /// only care about operating on a single thread.
229 extern LLVMContext &getGlobalContext();
231 // Create wrappers for C Binding types (see CBindingWrapping.h).
232 DEFINE_SIMPLE_CONVERSION_FUNCTIONS(LLVMContext, LLVMContextRef)
234 /* Specialized opaque context conversions.
236 inline LLVMContext **unwrap(LLVMContextRef* Tys) {
237 return reinterpret_cast<LLVMContext**>(Tys);
240 inline LLVMContextRef *wrap(const LLVMContext **Tys) {
241 return reinterpret_cast<LLVMContextRef*>(const_cast<LLVMContext**>(Tys));