1 ; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py
2 ; RUN: llc -mtriple=aarch64-unknown-linux-gnu < %s | FileCheck %s
4 ; This tests the BuildREMEqFold optimization with UREM, i32, odd divisor, SETEQ.
5 ; The corresponding pseudocode is:
6 ; Q <- [N * multInv(5, 2^32)] <=> [N * 0xCCCCCCCD] <=> [N * (-858993459)]
7 ; res <- [Q <= (2^32 - 1) / 5] <=> [Q <= 858993459] <=> [Q < 858993460]
8 define i32 @test_urem_odd(i32 %X) nounwind readnone {
9 ; CHECK-LABEL: test_urem_odd:
11 ; CHECK-NEXT: mov w8, #52429
12 ; CHECK-NEXT: movk w8, #52428, lsl #16
13 ; CHECK-NEXT: umull x8, w0, w8
14 ; CHECK-NEXT: lsr x8, x8, #34
15 ; CHECK-NEXT: add w8, w8, w8, lsl #2
16 ; CHECK-NEXT: cmp w0, w8
17 ; CHECK-NEXT: cset w0, eq
19 %urem = urem i32 %X, 5
20 %cmp = icmp eq i32 %urem, 0
21 %ret = zext i1 %cmp to i32
25 ; This is like test_urem_odd, except the divisor has bit 30 set.
26 define i32 @test_urem_odd_bit30(i32 %X) nounwind readnone {
27 ; CHECK-LABEL: test_urem_odd_bit30:
29 ; CHECK-NEXT: mov w8, #-11
30 ; CHECK-NEXT: umull x8, w0, w8
31 ; CHECK-NEXT: mov w9, #3
32 ; CHECK-NEXT: lsr x8, x8, #62
33 ; CHECK-NEXT: movk w9, #16384, lsl #16
34 ; CHECK-NEXT: msub w8, w8, w9, w0
35 ; CHECK-NEXT: cmp w8, #0 // =0
36 ; CHECK-NEXT: cset w0, eq
38 %urem = urem i32 %X, 1073741827
39 %cmp = icmp eq i32 %urem, 0
40 %ret = zext i1 %cmp to i32
44 ; This is like test_urem_odd, except the divisor has bit 31 set.
45 define i32 @test_urem_odd_bit31(i32 %X) nounwind readnone {
46 ; CHECK-LABEL: test_urem_odd_bit31:
48 ; CHECK-NEXT: mov w8, w0
49 ; CHECK-NEXT: lsl x9, x8, #30
50 ; CHECK-NEXT: sub x8, x9, x8
51 ; CHECK-NEXT: lsr x8, x8, #61
52 ; CHECK-NEXT: mov w9, #-2147483645
53 ; CHECK-NEXT: msub w8, w8, w9, w0
54 ; CHECK-NEXT: cmp w8, #0 // =0
55 ; CHECK-NEXT: cset w0, eq
57 %urem = urem i32 %X, 2147483651
58 %cmp = icmp eq i32 %urem, 0
59 %ret = zext i1 %cmp to i32
63 ; This tests the BuildREMEqFold optimization with UREM, i16, even divisor, SETNE.
64 ; In this case, D <=> 14 <=> 7 * 2^1, so D0 = 7 and K = 1.
65 ; The corresponding pseudocode is:
66 ; Q <- [N * multInv(D0, 2^16)] <=> [N * multInv(7, 2^16)] <=> [N * 28087]
67 ; Q <- [Q >>rot K] <=> [Q >>rot 1]
68 ; res <- ![Q <= (2^16 - 1) / 7] <=> ![Q <= 9362] <=> [Q > 9362]
69 define i16 @test_urem_even(i16 %X) nounwind readnone {
70 ; CHECK-LABEL: test_urem_even:
72 ; CHECK-NEXT: mov w10, #9363
73 ; CHECK-NEXT: ubfx w9, w0, #1, #15
74 ; CHECK-NEXT: movk w10, #37449, lsl #16
75 ; CHECK-NEXT: umull x9, w9, w10
76 ; CHECK-NEXT: and w8, w0, #0xffff
77 ; CHECK-NEXT: lsr x9, x9, #34
78 ; CHECK-NEXT: orr w10, wzr, #0xe
79 ; CHECK-NEXT: msub w8, w9, w10, w8
80 ; CHECK-NEXT: cmp w8, #0 // =0
81 ; CHECK-NEXT: cset w0, ne
83 %urem = urem i16 %X, 14
84 %cmp = icmp ne i16 %urem, 0
85 %ret = zext i1 %cmp to i16
89 ; This is like test_urem_even, except the divisor has bit 30 set.
90 define i32 @test_urem_even_bit30(i32 %X) nounwind readnone {
91 ; CHECK-LABEL: test_urem_even_bit30:
93 ; CHECK-NEXT: mov w8, #-415
94 ; CHECK-NEXT: umull x8, w0, w8
95 ; CHECK-NEXT: mov w9, #104
96 ; CHECK-NEXT: lsr x8, x8, #62
97 ; CHECK-NEXT: movk w9, #16384, lsl #16
98 ; CHECK-NEXT: msub w8, w8, w9, w0
99 ; CHECK-NEXT: cmp w8, #0 // =0
100 ; CHECK-NEXT: cset w0, eq
102 %urem = urem i32 %X, 1073741928
103 %cmp = icmp eq i32 %urem, 0
104 %ret = zext i1 %cmp to i32
108 ; This is like test_urem_odd, except the divisor has bit 31 set.
109 define i32 @test_urem_even_bit31(i32 %X) nounwind readnone {
110 ; CHECK-LABEL: test_urem_even_bit31:
112 ; CHECK-NEXT: mov w8, #65435
113 ; CHECK-NEXT: movk w8, #32767, lsl #16
114 ; CHECK-NEXT: umull x8, w0, w8
115 ; CHECK-NEXT: mov w9, #102
116 ; CHECK-NEXT: lsr x8, x8, #62
117 ; CHECK-NEXT: movk w9, #32768, lsl #16
118 ; CHECK-NEXT: msub w8, w8, w9, w0
119 ; CHECK-NEXT: cmp w8, #0 // =0
120 ; CHECK-NEXT: cset w0, eq
122 %urem = urem i32 %X, 2147483750
123 %cmp = icmp eq i32 %urem, 0
124 %ret = zext i1 %cmp to i32
128 ; We should not proceed with this fold if the divisor is 1 or -1
129 define i32 @test_urem_one(i32 %X) nounwind readnone {
130 ; CHECK-LABEL: test_urem_one:
132 ; CHECK-NEXT: orr w0, wzr, #0x1
134 %urem = urem i32 %X, 1
135 %cmp = icmp eq i32 %urem, 0
136 %ret = zext i1 %cmp to i32
140 ; We can lower remainder of division by powers of two much better elsewhere;
141 ; also, BuildREMEqFold does not work when the only odd factor of the divisor is 1.
142 ; This ensures we don't touch powers of two.
143 define i32 @test_urem_pow2(i32 %X) nounwind readnone {
144 ; CHECK-LABEL: test_urem_pow2:
146 ; CHECK-NEXT: tst w0, #0xf
147 ; CHECK-NEXT: cset w0, eq
149 %urem = urem i32 %X, 16
150 %cmp = icmp eq i32 %urem, 0
151 %ret = zext i1 %cmp to i32