Skip to content

Commit 6292a80

Browse files
jmorseSLTozer
andauthored
[NFC][DebugInfo] Use iterator-flavour getFirstNonPHI at many call-sites (#123737)
As part of the "RemoveDIs" project, BasicBlock::iterator now carries a debug-info bit that's needed when getFirstNonPHI and similar feed into instruction insertion positions. Call-sites where that's necessary were updated a year ago; but to ensure some type safety however, we'd like to have all calls to getFirstNonPHI use the iterator-returning version. This patch changes a bunch of call-sites calling getFirstNonPHI to use getFirstNonPHIIt, which returns an iterator. All these call sites are where it's obviously safe to fetch the iterator then dereference it. A follow-up patch will contain less-obviously-safe changes. We'll eventually deprecate and remove the instruction-pointer getFirstNonPHI, but not before adding concise documentation of what considerations are needed (very few). --------- Co-authored-by: Stephen Tozer <[email protected]>
1 parent a5cc897 commit 6292a80

File tree

64 files changed

+217
-200
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

64 files changed

+217
-200
lines changed

clang/lib/CodeGen/CGException.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1251,11 +1251,12 @@ void CodeGenFunction::ExitCXXTryStmt(const CXXTryStmt &S, bool IsFnTryBlock) {
12511251
llvm::BasicBlock *WasmCatchStartBlock = nullptr;
12521252
if (EHPersonality::get(*this).isWasmPersonality()) {
12531253
auto *CatchSwitch =
1254-
cast<llvm::CatchSwitchInst>(DispatchBlock->getFirstNonPHI());
1254+
cast<llvm::CatchSwitchInst>(DispatchBlock->getFirstNonPHIIt());
12551255
WasmCatchStartBlock = CatchSwitch->hasUnwindDest()
12561256
? CatchSwitch->getSuccessor(1)
12571257
: CatchSwitch->getSuccessor(0);
1258-
auto *CPI = cast<llvm::CatchPadInst>(WasmCatchStartBlock->getFirstNonPHI());
1258+
auto *CPI =
1259+
cast<llvm::CatchPadInst>(WasmCatchStartBlock->getFirstNonPHIIt());
12591260
CurrentFuncletPad = CPI;
12601261
}
12611262

@@ -2252,7 +2253,7 @@ void CodeGenFunction::ExitSEHTryStmt(const SEHTryStmt &S) {
22522253
// __except blocks don't get outlined into funclets, so immediately do a
22532254
// catchret.
22542255
llvm::CatchPadInst *CPI =
2255-
cast<llvm::CatchPadInst>(CatchPadBB->getFirstNonPHI());
2256+
cast<llvm::CatchPadInst>(CatchPadBB->getFirstNonPHIIt());
22562257
llvm::BasicBlock *ExceptBB = createBasicBlock("__except");
22572258
Builder.CreateCatchRet(CPI, ExceptBB);
22582259
EmitBlock(ExceptBB);

clang/lib/CodeGen/MicrosoftCXXABI.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -918,7 +918,7 @@ void MicrosoftCXXABI::emitBeginCatch(CodeGenFunction &CGF,
918918
VarDecl *CatchParam = S->getExceptionDecl();
919919
llvm::BasicBlock *CatchPadBB = CGF.Builder.GetInsertBlock();
920920
llvm::CatchPadInst *CPI =
921-
cast<llvm::CatchPadInst>(CatchPadBB->getFirstNonPHI());
921+
cast<llvm::CatchPadInst>(CatchPadBB->getFirstNonPHIIt());
922922
CGF.CurrentFuncletPad = CPI;
923923

924924
// If this is a catch-all or the catch parameter is unnamed, we don't need to

llvm/include/llvm/IR/BasicBlock.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -673,7 +673,7 @@ class BasicBlock final : public Value, // Basic blocks are data objects also
673673
void replaceSuccessorsPhiUsesWith(BasicBlock *New);
674674

675675
/// Return true if this basic block is an exception handling block.
676-
bool isEHPad() const { return getFirstNonPHI()->isEHPad(); }
676+
bool isEHPad() const { return getFirstNonPHIIt()->isEHPad(); }
677677

678678
/// Return true if this basic block is a landing pad.
679679
///

llvm/include/llvm/Transforms/Utils/Instrumentation.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,11 @@ struct InstrumentationIRBuilder : IRBuilder<> {
204204
explicit InstrumentationIRBuilder(Instruction *IP) : IRBuilder<>(IP) {
205205
ensureDebugInfo(*this, *IP->getFunction());
206206
}
207+
208+
explicit InstrumentationIRBuilder(BasicBlock *BB, BasicBlock::iterator It)
209+
: IRBuilder<>(BB, It) {
210+
ensureDebugInfo(*this, *BB->getParent());
211+
}
207212
};
208213
} // end namespace llvm
209214

llvm/lib/Analysis/Loads.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,7 @@ bool llvm::isDereferenceableAndAlignedInLoop(
284284
DL.getTypeStoreSize(LI->getType()).getFixedValue());
285285
const Align Alignment = LI->getAlign();
286286

287-
Instruction *HeaderFirstNonPHI = L->getHeader()->getFirstNonPHI();
287+
Instruction *HeaderFirstNonPHI = &*L->getHeader()->getFirstNonPHIIt();
288288

289289
// If given a uniform (i.e. non-varying) address, see if we can prove the
290290
// access is safe within the loop w/o needing predication.

llvm/lib/Analysis/LoopNestAnalysis.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -346,7 +346,7 @@ static bool checkLoopsStructure(const Loop &OuterLoop, const Loop &InnerLoop,
346346
// "guarded" inner loop which contains "only" Phi nodes corresponding to the
347347
// LCSSA Phi nodes in the exit block.
348348
auto IsExtraPhiBlock = [&](const BasicBlock &BB) {
349-
return BB.getFirstNonPHI() == BB.getTerminator() &&
349+
return &*BB.getFirstNonPHIIt() == BB.getTerminator() &&
350350
all_of(BB.phis(), [&](const PHINode &PN) {
351351
return all_of(PN.blocks(), [&](const BasicBlock *IncomingBlock) {
352352
return IncomingBlock == InnerLoopExit ||

llvm/lib/Analysis/MustExecute.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,7 @@ bool SimpleLoopSafetyInfo::isGuaranteedToExecute(const Instruction &Inst,
275275
// exit. At the moment, we use a (cheap) hack for the common case where
276276
// the instruction of interest is the first one in the block.
277277
return !HeaderMayThrow ||
278-
Inst.getParent()->getFirstNonPHIOrDbg() == &Inst;
278+
&*Inst.getParent()->getFirstNonPHIOrDbg() == &Inst;
279279

280280
// If there is a path from header to exit or latch that doesn't lead to our
281281
// instruction's block, return false.

llvm/lib/Analysis/ValueTracking.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8246,7 +8246,7 @@ static bool programUndefinedIfUndefOrPoison(const Value *V,
82468246
if (!BB || !Visited.insert(BB).second)
82478247
break;
82488248

8249-
Begin = BB->getFirstNonPHI()->getIterator();
8249+
Begin = BB->getFirstNonPHIIt();
82508250
End = BB->end();
82518251
}
82528252
return false;

llvm/lib/CodeGen/AsmPrinter/WinException.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -928,8 +928,8 @@ void WinException::computeIP2StateTable(
928928
BaseState = NullState;
929929
StartLabel = Asm->getFunctionBegin();
930930
} else {
931-
auto *FuncletPad =
932-
cast<FuncletPadInst>(FuncletStart->getBasicBlock()->getFirstNonPHI());
931+
auto *FuncletPad = cast<FuncletPadInst>(
932+
FuncletStart->getBasicBlock()->getFirstNonPHIIt());
933933
assert(FuncInfo.FuncletBaseStateMap.count(FuncletPad) != 0);
934934
BaseState = FuncInfo.FuncletBaseStateMap.find(FuncletPad)->second;
935935
StartLabel = getMCSymbolForMBB(Asm, &*FuncletStart);

llvm/lib/CodeGen/GlobalISel/IRTranslator.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2866,7 +2866,7 @@ bool IRTranslator::findUnwindDestinations(
28662866
}
28672867

28682868
while (EHPadBB) {
2869-
const Instruction *Pad = EHPadBB->getFirstNonPHI();
2869+
BasicBlock::const_iterator Pad = EHPadBB->getFirstNonPHIIt();
28702870
BasicBlock *NewEHPadBB = nullptr;
28712871
if (isa<LandingPadInst>(Pad)) {
28722872
// Stop on landingpads. They are not funclets.
@@ -2927,7 +2927,7 @@ bool IRTranslator::translateInvoke(const User &U,
29272927
return false;
29282928

29292929
// FIXME: support Windows exception handling.
2930-
if (!isa<LandingPadInst>(EHPadBB->getFirstNonPHI()))
2930+
if (!isa<LandingPadInst>(EHPadBB->getFirstNonPHIIt()))
29312931
return false;
29322932

29332933
// FIXME: support Windows dllimport function calls and calls through
@@ -4031,7 +4031,7 @@ bool IRTranslator::runOnMachineFunction(MachineFunction &CurMF) {
40314031
MF->push_back(EntryBB);
40324032
EntryBuilder->setMBB(*EntryBB);
40334033

4034-
DebugLoc DbgLoc = F.getEntryBlock().getFirstNonPHI()->getDebugLoc();
4034+
DebugLoc DbgLoc = F.getEntryBlock().getFirstNonPHIIt()->getDebugLoc();
40354035
SwiftError.setFunction(CurMF);
40364036
SwiftError.createEntriesInEntryBlock(DbgLoc);
40374037

0 commit comments

Comments
 (0)