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/IR/DebugLoc.h"
11 #include "LLVMContextImpl.h"
12 #include "llvm/ADT/DenseMapInfo.h"
13 #include "llvm/IR/DebugInfo.h"
16 //===----------------------------------------------------------------------===//
17 // DebugLoc Implementation
18 //===----------------------------------------------------------------------===//
20 MDNode *DebugLoc::getScope(const LLVMContext &Ctx) const {
21 if (ScopeIdx == 0) return nullptr;
24 // Positive ScopeIdx is an index into ScopeRecords, which has no inlined-at
25 // position specified.
26 assert(unsigned(ScopeIdx) <= Ctx.pImpl->ScopeRecords.size() &&
28 return Ctx.pImpl->ScopeRecords[ScopeIdx-1].get();
31 // Otherwise, the index is in the ScopeInlinedAtRecords array.
32 assert(unsigned(-ScopeIdx) <= Ctx.pImpl->ScopeInlinedAtRecords.size() &&
34 return Ctx.pImpl->ScopeInlinedAtRecords[-ScopeIdx-1].first.get();
37 MDNode *DebugLoc::getInlinedAt(const LLVMContext &Ctx) const {
38 // Positive ScopeIdx is an index into ScopeRecords, which has no inlined-at
39 // position specified. Zero is invalid.
40 if (ScopeIdx >= 0) return nullptr;
42 // Otherwise, the index is in the ScopeInlinedAtRecords array.
43 assert(unsigned(-ScopeIdx) <= Ctx.pImpl->ScopeInlinedAtRecords.size() &&
45 return Ctx.pImpl->ScopeInlinedAtRecords[-ScopeIdx-1].second.get();
48 /// Return both the Scope and the InlinedAt values.
49 void DebugLoc::getScopeAndInlinedAt(MDNode *&Scope, MDNode *&IA,
50 const LLVMContext &Ctx) const {
57 // Positive ScopeIdx is an index into ScopeRecords, which has no inlined-at
58 // position specified.
59 assert(unsigned(ScopeIdx) <= Ctx.pImpl->ScopeRecords.size() &&
61 Scope = Ctx.pImpl->ScopeRecords[ScopeIdx-1].get();
66 // Otherwise, the index is in the ScopeInlinedAtRecords array.
67 assert(unsigned(-ScopeIdx) <= Ctx.pImpl->ScopeInlinedAtRecords.size() &&
69 Scope = Ctx.pImpl->ScopeInlinedAtRecords[-ScopeIdx-1].first.get();
70 IA = Ctx.pImpl->ScopeInlinedAtRecords[-ScopeIdx-1].second.get();
73 MDNode *DebugLoc::getScopeNode(const LLVMContext &Ctx) const {
74 if (MDNode *InlinedAt = getInlinedAt(Ctx))
75 return DebugLoc::getFromDILocation(InlinedAt).getScopeNode(Ctx);
79 DebugLoc DebugLoc::getFnDebugLoc(const LLVMContext &Ctx) const {
80 const MDNode *Scope = getScopeNode(Ctx);
81 DISubprogram SP = getDISubprogram(Scope);
82 if (SP.isSubprogram())
83 return DebugLoc::get(SP.getScopeLineNumber(), 0, SP);
88 DebugLoc DebugLoc::get(unsigned Line, unsigned Col,
89 MDNode *Scope, MDNode *InlinedAt) {
92 // If no scope is available, this is an unknown location.
93 if (!Scope) return Result;
95 // Saturate line and col to "unknown".
96 if (Col > 255) Col = 0;
97 if (Line >= (1 << 24)) Line = 0;
98 Result.LineCol = Line | (Col << 24);
100 LLVMContext &Ctx = Scope->getContext();
102 // If there is no inlined-at location, use the ScopeRecords array.
104 Result.ScopeIdx = Ctx.pImpl->getOrAddScopeRecordIdxEntry(Scope, 0);
106 Result.ScopeIdx = Ctx.pImpl->getOrAddScopeInlinedAtIdxEntry(Scope,
112 /// getAsMDNode - This method converts the compressed DebugLoc node into a
113 /// DILocation-compatible MDNode.
114 MDNode *DebugLoc::getAsMDNode(const LLVMContext &Ctx) const {
115 if (isUnknown()) return nullptr;
118 getScopeAndInlinedAt(Scope, IA, Ctx);
119 assert(Scope && "If scope is null, this should be isUnknown()");
121 LLVMContext &Ctx2 = Scope->getContext();
122 Type *Int32 = Type::getInt32Ty(Ctx2);
124 ConstantInt::get(Int32, getLine()), ConstantInt::get(Int32, getCol()),
127 return MDNode::get(Ctx2, Elts);
130 /// getFromDILocation - Translate the DILocation quad into a DebugLoc.
131 DebugLoc DebugLoc::getFromDILocation(MDNode *N) {
133 MDNode *Scope = Loc.getScope();
134 if (!Scope) return DebugLoc();
135 return get(Loc.getLineNumber(), Loc.getColumnNumber(), Scope,
136 Loc.getOrigLocation());
139 /// getFromDILexicalBlock - Translate the DILexicalBlock into a DebugLoc.
140 DebugLoc DebugLoc::getFromDILexicalBlock(MDNode *N) {
141 DILexicalBlock LexBlock(N);
142 MDNode *Scope = LexBlock.getContext();
143 if (!Scope) return DebugLoc();
144 return get(LexBlock.getLineNumber(), LexBlock.getColumnNumber(), Scope,
148 void DebugLoc::dump(const LLVMContext &Ctx) const {
153 dbgs() << ',' << getCol();
154 DebugLoc InlinedAtDL = DebugLoc::getFromDILocation(getInlinedAt(Ctx));
155 if (!InlinedAtDL.isUnknown()) {
157 InlinedAtDL.dump(Ctx);
164 void DebugLoc::print(const LLVMContext &Ctx, raw_ostream &OS) const {
166 // Print source line info.
167 DIScope Scope(getScope(Ctx));
168 assert((!Scope || Scope.isScope()) &&
169 "Scope of a DebugLoc should be null or a DIScope.");
171 OS << Scope.getFilename();
174 OS << ':' << getLine();
176 OS << ':' << getCol();
177 DebugLoc InlinedAtDL = DebugLoc::getFromDILocation(getInlinedAt(Ctx));
178 if (!InlinedAtDL.isUnknown()) {
180 InlinedAtDL.print(Ctx, OS);
186 //===----------------------------------------------------------------------===//
187 // DenseMap specialization
188 //===----------------------------------------------------------------------===//
190 unsigned DenseMapInfo<DebugLoc>::getHashValue(const DebugLoc &Key) {
191 return static_cast<unsigned>(hash_combine(Key.LineCol, Key.ScopeIdx));
194 //===----------------------------------------------------------------------===//
195 // LLVMContextImpl Implementation
196 //===----------------------------------------------------------------------===//
198 int LLVMContextImpl::getOrAddScopeRecordIdxEntry(MDNode *Scope,
200 // If we already have an entry for this scope, return it.
201 int &Idx = ScopeRecordIdx[Scope];
204 // If we don't have an entry, but ExistingIdx is specified, use it.
206 return Idx = ExistingIdx;
208 // Otherwise add a new entry.
210 // Start out ScopeRecords with a minimal reasonable size to avoid
211 // excessive reallocation starting out.
212 if (ScopeRecords.empty())
213 ScopeRecords.reserve(128);
215 // Index is biased by 1 for index.
216 Idx = ScopeRecords.size()+1;
217 ScopeRecords.push_back(DebugRecVH(Scope, this, Idx));
221 int LLVMContextImpl::getOrAddScopeInlinedAtIdxEntry(MDNode *Scope, MDNode *IA,
223 // If we already have an entry, return it.
224 int &Idx = ScopeInlinedAtIdx[std::make_pair(Scope, IA)];
227 // If we don't have an entry, but ExistingIdx is specified, use it.
229 return Idx = ExistingIdx;
231 // Start out ScopeInlinedAtRecords with a minimal reasonable size to avoid
232 // excessive reallocation starting out.
233 if (ScopeInlinedAtRecords.empty())
234 ScopeInlinedAtRecords.reserve(128);
236 // Index is biased by 1 and negated.
237 Idx = -ScopeInlinedAtRecords.size()-1;
238 ScopeInlinedAtRecords.push_back(std::make_pair(DebugRecVH(Scope, this, Idx),
239 DebugRecVH(IA, this, Idx)));
244 //===----------------------------------------------------------------------===//
245 // DebugRecVH Implementation
246 //===----------------------------------------------------------------------===//
248 /// deleted - The MDNode this is pointing to got deleted, so this pointer needs
249 /// to drop to null and we need remove our entry from the DenseMap.
250 void DebugRecVH::deleted() {
251 // If this is a non-canonical reference, just drop the value to null, we know
252 // it doesn't have a map entry.
260 // If the index is positive, it is an entry in ScopeRecords.
262 assert(Ctx->ScopeRecordIdx[Cur] == Idx && "Mapping out of date!");
263 Ctx->ScopeRecordIdx.erase(Cur);
264 // Reset this VH to null and we're done.
270 // Otherwise, it is an entry in ScopeInlinedAtRecords, we don't know if it
271 // is the scope or the inlined-at record entry.
272 assert(unsigned(-Idx-1) < Ctx->ScopeInlinedAtRecords.size());
273 std::pair<DebugRecVH, DebugRecVH> &Entry = Ctx->ScopeInlinedAtRecords[-Idx-1];
274 assert((this == &Entry.first || this == &Entry.second) &&
275 "Mapping out of date!");
277 MDNode *OldScope = Entry.first.get();
278 MDNode *OldInlinedAt = Entry.second.get();
279 assert(OldScope && OldInlinedAt &&
280 "Entry should be non-canonical if either val dropped to null");
282 // Otherwise, we do have an entry in it, nuke it and we're done.
283 assert(Ctx->ScopeInlinedAtIdx[std::make_pair(OldScope, OldInlinedAt)] == Idx&&
284 "Mapping out of date");
285 Ctx->ScopeInlinedAtIdx.erase(std::make_pair(OldScope, OldInlinedAt));
287 // Reset this VH to null. Drop both 'Idx' values to null to indicate that
288 // we're in non-canonical form now.
290 Entry.first.Idx = Entry.second.Idx = 0;
293 void DebugRecVH::allUsesReplacedWith(Value *NewVa) {
294 // If being replaced with a non-mdnode value (e.g. undef) handle this as if
295 // the mdnode got deleted.
296 MDNode *NewVal = dyn_cast<MDNode>(NewVa);
297 if (!NewVal) return deleted();
299 // If this is a non-canonical reference, just change it, we know it already
300 // doesn't have a map entry.
306 MDNode *OldVal = get();
307 assert(OldVal != NewVa && "Node replaced with self?");
309 // If the index is positive, it is an entry in ScopeRecords.
311 assert(Ctx->ScopeRecordIdx[OldVal] == Idx && "Mapping out of date!");
312 Ctx->ScopeRecordIdx.erase(OldVal);
315 int NewEntry = Ctx->getOrAddScopeRecordIdxEntry(NewVal, Idx);
317 // If NewVal already has an entry, this becomes a non-canonical reference,
318 // just drop Idx to 0 to signify this.
324 // Otherwise, it is an entry in ScopeInlinedAtRecords, we don't know if it
325 // is the scope or the inlined-at record entry.
326 assert(unsigned(-Idx-1) < Ctx->ScopeInlinedAtRecords.size());
327 std::pair<DebugRecVH, DebugRecVH> &Entry = Ctx->ScopeInlinedAtRecords[-Idx-1];
328 assert((this == &Entry.first || this == &Entry.second) &&
329 "Mapping out of date!");
331 MDNode *OldScope = Entry.first.get();
332 MDNode *OldInlinedAt = Entry.second.get();
333 assert(OldScope && OldInlinedAt &&
334 "Entry should be non-canonical if either val dropped to null");
336 // Otherwise, we do have an entry in it, nuke it and we're done.
337 assert(Ctx->ScopeInlinedAtIdx[std::make_pair(OldScope, OldInlinedAt)] == Idx&&
338 "Mapping out of date");
339 Ctx->ScopeInlinedAtIdx.erase(std::make_pair(OldScope, OldInlinedAt));
341 // Reset this VH to the new value.
344 int NewIdx = Ctx->getOrAddScopeInlinedAtIdxEntry(Entry.first.get(),
345 Entry.second.get(), Idx);
346 // If NewVal already has an entry, this becomes a non-canonical reference,
347 // just drop Idx to 0 to signify this.
349 std::pair<DebugRecVH, DebugRecVH> &Entry=Ctx->ScopeInlinedAtRecords[-Idx-1];
350 Entry.first.Idx = Entry.second.Idx = 0;