1 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2 ; RUN: opt < %s -instcombine -S | FileCheck %s
4 define i32 @t1(i16 zeroext %x, i32 %y) {
7 ; CHECK-NEXT: [[CONV:%.*]] = zext i16 [[X:%.*]] to i32
8 ; CHECK-NEXT: [[TMP0:%.*]] = add i32 [[Y:%.*]], 1
9 ; CHECK-NEXT: [[D:%.*]] = lshr i32 [[CONV]], [[TMP0]]
10 ; CHECK-NEXT: ret i32 [[D]]
13 %conv = zext i16 %x to i32
15 %d = sdiv i32 %conv, %s
19 define <2 x i32> @t1vec(<2 x i16> %x, <2 x i32> %y) {
20 ; CHECK-LABEL: @t1vec(
22 ; CHECK-NEXT: [[CONV:%.*]] = zext <2 x i16> [[X:%.*]] to <2 x i32>
23 ; CHECK-NEXT: [[TMP0:%.*]] = add <2 x i32> [[Y:%.*]], <i32 1, i32 1>
24 ; CHECK-NEXT: [[D:%.*]] = lshr <2 x i32> [[CONV]], [[TMP0]]
25 ; CHECK-NEXT: ret <2 x i32> [[D]]
28 %conv = zext <2 x i16> %x to <2 x i32>
29 %s = shl <2 x i32> <i32 2, i32 2>, %y
30 %d = sdiv <2 x i32> %conv, %s
35 define i64 @t2(i64 %x, i32 %y) {
37 ; CHECK-NEXT: [[TMP1:%.*]] = zext i32 [[Y:%.*]] to i64
38 ; CHECK-NEXT: [[TMP2:%.*]] = lshr i64 [[X:%.*]], [[TMP1]]
39 ; CHECK-NEXT: ret i64 [[TMP2]]
42 %2 = zext i32 %1 to i64
48 define i64 @t3(i64 %x, i32 %y) {
50 ; CHECK-NEXT: [[TMP1:%.*]] = add i32 [[Y:%.*]], 2
51 ; CHECK-NEXT: [[TMP2:%.*]] = zext i32 [[TMP1]] to i64
52 ; CHECK-NEXT: [[TMP3:%.*]] = lshr i64 [[X:%.*]], [[TMP2]]
53 ; CHECK-NEXT: ret i64 [[TMP3]]
56 %2 = zext i32 %1 to i64
61 define i32 @t4(i32 %x, i32 %y) {
63 ; CHECK-NEXT: [[TMP1:%.*]] = icmp ugt i32 [[Y:%.*]], 5
64 ; CHECK-NEXT: [[DOTV:%.*]] = select i1 [[TMP1]], i32 [[Y]], i32 5
65 ; CHECK-NEXT: [[TMP2:%.*]] = lshr i32 [[X:%.*]], [[DOTV]]
66 ; CHECK-NEXT: ret i32 [[TMP2]]
69 %2 = icmp ult i32 %1, 32
70 %3 = select i1 %2, i32 32, i32 %1
75 define i32 @t5(i1 %x, i1 %y, i32 %V) {
77 ; CHECK-NEXT: [[DOTV:%.*]] = select i1 [[X:%.*]], i32 5, i32 6
78 ; CHECK-NEXT: [[TMP1:%.*]] = lshr i32 [[V:%.*]], [[DOTV]]
79 ; CHECK-NEXT: [[TMP2:%.*]] = select i1 [[Y:%.*]], i32 [[TMP1]], i32 0
80 ; CHECK-NEXT: ret i32 [[TMP2]]
83 %2 = select i1 %x, i32 32, i32 64
84 %3 = select i1 %y, i32 %2, i32 %1
89 define i32 @t6(i32 %x, i32 %z) {
91 ; CHECK-NEXT: [[X_IS_ZERO:%.*]] = icmp eq i32 [[X:%.*]], 0
92 ; CHECK-NEXT: [[DIVISOR:%.*]] = select i1 [[X_IS_ZERO]], i32 1, i32 [[X]]
93 ; CHECK-NEXT: [[Y:%.*]] = udiv i32 [[Z:%.*]], [[DIVISOR]]
94 ; CHECK-NEXT: ret i32 [[Y]]
96 %x_is_zero = icmp eq i32 %x, 0
97 %divisor = select i1 %x_is_zero, i32 1, i32 %x
98 %y = udiv i32 %z, %divisor
102 ; (X << C1) / X -> 1 << C1 optimizations
104 define i32 @t7(i32 %x) {
106 ; CHECK-NEXT: ret i32 4
108 %shl = shl nsw i32 %x, 2
109 %r = sdiv i32 %shl, %x
113 ; make sure the previous opt doesn't take place for wrapped shifts
115 define i32 @t8(i32 %x) {
117 ; CHECK-NEXT: [[SHL:%.*]] = shl i32 [[X:%.*]], 2
118 ; CHECK-NEXT: [[R:%.*]] = sdiv i32 [[SHL]], [[X]]
119 ; CHECK-NEXT: ret i32 [[R]]
122 %r = sdiv i32 %shl, %x
126 define <2 x i32> @t9(<2 x i32> %x) {
128 ; CHECK-NEXT: ret <2 x i32> <i32 4, i32 8>
130 %shl = shl nsw <2 x i32> %x, <i32 2, i32 3>
131 %r = sdiv <2 x i32> %shl, %x
135 define i32 @t10(i32 %x, i32 %y) {
137 ; CHECK-NEXT: [[R:%.*]] = shl nsw i32 1, [[Y:%.*]]
138 ; CHECK-NEXT: ret i32 [[R]]
140 %shl = shl nsw i32 %x, %y
141 %r = sdiv i32 %shl, %x
145 define <2 x i32> @t11(<2 x i32> %x, <2 x i32> %y) {
147 ; CHECK-NEXT: [[R:%.*]] = shl nsw <2 x i32> <i32 1, i32 1>, [[Y:%.*]]
148 ; CHECK-NEXT: ret <2 x i32> [[R]]
150 %shl = shl nsw <2 x i32> %x, %y
151 %r = sdiv <2 x i32> %shl, %x
155 define i32 @t12(i32 %x) {
157 ; CHECK-NEXT: ret i32 4
159 %shl = shl nuw i32 %x, 2
160 %r = udiv i32 %shl, %x
164 ; make sure the previous opt doesn't take place for wrapped shifts
166 define i32 @t13(i32 %x) {
168 ; CHECK-NEXT: [[SHL:%.*]] = shl i32 [[X:%.*]], 2
169 ; CHECK-NEXT: [[R:%.*]] = udiv i32 [[SHL]], [[X]]
170 ; CHECK-NEXT: ret i32 [[R]]
173 %r = udiv i32 %shl, %x
177 define <2 x i32> @t14(<2 x i32> %x) {
179 ; CHECK-NEXT: ret <2 x i32> <i32 4, i32 8>
181 %shl = shl nuw <2 x i32> %x, <i32 2, i32 3>
182 %r = udiv <2 x i32> %shl, %x
186 define i32 @t15(i32 %x, i32 %y) {
188 ; CHECK-NEXT: [[R:%.*]] = shl nuw i32 1, [[Y:%.*]]
189 ; CHECK-NEXT: ret i32 [[R]]
191 %shl = shl nuw i32 %x, %y
192 %r = udiv i32 %shl, %x
196 define <2 x i32> @t16(<2 x i32> %x, <2 x i32> %y) {
198 ; CHECK-NEXT: [[R:%.*]] = shl nuw <2 x i32> <i32 1, i32 1>, [[Y:%.*]]
199 ; CHECK-NEXT: ret <2 x i32> [[R]]
201 %shl = shl nuw <2 x i32> %x, %y
202 %r = udiv <2 x i32> %shl, %x