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/Types.h"
19 #include "llvm/Support/CBindingWrapping.h"
20 #include "llvm/Support/Options.h"
28 enum DiagnosticSeverity : char;
31 class LLVMContextImpl;
34 template <typename T> class SmallVectorImpl;
43 } // end namespace yaml
45 /// This is an important class for using LLVM in a threaded context. It
46 /// (opaquely) owns and manages the core "global" data of LLVM's core
47 /// infrastructure, including the type and constant uniquing tables.
48 /// LLVMContext itself provides no locking guarantees, so you should be careful
49 /// to have one context per thread.
52 LLVMContextImpl *const pImpl;
54 LLVMContext(LLVMContext &) = delete;
55 LLVMContext &operator=(const LLVMContext &) = delete;
58 // Pinned metadata names, which always have the same value. This is a
59 // compile-time performance optimization, not a correctness optimization.
62 MD_tbaa = 1, // "tbaa"
63 MD_prof = 2, // "prof"
64 MD_fpmath = 3, // "fpmath"
65 MD_range = 4, // "range"
66 MD_tbaa_struct = 5, // "tbaa.struct"
67 MD_invariant_load = 6, // "invariant.load"
68 MD_alias_scope = 7, // "alias.scope"
69 MD_noalias = 8, // "noalias",
70 MD_nontemporal = 9, // "nontemporal"
71 MD_mem_parallel_loop_access = 10, // "llvm.mem.parallel_loop_access"
72 MD_nonnull = 11, // "nonnull"
73 MD_dereferenceable = 12, // "dereferenceable"
74 MD_dereferenceable_or_null = 13, // "dereferenceable_or_null"
75 MD_make_implicit = 14, // "make.implicit"
76 MD_unpredictable = 15, // "unpredictable"
77 MD_invariant_group = 16, // "invariant.group"
78 MD_align = 17, // "align"
79 MD_loop = 18, // "llvm.loop"
80 MD_type = 19, // "type"
81 MD_section_prefix = 20, // "section_prefix"
82 MD_absolute_symbol = 21, // "absolute_symbol"
83 MD_associated = 22, // "associated"
86 /// Known operand bundle tag IDs, which always have the same value. All
87 /// operand bundle tags that LLVM has special knowledge of are listed here.
88 /// Additionally, this scheme allows LLVM to efficiently check for specific
89 /// operand bundle tags without comparing strings.
91 OB_deopt = 0, // "deopt"
92 OB_funclet = 1, // "funclet"
93 OB_gc_transition = 2, // "gc-transition"
96 /// getMDKindID - Return a unique non-zero ID for the specified metadata kind.
97 /// This ID is uniqued across modules in the current LLVMContext.
98 unsigned getMDKindID(StringRef Name) const;
100 /// getMDKindNames - Populate client supplied SmallVector with the name for
101 /// custom metadata IDs registered in this LLVMContext.
102 void getMDKindNames(SmallVectorImpl<StringRef> &Result) const;
104 /// getOperandBundleTags - Populate client supplied SmallVector with the
105 /// bundle tags registered in this LLVMContext. The bundle tags are ordered
106 /// by increasing bundle IDs.
107 /// \see LLVMContext::getOperandBundleTagID
108 void getOperandBundleTags(SmallVectorImpl<StringRef> &Result) const;
110 /// getOperandBundleTagID - Maps a bundle tag to an integer ID. Every bundle
111 /// tag registered with an LLVMContext has an unique ID.
112 uint32_t getOperandBundleTagID(StringRef Tag) const;
114 /// Define the GC for a function
115 void setGC(const Function &Fn, std::string GCName);
117 /// Return the GC for a function
118 const std::string &getGC(const Function &Fn);
120 /// Remove the GC for a function
121 void deleteGC(const Function &Fn);
123 /// Return true if the Context runtime configuration is set to discard all
124 /// value names. When true, only GlobalValue names will be available in the
126 bool shouldDiscardValueNames() const;
128 /// Set the Context runtime configuration to discard all value name (but
129 /// GlobalValue). Clients can use this flag to save memory and runtime,
130 /// especially in release mode.
131 void setDiscardValueNames(bool Discard);
133 /// Whether there is a string map for uniquing debug info
134 /// identifiers across the context. Off by default.
135 bool isODRUniquingDebugTypes() const;
136 void enableDebugTypeODRUniquing();
137 void disableDebugTypeODRUniquing();
139 using InlineAsmDiagHandlerTy = void (*)(const SMDiagnostic&, void *Context,
142 /// Defines the type of a diagnostic handler.
143 /// \see LLVMContext::setDiagnosticHandler.
144 /// \see LLVMContext::diagnose.
145 using DiagnosticHandlerTy = void (*)(const DiagnosticInfo &DI, void *Context);
147 /// Defines the type of a yield callback.
148 /// \see LLVMContext::setYieldCallback.
149 using YieldCallbackTy = void (*)(LLVMContext *Context, void *OpaqueHandle);
151 /// setInlineAsmDiagnosticHandler - This method sets a handler that is invoked
152 /// when problems with inline asm are detected by the backend. The first
153 /// argument is a function pointer and the second is a context pointer that
154 /// gets passed into the DiagHandler.
156 /// LLVMContext doesn't take ownership or interpret either of these
158 void setInlineAsmDiagnosticHandler(InlineAsmDiagHandlerTy DiagHandler,
159 void *DiagContext = nullptr);
161 /// getInlineAsmDiagnosticHandler - Return the diagnostic handler set by
162 /// setInlineAsmDiagnosticHandler.
163 InlineAsmDiagHandlerTy getInlineAsmDiagnosticHandler() const;
165 /// getInlineAsmDiagnosticContext - Return the diagnostic context set by
166 /// setInlineAsmDiagnosticHandler.
167 void *getInlineAsmDiagnosticContext() const;
169 /// setDiagnosticHandler - This method sets a handler that is invoked
170 /// when the backend needs to report anything to the user. The first
171 /// argument is a function pointer and the second is a context pointer that
172 /// gets passed into the DiagHandler. The third argument should be set to
173 /// true if the handler only expects enabled diagnostics.
175 /// LLVMContext doesn't take ownership or interpret either of these
177 void setDiagnosticHandler(DiagnosticHandlerTy DiagHandler,
178 void *DiagContext = nullptr,
179 bool RespectFilters = false);
181 /// getDiagnosticHandler - Return the diagnostic handler set by
182 /// setDiagnosticHandler.
183 DiagnosticHandlerTy getDiagnosticHandler() const;
185 /// getDiagnosticContext - Return the diagnostic context set by
186 /// setDiagnosticContext.
187 void *getDiagnosticContext() const;
189 /// \brief Return if a code hotness metric should be included in optimization
191 bool getDiagnosticsHotnessRequested() const;
192 /// \brief Set if a code hotness metric should be included in optimization
194 void setDiagnosticsHotnessRequested(bool Requested);
196 /// \brief Return the minimum hotness value a diagnostic would need in order
197 /// to be included in optimization diagnostics. If there is no minimum, this
199 uint64_t getDiagnosticsHotnessThreshold() const;
201 /// \brief Set the minimum hotness value a diagnostic needs in order to be
202 /// included in optimization diagnostics.
203 void setDiagnosticsHotnessThreshold(uint64_t Threshold);
205 /// \brief Return the YAML file used by the backend to save optimization
206 /// diagnostics. If null, diagnostics are not saved in a file but only
207 /// emitted via the diagnostic handler.
208 yaml::Output *getDiagnosticsOutputFile();
209 /// Set the diagnostics output file used for optimization diagnostics.
211 /// By default or if invoked with null, diagnostics are not saved in a file
212 /// but only emitted via the diagnostic handler. Even if an output file is
213 /// set, the handler is invoked for each diagnostic message.
214 void setDiagnosticsOutputFile(std::unique_ptr<yaml::Output> F);
216 /// \brief Get the prefix that should be printed in front of a diagnostic of
217 /// the given \p Severity
218 static const char *getDiagnosticMessagePrefix(DiagnosticSeverity Severity);
220 /// \brief Report a message to the currently installed diagnostic handler.
222 /// This function returns, in particular in the case of error reporting
223 /// (DI.Severity == \a DS_Error), so the caller should leave the compilation
224 /// process in a self-consistent state, even though the generated code
225 /// need not be correct.
227 /// The diagnostic message will be implicitly prefixed with a severity keyword
228 /// according to \p DI.getSeverity(), i.e., "error: " for \a DS_Error,
229 /// "warning: " for \a DS_Warning, and "note: " for \a DS_Note.
230 void diagnose(const DiagnosticInfo &DI);
232 /// \brief Registers a yield callback with the given context.
234 /// The yield callback function may be called by LLVM to transfer control back
235 /// to the client that invoked the LLVM compilation. This can be used to yield
236 /// control of the thread, or perform periodic work needed by the client.
237 /// There is no guaranteed frequency at which callbacks must occur; in fact,
238 /// the client is not guaranteed to ever receive this callback. It is at the
239 /// sole discretion of LLVM to do so and only if it can guarantee that
240 /// suspending the thread won't block any forward progress in other LLVM
241 /// contexts in the same process.
243 /// At a suspend point, the state of the current LLVM context is intentionally
244 /// undefined. No assumptions about it can or should be made. Only LLVM
245 /// context API calls that explicitly state that they can be used during a
246 /// yield callback are allowed to be used. Any other API calls into the
247 /// context are not supported until the yield callback function returns
248 /// control to LLVM. Other LLVM contexts are unaffected by this restriction.
249 void setYieldCallback(YieldCallbackTy Callback, void *OpaqueHandle);
251 /// \brief Calls the yield callback (if applicable).
253 /// This transfers control of the current thread back to the client, which may
254 /// suspend the current thread. Only call this method when LLVM doesn't hold
255 /// any global mutex or cannot block the execution in another LLVM context.
258 /// emitError - Emit an error message to the currently installed error handler
259 /// with optional location information. This function returns, so code should
260 /// be prepared to drop the erroneous construct on the floor and "not crash".
261 /// The generated code need not be correct. The error message will be
262 /// implicitly prefixed with "error: " and should not end with a ".".
263 void emitError(unsigned LocCookie, const Twine &ErrorStr);
264 void emitError(const Instruction *I, const Twine &ErrorStr);
265 void emitError(const Twine &ErrorStr);
267 /// \brief Query for a debug option's value.
269 /// This function returns typed data populated from command line parsing.
270 template <typename ValT, typename Base, ValT(Base::*Mem)>
271 ValT getOption() const {
272 return OptionRegistry::instance().template get<ValT, Base, Mem>();
275 /// \brief Access the object which manages optimization bisection for failure
277 OptBisect &getOptBisect();
279 // Module needs access to the add/removeModule methods.
282 /// addModule - Register a module as being instantiated in this context. If
283 /// the context is deleted, the module will be deleted as well.
284 void addModule(Module*);
286 /// removeModule - Unregister a module from this context.
287 void removeModule(Module*);
290 // Create wrappers for C Binding types (see CBindingWrapping.h).
291 DEFINE_SIMPLE_CONVERSION_FUNCTIONS(LLVMContext, LLVMContextRef)
293 /* Specialized opaque context conversions.
295 inline LLVMContext **unwrap(LLVMContextRef* Tys) {
296 return reinterpret_cast<LLVMContext**>(Tys);
299 inline LLVMContextRef *wrap(const LLVMContext **Tys) {
300 return reinterpret_cast<LLVMContextRef*>(const_cast<LLVMContext**>(Tys));
303 } // end namespace llvm
305 #endif // LLVM_IR_LLVMCONTEXT_H