blob: b8d855146210bace010ed1942d69a891fe513d4a [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
10#include "llvm/Support/DebugLoc.h"
Bill Wendling786de352012-07-07 00:52:35 +000011#include "llvm/DebugInfo.h"
Nick Lewyckyddc72892011-04-06 05:36:52 +000012#include "llvm/ADT/DenseMapInfo.h"
Chris Lattner8cb2aeb2010-04-01 00:37:44 +000013#include "LLVMContextImpl.h"
14using namespace llvm;
15
16//===----------------------------------------------------------------------===//
17// DebugLoc Implementation
18//===----------------------------------------------------------------------===//
19
Chris Lattner593916d2010-04-02 20:21:22 +000020MDNode *DebugLoc::getScope(const LLVMContext &Ctx) const {
Chris Lattner8cb2aeb2010-04-01 00:37:44 +000021 if (ScopeIdx == 0) return 0;
22
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.
40 if (ScopeIdx >= 0) return 0;
41
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) {
52 Scope = IA = 0;
53 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();
62 IA = 0;
63 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
73
Chris Lattner593916d2010-04-02 20:21:22 +000074DebugLoc DebugLoc::get(unsigned Line, unsigned Col,
75 MDNode *Scope, MDNode *InlinedAt) {
76 DebugLoc Result;
Chris Lattner8cb2aeb2010-04-01 00:37:44 +000077
78 // If no scope is available, this is an unknown location.
79 if (Scope == 0) return Result;
80
81 // Saturate line and col to "unknown".
82 if (Col > 255) Col = 0;
83 if (Line >= (1 << 24)) Line = 0;
84 Result.LineCol = Line | (Col << 24);
85
86 LLVMContext &Ctx = Scope->getContext();
87
88 // If there is no inlined-at location, use the ScopeRecords array.
89 if (InlinedAt == 0)
90 Result.ScopeIdx = Ctx.pImpl->getOrAddScopeRecordIdxEntry(Scope, 0);
91 else
92 Result.ScopeIdx = Ctx.pImpl->getOrAddScopeInlinedAtIdxEntry(Scope,
93 InlinedAt, 0);
94
95 return Result;
96}
97
98/// getAsMDNode - This method converts the compressed DebugLoc node into a
99/// DILocation compatible MDNode.
Chris Lattner593916d2010-04-02 20:21:22 +0000100MDNode *DebugLoc::getAsMDNode(const LLVMContext &Ctx) const {
Chris Lattner8cb2aeb2010-04-01 00:37:44 +0000101 if (isUnknown()) return 0;
102
103 MDNode *Scope, *IA;
104 getScopeAndInlinedAt(Scope, IA, Ctx);
105 assert(Scope && "If scope is null, this should be isUnknown()");
106
107 LLVMContext &Ctx2 = Scope->getContext();
Chris Lattner229907c2011-07-18 04:54:35 +0000108 Type *Int32 = Type::getInt32Ty(Ctx2);
Chris Lattner8cb2aeb2010-04-01 00:37:44 +0000109 Value *Elts[] = {
110 ConstantInt::get(Int32, getLine()), ConstantInt::get(Int32, getCol()),
111 Scope, IA
112 };
Jay Foad5514afe2011-04-21 19:59:31 +0000113 return MDNode::get(Ctx2, Elts);
Chris Lattner8cb2aeb2010-04-01 00:37:44 +0000114}
115
Chris Lattner593916d2010-04-02 20:21:22 +0000116/// getFromDILocation - Translate the DILocation quad into a DebugLoc.
117DebugLoc DebugLoc::getFromDILocation(MDNode *N) {
Bill Wendling786de352012-07-07 00:52:35 +0000118 DILocation Loc(N);
119 MDNode *Scope = Loc.getScope();
Chris Lattner593916d2010-04-02 20:21:22 +0000120 if (Scope == 0) return DebugLoc();
Bill Wendling786de352012-07-07 00:52:35 +0000121 return get(Loc.getLineNumber(), Loc.getColumnNumber(), Scope,
122 Loc.getOrigLocation());
Chris Lattner56e4b4a2010-04-01 03:55:42 +0000123}
Chris Lattner8cb2aeb2010-04-01 00:37:44 +0000124
Devang Patel07d61ed2011-07-14 01:14:57 +0000125/// getFromDILexicalBlock - Translate the DILexicalBlock into a DebugLoc.
126DebugLoc DebugLoc::getFromDILexicalBlock(MDNode *N) {
Bill Wendling786de352012-07-07 00:52:35 +0000127 DILexicalBlock LexBlock(N);
128 MDNode *Scope = LexBlock.getContext();
Devang Patel07d61ed2011-07-14 01:14:57 +0000129 if (Scope == 0) return DebugLoc();
Bill Wendling786de352012-07-07 00:52:35 +0000130 return get(LexBlock.getLineNumber(), LexBlock.getColumnNumber(), Scope, NULL);
Devang Patel07d61ed2011-07-14 01:14:57 +0000131}
132
Devang Patel4db38442011-07-14 21:50:04 +0000133void DebugLoc::dump(const LLVMContext &Ctx) const {
134#ifndef NDEBUG
135 if (!isUnknown()) {
136 dbgs() << getLine();
137 if (getCol() != 0)
138 dbgs() << ',' << getCol();
139 DebugLoc InlinedAtDL = DebugLoc::getFromDILocation(getInlinedAt(Ctx));
140 if (!InlinedAtDL.isUnknown()) {
141 dbgs() << " @ ";
142 InlinedAtDL.dump(Ctx);
143 } else
144 dbgs() << "\n";
145 }
146#endif
147}
148
Chris Lattner8cb2aeb2010-04-01 00:37:44 +0000149//===----------------------------------------------------------------------===//
Nick Lewyckyddc72892011-04-06 05:36:52 +0000150// DenseMap specialization
151//===----------------------------------------------------------------------===//
152
153DebugLoc DenseMapInfo<DebugLoc>::getEmptyKey() {
Nick Lewyckyd4b3d292011-04-06 06:49:59 +0000154 return DebugLoc::getEmptyKey();
Nick Lewyckyddc72892011-04-06 05:36:52 +0000155}
156
157DebugLoc DenseMapInfo<DebugLoc>::getTombstoneKey() {
158 return DebugLoc::getTombstoneKey();
159}
160
161unsigned DenseMapInfo<DebugLoc>::getHashValue(const DebugLoc &Key) {
Benjamin Kramer7a426b52012-04-11 14:06:39 +0000162 return static_cast<unsigned>(hash_combine(Key.LineCol, Key.ScopeIdx));
Nick Lewyckyddc72892011-04-06 05:36:52 +0000163}
164
165bool DenseMapInfo<DebugLoc>::isEqual(const DebugLoc &LHS, const DebugLoc &RHS) {
166 return LHS == RHS;
167}
168
169//===----------------------------------------------------------------------===//
Chris Lattner8cb2aeb2010-04-01 00:37:44 +0000170// LLVMContextImpl Implementation
171//===----------------------------------------------------------------------===//
172
173int LLVMContextImpl::getOrAddScopeRecordIdxEntry(MDNode *Scope,
174 int ExistingIdx) {
175 // If we already have an entry for this scope, return it.
176 int &Idx = ScopeRecordIdx[Scope];
177 if (Idx) return Idx;
178
179 // If we don't have an entry, but ExistingIdx is specified, use it.
180 if (ExistingIdx)
181 return Idx = ExistingIdx;
182
183 // Otherwise add a new entry.
184
185 // Start out ScopeRecords with a minimal reasonable size to avoid
186 // excessive reallocation starting out.
187 if (ScopeRecords.empty())
188 ScopeRecords.reserve(128);
189
190 // Index is biased by 1 for index.
191 Idx = ScopeRecords.size()+1;
192 ScopeRecords.push_back(DebugRecVH(Scope, this, Idx));
193 return Idx;
194}
195
196int LLVMContextImpl::getOrAddScopeInlinedAtIdxEntry(MDNode *Scope, MDNode *IA,
197 int ExistingIdx) {
198 // If we already have an entry, return it.
199 int &Idx = ScopeInlinedAtIdx[std::make_pair(Scope, IA)];
200 if (Idx) return Idx;
201
202 // If we don't have an entry, but ExistingIdx is specified, use it.
203 if (ExistingIdx)
204 return Idx = ExistingIdx;
205
206 // Start out ScopeInlinedAtRecords with a minimal reasonable size to avoid
207 // excessive reallocation starting out.
208 if (ScopeInlinedAtRecords.empty())
209 ScopeInlinedAtRecords.reserve(128);
210
211 // Index is biased by 1 and negated.
212 Idx = -ScopeInlinedAtRecords.size()-1;
213 ScopeInlinedAtRecords.push_back(std::make_pair(DebugRecVH(Scope, this, Idx),
214 DebugRecVH(IA, this, Idx)));
215 return Idx;
216}
217
218
219//===----------------------------------------------------------------------===//
220// DebugRecVH Implementation
221//===----------------------------------------------------------------------===//
222
223/// deleted - The MDNode this is pointing to got deleted, so this pointer needs
224/// to drop to null and we need remove our entry from the DenseMap.
225void DebugRecVH::deleted() {
Eric Christophercbce39c2011-10-11 22:58:58 +0000226 // If this is a non-canonical reference, just drop the value to null, we know
Chris Lattner8cb2aeb2010-04-01 00:37:44 +0000227 // it doesn't have a map entry.
228 if (Idx == 0) {
229 setValPtr(0);
230 return;
231 }
232
233 MDNode *Cur = get();
234
235 // If the index is positive, it is an entry in ScopeRecords.
236 if (Idx > 0) {
237 assert(Ctx->ScopeRecordIdx[Cur] == Idx && "Mapping out of date!");
238 Ctx->ScopeRecordIdx.erase(Cur);
239 // Reset this VH to null and we're done.
240 setValPtr(0);
241 Idx = 0;
242 return;
243 }
244
245 // Otherwise, it is an entry in ScopeInlinedAtRecords, we don't know if it
246 // is the scope or the inlined-at record entry.
247 assert(unsigned(-Idx-1) < Ctx->ScopeInlinedAtRecords.size());
248 std::pair<DebugRecVH, DebugRecVH> &Entry = Ctx->ScopeInlinedAtRecords[-Idx-1];
249 assert((this == &Entry.first || this == &Entry.second) &&
250 "Mapping out of date!");
251
252 MDNode *OldScope = Entry.first.get();
253 MDNode *OldInlinedAt = Entry.second.get();
254 assert(OldScope != 0 && OldInlinedAt != 0 &&
255 "Entry should be non-canonical if either val dropped to null");
256
257 // Otherwise, we do have an entry in it, nuke it and we're done.
258 assert(Ctx->ScopeInlinedAtIdx[std::make_pair(OldScope, OldInlinedAt)] == Idx&&
259 "Mapping out of date");
260 Ctx->ScopeInlinedAtIdx.erase(std::make_pair(OldScope, OldInlinedAt));
261
Chris Lattnercf25a892010-04-01 05:12:07 +0000262 // Reset this VH to null. Drop both 'Idx' values to null to indicate that
263 // we're in non-canonical form now.
Chris Lattner8cb2aeb2010-04-01 00:37:44 +0000264 setValPtr(0);
Chris Lattnercf25a892010-04-01 05:12:07 +0000265 Entry.first.Idx = Entry.second.Idx = 0;
Chris Lattner8cb2aeb2010-04-01 00:37:44 +0000266}
267
268void DebugRecVH::allUsesReplacedWith(Value *NewVa) {
269 // If being replaced with a non-mdnode value (e.g. undef) handle this as if
270 // the mdnode got deleted.
271 MDNode *NewVal = dyn_cast<MDNode>(NewVa);
272 if (NewVal == 0) return deleted();
273
274 // If this is a non-canonical reference, just change it, we know it already
275 // doesn't have a map entry.
276 if (Idx == 0) {
277 setValPtr(NewVa);
278 return;
279 }
280
281 MDNode *OldVal = get();
282 assert(OldVal != NewVa && "Node replaced with self?");
283
284 // If the index is positive, it is an entry in ScopeRecords.
285 if (Idx > 0) {
286 assert(Ctx->ScopeRecordIdx[OldVal] == Idx && "Mapping out of date!");
287 Ctx->ScopeRecordIdx.erase(OldVal);
288 setValPtr(NewVal);
289
290 int NewEntry = Ctx->getOrAddScopeRecordIdxEntry(NewVal, Idx);
291
292 // If NewVal already has an entry, this becomes a non-canonical reference,
293 // just drop Idx to 0 to signify this.
294 if (NewEntry != Idx)
295 Idx = 0;
296 return;
297 }
298
299 // Otherwise, it is an entry in ScopeInlinedAtRecords, we don't know if it
300 // is the scope or the inlined-at record entry.
301 assert(unsigned(-Idx-1) < Ctx->ScopeInlinedAtRecords.size());
302 std::pair<DebugRecVH, DebugRecVH> &Entry = Ctx->ScopeInlinedAtRecords[-Idx-1];
303 assert((this == &Entry.first || this == &Entry.second) &&
304 "Mapping out of date!");
305
306 MDNode *OldScope = Entry.first.get();
307 MDNode *OldInlinedAt = Entry.second.get();
308 assert(OldScope != 0 && OldInlinedAt != 0 &&
309 "Entry should be non-canonical if either val dropped to null");
310
311 // Otherwise, we do have an entry in it, nuke it and we're done.
312 assert(Ctx->ScopeInlinedAtIdx[std::make_pair(OldScope, OldInlinedAt)] == Idx&&
313 "Mapping out of date");
314 Ctx->ScopeInlinedAtIdx.erase(std::make_pair(OldScope, OldInlinedAt));
315
316 // Reset this VH to the new value.
317 setValPtr(NewVal);
318
319 int NewIdx = Ctx->getOrAddScopeInlinedAtIdxEntry(Entry.first.get(),
320 Entry.second.get(), Idx);
321 // If NewVal already has an entry, this becomes a non-canonical reference,
322 // just drop Idx to 0 to signify this.
Chris Lattnercf25a892010-04-01 05:12:07 +0000323 if (NewIdx != Idx) {
324 std::pair<DebugRecVH, DebugRecVH> &Entry=Ctx->ScopeInlinedAtRecords[-Idx-1];
325 Entry.first.Idx = Entry.second.Idx = 0;
326 }
Chris Lattner8cb2aeb2010-04-01 00:37:44 +0000327}