Skip to content

Commit 47a2e73

Browse files
authored
[DebugInfo][RemoveDIs] Make getDbgValueRange inlineable (#79331)
`getDbgValueRange` is the replacement of a common LLVM idiom of: 1) Am I currently looking at a `DbgVariableIntrinsic` instruction? 2) Let's do something special with it! We instead iterate over the range of DPValues attached to an instruction and do special things with those. Unfortunately in the common case of "there is no debug-info", this generates a spurious function call that's paid by non-debug builds. To get around this, make `getDbgValueRange` inlineable so that the "`if (DbgMarker)`" test can be inlined and guard the more expensive call. The false path should be optimisable-awayable to skipping the loop. However, due to header inclusion order we can't just make `Instruction::getDbgValueRange` inline because `DPMarker` hasn't been declared yet. So, define an inlinable function in the llvm:: namespace and pre-declare it -- the eventual code should be inlineable almost 100% of the time.
1 parent 3490f03 commit 47a2e73

File tree

3 files changed

+16
-13
lines changed

3 files changed

+16
-13
lines changed

llvm/include/llvm/IR/DebugProgramInstruction.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -453,6 +453,17 @@ inline raw_ostream &operator<<(raw_ostream &OS, const DPValue &Value) {
453453
return OS;
454454
}
455455

456+
/// Inline helper to return a range of DPValues attached to a marker. It needs
457+
/// to be inlined as it's frequently called, but also come after the declaration
458+
/// of DPMarker. Thus: it's pre-declared by users like Instruction, then an
459+
/// inlineable body defined here.
460+
inline iterator_range<simple_ilist<DPValue>::iterator>
461+
getDbgValueRange(DPMarker *DbgMarker) {
462+
if (!DbgMarker)
463+
return DPMarker::getEmptyDPValueRange();
464+
return DbgMarker->getDbgValueRange();
465+
}
466+
456467
} // namespace llvm
457468

458469
#endif // LLVM_IR_DEBUGPROGRAMINSTRUCTION_H

llvm/include/llvm/IR/Instruction.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ template <> struct ilist_alloc_traits<Instruction> {
4040
static inline void deleteNode(Instruction *V);
4141
};
4242

43+
iterator_range<simple_ilist<DPValue>::iterator> getDbgValueRange(DPMarker *);
44+
4345
class Instruction : public User,
4446
public ilist_node_with_parent<Instruction, BasicBlock,
4547
ilist_iterator_bits<true>> {
@@ -76,7 +78,9 @@ class Instruction : public User,
7678
bool InsertAtHead = false);
7779

7880
/// Return a range over the DPValues attached to this instruction.
79-
iterator_range<simple_ilist<DPValue>::iterator> getDbgValueRange() const;
81+
iterator_range<simple_ilist<DPValue>::iterator> getDbgValueRange() const {
82+
return llvm::getDbgValueRange(DbgMarker);
83+
}
8084

8185
/// Return an iterator to the position of the "Next" DPValue after this
8286
/// instruction, or std::nullopt. This is the position to pass to

llvm/lib/IR/Instruction.cpp

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -243,18 +243,6 @@ Instruction::cloneDebugInfoFrom(const Instruction *From,
243243
return DbgMarker->cloneDebugInfoFrom(From->DbgMarker, FromHere, InsertAtHead);
244244
}
245245

246-
iterator_range<DPValue::self_iterator>
247-
Instruction::getDbgValueRange() const {
248-
BasicBlock *Parent = const_cast<BasicBlock *>(getParent());
249-
assert(Parent && "Instruction must be inserted to have DPValues");
250-
(void)Parent;
251-
252-
if (!DbgMarker)
253-
return DPMarker::getEmptyDPValueRange();
254-
255-
return DbgMarker->getDbgValueRange();
256-
}
257-
258246
std::optional<DPValue::self_iterator> Instruction::getDbgReinsertionPosition() {
259247
// Is there a marker on the next instruction?
260248
DPMarker *NextMarker = getParent()->getNextMarker(this);

0 commit comments

Comments
 (0)