Skip to content

Commit 1d82c76

Browse files
committed
[NFC][RemoveDIs] Provide an iterator-taking split-block method
As per the stack of patches this is attached to, allow users of BasicBlock::splitBasicBlock to provide an iterator for a position, instead of just an instruction pointer. This is to fit with my proposal for how to get rid of debug intrinsics [0]. There are other call-sites that would need to change, but this is sufficient for a stage2clang self host and some other C++ projects to build identical binaries, in the context of the whole remove-DIs project. [0] https://discourse.llvm.org/t/rfc-instruction-api-changes-needed-to-eliminate-debug-intrinsics-from-ir/68939 Differential Revision: https://reviews.llvm.org/D152545
1 parent ccef726 commit 1d82c76

File tree

4 files changed

+89
-23
lines changed

4 files changed

+89
-23
lines changed

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

Lines changed: 71 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -280,10 +280,16 @@ BasicBlock *ehAwareSplitEdge(BasicBlock *BB, BasicBlock *Succ,
280280
/// branch. The new block with name \p BBName is returned.
281281
///
282282
/// FIXME: deprecated, switch to the DomTreeUpdater-based one.
283-
BasicBlock *SplitBlock(BasicBlock *Old, Instruction *SplitPt, DominatorTree *DT,
283+
BasicBlock *SplitBlock(BasicBlock *Old, BasicBlock::iterator SplitPt, DominatorTree *DT,
284284
LoopInfo *LI = nullptr,
285285
MemorySSAUpdater *MSSAU = nullptr,
286286
const Twine &BBName = "", bool Before = false);
287+
inline BasicBlock *SplitBlock(BasicBlock *Old, Instruction *SplitPt, DominatorTree *DT,
288+
LoopInfo *LI = nullptr,
289+
MemorySSAUpdater *MSSAU = nullptr,
290+
const Twine &BBName = "", bool Before = false) {
291+
return SplitBlock(Old, SplitPt->getIterator(), DT, LI, MSSAU, BBName, Before);
292+
}
287293

288294
/// Split the specified block at the specified instruction.
289295
///
@@ -293,19 +299,30 @@ BasicBlock *SplitBlock(BasicBlock *Old, Instruction *SplitPt, DominatorTree *DT,
293299
/// Everything before \p SplitPt stays in \p Old and everything starting with \p
294300
/// SplitPt moves to a new block. The two blocks are joined by an unconditional
295301
/// branch. The new block with name \p BBName is returned.
296-
BasicBlock *SplitBlock(BasicBlock *Old, Instruction *SplitPt,
302+
BasicBlock *SplitBlock(BasicBlock *Old, BasicBlock::iterator SplitPt,
297303
DomTreeUpdater *DTU = nullptr, LoopInfo *LI = nullptr,
298304
MemorySSAUpdater *MSSAU = nullptr,
299305
const Twine &BBName = "", bool Before = false);
306+
inline BasicBlock *SplitBlock(BasicBlock *Old, Instruction *SplitPt,
307+
DomTreeUpdater *DTU = nullptr, LoopInfo *LI = nullptr,
308+
MemorySSAUpdater *MSSAU = nullptr,
309+
const Twine &BBName = "", bool Before = false) {
310+
return SplitBlock(Old, SplitPt->getIterator(), DTU, LI, MSSAU, BBName, Before);
311+
}
300312

301313
/// Split the specified block at the specified instruction \p SplitPt.
302314
/// All instructions before \p SplitPt are moved to a new block and all
303315
/// instructions after \p SplitPt stay in the old block. The new block and the
304316
/// old block are joined by inserting an unconditional branch to the end of the
305317
/// new block. The new block with name \p BBName is returned.
306-
BasicBlock *splitBlockBefore(BasicBlock *Old, Instruction *SplitPt,
318+
BasicBlock *splitBlockBefore(BasicBlock *Old, BasicBlock::iterator SplitPt,
307319
DomTreeUpdater *DTU, LoopInfo *LI,
308320
MemorySSAUpdater *MSSAU, const Twine &BBName = "");
321+
inline BasicBlock *splitBlockBefore(BasicBlock *Old, Instruction *SplitPt,
322+
DomTreeUpdater *DTU, LoopInfo *LI,
323+
MemorySSAUpdater *MSSAU, const Twine &BBName = "") {
324+
return splitBlockBefore(Old, SplitPt->getIterator(), DTU, LI, MSSAU, BBName);
325+
}
309326

310327
/// This method introduces at least one new basic block into the function and
311328
/// moves some of the predecessors of BB to be predecessors of the new block.
@@ -417,22 +434,44 @@ ReturnInst *FoldReturnIntoUncondBranch(ReturnInst *RI, BasicBlock *BB,
417434
/// Returns the NewBasicBlock's terminator.
418435
///
419436
/// Updates DTU and LI if given.
420-
Instruction *SplitBlockAndInsertIfThen(Value *Cond, Instruction *SplitBefore,
437+
Instruction *SplitBlockAndInsertIfThen(Value *Cond, BasicBlock::iterator SplitBefore,
421438
bool Unreachable,
422439
MDNode *BranchWeights = nullptr,
423440
DomTreeUpdater *DTU = nullptr,
424441
LoopInfo *LI = nullptr,
425442
BasicBlock *ThenBlock = nullptr);
426443

444+
inline Instruction *SplitBlockAndInsertIfThen(Value *Cond, Instruction *SplitBefore,
445+
bool Unreachable,
446+
MDNode *BranchWeights = nullptr,
447+
DomTreeUpdater *DTU = nullptr,
448+
LoopInfo *LI = nullptr,
449+
BasicBlock *ThenBlock = nullptr) {
450+
return SplitBlockAndInsertIfThen(Cond, SplitBefore->getIterator(),
451+
Unreachable, BranchWeights, DTU, LI,
452+
ThenBlock);
453+
}
454+
427455
/// Similar to SplitBlockAndInsertIfThen, but the inserted block is on the false
428456
/// path of the branch.
429-
Instruction *SplitBlockAndInsertIfElse(Value *Cond, Instruction *SplitBefore,
457+
Instruction *SplitBlockAndInsertIfElse(Value *Cond, BasicBlock::iterator SplitBefore,
430458
bool Unreachable,
431459
MDNode *BranchWeights = nullptr,
432460
DomTreeUpdater *DTU = nullptr,
433461
LoopInfo *LI = nullptr,
434462
BasicBlock *ElseBlock = nullptr);
435463

464+
inline Instruction *SplitBlockAndInsertIfElse(Value *Cond, Instruction *SplitBefore,
465+
bool Unreachable,
466+
MDNode *BranchWeights = nullptr,
467+
DomTreeUpdater *DTU = nullptr,
468+
LoopInfo *LI = nullptr,
469+
BasicBlock *ElseBlock = nullptr) {
470+
return SplitBlockAndInsertIfElse(Cond, SplitBefore->getIterator(),
471+
Unreachable, BranchWeights, DTU, LI,
472+
ElseBlock);
473+
}
474+
436475
/// SplitBlockAndInsertIfThenElse is similar to SplitBlockAndInsertIfThen,
437476
/// but also creates the ElseBlock.
438477
/// Before:
@@ -449,13 +488,25 @@ Instruction *SplitBlockAndInsertIfElse(Value *Cond, Instruction *SplitBefore,
449488
/// Tail
450489
///
451490
/// Updates DT if given.
452-
void SplitBlockAndInsertIfThenElse(Value *Cond, Instruction *SplitBefore,
491+
void SplitBlockAndInsertIfThenElse(Value *Cond,
492+
BasicBlock::iterator SplitBefore,
453493
Instruction **ThenTerm,
454494
Instruction **ElseTerm,
455495
MDNode *BranchWeights = nullptr,
456496
DomTreeUpdater *DTU = nullptr,
457497
LoopInfo *LI = nullptr);
458498

499+
inline void SplitBlockAndInsertIfThenElse(Value *Cond, Instruction *SplitBefore,
500+
Instruction **ThenTerm,
501+
Instruction **ElseTerm,
502+
MDNode *BranchWeights = nullptr,
503+
DomTreeUpdater *DTU = nullptr,
504+
LoopInfo *LI = nullptr)
505+
{
506+
SplitBlockAndInsertIfThenElse(Cond, SplitBefore->getIterator(), ThenTerm,
507+
ElseTerm, BranchWeights, DTU, LI);
508+
}
509+
459510
/// Split the containing block at the specified instruction - everything before
460511
/// SplitBefore stays in the old basic block, and the rest of the instructions
461512
/// in the BB are moved to a new block. The two blocks are connected by a
@@ -483,7 +534,8 @@ void SplitBlockAndInsertIfThenElse(Value *Cond, Instruction *SplitBefore,
483534
/// caller must ensure that Tail is reachable from Head.
484535
/// Returns the newly created blocks in \p ThenBlock and \p ElseBlock.
485536
/// Updates DTU and LI if given.
486-
void SplitBlockAndInsertIfThenElse(Value *Cond, Instruction *SplitBefore,
537+
void SplitBlockAndInsertIfThenElse(Value *Cond,
538+
BasicBlock::iterator SplitBefore,
487539
BasicBlock **ThenBlock,
488540
BasicBlock **ElseBlock,
489541
bool UnreachableThen = false,
@@ -492,6 +544,18 @@ void SplitBlockAndInsertIfThenElse(Value *Cond, Instruction *SplitBefore,
492544
DomTreeUpdater *DTU = nullptr,
493545
LoopInfo *LI = nullptr);
494546

547+
inline void SplitBlockAndInsertIfThenElse(Value *Cond, Instruction *SplitBefore,
548+
BasicBlock **ThenBlock,
549+
BasicBlock **ElseBlock,
550+
bool UnreachableThen = false,
551+
bool UnreachableElse = false,
552+
MDNode *BranchWeights = nullptr,
553+
DomTreeUpdater *DTU = nullptr,
554+
LoopInfo *LI = nullptr) {
555+
SplitBlockAndInsertIfThenElse(Cond, SplitBefore->getIterator(), ThenBlock,
556+
ElseBlock, UnreachableThen, UnreachableElse, BranchWeights, DTU, LI);
557+
}
558+
495559
/// Insert a for (int i = 0; i < End; i++) loop structure (with the exception
496560
/// that \p End is assumed > 0, and thus not checked on entry) at \p
497561
/// SplitBefore. Returns the first insert point in the loop body, and the

llvm/lib/Transforms/Scalar/SimpleLoopUnswitch.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -610,7 +610,7 @@ static bool unswitchTrivialBranch(Loop &L, BranchInst &BI, DominatorTree &DT,
610610
UnswitchedBB = LoopExitBB;
611611
} else {
612612
UnswitchedBB =
613-
SplitBlock(LoopExitBB, &LoopExitBB->front(), &DT, &LI, MSSAU);
613+
SplitBlock(LoopExitBB, LoopExitBB->begin(), &DT, &LI, MSSAU, "", false);
614614
}
615615

616616
if (MSSAU && VerifyMemorySSA)
@@ -883,7 +883,7 @@ static bool unswitchTrivialSwitch(Loop &L, SwitchInst &SI, DominatorTree &DT,
883883
rewritePHINodesForUnswitchedExitBlock(*DefaultExitBB, *ParentBB, *OldPH);
884884
} else {
885885
auto *SplitBB =
886-
SplitBlock(DefaultExitBB, &DefaultExitBB->front(), &DT, &LI, MSSAU);
886+
SplitBlock(DefaultExitBB, DefaultExitBB->begin(), &DT, &LI, MSSAU);
887887
rewritePHINodesForExitAndUnswitchedBlocks(*DefaultExitBB, *SplitBB,
888888
*ParentBB, *OldPH,
889889
/*FullUnswitch*/ true);
@@ -910,7 +910,7 @@ static bool unswitchTrivialSwitch(Loop &L, SwitchInst &SI, DominatorTree &DT,
910910
BasicBlock *&SplitExitBB = SplitExitBBMap[ExitBB];
911911
if (!SplitExitBB) {
912912
// If this is the first time we see this, do the split and remember it.
913-
SplitExitBB = SplitBlock(ExitBB, &ExitBB->front(), &DT, &LI, MSSAU);
913+
SplitExitBB = SplitBlock(ExitBB, ExitBB->begin(), &DT, &LI, MSSAU);
914914
rewritePHINodesForExitAndUnswitchedBlocks(*ExitBB, *SplitExitBB,
915915
*ParentBB, *OldPH,
916916
/*FullUnswitch*/ true);
@@ -1211,7 +1211,7 @@ static BasicBlock *buildClonedLoopBlocks(
12111211
// place to merge the CFG, so split the exit first. This is always safe to
12121212
// do because there cannot be any non-loop predecessors of a loop exit in
12131213
// loop simplified form.
1214-
auto *MergeBB = SplitBlock(ExitBB, &ExitBB->front(), &DT, &LI, MSSAU);
1214+
auto *MergeBB = SplitBlock(ExitBB, ExitBB->begin(), &DT, &LI, MSSAU);
12151215

12161216
// Rearrange the names to make it easier to write test cases by having the
12171217
// exit block carry the suffix rather than the merge block carrying the

llvm/lib/Transforms/Utils/BasicBlockUtils.cpp

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -879,7 +879,7 @@ llvm::SplitAllCriticalEdges(Function &F,
879879
return NumBroken;
880880
}
881881

882-
static BasicBlock *SplitBlockImpl(BasicBlock *Old, Instruction *SplitPt,
882+
static BasicBlock *SplitBlockImpl(BasicBlock *Old, BasicBlock::iterator SplitPt,
883883
DomTreeUpdater *DTU, DominatorTree *DT,
884884
LoopInfo *LI, MemorySSAUpdater *MSSAU,
885885
const Twine &BBName, bool Before) {
@@ -889,7 +889,7 @@ static BasicBlock *SplitBlockImpl(BasicBlock *Old, Instruction *SplitPt,
889889
DTU ? DTU : (DT ? &LocalDTU : nullptr), LI, MSSAU,
890890
BBName);
891891
}
892-
BasicBlock::iterator SplitIt = SplitPt->getIterator();
892+
BasicBlock::iterator SplitIt = SplitPt;
893893
while (isa<PHINode>(SplitIt) || SplitIt->isEHPad()) {
894894
++SplitIt;
895895
assert(SplitIt != SplitPt->getParent()->end());
@@ -935,27 +935,27 @@ static BasicBlock *SplitBlockImpl(BasicBlock *Old, Instruction *SplitPt,
935935
return New;
936936
}
937937

938-
BasicBlock *llvm::SplitBlock(BasicBlock *Old, Instruction *SplitPt,
938+
BasicBlock *llvm::SplitBlock(BasicBlock *Old, BasicBlock::iterator SplitPt,
939939
DominatorTree *DT, LoopInfo *LI,
940940
MemorySSAUpdater *MSSAU, const Twine &BBName,
941941
bool Before) {
942942
return SplitBlockImpl(Old, SplitPt, /*DTU=*/nullptr, DT, LI, MSSAU, BBName,
943943
Before);
944944
}
945-
BasicBlock *llvm::SplitBlock(BasicBlock *Old, Instruction *SplitPt,
945+
BasicBlock *llvm::SplitBlock(BasicBlock *Old, BasicBlock::iterator SplitPt,
946946
DomTreeUpdater *DTU, LoopInfo *LI,
947947
MemorySSAUpdater *MSSAU, const Twine &BBName,
948948
bool Before) {
949949
return SplitBlockImpl(Old, SplitPt, DTU, /*DT=*/nullptr, LI, MSSAU, BBName,
950950
Before);
951951
}
952952

953-
BasicBlock *llvm::splitBlockBefore(BasicBlock *Old, Instruction *SplitPt,
953+
BasicBlock *llvm::splitBlockBefore(BasicBlock *Old, BasicBlock::iterator SplitPt,
954954
DomTreeUpdater *DTU, LoopInfo *LI,
955955
MemorySSAUpdater *MSSAU,
956956
const Twine &BBName) {
957957

958-
BasicBlock::iterator SplitIt = SplitPt->getIterator();
958+
BasicBlock::iterator SplitIt = SplitPt;
959959
while (isa<PHINode>(SplitIt) || SplitIt->isEHPad())
960960
++SplitIt;
961961
std::string Name = BBName.str();
@@ -1471,7 +1471,7 @@ ReturnInst *llvm::FoldReturnIntoUncondBranch(ReturnInst *RI, BasicBlock *BB,
14711471
}
14721472

14731473
Instruction *llvm::SplitBlockAndInsertIfThen(Value *Cond,
1474-
Instruction *SplitBefore,
1474+
BasicBlock::iterator SplitBefore,
14751475
bool Unreachable,
14761476
MDNode *BranchWeights,
14771477
DomTreeUpdater *DTU, LoopInfo *LI,
@@ -1484,7 +1484,7 @@ Instruction *llvm::SplitBlockAndInsertIfThen(Value *Cond,
14841484
}
14851485

14861486
Instruction *llvm::SplitBlockAndInsertIfElse(Value *Cond,
1487-
Instruction *SplitBefore,
1487+
BasicBlock::iterator SplitBefore,
14881488
bool Unreachable,
14891489
MDNode *BranchWeights,
14901490
DomTreeUpdater *DTU, LoopInfo *LI,
@@ -1496,7 +1496,7 @@ Instruction *llvm::SplitBlockAndInsertIfElse(Value *Cond,
14961496
return ElseBlock->getTerminator();
14971497
}
14981498

1499-
void llvm::SplitBlockAndInsertIfThenElse(Value *Cond, Instruction *SplitBefore,
1499+
void llvm::SplitBlockAndInsertIfThenElse(Value *Cond, BasicBlock::iterator SplitBefore,
15001500
Instruction **ThenTerm,
15011501
Instruction **ElseTerm,
15021502
MDNode *BranchWeights,
@@ -1512,7 +1512,7 @@ void llvm::SplitBlockAndInsertIfThenElse(Value *Cond, Instruction *SplitBefore,
15121512
}
15131513

15141514
void llvm::SplitBlockAndInsertIfThenElse(
1515-
Value *Cond, Instruction *SplitBefore, BasicBlock **ThenBlock,
1515+
Value *Cond, BasicBlock::iterator SplitBefore, BasicBlock **ThenBlock,
15161516
BasicBlock **ElseBlock, bool UnreachableThen, bool UnreachableElse,
15171517
MDNode *BranchWeights, DomTreeUpdater *DTU, LoopInfo *LI) {
15181518
assert((ThenBlock || ElseBlock) &&
@@ -1529,7 +1529,7 @@ void llvm::SplitBlockAndInsertIfThenElse(
15291529
}
15301530

15311531
LLVMContext &C = Head->getContext();
1532-
BasicBlock *Tail = Head->splitBasicBlock(SplitBefore->getIterator());
1532+
BasicBlock *Tail = Head->splitBasicBlock(SplitBefore);
15331533
BasicBlock *TrueBlock = Tail;
15341534
BasicBlock *FalseBlock = Tail;
15351535
bool ThenToTailEdge = false;

llvm/lib/Transforms/Utils/SimplifyCFG.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4015,9 +4015,11 @@ static bool mergeConditionalStoreToAddress(
40154015
QPred = QB.CreateNot(QPred);
40164016
Value *CombinedPred = QB.CreateOr(PPred, QPred);
40174017

4018-
auto *T = SplitBlockAndInsertIfThen(CombinedPred, &*QB.GetInsertPoint(),
4018+
BasicBlock::iterator InsertPt = QB.GetInsertPoint();
4019+
auto *T = SplitBlockAndInsertIfThen(CombinedPred, InsertPt,
40194020
/*Unreachable=*/false,
40204021
/*BranchWeights=*/nullptr, DTU);
4022+
40214023
QB.SetInsertPoint(T);
40224024
StoreInst *SI = cast<StoreInst>(QB.CreateStore(QPHI, Address));
40234025
SI->setAAMetadata(PStore->getAAMetadata().merge(QStore->getAAMetadata()));

0 commit comments

Comments
 (0)