Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[NFC][DebugInfo] Deprecate iterator-taking moveBefore and getFirstNonPHI #124290

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions llvm/include/llvm/IR/BasicBlock.h
Original file line number Diff line number Diff line change
Expand Up @@ -280,14 +280,26 @@ class BasicBlock final : public Value, // Basic blocks are data objects also
/// When adding instructions to the beginning of the basic block, they should
/// be added before the returned value, not before the first instruction,
/// which might be PHI. Returns 0 is there's no non-PHI instruction.
///
/// Deprecated in favour of getFirstNonPHIIt, which returns an iterator that
/// preserves some debugging information.
LLVM_DEPRECATED("Use iterators as instruction positions", "getFirstNonPHIIt")
const Instruction* getFirstNonPHI() const;
LLVM_DEPRECATED("Use iterators as instruction positions", "getFirstNonPHIIt")
Instruction* getFirstNonPHI() {
return const_cast<Instruction *>(
static_cast<const BasicBlock *>(this)->getFirstNonPHI());
}

/// Iterator returning form of getFirstNonPHI. Installed as a placeholder for
/// the RemoveDIs project that will eventually remove debug intrinsics.
/// Returns an iterator to the first instruction in this block that is not a
/// PHINode instruction.
///
/// When adding instructions to the beginning of the basic block, they should
/// be added before the returned value, not before the first instruction,
/// which might be PHI. Returns end() if there's no non-PHI instruction.
///
/// Avoid unwrapping the iterator to an Instruction* before inserting here,
/// as debug-info relevant information is preserved in the iterator.
InstListType::const_iterator getFirstNonPHIIt() const;
InstListType::iterator getFirstNonPHIIt() {
BasicBlock::iterator It =
Expand Down
33 changes: 26 additions & 7 deletions llvm/include/llvm/IR/Instruction.h
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,12 @@ class Instruction : public User,

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

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

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

/// Unlink this instruction from its current basic block and insert it into
Expand All @@ -238,8 +250,20 @@ class Instruction : public User,
/// Perform a \ref moveBefore operation, while signalling that the caller
/// intends to preserve the original ordering of instructions. This implicitly
/// means that any adjacent debug-info should move with this instruction.
/// This method is currently a no-op placeholder, but it will become
/// meaningful when the "RemoveDIs" project is enabled.
void moveBeforePreserving(InstListType::iterator MovePos);

/// Perform a \ref moveBefore operation, while signalling that the caller
/// intends to preserve the original ordering of instructions. This implicitly
/// means that any adjacent debug-info should move with this instruction.
void moveBeforePreserving(BasicBlock &BB, InstListType::iterator I);

/// Perform a \ref moveBefore operation, while signalling that the caller
/// intends to preserve the original ordering of instructions. This implicitly
/// means that any adjacent debug-info should move with this instruction.
///
/// Deprecated in favour of the iterator-accepting flavour of
/// moveBeforePreserving, as all insertions should be at iterator positions.
LLVM_DEPRECATED("Use iterators as instruction positions", "")
void moveBeforePreserving(Instruction *MovePos);

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

void moveBeforePreserving(BasicBlock &BB, InstListType::iterator I);
/// Unlink this instruction from its current basic block and insert it into
/// the basic block that MovePos lives in, right before MovePos.
void moveBeforePreserving(InstListType::iterator I);

/// Unlink this instruction from its current basic block and insert it into
/// the basic block that MovePos lives in, right after MovePos.
void moveAfter(Instruction *MovePos);
Expand Down
14 changes: 10 additions & 4 deletions llvm/lib/IR/BasicBlock.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -372,9 +372,16 @@ const Instruction* BasicBlock::getFirstNonPHI() const {
}

BasicBlock::const_iterator BasicBlock::getFirstNonPHIIt() const {
const Instruction *I = getFirstNonPHI();
const Instruction *I = [&]() -> const Instruction * {
for (const Instruction &I : *this)
if (!isa<PHINode>(I))
return &I;
return nullptr;
}();

if (!I)
return end();

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this function can be simplified by just folding the success-return-path into the loop (and removing the lambda). Not a big deal, YMMV.

BasicBlock::const_iterator It = I->getIterator();
// Set the head-inclusive bit to indicate that this iterator includes
// any debug-info at the start of the block. This is a no-op unless the
Expand Down Expand Up @@ -414,11 +421,10 @@ BasicBlock::getFirstNonPHIOrDbgOrLifetime(bool SkipPseudoOp) const {
}

BasicBlock::const_iterator BasicBlock::getFirstInsertionPt() const {
const Instruction *FirstNonPHI = getFirstNonPHI();
if (!FirstNonPHI)
const_iterator InsertPt = getFirstNonPHIIt();
if (InsertPt == end())
return end();

const_iterator InsertPt = FirstNonPHI->getIterator();
if (InsertPt->isEHPad()) ++InsertPt;
// Set the head-inclusive bit to indicate that this iterator includes
// any debug-info at the start of the block. This is a no-op unless the
Expand Down
2 changes: 1 addition & 1 deletion llvm/lib/IR/Instruction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1169,7 +1169,7 @@ bool Instruction::mayThrow(bool IncludePhaseOneUnwind) const {
// Landingpads themselves don't unwind -- however, an invoke of a skipped
// landingpad may continue unwinding.
BasicBlock *UnwindDest = cast<InvokeInst>(this)->getUnwindDest();
Instruction *Pad = UnwindDest->getFirstNonPHI();
BasicBlock::iterator Pad = UnwindDest->getFirstNonPHIIt();
if (auto *LP = dyn_cast<LandingPadInst>(Pad))
return canUnwindPastLandingPad(LP, IncludePhaseOneUnwind);
return false;
Expand Down
Loading