1 //===-- LLVMContext.cpp - Implement LLVMContext ---------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements LLVMContext, as a wrapper around the opaque
11 // class LLVMContextImpl.
13 //===----------------------------------------------------------------------===//
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"
20 #include "llvm/IR/DiagnosticPrinter.h"
21 #include "llvm/IR/Instruction.h"
22 #include "llvm/IR/Metadata.h"
23 #include "llvm/Support/ManagedStatic.h"
24 #include "llvm/Support/SourceMgr.h"
28 static ManagedStatic<LLVMContext> GlobalContext;
30 LLVMContext& llvm::getGlobalContext() {
31 return *GlobalContext;
34 LLVMContext::LLVMContext() : pImpl(new LLVMContextImpl(*this)) {
35 // Create the fixed metadata kinds. This is done in the same order as the
36 // MD_* enum values so that they correspond.
38 // Create the 'dbg' metadata kind.
39 unsigned DbgID = getMDKindID("dbg");
40 assert(DbgID == MD_dbg && "dbg kind id drifted"); (void)DbgID;
42 // Create the 'tbaa' metadata kind.
43 unsigned TBAAID = getMDKindID("tbaa");
44 assert(TBAAID == MD_tbaa && "tbaa kind id drifted"); (void)TBAAID;
46 // Create the 'prof' metadata kind.
47 unsigned ProfID = getMDKindID("prof");
48 assert(ProfID == MD_prof && "prof kind id drifted"); (void)ProfID;
50 // Create the 'fpmath' metadata kind.
51 unsigned FPAccuracyID = getMDKindID("fpmath");
52 assert(FPAccuracyID == MD_fpmath && "fpmath kind id drifted");
55 // Create the 'range' metadata kind.
56 unsigned RangeID = getMDKindID("range");
57 assert(RangeID == MD_range && "range kind id drifted");
60 // Create the 'tbaa.struct' metadata kind.
61 unsigned TBAAStructID = getMDKindID("tbaa.struct");
62 assert(TBAAStructID == MD_tbaa_struct && "tbaa.struct kind id drifted");
65 // Create the 'invariant.load' metadata kind.
66 unsigned InvariantLdId = getMDKindID("invariant.load");
67 assert(InvariantLdId == MD_invariant_load && "invariant.load kind id drifted");
70 // Create the 'alias.scope' metadata kind.
71 unsigned AliasScopeID = getMDKindID("alias.scope");
72 assert(AliasScopeID == MD_alias_scope && "alias.scope kind id drifted");
75 // Create the 'noalias' metadata kind.
76 unsigned NoAliasID = getMDKindID("noalias");
77 assert(NoAliasID == MD_noalias && "noalias kind id drifted");
80 // Create the 'nontemporal' metadata kind.
81 unsigned NonTemporalID = getMDKindID("nontemporal");
82 assert(NonTemporalID == MD_nontemporal && "nontemporal kind id drifted");
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;
91 // Create the 'nonnull' metadata kind.
92 unsigned NonNullID = getMDKindID("nonnull");
93 assert(NonNullID == MD_nonnull && "nonnull kind id drifted");
96 // Create the 'dereferenceable' metadata kind.
97 unsigned DereferenceableID = getMDKindID("dereferenceable");
98 assert(DereferenceableID == MD_dereferenceable &&
99 "dereferenceable kind id drifted");
100 (void)DereferenceableID;
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;
108 LLVMContext::~LLVMContext() { delete pImpl; }
110 void LLVMContext::addModule(Module *M) {
111 pImpl->OwnedModules.insert(M);
114 void LLVMContext::removeModule(Module *M) {
115 pImpl->OwnedModules.erase(M);
118 //===----------------------------------------------------------------------===//
119 // Recoverable Backend Errors
120 //===----------------------------------------------------------------------===//
123 setInlineAsmDiagnosticHandler(InlineAsmDiagHandlerTy DiagHandler,
125 pImpl->InlineAsmDiagHandler = DiagHandler;
126 pImpl->InlineAsmDiagContext = DiagContext;
129 /// getInlineAsmDiagnosticHandler - Return the diagnostic handler set by
130 /// setInlineAsmDiagnosticHandler.
131 LLVMContext::InlineAsmDiagHandlerTy
132 LLVMContext::getInlineAsmDiagnosticHandler() const {
133 return pImpl->InlineAsmDiagHandler;
136 /// getInlineAsmDiagnosticContext - Return the diagnostic context set by
137 /// setInlineAsmDiagnosticHandler.
138 void *LLVMContext::getInlineAsmDiagnosticContext() const {
139 return pImpl->InlineAsmDiagContext;
142 void LLVMContext::setDiagnosticHandler(DiagnosticHandlerTy DiagnosticHandler,
143 void *DiagnosticContext,
144 bool RespectFilters) {
145 pImpl->DiagnosticHandler = DiagnosticHandler;
146 pImpl->DiagnosticContext = DiagnosticContext;
147 pImpl->RespectDiagnosticFilters = RespectFilters;
150 LLVMContext::DiagnosticHandlerTy LLVMContext::getDiagnosticHandler() const {
151 return pImpl->DiagnosticHandler;
154 void *LLVMContext::getDiagnosticContext() const {
155 return pImpl->DiagnosticContext;
158 void LLVMContext::setYieldCallback(YieldCallbackTy Callback, void *OpaqueHandle)
160 pImpl->YieldCallback = Callback;
161 pImpl->YieldOpaqueHandle = OpaqueHandle;
164 void LLVMContext::yield() {
165 if (pImpl->YieldCallback)
166 pImpl->YieldCallback(this, pImpl->YieldOpaqueHandle);
169 void LLVMContext::emitError(const Twine &ErrorStr) {
170 diagnose(DiagnosticInfoInlineAsm(ErrorStr));
173 void LLVMContext::emitError(const Instruction *I, const Twine &ErrorStr) {
174 assert (I && "Invalid instruction");
175 diagnose(DiagnosticInfoInlineAsm(*I, ErrorStr));
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()) {
184 case llvm::DK_OptimizationRemark:
185 if (!cast<DiagnosticInfoOptimizationRemark>(DI).isEnabled())
188 case llvm::DK_OptimizationRemarkMissed:
189 if (!cast<DiagnosticInfoOptimizationRemarkMissed>(DI).isEnabled())
192 case llvm::DK_OptimizationRemarkAnalysis:
193 if (!cast<DiagnosticInfoOptimizationRemarkAnalysis>(DI).isEnabled())
202 void LLVMContext::diagnose(const DiagnosticInfo &DI) {
203 // If there is a report handler, use it.
204 if (pImpl->DiagnosticHandler) {
205 if (!pImpl->RespectDiagnosticFilters || isDiagnosticEnabled(DI))
206 pImpl->DiagnosticHandler(DI, pImpl->DiagnosticContext);
210 if (!isDiagnosticEnabled(DI))
213 // Otherwise, print the message with a prefix based on the severity.
214 std::string MsgStorage;
215 raw_string_ostream Stream(MsgStorage);
216 DiagnosticPrinterRawOStream DP(Stream);
219 switch (DI.getSeverity()) {
221 errs() << "error: " << MsgStorage << "\n";
224 errs() << "warning: " << MsgStorage << "\n";
227 errs() << "remark: " << MsgStorage << "\n";
230 errs() << "note: " << MsgStorage << "\n";
235 void LLVMContext::emitError(unsigned LocCookie, const Twine &ErrorStr) {
236 diagnose(DiagnosticInfoInlineAsm(LocCookie, ErrorStr));
239 //===----------------------------------------------------------------------===//
240 // Metadata Kind Uniquing
241 //===----------------------------------------------------------------------===//
243 /// Return a unique non-zero ID for the specified metadata kind.
244 unsigned LLVMContext::getMDKindID(StringRef Name) const {
245 // If this is new, assign it its ID.
246 return pImpl->CustomMDKindNames.insert(
248 Name, pImpl->CustomMDKindNames.size()))
252 /// getHandlerNames - Populate client supplied smallvector using custome
253 /// metadata name and ID.
254 void LLVMContext::getMDKindNames(SmallVectorImpl<StringRef> &Names) const {
255 Names.resize(pImpl->CustomMDKindNames.size());
256 for (StringMap<unsigned>::const_iterator I = pImpl->CustomMDKindNames.begin(),
257 E = pImpl->CustomMDKindNames.end(); I != E; ++I)
258 Names[I->second] = I->first();