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