Skip to content

Commit

Permalink
[RemoveDIs][DebugInfo][NFC] Add Instruction and convenience functions…
Browse files Browse the repository at this point in the history
… to DPValue (llvm#77896)

This patch adds a set of functions to the DPValue class that
conveniently perform some common operations, and some that replicate
existing functions on `DbgVariableIntrinsic` and its subclasses.
  • Loading branch information
SLTozer authored Jan 16, 2024
1 parent e366e04 commit 2b08de4
Show file tree
Hide file tree
Showing 2 changed files with 124 additions and 1 deletion.
51 changes: 50 additions & 1 deletion llvm/include/llvm/IR/DebugProgramInstruction.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,12 @@
#ifndef LLVM_IR_DEBUGPROGRAMINSTRUCTION_H
#define LLVM_IR_DEBUGPROGRAMINSTRUCTION_H

#include "llvm/ADT/ilist_node.h"
#include "llvm/ADT/ilist.h"
#include "llvm/ADT/ilist_node.h"
#include "llvm/ADT/iterator.h"
#include "llvm/IR/DebugLoc.h"
#include "llvm/IR/Instruction.h"
#include "llvm/IR/SymbolTableListTraits.h"

namespace llvm {

Expand Down Expand Up @@ -91,6 +93,9 @@ class DPValue : public ilist_node<DPValue>, private DebugValueUser {
void removeFromParent();
void eraseFromParent();

DPValue *getNextNode() { return &*std::next(getIterator()); }
DPValue *getPrevNode() { return &*std::prev(getIterator()); }

using self_iterator = simple_ilist<DPValue>::iterator;
using const_self_iterator = simple_ilist<DPValue>::const_iterator;

Expand Down Expand Up @@ -118,6 +123,17 @@ class DPValue : public ilist_node<DPValue>, private DebugValueUser {
DPValue(Metadata *Location, DILocalVariable *DV, DIExpression *Expr,
const DILocation *DI, LocationType Type = LocationType::Value);

static DPValue *createDPValue(Value *Location, DILocalVariable *DV,
DIExpression *Expr, const DILocation *DI);
static DPValue *createDPValue(Value *Location, DILocalVariable *DV,
DIExpression *Expr, const DILocation *DI,
DPValue &InsertBefore);
static DPValue *createDPVDeclare(Value *Address, DILocalVariable *DV,
DIExpression *Expr, const DILocation *DI);
static DPValue *createDPVDeclare(Value *Address, DILocalVariable *DV,
DIExpression *Expr, const DILocation *DI,
DPValue &InsertBefore);

/// Iterator for ValueAsMetadata that internally uses direct pointer iteration
/// over either a ValueAsMetadata* or a ValueAsMetadata**, dereferencing to the
/// ValueAsMetadata .
Expand Down Expand Up @@ -166,6 +182,9 @@ class DPValue : public ilist_node<DPValue>, private DebugValueUser {
}
};

bool isDbgDeclare() { return Type == LocationType::Declare; }
bool isDbgValue() { return Type == LocationType::Value; }

/// Get the locations corresponding to the variable referenced by the debug
/// info intrinsic. Depending on the intrinsic, this could be the
/// variable's value or its address.
Expand Down Expand Up @@ -209,6 +228,10 @@ class DPValue : public ilist_node<DPValue>, private DebugValueUser {

Metadata *getRawLocation() const { return DebugValue; }

Value *getValue(unsigned OpIdx = 0) const {
return getVariableLocationOp(OpIdx);
}

/// Use of this should generally be avoided; instead,
/// replaceVariableLocationOp and addVariableLocationOps should be used where
/// possible to avoid creating invalid state.
Expand All @@ -224,6 +247,19 @@ class DPValue : public ilist_node<DPValue>, private DebugValueUser {
/// is described.
std::optional<uint64_t> getFragmentSizeInBits() const;

bool isEquivalentTo(const DPValue &Other) {
return std::tie(Type, DebugValue, Variable, Expression, DbgLoc) ==
std::tie(Other.Type, Other.DebugValue, Other.Variable,
Other.Expression, Other.DbgLoc);
}
// Matches the definition of the Instruction version, equivalent to above but
// without checking DbgLoc.
bool isIdenticalToWhenDefined(const DPValue &Other) {
return std::tie(Type, DebugValue, Variable, Expression) ==
std::tie(Other.Type, Other.DebugValue, Other.Variable,
Other.Expression);
}

DPValue *clone() const;
/// Convert this DPValue back into a dbg.value intrinsic.
/// \p InsertBefore Optional position to insert this intrinsic.
Expand Down Expand Up @@ -251,6 +287,13 @@ class DPValue : public ilist_node<DPValue>, private DebugValueUser {
LLVMContext &getContext();
const LLVMContext &getContext() const;

/// Insert this DPValue prior to \p InsertBefore. Must not be called if this
/// is already contained in a DPMarker.
void insertBefore(DPValue *InsertBefore);
void insertAfter(DPValue *InsertAfter);
void moveBefore(DPValue *MoveBefore);
void moveAfter(DPValue *MoveAfter);

void print(raw_ostream &O, bool IsForDebug = false) const;
void print(raw_ostream &ROS, ModuleSlotTracker &MST, bool IsForDebug) const;
};
Expand Down Expand Up @@ -309,6 +352,8 @@ class DPMarker {

/// Produce a range over all the DPValues in this Marker.
iterator_range<simple_ilist<DPValue>::iterator> getDbgValueRange();
iterator_range<simple_ilist<DPValue>::const_iterator>
getDbgValueRange() const;
/// Transfer any DPValues from \p Src into this DPMarker. If \p InsertAtHead
/// is true, place them before existing DPValues, otherwise afterwards.
void absorbDebugValues(DPMarker &Src, bool InsertAtHead);
Expand All @@ -320,6 +365,10 @@ class DPMarker {
/// Insert a DPValue into this DPMarker, at the end of the list. If
/// \p InsertAtHead is true, at the start.
void insertDPValue(DPValue *New, bool InsertAtHead);
/// Insert a DPValue prior to a DPValue contained within this marker.
void insertDPValue(DPValue *New, DPValue *InsertBefore);
/// Insert a DPValue after a DPValue contained within this marker.
void insertDPValueAfter(DPValue *New, DPValue *InsertAfter);
/// Clone all DPMarkers from \p From into this marker. There are numerous
/// options to customise the source/destination, due to gnarliness, see class
/// comment.
Expand Down
74 changes: 74 additions & 0 deletions llvm/lib/IR/DebugProgramInstruction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,34 @@ DPValue::DPValue(Metadata *Location, DILocalVariable *DV, DIExpression *Expr,

void DPValue::deleteInstr() { delete this; }

DPValue *DPValue::createDPValue(Value *Location, DILocalVariable *DV,
DIExpression *Expr, const DILocation *DI) {
return new DPValue(ValueAsMetadata::get(Location), DV, Expr, DI,
LocationType::Value);
}

DPValue *DPValue::createDPValue(Value *Location, DILocalVariable *DV,
DIExpression *Expr, const DILocation *DI,
DPValue &InsertBefore) {
auto *NewDPValue = createDPValue(Location, DV, Expr, DI);
NewDPValue->insertBefore(&InsertBefore);
return NewDPValue;
}

DPValue *DPValue::createDPVDeclare(Value *Address, DILocalVariable *DV,
DIExpression *Expr, const DILocation *DI) {
return new DPValue(ValueAsMetadata::get(Address), DV, Expr, DI,
LocationType::Declare);
}

DPValue *DPValue::createDPVDeclare(Value *Address, DILocalVariable *DV,
DIExpression *Expr, const DILocation *DI,
DPValue &InsertBefore) {
auto *NewDPVDeclare = createDPVDeclare(Address, DV, Expr, DI);
NewDPVDeclare->insertBefore(&InsertBefore);
return NewDPVDeclare;
}

iterator_range<DPValue::location_op_iterator> DPValue::location_ops() const {
auto *MD = getRawLocation();
// If a Value has been deleted, the "location" for this DPValue will be
Expand Down Expand Up @@ -249,6 +277,35 @@ const LLVMContext &DPValue::getContext() const {
return getBlock()->getContext();
}

void DPValue::insertBefore(DPValue *InsertBefore) {
assert(!getMarker() &&
"Cannot insert a DPValue that is already has a DPMarker!");
assert(InsertBefore->getMarker() &&
"Cannot insert a DPValue before a DPValue that does not have a "
"DPMarker!");
InsertBefore->getMarker()->insertDPValue(this, InsertBefore);
}
void DPValue::insertAfter(DPValue *InsertAfter) {
assert(!getMarker() &&
"Cannot insert a DPValue that is already has a DPMarker!");
assert(InsertAfter->getMarker() &&
"Cannot insert a DPValue after a DPValue that does not have a "
"DPMarker!");
InsertAfter->getMarker()->insertDPValueAfter(this, InsertAfter);
}
void DPValue::moveBefore(DPValue *MoveBefore) {
assert(getMarker() &&
"Canot move a DPValue that does not currently have a DPMarker!");
removeFromParent();
insertBefore(MoveBefore);
}
void DPValue::moveAfter(DPValue *MoveAfter) {
assert(getMarker() &&
"Canot move a DPValue that does not currently have a DPMarker!");
removeFromParent();
insertAfter(MoveAfter);
}

///////////////////////////////////////////////////////////////////////////////

// An empty, global, DPMarker for the purpose of describing empty ranges of
Expand Down Expand Up @@ -313,9 +370,14 @@ void DPMarker::eraseFromParent() {
iterator_range<DPValue::self_iterator> DPMarker::getDbgValueRange() {
return make_range(StoredDPValues.begin(), StoredDPValues.end());
}
iterator_range<DPValue::const_self_iterator>
DPMarker::getDbgValueRange() const {
return make_range(StoredDPValues.begin(), StoredDPValues.end());
}

void DPValue::removeFromParent() {
getMarker()->StoredDPValues.erase(getIterator());
Marker = nullptr;
}

void DPValue::eraseFromParent() {
Expand All @@ -328,6 +390,18 @@ void DPMarker::insertDPValue(DPValue *New, bool InsertAtHead) {
StoredDPValues.insert(It, *New);
New->setMarker(this);
}
void DPMarker::insertDPValue(DPValue *New, DPValue *InsertBefore) {
assert(InsertBefore->getMarker() == this &&
"DPValue 'InsertBefore' must be contained in this DPMarker!");
StoredDPValues.insert(InsertBefore->getIterator(), *New);
New->setMarker(this);
}
void DPMarker::insertDPValueAfter(DPValue *New, DPValue *InsertAfter) {
assert(InsertAfter->getMarker() == this &&
"DPValue 'InsertAfter' must be contained in this DPMarker!");
StoredDPValues.insert(++(InsertAfter->getIterator()), *New);
New->setMarker(this);
}

void DPMarker::absorbDebugValues(DPMarker &Src, bool InsertAtHead) {
auto It = InsertAtHead ? StoredDPValues.begin() : StoredDPValues.end();
Expand Down

0 comments on commit 2b08de4

Please sign in to comment.