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 "llvm/ADT/SmallVector.h"
17 #include "llvm/ADT/StringMap.h"
18 #include "llvm/ADT/StringRef.h"
19 #include "llvm/ADT/Twine.h"
20 #include "LLVMContextImpl.h"
21 #include "llvm/IR/DiagnosticInfo.h"
22 #include "llvm/IR/DiagnosticPrinter.h"
23 #include "llvm/IR/Metadata.h"
24 #include "llvm/IR/Module.h"
25 #include "llvm/Support/Casting.h"
26 #include "llvm/Support/ErrorHandling.h"
27 #include "llvm/Support/raw_ostream.h"
35 LLVMContext::LLVMContext() : pImpl(new LLVMContextImpl(*this)) {
36 // Create the fixed metadata kinds. This is done in the same order as the
37 // MD_* enum values so that they correspond.
39 // Create the 'dbg' metadata kind.
40 unsigned DbgID = getMDKindID("dbg");
41 assert(DbgID == MD_dbg && "dbg kind id drifted"); (void)DbgID;
43 // Create the 'tbaa' metadata kind.
44 unsigned TBAAID = getMDKindID("tbaa");
45 assert(TBAAID == MD_tbaa && "tbaa kind id drifted"); (void)TBAAID;
47 // Create the 'prof' metadata kind.
48 unsigned ProfID = getMDKindID("prof");
49 assert(ProfID == MD_prof && "prof kind id drifted"); (void)ProfID;
51 // Create the 'fpmath' metadata kind.
52 unsigned FPAccuracyID = getMDKindID("fpmath");
53 assert(FPAccuracyID == MD_fpmath && "fpmath kind id drifted");
56 // Create the 'range' metadata kind.
57 unsigned RangeID = getMDKindID("range");
58 assert(RangeID == MD_range && "range kind id drifted");
61 // Create the 'tbaa.struct' metadata kind.
62 unsigned TBAAStructID = getMDKindID("tbaa.struct");
63 assert(TBAAStructID == MD_tbaa_struct && "tbaa.struct kind id drifted");
66 // Create the 'invariant.load' metadata kind.
67 unsigned InvariantLdId = getMDKindID("invariant.load");
68 assert(InvariantLdId == MD_invariant_load && "invariant.load kind id drifted");
71 // Create the 'alias.scope' metadata kind.
72 unsigned AliasScopeID = getMDKindID("alias.scope");
73 assert(AliasScopeID == MD_alias_scope && "alias.scope kind id drifted");
76 // Create the 'noalias' metadata kind.
77 unsigned NoAliasID = getMDKindID("noalias");
78 assert(NoAliasID == MD_noalias && "noalias kind id drifted");
81 // Create the 'nontemporal' metadata kind.
82 unsigned NonTemporalID = getMDKindID("nontemporal");
83 assert(NonTemporalID == MD_nontemporal && "nontemporal kind id drifted");
86 // Create the 'llvm.mem.parallel_loop_access' metadata kind.
87 unsigned MemParallelLoopAccessID = getMDKindID("llvm.mem.parallel_loop_access");
88 assert(MemParallelLoopAccessID == MD_mem_parallel_loop_access &&
89 "mem_parallel_loop_access kind id drifted");
90 (void)MemParallelLoopAccessID;
92 // Create the 'nonnull' metadata kind.
93 unsigned NonNullID = getMDKindID("nonnull");
94 assert(NonNullID == MD_nonnull && "nonnull kind id drifted");
97 // Create the 'dereferenceable' metadata kind.
98 unsigned DereferenceableID = getMDKindID("dereferenceable");
99 assert(DereferenceableID == MD_dereferenceable &&
100 "dereferenceable kind id drifted");
101 (void)DereferenceableID;
103 // Create the 'dereferenceable_or_null' metadata kind.
104 unsigned DereferenceableOrNullID = getMDKindID("dereferenceable_or_null");
105 assert(DereferenceableOrNullID == MD_dereferenceable_or_null &&
106 "dereferenceable_or_null kind id drifted");
107 (void)DereferenceableOrNullID;
109 // Create the 'make.implicit' metadata kind.
110 unsigned MakeImplicitID = getMDKindID("make.implicit");
111 assert(MakeImplicitID == MD_make_implicit &&
112 "make.implicit kind id drifted");
113 (void)MakeImplicitID;
115 // Create the 'unpredictable' metadata kind.
116 unsigned UnpredictableID = getMDKindID("unpredictable");
117 assert(UnpredictableID == MD_unpredictable &&
118 "unpredictable kind id drifted");
119 (void)UnpredictableID;
121 // Create the 'invariant.group' metadata kind.
122 unsigned InvariantGroupId = getMDKindID("invariant.group");
123 assert(InvariantGroupId == MD_invariant_group &&
124 "invariant.group kind id drifted");
125 (void)InvariantGroupId;
127 // Create the 'align' metadata kind.
128 unsigned AlignID = getMDKindID("align");
129 assert(AlignID == MD_align && "align kind id drifted");
132 // Create the 'llvm.loop' metadata kind.
133 unsigned LoopID = getMDKindID("llvm.loop");
134 assert(LoopID == MD_loop && "llvm.loop kind id drifted");
137 unsigned TypeID = getMDKindID("type");
138 assert(TypeID == MD_type && "type kind id drifted");
141 auto *DeoptEntry = pImpl->getOrInsertBundleTag("deopt");
142 assert(DeoptEntry->second == LLVMContext::OB_deopt &&
143 "deopt operand bundle id drifted!");
146 auto *FuncletEntry = pImpl->getOrInsertBundleTag("funclet");
147 assert(FuncletEntry->second == LLVMContext::OB_funclet &&
148 "funclet operand bundle id drifted!");
151 auto *GCTransitionEntry = pImpl->getOrInsertBundleTag("gc-transition");
152 assert(GCTransitionEntry->second == LLVMContext::OB_gc_transition &&
153 "gc-transition operand bundle id drifted!");
154 (void)GCTransitionEntry;
157 LLVMContext::~LLVMContext() { delete pImpl; }
159 void LLVMContext::addModule(Module *M) {
160 pImpl->OwnedModules.insert(M);
163 void LLVMContext::removeModule(Module *M) {
164 pImpl->OwnedModules.erase(M);
167 //===----------------------------------------------------------------------===//
168 // Recoverable Backend Errors
169 //===----------------------------------------------------------------------===//
172 setInlineAsmDiagnosticHandler(InlineAsmDiagHandlerTy DiagHandler,
174 pImpl->InlineAsmDiagHandler = DiagHandler;
175 pImpl->InlineAsmDiagContext = DiagContext;
178 /// getInlineAsmDiagnosticHandler - Return the diagnostic handler set by
179 /// setInlineAsmDiagnosticHandler.
180 LLVMContext::InlineAsmDiagHandlerTy
181 LLVMContext::getInlineAsmDiagnosticHandler() const {
182 return pImpl->InlineAsmDiagHandler;
185 /// getInlineAsmDiagnosticContext - Return the diagnostic context set by
186 /// setInlineAsmDiagnosticHandler.
187 void *LLVMContext::getInlineAsmDiagnosticContext() const {
188 return pImpl->InlineAsmDiagContext;
191 void LLVMContext::setDiagnosticHandler(DiagnosticHandlerTy DiagnosticHandler,
192 void *DiagnosticContext,
193 bool RespectFilters) {
194 pImpl->DiagnosticHandler = DiagnosticHandler;
195 pImpl->DiagnosticContext = DiagnosticContext;
196 pImpl->RespectDiagnosticFilters = RespectFilters;
199 void LLVMContext::setDiagnosticHotnessRequested(bool Requested) {
200 pImpl->DiagnosticHotnessRequested = Requested;
202 bool LLVMContext::getDiagnosticHotnessRequested() const {
203 return pImpl->DiagnosticHotnessRequested;
206 LLVMContext::DiagnosticHandlerTy LLVMContext::getDiagnosticHandler() const {
207 return pImpl->DiagnosticHandler;
210 void *LLVMContext::getDiagnosticContext() const {
211 return pImpl->DiagnosticContext;
214 void LLVMContext::setYieldCallback(YieldCallbackTy Callback, void *OpaqueHandle)
216 pImpl->YieldCallback = Callback;
217 pImpl->YieldOpaqueHandle = OpaqueHandle;
220 void LLVMContext::yield() {
221 if (pImpl->YieldCallback)
222 pImpl->YieldCallback(this, pImpl->YieldOpaqueHandle);
225 void LLVMContext::emitError(const Twine &ErrorStr) {
226 diagnose(DiagnosticInfoInlineAsm(ErrorStr));
229 void LLVMContext::emitError(const Instruction *I, const Twine &ErrorStr) {
230 assert (I && "Invalid instruction");
231 diagnose(DiagnosticInfoInlineAsm(*I, ErrorStr));
234 static bool isDiagnosticEnabled(const DiagnosticInfo &DI) {
235 // Optimization remarks are selective. They need to check whether the regexp
236 // pattern, passed via one of the -pass-remarks* flags, matches the name of
237 // the pass that is emitting the diagnostic. If there is no match, ignore the
238 // diagnostic and return.
239 if (auto *Remark = dyn_cast<DiagnosticInfoOptimizationBase>(&DI))
240 return Remark->isEnabled();
246 LLVMContext::getDiagnosticMessagePrefix(DiagnosticSeverity Severity) {
257 llvm_unreachable("Unknown DiagnosticSeverity");
260 void LLVMContext::diagnose(const DiagnosticInfo &DI) {
261 // If there is a report handler, use it.
262 if (pImpl->DiagnosticHandler) {
263 if (!pImpl->RespectDiagnosticFilters || isDiagnosticEnabled(DI))
264 pImpl->DiagnosticHandler(DI, pImpl->DiagnosticContext);
268 if (!isDiagnosticEnabled(DI))
271 // Otherwise, print the message with a prefix based on the severity.
272 DiagnosticPrinterRawOStream DP(errs());
273 errs() << getDiagnosticMessagePrefix(DI.getSeverity()) << ": ";
276 if (DI.getSeverity() == DS_Error)
280 void LLVMContext::emitError(unsigned LocCookie, const Twine &ErrorStr) {
281 diagnose(DiagnosticInfoInlineAsm(LocCookie, ErrorStr));
284 //===----------------------------------------------------------------------===//
285 // Metadata Kind Uniquing
286 //===----------------------------------------------------------------------===//
288 /// Return a unique non-zero ID for the specified metadata kind.
289 unsigned LLVMContext::getMDKindID(StringRef Name) const {
290 // If this is new, assign it its ID.
291 return pImpl->CustomMDKindNames.insert(
293 Name, pImpl->CustomMDKindNames.size()))
297 /// getHandlerNames - Populate client-supplied smallvector using custom
298 /// metadata name and ID.
299 void LLVMContext::getMDKindNames(SmallVectorImpl<StringRef> &Names) const {
300 Names.resize(pImpl->CustomMDKindNames.size());
301 for (StringMap<unsigned>::const_iterator I = pImpl->CustomMDKindNames.begin(),
302 E = pImpl->CustomMDKindNames.end(); I != E; ++I)
303 Names[I->second] = I->first();
306 void LLVMContext::getOperandBundleTags(SmallVectorImpl<StringRef> &Tags) const {
307 pImpl->getOperandBundleTags(Tags);
310 uint32_t LLVMContext::getOperandBundleTagID(StringRef Tag) const {
311 return pImpl->getOperandBundleTagID(Tag);
314 void LLVMContext::setGC(const Function &Fn, std::string GCName) {
315 auto It = pImpl->GCNames.find(&Fn);
317 if (It == pImpl->GCNames.end()) {
318 pImpl->GCNames.insert(std::make_pair(&Fn, std::move(GCName)));
321 It->second = std::move(GCName);
324 const std::string &LLVMContext::getGC(const Function &Fn) {
325 return pImpl->GCNames[&Fn];
328 void LLVMContext::deleteGC(const Function &Fn) {
329 pImpl->GCNames.erase(&Fn);
332 bool LLVMContext::shouldDiscardValueNames() const {
333 return pImpl->DiscardValueNames;
336 bool LLVMContext::isODRUniquingDebugTypes() const { return !!pImpl->DITypeMap; }
338 void LLVMContext::enableDebugTypeODRUniquing() {
339 if (pImpl->DITypeMap)
342 pImpl->DITypeMap.emplace();
345 void LLVMContext::disableDebugTypeODRUniquing() { pImpl->DITypeMap.reset(); }
347 void LLVMContext::setDiscardValueNames(bool Discard) {
348 pImpl->DiscardValueNames = Discard;
351 OptBisect &LLVMContext::getOptBisect() {
352 return pImpl->getOptBisect();