blob: a45b622a71b9c258269ca4528b758ee342ad6a28 [file] [log] [blame]
Chris Lattner8cb2aeb2010-04-01 00:37:44 +00001//===-- DebugLoc.cpp - Implement DebugLoc class ---------------------------===//
2//
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//===----------------------------------------------------------------------===//
9
Chandler Carruth92051402014-03-05 10:30:38 +000010#include "llvm/IR/DebugLoc.h"
Chris Lattner8cb2aeb2010-04-01 00:37:44 +000011#include "LLVMContextImpl.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000012#include "llvm/ADT/DenseMapInfo.h"
Chandler Carruth9a4c9e52014-03-06 00:46:21 +000013#include "llvm/IR/DebugInfo.h"
Chris Lattner8cb2aeb2010-04-01 00:37:44 +000014using namespace llvm;
15
16//===----------------------------------------------------------------------===//
17// DebugLoc Implementation
18//===----------------------------------------------------------------------===//
19
Chris Lattner593916d2010-04-02 20:21:22 +000020MDNode *DebugLoc::getScope(const LLVMContext &Ctx) const {
Craig Topperc6207612014-04-09 06:08:46 +000021 if (ScopeIdx == 0) return nullptr;
Chris Lattner8cb2aeb2010-04-01 00:37:44 +000022
23 if (ScopeIdx > 0) {
24 // Positive ScopeIdx is an index into ScopeRecords, which has no inlined-at
25 // position specified.
26 assert(unsigned(ScopeIdx) <= Ctx.pImpl->ScopeRecords.size() &&
27 "Invalid ScopeIdx!");
28 return Ctx.pImpl->ScopeRecords[ScopeIdx-1].get();
29 }
30
31 // Otherwise, the index is in the ScopeInlinedAtRecords array.
32 assert(unsigned(-ScopeIdx) <= Ctx.pImpl->ScopeInlinedAtRecords.size() &&
33 "Invalid ScopeIdx");
34 return Ctx.pImpl->ScopeInlinedAtRecords[-ScopeIdx-1].first.get();
35}
36
Chris Lattner593916d2010-04-02 20:21:22 +000037MDNode *DebugLoc::getInlinedAt(const LLVMContext &Ctx) const {
Chris Lattner8cb2aeb2010-04-01 00:37:44 +000038 // Positive ScopeIdx is an index into ScopeRecords, which has no inlined-at
39 // position specified. Zero is invalid.
Craig Topperc6207612014-04-09 06:08:46 +000040 if (ScopeIdx >= 0) return nullptr;
Chris Lattner8cb2aeb2010-04-01 00:37:44 +000041
42 // Otherwise, the index is in the ScopeInlinedAtRecords array.
43 assert(unsigned(-ScopeIdx) <= Ctx.pImpl->ScopeInlinedAtRecords.size() &&
44 "Invalid ScopeIdx");
45 return Ctx.pImpl->ScopeInlinedAtRecords[-ScopeIdx-1].second.get();
46}
47
48/// Return both the Scope and the InlinedAt values.
Chris Lattner593916d2010-04-02 20:21:22 +000049void DebugLoc::getScopeAndInlinedAt(MDNode *&Scope, MDNode *&IA,
50 const LLVMContext &Ctx) const {
Chris Lattner8cb2aeb2010-04-01 00:37:44 +000051 if (ScopeIdx == 0) {
Craig Topperc6207612014-04-09 06:08:46 +000052 Scope = IA = nullptr;
Chris Lattner8cb2aeb2010-04-01 00:37:44 +000053 return;
54 }
55
56 if (ScopeIdx > 0) {
57 // Positive ScopeIdx is an index into ScopeRecords, which has no inlined-at
58 // position specified.
59 assert(unsigned(ScopeIdx) <= Ctx.pImpl->ScopeRecords.size() &&
60 "Invalid ScopeIdx!");
61 Scope = Ctx.pImpl->ScopeRecords[ScopeIdx-1].get();
Craig Topperc6207612014-04-09 06:08:46 +000062 IA = nullptr;
Chris Lattner8cb2aeb2010-04-01 00:37:44 +000063 return;
64 }
65
66 // Otherwise, the index is in the ScopeInlinedAtRecords array.
67 assert(unsigned(-ScopeIdx) <= Ctx.pImpl->ScopeInlinedAtRecords.size() &&
68 "Invalid ScopeIdx");
69 Scope = Ctx.pImpl->ScopeInlinedAtRecords[-ScopeIdx-1].first.get();
70 IA = Ctx.pImpl->ScopeInlinedAtRecords[-ScopeIdx-1].second.get();
71}
72
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +000073MDNode *DebugLoc::getScopeNode(const LLVMContext &Ctx) const {
74 if (MDNode *InlinedAt = getInlinedAt(Ctx))
75 return DebugLoc::getFromDILocation(InlinedAt).getScopeNode(Ctx);
76 return getScope(Ctx);
77}
78
79DebugLoc DebugLoc::getFnDebugLoc(const LLVMContext &Ctx) {
80 const MDNode *Scope = getScopeNode(Ctx);
81 DISubprogram SP = getDISubprogram(Scope);
82 if (SP.isSubprogram()) {
83 // Check for number of operands since the compatibility is
84 // cheap here. FIXME: Name the magic constant.
85 if (SP->getNumOperands() > 19)
86 return DebugLoc::get(SP.getScopeLineNumber(), 0, SP);
87 else
88 return DebugLoc::get(SP.getLineNumber(), 0, SP);
89 }
90
91 return DebugLoc();
92}
Chris Lattner8cb2aeb2010-04-01 00:37:44 +000093
Chris Lattner593916d2010-04-02 20:21:22 +000094DebugLoc DebugLoc::get(unsigned Line, unsigned Col,
95 MDNode *Scope, MDNode *InlinedAt) {
96 DebugLoc Result;
Chris Lattner8cb2aeb2010-04-01 00:37:44 +000097
98 // If no scope is available, this is an unknown location.
Craig Topperc6207612014-04-09 06:08:46 +000099 if (!Scope) return Result;
100
Chris Lattner8cb2aeb2010-04-01 00:37:44 +0000101 // Saturate line and col to "unknown".
102 if (Col > 255) Col = 0;
103 if (Line >= (1 << 24)) Line = 0;
104 Result.LineCol = Line | (Col << 24);
105
106 LLVMContext &Ctx = Scope->getContext();
107
108 // If there is no inlined-at location, use the ScopeRecords array.
Craig Topperc6207612014-04-09 06:08:46 +0000109 if (!InlinedAt)
Chris Lattner8cb2aeb2010-04-01 00:37:44 +0000110 Result.ScopeIdx = Ctx.pImpl->getOrAddScopeRecordIdxEntry(Scope, 0);
111 else
112 Result.ScopeIdx = Ctx.pImpl->getOrAddScopeInlinedAtIdxEntry(Scope,
113 InlinedAt, 0);
114
115 return Result;
116}
117
118/// getAsMDNode - This method converts the compressed DebugLoc node into a
Alon Mishne0394c1e2014-02-05 14:23:18 +0000119/// DILocation-compatible MDNode.
Chris Lattner593916d2010-04-02 20:21:22 +0000120MDNode *DebugLoc::getAsMDNode(const LLVMContext &Ctx) const {
Craig Topperc6207612014-04-09 06:08:46 +0000121 if (isUnknown()) return nullptr;
Chris Lattner8cb2aeb2010-04-01 00:37:44 +0000122
123 MDNode *Scope, *IA;
124 getScopeAndInlinedAt(Scope, IA, Ctx);
125 assert(Scope && "If scope is null, this should be isUnknown()");
126
127 LLVMContext &Ctx2 = Scope->getContext();
Chris Lattner229907c2011-07-18 04:54:35 +0000128 Type *Int32 = Type::getInt32Ty(Ctx2);
Chris Lattner8cb2aeb2010-04-01 00:37:44 +0000129 Value *Elts[] = {
130 ConstantInt::get(Int32, getLine()), ConstantInt::get(Int32, getCol()),
131 Scope, IA
132 };
Jay Foad5514afe2011-04-21 19:59:31 +0000133 return MDNode::get(Ctx2, Elts);
Chris Lattner8cb2aeb2010-04-01 00:37:44 +0000134}
135
Chris Lattner593916d2010-04-02 20:21:22 +0000136/// getFromDILocation - Translate the DILocation quad into a DebugLoc.
137DebugLoc DebugLoc::getFromDILocation(MDNode *N) {
Bill Wendling786de352012-07-07 00:52:35 +0000138 DILocation Loc(N);
139 MDNode *Scope = Loc.getScope();
Craig Topperc6207612014-04-09 06:08:46 +0000140 if (!Scope) return DebugLoc();
Bill Wendling786de352012-07-07 00:52:35 +0000141 return get(Loc.getLineNumber(), Loc.getColumnNumber(), Scope,
142 Loc.getOrigLocation());
Chris Lattner56e4b4a2010-04-01 03:55:42 +0000143}
Chris Lattner8cb2aeb2010-04-01 00:37:44 +0000144
Devang Patel07d61ed2011-07-14 01:14:57 +0000145/// getFromDILexicalBlock - Translate the DILexicalBlock into a DebugLoc.
146DebugLoc DebugLoc::getFromDILexicalBlock(MDNode *N) {
Bill Wendling786de352012-07-07 00:52:35 +0000147 DILexicalBlock LexBlock(N);
148 MDNode *Scope = LexBlock.getContext();
Craig Topperc6207612014-04-09 06:08:46 +0000149 if (!Scope) return DebugLoc();
150 return get(LexBlock.getLineNumber(), LexBlock.getColumnNumber(), Scope,
151 nullptr);
Devang Patel07d61ed2011-07-14 01:14:57 +0000152}
153
Devang Patel4db38442011-07-14 21:50:04 +0000154void DebugLoc::dump(const LLVMContext &Ctx) const {
155#ifndef NDEBUG
156 if (!isUnknown()) {
157 dbgs() << getLine();
158 if (getCol() != 0)
159 dbgs() << ',' << getCol();
160 DebugLoc InlinedAtDL = DebugLoc::getFromDILocation(getInlinedAt(Ctx));
161 if (!InlinedAtDL.isUnknown()) {
162 dbgs() << " @ ";
163 InlinedAtDL.dump(Ctx);
164 } else
165 dbgs() << "\n";
166 }
167#endif
168}
169
Chris Lattner8cb2aeb2010-04-01 00:37:44 +0000170//===----------------------------------------------------------------------===//
Nick Lewyckyddc72892011-04-06 05:36:52 +0000171// DenseMap specialization
172//===----------------------------------------------------------------------===//
173
Nick Lewyckyddc72892011-04-06 05:36:52 +0000174unsigned DenseMapInfo<DebugLoc>::getHashValue(const DebugLoc &Key) {
Benjamin Kramer7a426b52012-04-11 14:06:39 +0000175 return static_cast<unsigned>(hash_combine(Key.LineCol, Key.ScopeIdx));
Nick Lewyckyddc72892011-04-06 05:36:52 +0000176}
177
Nick Lewyckyddc72892011-04-06 05:36:52 +0000178//===----------------------------------------------------------------------===//
Chris Lattner8cb2aeb2010-04-01 00:37:44 +0000179// LLVMContextImpl Implementation
180//===----------------------------------------------------------------------===//
181
182int LLVMContextImpl::getOrAddScopeRecordIdxEntry(MDNode *Scope,
183 int ExistingIdx) {
184 // If we already have an entry for this scope, return it.
185 int &Idx = ScopeRecordIdx[Scope];
186 if (Idx) return Idx;
187
188 // If we don't have an entry, but ExistingIdx is specified, use it.
189 if (ExistingIdx)
190 return Idx = ExistingIdx;
191
192 // Otherwise add a new entry.
193
194 // Start out ScopeRecords with a minimal reasonable size to avoid
195 // excessive reallocation starting out.
196 if (ScopeRecords.empty())
197 ScopeRecords.reserve(128);
198
199 // Index is biased by 1 for index.
200 Idx = ScopeRecords.size()+1;
201 ScopeRecords.push_back(DebugRecVH(Scope, this, Idx));
202 return Idx;
203}
204
205int LLVMContextImpl::getOrAddScopeInlinedAtIdxEntry(MDNode *Scope, MDNode *IA,
206 int ExistingIdx) {
207 // If we already have an entry, return it.
208 int &Idx = ScopeInlinedAtIdx[std::make_pair(Scope, IA)];
209 if (Idx) return Idx;
210
211 // If we don't have an entry, but ExistingIdx is specified, use it.
212 if (ExistingIdx)
213 return Idx = ExistingIdx;
214
215 // Start out ScopeInlinedAtRecords with a minimal reasonable size to avoid
216 // excessive reallocation starting out.
217 if (ScopeInlinedAtRecords.empty())
218 ScopeInlinedAtRecords.reserve(128);
219
220 // Index is biased by 1 and negated.
221 Idx = -ScopeInlinedAtRecords.size()-1;
222 ScopeInlinedAtRecords.push_back(std::make_pair(DebugRecVH(Scope, this, Idx),
223 DebugRecVH(IA, this, Idx)));
224 return Idx;
225}
226
227
228//===----------------------------------------------------------------------===//
229// DebugRecVH Implementation
230//===----------------------------------------------------------------------===//
231
232/// deleted - The MDNode this is pointing to got deleted, so this pointer needs
233/// to drop to null and we need remove our entry from the DenseMap.
234void DebugRecVH::deleted() {
Eric Christophercbce39c2011-10-11 22:58:58 +0000235 // If this is a non-canonical reference, just drop the value to null, we know
Chris Lattner8cb2aeb2010-04-01 00:37:44 +0000236 // it doesn't have a map entry.
237 if (Idx == 0) {
Craig Topperc6207612014-04-09 06:08:46 +0000238 setValPtr(nullptr);
Chris Lattner8cb2aeb2010-04-01 00:37:44 +0000239 return;
240 }
241
242 MDNode *Cur = get();
243
244 // If the index is positive, it is an entry in ScopeRecords.
245 if (Idx > 0) {
246 assert(Ctx->ScopeRecordIdx[Cur] == Idx && "Mapping out of date!");
247 Ctx->ScopeRecordIdx.erase(Cur);
248 // Reset this VH to null and we're done.
Craig Topperc6207612014-04-09 06:08:46 +0000249 setValPtr(nullptr);
Chris Lattner8cb2aeb2010-04-01 00:37:44 +0000250 Idx = 0;
251 return;
252 }
253
254 // Otherwise, it is an entry in ScopeInlinedAtRecords, we don't know if it
255 // is the scope or the inlined-at record entry.
256 assert(unsigned(-Idx-1) < Ctx->ScopeInlinedAtRecords.size());
257 std::pair<DebugRecVH, DebugRecVH> &Entry = Ctx->ScopeInlinedAtRecords[-Idx-1];
258 assert((this == &Entry.first || this == &Entry.second) &&
259 "Mapping out of date!");
260
261 MDNode *OldScope = Entry.first.get();
262 MDNode *OldInlinedAt = Entry.second.get();
263 assert(OldScope != 0 && OldInlinedAt != 0 &&
264 "Entry should be non-canonical if either val dropped to null");
265
266 // Otherwise, we do have an entry in it, nuke it and we're done.
267 assert(Ctx->ScopeInlinedAtIdx[std::make_pair(OldScope, OldInlinedAt)] == Idx&&
268 "Mapping out of date");
269 Ctx->ScopeInlinedAtIdx.erase(std::make_pair(OldScope, OldInlinedAt));
270
Chris Lattnercf25a892010-04-01 05:12:07 +0000271 // Reset this VH to null. Drop both 'Idx' values to null to indicate that
272 // we're in non-canonical form now.
Craig Topperc6207612014-04-09 06:08:46 +0000273 setValPtr(nullptr);
Chris Lattnercf25a892010-04-01 05:12:07 +0000274 Entry.first.Idx = Entry.second.Idx = 0;
Chris Lattner8cb2aeb2010-04-01 00:37:44 +0000275}
276
277void DebugRecVH::allUsesReplacedWith(Value *NewVa) {
278 // If being replaced with a non-mdnode value (e.g. undef) handle this as if
279 // the mdnode got deleted.
280 MDNode *NewVal = dyn_cast<MDNode>(NewVa);
Craig Topperc6207612014-04-09 06:08:46 +0000281 if (!NewVal) return deleted();
282
Chris Lattner8cb2aeb2010-04-01 00:37:44 +0000283 // If this is a non-canonical reference, just change it, we know it already
284 // doesn't have a map entry.
285 if (Idx == 0) {
286 setValPtr(NewVa);
287 return;
288 }
289
290 MDNode *OldVal = get();
291 assert(OldVal != NewVa && "Node replaced with self?");
292
293 // If the index is positive, it is an entry in ScopeRecords.
294 if (Idx > 0) {
295 assert(Ctx->ScopeRecordIdx[OldVal] == Idx && "Mapping out of date!");
296 Ctx->ScopeRecordIdx.erase(OldVal);
297 setValPtr(NewVal);
298
299 int NewEntry = Ctx->getOrAddScopeRecordIdxEntry(NewVal, Idx);
300
301 // If NewVal already has an entry, this becomes a non-canonical reference,
302 // just drop Idx to 0 to signify this.
303 if (NewEntry != Idx)
304 Idx = 0;
305 return;
306 }
307
308 // Otherwise, it is an entry in ScopeInlinedAtRecords, we don't know if it
309 // is the scope or the inlined-at record entry.
310 assert(unsigned(-Idx-1) < Ctx->ScopeInlinedAtRecords.size());
311 std::pair<DebugRecVH, DebugRecVH> &Entry = Ctx->ScopeInlinedAtRecords[-Idx-1];
312 assert((this == &Entry.first || this == &Entry.second) &&
313 "Mapping out of date!");
314
315 MDNode *OldScope = Entry.first.get();
316 MDNode *OldInlinedAt = Entry.second.get();
317 assert(OldScope != 0 && OldInlinedAt != 0 &&
318 "Entry should be non-canonical if either val dropped to null");
319
320 // Otherwise, we do have an entry in it, nuke it and we're done.
321 assert(Ctx->ScopeInlinedAtIdx[std::make_pair(OldScope, OldInlinedAt)] == Idx&&
322 "Mapping out of date");
323 Ctx->ScopeInlinedAtIdx.erase(std::make_pair(OldScope, OldInlinedAt));
324
325 // Reset this VH to the new value.
326 setValPtr(NewVal);
327
328 int NewIdx = Ctx->getOrAddScopeInlinedAtIdxEntry(Entry.first.get(),
329 Entry.second.get(), Idx);
330 // If NewVal already has an entry, this becomes a non-canonical reference,
331 // just drop Idx to 0 to signify this.
Chris Lattnercf25a892010-04-01 05:12:07 +0000332 if (NewIdx != Idx) {
333 std::pair<DebugRecVH, DebugRecVH> &Entry=Ctx->ScopeInlinedAtRecords[-Idx-1];
334 Entry.first.Idx = Entry.second.Idx = 0;
335 }
Chris Lattner8cb2aeb2010-04-01 00:37:44 +0000336}