Skip to content

[irgen] Implement irgen::IRBuilder::SavedInsertionPointRAII. #41447

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
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
69 changes: 69 additions & 0 deletions lib/IRGen/IRBuilder.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@ class IRBuilder : public IRBuilderBase {
#endif
{}

/// Defined below.
class SavedInsertionPointRAII;

/// Determines if the current location is apparently reachable. The
/// invariant we maintain is that the insertion point of the builder
/// always points within a block unless the current location is
Expand Down Expand Up @@ -345,6 +348,72 @@ class IRBuilder : public IRBuilderBase {
}
return result;
}

bool insertingAtEndOfBlock() const {
assert(hasValidIP() && "Must have insertion point to ask about it");
return InsertPt == BB->end();
}
};

/// Given a Builder as input to its constructor, this class resets the Builder
/// so it has the same insertion point at end of scope.
class IRBuilder::SavedInsertionPointRAII {
IRBuilder &builder;
PointerUnion<llvm::Instruction *, llvm::BasicBlock *> savedInsertionPoint;

public:
/// Constructor that saves a Builder's insertion point without changing the
/// builder's underlying insertion point.
SavedInsertionPointRAII(IRBuilder &inputBuilder)
: builder(inputBuilder), savedInsertionPoint() {
// If our builder does not have a valid insertion point, just put nullptr
// into SavedIP.
if (!builder.hasValidIP()) {
savedInsertionPoint = static_cast<llvm::BasicBlock *>(nullptr);
return;
}

// If we are inserting into the end of the block, stash the insertion block.
if (builder.insertingAtEndOfBlock()) {
savedInsertionPoint = builder.GetInsertBlock();
return;
}

// Otherwise, stash the instruction.
auto *i = &*builder.GetInsertPoint();
savedInsertionPoint = i;
}

SavedInsertionPointRAII(IRBuilder &b, llvm::Instruction *newInsertionPoint)
: SavedInsertionPointRAII(b) {
builder.SetInsertPoint(newInsertionPoint);
}

SavedInsertionPointRAII(IRBuilder &b, llvm::BasicBlock *block,
llvm::BasicBlock::iterator iter)
: SavedInsertionPointRAII(b) {
builder.SetInsertPoint(block, iter);
}

SavedInsertionPointRAII(IRBuilder &b, llvm::BasicBlock *insertionBlock)
: SavedInsertionPointRAII(b) {
builder.SetInsertPoint(insertionBlock);
}

SavedInsertionPointRAII(const SavedInsertionPointRAII &) = delete;
SavedInsertionPointRAII &operator=(const SavedInsertionPointRAII &) = delete;
SavedInsertionPointRAII(SavedInsertionPointRAII &&) = delete;
SavedInsertionPointRAII &operator=(SavedInsertionPointRAII &&) = delete;

~SavedInsertionPointRAII() {
if (savedInsertionPoint.isNull()) {
builder.ClearInsertionPoint();
} else if (savedInsertionPoint.is<llvm::Instruction *>()) {
builder.SetInsertPoint(savedInsertionPoint.get<llvm::Instruction *>());
} else {
builder.SetInsertPoint(savedInsertionPoint.get<llvm::BasicBlock *>());
}
}
};

} // end namespace irgen
Expand Down