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 "LLVMContextImpl.h"
14 //===----------------------------------------------------------------------===//
15 // DebugLoc Implementation
16 //===----------------------------------------------------------------------===//
18 MDNode
*DebugLoc::getScope(const LLVMContext
&Ctx
) const {
19 if (ScopeIdx
== 0) return 0;
22 // Positive ScopeIdx is an index into ScopeRecords, which has no inlined-at
23 // position specified.
24 assert(unsigned(ScopeIdx
) <= Ctx
.pImpl
->ScopeRecords
.size() &&
26 return Ctx
.pImpl
->ScopeRecords
[ScopeIdx
-1].get();
29 // Otherwise, the index is in the ScopeInlinedAtRecords array.
30 assert(unsigned(-ScopeIdx
) <= Ctx
.pImpl
->ScopeInlinedAtRecords
.size() &&
32 return Ctx
.pImpl
->ScopeInlinedAtRecords
[-ScopeIdx
-1].first
.get();
35 MDNode
*DebugLoc::getInlinedAt(const LLVMContext
&Ctx
) const {
36 // Positive ScopeIdx is an index into ScopeRecords, which has no inlined-at
37 // position specified. Zero is invalid.
38 if (ScopeIdx
>= 0) return 0;
40 // Otherwise, the index is in the ScopeInlinedAtRecords array.
41 assert(unsigned(-ScopeIdx
) <= Ctx
.pImpl
->ScopeInlinedAtRecords
.size() &&
43 return Ctx
.pImpl
->ScopeInlinedAtRecords
[-ScopeIdx
-1].second
.get();
46 /// Return both the Scope and the InlinedAt values.
47 void DebugLoc::getScopeAndInlinedAt(MDNode
*&Scope
, MDNode
*&IA
,
48 const LLVMContext
&Ctx
) const {
55 // Positive ScopeIdx is an index into ScopeRecords, which has no inlined-at
56 // position specified.
57 assert(unsigned(ScopeIdx
) <= Ctx
.pImpl
->ScopeRecords
.size() &&
59 Scope
= Ctx
.pImpl
->ScopeRecords
[ScopeIdx
-1].get();
64 // Otherwise, the index is in the ScopeInlinedAtRecords array.
65 assert(unsigned(-ScopeIdx
) <= Ctx
.pImpl
->ScopeInlinedAtRecords
.size() &&
67 Scope
= Ctx
.pImpl
->ScopeInlinedAtRecords
[-ScopeIdx
-1].first
.get();
68 IA
= Ctx
.pImpl
->ScopeInlinedAtRecords
[-ScopeIdx
-1].second
.get();
72 DebugLoc
DebugLoc::get(unsigned Line
, unsigned Col
,
73 MDNode
*Scope
, MDNode
*InlinedAt
) {
76 // If no scope is available, this is an unknown location.
77 if (Scope
== 0) return Result
;
79 // Saturate line and col to "unknown".
80 if (Col
> 255) Col
= 0;
81 if (Line
>= (1 << 24)) Line
= 0;
82 Result
.LineCol
= Line
| (Col
<< 24);
84 LLVMContext
&Ctx
= Scope
->getContext();
86 // If there is no inlined-at location, use the ScopeRecords array.
88 Result
.ScopeIdx
= Ctx
.pImpl
->getOrAddScopeRecordIdxEntry(Scope
, 0);
90 Result
.ScopeIdx
= Ctx
.pImpl
->getOrAddScopeInlinedAtIdxEntry(Scope
,
96 /// getAsMDNode - This method converts the compressed DebugLoc node into a
97 /// DILocation compatible MDNode.
98 MDNode
*DebugLoc::getAsMDNode(const LLVMContext
&Ctx
) const {
99 if (isUnknown()) return 0;
102 getScopeAndInlinedAt(Scope
, IA
, Ctx
);
103 assert(Scope
&& "If scope is null, this should be isUnknown()");
105 LLVMContext
&Ctx2
= Scope
->getContext();
106 const Type
*Int32
= Type::getInt32Ty(Ctx2
);
108 ConstantInt::get(Int32
, getLine()), ConstantInt::get(Int32
, getCol()),
111 return MDNode::get(Ctx2
, &Elts
[0], 4);
114 /// getFromDILocation - Translate the DILocation quad into a DebugLoc.
115 DebugLoc
DebugLoc::getFromDILocation(MDNode
*N
) {
116 if (N
== 0 || N
->getNumOperands() != 4) return DebugLoc();
118 MDNode
*Scope
= dyn_cast_or_null
<MDNode
>(N
->getOperand(2));
119 if (Scope
== 0) return DebugLoc();
121 unsigned LineNo
= 0, ColNo
= 0;
122 if (ConstantInt
*Line
= dyn_cast_or_null
<ConstantInt
>(N
->getOperand(0)))
123 LineNo
= Line
->getZExtValue();
124 if (ConstantInt
*Col
= dyn_cast_or_null
<ConstantInt
>(N
->getOperand(1)))
125 ColNo
= Col
->getZExtValue();
127 return get(LineNo
, ColNo
, Scope
, dyn_cast_or_null
<MDNode
>(N
->getOperand(3)));
130 //===----------------------------------------------------------------------===//
131 // LLVMContextImpl Implementation
132 //===----------------------------------------------------------------------===//
134 int LLVMContextImpl::getOrAddScopeRecordIdxEntry(MDNode
*Scope
,
136 // If we already have an entry for this scope, return it.
137 int &Idx
= ScopeRecordIdx
[Scope
];
140 // If we don't have an entry, but ExistingIdx is specified, use it.
142 return Idx
= ExistingIdx
;
144 // Otherwise add a new entry.
146 // Start out ScopeRecords with a minimal reasonable size to avoid
147 // excessive reallocation starting out.
148 if (ScopeRecords
.empty())
149 ScopeRecords
.reserve(128);
151 // Index is biased by 1 for index.
152 Idx
= ScopeRecords
.size()+1;
153 ScopeRecords
.push_back(DebugRecVH(Scope
, this, Idx
));
157 int LLVMContextImpl::getOrAddScopeInlinedAtIdxEntry(MDNode
*Scope
, MDNode
*IA
,
159 // If we already have an entry, return it.
160 int &Idx
= ScopeInlinedAtIdx
[std::make_pair(Scope
, IA
)];
163 // If we don't have an entry, but ExistingIdx is specified, use it.
165 return Idx
= ExistingIdx
;
167 // Start out ScopeInlinedAtRecords with a minimal reasonable size to avoid
168 // excessive reallocation starting out.
169 if (ScopeInlinedAtRecords
.empty())
170 ScopeInlinedAtRecords
.reserve(128);
172 // Index is biased by 1 and negated.
173 Idx
= -ScopeInlinedAtRecords
.size()-1;
174 ScopeInlinedAtRecords
.push_back(std::make_pair(DebugRecVH(Scope
, this, Idx
),
175 DebugRecVH(IA
, this, Idx
)));
180 //===----------------------------------------------------------------------===//
181 // DebugRecVH Implementation
182 //===----------------------------------------------------------------------===//
184 /// deleted - The MDNode this is pointing to got deleted, so this pointer needs
185 /// to drop to null and we need remove our entry from the DenseMap.
186 void DebugRecVH::deleted() {
187 // If this is a non-canonical reference, just drop the value to null, we know
188 // it doesn't have a map entry.
196 // If the index is positive, it is an entry in ScopeRecords.
198 assert(Ctx
->ScopeRecordIdx
[Cur
] == Idx
&& "Mapping out of date!");
199 Ctx
->ScopeRecordIdx
.erase(Cur
);
200 // Reset this VH to null and we're done.
206 // Otherwise, it is an entry in ScopeInlinedAtRecords, we don't know if it
207 // is the scope or the inlined-at record entry.
208 assert(unsigned(-Idx
-1) < Ctx
->ScopeInlinedAtRecords
.size());
209 std::pair
<DebugRecVH
, DebugRecVH
> &Entry
= Ctx
->ScopeInlinedAtRecords
[-Idx
-1];
210 assert((this == &Entry
.first
|| this == &Entry
.second
) &&
211 "Mapping out of date!");
213 MDNode
*OldScope
= Entry
.first
.get();
214 MDNode
*OldInlinedAt
= Entry
.second
.get();
215 assert(OldScope
!= 0 && OldInlinedAt
!= 0 &&
216 "Entry should be non-canonical if either val dropped to null");
218 // Otherwise, we do have an entry in it, nuke it and we're done.
219 assert(Ctx
->ScopeInlinedAtIdx
[std::make_pair(OldScope
, OldInlinedAt
)] == Idx
&&
220 "Mapping out of date");
221 Ctx
->ScopeInlinedAtIdx
.erase(std::make_pair(OldScope
, OldInlinedAt
));
223 // Reset this VH to null. Drop both 'Idx' values to null to indicate that
224 // we're in non-canonical form now.
226 Entry
.first
.Idx
= Entry
.second
.Idx
= 0;
229 void DebugRecVH::allUsesReplacedWith(Value
*NewVa
) {
230 // If being replaced with a non-mdnode value (e.g. undef) handle this as if
231 // the mdnode got deleted.
232 MDNode
*NewVal
= dyn_cast
<MDNode
>(NewVa
);
233 if (NewVal
== 0) return deleted();
235 // If this is a non-canonical reference, just change it, we know it already
236 // doesn't have a map entry.
242 MDNode
*OldVal
= get();
243 assert(OldVal
!= NewVa
&& "Node replaced with self?");
245 // If the index is positive, it is an entry in ScopeRecords.
247 assert(Ctx
->ScopeRecordIdx
[OldVal
] == Idx
&& "Mapping out of date!");
248 Ctx
->ScopeRecordIdx
.erase(OldVal
);
251 int NewEntry
= Ctx
->getOrAddScopeRecordIdxEntry(NewVal
, Idx
);
253 // If NewVal already has an entry, this becomes a non-canonical reference,
254 // just drop Idx to 0 to signify this.
260 // Otherwise, it is an entry in ScopeInlinedAtRecords, we don't know if it
261 // is the scope or the inlined-at record entry.
262 assert(unsigned(-Idx
-1) < Ctx
->ScopeInlinedAtRecords
.size());
263 std::pair
<DebugRecVH
, DebugRecVH
> &Entry
= Ctx
->ScopeInlinedAtRecords
[-Idx
-1];
264 assert((this == &Entry
.first
|| this == &Entry
.second
) &&
265 "Mapping out of date!");
267 MDNode
*OldScope
= Entry
.first
.get();
268 MDNode
*OldInlinedAt
= Entry
.second
.get();
269 assert(OldScope
!= 0 && OldInlinedAt
!= 0 &&
270 "Entry should be non-canonical if either val dropped to null");
272 // Otherwise, we do have an entry in it, nuke it and we're done.
273 assert(Ctx
->ScopeInlinedAtIdx
[std::make_pair(OldScope
, OldInlinedAt
)] == Idx
&&
274 "Mapping out of date");
275 Ctx
->ScopeInlinedAtIdx
.erase(std::make_pair(OldScope
, OldInlinedAt
));
277 // Reset this VH to the new value.
280 int NewIdx
= Ctx
->getOrAddScopeInlinedAtIdxEntry(Entry
.first
.get(),
281 Entry
.second
.get(), Idx
);
282 // If NewVal already has an entry, this becomes a non-canonical reference,
283 // just drop Idx to 0 to signify this.
285 std::pair
<DebugRecVH
, DebugRecVH
> &Entry
=Ctx
->ScopeInlinedAtRecords
[-Idx
-1];
286 Entry
.first
.Idx
= Entry
.second
.Idx
= 0;