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