blob: 6c6383c22255d288b800e5bdb0433009473f14c4 [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"
Eugene Zelenko5354a8a2016-04-28 18:04:41 +000016#include "llvm/ADT/SmallVector.h"
17#include "llvm/ADT/StringMap.h"
18#include "llvm/ADT/StringRef.h"
19#include "llvm/ADT/Twine.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000020#include "LLVMContextImpl.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"},
Peter Collingbourne6f0b4f22016-12-06 23:53:01 +000062 };
Dan Gohman41f14cf2010-09-14 21:25:10 +000063
Peter Collingbourne6f0b4f22016-12-06 23:53:01 +000064 for (auto &MDKind : MDKinds) {
65 unsigned ID = getMDKindID(MDKind.second);
66 assert(ID == MDKind.first && "metadata kind id drifted");
67 (void)ID;
68 }
Dehao Chen302b69c2016-10-18 20:42:47 +000069
Sanjoy Dascdafd842015-11-11 21:38:02 +000070 auto *DeoptEntry = pImpl->getOrInsertBundleTag("deopt");
71 assert(DeoptEntry->second == LLVMContext::OB_deopt &&
72 "deopt operand bundle id drifted!");
73 (void)DeoptEntry;
David Majnemer3bb88c02015-12-15 21:27:27 +000074
75 auto *FuncletEntry = pImpl->getOrInsertBundleTag("funclet");
76 assert(FuncletEntry->second == LLVMContext::OB_funclet &&
77 "funclet operand bundle id drifted!");
78 (void)FuncletEntry;
Sanjoy Dasa34ce952016-01-20 19:50:25 +000079
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;
Chris Lattnerc263b422010-03-30 23:03:27 +000084}
Eugene Zelenko5354a8a2016-04-28 18:04:41 +000085
Owen Anderson8e66e0b2009-06-30 00:48:55 +000086LLVMContext::~LLVMContext() { delete pImpl; }
Owen Andersonafd0c4c2009-08-04 22:41:48 +000087
Owen Anderson8e89e412010-09-08 18:03:32 +000088void LLVMContext::addModule(Module *M) {
89 pImpl->OwnedModules.insert(M);
90}
91
92void LLVMContext::removeModule(Module *M) {
93 pImpl->OwnedModules.erase(M);
94}
95
Chris Lattner1e457892010-04-07 23:40:44 +000096//===----------------------------------------------------------------------===//
97// Recoverable Backend Errors
98//===----------------------------------------------------------------------===//
99
Bob Wilsona594fab2013-02-11 05:37:07 +0000100void LLVMContext::
101setInlineAsmDiagnosticHandler(InlineAsmDiagHandlerTy DiagHandler,
102 void *DiagContext) {
103 pImpl->InlineAsmDiagHandler = DiagHandler;
104 pImpl->InlineAsmDiagContext = DiagContext;
Chris Lattner60955d42010-04-06 00:44:45 +0000105}
106
Bob Wilsona594fab2013-02-11 05:37:07 +0000107/// getInlineAsmDiagnosticHandler - Return the diagnostic handler set by
108/// setInlineAsmDiagnosticHandler.
109LLVMContext::InlineAsmDiagHandlerTy
110LLVMContext::getInlineAsmDiagnosticHandler() const {
111 return pImpl->InlineAsmDiagHandler;
Chris Lattner60955d42010-04-06 00:44:45 +0000112}
113
Bob Wilsona594fab2013-02-11 05:37:07 +0000114/// getInlineAsmDiagnosticContext - Return the diagnostic context set by
115/// setInlineAsmDiagnosticHandler.
116void *LLVMContext::getInlineAsmDiagnosticContext() const {
117 return pImpl->InlineAsmDiagContext;
Chris Lattner60955d42010-04-06 00:44:45 +0000118}
Chris Lattnera3b94ba2010-03-30 20:48:48 +0000119
Quentin Colombetb4c44d22013-12-17 17:47:22 +0000120void LLVMContext::setDiagnosticHandler(DiagnosticHandlerTy DiagnosticHandler,
Duncan P. N. Exon Smith30c92422014-10-01 18:36:03 +0000121 void *DiagnosticContext,
122 bool RespectFilters) {
Quentin Colombetb4c44d22013-12-17 17:47:22 +0000123 pImpl->DiagnosticHandler = DiagnosticHandler;
124 pImpl->DiagnosticContext = DiagnosticContext;
Duncan P. N. Exon Smith30c92422014-10-01 18:36:03 +0000125 pImpl->RespectDiagnosticFilters = RespectFilters;
Quentin Colombetb4c44d22013-12-17 17:47:22 +0000126}
127
Adam Nemetaad81602016-07-15 17:23:20 +0000128void LLVMContext::setDiagnosticHotnessRequested(bool Requested) {
129 pImpl->DiagnosticHotnessRequested = Requested;
130}
131bool LLVMContext::getDiagnosticHotnessRequested() const {
132 return pImpl->DiagnosticHotnessRequested;
133}
134
Adam Nemeta62b7e12016-09-27 20:55:07 +0000135yaml::Output *LLVMContext::getDiagnosticsOutputFile() {
136 return pImpl->DiagnosticsOutputFile.get();
137}
138
Mehdi Amini6f408362016-11-19 18:19:41 +0000139void LLVMContext::setDiagnosticsOutputFile(std::unique_ptr<yaml::Output> F) {
140 pImpl->DiagnosticsOutputFile = std::move(F);
Adam Nemeta62b7e12016-09-27 20:55:07 +0000141}
142
Quentin Colombetb4c44d22013-12-17 17:47:22 +0000143LLVMContext::DiagnosticHandlerTy LLVMContext::getDiagnosticHandler() const {
144 return pImpl->DiagnosticHandler;
145}
146
147void *LLVMContext::getDiagnosticContext() const {
148 return pImpl->DiagnosticContext;
149}
150
Juergen Ributzka34390c72014-05-16 02:33:15 +0000151void LLVMContext::setYieldCallback(YieldCallbackTy Callback, void *OpaqueHandle)
152{
153 pImpl->YieldCallback = Callback;
154 pImpl->YieldOpaqueHandle = OpaqueHandle;
155}
156
157void LLVMContext::yield() {
158 if (pImpl->YieldCallback)
159 pImpl->YieldCallback(this, pImpl->YieldOpaqueHandle);
160}
161
Chris Lattnere22e6132012-01-03 23:47:05 +0000162void LLVMContext::emitError(const Twine &ErrorStr) {
Quentin Colombetdec8d552014-02-22 00:34:11 +0000163 diagnose(DiagnosticInfoInlineAsm(ErrorStr));
Chris Lattner1e457892010-04-07 23:40:44 +0000164}
165
Bob Wilsonbfb44ef2013-02-08 21:48:29 +0000166void LLVMContext::emitError(const Instruction *I, const Twine &ErrorStr) {
Quentin Colombetdec8d552014-02-22 00:34:11 +0000167 assert (I && "Invalid instruction");
168 diagnose(DiagnosticInfoInlineAsm(*I, ErrorStr));
Chris Lattner1e457892010-04-07 23:40:44 +0000169}
170
Duncan P. N. Exon Smith30c92422014-10-01 18:36:03 +0000171static bool isDiagnosticEnabled(const DiagnosticInfo &DI) {
Diego Novillo7f8af8b2014-05-22 14:19:46 +0000172 // 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.
Akira Hatanakae9148dd2016-04-01 00:34:39 +0000176 if (auto *Remark = dyn_cast<DiagnosticInfoOptimizationBase>(&DI))
177 return Remark->isEnabled();
178
Duncan P. N. Exon Smith30c92422014-10-01 18:36:03 +0000179 return true;
180}
181
Renato Golin4b9c0d42016-05-16 14:28:02 +0000182const char *
183LLVMContext::getDiagnosticMessagePrefix(DiagnosticSeverity Severity) {
Alex Lorenz735c47e2015-06-15 20:30:22 +0000184 switch (Severity) {
185 case DS_Error:
186 return "error";
187 case DS_Warning:
188 return "warning";
189 case DS_Remark:
190 return "remark";
191 case DS_Note:
192 return "note";
193 }
Aaron Ballman52204762015-06-16 13:14:59 +0000194 llvm_unreachable("Unknown DiagnosticSeverity");
Alex Lorenz735c47e2015-06-15 20:30:22 +0000195}
196
Duncan P. N. Exon Smith30c92422014-10-01 18:36:03 +0000197void 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);
202 return;
203 }
204
205 if (!isDiagnosticEnabled(DI))
206 return;
Diego Novillodf655012014-04-16 16:53:41 +0000207
Quentin Colombetb4c44d22013-12-17 17:47:22 +0000208 // Otherwise, print the message with a prefix based on the severity.
Alex Lorenz735c47e2015-06-15 20:30:22 +0000209 DiagnosticPrinterRawOStream DP(errs());
210 errs() << getDiagnosticMessagePrefix(DI.getSeverity()) << ": ";
Quentin Colombetb4c44d22013-12-17 17:47:22 +0000211 DI.print(DP);
Alex Lorenz735c47e2015-06-15 20:30:22 +0000212 errs() << "\n";
213 if (DI.getSeverity() == DS_Error)
Quentin Colombetb4c44d22013-12-17 17:47:22 +0000214 exit(1);
Quentin Colombetb4c44d22013-12-17 17:47:22 +0000215}
216
Chris Lattnere22e6132012-01-03 23:47:05 +0000217void LLVMContext::emitError(unsigned LocCookie, const Twine &ErrorStr) {
Quentin Colombetdec8d552014-02-22 00:34:11 +0000218 diagnose(DiagnosticInfoInlineAsm(LocCookie, ErrorStr));
Chris Lattner1e457892010-04-07 23:40:44 +0000219}
220
221//===----------------------------------------------------------------------===//
222// Metadata Kind Uniquing
223//===----------------------------------------------------------------------===//
224
Filipe Cabecinhas1ac0d2d2015-06-02 21:25:00 +0000225/// Return a unique non-zero ID for the specified metadata kind.
Chris Lattnera3b94ba2010-03-30 20:48:48 +0000226unsigned LLVMContext::getMDKindID(StringRef Name) const {
Chris Lattnera3b94ba2010-03-30 20:48:48 +0000227 // If this is new, assign it its ID.
Filipe Cabecinhas1ac0d2d2015-06-02 21:25:00 +0000228 return pImpl->CustomMDKindNames.insert(
229 std::make_pair(
230 Name, pImpl->CustomMDKindNames.size()))
David Blaikie5106ce72014-11-19 05:49:42 +0000231 .first->second;
Chris Lattnera3b94ba2010-03-30 20:48:48 +0000232}
233
Sanjay Patel41043372015-08-24 23:20:16 +0000234/// getHandlerNames - Populate client-supplied smallvector using custom
Chris Lattnera3b94ba2010-03-30 20:48:48 +0000235/// metadata name and ID.
236void LLVMContext::getMDKindNames(SmallVectorImpl<StringRef> &Names) const {
Dan Gohman43aa8f02010-07-20 21:42:28 +0000237 Names.resize(pImpl->CustomMDKindNames.size());
Chris Lattnera3b94ba2010-03-30 20:48:48 +0000238 for (StringMap<unsigned>::const_iterator I = pImpl->CustomMDKindNames.begin(),
239 E = pImpl->CustomMDKindNames.end(); I != E; ++I)
Chris Lattnera3b94ba2010-03-30 20:48:48 +0000240 Names[I->second] = I->first();
241}
Sanjoy Das9303c242015-09-24 19:14:18 +0000242
243void LLVMContext::getOperandBundleTags(SmallVectorImpl<StringRef> &Tags) const {
244 pImpl->getOperandBundleTags(Tags);
245}
246
247uint32_t LLVMContext::getOperandBundleTagID(StringRef Tag) const {
248 return pImpl->getOperandBundleTagID(Tag);
249}
Mehdi Amini599ebf22016-01-08 02:28:20 +0000250
251void LLVMContext::setGC(const Function &Fn, std::string GCName) {
252 auto It = pImpl->GCNames.find(&Fn);
253
254 if (It == pImpl->GCNames.end()) {
255 pImpl->GCNames.insert(std::make_pair(&Fn, std::move(GCName)));
256 return;
257 }
258 It->second = std::move(GCName);
259}
Eugene Zelenko5354a8a2016-04-28 18:04:41 +0000260
Mehdi Amini599ebf22016-01-08 02:28:20 +0000261const std::string &LLVMContext::getGC(const Function &Fn) {
262 return pImpl->GCNames[&Fn];
263}
Eugene Zelenko5354a8a2016-04-28 18:04:41 +0000264
Mehdi Amini599ebf22016-01-08 02:28:20 +0000265void LLVMContext::deleteGC(const Function &Fn) {
266 pImpl->GCNames.erase(&Fn);
267}
Mehdi Amini09b4a8d2016-03-10 01:28:54 +0000268
Mehdi Aminie7090152016-04-02 03:59:58 +0000269bool LLVMContext::shouldDiscardValueNames() const {
270 return pImpl->DiscardValueNames;
271}
Mehdi Amini09b4a8d2016-03-10 01:28:54 +0000272
Duncan P. N. Exon Smithed8fdb22016-04-19 04:55:25 +0000273bool LLVMContext::isODRUniquingDebugTypes() const { return !!pImpl->DITypeMap; }
Duncan P. N. Exon Smith5ab2be02016-04-17 03:58:21 +0000274
Duncan P. N. Exon Smithed8fdb22016-04-19 04:55:25 +0000275void LLVMContext::enableDebugTypeODRUniquing() {
Duncan P. N. Exon Smith5ab2be02016-04-17 03:58:21 +0000276 if (pImpl->DITypeMap)
277 return;
278
Duncan P. N. Exon Smithe8b555c2016-04-19 16:06:50 +0000279 pImpl->DITypeMap.emplace();
Duncan P. N. Exon Smith5ab2be02016-04-17 03:58:21 +0000280}
281
Duncan P. N. Exon Smithed8fdb22016-04-19 04:55:25 +0000282void LLVMContext::disableDebugTypeODRUniquing() { pImpl->DITypeMap.reset(); }
Duncan P. N. Exon Smith5ab2be02016-04-17 03:58:21 +0000283
Mehdi Amini09b4a8d2016-03-10 01:28:54 +0000284void LLVMContext::setDiscardValueNames(bool Discard) {
285 pImpl->DiscardValueNames = Discard;
286}
Andrew Kayloraa641a52016-04-22 22:06:11 +0000287
288OptBisect &LLVMContext::getOptBisect() {
289 return pImpl->getOptBisect();
290}