Skip to content

[NFC][DebugInfo] Make some block-start-position methods return iterators #124287

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

Merged
merged 3 commits into from
Jan 27, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
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
6 changes: 3 additions & 3 deletions lldb/source/Plugins/ExpressionParser/Clang/IRForTarget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ static llvm::Value *FindEntryInstruction(llvm::Function *function) {
if (function->empty())
return nullptr;

return function->getEntryBlock().getFirstNonPHIOrDbg();
return &*function->getEntryBlock().getFirstNonPHIOrDbg();
}

IRForTarget::IRForTarget(lldb_private::ClangExpressionDeclMap *decl_map,
Expand Down Expand Up @@ -361,7 +361,7 @@ bool IRForTarget::CreateResultVariable(llvm::Function &llvm_function) {
// there's nothing to put into its equivalent persistent variable.

BasicBlock &entry_block(llvm_function.getEntryBlock());
Instruction *first_entry_instruction(entry_block.getFirstNonPHIOrDbg());
Instruction *first_entry_instruction(&*entry_block.getFirstNonPHIOrDbg());

if (!first_entry_instruction)
return false;
Expand Down Expand Up @@ -1505,7 +1505,7 @@ bool IRForTarget::ReplaceVariables(Function &llvm_function) {
LLDB_LOG(log, "Arg: \"{0}\"", PrintValue(argument));

BasicBlock &entry_block(llvm_function.getEntryBlock());
Instruction *FirstEntryInstruction(entry_block.getFirstNonPHIOrDbg());
Instruction *FirstEntryInstruction(&*entry_block.getFirstNonPHIOrDbg());

if (!FirstEntryInstruction) {
m_error_stream.Printf("Internal error [IRForTarget]: Couldn't find the "
Expand Down
22 changes: 12 additions & 10 deletions llvm/include/llvm/IR/BasicBlock.h
Original file line number Diff line number Diff line change
Expand Up @@ -299,22 +299,24 @@ class BasicBlock final : public Value, // Basic blocks are data objects also
/// Returns a pointer to the first instruction in this block that is not a
/// PHINode or a debug intrinsic, or any pseudo operation if \c SkipPseudoOp
/// is true.
const Instruction *getFirstNonPHIOrDbg(bool SkipPseudoOp = true) const;
Instruction *getFirstNonPHIOrDbg(bool SkipPseudoOp = true) {
return const_cast<Instruction *>(
static_cast<const BasicBlock *>(this)->getFirstNonPHIOrDbg(
SkipPseudoOp));
InstListType::const_iterator
getFirstNonPHIOrDbg(bool SkipPseudoOp = true) const;
InstListType::iterator getFirstNonPHIOrDbg(bool SkipPseudoOp = true) {
return static_cast<const BasicBlock *>(this)
->getFirstNonPHIOrDbg(SkipPseudoOp)
.getNonConst();
}

/// Returns a pointer to the first instruction in this block that is not a
/// PHINode, a debug intrinsic, or a lifetime intrinsic, or any pseudo
/// operation if \c SkipPseudoOp is true.
const Instruction *
InstListType::const_iterator
getFirstNonPHIOrDbgOrLifetime(bool SkipPseudoOp = true) const;
Instruction *getFirstNonPHIOrDbgOrLifetime(bool SkipPseudoOp = true) {
return const_cast<Instruction *>(
static_cast<const BasicBlock *>(this)->getFirstNonPHIOrDbgOrLifetime(
SkipPseudoOp));
InstListType::iterator
getFirstNonPHIOrDbgOrLifetime(bool SkipPseudoOp = true) {
return static_cast<const BasicBlock *>(this)
->getFirstNonPHIOrDbgOrLifetime(SkipPseudoOp)
.getNonConst();
}

/// Returns an iterator to the first instruction in this block that is
Expand Down
2 changes: 1 addition & 1 deletion llvm/lib/CodeGen/CodeGenPrepare.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -990,7 +990,7 @@ bool CodeGenPrepare::isMergingEmptyBlockProfitable(BasicBlock *BB,
isa<IndirectBrInst>(Pred->getTerminator())))
return true;

if (BB->getTerminator() != BB->getFirstNonPHIOrDbg())
if (BB->getTerminator() != &*BB->getFirstNonPHIOrDbg())
return true;

// We use a simple cost heuristic which determine skipping merging is
Expand Down
30 changes: 21 additions & 9 deletions llvm/lib/IR/BasicBlock.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -383,20 +383,25 @@ BasicBlock::const_iterator BasicBlock::getFirstNonPHIIt() const {
return It;
}

const Instruction *BasicBlock::getFirstNonPHIOrDbg(bool SkipPseudoOp) const {
BasicBlock::const_iterator
BasicBlock::getFirstNonPHIOrDbg(bool SkipPseudoOp) const {
for (const Instruction &I : *this) {
if (isa<PHINode>(I) || isa<DbgInfoIntrinsic>(I))
continue;

if (SkipPseudoOp && isa<PseudoProbeInst>(I))
continue;

return &I;
BasicBlock::const_iterator It = I.getIterator();
// This position comes after any debug records, the head bit should remain
// unset.
assert(!It.getHeadBit());
return It;
}
return nullptr;
return end();
}

const Instruction *
BasicBlock::const_iterator
BasicBlock::getFirstNonPHIOrDbgOrLifetime(bool SkipPseudoOp) const {
for (const Instruction &I : *this) {
if (isa<PHINode>(I) || isa<DbgInfoIntrinsic>(I))
Expand All @@ -408,9 +413,14 @@ BasicBlock::getFirstNonPHIOrDbgOrLifetime(bool SkipPseudoOp) const {
if (SkipPseudoOp && isa<PseudoProbeInst>(I))
continue;

return &I;
BasicBlock::const_iterator It = I.getIterator();
// This position comes after any debug records, the head bit should remain
// unset.
assert(!It.getHeadBit());

return It;
}
return nullptr;
return end();
}

BasicBlock::const_iterator BasicBlock::getFirstInsertionPt() const {
Expand All @@ -428,11 +438,10 @@ BasicBlock::const_iterator BasicBlock::getFirstInsertionPt() const {
}

BasicBlock::const_iterator BasicBlock::getFirstNonPHIOrDbgOrAlloca() 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;

Expand All @@ -448,6 +457,9 @@ BasicBlock::const_iterator BasicBlock::getFirstNonPHIOrDbgOrAlloca() const {
++InsertPt;
}
}

// Signal that this comes after any debug records.
InsertPt.setHeadBit(false);
Comment on lines +461 to +462
Copy link
Contributor

Choose a reason for hiding this comment

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

And again

Copy link
Member Author

Choose a reason for hiding this comment

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

The other two call-sites always generate iterators with a false head bit anyway, but there's a path from getFirstNonPHIIt to this line without InsertPt being assigned a different iterator. getFirstNonPHIIt will set the head bit, therefore we have to explicitly clear it here to ensure the position is "after" any debug records.

return InsertPt;
}

Expand Down
2 changes: 1 addition & 1 deletion llvm/lib/Target/AArch64/AArch64TargetTransformInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2188,7 +2188,7 @@ static std::optional<Instruction *> instCombineDMB(InstCombiner &IC,
NI = NI->getNextNonDebugInstruction();
if (!NI) {
if (auto *SuccBB = NIBB->getUniqueSuccessor())
NI = SuccBB->getFirstNonPHIOrDbgOrLifetime();
NI = &*SuccBB->getFirstNonPHIOrDbgOrLifetime();
else
break;
}
Expand Down
4 changes: 2 additions & 2 deletions llvm/lib/Target/AMDGPU/SIAnnotateControlFlow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ Value *SIAnnotateControlFlow::handleLoopCondition(
} else if (L->contains(Inst)) {
Insert = Term;
} else {
Insert = L->getHeader()->getFirstNonPHIOrDbgOrLifetime();
Insert = &*L->getHeader()->getFirstNonPHIOrDbgOrLifetime();
}

return CreateBreak(Insert);
Expand All @@ -247,7 +247,7 @@ Value *SIAnnotateControlFlow::handleLoopCondition(
}

if (isa<Argument>(Cond)) {
Instruction *Insert = L->getHeader()->getFirstNonPHIOrDbgOrLifetime();
Instruction *Insert = &*L->getHeader()->getFirstNonPHIOrDbgOrLifetime();
return CreateBreak(Insert);
}

Expand Down
2 changes: 1 addition & 1 deletion llvm/lib/Target/NVPTX/NVPTXGenericToNVVM.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ bool GenericToNVVM::runOnModule(Module &M) {
if (F.isDeclaration()) {
continue;
}
IRBuilder<> Builder(F.getEntryBlock().getFirstNonPHIOrDbg());
IRBuilder<> Builder(&*F.getEntryBlock().getFirstNonPHIOrDbg());
for (BasicBlock &BB : F) {
for (Instruction &II : BB) {
for (unsigned i = 0, e = II.getNumOperands(); i < e; ++i) {
Expand Down
3 changes: 2 additions & 1 deletion llvm/lib/Transforms/Coroutines/CoroFrame.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1697,7 +1697,8 @@ static void eliminateSwiftErrorAlloca(Function &F, AllocaInst *Alloca,
static void eliminateSwiftErrorArgument(Function &F, Argument &Arg,
coro::Shape &Shape,
SmallVectorImpl<AllocaInst*> &AllocasToPromote) {
IRBuilder<> Builder(F.getEntryBlock().getFirstNonPHIOrDbg());
IRBuilder<> Builder(&F.getEntryBlock(),
F.getEntryBlock().getFirstNonPHIOrDbg());

auto ArgTy = cast<PointerType>(Arg.getType());
auto ValueTy = PointerType::getUnqual(F.getContext());
Expand Down
5 changes: 3 additions & 2 deletions llvm/lib/Transforms/Coroutines/CoroSplit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -597,7 +597,8 @@ static void replaceSwiftErrorOps(Function &F, coro::Shape &Shape,
}

// Create a swifterror alloca.
IRBuilder<> Builder(F.getEntryBlock().getFirstNonPHIOrDbg());
IRBuilder<> Builder(&F.getEntryBlock(),
F.getEntryBlock().getFirstNonPHIOrDbg());
auto Alloca = Builder.CreateAlloca(ValueTy);
Alloca->setSwiftError(true);

Expand Down Expand Up @@ -828,7 +829,7 @@ static void updateScopeLine(Instruction *ActiveSuspend,
// instructions are not in the same BB.
if (auto *Branch = dyn_cast_or_null<BranchInst>(Successor);
Branch && Branch->isUnconditional())
Successor = Branch->getSuccessor(0)->getFirstNonPHIOrDbg();
Successor = &*Branch->getSuccessor(0)->getFirstNonPHIOrDbg();

// Find the first successor of ActiveSuspend with a non-zero line location.
// If that matches the file of ActiveSuspend, use it.
Expand Down
2 changes: 1 addition & 1 deletion llvm/lib/Transforms/IPO/IROutliner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ Value *OutlinableRegion::findCorrespondingValueIn(const OutlinableRegion &Other,
BasicBlock *
OutlinableRegion::findCorrespondingBlockIn(const OutlinableRegion &Other,
BasicBlock *BB) {
Instruction *FirstNonPHI = BB->getFirstNonPHIOrDbg();
Instruction *FirstNonPHI = &*BB->getFirstNonPHIOrDbg();
assert(FirstNonPHI && "block is empty?");
Value *CorrespondingVal = findCorrespondingValueIn(Other, FirstNonPHI);
if (!CorrespondingVal)
Expand Down
4 changes: 2 additions & 2 deletions llvm/lib/Transforms/IPO/SCCP.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -235,11 +235,11 @@ static bool runIPSCCP(
// nodes in executable blocks we found values for. The function's entry
// block is not part of BlocksToErase, so we have to handle it separately.
for (BasicBlock *BB : BlocksToErase) {
NumInstRemoved += changeToUnreachable(BB->getFirstNonPHIOrDbg(),
NumInstRemoved += changeToUnreachable(&*BB->getFirstNonPHIOrDbg(),
/*PreserveLCSSA=*/false, &DTU);
}
if (!Solver.isBlockExecutable(&F.front()))
NumInstRemoved += changeToUnreachable(F.front().getFirstNonPHIOrDbg(),
NumInstRemoved += changeToUnreachable(&*F.front().getFirstNonPHIOrDbg(),
/*PreserveLCSSA=*/false, &DTU);

BasicBlock *NewUnreachableBB = nullptr;
Expand Down
2 changes: 1 addition & 1 deletion llvm/lib/Transforms/IPO/SampleProfile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1747,7 +1747,7 @@ void SampleProfileLoader::generateMDProfMetadata(Function &F) {
if (Weight != 0) {
if (Weight > MaxWeight) {
MaxWeight = Weight;
MaxDestInst = Succ->getFirstNonPHIOrDbgOrLifetime();
MaxDestInst = &*Succ->getFirstNonPHIOrDbgOrLifetime();
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -462,8 +462,8 @@ Instruction *InstCombinerImpl::visitAllocaInst(AllocaInst &AI) {

// Get the first instruction in the entry block.
BasicBlock &EntryBlock = AI.getParent()->getParent()->getEntryBlock();
Instruction *FirstInst = EntryBlock.getFirstNonPHIOrDbg();
if (FirstInst != &AI) {
BasicBlock::iterator FirstInst = EntryBlock.getFirstNonPHIOrDbg();
if (&*FirstInst != &AI) {
// If the entry block doesn't start with a zero-size alloca then move
// this one to the start of the entry block. There is no problem with
// dominance as the array size was forced to a constant earlier already.
Expand Down
2 changes: 1 addition & 1 deletion llvm/lib/Transforms/Scalar/CallSiteSplitting.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -415,7 +415,7 @@ static void splitCallSite(CallBase &CB,
// constant incoming values.
static bool isPredicatedOnPHI(CallBase &CB) {
BasicBlock *Parent = CB.getParent();
if (&CB != Parent->getFirstNonPHIOrDbg())
if (&CB != &*Parent->getFirstNonPHIOrDbg())
return false;

for (auto &PN : Parent->phis()) {
Expand Down
2 changes: 1 addition & 1 deletion llvm/lib/Transforms/Scalar/SimpleLoopUnswitch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -770,7 +770,7 @@ static bool unswitchTrivialSwitch(Loop &L, SwitchInst &SI, DominatorTree &DT,
// instruction in the block.
auto *TI = BBToCheck.getTerminator();
bool isUnreachable = isa<UnreachableInst>(TI);
return !isUnreachable || BBToCheck.getFirstNonPHIOrDbg() != TI;
return !isUnreachable || &*BBToCheck.getFirstNonPHIOrDbg() != TI;
};

SmallVector<int, 4> ExitCaseIndices;
Expand Down
2 changes: 1 addition & 1 deletion llvm/lib/Transforms/Utils/CodeMoverUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -427,7 +427,7 @@ void llvm::moveInstructionsToTheBeginning(BasicBlock &FromBB, BasicBlock &ToBB,
DependenceInfo &DI) {
for (Instruction &I :
llvm::make_early_inc_range(llvm::drop_begin(llvm::reverse(FromBB)))) {
Instruction *MovePos = ToBB.getFirstNonPHIOrDbg();
BasicBlock::iterator MovePos = ToBB.getFirstNonPHIOrDbg();

if (isSafeToMoveBefore(I, *MovePos, DT, &PDT, &DI))
I.moveBeforePreserving(MovePos);
Expand Down
2 changes: 1 addition & 1 deletion llvm/lib/Transforms/Utils/IRNormalizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -475,7 +475,7 @@ void IRNormalizer::reorderInstructions(Function &F) const {
Call->getIntrinsicID() == Intrinsic::experimental_convergence_loop)
FirstNonPHIOrDbgOrAlloca++;
}
Instruction->moveBefore(&*FirstNonPHIOrDbgOrAlloca);
Instruction->moveBefore(FirstNonPHIOrDbgOrAlloca);
TopologicalSort.pop();
}
}
Expand Down
4 changes: 2 additions & 2 deletions llvm/lib/Transforms/Utils/SimplifyCFG.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6004,7 +6004,7 @@ static bool eliminateDeadSwitchCases(SwitchInst *SI, DomTreeUpdater *DTU,
/// the phi node, and set PhiIndex to BB's index in the phi node.
static PHINode *findPHIForConditionForwarding(ConstantInt *CaseValue,
BasicBlock *BB, int *PhiIndex) {
if (BB->getFirstNonPHIOrDbg() != BB->getTerminator())
if (&*BB->getFirstNonPHIIt() != BB->getTerminator())
return nullptr; // BB must be empty to be a candidate for simplification.
if (!BB->getSinglePredecessor())
return nullptr; // BB must be dominated by the switch.
Expand Down Expand Up @@ -7885,7 +7885,7 @@ bool SimplifyCFGOpt::simplifyUncondBranch(BranchInst *BI,
Options.NeedCanonicalLoop &&
(!LoopHeaders.empty() && BB->hasNPredecessorsOrMore(2) &&
(is_contained(LoopHeaders, BB) || is_contained(LoopHeaders, Succ)));
BasicBlock::iterator I = BB->getFirstNonPHIOrDbg(true)->getIterator();
BasicBlock::iterator I = BB->getFirstNonPHIOrDbg();
if (I->isTerminator() && BB != &BB->getParent()->getEntryBlock() &&
!NeedCanonicalLoop && TryToSimplifyUncondBranchFromEmptyBlock(BB, DTU))
return true;
Expand Down
24 changes: 16 additions & 8 deletions llvm/unittests/Frontend/OpenMPIRBuilderTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4643,9 +4643,11 @@ TEST_F(OpenMPIRBuilderTest, CreateTeamsWithThreadLimit) {
dyn_cast<BranchInst>(PushNumTeamsCallInst->getNextNonDebugInstruction());
ASSERT_NE(BrInst, nullptr);
ASSERT_EQ(BrInst->getNumSuccessors(), 1U);
Instruction *NextInstruction =
BasicBlock::iterator NextInstruction =
BrInst->getSuccessor(0)->getFirstNonPHIOrDbgOrLifetime();
CallInst *ForkTeamsCI = dyn_cast_if_present<CallInst>(NextInstruction);
CallInst *ForkTeamsCI = nullptr;
if (NextInstruction != BrInst->getSuccessor(0)->end())
ForkTeamsCI = dyn_cast_if_present<CallInst>(NextInstruction);
ASSERT_NE(ForkTeamsCI, nullptr);
EXPECT_EQ(ForkTeamsCI->getCalledFunction(),
OMPBuilder.getOrCreateRuntimeFunctionPtr(OMPRTL___kmpc_fork_teams));
Expand Down Expand Up @@ -4698,9 +4700,11 @@ TEST_F(OpenMPIRBuilderTest, CreateTeamsWithNumTeamsUpper) {
dyn_cast<BranchInst>(PushNumTeamsCallInst->getNextNonDebugInstruction());
ASSERT_NE(BrInst, nullptr);
ASSERT_EQ(BrInst->getNumSuccessors(), 1U);
Instruction *NextInstruction =
BasicBlock::iterator NextInstruction =
BrInst->getSuccessor(0)->getFirstNonPHIOrDbgOrLifetime();
CallInst *ForkTeamsCI = dyn_cast_if_present<CallInst>(NextInstruction);
CallInst *ForkTeamsCI = nullptr;
if (NextInstruction != BrInst->getSuccessor(0)->end())
ForkTeamsCI = dyn_cast_if_present<CallInst>(NextInstruction);
ASSERT_NE(ForkTeamsCI, nullptr);
EXPECT_EQ(ForkTeamsCI->getCalledFunction(),
OMPBuilder.getOrCreateRuntimeFunctionPtr(OMPRTL___kmpc_fork_teams));
Expand Down Expand Up @@ -4756,9 +4760,11 @@ TEST_F(OpenMPIRBuilderTest, CreateTeamsWithNumTeamsBoth) {
dyn_cast<BranchInst>(PushNumTeamsCallInst->getNextNonDebugInstruction());
ASSERT_NE(BrInst, nullptr);
ASSERT_EQ(BrInst->getNumSuccessors(), 1U);
Instruction *NextInstruction =
BasicBlock::iterator NextInstruction =
BrInst->getSuccessor(0)->getFirstNonPHIOrDbgOrLifetime();
CallInst *ForkTeamsCI = dyn_cast_if_present<CallInst>(NextInstruction);
CallInst *ForkTeamsCI = nullptr;
if (NextInstruction != BrInst->getSuccessor(0)->end())
ForkTeamsCI = dyn_cast_if_present<CallInst>(NextInstruction);
ASSERT_NE(ForkTeamsCI, nullptr);
EXPECT_EQ(ForkTeamsCI->getCalledFunction(),
OMPBuilder.getOrCreateRuntimeFunctionPtr(OMPRTL___kmpc_fork_teams));
Expand Down Expand Up @@ -4820,9 +4826,11 @@ TEST_F(OpenMPIRBuilderTest, CreateTeamsWithNumTeamsAndThreadLimit) {
dyn_cast<BranchInst>(PushNumTeamsCallInst->getNextNonDebugInstruction());
ASSERT_NE(BrInst, nullptr);
ASSERT_EQ(BrInst->getNumSuccessors(), 1U);
Instruction *NextInstruction =
BasicBlock::iterator NextInstruction =
BrInst->getSuccessor(0)->getFirstNonPHIOrDbgOrLifetime();
CallInst *ForkTeamsCI = dyn_cast_if_present<CallInst>(NextInstruction);
CallInst *ForkTeamsCI = nullptr;
if (NextInstruction != BrInst->getSuccessor(0)->end())
ForkTeamsCI = dyn_cast_if_present<CallInst>(NextInstruction);
ASSERT_NE(ForkTeamsCI, nullptr);
EXPECT_EQ(ForkTeamsCI->getCalledFunction(),
OMPBuilder.getOrCreateRuntimeFunctionPtr(OMPRTL___kmpc_fork_teams));
Expand Down
Loading