File: sext-of-trunc-nsw.ll

package info (click to toggle)
llvm-toolchain-17 1%3A17.0.6-22
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,799,624 kB
  • sloc: cpp: 6,428,607; ansic: 1,383,196; asm: 793,408; python: 223,504; objc: 75,364; f90: 60,502; lisp: 33,869; pascal: 15,282; sh: 9,684; perl: 7,453; ml: 4,937; awk: 3,523; makefile: 2,889; javascript: 2,149; xml: 888; fortran: 619; cs: 573
file content (224 lines) | stat: -rw-r--r-- 6,550 bytes parent folder | download | duplicates (6)
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
218
219
220
221
222
223
224
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
; RUN: opt < %s -passes=instcombine -S | FileCheck %s

; Iff we know that trunc only chops off the sign bits,
; and not all of them, then we can bypass said trunc
; for the purpose of sign extension.

declare void @use8(i8)
declare void @use4(i4)
declare void @usevec(<2 x i8>)

define i16 @t0(i8 %x) {
; CHECK-LABEL: @t0(
; CHECK-NEXT:    [[A:%.*]] = ashr i8 [[X:%.*]], 5
; CHECK-NEXT:    call void @use8(i8 [[A]])
; CHECK-NEXT:    [[C:%.*]] = sext i8 [[A]] to i16
; CHECK-NEXT:    ret i16 [[C]]
;
  %a = ashr i8 %x, 5
  call void @use8(i8 %a)
  %b = trunc i8 %a to i4
  %c = sext i4 %b to i16
  ret i16 %c
}

define i16 @t1(i8 %x) {
; CHECK-LABEL: @t1(
; CHECK-NEXT:    [[A:%.*]] = ashr i8 [[X:%.*]], 4
; CHECK-NEXT:    call void @use8(i8 [[A]])
; CHECK-NEXT:    [[C:%.*]] = sext i8 [[A]] to i16
; CHECK-NEXT:    ret i16 [[C]]
;
  %a = ashr i8 %x, 4
  call void @use8(i8 %a)
  %b = trunc i8 %a to i4
  %c = sext i4 %b to i16
  ret i16 %c
}

; Here we don't know that %a has any sign bits from %x left
define i16 @n2(i8 %x) {
; CHECK-LABEL: @n2(
; CHECK-NEXT:    [[A:%.*]] = ashr i8 [[X:%.*]], 3
; CHECK-NEXT:    call void @use8(i8 [[A]])
; CHECK-NEXT:    [[B:%.*]] = trunc i8 [[A]] to i4
; CHECK-NEXT:    [[C:%.*]] = sext i4 [[B]] to i16
; CHECK-NEXT:    ret i16 [[C]]
;
  %a = ashr i8 %x, 3
  call void @use8(i8 %a)
  %b = trunc i8 %a to i4
  %c = sext i4 %b to i16
  ret i16 %c
}

define <2 x i16> @t3_vec(<2 x i8> %x) {
; CHECK-LABEL: @t3_vec(
; CHECK-NEXT:    [[A:%.*]] = ashr <2 x i8> [[X:%.*]], <i8 4, i8 4>
; CHECK-NEXT:    call void @usevec(<2 x i8> [[A]])
; CHECK-NEXT:    [[C:%.*]] = sext <2 x i8> [[A]] to <2 x i16>
; CHECK-NEXT:    ret <2 x i16> [[C]]
;
  %a = ashr <2 x i8> %x, <i8 4, i8 4>
  call void @usevec(<2 x i8> %a)
  %b = trunc <2 x i8> %a to <2 x i4>
  %c = sext <2 x i4> %b to <2 x i16>
  ret <2 x i16> %c
}

define <2 x i16> @t4_vec_nonsplat(<2 x i8> %x) {
; CHECK-LABEL: @t4_vec_nonsplat(
; CHECK-NEXT:    [[A:%.*]] = ashr <2 x i8> [[X:%.*]], <i8 4, i8 3>
; CHECK-NEXT:    call void @usevec(<2 x i8> [[A]])
; CHECK-NEXT:    [[B:%.*]] = trunc <2 x i8> [[A]] to <2 x i4>
; CHECK-NEXT:    [[C:%.*]] = sext <2 x i4> [[B]] to <2 x i16>
; CHECK-NEXT:    ret <2 x i16> [[C]]
;
  %a = ashr <2 x i8> %x, <i8 4, i8 3>
  call void @usevec(<2 x i8> %a)
  %b = trunc <2 x i8> %a to <2 x i4>
  %c = sext <2 x i4> %b to <2 x i16>
  ret <2 x i16> %c
}

define i16 @t5_extrause(i8 %x) {
; CHECK-LABEL: @t5_extrause(
; CHECK-NEXT:    [[A:%.*]] = ashr i8 [[X:%.*]], 5
; CHECK-NEXT:    call void @use8(i8 [[A]])
; CHECK-NEXT:    [[B:%.*]] = trunc i8 [[A]] to i4
; CHECK-NEXT:    call void @use4(i4 [[B]])
; CHECK-NEXT:    [[C:%.*]] = sext i8 [[A]] to i16
; CHECK-NEXT:    ret i16 [[C]]
;
  %a = ashr i8 %x, 5
  call void @use8(i8 %a)
  %b = trunc i8 %a to i4
  call void @use4(i4 %b)
  %c = sext i4 %b to i16
  ret i16 %c
}

define i64 @narrow_source_matching_signbits(i32 %x) {
; CHECK-LABEL: @narrow_source_matching_signbits(
; CHECK-NEXT:    [[M:%.*]] = and i32 [[X:%.*]], 7
; CHECK-NEXT:    [[A:%.*]] = shl nsw i32 -1, [[M]]
; CHECK-NEXT:    [[C:%.*]] = sext i32 [[A]] to i64
; CHECK-NEXT:    ret i64 [[C]]
;
  %m = and i32 %x, 7
  %a = shl nsw i32 -1, %m
  %b = trunc i32 %a to i8
  %c = sext i8 %b to i64
  ret i64 %c
}

; negative test - not enough sign-bits

define i64 @narrow_source_not_matching_signbits(i32 %x) {
; CHECK-LABEL: @narrow_source_not_matching_signbits(
; CHECK-NEXT:    [[M:%.*]] = and i32 [[X:%.*]], 8
; CHECK-NEXT:    [[A:%.*]] = shl nsw i32 -1, [[M]]
; CHECK-NEXT:    [[B:%.*]] = trunc i32 [[A]] to i8
; CHECK-NEXT:    [[C:%.*]] = sext i8 [[B]] to i64
; CHECK-NEXT:    ret i64 [[C]]
;
  %m = and i32 %x, 8
  %a = shl nsw i32 -1, %m
  %b = trunc i32 %a to i8
  %c = sext i8 %b to i64
  ret i64 %c
}

define i24 @wide_source_matching_signbits(i32 %x) {
; CHECK-LABEL: @wide_source_matching_signbits(
; CHECK-NEXT:    [[M:%.*]] = and i32 [[X:%.*]], 7
; CHECK-NEXT:    [[A:%.*]] = shl nsw i32 -1, [[M]]
; CHECK-NEXT:    [[C:%.*]] = trunc i32 [[A]] to i24
; CHECK-NEXT:    ret i24 [[C]]
;
  %m = and i32 %x, 7
  %a = shl nsw i32 -1, %m
  %b = trunc i32 %a to i8
  %c = sext i8 %b to i24
  ret i24 %c
}

; negative test - not enough sign-bits

define i24 @wide_source_not_matching_signbits(i32 %x) {
; CHECK-LABEL: @wide_source_not_matching_signbits(
; CHECK-NEXT:    [[M2:%.*]] = and i32 [[X:%.*]], 8
; CHECK-NEXT:    [[A:%.*]] = shl nsw i32 -1, [[M2]]
; CHECK-NEXT:    [[B:%.*]] = trunc i32 [[A]] to i8
; CHECK-NEXT:    [[C:%.*]] = sext i8 [[B]] to i24
; CHECK-NEXT:    ret i24 [[C]]
;
  %m2 = and i32 %x, 8
  %a = shl nsw i32 -1, %m2
  %b = trunc i32 %a to i8
  %c = sext i8 %b to i24
  ret i24 %c
}

define i32 @same_source_matching_signbits(i32 %x) {
; CHECK-LABEL: @same_source_matching_signbits(
; CHECK-NEXT:    [[M:%.*]] = and i32 [[X:%.*]], 7
; CHECK-NEXT:    [[A:%.*]] = shl nsw i32 -1, [[M]]
; CHECK-NEXT:    ret i32 [[A]]
;
  %m = and i32 %x, 7
  %a = shl nsw i32 -1, %m
  %b = trunc i32 %a to i8
  %c = sext i8 %b to i32
  ret i32 %c
}

; negative test - not enough sign-bits

define i32 @same_source_not_matching_signbits(i32 %x) {
; CHECK-LABEL: @same_source_not_matching_signbits(
; CHECK-NEXT:    [[M2:%.*]] = and i32 [[X:%.*]], 8
; CHECK-NEXT:    [[TMP1:%.*]] = shl i32 -16777216, [[M2]]
; CHECK-NEXT:    [[C:%.*]] = ashr exact i32 [[TMP1]], 24
; CHECK-NEXT:    ret i32 [[C]]
;
  %m2 = and i32 %x, 8
  %a = shl nsw i32 -1, %m2
  %b = trunc i32 %a to i8
  %c = sext i8 %b to i32
  ret i32 %c
}

define i32 @same_source_matching_signbits_extra_use(i32 %x) {
; CHECK-LABEL: @same_source_matching_signbits_extra_use(
; CHECK-NEXT:    [[M:%.*]] = and i32 [[X:%.*]], 7
; CHECK-NEXT:    [[A:%.*]] = shl nsw i32 -1, [[M]]
; CHECK-NEXT:    [[B:%.*]] = trunc i32 [[A]] to i8
; CHECK-NEXT:    call void @use8(i8 [[B]])
; CHECK-NEXT:    ret i32 [[A]]
;
  %m = and i32 %x, 7
  %a = shl nsw i32 -1, %m
  %b = trunc i32 %a to i8
  call void @use8(i8 %b)
  %c = sext i8 %b to i32
  ret i32 %c
}

define i32 @same_source_not_matching_signbits_extra_use(i32 %x) {
; CHECK-LABEL: @same_source_not_matching_signbits_extra_use(
; CHECK-NEXT:    [[M2:%.*]] = and i32 [[X:%.*]], 8
; CHECK-NEXT:    [[A:%.*]] = shl nsw i32 -1, [[M2]]
; CHECK-NEXT:    [[B:%.*]] = trunc i32 [[A]] to i8
; CHECK-NEXT:    call void @use8(i8 [[B]])
; CHECK-NEXT:    [[C:%.*]] = sext i8 [[B]] to i32
; CHECK-NEXT:    ret i32 [[C]]
;
  %m2 = and i32 %x, 8
  %a = shl nsw i32 -1, %m2
  %b = trunc i32 %a to i8
  call void @use8(i8 %b)
  %c = sext i8 %b to i32
  ret i32 %c
}