LLVM  3.7.0
LLVMContext.cpp
Go to the documentation of this file.
1 //===-- LLVMContext.cpp - Implement LLVMContext ---------------------------===//
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 implements LLVMContext, as a wrapper around the opaque
11 // class LLVMContextImpl.
12 //
13 //===----------------------------------------------------------------------===//
14 
15 #include "llvm/IR/LLVMContext.h"
16 #include "LLVMContextImpl.h"
17 #include "llvm/IR/Constants.h"
18 #include "llvm/IR/DebugLoc.h"
19 #include "llvm/IR/DiagnosticInfo.h"
21 #include "llvm/IR/Instruction.h"
22 #include "llvm/IR/Metadata.h"
24 #include "llvm/Support/SourceMgr.h"
25 #include <cctype>
26 using namespace llvm;
27 
29 
31  return *GlobalContext;
32 }
33 
35  // Create the fixed metadata kinds. This is done in the same order as the
36  // MD_* enum values so that they correspond.
37 
38  // Create the 'dbg' metadata kind.
39  unsigned DbgID = getMDKindID("dbg");
40  assert(DbgID == MD_dbg && "dbg kind id drifted"); (void)DbgID;
41 
42  // Create the 'tbaa' metadata kind.
43  unsigned TBAAID = getMDKindID("tbaa");
44  assert(TBAAID == MD_tbaa && "tbaa kind id drifted"); (void)TBAAID;
45 
46  // Create the 'prof' metadata kind.
47  unsigned ProfID = getMDKindID("prof");
48  assert(ProfID == MD_prof && "prof kind id drifted"); (void)ProfID;
49 
50  // Create the 'fpmath' metadata kind.
51  unsigned FPAccuracyID = getMDKindID("fpmath");
52  assert(FPAccuracyID == MD_fpmath && "fpmath kind id drifted");
53  (void)FPAccuracyID;
54 
55  // Create the 'range' metadata kind.
56  unsigned RangeID = getMDKindID("range");
57  assert(RangeID == MD_range && "range kind id drifted");
58  (void)RangeID;
59 
60  // Create the 'tbaa.struct' metadata kind.
61  unsigned TBAAStructID = getMDKindID("tbaa.struct");
62  assert(TBAAStructID == MD_tbaa_struct && "tbaa.struct kind id drifted");
63  (void)TBAAStructID;
64 
65  // Create the 'invariant.load' metadata kind.
66  unsigned InvariantLdId = getMDKindID("invariant.load");
67  assert(InvariantLdId == MD_invariant_load && "invariant.load kind id drifted");
68  (void)InvariantLdId;
69 
70  // Create the 'alias.scope' metadata kind.
71  unsigned AliasScopeID = getMDKindID("alias.scope");
72  assert(AliasScopeID == MD_alias_scope && "alias.scope kind id drifted");
73  (void)AliasScopeID;
74 
75  // Create the 'noalias' metadata kind.
76  unsigned NoAliasID = getMDKindID("noalias");
77  assert(NoAliasID == MD_noalias && "noalias kind id drifted");
78  (void)NoAliasID;
79 
80  // Create the 'nontemporal' metadata kind.
81  unsigned NonTemporalID = getMDKindID("nontemporal");
82  assert(NonTemporalID == MD_nontemporal && "nontemporal kind id drifted");
83  (void)NonTemporalID;
84 
85  // Create the 'llvm.mem.parallel_loop_access' metadata kind.
86  unsigned MemParallelLoopAccessID = getMDKindID("llvm.mem.parallel_loop_access");
87  assert(MemParallelLoopAccessID == MD_mem_parallel_loop_access &&
88  "mem_parallel_loop_access kind id drifted");
89  (void)MemParallelLoopAccessID;
90 
91  // Create the 'nonnull' metadata kind.
92  unsigned NonNullID = getMDKindID("nonnull");
93  assert(NonNullID == MD_nonnull && "nonnull kind id drifted");
94  (void)NonNullID;
95 
96  // Create the 'dereferenceable' metadata kind.
97  unsigned DereferenceableID = getMDKindID("dereferenceable");
98  assert(DereferenceableID == MD_dereferenceable &&
99  "dereferenceable kind id drifted");
100  (void)DereferenceableID;
101 
102  // Create the 'dereferenceable_or_null' metadata kind.
103  unsigned DereferenceableOrNullID = getMDKindID("dereferenceable_or_null");
104  assert(DereferenceableOrNullID == MD_dereferenceable_or_null &&
105  "dereferenceable_or_null kind id drifted");
106  (void)DereferenceableOrNullID;
107 }
109 
110 void LLVMContext::addModule(Module *M) {
112 }
113 
114 void LLVMContext::removeModule(Module *M) {
116 }
117 
118 //===----------------------------------------------------------------------===//
119 // Recoverable Backend Errors
120 //===----------------------------------------------------------------------===//
121 
122 void LLVMContext::
123 setInlineAsmDiagnosticHandler(InlineAsmDiagHandlerTy DiagHandler,
124  void *DiagContext) {
125  pImpl->InlineAsmDiagHandler = DiagHandler;
126  pImpl->InlineAsmDiagContext = DiagContext;
127 }
128 
129 /// getInlineAsmDiagnosticHandler - Return the diagnostic handler set by
130 /// setInlineAsmDiagnosticHandler.
133  return pImpl->InlineAsmDiagHandler;
134 }
135 
136 /// getInlineAsmDiagnosticContext - Return the diagnostic context set by
137 /// setInlineAsmDiagnosticHandler.
139  return pImpl->InlineAsmDiagContext;
140 }
141 
142 void LLVMContext::setDiagnosticHandler(DiagnosticHandlerTy DiagnosticHandler,
143  void *DiagnosticContext,
144  bool RespectFilters) {
145  pImpl->DiagnosticHandler = DiagnosticHandler;
146  pImpl->DiagnosticContext = DiagnosticContext;
147  pImpl->RespectDiagnosticFilters = RespectFilters;
148 }
149 
151  return pImpl->DiagnosticHandler;
152 }
153 
155  return pImpl->DiagnosticContext;
156 }
157 
158 void LLVMContext::setYieldCallback(YieldCallbackTy Callback, void *OpaqueHandle)
159 {
160  pImpl->YieldCallback = Callback;
161  pImpl->YieldOpaqueHandle = OpaqueHandle;
162 }
163 
165  if (pImpl->YieldCallback)
167 }
168 
169 void LLVMContext::emitError(const Twine &ErrorStr) {
171 }
172 
173 void LLVMContext::emitError(const Instruction *I, const Twine &ErrorStr) {
174  assert (I && "Invalid instruction");
175  diagnose(DiagnosticInfoInlineAsm(*I, ErrorStr));
176 }
177 
178 static bool isDiagnosticEnabled(const DiagnosticInfo &DI) {
179  // Optimization remarks are selective. They need to check whether the regexp
180  // pattern, passed via one of the -pass-remarks* flags, matches the name of
181  // the pass that is emitting the diagnostic. If there is no match, ignore the
182  // diagnostic and return.
183  switch (DI.getKind()) {
185  if (!cast<DiagnosticInfoOptimizationRemark>(DI).isEnabled())
186  return false;
187  break;
189  if (!cast<DiagnosticInfoOptimizationRemarkMissed>(DI).isEnabled())
190  return false;
191  break;
193  if (!cast<DiagnosticInfoOptimizationRemarkAnalysis>(DI).isEnabled())
194  return false;
195  break;
196  default:
197  break;
198  }
199  return true;
200 }
201 
202 static const char *getDiagnosticMessagePrefix(DiagnosticSeverity Severity) {
203  switch (Severity) {
204  case DS_Error:
205  return "error";
206  case DS_Warning:
207  return "warning";
208  case DS_Remark:
209  return "remark";
210  case DS_Note:
211  return "note";
212  }
213  llvm_unreachable("Unknown DiagnosticSeverity");
214 }
215 
217  // If there is a report handler, use it.
218  if (pImpl->DiagnosticHandler) {
221  return;
222  }
223 
224  if (!isDiagnosticEnabled(DI))
225  return;
226 
227  // Otherwise, print the message with a prefix based on the severity.
229  errs() << getDiagnosticMessagePrefix(DI.getSeverity()) << ": ";
230  DI.print(DP);
231  errs() << "\n";
232  if (DI.getSeverity() == DS_Error)
233  exit(1);
234 }
235 
236 void LLVMContext::emitError(unsigned LocCookie, const Twine &ErrorStr) {
237  diagnose(DiagnosticInfoInlineAsm(LocCookie, ErrorStr));
238 }
239 
240 //===----------------------------------------------------------------------===//
241 // Metadata Kind Uniquing
242 //===----------------------------------------------------------------------===//
243 
244 /// Return a unique non-zero ID for the specified metadata kind.
246  // If this is new, assign it its ID.
248  std::make_pair(
249  Name, pImpl->CustomMDKindNames.size()))
250  .first->second;
251 }
252 
253 /// getHandlerNames - Populate client supplied smallvector using custome
254 /// metadata name and ID.
258  E = pImpl->CustomMDKindNames.end(); I != E; ++I)
259  Names[I->second] = I->first();
260 }
raw_ostream & errs()
This returns a reference to a raw_ostream for standard error.
InlineAsmDiagHandlerTy getInlineAsmDiagnosticHandler() const
getInlineAsmDiagnosticHandler - Return the diagnostic handler set by setInlineAsmDiagnosticHandler.
A Module instance is used to store all the information related to an LLVM module. ...
Definition: Module.h:114
void(* DiagnosticHandlerTy)(const DiagnosticInfo &DI, void *Context)
Defines the type of a diagnostic handler.
Definition: LLVMContext.h:81
DiagnosticSeverity
Defines the different supported severity of a diagnostic.
DiagnosticHandlerTy getDiagnosticHandler() const
getDiagnosticHandler - Return the diagnostic handler set by setDiagnosticHandler. ...
This file contains the declarations for metadata subclasses.
void setInlineAsmDiagnosticHandler(InlineAsmDiagHandlerTy DiagHandler, void *DiagContext=nullptr)
setInlineAsmDiagnosticHandler - This method sets a handler that is invoked when problems with inline ...
void setYieldCallback(YieldCallbackTy Callback, void *OpaqueHandle)
Registers a yield callback with the given context.
void yield()
Calls the yield callback (if applicable).
LLVMContext::InlineAsmDiagHandlerTy InlineAsmDiagHandler
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:79
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
Definition: ErrorHandling.h:98
void emitError(unsigned LocCookie, const Twine &ErrorStr)
emitError - Emit an error message to the currently installed error handler with optional location inf...
static const char * getDiagnosticMessagePrefix(DiagnosticSeverity Severity)
DiagnosticSeverity getSeverity() const
static ManagedStatic< LLVMContext > GlobalContext
Definition: LLVMContext.cpp:28
LLVMContext::YieldCallbackTy YieldCallback
void getMDKindNames(SmallVectorImpl< StringRef > &Result) const
getMDKindNames - Populate client supplied SmallVector with the name for custom metadata IDs registere...
StringMap< unsigned > CustomMDKindNames
CustomMDKindNames - Map to hold the metadata string to ID mapping.
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:41
This file contains the declarations for the subclasses of Constant, which represent the different fla...
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
Definition: SmallPtrSet.h:264
unsigned size() const
Definition: StringMap.h:99
unsigned getMDKindID(StringRef Name) const
getMDKindID - Return a unique non-zero ID for the specified metadata kind.
void * getDiagnosticContext() const
getDiagnosticContext - Return the diagnostic context set by setDiagnosticContext. ...
Diagnostic information for inline asm reporting.
virtual void print(DiagnosticPrinter &DP) const =0
Print using the given DP a user-friendly message.
LLVMContextImpl *const pImpl
Definition: LLVMContext.h:43
bool erase(PtrType Ptr)
erase - If the set contains the specified pointer, remove it and return true, otherwise return false...
Definition: SmallPtrSet.h:271
bool insert(MapEntryTy *KeyValue)
insert - Insert the specified key/value pair into the map.
Definition: StringMap.h:298
SmallPtrSet< Module *, 4 > OwnedModules
OwnedModules - The set of modules instantiated in this context, and which will be automatically delet...
void(* InlineAsmDiagHandlerTy)(const SMDiagnostic &, void *Context, unsigned LocCookie)
Definition: LLVMContext.h:75
StringMap - This is an unconventional map that is specialized for handling keys that are "strings"...
Definition: StringMap.h:214
Basic diagnostic printer that uses an underlying raw_ostream.
void * getInlineAsmDiagnosticContext() const
getInlineAsmDiagnosticContext - Return the diagnostic context set by setInlineAsmDiagnosticHandler.
iterator begin()
Definition: StringMap.h:252
#define I(x, y, z)
Definition: MD5.cpp:54
void diagnose(const DiagnosticInfo &DI)
Report a message to the currently installed diagnostic handler.
void setDiagnosticHandler(DiagnosticHandlerTy DiagHandler, void *DiagContext=nullptr, bool RespectFilters=false)
setDiagnosticHandler - This method sets a handler that is invoked when the backend needs to report an...
LLVMContext::DiagnosticHandlerTy DiagnosticHandler
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:40
ManagedStatic - This transparently changes the behavior of global statics to be lazily constructed on...
Definition: ManagedStatic.h:61
static bool isDiagnosticEnabled(const DiagnosticInfo &DI)
iterator end()
Definition: StringMap.h:255
LLVMContext & getGlobalContext()
getGlobalContext - Returns a global context.
Definition: LLVMContext.cpp:30
int getKind() const
void resize(size_type N)
Definition: SmallVector.h:376