Skip to content

Commit e14962a

Browse files
authored
[NFC][DebugInfo] Use iterators for instruction insertion in more places (#124291)
As part of the "RemoveDIs" work to eliminate debug intrinsics, we're replacing methods that use Instruction*'s as positions with iterators. This patch changes some more complex call-sites, those crossing file boundaries and where I've had to perform some minor rewrites.
1 parent 5aafc6d commit e14962a

File tree

10 files changed

+52
-47
lines changed

10 files changed

+52
-47
lines changed

clang/lib/CodeGen/CodeGenFunction.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -451,7 +451,7 @@ class CodeGenFunction : public CodeGenTypeCache {
451451
"EBB should be entry block of the current code gen function");
452452
PostAllocaInsertPt = AllocaInsertPt->clone();
453453
PostAllocaInsertPt->setName("postallocapt");
454-
PostAllocaInsertPt->insertAfter(AllocaInsertPt);
454+
PostAllocaInsertPt->insertAfter(AllocaInsertPt->getIterator());
455455
}
456456

457457
return PostAllocaInsertPt;

llvm/include/llvm/Transforms/Utils/BasicBlockUtils.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -540,7 +540,7 @@ inline void SplitBlockAndInsertIfThenElse(Value *Cond, Instruction *SplitBefore,
540540
/// SplitBefore. Returns the first insert point in the loop body, and the
541541
/// PHINode for the induction variable (i.e. "i" above).
542542
std::pair<Instruction*, Value*>
543-
SplitBlockAndInsertSimpleForLoop(Value *End, Instruction *SplitBefore);
543+
SplitBlockAndInsertSimpleForLoop(Value *End, BasicBlock::iterator SplitBefore);
544544

545545
/// Utility function for performing a given action on each lane of a vector
546546
/// with \p EC elements. To simplify porting legacy code, this defaults to
@@ -550,9 +550,9 @@ SplitBlockAndInsertSimpleForLoop(Value *End, Instruction *SplitBefore);
550550
/// IRBuilder whose insert point is correctly set for instantiating the
551551
/// given index, and a value which is (at runtime) the index to access.
552552
/// This index *may* be a constant.
553-
void SplitBlockAndInsertForEachLane(ElementCount EC, Type *IndexTy,
554-
Instruction *InsertBefore,
555-
std::function<void(IRBuilderBase&, Value*)> Func);
553+
void SplitBlockAndInsertForEachLane(
554+
ElementCount EC, Type *IndexTy, BasicBlock::iterator InsertBefore,
555+
std::function<void(IRBuilderBase &, Value *)> Func);
556556

557557
/// Utility function for performing a given action on each lane of a vector
558558
/// with \p EVL effective length. EVL is assumed > 0. To simplify porting legacy
@@ -563,7 +563,7 @@ void SplitBlockAndInsertForEachLane(ElementCount EC, Type *IndexTy,
563563
/// the given index, and a value which is (at runtime) the index to access. This
564564
/// index *may* be a constant.
565565
void SplitBlockAndInsertForEachLane(
566-
Value *End, Instruction *InsertBefore,
566+
Value *End, BasicBlock::iterator InsertBefore,
567567
std::function<void(IRBuilderBase &, Value *)> Func);
568568

569569
/// Check whether BB is the merge point of a if-region.

llvm/lib/CodeGen/CodeGenPrepare.cpp

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2935,13 +2935,13 @@ bool CodeGenPrepare::dupRetToEnableTailCallOpts(BasicBlock *BB,
29352935

29362936
// Make sure there are no instructions between the first instruction
29372937
// and return.
2938-
const Instruction *BI = BB->getFirstNonPHI();
2938+
BasicBlock::const_iterator BI = BB->getFirstNonPHIIt();
29392939
// Skip over debug and the bitcast.
2940-
while (isa<DbgInfoIntrinsic>(BI) || BI == BCI || BI == EVI ||
2941-
isa<PseudoProbeInst>(BI) || isLifetimeEndOrBitCastFor(BI) ||
2942-
isFakeUse(BI))
2943-
BI = BI->getNextNode();
2944-
if (BI != RetI)
2940+
while (isa<DbgInfoIntrinsic>(BI) || &*BI == BCI || &*BI == EVI ||
2941+
isa<PseudoProbeInst>(BI) || isLifetimeEndOrBitCastFor(&*BI) ||
2942+
isFakeUse(&*BI))
2943+
BI = std::next(BI);
2944+
if (&*BI != RetI)
29452945
return false;
29462946

29472947
/// Only dup the ReturnInst if the CallInst is likely to be emitted as a tail
@@ -3265,8 +3265,8 @@ class TypePromotionTransaction {
32653265
/// Either an instruction:
32663266
/// - Is the first in a basic block: BB is used.
32673267
/// - Has a previous instruction: PrevInst is used.
3268-
union {
3269-
Instruction *PrevInst;
3268+
struct {
3269+
BasicBlock::iterator PrevInst;
32703270
BasicBlock *BB;
32713271
} Point;
32723272
std::optional<DbgRecord::self_iterator> BeforeDbgRecord = std::nullopt;
@@ -3286,7 +3286,7 @@ class TypePromotionTransaction {
32863286
BeforeDbgRecord = Inst->getDbgReinsertionPosition();
32873287

32883288
if (HasPrevInstruction) {
3289-
Point.PrevInst = &*std::prev(Inst->getIterator());
3289+
Point.PrevInst = std::prev(Inst->getIterator());
32903290
} else {
32913291
Point.BB = BB;
32923292
}
@@ -3297,7 +3297,7 @@ class TypePromotionTransaction {
32973297
if (HasPrevInstruction) {
32983298
if (Inst->getParent())
32993299
Inst->removeFromParent();
3300-
Inst->insertAfter(&*Point.PrevInst);
3300+
Inst->insertAfter(Point.PrevInst);
33013301
} else {
33023302
BasicBlock::iterator Position = Point.BB->getFirstInsertionPt();
33033303
if (Inst->getParent())
@@ -3317,7 +3317,7 @@ class TypePromotionTransaction {
33173317

33183318
public:
33193319
/// Move \p Inst before \p Before.
3320-
InstructionMoveBefore(Instruction *Inst, Instruction *Before)
3320+
InstructionMoveBefore(Instruction *Inst, BasicBlock::iterator Before)
33213321
: TypePromotionAction(Inst), Position(Inst) {
33223322
LLVM_DEBUG(dbgs() << "Do: move: " << *Inst << "\nbefore: " << *Before
33233323
<< "\n");

llvm/lib/Target/Hexagon/HexagonVectorCombine.cpp

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -342,7 +342,8 @@ class AlignVectors {
342342
MoveList createLoadGroups(const AddrList &Group) const;
343343
MoveList createStoreGroups(const AddrList &Group) const;
344344
bool moveTogether(MoveGroup &Move) const;
345-
template <typename T> InstMap cloneBefore(Instruction *To, T &&Insts) const;
345+
template <typename T>
346+
InstMap cloneBefore(BasicBlock::iterator To, T &&Insts) const;
346347

347348
void realignLoadGroup(IRBuilderBase &Builder, const ByteSpan &VSpan,
348349
int ScLen, Value *AlignVal, Value *AlignAddr) const;
@@ -1046,7 +1047,7 @@ auto AlignVectors::moveTogether(MoveGroup &Move) const -> bool {
10461047
if (Move.IsLoad) {
10471048
// Move all the loads (and dependencies) to where the first load is.
10481049
// Clone all deps to before Where, keeping order.
1049-
Move.Clones = cloneBefore(Where, Move.Deps);
1050+
Move.Clones = cloneBefore(Where->getIterator(), Move.Deps);
10501051
// Move all main instructions to after Where, keeping order.
10511052
ArrayRef<Instruction *> Main(Move.Main);
10521053
for (Instruction *M : Main) {
@@ -1067,7 +1068,7 @@ auto AlignVectors::moveTogether(MoveGroup &Move) const -> bool {
10671068
// Move all main instructions to before Where, inverting order.
10681069
ArrayRef<Instruction *> Main(Move.Main);
10691070
for (Instruction *M : Main.drop_front(1)) {
1070-
M->moveBefore(Where);
1071+
M->moveBefore(Where->getIterator());
10711072
Where = M;
10721073
}
10731074
}
@@ -1076,7 +1077,8 @@ auto AlignVectors::moveTogether(MoveGroup &Move) const -> bool {
10761077
}
10771078

10781079
template <typename T>
1079-
auto AlignVectors::cloneBefore(Instruction *To, T &&Insts) const -> InstMap {
1080+
auto AlignVectors::cloneBefore(BasicBlock::iterator To, T &&Insts) const
1081+
-> InstMap {
10801082
InstMap Map;
10811083

10821084
for (Instruction *I : Insts) {
@@ -1200,10 +1202,10 @@ auto AlignVectors::realignLoadGroup(IRBuilderBase &Builder,
12001202
VSpan.section(Start, Width).values());
12011203
};
12021204

1203-
auto moveBefore = [this](Instruction *In, Instruction *To) {
1205+
auto moveBefore = [this](BasicBlock::iterator In, BasicBlock::iterator To) {
12041206
// Move In and its upward dependencies to before To.
12051207
assert(In->getParent() == To->getParent());
1206-
DepList Deps = getUpwardDeps(In, To);
1208+
DepList Deps = getUpwardDeps(&*In, &*To);
12071209
In->moveBefore(To);
12081210
// DepList is sorted with respect to positions in the basic block.
12091211
InstMap Map = cloneBefore(In, Deps);
@@ -1236,7 +1238,7 @@ auto AlignVectors::realignLoadGroup(IRBuilderBase &Builder,
12361238
// in order to check legality.
12371239
if (auto *Load = dyn_cast<Instruction>(Loads[Index])) {
12381240
if (!HVC.isSafeToMoveBeforeInBB(*Load, BasePos))
1239-
moveBefore(Load, &*BasePos);
1241+
moveBefore(Load->getIterator(), BasePos);
12401242
}
12411243
LLVM_DEBUG(dbgs() << "Loads[" << Index << "]:" << *Loads[Index] << '\n');
12421244
}

llvm/lib/Transforms/Coroutines/CoroSplit.cpp

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1221,8 +1221,8 @@ static void handleNoSuspendCoroutine(coro::Shape &Shape) {
12211221
// SimplifySuspendPoint needs to check that there is no calls between
12221222
// coro_save and coro_suspend, since any of the calls may potentially resume
12231223
// the coroutine and if that is the case we cannot eliminate the suspend point.
1224-
static bool hasCallsInBlockBetween(Instruction *From, Instruction *To) {
1225-
for (Instruction *I = From; I != To; I = I->getNextNode()) {
1224+
static bool hasCallsInBlockBetween(iterator_range<BasicBlock::iterator> R) {
1225+
for (Instruction &I : R) {
12261226
// Assume that no intrinsic can resume the coroutine.
12271227
if (isa<IntrinsicInst>(I))
12281228
continue;
@@ -1256,7 +1256,7 @@ static bool hasCallsInBlocksBetween(BasicBlock *SaveBB, BasicBlock *ResDesBB) {
12561256
Set.erase(ResDesBB);
12571257

12581258
for (auto *BB : Set)
1259-
if (hasCallsInBlockBetween(BB->getFirstNonPHI(), nullptr))
1259+
if (hasCallsInBlockBetween({BB->getFirstNonPHIIt(), BB->end()}))
12601260
return true;
12611261

12621262
return false;
@@ -1265,17 +1265,19 @@ static bool hasCallsInBlocksBetween(BasicBlock *SaveBB, BasicBlock *ResDesBB) {
12651265
static bool hasCallsBetween(Instruction *Save, Instruction *ResumeOrDestroy) {
12661266
auto *SaveBB = Save->getParent();
12671267
auto *ResumeOrDestroyBB = ResumeOrDestroy->getParent();
1268+
BasicBlock::iterator SaveIt = Save->getIterator();
1269+
BasicBlock::iterator ResumeOrDestroyIt = ResumeOrDestroy->getIterator();
12681270

12691271
if (SaveBB == ResumeOrDestroyBB)
1270-
return hasCallsInBlockBetween(Save->getNextNode(), ResumeOrDestroy);
1272+
return hasCallsInBlockBetween({std::next(SaveIt), ResumeOrDestroyIt});
12711273

12721274
// Any calls from Save to the end of the block?
1273-
if (hasCallsInBlockBetween(Save->getNextNode(), nullptr))
1275+
if (hasCallsInBlockBetween({std::next(SaveIt), SaveBB->end()}))
12741276
return true;
12751277

12761278
// Any calls from begging of the block up to ResumeOrDestroy?
1277-
if (hasCallsInBlockBetween(ResumeOrDestroyBB->getFirstNonPHI(),
1278-
ResumeOrDestroy))
1279+
if (hasCallsInBlockBetween(
1280+
{ResumeOrDestroyBB->getFirstNonPHIIt(), ResumeOrDestroyIt}))
12791281
return true;
12801282

12811283
// Any calls in all of the blocks between SaveBB and ResumeOrDestroyBB?

llvm/lib/Transforms/Instrumentation/AddressSanitizer.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1661,7 +1661,7 @@ void AddressSanitizer::instrumentMaskedLoadOrStore(
16611661
if (Stride)
16621662
Stride = IB.CreateZExtOrTrunc(Stride, IntptrTy);
16631663

1664-
SplitBlockAndInsertForEachLane(EVL, LoopInsertBefore,
1664+
SplitBlockAndInsertForEachLane(EVL, LoopInsertBefore->getIterator(),
16651665
[&](IRBuilderBase &IRB, Value *Index) {
16661666
Value *MaskElem = IRB.CreateExtractElement(Mask, Index);
16671667
if (auto *MaskElemC = dyn_cast<ConstantInt>(MaskElem)) {

llvm/lib/Transforms/Instrumentation/MemorySanitizer.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1272,7 +1272,7 @@ struct MemorySanitizerVisitor : public InstVisitor<MemorySanitizerVisitor> {
12721272
Value *End =
12731273
IRB.CreateUDiv(RoundUp, ConstantInt::get(MS.IntptrTy, kOriginSize));
12741274
auto [InsertPt, Index] =
1275-
SplitBlockAndInsertSimpleForLoop(End, &*IRB.GetInsertPoint());
1275+
SplitBlockAndInsertSimpleForLoop(End, IRB.GetInsertPoint());
12761276
IRB.SetInsertPoint(InsertPt);
12771277

12781278
Value *GEP = IRB.CreateGEP(MS.OriginTy, OriginPtr, Index);

llvm/lib/Transforms/Scalar/LoopStrengthReduce.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6138,7 +6138,7 @@ void LSRInstance::ImplementSolution(
61386138
if (!llvm::all_of(BO->uses(),
61396139
[&](Use &U) {return DT.dominates(IVIncInsertPos, U);}))
61406140
continue;
6141-
BO->moveBefore(IVIncInsertPos);
6141+
BO->moveBefore(IVIncInsertPos->getIterator());
61426142
Changed = true;
61436143
}
61446144

llvm/lib/Transforms/Utils/BasicBlockUtils.cpp

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1728,8 +1728,9 @@ void llvm::SplitBlockAndInsertIfThenElse(
17281728
}
17291729
}
17301730

1731-
std::pair<Instruction*, Value*>
1732-
llvm::SplitBlockAndInsertSimpleForLoop(Value *End, Instruction *SplitBefore) {
1731+
std::pair<Instruction *, Value *>
1732+
llvm::SplitBlockAndInsertSimpleForLoop(Value *End,
1733+
BasicBlock::iterator SplitBefore) {
17331734
BasicBlock *LoopPred = SplitBefore->getParent();
17341735
BasicBlock *LoopBody = SplitBlock(SplitBefore->getParent(), SplitBefore);
17351736
BasicBlock *LoopExit = SplitBlock(SplitBefore->getParent(), SplitBefore);
@@ -1752,14 +1753,14 @@ llvm::SplitBlockAndInsertSimpleForLoop(Value *End, Instruction *SplitBefore) {
17521753
IV->addIncoming(ConstantInt::get(Ty, 0), LoopPred);
17531754
IV->addIncoming(IVNext, LoopBody);
17541755

1755-
return std::make_pair(LoopBody->getFirstNonPHI(), IV);
1756+
return std::make_pair(&*LoopBody->getFirstNonPHIIt(), IV);
17561757
}
17571758

1758-
void llvm::SplitBlockAndInsertForEachLane(ElementCount EC,
1759-
Type *IndexTy, Instruction *InsertBefore,
1760-
std::function<void(IRBuilderBase&, Value*)> Func) {
1759+
void llvm::SplitBlockAndInsertForEachLane(
1760+
ElementCount EC, Type *IndexTy, BasicBlock::iterator InsertBefore,
1761+
std::function<void(IRBuilderBase &, Value *)> Func) {
17611762

1762-
IRBuilder<> IRB(InsertBefore);
1763+
IRBuilder<> IRB(InsertBefore->getParent(), InsertBefore);
17631764

17641765
if (EC.isScalable()) {
17651766
Value *NumElements = IRB.CreateElementCount(IndexTy, EC);
@@ -1780,10 +1781,10 @@ void llvm::SplitBlockAndInsertForEachLane(ElementCount EC,
17801781
}
17811782

17821783
void llvm::SplitBlockAndInsertForEachLane(
1783-
Value *EVL, Instruction *InsertBefore,
1784+
Value *EVL, BasicBlock::iterator InsertBefore,
17841785
std::function<void(IRBuilderBase &, Value *)> Func) {
17851786

1786-
IRBuilder<> IRB(InsertBefore);
1787+
IRBuilder<> IRB(InsertBefore->getParent(), InsertBefore);
17871788
Type *Ty = EVL->getType();
17881789

17891790
if (!isa<ConstantInt>(EVL)) {

llvm/lib/Transforms/Utils/InlineFunction.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -184,14 +184,14 @@ namespace {
184184
} // end anonymous namespace
185185

186186
static IntrinsicInst *getConvergenceEntry(BasicBlock &BB) {
187-
auto *I = BB.getFirstNonPHI();
188-
while (I) {
189-
if (auto *IntrinsicCall = dyn_cast<ConvergenceControlInst>(I)) {
187+
BasicBlock::iterator It = BB.getFirstNonPHIIt();
188+
while (It != BB.end()) {
189+
if (auto *IntrinsicCall = dyn_cast<ConvergenceControlInst>(It)) {
190190
if (IntrinsicCall->isEntry()) {
191191
return IntrinsicCall;
192192
}
193193
}
194-
I = I->getNextNode();
194+
It = std::next(It);
195195
}
196196
return nullptr;
197197
}

0 commit comments

Comments
 (0)