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"},
62 {MD_callees, "callees"},
63 {MD_irr_loop, "irr_loop"},
64 {MD_access_group, "llvm.access.group"},
65 {MD_callback, "callback"},
68 for (auto &MDKind : MDKinds) {
69 unsigned ID = getMDKindID(MDKind.second);
70 assert(ID == MDKind.first && "metadata kind id drifted");
74 auto *DeoptEntry = pImpl->getOrInsertBundleTag("deopt");
75 assert(DeoptEntry->second == LLVMContext::OB_deopt &&
76 "deopt operand bundle id drifted!");
79 auto *FuncletEntry = pImpl->getOrInsertBundleTag("funclet");
80 assert(FuncletEntry->second == LLVMContext::OB_funclet &&
81 "funclet operand bundle id drifted!");
84 auto *GCTransitionEntry = pImpl->getOrInsertBundleTag("gc-transition");
85 assert(GCTransitionEntry->second == LLVMContext::OB_gc_transition &&
86 "gc-transition operand bundle id drifted!");
87 (void)GCTransitionEntry;
89 SyncScope::ID SingleThreadSSID =
90 pImpl->getOrInsertSyncScopeID("singlethread");
91 assert(SingleThreadSSID == SyncScope::SingleThread &&
92 "singlethread synchronization scope ID drifted!");
93 (void)SingleThreadSSID;
95 SyncScope::ID SystemSSID =
96 pImpl->getOrInsertSyncScopeID("");
97 assert(SystemSSID == SyncScope::System &&
98 "system synchronization scope ID drifted!");
102 LLVMContext::~LLVMContext() { delete pImpl; }
104 void LLVMContext::addModule(Module *M) {
105 pImpl->OwnedModules.insert(M);
108 void LLVMContext::removeModule(Module *M) {
109 pImpl->OwnedModules.erase(M);
112 //===----------------------------------------------------------------------===//
113 // Recoverable Backend Errors
114 //===----------------------------------------------------------------------===//
117 setInlineAsmDiagnosticHandler(InlineAsmDiagHandlerTy DiagHandler,
119 pImpl->InlineAsmDiagHandler = DiagHandler;
120 pImpl->InlineAsmDiagContext = DiagContext;
123 /// getInlineAsmDiagnosticHandler - Return the diagnostic handler set by
124 /// setInlineAsmDiagnosticHandler.
125 LLVMContext::InlineAsmDiagHandlerTy
126 LLVMContext::getInlineAsmDiagnosticHandler() const {
127 return pImpl->InlineAsmDiagHandler;
130 /// getInlineAsmDiagnosticContext - Return the diagnostic context set by
131 /// setInlineAsmDiagnosticHandler.
132 void *LLVMContext::getInlineAsmDiagnosticContext() const {
133 return pImpl->InlineAsmDiagContext;
136 void LLVMContext::setDiagnosticHandlerCallBack(
137 DiagnosticHandler::DiagnosticHandlerTy DiagnosticHandler,
138 void *DiagnosticContext, bool RespectFilters) {
139 pImpl->DiagHandler->DiagHandlerCallback = DiagnosticHandler;
140 pImpl->DiagHandler->DiagnosticContext = DiagnosticContext;
141 pImpl->RespectDiagnosticFilters = RespectFilters;
144 void LLVMContext::setDiagnosticHandler(std::unique_ptr<DiagnosticHandler> &&DH,
145 bool RespectFilters) {
146 pImpl->DiagHandler = std::move(DH);
147 pImpl->RespectDiagnosticFilters = RespectFilters;
150 void LLVMContext::setDiagnosticsHotnessRequested(bool Requested) {
151 pImpl->DiagnosticsHotnessRequested = Requested;
153 bool LLVMContext::getDiagnosticsHotnessRequested() const {
154 return pImpl->DiagnosticsHotnessRequested;
157 void LLVMContext::setDiagnosticsHotnessThreshold(uint64_t Threshold) {
158 pImpl->DiagnosticsHotnessThreshold = Threshold;
160 uint64_t LLVMContext::getDiagnosticsHotnessThreshold() const {
161 return pImpl->DiagnosticsHotnessThreshold;
164 yaml::Output *LLVMContext::getDiagnosticsOutputFile() {
165 return pImpl->DiagnosticsOutputFile.get();
168 void LLVMContext::setDiagnosticsOutputFile(std::unique_ptr<yaml::Output> F) {
169 pImpl->DiagnosticsOutputFile = std::move(F);
172 DiagnosticHandler::DiagnosticHandlerTy
173 LLVMContext::getDiagnosticHandlerCallBack() const {
174 return pImpl->DiagHandler->DiagHandlerCallback;
177 void *LLVMContext::getDiagnosticContext() const {
178 return pImpl->DiagHandler->DiagnosticContext;
181 void LLVMContext::setYieldCallback(YieldCallbackTy Callback, void *OpaqueHandle)
183 pImpl->YieldCallback = Callback;
184 pImpl->YieldOpaqueHandle = OpaqueHandle;
187 void LLVMContext::yield() {
188 if (pImpl->YieldCallback)
189 pImpl->YieldCallback(this, pImpl->YieldOpaqueHandle);
192 void LLVMContext::emitError(const Twine &ErrorStr) {
193 diagnose(DiagnosticInfoInlineAsm(ErrorStr));
196 void LLVMContext::emitError(const Instruction *I, const Twine &ErrorStr) {
197 assert (I && "Invalid instruction");
198 diagnose(DiagnosticInfoInlineAsm(*I, ErrorStr));
201 static bool isDiagnosticEnabled(const DiagnosticInfo &DI) {
202 // Optimization remarks are selective. They need to check whether the regexp
203 // pattern, passed via one of the -pass-remarks* flags, matches the name of
204 // the pass that is emitting the diagnostic. If there is no match, ignore the
205 // diagnostic and return.
207 // Also noisy remarks are only enabled if we have hotness information to sort
209 if (auto *Remark = dyn_cast<DiagnosticInfoOptimizationBase>(&DI))
210 return Remark->isEnabled() &&
211 (!Remark->isVerbose() || Remark->getHotness());
217 LLVMContext::getDiagnosticMessagePrefix(DiagnosticSeverity Severity) {
228 llvm_unreachable("Unknown DiagnosticSeverity");
231 void LLVMContext::diagnose(const DiagnosticInfo &DI) {
232 if (auto *OptDiagBase = dyn_cast<DiagnosticInfoOptimizationBase>(&DI)) {
233 yaml::Output *Out = getDiagnosticsOutputFile();
235 // For remarks the << operator takes a reference to a pointer.
236 auto *P = const_cast<DiagnosticInfoOptimizationBase *>(OptDiagBase);
240 // If there is a report handler, use it.
241 if (pImpl->DiagHandler &&
242 (!pImpl->RespectDiagnosticFilters || isDiagnosticEnabled(DI)) &&
243 pImpl->DiagHandler->handleDiagnostics(DI))
246 if (!isDiagnosticEnabled(DI))
249 // Otherwise, print the message with a prefix based on the severity.
250 DiagnosticPrinterRawOStream DP(errs());
251 errs() << getDiagnosticMessagePrefix(DI.getSeverity()) << ": ";
254 if (DI.getSeverity() == DS_Error)
258 void LLVMContext::emitError(unsigned LocCookie, const Twine &ErrorStr) {
259 diagnose(DiagnosticInfoInlineAsm(LocCookie, ErrorStr));
262 //===----------------------------------------------------------------------===//
263 // Metadata Kind Uniquing
264 //===----------------------------------------------------------------------===//
266 /// Return a unique non-zero ID for the specified metadata kind.
267 unsigned LLVMContext::getMDKindID(StringRef Name) const {
268 // If this is new, assign it its ID.
269 return pImpl->CustomMDKindNames.insert(
271 Name, pImpl->CustomMDKindNames.size()))
275 /// getHandlerNames - Populate client-supplied smallvector using custom
276 /// metadata name and ID.
277 void LLVMContext::getMDKindNames(SmallVectorImpl<StringRef> &Names) const {
278 Names.resize(pImpl->CustomMDKindNames.size());
279 for (StringMap<unsigned>::const_iterator I = pImpl->CustomMDKindNames.begin(),
280 E = pImpl->CustomMDKindNames.end(); I != E; ++I)
281 Names[I->second] = I->first();
284 void LLVMContext::getOperandBundleTags(SmallVectorImpl<StringRef> &Tags) const {
285 pImpl->getOperandBundleTags(Tags);
288 uint32_t LLVMContext::getOperandBundleTagID(StringRef Tag) const {
289 return pImpl->getOperandBundleTagID(Tag);
292 SyncScope::ID LLVMContext::getOrInsertSyncScopeID(StringRef SSN) {
293 return pImpl->getOrInsertSyncScopeID(SSN);
296 void LLVMContext::getSyncScopeNames(SmallVectorImpl<StringRef> &SSNs) const {
297 pImpl->getSyncScopeNames(SSNs);
300 void LLVMContext::setGC(const Function &Fn, std::string GCName) {
301 auto It = pImpl->GCNames.find(&Fn);
303 if (It == pImpl->GCNames.end()) {
304 pImpl->GCNames.insert(std::make_pair(&Fn, std::move(GCName)));
307 It->second = std::move(GCName);
310 const std::string &LLVMContext::getGC(const Function &Fn) {
311 return pImpl->GCNames[&Fn];
314 void LLVMContext::deleteGC(const Function &Fn) {
315 pImpl->GCNames.erase(&Fn);
318 bool LLVMContext::shouldDiscardValueNames() const {
319 return pImpl->DiscardValueNames;
322 bool LLVMContext::isODRUniquingDebugTypes() const { return !!pImpl->DITypeMap; }
324 void LLVMContext::enableDebugTypeODRUniquing() {
325 if (pImpl->DITypeMap)
328 pImpl->DITypeMap.emplace();
331 void LLVMContext::disableDebugTypeODRUniquing() { pImpl->DITypeMap.reset(); }
333 void LLVMContext::setDiscardValueNames(bool Discard) {
334 pImpl->DiscardValueNames = Discard;
337 OptPassGate &LLVMContext::getOptPassGate() const {
338 return pImpl->getOptPassGate();
341 void LLVMContext::setOptPassGate(OptPassGate& OPG) {
342 pImpl->setOptPassGate(OPG);
345 const DiagnosticHandler *LLVMContext::getDiagHandlerPtr() const {
346 return pImpl->DiagHandler.get();
349 std::unique_ptr<DiagnosticHandler> LLVMContext::getDiagnosticHandler() {
350 return std::move(pImpl->DiagHandler);