1 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2 ; RUN: opt -instcombine %s -S -o - | FileCheck %s
4 ; ((X - Y) - Z) --> X - (Y + Z) because we prefer add's.
9 define i8 @t0(i8 %x, i8 %y, i8 %z) {
11 ; CHECK-NEXT: [[TMP1:%.*]] = add i8 [[Y:%.*]], [[Z:%.*]]
12 ; CHECK-NEXT: [[R:%.*]] = sub i8 [[X:%.*]], [[TMP1]]
13 ; CHECK-NEXT: ret i8 [[R]]
20 ; No flags are propagated
21 define i8 @t1_flags(i8 %x, i8 %y, i8 %z) {
22 ; CHECK-LABEL: @t1_flags(
23 ; CHECK-NEXT: [[TMP1:%.*]] = add i8 [[Y:%.*]], [[Z:%.*]]
24 ; CHECK-NEXT: [[R:%.*]] = sub i8 [[X:%.*]], [[TMP1]]
25 ; CHECK-NEXT: ret i8 [[R]]
27 %o0 = sub nuw nsw i8 %x, %y
28 %r = sub nuw nsw i8 %o0, %z
32 ; The inner sub must have single use.
33 define i8 @n2(i8 %x, i8 %y, i8 %z) {
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]]
40 %i0 = sub i8 %x, %y ; extra use
41 call void @use8(i8 %i0)
46 ; What if some operand is constant?
48 define i8 @t3_c0(i8 %y, i8 %z) {
49 ; CHECK-LABEL: @t3_c0(
50 ; CHECK-NEXT: [[TMP1:%.*]] = add i8 [[Y:%.*]], [[Z:%.*]]
51 ; CHECK-NEXT: [[R:%.*]] = sub i8 42, [[TMP1]]
52 ; CHECK-NEXT: ret i8 [[R]]
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]]
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]]
81 ; What if some operand is constant and there was extra use?
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]]
91 call void @use8(i8 %i0)
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]]
104 call void @use8(i8 %i0)
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]]
117 call void @use8(i8 %i0)
122 ; What if two operands are constants?
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]]
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]]
144 ; What if two operands are constants and there was extra use?
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]]
154 call void @use8(i8 %i0)
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]]
167 call void @use8(i8 %i0)
173 @g0 = external global i8, align 1
174 @g1 = external global i8, align 1
175 define i32 @constantexpr0(i32 %x, i8* %y) unnamed_addr {
176 ; CHECK-LABEL: @constantexpr0(
177 ; CHECK-NEXT: [[I0:%.*]] = add i32 [[X:%.*]], ptrtoint (i8* @g0 to i32)
178 ; CHECK-NEXT: [[R:%.*]] = sub i32 0, [[I0]]
179 ; CHECK-NEXT: ret i32 [[R]]
181 %i0 = add i32 %x, ptrtoint (i8* @g0 to i32)
185 define i32 @constantexpr1(i32 %x, i8* %y) unnamed_addr {
186 ; CHECK-LABEL: @constantexpr1(
187 ; CHECK-NEXT: [[I0:%.*]] = add i32 [[X:%.*]], 42
188 ; CHECK-NEXT: [[R:%.*]] = sub i32 ptrtoint (i8* @g1 to i32), [[I0]]
189 ; CHECK-NEXT: ret i32 [[R]]
192 %r = sub i32 ptrtoint (i8* @g1 to i32), %i0
195 define i32 @constantexpr2(i32 %x, i8* %y) unnamed_addr {
196 ; CHECK-LABEL: @constantexpr2(
197 ; CHECK-NEXT: [[I0:%.*]] = add i32 [[X:%.*]], ptrtoint (i8* @g0 to i32)
198 ; CHECK-NEXT: [[R:%.*]] = sub i32 ptrtoint (i8* @g1 to i32), [[I0]]
199 ; CHECK-NEXT: ret i32 [[R]]
201 %i0 = add i32 %x, ptrtoint (i8* @g0 to i32)
202 %r = sub i32 ptrtoint (i8* @g1 to i32), %i0
206 define i64 @pr49870(i64 %x) {
207 ; CHECK-LABEL: @pr49870(
208 ; CHECK-NEXT: [[I0:%.*]] = xor i64 [[X:%.*]], -1
209 ; CHECK-NEXT: [[R:%.*]] = add i64 [[I0]], ptrtoint (i8* @g0 to i64)
210 ; CHECK-NEXT: ret i64 [[R]]
213 %r = add i64 %i0, ptrtoint (i8* @g0 to i64)