LLVM  4.0.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 "llvm/ADT/SmallVector.h"
17 #include "llvm/ADT/StringMap.h"
18 #include "llvm/ADT/StringRef.h"
19 #include "llvm/ADT/Twine.h"
20 #include "LLVMContextImpl.h"
21 #include "llvm/IR/DiagnosticInfo.h"
23 #include "llvm/IR/Metadata.h"
24 #include "llvm/IR/Module.h"
25 #include "llvm/Support/Casting.h"
28 #include <cassert>
29 #include <cstdlib>
30 #include <string>
31 #include <utility>
32 
33 using namespace llvm;
34 
36  // Create the fixed metadata kinds. This is done in the same order as the
37  // MD_* enum values so that they correspond.
38  std::pair<unsigned, StringRef> MDKinds[] = {
39  {MD_dbg, "dbg"},
40  {MD_tbaa, "tbaa"},
41  {MD_prof, "prof"},
42  {MD_fpmath, "fpmath"},
43  {MD_range, "range"},
44  {MD_tbaa_struct, "tbaa.struct"},
45  {MD_invariant_load, "invariant.load"},
46  {MD_alias_scope, "alias.scope"},
47  {MD_noalias, "noalias"},
48  {MD_nontemporal, "nontemporal"},
49  {MD_mem_parallel_loop_access, "llvm.mem.parallel_loop_access"},
50  {MD_nonnull, "nonnull"},
51  {MD_dereferenceable, "dereferenceable"},
52  {MD_dereferenceable_or_null, "dereferenceable_or_null"},
53  {MD_make_implicit, "make.implicit"},
54  {MD_unpredictable, "unpredictable"},
55  {MD_invariant_group, "invariant.group"},
56  {MD_align, "align"},
57  {MD_loop, "llvm.loop"},
58  {MD_type, "type"},
59  {MD_section_prefix, "section_prefix"},
60  {MD_absolute_symbol, "absolute_symbol"},
61  };
62 
63  for (auto &MDKind : MDKinds) {
64  unsigned ID = getMDKindID(MDKind.second);
65  assert(ID == MDKind.first && "metadata kind id drifted");
66  (void)ID;
67  }
68 
69  auto *DeoptEntry = pImpl->getOrInsertBundleTag("deopt");
70  assert(DeoptEntry->second == LLVMContext::OB_deopt &&
71  "deopt operand bundle id drifted!");
72  (void)DeoptEntry;
73 
74  auto *FuncletEntry = pImpl->getOrInsertBundleTag("funclet");
75  assert(FuncletEntry->second == LLVMContext::OB_funclet &&
76  "funclet operand bundle id drifted!");
77  (void)FuncletEntry;
78 
79  auto *GCTransitionEntry = pImpl->getOrInsertBundleTag("gc-transition");
80  assert(GCTransitionEntry->second == LLVMContext::OB_gc_transition &&
81  "gc-transition operand bundle id drifted!");
82  (void)GCTransitionEntry;
83 }
84 
86 
87 void LLVMContext::addModule(Module *M) {
89 }
90 
91 void LLVMContext::removeModule(Module *M) {
93 }
94 
95 //===----------------------------------------------------------------------===//
96 // Recoverable Backend Errors
97 //===----------------------------------------------------------------------===//
98 
99 void LLVMContext::
100 setInlineAsmDiagnosticHandler(InlineAsmDiagHandlerTy DiagHandler,
101  void *DiagContext) {
102  pImpl->InlineAsmDiagHandler = DiagHandler;
103  pImpl->InlineAsmDiagContext = DiagContext;
104 }
105 
106 /// getInlineAsmDiagnosticHandler - Return the diagnostic handler set by
107 /// setInlineAsmDiagnosticHandler.
110  return pImpl->InlineAsmDiagHandler;
111 }
112 
113 /// getInlineAsmDiagnosticContext - Return the diagnostic context set by
114 /// setInlineAsmDiagnosticHandler.
116  return pImpl->InlineAsmDiagContext;
117 }
118 
119 void LLVMContext::setDiagnosticHandler(DiagnosticHandlerTy DiagnosticHandler,
120  void *DiagnosticContext,
121  bool RespectFilters) {
122  pImpl->DiagnosticHandler = DiagnosticHandler;
123  pImpl->DiagnosticContext = DiagnosticContext;
124  pImpl->RespectDiagnosticFilters = RespectFilters;
125 }
126 
128  pImpl->DiagnosticHotnessRequested = Requested;
129 }
132 }
133 
135  return pImpl->DiagnosticsOutputFile.get();
136 }
137 
138 void LLVMContext::setDiagnosticsOutputFile(std::unique_ptr<yaml::Output> F) {
139  pImpl->DiagnosticsOutputFile = std::move(F);
140 }
141 
143  return pImpl->DiagnosticHandler;
144 }
145 
147  return pImpl->DiagnosticContext;
148 }
149 
150 void LLVMContext::setYieldCallback(YieldCallbackTy Callback, void *OpaqueHandle)
151 {
152  pImpl->YieldCallback = Callback;
153  pImpl->YieldOpaqueHandle = OpaqueHandle;
154 }
155 
157  if (pImpl->YieldCallback)
159 }
160 
161 void LLVMContext::emitError(const Twine &ErrorStr) {
163 }
164 
165 void LLVMContext::emitError(const Instruction *I, const Twine &ErrorStr) {
166  assert (I && "Invalid instruction");
167  diagnose(DiagnosticInfoInlineAsm(*I, ErrorStr));
168 }
169 
170 static bool isDiagnosticEnabled(const DiagnosticInfo &DI) {
171  // Optimization remarks are selective. They need to check whether the regexp
172  // pattern, passed via one of the -pass-remarks* flags, matches the name of
173  // the pass that is emitting the diagnostic. If there is no match, ignore the
174  // diagnostic and return.
175  if (auto *Remark = dyn_cast<DiagnosticInfoOptimizationBase>(&DI))
176  return Remark->isEnabled();
177 
178  return true;
179 }
180 
181 const char *
183  switch (Severity) {
184  case DS_Error:
185  return "error";
186  case DS_Warning:
187  return "warning";
188  case DS_Remark:
189  return "remark";
190  case DS_Note:
191  return "note";
192  }
193  llvm_unreachable("Unknown DiagnosticSeverity");
194 }
195 
197  // If there is a report handler, use it.
198  if (pImpl->DiagnosticHandler) {
201  return;
202  }
203 
204  if (!isDiagnosticEnabled(DI))
205  return;
206 
207  // Otherwise, print the message with a prefix based on the severity.
209  errs() << getDiagnosticMessagePrefix(DI.getSeverity()) << ": ";
210  DI.print(DP);
211  errs() << "\n";
212  if (DI.getSeverity() == DS_Error)
213  exit(1);
214 }
215 
216 void LLVMContext::emitError(unsigned LocCookie, const Twine &ErrorStr) {
217  diagnose(DiagnosticInfoInlineAsm(LocCookie, ErrorStr));
218 }
219 
220 //===----------------------------------------------------------------------===//
221 // Metadata Kind Uniquing
222 //===----------------------------------------------------------------------===//
223 
224 /// Return a unique non-zero ID for the specified metadata kind.
226  // If this is new, assign it its ID.
228  std::make_pair(
229  Name, pImpl->CustomMDKindNames.size()))
230  .first->second;
231 }
232 
233 /// getHandlerNames - Populate client-supplied smallvector using custom
234 /// metadata name and ID.
238  E = pImpl->CustomMDKindNames.end(); I != E; ++I)
239  Names[I->second] = I->first();
240 }
241 
244 }
245 
247  return pImpl->getOperandBundleTagID(Tag);
248 }
249 
250 void LLVMContext::setGC(const Function &Fn, std::string GCName) {
251  auto It = pImpl->GCNames.find(&Fn);
252 
253  if (It == pImpl->GCNames.end()) {
254  pImpl->GCNames.insert(std::make_pair(&Fn, std::move(GCName)));
255  return;
256  }
257  It->second = std::move(GCName);
258 }
259 
260 const std::string &LLVMContext::getGC(const Function &Fn) {
261  return pImpl->GCNames[&Fn];
262 }
263 
265  pImpl->GCNames.erase(&Fn);
266 }
267 
269  return pImpl->DiscardValueNames;
270 }
271 
273 
275  if (pImpl->DITypeMap)
276  return;
277 
278  pImpl->DITypeMap.emplace();
279 }
280 
282 
284  pImpl->DiscardValueNames = Discard;
285 }
286 
288  return pImpl->getOptBisect();
289 }
raw_ostream & errs()
This returns a reference to a raw_ostream for standard error.
void setDiagnosticsOutputFile(std::unique_ptr< yaml::Output > F)
Set the diagnostics output file used for optimization diagnostics.
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:52
const std::string & getGC(const Function &Fn)
Return the GC for a function.
void(* DiagnosticHandlerTy)(const DiagnosticInfo &DI, void *Context)
Defines the type of a diagnostic handler.
Definition: LLVMContext.h:142
bool shouldDiscardValueNames() const
Return true if the Context runtime configuration is set to discard all value names.
DiagnosticHandlerTy getDiagnosticHandler() const
getDiagnosticHandler - Return the diagnostic handler set by setDiagnosticHandler. ...
void setGC(const Function &Fn, std::string GCName)
Define the GC for a function.
This file contains the declarations for metadata subclasses.
void enableDebugTypeODRUniquing()
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
void setDiscardValueNames(bool Discard)
Set the Context runtime configuration to discard all value name (but GlobalValue).
void disableDebugTypeODRUniquing()
bool isODRUniquingDebugTypes() const
Whether there is a string map for uniquing debug info identifiers across the context.
DiagnosticSeverity
Defines the different supported severity of a diagnostic.
OptBisect & getOptBisect()
Access the object which manages optimization bisection for failure analysis.
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:81
void setDiagnosticHotnessRequested(bool Requested)
Set if a code hotness metric should be included in optimization diagnostics.
void deleteGC(const Function &Fn)
Remove the GC for a function.
bool DiscardValueNames
Flag to indicate if Value (other than GlobalValue) retains their name or not.
void emitError(unsigned LocCookie, const Twine &ErrorStr)
emitError - Emit an error message to the currently installed error handler with optional location inf...
#define F(x, y, z)
Definition: MD5.cpp:51
DiagnosticSeverity getSeverity() const
yaml::Output * getDiagnosticsOutputFile()
Return the YAML file used by the backend to save optimization diagnostics.
static GCRegistry::Add< CoreCLRGC > E("coreclr","CoreCLR-compatible GC")
LLVMContext::YieldCallbackTy YieldCallback
void getMDKindNames(SmallVectorImpl< StringRef > &Result) const
getMDKindNames - Populate client supplied SmallVector with the name for custom metadata IDs registere...
This class implements a mechanism to disable passes and individual optimizations at compile time base...
Definition: OptBisect.h:28
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.
StringMapEntry< uint32_t > * getOrInsertBundleTag(StringRef Tag)
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:368
unsigned size() const
Definition: StringMap.h:114
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. ...
The Output class is used to generate a yaml document from in-memory structs and vectors.
Definition: YAMLTraits.h:1249
bool getDiagnosticHotnessRequested() const
Return if a code hotness metric should be included in optimization diagnostics.
uint32_t getOperandBundleTagID(StringRef Tag) const
Diagnostic information for inline asm reporting.
virtual void print(DiagnosticPrinter &DP) const =0
Print using the given DP a user-friendly message.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
LLVMContextImpl *const pImpl
Definition: LLVMContext.h:50
void getOperandBundleTags(SmallVectorImpl< StringRef > &Result) const
getOperandBundleTags - Populate client supplied SmallVector with the bundle tags registered in this L...
bool erase(PtrType Ptr)
erase - If the set contains the specified pointer, remove it and return true, otherwise return false...
Definition: SmallPtrSet.h:375
Module.h This file contains the declarations for the Module class.
static const char * getDiagnosticMessagePrefix(DiagnosticSeverity Severity)
Get the prefix that should be printed in front of a diagnostic of the given Severity.
bool insert(MapEntryTy *KeyValue)
insert - Insert the specified key/value pair into the map.
Definition: StringMap.h:348
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:136
StringMap - This is an unconventional map that is specialized for handling keys that are "strings"...
Definition: StringMap.h:223
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:302
OptBisect & getOptBisect()
Access the object which manages optimization bisection for failure analysis.
uint32_t getOperandBundleTagID(StringRef Tag) const
getOperandBundleTagID - Maps a bundle tag to an integer ID.
std::unique_ptr< yaml::Output > DiagnosticsOutputFile
#define I(x, y, z)
Definition: MD5.cpp:54
DenseMap< const Function *, std::string > GCNames
Maintain the GC name for each function.
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...
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
LLVMContext::DiagnosticHandlerTy DiagnosticHandler
void getOperandBundleTags(SmallVectorImpl< StringRef > &Tags) const
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:47
static bool isDiagnosticEnabled(const DiagnosticInfo &DI)
Optional< DenseMap< const MDString *, DICompositeType * > > DITypeMap
iterator end()
Definition: StringMap.h:305
void resize(size_type N)
Definition: SmallVector.h:352