Skip to content

Commit 80f8814

Browse files
authored
[LLVM] Add InsertPosition union-type to remove overloads of Instruction-creation (#94226)
This patch simplifies instruction creation by replacing all overloads of instruction constructors/Create methods that are identical other than the Instruction *InsertBefore/BasicBlock *InsertAtEnd/BasicBlock::iterator InsertBefore argument with a single version that takes an InsertPosition argument. The InsertPosition class can be implicitly constructed from any of the above, internally converting them to the appropriate BasicBlock::iterator value which can then be used to insert the instruction (or to not insert it if an invalid iterator is passed). The upshot of this is that code will be deduplicated, and all callsites will switch to calling the new unified version without any changes needed to make the compiler happy. There is at least one exception to this; the construction of InsertPosition is a user-defined conversion, so any caller that was already relying on a different user-defined conversion won't work. In all of LLVM and Clang this happens exactly once: at clang/lib/CodeGen/CGExpr.cpp:123 we try to construct an alloca with an AssertingVH<Instruction> argument, which must now be cast to an Instruction* by using `&*`. If this is more common elsewhere, it could be fixed by adding an appropriate constructor to InsertPosition.
1 parent e2296d8 commit 80f8814

File tree

12 files changed

+411
-3153
lines changed

12 files changed

+411
-3153
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: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ 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, &*AllocaInsertPt);
124124
if (Allocas) {
125125
Allocas->Add(Alloca);
126126
}

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: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,9 @@ 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);
7069
I->setName(Name);
7170
}
7271
};
@@ -83,9 +82,8 @@ class IRBuilderCallbackInserter : public IRBuilderDefaultInserter {
8382
: Callback(std::move(Callback)) {}
8483

8584
void InsertHelper(Instruction *I, const Twine &Name,
86-
BasicBlock *BB,
8785
BasicBlock::iterator InsertPt) const override {
88-
IRBuilderDefaultInserter::InsertHelper(I, Name, BB, InsertPt);
86+
IRBuilderDefaultInserter::InsertHelper(I, Name, InsertPt);
8987
Callback(I);
9088
}
9189
};
@@ -143,7 +141,7 @@ class IRBuilderBase {
143141
/// Insert and return the specified instruction.
144142
template<typename InstTy>
145143
InstTy *Insert(InstTy *I, const Twine &Name = "") const {
146-
Inserter.InsertHelper(I, Name, BB, InsertPt);
144+
Inserter.InsertHelper(I, Name, InsertPt);
147145
AddMetadataToInst(I);
148146
return I;
149147
}

llvm/include/llvm/IR/InstrTypes.h

Lines changed: 61 additions & 353 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::nullptr_t) : InsertAt() {}
54+
// LLVM_DEPRECATED("Use BasicBlock::iterators for insertion instead",
55+
// "BasicBlock::iterator")
56+
InsertPosition(Instruction *InsertBefore);
57+
InsertPosition(BasicBlock *InsertAtEnd);
58+
InsertPosition(InstListType::iterator InsertAt) : InsertAt(InsertAt) {}
59+
operator InstListType::iterator() const { return InsertAt; }
60+
bool isValid() const { return InsertAt.isValid(); }
61+
BasicBlock *getBasicBlock() { return InsertAt.getNodeParent(); }
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)