Skip to content

Commit 81d18ad

Browse files
authored
[NFC][DebugInfo] Make some block-start-position methods return iterators (llvm#124287)
As part of the "RemoveDIs" work to eliminate debug intrinsics, we're replacing methods that use Instruction*'s as positions with iterators. A number of these (such as getFirstNonPHIOrDbg) are sufficiently infrequently used that we can just replace the pointer-returning version with an iterator-returning version, hopefully without much/any disruption. Thus this patch has getFirstNonPHIOrDbg and getFirstNonPHIOrDbgOrLifetime return an iterator, and updates all call-sites. There are no concerns about the iterators returned being converted to Instruction*'s and losing the debug-info bit: because the methods skip debug intrinsics, the iterator head bit is always false anyway.
1 parent 5f5cdf4 commit 81d18ad

File tree

19 files changed

+74
-50
lines changed

19 files changed

+74
-50
lines changed

lldb/source/Plugins/ExpressionParser/Clang/IRForTarget.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ static llvm::Value *FindEntryInstruction(llvm::Function *function) {
6666
if (function->empty())
6767
return nullptr;
6868

69-
return function->getEntryBlock().getFirstNonPHIOrDbg();
69+
return &*function->getEntryBlock().getFirstNonPHIOrDbg();
7070
}
7171

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

363363
BasicBlock &entry_block(llvm_function.getEntryBlock());
364-
Instruction *first_entry_instruction(entry_block.getFirstNonPHIOrDbg());
364+
Instruction *first_entry_instruction(&*entry_block.getFirstNonPHIOrDbg());
365365

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

15071507
BasicBlock &entry_block(llvm_function.getEntryBlock());
1508-
Instruction *FirstEntryInstruction(entry_block.getFirstNonPHIOrDbg());
1508+
Instruction *FirstEntryInstruction(&*entry_block.getFirstNonPHIOrDbg());
15091509

15101510
if (!FirstEntryInstruction) {
15111511
m_error_stream.Printf("Internal error [IRForTarget]: Couldn't find the "

llvm/include/llvm/IR/BasicBlock.h

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -299,22 +299,24 @@ class BasicBlock final : public Value, // Basic blocks are data objects also
299299
/// Returns a pointer to the first instruction in this block that is not a
300300
/// PHINode or a debug intrinsic, or any pseudo operation if \c SkipPseudoOp
301301
/// is true.
302-
const Instruction *getFirstNonPHIOrDbg(bool SkipPseudoOp = true) const;
303-
Instruction *getFirstNonPHIOrDbg(bool SkipPseudoOp = true) {
304-
return const_cast<Instruction *>(
305-
static_cast<const BasicBlock *>(this)->getFirstNonPHIOrDbg(
306-
SkipPseudoOp));
302+
InstListType::const_iterator
303+
getFirstNonPHIOrDbg(bool SkipPseudoOp = true) const;
304+
InstListType::iterator getFirstNonPHIOrDbg(bool SkipPseudoOp = true) {
305+
return static_cast<const BasicBlock *>(this)
306+
->getFirstNonPHIOrDbg(SkipPseudoOp)
307+
.getNonConst();
307308
}
308309

309310
/// Returns a pointer to the first instruction in this block that is not a
310311
/// PHINode, a debug intrinsic, or a lifetime intrinsic, or any pseudo
311312
/// operation if \c SkipPseudoOp is true.
312-
const Instruction *
313+
InstListType::const_iterator
313314
getFirstNonPHIOrDbgOrLifetime(bool SkipPseudoOp = true) const;
314-
Instruction *getFirstNonPHIOrDbgOrLifetime(bool SkipPseudoOp = true) {
315-
return const_cast<Instruction *>(
316-
static_cast<const BasicBlock *>(this)->getFirstNonPHIOrDbgOrLifetime(
317-
SkipPseudoOp));
315+
InstListType::iterator
316+
getFirstNonPHIOrDbgOrLifetime(bool SkipPseudoOp = true) {
317+
return static_cast<const BasicBlock *>(this)
318+
->getFirstNonPHIOrDbgOrLifetime(SkipPseudoOp)
319+
.getNonConst();
318320
}
319321

320322
/// Returns an iterator to the first instruction in this block that is

llvm/lib/CodeGen/CodeGenPrepare.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -990,7 +990,7 @@ bool CodeGenPrepare::isMergingEmptyBlockProfitable(BasicBlock *BB,
990990
isa<IndirectBrInst>(Pred->getTerminator())))
991991
return true;
992992

993-
if (BB->getTerminator() != BB->getFirstNonPHIOrDbg())
993+
if (BB->getTerminator() != &*BB->getFirstNonPHIOrDbg())
994994
return true;
995995

996996
// We use a simple cost heuristic which determine skipping merging is

llvm/lib/IR/BasicBlock.cpp

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -383,20 +383,25 @@ BasicBlock::const_iterator BasicBlock::getFirstNonPHIIt() const {
383383
return It;
384384
}
385385

386-
const Instruction *BasicBlock::getFirstNonPHIOrDbg(bool SkipPseudoOp) const {
386+
BasicBlock::const_iterator
387+
BasicBlock::getFirstNonPHIOrDbg(bool SkipPseudoOp) const {
387388
for (const Instruction &I : *this) {
388389
if (isa<PHINode>(I) || isa<DbgInfoIntrinsic>(I))
389390
continue;
390391

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

394-
return &I;
395+
BasicBlock::const_iterator It = I.getIterator();
396+
// This position comes after any debug records, the head bit should remain
397+
// unset.
398+
assert(!It.getHeadBit());
399+
return It;
395400
}
396-
return nullptr;
401+
return end();
397402
}
398403

399-
const Instruction *
404+
BasicBlock::const_iterator
400405
BasicBlock::getFirstNonPHIOrDbgOrLifetime(bool SkipPseudoOp) const {
401406
for (const Instruction &I : *this) {
402407
if (isa<PHINode>(I) || isa<DbgInfoIntrinsic>(I))
@@ -408,9 +413,14 @@ BasicBlock::getFirstNonPHIOrDbgOrLifetime(bool SkipPseudoOp) const {
408413
if (SkipPseudoOp && isa<PseudoProbeInst>(I))
409414
continue;
410415

411-
return &I;
416+
BasicBlock::const_iterator It = I.getIterator();
417+
// This position comes after any debug records, the head bit should remain
418+
// unset.
419+
assert(!It.getHeadBit());
420+
421+
return It;
412422
}
413-
return nullptr;
423+
return end();
414424
}
415425

416426
BasicBlock::const_iterator BasicBlock::getFirstInsertionPt() const {
@@ -428,11 +438,10 @@ BasicBlock::const_iterator BasicBlock::getFirstInsertionPt() const {
428438
}
429439

430440
BasicBlock::const_iterator BasicBlock::getFirstNonPHIOrDbgOrAlloca() const {
431-
const Instruction *FirstNonPHI = getFirstNonPHI();
432-
if (!FirstNonPHI)
441+
const_iterator InsertPt = getFirstNonPHIIt();
442+
if (InsertPt == end())
433443
return end();
434444

435-
const_iterator InsertPt = FirstNonPHI->getIterator();
436445
if (InsertPt->isEHPad())
437446
++InsertPt;
438447

@@ -448,6 +457,9 @@ BasicBlock::const_iterator BasicBlock::getFirstNonPHIOrDbgOrAlloca() const {
448457
++InsertPt;
449458
}
450459
}
460+
461+
// Signal that this comes after any debug records.
462+
InsertPt.setHeadBit(false);
451463
return InsertPt;
452464
}
453465

llvm/lib/Target/AArch64/AArch64TargetTransformInfo.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2188,7 +2188,7 @@ static std::optional<Instruction *> instCombineDMB(InstCombiner &IC,
21882188
NI = NI->getNextNonDebugInstruction();
21892189
if (!NI) {
21902190
if (auto *SuccBB = NIBB->getUniqueSuccessor())
2191-
NI = SuccBB->getFirstNonPHIOrDbgOrLifetime();
2191+
NI = &*SuccBB->getFirstNonPHIOrDbgOrLifetime();
21922192
else
21932193
break;
21942194
}

llvm/lib/Target/AMDGPU/SIAnnotateControlFlow.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ Value *SIAnnotateControlFlow::handleLoopCondition(
232232
} else if (L->contains(Inst)) {
233233
Insert = Term;
234234
} else {
235-
Insert = L->getHeader()->getFirstNonPHIOrDbgOrLifetime();
235+
Insert = &*L->getHeader()->getFirstNonPHIOrDbgOrLifetime();
236236
}
237237

238238
return CreateBreak(Insert);
@@ -247,7 +247,7 @@ Value *SIAnnotateControlFlow::handleLoopCondition(
247247
}
248248

249249
if (isa<Argument>(Cond)) {
250-
Instruction *Insert = L->getHeader()->getFirstNonPHIOrDbgOrLifetime();
250+
Instruction *Insert = &*L->getHeader()->getFirstNonPHIOrDbgOrLifetime();
251251
return CreateBreak(Insert);
252252
}
253253

llvm/lib/Target/NVPTX/NVPTXGenericToNVVM.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ bool GenericToNVVM::runOnModule(Module &M) {
8686
if (F.isDeclaration()) {
8787
continue;
8888
}
89-
IRBuilder<> Builder(F.getEntryBlock().getFirstNonPHIOrDbg());
89+
IRBuilder<> Builder(&*F.getEntryBlock().getFirstNonPHIOrDbg());
9090
for (BasicBlock &BB : F) {
9191
for (Instruction &II : BB) {
9292
for (unsigned i = 0, e = II.getNumOperands(); i < e; ++i) {

llvm/lib/Transforms/Coroutines/CoroFrame.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1697,7 +1697,8 @@ static void eliminateSwiftErrorAlloca(Function &F, AllocaInst *Alloca,
16971697
static void eliminateSwiftErrorArgument(Function &F, Argument &Arg,
16981698
coro::Shape &Shape,
16991699
SmallVectorImpl<AllocaInst*> &AllocasToPromote) {
1700-
IRBuilder<> Builder(F.getEntryBlock().getFirstNonPHIOrDbg());
1700+
IRBuilder<> Builder(&F.getEntryBlock(),
1701+
F.getEntryBlock().getFirstNonPHIOrDbg());
17011702

17021703
auto ArgTy = cast<PointerType>(Arg.getType());
17031704
auto ValueTy = PointerType::getUnqual(F.getContext());

llvm/lib/Transforms/Coroutines/CoroSplit.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -597,7 +597,8 @@ static void replaceSwiftErrorOps(Function &F, coro::Shape &Shape,
597597
}
598598

599599
// Create a swifterror alloca.
600-
IRBuilder<> Builder(F.getEntryBlock().getFirstNonPHIOrDbg());
600+
IRBuilder<> Builder(&F.getEntryBlock(),
601+
F.getEntryBlock().getFirstNonPHIOrDbg());
601602
auto Alloca = Builder.CreateAlloca(ValueTy);
602603
Alloca->setSwiftError(true);
603604

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

833834
// Find the first successor of ActiveSuspend with a non-zero line location.
834835
// If that matches the file of ActiveSuspend, use it.

llvm/lib/Transforms/IPO/IROutliner.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ Value *OutlinableRegion::findCorrespondingValueIn(const OutlinableRegion &Other,
197197
BasicBlock *
198198
OutlinableRegion::findCorrespondingBlockIn(const OutlinableRegion &Other,
199199
BasicBlock *BB) {
200-
Instruction *FirstNonPHI = BB->getFirstNonPHIOrDbg();
200+
Instruction *FirstNonPHI = &*BB->getFirstNonPHIOrDbg();
201201
assert(FirstNonPHI && "block is empty?");
202202
Value *CorrespondingVal = findCorrespondingValueIn(Other, FirstNonPHI);
203203
if (!CorrespondingVal)

0 commit comments

Comments
 (0)