blob: 94934b385de876f4d7b3350d08e511c6a23bbba8 [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.
38
Bob Wilsonec3ff9c2010-12-17 23:06:32 +000039 // Create the 'dbg' metadata kind.
Chris Lattnerc263b422010-03-30 23:03:27 +000040 unsigned DbgID = getMDKindID("dbg");
41 assert(DbgID == MD_dbg && "dbg kind id drifted"); (void)DbgID;
Dan Gohman41f14cf2010-09-14 21:25:10 +000042
43 // Create the 'tbaa' metadata kind.
44 unsigned TBAAID = getMDKindID("tbaa");
45 assert(TBAAID == MD_tbaa && "tbaa kind id drifted"); (void)TBAAID;
Jakub Staszak3f158fd2011-07-06 18:22:43 +000046
47 // Create the 'prof' metadata kind.
48 unsigned ProfID = getMDKindID("prof");
49 assert(ProfID == MD_prof && "prof kind id drifted"); (void)ProfID;
Peter Collingbournef7d1e7b2011-10-27 19:19:14 +000050
Duncan Sands34bd91a2012-04-14 12:36:06 +000051 // Create the 'fpmath' metadata kind.
52 unsigned FPAccuracyID = getMDKindID("fpmath");
53 assert(FPAccuracyID == MD_fpmath && "fpmath kind id drifted");
Peter Collingbournef7d1e7b2011-10-27 19:19:14 +000054 (void)FPAccuracyID;
Rafael Espindolaef9f5502012-03-24 00:14:51 +000055
56 // Create the 'range' metadata kind.
57 unsigned RangeID = getMDKindID("range");
58 assert(RangeID == MD_range && "range kind id drifted");
59 (void)RangeID;
Dan Gohman3effe812012-09-13 17:56:17 +000060
61 // Create the 'tbaa.struct' metadata kind.
62 unsigned TBAAStructID = getMDKindID("tbaa.struct");
63 assert(TBAAStructID == MD_tbaa_struct && "tbaa.struct kind id drifted");
64 (void)TBAAStructID;
Shuxin Yang408bdad2013-03-06 17:48:48 +000065
66 // Create the 'invariant.load' metadata kind.
67 unsigned InvariantLdId = getMDKindID("invariant.load");
68 assert(InvariantLdId == MD_invariant_load && "invariant.load kind id drifted");
69 (void)InvariantLdId;
Hal Finkel94146652014-07-24 14:25:39 +000070
71 // Create the 'alias.scope' metadata kind.
72 unsigned AliasScopeID = getMDKindID("alias.scope");
73 assert(AliasScopeID == MD_alias_scope && "alias.scope kind id drifted");
74 (void)AliasScopeID;
75
76 // Create the 'noalias' metadata kind.
77 unsigned NoAliasID = getMDKindID("noalias");
78 assert(NoAliasID == MD_noalias && "noalias kind id drifted");
79 (void)NoAliasID;
Philip Reames5a3f5f72014-10-21 00:13:20 +000080
81 // Create the 'nontemporal' metadata kind.
82 unsigned NonTemporalID = getMDKindID("nontemporal");
83 assert(NonTemporalID == MD_nontemporal && "nontemporal kind id drifted");
84 (void)NonTemporalID;
85
86 // Create the 'llvm.mem.parallel_loop_access' metadata kind.
87 unsigned MemParallelLoopAccessID = getMDKindID("llvm.mem.parallel_loop_access");
88 assert(MemParallelLoopAccessID == MD_mem_parallel_loop_access &&
89 "mem_parallel_loop_access kind id drifted");
90 (void)MemParallelLoopAccessID;
91
Philip Reames5a3f5f72014-10-21 00:13:20 +000092 // Create the 'nonnull' metadata kind.
93 unsigned NonNullID = getMDKindID("nonnull");
94 assert(NonNullID == MD_nonnull && "nonnull kind id drifted");
95 (void)NonNullID;
Sanjoy Dasf9995472015-05-19 20:10:19 +000096
97 // Create the 'dereferenceable' metadata kind.
98 unsigned DereferenceableID = getMDKindID("dereferenceable");
99 assert(DereferenceableID == MD_dereferenceable &&
100 "dereferenceable kind id drifted");
101 (void)DereferenceableID;
102
103 // Create the 'dereferenceable_or_null' metadata kind.
104 unsigned DereferenceableOrNullID = getMDKindID("dereferenceable_or_null");
105 assert(DereferenceableOrNullID == MD_dereferenceable_or_null &&
106 "dereferenceable_or_null kind id drifted");
107 (void)DereferenceableOrNullID;
Chen Li00038782015-08-04 04:41:34 +0000108
109 // Create the 'make.implicit' metadata kind.
110 unsigned MakeImplicitID = getMDKindID("make.implicit");
111 assert(MakeImplicitID == MD_make_implicit &&
112 "make.implicit kind id drifted");
113 (void)MakeImplicitID;
Sanjay Patela99ab1f2015-09-02 19:06:43 +0000114
115 // Create the 'unpredictable' metadata kind.
116 unsigned UnpredictableID = getMDKindID("unpredictable");
117 assert(UnpredictableID == MD_unpredictable &&
118 "unpredictable kind id drifted");
119 (void)UnpredictableID;
Piotr Padlewskiea092882015-09-17 20:25:07 +0000120
121 // Create the 'invariant.group' metadata kind.
122 unsigned InvariantGroupId = getMDKindID("invariant.group");
123 assert(InvariantGroupId == MD_invariant_group &&
124 "invariant.group kind id drifted");
125 (void)InvariantGroupId;
126
Artur Pilipenkob4d00902015-09-28 17:41:08 +0000127 // Create the 'align' metadata kind.
128 unsigned AlignID = getMDKindID("align");
129 assert(AlignID == MD_align && "align kind id drifted");
130 (void)AlignID;
Sanjoy Dascdafd842015-11-11 21:38:02 +0000131
Duncan P. N. Exon Smith1d15a9f2016-03-25 00:35:38 +0000132 // Create the 'llvm.loop' metadata kind.
133 unsigned LoopID = getMDKindID("llvm.loop");
134 assert(LoopID == MD_loop && "llvm.loop kind id drifted");
135 (void)LoopID;
136
Peter Collingbourne7efd7502016-06-24 21:21:32 +0000137 unsigned TypeID = getMDKindID("type");
138 assert(TypeID == MD_type && "type kind id drifted");
139 (void)TypeID;
140
Dehao Chen302b69c2016-10-18 20:42:47 +0000141 unsigned SectionPrefixID = getMDKindID("section_prefix");
142 assert(SectionPrefixID == MD_section_prefix &&
143 "section_prefix kind id drifted");
144 (void)SectionPrefixID;
145
Sanjoy Dascdafd842015-11-11 21:38:02 +0000146 auto *DeoptEntry = pImpl->getOrInsertBundleTag("deopt");
147 assert(DeoptEntry->second == LLVMContext::OB_deopt &&
148 "deopt operand bundle id drifted!");
149 (void)DeoptEntry;
David Majnemer3bb88c02015-12-15 21:27:27 +0000150
151 auto *FuncletEntry = pImpl->getOrInsertBundleTag("funclet");
152 assert(FuncletEntry->second == LLVMContext::OB_funclet &&
153 "funclet operand bundle id drifted!");
154 (void)FuncletEntry;
Sanjoy Dasa34ce952016-01-20 19:50:25 +0000155
156 auto *GCTransitionEntry = pImpl->getOrInsertBundleTag("gc-transition");
157 assert(GCTransitionEntry->second == LLVMContext::OB_gc_transition &&
158 "gc-transition operand bundle id drifted!");
159 (void)GCTransitionEntry;
Chris Lattnerc263b422010-03-30 23:03:27 +0000160}
Eugene Zelenko5354a8a2016-04-28 18:04:41 +0000161
Owen Anderson8e66e0b2009-06-30 00:48:55 +0000162LLVMContext::~LLVMContext() { delete pImpl; }
Owen Andersonafd0c4c2009-08-04 22:41:48 +0000163
Owen Anderson8e89e412010-09-08 18:03:32 +0000164void LLVMContext::addModule(Module *M) {
165 pImpl->OwnedModules.insert(M);
166}
167
168void LLVMContext::removeModule(Module *M) {
169 pImpl->OwnedModules.erase(M);
170}
171
Chris Lattner1e457892010-04-07 23:40:44 +0000172//===----------------------------------------------------------------------===//
173// Recoverable Backend Errors
174//===----------------------------------------------------------------------===//
175
Bob Wilsona594fab2013-02-11 05:37:07 +0000176void LLVMContext::
177setInlineAsmDiagnosticHandler(InlineAsmDiagHandlerTy DiagHandler,
178 void *DiagContext) {
179 pImpl->InlineAsmDiagHandler = DiagHandler;
180 pImpl->InlineAsmDiagContext = DiagContext;
Chris Lattner60955d42010-04-06 00:44:45 +0000181}
182
Bob Wilsona594fab2013-02-11 05:37:07 +0000183/// getInlineAsmDiagnosticHandler - Return the diagnostic handler set by
184/// setInlineAsmDiagnosticHandler.
185LLVMContext::InlineAsmDiagHandlerTy
186LLVMContext::getInlineAsmDiagnosticHandler() const {
187 return pImpl->InlineAsmDiagHandler;
Chris Lattner60955d42010-04-06 00:44:45 +0000188}
189
Bob Wilsona594fab2013-02-11 05:37:07 +0000190/// getInlineAsmDiagnosticContext - Return the diagnostic context set by
191/// setInlineAsmDiagnosticHandler.
192void *LLVMContext::getInlineAsmDiagnosticContext() const {
193 return pImpl->InlineAsmDiagContext;
Chris Lattner60955d42010-04-06 00:44:45 +0000194}
Chris Lattnera3b94ba2010-03-30 20:48:48 +0000195
Quentin Colombetb4c44d22013-12-17 17:47:22 +0000196void LLVMContext::setDiagnosticHandler(DiagnosticHandlerTy DiagnosticHandler,
Duncan P. N. Exon Smith30c92422014-10-01 18:36:03 +0000197 void *DiagnosticContext,
198 bool RespectFilters) {
Quentin Colombetb4c44d22013-12-17 17:47:22 +0000199 pImpl->DiagnosticHandler = DiagnosticHandler;
200 pImpl->DiagnosticContext = DiagnosticContext;
Duncan P. N. Exon Smith30c92422014-10-01 18:36:03 +0000201 pImpl->RespectDiagnosticFilters = RespectFilters;
Quentin Colombetb4c44d22013-12-17 17:47:22 +0000202}
203
Adam Nemetaad81602016-07-15 17:23:20 +0000204void LLVMContext::setDiagnosticHotnessRequested(bool Requested) {
205 pImpl->DiagnosticHotnessRequested = Requested;
206}
207bool LLVMContext::getDiagnosticHotnessRequested() const {
208 return pImpl->DiagnosticHotnessRequested;
209}
210
Adam Nemeta62b7e12016-09-27 20:55:07 +0000211yaml::Output *LLVMContext::getDiagnosticsOutputFile() {
212 return pImpl->DiagnosticsOutputFile.get();
213}
214
Mehdi Amini6f408362016-11-19 18:19:41 +0000215void LLVMContext::setDiagnosticsOutputFile(std::unique_ptr<yaml::Output> F) {
216 pImpl->DiagnosticsOutputFile = std::move(F);
Adam Nemeta62b7e12016-09-27 20:55:07 +0000217}
218
Quentin Colombetb4c44d22013-12-17 17:47:22 +0000219LLVMContext::DiagnosticHandlerTy LLVMContext::getDiagnosticHandler() const {
220 return pImpl->DiagnosticHandler;
221}
222
223void *LLVMContext::getDiagnosticContext() const {
224 return pImpl->DiagnosticContext;
225}
226
Juergen Ributzka34390c72014-05-16 02:33:15 +0000227void LLVMContext::setYieldCallback(YieldCallbackTy Callback, void *OpaqueHandle)
228{
229 pImpl->YieldCallback = Callback;
230 pImpl->YieldOpaqueHandle = OpaqueHandle;
231}
232
233void LLVMContext::yield() {
234 if (pImpl->YieldCallback)
235 pImpl->YieldCallback(this, pImpl->YieldOpaqueHandle);
236}
237
Chris Lattnere22e6132012-01-03 23:47:05 +0000238void LLVMContext::emitError(const Twine &ErrorStr) {
Quentin Colombetdec8d552014-02-22 00:34:11 +0000239 diagnose(DiagnosticInfoInlineAsm(ErrorStr));
Chris Lattner1e457892010-04-07 23:40:44 +0000240}
241
Bob Wilsonbfb44ef2013-02-08 21:48:29 +0000242void LLVMContext::emitError(const Instruction *I, const Twine &ErrorStr) {
Quentin Colombetdec8d552014-02-22 00:34:11 +0000243 assert (I && "Invalid instruction");
244 diagnose(DiagnosticInfoInlineAsm(*I, ErrorStr));
Chris Lattner1e457892010-04-07 23:40:44 +0000245}
246
Duncan P. N. Exon Smith30c92422014-10-01 18:36:03 +0000247static bool isDiagnosticEnabled(const DiagnosticInfo &DI) {
Diego Novillo7f8af8b2014-05-22 14:19:46 +0000248 // Optimization remarks are selective. They need to check whether the regexp
249 // pattern, passed via one of the -pass-remarks* flags, matches the name of
250 // the pass that is emitting the diagnostic. If there is no match, ignore the
251 // diagnostic and return.
Akira Hatanakae9148dd2016-04-01 00:34:39 +0000252 if (auto *Remark = dyn_cast<DiagnosticInfoOptimizationBase>(&DI))
253 return Remark->isEnabled();
254
Duncan P. N. Exon Smith30c92422014-10-01 18:36:03 +0000255 return true;
256}
257
Renato Golin4b9c0d42016-05-16 14:28:02 +0000258const char *
259LLVMContext::getDiagnosticMessagePrefix(DiagnosticSeverity Severity) {
Alex Lorenz735c47e2015-06-15 20:30:22 +0000260 switch (Severity) {
261 case DS_Error:
262 return "error";
263 case DS_Warning:
264 return "warning";
265 case DS_Remark:
266 return "remark";
267 case DS_Note:
268 return "note";
269 }
Aaron Ballman52204762015-06-16 13:14:59 +0000270 llvm_unreachable("Unknown DiagnosticSeverity");
Alex Lorenz735c47e2015-06-15 20:30:22 +0000271}
272
Duncan P. N. Exon Smith30c92422014-10-01 18:36:03 +0000273void LLVMContext::diagnose(const DiagnosticInfo &DI) {
274 // If there is a report handler, use it.
275 if (pImpl->DiagnosticHandler) {
276 if (!pImpl->RespectDiagnosticFilters || isDiagnosticEnabled(DI))
277 pImpl->DiagnosticHandler(DI, pImpl->DiagnosticContext);
278 return;
279 }
280
281 if (!isDiagnosticEnabled(DI))
282 return;
Diego Novillodf655012014-04-16 16:53:41 +0000283
Quentin Colombetb4c44d22013-12-17 17:47:22 +0000284 // Otherwise, print the message with a prefix based on the severity.
Alex Lorenz735c47e2015-06-15 20:30:22 +0000285 DiagnosticPrinterRawOStream DP(errs());
286 errs() << getDiagnosticMessagePrefix(DI.getSeverity()) << ": ";
Quentin Colombetb4c44d22013-12-17 17:47:22 +0000287 DI.print(DP);
Alex Lorenz735c47e2015-06-15 20:30:22 +0000288 errs() << "\n";
289 if (DI.getSeverity() == DS_Error)
Quentin Colombetb4c44d22013-12-17 17:47:22 +0000290 exit(1);
Quentin Colombetb4c44d22013-12-17 17:47:22 +0000291}
292
Chris Lattnere22e6132012-01-03 23:47:05 +0000293void LLVMContext::emitError(unsigned LocCookie, const Twine &ErrorStr) {
Quentin Colombetdec8d552014-02-22 00:34:11 +0000294 diagnose(DiagnosticInfoInlineAsm(LocCookie, ErrorStr));
Chris Lattner1e457892010-04-07 23:40:44 +0000295}
296
297//===----------------------------------------------------------------------===//
298// Metadata Kind Uniquing
299//===----------------------------------------------------------------------===//
300
Filipe Cabecinhas1ac0d2d2015-06-02 21:25:00 +0000301/// Return a unique non-zero ID for the specified metadata kind.
Chris Lattnera3b94ba2010-03-30 20:48:48 +0000302unsigned LLVMContext::getMDKindID(StringRef Name) const {
Chris Lattnera3b94ba2010-03-30 20:48:48 +0000303 // If this is new, assign it its ID.
Filipe Cabecinhas1ac0d2d2015-06-02 21:25:00 +0000304 return pImpl->CustomMDKindNames.insert(
305 std::make_pair(
306 Name, pImpl->CustomMDKindNames.size()))
David Blaikie5106ce72014-11-19 05:49:42 +0000307 .first->second;
Chris Lattnera3b94ba2010-03-30 20:48:48 +0000308}
309
Sanjay Patel41043372015-08-24 23:20:16 +0000310/// getHandlerNames - Populate client-supplied smallvector using custom
Chris Lattnera3b94ba2010-03-30 20:48:48 +0000311/// metadata name and ID.
312void LLVMContext::getMDKindNames(SmallVectorImpl<StringRef> &Names) const {
Dan Gohman43aa8f02010-07-20 21:42:28 +0000313 Names.resize(pImpl->CustomMDKindNames.size());
Chris Lattnera3b94ba2010-03-30 20:48:48 +0000314 for (StringMap<unsigned>::const_iterator I = pImpl->CustomMDKindNames.begin(),
315 E = pImpl->CustomMDKindNames.end(); I != E; ++I)
Chris Lattnera3b94ba2010-03-30 20:48:48 +0000316 Names[I->second] = I->first();
317}
Sanjoy Das9303c242015-09-24 19:14:18 +0000318
319void LLVMContext::getOperandBundleTags(SmallVectorImpl<StringRef> &Tags) const {
320 pImpl->getOperandBundleTags(Tags);
321}
322
323uint32_t LLVMContext::getOperandBundleTagID(StringRef Tag) const {
324 return pImpl->getOperandBundleTagID(Tag);
325}
Mehdi Amini599ebf22016-01-08 02:28:20 +0000326
327void LLVMContext::setGC(const Function &Fn, std::string GCName) {
328 auto It = pImpl->GCNames.find(&Fn);
329
330 if (It == pImpl->GCNames.end()) {
331 pImpl->GCNames.insert(std::make_pair(&Fn, std::move(GCName)));
332 return;
333 }
334 It->second = std::move(GCName);
335}
Eugene Zelenko5354a8a2016-04-28 18:04:41 +0000336
Mehdi Amini599ebf22016-01-08 02:28:20 +0000337const std::string &LLVMContext::getGC(const Function &Fn) {
338 return pImpl->GCNames[&Fn];
339}
Eugene Zelenko5354a8a2016-04-28 18:04:41 +0000340
Mehdi Amini599ebf22016-01-08 02:28:20 +0000341void LLVMContext::deleteGC(const Function &Fn) {
342 pImpl->GCNames.erase(&Fn);
343}
Mehdi Amini09b4a8d2016-03-10 01:28:54 +0000344
Mehdi Aminie7090152016-04-02 03:59:58 +0000345bool LLVMContext::shouldDiscardValueNames() const {
346 return pImpl->DiscardValueNames;
347}
Mehdi Amini09b4a8d2016-03-10 01:28:54 +0000348
Duncan P. N. Exon Smithed8fdb22016-04-19 04:55:25 +0000349bool LLVMContext::isODRUniquingDebugTypes() const { return !!pImpl->DITypeMap; }
Duncan P. N. Exon Smith5ab2be02016-04-17 03:58:21 +0000350
Duncan P. N. Exon Smithed8fdb22016-04-19 04:55:25 +0000351void LLVMContext::enableDebugTypeODRUniquing() {
Duncan P. N. Exon Smith5ab2be02016-04-17 03:58:21 +0000352 if (pImpl->DITypeMap)
353 return;
354
Duncan P. N. Exon Smithe8b555c2016-04-19 16:06:50 +0000355 pImpl->DITypeMap.emplace();
Duncan P. N. Exon Smith5ab2be02016-04-17 03:58:21 +0000356}
357
Duncan P. N. Exon Smithed8fdb22016-04-19 04:55:25 +0000358void LLVMContext::disableDebugTypeODRUniquing() { pImpl->DITypeMap.reset(); }
Duncan P. N. Exon Smith5ab2be02016-04-17 03:58:21 +0000359
Mehdi Amini09b4a8d2016-03-10 01:28:54 +0000360void LLVMContext::setDiscardValueNames(bool Discard) {
361 pImpl->DiscardValueNames = Discard;
362}
Andrew Kayloraa641a52016-04-22 22:06:11 +0000363
364OptBisect &LLVMContext::getOptBisect() {
365 return pImpl->getOptBisect();
366}