Skip to content

Commit 23209eb

Browse files
committed
Revert "[DebugInfo] Update DIBuilder insertion to take InsertPosition (#126059)"
This reverts commit 3ec9f74.
1 parent 3ec9f74 commit 23209eb

File tree

11 files changed

+200
-99
lines changed

11 files changed

+200
-99
lines changed

clang/lib/CodeGen/CGDebugInfo.cpp

Lines changed: 8 additions & 9 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()->end());
4904+
Builder.GetInsertBlock());
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()->end());
4972+
Builder.GetInsertBlock());
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()->end());
5078+
Builder.GetInsertBlock());
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()->end());
5125+
Builder.GetInsertBlock());
51265126
}
51275127

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

52095208
llvm::DILocalVariable *
@@ -5386,7 +5385,7 @@ void CGDebugInfo::EmitDeclareOfBlockLiteralArgVariable(const CGBlockInfo &block,
53865385
DBuilder.insertDeclare(Alloca, debugVar, DBuilder.createExpression(),
53875386
llvm::DILocation::get(CGM.getLLVMContext(), line,
53885387
column, scope, CurInlinedAt),
5389-
Builder.GetInsertBlock()->end());
5388+
Builder.GetInsertBlock());
53905389
}
53915390

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

58675866
if (auto InsertPoint = Value->getInsertionPointAfterDef()) {
58685867
DBuilder.insertDbgValueIntrinsic(Value, D, DBuilder.createExpression(), DIL,
5869-
*InsertPoint);
5868+
&**InsertPoint);
58705869
}
58715870
}
58725871

llvm/include/llvm/IR/DIBuilder.h

Lines changed: 54 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -92,15 +92,33 @@ 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+
95104
/// Internal helper. Track metadata if untracked and insert \p DVR.
96-
void insertDbgVariableRecord(DbgVariableRecord *DVR,
97-
InsertPosition InsertPt);
105+
void insertDbgVariableRecord(DbgVariableRecord *DVR, BasicBlock *InsertBB,
106+
Instruction *InsertBefore,
107+
bool InsertAtHead = false);
98108

99109
/// Internal helper with common code used by insertDbg{Value,Addr}Intrinsic.
100110
Instruction *insertDbgIntrinsic(llvm::Function *Intrinsic, llvm::Value *Val,
101111
DILocalVariable *VarInfo,
102112
DIExpression *Expr, const DILocation *DL,
103-
InsertPosition InsertPt);
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);
104122

105123
public:
106124
/// Construct a builder for a module.
@@ -943,6 +961,16 @@ namespace llvm {
943961
StringRef Name = "",
944962
DINodeArray Elements = nullptr);
945963

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+
946974
/// Insert a new llvm.dbg.assign intrinsic call.
947975
/// \param LinkedInstr Instruction with a DIAssignID to link with the new
948976
/// intrinsic. The intrinsic will be inserted after
@@ -967,28 +995,46 @@ namespace llvm {
967995
/// \param VarInfo Variable's debug info descriptor.
968996
/// \param Expr A complex location expression.
969997
/// \param DL Debug info location.
970-
/// \param InsertPt Location for the new intrinsic.
998+
/// \param InsertBefore Location for the new intrinsic.
971999
DbgInstPtr insertDeclare(llvm::Value *Storage, DILocalVariable *VarInfo,
9721000
DIExpression *Expr, const DILocation *DL,
973-
InsertPosition InsertPt);
1001+
Instruction *InsertBefore);
9741002

9751003
/// Insert a new llvm.dbg.label intrinsic call.
9761004
/// \param LabelInfo Label's debug info descriptor.
9771005
/// \param DL Debug info location.
9781006
/// \param InsertBefore Location for the new intrinsic.
9791007
DbgInstPtr insertLabel(DILabel *LabelInfo, const DILocation *DL,
980-
InsertPosition InsertPt);
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);
9811027

9821028
/// Insert a new llvm.dbg.value intrinsic call.
9831029
/// \param Val llvm::Value of the variable
9841030
/// \param VarInfo Variable's debug info descriptor.
9851031
/// \param Expr A complex location expression.
9861032
/// \param DL Debug info location.
987-
/// \param InsertPt Location for the new intrinsic.
1033+
/// \param InsertBefore Location for the new intrinsic.
9881034
DbgInstPtr insertDbgValueIntrinsic(llvm::Value *Val,
9891035
DILocalVariable *VarInfo,
9901036
DIExpression *Expr, const DILocation *DL,
991-
InsertPosition InsertPt);
1037+
Instruction *InsertBefore);
9921038

9931039
/// Replace the vtable holder in the given type.
9941040
///

llvm/lib/IR/DIBuilder.cpp

Lines changed: 88 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -960,6 +960,22 @@ 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+
963979
DbgInstPtr DIBuilder::insertDbgAssign(Instruction *LinkedInstr, Value *Val,
964980
DILocalVariable *SrcVar,
965981
DIExpression *ValExpr, Value *Addr,
@@ -972,10 +988,11 @@ DbgInstPtr DIBuilder::insertDbgAssign(Instruction *LinkedInstr, Value *Val,
972988
if (M.IsNewDbgInfoFormat) {
973989
DbgVariableRecord *DVR = DbgVariableRecord::createDVRAssign(
974990
Val, SrcVar, ValExpr, Link, Addr, AddrExpr, DL);
991+
BasicBlock *InsertBB = LinkedInstr->getParent();
975992
// Insert after LinkedInstr.
976993
BasicBlock::iterator NextIt = std::next(LinkedInstr->getIterator());
977-
NextIt.setHeadBit(true);
978-
insertDbgVariableRecord(DVR, NextIt);
994+
Instruction *InsertBefore = NextIt == InsertBB->end() ? nullptr : &*NextIt;
995+
insertDbgVariableRecord(DVR, InsertBB, InsertBefore, true);
979996
return DVR;
980997
}
981998

@@ -1001,11 +1018,47 @@ DbgInstPtr DIBuilder::insertDbgAssign(Instruction *LinkedInstr, Value *Val,
10011018
return DVI;
10021019
}
10031020

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+
10041054
/// Initialize IRBuilder for inserting dbg.declare and dbg.value intrinsics.
10051055
/// This abstracts over the various ways to specify an insert position.
10061056
static void initIRBuilder(IRBuilder<> &Builder, const DILocation *DL,
1007-
InsertPosition InsertPt) {
1008-
Builder.SetInsertPoint(InsertPt.getBasicBlock(), InsertPt);
1057+
BasicBlock *InsertBB, Instruction *InsertBefore) {
1058+
if (InsertBefore)
1059+
Builder.SetInsertPoint(InsertBefore);
1060+
else if (InsertBB)
1061+
Builder.SetInsertPoint(InsertBB);
10091062
Builder.SetCurrentDebugLocation(DL);
10101063
}
10111064

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

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

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

10401090
DbgInstPtr DIBuilder::insertDeclare(Value *Storage, DILocalVariable *VarInfo,
10411091
DIExpression *Expr, const DILocation *DL,
1042-
InsertPosition InsertPt) {
1092+
BasicBlock *InsertBB,
1093+
Instruction *InsertBefore) {
10431094
assert(VarInfo && "empty or invalid DILocalVariable* passed to dbg.declare");
10441095
assert(DL && "Expected debug loc");
10451096
assert(DL->getScope()->getSubprogram() ==
@@ -1049,7 +1100,7 @@ DbgInstPtr DIBuilder::insertDeclare(Value *Storage, DILocalVariable *VarInfo,
10491100
if (M.IsNewDbgInfoFormat) {
10501101
DbgVariableRecord *DVR =
10511102
DbgVariableRecord::createDVRDeclare(Storage, VarInfo, Expr, DL);
1052-
insertDbgVariableRecord(DVR, InsertPt);
1103+
insertDbgVariableRecord(DVR, InsertBB, InsertBefore);
10531104
return DVR;
10541105
}
10551106

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

10651116
IRBuilder<> B(DL->getContext());
1066-
initIRBuilder(B, DL, InsertPt);
1117+
initIRBuilder(B, DL, InsertBB, InsertBefore);
10671118
return B.CreateCall(DeclareFn, Args);
10681119
}
10691120

10701121
void DIBuilder::insertDbgVariableRecord(DbgVariableRecord *DVR,
1071-
InsertPosition InsertPt) {
1072-
assert(InsertPt.isValid());
1122+
BasicBlock *InsertBB,
1123+
Instruction *InsertBefore,
1124+
bool InsertAtHead) {
1125+
assert(InsertBefore || InsertBB);
10731126
trackIfUnresolved(DVR->getVariable());
10741127
trackIfUnresolved(DVR->getExpression());
10751128
if (DVR->isDbgAssign())
10761129
trackIfUnresolved(DVR->getAddressExpression());
10771130

1078-
auto *BB = InsertPt.getBasicBlock();
1079-
BB->insertDbgRecordBefore(DVR, InsertPt);
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);
10801138
}
10811139

10821140
Instruction *DIBuilder::insertDbgIntrinsic(llvm::Function *IntrinsicFn,
10831141
Value *V, DILocalVariable *VarInfo,
10841142
DIExpression *Expr,
10851143
const DILocation *DL,
1086-
InsertPosition InsertPt) {
1144+
BasicBlock *InsertBB,
1145+
Instruction *InsertBefore) {
10871146
assert(IntrinsicFn && "must pass a non-null intrinsic function");
10881147
assert(V && "must pass a value to a dbg intrinsic");
10891148
assert(VarInfo &&
@@ -1100,12 +1159,13 @@ Instruction *DIBuilder::insertDbgIntrinsic(llvm::Function *IntrinsicFn,
11001159
MetadataAsValue::get(VMContext, Expr)};
11011160

11021161
IRBuilder<> B(DL->getContext());
1103-
initIRBuilder(B, DL, InsertPt);
1162+
initIRBuilder(B, DL, InsertBB, InsertBefore);
11041163
return B.CreateCall(IntrinsicFn, Args);
11051164
}
11061165

11071166
DbgInstPtr DIBuilder::insertLabel(DILabel *LabelInfo, const DILocation *DL,
1108-
InsertPosition InsertPt) {
1167+
BasicBlock *InsertBB,
1168+
Instruction *InsertBefore) {
11091169
assert(LabelInfo && "empty or invalid DILabel* passed to dbg.label");
11101170
assert(DL && "Expected debug loc");
11111171
assert(DL->getScope()->getSubprogram() ==
@@ -1115,10 +1175,10 @@ DbgInstPtr DIBuilder::insertLabel(DILabel *LabelInfo, const DILocation *DL,
11151175
trackIfUnresolved(LabelInfo);
11161176
if (M.IsNewDbgInfoFormat) {
11171177
DbgLabelRecord *DLR = new DbgLabelRecord(LabelInfo, DL);
1118-
if (InsertPt.isValid()) {
1119-
auto *BB = InsertPt.getBasicBlock();
1120-
BB->insertDbgRecordBefore(DLR, InsertPt);
1121-
}
1178+
if (InsertBB && InsertBefore)
1179+
InsertBB->insertDbgRecordBefore(DLR, InsertBefore->getIterator());
1180+
else if (InsertBB)
1181+
InsertBB->insertDbgRecordBefore(DLR, InsertBB->end());
11221182
return DLR;
11231183
}
11241184

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

11301190
IRBuilder<> B(DL->getContext());
1131-
initIRBuilder(B, DL, InsertPt);
1191+
initIRBuilder(B, DL, InsertBB, InsertBefore);
11321192
return B.CreateCall(LabelFn, Args);
11331193
}
11341194

0 commit comments

Comments
 (0)