Simplify and delay extracting DebugLoc elements, scope and InlinedAt, as much as...
[oota-llvm.git] / lib / VMCore / DebugLoc.cpp
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
19 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
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;
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.
48 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
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.
99 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   const 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.
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.
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
147 //===----------------------------------------------------------------------===//
148 // DenseMap specialization
149 //===----------------------------------------------------------------------===//
150
151 DebugLoc DenseMapInfo<DebugLoc>::getEmptyKey() {
152   return DebugLoc::getEmptyKey();
153 }
154
155 DebugLoc DenseMapInfo<DebugLoc>::getTombstoneKey() {
156   return DebugLoc::getTombstoneKey();
157 }
158
159 unsigned DenseMapInfo<DebugLoc>::getHashValue(const DebugLoc &Key) {
160   FoldingSetNodeID ID;
161   ID.AddInteger(Key.LineCol);
162   ID.AddInteger(Key.ScopeIdx);
163   return ID.ComputeHash();
164 }
165
166 bool DenseMapInfo<DebugLoc>::isEqual(const DebugLoc &LHS, const DebugLoc &RHS) {
167   return LHS == RHS;
168 }
169
170 //===----------------------------------------------------------------------===//
171 // LLVMContextImpl Implementation
172 //===----------------------------------------------------------------------===//
173
174 int LLVMContextImpl::getOrAddScopeRecordIdxEntry(MDNode *Scope,
175                                                  int ExistingIdx) {
176   // If we already have an entry for this scope, return it.
177   int &Idx = ScopeRecordIdx[Scope];
178   if (Idx) return Idx;
179   
180   // If we don't have an entry, but ExistingIdx is specified, use it.
181   if (ExistingIdx)
182     return Idx = ExistingIdx;
183   
184   // Otherwise add a new entry.
185   
186   // Start out ScopeRecords with a minimal reasonable size to avoid
187   // excessive reallocation starting out.
188   if (ScopeRecords.empty())
189     ScopeRecords.reserve(128);
190   
191   // Index is biased by 1 for index.
192   Idx = ScopeRecords.size()+1;
193   ScopeRecords.push_back(DebugRecVH(Scope, this, Idx));
194   return Idx;
195 }
196
197 int LLVMContextImpl::getOrAddScopeInlinedAtIdxEntry(MDNode *Scope, MDNode *IA,
198                                                     int ExistingIdx) {
199   // If we already have an entry, return it.
200   int &Idx = ScopeInlinedAtIdx[std::make_pair(Scope, IA)];
201   if (Idx) return Idx;
202   
203   // If we don't have an entry, but ExistingIdx is specified, use it.
204   if (ExistingIdx)
205     return Idx = ExistingIdx;
206   
207   // Start out ScopeInlinedAtRecords with a minimal reasonable size to avoid
208   // excessive reallocation starting out.
209   if (ScopeInlinedAtRecords.empty())
210     ScopeInlinedAtRecords.reserve(128);
211     
212   // Index is biased by 1 and negated.
213   Idx = -ScopeInlinedAtRecords.size()-1;
214   ScopeInlinedAtRecords.push_back(std::make_pair(DebugRecVH(Scope, this, Idx),
215                                                  DebugRecVH(IA, this, Idx)));
216   return Idx;
217 }
218
219
220 //===----------------------------------------------------------------------===//
221 // DebugRecVH Implementation
222 //===----------------------------------------------------------------------===//
223
224 /// deleted - The MDNode this is pointing to got deleted, so this pointer needs
225 /// to drop to null and we need remove our entry from the DenseMap.
226 void DebugRecVH::deleted() {
227   // If this is a  non-canonical reference, just drop the value to null, we know
228   // it doesn't have a map entry.
229   if (Idx == 0) {
230     setValPtr(0);
231     return;
232   }
233     
234   MDNode *Cur = get();
235   
236   // If the index is positive, it is an entry in ScopeRecords.
237   if (Idx > 0) {
238     assert(Ctx->ScopeRecordIdx[Cur] == Idx && "Mapping out of date!");
239     Ctx->ScopeRecordIdx.erase(Cur);
240     // Reset this VH to null and we're done.
241     setValPtr(0);
242     Idx = 0;
243     return;
244   }
245   
246   // Otherwise, it is an entry in ScopeInlinedAtRecords, we don't know if it
247   // is the scope or the inlined-at record entry.
248   assert(unsigned(-Idx-1) < Ctx->ScopeInlinedAtRecords.size());
249   std::pair<DebugRecVH, DebugRecVH> &Entry = Ctx->ScopeInlinedAtRecords[-Idx-1];
250   assert((this == &Entry.first || this == &Entry.second) &&
251          "Mapping out of date!");
252   
253   MDNode *OldScope = Entry.first.get();
254   MDNode *OldInlinedAt = Entry.second.get();
255   assert(OldScope != 0 && OldInlinedAt != 0 &&
256          "Entry should be non-canonical if either val dropped to null");
257
258   // Otherwise, we do have an entry in it, nuke it and we're done.
259   assert(Ctx->ScopeInlinedAtIdx[std::make_pair(OldScope, OldInlinedAt)] == Idx&&
260          "Mapping out of date");
261   Ctx->ScopeInlinedAtIdx.erase(std::make_pair(OldScope, OldInlinedAt));
262   
263   // Reset this VH to null.  Drop both 'Idx' values to null to indicate that
264   // we're in non-canonical form now.
265   setValPtr(0);
266   Entry.first.Idx = Entry.second.Idx = 0;
267 }
268
269 void DebugRecVH::allUsesReplacedWith(Value *NewVa) {
270   // If being replaced with a non-mdnode value (e.g. undef) handle this as if
271   // the mdnode got deleted.
272   MDNode *NewVal = dyn_cast<MDNode>(NewVa);
273   if (NewVal == 0) return deleted();
274   
275   // If this is a non-canonical reference, just change it, we know it already
276   // doesn't have a map entry.
277   if (Idx == 0) {
278     setValPtr(NewVa);
279     return;
280   }
281   
282   MDNode *OldVal = get();
283   assert(OldVal != NewVa && "Node replaced with self?");
284   
285   // If the index is positive, it is an entry in ScopeRecords.
286   if (Idx > 0) {
287     assert(Ctx->ScopeRecordIdx[OldVal] == Idx && "Mapping out of date!");
288     Ctx->ScopeRecordIdx.erase(OldVal);
289     setValPtr(NewVal);
290
291     int NewEntry = Ctx->getOrAddScopeRecordIdxEntry(NewVal, Idx);
292     
293     // If NewVal already has an entry, this becomes a non-canonical reference,
294     // just drop Idx to 0 to signify this.
295     if (NewEntry != Idx)
296       Idx = 0;
297     return;
298   }
299   
300   // Otherwise, it is an entry in ScopeInlinedAtRecords, we don't know if it
301   // is the scope or the inlined-at record entry.
302   assert(unsigned(-Idx-1) < Ctx->ScopeInlinedAtRecords.size());
303   std::pair<DebugRecVH, DebugRecVH> &Entry = Ctx->ScopeInlinedAtRecords[-Idx-1];
304   assert((this == &Entry.first || this == &Entry.second) &&
305          "Mapping out of date!");
306   
307   MDNode *OldScope = Entry.first.get();
308   MDNode *OldInlinedAt = Entry.second.get();
309   assert(OldScope != 0 && OldInlinedAt != 0 &&
310          "Entry should be non-canonical if either val dropped to null");
311   
312   // Otherwise, we do have an entry in it, nuke it and we're done.
313   assert(Ctx->ScopeInlinedAtIdx[std::make_pair(OldScope, OldInlinedAt)] == Idx&&
314          "Mapping out of date");
315   Ctx->ScopeInlinedAtIdx.erase(std::make_pair(OldScope, OldInlinedAt));
316   
317   // Reset this VH to the new value.
318   setValPtr(NewVal);
319
320   int NewIdx = Ctx->getOrAddScopeInlinedAtIdxEntry(Entry.first.get(),
321                                                    Entry.second.get(), Idx);
322   // If NewVal already has an entry, this becomes a non-canonical reference,
323   // just drop Idx to 0 to signify this.
324   if (NewIdx != Idx) {
325     std::pair<DebugRecVH, DebugRecVH> &Entry=Ctx->ScopeInlinedAtRecords[-Idx-1];
326     Entry.first.Idx = Entry.second.Idx = 0;
327   }
328 }