IR: Use the new DebugLoc API, NFC

Update lib/IR and lib/Bitcode to use the new `DebugLoc` API.  Added an
explicit conversion to `bool` (avoiding a conversion to `MDLocation`),
since a couple of these use cases need to handle broken code.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@233585 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Duncan P. N. Exon Smith 2015-03-30 19:40:05 +00:00
parent 103d622517
commit 39acf8f243
10 changed files with 44 additions and 43 deletions

View File

@ -1207,6 +1207,7 @@ public:
class DILocation : public DIDescriptor {
public:
explicit DILocation(const MDNode *N) : DIDescriptor(N) {}
DILocation(const MDLocation *N) : DIDescriptor(N) {}
MDLocation *get() const {
return cast_or_null<MDLocation>(DIDescriptor::get());

View File

@ -58,10 +58,23 @@ namespace llvm {
/// IR.
explicit DebugLoc(MDNode *N);
/// \brief Get the underlying \a MDLocation.
///
/// \pre !*this or \c isa<MDLocation>(getAsMDNode()).
/// @{
MDLocation *get() const;
operator MDLocation *() const { return get(); }
MDLocation *operator->() const { return get(); }
MDLocation &operator*() const { return *get(); }
/// @}
/// \brief Check for null.
///
/// Check for null in a way that is safe with broken debug info. Unlike
/// the conversion to \c MDLocation, this doesn't require that \c Loc is of
/// the right type. Important for cases like \a llvm::StripDebugInfo() and
/// \a Instruction::hasMetadata().
explicit operator bool() const { return Loc; }
/// \brief Check whether this has a trivial destructor.
bool hasTrivialDestructor() const { return Loc.hasTrivialDestructor(); }

View File

@ -123,7 +123,7 @@ public:
/// \brief If this builder has a current debug location, set it on the
/// specified instruction.
void SetInstDebugLocation(Instruction *I) const {
if (!CurDbgLocation.isUnknown())
if (CurDbgLocation)
I->setDebugLoc(CurDbgLocation);
}

View File

@ -134,9 +134,7 @@ public:
/// hasMetadata() - Return true if this instruction has any metadata attached
/// to it.
bool hasMetadata() const {
return !DbgLoc.isUnknown() || hasMetadataHashEntry();
}
bool hasMetadata() const { return DbgLoc || hasMetadataHashEntry(); }
/// hasMetadataOtherThanDebugLoc - Return true if this instruction has
/// metadata attached to it other than a debug location.

View File

@ -2089,7 +2089,7 @@ static void WriteFunction(const Function &F, ValueEnumerator &VE,
bool NeedsMetadataAttachment = false;
DebugLoc LastDL;
MDLocation *LastDL = nullptr;
// Finally, emit all the instructions, in order.
for (Function::const_iterator BB = F.begin(), E = F.end(); BB != E; ++BB)
@ -2104,10 +2104,9 @@ static void WriteFunction(const Function &F, ValueEnumerator &VE,
NeedsMetadataAttachment |= I->hasMetadataOtherThanDebugLoc();
// If the instruction has a debug location, emit it.
DebugLoc DL = I->getDebugLoc();
if (DL.isUnknown()) {
MDLocation *DL = I->getDebugLoc();
if (!DL)
continue;
}
if (DL == LastDL) {
// Just repeat the same debug loc as last time.
@ -2115,18 +2114,12 @@ static void WriteFunction(const Function &F, ValueEnumerator &VE,
continue;
}
MDNode *Scope, *IA;
DL.getScopeAndInlinedAt(Scope, IA, I->getContext());
assert(Scope && "Expected valid scope");
Vals.push_back(DL.getLine());
Vals.push_back(DL.getCol());
Vals.push_back(VE.getMetadataOrNullID(Scope));
Vals.push_back(VE.getMetadataOrNullID(IA));
Vals.push_back(DL->getLine());
Vals.push_back(DL->getColumn());
Vals.push_back(VE.getMetadataOrNullID(DL->getScope()));
Vals.push_back(VE.getMetadataOrNullID(DL->getInlinedAt()));
Stream.EmitRecord(bitc::FUNC_CODE_DEBUG_LOC, Vals);
Vals.clear();
LastDL = DL;
}
// Emit names for all the instructions etc.

View File

@ -373,12 +373,10 @@ ValueEnumerator::ValueEnumerator(const Module &M)
for (unsigned i = 0, e = MDs.size(); i != e; ++i)
EnumerateMetadata(MDs[i].second);
if (!I.getDebugLoc().isUnknown()) {
MDNode *Scope, *IA;
I.getDebugLoc().getScopeAndInlinedAt(Scope, IA, I.getContext());
if (Scope) EnumerateMetadata(Scope);
if (IA) EnumerateMetadata(IA);
}
// Don't enumerate the location directly -- it has a special record
// type -- but enumerate its operands.
if (MDLocation *L = I.getDebugLoc())
EnumerateMDNodeOperands(L);
}
}

View File

@ -2181,14 +2181,13 @@ void LLVMDisposeBuilder(LLVMBuilderRef Builder) {
void LLVMSetCurrentDebugLocation(LLVMBuilderRef Builder, LLVMValueRef L) {
MDNode *Loc =
L ? cast<MDNode>(unwrap<MetadataAsValue>(L)->getMetadata()) : nullptr;
unwrap(Builder)->SetCurrentDebugLocation(DebugLoc::getFromDILocation(Loc));
unwrap(Builder)->SetCurrentDebugLocation(DebugLoc(Loc));
}
LLVMValueRef LLVMGetCurrentDebugLocation(LLVMBuilderRef Builder) {
LLVMContext &Context = unwrap(Builder)->getContext();
return wrap(MetadataAsValue::get(
Context,
unwrap(Builder)->getCurrentDebugLocation().getAsMDNode(Context)));
Context, unwrap(Builder)->getCurrentDebugLocation().getAsMDNode()));
}
void LLVMSetInstDebugLocation(LLVMBuilderRef Builder, LLVMValueRef Inst) {

View File

@ -343,8 +343,7 @@ bool DISubprogram::Verify() const {
if (auto *F = getFunction()) {
for (auto &BB : *F) {
for (auto &I : BB) {
MDLocation *DL =
cast_or_null<MDLocation>(I.getDebugLoc().getAsMDNode());
MDLocation *DL = I.getDebugLoc();
if (!DL)
continue;
@ -585,12 +584,12 @@ DISubprogram llvm::getDISubprogram(const Function *F) {
// We look for the first instr that has a debug annotation leading back to F.
for (auto &BB : *F) {
auto Inst = std::find_if(BB.begin(), BB.end(), [](const Instruction &Inst) {
return !Inst.getDebugLoc().isUnknown();
return Inst.getDebugLoc();
});
if (Inst == BB.end())
continue;
DebugLoc DLoc = Inst->getDebugLoc();
const MDNode *Scope = DLoc.getScopeNode();
const MDNode *Scope = DLoc.getInlinedAtScope();
DISubprogram Subprogram = getDISubprogram(Scope);
return Subprogram.describes(F) ? Subprogram : DISubprogram();
}
@ -889,10 +888,10 @@ void DIDescriptor::print(raw_ostream &OS) const {
static void printDebugLoc(DebugLoc DL, raw_ostream &CommentOS,
const LLVMContext &Ctx) {
if (DL.isUnknown())
if (!DL)
return;
DIScope Scope(DL.getScope(Ctx));
DIScope Scope(DL.getScope());
assert(Scope.isScope() && "Scope of a DebugLoc should be a DIScope.");
// Omit the directory, because it's likely to be long and uninteresting.
CommentOS << Scope.getFilename();
@ -900,8 +899,8 @@ static void printDebugLoc(DebugLoc DL, raw_ostream &CommentOS,
if (DL.getCol() != 0)
CommentOS << ':' << DL.getCol();
DebugLoc InlinedAtDL = DebugLoc::getFromDILocation(DL.getInlinedAt(Ctx));
if (InlinedAtDL.isUnknown())
DebugLoc InlinedAtDL = DL.getInlinedAt();
if (!InlinedAtDL)
return;
CommentOS << " @[ ";
@ -914,9 +913,8 @@ void DIVariable::printExtendedName(raw_ostream &OS) const {
StringRef Res = getName();
if (!Res.empty())
OS << Res << "," << getLineNumber();
if (MDNode *InlinedAt = getInlinedAt()) {
DebugLoc InlinedAtDL = DebugLoc::getFromDILocation(InlinedAt);
if (!InlinedAtDL.isUnknown()) {
if (auto *InlinedAt = get()->getInlinedAt()) {
if (DebugLoc InlinedAtDL = InlinedAt) {
OS << " @[";
printDebugLoc(InlinedAtDL, OS, Ctx);
OS << "]";
@ -985,7 +983,7 @@ bool llvm::StripDebugInfo(Module &M) {
++FI)
for (BasicBlock::iterator BI = FI->begin(), BE = FI->end(); BI != BE;
++BI) {
if (!BI->getDebugLoc().isUnknown()) {
if (BI->getDebugLoc()) {
Changed = true;
BI->setDebugLoc(DebugLoc());
}

View File

@ -129,13 +129,14 @@ void DiagnosticInfoSampleProfile::print(DiagnosticPrinter &DP) const {
}
bool DiagnosticInfoOptimizationBase::isLocationAvailable() const {
return !getDebugLoc().isUnknown();
return getDebugLoc();
}
void DiagnosticInfoOptimizationBase::getLocation(StringRef *Filename,
unsigned *Line,
unsigned *Column) const {
DILocation DIL(getDebugLoc().getAsMDNode(getFunction().getContext()));
MDLocation *L = getDebugLoc();
DILocation DIL = L;
*Filename = DIL.getFilename();
*Line = DIL.getLineNumber();
*Column = DIL.getColumnNumber();

View File

@ -1035,7 +1035,7 @@ void Instruction::setMetadata(unsigned KindID, MDNode *Node) {
// Handle 'dbg' as a special case since it is not stored in the hash table.
if (KindID == LLVMContext::MD_dbg) {
DbgLoc = DebugLoc::getFromDILocation(Node);
DbgLoc = DebugLoc(Node);
return;
}
@ -1114,7 +1114,7 @@ void Instruction::getAllMetadataImpl(
Result.clear();
// Handle 'dbg' as a special case since it is not stored in the hash table.
if (!DbgLoc.isUnknown()) {
if (DbgLoc) {
Result.push_back(
std::make_pair((unsigned)LLVMContext::MD_dbg, DbgLoc.getAsMDNode()));
if (!hasMetadataHashEntry()) return;