Skip to content

Commit 8165792

Browse files
committed
[ShrinkWrap] Allow shrinkwrapping past memory accesses to jump tables
This patch adds a check for whether the memory operand is known to be a jump table and, if so, allows shrinkwrapping to continue. In the case that we are looking at a jump table, I believe it is safe to assume that the access will not be to the stack (but please correct me if I am wrong here). In the test attached, this is helpful in that we are able to generate only one instruction for each non-default case in the original switch statement. Differential Revision: https://reviews.llvm.org/D149886
1 parent 5ac48ef commit 8165792

File tree

2 files changed

+86
-2
lines changed

2 files changed

+86
-2
lines changed

llvm/lib/CodeGen/ShrinkWrap.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -288,8 +288,8 @@ INITIALIZE_PASS_END(ShrinkWrap, DEBUG_TYPE, "Shrink Wrap Pass", false, false)
288288
bool ShrinkWrap::useOrDefCSROrFI(const MachineInstr &MI,
289289
RegScavenger *RS) const {
290290
/// Check if \p Op is known to access an address not on the function's stack .
291-
/// At the moment, accesses where the underlying object is a global or a
292-
/// function argument are considered non-stack accesses. Note that the
291+
/// At the moment, accesses where the underlying object is a global, function
292+
/// argument, or jump table are considered non-stack accesses. Note that the
293293
/// caller's stack may get accessed when passing an argument via the stack,
294294
/// but not the stack of the current function.
295295
///
@@ -302,6 +302,8 @@ bool ShrinkWrap::useOrDefCSROrFI(const MachineInstr &MI,
302302
return !Arg->hasPassPointeeByValueCopyAttr();
303303
return isa<GlobalValue>(UO);
304304
}
305+
if (const PseudoSourceValue *PSV = Op->getPseudoValue())
306+
return PSV->isJumpTable();
305307
return false;
306308
};
307309
// This prevents premature stack popping when occurs a indirect stack
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py UTC_ARGS: --version 2
2+
; RUN: llc -mtriple riscv64 < %s | FileCheck %s
3+
4+
declare signext i32 @func1(ptr noundef) local_unnamed_addr
5+
declare signext i32 @func2(ptr noundef) local_unnamed_addr
6+
declare signext i32 @func3(ptr noundef) local_unnamed_addr
7+
declare signext i32 @func4(ptr noundef) local_unnamed_addr
8+
declare signext i32 @func5(ptr noundef) local_unnamed_addr
9+
declare signext i32 @default_func(ptr noundef) local_unnamed_addr
10+
11+
define dso_local signext i32 @test_shrinkwrap_jump_table(ptr noundef %m) local_unnamed_addr {
12+
; CHECK-LABEL: test_shrinkwrap_jump_table:
13+
; CHECK: # %bb.0: # %entry
14+
; CHECK-NEXT: lw a1, 0(a0)
15+
; CHECK-NEXT: addi a1, a1, -1
16+
; CHECK-NEXT: li a2, 4
17+
; CHECK-NEXT: bltu a2, a1, .LBB0_3
18+
; CHECK-NEXT: # %bb.1: # %entry
19+
; CHECK-NEXT: slli a1, a1, 2
20+
; CHECK-NEXT: lui a2, %hi(.LJTI0_0)
21+
; CHECK-NEXT: addi a2, a2, %lo(.LJTI0_0)
22+
; CHECK-NEXT: add a1, a1, a2
23+
; CHECK-NEXT: lw a1, 0(a1)
24+
; CHECK-NEXT: jr a1
25+
; CHECK-NEXT: .LBB0_2: # %sw.bb
26+
; CHECK-NEXT: tail func1@plt
27+
; CHECK-NEXT: .LBB0_3: # %sw.default
28+
; CHECK-NEXT: addi sp, sp, -16
29+
; CHECK-NEXT: .cfi_def_cfa_offset 16
30+
; CHECK-NEXT: sd ra, 8(sp) # 8-byte Folded Spill
31+
; CHECK-NEXT: .cfi_offset ra, -8
32+
; CHECK-NEXT: call default_func@plt
33+
; CHECK-NEXT: li a0, 0
34+
; CHECK-NEXT: ld ra, 8(sp) # 8-byte Folded Reload
35+
; CHECK-NEXT: addi sp, sp, 16
36+
; CHECK-NEXT: ret
37+
; CHECK-NEXT: .LBB0_4: # %sw.bb1
38+
; CHECK-NEXT: tail func2@plt
39+
; CHECK-NEXT: .LBB0_5: # %sw.bb3
40+
; CHECK-NEXT: tail func3@plt
41+
; CHECK-NEXT: .LBB0_6: # %sw.bb5
42+
; CHECK-NEXT: tail func4@plt
43+
; CHECK-NEXT: .LBB0_7: # %sw.bb7
44+
; CHECK-NEXT: tail func5@plt
45+
entry:
46+
%0 = load i32, ptr %m, align 4
47+
switch i32 %0, label %sw.default [
48+
i32 1, label %sw.bb
49+
i32 2, label %sw.bb1
50+
i32 3, label %sw.bb3
51+
i32 4, label %sw.bb5
52+
i32 5, label %sw.bb7
53+
]
54+
55+
sw.bb:
56+
%call = tail call signext i32 @func1(ptr noundef nonnull %m)
57+
br label %sw.epilog
58+
59+
sw.bb1:
60+
%call2 = tail call signext i32 @func2(ptr noundef nonnull %m)
61+
br label %sw.epilog
62+
63+
sw.bb3:
64+
%call4 = tail call signext i32 @func3(ptr noundef nonnull %m)
65+
br label %sw.epilog
66+
67+
sw.bb5:
68+
%call6 = tail call signext i32 @func4(ptr noundef nonnull %m)
69+
br label %sw.epilog
70+
71+
sw.bb7:
72+
%call8 = tail call signext i32 @func5(ptr noundef nonnull %m)
73+
br label %sw.epilog
74+
75+
sw.default:
76+
%call9 = tail call signext i32 @default_func(ptr noundef nonnull %m)
77+
br label %sw.epilog
78+
79+
sw.epilog:
80+
%ret.0 = phi i32 [ 0, %sw.default ], [ %call8, %sw.bb7 ], [ %call6, %sw.bb5 ], [ %call4, %sw.bb3 ], [ %call2, %sw.bb1 ], [ %call, %sw.bb ]
81+
ret i32 %ret.0
82+
}

0 commit comments

Comments
 (0)