Skip to content

[LLVM] Add InsertPosition union-type to remove overloads of Instruction-creation #94226

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jun 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion clang/lib/CodeGen/CGBuilder.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ class CGBuilderInserter final : public llvm::IRBuilderDefaultInserter {

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

private:
Expand Down
2 changes: 1 addition & 1 deletion clang/lib/CodeGen/CGExpr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ llvm::AllocaInst *CodeGenFunction::CreateTempAlloca(llvm::Type *Ty,
Alloca = Builder.CreateAlloca(Ty, ArraySize, Name);
else
Alloca = new llvm::AllocaInst(Ty, CGM.getDataLayout().getAllocaAddrSpace(),
ArraySize, Name, AllocaInsertPt);
ArraySize, Name, &*AllocaInsertPt);
if (Allocas) {
Allocas->Add(Alloca);
}
Expand Down
7 changes: 3 additions & 4 deletions clang/lib/CodeGen/CodeGenFunction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2637,19 +2637,18 @@ CodeGenFunction::SanitizerScope::~SanitizerScope() {

void CodeGenFunction::InsertHelper(llvm::Instruction *I,
const llvm::Twine &Name,
llvm::BasicBlock *BB,
llvm::BasicBlock::iterator InsertPt) const {
LoopStack.InsertHelper(I);
if (IsSanitizerScope)
I->setNoSanitizeMetadata();
}

void CGBuilderInserter::InsertHelper(
llvm::Instruction *I, const llvm::Twine &Name, llvm::BasicBlock *BB,
llvm::Instruction *I, const llvm::Twine &Name,
llvm::BasicBlock::iterator InsertPt) const {
llvm::IRBuilderDefaultInserter::InsertHelper(I, Name, BB, InsertPt);
llvm::IRBuilderDefaultInserter::InsertHelper(I, Name, InsertPt);
if (CGF)
CGF->InsertHelper(I, Name, BB, InsertPt);
CGF->InsertHelper(I, Name, InsertPt);
}

// Emits an error if we don't have a valid set of target features for the
Expand Down
1 change: 0 additions & 1 deletion clang/lib/CodeGen/CodeGenFunction.h
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,6 @@ class CodeGenFunction : public CodeGenTypeCache {
/// CGBuilder insert helper. This function is called after an
/// instruction is created using Builder.
void InsertHelper(llvm::Instruction *I, const llvm::Twine &Name,
llvm::BasicBlock *BB,
llvm::BasicBlock::iterator InsertPt) const;

/// CurFuncDecl - Holds the Decl for the current outermost
Expand Down
10 changes: 4 additions & 6 deletions llvm/include/llvm/IR/IRBuilder.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,9 @@ class IRBuilderDefaultInserter {
virtual ~IRBuilderDefaultInserter();

virtual void InsertHelper(Instruction *I, const Twine &Name,
BasicBlock *BB,
BasicBlock::iterator InsertPt) const {
if (BB)
I->insertInto(BB, InsertPt);
if (InsertPt.isValid())
I->insertInto(InsertPt.getNodeParent(), InsertPt);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should drop the BasicBlock argument from this API now, but that can be a followup...

I->setName(Name);
}
};
Expand All @@ -83,9 +82,8 @@ class IRBuilderCallbackInserter : public IRBuilderDefaultInserter {
: Callback(std::move(Callback)) {}

void InsertHelper(Instruction *I, const Twine &Name,
BasicBlock *BB,
BasicBlock::iterator InsertPt) const override {
IRBuilderDefaultInserter::InsertHelper(I, Name, BB, InsertPt);
IRBuilderDefaultInserter::InsertHelper(I, Name, InsertPt);
Callback(I);
}
};
Expand Down Expand Up @@ -143,7 +141,7 @@ class IRBuilderBase {
/// Insert and return the specified instruction.
template<typename InstTy>
InstTy *Insert(InstTy *I, const Twine &Name = "") const {
Inserter.InsertHelper(I, Name, BB, InsertPt);
Inserter.InsertHelper(I, Name, InsertPt);
AddMetadataToInst(I);
return I;
}
Expand Down
414 changes: 61 additions & 353 deletions llvm/include/llvm/IR/InstrTypes.h

Large diffs are not rendered by default.

23 changes: 18 additions & 5 deletions llvm/include/llvm/IR/Instruction.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,23 @@ template <> struct ilist_alloc_traits<Instruction> {
iterator_range<simple_ilist<DbgRecord>::iterator>
getDbgRecordRange(DbgMarker *);

class InsertPosition {
using InstListType = SymbolTableList<Instruction, ilist_iterator_bits<true>,
ilist_parent<BasicBlock>>;
InstListType::iterator InsertAt;

public:
InsertPosition(std::nullptr_t) : InsertAt() {}
// LLVM_DEPRECATED("Use BasicBlock::iterators for insertion instead",
// "BasicBlock::iterator")
InsertPosition(Instruction *InsertBefore);
InsertPosition(BasicBlock *InsertAtEnd);
InsertPosition(InstListType::iterator InsertAt) : InsertAt(InsertAt) {}
operator InstListType::iterator() const { return InsertAt; }
bool isValid() const { return InsertAt.isValid(); }
BasicBlock *getBasicBlock() { return InsertAt.getNodeParent(); }
};

class Instruction : public User,
public ilist_node_with_parent<Instruction, BasicBlock,
ilist_iterator_bits<true>,
Expand Down Expand Up @@ -1018,11 +1035,7 @@ class Instruction : public User,
}

Instruction(Type *Ty, unsigned iType, Use *Ops, unsigned NumOps,
InstListType::iterator InsertBefore);
Instruction(Type *Ty, unsigned iType, Use *Ops, unsigned NumOps,
Instruction *InsertBefore = nullptr);
Instruction(Type *Ty, unsigned iType, Use *Ops, unsigned NumOps,
BasicBlock *InsertAtEnd);
InsertPosition InsertBefore = nullptr);

private:
/// Create a copy of this instruction.
Expand Down
Loading
Loading