1; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py
2; RUN: llc < %s -mtriple=arm64-eabi | FileCheck %s
3
4; Optimize expanded SRL/SHL used as an input of
5; SETCC comparing it with zero by removing rotation.
6;
7; See https://bugs.llvm.org/show_bug.cgi?id=50197
8define i128 @opt_setcc_lt_power_of_2(i128 %a) nounwind {
9; CHECK-LABEL: opt_setcc_lt_power_of_2:
10; CHECK:       // %bb.0:
11; CHECK-NEXT:  .LBB0_1: // %loop
12; CHECK-NEXT:    // =>This Inner Loop Header: Depth=1
13; CHECK-NEXT:    adds x0, x0, #1
14; CHECK-NEXT:    adcs x1, x1, xzr
15; CHECK-NEXT:    extr x8, x1, x0, #60
16; CHECK-NEXT:    orr x8, x8, x1, lsr #60
17; CHECK-NEXT:    cbnz x8, .LBB0_1
18; CHECK-NEXT:  // %bb.2: // %exit
19; CHECK-NEXT:    ret
20  br label %loop
21
22loop:
23  %phi.a = phi i128 [ %a, %0 ], [ %inc, %loop ]
24  %inc = add i128 %phi.a, 1
25  %cmp = icmp ult i128 %inc, 1152921504606846976
26  br i1 %cmp, label %exit, label %loop
27
28exit:
29  ret i128 %inc
30}
31
32define i1 @opt_setcc_srl_eq_zero(i128 %a) nounwind {
33; CHECK-LABEL: opt_setcc_srl_eq_zero:
34; CHECK:       // %bb.0:
35; CHECK-NEXT:    extr x8, x1, x0, #17
36; CHECK-NEXT:    orr x8, x8, x1, lsr #17
37; CHECK-NEXT:    cmp x8, #0
38; CHECK-NEXT:    cset w0, eq
39; CHECK-NEXT:    ret
40   %srl = lshr i128 %a, 17
41   %cmp = icmp eq i128 %srl, 0
42   ret i1 %cmp
43}
44
45define i1 @opt_setcc_srl_ne_zero(i128 %a) nounwind {
46; CHECK-LABEL: opt_setcc_srl_ne_zero:
47; CHECK:       // %bb.0:
48; CHECK-NEXT:    extr x8, x1, x0, #17
49; CHECK-NEXT:    orr x8, x8, x1, lsr #17
50; CHECK-NEXT:    cmp x8, #0
51; CHECK-NEXT:    cset w0, ne
52; CHECK-NEXT:    ret
53   %srl = lshr i128 %a, 17
54   %cmp = icmp ne i128 %srl, 0
55   ret i1 %cmp
56}
57
58define i1 @opt_setcc_shl_eq_zero(i128 %a) nounwind {
59; CHECK-LABEL: opt_setcc_shl_eq_zero:
60; CHECK:       // %bb.0:
61; CHECK-NEXT:    extr x8, x1, x0, #47
62; CHECK-NEXT:    orr x8, x8, x0, lsl #17
63; CHECK-NEXT:    cmp x8, #0
64; CHECK-NEXT:    cset w0, eq
65; CHECK-NEXT:    ret
66   %shl = shl i128 %a, 17
67   %cmp = icmp eq i128 %shl, 0
68   ret i1 %cmp
69}
70
71define i1 @opt_setcc_shl_ne_zero(i128 %a) nounwind {
72; CHECK-LABEL: opt_setcc_shl_ne_zero:
73; CHECK:       // %bb.0:
74; CHECK-NEXT:    extr x8, x1, x0, #47
75; CHECK-NEXT:    orr x8, x8, x0, lsl #17
76; CHECK-NEXT:    cmp x8, #0
77; CHECK-NEXT:    cset w0, ne
78; CHECK-NEXT:    ret
79   %shl = shl i128 %a, 17
80   %cmp = icmp ne i128 %shl, 0
81   ret i1 %cmp
82}
83
84; Negative test: optimization should not be applied if shift has multiple users.
85define i1 @opt_setcc_shl_eq_zero_multiple_shl_users(i128 %a) nounwind {
86; CHECK-LABEL: opt_setcc_shl_eq_zero_multiple_shl_users:
87; CHECK:       // %bb.0:
88; CHECK-NEXT:    stp x30, x19, [sp, #-16]! // 16-byte Folded Spill
89; CHECK-NEXT:    extr x1, x1, x0, #47
90; CHECK-NEXT:    lsl x0, x0, #17
91; CHECK-NEXT:    orr x8, x0, x1
92; CHECK-NEXT:    cmp x8, #0
93; CHECK-NEXT:    cset w19, eq
94; CHECK-NEXT:    bl use
95; CHECK-NEXT:    mov w0, w19
96; CHECK-NEXT:    ldp x30, x19, [sp], #16 // 16-byte Folded Reload
97; CHECK-NEXT:    ret
98   %shl = shl i128 %a, 17
99   %cmp = icmp eq i128 %shl, 0
100   call void @use(i128 %shl)
101   ret i1 %cmp
102}
103
104; Check that optimization is applied to DAG having appropriate shape
105; even if there were no actual shift's expansion.
106define i1 @opt_setcc_expanded_shl_correct_shifts(i64 %a, i64 %b) nounwind {
107; CHECK-LABEL: opt_setcc_expanded_shl_correct_shifts:
108; CHECK:       // %bb.0:
109; CHECK-NEXT:    extr x8, x0, x1, #47
110; CHECK-NEXT:    orr x8, x8, x1, lsl #17
111; CHECK-NEXT:    cmp x8, #0
112; CHECK-NEXT:    cset w0, eq
113; CHECK-NEXT:    ret
114  %shl.a = shl i64 %a, 17
115  %srl.b = lshr i64 %b, 47
116  %or.0 = or i64 %shl.a, %srl.b
117  %shl.b = shl i64 %b, 17
118  %or.1 = or i64 %or.0, %shl.b
119  %cmp = icmp eq i64 %or.1, 0
120  ret i1 %cmp
121}
122
123; Negative test: optimization should not be applied as
124; constants used in shifts do not match.
125define i1 @opt_setcc_expanded_shl_wrong_shifts(i64 %a, i64 %b) nounwind {
126; CHECK-LABEL: opt_setcc_expanded_shl_wrong_shifts:
127; CHECK:       // %bb.0:
128; CHECK-NEXT:    extr x8, x0, x1, #47
129; CHECK-NEXT:    orr x8, x8, x1, lsl #18
130; CHECK-NEXT:    cmp x8, #0
131; CHECK-NEXT:    cset w0, eq
132; CHECK-NEXT:    ret
133  %shl.a = shl i64 %a, 17
134  %srl.b = lshr i64 %b, 47
135  %or.0 = or i64 %shl.a, %srl.b
136  %shl.b = shl i64 %b, 18
137  %or.1 = or i64 %or.0, %shl.b
138  %cmp = icmp eq i64 %or.1, 0
139  ret i1 %cmp
140}
141
142declare void @use(i128 %a)
143