1 ; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py
2 ; RUN: llc < %s -mtriple=x86_64-unknown-unknown -global-isel | FileCheck %s
4 ; The fundamental problem: an add separated from other arithmetic by a sign or
5 ; zero extension can't be combined with the later instructions. However, if the
6 ; first add is 'nsw' or 'nuw' respectively, then we can promote the extension
7 ; ahead of that add to allow optimizations.
9 define i64 @add_nsw_consts(i32 %i) {
10 ; CHECK-LABEL: add_nsw_consts:
12 ; CHECK-NEXT: addl $5, %edi
13 ; CHECK-NEXT: movslq %edi, %rax
14 ; CHECK-NEXT: addq $7, %rax
17 %add = add nsw i32 %i, 5
18 %ext = sext i32 %add to i64
19 %idx = add i64 %ext, 7
23 ; An x86 bonus: If we promote the sext ahead of the 'add nsw',
24 ; we allow LEA formation and eliminate an add instruction.
26 define i64 @add_nsw_sext_add(i32 %i, i64 %x) {
27 ; CHECK-LABEL: add_nsw_sext_add:
29 ; CHECK-NEXT: addl $5, %edi
30 ; CHECK-NEXT: movslq %edi, %rax
31 ; CHECK-NEXT: addq %rsi, %rax
34 %add = add nsw i32 %i, 5
35 %ext = sext i32 %add to i64
36 %idx = add i64 %x, %ext
40 ; Throw in a scale (left shift) because an LEA can do that too.
41 ; Use a negative constant (LEA displacement) to verify that's handled correctly.
43 define i64 @add_nsw_sext_lsh_add(i32 %i, i64 %x) {
44 ; CHECK-LABEL: add_nsw_sext_lsh_add:
46 ; CHECK-NEXT: addl $-5, %edi
47 ; CHECK-NEXT: movslq %edi, %rax
48 ; CHECK-NEXT: movq $3, %rcx
49 ; CHECK-NEXT: shlq %cl, %rax
50 ; CHECK-NEXT: addq %rsi, %rax
53 %add = add nsw i32 %i, -5
54 %ext = sext i32 %add to i64
55 %shl = shl i64 %ext, 3
56 %idx = add i64 %x, %shl
60 ; Don't promote the sext if it has no users. The wider add instruction needs an
61 ; extra byte to encode.
63 define i64 @add_nsw_sext(i32 %i, i64 %x) {
64 ; CHECK-LABEL: add_nsw_sext:
66 ; CHECK-NEXT: addl $5, %edi
67 ; CHECK-NEXT: movslq %edi, %rax
70 %add = add nsw i32 %i, 5
71 %ext = sext i32 %add to i64
75 ; The typical use case: a 64-bit system where an 'int' is used as an index into an array.
77 define i8* @gep8(i32 %i, i8* %x) {
80 ; CHECK-NEXT: addl $5, %edi
81 ; CHECK-NEXT: movslq %edi, %rax
82 ; CHECK-NEXT: addq %rsi, %rax
85 %add = add nsw i32 %i, 5
86 %ext = sext i32 %add to i64
87 %idx = getelementptr i8, i8* %x, i64 %ext
91 define i16* @gep16(i32 %i, i16* %x) {
94 ; CHECK-NEXT: addl $-5, %edi
95 ; CHECK-NEXT: movslq %edi, %rax
96 ; CHECK-NEXT: movq $2, %rcx
97 ; CHECK-NEXT: imulq %rax, %rcx
98 ; CHECK-NEXT: leaq (%rsi,%rcx), %rax
101 %add = add nsw i32 %i, -5
102 %ext = sext i32 %add to i64
103 %idx = getelementptr i16, i16* %x, i64 %ext
107 define i32* @gep32(i32 %i, i32* %x) {
108 ; CHECK-LABEL: gep32:
110 ; CHECK-NEXT: addl $5, %edi
111 ; CHECK-NEXT: movslq %edi, %rax
112 ; CHECK-NEXT: movq $4, %rcx
113 ; CHECK-NEXT: imulq %rax, %rcx
114 ; CHECK-NEXT: leaq (%rsi,%rcx), %rax
117 %add = add nsw i32 %i, 5
118 %ext = sext i32 %add to i64
119 %idx = getelementptr i32, i32* %x, i64 %ext
123 define i64* @gep64(i32 %i, i64* %x) {
124 ; CHECK-LABEL: gep64:
126 ; CHECK-NEXT: addl $-5, %edi
127 ; CHECK-NEXT: movslq %edi, %rax
128 ; CHECK-NEXT: movq $8, %rcx
129 ; CHECK-NEXT: imulq %rax, %rcx
130 ; CHECK-NEXT: leaq (%rsi,%rcx), %rax
133 %add = add nsw i32 %i, -5
134 %ext = sext i32 %add to i64
135 %idx = getelementptr i64, i64* %x, i64 %ext
139 ; LEA can't scale by 16, but the adds can still be combined into an LEA.
141 define i128* @gep128(i32 %i, i128* %x) {
142 ; CHECK-LABEL: gep128:
144 ; CHECK-NEXT: addl $5, %edi
145 ; CHECK-NEXT: movslq %edi, %rax
146 ; CHECK-NEXT: movq $16, %rcx
147 ; CHECK-NEXT: imulq %rax, %rcx
148 ; CHECK-NEXT: leaq (%rsi,%rcx), %rax
151 %add = add nsw i32 %i, 5
152 %ext = sext i32 %add to i64
153 %idx = getelementptr i128, i128* %x, i64 %ext
157 ; A bigger win can be achieved when there is more than one use of the
158 ; sign extended value. In this case, we can eliminate sign extension
159 ; instructions plus use more efficient addressing modes for memory ops.
161 define void @PR20134(i32* %a, i32 %i) {
162 ; CHECK-LABEL: PR20134:
164 ; CHECK-NEXT: # kill: def $esi killed $esi def $rsi
165 ; CHECK-NEXT: leal 1(%rsi), %eax
167 ; CHECK-NEXT: movq $4, %rcx
168 ; CHECK-NEXT: imulq %rcx, %rax
169 ; CHECK-NEXT: addq %rdi, %rax
170 ; CHECK-NEXT: leal 2(%rsi), %edx
171 ; CHECK-NEXT: movslq %edx, %rdx
172 ; CHECK-NEXT: imulq %rcx, %rdx
173 ; CHECK-NEXT: addq %rdi, %rdx
174 ; CHECK-NEXT: movl (%rdx), %edx
175 ; CHECK-NEXT: addl (%rax), %edx
176 ; CHECK-NEXT: movslq %esi, %rax
177 ; CHECK-NEXT: imulq %rcx, %rax
178 ; CHECK-NEXT: addq %rdi, %rax
179 ; CHECK-NEXT: movl %edx, (%rax)
182 %add1 = add nsw i32 %i, 1
183 %idx1 = sext i32 %add1 to i64
184 %gep1 = getelementptr i32, i32* %a, i64 %idx1
185 %load1 = load i32, i32* %gep1, align 4
187 %add2 = add nsw i32 %i, 2
188 %idx2 = sext i32 %add2 to i64
189 %gep2 = getelementptr i32, i32* %a, i64 %idx2
190 %load2 = load i32, i32* %gep2, align 4
192 %add3 = add i32 %load1, %load2
193 %idx3 = sext i32 %i to i64
194 %gep3 = getelementptr i32, i32* %a, i64 %idx3
195 store i32 %add3, i32* %gep3, align 4
199 ; The same as @PR20134 but sign extension is replaced with zero extension
200 define void @PR20134_zext(i32* %a, i32 %i) {
201 ; CHECK-LABEL: PR20134_zext:
203 ; CHECK-NEXT: # kill: def $esi killed $esi def $rsi
204 ; CHECK-NEXT: leal 1(%rsi), %eax
205 ; CHECK-NEXT: movq $4, %rcx
206 ; CHECK-NEXT: imulq %rcx, %rax
207 ; CHECK-NEXT: addq %rdi, %rax
208 ; CHECK-NEXT: leal 2(%rsi), %edx
209 ; CHECK-NEXT: imulq %rcx, %rdx
210 ; CHECK-NEXT: addq %rdi, %rdx
211 ; CHECK-NEXT: movl (%rdx), %edx
212 ; CHECK-NEXT: addl (%rax), %edx
213 ; CHECK-NEXT: imulq %rcx, %rsi
214 ; CHECK-NEXT: leaq (%rdi,%rsi), %rax
215 ; CHECK-NEXT: movl %edx, (%rax)
218 %add1 = add nuw i32 %i, 1
219 %idx1 = zext i32 %add1 to i64
220 %gep1 = getelementptr i32, i32* %a, i64 %idx1
221 %load1 = load i32, i32* %gep1, align 4
223 %add2 = add nuw i32 %i, 2
224 %idx2 = zext i32 %add2 to i64
225 %gep2 = getelementptr i32, i32* %a, i64 %idx2
226 %load2 = load i32, i32* %gep2, align 4
228 %add3 = add i32 %load1, %load2
229 %idx3 = zext i32 %i to i64
230 %gep3 = getelementptr i32, i32* %a, i64 %idx3
231 store i32 %add3, i32* %gep3, align 4