Skip to content

Commit 9b818af

Browse files
authored
[DebugInfo] Propagate DebugLoc from switch in simplifySwitchOfPowersOfTwo (llvm#165335)
A recent commit 00f5a1e modified simplifySwitchOfPowersOfTwo to generate a branch to handle the non-power-of-2 case when appropriate, but does not set a DebugLoc on the new branch instruction; this patch propagates the switch's DebugLoc to the new branch, as for the other instructions generated in the same block. Found using llvm#107279.
1 parent 9f50e24 commit 9b818af

File tree

2 files changed

+84
-1
lines changed

2 files changed

+84
-1
lines changed

llvm/lib/Transforms/Utils/SimplifyCFG.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7623,7 +7623,9 @@ static bool simplifySwitchOfPowersOfTwo(SwitchInst *SI, IRBuilder<> &Builder,
76237623
auto *DefaultCaseBB = SI->getDefaultDest();
76247624
BasicBlock *SplitBB = SplitBlock(OrigBB, SI, DTU);
76257625
auto It = OrigBB->getTerminator()->getIterator();
7626-
BranchInst::Create(SplitBB, DefaultCaseBB, IsPow2, It);
7626+
auto *BI = BranchInst::Create(SplitBB, DefaultCaseBB, IsPow2, It);
7627+
// BI is handling the default case for SI, and so should share its DebugLoc.
7628+
BI->setDebugLoc(SI->getDebugLoc());
76277629
It->eraseFromParent();
76287630

76297631
addPredecessorToBlock(DefaultCaseBB, OrigBB, SplitBB);
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --tool ./build/bin/opt --version 6
2+
; RUN: opt -passes='simplifycfg<switch-to-lookup>' -simplifycfg-require-and-preserve-domtree=1 -S < %s | FileCheck %s
3+
;; As we replace the switch statement with a set of instructions that may more
4+
;; efficiently perform the conditional check, the DILocation of the switch
5+
;; should be propagated to all of its replacing instructions.
6+
7+
target triple = "x86_64-unknown-linux-gnu"
8+
9+
define i32 @switch_of_powers_two_default_reachable(i32 %arg) !dbg !5 {
10+
; CHECK-LABEL: define i32 @switch_of_powers_two_default_reachable(
11+
; CHECK-SAME: i32 [[ARG:%.*]]) !dbg [[DBG5:![0-9]+]] {
12+
; CHECK-NEXT: [[ENTRY:.*]]:
13+
; CHECK-NEXT: [[TMP0:%.*]] = call i32 @llvm.ctpop.i32(i32 [[ARG]]), !dbg [[DBG8:![0-9]+]]
14+
; CHECK-NEXT: [[TMP1:%.*]] = icmp eq i32 [[TMP0]], 1, !dbg [[DBG8]]
15+
; CHECK-NEXT: br i1 [[TMP1]], label %[[ENTRY_SPLIT:.*]], label %[[RETURN:.*]], !dbg [[DBG8]]
16+
; CHECK: [[ENTRY_SPLIT]]:
17+
; CHECK-NEXT: [[TMP2:%.*]] = call i32 @llvm.cttz.i32(i32 [[ARG]], i1 true), !dbg [[DBG8]]
18+
; CHECK-NEXT: [[TMP3:%.*]] = icmp ult i32 [[TMP2]], 7, !dbg [[DBG8]]
19+
; CHECK-NEXT: br i1 [[TMP3]], label %[[SWITCH_LOOKUP:.*]], label %[[RETURN]], !dbg [[DBG8]]
20+
; CHECK: [[SWITCH_LOOKUP]]:
21+
; CHECK-NEXT: [[TMP4:%.*]] = zext nneg i32 [[TMP2]] to i64, !dbg [[DBG8]]
22+
; CHECK-NEXT: [[SWITCH_GEP:%.*]] = getelementptr inbounds [7 x i32], ptr @switch.table.switch_of_powers_two_default_reachable, i64 0, i64 [[TMP4]], !dbg [[DBG8]]
23+
; CHECK-NEXT: [[SWITCH_LOAD:%.*]] = load i32, ptr [[SWITCH_GEP]], align 4, !dbg [[DBG8]]
24+
; CHECK-NEXT: br label %[[RETURN]], !dbg [[DBG8]]
25+
; CHECK: [[RETURN]]:
26+
; CHECK-NEXT: [[PHI:%.*]] = phi i32 [ 5, %[[ENTRY]] ], [ 5, %[[ENTRY_SPLIT]] ], [ [[SWITCH_LOAD]], %[[SWITCH_LOOKUP]] ]
27+
; CHECK-NEXT: ret i32 [[PHI]]
28+
;
29+
entry:
30+
switch i32 %arg, label %default_case [
31+
i32 1, label %bb1
32+
i32 8, label %bb2
33+
i32 16, label %bb3
34+
i32 32, label %bb4
35+
i32 64, label %bb5
36+
], !dbg !8
37+
38+
default_case: ; preds = %entry
39+
br label %return
40+
41+
bb1: ; preds = %entry
42+
br label %return
43+
44+
bb2: ; preds = %entry
45+
br label %return
46+
47+
bb3: ; preds = %entry
48+
br label %return
49+
50+
bb4: ; preds = %entry
51+
br label %return
52+
53+
bb5: ; preds = %entry
54+
br label %return
55+
56+
return: ; preds = %bb5, %bb4, %bb3, %bb2, %bb1, %default_case
57+
%phi = phi i32 [ 3, %bb1 ], [ 2, %bb2 ], [ 1, %bb3 ], [ 0, %bb4 ], [ 42, %bb5 ], [ 5, %default_case ]
58+
ret i32 %phi
59+
}
60+
61+
!llvm.dbg.cu = !{!0}
62+
!llvm.debugify = !{!2, !3}
63+
!llvm.module.flags = !{!4}
64+
65+
!0 = distinct !DICompileUnit(language: DW_LANG_C, file: !1, producer: "debugify", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug)
66+
!1 = !DIFile(filename: "debugloc-switch-powers-of-two.ll", directory: "/")
67+
!2 = !{i32 9}
68+
!3 = !{i32 1}
69+
!4 = !{i32 2, !"Debug Info Version", i32 3}
70+
!5 = distinct !DISubprogram(name: "switch_of_powers_two_default_reachable", linkageName: "switch_of_powers_two_default_reachable", scope: null, file: !1, line: 1, type: !6, scopeLine: 1, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: !0, retainedNodes: !7)
71+
!6 = !DISubroutineType(types: !7)
72+
!7 = !{}
73+
!8 = !DILocation(line: 1, column: 1, scope: !5)
74+
;.
75+
; CHECK: [[META0:![0-9]+]] = distinct !DICompileUnit(language: DW_LANG_C, file: [[META1:![0-9]+]], producer: "debugify", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug)
76+
; CHECK: [[META1]] = !DIFile(filename: "{{.*}}debugloc-switch-powers-of-two.ll", directory: {{.*}})
77+
; CHECK: [[DBG5]] = distinct !DISubprogram(name: "switch_of_powers_two_default_reachable", linkageName: "switch_of_powers_two_default_reachable", scope: null, file: [[META1]], line: 1, type: [[META6:![0-9]+]], scopeLine: 1, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: [[META0]], retainedNodes: [[META7:![0-9]+]])
78+
; CHECK: [[META6]] = !DISubroutineType(types: [[META7]])
79+
; CHECK: [[META7]] = !{}
80+
; CHECK: [[DBG8]] = !DILocation(line: 1, column: 1, scope: [[DBG5]])
81+
;.

0 commit comments

Comments
 (0)