File: signext.ll

package info (click to toggle)
llvm-toolchain-14 1%3A14.0.6-12
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 1,496,180 kB
  • sloc: cpp: 5,593,972; ansic: 986,872; asm: 585,869; python: 184,223; objc: 72,530; lisp: 31,119; f90: 27,793; javascript: 9,780; pascal: 9,762; sh: 9,482; perl: 7,468; ml: 5,432; awk: 3,523; makefile: 2,538; xml: 953; cs: 573; fortran: 567
file content (191 lines) | stat: -rw-r--r-- 5,554 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
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
; RUN: opt < %s -instcombine -S | FileCheck %s

target datalayout = "n8:16:32:64"

declare void @use(i32)

define i32 @sextinreg(i32 %x) {
; CHECK-LABEL: @sextinreg(
; CHECK-NEXT:    [[SEXT:%.*]] = shl i32 [[X:%.*]], 16
; CHECK-NEXT:    [[T3:%.*]] = ashr exact i32 [[SEXT]], 16
; CHECK-NEXT:    ret i32 [[T3]]
;
  %t1 = and i32 %x, 65535
  %t2 = xor i32 %t1, -32768
  %t3 = add i32 %t2, 32768
  ret i32 %t3
}

define i32 @sextinreg_extra_use(i32 %x) {
; CHECK-LABEL: @sextinreg_extra_use(
; CHECK-NEXT:    [[T1:%.*]] = and i32 [[X:%.*]], 65535
; CHECK-NEXT:    [[T2:%.*]] = xor i32 [[T1]], -32768
; CHECK-NEXT:    call void @use(i32 [[T2]])
; CHECK-NEXT:    [[T3:%.*]] = add nsw i32 [[T2]], 32768
; CHECK-NEXT:    ret i32 [[T3]]
;
  %t1 = and i32 %x, 65535
  %t2 = xor i32 %t1, -32768
  call void @use(i32 %t2)
  %t3 = add i32 %t2, 32768
  ret i32 %t3
}

define <2 x i32> @sextinreg_splat(<2 x i32> %x) {
; CHECK-LABEL: @sextinreg_splat(
; CHECK-NEXT:    [[SEXT:%.*]] = shl <2 x i32> [[X:%.*]], <i32 16, i32 16>
; CHECK-NEXT:    [[T3:%.*]] = ashr exact <2 x i32> [[SEXT]], <i32 16, i32 16>
; CHECK-NEXT:    ret <2 x i32> [[T3]]
;
  %t1 = and <2 x i32> %x, <i32 65535, i32 65535>
  %t2 = xor <2 x i32> %t1, <i32 -32768, i32 -32768>
  %t3 = add <2 x i32> %t2, <i32 32768, i32 32768>
  ret <2 x i32> %t3
}

define i32 @sextinreg_alt(i32 %x) {
; CHECK-LABEL: @sextinreg_alt(
; CHECK-NEXT:    [[SEXT:%.*]] = shl i32 [[X:%.*]], 16
; CHECK-NEXT:    [[T3:%.*]] = ashr exact i32 [[SEXT]], 16
; CHECK-NEXT:    ret i32 [[T3]]
;
  %t1 = and i32 %x, 65535
  %t2 = xor i32 %t1, 32768
  %t3 = add i32 %t2, -32768
  ret i32 %t3
}

define <2 x i32> @sextinreg_alt_splat(<2 x i32> %x) {
; CHECK-LABEL: @sextinreg_alt_splat(
; CHECK-NEXT:    [[SEXT:%.*]] = shl <2 x i32> [[X:%.*]], <i32 16, i32 16>
; CHECK-NEXT:    [[T3:%.*]] = ashr exact <2 x i32> [[SEXT]], <i32 16, i32 16>
; CHECK-NEXT:    ret <2 x i32> [[T3]]
;
  %t1 = and <2 x i32> %x, <i32 65535, i32 65535>
  %t2 = xor <2 x i32> %t1, <i32 32768, i32 32768>
  %t3 = add <2 x i32> %t2, <i32 -32768, i32 -32768>
  ret <2 x i32> %t3
}

define i32 @sext(i16 %P) {
; CHECK-LABEL: @sext(
; CHECK-NEXT:    [[T5:%.*]] = sext i16 [[P:%.*]] to i32
; CHECK-NEXT:    ret i32 [[T5]]
;
  %t1 = zext i16 %P to i32
  %t4 = xor i32 %t1, 32768
  %t5 = add i32 %t4, -32768
  ret i32 %t5
}

define i32 @sext_extra_use(i16 %P) {
; CHECK-LABEL: @sext_extra_use(
; CHECK-NEXT:    [[TMP1:%.*]] = xor i16 [[P:%.*]], -32768
; CHECK-NEXT:    [[T4:%.*]] = zext i16 [[TMP1]] to i32
; CHECK-NEXT:    call void @use(i32 [[T4]])
; CHECK-NEXT:    [[T5:%.*]] = sext i16 [[P]] to i32
; CHECK-NEXT:    ret i32 [[T5]]
;
  %t1 = zext i16 %P to i32
  %t4 = xor i32 %t1, 32768
  call void @use(i32 %t4)
  %t5 = add i32 %t4, -32768
  ret i32 %t5
}

define <2 x i32> @sext_splat(<2 x i16> %P) {
; CHECK-LABEL: @sext_splat(
; CHECK-NEXT:    [[T5:%.*]] = sext <2 x i16> [[P:%.*]] to <2 x i32>
; CHECK-NEXT:    ret <2 x i32> [[T5]]
;
  %t1 = zext <2 x i16> %P to <2 x i32>
  %t4 = xor <2 x i32> %t1, <i32 32768, i32 32768>
  %t5 = add <2 x i32> %t4, <i32 -32768, i32 -32768>
  ret <2 x i32> %t5
}

define i32 @sextinreg2(i32 %x) {
; CHECK-LABEL: @sextinreg2(
; CHECK-NEXT:    [[SEXT:%.*]] = shl i32 [[X:%.*]], 24
; CHECK-NEXT:    [[T3:%.*]] = ashr exact i32 [[SEXT]], 24
; CHECK-NEXT:    ret i32 [[T3]]
;
  %t1 = and i32 %x, 255
  %t2 = xor i32 %t1, 128
  %t3 = add i32 %t2, -128
  ret i32 %t3
}

define <2 x i32> @sextinreg2_splat(<2 x i32> %x) {
; CHECK-LABEL: @sextinreg2_splat(
; CHECK-NEXT:    [[SEXT:%.*]] = shl <2 x i32> [[X:%.*]], <i32 24, i32 24>
; CHECK-NEXT:    [[T3:%.*]] = ashr exact <2 x i32> [[SEXT]], <i32 24, i32 24>
; CHECK-NEXT:    ret <2 x i32> [[T3]]
;
  %t1 = and <2 x i32> %x, <i32 255, i32 255>
  %t2 = xor <2 x i32> %t1, <i32 128, i32 128>
  %t3 = add <2 x i32> %t2, <i32 -128, i32 -128>
  ret <2 x i32> %t3
}

define i32 @test5(i32 %x) {
; CHECK-LABEL: @test5(
; CHECK-NEXT:    [[T2:%.*]] = shl i32 [[X:%.*]], 16
; CHECK-NEXT:    [[T4:%.*]] = ashr exact i32 [[T2]], 16
; CHECK-NEXT:    ret i32 [[T4]]
;
  %t2 = shl i32 %x, 16
  %t4 = ashr i32 %t2, 16
  ret i32 %t4
}

;  If the shift amount equals the difference in width of the destination
;  and source scalar types:
;  ashr (shl (zext X), C), C --> sext X

define i32 @test6(i16 %P) {
; CHECK-LABEL: @test6(
; CHECK-NEXT:    [[T5:%.*]] = sext i16 [[P:%.*]] to i32
; CHECK-NEXT:    ret i32 [[T5]]
;
  %t1 = zext i16 %P to i32
  %sext1 = shl i32 %t1, 16
  %t5 = ashr i32 %sext1, 16
  ret i32 %t5
}

; Vectors should get the same fold as above.

define <2 x i32> @test6_splat_vec(<2 x i12> %P) {
; CHECK-LABEL: @test6_splat_vec(
; CHECK-NEXT:    [[ASHR:%.*]] = sext <2 x i12> [[P:%.*]] to <2 x i32>
; CHECK-NEXT:    ret <2 x i32> [[ASHR]]
;
  %z = zext <2 x i12> %P to <2 x i32>
  %shl = shl <2 x i32> %z, <i32 20, i32 20>
  %ashr = ashr <2 x i32> %shl, <i32 20, i32 20>
  ret <2 x i32> %ashr
}

define i32 @ashr(i32 %x) {
; CHECK-LABEL: @ashr(
; CHECK-NEXT:    [[SUB:%.*]] = ashr i32 [[X:%.*]], 5
; CHECK-NEXT:    ret i32 [[SUB]]
;
  %shr = lshr i32 %x, 5
  %xor = xor i32 %shr, 67108864
  %sub = add i32 %xor, -67108864
  ret i32 %sub
}

define <2 x i32> @ashr_splat(<2 x i32> %x) {
; CHECK-LABEL: @ashr_splat(
; CHECK-NEXT:    [[SUB:%.*]] = ashr <2 x i32> [[X:%.*]], <i32 5, i32 5>
; CHECK-NEXT:    ret <2 x i32> [[SUB]]
;
  %shr = lshr <2 x i32> %x, <i32 5, i32 5>
  %xor = xor <2 x i32> %shr, <i32 67108864, i32 67108864>
  %sub = add <2 x i32> %xor, <i32 -67108864, i32 -67108864>
  ret <2 x i32> %sub
}