Chris Lattner | 8cb2aeb | 2010-04-01 00:37:44 +0000 | [diff] [blame] | 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" |
Nick Lewycky | ddc7289 | 2011-04-06 05:36:52 +0000 | [diff] [blame^] | 11 | #include "llvm/ADT/DenseMapInfo.h" |
Chris Lattner | 8cb2aeb | 2010-04-01 00:37:44 +0000 | [diff] [blame] | 12 | #include "LLVMContextImpl.h" |
| 13 | using namespace llvm; |
| 14 | |
| 15 | //===----------------------------------------------------------------------===// |
| 16 | // DebugLoc Implementation |
| 17 | //===----------------------------------------------------------------------===// |
| 18 | |
Chris Lattner | 593916d | 2010-04-02 20:21:22 +0000 | [diff] [blame] | 19 | MDNode *DebugLoc::getScope(const LLVMContext &Ctx) const { |
Chris Lattner | 8cb2aeb | 2010-04-01 00:37:44 +0000 | [diff] [blame] | 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 | |
Chris Lattner | 593916d | 2010-04-02 20:21:22 +0000 | [diff] [blame] | 36 | MDNode *DebugLoc::getInlinedAt(const LLVMContext &Ctx) const { |
Chris Lattner | 8cb2aeb | 2010-04-01 00:37:44 +0000 | [diff] [blame] | 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. |
Chris Lattner | 593916d | 2010-04-02 20:21:22 +0000 | [diff] [blame] | 48 | void DebugLoc::getScopeAndInlinedAt(MDNode *&Scope, MDNode *&IA, |
| 49 | const LLVMContext &Ctx) const { |
Chris Lattner | 8cb2aeb | 2010-04-01 00:37:44 +0000 | [diff] [blame] | 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 | |
Chris Lattner | 593916d | 2010-04-02 20:21:22 +0000 | [diff] [blame] | 73 | DebugLoc DebugLoc::get(unsigned Line, unsigned Col, |
| 74 | MDNode *Scope, MDNode *InlinedAt) { |
| 75 | DebugLoc Result; |
Chris Lattner | 8cb2aeb | 2010-04-01 00:37:44 +0000 | [diff] [blame] | 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. |
Chris Lattner | 593916d | 2010-04-02 20:21:22 +0000 | [diff] [blame] | 99 | MDNode *DebugLoc::getAsMDNode(const LLVMContext &Ctx) const { |
Chris Lattner | 8cb2aeb | 2010-04-01 00:37:44 +0000 | [diff] [blame] | 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[0], 4); |
| 113 | } |
| 114 | |
Chris Lattner | 593916d | 2010-04-02 20:21:22 +0000 | [diff] [blame] | 115 | /// getFromDILocation - Translate the DILocation quad into a DebugLoc. |
| 116 | DebugLoc DebugLoc::getFromDILocation(MDNode *N) { |
| 117 | if (N == 0 || N->getNumOperands() != 4) return DebugLoc(); |
Chris Lattner | 56e4b4a | 2010-04-01 03:55:42 +0000 | [diff] [blame] | 118 | |
| 119 | MDNode *Scope = dyn_cast_or_null<MDNode>(N->getOperand(2)); |
Chris Lattner | 593916d | 2010-04-02 20:21:22 +0000 | [diff] [blame] | 120 | if (Scope == 0) return DebugLoc(); |
Chris Lattner | 56e4b4a | 2010-04-01 03:55:42 +0000 | [diff] [blame] | 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 | } |
Chris Lattner | 8cb2aeb | 2010-04-01 00:37:44 +0000 | [diff] [blame] | 130 | |
| 131 | //===----------------------------------------------------------------------===// |
Nick Lewycky | ddc7289 | 2011-04-06 05:36:52 +0000 | [diff] [blame^] | 132 | // DenseMap specialization |
| 133 | //===----------------------------------------------------------------------===// |
| 134 | |
| 135 | DebugLoc DenseMapInfo<DebugLoc>::getEmptyKey() { |
| 136 | return DebugLoc(); |
| 137 | } |
| 138 | |
| 139 | DebugLoc DenseMapInfo<DebugLoc>::getTombstoneKey() { |
| 140 | return DebugLoc::getTombstoneKey(); |
| 141 | } |
| 142 | |
| 143 | unsigned DenseMapInfo<DebugLoc>::getHashValue(const DebugLoc &Key) { |
| 144 | FoldingSetNodeID ID; |
| 145 | ID.AddInteger(Key.LineCol); |
| 146 | ID.AddInteger(Key.ScopeIdx); |
| 147 | return ID.ComputeHash(); |
| 148 | } |
| 149 | |
| 150 | bool DenseMapInfo<DebugLoc>::isEqual(const DebugLoc &LHS, const DebugLoc &RHS) { |
| 151 | return LHS == RHS; |
| 152 | } |
| 153 | |
| 154 | //===----------------------------------------------------------------------===// |
Chris Lattner | 8cb2aeb | 2010-04-01 00:37:44 +0000 | [diff] [blame] | 155 | // LLVMContextImpl Implementation |
| 156 | //===----------------------------------------------------------------------===// |
| 157 | |
| 158 | int LLVMContextImpl::getOrAddScopeRecordIdxEntry(MDNode *Scope, |
| 159 | int ExistingIdx) { |
| 160 | // If we already have an entry for this scope, return it. |
| 161 | int &Idx = ScopeRecordIdx[Scope]; |
| 162 | if (Idx) return Idx; |
| 163 | |
| 164 | // If we don't have an entry, but ExistingIdx is specified, use it. |
| 165 | if (ExistingIdx) |
| 166 | return Idx = ExistingIdx; |
| 167 | |
| 168 | // Otherwise add a new entry. |
| 169 | |
| 170 | // Start out ScopeRecords with a minimal reasonable size to avoid |
| 171 | // excessive reallocation starting out. |
| 172 | if (ScopeRecords.empty()) |
| 173 | ScopeRecords.reserve(128); |
| 174 | |
| 175 | // Index is biased by 1 for index. |
| 176 | Idx = ScopeRecords.size()+1; |
| 177 | ScopeRecords.push_back(DebugRecVH(Scope, this, Idx)); |
| 178 | return Idx; |
| 179 | } |
| 180 | |
| 181 | int LLVMContextImpl::getOrAddScopeInlinedAtIdxEntry(MDNode *Scope, MDNode *IA, |
| 182 | int ExistingIdx) { |
| 183 | // If we already have an entry, return it. |
| 184 | int &Idx = ScopeInlinedAtIdx[std::make_pair(Scope, IA)]; |
| 185 | if (Idx) return Idx; |
| 186 | |
| 187 | // If we don't have an entry, but ExistingIdx is specified, use it. |
| 188 | if (ExistingIdx) |
| 189 | return Idx = ExistingIdx; |
| 190 | |
| 191 | // Start out ScopeInlinedAtRecords with a minimal reasonable size to avoid |
| 192 | // excessive reallocation starting out. |
| 193 | if (ScopeInlinedAtRecords.empty()) |
| 194 | ScopeInlinedAtRecords.reserve(128); |
| 195 | |
| 196 | // Index is biased by 1 and negated. |
| 197 | Idx = -ScopeInlinedAtRecords.size()-1; |
| 198 | ScopeInlinedAtRecords.push_back(std::make_pair(DebugRecVH(Scope, this, Idx), |
| 199 | DebugRecVH(IA, this, Idx))); |
| 200 | return Idx; |
| 201 | } |
| 202 | |
| 203 | |
| 204 | //===----------------------------------------------------------------------===// |
| 205 | // DebugRecVH Implementation |
| 206 | //===----------------------------------------------------------------------===// |
| 207 | |
| 208 | /// deleted - The MDNode this is pointing to got deleted, so this pointer needs |
| 209 | /// to drop to null and we need remove our entry from the DenseMap. |
| 210 | void DebugRecVH::deleted() { |
| 211 | // If this is a non-canonical reference, just drop the value to null, we know |
| 212 | // it doesn't have a map entry. |
| 213 | if (Idx == 0) { |
| 214 | setValPtr(0); |
| 215 | return; |
| 216 | } |
| 217 | |
| 218 | MDNode *Cur = get(); |
| 219 | |
| 220 | // If the index is positive, it is an entry in ScopeRecords. |
| 221 | if (Idx > 0) { |
| 222 | assert(Ctx->ScopeRecordIdx[Cur] == Idx && "Mapping out of date!"); |
| 223 | Ctx->ScopeRecordIdx.erase(Cur); |
| 224 | // Reset this VH to null and we're done. |
| 225 | setValPtr(0); |
| 226 | Idx = 0; |
| 227 | return; |
| 228 | } |
| 229 | |
| 230 | // Otherwise, it is an entry in ScopeInlinedAtRecords, we don't know if it |
| 231 | // is the scope or the inlined-at record entry. |
| 232 | assert(unsigned(-Idx-1) < Ctx->ScopeInlinedAtRecords.size()); |
| 233 | std::pair<DebugRecVH, DebugRecVH> &Entry = Ctx->ScopeInlinedAtRecords[-Idx-1]; |
| 234 | assert((this == &Entry.first || this == &Entry.second) && |
| 235 | "Mapping out of date!"); |
| 236 | |
| 237 | MDNode *OldScope = Entry.first.get(); |
| 238 | MDNode *OldInlinedAt = Entry.second.get(); |
| 239 | assert(OldScope != 0 && OldInlinedAt != 0 && |
| 240 | "Entry should be non-canonical if either val dropped to null"); |
| 241 | |
| 242 | // Otherwise, we do have an entry in it, nuke it and we're done. |
| 243 | assert(Ctx->ScopeInlinedAtIdx[std::make_pair(OldScope, OldInlinedAt)] == Idx&& |
| 244 | "Mapping out of date"); |
| 245 | Ctx->ScopeInlinedAtIdx.erase(std::make_pair(OldScope, OldInlinedAt)); |
| 246 | |
Chris Lattner | cf25a89 | 2010-04-01 05:12:07 +0000 | [diff] [blame] | 247 | // Reset this VH to null. Drop both 'Idx' values to null to indicate that |
| 248 | // we're in non-canonical form now. |
Chris Lattner | 8cb2aeb | 2010-04-01 00:37:44 +0000 | [diff] [blame] | 249 | setValPtr(0); |
Chris Lattner | cf25a89 | 2010-04-01 05:12:07 +0000 | [diff] [blame] | 250 | Entry.first.Idx = Entry.second.Idx = 0; |
Chris Lattner | 8cb2aeb | 2010-04-01 00:37:44 +0000 | [diff] [blame] | 251 | } |
| 252 | |
| 253 | void DebugRecVH::allUsesReplacedWith(Value *NewVa) { |
| 254 | // If being replaced with a non-mdnode value (e.g. undef) handle this as if |
| 255 | // the mdnode got deleted. |
| 256 | MDNode *NewVal = dyn_cast<MDNode>(NewVa); |
| 257 | if (NewVal == 0) return deleted(); |
| 258 | |
| 259 | // If this is a non-canonical reference, just change it, we know it already |
| 260 | // doesn't have a map entry. |
| 261 | if (Idx == 0) { |
| 262 | setValPtr(NewVa); |
| 263 | return; |
| 264 | } |
| 265 | |
| 266 | MDNode *OldVal = get(); |
| 267 | assert(OldVal != NewVa && "Node replaced with self?"); |
| 268 | |
| 269 | // If the index is positive, it is an entry in ScopeRecords. |
| 270 | if (Idx > 0) { |
| 271 | assert(Ctx->ScopeRecordIdx[OldVal] == Idx && "Mapping out of date!"); |
| 272 | Ctx->ScopeRecordIdx.erase(OldVal); |
| 273 | setValPtr(NewVal); |
| 274 | |
| 275 | int NewEntry = Ctx->getOrAddScopeRecordIdxEntry(NewVal, Idx); |
| 276 | |
| 277 | // If NewVal already has an entry, this becomes a non-canonical reference, |
| 278 | // just drop Idx to 0 to signify this. |
| 279 | if (NewEntry != Idx) |
| 280 | Idx = 0; |
| 281 | return; |
| 282 | } |
| 283 | |
| 284 | // Otherwise, it is an entry in ScopeInlinedAtRecords, we don't know if it |
| 285 | // is the scope or the inlined-at record entry. |
| 286 | assert(unsigned(-Idx-1) < Ctx->ScopeInlinedAtRecords.size()); |
| 287 | std::pair<DebugRecVH, DebugRecVH> &Entry = Ctx->ScopeInlinedAtRecords[-Idx-1]; |
| 288 | assert((this == &Entry.first || this == &Entry.second) && |
| 289 | "Mapping out of date!"); |
| 290 | |
| 291 | MDNode *OldScope = Entry.first.get(); |
| 292 | MDNode *OldInlinedAt = Entry.second.get(); |
| 293 | assert(OldScope != 0 && OldInlinedAt != 0 && |
| 294 | "Entry should be non-canonical if either val dropped to null"); |
| 295 | |
| 296 | // Otherwise, we do have an entry in it, nuke it and we're done. |
| 297 | assert(Ctx->ScopeInlinedAtIdx[std::make_pair(OldScope, OldInlinedAt)] == Idx&& |
| 298 | "Mapping out of date"); |
| 299 | Ctx->ScopeInlinedAtIdx.erase(std::make_pair(OldScope, OldInlinedAt)); |
| 300 | |
| 301 | // Reset this VH to the new value. |
| 302 | setValPtr(NewVal); |
| 303 | |
| 304 | int NewIdx = Ctx->getOrAddScopeInlinedAtIdxEntry(Entry.first.get(), |
| 305 | Entry.second.get(), Idx); |
| 306 | // If NewVal already has an entry, this becomes a non-canonical reference, |
| 307 | // just drop Idx to 0 to signify this. |
Chris Lattner | cf25a89 | 2010-04-01 05:12:07 +0000 | [diff] [blame] | 308 | if (NewIdx != Idx) { |
| 309 | std::pair<DebugRecVH, DebugRecVH> &Entry=Ctx->ScopeInlinedAtRecords[-Idx-1]; |
| 310 | Entry.first.Idx = Entry.second.Idx = 0; |
| 311 | } |
Chris Lattner | 8cb2aeb | 2010-04-01 00:37:44 +0000 | [diff] [blame] | 312 | } |