1; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py
2; RUN: llc -mtriple=i686-- < %s | FileCheck %s --check-prefix=X86
3; RUN: llc -mtriple=x86_64-- < %s | FileCheck %s --check-prefix=X64
4
5; Optimize expanded SRL/SHL used as an input of
6; SETCC comparing it with zero by removing rotation.
7;
8; See https://bugs.llvm.org/show_bug.cgi?id=50197
9define i128 @opt_setcc_lt_power_of_2(i128 %a) nounwind {
10; X86-LABEL: opt_setcc_lt_power_of_2:
11; X86:       # %bb.0:
12; X86-NEXT:    pushl %ebp
13; X86-NEXT:    pushl %ebx
14; X86-NEXT:    pushl %edi
15; X86-NEXT:    pushl %esi
16; X86-NEXT:    movl {{[0-9]+}}(%esp), %eax
17; X86-NEXT:    movl {{[0-9]+}}(%esp), %ecx
18; X86-NEXT:    movl {{[0-9]+}}(%esp), %edx
19; X86-NEXT:    movl {{[0-9]+}}(%esp), %esi
20; X86-NEXT:    movl {{[0-9]+}}(%esp), %edi
21; X86-NEXT:    .p2align 4, 0x90
22; X86-NEXT:  .LBB0_1: # %loop
23; X86-NEXT:    # =>This Inner Loop Header: Depth=1
24; X86-NEXT:    addl $1, %edi
25; X86-NEXT:    adcl $0, %esi
26; X86-NEXT:    adcl $0, %edx
27; X86-NEXT:    adcl $0, %ecx
28; X86-NEXT:    movl %ecx, %ebx
29; X86-NEXT:    shldl $4, %edx, %ebx
30; X86-NEXT:    movl %esi, %ebp
31; X86-NEXT:    orl %ecx, %ebp
32; X86-NEXT:    shrdl $28, %edx, %ebp
33; X86-NEXT:    orl %ebx, %ebp
34; X86-NEXT:    jne .LBB0_1
35; X86-NEXT:  # %bb.2: # %exit
36; X86-NEXT:    movl %edi, (%eax)
37; X86-NEXT:    movl %esi, 4(%eax)
38; X86-NEXT:    movl %edx, 8(%eax)
39; X86-NEXT:    movl %ecx, 12(%eax)
40; X86-NEXT:    popl %esi
41; X86-NEXT:    popl %edi
42; X86-NEXT:    popl %ebx
43; X86-NEXT:    popl %ebp
44; X86-NEXT:    retl $4
45;
46; X64-LABEL: opt_setcc_lt_power_of_2:
47; X64:       # %bb.0:
48; X64-NEXT:    movq %rsi, %rdx
49; X64-NEXT:    movq %rdi, %rax
50; X64-NEXT:    .p2align 4, 0x90
51; X64-NEXT:  .LBB0_1: # %loop
52; X64-NEXT:    # =>This Inner Loop Header: Depth=1
53; X64-NEXT:    addq $1, %rax
54; X64-NEXT:    adcq $0, %rdx
55; X64-NEXT:    movq %rax, %rcx
56; X64-NEXT:    shrq $60, %rcx
57; X64-NEXT:    orq %rdx, %rcx
58; X64-NEXT:    jne .LBB0_1
59; X64-NEXT:  # %bb.2: # %exit
60; X64-NEXT:    retq
61  br label %loop
62
63loop:
64  %phi.a = phi i128 [ %a, %0 ], [ %inc, %loop ]
65  %inc = add i128 %phi.a, 1
66  %cmp = icmp ult i128 %inc, 1152921504606846976
67  br i1 %cmp, label %exit, label %loop
68
69exit:
70  ret i128 %inc
71}
72
73define i1 @opt_setcc_srl_eq_zero(i128 %a) nounwind {
74; X86-LABEL: opt_setcc_srl_eq_zero:
75; X86:       # %bb.0:
76; X86-NEXT:    pushl %esi
77; X86-NEXT:    movl {{[0-9]+}}(%esp), %eax
78; X86-NEXT:    movl {{[0-9]+}}(%esp), %ecx
79; X86-NEXT:    movl {{[0-9]+}}(%esp), %edx
80; X86-NEXT:    movl {{[0-9]+}}(%esp), %esi
81; X86-NEXT:    shrdl $17, %ecx, %eax
82; X86-NEXT:    orl %esi, %ecx
83; X86-NEXT:    shldl $15, %edx, %esi
84; X86-NEXT:    orl %esi, %eax
85; X86-NEXT:    shrdl $17, %edx, %ecx
86; X86-NEXT:    orl %eax, %ecx
87; X86-NEXT:    sete %al
88; X86-NEXT:    popl %esi
89; X86-NEXT:    retl
90;
91; X64-LABEL: opt_setcc_srl_eq_zero:
92; X64:       # %bb.0:
93; X64-NEXT:    shrq $17, %rdi
94; X64-NEXT:    orq %rsi, %rdi
95; X64-NEXT:    sete %al
96; X64-NEXT:    retq
97   %srl = lshr i128 %a, 17
98   %cmp = icmp eq i128 %srl, 0
99   ret i1 %cmp
100}
101
102define i1 @opt_setcc_srl_ne_zero(i128 %a) nounwind {
103; X86-LABEL: opt_setcc_srl_ne_zero:
104; X86:       # %bb.0:
105; X86-NEXT:    pushl %esi
106; X86-NEXT:    movl {{[0-9]+}}(%esp), %eax
107; X86-NEXT:    movl {{[0-9]+}}(%esp), %ecx
108; X86-NEXT:    movl {{[0-9]+}}(%esp), %edx
109; X86-NEXT:    movl {{[0-9]+}}(%esp), %esi
110; X86-NEXT:    shrdl $17, %ecx, %eax
111; X86-NEXT:    orl %esi, %ecx
112; X86-NEXT:    shldl $15, %edx, %esi
113; X86-NEXT:    orl %esi, %eax
114; X86-NEXT:    shrdl $17, %edx, %ecx
115; X86-NEXT:    orl %eax, %ecx
116; X86-NEXT:    setne %al
117; X86-NEXT:    popl %esi
118; X86-NEXT:    retl
119;
120; X64-LABEL: opt_setcc_srl_ne_zero:
121; X64:       # %bb.0:
122; X64-NEXT:    shrq $17, %rdi
123; X64-NEXT:    orq %rsi, %rdi
124; X64-NEXT:    setne %al
125; X64-NEXT:    retq
126   %srl = lshr i128 %a, 17
127   %cmp = icmp ne i128 %srl, 0
128   ret i1 %cmp
129}
130
131define i1 @opt_setcc_shl_eq_zero(i128 %a) nounwind {
132; X86-LABEL: opt_setcc_shl_eq_zero:
133; X86:       # %bb.0:
134; X86-NEXT:    pushl %esi
135; X86-NEXT:    movl {{[0-9]+}}(%esp), %eax
136; X86-NEXT:    movl {{[0-9]+}}(%esp), %ecx
137; X86-NEXT:    movl {{[0-9]+}}(%esp), %edx
138; X86-NEXT:    movl {{[0-9]+}}(%esp), %esi
139; X86-NEXT:    shldl $17, %edx, %esi
140; X86-NEXT:    orl %eax, %edx
141; X86-NEXT:    shldl $17, %ecx, %edx
142; X86-NEXT:    shldl $17, %eax, %ecx
143; X86-NEXT:    orl %esi, %ecx
144; X86-NEXT:    orl %ecx, %edx
145; X86-NEXT:    sete %al
146; X86-NEXT:    popl %esi
147; X86-NEXT:    retl
148;
149; X64-LABEL: opt_setcc_shl_eq_zero:
150; X64:       # %bb.0:
151; X64-NEXT:    shlq $17, %rsi
152; X64-NEXT:    orq %rdi, %rsi
153; X64-NEXT:    sete %al
154; X64-NEXT:    retq
155   %shl = shl i128 %a, 17
156   %cmp = icmp eq i128 %shl, 0
157   ret i1 %cmp
158}
159
160define i1 @opt_setcc_shl_ne_zero(i128 %a) nounwind {
161; X86-LABEL: opt_setcc_shl_ne_zero:
162; X86:       # %bb.0:
163; X86-NEXT:    pushl %esi
164; X86-NEXT:    movl {{[0-9]+}}(%esp), %eax
165; X86-NEXT:    movl {{[0-9]+}}(%esp), %ecx
166; X86-NEXT:    movl {{[0-9]+}}(%esp), %edx
167; X86-NEXT:    movl {{[0-9]+}}(%esp), %esi
168; X86-NEXT:    shldl $17, %edx, %esi
169; X86-NEXT:    orl %eax, %edx
170; X86-NEXT:    shldl $17, %ecx, %edx
171; X86-NEXT:    shldl $17, %eax, %ecx
172; X86-NEXT:    orl %esi, %ecx
173; X86-NEXT:    orl %ecx, %edx
174; X86-NEXT:    setne %al
175; X86-NEXT:    popl %esi
176; X86-NEXT:    retl
177;
178; X64-LABEL: opt_setcc_shl_ne_zero:
179; X64:       # %bb.0:
180; X64-NEXT:    shlq $17, %rsi
181; X64-NEXT:    orq %rdi, %rsi
182; X64-NEXT:    setne %al
183; X64-NEXT:    retq
184   %shl = shl i128 %a, 17
185   %cmp = icmp ne i128 %shl, 0
186   ret i1 %cmp
187}
188
189; Negative test: optimization should not be applied if shift has multiple users.
190define i1 @opt_setcc_shl_eq_zero_multiple_shl_users(i128 %a) nounwind {
191; X86-LABEL: opt_setcc_shl_eq_zero_multiple_shl_users:
192; X86:       # %bb.0:
193; X86-NEXT:    pushl %ebx
194; X86-NEXT:    pushl %edi
195; X86-NEXT:    pushl %esi
196; X86-NEXT:    movl {{[0-9]+}}(%esp), %edx
197; X86-NEXT:    movl {{[0-9]+}}(%esp), %eax
198; X86-NEXT:    movl {{[0-9]+}}(%esp), %ecx
199; X86-NEXT:    movl {{[0-9]+}}(%esp), %esi
200; X86-NEXT:    shldl $17, %esi, %edx
201; X86-NEXT:    shldl $17, %ecx, %esi
202; X86-NEXT:    shldl $17, %eax, %ecx
203; X86-NEXT:    shll $17, %eax
204; X86-NEXT:    movl %ecx, %edi
205; X86-NEXT:    orl %edx, %edi
206; X86-NEXT:    movl %eax, %ebx
207; X86-NEXT:    orl %esi, %ebx
208; X86-NEXT:    orl %edi, %ebx
209; X86-NEXT:    sete %bl
210; X86-NEXT:    pushl %edx
211; X86-NEXT:    pushl %esi
212; X86-NEXT:    pushl %ecx
213; X86-NEXT:    pushl %eax
214; X86-NEXT:    calll use@PLT
215; X86-NEXT:    addl $16, %esp
216; X86-NEXT:    movl %ebx, %eax
217; X86-NEXT:    popl %esi
218; X86-NEXT:    popl %edi
219; X86-NEXT:    popl %ebx
220; X86-NEXT:    retl
221;
222; X64-LABEL: opt_setcc_shl_eq_zero_multiple_shl_users:
223; X64:       # %bb.0:
224; X64-NEXT:    pushq %rbx
225; X64-NEXT:    shldq $17, %rdi, %rsi
226; X64-NEXT:    shlq $17, %rdi
227; X64-NEXT:    movq %rdi, %rax
228; X64-NEXT:    orq %rsi, %rax
229; X64-NEXT:    sete %bl
230; X64-NEXT:    callq use@PLT
231; X64-NEXT:    movl %ebx, %eax
232; X64-NEXT:    popq %rbx
233; X64-NEXT:    retq
234   %shl = shl i128 %a, 17
235   %cmp = icmp eq i128 %shl, 0
236   call void @use(i128 %shl)
237   ret i1 %cmp
238}
239
240; Check that optimization is applied to DAG having appropriate shape
241; even if there were no actual shift's expansion.
242define i1 @opt_setcc_expanded_shl_correct_shifts(i64 %a, i64 %b) nounwind {
243; X86-LABEL: opt_setcc_expanded_shl_correct_shifts:
244; X86:       # %bb.0:
245; X86-NEXT:    movl {{[0-9]+}}(%esp), %eax
246; X86-NEXT:    movl {{[0-9]+}}(%esp), %ecx
247; X86-NEXT:    orl {{[0-9]+}}(%esp), %ecx
248; X86-NEXT:    movl {{[0-9]+}}(%esp), %edx
249; X86-NEXT:    orl %eax, %edx
250; X86-NEXT:    shldl $17, %ecx, %edx
251; X86-NEXT:    shldl $17, %eax, %ecx
252; X86-NEXT:    orl %edx, %ecx
253; X86-NEXT:    sete %al
254; X86-NEXT:    retl
255;
256; X64-LABEL: opt_setcc_expanded_shl_correct_shifts:
257; X64:       # %bb.0:
258; X64-NEXT:    shlq $17, %rdi
259; X64-NEXT:    orq %rsi, %rdi
260; X64-NEXT:    sete %al
261; X64-NEXT:    retq
262  %shl.a = shl i64 %a, 17
263  %srl.b = lshr i64 %b, 47
264  %or.0 = or i64 %shl.a, %srl.b
265  %shl.b = shl i64 %b, 17
266  %or.1 = or i64 %or.0, %shl.b
267  %cmp = icmp eq i64 %or.1, 0
268  ret i1 %cmp
269}
270
271; Negative test: optimization should not be applied as
272; constants used in shifts do not match.
273define i1 @opt_setcc_expanded_shl_wrong_shifts(i64 %a, i64 %b) nounwind {
274; X86-LABEL: opt_setcc_expanded_shl_wrong_shifts:
275; X86:       # %bb.0:
276; X86-NEXT:    pushl %esi
277; X86-NEXT:    movl {{[0-9]+}}(%esp), %eax
278; X86-NEXT:    movl {{[0-9]+}}(%esp), %ecx
279; X86-NEXT:    movl {{[0-9]+}}(%esp), %edx
280; X86-NEXT:    movl {{[0-9]+}}(%esp), %esi
281; X86-NEXT:    shldl $17, %edx, %esi
282; X86-NEXT:    shldl $17, %ecx, %edx
283; X86-NEXT:    shldl $18, %eax, %ecx
284; X86-NEXT:    shll $18, %eax
285; X86-NEXT:    orl %edx, %eax
286; X86-NEXT:    orl %esi, %ecx
287; X86-NEXT:    orl %eax, %ecx
288; X86-NEXT:    sete %al
289; X86-NEXT:    popl %esi
290; X86-NEXT:    retl
291;
292; X64-LABEL: opt_setcc_expanded_shl_wrong_shifts:
293; X64:       # %bb.0:
294; X64-NEXT:    shldq $17, %rsi, %rdi
295; X64-NEXT:    shlq $18, %rsi
296; X64-NEXT:    orq %rdi, %rsi
297; X64-NEXT:    sete %al
298; X64-NEXT:    retq
299  %shl.a = shl i64 %a, 17
300  %srl.b = lshr i64 %b, 47
301  %or.0 = or i64 %shl.a, %srl.b
302  %shl.b = shl i64 %b, 18
303  %or.1 = or i64 %or.0, %shl.b
304  %cmp = icmp eq i64 %or.1, 0
305  ret i1 %cmp
306}
307
308declare void @use(i128 %a)
309