LLVM  6.0.0svn
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/ADT/SmallVector.h"
18 #include "llvm/ADT/StringMap.h"
19 #include "llvm/ADT/StringRef.h"
20 #include "llvm/ADT/Twine.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  {MD_associated, "associated"},
62  };
63 
64  for (auto &MDKind : MDKinds) {
65  unsigned ID = getMDKindID(MDKind.second);
66  assert(ID == MDKind.first && "metadata kind id drifted");
67  (void)ID;
68  }
69 
70  auto *DeoptEntry = pImpl->getOrInsertBundleTag("deopt");
71  assert(DeoptEntry->second == LLVMContext::OB_deopt &&
72  "deopt operand bundle id drifted!");
73  (void)DeoptEntry;
74 
75  auto *FuncletEntry = pImpl->getOrInsertBundleTag("funclet");
76  assert(FuncletEntry->second == LLVMContext::OB_funclet &&
77  "funclet operand bundle id drifted!");
78  (void)FuncletEntry;
79 
80  auto *GCTransitionEntry = pImpl->getOrInsertBundleTag("gc-transition");
81  assert(GCTransitionEntry->second == LLVMContext::OB_gc_transition &&
82  "gc-transition operand bundle id drifted!");
83  (void)GCTransitionEntry;
84 
85  SyncScope::ID SingleThreadSSID =
86  pImpl->getOrInsertSyncScopeID("singlethread");
87  assert(SingleThreadSSID == SyncScope::SingleThread &&
88  "singlethread synchronization scope ID drifted!");
89  (void)SingleThreadSSID;
90 
91  SyncScope::ID SystemSSID =
93  assert(SystemSSID == SyncScope::System &&
94  "system synchronization scope ID drifted!");
95  (void)SystemSSID;
96 }
97 
99 
100 void LLVMContext::addModule(Module *M) {
102 }
103 
104 void LLVMContext::removeModule(Module *M) {
106 }
107 
108 //===----------------------------------------------------------------------===//
109 // Recoverable Backend Errors
110 //===----------------------------------------------------------------------===//
111 
112 void LLVMContext::
114  void *DiagContext) {
115  pImpl->InlineAsmDiagHandler = DiagHandler;
116  pImpl->InlineAsmDiagContext = DiagContext;
117 }
118 
119 /// getInlineAsmDiagnosticHandler - Return the diagnostic handler set by
120 /// setInlineAsmDiagnosticHandler.
123  return pImpl->InlineAsmDiagHandler;
124 }
125 
126 /// getInlineAsmDiagnosticContext - Return the diagnostic context set by
127 /// setInlineAsmDiagnosticHandler.
129  return pImpl->InlineAsmDiagContext;
130 }
131 
134  void *DiagnosticContext, bool RespectFilters) {
135  pImpl->DiagHandler->DiagHandlerCallback = DiagnosticHandler;
136  pImpl->DiagHandler->DiagnosticContext = DiagnosticContext;
137  pImpl->RespectDiagnosticFilters = RespectFilters;
138 }
139 
140 void LLVMContext::setDiagnosticHandler(std::unique_ptr<DiagnosticHandler> &&DH,
141  bool RespectFilters) {
142  pImpl->DiagHandler = std::move(DH);
143  pImpl->RespectDiagnosticFilters = RespectFilters;
144 }
145 
147  pImpl->DiagnosticsHotnessRequested = Requested;
148 }
151 }
152 
155 }
158 }
159 
161  return pImpl->DiagnosticsOutputFile.get();
162 }
163 
164 void LLVMContext::setDiagnosticsOutputFile(std::unique_ptr<yaml::Output> F) {
165  pImpl->DiagnosticsOutputFile = std::move(F);
166 }
167 
170  return pImpl->DiagHandler->DiagHandlerCallback;
171 }
172 
174  return pImpl->DiagHandler->DiagnosticContext;
175 }
176 
177 void LLVMContext::setYieldCallback(YieldCallbackTy Callback, void *OpaqueHandle)
178 {
179  pImpl->YieldCallback = Callback;
180  pImpl->YieldOpaqueHandle = OpaqueHandle;
181 }
182 
184  if (pImpl->YieldCallback)
186 }
187 
188 void LLVMContext::emitError(const Twine &ErrorStr) {
190 }
191 
192 void LLVMContext::emitError(const Instruction *I, const Twine &ErrorStr) {
193  assert (I && "Invalid instruction");
194  diagnose(DiagnosticInfoInlineAsm(*I, ErrorStr));
195 }
196 
197 static bool isDiagnosticEnabled(const DiagnosticInfo &DI) {
198  // Optimization remarks are selective. They need to check whether the regexp
199  // pattern, passed via one of the -pass-remarks* flags, matches the name of
200  // the pass that is emitting the diagnostic. If there is no match, ignore the
201  // diagnostic and return.
202  if (auto *Remark = dyn_cast<DiagnosticInfoOptimizationBase>(&DI))
203  return Remark->isEnabled();
204 
205  return true;
206 }
207 
208 const char *
210  switch (Severity) {
211  case DS_Error:
212  return "error";
213  case DS_Warning:
214  return "warning";
215  case DS_Remark:
216  return "remark";
217  case DS_Note:
218  return "note";
219  }
220  llvm_unreachable("Unknown DiagnosticSeverity");
221 }
222 
224  // If there is a report handler, use it.
225  if (pImpl->DiagHandler &&
227  pImpl->DiagHandler->handleDiagnostics(DI))
228  return;
229 
230  if (!isDiagnosticEnabled(DI))
231  return;
232 
233  // Otherwise, print the message with a prefix based on the severity.
235  errs() << getDiagnosticMessagePrefix(DI.getSeverity()) << ": ";
236  DI.print(DP);
237  errs() << "\n";
238  if (DI.getSeverity() == DS_Error)
239  exit(1);
240 }
241 
242 void LLVMContext::emitError(unsigned LocCookie, const Twine &ErrorStr) {
243  diagnose(DiagnosticInfoInlineAsm(LocCookie, ErrorStr));
244 }
245 
246 //===----------------------------------------------------------------------===//
247 // Metadata Kind Uniquing
248 //===----------------------------------------------------------------------===//
249 
250 /// Return a unique non-zero ID for the specified metadata kind.
252  // If this is new, assign it its ID.
254  std::make_pair(
255  Name, pImpl->CustomMDKindNames.size()))
256  .first->second;
257 }
258 
259 /// getHandlerNames - Populate client-supplied smallvector using custom
260 /// metadata name and ID.
264  E = pImpl->CustomMDKindNames.end(); I != E; ++I)
265  Names[I->second] = I->first();
266 }
267 
270 }
271 
273  return pImpl->getOperandBundleTagID(Tag);
274 }
275 
277  return pImpl->getOrInsertSyncScopeID(SSN);
278 }
279 
281  pImpl->getSyncScopeNames(SSNs);
282 }
283 
284 void LLVMContext::setGC(const Function &Fn, std::string GCName) {
285  auto It = pImpl->GCNames.find(&Fn);
286 
287  if (It == pImpl->GCNames.end()) {
288  pImpl->GCNames.insert(std::make_pair(&Fn, std::move(GCName)));
289  return;
290  }
291  It->second = std::move(GCName);
292 }
293 
294 const std::string &LLVMContext::getGC(const Function &Fn) {
295  return pImpl->GCNames[&Fn];
296 }
297 
299  pImpl->GCNames.erase(&Fn);
300 }
301 
303  return pImpl->DiscardValueNames;
304 }
305 
307 
309  if (pImpl->DITypeMap)
310  return;
311 
312  pImpl->DITypeMap.emplace();
313 }
314 
316 
318  pImpl->DiscardValueNames = Discard;
319 }
320 
322  return pImpl->getOptBisect();
323 }
324 
326  return pImpl->DiagHandler.get();
327 }
328 
329 std::unique_ptr<DiagnosticHandler> LLVMContext::getDiagnosticHandler() {
330  return std::move(pImpl->DiagHandler);
331 }
void setDiagnosticHandler(std::unique_ptr< DiagnosticHandler > &&DH, bool RespectFilters=false)
setDiagnosticHandler - This method sets unique_ptr to object of DiagnosticHandler to provide custom d...
This is the base class for diagnostic handling in LLVM.
raw_ostream & errs()
This returns a reference to a raw_ostream for standard error.
void getOperandBundleTags(SmallVectorImpl< StringRef > &Tags) const
Compute iterated dominance frontiers using a linear time algorithm.
Definition: AllocatorList.h:24
void setDiagnosticsOutputFile(std::unique_ptr< yaml::Output > F)
Set the diagnostics output file used for optimization diagnostics.
A Module instance is used to store all the information related to an LLVM module. ...
Definition: Module.h:63
const std::string & getGC(const Function &Fn)
Return the GC for a function.
std::unique_ptr< DiagnosticHandler > getDiagnosticHandler()
getDiagnosticHandler - transfers owenership of DiagnosticHandler unique_ptr to caller.
void(*)(LLVMContext *Context, void *OpaqueHandle) YieldCallbackTy
Defines the type of a yield callback.
Definition: LLVMContext.h:173
uint32_t getOperandBundleTagID(StringRef Tag) const
void setGC(const Function &Fn, std::string GCName)
Define the GC for a function.
SyncScope::ID getOrInsertSyncScopeID(StringRef SSN)
getOrInsertSyncScopeID - Maps synchronization scope name to synchronization scope ID...
This file contains the declarations for metadata subclasses.
Synchronized with respect to signal handlers executing in the same thread.
Definition: LLVMContext.h:56
void enableDebugTypeODRUniquing()
void * getInlineAsmDiagnosticContext() const
getInlineAsmDiagnosticContext - Return the diagnostic context set by setInlineAsmDiagnosticHandler.
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 getSyncScopeNames(SmallVectorImpl< StringRef > &SSNs) const
getSyncScopeNames - Populates client supplied SmallVector with synchronization scope names registered...
F(f)
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()
unsigned getMDKindID(StringRef Name) const
getMDKindID - Return a unique non-zero ID for the specified metadata kind.
void setDiagnosticsHotnessThreshold(uint64_t Threshold)
Set the minimum hotness value a diagnostic needs in order to be included in optimization diagnostics...
DiagnosticSeverity
Defines the different supported severity of a diagnostic.
unsigned size() const
Definition: StringMap.h:112
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 getMDKindNames(SmallVectorImpl< StringRef > &Result) const
getMDKindNames - Populate client supplied SmallVector with the name for custom metadata IDs registere...
void deleteGC(const Function &Fn)
Remove the GC for a function.
std::unique_ptr< DiagnosticHandler > DiagHandler
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...
void setDiagnosticsHotnessRequested(bool Requested)
Set if a code hotness metric should be included in optimization diagnostics.
void(*)(const DiagnosticInfo &DI, void *Context) DiagnosticHandlerTy
yaml::Output * getDiagnosticsOutputFile()
Return the YAML file used by the backend to save optimization diagnostics.
LLVMContext::YieldCallbackTy YieldCallback
void setDiagnosticHandlerCallBack(DiagnosticHandler::DiagnosticHandlerTy DiagHandler, void *DiagContext=nullptr, bool RespectFilters=false)
setDiagnosticHandlerCallBack - This method sets a handler call back that is invoked when the backend ...
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.
Synchronized with respect to all concurrently executing threads.
Definition: LLVMContext.h:59
This is the base abstract class for diagnostic reporting in the backend.
StringMapEntry< uint32_t > * getOrInsertBundleTag(StringRef Tag)
bool shouldDiscardValueNames() const
Return true if the Context runtime configuration is set to discard all value names.
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
void getOperandBundleTags(SmallVectorImpl< StringRef > &Result) const
getOperandBundleTags - Populate client supplied SmallVector with the bundle tags registered in this L...
void getSyncScopeNames(SmallVectorImpl< StringRef > &SSNs) const
getSyncScopeNames - Populates client supplied SmallVector with synchronization scope names registered...
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:363
uint64_t getDiagnosticsHotnessThreshold() const
Return the minimum hotness value a diagnostic would need in order to be included in optimization diag...
bool getDiagnosticsHotnessRequested() const
Return if a code hotness metric should be included in optimization diagnostics.
bool isODRUniquingDebugTypes() const
Whether there is a string map for uniquing debug info identifiers across the context.
uint64_t DiagnosticsHotnessThreshold
The Output class is used to generate a yaml document from in-memory structs and vectors.
Definition: YAMLTraits.h:1260
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:71
bool erase(PtrType Ptr)
erase - If the set contains the specified pointer, remove it and return true, otherwise return false...
Definition: SmallPtrSet.h:370
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:370
SmallPtrSet< Module *, 4 > OwnedModules
OwnedModules - The set of modules instantiated in this context, and which will be automatically delet...
StringMap - This is an unconventional map that is specialized for handling keys that are "strings"...
Definition: StringMap.h:224
void(*)(const SMDiagnostic &, void *Context, unsigned LocCookie) InlineAsmDiagHandlerTy
Definition: LLVMContext.h:169
SyncScope::ID getOrInsertSyncScopeID(StringRef SSN)
getOrInsertSyncScopeID - Maps synchronization scope name to synchronization scope ID...
Basic diagnostic printer that uses an underlying raw_ostream.
iterator begin()
Definition: StringMap.h:319
OptBisect & getOptBisect()
Access the object which manages optimization bisection for failure analysis.
std::unique_ptr< yaml::Output > DiagnosticsOutputFile
#define I(x, y, z)
Definition: MD5.cpp:58
DenseMap< const Function *, std::string > GCNames
Maintain the GC name for each function.
DiagnosticSeverity getSeverity() const
InlineAsmDiagHandlerTy getInlineAsmDiagnosticHandler() const
getInlineAsmDiagnosticHandler - Return the diagnostic handler set by setInlineAsmDiagnosticHandler.
void diagnose(const DiagnosticInfo &DI)
Report a message to the currently installed diagnostic handler.
const DiagnosticHandler * getDiagHandlerPtr() const
getDiagHandlerPtr - Returns const raw pointer of DiagnosticHandler set by setDiagnosticHandler.
static int const Threshold
TODO: Write a new FunctionPass AliasAnalysis so that it can keep a cache.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:49
void * getDiagnosticContext() const
getDiagnosticContext - Return the diagnostic context set by setDiagnosticContext. ...
static bool isDiagnosticEnabled(const DiagnosticInfo &DI)
DiagnosticHandler::DiagnosticHandlerTy getDiagnosticHandlerCallBack() const
getDiagnosticHandlerCallBack - Return the diagnostic handler call back set by setDiagnosticHandlerCal...
Optional< DenseMap< const MDString *, DICompositeType * > > DITypeMap
iterator end()
Definition: StringMap.h:322
uint32_t getOperandBundleTagID(StringRef Tag) const
getOperandBundleTagID - Maps a bundle tag to an integer ID.
void resize(size_type N)
Definition: SmallVector.h:355