Skip to content

Commit 4dc56d5

Browse files
hvdijkjoaosaffran
authored andcommitted
[reland][DebugInfo] Update DIBuilder insertion to take InsertPosition (llvm#126967)
After llvm#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.
1 parent 28b18ef commit 4dc56d5

File tree

11 files changed

+98
-179
lines changed

11 files changed

+98
-179
lines changed

clang/lib/CodeGen/CGDebugInfo.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -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,7 +5200,7 @@ 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
52055205
DBuilder.insertDeclare(Storage, D, Expr, DL, Builder.GetInsertBlock());
52065206
}
@@ -5865,7 +5865,7 @@ void CGDebugInfo::EmitPseudoVariable(CGBuilderTy &Builder,
58655865

58665866
if (auto InsertPoint = Value->getInsertionPointAfterDef()) {
58675867
DBuilder.insertDbgValueIntrinsic(Value, D, DBuilder.createExpression(), DIL,
5868-
&**InsertPoint);
5868+
*InsertPoint);
58695869
}
58705870
}
58715871

llvm/include/llvm/IR/DIBuilder.h

Lines changed: 8 additions & 44 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.
@@ -995,46 +977,28 @@ namespace llvm {
995977
/// \param VarInfo Variable's debug info descriptor.
996978
/// \param Expr A complex location expression.
997979
/// \param DL Debug info location.
998-
/// \param InsertBefore Location for the new intrinsic.
980+
/// \param InsertPt Location for the new intrinsic.
999981
DbgInstPtr insertDeclare(llvm::Value *Storage, DILocalVariable *VarInfo,
1000982
DIExpression *Expr, const DILocation *DL,
1001-
Instruction *InsertBefore);
983+
InsertPosition InsertPt);
1002984

1003985
/// Insert a new llvm.dbg.label intrinsic call.
1004986
/// \param LabelInfo Label's debug info descriptor.
1005987
/// \param DL Debug info location.
1006988
/// \param InsertBefore Location for the new intrinsic.
1007989
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);
990+
InsertPosition InsertPt);
1016991

1017992
/// Insert a new llvm.dbg.value intrinsic call.
1018993
/// \param Val llvm::Value of the variable
1019994
/// \param VarInfo Variable's debug info descriptor.
1020995
/// \param Expr A complex location expression.
1021996
/// \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);
1027-
1028-
/// Insert a new llvm.dbg.value intrinsic call.
1029-
/// \param Val llvm::Value of the variable
1030-
/// \param VarInfo Variable's debug info descriptor.
1031-
/// \param Expr A complex location expression.
1032-
/// \param DL Debug info location.
1033-
/// \param InsertBefore Location for the new intrinsic.
997+
/// \param InsertPt Location for the new intrinsic.
1034998
DbgInstPtr insertDbgValueIntrinsic(llvm::Value *Val,
1035999
DILocalVariable *VarInfo,
10361000
DIExpression *Expr, const DILocation *DL,
1037-
Instruction *InsertBefore);
1001+
InsertPosition InsertPt);
10381002

10391003
/// Replace the vtable holder in the given type.
10401004
///

llvm/lib/IR/DIBuilder.cpp

Lines changed: 31 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -960,20 +960,15 @@ 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-
970963
DbgInstPtr DIBuilder::insertDeclare(Value *Storage, DILocalVariable *VarInfo,
971964
DIExpression *Expr, const DILocation *DL,
972965
BasicBlock *InsertAtEnd) {
973966
// If this block already has a terminator then insert this intrinsic before
974967
// the terminator. Otherwise, put it at the end of the block.
975968
Instruction *InsertBefore = InsertAtEnd->getTerminator();
976-
return insertDeclare(Storage, VarInfo, Expr, DL, InsertAtEnd, InsertBefore);
969+
return insertDeclare(Storage, VarInfo, Expr, DL,
970+
InsertBefore ? InsertBefore->getIterator()
971+
: InsertAtEnd->end());
977972
}
978973

979974
DbgInstPtr DIBuilder::insertDbgAssign(Instruction *LinkedInstr, Value *Val,
@@ -988,11 +983,10 @@ DbgInstPtr DIBuilder::insertDbgAssign(Instruction *LinkedInstr, Value *Val,
988983
if (M.IsNewDbgInfoFormat) {
989984
DbgVariableRecord *DVR = DbgVariableRecord::createDVRAssign(
990985
Val, SrcVar, ValExpr, Link, Addr, AddrExpr, DL);
991-
BasicBlock *InsertBB = LinkedInstr->getParent();
992986
// Insert after LinkedInstr.
993987
BasicBlock::iterator NextIt = std::next(LinkedInstr->getIterator());
994-
Instruction *InsertBefore = NextIt == InsertBB->end() ? nullptr : &*NextIt;
995-
insertDbgVariableRecord(DVR, InsertBB, InsertBefore, true);
988+
NextIt.setHeadBit(true);
989+
insertDbgVariableRecord(DVR, NextIt);
996990
return DVR;
997991
}
998992

@@ -1018,47 +1012,11 @@ DbgInstPtr DIBuilder::insertDbgAssign(Instruction *LinkedInstr, Value *Val,
10181012
return DVI;
10191013
}
10201014

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-
10541015
/// Initialize IRBuilder for inserting dbg.declare and dbg.value intrinsics.
10551016
/// This abstracts over the various ways to specify an insert position.
10561017
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);
1018+
InsertPosition InsertPt) {
1019+
Builder.SetInsertPoint(InsertPt.getBasicBlock(), InsertPt);
10621020
Builder.SetCurrentDebugLocation(DL);
10631021
}
10641022

@@ -1071,26 +1029,28 @@ static Function *getDeclareIntrin(Module &M) {
10711029
return Intrinsic::getOrInsertDeclaration(&M, Intrinsic::dbg_declare);
10721030
}
10731031

1074-
DbgInstPtr DIBuilder::insertDbgValueIntrinsic(
1075-
llvm::Value *Val, DILocalVariable *VarInfo, DIExpression *Expr,
1076-
const DILocation *DL, BasicBlock *InsertBB, Instruction *InsertBefore) {
1032+
DbgInstPtr DIBuilder::insertDbgValueIntrinsic(llvm::Value *Val,
1033+
DILocalVariable *VarInfo,
1034+
DIExpression *Expr,
1035+
const DILocation *DL,
1036+
InsertPosition InsertPt) {
10771037
if (M.IsNewDbgInfoFormat) {
10781038
DbgVariableRecord *DVR =
10791039
DbgVariableRecord::createDbgVariableRecord(Val, VarInfo, Expr, DL);
1080-
insertDbgVariableRecord(DVR, InsertBB, InsertBefore);
1040+
insertDbgVariableRecord(DVR, InsertPt);
10811041
return DVR;
10821042
}
10831043

10841044
if (!ValueFn)
10851045
ValueFn = Intrinsic::getOrInsertDeclaration(&M, Intrinsic::dbg_value);
1086-
return insertDbgIntrinsic(ValueFn, Val, VarInfo, Expr, DL, InsertBB,
1087-
InsertBefore);
1046+
auto *DVI = insertDbgIntrinsic(ValueFn, Val, VarInfo, Expr, DL, InsertPt);
1047+
cast<CallInst>(DVI)->setTailCall();
1048+
return DVI;
10881049
}
10891050

10901051
DbgInstPtr DIBuilder::insertDeclare(Value *Storage, DILocalVariable *VarInfo,
10911052
DIExpression *Expr, const DILocation *DL,
1092-
BasicBlock *InsertBB,
1093-
Instruction *InsertBefore) {
1053+
InsertPosition InsertPt) {
10941054
assert(VarInfo && "empty or invalid DILocalVariable* passed to dbg.declare");
10951055
assert(DL && "Expected debug loc");
10961056
assert(DL->getScope()->getSubprogram() ==
@@ -1100,7 +1060,7 @@ DbgInstPtr DIBuilder::insertDeclare(Value *Storage, DILocalVariable *VarInfo,
11001060
if (M.IsNewDbgInfoFormat) {
11011061
DbgVariableRecord *DVR =
11021062
DbgVariableRecord::createDVRDeclare(Storage, VarInfo, Expr, DL);
1103-
insertDbgVariableRecord(DVR, InsertBB, InsertBefore);
1063+
insertDbgVariableRecord(DVR, InsertPt);
11041064
return DVR;
11051065
}
11061066

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

11161076
IRBuilder<> B(DL->getContext());
1117-
initIRBuilder(B, DL, InsertBB, InsertBefore);
1077+
initIRBuilder(B, DL, InsertPt);
11181078
return B.CreateCall(DeclareFn, Args);
11191079
}
11201080

11211081
void DIBuilder::insertDbgVariableRecord(DbgVariableRecord *DVR,
1122-
BasicBlock *InsertBB,
1123-
Instruction *InsertBefore,
1124-
bool InsertAtHead) {
1125-
assert(InsertBefore || InsertBB);
1082+
InsertPosition InsertPt) {
1083+
assert(InsertPt.isValid());
11261084
trackIfUnresolved(DVR->getVariable());
11271085
trackIfUnresolved(DVR->getExpression());
11281086
if (DVR->isDbgAssign())
11291087
trackIfUnresolved(DVR->getAddressExpression());
11301088

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);
1089+
auto *BB = InsertPt.getBasicBlock();
1090+
BB->insertDbgRecordBefore(DVR, InsertPt);
11381091
}
11391092

11401093
Instruction *DIBuilder::insertDbgIntrinsic(llvm::Function *IntrinsicFn,
11411094
Value *V, DILocalVariable *VarInfo,
11421095
DIExpression *Expr,
11431096
const DILocation *DL,
1144-
BasicBlock *InsertBB,
1145-
Instruction *InsertBefore) {
1097+
InsertPosition InsertPt) {
11461098
assert(IntrinsicFn && "must pass a non-null intrinsic function");
11471099
assert(V && "must pass a value to a dbg intrinsic");
11481100
assert(VarInfo &&
@@ -1159,13 +1111,12 @@ Instruction *DIBuilder::insertDbgIntrinsic(llvm::Function *IntrinsicFn,
11591111
MetadataAsValue::get(VMContext, Expr)};
11601112

11611113
IRBuilder<> B(DL->getContext());
1162-
initIRBuilder(B, DL, InsertBB, InsertBefore);
1114+
initIRBuilder(B, DL, InsertPt);
11631115
return B.CreateCall(IntrinsicFn, Args);
11641116
}
11651117

11661118
DbgInstPtr DIBuilder::insertLabel(DILabel *LabelInfo, const DILocation *DL,
1167-
BasicBlock *InsertBB,
1168-
Instruction *InsertBefore) {
1119+
InsertPosition InsertPt) {
11691120
assert(LabelInfo && "empty or invalid DILabel* passed to dbg.label");
11701121
assert(DL && "Expected debug loc");
11711122
assert(DL->getScope()->getSubprogram() ==
@@ -1175,10 +1126,10 @@ DbgInstPtr DIBuilder::insertLabel(DILabel *LabelInfo, const DILocation *DL,
11751126
trackIfUnresolved(LabelInfo);
11761127
if (M.IsNewDbgInfoFormat) {
11771128
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());
1129+
if (InsertPt.isValid()) {
1130+
auto *BB = InsertPt.getBasicBlock();
1131+
BB->insertDbgRecordBefore(DLR, InsertPt);
1132+
}
11821133
return DLR;
11831134
}
11841135

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

11901141
IRBuilder<> B(DL->getContext());
1191-
initIRBuilder(B, DL, InsertBB, InsertBefore);
1142+
initIRBuilder(B, DL, InsertPt);
11921143
return B.CreateCall(LabelFn, Args);
11931144
}
11941145

0 commit comments

Comments
 (0)