Skip to content

Commit 2b08de4

Browse files
authored
[RemoveDIs][DebugInfo][NFC] Add Instruction and convenience functions to DPValue (#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.
1 parent e366e04 commit 2b08de4

File tree

2 files changed

+124
-1
lines changed

2 files changed

+124
-1
lines changed

llvm/include/llvm/IR/DebugProgramInstruction.h

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,12 @@
4747
#ifndef LLVM_IR_DEBUGPROGRAMINSTRUCTION_H
4848
#define LLVM_IR_DEBUGPROGRAMINSTRUCTION_H
4949

50-
#include "llvm/ADT/ilist_node.h"
5150
#include "llvm/ADT/ilist.h"
51+
#include "llvm/ADT/ilist_node.h"
5252
#include "llvm/ADT/iterator.h"
5353
#include "llvm/IR/DebugLoc.h"
54+
#include "llvm/IR/Instruction.h"
55+
#include "llvm/IR/SymbolTableListTraits.h"
5456

5557
namespace llvm {
5658

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

96+
DPValue *getNextNode() { return &*std::next(getIterator()); }
97+
DPValue *getPrevNode() { return &*std::prev(getIterator()); }
98+
9499
using self_iterator = simple_ilist<DPValue>::iterator;
95100
using const_self_iterator = simple_ilist<DPValue>::const_iterator;
96101

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

126+
static DPValue *createDPValue(Value *Location, DILocalVariable *DV,
127+
DIExpression *Expr, const DILocation *DI);
128+
static DPValue *createDPValue(Value *Location, DILocalVariable *DV,
129+
DIExpression *Expr, const DILocation *DI,
130+
DPValue &InsertBefore);
131+
static DPValue *createDPVDeclare(Value *Address, DILocalVariable *DV,
132+
DIExpression *Expr, const DILocation *DI);
133+
static DPValue *createDPVDeclare(Value *Address, DILocalVariable *DV,
134+
DIExpression *Expr, const DILocation *DI,
135+
DPValue &InsertBefore);
136+
121137
/// Iterator for ValueAsMetadata that internally uses direct pointer iteration
122138
/// over either a ValueAsMetadata* or a ValueAsMetadata**, dereferencing to the
123139
/// ValueAsMetadata .
@@ -166,6 +182,9 @@ class DPValue : public ilist_node<DPValue>, private DebugValueUser {
166182
}
167183
};
168184

185+
bool isDbgDeclare() { return Type == LocationType::Declare; }
186+
bool isDbgValue() { return Type == LocationType::Value; }
187+
169188
/// Get the locations corresponding to the variable referenced by the debug
170189
/// info intrinsic. Depending on the intrinsic, this could be the
171190
/// variable's value or its address.
@@ -209,6 +228,10 @@ class DPValue : public ilist_node<DPValue>, private DebugValueUser {
209228

210229
Metadata *getRawLocation() const { return DebugValue; }
211230

231+
Value *getValue(unsigned OpIdx = 0) const {
232+
return getVariableLocationOp(OpIdx);
233+
}
234+
212235
/// Use of this should generally be avoided; instead,
213236
/// replaceVariableLocationOp and addVariableLocationOps should be used where
214237
/// possible to avoid creating invalid state.
@@ -224,6 +247,19 @@ class DPValue : public ilist_node<DPValue>, private DebugValueUser {
224247
/// is described.
225248
std::optional<uint64_t> getFragmentSizeInBits() const;
226249

250+
bool isEquivalentTo(const DPValue &Other) {
251+
return std::tie(Type, DebugValue, Variable, Expression, DbgLoc) ==
252+
std::tie(Other.Type, Other.DebugValue, Other.Variable,
253+
Other.Expression, Other.DbgLoc);
254+
}
255+
// Matches the definition of the Instruction version, equivalent to above but
256+
// without checking DbgLoc.
257+
bool isIdenticalToWhenDefined(const DPValue &Other) {
258+
return std::tie(Type, DebugValue, Variable, Expression) ==
259+
std::tie(Other.Type, Other.DebugValue, Other.Variable,
260+
Other.Expression);
261+
}
262+
227263
DPValue *clone() const;
228264
/// Convert this DPValue back into a dbg.value intrinsic.
229265
/// \p InsertBefore Optional position to insert this intrinsic.
@@ -251,6 +287,13 @@ class DPValue : public ilist_node<DPValue>, private DebugValueUser {
251287
LLVMContext &getContext();
252288
const LLVMContext &getContext() const;
253289

290+
/// Insert this DPValue prior to \p InsertBefore. Must not be called if this
291+
/// is already contained in a DPMarker.
292+
void insertBefore(DPValue *InsertBefore);
293+
void insertAfter(DPValue *InsertAfter);
294+
void moveBefore(DPValue *MoveBefore);
295+
void moveAfter(DPValue *MoveAfter);
296+
254297
void print(raw_ostream &O, bool IsForDebug = false) const;
255298
void print(raw_ostream &ROS, ModuleSlotTracker &MST, bool IsForDebug) const;
256299
};
@@ -309,6 +352,8 @@ class DPMarker {
309352

310353
/// Produce a range over all the DPValues in this Marker.
311354
iterator_range<simple_ilist<DPValue>::iterator> getDbgValueRange();
355+
iterator_range<simple_ilist<DPValue>::const_iterator>
356+
getDbgValueRange() const;
312357
/// Transfer any DPValues from \p Src into this DPMarker. If \p InsertAtHead
313358
/// is true, place them before existing DPValues, otherwise afterwards.
314359
void absorbDebugValues(DPMarker &Src, bool InsertAtHead);
@@ -320,6 +365,10 @@ class DPMarker {
320365
/// Insert a DPValue into this DPMarker, at the end of the list. If
321366
/// \p InsertAtHead is true, at the start.
322367
void insertDPValue(DPValue *New, bool InsertAtHead);
368+
/// Insert a DPValue prior to a DPValue contained within this marker.
369+
void insertDPValue(DPValue *New, DPValue *InsertBefore);
370+
/// Insert a DPValue after a DPValue contained within this marker.
371+
void insertDPValueAfter(DPValue *New, DPValue *InsertAfter);
323372
/// Clone all DPMarkers from \p From into this marker. There are numerous
324373
/// options to customise the source/destination, due to gnarliness, see class
325374
/// comment.

llvm/lib/IR/DebugProgramInstruction.cpp

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,34 @@ DPValue::DPValue(Metadata *Location, DILocalVariable *DV, DIExpression *Expr,
4141

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

44+
DPValue *DPValue::createDPValue(Value *Location, DILocalVariable *DV,
45+
DIExpression *Expr, const DILocation *DI) {
46+
return new DPValue(ValueAsMetadata::get(Location), DV, Expr, DI,
47+
LocationType::Value);
48+
}
49+
50+
DPValue *DPValue::createDPValue(Value *Location, DILocalVariable *DV,
51+
DIExpression *Expr, const DILocation *DI,
52+
DPValue &InsertBefore) {
53+
auto *NewDPValue = createDPValue(Location, DV, Expr, DI);
54+
NewDPValue->insertBefore(&InsertBefore);
55+
return NewDPValue;
56+
}
57+
58+
DPValue *DPValue::createDPVDeclare(Value *Address, DILocalVariable *DV,
59+
DIExpression *Expr, const DILocation *DI) {
60+
return new DPValue(ValueAsMetadata::get(Address), DV, Expr, DI,
61+
LocationType::Declare);
62+
}
63+
64+
DPValue *DPValue::createDPVDeclare(Value *Address, DILocalVariable *DV,
65+
DIExpression *Expr, const DILocation *DI,
66+
DPValue &InsertBefore) {
67+
auto *NewDPVDeclare = createDPVDeclare(Address, DV, Expr, DI);
68+
NewDPVDeclare->insertBefore(&InsertBefore);
69+
return NewDPVDeclare;
70+
}
71+
4472
iterator_range<DPValue::location_op_iterator> DPValue::location_ops() const {
4573
auto *MD = getRawLocation();
4674
// If a Value has been deleted, the "location" for this DPValue will be
@@ -249,6 +277,35 @@ const LLVMContext &DPValue::getContext() const {
249277
return getBlock()->getContext();
250278
}
251279

280+
void DPValue::insertBefore(DPValue *InsertBefore) {
281+
assert(!getMarker() &&
282+
"Cannot insert a DPValue that is already has a DPMarker!");
283+
assert(InsertBefore->getMarker() &&
284+
"Cannot insert a DPValue before a DPValue that does not have a "
285+
"DPMarker!");
286+
InsertBefore->getMarker()->insertDPValue(this, InsertBefore);
287+
}
288+
void DPValue::insertAfter(DPValue *InsertAfter) {
289+
assert(!getMarker() &&
290+
"Cannot insert a DPValue that is already has a DPMarker!");
291+
assert(InsertAfter->getMarker() &&
292+
"Cannot insert a DPValue after a DPValue that does not have a "
293+
"DPMarker!");
294+
InsertAfter->getMarker()->insertDPValueAfter(this, InsertAfter);
295+
}
296+
void DPValue::moveBefore(DPValue *MoveBefore) {
297+
assert(getMarker() &&
298+
"Canot move a DPValue that does not currently have a DPMarker!");
299+
removeFromParent();
300+
insertBefore(MoveBefore);
301+
}
302+
void DPValue::moveAfter(DPValue *MoveAfter) {
303+
assert(getMarker() &&
304+
"Canot move a DPValue that does not currently have a DPMarker!");
305+
removeFromParent();
306+
insertAfter(MoveAfter);
307+
}
308+
252309
///////////////////////////////////////////////////////////////////////////////
253310

254311
// An empty, global, DPMarker for the purpose of describing empty ranges of
@@ -313,9 +370,14 @@ void DPMarker::eraseFromParent() {
313370
iterator_range<DPValue::self_iterator> DPMarker::getDbgValueRange() {
314371
return make_range(StoredDPValues.begin(), StoredDPValues.end());
315372
}
373+
iterator_range<DPValue::const_self_iterator>
374+
DPMarker::getDbgValueRange() const {
375+
return make_range(StoredDPValues.begin(), StoredDPValues.end());
376+
}
316377

317378
void DPValue::removeFromParent() {
318379
getMarker()->StoredDPValues.erase(getIterator());
380+
Marker = nullptr;
319381
}
320382

321383
void DPValue::eraseFromParent() {
@@ -328,6 +390,18 @@ void DPMarker::insertDPValue(DPValue *New, bool InsertAtHead) {
328390
StoredDPValues.insert(It, *New);
329391
New->setMarker(this);
330392
}
393+
void DPMarker::insertDPValue(DPValue *New, DPValue *InsertBefore) {
394+
assert(InsertBefore->getMarker() == this &&
395+
"DPValue 'InsertBefore' must be contained in this DPMarker!");
396+
StoredDPValues.insert(InsertBefore->getIterator(), *New);
397+
New->setMarker(this);
398+
}
399+
void DPMarker::insertDPValueAfter(DPValue *New, DPValue *InsertAfter) {
400+
assert(InsertAfter->getMarker() == this &&
401+
"DPValue 'InsertAfter' must be contained in this DPMarker!");
402+
StoredDPValues.insert(++(InsertAfter->getIterator()), *New);
403+
New->setMarker(this);
404+
}
331405

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

0 commit comments

Comments
 (0)