Skip to content

Commit 394bba7

Browse files
authored
[CodeGen][DebugInfo] Add missing debug info for jump table BB (#71021)
visitJumpTable is called on FinishBasicBlock. At that time, getCurSDLoc will always return SDLoc without DebugLoc since CurInst was set to nullptr after visiting each instruction. This patch passes SDLoc to buildJumpTable when visiting SwitchInst so that visitJumpTable can use it later.
1 parent bda785a commit 394bba7

File tree

5 files changed

+24
-17
lines changed

5 files changed

+24
-17
lines changed

llvm/include/llvm/CodeGen/SwitchLoweringUtils.h

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -174,8 +174,12 @@ struct JumpTable {
174174
/// check MBB. This is when updating PHI nodes in successors.
175175
MachineBasicBlock *Default;
176176

177-
JumpTable(unsigned R, unsigned J, MachineBasicBlock *M, MachineBasicBlock *D)
178-
: Reg(R), JTI(J), MBB(M), Default(D) {}
177+
/// The debug location of the instruction this JumpTable was produced from.
178+
std::optional<SDLoc> SL; // For SelectionDAG
179+
180+
JumpTable(unsigned R, unsigned J, MachineBasicBlock *M, MachineBasicBlock *D,
181+
std::optional<SDLoc> SL)
182+
: Reg(R), JTI(J), MBB(M), Default(D), SL(SL) {}
179183
};
180184
struct JumpTableHeader {
181185
APInt First;
@@ -270,14 +274,14 @@ class SwitchLowering {
270274
std::vector<BitTestBlock> BitTestCases;
271275

272276
void findJumpTables(CaseClusterVector &Clusters, const SwitchInst *SI,
273-
MachineBasicBlock *DefaultMBB,
277+
std::optional<SDLoc> SL, MachineBasicBlock *DefaultMBB,
274278
ProfileSummaryInfo *PSI, BlockFrequencyInfo *BFI);
275279

276280
bool buildJumpTable(const CaseClusterVector &Clusters, unsigned First,
277281
unsigned Last, const SwitchInst *SI,
282+
const std::optional<SDLoc> &SL,
278283
MachineBasicBlock *DefaultMBB, CaseCluster &JTCluster);
279284

280-
281285
void findBitTestClusters(CaseClusterVector &Clusters, const SwitchInst *SI);
282286

283287
/// Build a bit test cluster from Clusters[First..Last]. Returns false if it

llvm/lib/CodeGen/GlobalISel/IRTranslator.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -722,7 +722,7 @@ bool IRTranslator::translateSwitch(const User &U, MachineIRBuilder &MIB) {
722722
return true;
723723
}
724724

725-
SL->findJumpTables(Clusters, &SI, DefaultMBB, nullptr, nullptr);
725+
SL->findJumpTables(Clusters, &SI, std::nullopt, DefaultMBB, nullptr, nullptr);
726726
SL->findBitTestClusters(Clusters, &SI);
727727

728728
LLVM_DEBUG({

llvm/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2670,14 +2670,13 @@ void SelectionDAGBuilder::visitSwitchCase(CaseBlock &CB,
26702670
/// visitJumpTable - Emit JumpTable node in the current MBB
26712671
void SelectionDAGBuilder::visitJumpTable(SwitchCG::JumpTable &JT) {
26722672
// Emit the code for the jump table
2673+
assert(JT.SL && "Should set SDLoc for SelectionDAG!");
26732674
assert(JT.Reg != -1U && "Should lower JT Header first!");
26742675
EVT PTy = DAG.getTargetLoweringInfo().getPointerTy(DAG.getDataLayout());
2675-
SDValue Index = DAG.getCopyFromReg(getControlRoot(), getCurSDLoc(),
2676-
JT.Reg, PTy);
2676+
SDValue Index = DAG.getCopyFromReg(getControlRoot(), *JT.SL, JT.Reg, PTy);
26772677
SDValue Table = DAG.getJumpTable(JT.JTI, PTy);
2678-
SDValue BrJumpTable = DAG.getNode(ISD::BR_JT, getCurSDLoc(),
2679-
MVT::Other, Index.getValue(1),
2680-
Table, Index);
2678+
SDValue BrJumpTable = DAG.getNode(ISD::BR_JT, *JT.SL, MVT::Other,
2679+
Index.getValue(1), Table, Index);
26812680
DAG.setRoot(BrJumpTable);
26822681
}
26832682

@@ -2686,7 +2685,8 @@ void SelectionDAGBuilder::visitJumpTable(SwitchCG::JumpTable &JT) {
26862685
void SelectionDAGBuilder::visitJumpTableHeader(SwitchCG::JumpTable &JT,
26872686
JumpTableHeader &JTH,
26882687
MachineBasicBlock *SwitchBB) {
2689-
SDLoc dl = getCurSDLoc();
2688+
assert(JT.SL && "Should set SDLoc for SelectionDAG!");
2689+
const SDLoc &dl = *JT.SL;
26902690

26912691
// Subtract the lowest switch case value from the value being switched on.
26922692
SDValue SwitchOp = getValue(JTH.SValue);
@@ -11869,7 +11869,8 @@ void SelectionDAGBuilder::visitSwitch(const SwitchInst &SI) {
1186911869
return;
1187011870
}
1187111871

11872-
SL->findJumpTables(Clusters, &SI, DefaultMBB, DAG.getPSI(), DAG.getBFI());
11872+
SL->findJumpTables(Clusters, &SI, getCurSDLoc(), DefaultMBB, DAG.getPSI(),
11873+
DAG.getBFI());
1187311874
SL->findBitTestClusters(Clusters, &SI);
1187411875

1187511876
LLVM_DEBUG({

llvm/lib/CodeGen/SwitchLoweringUtils.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ SwitchCG::getJumpTableNumCases(const SmallVectorImpl<unsigned> &TotalCases,
4545

4646
void SwitchCG::SwitchLowering::findJumpTables(CaseClusterVector &Clusters,
4747
const SwitchInst *SI,
48+
std::optional<SDLoc> SL,
4849
MachineBasicBlock *DefaultMBB,
4950
ProfileSummaryInfo *PSI,
5051
BlockFrequencyInfo *BFI) {
@@ -87,7 +88,7 @@ void SwitchCG::SwitchLowering::findJumpTables(CaseClusterVector &Clusters,
8788
// Cheap case: the whole range may be suitable for jump table.
8889
if (TLI->isSuitableForJumpTable(SI, NumCases, Range, PSI, BFI)) {
8990
CaseCluster JTCluster;
90-
if (buildJumpTable(Clusters, 0, N - 1, SI, DefaultMBB, JTCluster)) {
91+
if (buildJumpTable(Clusters, 0, N - 1, SI, SL, DefaultMBB, JTCluster)) {
9192
Clusters[0] = JTCluster;
9293
Clusters.resize(1);
9394
return;
@@ -177,7 +178,7 @@ void SwitchCG::SwitchLowering::findJumpTables(CaseClusterVector &Clusters,
177178

178179
CaseCluster JTCluster;
179180
if (NumClusters >= MinJumpTableEntries &&
180-
buildJumpTable(Clusters, First, Last, SI, DefaultMBB, JTCluster)) {
181+
buildJumpTable(Clusters, First, Last, SI, SL, DefaultMBB, JTCluster)) {
181182
Clusters[DstIndex++] = JTCluster;
182183
} else {
183184
for (unsigned I = First; I <= Last; ++I)
@@ -190,6 +191,7 @@ void SwitchCG::SwitchLowering::findJumpTables(CaseClusterVector &Clusters,
190191
bool SwitchCG::SwitchLowering::buildJumpTable(const CaseClusterVector &Clusters,
191192
unsigned First, unsigned Last,
192193
const SwitchInst *SI,
194+
const std::optional<SDLoc> &SL,
193195
MachineBasicBlock *DefaultMBB,
194196
CaseCluster &JTCluster) {
195197
assert(First <= Last);
@@ -251,7 +253,7 @@ bool SwitchCG::SwitchLowering::buildJumpTable(const CaseClusterVector &Clusters,
251253
->createJumpTableIndex(Table);
252254

253255
// Set up the jump table info.
254-
JumpTable JT(-1U, JTI, JumpTableMBB, nullptr);
256+
JumpTable JT(-1U, JTI, JumpTableMBB, nullptr, SL);
255257
JumpTableHeader JTH(Clusters[First].Low->getValue(),
256258
Clusters[Last].High->getValue(), SI->getCondition(),
257259
nullptr, false);

llvm/test/DebugInfo/X86/debug-info-jump-table.ll

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ define dso_local void @foo(i32 noundef %cond) local_unnamed_addr #0 !dbg !42 {
2424
;CHECK: Initial selection DAG: %bb.{{[0-9]+}} 'foo:entry'
2525
;CHECK: SelectionDAG has 5 nodes:
2626
;CHECK: [[TMP1:t.*]]: ch,glue = EntryToken
27-
;CHECK: [[TMP2:t.*]]: i64,ch = CopyFromReg [[TMP1]], Register:i64 %{{[0-9]+}}
28-
;CHECK: t{{[0-9]+}}: ch = br_jt [[TMP2]]:1, JumpTable:i64<0>, [[TMP2]]
27+
;CHECK: [[TMP2:t.*]]: i64,ch = CopyFromReg [[TMP1]], Register:i64 %{{[0-9]+}}, jump_table.c:4:3
28+
;CHECK: t{{[0-9]+}}: ch = br_jt [[TMP2]]:1, JumpTable:i64<0>, [[TMP2]], jump_table.c:4:3
2929

3030
entry:
3131
call void @llvm.dbg.value(metadata i32 %cond, metadata !47, metadata !DIExpression()), !dbg !48

0 commit comments

Comments
 (0)