Skip to content

Commit 5ab489d

Browse files
committed
[NFC][InstCombine] Add tests for '((X - Y) - Z)' pattern (PR49858)
1 parent 5efe9ef commit 5ab489d

File tree

1 file changed

+170
-0
lines changed

1 file changed

+170
-0
lines changed
Lines changed: 170 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
1+
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2+
; RUN: opt -instcombine %s -S -o - | FileCheck %s
3+
4+
; ((X - Y) - Z) --> X - (Y + Z) because we prefer add's.
5+
6+
declare void @use8(i8)
7+
8+
; Basic test
9+
define i8 @t0(i8 %x, i8 %y, i8 %z) {
10+
; CHECK-LABEL: @t0(
11+
; CHECK-NEXT: [[I0:%.*]] = sub i8 [[X:%.*]], [[Y:%.*]]
12+
; CHECK-NEXT: [[R:%.*]] = sub i8 [[I0]], [[Z:%.*]]
13+
; CHECK-NEXT: ret i8 [[R]]
14+
;
15+
%i0 = sub i8 %x, %y
16+
%r = sub i8 %i0, %z
17+
ret i8 %r
18+
}
19+
20+
; No flags are propagated
21+
define i8 @t1_flags(i8 %x, i8 %y, i8 %z) {
22+
; CHECK-LABEL: @t1_flags(
23+
; CHECK-NEXT: [[O0:%.*]] = sub nuw nsw i8 [[X:%.*]], [[Y:%.*]]
24+
; CHECK-NEXT: [[R:%.*]] = sub nuw nsw i8 [[O0]], [[Z:%.*]]
25+
; CHECK-NEXT: ret i8 [[R]]
26+
;
27+
%o0 = sub nuw nsw i8 %x, %y
28+
%r = sub nuw nsw i8 %o0, %z
29+
ret i8 %r
30+
}
31+
32+
; The inner sub must have single use.
33+
define i8 @n2(i8 %x, i8 %y, i8 %z) {
34+
; CHECK-LABEL: @n2(
35+
; CHECK-NEXT: [[I0:%.*]] = sub i8 [[X:%.*]], [[Y:%.*]]
36+
; CHECK-NEXT: call void @use8(i8 [[I0]])
37+
; CHECK-NEXT: [[R:%.*]] = sub i8 [[I0]], [[Z:%.*]]
38+
; CHECK-NEXT: ret i8 [[R]]
39+
;
40+
%i0 = sub i8 %x, %y ; extra use
41+
call void @use8(i8 %i0)
42+
%r = sub i8 %i0, %z
43+
ret i8 %r
44+
}
45+
46+
; What if some operand is constant?
47+
48+
define i8 @t3_c0(i8 %y, i8 %z) {
49+
; CHECK-LABEL: @t3_c0(
50+
; CHECK-NEXT: [[I0:%.*]] = sub i8 42, [[Y:%.*]]
51+
; CHECK-NEXT: [[R:%.*]] = sub i8 [[I0]], [[Z:%.*]]
52+
; CHECK-NEXT: ret i8 [[R]]
53+
;
54+
%i0 = sub i8 42, %y
55+
%r = sub i8 %i0, %z
56+
ret i8 %r
57+
}
58+
59+
define i8 @t4_c1(i8 %x, i8 %z) {
60+
; CHECK-LABEL: @t4_c1(
61+
; CHECK-NEXT: [[I0:%.*]] = add i8 [[X:%.*]], -42
62+
; CHECK-NEXT: [[R:%.*]] = sub i8 [[I0]], [[Z:%.*]]
63+
; CHECK-NEXT: ret i8 [[R]]
64+
;
65+
%i0 = sub i8 %x, 42
66+
%r = sub i8 %i0, %z
67+
ret i8 %r
68+
}
69+
70+
define i8 @t5_c2(i8 %x, i8 %y) {
71+
; CHECK-LABEL: @t5_c2(
72+
; CHECK-NEXT: [[I0:%.*]] = sub i8 [[X:%.*]], [[Y:%.*]]
73+
; CHECK-NEXT: [[R:%.*]] = add i8 [[I0]], -42
74+
; CHECK-NEXT: ret i8 [[R]]
75+
;
76+
%i0 = sub i8 %x, %y
77+
%r = sub i8 %i0, 42
78+
ret i8 %r
79+
}
80+
81+
; What if some operand is constant and there was extra use?
82+
83+
define i8 @t6_c0_extrause(i8 %y, i8 %z) {
84+
; CHECK-LABEL: @t6_c0_extrause(
85+
; CHECK-NEXT: [[I0:%.*]] = sub i8 42, [[Y:%.*]]
86+
; CHECK-NEXT: call void @use8(i8 [[I0]])
87+
; CHECK-NEXT: [[R:%.*]] = sub i8 [[I0]], [[Z:%.*]]
88+
; CHECK-NEXT: ret i8 [[R]]
89+
;
90+
%i0 = sub i8 42, %y
91+
call void @use8(i8 %i0)
92+
%r = sub i8 %i0, %z
93+
ret i8 %r
94+
}
95+
96+
define i8 @t7_c1_extrause(i8 %x, i8 %z) {
97+
; CHECK-LABEL: @t7_c1_extrause(
98+
; CHECK-NEXT: [[I0:%.*]] = add i8 [[X:%.*]], -42
99+
; CHECK-NEXT: call void @use8(i8 [[I0]])
100+
; CHECK-NEXT: [[R:%.*]] = sub i8 [[I0]], [[Z:%.*]]
101+
; CHECK-NEXT: ret i8 [[R]]
102+
;
103+
%i0 = sub i8 %x, 42
104+
call void @use8(i8 %i0)
105+
%r = sub i8 %i0, %z
106+
ret i8 %r
107+
}
108+
109+
define i8 @t8_c2_extrause(i8 %x, i8 %y) {
110+
; CHECK-LABEL: @t8_c2_extrause(
111+
; CHECK-NEXT: [[I0:%.*]] = sub i8 [[X:%.*]], [[Y:%.*]]
112+
; CHECK-NEXT: call void @use8(i8 [[I0]])
113+
; CHECK-NEXT: [[R:%.*]] = add i8 [[I0]], -42
114+
; CHECK-NEXT: ret i8 [[R]]
115+
;
116+
%i0 = sub i8 %x, %y
117+
call void @use8(i8 %i0)
118+
%r = sub i8 %i0, 42
119+
ret i8 %r
120+
}
121+
122+
; What if two operands are constants?
123+
124+
define i8 @t9_c0_c2(i8 %y, i8 %z) {
125+
; CHECK-LABEL: @t9_c0_c2(
126+
; CHECK-NEXT: [[R:%.*]] = sub i8 18, [[Y:%.*]]
127+
; CHECK-NEXT: ret i8 [[R]]
128+
;
129+
%i0 = sub i8 42, %y
130+
%r = sub i8 %i0, 24
131+
ret i8 %r
132+
}
133+
134+
define i8 @t10_c1_c2(i8 %x, i8 %z) {
135+
; CHECK-LABEL: @t10_c1_c2(
136+
; CHECK-NEXT: [[R:%.*]] = add i8 [[X:%.*]], -66
137+
; CHECK-NEXT: ret i8 [[R]]
138+
;
139+
%i0 = sub i8 %x, 42
140+
%r = sub i8 %i0, 24
141+
ret i8 %r
142+
}
143+
144+
; What if two operands are constants and there was extra use?
145+
146+
define i8 @t11_c0_c2_extrause(i8 %y, i8 %z) {
147+
; CHECK-LABEL: @t11_c0_c2_extrause(
148+
; CHECK-NEXT: [[I0:%.*]] = sub i8 42, [[Y:%.*]]
149+
; CHECK-NEXT: call void @use8(i8 [[I0]])
150+
; CHECK-NEXT: [[R:%.*]] = sub i8 18, [[Y]]
151+
; CHECK-NEXT: ret i8 [[R]]
152+
;
153+
%i0 = sub i8 42, %y
154+
call void @use8(i8 %i0)
155+
%r = sub i8 %i0, 24
156+
ret i8 %r
157+
}
158+
159+
define i8 @t12_c1_c2_exrause(i8 %x, i8 %z) {
160+
; CHECK-LABEL: @t12_c1_c2_exrause(
161+
; CHECK-NEXT: [[I0:%.*]] = add i8 [[X:%.*]], -42
162+
; CHECK-NEXT: call void @use8(i8 [[I0]])
163+
; CHECK-NEXT: [[R:%.*]] = add i8 [[X]], -66
164+
; CHECK-NEXT: ret i8 [[R]]
165+
;
166+
%i0 = sub i8 %x, 42
167+
call void @use8(i8 %i0)
168+
%r = sub i8 %i0, 24
169+
ret i8 %r
170+
}

0 commit comments

Comments
 (0)