1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
|
; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py
; RUN: llc < %s -mtriple=x86_64-unknown-unknown | FileCheck %s
; The fundamental problem: an add separated from other arithmetic by a sign or
; zero extension can't be combined with the later instructions. However, if the
; first add is 'nsw' or 'nuw' respectively, then we can promote the extension
; ahead of that add to allow optimizations.
define i64 @add_nsw_consts(i32 %i) {
; CHECK-LABEL: add_nsw_consts:
; CHECK: # %bb.0:
; CHECK-NEXT: movslq %edi, %rax
; CHECK-NEXT: addq $12, %rax
; CHECK-NEXT: retq
%add = add nsw i32 %i, 5
%ext = sext i32 %add to i64
%idx = add i64 %ext, 7
ret i64 %idx
}
; An x86 bonus: If we promote the sext ahead of the 'add nsw',
; we allow LEA formation and eliminate an add instruction.
define i64 @add_nsw_sext_add(i32 %i, i64 %x) {
; CHECK-LABEL: add_nsw_sext_add:
; CHECK: # %bb.0:
; CHECK-NEXT: movslq %edi, %rax
; CHECK-NEXT: leaq 5(%rax,%rsi), %rax
; CHECK-NEXT: retq
%add = add nsw i32 %i, 5
%ext = sext i32 %add to i64
%idx = add i64 %x, %ext
ret i64 %idx
}
; Throw in a scale (left shift) because an LEA can do that too.
; Use a negative constant (LEA displacement) to verify that's handled correctly.
define i64 @add_nsw_sext_lsh_add(i32 %i, i64 %x) {
; CHECK-LABEL: add_nsw_sext_lsh_add:
; CHECK: # %bb.0:
; CHECK-NEXT: movslq %edi, %rax
; CHECK-NEXT: leaq -40(%rsi,%rax,8), %rax
; CHECK-NEXT: retq
%add = add nsw i32 %i, -5
%ext = sext i32 %add to i64
%shl = shl i64 %ext, 3
%idx = add i64 %x, %shl
ret i64 %idx
}
; Don't promote the sext if it has no users. The wider add instruction needs an
; extra byte to encode.
define i64 @add_nsw_sext(i32 %i, i64 %x) {
; CHECK-LABEL: add_nsw_sext:
; CHECK: # %bb.0:
; CHECK-NEXT: addl $5, %edi
; CHECK-NEXT: movslq %edi, %rax
; CHECK-NEXT: retq
%add = add nsw i32 %i, 5
%ext = sext i32 %add to i64
ret i64 %ext
}
; The typical use case: a 64-bit system where an 'int' is used as an index into an array.
define ptr @gep8(i32 %i, ptr %x) {
; CHECK-LABEL: gep8:
; CHECK: # %bb.0:
; CHECK-NEXT: movslq %edi, %rax
; CHECK-NEXT: leaq 5(%rax,%rsi), %rax
; CHECK-NEXT: retq
%add = add nsw i32 %i, 5
%ext = sext i32 %add to i64
%idx = getelementptr i8, ptr %x, i64 %ext
ret ptr %idx
}
define ptr @gep16(i32 %i, ptr %x) {
; CHECK-LABEL: gep16:
; CHECK: # %bb.0:
; CHECK-NEXT: movslq %edi, %rax
; CHECK-NEXT: leaq -10(%rsi,%rax,2), %rax
; CHECK-NEXT: retq
%add = add nsw i32 %i, -5
%ext = sext i32 %add to i64
%idx = getelementptr i16, ptr %x, i64 %ext
ret ptr %idx
}
define ptr @gep32(i32 %i, ptr %x) {
; CHECK-LABEL: gep32:
; CHECK: # %bb.0:
; CHECK-NEXT: movslq %edi, %rax
; CHECK-NEXT: leaq 20(%rsi,%rax,4), %rax
; CHECK-NEXT: retq
%add = add nsw i32 %i, 5
%ext = sext i32 %add to i64
%idx = getelementptr i32, ptr %x, i64 %ext
ret ptr %idx
}
define ptr @gep64(i32 %i, ptr %x) {
; CHECK-LABEL: gep64:
; CHECK: # %bb.0:
; CHECK-NEXT: movslq %edi, %rax
; CHECK-NEXT: leaq -40(%rsi,%rax,8), %rax
; CHECK-NEXT: retq
%add = add nsw i32 %i, -5
%ext = sext i32 %add to i64
%idx = getelementptr i64, ptr %x, i64 %ext
ret ptr %idx
}
; LEA can't scale by 16, but the adds can still be combined into an LEA.
define ptr @gep128(i32 %i, ptr %x) {
; CHECK-LABEL: gep128:
; CHECK: # %bb.0:
; CHECK-NEXT: movslq %edi, %rax
; CHECK-NEXT: shlq $4, %rax
; CHECK-NEXT: leaq 80(%rax,%rsi), %rax
; CHECK-NEXT: retq
%add = add nsw i32 %i, 5
%ext = sext i32 %add to i64
%idx = getelementptr i128, ptr %x, i64 %ext
ret ptr %idx
}
; A bigger win can be achieved when there is more than one use of the
; sign extended value. In this case, we can eliminate sign extension
; instructions plus use more efficient addressing modes for memory ops.
define void @PR20134(ptr %a, i32 %i) {
; CHECK-LABEL: PR20134:
; CHECK: # %bb.0:
; CHECK-NEXT: movslq %esi, %rax
; CHECK-NEXT: movl 4(%rdi,%rax,4), %ecx
; CHECK-NEXT: addl 8(%rdi,%rax,4), %ecx
; CHECK-NEXT: movl %ecx, (%rdi,%rax,4)
; CHECK-NEXT: retq
%add1 = add nsw i32 %i, 1
%idx1 = sext i32 %add1 to i64
%gep1 = getelementptr i32, ptr %a, i64 %idx1
%load1 = load i32, ptr %gep1, align 4
%add2 = add nsw i32 %i, 2
%idx2 = sext i32 %add2 to i64
%gep2 = getelementptr i32, ptr %a, i64 %idx2
%load2 = load i32, ptr %gep2, align 4
%add3 = add i32 %load1, %load2
%idx3 = sext i32 %i to i64
%gep3 = getelementptr i32, ptr %a, i64 %idx3
store i32 %add3, ptr %gep3, align 4
ret void
}
; The same as @PR20134 but sign extension is replaced with zero extension
define void @PR20134_zext(ptr %a, i32 %i) {
; CHECK-LABEL: PR20134_zext:
; CHECK: # %bb.0:
; CHECK-NEXT: movl %esi, %eax
; CHECK-NEXT: movl 4(%rdi,%rax,4), %ecx
; CHECK-NEXT: addl 8(%rdi,%rax,4), %ecx
; CHECK-NEXT: movl %ecx, (%rdi,%rax,4)
; CHECK-NEXT: retq
%add1 = add nuw i32 %i, 1
%idx1 = zext i32 %add1 to i64
%gep1 = getelementptr i32, ptr %a, i64 %idx1
%load1 = load i32, ptr %gep1, align 4
%add2 = add nuw i32 %i, 2
%idx2 = zext i32 %add2 to i64
%gep2 = getelementptr i32, ptr %a, i64 %idx2
%load2 = load i32, ptr %gep2, align 4
%add3 = add i32 %load1, %load2
%idx3 = zext i32 %i to i64
%gep3 = getelementptr i32, ptr %a, i64 %idx3
store i32 %add3, ptr %gep3, align 4
ret void
}
|