1 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2 ; RUN: opt < %s -instcombine -S | FileCheck %s
4 ; If we zero-extend some value, and then immediately left-shift-out all the new
5 ; sign bits, and apply a mask to keep only the sign bit (which is the original
6 ; sign bit from before zero-extension), we might as well just sign-extend
7 ; and apply the same signmask.
9 declare void @use32(i32)
13 define i32 @t0(i16 %x) {
15 ; CHECK-NEXT: [[X_SIGNEXT:%.*]] = sext i16 [[X:%.*]] to i32
16 ; CHECK-NEXT: [[R:%.*]] = and i32 [[X_SIGNEXT]], -2147483648
17 ; CHECK-NEXT: ret i32 [[R]]
19 %i0 = zext i16 %x to i32
21 %r = and i32 %i1, -2147483648
24 define i32 @t1(i8 %x) {
26 ; CHECK-NEXT: [[X_SIGNEXT:%.*]] = sext i8 [[X:%.*]] to i32
27 ; CHECK-NEXT: [[R:%.*]] = and i32 [[X_SIGNEXT]], -2147483648
28 ; CHECK-NEXT: ret i32 [[R]]
30 %i0 = zext i8 %x to i32
32 %r = and i32 %i1, -2147483648
38 define i32 @n2(i16 %x) {
40 ; CHECK-NEXT: ret i32 0
42 %i0 = zext i16 %x to i32
43 %i1 = shl i32 %i0, 15 ; undershifting
44 %r = and i32 %i1, -2147483648
47 define i32 @n3(i16 %x) {
49 ; CHECK-NEXT: [[I0:%.*]] = zext i16 [[X:%.*]] to i32
50 ; CHECK-NEXT: [[I1:%.*]] = shl i32 [[I0]], 17
51 ; CHECK-NEXT: [[R:%.*]] = and i32 [[I1]], -2147483648
52 ; CHECK-NEXT: ret i32 [[R]]
54 %i0 = zext i16 %x to i32
55 %i1 = shl i32 %i0, 17 ; overshifting
56 %r = and i32 %i1, -2147483648
59 define i32 @n4(i16 %x) {
61 ; CHECK-NEXT: [[I0:%.*]] = zext i16 [[X:%.*]] to i32
62 ; CHECK-NEXT: [[I1:%.*]] = shl nuw i32 [[I0]], 16
63 ; CHECK-NEXT: [[R:%.*]] = and i32 [[I1]], -1073741824
64 ; CHECK-NEXT: ret i32 [[R]]
66 %i0 = zext i16 %x to i32
68 %r = and i32 %i1, 3221225472 ; not a sign bit
74 define i32 @t5(i16 %x) {
76 ; CHECK-NEXT: [[I0:%.*]] = zext i16 [[X:%.*]] to i32
77 ; CHECK-NEXT: call void @use32(i32 [[I0]])
78 ; CHECK-NEXT: [[X_SIGNEXT:%.*]] = sext i16 [[X]] to i32
79 ; CHECK-NEXT: [[R:%.*]] = and i32 [[X_SIGNEXT]], -2147483648
80 ; CHECK-NEXT: ret i32 [[R]]
82 %i0 = zext i16 %x to i32
83 call void @use32(i32 %i0)
85 %r = and i32 %i1, -2147483648
88 define i32 @n6(i16 %x) {
90 ; CHECK-NEXT: [[I0:%.*]] = zext i16 [[X:%.*]] to i32
91 ; CHECK-NEXT: [[I1:%.*]] = shl nuw i32 [[I0]], 16
92 ; CHECK-NEXT: call void @use32(i32 [[I1]])
93 ; CHECK-NEXT: [[R:%.*]] = and i32 [[I1]], -2147483648
94 ; CHECK-NEXT: ret i32 [[R]]
96 %i0 = zext i16 %x to i32
97 %i1 = shl i32 %i0, 16 ; not one-use
98 call void @use32(i32 %i1)
99 %r = and i32 %i1, -2147483648
102 define i32 @n7(i16 %x) {
104 ; CHECK-NEXT: [[I0:%.*]] = zext i16 [[X:%.*]] to i32
105 ; CHECK-NEXT: call void @use32(i32 [[I0]])
106 ; CHECK-NEXT: [[I1:%.*]] = shl nuw i32 [[I0]], 16
107 ; CHECK-NEXT: call void @use32(i32 [[I1]])
108 ; CHECK-NEXT: [[R:%.*]] = and i32 [[I1]], -2147483648
109 ; CHECK-NEXT: ret i32 [[R]]
111 %i0 = zext i16 %x to i32 ; not one-use
112 call void @use32(i32 %i0)
113 %i1 = shl i32 %i0, 16 ; not one-use
114 call void @use32(i32 %i1)
115 %r = and i32 %i1, -2147483648
121 define <2 x i32> @t8(<2 x i16> %x) {
123 ; CHECK-NEXT: [[X_SIGNEXT:%.*]] = sext <2 x i16> [[X:%.*]] to <2 x i32>
124 ; CHECK-NEXT: [[R:%.*]] = and <2 x i32> [[X_SIGNEXT]], <i32 -2147483648, i32 -2147483648>
125 ; CHECK-NEXT: ret <2 x i32> [[R]]
127 %i0 = zext <2 x i16> %x to <2 x i32>
128 %i1 = shl <2 x i32> %i0, <i32 16, i32 16>
129 %r = and <2 x i32> %i1, <i32 -2147483648, i32 -2147483648>
132 define <2 x i32> @t9(<2 x i16> %x) {
134 ; CHECK-NEXT: [[X_SIGNEXT:%.*]] = sext <2 x i16> [[X:%.*]] to <2 x i32>
135 ; CHECK-NEXT: [[R:%.*]] = and <2 x i32> [[X_SIGNEXT]], <i32 -2147483648, i32 undef>
136 ; CHECK-NEXT: ret <2 x i32> [[R]]
138 %i0 = zext <2 x i16> %x to <2 x i32>
139 %i1 = shl <2 x i32> %i0, <i32 16, i32 undef>
140 %r = and <2 x i32> %i1, <i32 -2147483648, i32 -2147483648>
141 ; Here undef can be propagated into the mask.
144 define <2 x i32> @t10(<2 x i16> %x) {
146 ; CHECK-NEXT: [[X_SIGNEXT:%.*]] = sext <2 x i16> [[X:%.*]] to <2 x i32>
147 ; CHECK-NEXT: [[R:%.*]] = and <2 x i32> [[X_SIGNEXT]], <i32 -2147483648, i32 0>
148 ; CHECK-NEXT: ret <2 x i32> [[R]]
150 %i0 = zext <2 x i16> %x to <2 x i32>
151 %i1 = shl <2 x i32> %i0, <i32 16, i32 16>
152 %r = and <2 x i32> %i1, <i32 -2147483648, i32 undef>
153 ; CAREFUL! We can't keep undef mask here, since high bits are no longer zero,
154 ; we must sanitize it to 0.
157 define <2 x i32> @t11(<2 x i16> %x) {
159 ; CHECK-NEXT: [[X_SIGNEXT:%.*]] = sext <2 x i16> [[X:%.*]] to <2 x i32>
160 ; CHECK-NEXT: [[R:%.*]] = and <2 x i32> [[X_SIGNEXT]], <i32 -2147483648, i32 undef>
161 ; CHECK-NEXT: ret <2 x i32> [[R]]
163 %i0 = zext <2 x i16> %x to <2 x i32>
164 %i1 = shl <2 x i32> %i0, <i32 16, i32 undef>
165 %r = and <2 x i32> %i1, <i32 -2147483648, i32 undef>
166 ; Here undef mask is fine.