Skip to content

Commit

Permalink
[NFC] Cleanup: Replaces BB->getInstList().insert() with I->insertAt().
Browse files Browse the repository at this point in the history
This is part of a series of cleanup patches towards making BasicBlock::getInstList() private.

Differential Revision: https://reviews.llvm.org/D138877
  • Loading branch information
vporpo committed Dec 12, 2022
1 parent 816b5e5 commit 06911ba
Show file tree
Hide file tree
Showing 30 changed files with 66 additions and 67 deletions.
2 changes: 1 addition & 1 deletion clang/lib/CodeGen/CGCleanup.cpp
Expand Up @@ -942,7 +942,7 @@ void CodeGenFunction::PopCleanupBlock(bool FallthroughIsBranchThrough) {
// Append the prepared cleanup prologue from above.
llvm::BasicBlock *NormalExit = Builder.GetInsertBlock();
for (unsigned I = 0, E = InstsToAppend.size(); I != E; ++I)
NormalExit->getInstList().push_back(InstsToAppend[I]);
InstsToAppend[I]->insertAt(NormalExit, NormalExit->end());

// Optimistically hope that any fixups will continue falling through.
for (unsigned I = FixupDepth, E = EHStack.getNumBranchFixups();
Expand Down
3 changes: 2 additions & 1 deletion llvm/include/llvm/IR/IRBuilder.h
Expand Up @@ -65,7 +65,8 @@ class IRBuilderDefaultInserter {
virtual void InsertHelper(Instruction *I, const Twine &Name,
BasicBlock *BB,
BasicBlock::iterator InsertPt) const {
if (BB) BB->getInstList().insert(InsertPt, I);
if (BB)
I->insertAt(BB, InsertPt);
I->setName(Name);
}
};
Expand Down
2 changes: 1 addition & 1 deletion llvm/include/llvm/Transforms/InstCombine/InstCombiner.h
Expand Up @@ -397,7 +397,7 @@ class LLVM_LIBRARY_VISIBILITY InstCombiner {
assert(New && !New->getParent() &&
"New instruction already inserted into a basic block!");
BasicBlock *BB = Old.getParent();
BB->getInstList().insert(Old.getIterator(), New); // Insert inst
New->insertAt(BB, Old.getIterator()); // Insert inst
Worklist.push(New);
return New;
}
Expand Down
4 changes: 2 additions & 2 deletions llvm/include/llvm/Transforms/Utils/BasicBlockUtils.h
Expand Up @@ -121,8 +121,8 @@ void ReplaceInstWithValue(BasicBlock::InstListType &BIL,
/// Copies DebugLoc from BI to I, if I doesn't already have a DebugLoc. The
/// original instruction is deleted and BI is updated to point to the new
/// instruction.
void ReplaceInstWithInst(BasicBlock::InstListType &BIL,
BasicBlock::iterator &BI, Instruction *I);
void ReplaceInstWithInst(BasicBlock *BB, BasicBlock::iterator &BI,
Instruction *I);

/// Replace the instruction specified by From with the instruction specified by
/// To. Copies DebugLoc from BI to I, if I doesn't already have a DebugLoc.
Expand Down
4 changes: 2 additions & 2 deletions llvm/lib/AsmParser/LLParser.cpp
Expand Up @@ -6166,7 +6166,7 @@ bool LLParser::parseBasicBlock(PerFunctionState &PFS) {
llvm_unreachable("Unknown parseInstruction result!");
case InstError: return true;
case InstNormal:
BB->getInstList().push_back(Inst);
Inst->insertAt(BB, BB->end());

// With a normal result, we check to see if the instruction is followed by
// a comma and metadata.
Expand All @@ -6175,7 +6175,7 @@ bool LLParser::parseBasicBlock(PerFunctionState &PFS) {
return true;
break;
case InstExtraComma:
BB->getInstList().push_back(Inst);
Inst->insertAt(BB, BB->end());

// If the instruction parser ate an extra comma at the end of it, it
// *must* be followed by metadata.
Expand Down
6 changes: 3 additions & 3 deletions llvm/lib/Bitcode/Reader/BitcodeReader.cpp
Expand Up @@ -4840,7 +4840,7 @@ Error BitcodeReader::parseFunctionBody(Function *F) {
if (Temp) {
InstructionList.push_back(Temp);
assert(CurBB && "No current BB?");
CurBB->getInstList().push_back(Temp);
Temp->insertAt(CurBB, CurBB->end());
}
} else {
auto CastOp = (Instruction::CastOps)Opc;
Expand Down Expand Up @@ -6088,7 +6088,7 @@ Error BitcodeReader::parseFunctionBody(Function *F) {
// Before weak cmpxchgs existed, the instruction simply returned the
// value loaded from memory, so bitcode files from that era will be
// expecting the first component of a modern cmpxchg.
CurBB->getInstList().push_back(I);
I->insertAt(CurBB, CurBB->end());
I = ExtractValueInst::Create(I, 0);
ResTypeID = CmpTypeID;
} else {
Expand Down Expand Up @@ -6412,7 +6412,7 @@ Error BitcodeReader::parseFunctionBody(Function *F) {
I->deleteValue();
return error("Operand bundles found with no consumer");
}
CurBB->getInstList().push_back(I);
I->insertAt(CurBB, CurBB->end());

// If this was a terminator instruction, move to the next block.
if (I->isTerminator()) {
Expand Down
4 changes: 2 additions & 2 deletions llvm/lib/CodeGen/WinEHPrepare.cpp
Expand Up @@ -1212,8 +1212,8 @@ void WinEHPrepare::replaceUseWithLoad(Value *V, Use &U, AllocaInst *&SpillSlot,
BranchInst *Goto = cast<BranchInst>(IncomingBlock->getTerminator());
Goto->removeFromParent();
CatchRet->removeFromParent();
IncomingBlock->getInstList().push_back(CatchRet);
NewBlock->getInstList().push_back(Goto);
CatchRet->insertAt(IncomingBlock, IncomingBlock->end());
Goto->insertAt(NewBlock, NewBlock->end());
Goto->setSuccessor(0, PHIBlock);
CatchRet->setSuccessor(NewBlock);
// Update the color mapping for the newly split edge.
Expand Down
9 changes: 4 additions & 5 deletions llvm/lib/IR/Instruction.cpp
Expand Up @@ -28,7 +28,7 @@ Instruction::Instruction(Type *ty, unsigned it, Use *Ops, unsigned NumOps,
if (InsertBefore) {
BasicBlock *BB = InsertBefore->getParent();
assert(BB && "Instruction to insert before is not in a basic block!");
BB->getInstList().insert(InsertBefore->getIterator(), this);
insertAt(BB, InsertBefore->getIterator());
}
}

Expand All @@ -38,7 +38,7 @@ Instruction::Instruction(Type *ty, unsigned it, Use *Ops, unsigned NumOps,

// append this instruction into the basic block
assert(InsertAtEnd && "Basic block to append to may not be NULL!");
InsertAtEnd->getInstList().push_back(this);
insertAt(InsertAtEnd, InsertAtEnd->end());
}

Instruction::~Instruction() {
Expand Down Expand Up @@ -85,14 +85,13 @@ iplist<Instruction>::iterator Instruction::eraseFromParent() {
/// Insert an unlinked instruction into a basic block immediately before the
/// specified instruction.
void Instruction::insertBefore(Instruction *InsertPos) {
InsertPos->getParent()->getInstList().insert(InsertPos->getIterator(), this);
insertAt(InsertPos->getParent(), InsertPos->getIterator());
}

/// Insert an unlinked instruction into a basic block immediately after the
/// specified instruction.
void Instruction::insertAfter(Instruction *InsertPos) {
InsertPos->getParent()->getInstList().insertAfter(InsertPos->getIterator(),
this);
insertAt(InsertPos->getParent(), std::next(InsertPos->getIterator()));
}

BasicBlock::iterator Instruction::insertAt(BasicBlock *BB,
Expand Down
6 changes: 3 additions & 3 deletions llvm/lib/IR/Instructions.cpp
Expand Up @@ -824,7 +824,7 @@ static Instruction *createMalloc(Instruction *InsertBefore,
MCall = CallInst::Create(MallocFunc, AllocSize, OpB, "malloccall");
Result = MCall;
if (Result->getType() != AllocPtrType) {
InsertAtEnd->getInstList().push_back(MCall);
MCall->insertAt(InsertAtEnd, InsertAtEnd->end());
// Create a cast instruction to convert to the right type...
Result = new BitCastInst(MCall, AllocPtrType, Name);
}
Expand Down Expand Up @@ -2815,7 +2815,7 @@ UnaryOperator *UnaryOperator::Create(UnaryOps Op, Value *S,
const Twine &Name,
BasicBlock *InsertAtEnd) {
UnaryOperator *Res = Create(Op, S, Name);
InsertAtEnd->getInstList().push_back(Res);
Res->insertAt(InsertAtEnd, InsertAtEnd->end());
return Res;
}

Expand Down Expand Up @@ -2946,7 +2946,7 @@ BinaryOperator *BinaryOperator::Create(BinaryOps Op, Value *S1, Value *S2,
const Twine &Name,
BasicBlock *InsertAtEnd) {
BinaryOperator *Res = Create(Op, S1, S2, Name);
InsertAtEnd->getInstList().push_back(Res);
Res->insertAt(InsertAtEnd, InsertAtEnd->end());
return Res;
}

Expand Down
2 changes: 1 addition & 1 deletion llvm/lib/Transforms/IPO/IROutliner.cpp
Expand Up @@ -1872,7 +1872,7 @@ replaceArgumentUses(OutlinableRegion &Region,
StoreInst *NewI = cast<StoreInst>(I->clone());
NewI->setDebugLoc(DebugLoc());
BasicBlock *OutputBB = VBBIt->second;
OutputBB->getInstList().push_back(NewI);
NewI->insertAt(OutputBB, OutputBB->end());
LLVM_DEBUG(dbgs() << "Move store for instruction " << *I << " to "
<< *OutputBB << "\n");

Expand Down
2 changes: 1 addition & 1 deletion llvm/lib/Transforms/InstCombine/InstCombineInternal.h
Expand Up @@ -388,7 +388,7 @@ class LLVM_LIBRARY_VISIBILITY InstCombinerImpl final
assert(New && !New->getParent() &&
"New instruction already inserted into a basic block!");
BasicBlock *BB = Old.getParent();
BB->getInstList().insert(Old.getIterator(), New); // Insert inst
New->insertAt(BB, Old.getIterator()); // Insert inst
Worklist.add(New);
return New;
}
Expand Down
7 changes: 3 additions & 4 deletions llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
Expand Up @@ -2213,7 +2213,7 @@ Instruction *InstCombinerImpl::visitGEPOfBitcast(BitCastInst *BCI,
if (Instruction *I = visitBitCast(*BCI)) {
if (I != BCI) {
I->takeName(BCI);
BCI->getParent()->getInstList().insert(BCI->getIterator(), I);
I->insertAt(BCI->getParent(), BCI->getIterator());
replaceInstUsesWith(*BCI, I);
}
return &GEP;
Expand Down Expand Up @@ -2419,8 +2419,7 @@ Instruction *InstCombinerImpl::visitGetElementPtrInst(GetElementPtrInst &GEP) {
NewGEP->setOperand(DI, NewPN);
}

GEP.getParent()->getInstList().insert(
GEP.getParent()->getFirstInsertionPt(), NewGEP);
NewGEP->insertAt(GEP.getParent(), GEP.getParent()->getFirstInsertionPt());
replaceOperand(GEP, 0, NewGEP);
PtrOp = NewGEP;
}
Expand Down Expand Up @@ -4333,7 +4332,7 @@ bool InstCombinerImpl::run() {
InsertPos = InstParent->getFirstNonPHI()->getIterator();
}

InstParent->getInstList().insert(InsertPos, Result);
Result->insertAt(InstParent, InsertPos);

// Push the new instruction and any users onto the worklist.
Worklist.pushUsersToWorkList(*Result);
Expand Down
Expand Up @@ -1838,7 +1838,7 @@ BranchInst *CHR::createMergedBranch(BasicBlock *PreEntryBlock,
BranchInst *NewBR = BranchInst::Create(NewEntryBlock,
cast<BasicBlock>(VMap[NewEntryBlock]),
ConstantInt::getTrue(F.getContext()));
PreEntryBlock->getInstList().push_back(NewBR);
NewBR->insertAt(PreEntryBlock, PreEntryBlock->end());
assert(NewEntryBlock->getSinglePredecessor() == EntryBlock &&
"NewEntryBlock's only pred must be EntryBlock");
return NewBR;
Expand Down
2 changes: 1 addition & 1 deletion llvm/lib/Transforms/Instrumentation/PGOMemOPSizeOpt.cpp
Expand Up @@ -422,7 +422,7 @@ bool MemOPSizeOpt::perform(MemOp MO) {
assert(SizeType && "Expected integer type size argument.");
ConstantInt *CaseSizeId = ConstantInt::get(SizeType, SizeId);
NewMO.setLength(CaseSizeId);
CaseBB->getInstList().push_back(NewMO.I);
NewMO.I->insertAt(CaseBB, CaseBB->end());
IRBuilder<> IRBCase(CaseBB);
IRBCase.CreateBr(MergeBB);
SI->addCase(CaseSizeId, CaseBB);
Expand Down
6 changes: 3 additions & 3 deletions llvm/lib/Transforms/Scalar/JumpThreading.cpp
Expand Up @@ -2108,7 +2108,7 @@ JumpThreadingPass::cloneInstructions(BasicBlock::iterator BI,
for (; BI != BE; ++BI) {
Instruction *New = BI->clone();
New->setName(BI->getName());
NewBB->getInstList().push_back(New);
New->insertAt(NewBB, NewBB->end());
ValueMapping[&*BI] = New;
adaptNoAliasScopes(New, ClonedScopes, Context);

Expand Down Expand Up @@ -2701,7 +2701,7 @@ bool JumpThreadingPass::duplicateCondBranchOnPHIIntoPred(
if (New) {
// Otherwise, insert the new instruction into the block.
New->setName(BI->getName());
PredBB->getInstList().insert(OldPredBranch->getIterator(), New);
New->insertAt(PredBB, OldPredBranch->getIterator());
// Update Dominance from simplified New instruction operands.
for (unsigned i = 0, e = New->getNumOperands(); i != e; ++i)
if (BasicBlock *SuccBB = dyn_cast<BasicBlock>(New->getOperand(i)))
Expand Down Expand Up @@ -2755,7 +2755,7 @@ void JumpThreadingPass::unfoldSelectInstr(BasicBlock *Pred, BasicBlock *BB,
BB->getParent(), BB);
// Move the unconditional branch to NewBB.
PredTerm->removeFromParent();
NewBB->getInstList().insert(NewBB->end(), PredTerm);
PredTerm->insertAt(NewBB, NewBB->end());
// Create a conditional branch and update PHI nodes.
auto *BI = BranchInst::Create(NewBB, BB, SI->getCondition(), Pred);
BI->applyMergedLocation(PredTerm->getDebugLoc(), SI->getDebugLoc());
Expand Down
2 changes: 1 addition & 1 deletion llvm/lib/Transforms/Scalar/LICM.cpp
Expand Up @@ -1430,7 +1430,7 @@ static Instruction *cloneInstructionInExitBlock(
New = I.clone();
}

ExitBlock.getInstList().insert(ExitBlock.getFirstInsertionPt(), New);
New->insertAt(&ExitBlock, ExitBlock.getFirstInsertionPt());
if (!I.getName().empty())
New->setName(I.getName() + ".le");

Expand Down
2 changes: 1 addition & 1 deletion llvm/lib/Transforms/Scalar/LoopStrengthReduce.cpp
Expand Up @@ -2535,7 +2535,7 @@ LSRInstance::OptimizeLoopTermCond() {
ICmpInst *OldCond = Cond;
Cond = cast<ICmpInst>(Cond->clone());
Cond->setName(L->getHeader()->getName() + ".termcond");
ExitingBlock->getInstList().insert(TermBr->getIterator(), Cond);
Cond->insertAt(ExitingBlock, TermBr->getIterator());

// Clone the IVUse, as the old use still exists!
CondUse = &IU.AddUser(Cond, CondUse->getOperandValToReplace());
Expand Down
6 changes: 3 additions & 3 deletions llvm/lib/Transforms/Scalar/SimpleLoopUnswitch.cpp
Expand Up @@ -254,7 +254,7 @@ static void buildPartialInvariantUnswitchConditionalBranch(
for (auto *Val : reverse(ToDuplicate)) {
Instruction *Inst = cast<Instruction>(Val);
Instruction *NewInst = Inst->clone();
BB.getInstList().insert(BB.end(), NewInst);
NewInst->insertAt(&BB, BB.end());
RemapInstruction(NewInst, VMap,
RF_NoModuleLevelChanges | RF_IgnoreMissingLocals);
VMap[Val] = NewInst;
Expand Down Expand Up @@ -584,7 +584,7 @@ static bool unswitchTrivialBranch(Loop &L, BranchInst &BI, DominatorTree &DT,
if (MSSAU) {
// Temporarily clone the terminator, to make MSSA update cheaper by
// separating "insert edge" updates from "remove edge" ones.
ParentBB->getInstList().push_back(BI.clone());
BI.clone()->insertAt(ParentBB, ParentBB->end());
} else {
// Create a new unconditional branch that will continue the loop as a new
// terminator.
Expand Down Expand Up @@ -2254,7 +2254,7 @@ static void unswitchNontrivialInvariants(

// Keep a clone of the terminator for MSSA updates.
Instruction *NewTI = TI.clone();
ParentBB->getInstList().push_back(NewTI);
NewTI->insertAt(ParentBB, ParentBB->end());

// First wire up the moved terminator to the preheaders.
if (BI) {
Expand Down
4 changes: 2 additions & 2 deletions llvm/lib/Transforms/Scalar/SimplifyCFGPass.cpp
Expand Up @@ -108,12 +108,12 @@ performBlockTailMerging(Function &F, ArrayRef<BasicBlock *> BBs,
std::get<1>(I) = PHINode::Create(std::get<0>(I)->getType(),
/*NumReservedValues=*/BBs.size(),
CanonicalBB->getName() + ".op");
CanonicalBB->getInstList().push_back(std::get<1>(I));
std::get<1>(I)->insertAt(CanonicalBB, CanonicalBB->end());
}
// Make it so that this canonical block actually has the right
// terminator.
CanonicalTerm = Term->clone();
CanonicalBB->getInstList().push_back(CanonicalTerm);
CanonicalTerm->insertAt(CanonicalBB, CanonicalBB->end());
// If the canonical terminator has operands, rewrite it to take PHI's.
for (auto I : zip(NewOps, CanonicalTerm->operands()))
std::get<1>(I) = std::get<0>(I);
Expand Down
2 changes: 1 addition & 1 deletion llvm/lib/Transforms/Scalar/TLSVariableHoist.cpp
Expand Up @@ -234,7 +234,7 @@ Instruction *TLSVariableHoistPass::genBitCastInst(Function &Fn,
BasicBlock::iterator Iter = findInsertPos(Fn, GV, PosBB);
Type *Ty = GV->getType();
auto *CastInst = new BitCastInst(GV, Ty, "tls_bitcast");
PosBB->getInstList().insert(Iter, CastInst);
CastInst->insertAt(PosBB, Iter);
return CastInst;
}

Expand Down
22 changes: 11 additions & 11 deletions llvm/lib/Transforms/Utils/BasicBlockUtils.cpp
Expand Up @@ -555,8 +555,8 @@ void llvm::ReplaceInstWithValue(BasicBlock::InstListType &BIL,
BI = BIL.erase(BI);
}

void llvm::ReplaceInstWithInst(BasicBlock::InstListType &BIL,
BasicBlock::iterator &BI, Instruction *I) {
void llvm::ReplaceInstWithInst(BasicBlock *BB, BasicBlock::iterator &BI,
Instruction *I) {
assert(I->getParent() == nullptr &&
"ReplaceInstWithInst: Instruction already inserted into basic block!");

Expand All @@ -566,10 +566,10 @@ void llvm::ReplaceInstWithInst(BasicBlock::InstListType &BIL,
I->setDebugLoc(BI->getDebugLoc());

// Insert the new instruction into the basic block...
BasicBlock::iterator New = BIL.insert(BI, I);
BasicBlock::iterator New = I->insertAt(BB, BI);

// Replace all uses of the old instruction, and delete it.
ReplaceInstWithValue(BIL, BI, I);
ReplaceInstWithValue(BB->getInstList(), BI, I);

// Move BI back to point to the newly inserted instruction
BI = New;
Expand All @@ -591,7 +591,7 @@ bool llvm::IsBlockFollowedByDeoptOrUnreachable(const BasicBlock *BB) {

void llvm::ReplaceInstWithInst(Instruction *From, Instruction *To) {
BasicBlock::iterator BI(From);
ReplaceInstWithInst(From->getParent()->getInstList(), BI, To);
ReplaceInstWithInst(From->getParent(), BI, To);
}

BasicBlock *llvm::SplitEdge(BasicBlock *BB, BasicBlock *Succ, DominatorTree *DT,
Expand Down Expand Up @@ -1344,12 +1344,12 @@ static void SplitLandingPadPredecessorsImpl(
LandingPadInst *LPad = OrigBB->getLandingPadInst();
Instruction *Clone1 = LPad->clone();
Clone1->setName(Twine("lpad") + Suffix1);
NewBB1->getInstList().insert(NewBB1->getFirstInsertionPt(), Clone1);
Clone1->insertAt(NewBB1, NewBB1->getFirstInsertionPt());

if (NewBB2) {
Instruction *Clone2 = LPad->clone();
Clone2->setName(Twine("lpad") + Suffix2);
NewBB2->getInstList().insert(NewBB2->getFirstInsertionPt(), Clone2);
Clone2->insertAt(NewBB2, NewBB2->getFirstInsertionPt());

// Create a PHI node for the two cloned landingpad instructions only
// if the original landingpad instruction has some uses.
Expand Down Expand Up @@ -1400,7 +1400,7 @@ ReturnInst *llvm::FoldReturnIntoUncondBranch(ReturnInst *RI, BasicBlock *BB,
Instruction *UncondBranch = Pred->getTerminator();
// Clone the return and add it to the end of the predecessor.
Instruction *NewRet = RI->clone();
Pred->getInstList().push_back(NewRet);
NewRet->insertAt(Pred, Pred->end());

// If the return instruction returns a value, and if the value was a
// PHI node in "BB", propagate the right value into the return.
Expand All @@ -1412,7 +1412,7 @@ ReturnInst *llvm::FoldReturnIntoUncondBranch(ReturnInst *RI, BasicBlock *BB,
// return instruction.
V = BCI->getOperand(0);
NewBC = BCI->clone();
Pred->getInstList().insert(NewRet->getIterator(), NewBC);
NewBC->insertAt(Pred, NewRet->getIterator());
Op = NewBC;
}

Expand All @@ -1422,9 +1422,9 @@ ReturnInst *llvm::FoldReturnIntoUncondBranch(ReturnInst *RI, BasicBlock *BB,
NewEV = EVI->clone();
if (NewBC) {
NewBC->setOperand(0, NewEV);
Pred->getInstList().insert(NewBC->getIterator(), NewEV);
NewEV->insertAt(Pred, NewBC->getIterator());
} else {
Pred->getInstList().insert(NewRet->getIterator(), NewEV);
NewEV->insertAt(Pred, NewRet->getIterator());
Op = NewEV;
}
}
Expand Down

0 comments on commit 06911ba

Please sign in to comment.