Skip to content

Commit c582d15

Browse files
SLTozerAlexisPerry
authored andcommitted
[IR][NFC] Update IRBuilder to use InsertPosition (llvm#96497)
Uses the new InsertPosition class (added in llvm#94226) to simplify some of the IRBuilder interface, and removes the need to pass a BasicBlock alongside a BasicBlock::iterator, using the fact that we can now get the parent basic block from the iterator even if it points to the sentinel. This patch removes the BasicBlock argument from each constructor or call to setInsertPoint. This has no functional effect, but later on as we look to remove the `Instruction *InsertBefore` argument from instruction-creation (discussed [here](https://discourse.llvm.org/t/psa-instruction-constructors-changing-to-iterator-only-insertion/77845)), this will simplify the process by allowing us to deprecate the InsertPosition constructor directly and catch all the cases where we use instructions rather than iterators.
1 parent 367d75d commit c582d15

File tree

97 files changed

+252
-326
lines changed

Some content is hidden

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

97 files changed

+252
-326
lines changed

clang/lib/CodeGen/CGBlocks.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1545,7 +1545,7 @@ llvm::Function *CodeGenFunction::GenerateBlockFunction(
15451545
entry_ptr = entry_ptr->getNextNonDebugInstruction()->getIterator();
15461546
else
15471547
entry_ptr = entry->end();
1548-
Builder.SetInsertPoint(entry, entry_ptr);
1548+
Builder.SetInsertPoint(entry_ptr);
15491549

15501550
// Emit debug information for all the DeclRefExprs.
15511551
// FIXME: also for 'this'

clang/lib/CodeGen/CGGPUBuiltin.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -202,13 +202,13 @@ RValue CodeGenFunction::EmitAMDGPUDevicePrintfCallExpr(const CallExpr *E) {
202202
Args.push_back(Arg);
203203
}
204204

205-
llvm::IRBuilder<> IRB(Builder.GetInsertBlock(), Builder.GetInsertPoint());
205+
llvm::IRBuilder<> IRB(Builder.GetInsertPoint());
206206
IRB.SetCurrentDebugLocation(Builder.getCurrentDebugLocation());
207207

208208
bool isBuffered = (CGM.getTarget().getTargetOpts().AMDGPUPrintfKindVal ==
209209
clang::TargetOptions::AMDGPUPrintfKind::Buffered);
210210
auto Printf = llvm::emitAMDGPUPrintfCall(IRB, Args, isBuffered);
211-
Builder.SetInsertPoint(IRB.GetInsertBlock(), IRB.GetInsertPoint());
211+
Builder.SetInsertPoint(IRB.GetInsertPoint());
212212
return RValue::get(Printf);
213213
}
214214

clang/lib/CodeGen/CGHLSLRuntime.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -436,7 +436,7 @@ void CGHLSLRuntime::generateGlobalCtorDtorCalls() {
436436
for (auto &F : M.functions()) {
437437
if (!F.hasFnAttribute("hlsl.shader"))
438438
continue;
439-
IRBuilder<> B(&F.getEntryBlock(), F.getEntryBlock().begin());
439+
IRBuilder<> B(F.getEntryBlock().begin());
440440
for (auto *Fn : CtorFns)
441441
B.CreateCall(FunctionCallee(Fn));
442442

clang/lib/CodeGen/CGObjC.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2970,21 +2970,20 @@ static llvm::Value *emitARCOperationAfterCall(CodeGenFunction &CGF,
29702970
value = doFallback(CGF, value);
29712971
} else if (llvm::CallInst *call = dyn_cast<llvm::CallInst>(value)) {
29722972
// Place the retain immediately following the call.
2973-
CGF.Builder.SetInsertPoint(call->getParent(),
2974-
++llvm::BasicBlock::iterator(call));
2973+
CGF.Builder.SetInsertPoint(++llvm::BasicBlock::iterator(call));
29752974
value = doAfterCall(CGF, value);
29762975
} else if (llvm::InvokeInst *invoke = dyn_cast<llvm::InvokeInst>(value)) {
29772976
// Place the retain at the beginning of the normal destination block.
29782977
llvm::BasicBlock *BB = invoke->getNormalDest();
2979-
CGF.Builder.SetInsertPoint(BB, BB->begin());
2978+
CGF.Builder.SetInsertPoint(BB->begin());
29802979
value = doAfterCall(CGF, value);
29812980

29822981
// Bitcasts can arise because of related-result returns. Rewrite
29832982
// the operand.
29842983
} else if (llvm::BitCastInst *bitcast = dyn_cast<llvm::BitCastInst>(value)) {
29852984
// Change the insert point to avoid emitting the fall-back call after the
29862985
// bitcast.
2987-
CGF.Builder.SetInsertPoint(bitcast->getParent(), bitcast->getIterator());
2986+
CGF.Builder.SetInsertPoint(bitcast->getIterator());
29882987
llvm::Value *operand = bitcast->getOperand(0);
29892988
operand = emitARCOperationAfterCall(CGF, operand, doAfterCall, doFallback);
29902989
bitcast->setOperand(0, operand);

clang/lib/CodeGen/CGObjCMac.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4417,7 +4417,7 @@ void FragileHazards::emitHazardsInNewBlocks() {
44174417
// call. If the call throws, then this is sufficient to
44184418
// guarantee correctness as long as it doesn't also write to any
44194419
// locals.
4420-
Builder.SetInsertPoint(&BB, BI);
4420+
Builder.SetInsertPoint(BI);
44214421
emitReadHazard(Builder);
44224422
}
44234423
}

clang/lib/CodeGen/CGOpenMPRuntime.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1447,7 +1447,7 @@ llvm::Value *CGOpenMPRuntime::getThreadID(CodeGenFunction &CGF,
14471447
if (!Elem.second.ServiceInsertPt)
14481448
setLocThreadIdInsertPt(CGF);
14491449
CGBuilderTy::InsertPointGuard IPG(CGF.Builder);
1450-
CGF.Builder.SetInsertPoint(Elem.second.ServiceInsertPt);
1450+
CGF.Builder.SetInsertPoint(&*Elem.second.ServiceInsertPt);
14511451
auto DL = ApplyDebugLocation::CreateDefaultArtificial(CGF, Loc);
14521452
llvm::CallInst *Call = CGF.Builder.CreateCall(
14531453
OMPBuilder.getOrCreateRuntimeFunction(CGM.getModule(),

clang/lib/CodeGen/CGStmt.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3076,7 +3076,7 @@ void CodeGenFunction::EmitAsmStmt(const AsmStmt &S) {
30763076
if (IsGCCAsmGoto && !CBRRegResults.empty()) {
30773077
for (llvm::BasicBlock *Succ : CBR->getIndirectDests()) {
30783078
llvm::IRBuilderBase::InsertPointGuard IPG(Builder);
3079-
Builder.SetInsertPoint(Succ, --(Succ->end()));
3079+
Builder.SetInsertPoint(--(Succ->end()));
30803080
EmitAsmStores(*this, S, CBRRegResults[Succ], ResultRegTypes,
30813081
ResultTruncRegTypes, ResultRegDests, ResultRegQualTys,
30823082
ResultTypeRequiresCast, ResultRegIsFlagReg);

clang/lib/CodeGen/CodeGenABITypes.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ llvm::Value *CodeGen::getCXXDestructorImplicitParam(
123123
CGF.CurCodeDecl = D;
124124
CGF.CurFuncDecl = D;
125125
CGF.CurFn = InsertBlock->getParent();
126-
CGF.Builder.SetInsertPoint(InsertBlock, InsertPoint);
126+
CGF.Builder.SetInsertPoint(InsertPoint);
127127
return CGM.getCXXABI().getCXXDestructorImplicitParam(
128128
CGF, D, Type, ForVirtualBase, Delegating);
129129
}

clang/lib/CodeGen/CodeGenFunction.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2764,7 +2764,7 @@ void CodeGenFunction::EmitSanitizerStatReport(llvm::SanitizerStatKind SSK) {
27642764
if (!CGM.getCodeGenOpts().SanitizeStats)
27652765
return;
27662766

2767-
llvm::IRBuilder<> IRB(Builder.GetInsertBlock(), Builder.GetInsertPoint());
2767+
llvm::IRBuilder<> IRB(Builder.GetInsertPoint());
27682768
IRB.SetCurrentDebugLocation(Builder.getCurrentDebugLocation());
27692769
CGM.getSanStats().create(IRB, SSK);
27702770
}
@@ -2883,7 +2883,7 @@ void CodeGenFunction::EmitAArch64MultiVersionResolver(
28832883
}
28842884

28852885
if (!AArch64CpuInitialized) {
2886-
Builder.SetInsertPoint(CurBlock, CurBlock->begin());
2886+
Builder.SetInsertPoint(CurBlock->begin());
28872887
EmitAArch64CpuInit();
28882888
AArch64CpuInitialized = true;
28892889
Builder.SetInsertPoint(CurBlock);

llvm/include/llvm/IR/IRBuilder.h

Lines changed: 15 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -173,37 +173,13 @@ class IRBuilderBase {
173173
BasicBlock::iterator GetInsertPoint() const { return InsertPt; }
174174
LLVMContext &getContext() const { return Context; }
175175

176-
/// This specifies that created instructions should be appended to the
177-
/// end of the specified block.
178-
void SetInsertPoint(BasicBlock *TheBB) {
179-
BB = TheBB;
180-
InsertPt = BB->end();
181-
}
182-
183-
/// This specifies that created instructions should be inserted before
184-
/// the specified instruction.
185-
void SetInsertPoint(Instruction *I) {
186-
BB = I->getParent();
187-
InsertPt = I->getIterator();
188-
assert(InsertPt != BB->end() && "Can't read debug loc from end()");
189-
SetCurrentDebugLocation(I->getStableDebugLoc());
190-
}
191-
192176
/// This specifies that created instructions should be inserted at the
193-
/// specified point.
194-
void SetInsertPoint(BasicBlock *TheBB, BasicBlock::iterator IP) {
195-
BB = TheBB;
196-
InsertPt = IP;
197-
if (IP != TheBB->end())
198-
SetCurrentDebugLocation(IP->getStableDebugLoc());
199-
}
200-
201-
/// This specifies that created instructions should be inserted at
202-
/// the specified point, but also requires that \p IP is dereferencable.
203-
void SetInsertPoint(BasicBlock::iterator IP) {
204-
BB = IP->getParent();
177+
/// specified insert position.
178+
void SetInsertPoint(InsertPosition IP) {
179+
BB = IP.getBasicBlock();
205180
InsertPt = IP;
206-
SetCurrentDebugLocation(IP->getStableDebugLoc());
181+
if (InsertPt != BB->end())
182+
SetCurrentDebugLocation(InsertPt->getStableDebugLoc());
207183
}
208184

209185
/// This specifies that created instructions should inserted at the beginning
@@ -286,7 +262,7 @@ class IRBuilderBase {
286262
/// Sets the current insert point to a previously-saved location.
287263
void restoreIP(InsertPoint IP) {
288264
if (IP.isSet())
289-
SetInsertPoint(IP.getBlock(), IP.getPoint());
265+
SetInsertPoint(IP.getPoint());
290266
else
291267
ClearInsertionPoint();
292268
}
@@ -2677,44 +2653,20 @@ class IRBuilder : public IRBuilderBase {
26772653
ArrayRef<OperandBundleDef> OpBundles = std::nullopt)
26782654
: IRBuilderBase(C, this->Folder, this->Inserter, FPMathTag, OpBundles) {}
26792655

2680-
explicit IRBuilder(BasicBlock *TheBB, FolderTy Folder,
2681-
MDNode *FPMathTag = nullptr,
2682-
ArrayRef<OperandBundleDef> OpBundles = std::nullopt)
2683-
: IRBuilderBase(TheBB->getContext(), this->Folder, this->Inserter,
2684-
FPMathTag, OpBundles),
2685-
Folder(Folder) {
2686-
SetInsertPoint(TheBB);
2687-
}
2688-
2689-
explicit IRBuilder(BasicBlock *TheBB, MDNode *FPMathTag = nullptr,
2656+
explicit IRBuilder(InsertPosition IP, MDNode *FPMathTag = nullptr,
26902657
ArrayRef<OperandBundleDef> OpBundles = std::nullopt)
2691-
: IRBuilderBase(TheBB->getContext(), this->Folder, this->Inserter,
2692-
FPMathTag, OpBundles) {
2693-
SetInsertPoint(TheBB);
2694-
}
2695-
2696-
explicit IRBuilder(Instruction *IP, MDNode *FPMathTag = nullptr,
2697-
ArrayRef<OperandBundleDef> OpBundles = std::nullopt)
2698-
: IRBuilderBase(IP->getContext(), this->Folder, this->Inserter, FPMathTag,
2699-
OpBundles) {
2658+
: IRBuilderBase(IP.getBasicBlock()->getContext(), this->Folder,
2659+
this->Inserter, FPMathTag, OpBundles) {
27002660
SetInsertPoint(IP);
27012661
}
27022662

2703-
IRBuilder(BasicBlock *TheBB, BasicBlock::iterator IP, FolderTy Folder,
2704-
MDNode *FPMathTag = nullptr,
2705-
ArrayRef<OperandBundleDef> OpBundles = std::nullopt)
2706-
: IRBuilderBase(TheBB->getContext(), this->Folder, this->Inserter,
2707-
FPMathTag, OpBundles),
2663+
explicit IRBuilder(InsertPosition IP, FolderTy Folder,
2664+
MDNode *FPMathTag = nullptr,
2665+
ArrayRef<OperandBundleDef> OpBundles = std::nullopt)
2666+
: IRBuilderBase(IP.getBasicBlock()->getContext(), this->Folder,
2667+
this->Inserter, FPMathTag, OpBundles),
27082668
Folder(Folder) {
2709-
SetInsertPoint(TheBB, IP);
2710-
}
2711-
2712-
IRBuilder(BasicBlock *TheBB, BasicBlock::iterator IP,
2713-
MDNode *FPMathTag = nullptr,
2714-
ArrayRef<OperandBundleDef> OpBundles = std::nullopt)
2715-
: IRBuilderBase(TheBB->getContext(), this->Folder, this->Inserter,
2716-
FPMathTag, OpBundles) {
2717-
SetInsertPoint(TheBB, IP);
2669+
SetInsertPoint(IP);
27182670
}
27192671

27202672
/// Avoid copying the full IRBuilder. Prefer using InsertPointGuard

0 commit comments

Comments
 (0)