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