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) const {
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 void DebugLoc::print(const LLVMContext &Ctx, raw_ostream &OS) const {
00171   if (!isUnknown()) {
00172     // Print source line info.
00173     DIScope Scope(getScope(Ctx));
00174     assert((!Scope || Scope.isScope()) &&
00175            "Scope of a DebugLoc should be null or a DIScope.");
00176     if (Scope)
00177       OS << Scope.getFilename();
00178     else
00179       OS << "<unknown>";
00180     OS << ':' << getLine();
00181     if (getCol() != 0)
00182       OS << ':' << getCol();
00183     DebugLoc InlinedAtDL = DebugLoc::getFromDILocation(getInlinedAt(Ctx));
00184     if (!InlinedAtDL.isUnknown()) {
00185       OS << " @[ ";
00186       InlinedAtDL.print(Ctx, OS);
00187       OS << " ]";
00188     }
00189   }
00190 }
00191 
00192 //===----------------------------------------------------------------------===//
00193 // DenseMap specialization
00194 //===----------------------------------------------------------------------===//
00195 
00196 unsigned DenseMapInfo<DebugLoc>::getHashValue(const DebugLoc &Key) {
00197   return static_cast<unsigned>(hash_combine(Key.LineCol, Key.ScopeIdx));
00198 }
00199 
00200 //===----------------------------------------------------------------------===//
00201 // LLVMContextImpl Implementation
00202 //===----------------------------------------------------------------------===//
00203 
00204 int LLVMContextImpl::getOrAddScopeRecordIdxEntry(MDNode *Scope,
00205                                                  int ExistingIdx) {
00206   // If we already have an entry for this scope, return it.
00207   int &Idx = ScopeRecordIdx[Scope];
00208   if (Idx) return Idx;
00209   
00210   // If we don't have an entry, but ExistingIdx is specified, use it.
00211   if (ExistingIdx)
00212     return Idx = ExistingIdx;
00213   
00214   // Otherwise add a new entry.
00215   
00216   // Start out ScopeRecords with a minimal reasonable size to avoid
00217   // excessive reallocation starting out.
00218   if (ScopeRecords.empty())
00219     ScopeRecords.reserve(128);
00220   
00221   // Index is biased by 1 for index.
00222   Idx = ScopeRecords.size()+1;
00223   ScopeRecords.push_back(DebugRecVH(Scope, this, Idx));
00224   return Idx;
00225 }
00226 
00227 int LLVMContextImpl::getOrAddScopeInlinedAtIdxEntry(MDNode *Scope, MDNode *IA,
00228                                                     int ExistingIdx) {
00229   // If we already have an entry, return it.
00230   int &Idx = ScopeInlinedAtIdx[std::make_pair(Scope, IA)];
00231   if (Idx) return Idx;
00232   
00233   // If we don't have an entry, but ExistingIdx is specified, use it.
00234   if (ExistingIdx)
00235     return Idx = ExistingIdx;
00236   
00237   // Start out ScopeInlinedAtRecords with a minimal reasonable size to avoid
00238   // excessive reallocation starting out.
00239   if (ScopeInlinedAtRecords.empty())
00240     ScopeInlinedAtRecords.reserve(128);
00241     
00242   // Index is biased by 1 and negated.
00243   Idx = -ScopeInlinedAtRecords.size()-1;
00244   ScopeInlinedAtRecords.push_back(std::make_pair(DebugRecVH(Scope, this, Idx),
00245                                                  DebugRecVH(IA, this, Idx)));
00246   return Idx;
00247 }
00248 
00249 
00250 //===----------------------------------------------------------------------===//
00251 // DebugRecVH Implementation
00252 //===----------------------------------------------------------------------===//
00253 
00254 /// deleted - The MDNode this is pointing to got deleted, so this pointer needs
00255 /// to drop to null and we need remove our entry from the DenseMap.
00256 void DebugRecVH::deleted() {
00257   // If this is a non-canonical reference, just drop the value to null, we know
00258   // it doesn't have a map entry.
00259   if (Idx == 0) {
00260     setValPtr(nullptr);
00261     return;
00262   }
00263     
00264   MDNode *Cur = get();
00265   
00266   // If the index is positive, it is an entry in ScopeRecords.
00267   if (Idx > 0) {
00268     assert(Ctx->ScopeRecordIdx[Cur] == Idx && "Mapping out of date!");
00269     Ctx->ScopeRecordIdx.erase(Cur);
00270     // Reset this VH to null and we're done.
00271     setValPtr(nullptr);
00272     Idx = 0;
00273     return;
00274   }
00275   
00276   // Otherwise, it is an entry in ScopeInlinedAtRecords, we don't know if it
00277   // is the scope or the inlined-at record entry.
00278   assert(unsigned(-Idx-1) < Ctx->ScopeInlinedAtRecords.size());
00279   std::pair<DebugRecVH, DebugRecVH> &Entry = Ctx->ScopeInlinedAtRecords[-Idx-1];
00280   assert((this == &Entry.first || this == &Entry.second) &&
00281          "Mapping out of date!");
00282   
00283   MDNode *OldScope = Entry.first.get();
00284   MDNode *OldInlinedAt = Entry.second.get();
00285   assert(OldScope && OldInlinedAt &&
00286          "Entry should be non-canonical if either val dropped to null");
00287 
00288   // Otherwise, we do have an entry in it, nuke it and we're done.
00289   assert(Ctx->ScopeInlinedAtIdx[std::make_pair(OldScope, OldInlinedAt)] == Idx&&
00290          "Mapping out of date");
00291   Ctx->ScopeInlinedAtIdx.erase(std::make_pair(OldScope, OldInlinedAt));
00292   
00293   // Reset this VH to null.  Drop both 'Idx' values to null to indicate that
00294   // we're in non-canonical form now.
00295   setValPtr(nullptr);
00296   Entry.first.Idx = Entry.second.Idx = 0;
00297 }
00298 
00299 void DebugRecVH::allUsesReplacedWith(Value *NewVa) {
00300   // If being replaced with a non-mdnode value (e.g. undef) handle this as if
00301   // the mdnode got deleted.
00302   MDNode *NewVal = dyn_cast<MDNode>(NewVa);
00303   if (!NewVal) return deleted();
00304 
00305   // If this is a non-canonical reference, just change it, we know it already
00306   // doesn't have a map entry.
00307   if (Idx == 0) {
00308     setValPtr(NewVa);
00309     return;
00310   }
00311   
00312   MDNode *OldVal = get();
00313   assert(OldVal != NewVa && "Node replaced with self?");
00314   
00315   // If the index is positive, it is an entry in ScopeRecords.
00316   if (Idx > 0) {
00317     assert(Ctx->ScopeRecordIdx[OldVal] == Idx && "Mapping out of date!");
00318     Ctx->ScopeRecordIdx.erase(OldVal);
00319     setValPtr(NewVal);
00320 
00321     int NewEntry = Ctx->getOrAddScopeRecordIdxEntry(NewVal, Idx);
00322     
00323     // If NewVal already has an entry, this becomes a non-canonical reference,
00324     // just drop Idx to 0 to signify this.
00325     if (NewEntry != Idx)
00326       Idx = 0;
00327     return;
00328   }
00329   
00330   // Otherwise, it is an entry in ScopeInlinedAtRecords, we don't know if it
00331   // is the scope or the inlined-at record entry.
00332   assert(unsigned(-Idx-1) < Ctx->ScopeInlinedAtRecords.size());
00333   std::pair<DebugRecVH, DebugRecVH> &Entry = Ctx->ScopeInlinedAtRecords[-Idx-1];
00334   assert((this == &Entry.first || this == &Entry.second) &&
00335          "Mapping out of date!");
00336   
00337   MDNode *OldScope = Entry.first.get();
00338   MDNode *OldInlinedAt = Entry.second.get();
00339   assert(OldScope && OldInlinedAt &&
00340          "Entry should be non-canonical if either val dropped to null");
00341   
00342   // Otherwise, we do have an entry in it, nuke it and we're done.
00343   assert(Ctx->ScopeInlinedAtIdx[std::make_pair(OldScope, OldInlinedAt)] == Idx&&
00344          "Mapping out of date");
00345   Ctx->ScopeInlinedAtIdx.erase(std::make_pair(OldScope, OldInlinedAt));
00346   
00347   // Reset this VH to the new value.
00348   setValPtr(NewVal);
00349 
00350   int NewIdx = Ctx->getOrAddScopeInlinedAtIdxEntry(Entry.first.get(),
00351                                                    Entry.second.get(), Idx);
00352   // If NewVal already has an entry, this becomes a non-canonical reference,
00353   // just drop Idx to 0 to signify this.
00354   if (NewIdx != Idx) {
00355     std::pair<DebugRecVH, DebugRecVH> &Entry=Ctx->ScopeInlinedAtRecords[-Idx-1];
00356     Entry.first.Idx = Entry.second.Idx = 0;
00357   }
00358 }