LLVM  6.0.0svn
LLVMContext.h
Go to the documentation of this file.
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/Types.h"
21 #include "llvm/Support/Options.h"
22 #include <cstdint>
23 #include <memory>
24 #include <string>
25 
26 namespace llvm {
27 
28 class DiagnosticInfo;
30 class Function;
31 class Instruction;
32 class LLVMContextImpl;
33 class Module;
34 class OptBisect;
35 template <typename T> class SmallVectorImpl;
36 class SMDiagnostic;
37 class StringRef;
38 class Twine;
39 
40 namespace yaml {
41 
42 class Output;
43 
44 } // end namespace yaml
45 
46 namespace SyncScope {
47 
48 typedef uint8_t ID;
49 
50 /// Known synchronization scope IDs, which always have the same value. All
51 /// synchronization scope IDs that LLVM has special knowledge of are listed
52 /// here. Additionally, this scheme allows LLVM to efficiently check for
53 /// specific synchronization scope ID without comparing strings.
54 enum {
55  /// Synchronized with respect to signal handlers executing in the same thread.
57 
58  /// Synchronized with respect to all concurrently executing threads.
59  System = 1
60 };
61 
62 } // end namespace SyncScope
63 
64 /// This is an important class for using LLVM in a threaded context. It
65 /// (opaquely) owns and manages the core "global" data of LLVM's core
66 /// infrastructure, including the type and constant uniquing tables.
67 /// LLVMContext itself provides no locking guarantees, so you should be careful
68 /// to have one context per thread.
69 class LLVMContext {
70 public:
72  LLVMContext();
73  LLVMContext(LLVMContext &) = delete;
74  LLVMContext &operator=(const LLVMContext &) = delete;
75  ~LLVMContext();
76 
77  // Pinned metadata names, which always have the same value. This is a
78  // compile-time performance optimization, not a correctness optimization.
79  enum {
80  MD_dbg = 0, // "dbg"
81  MD_tbaa = 1, // "tbaa"
82  MD_prof = 2, // "prof"
83  MD_fpmath = 3, // "fpmath"
84  MD_range = 4, // "range"
85  MD_tbaa_struct = 5, // "tbaa.struct"
86  MD_invariant_load = 6, // "invariant.load"
87  MD_alias_scope = 7, // "alias.scope"
88  MD_noalias = 8, // "noalias",
89  MD_nontemporal = 9, // "nontemporal"
90  MD_mem_parallel_loop_access = 10, // "llvm.mem.parallel_loop_access"
91  MD_nonnull = 11, // "nonnull"
92  MD_dereferenceable = 12, // "dereferenceable"
93  MD_dereferenceable_or_null = 13, // "dereferenceable_or_null"
94  MD_make_implicit = 14, // "make.implicit"
95  MD_unpredictable = 15, // "unpredictable"
96  MD_invariant_group = 16, // "invariant.group"
97  MD_align = 17, // "align"
98  MD_loop = 18, // "llvm.loop"
99  MD_type = 19, // "type"
100  MD_section_prefix = 20, // "section_prefix"
101  MD_absolute_symbol = 21, // "absolute_symbol"
102  MD_associated = 22, // "associated"
103  };
104 
105  /// Known operand bundle tag IDs, which always have the same value. All
106  /// operand bundle tags that LLVM has special knowledge of are listed here.
107  /// Additionally, this scheme allows LLVM to efficiently check for specific
108  /// operand bundle tags without comparing strings.
109  enum {
110  OB_deopt = 0, // "deopt"
111  OB_funclet = 1, // "funclet"
112  OB_gc_transition = 2, // "gc-transition"
113  };
114 
115  /// getMDKindID - Return a unique non-zero ID for the specified metadata kind.
116  /// This ID is uniqued across modules in the current LLVMContext.
117  unsigned getMDKindID(StringRef Name) const;
118 
119  /// getMDKindNames - Populate client supplied SmallVector with the name for
120  /// custom metadata IDs registered in this LLVMContext.
121  void getMDKindNames(SmallVectorImpl<StringRef> &Result) const;
122 
123  /// getOperandBundleTags - Populate client supplied SmallVector with the
124  /// bundle tags registered in this LLVMContext. The bundle tags are ordered
125  /// by increasing bundle IDs.
126  /// \see LLVMContext::getOperandBundleTagID
127  void getOperandBundleTags(SmallVectorImpl<StringRef> &Result) const;
128 
129  /// getOperandBundleTagID - Maps a bundle tag to an integer ID. Every bundle
130  /// tag registered with an LLVMContext has an unique ID.
131  uint32_t getOperandBundleTagID(StringRef Tag) const;
132 
133  /// getOrInsertSyncScopeID - Maps synchronization scope name to
134  /// synchronization scope ID. Every synchronization scope registered with
135  /// LLVMContext has unique ID except pre-defined ones.
136  SyncScope::ID getOrInsertSyncScopeID(StringRef SSN);
137 
138  /// getSyncScopeNames - Populates client supplied SmallVector with
139  /// synchronization scope names registered with LLVMContext. Synchronization
140  /// scope names are ordered by increasing synchronization scope IDs.
141  void getSyncScopeNames(SmallVectorImpl<StringRef> &SSNs) const;
142 
143  /// Define the GC for a function
144  void setGC(const Function &Fn, std::string GCName);
145 
146  /// Return the GC for a function
147  const std::string &getGC(const Function &Fn);
148 
149  /// Remove the GC for a function
150  void deleteGC(const Function &Fn);
151 
152  /// Return true if the Context runtime configuration is set to discard all
153  /// value names. When true, only GlobalValue names will be available in the
154  /// IR.
155  bool shouldDiscardValueNames() const;
156 
157  /// Set the Context runtime configuration to discard all value name (but
158  /// GlobalValue). Clients can use this flag to save memory and runtime,
159  /// especially in release mode.
160  void setDiscardValueNames(bool Discard);
161 
162  /// Whether there is a string map for uniquing debug info
163  /// identifiers across the context. Off by default.
164  bool isODRUniquingDebugTypes() const;
165  void enableDebugTypeODRUniquing();
166  void disableDebugTypeODRUniquing();
167 
168  using InlineAsmDiagHandlerTy = void (*)(const SMDiagnostic&, void *Context,
169  unsigned LocCookie);
170 
171  /// Defines the type of a yield callback.
172  /// \see LLVMContext::setYieldCallback.
173  using YieldCallbackTy = void (*)(LLVMContext *Context, void *OpaqueHandle);
174 
175  /// setInlineAsmDiagnosticHandler - This method sets a handler that is invoked
176  /// when problems with inline asm are detected by the backend. The first
177  /// argument is a function pointer and the second is a context pointer that
178  /// gets passed into the DiagHandler.
179  ///
180  /// LLVMContext doesn't take ownership or interpret either of these
181  /// pointers.
182  void setInlineAsmDiagnosticHandler(InlineAsmDiagHandlerTy DiagHandler,
183  void *DiagContext = nullptr);
184 
185  /// getInlineAsmDiagnosticHandler - Return the diagnostic handler set by
186  /// setInlineAsmDiagnosticHandler.
187  InlineAsmDiagHandlerTy getInlineAsmDiagnosticHandler() const;
188 
189  /// getInlineAsmDiagnosticContext - Return the diagnostic context set by
190  /// setInlineAsmDiagnosticHandler.
191  void *getInlineAsmDiagnosticContext() const;
192 
193  /// setDiagnosticHandlerCallBack - This method sets a handler call back
194  /// that is invoked when the backend needs to report anything to the user.
195  /// The first argument is a function pointer and the second is a context pointer
196  /// that gets passed into the DiagHandler. The third argument should be set to
197  /// true if the handler only expects enabled diagnostics.
198  ///
199  /// LLVMContext doesn't take ownership or interpret either of these
200  /// pointers.
201  void setDiagnosticHandlerCallBack(
203  void *DiagContext = nullptr, bool RespectFilters = false);
204 
205  /// setDiagnosticHandler - This method sets unique_ptr to object of DiagnosticHandler
206  /// to provide custom diagnostic handling. The first argument is unique_ptr of object
207  /// of type DiagnosticHandler or a derived of that. The third argument should be
208  /// set to true if the handler only expects enabled diagnostics.
209  ///
210  /// Ownership of this pointer is moved to LLVMContextImpl.
211  void setDiagnosticHandler(std::unique_ptr<DiagnosticHandler> &&DH,
212  bool RespectFilters = false);
213 
214  /// getDiagnosticHandlerCallBack - Return the diagnostic handler call back set by
215  /// setDiagnosticHandlerCallBack.
216  DiagnosticHandler::DiagnosticHandlerTy getDiagnosticHandlerCallBack() const;
217 
218  /// getDiagnosticContext - Return the diagnostic context set by
219  /// setDiagnosticContext.
220  void *getDiagnosticContext() const;
221 
222  /// getDiagHandlerPtr - Returns const raw pointer of DiagnosticHandler set by
223  /// setDiagnosticHandler.
224  const DiagnosticHandler *getDiagHandlerPtr() const;
225 
226  /// getDiagnosticHandler - transfers owenership of DiagnosticHandler unique_ptr
227  /// to caller.
228  std::unique_ptr<DiagnosticHandler> getDiagnosticHandler();
229 
230  /// \brief Return if a code hotness metric should be included in optimization
231  /// diagnostics.
232  bool getDiagnosticsHotnessRequested() const;
233  /// \brief Set if a code hotness metric should be included in optimization
234  /// diagnostics.
235  void setDiagnosticsHotnessRequested(bool Requested);
236 
237  /// \brief Return the minimum hotness value a diagnostic would need in order
238  /// to be included in optimization diagnostics. If there is no minimum, this
239  /// returns None.
240  uint64_t getDiagnosticsHotnessThreshold() const;
241 
242  /// \brief Set the minimum hotness value a diagnostic needs in order to be
243  /// included in optimization diagnostics.
244  void setDiagnosticsHotnessThreshold(uint64_t Threshold);
245 
246  /// \brief Return the YAML file used by the backend to save optimization
247  /// diagnostics. If null, diagnostics are not saved in a file but only
248  /// emitted via the diagnostic handler.
249  yaml::Output *getDiagnosticsOutputFile();
250  /// Set the diagnostics output file used for optimization diagnostics.
251  ///
252  /// By default or if invoked with null, diagnostics are not saved in a file
253  /// but only emitted via the diagnostic handler. Even if an output file is
254  /// set, the handler is invoked for each diagnostic message.
255  void setDiagnosticsOutputFile(std::unique_ptr<yaml::Output> F);
256 
257  /// \brief Get the prefix that should be printed in front of a diagnostic of
258  /// the given \p Severity
259  static const char *getDiagnosticMessagePrefix(DiagnosticSeverity Severity);
260 
261  /// \brief Report a message to the currently installed diagnostic handler.
262  ///
263  /// This function returns, in particular in the case of error reporting
264  /// (DI.Severity == \a DS_Error), so the caller should leave the compilation
265  /// process in a self-consistent state, even though the generated code
266  /// need not be correct.
267  ///
268  /// The diagnostic message will be implicitly prefixed with a severity keyword
269  /// according to \p DI.getSeverity(), i.e., "error: " for \a DS_Error,
270  /// "warning: " for \a DS_Warning, and "note: " for \a DS_Note.
271  void diagnose(const DiagnosticInfo &DI);
272 
273  /// \brief Registers a yield callback with the given context.
274  ///
275  /// The yield callback function may be called by LLVM to transfer control back
276  /// to the client that invoked the LLVM compilation. This can be used to yield
277  /// control of the thread, or perform periodic work needed by the client.
278  /// There is no guaranteed frequency at which callbacks must occur; in fact,
279  /// the client is not guaranteed to ever receive this callback. It is at the
280  /// sole discretion of LLVM to do so and only if it can guarantee that
281  /// suspending the thread won't block any forward progress in other LLVM
282  /// contexts in the same process.
283  ///
284  /// At a suspend point, the state of the current LLVM context is intentionally
285  /// undefined. No assumptions about it can or should be made. Only LLVM
286  /// context API calls that explicitly state that they can be used during a
287  /// yield callback are allowed to be used. Any other API calls into the
288  /// context are not supported until the yield callback function returns
289  /// control to LLVM. Other LLVM contexts are unaffected by this restriction.
290  void setYieldCallback(YieldCallbackTy Callback, void *OpaqueHandle);
291 
292  /// \brief Calls the yield callback (if applicable).
293  ///
294  /// This transfers control of the current thread back to the client, which may
295  /// suspend the current thread. Only call this method when LLVM doesn't hold
296  /// any global mutex or cannot block the execution in another LLVM context.
297  void yield();
298 
299  /// emitError - Emit an error message to the currently installed error handler
300  /// with optional location information. This function returns, so code should
301  /// be prepared to drop the erroneous construct on the floor and "not crash".
302  /// The generated code need not be correct. The error message will be
303  /// implicitly prefixed with "error: " and should not end with a ".".
304  void emitError(unsigned LocCookie, const Twine &ErrorStr);
305  void emitError(const Instruction *I, const Twine &ErrorStr);
306  void emitError(const Twine &ErrorStr);
307 
308  /// \brief Query for a debug option's value.
309  ///
310  /// This function returns typed data populated from command line parsing.
311  template <typename ValT, typename Base, ValT(Base::*Mem)>
312  ValT getOption() const {
313  return OptionRegistry::instance().template get<ValT, Base, Mem>();
314  }
315 
316  /// \brief Access the object which manages optimization bisection for failure
317  /// analysis.
318  OptBisect &getOptBisect();
319 private:
320  // Module needs access to the add/removeModule methods.
321  friend class Module;
322 
323  /// addModule - Register a module as being instantiated in this context. If
324  /// the context is deleted, the module will be deleted as well.
325  void addModule(Module*);
326 
327  /// removeModule - Unregister a module from this context.
328  void removeModule(Module*);
329 };
330 
331 // Create wrappers for C Binding types (see CBindingWrapping.h).
333 
334 /* Specialized opaque context conversions.
335  */
337  return reinterpret_cast<LLVMContext**>(Tys);
338 }
339 
340 inline LLVMContextRef *wrap(const LLVMContext **Tys) {
341  return reinterpret_cast<LLVMContextRef*>(const_cast<LLVMContext**>(Tys));
342 }
343 
344 } // end namespace llvm
345 
346 #endif // LLVM_IR_LLVMCONTEXT_H
This is the base class for diagnostic handling in LLVM.
LLVMContext & Context
Compute iterated dominance frontiers using a linear time algorithm.
Definition: AllocatorList.h:24
A Module instance is used to store all the information related to an LLVM module. ...
Definition: Module.h:63
void(*)(LLVMContext *Context, void *OpaqueHandle) YieldCallbackTy
Defines the type of a yield callback.
Definition: LLVMContext.h:173
Synchronized with respect to signal handlers executing in the same thread.
Definition: LLVMContext.h:56
F(f)
DiagnosticSeverity
Defines the different supported severity of a diagnostic.
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:81
Attribute unwrap(LLVMAttributeRef Attr)
Definition: Attributes.h:195
static OptionRegistry & instance()
Returns a reference to the singleton instance.
Definition: Options.cpp:33
struct LLVMOpaqueContext * LLVMContextRef
The top-level container for all LLVM global data.
Definition: Types.h:54
void(*)(const DiagnosticInfo &DI, void *Context) DiagnosticHandlerTy
This class implements a mechanism to disable passes and individual optimizations at compile time base...
Definition: OptBisect.h:28
Synchronized with respect to all concurrently executing threads.
Definition: LLVMContext.h:59
This is the base abstract class for diagnostic reporting in the backend.
This is an important class for using LLVM in a threaded context.
Definition: LLVMContext.h:69
amdgpu inline
The Output class is used to generate a yaml document from in-memory structs and vectors.
Definition: YAMLTraits.h:1260
DEFINE_SIMPLE_CONVERSION_FUNCTIONS(std::shared_ptr< Module >, LLVMSharedModuleRef) namespace detail
LLVMContextImpl *const pImpl
Definition: LLVMContext.h:71
ValT getOption() const
Query for a debug option&#39;s value.
Definition: LLVMContext.h:312
This file declares helper objects for defining debug options that can be configured via the command l...
void(*)(const SMDiagnostic &, void *Context, unsigned LocCookie) InlineAsmDiagHandlerTy
Definition: LLVMContext.h:169
LLVMAttributeRef wrap(Attribute Attr)
Definition: Attributes.h:190
#define I(x, y, z)
Definition: MD5.cpp:58
static int const Threshold
TODO: Write a new FunctionPass AliasAnalysis so that it can keep a cache.
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:49
Instances of this class encapsulate one diagnostic report, allowing printing to a raw_ostream as a ca...
Definition: SourceMgr.h:232