blob: b6d95c4fcf32c44eec54e5ffdb66dcc0a9ba90b7 [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
91
92 // Create the 'nonnull' metadata kind.
93 unsigned NonNullID = getMDKindID("nonnull");
94 assert(NonNullID == MD_nonnull && "nonnull kind id drifted");
95 (void)NonNullID;
Chris Lattnerc263b422010-03-30 23:03:27 +000096}
Owen Anderson8e66e0b2009-06-30 00:48:55 +000097LLVMContext::~LLVMContext() { delete pImpl; }
Owen Andersonafd0c4c2009-08-04 22:41:48 +000098
Owen Anderson8e89e412010-09-08 18:03:32 +000099void LLVMContext::addModule(Module *M) {
100 pImpl->OwnedModules.insert(M);
101}
102
103void LLVMContext::removeModule(Module *M) {
104 pImpl->OwnedModules.erase(M);
105}
106
Chris Lattner1e457892010-04-07 23:40:44 +0000107//===----------------------------------------------------------------------===//
108// Recoverable Backend Errors
109//===----------------------------------------------------------------------===//
110
Bob Wilsona594fab2013-02-11 05:37:07 +0000111void LLVMContext::
112setInlineAsmDiagnosticHandler(InlineAsmDiagHandlerTy DiagHandler,
113 void *DiagContext) {
114 pImpl->InlineAsmDiagHandler = DiagHandler;
115 pImpl->InlineAsmDiagContext = DiagContext;
Chris Lattner60955d42010-04-06 00:44:45 +0000116}
117
Bob Wilsona594fab2013-02-11 05:37:07 +0000118/// getInlineAsmDiagnosticHandler - Return the diagnostic handler set by
119/// setInlineAsmDiagnosticHandler.
120LLVMContext::InlineAsmDiagHandlerTy
121LLVMContext::getInlineAsmDiagnosticHandler() const {
122 return pImpl->InlineAsmDiagHandler;
Chris Lattner60955d42010-04-06 00:44:45 +0000123}
124
Bob Wilsona594fab2013-02-11 05:37:07 +0000125/// getInlineAsmDiagnosticContext - Return the diagnostic context set by
126/// setInlineAsmDiagnosticHandler.
127void *LLVMContext::getInlineAsmDiagnosticContext() const {
128 return pImpl->InlineAsmDiagContext;
Chris Lattner60955d42010-04-06 00:44:45 +0000129}
Chris Lattnera3b94ba2010-03-30 20:48:48 +0000130
Quentin Colombetb4c44d22013-12-17 17:47:22 +0000131void LLVMContext::setDiagnosticHandler(DiagnosticHandlerTy DiagnosticHandler,
Duncan P. N. Exon Smith30c92422014-10-01 18:36:03 +0000132 void *DiagnosticContext,
133 bool RespectFilters) {
Quentin Colombetb4c44d22013-12-17 17:47:22 +0000134 pImpl->DiagnosticHandler = DiagnosticHandler;
135 pImpl->DiagnosticContext = DiagnosticContext;
Duncan P. N. Exon Smith30c92422014-10-01 18:36:03 +0000136 pImpl->RespectDiagnosticFilters = RespectFilters;
Quentin Colombetb4c44d22013-12-17 17:47:22 +0000137}
138
139LLVMContext::DiagnosticHandlerTy LLVMContext::getDiagnosticHandler() const {
140 return pImpl->DiagnosticHandler;
141}
142
143void *LLVMContext::getDiagnosticContext() const {
144 return pImpl->DiagnosticContext;
145}
146
Juergen Ributzka34390c72014-05-16 02:33:15 +0000147void LLVMContext::setYieldCallback(YieldCallbackTy Callback, void *OpaqueHandle)
148{
149 pImpl->YieldCallback = Callback;
150 pImpl->YieldOpaqueHandle = OpaqueHandle;
151}
152
153void LLVMContext::yield() {
154 if (pImpl->YieldCallback)
155 pImpl->YieldCallback(this, pImpl->YieldOpaqueHandle);
156}
157
Chris Lattnere22e6132012-01-03 23:47:05 +0000158void LLVMContext::emitError(const Twine &ErrorStr) {
Quentin Colombetdec8d552014-02-22 00:34:11 +0000159 diagnose(DiagnosticInfoInlineAsm(ErrorStr));
Chris Lattner1e457892010-04-07 23:40:44 +0000160}
161
Bob Wilsonbfb44ef2013-02-08 21:48:29 +0000162void LLVMContext::emitError(const Instruction *I, const Twine &ErrorStr) {
Quentin Colombetdec8d552014-02-22 00:34:11 +0000163 assert (I && "Invalid instruction");
164 diagnose(DiagnosticInfoInlineAsm(*I, ErrorStr));
Chris Lattner1e457892010-04-07 23:40:44 +0000165}
166
Duncan P. N. Exon Smith30c92422014-10-01 18:36:03 +0000167static bool isDiagnosticEnabled(const DiagnosticInfo &DI) {
Diego Novillo7f8af8b2014-05-22 14:19:46 +0000168 // Optimization remarks are selective. They need to check whether the regexp
169 // pattern, passed via one of the -pass-remarks* flags, matches the name of
170 // the pass that is emitting the diagnostic. If there is no match, ignore the
171 // diagnostic and return.
172 switch (DI.getKind()) {
173 case llvm::DK_OptimizationRemark:
Diego Novillo0b761a42014-05-22 17:19:01 +0000174 if (!cast<DiagnosticInfoOptimizationRemark>(DI).isEnabled())
Duncan P. N. Exon Smith30c92422014-10-01 18:36:03 +0000175 return false;
Diego Novillo7f8af8b2014-05-22 14:19:46 +0000176 break;
177 case llvm::DK_OptimizationRemarkMissed:
Diego Novillo0b761a42014-05-22 17:19:01 +0000178 if (!cast<DiagnosticInfoOptimizationRemarkMissed>(DI).isEnabled())
Duncan P. N. Exon Smith30c92422014-10-01 18:36:03 +0000179 return false;
Diego Novillo7f8af8b2014-05-22 14:19:46 +0000180 break;
181 case llvm::DK_OptimizationRemarkAnalysis:
Diego Novillo0b761a42014-05-22 17:19:01 +0000182 if (!cast<DiagnosticInfoOptimizationRemarkAnalysis>(DI).isEnabled())
Duncan P. N. Exon Smith30c92422014-10-01 18:36:03 +0000183 return false;
Diego Novillo7f8af8b2014-05-22 14:19:46 +0000184 break;
185 default:
186 break;
187 }
Duncan P. N. Exon Smith30c92422014-10-01 18:36:03 +0000188 return true;
189}
190
191void LLVMContext::diagnose(const DiagnosticInfo &DI) {
192 // If there is a report handler, use it.
193 if (pImpl->DiagnosticHandler) {
194 if (!pImpl->RespectDiagnosticFilters || isDiagnosticEnabled(DI))
195 pImpl->DiagnosticHandler(DI, pImpl->DiagnosticContext);
196 return;
197 }
198
199 if (!isDiagnosticEnabled(DI))
200 return;
Diego Novillodf655012014-04-16 16:53:41 +0000201
Quentin Colombetb4c44d22013-12-17 17:47:22 +0000202 // Otherwise, print the message with a prefix based on the severity.
Alp Tokere69170a2014-06-26 22:52:05 +0000203 std::string MsgStorage;
204 raw_string_ostream Stream(MsgStorage);
205 DiagnosticPrinterRawOStream DP(Stream);
Quentin Colombetb4c44d22013-12-17 17:47:22 +0000206 DI.print(DP);
Alp Tokere69170a2014-06-26 22:52:05 +0000207 Stream.flush();
Quentin Colombetb4c44d22013-12-17 17:47:22 +0000208 switch (DI.getSeverity()) {
209 case DS_Error:
Alp Tokere69170a2014-06-26 22:52:05 +0000210 errs() << "error: " << MsgStorage << "\n";
Quentin Colombetb4c44d22013-12-17 17:47:22 +0000211 exit(1);
212 case DS_Warning:
Alp Tokere69170a2014-06-26 22:52:05 +0000213 errs() << "warning: " << MsgStorage << "\n";
Quentin Colombetb4c44d22013-12-17 17:47:22 +0000214 break;
Tobias Grossere8d4c9a2014-02-28 09:08:45 +0000215 case DS_Remark:
Alp Tokere69170a2014-06-26 22:52:05 +0000216 errs() << "remark: " << MsgStorage << "\n";
Tobias Grossere8d4c9a2014-02-28 09:08:45 +0000217 break;
Quentin Colombetb4c44d22013-12-17 17:47:22 +0000218 case DS_Note:
Alp Tokere69170a2014-06-26 22:52:05 +0000219 errs() << "note: " << MsgStorage << "\n";
Quentin Colombetb4c44d22013-12-17 17:47:22 +0000220 break;
221 }
222}
223
Chris Lattnere22e6132012-01-03 23:47:05 +0000224void LLVMContext::emitError(unsigned LocCookie, const Twine &ErrorStr) {
Quentin Colombetdec8d552014-02-22 00:34:11 +0000225 diagnose(DiagnosticInfoInlineAsm(LocCookie, ErrorStr));
Chris Lattner1e457892010-04-07 23:40:44 +0000226}
227
228//===----------------------------------------------------------------------===//
229// Metadata Kind Uniquing
230//===----------------------------------------------------------------------===//
231
Chris Lattnera3b94ba2010-03-30 20:48:48 +0000232/// getMDKindID - Return a unique non-zero ID for the specified metadata kind.
233unsigned LLVMContext::getMDKindID(StringRef Name) const {
Nick Lewyckydde76ff2014-12-11 02:10:28 +0000234 assert(!std::isdigit(Name.front()) &&
235 "Named metadata may not start with a digit");
Dan Gohman43aa8f02010-07-20 21:42:28 +0000236
Chris Lattnera3b94ba2010-03-30 20:48:48 +0000237 // If this is new, assign it its ID.
David Blaikie5106ce72014-11-19 05:49:42 +0000238 return pImpl->CustomMDKindNames.insert(std::make_pair(
239 Name,
240 pImpl->CustomMDKindNames.size()))
241 .first->second;
Chris Lattnera3b94ba2010-03-30 20:48:48 +0000242}
243
244/// getHandlerNames - Populate client supplied smallvector using custome
245/// metadata name and ID.
246void LLVMContext::getMDKindNames(SmallVectorImpl<StringRef> &Names) const {
Dan Gohman43aa8f02010-07-20 21:42:28 +0000247 Names.resize(pImpl->CustomMDKindNames.size());
Chris Lattnera3b94ba2010-03-30 20:48:48 +0000248 for (StringMap<unsigned>::const_iterator I = pImpl->CustomMDKindNames.begin(),
249 E = pImpl->CustomMDKindNames.end(); I != E; ++I)
Chris Lattnera3b94ba2010-03-30 20:48:48 +0000250 Names[I->second] = I->first();
251}