Skip to content

Commit cbfc105

Browse files
committed
[NFC][DebugInfo] Deprecate iterator-taking moveBefore and getFirstNonPHI
The RemoveDIs project has moved debugging information out of intrinsics, but metadata is sometimes required when inserting instructions at the start of blocks. To maintain accuracy, in the future instruction insertion needs to be performed with a BasicBlock::iterator, so that a debug-info bit can be stored in the iterator. To that end, we're deprecating getFirstNonPHI and moveBefore for instruction pointers. They're replaced by getFirstNonPHIIt and an iterator-taking moveBefore: switching to the replacement is straightforwards, and 99% of call-sites need only to unwrap the iterator with &* or call getIterator() on an Instruction pointer. The exception is when inserting instructions at the start of a block: if you call getFirstNonPHI() (or begin() or getFirstInsertionPt()) and then insert something at that position, you must pass the BasicBlock::iterator returned into the insertion method. Unwrapping with &* and then calling getIterator strips the debug-info bit we wish to preserve. Please do contact us about any use case that's confusing or unclear: https://discourse.llvm.org/t/psa-ir-output-changing-from-debug-intrinsics-to-debug-records/79578
1 parent c546b53 commit cbfc105

File tree

4 files changed

+51
-14
lines changed

4 files changed

+51
-14
lines changed

llvm/include/llvm/IR/BasicBlock.h

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -280,14 +280,26 @@ class BasicBlock final : public Value, // Basic blocks are data objects also
280280
/// When adding instructions to the beginning of the basic block, they should
281281
/// be added before the returned value, not before the first instruction,
282282
/// which might be PHI. Returns 0 is there's no non-PHI instruction.
283+
///
284+
/// Deprecated in favour of getFirstNonPHIIt, which returns an iterator that
285+
/// preserves some debugging information.
286+
LLVM_DEPRECATED("Use iterators as instruction positions", "getFirstNonPHIIt")
283287
const Instruction* getFirstNonPHI() const;
288+
LLVM_DEPRECATED("Use iterators as instruction positions", "getFirstNonPHIIt")
284289
Instruction* getFirstNonPHI() {
285290
return const_cast<Instruction *>(
286291
static_cast<const BasicBlock *>(this)->getFirstNonPHI());
287292
}
288293

289-
/// Iterator returning form of getFirstNonPHI. Installed as a placeholder for
290-
/// the RemoveDIs project that will eventually remove debug intrinsics.
294+
/// Returns an iterator to the first instruction in this block that is not a
295+
/// PHINode instruction.
296+
///
297+
/// When adding instructions to the beginning of the basic block, they should
298+
/// be added before the returned value, not before the first instruction,
299+
/// which might be PHI. Returns end() if there's no non-PHI instruction.
300+
///
301+
/// Avoid unwrapping the iterator to an Instruction* before inserting here,
302+
/// as debug-info relevant information is preserved in the iterator.
291303
InstListType::const_iterator getFirstNonPHIIt() const;
292304
InstListType::iterator getFirstNonPHIIt() {
293305
BasicBlock::iterator It =

llvm/include/llvm/IR/Instruction.h

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,12 @@ class Instruction : public User,
206206

207207
/// Insert an unlinked instruction into a basic block immediately before
208208
/// the specified instruction.
209+
///
210+
/// Deprecated in favour of the iterator-accepting flavour. Iterators at the
211+
/// start of a block such as BasicBlock::getFirstNonPHIIt must be passed into
212+
/// insertBefore without unwrapping/rewrapping. For all other positions, call
213+
/// getIterator to fetch the instruction iterator.
214+
LLVM_DEPRECATED("Use iterators as instruction positions", "")
209215
void insertBefore(Instruction *InsertPos);
210216

211217
/// Insert an unlinked instruction into a basic block immediately before
@@ -229,6 +235,12 @@ class Instruction : public User,
229235

230236
/// Unlink this instruction from its current basic block and insert it into
231237
/// the basic block that MovePos lives in, right before MovePos.
238+
///
239+
/// Deprecated in favour of the iterator-accepting flavour. Iterators at the
240+
/// start of a block such as BasicBlock::getFirstNonPHIIt must be passed into
241+
/// moveBefore without unwrapping/rewrapping. For all other positions, call
242+
/// getIterator to fetch the instruction iterator.
243+
LLVM_DEPRECATED("Use iterators as instruction positions", "")
232244
void moveBefore(Instruction *MovePos);
233245

234246
/// Unlink this instruction from its current basic block and insert it into
@@ -238,8 +250,20 @@ class Instruction : public User,
238250
/// Perform a \ref moveBefore operation, while signalling that the caller
239251
/// intends to preserve the original ordering of instructions. This implicitly
240252
/// means that any adjacent debug-info should move with this instruction.
241-
/// This method is currently a no-op placeholder, but it will become
242-
/// meaningful when the "RemoveDIs" project is enabled.
253+
void moveBeforePreserving(InstListType::iterator MovePos);
254+
255+
/// Perform a \ref moveBefore operation, while signalling that the caller
256+
/// intends to preserve the original ordering of instructions. This implicitly
257+
/// means that any adjacent debug-info should move with this instruction.
258+
void moveBeforePreserving(BasicBlock &BB, InstListType::iterator I);
259+
260+
/// Perform a \ref moveBefore operation, while signalling that the caller
261+
/// intends to preserve the original ordering of instructions. This implicitly
262+
/// means that any adjacent debug-info should move with this instruction.
263+
///
264+
/// Deprecated in favour of the iterator-accepting flavour of
265+
/// moveBeforePreserving, as all insertions should be at iterator positions.
266+
LLVM_DEPRECATED("Use iterators as instruction positions", "")
243267
void moveBeforePreserving(Instruction *MovePos);
244268

245269
private:
@@ -253,11 +277,6 @@ class Instruction : public User,
253277
/// \pre I is a valid iterator into BB.
254278
void moveBefore(BasicBlock &BB, InstListType::iterator I);
255279

256-
void moveBeforePreserving(BasicBlock &BB, InstListType::iterator I);
257-
/// Unlink this instruction from its current basic block and insert it into
258-
/// the basic block that MovePos lives in, right before MovePos.
259-
void moveBeforePreserving(InstListType::iterator I);
260-
261280
/// Unlink this instruction from its current basic block and insert it into
262281
/// the basic block that MovePos lives in, right after MovePos.
263282
void moveAfter(Instruction *MovePos);

llvm/lib/IR/BasicBlock.cpp

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -372,9 +372,16 @@ const Instruction* BasicBlock::getFirstNonPHI() const {
372372
}
373373

374374
BasicBlock::const_iterator BasicBlock::getFirstNonPHIIt() const {
375-
const Instruction *I = getFirstNonPHI();
375+
const Instruction *I = [&]() -> const Instruction * {
376+
for (const Instruction &I : *this)
377+
if (!isa<PHINode>(I))
378+
return &I;
379+
return nullptr;
380+
}();
381+
376382
if (!I)
377383
return end();
384+
378385
BasicBlock::const_iterator It = I->getIterator();
379386
// Set the head-inclusive bit to indicate that this iterator includes
380387
// any debug-info at the start of the block. This is a no-op unless the
@@ -414,11 +421,10 @@ BasicBlock::getFirstNonPHIOrDbgOrLifetime(bool SkipPseudoOp) const {
414421
}
415422

416423
BasicBlock::const_iterator BasicBlock::getFirstInsertionPt() const {
417-
const Instruction *FirstNonPHI = getFirstNonPHI();
418-
if (!FirstNonPHI)
424+
const_iterator InsertPt = getFirstNonPHIIt();
425+
if (InsertPt == end())
419426
return end();
420427

421-
const_iterator InsertPt = FirstNonPHI->getIterator();
422428
if (InsertPt->isEHPad()) ++InsertPt;
423429
// Set the head-inclusive bit to indicate that this iterator includes
424430
// any debug-info at the start of the block. This is a no-op unless the

llvm/lib/IR/Instruction.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1169,7 +1169,7 @@ bool Instruction::mayThrow(bool IncludePhaseOneUnwind) const {
11691169
// Landingpads themselves don't unwind -- however, an invoke of a skipped
11701170
// landingpad may continue unwinding.
11711171
BasicBlock *UnwindDest = cast<InvokeInst>(this)->getUnwindDest();
1172-
Instruction *Pad = UnwindDest->getFirstNonPHI();
1172+
BasicBlock::iterator Pad = UnwindDest->getFirstNonPHIIt();
11731173
if (auto *LP = dyn_cast<LandingPadInst>(Pad))
11741174
return canUnwindPastLandingPad(LP, IncludePhaseOneUnwind);
11751175
return false;

0 commit comments

Comments
 (0)