diff --git a/llvm/include/llvm/IR/IRBuilder.h b/llvm/include/llvm/IR/IRBuilder.h --- a/llvm/include/llvm/IR/IRBuilder.h +++ b/llvm/include/llvm/IR/IRBuilder.h @@ -984,6 +984,10 @@ SetInsertPoint(TheBB, IP); } + /// Avoid copying the full IRBuilder. Prefer using InsertPointGuard + /// or FastMathFlagGuard instead. + IRBuilder(const IRBuilder &) = delete; + /// Get the constant folder being used. const T &getFolder() { return Folder; } diff --git a/llvm/lib/IR/DIBuilder.cpp b/llvm/lib/IR/DIBuilder.cpp --- a/llvm/lib/IR/DIBuilder.cpp +++ b/llvm/lib/IR/DIBuilder.cpp @@ -895,18 +895,15 @@ return insertDbgValueIntrinsic(V, VarInfo, Expr, DL, InsertAtEnd, nullptr); } -/// Return an IRBuilder for inserting dbg.declare and dbg.value intrinsics. This -/// abstracts over the various ways to specify an insert position. -static IRBuilder<> getIRBForDbgInsertion(const DILocation *DL, - BasicBlock *InsertBB, - Instruction *InsertBefore) { - IRBuilder<> B(DL->getContext()); +/// Initialize IRBuilder for inserting dbg.declare and dbg.value intrinsics. +/// This abstracts over the various ways to specify an insert position. +static void initIRBuilder(IRBuilder<> &Builder, const DILocation *DL, + BasicBlock *InsertBB, Instruction *InsertBefore) { if (InsertBefore) - B.SetInsertPoint(InsertBefore); + Builder.SetInsertPoint(InsertBefore); else if (InsertBB) - B.SetInsertPoint(InsertBB); - B.SetCurrentDebugLocation(DL); - return B; + Builder.SetInsertPoint(InsertBB); + Builder.SetCurrentDebugLocation(DL); } static Value *getDbgIntrinsicValueImpl(LLVMContext &VMContext, Value *V) { @@ -936,7 +933,8 @@ MetadataAsValue::get(VMContext, VarInfo), MetadataAsValue::get(VMContext, Expr)}; - IRBuilder<> B = getIRBForDbgInsertion(DL, InsertBB, InsertBefore); + IRBuilder<> B(DL->getContext()); + initIRBuilder(B, DL, InsertBB, InsertBefore); return B.CreateCall(DeclareFn, Args); } @@ -958,7 +956,8 @@ MetadataAsValue::get(VMContext, VarInfo), MetadataAsValue::get(VMContext, Expr)}; - IRBuilder<> B = getIRBForDbgInsertion(DL, InsertBB, InsertBefore); + IRBuilder<> B(DL->getContext()); + initIRBuilder(B, DL, InsertBB, InsertBefore); return B.CreateCall(ValueFn, Args); } @@ -976,7 +975,8 @@ trackIfUnresolved(LabelInfo); Value *Args[] = {MetadataAsValue::get(VMContext, LabelInfo)}; - IRBuilder<> B = getIRBForDbgInsertion(DL, InsertBB, InsertBefore); + IRBuilder<> B(DL->getContext()); + initIRBuilder(B, DL, InsertBB, InsertBefore); return B.CreateCall(LabelFn, Args); } diff --git a/llvm/lib/Target/X86/X86ISelLowering.cpp b/llvm/lib/Target/X86/X86ISelLowering.cpp --- a/llvm/lib/Target/X86/X86ISelLowering.cpp +++ b/llvm/lib/Target/X86/X86ISelLowering.cpp @@ -27717,7 +27717,7 @@ AI->use_empty()) return nullptr; - auto Builder = IRBuilder<>(AI); + IRBuilder<> Builder(AI); Module *M = Builder.GetInsertBlock()->getParent()->getParent(); auto SSID = AI->getSyncScopeID(); // We must restrict the ordering to avoid generating loads with Release or diff --git a/llvm/lib/Transforms/Instrumentation/AddressSanitizer.cpp b/llvm/lib/Transforms/Instrumentation/AddressSanitizer.cpp --- a/llvm/lib/Transforms/Instrumentation/AddressSanitizer.cpp +++ b/llvm/lib/Transforms/Instrumentation/AddressSanitizer.cpp @@ -787,7 +787,7 @@ StringRef OriginalName); void SetComdatForGlobalMetadata(GlobalVariable *G, GlobalVariable *Metadata, StringRef InternalSuffix); - IRBuilder<> CreateAsanModuleDtor(Module &M); + Instruction *CreateAsanModuleDtor(Module &M); bool ShouldInstrumentGlobal(GlobalVariable *G); bool ShouldUseMachOGlobalsSection() const; @@ -2030,13 +2030,13 @@ return Metadata; } -IRBuilder<> ModuleAddressSanitizer::CreateAsanModuleDtor(Module &M) { +Instruction *ModuleAddressSanitizer::CreateAsanModuleDtor(Module &M) { AsanDtorFunction = Function::Create(FunctionType::get(Type::getVoidTy(*C), false), GlobalValue::InternalLinkage, kAsanModuleDtorName, &M); BasicBlock *AsanDtorBB = BasicBlock::Create(*C, "", AsanDtorFunction); - return IRBuilder<>(ReturnInst::Create(*C, AsanDtorBB)); + return ReturnInst::Create(*C, AsanDtorBB); } void ModuleAddressSanitizer::InstrumentGlobalsCOFF( @@ -2115,7 +2115,7 @@ // We also need to unregister globals at the end, e.g., when a shared library // gets closed. - IRBuilder<> IRB_Dtor = CreateAsanModuleDtor(M); + IRBuilder<> IRB_Dtor(CreateAsanModuleDtor(M)); IRB_Dtor.CreateCall(AsanUnregisterElfGlobals, {IRB.CreatePointerCast(RegisteredFlag, IntptrTy), IRB.CreatePointerCast(StartELFMetadata, IntptrTy), @@ -2174,7 +2174,7 @@ // We also need to unregister globals at the end, e.g., when a shared library // gets closed. - IRBuilder<> IRB_Dtor = CreateAsanModuleDtor(M); + IRBuilder<> IRB_Dtor(CreateAsanModuleDtor(M)); IRB_Dtor.CreateCall(AsanUnregisterImageGlobals, {IRB.CreatePointerCast(RegisteredFlag, IntptrTy)}); } @@ -2202,7 +2202,7 @@ // We also need to unregister globals at the end, e.g., when a shared library // gets closed. - IRBuilder<> IRB_Dtor = CreateAsanModuleDtor(M); + IRBuilder<> IRB_Dtor(CreateAsanModuleDtor(M)); IRB_Dtor.CreateCall(AsanUnregisterGlobals, {IRB.CreatePointerCast(AllGlobals, IntptrTy), ConstantInt::get(IntptrTy, N)});