1 //===-- DebugLoc.cpp - Implement DebugLoc class ---------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "llvm/Support/DebugLoc.h"
11 #include "llvm/ADT/DenseMapInfo.h"
12 #include "LLVMContextImpl.h"
15 //===----------------------------------------------------------------------===//
16 // DebugLoc Implementation
17 //===----------------------------------------------------------------------===//
19 MDNode *DebugLoc::getScope(const LLVMContext &Ctx) const {
20 if (ScopeIdx == 0) return 0;
23 // Positive ScopeIdx is an index into ScopeRecords, which has no inlined-at
24 // position specified.
25 assert(unsigned(ScopeIdx) <= Ctx.pImpl->ScopeRecords.size() &&
27 return Ctx.pImpl->ScopeRecords[ScopeIdx-1].get();
30 // Otherwise, the index is in the ScopeInlinedAtRecords array.
31 assert(unsigned(-ScopeIdx) <= Ctx.pImpl->ScopeInlinedAtRecords.size() &&
33 return Ctx.pImpl->ScopeInlinedAtRecords[-ScopeIdx-1].first.get();
36 MDNode *DebugLoc::getInlinedAt(const LLVMContext &Ctx) const {
37 // Positive ScopeIdx is an index into ScopeRecords, which has no inlined-at
38 // position specified. Zero is invalid.
39 if (ScopeIdx >= 0) return 0;
41 // Otherwise, the index is in the ScopeInlinedAtRecords array.
42 assert(unsigned(-ScopeIdx) <= Ctx.pImpl->ScopeInlinedAtRecords.size() &&
44 return Ctx.pImpl->ScopeInlinedAtRecords[-ScopeIdx-1].second.get();
47 /// Return both the Scope and the InlinedAt values.
48 void DebugLoc::getScopeAndInlinedAt(MDNode *&Scope, MDNode *&IA,
49 const LLVMContext &Ctx) const {
56 // Positive ScopeIdx is an index into ScopeRecords, which has no inlined-at
57 // position specified.
58 assert(unsigned(ScopeIdx) <= Ctx.pImpl->ScopeRecords.size() &&
60 Scope = Ctx.pImpl->ScopeRecords[ScopeIdx-1].get();
65 // Otherwise, the index is in the ScopeInlinedAtRecords array.
66 assert(unsigned(-ScopeIdx) <= Ctx.pImpl->ScopeInlinedAtRecords.size() &&
68 Scope = Ctx.pImpl->ScopeInlinedAtRecords[-ScopeIdx-1].first.get();
69 IA = Ctx.pImpl->ScopeInlinedAtRecords[-ScopeIdx-1].second.get();
73 DebugLoc DebugLoc::get(unsigned Line, unsigned Col,
74 MDNode *Scope, MDNode *InlinedAt) {
77 // If no scope is available, this is an unknown location.
78 if (Scope == 0) return Result;
80 // Saturate line and col to "unknown".
81 if (Col > 255) Col = 0;
82 if (Line >= (1 << 24)) Line = 0;
83 Result.LineCol = Line | (Col << 24);
85 LLVMContext &Ctx = Scope->getContext();
87 // If there is no inlined-at location, use the ScopeRecords array.
89 Result.ScopeIdx = Ctx.pImpl->getOrAddScopeRecordIdxEntry(Scope, 0);
91 Result.ScopeIdx = Ctx.pImpl->getOrAddScopeInlinedAtIdxEntry(Scope,
97 /// getAsMDNode - This method converts the compressed DebugLoc node into a
98 /// DILocation compatible MDNode.
99 MDNode *DebugLoc::getAsMDNode(const LLVMContext &Ctx) const {
100 if (isUnknown()) return 0;
103 getScopeAndInlinedAt(Scope, IA, Ctx);
104 assert(Scope && "If scope is null, this should be isUnknown()");
106 LLVMContext &Ctx2 = Scope->getContext();
107 Type *Int32 = Type::getInt32Ty(Ctx2);
109 ConstantInt::get(Int32, getLine()), ConstantInt::get(Int32, getCol()),
112 return MDNode::get(Ctx2, Elts);
115 /// getFromDILocation - Translate the DILocation quad into a DebugLoc.
116 DebugLoc DebugLoc::getFromDILocation(MDNode *N) {
117 if (N == 0 || N->getNumOperands() != 4) return DebugLoc();
119 MDNode *Scope = dyn_cast_or_null<MDNode>(N->getOperand(2));
120 if (Scope == 0) return DebugLoc();
122 unsigned LineNo = 0, ColNo = 0;
123 if (ConstantInt *Line = dyn_cast_or_null<ConstantInt>(N->getOperand(0)))
124 LineNo = Line->getZExtValue();
125 if (ConstantInt *Col = dyn_cast_or_null<ConstantInt>(N->getOperand(1)))
126 ColNo = Col->getZExtValue();
128 return get(LineNo, ColNo, Scope, dyn_cast_or_null<MDNode>(N->getOperand(3)));
131 /// getFromDILexicalBlock - Translate the DILexicalBlock into a DebugLoc.
132 DebugLoc DebugLoc::getFromDILexicalBlock(MDNode *N) {
133 if (N == 0 || N->getNumOperands() < 3) return DebugLoc();
135 MDNode *Scope = dyn_cast_or_null<MDNode>(N->getOperand(1));
136 if (Scope == 0) return DebugLoc();
138 unsigned LineNo = 0, ColNo = 0;
139 if (ConstantInt *Line = dyn_cast_or_null<ConstantInt>(N->getOperand(2)))
140 LineNo = Line->getZExtValue();
141 if (ConstantInt *Col = dyn_cast_or_null<ConstantInt>(N->getOperand(3)))
142 ColNo = Col->getZExtValue();
144 return get(LineNo, ColNo, Scope, NULL);
147 void DebugLoc::dump(const LLVMContext &Ctx) const {
152 dbgs() << ',' << getCol();
153 DebugLoc InlinedAtDL = DebugLoc::getFromDILocation(getInlinedAt(Ctx));
154 if (!InlinedAtDL.isUnknown()) {
156 InlinedAtDL.dump(Ctx);
163 //===----------------------------------------------------------------------===//
164 // DenseMap specialization
165 //===----------------------------------------------------------------------===//
167 DebugLoc DenseMapInfo<DebugLoc>::getEmptyKey() {
168 return DebugLoc::getEmptyKey();
171 DebugLoc DenseMapInfo<DebugLoc>::getTombstoneKey() {
172 return DebugLoc::getTombstoneKey();
175 unsigned DenseMapInfo<DebugLoc>::getHashValue(const DebugLoc &Key) {
176 return static_cast<unsigned>(hash_combine(Key.LineCol, Key.ScopeIdx));
179 bool DenseMapInfo<DebugLoc>::isEqual(const DebugLoc &LHS, const DebugLoc &RHS) {
183 //===----------------------------------------------------------------------===//
184 // LLVMContextImpl Implementation
185 //===----------------------------------------------------------------------===//
187 int LLVMContextImpl::getOrAddScopeRecordIdxEntry(MDNode *Scope,
189 // If we already have an entry for this scope, return it.
190 int &Idx = ScopeRecordIdx[Scope];
193 // If we don't have an entry, but ExistingIdx is specified, use it.
195 return Idx = ExistingIdx;
197 // Otherwise add a new entry.
199 // Start out ScopeRecords with a minimal reasonable size to avoid
200 // excessive reallocation starting out.
201 if (ScopeRecords.empty())
202 ScopeRecords.reserve(128);
204 // Index is biased by 1 for index.
205 Idx = ScopeRecords.size()+1;
206 ScopeRecords.push_back(DebugRecVH(Scope, this, Idx));
210 int LLVMContextImpl::getOrAddScopeInlinedAtIdxEntry(MDNode *Scope, MDNode *IA,
212 // If we already have an entry, return it.
213 int &Idx = ScopeInlinedAtIdx[std::make_pair(Scope, IA)];
216 // If we don't have an entry, but ExistingIdx is specified, use it.
218 return Idx = ExistingIdx;
220 // Start out ScopeInlinedAtRecords with a minimal reasonable size to avoid
221 // excessive reallocation starting out.
222 if (ScopeInlinedAtRecords.empty())
223 ScopeInlinedAtRecords.reserve(128);
225 // Index is biased by 1 and negated.
226 Idx = -ScopeInlinedAtRecords.size()-1;
227 ScopeInlinedAtRecords.push_back(std::make_pair(DebugRecVH(Scope, this, Idx),
228 DebugRecVH(IA, this, Idx)));
233 //===----------------------------------------------------------------------===//
234 // DebugRecVH Implementation
235 //===----------------------------------------------------------------------===//
237 /// deleted - The MDNode this is pointing to got deleted, so this pointer needs
238 /// to drop to null and we need remove our entry from the DenseMap.
239 void DebugRecVH::deleted() {
240 // If this is a non-canonical reference, just drop the value to null, we know
241 // it doesn't have a map entry.
249 // If the index is positive, it is an entry in ScopeRecords.
251 assert(Ctx->ScopeRecordIdx[Cur] == Idx && "Mapping out of date!");
252 Ctx->ScopeRecordIdx.erase(Cur);
253 // Reset this VH to null and we're done.
259 // Otherwise, it is an entry in ScopeInlinedAtRecords, we don't know if it
260 // is the scope or the inlined-at record entry.
261 assert(unsigned(-Idx-1) < Ctx->ScopeInlinedAtRecords.size());
262 std::pair<DebugRecVH, DebugRecVH> &Entry = Ctx->ScopeInlinedAtRecords[-Idx-1];
263 assert((this == &Entry.first || this == &Entry.second) &&
264 "Mapping out of date!");
266 MDNode *OldScope = Entry.first.get();
267 MDNode *OldInlinedAt = Entry.second.get();
268 assert(OldScope != 0 && OldInlinedAt != 0 &&
269 "Entry should be non-canonical if either val dropped to null");
271 // Otherwise, we do have an entry in it, nuke it and we're done.
272 assert(Ctx->ScopeInlinedAtIdx[std::make_pair(OldScope, OldInlinedAt)] == Idx&&
273 "Mapping out of date");
274 Ctx->ScopeInlinedAtIdx.erase(std::make_pair(OldScope, OldInlinedAt));
276 // Reset this VH to null. Drop both 'Idx' values to null to indicate that
277 // we're in non-canonical form now.
279 Entry.first.Idx = Entry.second.Idx = 0;
282 void DebugRecVH::allUsesReplacedWith(Value *NewVa) {
283 // If being replaced with a non-mdnode value (e.g. undef) handle this as if
284 // the mdnode got deleted.
285 MDNode *NewVal = dyn_cast<MDNode>(NewVa);
286 if (NewVal == 0) return deleted();
288 // If this is a non-canonical reference, just change it, we know it already
289 // doesn't have a map entry.
295 MDNode *OldVal = get();
296 assert(OldVal != NewVa && "Node replaced with self?");
298 // If the index is positive, it is an entry in ScopeRecords.
300 assert(Ctx->ScopeRecordIdx[OldVal] == Idx && "Mapping out of date!");
301 Ctx->ScopeRecordIdx.erase(OldVal);
304 int NewEntry = Ctx->getOrAddScopeRecordIdxEntry(NewVal, Idx);
306 // If NewVal already has an entry, this becomes a non-canonical reference,
307 // just drop Idx to 0 to signify this.
313 // Otherwise, it is an entry in ScopeInlinedAtRecords, we don't know if it
314 // is the scope or the inlined-at record entry.
315 assert(unsigned(-Idx-1) < Ctx->ScopeInlinedAtRecords.size());
316 std::pair<DebugRecVH, DebugRecVH> &Entry = Ctx->ScopeInlinedAtRecords[-Idx-1];
317 assert((this == &Entry.first || this == &Entry.second) &&
318 "Mapping out of date!");
320 MDNode *OldScope = Entry.first.get();
321 MDNode *OldInlinedAt = Entry.second.get();
322 assert(OldScope != 0 && OldInlinedAt != 0 &&
323 "Entry should be non-canonical if either val dropped to null");
325 // Otherwise, we do have an entry in it, nuke it and we're done.
326 assert(Ctx->ScopeInlinedAtIdx[std::make_pair(OldScope, OldInlinedAt)] == Idx&&
327 "Mapping out of date");
328 Ctx->ScopeInlinedAtIdx.erase(std::make_pair(OldScope, OldInlinedAt));
330 // Reset this VH to the new value.
333 int NewIdx = Ctx->getOrAddScopeInlinedAtIdxEntry(Entry.first.get(),
334 Entry.second.get(), Idx);
335 // If NewVal already has an entry, this becomes a non-canonical reference,
336 // just drop Idx to 0 to signify this.
338 std::pair<DebugRecVH, DebugRecVH> &Entry=Ctx->ScopeInlinedAtRecords[-Idx-1];
339 Entry.first.Idx = Entry.second.Idx = 0;