apply unary op to both sides of (vec_cond x cst1 cst2)
[gcc.git] / gcc / match.pd
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
5 Copyright (C) 2014-2019 Free Software Foundation, Inc.
6 Contributed by Richard Biener <rguenther@suse.de>
7 and Prathamesh Kulkarni <bilbotheelffriend@gmail.com>
8
9 This file is part of GCC.
10
11 GCC is free software; you can redistribute it and/or modify it under
12 the terms of the GNU General Public License as published by the Free
13 Software Foundation; either version 3, or (at your option) any later
14 version.
15
16 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
17 WARRANTY; without even the implied warranty of MERCHANTABILITY or
18 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19 for more details.
20
21 You should have received a copy of the GNU General Public License
22 along with GCC; see the file COPYING3. If not see
23 <http://www.gnu.org/licenses/>. */
24
25
26 /* Generic tree predicates we inherit. */
27 (define_predicates
28 integer_onep integer_zerop integer_all_onesp integer_minus_onep
29 integer_each_onep integer_truep integer_nonzerop
30 real_zerop real_onep real_minus_onep
31 zerop
32 initializer_each_zero_or_onep
33 CONSTANT_CLASS_P
34 tree_expr_nonnegative_p
35 tree_expr_nonzero_p
36 integer_valued_real_p
37 integer_pow2p
38 uniform_integer_cst_p
39 HONOR_NANS)
40
41 /* Operator lists. */
42 (define_operator_list tcc_comparison
43 lt le eq ne ge gt unordered ordered unlt unle ungt unge uneq ltgt)
44 (define_operator_list inverted_tcc_comparison
45 ge gt ne eq lt le ordered unordered ge gt le lt ltgt uneq)
46 (define_operator_list inverted_tcc_comparison_with_nans
47 unge ungt ne eq unlt unle ordered unordered ge gt le lt ltgt uneq)
48 (define_operator_list swapped_tcc_comparison
49 gt ge eq ne le lt unordered ordered ungt unge unlt unle uneq ltgt)
50 (define_operator_list simple_comparison lt le eq ne ge gt)
51 (define_operator_list swapped_simple_comparison gt ge eq ne le lt)
52
53 #include "cfn-operators.pd"
54
55 /* Define operand lists for math rounding functions {,i,l,ll}FN,
56 where the versions prefixed with "i" return an int, those prefixed with
57 "l" return a long and those prefixed with "ll" return a long long.
58
59 Also define operand lists:
60
61 X<FN>F for all float functions, in the order i, l, ll
62 X<FN> for all double functions, in the same order
63 X<FN>L for all long double functions, in the same order. */
64 #define DEFINE_INT_AND_FLOAT_ROUND_FN(FN) \
65 (define_operator_list X##FN##F BUILT_IN_I##FN##F \
66 BUILT_IN_L##FN##F \
67 BUILT_IN_LL##FN##F) \
68 (define_operator_list X##FN BUILT_IN_I##FN \
69 BUILT_IN_L##FN \
70 BUILT_IN_LL##FN) \
71 (define_operator_list X##FN##L BUILT_IN_I##FN##L \
72 BUILT_IN_L##FN##L \
73 BUILT_IN_LL##FN##L)
74
75 DEFINE_INT_AND_FLOAT_ROUND_FN (FLOOR)
76 DEFINE_INT_AND_FLOAT_ROUND_FN (CEIL)
77 DEFINE_INT_AND_FLOAT_ROUND_FN (ROUND)
78 DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
79
80 /* Binary operations and their associated IFN_COND_* function. */
81 (define_operator_list UNCOND_BINARY
82 plus minus
83 mult trunc_div trunc_mod rdiv
84 min max
85 bit_and bit_ior bit_xor)
86 (define_operator_list COND_BINARY
87 IFN_COND_ADD IFN_COND_SUB
88 IFN_COND_MUL IFN_COND_DIV IFN_COND_MOD IFN_COND_RDIV
89 IFN_COND_MIN IFN_COND_MAX
90 IFN_COND_AND IFN_COND_IOR IFN_COND_XOR)
91
92 /* Same for ternary operations. */
93 (define_operator_list UNCOND_TERNARY
94 IFN_FMA IFN_FMS IFN_FNMA IFN_FNMS)
95 (define_operator_list COND_TERNARY
96 IFN_COND_FMA IFN_COND_FMS IFN_COND_FNMA IFN_COND_FNMS)
97
98 /* As opposed to convert?, this still creates a single pattern, so
99 it is not a suitable replacement for convert? in all cases. */
100 (match (nop_convert @0)
101 (convert @0)
102 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))))
103 (match (nop_convert @0)
104 (view_convert @0)
105 (if (VECTOR_TYPE_P (type) && VECTOR_TYPE_P (TREE_TYPE (@0))
106 && known_eq (TYPE_VECTOR_SUBPARTS (type),
107 TYPE_VECTOR_SUBPARTS (TREE_TYPE (@0)))
108 && tree_nop_conversion_p (TREE_TYPE (type), TREE_TYPE (TREE_TYPE (@0))))))
109 /* This one has to be last, or it shadows the others. */
110 (match (nop_convert @0)
111 @0)
112
113 /* Transform likes of (char) ABS_EXPR <(int) x> into (char) ABSU_EXPR <x>
114 ABSU_EXPR returns unsigned absolute value of the operand and the operand
115 of the ABSU_EXPR will have the corresponding signed type. */
116 (simplify (abs (convert @0))
117 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
118 && !TYPE_UNSIGNED (TREE_TYPE (@0))
119 && element_precision (type) > element_precision (TREE_TYPE (@0)))
120 (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
121 (convert (absu:utype @0)))))
122
123
124 /* Simplifications of operations with one constant operand and
125 simplifications to constants or single values. */
126
127 (for op (plus pointer_plus minus bit_ior bit_xor)
128 (simplify
129 (op @0 integer_zerop)
130 (non_lvalue @0)))
131
132 /* 0 +p index -> (type)index */
133 (simplify
134 (pointer_plus integer_zerop @1)
135 (non_lvalue (convert @1)))
136
137 /* ptr - 0 -> (type)ptr */
138 (simplify
139 (pointer_diff @0 integer_zerop)
140 (convert @0))
141
142 /* See if ARG1 is zero and X + ARG1 reduces to X.
143 Likewise if the operands are reversed. */
144 (simplify
145 (plus:c @0 real_zerop@1)
146 (if (fold_real_zero_addition_p (type, @1, 0))
147 (non_lvalue @0)))
148
149 /* See if ARG1 is zero and X - ARG1 reduces to X. */
150 (simplify
151 (minus @0 real_zerop@1)
152 (if (fold_real_zero_addition_p (type, @1, 1))
153 (non_lvalue @0)))
154
155 /* Even if the fold_real_zero_addition_p can't simplify X + 0.0
156 into X, we can optimize (X + 0.0) + 0.0 or (X + 0.0) - 0.0
157 or (X - 0.0) + 0.0 into X + 0.0 and (X - 0.0) - 0.0 into X - 0.0
158 if not -frounding-math. For sNaNs the first operation would raise
159 exceptions but turn the result into qNan, so the second operation
160 would not raise it. */
161 (for inner_op (plus minus)
162 (for outer_op (plus minus)
163 (simplify
164 (outer_op (inner_op@3 @0 REAL_CST@1) REAL_CST@2)
165 (if (real_zerop (@1)
166 && real_zerop (@2)
167 && !HONOR_SIGN_DEPENDENT_ROUNDING (type))
168 (with { bool inner_plus = ((inner_op == PLUS_EXPR)
169 ^ REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (@1)));
170 bool outer_plus
171 = ((outer_op == PLUS_EXPR)
172 ^ REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (@2))); }
173 (if (outer_plus && !inner_plus)
174 (outer_op @0 @2)
175 @3))))))
176
177 /* Simplify x - x.
178 This is unsafe for certain floats even in non-IEEE formats.
179 In IEEE, it is unsafe because it does wrong for NaNs.
180 Also note that operand_equal_p is always false if an operand
181 is volatile. */
182 (simplify
183 (minus @0 @0)
184 (if (!FLOAT_TYPE_P (type) || !HONOR_NANS (type))
185 { build_zero_cst (type); }))
186 (simplify
187 (pointer_diff @@0 @0)
188 { build_zero_cst (type); })
189
190 (simplify
191 (mult @0 integer_zerop@1)
192 @1)
193
194 /* Maybe fold x * 0 to 0. The expressions aren't the same
195 when x is NaN, since x * 0 is also NaN. Nor are they the
196 same in modes with signed zeros, since multiplying a
197 negative value by 0 gives -0, not +0. */
198 (simplify
199 (mult @0 real_zerop@1)
200 (if (!HONOR_NANS (type) && !HONOR_SIGNED_ZEROS (type))
201 @1))
202
203 /* In IEEE floating point, x*1 is not equivalent to x for snans.
204 Likewise for complex arithmetic with signed zeros. */
205 (simplify
206 (mult @0 real_onep)
207 (if (!HONOR_SNANS (type)
208 && (!HONOR_SIGNED_ZEROS (type)
209 || !COMPLEX_FLOAT_TYPE_P (type)))
210 (non_lvalue @0)))
211
212 /* Transform x * -1.0 into -x. */
213 (simplify
214 (mult @0 real_minus_onep)
215 (if (!HONOR_SNANS (type)
216 && (!HONOR_SIGNED_ZEROS (type)
217 || !COMPLEX_FLOAT_TYPE_P (type)))
218 (negate @0)))
219
220 /* Transform x * { 0 or 1, 0 or 1, ... } into x & { 0 or -1, 0 or -1, ...},
221 unless the target has native support for the former but not the latter. */
222 (simplify
223 (mult @0 VECTOR_CST@1)
224 (if (initializer_each_zero_or_onep (@1)
225 && !HONOR_SNANS (type)
226 && !HONOR_SIGNED_ZEROS (type))
227 (with { tree itype = FLOAT_TYPE_P (type) ? unsigned_type_for (type) : type; }
228 (if (itype
229 && (!VECTOR_MODE_P (TYPE_MODE (type))
230 || (VECTOR_MODE_P (TYPE_MODE (itype))
231 && optab_handler (and_optab,
232 TYPE_MODE (itype)) != CODE_FOR_nothing)))
233 (view_convert (bit_and:itype (view_convert @0)
234 (ne @1 { build_zero_cst (type); })))))))
235
236 (for cmp (gt ge lt le)
237 outp (convert convert negate negate)
238 outn (negate negate convert convert)
239 /* Transform (X > 0.0 ? 1.0 : -1.0) into copysign(1, X). */
240 /* Transform (X >= 0.0 ? 1.0 : -1.0) into copysign(1, X). */
241 /* Transform (X < 0.0 ? 1.0 : -1.0) into copysign(1,-X). */
242 /* Transform (X <= 0.0 ? 1.0 : -1.0) into copysign(1,-X). */
243 (simplify
244 (cond (cmp @0 real_zerop) real_onep@1 real_minus_onep)
245 (if (!HONOR_NANS (type) && !HONOR_SIGNED_ZEROS (type)
246 && types_match (type, TREE_TYPE (@0)))
247 (switch
248 (if (types_match (type, float_type_node))
249 (BUILT_IN_COPYSIGNF @1 (outp @0)))
250 (if (types_match (type, double_type_node))
251 (BUILT_IN_COPYSIGN @1 (outp @0)))
252 (if (types_match (type, long_double_type_node))
253 (BUILT_IN_COPYSIGNL @1 (outp @0))))))
254 /* Transform (X > 0.0 ? -1.0 : 1.0) into copysign(1,-X). */
255 /* Transform (X >= 0.0 ? -1.0 : 1.0) into copysign(1,-X). */
256 /* Transform (X < 0.0 ? -1.0 : 1.0) into copysign(1,X). */
257 /* Transform (X <= 0.0 ? -1.0 : 1.0) into copysign(1,X). */
258 (simplify
259 (cond (cmp @0 real_zerop) real_minus_onep real_onep@1)
260 (if (!HONOR_NANS (type) && !HONOR_SIGNED_ZEROS (type)
261 && types_match (type, TREE_TYPE (@0)))
262 (switch
263 (if (types_match (type, float_type_node))
264 (BUILT_IN_COPYSIGNF @1 (outn @0)))
265 (if (types_match (type, double_type_node))
266 (BUILT_IN_COPYSIGN @1 (outn @0)))
267 (if (types_match (type, long_double_type_node))
268 (BUILT_IN_COPYSIGNL @1 (outn @0)))))))
269
270 /* Transform X * copysign (1.0, X) into abs(X). */
271 (simplify
272 (mult:c @0 (COPYSIGN_ALL real_onep @0))
273 (if (!HONOR_NANS (type) && !HONOR_SIGNED_ZEROS (type))
274 (abs @0)))
275
276 /* Transform X * copysign (1.0, -X) into -abs(X). */
277 (simplify
278 (mult:c @0 (COPYSIGN_ALL real_onep (negate @0)))
279 (if (!HONOR_NANS (type) && !HONOR_SIGNED_ZEROS (type))
280 (negate (abs @0))))
281
282 /* Transform copysign (CST, X) into copysign (ABS(CST), X). */
283 (simplify
284 (COPYSIGN_ALL REAL_CST@0 @1)
285 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@0)))
286 (COPYSIGN_ALL (negate @0) @1)))
287
288 /* X * 1, X / 1 -> X. */
289 (for op (mult trunc_div ceil_div floor_div round_div exact_div)
290 (simplify
291 (op @0 integer_onep)
292 (non_lvalue @0)))
293
294 /* (A / (1 << B)) -> (A >> B).
295 Only for unsigned A. For signed A, this would not preserve rounding
296 toward zero.
297 For example: (-1 / ( 1 << B)) != -1 >> B. */
298 (simplify
299 (trunc_div @0 (lshift integer_onep@1 @2))
300 (if ((TYPE_UNSIGNED (type) || tree_expr_nonnegative_p (@0))
301 && (!VECTOR_TYPE_P (type)
302 || target_supports_op_p (type, RSHIFT_EXPR, optab_vector)
303 || target_supports_op_p (type, RSHIFT_EXPR, optab_scalar)))
304 (rshift @0 @2)))
305
306 /* Preserve explicit divisions by 0: the C++ front-end wants to detect
307 undefined behavior in constexpr evaluation, and assuming that the division
308 traps enables better optimizations than these anyway. */
309 (for div (trunc_div ceil_div floor_div round_div exact_div)
310 /* 0 / X is always zero. */
311 (simplify
312 (div integer_zerop@0 @1)
313 /* But not for 0 / 0 so that we can get the proper warnings and errors. */
314 (if (!integer_zerop (@1))
315 @0))
316 /* X / -1 is -X. */
317 (simplify
318 (div @0 integer_minus_onep@1)
319 (if (!TYPE_UNSIGNED (type))
320 (negate @0)))
321 /* X / X is one. */
322 (simplify
323 (div @0 @0)
324 /* But not for 0 / 0 so that we can get the proper warnings and errors.
325 And not for _Fract types where we can't build 1. */
326 (if (!integer_zerop (@0) && !ALL_FRACT_MODE_P (TYPE_MODE (type)))
327 { build_one_cst (type); }))
328 /* X / abs (X) is X < 0 ? -1 : 1. */
329 (simplify
330 (div:C @0 (abs @0))
331 (if (INTEGRAL_TYPE_P (type)
332 && TYPE_OVERFLOW_UNDEFINED (type))
333 (cond (lt @0 { build_zero_cst (type); })
334 { build_minus_one_cst (type); } { build_one_cst (type); })))
335 /* X / -X is -1. */
336 (simplify
337 (div:C @0 (negate @0))
338 (if ((INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type))
339 && TYPE_OVERFLOW_UNDEFINED (type))
340 { build_minus_one_cst (type); })))
341
342 /* For unsigned integral types, FLOOR_DIV_EXPR is the same as
343 TRUNC_DIV_EXPR. Rewrite into the latter in this case. */
344 (simplify
345 (floor_div @0 @1)
346 (if ((INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type))
347 && TYPE_UNSIGNED (type))
348 (trunc_div @0 @1)))
349
350 /* Combine two successive divisions. Note that combining ceil_div
351 and floor_div is trickier and combining round_div even more so. */
352 (for div (trunc_div exact_div)
353 (simplify
354 (div (div@3 @0 INTEGER_CST@1) INTEGER_CST@2)
355 (with {
356 wi::overflow_type overflow;
357 wide_int mul = wi::mul (wi::to_wide (@1), wi::to_wide (@2),
358 TYPE_SIGN (type), &overflow);
359 }
360 (if (div == EXACT_DIV_EXPR
361 || optimize_successive_divisions_p (@2, @3))
362 (if (!overflow)
363 (div @0 { wide_int_to_tree (type, mul); })
364 (if (TYPE_UNSIGNED (type)
365 || mul != wi::min_value (TYPE_PRECISION (type), SIGNED))
366 { build_zero_cst (type); }))))))
367
368 /* Combine successive multiplications. Similar to above, but handling
369 overflow is different. */
370 (simplify
371 (mult (mult @0 INTEGER_CST@1) INTEGER_CST@2)
372 (with {
373 wi::overflow_type overflow;
374 wide_int mul = wi::mul (wi::to_wide (@1), wi::to_wide (@2),
375 TYPE_SIGN (type), &overflow);
376 }
377 /* Skip folding on overflow: the only special case is @1 * @2 == -INT_MIN,
378 otherwise undefined overflow implies that @0 must be zero. */
379 (if (!overflow || TYPE_OVERFLOW_WRAPS (type))
380 (mult @0 { wide_int_to_tree (type, mul); }))))
381
382 /* Optimize A / A to 1.0 if we don't care about
383 NaNs or Infinities. */
384 (simplify
385 (rdiv @0 @0)
386 (if (FLOAT_TYPE_P (type)
387 && ! HONOR_NANS (type)
388 && ! HONOR_INFINITIES (type))
389 { build_one_cst (type); }))
390
391 /* Optimize -A / A to -1.0 if we don't care about
392 NaNs or Infinities. */
393 (simplify
394 (rdiv:C @0 (negate @0))
395 (if (FLOAT_TYPE_P (type)
396 && ! HONOR_NANS (type)
397 && ! HONOR_INFINITIES (type))
398 { build_minus_one_cst (type); }))
399
400 /* PR71078: x / abs(x) -> copysign (1.0, x) */
401 (simplify
402 (rdiv:C (convert? @0) (convert? (abs @0)))
403 (if (SCALAR_FLOAT_TYPE_P (type)
404 && ! HONOR_NANS (type)
405 && ! HONOR_INFINITIES (type))
406 (switch
407 (if (types_match (type, float_type_node))
408 (BUILT_IN_COPYSIGNF { build_one_cst (type); } (convert @0)))
409 (if (types_match (type, double_type_node))
410 (BUILT_IN_COPYSIGN { build_one_cst (type); } (convert @0)))
411 (if (types_match (type, long_double_type_node))
412 (BUILT_IN_COPYSIGNL { build_one_cst (type); } (convert @0))))))
413
414 /* In IEEE floating point, x/1 is not equivalent to x for snans. */
415 (simplify
416 (rdiv @0 real_onep)
417 (if (!HONOR_SNANS (type))
418 (non_lvalue @0)))
419
420 /* In IEEE floating point, x/-1 is not equivalent to -x for snans. */
421 (simplify
422 (rdiv @0 real_minus_onep)
423 (if (!HONOR_SNANS (type))
424 (negate @0)))
425
426 (if (flag_reciprocal_math)
427 /* Convert (A/B)/C to A/(B*C). */
428 (simplify
429 (rdiv (rdiv:s @0 @1) @2)
430 (rdiv @0 (mult @1 @2)))
431
432 /* Canonicalize x / (C1 * y) to (x * C2) / y. */
433 (simplify
434 (rdiv @0 (mult:s @1 REAL_CST@2))
435 (with
436 { tree tem = const_binop (RDIV_EXPR, type, build_one_cst (type), @2); }
437 (if (tem)
438 (rdiv (mult @0 { tem; } ) @1))))
439
440 /* Convert A/(B/C) to (A/B)*C */
441 (simplify
442 (rdiv @0 (rdiv:s @1 @2))
443 (mult (rdiv @0 @1) @2)))
444
445 /* Simplify x / (- y) to -x / y. */
446 (simplify
447 (rdiv @0 (negate @1))
448 (rdiv (negate @0) @1))
449
450 (if (flag_unsafe_math_optimizations)
451 /* Simplify (C / x op 0.0) to x op 0.0 for C != 0, C != Inf/Nan.
452 Since C / x may underflow to zero, do this only for unsafe math. */
453 (for op (lt le gt ge)
454 neg_op (gt ge lt le)
455 (simplify
456 (op (rdiv REAL_CST@0 @1) real_zerop@2)
457 (if (!HONOR_SIGNED_ZEROS (@1) && !HONOR_INFINITIES (@1))
458 (switch
459 (if (real_less (&dconst0, TREE_REAL_CST_PTR (@0)))
460 (op @1 @2))
461 /* For C < 0, use the inverted operator. */
462 (if (real_less (TREE_REAL_CST_PTR (@0), &dconst0))
463 (neg_op @1 @2)))))))
464
465 /* Optimize (X & (-A)) / A where A is a power of 2, to X >> log2(A) */
466 (for div (trunc_div ceil_div floor_div round_div exact_div)
467 (simplify
468 (div (convert? (bit_and @0 INTEGER_CST@1)) INTEGER_CST@2)
469 (if (integer_pow2p (@2)
470 && tree_int_cst_sgn (@2) > 0
471 && tree_nop_conversion_p (type, TREE_TYPE (@0))
472 && wi::to_wide (@2) + wi::to_wide (@1) == 0)
473 (rshift (convert @0)
474 { build_int_cst (integer_type_node,
475 wi::exact_log2 (wi::to_wide (@2))); }))))
476
477 /* If ARG1 is a constant, we can convert this to a multiply by the
478 reciprocal. This does not have the same rounding properties,
479 so only do this if -freciprocal-math. We can actually
480 always safely do it if ARG1 is a power of two, but it's hard to
481 tell if it is or not in a portable manner. */
482 (for cst (REAL_CST COMPLEX_CST VECTOR_CST)
483 (simplify
484 (rdiv @0 cst@1)
485 (if (optimize)
486 (if (flag_reciprocal_math
487 && !real_zerop (@1))
488 (with
489 { tree tem = const_binop (RDIV_EXPR, type, build_one_cst (type), @1); }
490 (if (tem)
491 (mult @0 { tem; } )))
492 (if (cst != COMPLEX_CST)
493 (with { tree inverse = exact_inverse (type, @1); }
494 (if (inverse)
495 (mult @0 { inverse; } ))))))))
496
497 (for mod (ceil_mod floor_mod round_mod trunc_mod)
498 /* 0 % X is always zero. */
499 (simplify
500 (mod integer_zerop@0 @1)
501 /* But not for 0 % 0 so that we can get the proper warnings and errors. */
502 (if (!integer_zerop (@1))
503 @0))
504 /* X % 1 is always zero. */
505 (simplify
506 (mod @0 integer_onep)
507 { build_zero_cst (type); })
508 /* X % -1 is zero. */
509 (simplify
510 (mod @0 integer_minus_onep@1)
511 (if (!TYPE_UNSIGNED (type))
512 { build_zero_cst (type); }))
513 /* X % X is zero. */
514 (simplify
515 (mod @0 @0)
516 /* But not for 0 % 0 so that we can get the proper warnings and errors. */
517 (if (!integer_zerop (@0))
518 { build_zero_cst (type); }))
519 /* (X % Y) % Y is just X % Y. */
520 (simplify
521 (mod (mod@2 @0 @1) @1)
522 @2)
523 /* From extract_muldiv_1: (X * C1) % C2 is zero if C1 is a multiple of C2. */
524 (simplify
525 (mod (mult @0 INTEGER_CST@1) INTEGER_CST@2)
526 (if (ANY_INTEGRAL_TYPE_P (type)
527 && TYPE_OVERFLOW_UNDEFINED (type)
528 && wi::multiple_of_p (wi::to_wide (@1), wi::to_wide (@2),
529 TYPE_SIGN (type)))
530 { build_zero_cst (type); }))
531 /* For (X % C) == 0, if X is signed and C is power of 2, use unsigned
532 modulo and comparison, since it is simpler and equivalent. */
533 (for cmp (eq ne)
534 (simplify
535 (cmp (mod @0 integer_pow2p@2) integer_zerop@1)
536 (if (!TYPE_UNSIGNED (TREE_TYPE (@0)))
537 (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
538 (cmp (mod (convert:utype @0) (convert:utype @2)) (convert:utype @1)))))))
539
540 /* X % -C is the same as X % C. */
541 (simplify
542 (trunc_mod @0 INTEGER_CST@1)
543 (if (TYPE_SIGN (type) == SIGNED
544 && !TREE_OVERFLOW (@1)
545 && wi::neg_p (wi::to_wide (@1))
546 && !TYPE_OVERFLOW_TRAPS (type)
547 /* Avoid this transformation if C is INT_MIN, i.e. C == -C. */
548 && !sign_bit_p (@1, @1))
549 (trunc_mod @0 (negate @1))))
550
551 /* X % -Y is the same as X % Y. */
552 (simplify
553 (trunc_mod @0 (convert? (negate @1)))
554 (if (INTEGRAL_TYPE_P (type)
555 && !TYPE_UNSIGNED (type)
556 && !TYPE_OVERFLOW_TRAPS (type)
557 && tree_nop_conversion_p (type, TREE_TYPE (@1))
558 /* Avoid this transformation if X might be INT_MIN or
559 Y might be -1, because we would then change valid
560 INT_MIN % -(-1) into invalid INT_MIN % -1. */
561 && (expr_not_equal_to (@0, wi::to_wide (TYPE_MIN_VALUE (type)))
562 || expr_not_equal_to (@1, wi::minus_one (TYPE_PRECISION
563 (TREE_TYPE (@1))))))
564 (trunc_mod @0 (convert @1))))
565
566 /* X - (X / Y) * Y is the same as X % Y. */
567 (simplify
568 (minus (convert1? @0) (convert2? (mult:c (trunc_div @@0 @@1) @1)))
569 (if (INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type))
570 (convert (trunc_mod @0 @1))))
571
572 /* Optimize TRUNC_MOD_EXPR by a power of two into a BIT_AND_EXPR,
573 i.e. "X % C" into "X & (C - 1)", if X and C are positive.
574 Also optimize A % (C << N) where C is a power of 2,
575 to A & ((C << N) - 1). */
576 (match (power_of_two_cand @1)
577 INTEGER_CST@1)
578 (match (power_of_two_cand @1)
579 (lshift INTEGER_CST@1 @2))
580 (for mod (trunc_mod floor_mod)
581 (simplify
582 (mod @0 (convert?@3 (power_of_two_cand@1 @2)))
583 (if ((TYPE_UNSIGNED (type)
584 || tree_expr_nonnegative_p (@0))
585 && tree_nop_conversion_p (type, TREE_TYPE (@3))
586 && integer_pow2p (@2) && tree_int_cst_sgn (@2) > 0)
587 (bit_and @0 (convert (minus @1 { build_int_cst (TREE_TYPE (@1), 1); }))))))
588
589 /* Simplify (unsigned t * 2)/2 -> unsigned t & 0x7FFFFFFF. */
590 (simplify
591 (trunc_div (mult @0 integer_pow2p@1) @1)
592 (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
593 (bit_and @0 { wide_int_to_tree
594 (type, wi::mask (TYPE_PRECISION (type)
595 - wi::exact_log2 (wi::to_wide (@1)),
596 false, TYPE_PRECISION (type))); })))
597
598 /* Simplify (unsigned t / 2) * 2 -> unsigned t & ~1. */
599 (simplify
600 (mult (trunc_div @0 integer_pow2p@1) @1)
601 (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
602 (bit_and @0 (negate @1))))
603
604 /* Simplify (t * 2) / 2) -> t. */
605 (for div (trunc_div ceil_div floor_div round_div exact_div)
606 (simplify
607 (div (mult:c @0 @1) @1)
608 (if (ANY_INTEGRAL_TYPE_P (type)
609 && TYPE_OVERFLOW_UNDEFINED (type))
610 @0)))
611
612 (for op (negate abs)
613 /* Simplify cos(-x) and cos(|x|) -> cos(x). Similarly for cosh. */
614 (for coss (COS COSH)
615 (simplify
616 (coss (op @0))
617 (coss @0)))
618 /* Simplify pow(-x, y) and pow(|x|,y) -> pow(x,y) if y is an even integer. */
619 (for pows (POW)
620 (simplify
621 (pows (op @0) REAL_CST@1)
622 (with { HOST_WIDE_INT n; }
623 (if (real_isinteger (&TREE_REAL_CST (@1), &n) && (n & 1) == 0)
624 (pows @0 @1)))))
625 /* Likewise for powi. */
626 (for pows (POWI)
627 (simplify
628 (pows (op @0) INTEGER_CST@1)
629 (if ((wi::to_wide (@1) & 1) == 0)
630 (pows @0 @1))))
631 /* Strip negate and abs from both operands of hypot. */
632 (for hypots (HYPOT)
633 (simplify
634 (hypots (op @0) @1)
635 (hypots @0 @1))
636 (simplify
637 (hypots @0 (op @1))
638 (hypots @0 @1)))
639 /* copysign(-x, y) and copysign(abs(x), y) -> copysign(x, y). */
640 (for copysigns (COPYSIGN_ALL)
641 (simplify
642 (copysigns (op @0) @1)
643 (copysigns @0 @1))))
644
645 /* abs(x)*abs(x) -> x*x. Should be valid for all types. */
646 (simplify
647 (mult (abs@1 @0) @1)
648 (mult @0 @0))
649
650 /* Convert absu(x)*absu(x) -> x*x. */
651 (simplify
652 (mult (absu@1 @0) @1)
653 (mult (convert@2 @0) @2))
654
655 /* cos(copysign(x, y)) -> cos(x). Similarly for cosh. */
656 (for coss (COS COSH)
657 copysigns (COPYSIGN)
658 (simplify
659 (coss (copysigns @0 @1))
660 (coss @0)))
661
662 /* pow(copysign(x, y), z) -> pow(x, z) if z is an even integer. */
663 (for pows (POW)
664 copysigns (COPYSIGN)
665 (simplify
666 (pows (copysigns @0 @2) REAL_CST@1)
667 (with { HOST_WIDE_INT n; }
668 (if (real_isinteger (&TREE_REAL_CST (@1), &n) && (n & 1) == 0)
669 (pows @0 @1)))))
670 /* Likewise for powi. */
671 (for pows (POWI)
672 copysigns (COPYSIGN)
673 (simplify
674 (pows (copysigns @0 @2) INTEGER_CST@1)
675 (if ((wi::to_wide (@1) & 1) == 0)
676 (pows @0 @1))))
677
678 (for hypots (HYPOT)
679 copysigns (COPYSIGN)
680 /* hypot(copysign(x, y), z) -> hypot(x, z). */
681 (simplify
682 (hypots (copysigns @0 @1) @2)
683 (hypots @0 @2))
684 /* hypot(x, copysign(y, z)) -> hypot(x, y). */
685 (simplify
686 (hypots @0 (copysigns @1 @2))
687 (hypots @0 @1)))
688
689 /* copysign(x, CST) -> [-]abs (x). */
690 (for copysigns (COPYSIGN_ALL)
691 (simplify
692 (copysigns @0 REAL_CST@1)
693 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1)))
694 (negate (abs @0))
695 (abs @0))))
696
697 /* copysign(copysign(x, y), z) -> copysign(x, z). */
698 (for copysigns (COPYSIGN_ALL)
699 (simplify
700 (copysigns (copysigns @0 @1) @2)
701 (copysigns @0 @2)))
702
703 /* copysign(x,y)*copysign(x,y) -> x*x. */
704 (for copysigns (COPYSIGN_ALL)
705 (simplify
706 (mult (copysigns@2 @0 @1) @2)
707 (mult @0 @0)))
708
709 /* ccos(-x) -> ccos(x). Similarly for ccosh. */
710 (for ccoss (CCOS CCOSH)
711 (simplify
712 (ccoss (negate @0))
713 (ccoss @0)))
714
715 /* cabs(-x) and cos(conj(x)) -> cabs(x). */
716 (for ops (conj negate)
717 (for cabss (CABS)
718 (simplify
719 (cabss (ops @0))
720 (cabss @0))))
721
722 /* Fold (a * (1 << b)) into (a << b) */
723 (simplify
724 (mult:c @0 (convert? (lshift integer_onep@1 @2)))
725 (if (! FLOAT_TYPE_P (type)
726 && tree_nop_conversion_p (type, TREE_TYPE (@1)))
727 (lshift @0 @2)))
728
729 /* Fold (1 << (C - x)) where C = precision(type) - 1
730 into ((1 << C) >> x). */
731 (simplify
732 (lshift integer_onep@0 (minus@1 INTEGER_CST@2 @3))
733 (if (INTEGRAL_TYPE_P (type)
734 && wi::eq_p (wi::to_wide (@2), TYPE_PRECISION (type) - 1)
735 && single_use (@1))
736 (if (TYPE_UNSIGNED (type))
737 (rshift (lshift @0 @2) @3)
738 (with
739 { tree utype = unsigned_type_for (type); }
740 (convert (rshift (lshift (convert:utype @0) @2) @3))))))
741
742 /* Fold (C1/X)*C2 into (C1*C2)/X. */
743 (simplify
744 (mult (rdiv@3 REAL_CST@0 @1) REAL_CST@2)
745 (if (flag_associative_math
746 && single_use (@3))
747 (with
748 { tree tem = const_binop (MULT_EXPR, type, @0, @2); }
749 (if (tem)
750 (rdiv { tem; } @1)))))
751
752 /* Simplify ~X & X as zero. */
753 (simplify
754 (bit_and:c (convert? @0) (convert? (bit_not @0)))
755 { build_zero_cst (type); })
756
757 /* PR71636: Transform x & ((1U << b) - 1) -> x & ~(~0U << b); */
758 (simplify
759 (bit_and:c @0 (plus:s (lshift:s integer_onep @1) integer_minus_onep))
760 (if (TYPE_UNSIGNED (type))
761 (bit_and @0 (bit_not (lshift { build_all_ones_cst (type); } @1)))))
762
763 (for bitop (bit_and bit_ior)
764 cmp (eq ne)
765 /* PR35691: Transform
766 (x == 0 & y == 0) -> (x | typeof(x)(y)) == 0.
767 (x != 0 | y != 0) -> (x | typeof(x)(y)) != 0. */
768 (simplify
769 (bitop (cmp @0 integer_zerop@2) (cmp @1 integer_zerop))
770 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
771 && INTEGRAL_TYPE_P (TREE_TYPE (@1))
772 && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1)))
773 (cmp (bit_ior @0 (convert @1)) @2)))
774 /* Transform:
775 (x == -1 & y == -1) -> (x & typeof(x)(y)) == -1.
776 (x != -1 | y != -1) -> (x & typeof(x)(y)) != -1. */
777 (simplify
778 (bitop (cmp @0 integer_all_onesp@2) (cmp @1 integer_all_onesp))
779 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
780 && INTEGRAL_TYPE_P (TREE_TYPE (@1))
781 && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1)))
782 (cmp (bit_and @0 (convert @1)) @2))))
783
784 /* Fold (A & ~B) - (A & B) into (A ^ B) - B. */
785 (simplify
786 (minus (bit_and:cs @0 (bit_not @1)) (bit_and:cs @0 @1))
787 (minus (bit_xor @0 @1) @1))
788 (simplify
789 (minus (bit_and:s @0 INTEGER_CST@2) (bit_and:s @0 INTEGER_CST@1))
790 (if (~wi::to_wide (@2) == wi::to_wide (@1))
791 (minus (bit_xor @0 @1) @1)))
792
793 /* Fold (A & B) - (A & ~B) into B - (A ^ B). */
794 (simplify
795 (minus (bit_and:cs @0 @1) (bit_and:cs @0 (bit_not @1)))
796 (minus @1 (bit_xor @0 @1)))
797
798 /* Simplify (X & ~Y) |^+ (~X & Y) -> X ^ Y. */
799 (for op (bit_ior bit_xor plus)
800 (simplify
801 (op (bit_and:c @0 (bit_not @1)) (bit_and:c (bit_not @0) @1))
802 (bit_xor @0 @1))
803 (simplify
804 (op:c (bit_and @0 INTEGER_CST@2) (bit_and (bit_not @0) INTEGER_CST@1))
805 (if (~wi::to_wide (@2) == wi::to_wide (@1))
806 (bit_xor @0 @1))))
807
808 /* PR53979: Transform ((a ^ b) | a) -> (a | b) */
809 (simplify
810 (bit_ior:c (bit_xor:c @0 @1) @0)
811 (bit_ior @0 @1))
812
813 /* (a & ~b) | (a ^ b) --> a ^ b */
814 (simplify
815 (bit_ior:c (bit_and:c @0 (bit_not @1)) (bit_xor:c@2 @0 @1))
816 @2)
817
818 /* (a & ~b) ^ ~a --> ~(a & b) */
819 (simplify
820 (bit_xor:c (bit_and:cs @0 (bit_not @1)) (bit_not @0))
821 (bit_not (bit_and @0 @1)))
822
823 /* (a | b) & ~(a ^ b) --> a & b */
824 (simplify
825 (bit_and:c (bit_ior @0 @1) (bit_not (bit_xor:c @0 @1)))
826 (bit_and @0 @1))
827
828 /* a | ~(a ^ b) --> a | ~b */
829 (simplify
830 (bit_ior:c @0 (bit_not:s (bit_xor:c @0 @1)))
831 (bit_ior @0 (bit_not @1)))
832
833 /* (a | b) | (a &^ b) --> a | b */
834 (for op (bit_and bit_xor)
835 (simplify
836 (bit_ior:c (bit_ior@2 @0 @1) (op:c @0 @1))
837 @2))
838
839 /* (a & b) | ~(a ^ b) --> ~(a ^ b) */
840 (simplify
841 (bit_ior:c (bit_and:c @0 @1) (bit_not@2 (bit_xor @0 @1)))
842 @2)
843
844 /* ~(~a & b) --> a | ~b */
845 (simplify
846 (bit_not (bit_and:cs (bit_not @0) @1))
847 (bit_ior @0 (bit_not @1)))
848
849 /* ~(~a | b) --> a & ~b */
850 (simplify
851 (bit_not (bit_ior:cs (bit_not @0) @1))
852 (bit_and @0 (bit_not @1)))
853
854 /* Simplify (~X & Y) to X ^ Y if we know that (X & ~Y) is 0. */
855 #if GIMPLE
856 (simplify
857 (bit_and (bit_not SSA_NAME@0) INTEGER_CST@1)
858 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
859 && wi::bit_and_not (get_nonzero_bits (@0), wi::to_wide (@1)) == 0)
860 (bit_xor @0 @1)))
861 #endif
862
863 /* For constants M and N, if M == (1LL << cst) - 1 && (N & M) == M,
864 ((A & N) + B) & M -> (A + B) & M
865 Similarly if (N & M) == 0,
866 ((A | N) + B) & M -> (A + B) & M
867 and for - instead of + (or unary - instead of +)
868 and/or ^ instead of |.
869 If B is constant and (B & M) == 0, fold into A & M. */
870 (for op (plus minus)
871 (for bitop (bit_and bit_ior bit_xor)
872 (simplify
873 (bit_and (op:s (bitop:s@0 @3 INTEGER_CST@4) @1) INTEGER_CST@2)
874 (with
875 { tree pmop[2];
876 tree utype = fold_bit_and_mask (TREE_TYPE (@0), @2, op, @0, bitop,
877 @3, @4, @1, ERROR_MARK, NULL_TREE,
878 NULL_TREE, pmop); }
879 (if (utype)
880 (convert (bit_and (op (convert:utype { pmop[0]; })
881 (convert:utype { pmop[1]; }))
882 (convert:utype @2))))))
883 (simplify
884 (bit_and (op:s @0 (bitop:s@1 @3 INTEGER_CST@4)) INTEGER_CST@2)
885 (with
886 { tree pmop[2];
887 tree utype = fold_bit_and_mask (TREE_TYPE (@0), @2, op, @0, ERROR_MARK,
888 NULL_TREE, NULL_TREE, @1, bitop, @3,
889 @4, pmop); }
890 (if (utype)
891 (convert (bit_and (op (convert:utype { pmop[0]; })
892 (convert:utype { pmop[1]; }))
893 (convert:utype @2)))))))
894 (simplify
895 (bit_and (op:s @0 @1) INTEGER_CST@2)
896 (with
897 { tree pmop[2];
898 tree utype = fold_bit_and_mask (TREE_TYPE (@0), @2, op, @0, ERROR_MARK,
899 NULL_TREE, NULL_TREE, @1, ERROR_MARK,
900 NULL_TREE, NULL_TREE, pmop); }
901 (if (utype)
902 (convert (bit_and (op (convert:utype { pmop[0]; })
903 (convert:utype { pmop[1]; }))
904 (convert:utype @2)))))))
905 (for bitop (bit_and bit_ior bit_xor)
906 (simplify
907 (bit_and (negate:s (bitop:s@0 @2 INTEGER_CST@3)) INTEGER_CST@1)
908 (with
909 { tree pmop[2];
910 tree utype = fold_bit_and_mask (TREE_TYPE (@0), @1, NEGATE_EXPR, @0,
911 bitop, @2, @3, NULL_TREE, ERROR_MARK,
912 NULL_TREE, NULL_TREE, pmop); }
913 (if (utype)
914 (convert (bit_and (negate (convert:utype { pmop[0]; }))
915 (convert:utype @1)))))))
916
917 /* X % Y is smaller than Y. */
918 (for cmp (lt ge)
919 (simplify
920 (cmp (trunc_mod @0 @1) @1)
921 (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
922 { constant_boolean_node (cmp == LT_EXPR, type); })))
923 (for cmp (gt le)
924 (simplify
925 (cmp @1 (trunc_mod @0 @1))
926 (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
927 { constant_boolean_node (cmp == GT_EXPR, type); })))
928
929 /* x | ~0 -> ~0 */
930 (simplify
931 (bit_ior @0 integer_all_onesp@1)
932 @1)
933
934 /* x | 0 -> x */
935 (simplify
936 (bit_ior @0 integer_zerop)
937 @0)
938
939 /* x & 0 -> 0 */
940 (simplify
941 (bit_and @0 integer_zerop@1)
942 @1)
943
944 /* ~x | x -> -1 */
945 /* ~x ^ x -> -1 */
946 /* ~x + x -> -1 */
947 (for op (bit_ior bit_xor plus)
948 (simplify
949 (op:c (convert? @0) (convert? (bit_not @0)))
950 (convert { build_all_ones_cst (TREE_TYPE (@0)); })))
951
952 /* x ^ x -> 0 */
953 (simplify
954 (bit_xor @0 @0)
955 { build_zero_cst (type); })
956
957 /* Canonicalize X ^ ~0 to ~X. */
958 (simplify
959 (bit_xor @0 integer_all_onesp@1)
960 (bit_not @0))
961
962 /* x & ~0 -> x */
963 (simplify
964 (bit_and @0 integer_all_onesp)
965 (non_lvalue @0))
966
967 /* x & x -> x, x | x -> x */
968 (for bitop (bit_and bit_ior)
969 (simplify
970 (bitop @0 @0)
971 (non_lvalue @0)))
972
973 /* x & C -> x if we know that x & ~C == 0. */
974 #if GIMPLE
975 (simplify
976 (bit_and SSA_NAME@0 INTEGER_CST@1)
977 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
978 && wi::bit_and_not (get_nonzero_bits (@0), wi::to_wide (@1)) == 0)
979 @0))
980 #endif
981
982 /* x + (x & 1) -> (x + 1) & ~1 */
983 (simplify
984 (plus:c @0 (bit_and:s @0 integer_onep@1))
985 (bit_and (plus @0 @1) (bit_not @1)))
986
987 /* x & ~(x & y) -> x & ~y */
988 /* x | ~(x | y) -> x | ~y */
989 (for bitop (bit_and bit_ior)
990 (simplify
991 (bitop:c @0 (bit_not (bitop:cs @0 @1)))
992 (bitop @0 (bit_not @1))))
993
994 /* (~x & y) | ~(x | y) -> ~x */
995 (simplify
996 (bit_ior:c (bit_and:c (bit_not@2 @0) @1) (bit_not (bit_ior:c @0 @1)))
997 @2)
998
999 /* (x | y) ^ (x | ~y) -> ~x */
1000 (simplify
1001 (bit_xor:c (bit_ior:c @0 @1) (bit_ior:c @0 (bit_not @1)))
1002 (bit_not @0))
1003
1004 /* (x & y) | ~(x | y) -> ~(x ^ y) */
1005 (simplify
1006 (bit_ior:c (bit_and:s @0 @1) (bit_not:s (bit_ior:s @0 @1)))
1007 (bit_not (bit_xor @0 @1)))
1008
1009 /* (~x | y) ^ (x ^ y) -> x | ~y */
1010 (simplify
1011 (bit_xor:c (bit_ior:cs (bit_not @0) @1) (bit_xor:s @0 @1))
1012 (bit_ior @0 (bit_not @1)))
1013
1014 /* (x ^ y) | ~(x | y) -> ~(x & y) */
1015 (simplify
1016 (bit_ior:c (bit_xor:s @0 @1) (bit_not:s (bit_ior:s @0 @1)))
1017 (bit_not (bit_and @0 @1)))
1018
1019 /* (x | y) & ~x -> y & ~x */
1020 /* (x & y) | ~x -> y | ~x */
1021 (for bitop (bit_and bit_ior)
1022 rbitop (bit_ior bit_and)
1023 (simplify
1024 (bitop:c (rbitop:c @0 @1) (bit_not@2 @0))
1025 (bitop @1 @2)))
1026
1027 /* (x & y) ^ (x | y) -> x ^ y */
1028 (simplify
1029 (bit_xor:c (bit_and @0 @1) (bit_ior @0 @1))
1030 (bit_xor @0 @1))
1031
1032 /* (x ^ y) ^ (x | y) -> x & y */
1033 (simplify
1034 (bit_xor:c (bit_xor @0 @1) (bit_ior @0 @1))
1035 (bit_and @0 @1))
1036
1037 /* (x & y) + (x ^ y) -> x | y */
1038 /* (x & y) | (x ^ y) -> x | y */
1039 /* (x & y) ^ (x ^ y) -> x | y */
1040 (for op (plus bit_ior bit_xor)
1041 (simplify
1042 (op:c (bit_and @0 @1) (bit_xor @0 @1))
1043 (bit_ior @0 @1)))
1044
1045 /* (x & y) + (x | y) -> x + y */
1046 (simplify
1047 (plus:c (bit_and @0 @1) (bit_ior @0 @1))
1048 (plus @0 @1))
1049
1050 /* (x + y) - (x | y) -> x & y */
1051 (simplify
1052 (minus (plus @0 @1) (bit_ior @0 @1))
1053 (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
1054 && !TYPE_SATURATING (type))
1055 (bit_and @0 @1)))
1056
1057 /* (x + y) - (x & y) -> x | y */
1058 (simplify
1059 (minus (plus @0 @1) (bit_and @0 @1))
1060 (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
1061 && !TYPE_SATURATING (type))
1062 (bit_ior @0 @1)))
1063
1064 /* (x | y) - (x ^ y) -> x & y */
1065 (simplify
1066 (minus (bit_ior @0 @1) (bit_xor @0 @1))
1067 (bit_and @0 @1))
1068
1069 /* (x | y) - (x & y) -> x ^ y */
1070 (simplify
1071 (minus (bit_ior @0 @1) (bit_and @0 @1))
1072 (bit_xor @0 @1))
1073
1074 /* (x | y) & ~(x & y) -> x ^ y */
1075 (simplify
1076 (bit_and:c (bit_ior @0 @1) (bit_not (bit_and @0 @1)))
1077 (bit_xor @0 @1))
1078
1079 /* (x | y) & (~x ^ y) -> x & y */
1080 (simplify
1081 (bit_and:c (bit_ior:c @0 @1) (bit_xor:c @1 (bit_not @0)))
1082 (bit_and @0 @1))
1083
1084 /* (~x | y) & (x | ~y) -> ~(x ^ y) */
1085 (simplify
1086 (bit_and (bit_ior:cs (bit_not @0) @1) (bit_ior:cs @0 (bit_not @1)))
1087 (bit_not (bit_xor @0 @1)))
1088
1089 /* (~x | y) ^ (x | ~y) -> x ^ y */
1090 (simplify
1091 (bit_xor (bit_ior:c (bit_not @0) @1) (bit_ior:c @0 (bit_not @1)))
1092 (bit_xor @0 @1))
1093
1094 /* ~x & ~y -> ~(x | y)
1095 ~x | ~y -> ~(x & y) */
1096 (for op (bit_and bit_ior)
1097 rop (bit_ior bit_and)
1098 (simplify
1099 (op (convert1? (bit_not @0)) (convert2? (bit_not @1)))
1100 (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
1101 && element_precision (type) <= element_precision (TREE_TYPE (@1)))
1102 (bit_not (rop (convert @0) (convert @1))))))
1103
1104 /* If we are XORing or adding two BIT_AND_EXPR's, both of which are and'ing
1105 with a constant, and the two constants have no bits in common,
1106 we should treat this as a BIT_IOR_EXPR since this may produce more
1107 simplifications. */
1108 (for op (bit_xor plus)
1109 (simplify
1110 (op (convert1? (bit_and@4 @0 INTEGER_CST@1))
1111 (convert2? (bit_and@5 @2 INTEGER_CST@3)))
1112 (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
1113 && tree_nop_conversion_p (type, TREE_TYPE (@2))
1114 && (wi::to_wide (@1) & wi::to_wide (@3)) == 0)
1115 (bit_ior (convert @4) (convert @5)))))
1116
1117 /* (X | Y) ^ X -> Y & ~ X*/
1118 (simplify
1119 (bit_xor:c (convert1? (bit_ior:c @@0 @1)) (convert2? @0))
1120 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1121 (convert (bit_and @1 (bit_not @0)))))
1122
1123 /* Convert ~X ^ ~Y to X ^ Y. */
1124 (simplify
1125 (bit_xor (convert1? (bit_not @0)) (convert2? (bit_not @1)))
1126 (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
1127 && element_precision (type) <= element_precision (TREE_TYPE (@1)))
1128 (bit_xor (convert @0) (convert @1))))
1129
1130 /* Convert ~X ^ C to X ^ ~C. */
1131 (simplify
1132 (bit_xor (convert? (bit_not @0)) INTEGER_CST@1)
1133 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1134 (bit_xor (convert @0) (bit_not @1))))
1135
1136 /* Fold (X & Y) ^ Y and (X ^ Y) & Y as ~X & Y. */
1137 (for opo (bit_and bit_xor)
1138 opi (bit_xor bit_and)
1139 (simplify
1140 (opo:c (opi:cs @0 @1) @1)
1141 (bit_and (bit_not @0) @1)))
1142
1143 /* Given a bit-wise operation CODE applied to ARG0 and ARG1, see if both
1144 operands are another bit-wise operation with a common input. If so,
1145 distribute the bit operations to save an operation and possibly two if
1146 constants are involved. For example, convert
1147 (A | B) & (A | C) into A | (B & C)
1148 Further simplification will occur if B and C are constants. */
1149 (for op (bit_and bit_ior bit_xor)
1150 rop (bit_ior bit_and bit_and)
1151 (simplify
1152 (op (convert? (rop:c @@0 @1)) (convert? (rop:c @0 @2)))
1153 (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
1154 && tree_nop_conversion_p (type, TREE_TYPE (@2)))
1155 (rop (convert @0) (op (convert @1) (convert @2))))))
1156
1157 /* Some simple reassociation for bit operations, also handled in reassoc. */
1158 /* (X & Y) & Y -> X & Y
1159 (X | Y) | Y -> X | Y */
1160 (for op (bit_and bit_ior)
1161 (simplify
1162 (op:c (convert1?@2 (op:c @0 @@1)) (convert2? @1))
1163 @2))
1164 /* (X ^ Y) ^ Y -> X */
1165 (simplify
1166 (bit_xor:c (convert1? (bit_xor:c @0 @@1)) (convert2? @1))
1167 (convert @0))
1168 /* (X & Y) & (X & Z) -> (X & Y) & Z
1169 (X | Y) | (X | Z) -> (X | Y) | Z */
1170 (for op (bit_and bit_ior)
1171 (simplify
1172 (op (convert1?@3 (op:c@4 @0 @1)) (convert2?@5 (op:c@6 @0 @2)))
1173 (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
1174 && tree_nop_conversion_p (type, TREE_TYPE (@2)))
1175 (if (single_use (@5) && single_use (@6))
1176 (op @3 (convert @2))
1177 (if (single_use (@3) && single_use (@4))
1178 (op (convert @1) @5))))))
1179 /* (X ^ Y) ^ (X ^ Z) -> Y ^ Z */
1180 (simplify
1181 (bit_xor (convert1? (bit_xor:c @0 @1)) (convert2? (bit_xor:c @0 @2)))
1182 (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
1183 && tree_nop_conversion_p (type, TREE_TYPE (@2)))
1184 (bit_xor (convert @1) (convert @2))))
1185
1186 /* Convert abs (abs (X)) into abs (X).
1187 also absu (absu (X)) into absu (X). */
1188 (simplify
1189 (abs (abs@1 @0))
1190 @1)
1191
1192 (simplify
1193 (absu (convert@2 (absu@1 @0)))
1194 (if (tree_nop_conversion_p (TREE_TYPE (@2), TREE_TYPE (@1)))
1195 @1))
1196
1197 /* Convert abs[u] (-X) -> abs[u] (X). */
1198 (simplify
1199 (abs (negate @0))
1200 (abs @0))
1201
1202 (simplify
1203 (absu (negate @0))
1204 (absu @0))
1205
1206 /* Convert abs[u] (X) where X is nonnegative -> (X). */
1207 (simplify
1208 (abs tree_expr_nonnegative_p@0)
1209 @0)
1210
1211 (simplify
1212 (absu tree_expr_nonnegative_p@0)
1213 (convert @0))
1214
1215 /* A few cases of fold-const.c negate_expr_p predicate. */
1216 (match negate_expr_p
1217 INTEGER_CST
1218 (if ((INTEGRAL_TYPE_P (type)
1219 && TYPE_UNSIGNED (type))
1220 || (!TYPE_OVERFLOW_SANITIZED (type)
1221 && may_negate_without_overflow_p (t)))))
1222 (match negate_expr_p
1223 FIXED_CST)
1224 (match negate_expr_p
1225 (negate @0)
1226 (if (!TYPE_OVERFLOW_SANITIZED (type))))
1227 (match negate_expr_p
1228 REAL_CST
1229 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (t)))))
1230 /* VECTOR_CST handling of non-wrapping types would recurse in unsupported
1231 ways. */
1232 (match negate_expr_p
1233 VECTOR_CST
1234 (if (FLOAT_TYPE_P (TREE_TYPE (type)) || TYPE_OVERFLOW_WRAPS (type))))
1235 (match negate_expr_p
1236 (minus @0 @1)
1237 (if ((ANY_INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_WRAPS (type))
1238 || (FLOAT_TYPE_P (type)
1239 && !HONOR_SIGN_DEPENDENT_ROUNDING (type)
1240 && !HONOR_SIGNED_ZEROS (type)))))
1241
1242 /* (-A) * (-B) -> A * B */
1243 (simplify
1244 (mult:c (convert1? (negate @0)) (convert2? negate_expr_p@1))
1245 (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
1246 && tree_nop_conversion_p (type, TREE_TYPE (@1)))
1247 (mult (convert @0) (convert (negate @1)))))
1248
1249 /* -(A + B) -> (-B) - A. */
1250 (simplify
1251 (negate (plus:c @0 negate_expr_p@1))
1252 (if (!HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (type))
1253 && !HONOR_SIGNED_ZEROS (element_mode (type)))
1254 (minus (negate @1) @0)))
1255
1256 /* -(A - B) -> B - A. */
1257 (simplify
1258 (negate (minus @0 @1))
1259 (if ((ANY_INTEGRAL_TYPE_P (type) && !TYPE_OVERFLOW_SANITIZED (type))
1260 || (FLOAT_TYPE_P (type)
1261 && !HONOR_SIGN_DEPENDENT_ROUNDING (type)
1262 && !HONOR_SIGNED_ZEROS (type)))
1263 (minus @1 @0)))
1264 (simplify
1265 (negate (pointer_diff @0 @1))
1266 (if (TYPE_OVERFLOW_UNDEFINED (type))
1267 (pointer_diff @1 @0)))
1268
1269 /* A - B -> A + (-B) if B is easily negatable. */
1270 (simplify
1271 (minus @0 negate_expr_p@1)
1272 (if (!FIXED_POINT_TYPE_P (type))
1273 (plus @0 (negate @1))))
1274
1275 /* Try to fold (type) X op CST -> (type) (X op ((type-x) CST))
1276 when profitable.
1277 For bitwise binary operations apply operand conversions to the
1278 binary operation result instead of to the operands. This allows
1279 to combine successive conversions and bitwise binary operations.
1280 We combine the above two cases by using a conditional convert. */
1281 (for bitop (bit_and bit_ior bit_xor)
1282 (simplify
1283 (bitop (convert @0) (convert? @1))
1284 (if (((TREE_CODE (@1) == INTEGER_CST
1285 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
1286 && int_fits_type_p (@1, TREE_TYPE (@0)))
1287 || types_match (@0, @1))
1288 /* ??? This transform conflicts with fold-const.c doing
1289 Convert (T)(x & c) into (T)x & (T)c, if c is an integer
1290 constants (if x has signed type, the sign bit cannot be set
1291 in c). This folds extension into the BIT_AND_EXPR.
1292 Restrict it to GIMPLE to avoid endless recursions. */
1293 && (bitop != BIT_AND_EXPR || GIMPLE)
1294 && (/* That's a good idea if the conversion widens the operand, thus
1295 after hoisting the conversion the operation will be narrower. */
1296 TYPE_PRECISION (TREE_TYPE (@0)) < TYPE_PRECISION (type)
1297 /* It's also a good idea if the conversion is to a non-integer
1298 mode. */
1299 || GET_MODE_CLASS (TYPE_MODE (type)) != MODE_INT
1300 /* Or if the precision of TO is not the same as the precision
1301 of its mode. */
1302 || !type_has_mode_precision_p (type)))
1303 (convert (bitop @0 (convert @1))))))
1304
1305 (for bitop (bit_and bit_ior)
1306 rbitop (bit_ior bit_and)
1307 /* (x | y) & x -> x */
1308 /* (x & y) | x -> x */
1309 (simplify
1310 (bitop:c (rbitop:c @0 @1) @0)
1311 @0)
1312 /* (~x | y) & x -> x & y */
1313 /* (~x & y) | x -> x | y */
1314 (simplify
1315 (bitop:c (rbitop:c (bit_not @0) @1) @0)
1316 (bitop @0 @1)))
1317
1318 /* (x | CST1) & CST2 -> (x & CST2) | (CST1 & CST2) */
1319 (simplify
1320 (bit_and (bit_ior @0 CONSTANT_CLASS_P@1) CONSTANT_CLASS_P@2)
1321 (bit_ior (bit_and @0 @2) (bit_and @1 @2)))
1322
1323 /* Combine successive equal operations with constants. */
1324 (for bitop (bit_and bit_ior bit_xor)
1325 (simplify
1326 (bitop (bitop @0 CONSTANT_CLASS_P@1) CONSTANT_CLASS_P@2)
1327 (if (!CONSTANT_CLASS_P (@0))
1328 /* This is the canonical form regardless of whether (bitop @1 @2) can be
1329 folded to a constant. */
1330 (bitop @0 (bitop @1 @2))
1331 /* In this case we have three constants and (bitop @0 @1) doesn't fold
1332 to a constant. This can happen if @0 or @1 is a POLY_INT_CST and if
1333 the values involved are such that the operation can't be decided at
1334 compile time. Try folding one of @0 or @1 with @2 to see whether
1335 that combination can be decided at compile time.
1336
1337 Keep the existing form if both folds fail, to avoid endless
1338 oscillation. */
1339 (with { tree cst1 = const_binop (bitop, type, @0, @2); }
1340 (if (cst1)
1341 (bitop @1 { cst1; })
1342 (with { tree cst2 = const_binop (bitop, type, @1, @2); }
1343 (if (cst2)
1344 (bitop @0 { cst2; }))))))))
1345
1346 /* Try simple folding for X op !X, and X op X with the help
1347 of the truth_valued_p and logical_inverted_value predicates. */
1348 (match truth_valued_p
1349 @0
1350 (if (INTEGRAL_TYPE_P (type) && TYPE_PRECISION (type) == 1)))
1351 (for op (tcc_comparison truth_and truth_andif truth_or truth_orif truth_xor)
1352 (match truth_valued_p
1353 (op @0 @1)))
1354 (match truth_valued_p
1355 (truth_not @0))
1356
1357 (match (logical_inverted_value @0)
1358 (truth_not @0))
1359 (match (logical_inverted_value @0)
1360 (bit_not truth_valued_p@0))
1361 (match (logical_inverted_value @0)
1362 (eq @0 integer_zerop))
1363 (match (logical_inverted_value @0)
1364 (ne truth_valued_p@0 integer_truep))
1365 (match (logical_inverted_value @0)
1366 (bit_xor truth_valued_p@0 integer_truep))
1367
1368 /* X & !X -> 0. */
1369 (simplify
1370 (bit_and:c @0 (logical_inverted_value @0))
1371 { build_zero_cst (type); })
1372 /* X | !X and X ^ !X -> 1, , if X is truth-valued. */
1373 (for op (bit_ior bit_xor)
1374 (simplify
1375 (op:c truth_valued_p@0 (logical_inverted_value @0))
1376 { constant_boolean_node (true, type); }))
1377 /* X ==/!= !X is false/true. */
1378 (for op (eq ne)
1379 (simplify
1380 (op:c truth_valued_p@0 (logical_inverted_value @0))
1381 { constant_boolean_node (op == NE_EXPR ? true : false, type); }))
1382
1383 /* ~~x -> x */
1384 (simplify
1385 (bit_not (bit_not @0))
1386 @0)
1387
1388 /* Convert ~ (-A) to A - 1. */
1389 (simplify
1390 (bit_not (convert? (negate @0)))
1391 (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
1392 || !TYPE_UNSIGNED (TREE_TYPE (@0)))
1393 (convert (minus @0 { build_each_one_cst (TREE_TYPE (@0)); }))))
1394
1395 /* Convert - (~A) to A + 1. */
1396 (simplify
1397 (negate (nop_convert (bit_not @0)))
1398 (plus (view_convert @0) { build_each_one_cst (type); }))
1399
1400 /* Convert ~ (A - 1) or ~ (A + -1) to -A. */
1401 (simplify
1402 (bit_not (convert? (minus @0 integer_each_onep)))
1403 (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
1404 || !TYPE_UNSIGNED (TREE_TYPE (@0)))
1405 (convert (negate @0))))
1406 (simplify
1407 (bit_not (convert? (plus @0 integer_all_onesp)))
1408 (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
1409 || !TYPE_UNSIGNED (TREE_TYPE (@0)))
1410 (convert (negate @0))))
1411
1412 /* Part of convert ~(X ^ Y) to ~X ^ Y or X ^ ~Y if ~X or ~Y simplify. */
1413 (simplify
1414 (bit_not (convert? (bit_xor @0 INTEGER_CST@1)))
1415 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1416 (convert (bit_xor @0 (bit_not @1)))))
1417 (simplify
1418 (bit_not (convert? (bit_xor:c (bit_not @0) @1)))
1419 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1420 (convert (bit_xor @0 @1))))
1421
1422 /* Otherwise prefer ~(X ^ Y) to ~X ^ Y as more canonical. */
1423 (simplify
1424 (bit_xor:c (nop_convert:s (bit_not:s @0)) @1)
1425 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1426 (bit_not (bit_xor (view_convert @0) @1))))
1427
1428 /* (x & ~m) | (y & m) -> ((x ^ y) & m) ^ x */
1429 (simplify
1430 (bit_ior:c (bit_and:cs @0 (bit_not @2)) (bit_and:cs @1 @2))
1431 (bit_xor (bit_and (bit_xor @0 @1) @2) @0))
1432
1433 /* Fold A - (A & B) into ~B & A. */
1434 (simplify
1435 (minus (convert1? @0) (convert2?:s (bit_and:cs @@0 @1)))
1436 (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
1437 && tree_nop_conversion_p (type, TREE_TYPE (@1)))
1438 (convert (bit_and (bit_not @1) @0))))
1439
1440 /* (m1 CMP m2) * d -> (m1 CMP m2) ? d : 0 */
1441 (for cmp (gt lt ge le)
1442 (simplify
1443 (mult (convert (cmp @0 @1)) @2)
1444 (cond (cmp @0 @1) @2 { build_zero_cst (type); })))
1445
1446 /* For integral types with undefined overflow and C != 0 fold
1447 x * C EQ/NE y * C into x EQ/NE y. */
1448 (for cmp (eq ne)
1449 (simplify
1450 (cmp (mult:c @0 @1) (mult:c @2 @1))
1451 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
1452 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
1453 && tree_expr_nonzero_p (@1))
1454 (cmp @0 @2))))
1455
1456 /* For integral types with wrapping overflow and C odd fold
1457 x * C EQ/NE y * C into x EQ/NE y. */
1458 (for cmp (eq ne)
1459 (simplify
1460 (cmp (mult @0 INTEGER_CST@1) (mult @2 @1))
1461 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
1462 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))
1463 && (TREE_INT_CST_LOW (@1) & 1) != 0)
1464 (cmp @0 @2))))
1465
1466 /* For integral types with undefined overflow and C != 0 fold
1467 x * C RELOP y * C into:
1468
1469 x RELOP y for nonnegative C
1470 y RELOP x for negative C */
1471 (for cmp (lt gt le ge)
1472 (simplify
1473 (cmp (mult:c @0 @1) (mult:c @2 @1))
1474 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
1475 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
1476 (if (tree_expr_nonnegative_p (@1) && tree_expr_nonzero_p (@1))
1477 (cmp @0 @2)
1478 (if (TREE_CODE (@1) == INTEGER_CST
1479 && wi::neg_p (wi::to_wide (@1), TYPE_SIGN (TREE_TYPE (@1))))
1480 (cmp @2 @0))))))
1481
1482 /* (X - 1U) <= INT_MAX-1U into (int) X > 0. */
1483 (for cmp (le gt)
1484 icmp (gt le)
1485 (simplify
1486 (cmp (plus @0 integer_minus_onep@1) INTEGER_CST@2)
1487 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1488 && TYPE_UNSIGNED (TREE_TYPE (@0))
1489 && TYPE_PRECISION (TREE_TYPE (@0)) > 1
1490 && (wi::to_wide (@2)
1491 == wi::max_value (TYPE_PRECISION (TREE_TYPE (@0)), SIGNED) - 1))
1492 (with { tree stype = signed_type_for (TREE_TYPE (@0)); }
1493 (icmp (convert:stype @0) { build_int_cst (stype, 0); })))))
1494
1495 /* X / 4 < Y / 4 iff X < Y when the division is known to be exact. */
1496 (for cmp (simple_comparison)
1497 (simplify
1498 (cmp (exact_div @0 INTEGER_CST@2) (exact_div @1 @2))
1499 (if (wi::gt_p (wi::to_wide (@2), 0, TYPE_SIGN (TREE_TYPE (@2))))
1500 (cmp @0 @1)
1501 (if (wi::lt_p (wi::to_wide (@2), 0, TYPE_SIGN (TREE_TYPE (@2))))
1502 (cmp @1 @0)))))
1503
1504 /* X / C1 op C2 into a simple range test. */
1505 (for cmp (simple_comparison)
1506 (simplify
1507 (cmp (trunc_div:s @0 INTEGER_CST@1) INTEGER_CST@2)
1508 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1509 && integer_nonzerop (@1)
1510 && !TREE_OVERFLOW (@1)
1511 && !TREE_OVERFLOW (@2))
1512 (with { tree lo, hi; bool neg_overflow;
1513 enum tree_code code = fold_div_compare (cmp, @1, @2, &lo, &hi,
1514 &neg_overflow); }
1515 (switch
1516 (if (code == LT_EXPR || code == GE_EXPR)
1517 (if (TREE_OVERFLOW (lo))
1518 { build_int_cst (type, (code == LT_EXPR) ^ neg_overflow); }
1519 (if (code == LT_EXPR)
1520 (lt @0 { lo; })
1521 (ge @0 { lo; }))))
1522 (if (code == LE_EXPR || code == GT_EXPR)
1523 (if (TREE_OVERFLOW (hi))
1524 { build_int_cst (type, (code == LE_EXPR) ^ neg_overflow); }
1525 (if (code == LE_EXPR)
1526 (le @0 { hi; })
1527 (gt @0 { hi; }))))
1528 (if (!lo && !hi)
1529 { build_int_cst (type, code == NE_EXPR); })
1530 (if (code == EQ_EXPR && !hi)
1531 (ge @0 { lo; }))
1532 (if (code == EQ_EXPR && !lo)
1533 (le @0 { hi; }))
1534 (if (code == NE_EXPR && !hi)
1535 (lt @0 { lo; }))
1536 (if (code == NE_EXPR && !lo)
1537 (gt @0 { hi; }))
1538 (if (GENERIC)
1539 { build_range_check (UNKNOWN_LOCATION, type, @0, code == EQ_EXPR,
1540 lo, hi); })
1541 (with
1542 {
1543 tree etype = range_check_type (TREE_TYPE (@0));
1544 if (etype)
1545 {
1546 if (! TYPE_UNSIGNED (etype))
1547 etype = unsigned_type_for (etype);
1548 hi = fold_convert (etype, hi);
1549 lo = fold_convert (etype, lo);
1550 hi = const_binop (MINUS_EXPR, etype, hi, lo);
1551 }
1552 }
1553 (if (etype && hi && !TREE_OVERFLOW (hi))
1554 (if (code == EQ_EXPR)
1555 (le (minus (convert:etype @0) { lo; }) { hi; })
1556 (gt (minus (convert:etype @0) { lo; }) { hi; })))))))))
1557
1558 /* X + Z < Y + Z is the same as X < Y when there is no overflow. */
1559 (for op (lt le ge gt)
1560 (simplify
1561 (op (plus:c @0 @2) (plus:c @1 @2))
1562 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1563 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
1564 (op @0 @1))))
1565 /* For equality and subtraction, this is also true with wrapping overflow. */
1566 (for op (eq ne minus)
1567 (simplify
1568 (op (plus:c @0 @2) (plus:c @1 @2))
1569 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1570 && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
1571 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))))
1572 (op @0 @1))))
1573
1574 /* X - Z < Y - Z is the same as X < Y when there is no overflow. */
1575 (for op (lt le ge gt)
1576 (simplify
1577 (op (minus @0 @2) (minus @1 @2))
1578 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1579 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
1580 (op @0 @1))))
1581 /* For equality and subtraction, this is also true with wrapping overflow. */
1582 (for op (eq ne minus)
1583 (simplify
1584 (op (minus @0 @2) (minus @1 @2))
1585 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1586 && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
1587 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))))
1588 (op @0 @1))))
1589 /* And for pointers... */
1590 (for op (simple_comparison)
1591 (simplify
1592 (op (pointer_diff@3 @0 @2) (pointer_diff @1 @2))
1593 (if (!TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2)))
1594 (op @0 @1))))
1595 (simplify
1596 (minus (pointer_diff@3 @0 @2) (pointer_diff @1 @2))
1597 (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@3))
1598 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2)))
1599 (pointer_diff @0 @1)))
1600
1601 /* Z - X < Z - Y is the same as Y < X when there is no overflow. */
1602 (for op (lt le ge gt)
1603 (simplify
1604 (op (minus @2 @0) (minus @2 @1))
1605 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1606 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
1607 (op @1 @0))))
1608 /* For equality and subtraction, this is also true with wrapping overflow. */
1609 (for op (eq ne minus)
1610 (simplify
1611 (op (minus @2 @0) (minus @2 @1))
1612 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1613 && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
1614 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))))
1615 (op @1 @0))))
1616 /* And for pointers... */
1617 (for op (simple_comparison)
1618 (simplify
1619 (op (pointer_diff@3 @2 @0) (pointer_diff @2 @1))
1620 (if (!TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2)))
1621 (op @1 @0))))
1622 (simplify
1623 (minus (pointer_diff@3 @2 @0) (pointer_diff @2 @1))
1624 (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@3))
1625 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2)))
1626 (pointer_diff @1 @0)))
1627
1628 /* X + Y < Y is the same as X < 0 when there is no overflow. */
1629 (for op (lt le gt ge)
1630 (simplify
1631 (op:c (plus:c@2 @0 @1) @1)
1632 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1633 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
1634 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@0))
1635 && (CONSTANT_CLASS_P (@0) || single_use (@2)))
1636 (op @0 { build_zero_cst (TREE_TYPE (@0)); }))))
1637 /* For equality, this is also true with wrapping overflow. */
1638 (for op (eq ne)
1639 (simplify
1640 (op:c (nop_convert@3 (plus:c@2 @0 (convert1? @1))) (convert2? @1))
1641 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1642 && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
1643 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
1644 && (CONSTANT_CLASS_P (@0) || (single_use (@2) && single_use (@3)))
1645 && tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@2))
1646 && tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@1)))
1647 (op @0 { build_zero_cst (TREE_TYPE (@0)); })))
1648 (simplify
1649 (op:c (nop_convert@3 (pointer_plus@2 (convert1? @0) @1)) (convert2? @0))
1650 (if (tree_nop_conversion_p (TREE_TYPE (@2), TREE_TYPE (@0))
1651 && tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@0))
1652 && (CONSTANT_CLASS_P (@1) || (single_use (@2) && single_use (@3))))
1653 (op @1 { build_zero_cst (TREE_TYPE (@1)); }))))
1654
1655 /* X - Y < X is the same as Y > 0 when there is no overflow.
1656 For equality, this is also true with wrapping overflow. */
1657 (for op (simple_comparison)
1658 (simplify
1659 (op:c @0 (minus@2 @0 @1))
1660 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1661 && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
1662 || ((op == EQ_EXPR || op == NE_EXPR)
1663 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))))
1664 && (CONSTANT_CLASS_P (@1) || single_use (@2)))
1665 (op @1 { build_zero_cst (TREE_TYPE (@1)); }))))
1666
1667 /* Transform:
1668 (X / Y) == 0 -> X < Y if X, Y are unsigned.
1669 (X / Y) != 0 -> X >= Y, if X, Y are unsigned. */
1670 (for cmp (eq ne)
1671 ocmp (lt ge)
1672 (simplify
1673 (cmp (trunc_div @0 @1) integer_zerop)
1674 (if (TYPE_UNSIGNED (TREE_TYPE (@0))
1675 /* Complex ==/!= is allowed, but not </>=. */
1676 && TREE_CODE (TREE_TYPE (@0)) != COMPLEX_TYPE
1677 && (VECTOR_TYPE_P (type) || !VECTOR_TYPE_P (TREE_TYPE (@0))))
1678 (ocmp @0 @1))))
1679
1680 /* X == C - X can never be true if C is odd. */
1681 (for cmp (eq ne)
1682 (simplify
1683 (cmp:c (convert? @0) (convert1? (minus INTEGER_CST@1 (convert2? @0))))
1684 (if (TREE_INT_CST_LOW (@1) & 1)
1685 { constant_boolean_node (cmp == NE_EXPR, type); })))
1686
1687 /* Arguments on which one can call get_nonzero_bits to get the bits
1688 possibly set. */
1689 (match with_possible_nonzero_bits
1690 INTEGER_CST@0)
1691 (match with_possible_nonzero_bits
1692 SSA_NAME@0
1693 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))))
1694 /* Slightly extended version, do not make it recursive to keep it cheap. */
1695 (match (with_possible_nonzero_bits2 @0)
1696 with_possible_nonzero_bits@0)
1697 (match (with_possible_nonzero_bits2 @0)
1698 (bit_and:c with_possible_nonzero_bits@0 @2))
1699
1700 /* Same for bits that are known to be set, but we do not have
1701 an equivalent to get_nonzero_bits yet. */
1702 (match (with_certain_nonzero_bits2 @0)
1703 INTEGER_CST@0)
1704 (match (with_certain_nonzero_bits2 @0)
1705 (bit_ior @1 INTEGER_CST@0))
1706
1707 /* X == C (or X & Z == Y | C) is impossible if ~nonzero(X) & C != 0. */
1708 (for cmp (eq ne)
1709 (simplify
1710 (cmp:c (with_possible_nonzero_bits2 @0) (with_certain_nonzero_bits2 @1))
1711 (if (wi::bit_and_not (wi::to_wide (@1), get_nonzero_bits (@0)) != 0)
1712 { constant_boolean_node (cmp == NE_EXPR, type); })))
1713
1714 /* ((X inner_op C0) outer_op C1)
1715 With X being a tree where value_range has reasoned certain bits to always be
1716 zero throughout its computed value range,
1717 inner_op = {|,^}, outer_op = {|,^} and inner_op != outer_op
1718 where zero_mask has 1's for all bits that are sure to be 0 in
1719 and 0's otherwise.
1720 if (inner_op == '^') C0 &= ~C1;
1721 if ((C0 & ~zero_mask) == 0) then emit (X outer_op (C0 outer_op C1)
1722 if ((C1 & ~zero_mask) == 0) then emit (X inner_op (C0 outer_op C1)
1723 */
1724 (for inner_op (bit_ior bit_xor)
1725 outer_op (bit_xor bit_ior)
1726 (simplify
1727 (outer_op
1728 (inner_op:s @2 INTEGER_CST@0) INTEGER_CST@1)
1729 (with
1730 {
1731 bool fail = false;
1732 wide_int zero_mask_not;
1733 wide_int C0;
1734 wide_int cst_emit;
1735
1736 if (TREE_CODE (@2) == SSA_NAME)
1737 zero_mask_not = get_nonzero_bits (@2);
1738 else
1739 fail = true;
1740
1741 if (inner_op == BIT_XOR_EXPR)
1742 {
1743 C0 = wi::bit_and_not (wi::to_wide (@0), wi::to_wide (@1));
1744 cst_emit = C0 | wi::to_wide (@1);
1745 }
1746 else
1747 {
1748 C0 = wi::to_wide (@0);
1749 cst_emit = C0 ^ wi::to_wide (@1);
1750 }
1751 }
1752 (if (!fail && (C0 & zero_mask_not) == 0)
1753 (outer_op @2 { wide_int_to_tree (type, cst_emit); })
1754 (if (!fail && (wi::to_wide (@1) & zero_mask_not) == 0)
1755 (inner_op @2 { wide_int_to_tree (type, cst_emit); }))))))
1756
1757 /* Associate (p +p off1) +p off2 as (p +p (off1 + off2)). */
1758 (simplify
1759 (pointer_plus (pointer_plus:s @0 @1) @3)
1760 (pointer_plus @0 (plus @1 @3)))
1761
1762 /* Pattern match
1763 tem1 = (long) ptr1;
1764 tem2 = (long) ptr2;
1765 tem3 = tem2 - tem1;
1766 tem4 = (unsigned long) tem3;
1767 tem5 = ptr1 + tem4;
1768 and produce
1769 tem5 = ptr2; */
1770 (simplify
1771 (pointer_plus @0 (convert?@2 (minus@3 (convert @1) (convert @0))))
1772 /* Conditionally look through a sign-changing conversion. */
1773 (if (TYPE_PRECISION (TREE_TYPE (@2)) == TYPE_PRECISION (TREE_TYPE (@3))
1774 && ((GIMPLE && useless_type_conversion_p (type, TREE_TYPE (@1)))
1775 || (GENERIC && type == TREE_TYPE (@1))))
1776 @1))
1777 (simplify
1778 (pointer_plus @0 (convert?@2 (pointer_diff@3 @1 @@0)))
1779 (if (TYPE_PRECISION (TREE_TYPE (@2)) >= TYPE_PRECISION (TREE_TYPE (@3)))
1780 (convert @1)))
1781
1782 /* Pattern match
1783 tem = (sizetype) ptr;
1784 tem = tem & algn;
1785 tem = -tem;
1786 ... = ptr p+ tem;
1787 and produce the simpler and easier to analyze with respect to alignment
1788 ... = ptr & ~algn; */
1789 (simplify
1790 (pointer_plus @0 (negate (bit_and (convert @0) INTEGER_CST@1)))
1791 (with { tree algn = wide_int_to_tree (TREE_TYPE (@0), ~wi::to_wide (@1)); }
1792 (bit_and @0 { algn; })))
1793
1794 /* Try folding difference of addresses. */
1795 (simplify
1796 (minus (convert ADDR_EXPR@0) (convert @1))
1797 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1798 (with { poly_int64 diff; }
1799 (if (ptr_difference_const (@0, @1, &diff))
1800 { build_int_cst_type (type, diff); }))))
1801 (simplify
1802 (minus (convert @0) (convert ADDR_EXPR@1))
1803 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1804 (with { poly_int64 diff; }
1805 (if (ptr_difference_const (@0, @1, &diff))
1806 { build_int_cst_type (type, diff); }))))
1807 (simplify
1808 (pointer_diff (convert?@2 ADDR_EXPR@0) (convert1?@3 @1))
1809 (if (tree_nop_conversion_p (TREE_TYPE(@2), TREE_TYPE (@0))
1810 && tree_nop_conversion_p (TREE_TYPE(@3), TREE_TYPE (@1)))
1811 (with { poly_int64 diff; }
1812 (if (ptr_difference_const (@0, @1, &diff))
1813 { build_int_cst_type (type, diff); }))))
1814 (simplify
1815 (pointer_diff (convert?@2 @0) (convert1?@3 ADDR_EXPR@1))
1816 (if (tree_nop_conversion_p (TREE_TYPE(@2), TREE_TYPE (@0))
1817 && tree_nop_conversion_p (TREE_TYPE(@3), TREE_TYPE (@1)))
1818 (with { poly_int64 diff; }
1819 (if (ptr_difference_const (@0, @1, &diff))
1820 { build_int_cst_type (type, diff); }))))
1821
1822 /* If arg0 is derived from the address of an object or function, we may
1823 be able to fold this expression using the object or function's
1824 alignment. */
1825 (simplify
1826 (bit_and (convert? @0) INTEGER_CST@1)
1827 (if (POINTER_TYPE_P (TREE_TYPE (@0))
1828 && tree_nop_conversion_p (type, TREE_TYPE (@0)))
1829 (with
1830 {
1831 unsigned int align;
1832 unsigned HOST_WIDE_INT bitpos;
1833 get_pointer_alignment_1 (@0, &align, &bitpos);
1834 }
1835 (if (wi::ltu_p (wi::to_wide (@1), align / BITS_PER_UNIT))
1836 { wide_int_to_tree (type, (wi::to_wide (@1)
1837 & (bitpos / BITS_PER_UNIT))); }))))
1838
1839
1840 /* We can't reassociate at all for saturating types. */
1841 (if (!TYPE_SATURATING (type))
1842
1843 /* Contract negates. */
1844 /* A + (-B) -> A - B */
1845 (simplify
1846 (plus:c @0 (convert? (negate @1)))
1847 /* Apply STRIP_NOPS on the negate. */
1848 (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
1849 && !TYPE_OVERFLOW_SANITIZED (type))
1850 (with
1851 {
1852 tree t1 = type;
1853 if (INTEGRAL_TYPE_P (type)
1854 && TYPE_OVERFLOW_WRAPS (type) != TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
1855 t1 = TYPE_OVERFLOW_WRAPS (type) ? type : TREE_TYPE (@1);
1856 }
1857 (convert (minus (convert:t1 @0) (convert:t1 @1))))))
1858 /* A - (-B) -> A + B */
1859 (simplify
1860 (minus @0 (convert? (negate @1)))
1861 (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
1862 && !TYPE_OVERFLOW_SANITIZED (type))
1863 (with
1864 {
1865 tree t1 = type;
1866 if (INTEGRAL_TYPE_P (type)
1867 && TYPE_OVERFLOW_WRAPS (type) != TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
1868 t1 = TYPE_OVERFLOW_WRAPS (type) ? type : TREE_TYPE (@1);
1869 }
1870 (convert (plus (convert:t1 @0) (convert:t1 @1))))))
1871 /* -(T)(-A) -> (T)A
1872 Sign-extension is ok except for INT_MIN, which thankfully cannot
1873 happen without overflow. */
1874 (simplify
1875 (negate (convert (negate @1)))
1876 (if (INTEGRAL_TYPE_P (type)
1877 && (TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@1))
1878 || (!TYPE_UNSIGNED (TREE_TYPE (@1))
1879 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@1))))
1880 && !TYPE_OVERFLOW_SANITIZED (type)
1881 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@1)))
1882 (convert @1)))
1883 (simplify
1884 (negate (convert negate_expr_p@1))
1885 (if (SCALAR_FLOAT_TYPE_P (type)
1886 && ((DECIMAL_FLOAT_TYPE_P (type)
1887 == DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@1))
1888 && TYPE_PRECISION (type) >= TYPE_PRECISION (TREE_TYPE (@1)))
1889 || !HONOR_SIGN_DEPENDENT_ROUNDING (type)))
1890 (convert (negate @1))))
1891 (simplify
1892 (negate (nop_convert (negate @1)))
1893 (if (!TYPE_OVERFLOW_SANITIZED (type)
1894 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@1)))
1895 (view_convert @1)))
1896
1897 /* We can't reassociate floating-point unless -fassociative-math
1898 or fixed-point plus or minus because of saturation to +-Inf. */
1899 (if ((!FLOAT_TYPE_P (type) || flag_associative_math)
1900 && !FIXED_POINT_TYPE_P (type))
1901
1902 /* Match patterns that allow contracting a plus-minus pair
1903 irrespective of overflow issues. */
1904 /* (A +- B) - A -> +- B */
1905 /* (A +- B) -+ B -> A */
1906 /* A - (A +- B) -> -+ B */
1907 /* A +- (B -+ A) -> +- B */
1908 (simplify
1909 (minus (plus:c @0 @1) @0)
1910 @1)
1911 (simplify
1912 (minus (minus @0 @1) @0)
1913 (negate @1))
1914 (simplify
1915 (plus:c (minus @0 @1) @1)
1916 @0)
1917 (simplify
1918 (minus @0 (plus:c @0 @1))
1919 (negate @1))
1920 (simplify
1921 (minus @0 (minus @0 @1))
1922 @1)
1923 /* (A +- B) + (C - A) -> C +- B */
1924 /* (A + B) - (A - C) -> B + C */
1925 /* More cases are handled with comparisons. */
1926 (simplify
1927 (plus:c (plus:c @0 @1) (minus @2 @0))
1928 (plus @2 @1))
1929 (simplify
1930 (plus:c (minus @0 @1) (minus @2 @0))
1931 (minus @2 @1))
1932 (simplify
1933 (plus:c (pointer_diff @0 @1) (pointer_diff @2 @0))
1934 (if (TYPE_OVERFLOW_UNDEFINED (type)
1935 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@0)))
1936 (pointer_diff @2 @1)))
1937 (simplify
1938 (minus (plus:c @0 @1) (minus @0 @2))
1939 (plus @1 @2))
1940
1941 /* (A +- CST1) +- CST2 -> A + CST3
1942 Use view_convert because it is safe for vectors and equivalent for
1943 scalars. */
1944 (for outer_op (plus minus)
1945 (for inner_op (plus minus)
1946 neg_inner_op (minus plus)
1947 (simplify
1948 (outer_op (nop_convert (inner_op @0 CONSTANT_CLASS_P@1))
1949 CONSTANT_CLASS_P@2)
1950 /* If one of the types wraps, use that one. */
1951 (if (!ANY_INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_WRAPS (type))
1952 /* If all 3 captures are CONSTANT_CLASS_P, punt, as we might recurse
1953 forever if something doesn't simplify into a constant. */
1954 (if (!CONSTANT_CLASS_P (@0))
1955 (if (outer_op == PLUS_EXPR)
1956 (plus (view_convert @0) (inner_op @2 (view_convert @1)))
1957 (minus (view_convert @0) (neg_inner_op @2 (view_convert @1)))))
1958 (if (!ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1959 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
1960 (if (outer_op == PLUS_EXPR)
1961 (view_convert (plus @0 (inner_op (view_convert @2) @1)))
1962 (view_convert (minus @0 (neg_inner_op (view_convert @2) @1))))
1963 /* If the constant operation overflows we cannot do the transform
1964 directly as we would introduce undefined overflow, for example
1965 with (a - 1) + INT_MIN. */
1966 (if (types_match (type, @0))
1967 (with { tree cst = const_binop (outer_op == inner_op
1968 ? PLUS_EXPR : MINUS_EXPR,
1969 type, @1, @2); }
1970 (if (cst && !TREE_OVERFLOW (cst))
1971 (inner_op @0 { cst; } )
1972 /* X+INT_MAX+1 is X-INT_MIN. */
1973 (if (INTEGRAL_TYPE_P (type) && cst
1974 && wi::to_wide (cst) == wi::min_value (type))
1975 (neg_inner_op @0 { wide_int_to_tree (type, wi::to_wide (cst)); })
1976 /* Last resort, use some unsigned type. */
1977 (with { tree utype = unsigned_type_for (type); }
1978 (if (utype)
1979 (view_convert (inner_op
1980 (view_convert:utype @0)
1981 (view_convert:utype
1982 { drop_tree_overflow (cst); }))))))))))))))
1983
1984 /* (CST1 - A) +- CST2 -> CST3 - A */
1985 (for outer_op (plus minus)
1986 (simplify
1987 (outer_op (minus CONSTANT_CLASS_P@1 @0) CONSTANT_CLASS_P@2)
1988 (with { tree cst = const_binop (outer_op, type, @1, @2); }
1989 (if (cst && !TREE_OVERFLOW (cst))
1990 (minus { cst; } @0)))))
1991
1992 /* CST1 - (CST2 - A) -> CST3 + A */
1993 (simplify
1994 (minus CONSTANT_CLASS_P@1 (minus CONSTANT_CLASS_P@2 @0))
1995 (with { tree cst = const_binop (MINUS_EXPR, type, @1, @2); }
1996 (if (cst && !TREE_OVERFLOW (cst))
1997 (plus { cst; } @0))))
1998
1999 /* ~A + A -> -1 */
2000 (simplify
2001 (plus:c (bit_not @0) @0)
2002 (if (!TYPE_OVERFLOW_TRAPS (type))
2003 { build_all_ones_cst (type); }))
2004
2005 /* ~A + 1 -> -A */
2006 (simplify
2007 (plus (convert? (bit_not @0)) integer_each_onep)
2008 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
2009 (negate (convert @0))))
2010
2011 /* -A - 1 -> ~A */
2012 (simplify
2013 (minus (convert? (negate @0)) integer_each_onep)
2014 (if (!TYPE_OVERFLOW_TRAPS (type)
2015 && tree_nop_conversion_p (type, TREE_TYPE (@0)))
2016 (bit_not (convert @0))))
2017
2018 /* -1 - A -> ~A */
2019 (simplify
2020 (minus integer_all_onesp @0)
2021 (bit_not @0))
2022
2023 /* (T)(P + A) - (T)P -> (T) A */
2024 (simplify
2025 (minus (convert (plus:c @@0 @1))
2026 (convert? @0))
2027 (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
2028 /* For integer types, if A has a smaller type
2029 than T the result depends on the possible
2030 overflow in P + A.
2031 E.g. T=size_t, A=(unsigned)429497295, P>0.
2032 However, if an overflow in P + A would cause
2033 undefined behavior, we can assume that there
2034 is no overflow. */
2035 || (INTEGRAL_TYPE_P (TREE_TYPE (@1))
2036 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@1))))
2037 (convert @1)))
2038 (simplify
2039 (minus (convert (pointer_plus @@0 @1))
2040 (convert @0))
2041 (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
2042 /* For pointer types, if the conversion of A to the
2043 final type requires a sign- or zero-extension,
2044 then we have to punt - it is not defined which
2045 one is correct. */
2046 || (POINTER_TYPE_P (TREE_TYPE (@0))
2047 && TREE_CODE (@1) == INTEGER_CST
2048 && tree_int_cst_sign_bit (@1) == 0))
2049 (convert @1)))
2050 (simplify
2051 (pointer_diff (pointer_plus @@0 @1) @0)
2052 /* The second argument of pointer_plus must be interpreted as signed, and
2053 thus sign-extended if necessary. */
2054 (with { tree stype = signed_type_for (TREE_TYPE (@1)); }
2055 /* Use view_convert instead of convert here, as POINTER_PLUS_EXPR
2056 second arg is unsigned even when we need to consider it as signed,
2057 we don't want to diagnose overflow here. */
2058 (convert (view_convert:stype @1))))
2059
2060 /* (T)P - (T)(P + A) -> -(T) A */
2061 (simplify
2062 (minus (convert? @0)
2063 (convert (plus:c @@0 @1)))
2064 (if (INTEGRAL_TYPE_P (type)
2065 && TYPE_OVERFLOW_UNDEFINED (type)
2066 && element_precision (type) <= element_precision (TREE_TYPE (@1)))
2067 (with { tree utype = unsigned_type_for (type); }
2068 (convert (negate (convert:utype @1))))
2069 (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
2070 /* For integer types, if A has a smaller type
2071 than T the result depends on the possible
2072 overflow in P + A.
2073 E.g. T=size_t, A=(unsigned)429497295, P>0.
2074 However, if an overflow in P + A would cause
2075 undefined behavior, we can assume that there
2076 is no overflow. */
2077 || (INTEGRAL_TYPE_P (TREE_TYPE (@1))
2078 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@1))))
2079 (negate (convert @1)))))
2080 (simplify
2081 (minus (convert @0)
2082 (convert (pointer_plus @@0 @1)))
2083 (if (INTEGRAL_TYPE_P (type)
2084 && TYPE_OVERFLOW_UNDEFINED (type)
2085 && element_precision (type) <= element_precision (TREE_TYPE (@1)))
2086 (with { tree utype = unsigned_type_for (type); }
2087 (convert (negate (convert:utype @1))))
2088 (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
2089 /* For pointer types, if the conversion of A to the
2090 final type requires a sign- or zero-extension,
2091 then we have to punt - it is not defined which
2092 one is correct. */
2093 || (POINTER_TYPE_P (TREE_TYPE (@0))
2094 && TREE_CODE (@1) == INTEGER_CST
2095 && tree_int_cst_sign_bit (@1) == 0))
2096 (negate (convert @1)))))
2097 (simplify
2098 (pointer_diff @0 (pointer_plus @@0 @1))
2099 /* The second argument of pointer_plus must be interpreted as signed, and
2100 thus sign-extended if necessary. */
2101 (with { tree stype = signed_type_for (TREE_TYPE (@1)); }
2102 /* Use view_convert instead of convert here, as POINTER_PLUS_EXPR
2103 second arg is unsigned even when we need to consider it as signed,
2104 we don't want to diagnose overflow here. */
2105 (negate (convert (view_convert:stype @1)))))
2106
2107 /* (T)(P + A) - (T)(P + B) -> (T)A - (T)B */
2108 (simplify
2109 (minus (convert (plus:c @@0 @1))
2110 (convert (plus:c @0 @2)))
2111 (if (INTEGRAL_TYPE_P (type)
2112 && TYPE_OVERFLOW_UNDEFINED (type)
2113 && element_precision (type) <= element_precision (TREE_TYPE (@1))
2114 && element_precision (type) <= element_precision (TREE_TYPE (@2)))
2115 (with { tree utype = unsigned_type_for (type); }
2116 (convert (minus (convert:utype @1) (convert:utype @2))))
2117 (if (((element_precision (type) <= element_precision (TREE_TYPE (@1)))
2118 == (element_precision (type) <= element_precision (TREE_TYPE (@2))))
2119 && (element_precision (type) <= element_precision (TREE_TYPE (@1))
2120 /* For integer types, if A has a smaller type
2121 than T the result depends on the possible
2122 overflow in P + A.
2123 E.g. T=size_t, A=(unsigned)429497295, P>0.
2124 However, if an overflow in P + A would cause
2125 undefined behavior, we can assume that there
2126 is no overflow. */
2127 || (INTEGRAL_TYPE_P (TREE_TYPE (@1))
2128 && INTEGRAL_TYPE_P (TREE_TYPE (@2))
2129 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@1))
2130 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@2)))))
2131 (minus (convert @1) (convert @2)))))
2132 (simplify
2133 (minus (convert (pointer_plus @@0 @1))
2134 (convert (pointer_plus @0 @2)))
2135 (if (INTEGRAL_TYPE_P (type)
2136 && TYPE_OVERFLOW_UNDEFINED (type)
2137 && element_precision (type) <= element_precision (TREE_TYPE (@1)))
2138 (with { tree utype = unsigned_type_for (type); }
2139 (convert (minus (convert:utype @1) (convert:utype @2))))
2140 (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
2141 /* For pointer types, if the conversion of A to the
2142 final type requires a sign- or zero-extension,
2143 then we have to punt - it is not defined which
2144 one is correct. */
2145 || (POINTER_TYPE_P (TREE_TYPE (@0))
2146 && TREE_CODE (@1) == INTEGER_CST
2147 && tree_int_cst_sign_bit (@1) == 0
2148 && TREE_CODE (@2) == INTEGER_CST
2149 && tree_int_cst_sign_bit (@2) == 0))
2150 (minus (convert @1) (convert @2)))))
2151 (simplify
2152 (pointer_diff (pointer_plus @@0 @1) (pointer_plus @0 @2))
2153 /* The second argument of pointer_plus must be interpreted as signed, and
2154 thus sign-extended if necessary. */
2155 (with { tree stype = signed_type_for (TREE_TYPE (@1)); }
2156 /* Use view_convert instead of convert here, as POINTER_PLUS_EXPR
2157 second arg is unsigned even when we need to consider it as signed,
2158 we don't want to diagnose overflow here. */
2159 (minus (convert (view_convert:stype @1))
2160 (convert (view_convert:stype @2)))))))
2161
2162 /* (A * C) +- (B * C) -> (A+-B) * C and (A * C) +- A -> A * (C+-1).
2163 Modeled after fold_plusminus_mult_expr. */
2164 (if (!TYPE_SATURATING (type)
2165 && (!FLOAT_TYPE_P (type) || flag_associative_math))
2166 (for plusminus (plus minus)
2167 (simplify
2168 (plusminus (mult:cs@3 @0 @1) (mult:cs@4 @0 @2))
2169 (if ((!ANY_INTEGRAL_TYPE_P (type)
2170 || TYPE_OVERFLOW_WRAPS (type)
2171 || (INTEGRAL_TYPE_P (type)
2172 && tree_expr_nonzero_p (@0)
2173 && expr_not_equal_to (@0, wi::minus_one (TYPE_PRECISION (type)))))
2174 /* If @1 +- @2 is constant require a hard single-use on either
2175 original operand (but not on both). */
2176 && (single_use (@3) || single_use (@4)))
2177 (mult (plusminus @1 @2) @0)))
2178 /* We cannot generate constant 1 for fract. */
2179 (if (!ALL_FRACT_MODE_P (TYPE_MODE (type)))
2180 (simplify
2181 (plusminus @0 (mult:c@3 @0 @2))
2182 (if ((!ANY_INTEGRAL_TYPE_P (type)
2183 || TYPE_OVERFLOW_WRAPS (type)
2184 || (INTEGRAL_TYPE_P (type)
2185 && tree_expr_nonzero_p (@0)
2186 && expr_not_equal_to (@0, wi::minus_one (TYPE_PRECISION (type)))))
2187 && single_use (@3))
2188 (mult (plusminus { build_one_cst (type); } @2) @0)))
2189 (simplify
2190 (plusminus (mult:c@3 @0 @2) @0)
2191 (if ((!ANY_INTEGRAL_TYPE_P (type)
2192 || TYPE_OVERFLOW_WRAPS (type)
2193 || (INTEGRAL_TYPE_P (type)
2194 && tree_expr_nonzero_p (@0)
2195 && expr_not_equal_to (@0, wi::minus_one (TYPE_PRECISION (type)))))
2196 && single_use (@3))
2197 (mult (plusminus @2 { build_one_cst (type); }) @0))))))
2198
2199 /* Simplifications of MIN_EXPR, MAX_EXPR, fmin() and fmax(). */
2200
2201 (for minmax (min max FMIN_ALL FMAX_ALL)
2202 (simplify
2203 (minmax @0 @0)
2204 @0))
2205 /* min(max(x,y),y) -> y. */
2206 (simplify
2207 (min:c (max:c @0 @1) @1)
2208 @1)
2209 /* max(min(x,y),y) -> y. */
2210 (simplify
2211 (max:c (min:c @0 @1) @1)
2212 @1)
2213 /* max(a,-a) -> abs(a). */
2214 (simplify
2215 (max:c @0 (negate @0))
2216 (if (TREE_CODE (type) != COMPLEX_TYPE
2217 && (! ANY_INTEGRAL_TYPE_P (type)
2218 || TYPE_OVERFLOW_UNDEFINED (type)))
2219 (abs @0)))
2220 /* min(a,-a) -> -abs(a). */
2221 (simplify
2222 (min:c @0 (negate @0))
2223 (if (TREE_CODE (type) != COMPLEX_TYPE
2224 && (! ANY_INTEGRAL_TYPE_P (type)
2225 || TYPE_OVERFLOW_UNDEFINED (type)))
2226 (negate (abs @0))))
2227 (simplify
2228 (min @0 @1)
2229 (switch
2230 (if (INTEGRAL_TYPE_P (type)
2231 && TYPE_MIN_VALUE (type)
2232 && operand_equal_p (@1, TYPE_MIN_VALUE (type), OEP_ONLY_CONST))
2233 @1)
2234 (if (INTEGRAL_TYPE_P (type)
2235 && TYPE_MAX_VALUE (type)
2236 && operand_equal_p (@1, TYPE_MAX_VALUE (type), OEP_ONLY_CONST))
2237 @0)))
2238 (simplify
2239 (max @0 @1)
2240 (switch
2241 (if (INTEGRAL_TYPE_P (type)
2242 && TYPE_MAX_VALUE (type)
2243 && operand_equal_p (@1, TYPE_MAX_VALUE (type), OEP_ONLY_CONST))
2244 @1)
2245 (if (INTEGRAL_TYPE_P (type)
2246 && TYPE_MIN_VALUE (type)
2247 && operand_equal_p (@1, TYPE_MIN_VALUE (type), OEP_ONLY_CONST))
2248 @0)))
2249
2250 /* max (a, a + CST) -> a + CST where CST is positive. */
2251 /* max (a, a + CST) -> a where CST is negative. */
2252 (simplify
2253 (max:c @0 (plus@2 @0 INTEGER_CST@1))
2254 (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
2255 (if (tree_int_cst_sgn (@1) > 0)
2256 @2
2257 @0)))
2258
2259 /* min (a, a + CST) -> a where CST is positive. */
2260 /* min (a, a + CST) -> a + CST where CST is negative. */
2261 (simplify
2262 (min:c @0 (plus@2 @0 INTEGER_CST@1))
2263 (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
2264 (if (tree_int_cst_sgn (@1) > 0)
2265 @0
2266 @2)))
2267
2268 /* (convert (minmax ((convert (x) c)))) -> minmax (x c) if x is promoted
2269 and the outer convert demotes the expression back to x's type. */
2270 (for minmax (min max)
2271 (simplify
2272 (convert (minmax@0 (convert @1) INTEGER_CST@2))
2273 (if (INTEGRAL_TYPE_P (type)
2274 && types_match (@1, type) && int_fits_type_p (@2, type)
2275 && TYPE_SIGN (TREE_TYPE (@0)) == TYPE_SIGN (type)
2276 && TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (type))
2277 (minmax @1 (convert @2)))))
2278
2279 (for minmax (FMIN_ALL FMAX_ALL)
2280 /* If either argument is NaN, return the other one. Avoid the
2281 transformation if we get (and honor) a signalling NaN. */
2282 (simplify
2283 (minmax:c @0 REAL_CST@1)
2284 (if (real_isnan (TREE_REAL_CST_PTR (@1))
2285 && (!HONOR_SNANS (@1) || !TREE_REAL_CST (@1).signalling))
2286 @0)))
2287 /* Convert fmin/fmax to MIN_EXPR/MAX_EXPR. C99 requires these
2288 functions to return the numeric arg if the other one is NaN.
2289 MIN and MAX don't honor that, so only transform if -ffinite-math-only
2290 is set. C99 doesn't require -0.0 to be handled, so we don't have to
2291 worry about it either. */
2292 (if (flag_finite_math_only)
2293 (simplify
2294 (FMIN_ALL @0 @1)
2295 (min @0 @1))
2296 (simplify
2297 (FMAX_ALL @0 @1)
2298 (max @0 @1)))
2299 /* min (-A, -B) -> -max (A, B) */
2300 (for minmax (min max FMIN_ALL FMAX_ALL)
2301 maxmin (max min FMAX_ALL FMIN_ALL)
2302 (simplify
2303 (minmax (negate:s@2 @0) (negate:s@3 @1))
2304 (if (FLOAT_TYPE_P (TREE_TYPE (@0))
2305 || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2306 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))))
2307 (negate (maxmin @0 @1)))))
2308 /* MIN (~X, ~Y) -> ~MAX (X, Y)
2309 MAX (~X, ~Y) -> ~MIN (X, Y) */
2310 (for minmax (min max)
2311 maxmin (max min)
2312 (simplify
2313 (minmax (bit_not:s@2 @0) (bit_not:s@3 @1))
2314 (bit_not (maxmin @0 @1))))
2315
2316 /* MIN (X, Y) == X -> X <= Y */
2317 (for minmax (min min max max)
2318 cmp (eq ne eq ne )
2319 out (le gt ge lt )
2320 (simplify
2321 (cmp:c (minmax:c @0 @1) @0)
2322 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0)))
2323 (out @0 @1))))
2324 /* MIN (X, 5) == 0 -> X == 0
2325 MIN (X, 5) == 7 -> false */
2326 (for cmp (eq ne)
2327 (simplify
2328 (cmp (min @0 INTEGER_CST@1) INTEGER_CST@2)
2329 (if (wi::lt_p (wi::to_wide (@1), wi::to_wide (@2),
2330 TYPE_SIGN (TREE_TYPE (@0))))
2331 { constant_boolean_node (cmp == NE_EXPR, type); }
2332 (if (wi::gt_p (wi::to_wide (@1), wi::to_wide (@2),
2333 TYPE_SIGN (TREE_TYPE (@0))))
2334 (cmp @0 @2)))))
2335 (for cmp (eq ne)
2336 (simplify
2337 (cmp (max @0 INTEGER_CST@1) INTEGER_CST@2)
2338 (if (wi::gt_p (wi::to_wide (@1), wi::to_wide (@2),
2339 TYPE_SIGN (TREE_TYPE (@0))))
2340 { constant_boolean_node (cmp == NE_EXPR, type); }
2341 (if (wi::lt_p (wi::to_wide (@1), wi::to_wide (@2),
2342 TYPE_SIGN (TREE_TYPE (@0))))
2343 (cmp @0 @2)))))
2344 /* MIN (X, C1) < C2 -> X < C2 || C1 < C2 */
2345 (for minmax (min min max max min min max max )
2346 cmp (lt le gt ge gt ge lt le )
2347 comb (bit_ior bit_ior bit_ior bit_ior bit_and bit_and bit_and bit_and)
2348 (simplify
2349 (cmp (minmax @0 INTEGER_CST@1) INTEGER_CST@2)
2350 (comb (cmp @0 @2) (cmp @1 @2))))
2351
2352 /* Simplifications of shift and rotates. */
2353
2354 (for rotate (lrotate rrotate)
2355 (simplify
2356 (rotate integer_all_onesp@0 @1)
2357 @0))
2358
2359 /* Optimize -1 >> x for arithmetic right shifts. */
2360 (simplify
2361 (rshift integer_all_onesp@0 @1)
2362 (if (!TYPE_UNSIGNED (type)
2363 && tree_expr_nonnegative_p (@1))
2364 @0))
2365
2366 /* Optimize (x >> c) << c into x & (-1<<c). */
2367 (simplify
2368 (lshift (rshift @0 INTEGER_CST@1) @1)
2369 (if (wi::ltu_p (wi::to_wide (@1), element_precision (type)))
2370 (bit_and @0 (lshift { build_minus_one_cst (type); } @1))))
2371
2372 /* Optimize (x << c) >> c into x & ((unsigned)-1 >> c) for unsigned
2373 types. */
2374 (simplify
2375 (rshift (lshift @0 INTEGER_CST@1) @1)
2376 (if (TYPE_UNSIGNED (type)
2377 && (wi::ltu_p (wi::to_wide (@1), element_precision (type))))
2378 (bit_and @0 (rshift { build_minus_one_cst (type); } @1))))
2379
2380 (for shiftrotate (lrotate rrotate lshift rshift)
2381 (simplify
2382 (shiftrotate @0 integer_zerop)
2383 (non_lvalue @0))
2384 (simplify
2385 (shiftrotate integer_zerop@0 @1)
2386 @0)
2387 /* Prefer vector1 << scalar to vector1 << vector2
2388 if vector2 is uniform. */
2389 (for vec (VECTOR_CST CONSTRUCTOR)
2390 (simplify
2391 (shiftrotate @0 vec@1)
2392 (with { tree tem = uniform_vector_p (@1); }
2393 (if (tem)
2394 (shiftrotate @0 { tem; }))))))
2395
2396 /* Simplify X << Y where Y's low width bits are 0 to X, as only valid
2397 Y is 0. Similarly for X >> Y. */
2398 #if GIMPLE
2399 (for shift (lshift rshift)
2400 (simplify
2401 (shift @0 SSA_NAME@1)
2402 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1)))
2403 (with {
2404 int width = ceil_log2 (element_precision (TREE_TYPE (@0)));
2405 int prec = TYPE_PRECISION (TREE_TYPE (@1));
2406 }
2407 (if ((get_nonzero_bits (@1) & wi::mask (width, false, prec)) == 0)
2408 @0)))))
2409 #endif
2410
2411 /* Rewrite an LROTATE_EXPR by a constant into an
2412 RROTATE_EXPR by a new constant. */
2413 (simplify
2414 (lrotate @0 INTEGER_CST@1)
2415 (rrotate @0 { const_binop (MINUS_EXPR, TREE_TYPE (@1),
2416 build_int_cst (TREE_TYPE (@1),
2417 element_precision (type)), @1); }))
2418
2419 /* Turn (a OP c1) OP c2 into a OP (c1+c2). */
2420 (for op (lrotate rrotate rshift lshift)
2421 (simplify
2422 (op (op @0 INTEGER_CST@1) INTEGER_CST@2)
2423 (with { unsigned int prec = element_precision (type); }
2424 (if (wi::ge_p (wi::to_wide (@1), 0, TYPE_SIGN (TREE_TYPE (@1)))
2425 && wi::lt_p (wi::to_wide (@1), prec, TYPE_SIGN (TREE_TYPE (@1)))
2426 && wi::ge_p (wi::to_wide (@2), 0, TYPE_SIGN (TREE_TYPE (@2)))
2427 && wi::lt_p (wi::to_wide (@2), prec, TYPE_SIGN (TREE_TYPE (@2))))
2428 (with { unsigned int low = (tree_to_uhwi (@1)
2429 + tree_to_uhwi (@2)); }
2430 /* Deal with a OP (c1 + c2) being undefined but (a OP c1) OP c2
2431 being well defined. */
2432 (if (low >= prec)
2433 (if (op == LROTATE_EXPR || op == RROTATE_EXPR)
2434 (op @0 { build_int_cst (TREE_TYPE (@1), low % prec); })
2435 (if (TYPE_UNSIGNED (type) || op == LSHIFT_EXPR)
2436 { build_zero_cst (type); }
2437 (op @0 { build_int_cst (TREE_TYPE (@1), prec - 1); })))
2438 (op @0 { build_int_cst (TREE_TYPE (@1), low); })))))))
2439
2440
2441 /* ((1 << A) & 1) != 0 -> A == 0
2442 ((1 << A) & 1) == 0 -> A != 0 */
2443 (for cmp (ne eq)
2444 icmp (eq ne)
2445 (simplify
2446 (cmp (bit_and (lshift integer_onep @0) integer_onep) integer_zerop)
2447 (icmp @0 { build_zero_cst (TREE_TYPE (@0)); })))
2448
2449 /* (CST1 << A) == CST2 -> A == ctz (CST2) - ctz (CST1)
2450 (CST1 << A) != CST2 -> A != ctz (CST2) - ctz (CST1)
2451 if CST2 != 0. */
2452 (for cmp (ne eq)
2453 (simplify
2454 (cmp (lshift INTEGER_CST@0 @1) INTEGER_CST@2)
2455 (with { int cand = wi::ctz (wi::to_wide (@2)) - wi::ctz (wi::to_wide (@0)); }
2456 (if (cand < 0
2457 || (!integer_zerop (@2)
2458 && wi::lshift (wi::to_wide (@0), cand) != wi::to_wide (@2)))
2459 { constant_boolean_node (cmp == NE_EXPR, type); }
2460 (if (!integer_zerop (@2)
2461 && wi::lshift (wi::to_wide (@0), cand) == wi::to_wide (@2))
2462 (cmp @1 { build_int_cst (TREE_TYPE (@1), cand); }))))))
2463
2464 /* Fold (X << C1) & C2 into (X << C1) & (C2 | ((1 << C1) - 1))
2465 (X >> C1) & C2 into (X >> C1) & (C2 | ~((type) -1 >> C1))
2466 if the new mask might be further optimized. */
2467 (for shift (lshift rshift)
2468 (simplify
2469 (bit_and (convert?:s@4 (shift:s@5 (convert1?@3 @0) INTEGER_CST@1))
2470 INTEGER_CST@2)
2471 (if (tree_nop_conversion_p (TREE_TYPE (@4), TREE_TYPE (@5))
2472 && TYPE_PRECISION (type) <= HOST_BITS_PER_WIDE_INT
2473 && tree_fits_uhwi_p (@1)
2474 && tree_to_uhwi (@1) > 0
2475 && tree_to_uhwi (@1) < TYPE_PRECISION (type))
2476 (with
2477 {
2478 unsigned int shiftc = tree_to_uhwi (@1);
2479 unsigned HOST_WIDE_INT mask = TREE_INT_CST_LOW (@2);
2480 unsigned HOST_WIDE_INT newmask, zerobits = 0;
2481 tree shift_type = TREE_TYPE (@3);
2482 unsigned int prec;
2483
2484 if (shift == LSHIFT_EXPR)
2485 zerobits = ((HOST_WIDE_INT_1U << shiftc) - 1);
2486 else if (shift == RSHIFT_EXPR
2487 && type_has_mode_precision_p (shift_type))
2488 {
2489 prec = TYPE_PRECISION (TREE_TYPE (@3));
2490 tree arg00 = @0;
2491 /* See if more bits can be proven as zero because of
2492 zero extension. */
2493 if (@3 != @0
2494 && TYPE_UNSIGNED (TREE_TYPE (@0)))
2495 {
2496 tree inner_type = TREE_TYPE (@0);
2497 if (type_has_mode_precision_p (inner_type)
2498 && TYPE_PRECISION (inner_type) < prec)
2499 {
2500 prec = TYPE_PRECISION (inner_type);
2501 /* See if we can shorten the right shift. */
2502 if (shiftc < prec)
2503 shift_type = inner_type;
2504 /* Otherwise X >> C1 is all zeros, so we'll optimize
2505 it into (X, 0) later on by making sure zerobits
2506 is all ones. */
2507 }
2508 }
2509 zerobits = HOST_WIDE_INT_M1U;
2510 if (shiftc < prec)
2511 {
2512 zerobits >>= HOST_BITS_PER_WIDE_INT - shiftc;
2513 zerobits <<= prec - shiftc;
2514 }
2515 /* For arithmetic shift if sign bit could be set, zerobits
2516 can contain actually sign bits, so no transformation is
2517 possible, unless MASK masks them all away. In that
2518 case the shift needs to be converted into logical shift. */
2519 if (!TYPE_UNSIGNED (TREE_TYPE (@3))
2520 && prec == TYPE_PRECISION (TREE_TYPE (@3)))
2521 {
2522 if ((mask & zerobits) == 0)
2523 shift_type = unsigned_type_for (TREE_TYPE (@3));
2524 else
2525 zerobits = 0;
2526 }
2527 }
2528 }
2529 /* ((X << 16) & 0xff00) is (X, 0). */
2530 (if ((mask & zerobits) == mask)
2531 { build_int_cst (type, 0); }
2532 (with { newmask = mask | zerobits; }
2533 (if (newmask != mask && (newmask & (newmask + 1)) == 0)
2534 (with
2535 {
2536 /* Only do the transformation if NEWMASK is some integer
2537 mode's mask. */
2538 for (prec = BITS_PER_UNIT;
2539 prec < HOST_BITS_PER_WIDE_INT; prec <<= 1)
2540 if (newmask == (HOST_WIDE_INT_1U << prec) - 1)
2541 break;
2542 }
2543 (if (prec < HOST_BITS_PER_WIDE_INT
2544 || newmask == HOST_WIDE_INT_M1U)
2545 (with
2546 { tree newmaskt = build_int_cst_type (TREE_TYPE (@2), newmask); }
2547 (if (!tree_int_cst_equal (newmaskt, @2))
2548 (if (shift_type != TREE_TYPE (@3))
2549 (bit_and (convert (shift:shift_type (convert @3) @1)) { newmaskt; })
2550 (bit_and @4 { newmaskt; })))))))))))))
2551
2552 /* Fold (X {&,^,|} C2) << C1 into (X << C1) {&,^,|} (C2 << C1)
2553 (X {&,^,|} C2) >> C1 into (X >> C1) & (C2 >> C1). */
2554 (for shift (lshift rshift)
2555 (for bit_op (bit_and bit_xor bit_ior)
2556 (simplify
2557 (shift (convert?:s (bit_op:s @0 INTEGER_CST@2)) INTEGER_CST@1)
2558 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
2559 (with { tree mask = int_const_binop (shift, fold_convert (type, @2), @1); }
2560 (bit_op (shift (convert @0) @1) { mask; }))))))
2561
2562 /* ~(~X >> Y) -> X >> Y (for arithmetic shift). */
2563 (simplify
2564 (bit_not (convert1?:s (rshift:s (convert2?@0 (bit_not @1)) @2)))
2565 (if (!TYPE_UNSIGNED (TREE_TYPE (@0))
2566 && (element_precision (TREE_TYPE (@0))
2567 <= element_precision (TREE_TYPE (@1))
2568 || !TYPE_UNSIGNED (TREE_TYPE (@1))))
2569 (with
2570 { tree shift_type = TREE_TYPE (@0); }
2571 (convert (rshift (convert:shift_type @1) @2)))))
2572
2573 /* ~(~X >>r Y) -> X >>r Y
2574 ~(~X <<r Y) -> X <<r Y */
2575 (for rotate (lrotate rrotate)
2576 (simplify
2577 (bit_not (convert1?:s (rotate:s (convert2?@0 (bit_not @1)) @2)))
2578 (if ((element_precision (TREE_TYPE (@0))
2579 <= element_precision (TREE_TYPE (@1))
2580 || !TYPE_UNSIGNED (TREE_TYPE (@1)))
2581 && (element_precision (type) <= element_precision (TREE_TYPE (@0))
2582 || !TYPE_UNSIGNED (TREE_TYPE (@0))))
2583 (with
2584 { tree rotate_type = TREE_TYPE (@0); }
2585 (convert (rotate (convert:rotate_type @1) @2))))))
2586
2587 /* Simplifications of conversions. */
2588
2589 /* Basic strip-useless-type-conversions / strip_nops. */
2590 (for cvt (convert view_convert float fix_trunc)
2591 (simplify
2592 (cvt @0)
2593 (if ((GIMPLE && useless_type_conversion_p (type, TREE_TYPE (@0)))
2594 || (GENERIC && type == TREE_TYPE (@0)))
2595 @0)))
2596
2597 /* Contract view-conversions. */
2598 (simplify
2599 (view_convert (view_convert @0))
2600 (view_convert @0))
2601
2602 /* For integral conversions with the same precision or pointer
2603 conversions use a NOP_EXPR instead. */
2604 (simplify
2605 (view_convert @0)
2606 (if ((INTEGRAL_TYPE_P (type) || POINTER_TYPE_P (type))
2607 && (INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))
2608 && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (@0)))
2609 (convert @0)))
2610
2611 /* Strip inner integral conversions that do not change precision or size, or
2612 zero-extend while keeping the same size (for bool-to-char). */
2613 (simplify
2614 (view_convert (convert@0 @1))
2615 (if ((INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))
2616 && (INTEGRAL_TYPE_P (TREE_TYPE (@1)) || POINTER_TYPE_P (TREE_TYPE (@1)))
2617 && TYPE_SIZE (TREE_TYPE (@0)) == TYPE_SIZE (TREE_TYPE (@1))
2618 && (TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1))
2619 || (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (TREE_TYPE (@1))
2620 && TYPE_UNSIGNED (TREE_TYPE (@1)))))
2621 (view_convert @1)))
2622
2623 /* Simplify a view-converted empty constructor. */
2624 (simplify
2625 (view_convert CONSTRUCTOR@0)
2626 (if (TREE_CODE (@0) != SSA_NAME
2627 && CONSTRUCTOR_NELTS (@0) == 0)
2628 { build_zero_cst (type); }))
2629
2630 /* Re-association barriers around constants and other re-association
2631 barriers can be removed. */
2632 (simplify
2633 (paren CONSTANT_CLASS_P@0)
2634 @0)
2635 (simplify
2636 (paren (paren@1 @0))
2637 @1)
2638
2639 /* Handle cases of two conversions in a row. */
2640 (for ocvt (convert float fix_trunc)
2641 (for icvt (convert float)
2642 (simplify
2643 (ocvt (icvt@1 @0))
2644 (with
2645 {
2646 tree inside_type = TREE_TYPE (@0);
2647 tree inter_type = TREE_TYPE (@1);
2648 int inside_int = INTEGRAL_TYPE_P (inside_type);
2649 int inside_ptr = POINTER_TYPE_P (inside_type);
2650 int inside_float = FLOAT_TYPE_P (inside_type);
2651 int inside_vec = VECTOR_TYPE_P (inside_type);
2652 unsigned int inside_prec = TYPE_PRECISION (inside_type);
2653 int inside_unsignedp = TYPE_UNSIGNED (inside_type);
2654 int inter_int = INTEGRAL_TYPE_P (inter_type);
2655 int inter_ptr = POINTER_TYPE_P (inter_type);
2656 int inter_float = FLOAT_TYPE_P (inter_type);
2657 int inter_vec = VECTOR_TYPE_P (inter_type);
2658 unsigned int inter_prec = TYPE_PRECISION (inter_type);
2659 int inter_unsignedp = TYPE_UNSIGNED (inter_type);
2660 int final_int = INTEGRAL_TYPE_P (type);
2661 int final_ptr = POINTER_TYPE_P (type);
2662 int final_float = FLOAT_TYPE_P (type);
2663 int final_vec = VECTOR_TYPE_P (type);
2664 unsigned int final_prec = TYPE_PRECISION (type);
2665 int final_unsignedp = TYPE_UNSIGNED (type);
2666 }
2667 (switch
2668 /* In addition to the cases of two conversions in a row
2669 handled below, if we are converting something to its own
2670 type via an object of identical or wider precision, neither
2671 conversion is needed. */
2672 (if (((GIMPLE && useless_type_conversion_p (type, inside_type))
2673 || (GENERIC
2674 && TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (inside_type)))
2675 && (((inter_int || inter_ptr) && final_int)
2676 || (inter_float && final_float))
2677 && inter_prec >= final_prec)
2678 (ocvt @0))
2679
2680 /* Likewise, if the intermediate and initial types are either both
2681 float or both integer, we don't need the middle conversion if the
2682 former is wider than the latter and doesn't change the signedness
2683 (for integers). Avoid this if the final type is a pointer since
2684 then we sometimes need the middle conversion. */
2685 (if (((inter_int && inside_int) || (inter_float && inside_float))
2686 && (final_int || final_float)
2687 && inter_prec >= inside_prec
2688 && (inter_float || inter_unsignedp == inside_unsignedp))
2689 (ocvt @0))
2690
2691 /* If we have a sign-extension of a zero-extended value, we can
2692 replace that by a single zero-extension. Likewise if the
2693 final conversion does not change precision we can drop the
2694 intermediate conversion. */
2695 (if (inside_int && inter_int && final_int
2696 && ((inside_prec < inter_prec && inter_prec < final_prec
2697 && inside_unsignedp && !inter_unsignedp)
2698 || final_prec == inter_prec))
2699 (ocvt @0))
2700
2701 /* Two conversions in a row are not needed unless:
2702 - some conversion is floating-point (overstrict for now), or
2703 - some conversion is a vector (overstrict for now), or
2704 - the intermediate type is narrower than both initial and
2705 final, or
2706 - the intermediate type and innermost type differ in signedness,
2707 and the outermost type is wider than the intermediate, or
2708 - the initial type is a pointer type and the precisions of the
2709 intermediate and final types differ, or
2710 - the final type is a pointer type and the precisions of the
2711 initial and intermediate types differ. */
2712 (if (! inside_float && ! inter_float && ! final_float
2713 && ! inside_vec && ! inter_vec && ! final_vec
2714 && (inter_prec >= inside_prec || inter_prec >= final_prec)
2715 && ! (inside_int && inter_int
2716 && inter_unsignedp != inside_unsignedp
2717 && inter_prec < final_prec)
2718 && ((inter_unsignedp && inter_prec > inside_prec)
2719 == (final_unsignedp && final_prec > inter_prec))
2720 && ! (inside_ptr && inter_prec != final_prec)
2721 && ! (final_ptr && inside_prec != inter_prec))
2722 (ocvt @0))
2723
2724 /* A truncation to an unsigned type (a zero-extension) should be
2725 canonicalized as bitwise and of a mask. */
2726 (if (GIMPLE /* PR70366: doing this in GENERIC breaks -Wconversion. */
2727 && final_int && inter_int && inside_int
2728 && final_prec == inside_prec
2729 && final_prec > inter_prec
2730 && inter_unsignedp)
2731 (convert (bit_and @0 { wide_int_to_tree
2732 (inside_type,
2733 wi::mask (inter_prec, false,
2734 TYPE_PRECISION (inside_type))); })))
2735
2736 /* If we are converting an integer to a floating-point that can
2737 represent it exactly and back to an integer, we can skip the
2738 floating-point conversion. */
2739 (if (GIMPLE /* PR66211 */
2740 && inside_int && inter_float && final_int &&
2741 (unsigned) significand_size (TYPE_MODE (inter_type))
2742 >= inside_prec - !inside_unsignedp)
2743 (convert @0)))))))
2744
2745 /* If we have a narrowing conversion to an integral type that is fed by a
2746 BIT_AND_EXPR, we might be able to remove the BIT_AND_EXPR if it merely
2747 masks off bits outside the final type (and nothing else). */
2748 (simplify
2749 (convert (bit_and @0 INTEGER_CST@1))
2750 (if (INTEGRAL_TYPE_P (type)
2751 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
2752 && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))
2753 && operand_equal_p (@1, build_low_bits_mask (TREE_TYPE (@1),
2754 TYPE_PRECISION (type)), 0))
2755 (convert @0)))
2756
2757
2758 /* (X /[ex] A) * A -> X. */
2759 (simplify
2760 (mult (convert1? (exact_div @0 @@1)) (convert2? @1))
2761 (convert @0))
2762
2763 /* Simplify (A / B) * B + (A % B) -> A. */
2764 (for div (trunc_div ceil_div floor_div round_div)
2765 mod (trunc_mod ceil_mod floor_mod round_mod)
2766 (simplify
2767 (plus:c (mult:c (div @0 @1) @1) (mod @0 @1))
2768 @0))
2769
2770 /* ((X /[ex] A) +- B) * A --> X +- A * B. */
2771 (for op (plus minus)
2772 (simplify
2773 (mult (convert1? (op (convert2? (exact_div @0 INTEGER_CST@@1)) INTEGER_CST@2)) @1)
2774 (if (tree_nop_conversion_p (type, TREE_TYPE (@2))
2775 && tree_nop_conversion_p (TREE_TYPE (@0), TREE_TYPE (@2)))
2776 (with
2777 {
2778 wi::overflow_type overflow;
2779 wide_int mul = wi::mul (wi::to_wide (@1), wi::to_wide (@2),
2780 TYPE_SIGN (type), &overflow);
2781 }
2782 (if (types_match (type, TREE_TYPE (@2))
2783 && types_match (TREE_TYPE (@0), TREE_TYPE (@2)) && !overflow)
2784 (op @0 { wide_int_to_tree (type, mul); })
2785 (with { tree utype = unsigned_type_for (type); }
2786 (convert (op (convert:utype @0)
2787 (mult (convert:utype @1) (convert:utype @2))))))))))
2788
2789 /* Canonicalization of binary operations. */
2790
2791 /* Convert X + -C into X - C. */
2792 (simplify
2793 (plus @0 REAL_CST@1)
2794 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1)))
2795 (with { tree tem = const_unop (NEGATE_EXPR, type, @1); }
2796 (if (!TREE_OVERFLOW (tem) || !flag_trapping_math)
2797 (minus @0 { tem; })))))
2798
2799 /* Convert x+x into x*2. */
2800 (simplify
2801 (plus @0 @0)
2802 (if (SCALAR_FLOAT_TYPE_P (type))
2803 (mult @0 { build_real (type, dconst2); })
2804 (if (INTEGRAL_TYPE_P (type))
2805 (mult @0 { build_int_cst (type, 2); }))))
2806
2807 /* 0 - X -> -X. */
2808 (simplify
2809 (minus integer_zerop @1)
2810 (negate @1))
2811 (simplify
2812 (pointer_diff integer_zerop @1)
2813 (negate (convert @1)))
2814
2815 /* (ARG0 - ARG1) is the same as (-ARG1 + ARG0). So check whether
2816 ARG0 is zero and X + ARG0 reduces to X, since that would mean
2817 (-ARG1 + ARG0) reduces to -ARG1. */
2818 (simplify
2819 (minus real_zerop@0 @1)
2820 (if (fold_real_zero_addition_p (type, @0, 0))
2821 (negate @1)))
2822
2823 /* Transform x * -1 into -x. */
2824 (simplify
2825 (mult @0 integer_minus_onep)
2826 (negate @0))
2827
2828 /* Reassociate (X * CST) * Y to (X * Y) * CST. This does not introduce
2829 signed overflow for CST != 0 && CST != -1. */
2830 (simplify
2831 (mult:c (mult:s@3 @0 INTEGER_CST@1) @2)
2832 (if (TREE_CODE (@2) != INTEGER_CST
2833 && single_use (@3)
2834 && !integer_zerop (@1) && !integer_minus_onep (@1))
2835 (mult (mult @0 @2) @1)))
2836
2837 /* True if we can easily extract the real and imaginary parts of a complex
2838 number. */
2839 (match compositional_complex
2840 (convert? (complex @0 @1)))
2841
2842 /* COMPLEX_EXPR and REALPART/IMAGPART_EXPR cancellations. */
2843 (simplify
2844 (complex (realpart @0) (imagpart @0))
2845 @0)
2846 (simplify
2847 (realpart (complex @0 @1))
2848 @0)
2849 (simplify
2850 (imagpart (complex @0 @1))
2851 @1)
2852
2853 /* Sometimes we only care about half of a complex expression. */
2854 (simplify
2855 (realpart (convert?:s (conj:s @0)))
2856 (convert (realpart @0)))
2857 (simplify
2858 (imagpart (convert?:s (conj:s @0)))
2859 (convert (negate (imagpart @0))))
2860 (for part (realpart imagpart)
2861 (for op (plus minus)
2862 (simplify
2863 (part (convert?:s@2 (op:s @0 @1)))
2864 (convert (op (part @0) (part @1))))))
2865 (simplify
2866 (realpart (convert?:s (CEXPI:s @0)))
2867 (convert (COS @0)))
2868 (simplify
2869 (imagpart (convert?:s (CEXPI:s @0)))
2870 (convert (SIN @0)))
2871
2872 /* conj(conj(x)) -> x */
2873 (simplify
2874 (conj (convert? (conj @0)))
2875 (if (tree_nop_conversion_p (TREE_TYPE (@0), type))
2876 (convert @0)))
2877
2878 /* conj({x,y}) -> {x,-y} */
2879 (simplify
2880 (conj (convert?:s (complex:s @0 @1)))
2881 (with { tree itype = TREE_TYPE (type); }
2882 (complex (convert:itype @0) (negate (convert:itype @1)))))
2883
2884 /* BSWAP simplifications, transforms checked by gcc.dg/builtin-bswap-8.c. */
2885 (for bswap (BUILT_IN_BSWAP16 BUILT_IN_BSWAP32 BUILT_IN_BSWAP64)
2886 (simplify
2887 (bswap (bswap @0))
2888 @0)
2889 (simplify
2890 (bswap (bit_not (bswap @0)))
2891 (bit_not @0))
2892 (for bitop (bit_xor bit_ior bit_and)
2893 (simplify
2894 (bswap (bitop:c (bswap @0) @1))
2895 (bitop @0 (bswap @1)))))
2896
2897
2898 /* Combine COND_EXPRs and VEC_COND_EXPRs. */
2899
2900 /* Simplify constant conditions.
2901 Only optimize constant conditions when the selected branch
2902 has the same type as the COND_EXPR. This avoids optimizing
2903 away "c ? x : throw", where the throw has a void type.
2904 Note that we cannot throw away the fold-const.c variant nor
2905 this one as we depend on doing this transform before possibly
2906 A ? B : B -> B triggers and the fold-const.c one can optimize
2907 0 ? A : B to B even if A has side-effects. Something
2908 genmatch cannot handle. */
2909 (simplify
2910 (cond INTEGER_CST@0 @1 @2)
2911 (if (integer_zerop (@0))
2912 (if (!VOID_TYPE_P (TREE_TYPE (@2)) || VOID_TYPE_P (type))
2913 @2)
2914 (if (!VOID_TYPE_P (TREE_TYPE (@1)) || VOID_TYPE_P (type))
2915 @1)))
2916 (simplify
2917 (vec_cond VECTOR_CST@0 @1 @2)
2918 (if (integer_all_onesp (@0))
2919 @1
2920 (if (integer_zerop (@0))
2921 @2)))
2922
2923 /* Sink unary operations to constant branches, but only if we do fold it to
2924 constants. */
2925 (for op (negate bit_not abs absu)
2926 (simplify
2927 (op (vec_cond @0 VECTOR_CST@1 VECTOR_CST@2))
2928 (with
2929 {
2930 tree cst1, cst2;
2931 cst1 = const_unop (op, type, @1);
2932 if (cst1)
2933 cst2 = const_unop (op, type, @2);
2934 }
2935 (if (cst1 && cst2)
2936 (vec_cond @0 { cst1; } { cst2; })))))
2937
2938 /* Simplification moved from fold_cond_expr_with_comparison. It may also
2939 be extended. */
2940 /* This pattern implements two kinds simplification:
2941
2942 Case 1)
2943 (cond (cmp (convert1? x) c1) (convert2? x) c2) -> (minmax (x c)) if:
2944 1) Conversions are type widening from smaller type.
2945 2) Const c1 equals to c2 after canonicalizing comparison.
2946 3) Comparison has tree code LT, LE, GT or GE.
2947 This specific pattern is needed when (cmp (convert x) c) may not
2948 be simplified by comparison patterns because of multiple uses of
2949 x. It also makes sense here because simplifying across multiple
2950 referred var is always benefitial for complicated cases.
2951
2952 Case 2)
2953 (cond (eq (convert1? x) c1) (convert2? x) c2) -> (cond (eq x c1) c1 c2). */
2954 (for cmp (lt le gt ge eq)
2955 (simplify
2956 (cond (cmp (convert1? @1) INTEGER_CST@3) (convert2? @1) INTEGER_CST@2)
2957 (with
2958 {
2959 tree from_type = TREE_TYPE (@1);
2960 tree c1_type = TREE_TYPE (@3), c2_type = TREE_TYPE (@2);
2961 enum tree_code code = ERROR_MARK;
2962
2963 if (INTEGRAL_TYPE_P (from_type)
2964 && int_fits_type_p (@2, from_type)
2965 && (types_match (c1_type, from_type)
2966 || (TYPE_PRECISION (c1_type) > TYPE_PRECISION (from_type)
2967 && (TYPE_UNSIGNED (from_type)
2968 || TYPE_SIGN (c1_type) == TYPE_SIGN (from_type))))
2969 && (types_match (c2_type, from_type)
2970 || (TYPE_PRECISION (c2_type) > TYPE_PRECISION (from_type)
2971 && (TYPE_UNSIGNED (from_type)
2972 || TYPE_SIGN (c2_type) == TYPE_SIGN (from_type)))))
2973 {
2974 if (cmp != EQ_EXPR)
2975 {
2976 if (wi::to_widest (@3) == (wi::to_widest (@2) - 1))
2977 {
2978 /* X <= Y - 1 equals to X < Y. */
2979 if (cmp == LE_EXPR)
2980 code = LT_EXPR;
2981 /* X > Y - 1 equals to X >= Y. */
2982 if (cmp == GT_EXPR)
2983 code = GE_EXPR;
2984 }
2985 if (wi::to_widest (@3) == (wi::to_widest (@2) + 1))
2986 {
2987 /* X < Y + 1 equals to X <= Y. */
2988 if (cmp == LT_EXPR)
2989 code = LE_EXPR;
2990 /* X >= Y + 1 equals to X > Y. */
2991 if (cmp == GE_EXPR)
2992 code = GT_EXPR;
2993 }
2994 if (code != ERROR_MARK
2995 || wi::to_widest (@2) == wi::to_widest (@3))
2996 {
2997 if (cmp == LT_EXPR || cmp == LE_EXPR)
2998 code = MIN_EXPR;
2999 if (cmp == GT_EXPR || cmp == GE_EXPR)
3000 code = MAX_EXPR;
3001 }
3002 }
3003 /* Can do A == C1 ? A : C2 -> A == C1 ? C1 : C2? */
3004 else if (int_fits_type_p (@3, from_type))
3005 code = EQ_EXPR;
3006 }
3007 }
3008 (if (code == MAX_EXPR)
3009 (convert (max @1 (convert @2)))
3010 (if (code == MIN_EXPR)
3011 (convert (min @1 (convert @2)))
3012 (if (code == EQ_EXPR)
3013 (convert (cond (eq @1 (convert @3))
3014 (convert:from_type @3) (convert:from_type @2)))))))))
3015
3016 /* (cond (cmp (convert? x) c1) (op x c2) c3) -> (op (minmax x c1) c2) if:
3017
3018 1) OP is PLUS or MINUS.
3019 2) CMP is LT, LE, GT or GE.
3020 3) C3 == (C1 op C2), and computation doesn't have undefined behavior.
3021
3022 This pattern also handles special cases like:
3023
3024 A) Operand x is a unsigned to signed type conversion and c1 is
3025 integer zero. In this case,
3026 (signed type)x < 0 <=> x > MAX_VAL(signed type)
3027 (signed type)x >= 0 <=> x <= MAX_VAL(signed type)
3028 B) Const c1 may not equal to (C3 op' C2). In this case we also
3029 check equality for (c1+1) and (c1-1) by adjusting comparison
3030 code.
3031
3032 TODO: Though signed type is handled by this pattern, it cannot be
3033 simplified at the moment because C standard requires additional
3034 type promotion. In order to match&simplify it here, the IR needs
3035 to be cleaned up by other optimizers, i.e, VRP. */
3036 (for op (plus minus)
3037 (for cmp (lt le gt ge)
3038 (simplify
3039 (cond (cmp (convert? @X) INTEGER_CST@1) (op @X INTEGER_CST@2) INTEGER_CST@3)
3040 (with { tree from_type = TREE_TYPE (@X), to_type = TREE_TYPE (@1); }
3041 (if (types_match (from_type, to_type)
3042 /* Check if it is special case A). */
3043 || (TYPE_UNSIGNED (from_type)
3044 && !TYPE_UNSIGNED (to_type)
3045 && TYPE_PRECISION (from_type) == TYPE_PRECISION (to_type)
3046 && integer_zerop (@1)
3047 && (cmp == LT_EXPR || cmp == GE_EXPR)))
3048 (with
3049 {
3050 wi::overflow_type overflow = wi::OVF_NONE;
3051 enum tree_code code, cmp_code = cmp;
3052 wide_int real_c1;
3053 wide_int c1 = wi::to_wide (@1);
3054 wide_int c2 = wi::to_wide (@2);
3055 wide_int c3 = wi::to_wide (@3);
3056 signop sgn = TYPE_SIGN (from_type);
3057
3058 /* Handle special case A), given x of unsigned type:
3059 ((signed type)x < 0) <=> (x > MAX_VAL(signed type))
3060 ((signed type)x >= 0) <=> (x <= MAX_VAL(signed type)) */
3061 if (!types_match (from_type, to_type))
3062 {
3063 if (cmp_code == LT_EXPR)
3064 cmp_code = GT_EXPR;
3065 if (cmp_code == GE_EXPR)
3066 cmp_code = LE_EXPR;
3067 c1 = wi::max_value (to_type);
3068 }
3069 /* To simplify this pattern, we require c3 = (c1 op c2). Here we
3070 compute (c3 op' c2) and check if it equals to c1 with op' being
3071 the inverted operator of op. Make sure overflow doesn't happen
3072 if it is undefined. */
3073 if (op == PLUS_EXPR)
3074 real_c1 = wi::sub (c3, c2, sgn, &overflow);
3075 else
3076 real_c1 = wi::add (c3, c2, sgn, &overflow);
3077
3078 code = cmp_code;
3079 if (!overflow || !TYPE_OVERFLOW_UNDEFINED (from_type))
3080 {
3081 /* Check if c1 equals to real_c1. Boundary condition is handled
3082 by adjusting comparison operation if necessary. */
3083 if (!wi::cmp (wi::sub (real_c1, 1, sgn, &overflow), c1, sgn)
3084 && !overflow)
3085 {
3086 /* X <= Y - 1 equals to X < Y. */
3087 if (cmp_code == LE_EXPR)
3088 code = LT_EXPR;
3089 /* X > Y - 1 equals to X >= Y. */
3090 if (cmp_code == GT_EXPR)
3091 code = GE_EXPR;
3092 }
3093 if (!wi::cmp (wi::add (real_c1, 1, sgn, &overflow), c1, sgn)
3094 && !overflow)
3095 {
3096 /* X < Y + 1 equals to X <= Y. */
3097 if (cmp_code == LT_EXPR)
3098 code = LE_EXPR;
3099 /* X >= Y + 1 equals to X > Y. */
3100 if (cmp_code == GE_EXPR)
3101 code = GT_EXPR;
3102 }
3103 if (code != cmp_code || !wi::cmp (real_c1, c1, sgn))
3104 {
3105 if (cmp_code == LT_EXPR || cmp_code == LE_EXPR)
3106 code = MIN_EXPR;
3107 if (cmp_code == GT_EXPR || cmp_code == GE_EXPR)
3108 code = MAX_EXPR;
3109 }
3110 }
3111 }
3112 (if (code == MAX_EXPR)
3113 (op (max @X { wide_int_to_tree (from_type, real_c1); })
3114 { wide_int_to_tree (from_type, c2); })
3115 (if (code == MIN_EXPR)
3116 (op (min @X { wide_int_to_tree (from_type, real_c1); })
3117 { wide_int_to_tree (from_type, c2); })))))))))
3118
3119 (for cnd (cond vec_cond)
3120 /* A ? B : (A ? X : C) -> A ? B : C. */
3121 (simplify
3122 (cnd @0 (cnd @0 @1 @2) @3)
3123 (cnd @0 @1 @3))
3124 (simplify
3125 (cnd @0 @1 (cnd @0 @2 @3))
3126 (cnd @0 @1 @3))
3127 /* A ? B : (!A ? C : X) -> A ? B : C. */
3128 /* ??? This matches embedded conditions open-coded because genmatch
3129 would generate matching code for conditions in separate stmts only.
3130 The following is still important to merge then and else arm cases
3131 from if-conversion. */
3132 (simplify
3133 (cnd @0 @1 (cnd @2 @3 @4))
3134 (if (inverse_conditions_p (@0, @2))
3135 (cnd @0 @1 @3)))
3136 (simplify
3137 (cnd @0 (cnd @1 @2 @3) @4)
3138 (if (inverse_conditions_p (@0, @1))
3139 (cnd @0 @3 @4)))
3140
3141 /* A ? B : B -> B. */
3142 (simplify
3143 (cnd @0 @1 @1)
3144 @1)
3145
3146 /* !A ? B : C -> A ? C : B. */
3147 (simplify
3148 (cnd (logical_inverted_value truth_valued_p@0) @1 @2)
3149 (cnd @0 @2 @1)))
3150
3151 /* A + (B vcmp C ? 1 : 0) -> A - (B vcmp C ? -1 : 0), since vector comparisons
3152 return all -1 or all 0 results. */
3153 /* ??? We could instead convert all instances of the vec_cond to negate,
3154 but that isn't necessarily a win on its own. */
3155 (simplify
3156 (plus:c @3 (view_convert? (vec_cond:s @0 integer_each_onep@1 integer_zerop@2)))
3157 (if (VECTOR_TYPE_P (type)
3158 && known_eq (TYPE_VECTOR_SUBPARTS (type),
3159 TYPE_VECTOR_SUBPARTS (TREE_TYPE (@1)))
3160 && (TYPE_MODE (TREE_TYPE (type))
3161 == TYPE_MODE (TREE_TYPE (TREE_TYPE (@1)))))
3162 (minus @3 (view_convert (vec_cond @0 (negate @1) @2)))))
3163
3164 /* ... likewise A - (B vcmp C ? 1 : 0) -> A + (B vcmp C ? -1 : 0). */
3165 (simplify
3166 (minus @3 (view_convert? (vec_cond:s @0 integer_each_onep@1 integer_zerop@2)))
3167 (if (VECTOR_TYPE_P (type)
3168 && known_eq (TYPE_VECTOR_SUBPARTS (type),
3169 TYPE_VECTOR_SUBPARTS (TREE_TYPE (@1)))
3170 && (TYPE_MODE (TREE_TYPE (type))
3171 == TYPE_MODE (TREE_TYPE (TREE_TYPE (@1)))))
3172 (plus @3 (view_convert (vec_cond @0 (negate @1) @2)))))
3173
3174
3175 /* Simplifications of comparisons. */
3176
3177 /* See if we can reduce the magnitude of a constant involved in a
3178 comparison by changing the comparison code. This is a canonicalization
3179 formerly done by maybe_canonicalize_comparison_1. */
3180 (for cmp (le gt)
3181 acmp (lt ge)
3182 (simplify
3183 (cmp @0 uniform_integer_cst_p@1)
3184 (with { tree cst = uniform_integer_cst_p (@1); }
3185 (if (tree_int_cst_sgn (cst) == -1)
3186 (acmp @0 { build_uniform_cst (TREE_TYPE (@1),
3187 wide_int_to_tree (TREE_TYPE (cst),
3188 wi::to_wide (cst)
3189 + 1)); })))))
3190 (for cmp (ge lt)
3191 acmp (gt le)
3192 (simplify
3193 (cmp @0 uniform_integer_cst_p@1)
3194 (with { tree cst = uniform_integer_cst_p (@1); }
3195 (if (tree_int_cst_sgn (cst) == 1)
3196 (acmp @0 { build_uniform_cst (TREE_TYPE (@1),
3197 wide_int_to_tree (TREE_TYPE (cst),
3198 wi::to_wide (cst) - 1)); })))))
3199
3200 /* We can simplify a logical negation of a comparison to the
3201 inverted comparison. As we cannot compute an expression
3202 operator using invert_tree_comparison we have to simulate
3203 that with expression code iteration. */
3204 (for cmp (tcc_comparison)
3205 icmp (inverted_tcc_comparison)
3206 ncmp (inverted_tcc_comparison_with_nans)
3207 /* Ideally we'd like to combine the following two patterns
3208 and handle some more cases by using
3209 (logical_inverted_value (cmp @0 @1))
3210 here but for that genmatch would need to "inline" that.
3211 For now implement what forward_propagate_comparison did. */
3212 (simplify
3213 (bit_not (cmp @0 @1))
3214 (if (VECTOR_TYPE_P (type)
3215 || (INTEGRAL_TYPE_P (type) && TYPE_PRECISION (type) == 1))
3216 /* Comparison inversion may be impossible for trapping math,
3217 invert_tree_comparison will tell us. But we can't use
3218 a computed operator in the replacement tree thus we have
3219 to play the trick below. */
3220 (with { enum tree_code ic = invert_tree_comparison
3221 (cmp, HONOR_NANS (@0)); }
3222 (if (ic == icmp)
3223 (icmp @0 @1)
3224 (if (ic == ncmp)
3225 (ncmp @0 @1))))))
3226 (simplify
3227 (bit_xor (cmp @0 @1) integer_truep)
3228 (with { enum tree_code ic = invert_tree_comparison
3229 (cmp, HONOR_NANS (@0)); }
3230 (if (ic == icmp)
3231 (icmp @0 @1)
3232 (if (ic == ncmp)
3233 (ncmp @0 @1))))))
3234
3235 /* Transform comparisons of the form X - Y CMP 0 to X CMP Y.
3236 ??? The transformation is valid for the other operators if overflow
3237 is undefined for the type, but performing it here badly interacts
3238 with the transformation in fold_cond_expr_with_comparison which
3239 attempts to synthetize ABS_EXPR. */
3240 (for cmp (eq ne)
3241 (for sub (minus pointer_diff)
3242 (simplify
3243 (cmp (sub@2 @0 @1) integer_zerop)
3244 (if (single_use (@2))
3245 (cmp @0 @1)))))
3246
3247 /* Transform comparisons of the form X * C1 CMP 0 to X CMP 0 in the
3248 signed arithmetic case. That form is created by the compiler
3249 often enough for folding it to be of value. One example is in
3250 computing loop trip counts after Operator Strength Reduction. */
3251 (for cmp (simple_comparison)
3252 scmp (swapped_simple_comparison)
3253 (simplify
3254 (cmp (mult@3 @0 INTEGER_CST@1) integer_zerop@2)
3255 /* Handle unfolded multiplication by zero. */
3256 (if (integer_zerop (@1))
3257 (cmp @1 @2)
3258 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
3259 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
3260 && single_use (@3))
3261 /* If @1 is negative we swap the sense of the comparison. */
3262 (if (tree_int_cst_sgn (@1) < 0)
3263 (scmp @0 @2)
3264 (cmp @0 @2))))))
3265
3266 /* Simplify comparison of something with itself. For IEEE
3267 floating-point, we can only do some of these simplifications. */
3268 (for cmp (eq ge le)
3269 (simplify
3270 (cmp @0 @0)
3271 (if (! FLOAT_TYPE_P (TREE_TYPE (@0))
3272 || ! HONOR_NANS (@0))
3273 { constant_boolean_node (true, type); }
3274 (if (cmp != EQ_EXPR)
3275 (eq @0 @0)))))
3276 (for cmp (ne gt lt)
3277 (simplify
3278 (cmp @0 @0)
3279 (if (cmp != NE_EXPR
3280 || ! FLOAT_TYPE_P (TREE_TYPE (@0))
3281 || ! HONOR_NANS (@0))
3282 { constant_boolean_node (false, type); })))
3283 (for cmp (unle unge uneq)
3284 (simplify
3285 (cmp @0 @0)
3286 { constant_boolean_node (true, type); }))
3287 (for cmp (unlt ungt)
3288 (simplify
3289 (cmp @0 @0)
3290 (unordered @0 @0)))
3291 (simplify
3292 (ltgt @0 @0)
3293 (if (!flag_trapping_math)
3294 { constant_boolean_node (false, type); }))
3295
3296 /* Fold ~X op ~Y as Y op X. */
3297 (for cmp (simple_comparison)
3298 (simplify
3299 (cmp (bit_not@2 @0) (bit_not@3 @1))
3300 (if (single_use (@2) && single_use (@3))
3301 (cmp @1 @0))))
3302
3303 /* Fold ~X op C as X op' ~C, where op' is the swapped comparison. */
3304 (for cmp (simple_comparison)
3305 scmp (swapped_simple_comparison)
3306 (simplify
3307 (cmp (bit_not@2 @0) CONSTANT_CLASS_P@1)
3308 (if (single_use (@2)
3309 && (TREE_CODE (@1) == INTEGER_CST || TREE_CODE (@1) == VECTOR_CST))
3310 (scmp @0 (bit_not @1)))))
3311
3312 (for cmp (simple_comparison)
3313 /* Fold (double)float1 CMP (double)float2 into float1 CMP float2. */
3314 (simplify
3315 (cmp (convert@2 @0) (convert? @1))
3316 (if (FLOAT_TYPE_P (TREE_TYPE (@0))
3317 && (DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@2))
3318 == DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@0)))
3319 && (DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@2))
3320 == DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@1))))
3321 (with
3322 {
3323 tree type1 = TREE_TYPE (@1);
3324 if (TREE_CODE (@1) == REAL_CST && !DECIMAL_FLOAT_TYPE_P (type1))
3325 {
3326 REAL_VALUE_TYPE orig = TREE_REAL_CST (@1);
3327 if (TYPE_PRECISION (type1) > TYPE_PRECISION (float_type_node)
3328 && exact_real_truncate (TYPE_MODE (float_type_node), &orig))
3329 type1 = float_type_node;
3330 if (TYPE_PRECISION (type1) > TYPE_PRECISION (double_type_node)
3331 && exact_real_truncate (TYPE_MODE (double_type_node), &orig))
3332 type1 = double_type_node;
3333 }
3334 tree newtype
3335 = (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (type1)
3336 ? TREE_TYPE (@0) : type1);
3337 }
3338 (if (TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (newtype))
3339 (cmp (convert:newtype @0) (convert:newtype @1))))))
3340
3341 (simplify
3342 (cmp @0 REAL_CST@1)
3343 /* IEEE doesn't distinguish +0 and -0 in comparisons. */
3344 (switch
3345 /* a CMP (-0) -> a CMP 0 */
3346 (if (REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (@1)))
3347 (cmp @0 { build_real (TREE_TYPE (@1), dconst0); }))
3348 /* x != NaN is always true, other ops are always false. */
3349 (if (REAL_VALUE_ISNAN (TREE_REAL_CST (@1))
3350 && ! HONOR_SNANS (@1))
3351 { constant_boolean_node (cmp == NE_EXPR, type); })
3352 /* Fold comparisons against infinity. */
3353 (if (REAL_VALUE_ISINF (TREE_REAL_CST (@1))
3354 && MODE_HAS_INFINITIES (TYPE_MODE (TREE_TYPE (@1))))
3355 (with
3356 {
3357 REAL_VALUE_TYPE max;
3358 enum tree_code code = cmp;
3359 bool neg = REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1));
3360 if (neg)
3361 code = swap_tree_comparison (code);
3362 }
3363 (switch
3364 /* x > +Inf is always false, if we ignore NaNs or exceptions. */
3365 (if (code == GT_EXPR
3366 && !(HONOR_NANS (@0) && flag_trapping_math))
3367 { constant_boolean_node (false, type); })
3368 (if (code == LE_EXPR)
3369 /* x <= +Inf is always true, if we don't care about NaNs. */
3370 (if (! HONOR_NANS (@0))
3371 { constant_boolean_node (true, type); }
3372 /* x <= +Inf is the same as x == x, i.e. !isnan(x), but this loses
3373 an "invalid" exception. */
3374 (if (!flag_trapping_math)
3375 (eq @0 @0))))
3376 /* x == +Inf and x >= +Inf are always equal to x > DBL_MAX, but
3377 for == this introduces an exception for x a NaN. */
3378 (if ((code == EQ_EXPR && !(HONOR_NANS (@0) && flag_trapping_math))
3379 || code == GE_EXPR)
3380 (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
3381 (if (neg)
3382 (lt @0 { build_real (TREE_TYPE (@0), max); })
3383 (gt @0 { build_real (TREE_TYPE (@0), max); }))))
3384 /* x < +Inf is always equal to x <= DBL_MAX. */
3385 (if (code == LT_EXPR)
3386 (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
3387 (if (neg)
3388 (ge @0 { build_real (TREE_TYPE (@0), max); })
3389 (le @0 { build_real (TREE_TYPE (@0), max); }))))
3390 /* x != +Inf is always equal to !(x > DBL_MAX), but this introduces
3391 an exception for x a NaN so use an unordered comparison. */
3392 (if (code == NE_EXPR)
3393 (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
3394 (if (! HONOR_NANS (@0))
3395 (if (neg)
3396 (ge @0 { build_real (TREE_TYPE (@0), max); })
3397 (le @0 { build_real (TREE_TYPE (@0), max); }))
3398 (if (neg)
3399 (unge @0 { build_real (TREE_TYPE (@0), max); })
3400 (unle @0 { build_real (TREE_TYPE (@0), max); }))))))))))
3401
3402 /* If this is a comparison of a real constant with a PLUS_EXPR
3403 or a MINUS_EXPR of a real constant, we can convert it into a
3404 comparison with a revised real constant as long as no overflow
3405 occurs when unsafe_math_optimizations are enabled. */
3406 (if (flag_unsafe_math_optimizations)
3407 (for op (plus minus)
3408 (simplify
3409 (cmp (op @0 REAL_CST@1) REAL_CST@2)
3410 (with
3411 {
3412 tree tem = const_binop (op == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR,
3413 TREE_TYPE (@1), @2, @1);
3414 }
3415 (if (tem && !TREE_OVERFLOW (tem))
3416 (cmp @0 { tem; }))))))
3417
3418 /* Likewise, we can simplify a comparison of a real constant with
3419 a MINUS_EXPR whose first operand is also a real constant, i.e.
3420 (c1 - x) < c2 becomes x > c1-c2. Reordering is allowed on
3421 floating-point types only if -fassociative-math is set. */
3422 (if (flag_associative_math)
3423 (simplify
3424 (cmp (minus REAL_CST@0 @1) REAL_CST@2)
3425 (with { tree tem = const_binop (MINUS_EXPR, TREE_TYPE (@1), @0, @2); }
3426 (if (tem && !TREE_OVERFLOW (tem))
3427 (cmp { tem; } @1)))))
3428
3429 /* Fold comparisons against built-in math functions. */
3430 (if (flag_unsafe_math_optimizations
3431 && ! flag_errno_math)
3432 (for sq (SQRT)
3433 (simplify
3434 (cmp (sq @0) REAL_CST@1)
3435 (switch
3436 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1)))
3437 (switch
3438 /* sqrt(x) < y is always false, if y is negative. */
3439 (if (cmp == EQ_EXPR || cmp == LT_EXPR || cmp == LE_EXPR)
3440 { constant_boolean_node (false, type); })
3441 /* sqrt(x) > y is always true, if y is negative and we
3442 don't care about NaNs, i.e. negative values of x. */
3443 (if (cmp == NE_EXPR || !HONOR_NANS (@0))
3444 { constant_boolean_node (true, type); })
3445 /* sqrt(x) > y is the same as x >= 0, if y is negative. */
3446 (ge @0 { build_real (TREE_TYPE (@0), dconst0); })))
3447 (if (real_equal (TREE_REAL_CST_PTR (@1), &dconst0))
3448 (switch
3449 /* sqrt(x) < 0 is always false. */
3450 (if (cmp == LT_EXPR)
3451 { constant_boolean_node (false, type); })
3452 /* sqrt(x) >= 0 is always true if we don't care about NaNs. */
3453 (if (cmp == GE_EXPR && !HONOR_NANS (@0))
3454 { constant_boolean_node (true, type); })
3455 /* sqrt(x) <= 0 -> x == 0. */
3456 (if (cmp == LE_EXPR)
3457 (eq @0 @1))
3458 /* Otherwise sqrt(x) cmp 0 -> x cmp 0. Here cmp can be >=, >,
3459 == or !=. In the last case:
3460
3461 (sqrt(x) != 0) == (NaN != 0) == true == (x != 0)
3462
3463 if x is negative or NaN. Due to -funsafe-math-optimizations,
3464 the results for other x follow from natural arithmetic. */
3465 (cmp @0 @1)))
3466 (if (cmp == GT_EXPR || cmp == GE_EXPR)
3467 (with
3468 {
3469 REAL_VALUE_TYPE c2;
3470 real_arithmetic (&c2, MULT_EXPR,
3471 &TREE_REAL_CST (@1), &TREE_REAL_CST (@1));
3472 real_convert (&c2, TYPE_MODE (TREE_TYPE (@0)), &c2);
3473 }
3474 (if (REAL_VALUE_ISINF (c2))
3475 /* sqrt(x) > y is x == +Inf, when y is very large. */
3476 (if (HONOR_INFINITIES (@0))
3477 (eq @0 { build_real (TREE_TYPE (@0), c2); })
3478 { constant_boolean_node (false, type); })
3479 /* sqrt(x) > c is the same as x > c*c. */
3480 (cmp @0 { build_real (TREE_TYPE (@0), c2); }))))
3481 (if (cmp == LT_EXPR || cmp == LE_EXPR)
3482 (with
3483 {
3484 REAL_VALUE_TYPE c2;
3485 real_arithmetic (&c2, MULT_EXPR,
3486 &TREE_REAL_CST (@1), &TREE_REAL_CST (@1));
3487 real_convert (&c2, TYPE_MODE (TREE_TYPE (@0)), &c2);
3488 }
3489 (if (REAL_VALUE_ISINF (c2))
3490 (switch
3491 /* sqrt(x) < y is always true, when y is a very large
3492 value and we don't care about NaNs or Infinities. */
3493 (if (! HONOR_NANS (@0) && ! HONOR_INFINITIES (@0))
3494 { constant_boolean_node (true, type); })
3495 /* sqrt(x) < y is x != +Inf when y is very large and we
3496 don't care about NaNs. */
3497 (if (! HONOR_NANS (@0))
3498 (ne @0 { build_real (TREE_TYPE (@0), c2); }))
3499 /* sqrt(x) < y is x >= 0 when y is very large and we
3500 don't care about Infinities. */
3501 (if (! HONOR_INFINITIES (@0))
3502 (ge @0 { build_real (TREE_TYPE (@0), dconst0); }))
3503 /* sqrt(x) < y is x >= 0 && x != +Inf, when y is large. */
3504 (if (GENERIC)
3505 (truth_andif
3506 (ge @0 { build_real (TREE_TYPE (@0), dconst0); })
3507 (ne @0 { build_real (TREE_TYPE (@0), c2); }))))
3508 /* sqrt(x) < c is the same as x < c*c, if we ignore NaNs. */
3509 (if (! HONOR_NANS (@0))
3510 (cmp @0 { build_real (TREE_TYPE (@0), c2); })
3511 /* sqrt(x) < c is the same as x >= 0 && x < c*c. */
3512 (if (GENERIC)
3513 (truth_andif
3514 (ge @0 { build_real (TREE_TYPE (@0), dconst0); })
3515 (cmp @0 { build_real (TREE_TYPE (@0), c2); })))))))))
3516 /* Transform sqrt(x) cmp sqrt(y) -> x cmp y. */
3517 (simplify
3518 (cmp (sq @0) (sq @1))
3519 (if (! HONOR_NANS (@0))
3520 (cmp @0 @1))))))
3521
3522 /* Optimize various special cases of (FTYPE) N CMP (FTYPE) M. */
3523 (for cmp (lt le eq ne ge gt unordered ordered unlt unle ungt unge uneq ltgt)
3524 icmp (lt le eq ne ge gt unordered ordered lt le gt ge eq ne)
3525 (simplify
3526 (cmp (float@0 @1) (float @2))
3527 (if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (@0))
3528 && ! DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@0)))
3529 (with
3530 {
3531 format_helper fmt (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (@0))));
3532 tree type1 = TREE_TYPE (@1);
3533 bool type1_signed_p = TYPE_SIGN (type1) == SIGNED;
3534 tree type2 = TREE_TYPE (@2);
3535 bool type2_signed_p = TYPE_SIGN (type2) == SIGNED;
3536 }
3537 (if (fmt.can_represent_integral_type_p (type1)
3538 && fmt.can_represent_integral_type_p (type2))
3539 (if (cmp == ORDERED_EXPR || cmp == UNORDERED_EXPR)
3540 { constant_boolean_node (cmp == ORDERED_EXPR, type); }
3541 (if (TYPE_PRECISION (type1) > TYPE_PRECISION (type2)
3542 && type1_signed_p >= type2_signed_p)
3543 (icmp @1 (convert @2))
3544 (if (TYPE_PRECISION (type1) < TYPE_PRECISION (type2)
3545 && type1_signed_p <= type2_signed_p)
3546 (icmp (convert:type2 @1) @2)
3547 (if (TYPE_PRECISION (type1) == TYPE_PRECISION (type2)
3548 && type1_signed_p == type2_signed_p)
3549 (icmp @1 @2))))))))))
3550
3551 /* Optimize various special cases of (FTYPE) N CMP CST. */
3552 (for cmp (lt le eq ne ge gt)
3553 icmp (le le eq ne ge ge)
3554 (simplify
3555 (cmp (float @0) REAL_CST@1)
3556 (if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (@1))
3557 && ! DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@1)))
3558 (with
3559 {
3560 tree itype = TREE_TYPE (@0);
3561 format_helper fmt (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (@1))));
3562 const REAL_VALUE_TYPE *cst = TREE_REAL_CST_PTR (@1);
3563 /* Be careful to preserve any potential exceptions due to
3564 NaNs. qNaNs are ok in == or != context.
3565 TODO: relax under -fno-trapping-math or
3566 -fno-signaling-nans. */
3567 bool exception_p
3568 = real_isnan (cst) && (cst->signalling
3569 || (cmp != EQ_EXPR && cmp != NE_EXPR));
3570 }
3571 /* TODO: allow non-fitting itype and SNaNs when
3572 -fno-trapping-math. */
3573 (if (fmt.can_represent_integral_type_p (itype) && ! exception_p)
3574 (with
3575 {
3576 signop isign = TYPE_SIGN (itype);
3577 REAL_VALUE_TYPE imin, imax;
3578 real_from_integer (&imin, fmt, wi::min_value (itype), isign);
3579 real_from_integer (&imax, fmt, wi::max_value (itype), isign);
3580
3581 REAL_VALUE_TYPE icst;
3582 if (cmp == GT_EXPR || cmp == GE_EXPR)
3583 real_ceil (&icst, fmt, cst);
3584 else if (cmp == LT_EXPR || cmp == LE_EXPR)
3585 real_floor (&icst, fmt, cst);
3586 else
3587 real_trunc (&icst, fmt, cst);
3588
3589 bool cst_int_p = !real_isnan (cst) && real_identical (&icst, cst);
3590
3591 bool overflow_p = false;
3592 wide_int icst_val
3593 = real_to_integer (&icst, &overflow_p, TYPE_PRECISION (itype));
3594 }
3595 (switch
3596 /* Optimize cases when CST is outside of ITYPE's range. */
3597 (if (real_compare (LT_EXPR, cst, &imin))
3598 { constant_boolean_node (cmp == GT_EXPR || cmp == GE_EXPR || cmp == NE_EXPR,
3599 type); })
3600 (if (real_compare (GT_EXPR, cst, &imax))
3601 { constant_boolean_node (cmp == LT_EXPR || cmp == LE_EXPR || cmp == NE_EXPR,
3602 type); })
3603 /* Remove cast if CST is an integer representable by ITYPE. */
3604 (if (cst_int_p)
3605 (cmp @0 { gcc_assert (!overflow_p);
3606 wide_int_to_tree (itype, icst_val); })
3607 )
3608 /* When CST is fractional, optimize
3609 (FTYPE) N == CST -> 0
3610 (FTYPE) N != CST -> 1. */
3611 (if (cmp == EQ_EXPR || cmp == NE_EXPR)
3612 { constant_boolean_node (cmp == NE_EXPR, type); })
3613 /* Otherwise replace with sensible integer constant. */
3614 (with
3615 {
3616 gcc_checking_assert (!overflow_p);
3617 }
3618 (icmp @0 { wide_int_to_tree (itype, icst_val); })))))))))
3619
3620 /* Fold A /[ex] B CMP C to A CMP B * C. */
3621 (for cmp (eq ne)
3622 (simplify
3623 (cmp (exact_div @0 @1) INTEGER_CST@2)
3624 (if (!integer_zerop (@1))
3625 (if (wi::to_wide (@2) == 0)
3626 (cmp @0 @2)
3627 (if (TREE_CODE (@1) == INTEGER_CST)
3628 (with
3629 {
3630 wi::overflow_type ovf;
3631 wide_int prod = wi::mul (wi::to_wide (@2), wi::to_wide (@1),
3632 TYPE_SIGN (TREE_TYPE (@1)), &ovf);
3633 }
3634 (if (ovf)
3635 { constant_boolean_node (cmp == NE_EXPR, type); }
3636 (cmp @0 { wide_int_to_tree (TREE_TYPE (@0), prod); }))))))))
3637 (for cmp (lt le gt ge)
3638 (simplify
3639 (cmp (exact_div @0 INTEGER_CST@1) INTEGER_CST@2)
3640 (if (wi::gt_p (wi::to_wide (@1), 0, TYPE_SIGN (TREE_TYPE (@1))))
3641 (with
3642 {
3643 wi::overflow_type ovf;
3644 wide_int prod = wi::mul (wi::to_wide (@2), wi::to_wide (@1),
3645 TYPE_SIGN (TREE_TYPE (@1)), &ovf);
3646 }
3647 (if (ovf)
3648 { constant_boolean_node (wi::lt_p (wi::to_wide (@2), 0,
3649 TYPE_SIGN (TREE_TYPE (@2)))
3650 != (cmp == LT_EXPR || cmp == LE_EXPR), type); }
3651 (cmp @0 { wide_int_to_tree (TREE_TYPE (@0), prod); }))))))
3652
3653 /* Fold (size_t)(A /[ex] B) CMP C to (size_t)A CMP (size_t)B * C or A CMP' 0.
3654
3655 For small C (less than max/B), this is (size_t)A CMP (size_t)B * C.
3656 For large C (more than min/B+2^size), this is also true, with the
3657 multiplication computed modulo 2^size.
3658 For intermediate C, this just tests the sign of A. */
3659 (for cmp (lt le gt ge)
3660 cmp2 (ge ge lt lt)
3661 (simplify
3662 (cmp (convert (exact_div @0 INTEGER_CST@1)) INTEGER_CST@2)
3663 (if (tree_nop_conversion_p (TREE_TYPE (@0), TREE_TYPE (@2))
3664 && TYPE_UNSIGNED (TREE_TYPE (@2)) && !TYPE_UNSIGNED (TREE_TYPE (@0))
3665 && wi::gt_p (wi::to_wide (@1), 0, TYPE_SIGN (TREE_TYPE (@1))))
3666 (with
3667 {
3668 tree utype = TREE_TYPE (@2);
3669 wide_int denom = wi::to_wide (@1);
3670 wide_int right = wi::to_wide (@2);
3671 wide_int smax = wi::sdiv_trunc (wi::max_value (TREE_TYPE (@0)), denom);
3672 wide_int smin = wi::sdiv_trunc (wi::min_value (TREE_TYPE (@0)), denom);
3673 bool small = wi::leu_p (right, smax);
3674 bool large = wi::geu_p (right, smin);
3675 }
3676 (if (small || large)
3677 (cmp (convert:utype @0) (mult @2 (convert @1)))
3678 (cmp2 @0 { build_zero_cst (TREE_TYPE (@0)); }))))))
3679
3680 /* Unordered tests if either argument is a NaN. */
3681 (simplify
3682 (bit_ior (unordered @0 @0) (unordered @1 @1))
3683 (if (types_match (@0, @1))
3684 (unordered @0 @1)))
3685 (simplify
3686 (bit_and (ordered @0 @0) (ordered @1 @1))
3687 (if (types_match (@0, @1))
3688 (ordered @0 @1)))
3689 (simplify
3690 (bit_ior:c (unordered @0 @0) (unordered:c@2 @0 @1))
3691 @2)
3692 (simplify
3693 (bit_and:c (ordered @0 @0) (ordered:c@2 @0 @1))
3694 @2)
3695
3696 /* Simple range test simplifications. */
3697 /* A < B || A >= B -> true. */
3698 (for test1 (lt le le le ne ge)
3699 test2 (ge gt ge ne eq ne)
3700 (simplify
3701 (bit_ior:c (test1 @0 @1) (test2 @0 @1))
3702 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
3703 || VECTOR_INTEGER_TYPE_P (TREE_TYPE (@0)))
3704 { constant_boolean_node (true, type); })))
3705 /* A < B && A >= B -> false. */
3706 (for test1 (lt lt lt le ne eq)
3707 test2 (ge gt eq gt eq gt)
3708 (simplify
3709 (bit_and:c (test1 @0 @1) (test2 @0 @1))
3710 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
3711 || VECTOR_INTEGER_TYPE_P (TREE_TYPE (@0)))
3712 { constant_boolean_node (false, type); })))
3713
3714 /* A & (2**N - 1) <= 2**K - 1 -> A & (2**N - 2**K) == 0
3715 A & (2**N - 1) > 2**K - 1 -> A & (2**N - 2**K) != 0
3716
3717 Note that comparisons
3718 A & (2**N - 1) < 2**K -> A & (2**N - 2**K) == 0
3719 A & (2**N - 1) >= 2**K -> A & (2**N - 2**K) != 0
3720 will be canonicalized to above so there's no need to
3721 consider them here.
3722 */
3723
3724 (for cmp (le gt)
3725 eqcmp (eq ne)
3726 (simplify
3727 (cmp (bit_and@0 @1 INTEGER_CST@2) INTEGER_CST@3)
3728 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)))
3729 (with
3730 {
3731 tree ty = TREE_TYPE (@0);
3732 unsigned prec = TYPE_PRECISION (ty);
3733 wide_int mask = wi::to_wide (@2, prec);
3734 wide_int rhs = wi::to_wide (@3, prec);
3735 signop sgn = TYPE_SIGN (ty);
3736 }
3737 (if ((mask & (mask + 1)) == 0 && wi::gt_p (rhs, 0, sgn)
3738 && (rhs & (rhs + 1)) == 0 && wi::ge_p (mask, rhs, sgn))
3739 (eqcmp (bit_and @1 { wide_int_to_tree (ty, mask - rhs); })
3740 { build_zero_cst (ty); }))))))
3741
3742 /* -A CMP -B -> B CMP A. */
3743 (for cmp (tcc_comparison)
3744 scmp (swapped_tcc_comparison)
3745 (simplify
3746 (cmp (negate @0) (negate @1))
3747 (if (FLOAT_TYPE_P (TREE_TYPE (@0))
3748 || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
3749 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))))
3750 (scmp @0 @1)))
3751 (simplify
3752 (cmp (negate @0) CONSTANT_CLASS_P@1)
3753 (if (FLOAT_TYPE_P (TREE_TYPE (@0))
3754 || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
3755 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))))
3756 (with { tree tem = const_unop (NEGATE_EXPR, TREE_TYPE (@0), @1); }
3757 (if (tem && !TREE_OVERFLOW (tem))
3758 (scmp @0 { tem; }))))))
3759
3760 /* Convert ABS_EXPR<x> == 0 or ABS_EXPR<x> != 0 to x == 0 or x != 0. */
3761 (for op (eq ne)
3762 (simplify
3763 (op (abs @0) zerop@1)
3764 (op @0 @1)))
3765
3766 /* From fold_sign_changed_comparison and fold_widened_comparison.
3767 FIXME: the lack of symmetry is disturbing. */
3768 (for cmp (simple_comparison)
3769 (simplify
3770 (cmp (convert@0 @00) (convert?@1 @10))
3771 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
3772 /* Disable this optimization if we're casting a function pointer
3773 type on targets that require function pointer canonicalization. */
3774 && !(targetm.have_canonicalize_funcptr_for_compare ()
3775 && ((POINTER_TYPE_P (TREE_TYPE (@00))
3776 && FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@00))))
3777 || (POINTER_TYPE_P (TREE_TYPE (@10))
3778 && FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@10))))))
3779 && single_use (@0))
3780 (if (TYPE_PRECISION (TREE_TYPE (@00)) == TYPE_PRECISION (TREE_TYPE (@0))
3781 && (TREE_CODE (@10) == INTEGER_CST
3782 || @1 != @10)
3783 && (TYPE_UNSIGNED (TREE_TYPE (@00)) == TYPE_UNSIGNED (TREE_TYPE (@0))
3784 || cmp == NE_EXPR
3785 || cmp == EQ_EXPR)
3786 && !POINTER_TYPE_P (TREE_TYPE (@00)))
3787 /* ??? The special-casing of INTEGER_CST conversion was in the original
3788 code and here to avoid a spurious overflow flag on the resulting
3789 constant which fold_convert produces. */
3790 (if (TREE_CODE (@1) == INTEGER_CST)
3791 (cmp @00 { force_fit_type (TREE_TYPE (@00), wi::to_widest (@1), 0,
3792 TREE_OVERFLOW (@1)); })
3793 (cmp @00 (convert @1)))
3794
3795 (if (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (TREE_TYPE (@00)))
3796 /* If possible, express the comparison in the shorter mode. */
3797 (if ((cmp == EQ_EXPR || cmp == NE_EXPR
3798 || TYPE_UNSIGNED (TREE_TYPE (@0)) == TYPE_UNSIGNED (TREE_TYPE (@00))
3799 || (!TYPE_UNSIGNED (TREE_TYPE (@0))
3800 && TYPE_UNSIGNED (TREE_TYPE (@00))))
3801 && (types_match (TREE_TYPE (@10), TREE_TYPE (@00))
3802 || ((TYPE_PRECISION (TREE_TYPE (@00))
3803 >= TYPE_PRECISION (TREE_TYPE (@10)))
3804 && (TYPE_UNSIGNED (TREE_TYPE (@00))
3805 == TYPE_UNSIGNED (TREE_TYPE (@10))))
3806 || (TREE_CODE (@10) == INTEGER_CST
3807 && INTEGRAL_TYPE_P (TREE_TYPE (@00))
3808 && int_fits_type_p (@10, TREE_TYPE (@00)))))
3809 (cmp @00 (convert @10))
3810 (if (TREE_CODE (@10) == INTEGER_CST
3811 && INTEGRAL_TYPE_P (TREE_TYPE (@00))
3812 && !int_fits_type_p (@10, TREE_TYPE (@00)))
3813 (with
3814 {
3815 tree min = lower_bound_in_type (TREE_TYPE (@10), TREE_TYPE (@00));
3816 tree max = upper_bound_in_type (TREE_TYPE (@10), TREE_TYPE (@00));
3817 bool above = integer_nonzerop (const_binop (LT_EXPR, type, max, @10));
3818 bool below = integer_nonzerop (const_binop (LT_EXPR, type, @10, min));
3819 }
3820 (if (above || below)
3821 (if (cmp == EQ_EXPR || cmp == NE_EXPR)
3822 { constant_boolean_node (cmp == EQ_EXPR ? false : true, type); }
3823 (if (cmp == LT_EXPR || cmp == LE_EXPR)
3824 { constant_boolean_node (above ? true : false, type); }
3825 (if (cmp == GT_EXPR || cmp == GE_EXPR)
3826 { constant_boolean_node (above ? false : true, type); }))))))))))))
3827
3828 (for cmp (eq ne)
3829 /* A local variable can never be pointed to by
3830 the default SSA name of an incoming parameter.
3831 SSA names are canonicalized to 2nd place. */
3832 (simplify
3833 (cmp addr@0 SSA_NAME@1)
3834 (if (SSA_NAME_IS_DEFAULT_DEF (@1)
3835 && TREE_CODE (SSA_NAME_VAR (@1)) == PARM_DECL)
3836 (with { tree base = get_base_address (TREE_OPERAND (@0, 0)); }
3837 (if (TREE_CODE (base) == VAR_DECL
3838 && auto_var_in_fn_p (base, current_function_decl))
3839 (if (cmp == NE_EXPR)
3840 { constant_boolean_node (true, type); }
3841 { constant_boolean_node (false, type); }))))))
3842
3843 /* Equality compare simplifications from fold_binary */
3844 (for cmp (eq ne)
3845
3846 /* If we have (A | C) == D where C & ~D != 0, convert this into 0.
3847 Similarly for NE_EXPR. */
3848 (simplify
3849 (cmp (convert?@3 (bit_ior @0 INTEGER_CST@1)) INTEGER_CST@2)
3850 (if (tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@0))
3851 && wi::bit_and_not (wi::to_wide (@1), wi::to_wide (@2)) != 0)
3852 { constant_boolean_node (cmp == NE_EXPR, type); }))
3853
3854 /* (X ^ Y) == 0 becomes X == Y, and (X ^ Y) != 0 becomes X != Y. */
3855 (simplify
3856 (cmp (bit_xor @0 @1) integer_zerop)
3857 (cmp @0 @1))
3858
3859 /* (X ^ Y) == Y becomes X == 0.
3860 Likewise (X ^ Y) == X becomes Y == 0. */
3861 (simplify
3862 (cmp:c (bit_xor:c @0 @1) @0)
3863 (cmp @1 { build_zero_cst (TREE_TYPE (@1)); }))
3864
3865 /* (X ^ C1) op C2 can be rewritten as X op (C1 ^ C2). */
3866 (simplify
3867 (cmp (convert?@3 (bit_xor @0 INTEGER_CST@1)) INTEGER_CST@2)
3868 (if (tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@0)))
3869 (cmp @0 (bit_xor @1 (convert @2)))))
3870
3871 (simplify
3872 (cmp (convert? addr@0) integer_zerop)
3873 (if (tree_single_nonzero_warnv_p (@0, NULL))
3874 { constant_boolean_node (cmp == NE_EXPR, type); })))
3875
3876 /* If we have (A & C) == C where C is a power of 2, convert this into
3877 (A & C) != 0. Similarly for NE_EXPR. */
3878 (for cmp (eq ne)
3879 icmp (ne eq)
3880 (simplify
3881 (cmp (bit_and@2 @0 integer_pow2p@1) @1)
3882 (icmp @2 { build_zero_cst (TREE_TYPE (@0)); })))
3883
3884 /* If we have (A & C) != 0 ? D : 0 where C and D are powers of 2,
3885 convert this into a shift followed by ANDing with D. */
3886 (simplify
3887 (cond
3888 (ne (bit_and @0 integer_pow2p@1) integer_zerop)
3889 INTEGER_CST@2 integer_zerop)
3890 (if (integer_pow2p (@2))
3891 (with {
3892 int shift = (wi::exact_log2 (wi::to_wide (@2))
3893 - wi::exact_log2 (wi::to_wide (@1)));
3894 }
3895 (if (shift > 0)
3896 (bit_and
3897 (lshift (convert @0) { build_int_cst (integer_type_node, shift); }) @2)
3898 (bit_and
3899 (convert (rshift @0 { build_int_cst (integer_type_node, -shift); }))
3900 @2)))))
3901
3902 /* If we have (A & C) != 0 where C is the sign bit of A, convert
3903 this into A < 0. Similarly for (A & C) == 0 into A >= 0. */
3904 (for cmp (eq ne)
3905 ncmp (ge lt)
3906 (simplify
3907 (cmp (bit_and (convert?@2 @0) integer_pow2p@1) integer_zerop)
3908 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
3909 && type_has_mode_precision_p (TREE_TYPE (@0))
3910 && element_precision (@2) >= element_precision (@0)
3911 && wi::only_sign_bit_p (wi::to_wide (@1), element_precision (@0)))
3912 (with { tree stype = signed_type_for (TREE_TYPE (@0)); }
3913 (ncmp (convert:stype @0) { build_zero_cst (stype); })))))
3914
3915 /* If we have A < 0 ? C : 0 where C is a power of 2, convert
3916 this into a right shift or sign extension followed by ANDing with C. */
3917 (simplify
3918 (cond
3919 (lt @0 integer_zerop)
3920 INTEGER_CST@1 integer_zerop)
3921 (if (integer_pow2p (@1)
3922 && !TYPE_UNSIGNED (TREE_TYPE (@0)))
3923 (with {
3924 int shift = element_precision (@0) - wi::exact_log2 (wi::to_wide (@1)) - 1;
3925 }
3926 (if (shift >= 0)
3927 (bit_and
3928 (convert (rshift @0 { build_int_cst (integer_type_node, shift); }))
3929 @1)
3930 /* Otherwise ctype must be wider than TREE_TYPE (@0) and pure
3931 sign extension followed by AND with C will achieve the effect. */
3932 (bit_and (convert @0) @1)))))
3933
3934 /* When the addresses are not directly of decls compare base and offset.
3935 This implements some remaining parts of fold_comparison address
3936 comparisons but still no complete part of it. Still it is good
3937 enough to make fold_stmt not regress when not dispatching to fold_binary. */
3938 (for cmp (simple_comparison)
3939 (simplify
3940 (cmp (convert1?@2 addr@0) (convert2? addr@1))
3941 (with
3942 {
3943 poly_int64 off0, off1;
3944 tree base0 = get_addr_base_and_unit_offset (TREE_OPERAND (@0, 0), &off0);
3945 tree base1 = get_addr_base_and_unit_offset (TREE_OPERAND (@1, 0), &off1);
3946 if (base0 && TREE_CODE (base0) == MEM_REF)
3947 {
3948 off0 += mem_ref_offset (base0).force_shwi ();
3949 base0 = TREE_OPERAND (base0, 0);
3950 }
3951 if (base1 && TREE_CODE (base1) == MEM_REF)
3952 {
3953 off1 += mem_ref_offset (base1).force_shwi ();
3954 base1 = TREE_OPERAND (base1, 0);
3955 }
3956 }
3957 (if (base0 && base1)
3958 (with
3959 {
3960 int equal = 2;
3961 /* Punt in GENERIC on variables with value expressions;
3962 the value expressions might point to fields/elements
3963 of other vars etc. */
3964 if (GENERIC
3965 && ((VAR_P (base0) && DECL_HAS_VALUE_EXPR_P (base0))
3966 || (VAR_P (base1) && DECL_HAS_VALUE_EXPR_P (base1))))
3967 ;
3968 else if (decl_in_symtab_p (base0)
3969 && decl_in_symtab_p (base1))
3970 equal = symtab_node::get_create (base0)
3971 ->equal_address_to (symtab_node::get_create (base1));
3972 else if ((DECL_P (base0)
3973 || TREE_CODE (base0) == SSA_NAME
3974 || TREE_CODE (base0) == STRING_CST)
3975 && (DECL_P (base1)
3976 || TREE_CODE (base1) == SSA_NAME
3977 || TREE_CODE (base1) == STRING_CST))
3978 equal = (base0 == base1);
3979 if (equal == 0)
3980 {
3981 HOST_WIDE_INT ioff0 = -1, ioff1 = -1;
3982 off0.is_constant (&ioff0);
3983 off1.is_constant (&ioff1);
3984 if ((DECL_P (base0) && TREE_CODE (base1) == STRING_CST)
3985 || (TREE_CODE (base0) == STRING_CST && DECL_P (base1))
3986 || (TREE_CODE (base0) == STRING_CST
3987 && TREE_CODE (base1) == STRING_CST
3988 && ioff0 >= 0 && ioff1 >= 0
3989 && ioff0 < TREE_STRING_LENGTH (base0)
3990 && ioff1 < TREE_STRING_LENGTH (base1)
3991 /* This is a too conservative test that the STRING_CSTs
3992 will not end up being string-merged. */
3993 && strncmp (TREE_STRING_POINTER (base0) + ioff0,
3994 TREE_STRING_POINTER (base1) + ioff1,
3995 MIN (TREE_STRING_LENGTH (base0) - ioff0,
3996 TREE_STRING_LENGTH (base1) - ioff1)) != 0))
3997 ;
3998 else if (!DECL_P (base0) || !DECL_P (base1))
3999 equal = 2;
4000 else if (cmp != EQ_EXPR && cmp != NE_EXPR)
4001 equal = 2;
4002 /* If this is a pointer comparison, ignore for now even
4003 valid equalities where one pointer is the offset zero
4004 of one object and the other to one past end of another one. */
4005 else if (!INTEGRAL_TYPE_P (TREE_TYPE (@2)))
4006 ;
4007 /* Assume that automatic variables can't be adjacent to global
4008 variables. */
4009 else if (is_global_var (base0) != is_global_var (base1))
4010 ;
4011 else
4012 {
4013 tree sz0 = DECL_SIZE_UNIT (base0);
4014 tree sz1 = DECL_SIZE_UNIT (base1);
4015 /* If sizes are unknown, e.g. VLA or not representable,
4016 punt. */
4017 if (!tree_fits_poly_int64_p (sz0)
4018 || !tree_fits_poly_int64_p (sz1))
4019 equal = 2;
4020 else
4021 {
4022 poly_int64 size0 = tree_to_poly_int64 (sz0);
4023 poly_int64 size1 = tree_to_poly_int64 (sz1);
4024 /* If one offset is pointing (or could be) to the beginning
4025 of one object and the other is pointing to one past the
4026 last byte of the other object, punt. */
4027 if (maybe_eq (off0, 0) && maybe_eq (off1, size1))
4028 equal = 2;
4029 else if (maybe_eq (off1, 0) && maybe_eq (off0, size0))
4030 equal = 2;
4031 /* If both offsets are the same, there are some cases
4032 we know that are ok. Either if we know they aren't
4033 zero, or if we know both sizes are no zero. */
4034 if (equal == 2
4035 && known_eq (off0, off1)
4036 && (known_ne (off0, 0)
4037 || (known_ne (size0, 0) && known_ne (size1, 0))))
4038 equal = 0;
4039 }
4040 }
4041 }
4042 }
4043 (if (equal == 1
4044 && (cmp == EQ_EXPR || cmp == NE_EXPR
4045 /* If the offsets are equal we can ignore overflow. */
4046 || known_eq (off0, off1)
4047 || TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
4048 /* Or if we compare using pointers to decls or strings. */
4049 || (POINTER_TYPE_P (TREE_TYPE (@2))
4050 && (DECL_P (base0) || TREE_CODE (base0) == STRING_CST))))
4051 (switch
4052 (if (cmp == EQ_EXPR && (known_eq (off0, off1) || known_ne (off0, off1)))
4053 { constant_boolean_node (known_eq (off0, off1), type); })
4054 (if (cmp == NE_EXPR && (known_eq (off0, off1) || known_ne (off0, off1)))
4055 { constant_boolean_node (known_ne (off0, off1), type); })
4056 (if (cmp == LT_EXPR && (known_lt (off0, off1) || known_ge (off0, off1)))
4057 { constant_boolean_node (known_lt (off0, off1), type); })
4058 (if (cmp == LE_EXPR && (known_le (off0, off1) || known_gt (off0, off1)))
4059 { constant_boolean_node (known_le (off0, off1), type); })
4060 (if (cmp == GE_EXPR && (known_ge (off0, off1) || known_lt (off0, off1)))
4061 { constant_boolean_node (known_ge (off0, off1), type); })
4062 (if (cmp == GT_EXPR && (known_gt (off0, off1) || known_le (off0, off1)))
4063 { constant_boolean_node (known_gt (off0, off1), type); }))
4064 (if (equal == 0)
4065 (switch
4066 (if (cmp == EQ_EXPR)
4067 { constant_boolean_node (false, type); })
4068 (if (cmp == NE_EXPR)
4069 { constant_boolean_node (true, type); })))))))))
4070
4071 /* Simplify pointer equality compares using PTA. */
4072 (for neeq (ne eq)
4073 (simplify
4074 (neeq @0 @1)
4075 (if (POINTER_TYPE_P (TREE_TYPE (@0))
4076 && ptrs_compare_unequal (@0, @1))
4077 { constant_boolean_node (neeq != EQ_EXPR, type); })))
4078
4079 /* PR70920: Transform (intptr_t)x eq/ne CST to x eq/ne (typeof x) CST.
4080 and (typeof ptr_cst) x eq/ne ptr_cst to x eq/ne (typeof x) CST.
4081 Disable the transform if either operand is pointer to function.
4082 This broke pr22051-2.c for arm where function pointer
4083 canonicalizaion is not wanted. */
4084
4085 (for cmp (ne eq)
4086 (simplify
4087 (cmp (convert @0) INTEGER_CST@1)
4088 (if (((POINTER_TYPE_P (TREE_TYPE (@0))
4089 && !FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@0)))
4090 && INTEGRAL_TYPE_P (TREE_TYPE (@1)))
4091 || (INTEGRAL_TYPE_P (TREE_TYPE (@0))
4092 && POINTER_TYPE_P (TREE_TYPE (@1))
4093 && !FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@1)))))
4094 && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1)))
4095 (cmp @0 (convert @1)))))
4096
4097 /* Non-equality compare simplifications from fold_binary */
4098 (for cmp (lt gt le ge)
4099 /* Comparisons with the highest or lowest possible integer of
4100 the specified precision will have known values. */
4101 (simplify
4102 (cmp (convert?@2 @0) uniform_integer_cst_p@1)
4103 (if ((INTEGRAL_TYPE_P (TREE_TYPE (@1))
4104 || POINTER_TYPE_P (TREE_TYPE (@1))
4105 || VECTOR_INTEGER_TYPE_P (TREE_TYPE (@1)))
4106 && tree_nop_conversion_p (TREE_TYPE (@2), TREE_TYPE (@0)))
4107 (with
4108 {
4109 tree cst = uniform_integer_cst_p (@1);
4110 tree arg1_type = TREE_TYPE (cst);
4111 unsigned int prec = TYPE_PRECISION (arg1_type);
4112 wide_int max = wi::max_value (arg1_type);
4113 wide_int signed_max = wi::max_value (prec, SIGNED);
4114 wide_int min = wi::min_value (arg1_type);
4115 }
4116 (switch
4117 (if (wi::to_wide (cst) == max)
4118 (switch
4119 (if (cmp == GT_EXPR)
4120 { constant_boolean_node (false, type); })
4121 (if (cmp == GE_EXPR)
4122 (eq @2 @1))
4123 (if (cmp == LE_EXPR)
4124 { constant_boolean_node (true, type); })
4125 (if (cmp == LT_EXPR)
4126 (ne @2 @1))))
4127 (if (wi::to_wide (cst) == min)
4128 (switch
4129 (if (cmp == LT_EXPR)
4130 { constant_boolean_node (false, type); })
4131 (if (cmp == LE_EXPR)
4132 (eq @2 @1))
4133 (if (cmp == GE_EXPR)
4134 { constant_boolean_node (true, type); })
4135 (if (cmp == GT_EXPR)
4136 (ne @2 @1))))
4137 (if (wi::to_wide (cst) == max - 1)
4138 (switch
4139 (if (cmp == GT_EXPR)
4140 (eq @2 { build_uniform_cst (TREE_TYPE (@1),
4141 wide_int_to_tree (TREE_TYPE (cst),
4142 wi::to_wide (cst)
4143 + 1)); }))
4144 (if (cmp == LE_EXPR)
4145 (ne @2 { build_uniform_cst (TREE_TYPE (@1),
4146 wide_int_to_tree (TREE_TYPE (cst),
4147 wi::to_wide (cst)
4148 + 1)); }))))
4149 (if (wi::to_wide (cst) == min + 1)
4150 (switch
4151 (if (cmp == GE_EXPR)
4152 (ne @2 { build_uniform_cst (TREE_TYPE (@1),
4153 wide_int_to_tree (TREE_TYPE (cst),
4154 wi::to_wide (cst)
4155 - 1)); }))
4156 (if (cmp == LT_EXPR)
4157 (eq @2 { build_uniform_cst (TREE_TYPE (@1),
4158 wide_int_to_tree (TREE_TYPE (cst),
4159 wi::to_wide (cst)
4160 - 1)); }))))
4161 (if (wi::to_wide (cst) == signed_max
4162 && TYPE_UNSIGNED (arg1_type)
4163 /* We will flip the signedness of the comparison operator
4164 associated with the mode of @1, so the sign bit is
4165 specified by this mode. Check that @1 is the signed
4166 max associated with this sign bit. */
4167 && prec == GET_MODE_PRECISION (SCALAR_INT_TYPE_MODE (arg1_type))
4168 /* signed_type does not work on pointer types. */
4169 && INTEGRAL_TYPE_P (arg1_type))
4170 /* The following case also applies to X < signed_max+1
4171 and X >= signed_max+1 because previous transformations. */
4172 (if (cmp == LE_EXPR || cmp == GT_EXPR)
4173 (with { tree st = signed_type_for (TREE_TYPE (@1)); }
4174 (switch
4175 (if (cst == @1 && cmp == LE_EXPR)
4176 (ge (convert:st @0) { build_zero_cst (st); }))
4177 (if (cst == @1 && cmp == GT_EXPR)
4178 (lt (convert:st @0) { build_zero_cst (st); }))
4179 (if (cmp == LE_EXPR)
4180 (ge (view_convert:st @0) { build_zero_cst (st); }))
4181 (if (cmp == GT_EXPR)
4182 (lt (view_convert:st @0) { build_zero_cst (st); })))))))))))
4183
4184 (for cmp (unordered ordered unlt unle ungt unge uneq ltgt)
4185 /* If the second operand is NaN, the result is constant. */
4186 (simplify
4187 (cmp @0 REAL_CST@1)
4188 (if (REAL_VALUE_ISNAN (TREE_REAL_CST (@1))
4189 && (cmp != LTGT_EXPR || ! flag_trapping_math))
4190 { constant_boolean_node (cmp == ORDERED_EXPR || cmp == LTGT_EXPR
4191 ? false : true, type); })))
4192
4193 /* bool_var != 0 becomes bool_var. */
4194 (simplify
4195 (ne @0 integer_zerop)
4196 (if (TREE_CODE (TREE_TYPE (@0)) == BOOLEAN_TYPE
4197 && types_match (type, TREE_TYPE (@0)))
4198 (non_lvalue @0)))
4199 /* bool_var == 1 becomes bool_var. */
4200 (simplify
4201 (eq @0 integer_onep)
4202 (if (TREE_CODE (TREE_TYPE (@0)) == BOOLEAN_TYPE
4203 && types_match (type, TREE_TYPE (@0)))
4204 (non_lvalue @0)))
4205 /* Do not handle
4206 bool_var == 0 becomes !bool_var or
4207 bool_var != 1 becomes !bool_var
4208 here because that only is good in assignment context as long
4209 as we require a tcc_comparison in GIMPLE_CONDs where we'd
4210 replace if (x == 0) with tem = ~x; if (tem != 0) which is
4211 clearly less optimal and which we'll transform again in forwprop. */
4212
4213 /* When one argument is a constant, overflow detection can be simplified.
4214 Currently restricted to single use so as not to interfere too much with
4215 ADD_OVERFLOW detection in tree-ssa-math-opts.c.
4216 A + CST CMP A -> A CMP' CST' */
4217 (for cmp (lt le ge gt)
4218 out (gt gt le le)
4219 (simplify
4220 (cmp:c (plus@2 @0 INTEGER_CST@1) @0)
4221 (if (TYPE_UNSIGNED (TREE_TYPE (@0))
4222 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))
4223 && wi::to_wide (@1) != 0
4224 && single_use (@2))
4225 (with { unsigned int prec = TYPE_PRECISION (TREE_TYPE (@0)); }
4226 (out @0 { wide_int_to_tree (TREE_TYPE (@0),
4227 wi::max_value (prec, UNSIGNED)
4228 - wi::to_wide (@1)); })))))
4229
4230 /* To detect overflow in unsigned A - B, A < B is simpler than A - B > A.
4231 However, the detection logic for SUB_OVERFLOW in tree-ssa-math-opts.c
4232 expects the long form, so we restrict the transformation for now. */
4233 (for cmp (gt le)
4234 (simplify
4235 (cmp:c (minus@2 @0 @1) @0)
4236 (if (single_use (@2)
4237 && ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
4238 && TYPE_UNSIGNED (TREE_TYPE (@0))
4239 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
4240 (cmp @1 @0))))
4241
4242 /* Testing for overflow is unnecessary if we already know the result. */
4243 /* A - B > A */
4244 (for cmp (gt le)
4245 out (ne eq)
4246 (simplify
4247 (cmp:c (realpart (IFN_SUB_OVERFLOW@2 @0 @1)) @0)
4248 (if (TYPE_UNSIGNED (TREE_TYPE (@0))
4249 && types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
4250 (out (imagpart @2) { build_zero_cst (TREE_TYPE (@0)); }))))
4251 /* A + B < A */
4252 (for cmp (lt ge)
4253 out (ne eq)
4254 (simplify
4255 (cmp:c (realpart (IFN_ADD_OVERFLOW:c@2 @0 @1)) @0)
4256 (if (TYPE_UNSIGNED (TREE_TYPE (@0))
4257 && types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
4258 (out (imagpart @2) { build_zero_cst (TREE_TYPE (@0)); }))))
4259
4260 /* For unsigned operands, -1 / B < A checks whether A * B would overflow.
4261 Simplify it to __builtin_mul_overflow (A, B, <unused>). */
4262 (for cmp (lt ge)
4263 out (ne eq)
4264 (simplify
4265 (cmp:c (trunc_div:s integer_all_onesp @1) @0)
4266 (if (TYPE_UNSIGNED (TREE_TYPE (@0)) && !VECTOR_TYPE_P (TREE_TYPE (@0)))
4267 (with { tree t = TREE_TYPE (@0), cpx = build_complex_type (t); }
4268 (out (imagpart (IFN_MUL_OVERFLOW:cpx @0 @1)) { build_zero_cst (t); })))))
4269
4270 /* Simplification of math builtins. These rules must all be optimizations
4271 as well as IL simplifications. If there is a possibility that the new
4272 form could be a pessimization, the rule should go in the canonicalization
4273 section that follows this one.
4274
4275 Rules can generally go in this section if they satisfy one of
4276 the following:
4277
4278 - the rule describes an identity
4279
4280 - the rule replaces calls with something as simple as addition or
4281 multiplication
4282
4283 - the rule contains unary calls only and simplifies the surrounding
4284 arithmetic. (The idea here is to exclude non-unary calls in which
4285 one operand is constant and in which the call is known to be cheap
4286 when the operand has that value.) */
4287
4288 (if (flag_unsafe_math_optimizations)
4289 /* Simplify sqrt(x) * sqrt(x) -> x. */
4290 (simplify
4291 (mult (SQRT_ALL@1 @0) @1)
4292 (if (!HONOR_SNANS (type))
4293 @0))
4294
4295 (for op (plus minus)
4296 /* Simplify (A / C) +- (B / C) -> (A +- B) / C. */
4297 (simplify
4298 (op (rdiv @0 @1)
4299 (rdiv @2 @1))
4300 (rdiv (op @0 @2) @1)))
4301
4302 (for cmp (lt le gt ge)
4303 neg_cmp (gt ge lt le)
4304 /* Simplify (x * C1) cmp C2 -> x cmp (C2 / C1), where C1 != 0. */
4305 (simplify
4306 (cmp (mult @0 REAL_CST@1) REAL_CST@2)
4307 (with
4308 { tree tem = const_binop (RDIV_EXPR, type, @2, @1); }
4309 (if (tem
4310 && !(REAL_VALUE_ISINF (TREE_REAL_CST (tem))
4311 || (real_zerop (tem) && !real_zerop (@1))))
4312 (switch
4313 (if (real_less (&dconst0, TREE_REAL_CST_PTR (@1)))
4314 (cmp @0 { tem; }))
4315 (if (real_less (TREE_REAL_CST_PTR (@1), &dconst0))
4316 (neg_cmp @0 { tem; })))))))
4317
4318 /* Simplify sqrt(x) * sqrt(y) -> sqrt(x*y). */
4319 (for root (SQRT CBRT)
4320 (simplify
4321 (mult (root:s @0) (root:s @1))
4322 (root (mult @0 @1))))
4323
4324 /* Simplify expN(x) * expN(y) -> expN(x+y). */
4325 (for exps (EXP EXP2 EXP10 POW10)
4326 (simplify
4327 (mult (exps:s @0) (exps:s @1))
4328 (exps (plus @0 @1))))
4329
4330 /* Simplify a/root(b/c) into a*root(c/b). */
4331 (for root (SQRT CBRT)
4332 (simplify
4333 (rdiv @0 (root:s (rdiv:s @1 @2)))
4334 (mult @0 (root (rdiv @2 @1)))))
4335
4336 /* Simplify x/expN(y) into x*expN(-y). */
4337 (for exps (EXP EXP2 EXP10 POW10)
4338 (simplify
4339 (rdiv @0 (exps:s @1))
4340 (mult @0 (exps (negate @1)))))
4341
4342 (for logs (LOG LOG2 LOG10 LOG10)
4343 exps (EXP EXP2 EXP10 POW10)
4344 /* logN(expN(x)) -> x. */
4345 (simplify
4346 (logs (exps @0))
4347 @0)
4348 /* expN(logN(x)) -> x. */
4349 (simplify
4350 (exps (logs @0))
4351 @0))
4352
4353 /* Optimize logN(func()) for various exponential functions. We
4354 want to determine the value "x" and the power "exponent" in
4355 order to transform logN(x**exponent) into exponent*logN(x). */
4356 (for logs (LOG LOG LOG LOG2 LOG2 LOG2 LOG10 LOG10)
4357 exps (EXP2 EXP10 POW10 EXP EXP10 POW10 EXP EXP2)
4358 (simplify
4359 (logs (exps @0))
4360 (if (SCALAR_FLOAT_TYPE_P (type))
4361 (with {
4362 tree x;
4363 switch (exps)
4364 {
4365 CASE_CFN_EXP:
4366 /* Prepare to do logN(exp(exponent)) -> exponent*logN(e). */
4367 x = build_real_truncate (type, dconst_e ());
4368 break;
4369 CASE_CFN_EXP2:
4370 /* Prepare to do logN(exp2(exponent)) -> exponent*logN(2). */
4371 x = build_real (type, dconst2);
4372 break;
4373 CASE_CFN_EXP10:
4374 CASE_CFN_POW10:
4375 /* Prepare to do logN(exp10(exponent)) -> exponent*logN(10). */
4376 {
4377 REAL_VALUE_TYPE dconst10;
4378 real_from_integer (&dconst10, VOIDmode, 10, SIGNED);
4379 x = build_real (type, dconst10);
4380 }
4381 break;
4382 default:
4383 gcc_unreachable ();
4384 }
4385 }
4386 (mult (logs { x; }) @0)))))
4387
4388 (for logs (LOG LOG
4389 LOG2 LOG2
4390 LOG10 LOG10)
4391 exps (SQRT CBRT)
4392 (simplify
4393 (logs (exps @0))
4394 (if (SCALAR_FLOAT_TYPE_P (type))
4395 (with {
4396 tree x;
4397 switch (exps)
4398 {
4399 CASE_CFN_SQRT:
4400 /* Prepare to do logN(sqrt(x)) -> 0.5*logN(x). */
4401 x = build_real (type, dconsthalf);
4402 break;
4403 CASE_CFN_CBRT:
4404 /* Prepare to do logN(cbrt(x)) -> (1/3)*logN(x). */
4405 x = build_real_truncate (type, dconst_third ());
4406 break;
4407 default:
4408 gcc_unreachable ();
4409 }
4410 }
4411 (mult { x; } (logs @0))))))
4412
4413 /* logN(pow(x,exponent)) -> exponent*logN(x). */
4414 (for logs (LOG LOG2 LOG10)
4415 pows (POW)
4416 (simplify
4417 (logs (pows @0 @1))
4418 (mult @1 (logs @0))))
4419
4420 /* pow(C,x) -> exp(log(C)*x) if C > 0,
4421 or if C is a positive power of 2,
4422 pow(C,x) -> exp2(log2(C)*x). */
4423 #if GIMPLE
4424 (for pows (POW)
4425 exps (EXP)
4426 logs (LOG)
4427 exp2s (EXP2)
4428 log2s (LOG2)
4429 (simplify
4430 (pows REAL_CST@0 @1)
4431 (if (real_compare (GT_EXPR, TREE_REAL_CST_PTR (@0), &dconst0)
4432 && real_isfinite (TREE_REAL_CST_PTR (@0))
4433 /* As libmvec doesn't have a vectorized exp2, defer optimizing
4434 the use_exp2 case until after vectorization. It seems actually
4435 beneficial for all constants to postpone this until later,
4436 because exp(log(C)*x), while faster, will have worse precision
4437 and if x folds into a constant too, that is unnecessary
4438 pessimization. */
4439 && canonicalize_math_after_vectorization_p ())
4440 (with {
4441 const REAL_VALUE_TYPE *const value = TREE_REAL_CST_PTR (@0);
4442 bool use_exp2 = false;
4443 if (targetm.libc_has_function (function_c99_misc)
4444 && value->cl == rvc_normal)
4445 {
4446 REAL_VALUE_TYPE frac_rvt = *value;
4447 SET_REAL_EXP (&frac_rvt, 1);
4448 if (real_equal (&frac_rvt, &dconst1))
4449 use_exp2 = true;
4450 }
4451 }
4452 (if (!use_exp2)
4453 (if (optimize_pow_to_exp (@0, @1))
4454 (exps (mult (logs @0) @1)))
4455 (exp2s (mult (log2s @0) @1)))))))
4456 #endif
4457
4458 /* pow(C,x)*expN(y) -> expN(logN(C)*x+y) if C > 0. */
4459 (for pows (POW)
4460 exps (EXP EXP2 EXP10 POW10)
4461 logs (LOG LOG2 LOG10 LOG10)
4462 (simplify
4463 (mult:c (pows:s REAL_CST@0 @1) (exps:s @2))
4464 (if (real_compare (GT_EXPR, TREE_REAL_CST_PTR (@0), &dconst0)
4465 && real_isfinite (TREE_REAL_CST_PTR (@0)))
4466 (exps (plus (mult (logs @0) @1) @2)))))
4467
4468 (for sqrts (SQRT)
4469 cbrts (CBRT)
4470 pows (POW)
4471 exps (EXP EXP2 EXP10 POW10)
4472 /* sqrt(expN(x)) -> expN(x*0.5). */
4473 (simplify
4474 (sqrts (exps @0))
4475 (exps (mult @0 { build_real (type, dconsthalf); })))
4476 /* cbrt(expN(x)) -> expN(x/3). */
4477 (simplify
4478 (cbrts (exps @0))
4479 (exps (mult @0 { build_real_truncate (type, dconst_third ()); })))
4480 /* pow(expN(x), y) -> expN(x*y). */
4481 (simplify
4482 (pows (exps @0) @1)
4483 (exps (mult @0 @1))))
4484
4485 /* tan(atan(x)) -> x. */
4486 (for tans (TAN)
4487 atans (ATAN)
4488 (simplify
4489 (tans (atans @0))
4490 @0)))
4491
4492 /* Simplify sin(atan(x)) -> x / sqrt(x*x + 1). */
4493 (for sins (SIN)
4494 atans (ATAN)
4495 sqrts (SQRT)
4496 copysigns (COPYSIGN)
4497 (simplify
4498 (sins (atans:s @0))
4499 (with
4500 {
4501 REAL_VALUE_TYPE r_cst;
4502 build_sinatan_real (&r_cst, type);
4503 tree t_cst = build_real (type, r_cst);
4504 tree t_one = build_one_cst (type);
4505 }
4506 (if (SCALAR_FLOAT_TYPE_P (type))
4507 (cond (lt (abs @0) { t_cst; })
4508 (rdiv @0 (sqrts (plus (mult @0 @0) { t_one; })))
4509 (copysigns { t_one; } @0))))))
4510
4511 /* Simplify cos(atan(x)) -> 1 / sqrt(x*x + 1). */
4512 (for coss (COS)
4513 atans (ATAN)
4514 sqrts (SQRT)
4515 copysigns (COPYSIGN)
4516 (simplify
4517 (coss (atans:s @0))
4518 (with
4519 {
4520 REAL_VALUE_TYPE r_cst;
4521 build_sinatan_real (&r_cst, type);
4522 tree t_cst = build_real (type, r_cst);
4523 tree t_one = build_one_cst (type);
4524 tree t_zero = build_zero_cst (type);
4525 }
4526 (if (SCALAR_FLOAT_TYPE_P (type))
4527 (cond (lt (abs @0) { t_cst; })
4528 (rdiv { t_one; } (sqrts (plus (mult @0 @0) { t_one; })))
4529 (copysigns { t_zero; } @0))))))
4530
4531 (if (!flag_errno_math)
4532 /* Simplify sinh(atanh(x)) -> x / sqrt((1 - x)*(1 + x)). */
4533 (for sinhs (SINH)
4534 atanhs (ATANH)
4535 sqrts (SQRT)
4536 (simplify
4537 (sinhs (atanhs:s @0))
4538 (with { tree t_one = build_one_cst (type); }
4539 (rdiv @0 (sqrts (mult (minus { t_one; } @0) (plus { t_one; } @0)))))))
4540
4541 /* Simplify cosh(atanh(x)) -> 1 / sqrt((1 - x)*(1 + x)) */
4542 (for coshs (COSH)
4543 atanhs (ATANH)
4544 sqrts (SQRT)
4545 (simplify
4546 (coshs (atanhs:s @0))
4547 (with { tree t_one = build_one_cst (type); }
4548 (rdiv { t_one; } (sqrts (mult (minus { t_one; } @0) (plus { t_one; } @0))))))))
4549
4550 /* cabs(x+0i) or cabs(0+xi) -> abs(x). */
4551 (simplify
4552 (CABS (complex:C @0 real_zerop@1))
4553 (abs @0))
4554
4555 /* trunc(trunc(x)) -> trunc(x), etc. */
4556 (for fns (TRUNC_ALL FLOOR_ALL CEIL_ALL ROUND_ALL NEARBYINT_ALL RINT_ALL)
4557 (simplify
4558 (fns (fns @0))
4559 (fns @0)))
4560 /* f(x) -> x if x is integer valued and f does nothing for such values. */
4561 (for fns (TRUNC_ALL FLOOR_ALL CEIL_ALL ROUND_ALL NEARBYINT_ALL RINT_ALL)
4562 (simplify
4563 (fns integer_valued_real_p@0)
4564 @0))
4565
4566 /* hypot(x,0) and hypot(0,x) -> abs(x). */
4567 (simplify
4568 (HYPOT:c @0 real_zerop@1)
4569 (abs @0))
4570
4571 /* pow(1,x) -> 1. */
4572 (simplify
4573 (POW real_onep@0 @1)
4574 @0)
4575
4576 (simplify
4577 /* copysign(x,x) -> x. */
4578 (COPYSIGN_ALL @0 @0)
4579 @0)
4580
4581 (simplify
4582 /* copysign(x,y) -> fabs(x) if y is nonnegative. */
4583 (COPYSIGN_ALL @0 tree_expr_nonnegative_p@1)
4584 (abs @0))
4585
4586 (for scale (LDEXP SCALBN SCALBLN)
4587 /* ldexp(0, x) -> 0. */
4588 (simplify
4589 (scale real_zerop@0 @1)
4590 @0)
4591 /* ldexp(x, 0) -> x. */
4592 (simplify
4593 (scale @0 integer_zerop@1)
4594 @0)
4595 /* ldexp(x, y) -> x if x is +-Inf or NaN. */
4596 (simplify
4597 (scale REAL_CST@0 @1)
4598 (if (!real_isfinite (TREE_REAL_CST_PTR (@0)))
4599 @0)))
4600
4601 /* Canonicalization of sequences of math builtins. These rules represent
4602 IL simplifications but are not necessarily optimizations.
4603
4604 The sincos pass is responsible for picking "optimal" implementations
4605 of math builtins, which may be more complicated and can sometimes go
4606 the other way, e.g. converting pow into a sequence of sqrts.
4607 We only want to do these canonicalizations before the pass has run. */
4608
4609 (if (flag_unsafe_math_optimizations && canonicalize_math_p ())
4610 /* Simplify tan(x) * cos(x) -> sin(x). */
4611 (simplify
4612 (mult:c (TAN:s @0) (COS:s @0))
4613 (SIN @0))
4614
4615 /* Simplify x * pow(x,c) -> pow(x,c+1). */
4616 (simplify
4617 (mult:c @0 (POW:s @0 REAL_CST@1))
4618 (if (!TREE_OVERFLOW (@1))
4619 (POW @0 (plus @1 { build_one_cst (type); }))))
4620
4621 /* Simplify sin(x) / cos(x) -> tan(x). */
4622 (simplify
4623 (rdiv (SIN:s @0) (COS:s @0))
4624 (TAN @0))
4625
4626 /* Simplify cos(x) / sin(x) -> 1 / tan(x). */
4627 (simplify
4628 (rdiv (COS:s @0) (SIN:s @0))
4629 (rdiv { build_one_cst (type); } (TAN @0)))
4630
4631 /* Simplify sin(x) / tan(x) -> cos(x). */
4632 (simplify
4633 (rdiv (SIN:s @0) (TAN:s @0))
4634 (if (! HONOR_NANS (@0)
4635 && ! HONOR_INFINITIES (@0))
4636 (COS @0)))
4637
4638 /* Simplify tan(x) / sin(x) -> 1.0 / cos(x). */
4639 (simplify
4640 (rdiv (TAN:s @0) (SIN:s @0))
4641 (if (! HONOR_NANS (@0)
4642 && ! HONOR_INFINITIES (@0))
4643 (rdiv { build_one_cst (type); } (COS @0))))
4644
4645 /* Simplify pow(x,y) * pow(x,z) -> pow(x,y+z). */
4646 (simplify
4647 (mult (POW:s @0 @1) (POW:s @0 @2))
4648 (POW @0 (plus @1 @2)))
4649
4650 /* Simplify pow(x,y) * pow(z,y) -> pow(x*z,y). */
4651 (simplify
4652 (mult (POW:s @0 @1) (POW:s @2 @1))
4653 (POW (mult @0 @2) @1))
4654
4655 /* Simplify powi(x,y) * powi(z,y) -> powi(x*z,y). */
4656 (simplify
4657 (mult (POWI:s @0 @1) (POWI:s @2 @1))
4658 (POWI (mult @0 @2) @1))
4659
4660 /* Simplify pow(x,c) / x -> pow(x,c-1). */
4661 (simplify
4662 (rdiv (POW:s @0 REAL_CST@1) @0)
4663 (if (!TREE_OVERFLOW (@1))
4664 (POW @0 (minus @1 { build_one_cst (type); }))))
4665
4666 /* Simplify x / pow (y,z) -> x * pow(y,-z). */
4667 (simplify
4668 (rdiv @0 (POW:s @1 @2))
4669 (mult @0 (POW @1 (negate @2))))
4670
4671 (for sqrts (SQRT)
4672 cbrts (CBRT)
4673 pows (POW)
4674 /* sqrt(sqrt(x)) -> pow(x,1/4). */
4675 (simplify
4676 (sqrts (sqrts @0))
4677 (pows @0 { build_real (type, dconst_quarter ()); }))
4678 /* sqrt(cbrt(x)) -> pow(x,1/6). */
4679 (simplify
4680 (sqrts (cbrts @0))
4681 (pows @0 { build_real_truncate (type, dconst_sixth ()); }))
4682 /* cbrt(sqrt(x)) -> pow(x,1/6). */
4683 (simplify
4684 (cbrts (sqrts @0))
4685 (pows @0 { build_real_truncate (type, dconst_sixth ()); }))
4686 /* cbrt(cbrt(x)) -> pow(x,1/9), iff x is nonnegative. */
4687 (simplify
4688 (cbrts (cbrts tree_expr_nonnegative_p@0))
4689 (pows @0 { build_real_truncate (type, dconst_ninth ()); }))
4690 /* sqrt(pow(x,y)) -> pow(|x|,y*0.5). */
4691 (simplify
4692 (sqrts (pows @0 @1))
4693 (pows (abs @0) (mult @1 { build_real (type, dconsthalf); })))
4694 /* cbrt(pow(x,y)) -> pow(x,y/3), iff x is nonnegative. */
4695 (simplify
4696 (cbrts (pows tree_expr_nonnegative_p@0 @1))
4697 (pows @0 (mult @1 { build_real_truncate (type, dconst_third ()); })))
4698 /* pow(sqrt(x),y) -> pow(x,y*0.5). */
4699 (simplify
4700 (pows (sqrts @0) @1)
4701 (pows @0 (mult @1 { build_real (type, dconsthalf); })))
4702 /* pow(cbrt(x),y) -> pow(x,y/3) iff x is nonnegative. */
4703 (simplify
4704 (pows (cbrts tree_expr_nonnegative_p@0) @1)
4705 (pows @0 (mult @1 { build_real_truncate (type, dconst_third ()); })))
4706 /* pow(pow(x,y),z) -> pow(x,y*z) iff x is nonnegative. */
4707 (simplify
4708 (pows (pows tree_expr_nonnegative_p@0 @1) @2)
4709 (pows @0 (mult @1 @2))))
4710
4711 /* cabs(x+xi) -> fabs(x)*sqrt(2). */
4712 (simplify
4713 (CABS (complex @0 @0))
4714 (mult (abs @0) { build_real_truncate (type, dconst_sqrt2 ()); }))
4715
4716 /* hypot(x,x) -> fabs(x)*sqrt(2). */
4717 (simplify
4718 (HYPOT @0 @0)
4719 (mult (abs @0) { build_real_truncate (type, dconst_sqrt2 ()); }))
4720
4721 /* cexp(x+yi) -> exp(x)*cexpi(y). */
4722 (for cexps (CEXP)
4723 exps (EXP)
4724 cexpis (CEXPI)
4725 (simplify
4726 (cexps compositional_complex@0)
4727 (if (targetm.libc_has_function (function_c99_math_complex))
4728 (complex
4729 (mult (exps@1 (realpart @0)) (realpart (cexpis:type@2 (imagpart @0))))
4730 (mult @1 (imagpart @2)))))))
4731
4732 (if (canonicalize_math_p ())
4733 /* floor(x) -> trunc(x) if x is nonnegative. */
4734 (for floors (FLOOR_ALL)
4735 truncs (TRUNC_ALL)
4736 (simplify
4737 (floors tree_expr_nonnegative_p@0)
4738 (truncs @0))))
4739
4740 (match double_value_p
4741 @0
4742 (if (TYPE_MAIN_VARIANT (TREE_TYPE (@0)) == double_type_node)))
4743 (for froms (BUILT_IN_TRUNCL
4744 BUILT_IN_FLOORL
4745 BUILT_IN_CEILL
4746 BUILT_IN_ROUNDL
4747 BUILT_IN_NEARBYINTL
4748 BUILT_IN_RINTL)
4749 tos (BUILT_IN_TRUNC
4750 BUILT_IN_FLOOR
4751 BUILT_IN_CEIL
4752 BUILT_IN_ROUND
4753 BUILT_IN_NEARBYINT
4754 BUILT_IN_RINT)
4755 /* truncl(extend(x)) -> extend(trunc(x)), etc., if x is a double. */
4756 (if (optimize && canonicalize_math_p ())
4757 (simplify
4758 (froms (convert double_value_p@0))
4759 (convert (tos @0)))))
4760
4761 (match float_value_p
4762 @0
4763 (if (TYPE_MAIN_VARIANT (TREE_TYPE (@0)) == float_type_node)))
4764 (for froms (BUILT_IN_TRUNCL BUILT_IN_TRUNC
4765 BUILT_IN_FLOORL BUILT_IN_FLOOR
4766 BUILT_IN_CEILL BUILT_IN_CEIL
4767 BUILT_IN_ROUNDL BUILT_IN_ROUND
4768 BUILT_IN_NEARBYINTL BUILT_IN_NEARBYINT
4769 BUILT_IN_RINTL BUILT_IN_RINT)
4770 tos (BUILT_IN_TRUNCF BUILT_IN_TRUNCF
4771 BUILT_IN_FLOORF BUILT_IN_FLOORF
4772 BUILT_IN_CEILF BUILT_IN_CEILF
4773 BUILT_IN_ROUNDF BUILT_IN_ROUNDF
4774 BUILT_IN_NEARBYINTF BUILT_IN_NEARBYINTF
4775 BUILT_IN_RINTF BUILT_IN_RINTF)
4776 /* truncl(extend(x)) and trunc(extend(x)) -> extend(truncf(x)), etc.,
4777 if x is a float. */
4778 (if (optimize && canonicalize_math_p ()
4779 && targetm.libc_has_function (function_c99_misc))
4780 (simplify
4781 (froms (convert float_value_p@0))
4782 (convert (tos @0)))))
4783
4784 (for froms (XFLOORL XCEILL XROUNDL XRINTL)
4785 tos (XFLOOR XCEIL XROUND XRINT)
4786 /* llfloorl(extend(x)) -> llfloor(x), etc., if x is a double. */
4787 (if (optimize && canonicalize_math_p ())
4788 (simplify
4789 (froms (convert double_value_p@0))
4790 (tos @0))))
4791
4792 (for froms (XFLOORL XCEILL XROUNDL XRINTL
4793 XFLOOR XCEIL XROUND XRINT)
4794 tos (XFLOORF XCEILF XROUNDF XRINTF)
4795 /* llfloorl(extend(x)) and llfloor(extend(x)) -> llfloorf(x), etc.,
4796 if x is a float. */
4797 (if (optimize && canonicalize_math_p ())
4798 (simplify
4799 (froms (convert float_value_p@0))
4800 (tos @0))))
4801
4802 (if (canonicalize_math_p ())
4803 /* xfloor(x) -> fix_trunc(x) if x is nonnegative. */
4804 (for floors (IFLOOR LFLOOR LLFLOOR)
4805 (simplify
4806 (floors tree_expr_nonnegative_p@0)
4807 (fix_trunc @0))))
4808
4809 (if (canonicalize_math_p ())
4810 /* xfloor(x) -> fix_trunc(x), etc., if x is integer valued. */
4811 (for fns (IFLOOR LFLOOR LLFLOOR
4812 ICEIL LCEIL LLCEIL
4813 IROUND LROUND LLROUND)
4814 (simplify
4815 (fns integer_valued_real_p@0)
4816 (fix_trunc @0)))
4817 (if (!flag_errno_math)
4818 /* xrint(x) -> fix_trunc(x), etc., if x is integer valued. */
4819 (for rints (IRINT LRINT LLRINT)
4820 (simplify
4821 (rints integer_valued_real_p@0)
4822 (fix_trunc @0)))))
4823
4824 (if (canonicalize_math_p ())
4825 (for ifn (IFLOOR ICEIL IROUND IRINT)
4826 lfn (LFLOOR LCEIL LROUND LRINT)
4827 llfn (LLFLOOR LLCEIL LLROUND LLRINT)
4828 /* Canonicalize iround (x) to lround (x) on ILP32 targets where
4829 sizeof (int) == sizeof (long). */
4830 (if (TYPE_PRECISION (integer_type_node)
4831 == TYPE_PRECISION (long_integer_type_node))
4832 (simplify
4833 (ifn @0)
4834 (lfn:long_integer_type_node @0)))
4835 /* Canonicalize llround (x) to lround (x) on LP64 targets where
4836 sizeof (long long) == sizeof (long). */
4837 (if (TYPE_PRECISION (long_long_integer_type_node)
4838 == TYPE_PRECISION (long_integer_type_node))
4839 (simplify
4840 (llfn @0)
4841 (lfn:long_integer_type_node @0)))))
4842
4843 /* cproj(x) -> x if we're ignoring infinities. */
4844 (simplify
4845 (CPROJ @0)
4846 (if (!HONOR_INFINITIES (type))
4847 @0))
4848
4849 /* If the real part is inf and the imag part is known to be
4850 nonnegative, return (inf + 0i). */
4851 (simplify
4852 (CPROJ (complex REAL_CST@0 tree_expr_nonnegative_p@1))
4853 (if (real_isinf (TREE_REAL_CST_PTR (@0)))
4854 { build_complex_inf (type, false); }))
4855
4856 /* If the imag part is inf, return (inf+I*copysign(0,imag)). */
4857 (simplify
4858 (CPROJ (complex @0 REAL_CST@1))
4859 (if (real_isinf (TREE_REAL_CST_PTR (@1)))
4860 { build_complex_inf (type, TREE_REAL_CST_PTR (@1)->sign); }))
4861
4862 (for pows (POW)
4863 sqrts (SQRT)
4864 cbrts (CBRT)
4865 (simplify
4866 (pows @0 REAL_CST@1)
4867 (with {
4868 const REAL_VALUE_TYPE *value = TREE_REAL_CST_PTR (@1);
4869 REAL_VALUE_TYPE tmp;
4870 }
4871 (switch
4872 /* pow(x,0) -> 1. */
4873 (if (real_equal (value, &dconst0))
4874 { build_real (type, dconst1); })
4875 /* pow(x,1) -> x. */
4876 (if (real_equal (value, &dconst1))
4877 @0)
4878 /* pow(x,-1) -> 1/x. */
4879 (if (real_equal (value, &dconstm1))
4880 (rdiv { build_real (type, dconst1); } @0))
4881 /* pow(x,0.5) -> sqrt(x). */
4882 (if (flag_unsafe_math_optimizations
4883 && canonicalize_math_p ()
4884 && real_equal (value, &dconsthalf))
4885 (sqrts @0))
4886 /* pow(x,1/3) -> cbrt(x). */
4887 (if (flag_unsafe_math_optimizations
4888 && canonicalize_math_p ()
4889 && (tmp = real_value_truncate (TYPE_MODE (type), dconst_third ()),
4890 real_equal (value, &tmp)))
4891 (cbrts @0))))))
4892
4893 /* powi(1,x) -> 1. */
4894 (simplify
4895 (POWI real_onep@0 @1)
4896 @0)
4897
4898 (simplify
4899 (POWI @0 INTEGER_CST@1)
4900 (switch
4901 /* powi(x,0) -> 1. */
4902 (if (wi::to_wide (@1) == 0)
4903 { build_real (type, dconst1); })
4904 /* powi(x,1) -> x. */
4905 (if (wi::to_wide (@1) == 1)
4906 @0)
4907 /* powi(x,-1) -> 1/x. */
4908 (if (wi::to_wide (@1) == -1)
4909 (rdiv { build_real (type, dconst1); } @0))))
4910
4911 /* Narrowing of arithmetic and logical operations.
4912
4913 These are conceptually similar to the transformations performed for
4914 the C/C++ front-ends by shorten_binary_op and shorten_compare. Long
4915 term we want to move all that code out of the front-ends into here. */
4916
4917 /* If we have a narrowing conversion of an arithmetic operation where
4918 both operands are widening conversions from the same type as the outer
4919 narrowing conversion. Then convert the innermost operands to a suitable
4920 unsigned type (to avoid introducing undefined behavior), perform the
4921 operation and convert the result to the desired type. */
4922 (for op (plus minus)
4923 (simplify
4924 (convert (op:s (convert@2 @0) (convert?@3 @1)))
4925 (if (INTEGRAL_TYPE_P (type)
4926 /* We check for type compatibility between @0 and @1 below,
4927 so there's no need to check that @1/@3 are integral types. */
4928 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
4929 && INTEGRAL_TYPE_P (TREE_TYPE (@2))
4930 /* The precision of the type of each operand must match the
4931 precision of the mode of each operand, similarly for the
4932 result. */
4933 && type_has_mode_precision_p (TREE_TYPE (@0))
4934 && type_has_mode_precision_p (TREE_TYPE (@1))
4935 && type_has_mode_precision_p (type)
4936 /* The inner conversion must be a widening conversion. */
4937 && TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (TREE_TYPE (@0))
4938 && types_match (@0, type)
4939 && (types_match (@0, @1)
4940 /* Or the second operand is const integer or converted const
4941 integer from valueize. */
4942 || TREE_CODE (@1) == INTEGER_CST))
4943 (if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
4944 (op @0 (convert @1))
4945 (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
4946 (convert (op (convert:utype @0)
4947 (convert:utype @1))))))))
4948
4949 /* This is another case of narrowing, specifically when there's an outer
4950 BIT_AND_EXPR which masks off bits outside the type of the innermost
4951 operands. Like the previous case we have to convert the operands
4952 to unsigned types to avoid introducing undefined behavior for the
4953 arithmetic operation. */
4954 (for op (minus plus)
4955 (simplify
4956 (bit_and (op:s (convert@2 @0) (convert@3 @1)) INTEGER_CST@4)
4957 (if (INTEGRAL_TYPE_P (type)
4958 /* We check for type compatibility between @0 and @1 below,
4959 so there's no need to check that @1/@3 are integral types. */
4960 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
4961 && INTEGRAL_TYPE_P (TREE_TYPE (@2))
4962 /* The precision of the type of each operand must match the
4963 precision of the mode of each operand, similarly for the
4964 result. */
4965 && type_has_mode_precision_p (TREE_TYPE (@0))
4966 && type_has_mode_precision_p (TREE_TYPE (@1))
4967 && type_has_mode_precision_p (type)
4968 /* The inner conversion must be a widening conversion. */
4969 && TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (TREE_TYPE (@0))
4970 && types_match (@0, @1)
4971 && (tree_int_cst_min_precision (@4, TYPE_SIGN (TREE_TYPE (@0)))
4972 <= TYPE_PRECISION (TREE_TYPE (@0)))
4973 && (wi::to_wide (@4)
4974 & wi::mask (TYPE_PRECISION (TREE_TYPE (@0)),
4975 true, TYPE_PRECISION (type))) == 0)
4976 (if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
4977 (with { tree ntype = TREE_TYPE (@0); }
4978 (convert (bit_and (op @0 @1) (convert:ntype @4))))
4979 (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
4980 (convert (bit_and (op (convert:utype @0) (convert:utype @1))
4981 (convert:utype @4))))))))
4982
4983 /* Transform (@0 < @1 and @0 < @2) to use min,
4984 (@0 > @1 and @0 > @2) to use max */
4985 (for logic (bit_and bit_and bit_and bit_and bit_ior bit_ior bit_ior bit_ior)
4986 op (lt le gt ge lt le gt ge )
4987 ext (min min max max max max min min )
4988 (simplify
4989 (logic (op:cs @0 @1) (op:cs @0 @2))
4990 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
4991 && TREE_CODE (@0) != INTEGER_CST)
4992 (op @0 (ext @1 @2)))))
4993
4994 (simplify
4995 /* signbit(x) -> 0 if x is nonnegative. */
4996 (SIGNBIT tree_expr_nonnegative_p@0)
4997 { integer_zero_node; })
4998
4999 (simplify
5000 /* signbit(x) -> x<0 if x doesn't have signed zeros. */
5001 (SIGNBIT @0)
5002 (if (!HONOR_SIGNED_ZEROS (@0))
5003 (convert (lt @0 { build_real (TREE_TYPE (@0), dconst0); }))))
5004
5005 /* Transform comparisons of the form X +- C1 CMP C2 to X CMP C2 -+ C1. */
5006 (for cmp (eq ne)
5007 (for op (plus minus)
5008 rop (minus plus)
5009 (simplify
5010 (cmp (op@3 @0 INTEGER_CST@1) INTEGER_CST@2)
5011 (if (!TREE_OVERFLOW (@1) && !TREE_OVERFLOW (@2)
5012 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@0))
5013 && !TYPE_OVERFLOW_TRAPS (TREE_TYPE (@0))
5014 && !TYPE_SATURATING (TREE_TYPE (@0)))
5015 (with { tree res = int_const_binop (rop, @2, @1); }
5016 (if (TREE_OVERFLOW (res)
5017 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
5018 { constant_boolean_node (cmp == NE_EXPR, type); }
5019 (if (single_use (@3))
5020 (cmp @0 { TREE_OVERFLOW (res)
5021 ? drop_tree_overflow (res) : res; }))))))))
5022 (for cmp (lt le gt ge)
5023 (for op (plus minus)
5024 rop (minus plus)
5025 (simplify
5026 (cmp (op@3 @0 INTEGER_CST@1) INTEGER_CST@2)
5027 (if (!TREE_OVERFLOW (@1) && !TREE_OVERFLOW (@2)
5028 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
5029 (with { tree res = int_const_binop (rop, @2, @1); }
5030 (if (TREE_OVERFLOW (res))
5031 {
5032 fold_overflow_warning (("assuming signed overflow does not occur "
5033 "when simplifying conditional to constant"),
5034 WARN_STRICT_OVERFLOW_CONDITIONAL);
5035 bool less = cmp == LE_EXPR || cmp == LT_EXPR;
5036 /* wi::ges_p (@2, 0) should be sufficient for a signed type. */
5037 bool ovf_high = wi::lt_p (wi::to_wide (@1), 0,
5038 TYPE_SIGN (TREE_TYPE (@1)))
5039 != (op == MINUS_EXPR);
5040 constant_boolean_node (less == ovf_high, type);
5041 }
5042 (if (single_use (@3))
5043 (with
5044 {
5045 fold_overflow_warning (("assuming signed overflow does not occur "
5046 "when changing X +- C1 cmp C2 to "
5047 "X cmp C2 -+ C1"),
5048 WARN_STRICT_OVERFLOW_COMPARISON);
5049 }
5050 (cmp @0 { res; })))))))))
5051
5052 /* Canonicalizations of BIT_FIELD_REFs. */
5053
5054 (simplify
5055 (BIT_FIELD_REF (BIT_FIELD_REF @0 @1 @2) @3 @4)
5056 (BIT_FIELD_REF @0 @3 { const_binop (PLUS_EXPR, bitsizetype, @2, @4); }))
5057
5058 (simplify
5059 (BIT_FIELD_REF (view_convert @0) @1 @2)
5060 (BIT_FIELD_REF @0 @1 @2))
5061
5062 (simplify
5063 (BIT_FIELD_REF @0 @1 integer_zerop)
5064 (if (tree_int_cst_equal (@1, TYPE_SIZE (TREE_TYPE (@0))))
5065 (view_convert @0)))
5066
5067 (simplify
5068 (BIT_FIELD_REF @0 @1 @2)
5069 (switch
5070 (if (TREE_CODE (TREE_TYPE (@0)) == COMPLEX_TYPE
5071 && tree_int_cst_equal (@1, TYPE_SIZE (TREE_TYPE (TREE_TYPE (@0)))))
5072 (switch
5073 (if (integer_zerop (@2))
5074 (view_convert (realpart @0)))
5075 (if (tree_int_cst_equal (@2, TYPE_SIZE (TREE_TYPE (TREE_TYPE (@0)))))
5076 (view_convert (imagpart @0)))))
5077 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
5078 && INTEGRAL_TYPE_P (type)
5079 /* On GIMPLE this should only apply to register arguments. */
5080 && (! GIMPLE || is_gimple_reg (@0))
5081 /* A bit-field-ref that referenced the full argument can be stripped. */
5082 && ((compare_tree_int (@1, TYPE_PRECISION (TREE_TYPE (@0))) == 0
5083 && integer_zerop (@2))
5084 /* Low-parts can be reduced to integral conversions.
5085 ??? The following doesn't work for PDP endian. */
5086 || (BYTES_BIG_ENDIAN == WORDS_BIG_ENDIAN
5087 /* Don't even think about BITS_BIG_ENDIAN. */
5088 && TYPE_PRECISION (TREE_TYPE (@0)) % BITS_PER_UNIT == 0
5089 && TYPE_PRECISION (type) % BITS_PER_UNIT == 0
5090 && compare_tree_int (@2, (BYTES_BIG_ENDIAN
5091 ? (TYPE_PRECISION (TREE_TYPE (@0))
5092 - TYPE_PRECISION (type))
5093 : 0)) == 0)))
5094 (convert @0))))
5095
5096 /* Simplify vector extracts. */
5097
5098 (simplify
5099 (BIT_FIELD_REF CONSTRUCTOR@0 @1 @2)
5100 (if (VECTOR_TYPE_P (TREE_TYPE (@0))
5101 && (types_match (type, TREE_TYPE (TREE_TYPE (@0)))
5102 || (VECTOR_TYPE_P (type)
5103 && types_match (TREE_TYPE (type), TREE_TYPE (TREE_TYPE (@0))))))
5104 (with
5105 {
5106 tree ctor = (TREE_CODE (@0) == SSA_NAME
5107 ? gimple_assign_rhs1 (SSA_NAME_DEF_STMT (@0)) : @0);
5108 tree eltype = TREE_TYPE (TREE_TYPE (ctor));
5109 unsigned HOST_WIDE_INT width = tree_to_uhwi (TYPE_SIZE (eltype));
5110 unsigned HOST_WIDE_INT n = tree_to_uhwi (@1);
5111 unsigned HOST_WIDE_INT idx = tree_to_uhwi (@2);
5112 }
5113 (if (n != 0
5114 && (idx % width) == 0
5115 && (n % width) == 0
5116 && known_le ((idx + n) / width,
5117 TYPE_VECTOR_SUBPARTS (TREE_TYPE (ctor))))
5118 (with
5119 {
5120 idx = idx / width;
5121 n = n / width;
5122 /* Constructor elements can be subvectors. */
5123 poly_uint64 k = 1;
5124 if (CONSTRUCTOR_NELTS (ctor) != 0)
5125 {
5126 tree cons_elem = TREE_TYPE (CONSTRUCTOR_ELT (ctor, 0)->value);
5127 if (TREE_CODE (cons_elem) == VECTOR_TYPE)
5128 k = TYPE_VECTOR_SUBPARTS (cons_elem);
5129 }
5130 unsigned HOST_WIDE_INT elt, count, const_k;
5131 }
5132 (switch
5133 /* We keep an exact subset of the constructor elements. */
5134 (if (multiple_p (idx, k, &elt) && multiple_p (n, k, &count))
5135 (if (CONSTRUCTOR_NELTS (ctor) == 0)
5136 { build_constructor (type, NULL); }
5137 (if (count == 1)
5138 (if (elt < CONSTRUCTOR_NELTS (ctor))
5139 (view_convert { CONSTRUCTOR_ELT (ctor, elt)->value; })
5140 { build_zero_cst (type); })
5141 {
5142 vec<constructor_elt, va_gc> *vals;
5143 vec_alloc (vals, count);
5144 for (unsigned i = 0;
5145 i < count && elt + i < CONSTRUCTOR_NELTS (ctor); ++i)
5146 CONSTRUCTOR_APPEND_ELT (vals, NULL_TREE,
5147 CONSTRUCTOR_ELT (ctor, elt + i)->value);
5148 build_constructor (type, vals);
5149 })))
5150 /* The bitfield references a single constructor element. */
5151 (if (k.is_constant (&const_k)
5152 && idx + n <= (idx / const_k + 1) * const_k)
5153 (switch
5154 (if (CONSTRUCTOR_NELTS (ctor) <= idx / const_k)
5155 { build_zero_cst (type); })
5156 (if (n == const_k)
5157 (view_convert { CONSTRUCTOR_ELT (ctor, idx / const_k)->value; }))
5158 (BIT_FIELD_REF { CONSTRUCTOR_ELT (ctor, idx / const_k)->value; }
5159 @1 { bitsize_int ((idx % const_k) * width); })))))))))
5160
5161 /* Simplify a bit extraction from a bit insertion for the cases with
5162 the inserted element fully covering the extraction or the insertion
5163 not touching the extraction. */
5164 (simplify
5165 (BIT_FIELD_REF (bit_insert @0 @1 @ipos) @rsize @rpos)
5166 (with
5167 {
5168 unsigned HOST_WIDE_INT isize;
5169 if (INTEGRAL_TYPE_P (TREE_TYPE (@1)))
5170 isize = TYPE_PRECISION (TREE_TYPE (@1));
5171 else
5172 isize = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (@1)));
5173 }
5174 (switch
5175 (if (wi::leu_p (wi::to_wide (@ipos), wi::to_wide (@rpos))
5176 && wi::leu_p (wi::to_wide (@rpos) + wi::to_wide (@rsize),
5177 wi::to_wide (@ipos) + isize))
5178 (BIT_FIELD_REF @1 @rsize { wide_int_to_tree (bitsizetype,
5179 wi::to_wide (@rpos)
5180 - wi::to_wide (@ipos)); }))
5181 (if (wi::geu_p (wi::to_wide (@ipos),
5182 wi::to_wide (@rpos) + wi::to_wide (@rsize))
5183 || wi::geu_p (wi::to_wide (@rpos),
5184 wi::to_wide (@ipos) + isize))
5185 (BIT_FIELD_REF @0 @rsize @rpos)))))
5186
5187 (if (canonicalize_math_after_vectorization_p ())
5188 (for fmas (FMA)
5189 (simplify
5190 (fmas:c (negate @0) @1 @2)
5191 (IFN_FNMA @0 @1 @2))
5192 (simplify
5193 (fmas @0 @1 (negate @2))
5194 (IFN_FMS @0 @1 @2))
5195 (simplify
5196 (fmas:c (negate @0) @1 (negate @2))
5197 (IFN_FNMS @0 @1 @2))
5198 (simplify
5199 (negate (fmas@3 @0 @1 @2))
5200 (if (single_use (@3))
5201 (IFN_FNMS @0 @1 @2))))
5202
5203 (simplify
5204 (IFN_FMS:c (negate @0) @1 @2)
5205 (IFN_FNMS @0 @1 @2))
5206 (simplify
5207 (IFN_FMS @0 @1 (negate @2))
5208 (IFN_FMA @0 @1 @2))
5209 (simplify
5210 (IFN_FMS:c (negate @0) @1 (negate @2))
5211 (IFN_FNMA @0 @1 @2))
5212 (simplify
5213 (negate (IFN_FMS@3 @0 @1 @2))
5214 (if (single_use (@3))
5215 (IFN_FNMA @0 @1 @2)))
5216
5217 (simplify
5218 (IFN_FNMA:c (negate @0) @1 @2)
5219 (IFN_FMA @0 @1 @2))
5220 (simplify
5221 (IFN_FNMA @0 @1 (negate @2))
5222 (IFN_FNMS @0 @1 @2))
5223 (simplify
5224 (IFN_FNMA:c (negate @0) @1 (negate @2))
5225 (IFN_FMS @0 @1 @2))
5226 (simplify
5227 (negate (IFN_FNMA@3 @0 @1 @2))
5228 (if (single_use (@3))
5229 (IFN_FMS @0 @1 @2)))
5230
5231 (simplify
5232 (IFN_FNMS:c (negate @0) @1 @2)
5233 (IFN_FMS @0 @1 @2))
5234 (simplify
5235 (IFN_FNMS @0 @1 (negate @2))
5236 (IFN_FNMA @0 @1 @2))
5237 (simplify
5238 (IFN_FNMS:c (negate @0) @1 (negate @2))
5239 (IFN_FMA @0 @1 @2))
5240 (simplify
5241 (negate (IFN_FNMS@3 @0 @1 @2))
5242 (if (single_use (@3))
5243 (IFN_FMA @0 @1 @2))))
5244
5245 /* POPCOUNT simplifications. */
5246 (for popcount (BUILT_IN_POPCOUNT BUILT_IN_POPCOUNTL BUILT_IN_POPCOUNTLL
5247 BUILT_IN_POPCOUNTIMAX)
5248 /* popcount(X&1) is nop_expr(X&1). */
5249 (simplify
5250 (popcount @0)
5251 (if (tree_nonzero_bits (@0) == 1)
5252 (convert @0)))
5253 /* popcount(X) + popcount(Y) is popcount(X|Y) when X&Y must be zero. */
5254 (simplify
5255 (plus (popcount:s @0) (popcount:s @1))
5256 (if (wi::bit_and (tree_nonzero_bits (@0), tree_nonzero_bits (@1)) == 0)
5257 (popcount (bit_ior @0 @1))))
5258 /* popcount(X) == 0 is X == 0, and related (in)equalities. */
5259 (for cmp (le eq ne gt)
5260 rep (eq eq ne ne)
5261 (simplify
5262 (cmp (popcount @0) integer_zerop)
5263 (rep @0 { build_zero_cst (TREE_TYPE (@0)); }))))
5264
5265 /* Simplify:
5266
5267 a = a1 op a2
5268 r = c ? a : b;
5269
5270 to:
5271
5272 r = c ? a1 op a2 : b;
5273
5274 if the target can do it in one go. This makes the operation conditional
5275 on c, so could drop potentially-trapping arithmetic, but that's a valid
5276 simplification if the result of the operation isn't needed.
5277
5278 Avoid speculatively generating a stand-alone vector comparison
5279 on targets that might not support them. Any target implementing
5280 conditional internal functions must support the same comparisons
5281 inside and outside a VEC_COND_EXPR. */
5282
5283 #if GIMPLE
5284 (for uncond_op (UNCOND_BINARY)
5285 cond_op (COND_BINARY)
5286 (simplify
5287 (vec_cond @0 (view_convert? (uncond_op@4 @1 @2)) @3)
5288 (with { tree op_type = TREE_TYPE (@4); }
5289 (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
5290 && element_precision (type) == element_precision (op_type))
5291 (view_convert (cond_op @0 @1 @2 (view_convert:op_type @3))))))
5292 (simplify
5293 (vec_cond @0 @1 (view_convert? (uncond_op@4 @2 @3)))
5294 (with { tree op_type = TREE_TYPE (@4); }
5295 (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
5296 && element_precision (type) == element_precision (op_type))
5297 (view_convert (cond_op (bit_not @0) @2 @3 (view_convert:op_type @1)))))))
5298
5299 /* Same for ternary operations. */
5300 (for uncond_op (UNCOND_TERNARY)
5301 cond_op (COND_TERNARY)
5302 (simplify
5303 (vec_cond @0 (view_convert? (uncond_op@5 @1 @2 @3)) @4)
5304 (with { tree op_type = TREE_TYPE (@5); }
5305 (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
5306 && element_precision (type) == element_precision (op_type))
5307 (view_convert (cond_op @0 @1 @2 @3 (view_convert:op_type @4))))))
5308 (simplify
5309 (vec_cond @0 @1 (view_convert? (uncond_op@5 @2 @3 @4)))
5310 (with { tree op_type = TREE_TYPE (@5); }
5311 (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
5312 && element_precision (type) == element_precision (op_type))
5313 (view_convert (cond_op (bit_not @0) @2 @3 @4
5314 (view_convert:op_type @1)))))))
5315 #endif
5316
5317 /* Detect cases in which a VEC_COND_EXPR effectively replaces the
5318 "else" value of an IFN_COND_*. */
5319 (for cond_op (COND_BINARY)
5320 (simplify
5321 (vec_cond @0 (view_convert? (cond_op @0 @1 @2 @3)) @4)
5322 (with { tree op_type = TREE_TYPE (@3); }
5323 (if (element_precision (type) == element_precision (op_type))
5324 (view_convert (cond_op @0 @1 @2 (view_convert:op_type @4))))))
5325 (simplify
5326 (vec_cond @0 @1 (view_convert? (cond_op @2 @3 @4 @5)))
5327 (with { tree op_type = TREE_TYPE (@5); }
5328 (if (inverse_conditions_p (@0, @2)
5329 && element_precision (type) == element_precision (op_type))
5330 (view_convert (cond_op @2 @3 @4 (view_convert:op_type @1)))))))
5331
5332 /* Same for ternary operations. */
5333 (for cond_op (COND_TERNARY)
5334 (simplify
5335 (vec_cond @0 (view_convert? (cond_op @0 @1 @2 @3 @4)) @5)
5336 (with { tree op_type = TREE_TYPE (@4); }
5337 (if (element_precision (type) == element_precision (op_type))
5338 (view_convert (cond_op @0 @1 @2 @3 (view_convert:op_type @5))))))
5339 (simplify
5340 (vec_cond @0 @1 (view_convert? (cond_op @2 @3 @4 @5 @6)))
5341 (with { tree op_type = TREE_TYPE (@6); }
5342 (if (inverse_conditions_p (@0, @2)
5343 && element_precision (type) == element_precision (op_type))
5344 (view_convert (cond_op @2 @3 @4 @5 (view_convert:op_type @1)))))))
5345
5346 /* For pointers @0 and @2 and nonnegative constant offset @1, look for
5347 expressions like:
5348
5349 A: (@0 + @1 < @2) | (@2 + @1 < @0)
5350 B: (@0 + @1 <= @2) | (@2 + @1 <= @0)
5351
5352 If pointers are known not to wrap, B checks whether @1 bytes starting
5353 at @0 and @2 do not overlap, while A tests the same thing for @1 + 1
5354 bytes. A is more efficiently tested as:
5355
5356 A: (sizetype) (@0 + @1 - @2) > @1 * 2
5357
5358 The equivalent expression for B is given by replacing @1 with @1 - 1:
5359
5360 B: (sizetype) (@0 + (@1 - 1) - @2) > (@1 - 1) * 2
5361
5362 @0 and @2 can be swapped in both expressions without changing the result.
5363
5364 The folds rely on sizetype's being unsigned (which is always true)
5365 and on its being the same width as the pointer (which we have to check).
5366
5367 The fold replaces two pointer_plus expressions, two comparisons and
5368 an IOR with a pointer_plus, a pointer_diff, and a comparison, so in
5369 the best case it's a saving of two operations. The A fold retains one
5370 of the original pointer_pluses, so is a win even if both pointer_pluses
5371 are used elsewhere. The B fold is a wash if both pointer_pluses are
5372 used elsewhere, since all we end up doing is replacing a comparison with
5373 a pointer_plus. We do still apply the fold under those circumstances
5374 though, in case applying it to other conditions eventually makes one of the
5375 pointer_pluses dead. */
5376 (for ior (truth_orif truth_or bit_ior)
5377 (for cmp (le lt)
5378 (simplify
5379 (ior (cmp:cs (pointer_plus@3 @0 INTEGER_CST@1) @2)
5380 (cmp:cs (pointer_plus@4 @2 @1) @0))
5381 (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
5382 && TYPE_OVERFLOW_WRAPS (sizetype)
5383 && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (sizetype))
5384 /* Calculate the rhs constant. */
5385 (with { offset_int off = wi::to_offset (@1) - (cmp == LE_EXPR ? 1 : 0);
5386 offset_int rhs = off * 2; }
5387 /* Always fails for negative values. */
5388 (if (wi::min_precision (rhs, UNSIGNED) <= TYPE_PRECISION (sizetype))
5389 /* Since the order of @0 and @2 doesn't matter, let tree_swap_operands_p
5390 pick a canonical order. This increases the chances of using the
5391 same pointer_plus in multiple checks. */
5392 (with { bool swap_p = tree_swap_operands_p (@0, @2);
5393 tree rhs_tree = wide_int_to_tree (sizetype, rhs); }
5394 (if (cmp == LT_EXPR)
5395 (gt (convert:sizetype
5396 (pointer_diff:ssizetype { swap_p ? @4 : @3; }
5397 { swap_p ? @0 : @2; }))
5398 { rhs_tree; })
5399 (gt (convert:sizetype
5400 (pointer_diff:ssizetype
5401 (pointer_plus { swap_p ? @2 : @0; }
5402 { wide_int_to_tree (sizetype, off); })
5403 { swap_p ? @0 : @2; }))
5404 { rhs_tree; })))))))))
5405
5406 /* Fold REDUC (@0 & @1) -> @0[I] & @1[I] if element I is the only nonzero
5407 element of @1. */
5408 (for reduc (IFN_REDUC_PLUS IFN_REDUC_IOR IFN_REDUC_XOR)
5409 (simplify (reduc (view_convert? (bit_and @0 VECTOR_CST@1)))
5410 (with { int i = single_nonzero_element (@1); }
5411 (if (i >= 0)
5412 (with { tree elt = vector_cst_elt (@1, i);
5413 tree elt_type = TREE_TYPE (elt);
5414 unsigned int elt_bits = tree_to_uhwi (TYPE_SIZE (elt_type));
5415 tree size = bitsize_int (elt_bits);
5416 tree pos = bitsize_int (elt_bits * i); }
5417 (view_convert
5418 (bit_and:elt_type
5419 (BIT_FIELD_REF:elt_type @0 { size; } { pos; })
5420 { elt; })))))))
5421
5422 (simplify
5423 (vec_perm @0 @1 VECTOR_CST@2)
5424 (with
5425 {
5426 tree op0 = @0, op1 = @1, op2 = @2;
5427
5428 /* Build a vector of integers from the tree mask. */
5429 vec_perm_builder builder;
5430 if (!tree_to_vec_perm_builder (&builder, op2))
5431 return NULL_TREE;
5432
5433 /* Create a vec_perm_indices for the integer vector. */
5434 poly_uint64 nelts = TYPE_VECTOR_SUBPARTS (type);
5435 bool single_arg = (op0 == op1);
5436 vec_perm_indices sel (builder, single_arg ? 1 : 2, nelts);
5437 }
5438 (if (sel.series_p (0, 1, 0, 1))
5439 { op0; }
5440 (if (sel.series_p (0, 1, nelts, 1))
5441 { op1; }
5442 (with
5443 {
5444 if (!single_arg)
5445 {
5446 if (sel.all_from_input_p (0))
5447 op1 = op0;
5448 else if (sel.all_from_input_p (1))
5449 {
5450 op0 = op1;
5451 sel.rotate_inputs (1);
5452 }
5453 else if (known_ge (poly_uint64 (sel[0]), nelts))
5454 {
5455 std::swap (op0, op1);
5456 sel.rotate_inputs (1);
5457 }
5458 }
5459 gassign *def;
5460 tree cop0 = op0, cop1 = op1;
5461 if (TREE_CODE (op0) == SSA_NAME
5462 && (def = dyn_cast <gassign *> (SSA_NAME_DEF_STMT (op0)))
5463 && gimple_assign_rhs_code (def) == CONSTRUCTOR)
5464 cop0 = gimple_assign_rhs1 (def);
5465 if (TREE_CODE (op1) == SSA_NAME
5466 && (def = dyn_cast <gassign *> (SSA_NAME_DEF_STMT (op1)))
5467 && gimple_assign_rhs_code (def) == CONSTRUCTOR)
5468 cop1 = gimple_assign_rhs1 (def);
5469
5470 tree t;
5471 }
5472 (if ((TREE_CODE (cop0) == VECTOR_CST
5473 || TREE_CODE (cop0) == CONSTRUCTOR)
5474 && (TREE_CODE (cop1) == VECTOR_CST
5475 || TREE_CODE (cop1) == CONSTRUCTOR)
5476 && (t = fold_vec_perm (type, cop0, cop1, sel)))
5477 { t; }
5478 (with
5479 {
5480 bool changed = (op0 == op1 && !single_arg);
5481 tree ins = NULL_TREE;
5482 unsigned at = 0;
5483
5484 /* See if the permutation is performing a single element
5485 insert from a CONSTRUCTOR or constant and use a BIT_INSERT_EXPR
5486 in that case. But only if the vector mode is supported,
5487 otherwise this is invalid GIMPLE. */
5488 if (TYPE_MODE (type) != BLKmode
5489 && (TREE_CODE (cop0) == VECTOR_CST
5490 || TREE_CODE (cop0) == CONSTRUCTOR
5491 || TREE_CODE (cop1) == VECTOR_CST
5492 || TREE_CODE (cop1) == CONSTRUCTOR))
5493 {
5494 if (sel.series_p (1, 1, nelts + 1, 1))
5495 {
5496 /* After canonicalizing the first elt to come from the
5497 first vector we only can insert the first elt from
5498 the first vector. */
5499 at = 0;
5500 if ((ins = fold_read_from_vector (cop0, 0)))
5501 op0 = op1;
5502 }
5503 else
5504 {
5505 unsigned int encoded_nelts = sel.encoding ().encoded_nelts ();
5506 for (at = 0; at < encoded_nelts; ++at)
5507 if (maybe_ne (sel[at], at))
5508 break;
5509 if (at < encoded_nelts && sel.series_p (at + 1, 1, at + 1, 1))
5510 {
5511 if (known_lt (at, nelts))
5512 ins = fold_read_from_vector (cop0, sel[at]);
5513 else
5514 ins = fold_read_from_vector (cop1, sel[at] - nelts);
5515 }
5516 }
5517 }
5518
5519 /* Generate a canonical form of the selector. */
5520 if (!ins && sel.encoding () != builder)
5521 {
5522 /* Some targets are deficient and fail to expand a single
5523 argument permutation while still allowing an equivalent
5524 2-argument version. */
5525 tree oldop2 = op2;
5526 if (sel.ninputs () == 2
5527 || can_vec_perm_const_p (TYPE_MODE (type), sel, false))
5528 op2 = vec_perm_indices_to_tree (TREE_TYPE (op2), sel);
5529 else
5530 {
5531 vec_perm_indices sel2 (builder, 2, nelts);
5532 if (can_vec_perm_const_p (TYPE_MODE (type), sel2, false))
5533 op2 = vec_perm_indices_to_tree (TREE_TYPE (op2), sel2);
5534 else
5535 /* Not directly supported with either encoding,
5536 so use the preferred form. */
5537 op2 = vec_perm_indices_to_tree (TREE_TYPE (op2), sel);
5538 }
5539 if (!operand_equal_p (op2, oldop2, 0))
5540 changed = true;
5541 }
5542 }
5543 (if (ins)
5544 (bit_insert { op0; } { ins; }
5545 { bitsize_int (at * tree_to_uhwi (TYPE_SIZE (TREE_TYPE (type)))); })
5546 (if (changed)
5547 (vec_perm { op0; } { op1; } { op2; }))))))))))