#include "llvm/IR/DebugLoc.h"
#include "LLVMContextImpl.h"
#include "llvm/ADT/DenseMapInfo.h"
#include "llvm/IR/DebugInfo.h"
using namespace llvm;
MDNode *DebugLoc::getScope(const LLVMContext &Ctx) const {
if (ScopeIdx == 0) return nullptr;
if (ScopeIdx > 0) {
assert(unsigned(ScopeIdx) <= Ctx.pImpl->ScopeRecords.size() &&
"Invalid ScopeIdx!");
return Ctx.pImpl->ScopeRecords[ScopeIdx-1].get();
}
assert(unsigned(-ScopeIdx) <= Ctx.pImpl->ScopeInlinedAtRecords.size() &&
"Invalid ScopeIdx");
return Ctx.pImpl->ScopeInlinedAtRecords[-ScopeIdx-1].first.get();
}
MDNode *DebugLoc::getInlinedAt(const LLVMContext &Ctx) const {
if (ScopeIdx >= 0) return nullptr;
assert(unsigned(-ScopeIdx) <= Ctx.pImpl->ScopeInlinedAtRecords.size() &&
"Invalid ScopeIdx");
return Ctx.pImpl->ScopeInlinedAtRecords[-ScopeIdx-1].second.get();
}
void DebugLoc::getScopeAndInlinedAt(MDNode *&Scope, MDNode *&IA,
const LLVMContext &Ctx) const {
if (ScopeIdx == 0) {
Scope = IA = nullptr;
return;
}
if (ScopeIdx > 0) {
assert(unsigned(ScopeIdx) <= Ctx.pImpl->ScopeRecords.size() &&
"Invalid ScopeIdx!");
Scope = Ctx.pImpl->ScopeRecords[ScopeIdx-1].get();
IA = nullptr;
return;
}
assert(unsigned(-ScopeIdx) <= Ctx.pImpl->ScopeInlinedAtRecords.size() &&
"Invalid ScopeIdx");
Scope = Ctx.pImpl->ScopeInlinedAtRecords[-ScopeIdx-1].first.get();
IA = Ctx.pImpl->ScopeInlinedAtRecords[-ScopeIdx-1].second.get();
}
MDNode *DebugLoc::getScopeNode(const LLVMContext &Ctx) const {
if (MDNode *InlinedAt = getInlinedAt(Ctx))
return DebugLoc::getFromDILocation(InlinedAt).getScopeNode(Ctx);
return getScope(Ctx);
}
DebugLoc DebugLoc::getFnDebugLoc(const LLVMContext &Ctx) const {
const MDNode *Scope = getScopeNode(Ctx);
DISubprogram SP = getDISubprogram(Scope);
if (SP.isSubprogram()) {
if (SP->getNumOperands() > 19)
return DebugLoc::get(SP.getScopeLineNumber(), 0, SP);
else
return DebugLoc::get(SP.getLineNumber(), 0, SP);
}
return DebugLoc();
}
DebugLoc DebugLoc::get(unsigned Line, unsigned Col,
MDNode *Scope, MDNode *InlinedAt) {
DebugLoc Result;
if (!Scope) return Result;
if (Col > 255) Col = 0;
if (Line >= (1 << 24)) Line = 0;
Result.LineCol = Line | (Col << 24);
LLVMContext &Ctx = Scope->getContext();
if (!InlinedAt)
Result.ScopeIdx = Ctx.pImpl->getOrAddScopeRecordIdxEntry(Scope, 0);
else
Result.ScopeIdx = Ctx.pImpl->getOrAddScopeInlinedAtIdxEntry(Scope,
InlinedAt, 0);
return Result;
}
MDNode *DebugLoc::getAsMDNode(const LLVMContext &Ctx) const {
if (isUnknown()) return nullptr;
MDNode *Scope, *IA;
getScopeAndInlinedAt(Scope, IA, Ctx);
assert(Scope && "If scope is null, this should be isUnknown()");
LLVMContext &Ctx2 = Scope->getContext();
Type *Int32 = Type::getInt32Ty(Ctx2);
Value *Elts[] = {
ConstantInt::get(Int32, getLine()), ConstantInt::get(Int32, getCol()),
Scope, IA
};
return MDNode::get(Ctx2, Elts);
}
DebugLoc DebugLoc::getFromDILocation(MDNode *N) {
DILocation Loc(N);
MDNode *Scope = Loc.getScope();
if (!Scope) return DebugLoc();
return get(Loc.getLineNumber(), Loc.getColumnNumber(), Scope,
Loc.getOrigLocation());
}
DebugLoc DebugLoc::getFromDILexicalBlock(MDNode *N) {
DILexicalBlock LexBlock(N);
MDNode *Scope = LexBlock.getContext();
if (!Scope) return DebugLoc();
return get(LexBlock.getLineNumber(), LexBlock.getColumnNumber(), Scope,
nullptr);
}
void DebugLoc::dump(const LLVMContext &Ctx) const {
#ifndef NDEBUG
if (!isUnknown()) {
dbgs() << getLine();
if (getCol() != 0)
dbgs() << ',' << getCol();
DebugLoc InlinedAtDL = DebugLoc::getFromDILocation(getInlinedAt(Ctx));
if (!InlinedAtDL.isUnknown()) {
dbgs() << " @ ";
InlinedAtDL.dump(Ctx);
} else
dbgs() << "\n";
}
#endif
}
void DebugLoc::print(const LLVMContext &Ctx, raw_ostream &OS) const {
if (!isUnknown()) {
DIScope Scope(getScope(Ctx));
assert((!Scope || Scope.isScope()) &&
"Scope of a DebugLoc should be null or a DIScope.");
if (Scope)
OS << Scope.getFilename();
else
OS << "<unknown>";
OS << ':' << getLine();
if (getCol() != 0)
OS << ':' << getCol();
DebugLoc InlinedAtDL = DebugLoc::getFromDILocation(getInlinedAt(Ctx));
if (!InlinedAtDL.isUnknown()) {
OS << " @[ ";
InlinedAtDL.print(Ctx, OS);
OS << " ]";
}
}
}
unsigned DenseMapInfo<DebugLoc>::getHashValue(const DebugLoc &Key) {
return static_cast<unsigned>(hash_combine(Key.LineCol, Key.ScopeIdx));
}
int LLVMContextImpl::getOrAddScopeRecordIdxEntry(MDNode *Scope,
int ExistingIdx) {
int &Idx = ScopeRecordIdx[Scope];
if (Idx) return Idx;
if (ExistingIdx)
return Idx = ExistingIdx;
if (ScopeRecords.empty())
ScopeRecords.reserve(128);
Idx = ScopeRecords.size()+1;
ScopeRecords.push_back(DebugRecVH(Scope, this, Idx));
return Idx;
}
int LLVMContextImpl::getOrAddScopeInlinedAtIdxEntry(MDNode *Scope, MDNode *IA,
int ExistingIdx) {
int &Idx = ScopeInlinedAtIdx[std::make_pair(Scope, IA)];
if (Idx) return Idx;
if (ExistingIdx)
return Idx = ExistingIdx;
if (ScopeInlinedAtRecords.empty())
ScopeInlinedAtRecords.reserve(128);
Idx = -ScopeInlinedAtRecords.size()-1;
ScopeInlinedAtRecords.push_back(std::make_pair(DebugRecVH(Scope, this, Idx),
DebugRecVH(IA, this, Idx)));
return Idx;
}
void DebugRecVH::deleted() {
if (Idx == 0) {
setValPtr(nullptr);
return;
}
MDNode *Cur = get();
if (Idx > 0) {
assert(Ctx->ScopeRecordIdx[Cur] == Idx && "Mapping out of date!");
Ctx->ScopeRecordIdx.erase(Cur);
setValPtr(nullptr);
Idx = 0;
return;
}
assert(unsigned(-Idx-1) < Ctx->ScopeInlinedAtRecords.size());
std::pair<DebugRecVH, DebugRecVH> &Entry = Ctx->ScopeInlinedAtRecords[-Idx-1];
assert((this == &Entry.first || this == &Entry.second) &&
"Mapping out of date!");
MDNode *OldScope = Entry.first.get();
MDNode *OldInlinedAt = Entry.second.get();
assert(OldScope && OldInlinedAt &&
"Entry should be non-canonical if either val dropped to null");
assert(Ctx->ScopeInlinedAtIdx[std::make_pair(OldScope, OldInlinedAt)] == Idx&&
"Mapping out of date");
Ctx->ScopeInlinedAtIdx.erase(std::make_pair(OldScope, OldInlinedAt));
setValPtr(nullptr);
Entry.first.Idx = Entry.second.Idx = 0;
}
void DebugRecVH::allUsesReplacedWith(Value *NewVa) {
MDNode *NewVal = dyn_cast<MDNode>(NewVa);
if (!NewVal) return deleted();
if (Idx == 0) {
setValPtr(NewVa);
return;
}
MDNode *OldVal = get();
assert(OldVal != NewVa && "Node replaced with self?");
if (Idx > 0) {
assert(Ctx->ScopeRecordIdx[OldVal] == Idx && "Mapping out of date!");
Ctx->ScopeRecordIdx.erase(OldVal);
setValPtr(NewVal);
int NewEntry = Ctx->getOrAddScopeRecordIdxEntry(NewVal, Idx);
if (NewEntry != Idx)
Idx = 0;
return;
}
assert(unsigned(-Idx-1) < Ctx->ScopeInlinedAtRecords.size());
std::pair<DebugRecVH, DebugRecVH> &Entry = Ctx->ScopeInlinedAtRecords[-Idx-1];
assert((this == &Entry.first || this == &Entry.second) &&
"Mapping out of date!");
MDNode *OldScope = Entry.first.get();
MDNode *OldInlinedAt = Entry.second.get();
assert(OldScope && OldInlinedAt &&
"Entry should be non-canonical if either val dropped to null");
assert(Ctx->ScopeInlinedAtIdx[std::make_pair(OldScope, OldInlinedAt)] == Idx&&
"Mapping out of date");
Ctx->ScopeInlinedAtIdx.erase(std::make_pair(OldScope, OldInlinedAt));
setValPtr(NewVal);
int NewIdx = Ctx->getOrAddScopeInlinedAtIdxEntry(Entry.first.get(),
Entry.second.get(), Idx);
if (NewIdx != Idx) {
std::pair<DebugRecVH, DebugRecVH> &Entry=Ctx->ScopeInlinedAtRecords[-Idx-1];
Entry.first.Idx = Entry.second.Idx = 0;
}
}