blob: 48b53b0f532afc3e98179ba65ad3b7c68b7aa668 [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 Carruthed0881b2012-12-03 16:50:05 +000016#include "LLVMContextImpl.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000017#include "llvm/IR/Constants.h"
Diego Novilloa9298b22014-04-08 16:42:34 +000018#include "llvm/IR/DebugLoc.h"
Quentin Colombetb4c44d22013-12-17 17:47:22 +000019#include "llvm/IR/DiagnosticInfo.h"
20#include "llvm/IR/DiagnosticPrinter.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000021#include "llvm/IR/Instruction.h"
22#include "llvm/IR/Metadata.h"
Owen Anderson1938fb12009-06-30 23:39:59 +000023#include "llvm/Support/ManagedStatic.h"
Chris Lattner1e457892010-04-07 23:40:44 +000024#include "llvm/Support/SourceMgr.h"
Nick Lewycky0de20af2010-12-19 20:43:38 +000025#include <cctype>
Owen Anderson8e66e0b2009-06-30 00:48:55 +000026using namespace llvm;
27
Owen Anderson1938fb12009-06-30 23:39:59 +000028static ManagedStatic<LLVMContext> GlobalContext;
29
Owen Anderson2a154432009-07-01 23:13:44 +000030LLVMContext& llvm::getGlobalContext() {
Owen Anderson1cf085d2009-07-01 21:22:36 +000031 return *GlobalContext;
Owen Anderson1938fb12009-06-30 23:39:59 +000032}
33
Chris Lattnerc263b422010-03-30 23:03:27 +000034LLVMContext::LLVMContext() : pImpl(new LLVMContextImpl(*this)) {
Dan Gohman41f14cf2010-09-14 21:25:10 +000035 // Create the fixed metadata kinds. This is done in the same order as the
36 // MD_* enum values so that they correspond.
37
Bob Wilsonec3ff9c2010-12-17 23:06:32 +000038 // Create the 'dbg' metadata kind.
Chris Lattnerc263b422010-03-30 23:03:27 +000039 unsigned DbgID = getMDKindID("dbg");
40 assert(DbgID == MD_dbg && "dbg kind id drifted"); (void)DbgID;
Dan Gohman41f14cf2010-09-14 21:25:10 +000041
42 // Create the 'tbaa' metadata kind.
43 unsigned TBAAID = getMDKindID("tbaa");
44 assert(TBAAID == MD_tbaa && "tbaa kind id drifted"); (void)TBAAID;
Jakub Staszak3f158fd2011-07-06 18:22:43 +000045
46 // Create the 'prof' metadata kind.
47 unsigned ProfID = getMDKindID("prof");
48 assert(ProfID == MD_prof && "prof kind id drifted"); (void)ProfID;
Peter Collingbournef7d1e7b2011-10-27 19:19:14 +000049
Duncan Sands34bd91a2012-04-14 12:36:06 +000050 // Create the 'fpmath' metadata kind.
51 unsigned FPAccuracyID = getMDKindID("fpmath");
52 assert(FPAccuracyID == MD_fpmath && "fpmath kind id drifted");
Peter Collingbournef7d1e7b2011-10-27 19:19:14 +000053 (void)FPAccuracyID;
Rafael Espindolaef9f5502012-03-24 00:14:51 +000054
55 // Create the 'range' metadata kind.
56 unsigned RangeID = getMDKindID("range");
57 assert(RangeID == MD_range && "range kind id drifted");
58 (void)RangeID;
Dan Gohman3effe812012-09-13 17:56:17 +000059
60 // Create the 'tbaa.struct' metadata kind.
61 unsigned TBAAStructID = getMDKindID("tbaa.struct");
62 assert(TBAAStructID == MD_tbaa_struct && "tbaa.struct kind id drifted");
63 (void)TBAAStructID;
Shuxin Yang408bdad2013-03-06 17:48:48 +000064
65 // Create the 'invariant.load' metadata kind.
66 unsigned InvariantLdId = getMDKindID("invariant.load");
67 assert(InvariantLdId == MD_invariant_load && "invariant.load kind id drifted");
68 (void)InvariantLdId;
Hal Finkel94146652014-07-24 14:25:39 +000069
70 // Create the 'alias.scope' metadata kind.
71 unsigned AliasScopeID = getMDKindID("alias.scope");
72 assert(AliasScopeID == MD_alias_scope && "alias.scope kind id drifted");
73 (void)AliasScopeID;
74
75 // Create the 'noalias' metadata kind.
76 unsigned NoAliasID = getMDKindID("noalias");
77 assert(NoAliasID == MD_noalias && "noalias kind id drifted");
78 (void)NoAliasID;
Philip Reames5a3f5f72014-10-21 00:13:20 +000079
80 // Create the 'nontemporal' metadata kind.
81 unsigned NonTemporalID = getMDKindID("nontemporal");
82 assert(NonTemporalID == MD_nontemporal && "nontemporal kind id drifted");
83 (void)NonTemporalID;
84
85 // Create the 'llvm.mem.parallel_loop_access' metadata kind.
86 unsigned MemParallelLoopAccessID = getMDKindID("llvm.mem.parallel_loop_access");
87 assert(MemParallelLoopAccessID == MD_mem_parallel_loop_access &&
88 "mem_parallel_loop_access kind id drifted");
89 (void)MemParallelLoopAccessID;
90
Philip Reames5a3f5f72014-10-21 00:13:20 +000091 // Create the 'nonnull' metadata kind.
92 unsigned NonNullID = getMDKindID("nonnull");
93 assert(NonNullID == MD_nonnull && "nonnull kind id drifted");
94 (void)NonNullID;
Sanjoy Dasf9995472015-05-19 20:10:19 +000095
96 // Create the 'dereferenceable' metadata kind.
97 unsigned DereferenceableID = getMDKindID("dereferenceable");
98 assert(DereferenceableID == MD_dereferenceable &&
99 "dereferenceable kind id drifted");
100 (void)DereferenceableID;
101
102 // Create the 'dereferenceable_or_null' metadata kind.
103 unsigned DereferenceableOrNullID = getMDKindID("dereferenceable_or_null");
104 assert(DereferenceableOrNullID == MD_dereferenceable_or_null &&
105 "dereferenceable_or_null kind id drifted");
106 (void)DereferenceableOrNullID;
Chen Li00038782015-08-04 04:41:34 +0000107
108 // Create the 'make.implicit' metadata kind.
109 unsigned MakeImplicitID = getMDKindID("make.implicit");
110 assert(MakeImplicitID == MD_make_implicit &&
111 "make.implicit kind id drifted");
112 (void)MakeImplicitID;
Sanjay Patela99ab1f2015-09-02 19:06:43 +0000113
114 // Create the 'unpredictable' metadata kind.
115 unsigned UnpredictableID = getMDKindID("unpredictable");
116 assert(UnpredictableID == MD_unpredictable &&
117 "unpredictable kind id drifted");
118 (void)UnpredictableID;
Piotr Padlewskiea092882015-09-17 20:25:07 +0000119
120 // Create the 'invariant.group' metadata kind.
121 unsigned InvariantGroupId = getMDKindID("invariant.group");
122 assert(InvariantGroupId == MD_invariant_group &&
123 "invariant.group kind id drifted");
124 (void)InvariantGroupId;
125
Artur Pilipenkob4d00902015-09-28 17:41:08 +0000126 // Create the 'align' metadata kind.
127 unsigned AlignID = getMDKindID("align");
128 assert(AlignID == MD_align && "align kind id drifted");
129 (void)AlignID;
Sanjoy Dascdafd842015-11-11 21:38:02 +0000130
131 auto *DeoptEntry = pImpl->getOrInsertBundleTag("deopt");
132 assert(DeoptEntry->second == LLVMContext::OB_deopt &&
133 "deopt operand bundle id drifted!");
134 (void)DeoptEntry;
David Majnemer3bb88c02015-12-15 21:27:27 +0000135
136 auto *FuncletEntry = pImpl->getOrInsertBundleTag("funclet");
137 assert(FuncletEntry->second == LLVMContext::OB_funclet &&
138 "funclet operand bundle id drifted!");
139 (void)FuncletEntry;
Chris Lattnerc263b422010-03-30 23:03:27 +0000140}
Owen Anderson8e66e0b2009-06-30 00:48:55 +0000141LLVMContext::~LLVMContext() { delete pImpl; }
Owen Andersonafd0c4c2009-08-04 22:41:48 +0000142
Owen Anderson8e89e412010-09-08 18:03:32 +0000143void LLVMContext::addModule(Module *M) {
144 pImpl->OwnedModules.insert(M);
145}
146
147void LLVMContext::removeModule(Module *M) {
148 pImpl->OwnedModules.erase(M);
149}
150
Chris Lattner1e457892010-04-07 23:40:44 +0000151//===----------------------------------------------------------------------===//
152// Recoverable Backend Errors
153//===----------------------------------------------------------------------===//
154
Bob Wilsona594fab2013-02-11 05:37:07 +0000155void LLVMContext::
156setInlineAsmDiagnosticHandler(InlineAsmDiagHandlerTy DiagHandler,
157 void *DiagContext) {
158 pImpl->InlineAsmDiagHandler = DiagHandler;
159 pImpl->InlineAsmDiagContext = DiagContext;
Chris Lattner60955d42010-04-06 00:44:45 +0000160}
161
Bob Wilsona594fab2013-02-11 05:37:07 +0000162/// getInlineAsmDiagnosticHandler - Return the diagnostic handler set by
163/// setInlineAsmDiagnosticHandler.
164LLVMContext::InlineAsmDiagHandlerTy
165LLVMContext::getInlineAsmDiagnosticHandler() const {
166 return pImpl->InlineAsmDiagHandler;
Chris Lattner60955d42010-04-06 00:44:45 +0000167}
168
Bob Wilsona594fab2013-02-11 05:37:07 +0000169/// getInlineAsmDiagnosticContext - Return the diagnostic context set by
170/// setInlineAsmDiagnosticHandler.
171void *LLVMContext::getInlineAsmDiagnosticContext() const {
172 return pImpl->InlineAsmDiagContext;
Chris Lattner60955d42010-04-06 00:44:45 +0000173}
Chris Lattnera3b94ba2010-03-30 20:48:48 +0000174
Quentin Colombetb4c44d22013-12-17 17:47:22 +0000175void LLVMContext::setDiagnosticHandler(DiagnosticHandlerTy DiagnosticHandler,
Duncan P. N. Exon Smith30c92422014-10-01 18:36:03 +0000176 void *DiagnosticContext,
177 bool RespectFilters) {
Quentin Colombetb4c44d22013-12-17 17:47:22 +0000178 pImpl->DiagnosticHandler = DiagnosticHandler;
179 pImpl->DiagnosticContext = DiagnosticContext;
Duncan P. N. Exon Smith30c92422014-10-01 18:36:03 +0000180 pImpl->RespectDiagnosticFilters = RespectFilters;
Quentin Colombetb4c44d22013-12-17 17:47:22 +0000181}
182
183LLVMContext::DiagnosticHandlerTy LLVMContext::getDiagnosticHandler() const {
184 return pImpl->DiagnosticHandler;
185}
186
187void *LLVMContext::getDiagnosticContext() const {
188 return pImpl->DiagnosticContext;
189}
190
Juergen Ributzka34390c72014-05-16 02:33:15 +0000191void LLVMContext::setYieldCallback(YieldCallbackTy Callback, void *OpaqueHandle)
192{
193 pImpl->YieldCallback = Callback;
194 pImpl->YieldOpaqueHandle = OpaqueHandle;
195}
196
197void LLVMContext::yield() {
198 if (pImpl->YieldCallback)
199 pImpl->YieldCallback(this, pImpl->YieldOpaqueHandle);
200}
201
Chris Lattnere22e6132012-01-03 23:47:05 +0000202void LLVMContext::emitError(const Twine &ErrorStr) {
Quentin Colombetdec8d552014-02-22 00:34:11 +0000203 diagnose(DiagnosticInfoInlineAsm(ErrorStr));
Chris Lattner1e457892010-04-07 23:40:44 +0000204}
205
Bob Wilsonbfb44ef2013-02-08 21:48:29 +0000206void LLVMContext::emitError(const Instruction *I, const Twine &ErrorStr) {
Quentin Colombetdec8d552014-02-22 00:34:11 +0000207 assert (I && "Invalid instruction");
208 diagnose(DiagnosticInfoInlineAsm(*I, ErrorStr));
Chris Lattner1e457892010-04-07 23:40:44 +0000209}
210
Duncan P. N. Exon Smith30c92422014-10-01 18:36:03 +0000211static bool isDiagnosticEnabled(const DiagnosticInfo &DI) {
Diego Novillo7f8af8b2014-05-22 14:19:46 +0000212 // Optimization remarks are selective. They need to check whether the regexp
213 // pattern, passed via one of the -pass-remarks* flags, matches the name of
214 // the pass that is emitting the diagnostic. If there is no match, ignore the
215 // diagnostic and return.
216 switch (DI.getKind()) {
217 case llvm::DK_OptimizationRemark:
Diego Novillo0b761a42014-05-22 17:19:01 +0000218 if (!cast<DiagnosticInfoOptimizationRemark>(DI).isEnabled())
Duncan P. N. Exon Smith30c92422014-10-01 18:36:03 +0000219 return false;
Diego Novillo7f8af8b2014-05-22 14:19:46 +0000220 break;
221 case llvm::DK_OptimizationRemarkMissed:
Diego Novillo0b761a42014-05-22 17:19:01 +0000222 if (!cast<DiagnosticInfoOptimizationRemarkMissed>(DI).isEnabled())
Duncan P. N. Exon Smith30c92422014-10-01 18:36:03 +0000223 return false;
Diego Novillo7f8af8b2014-05-22 14:19:46 +0000224 break;
225 case llvm::DK_OptimizationRemarkAnalysis:
Diego Novillo0b761a42014-05-22 17:19:01 +0000226 if (!cast<DiagnosticInfoOptimizationRemarkAnalysis>(DI).isEnabled())
Duncan P. N. Exon Smith30c92422014-10-01 18:36:03 +0000227 return false;
Diego Novillo7f8af8b2014-05-22 14:19:46 +0000228 break;
Tyler Nowickic1a86f52015-08-10 19:51:46 +0000229 case llvm::DK_OptimizationRemarkAnalysisFPCommute:
230 if (!cast<DiagnosticInfoOptimizationRemarkAnalysisFPCommute>(DI)
231 .isEnabled())
232 return false;
233 break;
Diego Novillo7f8af8b2014-05-22 14:19:46 +0000234 default:
235 break;
236 }
Duncan P. N. Exon Smith30c92422014-10-01 18:36:03 +0000237 return true;
238}
239
Alex Lorenz735c47e2015-06-15 20:30:22 +0000240static const char *getDiagnosticMessagePrefix(DiagnosticSeverity Severity) {
241 switch (Severity) {
242 case DS_Error:
243 return "error";
244 case DS_Warning:
245 return "warning";
246 case DS_Remark:
247 return "remark";
248 case DS_Note:
249 return "note";
250 }
Aaron Ballman52204762015-06-16 13:14:59 +0000251 llvm_unreachable("Unknown DiagnosticSeverity");
Alex Lorenz735c47e2015-06-15 20:30:22 +0000252}
253
Duncan P. N. Exon Smith30c92422014-10-01 18:36:03 +0000254void LLVMContext::diagnose(const DiagnosticInfo &DI) {
255 // If there is a report handler, use it.
256 if (pImpl->DiagnosticHandler) {
257 if (!pImpl->RespectDiagnosticFilters || isDiagnosticEnabled(DI))
258 pImpl->DiagnosticHandler(DI, pImpl->DiagnosticContext);
259 return;
260 }
261
262 if (!isDiagnosticEnabled(DI))
263 return;
Diego Novillodf655012014-04-16 16:53:41 +0000264
Quentin Colombetb4c44d22013-12-17 17:47:22 +0000265 // Otherwise, print the message with a prefix based on the severity.
Alex Lorenz735c47e2015-06-15 20:30:22 +0000266 DiagnosticPrinterRawOStream DP(errs());
267 errs() << getDiagnosticMessagePrefix(DI.getSeverity()) << ": ";
Quentin Colombetb4c44d22013-12-17 17:47:22 +0000268 DI.print(DP);
Alex Lorenz735c47e2015-06-15 20:30:22 +0000269 errs() << "\n";
270 if (DI.getSeverity() == DS_Error)
Quentin Colombetb4c44d22013-12-17 17:47:22 +0000271 exit(1);
Quentin Colombetb4c44d22013-12-17 17:47:22 +0000272}
273
Chris Lattnere22e6132012-01-03 23:47:05 +0000274void LLVMContext::emitError(unsigned LocCookie, const Twine &ErrorStr) {
Quentin Colombetdec8d552014-02-22 00:34:11 +0000275 diagnose(DiagnosticInfoInlineAsm(LocCookie, ErrorStr));
Chris Lattner1e457892010-04-07 23:40:44 +0000276}
277
278//===----------------------------------------------------------------------===//
279// Metadata Kind Uniquing
280//===----------------------------------------------------------------------===//
281
Filipe Cabecinhas1ac0d2d2015-06-02 21:25:00 +0000282/// Return a unique non-zero ID for the specified metadata kind.
Chris Lattnera3b94ba2010-03-30 20:48:48 +0000283unsigned LLVMContext::getMDKindID(StringRef Name) const {
Chris Lattnera3b94ba2010-03-30 20:48:48 +0000284 // If this is new, assign it its ID.
Filipe Cabecinhas1ac0d2d2015-06-02 21:25:00 +0000285 return pImpl->CustomMDKindNames.insert(
286 std::make_pair(
287 Name, pImpl->CustomMDKindNames.size()))
David Blaikie5106ce72014-11-19 05:49:42 +0000288 .first->second;
Chris Lattnera3b94ba2010-03-30 20:48:48 +0000289}
290
Sanjay Patel41043372015-08-24 23:20:16 +0000291/// getHandlerNames - Populate client-supplied smallvector using custom
Chris Lattnera3b94ba2010-03-30 20:48:48 +0000292/// metadata name and ID.
293void LLVMContext::getMDKindNames(SmallVectorImpl<StringRef> &Names) const {
Dan Gohman43aa8f02010-07-20 21:42:28 +0000294 Names.resize(pImpl->CustomMDKindNames.size());
Chris Lattnera3b94ba2010-03-30 20:48:48 +0000295 for (StringMap<unsigned>::const_iterator I = pImpl->CustomMDKindNames.begin(),
296 E = pImpl->CustomMDKindNames.end(); I != E; ++I)
Chris Lattnera3b94ba2010-03-30 20:48:48 +0000297 Names[I->second] = I->first();
298}
Sanjoy Das9303c242015-09-24 19:14:18 +0000299
300void LLVMContext::getOperandBundleTags(SmallVectorImpl<StringRef> &Tags) const {
301 pImpl->getOperandBundleTags(Tags);
302}
303
304uint32_t LLVMContext::getOperandBundleTagID(StringRef Tag) const {
305 return pImpl->getOperandBundleTagID(Tag);
306}
Mehdi Amini599ebf22016-01-08 02:28:20 +0000307
308void LLVMContext::setGC(const Function &Fn, std::string GCName) {
309 auto It = pImpl->GCNames.find(&Fn);
310
311 if (It == pImpl->GCNames.end()) {
312 pImpl->GCNames.insert(std::make_pair(&Fn, std::move(GCName)));
313 return;
314 }
315 It->second = std::move(GCName);
316}
317const std::string &LLVMContext::getGC(const Function &Fn) {
318 return pImpl->GCNames[&Fn];
319}
320void LLVMContext::deleteGC(const Function &Fn) {
321 pImpl->GCNames.erase(&Fn);
322}