Skip to content

Commit 3e5b178

Browse files
committed
Use InsertPosition for insertion in all Instructions and IRBuilder
1 parent f50c98b commit 3e5b178

File tree

11 files changed

+404
-3123
lines changed

11 files changed

+404
-3123
lines changed

clang/lib/CodeGen/CGBuilder.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ class CGBuilderInserter final : public llvm::IRBuilderDefaultInserter {
3535

3636
/// This forwards to CodeGenFunction::InsertHelper.
3737
void InsertHelper(llvm::Instruction *I, const llvm::Twine &Name,
38-
llvm::BasicBlock *BB,
3938
llvm::BasicBlock::iterator InsertPt) const override;
4039

4140
private:

clang/lib/CodeGen/CGExpr.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,8 @@ llvm::AllocaInst *CodeGenFunction::CreateTempAlloca(llvm::Type *Ty,
120120
Alloca = Builder.CreateAlloca(Ty, ArraySize, Name);
121121
else
122122
Alloca = new llvm::AllocaInst(Ty, CGM.getDataLayout().getAllocaAddrSpace(),
123-
ArraySize, Name, AllocaInsertPt);
123+
ArraySize, Name,
124+
(llvm::Instruction *)AllocaInsertPt);
124125
if (Allocas) {
125126
Allocas->Add(Alloca);
126127
}

clang/lib/CodeGen/CodeGenFunction.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2635,19 +2635,18 @@ CodeGenFunction::SanitizerScope::~SanitizerScope() {
26352635

26362636
void CodeGenFunction::InsertHelper(llvm::Instruction *I,
26372637
const llvm::Twine &Name,
2638-
llvm::BasicBlock *BB,
26392638
llvm::BasicBlock::iterator InsertPt) const {
26402639
LoopStack.InsertHelper(I);
26412640
if (IsSanitizerScope)
26422641
I->setNoSanitizeMetadata();
26432642
}
26442643

26452644
void CGBuilderInserter::InsertHelper(
2646-
llvm::Instruction *I, const llvm::Twine &Name, llvm::BasicBlock *BB,
2645+
llvm::Instruction *I, const llvm::Twine &Name,
26472646
llvm::BasicBlock::iterator InsertPt) const {
2648-
llvm::IRBuilderDefaultInserter::InsertHelper(I, Name, BB, InsertPt);
2647+
llvm::IRBuilderDefaultInserter::InsertHelper(I, Name, InsertPt);
26492648
if (CGF)
2650-
CGF->InsertHelper(I, Name, BB, InsertPt);
2649+
CGF->InsertHelper(I, Name, InsertPt);
26512650
}
26522651

26532652
// Emits an error if we don't have a valid set of target features for the

clang/lib/CodeGen/CodeGenFunction.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -345,7 +345,6 @@ class CodeGenFunction : public CodeGenTypeCache {
345345
/// CGBuilder insert helper. This function is called after an
346346
/// instruction is created using Builder.
347347
void InsertHelper(llvm::Instruction *I, const llvm::Twine &Name,
348-
llvm::BasicBlock *BB,
349348
llvm::BasicBlock::iterator InsertPt) const;
350349

351350
/// CurFuncDecl - Holds the Decl for the current outermost

llvm/include/llvm/IR/IRBuilder.h

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,10 @@ class IRBuilderDefaultInserter {
6363
virtual ~IRBuilderDefaultInserter();
6464

6565
virtual void InsertHelper(Instruction *I, const Twine &Name,
66-
BasicBlock *BB,
6766
BasicBlock::iterator InsertPt) const {
68-
if (BB)
69-
I->insertInto(BB, InsertPt);
67+
if (InsertPt.isValid()) {
68+
I->insertInto(InsertPt.getNodeParent(), InsertPt);
69+
}
7070
I->setName(Name);
7171
}
7272
};
@@ -83,9 +83,8 @@ class IRBuilderCallbackInserter : public IRBuilderDefaultInserter {
8383
: Callback(std::move(Callback)) {}
8484

8585
void InsertHelper(Instruction *I, const Twine &Name,
86-
BasicBlock *BB,
8786
BasicBlock::iterator InsertPt) const override {
88-
IRBuilderDefaultInserter::InsertHelper(I, Name, BB, InsertPt);
87+
IRBuilderDefaultInserter::InsertHelper(I, Name, InsertPt);
8988
Callback(I);
9089
}
9190
};
@@ -143,7 +142,7 @@ class IRBuilderBase {
143142
/// Insert and return the specified instruction.
144143
template<typename InstTy>
145144
InstTy *Insert(InstTy *I, const Twine &Name = "") const {
146-
Inserter.InsertHelper(I, Name, BB, InsertPt);
145+
Inserter.InsertHelper(I, Name, InsertPt);
147146
AddMetadataToInst(I);
148147
return I;
149148
}

llvm/include/llvm/IR/InstrTypes.h

Lines changed: 61 additions & 352 deletions
Large diffs are not rendered by default.

llvm/include/llvm/IR/Instruction.h

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,23 @@ template <> struct ilist_alloc_traits<Instruction> {
4444
iterator_range<simple_ilist<DbgRecord>::iterator>
4545
getDbgRecordRange(DbgMarker *);
4646

47+
class InsertPosition {
48+
using InstListType = SymbolTableList<Instruction, ilist_iterator_bits<true>,
49+
ilist_node_parent<BasicBlock>>;
50+
InstListType::iterator InsertAt;
51+
52+
public:
53+
InsertPosition(std::nullopt_t) : InsertAt() {}
54+
InsertPosition(std::nullptr_t) : InsertAt() {}
55+
// LLVM_DEPRECATED("Use BasicBlock::iterators for insertion instead",
56+
// "BasicBlock::iterator")
57+
InsertPosition(Instruction *InsertBefore);
58+
InsertPosition(BasicBlock *InsertAtEnd);
59+
InsertPosition(InstListType::iterator InsertAt) : InsertAt(InsertAt) {}
60+
operator InstListType::iterator() const { return InsertAt; }
61+
bool IsValid() const { return InsertAt.getNodePtr(); }
62+
};
63+
4764
class Instruction
4865
: public User,
4966
public ilist_node_with_parent<Instruction, BasicBlock,
@@ -1024,11 +1041,7 @@ class Instruction
10241041
}
10251042

10261043
Instruction(Type *Ty, unsigned iType, Use *Ops, unsigned NumOps,
1027-
InstListType::iterator InsertBefore);
1028-
Instruction(Type *Ty, unsigned iType, Use *Ops, unsigned NumOps,
1029-
Instruction *InsertBefore = nullptr);
1030-
Instruction(Type *Ty, unsigned iType, Use *Ops, unsigned NumOps,
1031-
BasicBlock *InsertAtEnd);
1044+
InsertPosition InsertBefore = nullptr);
10321045

10331046
private:
10341047
/// Create a copy of this instruction.

0 commit comments

Comments
 (0)