1*f4a2713aSLionel Sambuc; RUN: opt < %s -instcombine -S | FileCheck %s
2*f4a2713aSLionel Sambuc
3*f4a2713aSLionel Sambuc; CHECK-LABEL: @sdiv1(
4*f4a2713aSLionel Sambuc; CHECK: sdiv i32 %x, 8
5*f4a2713aSLionel Sambucdefine i32 @sdiv1(i32 %x) {
6*f4a2713aSLionel Sambuc  %y = sdiv i32 %x, 8
7*f4a2713aSLionel Sambuc  ret i32 %y
8*f4a2713aSLionel Sambuc}
9*f4a2713aSLionel Sambuc
10*f4a2713aSLionel Sambuc; CHECK-LABEL: @sdiv2(
11*f4a2713aSLionel Sambuc; CHECK: ashr exact i32 %x, 3
12*f4a2713aSLionel Sambucdefine i32 @sdiv2(i32 %x) {
13*f4a2713aSLionel Sambuc  %y = sdiv exact i32 %x, 8
14*f4a2713aSLionel Sambuc  ret i32 %y
15*f4a2713aSLionel Sambuc}
16*f4a2713aSLionel Sambuc
17*f4a2713aSLionel Sambuc; CHECK-LABEL: @sdiv3(
18*f4a2713aSLionel Sambuc; CHECK: %y = srem i32 %x, 3
19*f4a2713aSLionel Sambuc; CHECK: %z = sub i32 %x, %y
20*f4a2713aSLionel Sambuc; CHECK: ret i32 %z
21*f4a2713aSLionel Sambucdefine i32 @sdiv3(i32 %x) {
22*f4a2713aSLionel Sambuc  %y = sdiv i32 %x, 3
23*f4a2713aSLionel Sambuc  %z = mul i32 %y, 3
24*f4a2713aSLionel Sambuc  ret i32 %z
25*f4a2713aSLionel Sambuc}
26*f4a2713aSLionel Sambuc
27*f4a2713aSLionel Sambuc; CHECK-LABEL: @sdiv4(
28*f4a2713aSLionel Sambuc; CHECK: ret i32 %x
29*f4a2713aSLionel Sambucdefine i32 @sdiv4(i32 %x) {
30*f4a2713aSLionel Sambuc  %y = sdiv exact i32 %x, 3
31*f4a2713aSLionel Sambuc  %z = mul i32 %y, 3
32*f4a2713aSLionel Sambuc  ret i32 %z
33*f4a2713aSLionel Sambuc}
34*f4a2713aSLionel Sambuc
35*f4a2713aSLionel Sambuc; CHECK: i32 @sdiv5
36*f4a2713aSLionel Sambuc; CHECK: %y = srem i32 %x, 3
37*f4a2713aSLionel Sambuc; CHECK: %z = sub i32 %y, %x
38*f4a2713aSLionel Sambuc; CHECK: ret i32 %z
39*f4a2713aSLionel Sambucdefine i32 @sdiv5(i32 %x) {
40*f4a2713aSLionel Sambuc  %y = sdiv i32 %x, 3
41*f4a2713aSLionel Sambuc  %z = mul i32 %y, -3
42*f4a2713aSLionel Sambuc  ret i32 %z
43*f4a2713aSLionel Sambuc}
44*f4a2713aSLionel Sambuc
45*f4a2713aSLionel Sambuc; CHECK-LABEL: @sdiv6(
46*f4a2713aSLionel Sambuc; CHECK: %z = sub i32 0, %x
47*f4a2713aSLionel Sambuc; CHECK: ret i32 %z
48*f4a2713aSLionel Sambucdefine i32 @sdiv6(i32 %x) {
49*f4a2713aSLionel Sambuc  %y = sdiv exact i32 %x, 3
50*f4a2713aSLionel Sambuc  %z = mul i32 %y, -3
51*f4a2713aSLionel Sambuc  ret i32 %z
52*f4a2713aSLionel Sambuc}
53*f4a2713aSLionel Sambuc
54*f4a2713aSLionel Sambuc; CHECK-LABEL: @udiv1(
55*f4a2713aSLionel Sambuc; CHECK: ret i32 %x
56*f4a2713aSLionel Sambucdefine i32 @udiv1(i32 %x, i32 %w) {
57*f4a2713aSLionel Sambuc  %y = udiv exact i32 %x, %w
58*f4a2713aSLionel Sambuc  %z = mul i32 %y, %w
59*f4a2713aSLionel Sambuc  ret i32 %z
60*f4a2713aSLionel Sambuc}
61*f4a2713aSLionel Sambuc
62*f4a2713aSLionel Sambuc; CHECK-LABEL: @udiv2(
63*f4a2713aSLionel Sambuc; CHECK: %z = lshr exact i32 %x, %w
64*f4a2713aSLionel Sambuc; CHECK: ret i32 %z
65*f4a2713aSLionel Sambucdefine i32 @udiv2(i32 %x, i32 %w) {
66*f4a2713aSLionel Sambuc  %y = shl i32 1, %w
67*f4a2713aSLionel Sambuc  %z = udiv exact i32 %x, %y
68*f4a2713aSLionel Sambuc  ret i32 %z
69*f4a2713aSLionel Sambuc}
70*f4a2713aSLionel Sambuc
71*f4a2713aSLionel Sambuc; CHECK-LABEL: @ashr1(
72*f4a2713aSLionel Sambuc; CHECK: %B = ashr exact i64 %A, 2
73*f4a2713aSLionel Sambuc; CHECK: ret i64 %B
74*f4a2713aSLionel Sambucdefine i64 @ashr1(i64 %X) nounwind {
75*f4a2713aSLionel Sambuc  %A = shl i64 %X, 8
76*f4a2713aSLionel Sambuc  %B = ashr i64 %A, 2   ; X/4
77*f4a2713aSLionel Sambuc  ret i64 %B
78*f4a2713aSLionel Sambuc}
79*f4a2713aSLionel Sambuc
80*f4a2713aSLionel Sambuc; PR9120
81*f4a2713aSLionel Sambuc; CHECK-LABEL: @ashr_icmp1(
82*f4a2713aSLionel Sambuc; CHECK: %B = icmp eq i64 %X, 0
83*f4a2713aSLionel Sambuc; CHECK: ret i1 %B
84*f4a2713aSLionel Sambucdefine i1 @ashr_icmp1(i64 %X) nounwind {
85*f4a2713aSLionel Sambuc  %A = ashr exact i64 %X, 2   ; X/4
86*f4a2713aSLionel Sambuc  %B = icmp eq i64 %A, 0
87*f4a2713aSLionel Sambuc  ret i1 %B
88*f4a2713aSLionel Sambuc}
89*f4a2713aSLionel Sambuc
90*f4a2713aSLionel Sambuc; CHECK-LABEL: @ashr_icmp2(
91*f4a2713aSLionel Sambuc; CHECK: %Z = icmp slt i64 %X, 16
92*f4a2713aSLionel Sambuc; CHECK: ret i1 %Z
93*f4a2713aSLionel Sambucdefine i1 @ashr_icmp2(i64 %X) nounwind {
94*f4a2713aSLionel Sambuc %Y = ashr exact i64 %X, 2  ; x / 4
95*f4a2713aSLionel Sambuc %Z = icmp slt i64 %Y, 4    ; x < 16
96*f4a2713aSLionel Sambuc ret i1 %Z
97*f4a2713aSLionel Sambuc}
98*f4a2713aSLionel Sambuc
99*f4a2713aSLionel Sambuc; PR9998
100*f4a2713aSLionel Sambuc; Make sure we don't transform the ashr here into an sdiv
101*f4a2713aSLionel Sambuc; CHECK-LABEL: @pr9998(
102*f4a2713aSLionel Sambuc; CHECK:      [[BIT:%[A-Za-z0-9.]+]] = and i32 %V, 1
103*f4a2713aSLionel Sambuc; CHECK-NEXT: [[CMP:%[A-Za-z0-9.]+]] = icmp ne i32 [[BIT]], 0
104*f4a2713aSLionel Sambuc; CHECK-NEXT: ret i1 [[CMP]]
105*f4a2713aSLionel Sambucdefine i1 @pr9998(i32 %V) nounwind {
106*f4a2713aSLionel Sambucentry:
107*f4a2713aSLionel Sambuc  %W = shl i32 %V, 31
108*f4a2713aSLionel Sambuc  %X = ashr exact i32 %W, 31
109*f4a2713aSLionel Sambuc  %Y = sext i32 %X to i64
110*f4a2713aSLionel Sambuc  %Z = icmp ugt i64 %Y, 7297771788697658747
111*f4a2713aSLionel Sambuc  ret i1 %Z
112*f4a2713aSLionel Sambuc}
113*f4a2713aSLionel Sambuc
114*f4a2713aSLionel Sambuc
115*f4a2713aSLionel Sambuc
116*f4a2713aSLionel Sambuc; CHECK-LABEL: @udiv_icmp1(
117*f4a2713aSLionel Sambuc; CHECK: icmp ne i64 %X, 0
118*f4a2713aSLionel Sambucdefine i1 @udiv_icmp1(i64 %X) nounwind {
119*f4a2713aSLionel Sambuc  %A = udiv exact i64 %X, 5   ; X/5
120*f4a2713aSLionel Sambuc  %B = icmp ne i64 %A, 0
121*f4a2713aSLionel Sambuc  ret i1 %B
122*f4a2713aSLionel Sambuc}
123*f4a2713aSLionel Sambuc
124*f4a2713aSLionel Sambuc; CHECK-LABEL: @sdiv_icmp1(
125*f4a2713aSLionel Sambuc; CHECK: icmp eq i64 %X, 0
126*f4a2713aSLionel Sambucdefine i1 @sdiv_icmp1(i64 %X) nounwind {
127*f4a2713aSLionel Sambuc  %A = sdiv exact i64 %X, 5   ; X/5 == 0 --> x == 0
128*f4a2713aSLionel Sambuc  %B = icmp eq i64 %A, 0
129*f4a2713aSLionel Sambuc  ret i1 %B
130*f4a2713aSLionel Sambuc}
131*f4a2713aSLionel Sambuc
132*f4a2713aSLionel Sambuc; CHECK-LABEL: @sdiv_icmp2(
133*f4a2713aSLionel Sambuc; CHECK: icmp eq i64 %X, 5
134*f4a2713aSLionel Sambucdefine i1 @sdiv_icmp2(i64 %X) nounwind {
135*f4a2713aSLionel Sambuc  %A = sdiv exact i64 %X, 5   ; X/5 == 1 --> x == 5
136*f4a2713aSLionel Sambuc  %B = icmp eq i64 %A, 1
137*f4a2713aSLionel Sambuc  ret i1 %B
138*f4a2713aSLionel Sambuc}
139*f4a2713aSLionel Sambuc
140*f4a2713aSLionel Sambuc; CHECK-LABEL: @sdiv_icmp3(
141*f4a2713aSLionel Sambuc; CHECK: icmp eq i64 %X, -5
142*f4a2713aSLionel Sambucdefine i1 @sdiv_icmp3(i64 %X) nounwind {
143*f4a2713aSLionel Sambuc  %A = sdiv exact i64 %X, 5   ; X/5 == -1 --> x == -5
144*f4a2713aSLionel Sambuc  %B = icmp eq i64 %A, -1
145*f4a2713aSLionel Sambuc  ret i1 %B
146*f4a2713aSLionel Sambuc}
147*f4a2713aSLionel Sambuc
148*f4a2713aSLionel Sambuc; CHECK-LABEL: @sdiv_icmp4(
149*f4a2713aSLionel Sambuc; CHECK: icmp eq i64 %X, 0
150*f4a2713aSLionel Sambucdefine i1 @sdiv_icmp4(i64 %X) nounwind {
151*f4a2713aSLionel Sambuc  %A = sdiv exact i64 %X, -5   ; X/-5 == 0 --> x == 0
152*f4a2713aSLionel Sambuc  %B = icmp eq i64 %A, 0
153*f4a2713aSLionel Sambuc  ret i1 %B
154*f4a2713aSLionel Sambuc}
155*f4a2713aSLionel Sambuc
156*f4a2713aSLionel Sambuc; CHECK-LABEL: @sdiv_icmp5(
157*f4a2713aSLionel Sambuc; CHECK: icmp eq i64 %X, -5
158*f4a2713aSLionel Sambucdefine i1 @sdiv_icmp5(i64 %X) nounwind {
159*f4a2713aSLionel Sambuc  %A = sdiv exact i64 %X, -5   ; X/-5 == 1 --> x == -5
160*f4a2713aSLionel Sambuc  %B = icmp eq i64 %A, 1
161*f4a2713aSLionel Sambuc  ret i1 %B
162*f4a2713aSLionel Sambuc}
163*f4a2713aSLionel Sambuc
164*f4a2713aSLionel Sambuc; CHECK-LABEL: @sdiv_icmp6(
165*f4a2713aSLionel Sambuc; CHECK: icmp eq i64 %X, 5
166*f4a2713aSLionel Sambucdefine i1 @sdiv_icmp6(i64 %X) nounwind {
167*f4a2713aSLionel Sambuc  %A = sdiv exact i64 %X, -5   ; X/-5 == 1 --> x == 5
168*f4a2713aSLionel Sambuc  %B = icmp eq i64 %A, -1
169*f4a2713aSLionel Sambuc  ret i1 %B
170*f4a2713aSLionel Sambuc}
171*f4a2713aSLionel Sambuc
172