]>
Commit | Line | Data |
---|---|---|
3d2cf79f | 1 | /* Match-and-simplify patterns for shared GENERIC and GIMPLE folding. |
e53b6e56 ML |
2 | This file is consumed by genmatch which produces gimple-match.cc |
3 | and generic-match.cc from it. | |
3d2cf79f | 4 | |
aeee4812 | 5 | Copyright (C) 2014-2023 Free Software Foundation, Inc. |
3d2cf79f RB |
6 | Contributed by Richard Biener <rguenther@suse.de> |
7 | and Prathamesh Kulkarni <bilbotheelffriend@gmail.com> | |
8 | ||
9 | This file is part of GCC. | |
10 | ||
11 | GCC is free software; you can redistribute it and/or modify it under | |
12 | the terms of the GNU General Public License as published by the Free | |
13 | Software Foundation; either version 3, or (at your option) any later | |
14 | version. | |
15 | ||
16 | GCC is distributed in the hope that it will be useful, but WITHOUT ANY | |
17 | WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
18 | FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
19 | for more details. | |
20 | ||
21 | You should have received a copy of the GNU General Public License | |
22 | along with GCC; see the file COPYING3. If not see | |
23 | <http://www.gnu.org/licenses/>. */ | |
24 | ||
25 | ||
26 | /* Generic tree predicates we inherit. */ | |
27 | (define_predicates | |
cc7b5acf | 28 | integer_onep integer_zerop integer_all_onesp integer_minus_onep |
53a19317 | 29 | integer_each_onep integer_truep integer_nonzerop |
cc7b5acf | 30 | real_zerop real_onep real_minus_onep |
b0eb889b | 31 | zerop |
46c66a46 | 32 | initializer_each_zero_or_onep |
f3582e54 | 33 | CONSTANT_CLASS_P |
887ab609 | 34 | tree_expr_nonnegative_p |
e36c1cfe | 35 | tree_expr_nonzero_p |
67dbe582 | 36 | integer_valued_real_p |
53a19317 | 37 | integer_pow2p |
f06e47d7 | 38 | uniform_integer_cst_p |
21caa1a2 | 39 | HONOR_NANS |
d70720c2 | 40 | uniform_vector_p |
0888d6bb TC |
41 | expand_vec_cmp_expr_p |
42 | bitmask_inv_cst_vector_p) | |
e0ee10ed | 43 | |
f84e7fd6 RB |
44 | /* Operator lists. */ |
45 | (define_operator_list tcc_comparison | |
46 | lt le eq ne ge gt unordered ordered unlt unle ungt unge uneq ltgt) | |
47 | (define_operator_list inverted_tcc_comparison | |
48 | ge gt ne eq lt le ordered unordered ge gt le lt ltgt uneq) | |
49 | (define_operator_list inverted_tcc_comparison_with_nans | |
50 | unge ungt ne eq unlt unle ordered unordered ge gt le lt ltgt uneq) | |
534bd33b MG |
51 | (define_operator_list swapped_tcc_comparison |
52 | gt ge eq ne le lt unordered ordered ungt unge unlt unle uneq ltgt) | |
07cdc2b8 RB |
53 | (define_operator_list simple_comparison lt le eq ne ge gt) |
54 | (define_operator_list swapped_simple_comparison gt ge eq ne le lt) | |
55 | ||
b1dc4a20 | 56 | #include "cfn-operators.pd" |
257aecb4 | 57 | |
543a9bcd RS |
58 | /* Define operand lists for math rounding functions {,i,l,ll}FN, |
59 | where the versions prefixed with "i" return an int, those prefixed with | |
60 | "l" return a long and those prefixed with "ll" return a long long. | |
61 | ||
62 | Also define operand lists: | |
63 | ||
64 | X<FN>F for all float functions, in the order i, l, ll | |
65 | X<FN> for all double functions, in the same order | |
66 | X<FN>L for all long double functions, in the same order. */ | |
67 | #define DEFINE_INT_AND_FLOAT_ROUND_FN(FN) \ | |
543a9bcd RS |
68 | (define_operator_list X##FN##F BUILT_IN_I##FN##F \ |
69 | BUILT_IN_L##FN##F \ | |
70 | BUILT_IN_LL##FN##F) \ | |
71 | (define_operator_list X##FN BUILT_IN_I##FN \ | |
72 | BUILT_IN_L##FN \ | |
73 | BUILT_IN_LL##FN) \ | |
74 | (define_operator_list X##FN##L BUILT_IN_I##FN##L \ | |
75 | BUILT_IN_L##FN##L \ | |
76 | BUILT_IN_LL##FN##L) | |
77 | ||
543a9bcd RS |
78 | DEFINE_INT_AND_FLOAT_ROUND_FN (FLOOR) |
79 | DEFINE_INT_AND_FLOAT_ROUND_FN (CEIL) | |
80 | DEFINE_INT_AND_FLOAT_ROUND_FN (ROUND) | |
81 | DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) | |
0d2b3bca | 82 | |
20dcda98 | 83 | /* Unary operations and their associated IFN_COND_* function. */ |
84 | (define_operator_list UNCOND_UNARY | |
85 | negate) | |
86 | (define_operator_list COND_UNARY | |
87 | IFN_COND_NEG) | |
88 | ||
0d2b3bca RS |
89 | /* Binary operations and their associated IFN_COND_* function. */ |
90 | (define_operator_list UNCOND_BINARY | |
91 | plus minus | |
6c4fd4a9 | 92 | mult trunc_div trunc_mod rdiv |
0d2b3bca | 93 | min max |
70613000 | 94 | IFN_FMIN IFN_FMAX |
20103c0e RS |
95 | bit_and bit_ior bit_xor |
96 | lshift rshift) | |
0d2b3bca RS |
97 | (define_operator_list COND_BINARY |
98 | IFN_COND_ADD IFN_COND_SUB | |
6c4fd4a9 | 99 | IFN_COND_MUL IFN_COND_DIV IFN_COND_MOD IFN_COND_RDIV |
0d2b3bca | 100 | IFN_COND_MIN IFN_COND_MAX |
70613000 | 101 | IFN_COND_FMIN IFN_COND_FMAX |
20103c0e RS |
102 | IFN_COND_AND IFN_COND_IOR IFN_COND_XOR |
103 | IFN_COND_SHL IFN_COND_SHR) | |
b41d1f6e RS |
104 | |
105 | /* Same for ternary operations. */ | |
106 | (define_operator_list UNCOND_TERNARY | |
107 | IFN_FMA IFN_FMS IFN_FNMA IFN_FNMS) | |
108 | (define_operator_list COND_TERNARY | |
109 | IFN_COND_FMA IFN_COND_FMS IFN_COND_FNMA IFN_COND_FNMS) | |
03cc70b5 | 110 | |
fb161782 | 111 | /* __atomic_fetch_or_*, __atomic_fetch_xor_*, __atomic_xor_fetch_* */ |
112 | (define_operator_list ATOMIC_FETCH_OR_XOR_N | |
113 | BUILT_IN_ATOMIC_FETCH_OR_1 BUILT_IN_ATOMIC_FETCH_OR_2 | |
114 | BUILT_IN_ATOMIC_FETCH_OR_4 BUILT_IN_ATOMIC_FETCH_OR_8 | |
115 | BUILT_IN_ATOMIC_FETCH_OR_16 | |
116 | BUILT_IN_ATOMIC_FETCH_XOR_1 BUILT_IN_ATOMIC_FETCH_XOR_2 | |
117 | BUILT_IN_ATOMIC_FETCH_XOR_4 BUILT_IN_ATOMIC_FETCH_XOR_8 | |
118 | BUILT_IN_ATOMIC_FETCH_XOR_16 | |
119 | BUILT_IN_ATOMIC_XOR_FETCH_1 BUILT_IN_ATOMIC_XOR_FETCH_2 | |
120 | BUILT_IN_ATOMIC_XOR_FETCH_4 BUILT_IN_ATOMIC_XOR_FETCH_8 | |
121 | BUILT_IN_ATOMIC_XOR_FETCH_16) | |
122 | /* __sync_fetch_and_or_*, __sync_fetch_and_xor_*, __sync_xor_and_fetch_* */ | |
123 | (define_operator_list SYNC_FETCH_OR_XOR_N | |
124 | BUILT_IN_SYNC_FETCH_AND_OR_1 BUILT_IN_SYNC_FETCH_AND_OR_2 | |
125 | BUILT_IN_SYNC_FETCH_AND_OR_4 BUILT_IN_SYNC_FETCH_AND_OR_8 | |
126 | BUILT_IN_SYNC_FETCH_AND_OR_16 | |
127 | BUILT_IN_SYNC_FETCH_AND_XOR_1 BUILT_IN_SYNC_FETCH_AND_XOR_2 | |
128 | BUILT_IN_SYNC_FETCH_AND_XOR_4 BUILT_IN_SYNC_FETCH_AND_XOR_8 | |
129 | BUILT_IN_SYNC_FETCH_AND_XOR_16 | |
130 | BUILT_IN_SYNC_XOR_AND_FETCH_1 BUILT_IN_SYNC_XOR_AND_FETCH_2 | |
131 | BUILT_IN_SYNC_XOR_AND_FETCH_4 BUILT_IN_SYNC_XOR_AND_FETCH_8 | |
132 | BUILT_IN_SYNC_XOR_AND_FETCH_16) | |
133 | /* __atomic_fetch_and_*. */ | |
134 | (define_operator_list ATOMIC_FETCH_AND_N | |
135 | BUILT_IN_ATOMIC_FETCH_AND_1 BUILT_IN_ATOMIC_FETCH_AND_2 | |
136 | BUILT_IN_ATOMIC_FETCH_AND_4 BUILT_IN_ATOMIC_FETCH_AND_8 | |
137 | BUILT_IN_ATOMIC_FETCH_AND_16) | |
138 | /* __sync_fetch_and_and_*. */ | |
139 | (define_operator_list SYNC_FETCH_AND_AND_N | |
140 | BUILT_IN_SYNC_FETCH_AND_AND_1 BUILT_IN_SYNC_FETCH_AND_AND_2 | |
141 | BUILT_IN_SYNC_FETCH_AND_AND_4 BUILT_IN_SYNC_FETCH_AND_AND_8 | |
142 | BUILT_IN_SYNC_FETCH_AND_AND_16) | |
143 | ||
e150da38 RB |
144 | /* With nop_convert? combine convert? and view_convert? in one pattern |
145 | plus conditionalize on tree_nop_conversion_p conversions. */ | |
ed73f46f MG |
146 | (match (nop_convert @0) |
147 | (convert @0) | |
148 | (if (tree_nop_conversion_p (type, TREE_TYPE (@0))))) | |
149 | (match (nop_convert @0) | |
150 | (view_convert @0) | |
151 | (if (VECTOR_TYPE_P (type) && VECTOR_TYPE_P (TREE_TYPE (@0)) | |
928686b1 RS |
152 | && known_eq (TYPE_VECTOR_SUBPARTS (type), |
153 | TYPE_VECTOR_SUBPARTS (TREE_TYPE (@0))) | |
ed73f46f | 154 | && tree_nop_conversion_p (TREE_TYPE (type), TREE_TYPE (TREE_TYPE (@0)))))) |
f84e7fd6 | 155 | |
e197e64e KV |
156 | /* Transform likes of (char) ABS_EXPR <(int) x> into (char) ABSU_EXPR <x> |
157 | ABSU_EXPR returns unsigned absolute value of the operand and the operand | |
158 | of the ABSU_EXPR will have the corresponding signed type. */ | |
159 | (simplify (abs (convert @0)) | |
160 | (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0)) | |
161 | && !TYPE_UNSIGNED (TREE_TYPE (@0)) | |
162 | && element_precision (type) > element_precision (TREE_TYPE (@0))) | |
163 | (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); } | |
164 | (convert (absu:utype @0))))) | |
165 | ||
a0d732ee JJ |
166 | #if GIMPLE |
167 | /* Optimize (X + (X >> (prec - 1))) ^ (X >> (prec - 1)) into abs (X). */ | |
168 | (simplify | |
169 | (bit_xor:c (plus:c @0 (rshift@2 @0 INTEGER_CST@1)) @2) | |
170 | (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0)) | |
171 | && !TYPE_UNSIGNED (TREE_TYPE (@0)) | |
172 | && wi::to_widest (@1) == element_precision (TREE_TYPE (@0)) - 1) | |
173 | (abs @0))) | |
174 | #endif | |
e197e64e | 175 | |
e0ee10ed | 176 | /* Simplifications of operations with one constant operand and |
36a60e48 | 177 | simplifications to constants or single values. */ |
e0ee10ed RB |
178 | |
179 | (for op (plus pointer_plus minus bit_ior bit_xor) | |
180 | (simplify | |
181 | (op @0 integer_zerop) | |
182 | (non_lvalue @0))) | |
183 | ||
a499aac5 RB |
184 | /* 0 +p index -> (type)index */ |
185 | (simplify | |
186 | (pointer_plus integer_zerop @1) | |
187 | (non_lvalue (convert @1))) | |
188 | ||
d43177ad MG |
189 | /* ptr - 0 -> (type)ptr */ |
190 | (simplify | |
191 | (pointer_diff @0 integer_zerop) | |
192 | (convert @0)) | |
193 | ||
a7f24614 RB |
194 | /* See if ARG1 is zero and X + ARG1 reduces to X. |
195 | Likewise if the operands are reversed. */ | |
196 | (simplify | |
197 | (plus:c @0 real_zerop@1) | |
5b02ed4b | 198 | (if (fold_real_zero_addition_p (type, @0, @1, 0)) |
a7f24614 RB |
199 | (non_lvalue @0))) |
200 | ||
201 | /* See if ARG1 is zero and X - ARG1 reduces to X. */ | |
202 | (simplify | |
203 | (minus @0 real_zerop@1) | |
5b02ed4b | 204 | (if (fold_real_zero_addition_p (type, @0, @1, 1)) |
a7f24614 | 205 | (non_lvalue @0))) |
f7b7e5d0 JJ |
206 | |
207 | /* Even if the fold_real_zero_addition_p can't simplify X + 0.0 | |
208 | into X, we can optimize (X + 0.0) + 0.0 or (X + 0.0) - 0.0 | |
209 | or (X - 0.0) + 0.0 into X + 0.0 and (X - 0.0) - 0.0 into X - 0.0 | |
210 | if not -frounding-math. For sNaNs the first operation would raise | |
211 | exceptions but turn the result into qNan, so the second operation | |
212 | would not raise it. */ | |
213 | (for inner_op (plus minus) | |
214 | (for outer_op (plus minus) | |
215 | (simplify | |
216 | (outer_op (inner_op@3 @0 REAL_CST@1) REAL_CST@2) | |
217 | (if (real_zerop (@1) | |
218 | && real_zerop (@2) | |
219 | && !HONOR_SIGN_DEPENDENT_ROUNDING (type)) | |
220 | (with { bool inner_plus = ((inner_op == PLUS_EXPR) | |
221 | ^ REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (@1))); | |
222 | bool outer_plus | |
223 | = ((outer_op == PLUS_EXPR) | |
224 | ^ REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (@2))); } | |
225 | (if (outer_plus && !inner_plus) | |
226 | (outer_op @0 @2) | |
227 | @3)))))) | |
a7f24614 | 228 | |
e0ee10ed RB |
229 | /* Simplify x - x. |
230 | This is unsafe for certain floats even in non-IEEE formats. | |
231 | In IEEE, it is unsafe because it does wrong for NaNs. | |
72c24301 | 232 | PR middle-end/98420: x - x may be -0.0 with FE_DOWNWARD. |
e0ee10ed RB |
233 | Also note that operand_equal_p is always false if an operand |
234 | is volatile. */ | |
235 | (simplify | |
a7f24614 | 236 | (minus @0 @0) |
6ea5fb3c RS |
237 | (if (!FLOAT_TYPE_P (type) |
238 | || (!tree_expr_maybe_nan_p (@0) | |
72c24301 RS |
239 | && !tree_expr_maybe_infinite_p (@0) |
240 | && (!HONOR_SIGN_DEPENDENT_ROUNDING (type) | |
241 | || !HONOR_SIGNED_ZEROS (type)))) | |
a7f24614 | 242 | { build_zero_cst (type); })) |
1af4ebf5 MG |
243 | (simplify |
244 | (pointer_diff @@0 @0) | |
245 | { build_zero_cst (type); }) | |
e0ee10ed RB |
246 | |
247 | (simplify | |
a7f24614 RB |
248 | (mult @0 integer_zerop@1) |
249 | @1) | |
250 | ||
e888bea2 NR |
251 | /* -x == x -> x == 0 */ |
252 | (for cmp (eq ne) | |
253 | (simplify | |
254 | (cmp:c @0 (negate @0)) | |
255 | (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0)) | |
256 | && !TYPE_OVERFLOW_WRAPS (TREE_TYPE(@0))) | |
257 | (cmp @0 { build_zero_cst (TREE_TYPE(@0)); })))) | |
258 | ||
a7f24614 RB |
259 | /* Maybe fold x * 0 to 0. The expressions aren't the same |
260 | when x is NaN, since x * 0 is also NaN. Nor are they the | |
261 | same in modes with signed zeros, since multiplying a | |
b3763384 JJ |
262 | negative value by 0 gives -0, not +0. Nor when x is +-Inf, |
263 | since x * 0 is NaN. */ | |
a7f24614 RB |
264 | (simplify |
265 | (mult @0 real_zerop@1) | |
5b02ed4b | 266 | (if (!tree_expr_maybe_nan_p (@0) |
b3763384 | 267 | && (!HONOR_NANS (type) || !tree_expr_maybe_infinite_p (@0)) |
2d3c4775 | 268 | && (!HONOR_SIGNED_ZEROS (type) || tree_expr_nonnegative_p (@0))) |
a7f24614 RB |
269 | @1)) |
270 | ||
271 | /* In IEEE floating point, x*1 is not equivalent to x for snans. | |
272 | Likewise for complex arithmetic with signed zeros. */ | |
273 | (simplify | |
274 | (mult @0 real_onep) | |
5b02ed4b | 275 | (if (!tree_expr_maybe_signaling_nan_p (@0) |
8b5ee871 | 276 | && (!HONOR_SIGNED_ZEROS (type) |
a7f24614 RB |
277 | || !COMPLEX_FLOAT_TYPE_P (type))) |
278 | (non_lvalue @0))) | |
279 | ||
280 | /* Transform x * -1.0 into -x. */ | |
281 | (simplify | |
282 | (mult @0 real_minus_onep) | |
5b02ed4b | 283 | (if (!tree_expr_maybe_signaling_nan_p (@0) |
8b5ee871 | 284 | && (!HONOR_SIGNED_ZEROS (type) |
a7f24614 RB |
285 | || !COMPLEX_FLOAT_TYPE_P (type))) |
286 | (negate @0))) | |
e0ee10ed | 287 | |
46c66a46 RS |
288 | /* Transform x * { 0 or 1, 0 or 1, ... } into x & { 0 or -1, 0 or -1, ...}, |
289 | unless the target has native support for the former but not the latter. */ | |
290 | (simplify | |
291 | (mult @0 VECTOR_CST@1) | |
292 | (if (initializer_each_zero_or_onep (@1) | |
293 | && !HONOR_SNANS (type) | |
294 | && !HONOR_SIGNED_ZEROS (type)) | |
295 | (with { tree itype = FLOAT_TYPE_P (type) ? unsigned_type_for (type) : type; } | |
296 | (if (itype | |
297 | && (!VECTOR_MODE_P (TYPE_MODE (type)) | |
298 | || (VECTOR_MODE_P (TYPE_MODE (itype)) | |
299 | && optab_handler (and_optab, | |
300 | TYPE_MODE (itype)) != CODE_FOR_nothing))) | |
301 | (view_convert (bit_and:itype (view_convert @0) | |
302 | (ne @1 { build_zero_cst (type); }))))))) | |
303 | ||
8c2805bb AP |
304 | (for cmp (gt ge lt le) |
305 | outp (convert convert negate negate) | |
306 | outn (negate negate convert convert) | |
36fe1cdc JJ |
307 | /* Transform X * (X > 0.0 ? 1.0 : -1.0) into abs(X). */ |
308 | /* Transform X * (X >= 0.0 ? 1.0 : -1.0) into abs(X). */ | |
309 | /* Transform X * (X < 0.0 ? 1.0 : -1.0) into -abs(X). */ | |
310 | /* Transform X * (X <= 0.0 ? 1.0 : -1.0) into -abs(X). */ | |
8c2805bb | 311 | (simplify |
36fe1cdc | 312 | (mult:c @0 (cond (cmp @0 real_zerop) real_onep@1 real_minus_onep)) |
5b02ed4b | 313 | (if (!tree_expr_maybe_nan_p (@0) && !HONOR_SIGNED_ZEROS (type)) |
36fe1cdc JJ |
314 | (outp (abs @0)))) |
315 | /* Transform X * (X > 0.0 ? -1.0 : 1.0) into -abs(X). */ | |
316 | /* Transform X * (X >= 0.0 ? -1.0 : 1.0) into -abs(X). */ | |
317 | /* Transform X * (X < 0.0 ? -1.0 : 1.0) into abs(X). */ | |
318 | /* Transform X * (X <= 0.0 ? -1.0 : 1.0) into abs(X). */ | |
8c2805bb | 319 | (simplify |
36fe1cdc | 320 | (mult:c @0 (cond (cmp @0 real_zerop) real_minus_onep real_onep@1)) |
5b02ed4b | 321 | (if (!tree_expr_maybe_nan_p (@0) && !HONOR_SIGNED_ZEROS (type)) |
36fe1cdc | 322 | (outn (abs @0))))) |
8c2805bb AP |
323 | |
324 | /* Transform X * copysign (1.0, X) into abs(X). */ | |
325 | (simplify | |
c6cfa2bf | 326 | (mult:c @0 (COPYSIGN_ALL real_onep @0)) |
5b02ed4b | 327 | (if (!tree_expr_maybe_nan_p (@0) && !HONOR_SIGNED_ZEROS (type)) |
8c2805bb AP |
328 | (abs @0))) |
329 | ||
330 | /* Transform X * copysign (1.0, -X) into -abs(X). */ | |
331 | (simplify | |
c6cfa2bf | 332 | (mult:c @0 (COPYSIGN_ALL real_onep (negate @0))) |
5b02ed4b | 333 | (if (!tree_expr_maybe_nan_p (@0) && !HONOR_SIGNED_ZEROS (type)) |
8c2805bb AP |
334 | (negate (abs @0)))) |
335 | ||
336 | /* Transform copysign (CST, X) into copysign (ABS(CST), X). */ | |
337 | (simplify | |
c6cfa2bf | 338 | (COPYSIGN_ALL REAL_CST@0 @1) |
8c2805bb | 339 | (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@0))) |
c6cfa2bf | 340 | (COPYSIGN_ALL (negate @0) @1))) |
8c2805bb | 341 | |
7486fe15 RS |
342 | /* Transform c ? x * copysign (1, y) : z to c ? x ^ signs(y) : z. |
343 | tree-ssa-math-opts.cc does the corresponding optimization for | |
344 | unconditional multiplications (via xorsign). */ | |
345 | (simplify | |
346 | (IFN_COND_MUL:c @0 @1 (IFN_COPYSIGN real_onep @2) @3) | |
347 | (with { tree signs = sign_mask_for (type); } | |
348 | (if (signs) | |
349 | (with { tree inttype = TREE_TYPE (signs); } | |
350 | (view_convert:type | |
351 | (IFN_COND_XOR:inttype @0 | |
352 | (view_convert:inttype @1) | |
353 | (bit_and (view_convert:inttype @2) { signs; }) | |
354 | (view_convert:inttype @3))))))) | |
355 | ||
633e9920 SF |
356 | /* (x >= 0 ? x : 0) + (x <= 0 ? -x : 0) -> abs x. */ |
357 | (simplify | |
358 | (plus:c (max @0 integer_zerop) (max (negate @0) integer_zerop)) | |
359 | (abs @0)) | |
360 | ||
5b7f6ed0 | 361 | /* X * 1, X / 1 -> X. */ |
e0ee10ed RB |
362 | (for op (mult trunc_div ceil_div floor_div round_div exact_div) |
363 | (simplify | |
364 | (op @0 integer_onep) | |
365 | (non_lvalue @0))) | |
366 | ||
71f82be9 JG |
367 | /* (A / (1 << B)) -> (A >> B). |
368 | Only for unsigned A. For signed A, this would not preserve rounding | |
369 | toward zero. | |
873140e6 | 370 | For example: (-1 / ( 1 << B)) != -1 >> B. |
47d81b1b | 371 | Also handle widening conversions, like: |
873140e6 JJ |
372 | (A / (unsigned long long) (1U << B)) -> (A >> B) |
373 | or | |
374 | (A / (unsigned long long) (1 << B)) -> (A >> B). | |
375 | If the left shift is signed, it can be done only if the upper bits | |
376 | of A starting from shift's type sign bit are zero, as | |
377 | (unsigned long long) (1 << 31) is -2147483648ULL, not 2147483648ULL, | |
378 | so it is valid only if A >> 31 is zero. */ | |
71f82be9 | 379 | (simplify |
5ca24002 | 380 | (trunc_div (convert?@0 @3) (convert2? (lshift integer_onep@1 @2))) |
71f82be9 JG |
381 | (if ((TYPE_UNSIGNED (type) || tree_expr_nonnegative_p (@0)) |
382 | && (!VECTOR_TYPE_P (type) | |
383 | || target_supports_op_p (type, RSHIFT_EXPR, optab_vector) | |
873140e6 JJ |
384 | || target_supports_op_p (type, RSHIFT_EXPR, optab_scalar)) |
385 | && (useless_type_conversion_p (type, TREE_TYPE (@1)) | |
386 | || (element_precision (type) >= element_precision (TREE_TYPE (@1)) | |
387 | && (TYPE_UNSIGNED (TREE_TYPE (@1)) | |
388 | || (element_precision (type) | |
389 | == element_precision (TREE_TYPE (@1))) | |
6d5093da JJ |
390 | || (INTEGRAL_TYPE_P (type) |
391 | && (tree_nonzero_bits (@0) | |
392 | & wi::mask (element_precision (TREE_TYPE (@1)) - 1, | |
393 | true, | |
394 | element_precision (type))) == 0))))) | |
5ca24002 JJ |
395 | (if (!VECTOR_TYPE_P (type) |
396 | && useless_type_conversion_p (TREE_TYPE (@3), TREE_TYPE (@1)) | |
397 | && element_precision (TREE_TYPE (@3)) < element_precision (type)) | |
398 | (convert (rshift @3 @2)) | |
399 | (rshift @0 @2)))) | |
71f82be9 | 400 | |
5b7f6ed0 MG |
401 | /* Preserve explicit divisions by 0: the C++ front-end wants to detect |
402 | undefined behavior in constexpr evaluation, and assuming that the division | |
403 | traps enables better optimizations than these anyway. */ | |
a7f24614 | 404 | (for div (trunc_div ceil_div floor_div round_div exact_div) |
5b7f6ed0 MG |
405 | /* 0 / X is always zero. */ |
406 | (simplify | |
407 | (div integer_zerop@0 @1) | |
408 | /* But not for 0 / 0 so that we can get the proper warnings and errors. */ | |
409 | (if (!integer_zerop (@1)) | |
410 | @0)) | |
d41b0973 | 411 | /* X / -1 is -X. */ |
a7f24614 | 412 | (simplify |
d41b0973 JJ |
413 | (div @0 integer_minus_onep@1) |
414 | (if (!TYPE_UNSIGNED (type)) | |
415 | (negate @0))) | |
416 | /* X / bool_range_Y is X. */ | |
417 | (simplify | |
418 | (div @0 SSA_NAME@1) | |
bd14cdce EB |
419 | (if (INTEGRAL_TYPE_P (type) |
420 | && ssa_name_has_boolean_range (@1) | |
421 | && !flag_non_call_exceptions) | |
d41b0973 | 422 | @0)) |
5b7f6ed0 MG |
423 | /* X / X is one. */ |
424 | (simplify | |
425 | (div @0 @0) | |
9ebce098 JJ |
426 | /* But not for 0 / 0 so that we can get the proper warnings and errors. |
427 | And not for _Fract types where we can't build 1. */ | |
bd14cdce EB |
428 | (if (!ALL_FRACT_MODE_P (TYPE_MODE (type)) |
429 | && !integer_zerop (@0) | |
430 | && (!flag_non_call_exceptions || tree_expr_nonzero_p (@0))) | |
5b7f6ed0 | 431 | { build_one_cst (type); })) |
03cc70b5 | 432 | /* X / abs (X) is X < 0 ? -1 : 1. */ |
da186c1f | 433 | (simplify |
d96a5585 RB |
434 | (div:C @0 (abs @0)) |
435 | (if (INTEGRAL_TYPE_P (type) | |
bd14cdce EB |
436 | && TYPE_OVERFLOW_UNDEFINED (type) |
437 | && !integer_zerop (@0) | |
438 | && (!flag_non_call_exceptions || tree_expr_nonzero_p (@0))) | |
da186c1f RB |
439 | (cond (lt @0 { build_zero_cst (type); }) |
440 | { build_minus_one_cst (type); } { build_one_cst (type); }))) | |
441 | /* X / -X is -1. */ | |
442 | (simplify | |
d96a5585 | 443 | (div:C @0 (negate @0)) |
da186c1f | 444 | (if ((INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type)) |
bd14cdce EB |
445 | && TYPE_OVERFLOW_UNDEFINED (type) |
446 | && !integer_zerop (@0) | |
447 | && (!flag_non_call_exceptions || tree_expr_nonzero_p (@0))) | |
da186c1f | 448 | { build_minus_one_cst (type); }))) |
a7f24614 RB |
449 | |
450 | /* For unsigned integral types, FLOOR_DIV_EXPR is the same as | |
ffc7f200 JJ |
451 | TRUNC_DIV_EXPR. Rewrite into the latter in this case. Similarly |
452 | for MOD instead of DIV. */ | |
453 | (for floor_divmod (floor_div floor_mod) | |
454 | trunc_divmod (trunc_div trunc_mod) | |
455 | (simplify | |
456 | (floor_divmod @0 @1) | |
457 | (if ((INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type)) | |
458 | && TYPE_UNSIGNED (type)) | |
459 | (trunc_divmod @0 @1)))) | |
a7f24614 | 460 | |
a1544878 JJ |
461 | /* 1 / X -> X == 1 for unsigned integer X. |
462 | 1 / X -> X >= -1 && X <= 1 ? X : 0 for signed integer X. | |
463 | But not for 1 / 0 so that we can get proper warnings and errors, | |
464 | and not for 1-bit integers as they are edge cases better handled | |
465 | elsewhere. */ | |
c2b610e7 | 466 | (simplify |
a1544878 JJ |
467 | (trunc_div integer_onep@0 @1) |
468 | (if (INTEGRAL_TYPE_P (type) | |
bd14cdce | 469 | && TYPE_PRECISION (type) > 1 |
a1544878 | 470 | && !integer_zerop (@1) |
bd14cdce | 471 | && (!flag_non_call_exceptions || tree_expr_nonzero_p (@1))) |
a1544878 JJ |
472 | (if (TYPE_UNSIGNED (type)) |
473 | (convert (eq:boolean_type_node @1 { build_one_cst (type); })) | |
474 | (with { tree utype = unsigned_type_for (type); } | |
475 | (cond (le (plus (convert:utype @1) { build_one_cst (utype); }) | |
476 | { build_int_cst (utype, 2); }) | |
477 | @1 { build_zero_cst (type); }))))) | |
c2b610e7 | 478 | |
28093105 RB |
479 | /* Combine two successive divisions. Note that combining ceil_div |
480 | and floor_div is trickier and combining round_div even more so. */ | |
481 | (for div (trunc_div exact_div) | |
c306cfaf | 482 | (simplify |
98610dc5 | 483 | (div (div@3 @0 INTEGER_CST@1) INTEGER_CST@2) |
c306cfaf | 484 | (with { |
4a669ac3 | 485 | wi::overflow_type overflow; |
8e6cdc90 | 486 | wide_int mul = wi::mul (wi::to_wide (@1), wi::to_wide (@2), |
4a669ac3 | 487 | TYPE_SIGN (type), &overflow); |
c306cfaf | 488 | } |
98610dc5 JJ |
489 | (if (div == EXACT_DIV_EXPR |
490 | || optimize_successive_divisions_p (@2, @3)) | |
491 | (if (!overflow) | |
492 | (div @0 { wide_int_to_tree (type, mul); }) | |
493 | (if (TYPE_UNSIGNED (type) | |
494 | || mul != wi::min_value (TYPE_PRECISION (type), SIGNED)) | |
495 | { build_zero_cst (type); })))))) | |
c306cfaf | 496 | |
288fe52e AM |
497 | /* Combine successive multiplications. Similar to above, but handling |
498 | overflow is different. */ | |
499 | (simplify | |
500 | (mult (mult @0 INTEGER_CST@1) INTEGER_CST@2) | |
501 | (with { | |
4a669ac3 | 502 | wi::overflow_type overflow; |
8e6cdc90 | 503 | wide_int mul = wi::mul (wi::to_wide (@1), wi::to_wide (@2), |
4a669ac3 | 504 | TYPE_SIGN (type), &overflow); |
288fe52e AM |
505 | } |
506 | /* Skip folding on overflow: the only special case is @1 * @2 == -INT_MIN, | |
507 | otherwise undefined overflow implies that @0 must be zero. */ | |
4a669ac3 | 508 | (if (!overflow || TYPE_OVERFLOW_WRAPS (type)) |
288fe52e AM |
509 | (mult @0 { wide_int_to_tree (type, mul); })))) |
510 | ||
1089d083 | 511 | /* Similar to above, but there could be an extra add/sub between |
512 | successive multuiplications. */ | |
513 | (simplify | |
514 | (mult (plus:s (mult:s@4 @0 INTEGER_CST@1) INTEGER_CST@2) INTEGER_CST@3) | |
515 | (with { | |
516 | bool overflowed = true; | |
517 | wi::overflow_type ovf1, ovf2; | |
518 | wide_int mul = wi::mul (wi::to_wide (@1), wi::to_wide (@3), | |
519 | TYPE_SIGN (type), &ovf1); | |
520 | wide_int add = wi::mul (wi::to_wide (@2), wi::to_wide (@3), | |
521 | TYPE_SIGN (type), &ovf2); | |
522 | if (TYPE_OVERFLOW_UNDEFINED (type)) | |
523 | { | |
524 | #if GIMPLE | |
525 | value_range vr0; | |
526 | if (ovf1 == wi::OVF_NONE && ovf2 == wi::OVF_NONE | |
527 | && get_global_range_query ()->range_of_expr (vr0, @4) | |
528 | && vr0.kind () == VR_RANGE) | |
529 | { | |
530 | wide_int wmin0 = vr0.lower_bound (); | |
531 | wide_int wmax0 = vr0.upper_bound (); | |
532 | wmin0 = wi::mul (wmin0, wi::to_wide (@3), TYPE_SIGN (type), &ovf1); | |
533 | wmax0 = wi::mul (wmax0, wi::to_wide (@3), TYPE_SIGN (type), &ovf2); | |
534 | if (ovf1 == wi::OVF_NONE && ovf2 == wi::OVF_NONE) | |
535 | { | |
536 | wi::add (wmin0, add, TYPE_SIGN (type), &ovf1); | |
537 | wi::add (wmax0, add, TYPE_SIGN (type), &ovf2); | |
538 | if (ovf1 == wi::OVF_NONE && ovf2 == wi::OVF_NONE) | |
539 | overflowed = false; | |
540 | } | |
541 | } | |
542 | #endif | |
543 | } | |
544 | else | |
545 | overflowed = false; | |
546 | } | |
547 | /* Skip folding on overflow. */ | |
548 | (if (!overflowed) | |
549 | (plus (mult @0 { wide_int_to_tree (type, mul); }) | |
550 | { wide_int_to_tree (type, add); })))) | |
551 | ||
552 | /* Similar to above, but a multiplication between successive additions. */ | |
553 | (simplify | |
554 | (plus (mult:s (plus:s @0 INTEGER_CST@1) INTEGER_CST@2) INTEGER_CST@3) | |
555 | (with { | |
556 | bool overflowed = true; | |
557 | wi::overflow_type ovf1; | |
558 | wi::overflow_type ovf2; | |
559 | wide_int mul = wi::mul (wi::to_wide (@1), wi::to_wide (@2), | |
560 | TYPE_SIGN (type), &ovf1); | |
561 | wide_int add = wi::add (mul, wi::to_wide (@3), | |
562 | TYPE_SIGN (type), &ovf2); | |
563 | if (TYPE_OVERFLOW_UNDEFINED (type)) | |
564 | { | |
565 | #if GIMPLE | |
566 | value_range vr0; | |
567 | if (ovf1 == wi::OVF_NONE && ovf2 == wi::OVF_NONE | |
568 | && get_global_range_query ()->range_of_expr (vr0, @0) | |
569 | && vr0.kind () == VR_RANGE) | |
570 | { | |
571 | wide_int wmin0 = vr0.lower_bound (); | |
572 | wide_int wmax0 = vr0.upper_bound (); | |
573 | wmin0 = wi::mul (wmin0, wi::to_wide (@2), TYPE_SIGN (type), &ovf1); | |
574 | wmax0 = wi::mul (wmax0, wi::to_wide (@2), TYPE_SIGN (type), &ovf2); | |
575 | if (ovf1 == wi::OVF_NONE && ovf2 == wi::OVF_NONE) | |
576 | { | |
577 | wi::add (wmin0, mul, TYPE_SIGN (type), &ovf1); | |
578 | wi::add (wmax0, mul, TYPE_SIGN (type), &ovf2); | |
579 | if (ovf1 == wi::OVF_NONE && ovf2 == wi::OVF_NONE) | |
580 | overflowed = false; | |
581 | } | |
582 | } | |
583 | #endif | |
584 | } | |
585 | else | |
586 | overflowed = false; | |
587 | } | |
588 | /* Skip folding on overflow. */ | |
589 | (if (!overflowed) | |
590 | (plus (mult @0 @2) { wide_int_to_tree (type, add); })))) | |
591 | ||
a7f24614 | 592 | /* Optimize A / A to 1.0 if we don't care about |
09240451 | 593 | NaNs or Infinities. */ |
a7f24614 RB |
594 | (simplify |
595 | (rdiv @0 @0) | |
09240451 | 596 | (if (FLOAT_TYPE_P (type) |
1b457aa4 | 597 | && ! HONOR_NANS (type) |
8b5ee871 | 598 | && ! HONOR_INFINITIES (type)) |
09240451 MG |
599 | { build_one_cst (type); })) |
600 | ||
601 | /* Optimize -A / A to -1.0 if we don't care about | |
602 | NaNs or Infinities. */ | |
603 | (simplify | |
e04d2a35 | 604 | (rdiv:C @0 (negate @0)) |
09240451 | 605 | (if (FLOAT_TYPE_P (type) |
1b457aa4 | 606 | && ! HONOR_NANS (type) |
8b5ee871 | 607 | && ! HONOR_INFINITIES (type)) |
09240451 | 608 | { build_minus_one_cst (type); })) |
a7f24614 | 609 | |
8c6961ca PK |
610 | /* PR71078: x / abs(x) -> copysign (1.0, x) */ |
611 | (simplify | |
612 | (rdiv:C (convert? @0) (convert? (abs @0))) | |
613 | (if (SCALAR_FLOAT_TYPE_P (type) | |
614 | && ! HONOR_NANS (type) | |
615 | && ! HONOR_INFINITIES (type)) | |
616 | (switch | |
617 | (if (types_match (type, float_type_node)) | |
618 | (BUILT_IN_COPYSIGNF { build_one_cst (type); } (convert @0))) | |
619 | (if (types_match (type, double_type_node)) | |
620 | (BUILT_IN_COPYSIGN { build_one_cst (type); } (convert @0))) | |
621 | (if (types_match (type, long_double_type_node)) | |
622 | (BUILT_IN_COPYSIGNL { build_one_cst (type); } (convert @0)))))) | |
623 | ||
a7f24614 RB |
624 | /* In IEEE floating point, x/1 is not equivalent to x for snans. */ |
625 | (simplify | |
626 | (rdiv @0 real_onep) | |
5b02ed4b | 627 | (if (!tree_expr_maybe_signaling_nan_p (@0)) |
a7f24614 RB |
628 | (non_lvalue @0))) |
629 | ||
630 | /* In IEEE floating point, x/-1 is not equivalent to -x for snans. */ | |
631 | (simplify | |
632 | (rdiv @0 real_minus_onep) | |
5b02ed4b | 633 | (if (!tree_expr_maybe_signaling_nan_p (@0)) |
a7f24614 RB |
634 | (negate @0))) |
635 | ||
5711ac88 | 636 | (if (flag_reciprocal_math) |
81825e28 | 637 | /* Convert (A/B)/C to A/(B*C). */ |
5711ac88 N |
638 | (simplify |
639 | (rdiv (rdiv:s @0 @1) @2) | |
81825e28 WD |
640 | (rdiv @0 (mult @1 @2))) |
641 | ||
642 | /* Canonicalize x / (C1 * y) to (x * C2) / y. */ | |
643 | (simplify | |
644 | (rdiv @0 (mult:s @1 REAL_CST@2)) | |
645 | (with | |
646 | { tree tem = const_binop (RDIV_EXPR, type, build_one_cst (type), @2); } | |
647 | (if (tem) | |
648 | (rdiv (mult @0 { tem; } ) @1)))) | |
5711ac88 N |
649 | |
650 | /* Convert A/(B/C) to (A/B)*C */ | |
651 | (simplify | |
652 | (rdiv @0 (rdiv:s @1 @2)) | |
653 | (mult (rdiv @0 @1) @2))) | |
654 | ||
6a435314 WD |
655 | /* Simplify x / (- y) to -x / y. */ |
656 | (simplify | |
657 | (rdiv @0 (negate @1)) | |
658 | (rdiv (negate @0) @1)) | |
659 | ||
5e21d765 WD |
660 | (if (flag_unsafe_math_optimizations) |
661 | /* Simplify (C / x op 0.0) to x op 0.0 for C != 0, C != Inf/Nan. | |
662 | Since C / x may underflow to zero, do this only for unsafe math. */ | |
663 | (for op (lt le gt ge) | |
664 | neg_op (gt ge lt le) | |
665 | (simplify | |
666 | (op (rdiv REAL_CST@0 @1) real_zerop@2) | |
667 | (if (!HONOR_SIGNED_ZEROS (@1) && !HONOR_INFINITIES (@1)) | |
668 | (switch | |
669 | (if (real_less (&dconst0, TREE_REAL_CST_PTR (@0))) | |
670 | (op @1 @2)) | |
671 | /* For C < 0, use the inverted operator. */ | |
672 | (if (real_less (TREE_REAL_CST_PTR (@0), &dconst0)) | |
673 | (neg_op @1 @2))))))) | |
674 | ||
5711ac88 N |
675 | /* Optimize (X & (-A)) / A where A is a power of 2, to X >> log2(A) */ |
676 | (for div (trunc_div ceil_div floor_div round_div exact_div) | |
677 | (simplify | |
678 | (div (convert? (bit_and @0 INTEGER_CST@1)) INTEGER_CST@2) | |
679 | (if (integer_pow2p (@2) | |
680 | && tree_int_cst_sgn (@2) > 0 | |
a1488398 | 681 | && tree_nop_conversion_p (type, TREE_TYPE (@0)) |
8e6cdc90 RS |
682 | && wi::to_wide (@2) + wi::to_wide (@1) == 0) |
683 | (rshift (convert @0) | |
684 | { build_int_cst (integer_type_node, | |
685 | wi::exact_log2 (wi::to_wide (@2))); })))) | |
5711ac88 | 686 | |
a7f24614 RB |
687 | /* If ARG1 is a constant, we can convert this to a multiply by the |
688 | reciprocal. This does not have the same rounding properties, | |
689 | so only do this if -freciprocal-math. We can actually | |
690 | always safely do it if ARG1 is a power of two, but it's hard to | |
691 | tell if it is or not in a portable manner. */ | |
692 | (for cst (REAL_CST COMPLEX_CST VECTOR_CST) | |
693 | (simplify | |
694 | (rdiv @0 cst@1) | |
695 | (if (optimize) | |
53bc4b3a RB |
696 | (if (flag_reciprocal_math |
697 | && !real_zerop (@1)) | |
a7f24614 | 698 | (with |
249700b5 | 699 | { tree tem = const_binop (RDIV_EXPR, type, build_one_cst (type), @1); } |
a7f24614 | 700 | (if (tem) |
8fdc6c67 RB |
701 | (mult @0 { tem; } ))) |
702 | (if (cst != COMPLEX_CST) | |
703 | (with { tree inverse = exact_inverse (type, @1); } | |
704 | (if (inverse) | |
705 | (mult @0 { inverse; } )))))))) | |
a7f24614 | 706 | |
a7f24614 | 707 | (for mod (ceil_mod floor_mod round_mod trunc_mod) |
e0ee10ed RB |
708 | /* 0 % X is always zero. */ |
709 | (simplify | |
a7f24614 | 710 | (mod integer_zerop@0 @1) |
e0ee10ed RB |
711 | /* But not for 0 % 0 so that we can get the proper warnings and errors. */ |
712 | (if (!integer_zerop (@1)) | |
713 | @0)) | |
714 | /* X % 1 is always zero. */ | |
715 | (simplify | |
a7f24614 RB |
716 | (mod @0 integer_onep) |
717 | { build_zero_cst (type); }) | |
718 | /* X % -1 is zero. */ | |
719 | (simplify | |
09240451 MG |
720 | (mod @0 integer_minus_onep@1) |
721 | (if (!TYPE_UNSIGNED (type)) | |
bc4315fb | 722 | { build_zero_cst (type); })) |
5b7f6ed0 MG |
723 | /* X % X is zero. */ |
724 | (simplify | |
725 | (mod @0 @0) | |
726 | /* But not for 0 % 0 so that we can get the proper warnings and errors. */ | |
727 | (if (!integer_zerop (@0)) | |
728 | { build_zero_cst (type); })) | |
bc4315fb MG |
729 | /* (X % Y) % Y is just X % Y. */ |
730 | (simplify | |
731 | (mod (mod@2 @0 @1) @1) | |
98e30e51 RB |
732 | @2) |
733 | /* From extract_muldiv_1: (X * C1) % C2 is zero if C1 is a multiple of C2. */ | |
734 | (simplify | |
735 | (mod (mult @0 INTEGER_CST@1) INTEGER_CST@2) | |
736 | (if (ANY_INTEGRAL_TYPE_P (type) | |
737 | && TYPE_OVERFLOW_UNDEFINED (type) | |
8e6cdc90 RS |
738 | && wi::multiple_of_p (wi::to_wide (@1), wi::to_wide (@2), |
739 | TYPE_SIGN (type))) | |
392750c5 JJ |
740 | { build_zero_cst (type); })) |
741 | /* For (X % C) == 0, if X is signed and C is power of 2, use unsigned | |
742 | modulo and comparison, since it is simpler and equivalent. */ | |
743 | (for cmp (eq ne) | |
744 | (simplify | |
745 | (cmp (mod @0 integer_pow2p@2) integer_zerop@1) | |
746 | (if (!TYPE_UNSIGNED (TREE_TYPE (@0))) | |
747 | (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); } | |
748 | (cmp (mod (convert:utype @0) (convert:utype @2)) (convert:utype @1))))))) | |
a7f24614 RB |
749 | |
750 | /* X % -C is the same as X % C. */ | |
751 | (simplify | |
752 | (trunc_mod @0 INTEGER_CST@1) | |
753 | (if (TYPE_SIGN (type) == SIGNED | |
754 | && !TREE_OVERFLOW (@1) | |
8e6cdc90 | 755 | && wi::neg_p (wi::to_wide (@1)) |
a7f24614 RB |
756 | && !TYPE_OVERFLOW_TRAPS (type) |
757 | /* Avoid this transformation if C is INT_MIN, i.e. C == -C. */ | |
758 | && !sign_bit_p (@1, @1)) | |
759 | (trunc_mod @0 (negate @1)))) | |
e0ee10ed | 760 | |
8f0c696a RB |
761 | /* X % -Y is the same as X % Y. */ |
762 | (simplify | |
763 | (trunc_mod @0 (convert? (negate @1))) | |
a2a743a1 MP |
764 | (if (INTEGRAL_TYPE_P (type) |
765 | && !TYPE_UNSIGNED (type) | |
8f0c696a | 766 | && !TYPE_OVERFLOW_TRAPS (type) |
20b8d734 JJ |
767 | && tree_nop_conversion_p (type, TREE_TYPE (@1)) |
768 | /* Avoid this transformation if X might be INT_MIN or | |
769 | Y might be -1, because we would then change valid | |
770 | INT_MIN % -(-1) into invalid INT_MIN % -1. */ | |
8e6cdc90 | 771 | && (expr_not_equal_to (@0, wi::to_wide (TYPE_MIN_VALUE (type))) |
20b8d734 JJ |
772 | || expr_not_equal_to (@1, wi::minus_one (TYPE_PRECISION |
773 | (TREE_TYPE (@1)))))) | |
8f0c696a RB |
774 | (trunc_mod @0 (convert @1)))) |
775 | ||
f461569a MP |
776 | /* X - (X / Y) * Y is the same as X % Y. */ |
777 | (simplify | |
2eef1fc1 RB |
778 | (minus (convert1? @0) (convert2? (mult:c (trunc_div @@0 @@1) @1))) |
779 | (if (INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type)) | |
fba46f03 | 780 | (convert (trunc_mod @0 @1)))) |
f461569a | 781 | |
df1a0d52 NR |
782 | /* x * (1 + y / x) - y -> x - y % x */ |
783 | (simplify | |
784 | (minus (mult:cs @0 (plus:s (trunc_div:s @1 @0) integer_onep)) @1) | |
785 | (if (INTEGRAL_TYPE_P (type)) | |
786 | (minus @0 (trunc_mod @1 @0)))) | |
787 | ||
8f0c696a RB |
788 | /* Optimize TRUNC_MOD_EXPR by a power of two into a BIT_AND_EXPR, |
789 | i.e. "X % C" into "X & (C - 1)", if X and C are positive. | |
790 | Also optimize A % (C << N) where C is a power of 2, | |
222f312a | 791 | to A & ((C << N) - 1). |
792 | Also optimize "A shift (B % C)", if C is a power of 2, to | |
793 | "A shift (B & (C - 1))". SHIFT operation include "<<" and ">>" | |
794 | and assume (B % C) is nonnegative as shifts negative values would | |
795 | be UB. */ | |
8f0c696a RB |
796 | (match (power_of_two_cand @1) |
797 | INTEGER_CST@1) | |
798 | (match (power_of_two_cand @1) | |
799 | (lshift INTEGER_CST@1 @2)) | |
800 | (for mod (trunc_mod floor_mod) | |
222f312a | 801 | (for shift (lshift rshift) |
802 | (simplify | |
803 | (shift @0 (mod @1 (power_of_two_cand@2 @3))) | |
804 | (if (integer_pow2p (@3) && tree_int_cst_sgn (@3) > 0) | |
805 | (shift @0 (bit_and @1 (minus @2 { build_int_cst (TREE_TYPE (@2), | |
806 | 1); })))))) | |
8f0c696a | 807 | (simplify |
70099a6a JJ |
808 | (mod @0 (convert? (power_of_two_cand@1 @2))) |
809 | (if ((TYPE_UNSIGNED (type) || tree_expr_nonnegative_p (@0)) | |
810 | /* Allow any integral conversions of the divisor, except | |
811 | conversion from narrower signed to wider unsigned type | |
812 | where if @1 would be negative power of two, the divisor | |
813 | would not be a power of two. */ | |
814 | && INTEGRAL_TYPE_P (type) | |
815 | && INTEGRAL_TYPE_P (TREE_TYPE (@1)) | |
816 | && (TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@1)) | |
817 | || TYPE_UNSIGNED (TREE_TYPE (@1)) | |
818 | || !TYPE_UNSIGNED (type)) | |
819 | && integer_pow2p (@2) && tree_int_cst_sgn (@2) > 0) | |
820 | (with { tree utype = TREE_TYPE (@1); | |
821 | if (!TYPE_OVERFLOW_WRAPS (utype)) | |
822 | utype = unsigned_type_for (utype); } | |
823 | (bit_and @0 (convert (minus (convert:utype @1) | |
824 | { build_one_cst (utype); }))))))) | |
8f0c696a | 825 | |
887ab609 N |
826 | /* Simplify (unsigned t * 2)/2 -> unsigned t & 0x7FFFFFFF. */ |
827 | (simplify | |
828 | (trunc_div (mult @0 integer_pow2p@1) @1) | |
f62115c9 | 829 | (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)) && TYPE_UNSIGNED (TREE_TYPE (@0))) |
887ab609 | 830 | (bit_and @0 { wide_int_to_tree |
8e6cdc90 RS |
831 | (type, wi::mask (TYPE_PRECISION (type) |
832 | - wi::exact_log2 (wi::to_wide (@1)), | |
887ab609 N |
833 | false, TYPE_PRECISION (type))); }))) |
834 | ||
5f8d832e N |
835 | /* Simplify (unsigned t / 2) * 2 -> unsigned t & ~1. */ |
836 | (simplify | |
837 | (mult (trunc_div @0 integer_pow2p@1) @1) | |
f62115c9 | 838 | (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)) && TYPE_UNSIGNED (TREE_TYPE (@0))) |
5f8d832e N |
839 | (bit_and @0 (negate @1)))) |
840 | ||
95765f36 N |
841 | /* Simplify (t * 2) / 2) -> t. */ |
842 | (for div (trunc_div ceil_div floor_div round_div exact_div) | |
843 | (simplify | |
55d84e61 | 844 | (div (mult:c @0 @1) @1) |
a3ebc134 JJ |
845 | (if (ANY_INTEGRAL_TYPE_P (type)) |
846 | (if (TYPE_OVERFLOW_UNDEFINED (type)) | |
847 | @0 | |
848 | #if GIMPLE | |
83325a9d JJ |
849 | (with |
850 | { | |
851 | bool overflowed = true; | |
45f4e2b0 | 852 | value_range vr0, vr1; |
83325a9d | 853 | if (INTEGRAL_TYPE_P (type) |
45f4e2b0 AH |
854 | && get_global_range_query ()->range_of_expr (vr0, @0) |
855 | && get_global_range_query ()->range_of_expr (vr1, @1) | |
856 | && vr0.kind () == VR_RANGE | |
857 | && vr1.kind () == VR_RANGE) | |
83325a9d | 858 | { |
45f4e2b0 AH |
859 | wide_int wmin0 = vr0.lower_bound (); |
860 | wide_int wmax0 = vr0.upper_bound (); | |
861 | wide_int wmin1 = vr1.lower_bound (); | |
862 | wide_int wmax1 = vr1.upper_bound (); | |
83325a9d JJ |
863 | /* If the multiplication can't overflow/wrap around, then |
864 | it can be optimized too. */ | |
865 | wi::overflow_type min_ovf, max_ovf; | |
866 | wi::mul (wmin0, wmin1, TYPE_SIGN (type), &min_ovf); | |
867 | wi::mul (wmax0, wmax1, TYPE_SIGN (type), &max_ovf); | |
868 | if (min_ovf == wi::OVF_NONE && max_ovf == wi::OVF_NONE) | |
869 | { | |
870 | wi::mul (wmin0, wmax1, TYPE_SIGN (type), &min_ovf); | |
871 | wi::mul (wmax0, wmin1, TYPE_SIGN (type), &max_ovf); | |
872 | if (min_ovf == wi::OVF_NONE && max_ovf == wi::OVF_NONE) | |
873 | overflowed = false; | |
874 | } | |
875 | } | |
876 | } | |
877 | (if (!overflowed) | |
878 | @0)) | |
a3ebc134 JJ |
879 | #endif |
880 | )))) | |
95765f36 | 881 | |
d202f9bd | 882 | (for op (negate abs) |
9b054b08 RS |
883 | /* Simplify cos(-x) and cos(|x|) -> cos(x). Similarly for cosh. */ |
884 | (for coss (COS COSH) | |
885 | (simplify | |
886 | (coss (op @0)) | |
887 | (coss @0))) | |
888 | /* Simplify pow(-x, y) and pow(|x|,y) -> pow(x,y) if y is an even integer. */ | |
889 | (for pows (POW) | |
890 | (simplify | |
891 | (pows (op @0) REAL_CST@1) | |
892 | (with { HOST_WIDE_INT n; } | |
893 | (if (real_isinteger (&TREE_REAL_CST (@1), &n) && (n & 1) == 0) | |
5d3498b4 | 894 | (pows @0 @1))))) |
de3fbea3 RB |
895 | /* Likewise for powi. */ |
896 | (for pows (POWI) | |
897 | (simplify | |
898 | (pows (op @0) INTEGER_CST@1) | |
8e6cdc90 | 899 | (if ((wi::to_wide (@1) & 1) == 0) |
de3fbea3 | 900 | (pows @0 @1)))) |
5d3498b4 RS |
901 | /* Strip negate and abs from both operands of hypot. */ |
902 | (for hypots (HYPOT) | |
903 | (simplify | |
904 | (hypots (op @0) @1) | |
905 | (hypots @0 @1)) | |
906 | (simplify | |
907 | (hypots @0 (op @1)) | |
908 | (hypots @0 @1))) | |
909 | /* copysign(-x, y) and copysign(abs(x), y) -> copysign(x, y). */ | |
c6cfa2bf | 910 | (for copysigns (COPYSIGN_ALL) |
5d3498b4 RS |
911 | (simplify |
912 | (copysigns (op @0) @1) | |
913 | (copysigns @0 @1)))) | |
914 | ||
915 | /* abs(x)*abs(x) -> x*x. Should be valid for all types. */ | |
916 | (simplify | |
917 | (mult (abs@1 @0) @1) | |
918 | (mult @0 @0)) | |
919 | ||
64f7ea7c KV |
920 | /* Convert absu(x)*absu(x) -> x*x. */ |
921 | (simplify | |
922 | (mult (absu@1 @0) @1) | |
923 | (mult (convert@2 @0) @2)) | |
924 | ||
5d3498b4 RS |
925 | /* cos(copysign(x, y)) -> cos(x). Similarly for cosh. */ |
926 | (for coss (COS COSH) | |
927 | copysigns (COPYSIGN) | |
928 | (simplify | |
929 | (coss (copysigns @0 @1)) | |
930 | (coss @0))) | |
931 | ||
932 | /* pow(copysign(x, y), z) -> pow(x, z) if z is an even integer. */ | |
933 | (for pows (POW) | |
934 | copysigns (COPYSIGN) | |
935 | (simplify | |
de3fbea3 | 936 | (pows (copysigns @0 @2) REAL_CST@1) |
5d3498b4 RS |
937 | (with { HOST_WIDE_INT n; } |
938 | (if (real_isinteger (&TREE_REAL_CST (@1), &n) && (n & 1) == 0) | |
939 | (pows @0 @1))))) | |
de3fbea3 RB |
940 | /* Likewise for powi. */ |
941 | (for pows (POWI) | |
942 | copysigns (COPYSIGN) | |
943 | (simplify | |
944 | (pows (copysigns @0 @2) INTEGER_CST@1) | |
8e6cdc90 | 945 | (if ((wi::to_wide (@1) & 1) == 0) |
de3fbea3 | 946 | (pows @0 @1)))) |
5d3498b4 RS |
947 | |
948 | (for hypots (HYPOT) | |
949 | copysigns (COPYSIGN) | |
950 | /* hypot(copysign(x, y), z) -> hypot(x, z). */ | |
951 | (simplify | |
952 | (hypots (copysigns @0 @1) @2) | |
953 | (hypots @0 @2)) | |
954 | /* hypot(x, copysign(y, z)) -> hypot(x, y). */ | |
955 | (simplify | |
956 | (hypots @0 (copysigns @1 @2)) | |
957 | (hypots @0 @1))) | |
958 | ||
eeb57981 | 959 | /* copysign(x, CST) -> [-]abs (x). */ |
c6cfa2bf | 960 | (for copysigns (COPYSIGN_ALL) |
eeb57981 RB |
961 | (simplify |
962 | (copysigns @0 REAL_CST@1) | |
963 | (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1))) | |
964 | (negate (abs @0)) | |
965 | (abs @0)))) | |
966 | ||
5d3498b4 | 967 | /* copysign(copysign(x, y), z) -> copysign(x, z). */ |
c6cfa2bf | 968 | (for copysigns (COPYSIGN_ALL) |
5d3498b4 RS |
969 | (simplify |
970 | (copysigns (copysigns @0 @1) @2) | |
971 | (copysigns @0 @2))) | |
972 | ||
973 | /* copysign(x,y)*copysign(x,y) -> x*x. */ | |
c6cfa2bf | 974 | (for copysigns (COPYSIGN_ALL) |
5d3498b4 RS |
975 | (simplify |
976 | (mult (copysigns@2 @0 @1) @2) | |
977 | (mult @0 @0))) | |
978 | ||
979 | /* ccos(-x) -> ccos(x). Similarly for ccosh. */ | |
980 | (for ccoss (CCOS CCOSH) | |
981 | (simplify | |
982 | (ccoss (negate @0)) | |
983 | (ccoss @0))) | |
d202f9bd | 984 | |
abcc43f5 RS |
985 | /* cabs(-x) and cos(conj(x)) -> cabs(x). */ |
986 | (for ops (conj negate) | |
987 | (for cabss (CABS) | |
988 | (simplify | |
989 | (cabss (ops @0)) | |
990 | (cabss @0)))) | |
991 | ||
0a8f32b8 RB |
992 | /* Fold (a * (1 << b)) into (a << b) */ |
993 | (simplify | |
994 | (mult:c @0 (convert? (lshift integer_onep@1 @2))) | |
995 | (if (! FLOAT_TYPE_P (type) | |
9ff6fb6e | 996 | && tree_nop_conversion_p (type, TREE_TYPE (@1))) |
0a8f32b8 RB |
997 | (lshift @0 @2))) |
998 | ||
03acd8b6 RS |
999 | /* Shifts by constants distribute over several binary operations, |
1000 | hence (X << C) + (Y << C) can be simplified to (X + Y) << C. */ | |
1001 | (for op (plus minus) | |
1002 | (simplify | |
1003 | (op (lshift:s @0 @1) (lshift:s @2 @1)) | |
1004 | (if (INTEGRAL_TYPE_P (type) | |
1005 | && TYPE_OVERFLOW_WRAPS (type) | |
1006 | && !TYPE_SATURATING (type)) | |
1007 | (lshift (op @0 @2) @1)))) | |
1008 | ||
1009 | (for op (bit_and bit_ior bit_xor) | |
1010 | (simplify | |
1011 | (op (lshift:s @0 @1) (lshift:s @2 @1)) | |
1012 | (if (INTEGRAL_TYPE_P (type)) | |
1013 | (lshift (op @0 @2) @1))) | |
1014 | (simplify | |
1015 | (op (rshift:s @0 @1) (rshift:s @2 @1)) | |
1016 | (if (INTEGRAL_TYPE_P (type)) | |
1017 | (rshift (op @0 @2) @1)))) | |
1018 | ||
4349b15f SD |
1019 | /* Fold (1 << (C - x)) where C = precision(type) - 1 |
1020 | into ((1 << C) >> x). */ | |
1021 | (simplify | |
1022 | (lshift integer_onep@0 (minus@1 INTEGER_CST@2 @3)) | |
1023 | (if (INTEGRAL_TYPE_P (type) | |
56ccfbd6 | 1024 | && wi::eq_p (wi::to_wide (@2), TYPE_PRECISION (type) - 1) |
4349b15f SD |
1025 | && single_use (@1)) |
1026 | (if (TYPE_UNSIGNED (type)) | |
1027 | (rshift (lshift @0 @2) @3) | |
1028 | (with | |
1029 | { tree utype = unsigned_type_for (type); } | |
1030 | (convert (rshift (lshift (convert:utype @0) @2) @3)))))) | |
1031 | ||
11c4a06a AP |
1032 | /* Fold ((type)(a<0)) << SIGNBITOFA into ((type)a) & signbit. */ |
1033 | (simplify | |
1034 | (lshift (convert (lt @0 integer_zerop@1)) INTEGER_CST@2) | |
1035 | (if (TYPE_SIGN (TREE_TYPE (@0)) == SIGNED | |
1036 | && wi::eq_p (wi::to_wide (@2), TYPE_PRECISION (TREE_TYPE (@0)) - 1)) | |
1037 | (with { wide_int wone = wi::one (TYPE_PRECISION (type)); } | |
1038 | (bit_and (convert @0) | |
1039 | { wide_int_to_tree (type, | |
1040 | wi::lshift (wone, wi::to_wide (@2))); })))) | |
1041 | ||
d70720c2 TC |
1042 | /* Fold (-x >> C) into -(x > 0) where C = precision(type) - 1. */ |
1043 | (for cst (INTEGER_CST VECTOR_CST) | |
1044 | (simplify | |
1045 | (rshift (negate:s @0) cst@1) | |
1046 | (if (!TYPE_UNSIGNED (type) | |
1047 | && TYPE_OVERFLOW_UNDEFINED (type)) | |
1048 | (with { tree stype = TREE_TYPE (@1); | |
1049 | tree bt = truth_type_for (type); | |
1050 | tree zeros = build_zero_cst (type); | |
1051 | tree cst = NULL_TREE; } | |
1052 | (switch | |
1053 | /* Handle scalar case. */ | |
1054 | (if (INTEGRAL_TYPE_P (type) | |
1055 | /* If we apply the rule to the scalar type before vectorization | |
1056 | we will enforce the result of the comparison being a bool | |
1057 | which will require an extra AND on the result that will be | |
1058 | indistinguishable from when the user did actually want 0 | |
1059 | or 1 as the result so it can't be removed. */ | |
1060 | && canonicalize_math_after_vectorization_p () | |
1061 | && wi::eq_p (wi::to_wide (@1), TYPE_PRECISION (type) - 1)) | |
1062 | (negate (convert (gt @0 { zeros; })))) | |
1063 | /* Handle vector case. */ | |
1064 | (if (VECTOR_INTEGER_TYPE_P (type) | |
1065 | /* First check whether the target has the same mode for vector | |
1066 | comparison results as it's operands do. */ | |
1067 | && TYPE_MODE (bt) == TYPE_MODE (type) | |
1068 | /* Then check to see if the target is able to expand the comparison | |
1069 | with the given type later on, otherwise we may ICE. */ | |
1070 | && expand_vec_cmp_expr_p (type, bt, GT_EXPR) | |
1071 | && (cst = uniform_integer_cst_p (@1)) != NULL | |
1072 | && wi::eq_p (wi::to_wide (cst), element_precision (type) - 1)) | |
1073 | (view_convert (gt:bt @0 { zeros; })))))))) | |
1074 | ||
0a8f32b8 RB |
1075 | /* Fold (C1/X)*C2 into (C1*C2)/X. */ |
1076 | (simplify | |
ff86345f RB |
1077 | (mult (rdiv@3 REAL_CST@0 @1) REAL_CST@2) |
1078 | (if (flag_associative_math | |
1079 | && single_use (@3)) | |
0a8f32b8 RB |
1080 | (with |
1081 | { tree tem = const_binop (MULT_EXPR, type, @0, @2); } | |
1082 | (if (tem) | |
1083 | (rdiv { tem; } @1))))) | |
1084 | ||
1085 | /* Simplify ~X & X as zero. */ | |
1086 | (simplify | |
1087 | (bit_and:c (convert? @0) (convert? (bit_not @0))) | |
1088 | { build_zero_cst (type); }) | |
1089 | ||
89b80c42 PK |
1090 | /* PR71636: Transform x & ((1U << b) - 1) -> x & ~(~0U << b); */ |
1091 | (simplify | |
1092 | (bit_and:c @0 (plus:s (lshift:s integer_onep @1) integer_minus_onep)) | |
1093 | (if (TYPE_UNSIGNED (type)) | |
1094 | (bit_and @0 (bit_not (lshift { build_all_ones_cst (type); } @1))))) | |
1095 | ||
7aa13860 PK |
1096 | (for bitop (bit_and bit_ior) |
1097 | cmp (eq ne) | |
a93952d2 JJ |
1098 | /* PR35691: Transform |
1099 | (x == 0 & y == 0) -> (x | typeof(x)(y)) == 0. | |
1100 | (x != 0 | y != 0) -> (x | typeof(x)(y)) != 0. */ | |
7aa13860 PK |
1101 | (simplify |
1102 | (bitop (cmp @0 integer_zerop@2) (cmp @1 integer_zerop)) | |
1103 | (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)) | |
a93952d2 JJ |
1104 | && INTEGRAL_TYPE_P (TREE_TYPE (@1)) |
1105 | && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1))) | |
1106 | (cmp (bit_ior @0 (convert @1)) @2))) | |
1107 | /* Transform: | |
1108 | (x == -1 & y == -1) -> (x & typeof(x)(y)) == -1. | |
1109 | (x != -1 | y != -1) -> (x & typeof(x)(y)) != -1. */ | |
1110 | (simplify | |
1111 | (bitop (cmp @0 integer_all_onesp@2) (cmp @1 integer_all_onesp)) | |
1112 | (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)) | |
1113 | && INTEGRAL_TYPE_P (TREE_TYPE (@1)) | |
1114 | && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1))) | |
1115 | (cmp (bit_and @0 (convert @1)) @2)))) | |
7aa13860 | 1116 | |
10158317 RB |
1117 | /* Fold (A & ~B) - (A & B) into (A ^ B) - B. */ |
1118 | (simplify | |
a9658b11 | 1119 | (minus (bit_and:cs @0 (bit_not @1)) (bit_and:cs @0 @1)) |
10158317 RB |
1120 | (minus (bit_xor @0 @1) @1)) |
1121 | (simplify | |
1122 | (minus (bit_and:s @0 INTEGER_CST@2) (bit_and:s @0 INTEGER_CST@1)) | |
8e6cdc90 | 1123 | (if (~wi::to_wide (@2) == wi::to_wide (@1)) |
10158317 RB |
1124 | (minus (bit_xor @0 @1) @1))) |
1125 | ||
1126 | /* Fold (A & B) - (A & ~B) into B - (A ^ B). */ | |
1127 | (simplify | |
a8e9f9a3 | 1128 | (minus (bit_and:cs @0 @1) (bit_and:cs @0 (bit_not @1))) |
10158317 RB |
1129 | (minus @1 (bit_xor @0 @1))) |
1130 | ||
42bd89ce MG |
1131 | /* Simplify (X & ~Y) |^+ (~X & Y) -> X ^ Y. */ |
1132 | (for op (bit_ior bit_xor plus) | |
1133 | (simplify | |
1134 | (op (bit_and:c @0 (bit_not @1)) (bit_and:c (bit_not @0) @1)) | |
1135 | (bit_xor @0 @1)) | |
1136 | (simplify | |
1137 | (op:c (bit_and @0 INTEGER_CST@2) (bit_and (bit_not @0) INTEGER_CST@1)) | |
8e6cdc90 | 1138 | (if (~wi::to_wide (@2) == wi::to_wide (@1)) |
42bd89ce | 1139 | (bit_xor @0 @1)))) |
2066ef6a PK |
1140 | |
1141 | /* PR53979: Transform ((a ^ b) | a) -> (a | b) */ | |
1142 | (simplify | |
1143 | (bit_ior:c (bit_xor:c @0 @1) @0) | |
1144 | (bit_ior @0 @1)) | |
1145 | ||
e268a77b MG |
1146 | /* (a & ~b) | (a ^ b) --> a ^ b */ |
1147 | (simplify | |
1148 | (bit_ior:c (bit_and:c @0 (bit_not @1)) (bit_xor:c@2 @0 @1)) | |
1149 | @2) | |
1150 | ||
1151 | /* (a & ~b) ^ ~a --> ~(a & b) */ | |
1152 | (simplify | |
1153 | (bit_xor:c (bit_and:cs @0 (bit_not @1)) (bit_not @0)) | |
1154 | (bit_not (bit_and @0 @1))) | |
1155 | ||
52792faa KK |
1156 | /* (~a & b) ^ a --> (a | b) */ |
1157 | (simplify | |
1158 | (bit_xor:c (bit_and:cs (bit_not @0) @1) @0) | |
1159 | (bit_ior @0 @1)) | |
1160 | ||
e268a77b MG |
1161 | /* (a | b) & ~(a ^ b) --> a & b */ |
1162 | (simplify | |
1163 | (bit_and:c (bit_ior @0 @1) (bit_not (bit_xor:c @0 @1))) | |
1164 | (bit_and @0 @1)) | |
1165 | ||
1166 | /* a | ~(a ^ b) --> a | ~b */ | |
1167 | (simplify | |
1168 | (bit_ior:c @0 (bit_not:s (bit_xor:c @0 @1))) | |
1169 | (bit_ior @0 (bit_not @1))) | |
1170 | ||
1171 | /* (a | b) | (a &^ b) --> a | b */ | |
1172 | (for op (bit_and bit_xor) | |
1173 | (simplify | |
1174 | (bit_ior:c (bit_ior@2 @0 @1) (op:c @0 @1)) | |
1175 | @2)) | |
1176 | ||
1177 | /* (a & b) | ~(a ^ b) --> ~(a ^ b) */ | |
1178 | (simplify | |
1179 | (bit_ior:c (bit_and:c @0 @1) (bit_not@2 (bit_xor @0 @1))) | |
1180 | @2) | |
1181 | ||
1182 | /* ~(~a & b) --> a | ~b */ | |
1183 | (simplify | |
1184 | (bit_not (bit_and:cs (bit_not @0) @1)) | |
1185 | (bit_ior @0 (bit_not @1))) | |
1186 | ||
fd8303a5 MC |
1187 | /* ~(~a | b) --> a & ~b */ |
1188 | (simplify | |
1189 | (bit_not (bit_ior:cs (bit_not @0) @1)) | |
1190 | (bit_and @0 (bit_not @1))) | |
1191 | ||
f44e6091 ER |
1192 | /* (a ^ b) & ((b ^ c) ^ a) --> (a ^ b) & ~c */ |
1193 | (simplify | |
1194 | (bit_and:c (bit_xor:c@3 @0 @1) (bit_xor:cs (bit_xor:cs @1 @2) @0)) | |
1195 | (bit_and @3 (bit_not @2))) | |
1196 | ||
1197 | /* (a ^ b) | ((b ^ c) ^ a) --> (a ^ b) | c */ | |
1198 | (simplify | |
1199 | (bit_ior:c (bit_xor:c@3 @0 @1) (bit_xor:c (bit_xor:c @1 @2) @0)) | |
1200 | (bit_ior @3 @2)) | |
1201 | ||
8fc183cc JJ |
1202 | /* (~X | C) ^ D -> (X | C) ^ (~D ^ C) if (~D ^ C) can be simplified. */ |
1203 | (simplify | |
1204 | (bit_xor:c (bit_ior:cs (bit_not:s @0) @1) @2) | |
1205 | (bit_xor (bit_ior @0 @1) (bit_xor! (bit_not! @2) @1))) | |
1206 | ||
1207 | /* (~X & C) ^ D -> (X & C) ^ (D ^ C) if (D ^ C) can be simplified. */ | |
1208 | (simplify | |
1209 | (bit_xor:c (bit_and:cs (bit_not:s @0) @1) @2) | |
1210 | (bit_xor (bit_and @0 @1) (bit_xor! @2 @1))) | |
1211 | ||
1212 | /* Simplify (~X & Y) to X ^ Y if we know that (X & ~Y) is 0. */ | |
d982c5b7 MG |
1213 | (simplify |
1214 | (bit_and (bit_not SSA_NAME@0) INTEGER_CST@1) | |
1215 | (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)) | |
8e6cdc90 | 1216 | && wi::bit_and_not (get_nonzero_bits (@0), wi::to_wide (@1)) == 0) |
d982c5b7 | 1217 | (bit_xor @0 @1))) |
10158317 | 1218 | |
f2901002 JJ |
1219 | /* For constants M and N, if M == (1LL << cst) - 1 && (N & M) == M, |
1220 | ((A & N) + B) & M -> (A + B) & M | |
1221 | Similarly if (N & M) == 0, | |
1222 | ((A | N) + B) & M -> (A + B) & M | |
1223 | and for - instead of + (or unary - instead of +) | |
1224 | and/or ^ instead of |. | |
1225 | If B is constant and (B & M) == 0, fold into A & M. */ | |
1226 | (for op (plus minus) | |
1227 | (for bitop (bit_and bit_ior bit_xor) | |
1228 | (simplify | |
1229 | (bit_and (op:s (bitop:s@0 @3 INTEGER_CST@4) @1) INTEGER_CST@2) | |
1230 | (with | |
1231 | { tree pmop[2]; | |
1232 | tree utype = fold_bit_and_mask (TREE_TYPE (@0), @2, op, @0, bitop, | |
1233 | @3, @4, @1, ERROR_MARK, NULL_TREE, | |
1234 | NULL_TREE, pmop); } | |
1235 | (if (utype) | |
1236 | (convert (bit_and (op (convert:utype { pmop[0]; }) | |
1237 | (convert:utype { pmop[1]; })) | |
1238 | (convert:utype @2)))))) | |
1239 | (simplify | |
1240 | (bit_and (op:s @0 (bitop:s@1 @3 INTEGER_CST@4)) INTEGER_CST@2) | |
1241 | (with | |
1242 | { tree pmop[2]; | |
1243 | tree utype = fold_bit_and_mask (TREE_TYPE (@0), @2, op, @0, ERROR_MARK, | |
1244 | NULL_TREE, NULL_TREE, @1, bitop, @3, | |
1245 | @4, pmop); } | |
1246 | (if (utype) | |
1247 | (convert (bit_and (op (convert:utype { pmop[0]; }) | |
1248 | (convert:utype { pmop[1]; })) | |
1249 | (convert:utype @2))))))) | |
1250 | (simplify | |
1251 | (bit_and (op:s @0 @1) INTEGER_CST@2) | |
1252 | (with | |
1253 | { tree pmop[2]; | |
1254 | tree utype = fold_bit_and_mask (TREE_TYPE (@0), @2, op, @0, ERROR_MARK, | |
1255 | NULL_TREE, NULL_TREE, @1, ERROR_MARK, | |
1256 | NULL_TREE, NULL_TREE, pmop); } | |
1257 | (if (utype) | |
1258 | (convert (bit_and (op (convert:utype { pmop[0]; }) | |
1259 | (convert:utype { pmop[1]; })) | |
1260 | (convert:utype @2))))))) | |
1261 | (for bitop (bit_and bit_ior bit_xor) | |
1262 | (simplify | |
1263 | (bit_and (negate:s (bitop:s@0 @2 INTEGER_CST@3)) INTEGER_CST@1) | |
1264 | (with | |
1265 | { tree pmop[2]; | |
1266 | tree utype = fold_bit_and_mask (TREE_TYPE (@0), @1, NEGATE_EXPR, @0, | |
1267 | bitop, @2, @3, NULL_TREE, ERROR_MARK, | |
1268 | NULL_TREE, NULL_TREE, pmop); } | |
1269 | (if (utype) | |
1270 | (convert (bit_and (negate (convert:utype { pmop[0]; })) | |
1271 | (convert:utype @1))))))) | |
1272 | ||
bc4315fb MG |
1273 | /* X % Y is smaller than Y. */ |
1274 | (for cmp (lt ge) | |
1275 | (simplify | |
1276 | (cmp (trunc_mod @0 @1) @1) | |
1277 | (if (TYPE_UNSIGNED (TREE_TYPE (@0))) | |
1278 | { constant_boolean_node (cmp == LT_EXPR, type); }))) | |
1279 | (for cmp (gt le) | |
1280 | (simplify | |
1281 | (cmp @1 (trunc_mod @0 @1)) | |
1282 | (if (TYPE_UNSIGNED (TREE_TYPE (@0))) | |
1283 | { constant_boolean_node (cmp == GT_EXPR, type); }))) | |
1284 | ||
e0ee10ed RB |
1285 | /* x | ~0 -> ~0 */ |
1286 | (simplify | |
ca0b7ece RB |
1287 | (bit_ior @0 integer_all_onesp@1) |
1288 | @1) | |
1289 | ||
1290 | /* x | 0 -> x */ | |
1291 | (simplify | |
1292 | (bit_ior @0 integer_zerop) | |
1293 | @0) | |
e0ee10ed RB |
1294 | |
1295 | /* x & 0 -> 0 */ | |
1296 | (simplify | |
ca0b7ece RB |
1297 | (bit_and @0 integer_zerop@1) |
1298 | @1) | |
e0ee10ed | 1299 | |
a4398a30 | 1300 | /* ~x | x -> -1 */ |
8b5ee871 | 1301 | /* ~x ^ x -> -1 */ |
cf716ab5 | 1302 | (for op (bit_ior bit_xor) |
8b5ee871 MG |
1303 | (simplify |
1304 | (op:c (convert? @0) (convert? (bit_not @0))) | |
1305 | (convert { build_all_ones_cst (TREE_TYPE (@0)); }))) | |
a4398a30 | 1306 | |
e0ee10ed RB |
1307 | /* x ^ x -> 0 */ |
1308 | (simplify | |
1309 | (bit_xor @0 @0) | |
1310 | { build_zero_cst (type); }) | |
1311 | ||
36a60e48 RB |
1312 | /* Canonicalize X ^ ~0 to ~X. */ |
1313 | (simplify | |
1314 | (bit_xor @0 integer_all_onesp@1) | |
1315 | (bit_not @0)) | |
1316 | ||
1317 | /* x & ~0 -> x */ | |
1318 | (simplify | |
1319 | (bit_and @0 integer_all_onesp) | |
1320 | (non_lvalue @0)) | |
1321 | ||
1322 | /* x & x -> x, x | x -> x */ | |
1323 | (for bitop (bit_and bit_ior) | |
1324 | (simplify | |
1325 | (bitop @0 @0) | |
1326 | (non_lvalue @0))) | |
1327 | ||
c7986356 MG |
1328 | /* x & C -> x if we know that x & ~C == 0. */ |
1329 | #if GIMPLE | |
1330 | (simplify | |
1331 | (bit_and SSA_NAME@0 INTEGER_CST@1) | |
1332 | (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)) | |
8e6cdc90 | 1333 | && wi::bit_and_not (get_nonzero_bits (@0), wi::to_wide (@1)) == 0) |
c7986356 MG |
1334 | @0)) |
1335 | #endif | |
1336 | ||
a7b76d57 JJ |
1337 | /* ~(~X - Y) -> X + Y and ~(~X + Y) -> X - Y. */ |
1338 | (simplify | |
1339 | (bit_not (minus (bit_not @0) @1)) | |
1340 | (plus @0 @1)) | |
1341 | (simplify | |
1342 | (bit_not (plus:c (bit_not @0) @1)) | |
1343 | (minus @0 @1)) | |
be58bf98 TC |
1344 | /* (~X - ~Y) -> Y - X. */ |
1345 | (simplify | |
1346 | (minus (bit_not @0) (bit_not @1)) | |
07cc4c1d JJ |
1347 | (if (!TYPE_OVERFLOW_SANITIZED (type)) |
1348 | (with { tree utype = unsigned_type_for (type); } | |
1349 | (convert (minus (convert:utype @1) (convert:utype @0)))))) | |
a7b76d57 | 1350 | |
0bd67518 JJ |
1351 | /* ~(X - Y) -> ~X + Y. */ |
1352 | (simplify | |
1353 | (bit_not (minus:s @0 @1)) | |
1354 | (plus (bit_not @0) @1)) | |
1355 | (simplify | |
1356 | (bit_not (plus:s @0 INTEGER_CST@1)) | |
1357 | (if ((INTEGRAL_TYPE_P (type) | |
1358 | && TYPE_UNSIGNED (type)) | |
1359 | || (!TYPE_OVERFLOW_SANITIZED (type) | |
1360 | && may_negate_without_overflow_p (@1))) | |
1361 | (plus (bit_not @0) { const_unop (NEGATE_EXPR, type, @1); }))) | |
1362 | ||
1363 | #if GIMPLE | |
1364 | /* ~X + Y -> (Y - X) - 1. */ | |
1365 | (simplify | |
1366 | (plus:c (bit_not @0) @1) | |
1367 | (if (ANY_INTEGRAL_TYPE_P (type) | |
1368 | && TYPE_OVERFLOW_WRAPS (type) | |
1369 | /* -1 - X is folded to ~X, so we'd recurse endlessly. */ | |
1370 | && !integer_all_onesp (@1)) | |
1371 | (plus (minus @1 @0) { build_minus_one_cst (type); }) | |
1372 | (if (INTEGRAL_TYPE_P (type) | |
1373 | && TREE_CODE (@1) == INTEGER_CST | |
1374 | && wi::to_wide (@1) != wi::min_value (TYPE_PRECISION (type), | |
1375 | SIGNED)) | |
1376 | (minus (plus @1 { build_minus_one_cst (type); }) @0)))) | |
93416de0 | 1377 | #endif |
8f8762a2 JJ |
1378 | |
1379 | /* ~(X >> Y) -> ~X >> Y if ~X can be simplified. */ | |
1380 | (simplify | |
1381 | (bit_not (rshift:s @0 @1)) | |
1382 | (if (!TYPE_UNSIGNED (TREE_TYPE (@0))) | |
1383 | (rshift (bit_not! @0) @1) | |
1384 | /* For logical right shifts, this is possible only if @0 doesn't | |
1385 | have MSB set and the logical right shift is changed into | |
1386 | arithmetic shift. */ | |
193fccaa AP |
1387 | (if (INTEGRAL_TYPE_P (type) |
1388 | && !wi::neg_p (tree_nonzero_bits (@0))) | |
8f8762a2 JJ |
1389 | (with { tree stype = signed_type_for (TREE_TYPE (@0)); } |
1390 | (convert (rshift (bit_not! (convert:stype @0)) @1)))))) | |
0bd67518 | 1391 | |
0f770b01 RV |
1392 | /* x + (x & 1) -> (x + 1) & ~1 */ |
1393 | (simplify | |
44fc0a51 RB |
1394 | (plus:c @0 (bit_and:s @0 integer_onep@1)) |
1395 | (bit_and (plus @0 @1) (bit_not @1))) | |
0f770b01 RV |
1396 | |
1397 | /* x & ~(x & y) -> x & ~y */ | |
1398 | /* x | ~(x | y) -> x | ~y */ | |
1399 | (for bitop (bit_and bit_ior) | |
af563d4b | 1400 | (simplify |
44fc0a51 RB |
1401 | (bitop:c @0 (bit_not (bitop:cs @0 @1))) |
1402 | (bitop @0 (bit_not @1)))) | |
af563d4b | 1403 | |
03cc70b5 MC |
1404 | /* (~x & y) | ~(x | y) -> ~x */ |
1405 | (simplify | |
1406 | (bit_ior:c (bit_and:c (bit_not@2 @0) @1) (bit_not (bit_ior:c @0 @1))) | |
1407 | @2) | |
1408 | ||
1409 | /* (x | y) ^ (x | ~y) -> ~x */ | |
1410 | (simplify | |
1411 | (bit_xor:c (bit_ior:c @0 @1) (bit_ior:c @0 (bit_not @1))) | |
1412 | (bit_not @0)) | |
1413 | ||
1414 | /* (x & y) | ~(x | y) -> ~(x ^ y) */ | |
1415 | (simplify | |
1416 | (bit_ior:c (bit_and:s @0 @1) (bit_not:s (bit_ior:s @0 @1))) | |
1417 | (bit_not (bit_xor @0 @1))) | |
1418 | ||
1419 | /* (~x | y) ^ (x ^ y) -> x | ~y */ | |
1420 | (simplify | |
1421 | (bit_xor:c (bit_ior:cs (bit_not @0) @1) (bit_xor:s @0 @1)) | |
1422 | (bit_ior @0 (bit_not @1))) | |
1423 | ||
1424 | /* (x ^ y) | ~(x | y) -> ~(x & y) */ | |
1425 | (simplify | |
1426 | (bit_ior:c (bit_xor:s @0 @1) (bit_not:s (bit_ior:s @0 @1))) | |
1427 | (bit_not (bit_and @0 @1))) | |
1428 | ||
af563d4b MG |
1429 | /* (x | y) & ~x -> y & ~x */ |
1430 | /* (x & y) | ~x -> y | ~x */ | |
1431 | (for bitop (bit_and bit_ior) | |
1432 | rbitop (bit_ior bit_and) | |
1433 | (simplify | |
1434 | (bitop:c (rbitop:c @0 @1) (bit_not@2 @0)) | |
1435 | (bitop @1 @2))) | |
0f770b01 | 1436 | |
f13c4673 MP |
1437 | /* (x & y) ^ (x | y) -> x ^ y */ |
1438 | (simplify | |
2d6f2dce MP |
1439 | (bit_xor:c (bit_and @0 @1) (bit_ior @0 @1)) |
1440 | (bit_xor @0 @1)) | |
f13c4673 | 1441 | |
9ea65ca6 MP |
1442 | /* (x ^ y) ^ (x | y) -> x & y */ |
1443 | (simplify | |
1444 | (bit_xor:c (bit_xor @0 @1) (bit_ior @0 @1)) | |
1445 | (bit_and @0 @1)) | |
1446 | ||
1447 | /* (x & y) + (x ^ y) -> x | y */ | |
1448 | /* (x & y) | (x ^ y) -> x | y */ | |
1449 | /* (x & y) ^ (x ^ y) -> x | y */ | |
1450 | (for op (plus bit_ior bit_xor) | |
1451 | (simplify | |
1452 | (op:c (bit_and @0 @1) (bit_xor @0 @1)) | |
1453 | (bit_ior @0 @1))) | |
1454 | ||
1455 | /* (x & y) + (x | y) -> x + y */ | |
1456 | (simplify | |
1457 | (plus:c (bit_and @0 @1) (bit_ior @0 @1)) | |
1458 | (plus @0 @1)) | |
1459 | ||
9737efaf MP |
1460 | /* (x + y) - (x | y) -> x & y */ |
1461 | (simplify | |
1462 | (minus (plus @0 @1) (bit_ior @0 @1)) | |
1463 | (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type) | |
1464 | && !TYPE_SATURATING (type)) | |
1465 | (bit_and @0 @1))) | |
1466 | ||
1467 | /* (x + y) - (x & y) -> x | y */ | |
1468 | (simplify | |
1469 | (minus (plus @0 @1) (bit_and @0 @1)) | |
1470 | (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type) | |
1471 | && !TYPE_SATURATING (type)) | |
1472 | (bit_ior @0 @1))) | |
1473 | ||
e0bfe016 PW |
1474 | /* (x | y) - y -> (x & ~y) */ |
1475 | (simplify | |
1476 | (minus (bit_ior:cs @0 @1) @1) | |
1477 | (bit_and @0 (bit_not @1))) | |
1478 | ||
9ea65ca6 MP |
1479 | /* (x | y) - (x ^ y) -> x & y */ |
1480 | (simplify | |
1481 | (minus (bit_ior @0 @1) (bit_xor @0 @1)) | |
1482 | (bit_and @0 @1)) | |
1483 | ||
1484 | /* (x | y) - (x & y) -> x ^ y */ | |
1485 | (simplify | |
1486 | (minus (bit_ior @0 @1) (bit_and @0 @1)) | |
1487 | (bit_xor @0 @1)) | |
1488 | ||
66cc6273 MP |
1489 | /* (x | y) & ~(x & y) -> x ^ y */ |
1490 | (simplify | |
1491 | (bit_and:c (bit_ior @0 @1) (bit_not (bit_and @0 @1))) | |
1492 | (bit_xor @0 @1)) | |
1493 | ||
1494 | /* (x | y) & (~x ^ y) -> x & y */ | |
1495 | (simplify | |
1496 | (bit_and:c (bit_ior:c @0 @1) (bit_xor:c @1 (bit_not @0))) | |
1497 | (bit_and @0 @1)) | |
1498 | ||
fd8303a5 MC |
1499 | /* (~x | y) & (x | ~y) -> ~(x ^ y) */ |
1500 | (simplify | |
1501 | (bit_and (bit_ior:cs (bit_not @0) @1) (bit_ior:cs @0 (bit_not @1))) | |
1502 | (bit_not (bit_xor @0 @1))) | |
1503 | ||
1504 | /* (~x | y) ^ (x | ~y) -> x ^ y */ | |
1505 | (simplify | |
1506 | (bit_xor (bit_ior:c (bit_not @0) @1) (bit_ior:c @0 (bit_not @1))) | |
1507 | (bit_xor @0 @1)) | |
1508 | ||
553c6572 JL |
1509 | /* ((x & y) - (x | y)) - 1 -> ~(x ^ y) */ |
1510 | (simplify | |
1511 | (plus (nop_convert1? (minus@2 (nop_convert2? (bit_and:c @0 @1)) | |
1512 | (nop_convert2? (bit_ior @0 @1)))) | |
1513 | integer_all_onesp) | |
1514 | (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type) | |
1515 | && !TYPE_SATURATING (type) && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2)) | |
1516 | && !TYPE_OVERFLOW_TRAPS (TREE_TYPE (@2)) | |
1517 | && !TYPE_SATURATING (TREE_TYPE (@2))) | |
1518 | (bit_not (convert (bit_xor @0 @1))))) | |
1519 | (simplify | |
1520 | (minus (nop_convert1? (plus@2 (nop_convert2? (bit_and:c @0 @1)) | |
1521 | integer_all_onesp)) | |
1522 | (nop_convert3? (bit_ior @0 @1))) | |
1523 | (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type) | |
1524 | && !TYPE_SATURATING (type) && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2)) | |
1525 | && !TYPE_OVERFLOW_TRAPS (TREE_TYPE (@2)) | |
1526 | && !TYPE_SATURATING (TREE_TYPE (@2))) | |
1527 | (bit_not (convert (bit_xor @0 @1))))) | |
1528 | (simplify | |
1529 | (minus (nop_convert1? (bit_and @0 @1)) | |
1530 | (nop_convert2? (plus@2 (nop_convert3? (bit_ior:c @0 @1)) | |
1531 | integer_onep))) | |
1532 | (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type) | |
1533 | && !TYPE_SATURATING (type) && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2)) | |
1534 | && !TYPE_OVERFLOW_TRAPS (TREE_TYPE (@2)) | |
1535 | && !TYPE_SATURATING (TREE_TYPE (@2))) | |
1536 | (bit_not (convert (bit_xor @0 @1))))) | |
1537 | ||
5b00d921 RB |
1538 | /* ~x & ~y -> ~(x | y) |
1539 | ~x | ~y -> ~(x & y) */ | |
1540 | (for op (bit_and bit_ior) | |
1541 | rop (bit_ior bit_and) | |
1542 | (simplify | |
1543 | (op (convert1? (bit_not @0)) (convert2? (bit_not @1))) | |
ece46666 MG |
1544 | (if (element_precision (type) <= element_precision (TREE_TYPE (@0)) |
1545 | && element_precision (type) <= element_precision (TREE_TYPE (@1))) | |
5b00d921 RB |
1546 | (bit_not (rop (convert @0) (convert @1)))))) |
1547 | ||
14ea9f92 | 1548 | /* If we are XORing or adding two BIT_AND_EXPR's, both of which are and'ing |
5b00d921 RB |
1549 | with a constant, and the two constants have no bits in common, |
1550 | we should treat this as a BIT_IOR_EXPR since this may produce more | |
1551 | simplifications. */ | |
14ea9f92 RB |
1552 | (for op (bit_xor plus) |
1553 | (simplify | |
1554 | (op (convert1? (bit_and@4 @0 INTEGER_CST@1)) | |
1555 | (convert2? (bit_and@5 @2 INTEGER_CST@3))) | |
1556 | (if (tree_nop_conversion_p (type, TREE_TYPE (@0)) | |
1557 | && tree_nop_conversion_p (type, TREE_TYPE (@2)) | |
8e6cdc90 | 1558 | && (wi::to_wide (@1) & wi::to_wide (@3)) == 0) |
14ea9f92 | 1559 | (bit_ior (convert @4) (convert @5))))) |
5b00d921 RB |
1560 | |
1561 | /* (X | Y) ^ X -> Y & ~ X*/ | |
1562 | (simplify | |
2eef1fc1 | 1563 | (bit_xor:c (convert1? (bit_ior:c @@0 @1)) (convert2? @0)) |
5b00d921 RB |
1564 | (if (tree_nop_conversion_p (type, TREE_TYPE (@0))) |
1565 | (convert (bit_and @1 (bit_not @0))))) | |
1566 | ||
1567 | /* Convert ~X ^ ~Y to X ^ Y. */ | |
1568 | (simplify | |
1569 | (bit_xor (convert1? (bit_not @0)) (convert2? (bit_not @1))) | |
ece46666 MG |
1570 | (if (element_precision (type) <= element_precision (TREE_TYPE (@0)) |
1571 | && element_precision (type) <= element_precision (TREE_TYPE (@1))) | |
5b00d921 RB |
1572 | (bit_xor (convert @0) (convert @1)))) |
1573 | ||
1574 | /* Convert ~X ^ C to X ^ ~C. */ | |
1575 | (simplify | |
1576 | (bit_xor (convert? (bit_not @0)) INTEGER_CST@1) | |
c8ba6498 EB |
1577 | (if (tree_nop_conversion_p (type, TREE_TYPE (@0))) |
1578 | (bit_xor (convert @0) (bit_not @1)))) | |
5b00d921 | 1579 | |
e39dab2c MG |
1580 | /* Fold (X & Y) ^ Y and (X ^ Y) & Y as ~X & Y. */ |
1581 | (for opo (bit_and bit_xor) | |
1582 | opi (bit_xor bit_and) | |
1583 | (simplify | |
de5b5228 | 1584 | (opo:c (opi:cs @0 @1) @1) |
e39dab2c | 1585 | (bit_and (bit_not @0) @1))) |
97e77391 | 1586 | |
14ea9f92 RB |
1587 | /* Given a bit-wise operation CODE applied to ARG0 and ARG1, see if both |
1588 | operands are another bit-wise operation with a common input. If so, | |
1589 | distribute the bit operations to save an operation and possibly two if | |
1590 | constants are involved. For example, convert | |
1591 | (A | B) & (A | C) into A | (B & C) | |
1592 | Further simplification will occur if B and C are constants. */ | |
e07ab2fe MG |
1593 | (for op (bit_and bit_ior bit_xor) |
1594 | rop (bit_ior bit_and bit_and) | |
14ea9f92 | 1595 | (simplify |
2eef1fc1 | 1596 | (op (convert? (rop:c @@0 @1)) (convert? (rop:c @0 @2))) |
e07ab2fe MG |
1597 | (if (tree_nop_conversion_p (type, TREE_TYPE (@1)) |
1598 | && tree_nop_conversion_p (type, TREE_TYPE (@2))) | |
14ea9f92 RB |
1599 | (rop (convert @0) (op (convert @1) (convert @2)))))) |
1600 | ||
e39dab2c MG |
1601 | /* Some simple reassociation for bit operations, also handled in reassoc. */ |
1602 | /* (X & Y) & Y -> X & Y | |
1603 | (X | Y) | Y -> X | Y */ | |
1604 | (for op (bit_and bit_ior) | |
1605 | (simplify | |
2eef1fc1 | 1606 | (op:c (convert1?@2 (op:c @0 @@1)) (convert2? @1)) |
e39dab2c MG |
1607 | @2)) |
1608 | /* (X ^ Y) ^ Y -> X */ | |
1609 | (simplify | |
2eef1fc1 | 1610 | (bit_xor:c (convert1? (bit_xor:c @0 @@1)) (convert2? @1)) |
ece46666 | 1611 | (convert @0)) |
e39dab2c MG |
1612 | /* (X & Y) & (X & Z) -> (X & Y) & Z |
1613 | (X | Y) | (X | Z) -> (X | Y) | Z */ | |
1614 | (for op (bit_and bit_ior) | |
1615 | (simplify | |
6c35e5b0 | 1616 | (op (convert1?@3 (op:c@4 @0 @1)) (convert2?@5 (op:c@6 @0 @2))) |
e39dab2c MG |
1617 | (if (tree_nop_conversion_p (type, TREE_TYPE (@1)) |
1618 | && tree_nop_conversion_p (type, TREE_TYPE (@2))) | |
1619 | (if (single_use (@5) && single_use (@6)) | |
1620 | (op @3 (convert @2)) | |
1621 | (if (single_use (@3) && single_use (@4)) | |
1622 | (op (convert @1) @5)))))) | |
1623 | /* (X ^ Y) ^ (X ^ Z) -> Y ^ Z */ | |
1624 | (simplify | |
1625 | (bit_xor (convert1? (bit_xor:c @0 @1)) (convert2? (bit_xor:c @0 @2))) | |
1626 | (if (tree_nop_conversion_p (type, TREE_TYPE (@1)) | |
1627 | && tree_nop_conversion_p (type, TREE_TYPE (@2))) | |
d78789f5 | 1628 | (bit_xor (convert @1) (convert @2)))) |
5b00d921 | 1629 | |
64f7ea7c KV |
1630 | /* Convert abs (abs (X)) into abs (X). |
1631 | also absu (absu (X)) into absu (X). */ | |
b14a9c57 RB |
1632 | (simplify |
1633 | (abs (abs@1 @0)) | |
1634 | @1) | |
64f7ea7c KV |
1635 | |
1636 | (simplify | |
1637 | (absu (convert@2 (absu@1 @0))) | |
1638 | (if (tree_nop_conversion_p (TREE_TYPE (@2), TREE_TYPE (@1))) | |
1639 | @1)) | |
1640 | ||
1641 | /* Convert abs[u] (-X) -> abs[u] (X). */ | |
f3582e54 RB |
1642 | (simplify |
1643 | (abs (negate @0)) | |
1644 | (abs @0)) | |
64f7ea7c KV |
1645 | |
1646 | (simplify | |
1647 | (absu (negate @0)) | |
1648 | (absu @0)) | |
1649 | ||
1650 | /* Convert abs[u] (X) where X is nonnegative -> (X). */ | |
f3582e54 RB |
1651 | (simplify |
1652 | (abs tree_expr_nonnegative_p@0) | |
1653 | @0) | |
1654 | ||
64f7ea7c KV |
1655 | (simplify |
1656 | (absu tree_expr_nonnegative_p@0) | |
1657 | (convert @0)) | |
1658 | ||
3200de91 | 1659 | /* Simplify (-(X < 0) | 1) * X into abs (X) or absu(X). */ |
8f12ce2e | 1660 | (simplify |
3200de91 AP |
1661 | (mult:c (nop_convert1? |
1662 | (bit_ior (nop_convert2? (negate (convert? (lt @0 integer_zerop)))) | |
1663 | integer_onep)) | |
1664 | (nop_convert3? @0)) | |
8f12ce2e | 1665 | (if (INTEGRAL_TYPE_P (type) |
8f12ce2e JJ |
1666 | && INTEGRAL_TYPE_P (TREE_TYPE (@0)) |
1667 | && !TYPE_UNSIGNED (TREE_TYPE (@0))) | |
3200de91 AP |
1668 | (if (TYPE_UNSIGNED (type)) |
1669 | (absu @0) | |
1670 | (abs @0) | |
1671 | ) | |
1672 | ) | |
1673 | ) | |
8f12ce2e | 1674 | |
e53b6e56 | 1675 | /* A few cases of fold-const.cc negate_expr_p predicate. */ |
55cf3946 RB |
1676 | (match negate_expr_p |
1677 | INTEGER_CST | |
b14a9c57 | 1678 | (if ((INTEGRAL_TYPE_P (type) |
56a6d474 | 1679 | && TYPE_UNSIGNED (type)) |
b14a9c57 | 1680 | || (!TYPE_OVERFLOW_SANITIZED (type) |
55cf3946 RB |
1681 | && may_negate_without_overflow_p (t))))) |
1682 | (match negate_expr_p | |
1683 | FIXED_CST) | |
1684 | (match negate_expr_p | |
1685 | (negate @0) | |
1686 | (if (!TYPE_OVERFLOW_SANITIZED (type)))) | |
1687 | (match negate_expr_p | |
1688 | REAL_CST | |
1689 | (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (t))))) | |
1690 | /* VECTOR_CST handling of non-wrapping types would recurse in unsupported | |
1691 | ways. */ | |
1692 | (match negate_expr_p | |
1693 | VECTOR_CST | |
1694 | (if (FLOAT_TYPE_P (TREE_TYPE (type)) || TYPE_OVERFLOW_WRAPS (type)))) | |
81bd903a MG |
1695 | (match negate_expr_p |
1696 | (minus @0 @1) | |
1697 | (if ((ANY_INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_WRAPS (type)) | |
1698 | || (FLOAT_TYPE_P (type) | |
1699 | && !HONOR_SIGN_DEPENDENT_ROUNDING (type) | |
1700 | && !HONOR_SIGNED_ZEROS (type))))) | |
0a8f32b8 RB |
1701 | |
1702 | /* (-A) * (-B) -> A * B */ | |
1703 | (simplify | |
1704 | (mult:c (convert1? (negate @0)) (convert2? negate_expr_p@1)) | |
1705 | (if (tree_nop_conversion_p (type, TREE_TYPE (@0)) | |
1706 | && tree_nop_conversion_p (type, TREE_TYPE (@1))) | |
1707 | (mult (convert @0) (convert (negate @1))))) | |
03cc70b5 | 1708 | |
55cf3946 | 1709 | /* -(A + B) -> (-B) - A. */ |
b14a9c57 | 1710 | (simplify |
55cf3946 | 1711 | (negate (plus:c @0 negate_expr_p@1)) |
3c44b412 AP |
1712 | (if (!HONOR_SIGN_DEPENDENT_ROUNDING (type) |
1713 | && !HONOR_SIGNED_ZEROS (type)) | |
55cf3946 RB |
1714 | (minus (negate @1) @0))) |
1715 | ||
81bd903a MG |
1716 | /* -(A - B) -> B - A. */ |
1717 | (simplify | |
1718 | (negate (minus @0 @1)) | |
1719 | (if ((ANY_INTEGRAL_TYPE_P (type) && !TYPE_OVERFLOW_SANITIZED (type)) | |
1720 | || (FLOAT_TYPE_P (type) | |
1721 | && !HONOR_SIGN_DEPENDENT_ROUNDING (type) | |
1722 | && !HONOR_SIGNED_ZEROS (type))) | |
1723 | (minus @1 @0))) | |
1af4ebf5 MG |
1724 | (simplify |
1725 | (negate (pointer_diff @0 @1)) | |
1726 | (if (TYPE_OVERFLOW_UNDEFINED (type)) | |
1727 | (pointer_diff @1 @0))) | |
81bd903a | 1728 | |
55cf3946 | 1729 | /* A - B -> A + (-B) if B is easily negatable. */ |
b14a9c57 | 1730 | (simplify |
55cf3946 | 1731 | (minus @0 negate_expr_p@1) |
e4e96a4f KT |
1732 | (if (!FIXED_POINT_TYPE_P (type)) |
1733 | (plus @0 (negate @1)))) | |
d4573ffe | 1734 | |
a42ed1d9 AP |
1735 | /* 1 - a is a ^ 1 if a had a bool range. */ |
1736 | /* This is only enabled for gimple as sometimes | |
1737 | cfun is not set for the function which contains | |
1738 | the SSA_NAME (e.g. while IPA passes are happening, | |
1739 | fold might be called). */ | |
1740 | (simplify | |
1741 | (minus integer_onep@0 SSA_NAME@1) | |
1742 | (if (INTEGRAL_TYPE_P (type) | |
1743 | && ssa_name_has_boolean_range (@1)) | |
1744 | (bit_xor @1 @0))) | |
a42ed1d9 | 1745 | |
8f571e64 RS |
1746 | /* Other simplifications of negation (c.f. fold_negate_expr_1). */ |
1747 | (simplify | |
1748 | (negate (mult:c@0 @1 negate_expr_p@2)) | |
1749 | (if (! TYPE_UNSIGNED (type) | |
1750 | && ! HONOR_SIGN_DEPENDENT_ROUNDING (type) | |
1751 | && single_use (@0)) | |
1752 | (mult @1 (negate @2)))) | |
1753 | ||
1754 | (simplify | |
1755 | (negate (rdiv@0 @1 negate_expr_p@2)) | |
1756 | (if (! HONOR_SIGN_DEPENDENT_ROUNDING (type) | |
1757 | && single_use (@0)) | |
1758 | (rdiv @1 (negate @2)))) | |
1759 | ||
1760 | (simplify | |
1761 | (negate (rdiv@0 negate_expr_p@1 @2)) | |
1762 | (if (! HONOR_SIGN_DEPENDENT_ROUNDING (type) | |
1763 | && single_use (@0)) | |
1764 | (rdiv (negate @1) @2))) | |
1765 | ||
1766 | /* Fold -((int)x >> (prec - 1)) into (unsigned)x >> (prec - 1). */ | |
1767 | (simplify | |
1768 | (negate (convert? (rshift @0 INTEGER_CST@1))) | |
1769 | (if (tree_nop_conversion_p (type, TREE_TYPE (@0)) | |
1770 | && wi::to_wide (@1) == element_precision (type) - 1) | |
1771 | (with { tree stype = TREE_TYPE (@0); | |
1772 | tree ntype = TYPE_UNSIGNED (stype) ? signed_type_for (stype) | |
1773 | : unsigned_type_for (stype); } | |
e2521cd2 RB |
1774 | (if (VECTOR_TYPE_P (type)) |
1775 | (view_convert (rshift (view_convert:ntype @0) @1)) | |
1776 | (convert (rshift (convert:ntype @0) @1)))))) | |
8f571e64 | 1777 | |
5609420f RB |
1778 | /* Try to fold (type) X op CST -> (type) (X op ((type-x) CST)) |
1779 | when profitable. | |
1780 | For bitwise binary operations apply operand conversions to the | |
1781 | binary operation result instead of to the operands. This allows | |
1782 | to combine successive conversions and bitwise binary operations. | |
1783 | We combine the above two cases by using a conditional convert. */ | |
1784 | (for bitop (bit_and bit_ior bit_xor) | |
1785 | (simplify | |
496f4f88 | 1786 | (bitop (convert@2 @0) (convert?@3 @1)) |
5609420f RB |
1787 | (if (((TREE_CODE (@1) == INTEGER_CST |
1788 | && INTEGRAL_TYPE_P (TREE_TYPE (@0)) | |
74faa983 AP |
1789 | && (int_fits_type_p (@1, TREE_TYPE (@0)) |
1790 | || tree_nop_conversion_p (TREE_TYPE (@0), type))) | |
aea417d7 | 1791 | || types_match (@0, @1)) |
645ef01a JJ |
1792 | && !POINTER_TYPE_P (TREE_TYPE (@0)) |
1793 | && TREE_CODE (TREE_TYPE (@0)) != OFFSET_TYPE | |
e53b6e56 | 1794 | /* ??? This transform conflicts with fold-const.cc doing |
ad6f996c RB |
1795 | Convert (T)(x & c) into (T)x & (T)c, if c is an integer |
1796 | constants (if x has signed type, the sign bit cannot be set | |
1797 | in c). This folds extension into the BIT_AND_EXPR. | |
1798 | Restrict it to GIMPLE to avoid endless recursions. */ | |
1799 | && (bitop != BIT_AND_EXPR || GIMPLE) | |
5609420f | 1800 | && (/* That's a good idea if the conversion widens the operand, thus |
32221357 AP |
1801 | after hoisting the conversion the operation will be narrower. |
1802 | It is also a good if the conversion is a nop as moves the | |
1803 | conversion to one side; allowing for combining of the conversions. */ | |
ea2954df AP |
1804 | TYPE_PRECISION (TREE_TYPE (@0)) < TYPE_PRECISION (type) |
1805 | /* The conversion check for being a nop can only be done at the gimple | |
1806 | level as fold_binary has some re-association code which can conflict | |
1807 | with this if there is a "constant" which is not a full INTEGER_CST. */ | |
1808 | || (GIMPLE && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (type)) | |
5609420f RB |
1809 | /* It's also a good idea if the conversion is to a non-integer |
1810 | mode. */ | |
1811 | || GET_MODE_CLASS (TYPE_MODE (type)) != MODE_INT | |
1812 | /* Or if the precision of TO is not the same as the precision | |
1813 | of its mode. */ | |
496f4f88 JJ |
1814 | || !type_has_mode_precision_p (type) |
1815 | /* In GIMPLE, getting rid of 2 conversions for one new results | |
1816 | in smaller IL. */ | |
1817 | || (GIMPLE | |
1818 | && TREE_CODE (@1) != INTEGER_CST | |
1819 | && tree_nop_conversion_p (type, TREE_TYPE (@0)) | |
1820 | && single_use (@2) | |
1821 | && single_use (@3)))) | |
1822 | (convert (bitop @0 (convert @1))))) | |
1823 | /* In GIMPLE, getting rid of 2 conversions for one new results | |
1824 | in smaller IL. */ | |
1825 | (simplify | |
1826 | (convert (bitop:cs@2 (nop_convert:s @0) @1)) | |
1827 | (if (GIMPLE | |
1828 | && TREE_CODE (@1) != INTEGER_CST | |
1829 | && tree_nop_conversion_p (type, TREE_TYPE (@2)) | |
645ef01a JJ |
1830 | && types_match (type, @0) |
1831 | && !POINTER_TYPE_P (TREE_TYPE (@0)) | |
1832 | && TREE_CODE (TREE_TYPE (@0)) != OFFSET_TYPE) | |
496f4f88 | 1833 | (bitop @0 (convert @1))))) |
5609420f | 1834 | |
b14a9c57 RB |
1835 | (for bitop (bit_and bit_ior) |
1836 | rbitop (bit_ior bit_and) | |
1837 | /* (x | y) & x -> x */ | |
1838 | /* (x & y) | x -> x */ | |
1839 | (simplify | |
1840 | (bitop:c (rbitop:c @0 @1) @0) | |
1841 | @0) | |
1842 | /* (~x | y) & x -> x & y */ | |
1843 | /* (~x & y) | x -> x | y */ | |
1844 | (simplify | |
1845 | (bitop:c (rbitop:c (bit_not @0) @1) @0) | |
1846 | (bitop @0 @1))) | |
1847 | ||
28a7fdd8 ER |
1848 | /* ((x | y) & z) | x -> (z & y) | x */ |
1849 | (simplify | |
1850 | (bit_ior:c (bit_and:cs (bit_ior:cs @0 @1) @2) @0) | |
1851 | (bit_ior (bit_and @2 @1) @0)) | |
1852 | ||
5609420f RB |
1853 | /* (x | CST1) & CST2 -> (x & CST2) | (CST1 & CST2) */ |
1854 | (simplify | |
1855 | (bit_and (bit_ior @0 CONSTANT_CLASS_P@1) CONSTANT_CLASS_P@2) | |
1856 | (bit_ior (bit_and @0 @2) (bit_and @1 @2))) | |
1857 | ||
1858 | /* Combine successive equal operations with constants. */ | |
1859 | (for bitop (bit_and bit_ior bit_xor) | |
1860 | (simplify | |
1861 | (bitop (bitop @0 CONSTANT_CLASS_P@1) CONSTANT_CLASS_P@2) | |
fba05d9e RS |
1862 | (if (!CONSTANT_CLASS_P (@0)) |
1863 | /* This is the canonical form regardless of whether (bitop @1 @2) can be | |
1864 | folded to a constant. */ | |
1865 | (bitop @0 (bitop @1 @2)) | |
1866 | /* In this case we have three constants and (bitop @0 @1) doesn't fold | |
1867 | to a constant. This can happen if @0 or @1 is a POLY_INT_CST and if | |
1868 | the values involved are such that the operation can't be decided at | |
1869 | compile time. Try folding one of @0 or @1 with @2 to see whether | |
1870 | that combination can be decided at compile time. | |
1871 | ||
1872 | Keep the existing form if both folds fail, to avoid endless | |
1873 | oscillation. */ | |
1874 | (with { tree cst1 = const_binop (bitop, type, @0, @2); } | |
1875 | (if (cst1) | |
1876 | (bitop @1 { cst1; }) | |
1877 | (with { tree cst2 = const_binop (bitop, type, @1, @2); } | |
1878 | (if (cst2) | |
1879 | (bitop @0 { cst2; })))))))) | |
5609420f RB |
1880 | |
1881 | /* Try simple folding for X op !X, and X op X with the help | |
1882 | of the truth_valued_p and logical_inverted_value predicates. */ | |
1883 | (match truth_valued_p | |
1884 | @0 | |
1885 | (if (INTEGRAL_TYPE_P (type) && TYPE_PRECISION (type) == 1))) | |
f84e7fd6 | 1886 | (for op (tcc_comparison truth_and truth_andif truth_or truth_orif truth_xor) |
5609420f RB |
1887 | (match truth_valued_p |
1888 | (op @0 @1))) | |
1889 | (match truth_valued_p | |
1890 | (truth_not @0)) | |
1891 | ||
0a8f32b8 RB |
1892 | (match (logical_inverted_value @0) |
1893 | (truth_not @0)) | |
5609420f RB |
1894 | (match (logical_inverted_value @0) |
1895 | (bit_not truth_valued_p@0)) | |
1896 | (match (logical_inverted_value @0) | |
09240451 | 1897 | (eq @0 integer_zerop)) |
5609420f | 1898 | (match (logical_inverted_value @0) |
09240451 | 1899 | (ne truth_valued_p@0 integer_truep)) |
5609420f | 1900 | (match (logical_inverted_value @0) |
09240451 | 1901 | (bit_xor truth_valued_p@0 integer_truep)) |
5609420f RB |
1902 | |
1903 | /* X & !X -> 0. */ | |
1904 | (simplify | |
1905 | (bit_and:c @0 (logical_inverted_value @0)) | |
1906 | { build_zero_cst (type); }) | |
1907 | /* X | !X and X ^ !X -> 1, , if X is truth-valued. */ | |
1908 | (for op (bit_ior bit_xor) | |
1909 | (simplify | |
1910 | (op:c truth_valued_p@0 (logical_inverted_value @0)) | |
f84e7fd6 | 1911 | { constant_boolean_node (true, type); })) |
59c20dc7 RB |
1912 | /* X ==/!= !X is false/true. */ |
1913 | (for op (eq ne) | |
1914 | (simplify | |
1915 | (op:c truth_valued_p@0 (logical_inverted_value @0)) | |
1916 | { constant_boolean_node (op == NE_EXPR ? true : false, type); })) | |
5609420f | 1917 | |
5609420f RB |
1918 | /* ~~x -> x */ |
1919 | (simplify | |
1920 | (bit_not (bit_not @0)) | |
1921 | @0) | |
1922 | ||
8fb94fc6 RS |
1923 | (match zero_one_valued_p |
1924 | @0 | |
1925 | (if (INTEGRAL_TYPE_P (type) && tree_nonzero_bits (@0) == 1))) | |
1926 | (match zero_one_valued_p | |
1927 | truth_valued_p@0) | |
1928 | ||
1929 | /* Transform { 0 or 1 } * { 0 or 1 } into { 0 or 1 } & { 0 or 1 }. */ | |
1930 | (simplify | |
1931 | (mult zero_one_valued_p@0 zero_one_valued_p@1) | |
1932 | (if (INTEGRAL_TYPE_P (type)) | |
1933 | (bit_and @0 @1))) | |
1934 | ||
4d9db4bd TC |
1935 | (for cmp (tcc_comparison) |
1936 | icmp (inverted_tcc_comparison) | |
1937 | /* Fold (((a < b) & c) | ((a >= b) & d)) into (a < b ? c : d) & 1. */ | |
1938 | (simplify | |
1939 | (bit_ior | |
1940 | (bit_and:c (convert? (cmp@0 @01 @02)) @3) | |
1941 | (bit_and:c (convert? (icmp@4 @01 @02)) @5)) | |
1942 | (if (INTEGRAL_TYPE_P (type) | |
1943 | /* The scalar version has to be canonicalized after vectorization | |
1944 | because it makes unconditional loads conditional ones, which | |
1945 | means we lose vectorization because the loads may trap. */ | |
1946 | && canonicalize_math_after_vectorization_p ()) | |
1947 | (bit_and (cond @0 @3 @5) { build_one_cst (type); }))) | |
1948 | ||
1949 | /* Fold ((-(a < b) & c) | (-(a >= b) & d)) into a < b ? c : d. This is | |
1950 | canonicalized further and we recognize the conditional form: | |
1951 | (a < b ? c : 0) | (a >= b ? d : 0) into a < b ? c : d. */ | |
1952 | (simplify | |
1953 | (bit_ior | |
1954 | (cond (cmp@0 @01 @02) @3 zerop) | |
1955 | (cond (icmp@4 @01 @02) @5 zerop)) | |
1956 | (if (INTEGRAL_TYPE_P (type) | |
1957 | /* The scalar version has to be canonicalized after vectorization | |
1958 | because it makes unconditional loads conditional ones, which | |
1959 | means we lose vectorization because the loads may trap. */ | |
1960 | && canonicalize_math_after_vectorization_p ()) | |
1961 | (cond @0 @3 @5))) | |
1962 | ||
1963 | /* Vector Fold (((a < b) & c) | ((a >= b) & d)) into a < b ? c : d. | |
1964 | and ((~(a < b) & c) | (~(a >= b) & d)) into a < b ? c : d. */ | |
1965 | (simplify | |
1966 | (bit_ior | |
1967 | (bit_and:c (vec_cond:s (cmp@0 @6 @7) @4 @5) @2) | |
1968 | (bit_and:c (vec_cond:s (icmp@1 @6 @7) @4 @5) @3)) | |
1969 | (if (integer_zerop (@5)) | |
1970 | (switch | |
1971 | (if (integer_onep (@4)) | |
1972 | (bit_and (vec_cond @0 @2 @3) @4)) | |
1973 | (if (integer_minus_onep (@4)) | |
1974 | (vec_cond @0 @2 @3))) | |
1975 | (if (integer_zerop (@4)) | |
1976 | (switch | |
1977 | (if (integer_onep (@5)) | |
1978 | (bit_and (vec_cond @0 @3 @2) @5)) | |
1979 | (if (integer_minus_onep (@5)) | |
1980 | (vec_cond @0 @3 @2)))))) | |
1981 | ||
1982 | /* Scalar Vectorized Fold ((-(a < b) & c) | (-(a >= b) & d)) | |
1983 | into a < b ? d : c. */ | |
1984 | (simplify | |
1985 | (bit_ior | |
1986 | (vec_cond:s (cmp@0 @4 @5) @2 integer_zerop) | |
1987 | (vec_cond:s (icmp@1 @4 @5) @3 integer_zerop)) | |
1988 | (vec_cond @0 @2 @3))) | |
1989 | ||
8fb94fc6 RS |
1990 | /* Transform X & -Y into X * Y when Y is { 0 or 1 }. */ |
1991 | (simplify | |
1992 | (bit_and:c (convert? (negate zero_one_valued_p@0)) @1) | |
1993 | (if (INTEGRAL_TYPE_P (type) | |
1994 | && INTEGRAL_TYPE_P (TREE_TYPE (@0)) | |
1995 | && TREE_CODE (TREE_TYPE (@0)) != BOOLEAN_TYPE | |
1996 | && !TYPE_UNSIGNED (TREE_TYPE (@0))) | |
1997 | (mult (convert @0) @1))) | |
1998 | ||
9991d84d RS |
1999 | /* Narrow integer multiplication by a zero_one_valued_p operand. |
2000 | Multiplication by [0,1] is guaranteed not to overflow. */ | |
2001 | (simplify | |
2002 | (convert (mult@0 zero_one_valued_p@1 INTEGER_CST@2)) | |
2003 | (if (INTEGRAL_TYPE_P (type) | |
2004 | && INTEGRAL_TYPE_P (TREE_TYPE (@0)) | |
5e88fccf | 2005 | && TYPE_PRECISION (type) < TYPE_PRECISION (TREE_TYPE (@0))) |
9991d84d RS |
2006 | (mult (convert @1) (convert @2)))) |
2007 | ||
418b71c0 RS |
2008 | /* (X << C) != 0 can be simplified to X, when C is zero_one_valued_p. |
2009 | Check that the shift is well-defined (C is less than TYPE_PRECISION) | |
2010 | as some targets (such as x86's SSE) may return zero for larger C. */ | |
2011 | (simplify | |
2012 | (ne (lshift zero_one_valued_p@0 INTEGER_CST@1) integer_zerop@2) | |
2013 | (if (tree_fits_shwi_p (@1) | |
2014 | && tree_to_shwi (@1) > 0 | |
2015 | && tree_to_shwi (@1) < TYPE_PRECISION (TREE_TYPE (@0))) | |
2016 | (convert @0))) | |
2017 | ||
2018 | /* (X << C) == 0 can be simplified to X == 0, when C is zero_one_valued_p. | |
2019 | Check that the shift is well-defined (C is less than TYPE_PRECISION) | |
2020 | as some targets (such as x86's SSE) may return zero for larger C. */ | |
2021 | (simplify | |
2022 | (eq (lshift zero_one_valued_p@0 INTEGER_CST@1) integer_zerop@2) | |
2023 | (if (tree_fits_shwi_p (@1) | |
2024 | && tree_to_shwi (@1) > 0 | |
2025 | && tree_to_shwi (@1) < TYPE_PRECISION (TREE_TYPE (@0))) | |
2026 | (eq @0 @2))) | |
2027 | ||
b14a9c57 RB |
2028 | /* Convert ~ (-A) to A - 1. */ |
2029 | (simplify | |
2030 | (bit_not (convert? (negate @0))) | |
ece46666 MG |
2031 | (if (element_precision (type) <= element_precision (TREE_TYPE (@0)) |
2032 | || !TYPE_UNSIGNED (TREE_TYPE (@0))) | |
8b5ee871 | 2033 | (convert (minus @0 { build_each_one_cst (TREE_TYPE (@0)); })))) |
b14a9c57 | 2034 | |
81bd903a MG |
2035 | /* Convert - (~A) to A + 1. */ |
2036 | (simplify | |
e150da38 | 2037 | (negate (nop_convert? (bit_not @0))) |
81bd903a MG |
2038 | (plus (view_convert @0) { build_each_one_cst (type); })) |
2039 | ||
cb3ac198 NR |
2040 | /* (a & b) ^ (a == b) -> !(a | b) */ |
2041 | /* (a & b) == (a ^ b) -> !(a | b) */ | |
2042 | (for first_op (bit_xor eq) | |
2043 | second_op (eq bit_xor) | |
2044 | (simplify | |
2045 | (first_op:c (bit_and:c truth_valued_p@0 truth_valued_p@1) (second_op:c @0 @1)) | |
2046 | (bit_not (bit_ior @0 @1)))) | |
2047 | ||
b14a9c57 RB |
2048 | /* Convert ~ (A - 1) or ~ (A + -1) to -A. */ |
2049 | (simplify | |
8b5ee871 | 2050 | (bit_not (convert? (minus @0 integer_each_onep))) |
ece46666 MG |
2051 | (if (element_precision (type) <= element_precision (TREE_TYPE (@0)) |
2052 | || !TYPE_UNSIGNED (TREE_TYPE (@0))) | |
b14a9c57 RB |
2053 | (convert (negate @0)))) |
2054 | (simplify | |
2055 | (bit_not (convert? (plus @0 integer_all_onesp))) | |
ece46666 MG |
2056 | (if (element_precision (type) <= element_precision (TREE_TYPE (@0)) |
2057 | || !TYPE_UNSIGNED (TREE_TYPE (@0))) | |
b14a9c57 RB |
2058 | (convert (negate @0)))) |
2059 | ||
2060 | /* Part of convert ~(X ^ Y) to ~X ^ Y or X ^ ~Y if ~X or ~Y simplify. */ | |
2061 | (simplify | |
2062 | (bit_not (convert? (bit_xor @0 INTEGER_CST@1))) | |
2063 | (if (tree_nop_conversion_p (type, TREE_TYPE (@0))) | |
2064 | (convert (bit_xor @0 (bit_not @1))))) | |
2065 | (simplify | |
2066 | (bit_not (convert? (bit_xor:c (bit_not @0) @1))) | |
2067 | (if (tree_nop_conversion_p (type, TREE_TYPE (@0))) | |
2068 | (convert (bit_xor @0 @1)))) | |
2069 | ||
e268a77b MG |
2070 | /* Otherwise prefer ~(X ^ Y) to ~X ^ Y as more canonical. */ |
2071 | (simplify | |
e150da38 | 2072 | (bit_xor:c (nop_convert?:s (bit_not:s @0)) @1) |
e268a77b MG |
2073 | (if (tree_nop_conversion_p (type, TREE_TYPE (@0))) |
2074 | (bit_not (bit_xor (view_convert @0) @1)))) | |
2075 | ||
375668e0 RB |
2076 | /* ~(a ^ b) is a == b for truth valued a and b. */ |
2077 | (simplify | |
2078 | (bit_not (bit_xor:s truth_valued_p@0 truth_valued_p@1)) | |
613e3b86 RB |
2079 | (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)) |
2080 | && TYPE_PRECISION (TREE_TYPE (@0)) == 1) | |
375668e0 RB |
2081 | (convert (eq @0 @1)))) |
2082 | ||
f9378e3c AP |
2083 | /* (~a) == b is a ^ b for truth valued a and b. */ |
2084 | (simplify | |
2085 | (eq:c (bit_not:s truth_valued_p@0) truth_valued_p@1) | |
2086 | (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)) | |
2087 | && TYPE_PRECISION (TREE_TYPE (@0)) == 1) | |
2088 | (convert (bit_xor @0 @1)))) | |
2089 | ||
f52baa7b MP |
2090 | /* (x & ~m) | (y & m) -> ((x ^ y) & m) ^ x */ |
2091 | (simplify | |
44fc0a51 RB |
2092 | (bit_ior:c (bit_and:cs @0 (bit_not @2)) (bit_and:cs @1 @2)) |
2093 | (bit_xor (bit_and (bit_xor @0 @1) @2) @0)) | |
f52baa7b | 2094 | |
f7b7b0aa MP |
2095 | /* Fold A - (A & B) into ~B & A. */ |
2096 | (simplify | |
2eef1fc1 | 2097 | (minus (convert1? @0) (convert2?:s (bit_and:cs @@0 @1))) |
f7b7b0aa MP |
2098 | (if (tree_nop_conversion_p (type, TREE_TYPE (@0)) |
2099 | && tree_nop_conversion_p (type, TREE_TYPE (@1))) | |
2100 | (convert (bit_and (bit_not @1) @0)))) | |
5609420f | 2101 | |
2071f8f9 | 2102 | /* (m1 CMP m2) * d -> (m1 CMP m2) ? d : 0 */ |
527e54a4 | 2103 | (if (!canonicalize_math_p ()) |
fd1f5373 | 2104 | (for cmp (tcc_comparison) |
527e54a4 | 2105 | (simplify |
fd1f5373 RS |
2106 | (mult:c (convert (cmp@0 @1 @2)) @3) |
2107 | (if (INTEGRAL_TYPE_P (type) | |
2108 | && INTEGRAL_TYPE_P (TREE_TYPE (@0))) | |
2109 | (cond @0 @3 { build_zero_cst (type); }))) | |
2110 | /* (-(m1 CMP m2)) & d -> (m1 CMP m2) ? d : 0 */ | |
2111 | (simplify | |
2112 | (bit_and:c (negate (convert (cmp@0 @1 @2))) @3) | |
2113 | (if (INTEGRAL_TYPE_P (type) | |
2114 | && INTEGRAL_TYPE_P (TREE_TYPE (@0))) | |
2115 | (cond @0 @3 { build_zero_cst (type); }))) | |
2116 | ) | |
2117 | ) | |
2071f8f9 | 2118 | |
e36c1cfe N |
2119 | /* For integral types with undefined overflow and C != 0 fold |
2120 | x * C EQ/NE y * C into x EQ/NE y. */ | |
2121 | (for cmp (eq ne) | |
2122 | (simplify | |
2123 | (cmp (mult:c @0 @1) (mult:c @2 @1)) | |
2124 | (if (INTEGRAL_TYPE_P (TREE_TYPE (@1)) | |
2125 | && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)) | |
2126 | && tree_expr_nonzero_p (@1)) | |
2127 | (cmp @0 @2)))) | |
2128 | ||
42bd89ce MG |
2129 | /* For integral types with wrapping overflow and C odd fold |
2130 | x * C EQ/NE y * C into x EQ/NE y. */ | |
2131 | (for cmp (eq ne) | |
2132 | (simplify | |
2133 | (cmp (mult @0 INTEGER_CST@1) (mult @2 @1)) | |
2134 | (if (INTEGRAL_TYPE_P (TREE_TYPE (@1)) | |
2135 | && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)) | |
2136 | && (TREE_INT_CST_LOW (@1) & 1) != 0) | |
2137 | (cmp @0 @2)))) | |
2138 | ||
e36c1cfe N |
2139 | /* For integral types with undefined overflow and C != 0 fold |
2140 | x * C RELOP y * C into: | |
84ff66b8 | 2141 | |
e36c1cfe N |
2142 | x RELOP y for nonnegative C |
2143 | y RELOP x for negative C */ | |
2144 | (for cmp (lt gt le ge) | |
2145 | (simplify | |
2146 | (cmp (mult:c @0 @1) (mult:c @2 @1)) | |
2147 | (if (INTEGRAL_TYPE_P (TREE_TYPE (@1)) | |
2148 | && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))) | |
2149 | (if (tree_expr_nonnegative_p (@1) && tree_expr_nonzero_p (@1)) | |
2150 | (cmp @0 @2) | |
2151 | (if (TREE_CODE (@1) == INTEGER_CST | |
8e6cdc90 | 2152 | && wi::neg_p (wi::to_wide (@1), TYPE_SIGN (TREE_TYPE (@1)))) |
e36c1cfe | 2153 | (cmp @2 @0)))))) |
84ff66b8 | 2154 | |
564e405c JJ |
2155 | /* (X - 1U) <= INT_MAX-1U into (int) X > 0. */ |
2156 | (for cmp (le gt) | |
2157 | icmp (gt le) | |
2158 | (simplify | |
2159 | (cmp (plus @0 integer_minus_onep@1) INTEGER_CST@2) | |
2160 | (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)) | |
2161 | && TYPE_UNSIGNED (TREE_TYPE (@0)) | |
2162 | && TYPE_PRECISION (TREE_TYPE (@0)) > 1 | |
8e6cdc90 RS |
2163 | && (wi::to_wide (@2) |
2164 | == wi::max_value (TYPE_PRECISION (TREE_TYPE (@0)), SIGNED) - 1)) | |
564e405c JJ |
2165 | (with { tree stype = signed_type_for (TREE_TYPE (@0)); } |
2166 | (icmp (convert:stype @0) { build_int_cst (stype, 0); }))))) | |
2167 | ||
a8492d5e MG |
2168 | /* X / 4 < Y / 4 iff X < Y when the division is known to be exact. */ |
2169 | (for cmp (simple_comparison) | |
2170 | (simplify | |
9adfa8e2 MG |
2171 | (cmp (convert?@3 (exact_div @0 INTEGER_CST@2)) (convert? (exact_div @1 @2))) |
2172 | (if (element_precision (@3) >= element_precision (@0) | |
2173 | && types_match (@0, @1)) | |
9cf60d3b | 2174 | (if (wi::lt_p (wi::to_wide (@2), 0, TYPE_SIGN (TREE_TYPE (@2)))) |
9adfa8e2 MG |
2175 | (if (!TYPE_UNSIGNED (TREE_TYPE (@3))) |
2176 | (cmp @1 @0) | |
2177 | (if (tree_expr_nonzero_p (@0) && tree_expr_nonzero_p (@1)) | |
2178 | (with | |
2179 | { | |
2180 | tree utype = unsigned_type_for (TREE_TYPE (@0)); | |
2181 | } | |
2182 | (cmp (convert:utype @1) (convert:utype @0))))) | |
2183 | (if (wi::gt_p (wi::to_wide (@2), 1, TYPE_SIGN (TREE_TYPE (@2)))) | |
2184 | (if (TYPE_UNSIGNED (TREE_TYPE (@0)) || !TYPE_UNSIGNED (TREE_TYPE (@3))) | |
2185 | (cmp @0 @1) | |
2186 | (with | |
2187 | { | |
2188 | tree utype = unsigned_type_for (TREE_TYPE (@0)); | |
2189 | } | |
2190 | (cmp (convert:utype @0) (convert:utype @1))))))))) | |
a8492d5e | 2191 | |
8d1628eb JJ |
2192 | /* X / C1 op C2 into a simple range test. */ |
2193 | (for cmp (simple_comparison) | |
2194 | (simplify | |
2195 | (cmp (trunc_div:s @0 INTEGER_CST@1) INTEGER_CST@2) | |
2196 | (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)) | |
2197 | && integer_nonzerop (@1) | |
2198 | && !TREE_OVERFLOW (@1) | |
2199 | && !TREE_OVERFLOW (@2)) | |
2200 | (with { tree lo, hi; bool neg_overflow; | |
2201 | enum tree_code code = fold_div_compare (cmp, @1, @2, &lo, &hi, | |
2202 | &neg_overflow); } | |
2203 | (switch | |
2204 | (if (code == LT_EXPR || code == GE_EXPR) | |
2205 | (if (TREE_OVERFLOW (lo)) | |
2206 | { build_int_cst (type, (code == LT_EXPR) ^ neg_overflow); } | |
2207 | (if (code == LT_EXPR) | |
2208 | (lt @0 { lo; }) | |
2209 | (ge @0 { lo; })))) | |
2210 | (if (code == LE_EXPR || code == GT_EXPR) | |
2211 | (if (TREE_OVERFLOW (hi)) | |
2212 | { build_int_cst (type, (code == LE_EXPR) ^ neg_overflow); } | |
2213 | (if (code == LE_EXPR) | |
2214 | (le @0 { hi; }) | |
2215 | (gt @0 { hi; })))) | |
2216 | (if (!lo && !hi) | |
2217 | { build_int_cst (type, code == NE_EXPR); }) | |
2218 | (if (code == EQ_EXPR && !hi) | |
2219 | (ge @0 { lo; })) | |
2220 | (if (code == EQ_EXPR && !lo) | |
2221 | (le @0 { hi; })) | |
2222 | (if (code == NE_EXPR && !hi) | |
2223 | (lt @0 { lo; })) | |
2224 | (if (code == NE_EXPR && !lo) | |
2225 | (gt @0 { hi; })) | |
2226 | (if (GENERIC) | |
2227 | { build_range_check (UNKNOWN_LOCATION, type, @0, code == EQ_EXPR, | |
2228 | lo, hi); }) | |
2229 | (with | |
2230 | { | |
2231 | tree etype = range_check_type (TREE_TYPE (@0)); | |
2232 | if (etype) | |
2233 | { | |
8d1628eb JJ |
2234 | hi = fold_convert (etype, hi); |
2235 | lo = fold_convert (etype, lo); | |
2236 | hi = const_binop (MINUS_EXPR, etype, hi, lo); | |
2237 | } | |
2238 | } | |
2239 | (if (etype && hi && !TREE_OVERFLOW (hi)) | |
2240 | (if (code == EQ_EXPR) | |
2241 | (le (minus (convert:etype @0) { lo; }) { hi; }) | |
2242 | (gt (minus (convert:etype @0) { lo; }) { hi; }))))))))) | |
2243 | ||
d35256b6 MG |
2244 | /* X + Z < Y + Z is the same as X < Y when there is no overflow. */ |
2245 | (for op (lt le ge gt) | |
2246 | (simplify | |
2247 | (op (plus:c @0 @2) (plus:c @1 @2)) | |
2248 | (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0)) | |
2249 | && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))) | |
2250 | (op @0 @1)))) | |
ab981aab AS |
2251 | |
2252 | /* As a special case, X + C < Y + C is the same as (signed) X < (signed) Y | |
2253 | when C is an unsigned integer constant with only the MSB set, and X and | |
2254 | Y have types of equal or lower integer conversion rank than C's. */ | |
2255 | (for op (lt le ge gt) | |
2256 | (simplify | |
2257 | (op (plus @1 INTEGER_CST@0) (plus @2 @0)) | |
2258 | (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)) | |
2259 | && TYPE_UNSIGNED (TREE_TYPE (@0)) | |
2260 | && wi::only_sign_bit_p (wi::to_wide (@0))) | |
2261 | (with { tree stype = signed_type_for (TREE_TYPE (@0)); } | |
2262 | (op (convert:stype @1) (convert:stype @2)))))) | |
2263 | ||
d35256b6 MG |
2264 | /* For equality and subtraction, this is also true with wrapping overflow. */ |
2265 | (for op (eq ne minus) | |
2266 | (simplify | |
2267 | (op (plus:c @0 @2) (plus:c @1 @2)) | |
2268 | (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0)) | |
2269 | && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)) | |
2270 | || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))) | |
2271 | (op @0 @1)))) | |
2272 | ||
2273 | /* X - Z < Y - Z is the same as X < Y when there is no overflow. */ | |
2274 | (for op (lt le ge gt) | |
2275 | (simplify | |
2276 | (op (minus @0 @2) (minus @1 @2)) | |
2277 | (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0)) | |
2278 | && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))) | |
2279 | (op @0 @1)))) | |
2280 | /* For equality and subtraction, this is also true with wrapping overflow. */ | |
2281 | (for op (eq ne minus) | |
2282 | (simplify | |
2283 | (op (minus @0 @2) (minus @1 @2)) | |
2284 | (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0)) | |
2285 | && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)) | |
2286 | || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))) | |
2287 | (op @0 @1)))) | |
1af4ebf5 MG |
2288 | /* And for pointers... */ |
2289 | (for op (simple_comparison) | |
2290 | (simplify | |
2291 | (op (pointer_diff@3 @0 @2) (pointer_diff @1 @2)) | |
2292 | (if (!TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2))) | |
2293 | (op @0 @1)))) | |
2294 | (simplify | |
2295 | (minus (pointer_diff@3 @0 @2) (pointer_diff @1 @2)) | |
2296 | (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@3)) | |
2297 | && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2))) | |
2298 | (pointer_diff @0 @1))) | |
d35256b6 MG |
2299 | |
2300 | /* Z - X < Z - Y is the same as Y < X when there is no overflow. */ | |
2301 | (for op (lt le ge gt) | |
2302 | (simplify | |
2303 | (op (minus @2 @0) (minus @2 @1)) | |
2304 | (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0)) | |
2305 | && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))) | |
2306 | (op @1 @0)))) | |
2307 | /* For equality and subtraction, this is also true with wrapping overflow. */ | |
2308 | (for op (eq ne minus) | |
2309 | (simplify | |
2310 | (op (minus @2 @0) (minus @2 @1)) | |
2311 | (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0)) | |
2312 | && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)) | |
2313 | || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))) | |
2314 | (op @1 @0)))) | |
1af4ebf5 MG |
2315 | /* And for pointers... */ |
2316 | (for op (simple_comparison) | |
2317 | (simplify | |
2318 | (op (pointer_diff@3 @2 @0) (pointer_diff @2 @1)) | |
2319 | (if (!TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2))) | |
2320 | (op @1 @0)))) | |
2321 | (simplify | |
2322 | (minus (pointer_diff@3 @2 @0) (pointer_diff @2 @1)) | |
2323 | (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@3)) | |
2324 | && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2))) | |
2325 | (pointer_diff @1 @0))) | |
d35256b6 | 2326 | |
6358a676 MG |
2327 | /* X + Y < Y is the same as X < 0 when there is no overflow. */ |
2328 | (for op (lt le gt ge) | |
2329 | (simplify | |
2330 | (op:c (plus:c@2 @0 @1) @1) | |
2331 | (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0)) | |
2332 | && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)) | |
cbd42900 | 2333 | && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@0)) |
6358a676 MG |
2334 | && (CONSTANT_CLASS_P (@0) || single_use (@2))) |
2335 | (op @0 { build_zero_cst (TREE_TYPE (@0)); })))) | |
2336 | /* For equality, this is also true with wrapping overflow. */ | |
2337 | (for op (eq ne) | |
2338 | (simplify | |
e150da38 | 2339 | (op:c (nop_convert?@3 (plus:c@2 @0 (convert1? @1))) (convert2? @1)) |
6358a676 MG |
2340 | (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0)) |
2341 | && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)) | |
2342 | || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))) | |
2343 | && (CONSTANT_CLASS_P (@0) || (single_use (@2) && single_use (@3))) | |
2344 | && tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@2)) | |
2345 | && tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@1))) | |
2346 | (op @0 { build_zero_cst (TREE_TYPE (@0)); }))) | |
2347 | (simplify | |
e150da38 | 2348 | (op:c (nop_convert?@3 (pointer_plus@2 (convert1? @0) @1)) (convert2? @0)) |
6358a676 MG |
2349 | (if (tree_nop_conversion_p (TREE_TYPE (@2), TREE_TYPE (@0)) |
2350 | && tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@0)) | |
2351 | && (CONSTANT_CLASS_P (@1) || (single_use (@2) && single_use (@3)))) | |
2352 | (op @1 { build_zero_cst (TREE_TYPE (@1)); })))) | |
2353 | ||
892e8c52 RB |
2354 | /* (&a + b) !=/== (&a[1] + c) -> (&a[0] - &a[1]) + b !=/== c */ |
2355 | (for neeq (ne eq) | |
2356 | (simplify | |
2dc5d6b1 | 2357 | (neeq:c ADDR_EXPR@0 (pointer_plus @2 @3)) |
892e8c52 RB |
2358 | (with { poly_int64 diff; tree inner_type = TREE_TYPE (@3);} |
2359 | (if (ptr_difference_const (@0, @2, &diff)) | |
2360 | (neeq { build_int_cst_type (inner_type, diff); } @3)))) | |
2361 | (simplify | |
2362 | (neeq (pointer_plus ADDR_EXPR@0 @1) (pointer_plus ADDR_EXPR@2 @3)) | |
2363 | (with { poly_int64 diff; tree inner_type = TREE_TYPE (@1);} | |
2364 | (if (ptr_difference_const (@0, @2, &diff)) | |
2365 | (neeq (plus { build_int_cst_type (inner_type, diff); } @1) @3))))) | |
2366 | ||
6358a676 MG |
2367 | /* X - Y < X is the same as Y > 0 when there is no overflow. |
2368 | For equality, this is also true with wrapping overflow. */ | |
2369 | (for op (simple_comparison) | |
2370 | (simplify | |
2371 | (op:c @0 (minus@2 @0 @1)) | |
2372 | (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0)) | |
2373 | && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)) | |
2374 | || ((op == EQ_EXPR || op == NE_EXPR) | |
2375 | && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))) | |
2376 | && (CONSTANT_CLASS_P (@1) || single_use (@2))) | |
2377 | (op @1 { build_zero_cst (TREE_TYPE (@1)); })))) | |
2378 | ||
1d6fadee | 2379 | /* Transform: |
b8d85005 JJ |
2380 | (X / Y) == 0 -> X < Y if X, Y are unsigned. |
2381 | (X / Y) != 0 -> X >= Y, if X, Y are unsigned. */ | |
1d6fadee PK |
2382 | (for cmp (eq ne) |
2383 | ocmp (lt ge) | |
2384 | (simplify | |
2385 | (cmp (trunc_div @0 @1) integer_zerop) | |
2386 | (if (TYPE_UNSIGNED (TREE_TYPE (@0)) | |
b8d85005 JJ |
2387 | /* Complex ==/!= is allowed, but not </>=. */ |
2388 | && TREE_CODE (TREE_TYPE (@0)) != COMPLEX_TYPE | |
1d6fadee PK |
2389 | && (VECTOR_TYPE_P (type) || !VECTOR_TYPE_P (TREE_TYPE (@0)))) |
2390 | (ocmp @0 @1)))) | |
2391 | ||
8b656ca7 MG |
2392 | /* X == C - X can never be true if C is odd. */ |
2393 | (for cmp (eq ne) | |
2394 | (simplify | |
2395 | (cmp:c (convert? @0) (convert1? (minus INTEGER_CST@1 (convert2? @0)))) | |
2396 | (if (TREE_INT_CST_LOW (@1) & 1) | |
2397 | { constant_boolean_node (cmp == NE_EXPR, type); }))) | |
2398 | ||
10bc8017 MG |
2399 | /* Arguments on which one can call get_nonzero_bits to get the bits |
2400 | possibly set. */ | |
2401 | (match with_possible_nonzero_bits | |
2402 | INTEGER_CST@0) | |
2403 | (match with_possible_nonzero_bits | |
2404 | SSA_NAME@0 | |
2405 | (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0))))) | |
2406 | /* Slightly extended version, do not make it recursive to keep it cheap. */ | |
2407 | (match (with_possible_nonzero_bits2 @0) | |
2408 | with_possible_nonzero_bits@0) | |
2409 | (match (with_possible_nonzero_bits2 @0) | |
2410 | (bit_and:c with_possible_nonzero_bits@0 @2)) | |
2411 | ||
2412 | /* Same for bits that are known to be set, but we do not have | |
2413 | an equivalent to get_nonzero_bits yet. */ | |
2414 | (match (with_certain_nonzero_bits2 @0) | |
2415 | INTEGER_CST@0) | |
2416 | (match (with_certain_nonzero_bits2 @0) | |
2417 | (bit_ior @1 INTEGER_CST@0)) | |
2418 | ||
2419 | /* X == C (or X & Z == Y | C) is impossible if ~nonzero(X) & C != 0. */ | |
2420 | (for cmp (eq ne) | |
2421 | (simplify | |
2422 | (cmp:c (with_possible_nonzero_bits2 @0) (with_certain_nonzero_bits2 @1)) | |
8e6cdc90 | 2423 | (if (wi::bit_and_not (wi::to_wide (@1), get_nonzero_bits (@0)) != 0) |
10bc8017 MG |
2424 | { constant_boolean_node (cmp == NE_EXPR, type); }))) |
2425 | ||
84ff66b8 AV |
2426 | /* ((X inner_op C0) outer_op C1) |
2427 | With X being a tree where value_range has reasoned certain bits to always be | |
2428 | zero throughout its computed value range, | |
2429 | inner_op = {|,^}, outer_op = {|,^} and inner_op != outer_op | |
2430 | where zero_mask has 1's for all bits that are sure to be 0 in | |
2431 | and 0's otherwise. | |
2432 | if (inner_op == '^') C0 &= ~C1; | |
2433 | if ((C0 & ~zero_mask) == 0) then emit (X outer_op (C0 outer_op C1) | |
2434 | if ((C1 & ~zero_mask) == 0) then emit (X inner_op (C0 outer_op C1) | |
2435 | */ | |
2436 | (for inner_op (bit_ior bit_xor) | |
2437 | outer_op (bit_xor bit_ior) | |
2438 | (simplify | |
2439 | (outer_op | |
2440 | (inner_op:s @2 INTEGER_CST@0) INTEGER_CST@1) | |
2441 | (with | |
2442 | { | |
2443 | bool fail = false; | |
2444 | wide_int zero_mask_not; | |
2445 | wide_int C0; | |
2446 | wide_int cst_emit; | |
2447 | ||
2448 | if (TREE_CODE (@2) == SSA_NAME) | |
2449 | zero_mask_not = get_nonzero_bits (@2); | |
2450 | else | |
2451 | fail = true; | |
2452 | ||
2453 | if (inner_op == BIT_XOR_EXPR) | |
2454 | { | |
8e6cdc90 RS |
2455 | C0 = wi::bit_and_not (wi::to_wide (@0), wi::to_wide (@1)); |
2456 | cst_emit = C0 | wi::to_wide (@1); | |
84ff66b8 AV |
2457 | } |
2458 | else | |
2459 | { | |
8e6cdc90 RS |
2460 | C0 = wi::to_wide (@0); |
2461 | cst_emit = C0 ^ wi::to_wide (@1); | |
84ff66b8 AV |
2462 | } |
2463 | } | |
8e6cdc90 | 2464 | (if (!fail && (C0 & zero_mask_not) == 0) |
84ff66b8 | 2465 | (outer_op @2 { wide_int_to_tree (type, cst_emit); }) |
8e6cdc90 | 2466 | (if (!fail && (wi::to_wide (@1) & zero_mask_not) == 0) |
84ff66b8 AV |
2467 | (inner_op @2 { wide_int_to_tree (type, cst_emit); })))))) |
2468 | ||
a499aac5 RB |
2469 | /* Associate (p +p off1) +p off2 as (p +p (off1 + off2)). */ |
2470 | (simplify | |
44fc0a51 RB |
2471 | (pointer_plus (pointer_plus:s @0 @1) @3) |
2472 | (pointer_plus @0 (plus @1 @3))) | |
51d464b6 JJ |
2473 | #if GENERIC |
2474 | (simplify | |
2475 | (pointer_plus (convert:s (pointer_plus:s @0 @1)) @3) | |
2476 | (convert:type (pointer_plus @0 (plus @1 @3)))) | |
2477 | #endif | |
a499aac5 RB |
2478 | |
2479 | /* Pattern match | |
2480 | tem1 = (long) ptr1; | |
2481 | tem2 = (long) ptr2; | |
2482 | tem3 = tem2 - tem1; | |
2483 | tem4 = (unsigned long) tem3; | |
2484 | tem5 = ptr1 + tem4; | |
2485 | and produce | |
2486 | tem5 = ptr2; */ | |
2487 | (simplify | |
2488 | (pointer_plus @0 (convert?@2 (minus@3 (convert @1) (convert @0)))) | |
2489 | /* Conditionally look through a sign-changing conversion. */ | |
2490 | (if (TYPE_PRECISION (TREE_TYPE (@2)) == TYPE_PRECISION (TREE_TYPE (@3)) | |
2491 | && ((GIMPLE && useless_type_conversion_p (type, TREE_TYPE (@1))) | |
2492 | || (GENERIC && type == TREE_TYPE (@1)))) | |
2493 | @1)) | |
1af4ebf5 MG |
2494 | (simplify |
2495 | (pointer_plus @0 (convert?@2 (pointer_diff@3 @1 @@0))) | |
2496 | (if (TYPE_PRECISION (TREE_TYPE (@2)) >= TYPE_PRECISION (TREE_TYPE (@3))) | |
2497 | (convert @1))) | |
a499aac5 RB |
2498 | |
2499 | /* Pattern match | |
2500 | tem = (sizetype) ptr; | |
2501 | tem = tem & algn; | |
2502 | tem = -tem; | |
2503 | ... = ptr p+ tem; | |
2504 | and produce the simpler and easier to analyze with respect to alignment | |
2505 | ... = ptr & ~algn; */ | |
2506 | (simplify | |
2507 | (pointer_plus @0 (negate (bit_and (convert @0) INTEGER_CST@1))) | |
8e6cdc90 | 2508 | (with { tree algn = wide_int_to_tree (TREE_TYPE (@0), ~wi::to_wide (@1)); } |
a499aac5 RB |
2509 | (bit_and @0 { algn; }))) |
2510 | ||
99e943a2 RB |
2511 | /* Try folding difference of addresses. */ |
2512 | (simplify | |
2513 | (minus (convert ADDR_EXPR@0) (convert @1)) | |
2514 | (if (tree_nop_conversion_p (type, TREE_TYPE (@0))) | |
f37fac2b | 2515 | (with { poly_int64 diff; } |
99e943a2 RB |
2516 | (if (ptr_difference_const (@0, @1, &diff)) |
2517 | { build_int_cst_type (type, diff); })))) | |
2518 | (simplify | |
2519 | (minus (convert @0) (convert ADDR_EXPR@1)) | |
2520 | (if (tree_nop_conversion_p (type, TREE_TYPE (@0))) | |
f37fac2b | 2521 | (with { poly_int64 diff; } |
99e943a2 RB |
2522 | (if (ptr_difference_const (@0, @1, &diff)) |
2523 | { build_int_cst_type (type, diff); })))) | |
1af4ebf5 | 2524 | (simplify |
67fccea4 | 2525 | (pointer_diff (convert?@2 ADDR_EXPR@0) (convert1?@3 @1)) |
1af4ebf5 MG |
2526 | (if (tree_nop_conversion_p (TREE_TYPE(@2), TREE_TYPE (@0)) |
2527 | && tree_nop_conversion_p (TREE_TYPE(@3), TREE_TYPE (@1))) | |
f37fac2b | 2528 | (with { poly_int64 diff; } |
1af4ebf5 MG |
2529 | (if (ptr_difference_const (@0, @1, &diff)) |
2530 | { build_int_cst_type (type, diff); })))) | |
2531 | (simplify | |
67fccea4 | 2532 | (pointer_diff (convert?@2 @0) (convert1?@3 ADDR_EXPR@1)) |
1af4ebf5 MG |
2533 | (if (tree_nop_conversion_p (TREE_TYPE(@2), TREE_TYPE (@0)) |
2534 | && tree_nop_conversion_p (TREE_TYPE(@3), TREE_TYPE (@1))) | |
f37fac2b | 2535 | (with { poly_int64 diff; } |
1af4ebf5 MG |
2536 | (if (ptr_difference_const (@0, @1, &diff)) |
2537 | { build_int_cst_type (type, diff); })))) | |
99e943a2 | 2538 | |
564efbf4 AP |
2539 | /* (&a+b) - (&a[1] + c) -> sizeof(a[0]) + (b - c) */ |
2540 | (simplify | |
2541 | (pointer_diff (pointer_plus ADDR_EXPR@0 @1) (pointer_plus ADDR_EXPR@2 @3)) | |
2542 | (with { poly_int64 diff; } | |
2543 | (if (ptr_difference_const (@0, @2, &diff)) | |
2544 | (plus { build_int_cst_type (type, diff); } (convert (minus @1 @3)))))) | |
d13b86f9 RB |
2545 | /* (p + b) - &p->d -> offsetof (*p, d) + b */ |
2546 | (simplify | |
2547 | (pointer_diff (pointer_plus @0 @1) ADDR_EXPR@2) | |
2548 | (with { poly_int64 diff; } | |
2549 | (if (ptr_difference_const (@0, @2, &diff)) | |
2550 | (plus { build_int_cst_type (type, diff); } (convert @1))))) | |
2551 | (simplify | |
2552 | (pointer_diff ADDR_EXPR@0 (pointer_plus @1 @2)) | |
2553 | (with { poly_int64 diff; } | |
2554 | (if (ptr_difference_const (@0, @1, &diff)) | |
2555 | (minus { build_int_cst_type (type, diff); } (convert @2))))) | |
564efbf4 | 2556 | |
cb99630f RB |
2557 | /* Canonicalize (T *)(ptr - ptr-cst) to &MEM[ptr + -ptr-cst]. */ |
2558 | (simplify | |
2559 | (convert (pointer_diff @0 INTEGER_CST@1)) | |
2560 | (if (POINTER_TYPE_P (type)) | |
2561 | { build_fold_addr_expr_with_type | |
2562 | (build2 (MEM_REF, char_type_node, @0, | |
2563 | wide_int_to_tree (ptr_type_node, wi::neg (wi::to_wide (@1)))), | |
2564 | type); })) | |
2565 | ||
bab73f11 RB |
2566 | /* If arg0 is derived from the address of an object or function, we may |
2567 | be able to fold this expression using the object or function's | |
2568 | alignment. */ | |
2569 | (simplify | |
2570 | (bit_and (convert? @0) INTEGER_CST@1) | |
2571 | (if (POINTER_TYPE_P (TREE_TYPE (@0)) | |
2572 | && tree_nop_conversion_p (type, TREE_TYPE (@0))) | |
2573 | (with | |
2574 | { | |
2575 | unsigned int align; | |
2576 | unsigned HOST_WIDE_INT bitpos; | |
2577 | get_pointer_alignment_1 (@0, &align, &bitpos); | |
2578 | } | |
8e6cdc90 RS |
2579 | (if (wi::ltu_p (wi::to_wide (@1), align / BITS_PER_UNIT)) |
2580 | { wide_int_to_tree (type, (wi::to_wide (@1) | |
2581 | & (bitpos / BITS_PER_UNIT))); })))) | |
99e943a2 | 2582 | |
c16504f6 LJH |
2583 | (match min_value |
2584 | INTEGER_CST | |
2585 | (if (INTEGRAL_TYPE_P (type) | |
2586 | && wi::eq_p (wi::to_wide (t), wi::min_value (type))))) | |
2587 | ||
2588 | (match max_value | |
2589 | INTEGER_CST | |
2590 | (if (INTEGRAL_TYPE_P (type) | |
2591 | && wi::eq_p (wi::to_wide (t), wi::max_value (type))))) | |
2592 | ||
2593 | /* x > y && x != XXX_MIN --> x > y | |
2594 | x > y && x == XXX_MIN --> false . */ | |
2595 | (for eqne (eq ne) | |
2596 | (simplify | |
2597 | (bit_and:c (gt:c@2 @0 @1) (eqne @0 min_value)) | |
2598 | (switch | |
2599 | (if (eqne == EQ_EXPR) | |
2600 | { constant_boolean_node (false, type); }) | |
2601 | (if (eqne == NE_EXPR) | |
2602 | @2) | |
2603 | ))) | |
2604 | ||
2605 | /* x < y && x != XXX_MAX --> x < y | |
2606 | x < y && x == XXX_MAX --> false. */ | |
2607 | (for eqne (eq ne) | |
2608 | (simplify | |
2609 | (bit_and:c (lt:c@2 @0 @1) (eqne @0 max_value)) | |
2610 | (switch | |
2611 | (if (eqne == EQ_EXPR) | |
2612 | { constant_boolean_node (false, type); }) | |
2613 | (if (eqne == NE_EXPR) | |
2614 | @2) | |
2615 | ))) | |
2616 | ||
2617 | /* x <= y && x == XXX_MIN --> x == XXX_MIN. */ | |
2618 | (simplify | |
2619 | (bit_and:c (le:c @0 @1) (eq@2 @0 min_value)) | |
2620 | @2) | |
2621 | ||
2622 | /* x >= y && x == XXX_MAX --> x == XXX_MAX. */ | |
2623 | (simplify | |
2624 | (bit_and:c (ge:c @0 @1) (eq@2 @0 max_value)) | |
2625 | @2) | |
2626 | ||
2627 | /* x > y || x != XXX_MIN --> x != XXX_MIN. */ | |
2628 | (simplify | |
2629 | (bit_ior:c (gt:c @0 @1) (ne@2 @0 min_value)) | |
2630 | @2) | |
2631 | ||
2632 | /* x <= y || x != XXX_MIN --> true. */ | |
2633 | (simplify | |
2634 | (bit_ior:c (le:c @0 @1) (ne @0 min_value)) | |
2635 | { constant_boolean_node (true, type); }) | |
2636 | ||
2637 | /* x <= y || x == XXX_MIN --> x <= y. */ | |
2638 | (simplify | |
2639 | (bit_ior:c (le:c@2 @0 @1) (eq @0 min_value)) | |
2640 | @2) | |
2641 | ||
2642 | /* x < y || x != XXX_MAX --> x != XXX_MAX. */ | |
2643 | (simplify | |
2644 | (bit_ior:c (lt:c @0 @1) (ne@2 @0 max_value)) | |
2645 | @2) | |
2646 | ||
2647 | /* x >= y || x != XXX_MAX --> true | |
2648 | x >= y || x == XXX_MAX --> x >= y. */ | |
2649 | (for eqne (eq ne) | |
2650 | (simplify | |
2651 | (bit_ior:c (ge:c@2 @0 @1) (eqne @0 max_value)) | |
2652 | (switch | |
2653 | (if (eqne == EQ_EXPR) | |
2654 | @2) | |
2655 | (if (eqne == NE_EXPR) | |
2656 | { constant_boolean_node (true, type); })))) | |
a499aac5 | 2657 | |
49e8c14e ER |
2658 | /* y == XXX_MIN || x < y --> x <= y - 1 */ |
2659 | (simplify | |
9642d07c | 2660 | (bit_ior:c (eq:s @1 min_value) (lt:cs @0 @1)) |
49e8c14e ER |
2661 | (if (INTEGRAL_TYPE_P (TREE_TYPE (@1)) |
2662 | && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1))) | |
2663 | (le @0 (minus @1 { build_int_cst (TREE_TYPE (@1), 1); })))) | |
2664 | ||
2665 | /* y != XXX_MIN && x >= y --> x > y - 1 */ | |
2666 | (simplify | |
9642d07c | 2667 | (bit_and:c (ne:s @1 min_value) (ge:cs @0 @1)) |
49e8c14e ER |
2668 | (if (INTEGRAL_TYPE_P (TREE_TYPE (@1)) |
2669 | && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1))) | |
2670 | (gt @0 (minus @1 { build_int_cst (TREE_TYPE (@1), 1); })))) | |
2671 | ||
ae9c3507 ML |
2672 | /* Convert (X == CST1) && (X OP2 CST2) to a known value |
2673 | based on CST1 OP2 CST2. Similarly for (X != CST1). */ | |
2674 | ||
2675 | (for code1 (eq ne) | |
2676 | (for code2 (eq ne lt gt le ge) | |
2677 | (simplify | |
2678 | (bit_and:c (code1@3 @0 INTEGER_CST@1) (code2@4 @0 INTEGER_CST@2)) | |
2679 | (with | |
2680 | { | |
2681 | int cmp = tree_int_cst_compare (@1, @2); | |
2682 | bool val; | |
2683 | switch (code2) | |
2684 | { | |
2685 | case EQ_EXPR: val = (cmp == 0); break; | |
2686 | case NE_EXPR: val = (cmp != 0); break; | |
2687 | case LT_EXPR: val = (cmp < 0); break; | |
2688 | case GT_EXPR: val = (cmp > 0); break; | |
2689 | case LE_EXPR: val = (cmp <= 0); break; | |
2690 | case GE_EXPR: val = (cmp >= 0); break; | |
2691 | default: gcc_unreachable (); | |
2692 | } | |
2693 | } | |
2694 | (switch | |
2695 | (if (code1 == EQ_EXPR && val) @3) | |
2696 | (if (code1 == EQ_EXPR && !val) { constant_boolean_node (false, type); }) | |
2697 | (if (code1 == NE_EXPR && !val) @4)))))) | |
2698 | ||
2699 | /* Convert (X OP1 CST1) && (X OP2 CST2). */ | |
2700 | ||
2701 | (for code1 (lt le gt ge) | |
2702 | (for code2 (lt le gt ge) | |
2703 | (simplify | |
2704 | (bit_and (code1:c@3 @0 INTEGER_CST@1) (code2:c@4 @0 INTEGER_CST@2)) | |
2705 | (with | |
2706 | { | |
2707 | int cmp = tree_int_cst_compare (@1, @2); | |
2708 | } | |
2709 | (switch | |
2710 | /* Choose the more restrictive of two < or <= comparisons. */ | |
2711 | (if ((code1 == LT_EXPR || code1 == LE_EXPR) | |
2712 | && (code2 == LT_EXPR || code2 == LE_EXPR)) | |
2713 | (if ((cmp < 0) || (cmp == 0 && code1 == LT_EXPR)) | |
2714 | @3 | |
2715 | @4)) | |
2716 | /* Likewise chose the more restrictive of two > or >= comparisons. */ | |
2717 | (if ((code1 == GT_EXPR || code1 == GE_EXPR) | |
2718 | && (code2 == GT_EXPR || code2 == GE_EXPR)) | |
2719 | (if ((cmp > 0) || (cmp == 0 && code1 == GT_EXPR)) | |
2720 | @3 | |
2721 | @4)) | |
2722 | /* Check for singleton ranges. */ | |
2723 | (if (cmp == 0 | |
2724 | && ((code1 == LE_EXPR && code2 == GE_EXPR) | |
2725 | || (code1 == GE_EXPR && code2 == LE_EXPR))) | |
2726 | (eq @0 @1)) | |
2727 | /* Check for disjoint ranges. */ | |
2728 | (if (cmp <= 0 | |
2729 | && (code1 == LT_EXPR || code1 == LE_EXPR) | |
2730 | && (code2 == GT_EXPR || code2 == GE_EXPR)) | |
2731 | { constant_boolean_node (false, type); }) | |
2732 | (if (cmp >= 0 | |
2733 | && (code1 == GT_EXPR || code1 == GE_EXPR) | |
2734 | && (code2 == LT_EXPR || code2 == LE_EXPR)) | |
2735 | { constant_boolean_node (false, type); }) | |
2736 | ))))) | |
2737 | ||
130c4034 ML |
2738 | /* Convert (X == CST1) || (X OP2 CST2) to a known value |
2739 | based on CST1 OP2 CST2. Similarly for (X != CST1). */ | |
2740 | ||
2741 | (for code1 (eq ne) | |
2742 | (for code2 (eq ne lt gt le ge) | |
2743 | (simplify | |
2744 | (bit_ior:c (code1@3 @0 INTEGER_CST@1) (code2@4 @0 INTEGER_CST@2)) | |
2745 | (with | |
2746 | { | |
2747 | int cmp = tree_int_cst_compare (@1, @2); | |
2748 | bool val; | |
2749 | switch (code2) | |
2750 | { | |
2751 | case EQ_EXPR: val = (cmp == 0); break; | |
2752 | case NE_EXPR: val = (cmp != 0); break; | |
2753 | case LT_EXPR: val = (cmp < 0); break; | |
2754 | case GT_EXPR: val = (cmp > 0); break; | |
2755 | case LE_EXPR: val = (cmp <= 0); break; | |
2756 | case GE_EXPR: val = (cmp >= 0); break; | |
2757 | default: gcc_unreachable (); | |
2758 | } | |
2759 | } | |
2760 | (switch | |
2761 | (if (code1 == EQ_EXPR && val) @4) | |
2762 | (if (code1 == NE_EXPR && val) { constant_boolean_node (true, type); }) | |
2763 | (if (code1 == NE_EXPR && !val) @3)))))) | |
2764 | ||
cda65821 ML |
2765 | /* Convert (X OP1 CST1) || (X OP2 CST2). */ |
2766 | ||
2767 | (for code1 (lt le gt ge) | |
2768 | (for code2 (lt le gt ge) | |
2769 | (simplify | |
2770 | (bit_ior (code1@3 @0 INTEGER_CST@1) (code2@4 @0 INTEGER_CST@2)) | |
2771 | (with | |
2772 | { | |
2773 | int cmp = tree_int_cst_compare (@1, @2); | |
2774 | } | |
2775 | (switch | |
2776 | /* Choose the more restrictive of two < or <= comparisons. */ | |
2777 | (if ((code1 == LT_EXPR || code1 == LE_EXPR) | |
2778 | && (code2 == LT_EXPR || code2 == LE_EXPR)) | |
2779 | (if ((cmp < 0) || (cmp == 0 && code1 == LT_EXPR)) | |
2780 | @4 | |
2781 | @3)) | |
2782 | /* Likewise chose the more restrictive of two > or >= comparisons. */ | |
2783 | (if ((code1 == GT_EXPR || code1 == GE_EXPR) | |
2784 | && (code2 == GT_EXPR || code2 == GE_EXPR)) | |
2785 | (if ((cmp > 0) || (cmp == 0 && code1 == GT_EXPR)) | |
2786 | @4 | |
2787 | @3)) | |
2788 | /* Check for singleton ranges. */ | |
2789 | (if (cmp == 0 | |
2790 | && ((code1 == LT_EXPR && code2 == GT_EXPR) | |
2791 | || (code1 == GT_EXPR && code2 == LT_EXPR))) | |
2792 | (ne @0 @2)) | |
2793 | /* Check for disjoint ranges. */ | |
2794 | (if (cmp >= 0 | |
2795 | && (code1 == LT_EXPR || code1 == LE_EXPR) | |
2796 | && (code2 == GT_EXPR || code2 == GE_EXPR)) | |
2797 | { constant_boolean_node (true, type); }) | |
2798 | (if (cmp <= 0 | |
2799 | && (code1 == GT_EXPR || code1 == GE_EXPR) | |
2800 | && (code2 == LT_EXPR || code2 == LE_EXPR)) | |
2801 | { constant_boolean_node (true, type); }) | |
2802 | ))))) | |
130c4034 | 2803 | |
cc7b5acf RB |
2804 | /* We can't reassociate at all for saturating types. */ |
2805 | (if (!TYPE_SATURATING (type)) | |
2806 | ||
2807 | /* Contract negates. */ | |
2808 | /* A + (-B) -> A - B */ | |
2809 | (simplify | |
248179b5 RB |
2810 | (plus:c @0 (convert? (negate @1))) |
2811 | /* Apply STRIP_NOPS on the negate. */ | |
2812 | (if (tree_nop_conversion_p (type, TREE_TYPE (@1)) | |
6a4f0678 | 2813 | && !TYPE_OVERFLOW_SANITIZED (type)) |
248179b5 RB |
2814 | (with |
2815 | { | |
2816 | tree t1 = type; | |
2817 | if (INTEGRAL_TYPE_P (type) | |
2818 | && TYPE_OVERFLOW_WRAPS (type) != TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1))) | |
2819 | t1 = TYPE_OVERFLOW_WRAPS (type) ? type : TREE_TYPE (@1); | |
2820 | } | |
2821 | (convert (minus (convert:t1 @0) (convert:t1 @1)))))) | |
cc7b5acf RB |
2822 | /* A - (-B) -> A + B */ |
2823 | (simplify | |
248179b5 RB |
2824 | (minus @0 (convert? (negate @1))) |
2825 | (if (tree_nop_conversion_p (type, TREE_TYPE (@1)) | |
6a4f0678 | 2826 | && !TYPE_OVERFLOW_SANITIZED (type)) |
248179b5 RB |
2827 | (with |
2828 | { | |
2829 | tree t1 = type; | |
2830 | if (INTEGRAL_TYPE_P (type) | |
2831 | && TYPE_OVERFLOW_WRAPS (type) != TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1))) | |
2832 | t1 = TYPE_OVERFLOW_WRAPS (type) ? type : TREE_TYPE (@1); | |
2833 | } | |
2834 | (convert (plus (convert:t1 @0) (convert:t1 @1)))))) | |
63626547 MG |
2835 | /* -(T)(-A) -> (T)A |
2836 | Sign-extension is ok except for INT_MIN, which thankfully cannot | |
2837 | happen without overflow. */ | |
2838 | (simplify | |
2839 | (negate (convert (negate @1))) | |
2840 | (if (INTEGRAL_TYPE_P (type) | |
2841 | && (TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@1)) | |
2842 | || (!TYPE_UNSIGNED (TREE_TYPE (@1)) | |
2843 | && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@1)))) | |
2844 | && !TYPE_OVERFLOW_SANITIZED (type) | |
2845 | && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@1))) | |
a0f12cf8 | 2846 | (convert @1))) |
63626547 MG |
2847 | (simplify |
2848 | (negate (convert negate_expr_p@1)) | |
2849 | (if (SCALAR_FLOAT_TYPE_P (type) | |
2850 | && ((DECIMAL_FLOAT_TYPE_P (type) | |
2851 | == DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@1)) | |
2852 | && TYPE_PRECISION (type) >= TYPE_PRECISION (TREE_TYPE (@1))) | |
2853 | || !HONOR_SIGN_DEPENDENT_ROUNDING (type))) | |
2854 | (convert (negate @1)))) | |
2855 | (simplify | |
e150da38 | 2856 | (negate (nop_convert? (negate @1))) |
63626547 MG |
2857 | (if (!TYPE_OVERFLOW_SANITIZED (type) |
2858 | && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@1))) | |
2859 | (view_convert @1))) | |
cc7b5acf | 2860 | |
7318e44f RB |
2861 | /* We can't reassociate floating-point unless -fassociative-math |
2862 | or fixed-point plus or minus because of saturation to +-Inf. */ | |
2863 | (if ((!FLOAT_TYPE_P (type) || flag_associative_math) | |
2864 | && !FIXED_POINT_TYPE_P (type)) | |
cc7b5acf RB |
2865 | |
2866 | /* Match patterns that allow contracting a plus-minus pair | |
2867 | irrespective of overflow issues. */ | |
2868 | /* (A +- B) - A -> +- B */ | |
2869 | /* (A +- B) -+ B -> A */ | |
2870 | /* A - (A +- B) -> -+ B */ | |
2871 | /* A +- (B -+ A) -> +- B */ | |
2872 | (simplify | |
e150da38 | 2873 | (minus (nop_convert1? (plus:c (nop_convert2? @0) @1)) @0) |
526b4c71 | 2874 | (view_convert @1)) |
cc7b5acf | 2875 | (simplify |
e150da38 | 2876 | (minus (nop_convert1? (minus (nop_convert2? @0) @1)) @0) |
526b4c71 JJ |
2877 | (if (!ANY_INTEGRAL_TYPE_P (type) |
2878 | || TYPE_OVERFLOW_WRAPS (type)) | |
2879 | (negate (view_convert @1)) | |
2880 | (view_convert (negate @1)))) | |
cc7b5acf | 2881 | (simplify |
e150da38 | 2882 | (plus:c (nop_convert1? (minus @0 (nop_convert2? @1))) @1) |
526b4c71 | 2883 | (view_convert @0)) |
cc7b5acf | 2884 | (simplify |
e150da38 | 2885 | (minus @0 (nop_convert1? (plus:c (nop_convert2? @0) @1))) |
526b4c71 JJ |
2886 | (if (!ANY_INTEGRAL_TYPE_P (type) |
2887 | || TYPE_OVERFLOW_WRAPS (type)) | |
2888 | (negate (view_convert @1)) | |
2889 | (view_convert (negate @1)))) | |
cc7b5acf | 2890 | (simplify |
e150da38 | 2891 | (minus @0 (nop_convert1? (minus (nop_convert2? @0) @1))) |
526b4c71 | 2892 | (view_convert @1)) |
1e7df2e6 MG |
2893 | /* (A +- B) + (C - A) -> C +- B */ |
2894 | /* (A + B) - (A - C) -> B + C */ | |
2895 | /* More cases are handled with comparisons. */ | |
2896 | (simplify | |
2897 | (plus:c (plus:c @0 @1) (minus @2 @0)) | |
2898 | (plus @2 @1)) | |
2899 | (simplify | |
2900 | (plus:c (minus @0 @1) (minus @2 @0)) | |
2901 | (minus @2 @1)) | |
1af4ebf5 MG |
2902 | (simplify |
2903 | (plus:c (pointer_diff @0 @1) (pointer_diff @2 @0)) | |
2904 | (if (TYPE_OVERFLOW_UNDEFINED (type) | |
2905 | && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@0))) | |
2906 | (pointer_diff @2 @1))) | |
1e7df2e6 MG |
2907 | (simplify |
2908 | (minus (plus:c @0 @1) (minus @0 @2)) | |
2909 | (plus @1 @2)) | |
cc7b5acf | 2910 | |
ed73f46f MG |
2911 | /* (A +- CST1) +- CST2 -> A + CST3 |
2912 | Use view_convert because it is safe for vectors and equivalent for | |
2913 | scalars. */ | |
cc7b5acf RB |
2914 | (for outer_op (plus minus) |
2915 | (for inner_op (plus minus) | |
ed73f46f | 2916 | neg_inner_op (minus plus) |
cc7b5acf | 2917 | (simplify |
e150da38 | 2918 | (outer_op (nop_convert? (inner_op @0 CONSTANT_CLASS_P@1)) |
ed73f46f MG |
2919 | CONSTANT_CLASS_P@2) |
2920 | /* If one of the types wraps, use that one. */ | |
2921 | (if (!ANY_INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_WRAPS (type)) | |
3eb1eecf JJ |
2922 | /* If all 3 captures are CONSTANT_CLASS_P, punt, as we might recurse |
2923 | forever if something doesn't simplify into a constant. */ | |
2924 | (if (!CONSTANT_CLASS_P (@0)) | |
2925 | (if (outer_op == PLUS_EXPR) | |
2926 | (plus (view_convert @0) (inner_op @2 (view_convert @1))) | |
2927 | (minus (view_convert @0) (neg_inner_op @2 (view_convert @1))))) | |
ed73f46f MG |
2928 | (if (!ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0)) |
2929 | || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))) | |
2930 | (if (outer_op == PLUS_EXPR) | |
2931 | (view_convert (plus @0 (inner_op (view_convert @2) @1))) | |
2932 | (view_convert (minus @0 (neg_inner_op (view_convert @2) @1)))) | |
2933 | /* If the constant operation overflows we cannot do the transform | |
2934 | directly as we would introduce undefined overflow, for example | |
2935 | with (a - 1) + INT_MIN. */ | |
2936 | (if (types_match (type, @0)) | |
2937 | (with { tree cst = const_binop (outer_op == inner_op | |
2938 | ? PLUS_EXPR : MINUS_EXPR, | |
2939 | type, @1, @2); } | |
2940 | (if (cst && !TREE_OVERFLOW (cst)) | |
2941 | (inner_op @0 { cst; } ) | |
2942 | /* X+INT_MAX+1 is X-INT_MIN. */ | |
2943 | (if (INTEGRAL_TYPE_P (type) && cst | |
8e6cdc90 RS |
2944 | && wi::to_wide (cst) == wi::min_value (type)) |
2945 | (neg_inner_op @0 { wide_int_to_tree (type, wi::to_wide (cst)); }) | |
ed73f46f MG |
2946 | /* Last resort, use some unsigned type. */ |
2947 | (with { tree utype = unsigned_type_for (type); } | |
48fcd201 JJ |
2948 | (if (utype) |
2949 | (view_convert (inner_op | |
2950 | (view_convert:utype @0) | |
2951 | (view_convert:utype | |
2952 | { drop_tree_overflow (cst); })))))))))))))) | |
cc7b5acf | 2953 | |
b302f2e0 | 2954 | /* (CST1 - A) +- CST2 -> CST3 - A */ |
cc7b5acf RB |
2955 | (for outer_op (plus minus) |
2956 | (simplify | |
e150da38 | 2957 | (outer_op (nop_convert? (minus CONSTANT_CLASS_P@1 @0)) CONSTANT_CLASS_P@2) |
129bd066 JJ |
2958 | /* If one of the types wraps, use that one. */ |
2959 | (if (!ANY_INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_WRAPS (type)) | |
2960 | /* If all 3 captures are CONSTANT_CLASS_P, punt, as we might recurse | |
2961 | forever if something doesn't simplify into a constant. */ | |
2962 | (if (!CONSTANT_CLASS_P (@0)) | |
2963 | (minus (outer_op (view_convert @1) @2) (view_convert @0))) | |
2964 | (if (!ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0)) | |
2965 | || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))) | |
2966 | (view_convert (minus (outer_op @1 (view_convert @2)) @0)) | |
2967 | (if (types_match (type, @0)) | |
2968 | (with { tree cst = const_binop (outer_op, type, @1, @2); } | |
2969 | (if (cst && !TREE_OVERFLOW (cst)) | |
2970 | (minus { cst; } @0)))))))) | |
2971 | ||
2972 | /* CST1 - (CST2 - A) -> CST3 + A | |
2973 | Use view_convert because it is safe for vectors and equivalent for | |
2974 | scalars. */ | |
b302f2e0 | 2975 | (simplify |
e150da38 | 2976 | (minus CONSTANT_CLASS_P@1 (nop_convert? (minus CONSTANT_CLASS_P@2 @0))) |
129bd066 JJ |
2977 | /* If one of the types wraps, use that one. */ |
2978 | (if (!ANY_INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_WRAPS (type)) | |
2979 | /* If all 3 captures are CONSTANT_CLASS_P, punt, as we might recurse | |
2980 | forever if something doesn't simplify into a constant. */ | |
2981 | (if (!CONSTANT_CLASS_P (@0)) | |
2982 | (plus (view_convert @0) (minus @1 (view_convert @2)))) | |
2983 | (if (!ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0)) | |
2984 | || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))) | |
2985 | (view_convert (plus @0 (minus (view_convert @1) @2))) | |
2986 | (if (types_match (type, @0)) | |
2987 | (with { tree cst = const_binop (MINUS_EXPR, type, @1, @2); } | |
2988 | (if (cst && !TREE_OVERFLOW (cst)) | |
2989 | (plus { cst; } @0))))))) | |
b302f2e0 | 2990 | |
df7d46d9 RD |
2991 | /* ((T)(A)) + CST -> (T)(A + CST) */ |
2992 | #if GIMPLE | |
2993 | (simplify | |
880682e7 | 2994 | (plus (convert:s SSA_NAME@0) INTEGER_CST@1) |
df7d46d9 RD |
2995 | (if (TREE_CODE (TREE_TYPE (@0)) == INTEGER_TYPE |
2996 | && TREE_CODE (type) == INTEGER_TYPE | |
2997 | && TYPE_PRECISION (type) > TYPE_PRECISION (TREE_TYPE (@0)) | |
2998 | && int_fits_type_p (@1, TREE_TYPE (@0))) | |
2999 | /* Perform binary operation inside the cast if the constant fits | |
3000 | and (A + CST)'s range does not overflow. */ | |
3001 | (with | |
3002 | { | |
3003 | wi::overflow_type min_ovf = wi::OVF_OVERFLOW, | |
3004 | max_ovf = wi::OVF_OVERFLOW; | |
3005 | tree inner_type = TREE_TYPE (@0); | |
3006 | ||
81b40582 JJ |
3007 | wide_int w1 |
3008 | = wide_int::from (wi::to_wide (@1), TYPE_PRECISION (inner_type), | |
3009 | TYPE_SIGN (inner_type)); | |
df7d46d9 | 3010 | |
45f4e2b0 AH |
3011 | value_range vr; |
3012 | if (get_global_range_query ()->range_of_expr (vr, @0) | |
3013 | && vr.kind () == VR_RANGE) | |
df7d46d9 | 3014 | { |
45f4e2b0 AH |
3015 | wide_int wmin0 = vr.lower_bound (); |
3016 | wide_int wmax0 = vr.upper_bound (); | |
df7d46d9 RD |
3017 | wi::add (wmin0, w1, TYPE_SIGN (inner_type), &min_ovf); |
3018 | wi::add (wmax0, w1, TYPE_SIGN (inner_type), &max_ovf); | |
3019 | } | |
3020 | } | |
3021 | (if (min_ovf == wi::OVF_NONE && max_ovf == wi::OVF_NONE) | |
3022 | (convert (plus @0 { wide_int_to_tree (TREE_TYPE (@0), w1); } ))) | |
3023 | ))) | |
3024 | #endif | |
3025 | ||
81b40582 JJ |
3026 | /* ((T)(A + CST1)) + CST2 -> (T)(A) + (T)CST1 + CST2 */ |
3027 | #if GIMPLE | |
3028 | (for op (plus minus) | |
3029 | (simplify | |
3030 | (plus (convert:s (op:s @0 INTEGER_CST@1)) INTEGER_CST@2) | |
3031 | (if (TREE_CODE (TREE_TYPE (@0)) == INTEGER_TYPE | |
3032 | && TREE_CODE (type) == INTEGER_TYPE | |
3033 | && TYPE_PRECISION (type) > TYPE_PRECISION (TREE_TYPE (@0)) | |
3034 | && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)) | |
3035 | && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@0)) | |
3036 | && TYPE_OVERFLOW_WRAPS (type)) | |
3037 | (plus (convert @0) (op @2 (convert @1)))))) | |
3038 | #endif | |
3039 | ||
8f0d743c FX |
3040 | /* (T)(A) +- (T)(B) -> (T)(A +- B) only when (A +- B) could be simplified |
3041 | to a simple value. */ | |
8f0d743c FX |
3042 | (for op (plus minus) |
3043 | (simplify | |
3044 | (op (convert @0) (convert @1)) | |
3045 | (if (INTEGRAL_TYPE_P (type) | |
3046 | && INTEGRAL_TYPE_P (TREE_TYPE (@0)) | |
3047 | && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0)) | |
3048 | && types_match (TREE_TYPE (@0), TREE_TYPE (@1)) | |
3049 | && !TYPE_OVERFLOW_TRAPS (type) | |
3050 | && !TYPE_OVERFLOW_SANITIZED (type)) | |
3051 | (convert (op! @0 @1))))) | |
8f0d743c | 3052 | |
cc7b5acf RB |
3053 | /* ~A + A -> -1 */ |
3054 | (simplify | |
cf716ab5 | 3055 | (plus:c (convert? (bit_not @0)) (convert? @0)) |
cc7b5acf | 3056 | (if (!TYPE_OVERFLOW_TRAPS (type)) |
cf716ab5 | 3057 | (convert { build_all_ones_cst (TREE_TYPE (@0)); }))) |
cc7b5acf RB |
3058 | |
3059 | /* ~A + 1 -> -A */ | |
3060 | (simplify | |
e19740ae RB |
3061 | (plus (convert? (bit_not @0)) integer_each_onep) |
3062 | (if (tree_nop_conversion_p (type, TREE_TYPE (@0))) | |
3063 | (negate (convert @0)))) | |
3064 | ||
3065 | /* -A - 1 -> ~A */ | |
3066 | (simplify | |
3067 | (minus (convert? (negate @0)) integer_each_onep) | |
3068 | (if (!TYPE_OVERFLOW_TRAPS (type) | |
758671b8 | 3069 | && TREE_CODE (type) != COMPLEX_TYPE |
e19740ae RB |
3070 | && tree_nop_conversion_p (type, TREE_TYPE (@0))) |
3071 | (bit_not (convert @0)))) | |
3072 | ||
3073 | /* -1 - A -> ~A */ | |
3074 | (simplify | |
3075 | (minus integer_all_onesp @0) | |
758671b8 JJ |
3076 | (if (TREE_CODE (type) != COMPLEX_TYPE) |
3077 | (bit_not @0))) | |
cc7b5acf RB |
3078 | |
3079 | /* (T)(P + A) - (T)P -> (T) A */ | |
d7f44d4d | 3080 | (simplify |
a72610d4 JJ |
3081 | (minus (convert (plus:c @@0 @1)) |
3082 | (convert? @0)) | |
d7f44d4d JJ |
3083 | (if (element_precision (type) <= element_precision (TREE_TYPE (@1)) |
3084 | /* For integer types, if A has a smaller type | |
3085 | than T the result depends on the possible | |
3086 | overflow in P + A. | |
3087 | E.g. T=size_t, A=(unsigned)429497295, P>0. | |
3088 | However, if an overflow in P + A would cause | |
3089 | undefined behavior, we can assume that there | |
3090 | is no overflow. */ | |
a72610d4 JJ |
3091 | || (INTEGRAL_TYPE_P (TREE_TYPE (@1)) |
3092 | && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@1)))) | |
d7f44d4d JJ |
3093 | (convert @1))) |
3094 | (simplify | |
3095 | (minus (convert (pointer_plus @@0 @1)) | |
3096 | (convert @0)) | |
3097 | (if (element_precision (type) <= element_precision (TREE_TYPE (@1)) | |
3098 | /* For pointer types, if the conversion of A to the | |
3099 | final type requires a sign- or zero-extension, | |
3100 | then we have to punt - it is not defined which | |
3101 | one is correct. */ | |
3102 | || (POINTER_TYPE_P (TREE_TYPE (@0)) | |
3103 | && TREE_CODE (@1) == INTEGER_CST | |
3104 | && tree_int_cst_sign_bit (@1) == 0)) | |
3105 | (convert @1))) | |
1af4ebf5 MG |
3106 | (simplify |
3107 | (pointer_diff (pointer_plus @@0 @1) @0) | |
3108 | /* The second argument of pointer_plus must be interpreted as signed, and | |
3109 | thus sign-extended if necessary. */ | |
3110 | (with { tree stype = signed_type_for (TREE_TYPE (@1)); } | |
8ae43881 JJ |
3111 | /* Use view_convert instead of convert here, as POINTER_PLUS_EXPR |
3112 | second arg is unsigned even when we need to consider it as signed, | |
3113 | we don't want to diagnose overflow here. */ | |
3114 | (convert (view_convert:stype @1)))) | |
a8fc2579 RB |
3115 | |
3116 | /* (T)P - (T)(P + A) -> -(T) A */ | |
d7f44d4d | 3117 | (simplify |
a72610d4 JJ |
3118 | (minus (convert? @0) |
3119 | (convert (plus:c @@0 @1))) | |
d7f44d4d JJ |
3120 | (if (INTEGRAL_TYPE_P (type) |
3121 | && TYPE_OVERFLOW_UNDEFINED (type) | |
3122 | && element_precision (type) <= element_precision (TREE_TYPE (@1))) | |
3123 | (with { tree utype = unsigned_type_for (type); } | |
3124 | (convert (negate (convert:utype @1)))) | |
a8fc2579 RB |
3125 | (if (element_precision (type) <= element_precision (TREE_TYPE (@1)) |
3126 | /* For integer types, if A has a smaller type | |
3127 | than T the result depends on the possible | |
3128 | overflow in P + A. | |
3129 | E.g. T=size_t, A=(unsigned)429497295, P>0. | |
3130 | However, if an overflow in P + A would cause | |
3131 | undefined behavior, we can assume that there | |
3132 | is no overflow. */ | |
a72610d4 JJ |
3133 | || (INTEGRAL_TYPE_P (TREE_TYPE (@1)) |
3134 | && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@1)))) | |
d7f44d4d JJ |
3135 | (negate (convert @1))))) |
3136 | (simplify | |
3137 | (minus (convert @0) | |
3138 | (convert (pointer_plus @@0 @1))) | |
3139 | (if (INTEGRAL_TYPE_P (type) | |
3140 | && TYPE_OVERFLOW_UNDEFINED (type) | |
3141 | && element_precision (type) <= element_precision (TREE_TYPE (@1))) | |
3142 | (with { tree utype = unsigned_type_for (type); } | |
3143 | (convert (negate (convert:utype @1)))) | |
3144 | (if (element_precision (type) <= element_precision (TREE_TYPE (@1)) | |
a8fc2579 RB |
3145 | /* For pointer types, if the conversion of A to the |
3146 | final type requires a sign- or zero-extension, | |
3147 | then we have to punt - it is not defined which | |
3148 | one is correct. */ | |
3149 | || (POINTER_TYPE_P (TREE_TYPE (@0)) | |
3150 | && TREE_CODE (@1) == INTEGER_CST | |
3151 | && tree_int_cst_sign_bit (@1) == 0)) | |
3152 | (negate (convert @1))))) | |
1af4ebf5 MG |
3153 | (simplify |
3154 | (pointer_diff @0 (pointer_plus @@0 @1)) | |
3155 | /* The second argument of pointer_plus must be interpreted as signed, and | |
3156 | thus sign-extended if necessary. */ | |
3157 | (with { tree stype = signed_type_for (TREE_TYPE (@1)); } | |
8ae43881 JJ |
3158 | /* Use view_convert instead of convert here, as POINTER_PLUS_EXPR |
3159 | second arg is unsigned even when we need to consider it as signed, | |
3160 | we don't want to diagnose overflow here. */ | |
3161 | (negate (convert (view_convert:stype @1))))) | |
a8fc2579 RB |
3162 | |
3163 | /* (T)(P + A) - (T)(P + B) -> (T)A - (T)B */ | |
d7f44d4d | 3164 | (simplify |
a72610d4 | 3165 | (minus (convert (plus:c @@0 @1)) |
d7f44d4d JJ |
3166 | (convert (plus:c @0 @2))) |
3167 | (if (INTEGRAL_TYPE_P (type) | |
3168 | && TYPE_OVERFLOW_UNDEFINED (type) | |
a72610d4 JJ |
3169 | && element_precision (type) <= element_precision (TREE_TYPE (@1)) |
3170 | && element_precision (type) <= element_precision (TREE_TYPE (@2))) | |
d7f44d4d JJ |
3171 | (with { tree utype = unsigned_type_for (type); } |
3172 | (convert (minus (convert:utype @1) (convert:utype @2)))) | |
a72610d4 JJ |
3173 | (if (((element_precision (type) <= element_precision (TREE_TYPE (@1))) |
3174 | == (element_precision (type) <= element_precision (TREE_TYPE (@2)))) | |
3175 | && (element_precision (type) <= element_precision (TREE_TYPE (@1)) | |
3176 | /* For integer types, if A has a smaller type | |
3177 | than T the result depends on the possible | |
3178 | overflow in P + A. | |
3179 | E.g. T=size_t, A=(unsigned)429497295, P>0. | |
3180 | However, if an overflow in P + A would cause | |
3181 | undefined behavior, we can assume that there | |
3182 | is no overflow. */ | |
3183 | || (INTEGRAL_TYPE_P (TREE_TYPE (@1)) | |
3184 | && INTEGRAL_TYPE_P (TREE_TYPE (@2)) | |
3185 | && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@1)) | |
3186 | && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@2))))) | |
d7f44d4d JJ |
3187 | (minus (convert @1) (convert @2))))) |
3188 | (simplify | |
3189 | (minus (convert (pointer_plus @@0 @1)) | |
3190 | (convert (pointer_plus @0 @2))) | |
3191 | (if (INTEGRAL_TYPE_P (type) | |
3192 | && TYPE_OVERFLOW_UNDEFINED (type) | |
3193 | && element_precision (type) <= element_precision (TREE_TYPE (@1))) | |
3194 | (with { tree utype = unsigned_type_for (type); } | |
3195 | (convert (minus (convert:utype @1) (convert:utype @2)))) | |
3196 | (if (element_precision (type) <= element_precision (TREE_TYPE (@1)) | |
a8fc2579 RB |
3197 | /* For pointer types, if the conversion of A to the |
3198 | final type requires a sign- or zero-extension, | |
3199 | then we have to punt - it is not defined which | |
3200 | one is correct. */ | |
3201 | || (POINTER_TYPE_P (TREE_TYPE (@0)) | |
3202 | && TREE_CODE (@1) == INTEGER_CST | |
3203 | && tree_int_cst_sign_bit (@1) == 0 | |
3204 | && TREE_CODE (@2) == INTEGER_CST | |
3205 | && tree_int_cst_sign_bit (@2) == 0)) | |
d7f44d4d | 3206 | (minus (convert @1) (convert @2))))) |
459f6f68 FX |
3207 | (simplify |
3208 | (pointer_diff (pointer_plus @0 @2) (pointer_plus @1 @2)) | |
3209 | (pointer_diff @0 @1)) | |
1af4ebf5 MG |
3210 | (simplify |
3211 | (pointer_diff (pointer_plus @@0 @1) (pointer_plus @0 @2)) | |
3212 | /* The second argument of pointer_plus must be interpreted as signed, and | |
3213 | thus sign-extended if necessary. */ | |
3214 | (with { tree stype = signed_type_for (TREE_TYPE (@1)); } | |
8ae43881 JJ |
3215 | /* Use view_convert instead of convert here, as POINTER_PLUS_EXPR |
3216 | second arg is unsigned even when we need to consider it as signed, | |
3217 | we don't want to diagnose overflow here. */ | |
3218 | (minus (convert (view_convert:stype @1)) | |
3219 | (convert (view_convert:stype @2))))))) | |
cc7b5acf | 3220 | |
5b55e6e3 RB |
3221 | /* (A * C) +- (B * C) -> (A+-B) * C and (A * C) +- A -> A * (C+-1). |
3222 | Modeled after fold_plusminus_mult_expr. */ | |
3223 | (if (!TYPE_SATURATING (type) | |
3224 | && (!FLOAT_TYPE_P (type) || flag_associative_math)) | |
3225 | (for plusminus (plus minus) | |
3226 | (simplify | |
c1bbe5b3 | 3227 | (plusminus (mult:cs@3 @0 @1) (mult:cs@4 @0 @2)) |
f9d2def0 FX |
3228 | (if (!ANY_INTEGRAL_TYPE_P (type) |
3229 | || TYPE_OVERFLOW_WRAPS (type) | |
3230 | || (INTEGRAL_TYPE_P (type) | |
3231 | && tree_expr_nonzero_p (@0) | |
3232 | && expr_not_equal_to (@0, wi::minus_one (TYPE_PRECISION (type))))) | |
3233 | (if (single_use (@3) || single_use (@4)) | |
3234 | /* If @1 +- @2 is constant require a hard single-use on either | |
3235 | original operand (but not on both). */ | |
3236 | (mult (plusminus @1 @2) @0) | |
f9d2def0 | 3237 | (mult! (plusminus @1 @2) @0) |
f9d2def0 | 3238 | ))) |
c1bbe5b3 RB |
3239 | /* We cannot generate constant 1 for fract. */ |
3240 | (if (!ALL_FRACT_MODE_P (TYPE_MODE (type))) | |
3241 | (simplify | |
3242 | (plusminus @0 (mult:c@3 @0 @2)) | |
3243 | (if ((!ANY_INTEGRAL_TYPE_P (type) | |
3244 | || TYPE_OVERFLOW_WRAPS (type) | |
a213ab38 JJ |
3245 | /* For @0 + @0*@2 this transformation would introduce UB |
3246 | (where there was none before) for @0 in [-1,0] and @2 max. | |
3247 | For @0 - @0*@2 this transformation would introduce UB | |
3248 | for @0 0 and @2 in [min,min+1] or @0 -1 and @2 min+1. */ | |
c1bbe5b3 | 3249 | || (INTEGRAL_TYPE_P (type) |
a213ab38 JJ |
3250 | && ((tree_expr_nonzero_p (@0) |
3251 | && expr_not_equal_to (@0, | |
3252 | wi::minus_one (TYPE_PRECISION (type)))) | |
3253 | || (plusminus == PLUS_EXPR | |
3254 | ? expr_not_equal_to (@2, | |
3255 | wi::max_value (TYPE_PRECISION (type), SIGNED)) | |
3256 | /* Let's ignore the @0 -1 and @2 min case. */ | |
3257 | : (expr_not_equal_to (@2, | |
3258 | wi::min_value (TYPE_PRECISION (type), SIGNED)) | |
3259 | && expr_not_equal_to (@2, | |
3260 | wi::min_value (TYPE_PRECISION (type), SIGNED) | |
3261 | + 1)))))) | |
c1bbe5b3 | 3262 | && single_use (@3)) |
5b55e6e3 RB |
3263 | (mult (plusminus { build_one_cst (type); } @2) @0))) |
3264 | (simplify | |
c1bbe5b3 RB |
3265 | (plusminus (mult:c@3 @0 @2) @0) |
3266 | (if ((!ANY_INTEGRAL_TYPE_P (type) | |
3267 | || TYPE_OVERFLOW_WRAPS (type) | |
a213ab38 JJ |
3268 | /* For @0*@2 + @0 this transformation would introduce UB |
3269 | (where there was none before) for @0 in [-1,0] and @2 max. | |
3270 | For @0*@2 - @0 this transformation would introduce UB | |
3271 | for @0 0 and @2 min. */ | |
c1bbe5b3 | 3272 | || (INTEGRAL_TYPE_P (type) |
a213ab38 JJ |
3273 | && ((tree_expr_nonzero_p (@0) |
3274 | && (plusminus == MINUS_EXPR | |
3275 | || expr_not_equal_to (@0, | |
3276 | wi::minus_one (TYPE_PRECISION (type))))) | |
3277 | || expr_not_equal_to (@2, | |
3278 | (plusminus == PLUS_EXPR | |
3279 | ? wi::max_value (TYPE_PRECISION (type), SIGNED) | |
3280 | : wi::min_value (TYPE_PRECISION (type), SIGNED)))))) | |
c1bbe5b3 | 3281 | && single_use (@3)) |
5b55e6e3 | 3282 | (mult (plusminus @2 { build_one_cst (type); }) @0)))))) |
cc7b5acf | 3283 | |
144aee70 JJ |
3284 | #if GIMPLE |
3285 | /* Canonicalize X + (X << C) into X * (1 + (1 << C)) and | |
3286 | (X << C1) + (X << C2) into X * ((1 << C1) + (1 << C2)). */ | |
3287 | (simplify | |
3288 | (plus:c @0 (lshift:s @0 INTEGER_CST@1)) | |
3289 | (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0)) | |
3290 | && tree_fits_uhwi_p (@1) | |
bbdf59fd JJ |
3291 | && tree_to_uhwi (@1) < element_precision (type) |
3292 | && (INTEGRAL_TYPE_P (TREE_TYPE (@0)) | |
3293 | || optab_handler (smul_optab, | |
3294 | TYPE_MODE (type)) != CODE_FOR_nothing)) | |
144aee70 JJ |
3295 | (with { tree t = type; |
3296 | if (!TYPE_OVERFLOW_WRAPS (t)) t = unsigned_type_for (t); | |
3297 | wide_int w = wi::set_bit_in_zero (tree_to_uhwi (@1), | |
3298 | element_precision (type)); | |
3299 | w += 1; | |
3300 | tree cst = wide_int_to_tree (VECTOR_TYPE_P (t) ? TREE_TYPE (t) | |
3301 | : t, w); | |
3302 | cst = build_uniform_cst (t, cst); } | |
3303 | (convert (mult (convert:t @0) { cst; }))))) | |
3304 | (simplify | |
3305 | (plus (lshift:s @0 INTEGER_CST@1) (lshift:s @0 INTEGER_CST@2)) | |
3306 | (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0)) | |
3307 | && tree_fits_uhwi_p (@1) | |
3308 | && tree_to_uhwi (@1) < element_precision (type) | |
3309 | && tree_fits_uhwi_p (@2) | |
bbdf59fd JJ |
3310 | && tree_to_uhwi (@2) < element_precision (type) |
3311 | && (INTEGRAL_TYPE_P (TREE_TYPE (@0)) | |
3312 | || optab_handler (smul_optab, | |
3313 | TYPE_MODE (type)) != CODE_FOR_nothing)) | |
144aee70 JJ |
3314 | (with { tree t = type; |
3315 | if (!TYPE_OVERFLOW_WRAPS (t)) t = unsigned_type_for (t); | |
3316 | unsigned int prec = element_precision (type); | |
3317 | wide_int w = wi::set_bit_in_zero (tree_to_uhwi (@1), prec); | |
3318 | w += wi::set_bit_in_zero (tree_to_uhwi (@2), prec); | |
3319 | tree cst = wide_int_to_tree (VECTOR_TYPE_P (t) ? TREE_TYPE (t) | |
3320 | : t, w); | |
3321 | cst = build_uniform_cst (t, cst); } | |
3322 | (convert (mult (convert:t @0) { cst; }))))) | |
3323 | #endif | |
3324 | ||
96146e61 RS |
3325 | /* Canonicalize (X*C1)|(X*C2) and (X*C1)^(X*C2) to (C1+C2)*X when |
3326 | tree_nonzero_bits allows IOR and XOR to be treated like PLUS. | |
3327 | Likewise, handle (X<<C3) and X as legitimate variants of X*C. */ | |
3328 | (for op (bit_ior bit_xor) | |
3329 | (simplify | |
3330 | (op (mult:s@0 @1 INTEGER_CST@2) | |
3331 | (mult:s@3 @1 INTEGER_CST@4)) | |
3332 | (if (INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_WRAPS (type) | |
3333 | && (tree_nonzero_bits (@0) & tree_nonzero_bits (@3)) == 0) | |
3334 | (mult @1 | |
3335 | { wide_int_to_tree (type, wi::to_wide (@2) + wi::to_wide (@4)); }))) | |
3336 | (simplify | |
3337 | (op:c (mult:s@0 @1 INTEGER_CST@2) | |
3338 | (lshift:s@3 @1 INTEGER_CST@4)) | |
3339 | (if (INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_WRAPS (type) | |
3340 | && tree_int_cst_sgn (@4) > 0 | |
3341 | && (tree_nonzero_bits (@0) & tree_nonzero_bits (@3)) == 0) | |
3342 | (with { wide_int wone = wi::one (TYPE_PRECISION (type)); | |
3343 | wide_int c = wi::add (wi::to_wide (@2), | |
3344 | wi::lshift (wone, wi::to_wide (@4))); } | |
3345 | (mult @1 { wide_int_to_tree (type, c); })))) | |
3346 | (simplify | |
3347 | (op:c (mult:s@0 @1 INTEGER_CST@2) | |
3348 | @1) | |
3349 | (if (INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_WRAPS (type) | |
3350 | && (tree_nonzero_bits (@0) & tree_nonzero_bits (@1)) == 0) | |
3351 | (mult @1 | |
3352 | { wide_int_to_tree (type, | |
3353 | wi::add (wi::to_wide (@2), 1)); }))) | |
3354 | (simplify | |
3355 | (op (lshift:s@0 @1 INTEGER_CST@2) | |
3356 | (lshift:s@3 @1 INTEGER_CST@4)) | |
3357 | (if (INTEGRAL_TYPE_P (type) | |
3358 | && tree_int_cst_sgn (@2) > 0 | |
3359 | && tree_int_cst_sgn (@4) > 0 | |
3360 | && (tree_nonzero_bits (@0) & tree_nonzero_bits (@3)) == 0) | |
3361 | (with { tree t = type; | |
3362 | if (!TYPE_OVERFLOW_WRAPS (t)) | |
3363 | t = unsigned_type_for (t); | |
3364 | wide_int wone = wi::one (TYPE_PRECISION (t)); | |
3365 | wide_int c = wi::add (wi::lshift (wone, wi::to_wide (@2)), | |
3366 | wi::lshift (wone, wi::to_wide (@4))); } | |
3367 | (convert (mult:t (convert:t @1) { wide_int_to_tree (t,c); }))))) | |
3368 | (simplify | |
3369 | (op:c (lshift:s@0 @1 INTEGER_CST@2) | |
3370 | @1) | |
3371 | (if (INTEGRAL_TYPE_P (type) | |
3372 | && tree_int_cst_sgn (@2) > 0 | |
3373 | && (tree_nonzero_bits (@0) & tree_nonzero_bits (@1)) == 0) | |
3374 | (with { tree t = type; | |
3375 | if (!TYPE_OVERFLOW_WRAPS (t)) | |
3376 | t = unsigned_type_for (t); | |
3377 | wide_int wone = wi::one (TYPE_PRECISION (t)); | |
3378 | wide_int c = wi::add (wi::lshift (wone, wi::to_wide (@2)), wone); } | |
3379 | (convert (mult:t (convert:t @1) { wide_int_to_tree (t, c); })))))) | |
3380 | ||
0122e8e5 | 3381 | /* Simplifications of MIN_EXPR, MAX_EXPR, fmin() and fmax(). */ |
a7f24614 | 3382 | |
344e4253 | 3383 | (for minmax (min max) |
a7f24614 RB |
3384 | (simplify |
3385 | (minmax @0 @0) | |
3386 | @0)) | |
344e4253 HG |
3387 | /* For fmin() and fmax(), skip folding when both are sNaN. */ |
3388 | (for minmax (FMIN_ALL FMAX_ALL) | |
3389 | (simplify | |
3390 | (minmax @0 @0) | |
3391 | (if (!tree_expr_maybe_signaling_nan_p (@0)) | |
3392 | @0))) | |
4a334cba RS |
3393 | /* min(max(x,y),y) -> y. */ |
3394 | (simplify | |
3395 | (min:c (max:c @0 @1) @1) | |
3396 | @1) | |
3397 | /* max(min(x,y),y) -> y. */ | |
3398 | (simplify | |
3399 | (max:c (min:c @0 @1) @1) | |
3400 | @1) | |
d657e995 RB |
3401 | /* max(a,-a) -> abs(a). */ |
3402 | (simplify | |
3403 | (max:c @0 (negate @0)) | |
3404 | (if (TREE_CODE (type) != COMPLEX_TYPE | |
3405 | && (! ANY_INTEGRAL_TYPE_P (type) | |
3406 | || TYPE_OVERFLOW_UNDEFINED (type))) | |
3407 | (abs @0))) | |
54f84ca9 RB |
3408 | /* min(a,-a) -> -abs(a). */ |
3409 | (simplify | |
3410 | (min:c @0 (negate @0)) | |
3411 | (if (TREE_CODE (type) != COMPLEX_TYPE | |
3412 | && (! ANY_INTEGRAL_TYPE_P (type) | |
3413 | || TYPE_OVERFLOW_UNDEFINED (type))) | |
3414 | (negate (abs @0)))) | |
a7f24614 RB |
3415 | (simplify |
3416 | (min @0 @1) | |
2c2870a1 MG |
3417 | (switch |
3418 | (if (INTEGRAL_TYPE_P (type) | |
3419 | && TYPE_MIN_VALUE (type) | |
3420 | && operand_equal_p (@1, TYPE_MIN_VALUE (type), OEP_ONLY_CONST)) | |
3421 | @1) | |
3422 | (if (INTEGRAL_TYPE_P (type) | |
3423 | && TYPE_MAX_VALUE (type) | |
3424 | && operand_equal_p (@1, TYPE_MAX_VALUE (type), OEP_ONLY_CONST)) | |
3425 | @0))) | |
a7f24614 RB |
3426 | (simplify |
3427 | (max @0 @1) | |
2c2870a1 MG |
3428 | (switch |
3429 | (if (INTEGRAL_TYPE_P (type) | |
3430 | && TYPE_MAX_VALUE (type) | |
3431 | && operand_equal_p (@1, TYPE_MAX_VALUE (type), OEP_ONLY_CONST)) | |
3432 | @1) | |
3433 | (if (INTEGRAL_TYPE_P (type) | |
3434 | && TYPE_MIN_VALUE (type) | |
3435 | && operand_equal_p (@1, TYPE_MIN_VALUE (type), OEP_ONLY_CONST)) | |
3436 | @0))) | |
ad6e4ba8 | 3437 | |
182f37c9 N |
3438 | /* max (a, a + CST) -> a + CST where CST is positive. */ |
3439 | /* max (a, a + CST) -> a where CST is negative. */ | |
3440 | (simplify | |
3441 | (max:c @0 (plus@2 @0 INTEGER_CST@1)) | |
3442 | (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))) | |
3443 | (if (tree_int_cst_sgn (@1) > 0) | |
3444 | @2 | |
3445 | @0))) | |
3446 | ||
3447 | /* min (a, a + CST) -> a where CST is positive. */ | |
3448 | /* min (a, a + CST) -> a + CST where CST is negative. */ | |
3449 | (simplify | |
3450 | (min:c @0 (plus@2 @0 INTEGER_CST@1)) | |
3451 | (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))) | |
3452 | (if (tree_int_cst_sgn (@1) > 0) | |
3453 | @0 | |
3454 | @2))) | |
3455 | ||
6123b998 JJ |
3456 | /* Simplify min (&var[off0], &var[off1]) etc. depending on whether |
3457 | the addresses are known to be less, equal or greater. */ | |
3458 | (for minmax (min max) | |
3459 | cmp (lt gt) | |
3460 | (simplify | |
3461 | (minmax (convert1?@2 addr@0) (convert2?@3 addr@1)) | |
3462 | (with | |
3463 | { | |
3464 | poly_int64 off0, off1; | |
3465 | tree base0, base1; | |
3466 | int equal = address_compare (cmp, TREE_TYPE (@2), @0, @1, base0, base1, | |
3467 | off0, off1, GENERIC); | |
3468 | } | |
3469 | (if (equal == 1) | |
3470 | (if (minmax == MIN_EXPR) | |
3471 | (if (known_le (off0, off1)) | |
3472 | @2 | |
3473 | (if (known_gt (off0, off1)) | |
3474 | @3)) | |
3475 | (if (known_ge (off0, off1)) | |
3476 | @2 | |
3477 | (if (known_lt (off0, off1)) | |
3478 | @3))))))) | |
3479 | ||
ad6e4ba8 BC |
3480 | /* (convert (minmax ((convert (x) c)))) -> minmax (x c) if x is promoted |
3481 | and the outer convert demotes the expression back to x's type. */ | |
3482 | (for minmax (min max) | |
3483 | (simplify | |
3484 | (convert (minmax@0 (convert @1) INTEGER_CST@2)) | |
ebf41734 BC |
3485 | (if (INTEGRAL_TYPE_P (type) |
3486 | && types_match (@1, type) && int_fits_type_p (@2, type) | |
ad6e4ba8 BC |
3487 | && TYPE_SIGN (TREE_TYPE (@0)) == TYPE_SIGN (type) |
3488 | && TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (type)) | |
3489 | (minmax @1 (convert @2))))) | |
3490 | ||
c6cfa2bf | 3491 | (for minmax (FMIN_ALL FMAX_ALL) |
344e4253 HG |
3492 | /* If either argument is NaN and other one is not sNaN, return the other |
3493 | one. Avoid the transformation if we get (and honor) a signalling NaN. */ | |
0122e8e5 RS |
3494 | (simplify |
3495 | (minmax:c @0 REAL_CST@1) | |
344e4253 HG |
3496 | (if (real_isnan (TREE_REAL_CST_PTR (@1)) |
3497 | && (!HONOR_SNANS (@1) || !TREE_REAL_CST (@1).signalling) | |
3498 | && !tree_expr_maybe_signaling_nan_p (@0)) | |
0122e8e5 RS |
3499 | @0))) |
3500 | /* Convert fmin/fmax to MIN_EXPR/MAX_EXPR. C99 requires these | |
3501 | functions to return the numeric arg if the other one is NaN. | |
3502 | MIN and MAX don't honor that, so only transform if -ffinite-math-only | |
3503 | is set. C99 doesn't require -0.0 to be handled, so we don't have to | |
3504 | worry about it either. */ | |
3505 | (if (flag_finite_math_only) | |
3506 | (simplify | |
c6cfa2bf | 3507 | (FMIN_ALL @0 @1) |
0122e8e5 | 3508 | (min @0 @1)) |
4119b2eb | 3509 | (simplify |
c6cfa2bf | 3510 | (FMAX_ALL @0 @1) |
0122e8e5 | 3511 | (max @0 @1))) |
ce0e66ff | 3512 | /* min (-A, -B) -> -max (A, B) */ |
c6cfa2bf MM |
3513 | (for minmax (min max FMIN_ALL FMAX_ALL) |
3514 | maxmin (max min FMAX_ALL FMIN_ALL) | |
ce0e66ff MG |
3515 | (simplify |
3516 | (minmax (negate:s@2 @0) (negate:s@3 @1)) | |
3517 | (if (FLOAT_TYPE_P (TREE_TYPE (@0)) | |
3518 | || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0)) | |
3519 | && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))) | |
3520 | (negate (maxmin @0 @1))))) | |
3521 | /* MIN (~X, ~Y) -> ~MAX (X, Y) | |
3522 | MAX (~X, ~Y) -> ~MIN (X, Y) */ | |
3523 | (for minmax (min max) | |
3524 | maxmin (max min) | |
3525 | (simplify | |
3526 | (minmax (bit_not:s@2 @0) (bit_not:s@3 @1)) | |
3527 | (bit_not (maxmin @0 @1)))) | |
a7f24614 | 3528 | |
b4817bd6 MG |
3529 | /* MIN (X, Y) == X -> X <= Y */ |
3530 | (for minmax (min min max max) | |
3531 | cmp (eq ne eq ne ) | |
3532 | out (le gt ge lt ) | |
3533 | (simplify | |
3534 | (cmp:c (minmax:c @0 @1) @0) | |
3535 | (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))) | |
3536 | (out @0 @1)))) | |
3537 | /* MIN (X, 5) == 0 -> X == 0 | |
3538 | MIN (X, 5) == 7 -> false */ | |
3539 | (for cmp (eq ne) | |
3540 | (simplify | |
3541 | (cmp (min @0 INTEGER_CST@1) INTEGER_CST@2) | |
8e6cdc90 RS |
3542 | (if (wi::lt_p (wi::to_wide (@1), wi::to_wide (@2), |
3543 | TYPE_SIGN (TREE_TYPE (@0)))) | |
b4817bd6 | 3544 | { constant_boolean_node (cmp == NE_EXPR, type); } |
8e6cdc90 RS |
3545 | (if (wi::gt_p (wi::to_wide (@1), wi::to_wide (@2), |
3546 | TYPE_SIGN (TREE_TYPE (@0)))) | |
b4817bd6 MG |
3547 | (cmp @0 @2))))) |
3548 | (for cmp (eq ne) | |
3549 | (simplify | |
3550 | (cmp (max @0 INTEGER_CST@1) INTEGER_CST@2) | |
8e6cdc90 RS |
3551 | (if (wi::gt_p (wi::to_wide (@1), wi::to_wide (@2), |
3552 | TYPE_SIGN (TREE_TYPE (@0)))) | |
b4817bd6 | 3553 | { constant_boolean_node (cmp == NE_EXPR, type); } |
8e6cdc90 RS |
3554 | (if (wi::lt_p (wi::to_wide (@1), wi::to_wide (@2), |
3555 | TYPE_SIGN (TREE_TYPE (@0)))) | |
b4817bd6 MG |
3556 | (cmp @0 @2))))) |
3557 | /* MIN (X, C1) < C2 -> X < C2 || C1 < C2 */ | |
3558 | (for minmax (min min max max min min max max ) | |
3559 | cmp (lt le gt ge gt ge lt le ) | |
3560 | comb (bit_ior bit_ior bit_ior bit_ior bit_and bit_and bit_and bit_and) | |
3561 | (simplify | |
3562 | (cmp (minmax @0 INTEGER_CST@1) INTEGER_CST@2) | |
3563 | (comb (cmp @0 @2) (cmp @1 @2)))) | |
3564 | ||
abef3691 ER |
3565 | /* X <= MAX(X, Y) -> true |
3566 | X > MAX(X, Y) -> false | |
3567 | X >= MIN(X, Y) -> true | |
3568 | X < MIN(X, Y) -> false */ | |
3569 | (for minmax (min min max max ) | |
3570 | cmp (ge lt le gt ) | |
3571 | (simplify | |
3572 | (cmp @0 (minmax:c @0 @1)) | |
3573 | { constant_boolean_node (cmp == GE_EXPR || cmp == LE_EXPR, type); } )) | |
3574 | ||
8fb94fc6 RS |
3575 | /* Undo fancy ways of writing max/min or other ?: expressions, like |
3576 | a - ((a - b) & -(a < b)) and a - (a - b) * (a < b) into (a < b) ? b : a. | |
49647b7b | 3577 | People normally use ?: and that is what we actually try to optimize. */ |
8fb94fc6 RS |
3578 | /* Transform A + (B-A)*cmp into cmp ? B : A. */ |
3579 | (simplify | |
3580 | (plus:c @0 (mult:c (minus @1 @0) zero_one_valued_p@2)) | |
3581 | (if (INTEGRAL_TYPE_P (type) | |
3582 | && (GIMPLE || !TREE_SIDE_EFFECTS (@1))) | |
3583 | (cond (convert:boolean_type_node @2) @1 @0))) | |
3584 | /* Transform A - (A-B)*cmp into cmp ? B : A. */ | |
3585 | (simplify | |
3586 | (minus @0 (mult:c (minus @0 @1) zero_one_valued_p@2)) | |
3587 | (if (INTEGRAL_TYPE_P (type) | |
3588 | && (GIMPLE || !TREE_SIDE_EFFECTS (@1))) | |
3589 | (cond (convert:boolean_type_node @2) @1 @0))) | |
3590 | /* Transform A ^ (A^B)*cmp into cmp ? B : A. */ | |
3591 | (simplify | |
3592 | (bit_xor:c @0 (mult:c (bit_xor:c @0 @1) zero_one_valued_p@2)) | |
3593 | (if (INTEGRAL_TYPE_P (type) | |
3594 | && (GIMPLE || !TREE_SIDE_EFFECTS (@1))) | |
3595 | (cond (convert:boolean_type_node @2) @1 @0))) | |
49647b7b | 3596 | |
633e9920 SF |
3597 | /* (x <= 0 ? -x : 0) -> max(-x, 0). */ |
3598 | (simplify | |
3599 | (cond (le @0 integer_zerop@1) (negate@2 @0) integer_zerop@1) | |
3600 | (max @2 @1)) | |
3601 | ||
6508d5e5 MC |
3602 | /* ((x & 0x1) == 0) ? y : z <op> y -> (-(typeof(y))(x & 0x1) & z) <op> y */ |
3603 | (for op (bit_xor bit_ior) | |
3604 | (simplify | |
3605 | (cond (eq zero_one_valued_p@0 | |
3606 | integer_zerop) | |
3607 | @1 | |
3608 | (op:c @2 @1)) | |
3609 | (if (INTEGRAL_TYPE_P (type) | |
3610 | && TYPE_PRECISION (type) > 1 | |
3611 | && (INTEGRAL_TYPE_P (TREE_TYPE (@0)))) | |
3612 | (op (bit_and (negate (convert:type @0)) @2) @1)))) | |
3613 | ||
3614 | /* ((x & 0x1) == 0) ? z <op> y : y -> (-(typeof(y))(x & 0x1) & z) <op> y */ | |
3615 | (for op (bit_xor bit_ior) | |
3616 | (simplify | |
3617 | (cond (ne zero_one_valued_p@0 | |
3618 | integer_zerop) | |
3619 | (op:c @2 @1) | |
3620 | @1) | |
3621 | (if (INTEGRAL_TYPE_P (type) | |
3622 | && TYPE_PRECISION (type) > 1 | |
3623 | && (INTEGRAL_TYPE_P (TREE_TYPE (@0)))) | |
3624 | (op (bit_and (negate (convert:type @0)) @2) @1)))) | |
3625 | ||
a7f24614 RB |
3626 | /* Simplifications of shift and rotates. */ |
3627 | ||
3628 | (for rotate (lrotate rrotate) | |
3629 | (simplify | |
3630 | (rotate integer_all_onesp@0 @1) | |
3631 | @0)) | |
3632 | ||
3633 | /* Optimize -1 >> x for arithmetic right shifts. */ | |
3634 | (simplify | |
3635 | (rshift integer_all_onesp@0 @1) | |
4866b2f5 | 3636 | (if (!TYPE_UNSIGNED (type)) |
a7f24614 RB |
3637 | @0)) |
3638 | ||
12085390 N |
3639 | /* Optimize (x >> c) << c into x & (-1<<c). */ |
3640 | (simplify | |
f26916c2 | 3641 | (lshift (nop_convert? (rshift @0 INTEGER_CST@1)) @1) |
8e6cdc90 | 3642 | (if (wi::ltu_p (wi::to_wide (@1), element_precision (type))) |
f26916c2 JJ |
3643 | /* It doesn't matter if the right shift is arithmetic or logical. */ |
3644 | (bit_and (view_convert @0) (lshift { build_minus_one_cst (type); } @1)))) | |
3645 | ||
3646 | (simplify | |
3647 | (lshift (convert (convert@2 (rshift @0 INTEGER_CST@1))) @1) | |
3648 | (if (wi::ltu_p (wi::to_wide (@1), element_precision (type)) | |
3649 | /* Allow intermediate conversion to integral type with whatever sign, as | |
3650 | long as the low TYPE_PRECISION (type) | |
3651 | - TYPE_PRECISION (TREE_TYPE (@2)) bits are preserved. */ | |
3652 | && INTEGRAL_TYPE_P (type) | |
3653 | && INTEGRAL_TYPE_P (TREE_TYPE (@2)) | |
3654 | && INTEGRAL_TYPE_P (TREE_TYPE (@0)) | |
3655 | && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (@0)) | |
3656 | && (TYPE_PRECISION (TREE_TYPE (@2)) >= TYPE_PRECISION (type) | |
3657 | || wi::geu_p (wi::to_wide (@1), | |
3658 | TYPE_PRECISION (type) | |
3659 | - TYPE_PRECISION (TREE_TYPE (@2))))) | |
3660 | (bit_and (convert @0) (lshift { build_minus_one_cst (type); } @1)))) | |
12085390 N |
3661 | |
3662 | /* Optimize (x << c) >> c into x & ((unsigned)-1 >> c) for unsigned | |
3663 | types. */ | |
3664 | (simplify | |
3665 | (rshift (lshift @0 INTEGER_CST@1) @1) | |
3666 | (if (TYPE_UNSIGNED (type) | |
8e6cdc90 | 3667 | && (wi::ltu_p (wi::to_wide (@1), element_precision (type)))) |
12085390 N |
3668 | (bit_and @0 (rshift { build_minus_one_cst (type); } @1)))) |
3669 | ||
6483f059 ER |
3670 | /* Optimize x >> x into 0 */ |
3671 | (simplify | |
3672 | (rshift @0 @0) | |
3673 | { build_zero_cst (type); }) | |
3674 | ||
a7f24614 RB |
3675 | (for shiftrotate (lrotate rrotate lshift rshift) |
3676 | (simplify | |
3677 | (shiftrotate @0 integer_zerop) | |
3678 | (non_lvalue @0)) | |
3679 | (simplify | |
3680 | (shiftrotate integer_zerop@0 @1) | |
3681 | @0) | |
3682 | /* Prefer vector1 << scalar to vector1 << vector2 | |
3683 | if vector2 is uniform. */ | |
3684 | (for vec (VECTOR_CST CONSTRUCTOR) | |
3685 | (simplify | |
3686 | (shiftrotate @0 vec@1) | |
3687 | (with { tree tem = uniform_vector_p (@1); } | |
3688 | (if (tem) | |
3689 | (shiftrotate @0 { tem; })))))) | |
3690 | ||
165ba2e9 JJ |
3691 | /* Simplify X << Y where Y's low width bits are 0 to X, as only valid |
3692 | Y is 0. Similarly for X >> Y. */ | |
3693 | #if GIMPLE | |
3694 | (for shift (lshift rshift) | |
3695 | (simplify | |
3696 | (shift @0 SSA_NAME@1) | |
3697 | (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))) | |
3698 | (with { | |
3699 | int width = ceil_log2 (element_precision (TREE_TYPE (@0))); | |
3700 | int prec = TYPE_PRECISION (TREE_TYPE (@1)); | |
3701 | } | |
3702 | (if ((get_nonzero_bits (@1) & wi::mask (width, false, prec)) == 0) | |
3703 | @0))))) | |
3704 | #endif | |
3705 | ||
a7f24614 RB |
3706 | /* Rewrite an LROTATE_EXPR by a constant into an |
3707 | RROTATE_EXPR by a new constant. */ | |
3708 | (simplify | |
3709 | (lrotate @0 INTEGER_CST@1) | |
23f27839 | 3710 | (rrotate @0 { const_binop (MINUS_EXPR, TREE_TYPE (@1), |
a7f24614 RB |
3711 | build_int_cst (TREE_TYPE (@1), |
3712 | element_precision (type)), @1); })) | |
3713 | ||
14ea9f92 RB |
3714 | /* Turn (a OP c1) OP c2 into a OP (c1+c2). */ |
3715 | (for op (lrotate rrotate rshift lshift) | |
3716 | (simplify | |
3717 | (op (op @0 INTEGER_CST@1) INTEGER_CST@2) | |
3718 | (with { unsigned int prec = element_precision (type); } | |
8e6cdc90 RS |
3719 | (if (wi::ge_p (wi::to_wide (@1), 0, TYPE_SIGN (TREE_TYPE (@1))) |
3720 | && wi::lt_p (wi::to_wide (@1), prec, TYPE_SIGN (TREE_TYPE (@1))) | |
3721 | && wi::ge_p (wi::to_wide (@2), 0, TYPE_SIGN (TREE_TYPE (@2))) | |
3722 | && wi::lt_p (wi::to_wide (@2), prec, TYPE_SIGN (TREE_TYPE (@2)))) | |
a1488398 RS |
3723 | (with { unsigned int low = (tree_to_uhwi (@1) |
3724 | + tree_to_uhwi (@2)); } | |
14ea9f92 RB |
3725 | /* Deal with a OP (c1 + c2) being undefined but (a OP c1) OP c2 |
3726 | being well defined. */ | |
3727 | (if (low >= prec) | |
3728 | (if (op == LROTATE_EXPR || op == RROTATE_EXPR) | |
8fdc6c67 | 3729 | (op @0 { build_int_cst (TREE_TYPE (@1), low % prec); }) |
50301115 | 3730 | (if (TYPE_UNSIGNED (type) || op == LSHIFT_EXPR) |
8fdc6c67 RB |
3731 | { build_zero_cst (type); } |
3732 | (op @0 { build_int_cst (TREE_TYPE (@1), prec - 1); }))) | |
3733 | (op @0 { build_int_cst (TREE_TYPE (@1), low); }))))))) | |
14ea9f92 RB |
3734 | |
3735 | ||
e2559c39 JJ |
3736 | /* Simplify (CST << x) & 1 to 0 if CST is even or to x == 0 if it is odd. */ |
3737 | (simplify | |
3738 | (bit_and (lshift INTEGER_CST@1 @0) integer_onep) | |
3739 | (if ((wi::to_wide (@1) & 1) != 0) | |
3740 | (convert (eq:boolean_type_node @0 { build_zero_cst (TREE_TYPE (@0)); })) | |
3741 | { build_zero_cst (type); })) | |
3742 | ||
0425f4c1 JJ |
3743 | /* Simplify ((C << x) & D) != 0 where C and D are power of two constants, |
3744 | either to false if D is smaller (unsigned comparison) than C, or to | |
3745 | x == log2 (D) - log2 (C). Similarly for right shifts. */ | |
01ada710 MP |
3746 | (for cmp (ne eq) |
3747 | icmp (eq ne) | |
3748 | (simplify | |
0425f4c1 JJ |
3749 | (cmp (bit_and (lshift integer_pow2p@1 @0) integer_pow2p@2) integer_zerop) |
3750 | (with { int c1 = wi::clz (wi::to_wide (@1)); | |
3751 | int c2 = wi::clz (wi::to_wide (@2)); } | |
3752 | (if (c1 < c2) | |
3753 | { constant_boolean_node (cmp == NE_EXPR ? false : true, type); } | |
3754 | (icmp @0 { build_int_cst (TREE_TYPE (@0), c1 - c2); })))) | |
3755 | (simplify | |
3756 | (cmp (bit_and (rshift integer_pow2p@1 @0) integer_pow2p@2) integer_zerop) | |
3757 | (if (tree_int_cst_sgn (@1) > 0) | |
3758 | (with { int c1 = wi::clz (wi::to_wide (@1)); | |
3759 | int c2 = wi::clz (wi::to_wide (@2)); } | |
3760 | (if (c1 > c2) | |
3761 | { constant_boolean_node (cmp == NE_EXPR ? false : true, type); } | |
3762 | (icmp @0 { build_int_cst (TREE_TYPE (@0), c2 - c1); })))))) | |
cc7b5acf | 3763 | |
f2e609c3 MP |
3764 | /* (CST1 << A) == CST2 -> A == ctz (CST2) - ctz (CST1) |
3765 | (CST1 << A) != CST2 -> A != ctz (CST2) - ctz (CST1) | |
3766 | if CST2 != 0. */ | |
3767 | (for cmp (ne eq) | |
3768 | (simplify | |
3769 | (cmp (lshift INTEGER_CST@0 @1) INTEGER_CST@2) | |
8e6cdc90 | 3770 | (with { int cand = wi::ctz (wi::to_wide (@2)) - wi::ctz (wi::to_wide (@0)); } |
f2e609c3 MP |
3771 | (if (cand < 0 |
3772 | || (!integer_zerop (@2) | |
8e6cdc90 | 3773 | && wi::lshift (wi::to_wide (@0), cand) != wi::to_wide (@2))) |
8fdc6c67 RB |
3774 | { constant_boolean_node (cmp == NE_EXPR, type); } |
3775 | (if (!integer_zerop (@2) | |
8e6cdc90 | 3776 | && wi::lshift (wi::to_wide (@0), cand) == wi::to_wide (@2)) |
8fdc6c67 | 3777 | (cmp @1 { build_int_cst (TREE_TYPE (@1), cand); })))))) |
f2e609c3 | 3778 | |
f3f73e86 RS |
3779 | /* Fold ((X << C1) & C2) cmp C3 into (X & (C2 >> C1)) cmp (C3 >> C1) |
3780 | ((X >> C1) & C2) cmp C3 into (X & (C2 << C1)) cmp (C3 << C1). */ | |
3781 | (for cmp (ne eq) | |
3782 | (simplify | |
3783 | (cmp (bit_and:s (lshift:s @0 INTEGER_CST@1) INTEGER_CST@2) INTEGER_CST@3) | |
3784 | (if (tree_fits_shwi_p (@1) | |
3785 | && tree_to_shwi (@1) > 0 | |
6fc14f19 RS |
3786 | && tree_to_shwi (@1) < TYPE_PRECISION (TREE_TYPE (@0))) |
3787 | (if (tree_to_shwi (@1) > wi::ctz (wi::to_wide (@3))) | |
3788 | { constant_boolean_node (cmp == NE_EXPR, type); } | |
3789 | (with { wide_int c1 = wi::to_wide (@1); | |
3790 | wide_int c2 = wi::lrshift (wi::to_wide (@2), c1); | |
3791 | wide_int c3 = wi::lrshift (wi::to_wide (@3), c1); } | |
3792 | (cmp (bit_and @0 { wide_int_to_tree (TREE_TYPE (@0), c2); }) | |
3793 | { wide_int_to_tree (TREE_TYPE (@0), c3); }))))) | |
f3f73e86 RS |
3794 | (simplify |
3795 | (cmp (bit_and:s (rshift:s @0 INTEGER_CST@1) INTEGER_CST@2) INTEGER_CST@3) | |
3796 | (if (tree_fits_shwi_p (@1) | |
3797 | && tree_to_shwi (@1) > 0 | |
6fc14f19 RS |
3798 | && tree_to_shwi (@1) < TYPE_PRECISION (TREE_TYPE (@0))) |
3799 | (with { tree t0 = TREE_TYPE (@0); | |
3800 | unsigned int prec = TYPE_PRECISION (t0); | |
3801 | wide_int c1 = wi::to_wide (@1); | |
3802 | wide_int c2 = wi::to_wide (@2); | |
3803 | wide_int c3 = wi::to_wide (@3); | |
3804 | wide_int sb = wi::set_bit_in_zero (prec - 1, prec); } | |
3805 | (if ((c2 & c3) != c3) | |
3806 | { constant_boolean_node (cmp == NE_EXPR, type); } | |
3807 | (if (TYPE_UNSIGNED (t0)) | |
3808 | (if ((c3 & wi::arshift (sb, c1 - 1)) != 0) | |
3809 | { constant_boolean_node (cmp == NE_EXPR, type); } | |
3810 | (cmp (bit_and @0 { wide_int_to_tree (t0, c2 << c1); }) | |
3811 | { wide_int_to_tree (t0, c3 << c1); })) | |
3812 | (with { wide_int smask = wi::arshift (sb, c1); } | |
3813 | (switch | |
3814 | (if ((c2 & smask) == 0) | |
3815 | (cmp (bit_and @0 { wide_int_to_tree (t0, c2 << c1); }) | |
3816 | { wide_int_to_tree (t0, c3 << c1); })) | |
3817 | (if ((c3 & smask) == 0) | |
3818 | (cmp (bit_and @0 { wide_int_to_tree (t0, (c2 << c1) | sb); }) | |
3819 | { wide_int_to_tree (t0, c3 << c1); })) | |
3820 | (if ((c2 & smask) != (c3 & smask)) | |
3821 | { constant_boolean_node (cmp == NE_EXPR, type); }) | |
3822 | (cmp (bit_and @0 { wide_int_to_tree (t0, (c2 << c1) | sb); }) | |
3823 | { wide_int_to_tree (t0, (c3 << c1) | sb); }))))))))) | |
f3f73e86 | 3824 | |
1ffbaa3f RB |
3825 | /* Fold (X << C1) & C2 into (X << C1) & (C2 | ((1 << C1) - 1)) |
3826 | (X >> C1) & C2 into (X >> C1) & (C2 | ~((type) -1 >> C1)) | |
3827 | if the new mask might be further optimized. */ | |
3828 | (for shift (lshift rshift) | |
3829 | (simplify | |
44fc0a51 RB |
3830 | (bit_and (convert?:s@4 (shift:s@5 (convert1?@3 @0) INTEGER_CST@1)) |
3831 | INTEGER_CST@2) | |
1ffbaa3f RB |
3832 | (if (tree_nop_conversion_p (TREE_TYPE (@4), TREE_TYPE (@5)) |
3833 | && TYPE_PRECISION (type) <= HOST_BITS_PER_WIDE_INT | |
3834 | && tree_fits_uhwi_p (@1) | |
3835 | && tree_to_uhwi (@1) > 0 | |
3836 | && tree_to_uhwi (@1) < TYPE_PRECISION (type)) | |
3837 | (with | |
3838 | { | |
3839 | unsigned int shiftc = tree_to_uhwi (@1); | |
3840 | unsigned HOST_WIDE_INT mask = TREE_INT_CST_LOW (@2); | |
3841 | unsigned HOST_WIDE_INT newmask, zerobits = 0; | |
3842 | tree shift_type = TREE_TYPE (@3); | |
3843 | unsigned int prec; | |
3844 | ||
3845 | if (shift == LSHIFT_EXPR) | |
fecfbfa4 | 3846 | zerobits = ((HOST_WIDE_INT_1U << shiftc) - 1); |
1ffbaa3f | 3847 | else if (shift == RSHIFT_EXPR |
2be65d9e | 3848 | && type_has_mode_precision_p (shift_type)) |
1ffbaa3f RB |
3849 | { |
3850 | prec = TYPE_PRECISION (TREE_TYPE (@3)); | |
3851 | tree arg00 = @0; | |
3852 | /* See if more bits can be proven as zero because of | |
3853 | zero extension. */ | |
3854 | if (@3 != @0 | |
3855 | && TYPE_UNSIGNED (TREE_TYPE (@0))) | |
3856 | { | |
3857 | tree inner_type = TREE_TYPE (@0); | |
2be65d9e | 3858 | if (type_has_mode_precision_p (inner_type) |
1ffbaa3f RB |
3859 | && TYPE_PRECISION (inner_type) < prec) |
3860 | { | |
3861 | prec = TYPE_PRECISION (inner_type); | |
3862 | /* See if we can shorten the right shift. */ | |
3863 | if (shiftc < prec) | |
3864 | shift_type = inner_type; | |
3865 | /* Otherwise X >> C1 is all zeros, so we'll optimize | |
3866 | it into (X, 0) later on by making sure zerobits | |
3867 | is all ones. */ | |
3868 | } | |
3869 | } | |
dd4786fe | 3870 | zerobits = HOST_WIDE_INT_M1U; |
1ffbaa3f RB |
3871 | if (shiftc < prec) |
3872 | { | |
3873 | zerobits >>= HOST_BITS_PER_WIDE_INT - shiftc; | |
3874 | zerobits <<= prec - shiftc; | |
3875 | } | |
3876 | /* For arithmetic shift if sign bit could be set, zerobits | |
3877 | can contain actually sign bits, so no transformation is | |
3878 | possible, unless MASK masks them all away. In that | |
3879 | case the shift needs to be converted into logical shift. */ | |
3880 | if (!TYPE_UNSIGNED (TREE_TYPE (@3)) | |
3881 | && prec == TYPE_PRECISION (TREE_TYPE (@3))) | |
3882 | { | |
3883 | if ((mask & zerobits) == 0) | |
3884 | shift_type = unsigned_type_for (TREE_TYPE (@3)); | |
3885 | else | |
3886 | zerobits = 0; | |
3887 | } | |
3888 | } | |
3889 | } | |
3890 | /* ((X << 16) & 0xff00) is (X, 0). */ | |
3891 | (if ((mask & zerobits) == mask) | |
8fdc6c67 RB |
3892 | { build_int_cst (type, 0); } |
3893 | (with { newmask = mask | zerobits; } | |
3894 | (if (newmask != mask && (newmask & (newmask + 1)) == 0) | |
3895 | (with | |
3896 | { | |
3897 | /* Only do the transformation if NEWMASK is some integer | |
3898 | mode's mask. */ | |
3899 | for (prec = BITS_PER_UNIT; | |
3900 | prec < HOST_BITS_PER_WIDE_INT; prec <<= 1) | |
fecfbfa4 | 3901 | if (newmask == (HOST_WIDE_INT_1U << prec) - 1) |
8fdc6c67 RB |
3902 | break; |
3903 | } | |
3904 | (if (prec < HOST_BITS_PER_WIDE_INT | |
dd4786fe | 3905 | || newmask == HOST_WIDE_INT_M1U) |
8fdc6c67 RB |
3906 | (with |
3907 | { tree newmaskt = build_int_cst_type (TREE_TYPE (@2), newmask); } | |
3908 | (if (!tree_int_cst_equal (newmaskt, @2)) | |
3909 | (if (shift_type != TREE_TYPE (@3)) | |
3910 | (bit_and (convert (shift:shift_type (convert @3) @1)) { newmaskt; }) | |
3911 | (bit_and @4 { newmaskt; }))))))))))))) | |
1ffbaa3f | 3912 | |
2abd924f JL |
3913 | /* ((1 << n) & M) != 0 -> n == log2 (M) */ |
3914 | (for cmp (ne eq) | |
3915 | icmp (eq ne) | |
3916 | (simplify | |
3917 | (cmp | |
3918 | (bit_and | |
3919 | (nop_convert? (lshift integer_onep @0)) integer_pow2p@1) integer_zerop) | |
3920 | (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))) | |
3921 | (icmp @0 { wide_int_to_tree (TREE_TYPE (@0), | |
3922 | wi::exact_log2 (wi::to_wide (@1))); })))) | |
3923 | ||
84ff66b8 AV |
3924 | /* Fold (X {&,^,|} C2) << C1 into (X << C1) {&,^,|} (C2 << C1) |
3925 | (X {&,^,|} C2) >> C1 into (X >> C1) & (C2 >> C1). */ | |
98e30e51 | 3926 | (for shift (lshift rshift) |
84ff66b8 AV |
3927 | (for bit_op (bit_and bit_xor bit_ior) |
3928 | (simplify | |
3929 | (shift (convert?:s (bit_op:s @0 INTEGER_CST@2)) INTEGER_CST@1) | |
3930 | (if (tree_nop_conversion_p (type, TREE_TYPE (@0))) | |
3931 | (with { tree mask = int_const_binop (shift, fold_convert (type, @2), @1); } | |
39f5e9ad JJ |
3932 | (if (mask) |
3933 | (bit_op (shift (convert @0) @1) { mask; }))))))) | |
98e30e51 | 3934 | |
ad1d92ab MM |
3935 | /* ~(~X >> Y) -> X >> Y (for arithmetic shift). */ |
3936 | (simplify | |
3937 | (bit_not (convert1?:s (rshift:s (convert2?@0 (bit_not @1)) @2))) | |
3938 | (if (!TYPE_UNSIGNED (TREE_TYPE (@0)) | |
ece46666 MG |
3939 | && (element_precision (TREE_TYPE (@0)) |
3940 | <= element_precision (TREE_TYPE (@1)) | |
3941 | || !TYPE_UNSIGNED (TREE_TYPE (@1)))) | |
ad1d92ab MM |
3942 | (with |
3943 | { tree shift_type = TREE_TYPE (@0); } | |
3944 | (convert (rshift (convert:shift_type @1) @2))))) | |
3945 | ||
3946 | /* ~(~X >>r Y) -> X >>r Y | |
3947 | ~(~X <<r Y) -> X <<r Y */ | |
3948 | (for rotate (lrotate rrotate) | |
3949 | (simplify | |
3950 | (bit_not (convert1?:s (rotate:s (convert2?@0 (bit_not @1)) @2))) | |
ece46666 MG |
3951 | (if ((element_precision (TREE_TYPE (@0)) |
3952 | <= element_precision (TREE_TYPE (@1)) | |
3953 | || !TYPE_UNSIGNED (TREE_TYPE (@1))) | |
3954 | && (element_precision (type) <= element_precision (TREE_TYPE (@0)) | |
3955 | || !TYPE_UNSIGNED (TREE_TYPE (@0)))) | |
ad1d92ab MM |
3956 | (with |
3957 | { tree rotate_type = TREE_TYPE (@0); } | |
3958 | (convert (rotate (convert:rotate_type @1) @2)))))) | |
98e30e51 | 3959 | |
cf5f5442 RS |
3960 | (for cmp (eq ne) |
3961 | (for rotate (lrotate rrotate) | |
3962 | invrot (rrotate lrotate) | |
3963 | /* (X >>r Y) cmp (Z >>r Y) may simplify to X cmp Y. */ | |
3964 | (simplify | |
3965 | (cmp (rotate @1 @0) (rotate @2 @0)) | |
3966 | (cmp @1 @2)) | |
3967 | /* (X >>r C1) cmp C2 may simplify to X cmp C3. */ | |
3968 | (simplify | |
3969 | (cmp (rotate @0 INTEGER_CST@1) INTEGER_CST@2) | |
3970 | (cmp @0 { const_binop (invrot, TREE_TYPE (@0), @2, @1); })) | |
3971 | /* (X >>r Y) cmp C where C is 0 or ~0, may simplify to X cmp C. */ | |
3972 | (simplify | |
3973 | (cmp (rotate @0 @1) INTEGER_CST@2) | |
3974 | (if (integer_zerop (@2) || integer_all_onesp (@2)) | |
3975 | (cmp @0 @2))))) | |
3976 | ||
acb1e6f4 | 3977 | /* Narrow a lshift by constant. */ |
1d244020 | 3978 | (simplify |
acb1e6f4 | 3979 | (convert (lshift:s@0 @1 INTEGER_CST@2)) |
78fa5112 | 3980 | (if (INTEGRAL_TYPE_P (type) |
acb1e6f4 RS |
3981 | && INTEGRAL_TYPE_P (TREE_TYPE (@0)) |
3982 | && !integer_zerop (@2) | |
3983 | && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))) | |
3984 | (if (TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (@0)) | |
3985 | || wi::ltu_p (wi::to_wide (@2), TYPE_PRECISION (type))) | |
3986 | (lshift (convert @1) @2) | |
3987 | (if (wi::ltu_p (wi::to_wide (@2), TYPE_PRECISION (TREE_TYPE (@0)))) | |
3988 | { build_zero_cst (type); })))) | |
1d244020 | 3989 | |
d4573ffe RB |
3990 | /* Simplifications of conversions. */ |
3991 | ||
3992 | /* Basic strip-useless-type-conversions / strip_nops. */ | |
f3582e54 | 3993 | (for cvt (convert view_convert float fix_trunc) |
d4573ffe RB |
3994 | (simplify |
3995 | (cvt @0) | |
3996 | (if ((GIMPLE && useless_type_conversion_p (type, TREE_TYPE (@0))) | |
3997 | || (GENERIC && type == TREE_TYPE (@0))) | |
3998 | @0))) | |
3999 | ||
4000 | /* Contract view-conversions. */ | |
4001 | (simplify | |
4002 | (view_convert (view_convert @0)) | |
4003 | (view_convert @0)) | |
4004 | ||
4005 | /* For integral conversions with the same precision or pointer | |
4006 | conversions use a NOP_EXPR instead. */ | |
4007 | (simplify | |
4008 | (view_convert @0) | |
4009 | (if ((INTEGRAL_TYPE_P (type) || POINTER_TYPE_P (type)) | |
4010 | && (INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0))) | |
4011 | && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (@0))) | |
4012 | (convert @0))) | |
4013 | ||
bce8ef71 MG |
4014 | /* Strip inner integral conversions that do not change precision or size, or |
4015 | zero-extend while keeping the same size (for bool-to-char). */ | |
d4573ffe RB |
4016 | (simplify |
4017 | (view_convert (convert@0 @1)) | |
4018 | (if ((INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0))) | |
4019 | && (INTEGRAL_TYPE_P (TREE_TYPE (@1)) || POINTER_TYPE_P (TREE_TYPE (@1))) | |
bce8ef71 MG |
4020 | && TYPE_SIZE (TREE_TYPE (@0)) == TYPE_SIZE (TREE_TYPE (@1)) |
4021 | && (TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1)) | |
4022 | || (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (TREE_TYPE (@1)) | |
4023 | && TYPE_UNSIGNED (TREE_TYPE (@1))))) | |
d4573ffe RB |
4024 | (view_convert @1))) |
4025 | ||
90467f0a | 4026 | /* Simplify a view-converted empty or single-element constructor. */ |
f469220d RB |
4027 | (simplify |
4028 | (view_convert CONSTRUCTOR@0) | |
90467f0a | 4029 | (with |
49bf49bb RB |
4030 | { tree ctor = (TREE_CODE (@0) == SSA_NAME |
4031 | ? gimple_assign_rhs1 (SSA_NAME_DEF_STMT (@0)) : @0); } | |
90467f0a RB |
4032 | (switch |
4033 | (if (CONSTRUCTOR_NELTS (ctor) == 0) | |
4034 | { build_zero_cst (type); }) | |
4035 | (if (CONSTRUCTOR_NELTS (ctor) == 1 | |
4036 | && VECTOR_TYPE_P (TREE_TYPE (ctor)) | |
4037 | && operand_equal_p (TYPE_SIZE (type), | |
4038 | TYPE_SIZE (TREE_TYPE | |
4039 | (CONSTRUCTOR_ELT (ctor, 0)->value)))) | |
4040 | (view_convert { CONSTRUCTOR_ELT (ctor, 0)->value; }))))) | |
f469220d | 4041 | |
d4573ffe RB |
4042 | /* Re-association barriers around constants and other re-association |
4043 | barriers can be removed. */ | |
4044 | (simplify | |
4045 | (paren CONSTANT_CLASS_P@0) | |
4046 | @0) | |
4047 | (simplify | |
4048 | (paren (paren@1 @0)) | |
4049 | @1) | |
1e51d0a2 RB |
4050 | |
4051 | /* Handle cases of two conversions in a row. */ | |
4052 | (for ocvt (convert float fix_trunc) | |
4053 | (for icvt (convert float) | |
4054 | (simplify | |
4055 | (ocvt (icvt@1 @0)) | |
4056 | (with | |
4057 | { | |
4058 | tree inside_type = TREE_TYPE (@0); | |
4059 | tree inter_type = TREE_TYPE (@1); | |
4060 | int inside_int = INTEGRAL_TYPE_P (inside_type); | |
4061 | int inside_ptr = POINTER_TYPE_P (inside_type); | |
4062 | int inside_float = FLOAT_TYPE_P (inside_type); | |
09240451 | 4063 | int inside_vec = VECTOR_TYPE_P (inside_type); |
1e51d0a2 RB |
4064 | unsigned int inside_prec = TYPE_PRECISION (inside_type); |
4065 | int inside_unsignedp = TYPE_UNSIGNED (inside_type); | |
4066 | int inter_int = INTEGRAL_TYPE_P (inter_type); | |
4067 | int inter_ptr = POINTER_TYPE_P (inter_type); | |
4068 | int inter_float = FLOAT_TYPE_P (inter_type); | |
09240451 | 4069 | int inter_vec = VECTOR_TYPE_P (inter_type); |
1e51d0a2 RB |
4070 | unsigned int inter_prec = TYPE_PRECISION (inter_type); |
4071 | int inter_unsignedp = TYPE_UNSIGNED (inter_type); | |
4072 | int final_int = INTEGRAL_TYPE_P (type); | |
4073 | int final_ptr = POINTER_TYPE_P (type); | |
4074 | int final_float = FLOAT_TYPE_P (type); | |
09240451 | 4075 | int final_vec = VECTOR_TYPE_P (type); |
1e51d0a2 RB |
4076 | unsigned int final_prec = TYPE_PRECISION (type); |
4077 | int final_unsignedp = TYPE_UNSIGNED (type); | |
4078 | } | |
64d3a1f0 RB |
4079 | (switch |
4080 | /* In addition to the cases of two conversions in a row | |
4081 | handled below, if we are converting something to its own | |
4082 | type via an object of identical or wider precision, neither | |
4083 | conversion is needed. */ | |
4084 | (if (((GIMPLE && useless_type_conversion_p (type, inside_type)) | |
4085 | || (GENERIC | |
4086 | && TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (inside_type))) | |
4087 | && (((inter_int || inter_ptr) && final_int) | |
4088 | || (inter_float && final_float)) | |
4089 | && inter_prec >= final_prec) | |
4090 | (ocvt @0)) | |
4091 | ||
4092 | /* Likewise, if the intermediate and initial types are either both | |
4093 | float or both integer, we don't need the middle conversion if the | |
4094 | former is wider than the latter and doesn't change the signedness | |
4095 | (for integers). Avoid this if the final type is a pointer since | |
36088299 | 4096 | then we sometimes need the middle conversion. */ |
64d3a1f0 RB |
4097 | (if (((inter_int && inside_int) || (inter_float && inside_float)) |
4098 | && (final_int || final_float) | |
4099 | && inter_prec >= inside_prec | |
36088299 | 4100 | && (inter_float || inter_unsignedp == inside_unsignedp)) |
64d3a1f0 RB |
4101 | (ocvt @0)) |
4102 | ||
4103 | /* If we have a sign-extension of a zero-extended value, we can | |
4104 | replace that by a single zero-extension. Likewise if the | |
4105 | final conversion does not change precision we can drop the | |
4106 | intermediate conversion. */ | |
4107 | (if (inside_int && inter_int && final_int | |
4108 | && ((inside_prec < inter_prec && inter_prec < final_prec | |
4109 | && inside_unsignedp && !inter_unsignedp) | |
4110 | || final_prec == inter_prec)) | |
4111 | (ocvt @0)) | |
4112 | ||
4113 | /* Two conversions in a row are not needed unless: | |
1e51d0a2 RB |
4114 | - some conversion is floating-point (overstrict for now), or |
4115 | - some conversion is a vector (overstrict for now), or | |
4116 | - the intermediate type is narrower than both initial and | |
4117 | final, or | |
4118 | - the intermediate type and innermost type differ in signedness, | |
4119 | and the outermost type is wider than the intermediate, or | |
4120 | - the initial type is a pointer type and the precisions of the | |
4121 | intermediate and final types differ, or | |
4122 | - the final type is a pointer type and the precisions of the | |
4123 | initial and intermediate types differ. */ | |
64d3a1f0 RB |
4124 | (if (! inside_float && ! inter_float && ! final_float |
4125 | && ! inside_vec && ! inter_vec && ! final_vec | |
4126 | && (inter_prec >= inside_prec || inter_prec >= final_prec) | |
4127 | && ! (inside_int && inter_int | |
4128 | && inter_unsignedp != inside_unsignedp | |
4129 | && inter_prec < final_prec) | |
4130 | && ((inter_unsignedp && inter_prec > inside_prec) | |
4131 | == (final_unsignedp && final_prec > inter_prec)) | |
4132 | && ! (inside_ptr && inter_prec != final_prec) | |
36088299 | 4133 | && ! (final_ptr && inside_prec != inter_prec)) |
64d3a1f0 RB |
4134 | (ocvt @0)) |
4135 | ||
4136 | /* A truncation to an unsigned type (a zero-extension) should be | |
4137 | canonicalized as bitwise and of a mask. */ | |
1d510e04 JJ |
4138 | (if (GIMPLE /* PR70366: doing this in GENERIC breaks -Wconversion. */ |
4139 | && final_int && inter_int && inside_int | |
64d3a1f0 RB |
4140 | && final_prec == inside_prec |
4141 | && final_prec > inter_prec | |
4142 | && inter_unsignedp) | |
4143 | (convert (bit_and @0 { wide_int_to_tree | |
4144 | (inside_type, | |
4145 | wi::mask (inter_prec, false, | |
4146 | TYPE_PRECISION (inside_type))); }))) | |
4147 | ||
4148 | /* If we are converting an integer to a floating-point that can | |
4149 | represent it exactly and back to an integer, we can skip the | |
4150 | floating-point conversion. */ | |
4151 | (if (GIMPLE /* PR66211 */ | |
4152 | && inside_int && inter_float && final_int && | |
4153 | (unsigned) significand_size (TYPE_MODE (inter_type)) | |
4154 | >= inside_prec - !inside_unsignedp) | |
4155 | (convert @0))))))) | |
ea2042ba | 4156 | |
914045df ASDV |
4157 | /* (float_type)(integer_type) x -> trunc (x) if the type of x matches |
4158 | float_type. Only do the transformation if we do not need to preserve | |
4159 | trapping behaviour, so require !flag_trapping_math. */ | |
4160 | #if GIMPLE | |
4161 | (simplify | |
4162 | (float (fix_trunc @0)) | |
4163 | (if (!flag_trapping_math | |
4164 | && types_match (type, TREE_TYPE (@0)) | |
4165 | && direct_internal_fn_supported_p (IFN_TRUNC, type, | |
4166 | OPTIMIZE_FOR_BOTH)) | |
4167 | (IFN_TRUNC @0))) | |
4168 | #endif | |
4169 | ||
ea2042ba RB |
4170 | /* If we have a narrowing conversion to an integral type that is fed by a |
4171 | BIT_AND_EXPR, we might be able to remove the BIT_AND_EXPR if it merely | |
4172 | masks off bits outside the final type (and nothing else). */ | |
4173 | (simplify | |
4174 | (convert (bit_and @0 INTEGER_CST@1)) | |
4175 | (if (INTEGRAL_TYPE_P (type) | |
4176 | && INTEGRAL_TYPE_P (TREE_TYPE (@0)) | |
4177 | && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0)) | |
4178 | && operand_equal_p (@1, build_low_bits_mask (TREE_TYPE (@1), | |
4179 | TYPE_PRECISION (type)), 0)) | |
4180 | (convert @0))) | |
a25454ea RB |
4181 | |
4182 | ||
4183 | /* (X /[ex] A) * A -> X. */ | |
4184 | (simplify | |
2eef1fc1 RB |
4185 | (mult (convert1? (exact_div @0 @@1)) (convert2? @1)) |
4186 | (convert @0)) | |
eaeba53a | 4187 | |
839d0860 RB |
4188 | /* Simplify (A / B) * B + (A % B) -> A. */ |
4189 | (for div (trunc_div ceil_div floor_div round_div) | |
4190 | mod (trunc_mod ceil_mod floor_mod round_mod) | |
4191 | (simplify | |
4192 | (plus:c (mult:c (div @0 @1) @1) (mod @0 @1)) | |
4193 | @0)) | |
4194 | ||
388fbbd8 SF |
4195 | /* x / y * y == x -> x % y == 0. */ |
4196 | (simplify | |
4197 | (eq:c (mult:c (trunc_div:s @0 @1) @1) @0) | |
4198 | (if (TREE_CODE (TREE_TYPE (@0)) != COMPLEX_TYPE) | |
4199 | (eq (trunc_mod @0 @1) { build_zero_cst (TREE_TYPE (@0)); }))) | |
4200 | ||
0036218b MG |
4201 | /* ((X /[ex] A) +- B) * A --> X +- A * B. */ |
4202 | (for op (plus minus) | |
4203 | (simplify | |
4204 | (mult (convert1? (op (convert2? (exact_div @0 INTEGER_CST@@1)) INTEGER_CST@2)) @1) | |
4205 | (if (tree_nop_conversion_p (type, TREE_TYPE (@2)) | |
4206 | && tree_nop_conversion_p (TREE_TYPE (@0), TREE_TYPE (@2))) | |
4207 | (with | |
4208 | { | |
4209 | wi::overflow_type overflow; | |
4210 | wide_int mul = wi::mul (wi::to_wide (@1), wi::to_wide (@2), | |
4211 | TYPE_SIGN (type), &overflow); | |
4212 | } | |
4213 | (if (types_match (type, TREE_TYPE (@2)) | |
4214 | && types_match (TREE_TYPE (@0), TREE_TYPE (@2)) && !overflow) | |
4215 | (op @0 { wide_int_to_tree (type, mul); }) | |
4216 | (with { tree utype = unsigned_type_for (type); } | |
4217 | (convert (op (convert:utype @0) | |
4218 | (mult (convert:utype @1) (convert:utype @2)))))))))) | |
4219 | ||
a7f24614 RB |
4220 | /* Canonicalization of binary operations. */ |
4221 | ||
4222 | /* Convert X + -C into X - C. */ | |
4223 | (simplify | |
4224 | (plus @0 REAL_CST@1) | |
4225 | (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1))) | |
23f27839 | 4226 | (with { tree tem = const_unop (NEGATE_EXPR, type, @1); } |
a7f24614 RB |
4227 | (if (!TREE_OVERFLOW (tem) || !flag_trapping_math) |
4228 | (minus @0 { tem; }))))) | |
4229 | ||
6b6aa8d3 | 4230 | /* Convert x+x into x*2. */ |
a7f24614 RB |
4231 | (simplify |
4232 | (plus @0 @0) | |
4233 | (if (SCALAR_FLOAT_TYPE_P (type)) | |
6b6aa8d3 MG |
4234 | (mult @0 { build_real (type, dconst2); }) |
4235 | (if (INTEGRAL_TYPE_P (type)) | |
4236 | (mult @0 { build_int_cst (type, 2); })))) | |
a7f24614 | 4237 | |
406520e2 | 4238 | /* 0 - X -> -X. */ |
a7f24614 RB |
4239 | (simplify |
4240 | (minus integer_zerop @1) | |
4241 | (negate @1)) | |
406520e2 MG |
4242 | (simplify |
4243 | (pointer_diff integer_zerop @1) | |
4244 | (negate (convert @1))) | |
a7f24614 RB |
4245 | |
4246 | /* (ARG0 - ARG1) is the same as (-ARG1 + ARG0). So check whether | |
4247 | ARG0 is zero and X + ARG0 reduces to X, since that would mean | |
4248 | (-ARG1 + ARG0) reduces to -ARG1. */ | |
4249 | (simplify | |
4250 | (minus real_zerop@0 @1) | |
5b02ed4b | 4251 | (if (fold_real_zero_addition_p (type, @1, @0, 0)) |
a7f24614 RB |
4252 | (negate @1))) |
4253 | ||
4254 | /* Transform x * -1 into -x. */ | |
4255 | (simplify | |
4256 | (mult @0 integer_minus_onep) | |
4257 | (negate @0)) | |
eaeba53a | 4258 | |
b771c609 AM |
4259 | /* Reassociate (X * CST) * Y to (X * Y) * CST. This does not introduce |
4260 | signed overflow for CST != 0 && CST != -1. */ | |
4261 | (simplify | |
b46ebc6c | 4262 | (mult:c (mult:s@3 @0 INTEGER_CST@1) @2) |
b771c609 | 4263 | (if (TREE_CODE (@2) != INTEGER_CST |
b46ebc6c | 4264 | && single_use (@3) |
b771c609 AM |
4265 | && !integer_zerop (@1) && !integer_minus_onep (@1)) |
4266 | (mult (mult @0 @2) @1))) | |
4267 | ||
96285749 RS |
4268 | /* True if we can easily extract the real and imaginary parts of a complex |
4269 | number. */ | |
4270 | (match compositional_complex | |
4271 | (convert? (complex @0 @1))) | |
4272 | ||
eaeba53a RB |
4273 | /* COMPLEX_EXPR and REALPART/IMAGPART_EXPR cancellations. */ |
4274 | (simplify | |
4275 | (complex (realpart @0) (imagpart @0)) | |
4276 | @0) | |
4277 | (simplify | |
4278 | (realpart (complex @0 @1)) | |
4279 | @0) | |
4280 | (simplify | |
4281 | (imagpart (complex @0 @1)) | |
4282 | @1) | |
83633539 | 4283 | |
77c028c5 MG |
4284 | /* Sometimes we only care about half of a complex expression. */ |
4285 | (simplify | |
4286 | (realpart (convert?:s (conj:s @0))) | |
4287 | (convert (realpart @0))) | |
4288 | (simplify | |
4289 | (imagpart (convert?:s (conj:s @0))) | |
4290 | (convert (negate (imagpart @0)))) | |
4291 | (for part (realpart imagpart) | |
4292 | (for op (plus minus) | |
4293 | (simplify | |
4294 | (part (convert?:s@2 (op:s @0 @1))) | |
4295 | (convert (op (part @0) (part @1)))))) | |
4296 | (simplify | |
4297 | (realpart (convert?:s (CEXPI:s @0))) | |
4298 | (convert (COS @0))) | |
4299 | (simplify | |
4300 | (imagpart (convert?:s (CEXPI:s @0))) | |
4301 | (convert (SIN @0))) | |
4302 | ||
4303 | /* conj(conj(x)) -> x */ | |
4304 | (simplify | |
4305 | (conj (convert? (conj @0))) | |
4306 | (if (tree_nop_conversion_p (TREE_TYPE (@0), type)) | |
4307 | (convert @0))) | |
4308 | ||
4309 | /* conj({x,y}) -> {x,-y} */ | |
4310 | (simplify | |
4311 | (conj (convert?:s (complex:s @0 @1))) | |
4312 | (with { tree itype = TREE_TYPE (type); } | |
4313 | (complex (convert:itype @0) (negate (convert:itype @1))))) | |
83633539 RB |
4314 | |
4315 | /* BSWAP simplifications, transforms checked by gcc.dg/builtin-bswap-8.c. */ | |
4c619132 RS |
4316 | (for bswap (BUILT_IN_BSWAP16 BUILT_IN_BSWAP32 |
4317 | BUILT_IN_BSWAP64 BUILT_IN_BSWAP128) | |
83633539 RB |
4318 | (simplify |
4319 | (bswap (bswap @0)) | |
4320 | @0) | |
4321 | (simplify | |
4322 | (bswap (bit_not (bswap @0))) | |
4323 | (bit_not @0)) | |
4324 | (for bitop (bit_xor bit_ior bit_and) | |
4325 | (simplify | |
4326 | (bswap (bitop:c (bswap @0) @1)) | |
4c619132 | 4327 | (bitop @0 (bswap @1)))) |
cf5f5442 RS |
4328 | (for cmp (eq ne) |
4329 | (simplify | |
b4fc4df9 JJ |
4330 | (cmp (bswap@2 @0) (bswap @1)) |
4331 | (with { tree ctype = TREE_TYPE (@2); } | |
4332 | (cmp (convert:ctype @0) (convert:ctype @1)))) | |
cf5f5442 RS |
4333 | (simplify |
4334 | (cmp (bswap @0) INTEGER_CST@1) | |
b4fc4df9 | 4335 | (with { tree ctype = TREE_TYPE (@1); } |
fdc46830 | 4336 | (cmp (convert:ctype @0) (bswap! @1))))) |
4c619132 RS |
4337 | /* (bswap(x) >> C1) & C2 can sometimes be simplified to (x >> C3) & C2. */ |
4338 | (simplify | |
4339 | (bit_and (convert1? (rshift@0 (convert2? (bswap@4 @1)) INTEGER_CST@2)) | |
4340 | INTEGER_CST@3) | |
4341 | (if (BITS_PER_UNIT == 8 | |
4342 | && tree_fits_uhwi_p (@2) | |
4343 | && tree_fits_uhwi_p (@3)) | |
4344 | (with | |
4345 | { | |
4346 | unsigned HOST_WIDE_INT prec = TYPE_PRECISION (TREE_TYPE (@4)); | |
4347 | unsigned HOST_WIDE_INT bits = tree_to_uhwi (@2); | |
4348 | unsigned HOST_WIDE_INT mask = tree_to_uhwi (@3); | |
4349 | unsigned HOST_WIDE_INT lo = bits & 7; | |
4350 | unsigned HOST_WIDE_INT hi = bits - lo; | |
4351 | } | |
4352 | (if (bits < prec | |
4353 | && mask < (256u>>lo) | |
4354 | && bits < TYPE_PRECISION (TREE_TYPE(@0))) | |
4355 | (with { unsigned HOST_WIDE_INT ns = (prec - (hi + 8)) + lo; } | |
4356 | (if (ns == 0) | |
4357 | (bit_and (convert @1) @3) | |
4358 | (with | |
4359 | { | |
4360 | tree utype = unsigned_type_for (TREE_TYPE (@1)); | |
4361 | tree nst = build_int_cst (integer_type_node, ns); | |
4362 | } | |
4363 | (bit_and (convert (rshift:utype (convert:utype @1) {nst;})) @3)))))))) | |
4364 | /* bswap(x) >> C1 can sometimes be simplified to (T)x >> C2. */ | |
4365 | (simplify | |
4366 | (rshift (convert? (bswap@2 @0)) INTEGER_CST@1) | |
4367 | (if (BITS_PER_UNIT == 8 | |
4368 | && CHAR_TYPE_SIZE == 8 | |
4369 | && tree_fits_uhwi_p (@1)) | |
4370 | (with | |
4371 | { | |
4372 | unsigned HOST_WIDE_INT prec = TYPE_PRECISION (TREE_TYPE (@2)); | |
4373 | unsigned HOST_WIDE_INT bits = tree_to_uhwi (@1); | |
5f5fbb55 RS |
4374 | /* If the bswap was extended before the original shift, this |
4375 | byte (shift) has the sign of the extension, not the sign of | |
4376 | the original shift. */ | |
4377 | tree st = TYPE_PRECISION (type) > prec ? TREE_TYPE (@2) : type; | |
4c619132 | 4378 | } |
5f5fbb55 RS |
4379 | /* Special case: logical right shift of sign-extended bswap. |
4380 | (unsigned)(short)bswap16(x)>>12 is (unsigned)((short)x<<8)>>12. */ | |
4381 | (if (TYPE_PRECISION (type) > prec | |
4382 | && !TYPE_UNSIGNED (TREE_TYPE (@2)) | |
4383 | && TYPE_UNSIGNED (type) | |
4384 | && bits < prec && bits + 8 >= prec) | |
4385 | (with { tree nst = build_int_cst (integer_type_node, prec - 8); } | |
4386 | (rshift (convert (lshift:st (convert:st @0) {nst;})) @1)) | |
4387 | (if (bits + 8 == prec) | |
4388 | (if (TYPE_UNSIGNED (st)) | |
4389 | (convert (convert:unsigned_char_type_node @0)) | |
4390 | (convert (convert:signed_char_type_node @0))) | |
4391 | (if (bits < prec && bits + 8 > prec) | |
4392 | (with | |
4393 | { | |
4394 | tree nst = build_int_cst (integer_type_node, bits & 7); | |
4395 | tree bt = TYPE_UNSIGNED (st) ? unsigned_char_type_node | |
4396 | : signed_char_type_node; | |
4397 | } | |
4398 | (convert (rshift:bt (convert:bt @0) {nst;}))))))))) | |
4c619132 RS |
4399 | /* bswap(x) & C1 can sometimes be simplified to (x >> C2) & C1. */ |
4400 | (simplify | |
4401 | (bit_and (convert? (bswap@2 @0)) INTEGER_CST@1) | |
4402 | (if (BITS_PER_UNIT == 8 | |
4403 | && tree_fits_uhwi_p (@1) | |
4404 | && tree_to_uhwi (@1) < 256) | |
4405 | (with | |
4406 | { | |
4407 | unsigned HOST_WIDE_INT prec = TYPE_PRECISION (TREE_TYPE (@2)); | |
4408 | tree utype = unsigned_type_for (TREE_TYPE (@0)); | |
4409 | tree nst = build_int_cst (integer_type_node, prec - 8); | |
4410 | } | |
4411 | (bit_and (convert (rshift:utype (convert:utype @0) {nst;})) @1))))) | |
96994de0 RB |
4412 | |
4413 | ||
4414 | /* Combine COND_EXPRs and VEC_COND_EXPRs. */ | |
4415 | ||
4416 | /* Simplify constant conditions. | |
4417 | Only optimize constant conditions when the selected branch | |
4418 | has the same type as the COND_EXPR. This avoids optimizing | |
4419 | away "c ? x : throw", where the throw has a void type. | |
e53b6e56 | 4420 | Note that we cannot throw away the fold-const.cc variant nor |
96994de0 | 4421 | this one as we depend on doing this transform before possibly |
e53b6e56 | 4422 | A ? B : B -> B triggers and the fold-const.cc one can optimize |
96994de0 RB |
4423 | 0 ? A : B to B even if A has side-effects. Something |
4424 | genmatch cannot handle. */ | |
4425 | (simplify | |
4426 | (cond INTEGER_CST@0 @1 @2) | |
8fdc6c67 RB |
4427 | (if (integer_zerop (@0)) |
4428 | (if (!VOID_TYPE_P (TREE_TYPE (@2)) || VOID_TYPE_P (type)) | |
4429 | @2) | |
4430 | (if (!VOID_TYPE_P (TREE_TYPE (@1)) || VOID_TYPE_P (type)) | |
4431 | @1))) | |
96994de0 RB |
4432 | (simplify |
4433 | (vec_cond VECTOR_CST@0 @1 @2) | |
4434 | (if (integer_all_onesp (@0)) | |
8fdc6c67 RB |
4435 | @1 |
4436 | (if (integer_zerop (@0)) | |
4437 | @2))) | |
96994de0 | 4438 | |
229752af | 4439 | /* Sink unary operations to branches, but only if we do fold both. */ |
34a13a52 MG |
4440 | (for op (negate bit_not abs absu) |
4441 | (simplify | |
229752af MG |
4442 | (op (vec_cond:s @0 @1 @2)) |
4443 | (vec_cond @0 (op! @1) (op! @2)))) | |
4444 | ||
4445 | /* Sink binary operation to branches, but only if we can fold it. */ | |
4446 | (for op (tcc_comparison plus minus mult bit_and bit_ior bit_xor | |
5240c5ca | 4447 | lshift rshift rdiv trunc_div ceil_div floor_div round_div |
229752af MG |
4448 | trunc_mod ceil_mod floor_mod round_mod min max) |
4449 | /* (c ? a : b) op (c ? d : e) --> c ? (a op d) : (b op e) */ | |
4450 | (simplify | |
4451 | (op (vec_cond:s @0 @1 @2) (vec_cond:s @0 @3 @4)) | |
4452 | (vec_cond @0 (op! @1 @3) (op! @2 @4))) | |
4453 | ||
4454 | /* (c ? a : b) op d --> c ? (a op d) : (b op d) */ | |
4455 | (simplify | |
4456 | (op (vec_cond:s @0 @1 @2) @3) | |
4457 | (vec_cond @0 (op! @1 @3) (op! @2 @3))) | |
4458 | (simplify | |
4459 | (op @3 (vec_cond:s @0 @1 @2)) | |
4460 | (vec_cond @0 (op! @3 @1) (op! @3 @2)))) | |
229752af | 4461 | |
fb161782 | 4462 | #if GIMPLE |
4463 | (match (nop_atomic_bit_test_and_p @0 @1 @4) | |
7df89377 | 4464 | (bit_and (convert?@4 (ATOMIC_FETCH_OR_XOR_N @2 INTEGER_CST@0 @3)) |
fb161782 | 4465 | INTEGER_CST@1) |
4466 | (with { | |
4467 | int ibit = tree_log2 (@0); | |
4468 | int ibit2 = tree_log2 (@1); | |
4469 | } | |
4470 | (if (ibit == ibit2 | |
7df89377 | 4471 | && ibit >= 0 |
379be00f | 4472 | && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0)))))) |
fb161782 | 4473 | |
4474 | (match (nop_atomic_bit_test_and_p @0 @1 @3) | |
7df89377 | 4475 | (bit_and (convert?@3 (SYNC_FETCH_OR_XOR_N @2 INTEGER_CST@0)) |
fb161782 | 4476 | INTEGER_CST@1) |
4477 | (with { | |
4478 | int ibit = tree_log2 (@0); | |
4479 | int ibit2 = tree_log2 (@1); | |
4480 | } | |
4481 | (if (ibit == ibit2 | |
7df89377 | 4482 | && ibit >= 0 |
379be00f | 4483 | && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0)))))) |
fb161782 | 4484 | |
4485 | (match (nop_atomic_bit_test_and_p @0 @0 @4) | |
4486 | (bit_and:c | |
7df89377 | 4487 | (convert1?@4 |
fb161782 | 4488 | (ATOMIC_FETCH_OR_XOR_N @2 (nop_convert? (lshift@0 integer_onep@5 @6)) @3)) |
7df89377 | 4489 | (convert2? @0)) |
379be00f | 4490 | (if (TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))))) |
fb161782 | 4491 | |
4492 | (match (nop_atomic_bit_test_and_p @0 @0 @4) | |
4493 | (bit_and:c | |
7df89377 | 4494 | (convert1?@4 |
fb161782 | 4495 | (SYNC_FETCH_OR_XOR_N @2 (nop_convert? (lshift@0 integer_onep@3 @5)))) |
7df89377 | 4496 | (convert2? @0)) |
379be00f | 4497 | (if (TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))))) |
fb161782 | 4498 | |
4499 | (match (nop_atomic_bit_test_and_p @0 @1 @3) | |
7df89377 | 4500 | (bit_and@4 (convert?@3 (ATOMIC_FETCH_AND_N @2 INTEGER_CST@0 @5)) |
fb161782 | 4501 | INTEGER_CST@1) |
4502 | (with { | |
4503 | int ibit = wi::exact_log2 (wi::zext (wi::bit_not (wi::to_wide (@0)), | |
4504 | TYPE_PRECISION(type))); | |
4505 | int ibit2 = tree_log2 (@1); | |
4506 | } | |
4507 | (if (ibit == ibit2 | |
7df89377 | 4508 | && ibit >= 0 |
379be00f | 4509 | && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0)))))) |
fb161782 | 4510 | |
4511 | (match (nop_atomic_bit_test_and_p @0 @1 @3) | |
4512 | (bit_and@4 | |
7df89377 | 4513 | (convert?@3 (SYNC_FETCH_AND_AND_N @2 INTEGER_CST@0)) |
fb161782 | 4514 | INTEGER_CST@1) |
4515 | (with { | |
4516 | int ibit = wi::exact_log2 (wi::zext (wi::bit_not (wi::to_wide (@0)), | |
4517 | TYPE_PRECISION(type))); | |
4518 | int ibit2 = tree_log2 (@1); | |
4519 | } | |
4520 | (if (ibit == ibit2 | |
7df89377 | 4521 | && ibit >= 0 |
379be00f | 4522 | && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0)))))) |
fb161782 | 4523 | |
7df89377 | 4524 | (match (nop_atomic_bit_test_and_p @4 @0 @3) |
fb161782 | 4525 | (bit_and:c |
7df89377 | 4526 | (convert1?@3 |
4527 | (ATOMIC_FETCH_AND_N @2 (nop_convert?@4 (bit_not (lshift@0 integer_onep@6 @7))) @5)) | |
4528 | (convert2? @0)) | |
379be00f | 4529 | (if (TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@4))))) |
fb161782 | 4530 | |
7df89377 | 4531 | (match (nop_atomic_bit_test_and_p @4 @0 @3) |
fb161782 | 4532 | (bit_and:c |
7df89377 | 4533 | (convert1?@3 |
4534 | (SYNC_FETCH_AND_AND_N @2 (nop_convert?@4 (bit_not (lshift@0 integer_onep@6 @7))))) | |
4535 | (convert2? @0)) | |
379be00f | 4536 | (if (TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@4))))) |
fb161782 | 4537 | |
4538 | #endif | |
4539 | ||
a1ee6d50 MG |
4540 | /* (v ? w : 0) ? a : b is just (v & w) ? a : b |
4541 | Currently disabled after pass lvec because ARM understands | |
4542 | VEC_COND_EXPR<v==w,-1,0> but not a plain v==w fed to BIT_IOR_EXPR. */ | |
229752af MG |
4543 | (simplify |
4544 | (vec_cond (vec_cond:s @0 @3 integer_zerop) @1 @2) | |
a1ee6d50 | 4545 | (if (optimize_vectors_before_lowering_p () && types_match (@0, @3)) |
229752af MG |
4546 | (vec_cond (bit_and @0 @3) @1 @2))) |
4547 | (simplify | |
4548 | (vec_cond (vec_cond:s @0 integer_all_onesp @3) @1 @2) | |
a1ee6d50 | 4549 | (if (optimize_vectors_before_lowering_p () && types_match (@0, @3)) |
229752af MG |
4550 | (vec_cond (bit_ior @0 @3) @1 @2))) |
4551 | (simplify | |
4552 | (vec_cond (vec_cond:s @0 integer_zerop @3) @1 @2) | |
a1ee6d50 | 4553 | (if (optimize_vectors_before_lowering_p () && types_match (@0, @3)) |
229752af MG |
4554 | (vec_cond (bit_ior @0 (bit_not @3)) @2 @1))) |
4555 | (simplify | |
4556 | (vec_cond (vec_cond:s @0 @3 integer_all_onesp) @1 @2) | |
a1ee6d50 | 4557 | (if (optimize_vectors_before_lowering_p () && types_match (@0, @3)) |
229752af MG |
4558 | (vec_cond (bit_and @0 (bit_not @3)) @2 @1))) |
4559 | ||
4560 | /* c1 ? c2 ? a : b : b --> (c1 & c2) ? a : b */ | |
4561 | (simplify | |
4562 | (vec_cond @0 (vec_cond:s @1 @2 @3) @3) | |
a1ee6d50 | 4563 | (if (optimize_vectors_before_lowering_p () && types_match (@0, @1)) |
229752af MG |
4564 | (vec_cond (bit_and @0 @1) @2 @3))) |
4565 | (simplify | |
4566 | (vec_cond @0 @2 (vec_cond:s @1 @2 @3)) | |
a1ee6d50 | 4567 | (if (optimize_vectors_before_lowering_p () && types_match (@0, @1)) |
229752af MG |
4568 | (vec_cond (bit_ior @0 @1) @2 @3))) |
4569 | (simplify | |
4570 | (vec_cond @0 (vec_cond:s @1 @2 @3) @2) | |
a1ee6d50 | 4571 | (if (optimize_vectors_before_lowering_p () && types_match (@0, @1)) |
229752af MG |
4572 | (vec_cond (bit_ior (bit_not @0) @1) @2 @3))) |
4573 | (simplify | |
4574 | (vec_cond @0 @3 (vec_cond:s @1 @2 @3)) | |
a1ee6d50 | 4575 | (if (optimize_vectors_before_lowering_p () && types_match (@0, @1)) |
229752af | 4576 | (vec_cond (bit_and (bit_not @0) @1) @2 @3))) |
34a13a52 | 4577 | |
10843f83 RB |
4578 | /* Canonicalize mask ? { 0, ... } : { -1, ...} to ~mask if the mask |
4579 | types are compatible. */ | |
4580 | (simplify | |
4581 | (vec_cond @0 VECTOR_CST@1 VECTOR_CST@2) | |
4582 | (if (VECTOR_BOOLEAN_TYPE_P (type) | |
4583 | && types_match (type, TREE_TYPE (@0))) | |
4584 | (if (integer_zerop (@1) && integer_all_onesp (@2)) | |
4585 | (bit_not @0) | |
4586 | (if (integer_all_onesp (@1) && integer_zerop (@2)) | |
4587 | @0)))) | |
4588 | ||
b6bdd7a4 AP |
4589 | /* A few simplifications of "a ? CST1 : CST2". */ |
4590 | /* NOTE: Only do this on gimple as the if-chain-to-switch | |
4591 | optimization depends on the gimple to have if statements in it. */ | |
4592 | #if GIMPLE | |
4593 | (simplify | |
4594 | (cond @0 INTEGER_CST@1 INTEGER_CST@2) | |
4595 | (switch | |
4596 | (if (integer_zerop (@2)) | |
4597 | (switch | |
4598 | /* a ? 1 : 0 -> a if 0 and 1 are integral types. */ | |
4599 | (if (integer_onep (@1)) | |
4600 | (convert (convert:boolean_type_node @0))) | |
b6bdd7a4 | 4601 | /* a ? powerof2cst : 0 -> a << (log2(powerof2cst)) */ |
d4faa36e | 4602 | (if (INTEGRAL_TYPE_P (type) && integer_pow2p (@1)) |
b6bdd7a4 AP |
4603 | (with { |
4604 | tree shift = build_int_cst (integer_type_node, tree_log2 (@1)); | |
4605 | } | |
882d806c AP |
4606 | (lshift (convert (convert:boolean_type_node @0)) { shift; }))) |
4607 | /* a ? -1 : 0 -> -a. No need to check the TYPE_PRECISION not being 1 | |
4608 | here as the powerof2cst case above will handle that case correctly. */ | |
4609 | (if (INTEGRAL_TYPE_P (type) && integer_all_onesp (@1)) | |
4610 | (negate (convert (convert:boolean_type_node @0)))))) | |
b6bdd7a4 AP |
4611 | (if (integer_zerop (@1)) |
4612 | (with { | |
4613 | tree booltrue = constant_boolean_node (true, boolean_type_node); | |
4614 | } | |
4615 | (switch | |
4616 | /* a ? 0 : 1 -> !a. */ | |
4617 | (if (integer_onep (@2)) | |
4618 | (convert (bit_xor (convert:boolean_type_node @0) { booltrue; } ))) | |
b6bdd7a4 | 4619 | /* a ? powerof2cst : 0 -> (!a) << (log2(powerof2cst)) */ |
f7844b6a | 4620 | (if (INTEGRAL_TYPE_P (type) && integer_pow2p (@2)) |
b6bdd7a4 AP |
4621 | (with { |
4622 | tree shift = build_int_cst (integer_type_node, tree_log2 (@2)); | |
4623 | } | |
4624 | (lshift (convert (bit_xor (convert:boolean_type_node @0) { booltrue; } )) | |
882d806c AP |
4625 | { shift; }))) |
4626 | /* a ? -1 : 0 -> -(!a). No need to check the TYPE_PRECISION not being 1 | |
4627 | here as the powerof2cst case above will handle that case correctly. */ | |
4628 | (if (INTEGRAL_TYPE_P (type) && integer_all_onesp (@2)) | |
4629 | (negate (convert (bit_xor (convert:boolean_type_node @0) { booltrue; } )))) | |
4630 | ) | |
4631 | ) | |
4632 | ) | |
4633 | ) | |
4634 | ) | |
b6bdd7a4 AP |
4635 | #endif |
4636 | ||
9991d84d RS |
4637 | (simplify |
4638 | (convert (cond@0 @1 INTEGER_CST@2 INTEGER_CST@3)) | |
4639 | (if (INTEGRAL_TYPE_P (type) | |
4640 | && INTEGRAL_TYPE_P (TREE_TYPE (@0))) | |
4641 | (cond @1 (convert @2) (convert @3)))) | |
4642 | ||
b5481987 BC |
4643 | /* Simplification moved from fold_cond_expr_with_comparison. It may also |
4644 | be extended. */ | |
e2535011 BC |
4645 | /* This pattern implements two kinds simplification: |
4646 | ||
4647 | Case 1) | |
4648 | (cond (cmp (convert1? x) c1) (convert2? x) c2) -> (minmax (x c)) if: | |
b5481987 BC |
4649 | 1) Conversions are type widening from smaller type. |
4650 | 2) Const c1 equals to c2 after canonicalizing comparison. | |
4651 | 3) Comparison has tree code LT, LE, GT or GE. | |
4652 | This specific pattern is needed when (cmp (convert x) c) may not | |
4653 | be simplified by comparison patterns because of multiple uses of | |
4654 | x. It also makes sense here because simplifying across multiple | |
e2535011 BC |
4655 | referred var is always benefitial for complicated cases. |
4656 | ||
4657 | Case 2) | |
4658 | (cond (eq (convert1? x) c1) (convert2? x) c2) -> (cond (eq x c1) c1 c2). */ | |
4659 | (for cmp (lt le gt ge eq) | |
b5481987 | 4660 | (simplify |
ae22bc5d | 4661 | (cond (cmp (convert1? @1) INTEGER_CST@3) (convert2? @1) INTEGER_CST@2) |
b5481987 BC |
4662 | (with |
4663 | { | |
4664 | tree from_type = TREE_TYPE (@1); | |
4665 | tree c1_type = TREE_TYPE (@3), c2_type = TREE_TYPE (@2); | |
ae22bc5d | 4666 | enum tree_code code = ERROR_MARK; |
b5481987 | 4667 | |
ae22bc5d BC |
4668 | if (INTEGRAL_TYPE_P (from_type) |
4669 | && int_fits_type_p (@2, from_type) | |
b5481987 BC |
4670 | && (types_match (c1_type, from_type) |
4671 | || (TYPE_PRECISION (c1_type) > TYPE_PRECISION (from_type) | |
4672 | && (TYPE_UNSIGNED (from_type) | |
4673 | || TYPE_SIGN (c1_type) == TYPE_SIGN (from_type)))) | |
4674 | && (types_match (c2_type, from_type) | |
4675 | || (TYPE_PRECISION (c2_type) > TYPE_PRECISION (from_type) | |
4676 | && (TYPE_UNSIGNED (from_type) | |
4677 | || TYPE_SIGN (c2_type) == TYPE_SIGN (from_type))))) | |
4678 | { | |
ae22bc5d | 4679 | if (cmp != EQ_EXPR) |
b5481987 | 4680 | { |
e2535011 BC |
4681 | if (wi::to_widest (@3) == (wi::to_widest (@2) - 1)) |
4682 | { | |
4683 | /* X <= Y - 1 equals to X < Y. */ | |
ae22bc5d | 4684 | if (cmp == LE_EXPR) |
e2535011 BC |
4685 | code = LT_EXPR; |
4686 | /* X > Y - 1 equals to X >= Y. */ | |
ae22bc5d | 4687 | if (cmp == GT_EXPR) |
e2535011 BC |
4688 | code = GE_EXPR; |
4689 | } | |
4690 | if (wi::to_widest (@3) == (wi::to_widest (@2) + 1)) | |
4691 | { | |
4692 | /* X < Y + 1 equals to X <= Y. */ | |
ae22bc5d | 4693 | if (cmp == LT_EXPR) |
e2535011 BC |
4694 | code = LE_EXPR; |
4695 | /* X >= Y + 1 equals to X > Y. */ | |
ae22bc5d | 4696 | if (cmp == GE_EXPR) |
e2535011 BC |
4697 | code = GT_EXPR; |
4698 | } | |
ae22bc5d BC |
4699 | if (code != ERROR_MARK |
4700 | || wi::to_widest (@2) == wi::to_widest (@3)) | |
e2535011 | 4701 | { |
ae22bc5d | 4702 | if (cmp == LT_EXPR || cmp == LE_EXPR) |
e2535011 | 4703 | code = MIN_EXPR; |
ae22bc5d | 4704 | if (cmp == GT_EXPR || cmp == GE_EXPR) |
e2535011 BC |
4705 | code = MAX_EXPR; |
4706 | } | |
b5481987 | 4707 | } |
e2535011 | 4708 | /* Can do A == C1 ? A : C2 -> A == C1 ? C1 : C2? */ |
ae22bc5d BC |
4709 | else if (int_fits_type_p (@3, from_type)) |
4710 | code = EQ_EXPR; | |
b5481987 BC |
4711 | } |
4712 | } | |
4713 | (if (code == MAX_EXPR) | |
21aaaf1e | 4714 | (convert (max @1 (convert @2))) |
b5481987 | 4715 | (if (code == MIN_EXPR) |
21aaaf1e | 4716 | (convert (min @1 (convert @2))) |
e2535011 | 4717 | (if (code == EQ_EXPR) |
ae22bc5d | 4718 | (convert (cond (eq @1 (convert @3)) |
21aaaf1e | 4719 | (convert:from_type @3) (convert:from_type @2))))))))) |
b5481987 | 4720 | |
714445ae BC |
4721 | /* (cond (cmp (convert? x) c1) (op x c2) c3) -> (op (minmax x c1) c2) if: |
4722 | ||
4723 | 1) OP is PLUS or MINUS. | |
4724 | 2) CMP is LT, LE, GT or GE. | |
4725 | 3) C3 == (C1 op C2), and computation doesn't have undefined behavior. | |
4726 | ||
4727 | This pattern also handles special cases like: | |
4728 | ||
4729 | A) Operand x is a unsigned to signed type conversion and c1 is | |
4730 | integer zero. In this case, | |
4731 | (signed type)x < 0 <=> x > MAX_VAL(signed type) | |
4732 | (signed type)x >= 0 <=> x <= MAX_VAL(signed type) | |
4733 | B) Const c1 may not equal to (C3 op' C2). In this case we also | |
4734 | check equality for (c1+1) and (c1-1) by adjusting comparison | |
4735 | code. | |
4736 | ||
4737 | TODO: Though signed type is handled by this pattern, it cannot be | |
4738 | simplified at the moment because C standard requires additional | |
4739 | type promotion. In order to match&simplify it here, the IR needs | |
4740 | to be cleaned up by other optimizers, i.e, VRP. */ | |
4741 | (for op (plus minus) | |
4742 | (for cmp (lt le gt ge) | |
4743 | (simplify | |
4744 | (cond (cmp (convert? @X) INTEGER_CST@1) (op @X INTEGER_CST@2) INTEGER_CST@3) | |
4745 | (with { tree from_type = TREE_TYPE (@X), to_type = TREE_TYPE (@1); } | |
4746 | (if (types_match (from_type, to_type) | |
4747 | /* Check if it is special case A). */ | |
4748 | || (TYPE_UNSIGNED (from_type) | |
4749 | && !TYPE_UNSIGNED (to_type) | |
4750 | && TYPE_PRECISION (from_type) == TYPE_PRECISION (to_type) | |
4751 | && integer_zerop (@1) | |
4752 | && (cmp == LT_EXPR || cmp == GE_EXPR))) | |
4753 | (with | |
4754 | { | |
4a669ac3 | 4755 | wi::overflow_type overflow = wi::OVF_NONE; |
714445ae | 4756 | enum tree_code code, cmp_code = cmp; |
8e6cdc90 RS |
4757 | wide_int real_c1; |
4758 | wide_int c1 = wi::to_wide (@1); | |
4759 | wide_int c2 = wi::to_wide (@2); | |
4760 | wide_int c3 = wi::to_wide (@3); | |
714445ae BC |
4761 | signop sgn = TYPE_SIGN (from_type); |
4762 | ||
4763 | /* Handle special case A), given x of unsigned type: | |
4764 | ((signed type)x < 0) <=> (x > MAX_VAL(signed type)) | |
4765 | ((signed type)x >= 0) <=> (x <= MAX_VAL(signed type)) */ | |
4766 | if (!types_match (from_type, to_type)) | |
4767 | { | |
4768 | if (cmp_code == LT_EXPR) | |
4769 | cmp_code = GT_EXPR; | |
4770 | if (cmp_code == GE_EXPR) | |
4771 | cmp_code = LE_EXPR; | |
4772 | c1 = wi::max_value (to_type); | |
4773 | } | |
4774 | /* To simplify this pattern, we require c3 = (c1 op c2). Here we | |
4775 | compute (c3 op' c2) and check if it equals to c1 with op' being | |
4776 | the inverted operator of op. Make sure overflow doesn't happen | |
4777 | if it is undefined. */ | |
4778 | if (op == PLUS_EXPR) | |
4779 | real_c1 = wi::sub (c3, c2, sgn, &overflow); | |
4780 | else | |
4781 | real_c1 = wi::add (c3, c2, sgn, &overflow); | |
4782 | ||
4783 | code = cmp_code; | |
4784 | if (!overflow || !TYPE_OVERFLOW_UNDEFINED (from_type)) | |
4785 | { | |
4786 | /* Check if c1 equals to real_c1. Boundary condition is handled | |
4787 | by adjusting comparison operation if necessary. */ | |
4788 | if (!wi::cmp (wi::sub (real_c1, 1, sgn, &overflow), c1, sgn) | |
4789 | && !overflow) | |
4790 | { | |
4791 | /* X <= Y - 1 equals to X < Y. */ | |
4792 | if (cmp_code == LE_EXPR) | |
4793 | code = LT_EXPR; | |
4794 | /* X > Y - 1 equals to X >= Y. */ | |
4795 | if (cmp_code == GT_EXPR) | |
4796 | code = GE_EXPR; | |
4797 | } | |
4798 | if (!wi::cmp (wi::add (real_c1, 1, sgn, &overflow), c1, sgn) | |
4799 | && !overflow) | |
4800 | { | |
4801 | /* X < Y + 1 equals to X <= Y. */ | |
4802 | if (cmp_code == LT_EXPR) | |
4803 | code = LE_EXPR; | |
4804 | /* X >= Y + 1 equals to X > Y. */ | |
4805 | if (cmp_code == GE_EXPR) | |
4806 | code = GT_EXPR; | |
4807 | } | |
4808 | if (code != cmp_code || !wi::cmp (real_c1, c1, sgn)) | |
4809 | { | |
4810 | if (cmp_code == LT_EXPR || cmp_code == LE_EXPR) | |
4811 | code = MIN_EXPR; | |
4812 | if (cmp_code == GT_EXPR || cmp_code == GE_EXPR) | |
4813 | code = MAX_EXPR; | |
4814 | } | |
4815 | } | |
4816 | } | |
4817 | (if (code == MAX_EXPR) | |
4818 | (op (max @X { wide_int_to_tree (from_type, real_c1); }) | |
4819 | { wide_int_to_tree (from_type, c2); }) | |
4820 | (if (code == MIN_EXPR) | |
4821 | (op (min @X { wide_int_to_tree (from_type, real_c1); }) | |
4822 | { wide_int_to_tree (from_type, c2); }))))))))) | |
4823 | ||
9a53101c RB |
4824 | #if GIMPLE |
4825 | /* A >= B ? A : B -> max (A, B) and friends. The code is still | |
4826 | in fold_cond_expr_with_comparison for GENERIC folding with | |
4827 | some extra constraints. */ | |
4828 | (for cmp (eq ne le lt unle unlt ge gt unge ungt uneq ltgt) | |
4829 | (simplify | |
4830 | (cond (cmp:c (nop_convert1?@c0 @0) (nop_convert2?@c1 @1)) | |
4831 | (convert3? @0) (convert4? @1)) | |
4832 | (if (!HONOR_SIGNED_ZEROS (type) | |
b36a1c96 RB |
4833 | && (/* Allow widening conversions of the compare operands as data. */ |
4834 | (INTEGRAL_TYPE_P (type) | |
4835 | && types_match (TREE_TYPE (@c0), TREE_TYPE (@0)) | |
4836 | && types_match (TREE_TYPE (@c1), TREE_TYPE (@1)) | |
9a53101c RB |
4837 | && TYPE_PRECISION (TREE_TYPE (@0)) <= TYPE_PRECISION (type) |
4838 | && TYPE_PRECISION (TREE_TYPE (@1)) <= TYPE_PRECISION (type)) | |
b36a1c96 RB |
4839 | /* Or sign conversions for the comparison. */ |
4840 | || (types_match (type, TREE_TYPE (@0)) | |
4841 | && types_match (type, TREE_TYPE (@1))))) | |
9a53101c RB |
4842 | (switch |
4843 | (if (cmp == EQ_EXPR) | |
4844 | (if (VECTOR_TYPE_P (type)) | |
4845 | (view_convert @c1) | |
4846 | (convert @c1))) | |
4847 | (if (cmp == NE_EXPR) | |
4848 | (if (VECTOR_TYPE_P (type)) | |
4849 | (view_convert @c0) | |
4850 | (convert @c0))) | |
4851 | (if (cmp == LE_EXPR || cmp == UNLE_EXPR || cmp == LT_EXPR || cmp == UNLT_EXPR) | |
4852 | (if (!HONOR_NANS (type)) | |
4853 | (if (VECTOR_TYPE_P (type)) | |
4854 | (view_convert (min @c0 @c1)) | |
4855 | (convert (min @c0 @c1))))) | |
4856 | (if (cmp == GE_EXPR || cmp == UNGE_EXPR || cmp == GT_EXPR || cmp == UNGT_EXPR) | |
4857 | (if (!HONOR_NANS (type)) | |
4858 | (if (VECTOR_TYPE_P (type)) | |
4859 | (view_convert (max @c0 @c1)) | |
4860 | (convert (max @c0 @c1))))) | |
4861 | (if (cmp == UNEQ_EXPR) | |
4862 | (if (!HONOR_NANS (type)) | |
4863 | (if (VECTOR_TYPE_P (type)) | |
4864 | (view_convert @c1) | |
4865 | (convert @c1)))) | |
4866 | (if (cmp == LTGT_EXPR) | |
4867 | (if (!HONOR_NANS (type)) | |
4868 | (if (VECTOR_TYPE_P (type)) | |
4869 | (view_convert @c0) | |
4870 | (convert @c0)))))))) | |
4871 | #endif | |
4872 | ||
de3e5aae RS |
4873 | /* X != C1 ? -X : C2 simplifies to -X when -C1 == C2. */ |
4874 | (simplify | |
4875 | (cond (ne @0 INTEGER_CST@1) (negate@3 @0) INTEGER_CST@2) | |
4876 | (if (!TYPE_SATURATING (type) | |
4877 | && (TYPE_OVERFLOW_WRAPS (type) | |
4878 | || !wi::only_sign_bit_p (wi::to_wide (@1))) | |
4879 | && wi::eq_p (wi::neg (wi::to_wide (@1)), wi::to_wide (@2))) | |
4880 | @3)) | |
4881 | ||
4882 | /* X != C1 ? ~X : C2 simplifies to ~X when ~C1 == C2. */ | |
4883 | (simplify | |
4884 | (cond (ne @0 INTEGER_CST@1) (bit_not@3 @0) INTEGER_CST@2) | |
4885 | (if (wi::eq_p (wi::bit_not (wi::to_wide (@1)), wi::to_wide (@2))) | |
4886 | @3)) | |
4887 | ||
4888 | /* (X + 1) > Y ? -X : 1 simplifies to X >= Y ? -X : 1 when | |
4889 | X is unsigned, as when X + 1 overflows, X is -1, so -X == 1. */ | |
4890 | (simplify | |
4891 | (cond (gt (plus @0 integer_onep) @1) (negate @0) integer_onep@2) | |
4892 | (if (TYPE_UNSIGNED (type)) | |
4893 | (cond (ge @0 @1) (negate @0) @2))) | |
4894 | ||
96994de0 RB |
4895 | (for cnd (cond vec_cond) |
4896 | /* A ? B : (A ? X : C) -> A ? B : C. */ | |
4897 | (simplify | |
4898 | (cnd @0 (cnd @0 @1 @2) @3) | |
4899 | (cnd @0 @1 @3)) | |
4900 | (simplify | |
4901 | (cnd @0 @1 (cnd @0 @2 @3)) | |
4902 | (cnd @0 @1 @3)) | |
24a179f8 RB |
4903 | /* A ? B : (!A ? C : X) -> A ? B : C. */ |
4904 | /* ??? This matches embedded conditions open-coded because genmatch | |
4905 | would generate matching code for conditions in separate stmts only. | |
4906 | The following is still important to merge then and else arm cases | |
4907 | from if-conversion. */ | |
4908 | (simplify | |
4909 | (cnd @0 @1 (cnd @2 @3 @4)) | |
2c58d42c | 4910 | (if (inverse_conditions_p (@0, @2)) |
24a179f8 RB |
4911 | (cnd @0 @1 @3))) |
4912 | (simplify | |
4913 | (cnd @0 (cnd @1 @2 @3) @4) | |
2c58d42c | 4914 | (if (inverse_conditions_p (@0, @1)) |
24a179f8 | 4915 | (cnd @0 @3 @4))) |
96994de0 RB |
4916 | |
4917 | /* A ? B : B -> B. */ | |
4918 | (simplify | |
4919 | (cnd @0 @1 @1) | |
09240451 | 4920 | @1) |
96994de0 | 4921 | |
09240451 MG |
4922 | /* !A ? B : C -> A ? C : B. */ |
4923 | (simplify | |
4924 | (cnd (logical_inverted_value truth_valued_p@0) @1 @2) | |
4925 | (cnd @0 @2 @1))) | |
f84e7fd6 | 4926 | |
7d697919 AP |
4927 | /* abs/negative simplifications moved from fold_cond_expr_with_comparison, |
4928 | Need to handle (A - B) case as fold_cond_expr_with_comparison does. | |
4929 | Need to handle UN* comparisons. | |
4930 | ||
4931 | None of these transformations work for modes with signed | |
4932 | zeros. If A is +/-0, the first two transformations will | |
4933 | change the sign of the result (from +0 to -0, or vice | |
4934 | versa). The last four will fix the sign of the result, | |
4935 | even though the original expressions could be positive or | |
4936 | negative, depending on the sign of A. | |
4937 | ||
4938 | Note that all these transformations are correct if A is | |
4939 | NaN, since the two alternatives (A and -A) are also NaNs. */ | |
4940 | ||
4941 | (for cnd (cond vec_cond) | |
4942 | /* A == 0 ? A : -A same as -A */ | |
4943 | (for cmp (eq uneq) | |
4944 | (simplify | |
4945 | (cnd (cmp @0 zerop) @0 (negate@1 @0)) | |
4946 | (if (!HONOR_SIGNED_ZEROS (type)) | |
4947 | @1)) | |
4948 | (simplify | |
df8fe4ad | 4949 | (cnd (cmp @0 zerop) zerop (negate@1 @0)) |
7d697919 AP |
4950 | (if (!HONOR_SIGNED_ZEROS (type)) |
4951 | @1)) | |
4952 | ) | |
4953 | /* A != 0 ? A : -A same as A */ | |
4954 | (for cmp (ne ltgt) | |
4955 | (simplify | |
4956 | (cnd (cmp @0 zerop) @0 (negate @0)) | |
4957 | (if (!HONOR_SIGNED_ZEROS (type)) | |
4958 | @0)) | |
4959 | (simplify | |
4960 | (cnd (cmp @0 zerop) @0 integer_zerop) | |
4961 | (if (!HONOR_SIGNED_ZEROS (type)) | |
4962 | @0)) | |
4963 | ) | |
4964 | /* A >=/> 0 ? A : -A same as abs (A) */ | |
4965 | (for cmp (ge gt) | |
4966 | (simplify | |
4967 | (cnd (cmp @0 zerop) @0 (negate @0)) | |
4968 | (if (!HONOR_SIGNED_ZEROS (type) | |
4969 | && !TYPE_UNSIGNED (type)) | |
4970 | (abs @0)))) | |
4971 | /* A <=/< 0 ? A : -A same as -abs (A) */ | |
4972 | (for cmp (le lt) | |
4973 | (simplify | |
4974 | (cnd (cmp @0 zerop) @0 (negate @0)) | |
4975 | (if (!HONOR_SIGNED_ZEROS (type) | |
4976 | && !TYPE_UNSIGNED (type)) | |
4977 | (if (ANY_INTEGRAL_TYPE_P (type) | |
4978 | && !TYPE_OVERFLOW_WRAPS (type)) | |
4979 | (with { | |
4980 | tree utype = unsigned_type_for (type); | |
4981 | } | |
4982 | (convert (negate (absu:utype @0)))) | |
4983 | (negate (abs @0))))) | |
4984 | ) | |
4985 | ) | |
4986 | ||
9e12b8b1 JJ |
4987 | /* -(type)!A -> (type)A - 1. */ |
4988 | (simplify | |
4989 | (negate (convert?:s (logical_inverted_value:s @0))) | |
4990 | (if (INTEGRAL_TYPE_P (type) | |
4991 | && TREE_CODE (type) != BOOLEAN_TYPE | |
4992 | && TYPE_PRECISION (type) > 1 | |
4993 | && TREE_CODE (@0) == SSA_NAME | |
4994 | && ssa_name_has_boolean_range (@0)) | |
4995 | (plus (convert:type @0) { build_all_ones_cst (type); }))) | |
4996 | ||
a3ca1bc5 RB |
4997 | /* A + (B vcmp C ? 1 : 0) -> A - (B vcmp C ? -1 : 0), since vector comparisons |
4998 | return all -1 or all 0 results. */ | |
f43d102e RS |
4999 | /* ??? We could instead convert all instances of the vec_cond to negate, |
5000 | but that isn't necessarily a win on its own. */ | |
5001 | (simplify | |
a3ca1bc5 | 5002 | (plus:c @3 (view_convert? (vec_cond:s @0 integer_each_onep@1 integer_zerop@2))) |
f43d102e | 5003 | (if (VECTOR_TYPE_P (type) |
928686b1 RS |
5004 | && known_eq (TYPE_VECTOR_SUBPARTS (type), |
5005 | TYPE_VECTOR_SUBPARTS (TREE_TYPE (@1))) | |
f43d102e | 5006 | && (TYPE_MODE (TREE_TYPE (type)) |
4d8989d5 | 5007 | == TYPE_MODE (TREE_TYPE (TREE_TYPE (@1))))) |
a3ca1bc5 | 5008 | (minus @3 (view_convert (vec_cond @0 (negate @1) @2))))) |
f43d102e | 5009 | |
a3ca1bc5 | 5010 | /* ... likewise A - (B vcmp C ? 1 : 0) -> A + (B vcmp C ? -1 : 0). */ |
f43d102e | 5011 | (simplify |
a3ca1bc5 | 5012 | (minus @3 (view_convert? (vec_cond:s @0 integer_each_onep@1 integer_zerop@2))) |
f43d102e | 5013 | (if (VECTOR_TYPE_P (type) |
928686b1 RS |
5014 | && known_eq (TYPE_VECTOR_SUBPARTS (type), |
5015 | TYPE_VECTOR_SUBPARTS (TREE_TYPE (@1))) | |
f43d102e | 5016 | && (TYPE_MODE (TREE_TYPE (type)) |
4d8989d5 | 5017 | == TYPE_MODE (TREE_TYPE (TREE_TYPE (@1))))) |
a3ca1bc5 | 5018 | (plus @3 (view_convert (vec_cond @0 (negate @1) @2))))) |
f84e7fd6 | 5019 | |
2ee05f1e | 5020 | |
f84e7fd6 RB |
5021 | /* Simplifications of comparisons. */ |
5022 | ||
24f1db9c RB |
5023 | /* See if we can reduce the magnitude of a constant involved in a |
5024 | comparison by changing the comparison code. This is a canonicalization | |
5025 | formerly done by maybe_canonicalize_comparison_1. */ | |
5026 | (for cmp (le gt) | |
5027 | acmp (lt ge) | |
5028 | (simplify | |
f06e47d7 JJ |
5029 | (cmp @0 uniform_integer_cst_p@1) |
5030 | (with { tree cst = uniform_integer_cst_p (@1); } | |
5031 | (if (tree_int_cst_sgn (cst) == -1) | |
5032 | (acmp @0 { build_uniform_cst (TREE_TYPE (@1), | |
5033 | wide_int_to_tree (TREE_TYPE (cst), | |
5034 | wi::to_wide (cst) | |
5035 | + 1)); }))))) | |
24f1db9c RB |
5036 | (for cmp (ge lt) |
5037 | acmp (gt le) | |
5038 | (simplify | |
f06e47d7 JJ |
5039 | (cmp @0 uniform_integer_cst_p@1) |
5040 | (with { tree cst = uniform_integer_cst_p (@1); } | |
5041 | (if (tree_int_cst_sgn (cst) == 1) | |
5042 | (acmp @0 { build_uniform_cst (TREE_TYPE (@1), | |
5043 | wide_int_to_tree (TREE_TYPE (cst), | |
5044 | wi::to_wide (cst) - 1)); }))))) | |
24f1db9c | 5045 | |
f84e7fd6 RB |
5046 | /* We can simplify a logical negation of a comparison to the |
5047 | inverted comparison. As we cannot compute an expression | |
5048 | operator using invert_tree_comparison we have to simulate | |
5049 | that with expression code iteration. */ | |
5050 | (for cmp (tcc_comparison) | |
5051 | icmp (inverted_tcc_comparison) | |
5052 | ncmp (inverted_tcc_comparison_with_nans) | |
5053 | /* Ideally we'd like to combine the following two patterns | |
5054 | and handle some more cases by using | |
5055 | (logical_inverted_value (cmp @0 @1)) | |
5056 | here but for that genmatch would need to "inline" that. | |
5057 | For now implement what forward_propagate_comparison did. */ | |
5058 | (simplify | |
5059 | (bit_not (cmp @0 @1)) | |
5060 | (if (VECTOR_TYPE_P (type) | |
5061 | || (INTEGRAL_TYPE_P (type) && TYPE_PRECISION (type) == 1)) | |
5062 | /* Comparison inversion may be impossible for trapping math, | |
5063 | invert_tree_comparison will tell us. But we can't use | |
5064 | a computed operator in the replacement tree thus we have | |
5065 | to play the trick below. */ | |
5066 | (with { enum tree_code ic = invert_tree_comparison | |
1b457aa4 | 5067 | (cmp, HONOR_NANS (@0)); } |
f84e7fd6 | 5068 | (if (ic == icmp) |
8fdc6c67 RB |
5069 | (icmp @0 @1) |
5070 | (if (ic == ncmp) | |
5071 | (ncmp @0 @1)))))) | |
f84e7fd6 | 5072 | (simplify |
09240451 MG |
5073 | (bit_xor (cmp @0 @1) integer_truep) |
5074 | (with { enum tree_code ic = invert_tree_comparison | |
1b457aa4 | 5075 | (cmp, HONOR_NANS (@0)); } |
09240451 | 5076 | (if (ic == icmp) |
8fdc6c67 RB |
5077 | (icmp @0 @1) |
5078 | (if (ic == ncmp) | |
7f04b0d7 RB |
5079 | (ncmp @0 @1))))) |
5080 | /* The following bits are handled by fold_binary_op_with_conditional_arg. */ | |
5081 | (simplify | |
5082 | (ne (cmp@2 @0 @1) integer_zerop) | |
5083 | (if (types_match (type, TREE_TYPE (@2))) | |
5084 | (cmp @0 @1))) | |
5085 | (simplify | |
5086 | (eq (cmp@2 @0 @1) integer_truep) | |
5087 | (if (types_match (type, TREE_TYPE (@2))) | |
5088 | (cmp @0 @1))) | |
5089 | (simplify | |
5090 | (ne (cmp@2 @0 @1) integer_truep) | |
5091 | (if (types_match (type, TREE_TYPE (@2))) | |
5092 | (with { enum tree_code ic = invert_tree_comparison | |
5093 | (cmp, HONOR_NANS (@0)); } | |
5094 | (if (ic == icmp) | |
5095 | (icmp @0 @1) | |
5096 | (if (ic == ncmp) | |
5097 | (ncmp @0 @1)))))) | |
5098 | (simplify | |
5099 | (eq (cmp@2 @0 @1) integer_zerop) | |
5100 | (if (types_match (type, TREE_TYPE (@2))) | |
5101 | (with { enum tree_code ic = invert_tree_comparison | |
5102 | (cmp, HONOR_NANS (@0)); } | |
5103 | (if (ic == icmp) | |
5104 | (icmp @0 @1) | |
5105 | (if (ic == ncmp) | |
5106 | (ncmp @0 @1))))))) | |
e18c1d66 | 5107 | |
2ee05f1e RB |
5108 | /* Transform comparisons of the form X - Y CMP 0 to X CMP Y. |
5109 | ??? The transformation is valid for the other operators if overflow | |
5110 | is undefined for the type, but performing it here badly interacts | |
5111 | with the transformation in fold_cond_expr_with_comparison which | |
5112 | attempts to synthetize ABS_EXPR. */ | |
5113 | (for cmp (eq ne) | |
1af4ebf5 MG |
5114 | (for sub (minus pointer_diff) |
5115 | (simplify | |
5116 | (cmp (sub@2 @0 @1) integer_zerop) | |
5117 | (if (single_use (@2)) | |
5118 | (cmp @0 @1))))) | |
2ee05f1e | 5119 | |
5c046034 JJ |
5120 | /* Simplify (x < 0) ^ (y < 0) to (x ^ y) < 0 and |
5121 | (x >= 0) ^ (y >= 0) to (x ^ y) < 0. */ | |
5122 | (for cmp (lt ge) | |
5123 | (simplify | |
5124 | (bit_xor (cmp:s @0 integer_zerop) (cmp:s @1 integer_zerop)) | |
5125 | (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)) | |
5126 | && !TYPE_UNSIGNED (TREE_TYPE (@0)) | |
5127 | && types_match (TREE_TYPE (@0), TREE_TYPE (@1))) | |
5128 | (lt (bit_xor @0 @1) { build_zero_cst (TREE_TYPE (@0)); })))) | |
5129 | /* Simplify (x < 0) ^ (y >= 0) to (x ^ y) >= 0 and | |
5130 | (x >= 0) ^ (y < 0) to (x ^ y) >= 0. */ | |
5131 | (simplify | |
5132 | (bit_xor:c (lt:s @0 integer_zerop) (ge:s @1 integer_zerop)) | |
5133 | (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)) | |
5134 | && !TYPE_UNSIGNED (TREE_TYPE (@0)) | |
5135 | && types_match (TREE_TYPE (@0), TREE_TYPE (@1))) | |
5136 | (ge (bit_xor @0 @1) { build_zero_cst (TREE_TYPE (@0)); }))) | |
5137 | ||
2ee05f1e RB |
5138 | /* Transform comparisons of the form X * C1 CMP 0 to X CMP 0 in the |
5139 | signed arithmetic case. That form is created by the compiler | |
5140 | often enough for folding it to be of value. One example is in | |
5141 | computing loop trip counts after Operator Strength Reduction. */ | |
07cdc2b8 RB |
5142 | (for cmp (simple_comparison) |
5143 | scmp (swapped_simple_comparison) | |
2ee05f1e | 5144 | (simplify |
bc6e9db4 | 5145 | (cmp (mult@3 @0 INTEGER_CST@1) integer_zerop@2) |
2ee05f1e RB |
5146 | /* Handle unfolded multiplication by zero. */ |
5147 | (if (integer_zerop (@1)) | |
8fdc6c67 RB |
5148 | (cmp @1 @2) |
5149 | (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0)) | |
bc6e9db4 RB |
5150 | && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)) |
5151 | && single_use (@3)) | |
8fdc6c67 RB |
5152 | /* If @1 is negative we swap the sense of the comparison. */ |
5153 | (if (tree_int_cst_sgn (@1) < 0) | |
5154 | (scmp @0 @2) | |
5155 | (cmp @0 @2)))))) | |
03cc70b5 | 5156 | |
ca2b8c08 | 5157 | /* For integral types with undefined overflow fold |
28752261 MG |
5158 | x * C1 == C2 into x == C2 / C1 or false. |
5159 | If overflow wraps and C1 is odd, simplify to x == C2 / C1 in the ring | |
5160 | Z / 2^n Z. */ | |
ca2b8c08 MG |
5161 | (for cmp (eq ne) |
5162 | (simplify | |
5163 | (cmp (mult @0 INTEGER_CST@1) INTEGER_CST@2) | |
5164 | (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)) | |
5165 | && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)) | |
5166 | && wi::to_wide (@1) != 0) | |
5167 | (with { widest_int quot; } | |
5168 | (if (wi::multiple_of_p (wi::to_widest (@2), wi::to_widest (@1), | |
5169 | TYPE_SIGN (TREE_TYPE (@0)), ")) | |
5170 | (cmp @0 { wide_int_to_tree (TREE_TYPE (@0), quot); }) | |
28752261 MG |
5171 | { constant_boolean_node (cmp == NE_EXPR, type); })) |
5172 | (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)) | |
5173 | && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)) | |
5174 | && (wi::bit_and (wi::to_wide (@1), 1) == 1)) | |
5175 | (cmp @0 | |
5176 | { | |
5177 | tree itype = TREE_TYPE (@0); | |
5178 | int p = TYPE_PRECISION (itype); | |
5179 | wide_int m = wi::one (p + 1) << p; | |
5180 | wide_int a = wide_int::from (wi::to_wide (@1), p + 1, UNSIGNED); | |
5181 | wide_int i = wide_int::from (wi::mod_inv (a, m), | |
5182 | p, TYPE_SIGN (itype)); | |
5183 | wide_int_to_tree (itype, wi::mul (i, wi::to_wide (@2))); | |
5184 | }))))) | |
ca2b8c08 | 5185 | |
2ee05f1e RB |
5186 | /* Simplify comparison of something with itself. For IEEE |
5187 | floating-point, we can only do some of these simplifications. */ | |
287f8f17 | 5188 | (for cmp (eq ge le) |
2ee05f1e RB |
5189 | (simplify |
5190 | (cmp @0 @0) | |
287f8f17 | 5191 | (if (! FLOAT_TYPE_P (TREE_TYPE (@0)) |
7690bee9 | 5192 | || ! tree_expr_maybe_nan_p (@0)) |
287f8f17 | 5193 | { constant_boolean_node (true, type); } |
d9ca2ca3 RB |
5194 | (if (cmp != EQ_EXPR |
5195 | /* With -ftrapping-math conversion to EQ loses an exception. */ | |
5196 | && (! FLOAT_TYPE_P (TREE_TYPE (@0)) | |
5197 | || ! flag_trapping_math)) | |
287f8f17 | 5198 | (eq @0 @0))))) |
2ee05f1e RB |
5199 | (for cmp (ne gt lt) |
5200 | (simplify | |
5201 | (cmp @0 @0) | |
5202 | (if (cmp != NE_EXPR | |
5203 | || ! FLOAT_TYPE_P (TREE_TYPE (@0)) | |
7690bee9 | 5204 | || ! tree_expr_maybe_nan_p (@0)) |
2ee05f1e | 5205 | { constant_boolean_node (false, type); }))) |
b5d3d787 RB |
5206 | (for cmp (unle unge uneq) |
5207 | (simplify | |
5208 | (cmp @0 @0) | |
5209 | { constant_boolean_node (true, type); })) | |
dd53d197 MG |
5210 | (for cmp (unlt ungt) |
5211 | (simplify | |
5212 | (cmp @0 @0) | |
5213 | (unordered @0 @0))) | |
b5d3d787 RB |
5214 | (simplify |
5215 | (ltgt @0 @0) | |
7690bee9 | 5216 | (if (!flag_trapping_math || !tree_expr_maybe_nan_p (@0)) |
b5d3d787 | 5217 | { constant_boolean_node (false, type); })) |
2ee05f1e | 5218 | |
ad64e807 JJ |
5219 | /* x == ~x -> false */ |
5220 | /* x != ~x -> true */ | |
5221 | (for cmp (eq ne) | |
5222 | (simplify | |
5223 | (cmp:c @0 (bit_not @0)) | |
5224 | { constant_boolean_node (cmp == NE_EXPR, type); })) | |
5225 | ||
2ee05f1e | 5226 | /* Fold ~X op ~Y as Y op X. */ |
07cdc2b8 | 5227 | (for cmp (simple_comparison) |
2ee05f1e | 5228 | (simplify |
7fe996ba RB |
5229 | (cmp (bit_not@2 @0) (bit_not@3 @1)) |
5230 | (if (single_use (@2) && single_use (@3)) | |
5231 | (cmp @1 @0)))) | |
2ee05f1e RB |
5232 | |
5233 | /* Fold ~X op C as X op' ~C, where op' is the swapped comparison. */ | |
07cdc2b8 RB |
5234 | (for cmp (simple_comparison) |
5235 | scmp (swapped_simple_comparison) | |
2ee05f1e | 5236 | (simplify |
7fe996ba RB |
5237 | (cmp (bit_not@2 @0) CONSTANT_CLASS_P@1) |
5238 | (if (single_use (@2) | |
5239 | && (TREE_CODE (@1) == INTEGER_CST || TREE_CODE (@1) == VECTOR_CST)) | |
2ee05f1e RB |
5240 | (scmp @0 (bit_not @1))))) |
5241 | ||
07cdc2b8 | 5242 | (for cmp (simple_comparison) |
07cdc2b8 RB |
5243 | (simplify |
5244 | (cmp @0 REAL_CST@1) | |
5245 | /* IEEE doesn't distinguish +0 and -0 in comparisons. */ | |
64d3a1f0 RB |
5246 | (switch |
5247 | /* a CMP (-0) -> a CMP 0 */ | |
5248 | (if (REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (@1))) | |
5249 | (cmp @0 { build_real (TREE_TYPE (@1), dconst0); })) | |
e9e46864 RS |
5250 | /* (-0) CMP b -> 0 CMP b. */ |
5251 | (if (TREE_CODE (@0) == REAL_CST | |
5252 | && REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (@0))) | |
5253 | (cmp { build_real (TREE_TYPE (@0), dconst0); } @1)) | |
64d3a1f0 RB |
5254 | /* x != NaN is always true, other ops are always false. */ |
5255 | (if (REAL_VALUE_ISNAN (TREE_REAL_CST (@1)) | |
109148dd | 5256 | && (cmp == EQ_EXPR || cmp == NE_EXPR || !flag_trapping_math) |
e9e46864 RS |
5257 | && !tree_expr_signaling_nan_p (@1) |
5258 | && !tree_expr_maybe_signaling_nan_p (@0)) | |
5259 | { constant_boolean_node (cmp == NE_EXPR, type); }) | |
5260 | /* NaN != y is always true, other ops are always false. */ | |
5261 | (if (TREE_CODE (@0) == REAL_CST | |
5262 | && REAL_VALUE_ISNAN (TREE_REAL_CST (@0)) | |
109148dd | 5263 | && (cmp == EQ_EXPR || cmp == NE_EXPR || !flag_trapping_math) |
e9e46864 RS |
5264 | && !tree_expr_signaling_nan_p (@0) |
5265 | && !tree_expr_signaling_nan_p (@1)) | |
64d3a1f0 RB |
5266 | { constant_boolean_node (cmp == NE_EXPR, type); }) |
5267 | /* Fold comparisons against infinity. */ | |
5268 | (if (REAL_VALUE_ISINF (TREE_REAL_CST (@1)) | |
5269 | && MODE_HAS_INFINITIES (TYPE_MODE (TREE_TYPE (@1)))) | |
5270 | (with | |
5271 | { | |
5272 | REAL_VALUE_TYPE max; | |
5273 | enum tree_code code = cmp; | |
5274 | bool neg = REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1)); | |
5275 | if (neg) | |
5276 | code = swap_tree_comparison (code); | |
5277 | } | |
5278 | (switch | |
e96a5786 | 5279 | /* x > +Inf is always false, if we ignore NaNs or exceptions. */ |
64d3a1f0 | 5280 | (if (code == GT_EXPR |
e96a5786 | 5281 | && !(HONOR_NANS (@0) && flag_trapping_math)) |
64d3a1f0 RB |
5282 | { constant_boolean_node (false, type); }) |
5283 | (if (code == LE_EXPR) | |
e96a5786 | 5284 | /* x <= +Inf is always true, if we don't care about NaNs. */ |
64d3a1f0 RB |
5285 | (if (! HONOR_NANS (@0)) |
5286 | { constant_boolean_node (true, type); } | |
e96a5786 JM |
5287 | /* x <= +Inf is the same as x == x, i.e. !isnan(x), but this loses |
5288 | an "invalid" exception. */ | |
5289 | (if (!flag_trapping_math) | |
5290 | (eq @0 @0)))) | |
5291 | /* x == +Inf and x >= +Inf are always equal to x > DBL_MAX, but | |
5292 | for == this introduces an exception for x a NaN. */ | |
5293 | (if ((code == EQ_EXPR && !(HONOR_NANS (@0) && flag_trapping_math)) | |
5294 | || code == GE_EXPR) | |
64d3a1f0 RB |
5295 | (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); } |
5296 | (if (neg) | |
5297 | (lt @0 { build_real (TREE_TYPE (@0), max); }) | |
5298 | (gt @0 { build_real (TREE_TYPE (@0), max); })))) | |
5299 | /* x < +Inf is always equal to x <= DBL_MAX. */ | |
5300 | (if (code == LT_EXPR) | |
5301 | (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); } | |
5302 | (if (neg) | |
5303 | (ge @0 { build_real (TREE_TYPE (@0), max); }) | |
5304 | (le @0 { build_real (TREE_TYPE (@0), max); })))) | |
e96a5786 JM |
5305 | /* x != +Inf is always equal to !(x > DBL_MAX), but this introduces |
5306 | an exception for x a NaN so use an unordered comparison. */ | |
64d3a1f0 RB |
5307 | (if (code == NE_EXPR) |
5308 | (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); } | |
5309 | (if (! HONOR_NANS (@0)) | |
5310 | (if (neg) | |
5311 | (ge @0 { build_real (TREE_TYPE (@0), max); }) | |
5312 | (le @0 { build_real (TREE_TYPE (@0), max); })) | |
5313 | (if (neg) | |
e96a5786 JM |
5314 | (unge @0 { build_real (TREE_TYPE (@0), max); }) |
5315 | (unle @0 { build_real (TREE_TYPE (@0), max); })))))))))) | |
07cdc2b8 RB |
5316 | |
5317 | /* If this is a comparison of a real constant with a PLUS_EXPR | |
5318 | or a MINUS_EXPR of a real constant, we can convert it into a | |
5319 | comparison with a revised real constant as long as no overflow | |
5320 | occurs when unsafe_math_optimizations are enabled. */ | |
5321 | (if (flag_unsafe_math_optimizations) | |
5322 | (for op (plus minus) | |
5323 | (simplify | |
5324 | (cmp (op @0 REAL_CST@1) REAL_CST@2) | |
5325 | (with | |
5326 | { | |
5327 | tree tem = const_binop (op == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR, | |
5328 | TREE_TYPE (@1), @2, @1); | |
5329 | } | |
f980c9a2 | 5330 | (if (tem && !TREE_OVERFLOW (tem)) |
07cdc2b8 RB |
5331 | (cmp @0 { tem; })))))) |
5332 | ||
5333 | /* Likewise, we can simplify a comparison of a real constant with | |
5334 | a MINUS_EXPR whose first operand is also a real constant, i.e. | |
5335 | (c1 - x) < c2 becomes x > c1-c2. Reordering is allowed on | |
5336 | floating-point types only if -fassociative-math is set. */ | |
5337 | (if (flag_associative_math) | |
5338 | (simplify | |
0409237b | 5339 | (cmp (minus REAL_CST@0 @1) REAL_CST@2) |
07cdc2b8 | 5340 | (with { tree tem = const_binop (MINUS_EXPR, TREE_TYPE (@1), @0, @2); } |
f980c9a2 | 5341 | (if (tem && !TREE_OVERFLOW (tem)) |
07cdc2b8 RB |
5342 | (cmp { tem; } @1))))) |
5343 | ||
5344 | /* Fold comparisons against built-in math functions. */ | |
0043b528 | 5345 | (if (flag_unsafe_math_optimizations && ! flag_errno_math) |
07cdc2b8 RB |
5346 | (for sq (SQRT) |
5347 | (simplify | |
5348 | (cmp (sq @0) REAL_CST@1) | |
64d3a1f0 RB |
5349 | (switch |
5350 | (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1))) | |
5351 | (switch | |
5352 | /* sqrt(x) < y is always false, if y is negative. */ | |
5353 | (if (cmp == EQ_EXPR || cmp == LT_EXPR || cmp == LE_EXPR) | |
8fdc6c67 | 5354 | { constant_boolean_node (false, type); }) |
64d3a1f0 RB |
5355 | /* sqrt(x) > y is always true, if y is negative and we |
5356 | don't care about NaNs, i.e. negative values of x. */ | |
5357 | (if (cmp == NE_EXPR || !HONOR_NANS (@0)) | |
5358 | { constant_boolean_node (true, type); }) | |
5359 | /* sqrt(x) > y is the same as x >= 0, if y is negative. */ | |
5360 | (ge @0 { build_real (TREE_TYPE (@0), dconst0); }))) | |
c53233c6 RS |
5361 | (if (real_equal (TREE_REAL_CST_PTR (@1), &dconst0)) |
5362 | (switch | |
5363 | /* sqrt(x) < 0 is always false. */ | |
5364 | (if (cmp == LT_EXPR) | |
5365 | { constant_boolean_node (false, type); }) | |
5366 | /* sqrt(x) >= 0 is always true if we don't care about NaNs. */ | |
5367 | (if (cmp == GE_EXPR && !HONOR_NANS (@0)) | |
5368 | { constant_boolean_node (true, type); }) | |
5369 | /* sqrt(x) <= 0 -> x == 0. */ | |
5370 | (if (cmp == LE_EXPR) | |
5371 | (eq @0 @1)) | |
5372 | /* Otherwise sqrt(x) cmp 0 -> x cmp 0. Here cmp can be >=, >, | |
5373 | == or !=. In the last case: | |
5374 | ||
5375 | (sqrt(x) != 0) == (NaN != 0) == true == (x != 0) | |
5376 | ||
5377 | if x is negative or NaN. Due to -funsafe-math-optimizations, | |
5378 | the results for other x follow from natural arithmetic. */ | |
5379 | (cmp @0 @1))) | |
0043b528 JJ |
5380 | (if ((cmp == LT_EXPR |
5381 | || cmp == LE_EXPR | |
5382 | || cmp == GT_EXPR | |
5383 | || cmp == GE_EXPR) | |
5384 | && !REAL_VALUE_ISNAN (TREE_REAL_CST (@1)) | |
5385 | /* Give up for -frounding-math. */ | |
5386 | && !HONOR_SIGN_DEPENDENT_ROUNDING (TREE_TYPE (@0))) | |
64d3a1f0 RB |
5387 | (with |
5388 | { | |
0043b528 JJ |
5389 | REAL_VALUE_TYPE c2; |
5390 | enum tree_code ncmp = cmp; | |
5391 | const real_format *fmt | |
5392 | = REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (@0))); | |
5c88ea94 RS |
5393 | real_arithmetic (&c2, MULT_EXPR, |
5394 | &TREE_REAL_CST (@1), &TREE_REAL_CST (@1)); | |
0043b528 JJ |
5395 | real_convert (&c2, fmt, &c2); |
5396 | /* See PR91734: if c2 is inexact and sqrt(c2) < c (or sqrt(c2) >= c), | |
5397 | then change LT_EXPR into LE_EXPR or GE_EXPR into GT_EXPR. */ | |
5398 | if (!REAL_VALUE_ISINF (c2)) | |
5399 | { | |
5400 | tree c3 = fold_const_call (CFN_SQRT, TREE_TYPE (@0), | |
5401 | build_real (TREE_TYPE (@0), c2)); | |
5402 | if (c3 == NULL_TREE || TREE_CODE (c3) != REAL_CST) | |
5403 | ncmp = ERROR_MARK; | |
5404 | else if ((cmp == LT_EXPR || cmp == GE_EXPR) | |
5405 | && real_less (&TREE_REAL_CST (c3), &TREE_REAL_CST (@1))) | |
5406 | ncmp = cmp == LT_EXPR ? LE_EXPR : GT_EXPR; | |
5407 | else if ((cmp == LE_EXPR || cmp == GT_EXPR) | |
5408 | && real_less (&TREE_REAL_CST (@1), &TREE_REAL_CST (c3))) | |
5409 | ncmp = cmp == LE_EXPR ? LT_EXPR : GE_EXPR; | |
5410 | else | |
5411 | { | |
5412 | /* With rounding to even, sqrt of up to 3 different values | |
5413 | gives the same normal result, so in some cases c2 needs | |
5414 | to be adjusted. */ | |
5415 | REAL_VALUE_TYPE c2alt, tow; | |
5416 | if (cmp == LT_EXPR || cmp == GE_EXPR) | |
5417 | tow = dconst0; | |
5418 | else | |
bb9d4344 | 5419 | tow = dconstinf; |
0043b528 JJ |
5420 | real_nextafter (&c2alt, fmt, &c2, &tow); |
5421 | real_convert (&c2alt, fmt, &c2alt); | |
5422 | if (REAL_VALUE_ISINF (c2alt)) | |
5423 | ncmp = ERROR_MARK; | |
5424 | else | |
5425 | { | |
5426 | c3 = fold_const_call (CFN_SQRT, TREE_TYPE (@0), | |
5427 | build_real (TREE_TYPE (@0), c2alt)); | |
5428 | if (c3 == NULL_TREE || TREE_CODE (c3) != REAL_CST) | |
5429 | ncmp = ERROR_MARK; | |
5430 | else if (real_equal (&TREE_REAL_CST (c3), | |
5431 | &TREE_REAL_CST (@1))) | |
5432 | c2 = c2alt; | |
5433 | } | |
5434 | } | |
5435 | } | |
64d3a1f0 | 5436 | } |
0043b528 JJ |
5437 | (if (cmp == GT_EXPR || cmp == GE_EXPR) |
5438 | (if (REAL_VALUE_ISINF (c2)) | |
5439 | /* sqrt(x) > y is x == +Inf, when y is very large. */ | |
5440 | (if (HONOR_INFINITIES (@0)) | |
5441 | (eq @0 { build_real (TREE_TYPE (@0), c2); }) | |
5442 | { constant_boolean_node (false, type); }) | |
5443 | /* sqrt(x) > c is the same as x > c*c. */ | |
5444 | (if (ncmp != ERROR_MARK) | |
5445 | (if (ncmp == GE_EXPR) | |
5446 | (ge @0 { build_real (TREE_TYPE (@0), c2); }) | |
5447 | (gt @0 { build_real (TREE_TYPE (@0), c2); })))) | |
5448 | /* else if (cmp == LT_EXPR || cmp == LE_EXPR) */ | |
5449 | (if (REAL_VALUE_ISINF (c2)) | |
5450 | (switch | |
5451 | /* sqrt(x) < y is always true, when y is a very large | |
5452 | value and we don't care about NaNs or Infinities. */ | |
5453 | (if (! HONOR_NANS (@0) && ! HONOR_INFINITIES (@0)) | |
5454 | { constant_boolean_node (true, type); }) | |
5455 | /* sqrt(x) < y is x != +Inf when y is very large and we | |
5456 | don't care about NaNs. */ | |
5457 | (if (! HONOR_NANS (@0)) | |
5458 | (ne @0 { build_real (TREE_TYPE (@0), c2); })) | |
5459 | /* sqrt(x) < y is x >= 0 when y is very large and we | |
5460 | don't care about Infinities. */ | |
5461 | (if (! HONOR_INFINITIES (@0)) | |
5462 | (ge @0 { build_real (TREE_TYPE (@0), dconst0); })) | |
5463 | /* sqrt(x) < y is x >= 0 && x != +Inf, when y is large. */ | |
5464 | (if (GENERIC) | |
5465 | (truth_andif | |
5466 | (ge @0 { build_real (TREE_TYPE (@0), dconst0); }) | |
5467 | (ne @0 { build_real (TREE_TYPE (@0), c2); })))) | |
5468 | /* sqrt(x) < c is the same as x < c*c, if we ignore NaNs. */ | |
5469 | (if (ncmp != ERROR_MARK && ! HONOR_NANS (@0)) | |
5470 | (if (ncmp == LT_EXPR) | |
5471 | (lt @0 { build_real (TREE_TYPE (@0), c2); }) | |
5472 | (le @0 { build_real (TREE_TYPE (@0), c2); })) | |
5473 | /* sqrt(x) < c is the same as x >= 0 && x < c*c. */ | |
5474 | (if (ncmp != ERROR_MARK && GENERIC) | |
5475 | (if (ncmp == LT_EXPR) | |
5476 | (truth_andif | |
5477 | (ge @0 { build_real (TREE_TYPE (@0), dconst0); }) | |
5478 | (lt @0 { build_real (TREE_TYPE (@0), c2); })) | |
5479 | (truth_andif | |
5480 | (ge @0 { build_real (TREE_TYPE (@0), dconst0); }) | |
5481 | (le @0 { build_real (TREE_TYPE (@0), c2); }))))))))))) | |
0ca2e7f7 PK |
5482 | /* Transform sqrt(x) cmp sqrt(y) -> x cmp y. */ |
5483 | (simplify | |
5484 | (cmp (sq @0) (sq @1)) | |
5485 | (if (! HONOR_NANS (@0)) | |
5486 | (cmp @0 @1)))))) | |
2ee05f1e | 5487 | |
e41ec71b | 5488 | /* Optimize various special cases of (FTYPE) N CMP (FTYPE) M. */ |
f3842847 YG |
5489 | (for cmp (lt le eq ne ge gt unordered ordered unlt unle ungt unge uneq ltgt) |
5490 | icmp (lt le eq ne ge gt unordered ordered lt le gt ge eq ne) | |
e41ec71b YG |
5491 | (simplify |
5492 | (cmp (float@0 @1) (float @2)) | |
5493 | (if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (@0)) | |
5494 | && ! DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@0))) | |
5495 | (with | |
5496 | { | |
5497 | format_helper fmt (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (@0)))); | |
5498 | tree type1 = TREE_TYPE (@1); | |
5499 | bool type1_signed_p = TYPE_SIGN (type1) == SIGNED; | |
5500 | tree type2 = TREE_TYPE (@2); | |
5501 | bool type2_signed_p = TYPE_SIGN (type2) == SIGNED; | |
5502 | } | |
5503 | (if (fmt.can_represent_integral_type_p (type1) | |
5504 | && fmt.can_represent_integral_type_p (type2)) | |
f3842847 YG |
5505 | (if (cmp == ORDERED_EXPR || cmp == UNORDERED_EXPR) |
5506 | { constant_boolean_node (cmp == ORDERED_EXPR, type); } | |
5507 | (if (TYPE_PRECISION (type1) > TYPE_PRECISION (type2) | |
5508 | && type1_signed_p >= type2_signed_p) | |
5509 | (icmp @1 (convert @2)) | |
5510 | (if (TYPE_PRECISION (type1) < TYPE_PRECISION (type2) | |
5511 | && type1_signed_p <= type2_signed_p) | |
5512 | (icmp (convert:type2 @1) @2) | |
5513 | (if (TYPE_PRECISION (type1) == TYPE_PRECISION (type2) | |
5514 | && type1_signed_p == type2_signed_p) | |
5515 | (icmp @1 @2)))))))))) | |
e41ec71b | 5516 | |
c779bea5 YG |
5517 | /* Optimize various special cases of (FTYPE) N CMP CST. */ |
5518 | (for cmp (lt le eq ne ge gt) | |
5519 | icmp (le le eq ne ge ge) | |
5520 | (simplify | |
5521 | (cmp (float @0) REAL_CST@1) | |
5522 | (if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (@1)) | |
5523 | && ! DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@1))) | |
5524 | (with | |
5525 | { | |
5526 | tree itype = TREE_TYPE (@0); | |
c779bea5 YG |
5527 | format_helper fmt (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (@1)))); |
5528 | const REAL_VALUE_TYPE *cst = TREE_REAL_CST_PTR (@1); | |
5529 | /* Be careful to preserve any potential exceptions due to | |
5530 | NaNs. qNaNs are ok in == or != context. | |
5531 | TODO: relax under -fno-trapping-math or | |
5532 | -fno-signaling-nans. */ | |
5533 | bool exception_p | |
5534 | = real_isnan (cst) && (cst->signalling | |
c651dca2 | 5535 | || (cmp != EQ_EXPR && cmp != NE_EXPR)); |
c779bea5 YG |
5536 | } |
5537 | /* TODO: allow non-fitting itype and SNaNs when | |
5538 | -fno-trapping-math. */ | |
e41ec71b | 5539 | (if (fmt.can_represent_integral_type_p (itype) && ! exception_p) |
c779bea5 YG |
5540 | (with |
5541 | { | |
e41ec71b | 5542 | signop isign = TYPE_SIGN (itype); |
c779bea5 YG |
5543 | REAL_VALUE_TYPE imin, imax; |
5544 | real_from_integer (&imin, fmt, wi::min_value (itype), isign); | |
5545 | real_from_integer (&imax, fmt, wi::max_value (itype), isign); | |
5546 | ||
5547 | REAL_VALUE_TYPE icst; | |
5548 | if (cmp == GT_EXPR || cmp == GE_EXPR) | |
5549 | real_ceil (&icst, fmt, cst); | |
5550 | else if (cmp == LT_EXPR || cmp == LE_EXPR) | |
5551 | real_floor (&icst, fmt, cst); | |
5552 | else | |
5553 | real_trunc (&icst, fmt, cst); | |
5554 | ||
b09bf97b | 5555 | bool cst_int_p = !real_isnan (cst) && real_identical (&icst, cst); |
c779bea5 YG |
5556 | |
5557 | bool overflow_p = false; | |
5558 | wide_int icst_val | |
5559 | = real_to_integer (&icst, &overflow_p, TYPE_PRECISION (itype)); | |
5560 | } | |
5561 | (switch | |
5562 | /* Optimize cases when CST is outside of ITYPE's range. */ | |
5563 | (if (real_compare (LT_EXPR, cst, &imin)) | |
5564 | { constant_boolean_node (cmp == GT_EXPR || cmp == GE_EXPR || cmp == NE_EXPR, | |
5565 | type); }) | |
5566 | (if (real_compare (GT_EXPR, cst, &imax)) | |
5567 | { constant_boolean_node (cmp == LT_EXPR || cmp == LE_EXPR || cmp == NE_EXPR, | |
5568 | type); }) | |
5569 | /* Remove cast if CST is an integer representable by ITYPE. */ | |
5570 | (if (cst_int_p) | |
5571 | (cmp @0 { gcc_assert (!overflow_p); | |
5572 | wide_int_to_tree (itype, icst_val); }) | |
5573 | ) | |
5574 | /* When CST is fractional, optimize | |
5575 | (FTYPE) N == CST -> 0 | |
5576 | (FTYPE) N != CST -> 1. */ | |
5577 | (if (cmp == EQ_EXPR || cmp == NE_EXPR) | |
03cc70b5 | 5578 | { constant_boolean_node (cmp == NE_EXPR, type); }) |
c779bea5 YG |
5579 | /* Otherwise replace with sensible integer constant. */ |
5580 | (with | |
5581 | { | |
5582 | gcc_checking_assert (!overflow_p); | |
5583 | } | |
5584 | (icmp @0 { wide_int_to_tree (itype, icst_val); }))))))))) | |
5585 | ||
40fd269a MG |
5586 | /* Fold A /[ex] B CMP C to A CMP B * C. */ |
5587 | (for cmp (eq ne) | |
5588 | (simplify | |
5589 | (cmp (exact_div @0 @1) INTEGER_CST@2) | |
5590 | (if (!integer_zerop (@1)) | |
8e6cdc90 | 5591 | (if (wi::to_wide (@2) == 0) |
40fd269a MG |
5592 | (cmp @0 @2) |
5593 | (if (TREE_CODE (@1) == INTEGER_CST) | |
5594 | (with | |
5595 | { | |
4a669ac3 | 5596 | wi::overflow_type ovf; |
8e6cdc90 RS |
5597 | wide_int prod = wi::mul (wi::to_wide (@2), wi::to_wide (@1), |
5598 | TYPE_SIGN (TREE_TYPE (@1)), &ovf); | |
40fd269a MG |
5599 | } |
5600 | (if (ovf) | |
5601 | { constant_boolean_node (cmp == NE_EXPR, type); } | |
5602 | (cmp @0 { wide_int_to_tree (TREE_TYPE (@0), prod); })))))))) | |
5603 | (for cmp (lt le gt ge) | |
5604 | (simplify | |
5605 | (cmp (exact_div @0 INTEGER_CST@1) INTEGER_CST@2) | |
8e6cdc90 | 5606 | (if (wi::gt_p (wi::to_wide (@1), 0, TYPE_SIGN (TREE_TYPE (@1)))) |
40fd269a MG |
5607 | (with |
5608 | { | |
4a669ac3 | 5609 | wi::overflow_type ovf; |
8e6cdc90 RS |
5610 | wide_int prod = wi::mul (wi::to_wide (@2), wi::to_wide (@1), |
5611 | TYPE_SIGN (TREE_TYPE (@1)), &ovf); | |
40fd269a MG |
5612 | } |
5613 | (if (ovf) | |
8e6cdc90 RS |
5614 | { constant_boolean_node (wi::lt_p (wi::to_wide (@2), 0, |
5615 | TYPE_SIGN (TREE_TYPE (@2))) | |
40fd269a MG |
5616 | != (cmp == LT_EXPR || cmp == LE_EXPR), type); } |
5617 | (cmp @0 { wide_int_to_tree (TREE_TYPE (@0), prod); })))))) | |
5618 | ||
9cf60d3b MG |
5619 | /* Fold (size_t)(A /[ex] B) CMP C to (size_t)A CMP (size_t)B * C or A CMP' 0. |
5620 | ||
5621 | For small C (less than max/B), this is (size_t)A CMP (size_t)B * C. | |
5622 | For large C (more than min/B+2^size), this is also true, with the | |
5623 | multiplication computed modulo 2^size. | |
5624 | For intermediate C, this just tests the sign of A. */ | |
5625 | (for cmp (lt le gt ge) | |
5626 | cmp2 (ge ge lt lt) | |
5627 | (simplify | |
5628 | (cmp (convert (exact_div @0 INTEGER_CST@1)) INTEGER_CST@2) | |
5629 | (if (tree_nop_conversion_p (TREE_TYPE (@0), TREE_TYPE (@2)) | |
5630 | && TYPE_UNSIGNED (TREE_TYPE (@2)) && !TYPE_UNSIGNED (TREE_TYPE (@0)) | |
5631 | && wi::gt_p (wi::to_wide (@1), 0, TYPE_SIGN (TREE_TYPE (@1)))) | |
5632 | (with | |
5633 | { | |
5634 | tree utype = TREE_TYPE (@2); | |
5635 | wide_int denom = wi::to_wide (@1); | |
5636 | wide_int right = wi::to_wide (@2); | |
5637 | wide_int smax = wi::sdiv_trunc (wi::max_value (TREE_TYPE (@0)), denom); | |
5638 | wide_int smin = wi::sdiv_trunc (wi::min_value (TREE_TYPE (@0)), denom); | |
5639 | bool small = wi::leu_p (right, smax); | |
5640 | bool large = wi::geu_p (right, smin); | |
5641 | } | |
5642 | (if (small || large) | |
5643 | (cmp (convert:utype @0) (mult @2 (convert @1))) | |
5644 | (cmp2 @0 { build_zero_cst (TREE_TYPE (@0)); })))))) | |
5645 | ||
cfdc4f33 MG |
5646 | /* Unordered tests if either argument is a NaN. */ |
5647 | (simplify | |
5648 | (bit_ior (unordered @0 @0) (unordered @1 @1)) | |
aea417d7 | 5649 | (if (types_match (@0, @1)) |
cfdc4f33 | 5650 | (unordered @0 @1))) |
257b01ba MG |
5651 | (simplify |
5652 | (bit_and (ordered @0 @0) (ordered @1 @1)) | |
5653 | (if (types_match (@0, @1)) | |
5654 | (ordered @0 @1))) | |
cfdc4f33 MG |
5655 | (simplify |
5656 | (bit_ior:c (unordered @0 @0) (unordered:c@2 @0 @1)) | |
5657 | @2) | |
257b01ba MG |
5658 | (simplify |
5659 | (bit_and:c (ordered @0 @0) (ordered:c@2 @0 @1)) | |
5660 | @2) | |
e18c1d66 | 5661 | |
90c6f26c RB |
5662 | /* Simple range test simplifications. */ |
5663 | /* A < B || A >= B -> true. */ | |
5d30c58d RB |
5664 | (for test1 (lt le le le ne ge) |
5665 | test2 (ge gt ge ne eq ne) | |
90c6f26c RB |
5666 | (simplify |
5667 | (bit_ior:c (test1 @0 @1) (test2 @0 @1)) | |
5668 | (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)) | |
5669 | || VECTOR_INTEGER_TYPE_P (TREE_TYPE (@0))) | |
5670 | { constant_boolean_node (true, type); }))) | |
5671 | /* A < B && A >= B -> false. */ | |
5672 | (for test1 (lt lt lt le ne eq) | |
5673 | test2 (ge gt eq gt eq gt) | |
5674 | (simplify | |
5675 | (bit_and:c (test1 @0 @1) (test2 @0 @1)) | |
5676 | (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)) | |
5677 | || VECTOR_INTEGER_TYPE_P (TREE_TYPE (@0))) | |
5678 | { constant_boolean_node (false, type); }))) | |
5679 | ||
9ebc3467 YG |
5680 | /* A & (2**N - 1) <= 2**K - 1 -> A & (2**N - 2**K) == 0 |
5681 | A & (2**N - 1) > 2**K - 1 -> A & (2**N - 2**K) != 0 | |
5682 | ||
5683 | Note that comparisons | |
5684 | A & (2**N - 1) < 2**K -> A & (2**N - 2**K) == 0 | |
5685 | A & (2**N - 1) >= 2**K -> A & (2**N - 2**K) != 0 | |
5686 | will be canonicalized to above so there's no need to | |
5687 | consider them here. | |
5688 | */ | |
5689 | ||
5690 | (for cmp (le gt) | |
5691 | eqcmp (eq ne) | |
5692 | (simplify | |
5693 | (cmp (bit_and@0 @1 INTEGER_CST@2) INTEGER_CST@3) | |
5694 | (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))) | |
5695 | (with | |
5696 | { | |
5697 | tree ty = TREE_TYPE (@0); | |
5698 | unsigned prec = TYPE_PRECISION (ty); | |
5699 | wide_int mask = wi::to_wide (@2, prec); | |
5700 | wide_int rhs = wi::to_wide (@3, prec); | |
5701 | signop sgn = TYPE_SIGN (ty); | |
5702 | } | |
5703 | (if ((mask & (mask + 1)) == 0 && wi::gt_p (rhs, 0, sgn) | |
5704 | && (rhs & (rhs + 1)) == 0 && wi::ge_p (mask, rhs, sgn)) | |
5705 | (eqcmp (bit_and @1 { wide_int_to_tree (ty, mask - rhs); }) | |
5706 | { build_zero_cst (ty); })))))) | |
5707 | ||
534bd33b MG |
5708 | /* -A CMP -B -> B CMP A. */ |
5709 | (for cmp (tcc_comparison) | |
5710 | scmp (swapped_tcc_comparison) | |
5711 | (simplify | |
5712 | (cmp (negate @0) (negate @1)) | |
5713 | (if (FLOAT_TYPE_P (TREE_TYPE (@0)) | |
5714 | || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0)) | |
5715 | && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))) | |
5716 | (scmp @0 @1))) | |
5717 | (simplify | |
5718 | (cmp (negate @0) CONSTANT_CLASS_P@1) | |
5719 | (if (FLOAT_TYPE_P (TREE_TYPE (@0)) | |
5720 | || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0)) | |
5721 | && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))) | |
23f27839 | 5722 | (with { tree tem = const_unop (NEGATE_EXPR, TREE_TYPE (@0), @1); } |
534bd33b MG |
5723 | (if (tem && !TREE_OVERFLOW (tem)) |
5724 | (scmp @0 { tem; })))))) | |
5725 | ||
b0eb889b MG |
5726 | /* Convert ABS_EXPR<x> == 0 or ABS_EXPR<x> != 0 to x == 0 or x != 0. */ |
5727 | (for op (eq ne) | |
5728 | (simplify | |
5729 | (op (abs @0) zerop@1) | |
5730 | (op @0 @1))) | |
5731 | ||
6358a676 MG |
5732 | /* From fold_sign_changed_comparison and fold_widened_comparison. |
5733 | FIXME: the lack of symmetry is disturbing. */ | |
79d4f7c6 RB |
5734 | (for cmp (simple_comparison) |
5735 | (simplify | |
5736 | (cmp (convert@0 @00) (convert?@1 @10)) | |
452ec2a5 | 5737 | (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)) |
79d4f7c6 RB |
5738 | /* Disable this optimization if we're casting a function pointer |
5739 | type on targets that require function pointer canonicalization. */ | |
5740 | && !(targetm.have_canonicalize_funcptr_for_compare () | |
400bc526 JDA |
5741 | && ((POINTER_TYPE_P (TREE_TYPE (@00)) |
5742 | && FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@00)))) | |
5743 | || (POINTER_TYPE_P (TREE_TYPE (@10)) | |
5744 | && FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@10)))))) | |
2fde61e3 | 5745 | && single_use (@0)) |
79d4f7c6 RB |
5746 | (if (TYPE_PRECISION (TREE_TYPE (@00)) == TYPE_PRECISION (TREE_TYPE (@0)) |
5747 | && (TREE_CODE (@10) == INTEGER_CST | |
6358a676 | 5748 | || @1 != @10) |
79d4f7c6 RB |
5749 | && (TYPE_UNSIGNED (TREE_TYPE (@00)) == TYPE_UNSIGNED (TREE_TYPE (@0)) |
5750 | || cmp == NE_EXPR | |
5751 | || cmp == EQ_EXPR) | |
a3ca1fc5 RB |
5752 | && !POINTER_TYPE_P (TREE_TYPE (@00)) |
5753 | /* (int)bool:32 != (int)uint is not the same as | |
5754 | bool:32 != (bool:32)uint since boolean types only have two valid | |
5755 | values independent of their precision. */ | |
5756 | && (TREE_CODE (TREE_TYPE (@00)) != BOOLEAN_TYPE | |
5757 | || TREE_CODE (TREE_TYPE (@10)) == BOOLEAN_TYPE)) | |
79d4f7c6 RB |
5758 | /* ??? The special-casing of INTEGER_CST conversion was in the original |
5759 | code and here to avoid a spurious overflow flag on the resulting | |
5760 | constant which fold_convert produces. */ | |
5761 | (if (TREE_CODE (@1) == INTEGER_CST) | |
5762 | (cmp @00 { force_fit_type (TREE_TYPE (@00), wi::to_widest (@1), 0, | |
5763 | TREE_OVERFLOW (@1)); }) | |
5764 | (cmp @00 (convert @1))) | |
5765 | ||
5766 | (if (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (TREE_TYPE (@00))) | |
5767 | /* If possible, express the comparison in the shorter mode. */ | |
5768 | (if ((cmp == EQ_EXPR || cmp == NE_EXPR | |
7fd82d52 PP |
5769 | || TYPE_UNSIGNED (TREE_TYPE (@0)) == TYPE_UNSIGNED (TREE_TYPE (@00)) |
5770 | || (!TYPE_UNSIGNED (TREE_TYPE (@0)) | |
5771 | && TYPE_UNSIGNED (TREE_TYPE (@00)))) | |
79d4f7c6 RB |
5772 | && (types_match (TREE_TYPE (@10), TREE_TYPE (@00)) |
5773 | || ((TYPE_PRECISION (TREE_TYPE (@00)) | |
5774 | >= TYPE_PRECISION (TREE_TYPE (@10))) | |
5775 | && (TYPE_UNSIGNED (TREE_TYPE (@00)) | |
5776 | == TYPE_UNSIGNED (TREE_TYPE (@10)))) | |
5777 | || (TREE_CODE (@10) == INTEGER_CST | |
f6c15759 | 5778 | && INTEGRAL_TYPE_P (TREE_TYPE (@00)) |
79d4f7c6 RB |
5779 | && int_fits_type_p (@10, TREE_TYPE (@00))))) |
5780 | (cmp @00 (convert @10)) | |
5781 | (if (TREE_CODE (@10) == INTEGER_CST | |
f6c15759 | 5782 | && INTEGRAL_TYPE_P (TREE_TYPE (@00)) |
79d4f7c6 RB |
5783 | && !int_fits_type_p (@10, TREE_TYPE (@00))) |
5784 | (with | |
5785 | { | |
5786 | tree min = lower_bound_in_type (TREE_TYPE (@10), TREE_TYPE (@00)); | |
5787 | tree max = upper_bound_in_type (TREE_TYPE (@10), TREE_TYPE (@00)); | |
5788 | bool above = integer_nonzerop (const_binop (LT_EXPR, type, max, @10)); | |
5789 | bool below = integer_nonzerop (const_binop (LT_EXPR, type, @10, min)); | |
5790 | } | |
5791 | (if (above || below) | |
5792 | (if (cmp == EQ_EXPR || cmp == NE_EXPR) | |
5793 | { constant_boolean_node (cmp == EQ_EXPR ? false : true, type); } | |
5794 | (if (cmp == LT_EXPR || cmp == LE_EXPR) | |
5795 | { constant_boolean_node (above ? true : false, type); } | |
5796 | (if (cmp == GT_EXPR || cmp == GE_EXPR) | |
cf716ab5 RB |
5797 | { constant_boolean_node (above ? false : true, type); }))))))))) |
5798 | /* Fold (double)float1 CMP (double)float2 into float1 CMP float2. */ | |
5799 | (if (FLOAT_TYPE_P (TREE_TYPE (@00)) | |
5800 | && (DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@0)) | |
5801 | == DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@00))) | |
5802 | && (DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@0)) | |
5803 | == DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@10)))) | |
5804 | (with | |
5805 | { | |
5806 | tree type1 = TREE_TYPE (@10); | |
5807 | if (TREE_CODE (@10) == REAL_CST && !DECIMAL_FLOAT_TYPE_P (type1)) | |
5808 | { | |
5809 | REAL_VALUE_TYPE orig = TREE_REAL_CST (@10); | |
5810 | if (TYPE_PRECISION (type1) > TYPE_PRECISION (float_type_node) | |
5811 | && exact_real_truncate (TYPE_MODE (float_type_node), &orig)) | |
5812 | type1 = float_type_node; | |
5813 | if (TYPE_PRECISION (type1) > TYPE_PRECISION (double_type_node) | |
5814 | && exact_real_truncate (TYPE_MODE (double_type_node), &orig)) | |
5815 | type1 = double_type_node; | |
5816 | } | |
5817 | tree newtype | |
5818 | = (TYPE_PRECISION (TREE_TYPE (@00)) > TYPE_PRECISION (type1) | |
5819 | ? TREE_TYPE (@00) : type1); | |
5820 | } | |
5821 | (if (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (newtype)) | |
5822 | (cmp (convert:newtype @00) (convert:newtype @10)))))))) | |
5823 | ||
66e1cacf | 5824 | |
96a111a3 | 5825 | (for cmp (eq ne) |
96a111a3 | 5826 | (simplify |
5124c34f | 5827 | /* SSA names are canonicalized to 2nd place. */ |
96a111a3 | 5828 | (cmp addr@0 SSA_NAME@1) |
5124c34f | 5829 | (with |
49bf49bb RB |
5830 | { |
5831 | poly_int64 off; tree base; | |
5832 | tree addr = (TREE_CODE (@0) == SSA_NAME | |
5833 | ? gimple_assign_rhs1 (SSA_NAME_DEF_STMT (@0)) : @0); | |
5834 | } | |
5124c34f RB |
5835 | /* A local variable can never be pointed to by |
5836 | the default SSA name of an incoming parameter. */ | |
5837 | (if (SSA_NAME_IS_DEFAULT_DEF (@1) | |
5838 | && TREE_CODE (SSA_NAME_VAR (@1)) == PARM_DECL | |
49bf49bb | 5839 | && (base = get_base_address (TREE_OPERAND (addr, 0))) |
5124c34f RB |
5840 | && TREE_CODE (base) == VAR_DECL |
5841 | && auto_var_in_fn_p (base, current_function_decl)) | |
5842 | (if (cmp == NE_EXPR) | |
5843 | { constant_boolean_node (true, type); } | |
5844 | { constant_boolean_node (false, type); }) | |
5845 | /* If the address is based on @1 decide using the offset. */ | |
49bf49bb | 5846 | (if ((base = get_addr_base_and_unit_offset (TREE_OPERAND (addr, 0), &off)) |
5124c34f RB |
5847 | && TREE_CODE (base) == MEM_REF |
5848 | && TREE_OPERAND (base, 0) == @1) | |
5849 | (with { off += mem_ref_offset (base).force_shwi (); } | |
5850 | (if (known_ne (off, 0)) | |
5851 | { constant_boolean_node (cmp == NE_EXPR, type); } | |
5852 | (if (known_eq (off, 0)) | |
5853 | { constant_boolean_node (cmp == EQ_EXPR, type); })))))))) | |
96a111a3 | 5854 | |
66e1cacf RB |
5855 | /* Equality compare simplifications from fold_binary */ |
5856 | (for cmp (eq ne) | |
5857 | ||
5858 | /* If we have (A | C) == D where C & ~D != 0, convert this into 0. | |
5859 | Similarly for NE_EXPR. */ | |
5860 | (simplify | |
5861 | (cmp (convert?@3 (bit_ior @0 INTEGER_CST@1)) INTEGER_CST@2) | |
5862 | (if (tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@0)) | |
8e6cdc90 | 5863 | && wi::bit_and_not (wi::to_wide (@1), wi::to_wide (@2)) != 0) |
66e1cacf RB |
5864 | { constant_boolean_node (cmp == NE_EXPR, type); })) |
5865 | ||
5866 | /* (X ^ Y) == 0 becomes X == Y, and (X ^ Y) != 0 becomes X != Y. */ | |
5867 | (simplify | |
5868 | (cmp (bit_xor @0 @1) integer_zerop) | |
5869 | (cmp @0 @1)) | |
5870 | ||
5871 | /* (X ^ Y) == Y becomes X == 0. | |
5872 | Likewise (X ^ Y) == X becomes Y == 0. */ | |
5873 | (simplify | |
99e943a2 | 5874 | (cmp:c (bit_xor:c @0 @1) @0) |
66e1cacf RB |
5875 | (cmp @1 { build_zero_cst (TREE_TYPE (@1)); })) |
5876 | ||
f5f18384 JJ |
5877 | /* (X & Y) == X becomes (X & ~Y) == 0. */ |
5878 | (simplify | |
5879 | (cmp:c (bit_and:c @0 @1) @0) | |
5880 | (cmp (bit_and @0 (bit_not! @1)) { build_zero_cst (TREE_TYPE (@0)); })) | |
2b536797 JJ |
5881 | (simplify |
5882 | (cmp:c (convert@3 (bit_and (convert@2 @0) INTEGER_CST@1)) (convert @0)) | |
5883 | (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)) | |
5884 | && INTEGRAL_TYPE_P (TREE_TYPE (@2)) | |
5885 | && INTEGRAL_TYPE_P (TREE_TYPE (@3)) | |
5886 | && TYPE_PRECISION (TREE_TYPE (@2)) == TYPE_PRECISION (TREE_TYPE (@0)) | |
5887 | && TYPE_PRECISION (TREE_TYPE (@3)) > TYPE_PRECISION (TREE_TYPE (@2)) | |
5888 | && !wi::neg_p (wi::to_wide (@1))) | |
5889 | (cmp (bit_and @0 (convert (bit_not @1))) | |
5890 | { build_zero_cst (TREE_TYPE (@0)); }))) | |
f5f18384 JJ |
5891 | |
5892 | /* (X | Y) == Y becomes (X & ~Y) == 0. */ | |
5893 | (simplify | |
5894 | (cmp:c (bit_ior:c @0 @1) @1) | |
5895 | (cmp (bit_and @0 (bit_not! @1)) { build_zero_cst (TREE_TYPE (@0)); })) | |
f5f18384 | 5896 | |
66e1cacf RB |
5897 | /* (X ^ C1) op C2 can be rewritten as X op (C1 ^ C2). */ |
5898 | (simplify | |
5899 | (cmp (convert?@3 (bit_xor @0 INTEGER_CST@1)) INTEGER_CST@2) | |
5900 | (if (tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@0))) | |
d057c866 | 5901 | (cmp @0 (bit_xor @1 (convert @2))))) |
d057c866 RB |
5902 | |
5903 | (simplify | |
5904 | (cmp (convert? addr@0) integer_zerop) | |
5905 | (if (tree_single_nonzero_warnv_p (@0, NULL)) | |
73a80434 JJ |
5906 | { constant_boolean_node (cmp == NE_EXPR, type); })) |
5907 | ||
5908 | /* (X & C) op (Y & C) into (X ^ Y) & C op 0. */ | |
5909 | (simplify | |
5910 | (cmp (bit_and:cs @0 @2) (bit_and:cs @1 @2)) | |
5911 | (cmp (bit_and (bit_xor @0 @1) @2) { build_zero_cst (TREE_TYPE (@2)); }))) | |
d057c866 | 5912 | |
6b5c7ee0 JJ |
5913 | /* (X < 0) != (Y < 0) into (X ^ Y) < 0. |
5914 | (X >= 0) != (Y >= 0) into (X ^ Y) < 0. | |
5915 | (X < 0) == (Y < 0) into (X ^ Y) >= 0. | |
5916 | (X >= 0) == (Y >= 0) into (X ^ Y) >= 0. */ | |
5917 | (for cmp (eq ne) | |
5918 | ncmp (ge lt) | |
5919 | (for sgncmp (ge lt) | |
5920 | (simplify | |
5921 | (cmp (sgncmp @0 integer_zerop@2) (sgncmp @1 integer_zerop)) | |
5922 | (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0)) | |
5923 | && !TYPE_UNSIGNED (TREE_TYPE (@0)) | |
5924 | && types_match (@0, @1)) | |
5925 | (ncmp (bit_xor @0 @1) @2))))) | |
5926 | /* (X < 0) == (Y >= 0) into (X ^ Y) < 0. | |
5927 | (X < 0) != (Y >= 0) into (X ^ Y) >= 0. */ | |
5928 | (for cmp (eq ne) | |
5929 | ncmp (lt ge) | |
5930 | (simplify | |
5931 | (cmp:c (lt @0 integer_zerop@2) (ge @1 integer_zerop)) | |
5932 | (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0)) | |
5933 | && !TYPE_UNSIGNED (TREE_TYPE (@0)) | |
5934 | && types_match (@0, @1)) | |
5935 | (ncmp (bit_xor @0 @1) @2)))) | |
5936 | ||
b0eb889b MG |
5937 | /* If we have (A & C) == C where C is a power of 2, convert this into |
5938 | (A & C) != 0. Similarly for NE_EXPR. */ | |
5939 | (for cmp (eq ne) | |
5940 | icmp (ne eq) | |
5941 | (simplify | |
5942 | (cmp (bit_and@2 @0 integer_pow2p@1) @1) | |
5943 | (icmp @2 { build_zero_cst (TREE_TYPE (@0)); }))) | |
03cc70b5 | 5944 | |
ac68f904 | 5945 | #if GIMPLE |
7f04b0d7 RB |
5946 | /* From fold_binary_op_with_conditional_arg handle the case of |
5947 | rewriting (a ? b : c) > d to a ? (b > d) : (c > d) when the | |
5948 | compares simplify. */ | |
5949 | (for cmp (simple_comparison) | |
5950 | (simplify | |
5951 | (cmp:c (cond @0 @1 @2) @3) | |
5952 | /* Do not move possibly trapping operations into the conditional as this | |
5953 | pessimizes code and causes gimplification issues when applied late. */ | |
5954 | (if (!FLOAT_TYPE_P (TREE_TYPE (@3)) | |
ac68f904 | 5955 | || !operation_could_trap_p (cmp, true, false, @3)) |
7f04b0d7 | 5956 | (cond @0 (cmp! @1 @3) (cmp! @2 @3))))) |
ac68f904 | 5957 | #endif |
7f04b0d7 | 5958 | |
1fd76b24 AP |
5959 | (for cmp (ge lt) |
5960 | /* x < 0 ? ~y : y into (x >> (prec-1)) ^ y. */ | |
5961 | /* x >= 0 ? ~y : y into ~((x >> (prec-1)) ^ y). */ | |
5962 | (simplify | |
5963 | (cond (cmp @0 integer_zerop) (bit_not @1) @1) | |
5964 | (if (INTEGRAL_TYPE_P (type) | |
5965 | && INTEGRAL_TYPE_P (TREE_TYPE (@0)) | |
5966 | && !TYPE_UNSIGNED (TREE_TYPE (@0)) | |
5967 | && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (type)) | |
5968 | (with | |
5969 | { | |
5970 | tree shifter = build_int_cst (integer_type_node, TYPE_PRECISION (type) - 1); | |
5971 | } | |
5972 | (if (cmp == LT_EXPR) | |
5973 | (bit_xor (convert (rshift @0 {shifter;})) @1) | |
5974 | (bit_not (bit_xor (convert (rshift @0 {shifter;})) @1)))))) | |
5975 | /* x < 0 ? y : ~y into ~((x >> (prec-1)) ^ y). */ | |
5976 | /* x >= 0 ? y : ~y into (x >> (prec-1)) ^ y. */ | |
5977 | (simplify | |
5978 | (cond (cmp @0 integer_zerop) @1 (bit_not @1)) | |
5979 | (if (INTEGRAL_TYPE_P (type) | |
5980 | && INTEGRAL_TYPE_P (TREE_TYPE (@0)) | |
5981 | && !TYPE_UNSIGNED (TREE_TYPE (@0)) | |
5982 | && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (type)) | |
5983 | (with | |
5984 | { | |
5985 | tree shifter = build_int_cst (integer_type_node, TYPE_PRECISION (type) - 1); | |
5986 | } | |
5987 | (if (cmp == GE_EXPR) | |
5988 | (bit_xor (convert (rshift @0 {shifter;})) @1) | |
5989 | (bit_not (bit_xor (convert (rshift @0 {shifter;})) @1))))))) | |
5990 | ||
519e0faa PB |
5991 | /* If we have (A & C) != 0 ? D : 0 where C and D are powers of 2, |
5992 | convert this into a shift followed by ANDing with D. */ | |
5993 | (simplify | |
5994 | (cond | |
5995 | (ne (bit_and @0 integer_pow2p@1) integer_zerop) | |
9e61e48e | 5996 | INTEGER_CST@2 integer_zerop) |
99b76adb | 5997 | (if (!POINTER_TYPE_P (type) && integer_pow2p (@2)) |
9e61e48e JJ |
5998 | (with { |
5999 | int shift = (wi::exact_log2 (wi::to_wide (@2)) | |
6000 | - wi::exact_log2 (wi::to_wide (@1))); | |
6001 | } | |
6002 | (if (shift > 0) | |
6003 | (bit_and | |
6004 | (lshift (convert @0) { build_int_cst (integer_type_node, shift); }) @2) | |
6005 | (bit_and | |
6006 | (convert (rshift @0 { build_int_cst (integer_type_node, -shift); })) | |
6007 | @2))))) | |
519e0faa | 6008 | |
b0eb889b MG |
6009 | /* If we have (A & C) != 0 where C is the sign bit of A, convert |
6010 | this into A < 0. Similarly for (A & C) == 0 into A >= 0. */ | |
6011 | (for cmp (eq ne) | |
6012 | ncmp (ge lt) | |
6013 | (simplify | |
6014 | (cmp (bit_and (convert?@2 @0) integer_pow2p@1) integer_zerop) | |
6015 | (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)) | |
2be65d9e | 6016 | && type_has_mode_precision_p (TREE_TYPE (@0)) |
b0eb889b | 6017 | && element_precision (@2) >= element_precision (@0) |
8e6cdc90 | 6018 | && wi::only_sign_bit_p (wi::to_wide (@1), element_precision (@0))) |
b0eb889b MG |
6019 | (with { tree stype = signed_type_for (TREE_TYPE (@0)); } |
6020 | (ncmp (convert:stype @0) { build_zero_cst (stype); }))))) | |
6021 | ||
519e0faa | 6022 | /* If we have A < 0 ? C : 0 where C is a power of 2, convert |
c0140e3c | 6023 | this into a right shift or sign extension followed by ANDing with C. */ |
519e0faa PB |
6024 | (simplify |
6025 | (cond | |
6026 | (lt @0 integer_zerop) | |
9e61e48e JJ |
6027 | INTEGER_CST@1 integer_zerop) |
6028 | (if (integer_pow2p (@1) | |
6029 | && !TYPE_UNSIGNED (TREE_TYPE (@0))) | |
c0140e3c | 6030 | (with { |
8e6cdc90 | 6031 | int shift = element_precision (@0) - wi::exact_log2 (wi::to_wide (@1)) - 1; |
c0140e3c JJ |
6032 | } |
6033 | (if (shift >= 0) | |
6034 | (bit_and | |
6035 | (convert (rshift @0 { build_int_cst (integer_type_node, shift); })) | |
6036 | @1) | |
6037 | /* Otherwise ctype must be wider than TREE_TYPE (@0) and pure | |
6038 | sign extension followed by AND with C will achieve the effect. */ | |
6039 | (bit_and (convert @0) @1))))) | |
519e0faa | 6040 | |
68aba1f6 RB |
6041 | /* When the addresses are not directly of decls compare base and offset. |
6042 | This implements some remaining parts of fold_comparison address | |
6043 | comparisons but still no complete part of it. Still it is good | |
6044 | enough to make fold_stmt not regress when not dispatching to fold_binary. */ | |
6045 | (for cmp (simple_comparison) | |
6046 | (simplify | |
f501d5cd | 6047 | (cmp (convert1?@2 addr@0) (convert2? addr@1)) |
68aba1f6 RB |
6048 | (with |
6049 | { | |
a90c8804 | 6050 | poly_int64 off0, off1; |
6123b998 JJ |
6051 | tree base0, base1; |
6052 | int equal = address_compare (cmp, TREE_TYPE (@2), @0, @1, base0, base1, | |
6053 | off0, off1, GENERIC); | |
68aba1f6 | 6054 | } |
6123b998 JJ |
6055 | (if (equal == 1) |
6056 | (switch | |
6057 | (if (cmp == EQ_EXPR && (known_eq (off0, off1) || known_ne (off0, off1))) | |
6058 | { constant_boolean_node (known_eq (off0, off1), type); }) | |
6059 | (if (cmp == NE_EXPR && (known_eq (off0, off1) || known_ne (off0, off1))) | |
6060 | { constant_boolean_node (known_ne (off0, off1), type); }) | |
6061 | (if (cmp == LT_EXPR && (known_lt (off0, off1) || known_ge (off0, off1))) | |
6062 | { constant_boolean_node (known_lt (off0, off1), type); }) | |
6063 | (if (cmp == LE_EXPR && (known_le (off0, off1) || known_gt (off0, off1))) | |
6064 | { constant_boolean_node (known_le (off0, off1), type); }) | |
6065 | (if (cmp == GE_EXPR && (known_ge (off0, off1) || known_lt (off0, off1))) | |
6066 | { constant_boolean_node (known_ge (off0, off1), type); }) | |
6067 | (if (cmp == GT_EXPR && (known_gt (off0, off1) || known_le (off0, off1))) | |
6068 | { constant_boolean_node (known_gt (off0, off1), type); })) | |
6069 | (if (equal == 0) | |
6070 | (switch | |
6071 | (if (cmp == EQ_EXPR) | |
6072 | { constant_boolean_node (false, type); }) | |
6073 | (if (cmp == NE_EXPR) | |
6074 | { constant_boolean_node (true, type); }))))))) | |
66e1cacf | 6075 | |
98998245 RB |
6076 | /* Simplify pointer equality compares using PTA. */ |
6077 | (for neeq (ne eq) | |
6078 | (simplify | |
6079 | (neeq @0 @1) | |
6080 | (if (POINTER_TYPE_P (TREE_TYPE (@0)) | |
6081 | && ptrs_compare_unequal (@0, @1)) | |
f913ff2a | 6082 | { constant_boolean_node (neeq != EQ_EXPR, type); }))) |
98998245 | 6083 | |
8f63caf6 | 6084 | /* PR70920: Transform (intptr_t)x eq/ne CST to x eq/ne (typeof x) CST. |
467719fb PK |
6085 | and (typeof ptr_cst) x eq/ne ptr_cst to x eq/ne (typeof x) CST. |
6086 | Disable the transform if either operand is pointer to function. | |
6087 | This broke pr22051-2.c for arm where function pointer | |
6088 | canonicalizaion is not wanted. */ | |
1c0a8806 | 6089 | |
8f63caf6 RB |
6090 | (for cmp (ne eq) |
6091 | (simplify | |
6092 | (cmp (convert @0) INTEGER_CST@1) | |
f53e7e13 JJ |
6093 | (if (((POINTER_TYPE_P (TREE_TYPE (@0)) |
6094 | && !FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@0))) | |
53fd7544 JJ |
6095 | && INTEGRAL_TYPE_P (TREE_TYPE (@1)) |
6096 | /* Don't perform this optimization in GENERIC if @0 has reference | |
6097 | type when sanitizing. See PR101210. */ | |
6098 | && !(GENERIC | |
6099 | && TREE_CODE (TREE_TYPE (@0)) == REFERENCE_TYPE | |
6100 | && (flag_sanitize & (SANITIZE_NULL | SANITIZE_ALIGNMENT)))) | |
f53e7e13 JJ |
6101 | || (INTEGRAL_TYPE_P (TREE_TYPE (@0)) |
6102 | && POINTER_TYPE_P (TREE_TYPE (@1)) | |
6103 | && !FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@1))))) | |
6104 | && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1))) | |
8f63caf6 RB |
6105 | (cmp @0 (convert @1))))) |
6106 | ||
21aacde4 RB |
6107 | /* Non-equality compare simplifications from fold_binary */ |
6108 | (for cmp (lt gt le ge) | |
6109 | /* Comparisons with the highest or lowest possible integer of | |
6110 | the specified precision will have known values. */ | |
6111 | (simplify | |
f06e47d7 JJ |
6112 | (cmp (convert?@2 @0) uniform_integer_cst_p@1) |
6113 | (if ((INTEGRAL_TYPE_P (TREE_TYPE (@1)) | |
6114 | || POINTER_TYPE_P (TREE_TYPE (@1)) | |
6115 | || VECTOR_INTEGER_TYPE_P (TREE_TYPE (@1))) | |
21aacde4 RB |
6116 | && tree_nop_conversion_p (TREE_TYPE (@2), TREE_TYPE (@0))) |
6117 | (with | |
6118 | { | |
f06e47d7 JJ |
6119 | tree cst = uniform_integer_cst_p (@1); |
6120 | tree arg1_type = TREE_TYPE (cst); | |
21aacde4 RB |
6121 | unsigned int prec = TYPE_PRECISION (arg1_type); |
6122 | wide_int max = wi::max_value (arg1_type); | |
6123 | wide_int signed_max = wi::max_value (prec, SIGNED); | |
6124 | wide_int min = wi::min_value (arg1_type); | |
6125 | } | |
6126 | (switch | |
f06e47d7 | 6127 | (if (wi::to_wide (cst) == max) |
21aacde4 RB |
6128 | (switch |
6129 | (if (cmp == GT_EXPR) | |
6130 | { constant_boolean_node (false, type); }) | |
6131 | (if (cmp == GE_EXPR) | |
6132 | (eq @2 @1)) | |
6133 | (if (cmp == LE_EXPR) | |
6134 | { constant_boolean_node (true, type); }) | |
6135 | (if (cmp == LT_EXPR) | |
6136 | (ne @2 @1)))) | |
f06e47d7 | 6137 | (if (wi::to_wide (cst) == min) |
21aacde4 RB |
6138 | (switch |
6139 | (if (cmp == LT_EXPR) | |
6140 | { constant_boolean_node (false, type); }) | |
6141 | (if (cmp == LE_EXPR) | |
6142 | (eq @2 @1)) | |
6143 | (if (cmp == GE_EXPR) | |
6144 | { constant_boolean_node (true, type); }) | |
6145 | (if (cmp == GT_EXPR) | |
6146 | (ne @2 @1)))) | |
f06e47d7 | 6147 | (if (wi::to_wide (cst) == max - 1) |
9bc22d19 RB |
6148 | (switch |
6149 | (if (cmp == GT_EXPR) | |
f06e47d7 JJ |
6150 | (eq @2 { build_uniform_cst (TREE_TYPE (@1), |
6151 | wide_int_to_tree (TREE_TYPE (cst), | |
6152 | wi::to_wide (cst) | |
6153 | + 1)); })) | |
9bc22d19 | 6154 | (if (cmp == LE_EXPR) |
f06e47d7 JJ |
6155 | (ne @2 { build_uniform_cst (TREE_TYPE (@1), |
6156 | wide_int_to_tree (TREE_TYPE (cst), | |
6157 | wi::to_wide (cst) | |
6158 | + 1)); })))) | |
6159 | (if (wi::to_wide (cst) == min + 1) | |
21aacde4 RB |
6160 | (switch |
6161 | (if (cmp == GE_EXPR) | |
f06e47d7 JJ |
6162 | (ne @2 { build_uniform_cst (TREE_TYPE (@1), |
6163 | wide_int_to_tree (TREE_TYPE (cst), | |
6164 | wi::to_wide (cst) | |
6165 | - 1)); })) | |
21aacde4 | 6166 | (if (cmp == LT_EXPR) |
f06e47d7 JJ |
6167 | (eq @2 { build_uniform_cst (TREE_TYPE (@1), |
6168 | wide_int_to_tree (TREE_TYPE (cst), | |
6169 | wi::to_wide (cst) | |
6170 | - 1)); })))) | |
6171 | (if (wi::to_wide (cst) == signed_max | |
21aacde4 RB |
6172 | && TYPE_UNSIGNED (arg1_type) |
6173 | /* We will flip the signedness of the comparison operator | |
6174 | associated with the mode of @1, so the sign bit is | |
6175 | specified by this mode. Check that @1 is the signed | |
6176 | max associated with this sign bit. */ | |
7a504f33 | 6177 | && prec == GET_MODE_PRECISION (SCALAR_INT_TYPE_MODE (arg1_type)) |
21aacde4 RB |
6178 | /* signed_type does not work on pointer types. */ |
6179 | && INTEGRAL_TYPE_P (arg1_type)) | |
6180 | /* The following case also applies to X < signed_max+1 | |
6181 | and X >= signed_max+1 because previous transformations. */ | |
6182 | (if (cmp == LE_EXPR || cmp == GT_EXPR) | |
f06e47d7 JJ |
6183 | (with { tree st = signed_type_for (TREE_TYPE (@1)); } |
6184 | (switch | |
6185 | (if (cst == @1 && cmp == LE_EXPR) | |
6186 | (ge (convert:st @0) { build_zero_cst (st); })) | |
6187 | (if (cst == @1 && cmp == GT_EXPR) | |
6188 | (lt (convert:st @0) { build_zero_cst (st); })) | |
6189 | (if (cmp == LE_EXPR) | |
6190 | (ge (view_convert:st @0) { build_zero_cst (st); })) | |
6191 | (if (cmp == GT_EXPR) | |
6192 | (lt (view_convert:st @0) { build_zero_cst (st); }))))))))))) | |
03cc70b5 | 6193 | |
b5d3d787 RB |
6194 | (for cmp (unordered ordered unlt unle ungt unge uneq ltgt) |
6195 | /* If the second operand is NaN, the result is constant. */ | |
6196 | (simplify | |
6197 | (cmp @0 REAL_CST@1) | |
6198 | (if (REAL_VALUE_ISNAN (TREE_REAL_CST (@1)) | |
6199 | && (cmp != LTGT_EXPR || ! flag_trapping_math)) | |
50301115 | 6200 | { constant_boolean_node (cmp == ORDERED_EXPR || cmp == LTGT_EXPR |
b5d3d787 | 6201 | ? false : true, type); }))) |
21aacde4 | 6202 | |
1be48781 RS |
6203 | /* Fold UNORDERED if either operand must be NaN, or neither can be. */ |
6204 | (simplify | |
6205 | (unordered @0 @1) | |
6206 | (switch | |
6207 | (if (tree_expr_nan_p (@0) || tree_expr_nan_p (@1)) | |
6208 | { constant_boolean_node (true, type); }) | |
6209 | (if (!tree_expr_maybe_nan_p (@0) && !tree_expr_maybe_nan_p (@1)) | |
6210 | { constant_boolean_node (false, type); }))) | |
6211 | ||
6212 | /* Fold ORDERED if either operand must be NaN, or neither can be. */ | |
6213 | (simplify | |
6214 | (ordered @0 @1) | |
6215 | (switch | |
6216 | (if (tree_expr_nan_p (@0) || tree_expr_nan_p (@1)) | |
6217 | { constant_boolean_node (false, type); }) | |
6218 | (if (!tree_expr_maybe_nan_p (@0) && !tree_expr_maybe_nan_p (@1)) | |
6219 | { constant_boolean_node (true, type); }))) | |
6220 | ||
55cf3946 RB |
6221 | /* bool_var != 0 becomes bool_var. */ |
6222 | (simplify | |
b5d3d787 | 6223 | (ne @0 integer_zerop) |
55cf3946 RB |
6224 | (if (TREE_CODE (TREE_TYPE (@0)) == BOOLEAN_TYPE |
6225 | && types_match (type, TREE_TYPE (@0))) | |
6226 | (non_lvalue @0))) | |
6227 | /* bool_var == 1 becomes bool_var. */ | |
6228 | (simplify | |
b5d3d787 | 6229 | (eq @0 integer_onep) |
55cf3946 RB |
6230 | (if (TREE_CODE (TREE_TYPE (@0)) == BOOLEAN_TYPE |
6231 | && types_match (type, TREE_TYPE (@0))) | |
6232 | (non_lvalue @0))) | |
b5d3d787 RB |
6233 | /* Do not handle |
6234 | bool_var == 0 becomes !bool_var or | |
6235 | bool_var != 1 becomes !bool_var | |
6236 | here because that only is good in assignment context as long | |
6237 | as we require a tcc_comparison in GIMPLE_CONDs where we'd | |
6238 | replace if (x == 0) with tem = ~x; if (tem != 0) which is | |
6239 | clearly less optimal and which we'll transform again in forwprop. */ | |
55cf3946 | 6240 | |
29df53fe TC |
6241 | /* Transform comparisons of the form (X & Y) CMP 0 to X CMP2 Z |
6242 | where ~Y + 1 == pow2 and Z = ~Y. */ | |
6243 | (for cst (VECTOR_CST INTEGER_CST) | |
6244 | (for cmp (eq ne) | |
6245 | icmp (le gt) | |
6246 | (simplify | |
6247 | (cmp (bit_and:c@2 @0 cst@1) integer_zerop) | |
6248 | (with { tree csts = bitmask_inv_cst_vector_p (@1); } | |
6249 | (if (csts && (VECTOR_TYPE_P (TREE_TYPE (@1)) || single_use (@2))) | |
f7854e2f TC |
6250 | (with { auto optab = VECTOR_TYPE_P (TREE_TYPE (@1)) |
6251 | ? optab_vector : optab_default; | |
6252 | tree utype = unsigned_type_for (TREE_TYPE (@1)); } | |
6253 | (if (target_supports_op_p (utype, icmp, optab) | |
6254 | || (optimize_vectors_before_lowering_p () | |
6255 | && (!target_supports_op_p (type, cmp, optab) | |
6256 | || !target_supports_op_p (type, BIT_AND_EXPR, optab)))) | |
6257 | (if (TYPE_UNSIGNED (TREE_TYPE (@1))) | |
6258 | (icmp @0 { csts; }) | |
6259 | (icmp (view_convert:utype @0) { csts; }))))))))) | |
29df53fe | 6260 | |
ca1206be MG |
6261 | /* When one argument is a constant, overflow detection can be simplified. |
6262 | Currently restricted to single use so as not to interfere too much with | |
e53b6e56 | 6263 | ADD_OVERFLOW detection in tree-ssa-math-opts.cc. |
32ee4728 | 6264 | CONVERT?(CONVERT?(A) + CST) CMP A -> A CMP' CST' */ |
ca1206be MG |
6265 | (for cmp (lt le ge gt) |
6266 | out (gt gt le le) | |
6267 | (simplify | |
32ee4728 JL |
6268 | (cmp:c (convert?@3 (plus@2 (convert?@4 @0) INTEGER_CST@1)) @0) |
6269 | (if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (@2)) | |
6270 | && types_match (TREE_TYPE (@0), TREE_TYPE (@3)) | |
6271 | && tree_nop_conversion_p (TREE_TYPE (@4), TREE_TYPE (@0)) | |
8e6cdc90 | 6272 | && wi::to_wide (@1) != 0 |
ca1206be | 6273 | && single_use (@2)) |
32ee4728 JL |
6274 | (with { |
6275 | unsigned int prec = TYPE_PRECISION (TREE_TYPE (@0)); | |
6276 | signop sign = TYPE_SIGN (TREE_TYPE (@0)); | |
6277 | } | |
8e6cdc90 | 6278 | (out @0 { wide_int_to_tree (TREE_TYPE (@0), |
32ee4728 | 6279 | wi::max_value (prec, sign) |
8e6cdc90 | 6280 | - wi::to_wide (@1)); }))))) |
ca1206be | 6281 | |
3563f78f | 6282 | /* To detect overflow in unsigned A - B, A < B is simpler than A - B > A. |
e53b6e56 | 6283 | However, the detection logic for SUB_OVERFLOW in tree-ssa-math-opts.cc |
3563f78f MG |
6284 | expects the long form, so we restrict the transformation for now. */ |
6285 | (for cmp (gt le) | |
6286 | (simplify | |
a8e9f9a3 | 6287 | (cmp:c (minus@2 @0 @1) @0) |
3563f78f MG |
6288 | (if (single_use (@2) |
6289 | && ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0)) | |
ff336801 | 6290 | && TYPE_UNSIGNED (TREE_TYPE (@0))) |
3563f78f | 6291 | (cmp @1 @0)))) |
3563f78f | 6292 | |
ff336801 JJ |
6293 | /* Optimize A - B + -1 >= A into B >= A for unsigned comparisons. */ |
6294 | (for cmp (ge lt) | |
6295 | (simplify | |
6296 | (cmp:c (plus (minus @0 @1) integer_minus_onep) @0) | |
6297 | (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0)) | |
6298 | && TYPE_UNSIGNED (TREE_TYPE (@0))) | |
6299 | (cmp @1 @0)))) | |
6300 | ||
3563f78f | 6301 | /* Testing for overflow is unnecessary if we already know the result. */ |
3563f78f MG |
6302 | /* A - B > A */ |
6303 | (for cmp (gt le) | |
6304 | out (ne eq) | |
6305 | (simplify | |
a8e9f9a3 | 6306 | (cmp:c (realpart (IFN_SUB_OVERFLOW@2 @0 @1)) @0) |
3563f78f MG |
6307 | (if (TYPE_UNSIGNED (TREE_TYPE (@0)) |
6308 | && types_match (TREE_TYPE (@0), TREE_TYPE (@1))) | |
6309 | (out (imagpart @2) { build_zero_cst (TREE_TYPE (@0)); })))) | |
6310 | /* A + B < A */ | |
6311 | (for cmp (lt ge) | |
6312 | out (ne eq) | |
6313 | (simplify | |
a8e9f9a3 | 6314 | (cmp:c (realpart (IFN_ADD_OVERFLOW:c@2 @0 @1)) @0) |
3563f78f MG |
6315 | (if (TYPE_UNSIGNED (TREE_TYPE (@0)) |
6316 | && types_match (TREE_TYPE (@0), TREE_TYPE (@1))) | |
6317 | (out (imagpart @2) { build_zero_cst (TREE_TYPE (@0)); })))) | |
6318 | ||
603aeb87 | 6319 | /* For unsigned operands, -1 / B < A checks whether A * B would overflow. |
0557293f | 6320 | Simplify it to __builtin_mul_overflow (A, B, <unused>). */ |
0557293f AM |
6321 | (for cmp (lt ge) |
6322 | out (ne eq) | |
6323 | (simplify | |
603aeb87 | 6324 | (cmp:c (trunc_div:s integer_all_onesp @1) @0) |
0557293f AM |
6325 | (if (TYPE_UNSIGNED (TREE_TYPE (@0)) && !VECTOR_TYPE_P (TREE_TYPE (@0))) |
6326 | (with { tree t = TREE_TYPE (@0), cpx = build_complex_type (t); } | |
6327 | (out (imagpart (IFN_MUL_OVERFLOW:cpx @0 @1)) { build_zero_cst (t); }))))) | |
55cf3946 | 6328 | |
6d938a5d JJ |
6329 | /* Similarly, for unsigned operands, (((type) A * B) >> prec) != 0 where type |
6330 | is at least twice as wide as type of A and B, simplify to | |
6331 | __builtin_mul_overflow (A, B, <unused>). */ | |
6332 | (for cmp (eq ne) | |
6333 | (simplify | |
6334 | (cmp (rshift (mult:s (convert@3 @0) (convert @1)) INTEGER_CST@2) | |
6335 | integer_zerop) | |
6336 | (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)) | |
6337 | && INTEGRAL_TYPE_P (TREE_TYPE (@3)) | |
6338 | && TYPE_UNSIGNED (TREE_TYPE (@0)) | |
6339 | && (TYPE_PRECISION (TREE_TYPE (@3)) | |
6340 | >= 2 * TYPE_PRECISION (TREE_TYPE (@0))) | |
6341 | && tree_fits_uhwi_p (@2) | |
6342 | && tree_to_uhwi (@2) == TYPE_PRECISION (TREE_TYPE (@0)) | |
6343 | && types_match (@0, @1) | |
6344 | && type_has_mode_precision_p (TREE_TYPE (@0)) | |
6345 | && (optab_handler (umulv4_optab, TYPE_MODE (TREE_TYPE (@0))) | |
6346 | != CODE_FOR_nothing)) | |
6347 | (with { tree t = TREE_TYPE (@0), cpx = build_complex_type (t); } | |
6348 | (cmp (imagpart (IFN_MUL_OVERFLOW:cpx @0 @1)) { build_zero_cst (t); }))))) | |
6349 | ||
2af6dd77 JJ |
6350 | /* Demote operands of IFN_{ADD,SUB,MUL}_OVERFLOW. */ |
6351 | (for ovf (IFN_ADD_OVERFLOW IFN_SUB_OVERFLOW IFN_MUL_OVERFLOW) | |
6352 | (simplify | |
6353 | (ovf (convert@2 @0) @1) | |
6354 | (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)) | |
6355 | && INTEGRAL_TYPE_P (TREE_TYPE (@2)) | |
8a4602c2 JJ |
6356 | && TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (TREE_TYPE (@0)) |
6357 | && (!TYPE_UNSIGNED (TREE_TYPE (@2)) || TYPE_UNSIGNED (TREE_TYPE (@0)))) | |
2af6dd77 JJ |
6358 | (ovf @0 @1))) |
6359 | (simplify | |
6360 | (ovf @1 (convert@2 @0)) | |
6361 | (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)) | |
6362 | && INTEGRAL_TYPE_P (TREE_TYPE (@2)) | |
8a4602c2 JJ |
6363 | && TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (TREE_TYPE (@0)) |
6364 | && (!TYPE_UNSIGNED (TREE_TYPE (@2)) || TYPE_UNSIGNED (TREE_TYPE (@0)))) | |
2af6dd77 JJ |
6365 | (ovf @1 @0)))) |
6366 | ||
cf78d841 | 6367 | /* Optimize __builtin_mul_overflow_p (x, cst, (utype) 0) if all 3 types |
1982fe26 JJ |
6368 | are unsigned to x > (umax / cst). Similarly for signed type, but |
6369 | in that case it needs to be outside of a range. */ | |
cf78d841 JJ |
6370 | (simplify |
6371 | (imagpart (IFN_MUL_OVERFLOW:cs@2 @0 integer_nonzerop@1)) | |
6372 | (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)) | |
cf78d841 JJ |
6373 | && TYPE_MAX_VALUE (TREE_TYPE (@0)) |
6374 | && types_match (TREE_TYPE (@0), TREE_TYPE (TREE_TYPE (@2))) | |
6375 | && int_fits_type_p (@1, TREE_TYPE (@0))) | |
1982fe26 JJ |
6376 | (if (TYPE_UNSIGNED (TREE_TYPE (@0))) |
6377 | (convert (gt @0 (trunc_div! { TYPE_MAX_VALUE (TREE_TYPE (@0)); } @1))) | |
6378 | (if (TYPE_MIN_VALUE (TREE_TYPE (@0))) | |
6379 | (if (integer_minus_onep (@1)) | |
6380 | (convert (eq @0 { TYPE_MIN_VALUE (TREE_TYPE (@0)); })) | |
6381 | (with | |
6382 | { | |
74e6a403 | 6383 | tree div = fold_convert (TREE_TYPE (@0), @1); |
1982fe26 | 6384 | tree lo = int_const_binop (TRUNC_DIV_EXPR, |
74e6a403 | 6385 | TYPE_MIN_VALUE (TREE_TYPE (@0)), div); |
1982fe26 | 6386 | tree hi = int_const_binop (TRUNC_DIV_EXPR, |
74e6a403 | 6387 | TYPE_MAX_VALUE (TREE_TYPE (@0)), div); |
1982fe26 JJ |
6388 | tree etype = range_check_type (TREE_TYPE (@0)); |
6389 | if (etype) | |
6390 | { | |
74e6a403 | 6391 | if (wi::neg_p (wi::to_wide (div))) |
1982fe26 JJ |
6392 | std::swap (lo, hi); |
6393 | lo = fold_convert (etype, lo); | |
6394 | hi = fold_convert (etype, hi); | |
6395 | hi = int_const_binop (MINUS_EXPR, hi, lo); | |
6396 | } | |
6397 | } | |
6398 | (if (etype) | |
6399 | (convert (gt (minus (convert:etype @0) { lo; }) { hi; }))))))))) | |
cf78d841 | 6400 | |
53f3cd25 RS |
6401 | /* Simplification of math builtins. These rules must all be optimizations |
6402 | as well as IL simplifications. If there is a possibility that the new | |
6403 | form could be a pessimization, the rule should go in the canonicalization | |
6404 | section that follows this one. | |
e18c1d66 | 6405 | |
53f3cd25 RS |
6406 | Rules can generally go in this section if they satisfy one of |
6407 | the following: | |
6408 | ||
6409 | - the rule describes an identity | |
6410 | ||
6411 | - the rule replaces calls with something as simple as addition or | |
6412 | multiplication | |
6413 | ||
6414 | - the rule contains unary calls only and simplifies the surrounding | |
6415 | arithmetic. (The idea here is to exclude non-unary calls in which | |
6416 | one operand is constant and in which the call is known to be cheap | |
6417 | when the operand has that value.) */ | |
52c6378a | 6418 | |
53f3cd25 | 6419 | (if (flag_unsafe_math_optimizations) |
52c6378a N |
6420 | /* Simplify sqrt(x) * sqrt(x) -> x. */ |
6421 | (simplify | |
c6cfa2bf | 6422 | (mult (SQRT_ALL@1 @0) @1) |
1be48781 | 6423 | (if (!tree_expr_maybe_signaling_nan_p (@0)) |
52c6378a N |
6424 | @0)) |
6425 | ||
ed17cb57 JW |
6426 | (for op (plus minus) |
6427 | /* Simplify (A / C) +- (B / C) -> (A +- B) / C. */ | |
6428 | (simplify | |
6429 | (op (rdiv @0 @1) | |
6430 | (rdiv @2 @1)) | |
6431 | (rdiv (op @0 @2) @1))) | |
6432 | ||
5e21d765 WD |
6433 | (for cmp (lt le gt ge) |
6434 | neg_cmp (gt ge lt le) | |
6435 | /* Simplify (x * C1) cmp C2 -> x cmp (C2 / C1), where C1 != 0. */ | |
6436 | (simplify | |
6437 | (cmp (mult @0 REAL_CST@1) REAL_CST@2) | |
6438 | (with | |
6439 | { tree tem = const_binop (RDIV_EXPR, type, @2, @1); } | |
6440 | (if (tem | |
6441 | && !(REAL_VALUE_ISINF (TREE_REAL_CST (tem)) | |
6442 | || (real_zerop (tem) && !real_zerop (@1)))) | |
6443 | (switch | |
6444 | (if (real_less (&dconst0, TREE_REAL_CST_PTR (@1))) | |
6445 | (cmp @0 { tem; })) | |
6446 | (if (real_less (TREE_REAL_CST_PTR (@1), &dconst0)) | |
6447 | (neg_cmp @0 { tem; }))))))) | |
6448 | ||
35401640 N |
6449 | /* Simplify sqrt(x) * sqrt(y) -> sqrt(x*y). */ |
6450 | (for root (SQRT CBRT) | |
6451 | (simplify | |
6452 | (mult (root:s @0) (root:s @1)) | |
6453 | (root (mult @0 @1)))) | |
6454 | ||
35401640 N |
6455 | /* Simplify expN(x) * expN(y) -> expN(x+y). */ |
6456 | (for exps (EXP EXP2 EXP10 POW10) | |
6457 | (simplify | |
6458 | (mult (exps:s @0) (exps:s @1)) | |
6459 | (exps (plus @0 @1)))) | |
6460 | ||
52c6378a | 6461 | /* Simplify a/root(b/c) into a*root(c/b). */ |
35401640 N |
6462 | (for root (SQRT CBRT) |
6463 | (simplify | |
6464 | (rdiv @0 (root:s (rdiv:s @1 @2))) | |
6465 | (mult @0 (root (rdiv @2 @1))))) | |
6466 | ||
6467 | /* Simplify x/expN(y) into x*expN(-y). */ | |
6468 | (for exps (EXP EXP2 EXP10 POW10) | |
6469 | (simplify | |
6470 | (rdiv @0 (exps:s @1)) | |
6471 | (mult @0 (exps (negate @1))))) | |
52c6378a | 6472 | |
eee7b6c4 RB |
6473 | (for logs (LOG LOG2 LOG10 LOG10) |
6474 | exps (EXP EXP2 EXP10 POW10) | |
8acda9b2 | 6475 | /* logN(expN(x)) -> x. */ |
e18c1d66 RB |
6476 | (simplify |
6477 | (logs (exps @0)) | |
8acda9b2 RS |
6478 | @0) |
6479 | /* expN(logN(x)) -> x. */ | |
6480 | (simplify | |
6481 | (exps (logs @0)) | |
6482 | @0)) | |
53f3cd25 | 6483 | |
e18c1d66 RB |
6484 | /* Optimize logN(func()) for various exponential functions. We |
6485 | want to determine the value "x" and the power "exponent" in | |
6486 | order to transform logN(x**exponent) into exponent*logN(x). */ | |
eee7b6c4 RB |
6487 | (for logs (LOG LOG LOG LOG2 LOG2 LOG2 LOG10 LOG10) |
6488 | exps (EXP2 EXP10 POW10 EXP EXP10 POW10 EXP EXP2) | |
e18c1d66 RB |
6489 | (simplify |
6490 | (logs (exps @0)) | |
c9e926ce RS |
6491 | (if (SCALAR_FLOAT_TYPE_P (type)) |
6492 | (with { | |
6493 | tree x; | |
6494 | switch (exps) | |
6495 | { | |
6496 | CASE_CFN_EXP: | |
6497 | /* Prepare to do logN(exp(exponent)) -> exponent*logN(e). */ | |
6498 | x = build_real_truncate (type, dconst_e ()); | |
6499 | break; | |
6500 | CASE_CFN_EXP2: | |
6501 | /* Prepare to do logN(exp2(exponent)) -> exponent*logN(2). */ | |
6502 | x = build_real (type, dconst2); | |
6503 | break; | |
6504 | CASE_CFN_EXP10: | |
6505 | CASE_CFN_POW10: | |
6506 | /* Prepare to do logN(exp10(exponent)) -> exponent*logN(10). */ | |
6507 | { | |
6508 | REAL_VALUE_TYPE dconst10; | |
6509 | real_from_integer (&dconst10, VOIDmode, 10, SIGNED); | |
6510 | x = build_real (type, dconst10); | |
6511 | } | |
6512 | break; | |
6513 | default: | |
6514 | gcc_unreachable (); | |
6515 | } | |
6516 | } | |
6517 | (mult (logs { x; }) @0))))) | |
53f3cd25 | 6518 | |
e18c1d66 RB |
6519 | (for logs (LOG LOG |
6520 | LOG2 LOG2 | |
6521 | LOG10 LOG10) | |
6522 | exps (SQRT CBRT) | |
6523 | (simplify | |
6524 | (logs (exps @0)) | |
c9e926ce RS |
6525 | (if (SCALAR_FLOAT_TYPE_P (type)) |
6526 | (with { | |
6527 | tree x; | |
6528 | switch (exps) | |
6529 | { | |
6530 | CASE_CFN_SQRT: | |
6531 | /* Prepare to do logN(sqrt(x)) -> 0.5*logN(x). */ | |
6532 | x = build_real (type, dconsthalf); | |
6533 | break; | |
6534 | CASE_CFN_CBRT: | |
6535 | /* Prepare to do logN(cbrt(x)) -> (1/3)*logN(x). */ | |
6536 | x = build_real_truncate (type, dconst_third ()); | |
6537 | break; | |
6538 | default: | |
6539 | gcc_unreachable (); | |
6540 | } | |
6541 | } | |
6542 | (mult { x; } (logs @0)))))) | |
53f3cd25 RS |
6543 | |
6544 | /* logN(pow(x,exponent)) -> exponent*logN(x). */ | |
e18c1d66 RB |
6545 | (for logs (LOG LOG2 LOG10) |
6546 | pows (POW) | |
6547 | (simplify | |
6548 | (logs (pows @0 @1)) | |
53f3cd25 RS |
6549 | (mult @1 (logs @0)))) |
6550 | ||
848bb6fc JJ |
6551 | /* pow(C,x) -> exp(log(C)*x) if C > 0, |
6552 | or if C is a positive power of 2, | |
6553 | pow(C,x) -> exp2(log2(C)*x). */ | |
30a2c10e | 6554 | #if GIMPLE |
e83fe013 WD |
6555 | (for pows (POW) |
6556 | exps (EXP) | |
6557 | logs (LOG) | |
848bb6fc JJ |
6558 | exp2s (EXP2) |
6559 | log2s (LOG2) | |
e83fe013 WD |
6560 | (simplify |
6561 | (pows REAL_CST@0 @1) | |
848bb6fc | 6562 | (if (real_compare (GT_EXPR, TREE_REAL_CST_PTR (@0), &dconst0) |
ef7866a3 JJ |
6563 | && real_isfinite (TREE_REAL_CST_PTR (@0)) |
6564 | /* As libmvec doesn't have a vectorized exp2, defer optimizing | |
6565 | the use_exp2 case until after vectorization. It seems actually | |
6566 | beneficial for all constants to postpone this until later, | |
6567 | because exp(log(C)*x), while faster, will have worse precision | |
6568 | and if x folds into a constant too, that is unnecessary | |
6569 | pessimization. */ | |
6570 | && canonicalize_math_after_vectorization_p ()) | |
848bb6fc JJ |
6571 | (with { |
6572 | const REAL_VALUE_TYPE *const value = TREE_REAL_CST_PTR (@0); | |
6573 | bool use_exp2 = false; | |
bae974e6 | 6574 | if (targetm.libc_has_function (function_c99_misc, TREE_TYPE (@0)) |
848bb6fc JJ |
6575 | && value->cl == rvc_normal) |
6576 | { | |
6577 | REAL_VALUE_TYPE frac_rvt = *value; | |
6578 | SET_REAL_EXP (&frac_rvt, 1); | |
6579 | if (real_equal (&frac_rvt, &dconst1)) | |
6580 | use_exp2 = true; | |
6581 | } | |
6582 | } | |
6583 | (if (!use_exp2) | |
30a2c10e JJ |
6584 | (if (optimize_pow_to_exp (@0, @1)) |
6585 | (exps (mult (logs @0) @1))) | |
ef7866a3 | 6586 | (exp2s (mult (log2s @0) @1))))))) |
30a2c10e | 6587 | #endif |
e83fe013 | 6588 | |
16ef0a8c JJ |
6589 | /* pow(C,x)*expN(y) -> expN(logN(C)*x+y) if C > 0. */ |
6590 | (for pows (POW) | |
6591 | exps (EXP EXP2 EXP10 POW10) | |
6592 | logs (LOG LOG2 LOG10 LOG10) | |
6593 | (simplify | |
6594 | (mult:c (pows:s REAL_CST@0 @1) (exps:s @2)) | |
6595 | (if (real_compare (GT_EXPR, TREE_REAL_CST_PTR (@0), &dconst0) | |
6596 | && real_isfinite (TREE_REAL_CST_PTR (@0))) | |
6597 | (exps (plus (mult (logs @0) @1) @2))))) | |
6598 | ||
53f3cd25 RS |
6599 | (for sqrts (SQRT) |
6600 | cbrts (CBRT) | |
b4838d77 | 6601 | pows (POW) |
53f3cd25 RS |
6602 | exps (EXP EXP2 EXP10 POW10) |
6603 | /* sqrt(expN(x)) -> expN(x*0.5). */ | |
6604 | (simplify | |
6605 | (sqrts (exps @0)) | |
6606 | (exps (mult @0 { build_real (type, dconsthalf); }))) | |
6607 | /* cbrt(expN(x)) -> expN(x/3). */ | |
6608 | (simplify | |
6609 | (cbrts (exps @0)) | |
b4838d77 RS |
6610 | (exps (mult @0 { build_real_truncate (type, dconst_third ()); }))) |
6611 | /* pow(expN(x), y) -> expN(x*y). */ | |
6612 | (simplify | |
6613 | (pows (exps @0) @1) | |
6614 | (exps (mult @0 @1)))) | |
cfed37a0 RS |
6615 | |
6616 | /* tan(atan(x)) -> x. */ | |
6617 | (for tans (TAN) | |
6618 | atans (ATAN) | |
6619 | (simplify | |
6620 | (tans (atans @0)) | |
6621 | @0))) | |
53f3cd25 | 6622 | |
121ef08b GB |
6623 | /* Simplify sin(atan(x)) -> x / sqrt(x*x + 1). */ |
6624 | (for sins (SIN) | |
6625 | atans (ATAN) | |
6626 | sqrts (SQRT) | |
6627 | copysigns (COPYSIGN) | |
6628 | (simplify | |
6629 | (sins (atans:s @0)) | |
6630 | (with | |
6631 | { | |
6632 | REAL_VALUE_TYPE r_cst; | |
6633 | build_sinatan_real (&r_cst, type); | |
6634 | tree t_cst = build_real (type, r_cst); | |
6635 | tree t_one = build_one_cst (type); | |
6636 | } | |
6637 | (if (SCALAR_FLOAT_TYPE_P (type)) | |
5f054b17 | 6638 | (cond (lt (abs @0) { t_cst; }) |
121ef08b GB |
6639 | (rdiv @0 (sqrts (plus (mult @0 @0) { t_one; }))) |
6640 | (copysigns { t_one; } @0)))))) | |
6641 | ||
6642 | /* Simplify cos(atan(x)) -> 1 / sqrt(x*x + 1). */ | |
6643 | (for coss (COS) | |
6644 | atans (ATAN) | |
6645 | sqrts (SQRT) | |
6646 | copysigns (COPYSIGN) | |
6647 | (simplify | |
6648 | (coss (atans:s @0)) | |
6649 | (with | |
6650 | { | |
6651 | REAL_VALUE_TYPE r_cst; | |
6652 | build_sinatan_real (&r_cst, type); | |
6653 | tree t_cst = build_real (type, r_cst); | |
6654 | tree t_one = build_one_cst (type); | |
6655 | tree t_zero = build_zero_cst (type); | |
6656 | } | |
6657 | (if (SCALAR_FLOAT_TYPE_P (type)) | |
5f054b17 | 6658 | (cond (lt (abs @0) { t_cst; }) |
121ef08b GB |
6659 | (rdiv { t_one; } (sqrts (plus (mult @0 @0) { t_one; }))) |
6660 | (copysigns { t_zero; } @0)))))) | |
6661 | ||
4aff6d17 GB |
6662 | (if (!flag_errno_math) |
6663 | /* Simplify sinh(atanh(x)) -> x / sqrt((1 - x)*(1 + x)). */ | |
6664 | (for sinhs (SINH) | |
6665 | atanhs (ATANH) | |
6666 | sqrts (SQRT) | |
6667 | (simplify | |
6668 | (sinhs (atanhs:s @0)) | |
6669 | (with { tree t_one = build_one_cst (type); } | |
6670 | (rdiv @0 (sqrts (mult (minus { t_one; } @0) (plus { t_one; } @0))))))) | |
6671 | ||
6672 | /* Simplify cosh(atanh(x)) -> 1 / sqrt((1 - x)*(1 + x)) */ | |
6673 | (for coshs (COSH) | |
6674 | atanhs (ATANH) | |
6675 | sqrts (SQRT) | |
6676 | (simplify | |
6677 | (coshs (atanhs:s @0)) | |
6678 | (with { tree t_one = build_one_cst (type); } | |
6679 | (rdiv { t_one; } (sqrts (mult (minus { t_one; } @0) (plus { t_one; } @0)))))))) | |
6680 | ||
abcc43f5 RS |
6681 | /* cabs(x+0i) or cabs(0+xi) -> abs(x). */ |
6682 | (simplify | |
e04d2a35 | 6683 | (CABS (complex:C @0 real_zerop@1)) |
abcc43f5 RS |
6684 | (abs @0)) |
6685 | ||
67dbe582 | 6686 | /* trunc(trunc(x)) -> trunc(x), etc. */ |
c6cfa2bf | 6687 | (for fns (TRUNC_ALL FLOOR_ALL CEIL_ALL ROUND_ALL NEARBYINT_ALL RINT_ALL) |
67dbe582 RS |
6688 | (simplify |
6689 | (fns (fns @0)) | |
6690 | (fns @0))) | |
6691 | /* f(x) -> x if x is integer valued and f does nothing for such values. */ | |
c6cfa2bf | 6692 | (for fns (TRUNC_ALL FLOOR_ALL CEIL_ALL ROUND_ALL NEARBYINT_ALL RINT_ALL) |
67dbe582 RS |
6693 | (simplify |
6694 | (fns integer_valued_real_p@0) | |
6695 | @0)) | |
67dbe582 | 6696 | |
4d7836c4 RS |
6697 | /* hypot(x,0) and hypot(0,x) -> abs(x). */ |
6698 | (simplify | |
c9e926ce | 6699 | (HYPOT:c @0 real_zerop@1) |
4d7836c4 RS |
6700 | (abs @0)) |
6701 | ||
b4838d77 RS |
6702 | /* pow(1,x) -> 1. */ |
6703 | (simplify | |
6704 | (POW real_onep@0 @1) | |
6705 | @0) | |
6706 | ||
461e4145 RS |
6707 | (simplify |
6708 | /* copysign(x,x) -> x. */ | |
c6cfa2bf | 6709 | (COPYSIGN_ALL @0 @0) |
461e4145 RS |
6710 | @0) |
6711 | ||
bb5e8952 JJ |
6712 | (simplify |
6713 | /* copysign(x,-x) -> -x. */ | |
6714 | (COPYSIGN_ALL @0 (negate@1 @0)) | |
6715 | @1) | |
6716 | ||
461e4145 RS |
6717 | (simplify |
6718 | /* copysign(x,y) -> fabs(x) if y is nonnegative. */ | |
c6cfa2bf | 6719 | (COPYSIGN_ALL @0 tree_expr_nonnegative_p@1) |
461e4145 RS |
6720 | (abs @0)) |
6721 | ||
86c0733f RS |
6722 | (for scale (LDEXP SCALBN SCALBLN) |
6723 | /* ldexp(0, x) -> 0. */ | |
6724 | (simplify | |
6725 | (scale real_zerop@0 @1) | |
6726 | @0) | |
6727 | /* ldexp(x, 0) -> x. */ | |
6728 | (simplify | |
6729 | (scale @0 integer_zerop@1) | |
6730 | @0) | |
6731 | /* ldexp(x, y) -> x if x is +-Inf or NaN. */ | |
6732 | (simplify | |
6733 | (scale REAL_CST@0 @1) | |
6734 | (if (!real_isfinite (TREE_REAL_CST_PTR (@0))) | |
6735 | @0))) | |
6736 | ||
53f3cd25 RS |
6737 | /* Canonicalization of sequences of math builtins. These rules represent |
6738 | IL simplifications but are not necessarily optimizations. | |
6739 | ||
6740 | The sincos pass is responsible for picking "optimal" implementations | |
6741 | of math builtins, which may be more complicated and can sometimes go | |
6742 | the other way, e.g. converting pow into a sequence of sqrts. | |
6743 | We only want to do these canonicalizations before the pass has run. */ | |
6744 | ||
6745 | (if (flag_unsafe_math_optimizations && canonicalize_math_p ()) | |
6746 | /* Simplify tan(x) * cos(x) -> sin(x). */ | |
6747 | (simplify | |
6748 | (mult:c (TAN:s @0) (COS:s @0)) | |
6749 | (SIN @0)) | |
6750 | ||
6751 | /* Simplify x * pow(x,c) -> pow(x,c+1). */ | |
6752 | (simplify | |
de3fbea3 | 6753 | (mult:c @0 (POW:s @0 REAL_CST@1)) |
53f3cd25 RS |
6754 | (if (!TREE_OVERFLOW (@1)) |
6755 | (POW @0 (plus @1 { build_one_cst (type); })))) | |
6756 | ||
6757 | /* Simplify sin(x) / cos(x) -> tan(x). */ | |
6758 | (simplify | |
6759 | (rdiv (SIN:s @0) (COS:s @0)) | |
6760 | (TAN @0)) | |
6761 | ||
2066f795 RT |
6762 | /* Simplify sinh(x) / cosh(x) -> tanh(x). */ |
6763 | (simplify | |
6764 | (rdiv (SINH:s @0) (COSH:s @0)) | |
6765 | (TANH @0)) | |
6766 | ||
29e304fd VG |
6767 | /* Simplify tanh (x) / sinh (x) -> 1.0 / cosh (x). */ |
6768 | (simplify | |
6769 | (rdiv (TANH:s @0) (SINH:s @0)) | |
6770 | (rdiv {build_one_cst (type);} (COSH @0))) | |
6771 | ||
53f3cd25 RS |
6772 | /* Simplify cos(x) / sin(x) -> 1 / tan(x). */ |
6773 | (simplify | |
6774 | (rdiv (COS:s @0) (SIN:s @0)) | |
6775 | (rdiv { build_one_cst (type); } (TAN @0))) | |
6776 | ||
6777 | /* Simplify sin(x) / tan(x) -> cos(x). */ | |
6778 | (simplify | |
6779 | (rdiv (SIN:s @0) (TAN:s @0)) | |
6780 | (if (! HONOR_NANS (@0) | |
6781 | && ! HONOR_INFINITIES (@0)) | |
c9e926ce | 6782 | (COS @0))) |
53f3cd25 RS |
6783 | |
6784 | /* Simplify tan(x) / sin(x) -> 1.0 / cos(x). */ | |
6785 | (simplify | |
6786 | (rdiv (TAN:s @0) (SIN:s @0)) | |
6787 | (if (! HONOR_NANS (@0) | |
6788 | && ! HONOR_INFINITIES (@0)) | |
6789 | (rdiv { build_one_cst (type); } (COS @0)))) | |
6790 | ||
6791 | /* Simplify pow(x,y) * pow(x,z) -> pow(x,y+z). */ | |
6792 | (simplify | |
6793 | (mult (POW:s @0 @1) (POW:s @0 @2)) | |
6794 | (POW @0 (plus @1 @2))) | |
6795 | ||
6796 | /* Simplify pow(x,y) * pow(z,y) -> pow(x*z,y). */ | |
6797 | (simplify | |
6798 | (mult (POW:s @0 @1) (POW:s @2 @1)) | |
6799 | (POW (mult @0 @2) @1)) | |
6800 | ||
de3fbea3 RB |
6801 | /* Simplify powi(x,y) * powi(z,y) -> powi(x*z,y). */ |
6802 | (simplify | |
6803 | (mult (POWI:s @0 @1) (POWI:s @2 @1)) | |
6804 | (POWI (mult @0 @2) @1)) | |
6805 | ||
53f3cd25 RS |
6806 | /* Simplify pow(x,c) / x -> pow(x,c-1). */ |
6807 | (simplify | |
6808 | (rdiv (POW:s @0 REAL_CST@1) @0) | |
6809 | (if (!TREE_OVERFLOW (@1)) | |
6810 | (POW @0 (minus @1 { build_one_cst (type); })))) | |
6811 | ||
6812 | /* Simplify x / pow (y,z) -> x * pow(y,-z). */ | |
6813 | (simplify | |
6814 | (rdiv @0 (POW:s @1 @2)) | |
6815 | (mult @0 (POW @1 (negate @2)))) | |
6816 | ||
6817 | (for sqrts (SQRT) | |
6818 | cbrts (CBRT) | |
6819 | pows (POW) | |
6820 | /* sqrt(sqrt(x)) -> pow(x,1/4). */ | |
6821 | (simplify | |
6822 | (sqrts (sqrts @0)) | |
6823 | (pows @0 { build_real (type, dconst_quarter ()); })) | |
6824 | /* sqrt(cbrt(x)) -> pow(x,1/6). */ | |
6825 | (simplify | |
6826 | (sqrts (cbrts @0)) | |
6827 | (pows @0 { build_real_truncate (type, dconst_sixth ()); })) | |
6828 | /* cbrt(sqrt(x)) -> pow(x,1/6). */ | |
6829 | (simplify | |
6830 | (cbrts (sqrts @0)) | |
6831 | (pows @0 { build_real_truncate (type, dconst_sixth ()); })) | |
6832 | /* cbrt(cbrt(x)) -> pow(x,1/9), iff x is nonnegative. */ | |
6833 | (simplify | |
6834 | (cbrts (cbrts tree_expr_nonnegative_p@0)) | |
6835 | (pows @0 { build_real_truncate (type, dconst_ninth ()); })) | |
6836 | /* sqrt(pow(x,y)) -> pow(|x|,y*0.5). */ | |
6837 | (simplify | |
6838 | (sqrts (pows @0 @1)) | |
6839 | (pows (abs @0) (mult @1 { build_real (type, dconsthalf); }))) | |
6840 | /* cbrt(pow(x,y)) -> pow(x,y/3), iff x is nonnegative. */ | |
6841 | (simplify | |
6842 | (cbrts (pows tree_expr_nonnegative_p@0 @1)) | |
b4838d77 RS |
6843 | (pows @0 (mult @1 { build_real_truncate (type, dconst_third ()); }))) |
6844 | /* pow(sqrt(x),y) -> pow(x,y*0.5). */ | |
6845 | (simplify | |
6846 | (pows (sqrts @0) @1) | |
6847 | (pows @0 (mult @1 { build_real (type, dconsthalf); }))) | |
6848 | /* pow(cbrt(x),y) -> pow(x,y/3) iff x is nonnegative. */ | |
6849 | (simplify | |
6850 | (pows (cbrts tree_expr_nonnegative_p@0) @1) | |
6851 | (pows @0 (mult @1 { build_real_truncate (type, dconst_third ()); }))) | |
6852 | /* pow(pow(x,y),z) -> pow(x,y*z) iff x is nonnegative. */ | |
6853 | (simplify | |
6854 | (pows (pows tree_expr_nonnegative_p@0 @1) @2) | |
6855 | (pows @0 (mult @1 @2)))) | |
abcc43f5 RS |
6856 | |
6857 | /* cabs(x+xi) -> fabs(x)*sqrt(2). */ | |
6858 | (simplify | |
6859 | (CABS (complex @0 @0)) | |
96285749 RS |
6860 | (mult (abs @0) { build_real_truncate (type, dconst_sqrt2 ()); })) |
6861 | ||
4d7836c4 RS |
6862 | /* hypot(x,x) -> fabs(x)*sqrt(2). */ |
6863 | (simplify | |
6864 | (HYPOT @0 @0) | |
6865 | (mult (abs @0) { build_real_truncate (type, dconst_sqrt2 ()); })) | |
6866 | ||
96285749 RS |
6867 | /* cexp(x+yi) -> exp(x)*cexpi(y). */ |
6868 | (for cexps (CEXP) | |
6869 | exps (EXP) | |
6870 | cexpis (CEXPI) | |
6871 | (simplify | |
6872 | (cexps compositional_complex@0) | |
bae974e6 | 6873 | (if (targetm.libc_has_function (function_c99_math_complex, TREE_TYPE (@0))) |
96285749 RS |
6874 | (complex |
6875 | (mult (exps@1 (realpart @0)) (realpart (cexpis:type@2 (imagpart @0)))) | |
6876 | (mult @1 (imagpart @2))))))) | |
e18c1d66 | 6877 | |
67dbe582 RS |
6878 | (if (canonicalize_math_p ()) |
6879 | /* floor(x) -> trunc(x) if x is nonnegative. */ | |
c6cfa2bf MM |
6880 | (for floors (FLOOR_ALL) |
6881 | truncs (TRUNC_ALL) | |
67dbe582 RS |
6882 | (simplify |
6883 | (floors tree_expr_nonnegative_p@0) | |
6884 | (truncs @0)))) | |
6885 | ||
6886 | (match double_value_p | |
6887 | @0 | |
6888 | (if (TYPE_MAIN_VARIANT (TREE_TYPE (@0)) == double_type_node))) | |
6889 | (for froms (BUILT_IN_TRUNCL | |
6890 | BUILT_IN_FLOORL | |
6891 | BUILT_IN_CEILL | |
6892 | BUILT_IN_ROUNDL | |
6893 | BUILT_IN_NEARBYINTL | |
6894 | BUILT_IN_RINTL) | |
6895 | tos (BUILT_IN_TRUNC | |
6896 | BUILT_IN_FLOOR | |
6897 | BUILT_IN_CEIL | |
6898 | BUILT_IN_ROUND | |
6899 | BUILT_IN_NEARBYINT | |
6900 | BUILT_IN_RINT) | |
6901 | /* truncl(extend(x)) -> extend(trunc(x)), etc., if x is a double. */ | |
6902 | (if (optimize && canonicalize_math_p ()) | |
6903 | (simplify | |
6904 | (froms (convert double_value_p@0)) | |
6905 | (convert (tos @0))))) | |
6906 | ||
6907 | (match float_value_p | |
6908 | @0 | |
6909 | (if (TYPE_MAIN_VARIANT (TREE_TYPE (@0)) == float_type_node))) | |
6910 | (for froms (BUILT_IN_TRUNCL BUILT_IN_TRUNC | |
6911 | BUILT_IN_FLOORL BUILT_IN_FLOOR | |
6912 | BUILT_IN_CEILL BUILT_IN_CEIL | |
6913 | BUILT_IN_ROUNDL BUILT_IN_ROUND | |
6914 | BUILT_IN_NEARBYINTL BUILT_IN_NEARBYINT | |
6915 | BUILT_IN_RINTL BUILT_IN_RINT) | |
6916 | tos (BUILT_IN_TRUNCF BUILT_IN_TRUNCF | |
6917 | BUILT_IN_FLOORF BUILT_IN_FLOORF | |
6918 | BUILT_IN_CEILF BUILT_IN_CEILF | |
6919 | BUILT_IN_ROUNDF BUILT_IN_ROUNDF | |
6920 | BUILT_IN_NEARBYINTF BUILT_IN_NEARBYINTF | |
6921 | BUILT_IN_RINTF BUILT_IN_RINTF) | |
6922 | /* truncl(extend(x)) and trunc(extend(x)) -> extend(truncf(x)), etc., | |
6923 | if x is a float. */ | |
5dac7dbd | 6924 | (if (optimize && canonicalize_math_p () |
bae974e6 | 6925 | && targetm.libc_has_function (function_c99_misc, NULL_TREE)) |
67dbe582 RS |
6926 | (simplify |
6927 | (froms (convert float_value_p@0)) | |
6928 | (convert (tos @0))))) | |
6929 | ||
61319646 | 6930 | #if GIMPLE |
6931 | (match float16_value_p | |
6932 | @0 | |
6933 | (if (TYPE_MAIN_VARIANT (TREE_TYPE (@0)) == float16_type_node))) | |
6934 | (for froms (BUILT_IN_TRUNCL BUILT_IN_TRUNC BUILT_IN_TRUNCF | |
6935 | BUILT_IN_FLOORL BUILT_IN_FLOOR BUILT_IN_FLOORF | |
6936 | BUILT_IN_CEILL BUILT_IN_CEIL BUILT_IN_CEILF | |
6937 | BUILT_IN_ROUNDEVENL BUILT_IN_ROUNDEVEN BUILT_IN_ROUNDEVENF | |
6938 | BUILT_IN_ROUNDL BUILT_IN_ROUND BUILT_IN_ROUNDF | |
6939 | BUILT_IN_NEARBYINTL BUILT_IN_NEARBYINT BUILT_IN_NEARBYINTF | |
1a07bc9c | 6940 | BUILT_IN_RINTL BUILT_IN_RINT BUILT_IN_RINTF |
6941 | BUILT_IN_SQRTL BUILT_IN_SQRT BUILT_IN_SQRTF) | |
61319646 | 6942 | tos (IFN_TRUNC IFN_TRUNC IFN_TRUNC |
6943 | IFN_FLOOR IFN_FLOOR IFN_FLOOR | |
6944 | IFN_CEIL IFN_CEIL IFN_CEIL | |
6945 | IFN_ROUNDEVEN IFN_ROUNDEVEN IFN_ROUNDEVEN | |
6946 | IFN_ROUND IFN_ROUND IFN_ROUND | |
6947 | IFN_NEARBYINT IFN_NEARBYINT IFN_NEARBYINT | |
1a07bc9c | 6948 | IFN_RINT IFN_RINT IFN_RINT |
6949 | IFN_SQRT IFN_SQRT IFN_SQRT) | |
61319646 | 6950 | /* (_Float16) round ((doube) x) -> __built_in_roundf16 (x), etc., |
6951 | if x is a _Float16. */ | |
6952 | (simplify | |
6953 | (convert (froms (convert float16_value_p@0))) | |
6954 | (if (optimize | |
6955 | && types_match (type, TREE_TYPE (@0)) | |
6956 | && direct_internal_fn_supported_p (as_internal_fn (tos), | |
6957 | type, OPTIMIZE_FOR_BOTH)) | |
6958 | (tos @0)))) | |
22ce7382 | 6959 | |
6960 | /* Simplify (trunc)copysign ((extend)x, (extend)y) to copysignf (x, y), | |
6961 | x,y is float value, similar for _Float16/double. */ | |
6962 | (for copysigns (COPYSIGN_ALL) | |
6963 | (simplify | |
6964 | (convert (copysigns (convert@2 @0) (convert @1))) | |
6965 | (if (optimize | |
a1f7ead0 | 6966 | && !HONOR_SNANS (@2) |
22ce7382 | 6967 | && types_match (type, TREE_TYPE (@0)) |
6968 | && types_match (type, TREE_TYPE (@1)) | |
6969 | && TYPE_PRECISION (type) < TYPE_PRECISION (TREE_TYPE (@2)) | |
6970 | && direct_internal_fn_supported_p (IFN_COPYSIGN, | |
6971 | type, OPTIMIZE_FOR_BOTH)) | |
6972 | (IFN_COPYSIGN @0 @1)))) | |
6973 | ||
2ad1e808 | 6974 | (for froms (BUILT_IN_FMAF BUILT_IN_FMA BUILT_IN_FMAL) |
6975 | tos (IFN_FMA IFN_FMA IFN_FMA) | |
6976 | (simplify | |
6977 | (convert (froms (convert@3 @0) (convert @1) (convert @2))) | |
6978 | (if (flag_unsafe_math_optimizations | |
6979 | && optimize | |
6980 | && FLOAT_TYPE_P (type) | |
6981 | && FLOAT_TYPE_P (TREE_TYPE (@3)) | |
6982 | && types_match (type, TREE_TYPE (@0)) | |
6983 | && types_match (type, TREE_TYPE (@1)) | |
6984 | && types_match (type, TREE_TYPE (@2)) | |
6985 | && TYPE_PRECISION (type) < TYPE_PRECISION (TREE_TYPE (@3)) | |
6986 | && direct_internal_fn_supported_p (as_internal_fn (tos), | |
6987 | type, OPTIMIZE_FOR_BOTH)) | |
6988 | (tos @0 @1 @2)))) | |
b879d40a | 6989 | |
6990 | (for maxmin (max min) | |
6991 | (simplify | |
6992 | (convert (maxmin (convert@2 @0) (convert @1))) | |
6993 | (if (optimize | |
6994 | && FLOAT_TYPE_P (type) | |
6995 | && FLOAT_TYPE_P (TREE_TYPE (@2)) | |
6996 | && types_match (type, TREE_TYPE (@0)) | |
6997 | && types_match (type, TREE_TYPE (@1)) | |
6998 | && element_precision (type) < element_precision (TREE_TYPE (@2))) | |
6999 | (maxmin @0 @1)))) | |
61319646 | 7000 | #endif |
7001 | ||
543a9bcd RS |
7002 | (for froms (XFLOORL XCEILL XROUNDL XRINTL) |
7003 | tos (XFLOOR XCEIL XROUND XRINT) | |
7004 | /* llfloorl(extend(x)) -> llfloor(x), etc., if x is a double. */ | |
7005 | (if (optimize && canonicalize_math_p ()) | |
7006 | (simplify | |
7007 | (froms (convert double_value_p@0)) | |
7008 | (tos @0)))) | |
7009 | ||
7010 | (for froms (XFLOORL XCEILL XROUNDL XRINTL | |
7011 | XFLOOR XCEIL XROUND XRINT) | |
7012 | tos (XFLOORF XCEILF XROUNDF XRINTF) | |
7013 | /* llfloorl(extend(x)) and llfloor(extend(x)) -> llfloorf(x), etc., | |
7014 | if x is a float. */ | |
7015 | (if (optimize && canonicalize_math_p ()) | |
7016 | (simplify | |
7017 | (froms (convert float_value_p@0)) | |
7018 | (tos @0)))) | |
7019 | ||
7020 | (if (canonicalize_math_p ()) | |
7021 | /* xfloor(x) -> fix_trunc(x) if x is nonnegative. */ | |
7022 | (for floors (IFLOOR LFLOOR LLFLOOR) | |
7023 | (simplify | |
7024 | (floors tree_expr_nonnegative_p@0) | |
7025 | (fix_trunc @0)))) | |
7026 | ||
7027 | (if (canonicalize_math_p ()) | |
7028 | /* xfloor(x) -> fix_trunc(x), etc., if x is integer valued. */ | |
7029 | (for fns (IFLOOR LFLOOR LLFLOOR | |
7030 | ICEIL LCEIL LLCEIL | |
7031 | IROUND LROUND LLROUND) | |
7032 | (simplify | |
7033 | (fns integer_valued_real_p@0) | |
7034 | (fix_trunc @0))) | |
7035 | (if (!flag_errno_math) | |
7036 | /* xrint(x) -> fix_trunc(x), etc., if x is integer valued. */ | |
7037 | (for rints (IRINT LRINT LLRINT) | |
7038 | (simplify | |
7039 | (rints integer_valued_real_p@0) | |
7040 | (fix_trunc @0))))) | |
7041 | ||
7042 | (if (canonicalize_math_p ()) | |
7043 | (for ifn (IFLOOR ICEIL IROUND IRINT) | |
7044 | lfn (LFLOOR LCEIL LROUND LRINT) | |
7045 | llfn (LLFLOOR LLCEIL LLROUND LLRINT) | |
7046 | /* Canonicalize iround (x) to lround (x) on ILP32 targets where | |
7047 | sizeof (int) == sizeof (long). */ | |
7048 | (if (TYPE_PRECISION (integer_type_node) | |
7049 | == TYPE_PRECISION (long_integer_type_node)) | |
7050 | (simplify | |
7051 | (ifn @0) | |
7052 | (lfn:long_integer_type_node @0))) | |
7053 | /* Canonicalize llround (x) to lround (x) on LP64 targets where | |
7054 | sizeof (long long) == sizeof (long). */ | |
7055 | (if (TYPE_PRECISION (long_long_integer_type_node) | |
7056 | == TYPE_PRECISION (long_integer_type_node)) | |
7057 | (simplify | |
7058 | (llfn @0) | |
7059 | (lfn:long_integer_type_node @0))))) | |
7060 | ||
92c52eab RS |
7061 | /* cproj(x) -> x if we're ignoring infinities. */ |
7062 | (simplify | |
7063 | (CPROJ @0) | |
7064 | (if (!HONOR_INFINITIES (type)) | |
7065 | @0)) | |
7066 | ||
4534c203 RB |
7067 | /* If the real part is inf and the imag part is known to be |
7068 | nonnegative, return (inf + 0i). */ | |
7069 | (simplify | |
7070 | (CPROJ (complex REAL_CST@0 tree_expr_nonnegative_p@1)) | |
7071 | (if (real_isinf (TREE_REAL_CST_PTR (@0))) | |
92c52eab RS |
7072 | { build_complex_inf (type, false); })) |
7073 | ||
4534c203 RB |
7074 | /* If the imag part is inf, return (inf+I*copysign(0,imag)). */ |
7075 | (simplify | |
7076 | (CPROJ (complex @0 REAL_CST@1)) | |
7077 | (if (real_isinf (TREE_REAL_CST_PTR (@1))) | |
92c52eab | 7078 | { build_complex_inf (type, TREE_REAL_CST_PTR (@1)->sign); })) |
4534c203 | 7079 | |
b4838d77 RS |
7080 | (for pows (POW) |
7081 | sqrts (SQRT) | |
7082 | cbrts (CBRT) | |
7083 | (simplify | |
7084 | (pows @0 REAL_CST@1) | |
7085 | (with { | |
7086 | const REAL_VALUE_TYPE *value = TREE_REAL_CST_PTR (@1); | |
7087 | REAL_VALUE_TYPE tmp; | |
7088 | } | |
7089 | (switch | |
7090 | /* pow(x,0) -> 1. */ | |
7091 | (if (real_equal (value, &dconst0)) | |
7092 | { build_real (type, dconst1); }) | |
7093 | /* pow(x,1) -> x. */ | |
7094 | (if (real_equal (value, &dconst1)) | |
7095 | @0) | |
7096 | /* pow(x,-1) -> 1/x. */ | |
7097 | (if (real_equal (value, &dconstm1)) | |
7098 | (rdiv { build_real (type, dconst1); } @0)) | |
7099 | /* pow(x,0.5) -> sqrt(x). */ | |
7100 | (if (flag_unsafe_math_optimizations | |
7101 | && canonicalize_math_p () | |
7102 | && real_equal (value, &dconsthalf)) | |
7103 | (sqrts @0)) | |
7104 | /* pow(x,1/3) -> cbrt(x). */ | |
7105 | (if (flag_unsafe_math_optimizations | |
7106 | && canonicalize_math_p () | |
7107 | && (tmp = real_value_truncate (TYPE_MODE (type), dconst_third ()), | |
7108 | real_equal (value, &tmp))) | |
7109 | (cbrts @0)))))) | |
4534c203 | 7110 | |
5ddc84ca RS |
7111 | /* powi(1,x) -> 1. */ |
7112 | (simplify | |
7113 | (POWI real_onep@0 @1) | |
7114 | @0) | |
7115 | ||
7116 | (simplify | |
7117 | (POWI @0 INTEGER_CST@1) | |
7118 | (switch | |
7119 | /* powi(x,0) -> 1. */ | |
8e6cdc90 | 7120 | (if (wi::to_wide (@1) == 0) |
5ddc84ca RS |
7121 | { build_real (type, dconst1); }) |
7122 | /* powi(x,1) -> x. */ | |
8e6cdc90 | 7123 | (if (wi::to_wide (@1) == 1) |
5ddc84ca RS |
7124 | @0) |
7125 | /* powi(x,-1) -> 1/x. */ | |
8e6cdc90 | 7126 | (if (wi::to_wide (@1) == -1) |
5ddc84ca RS |
7127 | (rdiv { build_real (type, dconst1); } @0)))) |
7128 | ||
03cc70b5 | 7129 | /* Narrowing of arithmetic and logical operations. |
be144838 JL |
7130 | |
7131 | These are conceptually similar to the transformations performed for | |
7132 | the C/C++ front-ends by shorten_binary_op and shorten_compare. Long | |
7133 | term we want to move all that code out of the front-ends into here. */ | |
7134 | ||
8f5331b2 TC |
7135 | /* Convert (outertype)((innertype0)a+(innertype1)b) |
7136 | into ((newtype)a+(newtype)b) where newtype | |
7137 | is the widest mode from all of these. */ | |
7138 | (for op (plus minus mult rdiv) | |
7139 | (simplify | |
7140 | (convert (op:s@0 (convert1?@3 @1) (convert2?@4 @2))) | |
7141 | /* If we have a narrowing conversion of an arithmetic operation where | |
7142 | both operands are widening conversions from the same type as the outer | |
7143 | narrowing conversion. Then convert the innermost operands to a | |
7144 | suitable unsigned type (to avoid introducing undefined behavior), | |
7145 | perform the operation and convert the result to the desired type. */ | |
7146 | (if (INTEGRAL_TYPE_P (type) | |
7147 | && op != MULT_EXPR | |
7148 | && op != RDIV_EXPR | |
7149 | /* We check for type compatibility between @0 and @1 below, | |
7150 | so there's no need to check that @2/@4 are integral types. */ | |
7151 | && INTEGRAL_TYPE_P (TREE_TYPE (@1)) | |
7152 | && INTEGRAL_TYPE_P (TREE_TYPE (@3)) | |
7153 | /* The precision of the type of each operand must match the | |
7154 | precision of the mode of each operand, similarly for the | |
7155 | result. */ | |
7156 | && type_has_mode_precision_p (TREE_TYPE (@1)) | |
7157 | && type_has_mode_precision_p (TREE_TYPE (@2)) | |
7158 | && type_has_mode_precision_p (type) | |
7159 | /* The inner conversion must be a widening conversion. */ | |
7160 | && TYPE_PRECISION (TREE_TYPE (@3)) > TYPE_PRECISION (TREE_TYPE (@1)) | |
7161 | && types_match (@1, type) | |
7162 | && (types_match (@1, @2) | |
7163 | /* Or the second operand is const integer or converted const | |
7164 | integer from valueize. */ | |
298b0db7 | 7165 | || poly_int_tree_p (@4))) |
8f5331b2 TC |
7166 | (if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1))) |
7167 | (op @1 (convert @2)) | |
7168 | (with { tree utype = unsigned_type_for (TREE_TYPE (@1)); } | |
7169 | (convert (op (convert:utype @1) | |
7170 | (convert:utype @2))))) | |
7171 | (if (FLOAT_TYPE_P (type) | |
7172 | && DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@0)) | |
7173 | == DECIMAL_FLOAT_TYPE_P (type)) | |
7174 | (with { tree arg0 = strip_float_extensions (@1); | |
7175 | tree arg1 = strip_float_extensions (@2); | |
7176 | tree itype = TREE_TYPE (@0); | |
7177 | tree ty1 = TREE_TYPE (arg0); | |
7178 | tree ty2 = TREE_TYPE (arg1); | |
7179 | enum tree_code code = TREE_CODE (itype); } | |
7180 | (if (FLOAT_TYPE_P (ty1) | |
7181 | && FLOAT_TYPE_P (ty2)) | |
7182 | (with { tree newtype = type; | |
7183 | if (TYPE_MODE (ty1) == SDmode | |
7184 | || TYPE_MODE (ty2) == SDmode | |
7185 | || TYPE_MODE (type) == SDmode) | |
7186 | newtype = dfloat32_type_node; | |
7187 | if (TYPE_MODE (ty1) == DDmode | |
7188 | || TYPE_MODE (ty2) == DDmode | |
7189 | || TYPE_MODE (type) == DDmode) | |
7190 | newtype = dfloat64_type_node; | |
7191 | if (TYPE_MODE (ty1) == TDmode | |
7192 | || TYPE_MODE (ty2) == TDmode | |
7193 | || TYPE_MODE (type) == TDmode) | |
7194 | newtype = dfloat128_type_node; } | |
7195 | (if ((newtype == dfloat32_type_node | |
7196 | || newtype == dfloat64_type_node | |
7197 | || newtype == dfloat128_type_node) | |
7198 | && newtype == type | |
7199 | && types_match (newtype, type)) | |
7200 | (op (convert:newtype @1) (convert:newtype @2)) | |
dc5b1191 | 7201 | (with { if (TYPE_PRECISION (ty1) > TYPE_PRECISION (newtype)) |
8f5331b2 TC |
7202 | newtype = ty1; |
7203 | if (TYPE_PRECISION (ty2) > TYPE_PRECISION (newtype)) | |
dc5b1191 | 7204 | newtype = ty2; } |
8f5331b2 TC |
7205 | /* Sometimes this transformation is safe (cannot |
7206 | change results through affecting double rounding | |
7207 | cases) and sometimes it is not. If NEWTYPE is | |
7208 | wider than TYPE, e.g. (float)((long double)double | |
7209 | + (long double)double) converted to | |
7210 | (float)(double + double), the transformation is | |
7211 | unsafe regardless of the details of the types | |
7212 | involved; double rounding can arise if the result | |
7213 | of NEWTYPE arithmetic is a NEWTYPE value half way | |
7214 | between two representable TYPE values but the | |
7215 | exact value is sufficiently different (in the | |
7216 | right direction) for this difference to be | |
7217 | visible in ITYPE arithmetic. If NEWTYPE is the | |
7218 | same as TYPE, however, the transformation may be | |
7219 | safe depending on the types involved: it is safe | |
7220 | if the ITYPE has strictly more than twice as many | |
7221 | mantissa bits as TYPE, can represent infinities | |
7222 | and NaNs if the TYPE can, and has sufficient | |
7223 | exponent range for the product or ratio of two | |
7224 | values representable in the TYPE to be within the | |
7225 | range of normal values of ITYPE. */ | |
7226 | (if (TYPE_PRECISION (newtype) < TYPE_PRECISION (itype) | |
7227 | && (flag_unsafe_math_optimizations | |
7228 | || (TYPE_PRECISION (newtype) == TYPE_PRECISION (type) | |
7229 | && real_can_shorten_arithmetic (TYPE_MODE (itype), | |
7230 | TYPE_MODE (type)) | |
7231 | && !excess_precision_type (newtype))) | |
7232 | && !types_match (itype, newtype)) | |
7233 | (convert:type (op (convert:newtype @1) | |
7234 | (convert:newtype @2))) | |
7235 | )))) ) | |
7236 | )) | |
7237 | ))) | |
48451e8f JL |
7238 | |
7239 | /* This is another case of narrowing, specifically when there's an outer | |
7240 | BIT_AND_EXPR which masks off bits outside the type of the innermost | |
7241 | operands. Like the previous case we have to convert the operands | |
9c582551 | 7242 | to unsigned types to avoid introducing undefined behavior for the |
48451e8f JL |
7243 | arithmetic operation. */ |
7244 | (for op (minus plus) | |
8fdc6c67 RB |
7245 | (simplify |
7246 | (bit_and (op:s (convert@2 @0) (convert@3 @1)) INTEGER_CST@4) | |
7247 | (if (INTEGRAL_TYPE_P (type) | |
7248 | /* We check for type compatibility between @0 and @1 below, | |
7249 | so there's no need to check that @1/@3 are integral types. */ | |
7250 | && INTEGRAL_TYPE_P (TREE_TYPE (@0)) | |
7251 | && INTEGRAL_TYPE_P (TREE_TYPE (@2)) | |
7252 | /* The precision of the type of each operand must match the | |
7253 | precision of the mode of each operand, similarly for the | |
7254 | result. */ | |
2be65d9e RS |
7255 | && type_has_mode_precision_p (TREE_TYPE (@0)) |
7256 | && type_has_mode_precision_p (TREE_TYPE (@1)) | |
7257 | && type_has_mode_precision_p (type) | |
8fdc6c67 RB |
7258 | /* The inner conversion must be a widening conversion. */ |
7259 | && TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (TREE_TYPE (@0)) | |
7260 | && types_match (@0, @1) | |
7261 | && (tree_int_cst_min_precision (@4, TYPE_SIGN (TREE_TYPE (@0))) | |
7262 | <= TYPE_PRECISION (TREE_TYPE (@0))) | |
8e6cdc90 RS |
7263 | && (wi::to_wide (@4) |
7264 | & wi::mask (TYPE_PRECISION (TREE_TYPE (@0)), | |
7265 | true, TYPE_PRECISION (type))) == 0) | |
8fdc6c67 RB |
7266 | (if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))) |
7267 | (with { tree ntype = TREE_TYPE (@0); } | |
7268 | (convert (bit_and (op @0 @1) (convert:ntype @4)))) | |
7269 | (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); } | |
7270 | (convert (bit_and (op (convert:utype @0) (convert:utype @1)) | |
7271 | (convert:utype @4)))))))) | |
4f7a5692 | 7272 | |
03cc70b5 | 7273 | /* Transform (@0 < @1 and @0 < @2) to use min, |
4f7a5692 | 7274 | (@0 > @1 and @0 > @2) to use max */ |
dac920e8 MG |
7275 | (for logic (bit_and bit_and bit_and bit_and bit_ior bit_ior bit_ior bit_ior) |
7276 | op (lt le gt ge lt le gt ge ) | |
7277 | ext (min min max max max max min min ) | |
4f7a5692 | 7278 | (simplify |
dac920e8 | 7279 | (logic (op:cs @0 @1) (op:cs @0 @2)) |
4618c453 RB |
7280 | (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)) |
7281 | && TREE_CODE (@0) != INTEGER_CST) | |
4f7a5692 MC |
7282 | (op @0 (ext @1 @2))))) |
7283 | ||
7317ef4a RS |
7284 | (simplify |
7285 | /* signbit(x) -> 0 if x is nonnegative. */ | |
7286 | (SIGNBIT tree_expr_nonnegative_p@0) | |
7287 | { integer_zero_node; }) | |
7288 | ||
7289 | (simplify | |
7290 | /* signbit(x) -> x<0 if x doesn't have signed zeros. */ | |
7291 | (SIGNBIT @0) | |
7292 | (if (!HONOR_SIGNED_ZEROS (@0)) | |
7293 | (convert (lt @0 { build_real (TREE_TYPE (@0), dconst0); })))) | |
a8b85ce9 MG |
7294 | |
7295 | /* Transform comparisons of the form X +- C1 CMP C2 to X CMP C2 -+ C1. */ | |
7296 | (for cmp (eq ne) | |
7297 | (for op (plus minus) | |
7298 | rop (minus plus) | |
7299 | (simplify | |
7300 | (cmp (op@3 @0 INTEGER_CST@1) INTEGER_CST@2) | |
7301 | (if (!TREE_OVERFLOW (@1) && !TREE_OVERFLOW (@2) | |
7302 | && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@0)) | |
7303 | && !TYPE_OVERFLOW_TRAPS (TREE_TYPE (@0)) | |
7304 | && !TYPE_SATURATING (TREE_TYPE (@0))) | |
7305 | (with { tree res = int_const_binop (rop, @2, @1); } | |
75473a91 RB |
7306 | (if (TREE_OVERFLOW (res) |
7307 | && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))) | |
a8b85ce9 MG |
7308 | { constant_boolean_node (cmp == NE_EXPR, type); } |
7309 | (if (single_use (@3)) | |
11c1e63c JJ |
7310 | (cmp @0 { TREE_OVERFLOW (res) |
7311 | ? drop_tree_overflow (res) : res; })))))))) | |
a8b85ce9 MG |
7312 | (for cmp (lt le gt ge) |
7313 | (for op (plus minus) | |
7314 | rop (minus plus) | |
7315 | (simplify | |
7316 | (cmp (op@3 @0 INTEGER_CST@1) INTEGER_CST@2) | |
7317 | (if (!TREE_OVERFLOW (@1) && !TREE_OVERFLOW (@2) | |
7318 | && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))) | |
7319 | (with { tree res = int_const_binop (rop, @2, @1); } | |
7320 | (if (TREE_OVERFLOW (res)) | |
7321 | { | |
7322 | fold_overflow_warning (("assuming signed overflow does not occur " | |
7323 | "when simplifying conditional to constant"), | |
7324 | WARN_STRICT_OVERFLOW_CONDITIONAL); | |
7325 | bool less = cmp == LE_EXPR || cmp == LT_EXPR; | |
7326 | /* wi::ges_p (@2, 0) should be sufficient for a signed type. */ | |
8e6cdc90 RS |
7327 | bool ovf_high = wi::lt_p (wi::to_wide (@1), 0, |
7328 | TYPE_SIGN (TREE_TYPE (@1))) | |
a8b85ce9 MG |
7329 | != (op == MINUS_EXPR); |
7330 | constant_boolean_node (less == ovf_high, type); | |
7331 | } | |
7332 | (if (single_use (@3)) | |
7333 | (with | |
7334 | { | |
7335 | fold_overflow_warning (("assuming signed overflow does not occur " | |
7336 | "when changing X +- C1 cmp C2 to " | |
7337 | "X cmp C2 -+ C1"), | |
7338 | WARN_STRICT_OVERFLOW_COMPARISON); | |
7339 | } | |
7340 | (cmp @0 { res; }))))))))) | |
d3e40b76 RB |
7341 | |
7342 | /* Canonicalizations of BIT_FIELD_REFs. */ | |
7343 | ||
6ec96dcb RB |
7344 | (simplify |
7345 | (BIT_FIELD_REF (BIT_FIELD_REF @0 @1 @2) @3 @4) | |
7346 | (BIT_FIELD_REF @0 @3 { const_binop (PLUS_EXPR, bitsizetype, @2, @4); })) | |
7347 | ||
7348 | (simplify | |
7349 | (BIT_FIELD_REF (view_convert @0) @1 @2) | |
7350 | (BIT_FIELD_REF @0 @1 @2)) | |
7351 | ||
7352 | (simplify | |
7353 | (BIT_FIELD_REF @0 @1 integer_zerop) | |
7354 | (if (tree_int_cst_equal (@1, TYPE_SIZE (TREE_TYPE (@0)))) | |
7355 | (view_convert @0))) | |
7356 | ||
d3e40b76 RB |
7357 | (simplify |
7358 | (BIT_FIELD_REF @0 @1 @2) | |
7359 | (switch | |
7360 | (if (TREE_CODE (TREE_TYPE (@0)) == COMPLEX_TYPE | |
7361 | && tree_int_cst_equal (@1, TYPE_SIZE (TREE_TYPE (TREE_TYPE (@0))))) | |
7362 | (switch | |
7363 | (if (integer_zerop (@2)) | |
7364 | (view_convert (realpart @0))) | |
7365 | (if (tree_int_cst_equal (@2, TYPE_SIZE (TREE_TYPE (TREE_TYPE (@0))))) | |
7366 | (view_convert (imagpart @0))))) | |
7367 | (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)) | |
7368 | && INTEGRAL_TYPE_P (type) | |
171f6f05 RB |
7369 | /* On GIMPLE this should only apply to register arguments. */ |
7370 | && (! GIMPLE || is_gimple_reg (@0)) | |
d3e40b76 RB |
7371 | /* A bit-field-ref that referenced the full argument can be stripped. */ |
7372 | && ((compare_tree_int (@1, TYPE_PRECISION (TREE_TYPE (@0))) == 0 | |
7373 | && integer_zerop (@2)) | |
7374 | /* Low-parts can be reduced to integral conversions. | |
7375 | ??? The following doesn't work for PDP endian. */ | |
7376 | || (BYTES_BIG_ENDIAN == WORDS_BIG_ENDIAN | |
285fa338 RB |
7377 | /* But only do this after vectorization. */ |
7378 | && canonicalize_math_after_vectorization_p () | |
d3e40b76 RB |
7379 | /* Don't even think about BITS_BIG_ENDIAN. */ |
7380 | && TYPE_PRECISION (TREE_TYPE (@0)) % BITS_PER_UNIT == 0 | |
7381 | && TYPE_PRECISION (type) % BITS_PER_UNIT == 0 | |
7382 | && compare_tree_int (@2, (BYTES_BIG_ENDIAN | |
7383 | ? (TYPE_PRECISION (TREE_TYPE (@0)) | |
7384 | - TYPE_PRECISION (type)) | |
7385 | : 0)) == 0))) | |
7386 | (convert @0)))) | |
7387 | ||
7388 | /* Simplify vector extracts. */ | |
7389 | ||
7390 | (simplify | |
7391 | (BIT_FIELD_REF CONSTRUCTOR@0 @1 @2) | |
7392 | (if (VECTOR_TYPE_P (TREE_TYPE (@0)) | |
7d6bb809 RB |
7393 | && tree_fits_uhwi_p (TYPE_SIZE (type)) |
7394 | && ((tree_to_uhwi (TYPE_SIZE (type)) | |
7395 | == tree_to_uhwi (TYPE_SIZE (TREE_TYPE (TREE_TYPE (@0))))) | |
d3e40b76 | 7396 | || (VECTOR_TYPE_P (type) |
7d6bb809 RB |
7397 | && (tree_to_uhwi (TYPE_SIZE (TREE_TYPE (type))) |
7398 | == tree_to_uhwi (TYPE_SIZE (TREE_TYPE (TREE_TYPE (@0)))))))) | |
d3e40b76 RB |
7399 | (with |
7400 | { | |
49bf49bb RB |
7401 | tree ctor = (TREE_CODE (@0) == SSA_NAME |
7402 | ? gimple_assign_rhs1 (SSA_NAME_DEF_STMT (@0)) : @0); | |
d3e40b76 RB |
7403 | tree eltype = TREE_TYPE (TREE_TYPE (ctor)); |
7404 | unsigned HOST_WIDE_INT width = tree_to_uhwi (TYPE_SIZE (eltype)); | |
7405 | unsigned HOST_WIDE_INT n = tree_to_uhwi (@1); | |
7406 | unsigned HOST_WIDE_INT idx = tree_to_uhwi (@2); | |
7407 | } | |
7408 | (if (n != 0 | |
7409 | && (idx % width) == 0 | |
7410 | && (n % width) == 0 | |
928686b1 RS |
7411 | && known_le ((idx + n) / width, |
7412 | TYPE_VECTOR_SUBPARTS (TREE_TYPE (ctor)))) | |
d3e40b76 RB |
7413 | (with |
7414 | { | |
7415 | idx = idx / width; | |
7416 | n = n / width; | |
7417 | /* Constructor elements can be subvectors. */ | |
d34457c1 | 7418 | poly_uint64 k = 1; |
d3e40b76 RB |
7419 | if (CONSTRUCTOR_NELTS (ctor) != 0) |
7420 | { | |
7421 | tree cons_elem = TREE_TYPE (CONSTRUCTOR_ELT (ctor, 0)->value); | |
7422 | if (TREE_CODE (cons_elem) == VECTOR_TYPE) | |
7423 | k = TYPE_VECTOR_SUBPARTS (cons_elem); | |
7424 | } | |
d34457c1 | 7425 | unsigned HOST_WIDE_INT elt, count, const_k; |
d3e40b76 RB |
7426 | } |
7427 | (switch | |
7428 | /* We keep an exact subset of the constructor elements. */ | |
d34457c1 | 7429 | (if (multiple_p (idx, k, &elt) && multiple_p (n, k, &count)) |
d3e40b76 | 7430 | (if (CONSTRUCTOR_NELTS (ctor) == 0) |
b972e036 | 7431 | { build_zero_cst (type); } |
d34457c1 RS |
7432 | (if (count == 1) |
7433 | (if (elt < CONSTRUCTOR_NELTS (ctor)) | |
4c1da8ea | 7434 | (view_convert { CONSTRUCTOR_ELT (ctor, elt)->value; }) |
d34457c1 | 7435 | { build_zero_cst (type); }) |
c265dfbf RB |
7436 | /* We don't want to emit new CTORs unless the old one goes away. |
7437 | ??? Eventually allow this if the CTOR ends up constant or | |
7438 | uniform. */ | |
7439 | (if (single_use (@0)) | |
b972e036 RB |
7440 | (with |
7441 | { | |
7442 | vec<constructor_elt, va_gc> *vals; | |
7443 | vec_alloc (vals, count); | |
7444 | bool constant_p = true; | |
7445 | tree res; | |
7446 | for (unsigned i = 0; | |
7447 | i < count && elt + i < CONSTRUCTOR_NELTS (ctor); ++i) | |
7448 | { | |
7449 | tree e = CONSTRUCTOR_ELT (ctor, elt + i)->value; | |
7450 | CONSTRUCTOR_APPEND_ELT (vals, NULL_TREE, e); | |
7451 | if (!CONSTANT_CLASS_P (e)) | |
7452 | constant_p = false; | |
7453 | } | |
7d6bb809 RB |
7454 | tree evtype = (types_match (TREE_TYPE (type), |
7455 | TREE_TYPE (TREE_TYPE (ctor))) | |
7456 | ? type | |
7457 | : build_vector_type (TREE_TYPE (TREE_TYPE (ctor)), | |
4a808328 | 7458 | count * k)); |
7d6bb809 RB |
7459 | res = (constant_p ? build_vector_from_ctor (evtype, vals) |
7460 | : build_constructor (evtype, vals)); | |
b972e036 | 7461 | } |
7d6bb809 | 7462 | (view_convert { res; })))))) |
d3e40b76 | 7463 | /* The bitfield references a single constructor element. */ |
d34457c1 RS |
7464 | (if (k.is_constant (&const_k) |
7465 | && idx + n <= (idx / const_k + 1) * const_k) | |
d3e40b76 | 7466 | (switch |
d34457c1 | 7467 | (if (CONSTRUCTOR_NELTS (ctor) <= idx / const_k) |
d3e40b76 | 7468 | { build_zero_cst (type); }) |
d34457c1 | 7469 | (if (n == const_k) |
4c1da8ea | 7470 | (view_convert { CONSTRUCTOR_ELT (ctor, idx / const_k)->value; })) |
d34457c1 RS |
7471 | (BIT_FIELD_REF { CONSTRUCTOR_ELT (ctor, idx / const_k)->value; } |
7472 | @1 { bitsize_int ((idx % const_k) * width); }))))))))) | |
92e29a5e RB |
7473 | |
7474 | /* Simplify a bit extraction from a bit insertion for the cases with | |
7475 | the inserted element fully covering the extraction or the insertion | |
7476 | not touching the extraction. */ | |
7477 | (simplify | |
7478 | (BIT_FIELD_REF (bit_insert @0 @1 @ipos) @rsize @rpos) | |
7479 | (with | |
7480 | { | |
7481 | unsigned HOST_WIDE_INT isize; | |
7482 | if (INTEGRAL_TYPE_P (TREE_TYPE (@1))) | |
7483 | isize = TYPE_PRECISION (TREE_TYPE (@1)); | |
7484 | else | |
7485 | isize = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (@1))); | |
7486 | } | |
7487 | (switch | |
44f308e5 | 7488 | (if ((!INTEGRAL_TYPE_P (TREE_TYPE (@1)) |
bcca64d7 JJ |
7489 | || type_has_mode_precision_p (TREE_TYPE (@1))) |
7490 | && wi::leu_p (wi::to_wide (@ipos), wi::to_wide (@rpos)) | |
8e6cdc90 RS |
7491 | && wi::leu_p (wi::to_wide (@rpos) + wi::to_wide (@rsize), |
7492 | wi::to_wide (@ipos) + isize)) | |
92e29a5e | 7493 | (BIT_FIELD_REF @1 @rsize { wide_int_to_tree (bitsizetype, |
bcca64d7 | 7494 | wi::to_wide (@rpos) |
8e6cdc90 | 7495 | - wi::to_wide (@ipos)); })) |
bcca64d7 JJ |
7496 | (if (wi::eq_p (wi::to_wide (@ipos), wi::to_wide (@rpos)) |
7497 | && compare_tree_int (@rsize, isize) == 0) | |
7498 | (convert @1)) | |
8e6cdc90 RS |
7499 | (if (wi::geu_p (wi::to_wide (@ipos), |
7500 | wi::to_wide (@rpos) + wi::to_wide (@rsize)) | |
7501 | || wi::geu_p (wi::to_wide (@rpos), | |
7502 | wi::to_wide (@ipos) + isize)) | |
92e29a5e | 7503 | (BIT_FIELD_REF @0 @rsize @rpos))))) |
c566cc9f | 7504 | |
c453ccc2 RS |
7505 | (if (canonicalize_math_after_vectorization_p ()) |
7506 | (for fmas (FMA) | |
7507 | (simplify | |
7508 | (fmas:c (negate @0) @1 @2) | |
7509 | (IFN_FNMA @0 @1 @2)) | |
7510 | (simplify | |
7511 | (fmas @0 @1 (negate @2)) | |
7512 | (IFN_FMS @0 @1 @2)) | |
7513 | (simplify | |
7514 | (fmas:c (negate @0) @1 (negate @2)) | |
7515 | (IFN_FNMS @0 @1 @2)) | |
7516 | (simplify | |
7517 | (negate (fmas@3 @0 @1 @2)) | |
7518 | (if (single_use (@3)) | |
7519 | (IFN_FNMS @0 @1 @2)))) | |
7520 | ||
c566cc9f | 7521 | (simplify |
c453ccc2 RS |
7522 | (IFN_FMS:c (negate @0) @1 @2) |
7523 | (IFN_FNMS @0 @1 @2)) | |
7524 | (simplify | |
7525 | (IFN_FMS @0 @1 (negate @2)) | |
7526 | (IFN_FMA @0 @1 @2)) | |
7527 | (simplify | |
7528 | (IFN_FMS:c (negate @0) @1 (negate @2)) | |
c566cc9f RS |
7529 | (IFN_FNMA @0 @1 @2)) |
7530 | (simplify | |
c453ccc2 RS |
7531 | (negate (IFN_FMS@3 @0 @1 @2)) |
7532 | (if (single_use (@3)) | |
7533 | (IFN_FNMA @0 @1 @2))) | |
7534 | ||
7535 | (simplify | |
7536 | (IFN_FNMA:c (negate @0) @1 @2) | |
7537 | (IFN_FMA @0 @1 @2)) | |
c566cc9f | 7538 | (simplify |
c453ccc2 | 7539 | (IFN_FNMA @0 @1 (negate @2)) |
c566cc9f RS |
7540 | (IFN_FNMS @0 @1 @2)) |
7541 | (simplify | |
c453ccc2 RS |
7542 | (IFN_FNMA:c (negate @0) @1 (negate @2)) |
7543 | (IFN_FMS @0 @1 @2)) | |
7544 | (simplify | |
7545 | (negate (IFN_FNMA@3 @0 @1 @2)) | |
c566cc9f | 7546 | (if (single_use (@3)) |
c453ccc2 | 7547 | (IFN_FMS @0 @1 @2))) |
c566cc9f | 7548 | |
c453ccc2 RS |
7549 | (simplify |
7550 | (IFN_FNMS:c (negate @0) @1 @2) | |
7551 | (IFN_FMS @0 @1 @2)) | |
7552 | (simplify | |
7553 | (IFN_FNMS @0 @1 (negate @2)) | |
7554 | (IFN_FNMA @0 @1 @2)) | |
7555 | (simplify | |
7556 | (IFN_FNMS:c (negate @0) @1 (negate @2)) | |
7557 | (IFN_FMA @0 @1 @2)) | |
7558 | (simplify | |
7559 | (negate (IFN_FNMS@3 @0 @1 @2)) | |
c566cc9f | 7560 | (if (single_use (@3)) |
c453ccc2 | 7561 | (IFN_FMA @0 @1 @2)))) |
ba6557e2 | 7562 | |
d2eb616a JJ |
7563 | /* CLZ simplifications. */ |
7564 | (for clz (CLZ) | |
7565 | (for op (eq ne) | |
7566 | cmp (lt ge) | |
7567 | (simplify | |
a2ef38b1 HPN |
7568 | (op (clz:s@2 @0) INTEGER_CST@1) |
7569 | (if (integer_zerop (@1) && single_use (@2)) | |
d2eb616a | 7570 | /* clz(X) == 0 is (int)X < 0 and clz(X) != 0 is (int)X >= 0. */ |
75f89001 JJ |
7571 | (with { tree type0 = TREE_TYPE (@0); |
7572 | tree stype = signed_type_for (type0); | |
d2eb616a | 7573 | HOST_WIDE_INT val = 0; |
d2eb616a | 7574 | /* Punt on hypothetical weird targets. */ |
75f89001 JJ |
7575 | if (clz == CFN_CLZ |
7576 | && CLZ_DEFINED_VALUE_AT_ZERO (SCALAR_TYPE_MODE (type0), | |
7577 | val) == 2 | |
d2eb616a JJ |
7578 | && val == 0) |
7579 | stype = NULL_TREE; | |
d2eb616a JJ |
7580 | } |
7581 | (if (stype) | |
7582 | (cmp (convert:stype @0) { build_zero_cst (stype); }))) | |
7583 | /* clz(X) == (prec-1) is X == 1 and clz(X) != (prec-1) is X != 1. */ | |
7584 | (with { bool ok = true; | |
75f89001 JJ |
7585 | HOST_WIDE_INT val = 0; |
7586 | tree type0 = TREE_TYPE (@0); | |
d2eb616a | 7587 | /* Punt on hypothetical weird targets. */ |
75f89001 JJ |
7588 | if (clz == CFN_CLZ |
7589 | && CLZ_DEFINED_VALUE_AT_ZERO (SCALAR_TYPE_MODE (type0), | |
7590 | val) == 2 | |
7591 | && val == TYPE_PRECISION (type0) - 1) | |
d2eb616a | 7592 | ok = false; |
d2eb616a | 7593 | } |
75f89001 JJ |
7594 | (if (ok && wi::to_wide (@1) == (TYPE_PRECISION (type0) - 1)) |
7595 | (op @0 { build_one_cst (type0); }))))))) | |
7596 | ||
7597 | /* CTZ simplifications. */ | |
7598 | (for ctz (CTZ) | |
7599 | (for op (ge gt le lt) | |
7600 | cmp (eq eq ne ne) | |
7601 | (simplify | |
7602 | /* __builtin_ctz (x) >= C -> (x & ((1 << C) - 1)) == 0. */ | |
7603 | (op (ctz:s @0) INTEGER_CST@1) | |
7604 | (with { bool ok = true; | |
7605 | HOST_WIDE_INT val = 0; | |
7606 | if (!tree_fits_shwi_p (@1)) | |
7607 | ok = false; | |
7608 | else | |
7609 | { | |
7610 | val = tree_to_shwi (@1); | |
7611 | /* Canonicalize to >= or <. */ | |
7612 | if (op == GT_EXPR || op == LE_EXPR) | |
7613 | { | |
7614 | if (val == HOST_WIDE_INT_MAX) | |
7615 | ok = false; | |
7616 | else | |
7617 | val++; | |
7618 | } | |
7619 | } | |
7620 | bool zero_res = false; | |
7621 | HOST_WIDE_INT zero_val = 0; | |
7622 | tree type0 = TREE_TYPE (@0); | |
7623 | int prec = TYPE_PRECISION (type0); | |
7624 | if (ctz == CFN_CTZ | |
7625 | && CTZ_DEFINED_VALUE_AT_ZERO (SCALAR_TYPE_MODE (type0), | |
7626 | zero_val) == 2) | |
7627 | zero_res = true; | |
7628 | } | |
7629 | (if (val <= 0) | |
7630 | (if (ok && (!zero_res || zero_val >= val)) | |
7631 | { constant_boolean_node (cmp == EQ_EXPR ? true : false, type); }) | |
7632 | (if (val >= prec) | |
7633 | (if (ok && (!zero_res || zero_val < val)) | |
7634 | { constant_boolean_node (cmp == EQ_EXPR ? false : true, type); }) | |
7635 | (if (ok && (!zero_res || zero_val < 0 || zero_val >= prec)) | |
7636 | (cmp (bit_and @0 { wide_int_to_tree (type0, | |
7637 | wi::mask (val, false, prec)); }) | |
7638 | { build_zero_cst (type0); }))))))) | |
7639 | (for op (eq ne) | |
7640 | (simplify | |
7641 | /* __builtin_ctz (x) == C -> (x & ((1 << (C + 1)) - 1)) == (1 << C). */ | |
7642 | (op (ctz:s @0) INTEGER_CST@1) | |
7643 | (with { bool zero_res = false; | |
7644 | HOST_WIDE_INT zero_val = 0; | |
7645 | tree type0 = TREE_TYPE (@0); | |
7646 | int prec = TYPE_PRECISION (type0); | |
7647 | if (ctz == CFN_CTZ | |
7648 | && CTZ_DEFINED_VALUE_AT_ZERO (SCALAR_TYPE_MODE (type0), | |
7649 | zero_val) == 2) | |
7650 | zero_res = true; | |
7651 | } | |
7652 | (if (tree_int_cst_sgn (@1) < 0 || wi::to_widest (@1) >= prec) | |
7653 | (if (!zero_res || zero_val != wi::to_widest (@1)) | |
7654 | { constant_boolean_node (op == EQ_EXPR ? false : true, type); }) | |
7655 | (if (!zero_res || zero_val < 0 || zero_val >= prec) | |
7656 | (op (bit_and @0 { wide_int_to_tree (type0, | |
7657 | wi::mask (tree_to_uhwi (@1) + 1, | |
7658 | false, prec)); }) | |
7659 | { wide_int_to_tree (type0, | |
7660 | wi::shifted_mask (tree_to_uhwi (@1), 1, | |
7661 | false, prec)); }))))))) | |
d2eb616a | 7662 | |
ba6557e2 | 7663 | /* POPCOUNT simplifications. */ |
33bf56dd RS |
7664 | /* popcount(X) + popcount(Y) is popcount(X|Y) when X&Y must be zero. */ |
7665 | (simplify | |
7666 | (plus (POPCOUNT:s @0) (POPCOUNT:s @1)) | |
193fccaa AP |
7667 | (if (INTEGRAL_TYPE_P (type) |
7668 | && wi::bit_and (tree_nonzero_bits (@0), tree_nonzero_bits (@1)) == 0) | |
33bf56dd RS |
7669 | (POPCOUNT (bit_ior @0 @1)))) |
7670 | ||
7671 | /* popcount(X) == 0 is X == 0, and related (in)equalities. */ | |
7672 | (for popcount (POPCOUNT) | |
ba6557e2 RS |
7673 | (for cmp (le eq ne gt) |
7674 | rep (eq eq ne ne) | |
7675 | (simplify | |
7676 | (cmp (popcount @0) integer_zerop) | |
7677 | (rep @0 { build_zero_cst (TREE_TYPE (@0)); })))) | |
0d2b3bca | 7678 | |
33bf56dd RS |
7679 | /* Canonicalize POPCOUNT(x)&1 as PARITY(X). */ |
7680 | (simplify | |
7681 | (bit_and (POPCOUNT @0) integer_onep) | |
7682 | (PARITY @0)) | |
7683 | ||
7684 | /* PARITY simplifications. */ | |
7685 | /* parity(~X) is parity(X). */ | |
7686 | (simplify | |
7687 | (PARITY (bit_not @0)) | |
7688 | (PARITY @0)) | |
7689 | ||
7690 | /* parity(X)^parity(Y) is parity(X^Y). */ | |
7691 | (simplify | |
7692 | (bit_xor (PARITY:s @0) (PARITY:s @1)) | |
7693 | (PARITY (bit_xor @0 @1))) | |
7694 | ||
7695 | /* Common POPCOUNT/PARITY simplifications. */ | |
7696 | /* popcount(X&C1) is (X>>C2)&1 when C1 == 1<<C2. Same for parity(X&C1). */ | |
7697 | (for pfun (POPCOUNT PARITY) | |
7698 | (simplify | |
7699 | (pfun @0) | |
193fccaa AP |
7700 | (if (INTEGRAL_TYPE_P (type)) |
7701 | (with { wide_int nz = tree_nonzero_bits (@0); } | |
7702 | (switch | |
7703 | (if (nz == 1) | |
7704 | (convert @0)) | |
7705 | (if (wi::popcount (nz) == 1) | |
7706 | (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); } | |
7707 | (convert (rshift:utype (convert:utype @0) | |
7708 | { build_int_cst (integer_type_node, | |
7709 | wi::ctz (nz)); }))))))))) | |
33bf56dd | 7710 | |
ac87f0f3 DP |
7711 | #if GIMPLE |
7712 | /* 64- and 32-bits branchless implementations of popcount are detected: | |
7713 | ||
7714 | int popcount64c (uint64_t x) | |
7715 | { | |
7716 | x -= (x >> 1) & 0x5555555555555555ULL; | |
7717 | x = (x & 0x3333333333333333ULL) + ((x >> 2) & 0x3333333333333333ULL); | |
7718 | x = (x + (x >> 4)) & 0x0f0f0f0f0f0f0f0fULL; | |
7719 | return (x * 0x0101010101010101ULL) >> 56; | |
7720 | } | |
7721 | ||
7722 | int popcount32c (uint32_t x) | |
7723 | { | |
7724 | x -= (x >> 1) & 0x55555555; | |
7725 | x = (x & 0x33333333) + ((x >> 2) & 0x33333333); | |
7726 | x = (x + (x >> 4)) & 0x0f0f0f0f; | |
7727 | return (x * 0x01010101) >> 24; | |
7728 | } */ | |
7729 | (simplify | |
2efa10d5 JJ |
7730 | (rshift |
7731 | (mult | |
7732 | (bit_and | |
7733 | (plus:c | |
7734 | (rshift @8 INTEGER_CST@5) | |
7735 | (plus:c@8 | |
7736 | (bit_and @6 INTEGER_CST@7) | |
7737 | (bit_and | |
7738 | (rshift | |
7739 | (minus@6 @0 | |
7740 | (bit_and (rshift @0 INTEGER_CST@4) INTEGER_CST@11)) | |
7741 | INTEGER_CST@10) | |
7742 | INTEGER_CST@9))) | |
7743 | INTEGER_CST@3) | |
7744 | INTEGER_CST@2) | |
7745 | INTEGER_CST@1) | |
ac87f0f3 | 7746 | /* Check constants and optab. */ |
2efa10d5 JJ |
7747 | (with { unsigned prec = TYPE_PRECISION (type); |
7748 | int shift = (64 - prec) & 63; | |
7749 | unsigned HOST_WIDE_INT c1 | |
7750 | = HOST_WIDE_INT_UC (0x0101010101010101) >> shift; | |
7751 | unsigned HOST_WIDE_INT c2 | |
7752 | = HOST_WIDE_INT_UC (0x0F0F0F0F0F0F0F0F) >> shift; | |
7753 | unsigned HOST_WIDE_INT c3 | |
7754 | = HOST_WIDE_INT_UC (0x3333333333333333) >> shift; | |
7755 | unsigned HOST_WIDE_INT c4 | |
7756 | = HOST_WIDE_INT_UC (0x5555555555555555) >> shift; | |
7757 | } | |
7758 | (if (prec >= 16 | |
7759 | && prec <= 64 | |
7760 | && pow2p_hwi (prec) | |
7761 | && TYPE_UNSIGNED (type) | |
7762 | && integer_onep (@4) | |
7763 | && wi::to_widest (@10) == 2 | |
7764 | && wi::to_widest (@5) == 4 | |
7765 | && wi::to_widest (@1) == prec - 8 | |
7766 | && tree_to_uhwi (@2) == c1 | |
7767 | && tree_to_uhwi (@3) == c2 | |
7768 | && tree_to_uhwi (@9) == c3 | |
7769 | && tree_to_uhwi (@7) == c3 | |
5b43f6ac JR |
7770 | && tree_to_uhwi (@11) == c4) |
7771 | (if (direct_internal_fn_supported_p (IFN_POPCOUNT, type, | |
7772 | OPTIMIZE_FOR_BOTH)) | |
7773 | (convert (IFN_POPCOUNT:type @0)) | |
7774 | /* Try to do popcount in two halves. PREC must be at least | |
7775 | five bits for this to work without extension before adding. */ | |
7776 | (with { | |
7777 | tree half_type = NULL_TREE; | |
7778 | opt_machine_mode m = mode_for_size ((prec + 1) / 2, MODE_INT, 1); | |
7779 | int half_prec = 8; | |
7780 | if (m.exists () | |
7781 | && m.require () != TYPE_MODE (type)) | |
7782 | { | |
7783 | half_prec = GET_MODE_PRECISION (as_a <scalar_int_mode> (m)); | |
7784 | half_type = build_nonstandard_integer_type (half_prec, 1); | |
7785 | } | |
7786 | gcc_assert (half_prec > 2); | |
7787 | } | |
7788 | (if (half_type != NULL_TREE | |
7789 | && direct_internal_fn_supported_p (IFN_POPCOUNT, half_type, | |
7790 | OPTIMIZE_FOR_BOTH)) | |
7791 | (convert (plus | |
7792 | (IFN_POPCOUNT:half_type (convert @0)) | |
7793 | (IFN_POPCOUNT:half_type (convert (rshift @0 | |
7794 | { build_int_cst (integer_type_node, half_prec); } ))))))))))) | |
df569f7d JJ |
7795 | |
7796 | /* __builtin_ffs needs to deal on many targets with the possible zero | |
7797 | argument. If we know the argument is always non-zero, __builtin_ctz + 1 | |
7798 | should lead to better code. */ | |
7799 | (simplify | |
7800 | (FFS tree_expr_nonzero_p@0) | |
7801 | (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)) | |
7802 | && direct_internal_fn_supported_p (IFN_CTZ, TREE_TYPE (@0), | |
7803 | OPTIMIZE_FOR_SPEED)) | |
600cf112 JJ |
7804 | (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); } |
7805 | (plus (CTZ:type (convert:utype @0)) { build_one_cst (type); })))) | |
ac87f0f3 DP |
7806 | #endif |
7807 | ||
653ab081 JJ |
7808 | (for ffs (BUILT_IN_FFS BUILT_IN_FFSL BUILT_IN_FFSLL |
7809 | BUILT_IN_FFSIMAX) | |
7810 | /* __builtin_ffs (X) == 0 -> X == 0. | |
7811 | __builtin_ffs (X) == 6 -> (X & 63) == 32. */ | |
7812 | (for cmp (eq ne) | |
7813 | (simplify | |
7814 | (cmp (ffs@2 @0) INTEGER_CST@1) | |
7815 | (with { int prec = TYPE_PRECISION (TREE_TYPE (@0)); } | |
7816 | (switch | |
7817 | (if (integer_zerop (@1)) | |
7818 | (cmp @0 { build_zero_cst (TREE_TYPE (@0)); })) | |
7819 | (if (tree_int_cst_sgn (@1) < 0 || wi::to_widest (@1) > prec) | |
7820 | { constant_boolean_node (cmp == NE_EXPR ? true : false, type); }) | |
7821 | (if (single_use (@2)) | |
7822 | (cmp (bit_and @0 { wide_int_to_tree (TREE_TYPE (@0), | |
7823 | wi::mask (tree_to_uhwi (@1), | |
7824 | false, prec)); }) | |
7825 | { wide_int_to_tree (TREE_TYPE (@0), | |
7826 | wi::shifted_mask (tree_to_uhwi (@1) - 1, 1, | |
7827 | false, prec)); })))))) | |
7828 | ||
7829 | /* __builtin_ffs (X) > 6 -> X != 0 && (X & 63) == 0. */ | |
7830 | (for cmp (gt le) | |
7831 | cmp2 (ne eq) | |
7832 | cmp3 (eq ne) | |
7833 | bit_op (bit_and bit_ior) | |
7834 | (simplify | |
7835 | (cmp (ffs@2 @0) INTEGER_CST@1) | |
7836 | (with { int prec = TYPE_PRECISION (TREE_TYPE (@0)); } | |
7837 | (switch | |
7838 | (if (integer_zerop (@1)) | |
7839 | (cmp2 @0 { build_zero_cst (TREE_TYPE (@0)); })) | |
7840 | (if (tree_int_cst_sgn (@1) < 0) | |
7841 | { constant_boolean_node (cmp == GT_EXPR ? true : false, type); }) | |
7842 | (if (wi::to_widest (@1) >= prec) | |
7843 | { constant_boolean_node (cmp == GT_EXPR ? false : true, type); }) | |
7844 | (if (wi::to_widest (@1) == prec - 1) | |
7845 | (cmp3 @0 { wide_int_to_tree (TREE_TYPE (@0), | |
7846 | wi::shifted_mask (prec - 1, 1, | |
7847 | false, prec)); })) | |
7848 | (if (single_use (@2)) | |
7849 | (bit_op (cmp2 @0 { build_zero_cst (TREE_TYPE (@0)); }) | |
7850 | (cmp3 (bit_and @0 | |
7851 | { wide_int_to_tree (TREE_TYPE (@0), | |
7852 | wi::mask (tree_to_uhwi (@1), | |
7853 | false, prec)); }) | |
7854 | { build_zero_cst (TREE_TYPE (@0)); })))))))) | |
7855 | ||
20dcda98 | 7856 | #if GIMPLE |
7857 | ||
7858 | /* Simplify: | |
7859 | a = op a1 | |
7860 | r = cond ? a : b | |
7861 | --> r = .COND_FN (cond, a, b) | |
7862 | and, | |
7863 | a = op a1 | |
7864 | r = cond ? b : a | |
7865 | --> r = .COND_FN (~cond, b, a). */ | |
7866 | ||
7867 | (for uncond_op (UNCOND_UNARY) | |
7868 | cond_op (COND_UNARY) | |
7869 | (simplify | |
7870 | (vec_cond @0 (view_convert? (uncond_op@3 @1)) @2) | |
7871 | (with { tree op_type = TREE_TYPE (@3); } | |
7872 | (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type) | |
7873 | && is_truth_type_for (op_type, TREE_TYPE (@0))) | |
7874 | (cond_op @0 @1 @2)))) | |
7875 | (simplify | |
7876 | (vec_cond @0 @1 (view_convert? (uncond_op@3 @2))) | |
7877 | (with { tree op_type = TREE_TYPE (@3); } | |
7878 | (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type) | |
7879 | && is_truth_type_for (op_type, TREE_TYPE (@0))) | |
7880 | (cond_op (bit_not @0) @2 @1))))) | |
7881 | ||
0d2b3bca RS |
7882 | /* Simplify: |
7883 | ||
7884 | a = a1 op a2 | |
7885 | r = c ? a : b; | |
7886 | ||
7887 | to: | |
7888 | ||
7889 | r = c ? a1 op a2 : b; | |
7890 | ||
7891 | if the target can do it in one go. This makes the operation conditional | |
7892 | on c, so could drop potentially-trapping arithmetic, but that's a valid | |
cff1a122 JJ |
7893 | simplification if the result of the operation isn't needed. |
7894 | ||
c16504f6 LJH |
7895 | Avoid speculatively generating a stand-alone vector comparison |
7896 | on targets that might not support them. Any target implementing | |
7897 | conditional internal functions must support the same comparisons | |
7898 | inside and outside a VEC_COND_EXPR. */ | |
cff1a122 | 7899 | |
0d2b3bca RS |
7900 | (for uncond_op (UNCOND_BINARY) |
7901 | cond_op (COND_BINARY) | |
7902 | (simplify | |
7903 | (vec_cond @0 (view_convert? (uncond_op@4 @1 @2)) @3) | |
cff1a122 JJ |
7904 | (with { tree op_type = TREE_TYPE (@4); } |
7905 | (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type) | |
165947fe | 7906 | && is_truth_type_for (op_type, TREE_TYPE (@0)) |
7907 | && single_use (@4)) | |
0d2b3bca RS |
7908 | (view_convert (cond_op @0 @1 @2 (view_convert:op_type @3)))))) |
7909 | (simplify | |
7910 | (vec_cond @0 @1 (view_convert? (uncond_op@4 @2 @3))) | |
cff1a122 JJ |
7911 | (with { tree op_type = TREE_TYPE (@4); } |
7912 | (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type) | |
165947fe | 7913 | && is_truth_type_for (op_type, TREE_TYPE (@0)) |
7914 | && single_use (@4)) | |
0d2b3bca | 7915 | (view_convert (cond_op (bit_not @0) @2 @3 (view_convert:op_type @1))))))) |
6a86928d | 7916 | |
b41d1f6e RS |
7917 | /* Same for ternary operations. */ |
7918 | (for uncond_op (UNCOND_TERNARY) | |
7919 | cond_op (COND_TERNARY) | |
7920 | (simplify | |
7921 | (vec_cond @0 (view_convert? (uncond_op@5 @1 @2 @3)) @4) | |
cff1a122 JJ |
7922 | (with { tree op_type = TREE_TYPE (@5); } |
7923 | (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type) | |
165947fe | 7924 | && is_truth_type_for (op_type, TREE_TYPE (@0)) |
7925 | && single_use (@5)) | |
b41d1f6e RS |
7926 | (view_convert (cond_op @0 @1 @2 @3 (view_convert:op_type @4)))))) |
7927 | (simplify | |
7928 | (vec_cond @0 @1 (view_convert? (uncond_op@5 @2 @3 @4))) | |
cff1a122 JJ |
7929 | (with { tree op_type = TREE_TYPE (@5); } |
7930 | (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type) | |
165947fe | 7931 | && is_truth_type_for (op_type, TREE_TYPE (@0)) |
7932 | && single_use (@5)) | |
b41d1f6e RS |
7933 | (view_convert (cond_op (bit_not @0) @2 @3 @4 |
7934 | (view_convert:op_type @1))))))) | |
ea5212b7 | 7935 | #endif |
b41d1f6e | 7936 | |
6a86928d RS |
7937 | /* Detect cases in which a VEC_COND_EXPR effectively replaces the |
7938 | "else" value of an IFN_COND_*. */ | |
7939 | (for cond_op (COND_BINARY) | |
7940 | (simplify | |
7941 | (vec_cond @0 (view_convert? (cond_op @0 @1 @2 @3)) @4) | |
7942 | (with { tree op_type = TREE_TYPE (@3); } | |
7943 | (if (element_precision (type) == element_precision (op_type)) | |
2c58d42c RS |
7944 | (view_convert (cond_op @0 @1 @2 (view_convert:op_type @4)))))) |
7945 | (simplify | |
7946 | (vec_cond @0 @1 (view_convert? (cond_op @2 @3 @4 @5))) | |
7947 | (with { tree op_type = TREE_TYPE (@5); } | |
7948 | (if (inverse_conditions_p (@0, @2) | |
7949 | && element_precision (type) == element_precision (op_type)) | |
7950 | (view_convert (cond_op @2 @3 @4 (view_convert:op_type @1))))))) | |
b41d1f6e RS |
7951 | |
7952 | /* Same for ternary operations. */ | |
7953 | (for cond_op (COND_TERNARY) | |
7954 | (simplify | |
7955 | (vec_cond @0 (view_convert? (cond_op @0 @1 @2 @3 @4)) @5) | |
7956 | (with { tree op_type = TREE_TYPE (@4); } | |
7957 | (if (element_precision (type) == element_precision (op_type)) | |
2c58d42c RS |
7958 | (view_convert (cond_op @0 @1 @2 @3 (view_convert:op_type @5)))))) |
7959 | (simplify | |
7960 | (vec_cond @0 @1 (view_convert? (cond_op @2 @3 @4 @5 @6))) | |
7961 | (with { tree op_type = TREE_TYPE (@6); } | |
7962 | (if (inverse_conditions_p (@0, @2) | |
7963 | && element_precision (type) == element_precision (op_type)) | |
7964 | (view_convert (cond_op @2 @3 @4 @5 (view_convert:op_type @1))))))) | |
a19f98d5 | 7965 | |
62b505a4 TC |
7966 | /* Detect simplication for a conditional reduction where |
7967 | ||
7968 | a = mask1 ? b : 0 | |
7969 | c = mask2 ? d + a : d | |
7970 | ||
7971 | is turned into | |
7972 | ||
7973 | c = mask1 && mask2 ? d + b : d. */ | |
7974 | (simplify | |
7975 | (IFN_COND_ADD @0 @1 (vec_cond @2 @3 integer_zerop) @1) | |
7976 | (IFN_COND_ADD (bit_and @0 @2) @1 @3 @1)) | |
7977 | ||
a19f98d5 RS |
7978 | /* For pointers @0 and @2 and nonnegative constant offset @1, look for |
7979 | expressions like: | |
7980 | ||
7981 | A: (@0 + @1 < @2) | (@2 + @1 < @0) | |
7982 | B: (@0 + @1 <= @2) | (@2 + @1 <= @0) | |
7983 | ||
7984 | If pointers are known not to wrap, B checks whether @1 bytes starting | |
7985 | at @0 and @2 do not overlap, while A tests the same thing for @1 + 1 | |
7986 | bytes. A is more efficiently tested as: | |
7987 | ||
7988 | A: (sizetype) (@0 + @1 - @2) > @1 * 2 | |
7989 | ||
7990 | The equivalent expression for B is given by replacing @1 with @1 - 1: | |
7991 | ||
7992 | B: (sizetype) (@0 + (@1 - 1) - @2) > (@1 - 1) * 2 | |
7993 | ||
7994 | @0 and @2 can be swapped in both expressions without changing the result. | |
7995 | ||
7996 | The folds rely on sizetype's being unsigned (which is always true) | |
7997 | and on its being the same width as the pointer (which we have to check). | |
7998 | ||
7999 | The fold replaces two pointer_plus expressions, two comparisons and | |
8000 | an IOR with a pointer_plus, a pointer_diff, and a comparison, so in | |
8001 | the best case it's a saving of two operations. The A fold retains one | |
8002 | of the original pointer_pluses, so is a win even if both pointer_pluses | |
8003 | are used elsewhere. The B fold is a wash if both pointer_pluses are | |
8004 | used elsewhere, since all we end up doing is replacing a comparison with | |
8005 | a pointer_plus. We do still apply the fold under those circumstances | |
8006 | though, in case applying it to other conditions eventually makes one of the | |
8007 | pointer_pluses dead. */ | |
8008 | (for ior (truth_orif truth_or bit_ior) | |
8009 | (for cmp (le lt) | |
8010 | (simplify | |
8011 | (ior (cmp:cs (pointer_plus@3 @0 INTEGER_CST@1) @2) | |
8012 | (cmp:cs (pointer_plus@4 @2 @1) @0)) | |
8013 | (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)) | |
8014 | && TYPE_OVERFLOW_WRAPS (sizetype) | |
8015 | && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (sizetype)) | |
8016 | /* Calculate the rhs constant. */ | |
8017 | (with { offset_int off = wi::to_offset (@1) - (cmp == LE_EXPR ? 1 : 0); | |
8018 | offset_int rhs = off * 2; } | |
8019 | /* Always fails for negative values. */ | |
8020 | (if (wi::min_precision (rhs, UNSIGNED) <= TYPE_PRECISION (sizetype)) | |
8021 | /* Since the order of @0 and @2 doesn't matter, let tree_swap_operands_p | |
8022 | pick a canonical order. This increases the chances of using the | |
8023 | same pointer_plus in multiple checks. */ | |
8024 | (with { bool swap_p = tree_swap_operands_p (@0, @2); | |
8025 | tree rhs_tree = wide_int_to_tree (sizetype, rhs); } | |
8026 | (if (cmp == LT_EXPR) | |
8027 | (gt (convert:sizetype | |
8028 | (pointer_diff:ssizetype { swap_p ? @4 : @3; } | |
8029 | { swap_p ? @0 : @2; })) | |
8030 | { rhs_tree; }) | |
8031 | (gt (convert:sizetype | |
8032 | (pointer_diff:ssizetype | |
8033 | (pointer_plus { swap_p ? @2 : @0; } | |
8034 | { wide_int_to_tree (sizetype, off); }) | |
8035 | { swap_p ? @0 : @2; })) | |
8036 | { rhs_tree; }))))))))) | |
f4bf2aab RS |
8037 | |
8038 | /* Fold REDUC (@0 & @1) -> @0[I] & @1[I] if element I is the only nonzero | |
8039 | element of @1. */ | |
8040 | (for reduc (IFN_REDUC_PLUS IFN_REDUC_IOR IFN_REDUC_XOR) | |
8041 | (simplify (reduc (view_convert? (bit_and @0 VECTOR_CST@1))) | |
8042 | (with { int i = single_nonzero_element (@1); } | |
8043 | (if (i >= 0) | |
8044 | (with { tree elt = vector_cst_elt (@1, i); | |
8045 | tree elt_type = TREE_TYPE (elt); | |
8046 | unsigned int elt_bits = tree_to_uhwi (TYPE_SIZE (elt_type)); | |
8047 | tree size = bitsize_int (elt_bits); | |
8048 | tree pos = bitsize_int (elt_bits * i); } | |
8049 | (view_convert | |
8050 | (bit_and:elt_type | |
8051 | (BIT_FIELD_REF:elt_type @0 { size; } { pos; }) | |
8052 | { elt; }))))))) | |
ebd733a7 | 8053 | |
2ef0e75d RS |
8054 | /* Fold reduction of a single nonzero element constructor. */ |
8055 | (for reduc (IFN_REDUC_PLUS IFN_REDUC_IOR IFN_REDUC_XOR) | |
8056 | (simplify (reduc (CONSTRUCTOR@0)) | |
49bf49bb RB |
8057 | (with { tree ctor = (TREE_CODE (@0) == SSA_NAME |
8058 | ? gimple_assign_rhs1 (SSA_NAME_DEF_STMT (@0)) : @0); | |
2ef0e75d RS |
8059 | tree elt = ctor_single_nonzero_element (ctor); } |
8060 | (if (elt | |
8061 | && !HONOR_SNANS (type) | |
8062 | && !HONOR_SIGNED_ZEROS (type)) | |
8063 | { elt; })))) | |
8064 | ||
8065 | /* Fold REDUC (@0 op VECTOR_CST) as REDUC (@0) op REDUC (VECTOR_CST). */ | |
8066 | (for reduc (IFN_REDUC_PLUS IFN_REDUC_MAX IFN_REDUC_MIN IFN_REDUC_FMAX | |
8067 | IFN_REDUC_FMIN IFN_REDUC_AND IFN_REDUC_IOR IFN_REDUC_XOR) | |
8068 | op (plus max min IFN_FMAX IFN_FMIN bit_and bit_ior bit_xor) | |
8069 | (simplify (reduc (op @0 VECTOR_CST@1)) | |
8070 | (op (reduc:type @0) (reduc:type @1)))) | |
8071 | ||
b2bb611d TC |
8072 | /* Simplify vector floating point operations of alternating sub/add pairs |
8073 | into using an fneg of a wider element type followed by a normal add. | |
8074 | under IEEE 754 the fneg of the wider type will negate every even entry | |
8075 | and when doing an add we get a sub of the even and add of every odd | |
8076 | elements. */ | |
8077 | (simplify | |
8078 | (vec_perm (plus:c @0 @1) (minus @0 @1) VECTOR_CST@2) | |
8079 | (if (!VECTOR_INTEGER_TYPE_P (type) | |
8080 | && !FLOAT_WORDS_BIG_ENDIAN) | |
8081 | (with | |
8082 | { | |
8083 | /* Build a vector of integers from the tree mask. */ | |
8084 | vec_perm_builder builder; | |
b2bb611d | 8085 | } |
2044cf2d | 8086 | (if (tree_to_vec_perm_builder (&builder, @2)) |
b2bb611d TC |
8087 | (with |
8088 | { | |
2044cf2d TC |
8089 | /* Create a vec_perm_indices for the integer vector. */ |
8090 | poly_uint64 nelts = TYPE_VECTOR_SUBPARTS (type); | |
8091 | vec_perm_indices sel (builder, 2, nelts); | |
b2bb611d TC |
8092 | machine_mode vec_mode = TYPE_MODE (type); |
8093 | machine_mode wide_mode; | |
1bc7efa9 TC |
8094 | scalar_mode wide_elt_mode; |
8095 | poly_uint64 wide_nunits; | |
8096 | scalar_mode inner_mode = GET_MODE_INNER (vec_mode); | |
b2bb611d | 8097 | } |
2044cf2d | 8098 | (if (sel.series_p (0, 2, 0, 2) |
07fc3491 | 8099 | && sel.series_p (1, 2, nelts + 1, 2) |
1bc7efa9 TC |
8100 | && GET_MODE_2XWIDER_MODE (inner_mode).exists (&wide_elt_mode) |
8101 | && multiple_p (GET_MODE_NUNITS (vec_mode), 2, &wide_nunits) | |
8102 | && related_vector_mode (vec_mode, wide_elt_mode, | |
8103 | wide_nunits).exists (&wide_mode)) | |
2044cf2d TC |
8104 | (with |
8105 | { | |
8106 | tree stype | |
8107 | = lang_hooks.types.type_for_mode (GET_MODE_INNER (wide_mode), | |
8108 | TYPE_UNSIGNED (type)); | |
8109 | tree ntype = build_vector_type_for_mode (stype, wide_mode); | |
8110 | ||
8111 | /* The format has to be a non-extended ieee format. */ | |
8112 | const struct real_format *fmt_old = FLOAT_MODE_FORMAT (vec_mode); | |
8113 | const struct real_format *fmt_new = FLOAT_MODE_FORMAT (wide_mode); | |
8114 | } | |
8115 | (if (TYPE_MODE (stype) != BLKmode | |
8116 | && VECTOR_TYPE_P (ntype) | |
8117 | && fmt_old != NULL | |
8118 | && fmt_new != NULL) | |
8119 | (with | |
8120 | { | |
8121 | /* If the target doesn't support v1xx vectors, try using | |
8122 | scalar mode xx instead. */ | |
8123 | if (known_eq (GET_MODE_NUNITS (wide_mode), 1) | |
8124 | && !target_supports_op_p (ntype, NEGATE_EXPR, optab_vector)) | |
8125 | ntype = stype; | |
8126 | } | |
8127 | (if (fmt_new->signbit_rw | |
8128 | == fmt_old->signbit_rw + GET_MODE_UNIT_BITSIZE (vec_mode) | |
8129 | && fmt_new->signbit_rw == fmt_new->signbit_ro | |
8130 | && targetm.can_change_mode_class (TYPE_MODE (ntype), TYPE_MODE (type), ALL_REGS) | |
8131 | && ((optimize_vectors_before_lowering_p () && VECTOR_TYPE_P (ntype)) | |
8132 | || target_supports_op_p (ntype, NEGATE_EXPR, optab_vector))) | |
8133 | (plus (view_convert:type (negate (view_convert:ntype @1))) @0))))))))))) | |
b2bb611d | 8134 | |
ebd733a7 RB |
8135 | (simplify |
8136 | (vec_perm @0 @1 VECTOR_CST@2) | |
8137 | (with | |
8138 | { | |
8139 | tree op0 = @0, op1 = @1, op2 = @2; | |
ae8decf1 PK |
8140 | machine_mode result_mode = TYPE_MODE (type); |
8141 | machine_mode op_mode = TYPE_MODE (TREE_TYPE (op0)); | |
ebd733a7 RB |
8142 | |
8143 | /* Build a vector of integers from the tree mask. */ | |
8144 | vec_perm_builder builder; | |
ebd733a7 | 8145 | } |
786e4c02 RB |
8146 | (if (tree_to_vec_perm_builder (&builder, op2)) |
8147 | (with | |
8148 | { | |
8149 | /* Create a vec_perm_indices for the integer vector. */ | |
8150 | poly_uint64 nelts = TYPE_VECTOR_SUBPARTS (type); | |
8151 | bool single_arg = (op0 == op1); | |
8152 | vec_perm_indices sel (builder, single_arg ? 1 : 2, nelts); | |
8153 | } | |
8154 | (if (sel.series_p (0, 1, 0, 1)) | |
8155 | { op0; } | |
8156 | (if (sel.series_p (0, 1, nelts, 1)) | |
8157 | { op1; } | |
8158 | (with | |
8159 | { | |
8160 | if (!single_arg) | |
8161 | { | |
8162 | if (sel.all_from_input_p (0)) | |
8163 | op1 = op0; | |
8164 | else if (sel.all_from_input_p (1)) | |
8165 | { | |
8166 | op0 = op1; | |
8167 | sel.rotate_inputs (1); | |
8168 | } | |
8169 | else if (known_ge (poly_uint64 (sel[0]), nelts)) | |
8170 | { | |
8171 | std::swap (op0, op1); | |
8172 | sel.rotate_inputs (1); | |
8173 | } | |
8174 | } | |
8175 | gassign *def; | |
8176 | tree cop0 = op0, cop1 = op1; | |
8177 | if (TREE_CODE (op0) == SSA_NAME | |
8178 | && (def = dyn_cast <gassign *> (SSA_NAME_DEF_STMT (op0))) | |
8179 | && gimple_assign_rhs_code (def) == CONSTRUCTOR) | |
8180 | cop0 = gimple_assign_rhs1 (def); | |
8181 | if (TREE_CODE (op1) == SSA_NAME | |
8182 | && (def = dyn_cast <gassign *> (SSA_NAME_DEF_STMT (op1))) | |
8183 | && gimple_assign_rhs_code (def) == CONSTRUCTOR) | |
8184 | cop1 = gimple_assign_rhs1 (def); | |
8185 | tree t; | |
8186 | } | |
8187 | (if ((TREE_CODE (cop0) == VECTOR_CST | |
8188 | || TREE_CODE (cop0) == CONSTRUCTOR) | |
8189 | && (TREE_CODE (cop1) == VECTOR_CST | |
8190 | || TREE_CODE (cop1) == CONSTRUCTOR) | |
8191 | && (t = fold_vec_perm (type, cop0, cop1, sel))) | |
8192 | { t; } | |
8193 | (with | |
8194 | { | |
8195 | bool changed = (op0 == op1 && !single_arg); | |
8196 | tree ins = NULL_TREE; | |
8197 | unsigned at = 0; | |
8198 | ||
8199 | /* See if the permutation is performing a single element | |
8200 | insert from a CONSTRUCTOR or constant and use a BIT_INSERT_EXPR | |
8201 | in that case. But only if the vector mode is supported, | |
8202 | otherwise this is invalid GIMPLE. */ | |
8203 | if (op_mode != BLKmode | |
8204 | && (TREE_CODE (cop0) == VECTOR_CST | |
8205 | || TREE_CODE (cop0) == CONSTRUCTOR | |
8206 | || TREE_CODE (cop1) == VECTOR_CST | |
8207 | || TREE_CODE (cop1) == CONSTRUCTOR)) | |
ebd733a7 | 8208 | { |
786e4c02 RB |
8209 | bool insert_first_p = sel.series_p (1, 1, nelts + 1, 1); |
8210 | if (insert_first_p) | |
8211 | { | |
8212 | /* After canonicalizing the first elt to come from the | |
8213 | first vector we only can insert the first elt from | |
8214 | the first vector. */ | |
8215 | at = 0; | |
8216 | if ((ins = fold_read_from_vector (cop0, sel[0]))) | |
8217 | op0 = op1; | |
8218 | } | |
8219 | /* The above can fail for two-element vectors which always | |
8220 | appear to insert the first element, so try inserting | |
8221 | into the second lane as well. For more than two | |
8222 | elements that's wasted time. */ | |
8223 | if (!insert_first_p || (!ins && maybe_eq (nelts, 2u))) | |
8224 | { | |
8225 | unsigned int encoded_nelts = sel.encoding ().encoded_nelts (); | |
8226 | for (at = 0; at < encoded_nelts; ++at) | |
8227 | if (maybe_ne (sel[at], at)) | |
8228 | break; | |
8229 | if (at < encoded_nelts | |
8230 | && (known_eq (at + 1, nelts) | |
8231 | || sel.series_p (at + 1, 1, at + 1, 1))) | |
8232 | { | |
8233 | if (known_lt (poly_uint64 (sel[at]), nelts)) | |
8234 | ins = fold_read_from_vector (cop0, sel[at]); | |
8235 | else | |
8236 | ins = fold_read_from_vector (cop1, sel[at] - nelts); | |
8237 | } | |
8238 | } | |
ebd733a7 | 8239 | } |
786e4c02 RB |
8240 | |
8241 | /* Generate a canonical form of the selector. */ | |
8242 | if (!ins && sel.encoding () != builder) | |
4f8b89f0 | 8243 | { |
786e4c02 RB |
8244 | /* Some targets are deficient and fail to expand a single |
8245 | argument permutation while still allowing an equivalent | |
8246 | 2-argument version. */ | |
8247 | tree oldop2 = op2; | |
8248 | if (sel.ninputs () == 2 | |
8249 | || can_vec_perm_const_p (result_mode, op_mode, sel, false)) | |
8250 | op2 = vec_perm_indices_to_tree (TREE_TYPE (op2), sel); | |
8251 | else | |
8252 | { | |
8253 | vec_perm_indices sel2 (builder, 2, nelts); | |
8254 | if (can_vec_perm_const_p (result_mode, op_mode, sel2, false)) | |
8255 | op2 = vec_perm_indices_to_tree (TREE_TYPE (op2), sel2); | |
8256 | else | |
8257 | /* Not directly supported with either encoding, | |
8258 | so use the preferred form. */ | |
8259 | op2 = vec_perm_indices_to_tree (TREE_TYPE (op2), sel); | |
8260 | } | |
8261 | if (!operand_equal_p (op2, oldop2, 0)) | |
8262 | changed = true; | |
4f8b89f0 | 8263 | } |
786e4c02 RB |
8264 | } |
8265 | (if (ins) | |
8266 | (bit_insert { op0; } { ins; } | |
8267 | { bitsize_int (at * vector_element_bits (type)); }) | |
8268 | (if (changed) | |
8269 | (vec_perm { op0; } { op1; } { op2; })))))))))))) | |
21caa1a2 PK |
8270 | |
8271 | /* VEC_PERM_EXPR (v, v, mask) -> v where v contains same element. */ | |
8272 | ||
49fb0af9 RS |
8273 | (match vec_same_elem_p |
8274 | (vec_duplicate @0)) | |
8275 | ||
8276 | (match vec_same_elem_p | |
8277 | CONSTRUCTOR@0 | |
49bf49bb RB |
8278 | (if (TREE_CODE (@0) == SSA_NAME |
8279 | && uniform_vector_p (gimple_assign_rhs1 (SSA_NAME_DEF_STMT (@0)))))) | |
49fb0af9 | 8280 | |
21caa1a2 PK |
8281 | (match vec_same_elem_p |
8282 | @0 | |
8283 | (if (uniform_vector_p (@0)))) | |
8284 | ||
21caa1a2 PK |
8285 | |
8286 | (simplify | |
8287 | (vec_perm vec_same_elem_p@0 @0 @1) | |
8288 | @0) | |
b937050d | 8289 | |
49fb0af9 RS |
8290 | /* Push VEC_PERM earlier if that may help FMA perception (PR101895). */ |
8291 | (simplify | |
8292 | (plus:c (vec_perm:s (mult:c@0 @1 vec_same_elem_p@2) @0 @3) @4) | |
8293 | (if (TREE_CODE (@0) == SSA_NAME && num_imm_uses (@0) == 2) | |
8294 | (plus (mult (vec_perm @1 @1 @3) @2) @4))) | |
8295 | (simplify | |
8296 | (minus (vec_perm:s (mult:c@0 @1 vec_same_elem_p@2) @0 @3) @4) | |
8297 | (if (TREE_CODE (@0) == SSA_NAME && num_imm_uses (@0) == 2) | |
8298 | (minus (mult (vec_perm @1 @1 @3) @2) @4))) | |
8299 | ||
8300 | ||
b88adba7 LX |
8301 | /* Merge |
8302 | c = VEC_PERM_EXPR <a, b, VCST0>; | |
8303 | d = VEC_PERM_EXPR <c, c, VCST1>; | |
8304 | to | |
8305 | d = VEC_PERM_EXPR <a, b, NEW_VCST>; */ | |
8306 | ||
8307 | (simplify | |
8308 | (vec_perm (vec_perm@0 @1 @2 VECTOR_CST@3) @0 VECTOR_CST@4) | |
786e4c02 RB |
8309 | (if (TYPE_VECTOR_SUBPARTS (type).is_constant ()) |
8310 | (with | |
8311 | { | |
8312 | machine_mode result_mode = TYPE_MODE (type); | |
8313 | machine_mode op_mode = TYPE_MODE (TREE_TYPE (@1)); | |
8314 | int nelts = TYPE_VECTOR_SUBPARTS (type).to_constant (); | |
8315 | vec_perm_builder builder0; | |
8316 | vec_perm_builder builder1; | |
8317 | vec_perm_builder builder2 (nelts, nelts, 1); | |
8318 | } | |
8319 | (if (tree_to_vec_perm_builder (&builder0, @3) | |
8320 | && tree_to_vec_perm_builder (&builder1, @4)) | |
8321 | (with | |
8322 | { | |
8323 | vec_perm_indices sel0 (builder0, 2, nelts); | |
8324 | vec_perm_indices sel1 (builder1, 1, nelts); | |
b88adba7 | 8325 | |
786e4c02 RB |
8326 | for (int i = 0; i < nelts; i++) |
8327 | builder2.quick_push (sel0[sel1[i].to_constant ()]); | |
b88adba7 | 8328 | |
786e4c02 | 8329 | vec_perm_indices sel2 (builder2, 2, nelts); |
b88adba7 | 8330 | |
786e4c02 | 8331 | tree op0 = NULL_TREE; |
fa553ff2 JJ |
8332 | /* If the new VEC_PERM_EXPR can't be handled but both |
8333 | original VEC_PERM_EXPRs can, punt. | |
8334 | If one or both of the original VEC_PERM_EXPRs can't be | |
8335 | handled and the new one can't be either, don't increase | |
8336 | number of VEC_PERM_EXPRs that can't be handled. */ | |
8337 | if (can_vec_perm_const_p (result_mode, op_mode, sel2, false) | |
8338 | || (single_use (@0) | |
8339 | ? (!can_vec_perm_const_p (result_mode, op_mode, sel0, false) | |
8340 | || !can_vec_perm_const_p (result_mode, op_mode, sel1, false)) | |
8341 | : !can_vec_perm_const_p (result_mode, op_mode, sel1, false))) | |
786e4c02 RB |
8342 | op0 = vec_perm_indices_to_tree (TREE_TYPE (@4), sel2); |
8343 | } | |
8344 | (if (op0) | |
8345 | (vec_perm @1 @2 { op0; }))))))) | |
b88adba7 LX |
8346 | |
8347 | ||
b937050d WD |
8348 | /* Match count trailing zeroes for simplify_count_trailing_zeroes in fwprop. |
8349 | The canonical form is array[((x & -x) * C) >> SHIFT] where C is a magic | |
8350 | constant which when multiplied by a power of 2 contains a unique value | |
8351 | in the top 5 or 6 bits. This is then indexed into a table which maps it | |
8352 | to the number of trailing zeroes. */ | |
8353 | (match (ctz_table_index @1 @2 @3) | |
8354 | (rshift (mult (bit_and:c (negate @1) @1) INTEGER_CST@2) INTEGER_CST@3)) | |
7e204bd2 | 8355 | |
8356 | (match (cond_expr_convert_p @0 @2 @3 @6) | |
8357 | (cond (simple_comparison@6 @0 @1) (convert@4 @2) (convert@5 @3)) | |
8358 | (if (INTEGRAL_TYPE_P (type) | |
8359 | && INTEGRAL_TYPE_P (TREE_TYPE (@2)) | |
8360 | && INTEGRAL_TYPE_P (TREE_TYPE (@0)) | |
8361 | && INTEGRAL_TYPE_P (TREE_TYPE (@3)) | |
8362 | && TYPE_PRECISION (type) != TYPE_PRECISION (TREE_TYPE (@0)) | |
8363 | && TYPE_PRECISION (TREE_TYPE (@0)) | |
8364 | == TYPE_PRECISION (TREE_TYPE (@2)) | |
8365 | && TYPE_PRECISION (TREE_TYPE (@0)) | |
8366 | == TYPE_PRECISION (TREE_TYPE (@3)) | |
754dce90 | 8367 | /* For vect_recog_cond_expr_convert_pattern, @2 and @3 can differ in |
8368 | signess when convert is truncation, but not ok for extension since | |
8369 | it's sign_extend vs zero_extend. */ | |
8370 | && (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (type) | |
8371 | || (TYPE_UNSIGNED (TREE_TYPE (@2)) | |
8372 | == TYPE_UNSIGNED (TREE_TYPE (@3)))) | |
7e204bd2 | 8373 | && single_use (@4) |
8374 | && single_use (@5)))) | |
9d1336d9 | 8375 | |
8376 | (for bit_op (bit_and bit_ior bit_xor) | |
8377 | (match (bitwise_induction_p @0 @2 @3) | |
8378 | (bit_op:c | |
8379 | (nop_convert1? (bit_not2?@0 (convert3? (lshift integer_onep@1 @2)))) | |
8380 | @3))) | |
8381 | ||
8382 | (match (bitwise_induction_p @0 @2 @3) | |
8383 | (bit_not | |
8384 | (nop_convert1? (bit_xor@0 (convert2? (lshift integer_onep@1 @2)) @3)))) | |
d9fa599d SF |
8385 | |
8386 | /* n - (((n > C1) ? n : C1) & -C2) -> n & C1 for unsigned case. | |
8387 | n - (((n > C1) ? n : C1) & -C2) -> (n <= C1) ? n : (n & C1) for signed case. */ | |
8388 | (simplify | |
8389 | (minus @0 (bit_and (max @0 INTEGER_CST@1) INTEGER_CST@2)) | |
8390 | (with { auto i = wi::neg (wi::to_wide (@2)); } | |
8391 | /* Check if -C2 is a power of 2 and C1 = -C2 - 1. */ | |
8392 | (if (wi::popcount (i) == 1 | |
8393 | && (wi::to_wide (@1)) == (i - 1)) | |
8394 | (if (TYPE_UNSIGNED (TREE_TYPE (@0))) | |
8395 | (bit_and @0 @1) | |
8396 | (cond (le @0 @1) @0 (bit_and @0 @1)))))) | |
39579ba8 SF |
8397 | |
8398 | /* -x & 1 -> x & 1. */ | |
8399 | (simplify | |
07cc4c1d JJ |
8400 | (bit_and (negate @0) integer_onep@1) |
8401 | (if (!TYPE_OVERFLOW_SANITIZED (type)) | |
8402 | (bit_and @0 @1))) | |
dc95e1e9 HW |
8403 | |
8404 | /* Optimize | |
8405 | c1 = VEC_PERM_EXPR (a, a, mask) | |
8406 | c2 = VEC_PERM_EXPR (b, b, mask) | |
8407 | c3 = c1 op c2 | |
8408 | --> | |
8409 | c = a op b | |
8410 | c3 = VEC_PERM_EXPR (c, c, mask) | |
8411 | For all integer non-div operations. */ | |
8412 | (for op (plus minus mult bit_and bit_ior bit_xor | |
8413 | lshift rshift) | |
8414 | (simplify | |
8415 | (op (vec_perm @0 @0 @2) (vec_perm @1 @1 @2)) | |
8416 | (if (VECTOR_INTEGER_TYPE_P (type)) | |
cbe31306 | 8417 | (vec_perm (op@3 @0 @1) @3 @2)))) |
dc95e1e9 HW |
8418 | |
8419 | /* Similar for float arithmetic when permutation constant covers | |
8420 | all vector elements. */ | |
8421 | (for op (plus minus mult) | |
8422 | (simplify | |
8423 | (op (vec_perm @0 @0 VECTOR_CST@2) (vec_perm @1 @1 VECTOR_CST@2)) | |
8424 | (if (VECTOR_FLOAT_TYPE_P (type) | |
8425 | && TYPE_VECTOR_SUBPARTS (type).is_constant ()) | |
8426 | (with | |
8427 | { | |
8428 | tree perm_cst = @2; | |
8429 | vec_perm_builder builder; | |
8430 | bool full_perm_p = false; | |
8431 | if (tree_to_vec_perm_builder (&builder, perm_cst)) | |
8432 | { | |
8433 | unsigned HOST_WIDE_INT nelts; | |
8434 | ||
8435 | nelts = TYPE_VECTOR_SUBPARTS (type).to_constant (); | |
8436 | /* Create a vec_perm_indices for the VECTOR_CST. */ | |
8437 | vec_perm_indices sel (builder, 1, nelts); | |
8438 | ||
8439 | /* Check if perm indices covers all vector elements. */ | |
8440 | if (sel.encoding ().encoded_full_vector_p ()) | |
8441 | { | |
8442 | auto_sbitmap seen (nelts); | |
ee892832 AP |
8443 | bitmap_clear (seen); |
8444 | ||
dc95e1e9 HW |
8445 | unsigned HOST_WIDE_INT count = 0, i; |
8446 | ||
8447 | for (i = 0; i < nelts; i++) | |
8448 | { | |
8449 | if (!bitmap_set_bit (seen, sel[i].to_constant ())) | |
8450 | break; | |
8451 | count++; | |
8452 | } | |
8453 | full_perm_p = count == nelts; | |
8454 | } | |
8455 | } | |
8456 | } | |
8457 | (if (full_perm_p) | |
cbe31306 | 8458 | (vec_perm (op@3 @0 @1) @3 @2)))))) |