blob: 7a157ecd3fe644abccb8bfa03ec6a9ed18e64922 [file] [log] [blame]
James Y Knight7c905062015-11-23 21:33:58 +00001; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py
Sanjay Patele2b52802015-10-14 21:47:03 +00002; RUN: llc < %s -mtriple=x86_64-unknown-unknown | FileCheck %s
3
Artur Pilipenko87e40382016-08-12 16:08:30 +00004; 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.
Sanjay Patele2b52802015-10-14 21:47:03 +00008
9define i64 @add_nsw_consts(i32 %i) {
10; CHECK-LABEL: add_nsw_consts:
11; CHECK: # BB#0:
Sanjay Patele2b52802015-10-14 21:47:03 +000012; CHECK-NEXT: movslq %edi, %rax
Sanjay Patelbbd52442015-10-16 22:14:12 +000013; CHECK-NEXT: addq $12, %rax
Sanjay Patele2b52802015-10-14 21:47:03 +000014; CHECK-NEXT: retq
15
16 %add = add nsw i32 %i, 5
17 %ext = sext i32 %add to i64
18 %idx = add i64 %ext, 7
19 ret i64 %idx
20}
21
22; An x86 bonus: If we promote the sext ahead of the 'add nsw',
23; we allow LEA formation and eliminate an add instruction.
24
25define i64 @add_nsw_sext_add(i32 %i, i64 %x) {
26; CHECK-LABEL: add_nsw_sext_add:
27; CHECK: # BB#0:
Sanjay Patele2b52802015-10-14 21:47:03 +000028; CHECK-NEXT: movslq %edi, %rax
Matt Arsenault982224c2016-02-27 19:57:45 +000029; CHECK-NEXT: leaq 5(%rsi,%rax), %rax
Sanjay Patele2b52802015-10-14 21:47:03 +000030; CHECK-NEXT: retq
31
32 %add = add nsw i32 %i, 5
33 %ext = sext i32 %add to i64
34 %idx = add i64 %x, %ext
35 ret i64 %idx
36}
37
38; Throw in a scale (left shift) because an LEA can do that too.
39; Use a negative constant (LEA displacement) to verify that's handled correctly.
40
41define i64 @add_nsw_sext_lsh_add(i32 %i, i64 %x) {
42; CHECK-LABEL: add_nsw_sext_lsh_add:
43; CHECK: # BB#0:
Sanjay Patele2b52802015-10-14 21:47:03 +000044; CHECK-NEXT: movslq %edi, %rax
Sanjay Patelbbd52442015-10-16 22:14:12 +000045; CHECK-NEXT: leaq -40(%rsi,%rax,8), %rax
Sanjay Patele2b52802015-10-14 21:47:03 +000046; CHECK-NEXT: retq
47
48 %add = add nsw i32 %i, -5
49 %ext = sext i32 %add to i64
50 %shl = shl i64 %ext, 3
51 %idx = add i64 %x, %shl
52 ret i64 %idx
53}
54
55; Don't promote the sext if it has no users. The wider add instruction needs an
56; extra byte to encode.
57
58define i64 @add_nsw_sext(i32 %i, i64 %x) {
59; CHECK-LABEL: add_nsw_sext:
60; CHECK: # BB#0:
61; CHECK-NEXT: addl $5, %edi
62; CHECK-NEXT: movslq %edi, %rax
63; CHECK-NEXT: retq
64
65 %add = add nsw i32 %i, 5
66 %ext = sext i32 %add to i64
67 ret i64 %ext
68}
69
70; The typical use case: a 64-bit system where an 'int' is used as an index into an array.
71
72define i8* @gep8(i32 %i, i8* %x) {
73; CHECK-LABEL: gep8:
74; CHECK: # BB#0:
Sanjay Patele2b52802015-10-14 21:47:03 +000075; CHECK-NEXT: movslq %edi, %rax
Matt Arsenault982224c2016-02-27 19:57:45 +000076; CHECK-NEXT: leaq 5(%rsi,%rax), %rax
Sanjay Patele2b52802015-10-14 21:47:03 +000077; CHECK-NEXT: retq
78
79 %add = add nsw i32 %i, 5
80 %ext = sext i32 %add to i64
81 %idx = getelementptr i8, i8* %x, i64 %ext
82 ret i8* %idx
83}
84
85define i16* @gep16(i32 %i, i16* %x) {
86; CHECK-LABEL: gep16:
87; CHECK: # BB#0:
Sanjay Patele2b52802015-10-14 21:47:03 +000088; CHECK-NEXT: movslq %edi, %rax
Sanjay Patelbbd52442015-10-16 22:14:12 +000089; CHECK-NEXT: leaq -10(%rsi,%rax,2), %rax
Sanjay Patele2b52802015-10-14 21:47:03 +000090; CHECK-NEXT: retq
91
92 %add = add nsw i32 %i, -5
93 %ext = sext i32 %add to i64
94 %idx = getelementptr i16, i16* %x, i64 %ext
95 ret i16* %idx
96}
97
98define i32* @gep32(i32 %i, i32* %x) {
99; CHECK-LABEL: gep32:
100; CHECK: # BB#0:
Sanjay Patele2b52802015-10-14 21:47:03 +0000101; CHECK-NEXT: movslq %edi, %rax
Sanjay Patelbbd52442015-10-16 22:14:12 +0000102; CHECK-NEXT: leaq 20(%rsi,%rax,4), %rax
Sanjay Patele2b52802015-10-14 21:47:03 +0000103; CHECK-NEXT: retq
104
105 %add = add nsw i32 %i, 5
106 %ext = sext i32 %add to i64
107 %idx = getelementptr i32, i32* %x, i64 %ext
108 ret i32* %idx
109}
110
111define i64* @gep64(i32 %i, i64* %x) {
112; CHECK-LABEL: gep64:
113; CHECK: # BB#0:
Sanjay Patele2b52802015-10-14 21:47:03 +0000114; CHECK-NEXT: movslq %edi, %rax
Sanjay Patelbbd52442015-10-16 22:14:12 +0000115; CHECK-NEXT: leaq -40(%rsi,%rax,8), %rax
Sanjay Patele2b52802015-10-14 21:47:03 +0000116; CHECK-NEXT: retq
117
118 %add = add nsw i32 %i, -5
119 %ext = sext i32 %add to i64
120 %idx = getelementptr i64, i64* %x, i64 %ext
121 ret i64* %idx
122}
123
124; LEA can't scale by 16, but the adds can still be combined into an LEA.
125
126define i128* @gep128(i32 %i, i128* %x) {
127; CHECK-LABEL: gep128:
128; CHECK: # BB#0:
Sanjay Patele2b52802015-10-14 21:47:03 +0000129; CHECK-NEXT: movslq %edi, %rax
130; CHECK-NEXT: shlq $4, %rax
Matt Arsenault982224c2016-02-27 19:57:45 +0000131; CHECK-NEXT: leaq 80(%rsi,%rax), %rax
Sanjay Patele2b52802015-10-14 21:47:03 +0000132; CHECK-NEXT: retq
133
134 %add = add nsw i32 %i, 5
135 %ext = sext i32 %add to i64
136 %idx = getelementptr i128, i128* %x, i64 %ext
137 ret i128* %idx
138}
139
140; A bigger win can be achieved when there is more than one use of the
141; sign extended value. In this case, we can eliminate sign extension
142; instructions plus use more efficient addressing modes for memory ops.
143
144define void @PR20134(i32* %a, i32 %i) {
145; CHECK-LABEL: PR20134:
146; CHECK: # BB#0:
Sanjay Patelbbd52442015-10-16 22:14:12 +0000147; CHECK-NEXT: movslq %esi, %rax
148; CHECK-NEXT: movl 4(%rdi,%rax,4), %ecx
149; CHECK-NEXT: addl 8(%rdi,%rax,4), %ecx
150; CHECK-NEXT: movl %ecx, (%rdi,%rax,4)
Sanjay Patele2b52802015-10-14 21:47:03 +0000151; CHECK-NEXT: retq
152
153 %add1 = add nsw i32 %i, 1
154 %idx1 = sext i32 %add1 to i64
155 %gep1 = getelementptr i32, i32* %a, i64 %idx1
156 %load1 = load i32, i32* %gep1, align 4
157
158 %add2 = add nsw i32 %i, 2
159 %idx2 = sext i32 %add2 to i64
160 %gep2 = getelementptr i32, i32* %a, i64 %idx2
161 %load2 = load i32, i32* %gep2, align 4
162
163 %add3 = add i32 %load1, %load2
164 %idx3 = sext i32 %i to i64
165 %gep3 = getelementptr i32, i32* %a, i64 %idx3
166 store i32 %add3, i32* %gep3, align 4
167 ret void
168}
169
Artur Pilipenko87e40382016-08-12 16:08:30 +0000170; The same as @PR20134 but sign extension is replaced with zero extension
171define void @PR20134_zext(i32* %a, i32 %i) {
172; CHECK: # BB#0:
173; CHECK-NEXT: movl %esi, %eax
174; CHECK-NEXT: movl 4(%rdi,%rax,4), %ecx
175; CHECK-NEXT: addl 8(%rdi,%rax,4), %ecx
176; CHECK-NEXT: movl %ecx, (%rdi,%rax,4)
177; CHECK-NEXT: retq
178
179 %add1 = add nuw i32 %i, 1
180 %idx1 = zext i32 %add1 to i64
181 %gep1 = getelementptr i32, i32* %a, i64 %idx1
182 %load1 = load i32, i32* %gep1, align 4
183
184 %add2 = add nuw i32 %i, 2
185 %idx2 = zext i32 %add2 to i64
186 %gep2 = getelementptr i32, i32* %a, i64 %idx2
187 %load2 = load i32, i32* %gep2, align 4
188
189 %add3 = add i32 %load1, %load2
190 %idx3 = zext i32 %i to i64
191 %gep3 = getelementptr i32, i32* %a, i64 %idx3
192 store i32 %add3, i32* %gep3, align 4
193 ret void
194}