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     return DebugLoc::get(SP.getScopeLineNumber(), 0, SP);
00084 
00085   return DebugLoc();
00086 }
00087 
00088 DebugLoc DebugLoc::get(unsigned Line, unsigned Col,
00089                        MDNode *Scope, MDNode *InlinedAt) {
00090   DebugLoc Result;
00091   
00092   // If no scope is available, this is an unknown location.
00093   if (!Scope) return Result;
00094 
00095   // Saturate line and col to "unknown".
00096   if (Col > 255) Col = 0;
00097   if (Line >= (1 << 24)) Line = 0;
00098   Result.LineCol = Line | (Col << 24);
00099   
00100   LLVMContext &Ctx = Scope->getContext();
00101   
00102   // If there is no inlined-at location, use the ScopeRecords array.
00103   if (!InlinedAt)
00104     Result.ScopeIdx = Ctx.pImpl->getOrAddScopeRecordIdxEntry(Scope, 0);
00105   else
00106     Result.ScopeIdx = Ctx.pImpl->getOrAddScopeInlinedAtIdxEntry(Scope,
00107                                                                 InlinedAt, 0);
00108 
00109   return Result;
00110 }
00111 
00112 /// getAsMDNode - This method converts the compressed DebugLoc node into a
00113 /// DILocation-compatible MDNode.
00114 MDNode *DebugLoc::getAsMDNode(const LLVMContext &Ctx) const {
00115   if (isUnknown()) return nullptr;
00116   
00117   MDNode *Scope, *IA;
00118   getScopeAndInlinedAt(Scope, IA, Ctx);
00119   assert(Scope && "If scope is null, this should be isUnknown()");
00120   
00121   LLVMContext &Ctx2 = Scope->getContext();
00122   Type *Int32 = Type::getInt32Ty(Ctx2);
00123   Value *Elts[] = {
00124     ConstantInt::get(Int32, getLine()), ConstantInt::get(Int32, getCol()),
00125     Scope, IA
00126   };
00127   return MDNode::get(Ctx2, Elts);
00128 }
00129 
00130 /// getFromDILocation - Translate the DILocation quad into a DebugLoc.
00131 DebugLoc DebugLoc::getFromDILocation(MDNode *N) {
00132   DILocation Loc(N);
00133   MDNode *Scope = Loc.getScope();
00134   if (!Scope) return DebugLoc();
00135   return get(Loc.getLineNumber(), Loc.getColumnNumber(), Scope,
00136              Loc.getOrigLocation());
00137 }
00138 
00139 /// getFromDILexicalBlock - Translate the DILexicalBlock into a DebugLoc.
00140 DebugLoc DebugLoc::getFromDILexicalBlock(MDNode *N) {
00141   DILexicalBlock LexBlock(N);
00142   MDNode *Scope = LexBlock.getContext();
00143   if (!Scope) return DebugLoc();
00144   return get(LexBlock.getLineNumber(), LexBlock.getColumnNumber(), Scope,
00145              nullptr);
00146 }
00147 
00148 void DebugLoc::dump(const LLVMContext &Ctx) const {
00149 #ifndef NDEBUG
00150   if (!isUnknown()) {
00151     dbgs() << getLine();
00152     if (getCol() != 0)
00153       dbgs() << ',' << getCol();
00154     DebugLoc InlinedAtDL = DebugLoc::getFromDILocation(getInlinedAt(Ctx));
00155     if (!InlinedAtDL.isUnknown()) {
00156       dbgs() << " @ ";
00157       InlinedAtDL.dump(Ctx);
00158     } else
00159       dbgs() << "\n";
00160   }
00161 #endif
00162 }
00163 
00164 void DebugLoc::print(const LLVMContext &Ctx, raw_ostream &OS) const {
00165   if (!isUnknown()) {
00166     // Print source line info.
00167     DIScope Scope(getScope(Ctx));
00168     assert((!Scope || Scope.isScope()) &&
00169            "Scope of a DebugLoc should be null or a DIScope.");
00170     if (Scope)
00171       OS << Scope.getFilename();
00172     else
00173       OS << "<unknown>";
00174     OS << ':' << getLine();
00175     if (getCol() != 0)
00176       OS << ':' << getCol();
00177     DebugLoc InlinedAtDL = DebugLoc::getFromDILocation(getInlinedAt(Ctx));
00178     if (!InlinedAtDL.isUnknown()) {
00179       OS << " @[ ";
00180       InlinedAtDL.print(Ctx, OS);
00181       OS << " ]";
00182     }
00183   }
00184 }
00185 
00186 //===----------------------------------------------------------------------===//
00187 // DenseMap specialization
00188 //===----------------------------------------------------------------------===//
00189 
00190 unsigned DenseMapInfo<DebugLoc>::getHashValue(const DebugLoc &Key) {
00191   return static_cast<unsigned>(hash_combine(Key.LineCol, Key.ScopeIdx));
00192 }
00193 
00194 //===----------------------------------------------------------------------===//
00195 // LLVMContextImpl Implementation
00196 //===----------------------------------------------------------------------===//
00197 
00198 int LLVMContextImpl::getOrAddScopeRecordIdxEntry(MDNode *Scope,
00199                                                  int ExistingIdx) {
00200   // If we already have an entry for this scope, return it.
00201   int &Idx = ScopeRecordIdx[Scope];
00202   if (Idx) return Idx;
00203   
00204   // If we don't have an entry, but ExistingIdx is specified, use it.
00205   if (ExistingIdx)
00206     return Idx = ExistingIdx;
00207   
00208   // Otherwise add a new entry.
00209   
00210   // Start out ScopeRecords with a minimal reasonable size to avoid
00211   // excessive reallocation starting out.
00212   if (ScopeRecords.empty())
00213     ScopeRecords.reserve(128);
00214   
00215   // Index is biased by 1 for index.
00216   Idx = ScopeRecords.size()+1;
00217   ScopeRecords.push_back(DebugRecVH(Scope, this, Idx));
00218   return Idx;
00219 }
00220 
00221 int LLVMContextImpl::getOrAddScopeInlinedAtIdxEntry(MDNode *Scope, MDNode *IA,
00222                                                     int ExistingIdx) {
00223   // If we already have an entry, return it.
00224   int &Idx = ScopeInlinedAtIdx[std::make_pair(Scope, IA)];
00225   if (Idx) return Idx;
00226   
00227   // If we don't have an entry, but ExistingIdx is specified, use it.
00228   if (ExistingIdx)
00229     return Idx = ExistingIdx;
00230   
00231   // Start out ScopeInlinedAtRecords with a minimal reasonable size to avoid
00232   // excessive reallocation starting out.
00233   if (ScopeInlinedAtRecords.empty())
00234     ScopeInlinedAtRecords.reserve(128);
00235     
00236   // Index is biased by 1 and negated.
00237   Idx = -ScopeInlinedAtRecords.size()-1;
00238   ScopeInlinedAtRecords.push_back(std::make_pair(DebugRecVH(Scope, this, Idx),
00239                                                  DebugRecVH(IA, this, Idx)));
00240   return Idx;
00241 }
00242 
00243 
00244 //===----------------------------------------------------------------------===//
00245 // DebugRecVH Implementation
00246 //===----------------------------------------------------------------------===//
00247 
00248 /// deleted - The MDNode this is pointing to got deleted, so this pointer needs
00249 /// to drop to null and we need remove our entry from the DenseMap.
00250 void DebugRecVH::deleted() {
00251   // If this is a non-canonical reference, just drop the value to null, we know
00252   // it doesn't have a map entry.
00253   if (Idx == 0) {
00254     setValPtr(nullptr);
00255     return;
00256   }
00257     
00258   MDNode *Cur = get();
00259   
00260   // If the index is positive, it is an entry in ScopeRecords.
00261   if (Idx > 0) {
00262     assert(Ctx->ScopeRecordIdx[Cur] == Idx && "Mapping out of date!");
00263     Ctx->ScopeRecordIdx.erase(Cur);
00264     // Reset this VH to null and we're done.
00265     setValPtr(nullptr);
00266     Idx = 0;
00267     return;
00268   }
00269   
00270   // Otherwise, it is an entry in ScopeInlinedAtRecords, we don't know if it
00271   // is the scope or the inlined-at record entry.
00272   assert(unsigned(-Idx-1) < Ctx->ScopeInlinedAtRecords.size());
00273   std::pair<DebugRecVH, DebugRecVH> &Entry = Ctx->ScopeInlinedAtRecords[-Idx-1];
00274   assert((this == &Entry.first || this == &Entry.second) &&
00275          "Mapping out of date!");
00276   
00277   MDNode *OldScope = Entry.first.get();
00278   MDNode *OldInlinedAt = Entry.second.get();
00279   assert(OldScope && OldInlinedAt &&
00280          "Entry should be non-canonical if either val dropped to null");
00281 
00282   // Otherwise, we do have an entry in it, nuke it and we're done.
00283   assert(Ctx->ScopeInlinedAtIdx[std::make_pair(OldScope, OldInlinedAt)] == Idx&&
00284          "Mapping out of date");
00285   Ctx->ScopeInlinedAtIdx.erase(std::make_pair(OldScope, OldInlinedAt));
00286   
00287   // Reset this VH to null.  Drop both 'Idx' values to null to indicate that
00288   // we're in non-canonical form now.
00289   setValPtr(nullptr);
00290   Entry.first.Idx = Entry.second.Idx = 0;
00291 }
00292 
00293 void DebugRecVH::allUsesReplacedWith(Value *NewVa) {
00294   // If being replaced with a non-mdnode value (e.g. undef) handle this as if
00295   // the mdnode got deleted.
00296   MDNode *NewVal = dyn_cast<MDNode>(NewVa);
00297   if (!NewVal) return deleted();
00298 
00299   // If this is a non-canonical reference, just change it, we know it already
00300   // doesn't have a map entry.
00301   if (Idx == 0) {
00302     setValPtr(NewVa);
00303     return;
00304   }
00305   
00306   MDNode *OldVal = get();
00307   assert(OldVal != NewVa && "Node replaced with self?");
00308   
00309   // If the index is positive, it is an entry in ScopeRecords.
00310   if (Idx > 0) {
00311     assert(Ctx->ScopeRecordIdx[OldVal] == Idx && "Mapping out of date!");
00312     Ctx->ScopeRecordIdx.erase(OldVal);
00313     setValPtr(NewVal);
00314 
00315     int NewEntry = Ctx->getOrAddScopeRecordIdxEntry(NewVal, Idx);
00316     
00317     // If NewVal already has an entry, this becomes a non-canonical reference,
00318     // just drop Idx to 0 to signify this.
00319     if (NewEntry != Idx)
00320       Idx = 0;
00321     return;
00322   }
00323   
00324   // Otherwise, it is an entry in ScopeInlinedAtRecords, we don't know if it
00325   // is the scope or the inlined-at record entry.
00326   assert(unsigned(-Idx-1) < Ctx->ScopeInlinedAtRecords.size());
00327   std::pair<DebugRecVH, DebugRecVH> &Entry = Ctx->ScopeInlinedAtRecords[-Idx-1];
00328   assert((this == &Entry.first || this == &Entry.second) &&
00329          "Mapping out of date!");
00330   
00331   MDNode *OldScope = Entry.first.get();
00332   MDNode *OldInlinedAt = Entry.second.get();
00333   assert(OldScope && OldInlinedAt &&
00334          "Entry should be non-canonical if either val dropped to null");
00335   
00336   // Otherwise, we do have an entry in it, nuke it and we're done.
00337   assert(Ctx->ScopeInlinedAtIdx[std::make_pair(OldScope, OldInlinedAt)] == Idx&&
00338          "Mapping out of date");
00339   Ctx->ScopeInlinedAtIdx.erase(std::make_pair(OldScope, OldInlinedAt));
00340   
00341   // Reset this VH to the new value.
00342   setValPtr(NewVal);
00343 
00344   int NewIdx = Ctx->getOrAddScopeInlinedAtIdxEntry(Entry.first.get(),
00345                                                    Entry.second.get(), Idx);
00346   // If NewVal already has an entry, this becomes a non-canonical reference,
00347   // just drop Idx to 0 to signify this.
00348   if (NewIdx != Idx) {
00349     std::pair<DebugRecVH, DebugRecVH> &Entry=Ctx->ScopeInlinedAtRecords[-Idx-1];
00350     Entry.first.Idx = Entry.second.Idx = 0;
00351   }
00352 }