From 1083ec647f16314bcc9af8c4d6b11f50d288bca6 Mon Sep 17 00:00:00 2001 From: Harald van Dijk Date: Thu, 13 Feb 2025 10:46:42 +0000 Subject: [reland][DebugInfo] Update DIBuilder insertion to take InsertPosition (#126967) 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. As a special exception, DIBuilder::insertDeclare() keeps a separate overload accepting a BasicBlock *InsertAtEnd. This is because despite the name, this method does not insert at the end of the block, therefore this cannot be handled implicitly by using InsertPosition. --- llvm/lib/IR/DIBuilder.cpp | 111 +++++++++++++--------------------------------- 1 file changed, 31 insertions(+), 80 deletions(-) (limited to 'llvm/lib/IR/DIBuilder.cpp') diff --git a/llvm/lib/IR/DIBuilder.cpp b/llvm/lib/IR/DIBuilder.cpp index b49b4e4..bbe4d1f 100644 --- a/llvm/lib/IR/DIBuilder.cpp +++ b/llvm/lib/IR/DIBuilder.cpp @@ -962,18 +962,13 @@ DILexicalBlock *DIBuilder::createLexicalBlock(DIScope *Scope, DIFile *File, 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); + return insertDeclare(Storage, VarInfo, Expr, DL, + InsertBefore ? InsertBefore->getIterator() + : InsertAtEnd->end()); } DbgInstPtr DIBuilder::insertDbgAssign(Instruction *LinkedInstr, Value *Val, @@ -988,11 +983,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 +1012,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 +1029,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 +1060,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 +1074,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 +1111,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 +1126,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 +1139,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); } -- cgit v1.1