Skip to content

Commit 3ec9f74

Browse files
authored
[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.
1 parent f8c7457 commit 3ec9f74

File tree

11 files changed

+99
-200
lines changed

11 files changed

+99
-200
lines changed

clang/lib/CodeGen/CGDebugInfo.cpp

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4901,7 +4901,7 @@ llvm::DILocalVariable *CGDebugInfo::EmitDeclare(const VarDecl *VD,
49014901
llvm::DILocation::get(CGM.getLLVMContext(), Line,
49024902
Column, Scope,
49034903
CurInlinedAt),
4904-
Builder.GetInsertBlock());
4904+
Builder.GetInsertBlock()->end());
49054905
}
49064906
}
49074907
}
@@ -4969,7 +4969,7 @@ llvm::DILocalVariable *CGDebugInfo::EmitDeclare(const VarDecl *VD,
49694969
DBuilder.insertDeclare(Storage, D, DBuilder.createExpression(Expr),
49704970
llvm::DILocation::get(CGM.getLLVMContext(), Line,
49714971
Column, Scope, CurInlinedAt),
4972-
Builder.GetInsertBlock());
4972+
Builder.GetInsertBlock()->end());
49734973

49744974
return D;
49754975
}
@@ -5075,7 +5075,7 @@ llvm::DILocalVariable *CGDebugInfo::EmitDeclare(const BindingDecl *BD,
50755075
DBuilder.insertDeclare(Storage, D, DBuilder.createExpression(Expr),
50765076
llvm::DILocation::get(CGM.getLLVMContext(), Line,
50775077
Column, Scope, CurInlinedAt),
5078-
Builder.GetInsertBlock());
5078+
Builder.GetInsertBlock()->end());
50795079

50805080
return D;
50815081
}
@@ -5122,7 +5122,7 @@ void CGDebugInfo::EmitLabel(const LabelDecl *D, CGBuilderTy &Builder) {
51225122
DBuilder.insertLabel(L,
51235123
llvm::DILocation::get(CGM.getLLVMContext(), Line, Column,
51245124
Scope, CurInlinedAt),
5125-
Builder.GetInsertBlock());
5125+
Builder.GetInsertBlock()->end());
51265126
}
51275127

51285128
llvm::DIType *CGDebugInfo::CreateSelfType(const QualType &QualTy,
@@ -5200,9 +5200,10 @@ void CGDebugInfo::EmitDeclareOfBlockDeclRefVariable(
52005200
LexicalBlockStack.back(), CurInlinedAt);
52015201
auto *Expr = DBuilder.createExpression(addr);
52025202
if (InsertPoint)
5203-
DBuilder.insertDeclare(Storage, D, Expr, DL, InsertPoint);
5203+
DBuilder.insertDeclare(Storage, D, Expr, DL, InsertPoint->getIterator());
52045204
else
5205-
DBuilder.insertDeclare(Storage, D, Expr, DL, Builder.GetInsertBlock());
5205+
DBuilder.insertDeclare(Storage, D, Expr, DL,
5206+
Builder.GetInsertBlock()->end());
52065207
}
52075208

52085209
llvm::DILocalVariable *
@@ -5385,7 +5386,7 @@ void CGDebugInfo::EmitDeclareOfBlockLiteralArgVariable(const CGBlockInfo &block,
53855386
DBuilder.insertDeclare(Alloca, debugVar, DBuilder.createExpression(),
53865387
llvm::DILocation::get(CGM.getLLVMContext(), line,
53875388
column, scope, CurInlinedAt),
5388-
Builder.GetInsertBlock());
5389+
Builder.GetInsertBlock()->end());
53895390
}
53905391

53915392
llvm::DIDerivedType *
@@ -5865,7 +5866,7 @@ void CGDebugInfo::EmitPseudoVariable(CGBuilderTy &Builder,
58655866

58665867
if (auto InsertPoint = Value->getInsertionPointAfterDef()) {
58675868
DBuilder.insertDbgValueIntrinsic(Value, D, DBuilder.createExpression(), DIL,
5868-
&**InsertPoint);
5869+
*InsertPoint);
58695870
}
58705871
}
58715872

llvm/include/llvm/IR/DIBuilder.h

Lines changed: 8 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -92,33 +92,15 @@ namespace llvm {
9292
/// Create an \a temporary node and track it in \a UnresolvedNodes.
9393
void trackIfUnresolved(MDNode *N);
9494

95-
/// Internal helper for insertDeclare.
96-
DbgInstPtr insertDeclare(llvm::Value *Storage, DILocalVariable *VarInfo,
97-
DIExpression *Expr, const DILocation *DL,
98-
BasicBlock *InsertBB, Instruction *InsertBefore);
99-
100-
/// Internal helper for insertLabel.
101-
DbgInstPtr insertLabel(DILabel *LabelInfo, const DILocation *DL,
102-
BasicBlock *InsertBB, Instruction *InsertBefore);
103-
10495
/// Internal helper. Track metadata if untracked and insert \p DVR.
105-
void insertDbgVariableRecord(DbgVariableRecord *DVR, BasicBlock *InsertBB,
106-
Instruction *InsertBefore,
107-
bool InsertAtHead = false);
96+
void insertDbgVariableRecord(DbgVariableRecord *DVR,
97+
InsertPosition InsertPt);
10898

10999
/// Internal helper with common code used by insertDbg{Value,Addr}Intrinsic.
110100
Instruction *insertDbgIntrinsic(llvm::Function *Intrinsic, llvm::Value *Val,
111101
DILocalVariable *VarInfo,
112102
DIExpression *Expr, const DILocation *DL,
113-
BasicBlock *InsertBB,
114-
Instruction *InsertBefore);
115-
116-
/// Internal helper for insertDbgValueIntrinsic.
117-
DbgInstPtr insertDbgValueIntrinsic(llvm::Value *Val,
118-
DILocalVariable *VarInfo,
119-
DIExpression *Expr, const DILocation *DL,
120-
BasicBlock *InsertBB,
121-
Instruction *InsertBefore);
103+
InsertPosition InsertPt);
122104

123105
public:
124106
/// Construct a builder for a module.
@@ -961,16 +943,6 @@ namespace llvm {
961943
StringRef Name = "",
962944
DINodeArray Elements = nullptr);
963945

964-
/// Insert a new llvm.dbg.declare intrinsic call.
965-
/// \param Storage llvm::Value of the variable
966-
/// \param VarInfo Variable's debug info descriptor.
967-
/// \param Expr A complex location expression.
968-
/// \param DL Debug info location.
969-
/// \param InsertAtEnd Location for the new intrinsic.
970-
DbgInstPtr insertDeclare(llvm::Value *Storage, DILocalVariable *VarInfo,
971-
DIExpression *Expr, const DILocation *DL,
972-
BasicBlock *InsertAtEnd);
973-
974946
/// Insert a new llvm.dbg.assign intrinsic call.
975947
/// \param LinkedInstr Instruction with a DIAssignID to link with the new
976948
/// intrinsic. The intrinsic will be inserted after
@@ -995,46 +967,28 @@ namespace llvm {
995967
/// \param VarInfo Variable's debug info descriptor.
996968
/// \param Expr A complex location expression.
997969
/// \param DL Debug info location.
998-
/// \param InsertBefore Location for the new intrinsic.
970+
/// \param InsertPt Location for the new intrinsic.
999971
DbgInstPtr insertDeclare(llvm::Value *Storage, DILocalVariable *VarInfo,
1000972
DIExpression *Expr, const DILocation *DL,
1001-
Instruction *InsertBefore);
973+
InsertPosition InsertPt);
1002974

1003975
/// Insert a new llvm.dbg.label intrinsic call.
1004976
/// \param LabelInfo Label's debug info descriptor.
1005977
/// \param DL Debug info location.
1006978
/// \param InsertBefore Location for the new intrinsic.
1007979
DbgInstPtr insertLabel(DILabel *LabelInfo, const DILocation *DL,
1008-
Instruction *InsertBefore);
1009-
1010-
/// Insert a new llvm.dbg.label intrinsic call.
1011-
/// \param LabelInfo Label's debug info descriptor.
1012-
/// \param DL Debug info location.
1013-
/// \param InsertAtEnd Location for the new intrinsic.
1014-
DbgInstPtr insertLabel(DILabel *LabelInfo, const DILocation *DL,
1015-
BasicBlock *InsertAtEnd);
1016-
1017-
/// Insert a new llvm.dbg.value intrinsic call.
1018-
/// \param Val llvm::Value of the variable
1019-
/// \param VarInfo Variable's debug info descriptor.
1020-
/// \param Expr A complex location expression.
1021-
/// \param DL Debug info location.
1022-
/// \param InsertAtEnd Location for the new intrinsic.
1023-
DbgInstPtr insertDbgValueIntrinsic(llvm::Value *Val,
1024-
DILocalVariable *VarInfo,
1025-
DIExpression *Expr, const DILocation *DL,
1026-
BasicBlock *InsertAtEnd);
980+
InsertPosition InsertPt);
1027981

1028982
/// Insert a new llvm.dbg.value intrinsic call.
1029983
/// \param Val llvm::Value of the variable
1030984
/// \param VarInfo Variable's debug info descriptor.
1031985
/// \param Expr A complex location expression.
1032986
/// \param DL Debug info location.
1033-
/// \param InsertBefore Location for the new intrinsic.
987+
/// \param InsertPt Location for the new intrinsic.
1034988
DbgInstPtr insertDbgValueIntrinsic(llvm::Value *Val,
1035989
DILocalVariable *VarInfo,
1036990
DIExpression *Expr, const DILocation *DL,
1037-
Instruction *InsertBefore);
991+
InsertPosition InsertPt);
1038992

1039993
/// Replace the vtable holder in the given type.
1040994
///

llvm/lib/IR/DIBuilder.cpp

Lines changed: 28 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -960,22 +960,6 @@ DILexicalBlock *DIBuilder::createLexicalBlock(DIScope *Scope, DIFile *File,
960960
File, Line, Col);
961961
}
962962

963-
DbgInstPtr DIBuilder::insertDeclare(Value *Storage, DILocalVariable *VarInfo,
964-
DIExpression *Expr, const DILocation *DL,
965-
Instruction *InsertBefore) {
966-
return insertDeclare(Storage, VarInfo, Expr, DL, InsertBefore->getParent(),
967-
InsertBefore);
968-
}
969-
970-
DbgInstPtr DIBuilder::insertDeclare(Value *Storage, DILocalVariable *VarInfo,
971-
DIExpression *Expr, const DILocation *DL,
972-
BasicBlock *InsertAtEnd) {
973-
// If this block already has a terminator then insert this intrinsic before
974-
// the terminator. Otherwise, put it at the end of the block.
975-
Instruction *InsertBefore = InsertAtEnd->getTerminator();
976-
return insertDeclare(Storage, VarInfo, Expr, DL, InsertAtEnd, InsertBefore);
977-
}
978-
979963
DbgInstPtr DIBuilder::insertDbgAssign(Instruction *LinkedInstr, Value *Val,
980964
DILocalVariable *SrcVar,
981965
DIExpression *ValExpr, Value *Addr,
@@ -988,11 +972,10 @@ DbgInstPtr DIBuilder::insertDbgAssign(Instruction *LinkedInstr, Value *Val,
988972
if (M.IsNewDbgInfoFormat) {
989973
DbgVariableRecord *DVR = DbgVariableRecord::createDVRAssign(
990974
Val, SrcVar, ValExpr, Link, Addr, AddrExpr, DL);
991-
BasicBlock *InsertBB = LinkedInstr->getParent();
992975
// Insert after LinkedInstr.
993976
BasicBlock::iterator NextIt = std::next(LinkedInstr->getIterator());
994-
Instruction *InsertBefore = NextIt == InsertBB->end() ? nullptr : &*NextIt;
995-
insertDbgVariableRecord(DVR, InsertBB, InsertBefore, true);
977+
NextIt.setHeadBit(true);
978+
insertDbgVariableRecord(DVR, NextIt);
996979
return DVR;
997980
}
998981

@@ -1018,47 +1001,11 @@ DbgInstPtr DIBuilder::insertDbgAssign(Instruction *LinkedInstr, Value *Val,
10181001
return DVI;
10191002
}
10201003

1021-
DbgInstPtr DIBuilder::insertLabel(DILabel *LabelInfo, const DILocation *DL,
1022-
Instruction *InsertBefore) {
1023-
return insertLabel(LabelInfo, DL,
1024-
InsertBefore ? InsertBefore->getParent() : nullptr,
1025-
InsertBefore);
1026-
}
1027-
1028-
DbgInstPtr DIBuilder::insertLabel(DILabel *LabelInfo, const DILocation *DL,
1029-
BasicBlock *InsertAtEnd) {
1030-
return insertLabel(LabelInfo, DL, InsertAtEnd, nullptr);
1031-
}
1032-
1033-
DbgInstPtr DIBuilder::insertDbgValueIntrinsic(Value *V,
1034-
DILocalVariable *VarInfo,
1035-
DIExpression *Expr,
1036-
const DILocation *DL,
1037-
Instruction *InsertBefore) {
1038-
DbgInstPtr DVI = insertDbgValueIntrinsic(
1039-
V, VarInfo, Expr, DL, InsertBefore ? InsertBefore->getParent() : nullptr,
1040-
InsertBefore);
1041-
if (auto *Inst = dyn_cast<Instruction *>(DVI))
1042-
cast<CallInst>(Inst)->setTailCall();
1043-
return DVI;
1044-
}
1045-
1046-
DbgInstPtr DIBuilder::insertDbgValueIntrinsic(Value *V,
1047-
DILocalVariable *VarInfo,
1048-
DIExpression *Expr,
1049-
const DILocation *DL,
1050-
BasicBlock *InsertAtEnd) {
1051-
return insertDbgValueIntrinsic(V, VarInfo, Expr, DL, InsertAtEnd, nullptr);
1052-
}
1053-
10541004
/// Initialize IRBuilder for inserting dbg.declare and dbg.value intrinsics.
10551005
/// This abstracts over the various ways to specify an insert position.
10561006
static void initIRBuilder(IRBuilder<> &Builder, const DILocation *DL,
1057-
BasicBlock *InsertBB, Instruction *InsertBefore) {
1058-
if (InsertBefore)
1059-
Builder.SetInsertPoint(InsertBefore);
1060-
else if (InsertBB)
1061-
Builder.SetInsertPoint(InsertBB);
1007+
InsertPosition InsertPt) {
1008+
Builder.SetInsertPoint(InsertPt.getBasicBlock(), InsertPt);
10621009
Builder.SetCurrentDebugLocation(DL);
10631010
}
10641011

@@ -1071,26 +1018,28 @@ static Function *getDeclareIntrin(Module &M) {
10711018
return Intrinsic::getOrInsertDeclaration(&M, Intrinsic::dbg_declare);
10721019
}
10731020

1074-
DbgInstPtr DIBuilder::insertDbgValueIntrinsic(
1075-
llvm::Value *Val, DILocalVariable *VarInfo, DIExpression *Expr,
1076-
const DILocation *DL, BasicBlock *InsertBB, Instruction *InsertBefore) {
1021+
DbgInstPtr DIBuilder::insertDbgValueIntrinsic(llvm::Value *Val,
1022+
DILocalVariable *VarInfo,
1023+
DIExpression *Expr,
1024+
const DILocation *DL,
1025+
InsertPosition InsertPt) {
10771026
if (M.IsNewDbgInfoFormat) {
10781027
DbgVariableRecord *DVR =
10791028
DbgVariableRecord::createDbgVariableRecord(Val, VarInfo, Expr, DL);
1080-
insertDbgVariableRecord(DVR, InsertBB, InsertBefore);
1029+
insertDbgVariableRecord(DVR, InsertPt);
10811030
return DVR;
10821031
}
10831032

10841033
if (!ValueFn)
10851034
ValueFn = Intrinsic::getOrInsertDeclaration(&M, Intrinsic::dbg_value);
1086-
return insertDbgIntrinsic(ValueFn, Val, VarInfo, Expr, DL, InsertBB,
1087-
InsertBefore);
1035+
auto *DVI = insertDbgIntrinsic(ValueFn, Val, VarInfo, Expr, DL, InsertPt);
1036+
cast<CallInst>(DVI)->setTailCall();
1037+
return DVI;
10881038
}
10891039

10901040
DbgInstPtr DIBuilder::insertDeclare(Value *Storage, DILocalVariable *VarInfo,
10911041
DIExpression *Expr, const DILocation *DL,
1092-
BasicBlock *InsertBB,
1093-
Instruction *InsertBefore) {
1042+
InsertPosition InsertPt) {
10941043
assert(VarInfo && "empty or invalid DILocalVariable* passed to dbg.declare");
10951044
assert(DL && "Expected debug loc");
10961045
assert(DL->getScope()->getSubprogram() ==
@@ -1100,7 +1049,7 @@ DbgInstPtr DIBuilder::insertDeclare(Value *Storage, DILocalVariable *VarInfo,
11001049
if (M.IsNewDbgInfoFormat) {
11011050
DbgVariableRecord *DVR =
11021051
DbgVariableRecord::createDVRDeclare(Storage, VarInfo, Expr, DL);
1103-
insertDbgVariableRecord(DVR, InsertBB, InsertBefore);
1052+
insertDbgVariableRecord(DVR, InsertPt);
11041053
return DVR;
11051054
}
11061055

@@ -1114,35 +1063,27 @@ DbgInstPtr DIBuilder::insertDeclare(Value *Storage, DILocalVariable *VarInfo,
11141063
MetadataAsValue::get(VMContext, Expr)};
11151064

11161065
IRBuilder<> B(DL->getContext());
1117-
initIRBuilder(B, DL, InsertBB, InsertBefore);
1066+
initIRBuilder(B, DL, InsertPt);
11181067
return B.CreateCall(DeclareFn, Args);
11191068
}
11201069

11211070
void DIBuilder::insertDbgVariableRecord(DbgVariableRecord *DVR,
1122-
BasicBlock *InsertBB,
1123-
Instruction *InsertBefore,
1124-
bool InsertAtHead) {
1125-
assert(InsertBefore || InsertBB);
1071+
InsertPosition InsertPt) {
1072+
assert(InsertPt.isValid());
11261073
trackIfUnresolved(DVR->getVariable());
11271074
trackIfUnresolved(DVR->getExpression());
11281075
if (DVR->isDbgAssign())
11291076
trackIfUnresolved(DVR->getAddressExpression());
11301077

1131-
BasicBlock::iterator InsertPt;
1132-
if (InsertBB && InsertBefore)
1133-
InsertPt = InsertBefore->getIterator();
1134-
else if (InsertBB)
1135-
InsertPt = InsertBB->end();
1136-
InsertPt.setHeadBit(InsertAtHead);
1137-
InsertBB->insertDbgRecordBefore(DVR, InsertPt);
1078+
auto *BB = InsertPt.getBasicBlock();
1079+
BB->insertDbgRecordBefore(DVR, InsertPt);
11381080
}
11391081

11401082
Instruction *DIBuilder::insertDbgIntrinsic(llvm::Function *IntrinsicFn,
11411083
Value *V, DILocalVariable *VarInfo,
11421084
DIExpression *Expr,
11431085
const DILocation *DL,
1144-
BasicBlock *InsertBB,
1145-
Instruction *InsertBefore) {
1086+
InsertPosition InsertPt) {
11461087
assert(IntrinsicFn && "must pass a non-null intrinsic function");
11471088
assert(V && "must pass a value to a dbg intrinsic");
11481089
assert(VarInfo &&
@@ -1159,13 +1100,12 @@ Instruction *DIBuilder::insertDbgIntrinsic(llvm::Function *IntrinsicFn,
11591100
MetadataAsValue::get(VMContext, Expr)};
11601101

11611102
IRBuilder<> B(DL->getContext());
1162-
initIRBuilder(B, DL, InsertBB, InsertBefore);
1103+
initIRBuilder(B, DL, InsertPt);
11631104
return B.CreateCall(IntrinsicFn, Args);
11641105
}
11651106

11661107
DbgInstPtr DIBuilder::insertLabel(DILabel *LabelInfo, const DILocation *DL,
1167-
BasicBlock *InsertBB,
1168-
Instruction *InsertBefore) {
1108+
InsertPosition InsertPt) {
11691109
assert(LabelInfo && "empty or invalid DILabel* passed to dbg.label");
11701110
assert(DL && "Expected debug loc");
11711111
assert(DL->getScope()->getSubprogram() ==
@@ -1175,10 +1115,10 @@ DbgInstPtr DIBuilder::insertLabel(DILabel *LabelInfo, const DILocation *DL,
11751115
trackIfUnresolved(LabelInfo);
11761116
if (M.IsNewDbgInfoFormat) {
11771117
DbgLabelRecord *DLR = new DbgLabelRecord(LabelInfo, DL);
1178-
if (InsertBB && InsertBefore)
1179-
InsertBB->insertDbgRecordBefore(DLR, InsertBefore->getIterator());
1180-
else if (InsertBB)
1181-
InsertBB->insertDbgRecordBefore(DLR, InsertBB->end());
1118+
if (InsertPt.isValid()) {
1119+
auto *BB = InsertPt.getBasicBlock();
1120+
BB->insertDbgRecordBefore(DLR, InsertPt);
1121+
}
11821122
return DLR;
11831123
}
11841124

@@ -1188,7 +1128,7 @@ DbgInstPtr DIBuilder::insertLabel(DILabel *LabelInfo, const DILocation *DL,
11881128
Value *Args[] = {MetadataAsValue::get(VMContext, LabelInfo)};
11891129

11901130
IRBuilder<> B(DL->getContext());
1191-
initIRBuilder(B, DL, InsertBB, InsertBefore);
1131+
initIRBuilder(B, DL, InsertPt);
11921132
return B.CreateCall(LabelFn, Args);
11931133
}
11941134

0 commit comments

Comments
 (0)