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