Skip to content

Commit 4f10d6d

Browse files
hvdijktstellar
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. (cherry picked from commit 1083ec6)
1 parent 17dd52c commit 4f10d6d

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
@@ -5119,7 +5119,7 @@ void CGDebugInfo::EmitLabel(const LabelDecl *D, CGBuilderTy &Builder) {
51195119
DBuilder.insertLabel(L,
51205120
llvm::DILocation::get(CGM.getLLVMContext(), Line, Column,
51215121
Scope, CurInlinedAt),
5122-
Builder.GetInsertBlock());
5122+
Builder.GetInsertBlock()->end());
51235123
}
51245124

51255125
llvm::DIType *CGDebugInfo::CreateSelfType(const QualType &QualTy,
@@ -5197,7 +5197,7 @@ void CGDebugInfo::EmitDeclareOfBlockDeclRefVariable(
51975197
LexicalBlockStack.back(), CurInlinedAt);
51985198
auto *Expr = DBuilder.createExpression(addr);
51995199
if (InsertPoint)
5200-
DBuilder.insertDeclare(Storage, D, Expr, DL, InsertPoint);
5200+
DBuilder.insertDeclare(Storage, D, Expr, DL, InsertPoint->getIterator());
52015201
else
52025202
DBuilder.insertDeclare(Storage, D, Expr, DL, Builder.GetInsertBlock());
52035203
}
@@ -5862,7 +5862,7 @@ void CGDebugInfo::EmitPseudoVariable(CGBuilderTy &Builder,
58625862

58635863
if (auto InsertPoint = Value->getInsertionPointAfterDef()) {
58645864
DBuilder.insertDbgValueIntrinsic(Value, D, DBuilder.createExpression(), DIL,
5865-
&**InsertPoint);
5865+
*InsertPoint);
58665866
}
58675867
}
58685868

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.
@@ -993,46 +975,28 @@ namespace llvm {
993975
/// \param VarInfo Variable's debug info descriptor.
994976
/// \param Expr A complex location expression.
995977
/// \param DL Debug info location.
996-
/// \param InsertBefore Location for the new intrinsic.
978+
/// \param InsertPt Location for the new intrinsic.
997979
DbgInstPtr insertDeclare(llvm::Value *Storage, DILocalVariable *VarInfo,
998980
DIExpression *Expr, const DILocation *DL,
999-
Instruction *InsertBefore);
981+
InsertPosition InsertPt);
1000982

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

1015990
/// Insert a new llvm.dbg.value intrinsic call.
1016991
/// \param Val llvm::Value of the variable
1017992
/// \param VarInfo Variable's debug info descriptor.
1018993
/// \param Expr A complex location expression.
1019994
/// \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);
1025-
1026-
/// Insert a new llvm.dbg.value intrinsic call.
1027-
/// \param Val llvm::Value of the variable
1028-
/// \param VarInfo Variable's debug info descriptor.
1029-
/// \param Expr A complex location expression.
1030-
/// \param DL Debug info location.
1031-
/// \param InsertBefore Location for the new intrinsic.
995+
/// \param InsertPt Location for the new intrinsic.
1032996
DbgInstPtr insertDbgValueIntrinsic(llvm::Value *Val,
1033997
DILocalVariable *VarInfo,
1034998
DIExpression *Expr, const DILocation *DL,
1035-
Instruction *InsertBefore);
999+
InsertPosition InsertPt);
10361000

10371001
/// Replace the vtable holder in the given type.
10381002
///

llvm/lib/IR/DIBuilder.cpp

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

978973
DbgInstPtr DIBuilder::insertDbgAssign(Instruction *LinkedInstr, Value *Val,
@@ -987,11 +982,10 @@ DbgInstPtr DIBuilder::insertDbgAssign(Instruction *LinkedInstr, Value *Val,
987982
if (M.IsNewDbgInfoFormat) {
988983
DbgVariableRecord *DVR = DbgVariableRecord::createDVRAssign(
989984
Val, SrcVar, ValExpr, Link, Addr, AddrExpr, DL);
990-
BasicBlock *InsertBB = LinkedInstr->getParent();
991985
// Insert after LinkedInstr.
992986
BasicBlock::iterator NextIt = std::next(LinkedInstr->getIterator());
993-
Instruction *InsertBefore = NextIt == InsertBB->end() ? nullptr : &*NextIt;
994-
insertDbgVariableRecord(DVR, InsertBB, InsertBefore, true);
987+
NextIt.setHeadBit(true);
988+
insertDbgVariableRecord(DVR, NextIt);
995989
return DVR;
996990
}
997991

@@ -1017,47 +1011,11 @@ DbgInstPtr DIBuilder::insertDbgAssign(Instruction *LinkedInstr, Value *Val,
10171011
return DVI;
10181012
}
10191013

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

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

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

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

10891050
DbgInstPtr DIBuilder::insertDeclare(Value *Storage, DILocalVariable *VarInfo,
10901051
DIExpression *Expr, const DILocation *DL,
1091-
BasicBlock *InsertBB,
1092-
Instruction *InsertBefore) {
1052+
InsertPosition InsertPt) {
10931053
assert(VarInfo && "empty or invalid DILocalVariable* passed to dbg.declare");
10941054
assert(DL && "Expected debug loc");
10951055
assert(DL->getScope()->getSubprogram() ==
@@ -1099,7 +1059,7 @@ DbgInstPtr DIBuilder::insertDeclare(Value *Storage, DILocalVariable *VarInfo,
10991059
if (M.IsNewDbgInfoFormat) {
11001060
DbgVariableRecord *DVR =
11011061
DbgVariableRecord::createDVRDeclare(Storage, VarInfo, Expr, DL);
1102-
insertDbgVariableRecord(DVR, InsertBB, InsertBefore);
1062+
insertDbgVariableRecord(DVR, InsertPt);
11031063
return DVR;
11041064
}
11051065

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

11151075
IRBuilder<> B(DL->getContext());
1116-
initIRBuilder(B, DL, InsertBB, InsertBefore);
1076+
initIRBuilder(B, DL, InsertPt);
11171077
return B.CreateCall(DeclareFn, Args);
11181078
}
11191079

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

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

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

11601112
IRBuilder<> B(DL->getContext());
1161-
initIRBuilder(B, DL, InsertBB, InsertBefore);
1113+
initIRBuilder(B, DL, InsertPt);
11621114
return B.CreateCall(IntrinsicFn, Args);
11631115
}
11641116

11651117
DbgInstPtr DIBuilder::insertLabel(DILabel *LabelInfo, const DILocation *DL,
1166-
BasicBlock *InsertBB,
1167-
Instruction *InsertBefore) {
1118+
InsertPosition InsertPt) {
11681119
assert(LabelInfo && "empty or invalid DILabel* passed to dbg.label");
11691120
assert(DL && "Expected debug loc");
11701121
assert(DL->getScope()->getSubprogram() ==
@@ -1174,10 +1125,10 @@ DbgInstPtr DIBuilder::insertLabel(DILabel *LabelInfo, const DILocation *DL,
11741125
trackIfUnresolved(LabelInfo);
11751126
if (M.IsNewDbgInfoFormat) {
11761127
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());
1128+
if (InsertPt.isValid()) {
1129+
auto *BB = InsertPt.getBasicBlock();
1130+
BB->insertDbgRecordBefore(DLR, InsertPt);
1131+
}
11811132
return DLR;
11821133
}
11831134

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

11891140
IRBuilder<> B(DL->getContext());
1190-
initIRBuilder(B, DL, InsertBB, InsertBefore);
1141+
initIRBuilder(B, DL, InsertPt);
11911142
return B.CreateCall(LabelFn, Args);
11921143
}
11931144

0 commit comments

Comments
 (0)