blob: 944d8265151df8e871687379fdb8acb4fab5fadb [file] [log] [blame]
Nick Lewyckydbf50812012-12-26 22:00:49 +00001//===-- LLVMContext.cpp - Implement LLVMContext ---------------------------===//
Owen Anderson2bc29dc2009-06-30 00:48:55 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
Owen Anderson52170072009-06-30 17:06:46 +00009//
10// This file implements LLVMContext, as a wrapper around the opaque
Benjamin Kramer12ddd402009-08-11 17:45:13 +000011// class LLVMContextImpl.
Owen Anderson52170072009-06-30 17:06:46 +000012//
13//===----------------------------------------------------------------------===//
Owen Anderson2bc29dc2009-06-30 00:48:55 +000014
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000015#include "llvm/IR/LLVMContext.h"
Chandler Carruthe3e43d92017-06-06 11:49:48 +000016#include "LLVMContextImpl.h"
Eugene Zelenkoaaf3f042016-04-28 18:04:41 +000017#include "llvm/ADT/SmallVector.h"
18#include "llvm/ADT/StringMap.h"
19#include "llvm/ADT/StringRef.h"
20#include "llvm/ADT/Twine.h"
Quentin Colombetde262fe2013-12-17 17:47:22 +000021#include "llvm/IR/DiagnosticInfo.h"
22#include "llvm/IR/DiagnosticPrinter.h"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000023#include "llvm/IR/Metadata.h"
Eugene Zelenkoaaf3f042016-04-28 18:04:41 +000024#include "llvm/IR/Module.h"
25#include "llvm/Support/Casting.h"
26#include "llvm/Support/ErrorHandling.h"
27#include "llvm/Support/raw_ostream.h"
28#include <cassert>
29#include <cstdlib>
30#include <string>
31#include <utility>
32
Owen Anderson2bc29dc2009-06-30 00:48:55 +000033using namespace llvm;
34
Chris Lattnerec39f092010-03-30 23:03:27 +000035LLVMContext::LLVMContext() : pImpl(new LLVMContextImpl(*this)) {
Dan Gohmanb2143b62010-09-14 21:25:10 +000036 // Create the fixed metadata kinds. This is done in the same order as the
37 // MD_* enum values so that they correspond.
Peter Collingbourne2af93f12016-12-06 23:53:01 +000038 std::pair<unsigned, StringRef> MDKinds[] = {
39 {MD_dbg, "dbg"},
40 {MD_tbaa, "tbaa"},
41 {MD_prof, "prof"},
42 {MD_fpmath, "fpmath"},
43 {MD_range, "range"},
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"},
56 {MD_align, "align"},
57 {MD_loop, "llvm.loop"},
58 {MD_type, "type"},
59 {MD_section_prefix, "section_prefix"},
Peter Collingbourneb29976c2016-12-08 19:01:00 +000060 {MD_absolute_symbol, "absolute_symbol"},
Evgeniy Stepanovdf808fe2017-03-17 22:17:24 +000061 {MD_associated, "associated"},
Matthew Simpson9f219e8d2017-10-16 22:22:11 +000062 {MD_callees, "callees"},
Hiroshi Yamauchidd33e172017-11-02 22:26:51 +000063 {MD_irr_loop, "irr_loop"},
Michael Kruse42a382c2018-12-20 04:58:07 +000064 {MD_access_group, "llvm.access.group"},
Peter Collingbourne2af93f12016-12-06 23:53:01 +000065 };
Dan Gohmanb2143b62010-09-14 21:25:10 +000066
Peter Collingbourne2af93f12016-12-06 23:53:01 +000067 for (auto &MDKind : MDKinds) {
68 unsigned ID = getMDKindID(MDKind.second);
69 assert(ID == MDKind.first && "metadata kind id drifted");
70 (void)ID;
71 }
Dehao Chen977fc822016-10-18 20:42:47 +000072
Sanjoy Dasb4b58ba2015-11-11 21:38:02 +000073 auto *DeoptEntry = pImpl->getOrInsertBundleTag("deopt");
74 assert(DeoptEntry->second == LLVMContext::OB_deopt &&
75 "deopt operand bundle id drifted!");
76 (void)DeoptEntry;
David Majnemerb46bb542015-12-15 21:27:27 +000077
78 auto *FuncletEntry = pImpl->getOrInsertBundleTag("funclet");
79 assert(FuncletEntry->second == LLVMContext::OB_funclet &&
80 "funclet operand bundle id drifted!");
81 (void)FuncletEntry;
Sanjoy Dase6a9ed72016-01-20 19:50:25 +000082
83 auto *GCTransitionEntry = pImpl->getOrInsertBundleTag("gc-transition");
84 assert(GCTransitionEntry->second == LLVMContext::OB_gc_transition &&
85 "gc-transition operand bundle id drifted!");
86 (void)GCTransitionEntry;
Konstantin Zhuravlyov8f856852017-07-11 22:23:00 +000087
88 SyncScope::ID SingleThreadSSID =
89 pImpl->getOrInsertSyncScopeID("singlethread");
90 assert(SingleThreadSSID == SyncScope::SingleThread &&
91 "singlethread synchronization scope ID drifted!");
Konstantin Zhuravlyov4cdc8832017-07-12 00:15:53 +000092 (void)SingleThreadSSID;
Konstantin Zhuravlyov8f856852017-07-11 22:23:00 +000093
94 SyncScope::ID SystemSSID =
95 pImpl->getOrInsertSyncScopeID("");
96 assert(SystemSSID == SyncScope::System &&
97 "system synchronization scope ID drifted!");
Konstantin Zhuravlyov4cdc8832017-07-12 00:15:53 +000098 (void)SystemSSID;
Chris Lattnerec39f092010-03-30 23:03:27 +000099}
Eugene Zelenkoaaf3f042016-04-28 18:04:41 +0000100
Owen Anderson2bc29dc2009-06-30 00:48:55 +0000101LLVMContext::~LLVMContext() { delete pImpl; }
Owen Anderson48b2f3e2009-08-04 22:41:48 +0000102
Owen Anderson30268be2010-09-08 18:03:32 +0000103void LLVMContext::addModule(Module *M) {
104 pImpl->OwnedModules.insert(M);
105}
106
107void LLVMContext::removeModule(Module *M) {
108 pImpl->OwnedModules.erase(M);
109}
110
Chris Lattner38686bd2010-04-07 23:40:44 +0000111//===----------------------------------------------------------------------===//
112// Recoverable Backend Errors
113//===----------------------------------------------------------------------===//
114
Bob Wilsonf64c8892013-02-11 05:37:07 +0000115void LLVMContext::
116setInlineAsmDiagnosticHandler(InlineAsmDiagHandlerTy DiagHandler,
117 void *DiagContext) {
118 pImpl->InlineAsmDiagHandler = DiagHandler;
119 pImpl->InlineAsmDiagContext = DiagContext;
Chris Lattner42a4ee02010-04-06 00:44:45 +0000120}
121
Bob Wilsonf64c8892013-02-11 05:37:07 +0000122/// getInlineAsmDiagnosticHandler - Return the diagnostic handler set by
123/// setInlineAsmDiagnosticHandler.
124LLVMContext::InlineAsmDiagHandlerTy
125LLVMContext::getInlineAsmDiagnosticHandler() const {
126 return pImpl->InlineAsmDiagHandler;
Chris Lattner42a4ee02010-04-06 00:44:45 +0000127}
128
Bob Wilsonf64c8892013-02-11 05:37:07 +0000129/// getInlineAsmDiagnosticContext - Return the diagnostic context set by
130/// setInlineAsmDiagnosticHandler.
131void *LLVMContext::getInlineAsmDiagnosticContext() const {
132 return pImpl->InlineAsmDiagContext;
Chris Lattner42a4ee02010-04-06 00:44:45 +0000133}
Chris Lattner04e3b1e2010-03-30 20:48:48 +0000134
Vivek Pandya18b4c372017-09-15 20:10:09 +0000135void LLVMContext::setDiagnosticHandlerCallBack(
136 DiagnosticHandler::DiagnosticHandlerTy DiagnosticHandler,
137 void *DiagnosticContext, bool RespectFilters) {
138 pImpl->DiagHandler->DiagHandlerCallback = DiagnosticHandler;
139 pImpl->DiagHandler->DiagnosticContext = DiagnosticContext;
140 pImpl->RespectDiagnosticFilters = RespectFilters;
141}
142
143void LLVMContext::setDiagnosticHandler(std::unique_ptr<DiagnosticHandler> &&DH,
144 bool RespectFilters) {
145 pImpl->DiagHandler = std::move(DH);
Duncan P. N. Exon Smith04d21862014-10-01 18:36:03 +0000146 pImpl->RespectDiagnosticFilters = RespectFilters;
Quentin Colombetde262fe2013-12-17 17:47:22 +0000147}
148
Brian Gesiak8e8ec782017-06-30 18:13:59 +0000149void LLVMContext::setDiagnosticsHotnessRequested(bool Requested) {
150 pImpl->DiagnosticsHotnessRequested = Requested;
151}
152bool LLVMContext::getDiagnosticsHotnessRequested() const {
153 return pImpl->DiagnosticsHotnessRequested;
Adam Nemetf8cec992016-07-15 17:23:20 +0000154}
155
Brian Gesiake3305282017-06-30 23:14:53 +0000156void LLVMContext::setDiagnosticsHotnessThreshold(uint64_t Threshold) {
157 pImpl->DiagnosticsHotnessThreshold = Threshold;
158}
159uint64_t LLVMContext::getDiagnosticsHotnessThreshold() const {
160 return pImpl->DiagnosticsHotnessThreshold;
161}
162
Adam Nemet47c0d492016-09-27 20:55:07 +0000163yaml::Output *LLVMContext::getDiagnosticsOutputFile() {
164 return pImpl->DiagnosticsOutputFile.get();
165}
166
Mehdi Amini1bb4b122016-11-19 18:19:41 +0000167void LLVMContext::setDiagnosticsOutputFile(std::unique_ptr<yaml::Output> F) {
168 pImpl->DiagnosticsOutputFile = std::move(F);
Adam Nemet47c0d492016-09-27 20:55:07 +0000169}
170
Vivek Pandya18b4c372017-09-15 20:10:09 +0000171DiagnosticHandler::DiagnosticHandlerTy
172LLVMContext::getDiagnosticHandlerCallBack() const {
173 return pImpl->DiagHandler->DiagHandlerCallback;
Quentin Colombetde262fe2013-12-17 17:47:22 +0000174}
175
176void *LLVMContext::getDiagnosticContext() const {
Vivek Pandya18b4c372017-09-15 20:10:09 +0000177 return pImpl->DiagHandler->DiagnosticContext;
Quentin Colombetde262fe2013-12-17 17:47:22 +0000178}
179
Juergen Ributzka9bc1b732014-05-16 02:33:15 +0000180void LLVMContext::setYieldCallback(YieldCallbackTy Callback, void *OpaqueHandle)
181{
182 pImpl->YieldCallback = Callback;
183 pImpl->YieldOpaqueHandle = OpaqueHandle;
184}
185
186void LLVMContext::yield() {
187 if (pImpl->YieldCallback)
188 pImpl->YieldCallback(this, pImpl->YieldOpaqueHandle);
189}
190
Chris Lattner3a4c60c2012-01-03 23:47:05 +0000191void LLVMContext::emitError(const Twine &ErrorStr) {
Quentin Colombet341149a2014-02-22 00:34:11 +0000192 diagnose(DiagnosticInfoInlineAsm(ErrorStr));
Chris Lattner38686bd2010-04-07 23:40:44 +0000193}
194
Bob Wilson58446912013-02-08 21:48:29 +0000195void LLVMContext::emitError(const Instruction *I, const Twine &ErrorStr) {
Quentin Colombet341149a2014-02-22 00:34:11 +0000196 assert (I && "Invalid instruction");
197 diagnose(DiagnosticInfoInlineAsm(*I, ErrorStr));
Chris Lattner38686bd2010-04-07 23:40:44 +0000198}
199
Duncan P. N. Exon Smith04d21862014-10-01 18:36:03 +0000200static bool isDiagnosticEnabled(const DiagnosticInfo &DI) {
Diego Novillod16404a2014-05-22 14:19:46 +0000201 // Optimization remarks are selective. They need to check whether the regexp
202 // pattern, passed via one of the -pass-remarks* flags, matches the name of
203 // the pass that is emitting the diagnostic. If there is no match, ignore the
204 // diagnostic and return.
Adam Nemet138faec2017-10-04 04:26:23 +0000205 //
206 // Also noisy remarks are only enabled if we have hotness information to sort
207 // them.
Akira Hatanaka5fe8a3c2016-04-01 00:34:39 +0000208 if (auto *Remark = dyn_cast<DiagnosticInfoOptimizationBase>(&DI))
Adam Nemet138faec2017-10-04 04:26:23 +0000209 return Remark->isEnabled() &&
210 (!Remark->isVerbose() || Remark->getHotness());
Akira Hatanaka5fe8a3c2016-04-01 00:34:39 +0000211
Duncan P. N. Exon Smith04d21862014-10-01 18:36:03 +0000212 return true;
213}
214
Renato Golin3b0d3772016-05-16 14:28:02 +0000215const char *
216LLVMContext::getDiagnosticMessagePrefix(DiagnosticSeverity Severity) {
Alex Lorenza15d8882015-06-15 20:30:22 +0000217 switch (Severity) {
218 case DS_Error:
219 return "error";
220 case DS_Warning:
221 return "warning";
222 case DS_Remark:
223 return "remark";
224 case DS_Note:
225 return "note";
226 }
Aaron Ballmandb8ece32015-06-16 13:14:59 +0000227 llvm_unreachable("Unknown DiagnosticSeverity");
Alex Lorenza15d8882015-06-15 20:30:22 +0000228}
229
Duncan P. N. Exon Smith04d21862014-10-01 18:36:03 +0000230void LLVMContext::diagnose(const DiagnosticInfo &DI) {
Adam Nemetc5599f62017-10-04 15:18:11 +0000231 if (auto *OptDiagBase = dyn_cast<DiagnosticInfoOptimizationBase>(&DI)) {
232 yaml::Output *Out = getDiagnosticsOutputFile();
233 if (Out) {
234 // For remarks the << operator takes a reference to a pointer.
235 auto *P = const_cast<DiagnosticInfoOptimizationBase *>(OptDiagBase);
236 *Out << P;
237 }
238 }
Duncan P. N. Exon Smith04d21862014-10-01 18:36:03 +0000239 // If there is a report handler, use it.
Vivek Pandya18b4c372017-09-15 20:10:09 +0000240 if (pImpl->DiagHandler &&
241 (!pImpl->RespectDiagnosticFilters || isDiagnosticEnabled(DI)) &&
242 pImpl->DiagHandler->handleDiagnostics(DI))
Duncan P. N. Exon Smith04d21862014-10-01 18:36:03 +0000243 return;
Duncan P. N. Exon Smith04d21862014-10-01 18:36:03 +0000244
245 if (!isDiagnosticEnabled(DI))
246 return;
Diego Novillob902acb2014-04-16 16:53:41 +0000247
Quentin Colombetde262fe2013-12-17 17:47:22 +0000248 // Otherwise, print the message with a prefix based on the severity.
Alex Lorenza15d8882015-06-15 20:30:22 +0000249 DiagnosticPrinterRawOStream DP(errs());
250 errs() << getDiagnosticMessagePrefix(DI.getSeverity()) << ": ";
Quentin Colombetde262fe2013-12-17 17:47:22 +0000251 DI.print(DP);
Alex Lorenza15d8882015-06-15 20:30:22 +0000252 errs() << "\n";
253 if (DI.getSeverity() == DS_Error)
Quentin Colombetde262fe2013-12-17 17:47:22 +0000254 exit(1);
Quentin Colombetde262fe2013-12-17 17:47:22 +0000255}
256
Chris Lattner3a4c60c2012-01-03 23:47:05 +0000257void LLVMContext::emitError(unsigned LocCookie, const Twine &ErrorStr) {
Quentin Colombet341149a2014-02-22 00:34:11 +0000258 diagnose(DiagnosticInfoInlineAsm(LocCookie, ErrorStr));
Chris Lattner38686bd2010-04-07 23:40:44 +0000259}
260
261//===----------------------------------------------------------------------===//
262// Metadata Kind Uniquing
263//===----------------------------------------------------------------------===//
264
Filipe Cabecinhas581e2552015-06-02 21:25:00 +0000265/// Return a unique non-zero ID for the specified metadata kind.
Chris Lattner04e3b1e2010-03-30 20:48:48 +0000266unsigned LLVMContext::getMDKindID(StringRef Name) const {
Chris Lattner04e3b1e2010-03-30 20:48:48 +0000267 // If this is new, assign it its ID.
Filipe Cabecinhas581e2552015-06-02 21:25:00 +0000268 return pImpl->CustomMDKindNames.insert(
269 std::make_pair(
270 Name, pImpl->CustomMDKindNames.size()))
David Blaikie1d4f28c2014-11-19 05:49:42 +0000271 .first->second;
Chris Lattner04e3b1e2010-03-30 20:48:48 +0000272}
273
Sanjay Patel002f8362015-08-24 23:20:16 +0000274/// getHandlerNames - Populate client-supplied smallvector using custom
Chris Lattner04e3b1e2010-03-30 20:48:48 +0000275/// metadata name and ID.
276void LLVMContext::getMDKindNames(SmallVectorImpl<StringRef> &Names) const {
Dan Gohman19538d12010-07-20 21:42:28 +0000277 Names.resize(pImpl->CustomMDKindNames.size());
Chris Lattner04e3b1e2010-03-30 20:48:48 +0000278 for (StringMap<unsigned>::const_iterator I = pImpl->CustomMDKindNames.begin(),
279 E = pImpl->CustomMDKindNames.end(); I != E; ++I)
Chris Lattner04e3b1e2010-03-30 20:48:48 +0000280 Names[I->second] = I->first();
281}
Sanjoy Das5b674c02015-09-24 19:14:18 +0000282
283void LLVMContext::getOperandBundleTags(SmallVectorImpl<StringRef> &Tags) const {
284 pImpl->getOperandBundleTags(Tags);
285}
286
287uint32_t LLVMContext::getOperandBundleTagID(StringRef Tag) const {
288 return pImpl->getOperandBundleTagID(Tag);
289}
Mehdi Amini3569d3c2016-01-08 02:28:20 +0000290
Konstantin Zhuravlyov8f856852017-07-11 22:23:00 +0000291SyncScope::ID LLVMContext::getOrInsertSyncScopeID(StringRef SSN) {
292 return pImpl->getOrInsertSyncScopeID(SSN);
293}
294
295void LLVMContext::getSyncScopeNames(SmallVectorImpl<StringRef> &SSNs) const {
296 pImpl->getSyncScopeNames(SSNs);
297}
298
Mehdi Amini3569d3c2016-01-08 02:28:20 +0000299void LLVMContext::setGC(const Function &Fn, std::string GCName) {
300 auto It = pImpl->GCNames.find(&Fn);
301
302 if (It == pImpl->GCNames.end()) {
303 pImpl->GCNames.insert(std::make_pair(&Fn, std::move(GCName)));
304 return;
305 }
306 It->second = std::move(GCName);
307}
Eugene Zelenkoaaf3f042016-04-28 18:04:41 +0000308
Mehdi Amini3569d3c2016-01-08 02:28:20 +0000309const std::string &LLVMContext::getGC(const Function &Fn) {
310 return pImpl->GCNames[&Fn];
311}
Eugene Zelenkoaaf3f042016-04-28 18:04:41 +0000312
Mehdi Amini3569d3c2016-01-08 02:28:20 +0000313void LLVMContext::deleteGC(const Function &Fn) {
314 pImpl->GCNames.erase(&Fn);
315}
Mehdi Amini2de99272016-03-10 01:28:54 +0000316
Mehdi Aminib3cb9bc2016-04-02 03:59:58 +0000317bool LLVMContext::shouldDiscardValueNames() const {
318 return pImpl->DiscardValueNames;
319}
Mehdi Amini2de99272016-03-10 01:28:54 +0000320
Duncan P. N. Exon Smith2663f352016-04-19 04:55:25 +0000321bool LLVMContext::isODRUniquingDebugTypes() const { return !!pImpl->DITypeMap; }
Duncan P. N. Exon Smith9bb5d5d2016-04-17 03:58:21 +0000322
Duncan P. N. Exon Smith2663f352016-04-19 04:55:25 +0000323void LLVMContext::enableDebugTypeODRUniquing() {
Duncan P. N. Exon Smith9bb5d5d2016-04-17 03:58:21 +0000324 if (pImpl->DITypeMap)
325 return;
326
Duncan P. N. Exon Smith75cc0512016-04-19 16:06:50 +0000327 pImpl->DITypeMap.emplace();
Duncan P. N. Exon Smith9bb5d5d2016-04-17 03:58:21 +0000328}
329
Duncan P. N. Exon Smith2663f352016-04-19 04:55:25 +0000330void LLVMContext::disableDebugTypeODRUniquing() { pImpl->DITypeMap.reset(); }
Duncan P. N. Exon Smith9bb5d5d2016-04-17 03:58:21 +0000331
Mehdi Amini2de99272016-03-10 01:28:54 +0000332void LLVMContext::setDiscardValueNames(bool Discard) {
333 pImpl->DiscardValueNames = Discard;
334}
Andrew Kaylor1e455c52016-04-22 22:06:11 +0000335
Fedor Sergeev46d5e2b2018-04-05 10:29:37 +0000336OptPassGate &LLVMContext::getOptPassGate() const {
Fedor Sergeev558f76f2018-03-27 16:57:20 +0000337 return pImpl->getOptPassGate();
Andrew Kaylor1e455c52016-04-22 22:06:11 +0000338}
Vivek Pandya18b4c372017-09-15 20:10:09 +0000339
Fedor Sergeev46d5e2b2018-04-05 10:29:37 +0000340void LLVMContext::setOptPassGate(OptPassGate& OPG) {
341 pImpl->setOptPassGate(OPG);
342}
343
Vivek Pandya18b4c372017-09-15 20:10:09 +0000344const DiagnosticHandler *LLVMContext::getDiagHandlerPtr() const {
345 return pImpl->DiagHandler.get();
346}
347
348std::unique_ptr<DiagnosticHandler> LLVMContext::getDiagnosticHandler() {
349 return std::move(pImpl->DiagHandler);
350}