blob: 5b10854b6d269778a98bdc71ec3b88693b01b5ea [file] [log] [blame]
Nick Lewyckyfca2acb2012-12-26 22:00:49 +00001//===-- LLVMContext.cpp - Implement LLVMContext ---------------------------===//
Owen Anderson8e66e0b2009-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 Anderson36f62e52009-06-30 17:06:46 +00009//
10// This file implements LLVMContext, as a wrapper around the opaque
Benjamin Kramer78c3bcb2009-08-11 17:45:13 +000011// class LLVMContextImpl.
Owen Anderson36f62e52009-06-30 17:06:46 +000012//
13//===----------------------------------------------------------------------===//
Owen Anderson8e66e0b2009-06-30 00:48:55 +000014
Chandler Carruth9fb823b2013-01-02 11:36:10 +000015#include "llvm/IR/LLVMContext.h"
Chandler Carruth6bda14b2017-06-06 11:49:48 +000016#include "LLVMContextImpl.h"
Eugene Zelenko5354a8a2016-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 Colombetb4c44d22013-12-17 17:47:22 +000021#include "llvm/IR/DiagnosticInfo.h"
22#include "llvm/IR/DiagnosticPrinter.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000023#include "llvm/IR/Metadata.h"
Eugene Zelenko5354a8a2016-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 Anderson8e66e0b2009-06-30 00:48:55 +000033using namespace llvm;
34
Chris Lattnerc263b422010-03-30 23:03:27 +000035LLVMContext::LLVMContext() : pImpl(new LLVMContextImpl(*this)) {
Dan Gohman41f14cf2010-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 Collingbourne6f0b4f22016-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 Collingbourne235c2752016-12-08 19:01:00 +000060 {MD_absolute_symbol, "absolute_symbol"},
Evgeniy Stepanov51c962f722017-03-17 22:17:24 +000061 {MD_associated, "associated"},
Matthew Simpson36bbc8c2017-10-16 22:22:11 +000062 {MD_callees, "callees"},
Hiroshi Yamauchidce9def2017-11-02 22:26:51 +000063 {MD_irr_loop, "irr_loop"},
Peter Collingbourne6f0b4f22016-12-06 23:53:01 +000064 };
Dan Gohman41f14cf2010-09-14 21:25:10 +000065
Peter Collingbourne6f0b4f22016-12-06 23:53:01 +000066 for (auto &MDKind : MDKinds) {
67 unsigned ID = getMDKindID(MDKind.second);
68 assert(ID == MDKind.first && "metadata kind id drifted");
69 (void)ID;
70 }
Dehao Chen302b69c2016-10-18 20:42:47 +000071
Sanjoy Dascdafd842015-11-11 21:38:02 +000072 auto *DeoptEntry = pImpl->getOrInsertBundleTag("deopt");
73 assert(DeoptEntry->second == LLVMContext::OB_deopt &&
74 "deopt operand bundle id drifted!");
75 (void)DeoptEntry;
David Majnemer3bb88c02015-12-15 21:27:27 +000076
77 auto *FuncletEntry = pImpl->getOrInsertBundleTag("funclet");
78 assert(FuncletEntry->second == LLVMContext::OB_funclet &&
79 "funclet operand bundle id drifted!");
80 (void)FuncletEntry;
Sanjoy Dasa34ce952016-01-20 19:50:25 +000081
82 auto *GCTransitionEntry = pImpl->getOrInsertBundleTag("gc-transition");
83 assert(GCTransitionEntry->second == LLVMContext::OB_gc_transition &&
84 "gc-transition operand bundle id drifted!");
85 (void)GCTransitionEntry;
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +000086
87 SyncScope::ID SingleThreadSSID =
88 pImpl->getOrInsertSyncScopeID("singlethread");
89 assert(SingleThreadSSID == SyncScope::SingleThread &&
90 "singlethread synchronization scope ID drifted!");
Konstantin Zhuravlyov878fdee2017-07-12 00:15:53 +000091 (void)SingleThreadSSID;
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +000092
93 SyncScope::ID SystemSSID =
94 pImpl->getOrInsertSyncScopeID("");
95 assert(SystemSSID == SyncScope::System &&
96 "system synchronization scope ID drifted!");
Konstantin Zhuravlyov878fdee2017-07-12 00:15:53 +000097 (void)SystemSSID;
Chris Lattnerc263b422010-03-30 23:03:27 +000098}
Eugene Zelenko5354a8a2016-04-28 18:04:41 +000099
Owen Anderson8e66e0b2009-06-30 00:48:55 +0000100LLVMContext::~LLVMContext() { delete pImpl; }
Owen Andersonafd0c4c2009-08-04 22:41:48 +0000101
Owen Anderson8e89e412010-09-08 18:03:32 +0000102void LLVMContext::addModule(Module *M) {
103 pImpl->OwnedModules.insert(M);
104}
105
106void LLVMContext::removeModule(Module *M) {
107 pImpl->OwnedModules.erase(M);
108}
109
Chris Lattner1e457892010-04-07 23:40:44 +0000110//===----------------------------------------------------------------------===//
111// Recoverable Backend Errors
112//===----------------------------------------------------------------------===//
113
Bob Wilsona594fab2013-02-11 05:37:07 +0000114void LLVMContext::
115setInlineAsmDiagnosticHandler(InlineAsmDiagHandlerTy DiagHandler,
116 void *DiagContext) {
117 pImpl->InlineAsmDiagHandler = DiagHandler;
118 pImpl->InlineAsmDiagContext = DiagContext;
Chris Lattner60955d42010-04-06 00:44:45 +0000119}
120
Bob Wilsona594fab2013-02-11 05:37:07 +0000121/// getInlineAsmDiagnosticHandler - Return the diagnostic handler set by
122/// setInlineAsmDiagnosticHandler.
123LLVMContext::InlineAsmDiagHandlerTy
124LLVMContext::getInlineAsmDiagnosticHandler() const {
125 return pImpl->InlineAsmDiagHandler;
Chris Lattner60955d42010-04-06 00:44:45 +0000126}
127
Bob Wilsona594fab2013-02-11 05:37:07 +0000128/// getInlineAsmDiagnosticContext - Return the diagnostic context set by
129/// setInlineAsmDiagnosticHandler.
130void *LLVMContext::getInlineAsmDiagnosticContext() const {
131 return pImpl->InlineAsmDiagContext;
Chris Lattner60955d42010-04-06 00:44:45 +0000132}
Chris Lattnera3b94ba2010-03-30 20:48:48 +0000133
Vivek Pandyab5ab8952017-09-15 20:10:09 +0000134void LLVMContext::setDiagnosticHandlerCallBack(
135 DiagnosticHandler::DiagnosticHandlerTy DiagnosticHandler,
136 void *DiagnosticContext, bool RespectFilters) {
137 pImpl->DiagHandler->DiagHandlerCallback = DiagnosticHandler;
138 pImpl->DiagHandler->DiagnosticContext = DiagnosticContext;
139 pImpl->RespectDiagnosticFilters = RespectFilters;
140}
141
142void LLVMContext::setDiagnosticHandler(std::unique_ptr<DiagnosticHandler> &&DH,
143 bool RespectFilters) {
144 pImpl->DiagHandler = std::move(DH);
Duncan P. N. Exon Smith30c92422014-10-01 18:36:03 +0000145 pImpl->RespectDiagnosticFilters = RespectFilters;
Quentin Colombetb4c44d22013-12-17 17:47:22 +0000146}
147
Brian Gesiak44e5f6c2017-06-30 18:13:59 +0000148void LLVMContext::setDiagnosticsHotnessRequested(bool Requested) {
149 pImpl->DiagnosticsHotnessRequested = Requested;
150}
151bool LLVMContext::getDiagnosticsHotnessRequested() const {
152 return pImpl->DiagnosticsHotnessRequested;
Adam Nemetaad81602016-07-15 17:23:20 +0000153}
154
Brian Gesiak4ef3daa2017-06-30 23:14:53 +0000155void LLVMContext::setDiagnosticsHotnessThreshold(uint64_t Threshold) {
156 pImpl->DiagnosticsHotnessThreshold = Threshold;
157}
158uint64_t LLVMContext::getDiagnosticsHotnessThreshold() const {
159 return pImpl->DiagnosticsHotnessThreshold;
160}
161
Adam Nemeta62b7e12016-09-27 20:55:07 +0000162yaml::Output *LLVMContext::getDiagnosticsOutputFile() {
163 return pImpl->DiagnosticsOutputFile.get();
164}
165
Mehdi Amini6f408362016-11-19 18:19:41 +0000166void LLVMContext::setDiagnosticsOutputFile(std::unique_ptr<yaml::Output> F) {
167 pImpl->DiagnosticsOutputFile = std::move(F);
Adam Nemeta62b7e12016-09-27 20:55:07 +0000168}
169
Vivek Pandyab5ab8952017-09-15 20:10:09 +0000170DiagnosticHandler::DiagnosticHandlerTy
171LLVMContext::getDiagnosticHandlerCallBack() const {
172 return pImpl->DiagHandler->DiagHandlerCallback;
Quentin Colombetb4c44d22013-12-17 17:47:22 +0000173}
174
175void *LLVMContext::getDiagnosticContext() const {
Vivek Pandyab5ab8952017-09-15 20:10:09 +0000176 return pImpl->DiagHandler->DiagnosticContext;
Quentin Colombetb4c44d22013-12-17 17:47:22 +0000177}
178
Juergen Ributzka34390c72014-05-16 02:33:15 +0000179void LLVMContext::setYieldCallback(YieldCallbackTy Callback, void *OpaqueHandle)
180{
181 pImpl->YieldCallback = Callback;
182 pImpl->YieldOpaqueHandle = OpaqueHandle;
183}
184
185void LLVMContext::yield() {
186 if (pImpl->YieldCallback)
187 pImpl->YieldCallback(this, pImpl->YieldOpaqueHandle);
188}
189
Chris Lattnere22e6132012-01-03 23:47:05 +0000190void LLVMContext::emitError(const Twine &ErrorStr) {
Quentin Colombetdec8d552014-02-22 00:34:11 +0000191 diagnose(DiagnosticInfoInlineAsm(ErrorStr));
Chris Lattner1e457892010-04-07 23:40:44 +0000192}
193
Bob Wilsonbfb44ef2013-02-08 21:48:29 +0000194void LLVMContext::emitError(const Instruction *I, const Twine &ErrorStr) {
Quentin Colombetdec8d552014-02-22 00:34:11 +0000195 assert (I && "Invalid instruction");
196 diagnose(DiagnosticInfoInlineAsm(*I, ErrorStr));
Chris Lattner1e457892010-04-07 23:40:44 +0000197}
198
Duncan P. N. Exon Smith30c92422014-10-01 18:36:03 +0000199static bool isDiagnosticEnabled(const DiagnosticInfo &DI) {
Diego Novillo7f8af8b2014-05-22 14:19:46 +0000200 // Optimization remarks are selective. They need to check whether the regexp
201 // pattern, passed via one of the -pass-remarks* flags, matches the name of
202 // the pass that is emitting the diagnostic. If there is no match, ignore the
203 // diagnostic and return.
Adam Nemetf31b1f32017-10-04 04:26:23 +0000204 //
205 // Also noisy remarks are only enabled if we have hotness information to sort
206 // them.
Akira Hatanakae9148dd2016-04-01 00:34:39 +0000207 if (auto *Remark = dyn_cast<DiagnosticInfoOptimizationBase>(&DI))
Adam Nemetf31b1f32017-10-04 04:26:23 +0000208 return Remark->isEnabled() &&
209 (!Remark->isVerbose() || Remark->getHotness());
Akira Hatanakae9148dd2016-04-01 00:34:39 +0000210
Duncan P. N. Exon Smith30c92422014-10-01 18:36:03 +0000211 return true;
212}
213
Renato Golin4b9c0d42016-05-16 14:28:02 +0000214const char *
215LLVMContext::getDiagnosticMessagePrefix(DiagnosticSeverity Severity) {
Alex Lorenz735c47e2015-06-15 20:30:22 +0000216 switch (Severity) {
217 case DS_Error:
218 return "error";
219 case DS_Warning:
220 return "warning";
221 case DS_Remark:
222 return "remark";
223 case DS_Note:
224 return "note";
225 }
Aaron Ballman52204762015-06-16 13:14:59 +0000226 llvm_unreachable("Unknown DiagnosticSeverity");
Alex Lorenz735c47e2015-06-15 20:30:22 +0000227}
228
Duncan P. N. Exon Smith30c92422014-10-01 18:36:03 +0000229void LLVMContext::diagnose(const DiagnosticInfo &DI) {
Adam Nemet6c381b72017-10-04 15:18:11 +0000230 if (auto *OptDiagBase = dyn_cast<DiagnosticInfoOptimizationBase>(&DI)) {
231 yaml::Output *Out = getDiagnosticsOutputFile();
232 if (Out) {
233 // For remarks the << operator takes a reference to a pointer.
234 auto *P = const_cast<DiagnosticInfoOptimizationBase *>(OptDiagBase);
235 *Out << P;
236 }
237 }
Duncan P. N. Exon Smith30c92422014-10-01 18:36:03 +0000238 // If there is a report handler, use it.
Vivek Pandyab5ab8952017-09-15 20:10:09 +0000239 if (pImpl->DiagHandler &&
240 (!pImpl->RespectDiagnosticFilters || isDiagnosticEnabled(DI)) &&
241 pImpl->DiagHandler->handleDiagnostics(DI))
Duncan P. N. Exon Smith30c92422014-10-01 18:36:03 +0000242 return;
Duncan P. N. Exon Smith30c92422014-10-01 18:36:03 +0000243
244 if (!isDiagnosticEnabled(DI))
245 return;
Diego Novillodf655012014-04-16 16:53:41 +0000246
Quentin Colombetb4c44d22013-12-17 17:47:22 +0000247 // Otherwise, print the message with a prefix based on the severity.
Alex Lorenz735c47e2015-06-15 20:30:22 +0000248 DiagnosticPrinterRawOStream DP(errs());
249 errs() << getDiagnosticMessagePrefix(DI.getSeverity()) << ": ";
Quentin Colombetb4c44d22013-12-17 17:47:22 +0000250 DI.print(DP);
Alex Lorenz735c47e2015-06-15 20:30:22 +0000251 errs() << "\n";
252 if (DI.getSeverity() == DS_Error)
Quentin Colombetb4c44d22013-12-17 17:47:22 +0000253 exit(1);
Quentin Colombetb4c44d22013-12-17 17:47:22 +0000254}
255
Chris Lattnere22e6132012-01-03 23:47:05 +0000256void LLVMContext::emitError(unsigned LocCookie, const Twine &ErrorStr) {
Quentin Colombetdec8d552014-02-22 00:34:11 +0000257 diagnose(DiagnosticInfoInlineAsm(LocCookie, ErrorStr));
Chris Lattner1e457892010-04-07 23:40:44 +0000258}
259
260//===----------------------------------------------------------------------===//
261// Metadata Kind Uniquing
262//===----------------------------------------------------------------------===//
263
Filipe Cabecinhas1ac0d2d2015-06-02 21:25:00 +0000264/// Return a unique non-zero ID for the specified metadata kind.
Chris Lattnera3b94ba2010-03-30 20:48:48 +0000265unsigned LLVMContext::getMDKindID(StringRef Name) const {
Chris Lattnera3b94ba2010-03-30 20:48:48 +0000266 // If this is new, assign it its ID.
Filipe Cabecinhas1ac0d2d2015-06-02 21:25:00 +0000267 return pImpl->CustomMDKindNames.insert(
268 std::make_pair(
269 Name, pImpl->CustomMDKindNames.size()))
David Blaikie5106ce72014-11-19 05:49:42 +0000270 .first->second;
Chris Lattnera3b94ba2010-03-30 20:48:48 +0000271}
272
Sanjay Patel41043372015-08-24 23:20:16 +0000273/// getHandlerNames - Populate client-supplied smallvector using custom
Chris Lattnera3b94ba2010-03-30 20:48:48 +0000274/// metadata name and ID.
275void LLVMContext::getMDKindNames(SmallVectorImpl<StringRef> &Names) const {
Dan Gohman43aa8f02010-07-20 21:42:28 +0000276 Names.resize(pImpl->CustomMDKindNames.size());
Chris Lattnera3b94ba2010-03-30 20:48:48 +0000277 for (StringMap<unsigned>::const_iterator I = pImpl->CustomMDKindNames.begin(),
278 E = pImpl->CustomMDKindNames.end(); I != E; ++I)
Chris Lattnera3b94ba2010-03-30 20:48:48 +0000279 Names[I->second] = I->first();
280}
Sanjoy Das9303c242015-09-24 19:14:18 +0000281
282void LLVMContext::getOperandBundleTags(SmallVectorImpl<StringRef> &Tags) const {
283 pImpl->getOperandBundleTags(Tags);
284}
285
286uint32_t LLVMContext::getOperandBundleTagID(StringRef Tag) const {
287 return pImpl->getOperandBundleTagID(Tag);
288}
Mehdi Amini599ebf22016-01-08 02:28:20 +0000289
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +0000290SyncScope::ID LLVMContext::getOrInsertSyncScopeID(StringRef SSN) {
291 return pImpl->getOrInsertSyncScopeID(SSN);
292}
293
294void LLVMContext::getSyncScopeNames(SmallVectorImpl<StringRef> &SSNs) const {
295 pImpl->getSyncScopeNames(SSNs);
296}
297
Mehdi Amini599ebf22016-01-08 02:28:20 +0000298void LLVMContext::setGC(const Function &Fn, std::string GCName) {
299 auto It = pImpl->GCNames.find(&Fn);
300
301 if (It == pImpl->GCNames.end()) {
302 pImpl->GCNames.insert(std::make_pair(&Fn, std::move(GCName)));
303 return;
304 }
305 It->second = std::move(GCName);
306}
Eugene Zelenko5354a8a2016-04-28 18:04:41 +0000307
Mehdi Amini599ebf22016-01-08 02:28:20 +0000308const std::string &LLVMContext::getGC(const Function &Fn) {
309 return pImpl->GCNames[&Fn];
310}
Eugene Zelenko5354a8a2016-04-28 18:04:41 +0000311
Mehdi Amini599ebf22016-01-08 02:28:20 +0000312void LLVMContext::deleteGC(const Function &Fn) {
313 pImpl->GCNames.erase(&Fn);
314}
Mehdi Amini09b4a8d2016-03-10 01:28:54 +0000315
Mehdi Aminie7090152016-04-02 03:59:58 +0000316bool LLVMContext::shouldDiscardValueNames() const {
317 return pImpl->DiscardValueNames;
318}
Mehdi Amini09b4a8d2016-03-10 01:28:54 +0000319
Duncan P. N. Exon Smithed8fdb22016-04-19 04:55:25 +0000320bool LLVMContext::isODRUniquingDebugTypes() const { return !!pImpl->DITypeMap; }
Duncan P. N. Exon Smith5ab2be02016-04-17 03:58:21 +0000321
Duncan P. N. Exon Smithed8fdb22016-04-19 04:55:25 +0000322void LLVMContext::enableDebugTypeODRUniquing() {
Duncan P. N. Exon Smith5ab2be02016-04-17 03:58:21 +0000323 if (pImpl->DITypeMap)
324 return;
325
Duncan P. N. Exon Smithe8b555c2016-04-19 16:06:50 +0000326 pImpl->DITypeMap.emplace();
Duncan P. N. Exon Smith5ab2be02016-04-17 03:58:21 +0000327}
328
Duncan P. N. Exon Smithed8fdb22016-04-19 04:55:25 +0000329void LLVMContext::disableDebugTypeODRUniquing() { pImpl->DITypeMap.reset(); }
Duncan P. N. Exon Smith5ab2be02016-04-17 03:58:21 +0000330
Mehdi Amini09b4a8d2016-03-10 01:28:54 +0000331void LLVMContext::setDiscardValueNames(bool Discard) {
332 pImpl->DiscardValueNames = Discard;
333}
Andrew Kayloraa641a52016-04-22 22:06:11 +0000334
Fedor Sergeev98014e42018-03-27 16:57:20 +0000335OptPassGate &LLVMContext::getOptPassGate() {
336 return pImpl->getOptPassGate();
Andrew Kayloraa641a52016-04-22 22:06:11 +0000337}
Vivek Pandyab5ab8952017-09-15 20:10:09 +0000338
339const DiagnosticHandler *LLVMContext::getDiagHandlerPtr() const {
340 return pImpl->DiagHandler.get();
341}
342
343std::unique_ptr<DiagnosticHandler> LLVMContext::getDiagnosticHandler() {
344 return std::move(pImpl->DiagHandler);
345}