Skip to content

Instantly share code, notes, and snippets.

@nikic
Created February 4, 2020 20:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nikic/7d6223bc0df4798181fa781ca771e78c to your computer and use it in GitHub Desktop.
Save nikic/7d6223bc0df4798181fa781ca771e78c to your computer and use it in GitHub Desktop.
diff --git a/llvm/include/llvm/IR/IRBuilder.h b/llvm/include/llvm/IR/IRBuilder.h
index 5a290464739..57c0d17a939 100644
--- a/llvm/include/llvm/IR/IRBuilder.h
+++ b/llvm/include/llvm/IR/IRBuilder.h
@@ -112,6 +112,16 @@ public:
ClearInsertionPoint();
}
+ /// Insert and return the specified instruction.
+ virtual Instruction *Insert(Instruction *I, const Twine &Name = "") const = 0;
+
+ /// Insert and return the specified instruction, preserving the type.
+ template<typename InstTy>
+ InstTy *Insert(InstTy *I, const Twine &Name = "") const {
+ Insert(cast<Instruction>(I), Name);
+ return I;
+ }
+
//===--------------------------------------------------------------------===//
// Builder configuration methods
//===--------------------------------------------------------------------===//
@@ -935,7 +945,7 @@ private:
/// every newly created insertion.
template <typename T = ConstantFolder,
typename Inserter = IRBuilderDefaultInserter>
-class IRBuilder : public IRBuilderBase, public Inserter {
+class IRBuilder final : public IRBuilderBase, public Inserter {
T Folder;
public:
@@ -985,13 +995,19 @@ public:
const T &getFolder() { return Folder; }
/// Insert and return the specified instruction.
- template<typename InstTy>
- InstTy *Insert(InstTy *I, const Twine &Name = "") const {
+ Instruction *Insert(Instruction *I, const Twine &Name = "") const override {
this->InsertHelper(I, Name, BB, InsertPt);
this->SetInstDebugLocation(I);
return I;
}
+ /// Insert and return the specified instruction, preserving the type.
+ template<typename InstTy>
+ InstTy *Insert(InstTy *I, const Twine &Name = "") const {
+ Insert(cast<Instruction>(I), Name);
+ return I;
+ }
+
/// No-op overload to handle constants.
Constant *Insert(Constant *C, const Twine& = "") const {
return C;
diff --git a/llvm/lib/IR/IRBuilder.cpp b/llvm/lib/IR/IRBuilder.cpp
index f59d68d1db0..cc6ecb0e36c 100644
--- a/llvm/lib/IR/IRBuilder.cpp
+++ b/llvm/lib/IR/IRBuilder.cpp
@@ -65,10 +65,7 @@ Value *IRBuilderBase::getCastedInt8PtrValue(Value *Ptr) {
// Otherwise, we need to insert a bitcast.
PT = getInt8PtrTy(PT->getAddressSpace());
- BitCastInst *BCI = new BitCastInst(Ptr, PT, "");
- BB->getInstList().insert(InsertPt, BCI);
- SetInstDebugLocation(BCI);
- return BCI;
+ return Insert(new BitCastInst(Ptr, PT), "");
}
static CallInst *createCallHelper(Function *Callee, ArrayRef<Value *> Ops,
@@ -78,9 +75,7 @@ static CallInst *createCallHelper(Function *Callee, ArrayRef<Value *> Ops,
CallInst *CI = CallInst::Create(Callee, Ops, Name);
if (FMFSource)
CI->copyFastMathFlags(FMFSource);
- Builder->GetInsertBlock()->getInstList().insert(Builder->GetInsertPoint(),CI);
- Builder->SetInstDebugLocation(CI);
- return CI;
+ return Builder->Insert(CI);
}
static InvokeInst *createInvokeHelper(Function *Invokee, BasicBlock *NormalDest,
@@ -88,12 +83,8 @@ static InvokeInst *createInvokeHelper(Function *Invokee, BasicBlock *NormalDest,
ArrayRef<Value *> Ops,
IRBuilderBase *Builder,
const Twine &Name = "") {
- InvokeInst *II =
- InvokeInst::Create(Invokee, NormalDest, UnwindDest, Ops, Name);
- Builder->GetInsertBlock()->getInstList().insert(Builder->GetInsertPoint(),
- II);
- Builder->SetInstDebugLocation(II);
- return II;
+ return Builder->Insert(
+ InvokeInst::Create(Invokee, NormalDest, UnwindDest, Ops, Name));
}
CallInst *IRBuilderBase::CreateMemSet(Value *Ptr, Value *Val, Value *Size,
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment