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