12bc29dc0bcb3c1441477a062e4a5cffff175c8caOwen Anderson//===-- llvm/LLVMContext.h - Class for managing "global" state --*- C++ -*-===//
22bc29dc0bcb3c1441477a062e4a5cffff175c8caOwen Anderson//
32bc29dc0bcb3c1441477a062e4a5cffff175c8caOwen Anderson//                     The LLVM Compiler Infrastructure
42bc29dc0bcb3c1441477a062e4a5cffff175c8caOwen Anderson//
52bc29dc0bcb3c1441477a062e4a5cffff175c8caOwen Anderson// This file is distributed under the University of Illinois Open Source
62bc29dc0bcb3c1441477a062e4a5cffff175c8caOwen Anderson// License. See LICENSE.TXT for details.
72bc29dc0bcb3c1441477a062e4a5cffff175c8caOwen Anderson//
82bc29dc0bcb3c1441477a062e4a5cffff175c8caOwen Anderson//===----------------------------------------------------------------------===//
95217007006e91fa4bbfe88fde5149f5db293b247Owen Anderson//
105217007006e91fa4bbfe88fde5149f5db293b247Owen Anderson// This file declares LLVMContext, a container of "global" state in LLVM, such
115217007006e91fa4bbfe88fde5149f5db293b247Owen Anderson// as the global type and constant uniquing tables.
125217007006e91fa4bbfe88fde5149f5db293b247Owen Anderson//
135217007006e91fa4bbfe88fde5149f5db293b247Owen Anderson//===----------------------------------------------------------------------===//
142bc29dc0bcb3c1441477a062e4a5cffff175c8caOwen Anderson
15674be02d525d4e24bc6943ed9274958c580bcfbcJakub Staszak#ifndef LLVM_IR_LLVMCONTEXT_H
16674be02d525d4e24bc6943ed9274958c580bcfbcJakub Staszak#define LLVM_IR_LLVMCONTEXT_H
172bc29dc0bcb3c1441477a062e4a5cffff175c8caOwen Anderson
1836b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines#include "llvm-c/Core.h"
1940be1e85665d10f5444186f0e7106e368dd735b8Filip Pizlo#include "llvm/Support/CBindingWrapping.h"
209f9ce61972871efcf794bdc6125835c2c32cd863Craig Topper#include "llvm/Support/Compiler.h"
219f9ce61972871efcf794bdc6125835c2c32cd863Craig Topper
222bc29dc0bcb3c1441477a062e4a5cffff175c8caOwen Andersonnamespace llvm {
232bc29dc0bcb3c1441477a062e4a5cffff175c8caOwen Anderson
2412ddd409535b52a7fa5157ded9a4cedd161fedb6Benjamin Kramerclass LLVMContextImpl;
25081134741b40b342fb2f85722c9cea5d412489a8Chris Lattnerclass StringRef;
263a4c60ca3b8ab835a71a92c02d288b9582e4a643Chris Lattnerclass Twine;
2738686bdffdebc09aa6fe6b8b54b34ac32c753f59Chris Lattnerclass Instruction;
2830268be89df6444f5ffb585439b3fbfec9055197Owen Andersonclass Module;
294afa12890f679034e9741a687a6ce33f2846f129Chris Lattnerclass SMDiagnostic;
3036b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hinesclass DiagnosticInfo;
31081134741b40b342fb2f85722c9cea5d412489a8Chris Lattnertemplate <typename T> class SmallVectorImpl;
32dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hinesclass Function;
33dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hinesclass DebugLoc;
3482a47e9118c95979ea5fd48f53472416295e51efDan Gohman
355217007006e91fa4bbfe88fde5149f5db293b247Owen Anderson/// This is an important class for using LLVM in a threaded context.  It
364e505a47c76967ded5ec91acc25e7d0f9ac997c1Jim Grosbach/// (opaquely) owns and manages the core "global" data of LLVM's core
375217007006e91fa4bbfe88fde5149f5db293b247Owen Anderson/// infrastructure, including the type and constant uniquing tables.
385217007006e91fa4bbfe88fde5149f5db293b247Owen Anderson/// LLVMContext itself provides no locking guarantees, so you should be careful
395217007006e91fa4bbfe88fde5149f5db293b247Owen Anderson/// to have one context per thread.
4012ddd409535b52a7fa5157ded9a4cedd161fedb6Benjamin Kramerclass LLVMContext {
411e8d5d2e2216e024293872a102ecf3be2a34e3d2Owen Andersonpublic:
421e8d5d2e2216e024293872a102ecf3be2a34e3d2Owen Anderson  LLVMContextImpl *const pImpl;
431e8d5d2e2216e024293872a102ecf3be2a34e3d2Owen Anderson  LLVMContext();
441e8d5d2e2216e024293872a102ecf3be2a34e3d2Owen Anderson  ~LLVMContext();
454e505a47c76967ded5ec91acc25e7d0f9ac997c1Jim Grosbach
46ec39f095f5abaf1ec90d7c6c46454032cda36e1cChris Lattner  // Pinned metadata names, which always have the same value.  This is a
47ec39f095f5abaf1ec90d7c6c46454032cda36e1cChris Lattner  // compile-time performance optimization, not a correctness optimization.
48ec39f095f5abaf1ec90d7c6c46454032cda36e1cChris Lattner  enum {
49b2143b6247901ae4eca2192ee134564c4f5f7853Dan Gohman    MD_dbg = 0,  // "dbg"
509da9934e27dfb48de77b80a3e20ed2d869b52024Jakub Staszak    MD_tbaa = 1, // "tbaa"
51999f90bedf94e7d55508f0797802b75064f1de09Peter Collingbourne    MD_prof = 2,  // "prof"
525e5c5f8259b90ba77c0b30e67cb360165000d1f6Duncan Sands    MD_fpmath = 3,  // "fpmath"
53b54834b7e67ac04874891f3dcf69226c69582b4bDan Gohman    MD_range = 4, // "range"
54985dac65791b9f6f631bdd51c18fe66592a67469Shuxin Yang    MD_tbaa_struct = 5, // "tbaa.struct"
55985dac65791b9f6f631bdd51c18fe66592a67469Shuxin Yang    MD_invariant_load = 6 // "invariant.load"
56ec39f095f5abaf1ec90d7c6c46454032cda36e1cChris Lattner  };
574e505a47c76967ded5ec91acc25e7d0f9ac997c1Jim Grosbach
58081134741b40b342fb2f85722c9cea5d412489a8Chris Lattner  /// getMDKindID - Return a unique non-zero ID for the specified metadata kind.
59081134741b40b342fb2f85722c9cea5d412489a8Chris Lattner  /// This ID is uniqued across modules in the current LLVMContext.
60081134741b40b342fb2f85722c9cea5d412489a8Chris Lattner  unsigned getMDKindID(StringRef Name) const;
614e505a47c76967ded5ec91acc25e7d0f9ac997c1Jim Grosbach
62081134741b40b342fb2f85722c9cea5d412489a8Chris Lattner  /// getMDKindNames - Populate client supplied SmallVector with the name for
635d809119834a1aa8170e8341ee883e69e5a6bbd1Dan Gohman  /// custom metadata IDs registered in this LLVMContext.
64081134741b40b342fb2f85722c9cea5d412489a8Chris Lattner  void getMDKindNames(SmallVectorImpl<StringRef> &Result) const;
654e505a47c76967ded5ec91acc25e7d0f9ac997c1Jim Grosbach
664e505a47c76967ded5ec91acc25e7d0f9ac997c1Jim Grosbach
67f64c889cc94417322b0ff8ad1c61939183bf3c38Bob Wilson  typedef void (*InlineAsmDiagHandlerTy)(const SMDiagnostic&, void *Context,
68f64c889cc94417322b0ff8ad1c61939183bf3c38Bob Wilson                                         unsigned LocCookie);
694e505a47c76967ded5ec91acc25e7d0f9ac997c1Jim Grosbach
7036b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  /// Defines the type of a diagnostic handler.
7136b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  /// \see LLVMContext::setDiagnosticHandler.
7236b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  /// \see LLVMContext::diagnose.
7336b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  typedef void (*DiagnosticHandlerTy)(const DiagnosticInfo &DI, void *Context);
7436b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines
75dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines  /// Defines the type of a yield callback.
76dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines  /// \see LLVMContext::setYieldCallback.
77dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines  typedef void (*YieldCallbackTy)(LLVMContext *Context, void *OpaqueHandle);
78dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines
79f64c889cc94417322b0ff8ad1c61939183bf3c38Bob Wilson  /// setInlineAsmDiagnosticHandler - This method sets a handler that is invoked
80f64c889cc94417322b0ff8ad1c61939183bf3c38Bob Wilson  /// when problems with inline asm are detected by the backend.  The first
81f64c889cc94417322b0ff8ad1c61939183bf3c38Bob Wilson  /// argument is a function pointer and the second is a context pointer that
82f64c889cc94417322b0ff8ad1c61939183bf3c38Bob Wilson  /// gets passed into the DiagHandler.
8342a4ee0a35b4672311c1b988bd883167de9f88cdChris Lattner  ///
844afa12890f679034e9741a687a6ce33f2846f129Chris Lattner  /// LLVMContext doesn't take ownership or interpret either of these
8542a4ee0a35b4672311c1b988bd883167de9f88cdChris Lattner  /// pointers.
86f64c889cc94417322b0ff8ad1c61939183bf3c38Bob Wilson  void setInlineAsmDiagnosticHandler(InlineAsmDiagHandlerTy DiagHandler,
87dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines                                     void *DiagContext = nullptr);
8842a4ee0a35b4672311c1b988bd883167de9f88cdChris Lattner
89f64c889cc94417322b0ff8ad1c61939183bf3c38Bob Wilson  /// getInlineAsmDiagnosticHandler - Return the diagnostic handler set by
90f64c889cc94417322b0ff8ad1c61939183bf3c38Bob Wilson  /// setInlineAsmDiagnosticHandler.
91f64c889cc94417322b0ff8ad1c61939183bf3c38Bob Wilson  InlineAsmDiagHandlerTy getInlineAsmDiagnosticHandler() const;
9242a4ee0a35b4672311c1b988bd883167de9f88cdChris Lattner
93f64c889cc94417322b0ff8ad1c61939183bf3c38Bob Wilson  /// getInlineAsmDiagnosticContext - Return the diagnostic context set by
94f64c889cc94417322b0ff8ad1c61939183bf3c38Bob Wilson  /// setInlineAsmDiagnosticHandler.
95f64c889cc94417322b0ff8ad1c61939183bf3c38Bob Wilson  void *getInlineAsmDiagnosticContext() const;
964e505a47c76967ded5ec91acc25e7d0f9ac997c1Jim Grosbach
9736b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  /// setDiagnosticHandler - This method sets a handler that is invoked
9836b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  /// when the backend needs to report anything to the user.  The first
9936b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  /// argument is a function pointer and the second is a context pointer that
10036b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  /// gets passed into the DiagHandler.
10136b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  ///
10236b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  /// LLVMContext doesn't take ownership or interpret either of these
10336b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  /// pointers.
10436b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  void setDiagnosticHandler(DiagnosticHandlerTy DiagHandler,
105dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines                            void *DiagContext = nullptr);
10636b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines
10736b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  /// getDiagnosticHandler - Return the diagnostic handler set by
10836b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  /// setDiagnosticHandler.
10936b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  DiagnosticHandlerTy getDiagnosticHandler() const;
11036b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines
11136b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  /// getDiagnosticContext - Return the diagnostic context set by
11236b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  /// setDiagnosticContext.
11336b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  void *getDiagnosticContext() const;
11436b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines
11536b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  /// diagnose - Report a message to the currently installed diagnostic handler.
11636b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  /// This function returns, in particular in the case of error reporting
11736b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  /// (DI.Severity == RS_Error), so the caller should leave the compilation
11836b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  /// process in a self-consistent state, even though the generated code
11936b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  /// need not be correct.
12036b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  /// The diagnostic message will be implicitly prefixed with a severity
12136b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  /// keyword according to \p DI.getSeverity(), i.e., "error: "
12236b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  /// for RS_Error, "warning: " for RS_Warning, and "note: " for RS_Note.
12336b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  void diagnose(const DiagnosticInfo &DI);
1244e505a47c76967ded5ec91acc25e7d0f9ac997c1Jim Grosbach
125dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines  /// \brief Registers a yield callback with the given context.
126dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines  ///
127dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines  /// The yield callback function may be called by LLVM to transfer control back
128dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines  /// to the client that invoked the LLVM compilation. This can be used to yield
129dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines  /// control of the thread, or perform periodic work needed by the client.
130dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines  /// There is no guaranteed frequency at which callbacks must occur; in fact,
131dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines  /// the client is not guaranteed to ever receive this callback. It is at the
132dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines  /// sole discretion of LLVM to do so and only if it can guarantee that
133dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines  /// suspending the thread won't block any forward progress in other LLVM
134dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines  /// contexts in the same process.
135dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines  ///
136dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines  /// At a suspend point, the state of the current LLVM context is intentionally
137dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines  /// undefined. No assumptions about it can or should be made. Only LLVM
138dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines  /// context API calls that explicitly state that they can be used during a
139dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines  /// yield callback are allowed to be used. Any other API calls into the
140dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines  /// context are not supported until the yield callback function returns
141dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines  /// control to LLVM. Other LLVM contexts are unaffected by this restriction.
142dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines  void setYieldCallback(YieldCallbackTy Callback, void *OpaqueHandle);
143dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines
144dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines  /// \brief Calls the yield callback (if applicable).
145dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines  ///
146dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines  /// This transfers control of the current thread back to the client, which may
147dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines  /// suspend the current thread. Only call this method when LLVM doesn't hold
148dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines  /// any global mutex or cannot block the execution in another LLVM context.
149dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines  void yield();
150dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines
15138686bdffdebc09aa6fe6b8b54b34ac32c753f59Chris Lattner  /// emitError - Emit an error message to the currently installed error handler
15238686bdffdebc09aa6fe6b8b54b34ac32c753f59Chris Lattner  /// with optional location information.  This function returns, so code should
15338686bdffdebc09aa6fe6b8b54b34ac32c753f59Chris Lattner  /// be prepared to drop the erroneous construct on the floor and "not crash".
15438686bdffdebc09aa6fe6b8b54b34ac32c753f59Chris Lattner  /// The generated code need not be correct.  The error message will be
15538686bdffdebc09aa6fe6b8b54b34ac32c753f59Chris Lattner  /// implicitly prefixed with "error: " and should not end with a ".".
1563a4c60ca3b8ab835a71a92c02d288b9582e4a643Chris Lattner  void emitError(unsigned LocCookie, const Twine &ErrorStr);
1573a4c60ca3b8ab835a71a92c02d288b9582e4a643Chris Lattner  void emitError(const Instruction *I, const Twine &ErrorStr);
1583a4c60ca3b8ab835a71a92c02d288b9582e4a643Chris Lattner  void emitError(const Twine &ErrorStr);
159eb92330c1d431a7ecbc31609b926a49c89be1d04Owen Anderson
160eb92330c1d431a7ecbc31609b926a49c89be1d04Owen Andersonprivate:
1619f9ce61972871efcf794bdc6125835c2c32cd863Craig Topper  LLVMContext(LLVMContext&) LLVM_DELETED_FUNCTION;
1629f9ce61972871efcf794bdc6125835c2c32cd863Craig Topper  void operator=(LLVMContext&) LLVM_DELETED_FUNCTION;
163eb92330c1d431a7ecbc31609b926a49c89be1d04Owen Anderson
164eb92330c1d431a7ecbc31609b926a49c89be1d04Owen Anderson  /// addModule - Register a module as being instantiated in this context.  If
165eb92330c1d431a7ecbc31609b926a49c89be1d04Owen Anderson  /// the context is deleted, the module will be deleted as well.
166eb92330c1d431a7ecbc31609b926a49c89be1d04Owen Anderson  void addModule(Module*);
1674e505a47c76967ded5ec91acc25e7d0f9ac997c1Jim Grosbach
168eb92330c1d431a7ecbc31609b926a49c89be1d04Owen Anderson  /// removeModule - Unregister a module from this context.
169eb92330c1d431a7ecbc31609b926a49c89be1d04Owen Anderson  void removeModule(Module*);
1704e505a47c76967ded5ec91acc25e7d0f9ac997c1Jim Grosbach
171eb92330c1d431a7ecbc31609b926a49c89be1d04Owen Anderson  // Module needs access to the add/removeModule methods.
172eb92330c1d431a7ecbc31609b926a49c89be1d04Owen Anderson  friend class Module;
1732bc29dc0bcb3c1441477a062e4a5cffff175c8caOwen Anderson};
1742bc29dc0bcb3c1441477a062e4a5cffff175c8caOwen Anderson
175081134741b40b342fb2f85722c9cea5d412489a8Chris Lattner/// getGlobalContext - Returns a global context.  This is for LLVM clients that
176081134741b40b342fb2f85722c9cea5d412489a8Chris Lattner/// only care about operating on a single thread.
177081134741b40b342fb2f85722c9cea5d412489a8Chris Lattnerextern LLVMContext &getGlobalContext();
178db47ed0925ce97f3134189c96ebc35b2cdae1ce3Owen Anderson
17940be1e85665d10f5444186f0e7106e368dd735b8Filip Pizlo// Create wrappers for C Binding types (see CBindingWrapping.h).
18040be1e85665d10f5444186f0e7106e368dd735b8Filip PizloDEFINE_SIMPLE_CONVERSION_FUNCTIONS(LLVMContext, LLVMContextRef)
18140be1e85665d10f5444186f0e7106e368dd735b8Filip Pizlo
18240be1e85665d10f5444186f0e7106e368dd735b8Filip Pizlo/* Specialized opaque context conversions.
18340be1e85665d10f5444186f0e7106e368dd735b8Filip Pizlo */
18440be1e85665d10f5444186f0e7106e368dd735b8Filip Pizloinline LLVMContext **unwrap(LLVMContextRef* Tys) {
18540be1e85665d10f5444186f0e7106e368dd735b8Filip Pizlo  return reinterpret_cast<LLVMContext**>(Tys);
18640be1e85665d10f5444186f0e7106e368dd735b8Filip Pizlo}
18740be1e85665d10f5444186f0e7106e368dd735b8Filip Pizlo
18840be1e85665d10f5444186f0e7106e368dd735b8Filip Pizloinline LLVMContextRef *wrap(const LLVMContext **Tys) {
18940be1e85665d10f5444186f0e7106e368dd735b8Filip Pizlo  return reinterpret_cast<LLVMContextRef*>(const_cast<LLVMContext**>(Tys));
19040be1e85665d10f5444186f0e7106e368dd735b8Filip Pizlo}
19140be1e85665d10f5444186f0e7106e368dd735b8Filip Pizlo
1922bc29dc0bcb3c1441477a062e4a5cffff175c8caOwen Anderson}
1932bc29dc0bcb3c1441477a062e4a5cffff175c8caOwen Anderson
1942bc29dc0bcb3c1441477a062e4a5cffff175c8caOwen Anderson#endif
195