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.
38 std::pair<unsigned, StringRef> MDKinds[] = {
42 {MD_fpmath, "fpmath"},
44 {MD_tbaa_struct, "tbaa.struct"},
45 {MD_invariant_load, "invariant.load"},
46 {MD_alias_scope, "alias.scope"},
47 {MD_noalias, "noalias"},
48 {MD_nontemporal, "nontemporal"},
49 {MD_mem_parallel_loop_access, "llvm.mem.parallel_loop_access"},
50 {MD_nonnull, "nonnull"},
51 {MD_dereferenceable, "dereferenceable"},
52 {MD_dereferenceable_or_null, "dereferenceable_or_null"},
53 {MD_make_implicit, "make.implicit"},
54 {MD_unpredictable, "unpredictable"},
55 {MD_invariant_group, "invariant.group"},
57 {MD_loop, "llvm.loop"},
59 {MD_section_prefix, "section_prefix"},
60 {MD_absolute_symbol, "absolute_symbol"},
63 for (auto &MDKind : MDKinds) {
64 unsigned ID = getMDKindID(MDKind.second);
65 assert(ID == MDKind.first && "metadata kind id drifted");
69 auto *DeoptEntry = pImpl->getOrInsertBundleTag("deopt");
70 assert(DeoptEntry->second == LLVMContext::OB_deopt &&
71 "deopt operand bundle id drifted!");
74 auto *FuncletEntry = pImpl->getOrInsertBundleTag("funclet");
75 assert(FuncletEntry->second == LLVMContext::OB_funclet &&
76 "funclet operand bundle id drifted!");
79 auto *GCTransitionEntry = pImpl->getOrInsertBundleTag("gc-transition");
80 assert(GCTransitionEntry->second == LLVMContext::OB_gc_transition &&
81 "gc-transition operand bundle id drifted!");
82 (void)GCTransitionEntry;
85 LLVMContext::~LLVMContext() { delete pImpl; }
87 void LLVMContext::addModule(Module *M) {
88 pImpl->OwnedModules.insert(M);
91 void LLVMContext::removeModule(Module *M) {
92 pImpl->OwnedModules.erase(M);
95 //===----------------------------------------------------------------------===//
96 // Recoverable Backend Errors
97 //===----------------------------------------------------------------------===//
100 setInlineAsmDiagnosticHandler(InlineAsmDiagHandlerTy DiagHandler,
102 pImpl->InlineAsmDiagHandler = DiagHandler;
103 pImpl->InlineAsmDiagContext = DiagContext;
106 /// getInlineAsmDiagnosticHandler - Return the diagnostic handler set by
107 /// setInlineAsmDiagnosticHandler.
108 LLVMContext::InlineAsmDiagHandlerTy
109 LLVMContext::getInlineAsmDiagnosticHandler() const {
110 return pImpl->InlineAsmDiagHandler;
113 /// getInlineAsmDiagnosticContext - Return the diagnostic context set by
114 /// setInlineAsmDiagnosticHandler.
115 void *LLVMContext::getInlineAsmDiagnosticContext() const {
116 return pImpl->InlineAsmDiagContext;
119 void LLVMContext::setDiagnosticHandler(DiagnosticHandlerTy DiagnosticHandler,
120 void *DiagnosticContext,
121 bool RespectFilters) {
122 pImpl->DiagnosticHandler = DiagnosticHandler;
123 pImpl->DiagnosticContext = DiagnosticContext;
124 pImpl->RespectDiagnosticFilters = RespectFilters;
127 void LLVMContext::setDiagnosticHotnessRequested(bool Requested) {
128 pImpl->DiagnosticHotnessRequested = Requested;
130 bool LLVMContext::getDiagnosticHotnessRequested() const {
131 return pImpl->DiagnosticHotnessRequested;
134 yaml::Output *LLVMContext::getDiagnosticsOutputFile() {
135 return pImpl->DiagnosticsOutputFile.get();
138 void LLVMContext::setDiagnosticsOutputFile(std::unique_ptr<yaml::Output> F) {
139 pImpl->DiagnosticsOutputFile = std::move(F);
142 LLVMContext::DiagnosticHandlerTy LLVMContext::getDiagnosticHandler() const {
143 return pImpl->DiagnosticHandler;
146 void *LLVMContext::getDiagnosticContext() const {
147 return pImpl->DiagnosticContext;
150 void LLVMContext::setYieldCallback(YieldCallbackTy Callback, void *OpaqueHandle)
152 pImpl->YieldCallback = Callback;
153 pImpl->YieldOpaqueHandle = OpaqueHandle;
156 void LLVMContext::yield() {
157 if (pImpl->YieldCallback)
158 pImpl->YieldCallback(this, pImpl->YieldOpaqueHandle);
161 void LLVMContext::emitError(const Twine &ErrorStr) {
162 diagnose(DiagnosticInfoInlineAsm(ErrorStr));
165 void LLVMContext::emitError(const Instruction *I, const Twine &ErrorStr) {
166 assert (I && "Invalid instruction");
167 diagnose(DiagnosticInfoInlineAsm(*I, ErrorStr));
170 static bool isDiagnosticEnabled(const DiagnosticInfo &DI) {
171 // Optimization remarks are selective. They need to check whether the regexp
172 // pattern, passed via one of the -pass-remarks* flags, matches the name of
173 // the pass that is emitting the diagnostic. If there is no match, ignore the
174 // diagnostic and return.
175 if (auto *Remark = dyn_cast<DiagnosticInfoOptimizationBase>(&DI))
176 return Remark->isEnabled();
182 LLVMContext::getDiagnosticMessagePrefix(DiagnosticSeverity Severity) {
193 llvm_unreachable("Unknown DiagnosticSeverity");
196 void LLVMContext::diagnose(const DiagnosticInfo &DI) {
197 // If there is a report handler, use it.
198 if (pImpl->DiagnosticHandler) {
199 if (!pImpl->RespectDiagnosticFilters || isDiagnosticEnabled(DI))
200 pImpl->DiagnosticHandler(DI, pImpl->DiagnosticContext);
204 if (!isDiagnosticEnabled(DI))
207 // Otherwise, print the message with a prefix based on the severity.
208 DiagnosticPrinterRawOStream DP(errs());
209 errs() << getDiagnosticMessagePrefix(DI.getSeverity()) << ": ";
212 if (DI.getSeverity() == DS_Error)
216 void LLVMContext::emitError(unsigned LocCookie, const Twine &ErrorStr) {
217 diagnose(DiagnosticInfoInlineAsm(LocCookie, ErrorStr));
220 //===----------------------------------------------------------------------===//
221 // Metadata Kind Uniquing
222 //===----------------------------------------------------------------------===//
224 /// Return a unique non-zero ID for the specified metadata kind.
225 unsigned LLVMContext::getMDKindID(StringRef Name) const {
226 // If this is new, assign it its ID.
227 return pImpl->CustomMDKindNames.insert(
229 Name, pImpl->CustomMDKindNames.size()))
233 /// getHandlerNames - Populate client-supplied smallvector using custom
234 /// metadata name and ID.
235 void LLVMContext::getMDKindNames(SmallVectorImpl<StringRef> &Names) const {
236 Names.resize(pImpl->CustomMDKindNames.size());
237 for (StringMap<unsigned>::const_iterator I = pImpl->CustomMDKindNames.begin(),
238 E = pImpl->CustomMDKindNames.end(); I != E; ++I)
239 Names[I->second] = I->first();
242 void LLVMContext::getOperandBundleTags(SmallVectorImpl<StringRef> &Tags) const {
243 pImpl->getOperandBundleTags(Tags);
246 uint32_t LLVMContext::getOperandBundleTagID(StringRef Tag) const {
247 return pImpl->getOperandBundleTagID(Tag);
250 void LLVMContext::setGC(const Function &Fn, std::string GCName) {
251 auto It = pImpl->GCNames.find(&Fn);
253 if (It == pImpl->GCNames.end()) {
254 pImpl->GCNames.insert(std::make_pair(&Fn, std::move(GCName)));
257 It->second = std::move(GCName);
260 const std::string &LLVMContext::getGC(const Function &Fn) {
261 return pImpl->GCNames[&Fn];
264 void LLVMContext::deleteGC(const Function &Fn) {
265 pImpl->GCNames.erase(&Fn);
268 bool LLVMContext::shouldDiscardValueNames() const {
269 return pImpl->DiscardValueNames;
272 bool LLVMContext::isODRUniquingDebugTypes() const { return !!pImpl->DITypeMap; }
274 void LLVMContext::enableDebugTypeODRUniquing() {
275 if (pImpl->DITypeMap)
278 pImpl->DITypeMap.emplace();
281 void LLVMContext::disableDebugTypeODRUniquing() { pImpl->DITypeMap.reset(); }
283 void LLVMContext::setDiscardValueNames(bool Discard) {
284 pImpl->DiscardValueNames = Discard;
287 OptBisect &LLVMContext::getOptBisect() {
288 return pImpl->getOptBisect();