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

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

llvm/lib/CodeGen/GlobalMerge.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -633,7 +633,7 @@ void GlobalMergeImpl::setMustKeepGlobalVariables(Module &M) {
633633

634634
for (Function &F : M) {
635635
for (BasicBlock &BB : F) {
636-
Instruction *Pad = BB.getFirstNonPHI();
636+
BasicBlock::iterator Pad = BB.getFirstNonPHIIt();
637637
auto *II = dyn_cast<IntrinsicInst>(Pad);
638638
if (!Pad->isEHPad() &&
639639
!(II && II->getIntrinsicID() == Intrinsic::eh_typeid_for))

llvm/lib/CodeGen/MachineFunction.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -833,7 +833,8 @@ MCSymbol *MachineFunction::addLandingPad(MachineBasicBlock *LandingPad) {
833833
LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
834834
LP.LandingPadLabel = LandingPadLabel;
835835

836-
const Instruction *FirstI = LandingPad->getBasicBlock()->getFirstNonPHI();
836+
BasicBlock::const_iterator FirstI =
837+
LandingPad->getBasicBlock()->getFirstNonPHIIt();
837838
if (const auto *LPI = dyn_cast<LandingPadInst>(FirstI)) {
838839
// If there's no typeid list specified, then "cleanup" is implicit.
839840
// Otherwise, id 0 is reserved for the cleanup action.

llvm/lib/CodeGen/SelectOptimize.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1217,7 +1217,7 @@ bool SelectOptimizeImpl::checkLoopHeuristics(const Loop *L,
12171217
return true;
12181218

12191219
OptimizationRemarkMissed ORmissL(DEBUG_TYPE, "SelectOpti",
1220-
L->getHeader()->getFirstNonPHI());
1220+
&*L->getHeader()->getFirstNonPHIIt());
12211221

12221222
if (LoopCost[0].NonPredCost > LoopCost[0].PredCost ||
12231223
LoopCost[1].NonPredCost >= LoopCost[1].PredCost) {

llvm/lib/CodeGen/SelectionDAG/FunctionLoweringInfo.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ void FunctionLoweringInfo::set(const Function &fn, MachineFunction &mf,
250250
// Don't create MachineBasicBlocks for imaginary EH pad blocks. These blocks
251251
// are really data, and no instructions can live here.
252252
if (BB.isEHPad()) {
253-
const Instruction *PadInst = BB.getFirstNonPHI();
253+
BasicBlock::const_iterator PadInst = BB.getFirstNonPHIIt();
254254
// If this is a non-landingpad EH pad, mark this function as using
255255
// funclets.
256256
// FIXME: SEH catchpads do not create EH scope/funclets, so we could avoid
@@ -261,13 +261,13 @@ void FunctionLoweringInfo::set(const Function &fn, MachineFunction &mf,
261261
MF->getFrameInfo().setHasOpaqueSPAdjustment(true);
262262
}
263263
if (isa<CatchSwitchInst>(PadInst)) {
264-
assert(&*BB.begin() == PadInst &&
264+
assert(BB.begin() == PadInst &&
265265
"WinEHPrepare failed to remove PHIs from imaginary BBs");
266266
continue;
267267
}
268268
if (isa<FuncletPadInst>(PadInst) &&
269269
Personality != EHPersonality::Wasm_CXX)
270-
assert(&*BB.begin() == PadInst && "WinEHPrepare failed to demote PHIs");
270+
assert(BB.begin() == PadInst && "WinEHPrepare failed to demote PHIs");
271271
}
272272

273273
MachineBasicBlock *MBB = mf.CreateMachineBasicBlock(&BB);

llvm/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2063,7 +2063,7 @@ static void findWasmUnwindDestinations(
20632063
SmallVectorImpl<std::pair<MachineBasicBlock *, BranchProbability>>
20642064
&UnwindDests) {
20652065
while (EHPadBB) {
2066-
const Instruction *Pad = EHPadBB->getFirstNonPHI();
2066+
BasicBlock::const_iterator Pad = EHPadBB->getFirstNonPHIIt();
20672067
if (isa<CleanupPadInst>(Pad)) {
20682068
// Stop on cleanup pads.
20692069
UnwindDests.emplace_back(FuncInfo.getMBB(EHPadBB), Prob);
@@ -2111,7 +2111,7 @@ static void findUnwindDestinations(
21112111
}
21122112

21132113
while (EHPadBB) {
2114-
const Instruction *Pad = EHPadBB->getFirstNonPHI();
2114+
BasicBlock::const_iterator Pad = EHPadBB->getFirstNonPHIIt();
21152115
BasicBlock *NewEHPadBB = nullptr;
21162116
if (isa<LandingPadInst>(Pad)) {
21172117
// Stop on landingpads. They are not funclets.

llvm/lib/CodeGen/SelectionDAG/SelectionDAGISel.cpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1421,7 +1421,7 @@ bool SelectionDAGISel::PrepareEHLandingPad() {
14211421
// Catchpads have one live-in register, which typically holds the exception
14221422
// pointer or code.
14231423
if (isFuncletEHPersonality(Pers)) {
1424-
if (const auto *CPI = dyn_cast<CatchPadInst>(LLVMBB->getFirstNonPHI())) {
1424+
if (const auto *CPI = dyn_cast<CatchPadInst>(LLVMBB->getFirstNonPHIIt())) {
14251425
if (hasExceptionPointerOrCodeUser(CPI)) {
14261426
// Get or create the virtual register to hold the pointer or code. Mark
14271427
// the live in physreg and copy into the vreg.
@@ -1452,7 +1452,7 @@ bool SelectionDAGISel::PrepareEHLandingPad() {
14521452
MF->getRegInfo().addPhysRegsUsedFromRegMask(RegMask);
14531453

14541454
if (Pers == EHPersonality::Wasm_CXX) {
1455-
if (const auto *CPI = dyn_cast<CatchPadInst>(LLVMBB->getFirstNonPHI()))
1455+
if (const auto *CPI = dyn_cast<CatchPadInst>(LLVMBB->getFirstNonPHIIt()))
14561456
mapWasmLandingPadIndex(MBB, CPI);
14571457
} else {
14581458
// Assign the call site to the landing pad's begin label.
@@ -1721,13 +1721,12 @@ void SelectionDAGISel::SelectAllBasicBlocks(const Function &Fn) {
17211721
// use anything def'd by or after the tail call.
17221722
{
17231723
BasicBlock::iterator BBStart =
1724-
const_cast<BasicBlock *>(LLVMBB)->getFirstNonPHI()->getIterator();
1724+
const_cast<BasicBlock *>(LLVMBB)->getFirstNonPHIIt();
17251725
BasicBlock::iterator BBEnd = const_cast<BasicBlock *>(LLVMBB)->end();
17261726
preserveFakeUses(BBStart, BBEnd);
17271727
}
17281728

1729-
BasicBlock::const_iterator const Begin =
1730-
LLVMBB->getFirstNonPHI()->getIterator();
1729+
BasicBlock::const_iterator const Begin = LLVMBB->getFirstNonPHIIt();
17311730
BasicBlock::const_iterator const End = LLVMBB->end();
17321731
BasicBlock::const_iterator BI = End;
17331732

llvm/lib/CodeGen/WasmEHPrepare.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ bool WasmEHPrepareImpl::prepareEHPads(Function &F) {
227227
for (BasicBlock &BB : F) {
228228
if (!BB.isEHPad())
229229
continue;
230-
auto *Pad = BB.getFirstNonPHI();
230+
BasicBlock::iterator Pad = BB.getFirstNonPHIIt();
231231
if (isa<CatchPadInst>(Pad))
232232
CatchPads.push_back(&BB);
233233
else if (isa<CleanupPadInst>(Pad))
@@ -284,7 +284,7 @@ bool WasmEHPrepareImpl::prepareEHPads(Function &F) {
284284

285285
unsigned Index = 0;
286286
for (auto *BB : CatchPads) {
287-
auto *CPI = cast<CatchPadInst>(BB->getFirstNonPHI());
287+
auto *CPI = cast<CatchPadInst>(BB->getFirstNonPHIIt());
288288
// In case of a single catch (...), we don't need to emit a personalify
289289
// function call
290290
if (CPI->arg_size() == 1 &&
@@ -309,7 +309,7 @@ void WasmEHPrepareImpl::prepareEHPad(BasicBlock *BB, bool NeedPersonality,
309309
IRBuilder<> IRB(BB->getContext());
310310
IRB.SetInsertPoint(BB, BB->getFirstInsertionPt());
311311

312-
auto *FPI = cast<FuncletPadInst>(BB->getFirstNonPHI());
312+
auto *FPI = cast<FuncletPadInst>(BB->getFirstNonPHIIt());
313313
Instruction *GetExnCI = nullptr, *GetSelectorCI = nullptr;
314314
for (auto &U : FPI->uses()) {
315315
if (auto *CI = dyn_cast<CallInst>(U.getUser())) {
@@ -388,13 +388,13 @@ void llvm::calculateWasmEHInfo(const Function *F, WasmEHFuncInfo &EHInfo) {
388388
for (const auto &BB : *F) {
389389
if (!BB.isEHPad())
390390
continue;
391-
const Instruction *Pad = BB.getFirstNonPHI();
391+
const Instruction *Pad = &*BB.getFirstNonPHIIt();
392392

393393
if (const auto *CatchPad = dyn_cast<CatchPadInst>(Pad)) {
394394
const auto *UnwindBB = CatchPad->getCatchSwitch()->getUnwindDest();
395395
if (!UnwindBB)
396396
continue;
397-
const Instruction *UnwindPad = UnwindBB->getFirstNonPHI();
397+
const Instruction *UnwindPad = &*UnwindBB->getFirstNonPHIIt();
398398
if (const auto *CatchSwitch = dyn_cast<CatchSwitchInst>(UnwindPad))
399399
// Currently there should be only one handler per a catchswitch.
400400
EHInfo.setUnwindDest(&BB, *CatchSwitch->handlers().begin());

0 commit comments

Comments
 (0)