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 // Create the 'make.implicit' metadata kind.
109 unsigned MakeImplicitID = getMDKindID("make.implicit");
110 assert(MakeImplicitID == MD_make_implicit &&
111 "make.implicit kind id drifted");
112 (void)MakeImplicitID;
114 // Create the 'unpredictable' metadata kind.
115 unsigned UnpredictableID = getMDKindID("unpredictable");
116 assert(UnpredictableID == MD_unpredictable &&
117 "unpredictable kind id drifted");
118 (void)UnpredictableID;
120 // Create the 'invariant.group' metadata kind.
121 unsigned InvariantGroupId = getMDKindID("invariant.group");
122 assert(InvariantGroupId == MD_invariant_group &&
123 "invariant.group kind id drifted");
124 (void)InvariantGroupId;
126 // Create the 'align' metadata kind.
127 unsigned AlignID = getMDKindID("align");
128 assert(AlignID == MD_align && "align kind id drifted");
131 LLVMContext::~LLVMContext() { delete pImpl; }
133 void LLVMContext::addModule(Module *M) {
134 pImpl->OwnedModules.insert(M);
137 void LLVMContext::removeModule(Module *M) {
138 pImpl->OwnedModules.erase(M);
141 //===----------------------------------------------------------------------===//
142 // Recoverable Backend Errors
143 //===----------------------------------------------------------------------===//
146 setInlineAsmDiagnosticHandler(InlineAsmDiagHandlerTy DiagHandler,
148 pImpl->InlineAsmDiagHandler = DiagHandler;
149 pImpl->InlineAsmDiagContext = DiagContext;
152 /// getInlineAsmDiagnosticHandler - Return the diagnostic handler set by
153 /// setInlineAsmDiagnosticHandler.
154 LLVMContext::InlineAsmDiagHandlerTy
155 LLVMContext::getInlineAsmDiagnosticHandler() const {
156 return pImpl->InlineAsmDiagHandler;
159 /// getInlineAsmDiagnosticContext - Return the diagnostic context set by
160 /// setInlineAsmDiagnosticHandler.
161 void *LLVMContext::getInlineAsmDiagnosticContext() const {
162 return pImpl->InlineAsmDiagContext;
165 void LLVMContext::setDiagnosticHandler(DiagnosticHandlerTy DiagnosticHandler,
166 void *DiagnosticContext,
167 bool RespectFilters) {
168 pImpl->DiagnosticHandler = DiagnosticHandler;
169 pImpl->DiagnosticContext = DiagnosticContext;
170 pImpl->RespectDiagnosticFilters = RespectFilters;
173 LLVMContext::DiagnosticHandlerTy LLVMContext::getDiagnosticHandler() const {
174 return pImpl->DiagnosticHandler;
177 void *LLVMContext::getDiagnosticContext() const {
178 return pImpl->DiagnosticContext;
181 void LLVMContext::setYieldCallback(YieldCallbackTy Callback, void *OpaqueHandle)
183 pImpl->YieldCallback = Callback;
184 pImpl->YieldOpaqueHandle = OpaqueHandle;
187 void LLVMContext::yield() {
188 if (pImpl->YieldCallback)
189 pImpl->YieldCallback(this, pImpl->YieldOpaqueHandle);
192 void LLVMContext::emitError(const Twine &ErrorStr) {
193 diagnose(DiagnosticInfoInlineAsm(ErrorStr));
196 void LLVMContext::emitError(const Instruction *I, const Twine &ErrorStr) {
197 assert (I && "Invalid instruction");
198 diagnose(DiagnosticInfoInlineAsm(*I, ErrorStr));
201 static bool isDiagnosticEnabled(const DiagnosticInfo &DI) {
202 // Optimization remarks are selective. They need to check whether the regexp
203 // pattern, passed via one of the -pass-remarks* flags, matches the name of
204 // the pass that is emitting the diagnostic. If there is no match, ignore the
205 // diagnostic and return.
206 switch (DI.getKind()) {
207 case llvm::DK_OptimizationRemark:
208 if (!cast<DiagnosticInfoOptimizationRemark>(DI).isEnabled())
211 case llvm::DK_OptimizationRemarkMissed:
212 if (!cast<DiagnosticInfoOptimizationRemarkMissed>(DI).isEnabled())
215 case llvm::DK_OptimizationRemarkAnalysis:
216 if (!cast<DiagnosticInfoOptimizationRemarkAnalysis>(DI).isEnabled())
219 case llvm::DK_OptimizationRemarkAnalysisFPCommute:
220 if (!cast<DiagnosticInfoOptimizationRemarkAnalysisFPCommute>(DI)
230 static const char *getDiagnosticMessagePrefix(DiagnosticSeverity Severity) {
241 llvm_unreachable("Unknown DiagnosticSeverity");
244 void LLVMContext::diagnose(const DiagnosticInfo &DI) {
245 // If there is a report handler, use it.
246 if (pImpl->DiagnosticHandler) {
247 if (!pImpl->RespectDiagnosticFilters || isDiagnosticEnabled(DI))
248 pImpl->DiagnosticHandler(DI, pImpl->DiagnosticContext);
252 if (!isDiagnosticEnabled(DI))
255 // Otherwise, print the message with a prefix based on the severity.
256 DiagnosticPrinterRawOStream DP(errs());
257 errs() << getDiagnosticMessagePrefix(DI.getSeverity()) << ": ";
260 if (DI.getSeverity() == DS_Error)
264 void LLVMContext::emitError(unsigned LocCookie, const Twine &ErrorStr) {
265 diagnose(DiagnosticInfoInlineAsm(LocCookie, ErrorStr));
268 //===----------------------------------------------------------------------===//
269 // Metadata Kind Uniquing
270 //===----------------------------------------------------------------------===//
272 /// Return a unique non-zero ID for the specified metadata kind.
273 unsigned LLVMContext::getMDKindID(StringRef Name) const {
274 // If this is new, assign it its ID.
275 return pImpl->CustomMDKindNames.insert(
277 Name, pImpl->CustomMDKindNames.size()))
281 /// getHandlerNames - Populate client-supplied smallvector using custom
282 /// metadata name and ID.
283 void LLVMContext::getMDKindNames(SmallVectorImpl<StringRef> &Names) const {
284 Names.resize(pImpl->CustomMDKindNames.size());
285 for (StringMap<unsigned>::const_iterator I = pImpl->CustomMDKindNames.begin(),
286 E = pImpl->CustomMDKindNames.end(); I != E; ++I)
287 Names[I->second] = I->first();
290 void LLVMContext::getOperandBundleTags(SmallVectorImpl<StringRef> &Tags) const {
291 pImpl->getOperandBundleTags(Tags);
294 uint32_t LLVMContext::getOperandBundleTagID(StringRef Tag) const {
295 return pImpl->getOperandBundleTagID(Tag);