Skip to content

Commit a815481

Browse files
committed
[DebugInfo] Update DIBuilder insertion to take InsertPosition
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 2f2ac3d commit a815481

File tree

11 files changed

+98
-200
lines changed

11 files changed

+98
-200
lines changed

clang/lib/CodeGen/CGDebugInfo.cpp

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4897,7 +4897,7 @@ llvm::DILocalVariable *CGDebugInfo::EmitDeclare(const VarDecl *VD,
48974897
llvm::DILocation::get(CGM.getLLVMContext(), Line,
48984898
Column, Scope,
48994899
CurInlinedAt),
4900-
Builder.GetInsertBlock());
4900+
Builder.GetInsertBlock()->end());
49014901
}
49024902
}
49034903
}
@@ -4965,7 +4965,7 @@ llvm::DILocalVariable *CGDebugInfo::EmitDeclare(const VarDecl *VD,
49654965
DBuilder.insertDeclare(Storage, D, DBuilder.createExpression(Expr),
49664966
llvm::DILocation::get(CGM.getLLVMContext(), Line,
49674967
Column, Scope, CurInlinedAt),
4968-
Builder.GetInsertBlock());
4968+
Builder.GetInsertBlock()->end());
49694969

49704970
return D;
49714971
}
@@ -5071,7 +5071,7 @@ llvm::DILocalVariable *CGDebugInfo::EmitDeclare(const BindingDecl *BD,
50715071
DBuilder.insertDeclare(Storage, D, DBuilder.createExpression(Expr),
50725072
llvm::DILocation::get(CGM.getLLVMContext(), Line,
50735073
Column, Scope, CurInlinedAt),
5074-
Builder.GetInsertBlock());
5074+
Builder.GetInsertBlock()->end());
50755075

50765076
return D;
50775077
}
@@ -5118,7 +5118,7 @@ void CGDebugInfo::EmitLabel(const LabelDecl *D, CGBuilderTy &Builder) {
51185118
DBuilder.insertLabel(L,
51195119
llvm::DILocation::get(CGM.getLLVMContext(), Line, Column,
51205120
Scope, CurInlinedAt),
5121-
Builder.GetInsertBlock());
5121+
Builder.GetInsertBlock()->end());
51225122
}
51235123

51245124
llvm::DIType *CGDebugInfo::CreateSelfType(const QualType &QualTy,
@@ -5196,9 +5196,10 @@ void CGDebugInfo::EmitDeclareOfBlockDeclRefVariable(
51965196
LexicalBlockStack.back(), CurInlinedAt);
51975197
auto *Expr = DBuilder.createExpression(addr);
51985198
if (InsertPoint)
5199-
DBuilder.insertDeclare(Storage, D, Expr, DL, InsertPoint);
5199+
DBuilder.insertDeclare(Storage, D, Expr, DL, InsertPoint->getIterator());
52005200
else
5201-
DBuilder.insertDeclare(Storage, D, Expr, DL, Builder.GetInsertBlock());
5201+
DBuilder.insertDeclare(Storage, D, Expr, DL,
5202+
Builder.GetInsertBlock()->end());
52025203
}
52035204

52045205
llvm::DILocalVariable *
@@ -5381,7 +5382,7 @@ void CGDebugInfo::EmitDeclareOfBlockLiteralArgVariable(const CGBlockInfo &block,
53815382
DBuilder.insertDeclare(Alloca, debugVar, DBuilder.createExpression(),
53825383
llvm::DILocation::get(CGM.getLLVMContext(), line,
53835384
column, scope, CurInlinedAt),
5384-
Builder.GetInsertBlock());
5385+
Builder.GetInsertBlock()->end());
53855386
}
53865387

53875388
llvm::DIDerivedType *
@@ -5861,7 +5862,7 @@ void CGDebugInfo::EmitPseudoVariable(CGBuilderTy &Builder,
58615862

58625863
if (auto InsertPoint = Value->getInsertionPointAfterDef()) {
58635864
DBuilder.insertDbgValueIntrinsic(Value, D, DBuilder.createExpression(), DIL,
5864-
&**InsertPoint);
5865+
*InsertPoint);
58655866
}
58665867
}
58675868

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.
@@ -959,16 +941,6 @@ namespace llvm {
959941
StringRef Name = "",
960942
DINodeArray Elements = nullptr);
961943

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

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

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

1037991
/// Replace the vtable holder in the given type.
1038992
///

llvm/lib/IR/DIBuilder.cpp

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

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

@@ -1017,47 +1000,11 @@ DbgInstPtr DIBuilder::insertDbgAssign(Instruction *LinkedInstr, Value *Val,
10171000
return DVI;
10181001
}
10191002

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

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

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

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

10891039
DbgInstPtr DIBuilder::insertDeclare(Value *Storage, DILocalVariable *VarInfo,
10901040
DIExpression *Expr, const DILocation *DL,
1091-
BasicBlock *InsertBB,
1092-
Instruction *InsertBefore) {
1041+
InsertPosition InsertPt) {
10931042
assert(VarInfo && "empty or invalid DILocalVariable* passed to dbg.declare");
10941043
assert(DL && "Expected debug loc");
10951044
assert(DL->getScope()->getSubprogram() ==
@@ -1099,7 +1048,7 @@ DbgInstPtr DIBuilder::insertDeclare(Value *Storage, DILocalVariable *VarInfo,
10991048
if (M.IsNewDbgInfoFormat) {
11001049
DbgVariableRecord *DVR =
11011050
DbgVariableRecord::createDVRDeclare(Storage, VarInfo, Expr, DL);
1102-
insertDbgVariableRecord(DVR, InsertBB, InsertBefore);
1051+
insertDbgVariableRecord(DVR, InsertPt);
11031052
return DVR;
11041053
}
11051054

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

11151064
IRBuilder<> B(DL->getContext());
1116-
initIRBuilder(B, DL, InsertBB, InsertBefore);
1065+
initIRBuilder(B, DL, InsertPt);
11171066
return B.CreateCall(DeclareFn, Args);
11181067
}
11191068

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

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

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

11601101
IRBuilder<> B(DL->getContext());
1161-
initIRBuilder(B, DL, InsertBB, InsertBefore);
1102+
initIRBuilder(B, DL, InsertPt);
11621103
return B.CreateCall(IntrinsicFn, Args);
11631104
}
11641105

11651106
DbgInstPtr DIBuilder::insertLabel(DILabel *LabelInfo, const DILocation *DL,
1166-
BasicBlock *InsertBB,
1167-
Instruction *InsertBefore) {
1107+
InsertPosition InsertPt) {
11681108
assert(LabelInfo && "empty or invalid DILabel* passed to dbg.label");
11691109
assert(DL && "Expected debug loc");
11701110
assert(DL->getScope()->getSubprogram() ==
@@ -1174,10 +1114,10 @@ DbgInstPtr DIBuilder::insertLabel(DILabel *LabelInfo, const DILocation *DL,
11741114
trackIfUnresolved(LabelInfo);
11751115
if (M.IsNewDbgInfoFormat) {
11761116
DbgLabelRecord *DLR = new DbgLabelRecord(LabelInfo, DL);
1177-
if (InsertBB && InsertBefore)
1178-
InsertBB->insertDbgRecordBefore(DLR, InsertBefore->getIterator());
1179-
else if (InsertBB)
1180-
InsertBB->insertDbgRecordBefore(DLR, InsertBB->end());
1117+
if (InsertPt.isValid()) {
1118+
auto *BB = InsertPt.getBasicBlock();
1119+
BB->insertDbgRecordBefore(DLR, InsertPt);
1120+
}
11811121
return DLR;
11821122
}
11831123

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

11891129
IRBuilder<> B(DL->getContext());
1190-
initIRBuilder(B, DL, InsertBB, InsertBefore);
1130+
initIRBuilder(B, DL, InsertPt);
11911131
return B.CreateCall(LabelFn, Args);
11921132
}
11931133

0 commit comments

Comments
 (0)