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 | |
Chandler Carruth | 9205140 | 2014-03-05 10:30:38 +0000 | [diff] [blame] | 10 | #include "llvm/IR/DebugLoc.h" |
Chris Lattner | 8cb2aeb | 2010-04-01 00:37:44 +0000 | [diff] [blame] | 11 | #include "LLVMContextImpl.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 12 | #include "llvm/ADT/DenseMapInfo.h" |
Chandler Carruth | 9a4c9e5 | 2014-03-06 00:46:21 +0000 | [diff] [blame^] | 13 | #include "llvm/IR/DebugInfo.h" |
Chris Lattner | 8cb2aeb | 2010-04-01 00:37:44 +0000 | [diff] [blame] | 14 | using namespace llvm; |
| 15 | |
| 16 | //===----------------------------------------------------------------------===// |
| 17 | // DebugLoc Implementation |
| 18 | //===----------------------------------------------------------------------===// |
| 19 | |
Chris Lattner | 593916d | 2010-04-02 20:21:22 +0000 | [diff] [blame] | 20 | MDNode *DebugLoc::getScope(const LLVMContext &Ctx) const { |
Chris Lattner | 8cb2aeb | 2010-04-01 00:37:44 +0000 | [diff] [blame] | 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 | |
Chris Lattner | 593916d | 2010-04-02 20:21:22 +0000 | [diff] [blame] | 37 | MDNode *DebugLoc::getInlinedAt(const LLVMContext &Ctx) const { |
Chris Lattner | 8cb2aeb | 2010-04-01 00:37:44 +0000 | [diff] [blame] | 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. |
Chris Lattner | 593916d | 2010-04-02 20:21:22 +0000 | [diff] [blame] | 49 | void DebugLoc::getScopeAndInlinedAt(MDNode *&Scope, MDNode *&IA, |
| 50 | const LLVMContext &Ctx) const { |
Chris Lattner | 8cb2aeb | 2010-04-01 00:37:44 +0000 | [diff] [blame] | 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 | |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 73 | MDNode *DebugLoc::getScopeNode(const LLVMContext &Ctx) const { |
| 74 | if (MDNode *InlinedAt = getInlinedAt(Ctx)) |
| 75 | return DebugLoc::getFromDILocation(InlinedAt).getScopeNode(Ctx); |
| 76 | return getScope(Ctx); |
| 77 | } |
| 78 | |
| 79 | DebugLoc DebugLoc::getFnDebugLoc(const LLVMContext &Ctx) { |
| 80 | const MDNode *Scope = getScopeNode(Ctx); |
| 81 | DISubprogram SP = getDISubprogram(Scope); |
| 82 | if (SP.isSubprogram()) { |
| 83 | // Check for number of operands since the compatibility is |
| 84 | // cheap here. FIXME: Name the magic constant. |
| 85 | if (SP->getNumOperands() > 19) |
| 86 | return DebugLoc::get(SP.getScopeLineNumber(), 0, SP); |
| 87 | else |
| 88 | return DebugLoc::get(SP.getLineNumber(), 0, SP); |
| 89 | } |
| 90 | |
| 91 | return DebugLoc(); |
| 92 | } |
Chris Lattner | 8cb2aeb | 2010-04-01 00:37:44 +0000 | [diff] [blame] | 93 | |
Chris Lattner | 593916d | 2010-04-02 20:21:22 +0000 | [diff] [blame] | 94 | DebugLoc DebugLoc::get(unsigned Line, unsigned Col, |
| 95 | MDNode *Scope, MDNode *InlinedAt) { |
| 96 | DebugLoc Result; |
Chris Lattner | 8cb2aeb | 2010-04-01 00:37:44 +0000 | [diff] [blame] | 97 | |
| 98 | // If no scope is available, this is an unknown location. |
| 99 | if (Scope == 0) return Result; |
| 100 | |
| 101 | // Saturate line and col to "unknown". |
| 102 | if (Col > 255) Col = 0; |
| 103 | if (Line >= (1 << 24)) Line = 0; |
| 104 | Result.LineCol = Line | (Col << 24); |
| 105 | |
| 106 | LLVMContext &Ctx = Scope->getContext(); |
| 107 | |
| 108 | // If there is no inlined-at location, use the ScopeRecords array. |
| 109 | if (InlinedAt == 0) |
| 110 | Result.ScopeIdx = Ctx.pImpl->getOrAddScopeRecordIdxEntry(Scope, 0); |
| 111 | else |
| 112 | Result.ScopeIdx = Ctx.pImpl->getOrAddScopeInlinedAtIdxEntry(Scope, |
| 113 | InlinedAt, 0); |
| 114 | |
| 115 | return Result; |
| 116 | } |
| 117 | |
| 118 | /// getAsMDNode - This method converts the compressed DebugLoc node into a |
Alon Mishne | 0394c1e | 2014-02-05 14:23:18 +0000 | [diff] [blame] | 119 | /// DILocation-compatible MDNode. |
Chris Lattner | 593916d | 2010-04-02 20:21:22 +0000 | [diff] [blame] | 120 | MDNode *DebugLoc::getAsMDNode(const LLVMContext &Ctx) const { |
Chris Lattner | 8cb2aeb | 2010-04-01 00:37:44 +0000 | [diff] [blame] | 121 | if (isUnknown()) return 0; |
| 122 | |
| 123 | MDNode *Scope, *IA; |
| 124 | getScopeAndInlinedAt(Scope, IA, Ctx); |
| 125 | assert(Scope && "If scope is null, this should be isUnknown()"); |
| 126 | |
| 127 | LLVMContext &Ctx2 = Scope->getContext(); |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 128 | Type *Int32 = Type::getInt32Ty(Ctx2); |
Chris Lattner | 8cb2aeb | 2010-04-01 00:37:44 +0000 | [diff] [blame] | 129 | Value *Elts[] = { |
| 130 | ConstantInt::get(Int32, getLine()), ConstantInt::get(Int32, getCol()), |
| 131 | Scope, IA |
| 132 | }; |
Jay Foad | 5514afe | 2011-04-21 19:59:31 +0000 | [diff] [blame] | 133 | return MDNode::get(Ctx2, Elts); |
Chris Lattner | 8cb2aeb | 2010-04-01 00:37:44 +0000 | [diff] [blame] | 134 | } |
| 135 | |
Chris Lattner | 593916d | 2010-04-02 20:21:22 +0000 | [diff] [blame] | 136 | /// getFromDILocation - Translate the DILocation quad into a DebugLoc. |
| 137 | DebugLoc DebugLoc::getFromDILocation(MDNode *N) { |
Bill Wendling | 786de35 | 2012-07-07 00:52:35 +0000 | [diff] [blame] | 138 | DILocation Loc(N); |
| 139 | MDNode *Scope = Loc.getScope(); |
Chris Lattner | 593916d | 2010-04-02 20:21:22 +0000 | [diff] [blame] | 140 | if (Scope == 0) return DebugLoc(); |
Bill Wendling | 786de35 | 2012-07-07 00:52:35 +0000 | [diff] [blame] | 141 | return get(Loc.getLineNumber(), Loc.getColumnNumber(), Scope, |
| 142 | Loc.getOrigLocation()); |
Chris Lattner | 56e4b4a | 2010-04-01 03:55:42 +0000 | [diff] [blame] | 143 | } |
Chris Lattner | 8cb2aeb | 2010-04-01 00:37:44 +0000 | [diff] [blame] | 144 | |
Devang Patel | 07d61ed | 2011-07-14 01:14:57 +0000 | [diff] [blame] | 145 | /// getFromDILexicalBlock - Translate the DILexicalBlock into a DebugLoc. |
| 146 | DebugLoc DebugLoc::getFromDILexicalBlock(MDNode *N) { |
Bill Wendling | 786de35 | 2012-07-07 00:52:35 +0000 | [diff] [blame] | 147 | DILexicalBlock LexBlock(N); |
| 148 | MDNode *Scope = LexBlock.getContext(); |
Devang Patel | 07d61ed | 2011-07-14 01:14:57 +0000 | [diff] [blame] | 149 | if (Scope == 0) return DebugLoc(); |
Bill Wendling | 786de35 | 2012-07-07 00:52:35 +0000 | [diff] [blame] | 150 | return get(LexBlock.getLineNumber(), LexBlock.getColumnNumber(), Scope, NULL); |
Devang Patel | 07d61ed | 2011-07-14 01:14:57 +0000 | [diff] [blame] | 151 | } |
| 152 | |
Devang Patel | 4db3844 | 2011-07-14 21:50:04 +0000 | [diff] [blame] | 153 | void DebugLoc::dump(const LLVMContext &Ctx) const { |
| 154 | #ifndef NDEBUG |
| 155 | if (!isUnknown()) { |
| 156 | dbgs() << getLine(); |
| 157 | if (getCol() != 0) |
| 158 | dbgs() << ',' << getCol(); |
| 159 | DebugLoc InlinedAtDL = DebugLoc::getFromDILocation(getInlinedAt(Ctx)); |
| 160 | if (!InlinedAtDL.isUnknown()) { |
| 161 | dbgs() << " @ "; |
| 162 | InlinedAtDL.dump(Ctx); |
| 163 | } else |
| 164 | dbgs() << "\n"; |
| 165 | } |
| 166 | #endif |
| 167 | } |
| 168 | |
Chris Lattner | 8cb2aeb | 2010-04-01 00:37:44 +0000 | [diff] [blame] | 169 | //===----------------------------------------------------------------------===// |
Nick Lewycky | ddc7289 | 2011-04-06 05:36:52 +0000 | [diff] [blame] | 170 | // DenseMap specialization |
| 171 | //===----------------------------------------------------------------------===// |
| 172 | |
Nick Lewycky | ddc7289 | 2011-04-06 05:36:52 +0000 | [diff] [blame] | 173 | unsigned DenseMapInfo<DebugLoc>::getHashValue(const DebugLoc &Key) { |
Benjamin Kramer | 7a426b5 | 2012-04-11 14:06:39 +0000 | [diff] [blame] | 174 | return static_cast<unsigned>(hash_combine(Key.LineCol, Key.ScopeIdx)); |
Nick Lewycky | ddc7289 | 2011-04-06 05:36:52 +0000 | [diff] [blame] | 175 | } |
| 176 | |
Nick Lewycky | ddc7289 | 2011-04-06 05:36:52 +0000 | [diff] [blame] | 177 | //===----------------------------------------------------------------------===// |
Chris Lattner | 8cb2aeb | 2010-04-01 00:37:44 +0000 | [diff] [blame] | 178 | // LLVMContextImpl Implementation |
| 179 | //===----------------------------------------------------------------------===// |
| 180 | |
| 181 | int LLVMContextImpl::getOrAddScopeRecordIdxEntry(MDNode *Scope, |
| 182 | int ExistingIdx) { |
| 183 | // If we already have an entry for this scope, return it. |
| 184 | int &Idx = ScopeRecordIdx[Scope]; |
| 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 | // Otherwise add a new entry. |
| 192 | |
| 193 | // Start out ScopeRecords with a minimal reasonable size to avoid |
| 194 | // excessive reallocation starting out. |
| 195 | if (ScopeRecords.empty()) |
| 196 | ScopeRecords.reserve(128); |
| 197 | |
| 198 | // Index is biased by 1 for index. |
| 199 | Idx = ScopeRecords.size()+1; |
| 200 | ScopeRecords.push_back(DebugRecVH(Scope, this, Idx)); |
| 201 | return Idx; |
| 202 | } |
| 203 | |
| 204 | int LLVMContextImpl::getOrAddScopeInlinedAtIdxEntry(MDNode *Scope, MDNode *IA, |
| 205 | int ExistingIdx) { |
| 206 | // If we already have an entry, return it. |
| 207 | int &Idx = ScopeInlinedAtIdx[std::make_pair(Scope, IA)]; |
| 208 | if (Idx) return Idx; |
| 209 | |
| 210 | // If we don't have an entry, but ExistingIdx is specified, use it. |
| 211 | if (ExistingIdx) |
| 212 | return Idx = ExistingIdx; |
| 213 | |
| 214 | // Start out ScopeInlinedAtRecords with a minimal reasonable size to avoid |
| 215 | // excessive reallocation starting out. |
| 216 | if (ScopeInlinedAtRecords.empty()) |
| 217 | ScopeInlinedAtRecords.reserve(128); |
| 218 | |
| 219 | // Index is biased by 1 and negated. |
| 220 | Idx = -ScopeInlinedAtRecords.size()-1; |
| 221 | ScopeInlinedAtRecords.push_back(std::make_pair(DebugRecVH(Scope, this, Idx), |
| 222 | DebugRecVH(IA, this, Idx))); |
| 223 | return Idx; |
| 224 | } |
| 225 | |
| 226 | |
| 227 | //===----------------------------------------------------------------------===// |
| 228 | // DebugRecVH Implementation |
| 229 | //===----------------------------------------------------------------------===// |
| 230 | |
| 231 | /// deleted - The MDNode this is pointing to got deleted, so this pointer needs |
| 232 | /// to drop to null and we need remove our entry from the DenseMap. |
| 233 | void DebugRecVH::deleted() { |
Eric Christopher | cbce39c | 2011-10-11 22:58:58 +0000 | [diff] [blame] | 234 | // If this is a non-canonical reference, just drop the value to null, we know |
Chris Lattner | 8cb2aeb | 2010-04-01 00:37:44 +0000 | [diff] [blame] | 235 | // it doesn't have a map entry. |
| 236 | if (Idx == 0) { |
| 237 | setValPtr(0); |
| 238 | return; |
| 239 | } |
| 240 | |
| 241 | MDNode *Cur = get(); |
| 242 | |
| 243 | // If the index is positive, it is an entry in ScopeRecords. |
| 244 | if (Idx > 0) { |
| 245 | assert(Ctx->ScopeRecordIdx[Cur] == Idx && "Mapping out of date!"); |
| 246 | Ctx->ScopeRecordIdx.erase(Cur); |
| 247 | // Reset this VH to null and we're done. |
| 248 | setValPtr(0); |
| 249 | Idx = 0; |
| 250 | return; |
| 251 | } |
| 252 | |
| 253 | // Otherwise, it is an entry in ScopeInlinedAtRecords, we don't know if it |
| 254 | // is the scope or the inlined-at record entry. |
| 255 | assert(unsigned(-Idx-1) < Ctx->ScopeInlinedAtRecords.size()); |
| 256 | std::pair<DebugRecVH, DebugRecVH> &Entry = Ctx->ScopeInlinedAtRecords[-Idx-1]; |
| 257 | assert((this == &Entry.first || this == &Entry.second) && |
| 258 | "Mapping out of date!"); |
| 259 | |
| 260 | MDNode *OldScope = Entry.first.get(); |
| 261 | MDNode *OldInlinedAt = Entry.second.get(); |
| 262 | assert(OldScope != 0 && OldInlinedAt != 0 && |
| 263 | "Entry should be non-canonical if either val dropped to null"); |
| 264 | |
| 265 | // Otherwise, we do have an entry in it, nuke it and we're done. |
| 266 | assert(Ctx->ScopeInlinedAtIdx[std::make_pair(OldScope, OldInlinedAt)] == Idx&& |
| 267 | "Mapping out of date"); |
| 268 | Ctx->ScopeInlinedAtIdx.erase(std::make_pair(OldScope, OldInlinedAt)); |
| 269 | |
Chris Lattner | cf25a89 | 2010-04-01 05:12:07 +0000 | [diff] [blame] | 270 | // Reset this VH to null. Drop both 'Idx' values to null to indicate that |
| 271 | // we're in non-canonical form now. |
Chris Lattner | 8cb2aeb | 2010-04-01 00:37:44 +0000 | [diff] [blame] | 272 | setValPtr(0); |
Chris Lattner | cf25a89 | 2010-04-01 05:12:07 +0000 | [diff] [blame] | 273 | Entry.first.Idx = Entry.second.Idx = 0; |
Chris Lattner | 8cb2aeb | 2010-04-01 00:37:44 +0000 | [diff] [blame] | 274 | } |
| 275 | |
| 276 | void DebugRecVH::allUsesReplacedWith(Value *NewVa) { |
| 277 | // If being replaced with a non-mdnode value (e.g. undef) handle this as if |
| 278 | // the mdnode got deleted. |
| 279 | MDNode *NewVal = dyn_cast<MDNode>(NewVa); |
| 280 | if (NewVal == 0) return deleted(); |
| 281 | |
| 282 | // If this is a non-canonical reference, just change it, we know it already |
| 283 | // doesn't have a map entry. |
| 284 | if (Idx == 0) { |
| 285 | setValPtr(NewVa); |
| 286 | return; |
| 287 | } |
| 288 | |
| 289 | MDNode *OldVal = get(); |
| 290 | assert(OldVal != NewVa && "Node replaced with self?"); |
| 291 | |
| 292 | // If the index is positive, it is an entry in ScopeRecords. |
| 293 | if (Idx > 0) { |
| 294 | assert(Ctx->ScopeRecordIdx[OldVal] == Idx && "Mapping out of date!"); |
| 295 | Ctx->ScopeRecordIdx.erase(OldVal); |
| 296 | setValPtr(NewVal); |
| 297 | |
| 298 | int NewEntry = Ctx->getOrAddScopeRecordIdxEntry(NewVal, Idx); |
| 299 | |
| 300 | // If NewVal already has an entry, this becomes a non-canonical reference, |
| 301 | // just drop Idx to 0 to signify this. |
| 302 | if (NewEntry != Idx) |
| 303 | Idx = 0; |
| 304 | return; |
| 305 | } |
| 306 | |
| 307 | // Otherwise, it is an entry in ScopeInlinedAtRecords, we don't know if it |
| 308 | // is the scope or the inlined-at record entry. |
| 309 | assert(unsigned(-Idx-1) < Ctx->ScopeInlinedAtRecords.size()); |
| 310 | std::pair<DebugRecVH, DebugRecVH> &Entry = Ctx->ScopeInlinedAtRecords[-Idx-1]; |
| 311 | assert((this == &Entry.first || this == &Entry.second) && |
| 312 | "Mapping out of date!"); |
| 313 | |
| 314 | MDNode *OldScope = Entry.first.get(); |
| 315 | MDNode *OldInlinedAt = Entry.second.get(); |
| 316 | assert(OldScope != 0 && OldInlinedAt != 0 && |
| 317 | "Entry should be non-canonical if either val dropped to null"); |
| 318 | |
| 319 | // Otherwise, we do have an entry in it, nuke it and we're done. |
| 320 | assert(Ctx->ScopeInlinedAtIdx[std::make_pair(OldScope, OldInlinedAt)] == Idx&& |
| 321 | "Mapping out of date"); |
| 322 | Ctx->ScopeInlinedAtIdx.erase(std::make_pair(OldScope, OldInlinedAt)); |
| 323 | |
| 324 | // Reset this VH to the new value. |
| 325 | setValPtr(NewVal); |
| 326 | |
| 327 | int NewIdx = Ctx->getOrAddScopeInlinedAtIdxEntry(Entry.first.get(), |
| 328 | Entry.second.get(), Idx); |
| 329 | // If NewVal already has an entry, this becomes a non-canonical reference, |
| 330 | // just drop Idx to 0 to signify this. |
Chris Lattner | cf25a89 | 2010-04-01 05:12:07 +0000 | [diff] [blame] | 331 | if (NewIdx != Idx) { |
| 332 | std::pair<DebugRecVH, DebugRecVH> &Entry=Ctx->ScopeInlinedAtRecords[-Idx-1]; |
| 333 | Entry.first.Idx = Entry.second.Idx = 0; |
| 334 | } |
Chris Lattner | 8cb2aeb | 2010-04-01 00:37:44 +0000 | [diff] [blame] | 335 | } |