Skip to content

Commit 8e68a72

Browse files
committed
Support iterator insertion for DIBuilder too
1 parent e84f6b6 commit 8e68a72

File tree

2 files changed

+83
-29
lines changed

2 files changed

+83
-29
lines changed

llvm/include/llvm/IR/DIBuilder.h

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -95,30 +95,29 @@ namespace llvm {
9595
/// Internal helper for insertDeclare.
9696
DbgInstPtr insertDeclare(llvm::Value *Storage, DILocalVariable *VarInfo,
9797
DIExpression *Expr, const DILocation *DL,
98-
BasicBlock *InsertBB, Instruction *InsertBefore);
98+
BasicBlock *InsertBB, std::optional<BasicBlock::iterator> InsertBefore);
9999

100100
/// Internal helper for insertLabel.
101101
DbgInstPtr insertLabel(DILabel *LabelInfo, const DILocation *DL,
102-
BasicBlock *InsertBB, Instruction *InsertBefore);
102+
BasicBlock *InsertBB, std::optional<BasicBlock::iterator> InsertBefore);
103103

104104
/// Internal helper. Track metadata if untracked and insert \p DVR.
105105
void insertDbgVariableRecord(DbgVariableRecord *DVR, BasicBlock *InsertBB,
106-
Instruction *InsertBefore,
107-
bool InsertAtHead = false);
106+
std::optional<BasicBlock::iterator> InsertBefore);
108107

109108
/// Internal helper with common code used by insertDbg{Value,Addr}Intrinsic.
110109
Instruction *insertDbgIntrinsic(llvm::Function *Intrinsic, llvm::Value *Val,
111110
DILocalVariable *VarInfo,
112111
DIExpression *Expr, const DILocation *DL,
113112
BasicBlock *InsertBB,
114-
Instruction *InsertBefore);
113+
std::optional<BasicBlock::iterator> InsertBefore);
115114

116115
/// Internal helper for insertDbgValueIntrinsic.
117116
DbgInstPtr insertDbgValueIntrinsic(llvm::Value *Val,
118117
DILocalVariable *VarInfo,
119118
DIExpression *Expr, const DILocation *DL,
120119
BasicBlock *InsertBB,
121-
Instruction *InsertBefore);
120+
std::optional<BasicBlock::iterator> InsertBefore);
122121

123122
public:
124123
/// Construct a builder for a module.
@@ -998,13 +997,30 @@ namespace llvm {
998997
DIExpression *Expr, const DILocation *DL,
999998
Instruction *InsertBefore);
1000999

1000+
/// Insert a new llvm.dbg.declare intrinsic call.
1001+
/// \param Storage llvm::Value of the variable
1002+
/// \param VarInfo Variable's debug info descriptor.
1003+
/// \param Expr A complex location expression.
1004+
/// \param DL Debug info location.
1005+
/// \param InsertBefore Location for the new intrinsic.
1006+
DbgInstPtr insertDeclare(llvm::Value *Storage, DILocalVariable *VarInfo,
1007+
DIExpression *Expr, const DILocation *DL,
1008+
BasicBlock::iterator InsertBefore);
1009+
10011010
/// Insert a new llvm.dbg.label intrinsic call.
10021011
/// \param LabelInfo Label's debug info descriptor.
10031012
/// \param DL Debug info location.
10041013
/// \param InsertBefore Location for the new intrinsic.
10051014
DbgInstPtr insertLabel(DILabel *LabelInfo, const DILocation *DL,
10061015
Instruction *InsertBefore);
10071016

1017+
/// Insert a new llvm.dbg.label intrinsic call.
1018+
/// \param LabelInfo Label's debug info descriptor.
1019+
/// \param DL Debug info location.
1020+
/// \param InsertBefore Location for the new intrinsic.
1021+
DbgInstPtr insertLabel(DILabel *LabelInfo, const DILocation *DL,
1022+
BasicBlock::iterator InsertBefore);
1023+
10081024
/// Insert a new llvm.dbg.label intrinsic call.
10091025
/// \param LabelInfo Label's debug info descriptor.
10101026
/// \param DL Debug info location.
@@ -1034,6 +1050,17 @@ namespace llvm {
10341050
DIExpression *Expr, const DILocation *DL,
10351051
Instruction *InsertBefore);
10361052

1053+
/// Insert a new llvm.dbg.value intrinsic call.
1054+
/// \param Val llvm::Value of the variable
1055+
/// \param VarInfo Variable's debug info descriptor.
1056+
/// \param Expr A complex location expression.
1057+
/// \param DL Debug info location.
1058+
/// \param InsertBefore Location for the new intrinsic.
1059+
DbgInstPtr insertDbgValueIntrinsic(llvm::Value *Val,
1060+
DILocalVariable *VarInfo,
1061+
DIExpression *Expr, const DILocation *DL,
1062+
BasicBlock::iterator InsertBefore);
1063+
10371064
/// Replace the vtable holder in the given type.
10381065
///
10391066
/// If this creates a self reference, it may orphan some unresolved cycles

llvm/lib/IR/DIBuilder.cpp

Lines changed: 50 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -961,18 +961,28 @@ DILexicalBlock *DIBuilder::createLexicalBlock(DIScope *Scope, DIFile *File,
961961

962962
DbgInstPtr DIBuilder::insertDeclare(Value *Storage, DILocalVariable *VarInfo,
963963
DIExpression *Expr, const DILocation *DL,
964-
Instruction *InsertBefore) {
964+
BasicBlock::iterator InsertBefore) {
965965
return insertDeclare(Storage, VarInfo, Expr, DL, InsertBefore->getParent(),
966966
InsertBefore);
967967
}
968968

969+
DbgInstPtr DIBuilder::insertDeclare(Value *Storage, DILocalVariable *VarInfo,
970+
DIExpression *Expr, const DILocation *DL,
971+
Instruction *InsertBefore) {
972+
return insertDeclare(Storage, VarInfo, Expr, DL, InsertBefore->getParent(),
973+
InsertBefore->getIterator());
974+
}
975+
969976
DbgInstPtr DIBuilder::insertDeclare(Value *Storage, DILocalVariable *VarInfo,
970977
DIExpression *Expr, const DILocation *DL,
971978
BasicBlock *InsertAtEnd) {
972979
// If this block already has a terminator then insert this intrinsic before
973980
// the terminator. Otherwise, put it at the end of the block.
974981
Instruction *InsertBefore = InsertAtEnd->getTerminator();
975-
return insertDeclare(Storage, VarInfo, Expr, DL, InsertAtEnd, InsertBefore);
982+
std::optional<BasicBlock::iterator> It = std::nullopt;
983+
if (InsertBefore)
984+
It = InsertBefore->getIterator();
985+
return insertDeclare(Storage, VarInfo, Expr, DL, InsertAtEnd, It);
976986
}
977987

978988
DbgInstPtr DIBuilder::insertDbgAssign(Instruction *LinkedInstr, Value *Val,
@@ -990,8 +1000,8 @@ DbgInstPtr DIBuilder::insertDbgAssign(Instruction *LinkedInstr, Value *Val,
9901000
BasicBlock *InsertBB = LinkedInstr->getParent();
9911001
// Insert after LinkedInstr.
9921002
BasicBlock::iterator NextIt = std::next(LinkedInstr->getIterator());
993-
Instruction *InsertBefore = NextIt == InsertBB->end() ? nullptr : &*NextIt;
994-
insertDbgVariableRecord(DVR, InsertBB, InsertBefore, true);
1003+
NextIt.setHeadBit(true);
1004+
insertDbgVariableRecord(DVR, InsertBB, NextIt);
9951005
return DVR;
9961006
}
9971007

@@ -1019,24 +1029,40 @@ DbgInstPtr DIBuilder::insertDbgAssign(Instruction *LinkedInstr, Value *Val,
10191029

10201030
DbgInstPtr DIBuilder::insertLabel(DILabel *LabelInfo, const DILocation *DL,
10211031
Instruction *InsertBefore) {
1022-
return insertLabel(LabelInfo, DL,
1023-
InsertBefore ? InsertBefore->getParent() : nullptr,
1024-
InsertBefore);
1032+
BasicBlock *BB = InsertBefore ? InsertBefore->getParent() : nullptr;
1033+
std::optional<BasicBlock::iterator> It = std::nullopt;
1034+
if (InsertBefore)
1035+
It = InsertBefore->getIterator();
1036+
return insertLabel(LabelInfo, DL, BB, It);
10251037
}
10261038

10271039
DbgInstPtr DIBuilder::insertLabel(DILabel *LabelInfo, const DILocation *DL,
10281040
BasicBlock *InsertAtEnd) {
1029-
return insertLabel(LabelInfo, DL, InsertAtEnd, nullptr);
1041+
return insertLabel(LabelInfo, DL, InsertAtEnd, std::nullopt);
1042+
}
1043+
1044+
DbgInstPtr DIBuilder::insertDbgValueIntrinsic(Value *V,
1045+
DILocalVariable *VarInfo,
1046+
DIExpression *Expr,
1047+
const DILocation *DL,
1048+
BasicBlock::iterator InsertBefore) {
1049+
BasicBlock *BB = InsertBefore->getParent();
1050+
DbgInstPtr DVI = insertDbgValueIntrinsic(V, VarInfo, Expr, DL, BB, InsertBefore);
1051+
if (auto *Inst = dyn_cast<Instruction *>(DVI))
1052+
cast<CallInst>(Inst)->setTailCall();
1053+
return DVI;
10301054
}
10311055

10321056
DbgInstPtr DIBuilder::insertDbgValueIntrinsic(Value *V,
10331057
DILocalVariable *VarInfo,
10341058
DIExpression *Expr,
10351059
const DILocation *DL,
10361060
Instruction *InsertBefore) {
1037-
DbgInstPtr DVI = insertDbgValueIntrinsic(
1038-
V, VarInfo, Expr, DL, InsertBefore ? InsertBefore->getParent() : nullptr,
1039-
InsertBefore);
1061+
BasicBlock *BB = InsertBefore ? InsertBefore->getParent() : nullptr;
1062+
std::optional<BasicBlock::iterator> It = std::nullopt;
1063+
if (InsertBefore)
1064+
It = InsertBefore->getIterator();
1065+
DbgInstPtr DVI = insertDbgValueIntrinsic(V, VarInfo, Expr, DL, BB, It);
10401066
if (auto *Inst = dyn_cast<Instruction *>(DVI))
10411067
cast<CallInst>(Inst)->setTailCall();
10421068
return DVI;
@@ -1047,15 +1073,16 @@ DbgInstPtr DIBuilder::insertDbgValueIntrinsic(Value *V,
10471073
DIExpression *Expr,
10481074
const DILocation *DL,
10491075
BasicBlock *InsertAtEnd) {
1050-
return insertDbgValueIntrinsic(V, VarInfo, Expr, DL, InsertAtEnd, nullptr);
1076+
return insertDbgValueIntrinsic(V, VarInfo, Expr, DL, InsertAtEnd, std::nullopt);
10511077
}
10521078

10531079
/// Initialize IRBuilder for inserting dbg.declare and dbg.value intrinsics.
10541080
/// This abstracts over the various ways to specify an insert position.
10551081
static void initIRBuilder(IRBuilder<> &Builder, const DILocation *DL,
1056-
BasicBlock *InsertBB, Instruction *InsertBefore) {
1082+
BasicBlock *InsertBB,
1083+
std::optional<BasicBlock::iterator> InsertBefore) {
10571084
if (InsertBefore)
1058-
Builder.SetInsertPoint(InsertBefore);
1085+
Builder.SetInsertPoint(InsertBB, *InsertBefore);
10591086
else if (InsertBB)
10601087
Builder.SetInsertPoint(InsertBB);
10611088
Builder.SetCurrentDebugLocation(DL);
@@ -1072,7 +1099,9 @@ static Function *getDeclareIntrin(Module &M) {
10721099

10731100
DbgInstPtr DIBuilder::insertDbgValueIntrinsic(
10741101
llvm::Value *Val, DILocalVariable *VarInfo, DIExpression *Expr,
1075-
const DILocation *DL, BasicBlock *InsertBB, Instruction *InsertBefore) {
1102+
const DILocation *DL, BasicBlock *InsertBB,
1103+
std::optional<BasicBlock::iterator> InsertBefore) {
1104+
10761105
if (M.IsNewDbgInfoFormat) {
10771106
DbgVariableRecord *DVR =
10781107
DbgVariableRecord::createDbgVariableRecord(Val, VarInfo, Expr, DL);
@@ -1089,7 +1118,7 @@ DbgInstPtr DIBuilder::insertDbgValueIntrinsic(
10891118
DbgInstPtr DIBuilder::insertDeclare(Value *Storage, DILocalVariable *VarInfo,
10901119
DIExpression *Expr, const DILocation *DL,
10911120
BasicBlock *InsertBB,
1092-
Instruction *InsertBefore) {
1121+
std::optional<BasicBlock::iterator> InsertBefore) {
10931122
assert(VarInfo && "empty or invalid DILocalVariable* passed to dbg.declare");
10941123
assert(DL && "Expected debug loc");
10951124
assert(DL->getScope()->getSubprogram() ==
@@ -1119,8 +1148,7 @@ DbgInstPtr DIBuilder::insertDeclare(Value *Storage, DILocalVariable *VarInfo,
11191148

11201149
void DIBuilder::insertDbgVariableRecord(DbgVariableRecord *DVR,
11211150
BasicBlock *InsertBB,
1122-
Instruction *InsertBefore,
1123-
bool InsertAtHead) {
1151+
std::optional<BasicBlock::iterator> InsertBefore) {
11241152
assert(InsertBefore || InsertBB);
11251153
trackIfUnresolved(DVR->getVariable());
11261154
trackIfUnresolved(DVR->getExpression());
@@ -1129,10 +1157,9 @@ void DIBuilder::insertDbgVariableRecord(DbgVariableRecord *DVR,
11291157

11301158
BasicBlock::iterator InsertPt;
11311159
if (InsertBB && InsertBefore)
1132-
InsertPt = InsertBefore->getIterator();
1160+
InsertPt = *InsertBefore;
11331161
else if (InsertBB)
11341162
InsertPt = InsertBB->end();
1135-
InsertPt.setHeadBit(InsertAtHead);
11361163
InsertBB->insertDbgRecordBefore(DVR, InsertPt);
11371164
}
11381165

@@ -1141,7 +1168,7 @@ Instruction *DIBuilder::insertDbgIntrinsic(llvm::Function *IntrinsicFn,
11411168
DIExpression *Expr,
11421169
const DILocation *DL,
11431170
BasicBlock *InsertBB,
1144-
Instruction *InsertBefore) {
1171+
std::optional<BasicBlock::iterator> InsertBefore) {
11451172
assert(IntrinsicFn && "must pass a non-null intrinsic function");
11461173
assert(V && "must pass a value to a dbg intrinsic");
11471174
assert(VarInfo &&
@@ -1164,7 +1191,7 @@ Instruction *DIBuilder::insertDbgIntrinsic(llvm::Function *IntrinsicFn,
11641191

11651192
DbgInstPtr DIBuilder::insertLabel(DILabel *LabelInfo, const DILocation *DL,
11661193
BasicBlock *InsertBB,
1167-
Instruction *InsertBefore) {
1194+
std::optional<BasicBlock::iterator> InsertBefore) {
11681195
assert(LabelInfo && "empty or invalid DILabel* passed to dbg.label");
11691196
assert(DL && "Expected debug loc");
11701197
assert(DL->getScope()->getSubprogram() ==
@@ -1175,7 +1202,7 @@ DbgInstPtr DIBuilder::insertLabel(DILabel *LabelInfo, const DILocation *DL,
11751202
if (M.IsNewDbgInfoFormat) {
11761203
DbgLabelRecord *DLR = new DbgLabelRecord(LabelInfo, DL);
11771204
if (InsertBB && InsertBefore)
1178-
InsertBB->insertDbgRecordBefore(DLR, InsertBefore->getIterator());
1205+
InsertBB->insertDbgRecordBefore(DLR, *InsertBefore);
11791206
else if (InsertBB)
11801207
InsertBB->insertDbgRecordBefore(DLR, InsertBB->end());
11811208
return DLR;

0 commit comments

Comments
 (0)