1 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2 ; RUN: opt < %s -passes=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>
133 define <2 x i32> @t9(<2 x i16> %x) {
135 ; CHECK-NEXT: [[I1:%.*]] = sext <2 x i16> [[X:%.*]] to <2 x i32>
136 ; CHECK-NEXT: [[R:%.*]] = and <2 x i32> [[I1]], <i32 -2147483648, i32 -2147483648>
137 ; CHECK-NEXT: ret <2 x i32> [[R]]
139 %i0 = zext <2 x i16> %x to <2 x i32>
140 %i1 = shl <2 x i32> %i0, <i32 16, i32 poison>
141 %r = and <2 x i32> %i1, <i32 -2147483648, i32 -2147483648>
145 ; If we folded this, we wouldn't be able to keep the undef mask.
146 define <2 x i32> @t10_undef(<2 x i16> %x) {
147 ; CHECK-LABEL: @t10_undef(
148 ; CHECK-NEXT: [[I0:%.*]] = zext <2 x i16> [[X:%.*]] to <2 x i32>
149 ; CHECK-NEXT: [[I1:%.*]] = shl nuw <2 x i32> [[I0]], <i32 16, i32 16>
150 ; CHECK-NEXT: [[R:%.*]] = and <2 x i32> [[I1]], <i32 -2147483648, i32 undef>
151 ; CHECK-NEXT: ret <2 x i32> [[R]]
153 %i0 = zext <2 x i16> %x to <2 x i32>
154 %i1 = shl <2 x i32> %i0, <i32 16, i32 16>
155 %r = and <2 x i32> %i1, <i32 -2147483648, i32 undef>
159 define <2 x i32> @t10_poison(<2 x i16> %x) {
160 ; CHECK-LABEL: @t10_poison(
161 ; CHECK-NEXT: [[I1:%.*]] = sext <2 x i16> [[X:%.*]] to <2 x i32>
162 ; CHECK-NEXT: [[R:%.*]] = and <2 x i32> [[I1]], <i32 -2147483648, i32 poison>
163 ; CHECK-NEXT: ret <2 x i32> [[R]]
165 %i0 = zext <2 x i16> %x to <2 x i32>
166 %i1 = shl <2 x i32> %i0, <i32 16, i32 16>
167 %r = and <2 x i32> %i1, <i32 -2147483648, i32 poison>
168 ; CAREFUL! We can't keep poison mask here, since high bits are no longer zero,
169 ; we must sanitize it to 0.
173 define <2 x i32> @t11(<2 x i16> %x) {
175 ; CHECK-NEXT: [[X_SIGNEXT:%.*]] = sext <2 x i16> [[X:%.*]] to <2 x i32>
176 ; CHECK-NEXT: [[R:%.*]] = and <2 x i32> [[X_SIGNEXT]], <i32 -2147483648, i32 poison>
177 ; CHECK-NEXT: ret <2 x i32> [[R]]
179 %i0 = zext <2 x i16> %x to <2 x i32>
180 %i1 = shl <2 x i32> %i0, <i32 16, i32 poison>
181 %r = and <2 x i32> %i1, <i32 -2147483648, i32 poison>
182 ; Here poison mask is fine.