]>
Commit | Line | Data |
---|---|---|
3d2cf79f RB |
1 | /* Match-and-simplify patterns for shared GENERIC and GIMPLE folding. |
2 | This file is consumed by genmatch which produces gimple-match.c | |
3 | and generic-match.c from it. | |
4 | ||
5624e564 | 5 | Copyright (C) 2014-2015 Free Software Foundation, Inc. |
3d2cf79f RB |
6 | Contributed by Richard Biener <rguenther@suse.de> |
7 | and Prathamesh Kulkarni <bilbotheelffriend@gmail.com> | |
8 | ||
9 | This file is part of GCC. | |
10 | ||
11 | GCC is free software; you can redistribute it and/or modify it under | |
12 | the terms of the GNU General Public License as published by the Free | |
13 | Software Foundation; either version 3, or (at your option) any later | |
14 | version. | |
15 | ||
16 | GCC is distributed in the hope that it will be useful, but WITHOUT ANY | |
17 | WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
18 | FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
19 | for more details. | |
20 | ||
21 | You should have received a copy of the GNU General Public License | |
22 | along with GCC; see the file COPYING3. If not see | |
23 | <http://www.gnu.org/licenses/>. */ | |
24 | ||
25 | ||
26 | /* Generic tree predicates we inherit. */ | |
27 | (define_predicates | |
cc7b5acf | 28 | integer_onep integer_zerop integer_all_onesp integer_minus_onep |
09240451 | 29 | integer_each_onep integer_truep |
cc7b5acf | 30 | real_zerop real_onep real_minus_onep |
f3582e54 RB |
31 | CONSTANT_CLASS_P |
32 | tree_expr_nonnegative_p) | |
e0ee10ed | 33 | |
f84e7fd6 RB |
34 | /* Operator lists. */ |
35 | (define_operator_list tcc_comparison | |
36 | lt le eq ne ge gt unordered ordered unlt unle ungt unge uneq ltgt) | |
37 | (define_operator_list inverted_tcc_comparison | |
38 | ge gt ne eq lt le ordered unordered ge gt le lt ltgt uneq) | |
39 | (define_operator_list inverted_tcc_comparison_with_nans | |
40 | unge ungt ne eq unlt unle ordered unordered ge gt le lt ltgt uneq) | |
534bd33b MG |
41 | (define_operator_list swapped_tcc_comparison |
42 | gt ge eq ne le lt unordered ordered ungt unge unlt unle uneq ltgt) | |
07cdc2b8 RB |
43 | (define_operator_list simple_comparison lt le eq ne ge gt) |
44 | (define_operator_list swapped_simple_comparison gt ge eq ne le lt) | |
45 | ||
46 | (define_operator_list LOG BUILT_IN_LOGF BUILT_IN_LOG BUILT_IN_LOGL) | |
47 | (define_operator_list EXP BUILT_IN_EXPF BUILT_IN_EXP BUILT_IN_EXPL) | |
48 | (define_operator_list LOG2 BUILT_IN_LOG2F BUILT_IN_LOG2 BUILT_IN_LOG2L) | |
49 | (define_operator_list EXP2 BUILT_IN_EXP2F BUILT_IN_EXP2 BUILT_IN_EXP2L) | |
50 | (define_operator_list LOG10 BUILT_IN_LOG10F BUILT_IN_LOG10 BUILT_IN_LOG10L) | |
51 | (define_operator_list EXP10 BUILT_IN_EXP10F BUILT_IN_EXP10 BUILT_IN_EXP10L) | |
52 | (define_operator_list POW BUILT_IN_POWF BUILT_IN_POW BUILT_IN_POWL) | |
53 | (define_operator_list POW10 BUILT_IN_POW10F BUILT_IN_POW10 BUILT_IN_POW10L) | |
54 | (define_operator_list SQRT BUILT_IN_SQRTF BUILT_IN_SQRT BUILT_IN_SQRTL) | |
55 | (define_operator_list CBRT BUILT_IN_CBRTF BUILT_IN_CBRT BUILT_IN_CBRTL) | |
f84e7fd6 | 56 | |
e0ee10ed RB |
57 | |
58 | /* Simplifications of operations with one constant operand and | |
36a60e48 | 59 | simplifications to constants or single values. */ |
e0ee10ed RB |
60 | |
61 | (for op (plus pointer_plus minus bit_ior bit_xor) | |
62 | (simplify | |
63 | (op @0 integer_zerop) | |
64 | (non_lvalue @0))) | |
65 | ||
a499aac5 RB |
66 | /* 0 +p index -> (type)index */ |
67 | (simplify | |
68 | (pointer_plus integer_zerop @1) | |
69 | (non_lvalue (convert @1))) | |
70 | ||
a7f24614 RB |
71 | /* See if ARG1 is zero and X + ARG1 reduces to X. |
72 | Likewise if the operands are reversed. */ | |
73 | (simplify | |
74 | (plus:c @0 real_zerop@1) | |
75 | (if (fold_real_zero_addition_p (type, @1, 0)) | |
76 | (non_lvalue @0))) | |
77 | ||
78 | /* See if ARG1 is zero and X - ARG1 reduces to X. */ | |
79 | (simplify | |
80 | (minus @0 real_zerop@1) | |
81 | (if (fold_real_zero_addition_p (type, @1, 1)) | |
82 | (non_lvalue @0))) | |
83 | ||
e0ee10ed RB |
84 | /* Simplify x - x. |
85 | This is unsafe for certain floats even in non-IEEE formats. | |
86 | In IEEE, it is unsafe because it does wrong for NaNs. | |
87 | Also note that operand_equal_p is always false if an operand | |
88 | is volatile. */ | |
89 | (simplify | |
a7f24614 | 90 | (minus @0 @0) |
1b457aa4 | 91 | (if (!FLOAT_TYPE_P (type) || !HONOR_NANS (type)) |
a7f24614 | 92 | { build_zero_cst (type); })) |
e0ee10ed RB |
93 | |
94 | (simplify | |
a7f24614 RB |
95 | (mult @0 integer_zerop@1) |
96 | @1) | |
97 | ||
98 | /* Maybe fold x * 0 to 0. The expressions aren't the same | |
99 | when x is NaN, since x * 0 is also NaN. Nor are they the | |
100 | same in modes with signed zeros, since multiplying a | |
101 | negative value by 0 gives -0, not +0. */ | |
102 | (simplify | |
103 | (mult @0 real_zerop@1) | |
1b457aa4 | 104 | (if (!HONOR_NANS (type) && !HONOR_SIGNED_ZEROS (element_mode (type))) |
a7f24614 RB |
105 | @1)) |
106 | ||
107 | /* In IEEE floating point, x*1 is not equivalent to x for snans. | |
108 | Likewise for complex arithmetic with signed zeros. */ | |
109 | (simplify | |
110 | (mult @0 real_onep) | |
09240451 MG |
111 | (if (!HONOR_SNANS (element_mode (type)) |
112 | && (!HONOR_SIGNED_ZEROS (element_mode (type)) | |
a7f24614 RB |
113 | || !COMPLEX_FLOAT_TYPE_P (type))) |
114 | (non_lvalue @0))) | |
115 | ||
116 | /* Transform x * -1.0 into -x. */ | |
117 | (simplify | |
118 | (mult @0 real_minus_onep) | |
09240451 MG |
119 | (if (!HONOR_SNANS (element_mode (type)) |
120 | && (!HONOR_SIGNED_ZEROS (element_mode (type)) | |
a7f24614 RB |
121 | || !COMPLEX_FLOAT_TYPE_P (type))) |
122 | (negate @0))) | |
e0ee10ed RB |
123 | |
124 | /* Make sure to preserve divisions by zero. This is the reason why | |
125 | we don't simplify x / x to 1 or 0 / x to 0. */ | |
126 | (for op (mult trunc_div ceil_div floor_div round_div exact_div) | |
127 | (simplify | |
128 | (op @0 integer_onep) | |
129 | (non_lvalue @0))) | |
130 | ||
a7f24614 RB |
131 | /* X / -1 is -X. */ |
132 | (for div (trunc_div ceil_div floor_div round_div exact_div) | |
133 | (simplify | |
09240451 MG |
134 | (div @0 integer_minus_onep@1) |
135 | (if (!TYPE_UNSIGNED (type)) | |
a7f24614 RB |
136 | (negate @0)))) |
137 | ||
138 | /* For unsigned integral types, FLOOR_DIV_EXPR is the same as | |
139 | TRUNC_DIV_EXPR. Rewrite into the latter in this case. */ | |
140 | (simplify | |
141 | (floor_div @0 @1) | |
09240451 MG |
142 | (if ((INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type)) |
143 | && TYPE_UNSIGNED (type)) | |
a7f24614 RB |
144 | (trunc_div @0 @1))) |
145 | ||
28093105 RB |
146 | /* Combine two successive divisions. Note that combining ceil_div |
147 | and floor_div is trickier and combining round_div even more so. */ | |
148 | (for div (trunc_div exact_div) | |
c306cfaf RB |
149 | (simplify |
150 | (div (div @0 INTEGER_CST@1) INTEGER_CST@2) | |
151 | (with { | |
152 | bool overflow_p; | |
153 | wide_int mul = wi::mul (@1, @2, TYPE_SIGN (type), &overflow_p); | |
154 | } | |
155 | (if (!overflow_p) | |
156 | (div @0 { wide_int_to_tree (type, mul); })) | |
ac19a303 RB |
157 | (if (overflow_p |
158 | && (TYPE_UNSIGNED (type) | |
159 | || mul != wi::min_value (TYPE_PRECISION (type), SIGNED))) | |
c306cfaf RB |
160 | { build_zero_cst (type); })))) |
161 | ||
a7f24614 | 162 | /* Optimize A / A to 1.0 if we don't care about |
09240451 | 163 | NaNs or Infinities. */ |
a7f24614 RB |
164 | (simplify |
165 | (rdiv @0 @0) | |
09240451 | 166 | (if (FLOAT_TYPE_P (type) |
1b457aa4 | 167 | && ! HONOR_NANS (type) |
09240451 MG |
168 | && ! HONOR_INFINITIES (element_mode (type))) |
169 | { build_one_cst (type); })) | |
170 | ||
171 | /* Optimize -A / A to -1.0 if we don't care about | |
172 | NaNs or Infinities. */ | |
173 | (simplify | |
174 | (rdiv:c @0 (negate @0)) | |
175 | (if (FLOAT_TYPE_P (type) | |
1b457aa4 | 176 | && ! HONOR_NANS (type) |
09240451 MG |
177 | && ! HONOR_INFINITIES (element_mode (type))) |
178 | { build_minus_one_cst (type); })) | |
a7f24614 RB |
179 | |
180 | /* In IEEE floating point, x/1 is not equivalent to x for snans. */ | |
181 | (simplify | |
182 | (rdiv @0 real_onep) | |
09240451 | 183 | (if (!HONOR_SNANS (element_mode (type))) |
a7f24614 RB |
184 | (non_lvalue @0))) |
185 | ||
186 | /* In IEEE floating point, x/-1 is not equivalent to -x for snans. */ | |
187 | (simplify | |
188 | (rdiv @0 real_minus_onep) | |
09240451 | 189 | (if (!HONOR_SNANS (element_mode (type))) |
a7f24614 RB |
190 | (negate @0))) |
191 | ||
192 | /* If ARG1 is a constant, we can convert this to a multiply by the | |
193 | reciprocal. This does not have the same rounding properties, | |
194 | so only do this if -freciprocal-math. We can actually | |
195 | always safely do it if ARG1 is a power of two, but it's hard to | |
196 | tell if it is or not in a portable manner. */ | |
197 | (for cst (REAL_CST COMPLEX_CST VECTOR_CST) | |
198 | (simplify | |
199 | (rdiv @0 cst@1) | |
200 | (if (optimize) | |
53bc4b3a RB |
201 | (if (flag_reciprocal_math |
202 | && !real_zerop (@1)) | |
a7f24614 | 203 | (with |
249700b5 | 204 | { tree tem = const_binop (RDIV_EXPR, type, build_one_cst (type), @1); } |
a7f24614 RB |
205 | (if (tem) |
206 | (mult @0 { tem; } )))) | |
207 | (if (cst != COMPLEX_CST) | |
208 | (with { tree inverse = exact_inverse (type, @1); } | |
209 | (if (inverse) | |
210 | (mult @0 { inverse; } ))))))) | |
211 | ||
e0ee10ed RB |
212 | /* Same applies to modulo operations, but fold is inconsistent here |
213 | and simplifies 0 % x to 0, only preserving literal 0 % 0. */ | |
a7f24614 | 214 | (for mod (ceil_mod floor_mod round_mod trunc_mod) |
e0ee10ed RB |
215 | /* 0 % X is always zero. */ |
216 | (simplify | |
a7f24614 | 217 | (mod integer_zerop@0 @1) |
e0ee10ed RB |
218 | /* But not for 0 % 0 so that we can get the proper warnings and errors. */ |
219 | (if (!integer_zerop (@1)) | |
220 | @0)) | |
221 | /* X % 1 is always zero. */ | |
222 | (simplify | |
a7f24614 RB |
223 | (mod @0 integer_onep) |
224 | { build_zero_cst (type); }) | |
225 | /* X % -1 is zero. */ | |
226 | (simplify | |
09240451 MG |
227 | (mod @0 integer_minus_onep@1) |
228 | (if (!TYPE_UNSIGNED (type)) | |
bc4315fb MG |
229 | { build_zero_cst (type); })) |
230 | /* (X % Y) % Y is just X % Y. */ | |
231 | (simplify | |
232 | (mod (mod@2 @0 @1) @1) | |
233 | @2)) | |
a7f24614 RB |
234 | |
235 | /* X % -C is the same as X % C. */ | |
236 | (simplify | |
237 | (trunc_mod @0 INTEGER_CST@1) | |
238 | (if (TYPE_SIGN (type) == SIGNED | |
239 | && !TREE_OVERFLOW (@1) | |
240 | && wi::neg_p (@1) | |
241 | && !TYPE_OVERFLOW_TRAPS (type) | |
242 | /* Avoid this transformation if C is INT_MIN, i.e. C == -C. */ | |
243 | && !sign_bit_p (@1, @1)) | |
244 | (trunc_mod @0 (negate @1)))) | |
e0ee10ed | 245 | |
8f0c696a RB |
246 | /* X % -Y is the same as X % Y. */ |
247 | (simplify | |
248 | (trunc_mod @0 (convert? (negate @1))) | |
249 | (if (!TYPE_UNSIGNED (type) | |
250 | && !TYPE_OVERFLOW_TRAPS (type) | |
251 | && tree_nop_conversion_p (type, TREE_TYPE (@1))) | |
252 | (trunc_mod @0 (convert @1)))) | |
253 | ||
f461569a MP |
254 | /* X - (X / Y) * Y is the same as X % Y. */ |
255 | (simplify | |
d3bc1d1b | 256 | (minus (convert1? @0) (convert2? (mult (trunc_div @0 @1) @1))) |
f461569a | 257 | (if (INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type)) |
d3bc1d1b | 258 | (trunc_mod (convert @0) (convert @1)))) |
f461569a | 259 | |
8f0c696a RB |
260 | /* Optimize TRUNC_MOD_EXPR by a power of two into a BIT_AND_EXPR, |
261 | i.e. "X % C" into "X & (C - 1)", if X and C are positive. | |
262 | Also optimize A % (C << N) where C is a power of 2, | |
263 | to A & ((C << N) - 1). */ | |
264 | (match (power_of_two_cand @1) | |
265 | INTEGER_CST@1) | |
266 | (match (power_of_two_cand @1) | |
267 | (lshift INTEGER_CST@1 @2)) | |
268 | (for mod (trunc_mod floor_mod) | |
269 | (simplify | |
4ab1e111 | 270 | (mod @0 (convert?@3 (power_of_two_cand@1 @2))) |
8f0c696a RB |
271 | (if ((TYPE_UNSIGNED (type) |
272 | || tree_expr_nonnegative_p (@0)) | |
4ab1e111 | 273 | && tree_nop_conversion_p (type, TREE_TYPE (@3)) |
8f0c696a | 274 | && integer_pow2p (@2) && tree_int_cst_sgn (@2) > 0) |
4ab1e111 | 275 | (bit_and @0 (convert (minus @1 { build_int_cst (TREE_TYPE (@1), 1); })))))) |
8f0c696a | 276 | |
bc4315fb MG |
277 | /* X % Y is smaller than Y. */ |
278 | (for cmp (lt ge) | |
279 | (simplify | |
280 | (cmp (trunc_mod @0 @1) @1) | |
281 | (if (TYPE_UNSIGNED (TREE_TYPE (@0))) | |
282 | { constant_boolean_node (cmp == LT_EXPR, type); }))) | |
283 | (for cmp (gt le) | |
284 | (simplify | |
285 | (cmp @1 (trunc_mod @0 @1)) | |
286 | (if (TYPE_UNSIGNED (TREE_TYPE (@0))) | |
287 | { constant_boolean_node (cmp == GT_EXPR, type); }))) | |
288 | ||
e0ee10ed RB |
289 | /* x | ~0 -> ~0 */ |
290 | (simplify | |
291 | (bit_ior @0 integer_all_onesp@1) | |
292 | @1) | |
293 | ||
294 | /* x & 0 -> 0 */ | |
295 | (simplify | |
296 | (bit_and @0 integer_zerop@1) | |
297 | @1) | |
298 | ||
a4398a30 MP |
299 | /* ~x | x -> -1 */ |
300 | (simplify | |
301 | (bit_ior:c (convert? @0) (convert? (bit_not @0))) | |
3db55b2b | 302 | (convert { build_all_ones_cst (TREE_TYPE (@0)); })) |
a4398a30 | 303 | |
e0ee10ed RB |
304 | /* x ^ x -> 0 */ |
305 | (simplify | |
306 | (bit_xor @0 @0) | |
307 | { build_zero_cst (type); }) | |
308 | ||
36a60e48 RB |
309 | /* Canonicalize X ^ ~0 to ~X. */ |
310 | (simplify | |
311 | (bit_xor @0 integer_all_onesp@1) | |
312 | (bit_not @0)) | |
313 | ||
97e77391 RB |
314 | /* ~X ^ X is -1. */ |
315 | (simplify | |
316 | (bit_xor:c (bit_not @0) @0) | |
317 | { build_all_ones_cst (type); }) | |
318 | ||
36a60e48 RB |
319 | /* x & ~0 -> x */ |
320 | (simplify | |
321 | (bit_and @0 integer_all_onesp) | |
322 | (non_lvalue @0)) | |
323 | ||
324 | /* x & x -> x, x | x -> x */ | |
325 | (for bitop (bit_and bit_ior) | |
326 | (simplify | |
327 | (bitop @0 @0) | |
328 | (non_lvalue @0))) | |
329 | ||
0f770b01 RV |
330 | /* x + (x & 1) -> (x + 1) & ~1 */ |
331 | (simplify | |
332 | (plus:c @0 (bit_and@2 @0 integer_onep@1)) | |
6e28e516 | 333 | (if (single_use (@2)) |
0f770b01 RV |
334 | (bit_and (plus @0 @1) (bit_not @1)))) |
335 | ||
336 | /* x & ~(x & y) -> x & ~y */ | |
337 | /* x | ~(x | y) -> x | ~y */ | |
338 | (for bitop (bit_and bit_ior) | |
af563d4b MG |
339 | (simplify |
340 | (bitop:c @0 (bit_not (bitop:c@2 @0 @1))) | |
6e28e516 | 341 | (if (single_use (@2)) |
af563d4b MG |
342 | (bitop @0 (bit_not @1))))) |
343 | ||
344 | /* (x | y) & ~x -> y & ~x */ | |
345 | /* (x & y) | ~x -> y | ~x */ | |
346 | (for bitop (bit_and bit_ior) | |
347 | rbitop (bit_ior bit_and) | |
348 | (simplify | |
349 | (bitop:c (rbitop:c @0 @1) (bit_not@2 @0)) | |
350 | (bitop @1 @2))) | |
0f770b01 | 351 | |
f13c4673 MP |
352 | /* (x & y) ^ (x | y) -> x ^ y */ |
353 | (simplify | |
2d6f2dce MP |
354 | (bit_xor:c (bit_and @0 @1) (bit_ior @0 @1)) |
355 | (bit_xor @0 @1)) | |
f13c4673 | 356 | |
9ea65ca6 MP |
357 | /* (x ^ y) ^ (x | y) -> x & y */ |
358 | (simplify | |
359 | (bit_xor:c (bit_xor @0 @1) (bit_ior @0 @1)) | |
360 | (bit_and @0 @1)) | |
361 | ||
362 | /* (x & y) + (x ^ y) -> x | y */ | |
363 | /* (x & y) | (x ^ y) -> x | y */ | |
364 | /* (x & y) ^ (x ^ y) -> x | y */ | |
365 | (for op (plus bit_ior bit_xor) | |
366 | (simplify | |
367 | (op:c (bit_and @0 @1) (bit_xor @0 @1)) | |
368 | (bit_ior @0 @1))) | |
369 | ||
370 | /* (x & y) + (x | y) -> x + y */ | |
371 | (simplify | |
372 | (plus:c (bit_and @0 @1) (bit_ior @0 @1)) | |
373 | (plus @0 @1)) | |
374 | ||
9737efaf MP |
375 | /* (x + y) - (x | y) -> x & y */ |
376 | (simplify | |
377 | (minus (plus @0 @1) (bit_ior @0 @1)) | |
378 | (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type) | |
379 | && !TYPE_SATURATING (type)) | |
380 | (bit_and @0 @1))) | |
381 | ||
382 | /* (x + y) - (x & y) -> x | y */ | |
383 | (simplify | |
384 | (minus (plus @0 @1) (bit_and @0 @1)) | |
385 | (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type) | |
386 | && !TYPE_SATURATING (type)) | |
387 | (bit_ior @0 @1))) | |
388 | ||
9ea65ca6 MP |
389 | /* (x | y) - (x ^ y) -> x & y */ |
390 | (simplify | |
391 | (minus (bit_ior @0 @1) (bit_xor @0 @1)) | |
392 | (bit_and @0 @1)) | |
393 | ||
394 | /* (x | y) - (x & y) -> x ^ y */ | |
395 | (simplify | |
396 | (minus (bit_ior @0 @1) (bit_and @0 @1)) | |
397 | (bit_xor @0 @1)) | |
398 | ||
66cc6273 MP |
399 | /* (x | y) & ~(x & y) -> x ^ y */ |
400 | (simplify | |
401 | (bit_and:c (bit_ior @0 @1) (bit_not (bit_and @0 @1))) | |
402 | (bit_xor @0 @1)) | |
403 | ||
404 | /* (x | y) & (~x ^ y) -> x & y */ | |
405 | (simplify | |
406 | (bit_and:c (bit_ior:c @0 @1) (bit_xor:c @1 (bit_not @0))) | |
407 | (bit_and @0 @1)) | |
408 | ||
5b00d921 RB |
409 | /* ~x & ~y -> ~(x | y) |
410 | ~x | ~y -> ~(x & y) */ | |
411 | (for op (bit_and bit_ior) | |
412 | rop (bit_ior bit_and) | |
413 | (simplify | |
414 | (op (convert1? (bit_not @0)) (convert2? (bit_not @1))) | |
415 | (if (tree_nop_conversion_p (type, TREE_TYPE (@0)) | |
416 | && tree_nop_conversion_p (type, TREE_TYPE (@1))) | |
417 | (bit_not (rop (convert @0) (convert @1)))))) | |
418 | ||
419 | /* If we are XORing two BIT_AND_EXPR's, both of which are and'ing | |
420 | with a constant, and the two constants have no bits in common, | |
421 | we should treat this as a BIT_IOR_EXPR since this may produce more | |
422 | simplifications. */ | |
423 | (simplify | |
424 | (bit_xor (convert1? (bit_and@4 @0 INTEGER_CST@1)) | |
425 | (convert2? (bit_and@5 @2 INTEGER_CST@3))) | |
426 | (if (tree_nop_conversion_p (type, TREE_TYPE (@0)) | |
427 | && tree_nop_conversion_p (type, TREE_TYPE (@2)) | |
428 | && wi::bit_and (@1, @3) == 0) | |
429 | (bit_ior (convert @4) (convert @5)))) | |
430 | ||
431 | /* (X | Y) ^ X -> Y & ~ X*/ | |
432 | (simplify | |
433 | (bit_xor:c (convert? (bit_ior:c @0 @1)) (convert? @0)) | |
434 | (if (tree_nop_conversion_p (type, TREE_TYPE (@0))) | |
435 | (convert (bit_and @1 (bit_not @0))))) | |
436 | ||
437 | /* Convert ~X ^ ~Y to X ^ Y. */ | |
438 | (simplify | |
439 | (bit_xor (convert1? (bit_not @0)) (convert2? (bit_not @1))) | |
440 | (if (tree_nop_conversion_p (type, TREE_TYPE (@0)) | |
441 | && tree_nop_conversion_p (type, TREE_TYPE (@1))) | |
442 | (bit_xor (convert @0) (convert @1)))) | |
443 | ||
444 | /* Convert ~X ^ C to X ^ ~C. */ | |
445 | (simplify | |
446 | (bit_xor (convert? (bit_not @0)) INTEGER_CST@1) | |
c8ba6498 EB |
447 | (if (tree_nop_conversion_p (type, TREE_TYPE (@0))) |
448 | (bit_xor (convert @0) (bit_not @1)))) | |
5b00d921 | 449 | |
97e77391 RB |
450 | /* Fold (X & Y) ^ Y as ~X & Y. */ |
451 | (simplify | |
452 | (bit_xor:c (bit_and:c @0 @1) @1) | |
453 | (bit_and (bit_not @0) @1)) | |
454 | ||
5b00d921 | 455 | |
b14a9c57 RB |
456 | (simplify |
457 | (abs (abs@1 @0)) | |
458 | @1) | |
f3582e54 RB |
459 | (simplify |
460 | (abs (negate @0)) | |
461 | (abs @0)) | |
462 | (simplify | |
463 | (abs tree_expr_nonnegative_p@0) | |
464 | @0) | |
465 | ||
b14a9c57 RB |
466 | /* A - B -> A + (-B) if B is easily negatable. This just covers |
467 | very few cases of "easily negatable", effectively inlining negate_expr_p. */ | |
468 | (simplify | |
469 | (minus @0 INTEGER_CST@1) | |
470 | (if ((INTEGRAL_TYPE_P (type) | |
471 | && TYPE_OVERFLOW_WRAPS (type)) | |
472 | || (!TYPE_OVERFLOW_SANITIZED (type) | |
473 | && may_negate_without_overflow_p (@1))) | |
474 | (plus @0 (negate @1)))) | |
475 | (simplify | |
476 | (minus @0 REAL_CST@1) | |
477 | (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1))) | |
478 | (plus @0 (negate @1)))) | |
479 | (simplify | |
480 | (minus @0 VECTOR_CST@1) | |
481 | (if (FLOAT_TYPE_P (type) || TYPE_OVERFLOW_WRAPS (type)) | |
482 | (plus @0 (negate @1)))) | |
483 | ||
d4573ffe | 484 | |
5609420f RB |
485 | /* Try to fold (type) X op CST -> (type) (X op ((type-x) CST)) |
486 | when profitable. | |
487 | For bitwise binary operations apply operand conversions to the | |
488 | binary operation result instead of to the operands. This allows | |
489 | to combine successive conversions and bitwise binary operations. | |
490 | We combine the above two cases by using a conditional convert. */ | |
491 | (for bitop (bit_and bit_ior bit_xor) | |
492 | (simplify | |
493 | (bitop (convert @0) (convert? @1)) | |
494 | (if (((TREE_CODE (@1) == INTEGER_CST | |
495 | && INTEGRAL_TYPE_P (TREE_TYPE (@0)) | |
ad6f996c | 496 | && int_fits_type_p (@1, TREE_TYPE (@0))) |
aea417d7 | 497 | || types_match (@0, @1)) |
ad6f996c RB |
498 | /* ??? This transform conflicts with fold-const.c doing |
499 | Convert (T)(x & c) into (T)x & (T)c, if c is an integer | |
500 | constants (if x has signed type, the sign bit cannot be set | |
501 | in c). This folds extension into the BIT_AND_EXPR. | |
502 | Restrict it to GIMPLE to avoid endless recursions. */ | |
503 | && (bitop != BIT_AND_EXPR || GIMPLE) | |
5609420f RB |
504 | && (/* That's a good idea if the conversion widens the operand, thus |
505 | after hoisting the conversion the operation will be narrower. */ | |
506 | TYPE_PRECISION (TREE_TYPE (@0)) < TYPE_PRECISION (type) | |
507 | /* It's also a good idea if the conversion is to a non-integer | |
508 | mode. */ | |
509 | || GET_MODE_CLASS (TYPE_MODE (type)) != MODE_INT | |
510 | /* Or if the precision of TO is not the same as the precision | |
511 | of its mode. */ | |
512 | || TYPE_PRECISION (type) != GET_MODE_PRECISION (TYPE_MODE (type)))) | |
513 | (convert (bitop @0 (convert @1)))))) | |
514 | ||
b14a9c57 RB |
515 | (for bitop (bit_and bit_ior) |
516 | rbitop (bit_ior bit_and) | |
517 | /* (x | y) & x -> x */ | |
518 | /* (x & y) | x -> x */ | |
519 | (simplify | |
520 | (bitop:c (rbitop:c @0 @1) @0) | |
521 | @0) | |
522 | /* (~x | y) & x -> x & y */ | |
523 | /* (~x & y) | x -> x | y */ | |
524 | (simplify | |
525 | (bitop:c (rbitop:c (bit_not @0) @1) @0) | |
526 | (bitop @0 @1))) | |
527 | ||
5609420f RB |
528 | /* Simplify (A & B) OP0 (C & B) to (A OP0 C) & B. */ |
529 | (for bitop (bit_and bit_ior bit_xor) | |
530 | (simplify | |
531 | (bitop (bit_and:c @0 @1) (bit_and @2 @1)) | |
532 | (bit_and (bitop @0 @2) @1))) | |
533 | ||
534 | /* (x | CST1) & CST2 -> (x & CST2) | (CST1 & CST2) */ | |
535 | (simplify | |
536 | (bit_and (bit_ior @0 CONSTANT_CLASS_P@1) CONSTANT_CLASS_P@2) | |
537 | (bit_ior (bit_and @0 @2) (bit_and @1 @2))) | |
538 | ||
539 | /* Combine successive equal operations with constants. */ | |
540 | (for bitop (bit_and bit_ior bit_xor) | |
541 | (simplify | |
542 | (bitop (bitop @0 CONSTANT_CLASS_P@1) CONSTANT_CLASS_P@2) | |
543 | (bitop @0 (bitop @1 @2)))) | |
544 | ||
545 | /* Try simple folding for X op !X, and X op X with the help | |
546 | of the truth_valued_p and logical_inverted_value predicates. */ | |
547 | (match truth_valued_p | |
548 | @0 | |
549 | (if (INTEGRAL_TYPE_P (type) && TYPE_PRECISION (type) == 1))) | |
f84e7fd6 | 550 | (for op (tcc_comparison truth_and truth_andif truth_or truth_orif truth_xor) |
5609420f RB |
551 | (match truth_valued_p |
552 | (op @0 @1))) | |
553 | (match truth_valued_p | |
554 | (truth_not @0)) | |
555 | ||
556 | (match (logical_inverted_value @0) | |
557 | (bit_not truth_valued_p@0)) | |
558 | (match (logical_inverted_value @0) | |
09240451 | 559 | (eq @0 integer_zerop)) |
5609420f | 560 | (match (logical_inverted_value @0) |
09240451 | 561 | (ne truth_valued_p@0 integer_truep)) |
5609420f | 562 | (match (logical_inverted_value @0) |
09240451 | 563 | (bit_xor truth_valued_p@0 integer_truep)) |
5609420f RB |
564 | |
565 | /* X & !X -> 0. */ | |
566 | (simplify | |
567 | (bit_and:c @0 (logical_inverted_value @0)) | |
568 | { build_zero_cst (type); }) | |
569 | /* X | !X and X ^ !X -> 1, , if X is truth-valued. */ | |
570 | (for op (bit_ior bit_xor) | |
571 | (simplify | |
572 | (op:c truth_valued_p@0 (logical_inverted_value @0)) | |
f84e7fd6 | 573 | { constant_boolean_node (true, type); })) |
5609420f | 574 | |
5609420f RB |
575 | /* If arg1 and arg2 are booleans (or any single bit type) |
576 | then try to simplify: | |
577 | ||
578 | (~X & Y) -> X < Y | |
579 | (X & ~Y) -> Y < X | |
580 | (~X | Y) -> X <= Y | |
581 | (X | ~Y) -> Y <= X | |
582 | ||
583 | But only do this if our result feeds into a comparison as | |
584 | this transformation is not always a win, particularly on | |
585 | targets with and-not instructions. | |
586 | -> simplify_bitwise_binary_boolean */ | |
587 | (simplify | |
588 | (ne (bit_and:c (bit_not @0) @1) integer_zerop) | |
589 | (if (INTEGRAL_TYPE_P (TREE_TYPE (@1)) | |
590 | && TYPE_PRECISION (TREE_TYPE (@1)) == 1) | |
591 | (lt @0 @1))) | |
592 | (simplify | |
593 | (ne (bit_ior:c (bit_not @0) @1) integer_zerop) | |
594 | (if (INTEGRAL_TYPE_P (TREE_TYPE (@1)) | |
595 | && TYPE_PRECISION (TREE_TYPE (@1)) == 1) | |
596 | (le @0 @1))) | |
597 | ||
5609420f RB |
598 | /* ~~x -> x */ |
599 | (simplify | |
600 | (bit_not (bit_not @0)) | |
601 | @0) | |
602 | ||
b14a9c57 RB |
603 | /* Convert ~ (-A) to A - 1. */ |
604 | (simplify | |
605 | (bit_not (convert? (negate @0))) | |
606 | (if (tree_nop_conversion_p (type, TREE_TYPE (@0))) | |
607 | (convert (minus @0 { build_one_cst (TREE_TYPE (@0)); })))) | |
608 | ||
609 | /* Convert ~ (A - 1) or ~ (A + -1) to -A. */ | |
610 | (simplify | |
611 | (bit_not (convert? (minus @0 integer_onep))) | |
612 | (if (tree_nop_conversion_p (type, TREE_TYPE (@0))) | |
613 | (convert (negate @0)))) | |
614 | (simplify | |
615 | (bit_not (convert? (plus @0 integer_all_onesp))) | |
616 | (if (tree_nop_conversion_p (type, TREE_TYPE (@0))) | |
617 | (convert (negate @0)))) | |
618 | ||
619 | /* Part of convert ~(X ^ Y) to ~X ^ Y or X ^ ~Y if ~X or ~Y simplify. */ | |
620 | (simplify | |
621 | (bit_not (convert? (bit_xor @0 INTEGER_CST@1))) | |
622 | (if (tree_nop_conversion_p (type, TREE_TYPE (@0))) | |
623 | (convert (bit_xor @0 (bit_not @1))))) | |
624 | (simplify | |
625 | (bit_not (convert? (bit_xor:c (bit_not @0) @1))) | |
626 | (if (tree_nop_conversion_p (type, TREE_TYPE (@0))) | |
627 | (convert (bit_xor @0 @1)))) | |
628 | ||
f52baa7b MP |
629 | /* (x & ~m) | (y & m) -> ((x ^ y) & m) ^ x */ |
630 | (simplify | |
631 | (bit_ior:c (bit_and:c@3 @0 (bit_not @2)) (bit_and:c@4 @1 @2)) | |
6e28e516 | 632 | (if (single_use (@3) && single_use (@4)) |
f52baa7b MP |
633 | (bit_xor (bit_and (bit_xor @0 @1) @2) @0))) |
634 | ||
5609420f | 635 | |
a499aac5 RB |
636 | /* Associate (p +p off1) +p off2 as (p +p (off1 + off2)). */ |
637 | (simplify | |
e6121733 | 638 | (pointer_plus (pointer_plus@2 @0 @1) @3) |
7318e44f RB |
639 | (if (single_use (@2) |
640 | || (TREE_CODE (@1) == INTEGER_CST && TREE_CODE (@3) == INTEGER_CST)) | |
e6121733 | 641 | (pointer_plus @0 (plus @1 @3)))) |
a499aac5 RB |
642 | |
643 | /* Pattern match | |
644 | tem1 = (long) ptr1; | |
645 | tem2 = (long) ptr2; | |
646 | tem3 = tem2 - tem1; | |
647 | tem4 = (unsigned long) tem3; | |
648 | tem5 = ptr1 + tem4; | |
649 | and produce | |
650 | tem5 = ptr2; */ | |
651 | (simplify | |
652 | (pointer_plus @0 (convert?@2 (minus@3 (convert @1) (convert @0)))) | |
653 | /* Conditionally look through a sign-changing conversion. */ | |
654 | (if (TYPE_PRECISION (TREE_TYPE (@2)) == TYPE_PRECISION (TREE_TYPE (@3)) | |
655 | && ((GIMPLE && useless_type_conversion_p (type, TREE_TYPE (@1))) | |
656 | || (GENERIC && type == TREE_TYPE (@1)))) | |
657 | @1)) | |
658 | ||
659 | /* Pattern match | |
660 | tem = (sizetype) ptr; | |
661 | tem = tem & algn; | |
662 | tem = -tem; | |
663 | ... = ptr p+ tem; | |
664 | and produce the simpler and easier to analyze with respect to alignment | |
665 | ... = ptr & ~algn; */ | |
666 | (simplify | |
667 | (pointer_plus @0 (negate (bit_and (convert @0) INTEGER_CST@1))) | |
668 | (with { tree algn = wide_int_to_tree (TREE_TYPE (@0), wi::bit_not (@1)); } | |
669 | (bit_and @0 { algn; }))) | |
670 | ||
99e943a2 RB |
671 | /* Try folding difference of addresses. */ |
672 | (simplify | |
673 | (minus (convert ADDR_EXPR@0) (convert @1)) | |
674 | (if (tree_nop_conversion_p (type, TREE_TYPE (@0))) | |
675 | (with { HOST_WIDE_INT diff; } | |
676 | (if (ptr_difference_const (@0, @1, &diff)) | |
677 | { build_int_cst_type (type, diff); })))) | |
678 | (simplify | |
679 | (minus (convert @0) (convert ADDR_EXPR@1)) | |
680 | (if (tree_nop_conversion_p (type, TREE_TYPE (@0))) | |
681 | (with { HOST_WIDE_INT diff; } | |
682 | (if (ptr_difference_const (@0, @1, &diff)) | |
683 | { build_int_cst_type (type, diff); })))) | |
684 | ||
bab73f11 RB |
685 | /* If arg0 is derived from the address of an object or function, we may |
686 | be able to fold this expression using the object or function's | |
687 | alignment. */ | |
688 | (simplify | |
689 | (bit_and (convert? @0) INTEGER_CST@1) | |
690 | (if (POINTER_TYPE_P (TREE_TYPE (@0)) | |
691 | && tree_nop_conversion_p (type, TREE_TYPE (@0))) | |
692 | (with | |
693 | { | |
694 | unsigned int align; | |
695 | unsigned HOST_WIDE_INT bitpos; | |
696 | get_pointer_alignment_1 (@0, &align, &bitpos); | |
697 | } | |
698 | (if (wi::ltu_p (@1, align / BITS_PER_UNIT)) | |
699 | { wide_int_to_tree (type, wi::bit_and (@1, bitpos / BITS_PER_UNIT)); })))) | |
99e943a2 | 700 | |
a499aac5 | 701 | |
cc7b5acf RB |
702 | /* We can't reassociate at all for saturating types. */ |
703 | (if (!TYPE_SATURATING (type)) | |
704 | ||
705 | /* Contract negates. */ | |
706 | /* A + (-B) -> A - B */ | |
707 | (simplify | |
708 | (plus:c (convert1? @0) (convert2? (negate @1))) | |
709 | /* Apply STRIP_NOPS on @0 and the negate. */ | |
710 | (if (tree_nop_conversion_p (type, TREE_TYPE (@0)) | |
711 | && tree_nop_conversion_p (type, TREE_TYPE (@1)) | |
6a4f0678 | 712 | && !TYPE_OVERFLOW_SANITIZED (type)) |
cc7b5acf RB |
713 | (minus (convert @0) (convert @1)))) |
714 | /* A - (-B) -> A + B */ | |
715 | (simplify | |
716 | (minus (convert1? @0) (convert2? (negate @1))) | |
717 | (if (tree_nop_conversion_p (type, TREE_TYPE (@0)) | |
2f68e8bc | 718 | && tree_nop_conversion_p (type, TREE_TYPE (@1)) |
6a4f0678 | 719 | && !TYPE_OVERFLOW_SANITIZED (type)) |
cc7b5acf RB |
720 | (plus (convert @0) (convert @1)))) |
721 | /* -(-A) -> A */ | |
722 | (simplify | |
723 | (negate (convert? (negate @1))) | |
724 | (if (tree_nop_conversion_p (type, TREE_TYPE (@1)) | |
6a4f0678 | 725 | && !TYPE_OVERFLOW_SANITIZED (type)) |
a0f12cf8 | 726 | (convert @1))) |
cc7b5acf | 727 | |
7318e44f RB |
728 | /* We can't reassociate floating-point unless -fassociative-math |
729 | or fixed-point plus or minus because of saturation to +-Inf. */ | |
730 | (if ((!FLOAT_TYPE_P (type) || flag_associative_math) | |
731 | && !FIXED_POINT_TYPE_P (type)) | |
cc7b5acf RB |
732 | |
733 | /* Match patterns that allow contracting a plus-minus pair | |
734 | irrespective of overflow issues. */ | |
735 | /* (A +- B) - A -> +- B */ | |
736 | /* (A +- B) -+ B -> A */ | |
737 | /* A - (A +- B) -> -+ B */ | |
738 | /* A +- (B -+ A) -> +- B */ | |
739 | (simplify | |
740 | (minus (plus:c @0 @1) @0) | |
741 | @1) | |
742 | (simplify | |
743 | (minus (minus @0 @1) @0) | |
744 | (negate @1)) | |
745 | (simplify | |
746 | (plus:c (minus @0 @1) @1) | |
747 | @0) | |
748 | (simplify | |
749 | (minus @0 (plus:c @0 @1)) | |
750 | (negate @1)) | |
751 | (simplify | |
752 | (minus @0 (minus @0 @1)) | |
753 | @1) | |
754 | ||
755 | /* (A +- CST) +- CST -> A + CST */ | |
756 | (for outer_op (plus minus) | |
757 | (for inner_op (plus minus) | |
758 | (simplify | |
759 | (outer_op (inner_op @0 CONSTANT_CLASS_P@1) CONSTANT_CLASS_P@2) | |
760 | /* If the constant operation overflows we cannot do the transform | |
761 | as we would introduce undefined overflow, for example | |
762 | with (a - 1) + INT_MIN. */ | |
763 | (with { tree cst = fold_binary (outer_op == inner_op | |
764 | ? PLUS_EXPR : MINUS_EXPR, type, @1, @2); } | |
765 | (if (cst && !TREE_OVERFLOW (cst)) | |
766 | (inner_op @0 { cst; } )))))) | |
767 | ||
768 | /* (CST - A) +- CST -> CST - A */ | |
769 | (for outer_op (plus minus) | |
770 | (simplify | |
771 | (outer_op (minus CONSTANT_CLASS_P@1 @0) CONSTANT_CLASS_P@2) | |
772 | (with { tree cst = fold_binary (outer_op, type, @1, @2); } | |
773 | (if (cst && !TREE_OVERFLOW (cst)) | |
774 | (minus { cst; } @0))))) | |
775 | ||
776 | /* ~A + A -> -1 */ | |
777 | (simplify | |
778 | (plus:c (bit_not @0) @0) | |
779 | (if (!TYPE_OVERFLOW_TRAPS (type)) | |
780 | { build_all_ones_cst (type); })) | |
781 | ||
782 | /* ~A + 1 -> -A */ | |
783 | (simplify | |
e19740ae RB |
784 | (plus (convert? (bit_not @0)) integer_each_onep) |
785 | (if (tree_nop_conversion_p (type, TREE_TYPE (@0))) | |
786 | (negate (convert @0)))) | |
787 | ||
788 | /* -A - 1 -> ~A */ | |
789 | (simplify | |
790 | (minus (convert? (negate @0)) integer_each_onep) | |
791 | (if (!TYPE_OVERFLOW_TRAPS (type) | |
792 | && tree_nop_conversion_p (type, TREE_TYPE (@0))) | |
793 | (bit_not (convert @0)))) | |
794 | ||
795 | /* -1 - A -> ~A */ | |
796 | (simplify | |
797 | (minus integer_all_onesp @0) | |
bc4315fb | 798 | (bit_not @0)) |
cc7b5acf RB |
799 | |
800 | /* (T)(P + A) - (T)P -> (T) A */ | |
801 | (for add (plus pointer_plus) | |
802 | (simplify | |
803 | (minus (convert (add @0 @1)) | |
804 | (convert @0)) | |
09240451 | 805 | (if (element_precision (type) <= element_precision (TREE_TYPE (@1)) |
cc7b5acf RB |
806 | /* For integer types, if A has a smaller type |
807 | than T the result depends on the possible | |
808 | overflow in P + A. | |
809 | E.g. T=size_t, A=(unsigned)429497295, P>0. | |
810 | However, if an overflow in P + A would cause | |
811 | undefined behavior, we can assume that there | |
812 | is no overflow. */ | |
813 | || (INTEGRAL_TYPE_P (TREE_TYPE (@0)) | |
814 | && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))) | |
815 | /* For pointer types, if the conversion of A to the | |
816 | final type requires a sign- or zero-extension, | |
817 | then we have to punt - it is not defined which | |
818 | one is correct. */ | |
819 | || (POINTER_TYPE_P (TREE_TYPE (@0)) | |
820 | && TREE_CODE (@1) == INTEGER_CST | |
821 | && tree_int_cst_sign_bit (@1) == 0)) | |
822 | (convert @1)))))) | |
823 | ||
824 | ||
a7f24614 RB |
825 | /* Simplifications of MIN_EXPR and MAX_EXPR. */ |
826 | ||
827 | (for minmax (min max) | |
828 | (simplify | |
829 | (minmax @0 @0) | |
830 | @0)) | |
831 | (simplify | |
832 | (min @0 @1) | |
833 | (if (INTEGRAL_TYPE_P (type) | |
834 | && TYPE_MIN_VALUE (type) | |
835 | && operand_equal_p (@1, TYPE_MIN_VALUE (type), OEP_ONLY_CONST)) | |
836 | @1)) | |
837 | (simplify | |
838 | (max @0 @1) | |
839 | (if (INTEGRAL_TYPE_P (type) | |
840 | && TYPE_MAX_VALUE (type) | |
841 | && operand_equal_p (@1, TYPE_MAX_VALUE (type), OEP_ONLY_CONST)) | |
842 | @1)) | |
843 | ||
844 | ||
845 | /* Simplifications of shift and rotates. */ | |
846 | ||
847 | (for rotate (lrotate rrotate) | |
848 | (simplify | |
849 | (rotate integer_all_onesp@0 @1) | |
850 | @0)) | |
851 | ||
852 | /* Optimize -1 >> x for arithmetic right shifts. */ | |
853 | (simplify | |
854 | (rshift integer_all_onesp@0 @1) | |
855 | (if (!TYPE_UNSIGNED (type) | |
856 | && tree_expr_nonnegative_p (@1)) | |
857 | @0)) | |
858 | ||
859 | (for shiftrotate (lrotate rrotate lshift rshift) | |
860 | (simplify | |
861 | (shiftrotate @0 integer_zerop) | |
862 | (non_lvalue @0)) | |
863 | (simplify | |
864 | (shiftrotate integer_zerop@0 @1) | |
865 | @0) | |
866 | /* Prefer vector1 << scalar to vector1 << vector2 | |
867 | if vector2 is uniform. */ | |
868 | (for vec (VECTOR_CST CONSTRUCTOR) | |
869 | (simplify | |
870 | (shiftrotate @0 vec@1) | |
871 | (with { tree tem = uniform_vector_p (@1); } | |
872 | (if (tem) | |
873 | (shiftrotate @0 { tem; })))))) | |
874 | ||
875 | /* Rewrite an LROTATE_EXPR by a constant into an | |
876 | RROTATE_EXPR by a new constant. */ | |
877 | (simplify | |
878 | (lrotate @0 INTEGER_CST@1) | |
879 | (rrotate @0 { fold_binary (MINUS_EXPR, TREE_TYPE (@1), | |
880 | build_int_cst (TREE_TYPE (@1), | |
881 | element_precision (type)), @1); })) | |
882 | ||
01ada710 MP |
883 | /* ((1 << A) & 1) != 0 -> A == 0 |
884 | ((1 << A) & 1) == 0 -> A != 0 */ | |
885 | (for cmp (ne eq) | |
886 | icmp (eq ne) | |
887 | (simplify | |
888 | (cmp (bit_and (lshift integer_onep @0) integer_onep) integer_zerop) | |
889 | (icmp @0 { build_zero_cst (TREE_TYPE (@0)); }))) | |
cc7b5acf | 890 | |
f2e609c3 MP |
891 | /* (CST1 << A) == CST2 -> A == ctz (CST2) - ctz (CST1) |
892 | (CST1 << A) != CST2 -> A != ctz (CST2) - ctz (CST1) | |
893 | if CST2 != 0. */ | |
894 | (for cmp (ne eq) | |
895 | (simplify | |
896 | (cmp (lshift INTEGER_CST@0 @1) INTEGER_CST@2) | |
897 | (with { int cand = wi::ctz (@2) - wi::ctz (@0); } | |
898 | (if (cand < 0 | |
899 | || (!integer_zerop (@2) | |
900 | && wi::ne_p (wi::lshift (@0, cand), @2))) | |
901 | { constant_boolean_node (cmp == NE_EXPR, type); }) | |
902 | (if (!integer_zerop (@2) | |
903 | && wi::eq_p (wi::lshift (@0, cand), @2)) | |
904 | (cmp @1 { build_int_cst (TREE_TYPE (@1), cand); }))))) | |
905 | ||
1ffbaa3f RB |
906 | /* Fold (X << C1) & C2 into (X << C1) & (C2 | ((1 << C1) - 1)) |
907 | (X >> C1) & C2 into (X >> C1) & (C2 | ~((type) -1 >> C1)) | |
908 | if the new mask might be further optimized. */ | |
909 | (for shift (lshift rshift) | |
910 | (simplify | |
911 | (bit_and (convert?@4 (shift@5 (convert1?@3 @0) INTEGER_CST@1)) INTEGER_CST@2) | |
912 | (if (tree_nop_conversion_p (TREE_TYPE (@4), TREE_TYPE (@5)) | |
913 | && TYPE_PRECISION (type) <= HOST_BITS_PER_WIDE_INT | |
914 | && tree_fits_uhwi_p (@1) | |
915 | && tree_to_uhwi (@1) > 0 | |
916 | && tree_to_uhwi (@1) < TYPE_PRECISION (type)) | |
917 | (with | |
918 | { | |
919 | unsigned int shiftc = tree_to_uhwi (@1); | |
920 | unsigned HOST_WIDE_INT mask = TREE_INT_CST_LOW (@2); | |
921 | unsigned HOST_WIDE_INT newmask, zerobits = 0; | |
922 | tree shift_type = TREE_TYPE (@3); | |
923 | unsigned int prec; | |
924 | ||
925 | if (shift == LSHIFT_EXPR) | |
926 | zerobits = ((((unsigned HOST_WIDE_INT) 1) << shiftc) - 1); | |
927 | else if (shift == RSHIFT_EXPR | |
928 | && (TYPE_PRECISION (shift_type) | |
929 | == GET_MODE_PRECISION (TYPE_MODE (shift_type)))) | |
930 | { | |
931 | prec = TYPE_PRECISION (TREE_TYPE (@3)); | |
932 | tree arg00 = @0; | |
933 | /* See if more bits can be proven as zero because of | |
934 | zero extension. */ | |
935 | if (@3 != @0 | |
936 | && TYPE_UNSIGNED (TREE_TYPE (@0))) | |
937 | { | |
938 | tree inner_type = TREE_TYPE (@0); | |
939 | if ((TYPE_PRECISION (inner_type) | |
940 | == GET_MODE_PRECISION (TYPE_MODE (inner_type))) | |
941 | && TYPE_PRECISION (inner_type) < prec) | |
942 | { | |
943 | prec = TYPE_PRECISION (inner_type); | |
944 | /* See if we can shorten the right shift. */ | |
945 | if (shiftc < prec) | |
946 | shift_type = inner_type; | |
947 | /* Otherwise X >> C1 is all zeros, so we'll optimize | |
948 | it into (X, 0) later on by making sure zerobits | |
949 | is all ones. */ | |
950 | } | |
951 | } | |
952 | zerobits = ~(unsigned HOST_WIDE_INT) 0; | |
953 | if (shiftc < prec) | |
954 | { | |
955 | zerobits >>= HOST_BITS_PER_WIDE_INT - shiftc; | |
956 | zerobits <<= prec - shiftc; | |
957 | } | |
958 | /* For arithmetic shift if sign bit could be set, zerobits | |
959 | can contain actually sign bits, so no transformation is | |
960 | possible, unless MASK masks them all away. In that | |
961 | case the shift needs to be converted into logical shift. */ | |
962 | if (!TYPE_UNSIGNED (TREE_TYPE (@3)) | |
963 | && prec == TYPE_PRECISION (TREE_TYPE (@3))) | |
964 | { | |
965 | if ((mask & zerobits) == 0) | |
966 | shift_type = unsigned_type_for (TREE_TYPE (@3)); | |
967 | else | |
968 | zerobits = 0; | |
969 | } | |
970 | } | |
971 | } | |
972 | /* ((X << 16) & 0xff00) is (X, 0). */ | |
973 | (if ((mask & zerobits) == mask) | |
974 | { build_int_cst (type, 0); }) | |
975 | (with { newmask = mask | zerobits; } | |
976 | (if (newmask != mask && (newmask & (newmask + 1)) == 0) | |
977 | (with | |
978 | { | |
979 | /* Only do the transformation if NEWMASK is some integer | |
980 | mode's mask. */ | |
981 | for (prec = BITS_PER_UNIT; | |
982 | prec < HOST_BITS_PER_WIDE_INT; prec <<= 1) | |
983 | if (newmask == (((unsigned HOST_WIDE_INT) 1) << prec) - 1) | |
984 | break; | |
985 | } | |
986 | (if (prec < HOST_BITS_PER_WIDE_INT | |
987 | || newmask == ~(unsigned HOST_WIDE_INT) 0) | |
988 | (with | |
989 | { tree newmaskt = build_int_cst_type (TREE_TYPE (@2), newmask); } | |
990 | (if (!tree_int_cst_equal (newmaskt, @2)) | |
991 | (if (shift_type != TREE_TYPE (@3) | |
992 | && single_use (@4) && single_use (@5)) | |
993 | (bit_and (convert (shift:shift_type (convert @3) @1)) { newmaskt; })) | |
994 | (if (shift_type == TREE_TYPE (@3)) | |
995 | (bit_and @4 { newmaskt; })))))))))))) | |
996 | ||
d4573ffe RB |
997 | /* Simplifications of conversions. */ |
998 | ||
999 | /* Basic strip-useless-type-conversions / strip_nops. */ | |
f3582e54 | 1000 | (for cvt (convert view_convert float fix_trunc) |
d4573ffe RB |
1001 | (simplify |
1002 | (cvt @0) | |
1003 | (if ((GIMPLE && useless_type_conversion_p (type, TREE_TYPE (@0))) | |
1004 | || (GENERIC && type == TREE_TYPE (@0))) | |
1005 | @0))) | |
1006 | ||
1007 | /* Contract view-conversions. */ | |
1008 | (simplify | |
1009 | (view_convert (view_convert @0)) | |
1010 | (view_convert @0)) | |
1011 | ||
1012 | /* For integral conversions with the same precision or pointer | |
1013 | conversions use a NOP_EXPR instead. */ | |
1014 | (simplify | |
1015 | (view_convert @0) | |
1016 | (if ((INTEGRAL_TYPE_P (type) || POINTER_TYPE_P (type)) | |
1017 | && (INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0))) | |
1018 | && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (@0))) | |
1019 | (convert @0))) | |
1020 | ||
1021 | /* Strip inner integral conversions that do not change precision or size. */ | |
1022 | (simplify | |
1023 | (view_convert (convert@0 @1)) | |
1024 | (if ((INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0))) | |
1025 | && (INTEGRAL_TYPE_P (TREE_TYPE (@1)) || POINTER_TYPE_P (TREE_TYPE (@1))) | |
1026 | && (TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1))) | |
1027 | && (TYPE_SIZE (TREE_TYPE (@0)) == TYPE_SIZE (TREE_TYPE (@1)))) | |
1028 | (view_convert @1))) | |
1029 | ||
1030 | /* Re-association barriers around constants and other re-association | |
1031 | barriers can be removed. */ | |
1032 | (simplify | |
1033 | (paren CONSTANT_CLASS_P@0) | |
1034 | @0) | |
1035 | (simplify | |
1036 | (paren (paren@1 @0)) | |
1037 | @1) | |
1e51d0a2 RB |
1038 | |
1039 | /* Handle cases of two conversions in a row. */ | |
1040 | (for ocvt (convert float fix_trunc) | |
1041 | (for icvt (convert float) | |
1042 | (simplify | |
1043 | (ocvt (icvt@1 @0)) | |
1044 | (with | |
1045 | { | |
1046 | tree inside_type = TREE_TYPE (@0); | |
1047 | tree inter_type = TREE_TYPE (@1); | |
1048 | int inside_int = INTEGRAL_TYPE_P (inside_type); | |
1049 | int inside_ptr = POINTER_TYPE_P (inside_type); | |
1050 | int inside_float = FLOAT_TYPE_P (inside_type); | |
09240451 | 1051 | int inside_vec = VECTOR_TYPE_P (inside_type); |
1e51d0a2 RB |
1052 | unsigned int inside_prec = TYPE_PRECISION (inside_type); |
1053 | int inside_unsignedp = TYPE_UNSIGNED (inside_type); | |
1054 | int inter_int = INTEGRAL_TYPE_P (inter_type); | |
1055 | int inter_ptr = POINTER_TYPE_P (inter_type); | |
1056 | int inter_float = FLOAT_TYPE_P (inter_type); | |
09240451 | 1057 | int inter_vec = VECTOR_TYPE_P (inter_type); |
1e51d0a2 RB |
1058 | unsigned int inter_prec = TYPE_PRECISION (inter_type); |
1059 | int inter_unsignedp = TYPE_UNSIGNED (inter_type); | |
1060 | int final_int = INTEGRAL_TYPE_P (type); | |
1061 | int final_ptr = POINTER_TYPE_P (type); | |
1062 | int final_float = FLOAT_TYPE_P (type); | |
09240451 | 1063 | int final_vec = VECTOR_TYPE_P (type); |
1e51d0a2 RB |
1064 | unsigned int final_prec = TYPE_PRECISION (type); |
1065 | int final_unsignedp = TYPE_UNSIGNED (type); | |
1066 | } | |
1067 | /* In addition to the cases of two conversions in a row | |
1068 | handled below, if we are converting something to its own | |
1069 | type via an object of identical or wider precision, neither | |
1070 | conversion is needed. */ | |
1071 | (if (((GIMPLE && useless_type_conversion_p (type, inside_type)) | |
1072 | || (GENERIC | |
1073 | && TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (inside_type))) | |
1074 | && (((inter_int || inter_ptr) && final_int) | |
1075 | || (inter_float && final_float)) | |
1076 | && inter_prec >= final_prec) | |
1077 | (ocvt @0)) | |
1078 | ||
1079 | /* Likewise, if the intermediate and initial types are either both | |
1080 | float or both integer, we don't need the middle conversion if the | |
1081 | former is wider than the latter and doesn't change the signedness | |
1082 | (for integers). Avoid this if the final type is a pointer since | |
1083 | then we sometimes need the middle conversion. Likewise if the | |
1084 | final type has a precision not equal to the size of its mode. */ | |
d51a6714 JJ |
1085 | (if (((inter_int && inside_int) || (inter_float && inside_float)) |
1086 | && (final_int || final_float) | |
1e51d0a2 | 1087 | && inter_prec >= inside_prec |
d51a6714 JJ |
1088 | && (inter_float || inter_unsignedp == inside_unsignedp) |
1089 | && ! (final_prec != GET_MODE_PRECISION (TYPE_MODE (type)) | |
1090 | && TYPE_MODE (type) == TYPE_MODE (inter_type))) | |
1e51d0a2 RB |
1091 | (ocvt @0)) |
1092 | ||
1093 | /* If we have a sign-extension of a zero-extended value, we can | |
1094 | replace that by a single zero-extension. Likewise if the | |
1095 | final conversion does not change precision we can drop the | |
1096 | intermediate conversion. */ | |
1097 | (if (inside_int && inter_int && final_int | |
1098 | && ((inside_prec < inter_prec && inter_prec < final_prec | |
1099 | && inside_unsignedp && !inter_unsignedp) | |
1100 | || final_prec == inter_prec)) | |
1101 | (ocvt @0)) | |
1102 | ||
1103 | /* Two conversions in a row are not needed unless: | |
1104 | - some conversion is floating-point (overstrict for now), or | |
1105 | - some conversion is a vector (overstrict for now), or | |
1106 | - the intermediate type is narrower than both initial and | |
1107 | final, or | |
1108 | - the intermediate type and innermost type differ in signedness, | |
1109 | and the outermost type is wider than the intermediate, or | |
1110 | - the initial type is a pointer type and the precisions of the | |
1111 | intermediate and final types differ, or | |
1112 | - the final type is a pointer type and the precisions of the | |
1113 | initial and intermediate types differ. */ | |
1114 | (if (! inside_float && ! inter_float && ! final_float | |
1115 | && ! inside_vec && ! inter_vec && ! final_vec | |
1116 | && (inter_prec >= inside_prec || inter_prec >= final_prec) | |
1117 | && ! (inside_int && inter_int | |
1118 | && inter_unsignedp != inside_unsignedp | |
1119 | && inter_prec < final_prec) | |
1120 | && ((inter_unsignedp && inter_prec > inside_prec) | |
1121 | == (final_unsignedp && final_prec > inter_prec)) | |
1122 | && ! (inside_ptr && inter_prec != final_prec) | |
1123 | && ! (final_ptr && inside_prec != inter_prec) | |
1124 | && ! (final_prec != GET_MODE_PRECISION (TYPE_MODE (type)) | |
1125 | && TYPE_MODE (type) == TYPE_MODE (inter_type))) | |
1f00c1b9 RB |
1126 | (ocvt @0)) |
1127 | ||
1128 | /* A truncation to an unsigned type (a zero-extension) should be | |
1129 | canonicalized as bitwise and of a mask. */ | |
1130 | (if (final_int && inter_int && inside_int | |
1131 | && final_prec == inside_prec | |
1132 | && final_prec > inter_prec | |
1133 | && inter_unsignedp) | |
1134 | (convert (bit_and @0 { wide_int_to_tree | |
1135 | (inside_type, | |
1136 | wi::mask (inter_prec, false, | |
1137 | TYPE_PRECISION (inside_type))); }))) | |
1138 | ||
1139 | /* If we are converting an integer to a floating-point that can | |
1140 | represent it exactly and back to an integer, we can skip the | |
1141 | floating-point conversion. */ | |
5ba3ae6d RB |
1142 | (if (GIMPLE /* PR66211 */ |
1143 | && inside_int && inter_float && final_int && | |
1f00c1b9 RB |
1144 | (unsigned) significand_size (TYPE_MODE (inter_type)) |
1145 | >= inside_prec - !inside_unsignedp) | |
1146 | (convert @0)))))) | |
ea2042ba RB |
1147 | |
1148 | /* If we have a narrowing conversion to an integral type that is fed by a | |
1149 | BIT_AND_EXPR, we might be able to remove the BIT_AND_EXPR if it merely | |
1150 | masks off bits outside the final type (and nothing else). */ | |
1151 | (simplify | |
1152 | (convert (bit_and @0 INTEGER_CST@1)) | |
1153 | (if (INTEGRAL_TYPE_P (type) | |
1154 | && INTEGRAL_TYPE_P (TREE_TYPE (@0)) | |
1155 | && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0)) | |
1156 | && operand_equal_p (@1, build_low_bits_mask (TREE_TYPE (@1), | |
1157 | TYPE_PRECISION (type)), 0)) | |
1158 | (convert @0))) | |
a25454ea RB |
1159 | |
1160 | ||
1161 | /* (X /[ex] A) * A -> X. */ | |
1162 | (simplify | |
1163 | (mult (convert? (exact_div @0 @1)) @1) | |
1164 | /* Look through a sign-changing conversion. */ | |
257b01ba | 1165 | (convert @0)) |
eaeba53a | 1166 | |
a7f24614 RB |
1167 | /* Canonicalization of binary operations. */ |
1168 | ||
1169 | /* Convert X + -C into X - C. */ | |
1170 | (simplify | |
1171 | (plus @0 REAL_CST@1) | |
1172 | (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1))) | |
1173 | (with { tree tem = fold_unary (NEGATE_EXPR, type, @1); } | |
1174 | (if (!TREE_OVERFLOW (tem) || !flag_trapping_math) | |
1175 | (minus @0 { tem; }))))) | |
1176 | ||
1177 | /* Convert x+x into x*2.0. */ | |
1178 | (simplify | |
1179 | (plus @0 @0) | |
1180 | (if (SCALAR_FLOAT_TYPE_P (type)) | |
1181 | (mult @0 { build_real (type, dconst2); }))) | |
1182 | ||
1183 | (simplify | |
1184 | (minus integer_zerop @1) | |
1185 | (negate @1)) | |
1186 | ||
1187 | /* (ARG0 - ARG1) is the same as (-ARG1 + ARG0). So check whether | |
1188 | ARG0 is zero and X + ARG0 reduces to X, since that would mean | |
1189 | (-ARG1 + ARG0) reduces to -ARG1. */ | |
1190 | (simplify | |
1191 | (minus real_zerop@0 @1) | |
1192 | (if (fold_real_zero_addition_p (type, @0, 0)) | |
1193 | (negate @1))) | |
1194 | ||
1195 | /* Transform x * -1 into -x. */ | |
1196 | (simplify | |
1197 | (mult @0 integer_minus_onep) | |
1198 | (negate @0)) | |
eaeba53a RB |
1199 | |
1200 | /* COMPLEX_EXPR and REALPART/IMAGPART_EXPR cancellations. */ | |
1201 | (simplify | |
1202 | (complex (realpart @0) (imagpart @0)) | |
1203 | @0) | |
1204 | (simplify | |
1205 | (realpart (complex @0 @1)) | |
1206 | @0) | |
1207 | (simplify | |
1208 | (imagpart (complex @0 @1)) | |
1209 | @1) | |
83633539 RB |
1210 | |
1211 | ||
1212 | /* BSWAP simplifications, transforms checked by gcc.dg/builtin-bswap-8.c. */ | |
1213 | (for bswap (BUILT_IN_BSWAP16 BUILT_IN_BSWAP32 BUILT_IN_BSWAP64) | |
1214 | (simplify | |
1215 | (bswap (bswap @0)) | |
1216 | @0) | |
1217 | (simplify | |
1218 | (bswap (bit_not (bswap @0))) | |
1219 | (bit_not @0)) | |
1220 | (for bitop (bit_xor bit_ior bit_and) | |
1221 | (simplify | |
1222 | (bswap (bitop:c (bswap @0) @1)) | |
1223 | (bitop @0 (bswap @1))))) | |
96994de0 RB |
1224 | |
1225 | ||
1226 | /* Combine COND_EXPRs and VEC_COND_EXPRs. */ | |
1227 | ||
1228 | /* Simplify constant conditions. | |
1229 | Only optimize constant conditions when the selected branch | |
1230 | has the same type as the COND_EXPR. This avoids optimizing | |
1231 | away "c ? x : throw", where the throw has a void type. | |
1232 | Note that we cannot throw away the fold-const.c variant nor | |
1233 | this one as we depend on doing this transform before possibly | |
1234 | A ? B : B -> B triggers and the fold-const.c one can optimize | |
1235 | 0 ? A : B to B even if A has side-effects. Something | |
1236 | genmatch cannot handle. */ | |
1237 | (simplify | |
1238 | (cond INTEGER_CST@0 @1 @2) | |
1239 | (if (integer_zerop (@0) | |
1240 | && (!VOID_TYPE_P (TREE_TYPE (@2)) | |
1241 | || VOID_TYPE_P (type))) | |
1242 | @2) | |
1243 | (if (!integer_zerop (@0) | |
1244 | && (!VOID_TYPE_P (TREE_TYPE (@1)) | |
1245 | || VOID_TYPE_P (type))) | |
1246 | @1)) | |
1247 | (simplify | |
1248 | (vec_cond VECTOR_CST@0 @1 @2) | |
1249 | (if (integer_all_onesp (@0)) | |
1250 | @1) | |
1251 | (if (integer_zerop (@0)) | |
1252 | @2)) | |
1253 | ||
1254 | (for cnd (cond vec_cond) | |
1255 | /* A ? B : (A ? X : C) -> A ? B : C. */ | |
1256 | (simplify | |
1257 | (cnd @0 (cnd @0 @1 @2) @3) | |
1258 | (cnd @0 @1 @3)) | |
1259 | (simplify | |
1260 | (cnd @0 @1 (cnd @0 @2 @3)) | |
1261 | (cnd @0 @1 @3)) | |
1262 | ||
1263 | /* A ? B : B -> B. */ | |
1264 | (simplify | |
1265 | (cnd @0 @1 @1) | |
09240451 | 1266 | @1) |
96994de0 | 1267 | |
09240451 MG |
1268 | /* !A ? B : C -> A ? C : B. */ |
1269 | (simplify | |
1270 | (cnd (logical_inverted_value truth_valued_p@0) @1 @2) | |
1271 | (cnd @0 @2 @1))) | |
f84e7fd6 | 1272 | |
f43d102e RS |
1273 | /* A + (B vcmp C ? 1 : 0) -> A - (B vcmp C), since vector comparisons |
1274 | return all-1 or all-0 results. */ | |
1275 | /* ??? We could instead convert all instances of the vec_cond to negate, | |
1276 | but that isn't necessarily a win on its own. */ | |
1277 | (simplify | |
1278 | (plus:c @3 (view_convert? (vec_cond @0 integer_each_onep@1 integer_zerop@2))) | |
1279 | (if (VECTOR_TYPE_P (type) | |
1280 | && TYPE_VECTOR_SUBPARTS (type) == TYPE_VECTOR_SUBPARTS (TREE_TYPE (@0)) | |
1281 | && (TYPE_MODE (TREE_TYPE (type)) | |
1282 | == TYPE_MODE (TREE_TYPE (TREE_TYPE (@0))))) | |
1283 | (minus @3 (view_convert @0)))) | |
1284 | ||
1285 | /* ... likewise A - (B vcmp C ? 1 : 0) -> A + (B vcmp C). */ | |
1286 | (simplify | |
1287 | (minus @3 (view_convert? (vec_cond @0 integer_each_onep@1 integer_zerop@2))) | |
1288 | (if (VECTOR_TYPE_P (type) | |
1289 | && TYPE_VECTOR_SUBPARTS (type) == TYPE_VECTOR_SUBPARTS (TREE_TYPE (@0)) | |
1290 | && (TYPE_MODE (TREE_TYPE (type)) | |
1291 | == TYPE_MODE (TREE_TYPE (TREE_TYPE (@0))))) | |
1292 | (plus @3 (view_convert @0)))) | |
f84e7fd6 | 1293 | |
2ee05f1e | 1294 | |
f84e7fd6 RB |
1295 | /* Simplifications of comparisons. */ |
1296 | ||
1297 | /* We can simplify a logical negation of a comparison to the | |
1298 | inverted comparison. As we cannot compute an expression | |
1299 | operator using invert_tree_comparison we have to simulate | |
1300 | that with expression code iteration. */ | |
1301 | (for cmp (tcc_comparison) | |
1302 | icmp (inverted_tcc_comparison) | |
1303 | ncmp (inverted_tcc_comparison_with_nans) | |
1304 | /* Ideally we'd like to combine the following two patterns | |
1305 | and handle some more cases by using | |
1306 | (logical_inverted_value (cmp @0 @1)) | |
1307 | here but for that genmatch would need to "inline" that. | |
1308 | For now implement what forward_propagate_comparison did. */ | |
1309 | (simplify | |
1310 | (bit_not (cmp @0 @1)) | |
1311 | (if (VECTOR_TYPE_P (type) | |
1312 | || (INTEGRAL_TYPE_P (type) && TYPE_PRECISION (type) == 1)) | |
1313 | /* Comparison inversion may be impossible for trapping math, | |
1314 | invert_tree_comparison will tell us. But we can't use | |
1315 | a computed operator in the replacement tree thus we have | |
1316 | to play the trick below. */ | |
1317 | (with { enum tree_code ic = invert_tree_comparison | |
1b457aa4 | 1318 | (cmp, HONOR_NANS (@0)); } |
f84e7fd6 RB |
1319 | (if (ic == icmp) |
1320 | (icmp @0 @1)) | |
1321 | (if (ic == ncmp) | |
1322 | (ncmp @0 @1))))) | |
1323 | (simplify | |
09240451 MG |
1324 | (bit_xor (cmp @0 @1) integer_truep) |
1325 | (with { enum tree_code ic = invert_tree_comparison | |
1b457aa4 | 1326 | (cmp, HONOR_NANS (@0)); } |
09240451 MG |
1327 | (if (ic == icmp) |
1328 | (icmp @0 @1)) | |
1329 | (if (ic == ncmp) | |
1330 | (ncmp @0 @1))))) | |
e18c1d66 | 1331 | |
2ee05f1e RB |
1332 | /* Transform comparisons of the form X - Y CMP 0 to X CMP Y. |
1333 | ??? The transformation is valid for the other operators if overflow | |
1334 | is undefined for the type, but performing it here badly interacts | |
1335 | with the transformation in fold_cond_expr_with_comparison which | |
1336 | attempts to synthetize ABS_EXPR. */ | |
1337 | (for cmp (eq ne) | |
1338 | (simplify | |
1339 | (cmp (minus @0 @1) integer_zerop) | |
1340 | (cmp @0 @1))) | |
1341 | ||
1342 | /* Transform comparisons of the form X * C1 CMP 0 to X CMP 0 in the | |
1343 | signed arithmetic case. That form is created by the compiler | |
1344 | often enough for folding it to be of value. One example is in | |
1345 | computing loop trip counts after Operator Strength Reduction. */ | |
07cdc2b8 RB |
1346 | (for cmp (simple_comparison) |
1347 | scmp (swapped_simple_comparison) | |
2ee05f1e RB |
1348 | (simplify |
1349 | (cmp (mult @0 INTEGER_CST@1) integer_zerop@2) | |
1350 | /* Handle unfolded multiplication by zero. */ | |
1351 | (if (integer_zerop (@1)) | |
1352 | (cmp @1 @2)) | |
1353 | (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0)) | |
1354 | && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))) | |
1355 | /* If @1 is negative we swap the sense of the comparison. */ | |
1356 | (if (tree_int_cst_sgn (@1) < 0) | |
1357 | (scmp @0 @2)) | |
1358 | (cmp @0 @2)))) | |
1359 | ||
1360 | /* Simplify comparison of something with itself. For IEEE | |
1361 | floating-point, we can only do some of these simplifications. */ | |
1362 | (simplify | |
1363 | (eq @0 @0) | |
1364 | (if (! FLOAT_TYPE_P (TREE_TYPE (@0)) | |
1365 | || ! HONOR_NANS (TYPE_MODE (TREE_TYPE (@0)))) | |
1366 | { constant_boolean_node (true, type); })) | |
1367 | (for cmp (ge le) | |
1368 | (simplify | |
1369 | (cmp @0 @0) | |
1370 | (eq @0 @0))) | |
1371 | (for cmp (ne gt lt) | |
1372 | (simplify | |
1373 | (cmp @0 @0) | |
1374 | (if (cmp != NE_EXPR | |
1375 | || ! FLOAT_TYPE_P (TREE_TYPE (@0)) | |
1376 | || ! HONOR_NANS (TYPE_MODE (TREE_TYPE (@0)))) | |
1377 | { constant_boolean_node (false, type); }))) | |
1378 | ||
1379 | /* Fold ~X op ~Y as Y op X. */ | |
07cdc2b8 | 1380 | (for cmp (simple_comparison) |
2ee05f1e RB |
1381 | (simplify |
1382 | (cmp (bit_not @0) (bit_not @1)) | |
1383 | (cmp @1 @0))) | |
1384 | ||
1385 | /* Fold ~X op C as X op' ~C, where op' is the swapped comparison. */ | |
07cdc2b8 RB |
1386 | (for cmp (simple_comparison) |
1387 | scmp (swapped_simple_comparison) | |
2ee05f1e RB |
1388 | (simplify |
1389 | (cmp (bit_not @0) CONSTANT_CLASS_P@1) | |
1390 | (if (TREE_CODE (@1) == INTEGER_CST || TREE_CODE (@1) == VECTOR_CST) | |
1391 | (scmp @0 (bit_not @1))))) | |
1392 | ||
07cdc2b8 RB |
1393 | (for cmp (simple_comparison) |
1394 | /* Fold (double)float1 CMP (double)float2 into float1 CMP float2. */ | |
1395 | (simplify | |
1396 | (cmp (convert@2 @0) (convert? @1)) | |
1397 | (if (FLOAT_TYPE_P (TREE_TYPE (@0)) | |
1398 | && (DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@2)) | |
1399 | == DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@0))) | |
1400 | && (DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@2)) | |
1401 | == DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@1)))) | |
1402 | (with | |
1403 | { | |
1404 | tree type1 = TREE_TYPE (@1); | |
1405 | if (TREE_CODE (@1) == REAL_CST && !DECIMAL_FLOAT_TYPE_P (type1)) | |
1406 | { | |
1407 | REAL_VALUE_TYPE orig = TREE_REAL_CST (@1); | |
1408 | if (TYPE_PRECISION (type1) > TYPE_PRECISION (float_type_node) | |
1409 | && exact_real_truncate (TYPE_MODE (float_type_node), &orig)) | |
1410 | type1 = float_type_node; | |
1411 | if (TYPE_PRECISION (type1) > TYPE_PRECISION (double_type_node) | |
1412 | && exact_real_truncate (TYPE_MODE (double_type_node), &orig)) | |
1413 | type1 = double_type_node; | |
1414 | } | |
1415 | tree newtype | |
1416 | = (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (type1) | |
1417 | ? TREE_TYPE (@0) : type1); | |
1418 | } | |
1419 | (if (TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (newtype)) | |
1420 | (cmp (convert:newtype @0) (convert:newtype @1)))))) | |
1421 | ||
1422 | (simplify | |
1423 | (cmp @0 REAL_CST@1) | |
1424 | /* IEEE doesn't distinguish +0 and -0 in comparisons. */ | |
1425 | /* a CMP (-0) -> a CMP 0 */ | |
1426 | (if (REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (@1))) | |
1427 | (cmp @0 { build_real (TREE_TYPE (@1), dconst0); })) | |
1428 | /* x != NaN is always true, other ops are always false. */ | |
1429 | (if (REAL_VALUE_ISNAN (TREE_REAL_CST (@1)) | |
1430 | && ! HONOR_SNANS (@1)) | |
1431 | { constant_boolean_node (cmp == NE_EXPR, type); }) | |
1432 | /* Fold comparisons against infinity. */ | |
1433 | (if (REAL_VALUE_ISINF (TREE_REAL_CST (@1)) | |
1434 | && MODE_HAS_INFINITIES (TYPE_MODE (TREE_TYPE (@1)))) | |
1435 | (with | |
1436 | { | |
1437 | REAL_VALUE_TYPE max; | |
1438 | enum tree_code code = cmp; | |
1439 | bool neg = REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1)); | |
1440 | if (neg) | |
1441 | code = swap_tree_comparison (code); | |
1442 | } | |
1443 | /* x > +Inf is always false, if with ignore sNANs. */ | |
1444 | (if (code == GT_EXPR | |
1445 | && ! HONOR_SNANS (@0)) | |
1446 | { constant_boolean_node (false, type); }) | |
1447 | (if (code == LE_EXPR) | |
1448 | /* x <= +Inf is always true, if we don't case about NaNs. */ | |
1449 | (if (! HONOR_NANS (@0)) | |
1450 | { constant_boolean_node (true, type); }) | |
1451 | /* x <= +Inf is the same as x == x, i.e. isfinite(x). */ | |
1452 | (eq @0 @0)) | |
1453 | /* x == +Inf and x >= +Inf are always equal to x > DBL_MAX. */ | |
1454 | (if (code == EQ_EXPR || code == GE_EXPR) | |
1455 | (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); } | |
1456 | (if (neg) | |
1457 | (lt @0 { build_real (TREE_TYPE (@0), max); })) | |
1458 | (gt @0 { build_real (TREE_TYPE (@0), max); }))) | |
1459 | /* x < +Inf is always equal to x <= DBL_MAX. */ | |
1460 | (if (code == LT_EXPR) | |
1461 | (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); } | |
1462 | (if (neg) | |
1463 | (ge @0 { build_real (TREE_TYPE (@0), max); })) | |
1464 | (le @0 { build_real (TREE_TYPE (@0), max); }))) | |
1465 | /* x != +Inf is always equal to !(x > DBL_MAX). */ | |
1466 | (if (code == NE_EXPR) | |
1467 | (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); } | |
1468 | (if (! HONOR_NANS (@0)) | |
1469 | (if (neg) | |
1470 | (ge @0 { build_real (TREE_TYPE (@0), max); })) | |
1471 | (le @0 { build_real (TREE_TYPE (@0), max); })) | |
1472 | (if (neg) | |
1473 | (bit_xor (lt @0 { build_real (TREE_TYPE (@0), max); }) | |
1474 | { build_one_cst (type); })) | |
1475 | (bit_xor (gt @0 { build_real (TREE_TYPE (@0), max); }) | |
1476 | { build_one_cst (type); })))))) | |
1477 | ||
1478 | /* If this is a comparison of a real constant with a PLUS_EXPR | |
1479 | or a MINUS_EXPR of a real constant, we can convert it into a | |
1480 | comparison with a revised real constant as long as no overflow | |
1481 | occurs when unsafe_math_optimizations are enabled. */ | |
1482 | (if (flag_unsafe_math_optimizations) | |
1483 | (for op (plus minus) | |
1484 | (simplify | |
1485 | (cmp (op @0 REAL_CST@1) REAL_CST@2) | |
1486 | (with | |
1487 | { | |
1488 | tree tem = const_binop (op == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR, | |
1489 | TREE_TYPE (@1), @2, @1); | |
1490 | } | |
1491 | (if (!TREE_OVERFLOW (tem)) | |
1492 | (cmp @0 { tem; })))))) | |
1493 | ||
1494 | /* Likewise, we can simplify a comparison of a real constant with | |
1495 | a MINUS_EXPR whose first operand is also a real constant, i.e. | |
1496 | (c1 - x) < c2 becomes x > c1-c2. Reordering is allowed on | |
1497 | floating-point types only if -fassociative-math is set. */ | |
1498 | (if (flag_associative_math) | |
1499 | (simplify | |
0409237b | 1500 | (cmp (minus REAL_CST@0 @1) REAL_CST@2) |
07cdc2b8 RB |
1501 | (with { tree tem = const_binop (MINUS_EXPR, TREE_TYPE (@1), @0, @2); } |
1502 | (if (!TREE_OVERFLOW (tem)) | |
1503 | (cmp { tem; } @1))))) | |
1504 | ||
1505 | /* Fold comparisons against built-in math functions. */ | |
1506 | (if (flag_unsafe_math_optimizations | |
1507 | && ! flag_errno_math) | |
1508 | (for sq (SQRT) | |
1509 | (simplify | |
1510 | (cmp (sq @0) REAL_CST@1) | |
1511 | (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1))) | |
1512 | /* sqrt(x) < y is always false, if y is negative. */ | |
1513 | (if (cmp == EQ_EXPR || cmp == LT_EXPR || cmp == LE_EXPR) | |
1514 | { constant_boolean_node (false, type); }) | |
1515 | /* sqrt(x) > y is always true, if y is negative and we | |
1516 | don't care about NaNs, i.e. negative values of x. */ | |
1517 | (if (cmp == NE_EXPR || !HONOR_NANS (@0)) | |
1518 | { constant_boolean_node (true, type); }) | |
1519 | /* sqrt(x) > y is the same as x >= 0, if y is negative. */ | |
1520 | (ge @0 { build_real (TREE_TYPE (@0), dconst0); })) | |
1521 | (if (cmp == GT_EXPR || cmp == GE_EXPR) | |
1522 | (with | |
1523 | { | |
1524 | REAL_VALUE_TYPE c2; | |
1525 | REAL_ARITHMETIC (c2, MULT_EXPR, TREE_REAL_CST (@1), TREE_REAL_CST (@1)); | |
1526 | real_convert (&c2, TYPE_MODE (TREE_TYPE (@0)), &c2); | |
1527 | } | |
1528 | (if (REAL_VALUE_ISINF (c2)) | |
1529 | /* sqrt(x) > y is x == +Inf, when y is very large. */ | |
1530 | (if (HONOR_INFINITIES (@0)) | |
1531 | (eq @0 { build_real (TREE_TYPE (@0), c2); })) | |
1532 | { constant_boolean_node (false, type); }) | |
1533 | /* sqrt(x) > c is the same as x > c*c. */ | |
1534 | (cmp @0 { build_real (TREE_TYPE (@0), c2); }))) | |
1535 | (if (cmp == LT_EXPR || cmp == LE_EXPR) | |
1536 | (with | |
1537 | { | |
1538 | REAL_VALUE_TYPE c2; | |
1539 | REAL_ARITHMETIC (c2, MULT_EXPR, TREE_REAL_CST (@1), TREE_REAL_CST (@1)); | |
1540 | real_convert (&c2, TYPE_MODE (TREE_TYPE (@0)), &c2); | |
1541 | } | |
1542 | (if (REAL_VALUE_ISINF (c2)) | |
1543 | /* sqrt(x) < y is always true, when y is a very large | |
1544 | value and we don't care about NaNs or Infinities. */ | |
1545 | (if (! HONOR_NANS (@0) && ! HONOR_INFINITIES (@0)) | |
1546 | { constant_boolean_node (true, type); }) | |
1547 | /* sqrt(x) < y is x != +Inf when y is very large and we | |
1548 | don't care about NaNs. */ | |
1549 | (if (! HONOR_NANS (@0)) | |
1550 | (ne @0 { build_real (TREE_TYPE (@0), c2); })) | |
1551 | /* sqrt(x) < y is x >= 0 when y is very large and we | |
1552 | don't care about Infinities. */ | |
1553 | (if (! HONOR_INFINITIES (@0)) | |
1554 | (ge @0 { build_real (TREE_TYPE (@0), dconst0); })) | |
1555 | /* sqrt(x) < y is x >= 0 && x != +Inf, when y is large. */ | |
1556 | (if (GENERIC) | |
1557 | (truth_andif | |
1558 | (ge @0 { build_real (TREE_TYPE (@0), dconst0); }) | |
1559 | (ne @0 { build_real (TREE_TYPE (@0), c2); })))) | |
1560 | /* sqrt(x) < c is the same as x < c*c, if we ignore NaNs. */ | |
1561 | (if (! REAL_VALUE_ISINF (c2) | |
1562 | && ! HONOR_NANS (@0)) | |
1563 | (cmp @0 { build_real (TREE_TYPE (@0), c2); })) | |
1564 | /* sqrt(x) < c is the same as x >= 0 && x < c*c. */ | |
1565 | (if (! REAL_VALUE_ISINF (c2) | |
1566 | && GENERIC) | |
1567 | (truth_andif | |
1568 | (ge @0 { build_real (TREE_TYPE (@0), dconst0); }) | |
1569 | (cmp @0 { build_real (TREE_TYPE (@0), c2); }))))))))) | |
2ee05f1e | 1570 | |
cfdc4f33 MG |
1571 | /* Unordered tests if either argument is a NaN. */ |
1572 | (simplify | |
1573 | (bit_ior (unordered @0 @0) (unordered @1 @1)) | |
aea417d7 | 1574 | (if (types_match (@0, @1)) |
cfdc4f33 | 1575 | (unordered @0 @1))) |
257b01ba MG |
1576 | (simplify |
1577 | (bit_and (ordered @0 @0) (ordered @1 @1)) | |
1578 | (if (types_match (@0, @1)) | |
1579 | (ordered @0 @1))) | |
cfdc4f33 MG |
1580 | (simplify |
1581 | (bit_ior:c (unordered @0 @0) (unordered:c@2 @0 @1)) | |
1582 | @2) | |
257b01ba MG |
1583 | (simplify |
1584 | (bit_and:c (ordered @0 @0) (ordered:c@2 @0 @1)) | |
1585 | @2) | |
e18c1d66 | 1586 | |
534bd33b MG |
1587 | /* -A CMP -B -> B CMP A. */ |
1588 | (for cmp (tcc_comparison) | |
1589 | scmp (swapped_tcc_comparison) | |
1590 | (simplify | |
1591 | (cmp (negate @0) (negate @1)) | |
1592 | (if (FLOAT_TYPE_P (TREE_TYPE (@0)) | |
1593 | || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0)) | |
1594 | && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))) | |
1595 | (scmp @0 @1))) | |
1596 | (simplify | |
1597 | (cmp (negate @0) CONSTANT_CLASS_P@1) | |
1598 | (if (FLOAT_TYPE_P (TREE_TYPE (@0)) | |
1599 | || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0)) | |
1600 | && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))) | |
1601 | (with { tree tem = fold_unary (NEGATE_EXPR, TREE_TYPE (@0), @1); } | |
1602 | (if (tem && !TREE_OVERFLOW (tem)) | |
1603 | (scmp @0 { tem; })))))) | |
1604 | ||
66e1cacf RB |
1605 | |
1606 | /* Equality compare simplifications from fold_binary */ | |
1607 | (for cmp (eq ne) | |
1608 | ||
1609 | /* If we have (A | C) == D where C & ~D != 0, convert this into 0. | |
1610 | Similarly for NE_EXPR. */ | |
1611 | (simplify | |
1612 | (cmp (convert?@3 (bit_ior @0 INTEGER_CST@1)) INTEGER_CST@2) | |
1613 | (if (tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@0)) | |
1614 | && wi::bit_and_not (@1, @2) != 0) | |
1615 | { constant_boolean_node (cmp == NE_EXPR, type); })) | |
1616 | ||
1617 | /* (X ^ Y) == 0 becomes X == Y, and (X ^ Y) != 0 becomes X != Y. */ | |
1618 | (simplify | |
1619 | (cmp (bit_xor @0 @1) integer_zerop) | |
1620 | (cmp @0 @1)) | |
1621 | ||
1622 | /* (X ^ Y) == Y becomes X == 0. | |
1623 | Likewise (X ^ Y) == X becomes Y == 0. */ | |
1624 | (simplify | |
99e943a2 | 1625 | (cmp:c (bit_xor:c @0 @1) @0) |
66e1cacf RB |
1626 | (cmp @1 { build_zero_cst (TREE_TYPE (@1)); })) |
1627 | ||
1628 | /* (X ^ C1) op C2 can be rewritten as X op (C1 ^ C2). */ | |
1629 | (simplify | |
1630 | (cmp (convert?@3 (bit_xor @0 INTEGER_CST@1)) INTEGER_CST@2) | |
1631 | (if (tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@0))) | |
1632 | (cmp @0 (bit_xor @1 (convert @2)))))) | |
1633 | ||
e18c1d66 RB |
1634 | /* Simplification of math builtins. */ |
1635 | ||
e18c1d66 RB |
1636 | /* fold_builtin_logarithm */ |
1637 | (if (flag_unsafe_math_optimizations) | |
1638 | /* Special case, optimize logN(expN(x)) = x. */ | |
1639 | (for logs (LOG LOG2 LOG10) | |
1640 | exps (EXP EXP2 EXP10) | |
1641 | (simplify | |
1642 | (logs (exps @0)) | |
1643 | @0)) | |
1644 | /* Optimize logN(func()) for various exponential functions. We | |
1645 | want to determine the value "x" and the power "exponent" in | |
1646 | order to transform logN(x**exponent) into exponent*logN(x). */ | |
1647 | (for logs (LOG LOG LOG LOG | |
1648 | LOG2 LOG2 LOG2 LOG2 | |
1649 | LOG10 LOG10 LOG10 LOG10) | |
1650 | exps (EXP EXP2 EXP10 POW10) | |
1651 | (simplify | |
1652 | (logs (exps @0)) | |
1653 | (with { | |
1654 | tree x; | |
1655 | switch (exps) | |
1656 | { | |
1657 | CASE_FLT_FN (BUILT_IN_EXP): | |
1658 | /* Prepare to do logN(exp(exponent) -> exponent*logN(e). */ | |
1659 | x = build_real (type, real_value_truncate (TYPE_MODE (type), | |
1660 | dconst_e ())); | |
1661 | break; | |
1662 | CASE_FLT_FN (BUILT_IN_EXP2): | |
1663 | /* Prepare to do logN(exp2(exponent) -> exponent*logN(2). */ | |
1664 | x = build_real (type, dconst2); | |
1665 | break; | |
1666 | CASE_FLT_FN (BUILT_IN_EXP10): | |
1667 | CASE_FLT_FN (BUILT_IN_POW10): | |
1668 | /* Prepare to do logN(exp10(exponent) -> exponent*logN(10). */ | |
1669 | { | |
1670 | REAL_VALUE_TYPE dconst10; | |
1671 | real_from_integer (&dconst10, VOIDmode, 10, SIGNED); | |
1672 | x = build_real (type, dconst10); | |
1673 | } | |
1674 | break; | |
1675 | } | |
1676 | } | |
1677 | (mult (logs { x; }) @0)))) | |
1678 | (for logs (LOG LOG | |
1679 | LOG2 LOG2 | |
1680 | LOG10 LOG10) | |
1681 | exps (SQRT CBRT) | |
1682 | (simplify | |
1683 | (logs (exps @0)) | |
1684 | (with { | |
1685 | tree x; | |
1686 | switch (exps) | |
1687 | { | |
1688 | CASE_FLT_FN (BUILT_IN_SQRT): | |
1689 | /* Prepare to do logN(sqrt(x) -> 0.5*logN(x). */ | |
1690 | x = build_real (type, dconsthalf); | |
1691 | break; | |
1692 | CASE_FLT_FN (BUILT_IN_CBRT): | |
1693 | /* Prepare to do logN(cbrt(x) -> (1/3)*logN(x). */ | |
1694 | x = build_real (type, real_value_truncate (TYPE_MODE (type), | |
1695 | dconst_third ())); | |
1696 | break; | |
1697 | } | |
1698 | } | |
1699 | (mult { x; } (logs @0))))) | |
1700 | /* logN(pow(x,exponent) -> exponent*logN(x). */ | |
1701 | (for logs (LOG LOG2 LOG10) | |
1702 | pows (POW) | |
1703 | (simplify | |
1704 | (logs (pows @0 @1)) | |
1705 | (mult @1 (logs @0))))) | |
1706 | ||
be144838 JL |
1707 | /* Narrowing of arithmetic and logical operations. |
1708 | ||
1709 | These are conceptually similar to the transformations performed for | |
1710 | the C/C++ front-ends by shorten_binary_op and shorten_compare. Long | |
1711 | term we want to move all that code out of the front-ends into here. */ | |
1712 | ||
1713 | /* If we have a narrowing conversion of an arithmetic operation where | |
1714 | both operands are widening conversions from the same type as the outer | |
1715 | narrowing conversion. Then convert the innermost operands to a suitable | |
1716 | unsigned type (to avoid introducing undefined behaviour), perform the | |
1717 | operation and convert the result to the desired type. */ | |
1718 | (for op (plus minus) | |
1719 | (simplify | |
48451e8f | 1720 | (convert (op@4 (convert@2 @0) (convert@3 @1))) |
be144838 JL |
1721 | (if (INTEGRAL_TYPE_P (type) |
1722 | /* We check for type compatibility between @0 and @1 below, | |
1723 | so there's no need to check that @1/@3 are integral types. */ | |
1724 | && INTEGRAL_TYPE_P (TREE_TYPE (@0)) | |
1725 | && INTEGRAL_TYPE_P (TREE_TYPE (@2)) | |
1726 | /* The precision of the type of each operand must match the | |
1727 | precision of the mode of each operand, similarly for the | |
1728 | result. */ | |
1729 | && (TYPE_PRECISION (TREE_TYPE (@0)) | |
1730 | == GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (@0)))) | |
1731 | && (TYPE_PRECISION (TREE_TYPE (@1)) | |
1732 | == GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (@1)))) | |
1733 | && TYPE_PRECISION (type) == GET_MODE_PRECISION (TYPE_MODE (type)) | |
1734 | /* The inner conversion must be a widening conversion. */ | |
1735 | && TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (TREE_TYPE (@0)) | |
aea417d7 MG |
1736 | && types_match (@0, @1) |
1737 | && types_match (@0, type) | |
48451e8f | 1738 | && single_use (@4)) |
be144838 JL |
1739 | (if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))) |
1740 | (convert (op @0 @1))) | |
1741 | (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); } | |
1742 | (convert (op (convert:utype @0) (convert:utype @1))))))) | |
48451e8f JL |
1743 | |
1744 | /* This is another case of narrowing, specifically when there's an outer | |
1745 | BIT_AND_EXPR which masks off bits outside the type of the innermost | |
1746 | operands. Like the previous case we have to convert the operands | |
1747 | to unsigned types to avoid introducing undefined behaviour for the | |
1748 | arithmetic operation. */ | |
1749 | (for op (minus plus) | |
1750 | (simplify | |
1751 | (bit_and (op@5 (convert@2 @0) (convert@3 @1)) INTEGER_CST@4) | |
1752 | (if (INTEGRAL_TYPE_P (type) | |
1753 | /* We check for type compatibility between @0 and @1 below, | |
1754 | so there's no need to check that @1/@3 are integral types. */ | |
1755 | && INTEGRAL_TYPE_P (TREE_TYPE (@0)) | |
1756 | && INTEGRAL_TYPE_P (TREE_TYPE (@2)) | |
1757 | /* The precision of the type of each operand must match the | |
1758 | precision of the mode of each operand, similarly for the | |
1759 | result. */ | |
1760 | && (TYPE_PRECISION (TREE_TYPE (@0)) | |
1761 | == GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (@0)))) | |
1762 | && (TYPE_PRECISION (TREE_TYPE (@1)) | |
1763 | == GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (@1)))) | |
1764 | && TYPE_PRECISION (type) == GET_MODE_PRECISION (TYPE_MODE (type)) | |
1765 | /* The inner conversion must be a widening conversion. */ | |
1766 | && TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (TREE_TYPE (@0)) | |
aea417d7 | 1767 | && types_match (@0, @1) |
a60c51fe | 1768 | && (tree_int_cst_min_precision (@4, TYPE_SIGN (TREE_TYPE (@0))) |
48451e8f | 1769 | <= TYPE_PRECISION (TREE_TYPE (@0))) |
a60c51fe JJ |
1770 | && (TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)) |
1771 | || tree_int_cst_sgn (@4) >= 0) | |
48451e8f JL |
1772 | && single_use (@5)) |
1773 | (if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))) | |
1774 | (with { tree ntype = TREE_TYPE (@0); } | |
1775 | (convert (bit_and (op @0 @1) (convert:ntype @4))))) | |
1776 | (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); } | |
1777 | (convert (bit_and (op (convert:utype @0) (convert:utype @1)) | |
1778 | (convert:utype @4))))))) | |
1779 |