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 LLVMContext::~LLVMContext() { delete pImpl; }
82 void LLVMContext::addModule(Module *M) {
83 pImpl->OwnedModules.insert(M);
86 void LLVMContext::removeModule(Module *M) {
87 pImpl->OwnedModules.erase(M);
90 //===----------------------------------------------------------------------===//
91 // Recoverable Backend Errors
92 //===----------------------------------------------------------------------===//
95 setInlineAsmDiagnosticHandler(InlineAsmDiagHandlerTy DiagHandler,
97 pImpl->InlineAsmDiagHandler = DiagHandler;
98 pImpl->InlineAsmDiagContext = DiagContext;
101 /// getInlineAsmDiagnosticHandler - Return the diagnostic handler set by
102 /// setInlineAsmDiagnosticHandler.
103 LLVMContext::InlineAsmDiagHandlerTy
104 LLVMContext::getInlineAsmDiagnosticHandler() const {
105 return pImpl->InlineAsmDiagHandler;
108 /// getInlineAsmDiagnosticContext - Return the diagnostic context set by
109 /// setInlineAsmDiagnosticHandler.
110 void *LLVMContext::getInlineAsmDiagnosticContext() const {
111 return pImpl->InlineAsmDiagContext;
114 void LLVMContext::setDiagnosticHandler(DiagnosticHandlerTy DiagnosticHandler,
115 void *DiagnosticContext) {
116 pImpl->DiagnosticHandler = DiagnosticHandler;
117 pImpl->DiagnosticContext = DiagnosticContext;
120 LLVMContext::DiagnosticHandlerTy LLVMContext::getDiagnosticHandler() const {
121 return pImpl->DiagnosticHandler;
124 void *LLVMContext::getDiagnosticContext() const {
125 return pImpl->DiagnosticContext;
128 void LLVMContext::setYieldCallback(YieldCallbackTy Callback, void *OpaqueHandle)
130 pImpl->YieldCallback = Callback;
131 pImpl->YieldOpaqueHandle = OpaqueHandle;
134 void LLVMContext::yield() {
135 if (pImpl->YieldCallback)
136 pImpl->YieldCallback(this, pImpl->YieldOpaqueHandle);
139 void LLVMContext::emitError(const Twine &ErrorStr) {
140 diagnose(DiagnosticInfoInlineAsm(ErrorStr));
143 void LLVMContext::emitError(const Instruction *I, const Twine &ErrorStr) {
144 assert (I && "Invalid instruction");
145 diagnose(DiagnosticInfoInlineAsm(*I, ErrorStr));
148 void LLVMContext::diagnose(const DiagnosticInfo &DI) {
149 // If there is a report handler, use it.
150 if (pImpl->DiagnosticHandler) {
151 pImpl->DiagnosticHandler(DI, pImpl->DiagnosticContext);
155 // Optimization remarks are selective. They need to check whether the regexp
156 // pattern, passed via one of the -pass-remarks* flags, matches the name of
157 // the pass that is emitting the diagnostic. If there is no match, ignore the
158 // diagnostic and return.
159 switch (DI.getKind()) {
160 case llvm::DK_OptimizationRemark:
161 if (!cast<DiagnosticInfoOptimizationRemark>(DI).isEnabled())
164 case llvm::DK_OptimizationRemarkMissed:
165 if (!cast<DiagnosticInfoOptimizationRemarkMissed>(DI).isEnabled())
168 case llvm::DK_OptimizationRemarkAnalysis:
169 if (!cast<DiagnosticInfoOptimizationRemarkAnalysis>(DI).isEnabled())
176 // Otherwise, print the message with a prefix based on the severity.
177 std::string MsgStorage;
178 raw_string_ostream Stream(MsgStorage);
179 DiagnosticPrinterRawOStream DP(Stream);
182 switch (DI.getSeverity()) {
184 errs() << "error: " << MsgStorage << "\n";
187 errs() << "warning: " << MsgStorage << "\n";
190 errs() << "remark: " << MsgStorage << "\n";
193 errs() << "note: " << MsgStorage << "\n";
198 void LLVMContext::emitError(unsigned LocCookie, const Twine &ErrorStr) {
199 diagnose(DiagnosticInfoInlineAsm(LocCookie, ErrorStr));
202 //===----------------------------------------------------------------------===//
203 // Metadata Kind Uniquing
204 //===----------------------------------------------------------------------===//
207 /// isValidName - Return true if Name is a valid custom metadata handler name.
208 static bool isValidName(StringRef MDName) {
212 if (!std::isalpha(static_cast<unsigned char>(MDName[0])))
215 for (StringRef::iterator I = MDName.begin() + 1, E = MDName.end(); I != E;
217 if (!std::isalnum(static_cast<unsigned char>(*I)) && *I != '_' &&
218 *I != '-' && *I != '.')
225 /// getMDKindID - Return a unique non-zero ID for the specified metadata kind.
226 unsigned LLVMContext::getMDKindID(StringRef Name) const {
227 assert(isValidName(Name) && "Invalid MDNode name");
229 // If this is new, assign it its ID.
231 pImpl->CustomMDKindNames.GetOrCreateValue(
232 Name, pImpl->CustomMDKindNames.size()).second;
235 /// getHandlerNames - Populate client supplied smallvector using custome
236 /// metadata name and ID.
237 void LLVMContext::getMDKindNames(SmallVectorImpl<StringRef> &Names) const {
238 Names.resize(pImpl->CustomMDKindNames.size());
239 for (StringMap<unsigned>::const_iterator I = pImpl->CustomMDKindNames.begin(),
240 E = pImpl->CustomMDKindNames.end(); I != E; ++I)
241 Names[I->second] = I->first();