1ef860a24SChandler Carruth //===-- LLVMContext.cpp - Implement LLVMContext ---------------------------===//
2ef860a24SChandler Carruth //
3ef860a24SChandler Carruth //                     The LLVM Compiler Infrastructure
4ef860a24SChandler Carruth //
5ef860a24SChandler Carruth // This file is distributed under the University of Illinois Open Source
6ef860a24SChandler Carruth // License. See LICENSE.TXT for details.
7ef860a24SChandler Carruth //
8ef860a24SChandler Carruth //===----------------------------------------------------------------------===//
9ef860a24SChandler Carruth //
10ef860a24SChandler Carruth //  This file implements LLVMContext, as a wrapper around the opaque
11ef860a24SChandler Carruth //  class LLVMContextImpl.
12ef860a24SChandler Carruth //
13ef860a24SChandler Carruth //===----------------------------------------------------------------------===//
14ef860a24SChandler Carruth 
159fb823bbSChandler Carruth #include "llvm/IR/LLVMContext.h"
166bda14b3SChandler Carruth #include "LLVMContextImpl.h"
175354a8aaSEugene Zelenko #include "llvm/ADT/SmallVector.h"
185354a8aaSEugene Zelenko #include "llvm/ADT/StringMap.h"
195354a8aaSEugene Zelenko #include "llvm/ADT/StringRef.h"
205354a8aaSEugene Zelenko #include "llvm/ADT/Twine.h"
21b4c44d23SQuentin Colombet #include "llvm/IR/DiagnosticInfo.h"
22b4c44d23SQuentin Colombet #include "llvm/IR/DiagnosticPrinter.h"
239fb823bbSChandler Carruth #include "llvm/IR/Metadata.h"
245354a8aaSEugene Zelenko #include "llvm/IR/Module.h"
255354a8aaSEugene Zelenko #include "llvm/Support/Casting.h"
265354a8aaSEugene Zelenko #include "llvm/Support/ErrorHandling.h"
275354a8aaSEugene Zelenko #include "llvm/Support/raw_ostream.h"
285354a8aaSEugene Zelenko #include <cassert>
295354a8aaSEugene Zelenko #include <cstdlib>
305354a8aaSEugene Zelenko #include <string>
315354a8aaSEugene Zelenko #include <utility>
325354a8aaSEugene Zelenko 
33ef860a24SChandler Carruth using namespace llvm;
34ef860a24SChandler Carruth 
35ef860a24SChandler Carruth LLVMContext::LLVMContext() : pImpl(new LLVMContextImpl(*this)) {
36ef860a24SChandler Carruth   // Create the fixed metadata kinds. This is done in the same order as the
37ef860a24SChandler Carruth   // MD_* enum values so that they correspond.
386f0b4f2eSPeter Collingbourne   std::pair<unsigned, StringRef> MDKinds[] = {
396f0b4f2eSPeter Collingbourne     {MD_dbg, "dbg"},
406f0b4f2eSPeter Collingbourne     {MD_tbaa, "tbaa"},
416f0b4f2eSPeter Collingbourne     {MD_prof, "prof"},
426f0b4f2eSPeter Collingbourne     {MD_fpmath, "fpmath"},
436f0b4f2eSPeter Collingbourne     {MD_range, "range"},
446f0b4f2eSPeter Collingbourne     {MD_tbaa_struct, "tbaa.struct"},
456f0b4f2eSPeter Collingbourne     {MD_invariant_load, "invariant.load"},
466f0b4f2eSPeter Collingbourne     {MD_alias_scope, "alias.scope"},
476f0b4f2eSPeter Collingbourne     {MD_noalias, "noalias"},
486f0b4f2eSPeter Collingbourne     {MD_nontemporal, "nontemporal"},
496f0b4f2eSPeter Collingbourne     {MD_mem_parallel_loop_access, "llvm.mem.parallel_loop_access"},
506f0b4f2eSPeter Collingbourne     {MD_nonnull, "nonnull"},
516f0b4f2eSPeter Collingbourne     {MD_dereferenceable, "dereferenceable"},
526f0b4f2eSPeter Collingbourne     {MD_dereferenceable_or_null, "dereferenceable_or_null"},
536f0b4f2eSPeter Collingbourne     {MD_make_implicit, "make.implicit"},
546f0b4f2eSPeter Collingbourne     {MD_unpredictable, "unpredictable"},
556f0b4f2eSPeter Collingbourne     {MD_invariant_group, "invariant.group"},
566f0b4f2eSPeter Collingbourne     {MD_align, "align"},
576f0b4f2eSPeter Collingbourne     {MD_loop, "llvm.loop"},
586f0b4f2eSPeter Collingbourne     {MD_type, "type"},
596f0b4f2eSPeter Collingbourne     {MD_section_prefix, "section_prefix"},
60235c275bSPeter Collingbourne     {MD_absolute_symbol, "absolute_symbol"},
6151c962f7SEvgeniy Stepanov     {MD_associated, "associated"},
626f0b4f2eSPeter Collingbourne   };
63ef860a24SChandler Carruth 
646f0b4f2eSPeter Collingbourne   for (auto &MDKind : MDKinds) {
656f0b4f2eSPeter Collingbourne     unsigned ID = getMDKindID(MDKind.second);
666f0b4f2eSPeter Collingbourne     assert(ID == MDKind.first && "metadata kind id drifted");
676f0b4f2eSPeter Collingbourne     (void)ID;
686f0b4f2eSPeter Collingbourne   }
69302b69c9SDehao Chen 
70cdafd849SSanjoy Das   auto *DeoptEntry = pImpl->getOrInsertBundleTag("deopt");
71cdafd849SSanjoy Das   assert(DeoptEntry->second == LLVMContext::OB_deopt &&
72cdafd849SSanjoy Das          "deopt operand bundle id drifted!");
73cdafd849SSanjoy Das   (void)DeoptEntry;
743bb88c02SDavid Majnemer 
753bb88c02SDavid Majnemer   auto *FuncletEntry = pImpl->getOrInsertBundleTag("funclet");
763bb88c02SDavid Majnemer   assert(FuncletEntry->second == LLVMContext::OB_funclet &&
773bb88c02SDavid Majnemer          "funclet operand bundle id drifted!");
783bb88c02SDavid Majnemer   (void)FuncletEntry;
79a34ce95bSSanjoy Das 
80a34ce95bSSanjoy Das   auto *GCTransitionEntry = pImpl->getOrInsertBundleTag("gc-transition");
81a34ce95bSSanjoy Das   assert(GCTransitionEntry->second == LLVMContext::OB_gc_transition &&
82a34ce95bSSanjoy Das          "gc-transition operand bundle id drifted!");
83a34ce95bSSanjoy Das   (void)GCTransitionEntry;
84bb80d3e1SKonstantin Zhuravlyov 
85bb80d3e1SKonstantin Zhuravlyov   SyncScope::ID SingleThreadSSID =
86bb80d3e1SKonstantin Zhuravlyov       pImpl->getOrInsertSyncScopeID("singlethread");
87bb80d3e1SKonstantin Zhuravlyov   assert(SingleThreadSSID == SyncScope::SingleThread &&
88bb80d3e1SKonstantin Zhuravlyov          "singlethread synchronization scope ID drifted!");
89*878fdee0SKonstantin Zhuravlyov   (void)SingleThreadSSID;
90bb80d3e1SKonstantin Zhuravlyov 
91bb80d3e1SKonstantin Zhuravlyov   SyncScope::ID SystemSSID =
92bb80d3e1SKonstantin Zhuravlyov       pImpl->getOrInsertSyncScopeID("");
93bb80d3e1SKonstantin Zhuravlyov   assert(SystemSSID == SyncScope::System &&
94bb80d3e1SKonstantin Zhuravlyov          "system synchronization scope ID drifted!");
95*878fdee0SKonstantin Zhuravlyov   (void)SystemSSID;
96ef860a24SChandler Carruth }
975354a8aaSEugene Zelenko 
98ef860a24SChandler Carruth LLVMContext::~LLVMContext() { delete pImpl; }
99ef860a24SChandler Carruth 
100ef860a24SChandler Carruth void LLVMContext::addModule(Module *M) {
101ef860a24SChandler Carruth   pImpl->OwnedModules.insert(M);
102ef860a24SChandler Carruth }
103ef860a24SChandler Carruth 
104ef860a24SChandler Carruth void LLVMContext::removeModule(Module *M) {
105ef860a24SChandler Carruth   pImpl->OwnedModules.erase(M);
106ef860a24SChandler Carruth }
107ef860a24SChandler Carruth 
108ef860a24SChandler Carruth //===----------------------------------------------------------------------===//
109ef860a24SChandler Carruth // Recoverable Backend Errors
110ef860a24SChandler Carruth //===----------------------------------------------------------------------===//
111ef860a24SChandler Carruth 
112a594fab4SBob Wilson void LLVMContext::
113a594fab4SBob Wilson setInlineAsmDiagnosticHandler(InlineAsmDiagHandlerTy DiagHandler,
114ef860a24SChandler Carruth                               void *DiagContext) {
115a594fab4SBob Wilson   pImpl->InlineAsmDiagHandler = DiagHandler;
116a594fab4SBob Wilson   pImpl->InlineAsmDiagContext = DiagContext;
117ef860a24SChandler Carruth }
118ef860a24SChandler Carruth 
119a594fab4SBob Wilson /// getInlineAsmDiagnosticHandler - Return the diagnostic handler set by
120a594fab4SBob Wilson /// setInlineAsmDiagnosticHandler.
121a594fab4SBob Wilson LLVMContext::InlineAsmDiagHandlerTy
122a594fab4SBob Wilson LLVMContext::getInlineAsmDiagnosticHandler() const {
123a594fab4SBob Wilson   return pImpl->InlineAsmDiagHandler;
124ef860a24SChandler Carruth }
125ef860a24SChandler Carruth 
126a594fab4SBob Wilson /// getInlineAsmDiagnosticContext - Return the diagnostic context set by
127a594fab4SBob Wilson /// setInlineAsmDiagnosticHandler.
128a594fab4SBob Wilson void *LLVMContext::getInlineAsmDiagnosticContext() const {
129a594fab4SBob Wilson   return pImpl->InlineAsmDiagContext;
130ef860a24SChandler Carruth }
131ef860a24SChandler Carruth 
132b4c44d23SQuentin Colombet void LLVMContext::setDiagnosticHandler(DiagnosticHandlerTy DiagnosticHandler,
13330c9242cSDuncan P. N. Exon Smith                                        void *DiagnosticContext,
13430c9242cSDuncan P. N. Exon Smith                                        bool RespectFilters) {
135b4c44d23SQuentin Colombet   pImpl->DiagnosticHandler = DiagnosticHandler;
136b4c44d23SQuentin Colombet   pImpl->DiagnosticContext = DiagnosticContext;
13730c9242cSDuncan P. N. Exon Smith   pImpl->RespectDiagnosticFilters = RespectFilters;
138b4c44d23SQuentin Colombet }
139b4c44d23SQuentin Colombet 
14044e5f6c4SBrian Gesiak void LLVMContext::setDiagnosticsHotnessRequested(bool Requested) {
14144e5f6c4SBrian Gesiak   pImpl->DiagnosticsHotnessRequested = Requested;
14244e5f6c4SBrian Gesiak }
14344e5f6c4SBrian Gesiak bool LLVMContext::getDiagnosticsHotnessRequested() const {
14444e5f6c4SBrian Gesiak   return pImpl->DiagnosticsHotnessRequested;
145aad81608SAdam Nemet }
146aad81608SAdam Nemet 
1474ef3daafSBrian Gesiak void LLVMContext::setDiagnosticsHotnessThreshold(uint64_t Threshold) {
1484ef3daafSBrian Gesiak   pImpl->DiagnosticsHotnessThreshold = Threshold;
1494ef3daafSBrian Gesiak }
1504ef3daafSBrian Gesiak uint64_t LLVMContext::getDiagnosticsHotnessThreshold() const {
1514ef3daafSBrian Gesiak   return pImpl->DiagnosticsHotnessThreshold;
1524ef3daafSBrian Gesiak }
1534ef3daafSBrian Gesiak 
154a62b7e1aSAdam Nemet yaml::Output *LLVMContext::getDiagnosticsOutputFile() {
155a62b7e1aSAdam Nemet   return pImpl->DiagnosticsOutputFile.get();
156a62b7e1aSAdam Nemet }
157a62b7e1aSAdam Nemet 
1586f408368SMehdi Amini void LLVMContext::setDiagnosticsOutputFile(std::unique_ptr<yaml::Output> F) {
1596f408368SMehdi Amini   pImpl->DiagnosticsOutputFile = std::move(F);
160a62b7e1aSAdam Nemet }
161a62b7e1aSAdam Nemet 
162b4c44d23SQuentin Colombet LLVMContext::DiagnosticHandlerTy LLVMContext::getDiagnosticHandler() const {
163b4c44d23SQuentin Colombet   return pImpl->DiagnosticHandler;
164b4c44d23SQuentin Colombet }
165b4c44d23SQuentin Colombet 
166b4c44d23SQuentin Colombet void *LLVMContext::getDiagnosticContext() const {
167b4c44d23SQuentin Colombet   return pImpl->DiagnosticContext;
168b4c44d23SQuentin Colombet }
169b4c44d23SQuentin Colombet 
17034390c70SJuergen Ributzka void LLVMContext::setYieldCallback(YieldCallbackTy Callback, void *OpaqueHandle)
17134390c70SJuergen Ributzka {
17234390c70SJuergen Ributzka   pImpl->YieldCallback = Callback;
17334390c70SJuergen Ributzka   pImpl->YieldOpaqueHandle = OpaqueHandle;
17434390c70SJuergen Ributzka }
17534390c70SJuergen Ributzka 
17634390c70SJuergen Ributzka void LLVMContext::yield() {
17734390c70SJuergen Ributzka   if (pImpl->YieldCallback)
17834390c70SJuergen Ributzka     pImpl->YieldCallback(this, pImpl->YieldOpaqueHandle);
17934390c70SJuergen Ributzka }
18034390c70SJuergen Ributzka 
181ef860a24SChandler Carruth void LLVMContext::emitError(const Twine &ErrorStr) {
182dec8d55bSQuentin Colombet   diagnose(DiagnosticInfoInlineAsm(ErrorStr));
183ef860a24SChandler Carruth }
184ef860a24SChandler Carruth 
185bfb44ef9SBob Wilson void LLVMContext::emitError(const Instruction *I, const Twine &ErrorStr) {
186dec8d55bSQuentin Colombet   assert (I && "Invalid instruction");
187dec8d55bSQuentin Colombet   diagnose(DiagnosticInfoInlineAsm(*I, ErrorStr));
188ef860a24SChandler Carruth }
189ef860a24SChandler Carruth 
19030c9242cSDuncan P. N. Exon Smith static bool isDiagnosticEnabled(const DiagnosticInfo &DI) {
1917f8af8bfSDiego Novillo   // Optimization remarks are selective. They need to check whether the regexp
1927f8af8bfSDiego Novillo   // pattern, passed via one of the -pass-remarks* flags, matches the name of
1937f8af8bfSDiego Novillo   // the pass that is emitting the diagnostic. If there is no match, ignore the
1947f8af8bfSDiego Novillo   // diagnostic and return.
195e9148dd6SAkira Hatanaka   if (auto *Remark = dyn_cast<DiagnosticInfoOptimizationBase>(&DI))
196e9148dd6SAkira Hatanaka     return Remark->isEnabled();
197e9148dd6SAkira Hatanaka 
19830c9242cSDuncan P. N. Exon Smith   return true;
19930c9242cSDuncan P. N. Exon Smith }
20030c9242cSDuncan P. N. Exon Smith 
2014b9c0d4dSRenato Golin const char *
2024b9c0d4dSRenato Golin LLVMContext::getDiagnosticMessagePrefix(DiagnosticSeverity Severity) {
203735c47ecSAlex Lorenz   switch (Severity) {
204735c47ecSAlex Lorenz   case DS_Error:
205735c47ecSAlex Lorenz     return "error";
206735c47ecSAlex Lorenz   case DS_Warning:
207735c47ecSAlex Lorenz     return "warning";
208735c47ecSAlex Lorenz   case DS_Remark:
209735c47ecSAlex Lorenz     return "remark";
210735c47ecSAlex Lorenz   case DS_Note:
211735c47ecSAlex Lorenz     return "note";
212735c47ecSAlex Lorenz   }
2135220476aSAaron Ballman   llvm_unreachable("Unknown DiagnosticSeverity");
214735c47ecSAlex Lorenz }
215735c47ecSAlex Lorenz 
21630c9242cSDuncan P. N. Exon Smith void LLVMContext::diagnose(const DiagnosticInfo &DI) {
21730c9242cSDuncan P. N. Exon Smith   // If there is a report handler, use it.
21830c9242cSDuncan P. N. Exon Smith   if (pImpl->DiagnosticHandler) {
21930c9242cSDuncan P. N. Exon Smith     if (!pImpl->RespectDiagnosticFilters || isDiagnosticEnabled(DI))
22030c9242cSDuncan P. N. Exon Smith       pImpl->DiagnosticHandler(DI, pImpl->DiagnosticContext);
22130c9242cSDuncan P. N. Exon Smith     return;
22230c9242cSDuncan P. N. Exon Smith   }
22330c9242cSDuncan P. N. Exon Smith 
22430c9242cSDuncan P. N. Exon Smith   if (!isDiagnosticEnabled(DI))
22530c9242cSDuncan P. N. Exon Smith     return;
226df655013SDiego Novillo 
227b4c44d23SQuentin Colombet   // Otherwise, print the message with a prefix based on the severity.
228735c47ecSAlex Lorenz   DiagnosticPrinterRawOStream DP(errs());
229735c47ecSAlex Lorenz   errs() << getDiagnosticMessagePrefix(DI.getSeverity()) << ": ";
230b4c44d23SQuentin Colombet   DI.print(DP);
231735c47ecSAlex Lorenz   errs() << "\n";
232735c47ecSAlex Lorenz   if (DI.getSeverity() == DS_Error)
233b4c44d23SQuentin Colombet     exit(1);
234b4c44d23SQuentin Colombet }
235b4c44d23SQuentin Colombet 
236ef860a24SChandler Carruth void LLVMContext::emitError(unsigned LocCookie, const Twine &ErrorStr) {
237dec8d55bSQuentin Colombet   diagnose(DiagnosticInfoInlineAsm(LocCookie, ErrorStr));
238ef860a24SChandler Carruth }
239ef860a24SChandler Carruth 
240ef860a24SChandler Carruth //===----------------------------------------------------------------------===//
241ef860a24SChandler Carruth // Metadata Kind Uniquing
242ef860a24SChandler Carruth //===----------------------------------------------------------------------===//
243ef860a24SChandler Carruth 
2441ac0d2deSFilipe Cabecinhas /// Return a unique non-zero ID for the specified metadata kind.
245ef860a24SChandler Carruth unsigned LLVMContext::getMDKindID(StringRef Name) const {
246ef860a24SChandler Carruth   // If this is new, assign it its ID.
2471ac0d2deSFilipe Cabecinhas   return pImpl->CustomMDKindNames.insert(
2481ac0d2deSFilipe Cabecinhas                                      std::make_pair(
2491ac0d2deSFilipe Cabecinhas                                          Name, pImpl->CustomMDKindNames.size()))
2505106ce78SDavid Blaikie       .first->second;
251ef860a24SChandler Carruth }
252ef860a24SChandler Carruth 
2534104337dSSanjay Patel /// getHandlerNames - Populate client-supplied smallvector using custom
254ef860a24SChandler Carruth /// metadata name and ID.
255ef860a24SChandler Carruth void LLVMContext::getMDKindNames(SmallVectorImpl<StringRef> &Names) const {
256ef860a24SChandler Carruth   Names.resize(pImpl->CustomMDKindNames.size());
257ef860a24SChandler Carruth   for (StringMap<unsigned>::const_iterator I = pImpl->CustomMDKindNames.begin(),
258ef860a24SChandler Carruth        E = pImpl->CustomMDKindNames.end(); I != E; ++I)
259ef860a24SChandler Carruth     Names[I->second] = I->first();
260ef860a24SChandler Carruth }
2619303c246SSanjoy Das 
2629303c246SSanjoy Das void LLVMContext::getOperandBundleTags(SmallVectorImpl<StringRef> &Tags) const {
2639303c246SSanjoy Das   pImpl->getOperandBundleTags(Tags);
2649303c246SSanjoy Das }
2659303c246SSanjoy Das 
2669303c246SSanjoy Das uint32_t LLVMContext::getOperandBundleTagID(StringRef Tag) const {
2679303c246SSanjoy Das   return pImpl->getOperandBundleTagID(Tag);
2689303c246SSanjoy Das }
269599ebf27SMehdi Amini 
270bb80d3e1SKonstantin Zhuravlyov SyncScope::ID LLVMContext::getOrInsertSyncScopeID(StringRef SSN) {
271bb80d3e1SKonstantin Zhuravlyov   return pImpl->getOrInsertSyncScopeID(SSN);
272bb80d3e1SKonstantin Zhuravlyov }
273bb80d3e1SKonstantin Zhuravlyov 
274bb80d3e1SKonstantin Zhuravlyov void LLVMContext::getSyncScopeNames(SmallVectorImpl<StringRef> &SSNs) const {
275bb80d3e1SKonstantin Zhuravlyov   pImpl->getSyncScopeNames(SSNs);
276bb80d3e1SKonstantin Zhuravlyov }
277bb80d3e1SKonstantin Zhuravlyov 
278599ebf27SMehdi Amini void LLVMContext::setGC(const Function &Fn, std::string GCName) {
279599ebf27SMehdi Amini   auto It = pImpl->GCNames.find(&Fn);
280599ebf27SMehdi Amini 
281599ebf27SMehdi Amini   if (It == pImpl->GCNames.end()) {
282599ebf27SMehdi Amini     pImpl->GCNames.insert(std::make_pair(&Fn, std::move(GCName)));
283599ebf27SMehdi Amini     return;
284599ebf27SMehdi Amini   }
285599ebf27SMehdi Amini   It->second = std::move(GCName);
286599ebf27SMehdi Amini }
2875354a8aaSEugene Zelenko 
288599ebf27SMehdi Amini const std::string &LLVMContext::getGC(const Function &Fn) {
289599ebf27SMehdi Amini   return pImpl->GCNames[&Fn];
290599ebf27SMehdi Amini }
2915354a8aaSEugene Zelenko 
292599ebf27SMehdi Amini void LLVMContext::deleteGC(const Function &Fn) {
293599ebf27SMehdi Amini   pImpl->GCNames.erase(&Fn);
294599ebf27SMehdi Amini }
29509b4a8daSMehdi Amini 
296e7090155SMehdi Amini bool LLVMContext::shouldDiscardValueNames() const {
297e7090155SMehdi Amini   return pImpl->DiscardValueNames;
298e7090155SMehdi Amini }
29909b4a8daSMehdi Amini 
300ed8fdb2aSDuncan P. N. Exon Smith bool LLVMContext::isODRUniquingDebugTypes() const { return !!pImpl->DITypeMap; }
3015ab2be09SDuncan P. N. Exon Smith 
302ed8fdb2aSDuncan P. N. Exon Smith void LLVMContext::enableDebugTypeODRUniquing() {
3035ab2be09SDuncan P. N. Exon Smith   if (pImpl->DITypeMap)
3045ab2be09SDuncan P. N. Exon Smith     return;
3055ab2be09SDuncan P. N. Exon Smith 
306e8b555c6SDuncan P. N. Exon Smith   pImpl->DITypeMap.emplace();
3075ab2be09SDuncan P. N. Exon Smith }
3085ab2be09SDuncan P. N. Exon Smith 
309ed8fdb2aSDuncan P. N. Exon Smith void LLVMContext::disableDebugTypeODRUniquing() { pImpl->DITypeMap.reset(); }
3105ab2be09SDuncan P. N. Exon Smith 
31109b4a8daSMehdi Amini void LLVMContext::setDiscardValueNames(bool Discard) {
31209b4a8daSMehdi Amini   pImpl->DiscardValueNames = Discard;
31309b4a8daSMehdi Amini }
314aa641a51SAndrew Kaylor 
315aa641a51SAndrew Kaylor OptBisect &LLVMContext::getOptBisect() {
316aa641a51SAndrew Kaylor   return pImpl->getOptBisect();
317aa641a51SAndrew Kaylor }
318