File: signmask-of-sext-vs-of-shl-of-zext.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 (168 lines) | stat: -rw-r--r-- 5,254 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
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
; RUN: opt < %s -instcombine -S | FileCheck %s

; If we zero-extend some value, and then immediately left-shift-out all the new
; sign bits, and apply a mask to keep only the sign bit (which is the original
; sign bit from before zero-extension), we might as well just sign-extend
; and apply the same signmask.

declare void @use32(i32)

; Basic pattern

define i32 @t0(i16 %x) {
; CHECK-LABEL: @t0(
; CHECK-NEXT:    [[X_SIGNEXT:%.*]] = sext i16 [[X:%.*]] to i32
; CHECK-NEXT:    [[R:%.*]] = and i32 [[X_SIGNEXT]], -2147483648
; CHECK-NEXT:    ret i32 [[R]]
;
  %i0 = zext i16 %x to i32
  %i1 = shl i32 %i0, 16
  %r = and i32 %i1, -2147483648
  ret i32 %r
}
define i32 @t1(i8 %x) {
; CHECK-LABEL: @t1(
; CHECK-NEXT:    [[X_SIGNEXT:%.*]] = sext i8 [[X:%.*]] to i32
; CHECK-NEXT:    [[R:%.*]] = and i32 [[X_SIGNEXT]], -2147483648
; CHECK-NEXT:    ret i32 [[R]]
;
  %i0 = zext i8 %x to i32
  %i1 = shl i32 %i0, 24
  %r = and i32 %i1, -2147483648
  ret i32 %r
}

; Some negative tests

define i32 @n2(i16 %x) {
; CHECK-LABEL: @n2(
; CHECK-NEXT:    ret i32 0
;
  %i0 = zext i16 %x to i32
  %i1 = shl i32 %i0, 15 ; undershifting
  %r = and i32 %i1, -2147483648
  ret i32 %r
}
define i32 @n3(i16 %x) {
; CHECK-LABEL: @n3(
; CHECK-NEXT:    [[I0:%.*]] = zext i16 [[X:%.*]] to i32
; CHECK-NEXT:    [[I1:%.*]] = shl i32 [[I0]], 17
; CHECK-NEXT:    [[R:%.*]] = and i32 [[I1]], -2147483648
; CHECK-NEXT:    ret i32 [[R]]
;
  %i0 = zext i16 %x to i32
  %i1 = shl i32 %i0, 17 ; overshifting
  %r = and i32 %i1, -2147483648
  ret i32 %r
}
define i32 @n4(i16 %x) {
; CHECK-LABEL: @n4(
; CHECK-NEXT:    [[I0:%.*]] = zext i16 [[X:%.*]] to i32
; CHECK-NEXT:    [[I1:%.*]] = shl nuw i32 [[I0]], 16
; CHECK-NEXT:    [[R:%.*]] = and i32 [[I1]], -1073741824
; CHECK-NEXT:    ret i32 [[R]]
;
  %i0 = zext i16 %x to i32
  %i1 = shl i32 %i0, 16
  %r = and i32 %i1, 3221225472 ; not a sign bit
  ret i32 %r
}

; Extra-use tests

define i32 @t5(i16 %x) {
; CHECK-LABEL: @t5(
; CHECK-NEXT:    [[I0:%.*]] = zext i16 [[X:%.*]] to i32
; CHECK-NEXT:    call void @use32(i32 [[I0]])
; CHECK-NEXT:    [[X_SIGNEXT:%.*]] = sext i16 [[X]] to i32
; CHECK-NEXT:    [[R:%.*]] = and i32 [[X_SIGNEXT]], -2147483648
; CHECK-NEXT:    ret i32 [[R]]
;
  %i0 = zext i16 %x to i32
  call void @use32(i32 %i0)
  %i1 = shl i32 %i0, 16
  %r = and i32 %i1, -2147483648
  ret i32 %r
}
define i32 @n6(i16 %x) {
; CHECK-LABEL: @n6(
; CHECK-NEXT:    [[I0:%.*]] = zext i16 [[X:%.*]] to i32
; CHECK-NEXT:    [[I1:%.*]] = shl nuw i32 [[I0]], 16
; CHECK-NEXT:    call void @use32(i32 [[I1]])
; CHECK-NEXT:    [[R:%.*]] = and i32 [[I1]], -2147483648
; CHECK-NEXT:    ret i32 [[R]]
;
  %i0 = zext i16 %x to i32
  %i1 = shl i32 %i0, 16 ; not one-use
  call void @use32(i32 %i1)
  %r = and i32 %i1, -2147483648
  ret i32 %r
}
define i32 @n7(i16 %x) {
; CHECK-LABEL: @n7(
; CHECK-NEXT:    [[I0:%.*]] = zext i16 [[X:%.*]] to i32
; CHECK-NEXT:    call void @use32(i32 [[I0]])
; CHECK-NEXT:    [[I1:%.*]] = shl nuw i32 [[I0]], 16
; CHECK-NEXT:    call void @use32(i32 [[I1]])
; CHECK-NEXT:    [[R:%.*]] = and i32 [[I1]], -2147483648
; CHECK-NEXT:    ret i32 [[R]]
;
  %i0 = zext i16 %x to i32 ; not one-use
  call void @use32(i32 %i0)
  %i1 = shl i32 %i0, 16 ; not one-use
  call void @use32(i32 %i1)
  %r = and i32 %i1, -2147483648
  ret i32 %r
}

; Some vector tests

define <2 x i32> @t8(<2 x i16> %x) {
; CHECK-LABEL: @t8(
; CHECK-NEXT:    [[X_SIGNEXT:%.*]] = sext <2 x i16> [[X:%.*]] to <2 x i32>
; CHECK-NEXT:    [[R:%.*]] = and <2 x i32> [[X_SIGNEXT]], <i32 -2147483648, i32 -2147483648>
; CHECK-NEXT:    ret <2 x i32> [[R]]
;
  %i0 = zext <2 x i16> %x to <2 x i32>
  %i1 = shl <2 x i32> %i0, <i32 16, i32 16>
  %r = and <2 x i32> %i1, <i32 -2147483648, i32 -2147483648>
  ret <2 x i32> %r
}
define <2 x i32> @t9(<2 x i16> %x) {
; CHECK-LABEL: @t9(
; CHECK-NEXT:    [[X_SIGNEXT:%.*]] = sext <2 x i16> [[X:%.*]] to <2 x i32>
; CHECK-NEXT:    [[R:%.*]] = and <2 x i32> [[X_SIGNEXT]], <i32 -2147483648, i32 undef>
; CHECK-NEXT:    ret <2 x i32> [[R]]
;
  %i0 = zext <2 x i16> %x to <2 x i32>
  %i1 = shl <2 x i32> %i0, <i32 16, i32 undef>
  %r = and <2 x i32> %i1, <i32 -2147483648, i32 -2147483648>
  ; Here undef can be propagated into the mask.
  ret <2 x i32> %r
}
define <2 x i32> @t10(<2 x i16> %x) {
; CHECK-LABEL: @t10(
; CHECK-NEXT:    [[X_SIGNEXT:%.*]] = sext <2 x i16> [[X:%.*]] to <2 x i32>
; CHECK-NEXT:    [[R:%.*]] = and <2 x i32> [[X_SIGNEXT]], <i32 -2147483648, i32 0>
; CHECK-NEXT:    ret <2 x i32> [[R]]
;
  %i0 = zext <2 x i16> %x to <2 x i32>
  %i1 = shl <2 x i32> %i0, <i32 16, i32 16>
  %r = and <2 x i32> %i1, <i32 -2147483648, i32 undef>
  ; CAREFUL! We can't keep undef mask here, since high bits are no longer zero,
  ; we must sanitize it to 0.
  ret <2 x i32> %r
}
define <2 x i32> @t11(<2 x i16> %x) {
; CHECK-LABEL: @t11(
; CHECK-NEXT:    [[X_SIGNEXT:%.*]] = sext <2 x i16> [[X:%.*]] to <2 x i32>
; CHECK-NEXT:    [[R:%.*]] = and <2 x i32> [[X_SIGNEXT]], <i32 -2147483648, i32 undef>
; CHECK-NEXT:    ret <2 x i32> [[R]]
;
  %i0 = zext <2 x i16> %x to <2 x i32>
  %i1 = shl <2 x i32> %i0, <i32 16, i32 undef>
  %r = and <2 x i32> %i1, <i32 -2147483648, i32 undef>
  ; Here undef mask is fine.
  ret <2 x i32> %r
}