1 //===-- 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"
11 #include "llvm/ADT/DenseMapInfo.h"
12 #include "LLVMContextImpl.h"
13 using namespace llvm;
14
15 //===----------------------------------------------------------------------===//
16 // DebugLoc Implementation
17 //===----------------------------------------------------------------------===//
18
getScope(const LLVMContext & Ctx) const19 MDNode *DebugLoc::getScope(const LLVMContext &Ctx) const {
20 if (ScopeIdx == 0) return 0;
21
22 if (ScopeIdx > 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() &&
26 "Invalid ScopeIdx!");
27 return Ctx.pImpl->ScopeRecords[ScopeIdx-1].get();
28 }
29
30 // Otherwise, the index is in the ScopeInlinedAtRecords array.
31 assert(unsigned(-ScopeIdx) <= Ctx.pImpl->ScopeInlinedAtRecords.size() &&
32 "Invalid ScopeIdx");
33 return Ctx.pImpl->ScopeInlinedAtRecords[-ScopeIdx-1].first.get();
34 }
35
getInlinedAt(const LLVMContext & Ctx) const36 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;
40
41 // Otherwise, the index is in the ScopeInlinedAtRecords array.
42 assert(unsigned(-ScopeIdx) <= Ctx.pImpl->ScopeInlinedAtRecords.size() &&
43 "Invalid ScopeIdx");
44 return Ctx.pImpl->ScopeInlinedAtRecords[-ScopeIdx-1].second.get();
45 }
46
47 /// Return both the Scope and the InlinedAt values.
getScopeAndInlinedAt(MDNode * & Scope,MDNode * & IA,const LLVMContext & Ctx) const48 void DebugLoc::getScopeAndInlinedAt(MDNode *&Scope, MDNode *&IA,
49 const LLVMContext &Ctx) const {
50 if (ScopeIdx == 0) {
51 Scope = IA = 0;
52 return;
53 }
54
55 if (ScopeIdx > 0) {
56 // Positive ScopeIdx is an index into ScopeRecords, which has no inlined-at
57 // position specified.
58 assert(unsigned(ScopeIdx) <= Ctx.pImpl->ScopeRecords.size() &&
59 "Invalid ScopeIdx!");
60 Scope = Ctx.pImpl->ScopeRecords[ScopeIdx-1].get();
61 IA = 0;
62 return;
63 }
64
65 // Otherwise, the index is in the ScopeInlinedAtRecords array.
66 assert(unsigned(-ScopeIdx) <= Ctx.pImpl->ScopeInlinedAtRecords.size() &&
67 "Invalid ScopeIdx");
68 Scope = Ctx.pImpl->ScopeInlinedAtRecords[-ScopeIdx-1].first.get();
69 IA = Ctx.pImpl->ScopeInlinedAtRecords[-ScopeIdx-1].second.get();
70 }
71
72
get(unsigned Line,unsigned Col,MDNode * Scope,MDNode * InlinedAt)73 DebugLoc DebugLoc::get(unsigned Line, unsigned Col,
74 MDNode *Scope, MDNode *InlinedAt) {
75 DebugLoc Result;
76
77 // If no scope is available, this is an unknown location.
78 if (Scope == 0) return Result;
79
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);
84
85 LLVMContext &Ctx = Scope->getContext();
86
87 // If there is no inlined-at location, use the ScopeRecords array.
88 if (InlinedAt == 0)
89 Result.ScopeIdx = Ctx.pImpl->getOrAddScopeRecordIdxEntry(Scope, 0);
90 else
91 Result.ScopeIdx = Ctx.pImpl->getOrAddScopeInlinedAtIdxEntry(Scope,
92 InlinedAt, 0);
93
94 return Result;
95 }
96
97 /// getAsMDNode - This method converts the compressed DebugLoc node into a
98 /// DILocation compatible MDNode.
getAsMDNode(const LLVMContext & Ctx) const99 MDNode *DebugLoc::getAsMDNode(const LLVMContext &Ctx) const {
100 if (isUnknown()) return 0;
101
102 MDNode *Scope, *IA;
103 getScopeAndInlinedAt(Scope, IA, Ctx);
104 assert(Scope && "If scope is null, this should be isUnknown()");
105
106 LLVMContext &Ctx2 = Scope->getContext();
107 Type *Int32 = Type::getInt32Ty(Ctx2);
108 Value *Elts[] = {
109 ConstantInt::get(Int32, getLine()), ConstantInt::get(Int32, getCol()),
110 Scope, IA
111 };
112 return MDNode::get(Ctx2, Elts);
113 }
114
115 /// getFromDILocation - Translate the DILocation quad into a DebugLoc.
getFromDILocation(MDNode * N)116 DebugLoc DebugLoc::getFromDILocation(MDNode *N) {
117 if (N == 0 || N->getNumOperands() != 4) return DebugLoc();
118
119 MDNode *Scope = dyn_cast_or_null<MDNode>(N->getOperand(2));
120 if (Scope == 0) return DebugLoc();
121
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();
127
128 return get(LineNo, ColNo, Scope, dyn_cast_or_null<MDNode>(N->getOperand(3)));
129 }
130
131 /// getFromDILexicalBlock - Translate the DILexicalBlock into a DebugLoc.
getFromDILexicalBlock(MDNode * N)132 DebugLoc DebugLoc::getFromDILexicalBlock(MDNode *N) {
133 if (N == 0 || N->getNumOperands() < 3) return DebugLoc();
134
135 MDNode *Scope = dyn_cast_or_null<MDNode>(N->getOperand(1));
136 if (Scope == 0) return DebugLoc();
137
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();
143
144 return get(LineNo, ColNo, Scope, NULL);
145 }
146
dump(const LLVMContext & Ctx) const147 void DebugLoc::dump(const LLVMContext &Ctx) const {
148 #ifndef NDEBUG
149 if (!isUnknown()) {
150 dbgs() << getLine();
151 if (getCol() != 0)
152 dbgs() << ',' << getCol();
153 DebugLoc InlinedAtDL = DebugLoc::getFromDILocation(getInlinedAt(Ctx));
154 if (!InlinedAtDL.isUnknown()) {
155 dbgs() << " @ ";
156 InlinedAtDL.dump(Ctx);
157 } else
158 dbgs() << "\n";
159 }
160 #endif
161 }
162
163 //===----------------------------------------------------------------------===//
164 // DenseMap specialization
165 //===----------------------------------------------------------------------===//
166
getEmptyKey()167 DebugLoc DenseMapInfo<DebugLoc>::getEmptyKey() {
168 return DebugLoc::getEmptyKey();
169 }
170
getTombstoneKey()171 DebugLoc DenseMapInfo<DebugLoc>::getTombstoneKey() {
172 return DebugLoc::getTombstoneKey();
173 }
174
getHashValue(const DebugLoc & Key)175 unsigned DenseMapInfo<DebugLoc>::getHashValue(const DebugLoc &Key) {
176 FoldingSetNodeID ID;
177 ID.AddInteger(Key.LineCol);
178 ID.AddInteger(Key.ScopeIdx);
179 return ID.ComputeHash();
180 }
181
isEqual(const DebugLoc & LHS,const DebugLoc & RHS)182 bool DenseMapInfo<DebugLoc>::isEqual(const DebugLoc &LHS, const DebugLoc &RHS) {
183 return LHS == RHS;
184 }
185
186 //===----------------------------------------------------------------------===//
187 // LLVMContextImpl Implementation
188 //===----------------------------------------------------------------------===//
189
getOrAddScopeRecordIdxEntry(MDNode * Scope,int ExistingIdx)190 int LLVMContextImpl::getOrAddScopeRecordIdxEntry(MDNode *Scope,
191 int ExistingIdx) {
192 // If we already have an entry for this scope, return it.
193 int &Idx = ScopeRecordIdx[Scope];
194 if (Idx) return Idx;
195
196 // If we don't have an entry, but ExistingIdx is specified, use it.
197 if (ExistingIdx)
198 return Idx = ExistingIdx;
199
200 // Otherwise add a new entry.
201
202 // Start out ScopeRecords with a minimal reasonable size to avoid
203 // excessive reallocation starting out.
204 if (ScopeRecords.empty())
205 ScopeRecords.reserve(128);
206
207 // Index is biased by 1 for index.
208 Idx = ScopeRecords.size()+1;
209 ScopeRecords.push_back(DebugRecVH(Scope, this, Idx));
210 return Idx;
211 }
212
getOrAddScopeInlinedAtIdxEntry(MDNode * Scope,MDNode * IA,int ExistingIdx)213 int LLVMContextImpl::getOrAddScopeInlinedAtIdxEntry(MDNode *Scope, MDNode *IA,
214 int ExistingIdx) {
215 // If we already have an entry, return it.
216 int &Idx = ScopeInlinedAtIdx[std::make_pair(Scope, IA)];
217 if (Idx) return Idx;
218
219 // If we don't have an entry, but ExistingIdx is specified, use it.
220 if (ExistingIdx)
221 return Idx = ExistingIdx;
222
223 // Start out ScopeInlinedAtRecords with a minimal reasonable size to avoid
224 // excessive reallocation starting out.
225 if (ScopeInlinedAtRecords.empty())
226 ScopeInlinedAtRecords.reserve(128);
227
228 // Index is biased by 1 and negated.
229 Idx = -ScopeInlinedAtRecords.size()-1;
230 ScopeInlinedAtRecords.push_back(std::make_pair(DebugRecVH(Scope, this, Idx),
231 DebugRecVH(IA, this, Idx)));
232 return Idx;
233 }
234
235
236 //===----------------------------------------------------------------------===//
237 // DebugRecVH Implementation
238 //===----------------------------------------------------------------------===//
239
240 /// deleted - The MDNode this is pointing to got deleted, so this pointer needs
241 /// to drop to null and we need remove our entry from the DenseMap.
deleted()242 void DebugRecVH::deleted() {
243 // If this is a non-canonical reference, just drop the value to null, we know
244 // it doesn't have a map entry.
245 if (Idx == 0) {
246 setValPtr(0);
247 return;
248 }
249
250 MDNode *Cur = get();
251
252 // If the index is positive, it is an entry in ScopeRecords.
253 if (Idx > 0) {
254 assert(Ctx->ScopeRecordIdx[Cur] == Idx && "Mapping out of date!");
255 Ctx->ScopeRecordIdx.erase(Cur);
256 // Reset this VH to null and we're done.
257 setValPtr(0);
258 Idx = 0;
259 return;
260 }
261
262 // Otherwise, it is an entry in ScopeInlinedAtRecords, we don't know if it
263 // is the scope or the inlined-at record entry.
264 assert(unsigned(-Idx-1) < Ctx->ScopeInlinedAtRecords.size());
265 std::pair<DebugRecVH, DebugRecVH> &Entry = Ctx->ScopeInlinedAtRecords[-Idx-1];
266 assert((this == &Entry.first || this == &Entry.second) &&
267 "Mapping out of date!");
268
269 MDNode *OldScope = Entry.first.get();
270 MDNode *OldInlinedAt = Entry.second.get();
271 assert(OldScope != 0 && OldInlinedAt != 0 &&
272 "Entry should be non-canonical if either val dropped to null");
273
274 // Otherwise, we do have an entry in it, nuke it and we're done.
275 assert(Ctx->ScopeInlinedAtIdx[std::make_pair(OldScope, OldInlinedAt)] == Idx&&
276 "Mapping out of date");
277 Ctx->ScopeInlinedAtIdx.erase(std::make_pair(OldScope, OldInlinedAt));
278
279 // Reset this VH to null. Drop both 'Idx' values to null to indicate that
280 // we're in non-canonical form now.
281 setValPtr(0);
282 Entry.first.Idx = Entry.second.Idx = 0;
283 }
284
allUsesReplacedWith(Value * NewVa)285 void DebugRecVH::allUsesReplacedWith(Value *NewVa) {
286 // If being replaced with a non-mdnode value (e.g. undef) handle this as if
287 // the mdnode got deleted.
288 MDNode *NewVal = dyn_cast<MDNode>(NewVa);
289 if (NewVal == 0) return deleted();
290
291 // If this is a non-canonical reference, just change it, we know it already
292 // doesn't have a map entry.
293 if (Idx == 0) {
294 setValPtr(NewVa);
295 return;
296 }
297
298 MDNode *OldVal = get();
299 assert(OldVal != NewVa && "Node replaced with self?");
300
301 // If the index is positive, it is an entry in ScopeRecords.
302 if (Idx > 0) {
303 assert(Ctx->ScopeRecordIdx[OldVal] == Idx && "Mapping out of date!");
304 Ctx->ScopeRecordIdx.erase(OldVal);
305 setValPtr(NewVal);
306
307 int NewEntry = Ctx->getOrAddScopeRecordIdxEntry(NewVal, Idx);
308
309 // If NewVal already has an entry, this becomes a non-canonical reference,
310 // just drop Idx to 0 to signify this.
311 if (NewEntry != Idx)
312 Idx = 0;
313 return;
314 }
315
316 // Otherwise, it is an entry in ScopeInlinedAtRecords, we don't know if it
317 // is the scope or the inlined-at record entry.
318 assert(unsigned(-Idx-1) < Ctx->ScopeInlinedAtRecords.size());
319 std::pair<DebugRecVH, DebugRecVH> &Entry = Ctx->ScopeInlinedAtRecords[-Idx-1];
320 assert((this == &Entry.first || this == &Entry.second) &&
321 "Mapping out of date!");
322
323 MDNode *OldScope = Entry.first.get();
324 MDNode *OldInlinedAt = Entry.second.get();
325 assert(OldScope != 0 && OldInlinedAt != 0 &&
326 "Entry should be non-canonical if either val dropped to null");
327
328 // Otherwise, we do have an entry in it, nuke it and we're done.
329 assert(Ctx->ScopeInlinedAtIdx[std::make_pair(OldScope, OldInlinedAt)] == Idx&&
330 "Mapping out of date");
331 Ctx->ScopeInlinedAtIdx.erase(std::make_pair(OldScope, OldInlinedAt));
332
333 // Reset this VH to the new value.
334 setValPtr(NewVal);
335
336 int NewIdx = Ctx->getOrAddScopeInlinedAtIdxEntry(Entry.first.get(),
337 Entry.second.get(), Idx);
338 // If NewVal already has an entry, this becomes a non-canonical reference,
339 // just drop Idx to 0 to signify this.
340 if (NewIdx != Idx) {
341 std::pair<DebugRecVH, DebugRecVH> &Entry=Ctx->ScopeInlinedAtRecords[-Idx-1];
342 Entry.first.Idx = Entry.second.Idx = 0;
343 }
344 }
345