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/ADT/SmallVector.h"
18 #include "llvm/ADT/StringMap.h"
19 #include "llvm/ADT/StringRef.h"
20 #include "llvm/ADT/Twine.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"},
61 {MD_associated, "associated"},
64 for (auto &MDKind : MDKinds) {
65 unsigned ID = getMDKindID(MDKind.second);
66 assert(ID == MDKind.first && "metadata kind id drifted");
70 auto *DeoptEntry = pImpl->getOrInsertBundleTag("deopt");
71 assert(DeoptEntry->second == LLVMContext::OB_deopt &&
72 "deopt operand bundle id drifted!");
75 auto *FuncletEntry = pImpl->getOrInsertBundleTag("funclet");
76 assert(FuncletEntry->second == LLVMContext::OB_funclet &&
77 "funclet operand bundle id drifted!");
80 auto *GCTransitionEntry = pImpl->getOrInsertBundleTag("gc-transition");
81 assert(GCTransitionEntry->second == LLVMContext::OB_gc_transition &&
82 "gc-transition operand bundle id drifted!");
83 (void)GCTransitionEntry;
86 LLVMContext::~LLVMContext() { delete pImpl; }
88 void LLVMContext::addModule(Module *M) {
89 pImpl->OwnedModules.insert(M);
92 void LLVMContext::removeModule(Module *M) {
93 pImpl->OwnedModules.erase(M);
96 //===----------------------------------------------------------------------===//
97 // Recoverable Backend Errors
98 //===----------------------------------------------------------------------===//
101 setInlineAsmDiagnosticHandler(InlineAsmDiagHandlerTy DiagHandler,
103 pImpl->InlineAsmDiagHandler = DiagHandler;
104 pImpl->InlineAsmDiagContext = DiagContext;
107 /// getInlineAsmDiagnosticHandler - Return the diagnostic handler set by
108 /// setInlineAsmDiagnosticHandler.
109 LLVMContext::InlineAsmDiagHandlerTy
110 LLVMContext::getInlineAsmDiagnosticHandler() const {
111 return pImpl->InlineAsmDiagHandler;
114 /// getInlineAsmDiagnosticContext - Return the diagnostic context set by
115 /// setInlineAsmDiagnosticHandler.
116 void *LLVMContext::getInlineAsmDiagnosticContext() const {
117 return pImpl->InlineAsmDiagContext;
120 void LLVMContext::setDiagnosticHandler(DiagnosticHandlerTy DiagnosticHandler,
121 void *DiagnosticContext,
122 bool RespectFilters) {
123 pImpl->DiagnosticHandler = DiagnosticHandler;
124 pImpl->DiagnosticContext = DiagnosticContext;
125 pImpl->RespectDiagnosticFilters = RespectFilters;
128 void LLVMContext::setDiagnosticHotnessRequested(bool Requested) {
129 pImpl->DiagnosticHotnessRequested = Requested;
131 bool LLVMContext::getDiagnosticHotnessRequested() const {
132 return pImpl->DiagnosticHotnessRequested;
135 yaml::Output *LLVMContext::getDiagnosticsOutputFile() {
136 return pImpl->DiagnosticsOutputFile.get();
139 void LLVMContext::setDiagnosticsOutputFile(std::unique_ptr<yaml::Output> F) {
140 pImpl->DiagnosticsOutputFile = std::move(F);
143 LLVMContext::DiagnosticHandlerTy LLVMContext::getDiagnosticHandler() const {
144 return pImpl->DiagnosticHandler;
147 void *LLVMContext::getDiagnosticContext() const {
148 return pImpl->DiagnosticContext;
151 void LLVMContext::setYieldCallback(YieldCallbackTy Callback, void *OpaqueHandle)
153 pImpl->YieldCallback = Callback;
154 pImpl->YieldOpaqueHandle = OpaqueHandle;
157 void LLVMContext::yield() {
158 if (pImpl->YieldCallback)
159 pImpl->YieldCallback(this, pImpl->YieldOpaqueHandle);
162 void LLVMContext::emitError(const Twine &ErrorStr) {
163 diagnose(DiagnosticInfoInlineAsm(ErrorStr));
166 void LLVMContext::emitError(const Instruction *I, const Twine &ErrorStr) {
167 assert (I && "Invalid instruction");
168 diagnose(DiagnosticInfoInlineAsm(*I, ErrorStr));
171 static bool isDiagnosticEnabled(const DiagnosticInfo &DI) {
172 // Optimization remarks are selective. They need to check whether the regexp
173 // pattern, passed via one of the -pass-remarks* flags, matches the name of
174 // the pass that is emitting the diagnostic. If there is no match, ignore the
175 // diagnostic and return.
176 if (auto *Remark = dyn_cast<DiagnosticInfoOptimizationBase>(&DI))
177 return Remark->isEnabled();
183 LLVMContext::getDiagnosticMessagePrefix(DiagnosticSeverity Severity) {
194 llvm_unreachable("Unknown DiagnosticSeverity");
197 void LLVMContext::diagnose(const DiagnosticInfo &DI) {
198 // If there is a report handler, use it.
199 if (pImpl->DiagnosticHandler) {
200 if (!pImpl->RespectDiagnosticFilters || isDiagnosticEnabled(DI))
201 pImpl->DiagnosticHandler(DI, pImpl->DiagnosticContext);
205 if (!isDiagnosticEnabled(DI))
208 // Otherwise, print the message with a prefix based on the severity.
209 DiagnosticPrinterRawOStream DP(errs());
210 errs() << getDiagnosticMessagePrefix(DI.getSeverity()) << ": ";
213 if (DI.getSeverity() == DS_Error)
217 void LLVMContext::emitError(unsigned LocCookie, const Twine &ErrorStr) {
218 diagnose(DiagnosticInfoInlineAsm(LocCookie, ErrorStr));
221 //===----------------------------------------------------------------------===//
222 // Metadata Kind Uniquing
223 //===----------------------------------------------------------------------===//
225 /// Return a unique non-zero ID for the specified metadata kind.
226 unsigned LLVMContext::getMDKindID(StringRef Name) const {
227 // If this is new, assign it its ID.
228 return pImpl->CustomMDKindNames.insert(
230 Name, pImpl->CustomMDKindNames.size()))
234 /// getHandlerNames - Populate client-supplied smallvector using custom
235 /// metadata name and ID.
236 void LLVMContext::getMDKindNames(SmallVectorImpl<StringRef> &Names) const {
237 Names.resize(pImpl->CustomMDKindNames.size());
238 for (StringMap<unsigned>::const_iterator I = pImpl->CustomMDKindNames.begin(),
239 E = pImpl->CustomMDKindNames.end(); I != E; ++I)
240 Names[I->second] = I->first();
243 void LLVMContext::getOperandBundleTags(SmallVectorImpl<StringRef> &Tags) const {
244 pImpl->getOperandBundleTags(Tags);
247 uint32_t LLVMContext::getOperandBundleTagID(StringRef Tag) const {
248 return pImpl->getOperandBundleTagID(Tag);
251 void LLVMContext::setGC(const Function &Fn, std::string GCName) {
252 auto It = pImpl->GCNames.find(&Fn);
254 if (It == pImpl->GCNames.end()) {
255 pImpl->GCNames.insert(std::make_pair(&Fn, std::move(GCName)));
258 It->second = std::move(GCName);
261 const std::string &LLVMContext::getGC(const Function &Fn) {
262 return pImpl->GCNames[&Fn];
265 void LLVMContext::deleteGC(const Function &Fn) {
266 pImpl->GCNames.erase(&Fn);
269 bool LLVMContext::shouldDiscardValueNames() const {
270 return pImpl->DiscardValueNames;
273 bool LLVMContext::isODRUniquingDebugTypes() const { return !!pImpl->DITypeMap; }
275 void LLVMContext::enableDebugTypeODRUniquing() {
276 if (pImpl->DITypeMap)
279 pImpl->DITypeMap.emplace();
282 void LLVMContext::disableDebugTypeODRUniquing() { pImpl->DITypeMap.reset(); }
284 void LLVMContext::setDiscardValueNames(bool Discard) {
285 pImpl->DiscardValueNames = Discard;
288 OptBisect &LLVMContext::getOptBisect() {
289 return pImpl->getOptBisect();