InstrProf: Simplify counting a file's regions when writing coverage (NFC)
[oota-llvm.git] / lib / IR / LLVMContext.cpp
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/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"
25 #include <cctype>
26 using namespace llvm;
27
28 static ManagedStatic<LLVMContext> GlobalContext;
29
30 LLVMContext& llvm::getGlobalContext() {
31   return *GlobalContext;
32 }
33
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.
37
38   // Create the 'dbg' metadata kind.
39   unsigned DbgID = getMDKindID("dbg");
40   assert(DbgID == MD_dbg && "dbg kind id drifted"); (void)DbgID;
41
42   // Create the 'tbaa' metadata kind.
43   unsigned TBAAID = getMDKindID("tbaa");
44   assert(TBAAID == MD_tbaa && "tbaa kind id drifted"); (void)TBAAID;
45
46   // Create the 'prof' metadata kind.
47   unsigned ProfID = getMDKindID("prof");
48   assert(ProfID == MD_prof && "prof kind id drifted"); (void)ProfID;
49
50   // Create the 'fpmath' metadata kind.
51   unsigned FPAccuracyID = getMDKindID("fpmath");
52   assert(FPAccuracyID == MD_fpmath && "fpmath kind id drifted");
53   (void)FPAccuracyID;
54
55   // Create the 'range' metadata kind.
56   unsigned RangeID = getMDKindID("range");
57   assert(RangeID == MD_range && "range kind id drifted");
58   (void)RangeID;
59
60   // Create the 'tbaa.struct' metadata kind.
61   unsigned TBAAStructID = getMDKindID("tbaa.struct");
62   assert(TBAAStructID == MD_tbaa_struct && "tbaa.struct kind id drifted");
63   (void)TBAAStructID;
64
65   // Create the 'invariant.load' metadata kind.
66   unsigned InvariantLdId = getMDKindID("invariant.load");
67   assert(InvariantLdId == MD_invariant_load && "invariant.load kind id drifted");
68   (void)InvariantLdId;
69
70   // Create the 'alias.scope' metadata kind.
71   unsigned AliasScopeID = getMDKindID("alias.scope");
72   assert(AliasScopeID == MD_alias_scope && "alias.scope kind id drifted");
73   (void)AliasScopeID;
74
75   // Create the 'noalias' metadata kind.
76   unsigned NoAliasID = getMDKindID("noalias");
77   assert(NoAliasID == MD_noalias && "noalias kind id drifted");
78   (void)NoAliasID;
79 }
80 LLVMContext::~LLVMContext() { delete pImpl; }
81
82 void LLVMContext::addModule(Module *M) {
83   pImpl->OwnedModules.insert(M);
84 }
85
86 void LLVMContext::removeModule(Module *M) {
87   pImpl->OwnedModules.erase(M);
88 }
89
90 //===----------------------------------------------------------------------===//
91 // Recoverable Backend Errors
92 //===----------------------------------------------------------------------===//
93
94 void LLVMContext::
95 setInlineAsmDiagnosticHandler(InlineAsmDiagHandlerTy DiagHandler,
96                               void *DiagContext) {
97   pImpl->InlineAsmDiagHandler = DiagHandler;
98   pImpl->InlineAsmDiagContext = DiagContext;
99 }
100
101 /// getInlineAsmDiagnosticHandler - Return the diagnostic handler set by
102 /// setInlineAsmDiagnosticHandler.
103 LLVMContext::InlineAsmDiagHandlerTy
104 LLVMContext::getInlineAsmDiagnosticHandler() const {
105   return pImpl->InlineAsmDiagHandler;
106 }
107
108 /// getInlineAsmDiagnosticContext - Return the diagnostic context set by
109 /// setInlineAsmDiagnosticHandler.
110 void *LLVMContext::getInlineAsmDiagnosticContext() const {
111   return pImpl->InlineAsmDiagContext;
112 }
113
114 void LLVMContext::setDiagnosticHandler(DiagnosticHandlerTy DiagnosticHandler,
115                                        void *DiagnosticContext,
116                                        bool RespectFilters) {
117   pImpl->DiagnosticHandler = DiagnosticHandler;
118   pImpl->DiagnosticContext = DiagnosticContext;
119   pImpl->RespectDiagnosticFilters = RespectFilters;
120 }
121
122 LLVMContext::DiagnosticHandlerTy LLVMContext::getDiagnosticHandler() const {
123   return pImpl->DiagnosticHandler;
124 }
125
126 void *LLVMContext::getDiagnosticContext() const {
127   return pImpl->DiagnosticContext;
128 }
129
130 void LLVMContext::setYieldCallback(YieldCallbackTy Callback, void *OpaqueHandle)
131 {
132   pImpl->YieldCallback = Callback;
133   pImpl->YieldOpaqueHandle = OpaqueHandle;
134 }
135
136 void LLVMContext::yield() {
137   if (pImpl->YieldCallback)
138     pImpl->YieldCallback(this, pImpl->YieldOpaqueHandle);
139 }
140
141 void LLVMContext::emitError(const Twine &ErrorStr) {
142   diagnose(DiagnosticInfoInlineAsm(ErrorStr));
143 }
144
145 void LLVMContext::emitError(const Instruction *I, const Twine &ErrorStr) {
146   assert (I && "Invalid instruction");
147   diagnose(DiagnosticInfoInlineAsm(*I, ErrorStr));
148 }
149
150 static bool isDiagnosticEnabled(const DiagnosticInfo &DI) {
151   // Optimization remarks are selective. They need to check whether the regexp
152   // pattern, passed via one of the -pass-remarks* flags, matches the name of
153   // the pass that is emitting the diagnostic. If there is no match, ignore the
154   // diagnostic and return.
155   switch (DI.getKind()) {
156   case llvm::DK_OptimizationRemark:
157     if (!cast<DiagnosticInfoOptimizationRemark>(DI).isEnabled())
158       return false;
159     break;
160   case llvm::DK_OptimizationRemarkMissed:
161     if (!cast<DiagnosticInfoOptimizationRemarkMissed>(DI).isEnabled())
162       return false;
163     break;
164   case llvm::DK_OptimizationRemarkAnalysis:
165     if (!cast<DiagnosticInfoOptimizationRemarkAnalysis>(DI).isEnabled())
166       return false;
167     break;
168   default:
169     break;
170   }
171   return true;
172 }
173
174 void LLVMContext::diagnose(const DiagnosticInfo &DI) {
175   // If there is a report handler, use it.
176   if (pImpl->DiagnosticHandler) {
177     if (!pImpl->RespectDiagnosticFilters || isDiagnosticEnabled(DI))
178       pImpl->DiagnosticHandler(DI, pImpl->DiagnosticContext);
179     return;
180   }
181
182   if (!isDiagnosticEnabled(DI))
183     return;
184
185   // Otherwise, print the message with a prefix based on the severity.
186   std::string MsgStorage;
187   raw_string_ostream Stream(MsgStorage);
188   DiagnosticPrinterRawOStream DP(Stream);
189   DI.print(DP);
190   Stream.flush();
191   switch (DI.getSeverity()) {
192   case DS_Error:
193     errs() << "error: " << MsgStorage << "\n";
194     exit(1);
195   case DS_Warning:
196     errs() << "warning: " << MsgStorage << "\n";
197     break;
198   case DS_Remark:
199     errs() << "remark: " << MsgStorage << "\n";
200     break;
201   case DS_Note:
202     errs() << "note: " << MsgStorage << "\n";
203     break;
204   }
205 }
206
207 void LLVMContext::emitError(unsigned LocCookie, const Twine &ErrorStr) {
208   diagnose(DiagnosticInfoInlineAsm(LocCookie, ErrorStr));
209 }
210
211 //===----------------------------------------------------------------------===//
212 // Metadata Kind Uniquing
213 //===----------------------------------------------------------------------===//
214
215 #ifndef NDEBUG
216 /// isValidName - Return true if Name is a valid custom metadata handler name.
217 static bool isValidName(StringRef MDName) {
218   if (MDName.empty())
219     return false;
220
221   if (!std::isalpha(static_cast<unsigned char>(MDName[0])))
222     return false;
223
224   for (StringRef::iterator I = MDName.begin() + 1, E = MDName.end(); I != E;
225        ++I) {
226     if (!std::isalnum(static_cast<unsigned char>(*I)) && *I != '_' &&
227         *I != '-' && *I != '.')
228       return false;
229   }
230   return true;
231 }
232 #endif
233
234 /// getMDKindID - Return a unique non-zero ID for the specified metadata kind.
235 unsigned LLVMContext::getMDKindID(StringRef Name) const {
236   assert(isValidName(Name) && "Invalid MDNode name");
237
238   // If this is new, assign it its ID.
239   return
240     pImpl->CustomMDKindNames.GetOrCreateValue(
241       Name, pImpl->CustomMDKindNames.size()).second;
242 }
243
244 /// getHandlerNames - Populate client supplied smallvector using custome
245 /// metadata name and ID.
246 void LLVMContext::getMDKindNames(SmallVectorImpl<StringRef> &Names) const {
247   Names.resize(pImpl->CustomMDKindNames.size());
248   for (StringMap<unsigned>::const_iterator I = pImpl->CustomMDKindNames.begin(),
249        E = pImpl->CustomMDKindNames.end(); I != E; ++I)
250     Names[I->second] = I->first();
251 }