dagcombine-shifts.ll
5.43 KB
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py
; RUN: llc < %s -mtriple=x86_64-linux-gnu | FileCheck %s
; fold (shl (zext (lshr (A, X))), X) -> (zext (shl (lshr (A, X)), X))
; Canolicalize the sequence shl/zext/lshr performing the zeroextend
; as the last instruction of the sequence.
; This will help DAGCombiner to identify and then fold the sequence
; of shifts into a single AND.
; This transformation is profitable if the shift amounts are the same
; and if there is only one use of the zext.
define i16 @fun1(i8 zeroext %v) {
; CHECK-LABEL: fun1:
; CHECK: # %bb.0: # %entry
; CHECK-NEXT: movl %edi, %eax
; CHECK-NEXT: andl $-16, %eax
; CHECK-NEXT: # kill: def $ax killed $ax killed $eax
; CHECK-NEXT: retq
entry:
%shr = lshr i8 %v, 4
%ext = zext i8 %shr to i16
%shl = shl i16 %ext, 4
ret i16 %shl
}
define i32 @fun2(i8 zeroext %v) {
; CHECK-LABEL: fun2:
; CHECK: # %bb.0: # %entry
; CHECK-NEXT: movl %edi, %eax
; CHECK-NEXT: andl $-16, %eax
; CHECK-NEXT: retq
entry:
%shr = lshr i8 %v, 4
%ext = zext i8 %shr to i32
%shl = shl i32 %ext, 4
ret i32 %shl
}
define i32 @fun3(i16 zeroext %v) {
; CHECK-LABEL: fun3:
; CHECK: # %bb.0: # %entry
; CHECK-NEXT: movl %edi, %eax
; CHECK-NEXT: andl $-16, %eax
; CHECK-NEXT: retq
entry:
%shr = lshr i16 %v, 4
%ext = zext i16 %shr to i32
%shl = shl i32 %ext, 4
ret i32 %shl
}
define i64 @fun4(i8 zeroext %v) {
; CHECK-LABEL: fun4:
; CHECK: # %bb.0: # %entry
; CHECK-NEXT: movl %edi, %eax
; CHECK-NEXT: andl $-16, %eax
; CHECK-NEXT: retq
entry:
%shr = lshr i8 %v, 4
%ext = zext i8 %shr to i64
%shl = shl i64 %ext, 4
ret i64 %shl
}
define i64 @fun5(i16 zeroext %v) {
; CHECK-LABEL: fun5:
; CHECK: # %bb.0: # %entry
; CHECK-NEXT: movl %edi, %eax
; CHECK-NEXT: andl $-16, %eax
; CHECK-NEXT: retq
entry:
%shr = lshr i16 %v, 4
%ext = zext i16 %shr to i64
%shl = shl i64 %ext, 4
ret i64 %shl
}
define i64 @fun6(i32 zeroext %v) {
; CHECK-LABEL: fun6:
; CHECK: # %bb.0: # %entry
; CHECK-NEXT: movl %edi, %eax
; CHECK-NEXT: andl $-16, %eax
; CHECK-NEXT: retq
entry:
%shr = lshr i32 %v, 4
%ext = zext i32 %shr to i64
%shl = shl i64 %ext, 4
ret i64 %shl
}
; Don't fold the pattern if we use arithmetic shifts.
define i64 @fun7(i8 zeroext %v) {
; CHECK-LABEL: fun7:
; CHECK: # %bb.0: # %entry
; CHECK-NEXT: sarb $4, %dil
; CHECK-NEXT: movzbl %dil, %eax
; CHECK-NEXT: shlq $4, %rax
; CHECK-NEXT: retq
entry:
%shr = ashr i8 %v, 4
%ext = zext i8 %shr to i64
%shl = shl i64 %ext, 4
ret i64 %shl
}
define i64 @fun8(i16 zeroext %v) {
; CHECK-LABEL: fun8:
; CHECK: # %bb.0: # %entry
; CHECK-NEXT: movswl %di, %eax
; CHECK-NEXT: shrl $4, %eax
; CHECK-NEXT: movzwl %ax, %eax
; CHECK-NEXT: shlq $4, %rax
; CHECK-NEXT: retq
entry:
%shr = ashr i16 %v, 4
%ext = zext i16 %shr to i64
%shl = shl i64 %ext, 4
ret i64 %shl
}
define i64 @fun9(i32 zeroext %v) {
; CHECK-LABEL: fun9:
; CHECK: # %bb.0: # %entry
; CHECK-NEXT: movl %edi, %eax
; CHECK-NEXT: sarl $4, %eax
; CHECK-NEXT: shlq $4, %rax
; CHECK-NEXT: retq
entry:
%shr = ashr i32 %v, 4
%ext = zext i32 %shr to i64
%shl = shl i64 %ext, 4
ret i64 %shl
}
; Don't fold the pattern if there is more than one use of the
; operand in input to the shift left.
define i64 @fun10(i8 zeroext %v) {
; CHECK-LABEL: fun10:
; CHECK: # %bb.0: # %entry
; CHECK-NEXT: shrb $4, %dil
; CHECK-NEXT: movzbl %dil, %ecx
; CHECK-NEXT: movq %rcx, %rax
; CHECK-NEXT: shlq $4, %rax
; CHECK-NEXT: orq %rcx, %rax
; CHECK-NEXT: retq
entry:
%shr = lshr i8 %v, 4
%ext = zext i8 %shr to i64
%shl = shl i64 %ext, 4
%add = add i64 %shl, %ext
ret i64 %add
}
define i64 @fun11(i16 zeroext %v) {
; CHECK-LABEL: fun11:
; CHECK: # %bb.0: # %entry
; CHECK-NEXT: # kill: def $edi killed $edi def $rdi
; CHECK-NEXT: shrl $4, %edi
; CHECK-NEXT: movq %rdi, %rax
; CHECK-NEXT: shlq $4, %rax
; CHECK-NEXT: addq %rdi, %rax
; CHECK-NEXT: retq
entry:
%shr = lshr i16 %v, 4
%ext = zext i16 %shr to i64
%shl = shl i64 %ext, 4
%add = add i64 %shl, %ext
ret i64 %add
}
define i64 @fun12(i32 zeroext %v) {
; CHECK-LABEL: fun12:
; CHECK: # %bb.0: # %entry
; CHECK-NEXT: # kill: def $edi killed $edi def $rdi
; CHECK-NEXT: shrl $4, %edi
; CHECK-NEXT: movq %rdi, %rax
; CHECK-NEXT: shlq $4, %rax
; CHECK-NEXT: addq %rdi, %rax
; CHECK-NEXT: retq
entry:
%shr = lshr i32 %v, 4
%ext = zext i32 %shr to i64
%shl = shl i64 %ext, 4
%add = add i64 %shl, %ext
ret i64 %add
}
; PR17380
; Make sure that the combined dags are legal if we run the DAGCombiner after
; Legalization took place. The add instruction is redundant and increases by
; one the number of uses of the zext. This prevents the transformation from
; firing before dags are legalized and optimized.
; Once the add is removed, the number of uses becomes one and therefore the
; dags are canonicalized. After Legalization, we need to make sure that the
; valuetype for the shift count is legal.
; Verify also that we correctly fold the shl-shr sequence into an
; AND with bitmask.
define void @g(i32 %a) {
; CHECK-LABEL: g:
; CHECK: # %bb.0:
; CHECK-NEXT: # kill: def $edi killed $edi def $rdi
; CHECK-NEXT: andl $-4, %edi
; CHECK-NEXT: jmp f # TAILCALL
%b = lshr i32 %a, 2
%c = zext i32 %b to i64
%d = add i64 %c, 1
%e = shl i64 %c, 2
tail call void @f(i64 %e)
ret void
}
declare void @f(i64)