LLVM API Documentation

DebugLoc.cpp
Go to the documentation of this file.
00001 //===-- DebugLoc.cpp - Implement DebugLoc class ---------------------------===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 
00010 #include "llvm/IR/DebugLoc.h"
00011 #include "LLVMContextImpl.h"
00012 #include "llvm/ADT/DenseMapInfo.h"
00013 #include "llvm/IR/DebugInfo.h"
00014 using namespace llvm;
00015 
00016 //===----------------------------------------------------------------------===//
00017 // DebugLoc Implementation
00018 //===----------------------------------------------------------------------===//
00019 
00020 MDNode *DebugLoc::getScope(const LLVMContext &Ctx) const {
00021   if (ScopeIdx == 0) return nullptr;
00022   
00023   if (ScopeIdx > 0) {
00024     // Positive ScopeIdx is an index into ScopeRecords, which has no inlined-at
00025     // position specified.
00026     assert(unsigned(ScopeIdx) <= Ctx.pImpl->ScopeRecords.size() &&
00027            "Invalid ScopeIdx!");
00028     return Ctx.pImpl->ScopeRecords[ScopeIdx-1].get();
00029   }
00030   
00031   // Otherwise, the index is in the ScopeInlinedAtRecords array.
00032   assert(unsigned(-ScopeIdx) <= Ctx.pImpl->ScopeInlinedAtRecords.size() &&
00033          "Invalid ScopeIdx");
00034   return Ctx.pImpl->ScopeInlinedAtRecords[-ScopeIdx-1].first.get();
00035 }
00036 
00037 MDNode *DebugLoc::getInlinedAt(const LLVMContext &Ctx) const {
00038   // Positive ScopeIdx is an index into ScopeRecords, which has no inlined-at
00039   // position specified.  Zero is invalid.
00040   if (ScopeIdx >= 0) return nullptr;
00041   
00042   // Otherwise, the index is in the ScopeInlinedAtRecords array.
00043   assert(unsigned(-ScopeIdx) <= Ctx.pImpl->ScopeInlinedAtRecords.size() &&
00044          "Invalid ScopeIdx");
00045   return Ctx.pImpl->ScopeInlinedAtRecords[-ScopeIdx-1].second.get();
00046 }
00047 
00048 /// Return both the Scope and the InlinedAt values.
00049 void DebugLoc::getScopeAndInlinedAt(MDNode *&Scope, MDNode *&IA,
00050                                     const LLVMContext &Ctx) const {
00051   if (ScopeIdx == 0) {
00052     Scope = IA = nullptr;
00053     return;
00054   }
00055   
00056   if (ScopeIdx > 0) {
00057     // Positive ScopeIdx is an index into ScopeRecords, which has no inlined-at
00058     // position specified.
00059     assert(unsigned(ScopeIdx) <= Ctx.pImpl->ScopeRecords.size() &&
00060            "Invalid ScopeIdx!");
00061     Scope = Ctx.pImpl->ScopeRecords[ScopeIdx-1].get();
00062     IA = nullptr;
00063     return;
00064   }
00065   
00066   // Otherwise, the index is in the ScopeInlinedAtRecords array.
00067   assert(unsigned(-ScopeIdx) <= Ctx.pImpl->ScopeInlinedAtRecords.size() &&
00068          "Invalid ScopeIdx");
00069   Scope = Ctx.pImpl->ScopeInlinedAtRecords[-ScopeIdx-1].first.get();
00070   IA    = Ctx.pImpl->ScopeInlinedAtRecords[-ScopeIdx-1].second.get();
00071 }
00072 
00073 MDNode *DebugLoc::getScopeNode(const LLVMContext &Ctx) const {
00074   if (MDNode *InlinedAt = getInlinedAt(Ctx))
00075     return DebugLoc::getFromDILocation(InlinedAt).getScopeNode(Ctx);
00076   return getScope(Ctx);
00077 }
00078 
00079 DebugLoc DebugLoc::getFnDebugLoc(const LLVMContext &Ctx) {
00080   const MDNode *Scope = getScopeNode(Ctx);
00081   DISubprogram SP = getDISubprogram(Scope);
00082   if (SP.isSubprogram()) {
00083     // Check for number of operands since the compatibility is
00084     // cheap here.  FIXME: Name the magic constant.
00085     if (SP->getNumOperands() > 19)
00086       return DebugLoc::get(SP.getScopeLineNumber(), 0, SP);
00087     else
00088       return DebugLoc::get(SP.getLineNumber(), 0, SP);
00089   }
00090 
00091   return DebugLoc();
00092 }
00093 
00094 DebugLoc DebugLoc::get(unsigned Line, unsigned Col,
00095                        MDNode *Scope, MDNode *InlinedAt) {
00096   DebugLoc Result;
00097   
00098   // If no scope is available, this is an unknown location.
00099   if (!Scope) return Result;
00100 
00101   // Saturate line and col to "unknown".
00102   if (Col > 255) Col = 0;
00103   if (Line >= (1 << 24)) Line = 0;
00104   Result.LineCol = Line | (Col << 24);
00105   
00106   LLVMContext &Ctx = Scope->getContext();
00107   
00108   // If there is no inlined-at location, use the ScopeRecords array.
00109   if (!InlinedAt)
00110     Result.ScopeIdx = Ctx.pImpl->getOrAddScopeRecordIdxEntry(Scope, 0);
00111   else
00112     Result.ScopeIdx = Ctx.pImpl->getOrAddScopeInlinedAtIdxEntry(Scope,
00113                                                                 InlinedAt, 0);
00114 
00115   return Result;
00116 }
00117 
00118 /// getAsMDNode - This method converts the compressed DebugLoc node into a
00119 /// DILocation-compatible MDNode.
00120 MDNode *DebugLoc::getAsMDNode(const LLVMContext &Ctx) const {
00121   if (isUnknown()) return nullptr;
00122   
00123   MDNode *Scope, *IA;
00124   getScopeAndInlinedAt(Scope, IA, Ctx);
00125   assert(Scope && "If scope is null, this should be isUnknown()");
00126   
00127   LLVMContext &Ctx2 = Scope->getContext();
00128   Type *Int32 = Type::getInt32Ty(Ctx2);
00129   Value *Elts[] = {
00130     ConstantInt::get(Int32, getLine()), ConstantInt::get(Int32, getCol()),
00131     Scope, IA
00132   };
00133   return MDNode::get(Ctx2, Elts);
00134 }
00135 
00136 /// getFromDILocation - Translate the DILocation quad into a DebugLoc.
00137 DebugLoc DebugLoc::getFromDILocation(MDNode *N) {
00138   DILocation Loc(N);
00139   MDNode *Scope = Loc.getScope();
00140   if (!Scope) return DebugLoc();
00141   return get(Loc.getLineNumber(), Loc.getColumnNumber(), Scope,
00142              Loc.getOrigLocation());
00143 }
00144 
00145 /// getFromDILexicalBlock - Translate the DILexicalBlock into a DebugLoc.
00146 DebugLoc DebugLoc::getFromDILexicalBlock(MDNode *N) {
00147   DILexicalBlock LexBlock(N);
00148   MDNode *Scope = LexBlock.getContext();
00149   if (!Scope) return DebugLoc();
00150   return get(LexBlock.getLineNumber(), LexBlock.getColumnNumber(), Scope,
00151              nullptr);
00152 }
00153 
00154 void DebugLoc::dump(const LLVMContext &Ctx) const {
00155 #ifndef NDEBUG
00156   if (!isUnknown()) {
00157     dbgs() << getLine();
00158     if (getCol() != 0)
00159       dbgs() << ',' << getCol();
00160     DebugLoc InlinedAtDL = DebugLoc::getFromDILocation(getInlinedAt(Ctx));
00161     if (!InlinedAtDL.isUnknown()) {
00162       dbgs() << " @ ";
00163       InlinedAtDL.dump(Ctx);
00164     } else
00165       dbgs() << "\n";
00166   }
00167 #endif
00168 }
00169 
00170 //===----------------------------------------------------------------------===//
00171 // DenseMap specialization
00172 //===----------------------------------------------------------------------===//
00173 
00174 unsigned DenseMapInfo<DebugLoc>::getHashValue(const DebugLoc &Key) {
00175   return static_cast<unsigned>(hash_combine(Key.LineCol, Key.ScopeIdx));
00176 }
00177 
00178 //===----------------------------------------------------------------------===//
00179 // LLVMContextImpl Implementation
00180 //===----------------------------------------------------------------------===//
00181 
00182 int LLVMContextImpl::getOrAddScopeRecordIdxEntry(MDNode *Scope,
00183                                                  int ExistingIdx) {
00184   // If we already have an entry for this scope, return it.
00185   int &Idx = ScopeRecordIdx[Scope];
00186   if (Idx) return Idx;
00187   
00188   // If we don't have an entry, but ExistingIdx is specified, use it.
00189   if (ExistingIdx)
00190     return Idx = ExistingIdx;
00191   
00192   // Otherwise add a new entry.
00193   
00194   // Start out ScopeRecords with a minimal reasonable size to avoid
00195   // excessive reallocation starting out.
00196   if (ScopeRecords.empty())
00197     ScopeRecords.reserve(128);
00198   
00199   // Index is biased by 1 for index.
00200   Idx = ScopeRecords.size()+1;
00201   ScopeRecords.push_back(DebugRecVH(Scope, this, Idx));
00202   return Idx;
00203 }
00204 
00205 int LLVMContextImpl::getOrAddScopeInlinedAtIdxEntry(MDNode *Scope, MDNode *IA,
00206                                                     int ExistingIdx) {
00207   // If we already have an entry, return it.
00208   int &Idx = ScopeInlinedAtIdx[std::make_pair(Scope, IA)];
00209   if (Idx) return Idx;
00210   
00211   // If we don't have an entry, but ExistingIdx is specified, use it.
00212   if (ExistingIdx)
00213     return Idx = ExistingIdx;
00214   
00215   // Start out ScopeInlinedAtRecords with a minimal reasonable size to avoid
00216   // excessive reallocation starting out.
00217   if (ScopeInlinedAtRecords.empty())
00218     ScopeInlinedAtRecords.reserve(128);
00219     
00220   // Index is biased by 1 and negated.
00221   Idx = -ScopeInlinedAtRecords.size()-1;
00222   ScopeInlinedAtRecords.push_back(std::make_pair(DebugRecVH(Scope, this, Idx),
00223                                                  DebugRecVH(IA, this, Idx)));
00224   return Idx;
00225 }
00226 
00227 
00228 //===----------------------------------------------------------------------===//
00229 // DebugRecVH Implementation
00230 //===----------------------------------------------------------------------===//
00231 
00232 /// deleted - The MDNode this is pointing to got deleted, so this pointer needs
00233 /// to drop to null and we need remove our entry from the DenseMap.
00234 void DebugRecVH::deleted() {
00235   // If this is a non-canonical reference, just drop the value to null, we know
00236   // it doesn't have a map entry.
00237   if (Idx == 0) {
00238     setValPtr(nullptr);
00239     return;
00240   }
00241     
00242   MDNode *Cur = get();
00243   
00244   // If the index is positive, it is an entry in ScopeRecords.
00245   if (Idx > 0) {
00246     assert(Ctx->ScopeRecordIdx[Cur] == Idx && "Mapping out of date!");
00247     Ctx->ScopeRecordIdx.erase(Cur);
00248     // Reset this VH to null and we're done.
00249     setValPtr(nullptr);
00250     Idx = 0;
00251     return;
00252   }
00253   
00254   // Otherwise, it is an entry in ScopeInlinedAtRecords, we don't know if it
00255   // is the scope or the inlined-at record entry.
00256   assert(unsigned(-Idx-1) < Ctx->ScopeInlinedAtRecords.size());
00257   std::pair<DebugRecVH, DebugRecVH> &Entry = Ctx->ScopeInlinedAtRecords[-Idx-1];
00258   assert((this == &Entry.first || this == &Entry.second) &&
00259          "Mapping out of date!");
00260   
00261   MDNode *OldScope = Entry.first.get();
00262   MDNode *OldInlinedAt = Entry.second.get();
00263   assert(OldScope && OldInlinedAt &&
00264          "Entry should be non-canonical if either val dropped to null");
00265 
00266   // Otherwise, we do have an entry in it, nuke it and we're done.
00267   assert(Ctx->ScopeInlinedAtIdx[std::make_pair(OldScope, OldInlinedAt)] == Idx&&
00268          "Mapping out of date");
00269   Ctx->ScopeInlinedAtIdx.erase(std::make_pair(OldScope, OldInlinedAt));
00270   
00271   // Reset this VH to null.  Drop both 'Idx' values to null to indicate that
00272   // we're in non-canonical form now.
00273   setValPtr(nullptr);
00274   Entry.first.Idx = Entry.second.Idx = 0;
00275 }
00276 
00277 void DebugRecVH::allUsesReplacedWith(Value *NewVa) {
00278   // If being replaced with a non-mdnode value (e.g. undef) handle this as if
00279   // the mdnode got deleted.
00280   MDNode *NewVal = dyn_cast<MDNode>(NewVa);
00281   if (!NewVal) return deleted();
00282 
00283   // If this is a non-canonical reference, just change it, we know it already
00284   // doesn't have a map entry.
00285   if (Idx == 0) {
00286     setValPtr(NewVa);
00287     return;
00288   }
00289   
00290   MDNode *OldVal = get();
00291   assert(OldVal != NewVa && "Node replaced with self?");
00292   
00293   // If the index is positive, it is an entry in ScopeRecords.
00294   if (Idx > 0) {
00295     assert(Ctx->ScopeRecordIdx[OldVal] == Idx && "Mapping out of date!");
00296     Ctx->ScopeRecordIdx.erase(OldVal);
00297     setValPtr(NewVal);
00298 
00299     int NewEntry = Ctx->getOrAddScopeRecordIdxEntry(NewVal, Idx);
00300     
00301     // If NewVal already has an entry, this becomes a non-canonical reference,
00302     // just drop Idx to 0 to signify this.
00303     if (NewEntry != Idx)
00304       Idx = 0;
00305     return;
00306   }
00307   
00308   // Otherwise, it is an entry in ScopeInlinedAtRecords, we don't know if it
00309   // is the scope or the inlined-at record entry.
00310   assert(unsigned(-Idx-1) < Ctx->ScopeInlinedAtRecords.size());
00311   std::pair<DebugRecVH, DebugRecVH> &Entry = Ctx->ScopeInlinedAtRecords[-Idx-1];
00312   assert((this == &Entry.first || this == &Entry.second) &&
00313          "Mapping out of date!");
00314   
00315   MDNode *OldScope = Entry.first.get();
00316   MDNode *OldInlinedAt = Entry.second.get();
00317   assert(OldScope && OldInlinedAt &&
00318          "Entry should be non-canonical if either val dropped to null");
00319   
00320   // Otherwise, we do have an entry in it, nuke it and we're done.
00321   assert(Ctx->ScopeInlinedAtIdx[std::make_pair(OldScope, OldInlinedAt)] == Idx&&
00322          "Mapping out of date");
00323   Ctx->ScopeInlinedAtIdx.erase(std::make_pair(OldScope, OldInlinedAt));
00324   
00325   // Reset this VH to the new value.
00326   setValPtr(NewVal);
00327 
00328   int NewIdx = Ctx->getOrAddScopeInlinedAtIdxEntry(Entry.first.get(),
00329                                                    Entry.second.get(), Idx);
00330   // If NewVal already has an entry, this becomes a non-canonical reference,
00331   // just drop Idx to 0 to signify this.
00332   if (NewIdx != Idx) {
00333     std::pair<DebugRecVH, DebugRecVH> &Entry=Ctx->ScopeInlinedAtRecords[-Idx-1];
00334     Entry.first.Idx = Entry.second.Idx = 0;
00335   }
00336 }