Skip to content

Commit da18a11

Browse files
committed
Use InsertPosition for insertion in all Instructions and IRBuilder
1 parent 0863bd8 commit da18a11

File tree

11 files changed

+413
-3145
lines changed

11 files changed

+413
-3145
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
@@ -2637,19 +2637,18 @@ CodeGenFunction::SanitizerScope::~SanitizerScope() {
26372637

26382638
void CodeGenFunction::InsertHelper(llvm::Instruction *I,
26392639
const llvm::Twine &Name,
2640-
llvm::BasicBlock *BB,
26412640
llvm::BasicBlock::iterator InsertPt) const {
26422641
LoopStack.InsertHelper(I);
26432642
if (IsSanitizerScope)
26442643
I->setNoSanitizeMetadata();
26452644
}
26462645

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

26552654
// 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
@@ -343,7 +343,6 @@ class CodeGenFunction : public CodeGenTypeCache {
343343
/// CGBuilder insert helper. This function is called after an
344344
/// instruction is created using Builder.
345345
void InsertHelper(llvm::Instruction *I, const llvm::Twine &Name,
346-
llvm::BasicBlock *BB,
347346
llvm::BasicBlock::iterator InsertPt) const;
348347

349348
/// 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_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 : public User,
4865
public ilist_node_with_parent<Instruction, BasicBlock,
4966
ilist_iterator_bits<true>,
@@ -1018,11 +1035,7 @@ class Instruction : public User,
10181035
}
10191036

10201037
Instruction(Type *Ty, unsigned iType, Use *Ops, unsigned NumOps,
1021-
InstListType::iterator InsertBefore);
1022-
Instruction(Type *Ty, unsigned iType, Use *Ops, unsigned NumOps,
1023-
Instruction *InsertBefore = nullptr);
1024-
Instruction(Type *Ty, unsigned iType, Use *Ops, unsigned NumOps,
1025-
BasicBlock *InsertAtEnd);
1038+
InsertPosition InsertBefore = nullptr);
10261039

10271040
private:
10281041
/// Create a copy of this instruction.

0 commit comments

Comments
 (0)