]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/match.pd
gimplify.c (gimplify_function_tree): Do not move the outer binds block.
[thirdparty/gcc.git] / gcc / match.pd
CommitLineData
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
818ab71a 5 Copyright (C) 2014-2016 Free Software Foundation, Inc.
3d2cf79f
RB
6 Contributed by Richard Biener <rguenther@suse.de>
7 and Prathamesh Kulkarni <bilbotheelffriend@gmail.com>
8
9This file is part of GCC.
10
11GCC is free software; you can redistribute it and/or modify it under
12the terms of the GNU General Public License as published by the Free
13Software Foundation; either version 3, or (at your option) any later
14version.
15
16GCC is distributed in the hope that it will be useful, but WITHOUT ANY
17WARRANTY; without even the implied warranty of MERCHANTABILITY or
18FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19for more details.
20
21You should have received a copy of the GNU General Public License
22along 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
53a19317 29 integer_each_onep integer_truep integer_nonzerop
cc7b5acf 30 real_zerop real_onep real_minus_onep
b0eb889b 31 zerop
f3582e54 32 CONSTANT_CLASS_P
887ab609 33 tree_expr_nonnegative_p
67dbe582 34 integer_valued_real_p
53a19317
RB
35 integer_pow2p
36 HONOR_NANS)
e0ee10ed 37
f84e7fd6
RB
38/* Operator lists. */
39(define_operator_list tcc_comparison
40 lt le eq ne ge gt unordered ordered unlt unle ungt unge uneq ltgt)
41(define_operator_list inverted_tcc_comparison
42 ge gt ne eq lt le ordered unordered ge gt le lt ltgt uneq)
43(define_operator_list inverted_tcc_comparison_with_nans
44 unge ungt ne eq unlt unle ordered unordered ge gt le lt ltgt uneq)
534bd33b
MG
45(define_operator_list swapped_tcc_comparison
46 gt ge eq ne le lt unordered ordered ungt unge unlt unle uneq ltgt)
07cdc2b8
RB
47(define_operator_list simple_comparison lt le eq ne ge gt)
48(define_operator_list swapped_simple_comparison gt ge eq ne le lt)
49
b1dc4a20 50#include "cfn-operators.pd"
257aecb4 51
543a9bcd
RS
52/* Define operand lists for math rounding functions {,i,l,ll}FN,
53 where the versions prefixed with "i" return an int, those prefixed with
54 "l" return a long and those prefixed with "ll" return a long long.
55
56 Also define operand lists:
57
58 X<FN>F for all float functions, in the order i, l, ll
59 X<FN> for all double functions, in the same order
60 X<FN>L for all long double functions, in the same order. */
61#define DEFINE_INT_AND_FLOAT_ROUND_FN(FN) \
543a9bcd
RS
62 (define_operator_list X##FN##F BUILT_IN_I##FN##F \
63 BUILT_IN_L##FN##F \
64 BUILT_IN_LL##FN##F) \
65 (define_operator_list X##FN BUILT_IN_I##FN \
66 BUILT_IN_L##FN \
67 BUILT_IN_LL##FN) \
68 (define_operator_list X##FN##L BUILT_IN_I##FN##L \
69 BUILT_IN_L##FN##L \
70 BUILT_IN_LL##FN##L)
71
543a9bcd
RS
72DEFINE_INT_AND_FLOAT_ROUND_FN (FLOOR)
73DEFINE_INT_AND_FLOAT_ROUND_FN (CEIL)
74DEFINE_INT_AND_FLOAT_ROUND_FN (ROUND)
75DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
f84e7fd6 76
e0ee10ed 77/* Simplifications of operations with one constant operand and
36a60e48 78 simplifications to constants or single values. */
e0ee10ed
RB
79
80(for op (plus pointer_plus minus bit_ior bit_xor)
81 (simplify
82 (op @0 integer_zerop)
83 (non_lvalue @0)))
84
a499aac5
RB
85/* 0 +p index -> (type)index */
86(simplify
87 (pointer_plus integer_zerop @1)
88 (non_lvalue (convert @1)))
89
a7f24614
RB
90/* See if ARG1 is zero and X + ARG1 reduces to X.
91 Likewise if the operands are reversed. */
92(simplify
93 (plus:c @0 real_zerop@1)
94 (if (fold_real_zero_addition_p (type, @1, 0))
95 (non_lvalue @0)))
96
97/* See if ARG1 is zero and X - ARG1 reduces to X. */
98(simplify
99 (minus @0 real_zerop@1)
100 (if (fold_real_zero_addition_p (type, @1, 1))
101 (non_lvalue @0)))
102
e0ee10ed
RB
103/* Simplify x - x.
104 This is unsafe for certain floats even in non-IEEE formats.
105 In IEEE, it is unsafe because it does wrong for NaNs.
106 Also note that operand_equal_p is always false if an operand
107 is volatile. */
108(simplify
a7f24614 109 (minus @0 @0)
1b457aa4 110 (if (!FLOAT_TYPE_P (type) || !HONOR_NANS (type))
a7f24614 111 { build_zero_cst (type); }))
e0ee10ed
RB
112
113(simplify
a7f24614
RB
114 (mult @0 integer_zerop@1)
115 @1)
116
117/* Maybe fold x * 0 to 0. The expressions aren't the same
118 when x is NaN, since x * 0 is also NaN. Nor are they the
119 same in modes with signed zeros, since multiplying a
120 negative value by 0 gives -0, not +0. */
121(simplify
122 (mult @0 real_zerop@1)
8b5ee871 123 (if (!HONOR_NANS (type) && !HONOR_SIGNED_ZEROS (type))
a7f24614
RB
124 @1))
125
126/* In IEEE floating point, x*1 is not equivalent to x for snans.
127 Likewise for complex arithmetic with signed zeros. */
128(simplify
129 (mult @0 real_onep)
8b5ee871
MG
130 (if (!HONOR_SNANS (type)
131 && (!HONOR_SIGNED_ZEROS (type)
a7f24614
RB
132 || !COMPLEX_FLOAT_TYPE_P (type)))
133 (non_lvalue @0)))
134
135/* Transform x * -1.0 into -x. */
136(simplify
137 (mult @0 real_minus_onep)
8b5ee871
MG
138 (if (!HONOR_SNANS (type)
139 && (!HONOR_SIGNED_ZEROS (type)
a7f24614
RB
140 || !COMPLEX_FLOAT_TYPE_P (type)))
141 (negate @0)))
e0ee10ed
RB
142
143/* Make sure to preserve divisions by zero. This is the reason why
144 we don't simplify x / x to 1 or 0 / x to 0. */
145(for op (mult trunc_div ceil_div floor_div round_div exact_div)
146 (simplify
147 (op @0 integer_onep)
148 (non_lvalue @0)))
149
a7f24614 150(for div (trunc_div ceil_div floor_div round_div exact_div)
da186c1f 151 /* X / -1 is -X. */
a7f24614 152 (simplify
09240451
MG
153 (div @0 integer_minus_onep@1)
154 (if (!TYPE_UNSIGNED (type))
da186c1f
RB
155 (negate @0)))
156 /* X / abs (X) is X < 0 ? -1 : 1. */
157 (simplify
d96a5585
RB
158 (div:C @0 (abs @0))
159 (if (INTEGRAL_TYPE_P (type)
da186c1f
RB
160 && TYPE_OVERFLOW_UNDEFINED (type))
161 (cond (lt @0 { build_zero_cst (type); })
162 { build_minus_one_cst (type); } { build_one_cst (type); })))
163 /* X / -X is -1. */
164 (simplify
d96a5585 165 (div:C @0 (negate @0))
da186c1f
RB
166 (if ((INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type))
167 && TYPE_OVERFLOW_UNDEFINED (type))
168 { build_minus_one_cst (type); })))
a7f24614
RB
169
170/* For unsigned integral types, FLOOR_DIV_EXPR is the same as
171 TRUNC_DIV_EXPR. Rewrite into the latter in this case. */
172(simplify
173 (floor_div @0 @1)
09240451
MG
174 (if ((INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type))
175 && TYPE_UNSIGNED (type))
a7f24614
RB
176 (trunc_div @0 @1)))
177
28093105
RB
178/* Combine two successive divisions. Note that combining ceil_div
179 and floor_div is trickier and combining round_div even more so. */
180(for div (trunc_div exact_div)
c306cfaf
RB
181 (simplify
182 (div (div @0 INTEGER_CST@1) INTEGER_CST@2)
183 (with {
184 bool overflow_p;
185 wide_int mul = wi::mul (@1, @2, TYPE_SIGN (type), &overflow_p);
186 }
187 (if (!overflow_p)
8fdc6c67
RB
188 (div @0 { wide_int_to_tree (type, mul); })
189 (if (TYPE_UNSIGNED (type)
190 || mul != wi::min_value (TYPE_PRECISION (type), SIGNED))
191 { build_zero_cst (type); })))))
c306cfaf 192
a7f24614 193/* Optimize A / A to 1.0 if we don't care about
09240451 194 NaNs or Infinities. */
a7f24614
RB
195(simplify
196 (rdiv @0 @0)
09240451 197 (if (FLOAT_TYPE_P (type)
1b457aa4 198 && ! HONOR_NANS (type)
8b5ee871 199 && ! HONOR_INFINITIES (type))
09240451
MG
200 { build_one_cst (type); }))
201
202/* Optimize -A / A to -1.0 if we don't care about
203 NaNs or Infinities. */
204(simplify
e04d2a35 205 (rdiv:C @0 (negate @0))
09240451 206 (if (FLOAT_TYPE_P (type)
1b457aa4 207 && ! HONOR_NANS (type)
8b5ee871 208 && ! HONOR_INFINITIES (type))
09240451 209 { build_minus_one_cst (type); }))
a7f24614 210
8c6961ca
PK
211/* PR71078: x / abs(x) -> copysign (1.0, x) */
212(simplify
213 (rdiv:C (convert? @0) (convert? (abs @0)))
214 (if (SCALAR_FLOAT_TYPE_P (type)
215 && ! HONOR_NANS (type)
216 && ! HONOR_INFINITIES (type))
217 (switch
218 (if (types_match (type, float_type_node))
219 (BUILT_IN_COPYSIGNF { build_one_cst (type); } (convert @0)))
220 (if (types_match (type, double_type_node))
221 (BUILT_IN_COPYSIGN { build_one_cst (type); } (convert @0)))
222 (if (types_match (type, long_double_type_node))
223 (BUILT_IN_COPYSIGNL { build_one_cst (type); } (convert @0))))))
224
a7f24614
RB
225/* In IEEE floating point, x/1 is not equivalent to x for snans. */
226(simplify
227 (rdiv @0 real_onep)
8b5ee871 228 (if (!HONOR_SNANS (type))
a7f24614
RB
229 (non_lvalue @0)))
230
231/* In IEEE floating point, x/-1 is not equivalent to -x for snans. */
232(simplify
233 (rdiv @0 real_minus_onep)
8b5ee871 234 (if (!HONOR_SNANS (type))
a7f24614
RB
235 (negate @0)))
236
5711ac88
N
237(if (flag_reciprocal_math)
238 /* Convert (A/B)/C to A/(B*C) */
239 (simplify
240 (rdiv (rdiv:s @0 @1) @2)
241 (rdiv @0 (mult @1 @2)))
242
243 /* Convert A/(B/C) to (A/B)*C */
244 (simplify
245 (rdiv @0 (rdiv:s @1 @2))
246 (mult (rdiv @0 @1) @2)))
247
248/* Optimize (X & (-A)) / A where A is a power of 2, to X >> log2(A) */
249(for div (trunc_div ceil_div floor_div round_div exact_div)
250 (simplify
251 (div (convert? (bit_and @0 INTEGER_CST@1)) INTEGER_CST@2)
252 (if (integer_pow2p (@2)
253 && tree_int_cst_sgn (@2) > 0
254 && wi::add (@2, @1) == 0
255 && tree_nop_conversion_p (type, TREE_TYPE (@0)))
256 (rshift (convert @0) { build_int_cst (integer_type_node,
257 wi::exact_log2 (@2)); }))))
258
a7f24614
RB
259/* If ARG1 is a constant, we can convert this to a multiply by the
260 reciprocal. This does not have the same rounding properties,
261 so only do this if -freciprocal-math. We can actually
262 always safely do it if ARG1 is a power of two, but it's hard to
263 tell if it is or not in a portable manner. */
264(for cst (REAL_CST COMPLEX_CST VECTOR_CST)
265 (simplify
266 (rdiv @0 cst@1)
267 (if (optimize)
53bc4b3a
RB
268 (if (flag_reciprocal_math
269 && !real_zerop (@1))
a7f24614 270 (with
249700b5 271 { tree tem = const_binop (RDIV_EXPR, type, build_one_cst (type), @1); }
a7f24614 272 (if (tem)
8fdc6c67
RB
273 (mult @0 { tem; } )))
274 (if (cst != COMPLEX_CST)
275 (with { tree inverse = exact_inverse (type, @1); }
276 (if (inverse)
277 (mult @0 { inverse; } ))))))))
a7f24614 278
e0ee10ed
RB
279/* Same applies to modulo operations, but fold is inconsistent here
280 and simplifies 0 % x to 0, only preserving literal 0 % 0. */
a7f24614 281(for mod (ceil_mod floor_mod round_mod trunc_mod)
e0ee10ed
RB
282 /* 0 % X is always zero. */
283 (simplify
a7f24614 284 (mod integer_zerop@0 @1)
e0ee10ed
RB
285 /* But not for 0 % 0 so that we can get the proper warnings and errors. */
286 (if (!integer_zerop (@1))
287 @0))
288 /* X % 1 is always zero. */
289 (simplify
a7f24614
RB
290 (mod @0 integer_onep)
291 { build_zero_cst (type); })
292 /* X % -1 is zero. */
293 (simplify
09240451
MG
294 (mod @0 integer_minus_onep@1)
295 (if (!TYPE_UNSIGNED (type))
bc4315fb
MG
296 { build_zero_cst (type); }))
297 /* (X % Y) % Y is just X % Y. */
298 (simplify
299 (mod (mod@2 @0 @1) @1)
98e30e51
RB
300 @2)
301 /* From extract_muldiv_1: (X * C1) % C2 is zero if C1 is a multiple of C2. */
302 (simplify
303 (mod (mult @0 INTEGER_CST@1) INTEGER_CST@2)
304 (if (ANY_INTEGRAL_TYPE_P (type)
305 && TYPE_OVERFLOW_UNDEFINED (type)
306 && wi::multiple_of_p (@1, @2, TYPE_SIGN (type)))
307 { build_zero_cst (type); })))
a7f24614
RB
308
309/* X % -C is the same as X % C. */
310(simplify
311 (trunc_mod @0 INTEGER_CST@1)
312 (if (TYPE_SIGN (type) == SIGNED
313 && !TREE_OVERFLOW (@1)
314 && wi::neg_p (@1)
315 && !TYPE_OVERFLOW_TRAPS (type)
316 /* Avoid this transformation if C is INT_MIN, i.e. C == -C. */
317 && !sign_bit_p (@1, @1))
318 (trunc_mod @0 (negate @1))))
e0ee10ed 319
8f0c696a
RB
320/* X % -Y is the same as X % Y. */
321(simplify
322 (trunc_mod @0 (convert? (negate @1)))
a2a743a1
MP
323 (if (INTEGRAL_TYPE_P (type)
324 && !TYPE_UNSIGNED (type)
8f0c696a 325 && !TYPE_OVERFLOW_TRAPS (type)
20b8d734
JJ
326 && tree_nop_conversion_p (type, TREE_TYPE (@1))
327 /* Avoid this transformation if X might be INT_MIN or
328 Y might be -1, because we would then change valid
329 INT_MIN % -(-1) into invalid INT_MIN % -1. */
330 && (expr_not_equal_to (@0, TYPE_MIN_VALUE (type))
331 || expr_not_equal_to (@1, wi::minus_one (TYPE_PRECISION
332 (TREE_TYPE (@1))))))
8f0c696a
RB
333 (trunc_mod @0 (convert @1))))
334
f461569a
MP
335/* X - (X / Y) * Y is the same as X % Y. */
336(simplify
2eef1fc1
RB
337 (minus (convert1? @0) (convert2? (mult:c (trunc_div @@0 @@1) @1)))
338 (if (INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type))
fba46f03 339 (convert (trunc_mod @0 @1))))
f461569a 340
8f0c696a
RB
341/* Optimize TRUNC_MOD_EXPR by a power of two into a BIT_AND_EXPR,
342 i.e. "X % C" into "X & (C - 1)", if X and C are positive.
343 Also optimize A % (C << N) where C is a power of 2,
344 to A & ((C << N) - 1). */
345(match (power_of_two_cand @1)
346 INTEGER_CST@1)
347(match (power_of_two_cand @1)
348 (lshift INTEGER_CST@1 @2))
349(for mod (trunc_mod floor_mod)
350 (simplify
4ab1e111 351 (mod @0 (convert?@3 (power_of_two_cand@1 @2)))
8f0c696a
RB
352 (if ((TYPE_UNSIGNED (type)
353 || tree_expr_nonnegative_p (@0))
4ab1e111 354 && tree_nop_conversion_p (type, TREE_TYPE (@3))
8f0c696a 355 && integer_pow2p (@2) && tree_int_cst_sgn (@2) > 0)
4ab1e111 356 (bit_and @0 (convert (minus @1 { build_int_cst (TREE_TYPE (@1), 1); }))))))
8f0c696a 357
887ab609
N
358/* Simplify (unsigned t * 2)/2 -> unsigned t & 0x7FFFFFFF. */
359(simplify
360 (trunc_div (mult @0 integer_pow2p@1) @1)
361 (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
362 (bit_and @0 { wide_int_to_tree
363 (type, wi::mask (TYPE_PRECISION (type) - wi::exact_log2 (@1),
364 false, TYPE_PRECISION (type))); })))
365
5f8d832e
N
366/* Simplify (unsigned t / 2) * 2 -> unsigned t & ~1. */
367(simplify
368 (mult (trunc_div @0 integer_pow2p@1) @1)
369 (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
370 (bit_and @0 (negate @1))))
371
95765f36
N
372/* Simplify (t * 2) / 2) -> t. */
373(for div (trunc_div ceil_div floor_div round_div exact_div)
374 (simplify
375 (div (mult @0 @1) @1)
376 (if (ANY_INTEGRAL_TYPE_P (type)
377 && TYPE_OVERFLOW_UNDEFINED (type))
378 @0)))
379
d202f9bd 380(for op (negate abs)
9b054b08
RS
381 /* Simplify cos(-x) and cos(|x|) -> cos(x). Similarly for cosh. */
382 (for coss (COS COSH)
383 (simplify
384 (coss (op @0))
385 (coss @0)))
386 /* Simplify pow(-x, y) and pow(|x|,y) -> pow(x,y) if y is an even integer. */
387 (for pows (POW)
388 (simplify
389 (pows (op @0) REAL_CST@1)
390 (with { HOST_WIDE_INT n; }
391 (if (real_isinteger (&TREE_REAL_CST (@1), &n) && (n & 1) == 0)
5d3498b4 392 (pows @0 @1)))))
de3fbea3
RB
393 /* Likewise for powi. */
394 (for pows (POWI)
395 (simplify
396 (pows (op @0) INTEGER_CST@1)
397 (if (wi::bit_and (@1, 1) == 0)
398 (pows @0 @1))))
5d3498b4
RS
399 /* Strip negate and abs from both operands of hypot. */
400 (for hypots (HYPOT)
401 (simplify
402 (hypots (op @0) @1)
403 (hypots @0 @1))
404 (simplify
405 (hypots @0 (op @1))
406 (hypots @0 @1)))
407 /* copysign(-x, y) and copysign(abs(x), y) -> copysign(x, y). */
408 (for copysigns (COPYSIGN)
409 (simplify
410 (copysigns (op @0) @1)
411 (copysigns @0 @1))))
412
413/* abs(x)*abs(x) -> x*x. Should be valid for all types. */
414(simplify
415 (mult (abs@1 @0) @1)
416 (mult @0 @0))
417
418/* cos(copysign(x, y)) -> cos(x). Similarly for cosh. */
419(for coss (COS COSH)
420 copysigns (COPYSIGN)
421 (simplify
422 (coss (copysigns @0 @1))
423 (coss @0)))
424
425/* pow(copysign(x, y), z) -> pow(x, z) if z is an even integer. */
426(for pows (POW)
427 copysigns (COPYSIGN)
428 (simplify
de3fbea3 429 (pows (copysigns @0 @2) REAL_CST@1)
5d3498b4
RS
430 (with { HOST_WIDE_INT n; }
431 (if (real_isinteger (&TREE_REAL_CST (@1), &n) && (n & 1) == 0)
432 (pows @0 @1)))))
de3fbea3
RB
433/* Likewise for powi. */
434(for pows (POWI)
435 copysigns (COPYSIGN)
436 (simplify
437 (pows (copysigns @0 @2) INTEGER_CST@1)
438 (if (wi::bit_and (@1, 1) == 0)
439 (pows @0 @1))))
5d3498b4
RS
440
441(for hypots (HYPOT)
442 copysigns (COPYSIGN)
443 /* hypot(copysign(x, y), z) -> hypot(x, z). */
444 (simplify
445 (hypots (copysigns @0 @1) @2)
446 (hypots @0 @2))
447 /* hypot(x, copysign(y, z)) -> hypot(x, y). */
448 (simplify
449 (hypots @0 (copysigns @1 @2))
450 (hypots @0 @1)))
451
eeb57981
RB
452/* copysign(x, CST) -> [-]abs (x). */
453(for copysigns (COPYSIGN)
454 (simplify
455 (copysigns @0 REAL_CST@1)
456 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1)))
457 (negate (abs @0))
458 (abs @0))))
459
5d3498b4
RS
460/* copysign(copysign(x, y), z) -> copysign(x, z). */
461(for copysigns (COPYSIGN)
462 (simplify
463 (copysigns (copysigns @0 @1) @2)
464 (copysigns @0 @2)))
465
466/* copysign(x,y)*copysign(x,y) -> x*x. */
467(for copysigns (COPYSIGN)
468 (simplify
469 (mult (copysigns@2 @0 @1) @2)
470 (mult @0 @0)))
471
472/* ccos(-x) -> ccos(x). Similarly for ccosh. */
473(for ccoss (CCOS CCOSH)
474 (simplify
475 (ccoss (negate @0))
476 (ccoss @0)))
d202f9bd 477
abcc43f5
RS
478/* cabs(-x) and cos(conj(x)) -> cabs(x). */
479(for ops (conj negate)
480 (for cabss (CABS)
481 (simplify
482 (cabss (ops @0))
483 (cabss @0))))
484
0a8f32b8
RB
485/* Fold (a * (1 << b)) into (a << b) */
486(simplify
487 (mult:c @0 (convert? (lshift integer_onep@1 @2)))
488 (if (! FLOAT_TYPE_P (type)
9ff6fb6e 489 && tree_nop_conversion_p (type, TREE_TYPE (@1)))
0a8f32b8
RB
490 (lshift @0 @2)))
491
492/* Fold (C1/X)*C2 into (C1*C2)/X. */
493(simplify
ff86345f
RB
494 (mult (rdiv@3 REAL_CST@0 @1) REAL_CST@2)
495 (if (flag_associative_math
496 && single_use (@3))
0a8f32b8
RB
497 (with
498 { tree tem = const_binop (MULT_EXPR, type, @0, @2); }
499 (if (tem)
500 (rdiv { tem; } @1)))))
501
5711ac88
N
502/* Convert C1/(X*C2) into (C1/C2)/X */
503(simplify
504 (rdiv REAL_CST@0 (mult @1 REAL_CST@2))
505 (if (flag_reciprocal_math)
506 (with
507 { tree tem = const_binop (RDIV_EXPR, type, @0, @2); }
508 (if (tem)
509 (rdiv { tem; } @1)))))
510
0a8f32b8
RB
511/* Simplify ~X & X as zero. */
512(simplify
513 (bit_and:c (convert? @0) (convert? (bit_not @0)))
514 { build_zero_cst (type); })
515
10158317
RB
516/* Fold (A & ~B) - (A & B) into (A ^ B) - B. */
517(simplify
a9658b11 518 (minus (bit_and:cs @0 (bit_not @1)) (bit_and:cs @0 @1))
10158317
RB
519 (minus (bit_xor @0 @1) @1))
520(simplify
521 (minus (bit_and:s @0 INTEGER_CST@2) (bit_and:s @0 INTEGER_CST@1))
522 (if (wi::bit_not (@2) == @1)
523 (minus (bit_xor @0 @1) @1)))
524
525/* Fold (A & B) - (A & ~B) into B - (A ^ B). */
526(simplify
a8e9f9a3 527 (minus (bit_and:cs @0 @1) (bit_and:cs @0 (bit_not @1)))
10158317
RB
528 (minus @1 (bit_xor @0 @1)))
529
530/* Simplify (X & ~Y) | (~X & Y) -> X ^ Y. */
531(simplify
a9658b11 532 (bit_ior (bit_and:c @0 (bit_not @1)) (bit_and:c (bit_not @0) @1))
10158317
RB
533 (bit_xor @0 @1))
534(simplify
535 (bit_ior:c (bit_and @0 INTEGER_CST@2) (bit_and (bit_not @0) INTEGER_CST@1))
536 (if (wi::bit_not (@2) == @1)
537 (bit_xor @0 @1)))
d982c5b7
MG
538/* Simplify (~X & Y) to X ^ Y if we know that (X & ~Y) is 0. */
539#if GIMPLE
540(simplify
541 (bit_and (bit_not SSA_NAME@0) INTEGER_CST@1)
542 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
543 && (get_nonzero_bits (@0) & wi::bit_not (@1)) == 0)
544 (bit_xor @0 @1)))
545#endif
10158317 546
bc4315fb
MG
547/* X % Y is smaller than Y. */
548(for cmp (lt ge)
549 (simplify
550 (cmp (trunc_mod @0 @1) @1)
551 (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
552 { constant_boolean_node (cmp == LT_EXPR, type); })))
553(for cmp (gt le)
554 (simplify
555 (cmp @1 (trunc_mod @0 @1))
556 (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
557 { constant_boolean_node (cmp == GT_EXPR, type); })))
558
e0ee10ed
RB
559/* x | ~0 -> ~0 */
560(simplify
ca0b7ece
RB
561 (bit_ior @0 integer_all_onesp@1)
562 @1)
563
564/* x | 0 -> x */
565(simplify
566 (bit_ior @0 integer_zerop)
567 @0)
e0ee10ed
RB
568
569/* x & 0 -> 0 */
570(simplify
ca0b7ece
RB
571 (bit_and @0 integer_zerop@1)
572 @1)
e0ee10ed 573
a4398a30 574/* ~x | x -> -1 */
8b5ee871
MG
575/* ~x ^ x -> -1 */
576/* ~x + x -> -1 */
577(for op (bit_ior bit_xor plus)
578 (simplify
579 (op:c (convert? @0) (convert? (bit_not @0)))
580 (convert { build_all_ones_cst (TREE_TYPE (@0)); })))
a4398a30 581
e0ee10ed
RB
582/* x ^ x -> 0 */
583(simplify
584 (bit_xor @0 @0)
585 { build_zero_cst (type); })
586
36a60e48
RB
587/* Canonicalize X ^ ~0 to ~X. */
588(simplify
589 (bit_xor @0 integer_all_onesp@1)
590 (bit_not @0))
591
592/* x & ~0 -> x */
593(simplify
594 (bit_and @0 integer_all_onesp)
595 (non_lvalue @0))
596
597/* x & x -> x, x | x -> x */
598(for bitop (bit_and bit_ior)
599 (simplify
600 (bitop @0 @0)
601 (non_lvalue @0)))
602
c7986356
MG
603/* x & C -> x if we know that x & ~C == 0. */
604#if GIMPLE
605(simplify
606 (bit_and SSA_NAME@0 INTEGER_CST@1)
607 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
608 && (get_nonzero_bits (@0) & wi::bit_not (@1)) == 0)
609 @0))
610#endif
611
0f770b01
RV
612/* x + (x & 1) -> (x + 1) & ~1 */
613(simplify
44fc0a51
RB
614 (plus:c @0 (bit_and:s @0 integer_onep@1))
615 (bit_and (plus @0 @1) (bit_not @1)))
0f770b01
RV
616
617/* x & ~(x & y) -> x & ~y */
618/* x | ~(x | y) -> x | ~y */
619(for bitop (bit_and bit_ior)
af563d4b 620 (simplify
44fc0a51
RB
621 (bitop:c @0 (bit_not (bitop:cs @0 @1)))
622 (bitop @0 (bit_not @1))))
af563d4b
MG
623
624/* (x | y) & ~x -> y & ~x */
625/* (x & y) | ~x -> y | ~x */
626(for bitop (bit_and bit_ior)
627 rbitop (bit_ior bit_and)
628 (simplify
629 (bitop:c (rbitop:c @0 @1) (bit_not@2 @0))
630 (bitop @1 @2)))
0f770b01 631
f13c4673
MP
632/* (x & y) ^ (x | y) -> x ^ y */
633(simplify
2d6f2dce
MP
634 (bit_xor:c (bit_and @0 @1) (bit_ior @0 @1))
635 (bit_xor @0 @1))
f13c4673 636
9ea65ca6
MP
637/* (x ^ y) ^ (x | y) -> x & y */
638(simplify
639 (bit_xor:c (bit_xor @0 @1) (bit_ior @0 @1))
640 (bit_and @0 @1))
641
642/* (x & y) + (x ^ y) -> x | y */
643/* (x & y) | (x ^ y) -> x | y */
644/* (x & y) ^ (x ^ y) -> x | y */
645(for op (plus bit_ior bit_xor)
646 (simplify
647 (op:c (bit_and @0 @1) (bit_xor @0 @1))
648 (bit_ior @0 @1)))
649
650/* (x & y) + (x | y) -> x + y */
651(simplify
652 (plus:c (bit_and @0 @1) (bit_ior @0 @1))
653 (plus @0 @1))
654
9737efaf
MP
655/* (x + y) - (x | y) -> x & y */
656(simplify
657 (minus (plus @0 @1) (bit_ior @0 @1))
658 (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
659 && !TYPE_SATURATING (type))
660 (bit_and @0 @1)))
661
662/* (x + y) - (x & y) -> x | y */
663(simplify
664 (minus (plus @0 @1) (bit_and @0 @1))
665 (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
666 && !TYPE_SATURATING (type))
667 (bit_ior @0 @1)))
668
9ea65ca6
MP
669/* (x | y) - (x ^ y) -> x & y */
670(simplify
671 (minus (bit_ior @0 @1) (bit_xor @0 @1))
672 (bit_and @0 @1))
673
674/* (x | y) - (x & y) -> x ^ y */
675(simplify
676 (minus (bit_ior @0 @1) (bit_and @0 @1))
677 (bit_xor @0 @1))
678
66cc6273
MP
679/* (x | y) & ~(x & y) -> x ^ y */
680(simplify
681 (bit_and:c (bit_ior @0 @1) (bit_not (bit_and @0 @1)))
682 (bit_xor @0 @1))
683
684/* (x | y) & (~x ^ y) -> x & y */
685(simplify
686 (bit_and:c (bit_ior:c @0 @1) (bit_xor:c @1 (bit_not @0)))
687 (bit_and @0 @1))
688
5b00d921
RB
689/* ~x & ~y -> ~(x | y)
690 ~x | ~y -> ~(x & y) */
691(for op (bit_and bit_ior)
692 rop (bit_ior bit_and)
693 (simplify
694 (op (convert1? (bit_not @0)) (convert2? (bit_not @1)))
ece46666
MG
695 (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
696 && element_precision (type) <= element_precision (TREE_TYPE (@1)))
5b00d921
RB
697 (bit_not (rop (convert @0) (convert @1))))))
698
14ea9f92 699/* If we are XORing or adding two BIT_AND_EXPR's, both of which are and'ing
5b00d921
RB
700 with a constant, and the two constants have no bits in common,
701 we should treat this as a BIT_IOR_EXPR since this may produce more
702 simplifications. */
14ea9f92
RB
703(for op (bit_xor plus)
704 (simplify
705 (op (convert1? (bit_and@4 @0 INTEGER_CST@1))
706 (convert2? (bit_and@5 @2 INTEGER_CST@3)))
707 (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
708 && tree_nop_conversion_p (type, TREE_TYPE (@2))
709 && wi::bit_and (@1, @3) == 0)
710 (bit_ior (convert @4) (convert @5)))))
5b00d921
RB
711
712/* (X | Y) ^ X -> Y & ~ X*/
713(simplify
2eef1fc1 714 (bit_xor:c (convert1? (bit_ior:c @@0 @1)) (convert2? @0))
5b00d921
RB
715 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
716 (convert (bit_and @1 (bit_not @0)))))
717
718/* Convert ~X ^ ~Y to X ^ Y. */
719(simplify
720 (bit_xor (convert1? (bit_not @0)) (convert2? (bit_not @1)))
ece46666
MG
721 (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
722 && element_precision (type) <= element_precision (TREE_TYPE (@1)))
5b00d921
RB
723 (bit_xor (convert @0) (convert @1))))
724
725/* Convert ~X ^ C to X ^ ~C. */
726(simplify
727 (bit_xor (convert? (bit_not @0)) INTEGER_CST@1)
c8ba6498
EB
728 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
729 (bit_xor (convert @0) (bit_not @1))))
5b00d921 730
e39dab2c
MG
731/* Fold (X & Y) ^ Y and (X ^ Y) & Y as ~X & Y. */
732(for opo (bit_and bit_xor)
733 opi (bit_xor bit_and)
734 (simplify
735 (opo:c (opi:c @0 @1) @1)
736 (bit_and (bit_not @0) @1)))
97e77391 737
14ea9f92
RB
738/* Given a bit-wise operation CODE applied to ARG0 and ARG1, see if both
739 operands are another bit-wise operation with a common input. If so,
740 distribute the bit operations to save an operation and possibly two if
741 constants are involved. For example, convert
742 (A | B) & (A | C) into A | (B & C)
743 Further simplification will occur if B and C are constants. */
e07ab2fe
MG
744(for op (bit_and bit_ior bit_xor)
745 rop (bit_ior bit_and bit_and)
14ea9f92 746 (simplify
2eef1fc1 747 (op (convert? (rop:c @@0 @1)) (convert? (rop:c @0 @2)))
e07ab2fe
MG
748 (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
749 && tree_nop_conversion_p (type, TREE_TYPE (@2)))
14ea9f92
RB
750 (rop (convert @0) (op (convert @1) (convert @2))))))
751
e39dab2c
MG
752/* Some simple reassociation for bit operations, also handled in reassoc. */
753/* (X & Y) & Y -> X & Y
754 (X | Y) | Y -> X | Y */
755(for op (bit_and bit_ior)
756 (simplify
2eef1fc1 757 (op:c (convert1?@2 (op:c @0 @@1)) (convert2? @1))
e39dab2c
MG
758 @2))
759/* (X ^ Y) ^ Y -> X */
760(simplify
2eef1fc1 761 (bit_xor:c (convert1? (bit_xor:c @0 @@1)) (convert2? @1))
ece46666 762 (convert @0))
e39dab2c
MG
763/* (X & Y) & (X & Z) -> (X & Y) & Z
764 (X | Y) | (X | Z) -> (X | Y) | Z */
765(for op (bit_and bit_ior)
766 (simplify
767 (op:c (convert1?@3 (op:c@4 @0 @1)) (convert2?@5 (op:c@6 @0 @2)))
768 (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
769 && tree_nop_conversion_p (type, TREE_TYPE (@2)))
770 (if (single_use (@5) && single_use (@6))
771 (op @3 (convert @2))
772 (if (single_use (@3) && single_use (@4))
773 (op (convert @1) @5))))))
774/* (X ^ Y) ^ (X ^ Z) -> Y ^ Z */
775(simplify
776 (bit_xor (convert1? (bit_xor:c @0 @1)) (convert2? (bit_xor:c @0 @2)))
777 (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
778 && tree_nop_conversion_p (type, TREE_TYPE (@2)))
d78789f5 779 (bit_xor (convert @1) (convert @2))))
5b00d921 780
b14a9c57
RB
781(simplify
782 (abs (abs@1 @0))
783 @1)
f3582e54
RB
784(simplify
785 (abs (negate @0))
786 (abs @0))
787(simplify
788 (abs tree_expr_nonnegative_p@0)
789 @0)
790
55cf3946
RB
791/* A few cases of fold-const.c negate_expr_p predicate. */
792(match negate_expr_p
793 INTEGER_CST
b14a9c57
RB
794 (if ((INTEGRAL_TYPE_P (type)
795 && TYPE_OVERFLOW_WRAPS (type))
796 || (!TYPE_OVERFLOW_SANITIZED (type)
55cf3946
RB
797 && may_negate_without_overflow_p (t)))))
798(match negate_expr_p
799 FIXED_CST)
800(match negate_expr_p
801 (negate @0)
802 (if (!TYPE_OVERFLOW_SANITIZED (type))))
803(match negate_expr_p
804 REAL_CST
805 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (t)))))
806/* VECTOR_CST handling of non-wrapping types would recurse in unsupported
807 ways. */
808(match negate_expr_p
809 VECTOR_CST
810 (if (FLOAT_TYPE_P (TREE_TYPE (type)) || TYPE_OVERFLOW_WRAPS (type))))
0a8f32b8
RB
811
812/* (-A) * (-B) -> A * B */
813(simplify
814 (mult:c (convert1? (negate @0)) (convert2? negate_expr_p@1))
815 (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
816 && tree_nop_conversion_p (type, TREE_TYPE (@1)))
817 (mult (convert @0) (convert (negate @1)))))
55cf3946
RB
818
819/* -(A + B) -> (-B) - A. */
b14a9c57 820(simplify
55cf3946
RB
821 (negate (plus:c @0 negate_expr_p@1))
822 (if (!HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (type))
823 && !HONOR_SIGNED_ZEROS (element_mode (type)))
824 (minus (negate @1) @0)))
825
826/* A - B -> A + (-B) if B is easily negatable. */
b14a9c57 827(simplify
55cf3946 828 (minus @0 negate_expr_p@1)
e4e96a4f
KT
829 (if (!FIXED_POINT_TYPE_P (type))
830 (plus @0 (negate @1))))
d4573ffe 831
5609420f
RB
832/* Try to fold (type) X op CST -> (type) (X op ((type-x) CST))
833 when profitable.
834 For bitwise binary operations apply operand conversions to the
835 binary operation result instead of to the operands. This allows
836 to combine successive conversions and bitwise binary operations.
837 We combine the above two cases by using a conditional convert. */
838(for bitop (bit_and bit_ior bit_xor)
839 (simplify
840 (bitop (convert @0) (convert? @1))
841 (if (((TREE_CODE (@1) == INTEGER_CST
842 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
ad6f996c 843 && int_fits_type_p (@1, TREE_TYPE (@0)))
aea417d7 844 || types_match (@0, @1))
ad6f996c
RB
845 /* ??? This transform conflicts with fold-const.c doing
846 Convert (T)(x & c) into (T)x & (T)c, if c is an integer
847 constants (if x has signed type, the sign bit cannot be set
848 in c). This folds extension into the BIT_AND_EXPR.
849 Restrict it to GIMPLE to avoid endless recursions. */
850 && (bitop != BIT_AND_EXPR || GIMPLE)
5609420f
RB
851 && (/* That's a good idea if the conversion widens the operand, thus
852 after hoisting the conversion the operation will be narrower. */
853 TYPE_PRECISION (TREE_TYPE (@0)) < TYPE_PRECISION (type)
854 /* It's also a good idea if the conversion is to a non-integer
855 mode. */
856 || GET_MODE_CLASS (TYPE_MODE (type)) != MODE_INT
857 /* Or if the precision of TO is not the same as the precision
858 of its mode. */
859 || TYPE_PRECISION (type) != GET_MODE_PRECISION (TYPE_MODE (type))))
860 (convert (bitop @0 (convert @1))))))
861
b14a9c57
RB
862(for bitop (bit_and bit_ior)
863 rbitop (bit_ior bit_and)
864 /* (x | y) & x -> x */
865 /* (x & y) | x -> x */
866 (simplify
867 (bitop:c (rbitop:c @0 @1) @0)
868 @0)
869 /* (~x | y) & x -> x & y */
870 /* (~x & y) | x -> x | y */
871 (simplify
872 (bitop:c (rbitop:c (bit_not @0) @1) @0)
873 (bitop @0 @1)))
874
5609420f
RB
875/* (x | CST1) & CST2 -> (x & CST2) | (CST1 & CST2) */
876(simplify
877 (bit_and (bit_ior @0 CONSTANT_CLASS_P@1) CONSTANT_CLASS_P@2)
878 (bit_ior (bit_and @0 @2) (bit_and @1 @2)))
879
880/* Combine successive equal operations with constants. */
881(for bitop (bit_and bit_ior bit_xor)
882 (simplify
883 (bitop (bitop @0 CONSTANT_CLASS_P@1) CONSTANT_CLASS_P@2)
884 (bitop @0 (bitop @1 @2))))
885
886/* Try simple folding for X op !X, and X op X with the help
887 of the truth_valued_p and logical_inverted_value predicates. */
888(match truth_valued_p
889 @0
890 (if (INTEGRAL_TYPE_P (type) && TYPE_PRECISION (type) == 1)))
f84e7fd6 891(for op (tcc_comparison truth_and truth_andif truth_or truth_orif truth_xor)
5609420f
RB
892 (match truth_valued_p
893 (op @0 @1)))
894(match truth_valued_p
895 (truth_not @0))
896
0a8f32b8
RB
897(match (logical_inverted_value @0)
898 (truth_not @0))
5609420f
RB
899(match (logical_inverted_value @0)
900 (bit_not truth_valued_p@0))
901(match (logical_inverted_value @0)
09240451 902 (eq @0 integer_zerop))
5609420f 903(match (logical_inverted_value @0)
09240451 904 (ne truth_valued_p@0 integer_truep))
5609420f 905(match (logical_inverted_value @0)
09240451 906 (bit_xor truth_valued_p@0 integer_truep))
5609420f
RB
907
908/* X & !X -> 0. */
909(simplify
910 (bit_and:c @0 (logical_inverted_value @0))
911 { build_zero_cst (type); })
912/* X | !X and X ^ !X -> 1, , if X is truth-valued. */
913(for op (bit_ior bit_xor)
914 (simplify
915 (op:c truth_valued_p@0 (logical_inverted_value @0))
f84e7fd6 916 { constant_boolean_node (true, type); }))
59c20dc7
RB
917/* X ==/!= !X is false/true. */
918(for op (eq ne)
919 (simplify
920 (op:c truth_valued_p@0 (logical_inverted_value @0))
921 { constant_boolean_node (op == NE_EXPR ? true : false, type); }))
5609420f 922
5609420f
RB
923/* If arg1 and arg2 are booleans (or any single bit type)
924 then try to simplify:
925
926 (~X & Y) -> X < Y
927 (X & ~Y) -> Y < X
928 (~X | Y) -> X <= Y
929 (X | ~Y) -> Y <= X
930
931 But only do this if our result feeds into a comparison as
932 this transformation is not always a win, particularly on
933 targets with and-not instructions.
934 -> simplify_bitwise_binary_boolean */
935(simplify
936 (ne (bit_and:c (bit_not @0) @1) integer_zerop)
937 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
938 && TYPE_PRECISION (TREE_TYPE (@1)) == 1)
0f3f9437
RB
939 (if (TYPE_UNSIGNED (TREE_TYPE (@1)))
940 (lt @0 @1)
941 (gt @0 @1))))
5609420f
RB
942(simplify
943 (ne (bit_ior:c (bit_not @0) @1) integer_zerop)
944 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
945 && TYPE_PRECISION (TREE_TYPE (@1)) == 1)
0f3f9437
RB
946 (if (TYPE_UNSIGNED (TREE_TYPE (@1)))
947 (le @0 @1)
948 (ge @0 @1))))
5609420f 949
5609420f
RB
950/* ~~x -> x */
951(simplify
952 (bit_not (bit_not @0))
953 @0)
954
b14a9c57
RB
955/* Convert ~ (-A) to A - 1. */
956(simplify
957 (bit_not (convert? (negate @0)))
ece46666
MG
958 (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
959 || !TYPE_UNSIGNED (TREE_TYPE (@0)))
8b5ee871 960 (convert (minus @0 { build_each_one_cst (TREE_TYPE (@0)); }))))
b14a9c57
RB
961
962/* Convert ~ (A - 1) or ~ (A + -1) to -A. */
963(simplify
8b5ee871 964 (bit_not (convert? (minus @0 integer_each_onep)))
ece46666
MG
965 (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
966 || !TYPE_UNSIGNED (TREE_TYPE (@0)))
b14a9c57
RB
967 (convert (negate @0))))
968(simplify
969 (bit_not (convert? (plus @0 integer_all_onesp)))
ece46666
MG
970 (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
971 || !TYPE_UNSIGNED (TREE_TYPE (@0)))
b14a9c57
RB
972 (convert (negate @0))))
973
974/* Part of convert ~(X ^ Y) to ~X ^ Y or X ^ ~Y if ~X or ~Y simplify. */
975(simplify
976 (bit_not (convert? (bit_xor @0 INTEGER_CST@1)))
977 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
978 (convert (bit_xor @0 (bit_not @1)))))
979(simplify
980 (bit_not (convert? (bit_xor:c (bit_not @0) @1)))
981 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
982 (convert (bit_xor @0 @1))))
983
f52baa7b
MP
984/* (x & ~m) | (y & m) -> ((x ^ y) & m) ^ x */
985(simplify
44fc0a51
RB
986 (bit_ior:c (bit_and:cs @0 (bit_not @2)) (bit_and:cs @1 @2))
987 (bit_xor (bit_and (bit_xor @0 @1) @2) @0))
f52baa7b 988
f7b7b0aa
MP
989/* Fold A - (A & B) into ~B & A. */
990(simplify
2eef1fc1 991 (minus (convert1? @0) (convert2?:s (bit_and:cs @@0 @1)))
f7b7b0aa
MP
992 (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
993 && tree_nop_conversion_p (type, TREE_TYPE (@1)))
994 (convert (bit_and (bit_not @1) @0))))
5609420f 995
84ff66b8
AV
996
997
998/* ((X inner_op C0) outer_op C1)
999 With X being a tree where value_range has reasoned certain bits to always be
1000 zero throughout its computed value range,
1001 inner_op = {|,^}, outer_op = {|,^} and inner_op != outer_op
1002 where zero_mask has 1's for all bits that are sure to be 0 in
1003 and 0's otherwise.
1004 if (inner_op == '^') C0 &= ~C1;
1005 if ((C0 & ~zero_mask) == 0) then emit (X outer_op (C0 outer_op C1)
1006 if ((C1 & ~zero_mask) == 0) then emit (X inner_op (C0 outer_op C1)
1007*/
1008(for inner_op (bit_ior bit_xor)
1009 outer_op (bit_xor bit_ior)
1010(simplify
1011 (outer_op
1012 (inner_op:s @2 INTEGER_CST@0) INTEGER_CST@1)
1013 (with
1014 {
1015 bool fail = false;
1016 wide_int zero_mask_not;
1017 wide_int C0;
1018 wide_int cst_emit;
1019
1020 if (TREE_CODE (@2) == SSA_NAME)
1021 zero_mask_not = get_nonzero_bits (@2);
1022 else
1023 fail = true;
1024
1025 if (inner_op == BIT_XOR_EXPR)
1026 {
1027 C0 = wi::bit_and_not (@0, @1);
1028 cst_emit = wi::bit_or (C0, @1);
1029 }
1030 else
1031 {
1032 C0 = @0;
1033 cst_emit = wi::bit_xor (@0, @1);
1034 }
1035 }
1036 (if (!fail && wi::bit_and (C0, zero_mask_not) == 0)
1037 (outer_op @2 { wide_int_to_tree (type, cst_emit); })
1038 (if (!fail && wi::bit_and (@1, zero_mask_not) == 0)
1039 (inner_op @2 { wide_int_to_tree (type, cst_emit); }))))))
1040
a499aac5
RB
1041/* Associate (p +p off1) +p off2 as (p +p (off1 + off2)). */
1042(simplify
44fc0a51
RB
1043 (pointer_plus (pointer_plus:s @0 @1) @3)
1044 (pointer_plus @0 (plus @1 @3)))
a499aac5
RB
1045
1046/* Pattern match
1047 tem1 = (long) ptr1;
1048 tem2 = (long) ptr2;
1049 tem3 = tem2 - tem1;
1050 tem4 = (unsigned long) tem3;
1051 tem5 = ptr1 + tem4;
1052 and produce
1053 tem5 = ptr2; */
1054(simplify
1055 (pointer_plus @0 (convert?@2 (minus@3 (convert @1) (convert @0))))
1056 /* Conditionally look through a sign-changing conversion. */
1057 (if (TYPE_PRECISION (TREE_TYPE (@2)) == TYPE_PRECISION (TREE_TYPE (@3))
1058 && ((GIMPLE && useless_type_conversion_p (type, TREE_TYPE (@1)))
1059 || (GENERIC && type == TREE_TYPE (@1))))
1060 @1))
1061
1062/* Pattern match
1063 tem = (sizetype) ptr;
1064 tem = tem & algn;
1065 tem = -tem;
1066 ... = ptr p+ tem;
1067 and produce the simpler and easier to analyze with respect to alignment
1068 ... = ptr & ~algn; */
1069(simplify
1070 (pointer_plus @0 (negate (bit_and (convert @0) INTEGER_CST@1)))
1071 (with { tree algn = wide_int_to_tree (TREE_TYPE (@0), wi::bit_not (@1)); }
1072 (bit_and @0 { algn; })))
1073
99e943a2
RB
1074/* Try folding difference of addresses. */
1075(simplify
1076 (minus (convert ADDR_EXPR@0) (convert @1))
1077 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1078 (with { HOST_WIDE_INT diff; }
1079 (if (ptr_difference_const (@0, @1, &diff))
1080 { build_int_cst_type (type, diff); }))))
1081(simplify
1082 (minus (convert @0) (convert ADDR_EXPR@1))
1083 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1084 (with { HOST_WIDE_INT diff; }
1085 (if (ptr_difference_const (@0, @1, &diff))
1086 { build_int_cst_type (type, diff); }))))
1087
bab73f11
RB
1088/* If arg0 is derived from the address of an object or function, we may
1089 be able to fold this expression using the object or function's
1090 alignment. */
1091(simplify
1092 (bit_and (convert? @0) INTEGER_CST@1)
1093 (if (POINTER_TYPE_P (TREE_TYPE (@0))
1094 && tree_nop_conversion_p (type, TREE_TYPE (@0)))
1095 (with
1096 {
1097 unsigned int align;
1098 unsigned HOST_WIDE_INT bitpos;
1099 get_pointer_alignment_1 (@0, &align, &bitpos);
1100 }
1101 (if (wi::ltu_p (@1, align / BITS_PER_UNIT))
1102 { wide_int_to_tree (type, wi::bit_and (@1, bitpos / BITS_PER_UNIT)); }))))
99e943a2 1103
a499aac5 1104
cc7b5acf
RB
1105/* We can't reassociate at all for saturating types. */
1106(if (!TYPE_SATURATING (type))
1107
1108 /* Contract negates. */
1109 /* A + (-B) -> A - B */
1110 (simplify
1111 (plus:c (convert1? @0) (convert2? (negate @1)))
1112 /* Apply STRIP_NOPS on @0 and the negate. */
1113 (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
1114 && tree_nop_conversion_p (type, TREE_TYPE (@1))
6a4f0678 1115 && !TYPE_OVERFLOW_SANITIZED (type))
cc7b5acf
RB
1116 (minus (convert @0) (convert @1))))
1117 /* A - (-B) -> A + B */
1118 (simplify
1119 (minus (convert1? @0) (convert2? (negate @1)))
1120 (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
2f68e8bc 1121 && tree_nop_conversion_p (type, TREE_TYPE (@1))
6a4f0678 1122 && !TYPE_OVERFLOW_SANITIZED (type))
cc7b5acf
RB
1123 (plus (convert @0) (convert @1))))
1124 /* -(-A) -> A */
1125 (simplify
1126 (negate (convert? (negate @1)))
1127 (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
6a4f0678 1128 && !TYPE_OVERFLOW_SANITIZED (type))
a0f12cf8 1129 (convert @1)))
cc7b5acf 1130
7318e44f
RB
1131 /* We can't reassociate floating-point unless -fassociative-math
1132 or fixed-point plus or minus because of saturation to +-Inf. */
1133 (if ((!FLOAT_TYPE_P (type) || flag_associative_math)
1134 && !FIXED_POINT_TYPE_P (type))
cc7b5acf
RB
1135
1136 /* Match patterns that allow contracting a plus-minus pair
1137 irrespective of overflow issues. */
1138 /* (A +- B) - A -> +- B */
1139 /* (A +- B) -+ B -> A */
1140 /* A - (A +- B) -> -+ B */
1141 /* A +- (B -+ A) -> +- B */
1142 (simplify
1143 (minus (plus:c @0 @1) @0)
1144 @1)
1145 (simplify
1146 (minus (minus @0 @1) @0)
1147 (negate @1))
1148 (simplify
1149 (plus:c (minus @0 @1) @1)
1150 @0)
1151 (simplify
1152 (minus @0 (plus:c @0 @1))
1153 (negate @1))
1154 (simplify
1155 (minus @0 (minus @0 @1))
1156 @1)
1157
1158 /* (A +- CST) +- CST -> A + CST */
1159 (for outer_op (plus minus)
1160 (for inner_op (plus minus)
1161 (simplify
1162 (outer_op (inner_op @0 CONSTANT_CLASS_P@1) CONSTANT_CLASS_P@2)
1163 /* If the constant operation overflows we cannot do the transform
1164 as we would introduce undefined overflow, for example
1165 with (a - 1) + INT_MIN. */
23f27839 1166 (with { tree cst = const_binop (outer_op == inner_op
cc7b5acf
RB
1167 ? PLUS_EXPR : MINUS_EXPR, type, @1, @2); }
1168 (if (cst && !TREE_OVERFLOW (cst))
1169 (inner_op @0 { cst; } ))))))
1170
1171 /* (CST - A) +- CST -> CST - A */
1172 (for outer_op (plus minus)
1173 (simplify
1174 (outer_op (minus CONSTANT_CLASS_P@1 @0) CONSTANT_CLASS_P@2)
23f27839 1175 (with { tree cst = const_binop (outer_op, type, @1, @2); }
cc7b5acf
RB
1176 (if (cst && !TREE_OVERFLOW (cst))
1177 (minus { cst; } @0)))))
1178
1179 /* ~A + A -> -1 */
1180 (simplify
1181 (plus:c (bit_not @0) @0)
1182 (if (!TYPE_OVERFLOW_TRAPS (type))
1183 { build_all_ones_cst (type); }))
1184
1185 /* ~A + 1 -> -A */
1186 (simplify
e19740ae
RB
1187 (plus (convert? (bit_not @0)) integer_each_onep)
1188 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1189 (negate (convert @0))))
1190
1191 /* -A - 1 -> ~A */
1192 (simplify
1193 (minus (convert? (negate @0)) integer_each_onep)
1194 (if (!TYPE_OVERFLOW_TRAPS (type)
1195 && tree_nop_conversion_p (type, TREE_TYPE (@0)))
1196 (bit_not (convert @0))))
1197
1198 /* -1 - A -> ~A */
1199 (simplify
1200 (minus integer_all_onesp @0)
bc4315fb 1201 (bit_not @0))
cc7b5acf
RB
1202
1203 /* (T)(P + A) - (T)P -> (T) A */
1204 (for add (plus pointer_plus)
1205 (simplify
2eef1fc1 1206 (minus (convert (add @@0 @1))
cc7b5acf 1207 (convert @0))
09240451 1208 (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
cc7b5acf
RB
1209 /* For integer types, if A has a smaller type
1210 than T the result depends on the possible
1211 overflow in P + A.
1212 E.g. T=size_t, A=(unsigned)429497295, P>0.
1213 However, if an overflow in P + A would cause
1214 undefined behavior, we can assume that there
1215 is no overflow. */
1216 || (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1217 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
1218 /* For pointer types, if the conversion of A to the
1219 final type requires a sign- or zero-extension,
1220 then we have to punt - it is not defined which
1221 one is correct. */
1222 || (POINTER_TYPE_P (TREE_TYPE (@0))
1223 && TREE_CODE (@1) == INTEGER_CST
1224 && tree_int_cst_sign_bit (@1) == 0))
a8fc2579
RB
1225 (convert @1))))
1226
1227 /* (T)P - (T)(P + A) -> -(T) A */
1228 (for add (plus pointer_plus)
1229 (simplify
1230 (minus (convert @0)
2eef1fc1 1231 (convert (add @@0 @1)))
a8fc2579
RB
1232 (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
1233 /* For integer types, if A has a smaller type
1234 than T the result depends on the possible
1235 overflow in P + A.
1236 E.g. T=size_t, A=(unsigned)429497295, P>0.
1237 However, if an overflow in P + A would cause
1238 undefined behavior, we can assume that there
1239 is no overflow. */
1240 || (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1241 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
1242 /* For pointer types, if the conversion of A to the
1243 final type requires a sign- or zero-extension,
1244 then we have to punt - it is not defined which
1245 one is correct. */
1246 || (POINTER_TYPE_P (TREE_TYPE (@0))
1247 && TREE_CODE (@1) == INTEGER_CST
1248 && tree_int_cst_sign_bit (@1) == 0))
1249 (negate (convert @1)))))
1250
1251 /* (T)(P + A) - (T)(P + B) -> (T)A - (T)B */
1252 (for add (plus pointer_plus)
1253 (simplify
2eef1fc1 1254 (minus (convert (add @@0 @1))
a8fc2579
RB
1255 (convert (add @0 @2)))
1256 (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
1257 /* For integer types, if A has a smaller type
1258 than T the result depends on the possible
1259 overflow in P + A.
1260 E.g. T=size_t, A=(unsigned)429497295, P>0.
1261 However, if an overflow in P + A would cause
1262 undefined behavior, we can assume that there
1263 is no overflow. */
1264 || (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1265 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
1266 /* For pointer types, if the conversion of A to the
1267 final type requires a sign- or zero-extension,
1268 then we have to punt - it is not defined which
1269 one is correct. */
1270 || (POINTER_TYPE_P (TREE_TYPE (@0))
1271 && TREE_CODE (@1) == INTEGER_CST
1272 && tree_int_cst_sign_bit (@1) == 0
1273 && TREE_CODE (@2) == INTEGER_CST
1274 && tree_int_cst_sign_bit (@2) == 0))
1275 (minus (convert @1) (convert @2)))))))
cc7b5acf
RB
1276
1277
0122e8e5 1278/* Simplifications of MIN_EXPR, MAX_EXPR, fmin() and fmax(). */
a7f24614 1279
0122e8e5 1280(for minmax (min max FMIN FMAX)
a7f24614
RB
1281 (simplify
1282 (minmax @0 @0)
1283 @0))
4a334cba
RS
1284/* min(max(x,y),y) -> y. */
1285(simplify
1286 (min:c (max:c @0 @1) @1)
1287 @1)
1288/* max(min(x,y),y) -> y. */
1289(simplify
1290 (max:c (min:c @0 @1) @1)
1291 @1)
d657e995
RB
1292/* max(a,-a) -> abs(a). */
1293(simplify
1294 (max:c @0 (negate @0))
1295 (if (TREE_CODE (type) != COMPLEX_TYPE
1296 && (! ANY_INTEGRAL_TYPE_P (type)
1297 || TYPE_OVERFLOW_UNDEFINED (type)))
1298 (abs @0)))
54f84ca9
RB
1299/* min(a,-a) -> -abs(a). */
1300(simplify
1301 (min:c @0 (negate @0))
1302 (if (TREE_CODE (type) != COMPLEX_TYPE
1303 && (! ANY_INTEGRAL_TYPE_P (type)
1304 || TYPE_OVERFLOW_UNDEFINED (type)))
1305 (negate (abs @0))))
a7f24614
RB
1306(simplify
1307 (min @0 @1)
2c2870a1
MG
1308 (switch
1309 (if (INTEGRAL_TYPE_P (type)
1310 && TYPE_MIN_VALUE (type)
1311 && operand_equal_p (@1, TYPE_MIN_VALUE (type), OEP_ONLY_CONST))
1312 @1)
1313 (if (INTEGRAL_TYPE_P (type)
1314 && TYPE_MAX_VALUE (type)
1315 && operand_equal_p (@1, TYPE_MAX_VALUE (type), OEP_ONLY_CONST))
1316 @0)))
a7f24614
RB
1317(simplify
1318 (max @0 @1)
2c2870a1
MG
1319 (switch
1320 (if (INTEGRAL_TYPE_P (type)
1321 && TYPE_MAX_VALUE (type)
1322 && operand_equal_p (@1, TYPE_MAX_VALUE (type), OEP_ONLY_CONST))
1323 @1)
1324 (if (INTEGRAL_TYPE_P (type)
1325 && TYPE_MIN_VALUE (type)
1326 && operand_equal_p (@1, TYPE_MIN_VALUE (type), OEP_ONLY_CONST))
1327 @0)))
0122e8e5
RS
1328(for minmax (FMIN FMAX)
1329 /* If either argument is NaN, return the other one. Avoid the
1330 transformation if we get (and honor) a signalling NaN. */
1331 (simplify
1332 (minmax:c @0 REAL_CST@1)
1333 (if (real_isnan (TREE_REAL_CST_PTR (@1))
1334 && (!HONOR_SNANS (@1) || !TREE_REAL_CST (@1).signalling))
1335 @0)))
1336/* Convert fmin/fmax to MIN_EXPR/MAX_EXPR. C99 requires these
1337 functions to return the numeric arg if the other one is NaN.
1338 MIN and MAX don't honor that, so only transform if -ffinite-math-only
1339 is set. C99 doesn't require -0.0 to be handled, so we don't have to
1340 worry about it either. */
1341(if (flag_finite_math_only)
1342 (simplify
1343 (FMIN @0 @1)
1344 (min @0 @1))
1345 (simplify
1346 (FMAX @0 @1)
1347 (max @0 @1)))
ce0e66ff
MG
1348/* min (-A, -B) -> -max (A, B) */
1349(for minmax (min max FMIN FMAX)
1350 maxmin (max min FMAX FMIN)
1351 (simplify
1352 (minmax (negate:s@2 @0) (negate:s@3 @1))
1353 (if (FLOAT_TYPE_P (TREE_TYPE (@0))
1354 || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1355 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))))
1356 (negate (maxmin @0 @1)))))
1357/* MIN (~X, ~Y) -> ~MAX (X, Y)
1358 MAX (~X, ~Y) -> ~MIN (X, Y) */
1359(for minmax (min max)
1360 maxmin (max min)
1361 (simplify
1362 (minmax (bit_not:s@2 @0) (bit_not:s@3 @1))
1363 (bit_not (maxmin @0 @1))))
a7f24614 1364
b4817bd6
MG
1365/* MIN (X, Y) == X -> X <= Y */
1366(for minmax (min min max max)
1367 cmp (eq ne eq ne )
1368 out (le gt ge lt )
1369 (simplify
1370 (cmp:c (minmax:c @0 @1) @0)
1371 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0)))
1372 (out @0 @1))))
1373/* MIN (X, 5) == 0 -> X == 0
1374 MIN (X, 5) == 7 -> false */
1375(for cmp (eq ne)
1376 (simplify
1377 (cmp (min @0 INTEGER_CST@1) INTEGER_CST@2)
1378 (if (wi::lt_p (@1, @2, TYPE_SIGN (TREE_TYPE (@0))))
1379 { constant_boolean_node (cmp == NE_EXPR, type); }
1380 (if (wi::gt_p (@1, @2, TYPE_SIGN (TREE_TYPE (@0))))
1381 (cmp @0 @2)))))
1382(for cmp (eq ne)
1383 (simplify
1384 (cmp (max @0 INTEGER_CST@1) INTEGER_CST@2)
1385 (if (wi::gt_p (@1, @2, TYPE_SIGN (TREE_TYPE (@0))))
1386 { constant_boolean_node (cmp == NE_EXPR, type); }
1387 (if (wi::lt_p (@1, @2, TYPE_SIGN (TREE_TYPE (@0))))
1388 (cmp @0 @2)))))
1389/* MIN (X, C1) < C2 -> X < C2 || C1 < C2 */
1390(for minmax (min min max max min min max max )
1391 cmp (lt le gt ge gt ge lt le )
1392 comb (bit_ior bit_ior bit_ior bit_ior bit_and bit_and bit_and bit_and)
1393 (simplify
1394 (cmp (minmax @0 INTEGER_CST@1) INTEGER_CST@2)
1395 (comb (cmp @0 @2) (cmp @1 @2))))
1396
a7f24614
RB
1397/* Simplifications of shift and rotates. */
1398
1399(for rotate (lrotate rrotate)
1400 (simplify
1401 (rotate integer_all_onesp@0 @1)
1402 @0))
1403
1404/* Optimize -1 >> x for arithmetic right shifts. */
1405(simplify
1406 (rshift integer_all_onesp@0 @1)
1407 (if (!TYPE_UNSIGNED (type)
1408 && tree_expr_nonnegative_p (@1))
1409 @0))
1410
12085390
N
1411/* Optimize (x >> c) << c into x & (-1<<c). */
1412(simplify
1413 (lshift (rshift @0 INTEGER_CST@1) @1)
1414 (if (wi::ltu_p (@1, element_precision (type)))
1415 (bit_and @0 (lshift { build_minus_one_cst (type); } @1))))
1416
1417/* Optimize (x << c) >> c into x & ((unsigned)-1 >> c) for unsigned
1418 types. */
1419(simplify
1420 (rshift (lshift @0 INTEGER_CST@1) @1)
1421 (if (TYPE_UNSIGNED (type)
1422 && (wi::ltu_p (@1, element_precision (type))))
1423 (bit_and @0 (rshift { build_minus_one_cst (type); } @1))))
1424
a7f24614
RB
1425(for shiftrotate (lrotate rrotate lshift rshift)
1426 (simplify
1427 (shiftrotate @0 integer_zerop)
1428 (non_lvalue @0))
1429 (simplify
1430 (shiftrotate integer_zerop@0 @1)
1431 @0)
1432 /* Prefer vector1 << scalar to vector1 << vector2
1433 if vector2 is uniform. */
1434 (for vec (VECTOR_CST CONSTRUCTOR)
1435 (simplify
1436 (shiftrotate @0 vec@1)
1437 (with { tree tem = uniform_vector_p (@1); }
1438 (if (tem)
1439 (shiftrotate @0 { tem; }))))))
1440
1441/* Rewrite an LROTATE_EXPR by a constant into an
1442 RROTATE_EXPR by a new constant. */
1443(simplify
1444 (lrotate @0 INTEGER_CST@1)
23f27839 1445 (rrotate @0 { const_binop (MINUS_EXPR, TREE_TYPE (@1),
a7f24614
RB
1446 build_int_cst (TREE_TYPE (@1),
1447 element_precision (type)), @1); }))
1448
14ea9f92
RB
1449/* Turn (a OP c1) OP c2 into a OP (c1+c2). */
1450(for op (lrotate rrotate rshift lshift)
1451 (simplify
1452 (op (op @0 INTEGER_CST@1) INTEGER_CST@2)
1453 (with { unsigned int prec = element_precision (type); }
1454 (if (wi::ge_p (@1, 0, TYPE_SIGN (TREE_TYPE (@1)))
1455 && wi::lt_p (@1, prec, TYPE_SIGN (TREE_TYPE (@1)))
1456 && wi::ge_p (@2, 0, TYPE_SIGN (TREE_TYPE (@2)))
1457 && wi::lt_p (@2, prec, TYPE_SIGN (TREE_TYPE (@2))))
1458 (with { unsigned int low = wi::add (@1, @2).to_uhwi (); }
1459 /* Deal with a OP (c1 + c2) being undefined but (a OP c1) OP c2
1460 being well defined. */
1461 (if (low >= prec)
1462 (if (op == LROTATE_EXPR || op == RROTATE_EXPR)
8fdc6c67 1463 (op @0 { build_int_cst (TREE_TYPE (@1), low % prec); })
50301115 1464 (if (TYPE_UNSIGNED (type) || op == LSHIFT_EXPR)
8fdc6c67
RB
1465 { build_zero_cst (type); }
1466 (op @0 { build_int_cst (TREE_TYPE (@1), prec - 1); })))
1467 (op @0 { build_int_cst (TREE_TYPE (@1), low); })))))))
14ea9f92
RB
1468
1469
01ada710
MP
1470/* ((1 << A) & 1) != 0 -> A == 0
1471 ((1 << A) & 1) == 0 -> A != 0 */
1472(for cmp (ne eq)
1473 icmp (eq ne)
1474 (simplify
1475 (cmp (bit_and (lshift integer_onep @0) integer_onep) integer_zerop)
1476 (icmp @0 { build_zero_cst (TREE_TYPE (@0)); })))
cc7b5acf 1477
f2e609c3
MP
1478/* (CST1 << A) == CST2 -> A == ctz (CST2) - ctz (CST1)
1479 (CST1 << A) != CST2 -> A != ctz (CST2) - ctz (CST1)
1480 if CST2 != 0. */
1481(for cmp (ne eq)
1482 (simplify
1483 (cmp (lshift INTEGER_CST@0 @1) INTEGER_CST@2)
1484 (with { int cand = wi::ctz (@2) - wi::ctz (@0); }
1485 (if (cand < 0
1486 || (!integer_zerop (@2)
1487 && wi::ne_p (wi::lshift (@0, cand), @2)))
8fdc6c67
RB
1488 { constant_boolean_node (cmp == NE_EXPR, type); }
1489 (if (!integer_zerop (@2)
1490 && wi::eq_p (wi::lshift (@0, cand), @2))
1491 (cmp @1 { build_int_cst (TREE_TYPE (@1), cand); }))))))
f2e609c3 1492
1ffbaa3f
RB
1493/* Fold (X << C1) & C2 into (X << C1) & (C2 | ((1 << C1) - 1))
1494 (X >> C1) & C2 into (X >> C1) & (C2 | ~((type) -1 >> C1))
1495 if the new mask might be further optimized. */
1496(for shift (lshift rshift)
1497 (simplify
44fc0a51
RB
1498 (bit_and (convert?:s@4 (shift:s@5 (convert1?@3 @0) INTEGER_CST@1))
1499 INTEGER_CST@2)
1ffbaa3f
RB
1500 (if (tree_nop_conversion_p (TREE_TYPE (@4), TREE_TYPE (@5))
1501 && TYPE_PRECISION (type) <= HOST_BITS_PER_WIDE_INT
1502 && tree_fits_uhwi_p (@1)
1503 && tree_to_uhwi (@1) > 0
1504 && tree_to_uhwi (@1) < TYPE_PRECISION (type))
1505 (with
1506 {
1507 unsigned int shiftc = tree_to_uhwi (@1);
1508 unsigned HOST_WIDE_INT mask = TREE_INT_CST_LOW (@2);
1509 unsigned HOST_WIDE_INT newmask, zerobits = 0;
1510 tree shift_type = TREE_TYPE (@3);
1511 unsigned int prec;
1512
1513 if (shift == LSHIFT_EXPR)
fecfbfa4 1514 zerobits = ((HOST_WIDE_INT_1U << shiftc) - 1);
1ffbaa3f
RB
1515 else if (shift == RSHIFT_EXPR
1516 && (TYPE_PRECISION (shift_type)
1517 == GET_MODE_PRECISION (TYPE_MODE (shift_type))))
1518 {
1519 prec = TYPE_PRECISION (TREE_TYPE (@3));
1520 tree arg00 = @0;
1521 /* See if more bits can be proven as zero because of
1522 zero extension. */
1523 if (@3 != @0
1524 && TYPE_UNSIGNED (TREE_TYPE (@0)))
1525 {
1526 tree inner_type = TREE_TYPE (@0);
1527 if ((TYPE_PRECISION (inner_type)
1528 == GET_MODE_PRECISION (TYPE_MODE (inner_type)))
1529 && TYPE_PRECISION (inner_type) < prec)
1530 {
1531 prec = TYPE_PRECISION (inner_type);
1532 /* See if we can shorten the right shift. */
1533 if (shiftc < prec)
1534 shift_type = inner_type;
1535 /* Otherwise X >> C1 is all zeros, so we'll optimize
1536 it into (X, 0) later on by making sure zerobits
1537 is all ones. */
1538 }
1539 }
dd4786fe 1540 zerobits = HOST_WIDE_INT_M1U;
1ffbaa3f
RB
1541 if (shiftc < prec)
1542 {
1543 zerobits >>= HOST_BITS_PER_WIDE_INT - shiftc;
1544 zerobits <<= prec - shiftc;
1545 }
1546 /* For arithmetic shift if sign bit could be set, zerobits
1547 can contain actually sign bits, so no transformation is
1548 possible, unless MASK masks them all away. In that
1549 case the shift needs to be converted into logical shift. */
1550 if (!TYPE_UNSIGNED (TREE_TYPE (@3))
1551 && prec == TYPE_PRECISION (TREE_TYPE (@3)))
1552 {
1553 if ((mask & zerobits) == 0)
1554 shift_type = unsigned_type_for (TREE_TYPE (@3));
1555 else
1556 zerobits = 0;
1557 }
1558 }
1559 }
1560 /* ((X << 16) & 0xff00) is (X, 0). */
1561 (if ((mask & zerobits) == mask)
8fdc6c67
RB
1562 { build_int_cst (type, 0); }
1563 (with { newmask = mask | zerobits; }
1564 (if (newmask != mask && (newmask & (newmask + 1)) == 0)
1565 (with
1566 {
1567 /* Only do the transformation if NEWMASK is some integer
1568 mode's mask. */
1569 for (prec = BITS_PER_UNIT;
1570 prec < HOST_BITS_PER_WIDE_INT; prec <<= 1)
fecfbfa4 1571 if (newmask == (HOST_WIDE_INT_1U << prec) - 1)
8fdc6c67
RB
1572 break;
1573 }
1574 (if (prec < HOST_BITS_PER_WIDE_INT
dd4786fe 1575 || newmask == HOST_WIDE_INT_M1U)
8fdc6c67
RB
1576 (with
1577 { tree newmaskt = build_int_cst_type (TREE_TYPE (@2), newmask); }
1578 (if (!tree_int_cst_equal (newmaskt, @2))
1579 (if (shift_type != TREE_TYPE (@3))
1580 (bit_and (convert (shift:shift_type (convert @3) @1)) { newmaskt; })
1581 (bit_and @4 { newmaskt; })))))))))))))
1ffbaa3f 1582
84ff66b8
AV
1583/* Fold (X {&,^,|} C2) << C1 into (X << C1) {&,^,|} (C2 << C1)
1584 (X {&,^,|} C2) >> C1 into (X >> C1) & (C2 >> C1). */
98e30e51 1585(for shift (lshift rshift)
84ff66b8
AV
1586 (for bit_op (bit_and bit_xor bit_ior)
1587 (simplify
1588 (shift (convert?:s (bit_op:s @0 INTEGER_CST@2)) INTEGER_CST@1)
1589 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1590 (with { tree mask = int_const_binop (shift, fold_convert (type, @2), @1); }
1591 (bit_op (shift (convert @0) @1) { mask; }))))))
98e30e51 1592
ad1d92ab
MM
1593/* ~(~X >> Y) -> X >> Y (for arithmetic shift). */
1594(simplify
1595 (bit_not (convert1?:s (rshift:s (convert2?@0 (bit_not @1)) @2)))
1596 (if (!TYPE_UNSIGNED (TREE_TYPE (@0))
ece46666
MG
1597 && (element_precision (TREE_TYPE (@0))
1598 <= element_precision (TREE_TYPE (@1))
1599 || !TYPE_UNSIGNED (TREE_TYPE (@1))))
ad1d92ab
MM
1600 (with
1601 { tree shift_type = TREE_TYPE (@0); }
1602 (convert (rshift (convert:shift_type @1) @2)))))
1603
1604/* ~(~X >>r Y) -> X >>r Y
1605 ~(~X <<r Y) -> X <<r Y */
1606(for rotate (lrotate rrotate)
1607 (simplify
1608 (bit_not (convert1?:s (rotate:s (convert2?@0 (bit_not @1)) @2)))
ece46666
MG
1609 (if ((element_precision (TREE_TYPE (@0))
1610 <= element_precision (TREE_TYPE (@1))
1611 || !TYPE_UNSIGNED (TREE_TYPE (@1)))
1612 && (element_precision (type) <= element_precision (TREE_TYPE (@0))
1613 || !TYPE_UNSIGNED (TREE_TYPE (@0))))
ad1d92ab
MM
1614 (with
1615 { tree rotate_type = TREE_TYPE (@0); }
1616 (convert (rotate (convert:rotate_type @1) @2))))))
98e30e51 1617
d4573ffe
RB
1618/* Simplifications of conversions. */
1619
1620/* Basic strip-useless-type-conversions / strip_nops. */
f3582e54 1621(for cvt (convert view_convert float fix_trunc)
d4573ffe
RB
1622 (simplify
1623 (cvt @0)
1624 (if ((GIMPLE && useless_type_conversion_p (type, TREE_TYPE (@0)))
1625 || (GENERIC && type == TREE_TYPE (@0)))
1626 @0)))
1627
1628/* Contract view-conversions. */
1629(simplify
1630 (view_convert (view_convert @0))
1631 (view_convert @0))
1632
1633/* For integral conversions with the same precision or pointer
1634 conversions use a NOP_EXPR instead. */
1635(simplify
1636 (view_convert @0)
1637 (if ((INTEGRAL_TYPE_P (type) || POINTER_TYPE_P (type))
1638 && (INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))
1639 && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (@0)))
1640 (convert @0)))
1641
1642/* Strip inner integral conversions that do not change precision or size. */
1643(simplify
1644 (view_convert (convert@0 @1))
1645 (if ((INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))
1646 && (INTEGRAL_TYPE_P (TREE_TYPE (@1)) || POINTER_TYPE_P (TREE_TYPE (@1)))
1647 && (TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1)))
1648 && (TYPE_SIZE (TREE_TYPE (@0)) == TYPE_SIZE (TREE_TYPE (@1))))
1649 (view_convert @1)))
1650
1651/* Re-association barriers around constants and other re-association
1652 barriers can be removed. */
1653(simplify
1654 (paren CONSTANT_CLASS_P@0)
1655 @0)
1656(simplify
1657 (paren (paren@1 @0))
1658 @1)
1e51d0a2
RB
1659
1660/* Handle cases of two conversions in a row. */
1661(for ocvt (convert float fix_trunc)
1662 (for icvt (convert float)
1663 (simplify
1664 (ocvt (icvt@1 @0))
1665 (with
1666 {
1667 tree inside_type = TREE_TYPE (@0);
1668 tree inter_type = TREE_TYPE (@1);
1669 int inside_int = INTEGRAL_TYPE_P (inside_type);
1670 int inside_ptr = POINTER_TYPE_P (inside_type);
1671 int inside_float = FLOAT_TYPE_P (inside_type);
09240451 1672 int inside_vec = VECTOR_TYPE_P (inside_type);
1e51d0a2
RB
1673 unsigned int inside_prec = TYPE_PRECISION (inside_type);
1674 int inside_unsignedp = TYPE_UNSIGNED (inside_type);
1675 int inter_int = INTEGRAL_TYPE_P (inter_type);
1676 int inter_ptr = POINTER_TYPE_P (inter_type);
1677 int inter_float = FLOAT_TYPE_P (inter_type);
09240451 1678 int inter_vec = VECTOR_TYPE_P (inter_type);
1e51d0a2
RB
1679 unsigned int inter_prec = TYPE_PRECISION (inter_type);
1680 int inter_unsignedp = TYPE_UNSIGNED (inter_type);
1681 int final_int = INTEGRAL_TYPE_P (type);
1682 int final_ptr = POINTER_TYPE_P (type);
1683 int final_float = FLOAT_TYPE_P (type);
09240451 1684 int final_vec = VECTOR_TYPE_P (type);
1e51d0a2
RB
1685 unsigned int final_prec = TYPE_PRECISION (type);
1686 int final_unsignedp = TYPE_UNSIGNED (type);
1687 }
64d3a1f0
RB
1688 (switch
1689 /* In addition to the cases of two conversions in a row
1690 handled below, if we are converting something to its own
1691 type via an object of identical or wider precision, neither
1692 conversion is needed. */
1693 (if (((GIMPLE && useless_type_conversion_p (type, inside_type))
1694 || (GENERIC
1695 && TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (inside_type)))
1696 && (((inter_int || inter_ptr) && final_int)
1697 || (inter_float && final_float))
1698 && inter_prec >= final_prec)
1699 (ocvt @0))
1700
1701 /* Likewise, if the intermediate and initial types are either both
1702 float or both integer, we don't need the middle conversion if the
1703 former is wider than the latter and doesn't change the signedness
1704 (for integers). Avoid this if the final type is a pointer since
36088299 1705 then we sometimes need the middle conversion. */
64d3a1f0
RB
1706 (if (((inter_int && inside_int) || (inter_float && inside_float))
1707 && (final_int || final_float)
1708 && inter_prec >= inside_prec
36088299 1709 && (inter_float || inter_unsignedp == inside_unsignedp))
64d3a1f0
RB
1710 (ocvt @0))
1711
1712 /* If we have a sign-extension of a zero-extended value, we can
1713 replace that by a single zero-extension. Likewise if the
1714 final conversion does not change precision we can drop the
1715 intermediate conversion. */
1716 (if (inside_int && inter_int && final_int
1717 && ((inside_prec < inter_prec && inter_prec < final_prec
1718 && inside_unsignedp && !inter_unsignedp)
1719 || final_prec == inter_prec))
1720 (ocvt @0))
1721
1722 /* Two conversions in a row are not needed unless:
1e51d0a2
RB
1723 - some conversion is floating-point (overstrict for now), or
1724 - some conversion is a vector (overstrict for now), or
1725 - the intermediate type is narrower than both initial and
1726 final, or
1727 - the intermediate type and innermost type differ in signedness,
1728 and the outermost type is wider than the intermediate, or
1729 - the initial type is a pointer type and the precisions of the
1730 intermediate and final types differ, or
1731 - the final type is a pointer type and the precisions of the
1732 initial and intermediate types differ. */
64d3a1f0
RB
1733 (if (! inside_float && ! inter_float && ! final_float
1734 && ! inside_vec && ! inter_vec && ! final_vec
1735 && (inter_prec >= inside_prec || inter_prec >= final_prec)
1736 && ! (inside_int && inter_int
1737 && inter_unsignedp != inside_unsignedp
1738 && inter_prec < final_prec)
1739 && ((inter_unsignedp && inter_prec > inside_prec)
1740 == (final_unsignedp && final_prec > inter_prec))
1741 && ! (inside_ptr && inter_prec != final_prec)
36088299 1742 && ! (final_ptr && inside_prec != inter_prec))
64d3a1f0
RB
1743 (ocvt @0))
1744
1745 /* A truncation to an unsigned type (a zero-extension) should be
1746 canonicalized as bitwise and of a mask. */
1d510e04
JJ
1747 (if (GIMPLE /* PR70366: doing this in GENERIC breaks -Wconversion. */
1748 && final_int && inter_int && inside_int
64d3a1f0
RB
1749 && final_prec == inside_prec
1750 && final_prec > inter_prec
1751 && inter_unsignedp)
1752 (convert (bit_and @0 { wide_int_to_tree
1753 (inside_type,
1754 wi::mask (inter_prec, false,
1755 TYPE_PRECISION (inside_type))); })))
1756
1757 /* If we are converting an integer to a floating-point that can
1758 represent it exactly and back to an integer, we can skip the
1759 floating-point conversion. */
1760 (if (GIMPLE /* PR66211 */
1761 && inside_int && inter_float && final_int &&
1762 (unsigned) significand_size (TYPE_MODE (inter_type))
1763 >= inside_prec - !inside_unsignedp)
1764 (convert @0)))))))
ea2042ba
RB
1765
1766/* If we have a narrowing conversion to an integral type that is fed by a
1767 BIT_AND_EXPR, we might be able to remove the BIT_AND_EXPR if it merely
1768 masks off bits outside the final type (and nothing else). */
1769(simplify
1770 (convert (bit_and @0 INTEGER_CST@1))
1771 (if (INTEGRAL_TYPE_P (type)
1772 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
1773 && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))
1774 && operand_equal_p (@1, build_low_bits_mask (TREE_TYPE (@1),
1775 TYPE_PRECISION (type)), 0))
1776 (convert @0)))
a25454ea
RB
1777
1778
1779/* (X /[ex] A) * A -> X. */
1780(simplify
2eef1fc1
RB
1781 (mult (convert1? (exact_div @0 @@1)) (convert2? @1))
1782 (convert @0))
eaeba53a 1783
a7f24614
RB
1784/* Canonicalization of binary operations. */
1785
1786/* Convert X + -C into X - C. */
1787(simplify
1788 (plus @0 REAL_CST@1)
1789 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1)))
23f27839 1790 (with { tree tem = const_unop (NEGATE_EXPR, type, @1); }
a7f24614
RB
1791 (if (!TREE_OVERFLOW (tem) || !flag_trapping_math)
1792 (minus @0 { tem; })))))
1793
6b6aa8d3 1794/* Convert x+x into x*2. */
a7f24614
RB
1795(simplify
1796 (plus @0 @0)
1797 (if (SCALAR_FLOAT_TYPE_P (type))
6b6aa8d3
MG
1798 (mult @0 { build_real (type, dconst2); })
1799 (if (INTEGRAL_TYPE_P (type))
1800 (mult @0 { build_int_cst (type, 2); }))))
a7f24614
RB
1801
1802(simplify
1803 (minus integer_zerop @1)
1804 (negate @1))
1805
1806/* (ARG0 - ARG1) is the same as (-ARG1 + ARG0). So check whether
1807 ARG0 is zero and X + ARG0 reduces to X, since that would mean
1808 (-ARG1 + ARG0) reduces to -ARG1. */
1809(simplify
1810 (minus real_zerop@0 @1)
1811 (if (fold_real_zero_addition_p (type, @0, 0))
1812 (negate @1)))
1813
1814/* Transform x * -1 into -x. */
1815(simplify
1816 (mult @0 integer_minus_onep)
1817 (negate @0))
eaeba53a 1818
96285749
RS
1819/* True if we can easily extract the real and imaginary parts of a complex
1820 number. */
1821(match compositional_complex
1822 (convert? (complex @0 @1)))
1823
eaeba53a
RB
1824/* COMPLEX_EXPR and REALPART/IMAGPART_EXPR cancellations. */
1825(simplify
1826 (complex (realpart @0) (imagpart @0))
1827 @0)
1828(simplify
1829 (realpart (complex @0 @1))
1830 @0)
1831(simplify
1832 (imagpart (complex @0 @1))
1833 @1)
83633539 1834
77c028c5
MG
1835/* Sometimes we only care about half of a complex expression. */
1836(simplify
1837 (realpart (convert?:s (conj:s @0)))
1838 (convert (realpart @0)))
1839(simplify
1840 (imagpart (convert?:s (conj:s @0)))
1841 (convert (negate (imagpart @0))))
1842(for part (realpart imagpart)
1843 (for op (plus minus)
1844 (simplify
1845 (part (convert?:s@2 (op:s @0 @1)))
1846 (convert (op (part @0) (part @1))))))
1847(simplify
1848 (realpart (convert?:s (CEXPI:s @0)))
1849 (convert (COS @0)))
1850(simplify
1851 (imagpart (convert?:s (CEXPI:s @0)))
1852 (convert (SIN @0)))
1853
1854/* conj(conj(x)) -> x */
1855(simplify
1856 (conj (convert? (conj @0)))
1857 (if (tree_nop_conversion_p (TREE_TYPE (@0), type))
1858 (convert @0)))
1859
1860/* conj({x,y}) -> {x,-y} */
1861(simplify
1862 (conj (convert?:s (complex:s @0 @1)))
1863 (with { tree itype = TREE_TYPE (type); }
1864 (complex (convert:itype @0) (negate (convert:itype @1)))))
83633539
RB
1865
1866/* BSWAP simplifications, transforms checked by gcc.dg/builtin-bswap-8.c. */
1867(for bswap (BUILT_IN_BSWAP16 BUILT_IN_BSWAP32 BUILT_IN_BSWAP64)
1868 (simplify
1869 (bswap (bswap @0))
1870 @0)
1871 (simplify
1872 (bswap (bit_not (bswap @0)))
1873 (bit_not @0))
1874 (for bitop (bit_xor bit_ior bit_and)
1875 (simplify
1876 (bswap (bitop:c (bswap @0) @1))
1877 (bitop @0 (bswap @1)))))
96994de0
RB
1878
1879
1880/* Combine COND_EXPRs and VEC_COND_EXPRs. */
1881
1882/* Simplify constant conditions.
1883 Only optimize constant conditions when the selected branch
1884 has the same type as the COND_EXPR. This avoids optimizing
1885 away "c ? x : throw", where the throw has a void type.
1886 Note that we cannot throw away the fold-const.c variant nor
1887 this one as we depend on doing this transform before possibly
1888 A ? B : B -> B triggers and the fold-const.c one can optimize
1889 0 ? A : B to B even if A has side-effects. Something
1890 genmatch cannot handle. */
1891(simplify
1892 (cond INTEGER_CST@0 @1 @2)
8fdc6c67
RB
1893 (if (integer_zerop (@0))
1894 (if (!VOID_TYPE_P (TREE_TYPE (@2)) || VOID_TYPE_P (type))
1895 @2)
1896 (if (!VOID_TYPE_P (TREE_TYPE (@1)) || VOID_TYPE_P (type))
1897 @1)))
96994de0
RB
1898(simplify
1899 (vec_cond VECTOR_CST@0 @1 @2)
1900 (if (integer_all_onesp (@0))
8fdc6c67
RB
1901 @1
1902 (if (integer_zerop (@0))
1903 @2)))
96994de0
RB
1904
1905(for cnd (cond vec_cond)
1906 /* A ? B : (A ? X : C) -> A ? B : C. */
1907 (simplify
1908 (cnd @0 (cnd @0 @1 @2) @3)
1909 (cnd @0 @1 @3))
1910 (simplify
1911 (cnd @0 @1 (cnd @0 @2 @3))
1912 (cnd @0 @1 @3))
24a179f8
RB
1913 /* A ? B : (!A ? C : X) -> A ? B : C. */
1914 /* ??? This matches embedded conditions open-coded because genmatch
1915 would generate matching code for conditions in separate stmts only.
1916 The following is still important to merge then and else arm cases
1917 from if-conversion. */
1918 (simplify
1919 (cnd @0 @1 (cnd @2 @3 @4))
1920 (if (COMPARISON_CLASS_P (@0)
1921 && COMPARISON_CLASS_P (@2)
1922 && invert_tree_comparison
1923 (TREE_CODE (@0), HONOR_NANS (TREE_OPERAND (@0, 0))) == TREE_CODE (@2)
1924 && operand_equal_p (TREE_OPERAND (@0, 0), TREE_OPERAND (@2, 0), 0)
1925 && operand_equal_p (TREE_OPERAND (@0, 1), TREE_OPERAND (@2, 1), 0))
1926 (cnd @0 @1 @3)))
1927 (simplify
1928 (cnd @0 (cnd @1 @2 @3) @4)
1929 (if (COMPARISON_CLASS_P (@0)
1930 && COMPARISON_CLASS_P (@1)
1931 && invert_tree_comparison
1932 (TREE_CODE (@0), HONOR_NANS (TREE_OPERAND (@0, 0))) == TREE_CODE (@1)
1933 && operand_equal_p (TREE_OPERAND (@0, 0), TREE_OPERAND (@1, 0), 0)
1934 && operand_equal_p (TREE_OPERAND (@0, 1), TREE_OPERAND (@1, 1), 0))
1935 (cnd @0 @3 @4)))
96994de0
RB
1936
1937 /* A ? B : B -> B. */
1938 (simplify
1939 (cnd @0 @1 @1)
09240451 1940 @1)
96994de0 1941
09240451
MG
1942 /* !A ? B : C -> A ? C : B. */
1943 (simplify
1944 (cnd (logical_inverted_value truth_valued_p@0) @1 @2)
1945 (cnd @0 @2 @1)))
f84e7fd6 1946
a3ca1bc5
RB
1947/* A + (B vcmp C ? 1 : 0) -> A - (B vcmp C ? -1 : 0), since vector comparisons
1948 return all -1 or all 0 results. */
f43d102e
RS
1949/* ??? We could instead convert all instances of the vec_cond to negate,
1950 but that isn't necessarily a win on its own. */
1951(simplify
a3ca1bc5 1952 (plus:c @3 (view_convert? (vec_cond:s @0 integer_each_onep@1 integer_zerop@2)))
f43d102e 1953 (if (VECTOR_TYPE_P (type)
4d8989d5 1954 && TYPE_VECTOR_SUBPARTS (type) == TYPE_VECTOR_SUBPARTS (TREE_TYPE (@1))
f43d102e 1955 && (TYPE_MODE (TREE_TYPE (type))
4d8989d5 1956 == TYPE_MODE (TREE_TYPE (TREE_TYPE (@1)))))
a3ca1bc5 1957 (minus @3 (view_convert (vec_cond @0 (negate @1) @2)))))
f43d102e 1958
a3ca1bc5 1959/* ... likewise A - (B vcmp C ? 1 : 0) -> A + (B vcmp C ? -1 : 0). */
f43d102e 1960(simplify
a3ca1bc5 1961 (minus @3 (view_convert? (vec_cond:s @0 integer_each_onep@1 integer_zerop@2)))
f43d102e 1962 (if (VECTOR_TYPE_P (type)
4d8989d5 1963 && TYPE_VECTOR_SUBPARTS (type) == TYPE_VECTOR_SUBPARTS (TREE_TYPE (@1))
f43d102e 1964 && (TYPE_MODE (TREE_TYPE (type))
4d8989d5 1965 == TYPE_MODE (TREE_TYPE (TREE_TYPE (@1)))))
a3ca1bc5 1966 (plus @3 (view_convert (vec_cond @0 (negate @1) @2)))))
f84e7fd6 1967
2ee05f1e 1968
f84e7fd6
RB
1969/* Simplifications of comparisons. */
1970
24f1db9c
RB
1971/* See if we can reduce the magnitude of a constant involved in a
1972 comparison by changing the comparison code. This is a canonicalization
1973 formerly done by maybe_canonicalize_comparison_1. */
1974(for cmp (le gt)
1975 acmp (lt ge)
1976 (simplify
1977 (cmp @0 INTEGER_CST@1)
1978 (if (tree_int_cst_sgn (@1) == -1)
1979 (acmp @0 { wide_int_to_tree (TREE_TYPE (@1), wi::add (@1, 1)); }))))
1980(for cmp (ge lt)
1981 acmp (gt le)
1982 (simplify
1983 (cmp @0 INTEGER_CST@1)
1984 (if (tree_int_cst_sgn (@1) == 1)
1985 (acmp @0 { wide_int_to_tree (TREE_TYPE (@1), wi::sub (@1, 1)); }))))
1986
1987
f84e7fd6
RB
1988/* We can simplify a logical negation of a comparison to the
1989 inverted comparison. As we cannot compute an expression
1990 operator using invert_tree_comparison we have to simulate
1991 that with expression code iteration. */
1992(for cmp (tcc_comparison)
1993 icmp (inverted_tcc_comparison)
1994 ncmp (inverted_tcc_comparison_with_nans)
1995 /* Ideally we'd like to combine the following two patterns
1996 and handle some more cases by using
1997 (logical_inverted_value (cmp @0 @1))
1998 here but for that genmatch would need to "inline" that.
1999 For now implement what forward_propagate_comparison did. */
2000 (simplify
2001 (bit_not (cmp @0 @1))
2002 (if (VECTOR_TYPE_P (type)
2003 || (INTEGRAL_TYPE_P (type) && TYPE_PRECISION (type) == 1))
2004 /* Comparison inversion may be impossible for trapping math,
2005 invert_tree_comparison will tell us. But we can't use
2006 a computed operator in the replacement tree thus we have
2007 to play the trick below. */
2008 (with { enum tree_code ic = invert_tree_comparison
1b457aa4 2009 (cmp, HONOR_NANS (@0)); }
f84e7fd6 2010 (if (ic == icmp)
8fdc6c67
RB
2011 (icmp @0 @1)
2012 (if (ic == ncmp)
2013 (ncmp @0 @1))))))
f84e7fd6 2014 (simplify
09240451
MG
2015 (bit_xor (cmp @0 @1) integer_truep)
2016 (with { enum tree_code ic = invert_tree_comparison
1b457aa4 2017 (cmp, HONOR_NANS (@0)); }
09240451 2018 (if (ic == icmp)
8fdc6c67
RB
2019 (icmp @0 @1)
2020 (if (ic == ncmp)
2021 (ncmp @0 @1))))))
e18c1d66 2022
2ee05f1e
RB
2023/* Transform comparisons of the form X - Y CMP 0 to X CMP Y.
2024 ??? The transformation is valid for the other operators if overflow
2025 is undefined for the type, but performing it here badly interacts
2026 with the transformation in fold_cond_expr_with_comparison which
2027 attempts to synthetize ABS_EXPR. */
2028(for cmp (eq ne)
2029 (simplify
d9ba1961
RB
2030 (cmp (minus@2 @0 @1) integer_zerop)
2031 (if (single_use (@2))
2032 (cmp @0 @1))))
2ee05f1e
RB
2033
2034/* Transform comparisons of the form X * C1 CMP 0 to X CMP 0 in the
2035 signed arithmetic case. That form is created by the compiler
2036 often enough for folding it to be of value. One example is in
2037 computing loop trip counts after Operator Strength Reduction. */
07cdc2b8
RB
2038(for cmp (simple_comparison)
2039 scmp (swapped_simple_comparison)
2ee05f1e 2040 (simplify
bc6e9db4 2041 (cmp (mult@3 @0 INTEGER_CST@1) integer_zerop@2)
2ee05f1e
RB
2042 /* Handle unfolded multiplication by zero. */
2043 (if (integer_zerop (@1))
8fdc6c67
RB
2044 (cmp @1 @2)
2045 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
bc6e9db4
RB
2046 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
2047 && single_use (@3))
8fdc6c67
RB
2048 /* If @1 is negative we swap the sense of the comparison. */
2049 (if (tree_int_cst_sgn (@1) < 0)
2050 (scmp @0 @2)
2051 (cmp @0 @2))))))
2ee05f1e
RB
2052
2053/* Simplify comparison of something with itself. For IEEE
2054 floating-point, we can only do some of these simplifications. */
287f8f17 2055(for cmp (eq ge le)
2ee05f1e
RB
2056 (simplify
2057 (cmp @0 @0)
287f8f17 2058 (if (! FLOAT_TYPE_P (TREE_TYPE (@0))
b9407883 2059 || ! HONOR_NANS (@0))
287f8f17
RB
2060 { constant_boolean_node (true, type); }
2061 (if (cmp != EQ_EXPR)
2062 (eq @0 @0)))))
2ee05f1e
RB
2063(for cmp (ne gt lt)
2064 (simplify
2065 (cmp @0 @0)
2066 (if (cmp != NE_EXPR
2067 || ! FLOAT_TYPE_P (TREE_TYPE (@0))
b9407883 2068 || ! HONOR_NANS (@0))
2ee05f1e 2069 { constant_boolean_node (false, type); })))
b5d3d787
RB
2070(for cmp (unle unge uneq)
2071 (simplify
2072 (cmp @0 @0)
2073 { constant_boolean_node (true, type); }))
dd53d197
MG
2074(for cmp (unlt ungt)
2075 (simplify
2076 (cmp @0 @0)
2077 (unordered @0 @0)))
b5d3d787
RB
2078(simplify
2079 (ltgt @0 @0)
2080 (if (!flag_trapping_math)
2081 { constant_boolean_node (false, type); }))
2ee05f1e
RB
2082
2083/* Fold ~X op ~Y as Y op X. */
07cdc2b8 2084(for cmp (simple_comparison)
2ee05f1e 2085 (simplify
7fe996ba
RB
2086 (cmp (bit_not@2 @0) (bit_not@3 @1))
2087 (if (single_use (@2) && single_use (@3))
2088 (cmp @1 @0))))
2ee05f1e
RB
2089
2090/* Fold ~X op C as X op' ~C, where op' is the swapped comparison. */
07cdc2b8
RB
2091(for cmp (simple_comparison)
2092 scmp (swapped_simple_comparison)
2ee05f1e 2093 (simplify
7fe996ba
RB
2094 (cmp (bit_not@2 @0) CONSTANT_CLASS_P@1)
2095 (if (single_use (@2)
2096 && (TREE_CODE (@1) == INTEGER_CST || TREE_CODE (@1) == VECTOR_CST))
2ee05f1e
RB
2097 (scmp @0 (bit_not @1)))))
2098
07cdc2b8
RB
2099(for cmp (simple_comparison)
2100 /* Fold (double)float1 CMP (double)float2 into float1 CMP float2. */
2101 (simplify
2102 (cmp (convert@2 @0) (convert? @1))
2103 (if (FLOAT_TYPE_P (TREE_TYPE (@0))
2104 && (DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@2))
2105 == DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@0)))
2106 && (DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@2))
2107 == DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@1))))
2108 (with
2109 {
2110 tree type1 = TREE_TYPE (@1);
2111 if (TREE_CODE (@1) == REAL_CST && !DECIMAL_FLOAT_TYPE_P (type1))
2112 {
2113 REAL_VALUE_TYPE orig = TREE_REAL_CST (@1);
2114 if (TYPE_PRECISION (type1) > TYPE_PRECISION (float_type_node)
2115 && exact_real_truncate (TYPE_MODE (float_type_node), &orig))
2116 type1 = float_type_node;
2117 if (TYPE_PRECISION (type1) > TYPE_PRECISION (double_type_node)
2118 && exact_real_truncate (TYPE_MODE (double_type_node), &orig))
2119 type1 = double_type_node;
2120 }
2121 tree newtype
2122 = (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (type1)
2123 ? TREE_TYPE (@0) : type1);
2124 }
2125 (if (TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (newtype))
2126 (cmp (convert:newtype @0) (convert:newtype @1))))))
2127
2128 (simplify
2129 (cmp @0 REAL_CST@1)
2130 /* IEEE doesn't distinguish +0 and -0 in comparisons. */
64d3a1f0
RB
2131 (switch
2132 /* a CMP (-0) -> a CMP 0 */
2133 (if (REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (@1)))
2134 (cmp @0 { build_real (TREE_TYPE (@1), dconst0); }))
2135 /* x != NaN is always true, other ops are always false. */
2136 (if (REAL_VALUE_ISNAN (TREE_REAL_CST (@1))
2137 && ! HONOR_SNANS (@1))
2138 { constant_boolean_node (cmp == NE_EXPR, type); })
2139 /* Fold comparisons against infinity. */
2140 (if (REAL_VALUE_ISINF (TREE_REAL_CST (@1))
2141 && MODE_HAS_INFINITIES (TYPE_MODE (TREE_TYPE (@1))))
2142 (with
2143 {
2144 REAL_VALUE_TYPE max;
2145 enum tree_code code = cmp;
2146 bool neg = REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1));
2147 if (neg)
2148 code = swap_tree_comparison (code);
2149 }
2150 (switch
2151 /* x > +Inf is always false, if with ignore sNANs. */
2152 (if (code == GT_EXPR
2153 && ! HONOR_SNANS (@0))
2154 { constant_boolean_node (false, type); })
2155 (if (code == LE_EXPR)
2156 /* x <= +Inf is always true, if we don't case about NaNs. */
2157 (if (! HONOR_NANS (@0))
2158 { constant_boolean_node (true, type); }
b0eb889b 2159 /* x <= +Inf is the same as x == x, i.e. !isnan(x). */
64d3a1f0
RB
2160 (eq @0 @0)))
2161 /* x == +Inf and x >= +Inf are always equal to x > DBL_MAX. */
2162 (if (code == EQ_EXPR || code == GE_EXPR)
2163 (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
2164 (if (neg)
2165 (lt @0 { build_real (TREE_TYPE (@0), max); })
2166 (gt @0 { build_real (TREE_TYPE (@0), max); }))))
2167 /* x < +Inf is always equal to x <= DBL_MAX. */
2168 (if (code == LT_EXPR)
2169 (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
2170 (if (neg)
2171 (ge @0 { build_real (TREE_TYPE (@0), max); })
2172 (le @0 { build_real (TREE_TYPE (@0), max); }))))
2173 /* x != +Inf is always equal to !(x > DBL_MAX). */
2174 (if (code == NE_EXPR)
2175 (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
2176 (if (! HONOR_NANS (@0))
2177 (if (neg)
2178 (ge @0 { build_real (TREE_TYPE (@0), max); })
2179 (le @0 { build_real (TREE_TYPE (@0), max); }))
2180 (if (neg)
2181 (bit_xor (lt @0 { build_real (TREE_TYPE (@0), max); })
2182 { build_one_cst (type); })
2183 (bit_xor (gt @0 { build_real (TREE_TYPE (@0), max); })
2184 { build_one_cst (type); }))))))))))
07cdc2b8
RB
2185
2186 /* If this is a comparison of a real constant with a PLUS_EXPR
2187 or a MINUS_EXPR of a real constant, we can convert it into a
2188 comparison with a revised real constant as long as no overflow
2189 occurs when unsafe_math_optimizations are enabled. */
2190 (if (flag_unsafe_math_optimizations)
2191 (for op (plus minus)
2192 (simplify
2193 (cmp (op @0 REAL_CST@1) REAL_CST@2)
2194 (with
2195 {
2196 tree tem = const_binop (op == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR,
2197 TREE_TYPE (@1), @2, @1);
2198 }
f980c9a2 2199 (if (tem && !TREE_OVERFLOW (tem))
07cdc2b8
RB
2200 (cmp @0 { tem; }))))))
2201
2202 /* Likewise, we can simplify a comparison of a real constant with
2203 a MINUS_EXPR whose first operand is also a real constant, i.e.
2204 (c1 - x) < c2 becomes x > c1-c2. Reordering is allowed on
2205 floating-point types only if -fassociative-math is set. */
2206 (if (flag_associative_math)
2207 (simplify
0409237b 2208 (cmp (minus REAL_CST@0 @1) REAL_CST@2)
07cdc2b8 2209 (with { tree tem = const_binop (MINUS_EXPR, TREE_TYPE (@1), @0, @2); }
f980c9a2 2210 (if (tem && !TREE_OVERFLOW (tem))
07cdc2b8
RB
2211 (cmp { tem; } @1)))))
2212
2213 /* Fold comparisons against built-in math functions. */
2214 (if (flag_unsafe_math_optimizations
2215 && ! flag_errno_math)
2216 (for sq (SQRT)
2217 (simplify
2218 (cmp (sq @0) REAL_CST@1)
64d3a1f0
RB
2219 (switch
2220 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1)))
2221 (switch
2222 /* sqrt(x) < y is always false, if y is negative. */
2223 (if (cmp == EQ_EXPR || cmp == LT_EXPR || cmp == LE_EXPR)
8fdc6c67 2224 { constant_boolean_node (false, type); })
64d3a1f0
RB
2225 /* sqrt(x) > y is always true, if y is negative and we
2226 don't care about NaNs, i.e. negative values of x. */
2227 (if (cmp == NE_EXPR || !HONOR_NANS (@0))
2228 { constant_boolean_node (true, type); })
2229 /* sqrt(x) > y is the same as x >= 0, if y is negative. */
2230 (ge @0 { build_real (TREE_TYPE (@0), dconst0); })))
c53233c6
RS
2231 (if (real_equal (TREE_REAL_CST_PTR (@1), &dconst0))
2232 (switch
2233 /* sqrt(x) < 0 is always false. */
2234 (if (cmp == LT_EXPR)
2235 { constant_boolean_node (false, type); })
2236 /* sqrt(x) >= 0 is always true if we don't care about NaNs. */
2237 (if (cmp == GE_EXPR && !HONOR_NANS (@0))
2238 { constant_boolean_node (true, type); })
2239 /* sqrt(x) <= 0 -> x == 0. */
2240 (if (cmp == LE_EXPR)
2241 (eq @0 @1))
2242 /* Otherwise sqrt(x) cmp 0 -> x cmp 0. Here cmp can be >=, >,
2243 == or !=. In the last case:
2244
2245 (sqrt(x) != 0) == (NaN != 0) == true == (x != 0)
2246
2247 if x is negative or NaN. Due to -funsafe-math-optimizations,
2248 the results for other x follow from natural arithmetic. */
2249 (cmp @0 @1)))
64d3a1f0
RB
2250 (if (cmp == GT_EXPR || cmp == GE_EXPR)
2251 (with
2252 {
2253 REAL_VALUE_TYPE c2;
5c88ea94
RS
2254 real_arithmetic (&c2, MULT_EXPR,
2255 &TREE_REAL_CST (@1), &TREE_REAL_CST (@1));
64d3a1f0
RB
2256 real_convert (&c2, TYPE_MODE (TREE_TYPE (@0)), &c2);
2257 }
2258 (if (REAL_VALUE_ISINF (c2))
2259 /* sqrt(x) > y is x == +Inf, when y is very large. */
2260 (if (HONOR_INFINITIES (@0))
2261 (eq @0 { build_real (TREE_TYPE (@0), c2); })
2262 { constant_boolean_node (false, type); })
2263 /* sqrt(x) > c is the same as x > c*c. */
2264 (cmp @0 { build_real (TREE_TYPE (@0), c2); }))))
2265 (if (cmp == LT_EXPR || cmp == LE_EXPR)
2266 (with
2267 {
2268 REAL_VALUE_TYPE c2;
5c88ea94
RS
2269 real_arithmetic (&c2, MULT_EXPR,
2270 &TREE_REAL_CST (@1), &TREE_REAL_CST (@1));
64d3a1f0
RB
2271 real_convert (&c2, TYPE_MODE (TREE_TYPE (@0)), &c2);
2272 }
2273 (if (REAL_VALUE_ISINF (c2))
2274 (switch
2275 /* sqrt(x) < y is always true, when y is a very large
2276 value and we don't care about NaNs or Infinities. */
2277 (if (! HONOR_NANS (@0) && ! HONOR_INFINITIES (@0))
2278 { constant_boolean_node (true, type); })
2279 /* sqrt(x) < y is x != +Inf when y is very large and we
2280 don't care about NaNs. */
2281 (if (! HONOR_NANS (@0))
2282 (ne @0 { build_real (TREE_TYPE (@0), c2); }))
2283 /* sqrt(x) < y is x >= 0 when y is very large and we
2284 don't care about Infinities. */
2285 (if (! HONOR_INFINITIES (@0))
2286 (ge @0 { build_real (TREE_TYPE (@0), dconst0); }))
2287 /* sqrt(x) < y is x >= 0 && x != +Inf, when y is large. */
2288 (if (GENERIC)
2289 (truth_andif
2290 (ge @0 { build_real (TREE_TYPE (@0), dconst0); })
2291 (ne @0 { build_real (TREE_TYPE (@0), c2); }))))
2292 /* sqrt(x) < c is the same as x < c*c, if we ignore NaNs. */
2293 (if (! HONOR_NANS (@0))
2294 (cmp @0 { build_real (TREE_TYPE (@0), c2); })
2295 /* sqrt(x) < c is the same as x >= 0 && x < c*c. */
2296 (if (GENERIC)
2297 (truth_andif
2298 (ge @0 { build_real (TREE_TYPE (@0), dconst0); })
2299 (cmp @0 { build_real (TREE_TYPE (@0), c2); }))))))))))))
2ee05f1e 2300
cfdc4f33
MG
2301/* Unordered tests if either argument is a NaN. */
2302(simplify
2303 (bit_ior (unordered @0 @0) (unordered @1 @1))
aea417d7 2304 (if (types_match (@0, @1))
cfdc4f33 2305 (unordered @0 @1)))
257b01ba
MG
2306(simplify
2307 (bit_and (ordered @0 @0) (ordered @1 @1))
2308 (if (types_match (@0, @1))
2309 (ordered @0 @1)))
cfdc4f33
MG
2310(simplify
2311 (bit_ior:c (unordered @0 @0) (unordered:c@2 @0 @1))
2312 @2)
257b01ba
MG
2313(simplify
2314 (bit_and:c (ordered @0 @0) (ordered:c@2 @0 @1))
2315 @2)
e18c1d66 2316
90c6f26c
RB
2317/* Simple range test simplifications. */
2318/* A < B || A >= B -> true. */
5d30c58d
RB
2319(for test1 (lt le le le ne ge)
2320 test2 (ge gt ge ne eq ne)
90c6f26c
RB
2321 (simplify
2322 (bit_ior:c (test1 @0 @1) (test2 @0 @1))
2323 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
2324 || VECTOR_INTEGER_TYPE_P (TREE_TYPE (@0)))
2325 { constant_boolean_node (true, type); })))
2326/* A < B && A >= B -> false. */
2327(for test1 (lt lt lt le ne eq)
2328 test2 (ge gt eq gt eq gt)
2329 (simplify
2330 (bit_and:c (test1 @0 @1) (test2 @0 @1))
2331 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
2332 || VECTOR_INTEGER_TYPE_P (TREE_TYPE (@0)))
2333 { constant_boolean_node (false, type); })))
2334
534bd33b
MG
2335/* -A CMP -B -> B CMP A. */
2336(for cmp (tcc_comparison)
2337 scmp (swapped_tcc_comparison)
2338 (simplify
2339 (cmp (negate @0) (negate @1))
2340 (if (FLOAT_TYPE_P (TREE_TYPE (@0))
2341 || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2342 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))))
2343 (scmp @0 @1)))
2344 (simplify
2345 (cmp (negate @0) CONSTANT_CLASS_P@1)
2346 (if (FLOAT_TYPE_P (TREE_TYPE (@0))
2347 || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2348 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))))
23f27839 2349 (with { tree tem = const_unop (NEGATE_EXPR, TREE_TYPE (@0), @1); }
534bd33b
MG
2350 (if (tem && !TREE_OVERFLOW (tem))
2351 (scmp @0 { tem; }))))))
2352
b0eb889b
MG
2353/* Convert ABS_EXPR<x> == 0 or ABS_EXPR<x> != 0 to x == 0 or x != 0. */
2354(for op (eq ne)
2355 (simplify
2356 (op (abs @0) zerop@1)
2357 (op @0 @1)))
2358
79d4f7c6
RB
2359/* From fold_sign_changed_comparison and fold_widened_comparison. */
2360(for cmp (simple_comparison)
2361 (simplify
2362 (cmp (convert@0 @00) (convert?@1 @10))
452ec2a5 2363 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
79d4f7c6
RB
2364 /* Disable this optimization if we're casting a function pointer
2365 type on targets that require function pointer canonicalization. */
2366 && !(targetm.have_canonicalize_funcptr_for_compare ()
2367 && TREE_CODE (TREE_TYPE (@00)) == POINTER_TYPE
2fde61e3
RB
2368 && TREE_CODE (TREE_TYPE (TREE_TYPE (@00))) == FUNCTION_TYPE)
2369 && single_use (@0))
79d4f7c6
RB
2370 (if (TYPE_PRECISION (TREE_TYPE (@00)) == TYPE_PRECISION (TREE_TYPE (@0))
2371 && (TREE_CODE (@10) == INTEGER_CST
2372 || (@1 != @10 && types_match (TREE_TYPE (@10), TREE_TYPE (@00))))
2373 && (TYPE_UNSIGNED (TREE_TYPE (@00)) == TYPE_UNSIGNED (TREE_TYPE (@0))
2374 || cmp == NE_EXPR
2375 || cmp == EQ_EXPR)
2376 && (POINTER_TYPE_P (TREE_TYPE (@00)) == POINTER_TYPE_P (TREE_TYPE (@0))))
2377 /* ??? The special-casing of INTEGER_CST conversion was in the original
2378 code and here to avoid a spurious overflow flag on the resulting
2379 constant which fold_convert produces. */
2380 (if (TREE_CODE (@1) == INTEGER_CST)
2381 (cmp @00 { force_fit_type (TREE_TYPE (@00), wi::to_widest (@1), 0,
2382 TREE_OVERFLOW (@1)); })
2383 (cmp @00 (convert @1)))
2384
2385 (if (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (TREE_TYPE (@00)))
2386 /* If possible, express the comparison in the shorter mode. */
2387 (if ((cmp == EQ_EXPR || cmp == NE_EXPR
7fd82d52
PP
2388 || TYPE_UNSIGNED (TREE_TYPE (@0)) == TYPE_UNSIGNED (TREE_TYPE (@00))
2389 || (!TYPE_UNSIGNED (TREE_TYPE (@0))
2390 && TYPE_UNSIGNED (TREE_TYPE (@00))))
79d4f7c6
RB
2391 && (types_match (TREE_TYPE (@10), TREE_TYPE (@00))
2392 || ((TYPE_PRECISION (TREE_TYPE (@00))
2393 >= TYPE_PRECISION (TREE_TYPE (@10)))
2394 && (TYPE_UNSIGNED (TREE_TYPE (@00))
2395 == TYPE_UNSIGNED (TREE_TYPE (@10))))
2396 || (TREE_CODE (@10) == INTEGER_CST
f6c15759 2397 && INTEGRAL_TYPE_P (TREE_TYPE (@00))
79d4f7c6
RB
2398 && int_fits_type_p (@10, TREE_TYPE (@00)))))
2399 (cmp @00 (convert @10))
2400 (if (TREE_CODE (@10) == INTEGER_CST
f6c15759 2401 && INTEGRAL_TYPE_P (TREE_TYPE (@00))
79d4f7c6
RB
2402 && !int_fits_type_p (@10, TREE_TYPE (@00)))
2403 (with
2404 {
2405 tree min = lower_bound_in_type (TREE_TYPE (@10), TREE_TYPE (@00));
2406 tree max = upper_bound_in_type (TREE_TYPE (@10), TREE_TYPE (@00));
2407 bool above = integer_nonzerop (const_binop (LT_EXPR, type, max, @10));
2408 bool below = integer_nonzerop (const_binop (LT_EXPR, type, @10, min));
2409 }
2410 (if (above || below)
2411 (if (cmp == EQ_EXPR || cmp == NE_EXPR)
2412 { constant_boolean_node (cmp == EQ_EXPR ? false : true, type); }
2413 (if (cmp == LT_EXPR || cmp == LE_EXPR)
2414 { constant_boolean_node (above ? true : false, type); }
2415 (if (cmp == GT_EXPR || cmp == GE_EXPR)
2416 { constant_boolean_node (above ? false : true, type); }))))))))))))
66e1cacf 2417
96a111a3
RB
2418(for cmp (eq ne)
2419 /* A local variable can never be pointed to by
2420 the default SSA name of an incoming parameter.
2421 SSA names are canonicalized to 2nd place. */
2422 (simplify
2423 (cmp addr@0 SSA_NAME@1)
2424 (if (SSA_NAME_IS_DEFAULT_DEF (@1)
2425 && TREE_CODE (SSA_NAME_VAR (@1)) == PARM_DECL)
2426 (with { tree base = get_base_address (TREE_OPERAND (@0, 0)); }
2427 (if (TREE_CODE (base) == VAR_DECL
2428 && auto_var_in_fn_p (base, current_function_decl))
2429 (if (cmp == NE_EXPR)
2430 { constant_boolean_node (true, type); }
2431 { constant_boolean_node (false, type); }))))))
2432
66e1cacf
RB
2433/* Equality compare simplifications from fold_binary */
2434(for cmp (eq ne)
2435
2436 /* If we have (A | C) == D where C & ~D != 0, convert this into 0.
2437 Similarly for NE_EXPR. */
2438 (simplify
2439 (cmp (convert?@3 (bit_ior @0 INTEGER_CST@1)) INTEGER_CST@2)
2440 (if (tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@0))
2441 && wi::bit_and_not (@1, @2) != 0)
2442 { constant_boolean_node (cmp == NE_EXPR, type); }))
2443
2444 /* (X ^ Y) == 0 becomes X == Y, and (X ^ Y) != 0 becomes X != Y. */
2445 (simplify
2446 (cmp (bit_xor @0 @1) integer_zerop)
2447 (cmp @0 @1))
2448
2449 /* (X ^ Y) == Y becomes X == 0.
2450 Likewise (X ^ Y) == X becomes Y == 0. */
2451 (simplify
99e943a2 2452 (cmp:c (bit_xor:c @0 @1) @0)
66e1cacf
RB
2453 (cmp @1 { build_zero_cst (TREE_TYPE (@1)); }))
2454
2455 /* (X ^ C1) op C2 can be rewritten as X op (C1 ^ C2). */
2456 (simplify
2457 (cmp (convert?@3 (bit_xor @0 INTEGER_CST@1)) INTEGER_CST@2)
2458 (if (tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@0)))
d057c866 2459 (cmp @0 (bit_xor @1 (convert @2)))))
d057c866
RB
2460
2461 (simplify
2462 (cmp (convert? addr@0) integer_zerop)
2463 (if (tree_single_nonzero_warnv_p (@0, NULL))
2464 { constant_boolean_node (cmp == NE_EXPR, type); })))
2465
b0eb889b
MG
2466/* If we have (A & C) == C where C is a power of 2, convert this into
2467 (A & C) != 0. Similarly for NE_EXPR. */
2468(for cmp (eq ne)
2469 icmp (ne eq)
2470 (simplify
2471 (cmp (bit_and@2 @0 integer_pow2p@1) @1)
2472 (icmp @2 { build_zero_cst (TREE_TYPE (@0)); })))
2473
2474/* If we have (A & C) != 0 where C is the sign bit of A, convert
2475 this into A < 0. Similarly for (A & C) == 0 into A >= 0. */
2476(for cmp (eq ne)
2477 ncmp (ge lt)
2478 (simplify
2479 (cmp (bit_and (convert?@2 @0) integer_pow2p@1) integer_zerop)
2480 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
2481 && (TYPE_PRECISION (TREE_TYPE (@0))
2482 == GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (@0))))
2483 && element_precision (@2) >= element_precision (@0)
2484 && wi::only_sign_bit_p (@1, element_precision (@0)))
2485 (with { tree stype = signed_type_for (TREE_TYPE (@0)); }
2486 (ncmp (convert:stype @0) { build_zero_cst (stype); })))))
2487
68aba1f6
RB
2488/* When the addresses are not directly of decls compare base and offset.
2489 This implements some remaining parts of fold_comparison address
2490 comparisons but still no complete part of it. Still it is good
2491 enough to make fold_stmt not regress when not dispatching to fold_binary. */
2492(for cmp (simple_comparison)
2493 (simplify
f501d5cd 2494 (cmp (convert1?@2 addr@0) (convert2? addr@1))
68aba1f6
RB
2495 (with
2496 {
2497 HOST_WIDE_INT off0, off1;
2498 tree base0 = get_addr_base_and_unit_offset (TREE_OPERAND (@0, 0), &off0);
2499 tree base1 = get_addr_base_and_unit_offset (TREE_OPERAND (@1, 0), &off1);
2500 if (base0 && TREE_CODE (base0) == MEM_REF)
2501 {
2502 off0 += mem_ref_offset (base0).to_short_addr ();
2503 base0 = TREE_OPERAND (base0, 0);
2504 }
2505 if (base1 && TREE_CODE (base1) == MEM_REF)
2506 {
2507 off1 += mem_ref_offset (base1).to_short_addr ();
2508 base1 = TREE_OPERAND (base1, 0);
2509 }
2510 }
da571fda
RB
2511 (if (base0 && base1)
2512 (with
2513 {
aad88aed 2514 int equal = 2;
da571fda
RB
2515 if (decl_in_symtab_p (base0)
2516 && decl_in_symtab_p (base1))
2517 equal = symtab_node::get_create (base0)
2518 ->equal_address_to (symtab_node::get_create (base1));
c3bea076
RB
2519 else if ((DECL_P (base0)
2520 || TREE_CODE (base0) == SSA_NAME
2521 || TREE_CODE (base0) == STRING_CST)
2522 && (DECL_P (base1)
2523 || TREE_CODE (base1) == SSA_NAME
2524 || TREE_CODE (base1) == STRING_CST))
aad88aed 2525 equal = (base0 == base1);
da571fda
RB
2526 }
2527 (if (equal == 1
2528 && (cmp == EQ_EXPR || cmp == NE_EXPR
2529 /* If the offsets are equal we can ignore overflow. */
2530 || off0 == off1
2531 || POINTER_TYPE_OVERFLOW_UNDEFINED
c3bea076 2532 /* Or if we compare using pointers to decls or strings. */
da571fda 2533 || (POINTER_TYPE_P (TREE_TYPE (@2))
c3bea076 2534 && (DECL_P (base0) || TREE_CODE (base0) == STRING_CST))))
da571fda
RB
2535 (switch
2536 (if (cmp == EQ_EXPR)
2537 { constant_boolean_node (off0 == off1, type); })
2538 (if (cmp == NE_EXPR)
2539 { constant_boolean_node (off0 != off1, type); })
2540 (if (cmp == LT_EXPR)
2541 { constant_boolean_node (off0 < off1, type); })
2542 (if (cmp == LE_EXPR)
2543 { constant_boolean_node (off0 <= off1, type); })
2544 (if (cmp == GE_EXPR)
2545 { constant_boolean_node (off0 >= off1, type); })
2546 (if (cmp == GT_EXPR)
2547 { constant_boolean_node (off0 > off1, type); }))
2548 (if (equal == 0
2549 && DECL_P (base0) && DECL_P (base1)
2550 /* If we compare this as integers require equal offset. */
2551 && (!INTEGRAL_TYPE_P (TREE_TYPE (@2))
2552 || off0 == off1))
2553 (switch
2554 (if (cmp == EQ_EXPR)
2555 { constant_boolean_node (false, type); })
2556 (if (cmp == NE_EXPR)
2557 { constant_boolean_node (true, type); })))))))))
66e1cacf 2558
98998245
RB
2559/* Simplify pointer equality compares using PTA. */
2560(for neeq (ne eq)
2561 (simplify
2562 (neeq @0 @1)
2563 (if (POINTER_TYPE_P (TREE_TYPE (@0))
2564 && ptrs_compare_unequal (@0, @1))
2565 { neeq == EQ_EXPR ? boolean_false_node : boolean_true_node; })))
2566
8f63caf6 2567/* PR70920: Transform (intptr_t)x eq/ne CST to x eq/ne (typeof x) CST.
467719fb
PK
2568 and (typeof ptr_cst) x eq/ne ptr_cst to x eq/ne (typeof x) CST.
2569 Disable the transform if either operand is pointer to function.
2570 This broke pr22051-2.c for arm where function pointer
2571 canonicalizaion is not wanted. */
2572
8f63caf6
RB
2573(for cmp (ne eq)
2574 (simplify
2575 (cmp (convert @0) INTEGER_CST@1)
467719fb
PK
2576 (if ((POINTER_TYPE_P (TREE_TYPE (@0)) && !FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@0)))
2577 && INTEGRAL_TYPE_P (TREE_TYPE (@1)))
2578 || (INTEGRAL_TYPE_P (TREE_TYPE (@0)) && POINTER_TYPE_P (TREE_TYPE (@1))
2579 && !FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@1)))))
8f63caf6
RB
2580 (cmp @0 (convert @1)))))
2581
21aacde4
RB
2582/* Non-equality compare simplifications from fold_binary */
2583(for cmp (lt gt le ge)
2584 /* Comparisons with the highest or lowest possible integer of
2585 the specified precision will have known values. */
2586 (simplify
2587 (cmp (convert?@2 @0) INTEGER_CST@1)
2588 (if ((INTEGRAL_TYPE_P (TREE_TYPE (@1)) || POINTER_TYPE_P (TREE_TYPE (@1)))
2589 && tree_nop_conversion_p (TREE_TYPE (@2), TREE_TYPE (@0)))
2590 (with
2591 {
2592 tree arg1_type = TREE_TYPE (@1);
2593 unsigned int prec = TYPE_PRECISION (arg1_type);
2594 wide_int max = wi::max_value (arg1_type);
2595 wide_int signed_max = wi::max_value (prec, SIGNED);
2596 wide_int min = wi::min_value (arg1_type);
2597 }
2598 (switch
2599 (if (wi::eq_p (@1, max))
2600 (switch
2601 (if (cmp == GT_EXPR)
2602 { constant_boolean_node (false, type); })
2603 (if (cmp == GE_EXPR)
2604 (eq @2 @1))
2605 (if (cmp == LE_EXPR)
2606 { constant_boolean_node (true, type); })
2607 (if (cmp == LT_EXPR)
2608 (ne @2 @1))))
21aacde4
RB
2609 (if (wi::eq_p (@1, min))
2610 (switch
2611 (if (cmp == LT_EXPR)
2612 { constant_boolean_node (false, type); })
2613 (if (cmp == LE_EXPR)
2614 (eq @2 @1))
2615 (if (cmp == GE_EXPR)
2616 { constant_boolean_node (true, type); })
2617 (if (cmp == GT_EXPR)
2618 (ne @2 @1))))
9bc22d19
RB
2619 (if (wi::eq_p (@1, max - 1))
2620 (switch
2621 (if (cmp == GT_EXPR)
2622 (eq @2 { wide_int_to_tree (TREE_TYPE (@1), wi::add (@1, 1)); }))
2623 (if (cmp == LE_EXPR)
2624 (ne @2 { wide_int_to_tree (TREE_TYPE (@1), wi::add (@1, 1)); }))))
21aacde4
RB
2625 (if (wi::eq_p (@1, min + 1))
2626 (switch
2627 (if (cmp == GE_EXPR)
2628 (ne @2 { wide_int_to_tree (TREE_TYPE (@1), wi::sub (@1, 1)); }))
2629 (if (cmp == LT_EXPR)
2630 (eq @2 { wide_int_to_tree (TREE_TYPE (@1), wi::sub (@1, 1)); }))))
2631 (if (wi::eq_p (@1, signed_max)
2632 && TYPE_UNSIGNED (arg1_type)
2633 /* We will flip the signedness of the comparison operator
2634 associated with the mode of @1, so the sign bit is
2635 specified by this mode. Check that @1 is the signed
2636 max associated with this sign bit. */
2637 && prec == GET_MODE_PRECISION (TYPE_MODE (arg1_type))
2638 /* signed_type does not work on pointer types. */
2639 && INTEGRAL_TYPE_P (arg1_type))
2640 /* The following case also applies to X < signed_max+1
2641 and X >= signed_max+1 because previous transformations. */
2642 (if (cmp == LE_EXPR || cmp == GT_EXPR)
2643 (with { tree st = signed_type_for (arg1_type); }
2644 (if (cmp == LE_EXPR)
2645 (ge (convert:st @0) { build_zero_cst (st); })
2646 (lt (convert:st @0) { build_zero_cst (st); }))))))))))
2647
b5d3d787
RB
2648(for cmp (unordered ordered unlt unle ungt unge uneq ltgt)
2649 /* If the second operand is NaN, the result is constant. */
2650 (simplify
2651 (cmp @0 REAL_CST@1)
2652 (if (REAL_VALUE_ISNAN (TREE_REAL_CST (@1))
2653 && (cmp != LTGT_EXPR || ! flag_trapping_math))
50301115 2654 { constant_boolean_node (cmp == ORDERED_EXPR || cmp == LTGT_EXPR
b5d3d787 2655 ? false : true, type); })))
21aacde4 2656
55cf3946
RB
2657/* bool_var != 0 becomes bool_var. */
2658(simplify
b5d3d787 2659 (ne @0 integer_zerop)
55cf3946
RB
2660 (if (TREE_CODE (TREE_TYPE (@0)) == BOOLEAN_TYPE
2661 && types_match (type, TREE_TYPE (@0)))
2662 (non_lvalue @0)))
2663/* bool_var == 1 becomes bool_var. */
2664(simplify
b5d3d787 2665 (eq @0 integer_onep)
55cf3946
RB
2666 (if (TREE_CODE (TREE_TYPE (@0)) == BOOLEAN_TYPE
2667 && types_match (type, TREE_TYPE (@0)))
2668 (non_lvalue @0)))
b5d3d787
RB
2669/* Do not handle
2670 bool_var == 0 becomes !bool_var or
2671 bool_var != 1 becomes !bool_var
2672 here because that only is good in assignment context as long
2673 as we require a tcc_comparison in GIMPLE_CONDs where we'd
2674 replace if (x == 0) with tem = ~x; if (tem != 0) which is
2675 clearly less optimal and which we'll transform again in forwprop. */
55cf3946 2676
ca1206be
MG
2677/* When one argument is a constant, overflow detection can be simplified.
2678 Currently restricted to single use so as not to interfere too much with
2679 ADD_OVERFLOW detection in tree-ssa-math-opts.c.
2680 A + CST CMP A -> A CMP' CST' */
2681(for cmp (lt le ge gt)
2682 out (gt gt le le)
2683 (simplify
a8e9f9a3 2684 (cmp:c (plus@2 @0 INTEGER_CST@1) @0)
ca1206be
MG
2685 (if (TYPE_UNSIGNED (TREE_TYPE (@0))
2686 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))
2687 && wi::ne_p (@1, 0)
2688 && single_use (@2))
2689 (out @0 { wide_int_to_tree (TREE_TYPE (@0), wi::max_value
2690 (TYPE_PRECISION (TREE_TYPE (@0)), UNSIGNED) - @1); }))))
2691
3563f78f
MG
2692/* To detect overflow in unsigned A - B, A < B is simpler than A - B > A.
2693 However, the detection logic for SUB_OVERFLOW in tree-ssa-math-opts.c
2694 expects the long form, so we restrict the transformation for now. */
2695(for cmp (gt le)
2696 (simplify
a8e9f9a3 2697 (cmp:c (minus@2 @0 @1) @0)
3563f78f
MG
2698 (if (single_use (@2)
2699 && ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2700 && TYPE_UNSIGNED (TREE_TYPE (@0))
2701 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
2702 (cmp @1 @0))))
3563f78f
MG
2703
2704/* Testing for overflow is unnecessary if we already know the result. */
3563f78f
MG
2705/* A - B > A */
2706(for cmp (gt le)
2707 out (ne eq)
2708 (simplify
a8e9f9a3 2709 (cmp:c (realpart (IFN_SUB_OVERFLOW@2 @0 @1)) @0)
3563f78f
MG
2710 (if (TYPE_UNSIGNED (TREE_TYPE (@0))
2711 && types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
2712 (out (imagpart @2) { build_zero_cst (TREE_TYPE (@0)); }))))
2713/* A + B < A */
2714(for cmp (lt ge)
2715 out (ne eq)
2716 (simplify
a8e9f9a3 2717 (cmp:c (realpart (IFN_ADD_OVERFLOW:c@2 @0 @1)) @0)
3563f78f
MG
2718 (if (TYPE_UNSIGNED (TREE_TYPE (@0))
2719 && types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
2720 (out (imagpart @2) { build_zero_cst (TREE_TYPE (@0)); }))))
2721
603aeb87 2722/* For unsigned operands, -1 / B < A checks whether A * B would overflow.
0557293f 2723 Simplify it to __builtin_mul_overflow (A, B, <unused>). */
0557293f
AM
2724(for cmp (lt ge)
2725 out (ne eq)
2726 (simplify
603aeb87 2727 (cmp:c (trunc_div:s integer_all_onesp @1) @0)
0557293f
AM
2728 (if (TYPE_UNSIGNED (TREE_TYPE (@0)) && !VECTOR_TYPE_P (TREE_TYPE (@0)))
2729 (with { tree t = TREE_TYPE (@0), cpx = build_complex_type (t); }
2730 (out (imagpart (IFN_MUL_OVERFLOW:cpx @0 @1)) { build_zero_cst (t); })))))
55cf3946 2731
53f3cd25
RS
2732/* Simplification of math builtins. These rules must all be optimizations
2733 as well as IL simplifications. If there is a possibility that the new
2734 form could be a pessimization, the rule should go in the canonicalization
2735 section that follows this one.
e18c1d66 2736
53f3cd25
RS
2737 Rules can generally go in this section if they satisfy one of
2738 the following:
2739
2740 - the rule describes an identity
2741
2742 - the rule replaces calls with something as simple as addition or
2743 multiplication
2744
2745 - the rule contains unary calls only and simplifies the surrounding
2746 arithmetic. (The idea here is to exclude non-unary calls in which
2747 one operand is constant and in which the call is known to be cheap
2748 when the operand has that value.) */
52c6378a 2749
53f3cd25 2750(if (flag_unsafe_math_optimizations)
52c6378a
N
2751 /* Simplify sqrt(x) * sqrt(x) -> x. */
2752 (simplify
2753 (mult (SQRT@1 @0) @1)
2754 (if (!HONOR_SNANS (type))
2755 @0))
2756
35401640
N
2757 /* Simplify sqrt(x) * sqrt(y) -> sqrt(x*y). */
2758 (for root (SQRT CBRT)
2759 (simplify
2760 (mult (root:s @0) (root:s @1))
2761 (root (mult @0 @1))))
2762
35401640
N
2763 /* Simplify expN(x) * expN(y) -> expN(x+y). */
2764 (for exps (EXP EXP2 EXP10 POW10)
2765 (simplify
2766 (mult (exps:s @0) (exps:s @1))
2767 (exps (plus @0 @1))))
2768
52c6378a 2769 /* Simplify a/root(b/c) into a*root(c/b). */
35401640
N
2770 (for root (SQRT CBRT)
2771 (simplify
2772 (rdiv @0 (root:s (rdiv:s @1 @2)))
2773 (mult @0 (root (rdiv @2 @1)))))
2774
2775 /* Simplify x/expN(y) into x*expN(-y). */
2776 (for exps (EXP EXP2 EXP10 POW10)
2777 (simplify
2778 (rdiv @0 (exps:s @1))
2779 (mult @0 (exps (negate @1)))))
52c6378a 2780
eee7b6c4
RB
2781 (for logs (LOG LOG2 LOG10 LOG10)
2782 exps (EXP EXP2 EXP10 POW10)
8acda9b2 2783 /* logN(expN(x)) -> x. */
e18c1d66
RB
2784 (simplify
2785 (logs (exps @0))
8acda9b2
RS
2786 @0)
2787 /* expN(logN(x)) -> x. */
2788 (simplify
2789 (exps (logs @0))
2790 @0))
53f3cd25 2791
e18c1d66
RB
2792 /* Optimize logN(func()) for various exponential functions. We
2793 want to determine the value "x" and the power "exponent" in
2794 order to transform logN(x**exponent) into exponent*logN(x). */
eee7b6c4
RB
2795 (for logs (LOG LOG LOG LOG2 LOG2 LOG2 LOG10 LOG10)
2796 exps (EXP2 EXP10 POW10 EXP EXP10 POW10 EXP EXP2)
e18c1d66
RB
2797 (simplify
2798 (logs (exps @0))
c9e926ce
RS
2799 (if (SCALAR_FLOAT_TYPE_P (type))
2800 (with {
2801 tree x;
2802 switch (exps)
2803 {
2804 CASE_CFN_EXP:
2805 /* Prepare to do logN(exp(exponent)) -> exponent*logN(e). */
2806 x = build_real_truncate (type, dconst_e ());
2807 break;
2808 CASE_CFN_EXP2:
2809 /* Prepare to do logN(exp2(exponent)) -> exponent*logN(2). */
2810 x = build_real (type, dconst2);
2811 break;
2812 CASE_CFN_EXP10:
2813 CASE_CFN_POW10:
2814 /* Prepare to do logN(exp10(exponent)) -> exponent*logN(10). */
2815 {
2816 REAL_VALUE_TYPE dconst10;
2817 real_from_integer (&dconst10, VOIDmode, 10, SIGNED);
2818 x = build_real (type, dconst10);
2819 }
2820 break;
2821 default:
2822 gcc_unreachable ();
2823 }
2824 }
2825 (mult (logs { x; }) @0)))))
53f3cd25 2826
e18c1d66
RB
2827 (for logs (LOG LOG
2828 LOG2 LOG2
2829 LOG10 LOG10)
2830 exps (SQRT CBRT)
2831 (simplify
2832 (logs (exps @0))
c9e926ce
RS
2833 (if (SCALAR_FLOAT_TYPE_P (type))
2834 (with {
2835 tree x;
2836 switch (exps)
2837 {
2838 CASE_CFN_SQRT:
2839 /* Prepare to do logN(sqrt(x)) -> 0.5*logN(x). */
2840 x = build_real (type, dconsthalf);
2841 break;
2842 CASE_CFN_CBRT:
2843 /* Prepare to do logN(cbrt(x)) -> (1/3)*logN(x). */
2844 x = build_real_truncate (type, dconst_third ());
2845 break;
2846 default:
2847 gcc_unreachable ();
2848 }
2849 }
2850 (mult { x; } (logs @0))))))
53f3cd25
RS
2851
2852 /* logN(pow(x,exponent)) -> exponent*logN(x). */
e18c1d66
RB
2853 (for logs (LOG LOG2 LOG10)
2854 pows (POW)
2855 (simplify
2856 (logs (pows @0 @1))
53f3cd25
RS
2857 (mult @1 (logs @0))))
2858
2859 (for sqrts (SQRT)
2860 cbrts (CBRT)
b4838d77 2861 pows (POW)
53f3cd25
RS
2862 exps (EXP EXP2 EXP10 POW10)
2863 /* sqrt(expN(x)) -> expN(x*0.5). */
2864 (simplify
2865 (sqrts (exps @0))
2866 (exps (mult @0 { build_real (type, dconsthalf); })))
2867 /* cbrt(expN(x)) -> expN(x/3). */
2868 (simplify
2869 (cbrts (exps @0))
b4838d77
RS
2870 (exps (mult @0 { build_real_truncate (type, dconst_third ()); })))
2871 /* pow(expN(x), y) -> expN(x*y). */
2872 (simplify
2873 (pows (exps @0) @1)
2874 (exps (mult @0 @1))))
cfed37a0
RS
2875
2876 /* tan(atan(x)) -> x. */
2877 (for tans (TAN)
2878 atans (ATAN)
2879 (simplify
2880 (tans (atans @0))
2881 @0)))
53f3cd25 2882
abcc43f5
RS
2883/* cabs(x+0i) or cabs(0+xi) -> abs(x). */
2884(simplify
e04d2a35 2885 (CABS (complex:C @0 real_zerop@1))
abcc43f5
RS
2886 (abs @0))
2887
67dbe582
RS
2888/* trunc(trunc(x)) -> trunc(x), etc. */
2889(for fns (TRUNC FLOOR CEIL ROUND NEARBYINT RINT)
2890 (simplify
2891 (fns (fns @0))
2892 (fns @0)))
2893/* f(x) -> x if x is integer valued and f does nothing for such values. */
afeb246c 2894(for fns (TRUNC FLOOR CEIL ROUND NEARBYINT RINT)
67dbe582
RS
2895 (simplify
2896 (fns integer_valued_real_p@0)
2897 @0))
67dbe582 2898
4d7836c4
RS
2899/* hypot(x,0) and hypot(0,x) -> abs(x). */
2900(simplify
c9e926ce 2901 (HYPOT:c @0 real_zerop@1)
4d7836c4
RS
2902 (abs @0))
2903
b4838d77
RS
2904/* pow(1,x) -> 1. */
2905(simplify
2906 (POW real_onep@0 @1)
2907 @0)
2908
461e4145
RS
2909(simplify
2910 /* copysign(x,x) -> x. */
2911 (COPYSIGN @0 @0)
2912 @0)
2913
2914(simplify
2915 /* copysign(x,y) -> fabs(x) if y is nonnegative. */
2916 (COPYSIGN @0 tree_expr_nonnegative_p@1)
2917 (abs @0))
2918
86c0733f
RS
2919(for scale (LDEXP SCALBN SCALBLN)
2920 /* ldexp(0, x) -> 0. */
2921 (simplify
2922 (scale real_zerop@0 @1)
2923 @0)
2924 /* ldexp(x, 0) -> x. */
2925 (simplify
2926 (scale @0 integer_zerop@1)
2927 @0)
2928 /* ldexp(x, y) -> x if x is +-Inf or NaN. */
2929 (simplify
2930 (scale REAL_CST@0 @1)
2931 (if (!real_isfinite (TREE_REAL_CST_PTR (@0)))
2932 @0)))
2933
53f3cd25
RS
2934/* Canonicalization of sequences of math builtins. These rules represent
2935 IL simplifications but are not necessarily optimizations.
2936
2937 The sincos pass is responsible for picking "optimal" implementations
2938 of math builtins, which may be more complicated and can sometimes go
2939 the other way, e.g. converting pow into a sequence of sqrts.
2940 We only want to do these canonicalizations before the pass has run. */
2941
2942(if (flag_unsafe_math_optimizations && canonicalize_math_p ())
2943 /* Simplify tan(x) * cos(x) -> sin(x). */
2944 (simplify
2945 (mult:c (TAN:s @0) (COS:s @0))
2946 (SIN @0))
2947
2948 /* Simplify x * pow(x,c) -> pow(x,c+1). */
2949 (simplify
de3fbea3 2950 (mult:c @0 (POW:s @0 REAL_CST@1))
53f3cd25
RS
2951 (if (!TREE_OVERFLOW (@1))
2952 (POW @0 (plus @1 { build_one_cst (type); }))))
2953
2954 /* Simplify sin(x) / cos(x) -> tan(x). */
2955 (simplify
2956 (rdiv (SIN:s @0) (COS:s @0))
2957 (TAN @0))
2958
2959 /* Simplify cos(x) / sin(x) -> 1 / tan(x). */
2960 (simplify
2961 (rdiv (COS:s @0) (SIN:s @0))
2962 (rdiv { build_one_cst (type); } (TAN @0)))
2963
2964 /* Simplify sin(x) / tan(x) -> cos(x). */
2965 (simplify
2966 (rdiv (SIN:s @0) (TAN:s @0))
2967 (if (! HONOR_NANS (@0)
2968 && ! HONOR_INFINITIES (@0))
c9e926ce 2969 (COS @0)))
53f3cd25
RS
2970
2971 /* Simplify tan(x) / sin(x) -> 1.0 / cos(x). */
2972 (simplify
2973 (rdiv (TAN:s @0) (SIN:s @0))
2974 (if (! HONOR_NANS (@0)
2975 && ! HONOR_INFINITIES (@0))
2976 (rdiv { build_one_cst (type); } (COS @0))))
2977
2978 /* Simplify pow(x,y) * pow(x,z) -> pow(x,y+z). */
2979 (simplify
2980 (mult (POW:s @0 @1) (POW:s @0 @2))
2981 (POW @0 (plus @1 @2)))
2982
2983 /* Simplify pow(x,y) * pow(z,y) -> pow(x*z,y). */
2984 (simplify
2985 (mult (POW:s @0 @1) (POW:s @2 @1))
2986 (POW (mult @0 @2) @1))
2987
de3fbea3
RB
2988 /* Simplify powi(x,y) * powi(z,y) -> powi(x*z,y). */
2989 (simplify
2990 (mult (POWI:s @0 @1) (POWI:s @2 @1))
2991 (POWI (mult @0 @2) @1))
2992
53f3cd25
RS
2993 /* Simplify pow(x,c) / x -> pow(x,c-1). */
2994 (simplify
2995 (rdiv (POW:s @0 REAL_CST@1) @0)
2996 (if (!TREE_OVERFLOW (@1))
2997 (POW @0 (minus @1 { build_one_cst (type); }))))
2998
2999 /* Simplify x / pow (y,z) -> x * pow(y,-z). */
3000 (simplify
3001 (rdiv @0 (POW:s @1 @2))
3002 (mult @0 (POW @1 (negate @2))))
3003
3004 (for sqrts (SQRT)
3005 cbrts (CBRT)
3006 pows (POW)
3007 /* sqrt(sqrt(x)) -> pow(x,1/4). */
3008 (simplify
3009 (sqrts (sqrts @0))
3010 (pows @0 { build_real (type, dconst_quarter ()); }))
3011 /* sqrt(cbrt(x)) -> pow(x,1/6). */
3012 (simplify
3013 (sqrts (cbrts @0))
3014 (pows @0 { build_real_truncate (type, dconst_sixth ()); }))
3015 /* cbrt(sqrt(x)) -> pow(x,1/6). */
3016 (simplify
3017 (cbrts (sqrts @0))
3018 (pows @0 { build_real_truncate (type, dconst_sixth ()); }))
3019 /* cbrt(cbrt(x)) -> pow(x,1/9), iff x is nonnegative. */
3020 (simplify
3021 (cbrts (cbrts tree_expr_nonnegative_p@0))
3022 (pows @0 { build_real_truncate (type, dconst_ninth ()); }))
3023 /* sqrt(pow(x,y)) -> pow(|x|,y*0.5). */
3024 (simplify
3025 (sqrts (pows @0 @1))
3026 (pows (abs @0) (mult @1 { build_real (type, dconsthalf); })))
3027 /* cbrt(pow(x,y)) -> pow(x,y/3), iff x is nonnegative. */
3028 (simplify
3029 (cbrts (pows tree_expr_nonnegative_p@0 @1))
b4838d77
RS
3030 (pows @0 (mult @1 { build_real_truncate (type, dconst_third ()); })))
3031 /* pow(sqrt(x),y) -> pow(x,y*0.5). */
3032 (simplify
3033 (pows (sqrts @0) @1)
3034 (pows @0 (mult @1 { build_real (type, dconsthalf); })))
3035 /* pow(cbrt(x),y) -> pow(x,y/3) iff x is nonnegative. */
3036 (simplify
3037 (pows (cbrts tree_expr_nonnegative_p@0) @1)
3038 (pows @0 (mult @1 { build_real_truncate (type, dconst_third ()); })))
3039 /* pow(pow(x,y),z) -> pow(x,y*z) iff x is nonnegative. */
3040 (simplify
3041 (pows (pows tree_expr_nonnegative_p@0 @1) @2)
3042 (pows @0 (mult @1 @2))))
abcc43f5
RS
3043
3044 /* cabs(x+xi) -> fabs(x)*sqrt(2). */
3045 (simplify
3046 (CABS (complex @0 @0))
96285749
RS
3047 (mult (abs @0) { build_real_truncate (type, dconst_sqrt2 ()); }))
3048
4d7836c4
RS
3049 /* hypot(x,x) -> fabs(x)*sqrt(2). */
3050 (simplify
3051 (HYPOT @0 @0)
3052 (mult (abs @0) { build_real_truncate (type, dconst_sqrt2 ()); }))
3053
96285749
RS
3054 /* cexp(x+yi) -> exp(x)*cexpi(y). */
3055 (for cexps (CEXP)
3056 exps (EXP)
3057 cexpis (CEXPI)
3058 (simplify
3059 (cexps compositional_complex@0)
3060 (if (targetm.libc_has_function (function_c99_math_complex))
3061 (complex
3062 (mult (exps@1 (realpart @0)) (realpart (cexpis:type@2 (imagpart @0))))
3063 (mult @1 (imagpart @2)))))))
e18c1d66 3064
67dbe582
RS
3065(if (canonicalize_math_p ())
3066 /* floor(x) -> trunc(x) if x is nonnegative. */
3067 (for floors (FLOOR)
3068 truncs (TRUNC)
3069 (simplify
3070 (floors tree_expr_nonnegative_p@0)
3071 (truncs @0))))
3072
3073(match double_value_p
3074 @0
3075 (if (TYPE_MAIN_VARIANT (TREE_TYPE (@0)) == double_type_node)))
3076(for froms (BUILT_IN_TRUNCL
3077 BUILT_IN_FLOORL
3078 BUILT_IN_CEILL
3079 BUILT_IN_ROUNDL
3080 BUILT_IN_NEARBYINTL
3081 BUILT_IN_RINTL)
3082 tos (BUILT_IN_TRUNC
3083 BUILT_IN_FLOOR
3084 BUILT_IN_CEIL
3085 BUILT_IN_ROUND
3086 BUILT_IN_NEARBYINT
3087 BUILT_IN_RINT)
3088 /* truncl(extend(x)) -> extend(trunc(x)), etc., if x is a double. */
3089 (if (optimize && canonicalize_math_p ())
3090 (simplify
3091 (froms (convert double_value_p@0))
3092 (convert (tos @0)))))
3093
3094(match float_value_p
3095 @0
3096 (if (TYPE_MAIN_VARIANT (TREE_TYPE (@0)) == float_type_node)))
3097(for froms (BUILT_IN_TRUNCL BUILT_IN_TRUNC
3098 BUILT_IN_FLOORL BUILT_IN_FLOOR
3099 BUILT_IN_CEILL BUILT_IN_CEIL
3100 BUILT_IN_ROUNDL BUILT_IN_ROUND
3101 BUILT_IN_NEARBYINTL BUILT_IN_NEARBYINT
3102 BUILT_IN_RINTL BUILT_IN_RINT)
3103 tos (BUILT_IN_TRUNCF BUILT_IN_TRUNCF
3104 BUILT_IN_FLOORF BUILT_IN_FLOORF
3105 BUILT_IN_CEILF BUILT_IN_CEILF
3106 BUILT_IN_ROUNDF BUILT_IN_ROUNDF
3107 BUILT_IN_NEARBYINTF BUILT_IN_NEARBYINTF
3108 BUILT_IN_RINTF BUILT_IN_RINTF)
3109 /* truncl(extend(x)) and trunc(extend(x)) -> extend(truncf(x)), etc.,
3110 if x is a float. */
5dac7dbd
JDA
3111 (if (optimize && canonicalize_math_p ()
3112 && targetm.libc_has_function (function_c99_misc))
67dbe582
RS
3113 (simplify
3114 (froms (convert float_value_p@0))
3115 (convert (tos @0)))))
3116
543a9bcd
RS
3117(for froms (XFLOORL XCEILL XROUNDL XRINTL)
3118 tos (XFLOOR XCEIL XROUND XRINT)
3119 /* llfloorl(extend(x)) -> llfloor(x), etc., if x is a double. */
3120 (if (optimize && canonicalize_math_p ())
3121 (simplify
3122 (froms (convert double_value_p@0))
3123 (tos @0))))
3124
3125(for froms (XFLOORL XCEILL XROUNDL XRINTL
3126 XFLOOR XCEIL XROUND XRINT)
3127 tos (XFLOORF XCEILF XROUNDF XRINTF)
3128 /* llfloorl(extend(x)) and llfloor(extend(x)) -> llfloorf(x), etc.,
3129 if x is a float. */
3130 (if (optimize && canonicalize_math_p ())
3131 (simplify
3132 (froms (convert float_value_p@0))
3133 (tos @0))))
3134
3135(if (canonicalize_math_p ())
3136 /* xfloor(x) -> fix_trunc(x) if x is nonnegative. */
3137 (for floors (IFLOOR LFLOOR LLFLOOR)
3138 (simplify
3139 (floors tree_expr_nonnegative_p@0)
3140 (fix_trunc @0))))
3141
3142(if (canonicalize_math_p ())
3143 /* xfloor(x) -> fix_trunc(x), etc., if x is integer valued. */
3144 (for fns (IFLOOR LFLOOR LLFLOOR
3145 ICEIL LCEIL LLCEIL
3146 IROUND LROUND LLROUND)
3147 (simplify
3148 (fns integer_valued_real_p@0)
3149 (fix_trunc @0)))
3150 (if (!flag_errno_math)
3151 /* xrint(x) -> fix_trunc(x), etc., if x is integer valued. */
3152 (for rints (IRINT LRINT LLRINT)
3153 (simplify
3154 (rints integer_valued_real_p@0)
3155 (fix_trunc @0)))))
3156
3157(if (canonicalize_math_p ())
3158 (for ifn (IFLOOR ICEIL IROUND IRINT)
3159 lfn (LFLOOR LCEIL LROUND LRINT)
3160 llfn (LLFLOOR LLCEIL LLROUND LLRINT)
3161 /* Canonicalize iround (x) to lround (x) on ILP32 targets where
3162 sizeof (int) == sizeof (long). */
3163 (if (TYPE_PRECISION (integer_type_node)
3164 == TYPE_PRECISION (long_integer_type_node))
3165 (simplify
3166 (ifn @0)
3167 (lfn:long_integer_type_node @0)))
3168 /* Canonicalize llround (x) to lround (x) on LP64 targets where
3169 sizeof (long long) == sizeof (long). */
3170 (if (TYPE_PRECISION (long_long_integer_type_node)
3171 == TYPE_PRECISION (long_integer_type_node))
3172 (simplify
3173 (llfn @0)
3174 (lfn:long_integer_type_node @0)))))
3175
92c52eab
RS
3176/* cproj(x) -> x if we're ignoring infinities. */
3177(simplify
3178 (CPROJ @0)
3179 (if (!HONOR_INFINITIES (type))
3180 @0))
3181
4534c203
RB
3182/* If the real part is inf and the imag part is known to be
3183 nonnegative, return (inf + 0i). */
3184(simplify
3185 (CPROJ (complex REAL_CST@0 tree_expr_nonnegative_p@1))
3186 (if (real_isinf (TREE_REAL_CST_PTR (@0)))
92c52eab
RS
3187 { build_complex_inf (type, false); }))
3188
4534c203
RB
3189/* If the imag part is inf, return (inf+I*copysign(0,imag)). */
3190(simplify
3191 (CPROJ (complex @0 REAL_CST@1))
3192 (if (real_isinf (TREE_REAL_CST_PTR (@1)))
92c52eab 3193 { build_complex_inf (type, TREE_REAL_CST_PTR (@1)->sign); }))
4534c203 3194
b4838d77
RS
3195(for pows (POW)
3196 sqrts (SQRT)
3197 cbrts (CBRT)
3198 (simplify
3199 (pows @0 REAL_CST@1)
3200 (with {
3201 const REAL_VALUE_TYPE *value = TREE_REAL_CST_PTR (@1);
3202 REAL_VALUE_TYPE tmp;
3203 }
3204 (switch
3205 /* pow(x,0) -> 1. */
3206 (if (real_equal (value, &dconst0))
3207 { build_real (type, dconst1); })
3208 /* pow(x,1) -> x. */
3209 (if (real_equal (value, &dconst1))
3210 @0)
3211 /* pow(x,-1) -> 1/x. */
3212 (if (real_equal (value, &dconstm1))
3213 (rdiv { build_real (type, dconst1); } @0))
3214 /* pow(x,0.5) -> sqrt(x). */
3215 (if (flag_unsafe_math_optimizations
3216 && canonicalize_math_p ()
3217 && real_equal (value, &dconsthalf))
3218 (sqrts @0))
3219 /* pow(x,1/3) -> cbrt(x). */
3220 (if (flag_unsafe_math_optimizations
3221 && canonicalize_math_p ()
3222 && (tmp = real_value_truncate (TYPE_MODE (type), dconst_third ()),
3223 real_equal (value, &tmp)))
3224 (cbrts @0))))))
4534c203 3225
5ddc84ca
RS
3226/* powi(1,x) -> 1. */
3227(simplify
3228 (POWI real_onep@0 @1)
3229 @0)
3230
3231(simplify
3232 (POWI @0 INTEGER_CST@1)
3233 (switch
3234 /* powi(x,0) -> 1. */
3235 (if (wi::eq_p (@1, 0))
3236 { build_real (type, dconst1); })
3237 /* powi(x,1) -> x. */
3238 (if (wi::eq_p (@1, 1))
3239 @0)
3240 /* powi(x,-1) -> 1/x. */
3241 (if (wi::eq_p (@1, -1))
3242 (rdiv { build_real (type, dconst1); } @0))))
3243
be144838
JL
3244/* Narrowing of arithmetic and logical operations.
3245
3246 These are conceptually similar to the transformations performed for
3247 the C/C++ front-ends by shorten_binary_op and shorten_compare. Long
3248 term we want to move all that code out of the front-ends into here. */
3249
3250/* If we have a narrowing conversion of an arithmetic operation where
3251 both operands are widening conversions from the same type as the outer
3252 narrowing conversion. Then convert the innermost operands to a suitable
9c582551 3253 unsigned type (to avoid introducing undefined behavior), perform the
be144838
JL
3254 operation and convert the result to the desired type. */
3255(for op (plus minus)
3256 (simplify
44fc0a51 3257 (convert (op:s (convert@2 @0) (convert@3 @1)))
be144838
JL
3258 (if (INTEGRAL_TYPE_P (type)
3259 /* We check for type compatibility between @0 and @1 below,
3260 so there's no need to check that @1/@3 are integral types. */
3261 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
3262 && INTEGRAL_TYPE_P (TREE_TYPE (@2))
3263 /* The precision of the type of each operand must match the
3264 precision of the mode of each operand, similarly for the
3265 result. */
3266 && (TYPE_PRECISION (TREE_TYPE (@0))
3267 == GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (@0))))
3268 && (TYPE_PRECISION (TREE_TYPE (@1))
3269 == GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (@1))))
3270 && TYPE_PRECISION (type) == GET_MODE_PRECISION (TYPE_MODE (type))
3271 /* The inner conversion must be a widening conversion. */
3272 && TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (TREE_TYPE (@0))
aea417d7 3273 && types_match (@0, @1)
44fc0a51 3274 && types_match (@0, type))
be144838 3275 (if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
8fdc6c67
RB
3276 (convert (op @0 @1))
3277 (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
3278 (convert (op (convert:utype @0) (convert:utype @1))))))))
48451e8f
JL
3279
3280/* This is another case of narrowing, specifically when there's an outer
3281 BIT_AND_EXPR which masks off bits outside the type of the innermost
3282 operands. Like the previous case we have to convert the operands
9c582551 3283 to unsigned types to avoid introducing undefined behavior for the
48451e8f
JL
3284 arithmetic operation. */
3285(for op (minus plus)
8fdc6c67
RB
3286 (simplify
3287 (bit_and (op:s (convert@2 @0) (convert@3 @1)) INTEGER_CST@4)
3288 (if (INTEGRAL_TYPE_P (type)
3289 /* We check for type compatibility between @0 and @1 below,
3290 so there's no need to check that @1/@3 are integral types. */
3291 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
3292 && INTEGRAL_TYPE_P (TREE_TYPE (@2))
3293 /* The precision of the type of each operand must match the
3294 precision of the mode of each operand, similarly for the
3295 result. */
3296 && (TYPE_PRECISION (TREE_TYPE (@0))
3297 == GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (@0))))
3298 && (TYPE_PRECISION (TREE_TYPE (@1))
3299 == GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (@1))))
3300 && TYPE_PRECISION (type) == GET_MODE_PRECISION (TYPE_MODE (type))
3301 /* The inner conversion must be a widening conversion. */
3302 && TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (TREE_TYPE (@0))
3303 && types_match (@0, @1)
3304 && (tree_int_cst_min_precision (@4, TYPE_SIGN (TREE_TYPE (@0)))
3305 <= TYPE_PRECISION (TREE_TYPE (@0)))
0a8c1e23
JL
3306 && (wi::bit_and (@4, wi::mask (TYPE_PRECISION (TREE_TYPE (@0)),
3307 true, TYPE_PRECISION (type))) == 0))
8fdc6c67
RB
3308 (if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
3309 (with { tree ntype = TREE_TYPE (@0); }
3310 (convert (bit_and (op @0 @1) (convert:ntype @4))))
3311 (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
3312 (convert (bit_and (op (convert:utype @0) (convert:utype @1))
3313 (convert:utype @4))))))))
4f7a5692
MC
3314
3315/* Transform (@0 < @1 and @0 < @2) to use min,
3316 (@0 > @1 and @0 > @2) to use max */
3317(for op (lt le gt ge)
3318 ext (min min max max)
3319 (simplify
4618c453
RB
3320 (bit_and (op:cs @0 @1) (op:cs @0 @2))
3321 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
3322 && TREE_CODE (@0) != INTEGER_CST)
4f7a5692
MC
3323 (op @0 (ext @1 @2)))))
3324
7317ef4a
RS
3325(simplify
3326 /* signbit(x) -> 0 if x is nonnegative. */
3327 (SIGNBIT tree_expr_nonnegative_p@0)
3328 { integer_zero_node; })
3329
3330(simplify
3331 /* signbit(x) -> x<0 if x doesn't have signed zeros. */
3332 (SIGNBIT @0)
3333 (if (!HONOR_SIGNED_ZEROS (@0))
3334 (convert (lt @0 { build_real (TREE_TYPE (@0), dconst0); }))))
a8b85ce9
MG
3335
3336/* Transform comparisons of the form X +- C1 CMP C2 to X CMP C2 -+ C1. */
3337(for cmp (eq ne)
3338 (for op (plus minus)
3339 rop (minus plus)
3340 (simplify
3341 (cmp (op@3 @0 INTEGER_CST@1) INTEGER_CST@2)
3342 (if (!TREE_OVERFLOW (@1) && !TREE_OVERFLOW (@2)
3343 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@0))
3344 && !TYPE_OVERFLOW_TRAPS (TREE_TYPE (@0))
3345 && !TYPE_SATURATING (TREE_TYPE (@0)))
3346 (with { tree res = int_const_binop (rop, @2, @1); }
3347 (if (TREE_OVERFLOW (res))
3348 { constant_boolean_node (cmp == NE_EXPR, type); }
3349 (if (single_use (@3))
3350 (cmp @0 { res; }))))))))
3351(for cmp (lt le gt ge)
3352 (for op (plus minus)
3353 rop (minus plus)
3354 (simplify
3355 (cmp (op@3 @0 INTEGER_CST@1) INTEGER_CST@2)
3356 (if (!TREE_OVERFLOW (@1) && !TREE_OVERFLOW (@2)
3357 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
3358 (with { tree res = int_const_binop (rop, @2, @1); }
3359 (if (TREE_OVERFLOW (res))
3360 {
3361 fold_overflow_warning (("assuming signed overflow does not occur "
3362 "when simplifying conditional to constant"),
3363 WARN_STRICT_OVERFLOW_CONDITIONAL);
3364 bool less = cmp == LE_EXPR || cmp == LT_EXPR;
3365 /* wi::ges_p (@2, 0) should be sufficient for a signed type. */
3366 bool ovf_high = wi::lt_p (@1, 0, TYPE_SIGN (TREE_TYPE (@1)))
3367 != (op == MINUS_EXPR);
3368 constant_boolean_node (less == ovf_high, type);
3369 }
3370 (if (single_use (@3))
3371 (with
3372 {
3373 fold_overflow_warning (("assuming signed overflow does not occur "
3374 "when changing X +- C1 cmp C2 to "
3375 "X cmp C2 -+ C1"),
3376 WARN_STRICT_OVERFLOW_COMPARISON);
3377 }
3378 (cmp @0 { res; })))))))))
d3e40b76
RB
3379
3380/* Canonicalizations of BIT_FIELD_REFs. */
3381
3382(simplify
3383 (BIT_FIELD_REF @0 @1 @2)
3384 (switch
3385 (if (TREE_CODE (TREE_TYPE (@0)) == COMPLEX_TYPE
3386 && tree_int_cst_equal (@1, TYPE_SIZE (TREE_TYPE (TREE_TYPE (@0)))))
3387 (switch
3388 (if (integer_zerop (@2))
3389 (view_convert (realpart @0)))
3390 (if (tree_int_cst_equal (@2, TYPE_SIZE (TREE_TYPE (TREE_TYPE (@0)))))
3391 (view_convert (imagpart @0)))))
3392 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
3393 && INTEGRAL_TYPE_P (type)
171f6f05
RB
3394 /* On GIMPLE this should only apply to register arguments. */
3395 && (! GIMPLE || is_gimple_reg (@0))
d3e40b76
RB
3396 /* A bit-field-ref that referenced the full argument can be stripped. */
3397 && ((compare_tree_int (@1, TYPE_PRECISION (TREE_TYPE (@0))) == 0
3398 && integer_zerop (@2))
3399 /* Low-parts can be reduced to integral conversions.
3400 ??? The following doesn't work for PDP endian. */
3401 || (BYTES_BIG_ENDIAN == WORDS_BIG_ENDIAN
3402 /* Don't even think about BITS_BIG_ENDIAN. */
3403 && TYPE_PRECISION (TREE_TYPE (@0)) % BITS_PER_UNIT == 0
3404 && TYPE_PRECISION (type) % BITS_PER_UNIT == 0
3405 && compare_tree_int (@2, (BYTES_BIG_ENDIAN
3406 ? (TYPE_PRECISION (TREE_TYPE (@0))
3407 - TYPE_PRECISION (type))
3408 : 0)) == 0)))
3409 (convert @0))))
3410
3411/* Simplify vector extracts. */
3412
3413(simplify
3414 (BIT_FIELD_REF CONSTRUCTOR@0 @1 @2)
3415 (if (VECTOR_TYPE_P (TREE_TYPE (@0))
3416 && (types_match (type, TREE_TYPE (TREE_TYPE (@0)))
3417 || (VECTOR_TYPE_P (type)
3418 && types_match (TREE_TYPE (type), TREE_TYPE (TREE_TYPE (@0))))))
3419 (with
3420 {
3421 tree ctor = (TREE_CODE (@0) == SSA_NAME
3422 ? gimple_assign_rhs1 (SSA_NAME_DEF_STMT (@0)) : @0);
3423 tree eltype = TREE_TYPE (TREE_TYPE (ctor));
3424 unsigned HOST_WIDE_INT width = tree_to_uhwi (TYPE_SIZE (eltype));
3425 unsigned HOST_WIDE_INT n = tree_to_uhwi (@1);
3426 unsigned HOST_WIDE_INT idx = tree_to_uhwi (@2);
3427 }
3428 (if (n != 0
3429 && (idx % width) == 0
3430 && (n % width) == 0
3431 && ((idx + n) / width) <= TYPE_VECTOR_SUBPARTS (TREE_TYPE (ctor)))
3432 (with
3433 {
3434 idx = idx / width;
3435 n = n / width;
3436 /* Constructor elements can be subvectors. */
3437 unsigned HOST_WIDE_INT k = 1;
3438 if (CONSTRUCTOR_NELTS (ctor) != 0)
3439 {
3440 tree cons_elem = TREE_TYPE (CONSTRUCTOR_ELT (ctor, 0)->value);
3441 if (TREE_CODE (cons_elem) == VECTOR_TYPE)
3442 k = TYPE_VECTOR_SUBPARTS (cons_elem);
3443 }
3444 }
3445 (switch
3446 /* We keep an exact subset of the constructor elements. */
3447 (if ((idx % k) == 0 && (n % k) == 0)
3448 (if (CONSTRUCTOR_NELTS (ctor) == 0)
3449 { build_constructor (type, NULL); }
3450 (with
3451 {
3452 idx /= k;
3453 n /= k;
3454 }
3455 (if (n == 1)
3456 (if (idx < CONSTRUCTOR_NELTS (ctor))
3457 { CONSTRUCTOR_ELT (ctor, idx)->value; }
3458 { build_zero_cst (type); })
3459 {
3460 vec<constructor_elt, va_gc> *vals;
3461 vec_alloc (vals, n);
3462 for (unsigned i = 0;
3463 i < n && idx + i < CONSTRUCTOR_NELTS (ctor); ++i)
3464 CONSTRUCTOR_APPEND_ELT (vals, NULL_TREE,
3465 CONSTRUCTOR_ELT (ctor, idx + i)->value);
3466 build_constructor (type, vals);
3467 }))))
3468 /* The bitfield references a single constructor element. */
3469 (if (idx + n <= (idx / k + 1) * k)
3470 (switch
3471 (if (CONSTRUCTOR_NELTS (ctor) <= idx / k)
3472 { build_zero_cst (type); })
3473 (if (n == k)
3474 { CONSTRUCTOR_ELT (ctor, idx / k)->value; })
3475 (BIT_FIELD_REF { CONSTRUCTOR_ELT (ctor, idx / k)->value; }
3476 @1 { bitsize_int ((idx % k) * width); })))))))))