From 3ec9f7494b31f2fe51d5ed0e07adcf4b7199def6 Mon Sep 17 00:00:00 2001 From: Harald van Dijk Date: Wed, 12 Feb 2025 17:38:59 +0000 Subject: [PATCH] [DebugInfo] Update DIBuilder insertion to take InsertPosition (#126059) After #124287 updated several functions to return iterators rather than Instruction *, it was no longer straightforward to pass their result to DIBuilder. This commit updates DIBuilder methods to accept an InsertPosition instead, so that they can be called with an iterator (preferred), or with a deprecation warning an Instruction *, or a BasicBlock *. This commit also updates the existing calls to the DIBuilder methods to pass in iterators. --- clang/lib/CodeGen/CGDebugInfo.cpp | 17 +-- llvm/include/llvm/IR/DIBuilder.h | 62 ++-------- llvm/lib/IR/DIBuilder.cpp | 116 +++++------------- llvm/lib/IR/DebugInfo.cpp | 41 ++++--- llvm/lib/Transforms/Coroutines/CoroFrame.cpp | 4 +- llvm/lib/Transforms/Scalar/SROA.cpp | 2 +- llvm/lib/Transforms/Utils/Debugify.cpp | 14 ++- llvm/lib/Transforms/Utils/Local.cpp | 23 +--- .../Utils/PromoteMemoryToRegister.cpp | 3 +- llvm/unittests/IR/IRBuilderTest.cpp | 12 +- .../Transforms/Utils/CloningTest.cpp | 5 +- 11 files changed, 99 insertions(+), 200 deletions(-) diff --git a/clang/lib/CodeGen/CGDebugInfo.cpp b/clang/lib/CodeGen/CGDebugInfo.cpp index d5b584ec0f2e9..b679cdddc975b 100644 --- a/clang/lib/CodeGen/CGDebugInfo.cpp +++ b/clang/lib/CodeGen/CGDebugInfo.cpp @@ -4901,7 +4901,7 @@ llvm::DILocalVariable *CGDebugInfo::EmitDeclare(const VarDecl *VD, llvm::DILocation::get(CGM.getLLVMContext(), Line, Column, Scope, CurInlinedAt), - Builder.GetInsertBlock()); + Builder.GetInsertBlock()->end()); } } } @@ -4969,7 +4969,7 @@ llvm::DILocalVariable *CGDebugInfo::EmitDeclare(const VarDecl *VD, DBuilder.insertDeclare(Storage, D, DBuilder.createExpression(Expr), llvm::DILocation::get(CGM.getLLVMContext(), Line, Column, Scope, CurInlinedAt), - Builder.GetInsertBlock()); + Builder.GetInsertBlock()->end()); return D; } @@ -5075,7 +5075,7 @@ llvm::DILocalVariable *CGDebugInfo::EmitDeclare(const BindingDecl *BD, DBuilder.insertDeclare(Storage, D, DBuilder.createExpression(Expr), llvm::DILocation::get(CGM.getLLVMContext(), Line, Column, Scope, CurInlinedAt), - Builder.GetInsertBlock()); + Builder.GetInsertBlock()->end()); return D; } @@ -5122,7 +5122,7 @@ void CGDebugInfo::EmitLabel(const LabelDecl *D, CGBuilderTy &Builder) { DBuilder.insertLabel(L, llvm::DILocation::get(CGM.getLLVMContext(), Line, Column, Scope, CurInlinedAt), - Builder.GetInsertBlock()); + Builder.GetInsertBlock()->end()); } llvm::DIType *CGDebugInfo::CreateSelfType(const QualType &QualTy, @@ -5200,9 +5200,10 @@ void CGDebugInfo::EmitDeclareOfBlockDeclRefVariable( LexicalBlockStack.back(), CurInlinedAt); auto *Expr = DBuilder.createExpression(addr); if (InsertPoint) - DBuilder.insertDeclare(Storage, D, Expr, DL, InsertPoint); + DBuilder.insertDeclare(Storage, D, Expr, DL, InsertPoint->getIterator()); else - DBuilder.insertDeclare(Storage, D, Expr, DL, Builder.GetInsertBlock()); + DBuilder.insertDeclare(Storage, D, Expr, DL, + Builder.GetInsertBlock()->end()); } llvm::DILocalVariable * @@ -5385,7 +5386,7 @@ void CGDebugInfo::EmitDeclareOfBlockLiteralArgVariable(const CGBlockInfo &block, DBuilder.insertDeclare(Alloca, debugVar, DBuilder.createExpression(), llvm::DILocation::get(CGM.getLLVMContext(), line, column, scope, CurInlinedAt), - Builder.GetInsertBlock()); + Builder.GetInsertBlock()->end()); } llvm::DIDerivedType * @@ -5865,7 +5866,7 @@ void CGDebugInfo::EmitPseudoVariable(CGBuilderTy &Builder, if (auto InsertPoint = Value->getInsertionPointAfterDef()) { DBuilder.insertDbgValueIntrinsic(Value, D, DBuilder.createExpression(), DIL, - &**InsertPoint); + *InsertPoint); } } diff --git a/llvm/include/llvm/IR/DIBuilder.h b/llvm/include/llvm/IR/DIBuilder.h index 8bee9f4703dd9..9b729eccda8ee 100644 --- a/llvm/include/llvm/IR/DIBuilder.h +++ b/llvm/include/llvm/IR/DIBuilder.h @@ -92,33 +92,15 @@ namespace llvm { /// Create an \a temporary node and track it in \a UnresolvedNodes. void trackIfUnresolved(MDNode *N); - /// Internal helper for insertDeclare. - DbgInstPtr insertDeclare(llvm::Value *Storage, DILocalVariable *VarInfo, - DIExpression *Expr, const DILocation *DL, - BasicBlock *InsertBB, Instruction *InsertBefore); - - /// Internal helper for insertLabel. - DbgInstPtr insertLabel(DILabel *LabelInfo, const DILocation *DL, - BasicBlock *InsertBB, Instruction *InsertBefore); - /// Internal helper. Track metadata if untracked and insert \p DVR. - void insertDbgVariableRecord(DbgVariableRecord *DVR, BasicBlock *InsertBB, - Instruction *InsertBefore, - bool InsertAtHead = false); + void insertDbgVariableRecord(DbgVariableRecord *DVR, + InsertPosition InsertPt); /// Internal helper with common code used by insertDbg{Value,Addr}Intrinsic. Instruction *insertDbgIntrinsic(llvm::Function *Intrinsic, llvm::Value *Val, DILocalVariable *VarInfo, DIExpression *Expr, const DILocation *DL, - BasicBlock *InsertBB, - Instruction *InsertBefore); - - /// Internal helper for insertDbgValueIntrinsic. - DbgInstPtr insertDbgValueIntrinsic(llvm::Value *Val, - DILocalVariable *VarInfo, - DIExpression *Expr, const DILocation *DL, - BasicBlock *InsertBB, - Instruction *InsertBefore); + InsertPosition InsertPt); public: /// Construct a builder for a module. @@ -961,16 +943,6 @@ namespace llvm { StringRef Name = "", DINodeArray Elements = nullptr); - /// Insert a new llvm.dbg.declare intrinsic call. - /// \param Storage llvm::Value of the variable - /// \param VarInfo Variable's debug info descriptor. - /// \param Expr A complex location expression. - /// \param DL Debug info location. - /// \param InsertAtEnd Location for the new intrinsic. - DbgInstPtr insertDeclare(llvm::Value *Storage, DILocalVariable *VarInfo, - DIExpression *Expr, const DILocation *DL, - BasicBlock *InsertAtEnd); - /// Insert a new llvm.dbg.assign intrinsic call. /// \param LinkedInstr Instruction with a DIAssignID to link with the new /// intrinsic. The intrinsic will be inserted after @@ -995,46 +967,28 @@ namespace llvm { /// \param VarInfo Variable's debug info descriptor. /// \param Expr A complex location expression. /// \param DL Debug info location. - /// \param InsertBefore Location for the new intrinsic. + /// \param InsertPt Location for the new intrinsic. DbgInstPtr insertDeclare(llvm::Value *Storage, DILocalVariable *VarInfo, DIExpression *Expr, const DILocation *DL, - Instruction *InsertBefore); + InsertPosition InsertPt); /// Insert a new llvm.dbg.label intrinsic call. /// \param LabelInfo Label's debug info descriptor. /// \param DL Debug info location. /// \param InsertBefore Location for the new intrinsic. DbgInstPtr insertLabel(DILabel *LabelInfo, const DILocation *DL, - Instruction *InsertBefore); - - /// Insert a new llvm.dbg.label intrinsic call. - /// \param LabelInfo Label's debug info descriptor. - /// \param DL Debug info location. - /// \param InsertAtEnd Location for the new intrinsic. - DbgInstPtr insertLabel(DILabel *LabelInfo, const DILocation *DL, - BasicBlock *InsertAtEnd); - - /// Insert a new llvm.dbg.value intrinsic call. - /// \param Val llvm::Value of the variable - /// \param VarInfo Variable's debug info descriptor. - /// \param Expr A complex location expression. - /// \param DL Debug info location. - /// \param InsertAtEnd Location for the new intrinsic. - DbgInstPtr insertDbgValueIntrinsic(llvm::Value *Val, - DILocalVariable *VarInfo, - DIExpression *Expr, const DILocation *DL, - BasicBlock *InsertAtEnd); + InsertPosition InsertPt); /// Insert a new llvm.dbg.value intrinsic call. /// \param Val llvm::Value of the variable /// \param VarInfo Variable's debug info descriptor. /// \param Expr A complex location expression. /// \param DL Debug info location. - /// \param InsertBefore Location for the new intrinsic. + /// \param InsertPt Location for the new intrinsic. DbgInstPtr insertDbgValueIntrinsic(llvm::Value *Val, DILocalVariable *VarInfo, DIExpression *Expr, const DILocation *DL, - Instruction *InsertBefore); + InsertPosition InsertPt); /// Replace the vtable holder in the given type. /// diff --git a/llvm/lib/IR/DIBuilder.cpp b/llvm/lib/IR/DIBuilder.cpp index b49b4e4f3fd2d..76896b95ed755 100644 --- a/llvm/lib/IR/DIBuilder.cpp +++ b/llvm/lib/IR/DIBuilder.cpp @@ -960,22 +960,6 @@ DILexicalBlock *DIBuilder::createLexicalBlock(DIScope *Scope, DIFile *File, File, Line, Col); } -DbgInstPtr DIBuilder::insertDeclare(Value *Storage, DILocalVariable *VarInfo, - DIExpression *Expr, const DILocation *DL, - Instruction *InsertBefore) { - return insertDeclare(Storage, VarInfo, Expr, DL, InsertBefore->getParent(), - InsertBefore); -} - -DbgInstPtr DIBuilder::insertDeclare(Value *Storage, DILocalVariable *VarInfo, - DIExpression *Expr, const DILocation *DL, - BasicBlock *InsertAtEnd) { - // If this block already has a terminator then insert this intrinsic before - // the terminator. Otherwise, put it at the end of the block. - Instruction *InsertBefore = InsertAtEnd->getTerminator(); - return insertDeclare(Storage, VarInfo, Expr, DL, InsertAtEnd, InsertBefore); -} - DbgInstPtr DIBuilder::insertDbgAssign(Instruction *LinkedInstr, Value *Val, DILocalVariable *SrcVar, DIExpression *ValExpr, Value *Addr, @@ -988,11 +972,10 @@ DbgInstPtr DIBuilder::insertDbgAssign(Instruction *LinkedInstr, Value *Val, if (M.IsNewDbgInfoFormat) { DbgVariableRecord *DVR = DbgVariableRecord::createDVRAssign( Val, SrcVar, ValExpr, Link, Addr, AddrExpr, DL); - BasicBlock *InsertBB = LinkedInstr->getParent(); // Insert after LinkedInstr. BasicBlock::iterator NextIt = std::next(LinkedInstr->getIterator()); - Instruction *InsertBefore = NextIt == InsertBB->end() ? nullptr : &*NextIt; - insertDbgVariableRecord(DVR, InsertBB, InsertBefore, true); + NextIt.setHeadBit(true); + insertDbgVariableRecord(DVR, NextIt); return DVR; } @@ -1018,47 +1001,11 @@ DbgInstPtr DIBuilder::insertDbgAssign(Instruction *LinkedInstr, Value *Val, return DVI; } -DbgInstPtr DIBuilder::insertLabel(DILabel *LabelInfo, const DILocation *DL, - Instruction *InsertBefore) { - return insertLabel(LabelInfo, DL, - InsertBefore ? InsertBefore->getParent() : nullptr, - InsertBefore); -} - -DbgInstPtr DIBuilder::insertLabel(DILabel *LabelInfo, const DILocation *DL, - BasicBlock *InsertAtEnd) { - return insertLabel(LabelInfo, DL, InsertAtEnd, nullptr); -} - -DbgInstPtr DIBuilder::insertDbgValueIntrinsic(Value *V, - DILocalVariable *VarInfo, - DIExpression *Expr, - const DILocation *DL, - Instruction *InsertBefore) { - DbgInstPtr DVI = insertDbgValueIntrinsic( - V, VarInfo, Expr, DL, InsertBefore ? InsertBefore->getParent() : nullptr, - InsertBefore); - if (auto *Inst = dyn_cast(DVI)) - cast(Inst)->setTailCall(); - return DVI; -} - -DbgInstPtr DIBuilder::insertDbgValueIntrinsic(Value *V, - DILocalVariable *VarInfo, - DIExpression *Expr, - const DILocation *DL, - BasicBlock *InsertAtEnd) { - return insertDbgValueIntrinsic(V, VarInfo, Expr, DL, InsertAtEnd, nullptr); -} - /// 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) - Builder.SetInsertPoint(InsertBefore); - else if (InsertBB) - Builder.SetInsertPoint(InsertBB); + InsertPosition InsertPt) { + Builder.SetInsertPoint(InsertPt.getBasicBlock(), InsertPt); Builder.SetCurrentDebugLocation(DL); } @@ -1071,26 +1018,28 @@ static Function *getDeclareIntrin(Module &M) { return Intrinsic::getOrInsertDeclaration(&M, Intrinsic::dbg_declare); } -DbgInstPtr DIBuilder::insertDbgValueIntrinsic( - llvm::Value *Val, DILocalVariable *VarInfo, DIExpression *Expr, - const DILocation *DL, BasicBlock *InsertBB, Instruction *InsertBefore) { +DbgInstPtr DIBuilder::insertDbgValueIntrinsic(llvm::Value *Val, + DILocalVariable *VarInfo, + DIExpression *Expr, + const DILocation *DL, + InsertPosition InsertPt) { if (M.IsNewDbgInfoFormat) { DbgVariableRecord *DVR = DbgVariableRecord::createDbgVariableRecord(Val, VarInfo, Expr, DL); - insertDbgVariableRecord(DVR, InsertBB, InsertBefore); + insertDbgVariableRecord(DVR, InsertPt); return DVR; } if (!ValueFn) ValueFn = Intrinsic::getOrInsertDeclaration(&M, Intrinsic::dbg_value); - return insertDbgIntrinsic(ValueFn, Val, VarInfo, Expr, DL, InsertBB, - InsertBefore); + auto *DVI = insertDbgIntrinsic(ValueFn, Val, VarInfo, Expr, DL, InsertPt); + cast(DVI)->setTailCall(); + return DVI; } DbgInstPtr DIBuilder::insertDeclare(Value *Storage, DILocalVariable *VarInfo, DIExpression *Expr, const DILocation *DL, - BasicBlock *InsertBB, - Instruction *InsertBefore) { + InsertPosition InsertPt) { assert(VarInfo && "empty or invalid DILocalVariable* passed to dbg.declare"); assert(DL && "Expected debug loc"); assert(DL->getScope()->getSubprogram() == @@ -1100,7 +1049,7 @@ DbgInstPtr DIBuilder::insertDeclare(Value *Storage, DILocalVariable *VarInfo, if (M.IsNewDbgInfoFormat) { DbgVariableRecord *DVR = DbgVariableRecord::createDVRDeclare(Storage, VarInfo, Expr, DL); - insertDbgVariableRecord(DVR, InsertBB, InsertBefore); + insertDbgVariableRecord(DVR, InsertPt); return DVR; } @@ -1114,35 +1063,27 @@ DbgInstPtr DIBuilder::insertDeclare(Value *Storage, DILocalVariable *VarInfo, MetadataAsValue::get(VMContext, Expr)}; IRBuilder<> B(DL->getContext()); - initIRBuilder(B, DL, InsertBB, InsertBefore); + initIRBuilder(B, DL, InsertPt); return B.CreateCall(DeclareFn, Args); } void DIBuilder::insertDbgVariableRecord(DbgVariableRecord *DVR, - BasicBlock *InsertBB, - Instruction *InsertBefore, - bool InsertAtHead) { - assert(InsertBefore || InsertBB); + InsertPosition InsertPt) { + assert(InsertPt.isValid()); trackIfUnresolved(DVR->getVariable()); trackIfUnresolved(DVR->getExpression()); if (DVR->isDbgAssign()) trackIfUnresolved(DVR->getAddressExpression()); - BasicBlock::iterator InsertPt; - if (InsertBB && InsertBefore) - InsertPt = InsertBefore->getIterator(); - else if (InsertBB) - InsertPt = InsertBB->end(); - InsertPt.setHeadBit(InsertAtHead); - InsertBB->insertDbgRecordBefore(DVR, InsertPt); + auto *BB = InsertPt.getBasicBlock(); + BB->insertDbgRecordBefore(DVR, InsertPt); } Instruction *DIBuilder::insertDbgIntrinsic(llvm::Function *IntrinsicFn, Value *V, DILocalVariable *VarInfo, DIExpression *Expr, const DILocation *DL, - BasicBlock *InsertBB, - Instruction *InsertBefore) { + InsertPosition InsertPt) { assert(IntrinsicFn && "must pass a non-null intrinsic function"); assert(V && "must pass a value to a dbg intrinsic"); assert(VarInfo && @@ -1159,13 +1100,12 @@ Instruction *DIBuilder::insertDbgIntrinsic(llvm::Function *IntrinsicFn, MetadataAsValue::get(VMContext, Expr)}; IRBuilder<> B(DL->getContext()); - initIRBuilder(B, DL, InsertBB, InsertBefore); + initIRBuilder(B, DL, InsertPt); return B.CreateCall(IntrinsicFn, Args); } DbgInstPtr DIBuilder::insertLabel(DILabel *LabelInfo, const DILocation *DL, - BasicBlock *InsertBB, - Instruction *InsertBefore) { + InsertPosition InsertPt) { assert(LabelInfo && "empty or invalid DILabel* passed to dbg.label"); assert(DL && "Expected debug loc"); assert(DL->getScope()->getSubprogram() == @@ -1175,10 +1115,10 @@ DbgInstPtr DIBuilder::insertLabel(DILabel *LabelInfo, const DILocation *DL, trackIfUnresolved(LabelInfo); if (M.IsNewDbgInfoFormat) { DbgLabelRecord *DLR = new DbgLabelRecord(LabelInfo, DL); - if (InsertBB && InsertBefore) - InsertBB->insertDbgRecordBefore(DLR, InsertBefore->getIterator()); - else if (InsertBB) - InsertBB->insertDbgRecordBefore(DLR, InsertBB->end()); + if (InsertPt.isValid()) { + auto *BB = InsertPt.getBasicBlock(); + BB->insertDbgRecordBefore(DLR, InsertPt); + } return DLR; } @@ -1188,7 +1128,7 @@ DbgInstPtr DIBuilder::insertLabel(DILabel *LabelInfo, const DILocation *DL, Value *Args[] = {MetadataAsValue::get(VMContext, LabelInfo)}; IRBuilder<> B(DL->getContext()); - initIRBuilder(B, DL, InsertBB, InsertBefore); + initIRBuilder(B, DL, InsertPt); return B.CreateCall(LabelFn, Args); } diff --git a/llvm/lib/IR/DebugInfo.cpp b/llvm/lib/IR/DebugInfo.cpp index ea1d79d436041..67c43c1750ed9 100644 --- a/llvm/lib/IR/DebugInfo.cpp +++ b/llvm/lib/IR/DebugInfo.cpp @@ -1690,7 +1690,8 @@ LLVMDbgRecordRef LLVMDIBuilderInsertDeclareRecordBefore( DbgInstPtr DbgInst = unwrap(Builder)->insertDeclare( unwrap(Storage), unwrap(VarInfo), unwrap(Expr), unwrap(DL), - unwrap(Instr)); + Instr ? InsertPosition(unwrap(Instr)->getIterator()) + : nullptr); // This assert will fail if the module is in the old debug info format. // This function should only be called if the module is in the new // debug info format. @@ -1706,7 +1707,8 @@ LLVMDbgRecordRef LLVMDIBuilderInsertDeclareRecordAtEnd( LLVMMetadataRef Expr, LLVMMetadataRef DL, LLVMBasicBlockRef Block) { DbgInstPtr DbgInst = unwrap(Builder)->insertDeclare( unwrap(Storage), unwrap(VarInfo), - unwrap(Expr), unwrap(DL), unwrap(Block)); + unwrap(Expr), unwrap(DL), + Block ? InsertPosition(unwrap(Block)->end()) : nullptr); // This assert will fail if the module is in the old debug info format. // This function should only be called if the module is in the new // debug info format. @@ -1722,7 +1724,9 @@ LLVMDbgRecordRef LLVMDIBuilderInsertDbgValueRecordBefore( LLVMMetadataRef Expr, LLVMMetadataRef DebugLoc, LLVMValueRef Instr) { DbgInstPtr DbgInst = unwrap(Builder)->insertDbgValueIntrinsic( unwrap(Val), unwrap(VarInfo), unwrap(Expr), - unwrap(DebugLoc), unwrap(Instr)); + unwrap(DebugLoc), + Instr ? InsertPosition(unwrap(Instr)->getIterator()) + : nullptr); // This assert will fail if the module is in the old debug info format. // This function should only be called if the module is in the new // debug info format. @@ -1738,7 +1742,8 @@ LLVMDbgRecordRef LLVMDIBuilderInsertDbgValueRecordAtEnd( LLVMMetadataRef Expr, LLVMMetadataRef DebugLoc, LLVMBasicBlockRef Block) { DbgInstPtr DbgInst = unwrap(Builder)->insertDbgValueIntrinsic( unwrap(Val), unwrap(VarInfo), unwrap(Expr), - unwrap(DebugLoc), unwrap(Block)); + unwrap(DebugLoc), + Block ? InsertPosition(unwrap(Block)->end()) : nullptr); // This assert will fail if the module is in the old debug info format. // This function should only be called if the module is in the new // debug info format. @@ -1809,16 +1814,19 @@ LLVMMetadataRef LLVMDIBuilderCreateLabel( LLVMMetadataRef Context, const char *Name, size_t NameLen, LLVMMetadataRef File, unsigned LineNo, LLVMBool AlwaysPreserve) { return wrap(unwrap(Builder)->createLabel( - unwrapDI(Context), StringRef(Name, NameLen), - unwrapDI(File), LineNo, AlwaysPreserve)); + unwrapDI(Context), StringRef(Name, NameLen), + unwrapDI(File), LineNo, AlwaysPreserve)); } -LLVMDbgRecordRef LLVMDIBuilderInsertLabelBefore( - LLVMDIBuilderRef Builder, LLVMMetadataRef LabelInfo, - LLVMMetadataRef Location, LLVMValueRef InsertBefore) { +LLVMDbgRecordRef LLVMDIBuilderInsertLabelBefore(LLVMDIBuilderRef Builder, + LLVMMetadataRef LabelInfo, + LLVMMetadataRef Location, + LLVMValueRef InsertBefore) { DbgInstPtr DbgInst = unwrap(Builder)->insertLabel( - unwrapDI(LabelInfo), unwrapDI(Location), - unwrap(InsertBefore)); + unwrapDI(LabelInfo), unwrapDI(Location), + InsertBefore + ? InsertPosition(unwrap(InsertBefore)->getIterator()) + : nullptr); // This assert will fail if the module is in the old debug info format. // This function should only be called if the module is in the new // debug info format. @@ -1829,12 +1837,13 @@ LLVMDbgRecordRef LLVMDIBuilderInsertLabelBefore( return wrap(cast(DbgInst)); } -LLVMDbgRecordRef LLVMDIBuilderInsertLabelAtEnd( - LLVMDIBuilderRef Builder, LLVMMetadataRef LabelInfo, - LLVMMetadataRef Location, LLVMBasicBlockRef InsertAtEnd) { +LLVMDbgRecordRef LLVMDIBuilderInsertLabelAtEnd(LLVMDIBuilderRef Builder, + LLVMMetadataRef LabelInfo, + LLVMMetadataRef Location, + LLVMBasicBlockRef InsertAtEnd) { DbgInstPtr DbgInst = unwrap(Builder)->insertLabel( - unwrapDI(LabelInfo), unwrapDI(Location), - unwrap(InsertAtEnd)); + unwrapDI(LabelInfo), unwrapDI(Location), + InsertAtEnd ? InsertPosition(unwrap(InsertAtEnd)->end()) : nullptr); // This assert will fail if the module is in the old debug info format. // This function should only be called if the module is in the new // debug info format. diff --git a/llvm/lib/Transforms/Coroutines/CoroFrame.cpp b/llvm/lib/Transforms/Coroutines/CoroFrame.cpp index 4104e4e533e9d..62fa3af502e29 100644 --- a/llvm/lib/Transforms/Coroutines/CoroFrame.cpp +++ b/llvm/lib/Transforms/Coroutines/CoroFrame.cpp @@ -851,7 +851,7 @@ static void buildFrameDebugInfo(Function &F, coro::Shape &Shape, } else { DBuilder.insertDeclare(Shape.FramePtr, FrameDIVar, DBuilder.createExpression(), DILoc, - &*Shape.getInsertPtAfterFramePtr()); + Shape.getInsertPtAfterFramePtr()); } } @@ -1146,7 +1146,7 @@ static void insertSpills(const FrameDataInfo &FrameData, coro::Shape &Shape) { DIBuilder(*CurrentBlock->getParent()->getParent(), AllowUnresolved) .insertDeclare(CurrentReload, DDI->getVariable(), DDI->getExpression(), DDI->getDebugLoc(), - &*Builder.GetInsertPoint()); + Builder.GetInsertPoint()); } // This dbg.declare is for the main function entry point. It // will be deleted in all coro-split functions. diff --git a/llvm/lib/Transforms/Scalar/SROA.cpp b/llvm/lib/Transforms/Scalar/SROA.cpp index 29240aaaa21be..e88c130cccf20 100644 --- a/llvm/lib/Transforms/Scalar/SROA.cpp +++ b/llvm/lib/Transforms/Scalar/SROA.cpp @@ -5154,7 +5154,7 @@ insertNewDbgInst(DIBuilder &DIB, DbgDeclareInst *Orig, AllocaInst *NewAddr, return; DIB.insertDeclare(NewAddr, Orig->getVariable(), NewAddrExpr, - Orig->getDebugLoc(), BeforeInst); + Orig->getDebugLoc(), BeforeInst->getIterator()); } /// Insert a new dbg.assign. diff --git a/llvm/lib/Transforms/Utils/Debugify.cpp b/llvm/lib/Transforms/Utils/Debugify.cpp index e5e2aa6556930..e47a6ce6e9205 100644 --- a/llvm/lib/Transforms/Utils/Debugify.cpp +++ b/llvm/lib/Transforms/Utils/Debugify.cpp @@ -127,7 +127,7 @@ bool llvm::applyDebugifyMetadata( // Helper that inserts a dbg.value before \p InsertBefore, copying the // location (and possibly the type, if it's non-void) from \p TemplateInst. auto insertDbgVal = [&](Instruction &TemplateInst, - Instruction *InsertBefore) { + BasicBlock::iterator InsertPt) { std::string Name = utostr(NextVar++); Value *V = &TemplateInst; if (TemplateInst.getType()->isVoidTy()) @@ -137,7 +137,7 @@ bool llvm::applyDebugifyMetadata( getCachedDIType(V->getType()), /*AlwaysPreserve=*/true); DIB.insertDbgValueIntrinsic(V, LocalVar, DIB.createExpression(), Loc, - InsertBefore); + InsertPt); }; for (BasicBlock &BB : F) { @@ -161,7 +161,9 @@ bool llvm::applyDebugifyMetadata( // are made. BasicBlock::iterator InsertPt = BB.getFirstInsertionPt(); assert(InsertPt != BB.end() && "Expected to find an insertion point"); - Instruction *InsertBefore = &*InsertPt; + + // Insert after existing debug values to preserve order. + InsertPt.setHeadBit(false); // Attach debug values. for (Instruction *I = &*BB.begin(); I != LastInst; I = I->getNextNode()) { @@ -172,9 +174,9 @@ bool llvm::applyDebugifyMetadata( // Phis and EH pads must be grouped at the beginning of the block. // Only advance the insertion point when we finish visiting these. if (!isa(I) && !I->isEHPad()) - InsertBefore = I->getNextNode(); + InsertPt = std::next(I->getIterator()); - insertDbgVal(*I, InsertBefore); + insertDbgVal(*I, InsertPt); InsertedDbgVal = true; } } @@ -185,7 +187,7 @@ bool llvm::applyDebugifyMetadata( // those tests, and this helps with that.) if (DebugifyLevel == Level::LocationsAndVariables && !InsertedDbgVal) { auto *Term = findTerminatingInstruction(F.getEntryBlock()); - insertDbgVal(*Term, Term); + insertDbgVal(*Term, Term->getIterator()); } if (ApplyToMF) ApplyToMF(DIB, F); diff --git a/llvm/lib/Transforms/Utils/Local.cpp b/llvm/lib/Transforms/Utils/Local.cpp index 2c6328300738f..6d7c710020c3e 100644 --- a/llvm/lib/Transforms/Utils/Local.cpp +++ b/llvm/lib/Transforms/Utils/Local.cpp @@ -1693,9 +1693,7 @@ static void insertDbgValueOrDbgVariableRecord(DIBuilder &Builder, Value *DV, const DebugLoc &NewLoc, BasicBlock::iterator Instr) { if (!UseNewDbgInfoFormat) { - auto DbgVal = Builder.insertDbgValueIntrinsic(DV, DIVar, DIExpr, NewLoc, - (Instruction *)nullptr); - cast(DbgVal)->insertBefore(Instr); + Builder.insertDbgValueIntrinsic(DV, DIVar, DIExpr, NewLoc, Instr); } else { // RemoveDIs: if we're using the new debug-info format, allocate a // DbgVariableRecord directly instead of a dbg.value intrinsic. @@ -1708,19 +1706,10 @@ static void insertDbgValueOrDbgVariableRecord(DIBuilder &Builder, Value *DV, static void insertDbgValueOrDbgVariableRecordAfter( DIBuilder &Builder, Value *DV, DILocalVariable *DIVar, DIExpression *DIExpr, - const DebugLoc &NewLoc, BasicBlock::iterator Instr) { - if (!UseNewDbgInfoFormat) { - auto DbgVal = Builder.insertDbgValueIntrinsic(DV, DIVar, DIExpr, NewLoc, - (Instruction *)nullptr); - cast(DbgVal)->insertAfter(Instr); - } else { - // RemoveDIs: if we're using the new debug-info format, allocate a - // DbgVariableRecord directly instead of a dbg.value intrinsic. - ValueAsMetadata *DVAM = ValueAsMetadata::get(DV); - DbgVariableRecord *DV = - new DbgVariableRecord(DVAM, DIVar, DIExpr, NewLoc.get()); - Instr->getParent()->insertDbgRecordAfter(DV, &*Instr); - } + const DebugLoc &NewLoc, Instruction *Instr) { + BasicBlock::iterator NextIt = std::next(Instr->getIterator()); + NextIt.setHeadBit(true); + insertDbgValueOrDbgVariableRecord(Builder, DV, DIVar, DIExpr, NewLoc, NextIt); } /// Inserts a llvm.dbg.value intrinsic before a store to an alloca'd value @@ -1812,7 +1801,7 @@ void llvm::ConvertDebugDeclareToDebugValue(DbgVariableIntrinsic *DII, // preferable to keep tracking both the loaded value and the original // address in case the alloca can not be elided. insertDbgValueOrDbgVariableRecordAfter(Builder, LI, DIVar, DIExpr, NewLoc, - LI->getIterator()); + LI); } void llvm::ConvertDebugDeclareToDebugValue(DbgVariableRecord *DVR, diff --git a/llvm/lib/Transforms/Utils/PromoteMemoryToRegister.cpp b/llvm/lib/Transforms/Utils/PromoteMemoryToRegister.cpp index 016186cb6b09d..05fd989271c32 100644 --- a/llvm/lib/Transforms/Utils/PromoteMemoryToRegister.cpp +++ b/llvm/lib/Transforms/Utils/PromoteMemoryToRegister.cpp @@ -119,7 +119,8 @@ static void createDebugValue(DIBuilder &DIB, Value *NewValue, DILocalVariable *Variable, DIExpression *Expression, const DILocation *DI, Instruction *InsertBefore) { - DIB.insertDbgValueIntrinsic(NewValue, Variable, Expression, DI, InsertBefore); + DIB.insertDbgValueIntrinsic(NewValue, Variable, Expression, DI, + InsertBefore->getIterator()); } /// Helper for updating assignment tracking debug info when promoting allocas. diff --git a/llvm/unittests/IR/IRBuilderTest.cpp b/llvm/unittests/IR/IRBuilderTest.cpp index 2fd52860e71b9..a49c6b8a0b474 100644 --- a/llvm/unittests/IR/IRBuilderTest.cpp +++ b/llvm/unittests/IR/IRBuilderTest.cpp @@ -923,12 +923,13 @@ TEST_F(IRBuilderTest, DIBuilder) { { /* dbg.label | DbgLabelRecord */ // Insert before I and check order. - ExpectOrder(DIB.insertLabel(Label, LabelLoc, I), I->getIterator()); + ExpectOrder(DIB.insertLabel(Label, LabelLoc, I->getIterator()), + I->getIterator()); // We should be able to insert at the end of the block, even if there's // no terminator yet. Note that in RemoveDIs mode this record won't get // inserted into the block untill another instruction is added. - DbgInstPtr LabelRecord = DIB.insertLabel(Label, LabelLoc, BB); + DbgInstPtr LabelRecord = DIB.insertLabel(Label, LabelLoc, BB->end()); // Specifically do not insert a terminator, to check this works. `I` // should have absorbed the DbgLabelRecord in the new debug info mode. I = Builder.CreateAlloca(Builder.getInt32Ty()); @@ -945,7 +946,7 @@ TEST_F(IRBuilderTest, DIBuilder) { DIB.createAutoVariable(BarSP, "Y", File, 2, IntType, true); { /* dbg.value | DbgVariableRecord::Value */ ExpectOrder(DIB.insertDbgValueIntrinsic(I, VarX, DIB.createExpression(), - VarLoc, I), + VarLoc, I->getIterator()), I->getIterator()); // Check inserting at end of the block works as with labels. DbgInstPtr VarXValue = DIB.insertDbgValueIntrinsic( @@ -955,11 +956,12 @@ TEST_F(IRBuilderTest, DIBuilder) { EXPECT_EQ(BB->getTrailingDbgRecords(), nullptr); } { /* dbg.declare | DbgVariableRecord::Declare */ - ExpectOrder(DIB.insertDeclare(I, VarY, DIB.createExpression(), VarLoc, I), + ExpectOrder(DIB.insertDeclare(I, VarY, DIB.createExpression(), VarLoc, + I->getIterator()), I->getIterator()); // Check inserting at end of the block works as with labels. DbgInstPtr VarYDeclare = - DIB.insertDeclare(I, VarY, DIB.createExpression(), VarLoc, BB); + DIB.insertDeclare(I, VarY, DIB.createExpression(), VarLoc, BB->end()); I = Builder.CreateAlloca(Builder.getInt32Ty()); ExpectOrder(VarYDeclare, I->getIterator()); EXPECT_EQ(BB->getTrailingDbgRecords(), nullptr); diff --git a/llvm/unittests/Transforms/Utils/CloningTest.cpp b/llvm/unittests/Transforms/Utils/CloningTest.cpp index f2b73c282b764..03769ff59e372 100644 --- a/llvm/unittests/Transforms/Utils/CloningTest.cpp +++ b/llvm/unittests/Transforms/Utils/CloningTest.cpp @@ -508,7 +508,7 @@ class CloneFunc : public ::testing::Test { auto *Variable = DBuilder.createAutoVariable(Subprogram, "x", File, 5, IntType, true); auto *DL = DILocation::get(Subprogram->getContext(), 5, 0, Subprogram); - DBuilder.insertDeclare(Alloca, Variable, E, DL, Store); + DBuilder.insertDeclare(Alloca, Variable, E, DL, Store->getIterator()); DBuilder.insertDbgValueIntrinsic(AllocaContent, Variable, E, DL, Entry); // Also create an inlined variable. // Create a distinct struct type that we should not duplicate during @@ -528,7 +528,8 @@ class CloneFunc : public ::testing::Test { Subprogram->getContext(), 9, 4, Scope, DILocation::get(Subprogram->getContext(), 5, 2, Subprogram)); IBuilder.SetCurrentDebugLocation(InlinedDL); - DBuilder.insertDeclare(Alloca, InlinedVar, E, InlinedDL, Store); + DBuilder.insertDeclare(Alloca, InlinedVar, E, InlinedDL, + Store->getIterator()); IBuilder.CreateStore(IBuilder.getInt32(2), Alloca); // Finalize the debug info. DBuilder.finalize();