]>
Commit | Line | Data |
---|---|---|
2bc77e10 | 1 | /* Fold a constant sub-tree into a single node for C-compiler |
22331643 | 2 | Copyright (C) 1987, 1988, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, |
7cf0dbf3 | 3 | 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010 |
eddad94a | 4 | Free Software Foundation, Inc. |
2bc77e10 | 5 | |
f12b58b3 | 6 | This file is part of GCC. |
2bc77e10 | 7 | |
f12b58b3 | 8 | GCC is free software; you can redistribute it and/or modify it under |
9 | the terms of the GNU General Public License as published by the Free | |
8c4c00c1 | 10 | Software Foundation; either version 3, or (at your option) any later |
f12b58b3 | 11 | version. |
2bc77e10 | 12 | |
f12b58b3 | 13 | GCC is distributed in the hope that it will be useful, but WITHOUT ANY |
14 | WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
15 | FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
16 | for more details. | |
2bc77e10 | 17 | |
18 | You should have received a copy of the GNU General Public License | |
8c4c00c1 | 19 | along with GCC; see the file COPYING3. If not see |
20 | <http://www.gnu.org/licenses/>. */ | |
2bc77e10 | 21 | |
4bbea254 | 22 | /*@@ This file should be rewritten to use an arbitrary precision |
2bc77e10 | 23 | @@ representation for "struct tree_int_cst" and "struct tree_real_cst". |
24 | @@ Perhaps the routines could also be used for bc/dc, and made a lib. | |
25 | @@ The routines that translate from the ap rep should | |
26 | @@ warn if precision et. al. is lost. | |
27 | @@ This would also make life easier when this technology is used | |
28 | @@ for cross-compilers. */ | |
29 | ||
30384dcf | 30 | /* The entry points in this file are fold, size_int_wide, size_binop |
eddad94a | 31 | and force_fit_type_double. |
2bc77e10 | 32 | |
33 | fold takes a tree as argument and returns a simplified tree. | |
34 | ||
35 | size_binop takes a tree code for an arithmetic operation | |
36 | and two operands that are trees, and produces a tree for the | |
37 | result, assuming the type comes from `sizetype'. | |
38 | ||
39 | size_int takes an integer value, and creates a tree constant | |
6e44befc | 40 | with type from `sizetype'. |
41 | ||
eddad94a | 42 | force_fit_type_double takes a constant, an overflowable flag and a |
43 | prior overflow indicator. It forces the value to fit the type and | |
44 | sets TREE_OVERFLOW. | |
45 | ||
35cc02b5 | 46 | Note: Since the folders get called on non-gimple code as well as |
47 | gimple code, we need to handle GIMPLE tuples as well as their | |
48 | corresponding tree equivalents. */ | |
6e44befc | 49 | |
0dbd1c74 | 50 | #include "config.h" |
5ee8fe30 | 51 | #include "system.h" |
805e22b2 | 52 | #include "coretypes.h" |
53 | #include "tm.h" | |
2bc77e10 | 54 | #include "flags.h" |
55 | #include "tree.h" | |
dae0b5cb | 56 | #include "realmpfr.h" |
0f9685e4 | 57 | #include "rtl.h" |
aed0bd19 | 58 | #include "expr.h" |
7953c610 | 59 | #include "tm_p.h" |
ffdf1c47 | 60 | #include "target.h" |
12874aaf | 61 | #include "toplev.h" |
add6ee5e | 62 | #include "intl.h" |
1bfd55c5 | 63 | #include "ggc.h" |
15d769aa | 64 | #include "hashtab.h" |
20325f61 | 65 | #include "langhooks.h" |
fc3df357 | 66 | #include "md5.h" |
75a70cf9 | 67 | #include "gimple.h" |
2bc77e10 | 68 | |
80777cd8 | 69 | /* Nonzero if we are folding constants inside an initializer; zero |
47be647d | 70 | otherwise. */ |
71 | int folding_initializer = 0; | |
72 | ||
318a728f | 73 | /* The following constants represent a bit based encoding of GCC's |
74 | comparison operators. This encoding simplifies transformations | |
75 | on relational comparison operators, such as AND and OR. */ | |
76 | enum comparison_code { | |
77 | COMPCODE_FALSE = 0, | |
78 | COMPCODE_LT = 1, | |
79 | COMPCODE_EQ = 2, | |
80 | COMPCODE_LE = 3, | |
81 | COMPCODE_GT = 4, | |
82 | COMPCODE_LTGT = 5, | |
83 | COMPCODE_GE = 6, | |
84 | COMPCODE_ORD = 7, | |
85 | COMPCODE_UNORD = 8, | |
86 | COMPCODE_UNLT = 9, | |
87 | COMPCODE_UNEQ = 10, | |
88 | COMPCODE_UNLE = 11, | |
89 | COMPCODE_UNGT = 12, | |
90 | COMPCODE_NE = 13, | |
91 | COMPCODE_UNGE = 14, | |
92 | COMPCODE_TRUE = 15 | |
93 | }; | |
94 | ||
bd214d13 | 95 | static bool negate_mathfn_p (enum built_in_function); |
de1b648b | 96 | static bool negate_expr_p (tree); |
97 | static tree negate_expr (tree); | |
98 | static tree split_tree (tree, enum tree_code, tree *, tree *, tree *, int); | |
389dd41b | 99 | static tree associate_trees (location_t, tree, tree, enum tree_code, tree); |
de1b648b | 100 | static tree const_binop (enum tree_code, tree, tree, int); |
318a728f | 101 | static enum comparison_code comparison_to_compcode (enum tree_code); |
102 | static enum tree_code compcode_to_comparison (enum comparison_code); | |
de1b648b | 103 | static int operand_equal_for_comparison_p (tree, tree, tree); |
104 | static int twoval_comparison_p (tree, tree *, tree *, int *); | |
389dd41b | 105 | static tree eval_subst (location_t, tree, tree, tree, tree, tree); |
106 | static tree pedantic_omit_one_operand_loc (location_t, tree, tree, tree); | |
107 | static tree distribute_bit_expr (location_t, enum tree_code, tree, tree, tree); | |
108 | static tree make_bit_field_ref (location_t, tree, tree, | |
109 | HOST_WIDE_INT, HOST_WIDE_INT, int); | |
110 | static tree optimize_bit_field_compare (location_t, enum tree_code, | |
111 | tree, tree, tree); | |
112 | static tree decode_field_reference (location_t, tree, HOST_WIDE_INT *, | |
113 | HOST_WIDE_INT *, | |
de1b648b | 114 | enum machine_mode *, int *, int *, |
115 | tree *, tree *); | |
2a64c730 | 116 | static int all_ones_mask_p (const_tree, int); |
b4b34335 | 117 | static tree sign_bit_p (tree, const_tree); |
118 | static int simple_operand_p (const_tree); | |
de1b648b | 119 | static tree range_binop (enum tree_code, tree, tree, int, tree, int); |
66108e20 | 120 | static tree range_predecessor (tree); |
121 | static tree range_successor (tree); | |
9c20c4fc | 122 | extern tree make_range (tree, int *, tree *, tree *, bool *); |
9c20c4fc | 123 | extern bool merge_ranges (int *, tree *, tree *, int, tree, tree, int, |
124 | tree, tree); | |
389dd41b | 125 | static tree fold_range_test (location_t, enum tree_code, tree, tree, tree); |
126 | static tree fold_cond_expr_with_comparison (location_t, tree, tree, tree, tree); | |
de1b648b | 127 | static tree unextend (tree, int, int, tree); |
389dd41b | 128 | static tree fold_truthop (location_t, enum tree_code, tree, tree, tree); |
129 | static tree optimize_minmax_comparison (location_t, enum tree_code, | |
130 | tree, tree, tree); | |
add6ee5e | 131 | static tree extract_muldiv (tree, tree, enum tree_code, tree, bool *); |
132 | static tree extract_muldiv_1 (tree, tree, enum tree_code, tree, bool *); | |
389dd41b | 133 | static tree fold_binary_op_with_conditional_arg (location_t, |
134 | enum tree_code, tree, | |
1ebe9a83 | 135 | tree, tree, |
5fe1fe72 | 136 | tree, tree, int); |
389dd41b | 137 | static tree fold_mathfn_compare (location_t, |
138 | enum built_in_function, enum tree_code, | |
de1b648b | 139 | tree, tree, tree); |
389dd41b | 140 | static tree fold_inf_compare (location_t, enum tree_code, tree, tree, tree); |
141 | static tree fold_div_compare (location_t, enum tree_code, tree, tree, tree); | |
b4b34335 | 142 | static bool reorder_operands_p (const_tree, const_tree); |
9d77437d | 143 | static tree fold_negate_const (tree, tree); |
c183306c | 144 | static tree fold_not_const (tree, tree); |
ad46984d | 145 | static tree fold_relational_const (enum tree_code, tree, tree, tree); |
87de4c68 | 146 | static tree fold_convert_const (enum tree_code, tree, tree); |
5f4092ed | 147 | |
9d77437d | 148 | |
083a2b5e | 149 | /* We know that A1 + B1 = SUM1, using 2's complement arithmetic and ignoring |
150 | overflow. Suppose A, B and SUM have the same respective signs as A1, B1, | |
151 | and SUM1. Then this yields nonzero if overflow occurred during the | |
152 | addition. | |
153 | ||
154 | Overflow occurs if A and B have the same sign, but A and SUM differ in | |
155 | sign. Use `^' to test whether signs differ, and `< 0' to isolate the | |
156 | sign. */ | |
157 | #define OVERFLOW_SUM_SIGN(a, b, sum) ((~((a) ^ (b)) & ((a) ^ (sum))) < 0) | |
2bc77e10 | 158 | \f |
86f023fe | 159 | /* If ARG2 divides ARG1 with zero remainder, carries out the division |
160 | of type CODE and returns the quotient. | |
161 | Otherwise returns NULL_TREE. */ | |
162 | ||
401d1fb3 | 163 | tree |
b4b34335 | 164 | div_if_zero_remainder (enum tree_code code, const_tree arg1, const_tree arg2) |
86f023fe | 165 | { |
41283922 | 166 | double_int quo, rem; |
8bdac511 | 167 | int uns; |
168 | ||
169 | /* The sign of the division is according to operand two, that | |
170 | does the correct thing for POINTER_PLUS_EXPR where we want | |
171 | a signed division. */ | |
172 | uns = TYPE_UNSIGNED (TREE_TYPE (arg2)); | |
173 | if (TREE_CODE (TREE_TYPE (arg2)) == INTEGER_TYPE | |
174 | && TYPE_IS_SIZETYPE (TREE_TYPE (arg2))) | |
175 | uns = false; | |
86f023fe | 176 | |
41283922 | 177 | quo = double_int_divmod (tree_to_double_int (arg1), |
178 | tree_to_double_int (arg2), | |
179 | uns, code, &rem); | |
86f023fe | 180 | |
41283922 | 181 | if (double_int_zero_p (rem)) |
182 | return build_int_cst_wide (TREE_TYPE (arg1), quo.low, quo.high); | |
86f023fe | 183 | |
41283922 | 184 | return NULL_TREE; |
86f023fe | 185 | } |
2bc77e10 | 186 | \f |
80777cd8 | 187 | /* This is nonzero if we should defer warnings about undefined |
add6ee5e | 188 | overflow. This facility exists because these warnings are a |
189 | special case. The code to estimate loop iterations does not want | |
190 | to issue any warnings, since it works with expressions which do not | |
191 | occur in user code. Various bits of cleanup code call fold(), but | |
192 | only use the result if it has certain characteristics (e.g., is a | |
193 | constant); that code only wants to issue a warning if the result is | |
194 | used. */ | |
195 | ||
196 | static int fold_deferring_overflow_warnings; | |
197 | ||
198 | /* If a warning about undefined overflow is deferred, this is the | |
199 | warning. Note that this may cause us to turn two warnings into | |
200 | one, but that is fine since it is sufficient to only give one | |
201 | warning per expression. */ | |
202 | ||
203 | static const char* fold_deferred_overflow_warning; | |
204 | ||
205 | /* If a warning about undefined overflow is deferred, this is the | |
206 | level at which the warning should be emitted. */ | |
207 | ||
208 | static enum warn_strict_overflow_code fold_deferred_overflow_code; | |
209 | ||
210 | /* Start deferring overflow warnings. We could use a stack here to | |
211 | permit nested calls, but at present it is not necessary. */ | |
212 | ||
213 | void | |
214 | fold_defer_overflow_warnings (void) | |
215 | { | |
216 | ++fold_deferring_overflow_warnings; | |
217 | } | |
218 | ||
219 | /* Stop deferring overflow warnings. If there is a pending warning, | |
220 | and ISSUE is true, then issue the warning if appropriate. STMT is | |
221 | the statement with which the warning should be associated (used for | |
222 | location information); STMT may be NULL. CODE is the level of the | |
223 | warning--a warn_strict_overflow_code value. This function will use | |
224 | the smaller of CODE and the deferred code when deciding whether to | |
225 | issue the warning. CODE may be zero to mean to always use the | |
226 | deferred code. */ | |
227 | ||
228 | void | |
75a70cf9 | 229 | fold_undefer_overflow_warnings (bool issue, const_gimple stmt, int code) |
add6ee5e | 230 | { |
231 | const char *warnmsg; | |
232 | location_t locus; | |
233 | ||
234 | gcc_assert (fold_deferring_overflow_warnings > 0); | |
235 | --fold_deferring_overflow_warnings; | |
236 | if (fold_deferring_overflow_warnings > 0) | |
237 | { | |
238 | if (fold_deferred_overflow_warning != NULL | |
239 | && code != 0 | |
240 | && code < (int) fold_deferred_overflow_code) | |
8458f4ca | 241 | fold_deferred_overflow_code = (enum warn_strict_overflow_code) code; |
add6ee5e | 242 | return; |
243 | } | |
244 | ||
245 | warnmsg = fold_deferred_overflow_warning; | |
246 | fold_deferred_overflow_warning = NULL; | |
247 | ||
248 | if (!issue || warnmsg == NULL) | |
249 | return; | |
250 | ||
75a70cf9 | 251 | if (gimple_no_warning_p (stmt)) |
72c59a18 | 252 | return; |
253 | ||
add6ee5e | 254 | /* Use the smallest code level when deciding to issue the |
255 | warning. */ | |
256 | if (code == 0 || code > (int) fold_deferred_overflow_code) | |
257 | code = fold_deferred_overflow_code; | |
258 | ||
259 | if (!issue_strict_overflow_warning (code)) | |
260 | return; | |
261 | ||
75a70cf9 | 262 | if (stmt == NULL) |
add6ee5e | 263 | locus = input_location; |
264 | else | |
75a70cf9 | 265 | locus = gimple_location (stmt); |
5fb6a912 | 266 | warning_at (locus, OPT_Wstrict_overflow, "%s", warnmsg); |
add6ee5e | 267 | } |
268 | ||
269 | /* Stop deferring overflow warnings, ignoring any deferred | |
270 | warnings. */ | |
271 | ||
272 | void | |
273 | fold_undefer_and_ignore_overflow_warnings (void) | |
274 | { | |
75a70cf9 | 275 | fold_undefer_overflow_warnings (false, NULL, 0); |
add6ee5e | 276 | } |
277 | ||
278 | /* Whether we are deferring overflow warnings. */ | |
279 | ||
280 | bool | |
281 | fold_deferring_overflow_warnings_p (void) | |
282 | { | |
283 | return fold_deferring_overflow_warnings > 0; | |
284 | } | |
285 | ||
286 | /* This is called when we fold something based on the fact that signed | |
287 | overflow is undefined. */ | |
288 | ||
289 | static void | |
290 | fold_overflow_warning (const char* gmsgid, enum warn_strict_overflow_code wc) | |
291 | { | |
add6ee5e | 292 | if (fold_deferring_overflow_warnings > 0) |
293 | { | |
294 | if (fold_deferred_overflow_warning == NULL | |
295 | || wc < fold_deferred_overflow_code) | |
296 | { | |
297 | fold_deferred_overflow_warning = gmsgid; | |
298 | fold_deferred_overflow_code = wc; | |
299 | } | |
300 | } | |
301 | else if (issue_strict_overflow_warning (wc)) | |
302 | warning (OPT_Wstrict_overflow, gmsgid); | |
303 | } | |
304 | \f | |
352e5c7a | 305 | /* Return true if the built-in mathematical function specified by CODE |
306 | is odd, i.e. -f(x) == f(-x). */ | |
bd214d13 | 307 | |
308 | static bool | |
309 | negate_mathfn_p (enum built_in_function code) | |
310 | { | |
311 | switch (code) | |
312 | { | |
4f35b1fc | 313 | CASE_FLT_FN (BUILT_IN_ASIN): |
314 | CASE_FLT_FN (BUILT_IN_ASINH): | |
315 | CASE_FLT_FN (BUILT_IN_ATAN): | |
316 | CASE_FLT_FN (BUILT_IN_ATANH): | |
503733d5 | 317 | CASE_FLT_FN (BUILT_IN_CASIN): |
318 | CASE_FLT_FN (BUILT_IN_CASINH): | |
319 | CASE_FLT_FN (BUILT_IN_CATAN): | |
320 | CASE_FLT_FN (BUILT_IN_CATANH): | |
4f35b1fc | 321 | CASE_FLT_FN (BUILT_IN_CBRT): |
503733d5 | 322 | CASE_FLT_FN (BUILT_IN_CPROJ): |
323 | CASE_FLT_FN (BUILT_IN_CSIN): | |
324 | CASE_FLT_FN (BUILT_IN_CSINH): | |
325 | CASE_FLT_FN (BUILT_IN_CTAN): | |
326 | CASE_FLT_FN (BUILT_IN_CTANH): | |
cacdc1af | 327 | CASE_FLT_FN (BUILT_IN_ERF): |
328 | CASE_FLT_FN (BUILT_IN_LLROUND): | |
329 | CASE_FLT_FN (BUILT_IN_LROUND): | |
330 | CASE_FLT_FN (BUILT_IN_ROUND): | |
4f35b1fc | 331 | CASE_FLT_FN (BUILT_IN_SIN): |
332 | CASE_FLT_FN (BUILT_IN_SINH): | |
333 | CASE_FLT_FN (BUILT_IN_TAN): | |
334 | CASE_FLT_FN (BUILT_IN_TANH): | |
cacdc1af | 335 | CASE_FLT_FN (BUILT_IN_TRUNC): |
bd214d13 | 336 | return true; |
337 | ||
cacdc1af | 338 | CASE_FLT_FN (BUILT_IN_LLRINT): |
339 | CASE_FLT_FN (BUILT_IN_LRINT): | |
340 | CASE_FLT_FN (BUILT_IN_NEARBYINT): | |
341 | CASE_FLT_FN (BUILT_IN_RINT): | |
342 | return !flag_rounding_math; | |
48e1416a | 343 | |
bd214d13 | 344 | default: |
345 | break; | |
346 | } | |
347 | return false; | |
348 | } | |
349 | ||
bb445479 | 350 | /* Check whether we may negate an integer constant T without causing |
351 | overflow. */ | |
352 | ||
353 | bool | |
b7bf20db | 354 | may_negate_without_overflow_p (const_tree t) |
bb445479 | 355 | { |
356 | unsigned HOST_WIDE_INT val; | |
357 | unsigned int prec; | |
358 | tree type; | |
359 | ||
fdada98f | 360 | gcc_assert (TREE_CODE (t) == INTEGER_CST); |
bb445479 | 361 | |
362 | type = TREE_TYPE (t); | |
363 | if (TYPE_UNSIGNED (type)) | |
364 | return false; | |
365 | ||
366 | prec = TYPE_PRECISION (type); | |
367 | if (prec > HOST_BITS_PER_WIDE_INT) | |
368 | { | |
369 | if (TREE_INT_CST_LOW (t) != 0) | |
370 | return true; | |
371 | prec -= HOST_BITS_PER_WIDE_INT; | |
372 | val = TREE_INT_CST_HIGH (t); | |
373 | } | |
374 | else | |
375 | val = TREE_INT_CST_LOW (t); | |
376 | if (prec < HOST_BITS_PER_WIDE_INT) | |
377 | val &= ((unsigned HOST_WIDE_INT) 1 << prec) - 1; | |
378 | return val != ((unsigned HOST_WIDE_INT) 1 << (prec - 1)); | |
379 | } | |
380 | ||
22331643 | 381 | /* Determine whether an expression T can be cheaply negated using |
58b22aa6 | 382 | the function negate_expr without introducing undefined overflow. */ |
22331643 | 383 | |
384 | static bool | |
de1b648b | 385 | negate_expr_p (tree t) |
22331643 | 386 | { |
22331643 | 387 | tree type; |
388 | ||
389 | if (t == 0) | |
390 | return false; | |
391 | ||
392 | type = TREE_TYPE (t); | |
393 | ||
394 | STRIP_SIGN_NOPS (t); | |
395 | switch (TREE_CODE (t)) | |
396 | { | |
397 | case INTEGER_CST: | |
981eb798 | 398 | if (TYPE_OVERFLOW_WRAPS (type)) |
bd214d13 | 399 | return true; |
22331643 | 400 | |
401 | /* Check that -CST will not overflow type. */ | |
bb445479 | 402 | return may_negate_without_overflow_p (t); |
5ea8b65a | 403 | case BIT_NOT_EXPR: |
981eb798 | 404 | return (INTEGRAL_TYPE_P (type) |
405 | && TYPE_OVERFLOW_WRAPS (type)); | |
22331643 | 406 | |
06f0b99c | 407 | case FIXED_CST: |
22331643 | 408 | case NEGATE_EXPR: |
22331643 | 409 | return true; |
410 | ||
53293165 | 411 | case REAL_CST: |
412 | /* We want to canonicalize to positive real constants. Pretend | |
413 | that only negative ones can be easily negated. */ | |
414 | return REAL_VALUE_NEGATIVE (TREE_REAL_CST (t)); | |
415 | ||
bd214d13 | 416 | case COMPLEX_CST: |
417 | return negate_expr_p (TREE_REALPART (t)) | |
418 | && negate_expr_p (TREE_IMAGPART (t)); | |
419 | ||
7c4eaf72 | 420 | case COMPLEX_EXPR: |
421 | return negate_expr_p (TREE_OPERAND (t, 0)) | |
422 | && negate_expr_p (TREE_OPERAND (t, 1)); | |
423 | ||
6da8bb4f | 424 | case CONJ_EXPR: |
425 | return negate_expr_p (TREE_OPERAND (t, 0)); | |
426 | ||
2169cab6 | 427 | case PLUS_EXPR: |
2815dfde | 428 | if (HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (type)) |
429 | || HONOR_SIGNED_ZEROS (TYPE_MODE (type))) | |
2169cab6 | 430 | return false; |
431 | /* -(A + B) -> (-B) - A. */ | |
432 | if (negate_expr_p (TREE_OPERAND (t, 1)) | |
433 | && reorder_operands_p (TREE_OPERAND (t, 0), | |
434 | TREE_OPERAND (t, 1))) | |
435 | return true; | |
436 | /* -(A + B) -> (-A) - B. */ | |
437 | return negate_expr_p (TREE_OPERAND (t, 0)); | |
438 | ||
d842742d | 439 | case MINUS_EXPR: |
440 | /* We can't turn -(A-B) into B-A when we honor signed zeros. */ | |
2815dfde | 441 | return !HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (type)) |
442 | && !HONOR_SIGNED_ZEROS (TYPE_MODE (type)) | |
bd214d13 | 443 | && reorder_operands_p (TREE_OPERAND (t, 0), |
444 | TREE_OPERAND (t, 1)); | |
d842742d | 445 | |
a12ecaaa | 446 | case MULT_EXPR: |
78a8ed03 | 447 | if (TYPE_UNSIGNED (TREE_TYPE (t))) |
a12ecaaa | 448 | break; |
449 | ||
450 | /* Fall through. */ | |
451 | ||
452 | case RDIV_EXPR: | |
453 | if (! HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (TREE_TYPE (t)))) | |
454 | return negate_expr_p (TREE_OPERAND (t, 1)) | |
455 | || negate_expr_p (TREE_OPERAND (t, 0)); | |
456 | break; | |
457 | ||
212f6d6d | 458 | case TRUNC_DIV_EXPR: |
459 | case ROUND_DIV_EXPR: | |
460 | case FLOOR_DIV_EXPR: | |
461 | case CEIL_DIV_EXPR: | |
462 | case EXACT_DIV_EXPR: | |
add6ee5e | 463 | /* In general we can't negate A / B, because if A is INT_MIN and |
464 | B is 1, we may turn this into INT_MIN / -1 which is undefined | |
465 | and actually traps on some architectures. But if overflow is | |
466 | undefined, we can negate, because - (INT_MIN / 1) is an | |
467 | overflow. */ | |
981eb798 | 468 | if (INTEGRAL_TYPE_P (TREE_TYPE (t)) |
469 | && !TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (t))) | |
212f6d6d | 470 | break; |
471 | return negate_expr_p (TREE_OPERAND (t, 1)) | |
472 | || negate_expr_p (TREE_OPERAND (t, 0)); | |
473 | ||
bd214d13 | 474 | case NOP_EXPR: |
475 | /* Negate -((double)float) as (double)(-float). */ | |
476 | if (TREE_CODE (type) == REAL_TYPE) | |
477 | { | |
478 | tree tem = strip_float_extensions (t); | |
479 | if (tem != t) | |
480 | return negate_expr_p (tem); | |
481 | } | |
482 | break; | |
483 | ||
484 | case CALL_EXPR: | |
485 | /* Negate -f(x) as f(-x). */ | |
486 | if (negate_mathfn_p (builtin_mathfn_code (t))) | |
c2f47e15 | 487 | return negate_expr_p (CALL_EXPR_ARG (t, 0)); |
bd214d13 | 488 | break; |
489 | ||
a22fd555 | 490 | case RSHIFT_EXPR: |
491 | /* Optimize -((int)x >> 31) into (unsigned)x >> 31. */ | |
492 | if (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST) | |
493 | { | |
494 | tree op1 = TREE_OPERAND (t, 1); | |
495 | if (TREE_INT_CST_HIGH (op1) == 0 | |
496 | && (unsigned HOST_WIDE_INT) (TYPE_PRECISION (type) - 1) | |
497 | == TREE_INT_CST_LOW (op1)) | |
498 | return true; | |
499 | } | |
500 | break; | |
501 | ||
22331643 | 502 | default: |
503 | break; | |
504 | } | |
505 | return false; | |
506 | } | |
507 | ||
58b22aa6 | 508 | /* Given T, an expression, return a folded tree for -T or NULL_TREE, if no |
509 | simplification is possible. | |
510 | If negate_expr_p would return true for T, NULL_TREE will never be | |
511 | returned. */ | |
2bc77e10 | 512 | |
23ec2d5e | 513 | static tree |
389dd41b | 514 | fold_negate_expr (location_t loc, tree t) |
23ec2d5e | 515 | { |
58b22aa6 | 516 | tree type = TREE_TYPE (t); |
23ec2d5e | 517 | tree tem; |
518 | ||
23ec2d5e | 519 | switch (TREE_CODE (t)) |
520 | { | |
5ea8b65a | 521 | /* Convert - (~A) to A + 1. */ |
522 | case BIT_NOT_EXPR: | |
58b22aa6 | 523 | if (INTEGRAL_TYPE_P (type)) |
389dd41b | 524 | return fold_build2_loc (loc, PLUS_EXPR, type, TREE_OPERAND (t, 0), |
5ea8b65a | 525 | build_int_cst (type, 1)); |
3a07b940 | 526 | break; |
48e1416a | 527 | |
23ec2d5e | 528 | case INTEGER_CST: |
9d77437d | 529 | tem = fold_negate_const (t, type); |
20e133d9 | 530 | if (TREE_OVERFLOW (tem) == TREE_OVERFLOW (t) |
981eb798 | 531 | || !TYPE_OVERFLOW_TRAPS (type)) |
23ec2d5e | 532 | return tem; |
533 | break; | |
534 | ||
a12ecaaa | 535 | case REAL_CST: |
9d77437d | 536 | tem = fold_negate_const (t, type); |
a12ecaaa | 537 | /* Two's complement FP formats, such as c4x, may overflow. */ |
f96bd2bf | 538 | if (!TREE_OVERFLOW (tem) || !flag_trapping_math) |
58b22aa6 | 539 | return tem; |
a12ecaaa | 540 | break; |
541 | ||
06f0b99c | 542 | case FIXED_CST: |
543 | tem = fold_negate_const (t, type); | |
544 | return tem; | |
545 | ||
bd214d13 | 546 | case COMPLEX_CST: |
547 | { | |
548 | tree rpart = negate_expr (TREE_REALPART (t)); | |
549 | tree ipart = negate_expr (TREE_IMAGPART (t)); | |
550 | ||
551 | if ((TREE_CODE (rpart) == REAL_CST | |
552 | && TREE_CODE (ipart) == REAL_CST) | |
553 | || (TREE_CODE (rpart) == INTEGER_CST | |
554 | && TREE_CODE (ipart) == INTEGER_CST)) | |
555 | return build_complex (type, rpart, ipart); | |
556 | } | |
557 | break; | |
558 | ||
7c4eaf72 | 559 | case COMPLEX_EXPR: |
560 | if (negate_expr_p (t)) | |
389dd41b | 561 | return fold_build2_loc (loc, COMPLEX_EXPR, type, |
562 | fold_negate_expr (loc, TREE_OPERAND (t, 0)), | |
563 | fold_negate_expr (loc, TREE_OPERAND (t, 1))); | |
7c4eaf72 | 564 | break; |
48e1416a | 565 | |
6da8bb4f | 566 | case CONJ_EXPR: |
567 | if (negate_expr_p (t)) | |
389dd41b | 568 | return fold_build1_loc (loc, CONJ_EXPR, type, |
569 | fold_negate_expr (loc, TREE_OPERAND (t, 0))); | |
6da8bb4f | 570 | break; |
571 | ||
23ec2d5e | 572 | case NEGATE_EXPR: |
58b22aa6 | 573 | return TREE_OPERAND (t, 0); |
23ec2d5e | 574 | |
2169cab6 | 575 | case PLUS_EXPR: |
2815dfde | 576 | if (!HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (type)) |
577 | && !HONOR_SIGNED_ZEROS (TYPE_MODE (type))) | |
2169cab6 | 578 | { |
579 | /* -(A + B) -> (-B) - A. */ | |
580 | if (negate_expr_p (TREE_OPERAND (t, 1)) | |
581 | && reorder_operands_p (TREE_OPERAND (t, 0), | |
582 | TREE_OPERAND (t, 1))) | |
fd96eeef | 583 | { |
584 | tem = negate_expr (TREE_OPERAND (t, 1)); | |
389dd41b | 585 | return fold_build2_loc (loc, MINUS_EXPR, type, |
58b22aa6 | 586 | tem, TREE_OPERAND (t, 0)); |
fd96eeef | 587 | } |
588 | ||
2169cab6 | 589 | /* -(A + B) -> (-A) - B. */ |
590 | if (negate_expr_p (TREE_OPERAND (t, 0))) | |
fd96eeef | 591 | { |
592 | tem = negate_expr (TREE_OPERAND (t, 0)); | |
389dd41b | 593 | return fold_build2_loc (loc, MINUS_EXPR, type, |
58b22aa6 | 594 | tem, TREE_OPERAND (t, 1)); |
fd96eeef | 595 | } |
2169cab6 | 596 | } |
597 | break; | |
598 | ||
23ec2d5e | 599 | case MINUS_EXPR: |
600 | /* - (A - B) -> B - A */ | |
2815dfde | 601 | if (!HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (type)) |
602 | && !HONOR_SIGNED_ZEROS (TYPE_MODE (type)) | |
bd214d13 | 603 | && reorder_operands_p (TREE_OPERAND (t, 0), TREE_OPERAND (t, 1))) |
389dd41b | 604 | return fold_build2_loc (loc, MINUS_EXPR, type, |
58b22aa6 | 605 | TREE_OPERAND (t, 1), TREE_OPERAND (t, 0)); |
23ec2d5e | 606 | break; |
607 | ||
a12ecaaa | 608 | case MULT_EXPR: |
58b22aa6 | 609 | if (TYPE_UNSIGNED (type)) |
a12ecaaa | 610 | break; |
611 | ||
612 | /* Fall through. */ | |
613 | ||
614 | case RDIV_EXPR: | |
58b22aa6 | 615 | if (! HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (type))) |
a12ecaaa | 616 | { |
617 | tem = TREE_OPERAND (t, 1); | |
618 | if (negate_expr_p (tem)) | |
389dd41b | 619 | return fold_build2_loc (loc, TREE_CODE (t), type, |
58b22aa6 | 620 | TREE_OPERAND (t, 0), negate_expr (tem)); |
a12ecaaa | 621 | tem = TREE_OPERAND (t, 0); |
622 | if (negate_expr_p (tem)) | |
389dd41b | 623 | return fold_build2_loc (loc, TREE_CODE (t), type, |
58b22aa6 | 624 | negate_expr (tem), TREE_OPERAND (t, 1)); |
a12ecaaa | 625 | } |
626 | break; | |
627 | ||
212f6d6d | 628 | case TRUNC_DIV_EXPR: |
629 | case ROUND_DIV_EXPR: | |
630 | case FLOOR_DIV_EXPR: | |
631 | case CEIL_DIV_EXPR: | |
632 | case EXACT_DIV_EXPR: | |
add6ee5e | 633 | /* In general we can't negate A / B, because if A is INT_MIN and |
634 | B is 1, we may turn this into INT_MIN / -1 which is undefined | |
635 | and actually traps on some architectures. But if overflow is | |
636 | undefined, we can negate, because - (INT_MIN / 1) is an | |
637 | overflow. */ | |
981eb798 | 638 | if (!INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_UNDEFINED (type)) |
212f6d6d | 639 | { |
add6ee5e | 640 | const char * const warnmsg = G_("assuming signed overflow does not " |
641 | "occur when negating a division"); | |
212f6d6d | 642 | tem = TREE_OPERAND (t, 1); |
643 | if (negate_expr_p (tem)) | |
add6ee5e | 644 | { |
645 | if (INTEGRAL_TYPE_P (type) | |
646 | && (TREE_CODE (tem) != INTEGER_CST | |
647 | || integer_onep (tem))) | |
648 | fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_MISC); | |
389dd41b | 649 | return fold_build2_loc (loc, TREE_CODE (t), type, |
add6ee5e | 650 | TREE_OPERAND (t, 0), negate_expr (tem)); |
651 | } | |
212f6d6d | 652 | tem = TREE_OPERAND (t, 0); |
653 | if (negate_expr_p (tem)) | |
add6ee5e | 654 | { |
655 | if (INTEGRAL_TYPE_P (type) | |
656 | && (TREE_CODE (tem) != INTEGER_CST | |
657 | || tree_int_cst_equal (tem, TYPE_MIN_VALUE (type)))) | |
658 | fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_MISC); | |
389dd41b | 659 | return fold_build2_loc (loc, TREE_CODE (t), type, |
add6ee5e | 660 | negate_expr (tem), TREE_OPERAND (t, 1)); |
661 | } | |
212f6d6d | 662 | } |
663 | break; | |
664 | ||
bd214d13 | 665 | case NOP_EXPR: |
666 | /* Convert -((double)float) into (double)(-float). */ | |
667 | if (TREE_CODE (type) == REAL_TYPE) | |
668 | { | |
669 | tem = strip_float_extensions (t); | |
670 | if (tem != t && negate_expr_p (tem)) | |
389dd41b | 671 | return fold_convert_loc (loc, type, negate_expr (tem)); |
bd214d13 | 672 | } |
673 | break; | |
674 | ||
675 | case CALL_EXPR: | |
676 | /* Negate -f(x) as f(-x). */ | |
677 | if (negate_mathfn_p (builtin_mathfn_code (t)) | |
c2f47e15 | 678 | && negate_expr_p (CALL_EXPR_ARG (t, 0))) |
bd214d13 | 679 | { |
c2f47e15 | 680 | tree fndecl, arg; |
bd214d13 | 681 | |
682 | fndecl = get_callee_fndecl (t); | |
c2f47e15 | 683 | arg = negate_expr (CALL_EXPR_ARG (t, 0)); |
389dd41b | 684 | return build_call_expr_loc (loc, fndecl, 1, arg); |
bd214d13 | 685 | } |
686 | break; | |
687 | ||
a22fd555 | 688 | case RSHIFT_EXPR: |
689 | /* Optimize -((int)x >> 31) into (unsigned)x >> 31. */ | |
690 | if (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST) | |
691 | { | |
692 | tree op1 = TREE_OPERAND (t, 1); | |
693 | if (TREE_INT_CST_HIGH (op1) == 0 | |
694 | && (unsigned HOST_WIDE_INT) (TYPE_PRECISION (type) - 1) | |
695 | == TREE_INT_CST_LOW (op1)) | |
696 | { | |
78a8ed03 | 697 | tree ntype = TYPE_UNSIGNED (type) |
11773141 | 698 | ? signed_type_for (type) |
71eea85c | 699 | : unsigned_type_for (type); |
389dd41b | 700 | tree temp = fold_convert_loc (loc, ntype, TREE_OPERAND (t, 0)); |
701 | temp = fold_build2_loc (loc, RSHIFT_EXPR, ntype, temp, op1); | |
702 | return fold_convert_loc (loc, type, temp); | |
a22fd555 | 703 | } |
704 | } | |
705 | break; | |
706 | ||
23ec2d5e | 707 | default: |
708 | break; | |
709 | } | |
710 | ||
58b22aa6 | 711 | return NULL_TREE; |
712 | } | |
713 | ||
714 | /* Like fold_negate_expr, but return a NEGATE_EXPR tree, if T can not be | |
715 | negated in a simpler way. Also allow for T to be NULL_TREE, in which case | |
716 | return NULL_TREE. */ | |
717 | ||
718 | static tree | |
719 | negate_expr (tree t) | |
720 | { | |
721 | tree type, tem; | |
389dd41b | 722 | location_t loc; |
58b22aa6 | 723 | |
724 | if (t == NULL_TREE) | |
725 | return NULL_TREE; | |
726 | ||
389dd41b | 727 | loc = EXPR_LOCATION (t); |
58b22aa6 | 728 | type = TREE_TYPE (t); |
729 | STRIP_SIGN_NOPS (t); | |
730 | ||
389dd41b | 731 | tem = fold_negate_expr (loc, t); |
58b22aa6 | 732 | if (!tem) |
389dd41b | 733 | { |
734 | tem = build1 (NEGATE_EXPR, TREE_TYPE (t), t); | |
735 | SET_EXPR_LOCATION (tem, loc); | |
736 | } | |
737 | return fold_convert_loc (loc, type, tem); | |
23ec2d5e | 738 | } |
739 | \f | |
740 | /* Split a tree IN into a constant, literal and variable parts that could be | |
741 | combined with CODE to make IN. "constant" means an expression with | |
742 | TREE_CONSTANT but that isn't an actual constant. CODE must be a | |
743 | commutative arithmetic operation. Store the constant part into *CONP, | |
b07ba9ff | 744 | the literal in *LITP and return the variable part. If a part isn't |
23ec2d5e | 745 | present, set it to null. If the tree does not decompose in this way, |
746 | return the entire tree as the variable part and the other parts as null. | |
747 | ||
748 | If CODE is PLUS_EXPR we also split trees that use MINUS_EXPR. In that | |
b07ba9ff | 749 | case, we negate an operand that was subtracted. Except if it is a |
750 | literal for which we use *MINUS_LITP instead. | |
751 | ||
752 | If NEGATE_P is true, we are negating all of IN, again except a literal | |
753 | for which we use *MINUS_LITP instead. | |
23ec2d5e | 754 | |
755 | If IN is itself a literal or constant, return it as appropriate. | |
756 | ||
757 | Note that we do not guarantee that any of the three values will be the | |
758 | same type as IN, but they will have the same signedness and mode. */ | |
759 | ||
760 | static tree | |
dc81944a | 761 | split_tree (tree in, enum tree_code code, tree *conp, tree *litp, |
762 | tree *minus_litp, int negate_p) | |
2bc77e10 | 763 | { |
23ec2d5e | 764 | tree var = 0; |
765 | ||
2bc77e10 | 766 | *conp = 0; |
23ec2d5e | 767 | *litp = 0; |
b07ba9ff | 768 | *minus_litp = 0; |
23ec2d5e | 769 | |
6312a35e | 770 | /* Strip any conversions that don't change the machine mode or signedness. */ |
23ec2d5e | 771 | STRIP_SIGN_NOPS (in); |
772 | ||
06f0b99c | 773 | if (TREE_CODE (in) == INTEGER_CST || TREE_CODE (in) == REAL_CST |
774 | || TREE_CODE (in) == FIXED_CST) | |
23ec2d5e | 775 | *litp = in; |
23ec2d5e | 776 | else if (TREE_CODE (in) == code |
a145256a | 777 | || ((! FLOAT_TYPE_P (TREE_TYPE (in)) || flag_associative_math) |
06f0b99c | 778 | && ! SAT_FIXED_POINT_TYPE_P (TREE_TYPE (in)) |
23ec2d5e | 779 | /* We can associate addition and subtraction together (even |
780 | though the C standard doesn't say so) for integers because | |
781 | the value is not affected. For reals, the value might be | |
782 | affected, so we can't. */ | |
783 | && ((code == PLUS_EXPR && TREE_CODE (in) == MINUS_EXPR) | |
784 | || (code == MINUS_EXPR && TREE_CODE (in) == PLUS_EXPR)))) | |
785 | { | |
786 | tree op0 = TREE_OPERAND (in, 0); | |
787 | tree op1 = TREE_OPERAND (in, 1); | |
788 | int neg1_p = TREE_CODE (in) == MINUS_EXPR; | |
789 | int neg_litp_p = 0, neg_conp_p = 0, neg_var_p = 0; | |
790 | ||
791 | /* First see if either of the operands is a literal, then a constant. */ | |
06f0b99c | 792 | if (TREE_CODE (op0) == INTEGER_CST || TREE_CODE (op0) == REAL_CST |
793 | || TREE_CODE (op0) == FIXED_CST) | |
23ec2d5e | 794 | *litp = op0, op0 = 0; |
06f0b99c | 795 | else if (TREE_CODE (op1) == INTEGER_CST || TREE_CODE (op1) == REAL_CST |
796 | || TREE_CODE (op1) == FIXED_CST) | |
23ec2d5e | 797 | *litp = op1, neg_litp_p = neg1_p, op1 = 0; |
798 | ||
799 | if (op0 != 0 && TREE_CONSTANT (op0)) | |
800 | *conp = op0, op0 = 0; | |
801 | else if (op1 != 0 && TREE_CONSTANT (op1)) | |
802 | *conp = op1, neg_conp_p = neg1_p, op1 = 0; | |
803 | ||
804 | /* If we haven't dealt with either operand, this is not a case we can | |
6312a35e | 805 | decompose. Otherwise, VAR is either of the ones remaining, if any. */ |
23ec2d5e | 806 | if (op0 != 0 && op1 != 0) |
807 | var = in; | |
808 | else if (op0 != 0) | |
809 | var = op0; | |
810 | else | |
811 | var = op1, neg_var_p = neg1_p; | |
2bc77e10 | 812 | |
23ec2d5e | 813 | /* Now do any needed negations. */ |
b07ba9ff | 814 | if (neg_litp_p) |
815 | *minus_litp = *litp, *litp = 0; | |
816 | if (neg_conp_p) | |
817 | *conp = negate_expr (*conp); | |
818 | if (neg_var_p) | |
819 | var = negate_expr (var); | |
23ec2d5e | 820 | } |
8541c166 | 821 | else if (TREE_CONSTANT (in)) |
822 | *conp = in; | |
23ec2d5e | 823 | else |
824 | var = in; | |
825 | ||
826 | if (negate_p) | |
2bc77e10 | 827 | { |
b07ba9ff | 828 | if (*litp) |
829 | *minus_litp = *litp, *litp = 0; | |
830 | else if (*minus_litp) | |
831 | *litp = *minus_litp, *minus_litp = 0; | |
23ec2d5e | 832 | *conp = negate_expr (*conp); |
b07ba9ff | 833 | var = negate_expr (var); |
2bc77e10 | 834 | } |
23ec2d5e | 835 | |
836 | return var; | |
837 | } | |
838 | ||
389dd41b | 839 | /* Re-associate trees split by the above function. T1 and T2 are |
840 | either expressions to associate or null. Return the new | |
841 | expression, if any. LOC is the location of the new expression. If | |
b07ba9ff | 842 | we build an operation, do it in TYPE and with CODE. */ |
23ec2d5e | 843 | |
844 | static tree | |
389dd41b | 845 | associate_trees (location_t loc, tree t1, tree t2, enum tree_code code, tree type) |
23ec2d5e | 846 | { |
389dd41b | 847 | tree tem; |
848 | ||
23ec2d5e | 849 | if (t1 == 0) |
850 | return t2; | |
851 | else if (t2 == 0) | |
852 | return t1; | |
853 | ||
23ec2d5e | 854 | /* If either input is CODE, a PLUS_EXPR, or a MINUS_EXPR, don't |
855 | try to fold this since we will have infinite recursion. But do | |
856 | deal with any NEGATE_EXPRs. */ | |
857 | if (TREE_CODE (t1) == code || TREE_CODE (t2) == code | |
858 | || TREE_CODE (t1) == MINUS_EXPR || TREE_CODE (t2) == MINUS_EXPR) | |
859 | { | |
5a3fb4d3 | 860 | if (code == PLUS_EXPR) |
861 | { | |
862 | if (TREE_CODE (t1) == NEGATE_EXPR) | |
389dd41b | 863 | tem = build2 (MINUS_EXPR, type, fold_convert_loc (loc, type, t2), |
864 | fold_convert_loc (loc, type, TREE_OPERAND (t1, 0))); | |
5a3fb4d3 | 865 | else if (TREE_CODE (t2) == NEGATE_EXPR) |
389dd41b | 866 | tem = build2 (MINUS_EXPR, type, fold_convert_loc (loc, type, t1), |
867 | fold_convert_loc (loc, type, TREE_OPERAND (t2, 0))); | |
faab57e3 | 868 | else if (integer_zerop (t2)) |
389dd41b | 869 | return fold_convert_loc (loc, type, t1); |
5a3fb4d3 | 870 | } |
faab57e3 | 871 | else if (code == MINUS_EXPR) |
872 | { | |
873 | if (integer_zerop (t2)) | |
389dd41b | 874 | return fold_convert_loc (loc, type, t1); |
faab57e3 | 875 | } |
876 | ||
389dd41b | 877 | tem = build2 (code, type, fold_convert_loc (loc, type, t1), |
878 | fold_convert_loc (loc, type, t2)); | |
879 | goto associate_trees_exit; | |
23ec2d5e | 880 | } |
881 | ||
389dd41b | 882 | return fold_build2_loc (loc, code, type, fold_convert_loc (loc, type, t1), |
883 | fold_convert_loc (loc, type, t2)); | |
884 | associate_trees_exit: | |
885 | protected_set_expr_location (tem, loc); | |
886 | return tem; | |
2bc77e10 | 887 | } |
888 | \f | |
2455d3ef | 889 | /* Check whether TYPE1 and TYPE2 are equivalent integer types, suitable |
890 | for use in int_const_binop, size_binop and size_diffop. */ | |
891 | ||
892 | static bool | |
b4b34335 | 893 | int_binop_types_match_p (enum tree_code code, const_tree type1, const_tree type2) |
2455d3ef | 894 | { |
895 | if (TREE_CODE (type1) != INTEGER_TYPE && !POINTER_TYPE_P (type1)) | |
896 | return false; | |
897 | if (TREE_CODE (type2) != INTEGER_TYPE && !POINTER_TYPE_P (type2)) | |
898 | return false; | |
899 | ||
900 | switch (code) | |
901 | { | |
902 | case LSHIFT_EXPR: | |
903 | case RSHIFT_EXPR: | |
904 | case LROTATE_EXPR: | |
905 | case RROTATE_EXPR: | |
906 | return true; | |
907 | ||
908 | default: | |
909 | break; | |
910 | } | |
911 | ||
912 | return TYPE_UNSIGNED (type1) == TYPE_UNSIGNED (type2) | |
913 | && TYPE_PRECISION (type1) == TYPE_PRECISION (type2) | |
914 | && TYPE_MODE (type1) == TYPE_MODE (type2); | |
915 | } | |
916 | ||
917 | ||
0dbd1c74 | 918 | /* Combine two integer constants ARG1 and ARG2 under operation CODE |
32cef1cc | 919 | to produce a new constant. Return NULL_TREE if we don't know how |
920 | to evaluate CODE at compile-time. | |
5485823f | 921 | |
15d769aa | 922 | If NOTRUNC is nonzero, do not truncate the result to fit the data type. */ |
2bc77e10 | 923 | |
4ee9c684 | 924 | tree |
b7bf20db | 925 | int_const_binop (enum tree_code code, const_tree arg1, const_tree arg2, int notrunc) |
2bc77e10 | 926 | { |
a0c2c45b | 927 | unsigned HOST_WIDE_INT int1l, int2l; |
928 | HOST_WIDE_INT int1h, int2h; | |
929 | unsigned HOST_WIDE_INT low; | |
930 | HOST_WIDE_INT hi; | |
931 | unsigned HOST_WIDE_INT garbagel; | |
932 | HOST_WIDE_INT garbageh; | |
19cb6b50 | 933 | tree t; |
15d769aa | 934 | tree type = TREE_TYPE (arg1); |
78a8ed03 | 935 | int uns = TYPE_UNSIGNED (type); |
15d769aa | 936 | int is_sizetype |
937 | = (TREE_CODE (type) == INTEGER_TYPE && TYPE_IS_SIZETYPE (type)); | |
0dbd1c74 | 938 | int overflow = 0; |
8ea862a9 | 939 | |
0dbd1c74 | 940 | int1l = TREE_INT_CST_LOW (arg1); |
941 | int1h = TREE_INT_CST_HIGH (arg1); | |
942 | int2l = TREE_INT_CST_LOW (arg2); | |
943 | int2h = TREE_INT_CST_HIGH (arg2); | |
944 | ||
945 | switch (code) | |
2bc77e10 | 946 | { |
0dbd1c74 | 947 | case BIT_IOR_EXPR: |
948 | low = int1l | int2l, hi = int1h | int2h; | |
949 | break; | |
2bc77e10 | 950 | |
0dbd1c74 | 951 | case BIT_XOR_EXPR: |
952 | low = int1l ^ int2l, hi = int1h ^ int2h; | |
953 | break; | |
2bc77e10 | 954 | |
0dbd1c74 | 955 | case BIT_AND_EXPR: |
956 | low = int1l & int2l, hi = int1h & int2h; | |
957 | break; | |
2bc77e10 | 958 | |
0dbd1c74 | 959 | case RSHIFT_EXPR: |
cc049fa3 | 960 | int2l = -int2l; |
0dbd1c74 | 961 | case LSHIFT_EXPR: |
962 | /* It's unclear from the C standard whether shifts can overflow. | |
963 | The following code ignores overflow; perhaps a C standard | |
964 | interpretation ruling is needed. */ | |
15d769aa | 965 | lshift_double (int1l, int1h, int2l, TYPE_PRECISION (type), |
02e7a332 | 966 | &low, &hi, !uns); |
0dbd1c74 | 967 | break; |
2bc77e10 | 968 | |
0dbd1c74 | 969 | case RROTATE_EXPR: |
970 | int2l = - int2l; | |
971 | case LROTATE_EXPR: | |
15d769aa | 972 | lrotate_double (int1l, int1h, int2l, TYPE_PRECISION (type), |
0dbd1c74 | 973 | &low, &hi); |
974 | break; | |
2bc77e10 | 975 | |
0dbd1c74 | 976 | case PLUS_EXPR: |
977 | overflow = add_double (int1l, int1h, int2l, int2h, &low, &hi); | |
978 | break; | |
2bc77e10 | 979 | |
0dbd1c74 | 980 | case MINUS_EXPR: |
981 | neg_double (int2l, int2h, &low, &hi); | |
982 | add_double (int1l, int1h, low, hi, &low, &hi); | |
083a2b5e | 983 | overflow = OVERFLOW_SUM_SIGN (hi, int2h, int1h); |
0dbd1c74 | 984 | break; |
2bc77e10 | 985 | |
0dbd1c74 | 986 | case MULT_EXPR: |
987 | overflow = mul_double (int1l, int1h, int2l, int2h, &low, &hi); | |
988 | break; | |
2bc77e10 | 989 | |
0dbd1c74 | 990 | case TRUNC_DIV_EXPR: |
991 | case FLOOR_DIV_EXPR: case CEIL_DIV_EXPR: | |
992 | case EXACT_DIV_EXPR: | |
993 | /* This is a shortcut for a common special case. */ | |
a0c2c45b | 994 | if (int2h == 0 && (HOST_WIDE_INT) int2l > 0 |
f96bd2bf | 995 | && !TREE_OVERFLOW (arg1) |
996 | && !TREE_OVERFLOW (arg2) | |
a0c2c45b | 997 | && int1h == 0 && (HOST_WIDE_INT) int1l >= 0) |
0dbd1c74 | 998 | { |
999 | if (code == CEIL_DIV_EXPR) | |
1000 | int1l += int2l - 1; | |
a0c2c45b | 1001 | |
0dbd1c74 | 1002 | low = int1l / int2l, hi = 0; |
2bc77e10 | 1003 | break; |
0dbd1c74 | 1004 | } |
2bc77e10 | 1005 | |
6312a35e | 1006 | /* ... fall through ... */ |
2bc77e10 | 1007 | |
cc049fa3 | 1008 | case ROUND_DIV_EXPR: |
32cef1cc | 1009 | if (int2h == 0 && int2l == 0) |
1010 | return NULL_TREE; | |
0dbd1c74 | 1011 | if (int2h == 0 && int2l == 1) |
1012 | { | |
1013 | low = int1l, hi = int1h; | |
2bc77e10 | 1014 | break; |
0dbd1c74 | 1015 | } |
1016 | if (int1l == int2l && int1h == int2h | |
1017 | && ! (int1l == 0 && int1h == 0)) | |
1018 | { | |
1019 | low = 1, hi = 0; | |
c13e6dce | 1020 | break; |
0dbd1c74 | 1021 | } |
15d769aa | 1022 | overflow = div_and_round_double (code, uns, int1l, int1h, int2l, int2h, |
0dbd1c74 | 1023 | &low, &hi, &garbagel, &garbageh); |
1024 | break; | |
c13e6dce | 1025 | |
0dbd1c74 | 1026 | case TRUNC_MOD_EXPR: |
1027 | case FLOOR_MOD_EXPR: case CEIL_MOD_EXPR: | |
1028 | /* This is a shortcut for a common special case. */ | |
a0c2c45b | 1029 | if (int2h == 0 && (HOST_WIDE_INT) int2l > 0 |
f96bd2bf | 1030 | && !TREE_OVERFLOW (arg1) |
1031 | && !TREE_OVERFLOW (arg2) | |
a0c2c45b | 1032 | && int1h == 0 && (HOST_WIDE_INT) int1l >= 0) |
0dbd1c74 | 1033 | { |
1034 | if (code == CEIL_MOD_EXPR) | |
1035 | int1l += int2l - 1; | |
1036 | low = int1l % int2l, hi = 0; | |
c13e6dce | 1037 | break; |
0dbd1c74 | 1038 | } |
c13e6dce | 1039 | |
6312a35e | 1040 | /* ... fall through ... */ |
0dbd1c74 | 1041 | |
cc049fa3 | 1042 | case ROUND_MOD_EXPR: |
32cef1cc | 1043 | if (int2h == 0 && int2l == 0) |
1044 | return NULL_TREE; | |
0dbd1c74 | 1045 | overflow = div_and_round_double (code, uns, |
1046 | int1l, int1h, int2l, int2h, | |
1047 | &garbagel, &garbageh, &low, &hi); | |
1048 | break; | |
1049 | ||
1050 | case MIN_EXPR: | |
1051 | case MAX_EXPR: | |
1052 | if (uns) | |
083a2b5e | 1053 | low = (((unsigned HOST_WIDE_INT) int1h |
1054 | < (unsigned HOST_WIDE_INT) int2h) | |
1055 | || (((unsigned HOST_WIDE_INT) int1h | |
1056 | == (unsigned HOST_WIDE_INT) int2h) | |
a0c2c45b | 1057 | && int1l < int2l)); |
a3f1e3ec | 1058 | else |
a0c2c45b | 1059 | low = (int1h < int2h |
1060 | || (int1h == int2h && int1l < int2l)); | |
083a2b5e | 1061 | |
0dbd1c74 | 1062 | if (low == (code == MIN_EXPR)) |
1063 | low = int1l, hi = int1h; | |
1064 | else | |
1065 | low = int2l, hi = int2h; | |
1066 | break; | |
8ea862a9 | 1067 | |
0dbd1c74 | 1068 | default: |
32cef1cc | 1069 | return NULL_TREE; |
8ea862a9 | 1070 | } |
0dbd1c74 | 1071 | |
4d28c5d1 | 1072 | if (notrunc) |
1073 | { | |
c8110c8f | 1074 | t = build_int_cst_wide (TREE_TYPE (arg1), low, hi); |
1075 | ||
4d28c5d1 | 1076 | /* Propagate overflow flags ourselves. */ |
1077 | if (((!uns || is_sizetype) && overflow) | |
1078 | | TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2)) | |
00b76131 | 1079 | { |
1080 | t = copy_node (t); | |
1081 | TREE_OVERFLOW (t) = 1; | |
00b76131 | 1082 | } |
4d28c5d1 | 1083 | } |
1084 | else | |
c8110c8f | 1085 | t = force_fit_type_double (TREE_TYPE (arg1), low, hi, 1, |
1086 | ((!uns || is_sizetype) && overflow) | |
eddad94a | 1087 | | TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2)); |
0c5713a2 | 1088 | |
0dbd1c74 | 1089 | return t; |
1090 | } | |
1091 | ||
083a2b5e | 1092 | /* Combine two constants ARG1 and ARG2 under operation CODE to produce a new |
1093 | constant. We assume ARG1 and ARG2 have the same data type, or at least | |
0b6fa2ba | 1094 | are the same kind of constant and the same machine mode. Return zero if |
1095 | combining the constants is not allowed in the current operating mode. | |
0dbd1c74 | 1096 | |
1097 | If NOTRUNC is nonzero, do not truncate the result to fit the data type. */ | |
1098 | ||
1099 | static tree | |
de1b648b | 1100 | const_binop (enum tree_code code, tree arg1, tree arg2, int notrunc) |
0dbd1c74 | 1101 | { |
0b6fa2ba | 1102 | /* Sanity check for the recursive cases. */ |
1103 | if (!arg1 || !arg2) | |
1104 | return NULL_TREE; | |
1105 | ||
cc049fa3 | 1106 | STRIP_NOPS (arg1); |
1107 | STRIP_NOPS (arg2); | |
0dbd1c74 | 1108 | |
1109 | if (TREE_CODE (arg1) == INTEGER_CST) | |
15d769aa | 1110 | return int_const_binop (code, arg1, arg2, notrunc); |
0dbd1c74 | 1111 | |
2bc77e10 | 1112 | if (TREE_CODE (arg1) == REAL_CST) |
1113 | { | |
276beea2 | 1114 | enum machine_mode mode; |
9a24cfc6 | 1115 | REAL_VALUE_TYPE d1; |
1116 | REAL_VALUE_TYPE d2; | |
536f5fb1 | 1117 | REAL_VALUE_TYPE value; |
a47b9d79 | 1118 | REAL_VALUE_TYPE result; |
1119 | bool inexact; | |
276beea2 | 1120 | tree t, type; |
2bc77e10 | 1121 | |
32cef1cc | 1122 | /* The following codes are handled by real_arithmetic. */ |
1123 | switch (code) | |
1124 | { | |
1125 | case PLUS_EXPR: | |
1126 | case MINUS_EXPR: | |
1127 | case MULT_EXPR: | |
1128 | case RDIV_EXPR: | |
1129 | case MIN_EXPR: | |
1130 | case MAX_EXPR: | |
1131 | break; | |
1132 | ||
1133 | default: | |
1134 | return NULL_TREE; | |
1135 | } | |
1136 | ||
9a24cfc6 | 1137 | d1 = TREE_REAL_CST (arg1); |
1138 | d2 = TREE_REAL_CST (arg2); | |
9248d3e0 | 1139 | |
276beea2 | 1140 | type = TREE_TYPE (arg1); |
1141 | mode = TYPE_MODE (type); | |
1142 | ||
1143 | /* Don't perform operation if we honor signaling NaNs and | |
1144 | either operand is a NaN. */ | |
1145 | if (HONOR_SNANS (mode) | |
1146 | && (REAL_VALUE_ISNAN (d1) || REAL_VALUE_ISNAN (d2))) | |
1147 | return NULL_TREE; | |
1148 | ||
1149 | /* Don't perform operation if it would raise a division | |
1150 | by zero exception. */ | |
1151 | if (code == RDIV_EXPR | |
1152 | && REAL_VALUES_EQUAL (d2, dconst0) | |
1153 | && (flag_trapping_math || ! MODE_HAS_INFINITIES (mode))) | |
1154 | return NULL_TREE; | |
1155 | ||
9248d3e0 | 1156 | /* If either operand is a NaN, just return it. Otherwise, set up |
1157 | for floating-point trap; we return an overflow. */ | |
1158 | if (REAL_VALUE_ISNAN (d1)) | |
1159 | return arg1; | |
1160 | else if (REAL_VALUE_ISNAN (d2)) | |
1161 | return arg2; | |
70192c5e | 1162 | |
a47b9d79 | 1163 | inexact = real_arithmetic (&value, code, &d1, &d2); |
1164 | real_convert (&result, mode, &value); | |
cc049fa3 | 1165 | |
f2e0dda3 | 1166 | /* Don't constant fold this floating point operation if |
1167 | the result has overflowed and flag_trapping_math. */ | |
f2e0dda3 | 1168 | if (flag_trapping_math |
1169 | && MODE_HAS_INFINITIES (mode) | |
1170 | && REAL_VALUE_ISINF (result) | |
1171 | && !REAL_VALUE_ISINF (d1) | |
1172 | && !REAL_VALUE_ISINF (d2)) | |
1173 | return NULL_TREE; | |
1174 | ||
a47b9d79 | 1175 | /* Don't constant fold this floating point operation if the |
1176 | result may dependent upon the run-time rounding mode and | |
09fde96c | 1177 | flag_rounding_math is set, or if GCC's software emulation |
1178 | is unable to accurately represent the result. */ | |
09fde96c | 1179 | if ((flag_rounding_math |
f2b38121 | 1180 | || (MODE_COMPOSITE_P (mode) && !flag_unsafe_math_optimizations)) |
a47b9d79 | 1181 | && (inexact || !real_identical (&result, &value))) |
1182 | return NULL_TREE; | |
1183 | ||
1184 | t = build_real (type, result); | |
23fed9b2 | 1185 | |
4d28c5d1 | 1186 | TREE_OVERFLOW (t) = TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2); |
c0244247 | 1187 | return t; |
2bc77e10 | 1188 | } |
32cef1cc | 1189 | |
06f0b99c | 1190 | if (TREE_CODE (arg1) == FIXED_CST) |
1191 | { | |
1192 | FIXED_VALUE_TYPE f1; | |
1193 | FIXED_VALUE_TYPE f2; | |
1194 | FIXED_VALUE_TYPE result; | |
1195 | tree t, type; | |
1196 | int sat_p; | |
1197 | bool overflow_p; | |
1198 | ||
1199 | /* The following codes are handled by fixed_arithmetic. */ | |
1200 | switch (code) | |
1201 | { | |
1202 | case PLUS_EXPR: | |
1203 | case MINUS_EXPR: | |
1204 | case MULT_EXPR: | |
1205 | case TRUNC_DIV_EXPR: | |
1206 | f2 = TREE_FIXED_CST (arg2); | |
1207 | break; | |
1208 | ||
1209 | case LSHIFT_EXPR: | |
1210 | case RSHIFT_EXPR: | |
1211 | f2.data.high = TREE_INT_CST_HIGH (arg2); | |
1212 | f2.data.low = TREE_INT_CST_LOW (arg2); | |
1213 | f2.mode = SImode; | |
1214 | break; | |
1215 | ||
1216 | default: | |
1217 | return NULL_TREE; | |
1218 | } | |
1219 | ||
1220 | f1 = TREE_FIXED_CST (arg1); | |
1221 | type = TREE_TYPE (arg1); | |
1222 | sat_p = TYPE_SATURATING (type); | |
1223 | overflow_p = fixed_arithmetic (&result, code, &f1, &f2, sat_p); | |
1224 | t = build_fixed (type, result); | |
1225 | /* Propagate overflow flags. */ | |
1226 | if (overflow_p | TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2)) | |
62126877 | 1227 | TREE_OVERFLOW (t) = 1; |
06f0b99c | 1228 | return t; |
1229 | } | |
1230 | ||
2bc77e10 | 1231 | if (TREE_CODE (arg1) == COMPLEX_CST) |
1232 | { | |
19cb6b50 | 1233 | tree type = TREE_TYPE (arg1); |
1234 | tree r1 = TREE_REALPART (arg1); | |
1235 | tree i1 = TREE_IMAGPART (arg1); | |
1236 | tree r2 = TREE_REALPART (arg2); | |
1237 | tree i2 = TREE_IMAGPART (arg2); | |
0b6fa2ba | 1238 | tree real, imag; |
2bc77e10 | 1239 | |
1240 | switch (code) | |
1241 | { | |
1242 | case PLUS_EXPR: | |
2bc77e10 | 1243 | case MINUS_EXPR: |
0b6fa2ba | 1244 | real = const_binop (code, r1, r2, notrunc); |
1245 | imag = const_binop (code, i1, i2, notrunc); | |
2bc77e10 | 1246 | break; |
1247 | ||
1248 | case MULT_EXPR: | |
63e89698 | 1249 | if (COMPLEX_FLOAT_TYPE_P (type)) |
652d9409 | 1250 | return do_mpc_arg2 (arg1, arg2, type, |
1251 | /* do_nonfinite= */ folding_initializer, | |
1252 | mpc_mul); | |
63e89698 | 1253 | |
0b6fa2ba | 1254 | real = const_binop (MINUS_EXPR, |
1255 | const_binop (MULT_EXPR, r1, r2, notrunc), | |
1256 | const_binop (MULT_EXPR, i1, i2, notrunc), | |
1257 | notrunc); | |
1258 | imag = const_binop (PLUS_EXPR, | |
1259 | const_binop (MULT_EXPR, r1, i2, notrunc), | |
1260 | const_binop (MULT_EXPR, i1, r2, notrunc), | |
1261 | notrunc); | |
2bc77e10 | 1262 | break; |
1263 | ||
1264 | case RDIV_EXPR: | |
63e89698 | 1265 | if (COMPLEX_FLOAT_TYPE_P (type)) |
652d9409 | 1266 | return do_mpc_arg2 (arg1, arg2, type, |
1267 | /* do_nonfinite= */ folding_initializer, | |
1268 | mpc_div); | |
03a7d9e9 | 1269 | /* Fallthru ... */ |
03a7d9e9 | 1270 | case TRUNC_DIV_EXPR: |
1271 | case CEIL_DIV_EXPR: | |
1272 | case FLOOR_DIV_EXPR: | |
1273 | case ROUND_DIV_EXPR: | |
1274 | if (flag_complex_method == 0) | |
2bc77e10 | 1275 | { |
03a7d9e9 | 1276 | /* Keep this algorithm in sync with |
1277 | tree-complex.c:expand_complex_div_straight(). | |
1278 | ||
1279 | Expand complex division to scalars, straightforward algorithm. | |
1280 | a / b = ((ar*br + ai*bi)/t) + i((ai*br - ar*bi)/t) | |
1281 | t = br*br + bi*bi | |
1282 | */ | |
19cb6b50 | 1283 | tree magsquared |
2bc77e10 | 1284 | = const_binop (PLUS_EXPR, |
5485823f | 1285 | const_binop (MULT_EXPR, r2, r2, notrunc), |
1286 | const_binop (MULT_EXPR, i2, i2, notrunc), | |
1287 | notrunc); | |
0b6fa2ba | 1288 | tree t1 |
1289 | = const_binop (PLUS_EXPR, | |
1290 | const_binop (MULT_EXPR, r1, r2, notrunc), | |
1291 | const_binop (MULT_EXPR, i1, i2, notrunc), | |
1292 | notrunc); | |
1293 | tree t2 | |
1294 | = const_binop (MINUS_EXPR, | |
1295 | const_binop (MULT_EXPR, i1, r2, notrunc), | |
1296 | const_binop (MULT_EXPR, r1, i2, notrunc), | |
1297 | notrunc); | |
86a914ce | 1298 | |
0b6fa2ba | 1299 | real = const_binop (code, t1, magsquared, notrunc); |
1300 | imag = const_binop (code, t2, magsquared, notrunc); | |
2bc77e10 | 1301 | } |
03a7d9e9 | 1302 | else |
1303 | { | |
1304 | /* Keep this algorithm in sync with | |
1305 | tree-complex.c:expand_complex_div_wide(). | |
1306 | ||
1307 | Expand complex division to scalars, modified algorithm to minimize | |
1308 | overflow with wide input ranges. */ | |
f7d5c4dd | 1309 | tree compare = fold_build2 (LT_EXPR, boolean_type_node, |
1310 | fold_abs_const (r2, TREE_TYPE (type)), | |
1311 | fold_abs_const (i2, TREE_TYPE (type))); | |
48e1416a | 1312 | |
03a7d9e9 | 1313 | if (integer_nonzerop (compare)) |
1314 | { | |
1315 | /* In the TRUE branch, we compute | |
1316 | ratio = br/bi; | |
1317 | div = (br * ratio) + bi; | |
1318 | tr = (ar * ratio) + ai; | |
1319 | ti = (ai * ratio) - ar; | |
1320 | tr = tr / div; | |
1321 | ti = ti / div; */ | |
f7d5c4dd | 1322 | tree ratio = const_binop (code, r2, i2, notrunc); |
1323 | tree div = const_binop (PLUS_EXPR, i2, | |
1324 | const_binop (MULT_EXPR, r2, ratio, | |
1325 | notrunc), | |
1326 | notrunc); | |
1327 | real = const_binop (MULT_EXPR, r1, ratio, notrunc); | |
1328 | real = const_binop (PLUS_EXPR, real, i1, notrunc); | |
1329 | real = const_binop (code, real, div, notrunc); | |
1330 | ||
1331 | imag = const_binop (MULT_EXPR, i1, ratio, notrunc); | |
1332 | imag = const_binop (MINUS_EXPR, imag, r1, notrunc); | |
1333 | imag = const_binop (code, imag, div, notrunc); | |
03a7d9e9 | 1334 | } |
1335 | else | |
1336 | { | |
1337 | /* In the FALSE branch, we compute | |
1338 | ratio = d/c; | |
1339 | divisor = (d * ratio) + c; | |
1340 | tr = (b * ratio) + a; | |
1341 | ti = b - (a * ratio); | |
1342 | tr = tr / div; | |
1343 | ti = ti / div; */ | |
f7d5c4dd | 1344 | tree ratio = const_binop (code, i2, r2, notrunc); |
1345 | tree div = const_binop (PLUS_EXPR, r2, | |
1346 | const_binop (MULT_EXPR, i2, ratio, | |
1347 | notrunc), | |
1348 | notrunc); | |
1349 | ||
1350 | real = const_binop (MULT_EXPR, i1, ratio, notrunc); | |
1351 | real = const_binop (PLUS_EXPR, real, r1, notrunc); | |
1352 | real = const_binop (code, real, div, notrunc); | |
1353 | ||
1354 | imag = const_binop (MULT_EXPR, r1, ratio, notrunc); | |
1355 | imag = const_binop (MINUS_EXPR, i1, imag, notrunc); | |
1356 | imag = const_binop (code, imag, div, notrunc); | |
03a7d9e9 | 1357 | } |
1358 | } | |
2bc77e10 | 1359 | break; |
1360 | ||
1361 | default: | |
32cef1cc | 1362 | return NULL_TREE; |
2bc77e10 | 1363 | } |
0b6fa2ba | 1364 | |
1365 | if (real && imag) | |
1366 | return build_complex (type, real, imag); | |
2bc77e10 | 1367 | } |
0b6fa2ba | 1368 | |
87de4c68 | 1369 | if (TREE_CODE (arg1) == VECTOR_CST) |
1370 | { | |
1371 | tree type = TREE_TYPE(arg1); | |
1372 | int count = TYPE_VECTOR_SUBPARTS (type), i; | |
1373 | tree elements1, elements2, list = NULL_TREE; | |
48e1416a | 1374 | |
87de4c68 | 1375 | if(TREE_CODE(arg2) != VECTOR_CST) |
1376 | return NULL_TREE; | |
48e1416a | 1377 | |
87de4c68 | 1378 | elements1 = TREE_VECTOR_CST_ELTS (arg1); |
1379 | elements2 = TREE_VECTOR_CST_ELTS (arg2); | |
1380 | ||
1381 | for (i = 0; i < count; i++) | |
1382 | { | |
1383 | tree elem1, elem2, elem; | |
48e1416a | 1384 | |
87de4c68 | 1385 | /* The trailing elements can be empty and should be treated as 0 */ |
1386 | if(!elements1) | |
1387 | elem1 = fold_convert_const (NOP_EXPR, TREE_TYPE (type), integer_zero_node); | |
1388 | else | |
1389 | { | |
1390 | elem1 = TREE_VALUE(elements1); | |
1391 | elements1 = TREE_CHAIN (elements1); | |
48e1416a | 1392 | } |
1393 | ||
87de4c68 | 1394 | if(!elements2) |
1395 | elem2 = fold_convert_const (NOP_EXPR, TREE_TYPE (type), integer_zero_node); | |
1396 | else | |
1397 | { | |
1398 | elem2 = TREE_VALUE(elements2); | |
1399 | elements2 = TREE_CHAIN (elements2); | |
1400 | } | |
48e1416a | 1401 | |
87de4c68 | 1402 | elem = const_binop (code, elem1, elem2, notrunc); |
48e1416a | 1403 | |
87de4c68 | 1404 | /* It is possible that const_binop cannot handle the given |
1405 | code and return NULL_TREE */ | |
1406 | if(elem == NULL_TREE) | |
1407 | return NULL_TREE; | |
48e1416a | 1408 | |
87de4c68 | 1409 | list = tree_cons (NULL_TREE, elem, list); |
1410 | } | |
48e1416a | 1411 | return build_vector(type, nreverse(list)); |
87de4c68 | 1412 | } |
32cef1cc | 1413 | return NULL_TREE; |
2bc77e10 | 1414 | } |
15d769aa | 1415 | |
85390276 | 1416 | /* Create a size type INT_CST node with NUMBER sign extended. KIND |
1417 | indicates which particular sizetype to create. */ | |
083a2b5e | 1418 | |
902de8ed | 1419 | tree |
1e9d55d7 | 1420 | size_int_kind (HOST_WIDE_INT number, enum size_type_kind kind) |
902de8ed | 1421 | { |
85390276 | 1422 | return build_int_cst (sizetype_tab[(int) kind], number); |
902de8ed | 1423 | } |
85390276 | 1424 | \f |
902de8ed | 1425 | /* Combine operands OP1 and OP2 with arithmetic operation CODE. CODE |
1426 | is a tree code. The type of the result is taken from the operands. | |
2455d3ef | 1427 | Both must be equivalent integer types, ala int_binop_types_match_p. |
2bc77e10 | 1428 | If the operands are constant, so is the result. */ |
1429 | ||
1430 | tree | |
389dd41b | 1431 | size_binop_loc (location_t loc, enum tree_code code, tree arg0, tree arg1) |
2bc77e10 | 1432 | { |
902de8ed | 1433 | tree type = TREE_TYPE (arg0); |
1434 | ||
4a698d62 | 1435 | if (arg0 == error_mark_node || arg1 == error_mark_node) |
1436 | return error_mark_node; | |
1437 | ||
2455d3ef | 1438 | gcc_assert (int_binop_types_match_p (code, TREE_TYPE (arg0), |
1439 | TREE_TYPE (arg1))); | |
902de8ed | 1440 | |
2bc77e10 | 1441 | /* Handle the special case of two integer constants faster. */ |
1442 | if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST) | |
1443 | { | |
1444 | /* And some specific cases even faster than that. */ | |
6117e415 | 1445 | if (code == PLUS_EXPR) |
1446 | { | |
1447 | if (integer_zerop (arg0) && !TREE_OVERFLOW (arg0)) | |
1448 | return arg1; | |
1449 | if (integer_zerop (arg1) && !TREE_OVERFLOW (arg1)) | |
1450 | return arg0; | |
1451 | } | |
1452 | else if (code == MINUS_EXPR) | |
1453 | { | |
1454 | if (integer_zerop (arg1) && !TREE_OVERFLOW (arg1)) | |
1455 | return arg0; | |
1456 | } | |
1457 | else if (code == MULT_EXPR) | |
1458 | { | |
1459 | if (integer_onep (arg0) && !TREE_OVERFLOW (arg0)) | |
1460 | return arg1; | |
1461 | } | |
a7baffe5 | 1462 | |
2bc77e10 | 1463 | /* Handle general case of two integer constants. */ |
15d769aa | 1464 | return int_const_binop (code, arg0, arg1, 0); |
2bc77e10 | 1465 | } |
1466 | ||
389dd41b | 1467 | return fold_build2_loc (loc, code, type, arg0, arg1); |
2bc77e10 | 1468 | } |
3fd3b688 | 1469 | |
902de8ed | 1470 | /* Given two values, either both of sizetype or both of bitsizetype, |
1471 | compute the difference between the two values. Return the value | |
1472 | in signed type corresponding to the type of the operands. */ | |
3fd3b688 | 1473 | |
1474 | tree | |
389dd41b | 1475 | size_diffop_loc (location_t loc, tree arg0, tree arg1) |
3fd3b688 | 1476 | { |
902de8ed | 1477 | tree type = TREE_TYPE (arg0); |
1478 | tree ctype; | |
3fd3b688 | 1479 | |
2455d3ef | 1480 | gcc_assert (int_binop_types_match_p (MINUS_EXPR, TREE_TYPE (arg0), |
1481 | TREE_TYPE (arg1))); | |
3fd3b688 | 1482 | |
902de8ed | 1483 | /* If the type is already signed, just do the simple thing. */ |
78a8ed03 | 1484 | if (!TYPE_UNSIGNED (type)) |
389dd41b | 1485 | return size_binop_loc (loc, MINUS_EXPR, arg0, arg1); |
902de8ed | 1486 | |
2455d3ef | 1487 | if (type == sizetype) |
1488 | ctype = ssizetype; | |
1489 | else if (type == bitsizetype) | |
1490 | ctype = sbitsizetype; | |
1491 | else | |
11773141 | 1492 | ctype = signed_type_for (type); |
902de8ed | 1493 | |
1494 | /* If either operand is not a constant, do the conversions to the signed | |
1495 | type and subtract. The hardware will do the right thing with any | |
1496 | overflow in the subtraction. */ | |
1497 | if (TREE_CODE (arg0) != INTEGER_CST || TREE_CODE (arg1) != INTEGER_CST) | |
389dd41b | 1498 | return size_binop_loc (loc, MINUS_EXPR, |
1499 | fold_convert_loc (loc, ctype, arg0), | |
1500 | fold_convert_loc (loc, ctype, arg1)); | |
902de8ed | 1501 | |
1502 | /* If ARG0 is larger than ARG1, subtract and return the result in CTYPE. | |
1503 | Otherwise, subtract the other way, convert to CTYPE (we know that can't | |
1504 | overflow) and negate (which can't either). Special-case a result | |
1505 | of zero while we're here. */ | |
1506 | if (tree_int_cst_equal (arg0, arg1)) | |
3c6185f1 | 1507 | return build_int_cst (ctype, 0); |
902de8ed | 1508 | else if (tree_int_cst_lt (arg1, arg0)) |
389dd41b | 1509 | return fold_convert_loc (loc, ctype, |
1510 | size_binop_loc (loc, MINUS_EXPR, arg0, arg1)); | |
902de8ed | 1511 | else |
389dd41b | 1512 | return size_binop_loc (loc, MINUS_EXPR, build_int_cst (ctype, 0), |
1513 | fold_convert_loc (loc, ctype, | |
1514 | size_binop_loc (loc, | |
1515 | MINUS_EXPR, | |
1516 | arg1, arg0))); | |
3fd3b688 | 1517 | } |
2bc77e10 | 1518 | \f |
b38d56be | 1519 | /* A subroutine of fold_convert_const handling conversions of an |
1520 | INTEGER_CST to another integer type. */ | |
80db63ef | 1521 | |
1522 | static tree | |
b4b34335 | 1523 | fold_convert_const_int_from_int (tree type, const_tree arg1) |
80db63ef | 1524 | { |
b38d56be | 1525 | tree t; |
80db63ef | 1526 | |
b38d56be | 1527 | /* Given an integer constant, make new constant with new type, |
1528 | appropriately sign-extended or truncated. */ | |
c8110c8f | 1529 | t = force_fit_type_double (type, TREE_INT_CST_LOW (arg1), |
1530 | TREE_INT_CST_HIGH (arg1), | |
c6feb9f1 | 1531 | !POINTER_TYPE_P (TREE_TYPE (arg1)), |
c8110c8f | 1532 | (TREE_INT_CST_HIGH (arg1) < 0 |
1533 | && (TYPE_UNSIGNED (type) | |
1534 | < TYPE_UNSIGNED (TREE_TYPE (arg1)))) | |
eddad94a | 1535 | | TREE_OVERFLOW (arg1)); |
80db63ef | 1536 | |
b38d56be | 1537 | return t; |
80db63ef | 1538 | } |
1539 | ||
b38d56be | 1540 | /* A subroutine of fold_convert_const handling conversions a REAL_CST |
1541 | to an integer type. */ | |
2bc77e10 | 1542 | |
1543 | static tree | |
b4b34335 | 1544 | fold_convert_const_int_from_real (enum tree_code code, tree type, const_tree arg1) |
2bc77e10 | 1545 | { |
23fed9b2 | 1546 | int overflow = 0; |
04b253e8 | 1547 | tree t; |
1548 | ||
b38d56be | 1549 | /* The following code implements the floating point to integer |
1550 | conversion rules required by the Java Language Specification, | |
1551 | that IEEE NaNs are mapped to zero and values that overflow | |
1552 | the target precision saturate, i.e. values greater than | |
1553 | INT_MAX are mapped to INT_MAX, and values less than INT_MIN | |
1554 | are mapped to INT_MIN. These semantics are allowed by the | |
1555 | C and C++ standards that simply state that the behavior of | |
1556 | FP-to-integer conversion is unspecified upon overflow. */ | |
2bc77e10 | 1557 | |
41283922 | 1558 | double_int val; |
b38d56be | 1559 | REAL_VALUE_TYPE r; |
1560 | REAL_VALUE_TYPE x = TREE_REAL_CST (arg1); | |
1561 | ||
1562 | switch (code) | |
2bc77e10 | 1563 | { |
b38d56be | 1564 | case FIX_TRUNC_EXPR: |
1565 | real_trunc (&r, VOIDmode, &x); | |
1566 | break; | |
1567 | ||
b38d56be | 1568 | default: |
1569 | gcc_unreachable (); | |
1570 | } | |
1571 | ||
1572 | /* If R is NaN, return zero and show we have an overflow. */ | |
1573 | if (REAL_VALUE_ISNAN (r)) | |
1574 | { | |
1575 | overflow = 1; | |
41283922 | 1576 | val = double_int_zero; |
b38d56be | 1577 | } |
1578 | ||
1579 | /* See if R is less than the lower bound or greater than the | |
1580 | upper bound. */ | |
1581 | ||
1582 | if (! overflow) | |
1583 | { | |
1584 | tree lt = TYPE_MIN_VALUE (type); | |
1585 | REAL_VALUE_TYPE l = real_value_from_int_cst (NULL_TREE, lt); | |
1586 | if (REAL_VALUES_LESS (r, l)) | |
2bc77e10 | 1587 | { |
b38d56be | 1588 | overflow = 1; |
41283922 | 1589 | val = tree_to_double_int (lt); |
2bc77e10 | 1590 | } |
b38d56be | 1591 | } |
1592 | ||
1593 | if (! overflow) | |
1594 | { | |
1595 | tree ut = TYPE_MAX_VALUE (type); | |
1596 | if (ut) | |
2bc77e10 | 1597 | { |
b38d56be | 1598 | REAL_VALUE_TYPE u = real_value_from_int_cst (NULL_TREE, ut); |
1599 | if (REAL_VALUES_LESS (u, r)) | |
04b253e8 | 1600 | { |
b38d56be | 1601 | overflow = 1; |
41283922 | 1602 | val = tree_to_double_int (ut); |
b38d56be | 1603 | } |
1604 | } | |
1605 | } | |
04b253e8 | 1606 | |
b38d56be | 1607 | if (! overflow) |
41283922 | 1608 | real_to_integer2 ((HOST_WIDE_INT *) &val.low, &val.high, &r); |
04b253e8 | 1609 | |
41283922 | 1610 | t = force_fit_type_double (type, val.low, val.high, -1, |
eddad94a | 1611 | overflow | TREE_OVERFLOW (arg1)); |
b38d56be | 1612 | return t; |
1613 | } | |
50c90ea2 | 1614 | |
06f0b99c | 1615 | /* A subroutine of fold_convert_const handling conversions of a |
1616 | FIXED_CST to an integer type. */ | |
1617 | ||
1618 | static tree | |
b4b34335 | 1619 | fold_convert_const_int_from_fixed (tree type, const_tree arg1) |
06f0b99c | 1620 | { |
1621 | tree t; | |
1622 | double_int temp, temp_trunc; | |
1623 | unsigned int mode; | |
1624 | ||
1625 | /* Right shift FIXED_CST to temp by fbit. */ | |
1626 | temp = TREE_FIXED_CST (arg1).data; | |
1627 | mode = TREE_FIXED_CST (arg1).mode; | |
1628 | if (GET_MODE_FBIT (mode) < 2 * HOST_BITS_PER_WIDE_INT) | |
1629 | { | |
41283922 | 1630 | temp = double_int_rshift (temp, GET_MODE_FBIT (mode), |
1631 | HOST_BITS_PER_DOUBLE_INT, | |
1632 | SIGNED_FIXED_POINT_MODE_P (mode)); | |
06f0b99c | 1633 | |
1634 | /* Left shift temp to temp_trunc by fbit. */ | |
41283922 | 1635 | temp_trunc = double_int_lshift (temp, GET_MODE_FBIT (mode), |
1636 | HOST_BITS_PER_DOUBLE_INT, | |
1637 | SIGNED_FIXED_POINT_MODE_P (mode)); | |
06f0b99c | 1638 | } |
1639 | else | |
1640 | { | |
41283922 | 1641 | temp = double_int_zero; |
1642 | temp_trunc = double_int_zero; | |
06f0b99c | 1643 | } |
1644 | ||
1645 | /* If FIXED_CST is negative, we need to round the value toward 0. | |
1646 | By checking if the fractional bits are not zero to add 1 to temp. */ | |
41283922 | 1647 | if (SIGNED_FIXED_POINT_MODE_P (mode) |
1648 | && double_int_negative_p (temp_trunc) | |
06f0b99c | 1649 | && !double_int_equal_p (TREE_FIXED_CST (arg1).data, temp_trunc)) |
41283922 | 1650 | temp = double_int_add (temp, double_int_one); |
06f0b99c | 1651 | |
1652 | /* Given a fixed-point constant, make new constant with new type, | |
1653 | appropriately sign-extended or truncated. */ | |
1654 | t = force_fit_type_double (type, temp.low, temp.high, -1, | |
41283922 | 1655 | (double_int_negative_p (temp) |
06f0b99c | 1656 | && (TYPE_UNSIGNED (type) |
1657 | < TYPE_UNSIGNED (TREE_TYPE (arg1)))) | |
1658 | | TREE_OVERFLOW (arg1)); | |
1659 | ||
1660 | return t; | |
1661 | } | |
1662 | ||
b38d56be | 1663 | /* A subroutine of fold_convert_const handling conversions a REAL_CST |
1664 | to another floating point type. */ | |
04b253e8 | 1665 | |
b38d56be | 1666 | static tree |
b4b34335 | 1667 | fold_convert_const_real_from_real (tree type, const_tree arg1) |
b38d56be | 1668 | { |
a47b9d79 | 1669 | REAL_VALUE_TYPE value; |
b38d56be | 1670 | tree t; |
f52483b5 | 1671 | |
a47b9d79 | 1672 | real_convert (&value, TYPE_MODE (type), &TREE_REAL_CST (arg1)); |
1673 | t = build_real (type, value); | |
67c65562 | 1674 | |
275b395f | 1675 | /* If converting an infinity or NAN to a representation that doesn't |
1676 | have one, set the overflow bit so that we can produce some kind of | |
1677 | error message at the appropriate point if necessary. It's not the | |
1678 | most user-friendly message, but it's better than nothing. */ | |
1679 | if (REAL_VALUE_ISINF (TREE_REAL_CST (arg1)) | |
1680 | && !MODE_HAS_INFINITIES (TYPE_MODE (type))) | |
1681 | TREE_OVERFLOW (t) = 1; | |
1682 | else if (REAL_VALUE_ISNAN (TREE_REAL_CST (arg1)) | |
1683 | && !MODE_HAS_NANS (TYPE_MODE (type))) | |
1684 | TREE_OVERFLOW (t) = 1; | |
1685 | /* Regular overflow, conversion produced an infinity in a mode that | |
1686 | can't represent them. */ | |
1687 | else if (!MODE_HAS_INFINITIES (TYPE_MODE (type)) | |
1688 | && REAL_VALUE_ISINF (value) | |
1689 | && !REAL_VALUE_ISINF (TREE_REAL_CST (arg1))) | |
1690 | TREE_OVERFLOW (t) = 1; | |
1691 | else | |
1692 | TREE_OVERFLOW (t) = TREE_OVERFLOW (arg1); | |
b38d56be | 1693 | return t; |
1694 | } | |
67c65562 | 1695 | |
06f0b99c | 1696 | /* A subroutine of fold_convert_const handling conversions a FIXED_CST |
1697 | to a floating point type. */ | |
1698 | ||
1699 | static tree | |
b4b34335 | 1700 | fold_convert_const_real_from_fixed (tree type, const_tree arg1) |
06f0b99c | 1701 | { |
1702 | REAL_VALUE_TYPE value; | |
1703 | tree t; | |
1704 | ||
1705 | real_convert_from_fixed (&value, TYPE_MODE (type), &TREE_FIXED_CST (arg1)); | |
1706 | t = build_real (type, value); | |
1707 | ||
1708 | TREE_OVERFLOW (t) = TREE_OVERFLOW (arg1); | |
06f0b99c | 1709 | return t; |
1710 | } | |
1711 | ||
1712 | /* A subroutine of fold_convert_const handling conversions a FIXED_CST | |
1713 | to another fixed-point type. */ | |
1714 | ||
1715 | static tree | |
b4b34335 | 1716 | fold_convert_const_fixed_from_fixed (tree type, const_tree arg1) |
06f0b99c | 1717 | { |
1718 | FIXED_VALUE_TYPE value; | |
1719 | tree t; | |
1720 | bool overflow_p; | |
1721 | ||
1722 | overflow_p = fixed_convert (&value, TYPE_MODE (type), &TREE_FIXED_CST (arg1), | |
1723 | TYPE_SATURATING (type)); | |
1724 | t = build_fixed (type, value); | |
1725 | ||
1726 | /* Propagate overflow flags. */ | |
1727 | if (overflow_p | TREE_OVERFLOW (arg1)) | |
62126877 | 1728 | TREE_OVERFLOW (t) = 1; |
06f0b99c | 1729 | return t; |
1730 | } | |
1731 | ||
1732 | /* A subroutine of fold_convert_const handling conversions an INTEGER_CST | |
1733 | to a fixed-point type. */ | |
1734 | ||
1735 | static tree | |
b4b34335 | 1736 | fold_convert_const_fixed_from_int (tree type, const_tree arg1) |
06f0b99c | 1737 | { |
1738 | FIXED_VALUE_TYPE value; | |
1739 | tree t; | |
1740 | bool overflow_p; | |
1741 | ||
1742 | overflow_p = fixed_convert_from_int (&value, TYPE_MODE (type), | |
1743 | TREE_INT_CST (arg1), | |
1744 | TYPE_UNSIGNED (TREE_TYPE (arg1)), | |
1745 | TYPE_SATURATING (type)); | |
1746 | t = build_fixed (type, value); | |
1747 | ||
1748 | /* Propagate overflow flags. */ | |
1749 | if (overflow_p | TREE_OVERFLOW (arg1)) | |
62126877 | 1750 | TREE_OVERFLOW (t) = 1; |
06f0b99c | 1751 | return t; |
1752 | } | |
1753 | ||
1754 | /* A subroutine of fold_convert_const handling conversions a REAL_CST | |
1755 | to a fixed-point type. */ | |
1756 | ||
1757 | static tree | |
b4b34335 | 1758 | fold_convert_const_fixed_from_real (tree type, const_tree arg1) |
06f0b99c | 1759 | { |
1760 | FIXED_VALUE_TYPE value; | |
1761 | tree t; | |
1762 | bool overflow_p; | |
1763 | ||
1764 | overflow_p = fixed_convert_from_real (&value, TYPE_MODE (type), | |
1765 | &TREE_REAL_CST (arg1), | |
1766 | TYPE_SATURATING (type)); | |
1767 | t = build_fixed (type, value); | |
1768 | ||
1769 | /* Propagate overflow flags. */ | |
1770 | if (overflow_p | TREE_OVERFLOW (arg1)) | |
62126877 | 1771 | TREE_OVERFLOW (t) = 1; |
06f0b99c | 1772 | return t; |
1773 | } | |
1774 | ||
b38d56be | 1775 | /* Attempt to fold type conversion operation CODE of expression ARG1 to |
1776 | type TYPE. If no simplification can be done return NULL_TREE. */ | |
67c65562 | 1777 | |
b38d56be | 1778 | static tree |
1779 | fold_convert_const (enum tree_code code, tree type, tree arg1) | |
1780 | { | |
1781 | if (TREE_TYPE (arg1) == type) | |
1782 | return arg1; | |
4d28c5d1 | 1783 | |
0bafabac | 1784 | if (POINTER_TYPE_P (type) || INTEGRAL_TYPE_P (type) |
1785 | || TREE_CODE (type) == OFFSET_TYPE) | |
b38d56be | 1786 | { |
1787 | if (TREE_CODE (arg1) == INTEGER_CST) | |
1788 | return fold_convert_const_int_from_int (type, arg1); | |
1789 | else if (TREE_CODE (arg1) == REAL_CST) | |
1790 | return fold_convert_const_int_from_real (code, type, arg1); | |
06f0b99c | 1791 | else if (TREE_CODE (arg1) == FIXED_CST) |
1792 | return fold_convert_const_int_from_fixed (type, arg1); | |
2bc77e10 | 1793 | } |
1794 | else if (TREE_CODE (type) == REAL_TYPE) | |
1795 | { | |
2bc77e10 | 1796 | if (TREE_CODE (arg1) == INTEGER_CST) |
1797 | return build_real_from_int_cst (type, arg1); | |
06f0b99c | 1798 | else if (TREE_CODE (arg1) == REAL_CST) |
b38d56be | 1799 | return fold_convert_const_real_from_real (type, arg1); |
06f0b99c | 1800 | else if (TREE_CODE (arg1) == FIXED_CST) |
1801 | return fold_convert_const_real_from_fixed (type, arg1); | |
1802 | } | |
1803 | else if (TREE_CODE (type) == FIXED_POINT_TYPE) | |
1804 | { | |
1805 | if (TREE_CODE (arg1) == FIXED_CST) | |
1806 | return fold_convert_const_fixed_from_fixed (type, arg1); | |
1807 | else if (TREE_CODE (arg1) == INTEGER_CST) | |
1808 | return fold_convert_const_fixed_from_int (type, arg1); | |
1809 | else if (TREE_CODE (arg1) == REAL_CST) | |
1810 | return fold_convert_const_fixed_from_real (type, arg1); | |
2bc77e10 | 1811 | } |
04b253e8 | 1812 | return NULL_TREE; |
2bc77e10 | 1813 | } |
b30e3dbc | 1814 | |
b38d56be | 1815 | /* Construct a vector of zero elements of vector type TYPE. */ |
1816 | ||
1817 | static tree | |
1818 | build_zero_vector (tree type) | |
1819 | { | |
1820 | tree elem, list; | |
1821 | int i, units; | |
1822 | ||
1823 | elem = fold_convert_const (NOP_EXPR, TREE_TYPE (type), integer_zero_node); | |
1824 | units = TYPE_VECTOR_SUBPARTS (type); | |
48e1416a | 1825 | |
b38d56be | 1826 | list = NULL_TREE; |
1827 | for (i = 0; i < units; i++) | |
1828 | list = tree_cons (NULL_TREE, elem, list); | |
1829 | return build_vector (type, list); | |
1830 | } | |
1831 | ||
f549b28d | 1832 | /* Returns true, if ARG is convertible to TYPE using a NOP_EXPR. */ |
1833 | ||
1834 | bool | |
b7bf20db | 1835 | fold_convertible_p (const_tree type, const_tree arg) |
f549b28d | 1836 | { |
1837 | tree orig = TREE_TYPE (arg); | |
1838 | ||
1839 | if (type == orig) | |
1840 | return true; | |
1841 | ||
1842 | if (TREE_CODE (arg) == ERROR_MARK | |
1843 | || TREE_CODE (type) == ERROR_MARK | |
1844 | || TREE_CODE (orig) == ERROR_MARK) | |
1845 | return false; | |
1846 | ||
1847 | if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (orig)) | |
1848 | return true; | |
1849 | ||
1850 | switch (TREE_CODE (type)) | |
1851 | { | |
1852 | case INTEGER_TYPE: case ENUMERAL_TYPE: case BOOLEAN_TYPE: | |
1853 | case POINTER_TYPE: case REFERENCE_TYPE: | |
1854 | case OFFSET_TYPE: | |
1855 | if (INTEGRAL_TYPE_P (orig) || POINTER_TYPE_P (orig) | |
1856 | || TREE_CODE (orig) == OFFSET_TYPE) | |
1857 | return true; | |
1858 | return (TREE_CODE (orig) == VECTOR_TYPE | |
1859 | && tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (orig))); | |
1860 | ||
4a2f7014 | 1861 | case REAL_TYPE: |
1862 | case FIXED_POINT_TYPE: | |
1863 | case COMPLEX_TYPE: | |
1864 | case VECTOR_TYPE: | |
1865 | case VOID_TYPE: | |
f549b28d | 1866 | return TREE_CODE (type) == TREE_CODE (orig); |
4a2f7014 | 1867 | |
1868 | default: | |
1869 | return false; | |
f549b28d | 1870 | } |
1871 | } | |
1872 | ||
b30e3dbc | 1873 | /* Convert expression ARG to type TYPE. Used by the middle-end for |
1874 | simple conversions in preference to calling the front-end's convert. */ | |
1875 | ||
d7aeca92 | 1876 | tree |
389dd41b | 1877 | fold_convert_loc (location_t loc, tree type, tree arg) |
b30e3dbc | 1878 | { |
1879 | tree orig = TREE_TYPE (arg); | |
1880 | tree tem; | |
1881 | ||
1882 | if (type == orig) | |
1883 | return arg; | |
1884 | ||
1885 | if (TREE_CODE (arg) == ERROR_MARK | |
1886 | || TREE_CODE (type) == ERROR_MARK | |
1887 | || TREE_CODE (orig) == ERROR_MARK) | |
1888 | return error_mark_node; | |
1889 | ||
c8ca3ee7 | 1890 | if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (orig)) |
389dd41b | 1891 | return fold_build1_loc (loc, NOP_EXPR, type, arg); |
b30e3dbc | 1892 | |
fdada98f | 1893 | switch (TREE_CODE (type)) |
b30e3dbc | 1894 | { |
bd1a81f7 | 1895 | case POINTER_TYPE: |
1896 | case REFERENCE_TYPE: | |
1897 | /* Handle conversions between pointers to different address spaces. */ | |
1898 | if (POINTER_TYPE_P (orig) | |
1899 | && (TYPE_ADDR_SPACE (TREE_TYPE (type)) | |
1900 | != TYPE_ADDR_SPACE (TREE_TYPE (orig)))) | |
1901 | return fold_build1_loc (loc, ADDR_SPACE_CONVERT_EXPR, type, arg); | |
1902 | /* fall through */ | |
1903 | ||
63bf54cf | 1904 | case INTEGER_TYPE: case ENUMERAL_TYPE: case BOOLEAN_TYPE: |
fdada98f | 1905 | case OFFSET_TYPE: |
b30e3dbc | 1906 | if (TREE_CODE (arg) == INTEGER_CST) |
1907 | { | |
1908 | tem = fold_convert_const (NOP_EXPR, type, arg); | |
1909 | if (tem != NULL_TREE) | |
1910 | return tem; | |
1911 | } | |
8d4b8f86 | 1912 | if (INTEGRAL_TYPE_P (orig) || POINTER_TYPE_P (orig) |
1913 | || TREE_CODE (orig) == OFFSET_TYPE) | |
389dd41b | 1914 | return fold_build1_loc (loc, NOP_EXPR, type, arg); |
b30e3dbc | 1915 | if (TREE_CODE (orig) == COMPLEX_TYPE) |
389dd41b | 1916 | return fold_convert_loc (loc, type, |
1917 | fold_build1_loc (loc, REALPART_EXPR, | |
1918 | TREE_TYPE (orig), arg)); | |
fdada98f | 1919 | gcc_assert (TREE_CODE (orig) == VECTOR_TYPE |
1920 | && tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (orig))); | |
389dd41b | 1921 | return fold_build1_loc (loc, NOP_EXPR, type, arg); |
0c5713a2 | 1922 | |
fdada98f | 1923 | case REAL_TYPE: |
b30e3dbc | 1924 | if (TREE_CODE (arg) == INTEGER_CST) |
1925 | { | |
1926 | tem = fold_convert_const (FLOAT_EXPR, type, arg); | |
1927 | if (tem != NULL_TREE) | |
1928 | return tem; | |
1929 | } | |
1930 | else if (TREE_CODE (arg) == REAL_CST) | |
1931 | { | |
1932 | tem = fold_convert_const (NOP_EXPR, type, arg); | |
1933 | if (tem != NULL_TREE) | |
1934 | return tem; | |
1935 | } | |
06f0b99c | 1936 | else if (TREE_CODE (arg) == FIXED_CST) |
1937 | { | |
1938 | tem = fold_convert_const (FIXED_CONVERT_EXPR, type, arg); | |
1939 | if (tem != NULL_TREE) | |
1940 | return tem; | |
1941 | } | |
b30e3dbc | 1942 | |
fdada98f | 1943 | switch (TREE_CODE (orig)) |
b30e3dbc | 1944 | { |
63bf54cf | 1945 | case INTEGER_TYPE: |
fdada98f | 1946 | case BOOLEAN_TYPE: case ENUMERAL_TYPE: |
1947 | case POINTER_TYPE: case REFERENCE_TYPE: | |
389dd41b | 1948 | return fold_build1_loc (loc, FLOAT_EXPR, type, arg); |
0c5713a2 | 1949 | |
fdada98f | 1950 | case REAL_TYPE: |
389dd41b | 1951 | return fold_build1_loc (loc, NOP_EXPR, type, arg); |
0c5713a2 | 1952 | |
06f0b99c | 1953 | case FIXED_POINT_TYPE: |
389dd41b | 1954 | return fold_build1_loc (loc, FIXED_CONVERT_EXPR, type, arg); |
06f0b99c | 1955 | |
1956 | case COMPLEX_TYPE: | |
389dd41b | 1957 | tem = fold_build1_loc (loc, REALPART_EXPR, TREE_TYPE (orig), arg); |
1958 | return fold_convert_loc (loc, type, tem); | |
06f0b99c | 1959 | |
1960 | default: | |
1961 | gcc_unreachable (); | |
1962 | } | |
1963 | ||
1964 | case FIXED_POINT_TYPE: | |
1965 | if (TREE_CODE (arg) == FIXED_CST || TREE_CODE (arg) == INTEGER_CST | |
1966 | || TREE_CODE (arg) == REAL_CST) | |
1967 | { | |
1968 | tem = fold_convert_const (FIXED_CONVERT_EXPR, type, arg); | |
1969 | if (tem != NULL_TREE) | |
389dd41b | 1970 | goto fold_convert_exit; |
06f0b99c | 1971 | } |
1972 | ||
1973 | switch (TREE_CODE (orig)) | |
1974 | { | |
1975 | case FIXED_POINT_TYPE: | |
1976 | case INTEGER_TYPE: | |
1977 | case ENUMERAL_TYPE: | |
1978 | case BOOLEAN_TYPE: | |
1979 | case REAL_TYPE: | |
389dd41b | 1980 | return fold_build1_loc (loc, FIXED_CONVERT_EXPR, type, arg); |
06f0b99c | 1981 | |
fdada98f | 1982 | case COMPLEX_TYPE: |
389dd41b | 1983 | tem = fold_build1_loc (loc, REALPART_EXPR, TREE_TYPE (orig), arg); |
1984 | return fold_convert_loc (loc, type, tem); | |
0c5713a2 | 1985 | |
fdada98f | 1986 | default: |
1987 | gcc_unreachable (); | |
b30e3dbc | 1988 | } |
0c5713a2 | 1989 | |
fdada98f | 1990 | case COMPLEX_TYPE: |
1991 | switch (TREE_CODE (orig)) | |
1992 | { | |
63bf54cf | 1993 | case INTEGER_TYPE: |
fdada98f | 1994 | case BOOLEAN_TYPE: case ENUMERAL_TYPE: |
1995 | case POINTER_TYPE: case REFERENCE_TYPE: | |
1996 | case REAL_TYPE: | |
06f0b99c | 1997 | case FIXED_POINT_TYPE: |
389dd41b | 1998 | return fold_build2_loc (loc, COMPLEX_EXPR, type, |
1999 | fold_convert_loc (loc, TREE_TYPE (type), arg), | |
2000 | fold_convert_loc (loc, TREE_TYPE (type), | |
261b246f | 2001 | integer_zero_node)); |
fdada98f | 2002 | case COMPLEX_TYPE: |
2003 | { | |
2004 | tree rpart, ipart; | |
0c5713a2 | 2005 | |
fdada98f | 2006 | if (TREE_CODE (arg) == COMPLEX_EXPR) |
2007 | { | |
389dd41b | 2008 | rpart = fold_convert_loc (loc, TREE_TYPE (type), |
2009 | TREE_OPERAND (arg, 0)); | |
2010 | ipart = fold_convert_loc (loc, TREE_TYPE (type), | |
2011 | TREE_OPERAND (arg, 1)); | |
2012 | return fold_build2_loc (loc, COMPLEX_EXPR, type, rpart, ipart); | |
fdada98f | 2013 | } |
0c5713a2 | 2014 | |
fdada98f | 2015 | arg = save_expr (arg); |
389dd41b | 2016 | rpart = fold_build1_loc (loc, REALPART_EXPR, TREE_TYPE (orig), arg); |
2017 | ipart = fold_build1_loc (loc, IMAGPART_EXPR, TREE_TYPE (orig), arg); | |
2018 | rpart = fold_convert_loc (loc, TREE_TYPE (type), rpart); | |
2019 | ipart = fold_convert_loc (loc, TREE_TYPE (type), ipart); | |
2020 | return fold_build2_loc (loc, COMPLEX_EXPR, type, rpart, ipart); | |
fdada98f | 2021 | } |
0c5713a2 | 2022 | |
fdada98f | 2023 | default: |
2024 | gcc_unreachable (); | |
2025 | } | |
0c5713a2 | 2026 | |
fdada98f | 2027 | case VECTOR_TYPE: |
80db63ef | 2028 | if (integer_zerop (arg)) |
2029 | return build_zero_vector (type); | |
fdada98f | 2030 | gcc_assert (tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (orig))); |
2031 | gcc_assert (INTEGRAL_TYPE_P (orig) || POINTER_TYPE_P (orig) | |
2032 | || TREE_CODE (orig) == VECTOR_TYPE); | |
389dd41b | 2033 | return fold_build1_loc (loc, VIEW_CONVERT_EXPR, type, arg); |
b30e3dbc | 2034 | |
fdada98f | 2035 | case VOID_TYPE: |
df0137b9 | 2036 | tem = fold_ignored_result (arg); |
75a70cf9 | 2037 | if (TREE_CODE (tem) == MODIFY_EXPR) |
389dd41b | 2038 | goto fold_convert_exit; |
2039 | return fold_build1_loc (loc, NOP_EXPR, type, tem); | |
b30e3dbc | 2040 | |
fdada98f | 2041 | default: |
2042 | gcc_unreachable (); | |
b30e3dbc | 2043 | } |
389dd41b | 2044 | fold_convert_exit: |
2045 | protected_set_expr_location (tem, loc); | |
2046 | return tem; | |
b30e3dbc | 2047 | } |
2bc77e10 | 2048 | \f |
77aa6362 | 2049 | /* Return false if expr can be assumed not to be an lvalue, true |
98fc7ffa | 2050 | otherwise. */ |
2bc77e10 | 2051 | |
98fc7ffa | 2052 | static bool |
b4b34335 | 2053 | maybe_lvalue_p (const_tree x) |
2bc77e10 | 2054 | { |
f4d47aeb | 2055 | /* We only need to wrap lvalue tree codes. */ |
2056 | switch (TREE_CODE (x)) | |
2057 | { | |
2058 | case VAR_DECL: | |
2059 | case PARM_DECL: | |
2060 | case RESULT_DECL: | |
2061 | case LABEL_DECL: | |
2062 | case FUNCTION_DECL: | |
2063 | case SSA_NAME: | |
2064 | ||
2065 | case COMPONENT_REF: | |
2066 | case INDIRECT_REF: | |
b056d812 | 2067 | case ALIGN_INDIRECT_REF: |
2068 | case MISALIGNED_INDIRECT_REF: | |
f4d47aeb | 2069 | case ARRAY_REF: |
6374121b | 2070 | case ARRAY_RANGE_REF: |
f4d47aeb | 2071 | case BIT_FIELD_REF: |
215e2f1d | 2072 | case OBJ_TYPE_REF: |
f4d47aeb | 2073 | |
2074 | case REALPART_EXPR: | |
2075 | case IMAGPART_EXPR: | |
2076 | case PREINCREMENT_EXPR: | |
2077 | case PREDECREMENT_EXPR: | |
2078 | case SAVE_EXPR: | |
f4d47aeb | 2079 | case TRY_CATCH_EXPR: |
2080 | case WITH_CLEANUP_EXPR: | |
2081 | case COMPOUND_EXPR: | |
2082 | case MODIFY_EXPR: | |
2083 | case TARGET_EXPR: | |
2084 | case COND_EXPR: | |
2085 | case BIND_EXPR: | |
f4d47aeb | 2086 | break; |
2087 | ||
2088 | default: | |
2089 | /* Assume the worst for front-end tree codes. */ | |
2090 | if ((int)TREE_CODE (x) >= NUM_TREE_CODES) | |
2091 | break; | |
98fc7ffa | 2092 | return false; |
f4d47aeb | 2093 | } |
98fc7ffa | 2094 | |
2095 | return true; | |
2096 | } | |
2097 | ||
2098 | /* Return an expr equal to X but certainly not valid as an lvalue. */ | |
2099 | ||
2100 | tree | |
389dd41b | 2101 | non_lvalue_loc (location_t loc, tree x) |
98fc7ffa | 2102 | { |
2103 | /* While we are in GIMPLE, NON_LVALUE_EXPR doesn't mean anything to | |
2104 | us. */ | |
2105 | if (in_gimple_form) | |
2106 | return x; | |
2107 | ||
2108 | if (! maybe_lvalue_p (x)) | |
2109 | return x; | |
389dd41b | 2110 | x = build1 (NON_LVALUE_EXPR, TREE_TYPE (x), x); |
2111 | SET_EXPR_LOCATION (x, loc); | |
2112 | return x; | |
2bc77e10 | 2113 | } |
56753054 | 2114 | |
b12c26dc | 2115 | /* Nonzero means lvalues are limited to those valid in pedantic ANSI C. |
2116 | Zero means allow extended lvalues. */ | |
2117 | ||
2118 | int pedantic_lvalues; | |
2119 | ||
56753054 | 2120 | /* When pedantic, return an expr equal to X but certainly not valid as a |
2121 | pedantic lvalue. Otherwise, return X. */ | |
2122 | ||
d50efa49 | 2123 | static tree |
389dd41b | 2124 | pedantic_non_lvalue_loc (location_t loc, tree x) |
56753054 | 2125 | { |
b12c26dc | 2126 | if (pedantic_lvalues) |
389dd41b | 2127 | return non_lvalue_loc (loc, x); |
2128 | protected_set_expr_location (x, loc); | |
2129 | return x; | |
56753054 | 2130 | } |
e233264a | 2131 | \f |
2132 | /* Given a tree comparison code, return the code that is the logical inverse | |
2133 | of the given code. It is not safe to do this for floating-point | |
318a728f | 2134 | comparisons, except for NE_EXPR and EQ_EXPR, so we receive a machine mode |
2135 | as well: if reversing the comparison is unsafe, return ERROR_MARK. */ | |
2bc77e10 | 2136 | |
eea12c72 | 2137 | enum tree_code |
318a728f | 2138 | invert_tree_comparison (enum tree_code code, bool honor_nans) |
e233264a | 2139 | { |
318a728f | 2140 | if (honor_nans && flag_trapping_math) |
2141 | return ERROR_MARK; | |
2142 | ||
e233264a | 2143 | switch (code) |
2144 | { | |
2145 | case EQ_EXPR: | |
2146 | return NE_EXPR; | |
2147 | case NE_EXPR: | |
2148 | return EQ_EXPR; | |
2149 | case GT_EXPR: | |
318a728f | 2150 | return honor_nans ? UNLE_EXPR : LE_EXPR; |
e233264a | 2151 | case GE_EXPR: |
318a728f | 2152 | return honor_nans ? UNLT_EXPR : LT_EXPR; |
e233264a | 2153 | case LT_EXPR: |
318a728f | 2154 | return honor_nans ? UNGE_EXPR : GE_EXPR; |
e233264a | 2155 | case LE_EXPR: |
318a728f | 2156 | return honor_nans ? UNGT_EXPR : GT_EXPR; |
2157 | case LTGT_EXPR: | |
2158 | return UNEQ_EXPR; | |
2159 | case UNEQ_EXPR: | |
2160 | return LTGT_EXPR; | |
2161 | case UNGT_EXPR: | |
2162 | return LE_EXPR; | |
2163 | case UNGE_EXPR: | |
2164 | return LT_EXPR; | |
2165 | case UNLT_EXPR: | |
2166 | return GE_EXPR; | |
2167 | case UNLE_EXPR: | |
e233264a | 2168 | return GT_EXPR; |
318a728f | 2169 | case ORDERED_EXPR: |
2170 | return UNORDERED_EXPR; | |
2171 | case UNORDERED_EXPR: | |
2172 | return ORDERED_EXPR; | |
e233264a | 2173 | default: |
fdada98f | 2174 | gcc_unreachable (); |
e233264a | 2175 | } |
2176 | } | |
2177 | ||
2178 | /* Similar, but return the comparison that results if the operands are | |
2179 | swapped. This is safe for floating-point. */ | |
2180 | ||
cc0bdf91 | 2181 | enum tree_code |
de1b648b | 2182 | swap_tree_comparison (enum tree_code code) |
e233264a | 2183 | { |
2184 | switch (code) | |
2185 | { | |
2186 | case EQ_EXPR: | |
2187 | case NE_EXPR: | |
6a0aeeaa | 2188 | case ORDERED_EXPR: |
2189 | case UNORDERED_EXPR: | |
2190 | case LTGT_EXPR: | |
2191 | case UNEQ_EXPR: | |
e233264a | 2192 | return code; |
2193 | case GT_EXPR: | |
2194 | return LT_EXPR; | |
2195 | case GE_EXPR: | |
2196 | return LE_EXPR; | |
2197 | case LT_EXPR: | |
2198 | return GT_EXPR; | |
2199 | case LE_EXPR: | |
2200 | return GE_EXPR; | |
6a0aeeaa | 2201 | case UNGT_EXPR: |
2202 | return UNLT_EXPR; | |
2203 | case UNGE_EXPR: | |
2204 | return UNLE_EXPR; | |
2205 | case UNLT_EXPR: | |
2206 | return UNGT_EXPR; | |
2207 | case UNLE_EXPR: | |
2208 | return UNGE_EXPR; | |
e233264a | 2209 | default: |
fdada98f | 2210 | gcc_unreachable (); |
e233264a | 2211 | } |
2212 | } | |
8b94828f | 2213 | |
7835f163 | 2214 | |
2215 | /* Convert a comparison tree code from an enum tree_code representation | |
2216 | into a compcode bit-based encoding. This function is the inverse of | |
2217 | compcode_to_comparison. */ | |
2218 | ||
318a728f | 2219 | static enum comparison_code |
de1b648b | 2220 | comparison_to_compcode (enum tree_code code) |
7835f163 | 2221 | { |
2222 | switch (code) | |
2223 | { | |
2224 | case LT_EXPR: | |
2225 | return COMPCODE_LT; | |
2226 | case EQ_EXPR: | |
2227 | return COMPCODE_EQ; | |
2228 | case LE_EXPR: | |
2229 | return COMPCODE_LE; | |
2230 | case GT_EXPR: | |
2231 | return COMPCODE_GT; | |
2232 | case NE_EXPR: | |
2233 | return COMPCODE_NE; | |
2234 | case GE_EXPR: | |
2235 | return COMPCODE_GE; | |
318a728f | 2236 | case ORDERED_EXPR: |
2237 | return COMPCODE_ORD; | |
2238 | case UNORDERED_EXPR: | |
2239 | return COMPCODE_UNORD; | |
2240 | case UNLT_EXPR: | |
2241 | return COMPCODE_UNLT; | |
2242 | case UNEQ_EXPR: | |
2243 | return COMPCODE_UNEQ; | |
2244 | case UNLE_EXPR: | |
2245 | return COMPCODE_UNLE; | |
2246 | case UNGT_EXPR: | |
2247 | return COMPCODE_UNGT; | |
2248 | case LTGT_EXPR: | |
2249 | return COMPCODE_LTGT; | |
2250 | case UNGE_EXPR: | |
2251 | return COMPCODE_UNGE; | |
7835f163 | 2252 | default: |
fdada98f | 2253 | gcc_unreachable (); |
7835f163 | 2254 | } |
2255 | } | |
2256 | ||
2257 | /* Convert a compcode bit-based encoding of a comparison operator back | |
2258 | to GCC's enum tree_code representation. This function is the | |
2259 | inverse of comparison_to_compcode. */ | |
2260 | ||
2261 | static enum tree_code | |
318a728f | 2262 | compcode_to_comparison (enum comparison_code code) |
7835f163 | 2263 | { |
2264 | switch (code) | |
2265 | { | |
2266 | case COMPCODE_LT: | |
2267 | return LT_EXPR; | |
2268 | case COMPCODE_EQ: | |
2269 | return EQ_EXPR; | |
2270 | case COMPCODE_LE: | |
2271 | return LE_EXPR; | |
2272 | case COMPCODE_GT: | |
2273 | return GT_EXPR; | |
2274 | case COMPCODE_NE: | |
2275 | return NE_EXPR; | |
2276 | case COMPCODE_GE: | |
2277 | return GE_EXPR; | |
318a728f | 2278 | case COMPCODE_ORD: |
2279 | return ORDERED_EXPR; | |
2280 | case COMPCODE_UNORD: | |
2281 | return UNORDERED_EXPR; | |
2282 | case COMPCODE_UNLT: | |
2283 | return UNLT_EXPR; | |
2284 | case COMPCODE_UNEQ: | |
2285 | return UNEQ_EXPR; | |
2286 | case COMPCODE_UNLE: | |
2287 | return UNLE_EXPR; | |
2288 | case COMPCODE_UNGT: | |
2289 | return UNGT_EXPR; | |
2290 | case COMPCODE_LTGT: | |
2291 | return LTGT_EXPR; | |
2292 | case COMPCODE_UNGE: | |
2293 | return UNGE_EXPR; | |
7835f163 | 2294 | default: |
fdada98f | 2295 | gcc_unreachable (); |
7835f163 | 2296 | } |
2297 | } | |
2298 | ||
318a728f | 2299 | /* Return a tree for the comparison which is the combination of |
2300 | doing the AND or OR (depending on CODE) of the two operations LCODE | |
2301 | and RCODE on the identical operands LL_ARG and LR_ARG. Take into account | |
2302 | the possibility of trapping if the mode has NaNs, and return NULL_TREE | |
2303 | if this makes the transformation invalid. */ | |
2304 | ||
2305 | tree | |
389dd41b | 2306 | combine_comparisons (location_t loc, |
2307 | enum tree_code code, enum tree_code lcode, | |
318a728f | 2308 | enum tree_code rcode, tree truth_type, |
2309 | tree ll_arg, tree lr_arg) | |
2310 | { | |
2311 | bool honor_nans = HONOR_NANS (TYPE_MODE (TREE_TYPE (ll_arg))); | |
2312 | enum comparison_code lcompcode = comparison_to_compcode (lcode); | |
2313 | enum comparison_code rcompcode = comparison_to_compcode (rcode); | |
8458f4ca | 2314 | int compcode; |
318a728f | 2315 | |
2316 | switch (code) | |
2317 | { | |
2318 | case TRUTH_AND_EXPR: case TRUTH_ANDIF_EXPR: | |
2319 | compcode = lcompcode & rcompcode; | |
2320 | break; | |
2321 | ||
2322 | case TRUTH_OR_EXPR: case TRUTH_ORIF_EXPR: | |
2323 | compcode = lcompcode | rcompcode; | |
2324 | break; | |
2325 | ||
2326 | default: | |
2327 | return NULL_TREE; | |
2328 | } | |
2329 | ||
2330 | if (!honor_nans) | |
2331 | { | |
2332 | /* Eliminate unordered comparisons, as well as LTGT and ORD | |
2333 | which are not used unless the mode has NaNs. */ | |
2334 | compcode &= ~COMPCODE_UNORD; | |
2335 | if (compcode == COMPCODE_LTGT) | |
2336 | compcode = COMPCODE_NE; | |
2337 | else if (compcode == COMPCODE_ORD) | |
2338 | compcode = COMPCODE_TRUE; | |
2339 | } | |
2340 | else if (flag_trapping_math) | |
2341 | { | |
7206da1b | 2342 | /* Check that the original operation and the optimized ones will trap |
318a728f | 2343 | under the same condition. */ |
2344 | bool ltrap = (lcompcode & COMPCODE_UNORD) == 0 | |
2345 | && (lcompcode != COMPCODE_EQ) | |
2346 | && (lcompcode != COMPCODE_ORD); | |
2347 | bool rtrap = (rcompcode & COMPCODE_UNORD) == 0 | |
2348 | && (rcompcode != COMPCODE_EQ) | |
2349 | && (rcompcode != COMPCODE_ORD); | |
2350 | bool trap = (compcode & COMPCODE_UNORD) == 0 | |
2351 | && (compcode != COMPCODE_EQ) | |
2352 | && (compcode != COMPCODE_ORD); | |
2353 | ||
2354 | /* In a short-circuited boolean expression the LHS might be | |
2355 | such that the RHS, if evaluated, will never trap. For | |
2356 | example, in ORD (x, y) && (x < y), we evaluate the RHS only | |
2357 | if neither x nor y is NaN. (This is a mixed blessing: for | |
2358 | example, the expression above will never trap, hence | |
2359 | optimizing it to x < y would be invalid). */ | |
2360 | if ((code == TRUTH_ORIF_EXPR && (lcompcode & COMPCODE_UNORD)) | |
2361 | || (code == TRUTH_ANDIF_EXPR && !(lcompcode & COMPCODE_UNORD))) | |
2362 | rtrap = false; | |
2363 | ||
2364 | /* If the comparison was short-circuited, and only the RHS | |
2365 | trapped, we may now generate a spurious trap. */ | |
2366 | if (rtrap && !ltrap | |
2367 | && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR)) | |
2368 | return NULL_TREE; | |
2369 | ||
2370 | /* If we changed the conditions that cause a trap, we lose. */ | |
2371 | if ((ltrap || rtrap) != trap) | |
2372 | return NULL_TREE; | |
2373 | } | |
2374 | ||
2375 | if (compcode == COMPCODE_TRUE) | |
20783f07 | 2376 | return constant_boolean_node (true, truth_type); |
318a728f | 2377 | else if (compcode == COMPCODE_FALSE) |
20783f07 | 2378 | return constant_boolean_node (false, truth_type); |
318a728f | 2379 | else |
8458f4ca | 2380 | { |
2381 | enum tree_code tcode; | |
2382 | ||
2383 | tcode = compcode_to_comparison ((enum comparison_code) compcode); | |
389dd41b | 2384 | return fold_build2_loc (loc, tcode, truth_type, ll_arg, lr_arg); |
8458f4ca | 2385 | } |
318a728f | 2386 | } |
e233264a | 2387 | \f |
9e6f4cc9 | 2388 | /* Return nonzero if two operands (typically of the same tree node) |
2389 | are necessarily equal. If either argument has side-effects this | |
365db11e | 2390 | function returns zero. FLAGS modifies behavior as follows: |
9e6f4cc9 | 2391 | |
4ee9c684 | 2392 | If OEP_ONLY_CONST is set, only return nonzero for constants. |
11acc1df | 2393 | This function tests whether the operands are indistinguishable; |
2394 | it does not test whether they are equal using C's == operation. | |
2395 | The distinction is important for IEEE floating point, because | |
2396 | (1) -0.0 and 0.0 are distinguishable, but -0.0==0.0, and | |
9e6f4cc9 | 2397 | (2) two NaNs may be indistinguishable, but NaN!=NaN. |
2398 | ||
4ee9c684 | 2399 | If OEP_ONLY_CONST is unset, a VAR_DECL is considered equal to itself |
9e6f4cc9 | 2400 | even though it may hold multiple values during a function. |
2401 | This is because a GCC tree node guarantees that nothing else is | |
2402 | executed between the evaluation of its "operands" (which may often | |
2403 | be evaluated in arbitrary order). Hence if the operands themselves | |
2404 | don't side-effect, the VAR_DECLs, PARM_DECLs etc... must hold the | |
9b931277 | 2405 | same value in each operand/subexpression. Hence leaving OEP_ONLY_CONST |
2406 | unset means assuming isochronic (or instantaneous) tree equivalence. | |
2407 | Unless comparing arbitrary expression trees, such as from different | |
2408 | statements, this flag can usually be left unset. | |
4ee9c684 | 2409 | |
2410 | If OEP_PURE_SAME is set, then pure functions with identical arguments | |
2411 | are considered the same. It is used when the caller has other ways | |
2412 | to ensure that global memory is unchanged in between. */ | |
2bc77e10 | 2413 | |
2414 | int | |
b7bf20db | 2415 | operand_equal_p (const_tree arg0, const_tree arg1, unsigned int flags) |
2bc77e10 | 2416 | { |
78a8ed03 | 2417 | /* If either is ERROR_MARK, they aren't equal. */ |
767a8a1c | 2418 | if (TREE_CODE (arg0) == ERROR_MARK || TREE_CODE (arg1) == ERROR_MARK |
2419 | || TREE_TYPE (arg0) == error_mark_node | |
2420 | || TREE_TYPE (arg1) == error_mark_node) | |
78a8ed03 | 2421 | return 0; |
2422 | ||
6d72287b | 2423 | /* Similar, if either does not have a type (like a released SSA name), |
2424 | they aren't equal. */ | |
2425 | if (!TREE_TYPE (arg0) || !TREE_TYPE (arg1)) | |
2426 | return 0; | |
2427 | ||
659ce413 | 2428 | /* Check equality of integer constants before bailing out due to |
2429 | precision differences. */ | |
2430 | if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST) | |
2431 | return tree_int_cst_equal (arg0, arg1); | |
2432 | ||
2bc77e10 | 2433 | /* If both types don't have the same signedness, then we can't consider |
2434 | them equal. We must check this before the STRIP_NOPS calls | |
07018da0 | 2435 | because they may change the signedness of the arguments. As pointers |
2436 | strictly don't have a signedness, require either two pointers or | |
2437 | two non-pointers as well. */ | |
2438 | if (TYPE_UNSIGNED (TREE_TYPE (arg0)) != TYPE_UNSIGNED (TREE_TYPE (arg1)) | |
2439 | || POINTER_TYPE_P (TREE_TYPE (arg0)) != POINTER_TYPE_P (TREE_TYPE (arg1))) | |
2bc77e10 | 2440 | return 0; |
2441 | ||
bd1a81f7 | 2442 | /* We cannot consider pointers to different address space equal. */ |
2443 | if (POINTER_TYPE_P (TREE_TYPE (arg0)) && POINTER_TYPE_P (TREE_TYPE (arg1)) | |
2444 | && (TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (arg0))) | |
2445 | != TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (arg1))))) | |
2446 | return 0; | |
2447 | ||
68826b15 | 2448 | /* If both types don't have the same precision, then it is not safe |
2449 | to strip NOPs. */ | |
2450 | if (TYPE_PRECISION (TREE_TYPE (arg0)) != TYPE_PRECISION (TREE_TYPE (arg1))) | |
2451 | return 0; | |
2452 | ||
2bc77e10 | 2453 | STRIP_NOPS (arg0); |
2454 | STRIP_NOPS (arg1); | |
2455 | ||
ffb99bfe | 2456 | /* In case both args are comparisons but with different comparison |
2457 | code, try to swap the comparison operands of one arg to produce | |
2458 | a match and compare that variant. */ | |
2459 | if (TREE_CODE (arg0) != TREE_CODE (arg1) | |
2460 | && COMPARISON_CLASS_P (arg0) | |
2461 | && COMPARISON_CLASS_P (arg1)) | |
2462 | { | |
2463 | enum tree_code swap_code = swap_tree_comparison (TREE_CODE (arg1)); | |
2464 | ||
2465 | if (TREE_CODE (arg0) == swap_code) | |
2466 | return operand_equal_p (TREE_OPERAND (arg0, 0), | |
2467 | TREE_OPERAND (arg1, 1), flags) | |
2468 | && operand_equal_p (TREE_OPERAND (arg0, 1), | |
2469 | TREE_OPERAND (arg1, 0), flags); | |
2470 | } | |
2471 | ||
8faaadf1 | 2472 | if (TREE_CODE (arg0) != TREE_CODE (arg1) |
2473 | /* This is needed for conversions and for COMPONENT_REF. | |
2474 | Might as well play it safe and always test this. */ | |
6a4737bf | 2475 | || TREE_CODE (TREE_TYPE (arg0)) == ERROR_MARK |
2476 | || TREE_CODE (TREE_TYPE (arg1)) == ERROR_MARK | |
8faaadf1 | 2477 | || TYPE_MODE (TREE_TYPE (arg0)) != TYPE_MODE (TREE_TYPE (arg1))) |
2bc77e10 | 2478 | return 0; |
2479 | ||
8faaadf1 | 2480 | /* If ARG0 and ARG1 are the same SAVE_EXPR, they are necessarily equal. |
2481 | We don't care about side effects in that case because the SAVE_EXPR | |
2482 | takes care of that for us. In all other cases, two expressions are | |
2483 | equal if they have no side effects. If we have two identical | |
2484 | expressions with side effects that should be treated the same due | |
2485 | to the only side effects being identical SAVE_EXPR's, that will | |
2486 | be detected in the recursive calls below. */ | |
4ee9c684 | 2487 | if (arg0 == arg1 && ! (flags & OEP_ONLY_CONST) |
8faaadf1 | 2488 | && (TREE_CODE (arg0) == SAVE_EXPR |
2489 | || (! TREE_SIDE_EFFECTS (arg0) && ! TREE_SIDE_EFFECTS (arg1)))) | |
2bc77e10 | 2490 | return 1; |
2491 | ||
8faaadf1 | 2492 | /* Next handle constant cases, those for which we can return 1 even |
2493 | if ONLY_CONST is set. */ | |
2494 | if (TREE_CONSTANT (arg0) && TREE_CONSTANT (arg1)) | |
2495 | switch (TREE_CODE (arg0)) | |
2496 | { | |
2497 | case INTEGER_CST: | |
bdb135dc | 2498 | return tree_int_cst_equal (arg0, arg1); |
8faaadf1 | 2499 | |
06f0b99c | 2500 | case FIXED_CST: |
2501 | return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (arg0), | |
2502 | TREE_FIXED_CST (arg1)); | |
2503 | ||
8faaadf1 | 2504 | case REAL_CST: |
90b56f40 | 2505 | if (REAL_VALUES_IDENTICAL (TREE_REAL_CST (arg0), |
2506 | TREE_REAL_CST (arg1))) | |
2507 | return 1; | |
2508 | ||
48e1416a | 2509 | |
90b56f40 | 2510 | if (!HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg0)))) |
2511 | { | |
2512 | /* If we do not distinguish between signed and unsigned zero, | |
2513 | consider them equal. */ | |
2514 | if (real_zerop (arg0) && real_zerop (arg1)) | |
2515 | return 1; | |
2516 | } | |
2517 | return 0; | |
8faaadf1 | 2518 | |
886cfd4f | 2519 | case VECTOR_CST: |
2520 | { | |
2521 | tree v1, v2; | |
2522 | ||
886cfd4f | 2523 | v1 = TREE_VECTOR_CST_ELTS (arg0); |
2524 | v2 = TREE_VECTOR_CST_ELTS (arg1); | |
2525 | while (v1 && v2) | |
2526 | { | |
11cb6006 | 2527 | if (!operand_equal_p (TREE_VALUE (v1), TREE_VALUE (v2), |
4ee9c684 | 2528 | flags)) |
886cfd4f | 2529 | return 0; |
2530 | v1 = TREE_CHAIN (v1); | |
2531 | v2 = TREE_CHAIN (v2); | |
2532 | } | |
2533 | ||
6349b545 | 2534 | return v1 == v2; |
886cfd4f | 2535 | } |
2536 | ||
8faaadf1 | 2537 | case COMPLEX_CST: |
2538 | return (operand_equal_p (TREE_REALPART (arg0), TREE_REALPART (arg1), | |
4ee9c684 | 2539 | flags) |
8faaadf1 | 2540 | && operand_equal_p (TREE_IMAGPART (arg0), TREE_IMAGPART (arg1), |
4ee9c684 | 2541 | flags)); |
8faaadf1 | 2542 | |
2543 | case STRING_CST: | |
2544 | return (TREE_STRING_LENGTH (arg0) == TREE_STRING_LENGTH (arg1) | |
6b918462 | 2545 | && ! memcmp (TREE_STRING_POINTER (arg0), |
8faaadf1 | 2546 | TREE_STRING_POINTER (arg1), |
2547 | TREE_STRING_LENGTH (arg0))); | |
2548 | ||
2549 | case ADDR_EXPR: | |
2550 | return operand_equal_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg1, 0), | |
2551 | 0); | |
0dbd1c74 | 2552 | default: |
2553 | break; | |
8faaadf1 | 2554 | } |
2bc77e10 | 2555 | |
4ee9c684 | 2556 | if (flags & OEP_ONLY_CONST) |
2bc77e10 | 2557 | return 0; |
2558 | ||
66bab57a | 2559 | /* Define macros to test an operand from arg0 and arg1 for equality and a |
fa31fec1 | 2560 | variant that allows null and views null as being different from any |
2561 | non-null value. In the latter case, if either is null, the both | |
2562 | must be; otherwise, do the normal comparison. */ | |
2563 | #define OP_SAME(N) operand_equal_p (TREE_OPERAND (arg0, N), \ | |
2564 | TREE_OPERAND (arg1, N), flags) | |
2565 | ||
2566 | #define OP_SAME_WITH_NULL(N) \ | |
2567 | ((!TREE_OPERAND (arg0, N) || !TREE_OPERAND (arg1, N)) \ | |
2568 | ? TREE_OPERAND (arg0, N) == TREE_OPERAND (arg1, N) : OP_SAME (N)) | |
2569 | ||
2bc77e10 | 2570 | switch (TREE_CODE_CLASS (TREE_CODE (arg0))) |
2571 | { | |
ce45a448 | 2572 | case tcc_unary: |
2bc77e10 | 2573 | /* Two conversions are equal only if signedness and modes match. */ |
e6546627 | 2574 | switch (TREE_CODE (arg0)) |
2575 | { | |
72dd6141 | 2576 | CASE_CONVERT: |
e6546627 | 2577 | case FIX_TRUNC_EXPR: |
e6546627 | 2578 | if (TYPE_UNSIGNED (TREE_TYPE (arg0)) |
2579 | != TYPE_UNSIGNED (TREE_TYPE (arg1))) | |
2580 | return 0; | |
2581 | break; | |
2582 | default: | |
2583 | break; | |
2584 | } | |
2bc77e10 | 2585 | |
fa31fec1 | 2586 | return OP_SAME (0); |
2587 | ||
2bc77e10 | 2588 | |
ce45a448 | 2589 | case tcc_comparison: |
2590 | case tcc_binary: | |
fa31fec1 | 2591 | if (OP_SAME (0) && OP_SAME (1)) |
8faaadf1 | 2592 | return 1; |
2593 | ||
2594 | /* For commutative ops, allow the other order. */ | |
21dff555 | 2595 | return (commutative_tree_code (TREE_CODE (arg0)) |
8faaadf1 | 2596 | && operand_equal_p (TREE_OPERAND (arg0, 0), |
4ee9c684 | 2597 | TREE_OPERAND (arg1, 1), flags) |
2bc77e10 | 2598 | && operand_equal_p (TREE_OPERAND (arg0, 1), |
4ee9c684 | 2599 | TREE_OPERAND (arg1, 0), flags)); |
2bc77e10 | 2600 | |
ce45a448 | 2601 | case tcc_reference: |
06506f5d | 2602 | /* If either of the pointer (or reference) expressions we are |
2603 | dereferencing contain a side effect, these cannot be equal. */ | |
dbc71562 | 2604 | if (TREE_SIDE_EFFECTS (arg0) |
2605 | || TREE_SIDE_EFFECTS (arg1)) | |
2606 | return 0; | |
2607 | ||
2bc77e10 | 2608 | switch (TREE_CODE (arg0)) |
2609 | { | |
2610 | case INDIRECT_REF: | |
b056d812 | 2611 | case ALIGN_INDIRECT_REF: |
2612 | case MISALIGNED_INDIRECT_REF: | |
b25de375 | 2613 | case REALPART_EXPR: |
2614 | case IMAGPART_EXPR: | |
fa31fec1 | 2615 | return OP_SAME (0); |
2bc77e10 | 2616 | |
2bc77e10 | 2617 | case ARRAY_REF: |
ba04d9d5 | 2618 | case ARRAY_RANGE_REF: |
a2501610 | 2619 | /* Operands 2 and 3 may be null. |
2620 | Compare the array index by value if it is constant first as we | |
2621 | may have different types but same value here. */ | |
fa31fec1 | 2622 | return (OP_SAME (0) |
a2501610 | 2623 | && (tree_int_cst_equal (TREE_OPERAND (arg0, 1), |
2624 | TREE_OPERAND (arg1, 1)) | |
2625 | || OP_SAME (1)) | |
fa31fec1 | 2626 | && OP_SAME_WITH_NULL (2) |
2627 | && OP_SAME_WITH_NULL (3)); | |
6ab43650 | 2628 | |
2629 | case COMPONENT_REF: | |
2f16183e | 2630 | /* Handle operand 2 the same as for ARRAY_REF. Operand 0 |
2631 | may be NULL when we're called to compare MEM_EXPRs. */ | |
2632 | return OP_SAME_WITH_NULL (0) | |
2633 | && OP_SAME (1) | |
2634 | && OP_SAME_WITH_NULL (2); | |
8d061c60 | 2635 | |
e715d92e | 2636 | case BIT_FIELD_REF: |
fa31fec1 | 2637 | return OP_SAME (0) && OP_SAME (1) && OP_SAME (2); |
2638 | ||
0dbd1c74 | 2639 | default: |
2640 | return 0; | |
2bc77e10 | 2641 | } |
1d322a97 | 2642 | |
ce45a448 | 2643 | case tcc_expression: |
564989a5 | 2644 | switch (TREE_CODE (arg0)) |
2645 | { | |
2646 | case ADDR_EXPR: | |
2647 | case TRUTH_NOT_EXPR: | |
fa31fec1 | 2648 | return OP_SAME (0); |
564989a5 | 2649 | |
bd975dc2 | 2650 | case TRUTH_ANDIF_EXPR: |
2651 | case TRUTH_ORIF_EXPR: | |
fa31fec1 | 2652 | return OP_SAME (0) && OP_SAME (1); |
bd975dc2 | 2653 | |
2654 | case TRUTH_AND_EXPR: | |
2655 | case TRUTH_OR_EXPR: | |
2656 | case TRUTH_XOR_EXPR: | |
fa31fec1 | 2657 | if (OP_SAME (0) && OP_SAME (1)) |
2658 | return 1; | |
2659 | ||
2660 | /* Otherwise take into account this is a commutative operation. */ | |
bd975dc2 | 2661 | return (operand_equal_p (TREE_OPERAND (arg0, 0), |
fa31fec1 | 2662 | TREE_OPERAND (arg1, 1), flags) |
bd975dc2 | 2663 | && operand_equal_p (TREE_OPERAND (arg0, 1), |
fa31fec1 | 2664 | TREE_OPERAND (arg1, 0), flags)); |
bd975dc2 | 2665 | |
c319d56a | 2666 | case COND_EXPR: |
2667 | return OP_SAME (0) && OP_SAME (1) && OP_SAME (2); | |
48e1416a | 2668 | |
c2f47e15 | 2669 | default: |
2670 | return 0; | |
2671 | } | |
2672 | ||
2673 | case tcc_vl_exp: | |
2674 | switch (TREE_CODE (arg0)) | |
2675 | { | |
06506f5d | 2676 | case CALL_EXPR: |
2677 | /* If the CALL_EXPRs call different functions, then they | |
2678 | clearly can not be equal. */ | |
c2f47e15 | 2679 | if (! operand_equal_p (CALL_EXPR_FN (arg0), CALL_EXPR_FN (arg1), |
2680 | flags)) | |
06506f5d | 2681 | return 0; |
2682 | ||
4ee9c684 | 2683 | { |
2684 | unsigned int cef = call_expr_flags (arg0); | |
2685 | if (flags & OEP_PURE_SAME) | |
2686 | cef &= ECF_CONST | ECF_PURE; | |
2687 | else | |
2688 | cef &= ECF_CONST; | |
2689 | if (!cef) | |
2690 | return 0; | |
2691 | } | |
06506f5d | 2692 | |
c2f47e15 | 2693 | /* Now see if all the arguments are the same. */ |
2694 | { | |
b7bf20db | 2695 | const_call_expr_arg_iterator iter0, iter1; |
2696 | const_tree a0, a1; | |
2697 | for (a0 = first_const_call_expr_arg (arg0, &iter0), | |
2698 | a1 = first_const_call_expr_arg (arg1, &iter1); | |
c2f47e15 | 2699 | a0 && a1; |
b7bf20db | 2700 | a0 = next_const_call_expr_arg (&iter0), |
2701 | a1 = next_const_call_expr_arg (&iter1)) | |
c2f47e15 | 2702 | if (! operand_equal_p (a0, a1, flags)) |
06506f5d | 2703 | return 0; |
2704 | ||
c2f47e15 | 2705 | /* If we get here and both argument lists are exhausted |
2706 | then the CALL_EXPRs are equal. */ | |
2707 | return ! (a0 || a1); | |
2708 | } | |
564989a5 | 2709 | default: |
2710 | return 0; | |
2711 | } | |
cc049fa3 | 2712 | |
ce45a448 | 2713 | case tcc_declaration: |
4ee9c684 | 2714 | /* Consider __builtin_sqrt equal to sqrt. */ |
2715 | return (TREE_CODE (arg0) == FUNCTION_DECL | |
2716 | && DECL_BUILT_IN (arg0) && DECL_BUILT_IN (arg1) | |
2717 | && DECL_BUILT_IN_CLASS (arg0) == DECL_BUILT_IN_CLASS (arg1) | |
2718 | && DECL_FUNCTION_CODE (arg0) == DECL_FUNCTION_CODE (arg1)); | |
06506f5d | 2719 | |
0dbd1c74 | 2720 | default: |
2721 | return 0; | |
2bc77e10 | 2722 | } |
fa31fec1 | 2723 | |
2724 | #undef OP_SAME | |
2725 | #undef OP_SAME_WITH_NULL | |
2bc77e10 | 2726 | } |
e233264a | 2727 | \f |
2728 | /* Similar to operand_equal_p, but see if ARG0 might have been made by | |
cc049fa3 | 2729 | shorten_compare from ARG1 when ARG1 was being compared with OTHER. |
2bc77e10 | 2730 | |
2bc77e10 | 2731 | When in doubt, return 0. */ |
2732 | ||
cc049fa3 | 2733 | static int |
de1b648b | 2734 | operand_equal_for_comparison_p (tree arg0, tree arg1, tree other) |
2bc77e10 | 2735 | { |
e233264a | 2736 | int unsignedp1, unsignedpo; |
df7caa7b | 2737 | tree primarg0, primarg1, primother; |
02e7a332 | 2738 | unsigned int correct_width; |
2bc77e10 | 2739 | |
e233264a | 2740 | if (operand_equal_p (arg0, arg1, 0)) |
2bc77e10 | 2741 | return 1; |
2742 | ||
154e6f12 | 2743 | if (! INTEGRAL_TYPE_P (TREE_TYPE (arg0)) |
2744 | || ! INTEGRAL_TYPE_P (TREE_TYPE (arg1))) | |
2bc77e10 | 2745 | return 0; |
2746 | ||
df7caa7b | 2747 | /* Discard any conversions that don't change the modes of ARG0 and ARG1 |
2748 | and see if the inner values are the same. This removes any | |
2749 | signedness comparison, which doesn't matter here. */ | |
2750 | primarg0 = arg0, primarg1 = arg1; | |
cc049fa3 | 2751 | STRIP_NOPS (primarg0); |
2752 | STRIP_NOPS (primarg1); | |
df7caa7b | 2753 | if (operand_equal_p (primarg0, primarg1, 0)) |
2754 | return 1; | |
2755 | ||
e233264a | 2756 | /* Duplicate what shorten_compare does to ARG1 and see if that gives the |
2757 | actual comparison operand, ARG0. | |
2bc77e10 | 2758 | |
e233264a | 2759 | First throw away any conversions to wider types |
2bc77e10 | 2760 | already present in the operands. */ |
2bc77e10 | 2761 | |
e233264a | 2762 | primarg1 = get_narrower (arg1, &unsignedp1); |
2763 | primother = get_narrower (other, &unsignedpo); | |
2764 | ||
2765 | correct_width = TYPE_PRECISION (TREE_TYPE (arg1)); | |
2766 | if (unsignedp1 == unsignedpo | |
2767 | && TYPE_PRECISION (TREE_TYPE (primarg1)) < correct_width | |
2768 | && TYPE_PRECISION (TREE_TYPE (primother)) < correct_width) | |
2bc77e10 | 2769 | { |
e233264a | 2770 | tree type = TREE_TYPE (arg0); |
2bc77e10 | 2771 | |
2772 | /* Make sure shorter operand is extended the right way | |
2773 | to match the longer operand. */ | |
11773141 | 2774 | primarg1 = fold_convert (signed_or_unsigned_type_for |
b30e3dbc | 2775 | (unsignedp1, TREE_TYPE (primarg1)), primarg1); |
2bc77e10 | 2776 | |
b30e3dbc | 2777 | if (operand_equal_p (arg0, fold_convert (type, primarg1), 0)) |
2bc77e10 | 2778 | return 1; |
2779 | } | |
2780 | ||
2781 | return 0; | |
2782 | } | |
2783 | \f | |
eb2f80f3 | 2784 | /* See if ARG is an expression that is either a comparison or is performing |
e233264a | 2785 | arithmetic on comparisons. The comparisons must only be comparing |
2786 | two different values, which will be stored in *CVAL1 and *CVAL2; if | |
6ef828f9 | 2787 | they are nonzero it means that some operands have already been found. |
e233264a | 2788 | No variables may be used anywhere else in the expression except in the |
d0314131 | 2789 | comparisons. If SAVE_P is true it means we removed a SAVE_EXPR around |
2790 | the expression and save_expr needs to be called with CVAL1 and CVAL2. | |
e233264a | 2791 | |
2792 | If this is true, return 1. Otherwise, return zero. */ | |
2793 | ||
2794 | static int | |
de1b648b | 2795 | twoval_comparison_p (tree arg, tree *cval1, tree *cval2, int *save_p) |
e233264a | 2796 | { |
2797 | enum tree_code code = TREE_CODE (arg); | |
f4e36c33 | 2798 | enum tree_code_class tclass = TREE_CODE_CLASS (code); |
e233264a | 2799 | |
ce45a448 | 2800 | /* We can handle some of the tcc_expression cases here. */ |
f4e36c33 | 2801 | if (tclass == tcc_expression && code == TRUTH_NOT_EXPR) |
2802 | tclass = tcc_unary; | |
2803 | else if (tclass == tcc_expression | |
e233264a | 2804 | && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR |
2805 | || code == COMPOUND_EXPR)) | |
f4e36c33 | 2806 | tclass = tcc_binary; |
8be91fe5 | 2807 | |
f4e36c33 | 2808 | else if (tclass == tcc_expression && code == SAVE_EXPR |
083a2b5e | 2809 | && ! TREE_SIDE_EFFECTS (TREE_OPERAND (arg, 0))) |
d0314131 | 2810 | { |
2811 | /* If we've already found a CVAL1 or CVAL2, this expression is | |
2812 | two complex to handle. */ | |
2813 | if (*cval1 || *cval2) | |
2814 | return 0; | |
2815 | ||
f4e36c33 | 2816 | tclass = tcc_unary; |
d0314131 | 2817 | *save_p = 1; |
2818 | } | |
e233264a | 2819 | |
f4e36c33 | 2820 | switch (tclass) |
e233264a | 2821 | { |
ce45a448 | 2822 | case tcc_unary: |
d0314131 | 2823 | return twoval_comparison_p (TREE_OPERAND (arg, 0), cval1, cval2, save_p); |
e233264a | 2824 | |
ce45a448 | 2825 | case tcc_binary: |
d0314131 | 2826 | return (twoval_comparison_p (TREE_OPERAND (arg, 0), cval1, cval2, save_p) |
2827 | && twoval_comparison_p (TREE_OPERAND (arg, 1), | |
2828 | cval1, cval2, save_p)); | |
e233264a | 2829 | |
ce45a448 | 2830 | case tcc_constant: |
e233264a | 2831 | return 1; |
2832 | ||
ce45a448 | 2833 | case tcc_expression: |
e233264a | 2834 | if (code == COND_EXPR) |
d0314131 | 2835 | return (twoval_comparison_p (TREE_OPERAND (arg, 0), |
2836 | cval1, cval2, save_p) | |
2837 | && twoval_comparison_p (TREE_OPERAND (arg, 1), | |
2838 | cval1, cval2, save_p) | |
e233264a | 2839 | && twoval_comparison_p (TREE_OPERAND (arg, 2), |
d0314131 | 2840 | cval1, cval2, save_p)); |
e233264a | 2841 | return 0; |
cc049fa3 | 2842 | |
ce45a448 | 2843 | case tcc_comparison: |
e233264a | 2844 | /* First see if we can handle the first operand, then the second. For |
2845 | the second operand, we know *CVAL1 can't be zero. It must be that | |
2846 | one side of the comparison is each of the values; test for the | |
2847 | case where this isn't true by failing if the two operands | |
2848 | are the same. */ | |
2849 | ||
2850 | if (operand_equal_p (TREE_OPERAND (arg, 0), | |
2851 | TREE_OPERAND (arg, 1), 0)) | |
2852 | return 0; | |
2853 | ||
2854 | if (*cval1 == 0) | |
2855 | *cval1 = TREE_OPERAND (arg, 0); | |
2856 | else if (operand_equal_p (*cval1, TREE_OPERAND (arg, 0), 0)) | |
2857 | ; | |
2858 | else if (*cval2 == 0) | |
2859 | *cval2 = TREE_OPERAND (arg, 0); | |
2860 | else if (operand_equal_p (*cval2, TREE_OPERAND (arg, 0), 0)) | |
2861 | ; | |
2862 | else | |
2863 | return 0; | |
2864 | ||
2865 | if (operand_equal_p (*cval1, TREE_OPERAND (arg, 1), 0)) | |
2866 | ; | |
2867 | else if (*cval2 == 0) | |
2868 | *cval2 = TREE_OPERAND (arg, 1); | |
2869 | else if (operand_equal_p (*cval2, TREE_OPERAND (arg, 1), 0)) | |
2870 | ; | |
2871 | else | |
2872 | return 0; | |
2873 | ||
2874 | return 1; | |
e233264a | 2875 | |
0dbd1c74 | 2876 | default: |
2877 | return 0; | |
2878 | } | |
e233264a | 2879 | } |
2880 | \f | |
2881 | /* ARG is a tree that is known to contain just arithmetic operations and | |
2882 | comparisons. Evaluate the operations in the tree substituting NEW0 for | |
eb2f80f3 | 2883 | any occurrence of OLD0 as an operand of a comparison and likewise for |
e233264a | 2884 | NEW1 and OLD1. */ |
2885 | ||
2886 | static tree | |
389dd41b | 2887 | eval_subst (location_t loc, tree arg, tree old0, tree new0, |
2888 | tree old1, tree new1) | |
e233264a | 2889 | { |
2890 | tree type = TREE_TYPE (arg); | |
2891 | enum tree_code code = TREE_CODE (arg); | |
f4e36c33 | 2892 | enum tree_code_class tclass = TREE_CODE_CLASS (code); |
e233264a | 2893 | |
ce45a448 | 2894 | /* We can handle some of the tcc_expression cases here. */ |
f4e36c33 | 2895 | if (tclass == tcc_expression && code == TRUTH_NOT_EXPR) |
2896 | tclass = tcc_unary; | |
2897 | else if (tclass == tcc_expression | |
e233264a | 2898 | && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR)) |
f4e36c33 | 2899 | tclass = tcc_binary; |
e233264a | 2900 | |
f4e36c33 | 2901 | switch (tclass) |
e233264a | 2902 | { |
ce45a448 | 2903 | case tcc_unary: |
389dd41b | 2904 | return fold_build1_loc (loc, code, type, |
2905 | eval_subst (loc, TREE_OPERAND (arg, 0), | |
7ab7fd4f | 2906 | old0, new0, old1, new1)); |
e233264a | 2907 | |
ce45a448 | 2908 | case tcc_binary: |
389dd41b | 2909 | return fold_build2_loc (loc, code, type, |
2910 | eval_subst (loc, TREE_OPERAND (arg, 0), | |
7ab7fd4f | 2911 | old0, new0, old1, new1), |
389dd41b | 2912 | eval_subst (loc, TREE_OPERAND (arg, 1), |
7ab7fd4f | 2913 | old0, new0, old1, new1)); |
e233264a | 2914 | |
ce45a448 | 2915 | case tcc_expression: |
e233264a | 2916 | switch (code) |
2917 | { | |
2918 | case SAVE_EXPR: | |
389dd41b | 2919 | return eval_subst (loc, TREE_OPERAND (arg, 0), old0, new0, |
2920 | old1, new1); | |
e233264a | 2921 | |
2922 | case COMPOUND_EXPR: | |
389dd41b | 2923 | return eval_subst (loc, TREE_OPERAND (arg, 1), old0, new0, |
2924 | old1, new1); | |
e233264a | 2925 | |
2926 | case COND_EXPR: | |
389dd41b | 2927 | return fold_build3_loc (loc, code, type, |
2928 | eval_subst (loc, TREE_OPERAND (arg, 0), | |
7ab7fd4f | 2929 | old0, new0, old1, new1), |
389dd41b | 2930 | eval_subst (loc, TREE_OPERAND (arg, 1), |
7ab7fd4f | 2931 | old0, new0, old1, new1), |
389dd41b | 2932 | eval_subst (loc, TREE_OPERAND (arg, 2), |
7ab7fd4f | 2933 | old0, new0, old1, new1)); |
0dbd1c74 | 2934 | default: |
2935 | break; | |
e233264a | 2936 | } |
b4b174c3 | 2937 | /* Fall through - ??? */ |
e233264a | 2938 | |
ce45a448 | 2939 | case tcc_comparison: |
e233264a | 2940 | { |
2941 | tree arg0 = TREE_OPERAND (arg, 0); | |
2942 | tree arg1 = TREE_OPERAND (arg, 1); | |
2943 | ||
2944 | /* We need to check both for exact equality and tree equality. The | |
2945 | former will be true if the operand has a side-effect. In that | |
2946 | case, we know the operand occurred exactly once. */ | |
2947 | ||
2948 | if (arg0 == old0 || operand_equal_p (arg0, old0, 0)) | |
2949 | arg0 = new0; | |
2950 | else if (arg0 == old1 || operand_equal_p (arg0, old1, 0)) | |
2951 | arg0 = new1; | |
2952 | ||
2953 | if (arg1 == old0 || operand_equal_p (arg1, old0, 0)) | |
2954 | arg1 = new0; | |
2955 | else if (arg1 == old1 || operand_equal_p (arg1, old1, 0)) | |
2956 | arg1 = new1; | |
2957 | ||
389dd41b | 2958 | return fold_build2_loc (loc, code, type, arg0, arg1); |
e233264a | 2959 | } |
e233264a | 2960 | |
0dbd1c74 | 2961 | default: |
2962 | return arg; | |
2963 | } | |
e233264a | 2964 | } |
2965 | \f | |
2bc77e10 | 2966 | /* Return a tree for the case when the result of an expression is RESULT |
2967 | converted to TYPE and OMITTED was previously an operand of the expression | |
2968 | but is now not needed (e.g., we folded OMITTED * 0). | |
2969 | ||
2970 | If OMITTED has side effects, we must evaluate it. Otherwise, just do | |
2971 | the conversion of RESULT to TYPE. */ | |
2972 | ||
e9f80ff5 | 2973 | tree |
389dd41b | 2974 | omit_one_operand_loc (location_t loc, tree type, tree result, tree omitted) |
2bc77e10 | 2975 | { |
389dd41b | 2976 | tree t = fold_convert_loc (loc, type, result); |
2bc77e10 | 2977 | |
becfaa62 | 2978 | /* If the resulting operand is an empty statement, just return the omitted |
9e0e518b | 2979 | statement casted to void. */ |
2980 | if (IS_EMPTY_STMT (t) && TREE_SIDE_EFFECTS (omitted)) | |
389dd41b | 2981 | { |
2982 | t = build1 (NOP_EXPR, void_type_node, fold_ignored_result (omitted)); | |
2983 | goto omit_one_operand_exit; | |
2984 | } | |
9e0e518b | 2985 | |
2bc77e10 | 2986 | if (TREE_SIDE_EFFECTS (omitted)) |
389dd41b | 2987 | { |
2988 | t = build2 (COMPOUND_EXPR, type, fold_ignored_result (omitted), t); | |
2989 | goto omit_one_operand_exit; | |
2990 | } | |
2991 | ||
2992 | return non_lvalue_loc (loc, t); | |
2bc77e10 | 2993 | |
389dd41b | 2994 | omit_one_operand_exit: |
2995 | protected_set_expr_location (t, loc); | |
2996 | return t; | |
2bc77e10 | 2997 | } |
6df5edfa | 2998 | |
2999 | /* Similar, but call pedantic_non_lvalue instead of non_lvalue. */ | |
3000 | ||
3001 | static tree | |
389dd41b | 3002 | pedantic_omit_one_operand_loc (location_t loc, tree type, tree result, |
3003 | tree omitted) | |
6df5edfa | 3004 | { |
389dd41b | 3005 | tree t = fold_convert_loc (loc, type, result); |
6df5edfa | 3006 | |
becfaa62 | 3007 | /* If the resulting operand is an empty statement, just return the omitted |
9e0e518b | 3008 | statement casted to void. */ |
3009 | if (IS_EMPTY_STMT (t) && TREE_SIDE_EFFECTS (omitted)) | |
389dd41b | 3010 | { |
3011 | t = build1 (NOP_EXPR, void_type_node, fold_ignored_result (omitted)); | |
3012 | goto pedantic_omit_one_operand_exit; | |
3013 | } | |
9e0e518b | 3014 | |
6df5edfa | 3015 | if (TREE_SIDE_EFFECTS (omitted)) |
389dd41b | 3016 | { |
3017 | t = build2 (COMPOUND_EXPR, type, fold_ignored_result (omitted), t); | |
3018 | goto pedantic_omit_one_operand_exit; | |
3019 | } | |
6df5edfa | 3020 | |
389dd41b | 3021 | return pedantic_non_lvalue_loc (loc, t); |
3022 | ||
3023 | pedantic_omit_one_operand_exit: | |
3024 | protected_set_expr_location (t, loc); | |
3025 | return t; | |
6df5edfa | 3026 | } |
9bc9f15f | 3027 | |
3028 | /* Return a tree for the case when the result of an expression is RESULT | |
3029 | converted to TYPE and OMITTED1 and OMITTED2 were previously operands | |
3030 | of the expression but are now not needed. | |
3031 | ||
3032 | If OMITTED1 or OMITTED2 has side effects, they must be evaluated. | |
3033 | If both OMITTED1 and OMITTED2 have side effects, OMITTED1 is | |
3034 | evaluated before OMITTED2. Otherwise, if neither has side effects, | |
3035 | just do the conversion of RESULT to TYPE. */ | |
3036 | ||
3037 | tree | |
389dd41b | 3038 | omit_two_operands_loc (location_t loc, tree type, tree result, |
3039 | tree omitted1, tree omitted2) | |
9bc9f15f | 3040 | { |
389dd41b | 3041 | tree t = fold_convert_loc (loc, type, result); |
9bc9f15f | 3042 | |
3043 | if (TREE_SIDE_EFFECTS (omitted2)) | |
389dd41b | 3044 | { |
3045 | t = build2 (COMPOUND_EXPR, type, omitted2, t); | |
3046 | SET_EXPR_LOCATION (t, loc); | |
3047 | } | |
9bc9f15f | 3048 | if (TREE_SIDE_EFFECTS (omitted1)) |
389dd41b | 3049 | { |
3050 | t = build2 (COMPOUND_EXPR, type, omitted1, t); | |
3051 | SET_EXPR_LOCATION (t, loc); | |
3052 | } | |
9bc9f15f | 3053 | |
389dd41b | 3054 | return TREE_CODE (t) != COMPOUND_EXPR ? non_lvalue_loc (loc, t) : t; |
9bc9f15f | 3055 | } |
3056 | ||
2bc77e10 | 3057 | \f |
46b0e007 | 3058 | /* Return a simplified tree node for the truth-negation of ARG. This |
3059 | never alters ARG itself. We assume that ARG is an operation that | |
318a728f | 3060 | returns a truth value (0 or 1). |
2bc77e10 | 3061 | |
318a728f | 3062 | FIXME: one would think we would fold the result, but it causes |
3063 | problems with the dominator optimizer. */ | |
6758b11c | 3064 | |
2bc77e10 | 3065 | tree |
389dd41b | 3066 | fold_truth_not_expr (location_t loc, tree arg) |
2bc77e10 | 3067 | { |
43158006 | 3068 | tree t, type = TREE_TYPE (arg); |
e233264a | 3069 | enum tree_code code = TREE_CODE (arg); |
389dd41b | 3070 | location_t loc1, loc2; |
2bc77e10 | 3071 | |
e233264a | 3072 | /* If this is a comparison, we can simply invert it, except for |
3073 | floating-point non-equality comparisons, in which case we just | |
3074 | enclose a TRUTH_NOT_EXPR around what we have. */ | |
2bc77e10 | 3075 | |
ce45a448 | 3076 | if (TREE_CODE_CLASS (code) == tcc_comparison) |
2bc77e10 | 3077 | { |
318a728f | 3078 | tree op_type = TREE_TYPE (TREE_OPERAND (arg, 0)); |
3079 | if (FLOAT_TYPE_P (op_type) | |
3080 | && flag_trapping_math | |
3081 | && code != ORDERED_EXPR && code != UNORDERED_EXPR | |
3082 | && code != NE_EXPR && code != EQ_EXPR) | |
6758b11c | 3083 | return NULL_TREE; |
43158006 | 3084 | |
3085 | code = invert_tree_comparison (code, HONOR_NANS (TYPE_MODE (op_type))); | |
3086 | if (code == ERROR_MARK) | |
3087 | return NULL_TREE; | |
3088 | ||
3089 | t = build2 (code, type, TREE_OPERAND (arg, 0), TREE_OPERAND (arg, 1)); | |
389dd41b | 3090 | SET_EXPR_LOCATION (t, loc); |
43158006 | 3091 | return t; |
e233264a | 3092 | } |
2bc77e10 | 3093 | |
e233264a | 3094 | switch (code) |
3095 | { | |
2bc77e10 | 3096 | case INTEGER_CST: |
b7f352d5 | 3097 | return constant_boolean_node (integer_zerop (arg), type); |
2bc77e10 | 3098 | |
3099 | case TRUTH_AND_EXPR: | |
389dd41b | 3100 | loc1 = EXPR_LOCATION (TREE_OPERAND (arg, 0)); |
3101 | loc2 = EXPR_LOCATION (TREE_OPERAND (arg, 1)); | |
3102 | if (loc1 == UNKNOWN_LOCATION) | |
3103 | loc1 = loc; | |
3104 | if (loc2 == UNKNOWN_LOCATION) | |
3105 | loc2 = loc; | |
43158006 | 3106 | t = build2 (TRUTH_OR_EXPR, type, |
389dd41b | 3107 | invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)), |
3108 | invert_truthvalue_loc (loc2, TREE_OPERAND (arg, 1))); | |
43158006 | 3109 | break; |
2bc77e10 | 3110 | |
3111 | case TRUTH_OR_EXPR: | |
389dd41b | 3112 | loc1 = EXPR_LOCATION (TREE_OPERAND (arg, 0)); |
3113 | loc2 = EXPR_LOCATION (TREE_OPERAND (arg, 1)); | |
3114 | if (loc1 == UNKNOWN_LOCATION) | |
3115 | loc1 = loc; | |
3116 | if (loc2 == UNKNOWN_LOCATION) | |
3117 | loc2 = loc; | |
43158006 | 3118 | t = build2 (TRUTH_AND_EXPR, type, |
389dd41b | 3119 | invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)), |
3120 | invert_truthvalue_loc (loc2, TREE_OPERAND (arg, 1))); | |
43158006 | 3121 | break; |
2bc77e10 | 3122 | |
9a7b73a1 | 3123 | case TRUTH_XOR_EXPR: |
3124 | /* Here we can invert either operand. We invert the first operand | |
3125 | unless the second operand is a TRUTH_NOT_EXPR in which case our | |
3126 | result is the XOR of the first operand with the inside of the | |
3127 | negation of the second operand. */ | |
3128 | ||
3129 | if (TREE_CODE (TREE_OPERAND (arg, 1)) == TRUTH_NOT_EXPR) | |
43158006 | 3130 | t = build2 (TRUTH_XOR_EXPR, type, TREE_OPERAND (arg, 0), |
3131 | TREE_OPERAND (TREE_OPERAND (arg, 1), 0)); | |
9a7b73a1 | 3132 | else |
43158006 | 3133 | t = build2 (TRUTH_XOR_EXPR, type, |
389dd41b | 3134 | invert_truthvalue_loc (loc, TREE_OPERAND (arg, 0)), |
43158006 | 3135 | TREE_OPERAND (arg, 1)); |
3136 | break; | |
9a7b73a1 | 3137 | |
2bc77e10 | 3138 | case TRUTH_ANDIF_EXPR: |
389dd41b | 3139 | loc1 = EXPR_LOCATION (TREE_OPERAND (arg, 0)); |
3140 | loc2 = EXPR_LOCATION (TREE_OPERAND (arg, 1)); | |
3141 | if (loc1 == UNKNOWN_LOCATION) | |
3142 | loc1 = loc; | |
3143 | if (loc2 == UNKNOWN_LOCATION) | |
3144 | loc2 = loc; | |
43158006 | 3145 | t = build2 (TRUTH_ORIF_EXPR, type, |
389dd41b | 3146 | invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)), |
3147 | invert_truthvalue_loc (loc2, TREE_OPERAND (arg, 1))); | |
43158006 | 3148 | break; |
2bc77e10 | 3149 | |
3150 | case TRUTH_ORIF_EXPR: | |
389dd41b | 3151 | loc1 = EXPR_LOCATION (TREE_OPERAND (arg, 0)); |
3152 | loc2 = EXPR_LOCATION (TREE_OPERAND (arg, 1)); | |
3153 | if (loc1 == UNKNOWN_LOCATION) | |
3154 | loc1 = loc; | |
3155 | if (loc2 == UNKNOWN_LOCATION) | |
3156 | loc2 = loc; | |
43158006 | 3157 | t = build2 (TRUTH_ANDIF_EXPR, type, |
389dd41b | 3158 | invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)), |
3159 | invert_truthvalue_loc (loc2, TREE_OPERAND (arg, 1))); | |
43158006 | 3160 | break; |
2bc77e10 | 3161 | |
3162 | case TRUTH_NOT_EXPR: | |
3163 | return TREE_OPERAND (arg, 0); | |
3164 | ||
3165 | case COND_EXPR: | |
76ce1401 | 3166 | { |
3167 | tree arg1 = TREE_OPERAND (arg, 1); | |
3168 | tree arg2 = TREE_OPERAND (arg, 2); | |
389dd41b | 3169 | |
3170 | loc1 = EXPR_LOCATION (TREE_OPERAND (arg, 1)); | |
3171 | loc2 = EXPR_LOCATION (TREE_OPERAND (arg, 2)); | |
3172 | if (loc1 == UNKNOWN_LOCATION) | |
3173 | loc1 = loc; | |
3174 | if (loc2 == UNKNOWN_LOCATION) | |
3175 | loc2 = loc; | |
3176 | ||
76ce1401 | 3177 | /* A COND_EXPR may have a throw as one operand, which |
3178 | then has void type. Just leave void operands | |
3179 | as they are. */ | |
43158006 | 3180 | t = build3 (COND_EXPR, type, TREE_OPERAND (arg, 0), |
3181 | VOID_TYPE_P (TREE_TYPE (arg1)) | |
389dd41b | 3182 | ? arg1 : invert_truthvalue_loc (loc1, arg1), |
43158006 | 3183 | VOID_TYPE_P (TREE_TYPE (arg2)) |
389dd41b | 3184 | ? arg2 : invert_truthvalue_loc (loc2, arg2)); |
43158006 | 3185 | break; |
76ce1401 | 3186 | } |
2bc77e10 | 3187 | |
3139f3ce | 3188 | case COMPOUND_EXPR: |
389dd41b | 3189 | loc1 = EXPR_LOCATION (TREE_OPERAND (arg, 1)); |
3190 | if (loc1 == UNKNOWN_LOCATION) | |
3191 | loc1 = loc; | |
3192 | t = build2 (COMPOUND_EXPR, type, | |
3193 | TREE_OPERAND (arg, 0), | |
3194 | invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 1))); | |
43158006 | 3195 | break; |
3139f3ce | 3196 | |
2bc77e10 | 3197 | case NON_LVALUE_EXPR: |
389dd41b | 3198 | loc1 = EXPR_LOCATION (TREE_OPERAND (arg, 0)); |
3199 | if (loc1 == UNKNOWN_LOCATION) | |
3200 | loc1 = loc; | |
3201 | return invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)); | |
2bc77e10 | 3202 | |
a9538d68 | 3203 | CASE_CONVERT: |
4ee9c684 | 3204 | if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE) |
43158006 | 3205 | { |
3206 | t = build1 (TRUTH_NOT_EXPR, type, arg); | |
3207 | break; | |
3208 | } | |
3209 | ||
3210 | /* ... fall through ... */ | |
4ee9c684 | 3211 | |
2bc77e10 | 3212 | case FLOAT_EXPR: |
389dd41b | 3213 | loc1 = EXPR_LOCATION (TREE_OPERAND (arg, 0)); |
3214 | if (loc1 == UNKNOWN_LOCATION) | |
3215 | loc1 = loc; | |
43158006 | 3216 | t = build1 (TREE_CODE (arg), type, |
389dd41b | 3217 | invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0))); |
43158006 | 3218 | break; |
2bc77e10 | 3219 | |
3220 | case BIT_AND_EXPR: | |
c35387e1 | 3221 | if (!integer_onep (TREE_OPERAND (arg, 1))) |
43158006 | 3222 | return NULL_TREE; |
3223 | t = build2 (EQ_EXPR, type, arg, build_int_cst (type, 0)); | |
3224 | break; | |
2bc77e10 | 3225 | |
468d693c | 3226 | case SAVE_EXPR: |
43158006 | 3227 | t = build1 (TRUTH_NOT_EXPR, type, arg); |
3228 | break; | |
f33c3a83 | 3229 | |
3230 | case CLEANUP_POINT_EXPR: | |
389dd41b | 3231 | loc1 = EXPR_LOCATION (TREE_OPERAND (arg, 0)); |
3232 | if (loc1 == UNKNOWN_LOCATION) | |
3233 | loc1 = loc; | |
43158006 | 3234 | t = build1 (CLEANUP_POINT_EXPR, type, |
389dd41b | 3235 | invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0))); |
43158006 | 3236 | break; |
0dbd1c74 | 3237 | |
3238 | default: | |
43158006 | 3239 | t = NULL_TREE; |
0dbd1c74 | 3240 | break; |
c35387e1 | 3241 | } |
6758b11c | 3242 | |
389dd41b | 3243 | if (t) |
3244 | SET_EXPR_LOCATION (t, loc); | |
43158006 | 3245 | |
3246 | return t; | |
6758b11c | 3247 | } |
3248 | ||
3249 | /* Return a simplified tree node for the truth-negation of ARG. This | |
3250 | never alters ARG itself. We assume that ARG is an operation that | |
3251 | returns a truth value (0 or 1). | |
3252 | ||
3253 | FIXME: one would think we would fold the result, but it causes | |
3254 | problems with the dominator optimizer. */ | |
3255 | ||
3256 | tree | |
389dd41b | 3257 | invert_truthvalue_loc (location_t loc, tree arg) |
6758b11c | 3258 | { |
3259 | tree tem; | |
3260 | ||
3261 | if (TREE_CODE (arg) == ERROR_MARK) | |
3262 | return arg; | |
3263 | ||
389dd41b | 3264 | tem = fold_truth_not_expr (loc, arg); |
6758b11c | 3265 | if (!tem) |
389dd41b | 3266 | { |
3267 | tem = build1 (TRUTH_NOT_EXPR, TREE_TYPE (arg), arg); | |
3268 | SET_EXPR_LOCATION (tem, loc); | |
3269 | } | |
6758b11c | 3270 | |
3271 | return tem; | |
2bc77e10 | 3272 | } |
3273 | ||
3274 | /* Given a bit-wise operation CODE applied to ARG0 and ARG1, see if both | |
3275 | operands are another bit-wise operation with a common input. If so, | |
3276 | distribute the bit operations to save an operation and possibly two if | |
3277 | constants are involved. For example, convert | |
de1b648b | 3278 | (A | B) & (A | C) into A | (B & C) |
2bc77e10 | 3279 | Further simplification will occur if B and C are constants. |
3280 | ||
3281 | If this optimization cannot be done, 0 will be returned. */ | |
3282 | ||
3283 | static tree | |
389dd41b | 3284 | distribute_bit_expr (location_t loc, enum tree_code code, tree type, |
3285 | tree arg0, tree arg1) | |
2bc77e10 | 3286 | { |
3287 | tree common; | |
3288 | tree left, right; | |
3289 | ||
3290 | if (TREE_CODE (arg0) != TREE_CODE (arg1) | |
3291 | || TREE_CODE (arg0) == code | |
5b1de181 | 3292 | || (TREE_CODE (arg0) != BIT_AND_EXPR |
3293 | && TREE_CODE (arg0) != BIT_IOR_EXPR)) | |
2bc77e10 | 3294 | return 0; |
3295 | ||
3296 | if (operand_equal_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg1, 0), 0)) | |
3297 | { | |
3298 | common = TREE_OPERAND (arg0, 0); | |
3299 | left = TREE_OPERAND (arg0, 1); | |
3300 | right = TREE_OPERAND (arg1, 1); | |
3301 | } | |
3302 | else if (operand_equal_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg1, 1), 0)) | |
3303 | { | |
3304 | common = TREE_OPERAND (arg0, 0); | |
3305 | left = TREE_OPERAND (arg0, 1); | |
3306 | right = TREE_OPERAND (arg1, 0); | |
3307 | } | |
3308 | else if (operand_equal_p (TREE_OPERAND (arg0, 1), TREE_OPERAND (arg1, 0), 0)) | |
3309 | { | |
3310 | common = TREE_OPERAND (arg0, 1); | |
3311 | left = TREE_OPERAND (arg0, 0); | |
3312 | right = TREE_OPERAND (arg1, 1); | |
3313 | } | |
3314 | else if (operand_equal_p (TREE_OPERAND (arg0, 1), TREE_OPERAND (arg1, 1), 0)) | |
3315 | { | |
3316 | common = TREE_OPERAND (arg0, 1); | |
3317 | left = TREE_OPERAND (arg0, 0); | |
3318 | right = TREE_OPERAND (arg1, 0); | |
3319 | } | |
3320 | else | |
3321 | return 0; | |
3322 | ||
389dd41b | 3323 | common = fold_convert_loc (loc, type, common); |
3324 | left = fold_convert_loc (loc, type, left); | |
3325 | right = fold_convert_loc (loc, type, right); | |
3326 | return fold_build2_loc (loc, TREE_CODE (arg0), type, common, | |
3327 | fold_build2_loc (loc, code, type, left, right)); | |
2bc77e10 | 3328 | } |
429f2f90 | 3329 | |
3330 | /* Knowing that ARG0 and ARG1 are both RDIV_EXPRs, simplify a binary operation | |
3331 | with code CODE. This optimization is unsafe. */ | |
3332 | static tree | |
389dd41b | 3333 | distribute_real_division (location_t loc, enum tree_code code, tree type, |
3334 | tree arg0, tree arg1) | |
429f2f90 | 3335 | { |
3336 | bool mul0 = TREE_CODE (arg0) == MULT_EXPR; | |
3337 | bool mul1 = TREE_CODE (arg1) == MULT_EXPR; | |
3338 | ||
3339 | /* (A / C) +- (B / C) -> (A +- B) / C. */ | |
3340 | if (mul0 == mul1 | |
3341 | && operand_equal_p (TREE_OPERAND (arg0, 1), | |
3342 | TREE_OPERAND (arg1, 1), 0)) | |
389dd41b | 3343 | return fold_build2_loc (loc, mul0 ? MULT_EXPR : RDIV_EXPR, type, |
3344 | fold_build2_loc (loc, code, type, | |
429f2f90 | 3345 | TREE_OPERAND (arg0, 0), |
3346 | TREE_OPERAND (arg1, 0)), | |
3347 | TREE_OPERAND (arg0, 1)); | |
3348 | ||
3349 | /* (A / C1) +- (A / C2) -> A * (1 / C1 +- 1 / C2). */ | |
3350 | if (operand_equal_p (TREE_OPERAND (arg0, 0), | |
3351 | TREE_OPERAND (arg1, 0), 0) | |
3352 | && TREE_CODE (TREE_OPERAND (arg0, 1)) == REAL_CST | |
3353 | && TREE_CODE (TREE_OPERAND (arg1, 1)) == REAL_CST) | |
3354 | { | |
3355 | REAL_VALUE_TYPE r0, r1; | |
3356 | r0 = TREE_REAL_CST (TREE_OPERAND (arg0, 1)); | |
3357 | r1 = TREE_REAL_CST (TREE_OPERAND (arg1, 1)); | |
3358 | if (!mul0) | |
3359 | real_arithmetic (&r0, RDIV_EXPR, &dconst1, &r0); | |
3360 | if (!mul1) | |
3361 | real_arithmetic (&r1, RDIV_EXPR, &dconst1, &r1); | |
3362 | real_arithmetic (&r0, code, &r0, &r1); | |
389dd41b | 3363 | return fold_build2_loc (loc, MULT_EXPR, type, |
429f2f90 | 3364 | TREE_OPERAND (arg0, 0), |
3365 | build_real (type, r0)); | |
3366 | } | |
3367 | ||
3368 | return NULL_TREE; | |
3369 | } | |
2bc77e10 | 3370 | \f |
2a64c730 | 3371 | /* Return a BIT_FIELD_REF of type TYPE to refer to BITSIZE bits of INNER |
3372 | starting at BITPOS. The field is unsigned if UNSIGNEDP is nonzero. */ | |
3373 | ||
3374 | static tree | |
389dd41b | 3375 | make_bit_field_ref (location_t loc, tree inner, tree type, |
3376 | HOST_WIDE_INT bitsize, HOST_WIDE_INT bitpos, int unsignedp) | |
2a64c730 | 3377 | { |
3378 | tree result, bftype; | |
3379 | ||
3380 | if (bitpos == 0) | |
3381 | { | |
3382 | tree size = TYPE_SIZE (TREE_TYPE (inner)); | |
3383 | if ((INTEGRAL_TYPE_P (TREE_TYPE (inner)) | |
3384 | || POINTER_TYPE_P (TREE_TYPE (inner))) | |
48e1416a | 3385 | && host_integerp (size, 0) |
2a64c730 | 3386 | && tree_low_cst (size, 0) == bitsize) |
389dd41b | 3387 | return fold_convert_loc (loc, type, inner); |
2a64c730 | 3388 | } |
3389 | ||
3390 | bftype = type; | |
3391 | if (TYPE_PRECISION (bftype) != bitsize | |
3392 | || TYPE_UNSIGNED (bftype) == !unsignedp) | |
3393 | bftype = build_nonstandard_integer_type (bitsize, 0); | |
3394 | ||
3395 | result = build3 (BIT_FIELD_REF, bftype, inner, | |
3396 | size_int (bitsize), bitsize_int (bitpos)); | |
389dd41b | 3397 | SET_EXPR_LOCATION (result, loc); |
2a64c730 | 3398 | |
3399 | if (bftype != type) | |
389dd41b | 3400 | result = fold_convert_loc (loc, type, result); |
2a64c730 | 3401 | |
3402 | return result; | |
3403 | } | |
3404 | ||
3405 | /* Optimize a bit-field compare. | |
3406 | ||
3407 | There are two cases: First is a compare against a constant and the | |
3408 | second is a comparison of two items where the fields are at the same | |
3409 | bit position relative to the start of a chunk (byte, halfword, word) | |
3410 | large enough to contain it. In these cases we can avoid the shift | |
3411 | implicit in bitfield extractions. | |
3412 | ||
3413 | For constants, we emit a compare of the shifted constant with the | |
3414 | BIT_AND_EXPR of a mask and a byte, halfword, or word of the operand being | |
3415 | compared. For two fields at the same position, we do the ANDs with the | |
3416 | similar mask and compare the result of the ANDs. | |
3417 | ||
3418 | CODE is the comparison code, known to be either NE_EXPR or EQ_EXPR. | |
3419 | COMPARE_TYPE is the type of the comparison, and LHS and RHS | |
3420 | are the left and right operands of the comparison, respectively. | |
3421 | ||
3422 | If the optimization described above can be done, we return the resulting | |
3423 | tree. Otherwise we return zero. */ | |
3424 | ||
3425 | static tree | |
389dd41b | 3426 | optimize_bit_field_compare (location_t loc, enum tree_code code, |
3427 | tree compare_type, tree lhs, tree rhs) | |
2a64c730 | 3428 | { |
3429 | HOST_WIDE_INT lbitpos, lbitsize, rbitpos, rbitsize, nbitpos, nbitsize; | |
3430 | tree type = TREE_TYPE (lhs); | |
3431 | tree signed_type, unsigned_type; | |
3432 | int const_p = TREE_CODE (rhs) == INTEGER_CST; | |
3433 | enum machine_mode lmode, rmode, nmode; | |
3434 | int lunsignedp, runsignedp; | |
3435 | int lvolatilep = 0, rvolatilep = 0; | |
3436 | tree linner, rinner = NULL_TREE; | |
3437 | tree mask; | |
3438 | tree offset; | |
3439 | ||
3440 | /* Get all the information about the extractions being done. If the bit size | |
3441 | if the same as the size of the underlying object, we aren't doing an | |
3442 | extraction at all and so can do nothing. We also don't want to | |
3443 | do anything if the inner expression is a PLACEHOLDER_EXPR since we | |
3444 | then will no longer be able to replace it. */ | |
3445 | linner = get_inner_reference (lhs, &lbitsize, &lbitpos, &offset, &lmode, | |
3446 | &lunsignedp, &lvolatilep, false); | |
3447 | if (linner == lhs || lbitsize == GET_MODE_BITSIZE (lmode) || lbitsize < 0 | |
3448 | || offset != 0 || TREE_CODE (linner) == PLACEHOLDER_EXPR) | |
3449 | return 0; | |
3450 | ||
3451 | if (!const_p) | |
3452 | { | |
3453 | /* If this is not a constant, we can only do something if bit positions, | |
3454 | sizes, and signedness are the same. */ | |
3455 | rinner = get_inner_reference (rhs, &rbitsize, &rbitpos, &offset, &rmode, | |
3456 | &runsignedp, &rvolatilep, false); | |
3457 | ||
3458 | if (rinner == rhs || lbitpos != rbitpos || lbitsize != rbitsize | |
3459 | || lunsignedp != runsignedp || offset != 0 | |
3460 | || TREE_CODE (rinner) == PLACEHOLDER_EXPR) | |
3461 | return 0; | |
3462 | } | |
3463 | ||
3464 | /* See if we can find a mode to refer to this field. We should be able to, | |
3465 | but fail if we can't. */ | |
3466 | nmode = get_best_mode (lbitsize, lbitpos, | |
3467 | const_p ? TYPE_ALIGN (TREE_TYPE (linner)) | |
3468 | : MIN (TYPE_ALIGN (TREE_TYPE (linner)), | |
3469 | TYPE_ALIGN (TREE_TYPE (rinner))), | |
3470 | word_mode, lvolatilep || rvolatilep); | |
3471 | if (nmode == VOIDmode) | |
3472 | return 0; | |
3473 | ||
3474 | /* Set signed and unsigned types of the precision of this mode for the | |
3475 | shifts below. */ | |
3476 | signed_type = lang_hooks.types.type_for_mode (nmode, 0); | |
3477 | unsigned_type = lang_hooks.types.type_for_mode (nmode, 1); | |
3478 | ||
3479 | /* Compute the bit position and size for the new reference and our offset | |
3480 | within it. If the new reference is the same size as the original, we | |
3481 | won't optimize anything, so return zero. */ | |
3482 | nbitsize = GET_MODE_BITSIZE (nmode); | |
3483 | nbitpos = lbitpos & ~ (nbitsize - 1); | |
3484 | lbitpos -= nbitpos; | |
3485 | if (nbitsize == lbitsize) | |
3486 | return 0; | |
3487 | ||
3488 | if (BYTES_BIG_ENDIAN) | |
3489 | lbitpos = nbitsize - lbitsize - lbitpos; | |
3490 | ||
3491 | /* Make the mask to be used against the extracted field. */ | |
3492 | mask = build_int_cst_type (unsigned_type, -1); | |
3493 | mask = const_binop (LSHIFT_EXPR, mask, size_int (nbitsize - lbitsize), 0); | |
3494 | mask = const_binop (RSHIFT_EXPR, mask, | |
3495 | size_int (nbitsize - lbitsize - lbitpos), 0); | |
3496 | ||
3497 | if (! const_p) | |
3498 | /* If not comparing with constant, just rework the comparison | |
3499 | and return. */ | |
389dd41b | 3500 | return fold_build2_loc (loc, code, compare_type, |
3501 | fold_build2_loc (loc, BIT_AND_EXPR, unsigned_type, | |
3502 | make_bit_field_ref (loc, linner, | |
2a64c730 | 3503 | unsigned_type, |
3504 | nbitsize, nbitpos, | |
3505 | 1), | |
3506 | mask), | |
389dd41b | 3507 | fold_build2_loc (loc, BIT_AND_EXPR, unsigned_type, |
3508 | make_bit_field_ref (loc, rinner, | |
2a64c730 | 3509 | unsigned_type, |
3510 | nbitsize, nbitpos, | |
3511 | 1), | |
3512 | mask)); | |
3513 | ||
3514 | /* Otherwise, we are handling the constant case. See if the constant is too | |
3515 | big for the field. Warn and return a tree of for 0 (false) if so. We do | |
3516 | this not only for its own sake, but to avoid having to test for this | |
3517 | error case below. If we didn't, we might generate wrong code. | |
3518 | ||
3519 | For unsigned fields, the constant shifted right by the field length should | |
3520 | be all zero. For signed fields, the high-order bits should agree with | |
3521 | the sign bit. */ | |
3522 | ||
3523 | if (lunsignedp) | |
3524 | { | |
3525 | if (! integer_zerop (const_binop (RSHIFT_EXPR, | |
389dd41b | 3526 | fold_convert_loc (loc, |
3527 | unsigned_type, rhs), | |
2a64c730 | 3528 | size_int (lbitsize), 0))) |
3529 | { | |
3530 | warning (0, "comparison is always %d due to width of bit-field", | |
3531 | code == NE_EXPR); | |
3532 | return constant_boolean_node (code == NE_EXPR, compare_type); | |
3533 | } | |
3534 | } | |
3535 | else | |
3536 | { | |
389dd41b | 3537 | tree tem = const_binop (RSHIFT_EXPR, |
3538 | fold_convert_loc (loc, signed_type, rhs), | |
2a64c730 | 3539 | size_int (lbitsize - 1), 0); |
3540 | if (! integer_zerop (tem) && ! integer_all_onesp (tem)) | |
3541 | { | |
3542 | warning (0, "comparison is always %d due to width of bit-field", | |
3543 | code == NE_EXPR); | |
3544 | return constant_boolean_node (code == NE_EXPR, compare_type); | |
3545 | } | |
3546 | } | |
3547 | ||
3548 | /* Single-bit compares should always be against zero. */ | |
3549 | if (lbitsize == 1 && ! integer_zerop (rhs)) | |
3550 | { | |
3551 | code = code == EQ_EXPR ? NE_EXPR : EQ_EXPR; | |
3552 | rhs = build_int_cst (type, 0); | |
3553 | } | |
3554 | ||
3555 | /* Make a new bitfield reference, shift the constant over the | |
3556 | appropriate number of bits and mask it with the computed mask | |
3557 | (in case this was a signed field). If we changed it, make a new one. */ | |
389dd41b | 3558 | lhs = make_bit_field_ref (loc, linner, unsigned_type, nbitsize, nbitpos, 1); |
2a64c730 | 3559 | if (lvolatilep) |
3560 | { | |
3561 | TREE_SIDE_EFFECTS (lhs) = 1; | |
3562 | TREE_THIS_VOLATILE (lhs) = 1; | |
3563 | } | |
3564 | ||
3565 | rhs = const_binop (BIT_AND_EXPR, | |
3566 | const_binop (LSHIFT_EXPR, | |
389dd41b | 3567 | fold_convert_loc (loc, unsigned_type, rhs), |
2a64c730 | 3568 | size_int (lbitpos), 0), |
3569 | mask, 0); | |
3570 | ||
389dd41b | 3571 | lhs = build2 (code, compare_type, |
3572 | build2 (BIT_AND_EXPR, unsigned_type, lhs, mask), | |
3573 | rhs); | |
3574 | SET_EXPR_LOCATION (lhs, loc); | |
3575 | return lhs; | |
2a64c730 | 3576 | } |
3577 | \f | |
79109eec | 3578 | /* Subroutine for fold_truthop: decode a field reference. |
2bc77e10 | 3579 | |
3580 | If EXP is a comparison reference, we return the innermost reference. | |
3581 | ||
3582 | *PBITSIZE is set to the number of bits in the reference, *PBITPOS is | |
3583 | set to the starting bit number. | |
3584 | ||
3585 | If the innermost field can be completely contained in a mode-sized | |
3586 | unit, *PMODE is set to that mode. Otherwise, it is set to VOIDmode. | |
3587 | ||
3588 | *PVOLATILEP is set to 1 if the any expression encountered is volatile; | |
3589 | otherwise it is not changed. | |
3590 | ||
3591 | *PUNSIGNEDP is set to the signedness of the field. | |
3592 | ||
3593 | *PMASK is set to the mask used. This is either contained in a | |
3594 | BIT_AND_EXPR or derived from the width of the field. | |
3595 | ||
3398e91d | 3596 | *PAND_MASK is set to the mask found in a BIT_AND_EXPR, if any. |
2a6329ae | 3597 | |
2bc77e10 | 3598 | Return 0 if this is not a component reference or is one that we can't |
3599 | do anything with. */ | |
3600 | ||
3601 | static tree | |
389dd41b | 3602 | decode_field_reference (location_t loc, tree exp, HOST_WIDE_INT *pbitsize, |
dc81944a | 3603 | HOST_WIDE_INT *pbitpos, enum machine_mode *pmode, |
3604 | int *punsignedp, int *pvolatilep, | |
de1b648b | 3605 | tree *pmask, tree *pand_mask) |
2bc77e10 | 3606 | { |
74878f86 | 3607 | tree outer_type = 0; |
4843fe7c | 3608 | tree and_mask = 0; |
3609 | tree mask, inner, offset; | |
3610 | tree unsigned_type; | |
02e7a332 | 3611 | unsigned int precision; |
2bc77e10 | 3612 | |
cc049fa3 | 3613 | /* All the optimizations using this function assume integer fields. |
e40566fc | 3614 | There are problems with FP fields since the type_for_size call |
3615 | below can fail for, e.g., XFmode. */ | |
3616 | if (! INTEGRAL_TYPE_P (TREE_TYPE (exp))) | |
3617 | return 0; | |
3618 | ||
74878f86 | 3619 | /* We are interested in the bare arrangement of bits, so strip everything |
3620 | that doesn't affect the machine mode. However, record the type of the | |
3621 | outermost expression if it may matter below. */ | |
72dd6141 | 3622 | if (CONVERT_EXPR_P (exp) |
74878f86 | 3623 | || TREE_CODE (exp) == NON_LVALUE_EXPR) |
3624 | outer_type = TREE_TYPE (exp); | |
78379bd9 | 3625 | STRIP_NOPS (exp); |
2bc77e10 | 3626 | |
3627 | if (TREE_CODE (exp) == BIT_AND_EXPR) | |
3628 | { | |
4843fe7c | 3629 | and_mask = TREE_OPERAND (exp, 1); |
2bc77e10 | 3630 | exp = TREE_OPERAND (exp, 0); |
4843fe7c | 3631 | STRIP_NOPS (exp); STRIP_NOPS (and_mask); |
3632 | if (TREE_CODE (and_mask) != INTEGER_CST) | |
2bc77e10 | 3633 | return 0; |
3634 | } | |
3635 | ||
bbfbdece | 3636 | inner = get_inner_reference (exp, pbitsize, pbitpos, &offset, pmode, |
e7e9416e | 3637 | punsignedp, pvolatilep, false); |
94f29e88 | 3638 | if ((inner == exp && and_mask == 0) |
155b05dc | 3639 | || *pbitsize < 0 || offset != 0 |
3640 | || TREE_CODE (inner) == PLACEHOLDER_EXPR) | |
e233264a | 3641 | return 0; |
cc049fa3 | 3642 | |
74878f86 | 3643 | /* If the number of bits in the reference is the same as the bitsize of |
3644 | the outer type, then the outer type gives the signedness. Otherwise | |
3645 | (in case of a small bitfield) the signedness is unchanged. */ | |
18dbec6f | 3646 | if (outer_type && *pbitsize == TYPE_PRECISION (outer_type)) |
78a8ed03 | 3647 | *punsignedp = TYPE_UNSIGNED (outer_type); |
74878f86 | 3648 | |
4843fe7c | 3649 | /* Compute the mask to access the bitfield. */ |
fa8b888f | 3650 | unsigned_type = lang_hooks.types.type_for_size (*pbitsize, 1); |
4843fe7c | 3651 | precision = TYPE_PRECISION (unsigned_type); |
3652 | ||
697bbc3f | 3653 | mask = build_int_cst_type (unsigned_type, -1); |
0c5713a2 | 3654 | |
4843fe7c | 3655 | mask = const_binop (LSHIFT_EXPR, mask, size_int (precision - *pbitsize), 0); |
3656 | mask = const_binop (RSHIFT_EXPR, mask, size_int (precision - *pbitsize), 0); | |
3657 | ||
3658 | /* Merge it with the mask we found in the BIT_AND_EXPR, if any. */ | |
3659 | if (and_mask != 0) | |
389dd41b | 3660 | mask = fold_build2_loc (loc, BIT_AND_EXPR, unsigned_type, |
3661 | fold_convert_loc (loc, unsigned_type, and_mask), mask); | |
2bc77e10 | 3662 | |
3663 | *pmask = mask; | |
2a6329ae | 3664 | *pand_mask = and_mask; |
2bc77e10 | 3665 | return inner; |
3666 | } | |
3667 | ||
2a64c730 | 3668 | /* Return nonzero if MASK represents a mask of SIZE ones in the low-order |
3669 | bit positions. */ | |
3670 | ||
3671 | static int | |
3672 | all_ones_mask_p (const_tree mask, int size) | |
3673 | { | |
3674 | tree type = TREE_TYPE (mask); | |
3675 | unsigned int precision = TYPE_PRECISION (type); | |
3676 | tree tmask; | |
3677 | ||
3678 | tmask = build_int_cst_type (signed_type_for (type), -1); | |
3679 | ||
3680 | return | |
3681 | tree_int_cst_equal (mask, | |
3682 | const_binop (RSHIFT_EXPR, | |
3683 | const_binop (LSHIFT_EXPR, tmask, | |
3684 | size_int (precision - size), | |
3685 | 0), | |
3686 | size_int (precision - size), 0)); | |
3687 | } | |
3688 | ||
203a24c4 | 3689 | /* Subroutine for fold: determine if VAL is the INTEGER_CONST that |
3690 | represents the sign bit of EXP's type. If EXP represents a sign | |
3691 | or zero extension, also test VAL against the unextended type. | |
3692 | The return value is the (sub)expression whose sign bit is VAL, | |
3693 | or NULL_TREE otherwise. */ | |
3694 | ||
3695 | static tree | |
b4b34335 | 3696 | sign_bit_p (tree exp, const_tree val) |
203a24c4 | 3697 | { |
a4de5624 | 3698 | unsigned HOST_WIDE_INT mask_lo, lo; |
3699 | HOST_WIDE_INT mask_hi, hi; | |
203a24c4 | 3700 | int width; |
3701 | tree t; | |
3702 | ||
95cc2547 | 3703 | /* Tree EXP must have an integral type. */ |
203a24c4 | 3704 | t = TREE_TYPE (exp); |
3705 | if (! INTEGRAL_TYPE_P (t)) | |
3706 | return NULL_TREE; | |
3707 | ||
3708 | /* Tree VAL must be an integer constant. */ | |
3709 | if (TREE_CODE (val) != INTEGER_CST | |
f96bd2bf | 3710 | || TREE_OVERFLOW (val)) |
203a24c4 | 3711 | return NULL_TREE; |
3712 | ||
3713 | width = TYPE_PRECISION (t); | |
3714 | if (width > HOST_BITS_PER_WIDE_INT) | |
3715 | { | |
3716 | hi = (unsigned HOST_WIDE_INT) 1 << (width - HOST_BITS_PER_WIDE_INT - 1); | |
3717 | lo = 0; | |
a4de5624 | 3718 | |
3719 | mask_hi = ((unsigned HOST_WIDE_INT) -1 | |
3720 | >> (2 * HOST_BITS_PER_WIDE_INT - width)); | |
3721 | mask_lo = -1; | |
203a24c4 | 3722 | } |
3723 | else | |
3724 | { | |
3725 | hi = 0; | |
3726 | lo = (unsigned HOST_WIDE_INT) 1 << (width - 1); | |
a4de5624 | 3727 | |
3728 | mask_hi = 0; | |
3729 | mask_lo = ((unsigned HOST_WIDE_INT) -1 | |
3730 | >> (HOST_BITS_PER_WIDE_INT - width)); | |
203a24c4 | 3731 | } |
3732 | ||
a4de5624 | 3733 | /* We mask off those bits beyond TREE_TYPE (exp) so that we can |
3734 | treat VAL as if it were unsigned. */ | |
3735 | if ((TREE_INT_CST_HIGH (val) & mask_hi) == hi | |
3736 | && (TREE_INT_CST_LOW (val) & mask_lo) == lo) | |
203a24c4 | 3737 | return exp; |
3738 | ||
3739 | /* Handle extension from a narrower type. */ | |
3740 | if (TREE_CODE (exp) == NOP_EXPR | |
3741 | && TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (exp, 0))) < width) | |
3742 | return sign_bit_p (TREE_OPERAND (exp, 0), val); | |
3743 | ||
3744 | return NULL_TREE; | |
3745 | } | |
3746 | ||
79109eec | 3747 | /* Subroutine for fold_truthop: determine if an operand is simple enough |
3748 | to be evaluated unconditionally. */ | |
3749 | ||
cc049fa3 | 3750 | static int |
b4b34335 | 3751 | simple_operand_p (const_tree exp) |
79109eec | 3752 | { |
3753 | /* Strip any conversions that don't change the machine mode. */ | |
9a73db25 | 3754 | STRIP_NOPS (exp); |
79109eec | 3755 | |
ce45a448 | 3756 | return (CONSTANT_CLASS_P (exp) |
9a73db25 | 3757 | || TREE_CODE (exp) == SSA_NAME |
9308e976 | 3758 | || (DECL_P (exp) |
79109eec | 3759 | && ! TREE_ADDRESSABLE (exp) |
3760 | && ! TREE_THIS_VOLATILE (exp) | |
7735dddb | 3761 | && ! DECL_NONLOCAL (exp) |
3762 | /* Don't regard global variables as simple. They may be | |
3763 | allocated in ways unknown to the compiler (shared memory, | |
3764 | #pragma weak, etc). */ | |
3765 | && ! TREE_PUBLIC (exp) | |
3766 | && ! DECL_EXTERNAL (exp) | |
3767 | /* Loading a static variable is unduly expensive, but global | |
3768 | registers aren't expensive. */ | |
3769 | && (! TREE_STATIC (exp) || DECL_REGISTER (exp)))); | |
79109eec | 3770 | } |
2bc77e10 | 3771 | \f |
12ec0a8a | 3772 | /* The following functions are subroutines to fold_range_test and allow it to |
3773 | try to change a logical combination of comparisons into a range test. | |
3774 | ||
3775 | For example, both | |
de1b648b | 3776 | X == 2 || X == 3 || X == 4 || X == 5 |
12ec0a8a | 3777 | and |
de1b648b | 3778 | X >= 2 && X <= 5 |
12ec0a8a | 3779 | are converted to |
3780 | (unsigned) (X - 2) <= 3 | |
3781 | ||
ad87de1e | 3782 | We describe each set of comparisons as being either inside or outside |
12ec0a8a | 3783 | a range, using a variable named like IN_P, and then describe the |
3784 | range with a lower and upper bound. If one of the bounds is omitted, | |
3785 | it represents either the highest or lowest value of the type. | |
3786 | ||
3787 | In the comments below, we represent a range by two numbers in brackets | |
ad87de1e | 3788 | preceded by a "+" to designate being inside that range, or a "-" to |
12ec0a8a | 3789 | designate being outside that range, so the condition can be inverted by |
3790 | flipping the prefix. An omitted bound is represented by a "-". For | |
3791 | example, "- [-, 10]" means being outside the range starting at the lowest | |
3792 | possible value and ending at 10, in other words, being greater than 10. | |
3793 | The range "+ [-, -]" is always true and hence the range "- [-, -]" is | |
3794 | always false. | |
3795 | ||
3796 | We set up things so that the missing bounds are handled in a consistent | |
3797 | manner so neither a missing bound nor "true" and "false" need to be | |
3798 | handled using a special case. */ | |
3799 | ||
3800 | /* Return the result of applying CODE to ARG0 and ARG1, but handle the case | |
3801 | of ARG0 and/or ARG1 being omitted, meaning an unlimited range. UPPER0_P | |
3802 | and UPPER1_P are nonzero if the respective argument is an upper bound | |
3803 | and zero for a lower. TYPE, if nonzero, is the type of the result; it | |
3804 | must be specified for a comparison. ARG1 will be converted to ARG0's | |
3805 | type if both are specified. */ | |
6f725368 | 3806 | |
12ec0a8a | 3807 | static tree |
dc81944a | 3808 | range_binop (enum tree_code code, tree type, tree arg0, int upper0_p, |
3809 | tree arg1, int upper1_p) | |
12ec0a8a | 3810 | { |
7560c8de | 3811 | tree tem; |
12ec0a8a | 3812 | int result; |
3813 | int sgn0, sgn1; | |
6f725368 | 3814 | |
12ec0a8a | 3815 | /* If neither arg represents infinity, do the normal operation. |
3816 | Else, if not a comparison, return infinity. Else handle the special | |
3817 | comparison rules. Note that most of the cases below won't occur, but | |
3818 | are handled for consistency. */ | |
6f725368 | 3819 | |
12ec0a8a | 3820 | if (arg0 != 0 && arg1 != 0) |
7560c8de | 3821 | { |
7ab7fd4f | 3822 | tem = fold_build2 (code, type != 0 ? type : TREE_TYPE (arg0), |
3823 | arg0, fold_convert (TREE_TYPE (arg0), arg1)); | |
7560c8de | 3824 | STRIP_NOPS (tem); |
3825 | return TREE_CODE (tem) == INTEGER_CST ? tem : 0; | |
3826 | } | |
6f725368 | 3827 | |
ce45a448 | 3828 | if (TREE_CODE_CLASS (code) != tcc_comparison) |
12ec0a8a | 3829 | return 0; |
3830 | ||
3831 | /* Set SGN[01] to -1 if ARG[01] is a lower bound, 1 for upper, and 0 | |
621ba396 | 3832 | for neither. In real maths, we cannot assume open ended ranges are |
3833 | the same. But, this is computer arithmetic, where numbers are finite. | |
3834 | We can therefore make the transformation of any unbounded range with | |
3835 | the value Z, Z being greater than any representable number. This permits | |
6312a35e | 3836 | us to treat unbounded ranges as equal. */ |
12ec0a8a | 3837 | sgn0 = arg0 != 0 ? 0 : (upper0_p ? 1 : -1); |
263497ab | 3838 | sgn1 = arg1 != 0 ? 0 : (upper1_p ? 1 : -1); |
12ec0a8a | 3839 | switch (code) |
3840 | { | |
621ba396 | 3841 | case EQ_EXPR: |
3842 | result = sgn0 == sgn1; | |
3843 | break; | |
3844 | case NE_EXPR: | |
3845 | result = sgn0 != sgn1; | |
12ec0a8a | 3846 | break; |
621ba396 | 3847 | case LT_EXPR: |
12ec0a8a | 3848 | result = sgn0 < sgn1; |
3849 | break; | |
621ba396 | 3850 | case LE_EXPR: |
3851 | result = sgn0 <= sgn1; | |
3852 | break; | |
3853 | case GT_EXPR: | |
12ec0a8a | 3854 | result = sgn0 > sgn1; |
3855 | break; | |
621ba396 | 3856 | case GE_EXPR: |
3857 | result = sgn0 >= sgn1; | |
3858 | break; | |
0dbd1c74 | 3859 | default: |
fdada98f | 3860 | gcc_unreachable (); |
12ec0a8a | 3861 | } |
3862 | ||
20783f07 | 3863 | return constant_boolean_node (result, type); |
12ec0a8a | 3864 | } |
cc049fa3 | 3865 | \f |
12ec0a8a | 3866 | /* Given EXP, a logical expression, set the range it is testing into |
3867 | variables denoted by PIN_P, PLOW, and PHIGH. Return the expression | |
add6ee5e | 3868 | actually being tested. *PLOW and *PHIGH will be made of the same |
3869 | type as the returned expression. If EXP is not a comparison, we | |
3870 | will most likely not be returning a useful value and range. Set | |
3871 | *STRICT_OVERFLOW_P to true if the return value is only valid | |
3872 | because signed overflow is undefined; otherwise, do not change | |
3873 | *STRICT_OVERFLOW_P. */ | |
6f725368 | 3874 | |
9c20c4fc | 3875 | tree |
add6ee5e | 3876 | make_range (tree exp, int *pin_p, tree *plow, tree *phigh, |
3877 | bool *strict_overflow_p) | |
6f725368 | 3878 | { |
12ec0a8a | 3879 | enum tree_code code; |
7206da1b | 3880 | tree arg0 = NULL_TREE, arg1 = NULL_TREE; |
3881 | tree exp_type = NULL_TREE, arg0_type = NULL_TREE; | |
12ec0a8a | 3882 | int in_p, n_in_p; |
3883 | tree low, high, n_low, n_high; | |
389dd41b | 3884 | location_t loc = EXPR_LOCATION (exp); |
6f725368 | 3885 | |
12ec0a8a | 3886 | /* Start with simply saying "EXP != 0" and then look at the code of EXP |
3887 | and see if we can refine the range. Some of the cases below may not | |
3888 | happen, but it doesn't seem worth worrying about this. We "continue" | |
3889 | the outer loop when we've changed something; otherwise we "break" | |
3890 | the switch, which will "break" the while. */ | |
6f725368 | 3891 | |
b30e3dbc | 3892 | in_p = 0; |
3c6185f1 | 3893 | low = high = build_int_cst (TREE_TYPE (exp), 0); |
12ec0a8a | 3894 | |
3895 | while (1) | |
6f725368 | 3896 | { |
12ec0a8a | 3897 | code = TREE_CODE (exp); |
7206da1b | 3898 | exp_type = TREE_TYPE (exp); |
5eb945de | 3899 | |
3900 | if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (code))) | |
3901 | { | |
c2f47e15 | 3902 | if (TREE_OPERAND_LENGTH (exp) > 0) |
13795292 | 3903 | arg0 = TREE_OPERAND (exp, 0); |
ce45a448 | 3904 | if (TREE_CODE_CLASS (code) == tcc_comparison |
3905 | || TREE_CODE_CLASS (code) == tcc_unary | |
3906 | || TREE_CODE_CLASS (code) == tcc_binary) | |
7206da1b | 3907 | arg0_type = TREE_TYPE (arg0); |
ce45a448 | 3908 | if (TREE_CODE_CLASS (code) == tcc_binary |
3909 | || TREE_CODE_CLASS (code) == tcc_comparison | |
3910 | || (TREE_CODE_CLASS (code) == tcc_expression | |
c2f47e15 | 3911 | && TREE_OPERAND_LENGTH (exp) > 1)) |
5eb945de | 3912 | arg1 = TREE_OPERAND (exp, 1); |
3913 | } | |
6f725368 | 3914 | |
12ec0a8a | 3915 | switch (code) |
3916 | { | |
3917 | case TRUTH_NOT_EXPR: | |
3918 | in_p = ! in_p, exp = arg0; | |
3919 | continue; | |
3920 | ||
3921 | case EQ_EXPR: case NE_EXPR: | |
3922 | case LT_EXPR: case LE_EXPR: case GE_EXPR: case GT_EXPR: | |
3923 | /* We can only do something if the range is testing for zero | |
3924 | and if the second operand is an integer constant. Note that | |
3925 | saying something is "in" the range we make is done by | |
3926 | complementing IN_P since it will set in the initial case of | |
3927 | being not equal to zero; "out" is leaving it alone. */ | |
3928 | if (low == 0 || high == 0 | |
3929 | || ! integer_zerop (low) || ! integer_zerop (high) | |
3930 | || TREE_CODE (arg1) != INTEGER_CST) | |
3931 | break; | |
6f725368 | 3932 | |
12ec0a8a | 3933 | switch (code) |
3934 | { | |
3935 | case NE_EXPR: /* - [c, c] */ | |
3936 | low = high = arg1; | |
3937 | break; | |
3938 | case EQ_EXPR: /* + [c, c] */ | |
3939 | in_p = ! in_p, low = high = arg1; | |
3940 | break; | |
3941 | case GT_EXPR: /* - [-, c] */ | |
3942 | low = 0, high = arg1; | |
3943 | break; | |
3944 | case GE_EXPR: /* + [c, -] */ | |
3945 | in_p = ! in_p, low = arg1, high = 0; | |
3946 | break; | |
3947 | case LT_EXPR: /* - [c, -] */ | |
3948 | low = arg1, high = 0; | |
3949 | break; | |
3950 | case LE_EXPR: /* + [-, c] */ | |
3951 | in_p = ! in_p, low = 0, high = arg1; | |
3952 | break; | |
0dbd1c74 | 3953 | default: |
fdada98f | 3954 | gcc_unreachable (); |
12ec0a8a | 3955 | } |
6f725368 | 3956 | |
c317c285 | 3957 | /* If this is an unsigned comparison, we also know that EXP is |
a9e29e86 | 3958 | greater than or equal to zero. We base the range tests we make |
3959 | on that fact, so we record it here so we can parse existing | |
7206da1b | 3960 | range tests. We test arg0_type since often the return type |
3961 | of, e.g. EQ_EXPR, is boolean. */ | |
3962 | if (TYPE_UNSIGNED (arg0_type) && (low == 0 || high == 0)) | |
12ec0a8a | 3963 | { |
5c9198bd | 3964 | if (! merge_ranges (&n_in_p, &n_low, &n_high, |
3965 | in_p, low, high, 1, | |
3c6185f1 | 3966 | build_int_cst (arg0_type, 0), |
a9e29e86 | 3967 | NULL_TREE)) |
12ec0a8a | 3968 | break; |
6f725368 | 3969 | |
12ec0a8a | 3970 | in_p = n_in_p, low = n_low, high = n_high; |
a9e29e86 | 3971 | |
751e10d1 | 3972 | /* If the high bound is missing, but we have a nonzero low |
e524954a | 3973 | bound, reverse the range so it goes from zero to the low bound |
3974 | minus 1. */ | |
3975 | if (high == 0 && low && ! integer_zerop (low)) | |
a9e29e86 | 3976 | { |
3977 | in_p = ! in_p; | |
3978 | high = range_binop (MINUS_EXPR, NULL_TREE, low, 0, | |
3979 | integer_one_node, 0); | |
3c6185f1 | 3980 | low = build_int_cst (arg0_type, 0); |
a9e29e86 | 3981 | } |
12ec0a8a | 3982 | } |
7206da1b | 3983 | |
3984 | exp = arg0; | |
12ec0a8a | 3985 | continue; |
3986 | ||
3987 | case NEGATE_EXPR: | |
3988 | /* (-x) IN [a,b] -> x in [-b, -a] */ | |
7206da1b | 3989 | n_low = range_binop (MINUS_EXPR, exp_type, |
3c6185f1 | 3990 | build_int_cst (exp_type, 0), |
b30e3dbc | 3991 | 0, high, 1); |
7206da1b | 3992 | n_high = range_binop (MINUS_EXPR, exp_type, |
3c6185f1 | 3993 | build_int_cst (exp_type, 0), |
b30e3dbc | 3994 | 0, low, 0); |
12ec0a8a | 3995 | low = n_low, high = n_high; |
3996 | exp = arg0; | |
3997 | continue; | |
3998 | ||
3999 | case BIT_NOT_EXPR: | |
4000 | /* ~ X -> -X - 1 */ | |
7206da1b | 4001 | exp = build2 (MINUS_EXPR, exp_type, negate_expr (arg0), |
3c6185f1 | 4002 | build_int_cst (exp_type, 1)); |
389dd41b | 4003 | SET_EXPR_LOCATION (exp, loc); |
12ec0a8a | 4004 | continue; |
4005 | ||
4006 | case PLUS_EXPR: case MINUS_EXPR: | |
4007 | if (TREE_CODE (arg1) != INTEGER_CST) | |
4008 | break; | |
4009 | ||
0a8b4135 | 4010 | /* If flag_wrapv and ARG0_TYPE is signed, then we cannot |
4011 | move a constant to the other side. */ | |
981eb798 | 4012 | if (!TYPE_UNSIGNED (arg0_type) |
4013 | && !TYPE_OVERFLOW_UNDEFINED (arg0_type)) | |
0a8b4135 | 4014 | break; |
4015 | ||
12ec0a8a | 4016 | /* If EXP is signed, any overflow in the computation is undefined, |
4017 | so we don't worry about it so long as our computations on | |
4018 | the bounds don't overflow. For unsigned, overflow is defined | |
4019 | and this is exactly the right thing. */ | |
4020 | n_low = range_binop (code == MINUS_EXPR ? PLUS_EXPR : MINUS_EXPR, | |
7206da1b | 4021 | arg0_type, low, 0, arg1, 0); |
12ec0a8a | 4022 | n_high = range_binop (code == MINUS_EXPR ? PLUS_EXPR : MINUS_EXPR, |
7206da1b | 4023 | arg0_type, high, 1, arg1, 0); |
12ec0a8a | 4024 | if ((n_low != 0 && TREE_OVERFLOW (n_low)) |
4025 | || (n_high != 0 && TREE_OVERFLOW (n_high))) | |
4026 | break; | |
4027 | ||
add6ee5e | 4028 | if (TYPE_OVERFLOW_UNDEFINED (arg0_type)) |
4029 | *strict_overflow_p = true; | |
4030 | ||
6b457c77 | 4031 | /* Check for an unsigned range which has wrapped around the maximum |
4032 | value thus making n_high < n_low, and normalize it. */ | |
98db800f | 4033 | if (n_low && n_high && tree_int_cst_lt (n_high, n_low)) |
6b457c77 | 4034 | { |
7206da1b | 4035 | low = range_binop (PLUS_EXPR, arg0_type, n_high, 0, |
a9e29e86 | 4036 | integer_one_node, 0); |
7206da1b | 4037 | high = range_binop (MINUS_EXPR, arg0_type, n_low, 0, |
a80d786b | 4038 | integer_one_node, 0); |
4039 | ||
4040 | /* If the range is of the form +/- [ x+1, x ], we won't | |
4041 | be able to normalize it. But then, it represents the | |
4042 | whole range or the empty set, so make it | |
4043 | +/- [ -, - ]. */ | |
4044 | if (tree_int_cst_equal (n_low, low) | |
4045 | && tree_int_cst_equal (n_high, high)) | |
4046 | low = high = 0; | |
4047 | else | |
4048 | in_p = ! in_p; | |
6b457c77 | 4049 | } |
98db800f | 4050 | else |
4051 | low = n_low, high = n_high; | |
7560c8de | 4052 | |
12ec0a8a | 4053 | exp = arg0; |
4054 | continue; | |
4055 | ||
72dd6141 | 4056 | CASE_CONVERT: case NON_LVALUE_EXPR: |
7206da1b | 4057 | if (TYPE_PRECISION (arg0_type) > TYPE_PRECISION (exp_type)) |
d6d65bd2 | 4058 | break; |
4059 | ||
7206da1b | 4060 | if (! INTEGRAL_TYPE_P (arg0_type) |
4061 | || (low != 0 && ! int_fits_type_p (low, arg0_type)) | |
4062 | || (high != 0 && ! int_fits_type_p (high, arg0_type))) | |
12ec0a8a | 4063 | break; |
4064 | ||
4cd44a59 | 4065 | n_low = low, n_high = high; |
12ec0a8a | 4066 | |
4cd44a59 | 4067 | if (n_low != 0) |
389dd41b | 4068 | n_low = fold_convert_loc (loc, arg0_type, n_low); |
4cd44a59 | 4069 | |
4070 | if (n_high != 0) | |
389dd41b | 4071 | n_high = fold_convert_loc (loc, arg0_type, n_high); |
4cd44a59 | 4072 | |
4cd44a59 | 4073 | |
7206da1b | 4074 | /* If we're converting arg0 from an unsigned type, to exp, |
2c763ed4 | 4075 | a signed type, we will be doing the comparison as unsigned. |
7206da1b | 4076 | The tests above have already verified that LOW and HIGH |
4077 | are both positive. | |
4078 | ||
4079 | So we have to ensure that we will handle large unsigned | |
4080 | values the same way that the current signed bounds treat | |
4081 | negative values. */ | |
4082 | ||
4083 | if (!TYPE_UNSIGNED (exp_type) && TYPE_UNSIGNED (arg0_type)) | |
4cd44a59 | 4084 | { |
f52483b5 | 4085 | tree high_positive; |
06f0b99c | 4086 | tree equiv_type; |
4087 | /* For fixed-point modes, we need to pass the saturating flag | |
4088 | as the 2nd parameter. */ | |
4089 | if (ALL_FIXED_POINT_MODE_P (TYPE_MODE (arg0_type))) | |
4090 | equiv_type = lang_hooks.types.type_for_mode | |
4091 | (TYPE_MODE (arg0_type), | |
4092 | TYPE_SATURATING (arg0_type)); | |
4093 | else | |
4094 | equiv_type = lang_hooks.types.type_for_mode | |
4095 | (TYPE_MODE (arg0_type), 1); | |
f52483b5 | 4096 | |
4097 | /* A range without an upper bound is, naturally, unbounded. | |
4098 | Since convert would have cropped a very large value, use | |
155b05dc | 4099 | the max value for the destination type. */ |
4100 | high_positive | |
4101 | = TYPE_MAX_VALUE (equiv_type) ? TYPE_MAX_VALUE (equiv_type) | |
7206da1b | 4102 | : TYPE_MAX_VALUE (arg0_type); |
f52483b5 | 4103 | |
7206da1b | 4104 | if (TYPE_PRECISION (exp_type) == TYPE_PRECISION (arg0_type)) |
389dd41b | 4105 | high_positive = fold_build2_loc (loc, RSHIFT_EXPR, arg0_type, |
4106 | fold_convert_loc (loc, arg0_type, | |
4107 | high_positive), | |
2455d3ef | 4108 | build_int_cst (arg0_type, 1)); |
cc049fa3 | 4109 | |
4cd44a59 | 4110 | /* If the low bound is specified, "and" the range with the |
4111 | range for which the original unsigned value will be | |
4112 | positive. */ | |
4113 | if (low != 0) | |
4114 | { | |
4115 | if (! merge_ranges (&n_in_p, &n_low, &n_high, | |
b30e3dbc | 4116 | 1, n_low, n_high, 1, |
389dd41b | 4117 | fold_convert_loc (loc, arg0_type, |
4118 | integer_zero_node), | |
4cd44a59 | 4119 | high_positive)) |
4120 | break; | |
4121 | ||
4122 | in_p = (n_in_p == in_p); | |
4123 | } | |
4124 | else | |
4125 | { | |
4126 | /* Otherwise, "or" the range with the range of the input | |
4127 | that will be interpreted as negative. */ | |
4128 | if (! merge_ranges (&n_in_p, &n_low, &n_high, | |
b30e3dbc | 4129 | 0, n_low, n_high, 1, |
389dd41b | 4130 | fold_convert_loc (loc, arg0_type, |
4131 | integer_zero_node), | |
4cd44a59 | 4132 | high_positive)) |
4133 | break; | |
4134 | ||
4135 | in_p = (in_p != n_in_p); | |
4136 | } | |
4137 | } | |
12ec0a8a | 4138 | |
4139 | exp = arg0; | |
4cd44a59 | 4140 | low = n_low, high = n_high; |
12ec0a8a | 4141 | continue; |
4cd44a59 | 4142 | |
4143 | default: | |
4144 | break; | |
6f725368 | 4145 | } |
12ec0a8a | 4146 | |
4147 | break; | |
6f725368 | 4148 | } |
12ec0a8a | 4149 | |
f83854c8 | 4150 | /* If EXP is a constant, we can evaluate whether this is true or false. */ |
4151 | if (TREE_CODE (exp) == INTEGER_CST) | |
4152 | { | |
4153 | in_p = in_p == (integer_onep (range_binop (GE_EXPR, integer_type_node, | |
4154 | exp, 0, low, 0)) | |
4155 | && integer_onep (range_binop (LE_EXPR, integer_type_node, | |
4156 | exp, 1, high, 1))); | |
4157 | low = high = 0; | |
4158 | exp = 0; | |
4159 | } | |
4160 | ||
12ec0a8a | 4161 | *pin_p = in_p, *plow = low, *phigh = high; |
4162 | return exp; | |
4163 | } | |
4164 | \f | |
4165 | /* Given a range, LOW, HIGH, and IN_P, an expression, EXP, and a result | |
4166 | type, TYPE, return an expression to test if EXP is in (or out of, depending | |
3b3a787a | 4167 | on IN_P) the range. Return 0 if the test couldn't be created. */ |
12ec0a8a | 4168 | |
9c20c4fc | 4169 | tree |
389dd41b | 4170 | build_range_check (location_t loc, tree type, tree exp, int in_p, |
4171 | tree low, tree high) | |
12ec0a8a | 4172 | { |
f2143b56 | 4173 | tree etype = TREE_TYPE (exp), value; |
12ec0a8a | 4174 | |
d067185e | 4175 | #ifdef HAVE_canonicalize_funcptr_for_compare |
4176 | /* Disable this optimization for function pointer expressions | |
4177 | on targets that require function pointer canonicalization. */ | |
4178 | if (HAVE_canonicalize_funcptr_for_compare | |
4179 | && TREE_CODE (etype) == POINTER_TYPE | |
4180 | && TREE_CODE (TREE_TYPE (etype)) == FUNCTION_TYPE) | |
4181 | return NULL_TREE; | |
4182 | #endif | |
4183 | ||
3b3a787a | 4184 | if (! in_p) |
4185 | { | |
389dd41b | 4186 | value = build_range_check (loc, type, exp, 1, low, high); |
3b3a787a | 4187 | if (value != 0) |
389dd41b | 4188 | return invert_truthvalue_loc (loc, value); |
3b3a787a | 4189 | |
4190 | return 0; | |
4191 | } | |
12ec0a8a | 4192 | |
843dd7a3 | 4193 | if (low == 0 && high == 0) |
3c6185f1 | 4194 | return build_int_cst (type, 1); |
12ec0a8a | 4195 | |
843dd7a3 | 4196 | if (low == 0) |
389dd41b | 4197 | return fold_build2_loc (loc, LE_EXPR, type, exp, |
4198 | fold_convert_loc (loc, etype, high)); | |
12ec0a8a | 4199 | |
843dd7a3 | 4200 | if (high == 0) |
389dd41b | 4201 | return fold_build2_loc (loc, GE_EXPR, type, exp, |
4202 | fold_convert_loc (loc, etype, low)); | |
12ec0a8a | 4203 | |
843dd7a3 | 4204 | if (operand_equal_p (low, high, 0)) |
389dd41b | 4205 | return fold_build2_loc (loc, EQ_EXPR, type, exp, |
4206 | fold_convert_loc (loc, etype, low)); | |
12ec0a8a | 4207 | |
843dd7a3 | 4208 | if (integer_zerop (low)) |
6f725368 | 4209 | { |
78a8ed03 | 4210 | if (! TYPE_UNSIGNED (etype)) |
d3371fcd | 4211 | { |
71eea85c | 4212 | etype = unsigned_type_for (etype); |
389dd41b | 4213 | high = fold_convert_loc (loc, etype, high); |
4214 | exp = fold_convert_loc (loc, etype, exp); | |
d3371fcd | 4215 | } |
389dd41b | 4216 | return build_range_check (loc, type, exp, 1, 0, high); |
12ec0a8a | 4217 | } |
6f725368 | 4218 | |
843dd7a3 | 4219 | /* Optimize (c>=1) && (c<=127) into (signed char)c > 0. */ |
4220 | if (integer_onep (low) && TREE_CODE (high) == INTEGER_CST) | |
4221 | { | |
4222 | unsigned HOST_WIDE_INT lo; | |
4223 | HOST_WIDE_INT hi; | |
4224 | int prec; | |
4225 | ||
4226 | prec = TYPE_PRECISION (etype); | |
4227 | if (prec <= HOST_BITS_PER_WIDE_INT) | |
d3371fcd | 4228 | { |
4229 | hi = 0; | |
4230 | lo = ((unsigned HOST_WIDE_INT) 1 << (prec - 1)) - 1; | |
4231 | } | |
843dd7a3 | 4232 | else |
d3371fcd | 4233 | { |
4234 | hi = ((HOST_WIDE_INT) 1 << (prec - HOST_BITS_PER_WIDE_INT - 1)) - 1; | |
4235 | lo = (unsigned HOST_WIDE_INT) -1; | |
4236 | } | |
843dd7a3 | 4237 | |
4238 | if (TREE_INT_CST_HIGH (high) == hi && TREE_INT_CST_LOW (high) == lo) | |
d3371fcd | 4239 | { |
78a8ed03 | 4240 | if (TYPE_UNSIGNED (etype)) |
d3371fcd | 4241 | { |
17c3cb97 | 4242 | tree signed_etype = signed_type_for (etype); |
4243 | if (TYPE_PRECISION (signed_etype) != TYPE_PRECISION (etype)) | |
4244 | etype | |
4245 | = build_nonstandard_integer_type (TYPE_PRECISION (etype), 0); | |
4246 | else | |
4247 | etype = signed_etype; | |
389dd41b | 4248 | exp = fold_convert_loc (loc, etype, exp); |
d3371fcd | 4249 | } |
389dd41b | 4250 | return fold_build2_loc (loc, GT_EXPR, type, exp, |
3c6185f1 | 4251 | build_int_cst (etype, 0)); |
d3371fcd | 4252 | } |
843dd7a3 | 4253 | } |
4254 | ||
66108e20 | 4255 | /* Optimize (c>=low) && (c<=high) into (c-low>=0) && (c-low<=high-low). |
a9538d68 | 4256 | This requires wrap-around arithmetics for the type of the expression. |
4257 | First make sure that arithmetics in this type is valid, then make sure | |
4258 | that it wraps around. */ | |
4259 | if (TREE_CODE (etype) == ENUMERAL_TYPE || TREE_CODE (etype) == BOOLEAN_TYPE) | |
4260 | etype = lang_hooks.types.type_for_size (TYPE_PRECISION (etype), | |
4261 | TYPE_UNSIGNED (etype)); | |
66108e20 | 4262 | |
a9538d68 | 4263 | if (TREE_CODE (etype) == INTEGER_TYPE && !TYPE_OVERFLOW_WRAPS (etype)) |
3b3a787a | 4264 | { |
4265 | tree utype, minv, maxv; | |
4266 | ||
4267 | /* Check if (unsigned) INT_MAX + 1 == (unsigned) INT_MIN | |
4268 | for the type in question, as we rely on this here. */ | |
71eea85c | 4269 | utype = unsigned_type_for (etype); |
389dd41b | 4270 | maxv = fold_convert_loc (loc, utype, TYPE_MAX_VALUE (etype)); |
66108e20 | 4271 | maxv = range_binop (PLUS_EXPR, NULL_TREE, maxv, 1, |
4272 | integer_one_node, 1); | |
389dd41b | 4273 | minv = fold_convert_loc (loc, utype, TYPE_MIN_VALUE (etype)); |
66108e20 | 4274 | |
4275 | if (integer_zerop (range_binop (NE_EXPR, integer_type_node, | |
4276 | minv, 1, maxv, 1))) | |
4277 | etype = utype; | |
4278 | else | |
4279 | return 0; | |
3b3a787a | 4280 | } |
4281 | ||
389dd41b | 4282 | high = fold_convert_loc (loc, etype, high); |
4283 | low = fold_convert_loc (loc, etype, low); | |
4284 | exp = fold_convert_loc (loc, etype, exp); | |
3f7c18bc | 4285 | |
66108e20 | 4286 | value = const_binop (MINUS_EXPR, high, low, 0); |
4287 | ||
0de36bdb | 4288 | |
4289 | if (POINTER_TYPE_P (etype)) | |
4290 | { | |
4291 | if (value != 0 && !TREE_OVERFLOW (value)) | |
4292 | { | |
389dd41b | 4293 | low = fold_convert_loc (loc, sizetype, low); |
4294 | low = fold_build1_loc (loc, NEGATE_EXPR, sizetype, low); | |
4295 | return build_range_check (loc, type, | |
4296 | fold_build2_loc (loc, POINTER_PLUS_EXPR, | |
4297 | etype, exp, low), | |
0de36bdb | 4298 | 1, build_int_cst (etype, 0), value); |
4299 | } | |
4300 | return 0; | |
4301 | } | |
4302 | ||
66108e20 | 4303 | if (value != 0 && !TREE_OVERFLOW (value)) |
389dd41b | 4304 | return build_range_check (loc, type, |
4305 | fold_build2_loc (loc, MINUS_EXPR, etype, exp, low), | |
66108e20 | 4306 | 1, build_int_cst (etype, 0), value); |
843dd7a3 | 4307 | |
4308 | return 0; | |
12ec0a8a | 4309 | } |
4310 | \f | |
1557b0a0 | 4311 | /* Return the predecessor of VAL in its type, handling the infinite case. */ |
4312 | ||
4313 | static tree | |
4314 | range_predecessor (tree val) | |
4315 | { | |
4316 | tree type = TREE_TYPE (val); | |
4317 | ||
20efd591 | 4318 | if (INTEGRAL_TYPE_P (type) |
4319 | && operand_equal_p (val, TYPE_MIN_VALUE (type), 0)) | |
1557b0a0 | 4320 | return 0; |
4321 | else | |
4322 | return range_binop (MINUS_EXPR, NULL_TREE, val, 0, integer_one_node, 0); | |
4323 | } | |
4324 | ||
4325 | /* Return the successor of VAL in its type, handling the infinite case. */ | |
4326 | ||
4327 | static tree | |
4328 | range_successor (tree val) | |
4329 | { | |
4330 | tree type = TREE_TYPE (val); | |
4331 | ||
20efd591 | 4332 | if (INTEGRAL_TYPE_P (type) |
4333 | && operand_equal_p (val, TYPE_MAX_VALUE (type), 0)) | |
1557b0a0 | 4334 | return 0; |
4335 | else | |
4336 | return range_binop (PLUS_EXPR, NULL_TREE, val, 0, integer_one_node, 0); | |
4337 | } | |
4338 | ||
cc049fa3 | 4339 | /* Given two ranges, see if we can merge them into one. Return 1 if we |
12ec0a8a | 4340 | can, 0 if we can't. Set the output range into the specified parameters. */ |
6f725368 | 4341 | |
9c20c4fc | 4342 | bool |
dc81944a | 4343 | merge_ranges (int *pin_p, tree *plow, tree *phigh, int in0_p, tree low0, |
4344 | tree high0, int in1_p, tree low1, tree high1) | |
12ec0a8a | 4345 | { |
4346 | int no_overlap; | |
4347 | int subset; | |
4348 | int temp; | |
4349 | tree tem; | |
4350 | int in_p; | |
4351 | tree low, high; | |
4cd44a59 | 4352 | int lowequal = ((low0 == 0 && low1 == 0) |
4353 | || integer_onep (range_binop (EQ_EXPR, integer_type_node, | |
4354 | low0, 0, low1, 0))); | |
4355 | int highequal = ((high0 == 0 && high1 == 0) | |
4356 | || integer_onep (range_binop (EQ_EXPR, integer_type_node, | |
4357 | high0, 1, high1, 1))); | |
4358 | ||
4359 | /* Make range 0 be the range that starts first, or ends last if they | |
4360 | start at the same value. Swap them if it isn't. */ | |
cc049fa3 | 4361 | if (integer_onep (range_binop (GT_EXPR, integer_type_node, |
12ec0a8a | 4362 | low0, 0, low1, 0)) |
4cd44a59 | 4363 | || (lowequal |
12ec0a8a | 4364 | && integer_onep (range_binop (GT_EXPR, integer_type_node, |
4cd44a59 | 4365 | high1, 1, high0, 1)))) |
12ec0a8a | 4366 | { |
4367 | temp = in0_p, in0_p = in1_p, in1_p = temp; | |
4368 | tem = low0, low0 = low1, low1 = tem; | |
4369 | tem = high0, high0 = high1, high1 = tem; | |
4370 | } | |
6f725368 | 4371 | |
12ec0a8a | 4372 | /* Now flag two cases, whether the ranges are disjoint or whether the |
4373 | second range is totally subsumed in the first. Note that the tests | |
4374 | below are simplified by the ones above. */ | |
4375 | no_overlap = integer_onep (range_binop (LT_EXPR, integer_type_node, | |
4376 | high0, 1, low1, 0)); | |
718acf6d | 4377 | subset = integer_onep (range_binop (LE_EXPR, integer_type_node, |
12ec0a8a | 4378 | high1, 1, high0, 1)); |
4379 | ||
4380 | /* We now have four cases, depending on whether we are including or | |
4381 | excluding the two ranges. */ | |
4382 | if (in0_p && in1_p) | |
4383 | { | |
4384 | /* If they don't overlap, the result is false. If the second range | |
4385 | is a subset it is the result. Otherwise, the range is from the start | |
4386 | of the second to the end of the first. */ | |
4387 | if (no_overlap) | |
4388 | in_p = 0, low = high = 0; | |
4389 | else if (subset) | |
4390 | in_p = 1, low = low1, high = high1; | |
4391 | else | |
4392 | in_p = 1, low = low1, high = high0; | |
4393 | } | |
6f725368 | 4394 | |
12ec0a8a | 4395 | else if (in0_p && ! in1_p) |
4396 | { | |
4cd44a59 | 4397 | /* If they don't overlap, the result is the first range. If they are |
4398 | equal, the result is false. If the second range is a subset of the | |
4399 | first, and the ranges begin at the same place, we go from just after | |
66108e20 | 4400 | the end of the second range to the end of the first. If the second |
4cd44a59 | 4401 | range is not a subset of the first, or if it is a subset and both |
4402 | ranges end at the same place, the range starts at the start of the | |
4403 | first range and ends just before the second range. | |
4404 | Otherwise, we can't describe this as a single range. */ | |
12ec0a8a | 4405 | if (no_overlap) |
4406 | in_p = 1, low = low0, high = high0; | |
4cd44a59 | 4407 | else if (lowequal && highequal) |
08986c47 | 4408 | in_p = 0, low = high = 0; |
4cd44a59 | 4409 | else if (subset && lowequal) |
4410 | { | |
66108e20 | 4411 | low = range_successor (high1); |
4412 | high = high0; | |
bdc68add | 4413 | in_p = 1; |
4414 | if (low == 0) | |
4415 | { | |
4416 | /* We are in the weird situation where high0 > high1 but | |
4417 | high1 has no successor. Punt. */ | |
4418 | return 0; | |
4419 | } | |
4cd44a59 | 4420 | } |
4421 | else if (! subset || highequal) | |
12ec0a8a | 4422 | { |
66108e20 | 4423 | low = low0; |
4424 | high = range_predecessor (low1); | |
bdc68add | 4425 | in_p = 1; |
4426 | if (high == 0) | |
4427 | { | |
4428 | /* low0 < low1 but low1 has no predecessor. Punt. */ | |
4429 | return 0; | |
4430 | } | |
12ec0a8a | 4431 | } |
4cd44a59 | 4432 | else |
4433 | return 0; | |
12ec0a8a | 4434 | } |
6f725368 | 4435 | |
12ec0a8a | 4436 | else if (! in0_p && in1_p) |
4437 | { | |
4438 | /* If they don't overlap, the result is the second range. If the second | |
4439 | is a subset of the first, the result is false. Otherwise, | |
4440 | the range starts just after the first range and ends at the | |
4441 | end of the second. */ | |
4442 | if (no_overlap) | |
4443 | in_p = 1, low = low1, high = high1; | |
155b05dc | 4444 | else if (subset || highequal) |
12ec0a8a | 4445 | in_p = 0, low = high = 0; |
4446 | else | |
4447 | { | |
66108e20 | 4448 | low = range_successor (high0); |
4449 | high = high1; | |
bdc68add | 4450 | in_p = 1; |
4451 | if (low == 0) | |
4452 | { | |
4453 | /* high1 > high0 but high0 has no successor. Punt. */ | |
4454 | return 0; | |
4455 | } | |
6f725368 | 4456 | } |
4457 | } | |
4458 | ||
12ec0a8a | 4459 | else |
4460 | { | |
4461 | /* The case where we are excluding both ranges. Here the complex case | |
4462 | is if they don't overlap. In that case, the only time we have a | |
4463 | range is if they are adjacent. If the second is a subset of the | |
4464 | first, the result is the first. Otherwise, the range to exclude | |
4465 | starts at the beginning of the first range and ends at the end of the | |
4466 | second. */ | |
4467 | if (no_overlap) | |
4468 | { | |
4469 | if (integer_onep (range_binop (EQ_EXPR, integer_type_node, | |
66108e20 | 4470 | range_successor (high0), |
12ec0a8a | 4471 | 1, low1, 0))) |
4472 | in_p = 0, low = low0, high = high1; | |
4473 | else | |
3b3a787a | 4474 | { |
4475 | /* Canonicalize - [min, x] into - [-, x]. */ | |
4476 | if (low0 && TREE_CODE (low0) == INTEGER_CST) | |
4477 | switch (TREE_CODE (TREE_TYPE (low0))) | |
4478 | { | |
4479 | case ENUMERAL_TYPE: | |
4480 | if (TYPE_PRECISION (TREE_TYPE (low0)) | |
4481 | != GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (low0)))) | |
4482 | break; | |
4483 | /* FALLTHROUGH */ | |
4484 | case INTEGER_TYPE: | |
3b3a787a | 4485 | if (tree_int_cst_equal (low0, |
4486 | TYPE_MIN_VALUE (TREE_TYPE (low0)))) | |
4487 | low0 = 0; | |
4488 | break; | |
4489 | case POINTER_TYPE: | |
4490 | if (TYPE_UNSIGNED (TREE_TYPE (low0)) | |
4491 | && integer_zerop (low0)) | |
4492 | low0 = 0; | |
4493 | break; | |
4494 | default: | |
4495 | break; | |
4496 | } | |
4497 | ||
4498 | /* Canonicalize - [x, max] into - [x, -]. */ | |
4499 | if (high1 && TREE_CODE (high1) == INTEGER_CST) | |
4500 | switch (TREE_CODE (TREE_TYPE (high1))) | |
4501 | { | |
4502 | case ENUMERAL_TYPE: | |
4503 | if (TYPE_PRECISION (TREE_TYPE (high1)) | |
4504 | != GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (high1)))) | |
4505 | break; | |
4506 | /* FALLTHROUGH */ | |
4507 | case INTEGER_TYPE: | |
3b3a787a | 4508 | if (tree_int_cst_equal (high1, |
4509 | TYPE_MAX_VALUE (TREE_TYPE (high1)))) | |
4510 | high1 = 0; | |
4511 | break; | |
4512 | case POINTER_TYPE: | |
4513 | if (TYPE_UNSIGNED (TREE_TYPE (high1)) | |
4514 | && integer_zerop (range_binop (PLUS_EXPR, NULL_TREE, | |
4515 | high1, 1, | |
4516 | integer_one_node, 1))) | |
4517 | high1 = 0; | |
4518 | break; | |
4519 | default: | |
4520 | break; | |
4521 | } | |
4522 | ||
4523 | /* The ranges might be also adjacent between the maximum and | |
4524 | minimum values of the given type. For | |
4525 | - [{min,-}, x] and - [y, {max,-}] ranges where x + 1 < y | |
4526 | return + [x + 1, y - 1]. */ | |
4527 | if (low0 == 0 && high1 == 0) | |
4528 | { | |
1557b0a0 | 4529 | low = range_successor (high0); |
4530 | high = range_predecessor (low1); | |
3b3a787a | 4531 | if (low == 0 || high == 0) |
4532 | return 0; | |
4533 | ||
4534 | in_p = 1; | |
4535 | } | |
4536 | else | |
4537 | return 0; | |
4538 | } | |
12ec0a8a | 4539 | } |
4540 | else if (subset) | |
4541 | in_p = 0, low = low0, high = high0; | |
4542 | else | |
4543 | in_p = 0, low = low0, high = high1; | |
4544 | } | |
b29eae68 | 4545 | |
12ec0a8a | 4546 | *pin_p = in_p, *plow = low, *phigh = high; |
4547 | return 1; | |
4548 | } | |
0023616d | 4549 | \f |
4550 | ||
4551 | /* Subroutine of fold, looking inside expressions of the form | |
9b1fa4a0 | 4552 | A op B ? A : C, where ARG0, ARG1 and ARG2 are the three operands |
4553 | of the COND_EXPR. This function is being used also to optimize | |
4554 | A op B ? C : A, by reversing the comparison first. | |
0023616d | 4555 | |
4556 | Return a folded expression whose code is not a COND_EXPR | |
4557 | anymore, or NULL_TREE if no folding opportunity is found. */ | |
4558 | ||
4559 | static tree | |
389dd41b | 4560 | fold_cond_expr_with_comparison (location_t loc, tree type, |
4561 | tree arg0, tree arg1, tree arg2) | |
0023616d | 4562 | { |
4563 | enum tree_code comp_code = TREE_CODE (arg0); | |
4564 | tree arg00 = TREE_OPERAND (arg0, 0); | |
4565 | tree arg01 = TREE_OPERAND (arg0, 1); | |
9b1fa4a0 | 4566 | tree arg1_type = TREE_TYPE (arg1); |
0023616d | 4567 | tree tem; |
9b1fa4a0 | 4568 | |
4569 | STRIP_NOPS (arg1); | |
0023616d | 4570 | STRIP_NOPS (arg2); |
4571 | ||
4572 | /* If we have A op 0 ? A : -A, consider applying the following | |
4573 | transformations: | |
4574 | ||
4575 | A == 0? A : -A same as -A | |
4576 | A != 0? A : -A same as A | |
4577 | A >= 0? A : -A same as abs (A) | |
4578 | A > 0? A : -A same as abs (A) | |
4579 | A <= 0? A : -A same as -abs (A) | |
4580 | A < 0? A : -A same as -abs (A) | |
4581 | ||
4582 | None of these transformations work for modes with signed | |
4583 | zeros. If A is +/-0, the first two transformations will | |
4584 | change the sign of the result (from +0 to -0, or vice | |
4585 | versa). The last four will fix the sign of the result, | |
4586 | even though the original expressions could be positive or | |
4587 | negative, depending on the sign of A. | |
4588 | ||
4589 | Note that all these transformations are correct if A is | |
4590 | NaN, since the two alternatives (A and -A) are also NaNs. */ | |
01e93ec4 | 4591 | if (!HONOR_SIGNED_ZEROS (TYPE_MODE (type)) |
4592 | && (FLOAT_TYPE_P (TREE_TYPE (arg01)) | |
4593 | ? real_zerop (arg01) | |
4594 | : integer_zerop (arg01)) | |
38f916c2 | 4595 | && ((TREE_CODE (arg2) == NEGATE_EXPR |
4596 | && operand_equal_p (TREE_OPERAND (arg2, 0), arg1, 0)) | |
4597 | /* In the case that A is of the form X-Y, '-A' (arg2) may | |
4598 | have already been folded to Y-X, check for that. */ | |
4599 | || (TREE_CODE (arg1) == MINUS_EXPR | |
4600 | && TREE_CODE (arg2) == MINUS_EXPR | |
4601 | && operand_equal_p (TREE_OPERAND (arg1, 0), | |
4602 | TREE_OPERAND (arg2, 1), 0) | |
4603 | && operand_equal_p (TREE_OPERAND (arg1, 1), | |
4604 | TREE_OPERAND (arg2, 0), 0)))) | |
0023616d | 4605 | switch (comp_code) |
4606 | { | |
4607 | case EQ_EXPR: | |
fe9b47eb | 4608 | case UNEQ_EXPR: |
389dd41b | 4609 | tem = fold_convert_loc (loc, arg1_type, arg1); |
4610 | return pedantic_non_lvalue_loc (loc, | |
4611 | fold_convert_loc (loc, type, | |
4612 | negate_expr (tem))); | |
0023616d | 4613 | case NE_EXPR: |
fe9b47eb | 4614 | case LTGT_EXPR: |
389dd41b | 4615 | return pedantic_non_lvalue_loc (loc, fold_convert_loc (loc, type, arg1)); |
fe9b47eb | 4616 | case UNGE_EXPR: |
4617 | case UNGT_EXPR: | |
4618 | if (flag_trapping_math) | |
4619 | break; | |
4620 | /* Fall through. */ | |
0023616d | 4621 | case GE_EXPR: |
4622 | case GT_EXPR: | |
9b1fa4a0 | 4623 | if (TYPE_UNSIGNED (TREE_TYPE (arg1))) |
389dd41b | 4624 | arg1 = fold_convert_loc (loc, signed_type_for |
9b1fa4a0 | 4625 | (TREE_TYPE (arg1)), arg1); |
389dd41b | 4626 | tem = fold_build1_loc (loc, ABS_EXPR, TREE_TYPE (arg1), arg1); |
4627 | return pedantic_non_lvalue_loc (loc, fold_convert_loc (loc, type, tem)); | |
fe9b47eb | 4628 | case UNLE_EXPR: |
4629 | case UNLT_EXPR: | |
4630 | if (flag_trapping_math) | |
4631 | break; | |
0023616d | 4632 | case LE_EXPR: |
4633 | case LT_EXPR: | |
9b1fa4a0 | 4634 | if (TYPE_UNSIGNED (TREE_TYPE (arg1))) |
389dd41b | 4635 | arg1 = fold_convert_loc (loc, signed_type_for |
9b1fa4a0 | 4636 | (TREE_TYPE (arg1)), arg1); |
389dd41b | 4637 | tem = fold_build1_loc (loc, ABS_EXPR, TREE_TYPE (arg1), arg1); |
4638 | return negate_expr (fold_convert_loc (loc, type, tem)); | |
0023616d | 4639 | default: |
ce45a448 | 4640 | gcc_assert (TREE_CODE_CLASS (comp_code) == tcc_comparison); |
fe9b47eb | 4641 | break; |
0023616d | 4642 | } |
4643 | ||
4644 | /* A != 0 ? A : 0 is simply A, unless A is -0. Likewise | |
4645 | A == 0 ? A : 0 is always 0 unless A is -0. Note that | |
4646 | both transformations are correct when A is NaN: A != 0 | |
4647 | is then true, and A == 0 is false. */ | |
4648 | ||
01e93ec4 | 4649 | if (!HONOR_SIGNED_ZEROS (TYPE_MODE (type)) |
4650 | && integer_zerop (arg01) && integer_zerop (arg2)) | |
0023616d | 4651 | { |
4652 | if (comp_code == NE_EXPR) | |
389dd41b | 4653 | return pedantic_non_lvalue_loc (loc, fold_convert_loc (loc, type, arg1)); |
0023616d | 4654 | else if (comp_code == EQ_EXPR) |
3c6185f1 | 4655 | return build_int_cst (type, 0); |
0023616d | 4656 | } |
4657 | ||
4658 | /* Try some transformations of A op B ? A : B. | |
4659 | ||
4660 | A == B? A : B same as B | |
4661 | A != B? A : B same as A | |
4662 | A >= B? A : B same as max (A, B) | |
4663 | A > B? A : B same as max (B, A) | |
4664 | A <= B? A : B same as min (A, B) | |
4665 | A < B? A : B same as min (B, A) | |
4666 | ||
4667 | As above, these transformations don't work in the presence | |
4668 | of signed zeros. For example, if A and B are zeros of | |
4669 | opposite sign, the first two transformations will change | |
4670 | the sign of the result. In the last four, the original | |
4671 | expressions give different results for (A=+0, B=-0) and | |
4672 | (A=-0, B=+0), but the transformed expressions do not. | |
4673 | ||
4674 | The first two transformations are correct if either A or B | |
4675 | is a NaN. In the first transformation, the condition will | |
4676 | be false, and B will indeed be chosen. In the case of the | |
4677 | second transformation, the condition A != B will be true, | |
4678 | and A will be chosen. | |
4679 | ||
4680 | The conversions to max() and min() are not correct if B is | |
4681 | a number and A is not. The conditions in the original | |
4682 | expressions will be false, so all four give B. The min() | |
4683 | and max() versions would give a NaN instead. */ | |
01e93ec4 | 4684 | if (!HONOR_SIGNED_ZEROS (TYPE_MODE (type)) |
4685 | && operand_equal_for_comparison_p (arg01, arg2, arg00) | |
98fc7ffa | 4686 | /* Avoid these transformations if the COND_EXPR may be used |
4687 | as an lvalue in the C++ front-end. PR c++/19199. */ | |
4688 | && (in_gimple_form | |
d0911b8e | 4689 | || (strcmp (lang_hooks.name, "GNU C++") != 0 |
4690 | && strcmp (lang_hooks.name, "GNU Objective-C++") != 0) | |
98fc7ffa | 4691 | || ! maybe_lvalue_p (arg1) |
4692 | || ! maybe_lvalue_p (arg2))) | |
0023616d | 4693 | { |
4694 | tree comp_op0 = arg00; | |
4695 | tree comp_op1 = arg01; | |
4696 | tree comp_type = TREE_TYPE (comp_op0); | |
4697 | ||
4698 | /* Avoid adding NOP_EXPRs in case this is an lvalue. */ | |
4699 | if (TYPE_MAIN_VARIANT (comp_type) == TYPE_MAIN_VARIANT (type)) | |
4700 | { | |
4701 | comp_type = type; | |
9b1fa4a0 | 4702 | comp_op0 = arg1; |
0023616d | 4703 | comp_op1 = arg2; |
4704 | } | |
4705 | ||
4706 | switch (comp_code) | |
4707 | { | |
4708 | case EQ_EXPR: | |
389dd41b | 4709 | return pedantic_non_lvalue_loc (loc, fold_convert_loc (loc, type, arg2)); |
0023616d | 4710 | case NE_EXPR: |
389dd41b | 4711 | return pedantic_non_lvalue_loc (loc, fold_convert_loc (loc, type, arg1)); |
0023616d | 4712 | case LE_EXPR: |
4713 | case LT_EXPR: | |
fe9b47eb | 4714 | case UNLE_EXPR: |
4715 | case UNLT_EXPR: | |
0023616d | 4716 | /* In C++ a ?: expression can be an lvalue, so put the |
4717 | operand which will be used if they are equal first | |
4718 | so that we can convert this back to the | |
4719 | corresponding COND_EXPR. */ | |
9b1fa4a0 | 4720 | if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg1)))) |
5c9198bd | 4721 | { |
389dd41b | 4722 | comp_op0 = fold_convert_loc (loc, comp_type, comp_op0); |
4723 | comp_op1 = fold_convert_loc (loc, comp_type, comp_op1); | |
fe9b47eb | 4724 | tem = (comp_code == LE_EXPR || comp_code == UNLE_EXPR) |
389dd41b | 4725 | ? fold_build2_loc (loc, MIN_EXPR, comp_type, comp_op0, comp_op1) |
4726 | : fold_build2_loc (loc, MIN_EXPR, comp_type, | |
4727 | comp_op1, comp_op0); | |
4728 | return pedantic_non_lvalue_loc (loc, | |
4729 | fold_convert_loc (loc, type, tem)); | |
5c9198bd | 4730 | } |
0023616d | 4731 | break; |
4732 | case GE_EXPR: | |
4733 | case GT_EXPR: | |
fe9b47eb | 4734 | case UNGE_EXPR: |
4735 | case UNGT_EXPR: | |
9b1fa4a0 | 4736 | if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg1)))) |
5c9198bd | 4737 | { |
389dd41b | 4738 | comp_op0 = fold_convert_loc (loc, comp_type, comp_op0); |
4739 | comp_op1 = fold_convert_loc (loc, comp_type, comp_op1); | |
fe9b47eb | 4740 | tem = (comp_code == GE_EXPR || comp_code == UNGE_EXPR) |
389dd41b | 4741 | ? fold_build2_loc (loc, MAX_EXPR, comp_type, comp_op0, comp_op1) |
4742 | : fold_build2_loc (loc, MAX_EXPR, comp_type, | |
4743 | comp_op1, comp_op0); | |
4744 | return pedantic_non_lvalue_loc (loc, | |
4745 | fold_convert_loc (loc, type, tem)); | |
5c9198bd | 4746 | } |
0023616d | 4747 | break; |
fe9b47eb | 4748 | case UNEQ_EXPR: |
4749 | if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg1)))) | |
389dd41b | 4750 | return pedantic_non_lvalue_loc (loc, |
4751 | fold_convert_loc (loc, type, arg2)); | |
fe9b47eb | 4752 | break; |
4753 | case LTGT_EXPR: | |
4754 | if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg1)))) | |
389dd41b | 4755 | return pedantic_non_lvalue_loc (loc, |
4756 | fold_convert_loc (loc, type, arg1)); | |
fe9b47eb | 4757 | break; |
0023616d | 4758 | default: |
ce45a448 | 4759 | gcc_assert (TREE_CODE_CLASS (comp_code) == tcc_comparison); |
fe9b47eb | 4760 | break; |
0023616d | 4761 | } |
4762 | } | |
4763 | ||
4764 | /* If this is A op C1 ? A : C2 with C1 and C2 constant integers, | |
4765 | we might still be able to simplify this. For example, | |
4766 | if C1 is one less or one more than C2, this might have started | |
4767 | out as a MIN or MAX and been transformed by this function. | |
4768 | Only good for INTEGER_TYPEs, because we need TYPE_MAX_VALUE. */ | |
4769 | ||
4770 | if (INTEGRAL_TYPE_P (type) | |
4771 | && TREE_CODE (arg01) == INTEGER_CST | |
4772 | && TREE_CODE (arg2) == INTEGER_CST) | |
4773 | switch (comp_code) | |
4774 | { | |
4775 | case EQ_EXPR: | |
507d706c | 4776 | if (TREE_CODE (arg1) == INTEGER_CST) |
4777 | break; | |
0023616d | 4778 | /* We can replace A with C1 in this case. */ |
389dd41b | 4779 | arg1 = fold_convert_loc (loc, type, arg01); |
4780 | return fold_build3_loc (loc, COND_EXPR, type, arg0, arg1, arg2); | |
0023616d | 4781 | |
4782 | case LT_EXPR: | |
0962300c | 4783 | /* If C1 is C2 + 1, this is min(A, C2), but use ARG00's type for |
4784 | MIN_EXPR, to preserve the signedness of the comparison. */ | |
0023616d | 4785 | if (! operand_equal_p (arg2, TYPE_MAX_VALUE (type), |
4786 | OEP_ONLY_CONST) | |
4787 | && operand_equal_p (arg01, | |
4788 | const_binop (PLUS_EXPR, arg2, | |
2455d3ef | 4789 | build_int_cst (type, 1), 0), |
0023616d | 4790 | OEP_ONLY_CONST)) |
0962300c | 4791 | { |
389dd41b | 4792 | tem = fold_build2_loc (loc, MIN_EXPR, TREE_TYPE (arg00), arg00, |
4793 | fold_convert_loc (loc, TREE_TYPE (arg00), | |
4794 | arg2)); | |
48e1416a | 4795 | return pedantic_non_lvalue_loc (loc, |
389dd41b | 4796 | fold_convert_loc (loc, type, tem)); |
0962300c | 4797 | } |
0023616d | 4798 | break; |
4799 | ||
4800 | case LE_EXPR: | |
0962300c | 4801 | /* If C1 is C2 - 1, this is min(A, C2), with the same care |
4802 | as above. */ | |
0023616d | 4803 | if (! operand_equal_p (arg2, TYPE_MIN_VALUE (type), |
4804 | OEP_ONLY_CONST) | |
4805 | && operand_equal_p (arg01, | |
4806 | const_binop (MINUS_EXPR, arg2, | |
2455d3ef | 4807 | build_int_cst (type, 1), 0), |
0023616d | 4808 | OEP_ONLY_CONST)) |
0962300c | 4809 | { |
389dd41b | 4810 | tem = fold_build2_loc (loc, MIN_EXPR, TREE_TYPE (arg00), arg00, |
4811 | fold_convert_loc (loc, TREE_TYPE (arg00), | |
4812 | arg2)); | |
4813 | return pedantic_non_lvalue_loc (loc, | |
4814 | fold_convert_loc (loc, type, tem)); | |
0962300c | 4815 | } |
0023616d | 4816 | break; |
4817 | ||
4818 | case GT_EXPR: | |
00211027 | 4819 | /* If C1 is C2 - 1, this is max(A, C2), but use ARG00's type for |
4820 | MAX_EXPR, to preserve the signedness of the comparison. */ | |
0023616d | 4821 | if (! operand_equal_p (arg2, TYPE_MIN_VALUE (type), |
4822 | OEP_ONLY_CONST) | |
4823 | && operand_equal_p (arg01, | |
4824 | const_binop (MINUS_EXPR, arg2, | |
2455d3ef | 4825 | build_int_cst (type, 1), 0), |
0023616d | 4826 | OEP_ONLY_CONST)) |
0962300c | 4827 | { |
389dd41b | 4828 | tem = fold_build2_loc (loc, MAX_EXPR, TREE_TYPE (arg00), arg00, |
4829 | fold_convert_loc (loc, TREE_TYPE (arg00), | |
4830 | arg2)); | |
4831 | return pedantic_non_lvalue_loc (loc, fold_convert_loc (loc, type, tem)); | |
0962300c | 4832 | } |
0023616d | 4833 | break; |
4834 | ||
4835 | case GE_EXPR: | |
00211027 | 4836 | /* If C1 is C2 + 1, this is max(A, C2), with the same care as above. */ |
0023616d | 4837 | if (! operand_equal_p (arg2, TYPE_MAX_VALUE (type), |
4838 | OEP_ONLY_CONST) | |
4839 | && operand_equal_p (arg01, | |
4840 | const_binop (PLUS_EXPR, arg2, | |
2455d3ef | 4841 | build_int_cst (type, 1), 0), |
0023616d | 4842 | OEP_ONLY_CONST)) |
0962300c | 4843 | { |
389dd41b | 4844 | tem = fold_build2_loc (loc, MAX_EXPR, TREE_TYPE (arg00), arg00, |
4845 | fold_convert_loc (loc, TREE_TYPE (arg00), | |
4846 | arg2)); | |
4847 | return pedantic_non_lvalue_loc (loc, fold_convert_loc (loc, type, tem)); | |
0962300c | 4848 | } |
0023616d | 4849 | break; |
4850 | case NE_EXPR: | |
4851 | break; | |
4852 | default: | |
fdada98f | 4853 | gcc_unreachable (); |
0023616d | 4854 | } |
4855 | ||
4856 | return NULL_TREE; | |
4857 | } | |
4858 | ||
4859 | ||
12ec0a8a | 4860 | \f |
17529f98 | 4861 | #ifndef LOGICAL_OP_NON_SHORT_CIRCUIT |
4a9d7ef7 | 4862 | #define LOGICAL_OP_NON_SHORT_CIRCUIT \ |
ad4341e8 | 4863 | (BRANCH_COST (optimize_function_for_speed_p (cfun), \ |
4a9d7ef7 | 4864 | false) >= 2) |
cf451ad8 | 4865 | #endif |
4866 | ||
12ec0a8a | 4867 | /* EXP is some logical combination of boolean tests. See if we can |
4868 | merge it into some range test. Return the new tree if so. */ | |
6f725368 | 4869 | |
12ec0a8a | 4870 | static tree |
389dd41b | 4871 | fold_range_test (location_t loc, enum tree_code code, tree type, |
4872 | tree op0, tree op1) | |
12ec0a8a | 4873 | { |
2c17ebb2 | 4874 | int or_op = (code == TRUTH_ORIF_EXPR |
4875 | || code == TRUTH_OR_EXPR); | |
12ec0a8a | 4876 | int in0_p, in1_p, in_p; |
4877 | tree low0, low1, low, high0, high1, high; | |
add6ee5e | 4878 | bool strict_overflow_p = false; |
4879 | tree lhs = make_range (op0, &in0_p, &low0, &high0, &strict_overflow_p); | |
4880 | tree rhs = make_range (op1, &in1_p, &low1, &high1, &strict_overflow_p); | |
12ec0a8a | 4881 | tree tem; |
add6ee5e | 4882 | const char * const warnmsg = G_("assuming signed overflow does not occur " |
4883 | "when simplifying range test"); | |
6f725368 | 4884 | |
12ec0a8a | 4885 | /* If this is an OR operation, invert both sides; we will invert |
4886 | again at the end. */ | |
4887 | if (or_op) | |
4888 | in0_p = ! in0_p, in1_p = ! in1_p; | |
4889 | ||
4890 | /* If both expressions are the same, if we can merge the ranges, and we | |
f83854c8 | 4891 | can build the range test, return it or it inverted. If one of the |
4892 | ranges is always true or always false, consider it to be the same | |
4893 | expression as the other. */ | |
4894 | if ((lhs == 0 || rhs == 0 || operand_equal_p (lhs, rhs, 0)) | |
12ec0a8a | 4895 | && merge_ranges (&in_p, &low, &high, in0_p, low0, high0, |
4896 | in1_p, low1, high1) | |
389dd41b | 4897 | && 0 != (tem = (build_range_check (UNKNOWN_LOCATION, type, |
f83854c8 | 4898 | lhs != 0 ? lhs |
4899 | : rhs != 0 ? rhs : integer_zero_node, | |
12ec0a8a | 4900 | in_p, low, high)))) |
add6ee5e | 4901 | { |
4902 | if (strict_overflow_p) | |
4903 | fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_COMPARISON); | |
389dd41b | 4904 | return or_op ? invert_truthvalue_loc (loc, tem) : tem; |
add6ee5e | 4905 | } |
12ec0a8a | 4906 | |
4907 | /* On machines where the branch cost is expensive, if this is a | |
4908 | short-circuited branch and the underlying object on both sides | |
4909 | is the same, make a non-short-circuit operation. */ | |
17529f98 | 4910 | else if (LOGICAL_OP_NON_SHORT_CIRCUIT |
1fdbc76b | 4911 | && lhs != 0 && rhs != 0 |
2c17ebb2 | 4912 | && (code == TRUTH_ANDIF_EXPR |
4913 | || code == TRUTH_ORIF_EXPR) | |
12ec0a8a | 4914 | && operand_equal_p (lhs, rhs, 0)) |
6f725368 | 4915 | { |
90a73592 | 4916 | /* If simple enough, just rewrite. Otherwise, make a SAVE_EXPR |
9e042f31 | 4917 | unless we are at top level or LHS contains a PLACEHOLDER_EXPR, in |
4918 | which cases we can't do this. */ | |
12ec0a8a | 4919 | if (simple_operand_p (lhs)) |
389dd41b | 4920 | { |
4921 | tem = build2 (code == TRUTH_ANDIF_EXPR | |
4922 | ? TRUTH_AND_EXPR : TRUTH_OR_EXPR, | |
4923 | type, op0, op1); | |
4924 | SET_EXPR_LOCATION (tem, loc); | |
4925 | return tem; | |
4926 | } | |
90a73592 | 4927 | |
fa8b888f | 4928 | else if (lang_hooks.decls.global_bindings_p () == 0 |
ce3fb06e | 4929 | && ! CONTAINS_PLACEHOLDER_P (lhs)) |
12ec0a8a | 4930 | { |
4931 | tree common = save_expr (lhs); | |
4932 | ||
389dd41b | 4933 | if (0 != (lhs = build_range_check (loc, type, common, |
12ec0a8a | 4934 | or_op ? ! in0_p : in0_p, |
4935 | low0, high0)) | |
389dd41b | 4936 | && (0 != (rhs = build_range_check (loc, type, common, |
12ec0a8a | 4937 | or_op ? ! in1_p : in1_p, |
4938 | low1, high1)))) | |
add6ee5e | 4939 | { |
4940 | if (strict_overflow_p) | |
4941 | fold_overflow_warning (warnmsg, | |
4942 | WARN_STRICT_OVERFLOW_COMPARISON); | |
389dd41b | 4943 | tem = build2 (code == TRUTH_ANDIF_EXPR |
4944 | ? TRUTH_AND_EXPR : TRUTH_OR_EXPR, | |
4945 | type, lhs, rhs); | |
4946 | SET_EXPR_LOCATION (tem, loc); | |
4947 | return tem; | |
add6ee5e | 4948 | } |
12ec0a8a | 4949 | } |
6f725368 | 4950 | } |
831e3af4 | 4951 | |
831e3af4 | 4952 | return 0; |
6f725368 | 4953 | } |
4954 | \f | |
94f29e88 | 4955 | /* Subroutine for fold_truthop: C is an INTEGER_CST interpreted as a P |
b2dcfbf7 | 4956 | bit value. Arrange things so the extra bits will be set to zero if and |
2a6329ae | 4957 | only if C is signed-extended to its full width. If MASK is nonzero, |
4958 | it is an INTEGER_CST that should be AND'ed with the extra bits. */ | |
94f29e88 | 4959 | |
4960 | static tree | |
de1b648b | 4961 | unextend (tree c, int p, int unsignedp, tree mask) |
94f29e88 | 4962 | { |
4963 | tree type = TREE_TYPE (c); | |
4964 | int modesize = GET_MODE_BITSIZE (TYPE_MODE (type)); | |
4965 | tree temp; | |
4966 | ||
4967 | if (p == modesize || unsignedp) | |
4968 | return c; | |
4969 | ||
94f29e88 | 4970 | /* We work by getting just the sign bit into the low-order bit, then |
c3418f42 | 4971 | into the high-order bit, then sign-extend. We then XOR that value |
94f29e88 | 4972 | with C. */ |
4973 | temp = const_binop (RSHIFT_EXPR, c, size_int (p - 1), 0); | |
4974 | temp = const_binop (BIT_AND_EXPR, temp, size_int (1), 0); | |
dd5f6dae | 4975 | |
4976 | /* We must use a signed type in order to get an arithmetic right shift. | |
4977 | However, we must also avoid introducing accidental overflows, so that | |
cc049fa3 | 4978 | a subsequent call to integer_zerop will work. Hence we must |
dd5f6dae | 4979 | do the type conversion here. At this point, the constant is either |
4980 | zero or one, and the conversion to a signed type can never overflow. | |
4981 | We could get an overflow if this conversion is done anywhere else. */ | |
78a8ed03 | 4982 | if (TYPE_UNSIGNED (type)) |
11773141 | 4983 | temp = fold_convert (signed_type_for (type), temp); |
dd5f6dae | 4984 | |
94f29e88 | 4985 | temp = const_binop (LSHIFT_EXPR, temp, size_int (modesize - 1), 0); |
4986 | temp = const_binop (RSHIFT_EXPR, temp, size_int (modesize - p - 1), 0); | |
2a6329ae | 4987 | if (mask != 0) |
b30e3dbc | 4988 | temp = const_binop (BIT_AND_EXPR, temp, |
389dd41b | 4989 | fold_convert (TREE_TYPE (c), mask), |
4990 | 0); | |
dd5f6dae | 4991 | /* If necessary, convert the type back to match the type of C. */ |
78a8ed03 | 4992 | if (TYPE_UNSIGNED (type)) |
b30e3dbc | 4993 | temp = fold_convert (type, temp); |
2a6329ae | 4994 | |
389dd41b | 4995 | return fold_convert (type, |
4996 | const_binop (BIT_XOR_EXPR, c, temp, 0)); | |
94f29e88 | 4997 | } |
4998 | \f | |
e230978b | 4999 | /* For an expression that has the form |
5000 | (A && B) || ~B | |
5001 | or | |
5002 | (A || B) && ~B, | |
5003 | we can drop one of the inner expressions and simplify to | |
5004 | A || ~B | |
5005 | or | |
5006 | A && ~B | |
5007 | LOC is the location of the resulting expression. OP is the inner | |
5008 | logical operation; the left-hand side in the examples above, while CMPOP | |
5009 | is the right-hand side. RHS_ONLY is used to prevent us from accidentally | |
5010 | removing a condition that guards another, as in | |
5011 | (A != NULL && A->...) || A == NULL | |
5012 | which we must not transform. If RHS_ONLY is true, only eliminate the | |
5013 | right-most operand of the inner logical operation. */ | |
5014 | ||
5015 | static tree | |
5016 | merge_truthop_with_opposite_arm (location_t loc, tree op, tree cmpop, | |
5017 | bool rhs_only) | |
5018 | { | |
5019 | tree type = TREE_TYPE (cmpop); | |
5020 | enum tree_code code = TREE_CODE (cmpop); | |
5021 | enum tree_code truthop_code = TREE_CODE (op); | |
5022 | tree lhs = TREE_OPERAND (op, 0); | |
5023 | tree rhs = TREE_OPERAND (op, 1); | |
5024 | tree orig_lhs = lhs, orig_rhs = rhs; | |
5025 | enum tree_code rhs_code = TREE_CODE (rhs); | |
5026 | enum tree_code lhs_code = TREE_CODE (lhs); | |
5027 | enum tree_code inv_code; | |
5028 | ||
5029 | if (TREE_SIDE_EFFECTS (op) || TREE_SIDE_EFFECTS (cmpop)) | |
5030 | return NULL_TREE; | |
5031 | ||
5032 | if (TREE_CODE_CLASS (code) != tcc_comparison) | |
5033 | return NULL_TREE; | |
5034 | ||
5035 | if (rhs_code == truthop_code) | |
5036 | { | |
5037 | tree newrhs = merge_truthop_with_opposite_arm (loc, rhs, cmpop, rhs_only); | |
5038 | if (newrhs != NULL_TREE) | |
5039 | { | |
5040 | rhs = newrhs; | |
5041 | rhs_code = TREE_CODE (rhs); | |
5042 | } | |
5043 | } | |
5044 | if (lhs_code == truthop_code && !rhs_only) | |
5045 | { | |
5046 | tree newlhs = merge_truthop_with_opposite_arm (loc, lhs, cmpop, false); | |
5047 | if (newlhs != NULL_TREE) | |
5048 | { | |
5049 | lhs = newlhs; | |
5050 | lhs_code = TREE_CODE (lhs); | |
5051 | } | |
5052 | } | |
5053 | ||
5054 | inv_code = invert_tree_comparison (code, HONOR_NANS (TYPE_MODE (type))); | |
5055 | if (inv_code == rhs_code | |
5056 | && operand_equal_p (TREE_OPERAND (rhs, 0), TREE_OPERAND (cmpop, 0), 0) | |
5057 | && operand_equal_p (TREE_OPERAND (rhs, 1), TREE_OPERAND (cmpop, 1), 0)) | |
5058 | return lhs; | |
5059 | if (!rhs_only && inv_code == lhs_code | |
5060 | && operand_equal_p (TREE_OPERAND (lhs, 0), TREE_OPERAND (cmpop, 0), 0) | |
5061 | && operand_equal_p (TREE_OPERAND (lhs, 1), TREE_OPERAND (cmpop, 1), 0)) | |
5062 | return rhs; | |
5063 | if (rhs != orig_rhs || lhs != orig_lhs) | |
5064 | return fold_build2_loc (loc, truthop_code, TREE_TYPE (cmpop), | |
5065 | lhs, rhs); | |
5066 | return NULL_TREE; | |
5067 | } | |
5068 | ||
79109eec | 5069 | /* Find ways of folding logical expressions of LHS and RHS: |
5070 | Try to merge two comparisons to the same innermost item. | |
5071 | Look for range tests like "ch >= '0' && ch <= '9'". | |
5072 | Look for combinations of simple terms on machines with expensive branches | |
5073 | and evaluate the RHS unconditionally. | |
2bc77e10 | 5074 | |
5075 | For example, if we have p->a == 2 && p->b == 4 and we can make an | |
5076 | object large enough to span both A and B, we can do this with a comparison | |
5077 | against the object ANDed with the a mask. | |
5078 | ||
5079 | If we have p->a == q->a && p->b == q->b, we may be able to use bit masking | |
5080 | operations to do this with one comparison. | |
5081 | ||
5082 | We check for both normal comparisons and the BIT_AND_EXPRs made this by | |
5083 | function and the one above. | |
5084 | ||
5085 | CODE is the logical operation being done. It can be TRUTH_ANDIF_EXPR, | |
5086 | TRUTH_AND_EXPR, TRUTH_ORIF_EXPR, or TRUTH_OR_EXPR. | |
5087 | ||
5088 | TRUTH_TYPE is the type of the logical operand and LHS and RHS are its | |
5089 | two operands. | |
5090 | ||
5091 | We return the simplified tree or 0 if no optimization is possible. */ | |
5092 | ||
5093 | static tree | |
389dd41b | 5094 | fold_truthop (location_t loc, enum tree_code code, tree truth_type, |
5095 | tree lhs, tree rhs) | |
2bc77e10 | 5096 | { |
62af9abe | 5097 | /* If this is the "or" of two comparisons, we can do something if |
2bc77e10 | 5098 | the comparisons are NE_EXPR. If this is the "and", we can do something |
cc049fa3 | 5099 | if the comparisons are EQ_EXPR. I.e., |
de1b648b | 5100 | (a->b == 2 && a->c == 4) can become (a->new == NEW). |
2bc77e10 | 5101 | |
5102 | WANTED_CODE is this operation code. For single bit fields, we can | |
5103 | convert EQ_EXPR to NE_EXPR so we need not reject the "wrong" | |
5104 | comparison for one-bit fields. */ | |
5105 | ||
79109eec | 5106 | enum tree_code wanted_code; |
2bc77e10 | 5107 | enum tree_code lcode, rcode; |
79109eec | 5108 | tree ll_arg, lr_arg, rl_arg, rr_arg; |
2bc77e10 | 5109 | tree ll_inner, lr_inner, rl_inner, rr_inner; |
02e7a332 | 5110 | HOST_WIDE_INT ll_bitsize, ll_bitpos, lr_bitsize, lr_bitpos; |
5111 | HOST_WIDE_INT rl_bitsize, rl_bitpos, rr_bitsize, rr_bitpos; | |
2a64c730 | 5112 | HOST_WIDE_INT xll_bitpos, xlr_bitpos, xrl_bitpos, xrr_bitpos; |
5113 | HOST_WIDE_INT lnbitsize, lnbitpos, rnbitsize, rnbitpos; | |
2bc77e10 | 5114 | int ll_unsignedp, lr_unsignedp, rl_unsignedp, rr_unsignedp; |
5115 | enum machine_mode ll_mode, lr_mode, rl_mode, rr_mode; | |
2a64c730 | 5116 | enum machine_mode lnmode, rnmode; |
2bc77e10 | 5117 | tree ll_mask, lr_mask, rl_mask, rr_mask; |
2a6329ae | 5118 | tree ll_and_mask, lr_and_mask, rl_and_mask, rr_and_mask; |
79109eec | 5119 | tree l_const, r_const; |
2a64c730 | 5120 | tree lntype, rntype, result; |
5121 | HOST_WIDE_INT first_bit, end_bit; | |
79109eec | 5122 | int volatilep; |
40c3c1b3 | 5123 | tree orig_lhs = lhs, orig_rhs = rhs; |
5124 | enum tree_code orig_code = code; | |
2bc77e10 | 5125 | |
12ec0a8a | 5126 | /* Start by getting the comparison codes. Fail if anything is volatile. |
5127 | If one operand is a BIT_AND_EXPR with the constant one, treat it as if | |
5128 | it were surrounded with a NE_EXPR. */ | |
2bc77e10 | 5129 | |
12ec0a8a | 5130 | if (TREE_SIDE_EFFECTS (lhs) || TREE_SIDE_EFFECTS (rhs)) |
79109eec | 5131 | return 0; |
5132 | ||
2bc77e10 | 5133 | lcode = TREE_CODE (lhs); |
5134 | rcode = TREE_CODE (rhs); | |
6f725368 | 5135 | |
b5ab1edd | 5136 | if (lcode == BIT_AND_EXPR && integer_onep (TREE_OPERAND (lhs, 1))) |
fd96eeef | 5137 | { |
5c9198bd | 5138 | lhs = build2 (NE_EXPR, truth_type, lhs, |
3c6185f1 | 5139 | build_int_cst (TREE_TYPE (lhs), 0)); |
fd96eeef | 5140 | lcode = NE_EXPR; |
5141 | } | |
b5ab1edd | 5142 | |
5143 | if (rcode == BIT_AND_EXPR && integer_onep (TREE_OPERAND (rhs, 1))) | |
fd96eeef | 5144 | { |
5c9198bd | 5145 | rhs = build2 (NE_EXPR, truth_type, rhs, |
3c6185f1 | 5146 | build_int_cst (TREE_TYPE (rhs), 0)); |
fd96eeef | 5147 | rcode = NE_EXPR; |
5148 | } | |
b5ab1edd | 5149 | |
ce45a448 | 5150 | if (TREE_CODE_CLASS (lcode) != tcc_comparison |
5151 | || TREE_CODE_CLASS (rcode) != tcc_comparison) | |
6f725368 | 5152 | return 0; |
5153 | ||
79109eec | 5154 | ll_arg = TREE_OPERAND (lhs, 0); |
5155 | lr_arg = TREE_OPERAND (lhs, 1); | |
5156 | rl_arg = TREE_OPERAND (rhs, 0); | |
5157 | rr_arg = TREE_OPERAND (rhs, 1); | |
cc049fa3 | 5158 | |
7835f163 | 5159 | /* Simplify (x<y) && (x==y) into (x<=y) and related optimizations. */ |
5160 | if (simple_operand_p (ll_arg) | |
318a728f | 5161 | && simple_operand_p (lr_arg)) |
7835f163 | 5162 | { |
318a728f | 5163 | tree result; |
7835f163 | 5164 | if (operand_equal_p (ll_arg, rl_arg, 0) |
5165 | && operand_equal_p (lr_arg, rr_arg, 0)) | |
318a728f | 5166 | { |
389dd41b | 5167 | result = combine_comparisons (loc, code, lcode, rcode, |
318a728f | 5168 | truth_type, ll_arg, lr_arg); |
5169 | if (result) | |
5170 | return result; | |
5171 | } | |
7835f163 | 5172 | else if (operand_equal_p (ll_arg, rr_arg, 0) |
5173 | && operand_equal_p (lr_arg, rl_arg, 0)) | |
318a728f | 5174 | { |
389dd41b | 5175 | result = combine_comparisons (loc, code, lcode, |
318a728f | 5176 | swap_tree_comparison (rcode), |
5177 | truth_type, ll_arg, lr_arg); | |
5178 | if (result) | |
5179 | return result; | |
5180 | } | |
7835f163 | 5181 | } |
5182 | ||
318a728f | 5183 | code = ((code == TRUTH_AND_EXPR || code == TRUTH_ANDIF_EXPR) |
5184 | ? TRUTH_AND_EXPR : TRUTH_OR_EXPR); | |
5185 | ||
7735dddb | 5186 | /* If the RHS can be evaluated unconditionally and its operands are |
79109eec | 5187 | simple, it wins to evaluate the RHS unconditionally on machines |
5188 | with expensive branches. In this case, this isn't a comparison | |
35212e61 | 5189 | that can be merged. Avoid doing this if the RHS is a floating-point |
5190 | comparison since those can trap. */ | |
79109eec | 5191 | |
ad4341e8 | 5192 | if (BRANCH_COST (optimize_function_for_speed_p (cfun), |
4a9d7ef7 | 5193 | false) >= 2 |
35212e61 | 5194 | && ! FLOAT_TYPE_P (TREE_TYPE (rl_arg)) |
79109eec | 5195 | && simple_operand_p (rl_arg) |
7735dddb | 5196 | && simple_operand_p (rr_arg)) |
0425437e | 5197 | { |
5198 | /* Convert (a != 0) || (b != 0) into (a | b) != 0. */ | |
5199 | if (code == TRUTH_OR_EXPR | |
5200 | && lcode == NE_EXPR && integer_zerop (lr_arg) | |
5201 | && rcode == NE_EXPR && integer_zerop (rr_arg) | |
d159b72d | 5202 | && TREE_TYPE (ll_arg) == TREE_TYPE (rl_arg) |
5203 | && INTEGRAL_TYPE_P (TREE_TYPE (ll_arg))) | |
389dd41b | 5204 | { |
5205 | result = build2 (NE_EXPR, truth_type, | |
5206 | build2 (BIT_IOR_EXPR, TREE_TYPE (ll_arg), | |
5207 | ll_arg, rl_arg), | |
5208 | build_int_cst (TREE_TYPE (ll_arg), 0)); | |
5209 | goto fold_truthop_exit; | |
5210 | } | |
0425437e | 5211 | |
5212 | /* Convert (a == 0) && (b == 0) into (a | b) == 0. */ | |
5213 | if (code == TRUTH_AND_EXPR | |
5214 | && lcode == EQ_EXPR && integer_zerop (lr_arg) | |
5215 | && rcode == EQ_EXPR && integer_zerop (rr_arg) | |
d159b72d | 5216 | && TREE_TYPE (ll_arg) == TREE_TYPE (rl_arg) |
5217 | && INTEGRAL_TYPE_P (TREE_TYPE (ll_arg))) | |
389dd41b | 5218 | { |
5219 | result = build2 (EQ_EXPR, truth_type, | |
5220 | build2 (BIT_IOR_EXPR, TREE_TYPE (ll_arg), | |
5221 | ll_arg, rl_arg), | |
5222 | build_int_cst (TREE_TYPE (ll_arg), 0)); | |
5223 | goto fold_truthop_exit; | |
5224 | } | |
0425437e | 5225 | |
17529f98 | 5226 | if (LOGICAL_OP_NON_SHORT_CIRCUIT) |
40c3c1b3 | 5227 | { |
5228 | if (code != orig_code || lhs != orig_lhs || rhs != orig_rhs) | |
389dd41b | 5229 | { |
5230 | result = build2 (code, truth_type, lhs, rhs); | |
5231 | goto fold_truthop_exit; | |
5232 | } | |
40c3c1b3 | 5233 | return NULL_TREE; |
5234 | } | |
0425437e | 5235 | } |
79109eec | 5236 | |
6f725368 | 5237 | /* See if the comparisons can be merged. Then get all the parameters for |
5238 | each side. */ | |
5239 | ||
2bc77e10 | 5240 | if ((lcode != EQ_EXPR && lcode != NE_EXPR) |
6f725368 | 5241 | || (rcode != EQ_EXPR && rcode != NE_EXPR)) |
2bc77e10 | 5242 | return 0; |
5243 | ||
79109eec | 5244 | volatilep = 0; |
389dd41b | 5245 | ll_inner = decode_field_reference (loc, ll_arg, |
2bc77e10 | 5246 | &ll_bitsize, &ll_bitpos, &ll_mode, |
2a6329ae | 5247 | &ll_unsignedp, &volatilep, &ll_mask, |
5248 | &ll_and_mask); | |
389dd41b | 5249 | lr_inner = decode_field_reference (loc, lr_arg, |
2bc77e10 | 5250 | &lr_bitsize, &lr_bitpos, &lr_mode, |
2a6329ae | 5251 | &lr_unsignedp, &volatilep, &lr_mask, |
5252 | &lr_and_mask); | |
389dd41b | 5253 | rl_inner = decode_field_reference (loc, rl_arg, |
2bc77e10 | 5254 | &rl_bitsize, &rl_bitpos, &rl_mode, |
2a6329ae | 5255 | &rl_unsignedp, &volatilep, &rl_mask, |
5256 | &rl_and_mask); | |
389dd41b | 5257 | rr_inner = decode_field_reference (loc, rr_arg, |
2bc77e10 | 5258 | &rr_bitsize, &rr_bitpos, &rr_mode, |
2a6329ae | 5259 | &rr_unsignedp, &volatilep, &rr_mask, |
5260 | &rr_and_mask); | |
2bc77e10 | 5261 | |
5262 | /* It must be true that the inner operation on the lhs of each | |
5263 | comparison must be the same if we are to be able to do anything. | |
5264 | Then see if we have constants. If not, the same must be true for | |
5265 | the rhs's. */ | |
5266 | if (volatilep || ll_inner == 0 || rl_inner == 0 | |
5267 | || ! operand_equal_p (ll_inner, rl_inner, 0)) | |
5268 | return 0; | |
5269 | ||
79109eec | 5270 | if (TREE_CODE (lr_arg) == INTEGER_CST |
5271 | && TREE_CODE (rr_arg) == INTEGER_CST) | |
5272 | l_const = lr_arg, r_const = rr_arg; | |
2bc77e10 | 5273 | else if (lr_inner == 0 || rr_inner == 0 |
5274 | || ! operand_equal_p (lr_inner, rr_inner, 0)) | |
5275 | return 0; | |
79109eec | 5276 | else |
5277 | l_const = r_const = 0; | |
2bc77e10 | 5278 | |
5279 | /* If either comparison code is not correct for our logical operation, | |
5280 | fail. However, we can convert a one-bit comparison against zero into | |
5281 | the opposite comparison against that bit being set in the field. */ | |
79109eec | 5282 | |
76e4a18b | 5283 | wanted_code = (code == TRUTH_AND_EXPR ? EQ_EXPR : NE_EXPR); |
2bc77e10 | 5284 | if (lcode != wanted_code) |
5285 | { | |
5286 | if (l_const && integer_zerop (l_const) && integer_pow2p (ll_mask)) | |
c6107ab0 | 5287 | { |
28bb328d | 5288 | /* Make the left operand unsigned, since we are only interested |
5289 | in the value of one bit. Otherwise we are doing the wrong | |
5290 | thing below. */ | |
5291 | ll_unsignedp = 1; | |
68ae709d | 5292 | l_const = ll_mask; |
c6107ab0 | 5293 | } |
2bc77e10 | 5294 | else |
5295 | return 0; | |
5296 | } | |
5297 | ||
68ae709d | 5298 | /* This is analogous to the code for l_const above. */ |
2bc77e10 | 5299 | if (rcode != wanted_code) |
5300 | { | |
5301 | if (r_const && integer_zerop (r_const) && integer_pow2p (rl_mask)) | |
c6107ab0 | 5302 | { |
28bb328d | 5303 | rl_unsignedp = 1; |
68ae709d | 5304 | r_const = rl_mask; |
c6107ab0 | 5305 | } |
2bc77e10 | 5306 | else |
5307 | return 0; | |
5308 | } | |
5309 | ||
5310 | /* See if we can find a mode that contains both fields being compared on | |
5311 | the left. If we can't, fail. Otherwise, update all constants and masks | |
5312 | to be relative to a field of that size. */ | |
5313 | first_bit = MIN (ll_bitpos, rl_bitpos); | |
5314 | end_bit = MAX (ll_bitpos + ll_bitsize, rl_bitpos + rl_bitsize); | |
5315 | lnmode = get_best_mode (end_bit - first_bit, first_bit, | |
5316 | TYPE_ALIGN (TREE_TYPE (ll_inner)), word_mode, | |
5317 | volatilep); | |
5318 | if (lnmode == VOIDmode) | |
5319 | return 0; | |
5320 | ||
5321 | lnbitsize = GET_MODE_BITSIZE (lnmode); | |
5322 | lnbitpos = first_bit & ~ (lnbitsize - 1); | |
fa8b888f | 5323 | lntype = lang_hooks.types.type_for_size (lnbitsize, 1); |
2bc77e10 | 5324 | xll_bitpos = ll_bitpos - lnbitpos, xrl_bitpos = rl_bitpos - lnbitpos; |
5325 | ||
51356f86 | 5326 | if (BYTES_BIG_ENDIAN) |
5327 | { | |
5328 | xll_bitpos = lnbitsize - xll_bitpos - ll_bitsize; | |
5329 | xrl_bitpos = lnbitsize - xrl_bitpos - rl_bitsize; | |
5330 | } | |
2bc77e10 | 5331 | |
389dd41b | 5332 | ll_mask = const_binop (LSHIFT_EXPR, fold_convert_loc (loc, lntype, ll_mask), |
5485823f | 5333 | size_int (xll_bitpos), 0); |
389dd41b | 5334 | rl_mask = const_binop (LSHIFT_EXPR, fold_convert_loc (loc, lntype, rl_mask), |
5485823f | 5335 | size_int (xrl_bitpos), 0); |
2bc77e10 | 5336 | |
2bc77e10 | 5337 | if (l_const) |
5338 | { | |
389dd41b | 5339 | l_const = fold_convert_loc (loc, lntype, l_const); |
cc049fa3 | 5340 | l_const = unextend (l_const, ll_bitsize, ll_unsignedp, ll_and_mask); |
94f29e88 | 5341 | l_const = const_binop (LSHIFT_EXPR, l_const, size_int (xll_bitpos), 0); |
5342 | if (! integer_zerop (const_binop (BIT_AND_EXPR, l_const, | |
389dd41b | 5343 | fold_build1_loc (loc, BIT_NOT_EXPR, |
7ab7fd4f | 5344 | lntype, ll_mask), |
94f29e88 | 5345 | 0))) |
5346 | { | |
c3ceba8e | 5347 | warning (0, "comparison is always %d", wanted_code == NE_EXPR); |
cc049fa3 | 5348 | |
20783f07 | 5349 | return constant_boolean_node (wanted_code == NE_EXPR, truth_type); |
94f29e88 | 5350 | } |
2bc77e10 | 5351 | } |
5352 | if (r_const) | |
5353 | { | |
389dd41b | 5354 | r_const = fold_convert_loc (loc, lntype, r_const); |
2a6329ae | 5355 | r_const = unextend (r_const, rl_bitsize, rl_unsignedp, rl_and_mask); |
94f29e88 | 5356 | r_const = const_binop (LSHIFT_EXPR, r_const, size_int (xrl_bitpos), 0); |
5357 | if (! integer_zerop (const_binop (BIT_AND_EXPR, r_const, | |
389dd41b | 5358 | fold_build1_loc (loc, BIT_NOT_EXPR, |
7ab7fd4f | 5359 | lntype, rl_mask), |
94f29e88 | 5360 | 0))) |
5361 | { | |
c3ceba8e | 5362 | warning (0, "comparison is always %d", wanted_code == NE_EXPR); |
be2828ce | 5363 | |
20783f07 | 5364 | return constant_boolean_node (wanted_code == NE_EXPR, truth_type); |
94f29e88 | 5365 | } |
2bc77e10 | 5366 | } |
5367 | ||
2a64c730 | 5368 | /* If the right sides are not constant, do the same for it. Also, |
5369 | disallow this optimization if a size or signedness mismatch occurs | |
5370 | between the left and right sides. */ | |
5371 | if (l_const == 0) | |
5372 | { | |
5373 | if (ll_bitsize != lr_bitsize || rl_bitsize != rr_bitsize | |
5374 | || ll_unsignedp != lr_unsignedp || rl_unsignedp != rr_unsignedp | |
5375 | /* Make sure the two fields on the right | |
5376 | correspond to the left without being swapped. */ | |
5377 | || ll_bitpos - rl_bitpos != lr_bitpos - rr_bitpos) | |
5378 | return 0; | |
5379 | ||
5380 | first_bit = MIN (lr_bitpos, rr_bitpos); | |
5381 | end_bit = MAX (lr_bitpos + lr_bitsize, rr_bitpos + rr_bitsize); | |
5382 | rnmode = get_best_mode (end_bit - first_bit, first_bit, | |
5383 | TYPE_ALIGN (TREE_TYPE (lr_inner)), word_mode, | |
5384 | volatilep); | |
5385 | if (rnmode == VOIDmode) | |
5386 | return 0; | |
5387 | ||
5388 | rnbitsize = GET_MODE_BITSIZE (rnmode); | |
5389 | rnbitpos = first_bit & ~ (rnbitsize - 1); | |
5390 | rntype = lang_hooks.types.type_for_size (rnbitsize, 1); | |
5391 | xlr_bitpos = lr_bitpos - rnbitpos, xrr_bitpos = rr_bitpos - rnbitpos; | |
5392 | ||
5393 | if (BYTES_BIG_ENDIAN) | |
5394 | { | |
5395 | xlr_bitpos = rnbitsize - xlr_bitpos - lr_bitsize; | |
5396 | xrr_bitpos = rnbitsize - xrr_bitpos - rr_bitsize; | |
5397 | } | |
5398 | ||
389dd41b | 5399 | lr_mask = const_binop (LSHIFT_EXPR, fold_convert_loc (loc, |
5400 | rntype, lr_mask), | |
2a64c730 | 5401 | size_int (xlr_bitpos), 0); |
389dd41b | 5402 | rr_mask = const_binop (LSHIFT_EXPR, fold_convert_loc (loc, |
5403 | rntype, rr_mask), | |
2a64c730 | 5404 | size_int (xrr_bitpos), 0); |
5405 | ||
5406 | /* Make a mask that corresponds to both fields being compared. | |
5407 | Do this for both items being compared. If the operands are the | |
5408 | same size and the bits being compared are in the same position | |
5409 | then we can do this by masking both and comparing the masked | |
5410 | results. */ | |
5411 | ll_mask = const_binop (BIT_IOR_EXPR, ll_mask, rl_mask, 0); | |
5412 | lr_mask = const_binop (BIT_IOR_EXPR, lr_mask, rr_mask, 0); | |
5413 | if (lnbitsize == rnbitsize && xll_bitpos == xlr_bitpos) | |
5414 | { | |
389dd41b | 5415 | lhs = make_bit_field_ref (loc, ll_inner, lntype, lnbitsize, lnbitpos, |
2a64c730 | 5416 | ll_unsignedp || rl_unsignedp); |
5417 | if (! all_ones_mask_p (ll_mask, lnbitsize)) | |
5418 | lhs = build2 (BIT_AND_EXPR, lntype, lhs, ll_mask); | |
5419 | ||
389dd41b | 5420 | rhs = make_bit_field_ref (loc, lr_inner, rntype, rnbitsize, rnbitpos, |
2a64c730 | 5421 | lr_unsignedp || rr_unsignedp); |
5422 | if (! all_ones_mask_p (lr_mask, rnbitsize)) | |
5423 | rhs = build2 (BIT_AND_EXPR, rntype, rhs, lr_mask); | |
5424 | ||
389dd41b | 5425 | result = build2 (wanted_code, truth_type, lhs, rhs); |
5426 | goto fold_truthop_exit; | |
2a64c730 | 5427 | } |
5428 | ||
5429 | /* There is still another way we can do something: If both pairs of | |
5430 | fields being compared are adjacent, we may be able to make a wider | |
5431 | field containing them both. | |
5432 | ||
5433 | Note that we still must mask the lhs/rhs expressions. Furthermore, | |
5434 | the mask must be shifted to account for the shift done by | |
5435 | make_bit_field_ref. */ | |
5436 | if ((ll_bitsize + ll_bitpos == rl_bitpos | |
5437 | && lr_bitsize + lr_bitpos == rr_bitpos) | |
5438 | || (ll_bitpos == rl_bitpos + rl_bitsize | |
5439 | && lr_bitpos == rr_bitpos + rr_bitsize)) | |
5440 | { | |
5441 | tree type; | |
5442 | ||
389dd41b | 5443 | lhs = make_bit_field_ref (loc, ll_inner, lntype, |
5444 | ll_bitsize + rl_bitsize, | |
2a64c730 | 5445 | MIN (ll_bitpos, rl_bitpos), ll_unsignedp); |
389dd41b | 5446 | rhs = make_bit_field_ref (loc, lr_inner, rntype, |
5447 | lr_bitsize + rr_bitsize, | |
2a64c730 | 5448 | MIN (lr_bitpos, rr_bitpos), lr_unsignedp); |
5449 | ||
5450 | ll_mask = const_binop (RSHIFT_EXPR, ll_mask, | |
5451 | size_int (MIN (xll_bitpos, xrl_bitpos)), 0); | |
5452 | lr_mask = const_binop (RSHIFT_EXPR, lr_mask, | |
5453 | size_int (MIN (xlr_bitpos, xrr_bitpos)), 0); | |
5454 | ||
5455 | /* Convert to the smaller type before masking out unwanted bits. */ | |
5456 | type = lntype; | |
5457 | if (lntype != rntype) | |
5458 | { | |
5459 | if (lnbitsize > rnbitsize) | |
5460 | { | |
389dd41b | 5461 | lhs = fold_convert_loc (loc, rntype, lhs); |
5462 | ll_mask = fold_convert_loc (loc, rntype, ll_mask); | |
2a64c730 | 5463 | type = rntype; |
5464 | } | |
5465 | else if (lnbitsize < rnbitsize) | |
5466 | { | |
389dd41b | 5467 | rhs = fold_convert_loc (loc, lntype, rhs); |
5468 | lr_mask = fold_convert_loc (loc, lntype, lr_mask); | |
2a64c730 | 5469 | type = lntype; |
5470 | } | |
5471 | } | |
5472 | ||
5473 | if (! all_ones_mask_p (ll_mask, ll_bitsize + rl_bitsize)) | |
5474 | lhs = build2 (BIT_AND_EXPR, type, lhs, ll_mask); | |
5475 | ||
5476 | if (! all_ones_mask_p (lr_mask, lr_bitsize + rr_bitsize)) | |
5477 | rhs = build2 (BIT_AND_EXPR, type, rhs, lr_mask); | |
5478 | ||
389dd41b | 5479 | result = build2 (wanted_code, truth_type, lhs, rhs); |
5480 | goto fold_truthop_exit; | |
2a64c730 | 5481 | } |
5482 | ||
5483 | return 0; | |
5484 | } | |
5485 | ||
2bc77e10 | 5486 | /* Handle the case of comparisons with constants. If there is something in |
5487 | common between the masks, those bits of the constants must be the same. | |
5488 | If not, the condition is always false. Test for this to avoid generating | |
5489 | incorrect code below. */ | |
5485823f | 5490 | result = const_binop (BIT_AND_EXPR, ll_mask, rl_mask, 0); |
2bc77e10 | 5491 | if (! integer_zerop (result) |
5485823f | 5492 | && simple_cst_equal (const_binop (BIT_AND_EXPR, result, l_const, 0), |
5493 | const_binop (BIT_AND_EXPR, result, r_const, 0)) != 1) | |
2bc77e10 | 5494 | { |
5495 | if (wanted_code == NE_EXPR) | |
5496 | { | |
c3ceba8e | 5497 | warning (0, "%<or%> of unmatched not-equal tests is always 1"); |
20783f07 | 5498 | return constant_boolean_node (true, truth_type); |
2bc77e10 | 5499 | } |
5500 | else | |
5501 | { | |
c3ceba8e | 5502 | warning (0, "%<and%> of mutually exclusive equal-tests is always 0"); |
20783f07 | 5503 | return constant_boolean_node (false, truth_type); |
2bc77e10 | 5504 | } |
5505 | } | |
5506 | ||
2a64c730 | 5507 | /* Construct the expression we will return. First get the component |
5508 | reference we will make. Unless the mask is all ones the width of | |
5509 | that field, perform the mask operation. Then compare with the | |
5510 | merged constant. */ | |
389dd41b | 5511 | result = make_bit_field_ref (loc, ll_inner, lntype, lnbitsize, lnbitpos, |
2a64c730 | 5512 | ll_unsignedp || rl_unsignedp); |
5513 | ||
5514 | ll_mask = const_binop (BIT_IOR_EXPR, ll_mask, rl_mask, 0); | |
5515 | if (! all_ones_mask_p (ll_mask, lnbitsize)) | |
389dd41b | 5516 | { |
5517 | result = build2 (BIT_AND_EXPR, lntype, result, ll_mask); | |
5518 | SET_EXPR_LOCATION (result, loc); | |
5519 | } | |
2a64c730 | 5520 | |
389dd41b | 5521 | result = build2 (wanted_code, truth_type, result, |
5522 | const_binop (BIT_IOR_EXPR, l_const, r_const, 0)); | |
5523 | ||
5524 | fold_truthop_exit: | |
5525 | SET_EXPR_LOCATION (result, loc); | |
5526 | return result; | |
2bc77e10 | 5527 | } |
5528 | \f | |
cc049fa3 | 5529 | /* Optimize T, which is a comparison of a MIN_EXPR or MAX_EXPR with a |
155b05dc | 5530 | constant. */ |
5531 | ||
5532 | static tree | |
389dd41b | 5533 | optimize_minmax_comparison (location_t loc, enum tree_code code, tree type, |
5534 | tree op0, tree op1) | |
155b05dc | 5535 | { |
155acab4 | 5536 | tree arg0 = op0; |
155b05dc | 5537 | enum tree_code op_code; |
226c5ce4 | 5538 | tree comp_const; |
155b05dc | 5539 | tree minmax_const; |
5540 | int consts_equal, consts_lt; | |
5541 | tree inner; | |
5542 | ||
5543 | STRIP_SIGN_NOPS (arg0); | |
5544 | ||
5545 | op_code = TREE_CODE (arg0); | |
5546 | minmax_const = TREE_OPERAND (arg0, 1); | |
389dd41b | 5547 | comp_const = fold_convert_loc (loc, TREE_TYPE (arg0), op1); |
155b05dc | 5548 | consts_equal = tree_int_cst_equal (minmax_const, comp_const); |
5549 | consts_lt = tree_int_cst_lt (minmax_const, comp_const); | |
5550 | inner = TREE_OPERAND (arg0, 0); | |
5551 | ||
5552 | /* If something does not permit us to optimize, return the original tree. */ | |
5553 | if ((op_code != MIN_EXPR && op_code != MAX_EXPR) | |
5554 | || TREE_CODE (comp_const) != INTEGER_CST | |
f96bd2bf | 5555 | || TREE_OVERFLOW (comp_const) |
155b05dc | 5556 | || TREE_CODE (minmax_const) != INTEGER_CST |
f96bd2bf | 5557 | || TREE_OVERFLOW (minmax_const)) |
155acab4 | 5558 | return NULL_TREE; |
155b05dc | 5559 | |
5560 | /* Now handle all the various comparison codes. We only handle EQ_EXPR | |
5561 | and GT_EXPR, doing the rest with recursive calls using logical | |
5562 | simplifications. */ | |
155acab4 | 5563 | switch (code) |
155b05dc | 5564 | { |
5565 | case NE_EXPR: case LT_EXPR: case LE_EXPR: | |
155acab4 | 5566 | { |
389dd41b | 5567 | tree tem |
5568 | = optimize_minmax_comparison (loc, | |
5569 | invert_tree_comparison (code, false), | |
5570 | type, op0, op1); | |
6758b11c | 5571 | if (tem) |
389dd41b | 5572 | return invert_truthvalue_loc (loc, tem); |
6758b11c | 5573 | return NULL_TREE; |
155acab4 | 5574 | } |
155b05dc | 5575 | |
5576 | case GE_EXPR: | |
5577 | return | |
389dd41b | 5578 | fold_build2_loc (loc, TRUTH_ORIF_EXPR, type, |
7ab7fd4f | 5579 | optimize_minmax_comparison |
389dd41b | 5580 | (loc, EQ_EXPR, type, arg0, comp_const), |
7ab7fd4f | 5581 | optimize_minmax_comparison |
389dd41b | 5582 | (loc, GT_EXPR, type, arg0, comp_const)); |
155b05dc | 5583 | |
5584 | case EQ_EXPR: | |
5585 | if (op_code == MAX_EXPR && consts_equal) | |
5586 | /* MAX (X, 0) == 0 -> X <= 0 */ | |
389dd41b | 5587 | return fold_build2_loc (loc, LE_EXPR, type, inner, comp_const); |
155b05dc | 5588 | |
5589 | else if (op_code == MAX_EXPR && consts_lt) | |
5590 | /* MAX (X, 0) == 5 -> X == 5 */ | |
389dd41b | 5591 | return fold_build2_loc (loc, EQ_EXPR, type, inner, comp_const); |
155b05dc | 5592 | |
5593 | else if (op_code == MAX_EXPR) | |
5594 | /* MAX (X, 0) == -1 -> false */ | |
389dd41b | 5595 | return omit_one_operand_loc (loc, type, integer_zero_node, inner); |
155b05dc | 5596 | |
5597 | else if (consts_equal) | |
5598 | /* MIN (X, 0) == 0 -> X >= 0 */ | |
389dd41b | 5599 | return fold_build2_loc (loc, GE_EXPR, type, inner, comp_const); |
155b05dc | 5600 | |
5601 | else if (consts_lt) | |
5602 | /* MIN (X, 0) == 5 -> false */ | |
389dd41b | 5603 | return omit_one_operand_loc (loc, type, integer_zero_node, inner); |
155b05dc | 5604 | |
5605 | else | |
5606 | /* MIN (X, 0) == -1 -> X == -1 */ | |
389dd41b | 5607 | return fold_build2_loc (loc, EQ_EXPR, type, inner, comp_const); |
155b05dc | 5608 | |
5609 | case GT_EXPR: | |
5610 | if (op_code == MAX_EXPR && (consts_equal || consts_lt)) | |
5611 | /* MAX (X, 0) > 0 -> X > 0 | |
5612 | MAX (X, 0) > 5 -> X > 5 */ | |
389dd41b | 5613 | return fold_build2_loc (loc, GT_EXPR, type, inner, comp_const); |
155b05dc | 5614 | |
5615 | else if (op_code == MAX_EXPR) | |
5616 | /* MAX (X, 0) > -1 -> true */ | |
389dd41b | 5617 | return omit_one_operand_loc (loc, type, integer_one_node, inner); |
155b05dc | 5618 | |
5619 | else if (op_code == MIN_EXPR && (consts_equal || consts_lt)) | |
5620 | /* MIN (X, 0) > 0 -> false | |
5621 | MIN (X, 0) > 5 -> false */ | |
389dd41b | 5622 | return omit_one_operand_loc (loc, type, integer_zero_node, inner); |
155b05dc | 5623 | |
5624 | else | |
5625 | /* MIN (X, 0) > -1 -> X > -1 */ | |
389dd41b | 5626 | return fold_build2_loc (loc, GT_EXPR, type, inner, comp_const); |
155b05dc | 5627 | |
5628 | default: | |
155acab4 | 5629 | return NULL_TREE; |
155b05dc | 5630 | } |
5631 | } | |
5632 | \f | |
23ec2d5e | 5633 | /* T is an integer expression that is being multiplied, divided, or taken a |
5634 | modulus (CODE says which and what kind of divide or modulus) by a | |
5635 | constant C. See if we can eliminate that operation by folding it with | |
5636 | other operations already in T. WIDE_TYPE, if non-null, is a type that | |
5637 | should be used for the computation if wider than our type. | |
5638 | ||
b07ba9ff | 5639 | For example, if we are dividing (X * 8) + (Y * 16) by 4, we can return |
5640 | (X * 2) + (Y * 4). We must, however, be assured that either the original | |
2f5cf552 | 5641 | expression would not overflow or that overflow is undefined for the type |
5642 | in the language in question. | |
5643 | ||
23ec2d5e | 5644 | If we return a non-null expression, it is an equivalent form of the |
add6ee5e | 5645 | original computation, but need not be in the original type. |
5646 | ||
5647 | We set *STRICT_OVERFLOW_P to true if the return values depends on | |
5648 | signed overflow being undefined. Otherwise we do not change | |
5649 | *STRICT_OVERFLOW_P. */ | |
23ec2d5e | 5650 | |
5651 | static tree | |
add6ee5e | 5652 | extract_muldiv (tree t, tree c, enum tree_code code, tree wide_type, |
5653 | bool *strict_overflow_p) | |
009f6e1c | 5654 | { |
5655 | /* To avoid exponential search depth, refuse to allow recursion past | |
5656 | three levels. Beyond that (1) it's highly unlikely that we'll find | |
5657 | something interesting and (2) we've probably processed it before | |
5658 | when we built the inner expression. */ | |
5659 | ||
5660 | static int depth; | |
5661 | tree ret; | |
5662 | ||
5663 | if (depth > 3) | |
5664 | return NULL; | |
5665 | ||
5666 | depth++; | |
add6ee5e | 5667 | ret = extract_muldiv_1 (t, c, code, wide_type, strict_overflow_p); |
009f6e1c | 5668 | depth--; |
5669 | ||
5670 | return ret; | |
5671 | } | |
5672 | ||
5673 | static tree | |
add6ee5e | 5674 | extract_muldiv_1 (tree t, tree c, enum tree_code code, tree wide_type, |
5675 | bool *strict_overflow_p) | |
23ec2d5e | 5676 | { |
5677 | tree type = TREE_TYPE (t); | |
5678 | enum tree_code tcode = TREE_CODE (t); | |
cc049fa3 | 5679 | tree ctype = (wide_type != 0 && (GET_MODE_SIZE (TYPE_MODE (wide_type)) |
23ec2d5e | 5680 | > GET_MODE_SIZE (TYPE_MODE (type))) |
5681 | ? wide_type : type); | |
5682 | tree t1, t2; | |
5683 | int same_p = tcode == code; | |
03435587 | 5684 | tree op0 = NULL_TREE, op1 = NULL_TREE; |
add6ee5e | 5685 | bool sub_strict_overflow_p; |
23ec2d5e | 5686 | |
5687 | /* Don't deal with constants of zero here; they confuse the code below. */ | |
5688 | if (integer_zerop (c)) | |
2f5cf552 | 5689 | return NULL_TREE; |
23ec2d5e | 5690 | |
ce45a448 | 5691 | if (TREE_CODE_CLASS (tcode) == tcc_unary) |
23ec2d5e | 5692 | op0 = TREE_OPERAND (t, 0); |
5693 | ||
ce45a448 | 5694 | if (TREE_CODE_CLASS (tcode) == tcc_binary) |
23ec2d5e | 5695 | op0 = TREE_OPERAND (t, 0), op1 = TREE_OPERAND (t, 1); |
5696 | ||
5697 | /* Note that we need not handle conditional operations here since fold | |
5698 | already handles those cases. So just do arithmetic here. */ | |
5699 | switch (tcode) | |
5700 | { | |
5701 | case INTEGER_CST: | |
5702 | /* For a constant, we can always simplify if we are a multiply | |
5703 | or (for divide and modulus) if it is a multiple of our constant. */ | |
5704 | if (code == MULT_EXPR | |
5705 | || integer_zerop (const_binop (TRUNC_MOD_EXPR, t, c, 0))) | |
b30e3dbc | 5706 | return const_binop (code, fold_convert (ctype, t), |
5707 | fold_convert (ctype, c), 0); | |
23ec2d5e | 5708 | break; |
5709 | ||
72dd6141 | 5710 | CASE_CONVERT: case NON_LVALUE_EXPR: |
12480406 | 5711 | /* If op0 is an expression ... */ |
ce45a448 | 5712 | if ((COMPARISON_CLASS_P (op0) |
5713 | || UNARY_CLASS_P (op0) | |
5714 | || BINARY_CLASS_P (op0) | |
c2f47e15 | 5715 | || VL_EXP_CLASS_P (op0) |
ce45a448 | 5716 | || EXPRESSION_CLASS_P (op0)) |
32054974 | 5717 | /* ... and has wrapping overflow, and its type is smaller |
5718 | than ctype, then we cannot pass through as widening. */ | |
5719 | && ((TYPE_OVERFLOW_WRAPS (TREE_TYPE (op0)) | |
12480406 | 5720 | && ! (TREE_CODE (TREE_TYPE (op0)) == INTEGER_TYPE |
5721 | && TYPE_IS_SIZETYPE (TREE_TYPE (op0))) | |
32054974 | 5722 | && (TYPE_PRECISION (ctype) |
5723 | > TYPE_PRECISION (TREE_TYPE (op0)))) | |
40309554 | 5724 | /* ... or this is a truncation (t is narrower than op0), |
5725 | then we cannot pass through this narrowing. */ | |
32054974 | 5726 | || (TYPE_PRECISION (type) |
5727 | < TYPE_PRECISION (TREE_TYPE (op0))) | |
cee280ef | 5728 | /* ... or signedness changes for division or modulus, |
5729 | then we cannot pass through this conversion. */ | |
5730 | || (code != MULT_EXPR | |
78a8ed03 | 5731 | && (TYPE_UNSIGNED (ctype) |
28fa8094 | 5732 | != TYPE_UNSIGNED (TREE_TYPE (op0)))) |
5733 | /* ... or has undefined overflow while the converted to | |
5734 | type has not, we cannot do the operation in the inner type | |
5735 | as that would introduce undefined overflow. */ | |
5736 | || (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (op0)) | |
5737 | && !TYPE_OVERFLOW_UNDEFINED (type)))) | |
3cb1a3c6 | 5738 | break; |
5739 | ||
23ec2d5e | 5740 | /* Pass the constant down and see if we can make a simplification. If |
5f0002b0 | 5741 | we can, replace this expression with the inner simplification for |
5742 | possible later conversion to our or some other type. */ | |
b30e3dbc | 5743 | if ((t2 = fold_convert (TREE_TYPE (op0), c)) != 0 |
f2fa1510 | 5744 | && TREE_CODE (t2) == INTEGER_CST |
f96bd2bf | 5745 | && !TREE_OVERFLOW (t2) |
f2fa1510 | 5746 | && (0 != (t1 = extract_muldiv (op0, t2, code, |
5747 | code == MULT_EXPR | |
add6ee5e | 5748 | ? ctype : NULL_TREE, |
5749 | strict_overflow_p)))) | |
23ec2d5e | 5750 | return t1; |
5751 | break; | |
5752 | ||
24877233 | 5753 | case ABS_EXPR: |
5754 | /* If widening the type changes it from signed to unsigned, then we | |
5755 | must avoid building ABS_EXPR itself as unsigned. */ | |
5756 | if (TYPE_UNSIGNED (ctype) && !TYPE_UNSIGNED (type)) | |
5757 | { | |
11773141 | 5758 | tree cstype = (*signed_type_for) (ctype); |
add6ee5e | 5759 | if ((t1 = extract_muldiv (op0, c, code, cstype, strict_overflow_p)) |
5760 | != 0) | |
24877233 | 5761 | { |
7ab7fd4f | 5762 | t1 = fold_build1 (tcode, cstype, fold_convert (cstype, t1)); |
24877233 | 5763 | return fold_convert (ctype, t1); |
5764 | } | |
5765 | break; | |
5766 | } | |
d8100984 | 5767 | /* If the constant is negative, we cannot simplify this. */ |
5768 | if (tree_int_cst_sgn (c) == -1) | |
5769 | break; | |
24877233 | 5770 | /* FALLTHROUGH */ |
5771 | case NEGATE_EXPR: | |
add6ee5e | 5772 | if ((t1 = extract_muldiv (op0, c, code, wide_type, strict_overflow_p)) |
5773 | != 0) | |
7ab7fd4f | 5774 | return fold_build1 (tcode, ctype, fold_convert (ctype, t1)); |
23ec2d5e | 5775 | break; |
5776 | ||
5777 | case MIN_EXPR: case MAX_EXPR: | |
6269027b | 5778 | /* If widening the type changes the signedness, then we can't perform |
5779 | this optimization as that changes the result. */ | |
78a8ed03 | 5780 | if (TYPE_UNSIGNED (ctype) != TYPE_UNSIGNED (type)) |
6269027b | 5781 | break; |
5782 | ||
23ec2d5e | 5783 | /* MIN (a, b) / 5 -> MIN (a / 5, b / 5) */ |
add6ee5e | 5784 | sub_strict_overflow_p = false; |
5785 | if ((t1 = extract_muldiv (op0, c, code, wide_type, | |
5786 | &sub_strict_overflow_p)) != 0 | |
5787 | && (t2 = extract_muldiv (op1, c, code, wide_type, | |
5788 | &sub_strict_overflow_p)) != 0) | |
5f0002b0 | 5789 | { |
5790 | if (tree_int_cst_sgn (c) < 0) | |
5791 | tcode = (tcode == MIN_EXPR ? MAX_EXPR : MIN_EXPR); | |
add6ee5e | 5792 | if (sub_strict_overflow_p) |
5793 | *strict_overflow_p = true; | |
7ab7fd4f | 5794 | return fold_build2 (tcode, ctype, fold_convert (ctype, t1), |
5795 | fold_convert (ctype, t2)); | |
5f0002b0 | 5796 | } |
23ec2d5e | 5797 | break; |
5798 | ||
23ec2d5e | 5799 | case LSHIFT_EXPR: case RSHIFT_EXPR: |
5800 | /* If the second operand is constant, this is a multiplication | |
5801 | or floor division, by a power of two, so we can treat it that | |
dceee6fb | 5802 | way unless the multiplier or divisor overflows. Signed |
5803 | left-shift overflow is implementation-defined rather than | |
5804 | undefined in C90, so do not convert signed left shift into | |
5805 | multiplication. */ | |
23ec2d5e | 5806 | if (TREE_CODE (op1) == INTEGER_CST |
dceee6fb | 5807 | && (tcode == RSHIFT_EXPR || TYPE_UNSIGNED (TREE_TYPE (op0))) |
c011f821 | 5808 | /* const_binop may not detect overflow correctly, |
5809 | so check for it explicitly here. */ | |
5810 | && TYPE_PRECISION (TREE_TYPE (size_one_node)) > TREE_INT_CST_LOW (op1) | |
5811 | && TREE_INT_CST_HIGH (op1) == 0 | |
b30e3dbc | 5812 | && 0 != (t1 = fold_convert (ctype, |
5813 | const_binop (LSHIFT_EXPR, | |
5814 | size_one_node, | |
5815 | op1, 0))) | |
f96bd2bf | 5816 | && !TREE_OVERFLOW (t1)) |
fd96eeef | 5817 | return extract_muldiv (build2 (tcode == LSHIFT_EXPR |
5818 | ? MULT_EXPR : FLOOR_DIV_EXPR, | |
389dd41b | 5819 | ctype, |
5820 | fold_convert (ctype, op0), | |
5821 | t1), | |
add6ee5e | 5822 | c, code, wide_type, strict_overflow_p); |
23ec2d5e | 5823 | break; |
5824 | ||
5825 | case PLUS_EXPR: case MINUS_EXPR: | |
5826 | /* See if we can eliminate the operation on both sides. If we can, we | |
5827 | can return a new PLUS or MINUS. If we can't, the only remaining | |
5828 | cases where we can do anything are if the second operand is a | |
5829 | constant. */ | |
add6ee5e | 5830 | sub_strict_overflow_p = false; |
5831 | t1 = extract_muldiv (op0, c, code, wide_type, &sub_strict_overflow_p); | |
5832 | t2 = extract_muldiv (op1, c, code, wide_type, &sub_strict_overflow_p); | |
17e3940f | 5833 | if (t1 != 0 && t2 != 0 |
5834 | && (code == MULT_EXPR | |
e5b30d78 | 5835 | /* If not multiplication, we can only do this if both operands |
5836 | are divisible by c. */ | |
5837 | || (multiple_of_p (ctype, op0, c) | |
5838 | && multiple_of_p (ctype, op1, c)))) | |
add6ee5e | 5839 | { |
5840 | if (sub_strict_overflow_p) | |
5841 | *strict_overflow_p = true; | |
5842 | return fold_build2 (tcode, ctype, fold_convert (ctype, t1), | |
5843 | fold_convert (ctype, t2)); | |
5844 | } | |
23ec2d5e | 5845 | |
5f0002b0 | 5846 | /* If this was a subtraction, negate OP1 and set it to be an addition. |
5847 | This simplifies the logic below. */ | |
5848 | if (tcode == MINUS_EXPR) | |
d5518ed9 | 5849 | { |
5850 | tcode = PLUS_EXPR, op1 = negate_expr (op1); | |
5851 | /* If OP1 was not easily negatable, the constant may be OP0. */ | |
5852 | if (TREE_CODE (op0) == INTEGER_CST) | |
5853 | { | |
5854 | tree tem = op0; | |
5855 | op0 = op1; | |
5856 | op1 = tem; | |
5857 | tem = t1; | |
5858 | t1 = t2; | |
5859 | t2 = tem; | |
5860 | } | |
5861 | } | |
5f0002b0 | 5862 | |
ec4d93b0 | 5863 | if (TREE_CODE (op1) != INTEGER_CST) |
5864 | break; | |
5865 | ||
5f0002b0 | 5866 | /* If either OP1 or C are negative, this optimization is not safe for |
5867 | some of the division and remainder types while for others we need | |
5868 | to change the code. */ | |
5869 | if (tree_int_cst_sgn (op1) < 0 || tree_int_cst_sgn (c) < 0) | |
5870 | { | |
5871 | if (code == CEIL_DIV_EXPR) | |
5872 | code = FLOOR_DIV_EXPR; | |
5f0002b0 | 5873 | else if (code == FLOOR_DIV_EXPR) |
5874 | code = CEIL_DIV_EXPR; | |
b575bb01 | 5875 | else if (code != MULT_EXPR |
5876 | && code != CEIL_MOD_EXPR && code != FLOOR_MOD_EXPR) | |
5f0002b0 | 5877 | break; |
5878 | } | |
5879 | ||
98248b34 | 5880 | /* If it's a multiply or a division/modulus operation of a multiple |
5881 | of our constant, do the operation and verify it doesn't overflow. */ | |
5882 | if (code == MULT_EXPR | |
5883 | || integer_zerop (const_binop (TRUNC_MOD_EXPR, op1, c, 0))) | |
d3371fcd | 5884 | { |
b30e3dbc | 5885 | op1 = const_binop (code, fold_convert (ctype, op1), |
5886 | fold_convert (ctype, c), 0); | |
f5c47dd7 | 5887 | /* We allow the constant to overflow with wrapping semantics. */ |
5888 | if (op1 == 0 | |
981eb798 | 5889 | || (TREE_OVERFLOW (op1) && !TYPE_OVERFLOW_WRAPS (ctype))) |
d3371fcd | 5890 | break; |
5891 | } | |
98248b34 | 5892 | else |
d3371fcd | 5893 | break; |
5f0002b0 | 5894 | |
fc452262 | 5895 | /* If we have an unsigned type is not a sizetype, we cannot widen |
5896 | the operation since it will change the result if the original | |
5897 | computation overflowed. */ | |
78a8ed03 | 5898 | if (TYPE_UNSIGNED (ctype) |
d490e2f2 | 5899 | && ! (TREE_CODE (ctype) == INTEGER_TYPE && TYPE_IS_SIZETYPE (ctype)) |
fc452262 | 5900 | && ctype != type) |
5901 | break; | |
5902 | ||
23ec2d5e | 5903 | /* If we were able to eliminate our operation from the first side, |
5f0002b0 | 5904 | apply our operation to the second side and reform the PLUS. */ |
5905 | if (t1 != 0 && (TREE_CODE (t1) != code || code == MULT_EXPR)) | |
7ab7fd4f | 5906 | return fold_build2 (tcode, ctype, fold_convert (ctype, t1), op1); |
23ec2d5e | 5907 | |
5908 | /* The last case is if we are a multiply. In that case, we can | |
5909 | apply the distributive law to commute the multiply and addition | |
6312a35e | 5910 | if the multiplication of the constants doesn't overflow. */ |
5f0002b0 | 5911 | if (code == MULT_EXPR) |
7ab7fd4f | 5912 | return fold_build2 (tcode, ctype, |
5913 | fold_build2 (code, ctype, | |
5914 | fold_convert (ctype, op0), | |
5915 | fold_convert (ctype, c)), | |
5916 | op1); | |
23ec2d5e | 5917 | |
5918 | break; | |
5919 | ||
5920 | case MULT_EXPR: | |
5921 | /* We have a special case here if we are doing something like | |
5922 | (C * 8) % 4 since we know that's zero. */ | |
5923 | if ((code == TRUNC_MOD_EXPR || code == CEIL_MOD_EXPR | |
5924 | || code == FLOOR_MOD_EXPR || code == ROUND_MOD_EXPR) | |
67f36f78 | 5925 | /* If the multiplication can overflow we cannot optimize this. |
5926 | ??? Until we can properly mark individual operations as | |
5927 | not overflowing we need to treat sizetype special here as | |
5928 | stor-layout relies on this opimization to make | |
5929 | DECL_FIELD_BIT_OFFSET always a constant. */ | |
5930 | && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (t)) | |
5931 | || (TREE_CODE (TREE_TYPE (t)) == INTEGER_TYPE | |
5932 | && TYPE_IS_SIZETYPE (TREE_TYPE (t)))) | |
23ec2d5e | 5933 | && TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST |
5934 | && integer_zerop (const_binop (TRUNC_MOD_EXPR, op1, c, 0))) | |
67f36f78 | 5935 | { |
5936 | *strict_overflow_p = true; | |
5937 | return omit_one_operand (type, integer_zero_node, op0); | |
5938 | } | |
23ec2d5e | 5939 | |
6312a35e | 5940 | /* ... fall through ... */ |
23ec2d5e | 5941 | |
5942 | case TRUNC_DIV_EXPR: case CEIL_DIV_EXPR: case FLOOR_DIV_EXPR: | |
5943 | case ROUND_DIV_EXPR: case EXACT_DIV_EXPR: | |
5944 | /* If we can extract our operation from the LHS, do so and return a | |
5945 | new operation. Likewise for the RHS from a MULT_EXPR. Otherwise, | |
5946 | do something only if the second operand is a constant. */ | |
5947 | if (same_p | |
add6ee5e | 5948 | && (t1 = extract_muldiv (op0, c, code, wide_type, |
5949 | strict_overflow_p)) != 0) | |
7ab7fd4f | 5950 | return fold_build2 (tcode, ctype, fold_convert (ctype, t1), |
5951 | fold_convert (ctype, op1)); | |
23ec2d5e | 5952 | else if (tcode == MULT_EXPR && code == MULT_EXPR |
add6ee5e | 5953 | && (t1 = extract_muldiv (op1, c, code, wide_type, |
5954 | strict_overflow_p)) != 0) | |
7ab7fd4f | 5955 | return fold_build2 (tcode, ctype, fold_convert (ctype, op0), |
5956 | fold_convert (ctype, t1)); | |
23ec2d5e | 5957 | else if (TREE_CODE (op1) != INTEGER_CST) |
5958 | return 0; | |
5959 | ||
5960 | /* If these are the same operation types, we can associate them | |
5961 | assuming no overflow. */ | |
5962 | if (tcode == code | |
389dd41b | 5963 | && 0 != (t1 = int_const_binop (MULT_EXPR, |
5964 | fold_convert (ctype, op1), | |
c79abec2 | 5965 | fold_convert (ctype, c), 1)) |
5966 | && 0 != (t1 = force_fit_type_double (ctype, TREE_INT_CST_LOW (t1), | |
5967 | TREE_INT_CST_HIGH (t1), | |
5968 | (TYPE_UNSIGNED (ctype) | |
5969 | && tcode != MULT_EXPR) ? -1 : 1, | |
5970 | TREE_OVERFLOW (t1))) | |
f96bd2bf | 5971 | && !TREE_OVERFLOW (t1)) |
7ab7fd4f | 5972 | return fold_build2 (tcode, ctype, fold_convert (ctype, op0), t1); |
23ec2d5e | 5973 | |
5974 | /* If these operations "cancel" each other, we have the main | |
5975 | optimizations of this pass, which occur when either constant is a | |
5976 | multiple of the other, in which case we replace this with either an | |
cc049fa3 | 5977 | operation or CODE or TCODE. |
2f5cf552 | 5978 | |
35a3065a | 5979 | If we have an unsigned type that is not a sizetype, we cannot do |
2f5cf552 | 5980 | this since it will change the result if the original computation |
5981 | overflowed. */ | |
981eb798 | 5982 | if ((TYPE_OVERFLOW_UNDEFINED (ctype) |
d490e2f2 | 5983 | || (TREE_CODE (ctype) == INTEGER_TYPE && TYPE_IS_SIZETYPE (ctype))) |
2f5cf552 | 5984 | && ((code == MULT_EXPR && tcode == EXACT_DIV_EXPR) |
5985 | || (tcode == MULT_EXPR | |
5986 | && code != TRUNC_MOD_EXPR && code != CEIL_MOD_EXPR | |
c50ab071 | 5987 | && code != FLOOR_MOD_EXPR && code != ROUND_MOD_EXPR |
5988 | && code != MULT_EXPR))) | |
23ec2d5e | 5989 | { |
5990 | if (integer_zerop (const_binop (TRUNC_MOD_EXPR, op1, c, 0))) | |
add6ee5e | 5991 | { |
5992 | if (TYPE_OVERFLOW_UNDEFINED (ctype)) | |
5993 | *strict_overflow_p = true; | |
5994 | return fold_build2 (tcode, ctype, fold_convert (ctype, op0), | |
5995 | fold_convert (ctype, | |
5996 | const_binop (TRUNC_DIV_EXPR, | |
5997 | op1, c, 0))); | |
5998 | } | |
23ec2d5e | 5999 | else if (integer_zerop (const_binop (TRUNC_MOD_EXPR, c, op1, 0))) |
add6ee5e | 6000 | { |
6001 | if (TYPE_OVERFLOW_UNDEFINED (ctype)) | |
6002 | *strict_overflow_p = true; | |
6003 | return fold_build2 (code, ctype, fold_convert (ctype, op0), | |
6004 | fold_convert (ctype, | |
6005 | const_binop (TRUNC_DIV_EXPR, | |
6006 | c, op1, 0))); | |
6007 | } | |
23ec2d5e | 6008 | } |
6009 | break; | |
6010 | ||
6011 | default: | |
6012 | break; | |
6013 | } | |
6014 | ||
6015 | return 0; | |
6016 | } | |
6017 | \f | |
b4af30fd | 6018 | /* Return a node which has the indicated constant VALUE (either 0 or |
6019 | 1), and is of the indicated TYPE. */ | |
6020 | ||
5c9198bd | 6021 | tree |
de1b648b | 6022 | constant_boolean_node (int value, tree type) |
b4af30fd | 6023 | { |
6024 | if (type == integer_type_node) | |
6025 | return value ? integer_one_node : integer_zero_node; | |
c4e122e7 | 6026 | else if (type == boolean_type_node) |
6027 | return value ? boolean_true_node : boolean_false_node; | |
cc049fa3 | 6028 | else |
7016c612 | 6029 | return build_int_cst (type, value); |
b4af30fd | 6030 | } |
6031 | ||
6d24c9aa | 6032 | |
203a24c4 | 6033 | /* Transform `a + (b ? x : y)' into `b ? (a + x) : (a + y)'. |
47cbd05d | 6034 | Transform, `a + (x < y)' into `(x < y) ? (a + 1) : (a + 0)'. Here |
6035 | CODE corresponds to the `+', COND to the `(b ? x : y)' or `(x < y)' | |
6ef828f9 | 6036 | expression, and ARG to `a'. If COND_FIRST_P is nonzero, then the |
47cbd05d | 6037 | COND is the first argument to CODE; otherwise (as in the example |
6038 | given here), it is the second argument. TYPE is the type of the | |
9c9bad97 | 6039 | original expression. Return NULL_TREE if no simplification is |
a6661800 | 6040 | possible. */ |
47cbd05d | 6041 | |
6042 | static tree | |
389dd41b | 6043 | fold_binary_op_with_conditional_arg (location_t loc, |
6044 | enum tree_code code, | |
1ebe9a83 | 6045 | tree type, tree op0, tree op1, |
6046 | tree cond, tree arg, int cond_first_p) | |
47cbd05d | 6047 | { |
1ebe9a83 | 6048 | tree cond_type = cond_first_p ? TREE_TYPE (op0) : TREE_TYPE (op1); |
84b251e4 | 6049 | tree arg_type = cond_first_p ? TREE_TYPE (op1) : TREE_TYPE (op0); |
47cbd05d | 6050 | tree test, true_value, false_value; |
6051 | tree lhs = NULL_TREE; | |
6052 | tree rhs = NULL_TREE; | |
a6661800 | 6053 | |
47cbd05d | 6054 | if (TREE_CODE (cond) == COND_EXPR) |
6055 | { | |
6056 | test = TREE_OPERAND (cond, 0); | |
6057 | true_value = TREE_OPERAND (cond, 1); | |
6058 | false_value = TREE_OPERAND (cond, 2); | |
6059 | /* If this operand throws an expression, then it does not make | |
6060 | sense to try to perform a logical or arithmetic operation | |
f2b83d13 | 6061 | involving it. */ |
47cbd05d | 6062 | if (VOID_TYPE_P (TREE_TYPE (true_value))) |
f2b83d13 | 6063 | lhs = true_value; |
47cbd05d | 6064 | if (VOID_TYPE_P (TREE_TYPE (false_value))) |
f2b83d13 | 6065 | rhs = false_value; |
47cbd05d | 6066 | } |
6067 | else | |
6068 | { | |
6069 | tree testtype = TREE_TYPE (cond); | |
6070 | test = cond; | |
20783f07 | 6071 | true_value = constant_boolean_node (true, testtype); |
6072 | false_value = constant_boolean_node (false, testtype); | |
47cbd05d | 6073 | } |
d3371fcd | 6074 | |
c6feb9f1 | 6075 | /* This transformation is only worthwhile if we don't have to wrap ARG |
6076 | in a SAVE_EXPR and the operation can be simplified on at least one | |
6077 | of the branches once its pushed inside the COND_EXPR. */ | |
6078 | if (!TREE_CONSTANT (arg) | |
6079 | && (TREE_SIDE_EFFECTS (arg) | |
6080 | || TREE_CONSTANT (true_value) || TREE_CONSTANT (false_value))) | |
6081 | return NULL_TREE; | |
6082 | ||
389dd41b | 6083 | arg = fold_convert_loc (loc, arg_type, arg); |
47cbd05d | 6084 | if (lhs == 0) |
5fe1fe72 | 6085 | { |
389dd41b | 6086 | true_value = fold_convert_loc (loc, cond_type, true_value); |
b085d4e5 | 6087 | if (cond_first_p) |
389dd41b | 6088 | lhs = fold_build2_loc (loc, code, type, true_value, arg); |
b085d4e5 | 6089 | else |
389dd41b | 6090 | lhs = fold_build2_loc (loc, code, type, arg, true_value); |
5fe1fe72 | 6091 | } |
47cbd05d | 6092 | if (rhs == 0) |
5fe1fe72 | 6093 | { |
389dd41b | 6094 | false_value = fold_convert_loc (loc, cond_type, false_value); |
b085d4e5 | 6095 | if (cond_first_p) |
389dd41b | 6096 | rhs = fold_build2_loc (loc, code, type, false_value, arg); |
b085d4e5 | 6097 | else |
389dd41b | 6098 | rhs = fold_build2_loc (loc, code, type, arg, false_value); |
5fe1fe72 | 6099 | } |
f2b83d13 | 6100 | |
c6feb9f1 | 6101 | /* Check that we have simplified at least one of the branches. */ |
6102 | if (!TREE_CONSTANT (arg) && !TREE_CONSTANT (lhs) && !TREE_CONSTANT (rhs)) | |
6103 | return NULL_TREE; | |
6104 | ||
6105 | return fold_build3_loc (loc, COND_EXPR, type, test, lhs, rhs); | |
47cbd05d | 6106 | } |
6107 | ||
be2828ce | 6108 | \f |
920d0fb5 | 6109 | /* Subroutine of fold() that checks for the addition of +/- 0.0. |
6110 | ||
6111 | If !NEGATE, return true if ADDEND is +/-0.0 and, for all X of type | |
6112 | TYPE, X + ADDEND is the same as X. If NEGATE, return true if X - | |
6113 | ADDEND is the same as X. | |
6114 | ||
6ef828f9 | 6115 | X + 0 and X - 0 both give X when X is NaN, infinite, or nonzero |
920d0fb5 | 6116 | and finite. The problematic cases are when X is zero, and its mode |
6117 | has signed zeros. In the case of rounding towards -infinity, | |
6118 | X - 0 is not the same as X because 0 - 0 is -0. In other rounding | |
6119 | modes, X + 0 is not the same as X because -0 + 0 is 0. */ | |
6120 | ||
46ef5347 | 6121 | bool |
b4b34335 | 6122 | fold_real_zero_addition_p (const_tree type, const_tree addend, int negate) |
920d0fb5 | 6123 | { |
6124 | if (!real_zerop (addend)) | |
6125 | return false; | |
6126 | ||
c7590f7e | 6127 | /* Don't allow the fold with -fsignaling-nans. */ |
6128 | if (HONOR_SNANS (TYPE_MODE (type))) | |
6129 | return false; | |
6130 | ||
920d0fb5 | 6131 | /* Allow the fold if zeros aren't signed, or their sign isn't important. */ |
6132 | if (!HONOR_SIGNED_ZEROS (TYPE_MODE (type))) | |
6133 | return true; | |
6134 | ||
6135 | /* Treat x + -0 as x - 0 and x - -0 as x + 0. */ | |
6136 | if (TREE_CODE (addend) == REAL_CST | |
6137 | && REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (addend))) | |
6138 | negate = !negate; | |
6139 | ||
6140 | /* The mode has signed zeros, and we have to honor their sign. | |
6141 | In this situation, there is only one case we can return true for. | |
6142 | X - 0 is the same as X unless rounding towards -infinity is | |
6143 | supported. */ | |
6144 | return negate && !HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (type)); | |
6145 | } | |
6146 | ||
4b0b9adb | 6147 | /* Subroutine of fold() that checks comparisons of built-in math |
6148 | functions against real constants. | |
6149 | ||
6150 | FCODE is the DECL_FUNCTION_CODE of the built-in, CODE is the comparison | |
6151 | operator: EQ_EXPR, NE_EXPR, GT_EXPR, LT_EXPR, GE_EXPR or LE_EXPR. TYPE | |
6152 | is the type of the result and ARG0 and ARG1 are the operands of the | |
6153 | comparison. ARG1 must be a TREE_REAL_CST. | |
6154 | ||
6155 | The function returns the constant folded tree if a simplification | |
6156 | can be made, and NULL_TREE otherwise. */ | |
6157 | ||
6158 | static tree | |
389dd41b | 6159 | fold_mathfn_compare (location_t loc, |
6160 | enum built_in_function fcode, enum tree_code code, | |
dc81944a | 6161 | tree type, tree arg0, tree arg1) |
4b0b9adb | 6162 | { |
6163 | REAL_VALUE_TYPE c; | |
6164 | ||
852da3c3 | 6165 | if (BUILTIN_SQRT_P (fcode)) |
4b0b9adb | 6166 | { |
c2f47e15 | 6167 | tree arg = CALL_EXPR_ARG (arg0, 0); |
4b0b9adb | 6168 | enum machine_mode mode = TYPE_MODE (TREE_TYPE (arg0)); |
6169 | ||
6170 | c = TREE_REAL_CST (arg1); | |
6171 | if (REAL_VALUE_NEGATIVE (c)) | |
6172 | { | |
6173 | /* sqrt(x) < y is always false, if y is negative. */ | |
6174 | if (code == EQ_EXPR || code == LT_EXPR || code == LE_EXPR) | |
389dd41b | 6175 | return omit_one_operand_loc (loc, type, integer_zero_node, arg); |
4b0b9adb | 6176 | |
6177 | /* sqrt(x) > y is always true, if y is negative and we | |
6178 | don't care about NaNs, i.e. negative values of x. */ | |
6179 | if (code == NE_EXPR || !HONOR_NANS (mode)) | |
389dd41b | 6180 | return omit_one_operand_loc (loc, type, integer_one_node, arg); |
4b0b9adb | 6181 | |
6182 | /* sqrt(x) > y is the same as x >= 0, if y is negative. */ | |
389dd41b | 6183 | return fold_build2_loc (loc, GE_EXPR, type, arg, |
7ab7fd4f | 6184 | build_real (TREE_TYPE (arg), dconst0)); |
4b0b9adb | 6185 | } |
6186 | else if (code == GT_EXPR || code == GE_EXPR) | |
6187 | { | |
6188 | REAL_VALUE_TYPE c2; | |
6189 | ||
6190 | REAL_ARITHMETIC (c2, MULT_EXPR, c, c); | |
6191 | real_convert (&c2, mode, &c2); | |
6192 | ||
6193 | if (REAL_VALUE_ISINF (c2)) | |
6194 | { | |
6195 | /* sqrt(x) > y is x == +Inf, when y is very large. */ | |
6196 | if (HONOR_INFINITIES (mode)) | |
389dd41b | 6197 | return fold_build2_loc (loc, EQ_EXPR, type, arg, |
7ab7fd4f | 6198 | build_real (TREE_TYPE (arg), c2)); |
4b0b9adb | 6199 | |
6200 | /* sqrt(x) > y is always false, when y is very large | |
6201 | and we don't care about infinities. */ | |
389dd41b | 6202 | return omit_one_operand_loc (loc, type, integer_zero_node, arg); |
4b0b9adb | 6203 | } |
6204 | ||
6205 | /* sqrt(x) > c is the same as x > c*c. */ | |
389dd41b | 6206 | return fold_build2_loc (loc, code, type, arg, |
7ab7fd4f | 6207 | build_real (TREE_TYPE (arg), c2)); |
4b0b9adb | 6208 | } |
6209 | else if (code == LT_EXPR || code == LE_EXPR) | |
6210 | { | |
6211 | REAL_VALUE_TYPE c2; | |
6212 | ||
6213 | REAL_ARITHMETIC (c2, MULT_EXPR, c, c); | |
6214 | real_convert (&c2, mode, &c2); | |
6215 | ||
6216 | if (REAL_VALUE_ISINF (c2)) | |
6217 | { | |
6218 | /* sqrt(x) < y is always true, when y is a very large | |
6219 | value and we don't care about NaNs or Infinities. */ | |
6220 | if (! HONOR_NANS (mode) && ! HONOR_INFINITIES (mode)) | |
389dd41b | 6221 | return omit_one_operand_loc (loc, type, integer_one_node, arg); |
4b0b9adb | 6222 | |
6223 | /* sqrt(x) < y is x != +Inf when y is very large and we | |
6224 | don't care about NaNs. */ | |
6225 | if (! HONOR_NANS (mode)) | |
389dd41b | 6226 | return fold_build2_loc (loc, NE_EXPR, type, arg, |
7ab7fd4f | 6227 | build_real (TREE_TYPE (arg), c2)); |
4b0b9adb | 6228 | |
6229 | /* sqrt(x) < y is x >= 0 when y is very large and we | |
6230 | don't care about Infinities. */ | |
6231 | if (! HONOR_INFINITIES (mode)) | |
389dd41b | 6232 | return fold_build2_loc (loc, GE_EXPR, type, arg, |
7ab7fd4f | 6233 | build_real (TREE_TYPE (arg), dconst0)); |
4b0b9adb | 6234 | |
6235 | /* sqrt(x) < y is x >= 0 && x != +Inf, when y is large. */ | |
fa8b888f | 6236 | if (lang_hooks.decls.global_bindings_p () != 0 |
ce3fb06e | 6237 | || CONTAINS_PLACEHOLDER_P (arg)) |
4b0b9adb | 6238 | return NULL_TREE; |
6239 | ||
6240 | arg = save_expr (arg); | |
389dd41b | 6241 | return fold_build2_loc (loc, TRUTH_ANDIF_EXPR, type, |
6242 | fold_build2_loc (loc, GE_EXPR, type, arg, | |
7ab7fd4f | 6243 | build_real (TREE_TYPE (arg), |
6244 | dconst0)), | |
389dd41b | 6245 | fold_build2_loc (loc, NE_EXPR, type, arg, |
7ab7fd4f | 6246 | build_real (TREE_TYPE (arg), |
6247 | c2))); | |
4b0b9adb | 6248 | } |
6249 | ||
6250 | /* sqrt(x) < c is the same as x < c*c, if we ignore NaNs. */ | |
6251 | if (! HONOR_NANS (mode)) | |
389dd41b | 6252 | return fold_build2_loc (loc, code, type, arg, |
7ab7fd4f | 6253 | build_real (TREE_TYPE (arg), c2)); |
4b0b9adb | 6254 | |
6255 | /* sqrt(x) < c is the same as x >= 0 && x < c*c. */ | |
fa8b888f | 6256 | if (lang_hooks.decls.global_bindings_p () == 0 |
ce3fb06e | 6257 | && ! CONTAINS_PLACEHOLDER_P (arg)) |
4b0b9adb | 6258 | { |
6259 | arg = save_expr (arg); | |
389dd41b | 6260 | return fold_build2_loc (loc, TRUTH_ANDIF_EXPR, type, |
6261 | fold_build2_loc (loc, GE_EXPR, type, arg, | |
7ab7fd4f | 6262 | build_real (TREE_TYPE (arg), |
6263 | dconst0)), | |
389dd41b | 6264 | fold_build2_loc (loc, code, type, arg, |
7ab7fd4f | 6265 | build_real (TREE_TYPE (arg), |
6266 | c2))); | |
4b0b9adb | 6267 | } |
6268 | } | |
6269 | } | |
6270 | ||
6271 | return NULL_TREE; | |
6272 | } | |
6273 | ||
6d2e901f | 6274 | /* Subroutine of fold() that optimizes comparisons against Infinities, |
6275 | either +Inf or -Inf. | |
6276 | ||
6277 | CODE is the comparison operator: EQ_EXPR, NE_EXPR, GT_EXPR, LT_EXPR, | |
6278 | GE_EXPR or LE_EXPR. TYPE is the type of the result and ARG0 and ARG1 | |
6279 | are the operands of the comparison. ARG1 must be a TREE_REAL_CST. | |
6280 | ||
6281 | The function returns the constant folded tree if a simplification | |
6282 | can be made, and NULL_TREE otherwise. */ | |
6283 | ||
6284 | static tree | |
389dd41b | 6285 | fold_inf_compare (location_t loc, enum tree_code code, tree type, |
6286 | tree arg0, tree arg1) | |
6d2e901f | 6287 | { |
ac4bd9a0 | 6288 | enum machine_mode mode; |
6289 | REAL_VALUE_TYPE max; | |
6290 | tree temp; | |
6291 | bool neg; | |
6292 | ||
6293 | mode = TYPE_MODE (TREE_TYPE (arg0)); | |
6294 | ||
6d2e901f | 6295 | /* For negative infinity swap the sense of the comparison. */ |
ac4bd9a0 | 6296 | neg = REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg1)); |
6297 | if (neg) | |
6d2e901f | 6298 | code = swap_tree_comparison (code); |
6299 | ||
6300 | switch (code) | |
6301 | { | |
6302 | case GT_EXPR: | |
6303 | /* x > +Inf is always false, if with ignore sNANs. */ | |
ac4bd9a0 | 6304 | if (HONOR_SNANS (mode)) |
6d2e901f | 6305 | return NULL_TREE; |
389dd41b | 6306 | return omit_one_operand_loc (loc, type, integer_zero_node, arg0); |
6d2e901f | 6307 | |
6308 | case LE_EXPR: | |
6309 | /* x <= +Inf is always true, if we don't case about NaNs. */ | |
ac4bd9a0 | 6310 | if (! HONOR_NANS (mode)) |
389dd41b | 6311 | return omit_one_operand_loc (loc, type, integer_one_node, arg0); |
6d2e901f | 6312 | |
6313 | /* x <= +Inf is the same as x == x, i.e. isfinite(x). */ | |
fa8b888f | 6314 | if (lang_hooks.decls.global_bindings_p () == 0 |
ce3fb06e | 6315 | && ! CONTAINS_PLACEHOLDER_P (arg0)) |
6d2e901f | 6316 | { |
6317 | arg0 = save_expr (arg0); | |
389dd41b | 6318 | return fold_build2_loc (loc, EQ_EXPR, type, arg0, arg0); |
6d2e901f | 6319 | } |
6320 | break; | |
6321 | ||
ac4bd9a0 | 6322 | case EQ_EXPR: |
6323 | case GE_EXPR: | |
6324 | /* x == +Inf and x >= +Inf are always equal to x > DBL_MAX. */ | |
6325 | real_maxval (&max, neg, mode); | |
389dd41b | 6326 | return fold_build2_loc (loc, neg ? LT_EXPR : GT_EXPR, type, |
7ab7fd4f | 6327 | arg0, build_real (TREE_TYPE (arg0), max)); |
ac4bd9a0 | 6328 | |
6329 | case LT_EXPR: | |
6330 | /* x < +Inf is always equal to x <= DBL_MAX. */ | |
6331 | real_maxval (&max, neg, mode); | |
389dd41b | 6332 | return fold_build2_loc (loc, neg ? GE_EXPR : LE_EXPR, type, |
7ab7fd4f | 6333 | arg0, build_real (TREE_TYPE (arg0), max)); |
ac4bd9a0 | 6334 | |
6335 | case NE_EXPR: | |
6336 | /* x != +Inf is always equal to !(x > DBL_MAX). */ | |
6337 | real_maxval (&max, neg, mode); | |
6338 | if (! HONOR_NANS (mode)) | |
389dd41b | 6339 | return fold_build2_loc (loc, neg ? GE_EXPR : LE_EXPR, type, |
7ab7fd4f | 6340 | arg0, build_real (TREE_TYPE (arg0), max)); |
bd1ec513 | 6341 | |
389dd41b | 6342 | temp = fold_build2_loc (loc, neg ? LT_EXPR : GT_EXPR, type, |
7ab7fd4f | 6343 | arg0, build_real (TREE_TYPE (arg0), max)); |
389dd41b | 6344 | return fold_build1_loc (loc, TRUTH_NOT_EXPR, type, temp); |
6d2e901f | 6345 | |
6346 | default: | |
6347 | break; | |
6348 | } | |
6349 | ||
6350 | return NULL_TREE; | |
6351 | } | |
920d0fb5 | 6352 | |
270029e0 | 6353 | /* Subroutine of fold() that optimizes comparisons of a division by |
365db11e | 6354 | a nonzero integer constant against an integer constant, i.e. |
270029e0 | 6355 | X/C1 op C2. |
6356 | ||
6357 | CODE is the comparison operator: EQ_EXPR, NE_EXPR, GT_EXPR, LT_EXPR, | |
6358 | GE_EXPR or LE_EXPR. TYPE is the type of the result and ARG0 and ARG1 | |
6359 | are the operands of the comparison. ARG1 must be a TREE_REAL_CST. | |
6360 | ||
6361 | The function returns the constant folded tree if a simplification | |
6362 | can be made, and NULL_TREE otherwise. */ | |
6363 | ||
6364 | static tree | |
389dd41b | 6365 | fold_div_compare (location_t loc, |
6366 | enum tree_code code, tree type, tree arg0, tree arg1) | |
270029e0 | 6367 | { |
6368 | tree prod, tmp, hi, lo; | |
6369 | tree arg00 = TREE_OPERAND (arg0, 0); | |
6370 | tree arg01 = TREE_OPERAND (arg0, 1); | |
6371 | unsigned HOST_WIDE_INT lpart; | |
6372 | HOST_WIDE_INT hpart; | |
81035ec1 | 6373 | bool unsigned_p = TYPE_UNSIGNED (TREE_TYPE (arg0)); |
4e35b483 | 6374 | bool neg_overflow; |
270029e0 | 6375 | int overflow; |
6376 | ||
6377 | /* We have to do this the hard way to detect unsigned overflow. | |
6378 | prod = int_const_binop (MULT_EXPR, arg01, arg1, 0); */ | |
81035ec1 | 6379 | overflow = mul_double_with_sign (TREE_INT_CST_LOW (arg01), |
6380 | TREE_INT_CST_HIGH (arg01), | |
6381 | TREE_INT_CST_LOW (arg1), | |
6382 | TREE_INT_CST_HIGH (arg1), | |
6383 | &lpart, &hpart, unsigned_p); | |
c8110c8f | 6384 | prod = force_fit_type_double (TREE_TYPE (arg00), lpart, hpart, |
eddad94a | 6385 | -1, overflow); |
4e35b483 | 6386 | neg_overflow = false; |
270029e0 | 6387 | |
81035ec1 | 6388 | if (unsigned_p) |
270029e0 | 6389 | { |
2455d3ef | 6390 | tmp = int_const_binop (MINUS_EXPR, arg01, |
6391 | build_int_cst (TREE_TYPE (arg01), 1), 0); | |
270029e0 | 6392 | lo = prod; |
6393 | ||
6394 | /* Likewise hi = int_const_binop (PLUS_EXPR, prod, tmp, 0). */ | |
81035ec1 | 6395 | overflow = add_double_with_sign (TREE_INT_CST_LOW (prod), |
6396 | TREE_INT_CST_HIGH (prod), | |
6397 | TREE_INT_CST_LOW (tmp), | |
6398 | TREE_INT_CST_HIGH (tmp), | |
6399 | &lpart, &hpart, unsigned_p); | |
c8110c8f | 6400 | hi = force_fit_type_double (TREE_TYPE (arg00), lpart, hpart, |
eddad94a | 6401 | -1, overflow | TREE_OVERFLOW (prod)); |
270029e0 | 6402 | } |
6403 | else if (tree_int_cst_sgn (arg01) >= 0) | |
6404 | { | |
2455d3ef | 6405 | tmp = int_const_binop (MINUS_EXPR, arg01, |
6406 | build_int_cst (TREE_TYPE (arg01), 1), 0); | |
270029e0 | 6407 | switch (tree_int_cst_sgn (arg1)) |
6408 | { | |
6409 | case -1: | |
4e35b483 | 6410 | neg_overflow = true; |
270029e0 | 6411 | lo = int_const_binop (MINUS_EXPR, prod, tmp, 0); |
6412 | hi = prod; | |
6413 | break; | |
6414 | ||
6415 | case 0: | |
6416 | lo = fold_negate_const (tmp, TREE_TYPE (arg0)); | |
6417 | hi = tmp; | |
6418 | break; | |
6419 | ||
6420 | case 1: | |
6421 | hi = int_const_binop (PLUS_EXPR, prod, tmp, 0); | |
6422 | lo = prod; | |
6423 | break; | |
6424 | ||
6425 | default: | |
fdada98f | 6426 | gcc_unreachable (); |
270029e0 | 6427 | } |
6428 | } | |
6429 | else | |
6430 | { | |
460c8e36 | 6431 | /* A negative divisor reverses the relational operators. */ |
6432 | code = swap_tree_comparison (code); | |
6433 | ||
2455d3ef | 6434 | tmp = int_const_binop (PLUS_EXPR, arg01, |
6435 | build_int_cst (TREE_TYPE (arg01), 1), 0); | |
270029e0 | 6436 | switch (tree_int_cst_sgn (arg1)) |
6437 | { | |
6438 | case -1: | |
6439 | hi = int_const_binop (MINUS_EXPR, prod, tmp, 0); | |
6440 | lo = prod; | |
6441 | break; | |
6442 | ||
6443 | case 0: | |
6444 | hi = fold_negate_const (tmp, TREE_TYPE (arg0)); | |
6445 | lo = tmp; | |
6446 | break; | |
6447 | ||
6448 | case 1: | |
4e35b483 | 6449 | neg_overflow = true; |
6450 | lo = int_const_binop (PLUS_EXPR, prod, tmp, 0); | |
270029e0 | 6451 | hi = prod; |
6452 | break; | |
6453 | ||
6454 | default: | |
fdada98f | 6455 | gcc_unreachable (); |
270029e0 | 6456 | } |
6457 | } | |
6458 | ||
6459 | switch (code) | |
6460 | { | |
6461 | case EQ_EXPR: | |
6462 | if (TREE_OVERFLOW (lo) && TREE_OVERFLOW (hi)) | |
389dd41b | 6463 | return omit_one_operand_loc (loc, type, integer_zero_node, arg00); |
270029e0 | 6464 | if (TREE_OVERFLOW (hi)) |
389dd41b | 6465 | return fold_build2_loc (loc, GE_EXPR, type, arg00, lo); |
270029e0 | 6466 | if (TREE_OVERFLOW (lo)) |
389dd41b | 6467 | return fold_build2_loc (loc, LE_EXPR, type, arg00, hi); |
6468 | return build_range_check (loc, type, arg00, 1, lo, hi); | |
270029e0 | 6469 | |
6470 | case NE_EXPR: | |
6471 | if (TREE_OVERFLOW (lo) && TREE_OVERFLOW (hi)) | |
389dd41b | 6472 | return omit_one_operand_loc (loc, type, integer_one_node, arg00); |
270029e0 | 6473 | if (TREE_OVERFLOW (hi)) |
389dd41b | 6474 | return fold_build2_loc (loc, LT_EXPR, type, arg00, lo); |
270029e0 | 6475 | if (TREE_OVERFLOW (lo)) |
389dd41b | 6476 | return fold_build2_loc (loc, GT_EXPR, type, arg00, hi); |
6477 | return build_range_check (loc, type, arg00, 0, lo, hi); | |
270029e0 | 6478 | |
6479 | case LT_EXPR: | |
6480 | if (TREE_OVERFLOW (lo)) | |
4e35b483 | 6481 | { |
6482 | tmp = neg_overflow ? integer_zero_node : integer_one_node; | |
389dd41b | 6483 | return omit_one_operand_loc (loc, type, tmp, arg00); |
4e35b483 | 6484 | } |
389dd41b | 6485 | return fold_build2_loc (loc, LT_EXPR, type, arg00, lo); |
270029e0 | 6486 | |
6487 | case LE_EXPR: | |
6488 | if (TREE_OVERFLOW (hi)) | |
4e35b483 | 6489 | { |
6490 | tmp = neg_overflow ? integer_zero_node : integer_one_node; | |
389dd41b | 6491 | return omit_one_operand_loc (loc, type, tmp, arg00); |
4e35b483 | 6492 | } |
389dd41b | 6493 | return fold_build2_loc (loc, LE_EXPR, type, arg00, hi); |
270029e0 | 6494 | |
6495 | case GT_EXPR: | |
6496 | if (TREE_OVERFLOW (hi)) | |
4e35b483 | 6497 | { |
6498 | tmp = neg_overflow ? integer_one_node : integer_zero_node; | |
389dd41b | 6499 | return omit_one_operand_loc (loc, type, tmp, arg00); |
4e35b483 | 6500 | } |
389dd41b | 6501 | return fold_build2_loc (loc, GT_EXPR, type, arg00, hi); |
270029e0 | 6502 | |
6503 | case GE_EXPR: | |
6504 | if (TREE_OVERFLOW (lo)) | |
4e35b483 | 6505 | { |
6506 | tmp = neg_overflow ? integer_one_node : integer_zero_node; | |
389dd41b | 6507 | return omit_one_operand_loc (loc, type, tmp, arg00); |
4e35b483 | 6508 | } |
389dd41b | 6509 | return fold_build2_loc (loc, GE_EXPR, type, arg00, lo); |
270029e0 | 6510 | |
6511 | default: | |
6512 | break; | |
6513 | } | |
6514 | ||
6515 | return NULL_TREE; | |
6516 | } | |
6517 | ||
6518 | ||
6881f973 | 6519 | /* If CODE with arguments ARG0 and ARG1 represents a single bit |
149f0db4 | 6520 | equality/inequality test, then return a simplified form of the test |
6521 | using a sign testing. Otherwise return NULL. TYPE is the desired | |
6522 | result type. */ | |
7206da1b | 6523 | |
149f0db4 | 6524 | static tree |
389dd41b | 6525 | fold_single_bit_test_into_sign_test (location_t loc, |
6526 | enum tree_code code, tree arg0, tree arg1, | |
149f0db4 | 6527 | tree result_type) |
6881f973 | 6528 | { |
6881f973 | 6529 | /* If this is testing a single bit, we can optimize the test. */ |
6530 | if ((code == NE_EXPR || code == EQ_EXPR) | |
6531 | && TREE_CODE (arg0) == BIT_AND_EXPR && integer_zerop (arg1) | |
6532 | && integer_pow2p (TREE_OPERAND (arg0, 1))) | |
6533 | { | |
6881f973 | 6534 | /* If we have (A & C) != 0 where C is the sign bit of A, convert |
6535 | this into A < 0. Similarly for (A & C) == 0 into A >= 0. */ | |
149f0db4 | 6536 | tree arg00 = sign_bit_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg0, 1)); |
6537 | ||
7cc00cbd | 6538 | if (arg00 != NULL_TREE |
6539 | /* This is only a win if casting to a signed type is cheap, | |
6540 | i.e. when arg00's type is not a partial mode. */ | |
6541 | && TYPE_PRECISION (TREE_TYPE (arg00)) | |
6542 | == GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (arg00)))) | |
6881f973 | 6543 | { |
11773141 | 6544 | tree stype = signed_type_for (TREE_TYPE (arg00)); |
389dd41b | 6545 | return fold_build2_loc (loc, code == EQ_EXPR ? GE_EXPR : LT_EXPR, |
6546 | result_type, | |
6547 | fold_convert_loc (loc, stype, arg00), | |
3c6185f1 | 6548 | build_int_cst (stype, 0)); |
6881f973 | 6549 | } |
149f0db4 | 6550 | } |
6551 | ||
6552 | return NULL_TREE; | |
6553 | } | |
6554 | ||
6555 | /* If CODE with arguments ARG0 and ARG1 represents a single bit | |
6556 | equality/inequality test, then return a simplified form of | |
6557 | the test using shifts and logical operations. Otherwise return | |
6558 | NULL. TYPE is the desired result type. */ | |
6559 | ||
6560 | tree | |
389dd41b | 6561 | fold_single_bit_test (location_t loc, enum tree_code code, |
6562 | tree arg0, tree arg1, tree result_type) | |
149f0db4 | 6563 | { |
6564 | /* If this is testing a single bit, we can optimize the test. */ | |
6565 | if ((code == NE_EXPR || code == EQ_EXPR) | |
6566 | && TREE_CODE (arg0) == BIT_AND_EXPR && integer_zerop (arg1) | |
6567 | && integer_pow2p (TREE_OPERAND (arg0, 1))) | |
6568 | { | |
6569 | tree inner = TREE_OPERAND (arg0, 0); | |
6570 | tree type = TREE_TYPE (arg0); | |
6571 | int bitnum = tree_log2 (TREE_OPERAND (arg0, 1)); | |
6572 | enum machine_mode operand_mode = TYPE_MODE (type); | |
6573 | int ops_unsigned; | |
6574 | tree signed_type, unsigned_type, intermediate_type; | |
2455d3ef | 6575 | tree tem, one; |
149f0db4 | 6576 | |
6577 | /* First, see if we can fold the single bit test into a sign-bit | |
6578 | test. */ | |
389dd41b | 6579 | tem = fold_single_bit_test_into_sign_test (loc, code, arg0, arg1, |
149f0db4 | 6580 | result_type); |
6581 | if (tem) | |
6582 | return tem; | |
a4de5624 | 6583 | |
7206da1b | 6584 | /* Otherwise we have (A & C) != 0 where C is a single bit, |
6881f973 | 6585 | convert that into ((A >> C2) & 1). Where C2 = log2(C). |
6586 | Similarly for (A & C) == 0. */ | |
6587 | ||
6588 | /* If INNER is a right shift of a constant and it plus BITNUM does | |
6589 | not overflow, adjust BITNUM and INNER. */ | |
6590 | if (TREE_CODE (inner) == RSHIFT_EXPR | |
6591 | && TREE_CODE (TREE_OPERAND (inner, 1)) == INTEGER_CST | |
6592 | && TREE_INT_CST_HIGH (TREE_OPERAND (inner, 1)) == 0 | |
6593 | && bitnum < TYPE_PRECISION (type) | |
6594 | && 0 > compare_tree_int (TREE_OPERAND (inner, 1), | |
6595 | bitnum - TYPE_PRECISION (type))) | |
6596 | { | |
6597 | bitnum += TREE_INT_CST_LOW (TREE_OPERAND (inner, 1)); | |
6598 | inner = TREE_OPERAND (inner, 0); | |
6599 | } | |
6600 | ||
6601 | /* If we are going to be able to omit the AND below, we must do our | |
6602 | operations as unsigned. If we must use the AND, we have a choice. | |
6603 | Normally unsigned is faster, but for some machines signed is. */ | |
6881f973 | 6604 | #ifdef LOAD_EXTEND_OP |
48e1416a | 6605 | ops_unsigned = (LOAD_EXTEND_OP (operand_mode) == SIGN_EXTEND |
a38d3d8b | 6606 | && !flag_syntax_only) ? 0 : 1; |
6881f973 | 6607 | #else |
a4de5624 | 6608 | ops_unsigned = 1; |
6881f973 | 6609 | #endif |
6881f973 | 6610 | |
fa8b888f | 6611 | signed_type = lang_hooks.types.type_for_mode (operand_mode, 0); |
6612 | unsigned_type = lang_hooks.types.type_for_mode (operand_mode, 1); | |
654d0fed | 6613 | intermediate_type = ops_unsigned ? unsigned_type : signed_type; |
389dd41b | 6614 | inner = fold_convert_loc (loc, intermediate_type, inner); |
6881f973 | 6615 | |
6616 | if (bitnum != 0) | |
fd96eeef | 6617 | inner = build2 (RSHIFT_EXPR, intermediate_type, |
6618 | inner, size_int (bitnum)); | |
6881f973 | 6619 | |
2455d3ef | 6620 | one = build_int_cst (intermediate_type, 1); |
6621 | ||
6881f973 | 6622 | if (code == EQ_EXPR) |
389dd41b | 6623 | inner = fold_build2_loc (loc, BIT_XOR_EXPR, intermediate_type, inner, one); |
6881f973 | 6624 | |
6625 | /* Put the AND last so it can combine with more things. */ | |
2455d3ef | 6626 | inner = build2 (BIT_AND_EXPR, intermediate_type, inner, one); |
6881f973 | 6627 | |
6628 | /* Make sure to return the proper type. */ | |
389dd41b | 6629 | inner = fold_convert_loc (loc, result_type, inner); |
6881f973 | 6630 | |
6631 | return inner; | |
6632 | } | |
6633 | return NULL_TREE; | |
6634 | } | |
fc3df357 | 6635 | |
bd214d13 | 6636 | /* Check whether we are allowed to reorder operands arg0 and arg1, |
6637 | such that the evaluation of arg1 occurs before arg0. */ | |
6638 | ||
6639 | static bool | |
b4b34335 | 6640 | reorder_operands_p (const_tree arg0, const_tree arg1) |
bd214d13 | 6641 | { |
6642 | if (! flag_evaluation_order) | |
0c5713a2 | 6643 | return true; |
bd214d13 | 6644 | if (TREE_CONSTANT (arg0) || TREE_CONSTANT (arg1)) |
6645 | return true; | |
6646 | return ! TREE_SIDE_EFFECTS (arg0) | |
6647 | && ! TREE_SIDE_EFFECTS (arg1); | |
6648 | } | |
6649 | ||
88e11d8f | 6650 | /* Test whether it is preferable two swap two operands, ARG0 and |
6651 | ARG1, for example because ARG0 is an integer constant and ARG1 | |
bd214d13 | 6652 | isn't. If REORDER is true, only recommend swapping if we can |
6653 | evaluate the operands in reverse order. */ | |
88e11d8f | 6654 | |
cc0bdf91 | 6655 | bool |
b7bf20db | 6656 | tree_swap_operands_p (const_tree arg0, const_tree arg1, bool reorder) |
88e11d8f | 6657 | { |
6658 | STRIP_SIGN_NOPS (arg0); | |
6659 | STRIP_SIGN_NOPS (arg1); | |
6660 | ||
6661 | if (TREE_CODE (arg1) == INTEGER_CST) | |
6662 | return 0; | |
6663 | if (TREE_CODE (arg0) == INTEGER_CST) | |
6664 | return 1; | |
6665 | ||
6666 | if (TREE_CODE (arg1) == REAL_CST) | |
6667 | return 0; | |
6668 | if (TREE_CODE (arg0) == REAL_CST) | |
6669 | return 1; | |
6670 | ||
06f0b99c | 6671 | if (TREE_CODE (arg1) == FIXED_CST) |
6672 | return 0; | |
6673 | if (TREE_CODE (arg0) == FIXED_CST) | |
6674 | return 1; | |
6675 | ||
88e11d8f | 6676 | if (TREE_CODE (arg1) == COMPLEX_CST) |
6677 | return 0; | |
6678 | if (TREE_CODE (arg0) == COMPLEX_CST) | |
6679 | return 1; | |
6680 | ||
6681 | if (TREE_CONSTANT (arg1)) | |
6682 | return 0; | |
6683 | if (TREE_CONSTANT (arg0)) | |
6684 | return 1; | |
7206da1b | 6685 | |
ad4341e8 | 6686 | if (optimize_function_for_size_p (cfun)) |
f9464d30 | 6687 | return 0; |
88e11d8f | 6688 | |
bd214d13 | 6689 | if (reorder && flag_evaluation_order |
6690 | && (TREE_SIDE_EFFECTS (arg0) || TREE_SIDE_EFFECTS (arg1))) | |
6691 | return 0; | |
6692 | ||
cc0bdf91 | 6693 | /* It is preferable to swap two SSA_NAME to ensure a canonical form |
6694 | for commutative and comparison operators. Ensuring a canonical | |
6695 | form allows the optimizers to find additional redundancies without | |
6696 | having to explicitly check for both orderings. */ | |
6697 | if (TREE_CODE (arg0) == SSA_NAME | |
6698 | && TREE_CODE (arg1) == SSA_NAME | |
6699 | && SSA_NAME_VERSION (arg0) > SSA_NAME_VERSION (arg1)) | |
6700 | return 1; | |
6701 | ||
d1d2495d | 6702 | /* Put SSA_NAMEs last. */ |
6703 | if (TREE_CODE (arg1) == SSA_NAME) | |
6704 | return 0; | |
6705 | if (TREE_CODE (arg0) == SSA_NAME) | |
6706 | return 1; | |
6707 | ||
6708 | /* Put variables last. */ | |
6709 | if (DECL_P (arg1)) | |
6710 | return 0; | |
6711 | if (DECL_P (arg0)) | |
6712 | return 1; | |
6713 | ||
88e11d8f | 6714 | return 0; |
6715 | } | |
6716 | ||
faab57e3 | 6717 | /* Fold comparison ARG0 CODE ARG1 (with result in TYPE), where |
6718 | ARG0 is extended to a wider type. */ | |
6719 | ||
6720 | static tree | |
389dd41b | 6721 | fold_widened_comparison (location_t loc, enum tree_code code, |
6722 | tree type, tree arg0, tree arg1) | |
faab57e3 | 6723 | { |
6724 | tree arg0_unw = get_unwidened (arg0, NULL_TREE); | |
6725 | tree arg1_unw; | |
6726 | tree shorter_type, outer_type; | |
6727 | tree min, max; | |
6728 | bool above, below; | |
6729 | ||
6730 | if (arg0_unw == arg0) | |
6731 | return NULL_TREE; | |
6732 | shorter_type = TREE_TYPE (arg0_unw); | |
fd66f095 | 6733 | |
085bb6ea | 6734 | #ifdef HAVE_canonicalize_funcptr_for_compare |
6735 | /* Disable this optimization if we're casting a function pointer | |
6736 | type on targets that require function pointer canonicalization. */ | |
6737 | if (HAVE_canonicalize_funcptr_for_compare | |
6738 | && TREE_CODE (shorter_type) == POINTER_TYPE | |
6739 | && TREE_CODE (TREE_TYPE (shorter_type)) == FUNCTION_TYPE) | |
6740 | return NULL_TREE; | |
6741 | #endif | |
6742 | ||
fd66f095 | 6743 | if (TYPE_PRECISION (TREE_TYPE (arg0)) <= TYPE_PRECISION (shorter_type)) |
6744 | return NULL_TREE; | |
6745 | ||
d06ed0b1 | 6746 | arg1_unw = get_unwidened (arg1, NULL_TREE); |
faab57e3 | 6747 | |
6748 | /* If possible, express the comparison in the shorter mode. */ | |
6749 | if ((code == EQ_EXPR || code == NE_EXPR | |
6750 | || TYPE_UNSIGNED (TREE_TYPE (arg0)) == TYPE_UNSIGNED (shorter_type)) | |
6751 | && (TREE_TYPE (arg1_unw) == shorter_type | |
ff5dd140 | 6752 | || ((TYPE_PRECISION (shorter_type) |
7ef0e31e | 6753 | >= TYPE_PRECISION (TREE_TYPE (arg1_unw))) |
ff5dd140 | 6754 | && (TYPE_UNSIGNED (shorter_type) |
6755 | == TYPE_UNSIGNED (TREE_TYPE (arg1_unw)))) | |
faab57e3 | 6756 | || (TREE_CODE (arg1_unw) == INTEGER_CST |
66787d4f | 6757 | && (TREE_CODE (shorter_type) == INTEGER_TYPE |
6758 | || TREE_CODE (shorter_type) == BOOLEAN_TYPE) | |
faab57e3 | 6759 | && int_fits_type_p (arg1_unw, shorter_type)))) |
389dd41b | 6760 | return fold_build2_loc (loc, code, type, arg0_unw, |
6761 | fold_convert_loc (loc, shorter_type, arg1_unw)); | |
faab57e3 | 6762 | |
a5543a83 | 6763 | if (TREE_CODE (arg1_unw) != INTEGER_CST |
6764 | || TREE_CODE (shorter_type) != INTEGER_TYPE | |
6765 | || !int_fits_type_p (arg1_unw, shorter_type)) | |
faab57e3 | 6766 | return NULL_TREE; |
6767 | ||
6768 | /* If we are comparing with the integer that does not fit into the range | |
6769 | of the shorter type, the result is known. */ | |
6770 | outer_type = TREE_TYPE (arg1_unw); | |
6771 | min = lower_bound_in_type (outer_type, shorter_type); | |
6772 | max = upper_bound_in_type (outer_type, shorter_type); | |
6773 | ||
6774 | above = integer_nonzerop (fold_relational_const (LT_EXPR, type, | |
6775 | max, arg1_unw)); | |
6776 | below = integer_nonzerop (fold_relational_const (LT_EXPR, type, | |
6777 | arg1_unw, min)); | |
6778 | ||
6779 | switch (code) | |
6780 | { | |
6781 | case EQ_EXPR: | |
6782 | if (above || below) | |
389dd41b | 6783 | return omit_one_operand_loc (loc, type, integer_zero_node, arg0); |
faab57e3 | 6784 | break; |
6785 | ||
6786 | case NE_EXPR: | |
6787 | if (above || below) | |
389dd41b | 6788 | return omit_one_operand_loc (loc, type, integer_one_node, arg0); |
faab57e3 | 6789 | break; |
6790 | ||
6791 | case LT_EXPR: | |
6792 | case LE_EXPR: | |
6793 | if (above) | |
389dd41b | 6794 | return omit_one_operand_loc (loc, type, integer_one_node, arg0); |
faab57e3 | 6795 | else if (below) |
389dd41b | 6796 | return omit_one_operand_loc (loc, type, integer_zero_node, arg0); |
faab57e3 | 6797 | |
6798 | case GT_EXPR: | |
6799 | case GE_EXPR: | |
6800 | if (above) | |
389dd41b | 6801 | return omit_one_operand_loc (loc, type, integer_zero_node, arg0); |
faab57e3 | 6802 | else if (below) |
389dd41b | 6803 | return omit_one_operand_loc (loc, type, integer_one_node, arg0); |
faab57e3 | 6804 | |
6805 | default: | |
6806 | break; | |
6807 | } | |
6808 | ||
6809 | return NULL_TREE; | |
6810 | } | |
6811 | ||
6812 | /* Fold comparison ARG0 CODE ARG1 (with result in TYPE), where for | |
6813 | ARG0 just the signedness is changed. */ | |
6814 | ||
6815 | static tree | |
389dd41b | 6816 | fold_sign_changed_comparison (location_t loc, enum tree_code code, tree type, |
faab57e3 | 6817 | tree arg0, tree arg1) |
6818 | { | |
c8110c8f | 6819 | tree arg0_inner; |
faab57e3 | 6820 | tree inner_type, outer_type; |
6821 | ||
72dd6141 | 6822 | if (!CONVERT_EXPR_P (arg0)) |
faab57e3 | 6823 | return NULL_TREE; |
6824 | ||
6825 | outer_type = TREE_TYPE (arg0); | |
6826 | arg0_inner = TREE_OPERAND (arg0, 0); | |
6827 | inner_type = TREE_TYPE (arg0_inner); | |
6828 | ||
085bb6ea | 6829 | #ifdef HAVE_canonicalize_funcptr_for_compare |
6830 | /* Disable this optimization if we're casting a function pointer | |
6831 | type on targets that require function pointer canonicalization. */ | |
6832 | if (HAVE_canonicalize_funcptr_for_compare | |
6833 | && TREE_CODE (inner_type) == POINTER_TYPE | |
6834 | && TREE_CODE (TREE_TYPE (inner_type)) == FUNCTION_TYPE) | |
6835 | return NULL_TREE; | |
6836 | #endif | |
6837 | ||
faab57e3 | 6838 | if (TYPE_PRECISION (inner_type) != TYPE_PRECISION (outer_type)) |
6839 | return NULL_TREE; | |
6840 | ||
6841 | if (TREE_CODE (arg1) != INTEGER_CST | |
72dd6141 | 6842 | && !(CONVERT_EXPR_P (arg1) |
faab57e3 | 6843 | && TREE_TYPE (TREE_OPERAND (arg1, 0)) == inner_type)) |
6844 | return NULL_TREE; | |
6845 | ||
4b475af8 | 6846 | if ((TYPE_UNSIGNED (inner_type) != TYPE_UNSIGNED (outer_type) |
6847 | || POINTER_TYPE_P (inner_type) != POINTER_TYPE_P (outer_type)) | |
faab57e3 | 6848 | && code != NE_EXPR |
6849 | && code != EQ_EXPR) | |
6850 | return NULL_TREE; | |
6851 | ||
6852 | if (TREE_CODE (arg1) == INTEGER_CST) | |
c8110c8f | 6853 | arg1 = force_fit_type_double (inner_type, TREE_INT_CST_LOW (arg1), |
6854 | TREE_INT_CST_HIGH (arg1), 0, | |
eddad94a | 6855 | TREE_OVERFLOW (arg1)); |
faab57e3 | 6856 | else |
389dd41b | 6857 | arg1 = fold_convert_loc (loc, inner_type, arg1); |
faab57e3 | 6858 | |
389dd41b | 6859 | return fold_build2_loc (loc, code, type, arg0_inner, arg1); |
faab57e3 | 6860 | } |
6861 | ||
0de36bdb | 6862 | /* Tries to replace &a[idx] p+ s * delta with &a[idx + delta], if s is |
389dd41b | 6863 | step of the array. Reconstructs s and delta in the case of s * |
6864 | delta being an integer constant (and thus already folded). ADDR is | |
6865 | the address. MULT is the multiplicative expression. If the | |
6866 | function succeeds, the new address expression is returned. | |
6867 | Otherwise NULL_TREE is returned. LOC is the location of the | |
6868 | resulting expression. */ | |
dede8dcc | 6869 | |
6870 | static tree | |
389dd41b | 6871 | try_move_mult_to_index (location_t loc, tree addr, tree op1) |
dede8dcc | 6872 | { |
6873 | tree s, delta, step; | |
dede8dcc | 6874 | tree ref = TREE_OPERAND (addr, 0), pref; |
6875 | tree ret, pos; | |
6876 | tree itype; | |
98f4d382 | 6877 | bool mdim = false; |
dede8dcc | 6878 | |
0de36bdb | 6879 | /* Strip the nops that might be added when converting op1 to sizetype. */ |
6880 | STRIP_NOPS (op1); | |
6881 | ||
ad1f9c12 | 6882 | /* Canonicalize op1 into a possibly non-constant delta |
6883 | and an INTEGER_CST s. */ | |
6884 | if (TREE_CODE (op1) == MULT_EXPR) | |
dede8dcc | 6885 | { |
ad1f9c12 | 6886 | tree arg0 = TREE_OPERAND (op1, 0), arg1 = TREE_OPERAND (op1, 1); |
6887 | ||
6888 | STRIP_NOPS (arg0); | |
6889 | STRIP_NOPS (arg1); | |
48e1416a | 6890 | |
ad1f9c12 | 6891 | if (TREE_CODE (arg0) == INTEGER_CST) |
6892 | { | |
6893 | s = arg0; | |
6894 | delta = arg1; | |
6895 | } | |
6896 | else if (TREE_CODE (arg1) == INTEGER_CST) | |
6897 | { | |
6898 | s = arg1; | |
6899 | delta = arg0; | |
6900 | } | |
6901 | else | |
6902 | return NULL_TREE; | |
dede8dcc | 6903 | } |
ad1f9c12 | 6904 | else if (TREE_CODE (op1) == INTEGER_CST) |
dede8dcc | 6905 | { |
ad1f9c12 | 6906 | delta = op1; |
6907 | s = NULL_TREE; | |
dede8dcc | 6908 | } |
6909 | else | |
ad1f9c12 | 6910 | { |
6911 | /* Simulate we are delta * 1. */ | |
6912 | delta = op1; | |
6913 | s = integer_one_node; | |
6914 | } | |
dede8dcc | 6915 | |
6916 | for (;; ref = TREE_OPERAND (ref, 0)) | |
6917 | { | |
6918 | if (TREE_CODE (ref) == ARRAY_REF) | |
6919 | { | |
5b36834c | 6920 | tree domain; |
6921 | ||
98f4d382 | 6922 | /* Remember if this was a multi-dimensional array. */ |
6923 | if (TREE_CODE (TREE_OPERAND (ref, 0)) == ARRAY_REF) | |
6924 | mdim = true; | |
6925 | ||
5b36834c | 6926 | domain = TYPE_DOMAIN (TREE_TYPE (TREE_OPERAND (ref, 0))); |
6927 | if (! domain) | |
86f023fe | 6928 | continue; |
5b36834c | 6929 | itype = TREE_TYPE (domain); |
86f023fe | 6930 | |
dede8dcc | 6931 | step = array_ref_element_size (ref); |
dede8dcc | 6932 | if (TREE_CODE (step) != INTEGER_CST) |
6933 | continue; | |
6934 | ||
ad1f9c12 | 6935 | if (s) |
6936 | { | |
6937 | if (! tree_int_cst_equal (step, s)) | |
6938 | continue; | |
6939 | } | |
6940 | else | |
6941 | { | |
6942 | /* Try if delta is a multiple of step. */ | |
5791999f | 6943 | tree tmp = div_if_zero_remainder (EXACT_DIV_EXPR, op1, step); |
86f023fe | 6944 | if (! tmp) |
ad1f9c12 | 6945 | continue; |
86f023fe | 6946 | delta = tmp; |
ad1f9c12 | 6947 | } |
dede8dcc | 6948 | |
98f4d382 | 6949 | /* Only fold here if we can verify we do not overflow one |
6950 | dimension of a multi-dimensional array. */ | |
6951 | if (mdim) | |
6952 | { | |
6953 | tree tmp; | |
6954 | ||
6955 | if (TREE_CODE (TREE_OPERAND (ref, 1)) != INTEGER_CST | |
5b36834c | 6956 | || !TYPE_MAX_VALUE (domain) |
6957 | || TREE_CODE (TYPE_MAX_VALUE (domain)) != INTEGER_CST) | |
98f4d382 | 6958 | continue; |
6959 | ||
389dd41b | 6960 | tmp = fold_binary_loc (loc, PLUS_EXPR, itype, |
5b36834c | 6961 | fold_convert_loc (loc, itype, |
6962 | TREE_OPERAND (ref, 1)), | |
6963 | fold_convert_loc (loc, itype, delta)); | |
98f4d382 | 6964 | if (!tmp |
6965 | || TREE_CODE (tmp) != INTEGER_CST | |
5b36834c | 6966 | || tree_int_cst_lt (TYPE_MAX_VALUE (domain), tmp)) |
98f4d382 | 6967 | continue; |
6968 | } | |
6969 | ||
dede8dcc | 6970 | break; |
6971 | } | |
98f4d382 | 6972 | else |
6973 | mdim = false; | |
dede8dcc | 6974 | |
6975 | if (!handled_component_p (ref)) | |
6976 | return NULL_TREE; | |
6977 | } | |
6978 | ||
6979 | /* We found the suitable array reference. So copy everything up to it, | |
6980 | and replace the index. */ | |
6981 | ||
6982 | pref = TREE_OPERAND (addr, 0); | |
6983 | ret = copy_node (pref); | |
389dd41b | 6984 | SET_EXPR_LOCATION (ret, loc); |
dede8dcc | 6985 | pos = ret; |
6986 | ||
6987 | while (pref != ref) | |
6988 | { | |
6989 | pref = TREE_OPERAND (pref, 0); | |
6990 | TREE_OPERAND (pos, 0) = copy_node (pref); | |
6991 | pos = TREE_OPERAND (pos, 0); | |
6992 | } | |
6993 | ||
389dd41b | 6994 | TREE_OPERAND (pos, 1) = fold_build2_loc (loc, PLUS_EXPR, itype, |
6995 | fold_convert_loc (loc, itype, | |
6996 | TREE_OPERAND (pos, 1)), | |
6997 | fold_convert_loc (loc, itype, delta)); | |
dede8dcc | 6998 | |
389dd41b | 6999 | return fold_build1_loc (loc, ADDR_EXPR, TREE_TYPE (addr), ret); |
dede8dcc | 7000 | } |
7001 | ||
9a73db25 | 7002 | |
7003 | /* Fold A < X && A + 1 > Y to A < X && A >= Y. Normally A + 1 > Y | |
7004 | means A >= Y && A != MAX, but in this case we know that | |
7005 | A < X <= MAX. INEQ is A + 1 > Y, BOUND is A < X. */ | |
7006 | ||
7007 | static tree | |
389dd41b | 7008 | fold_to_nonsharp_ineq_using_bound (location_t loc, tree ineq, tree bound) |
9a73db25 | 7009 | { |
7010 | tree a, typea, type = TREE_TYPE (ineq), a1, diff, y; | |
7011 | ||
7012 | if (TREE_CODE (bound) == LT_EXPR) | |
7013 | a = TREE_OPERAND (bound, 0); | |
7014 | else if (TREE_CODE (bound) == GT_EXPR) | |
7015 | a = TREE_OPERAND (bound, 1); | |
7016 | else | |
7017 | return NULL_TREE; | |
7018 | ||
7019 | typea = TREE_TYPE (a); | |
7020 | if (!INTEGRAL_TYPE_P (typea) | |
7021 | && !POINTER_TYPE_P (typea)) | |
7022 | return NULL_TREE; | |
7023 | ||
7024 | if (TREE_CODE (ineq) == LT_EXPR) | |
7025 | { | |
7026 | a1 = TREE_OPERAND (ineq, 1); | |
7027 | y = TREE_OPERAND (ineq, 0); | |
7028 | } | |
7029 | else if (TREE_CODE (ineq) == GT_EXPR) | |
7030 | { | |
7031 | a1 = TREE_OPERAND (ineq, 0); | |
7032 | y = TREE_OPERAND (ineq, 1); | |
7033 | } | |
7034 | else | |
7035 | return NULL_TREE; | |
7036 | ||
7037 | if (TREE_TYPE (a1) != typea) | |
7038 | return NULL_TREE; | |
7039 | ||
0de36bdb | 7040 | if (POINTER_TYPE_P (typea)) |
7041 | { | |
7042 | /* Convert the pointer types into integer before taking the difference. */ | |
389dd41b | 7043 | tree ta = fold_convert_loc (loc, ssizetype, a); |
7044 | tree ta1 = fold_convert_loc (loc, ssizetype, a1); | |
7045 | diff = fold_binary_loc (loc, MINUS_EXPR, ssizetype, ta1, ta); | |
0de36bdb | 7046 | } |
7047 | else | |
389dd41b | 7048 | diff = fold_binary_loc (loc, MINUS_EXPR, typea, a1, a); |
0de36bdb | 7049 | |
7050 | if (!diff || !integer_onep (diff)) | |
7051 | return NULL_TREE; | |
9a73db25 | 7052 | |
389dd41b | 7053 | return fold_build2_loc (loc, GE_EXPR, type, a, y); |
9a73db25 | 7054 | } |
7055 | ||
1c9af531 | 7056 | /* Fold a sum or difference of at least one multiplication. |
7057 | Returns the folded tree or NULL if no simplification could be made. */ | |
7058 | ||
7059 | static tree | |
389dd41b | 7060 | fold_plusminus_mult_expr (location_t loc, enum tree_code code, tree type, |
7061 | tree arg0, tree arg1) | |
1c9af531 | 7062 | { |
7063 | tree arg00, arg01, arg10, arg11; | |
7064 | tree alt0 = NULL_TREE, alt1 = NULL_TREE, same; | |
7065 | ||
7066 | /* (A * C) +- (B * C) -> (A+-B) * C. | |
7067 | (A * C) +- A -> A * (C+-1). | |
7068 | We are most concerned about the case where C is a constant, | |
7069 | but other combinations show up during loop reduction. Since | |
7070 | it is not difficult, try all four possibilities. */ | |
7071 | ||
7072 | if (TREE_CODE (arg0) == MULT_EXPR) | |
7073 | { | |
7074 | arg00 = TREE_OPERAND (arg0, 0); | |
7075 | arg01 = TREE_OPERAND (arg0, 1); | |
7076 | } | |
efd4cd99 | 7077 | else if (TREE_CODE (arg0) == INTEGER_CST) |
7078 | { | |
7079 | arg00 = build_one_cst (type); | |
7080 | arg01 = arg0; | |
7081 | } | |
1c9af531 | 7082 | else |
7083 | { | |
06f0b99c | 7084 | /* We cannot generate constant 1 for fract. */ |
7085 | if (ALL_FRACT_MODE_P (TYPE_MODE (type))) | |
7086 | return NULL_TREE; | |
1c9af531 | 7087 | arg00 = arg0; |
ba56cb50 | 7088 | arg01 = build_one_cst (type); |
1c9af531 | 7089 | } |
7090 | if (TREE_CODE (arg1) == MULT_EXPR) | |
7091 | { | |
7092 | arg10 = TREE_OPERAND (arg1, 0); | |
7093 | arg11 = TREE_OPERAND (arg1, 1); | |
7094 | } | |
efd4cd99 | 7095 | else if (TREE_CODE (arg1) == INTEGER_CST) |
7096 | { | |
7097 | arg10 = build_one_cst (type); | |
4f1351a2 | 7098 | /* As we canonicalize A - 2 to A + -2 get rid of that sign for |
7099 | the purpose of this canonicalization. */ | |
7100 | if (TREE_INT_CST_HIGH (arg1) == -1 | |
7101 | && negate_expr_p (arg1) | |
7102 | && code == PLUS_EXPR) | |
7103 | { | |
7104 | arg11 = negate_expr (arg1); | |
7105 | code = MINUS_EXPR; | |
7106 | } | |
7107 | else | |
7108 | arg11 = arg1; | |
efd4cd99 | 7109 | } |
1c9af531 | 7110 | else |
7111 | { | |
06f0b99c | 7112 | /* We cannot generate constant 1 for fract. */ |
7113 | if (ALL_FRACT_MODE_P (TYPE_MODE (type))) | |
7114 | return NULL_TREE; | |
1c9af531 | 7115 | arg10 = arg1; |
ba56cb50 | 7116 | arg11 = build_one_cst (type); |
1c9af531 | 7117 | } |
7118 | same = NULL_TREE; | |
7119 | ||
7120 | if (operand_equal_p (arg01, arg11, 0)) | |
7121 | same = arg01, alt0 = arg00, alt1 = arg10; | |
7122 | else if (operand_equal_p (arg00, arg10, 0)) | |
7123 | same = arg00, alt0 = arg01, alt1 = arg11; | |
7124 | else if (operand_equal_p (arg00, arg11, 0)) | |
7125 | same = arg00, alt0 = arg01, alt1 = arg10; | |
7126 | else if (operand_equal_p (arg01, arg10, 0)) | |
7127 | same = arg01, alt0 = arg00, alt1 = arg11; | |
7128 | ||
7129 | /* No identical multiplicands; see if we can find a common | |
7130 | power-of-two factor in non-power-of-two multiplies. This | |
7131 | can help in multi-dimensional array access. */ | |
7132 | else if (host_integerp (arg01, 0) | |
7133 | && host_integerp (arg11, 0)) | |
7134 | { | |
7135 | HOST_WIDE_INT int01, int11, tmp; | |
7136 | bool swap = false; | |
7137 | tree maybe_same; | |
7138 | int01 = TREE_INT_CST_LOW (arg01); | |
7139 | int11 = TREE_INT_CST_LOW (arg11); | |
7140 | ||
7141 | /* Move min of absolute values to int11. */ | |
7142 | if ((int01 >= 0 ? int01 : -int01) | |
7143 | < (int11 >= 0 ? int11 : -int11)) | |
7144 | { | |
7145 | tmp = int01, int01 = int11, int11 = tmp; | |
7146 | alt0 = arg00, arg00 = arg10, arg10 = alt0; | |
7147 | maybe_same = arg01; | |
7148 | swap = true; | |
7149 | } | |
7150 | else | |
7151 | maybe_same = arg11; | |
7152 | ||
04b63ffe | 7153 | if (exact_log2 (abs (int11)) > 0 && int01 % int11 == 0 |
7154 | /* The remainder should not be a constant, otherwise we | |
7155 | end up folding i * 4 + 2 to (i * 2 + 1) * 2 which has | |
7156 | increased the number of multiplications necessary. */ | |
7157 | && TREE_CODE (arg10) != INTEGER_CST) | |
1c9af531 | 7158 | { |
389dd41b | 7159 | alt0 = fold_build2_loc (loc, MULT_EXPR, TREE_TYPE (arg00), arg00, |
1c9af531 | 7160 | build_int_cst (TREE_TYPE (arg00), |
7161 | int01 / int11)); | |
7162 | alt1 = arg10; | |
7163 | same = maybe_same; | |
7164 | if (swap) | |
7165 | maybe_same = alt0, alt0 = alt1, alt1 = maybe_same; | |
7166 | } | |
7167 | } | |
7168 | ||
7169 | if (same) | |
389dd41b | 7170 | return fold_build2_loc (loc, MULT_EXPR, type, |
7171 | fold_build2_loc (loc, code, type, | |
7172 | fold_convert_loc (loc, type, alt0), | |
7173 | fold_convert_loc (loc, type, alt1)), | |
7174 | fold_convert_loc (loc, type, same)); | |
1c9af531 | 7175 | |
7176 | return NULL_TREE; | |
7177 | } | |
7178 | ||
5f4092ed | 7179 | /* Subroutine of native_encode_expr. Encode the INTEGER_CST |
7180 | specified by EXPR into the buffer PTR of length LEN bytes. | |
7181 | Return the number of bytes placed in the buffer, or zero | |
7182 | upon failure. */ | |
7183 | ||
7184 | static int | |
b7bf20db | 7185 | native_encode_int (const_tree expr, unsigned char *ptr, int len) |
5f4092ed | 7186 | { |
7187 | tree type = TREE_TYPE (expr); | |
7188 | int total_bytes = GET_MODE_SIZE (TYPE_MODE (type)); | |
7189 | int byte, offset, word, words; | |
7190 | unsigned char value; | |
7191 | ||
7192 | if (total_bytes > len) | |
7193 | return 0; | |
7194 | words = total_bytes / UNITS_PER_WORD; | |
7195 | ||
7196 | for (byte = 0; byte < total_bytes; byte++) | |
7197 | { | |
7198 | int bitpos = byte * BITS_PER_UNIT; | |
7199 | if (bitpos < HOST_BITS_PER_WIDE_INT) | |
7200 | value = (unsigned char) (TREE_INT_CST_LOW (expr) >> bitpos); | |
7201 | else | |
7202 | value = (unsigned char) (TREE_INT_CST_HIGH (expr) | |
7203 | >> (bitpos - HOST_BITS_PER_WIDE_INT)); | |
7204 | ||
7205 | if (total_bytes > UNITS_PER_WORD) | |
7206 | { | |
7207 | word = byte / UNITS_PER_WORD; | |
7208 | if (WORDS_BIG_ENDIAN) | |
7209 | word = (words - 1) - word; | |
7210 | offset = word * UNITS_PER_WORD; | |
7211 | if (BYTES_BIG_ENDIAN) | |
7212 | offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD); | |
7213 | else | |
7214 | offset += byte % UNITS_PER_WORD; | |
7215 | } | |
7216 | else | |
7217 | offset = BYTES_BIG_ENDIAN ? (total_bytes - 1) - byte : byte; | |
7218 | ptr[offset] = value; | |
7219 | } | |
7220 | return total_bytes; | |
7221 | } | |
7222 | ||
7223 | ||
7224 | /* Subroutine of native_encode_expr. Encode the REAL_CST | |
7225 | specified by EXPR into the buffer PTR of length LEN bytes. | |
7226 | Return the number of bytes placed in the buffer, or zero | |
7227 | upon failure. */ | |
7228 | ||
7229 | static int | |
b7bf20db | 7230 | native_encode_real (const_tree expr, unsigned char *ptr, int len) |
5f4092ed | 7231 | { |
7232 | tree type = TREE_TYPE (expr); | |
7233 | int total_bytes = GET_MODE_SIZE (TYPE_MODE (type)); | |
f83924bb | 7234 | int byte, offset, word, words, bitpos; |
5f4092ed | 7235 | unsigned char value; |
7236 | ||
7237 | /* There are always 32 bits in each long, no matter the size of | |
7238 | the hosts long. We handle floating point representations with | |
7239 | up to 192 bits. */ | |
7240 | long tmp[6]; | |
7241 | ||
7242 | if (total_bytes > len) | |
7243 | return 0; | |
0800f6ae | 7244 | words = (32 / BITS_PER_UNIT) / UNITS_PER_WORD; |
5f4092ed | 7245 | |
7246 | real_to_target (tmp, TREE_REAL_CST_PTR (expr), TYPE_MODE (type)); | |
7247 | ||
f83924bb | 7248 | for (bitpos = 0; bitpos < total_bytes * BITS_PER_UNIT; |
7249 | bitpos += BITS_PER_UNIT) | |
5f4092ed | 7250 | { |
f83924bb | 7251 | byte = (bitpos / BITS_PER_UNIT) & 3; |
5f4092ed | 7252 | value = (unsigned char) (tmp[bitpos / 32] >> (bitpos & 31)); |
7253 | ||
f83924bb | 7254 | if (UNITS_PER_WORD < 4) |
5f4092ed | 7255 | { |
7256 | word = byte / UNITS_PER_WORD; | |
f83924bb | 7257 | if (WORDS_BIG_ENDIAN) |
5f4092ed | 7258 | word = (words - 1) - word; |
7259 | offset = word * UNITS_PER_WORD; | |
7260 | if (BYTES_BIG_ENDIAN) | |
7261 | offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD); | |
7262 | else | |
7263 | offset += byte % UNITS_PER_WORD; | |
7264 | } | |
7265 | else | |
f83924bb | 7266 | offset = BYTES_BIG_ENDIAN ? 3 - byte : byte; |
7267 | ptr[offset + ((bitpos / BITS_PER_UNIT) & ~3)] = value; | |
5f4092ed | 7268 | } |
7269 | return total_bytes; | |
7270 | } | |
7271 | ||
7272 | /* Subroutine of native_encode_expr. Encode the COMPLEX_CST | |
7273 | specified by EXPR into the buffer PTR of length LEN bytes. | |
7274 | Return the number of bytes placed in the buffer, or zero | |
7275 | upon failure. */ | |
7276 | ||
7277 | static int | |
b7bf20db | 7278 | native_encode_complex (const_tree expr, unsigned char *ptr, int len) |
5f4092ed | 7279 | { |
7280 | int rsize, isize; | |
7281 | tree part; | |
7282 | ||
7283 | part = TREE_REALPART (expr); | |
7284 | rsize = native_encode_expr (part, ptr, len); | |
7285 | if (rsize == 0) | |
7286 | return 0; | |
7287 | part = TREE_IMAGPART (expr); | |
7288 | isize = native_encode_expr (part, ptr+rsize, len-rsize); | |
7289 | if (isize != rsize) | |
7290 | return 0; | |
7291 | return rsize + isize; | |
7292 | } | |
7293 | ||
7294 | ||
7295 | /* Subroutine of native_encode_expr. Encode the VECTOR_CST | |
7296 | specified by EXPR into the buffer PTR of length LEN bytes. | |
7297 | Return the number of bytes placed in the buffer, or zero | |
7298 | upon failure. */ | |
7299 | ||
7300 | static int | |
b7bf20db | 7301 | native_encode_vector (const_tree expr, unsigned char *ptr, int len) |
5f4092ed | 7302 | { |
3fa15ed1 | 7303 | int i, size, offset, count; |
9fd22806 | 7304 | tree itype, elem, elements; |
5f4092ed | 7305 | |
5f4092ed | 7306 | offset = 0; |
7307 | elements = TREE_VECTOR_CST_ELTS (expr); | |
7308 | count = TYPE_VECTOR_SUBPARTS (TREE_TYPE (expr)); | |
9fd22806 | 7309 | itype = TREE_TYPE (TREE_TYPE (expr)); |
7310 | size = GET_MODE_SIZE (TYPE_MODE (itype)); | |
5f4092ed | 7311 | for (i = 0; i < count; i++) |
7312 | { | |
7313 | if (elements) | |
7314 | { | |
7315 | elem = TREE_VALUE (elements); | |
7316 | elements = TREE_CHAIN (elements); | |
7317 | } | |
7318 | else | |
7319 | elem = NULL_TREE; | |
7320 | ||
7321 | if (elem) | |
7322 | { | |
9fd22806 | 7323 | if (native_encode_expr (elem, ptr+offset, len-offset) != size) |
5f4092ed | 7324 | return 0; |
7325 | } | |
9fd22806 | 7326 | else |
5f4092ed | 7327 | { |
7328 | if (offset + size > len) | |
7329 | return 0; | |
7330 | memset (ptr+offset, 0, size); | |
7331 | } | |
5f4092ed | 7332 | offset += size; |
7333 | } | |
7334 | return offset; | |
7335 | } | |
7336 | ||
7337 | ||
95b7221a | 7338 | /* Subroutine of native_encode_expr. Encode the STRING_CST |
7339 | specified by EXPR into the buffer PTR of length LEN bytes. | |
7340 | Return the number of bytes placed in the buffer, or zero | |
7341 | upon failure. */ | |
7342 | ||
7343 | static int | |
7344 | native_encode_string (const_tree expr, unsigned char *ptr, int len) | |
7345 | { | |
7346 | tree type = TREE_TYPE (expr); | |
7347 | HOST_WIDE_INT total_bytes; | |
7348 | ||
7349 | if (TREE_CODE (type) != ARRAY_TYPE | |
7350 | || TREE_CODE (TREE_TYPE (type)) != INTEGER_TYPE | |
7351 | || GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (type))) != BITS_PER_UNIT | |
7352 | || !host_integerp (TYPE_SIZE_UNIT (type), 0)) | |
7353 | return 0; | |
7354 | total_bytes = tree_low_cst (TYPE_SIZE_UNIT (type), 0); | |
7355 | if (total_bytes > len) | |
7356 | return 0; | |
7357 | if (TREE_STRING_LENGTH (expr) < total_bytes) | |
7358 | { | |
7359 | memcpy (ptr, TREE_STRING_POINTER (expr), TREE_STRING_LENGTH (expr)); | |
7360 | memset (ptr + TREE_STRING_LENGTH (expr), 0, | |
7361 | total_bytes - TREE_STRING_LENGTH (expr)); | |
7362 | } | |
7363 | else | |
7364 | memcpy (ptr, TREE_STRING_POINTER (expr), total_bytes); | |
7365 | return total_bytes; | |
7366 | } | |
7367 | ||
7368 | ||
5f4092ed | 7369 | /* Subroutine of fold_view_convert_expr. Encode the INTEGER_CST, |
7370 | REAL_CST, COMPLEX_CST or VECTOR_CST specified by EXPR into the | |
7371 | buffer PTR of length LEN bytes. Return the number of bytes | |
7372 | placed in the buffer, or zero upon failure. */ | |
7373 | ||
bd56c1f7 | 7374 | int |
b7bf20db | 7375 | native_encode_expr (const_tree expr, unsigned char *ptr, int len) |
5f4092ed | 7376 | { |
7377 | switch (TREE_CODE (expr)) | |
7378 | { | |
7379 | case INTEGER_CST: | |
7380 | return native_encode_int (expr, ptr, len); | |
7381 | ||
7382 | case REAL_CST: | |
7383 | return native_encode_real (expr, ptr, len); | |
7384 | ||
7385 | case COMPLEX_CST: | |
7386 | return native_encode_complex (expr, ptr, len); | |
7387 | ||
7388 | case VECTOR_CST: | |
7389 | return native_encode_vector (expr, ptr, len); | |
7390 | ||
95b7221a | 7391 | case STRING_CST: |
7392 | return native_encode_string (expr, ptr, len); | |
7393 | ||
5f4092ed | 7394 | default: |
7395 | return 0; | |
7396 | } | |
7397 | } | |
7398 | ||
7399 | ||
7400 | /* Subroutine of native_interpret_expr. Interpret the contents of | |
7401 | the buffer PTR of length LEN as an INTEGER_CST of type TYPE. | |
7402 | If the buffer cannot be interpreted, return NULL_TREE. */ | |
7403 | ||
7404 | static tree | |
b7bf20db | 7405 | native_interpret_int (tree type, const unsigned char *ptr, int len) |
5f4092ed | 7406 | { |
7407 | int total_bytes = GET_MODE_SIZE (TYPE_MODE (type)); | |
7408 | int byte, offset, word, words; | |
7409 | unsigned char value; | |
c5083e8b | 7410 | double_int result; |
5f4092ed | 7411 | |
7412 | if (total_bytes > len) | |
7413 | return NULL_TREE; | |
7414 | if (total_bytes * BITS_PER_UNIT > 2 * HOST_BITS_PER_WIDE_INT) | |
7415 | return NULL_TREE; | |
c5083e8b | 7416 | |
7417 | result = double_int_zero; | |
5f4092ed | 7418 | words = total_bytes / UNITS_PER_WORD; |
7419 | ||
7420 | for (byte = 0; byte < total_bytes; byte++) | |
7421 | { | |
7422 | int bitpos = byte * BITS_PER_UNIT; | |
7423 | if (total_bytes > UNITS_PER_WORD) | |
7424 | { | |
7425 | word = byte / UNITS_PER_WORD; | |
7426 | if (WORDS_BIG_ENDIAN) | |
7427 | word = (words - 1) - word; | |
7428 | offset = word * UNITS_PER_WORD; | |
7429 | if (BYTES_BIG_ENDIAN) | |
7430 | offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD); | |
7431 | else | |
7432 | offset += byte % UNITS_PER_WORD; | |
7433 | } | |
7434 | else | |
7435 | offset = BYTES_BIG_ENDIAN ? (total_bytes - 1) - byte : byte; | |
7436 | value = ptr[offset]; | |
7437 | ||
7438 | if (bitpos < HOST_BITS_PER_WIDE_INT) | |
c5083e8b | 7439 | result.low |= (unsigned HOST_WIDE_INT) value << bitpos; |
5f4092ed | 7440 | else |
c5083e8b | 7441 | result.high |= (unsigned HOST_WIDE_INT) value |
7442 | << (bitpos - HOST_BITS_PER_WIDE_INT); | |
5f4092ed | 7443 | } |
7444 | ||
c5083e8b | 7445 | return double_int_to_tree (type, result); |
5f4092ed | 7446 | } |
7447 | ||
7448 | ||
7449 | /* Subroutine of native_interpret_expr. Interpret the contents of | |
7450 | the buffer PTR of length LEN as a REAL_CST of type TYPE. | |
7451 | If the buffer cannot be interpreted, return NULL_TREE. */ | |
7452 | ||
7453 | static tree | |
b7bf20db | 7454 | native_interpret_real (tree type, const unsigned char *ptr, int len) |
5f4092ed | 7455 | { |
3fa15ed1 | 7456 | enum machine_mode mode = TYPE_MODE (type); |
7457 | int total_bytes = GET_MODE_SIZE (mode); | |
f83924bb | 7458 | int byte, offset, word, words, bitpos; |
5f4092ed | 7459 | unsigned char value; |
7460 | /* There are always 32 bits in each long, no matter the size of | |
7461 | the hosts long. We handle floating point representations with | |
7462 | up to 192 bits. */ | |
7463 | REAL_VALUE_TYPE r; | |
7464 | long tmp[6]; | |
7465 | ||
7466 | total_bytes = GET_MODE_SIZE (TYPE_MODE (type)); | |
7467 | if (total_bytes > len || total_bytes > 24) | |
7468 | return NULL_TREE; | |
0800f6ae | 7469 | words = (32 / BITS_PER_UNIT) / UNITS_PER_WORD; |
5f4092ed | 7470 | |
7471 | memset (tmp, 0, sizeof (tmp)); | |
f83924bb | 7472 | for (bitpos = 0; bitpos < total_bytes * BITS_PER_UNIT; |
7473 | bitpos += BITS_PER_UNIT) | |
5f4092ed | 7474 | { |
f83924bb | 7475 | byte = (bitpos / BITS_PER_UNIT) & 3; |
7476 | if (UNITS_PER_WORD < 4) | |
5f4092ed | 7477 | { |
7478 | word = byte / UNITS_PER_WORD; | |
f83924bb | 7479 | if (WORDS_BIG_ENDIAN) |
5f4092ed | 7480 | word = (words - 1) - word; |
7481 | offset = word * UNITS_PER_WORD; | |
7482 | if (BYTES_BIG_ENDIAN) | |
7483 | offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD); | |
7484 | else | |
7485 | offset += byte % UNITS_PER_WORD; | |
7486 | } | |
7487 | else | |
f83924bb | 7488 | offset = BYTES_BIG_ENDIAN ? 3 - byte : byte; |
7489 | value = ptr[offset + ((bitpos / BITS_PER_UNIT) & ~3)]; | |
5f4092ed | 7490 | |
7491 | tmp[bitpos / 32] |= (unsigned long)value << (bitpos & 31); | |
7492 | } | |
7493 | ||
7494 | real_from_target (&r, tmp, mode); | |
7495 | return build_real (type, r); | |
7496 | } | |
7497 | ||
7498 | ||
7499 | /* Subroutine of native_interpret_expr. Interpret the contents of | |
7500 | the buffer PTR of length LEN as a COMPLEX_CST of type TYPE. | |
7501 | If the buffer cannot be interpreted, return NULL_TREE. */ | |
7502 | ||
7503 | static tree | |
b7bf20db | 7504 | native_interpret_complex (tree type, const unsigned char *ptr, int len) |
5f4092ed | 7505 | { |
7506 | tree etype, rpart, ipart; | |
7507 | int size; | |
7508 | ||
7509 | etype = TREE_TYPE (type); | |
7510 | size = GET_MODE_SIZE (TYPE_MODE (etype)); | |
7511 | if (size * 2 > len) | |
7512 | return NULL_TREE; | |
7513 | rpart = native_interpret_expr (etype, ptr, size); | |
7514 | if (!rpart) | |
7515 | return NULL_TREE; | |
7516 | ipart = native_interpret_expr (etype, ptr+size, size); | |
7517 | if (!ipart) | |
7518 | return NULL_TREE; | |
7519 | return build_complex (type, rpart, ipart); | |
7520 | } | |
7521 | ||
7522 | ||
7523 | /* Subroutine of native_interpret_expr. Interpret the contents of | |
7524 | the buffer PTR of length LEN as a VECTOR_CST of type TYPE. | |
7525 | If the buffer cannot be interpreted, return NULL_TREE. */ | |
7526 | ||
7527 | static tree | |
b7bf20db | 7528 | native_interpret_vector (tree type, const unsigned char *ptr, int len) |
5f4092ed | 7529 | { |
7530 | tree etype, elem, elements; | |
7531 | int i, size, count; | |
7532 | ||
7533 | etype = TREE_TYPE (type); | |
7534 | size = GET_MODE_SIZE (TYPE_MODE (etype)); | |
7535 | count = TYPE_VECTOR_SUBPARTS (type); | |
7536 | if (size * count > len) | |
7537 | return NULL_TREE; | |
7538 | ||
7539 | elements = NULL_TREE; | |
7540 | for (i = count - 1; i >= 0; i--) | |
7541 | { | |
7542 | elem = native_interpret_expr (etype, ptr+(i*size), size); | |
7543 | if (!elem) | |
7544 | return NULL_TREE; | |
7545 | elements = tree_cons (NULL_TREE, elem, elements); | |
7546 | } | |
7547 | return build_vector (type, elements); | |
7548 | } | |
7549 | ||
7550 | ||
d961ae3a | 7551 | /* Subroutine of fold_view_convert_expr. Interpret the contents of |
5f4092ed | 7552 | the buffer PTR of length LEN as a constant of type TYPE. For |
7553 | INTEGRAL_TYPE_P we return an INTEGER_CST, for SCALAR_FLOAT_TYPE_P | |
7554 | we return a REAL_CST, etc... If the buffer cannot be interpreted, | |
7555 | return NULL_TREE. */ | |
7556 | ||
bd56c1f7 | 7557 | tree |
b7bf20db | 7558 | native_interpret_expr (tree type, const unsigned char *ptr, int len) |
5f4092ed | 7559 | { |
7560 | switch (TREE_CODE (type)) | |
7561 | { | |
7562 | case INTEGER_TYPE: | |
7563 | case ENUMERAL_TYPE: | |
7564 | case BOOLEAN_TYPE: | |
7565 | return native_interpret_int (type, ptr, len); | |
7566 | ||
7567 | case REAL_TYPE: | |
7568 | return native_interpret_real (type, ptr, len); | |
7569 | ||
7570 | case COMPLEX_TYPE: | |
7571 | return native_interpret_complex (type, ptr, len); | |
7572 | ||
7573 | case VECTOR_TYPE: | |
7574 | return native_interpret_vector (type, ptr, len); | |
7575 | ||
7576 | default: | |
7577 | return NULL_TREE; | |
7578 | } | |
7579 | } | |
7580 | ||
7581 | ||
7582 | /* Fold a VIEW_CONVERT_EXPR of a constant expression EXPR to type | |
7583 | TYPE at compile-time. If we're unable to perform the conversion | |
7584 | return NULL_TREE. */ | |
7585 | ||
7586 | static tree | |
7587 | fold_view_convert_expr (tree type, tree expr) | |
7588 | { | |
7589 | /* We support up to 512-bit values (for V8DFmode). */ | |
7590 | unsigned char buffer[64]; | |
7591 | int len; | |
7592 | ||
7593 | /* Check that the host and target are sane. */ | |
7594 | if (CHAR_BIT != 8 || BITS_PER_UNIT != 8) | |
7595 | return NULL_TREE; | |
7596 | ||
7597 | len = native_encode_expr (expr, buffer, sizeof (buffer)); | |
7598 | if (len == 0) | |
7599 | return NULL_TREE; | |
7600 | ||
7601 | return native_interpret_expr (type, buffer, len); | |
7602 | } | |
7603 | ||
2bf4108d | 7604 | /* Build an expression for the address of T. Folds away INDIRECT_REF |
86f2ad37 | 7605 | to avoid confusing the gimplify process. */ |
2bf4108d | 7606 | |
86f2ad37 | 7607 | tree |
389dd41b | 7608 | build_fold_addr_expr_with_type_loc (location_t loc, tree t, tree ptrtype) |
2bf4108d | 7609 | { |
7610 | /* The size of the object is not relevant when talking about its address. */ | |
7611 | if (TREE_CODE (t) == WITH_SIZE_EXPR) | |
7612 | t = TREE_OPERAND (t, 0); | |
7613 | ||
7614 | /* Note: doesn't apply to ALIGN_INDIRECT_REF */ | |
7615 | if (TREE_CODE (t) == INDIRECT_REF | |
7616 | || TREE_CODE (t) == MISALIGNED_INDIRECT_REF) | |
7617 | { | |
7618 | t = TREE_OPERAND (t, 0); | |
7619 | ||
7620 | if (TREE_TYPE (t) != ptrtype) | |
389dd41b | 7621 | { |
7622 | t = build1 (NOP_EXPR, ptrtype, t); | |
7623 | SET_EXPR_LOCATION (t, loc); | |
7624 | } | |
2bf4108d | 7625 | } |
52dbb6e6 | 7626 | else if (TREE_CODE (t) == VIEW_CONVERT_EXPR) |
7627 | { | |
389dd41b | 7628 | t = build_fold_addr_expr_loc (loc, TREE_OPERAND (t, 0)); |
52dbb6e6 | 7629 | |
7630 | if (TREE_TYPE (t) != ptrtype) | |
389dd41b | 7631 | t = fold_convert_loc (loc, ptrtype, t); |
52dbb6e6 | 7632 | } |
2bf4108d | 7633 | else |
389dd41b | 7634 | { |
7635 | t = build1 (ADDR_EXPR, ptrtype, t); | |
7636 | SET_EXPR_LOCATION (t, loc); | |
7637 | } | |
2bf4108d | 7638 | |
7639 | return t; | |
7640 | } | |
7641 | ||
86f2ad37 | 7642 | /* Build an expression for the address of T. */ |
2bf4108d | 7643 | |
7644 | tree | |
389dd41b | 7645 | build_fold_addr_expr_loc (location_t loc, tree t) |
2bf4108d | 7646 | { |
7647 | tree ptrtype = build_pointer_type (TREE_TYPE (t)); | |
7648 | ||
389dd41b | 7649 | return build_fold_addr_expr_with_type_loc (loc, t, ptrtype); |
2bf4108d | 7650 | } |
5f4092ed | 7651 | |
0d3711e2 | 7652 | /* Fold a unary expression of code CODE and type TYPE with operand |
7653 | OP0. Return the folded expression if folding is successful. | |
7654 | Otherwise, return NULL_TREE. */ | |
422c18cb | 7655 | |
d3858e14 | 7656 | tree |
389dd41b | 7657 | fold_unary_loc (location_t loc, enum tree_code code, tree type, tree op0) |
422c18cb | 7658 | { |
422c18cb | 7659 | tree tem; |
0052b98e | 7660 | tree arg0; |
422c18cb | 7661 | enum tree_code_class kind = TREE_CODE_CLASS (code); |
7662 | ||
7663 | gcc_assert (IS_EXPR_CODE_CLASS (kind) | |
7664 | && TREE_CODE_LENGTH (code) == 1); | |
7665 | ||
0052b98e | 7666 | arg0 = op0; |
422c18cb | 7667 | if (arg0) |
7668 | { | |
d9659041 | 7669 | if (CONVERT_EXPR_CODE_P (code) |
00bb4a78 | 7670 | || code == FLOAT_EXPR || code == ABS_EXPR) |
422c18cb | 7671 | { |
00bb4a78 | 7672 | /* Don't use STRIP_NOPS, because signedness of argument type |
7673 | matters. */ | |
422c18cb | 7674 | STRIP_SIGN_NOPS (arg0); |
7675 | } | |
7676 | else | |
7677 | { | |
7678 | /* Strip any conversions that don't change the mode. This | |
7679 | is safe for every expression, except for a comparison | |
7680 | expression because its signedness is derived from its | |
7681 | operands. | |
7682 | ||
7683 | Note that this is done as an internal manipulation within | |
7684 | the constant folder, in order to find the simplest | |
7685 | representation of the arguments so that their form can be | |
7686 | studied. In any cases, the appropriate type conversions | |
7687 | should be put back in the tree that will get out of the | |
7688 | constant folder. */ | |
7689 | STRIP_NOPS (arg0); | |
7690 | } | |
7691 | } | |
7692 | ||
7693 | if (TREE_CODE_CLASS (code) == tcc_unary) | |
7694 | { | |
7695 | if (TREE_CODE (arg0) == COMPOUND_EXPR) | |
7696 | return build2 (COMPOUND_EXPR, type, TREE_OPERAND (arg0, 0), | |
389dd41b | 7697 | fold_build1_loc (loc, code, type, |
7698 | fold_convert_loc (loc, TREE_TYPE (op0), | |
7699 | TREE_OPERAND (arg0, 1)))); | |
422c18cb | 7700 | else if (TREE_CODE (arg0) == COND_EXPR) |
7701 | { | |
7702 | tree arg01 = TREE_OPERAND (arg0, 1); | |
7703 | tree arg02 = TREE_OPERAND (arg0, 2); | |
7704 | if (! VOID_TYPE_P (TREE_TYPE (arg01))) | |
389dd41b | 7705 | arg01 = fold_build1_loc (loc, code, type, |
7706 | fold_convert_loc (loc, | |
7707 | TREE_TYPE (op0), arg01)); | |
422c18cb | 7708 | if (! VOID_TYPE_P (TREE_TYPE (arg02))) |
389dd41b | 7709 | arg02 = fold_build1_loc (loc, code, type, |
7710 | fold_convert_loc (loc, | |
7711 | TREE_TYPE (op0), arg02)); | |
7712 | tem = fold_build3_loc (loc, COND_EXPR, type, TREE_OPERAND (arg0, 0), | |
7ab7fd4f | 7713 | arg01, arg02); |
422c18cb | 7714 | |
7715 | /* If this was a conversion, and all we did was to move into | |
7716 | inside the COND_EXPR, bring it back out. But leave it if | |
7717 | it is a conversion from integer to integer and the | |
7718 | result precision is no wider than a word since such a | |
7719 | conversion is cheap and may be optimized away by combine, | |
7720 | while it couldn't if it were outside the COND_EXPR. Then return | |
7721 | so we don't get into an infinite recursion loop taking the | |
7722 | conversion out and then back in. */ | |
7723 | ||
d9659041 | 7724 | if ((CONVERT_EXPR_CODE_P (code) |
422c18cb | 7725 | || code == NON_LVALUE_EXPR) |
7726 | && TREE_CODE (tem) == COND_EXPR | |
7727 | && TREE_CODE (TREE_OPERAND (tem, 1)) == code | |
7728 | && TREE_CODE (TREE_OPERAND (tem, 2)) == code | |
7729 | && ! VOID_TYPE_P (TREE_OPERAND (tem, 1)) | |
7730 | && ! VOID_TYPE_P (TREE_OPERAND (tem, 2)) | |
7731 | && (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem, 1), 0)) | |
7732 | == TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem, 2), 0))) | |
7733 | && (! (INTEGRAL_TYPE_P (TREE_TYPE (tem)) | |
7734 | && (INTEGRAL_TYPE_P | |
7735 | (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem, 1), 0)))) | |
7736 | && TYPE_PRECISION (TREE_TYPE (tem)) <= BITS_PER_WORD) | |
7737 | || flag_syntax_only)) | |
389dd41b | 7738 | { |
7739 | tem = build1 (code, type, | |
7740 | build3 (COND_EXPR, | |
7741 | TREE_TYPE (TREE_OPERAND | |
7742 | (TREE_OPERAND (tem, 1), 0)), | |
7743 | TREE_OPERAND (tem, 0), | |
7744 | TREE_OPERAND (TREE_OPERAND (tem, 1), 0), | |
7745 | TREE_OPERAND (TREE_OPERAND (tem, 2), 0))); | |
7746 | SET_EXPR_LOCATION (tem, loc); | |
7747 | } | |
422c18cb | 7748 | return tem; |
7749 | } | |
7750 | else if (COMPARISON_CLASS_P (arg0)) | |
7751 | { | |
7752 | if (TREE_CODE (type) == BOOLEAN_TYPE) | |
7753 | { | |
7754 | arg0 = copy_node (arg0); | |
7755 | TREE_TYPE (arg0) = type; | |
7756 | return arg0; | |
7757 | } | |
7758 | else if (TREE_CODE (type) != INTEGER_TYPE) | |
389dd41b | 7759 | return fold_build3_loc (loc, COND_EXPR, type, arg0, |
7760 | fold_build1_loc (loc, code, type, | |
7ab7fd4f | 7761 | integer_one_node), |
389dd41b | 7762 | fold_build1_loc (loc, code, type, |
7ab7fd4f | 7763 | integer_zero_node)); |
422c18cb | 7764 | } |
7765 | } | |
7766 | ||
7767 | switch (code) | |
7768 | { | |
751ff693 | 7769 | case PAREN_EXPR: |
7770 | /* Re-association barriers around constants and other re-association | |
7771 | barriers can be removed. */ | |
7772 | if (CONSTANT_CLASS_P (op0) | |
7773 | || TREE_CODE (op0) == PAREN_EXPR) | |
389dd41b | 7774 | return fold_convert_loc (loc, type, op0); |
751ff693 | 7775 | return NULL_TREE; |
7776 | ||
72dd6141 | 7777 | CASE_CONVERT: |
422c18cb | 7778 | case FLOAT_EXPR: |
422c18cb | 7779 | case FIX_TRUNC_EXPR: |
fac5aff3 | 7780 | if (TREE_TYPE (op0) == type) |
7781 | return op0; | |
48e1416a | 7782 | |
191ec5a2 | 7783 | /* If we have (type) (a CMP b) and type is an integral type, return |
8aa776be | 7784 | new expression involving the new type. */ |
7785 | if (COMPARISON_CLASS_P (op0) && INTEGRAL_TYPE_P (type)) | |
389dd41b | 7786 | return fold_build2_loc (loc, TREE_CODE (op0), type, TREE_OPERAND (op0, 0), |
8aa776be | 7787 | TREE_OPERAND (op0, 1)); |
422c18cb | 7788 | |
7789 | /* Handle cases of two conversions in a row. */ | |
72dd6141 | 7790 | if (CONVERT_EXPR_P (op0)) |
422c18cb | 7791 | { |
fac5aff3 | 7792 | tree inside_type = TREE_TYPE (TREE_OPERAND (op0, 0)); |
7793 | tree inter_type = TREE_TYPE (op0); | |
422c18cb | 7794 | int inside_int = INTEGRAL_TYPE_P (inside_type); |
7795 | int inside_ptr = POINTER_TYPE_P (inside_type); | |
7796 | int inside_float = FLOAT_TYPE_P (inside_type); | |
6ff828af | 7797 | int inside_vec = TREE_CODE (inside_type) == VECTOR_TYPE; |
422c18cb | 7798 | unsigned int inside_prec = TYPE_PRECISION (inside_type); |
7799 | int inside_unsignedp = TYPE_UNSIGNED (inside_type); | |
7800 | int inter_int = INTEGRAL_TYPE_P (inter_type); | |
7801 | int inter_ptr = POINTER_TYPE_P (inter_type); | |
7802 | int inter_float = FLOAT_TYPE_P (inter_type); | |
6ff828af | 7803 | int inter_vec = TREE_CODE (inter_type) == VECTOR_TYPE; |
422c18cb | 7804 | unsigned int inter_prec = TYPE_PRECISION (inter_type); |
7805 | int inter_unsignedp = TYPE_UNSIGNED (inter_type); | |
7806 | int final_int = INTEGRAL_TYPE_P (type); | |
7807 | int final_ptr = POINTER_TYPE_P (type); | |
7808 | int final_float = FLOAT_TYPE_P (type); | |
6ff828af | 7809 | int final_vec = TREE_CODE (type) == VECTOR_TYPE; |
422c18cb | 7810 | unsigned int final_prec = TYPE_PRECISION (type); |
7811 | int final_unsignedp = TYPE_UNSIGNED (type); | |
7812 | ||
7813 | /* In addition to the cases of two conversions in a row | |
7814 | handled below, if we are converting something to its own | |
7815 | type via an object of identical or wider precision, neither | |
7816 | conversion is needed. */ | |
7817 | if (TYPE_MAIN_VARIANT (inside_type) == TYPE_MAIN_VARIANT (type) | |
219dad96 | 7818 | && (((inter_int || inter_ptr) && final_int) |
7819 | || (inter_float && final_float)) | |
422c18cb | 7820 | && inter_prec >= final_prec) |
389dd41b | 7821 | return fold_build1_loc (loc, code, type, TREE_OPERAND (op0, 0)); |
422c18cb | 7822 | |
4ce9876e | 7823 | /* Likewise, if the intermediate and initial types are either both |
7824 | float or both integer, we don't need the middle conversion if the | |
7825 | former is wider than the latter and doesn't change the signedness | |
7826 | (for integers). Avoid this if the final type is a pointer since | |
7827 | then we sometimes need the middle conversion. Likewise if the | |
7828 | final type has a precision not equal to the size of its mode. */ | |
0fd56ba6 | 7829 | if (((inter_int && inside_int) |
6ff828af | 7830 | || (inter_float && inside_float) |
7831 | || (inter_vec && inside_vec)) | |
422c18cb | 7832 | && inter_prec >= inside_prec |
6ff828af | 7833 | && (inter_float || inter_vec |
7834 | || inter_unsignedp == inside_unsignedp) | |
422c18cb | 7835 | && ! (final_prec != GET_MODE_BITSIZE (TYPE_MODE (type)) |
7836 | && TYPE_MODE (type) == TYPE_MODE (inter_type)) | |
6ff828af | 7837 | && ! final_ptr |
7838 | && (! final_vec || inter_prec == inside_prec)) | |
389dd41b | 7839 | return fold_build1_loc (loc, code, type, TREE_OPERAND (op0, 0)); |
422c18cb | 7840 | |
7841 | /* If we have a sign-extension of a zero-extended value, we can | |
7842 | replace that by a single zero-extension. */ | |
7843 | if (inside_int && inter_int && final_int | |
7844 | && inside_prec < inter_prec && inter_prec < final_prec | |
7845 | && inside_unsignedp && !inter_unsignedp) | |
389dd41b | 7846 | return fold_build1_loc (loc, code, type, TREE_OPERAND (op0, 0)); |
422c18cb | 7847 | |
7848 | /* Two conversions in a row are not needed unless: | |
7849 | - some conversion is floating-point (overstrict for now), or | |
6ff828af | 7850 | - some conversion is a vector (overstrict for now), or |
422c18cb | 7851 | - the intermediate type is narrower than both initial and |
7852 | final, or | |
7853 | - the intermediate type and innermost type differ in signedness, | |
7854 | and the outermost type is wider than the intermediate, or | |
7855 | - the initial type is a pointer type and the precisions of the | |
7856 | intermediate and final types differ, or | |
7857 | - the final type is a pointer type and the precisions of the | |
0b4a6afc | 7858 | initial and intermediate types differ. */ |
422c18cb | 7859 | if (! inside_float && ! inter_float && ! final_float |
6ff828af | 7860 | && ! inside_vec && ! inter_vec && ! final_vec |
219dad96 | 7861 | && (inter_prec >= inside_prec || inter_prec >= final_prec) |
422c18cb | 7862 | && ! (inside_int && inter_int |
7863 | && inter_unsignedp != inside_unsignedp | |
7864 | && inter_prec < final_prec) | |
7865 | && ((inter_unsignedp && inter_prec > inside_prec) | |
7866 | == (final_unsignedp && final_prec > inter_prec)) | |
7867 | && ! (inside_ptr && inter_prec != final_prec) | |
7868 | && ! (final_ptr && inside_prec != inter_prec) | |
7869 | && ! (final_prec != GET_MODE_BITSIZE (TYPE_MODE (type)) | |
0b4a6afc | 7870 | && TYPE_MODE (type) == TYPE_MODE (inter_type))) |
389dd41b | 7871 | return fold_build1_loc (loc, code, type, TREE_OPERAND (op0, 0)); |
422c18cb | 7872 | } |
7873 | ||
73d9e97d | 7874 | /* Handle (T *)&A.B.C for A being of type T and B and C |
23943319 | 7875 | living at offset zero. This occurs frequently in |
73d9e97d | 7876 | C++ upcasting and then accessing the base. */ |
7877 | if (TREE_CODE (op0) == ADDR_EXPR | |
7878 | && POINTER_TYPE_P (type) | |
7879 | && handled_component_p (TREE_OPERAND (op0, 0))) | |
7880 | { | |
7881 | HOST_WIDE_INT bitsize, bitpos; | |
7882 | tree offset; | |
7883 | enum machine_mode mode; | |
7884 | int unsignedp, volatilep; | |
7885 | tree base = TREE_OPERAND (op0, 0); | |
7886 | base = get_inner_reference (base, &bitsize, &bitpos, &offset, | |
7887 | &mode, &unsignedp, &volatilep, false); | |
7888 | /* If the reference was to a (constant) zero offset, we can use | |
7889 | the address of the base if it has the same base type | |
7f2d9047 | 7890 | as the result type and the pointer type is unqualified. */ |
73d9e97d | 7891 | if (! offset && bitpos == 0 |
7f2d9047 | 7892 | && (TYPE_MAIN_VARIANT (TREE_TYPE (type)) |
73d9e97d | 7893 | == TYPE_MAIN_VARIANT (TREE_TYPE (base))) |
7f2d9047 | 7894 | && TYPE_QUALS (type) == TYPE_UNQUALIFIED) |
389dd41b | 7895 | return fold_convert_loc (loc, type, |
7896 | build_fold_addr_expr_loc (loc, base)); | |
73d9e97d | 7897 | } |
7898 | ||
75a70cf9 | 7899 | if (TREE_CODE (op0) == MODIFY_EXPR |
7900 | && TREE_CONSTANT (TREE_OPERAND (op0, 1)) | |
422c18cb | 7901 | /* Detect assigning a bitfield. */ |
75a70cf9 | 7902 | && !(TREE_CODE (TREE_OPERAND (op0, 0)) == COMPONENT_REF |
35cc02b5 | 7903 | && DECL_BIT_FIELD |
75a70cf9 | 7904 | (TREE_OPERAND (TREE_OPERAND (op0, 0), 1)))) |
422c18cb | 7905 | { |
7906 | /* Don't leave an assignment inside a conversion | |
7907 | unless assigning a bitfield. */ | |
389dd41b | 7908 | tem = fold_build1_loc (loc, code, type, TREE_OPERAND (op0, 1)); |
422c18cb | 7909 | /* First do the assignment, then return converted constant. */ |
b085d4e5 | 7910 | tem = build2 (COMPOUND_EXPR, TREE_TYPE (tem), op0, tem); |
422c18cb | 7911 | TREE_NO_WARNING (tem) = 1; |
7912 | TREE_USED (tem) = 1; | |
389dd41b | 7913 | SET_EXPR_LOCATION (tem, loc); |
422c18cb | 7914 | return tem; |
7915 | } | |
7916 | ||
7917 | /* Convert (T)(x & c) into (T)x & (T)c, if c is an integer | |
7918 | constants (if x has signed type, the sign bit cannot be set | |
e7b454ed | 7919 | in c). This folds extension into the BIT_AND_EXPR. |
7920 | ??? We don't do it for BOOLEAN_TYPE or ENUMERAL_TYPE because they | |
7921 | very likely don't have maximal range for their precision and this | |
7922 | transformation effectively doesn't preserve non-maximal ranges. */ | |
101f2414 | 7923 | if (TREE_CODE (type) == INTEGER_TYPE |
fac5aff3 | 7924 | && TREE_CODE (op0) == BIT_AND_EXPR |
a9538d68 | 7925 | && TREE_CODE (TREE_OPERAND (op0, 1)) == INTEGER_CST) |
422c18cb | 7926 | { |
2a6a6991 | 7927 | tree and_expr = op0; |
7928 | tree and0 = TREE_OPERAND (and_expr, 0); | |
7929 | tree and1 = TREE_OPERAND (and_expr, 1); | |
422c18cb | 7930 | int change = 0; |
7931 | ||
2a6a6991 | 7932 | if (TYPE_UNSIGNED (TREE_TYPE (and_expr)) |
422c18cb | 7933 | || (TYPE_PRECISION (type) |
2a6a6991 | 7934 | <= TYPE_PRECISION (TREE_TYPE (and_expr)))) |
422c18cb | 7935 | change = 1; |
7936 | else if (TYPE_PRECISION (TREE_TYPE (and1)) | |
7937 | <= HOST_BITS_PER_WIDE_INT | |
7938 | && host_integerp (and1, 1)) | |
7939 | { | |
7940 | unsigned HOST_WIDE_INT cst; | |
7941 | ||
7942 | cst = tree_low_cst (and1, 1); | |
7943 | cst &= (HOST_WIDE_INT) -1 | |
7944 | << (TYPE_PRECISION (TREE_TYPE (and1)) - 1); | |
7945 | change = (cst == 0); | |
7946 | #ifdef LOAD_EXTEND_OP | |
7947 | if (change | |
7948 | && !flag_syntax_only | |
7949 | && (LOAD_EXTEND_OP (TYPE_MODE (TREE_TYPE (and0))) | |
7950 | == ZERO_EXTEND)) | |
7951 | { | |
71eea85c | 7952 | tree uns = unsigned_type_for (TREE_TYPE (and0)); |
389dd41b | 7953 | and0 = fold_convert_loc (loc, uns, and0); |
7954 | and1 = fold_convert_loc (loc, uns, and1); | |
422c18cb | 7955 | } |
7956 | #endif | |
7957 | } | |
7958 | if (change) | |
7959 | { | |
c8110c8f | 7960 | tem = force_fit_type_double (type, TREE_INT_CST_LOW (and1), |
7961 | TREE_INT_CST_HIGH (and1), 0, | |
eddad94a | 7962 | TREE_OVERFLOW (and1)); |
389dd41b | 7963 | return fold_build2_loc (loc, BIT_AND_EXPR, type, |
7964 | fold_convert_loc (loc, type, and0), tem); | |
422c18cb | 7965 | } |
7966 | } | |
7967 | ||
0de36bdb | 7968 | /* Convert (T1)(X p+ Y) into ((T1)X p+ Y), for pointer type, |
06f9fe3e | 7969 | when one of the new casts will fold away. Conservatively we assume |
0de36bdb | 7970 | that this happens when X or Y is NOP_EXPR or Y is INTEGER_CST. */ |
7971 | if (POINTER_TYPE_P (type) | |
7972 | && TREE_CODE (arg0) == POINTER_PLUS_EXPR | |
06f9fe3e | 7973 | && (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST |
7974 | || TREE_CODE (TREE_OPERAND (arg0, 0)) == NOP_EXPR | |
7975 | || TREE_CODE (TREE_OPERAND (arg0, 1)) == NOP_EXPR)) | |
422c18cb | 7976 | { |
7977 | tree arg00 = TREE_OPERAND (arg0, 0); | |
06f9fe3e | 7978 | tree arg01 = TREE_OPERAND (arg0, 1); |
7979 | ||
389dd41b | 7980 | return fold_build2_loc (loc, |
7981 | TREE_CODE (arg0), type, | |
7982 | fold_convert_loc (loc, type, arg00), | |
7983 | fold_convert_loc (loc, sizetype, arg01)); | |
422c18cb | 7984 | } |
7985 | ||
c348f27f | 7986 | /* Convert (T1)(~(T2)X) into ~(T1)X if T1 and T2 are integral types |
80777cd8 | 7987 | of the same precision, and X is an integer type not narrower than |
c348f27f | 7988 | types T1 or T2, i.e. the cast (T2)X isn't an extension. */ |
7989 | if (INTEGRAL_TYPE_P (type) | |
7990 | && TREE_CODE (op0) == BIT_NOT_EXPR | |
7991 | && INTEGRAL_TYPE_P (TREE_TYPE (op0)) | |
72dd6141 | 7992 | && CONVERT_EXPR_P (TREE_OPERAND (op0, 0)) |
c348f27f | 7993 | && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (op0))) |
7994 | { | |
7995 | tem = TREE_OPERAND (TREE_OPERAND (op0, 0), 0); | |
7996 | if (INTEGRAL_TYPE_P (TREE_TYPE (tem)) | |
7997 | && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (tem))) | |
389dd41b | 7998 | return fold_build1_loc (loc, BIT_NOT_EXPR, type, |
7999 | fold_convert_loc (loc, type, tem)); | |
c348f27f | 8000 | } |
8001 | ||
92b2f241 | 8002 | /* Convert (T1)(X * Y) into (T1)X * (T1)Y if T1 is narrower than the |
8003 | type of X and Y (integer types only). */ | |
8004 | if (INTEGRAL_TYPE_P (type) | |
8005 | && TREE_CODE (op0) == MULT_EXPR | |
8006 | && INTEGRAL_TYPE_P (TREE_TYPE (op0)) | |
8007 | && TYPE_PRECISION (type) < TYPE_PRECISION (TREE_TYPE (op0))) | |
8008 | { | |
8009 | /* Be careful not to introduce new overflows. */ | |
8010 | tree mult_type; | |
8011 | if (TYPE_OVERFLOW_WRAPS (type)) | |
8012 | mult_type = type; | |
8013 | else | |
8014 | mult_type = unsigned_type_for (type); | |
6d5aa66a | 8015 | |
8016 | if (TYPE_PRECISION (mult_type) < TYPE_PRECISION (TREE_TYPE (op0))) | |
8017 | { | |
389dd41b | 8018 | tem = fold_build2_loc (loc, MULT_EXPR, mult_type, |
8019 | fold_convert_loc (loc, mult_type, | |
8020 | TREE_OPERAND (op0, 0)), | |
8021 | fold_convert_loc (loc, mult_type, | |
8022 | TREE_OPERAND (op0, 1))); | |
8023 | return fold_convert_loc (loc, type, tem); | |
6d5aa66a | 8024 | } |
92b2f241 | 8025 | } |
8026 | ||
7a6537b3 | 8027 | tem = fold_convert_const (code, type, op0); |
e7edfbbd | 8028 | return tem ? tem : NULL_TREE; |
422c18cb | 8029 | |
bd1a81f7 | 8030 | case ADDR_SPACE_CONVERT_EXPR: |
8031 | if (integer_zerop (arg0)) | |
8032 | return fold_convert_const (code, type, arg0); | |
8033 | return NULL_TREE; | |
8034 | ||
06f0b99c | 8035 | case FIXED_CONVERT_EXPR: |
8036 | tem = fold_convert_const (code, type, arg0); | |
8037 | return tem ? tem : NULL_TREE; | |
8038 | ||
422c18cb | 8039 | case VIEW_CONVERT_EXPR: |
eb381097 | 8040 | if (TREE_TYPE (op0) == type) |
8041 | return op0; | |
802d9f2f | 8042 | if (TREE_CODE (op0) == VIEW_CONVERT_EXPR) |
389dd41b | 8043 | return fold_build1_loc (loc, VIEW_CONVERT_EXPR, |
8044 | type, TREE_OPERAND (op0, 0)); | |
802d9f2f | 8045 | |
8046 | /* For integral conversions with the same precision or pointer | |
8047 | conversions use a NOP_EXPR instead. */ | |
1d9353f3 | 8048 | if ((INTEGRAL_TYPE_P (type) |
8049 | || POINTER_TYPE_P (type)) | |
8050 | && (INTEGRAL_TYPE_P (TREE_TYPE (op0)) | |
8051 | || POINTER_TYPE_P (TREE_TYPE (op0))) | |
a9538d68 | 8052 | && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (op0))) |
389dd41b | 8053 | return fold_convert_loc (loc, type, op0); |
802d9f2f | 8054 | |
8055 | /* Strip inner integral conversions that do not change the precision. */ | |
72dd6141 | 8056 | if (CONVERT_EXPR_P (op0) |
1d9353f3 | 8057 | && (INTEGRAL_TYPE_P (TREE_TYPE (op0)) |
8058 | || POINTER_TYPE_P (TREE_TYPE (op0))) | |
8059 | && (INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (op0, 0))) | |
8060 | || POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (op0, 0)))) | |
802d9f2f | 8061 | && (TYPE_PRECISION (TREE_TYPE (op0)) |
8062 | == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op0, 0))))) | |
389dd41b | 8063 | return fold_build1_loc (loc, VIEW_CONVERT_EXPR, |
8064 | type, TREE_OPERAND (op0, 0)); | |
802d9f2f | 8065 | |
5f4092ed | 8066 | return fold_view_convert_expr (type, op0); |
422c18cb | 8067 | |
8068 | case NEGATE_EXPR: | |
389dd41b | 8069 | tem = fold_negate_expr (loc, arg0); |
58b22aa6 | 8070 | if (tem) |
389dd41b | 8071 | return fold_convert_loc (loc, type, tem); |
e7edfbbd | 8072 | return NULL_TREE; |
422c18cb | 8073 | |
8074 | case ABS_EXPR: | |
8075 | if (TREE_CODE (arg0) == INTEGER_CST || TREE_CODE (arg0) == REAL_CST) | |
8076 | return fold_abs_const (arg0, type); | |
8077 | else if (TREE_CODE (arg0) == NEGATE_EXPR) | |
389dd41b | 8078 | return fold_build1_loc (loc, ABS_EXPR, type, TREE_OPERAND (arg0, 0)); |
422c18cb | 8079 | /* Convert fabs((double)float) into (double)fabsf(float). */ |
8080 | else if (TREE_CODE (arg0) == NOP_EXPR | |
8081 | && TREE_CODE (type) == REAL_TYPE) | |
8082 | { | |
8083 | tree targ0 = strip_float_extensions (arg0); | |
8084 | if (targ0 != arg0) | |
389dd41b | 8085 | return fold_convert_loc (loc, type, |
8086 | fold_build1_loc (loc, ABS_EXPR, | |
8087 | TREE_TYPE (targ0), | |
8088 | targ0)); | |
422c18cb | 8089 | } |
8040d1c5 | 8090 | /* ABS_EXPR<ABS_EXPR<x>> = ABS_EXPR<x> even if flag_wrapv is on. */ |
add6ee5e | 8091 | else if (TREE_CODE (arg0) == ABS_EXPR) |
8092 | return arg0; | |
8093 | else if (tree_expr_nonnegative_p (arg0)) | |
422c18cb | 8094 | return arg0; |
8095 | ||
8096 | /* Strip sign ops from argument. */ | |
8097 | if (TREE_CODE (type) == REAL_TYPE) | |
8098 | { | |
8099 | tem = fold_strip_sign_ops (arg0); | |
8100 | if (tem) | |
389dd41b | 8101 | return fold_build1_loc (loc, ABS_EXPR, type, |
8102 | fold_convert_loc (loc, type, tem)); | |
422c18cb | 8103 | } |
e7edfbbd | 8104 | return NULL_TREE; |
422c18cb | 8105 | |
8106 | case CONJ_EXPR: | |
8107 | if (TREE_CODE (TREE_TYPE (arg0)) != COMPLEX_TYPE) | |
389dd41b | 8108 | return fold_convert_loc (loc, type, arg0); |
75e3ef6e | 8109 | if (TREE_CODE (arg0) == COMPLEX_EXPR) |
8110 | { | |
8111 | tree itype = TREE_TYPE (type); | |
389dd41b | 8112 | tree rpart = fold_convert_loc (loc, itype, TREE_OPERAND (arg0, 0)); |
8113 | tree ipart = fold_convert_loc (loc, itype, TREE_OPERAND (arg0, 1)); | |
8114 | return fold_build2_loc (loc, COMPLEX_EXPR, type, rpart, | |
8115 | negate_expr (ipart)); | |
75e3ef6e | 8116 | } |
8117 | if (TREE_CODE (arg0) == COMPLEX_CST) | |
8118 | { | |
8119 | tree itype = TREE_TYPE (type); | |
389dd41b | 8120 | tree rpart = fold_convert_loc (loc, itype, TREE_REALPART (arg0)); |
8121 | tree ipart = fold_convert_loc (loc, itype, TREE_IMAGPART (arg0)); | |
75e3ef6e | 8122 | return build_complex (type, rpart, negate_expr (ipart)); |
8123 | } | |
8124 | if (TREE_CODE (arg0) == CONJ_EXPR) | |
389dd41b | 8125 | return fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0)); |
e7edfbbd | 8126 | return NULL_TREE; |
422c18cb | 8127 | |
8128 | case BIT_NOT_EXPR: | |
8129 | if (TREE_CODE (arg0) == INTEGER_CST) | |
8130 | return fold_not_const (arg0, type); | |
8131 | else if (TREE_CODE (arg0) == BIT_NOT_EXPR) | |
389dd41b | 8132 | return fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0)); |
422c18cb | 8133 | /* Convert ~ (-A) to A - 1. */ |
8134 | else if (INTEGRAL_TYPE_P (type) && TREE_CODE (arg0) == NEGATE_EXPR) | |
389dd41b | 8135 | return fold_build2_loc (loc, MINUS_EXPR, type, |
8136 | fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0)), | |
7ab7fd4f | 8137 | build_int_cst (type, 1)); |
422c18cb | 8138 | /* Convert ~ (A - 1) or ~ (A + -1) to -A. */ |
8139 | else if (INTEGRAL_TYPE_P (type) | |
8140 | && ((TREE_CODE (arg0) == MINUS_EXPR | |
8141 | && integer_onep (TREE_OPERAND (arg0, 1))) | |
8142 | || (TREE_CODE (arg0) == PLUS_EXPR | |
8143 | && integer_all_onesp (TREE_OPERAND (arg0, 1))))) | |
389dd41b | 8144 | return fold_build1_loc (loc, NEGATE_EXPR, type, |
8145 | fold_convert_loc (loc, type, | |
8146 | TREE_OPERAND (arg0, 0))); | |
039f212d | 8147 | /* Convert ~(X ^ Y) to ~X ^ Y or X ^ ~Y if ~X or ~Y simplify. */ |
8148 | else if (TREE_CODE (arg0) == BIT_XOR_EXPR | |
389dd41b | 8149 | && (tem = fold_unary_loc (loc, BIT_NOT_EXPR, type, |
8150 | fold_convert_loc (loc, type, | |
8151 | TREE_OPERAND (arg0, 0))))) | |
8152 | return fold_build2_loc (loc, BIT_XOR_EXPR, type, tem, | |
8153 | fold_convert_loc (loc, type, | |
8154 | TREE_OPERAND (arg0, 1))); | |
039f212d | 8155 | else if (TREE_CODE (arg0) == BIT_XOR_EXPR |
389dd41b | 8156 | && (tem = fold_unary_loc (loc, BIT_NOT_EXPR, type, |
8157 | fold_convert_loc (loc, type, | |
8158 | TREE_OPERAND (arg0, 1))))) | |
8159 | return fold_build2_loc (loc, BIT_XOR_EXPR, type, | |
8160 | fold_convert_loc (loc, type, | |
8161 | TREE_OPERAND (arg0, 0)), tem); | |
89ee75ae | 8162 | /* Perform BIT_NOT_EXPR on each element individually. */ |
8163 | else if (TREE_CODE (arg0) == VECTOR_CST) | |
8164 | { | |
8165 | tree elements = TREE_VECTOR_CST_ELTS (arg0), elem, list = NULL_TREE; | |
8166 | int count = TYPE_VECTOR_SUBPARTS (type), i; | |
8167 | ||
8168 | for (i = 0; i < count; i++) | |
8169 | { | |
8170 | if (elements) | |
8171 | { | |
8172 | elem = TREE_VALUE (elements); | |
389dd41b | 8173 | elem = fold_unary_loc (loc, BIT_NOT_EXPR, TREE_TYPE (type), elem); |
89ee75ae | 8174 | if (elem == NULL_TREE) |
8175 | break; | |
8176 | elements = TREE_CHAIN (elements); | |
8177 | } | |
8178 | else | |
8179 | elem = build_int_cst (TREE_TYPE (type), -1); | |
8180 | list = tree_cons (NULL_TREE, elem, list); | |
8181 | } | |
8182 | if (i == count) | |
8183 | return build_vector (type, nreverse (list)); | |
8184 | } | |
039f212d | 8185 | |
e7edfbbd | 8186 | return NULL_TREE; |
422c18cb | 8187 | |
8188 | case TRUTH_NOT_EXPR: | |
8189 | /* The argument to invert_truthvalue must have Boolean type. */ | |
8190 | if (TREE_CODE (TREE_TYPE (arg0)) != BOOLEAN_TYPE) | |
389dd41b | 8191 | arg0 = fold_convert_loc (loc, boolean_type_node, arg0); |
422c18cb | 8192 | |
8193 | /* Note that the operand of this must be an int | |
8194 | and its values must be 0 or 1. | |
8195 | ("true" is a fixed value perhaps depending on the language, | |
8196 | but we don't handle values other than 1 correctly yet.) */ | |
389dd41b | 8197 | tem = fold_truth_not_expr (loc, arg0); |
6758b11c | 8198 | if (!tem) |
e7edfbbd | 8199 | return NULL_TREE; |
389dd41b | 8200 | return fold_convert_loc (loc, type, tem); |
422c18cb | 8201 | |
8202 | case REALPART_EXPR: | |
8203 | if (TREE_CODE (TREE_TYPE (arg0)) != COMPLEX_TYPE) | |
389dd41b | 8204 | return fold_convert_loc (loc, type, arg0); |
75e3ef6e | 8205 | if (TREE_CODE (arg0) == COMPLEX_EXPR) |
389dd41b | 8206 | return omit_one_operand_loc (loc, type, TREE_OPERAND (arg0, 0), |
422c18cb | 8207 | TREE_OPERAND (arg0, 1)); |
75e3ef6e | 8208 | if (TREE_CODE (arg0) == COMPLEX_CST) |
389dd41b | 8209 | return fold_convert_loc (loc, type, TREE_REALPART (arg0)); |
75e3ef6e | 8210 | if (TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR) |
8211 | { | |
8212 | tree itype = TREE_TYPE (TREE_TYPE (arg0)); | |
389dd41b | 8213 | tem = fold_build2_loc (loc, TREE_CODE (arg0), itype, |
8214 | fold_build1_loc (loc, REALPART_EXPR, itype, | |
75e3ef6e | 8215 | TREE_OPERAND (arg0, 0)), |
389dd41b | 8216 | fold_build1_loc (loc, REALPART_EXPR, itype, |
75e3ef6e | 8217 | TREE_OPERAND (arg0, 1))); |
389dd41b | 8218 | return fold_convert_loc (loc, type, tem); |
75e3ef6e | 8219 | } |
8220 | if (TREE_CODE (arg0) == CONJ_EXPR) | |
8221 | { | |
8222 | tree itype = TREE_TYPE (TREE_TYPE (arg0)); | |
389dd41b | 8223 | tem = fold_build1_loc (loc, REALPART_EXPR, itype, |
8224 | TREE_OPERAND (arg0, 0)); | |
8225 | return fold_convert_loc (loc, type, tem); | |
75e3ef6e | 8226 | } |
41ac51b1 | 8227 | if (TREE_CODE (arg0) == CALL_EXPR) |
8228 | { | |
8229 | tree fn = get_callee_fndecl (arg0); | |
71bf42bb | 8230 | if (fn && DECL_BUILT_IN_CLASS (fn) == BUILT_IN_NORMAL) |
41ac51b1 | 8231 | switch (DECL_FUNCTION_CODE (fn)) |
8232 | { | |
8233 | CASE_FLT_FN (BUILT_IN_CEXPI): | |
8234 | fn = mathfn_built_in (type, BUILT_IN_COS); | |
2a6b4c77 | 8235 | if (fn) |
389dd41b | 8236 | return build_call_expr_loc (loc, fn, 1, CALL_EXPR_ARG (arg0, 0)); |
2a6b4c77 | 8237 | break; |
41ac51b1 | 8238 | |
2a6b4c77 | 8239 | default: |
8240 | break; | |
41ac51b1 | 8241 | } |
8242 | } | |
e7edfbbd | 8243 | return NULL_TREE; |
422c18cb | 8244 | |
8245 | case IMAGPART_EXPR: | |
8246 | if (TREE_CODE (TREE_TYPE (arg0)) != COMPLEX_TYPE) | |
389dd41b | 8247 | return fold_convert_loc (loc, type, integer_zero_node); |
75e3ef6e | 8248 | if (TREE_CODE (arg0) == COMPLEX_EXPR) |
389dd41b | 8249 | return omit_one_operand_loc (loc, type, TREE_OPERAND (arg0, 1), |
422c18cb | 8250 | TREE_OPERAND (arg0, 0)); |
75e3ef6e | 8251 | if (TREE_CODE (arg0) == COMPLEX_CST) |
389dd41b | 8252 | return fold_convert_loc (loc, type, TREE_IMAGPART (arg0)); |
75e3ef6e | 8253 | if (TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR) |
8254 | { | |
8255 | tree itype = TREE_TYPE (TREE_TYPE (arg0)); | |
389dd41b | 8256 | tem = fold_build2_loc (loc, TREE_CODE (arg0), itype, |
8257 | fold_build1_loc (loc, IMAGPART_EXPR, itype, | |
75e3ef6e | 8258 | TREE_OPERAND (arg0, 0)), |
389dd41b | 8259 | fold_build1_loc (loc, IMAGPART_EXPR, itype, |
75e3ef6e | 8260 | TREE_OPERAND (arg0, 1))); |
389dd41b | 8261 | return fold_convert_loc (loc, type, tem); |
75e3ef6e | 8262 | } |
8263 | if (TREE_CODE (arg0) == CONJ_EXPR) | |
8264 | { | |
8265 | tree itype = TREE_TYPE (TREE_TYPE (arg0)); | |
389dd41b | 8266 | tem = fold_build1_loc (loc, IMAGPART_EXPR, itype, TREE_OPERAND (arg0, 0)); |
8267 | return fold_convert_loc (loc, type, negate_expr (tem)); | |
75e3ef6e | 8268 | } |
41ac51b1 | 8269 | if (TREE_CODE (arg0) == CALL_EXPR) |
8270 | { | |
8271 | tree fn = get_callee_fndecl (arg0); | |
71bf42bb | 8272 | if (fn && DECL_BUILT_IN_CLASS (fn) == BUILT_IN_NORMAL) |
41ac51b1 | 8273 | switch (DECL_FUNCTION_CODE (fn)) |
8274 | { | |
8275 | CASE_FLT_FN (BUILT_IN_CEXPI): | |
8276 | fn = mathfn_built_in (type, BUILT_IN_SIN); | |
2a6b4c77 | 8277 | if (fn) |
389dd41b | 8278 | return build_call_expr_loc (loc, fn, 1, CALL_EXPR_ARG (arg0, 0)); |
2a6b4c77 | 8279 | break; |
41ac51b1 | 8280 | |
2a6b4c77 | 8281 | default: |
8282 | break; | |
41ac51b1 | 8283 | } |
8284 | } | |
e7edfbbd | 8285 | return NULL_TREE; |
422c18cb | 8286 | |
a9b39de8 | 8287 | case INDIRECT_REF: |
8288 | /* Fold *&X to X if X is an lvalue. */ | |
8289 | if (TREE_CODE (op0) == ADDR_EXPR) | |
8290 | { | |
8291 | tree op00 = TREE_OPERAND (op0, 0); | |
8292 | if ((TREE_CODE (op00) == VAR_DECL | |
8293 | || TREE_CODE (op00) == PARM_DECL | |
8294 | || TREE_CODE (op00) == RESULT_DECL) | |
8295 | && !TREE_READONLY (op00)) | |
8296 | return op00; | |
8297 | } | |
8298 | return NULL_TREE; | |
8299 | ||
422c18cb | 8300 | default: |
e7edfbbd | 8301 | return NULL_TREE; |
422c18cb | 8302 | } /* switch (code) */ |
8303 | } | |
8304 | ||
cd30b839 | 8305 | |
8306 | /* If the operation was a conversion do _not_ mark a resulting constant | |
8307 | with TREE_OVERFLOW if the original constant was not. These conversions | |
8308 | have implementation defined behavior and retaining the TREE_OVERFLOW | |
8309 | flag here would confuse later passes such as VRP. */ | |
8310 | tree | |
389dd41b | 8311 | fold_unary_ignore_overflow_loc (location_t loc, enum tree_code code, |
8312 | tree type, tree op0) | |
cd30b839 | 8313 | { |
389dd41b | 8314 | tree res = fold_unary_loc (loc, code, type, op0); |
cd30b839 | 8315 | if (res |
8316 | && TREE_CODE (res) == INTEGER_CST | |
8317 | && TREE_CODE (op0) == INTEGER_CST | |
8318 | && CONVERT_EXPR_CODE_P (code)) | |
8319 | TREE_OVERFLOW (res) = TREE_OVERFLOW (op0); | |
8320 | ||
8321 | return res; | |
8322 | } | |
8323 | ||
7e50ecae | 8324 | /* Fold a binary expression of code CODE and type TYPE with operands |
8325 | OP0 and OP1, containing either a MIN-MAX or a MAX-MIN combination. | |
8326 | Return the folded expression if folding is successful. Otherwise, | |
8327 | return NULL_TREE. */ | |
8328 | ||
8329 | static tree | |
389dd41b | 8330 | fold_minmax (location_t loc, enum tree_code code, tree type, tree op0, tree op1) |
7e50ecae | 8331 | { |
8332 | enum tree_code compl_code; | |
8333 | ||
8334 | if (code == MIN_EXPR) | |
8335 | compl_code = MAX_EXPR; | |
8336 | else if (code == MAX_EXPR) | |
8337 | compl_code = MIN_EXPR; | |
8338 | else | |
d9560eb6 | 8339 | gcc_unreachable (); |
7e50ecae | 8340 | |
8aa01816 | 8341 | /* MIN (MAX (a, b), b) == b. */ |
7e50ecae | 8342 | if (TREE_CODE (op0) == compl_code |
8343 | && operand_equal_p (TREE_OPERAND (op0, 1), op1, 0)) | |
389dd41b | 8344 | return omit_one_operand_loc (loc, type, op1, TREE_OPERAND (op0, 0)); |
7e50ecae | 8345 | |
8aa01816 | 8346 | /* MIN (MAX (b, a), b) == b. */ |
7e50ecae | 8347 | if (TREE_CODE (op0) == compl_code |
8348 | && operand_equal_p (TREE_OPERAND (op0, 0), op1, 0) | |
8349 | && reorder_operands_p (TREE_OPERAND (op0, 1), op1)) | |
389dd41b | 8350 | return omit_one_operand_loc (loc, type, op1, TREE_OPERAND (op0, 1)); |
7e50ecae | 8351 | |
8aa01816 | 8352 | /* MIN (a, MAX (a, b)) == a. */ |
7e50ecae | 8353 | if (TREE_CODE (op1) == compl_code |
8354 | && operand_equal_p (op0, TREE_OPERAND (op1, 0), 0) | |
8355 | && reorder_operands_p (op0, TREE_OPERAND (op1, 1))) | |
389dd41b | 8356 | return omit_one_operand_loc (loc, type, op0, TREE_OPERAND (op1, 1)); |
7e50ecae | 8357 | |
8aa01816 | 8358 | /* MIN (a, MAX (b, a)) == a. */ |
7e50ecae | 8359 | if (TREE_CODE (op1) == compl_code |
8360 | && operand_equal_p (op0, TREE_OPERAND (op1, 1), 0) | |
8361 | && reorder_operands_p (op0, TREE_OPERAND (op1, 0))) | |
389dd41b | 8362 | return omit_one_operand_loc (loc, type, op0, TREE_OPERAND (op1, 0)); |
7e50ecae | 8363 | |
8364 | return NULL_TREE; | |
8365 | } | |
8366 | ||
26e1261a | 8367 | /* Helper that tries to canonicalize the comparison ARG0 CODE ARG1 |
8368 | by changing CODE to reduce the magnitude of constants involved in | |
8369 | ARG0 of the comparison. | |
8370 | Returns a canonicalized comparison tree if a simplification was | |
add6ee5e | 8371 | possible, otherwise returns NULL_TREE. |
8372 | Set *STRICT_OVERFLOW_P to true if the canonicalization is only | |
8373 | valid if signed overflow is undefined. */ | |
26e1261a | 8374 | |
8375 | static tree | |
389dd41b | 8376 | maybe_canonicalize_comparison_1 (location_t loc, enum tree_code code, tree type, |
add6ee5e | 8377 | tree arg0, tree arg1, |
8378 | bool *strict_overflow_p) | |
26e1261a | 8379 | { |
8380 | enum tree_code code0 = TREE_CODE (arg0); | |
8381 | tree t, cst0 = NULL_TREE; | |
8382 | int sgn0; | |
8383 | bool swap = false; | |
8384 | ||
dc5e8aba | 8385 | /* Match A +- CST code arg1 and CST code arg1. We can change the |
8386 | first form only if overflow is undefined. */ | |
8387 | if (!((TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0)) | |
8388 | /* In principle pointers also have undefined overflow behavior, | |
8389 | but that causes problems elsewhere. */ | |
8390 | && !POINTER_TYPE_P (TREE_TYPE (arg0)) | |
8391 | && (code0 == MINUS_EXPR | |
8392 | || code0 == PLUS_EXPR) | |
26e1261a | 8393 | && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST) |
8394 | || code0 == INTEGER_CST)) | |
8395 | return NULL_TREE; | |
8396 | ||
8397 | /* Identify the constant in arg0 and its sign. */ | |
8398 | if (code0 == INTEGER_CST) | |
8399 | cst0 = arg0; | |
8400 | else | |
8401 | cst0 = TREE_OPERAND (arg0, 1); | |
8402 | sgn0 = tree_int_cst_sgn (cst0); | |
8403 | ||
8404 | /* Overflowed constants and zero will cause problems. */ | |
8405 | if (integer_zerop (cst0) | |
8406 | || TREE_OVERFLOW (cst0)) | |
8407 | return NULL_TREE; | |
8408 | ||
fa7637bd | 8409 | /* See if we can reduce the magnitude of the constant in |
26e1261a | 8410 | arg0 by changing the comparison code. */ |
8411 | if (code0 == INTEGER_CST) | |
8412 | { | |
8413 | /* CST <= arg1 -> CST-1 < arg1. */ | |
8414 | if (code == LE_EXPR && sgn0 == 1) | |
8415 | code = LT_EXPR; | |
8416 | /* -CST < arg1 -> -CST-1 <= arg1. */ | |
8417 | else if (code == LT_EXPR && sgn0 == -1) | |
8418 | code = LE_EXPR; | |
8419 | /* CST > arg1 -> CST-1 >= arg1. */ | |
8420 | else if (code == GT_EXPR && sgn0 == 1) | |
8421 | code = GE_EXPR; | |
8422 | /* -CST >= arg1 -> -CST-1 > arg1. */ | |
8423 | else if (code == GE_EXPR && sgn0 == -1) | |
8424 | code = GT_EXPR; | |
8425 | else | |
8426 | return NULL_TREE; | |
8427 | /* arg1 code' CST' might be more canonical. */ | |
8428 | swap = true; | |
8429 | } | |
8430 | else | |
8431 | { | |
8432 | /* A - CST < arg1 -> A - CST-1 <= arg1. */ | |
8433 | if (code == LT_EXPR | |
8434 | && code0 == ((sgn0 == -1) ? PLUS_EXPR : MINUS_EXPR)) | |
8435 | code = LE_EXPR; | |
8436 | /* A + CST > arg1 -> A + CST-1 >= arg1. */ | |
8437 | else if (code == GT_EXPR | |
8438 | && code0 == ((sgn0 == -1) ? MINUS_EXPR : PLUS_EXPR)) | |
8439 | code = GE_EXPR; | |
8440 | /* A + CST <= arg1 -> A + CST-1 < arg1. */ | |
8441 | else if (code == LE_EXPR | |
8442 | && code0 == ((sgn0 == -1) ? MINUS_EXPR : PLUS_EXPR)) | |
8443 | code = LT_EXPR; | |
8444 | /* A - CST >= arg1 -> A - CST-1 > arg1. */ | |
8445 | else if (code == GE_EXPR | |
8446 | && code0 == ((sgn0 == -1) ? PLUS_EXPR : MINUS_EXPR)) | |
8447 | code = GT_EXPR; | |
8448 | else | |
8449 | return NULL_TREE; | |
add6ee5e | 8450 | *strict_overflow_p = true; |
26e1261a | 8451 | } |
8452 | ||
dc5e8aba | 8453 | /* Now build the constant reduced in magnitude. But not if that |
8454 | would produce one outside of its types range. */ | |
8455 | if (INTEGRAL_TYPE_P (TREE_TYPE (cst0)) | |
8456 | && ((sgn0 == 1 | |
8457 | && TYPE_MIN_VALUE (TREE_TYPE (cst0)) | |
8458 | && tree_int_cst_equal (cst0, TYPE_MIN_VALUE (TREE_TYPE (cst0)))) | |
8459 | || (sgn0 == -1 | |
8460 | && TYPE_MAX_VALUE (TREE_TYPE (cst0)) | |
8461 | && tree_int_cst_equal (cst0, TYPE_MAX_VALUE (TREE_TYPE (cst0)))))) | |
8462 | /* We cannot swap the comparison here as that would cause us to | |
8463 | endlessly recurse. */ | |
8464 | return NULL_TREE; | |
8465 | ||
26e1261a | 8466 | t = int_const_binop (sgn0 == -1 ? PLUS_EXPR : MINUS_EXPR, |
dc5e8aba | 8467 | cst0, build_int_cst (TREE_TYPE (cst0), 1), 0); |
26e1261a | 8468 | if (code0 != INTEGER_CST) |
389dd41b | 8469 | t = fold_build2_loc (loc, code0, TREE_TYPE (arg0), TREE_OPERAND (arg0, 0), t); |
26e1261a | 8470 | |
8471 | /* If swapping might yield to a more canonical form, do so. */ | |
8472 | if (swap) | |
389dd41b | 8473 | return fold_build2_loc (loc, swap_tree_comparison (code), type, arg1, t); |
26e1261a | 8474 | else |
389dd41b | 8475 | return fold_build2_loc (loc, code, type, t, arg1); |
26e1261a | 8476 | } |
8477 | ||
8478 | /* Canonicalize the comparison ARG0 CODE ARG1 with type TYPE with undefined | |
8479 | overflow further. Try to decrease the magnitude of constants involved | |
8480 | by changing LE_EXPR and GE_EXPR to LT_EXPR and GT_EXPR or vice versa | |
8481 | and put sole constants at the second argument position. | |
8482 | Returns the canonicalized tree if changed, otherwise NULL_TREE. */ | |
8483 | ||
8484 | static tree | |
389dd41b | 8485 | maybe_canonicalize_comparison (location_t loc, enum tree_code code, tree type, |
26e1261a | 8486 | tree arg0, tree arg1) |
8487 | { | |
8488 | tree t; | |
add6ee5e | 8489 | bool strict_overflow_p; |
8490 | const char * const warnmsg = G_("assuming signed overflow does not occur " | |
8491 | "when reducing constant in comparison"); | |
26e1261a | 8492 | |
26e1261a | 8493 | /* Try canonicalization by simplifying arg0. */ |
add6ee5e | 8494 | strict_overflow_p = false; |
389dd41b | 8495 | t = maybe_canonicalize_comparison_1 (loc, code, type, arg0, arg1, |
add6ee5e | 8496 | &strict_overflow_p); |
26e1261a | 8497 | if (t) |
add6ee5e | 8498 | { |
8499 | if (strict_overflow_p) | |
8500 | fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_MAGNITUDE); | |
8501 | return t; | |
8502 | } | |
26e1261a | 8503 | |
8504 | /* Try canonicalization by simplifying arg1 using the swapped | |
fa7637bd | 8505 | comparison. */ |
26e1261a | 8506 | code = swap_tree_comparison (code); |
add6ee5e | 8507 | strict_overflow_p = false; |
389dd41b | 8508 | t = maybe_canonicalize_comparison_1 (loc, code, type, arg1, arg0, |
add6ee5e | 8509 | &strict_overflow_p); |
8510 | if (t && strict_overflow_p) | |
8511 | fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_MAGNITUDE); | |
8512 | return t; | |
26e1261a | 8513 | } |
8514 | ||
9fa14f44 | 8515 | /* Return whether BASE + OFFSET + BITPOS may wrap around the address |
8516 | space. This is used to avoid issuing overflow warnings for | |
8517 | expressions like &p->x which can not wrap. */ | |
8518 | ||
8519 | static bool | |
8520 | pointer_may_wrap_p (tree base, tree offset, HOST_WIDE_INT bitpos) | |
8521 | { | |
9fa14f44 | 8522 | unsigned HOST_WIDE_INT offset_low, total_low; |
7ded8d6e | 8523 | HOST_WIDE_INT size, offset_high, total_high; |
9fa14f44 | 8524 | |
8525 | if (!POINTER_TYPE_P (TREE_TYPE (base))) | |
8526 | return true; | |
8527 | ||
8528 | if (bitpos < 0) | |
8529 | return true; | |
8530 | ||
9fa14f44 | 8531 | if (offset == NULL_TREE) |
8532 | { | |
8533 | offset_low = 0; | |
8534 | offset_high = 0; | |
8535 | } | |
8536 | else if (TREE_CODE (offset) != INTEGER_CST || TREE_OVERFLOW (offset)) | |
8537 | return true; | |
8538 | else | |
8539 | { | |
8540 | offset_low = TREE_INT_CST_LOW (offset); | |
8541 | offset_high = TREE_INT_CST_HIGH (offset); | |
8542 | } | |
8543 | ||
8544 | if (add_double_with_sign (offset_low, offset_high, | |
8545 | bitpos / BITS_PER_UNIT, 0, | |
8546 | &total_low, &total_high, | |
8547 | true)) | |
8548 | return true; | |
8549 | ||
7ded8d6e | 8550 | if (total_high != 0) |
9fa14f44 | 8551 | return true; |
7ded8d6e | 8552 | |
8553 | size = int_size_in_bytes (TREE_TYPE (TREE_TYPE (base))); | |
8554 | if (size <= 0) | |
8555 | return true; | |
8556 | ||
8557 | /* We can do slightly better for SIZE if we have an ADDR_EXPR of an | |
8558 | array. */ | |
8559 | if (TREE_CODE (base) == ADDR_EXPR) | |
8560 | { | |
8561 | HOST_WIDE_INT base_size; | |
8562 | ||
8563 | base_size = int_size_in_bytes (TREE_TYPE (TREE_OPERAND (base, 0))); | |
8564 | if (base_size > 0 && size < base_size) | |
8565 | size = base_size; | |
8566 | } | |
8567 | ||
8568 | return total_low > (unsigned HOST_WIDE_INT) size; | |
9fa14f44 | 8569 | } |
8570 | ||
6a451e87 | 8571 | /* Subroutine of fold_binary. This routine performs all of the |
8572 | transformations that are common to the equality/inequality | |
8573 | operators (EQ_EXPR and NE_EXPR) and the ordering operators | |
8574 | (LT_EXPR, LE_EXPR, GE_EXPR and GT_EXPR). Callers other than | |
8575 | fold_binary should call fold_binary. Fold a comparison with | |
8576 | tree code CODE and type TYPE with operands OP0 and OP1. Return | |
8577 | the folded comparison or NULL_TREE. */ | |
8578 | ||
8579 | static tree | |
389dd41b | 8580 | fold_comparison (location_t loc, enum tree_code code, tree type, |
8581 | tree op0, tree op1) | |
6a451e87 | 8582 | { |
8583 | tree arg0, arg1, tem; | |
8584 | ||
8585 | arg0 = op0; | |
8586 | arg1 = op1; | |
8587 | ||
8588 | STRIP_SIGN_NOPS (arg0); | |
8589 | STRIP_SIGN_NOPS (arg1); | |
8590 | ||
8591 | tem = fold_relational_const (code, type, arg0, arg1); | |
8592 | if (tem != NULL_TREE) | |
8593 | return tem; | |
8594 | ||
8595 | /* If one arg is a real or integer constant, put it last. */ | |
8596 | if (tree_swap_operands_p (arg0, arg1, true)) | |
389dd41b | 8597 | return fold_build2_loc (loc, swap_tree_comparison (code), type, op1, op0); |
6a451e87 | 8598 | |
6a451e87 | 8599 | /* Transform comparisons of the form X +- C1 CMP C2 to X CMP C2 +- C1. */ |
8600 | if ((TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR) | |
8601 | && (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST | |
8602 | && !TREE_OVERFLOW (TREE_OPERAND (arg0, 1)) | |
981eb798 | 8603 | && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1))) |
6a451e87 | 8604 | && (TREE_CODE (arg1) == INTEGER_CST |
8605 | && !TREE_OVERFLOW (arg1))) | |
8606 | { | |
8607 | tree const1 = TREE_OPERAND (arg0, 1); | |
8608 | tree const2 = arg1; | |
8609 | tree variable = TREE_OPERAND (arg0, 0); | |
8610 | tree lhs; | |
8611 | int lhs_add; | |
8612 | lhs_add = TREE_CODE (arg0) != PLUS_EXPR; | |
8613 | ||
389dd41b | 8614 | lhs = fold_build2_loc (loc, lhs_add ? PLUS_EXPR : MINUS_EXPR, |
6a451e87 | 8615 | TREE_TYPE (arg1), const2, const1); |
a9995c22 | 8616 | |
8617 | /* If the constant operation overflowed this can be | |
8618 | simplified as a comparison against INT_MAX/INT_MIN. */ | |
8619 | if (TREE_CODE (lhs) == INTEGER_CST | |
8620 | && TREE_OVERFLOW (lhs)) | |
8621 | { | |
8622 | int const1_sgn = tree_int_cst_sgn (const1); | |
8623 | enum tree_code code2 = code; | |
8624 | ||
8625 | /* Get the sign of the constant on the lhs if the | |
8626 | operation were VARIABLE + CONST1. */ | |
8627 | if (TREE_CODE (arg0) == MINUS_EXPR) | |
8628 | const1_sgn = -const1_sgn; | |
8629 | ||
8630 | /* The sign of the constant determines if we overflowed | |
8631 | INT_MAX (const1_sgn == -1) or INT_MIN (const1_sgn == 1). | |
8632 | Canonicalize to the INT_MIN overflow by swapping the comparison | |
8633 | if necessary. */ | |
8634 | if (const1_sgn == -1) | |
8635 | code2 = swap_tree_comparison (code); | |
8636 | ||
8637 | /* We now can look at the canonicalized case | |
8638 | VARIABLE + 1 CODE2 INT_MIN | |
8639 | and decide on the result. */ | |
8640 | if (code2 == LT_EXPR | |
8641 | || code2 == LE_EXPR | |
8642 | || code2 == EQ_EXPR) | |
389dd41b | 8643 | return omit_one_operand_loc (loc, type, boolean_false_node, variable); |
a9995c22 | 8644 | else if (code2 == NE_EXPR |
8645 | || code2 == GE_EXPR | |
8646 | || code2 == GT_EXPR) | |
389dd41b | 8647 | return omit_one_operand_loc (loc, type, boolean_true_node, variable); |
a9995c22 | 8648 | } |
8649 | ||
6a451e87 | 8650 | if (TREE_CODE (lhs) == TREE_CODE (arg1) |
8651 | && (TREE_CODE (lhs) != INTEGER_CST | |
8652 | || !TREE_OVERFLOW (lhs))) | |
add6ee5e | 8653 | { |
8654 | fold_overflow_warning (("assuming signed overflow does not occur " | |
8655 | "when changing X +- C1 cmp C2 to " | |
8656 | "X cmp C1 +- C2"), | |
8657 | WARN_STRICT_OVERFLOW_COMPARISON); | |
389dd41b | 8658 | return fold_build2_loc (loc, code, type, variable, lhs); |
add6ee5e | 8659 | } |
6a451e87 | 8660 | } |
8661 | ||
cb8fc1a4 | 8662 | /* For comparisons of pointers we can decompose it to a compile time |
8663 | comparison of the base objects and the offsets into the object. | |
ad92d3a8 | 8664 | This requires at least one operand being an ADDR_EXPR or a |
8665 | POINTER_PLUS_EXPR to do more than the operand_equal_p test below. */ | |
cb8fc1a4 | 8666 | if (POINTER_TYPE_P (TREE_TYPE (arg0)) |
8667 | && (TREE_CODE (arg0) == ADDR_EXPR | |
ad92d3a8 | 8668 | || TREE_CODE (arg1) == ADDR_EXPR |
8669 | || TREE_CODE (arg0) == POINTER_PLUS_EXPR | |
8670 | || TREE_CODE (arg1) == POINTER_PLUS_EXPR)) | |
cb8fc1a4 | 8671 | { |
8672 | tree base0, base1, offset0 = NULL_TREE, offset1 = NULL_TREE; | |
8673 | HOST_WIDE_INT bitsize, bitpos0 = 0, bitpos1 = 0; | |
8674 | enum machine_mode mode; | |
8675 | int volatilep, unsignedp; | |
ffdf1c47 | 8676 | bool indirect_base0 = false, indirect_base1 = false; |
cb8fc1a4 | 8677 | |
8678 | /* Get base and offset for the access. Strip ADDR_EXPR for | |
8679 | get_inner_reference, but put it back by stripping INDIRECT_REF | |
ffdf1c47 | 8680 | off the base object if possible. indirect_baseN will be true |
8681 | if baseN is not an address but refers to the object itself. */ | |
cb8fc1a4 | 8682 | base0 = arg0; |
8683 | if (TREE_CODE (arg0) == ADDR_EXPR) | |
8684 | { | |
8685 | base0 = get_inner_reference (TREE_OPERAND (arg0, 0), | |
8686 | &bitsize, &bitpos0, &offset0, &mode, | |
8687 | &unsignedp, &volatilep, false); | |
8688 | if (TREE_CODE (base0) == INDIRECT_REF) | |
8689 | base0 = TREE_OPERAND (base0, 0); | |
8690 | else | |
8691 | indirect_base0 = true; | |
8692 | } | |
ad92d3a8 | 8693 | else if (TREE_CODE (arg0) == POINTER_PLUS_EXPR) |
8694 | { | |
8695 | base0 = TREE_OPERAND (arg0, 0); | |
8696 | offset0 = TREE_OPERAND (arg0, 1); | |
8697 | } | |
cb8fc1a4 | 8698 | |
8699 | base1 = arg1; | |
8700 | if (TREE_CODE (arg1) == ADDR_EXPR) | |
8701 | { | |
8702 | base1 = get_inner_reference (TREE_OPERAND (arg1, 0), | |
8703 | &bitsize, &bitpos1, &offset1, &mode, | |
8704 | &unsignedp, &volatilep, false); | |
ffdf1c47 | 8705 | if (TREE_CODE (base1) == INDIRECT_REF) |
cb8fc1a4 | 8706 | base1 = TREE_OPERAND (base1, 0); |
ffdf1c47 | 8707 | else |
8708 | indirect_base1 = true; | |
cb8fc1a4 | 8709 | } |
ad92d3a8 | 8710 | else if (TREE_CODE (arg1) == POINTER_PLUS_EXPR) |
8711 | { | |
8712 | base1 = TREE_OPERAND (arg1, 0); | |
8713 | offset1 = TREE_OPERAND (arg1, 1); | |
8714 | } | |
cb8fc1a4 | 8715 | |
2b6cd5e4 | 8716 | /* A local variable can never be pointed to by |
8717 | the default SSA name of an incoming parameter. */ | |
8718 | if ((TREE_CODE (arg0) == ADDR_EXPR | |
8719 | && indirect_base0 | |
8720 | && TREE_CODE (base0) == VAR_DECL | |
8721 | && auto_var_in_fn_p (base0, current_function_decl) | |
8722 | && !indirect_base1 | |
8723 | && TREE_CODE (base1) == SSA_NAME | |
8724 | && TREE_CODE (SSA_NAME_VAR (base1)) == PARM_DECL | |
8725 | && SSA_NAME_IS_DEFAULT_DEF (base1)) | |
8726 | || (TREE_CODE (arg1) == ADDR_EXPR | |
8727 | && indirect_base1 | |
8728 | && TREE_CODE (base1) == VAR_DECL | |
8729 | && auto_var_in_fn_p (base1, current_function_decl) | |
8730 | && !indirect_base0 | |
8731 | && TREE_CODE (base0) == SSA_NAME | |
8732 | && TREE_CODE (SSA_NAME_VAR (base0)) == PARM_DECL | |
8733 | && SSA_NAME_IS_DEFAULT_DEF (base0))) | |
8734 | { | |
8735 | if (code == NE_EXPR) | |
8736 | return constant_boolean_node (1, type); | |
8737 | else if (code == EQ_EXPR) | |
8738 | return constant_boolean_node (0, type); | |
8739 | } | |
cb8fc1a4 | 8740 | /* If we have equivalent bases we might be able to simplify. */ |
2b6cd5e4 | 8741 | else if (indirect_base0 == indirect_base1 |
8742 | && operand_equal_p (base0, base1, 0)) | |
cb8fc1a4 | 8743 | { |
8744 | /* We can fold this expression to a constant if the non-constant | |
8745 | offset parts are equal. */ | |
9fa14f44 | 8746 | if ((offset0 == offset1 |
8747 | || (offset0 && offset1 | |
8748 | && operand_equal_p (offset0, offset1, 0))) | |
8749 | && (code == EQ_EXPR | |
8750 | || code == NE_EXPR | |
8751 | || POINTER_TYPE_OVERFLOW_UNDEFINED)) | |
48e1416a | 8752 | |
cb8fc1a4 | 8753 | { |
9fa14f44 | 8754 | if (code != EQ_EXPR |
8755 | && code != NE_EXPR | |
8756 | && bitpos0 != bitpos1 | |
8757 | && (pointer_may_wrap_p (base0, offset0, bitpos0) | |
8758 | || pointer_may_wrap_p (base1, offset1, bitpos1))) | |
8759 | fold_overflow_warning (("assuming pointer wraparound does not " | |
8760 | "occur when comparing P +- C1 with " | |
8761 | "P +- C2"), | |
8762 | WARN_STRICT_OVERFLOW_CONDITIONAL); | |
8763 | ||
cb8fc1a4 | 8764 | switch (code) |
8765 | { | |
8766 | case EQ_EXPR: | |
bd233430 | 8767 | return constant_boolean_node (bitpos0 == bitpos1, type); |
cb8fc1a4 | 8768 | case NE_EXPR: |
bd233430 | 8769 | return constant_boolean_node (bitpos0 != bitpos1, type); |
cb8fc1a4 | 8770 | case LT_EXPR: |
bd233430 | 8771 | return constant_boolean_node (bitpos0 < bitpos1, type); |
cb8fc1a4 | 8772 | case LE_EXPR: |
bd233430 | 8773 | return constant_boolean_node (bitpos0 <= bitpos1, type); |
cb8fc1a4 | 8774 | case GE_EXPR: |
bd233430 | 8775 | return constant_boolean_node (bitpos0 >= bitpos1, type); |
cb8fc1a4 | 8776 | case GT_EXPR: |
bd233430 | 8777 | return constant_boolean_node (bitpos0 > bitpos1, type); |
cb8fc1a4 | 8778 | default:; |
8779 | } | |
8780 | } | |
8781 | /* We can simplify the comparison to a comparison of the variable | |
8782 | offset parts if the constant offset parts are equal. | |
8783 | Be careful to use signed size type here because otherwise we | |
8784 | mess with array offsets in the wrong way. This is possible | |
8785 | because pointer arithmetic is restricted to retain within an | |
8786 | object and overflow on pointer differences is undefined as of | |
8787 | 6.5.6/8 and /9 with respect to the signed ptrdiff_t. */ | |
e867fa7f | 8788 | else if (bitpos0 == bitpos1 |
8789 | && ((code == EQ_EXPR || code == NE_EXPR) | |
8790 | || POINTER_TYPE_OVERFLOW_UNDEFINED)) | |
cb8fc1a4 | 8791 | { |
cb8fc1a4 | 8792 | /* By converting to signed size type we cover middle-end pointer |
8793 | arithmetic which operates on unsigned pointer types of size | |
8794 | type size and ARRAY_REF offsets which are properly sign or | |
8795 | zero extended from their type in case it is narrower than | |
8796 | size type. */ | |
8797 | if (offset0 == NULL_TREE) | |
ad086ed4 | 8798 | offset0 = build_int_cst (ssizetype, 0); |
cb8fc1a4 | 8799 | else |
ad086ed4 | 8800 | offset0 = fold_convert_loc (loc, ssizetype, offset0); |
cb8fc1a4 | 8801 | if (offset1 == NULL_TREE) |
ad086ed4 | 8802 | offset1 = build_int_cst (ssizetype, 0); |
cb8fc1a4 | 8803 | else |
ad086ed4 | 8804 | offset1 = fold_convert_loc (loc, ssizetype, offset1); |
cb8fc1a4 | 8805 | |
9fa14f44 | 8806 | if (code != EQ_EXPR |
8807 | && code != NE_EXPR | |
8808 | && (pointer_may_wrap_p (base0, offset0, bitpos0) | |
8809 | || pointer_may_wrap_p (base1, offset1, bitpos1))) | |
e867fa7f | 8810 | fold_overflow_warning (("assuming pointer wraparound does not " |
8811 | "occur when comparing P +- C1 with " | |
8812 | "P +- C2"), | |
8813 | WARN_STRICT_OVERFLOW_COMPARISON); | |
8814 | ||
389dd41b | 8815 | return fold_build2_loc (loc, code, type, offset0, offset1); |
cb8fc1a4 | 8816 | } |
8817 | } | |
ffdf1c47 | 8818 | /* For non-equal bases we can simplify if they are addresses |
8819 | of local binding decls or constants. */ | |
8820 | else if (indirect_base0 && indirect_base1 | |
8821 | /* We know that !operand_equal_p (base0, base1, 0) | |
dd691fd8 | 8822 | because the if condition was false. But make |
8823 | sure two decls are not the same. */ | |
8824 | && base0 != base1 | |
ffdf1c47 | 8825 | && TREE_CODE (arg0) == ADDR_EXPR |
8826 | && TREE_CODE (arg1) == ADDR_EXPR | |
dd691fd8 | 8827 | && (((TREE_CODE (base0) == VAR_DECL |
8828 | || TREE_CODE (base0) == PARM_DECL) | |
ffdf1c47 | 8829 | && (targetm.binds_local_p (base0) |
8830 | || CONSTANT_CLASS_P (base1))) | |
8831 | || CONSTANT_CLASS_P (base0)) | |
dd691fd8 | 8832 | && (((TREE_CODE (base1) == VAR_DECL |
8833 | || TREE_CODE (base1) == PARM_DECL) | |
ffdf1c47 | 8834 | && (targetm.binds_local_p (base1) |
8835 | || CONSTANT_CLASS_P (base0))) | |
8836 | || CONSTANT_CLASS_P (base1))) | |
8837 | { | |
8838 | if (code == EQ_EXPR) | |
389dd41b | 8839 | return omit_two_operands_loc (loc, type, boolean_false_node, |
8840 | arg0, arg1); | |
ffdf1c47 | 8841 | else if (code == NE_EXPR) |
389dd41b | 8842 | return omit_two_operands_loc (loc, type, boolean_true_node, |
8843 | arg0, arg1); | |
ffdf1c47 | 8844 | } |
8845 | /* For equal offsets we can simplify to a comparison of the | |
8846 | base addresses. */ | |
8847 | else if (bitpos0 == bitpos1 | |
8848 | && (indirect_base0 | |
8849 | ? base0 != TREE_OPERAND (arg0, 0) : base0 != arg0) | |
8850 | && (indirect_base1 | |
8851 | ? base1 != TREE_OPERAND (arg1, 0) : base1 != arg1) | |
8852 | && ((offset0 == offset1) | |
8853 | || (offset0 && offset1 | |
8854 | && operand_equal_p (offset0, offset1, 0)))) | |
8855 | { | |
8856 | if (indirect_base0) | |
389dd41b | 8857 | base0 = build_fold_addr_expr_loc (loc, base0); |
ffdf1c47 | 8858 | if (indirect_base1) |
389dd41b | 8859 | base1 = build_fold_addr_expr_loc (loc, base1); |
8860 | return fold_build2_loc (loc, code, type, base0, base1); | |
ffdf1c47 | 8861 | } |
cb8fc1a4 | 8862 | } |
8863 | ||
91ceb6b7 | 8864 | /* Transform comparisons of the form X +- C1 CMP Y +- C2 to |
8865 | X CMP Y +- C2 +- C1 for signed X, Y. This is valid if | |
8866 | the resulting offset is smaller in absolute value than the | |
8867 | original one. */ | |
981eb798 | 8868 | if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0)) |
91ceb6b7 | 8869 | && (TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR) |
8870 | && (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST | |
8871 | && !TREE_OVERFLOW (TREE_OPERAND (arg0, 1))) | |
8872 | && (TREE_CODE (arg1) == PLUS_EXPR || TREE_CODE (arg1) == MINUS_EXPR) | |
8873 | && (TREE_CODE (TREE_OPERAND (arg1, 1)) == INTEGER_CST | |
8874 | && !TREE_OVERFLOW (TREE_OPERAND (arg1, 1)))) | |
8875 | { | |
8876 | tree const1 = TREE_OPERAND (arg0, 1); | |
8877 | tree const2 = TREE_OPERAND (arg1, 1); | |
8878 | tree variable1 = TREE_OPERAND (arg0, 0); | |
8879 | tree variable2 = TREE_OPERAND (arg1, 0); | |
8880 | tree cst; | |
add6ee5e | 8881 | const char * const warnmsg = G_("assuming signed overflow does not " |
8882 | "occur when combining constants around " | |
8883 | "a comparison"); | |
91ceb6b7 | 8884 | |
8885 | /* Put the constant on the side where it doesn't overflow and is | |
8886 | of lower absolute value than before. */ | |
8887 | cst = int_const_binop (TREE_CODE (arg0) == TREE_CODE (arg1) | |
8888 | ? MINUS_EXPR : PLUS_EXPR, | |
8889 | const2, const1, 0); | |
8890 | if (!TREE_OVERFLOW (cst) | |
8891 | && tree_int_cst_compare (const2, cst) == tree_int_cst_sgn (const2)) | |
add6ee5e | 8892 | { |
8893 | fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_COMPARISON); | |
389dd41b | 8894 | return fold_build2_loc (loc, code, type, |
add6ee5e | 8895 | variable1, |
389dd41b | 8896 | fold_build2_loc (loc, |
8897 | TREE_CODE (arg1), TREE_TYPE (arg1), | |
add6ee5e | 8898 | variable2, cst)); |
8899 | } | |
91ceb6b7 | 8900 | |
8901 | cst = int_const_binop (TREE_CODE (arg0) == TREE_CODE (arg1) | |
8902 | ? MINUS_EXPR : PLUS_EXPR, | |
8903 | const1, const2, 0); | |
8904 | if (!TREE_OVERFLOW (cst) | |
8905 | && tree_int_cst_compare (const1, cst) == tree_int_cst_sgn (const1)) | |
add6ee5e | 8906 | { |
8907 | fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_COMPARISON); | |
389dd41b | 8908 | return fold_build2_loc (loc, code, type, |
8909 | fold_build2_loc (loc, TREE_CODE (arg0), TREE_TYPE (arg0), | |
add6ee5e | 8910 | variable1, cst), |
8911 | variable2); | |
8912 | } | |
91ceb6b7 | 8913 | } |
8914 | ||
9112c6d3 | 8915 | /* Transform comparisons of the form X * C1 CMP 0 to X CMP 0 in the |
8916 | signed arithmetic case. That form is created by the compiler | |
8917 | often enough for folding it to be of value. One example is in | |
8918 | computing loop trip counts after Operator Strength Reduction. */ | |
981eb798 | 8919 | if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0)) |
9112c6d3 | 8920 | && TREE_CODE (arg0) == MULT_EXPR |
8921 | && (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST | |
8922 | && !TREE_OVERFLOW (TREE_OPERAND (arg0, 1))) | |
8923 | && integer_zerop (arg1)) | |
8924 | { | |
8925 | tree const1 = TREE_OPERAND (arg0, 1); | |
8926 | tree const2 = arg1; /* zero */ | |
8927 | tree variable1 = TREE_OPERAND (arg0, 0); | |
8928 | enum tree_code cmp_code = code; | |
8929 | ||
30806cf1 | 8930 | /* Handle unfolded multiplication by zero. */ |
8931 | if (integer_zerop (const1)) | |
8932 | return fold_build2_loc (loc, cmp_code, type, const1, const2); | |
9112c6d3 | 8933 | |
add6ee5e | 8934 | fold_overflow_warning (("assuming signed overflow does not occur when " |
8935 | "eliminating multiplication in comparison " | |
8936 | "with zero"), | |
8937 | WARN_STRICT_OVERFLOW_COMPARISON); | |
8938 | ||
9112c6d3 | 8939 | /* If const1 is negative we swap the sense of the comparison. */ |
8940 | if (tree_int_cst_sgn (const1) < 0) | |
8941 | cmp_code = swap_tree_comparison (cmp_code); | |
8942 | ||
389dd41b | 8943 | return fold_build2_loc (loc, cmp_code, type, variable1, const2); |
9112c6d3 | 8944 | } |
8945 | ||
389dd41b | 8946 | tem = maybe_canonicalize_comparison (loc, code, type, op0, op1); |
26e1261a | 8947 | if (tem) |
8948 | return tem; | |
8949 | ||
6a451e87 | 8950 | if (FLOAT_TYPE_P (TREE_TYPE (arg0))) |
8951 | { | |
8952 | tree targ0 = strip_float_extensions (arg0); | |
8953 | tree targ1 = strip_float_extensions (arg1); | |
8954 | tree newtype = TREE_TYPE (targ0); | |
8955 | ||
8956 | if (TYPE_PRECISION (TREE_TYPE (targ1)) > TYPE_PRECISION (newtype)) | |
8957 | newtype = TREE_TYPE (targ1); | |
8958 | ||
8959 | /* Fold (double)float1 CMP (double)float2 into float1 CMP float2. */ | |
8960 | if (TYPE_PRECISION (newtype) < TYPE_PRECISION (TREE_TYPE (arg0))) | |
389dd41b | 8961 | return fold_build2_loc (loc, code, type, |
8962 | fold_convert_loc (loc, newtype, targ0), | |
8963 | fold_convert_loc (loc, newtype, targ1)); | |
6a451e87 | 8964 | |
8965 | /* (-a) CMP (-b) -> b CMP a */ | |
8966 | if (TREE_CODE (arg0) == NEGATE_EXPR | |
8967 | && TREE_CODE (arg1) == NEGATE_EXPR) | |
389dd41b | 8968 | return fold_build2_loc (loc, code, type, TREE_OPERAND (arg1, 0), |
6a451e87 | 8969 | TREE_OPERAND (arg0, 0)); |
8970 | ||
8971 | if (TREE_CODE (arg1) == REAL_CST) | |
8972 | { | |
8973 | REAL_VALUE_TYPE cst; | |
8974 | cst = TREE_REAL_CST (arg1); | |
8975 | ||
8976 | /* (-a) CMP CST -> a swap(CMP) (-CST) */ | |
8977 | if (TREE_CODE (arg0) == NEGATE_EXPR) | |
389dd41b | 8978 | return fold_build2_loc (loc, swap_tree_comparison (code), type, |
6a451e87 | 8979 | TREE_OPERAND (arg0, 0), |
8980 | build_real (TREE_TYPE (arg1), | |
dae0b5cb | 8981 | real_value_negate (&cst))); |
6a451e87 | 8982 | |
8983 | /* IEEE doesn't distinguish +0 and -0 in comparisons. */ | |
8984 | /* a CMP (-0) -> a CMP 0 */ | |
8985 | if (REAL_VALUE_MINUS_ZERO (cst)) | |
389dd41b | 8986 | return fold_build2_loc (loc, code, type, arg0, |
6a451e87 | 8987 | build_real (TREE_TYPE (arg1), dconst0)); |
8988 | ||
8989 | /* x != NaN is always true, other ops are always false. */ | |
8990 | if (REAL_VALUE_ISNAN (cst) | |
8991 | && ! HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg1)))) | |
8992 | { | |
8993 | tem = (code == NE_EXPR) ? integer_one_node : integer_zero_node; | |
389dd41b | 8994 | return omit_one_operand_loc (loc, type, tem, arg0); |
6a451e87 | 8995 | } |
8996 | ||
8997 | /* Fold comparisons against infinity. */ | |
944017fd | 8998 | if (REAL_VALUE_ISINF (cst) |
8999 | && MODE_HAS_INFINITIES (TYPE_MODE (TREE_TYPE (arg1)))) | |
6a451e87 | 9000 | { |
389dd41b | 9001 | tem = fold_inf_compare (loc, code, type, arg0, arg1); |
6a451e87 | 9002 | if (tem != NULL_TREE) |
9003 | return tem; | |
9004 | } | |
9005 | } | |
9006 | ||
9007 | /* If this is a comparison of a real constant with a PLUS_EXPR | |
9008 | or a MINUS_EXPR of a real constant, we can convert it into a | |
9009 | comparison with a revised real constant as long as no overflow | |
9010 | occurs when unsafe_math_optimizations are enabled. */ | |
9011 | if (flag_unsafe_math_optimizations | |
9012 | && TREE_CODE (arg1) == REAL_CST | |
9013 | && (TREE_CODE (arg0) == PLUS_EXPR | |
9014 | || TREE_CODE (arg0) == MINUS_EXPR) | |
9015 | && TREE_CODE (TREE_OPERAND (arg0, 1)) == REAL_CST | |
9016 | && 0 != (tem = const_binop (TREE_CODE (arg0) == PLUS_EXPR | |
9017 | ? MINUS_EXPR : PLUS_EXPR, | |
9018 | arg1, TREE_OPERAND (arg0, 1), 0)) | |
f96bd2bf | 9019 | && !TREE_OVERFLOW (tem)) |
389dd41b | 9020 | return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0), tem); |
6a451e87 | 9021 | |
9022 | /* Likewise, we can simplify a comparison of a real constant with | |
9023 | a MINUS_EXPR whose first operand is also a real constant, i.e. | |
48e1416a | 9024 | (c1 - x) < c2 becomes x > c1-c2. Reordering is allowed on |
49d060d7 | 9025 | floating-point types only if -fassociative-math is set. */ |
9026 | if (flag_associative_math | |
6a451e87 | 9027 | && TREE_CODE (arg1) == REAL_CST |
9028 | && TREE_CODE (arg0) == MINUS_EXPR | |
9029 | && TREE_CODE (TREE_OPERAND (arg0, 0)) == REAL_CST | |
9030 | && 0 != (tem = const_binop (MINUS_EXPR, TREE_OPERAND (arg0, 0), | |
9031 | arg1, 0)) | |
f96bd2bf | 9032 | && !TREE_OVERFLOW (tem)) |
389dd41b | 9033 | return fold_build2_loc (loc, swap_tree_comparison (code), type, |
6a451e87 | 9034 | TREE_OPERAND (arg0, 1), tem); |
9035 | ||
9036 | /* Fold comparisons against built-in math functions. */ | |
9037 | if (TREE_CODE (arg1) == REAL_CST | |
9038 | && flag_unsafe_math_optimizations | |
9039 | && ! flag_errno_math) | |
9040 | { | |
9041 | enum built_in_function fcode = builtin_mathfn_code (arg0); | |
9042 | ||
9043 | if (fcode != END_BUILTINS) | |
9044 | { | |
389dd41b | 9045 | tem = fold_mathfn_compare (loc, fcode, code, type, arg0, arg1); |
6a451e87 | 9046 | if (tem != NULL_TREE) |
9047 | return tem; | |
9048 | } | |
9049 | } | |
9050 | } | |
9051 | ||
6a451e87 | 9052 | if (TREE_CODE (TREE_TYPE (arg0)) == INTEGER_TYPE |
72dd6141 | 9053 | && CONVERT_EXPR_P (arg0)) |
6a451e87 | 9054 | { |
9055 | /* If we are widening one operand of an integer comparison, | |
9056 | see if the other operand is similarly being widened. Perhaps we | |
9057 | can do the comparison in the narrower type. */ | |
389dd41b | 9058 | tem = fold_widened_comparison (loc, code, type, arg0, arg1); |
6a451e87 | 9059 | if (tem) |
9060 | return tem; | |
9061 | ||
9062 | /* Or if we are changing signedness. */ | |
389dd41b | 9063 | tem = fold_sign_changed_comparison (loc, code, type, arg0, arg1); |
6a451e87 | 9064 | if (tem) |
9065 | return tem; | |
9066 | } | |
9067 | ||
9068 | /* If this is comparing a constant with a MIN_EXPR or a MAX_EXPR of a | |
9069 | constant, we can simplify it. */ | |
9070 | if (TREE_CODE (arg1) == INTEGER_CST | |
9071 | && (TREE_CODE (arg0) == MIN_EXPR | |
9072 | || TREE_CODE (arg0) == MAX_EXPR) | |
9073 | && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST) | |
9074 | { | |
389dd41b | 9075 | tem = optimize_minmax_comparison (loc, code, type, op0, op1); |
6a451e87 | 9076 | if (tem) |
9077 | return tem; | |
9078 | } | |
9079 | ||
9080 | /* Simplify comparison of something with itself. (For IEEE | |
9081 | floating-point, we can only do some of these simplifications.) */ | |
9082 | if (operand_equal_p (arg0, arg1, 0)) | |
9083 | { | |
9084 | switch (code) | |
9085 | { | |
9086 | case EQ_EXPR: | |
9087 | if (! FLOAT_TYPE_P (TREE_TYPE (arg0)) | |
9088 | || ! HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0)))) | |
9089 | return constant_boolean_node (1, type); | |
9090 | break; | |
9091 | ||
9092 | case GE_EXPR: | |
9093 | case LE_EXPR: | |
9094 | if (! FLOAT_TYPE_P (TREE_TYPE (arg0)) | |
9095 | || ! HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0)))) | |
9096 | return constant_boolean_node (1, type); | |
389dd41b | 9097 | return fold_build2_loc (loc, EQ_EXPR, type, arg0, arg1); |
6a451e87 | 9098 | |
9099 | case NE_EXPR: | |
9100 | /* For NE, we can only do this simplification if integer | |
9101 | or we don't honor IEEE floating point NaNs. */ | |
9102 | if (FLOAT_TYPE_P (TREE_TYPE (arg0)) | |
9103 | && HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0)))) | |
9104 | break; | |
9105 | /* ... fall through ... */ | |
9106 | case GT_EXPR: | |
9107 | case LT_EXPR: | |
9108 | return constant_boolean_node (0, type); | |
9109 | default: | |
9110 | gcc_unreachable (); | |
9111 | } | |
9112 | } | |
9113 | ||
9114 | /* If we are comparing an expression that just has comparisons | |
9115 | of two integer values, arithmetic expressions of those comparisons, | |
9116 | and constants, we can simplify it. There are only three cases | |
9117 | to check: the two values can either be equal, the first can be | |
9118 | greater, or the second can be greater. Fold the expression for | |
9119 | those three values. Since each value must be 0 or 1, we have | |
9120 | eight possibilities, each of which corresponds to the constant 0 | |
9121 | or 1 or one of the six possible comparisons. | |
9122 | ||
9123 | This handles common cases like (a > b) == 0 but also handles | |
9124 | expressions like ((x > y) - (y > x)) > 0, which supposedly | |
9125 | occur in macroized code. */ | |
9126 | ||
9127 | if (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg0) != INTEGER_CST) | |
9128 | { | |
9129 | tree cval1 = 0, cval2 = 0; | |
9130 | int save_p = 0; | |
9131 | ||
9132 | if (twoval_comparison_p (arg0, &cval1, &cval2, &save_p) | |
9133 | /* Don't handle degenerate cases here; they should already | |
9134 | have been handled anyway. */ | |
9135 | && cval1 != 0 && cval2 != 0 | |
9136 | && ! (TREE_CONSTANT (cval1) && TREE_CONSTANT (cval2)) | |
9137 | && TREE_TYPE (cval1) == TREE_TYPE (cval2) | |
9138 | && INTEGRAL_TYPE_P (TREE_TYPE (cval1)) | |
9139 | && TYPE_MAX_VALUE (TREE_TYPE (cval1)) | |
9140 | && TYPE_MAX_VALUE (TREE_TYPE (cval2)) | |
9141 | && ! operand_equal_p (TYPE_MIN_VALUE (TREE_TYPE (cval1)), | |
9142 | TYPE_MAX_VALUE (TREE_TYPE (cval2)), 0)) | |
9143 | { | |
9144 | tree maxval = TYPE_MAX_VALUE (TREE_TYPE (cval1)); | |
9145 | tree minval = TYPE_MIN_VALUE (TREE_TYPE (cval1)); | |
9146 | ||
9147 | /* We can't just pass T to eval_subst in case cval1 or cval2 | |
9148 | was the same as ARG1. */ | |
9149 | ||
9150 | tree high_result | |
389dd41b | 9151 | = fold_build2_loc (loc, code, type, |
9152 | eval_subst (loc, arg0, cval1, maxval, | |
6a451e87 | 9153 | cval2, minval), |
9154 | arg1); | |
9155 | tree equal_result | |
389dd41b | 9156 | = fold_build2_loc (loc, code, type, |
9157 | eval_subst (loc, arg0, cval1, maxval, | |
6a451e87 | 9158 | cval2, maxval), |
9159 | arg1); | |
9160 | tree low_result | |
389dd41b | 9161 | = fold_build2_loc (loc, code, type, |
9162 | eval_subst (loc, arg0, cval1, minval, | |
6a451e87 | 9163 | cval2, maxval), |
9164 | arg1); | |
9165 | ||
9166 | /* All three of these results should be 0 or 1. Confirm they are. | |
9167 | Then use those values to select the proper code to use. */ | |
9168 | ||
9169 | if (TREE_CODE (high_result) == INTEGER_CST | |
9170 | && TREE_CODE (equal_result) == INTEGER_CST | |
9171 | && TREE_CODE (low_result) == INTEGER_CST) | |
9172 | { | |
9173 | /* Make a 3-bit mask with the high-order bit being the | |
9174 | value for `>', the next for '=', and the low for '<'. */ | |
9175 | switch ((integer_onep (high_result) * 4) | |
9176 | + (integer_onep (equal_result) * 2) | |
9177 | + integer_onep (low_result)) | |
9178 | { | |
9179 | case 0: | |
9180 | /* Always false. */ | |
389dd41b | 9181 | return omit_one_operand_loc (loc, type, integer_zero_node, arg0); |
6a451e87 | 9182 | case 1: |
9183 | code = LT_EXPR; | |
9184 | break; | |
9185 | case 2: | |
9186 | code = EQ_EXPR; | |
9187 | break; | |
9188 | case 3: | |
9189 | code = LE_EXPR; | |
9190 | break; | |
9191 | case 4: | |
9192 | code = GT_EXPR; | |
9193 | break; | |
9194 | case 5: | |
9195 | code = NE_EXPR; | |
9196 | break; | |
9197 | case 6: | |
9198 | code = GE_EXPR; | |
9199 | break; | |
9200 | case 7: | |
9201 | /* Always true. */ | |
389dd41b | 9202 | return omit_one_operand_loc (loc, type, integer_one_node, arg0); |
6a451e87 | 9203 | } |
9204 | ||
9205 | if (save_p) | |
389dd41b | 9206 | { |
9207 | tem = save_expr (build2 (code, type, cval1, cval2)); | |
9208 | SET_EXPR_LOCATION (tem, loc); | |
9209 | return tem; | |
9210 | } | |
9211 | return fold_build2_loc (loc, code, type, cval1, cval2); | |
6a451e87 | 9212 | } |
9213 | } | |
9214 | } | |
9215 | ||
6a451e87 | 9216 | /* We can fold X/C1 op C2 where C1 and C2 are integer constants |
9217 | into a single range test. */ | |
9218 | if ((TREE_CODE (arg0) == TRUNC_DIV_EXPR | |
9219 | || TREE_CODE (arg0) == EXACT_DIV_EXPR) | |
9220 | && TREE_CODE (arg1) == INTEGER_CST | |
9221 | && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST | |
9222 | && !integer_zerop (TREE_OPERAND (arg0, 1)) | |
9223 | && !TREE_OVERFLOW (TREE_OPERAND (arg0, 1)) | |
9224 | && !TREE_OVERFLOW (arg1)) | |
9225 | { | |
389dd41b | 9226 | tem = fold_div_compare (loc, code, type, arg0, arg1); |
6a451e87 | 9227 | if (tem != NULL_TREE) |
9228 | return tem; | |
9229 | } | |
9230 | ||
746443a2 | 9231 | /* Fold ~X op ~Y as Y op X. */ |
9232 | if (TREE_CODE (arg0) == BIT_NOT_EXPR | |
9233 | && TREE_CODE (arg1) == BIT_NOT_EXPR) | |
626b33bb | 9234 | { |
9235 | tree cmp_type = TREE_TYPE (TREE_OPERAND (arg0, 0)); | |
389dd41b | 9236 | return fold_build2_loc (loc, code, type, |
9237 | fold_convert_loc (loc, cmp_type, | |
9238 | TREE_OPERAND (arg1, 0)), | |
626b33bb | 9239 | TREE_OPERAND (arg0, 0)); |
9240 | } | |
746443a2 | 9241 | |
9242 | /* Fold ~X op C as X op' ~C, where op' is the swapped comparison. */ | |
9243 | if (TREE_CODE (arg0) == BIT_NOT_EXPR | |
9244 | && TREE_CODE (arg1) == INTEGER_CST) | |
626b33bb | 9245 | { |
9246 | tree cmp_type = TREE_TYPE (TREE_OPERAND (arg0, 0)); | |
389dd41b | 9247 | return fold_build2_loc (loc, swap_tree_comparison (code), type, |
626b33bb | 9248 | TREE_OPERAND (arg0, 0), |
389dd41b | 9249 | fold_build1_loc (loc, BIT_NOT_EXPR, cmp_type, |
9250 | fold_convert_loc (loc, cmp_type, arg1))); | |
626b33bb | 9251 | } |
746443a2 | 9252 | |
6a451e87 | 9253 | return NULL_TREE; |
9254 | } | |
9255 | ||
5f4f3617 | 9256 | |
9257 | /* Subroutine of fold_binary. Optimize complex multiplications of the | |
9258 | form z * conj(z), as pow(realpart(z),2) + pow(imagpart(z),2). The | |
9259 | argument EXPR represents the expression "z" of type TYPE. */ | |
9260 | ||
9261 | static tree | |
389dd41b | 9262 | fold_mult_zconjz (location_t loc, tree type, tree expr) |
5f4f3617 | 9263 | { |
9264 | tree itype = TREE_TYPE (type); | |
9265 | tree rpart, ipart, tem; | |
9266 | ||
9267 | if (TREE_CODE (expr) == COMPLEX_EXPR) | |
9268 | { | |
9269 | rpart = TREE_OPERAND (expr, 0); | |
9270 | ipart = TREE_OPERAND (expr, 1); | |
9271 | } | |
9272 | else if (TREE_CODE (expr) == COMPLEX_CST) | |
9273 | { | |
9274 | rpart = TREE_REALPART (expr); | |
9275 | ipart = TREE_IMAGPART (expr); | |
9276 | } | |
9277 | else | |
9278 | { | |
9279 | expr = save_expr (expr); | |
389dd41b | 9280 | rpart = fold_build1_loc (loc, REALPART_EXPR, itype, expr); |
9281 | ipart = fold_build1_loc (loc, IMAGPART_EXPR, itype, expr); | |
5f4f3617 | 9282 | } |
9283 | ||
9284 | rpart = save_expr (rpart); | |
9285 | ipart = save_expr (ipart); | |
389dd41b | 9286 | tem = fold_build2_loc (loc, PLUS_EXPR, itype, |
9287 | fold_build2_loc (loc, MULT_EXPR, itype, rpart, rpart), | |
9288 | fold_build2_loc (loc, MULT_EXPR, itype, ipart, ipart)); | |
9289 | return fold_build2_loc (loc, COMPLEX_EXPR, type, tem, | |
9290 | fold_convert_loc (loc, itype, integer_zero_node)); | |
5f4f3617 | 9291 | } |
9292 | ||
9293 | ||
4486d2b7 | 9294 | /* Subroutine of fold_binary. If P is the value of EXPR, computes |
9295 | power-of-two M and (arbitrary) N such that M divides (P-N). This condition | |
9296 | guarantees that P and N have the same least significant log2(M) bits. | |
9297 | N is not otherwise constrained. In particular, N is not normalized to | |
9298 | 0 <= N < M as is common. In general, the precise value of P is unknown. | |
9299 | M is chosen as large as possible such that constant N can be determined. | |
9300 | ||
1c4607fd | 9301 | Returns M and sets *RESIDUE to N. |
9302 | ||
9303 | If ALLOW_FUNC_ALIGN is true, do take functions' DECL_ALIGN_UNIT into | |
9304 | account. This is not always possible due to PR 35705. | |
9305 | */ | |
4486d2b7 | 9306 | |
9307 | static unsigned HOST_WIDE_INT | |
1c4607fd | 9308 | get_pointer_modulus_and_residue (tree expr, unsigned HOST_WIDE_INT *residue, |
9309 | bool allow_func_align) | |
4486d2b7 | 9310 | { |
9311 | enum tree_code code; | |
9312 | ||
9313 | *residue = 0; | |
9314 | ||
9315 | code = TREE_CODE (expr); | |
9316 | if (code == ADDR_EXPR) | |
9317 | { | |
9318 | expr = TREE_OPERAND (expr, 0); | |
9319 | if (handled_component_p (expr)) | |
9320 | { | |
9321 | HOST_WIDE_INT bitsize, bitpos; | |
9322 | tree offset; | |
9323 | enum machine_mode mode; | |
9324 | int unsignedp, volatilep; | |
9325 | ||
9326 | expr = get_inner_reference (expr, &bitsize, &bitpos, &offset, | |
9327 | &mode, &unsignedp, &volatilep, false); | |
9328 | *residue = bitpos / BITS_PER_UNIT; | |
9329 | if (offset) | |
9330 | { | |
9331 | if (TREE_CODE (offset) == INTEGER_CST) | |
9332 | *residue += TREE_INT_CST_LOW (offset); | |
9333 | else | |
9334 | /* We don't handle more complicated offset expressions. */ | |
9335 | return 1; | |
9336 | } | |
9337 | } | |
9338 | ||
1c4607fd | 9339 | if (DECL_P (expr) |
9340 | && (allow_func_align || TREE_CODE (expr) != FUNCTION_DECL)) | |
4486d2b7 | 9341 | return DECL_ALIGN_UNIT (expr); |
9342 | } | |
9343 | else if (code == POINTER_PLUS_EXPR) | |
9344 | { | |
9345 | tree op0, op1; | |
9346 | unsigned HOST_WIDE_INT modulus; | |
9347 | enum tree_code inner_code; | |
48e1416a | 9348 | |
4486d2b7 | 9349 | op0 = TREE_OPERAND (expr, 0); |
9350 | STRIP_NOPS (op0); | |
1c4607fd | 9351 | modulus = get_pointer_modulus_and_residue (op0, residue, |
9352 | allow_func_align); | |
4486d2b7 | 9353 | |
9354 | op1 = TREE_OPERAND (expr, 1); | |
9355 | STRIP_NOPS (op1); | |
9356 | inner_code = TREE_CODE (op1); | |
9357 | if (inner_code == INTEGER_CST) | |
9358 | { | |
9359 | *residue += TREE_INT_CST_LOW (op1); | |
9360 | return modulus; | |
9361 | } | |
9362 | else if (inner_code == MULT_EXPR) | |
9363 | { | |
9364 | op1 = TREE_OPERAND (op1, 1); | |
9365 | if (TREE_CODE (op1) == INTEGER_CST) | |
9366 | { | |
9367 | unsigned HOST_WIDE_INT align; | |
48e1416a | 9368 | |
4486d2b7 | 9369 | /* Compute the greatest power-of-2 divisor of op1. */ |
9370 | align = TREE_INT_CST_LOW (op1); | |
9371 | align &= -align; | |
9372 | ||
9373 | /* If align is non-zero and less than *modulus, replace | |
9374 | *modulus with align., If align is 0, then either op1 is 0 | |
9375 | or the greatest power-of-2 divisor of op1 doesn't fit in an | |
9376 | unsigned HOST_WIDE_INT. In either case, no additional | |
9377 | constraint is imposed. */ | |
9378 | if (align) | |
9379 | modulus = MIN (modulus, align); | |
9380 | ||
9381 | return modulus; | |
9382 | } | |
9383 | } | |
9384 | } | |
9385 | ||
9386 | /* If we get here, we were unable to determine anything useful about the | |
9387 | expression. */ | |
9388 | return 1; | |
9389 | } | |
9390 | ||
9391 | ||
0d3711e2 | 9392 | /* Fold a binary expression of code CODE and type TYPE with operands |
389dd41b | 9393 | OP0 and OP1. LOC is the location of the resulting expression. |
9394 | Return the folded expression if folding is successful. Otherwise, | |
9395 | return NULL_TREE. */ | |
fef10b60 | 9396 | |
d3858e14 | 9397 | tree |
389dd41b | 9398 | fold_binary_loc (location_t loc, |
9399 | enum tree_code code, tree type, tree op0, tree op1) | |
fef10b60 | 9400 | { |
fef10b60 | 9401 | enum tree_code_class kind = TREE_CODE_CLASS (code); |
6a451e87 | 9402 | tree arg0, arg1, tem; |
9403 | tree t1 = NULL_TREE; | |
add6ee5e | 9404 | bool strict_overflow_p; |
fef10b60 | 9405 | |
75a70cf9 | 9406 | gcc_assert (IS_EXPR_CODE_CLASS (kind) |
32cef1cc | 9407 | && TREE_CODE_LENGTH (code) == 2 |
9408 | && op0 != NULL_TREE | |
9409 | && op1 != NULL_TREE); | |
fef10b60 | 9410 | |
0052b98e | 9411 | arg0 = op0; |
9412 | arg1 = op1; | |
2431fed3 | 9413 | |
32cef1cc | 9414 | /* Strip any conversions that don't change the mode. This is |
9415 | safe for every expression, except for a comparison expression | |
9416 | because its signedness is derived from its operands. So, in | |
9417 | the latter case, only strip conversions that don't change the | |
7db628eb | 9418 | signedness. MIN_EXPR/MAX_EXPR also need signedness of arguments |
9419 | preserved. | |
fef10b60 | 9420 | |
32cef1cc | 9421 | Note that this is done as an internal manipulation within the |
9422 | constant folder, in order to find the simplest representation | |
9423 | of the arguments so that their form can be studied. In any | |
9424 | cases, the appropriate type conversions should be put back in | |
9425 | the tree that will get out of the constant folder. */ | |
fef10b60 | 9426 | |
7db628eb | 9427 | if (kind == tcc_comparison || code == MIN_EXPR || code == MAX_EXPR) |
32cef1cc | 9428 | { |
9429 | STRIP_SIGN_NOPS (arg0); | |
9430 | STRIP_SIGN_NOPS (arg1); | |
2431fed3 | 9431 | } |
32cef1cc | 9432 | else |
2431fed3 | 9433 | { |
32cef1cc | 9434 | STRIP_NOPS (arg0); |
9435 | STRIP_NOPS (arg1); | |
9436 | } | |
fef10b60 | 9437 | |
32cef1cc | 9438 | /* Note that TREE_CONSTANT isn't enough: static var addresses are |
9439 | constant but we can't do arithmetic on them. */ | |
9440 | if ((TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST) | |
9441 | || (TREE_CODE (arg0) == REAL_CST && TREE_CODE (arg1) == REAL_CST) | |
06f0b99c | 9442 | || (TREE_CODE (arg0) == FIXED_CST && TREE_CODE (arg1) == FIXED_CST) |
9443 | || (TREE_CODE (arg0) == FIXED_CST && TREE_CODE (arg1) == INTEGER_CST) | |
32cef1cc | 9444 | || (TREE_CODE (arg0) == COMPLEX_CST && TREE_CODE (arg1) == COMPLEX_CST) |
9445 | || (TREE_CODE (arg0) == VECTOR_CST && TREE_CODE (arg1) == VECTOR_CST)) | |
9446 | { | |
9447 | if (kind == tcc_binary) | |
06f0b99c | 9448 | { |
9449 | /* Make sure type and arg0 have the same saturating flag. */ | |
9450 | gcc_assert (TYPE_SATURATING (type) | |
9451 | == TYPE_SATURATING (TREE_TYPE (arg0))); | |
9452 | tem = const_binop (code, arg0, arg1, 0); | |
9453 | } | |
32cef1cc | 9454 | else if (kind == tcc_comparison) |
9455 | tem = fold_relational_const (code, type, arg0, arg1); | |
2431fed3 | 9456 | else |
32cef1cc | 9457 | tem = NULL_TREE; |
2431fed3 | 9458 | |
32cef1cc | 9459 | if (tem != NULL_TREE) |
9460 | { | |
9461 | if (TREE_TYPE (tem) != type) | |
389dd41b | 9462 | tem = fold_convert_loc (loc, type, tem); |
32cef1cc | 9463 | return tem; |
9464 | } | |
fef10b60 | 9465 | } |
9466 | ||
9467 | /* If this is a commutative operation, and ARG0 is a constant, move it | |
9468 | to ARG1 to reduce the number of tests below. */ | |
9469 | if (commutative_tree_code (code) | |
9470 | && tree_swap_operands_p (arg0, arg1, true)) | |
389dd41b | 9471 | return fold_build2_loc (loc, code, type, op1, op0); |
fef10b60 | 9472 | |
32cef1cc | 9473 | /* ARG0 is the first operand of EXPR, and ARG1 is the second operand. |
fef10b60 | 9474 | |
9475 | First check for cases where an arithmetic operation is applied to a | |
9476 | compound, conditional, or comparison operation. Push the arithmetic | |
9477 | operation inside the compound or conditional to see if any folding | |
9478 | can then be done. Convert comparison to conditional for this purpose. | |
9479 | The also optimizes non-constant cases that used to be done in | |
9480 | expand_expr. | |
9481 | ||
9482 | Before we do that, see if this is a BIT_AND_EXPR or a BIT_IOR_EXPR, | |
9483 | one of the operands is a comparison and the other is a comparison, a | |
9484 | BIT_AND_EXPR with the constant 1, or a truth value. In that case, the | |
9485 | code below would make the expression more complex. Change it to a | |
9486 | TRUTH_{AND,OR}_EXPR. Likewise, convert a similar NE_EXPR to | |
9487 | TRUTH_XOR_EXPR and an EQ_EXPR to the inversion of a TRUTH_XOR_EXPR. */ | |
9488 | ||
9489 | if ((code == BIT_AND_EXPR || code == BIT_IOR_EXPR | |
9490 | || code == EQ_EXPR || code == NE_EXPR) | |
9491 | && ((truth_value_p (TREE_CODE (arg0)) | |
9492 | && (truth_value_p (TREE_CODE (arg1)) | |
9493 | || (TREE_CODE (arg1) == BIT_AND_EXPR | |
9494 | && integer_onep (TREE_OPERAND (arg1, 1))))) | |
9495 | || (truth_value_p (TREE_CODE (arg1)) | |
9496 | && (truth_value_p (TREE_CODE (arg0)) | |
9497 | || (TREE_CODE (arg0) == BIT_AND_EXPR | |
9498 | && integer_onep (TREE_OPERAND (arg0, 1))))))) | |
9499 | { | |
389dd41b | 9500 | tem = fold_build2_loc (loc, code == BIT_AND_EXPR ? TRUTH_AND_EXPR |
7ab7fd4f | 9501 | : code == BIT_IOR_EXPR ? TRUTH_OR_EXPR |
9502 | : TRUTH_XOR_EXPR, | |
9503 | boolean_type_node, | |
389dd41b | 9504 | fold_convert_loc (loc, boolean_type_node, arg0), |
9505 | fold_convert_loc (loc, boolean_type_node, arg1)); | |
fef10b60 | 9506 | |
9507 | if (code == EQ_EXPR) | |
389dd41b | 9508 | tem = invert_truthvalue_loc (loc, tem); |
fef10b60 | 9509 | |
389dd41b | 9510 | return fold_convert_loc (loc, type, tem); |
fef10b60 | 9511 | } |
9512 | ||
ea43e860 | 9513 | if (TREE_CODE_CLASS (code) == tcc_binary |
9514 | || TREE_CODE_CLASS (code) == tcc_comparison) | |
fef10b60 | 9515 | { |
9516 | if (TREE_CODE (arg0) == COMPOUND_EXPR) | |
389dd41b | 9517 | { |
9518 | tem = fold_build2_loc (loc, code, type, | |
9519 | fold_convert_loc (loc, TREE_TYPE (op0), | |
9520 | TREE_OPERAND (arg0, 1)), op1); | |
389dd41b | 9521 | tem = build2 (COMPOUND_EXPR, type, TREE_OPERAND (arg0, 0), tem); |
9522 | goto fold_binary_exit; | |
9523 | } | |
fef10b60 | 9524 | if (TREE_CODE (arg1) == COMPOUND_EXPR |
9525 | && reorder_operands_p (arg0, TREE_OPERAND (arg1, 0))) | |
389dd41b | 9526 | { |
9527 | tem = fold_build2_loc (loc, code, type, op0, | |
9528 | fold_convert_loc (loc, TREE_TYPE (op1), | |
9529 | TREE_OPERAND (arg1, 1))); | |
389dd41b | 9530 | tem = build2 (COMPOUND_EXPR, type, TREE_OPERAND (arg1, 0), tem); |
9531 | goto fold_binary_exit; | |
9532 | } | |
fef10b60 | 9533 | |
9534 | if (TREE_CODE (arg0) == COND_EXPR || COMPARISON_CLASS_P (arg0)) | |
9535 | { | |
389dd41b | 9536 | tem = fold_binary_op_with_conditional_arg (loc, code, type, op0, op1, |
48e1416a | 9537 | arg0, arg1, |
fef10b60 | 9538 | /*cond_first_p=*/1); |
9539 | if (tem != NULL_TREE) | |
9540 | return tem; | |
9541 | } | |
9542 | ||
9543 | if (TREE_CODE (arg1) == COND_EXPR || COMPARISON_CLASS_P (arg1)) | |
9544 | { | |
389dd41b | 9545 | tem = fold_binary_op_with_conditional_arg (loc, code, type, op0, op1, |
48e1416a | 9546 | arg1, arg0, |
fef10b60 | 9547 | /*cond_first_p=*/0); |
9548 | if (tem != NULL_TREE) | |
9549 | return tem; | |
9550 | } | |
9551 | } | |
9552 | ||
9553 | switch (code) | |
9554 | { | |
0de36bdb | 9555 | case POINTER_PLUS_EXPR: |
9556 | /* 0 +p index -> (type)index */ | |
9557 | if (integer_zerop (arg0)) | |
389dd41b | 9558 | return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg1)); |
0de36bdb | 9559 | |
9560 | /* PTR +p 0 -> PTR */ | |
9561 | if (integer_zerop (arg1)) | |
389dd41b | 9562 | return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0)); |
0de36bdb | 9563 | |
9564 | /* INT +p INT -> (PTR)(INT + INT). Stripping types allows for this. */ | |
9565 | if (INTEGRAL_TYPE_P (TREE_TYPE (arg1)) | |
9566 | && INTEGRAL_TYPE_P (TREE_TYPE (arg0))) | |
389dd41b | 9567 | return fold_convert_loc (loc, type, |
9568 | fold_build2_loc (loc, PLUS_EXPR, sizetype, | |
9569 | fold_convert_loc (loc, sizetype, | |
9570 | arg1), | |
9571 | fold_convert_loc (loc, sizetype, | |
9572 | arg0))); | |
0de36bdb | 9573 | |
69f111d5 | 9574 | /* index +p PTR -> PTR +p index */ |
9575 | if (POINTER_TYPE_P (TREE_TYPE (arg1)) | |
9576 | && INTEGRAL_TYPE_P (TREE_TYPE (arg0))) | |
389dd41b | 9577 | return fold_build2_loc (loc, POINTER_PLUS_EXPR, type, |
9578 | fold_convert_loc (loc, type, arg1), | |
9579 | fold_convert_loc (loc, sizetype, arg0)); | |
69f111d5 | 9580 | |
0de36bdb | 9581 | /* (PTR +p B) +p A -> PTR +p (B + A) */ |
9582 | if (TREE_CODE (arg0) == POINTER_PLUS_EXPR) | |
9583 | { | |
9584 | tree inner; | |
389dd41b | 9585 | tree arg01 = fold_convert_loc (loc, sizetype, TREE_OPERAND (arg0, 1)); |
0de36bdb | 9586 | tree arg00 = TREE_OPERAND (arg0, 0); |
389dd41b | 9587 | inner = fold_build2_loc (loc, PLUS_EXPR, sizetype, |
9588 | arg01, fold_convert_loc (loc, sizetype, arg1)); | |
9589 | return fold_convert_loc (loc, type, | |
9590 | fold_build2_loc (loc, POINTER_PLUS_EXPR, | |
9591 | TREE_TYPE (arg00), | |
9592 | arg00, inner)); | |
0de36bdb | 9593 | } |
9594 | ||
9595 | /* PTR_CST +p CST -> CST1 */ | |
9596 | if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST) | |
389dd41b | 9597 | return fold_build2_loc (loc, PLUS_EXPR, type, arg0, |
9598 | fold_convert_loc (loc, type, arg1)); | |
0de36bdb | 9599 | |
9600 | /* Try replacing &a[i1] +p c * i2 with &a[i1 + i2], if c is step | |
9601 | of the array. Loop optimizer sometimes produce this type of | |
9602 | expressions. */ | |
9603 | if (TREE_CODE (arg0) == ADDR_EXPR) | |
9604 | { | |
389dd41b | 9605 | tem = try_move_mult_to_index (loc, arg0, |
9606 | fold_convert_loc (loc, sizetype, arg1)); | |
0de36bdb | 9607 | if (tem) |
389dd41b | 9608 | return fold_convert_loc (loc, type, tem); |
0de36bdb | 9609 | } |
9610 | ||
9611 | return NULL_TREE; | |
15796f61 | 9612 | |
fef10b60 | 9613 | case PLUS_EXPR: |
9614 | /* A + (-B) -> A - B */ | |
9615 | if (TREE_CODE (arg1) == NEGATE_EXPR) | |
389dd41b | 9616 | return fold_build2_loc (loc, MINUS_EXPR, type, |
9617 | fold_convert_loc (loc, type, arg0), | |
9618 | fold_convert_loc (loc, type, | |
9619 | TREE_OPERAND (arg1, 0))); | |
fef10b60 | 9620 | /* (-A) + B -> B - A */ |
9621 | if (TREE_CODE (arg0) == NEGATE_EXPR | |
9622 | && reorder_operands_p (TREE_OPERAND (arg0, 0), arg1)) | |
389dd41b | 9623 | return fold_build2_loc (loc, MINUS_EXPR, type, |
9624 | fold_convert_loc (loc, type, arg1), | |
9625 | fold_convert_loc (loc, type, | |
9626 | TREE_OPERAND (arg0, 0))); | |
1c9af531 | 9627 | |
b30baf88 | 9628 | if (INTEGRAL_TYPE_P (type)) |
fef10b60 | 9629 | { |
b30baf88 | 9630 | /* Convert ~A + 1 to -A. */ |
9631 | if (TREE_CODE (arg0) == BIT_NOT_EXPR | |
9632 | && integer_onep (arg1)) | |
389dd41b | 9633 | return fold_build1_loc (loc, NEGATE_EXPR, type, |
9634 | fold_convert_loc (loc, type, | |
9635 | TREE_OPERAND (arg0, 0))); | |
fef10b60 | 9636 | |
0673139b | 9637 | /* ~X + X is -1. */ |
9638 | if (TREE_CODE (arg0) == BIT_NOT_EXPR | |
981eb798 | 9639 | && !TYPE_OVERFLOW_TRAPS (type)) |
0673139b | 9640 | { |
5bd7f4f4 | 9641 | tree tem = TREE_OPERAND (arg0, 0); |
9642 | ||
9643 | STRIP_NOPS (tem); | |
9644 | if (operand_equal_p (tem, arg1, 0)) | |
9645 | { | |
9646 | t1 = build_int_cst_type (type, -1); | |
389dd41b | 9647 | return omit_one_operand_loc (loc, type, t1, arg1); |
5bd7f4f4 | 9648 | } |
0673139b | 9649 | } |
9650 | ||
9651 | /* X + ~X is -1. */ | |
9652 | if (TREE_CODE (arg1) == BIT_NOT_EXPR | |
981eb798 | 9653 | && !TYPE_OVERFLOW_TRAPS (type)) |
0673139b | 9654 | { |
5bd7f4f4 | 9655 | tree tem = TREE_OPERAND (arg1, 0); |
9656 | ||
9657 | STRIP_NOPS (tem); | |
9658 | if (operand_equal_p (arg0, tem, 0)) | |
9659 | { | |
9660 | t1 = build_int_cst_type (type, -1); | |
389dd41b | 9661 | return omit_one_operand_loc (loc, type, t1, arg0); |
5bd7f4f4 | 9662 | } |
9663 | } | |
d997554f | 9664 | |
9665 | /* X + (X / CST) * -CST is X % CST. */ | |
9666 | if (TREE_CODE (arg1) == MULT_EXPR | |
9667 | && TREE_CODE (TREE_OPERAND (arg1, 0)) == TRUNC_DIV_EXPR | |
9668 | && operand_equal_p (arg0, | |
9669 | TREE_OPERAND (TREE_OPERAND (arg1, 0), 0), 0)) | |
9670 | { | |
9671 | tree cst0 = TREE_OPERAND (TREE_OPERAND (arg1, 0), 1); | |
9672 | tree cst1 = TREE_OPERAND (arg1, 1); | |
389dd41b | 9673 | tree sum = fold_binary_loc (loc, PLUS_EXPR, TREE_TYPE (cst1), |
9674 | cst1, cst0); | |
d997554f | 9675 | if (sum && integer_zerop (sum)) |
389dd41b | 9676 | return fold_convert_loc (loc, type, |
9677 | fold_build2_loc (loc, TRUNC_MOD_EXPR, | |
9678 | TREE_TYPE (arg0), arg0, | |
9679 | cst0)); | |
d997554f | 9680 | } |
b30baf88 | 9681 | } |
9682 | ||
9683 | /* Handle (A1 * C1) + (A2 * C2) with A1, A2 or C1, C2 being the | |
49d060d7 | 9684 | same or one. Make sure type is not saturating. |
9685 | fold_plusminus_mult_expr will re-associate. */ | |
b30baf88 | 9686 | if ((TREE_CODE (arg0) == MULT_EXPR |
9687 | || TREE_CODE (arg1) == MULT_EXPR) | |
06f0b99c | 9688 | && !TYPE_SATURATING (type) |
49d060d7 | 9689 | && (!FLOAT_TYPE_P (type) || flag_associative_math)) |
b30baf88 | 9690 | { |
389dd41b | 9691 | tree tem = fold_plusminus_mult_expr (loc, code, type, arg0, arg1); |
b30baf88 | 9692 | if (tem) |
9693 | return tem; | |
9694 | } | |
9695 | ||
9696 | if (! FLOAT_TYPE_P (type)) | |
9697 | { | |
9698 | if (integer_zerop (arg1)) | |
389dd41b | 9699 | return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0)); |
0673139b | 9700 | |
fef10b60 | 9701 | /* If we are adding two BIT_AND_EXPR's, both of which are and'ing |
9702 | with a constant, and the two constants have no bits in common, | |
9703 | we should treat this as a BIT_IOR_EXPR since this may produce more | |
9704 | simplifications. */ | |
9705 | if (TREE_CODE (arg0) == BIT_AND_EXPR | |
9706 | && TREE_CODE (arg1) == BIT_AND_EXPR | |
9707 | && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST | |
9708 | && TREE_CODE (TREE_OPERAND (arg1, 1)) == INTEGER_CST | |
9709 | && integer_zerop (const_binop (BIT_AND_EXPR, | |
9710 | TREE_OPERAND (arg0, 1), | |
9711 | TREE_OPERAND (arg1, 1), 0))) | |
9712 | { | |
9713 | code = BIT_IOR_EXPR; | |
9714 | goto bit_ior; | |
9715 | } | |
9716 | ||
9717 | /* Reassociate (plus (plus (mult) (foo)) (mult)) as | |
9718 | (plus (plus (mult) (mult)) (foo)) so that we can | |
9719 | take advantage of the factoring cases below. */ | |
9720 | if (((TREE_CODE (arg0) == PLUS_EXPR | |
9721 | || TREE_CODE (arg0) == MINUS_EXPR) | |
9722 | && TREE_CODE (arg1) == MULT_EXPR) | |
9723 | || ((TREE_CODE (arg1) == PLUS_EXPR | |
9724 | || TREE_CODE (arg1) == MINUS_EXPR) | |
9725 | && TREE_CODE (arg0) == MULT_EXPR)) | |
9726 | { | |
9727 | tree parg0, parg1, parg, marg; | |
9728 | enum tree_code pcode; | |
9729 | ||
9730 | if (TREE_CODE (arg1) == MULT_EXPR) | |
9731 | parg = arg0, marg = arg1; | |
9732 | else | |
9733 | parg = arg1, marg = arg0; | |
9734 | pcode = TREE_CODE (parg); | |
9735 | parg0 = TREE_OPERAND (parg, 0); | |
9736 | parg1 = TREE_OPERAND (parg, 1); | |
9737 | STRIP_NOPS (parg0); | |
9738 | STRIP_NOPS (parg1); | |
9739 | ||
9740 | if (TREE_CODE (parg0) == MULT_EXPR | |
9741 | && TREE_CODE (parg1) != MULT_EXPR) | |
389dd41b | 9742 | return fold_build2_loc (loc, pcode, type, |
9743 | fold_build2_loc (loc, PLUS_EXPR, type, | |
9744 | fold_convert_loc (loc, type, | |
9745 | parg0), | |
9746 | fold_convert_loc (loc, type, | |
9747 | marg)), | |
9748 | fold_convert_loc (loc, type, parg1)); | |
fef10b60 | 9749 | if (TREE_CODE (parg0) != MULT_EXPR |
9750 | && TREE_CODE (parg1) == MULT_EXPR) | |
389dd41b | 9751 | return |
9752 | fold_build2_loc (loc, PLUS_EXPR, type, | |
9753 | fold_convert_loc (loc, type, parg0), | |
9754 | fold_build2_loc (loc, pcode, type, | |
9755 | fold_convert_loc (loc, type, marg), | |
9756 | fold_convert_loc (loc, type, | |
9757 | parg1))); | |
fef10b60 | 9758 | } |
fef10b60 | 9759 | } |
9760 | else | |
9761 | { | |
9762 | /* See if ARG1 is zero and X + ARG1 reduces to X. */ | |
9763 | if (fold_real_zero_addition_p (TREE_TYPE (arg0), arg1, 0)) | |
389dd41b | 9764 | return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0)); |
fef10b60 | 9765 | |
9766 | /* Likewise if the operands are reversed. */ | |
9767 | if (fold_real_zero_addition_p (TREE_TYPE (arg1), arg0, 0)) | |
389dd41b | 9768 | return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg1)); |
fef10b60 | 9769 | |
9770 | /* Convert X + -C into X - C. */ | |
9771 | if (TREE_CODE (arg1) == REAL_CST | |
9772 | && REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg1))) | |
9773 | { | |
9774 | tem = fold_negate_const (arg1, type); | |
9775 | if (!TREE_OVERFLOW (arg1) || !flag_trapping_math) | |
389dd41b | 9776 | return fold_build2_loc (loc, MINUS_EXPR, type, |
9777 | fold_convert_loc (loc, type, arg0), | |
9778 | fold_convert_loc (loc, type, tem)); | |
fef10b60 | 9779 | } |
9780 | ||
ed97ac4e | 9781 | /* Fold __complex__ ( x, 0 ) + __complex__ ( 0, y ) |
9782 | to __complex__ ( x, y ). This is not the same for SNaNs or | |
1af0d139 | 9783 | if signed zeros are involved. */ |
ed97ac4e | 9784 | if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0))) |
9785 | && !HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg0))) | |
9786 | && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0))) | |
9787 | { | |
9788 | tree rtype = TREE_TYPE (TREE_TYPE (arg0)); | |
389dd41b | 9789 | tree arg0r = fold_unary_loc (loc, REALPART_EXPR, rtype, arg0); |
9790 | tree arg0i = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg0); | |
ed97ac4e | 9791 | bool arg0rz = false, arg0iz = false; |
9792 | if ((arg0r && (arg0rz = real_zerop (arg0r))) | |
9793 | || (arg0i && (arg0iz = real_zerop (arg0i)))) | |
9794 | { | |
389dd41b | 9795 | tree arg1r = fold_unary_loc (loc, REALPART_EXPR, rtype, arg1); |
9796 | tree arg1i = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg1); | |
ed97ac4e | 9797 | if (arg0rz && arg1i && real_zerop (arg1i)) |
9798 | { | |
9799 | tree rp = arg1r ? arg1r | |
9800 | : build1 (REALPART_EXPR, rtype, arg1); | |
9801 | tree ip = arg0i ? arg0i | |
9802 | : build1 (IMAGPART_EXPR, rtype, arg0); | |
389dd41b | 9803 | return fold_build2_loc (loc, COMPLEX_EXPR, type, rp, ip); |
ed97ac4e | 9804 | } |
9805 | else if (arg0iz && arg1r && real_zerop (arg1r)) | |
9806 | { | |
9807 | tree rp = arg0r ? arg0r | |
9808 | : build1 (REALPART_EXPR, rtype, arg0); | |
9809 | tree ip = arg1i ? arg1i | |
9810 | : build1 (IMAGPART_EXPR, rtype, arg1); | |
389dd41b | 9811 | return fold_build2_loc (loc, COMPLEX_EXPR, type, rp, ip); |
ed97ac4e | 9812 | } |
9813 | } | |
9814 | } | |
9815 | ||
1e5de3bd | 9816 | if (flag_unsafe_math_optimizations |
429f2f90 | 9817 | && (TREE_CODE (arg0) == RDIV_EXPR || TREE_CODE (arg0) == MULT_EXPR) |
9818 | && (TREE_CODE (arg1) == RDIV_EXPR || TREE_CODE (arg1) == MULT_EXPR) | |
389dd41b | 9819 | && (tem = distribute_real_division (loc, code, type, arg0, arg1))) |
429f2f90 | 9820 | return tem; |
9821 | ||
fef10b60 | 9822 | /* Convert x+x into x*2.0. */ |
9823 | if (operand_equal_p (arg0, arg1, 0) | |
9824 | && SCALAR_FLOAT_TYPE_P (type)) | |
389dd41b | 9825 | return fold_build2_loc (loc, MULT_EXPR, type, arg0, |
7ab7fd4f | 9826 | build_real (type, dconst2)); |
fef10b60 | 9827 | |
48e1416a | 9828 | /* Convert a + (b*c + d*e) into (a + b*c) + d*e. |
49d060d7 | 9829 | We associate floats only if the user has specified |
9830 | -fassociative-math. */ | |
9831 | if (flag_associative_math | |
fef10b60 | 9832 | && TREE_CODE (arg1) == PLUS_EXPR |
9833 | && TREE_CODE (arg0) != MULT_EXPR) | |
9834 | { | |
9835 | tree tree10 = TREE_OPERAND (arg1, 0); | |
9836 | tree tree11 = TREE_OPERAND (arg1, 1); | |
9837 | if (TREE_CODE (tree11) == MULT_EXPR | |
9838 | && TREE_CODE (tree10) == MULT_EXPR) | |
9839 | { | |
9840 | tree tree0; | |
389dd41b | 9841 | tree0 = fold_build2_loc (loc, PLUS_EXPR, type, arg0, tree10); |
9842 | return fold_build2_loc (loc, PLUS_EXPR, type, tree0, tree11); | |
fef10b60 | 9843 | } |
9844 | } | |
48e1416a | 9845 | /* Convert (b*c + d*e) + a into b*c + (d*e +a). |
49d060d7 | 9846 | We associate floats only if the user has specified |
9847 | -fassociative-math. */ | |
9848 | if (flag_associative_math | |
fef10b60 | 9849 | && TREE_CODE (arg0) == PLUS_EXPR |
9850 | && TREE_CODE (arg1) != MULT_EXPR) | |
9851 | { | |
9852 | tree tree00 = TREE_OPERAND (arg0, 0); | |
9853 | tree tree01 = TREE_OPERAND (arg0, 1); | |
9854 | if (TREE_CODE (tree01) == MULT_EXPR | |
9855 | && TREE_CODE (tree00) == MULT_EXPR) | |
9856 | { | |
9857 | tree tree0; | |
389dd41b | 9858 | tree0 = fold_build2_loc (loc, PLUS_EXPR, type, tree01, arg1); |
9859 | return fold_build2_loc (loc, PLUS_EXPR, type, tree00, tree0); | |
fef10b60 | 9860 | } |
9861 | } | |
9862 | } | |
9863 | ||
9864 | bit_rotate: | |
9865 | /* (A << C1) + (A >> C2) if A is unsigned and C1+C2 is the size of A | |
9866 | is a rotate of A by C1 bits. */ | |
9867 | /* (A << B) + (A >> (Z - B)) if A is unsigned and Z is the size of A | |
9868 | is a rotate of A by B bits. */ | |
9869 | { | |
9870 | enum tree_code code0, code1; | |
6295ca72 | 9871 | tree rtype; |
fef10b60 | 9872 | code0 = TREE_CODE (arg0); |
9873 | code1 = TREE_CODE (arg1); | |
9874 | if (((code0 == RSHIFT_EXPR && code1 == LSHIFT_EXPR) | |
9875 | || (code1 == RSHIFT_EXPR && code0 == LSHIFT_EXPR)) | |
9876 | && operand_equal_p (TREE_OPERAND (arg0, 0), | |
9877 | TREE_OPERAND (arg1, 0), 0) | |
6295ca72 | 9878 | && (rtype = TREE_TYPE (TREE_OPERAND (arg0, 0)), |
9879 | TYPE_UNSIGNED (rtype)) | |
9880 | /* Only create rotates in complete modes. Other cases are not | |
9881 | expanded properly. */ | |
9882 | && TYPE_PRECISION (rtype) == GET_MODE_PRECISION (TYPE_MODE (rtype))) | |
fef10b60 | 9883 | { |
9884 | tree tree01, tree11; | |
9885 | enum tree_code code01, code11; | |
9886 | ||
9887 | tree01 = TREE_OPERAND (arg0, 1); | |
9888 | tree11 = TREE_OPERAND (arg1, 1); | |
9889 | STRIP_NOPS (tree01); | |
9890 | STRIP_NOPS (tree11); | |
9891 | code01 = TREE_CODE (tree01); | |
9892 | code11 = TREE_CODE (tree11); | |
9893 | if (code01 == INTEGER_CST | |
9894 | && code11 == INTEGER_CST | |
9895 | && TREE_INT_CST_HIGH (tree01) == 0 | |
9896 | && TREE_INT_CST_HIGH (tree11) == 0 | |
9897 | && ((TREE_INT_CST_LOW (tree01) + TREE_INT_CST_LOW (tree11)) | |
9898 | == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg0, 0))))) | |
389dd41b | 9899 | { |
9900 | tem = build2 (LROTATE_EXPR, | |
9901 | TREE_TYPE (TREE_OPERAND (arg0, 0)), | |
9902 | TREE_OPERAND (arg0, 0), | |
9903 | code0 == LSHIFT_EXPR | |
9904 | ? tree01 : tree11); | |
9905 | SET_EXPR_LOCATION (tem, loc); | |
9906 | return fold_convert_loc (loc, type, tem); | |
9907 | } | |
fef10b60 | 9908 | else if (code11 == MINUS_EXPR) |
9909 | { | |
9910 | tree tree110, tree111; | |
9911 | tree110 = TREE_OPERAND (tree11, 0); | |
9912 | tree111 = TREE_OPERAND (tree11, 1); | |
9913 | STRIP_NOPS (tree110); | |
9914 | STRIP_NOPS (tree111); | |
9915 | if (TREE_CODE (tree110) == INTEGER_CST | |
9916 | && 0 == compare_tree_int (tree110, | |
9917 | TYPE_PRECISION | |
9918 | (TREE_TYPE (TREE_OPERAND | |
9919 | (arg0, 0)))) | |
9920 | && operand_equal_p (tree01, tree111, 0)) | |
389dd41b | 9921 | return |
9922 | fold_convert_loc (loc, type, | |
9923 | build2 ((code0 == LSHIFT_EXPR | |
9924 | ? LROTATE_EXPR | |
9925 | : RROTATE_EXPR), | |
9926 | TREE_TYPE (TREE_OPERAND (arg0, 0)), | |
9927 | TREE_OPERAND (arg0, 0), tree01)); | |
fef10b60 | 9928 | } |
9929 | else if (code01 == MINUS_EXPR) | |
9930 | { | |
9931 | tree tree010, tree011; | |
9932 | tree010 = TREE_OPERAND (tree01, 0); | |
9933 | tree011 = TREE_OPERAND (tree01, 1); | |
9934 | STRIP_NOPS (tree010); | |
9935 | STRIP_NOPS (tree011); | |
9936 | if (TREE_CODE (tree010) == INTEGER_CST | |
9937 | && 0 == compare_tree_int (tree010, | |
9938 | TYPE_PRECISION | |
9939 | (TREE_TYPE (TREE_OPERAND | |
9940 | (arg0, 0)))) | |
9941 | && operand_equal_p (tree11, tree011, 0)) | |
389dd41b | 9942 | return fold_convert_loc |
9943 | (loc, type, | |
9944 | build2 ((code0 != LSHIFT_EXPR | |
9945 | ? LROTATE_EXPR | |
9946 | : RROTATE_EXPR), | |
9947 | TREE_TYPE (TREE_OPERAND (arg0, 0)), | |
9948 | TREE_OPERAND (arg0, 0), tree11)); | |
fef10b60 | 9949 | } |
9950 | } | |
9951 | } | |
9952 | ||
9953 | associate: | |
9954 | /* In most languages, can't associate operations on floats through | |
9955 | parentheses. Rather than remember where the parentheses were, we | |
9956 | don't associate floats at all, unless the user has specified | |
49d060d7 | 9957 | -fassociative-math. |
06f0b99c | 9958 | And, we need to make sure type is not saturating. */ |
fef10b60 | 9959 | |
49d060d7 | 9960 | if ((! FLOAT_TYPE_P (type) || flag_associative_math) |
06f0b99c | 9961 | && !TYPE_SATURATING (type)) |
fef10b60 | 9962 | { |
9963 | tree var0, con0, lit0, minus_lit0; | |
9964 | tree var1, con1, lit1, minus_lit1; | |
fb700337 | 9965 | bool ok = true; |
fef10b60 | 9966 | |
9967 | /* Split both trees into variables, constants, and literals. Then | |
9968 | associate each group together, the constants with literals, | |
9969 | then the result with variables. This increases the chances of | |
9970 | literals being recombined later and of generating relocatable | |
9971 | expressions for the sum of a constant and literal. */ | |
9972 | var0 = split_tree (arg0, code, &con0, &lit0, &minus_lit0, 0); | |
9973 | var1 = split_tree (arg1, code, &con1, &lit1, &minus_lit1, | |
9974 | code == MINUS_EXPR); | |
9975 | ||
c6feb9f1 | 9976 | /* Recombine MINUS_EXPR operands by using PLUS_EXPR. */ |
9977 | if (code == MINUS_EXPR) | |
9978 | code = PLUS_EXPR; | |
9979 | ||
9980 | /* With undefined overflow we can only associate constants with one | |
9981 | variable, and constants whose association doesn't overflow. */ | |
9982 | if ((POINTER_TYPE_P (type) && POINTER_TYPE_OVERFLOW_UNDEFINED) | |
9983 | || (INTEGRAL_TYPE_P (type) && !TYPE_OVERFLOW_WRAPS (type))) | |
fb700337 | 9984 | { |
c6feb9f1 | 9985 | if (var0 && var1) |
9986 | { | |
9987 | tree tmp0 = var0; | |
9988 | tree tmp1 = var1; | |
9989 | ||
9990 | if (TREE_CODE (tmp0) == NEGATE_EXPR) | |
9991 | tmp0 = TREE_OPERAND (tmp0, 0); | |
9992 | if (TREE_CODE (tmp1) == NEGATE_EXPR) | |
9993 | tmp1 = TREE_OPERAND (tmp1, 0); | |
9994 | /* The only case we can still associate with two variables | |
9995 | is if they are the same, modulo negation. */ | |
9996 | if (!operand_equal_p (tmp0, tmp1, 0)) | |
9997 | ok = false; | |
9998 | } | |
9999 | ||
10000 | if (ok && lit0 && lit1) | |
10001 | { | |
10002 | tree tmp0 = fold_convert (type, lit0); | |
10003 | tree tmp1 = fold_convert (type, lit1); | |
10004 | ||
10005 | if (!TREE_OVERFLOW (tmp0) && !TREE_OVERFLOW (tmp1) | |
10006 | && TREE_OVERFLOW (fold_build2 (code, type, tmp0, tmp1))) | |
10007 | ok = false; | |
10008 | } | |
fb700337 | 10009 | } |
10010 | ||
fef10b60 | 10011 | /* Only do something if we found more than two objects. Otherwise, |
10012 | nothing has changed and we risk infinite recursion. */ | |
fb700337 | 10013 | if (ok |
10014 | && (2 < ((var0 != 0) + (var1 != 0) | |
10015 | + (con0 != 0) + (con1 != 0) | |
10016 | + (lit0 != 0) + (lit1 != 0) | |
10017 | + (minus_lit0 != 0) + (minus_lit1 != 0)))) | |
fef10b60 | 10018 | { |
389dd41b | 10019 | var0 = associate_trees (loc, var0, var1, code, type); |
10020 | con0 = associate_trees (loc, con0, con1, code, type); | |
10021 | lit0 = associate_trees (loc, lit0, lit1, code, type); | |
10022 | minus_lit0 = associate_trees (loc, minus_lit0, minus_lit1, code, type); | |
fef10b60 | 10023 | |
10024 | /* Preserve the MINUS_EXPR if the negative part of the literal is | |
10025 | greater than the positive part. Otherwise, the multiplicative | |
10026 | folding code (i.e extract_muldiv) may be fooled in case | |
10027 | unsigned constants are subtracted, like in the following | |
10028 | example: ((X*2 + 4) - 8U)/2. */ | |
10029 | if (minus_lit0 && lit0) | |
10030 | { | |
10031 | if (TREE_CODE (lit0) == INTEGER_CST | |
10032 | && TREE_CODE (minus_lit0) == INTEGER_CST | |
10033 | && tree_int_cst_lt (lit0, minus_lit0)) | |
10034 | { | |
389dd41b | 10035 | minus_lit0 = associate_trees (loc, minus_lit0, lit0, |
fef10b60 | 10036 | MINUS_EXPR, type); |
10037 | lit0 = 0; | |
10038 | } | |
10039 | else | |
10040 | { | |
389dd41b | 10041 | lit0 = associate_trees (loc, lit0, minus_lit0, |
fef10b60 | 10042 | MINUS_EXPR, type); |
10043 | minus_lit0 = 0; | |
10044 | } | |
10045 | } | |
10046 | if (minus_lit0) | |
10047 | { | |
10048 | if (con0 == 0) | |
389dd41b | 10049 | return |
10050 | fold_convert_loc (loc, type, | |
10051 | associate_trees (loc, var0, minus_lit0, | |
10052 | MINUS_EXPR, type)); | |
fef10b60 | 10053 | else |
10054 | { | |
389dd41b | 10055 | con0 = associate_trees (loc, con0, minus_lit0, |
fef10b60 | 10056 | MINUS_EXPR, type); |
389dd41b | 10057 | return |
10058 | fold_convert_loc (loc, type, | |
10059 | associate_trees (loc, var0, con0, | |
10060 | PLUS_EXPR, type)); | |
fef10b60 | 10061 | } |
10062 | } | |
10063 | ||
389dd41b | 10064 | con0 = associate_trees (loc, con0, lit0, code, type); |
10065 | return | |
10066 | fold_convert_loc (loc, type, associate_trees (loc, var0, con0, | |
10067 | code, type)); | |
fef10b60 | 10068 | } |
10069 | } | |
10070 | ||
e7edfbbd | 10071 | return NULL_TREE; |
fef10b60 | 10072 | |
10073 | case MINUS_EXPR: | |
0de36bdb | 10074 | /* Pointer simplifications for subtraction, simple reassociations. */ |
10075 | if (POINTER_TYPE_P (TREE_TYPE (arg1)) && POINTER_TYPE_P (TREE_TYPE (arg0))) | |
10076 | { | |
10077 | /* (PTR0 p+ A) - (PTR1 p+ B) -> (PTR0 - PTR1) + (A - B) */ | |
10078 | if (TREE_CODE (arg0) == POINTER_PLUS_EXPR | |
10079 | && TREE_CODE (arg1) == POINTER_PLUS_EXPR) | |
10080 | { | |
389dd41b | 10081 | tree arg00 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0)); |
10082 | tree arg01 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 1)); | |
10083 | tree arg10 = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 0)); | |
10084 | tree arg11 = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 1)); | |
10085 | return fold_build2_loc (loc, PLUS_EXPR, type, | |
10086 | fold_build2_loc (loc, MINUS_EXPR, type, | |
10087 | arg00, arg10), | |
10088 | fold_build2_loc (loc, MINUS_EXPR, type, | |
10089 | arg01, arg11)); | |
0de36bdb | 10090 | } |
10091 | /* (PTR0 p+ A) - PTR1 -> (PTR0 - PTR1) + A, assuming PTR0 - PTR1 simplifies. */ | |
10092 | else if (TREE_CODE (arg0) == POINTER_PLUS_EXPR) | |
10093 | { | |
389dd41b | 10094 | tree arg00 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0)); |
10095 | tree arg01 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 1)); | |
10096 | tree tmp = fold_binary_loc (loc, MINUS_EXPR, type, arg00, | |
10097 | fold_convert_loc (loc, type, arg1)); | |
0de36bdb | 10098 | if (tmp) |
389dd41b | 10099 | return fold_build2_loc (loc, PLUS_EXPR, type, tmp, arg01); |
0de36bdb | 10100 | } |
10101 | } | |
fef10b60 | 10102 | /* A - (-B) -> A + B */ |
10103 | if (TREE_CODE (arg1) == NEGATE_EXPR) | |
389dd41b | 10104 | return fold_build2_loc (loc, PLUS_EXPR, type, op0, |
10105 | fold_convert_loc (loc, type, | |
10106 | TREE_OPERAND (arg1, 0))); | |
fef10b60 | 10107 | /* (-A) - B -> (-B) - A where B is easily negated and we can swap. */ |
10108 | if (TREE_CODE (arg0) == NEGATE_EXPR | |
10109 | && (FLOAT_TYPE_P (type) | |
8a7907c1 | 10110 | || INTEGRAL_TYPE_P (type)) |
fef10b60 | 10111 | && negate_expr_p (arg1) |
10112 | && reorder_operands_p (arg0, arg1)) | |
389dd41b | 10113 | return fold_build2_loc (loc, MINUS_EXPR, type, |
10114 | fold_convert_loc (loc, type, | |
10115 | negate_expr (arg1)), | |
10116 | fold_convert_loc (loc, type, | |
10117 | TREE_OPERAND (arg0, 0))); | |
c860f88e | 10118 | /* Convert -A - 1 to ~A. */ |
10119 | if (INTEGRAL_TYPE_P (type) | |
10120 | && TREE_CODE (arg0) == NEGATE_EXPR | |
0673139b | 10121 | && integer_onep (arg1) |
981eb798 | 10122 | && !TYPE_OVERFLOW_TRAPS (type)) |
389dd41b | 10123 | return fold_build1_loc (loc, BIT_NOT_EXPR, type, |
10124 | fold_convert_loc (loc, type, | |
10125 | TREE_OPERAND (arg0, 0))); | |
c860f88e | 10126 | |
10127 | /* Convert -1 - A to ~A. */ | |
10128 | if (INTEGRAL_TYPE_P (type) | |
10129 | && integer_all_onesp (arg0)) | |
389dd41b | 10130 | return fold_build1_loc (loc, BIT_NOT_EXPR, type, op1); |
fef10b60 | 10131 | |
d997554f | 10132 | |
10133 | /* X - (X / CST) * CST is X % CST. */ | |
10134 | if (INTEGRAL_TYPE_P (type) | |
10135 | && TREE_CODE (arg1) == MULT_EXPR | |
10136 | && TREE_CODE (TREE_OPERAND (arg1, 0)) == TRUNC_DIV_EXPR | |
10137 | && operand_equal_p (arg0, | |
10138 | TREE_OPERAND (TREE_OPERAND (arg1, 0), 0), 0) | |
10139 | && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg1, 0), 1), | |
10140 | TREE_OPERAND (arg1, 1), 0)) | |
389dd41b | 10141 | return |
10142 | fold_convert_loc (loc, type, | |
10143 | fold_build2_loc (loc, TRUNC_MOD_EXPR, TREE_TYPE (arg0), | |
10144 | arg0, TREE_OPERAND (arg1, 1))); | |
d997554f | 10145 | |
fef10b60 | 10146 | if (! FLOAT_TYPE_P (type)) |
10147 | { | |
32cef1cc | 10148 | if (integer_zerop (arg0)) |
389dd41b | 10149 | return negate_expr (fold_convert_loc (loc, type, arg1)); |
fef10b60 | 10150 | if (integer_zerop (arg1)) |
389dd41b | 10151 | return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0)); |
fef10b60 | 10152 | |
10153 | /* Fold A - (A & B) into ~B & A. */ | |
10154 | if (!TREE_SIDE_EFFECTS (arg0) | |
10155 | && TREE_CODE (arg1) == BIT_AND_EXPR) | |
10156 | { | |
10157 | if (operand_equal_p (arg0, TREE_OPERAND (arg1, 1), 0)) | |
f6b25e1c | 10158 | { |
389dd41b | 10159 | tree arg10 = fold_convert_loc (loc, type, |
10160 | TREE_OPERAND (arg1, 0)); | |
10161 | return fold_build2_loc (loc, BIT_AND_EXPR, type, | |
10162 | fold_build1_loc (loc, BIT_NOT_EXPR, | |
10163 | type, arg10), | |
10164 | fold_convert_loc (loc, type, arg0)); | |
f6b25e1c | 10165 | } |
fef10b60 | 10166 | if (operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0)) |
f6b25e1c | 10167 | { |
389dd41b | 10168 | tree arg11 = fold_convert_loc (loc, |
10169 | type, TREE_OPERAND (arg1, 1)); | |
10170 | return fold_build2_loc (loc, BIT_AND_EXPR, type, | |
10171 | fold_build1_loc (loc, BIT_NOT_EXPR, | |
10172 | type, arg11), | |
10173 | fold_convert_loc (loc, type, arg0)); | |
f6b25e1c | 10174 | } |
fef10b60 | 10175 | } |
10176 | ||
10177 | /* Fold (A & ~B) - (A & B) into (A ^ B) - B, where B is | |
10178 | any power of 2 minus 1. */ | |
10179 | if (TREE_CODE (arg0) == BIT_AND_EXPR | |
10180 | && TREE_CODE (arg1) == BIT_AND_EXPR | |
10181 | && operand_equal_p (TREE_OPERAND (arg0, 0), | |
10182 | TREE_OPERAND (arg1, 0), 0)) | |
10183 | { | |
10184 | tree mask0 = TREE_OPERAND (arg0, 1); | |
10185 | tree mask1 = TREE_OPERAND (arg1, 1); | |
389dd41b | 10186 | tree tem = fold_build1_loc (loc, BIT_NOT_EXPR, type, mask0); |
fef10b60 | 10187 | |
10188 | if (operand_equal_p (tem, mask1, 0)) | |
10189 | { | |
389dd41b | 10190 | tem = fold_build2_loc (loc, BIT_XOR_EXPR, type, |
7ab7fd4f | 10191 | TREE_OPERAND (arg0, 0), mask1); |
389dd41b | 10192 | return fold_build2_loc (loc, MINUS_EXPR, type, tem, mask1); |
fef10b60 | 10193 | } |
10194 | } | |
10195 | } | |
10196 | ||
10197 | /* See if ARG1 is zero and X - ARG1 reduces to X. */ | |
10198 | else if (fold_real_zero_addition_p (TREE_TYPE (arg0), arg1, 1)) | |
389dd41b | 10199 | return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0)); |
fef10b60 | 10200 | |
10201 | /* (ARG0 - ARG1) is the same as (-ARG1 + ARG0). So check whether | |
10202 | ARG0 is zero and X + ARG0 reduces to X, since that would mean | |
10203 | (-ARG1 + ARG0) reduces to -ARG1. */ | |
32cef1cc | 10204 | else if (fold_real_zero_addition_p (TREE_TYPE (arg1), arg0, 0)) |
389dd41b | 10205 | return negate_expr (fold_convert_loc (loc, type, arg1)); |
fef10b60 | 10206 | |
1af0d139 | 10207 | /* Fold __complex__ ( x, 0 ) - __complex__ ( 0, y ) to |
10208 | __complex__ ( x, -y ). This is not the same for SNaNs or if | |
10209 | signed zeros are involved. */ | |
10210 | if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0))) | |
10211 | && !HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg0))) | |
10212 | && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0))) | |
10213 | { | |
10214 | tree rtype = TREE_TYPE (TREE_TYPE (arg0)); | |
389dd41b | 10215 | tree arg0r = fold_unary_loc (loc, REALPART_EXPR, rtype, arg0); |
10216 | tree arg0i = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg0); | |
1af0d139 | 10217 | bool arg0rz = false, arg0iz = false; |
10218 | if ((arg0r && (arg0rz = real_zerop (arg0r))) | |
10219 | || (arg0i && (arg0iz = real_zerop (arg0i)))) | |
10220 | { | |
389dd41b | 10221 | tree arg1r = fold_unary_loc (loc, REALPART_EXPR, rtype, arg1); |
10222 | tree arg1i = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg1); | |
1af0d139 | 10223 | if (arg0rz && arg1i && real_zerop (arg1i)) |
10224 | { | |
389dd41b | 10225 | tree rp = fold_build1_loc (loc, NEGATE_EXPR, rtype, |
1af0d139 | 10226 | arg1r ? arg1r |
10227 | : build1 (REALPART_EXPR, rtype, arg1)); | |
10228 | tree ip = arg0i ? arg0i | |
10229 | : build1 (IMAGPART_EXPR, rtype, arg0); | |
389dd41b | 10230 | return fold_build2_loc (loc, COMPLEX_EXPR, type, rp, ip); |
1af0d139 | 10231 | } |
10232 | else if (arg0iz && arg1r && real_zerop (arg1r)) | |
10233 | { | |
10234 | tree rp = arg0r ? arg0r | |
10235 | : build1 (REALPART_EXPR, rtype, arg0); | |
389dd41b | 10236 | tree ip = fold_build1_loc (loc, NEGATE_EXPR, rtype, |
1af0d139 | 10237 | arg1i ? arg1i |
10238 | : build1 (IMAGPART_EXPR, rtype, arg1)); | |
389dd41b | 10239 | return fold_build2_loc (loc, COMPLEX_EXPR, type, rp, ip); |
1af0d139 | 10240 | } |
10241 | } | |
10242 | } | |
10243 | ||
fef10b60 | 10244 | /* Fold &x - &x. This can happen from &x.foo - &x. |
10245 | This is unsafe for certain floats even in non-IEEE formats. | |
10246 | In IEEE, it is unsafe because it does wrong for NaNs. | |
10247 | Also note that operand_equal_p is always false if an operand | |
10248 | is volatile. */ | |
10249 | ||
19da70e0 | 10250 | if ((!FLOAT_TYPE_P (type) || !HONOR_NANS (TYPE_MODE (type))) |
fef10b60 | 10251 | && operand_equal_p (arg0, arg1, 0)) |
389dd41b | 10252 | return fold_convert_loc (loc, type, integer_zero_node); |
fef10b60 | 10253 | |
10254 | /* A - B -> A + (-B) if B is easily negatable. */ | |
32cef1cc | 10255 | if (negate_expr_p (arg1) |
fef10b60 | 10256 | && ((FLOAT_TYPE_P (type) |
10257 | /* Avoid this transformation if B is a positive REAL_CST. */ | |
10258 | && (TREE_CODE (arg1) != REAL_CST | |
10259 | || REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg1)))) | |
8a7907c1 | 10260 | || INTEGRAL_TYPE_P (type))) |
389dd41b | 10261 | return fold_build2_loc (loc, PLUS_EXPR, type, |
10262 | fold_convert_loc (loc, type, arg0), | |
10263 | fold_convert_loc (loc, type, | |
10264 | negate_expr (arg1))); | |
fef10b60 | 10265 | |
10266 | /* Try folding difference of addresses. */ | |
10267 | { | |
10268 | HOST_WIDE_INT diff; | |
10269 | ||
10270 | if ((TREE_CODE (arg0) == ADDR_EXPR | |
10271 | || TREE_CODE (arg1) == ADDR_EXPR) | |
10272 | && ptr_difference_const (arg0, arg1, &diff)) | |
10273 | return build_int_cst_type (type, diff); | |
10274 | } | |
d4e7fe89 | 10275 | |
10276 | /* Fold &a[i] - &a[j] to i-j. */ | |
10277 | if (TREE_CODE (arg0) == ADDR_EXPR | |
10278 | && TREE_CODE (TREE_OPERAND (arg0, 0)) == ARRAY_REF | |
10279 | && TREE_CODE (arg1) == ADDR_EXPR | |
10280 | && TREE_CODE (TREE_OPERAND (arg1, 0)) == ARRAY_REF) | |
10281 | { | |
10282 | tree aref0 = TREE_OPERAND (arg0, 0); | |
10283 | tree aref1 = TREE_OPERAND (arg1, 0); | |
10284 | if (operand_equal_p (TREE_OPERAND (aref0, 0), | |
10285 | TREE_OPERAND (aref1, 0), 0)) | |
10286 | { | |
389dd41b | 10287 | tree op0 = fold_convert_loc (loc, type, TREE_OPERAND (aref0, 1)); |
10288 | tree op1 = fold_convert_loc (loc, type, TREE_OPERAND (aref1, 1)); | |
d4e7fe89 | 10289 | tree esz = array_ref_element_size (aref0); |
10290 | tree diff = build2 (MINUS_EXPR, type, op0, op1); | |
389dd41b | 10291 | return fold_build2_loc (loc, MULT_EXPR, type, diff, |
10292 | fold_convert_loc (loc, type, esz)); | |
48e1416a | 10293 | |
d4e7fe89 | 10294 | } |
10295 | } | |
10296 | ||
1e5de3bd | 10297 | if (FLOAT_TYPE_P (type) |
10298 | && flag_unsafe_math_optimizations | |
429f2f90 | 10299 | && (TREE_CODE (arg0) == RDIV_EXPR || TREE_CODE (arg0) == MULT_EXPR) |
10300 | && (TREE_CODE (arg1) == RDIV_EXPR || TREE_CODE (arg1) == MULT_EXPR) | |
389dd41b | 10301 | && (tem = distribute_real_division (loc, code, type, arg0, arg1))) |
429f2f90 | 10302 | return tem; |
10303 | ||
1c9af531 | 10304 | /* Handle (A1 * C1) - (A2 * C2) with A1, A2 or C1, C2 being the |
49d060d7 | 10305 | same or one. Make sure type is not saturating. |
10306 | fold_plusminus_mult_expr will re-associate. */ | |
1c9af531 | 10307 | if ((TREE_CODE (arg0) == MULT_EXPR |
10308 | || TREE_CODE (arg1) == MULT_EXPR) | |
06f0b99c | 10309 | && !TYPE_SATURATING (type) |
49d060d7 | 10310 | && (!FLOAT_TYPE_P (type) || flag_associative_math)) |
1c9af531 | 10311 | { |
389dd41b | 10312 | tree tem = fold_plusminus_mult_expr (loc, code, type, arg0, arg1); |
1c9af531 | 10313 | if (tem) |
10314 | return tem; | |
fef10b60 | 10315 | } |
10316 | ||
10317 | goto associate; | |
10318 | ||
10319 | case MULT_EXPR: | |
10320 | /* (-A) * (-B) -> A * B */ | |
10321 | if (TREE_CODE (arg0) == NEGATE_EXPR && negate_expr_p (arg1)) | |
389dd41b | 10322 | return fold_build2_loc (loc, MULT_EXPR, type, |
10323 | fold_convert_loc (loc, type, | |
10324 | TREE_OPERAND (arg0, 0)), | |
10325 | fold_convert_loc (loc, type, | |
10326 | negate_expr (arg1))); | |
fef10b60 | 10327 | if (TREE_CODE (arg1) == NEGATE_EXPR && negate_expr_p (arg0)) |
389dd41b | 10328 | return fold_build2_loc (loc, MULT_EXPR, type, |
10329 | fold_convert_loc (loc, type, | |
10330 | negate_expr (arg0)), | |
10331 | fold_convert_loc (loc, type, | |
10332 | TREE_OPERAND (arg1, 0))); | |
fef10b60 | 10333 | |
fef10b60 | 10334 | if (! FLOAT_TYPE_P (type)) |
10335 | { | |
10336 | if (integer_zerop (arg1)) | |
389dd41b | 10337 | return omit_one_operand_loc (loc, type, arg1, arg0); |
fef10b60 | 10338 | if (integer_onep (arg1)) |
389dd41b | 10339 | return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0)); |
a69a910a | 10340 | /* Transform x * -1 into -x. Make sure to do the negation |
10341 | on the original operand with conversions not stripped | |
10342 | because we can only strip non-sign-changing conversions. */ | |
48854785 | 10343 | if (integer_all_onesp (arg1)) |
389dd41b | 10344 | return fold_convert_loc (loc, type, negate_expr (op0)); |
8a7907c1 | 10345 | /* Transform x * -C into -x * C if x is easily negatable. */ |
10346 | if (TREE_CODE (arg1) == INTEGER_CST | |
10347 | && tree_int_cst_sgn (arg1) == -1 | |
10348 | && negate_expr_p (arg0) | |
10349 | && (tem = negate_expr (arg1)) != arg1 | |
10350 | && !TREE_OVERFLOW (tem)) | |
389dd41b | 10351 | return fold_build2_loc (loc, MULT_EXPR, type, |
10352 | fold_convert_loc (loc, type, | |
10353 | negate_expr (arg0)), | |
10354 | tem); | |
fef10b60 | 10355 | |
10356 | /* (a * (1 << b)) is (a << b) */ | |
10357 | if (TREE_CODE (arg1) == LSHIFT_EXPR | |
10358 | && integer_onep (TREE_OPERAND (arg1, 0))) | |
389dd41b | 10359 | return fold_build2_loc (loc, LSHIFT_EXPR, type, op0, |
7ab7fd4f | 10360 | TREE_OPERAND (arg1, 1)); |
fef10b60 | 10361 | if (TREE_CODE (arg0) == LSHIFT_EXPR |
10362 | && integer_onep (TREE_OPERAND (arg0, 0))) | |
389dd41b | 10363 | return fold_build2_loc (loc, LSHIFT_EXPR, type, op1, |
7ab7fd4f | 10364 | TREE_OPERAND (arg0, 1)); |
fef10b60 | 10365 | |
f4cd9b29 | 10366 | /* (A + A) * C -> A * 2 * C */ |
10367 | if (TREE_CODE (arg0) == PLUS_EXPR | |
10368 | && TREE_CODE (arg1) == INTEGER_CST | |
10369 | && operand_equal_p (TREE_OPERAND (arg0, 0), | |
10370 | TREE_OPERAND (arg0, 1), 0)) | |
389dd41b | 10371 | return fold_build2_loc (loc, MULT_EXPR, type, |
10372 | omit_one_operand_loc (loc, type, | |
10373 | TREE_OPERAND (arg0, 0), | |
f4cd9b29 | 10374 | TREE_OPERAND (arg0, 1)), |
389dd41b | 10375 | fold_build2_loc (loc, MULT_EXPR, type, |
f4cd9b29 | 10376 | build_int_cst (type, 2) , arg1)); |
10377 | ||
add6ee5e | 10378 | strict_overflow_p = false; |
fef10b60 | 10379 | if (TREE_CODE (arg1) == INTEGER_CST |
28fa8094 | 10380 | && 0 != (tem = extract_muldiv (op0, arg1, code, NULL_TREE, |
add6ee5e | 10381 | &strict_overflow_p))) |
10382 | { | |
10383 | if (strict_overflow_p) | |
10384 | fold_overflow_warning (("assuming signed overflow does not " | |
10385 | "occur when simplifying " | |
10386 | "multiplication"), | |
10387 | WARN_STRICT_OVERFLOW_MISC); | |
389dd41b | 10388 | return fold_convert_loc (loc, type, tem); |
add6ee5e | 10389 | } |
fef10b60 | 10390 | |
5f4f3617 | 10391 | /* Optimize z * conj(z) for integer complex numbers. */ |
10392 | if (TREE_CODE (arg0) == CONJ_EXPR | |
10393 | && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)) | |
389dd41b | 10394 | return fold_mult_zconjz (loc, type, arg1); |
5f4f3617 | 10395 | if (TREE_CODE (arg1) == CONJ_EXPR |
10396 | && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0)) | |
389dd41b | 10397 | return fold_mult_zconjz (loc, type, arg0); |
fef10b60 | 10398 | } |
10399 | else | |
10400 | { | |
10401 | /* Maybe fold x * 0 to 0. The expressions aren't the same | |
10402 | when x is NaN, since x * 0 is also NaN. Nor are they the | |
10403 | same in modes with signed zeros, since multiplying a | |
10404 | negative value by 0 gives -0, not +0. */ | |
10405 | if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0))) | |
10406 | && !HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg0))) | |
10407 | && real_zerop (arg1)) | |
389dd41b | 10408 | return omit_one_operand_loc (loc, type, arg1, arg0); |
fc86f9df | 10409 | /* In IEEE floating point, x*1 is not equivalent to x for snans. |
10410 | Likewise for complex arithmetic with signed zeros. */ | |
fef10b60 | 10411 | if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0))) |
fc86f9df | 10412 | && (!HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg0))) |
10413 | || !COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0))) | |
fef10b60 | 10414 | && real_onep (arg1)) |
389dd41b | 10415 | return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0)); |
fef10b60 | 10416 | |
10417 | /* Transform x * -1.0 into -x. */ | |
10418 | if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0))) | |
fc86f9df | 10419 | && (!HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg0))) |
10420 | || !COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0))) | |
fef10b60 | 10421 | && real_minus_onep (arg1)) |
389dd41b | 10422 | return fold_convert_loc (loc, type, negate_expr (arg0)); |
fef10b60 | 10423 | |
49d060d7 | 10424 | /* Convert (C1/X)*C2 into (C1*C2)/X. This transformation may change |
10425 | the result for floating point types due to rounding so it is applied | |
10426 | only if -fassociative-math was specify. */ | |
10427 | if (flag_associative_math | |
fef10b60 | 10428 | && TREE_CODE (arg0) == RDIV_EXPR |
10429 | && TREE_CODE (arg1) == REAL_CST | |
10430 | && TREE_CODE (TREE_OPERAND (arg0, 0)) == REAL_CST) | |
10431 | { | |
10432 | tree tem = const_binop (MULT_EXPR, TREE_OPERAND (arg0, 0), | |
10433 | arg1, 0); | |
10434 | if (tem) | |
389dd41b | 10435 | return fold_build2_loc (loc, RDIV_EXPR, type, tem, |
7ab7fd4f | 10436 | TREE_OPERAND (arg0, 1)); |
fef10b60 | 10437 | } |
10438 | ||
10439 | /* Strip sign operations from X in X*X, i.e. -Y*-Y -> Y*Y. */ | |
10440 | if (operand_equal_p (arg0, arg1, 0)) | |
10441 | { | |
10442 | tree tem = fold_strip_sign_ops (arg0); | |
10443 | if (tem != NULL_TREE) | |
10444 | { | |
389dd41b | 10445 | tem = fold_convert_loc (loc, type, tem); |
10446 | return fold_build2_loc (loc, MULT_EXPR, type, tem, tem); | |
fef10b60 | 10447 | } |
10448 | } | |
10449 | ||
ed97ac4e | 10450 | /* Fold z * +-I to __complex__ (-+__imag z, +-__real z). |
1af0d139 | 10451 | This is not the same for NaNs or if signed zeros are |
ed97ac4e | 10452 | involved. */ |
10453 | if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0))) | |
10454 | && !HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg0))) | |
10455 | && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0)) | |
10456 | && TREE_CODE (arg1) == COMPLEX_CST | |
10457 | && real_zerop (TREE_REALPART (arg1))) | |
10458 | { | |
10459 | tree rtype = TREE_TYPE (TREE_TYPE (arg0)); | |
10460 | if (real_onep (TREE_IMAGPART (arg1))) | |
389dd41b | 10461 | return |
10462 | fold_build2_loc (loc, COMPLEX_EXPR, type, | |
10463 | negate_expr (fold_build1_loc (loc, IMAGPART_EXPR, | |
10464 | rtype, arg0)), | |
10465 | fold_build1_loc (loc, REALPART_EXPR, rtype, arg0)); | |
ed97ac4e | 10466 | else if (real_minus_onep (TREE_IMAGPART (arg1))) |
389dd41b | 10467 | return |
10468 | fold_build2_loc (loc, COMPLEX_EXPR, type, | |
10469 | fold_build1_loc (loc, IMAGPART_EXPR, rtype, arg0), | |
10470 | negate_expr (fold_build1_loc (loc, REALPART_EXPR, | |
10471 | rtype, arg0))); | |
ed97ac4e | 10472 | } |
10473 | ||
5f4f3617 | 10474 | /* Optimize z * conj(z) for floating point complex numbers. |
10475 | Guarded by flag_unsafe_math_optimizations as non-finite | |
10476 | imaginary components don't produce scalar results. */ | |
10477 | if (flag_unsafe_math_optimizations | |
10478 | && TREE_CODE (arg0) == CONJ_EXPR | |
10479 | && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)) | |
389dd41b | 10480 | return fold_mult_zconjz (loc, type, arg1); |
5f4f3617 | 10481 | if (flag_unsafe_math_optimizations |
10482 | && TREE_CODE (arg1) == CONJ_EXPR | |
10483 | && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0)) | |
389dd41b | 10484 | return fold_mult_zconjz (loc, type, arg0); |
5f4f3617 | 10485 | |
fef10b60 | 10486 | if (flag_unsafe_math_optimizations) |
10487 | { | |
10488 | enum built_in_function fcode0 = builtin_mathfn_code (arg0); | |
10489 | enum built_in_function fcode1 = builtin_mathfn_code (arg1); | |
10490 | ||
10491 | /* Optimizations of root(...)*root(...). */ | |
10492 | if (fcode0 == fcode1 && BUILTIN_ROOT_P (fcode0)) | |
10493 | { | |
c2f47e15 | 10494 | tree rootfn, arg; |
10495 | tree arg00 = CALL_EXPR_ARG (arg0, 0); | |
10496 | tree arg10 = CALL_EXPR_ARG (arg1, 0); | |
fef10b60 | 10497 | |
10498 | /* Optimize sqrt(x)*sqrt(x) as x. */ | |
10499 | if (BUILTIN_SQRT_P (fcode0) | |
10500 | && operand_equal_p (arg00, arg10, 0) | |
10501 | && ! HONOR_SNANS (TYPE_MODE (type))) | |
10502 | return arg00; | |
10503 | ||
10504 | /* Optimize root(x)*root(y) as root(x*y). */ | |
c2f47e15 | 10505 | rootfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0); |
389dd41b | 10506 | arg = fold_build2_loc (loc, MULT_EXPR, type, arg00, arg10); |
10507 | return build_call_expr_loc (loc, rootfn, 1, arg); | |
fef10b60 | 10508 | } |
10509 | ||
10510 | /* Optimize expN(x)*expN(y) as expN(x+y). */ | |
10511 | if (fcode0 == fcode1 && BUILTIN_EXPONENT_P (fcode0)) | |
10512 | { | |
c2f47e15 | 10513 | tree expfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0); |
389dd41b | 10514 | tree arg = fold_build2_loc (loc, PLUS_EXPR, type, |
c2f47e15 | 10515 | CALL_EXPR_ARG (arg0, 0), |
10516 | CALL_EXPR_ARG (arg1, 0)); | |
389dd41b | 10517 | return build_call_expr_loc (loc, expfn, 1, arg); |
fef10b60 | 10518 | } |
10519 | ||
10520 | /* Optimizations of pow(...)*pow(...). */ | |
10521 | if ((fcode0 == BUILT_IN_POW && fcode1 == BUILT_IN_POW) | |
10522 | || (fcode0 == BUILT_IN_POWF && fcode1 == BUILT_IN_POWF) | |
10523 | || (fcode0 == BUILT_IN_POWL && fcode1 == BUILT_IN_POWL)) | |
10524 | { | |
c2f47e15 | 10525 | tree arg00 = CALL_EXPR_ARG (arg0, 0); |
10526 | tree arg01 = CALL_EXPR_ARG (arg0, 1); | |
10527 | tree arg10 = CALL_EXPR_ARG (arg1, 0); | |
10528 | tree arg11 = CALL_EXPR_ARG (arg1, 1); | |
fef10b60 | 10529 | |
10530 | /* Optimize pow(x,y)*pow(z,y) as pow(x*z,y). */ | |
10531 | if (operand_equal_p (arg01, arg11, 0)) | |
10532 | { | |
c2f47e15 | 10533 | tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0); |
389dd41b | 10534 | tree arg = fold_build2_loc (loc, MULT_EXPR, type, |
10535 | arg00, arg10); | |
10536 | return build_call_expr_loc (loc, powfn, 2, arg, arg01); | |
fef10b60 | 10537 | } |
10538 | ||
10539 | /* Optimize pow(x,y)*pow(x,z) as pow(x,y+z). */ | |
10540 | if (operand_equal_p (arg00, arg10, 0)) | |
10541 | { | |
c2f47e15 | 10542 | tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0); |
389dd41b | 10543 | tree arg = fold_build2_loc (loc, PLUS_EXPR, type, |
10544 | arg01, arg11); | |
10545 | return build_call_expr_loc (loc, powfn, 2, arg00, arg); | |
fef10b60 | 10546 | } |
10547 | } | |
10548 | ||
10549 | /* Optimize tan(x)*cos(x) as sin(x). */ | |
10550 | if (((fcode0 == BUILT_IN_TAN && fcode1 == BUILT_IN_COS) | |
10551 | || (fcode0 == BUILT_IN_TANF && fcode1 == BUILT_IN_COSF) | |
10552 | || (fcode0 == BUILT_IN_TANL && fcode1 == BUILT_IN_COSL) | |
10553 | || (fcode0 == BUILT_IN_COS && fcode1 == BUILT_IN_TAN) | |
10554 | || (fcode0 == BUILT_IN_COSF && fcode1 == BUILT_IN_TANF) | |
10555 | || (fcode0 == BUILT_IN_COSL && fcode1 == BUILT_IN_TANL)) | |
c2f47e15 | 10556 | && operand_equal_p (CALL_EXPR_ARG (arg0, 0), |
10557 | CALL_EXPR_ARG (arg1, 0), 0)) | |
fef10b60 | 10558 | { |
10559 | tree sinfn = mathfn_built_in (type, BUILT_IN_SIN); | |
10560 | ||
10561 | if (sinfn != NULL_TREE) | |
389dd41b | 10562 | return build_call_expr_loc (loc, sinfn, 1, |
10563 | CALL_EXPR_ARG (arg0, 0)); | |
fef10b60 | 10564 | } |
10565 | ||
10566 | /* Optimize x*pow(x,c) as pow(x,c+1). */ | |
10567 | if (fcode1 == BUILT_IN_POW | |
10568 | || fcode1 == BUILT_IN_POWF | |
10569 | || fcode1 == BUILT_IN_POWL) | |
10570 | { | |
c2f47e15 | 10571 | tree arg10 = CALL_EXPR_ARG (arg1, 0); |
10572 | tree arg11 = CALL_EXPR_ARG (arg1, 1); | |
fef10b60 | 10573 | if (TREE_CODE (arg11) == REAL_CST |
f96bd2bf | 10574 | && !TREE_OVERFLOW (arg11) |
fef10b60 | 10575 | && operand_equal_p (arg0, arg10, 0)) |
10576 | { | |
c2f47e15 | 10577 | tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg1), 0); |
fef10b60 | 10578 | REAL_VALUE_TYPE c; |
c2f47e15 | 10579 | tree arg; |
fef10b60 | 10580 | |
10581 | c = TREE_REAL_CST (arg11); | |
10582 | real_arithmetic (&c, PLUS_EXPR, &c, &dconst1); | |
10583 | arg = build_real (type, c); | |
389dd41b | 10584 | return build_call_expr_loc (loc, powfn, 2, arg0, arg); |
fef10b60 | 10585 | } |
10586 | } | |
10587 | ||
10588 | /* Optimize pow(x,c)*x as pow(x,c+1). */ | |
10589 | if (fcode0 == BUILT_IN_POW | |
10590 | || fcode0 == BUILT_IN_POWF | |
10591 | || fcode0 == BUILT_IN_POWL) | |
10592 | { | |
c2f47e15 | 10593 | tree arg00 = CALL_EXPR_ARG (arg0, 0); |
10594 | tree arg01 = CALL_EXPR_ARG (arg0, 1); | |
fef10b60 | 10595 | if (TREE_CODE (arg01) == REAL_CST |
f96bd2bf | 10596 | && !TREE_OVERFLOW (arg01) |
fef10b60 | 10597 | && operand_equal_p (arg1, arg00, 0)) |
10598 | { | |
c2f47e15 | 10599 | tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0); |
fef10b60 | 10600 | REAL_VALUE_TYPE c; |
c2f47e15 | 10601 | tree arg; |
fef10b60 | 10602 | |
10603 | c = TREE_REAL_CST (arg01); | |
10604 | real_arithmetic (&c, PLUS_EXPR, &c, &dconst1); | |
10605 | arg = build_real (type, c); | |
389dd41b | 10606 | return build_call_expr_loc (loc, powfn, 2, arg1, arg); |
fef10b60 | 10607 | } |
10608 | } | |
10609 | ||
10610 | /* Optimize x*x as pow(x,2.0), which is expanded as x*x. */ | |
0bfd8d5c | 10611 | if (optimize_function_for_speed_p (cfun) |
fef10b60 | 10612 | && operand_equal_p (arg0, arg1, 0)) |
10613 | { | |
10614 | tree powfn = mathfn_built_in (type, BUILT_IN_POW); | |
10615 | ||
10616 | if (powfn) | |
10617 | { | |
10618 | tree arg = build_real (type, dconst2); | |
389dd41b | 10619 | return build_call_expr_loc (loc, powfn, 2, arg0, arg); |
fef10b60 | 10620 | } |
10621 | } | |
10622 | } | |
10623 | } | |
10624 | goto associate; | |
10625 | ||
10626 | case BIT_IOR_EXPR: | |
10627 | bit_ior: | |
10628 | if (integer_all_onesp (arg1)) | |
389dd41b | 10629 | return omit_one_operand_loc (loc, type, arg1, arg0); |
fef10b60 | 10630 | if (integer_zerop (arg1)) |
389dd41b | 10631 | return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0)); |
fef10b60 | 10632 | if (operand_equal_p (arg0, arg1, 0)) |
389dd41b | 10633 | return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0)); |
fef10b60 | 10634 | |
10635 | /* ~X | X is -1. */ | |
10636 | if (TREE_CODE (arg0) == BIT_NOT_EXPR | |
10637 | && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)) | |
10638 | { | |
389dd41b | 10639 | t1 = fold_convert_loc (loc, type, integer_zero_node); |
10640 | t1 = fold_unary_loc (loc, BIT_NOT_EXPR, type, t1); | |
10641 | return omit_one_operand_loc (loc, type, t1, arg1); | |
fef10b60 | 10642 | } |
10643 | ||
10644 | /* X | ~X is -1. */ | |
10645 | if (TREE_CODE (arg1) == BIT_NOT_EXPR | |
10646 | && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0)) | |
10647 | { | |
389dd41b | 10648 | t1 = fold_convert_loc (loc, type, integer_zero_node); |
10649 | t1 = fold_unary_loc (loc, BIT_NOT_EXPR, type, t1); | |
10650 | return omit_one_operand_loc (loc, type, t1, arg0); | |
fef10b60 | 10651 | } |
10652 | ||
191611dd | 10653 | /* Canonicalize (X & C1) | C2. */ |
10654 | if (TREE_CODE (arg0) == BIT_AND_EXPR | |
10655 | && TREE_CODE (arg1) == INTEGER_CST | |
10656 | && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST) | |
10657 | { | |
fd3e569d | 10658 | unsigned HOST_WIDE_INT hi1, lo1, hi2, lo2, hi3, lo3, mlo, mhi; |
10659 | int width = TYPE_PRECISION (type), w; | |
191611dd | 10660 | hi1 = TREE_INT_CST_HIGH (TREE_OPERAND (arg0, 1)); |
10661 | lo1 = TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1)); | |
10662 | hi2 = TREE_INT_CST_HIGH (arg1); | |
10663 | lo2 = TREE_INT_CST_LOW (arg1); | |
10664 | ||
10665 | /* If (C1&C2) == C1, then (X&C1)|C2 becomes (X,C2). */ | |
10666 | if ((hi1 & hi2) == hi1 && (lo1 & lo2) == lo1) | |
389dd41b | 10667 | return omit_one_operand_loc (loc, type, arg1, |
10668 | TREE_OPERAND (arg0, 0)); | |
191611dd | 10669 | |
10670 | if (width > HOST_BITS_PER_WIDE_INT) | |
10671 | { | |
48e1416a | 10672 | mhi = (unsigned HOST_WIDE_INT) -1 |
191611dd | 10673 | >> (2 * HOST_BITS_PER_WIDE_INT - width); |
10674 | mlo = -1; | |
10675 | } | |
10676 | else | |
10677 | { | |
10678 | mhi = 0; | |
10679 | mlo = (unsigned HOST_WIDE_INT) -1 | |
10680 | >> (HOST_BITS_PER_WIDE_INT - width); | |
10681 | } | |
10682 | ||
10683 | /* If (C1|C2) == ~0 then (X&C1)|C2 becomes X|C2. */ | |
10684 | if ((~(hi1 | hi2) & mhi) == 0 && (~(lo1 | lo2) & mlo) == 0) | |
389dd41b | 10685 | return fold_build2_loc (loc, BIT_IOR_EXPR, type, |
191611dd | 10686 | TREE_OPERAND (arg0, 0), arg1); |
10687 | ||
fd3e569d | 10688 | /* Minimize the number of bits set in C1, i.e. C1 := C1 & ~C2, |
10689 | unless (C1 & ~C2) | (C2 & C3) for some C3 is a mask of some | |
10690 | mode which allows further optimizations. */ | |
191611dd | 10691 | hi1 &= mhi; |
10692 | lo1 &= mlo; | |
fd3e569d | 10693 | hi2 &= mhi; |
10694 | lo2 &= mlo; | |
10695 | hi3 = hi1 & ~hi2; | |
10696 | lo3 = lo1 & ~lo2; | |
10697 | for (w = BITS_PER_UNIT; | |
10698 | w <= width && w <= HOST_BITS_PER_WIDE_INT; | |
10699 | w <<= 1) | |
10700 | { | |
10701 | unsigned HOST_WIDE_INT mask | |
10702 | = (unsigned HOST_WIDE_INT) -1 >> (HOST_BITS_PER_WIDE_INT - w); | |
10703 | if (((lo1 | lo2) & mask) == mask | |
10704 | && (lo1 & ~mask) == 0 && hi1 == 0) | |
10705 | { | |
10706 | hi3 = 0; | |
10707 | lo3 = mask; | |
10708 | break; | |
10709 | } | |
10710 | } | |
10711 | if (hi3 != hi1 || lo3 != lo1) | |
389dd41b | 10712 | return fold_build2_loc (loc, BIT_IOR_EXPR, type, |
10713 | fold_build2_loc (loc, BIT_AND_EXPR, type, | |
191611dd | 10714 | TREE_OPERAND (arg0, 0), |
10715 | build_int_cst_wide (type, | |
fd3e569d | 10716 | lo3, hi3)), |
191611dd | 10717 | arg1); |
10718 | } | |
10719 | ||
af55979c | 10720 | /* (X & Y) | Y is (X, Y). */ |
10721 | if (TREE_CODE (arg0) == BIT_AND_EXPR | |
10722 | && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0)) | |
389dd41b | 10723 | return omit_one_operand_loc (loc, type, arg1, TREE_OPERAND (arg0, 0)); |
af55979c | 10724 | /* (X & Y) | X is (Y, X). */ |
10725 | if (TREE_CODE (arg0) == BIT_AND_EXPR | |
10726 | && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0) | |
10727 | && reorder_operands_p (TREE_OPERAND (arg0, 1), arg1)) | |
389dd41b | 10728 | return omit_one_operand_loc (loc, type, arg1, TREE_OPERAND (arg0, 1)); |
af55979c | 10729 | /* X | (X & Y) is (Y, X). */ |
10730 | if (TREE_CODE (arg1) == BIT_AND_EXPR | |
10731 | && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0) | |
10732 | && reorder_operands_p (arg0, TREE_OPERAND (arg1, 1))) | |
389dd41b | 10733 | return omit_one_operand_loc (loc, type, arg0, TREE_OPERAND (arg1, 1)); |
af55979c | 10734 | /* X | (Y & X) is (Y, X). */ |
10735 | if (TREE_CODE (arg1) == BIT_AND_EXPR | |
10736 | && operand_equal_p (arg0, TREE_OPERAND (arg1, 1), 0) | |
10737 | && reorder_operands_p (arg0, TREE_OPERAND (arg1, 0))) | |
389dd41b | 10738 | return omit_one_operand_loc (loc, type, arg0, TREE_OPERAND (arg1, 0)); |
af55979c | 10739 | |
389dd41b | 10740 | t1 = distribute_bit_expr (loc, code, type, arg0, arg1); |
fef10b60 | 10741 | if (t1 != NULL_TREE) |
10742 | return t1; | |
10743 | ||
10744 | /* Convert (or (not arg0) (not arg1)) to (not (and (arg0) (arg1))). | |
10745 | ||
10746 | This results in more efficient code for machines without a NAND | |
10747 | instruction. Combine will canonicalize to the first form | |
10748 | which will allow use of NAND instructions provided by the | |
10749 | backend if they exist. */ | |
10750 | if (TREE_CODE (arg0) == BIT_NOT_EXPR | |
10751 | && TREE_CODE (arg1) == BIT_NOT_EXPR) | |
10752 | { | |
389dd41b | 10753 | return |
10754 | fold_build1_loc (loc, BIT_NOT_EXPR, type, | |
10755 | build2 (BIT_AND_EXPR, type, | |
10756 | fold_convert_loc (loc, type, | |
10757 | TREE_OPERAND (arg0, 0)), | |
10758 | fold_convert_loc (loc, type, | |
10759 | TREE_OPERAND (arg1, 0)))); | |
fef10b60 | 10760 | } |
10761 | ||
10762 | /* See if this can be simplified into a rotate first. If that | |
10763 | is unsuccessful continue in the association code. */ | |
10764 | goto bit_rotate; | |
10765 | ||
10766 | case BIT_XOR_EXPR: | |
10767 | if (integer_zerop (arg1)) | |
389dd41b | 10768 | return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0)); |
fef10b60 | 10769 | if (integer_all_onesp (arg1)) |
389dd41b | 10770 | return fold_build1_loc (loc, BIT_NOT_EXPR, type, op0); |
fef10b60 | 10771 | if (operand_equal_p (arg0, arg1, 0)) |
389dd41b | 10772 | return omit_one_operand_loc (loc, type, integer_zero_node, arg0); |
fef10b60 | 10773 | |
10774 | /* ~X ^ X is -1. */ | |
10775 | if (TREE_CODE (arg0) == BIT_NOT_EXPR | |
10776 | && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)) | |
10777 | { | |
389dd41b | 10778 | t1 = fold_convert_loc (loc, type, integer_zero_node); |
10779 | t1 = fold_unary_loc (loc, BIT_NOT_EXPR, type, t1); | |
10780 | return omit_one_operand_loc (loc, type, t1, arg1); | |
fef10b60 | 10781 | } |
10782 | ||
10783 | /* X ^ ~X is -1. */ | |
10784 | if (TREE_CODE (arg1) == BIT_NOT_EXPR | |
10785 | && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0)) | |
10786 | { | |
389dd41b | 10787 | t1 = fold_convert_loc (loc, type, integer_zero_node); |
10788 | t1 = fold_unary_loc (loc, BIT_NOT_EXPR, type, t1); | |
10789 | return omit_one_operand_loc (loc, type, t1, arg0); | |
fef10b60 | 10790 | } |
10791 | ||
10792 | /* If we are XORing two BIT_AND_EXPR's, both of which are and'ing | |
10793 | with a constant, and the two constants have no bits in common, | |
10794 | we should treat this as a BIT_IOR_EXPR since this may produce more | |
10795 | simplifications. */ | |
10796 | if (TREE_CODE (arg0) == BIT_AND_EXPR | |
10797 | && TREE_CODE (arg1) == BIT_AND_EXPR | |
10798 | && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST | |
10799 | && TREE_CODE (TREE_OPERAND (arg1, 1)) == INTEGER_CST | |
10800 | && integer_zerop (const_binop (BIT_AND_EXPR, | |
10801 | TREE_OPERAND (arg0, 1), | |
10802 | TREE_OPERAND (arg1, 1), 0))) | |
10803 | { | |
10804 | code = BIT_IOR_EXPR; | |
10805 | goto bit_ior; | |
10806 | } | |
10807 | ||
a433d54b | 10808 | /* (X | Y) ^ X -> Y & ~ X*/ |
10809 | if (TREE_CODE (arg0) == BIT_IOR_EXPR | |
10810 | && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)) | |
10811 | { | |
10812 | tree t2 = TREE_OPERAND (arg0, 1); | |
389dd41b | 10813 | t1 = fold_build1_loc (loc, BIT_NOT_EXPR, TREE_TYPE (arg1), |
a433d54b | 10814 | arg1); |
389dd41b | 10815 | t1 = fold_build2_loc (loc, BIT_AND_EXPR, type, |
10816 | fold_convert_loc (loc, type, t2), | |
10817 | fold_convert_loc (loc, type, t1)); | |
a433d54b | 10818 | return t1; |
10819 | } | |
10820 | ||
10821 | /* (Y | X) ^ X -> Y & ~ X*/ | |
10822 | if (TREE_CODE (arg0) == BIT_IOR_EXPR | |
10823 | && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0)) | |
10824 | { | |
10825 | tree t2 = TREE_OPERAND (arg0, 0); | |
389dd41b | 10826 | t1 = fold_build1_loc (loc, BIT_NOT_EXPR, TREE_TYPE (arg1), |
a433d54b | 10827 | arg1); |
389dd41b | 10828 | t1 = fold_build2_loc (loc, BIT_AND_EXPR, type, |
10829 | fold_convert_loc (loc, type, t2), | |
10830 | fold_convert_loc (loc, type, t1)); | |
a433d54b | 10831 | return t1; |
10832 | } | |
10833 | ||
10834 | /* X ^ (X | Y) -> Y & ~ X*/ | |
10835 | if (TREE_CODE (arg1) == BIT_IOR_EXPR | |
10836 | && operand_equal_p (TREE_OPERAND (arg1, 0), arg0, 0)) | |
10837 | { | |
10838 | tree t2 = TREE_OPERAND (arg1, 1); | |
389dd41b | 10839 | t1 = fold_build1_loc (loc, BIT_NOT_EXPR, TREE_TYPE (arg0), |
a433d54b | 10840 | arg0); |
389dd41b | 10841 | t1 = fold_build2_loc (loc, BIT_AND_EXPR, type, |
10842 | fold_convert_loc (loc, type, t2), | |
10843 | fold_convert_loc (loc, type, t1)); | |
a433d54b | 10844 | return t1; |
10845 | } | |
10846 | ||
10847 | /* X ^ (Y | X) -> Y & ~ X*/ | |
10848 | if (TREE_CODE (arg1) == BIT_IOR_EXPR | |
10849 | && operand_equal_p (TREE_OPERAND (arg1, 1), arg0, 0)) | |
10850 | { | |
10851 | tree t2 = TREE_OPERAND (arg1, 0); | |
389dd41b | 10852 | t1 = fold_build1_loc (loc, BIT_NOT_EXPR, TREE_TYPE (arg0), |
a433d54b | 10853 | arg0); |
389dd41b | 10854 | t1 = fold_build2_loc (loc, BIT_AND_EXPR, type, |
10855 | fold_convert_loc (loc, type, t2), | |
10856 | fold_convert_loc (loc, type, t1)); | |
a433d54b | 10857 | return t1; |
10858 | } | |
48e1416a | 10859 | |
8cadcd8f | 10860 | /* Convert ~X ^ ~Y to X ^ Y. */ |
10861 | if (TREE_CODE (arg0) == BIT_NOT_EXPR | |
10862 | && TREE_CODE (arg1) == BIT_NOT_EXPR) | |
389dd41b | 10863 | return fold_build2_loc (loc, code, type, |
10864 | fold_convert_loc (loc, type, | |
10865 | TREE_OPERAND (arg0, 0)), | |
10866 | fold_convert_loc (loc, type, | |
10867 | TREE_OPERAND (arg1, 0))); | |
8cadcd8f | 10868 | |
1518c00b | 10869 | /* Convert ~X ^ C to X ^ ~C. */ |
10870 | if (TREE_CODE (arg0) == BIT_NOT_EXPR | |
10871 | && TREE_CODE (arg1) == INTEGER_CST) | |
389dd41b | 10872 | return fold_build2_loc (loc, code, type, |
10873 | fold_convert_loc (loc, type, | |
10874 | TREE_OPERAND (arg0, 0)), | |
10875 | fold_build1_loc (loc, BIT_NOT_EXPR, type, arg1)); | |
1518c00b | 10876 | |
7ddc480e | 10877 | /* Fold (X & 1) ^ 1 as (X & 1) == 0. */ |
10878 | if (TREE_CODE (arg0) == BIT_AND_EXPR | |
10879 | && integer_onep (TREE_OPERAND (arg0, 1)) | |
10880 | && integer_onep (arg1)) | |
389dd41b | 10881 | return fold_build2_loc (loc, EQ_EXPR, type, arg0, |
7ddc480e | 10882 | build_int_cst (TREE_TYPE (arg0), 0)); |
10883 | ||
a3cf48f5 | 10884 | /* Fold (X & Y) ^ Y as ~X & Y. */ |
10885 | if (TREE_CODE (arg0) == BIT_AND_EXPR | |
10886 | && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0)) | |
10887 | { | |
389dd41b | 10888 | tem = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0)); |
48e1416a | 10889 | return fold_build2_loc (loc, BIT_AND_EXPR, type, |
389dd41b | 10890 | fold_build1_loc (loc, BIT_NOT_EXPR, type, tem), |
10891 | fold_convert_loc (loc, type, arg1)); | |
a3cf48f5 | 10892 | } |
10893 | /* Fold (X & Y) ^ X as ~Y & X. */ | |
10894 | if (TREE_CODE (arg0) == BIT_AND_EXPR | |
10895 | && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0) | |
10896 | && reorder_operands_p (TREE_OPERAND (arg0, 1), arg1)) | |
10897 | { | |
389dd41b | 10898 | tem = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 1)); |
10899 | return fold_build2_loc (loc, BIT_AND_EXPR, type, | |
10900 | fold_build1_loc (loc, BIT_NOT_EXPR, type, tem), | |
10901 | fold_convert_loc (loc, type, arg1)); | |
a3cf48f5 | 10902 | } |
10903 | /* Fold X ^ (X & Y) as X & ~Y. */ | |
10904 | if (TREE_CODE (arg1) == BIT_AND_EXPR | |
10905 | && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0)) | |
10906 | { | |
389dd41b | 10907 | tem = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 1)); |
10908 | return fold_build2_loc (loc, BIT_AND_EXPR, type, | |
10909 | fold_convert_loc (loc, type, arg0), | |
10910 | fold_build1_loc (loc, BIT_NOT_EXPR, type, tem)); | |
a3cf48f5 | 10911 | } |
10912 | /* Fold X ^ (Y & X) as ~Y & X. */ | |
10913 | if (TREE_CODE (arg1) == BIT_AND_EXPR | |
10914 | && operand_equal_p (arg0, TREE_OPERAND (arg1, 1), 0) | |
10915 | && reorder_operands_p (arg0, TREE_OPERAND (arg1, 0))) | |
10916 | { | |
389dd41b | 10917 | tem = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 0)); |
10918 | return fold_build2_loc (loc, BIT_AND_EXPR, type, | |
10919 | fold_build1_loc (loc, BIT_NOT_EXPR, type, tem), | |
10920 | fold_convert_loc (loc, type, arg0)); | |
a3cf48f5 | 10921 | } |
10922 | ||
fef10b60 | 10923 | /* See if this can be simplified into a rotate first. If that |
10924 | is unsuccessful continue in the association code. */ | |
10925 | goto bit_rotate; | |
10926 | ||
10927 | case BIT_AND_EXPR: | |
10928 | if (integer_all_onesp (arg1)) | |
389dd41b | 10929 | return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0)); |
fef10b60 | 10930 | if (integer_zerop (arg1)) |
389dd41b | 10931 | return omit_one_operand_loc (loc, type, arg1, arg0); |
fef10b60 | 10932 | if (operand_equal_p (arg0, arg1, 0)) |
389dd41b | 10933 | return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0)); |
fef10b60 | 10934 | |
10935 | /* ~X & X is always zero. */ | |
10936 | if (TREE_CODE (arg0) == BIT_NOT_EXPR | |
10937 | && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)) | |
389dd41b | 10938 | return omit_one_operand_loc (loc, type, integer_zero_node, arg1); |
fef10b60 | 10939 | |
10940 | /* X & ~X is always zero. */ | |
10941 | if (TREE_CODE (arg1) == BIT_NOT_EXPR | |
10942 | && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0)) | |
389dd41b | 10943 | return omit_one_operand_loc (loc, type, integer_zero_node, arg0); |
fef10b60 | 10944 | |
191611dd | 10945 | /* Canonicalize (X | C1) & C2 as (X & C2) | (C1 & C2). */ |
10946 | if (TREE_CODE (arg0) == BIT_IOR_EXPR | |
10947 | && TREE_CODE (arg1) == INTEGER_CST | |
10948 | && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST) | |
05e1595b | 10949 | { |
389dd41b | 10950 | tree tmp1 = fold_convert_loc (loc, type, arg1); |
10951 | tree tmp2 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0)); | |
10952 | tree tmp3 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 1)); | |
10953 | tmp2 = fold_build2_loc (loc, BIT_AND_EXPR, type, tmp2, tmp1); | |
10954 | tmp3 = fold_build2_loc (loc, BIT_AND_EXPR, type, tmp3, tmp1); | |
10955 | return | |
10956 | fold_convert_loc (loc, type, | |
10957 | fold_build2_loc (loc, BIT_IOR_EXPR, | |
10958 | type, tmp2, tmp3)); | |
05e1595b | 10959 | } |
191611dd | 10960 | |
af55979c | 10961 | /* (X | Y) & Y is (X, Y). */ |
10962 | if (TREE_CODE (arg0) == BIT_IOR_EXPR | |
10963 | && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0)) | |
389dd41b | 10964 | return omit_one_operand_loc (loc, type, arg1, TREE_OPERAND (arg0, 0)); |
af55979c | 10965 | /* (X | Y) & X is (Y, X). */ |
10966 | if (TREE_CODE (arg0) == BIT_IOR_EXPR | |
10967 | && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0) | |
10968 | && reorder_operands_p (TREE_OPERAND (arg0, 1), arg1)) | |
389dd41b | 10969 | return omit_one_operand_loc (loc, type, arg1, TREE_OPERAND (arg0, 1)); |
af55979c | 10970 | /* X & (X | Y) is (Y, X). */ |
10971 | if (TREE_CODE (arg1) == BIT_IOR_EXPR | |
10972 | && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0) | |
10973 | && reorder_operands_p (arg0, TREE_OPERAND (arg1, 1))) | |
389dd41b | 10974 | return omit_one_operand_loc (loc, type, arg0, TREE_OPERAND (arg1, 1)); |
af55979c | 10975 | /* X & (Y | X) is (Y, X). */ |
10976 | if (TREE_CODE (arg1) == BIT_IOR_EXPR | |
10977 | && operand_equal_p (arg0, TREE_OPERAND (arg1, 1), 0) | |
10978 | && reorder_operands_p (arg0, TREE_OPERAND (arg1, 0))) | |
389dd41b | 10979 | return omit_one_operand_loc (loc, type, arg0, TREE_OPERAND (arg1, 0)); |
af55979c | 10980 | |
7ddc480e | 10981 | /* Fold (X ^ 1) & 1 as (X & 1) == 0. */ |
10982 | if (TREE_CODE (arg0) == BIT_XOR_EXPR | |
10983 | && integer_onep (TREE_OPERAND (arg0, 1)) | |
10984 | && integer_onep (arg1)) | |
10985 | { | |
10986 | tem = TREE_OPERAND (arg0, 0); | |
389dd41b | 10987 | return fold_build2_loc (loc, EQ_EXPR, type, |
10988 | fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (tem), tem, | |
7ddc480e | 10989 | build_int_cst (TREE_TYPE (tem), 1)), |
10990 | build_int_cst (TREE_TYPE (tem), 0)); | |
10991 | } | |
10992 | /* Fold ~X & 1 as (X & 1) == 0. */ | |
10993 | if (TREE_CODE (arg0) == BIT_NOT_EXPR | |
10994 | && integer_onep (arg1)) | |
10995 | { | |
10996 | tem = TREE_OPERAND (arg0, 0); | |
389dd41b | 10997 | return fold_build2_loc (loc, EQ_EXPR, type, |
10998 | fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (tem), tem, | |
7ddc480e | 10999 | build_int_cst (TREE_TYPE (tem), 1)), |
11000 | build_int_cst (TREE_TYPE (tem), 0)); | |
11001 | } | |
11002 | ||
a3cf48f5 | 11003 | /* Fold (X ^ Y) & Y as ~X & Y. */ |
11004 | if (TREE_CODE (arg0) == BIT_XOR_EXPR | |
11005 | && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0)) | |
11006 | { | |
389dd41b | 11007 | tem = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0)); |
48e1416a | 11008 | return fold_build2_loc (loc, BIT_AND_EXPR, type, |
389dd41b | 11009 | fold_build1_loc (loc, BIT_NOT_EXPR, type, tem), |
11010 | fold_convert_loc (loc, type, arg1)); | |
a3cf48f5 | 11011 | } |
11012 | /* Fold (X ^ Y) & X as ~Y & X. */ | |
11013 | if (TREE_CODE (arg0) == BIT_XOR_EXPR | |
11014 | && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0) | |
11015 | && reorder_operands_p (TREE_OPERAND (arg0, 1), arg1)) | |
11016 | { | |
389dd41b | 11017 | tem = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 1)); |
11018 | return fold_build2_loc (loc, BIT_AND_EXPR, type, | |
11019 | fold_build1_loc (loc, BIT_NOT_EXPR, type, tem), | |
11020 | fold_convert_loc (loc, type, arg1)); | |
a3cf48f5 | 11021 | } |
11022 | /* Fold X & (X ^ Y) as X & ~Y. */ | |
11023 | if (TREE_CODE (arg1) == BIT_XOR_EXPR | |
11024 | && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0)) | |
11025 | { | |
389dd41b | 11026 | tem = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 1)); |
11027 | return fold_build2_loc (loc, BIT_AND_EXPR, type, | |
11028 | fold_convert_loc (loc, type, arg0), | |
11029 | fold_build1_loc (loc, BIT_NOT_EXPR, type, tem)); | |
a3cf48f5 | 11030 | } |
11031 | /* Fold X & (Y ^ X) as ~Y & X. */ | |
11032 | if (TREE_CODE (arg1) == BIT_XOR_EXPR | |
11033 | && operand_equal_p (arg0, TREE_OPERAND (arg1, 1), 0) | |
11034 | && reorder_operands_p (arg0, TREE_OPERAND (arg1, 0))) | |
11035 | { | |
389dd41b | 11036 | tem = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 0)); |
11037 | return fold_build2_loc (loc, BIT_AND_EXPR, type, | |
11038 | fold_build1_loc (loc, BIT_NOT_EXPR, type, tem), | |
11039 | fold_convert_loc (loc, type, arg0)); | |
a3cf48f5 | 11040 | } |
11041 | ||
389dd41b | 11042 | t1 = distribute_bit_expr (loc, code, type, arg0, arg1); |
fef10b60 | 11043 | if (t1 != NULL_TREE) |
11044 | return t1; | |
11045 | /* Simplify ((int)c & 0377) into (int)c, if c is unsigned char. */ | |
11046 | if (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg0) == NOP_EXPR | |
11047 | && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg0, 0)))) | |
11048 | { | |
11049 | unsigned int prec | |
11050 | = TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg0, 0))); | |
11051 | ||
11052 | if (prec < BITS_PER_WORD && prec < HOST_BITS_PER_WIDE_INT | |
11053 | && (~TREE_INT_CST_LOW (arg1) | |
11054 | & (((HOST_WIDE_INT) 1 << prec) - 1)) == 0) | |
389dd41b | 11055 | return |
11056 | fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0)); | |
fef10b60 | 11057 | } |
11058 | ||
11059 | /* Convert (and (not arg0) (not arg1)) to (not (or (arg0) (arg1))). | |
11060 | ||
11061 | This results in more efficient code for machines without a NOR | |
11062 | instruction. Combine will canonicalize to the first form | |
11063 | which will allow use of NOR instructions provided by the | |
11064 | backend if they exist. */ | |
11065 | if (TREE_CODE (arg0) == BIT_NOT_EXPR | |
11066 | && TREE_CODE (arg1) == BIT_NOT_EXPR) | |
11067 | { | |
389dd41b | 11068 | return fold_build1_loc (loc, BIT_NOT_EXPR, type, |
7ab7fd4f | 11069 | build2 (BIT_IOR_EXPR, type, |
389dd41b | 11070 | fold_convert_loc (loc, type, |
11071 | TREE_OPERAND (arg0, 0)), | |
11072 | fold_convert_loc (loc, type, | |
11073 | TREE_OPERAND (arg1, 0)))); | |
fef10b60 | 11074 | } |
11075 | ||
4486d2b7 | 11076 | /* If arg0 is derived from the address of an object or function, we may |
11077 | be able to fold this expression using the object or function's | |
11078 | alignment. */ | |
11079 | if (POINTER_TYPE_P (TREE_TYPE (arg0)) && host_integerp (arg1, 1)) | |
11080 | { | |
11081 | unsigned HOST_WIDE_INT modulus, residue; | |
11082 | unsigned HOST_WIDE_INT low = TREE_INT_CST_LOW (arg1); | |
11083 | ||
1c4607fd | 11084 | modulus = get_pointer_modulus_and_residue (arg0, &residue, |
11085 | integer_onep (arg1)); | |
4486d2b7 | 11086 | |
11087 | /* This works because modulus is a power of 2. If this weren't the | |
11088 | case, we'd have to replace it by its greatest power-of-2 | |
11089 | divisor: modulus & -modulus. */ | |
11090 | if (low < modulus) | |
11091 | return build_int_cst (type, residue & low); | |
11092 | } | |
11093 | ||
2cf28ced | 11094 | /* Fold (X << C1) & C2 into (X << C1) & (C2 | ((1 << C1) - 1)) |
11095 | (X >> C1) & C2 into (X >> C1) & (C2 | ~((type) -1 >> C1)) | |
11096 | if the new mask might be further optimized. */ | |
11097 | if ((TREE_CODE (arg0) == LSHIFT_EXPR | |
11098 | || TREE_CODE (arg0) == RSHIFT_EXPR) | |
11099 | && host_integerp (TREE_OPERAND (arg0, 1), 1) | |
11100 | && host_integerp (arg1, TYPE_UNSIGNED (TREE_TYPE (arg1))) | |
11101 | && tree_low_cst (TREE_OPERAND (arg0, 1), 1) | |
11102 | < TYPE_PRECISION (TREE_TYPE (arg0)) | |
11103 | && TYPE_PRECISION (TREE_TYPE (arg0)) <= HOST_BITS_PER_WIDE_INT | |
11104 | && tree_low_cst (TREE_OPERAND (arg0, 1), 1) > 0) | |
11105 | { | |
11106 | unsigned int shiftc = tree_low_cst (TREE_OPERAND (arg0, 1), 1); | |
11107 | unsigned HOST_WIDE_INT mask | |
11108 | = tree_low_cst (arg1, TYPE_UNSIGNED (TREE_TYPE (arg1))); | |
11109 | unsigned HOST_WIDE_INT newmask, zerobits = 0; | |
11110 | tree shift_type = TREE_TYPE (arg0); | |
11111 | ||
11112 | if (TREE_CODE (arg0) == LSHIFT_EXPR) | |
11113 | zerobits = ((((unsigned HOST_WIDE_INT) 1) << shiftc) - 1); | |
11114 | else if (TREE_CODE (arg0) == RSHIFT_EXPR | |
11115 | && TYPE_PRECISION (TREE_TYPE (arg0)) | |
11116 | == GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (arg0)))) | |
11117 | { | |
11118 | unsigned int prec = TYPE_PRECISION (TREE_TYPE (arg0)); | |
11119 | tree arg00 = TREE_OPERAND (arg0, 0); | |
11120 | /* See if more bits can be proven as zero because of | |
11121 | zero extension. */ | |
11122 | if (TREE_CODE (arg00) == NOP_EXPR | |
11123 | && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg00, 0)))) | |
11124 | { | |
11125 | tree inner_type = TREE_TYPE (TREE_OPERAND (arg00, 0)); | |
11126 | if (TYPE_PRECISION (inner_type) | |
11127 | == GET_MODE_BITSIZE (TYPE_MODE (inner_type)) | |
11128 | && TYPE_PRECISION (inner_type) < prec) | |
11129 | { | |
11130 | prec = TYPE_PRECISION (inner_type); | |
11131 | /* See if we can shorten the right shift. */ | |
11132 | if (shiftc < prec) | |
11133 | shift_type = inner_type; | |
11134 | } | |
11135 | } | |
11136 | zerobits = ~(unsigned HOST_WIDE_INT) 0; | |
11137 | zerobits >>= HOST_BITS_PER_WIDE_INT - shiftc; | |
11138 | zerobits <<= prec - shiftc; | |
11139 | /* For arithmetic shift if sign bit could be set, zerobits | |
11140 | can contain actually sign bits, so no transformation is | |
11141 | possible, unless MASK masks them all away. In that | |
11142 | case the shift needs to be converted into logical shift. */ | |
11143 | if (!TYPE_UNSIGNED (TREE_TYPE (arg0)) | |
11144 | && prec == TYPE_PRECISION (TREE_TYPE (arg0))) | |
11145 | { | |
11146 | if ((mask & zerobits) == 0) | |
11147 | shift_type = unsigned_type_for (TREE_TYPE (arg0)); | |
11148 | else | |
11149 | zerobits = 0; | |
11150 | } | |
11151 | } | |
11152 | ||
11153 | /* ((X << 16) & 0xff00) is (X, 0). */ | |
11154 | if ((mask & zerobits) == mask) | |
389dd41b | 11155 | return omit_one_operand_loc (loc, type, |
11156 | build_int_cst (type, 0), arg0); | |
2cf28ced | 11157 | |
11158 | newmask = mask | zerobits; | |
11159 | if (newmask != mask && (newmask & (newmask + 1)) == 0) | |
11160 | { | |
11161 | unsigned int prec; | |
11162 | ||
11163 | /* Only do the transformation if NEWMASK is some integer | |
11164 | mode's mask. */ | |
11165 | for (prec = BITS_PER_UNIT; | |
11166 | prec < HOST_BITS_PER_WIDE_INT; prec <<= 1) | |
11167 | if (newmask == (((unsigned HOST_WIDE_INT) 1) << prec) - 1) | |
11168 | break; | |
11169 | if (prec < HOST_BITS_PER_WIDE_INT | |
11170 | || newmask == ~(unsigned HOST_WIDE_INT) 0) | |
11171 | { | |
6189c517 | 11172 | tree newmaskt; |
11173 | ||
2cf28ced | 11174 | if (shift_type != TREE_TYPE (arg0)) |
11175 | { | |
389dd41b | 11176 | tem = fold_build2_loc (loc, TREE_CODE (arg0), shift_type, |
11177 | fold_convert_loc (loc, shift_type, | |
11178 | TREE_OPERAND (arg0, 0)), | |
2cf28ced | 11179 | TREE_OPERAND (arg0, 1)); |
389dd41b | 11180 | tem = fold_convert_loc (loc, type, tem); |
2cf28ced | 11181 | } |
11182 | else | |
11183 | tem = op0; | |
6189c517 | 11184 | newmaskt = build_int_cst_type (TREE_TYPE (op1), newmask); |
11185 | if (!tree_int_cst_equal (newmaskt, arg1)) | |
389dd41b | 11186 | return fold_build2_loc (loc, BIT_AND_EXPR, type, tem, newmaskt); |
2cf28ced | 11187 | } |
11188 | } | |
11189 | } | |
11190 | ||
fef10b60 | 11191 | goto associate; |
11192 | ||
11193 | case RDIV_EXPR: | |
11194 | /* Don't touch a floating-point divide by zero unless the mode | |
11195 | of the constant can represent infinity. */ | |
11196 | if (TREE_CODE (arg1) == REAL_CST | |
11197 | && !MODE_HAS_INFINITIES (TYPE_MODE (TREE_TYPE (arg1))) | |
11198 | && real_zerop (arg1)) | |
e7edfbbd | 11199 | return NULL_TREE; |
fef10b60 | 11200 | |
73444c65 | 11201 | /* Optimize A / A to 1.0 if we don't care about |
c2c96591 | 11202 | NaNs or Infinities. Skip the transformation |
11203 | for non-real operands. */ | |
11204 | if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (arg0)) | |
11205 | && ! HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0))) | |
73444c65 | 11206 | && ! HONOR_INFINITIES (TYPE_MODE (TREE_TYPE (arg0))) |
11207 | && operand_equal_p (arg0, arg1, 0)) | |
11208 | { | |
11209 | tree r = build_real (TREE_TYPE (arg0), dconst1); | |
11210 | ||
389dd41b | 11211 | return omit_two_operands_loc (loc, type, r, arg0, arg1); |
73444c65 | 11212 | } |
11213 | ||
c2c96591 | 11214 | /* The complex version of the above A / A optimization. */ |
11215 | if (COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0)) | |
11216 | && operand_equal_p (arg0, arg1, 0)) | |
11217 | { | |
11218 | tree elem_type = TREE_TYPE (TREE_TYPE (arg0)); | |
11219 | if (! HONOR_NANS (TYPE_MODE (elem_type)) | |
11220 | && ! HONOR_INFINITIES (TYPE_MODE (elem_type))) | |
11221 | { | |
11222 | tree r = build_real (elem_type, dconst1); | |
11223 | /* omit_two_operands will call fold_convert for us. */ | |
389dd41b | 11224 | return omit_two_operands_loc (loc, type, r, arg0, arg1); |
c2c96591 | 11225 | } |
11226 | } | |
11227 | ||
fef10b60 | 11228 | /* (-A) / (-B) -> A / B */ |
11229 | if (TREE_CODE (arg0) == NEGATE_EXPR && negate_expr_p (arg1)) | |
389dd41b | 11230 | return fold_build2_loc (loc, RDIV_EXPR, type, |
7ab7fd4f | 11231 | TREE_OPERAND (arg0, 0), |
11232 | negate_expr (arg1)); | |
fef10b60 | 11233 | if (TREE_CODE (arg1) == NEGATE_EXPR && negate_expr_p (arg0)) |
389dd41b | 11234 | return fold_build2_loc (loc, RDIV_EXPR, type, |
7ab7fd4f | 11235 | negate_expr (arg0), |
11236 | TREE_OPERAND (arg1, 0)); | |
fef10b60 | 11237 | |
11238 | /* In IEEE floating point, x/1 is not equivalent to x for snans. */ | |
11239 | if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0))) | |
11240 | && real_onep (arg1)) | |
389dd41b | 11241 | return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0)); |
fef10b60 | 11242 | |
11243 | /* In IEEE floating point, x/-1 is not equivalent to -x for snans. */ | |
11244 | if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0))) | |
11245 | && real_minus_onep (arg1)) | |
389dd41b | 11246 | return non_lvalue_loc (loc, fold_convert_loc (loc, type, |
11247 | negate_expr (arg0))); | |
fef10b60 | 11248 | |
11249 | /* If ARG1 is a constant, we can convert this to a multiply by the | |
11250 | reciprocal. This does not have the same rounding properties, | |
49d060d7 | 11251 | so only do this if -freciprocal-math. We can actually |
fef10b60 | 11252 | always safely do it if ARG1 is a power of two, but it's hard to |
11253 | tell if it is or not in a portable manner. */ | |
11254 | if (TREE_CODE (arg1) == REAL_CST) | |
11255 | { | |
49d060d7 | 11256 | if (flag_reciprocal_math |
fef10b60 | 11257 | && 0 != (tem = const_binop (code, build_real (type, dconst1), |
11258 | arg1, 0))) | |
389dd41b | 11259 | return fold_build2_loc (loc, MULT_EXPR, type, arg0, tem); |
fef10b60 | 11260 | /* Find the reciprocal if optimizing and the result is exact. */ |
11261 | if (optimize) | |
11262 | { | |
11263 | REAL_VALUE_TYPE r; | |
11264 | r = TREE_REAL_CST (arg1); | |
11265 | if (exact_real_inverse (TYPE_MODE(TREE_TYPE(arg0)), &r)) | |
11266 | { | |
11267 | tem = build_real (type, r); | |
389dd41b | 11268 | return fold_build2_loc (loc, MULT_EXPR, type, |
11269 | fold_convert_loc (loc, type, arg0), tem); | |
fef10b60 | 11270 | } |
11271 | } | |
11272 | } | |
48e1416a | 11273 | /* Convert A/B/C to A/(B*C). */ |
49d060d7 | 11274 | if (flag_reciprocal_math |
fef10b60 | 11275 | && TREE_CODE (arg0) == RDIV_EXPR) |
389dd41b | 11276 | return fold_build2_loc (loc, RDIV_EXPR, type, TREE_OPERAND (arg0, 0), |
11277 | fold_build2_loc (loc, MULT_EXPR, type, | |
7ab7fd4f | 11278 | TREE_OPERAND (arg0, 1), arg1)); |
fef10b60 | 11279 | |
11280 | /* Convert A/(B/C) to (A/B)*C. */ | |
49d060d7 | 11281 | if (flag_reciprocal_math |
fef10b60 | 11282 | && TREE_CODE (arg1) == RDIV_EXPR) |
389dd41b | 11283 | return fold_build2_loc (loc, MULT_EXPR, type, |
11284 | fold_build2_loc (loc, RDIV_EXPR, type, arg0, | |
7ab7fd4f | 11285 | TREE_OPERAND (arg1, 0)), |
11286 | TREE_OPERAND (arg1, 1)); | |
fef10b60 | 11287 | |
11288 | /* Convert C1/(X*C2) into (C1/C2)/X. */ | |
49d060d7 | 11289 | if (flag_reciprocal_math |
fef10b60 | 11290 | && TREE_CODE (arg1) == MULT_EXPR |
11291 | && TREE_CODE (arg0) == REAL_CST | |
11292 | && TREE_CODE (TREE_OPERAND (arg1, 1)) == REAL_CST) | |
11293 | { | |
11294 | tree tem = const_binop (RDIV_EXPR, arg0, | |
11295 | TREE_OPERAND (arg1, 1), 0); | |
11296 | if (tem) | |
389dd41b | 11297 | return fold_build2_loc (loc, RDIV_EXPR, type, tem, |
7ab7fd4f | 11298 | TREE_OPERAND (arg1, 0)); |
fef10b60 | 11299 | } |
11300 | ||
fef10b60 | 11301 | if (flag_unsafe_math_optimizations) |
11302 | { | |
11303 | enum built_in_function fcode0 = builtin_mathfn_code (arg0); | |
11304 | enum built_in_function fcode1 = builtin_mathfn_code (arg1); | |
11305 | ||
11306 | /* Optimize sin(x)/cos(x) as tan(x). */ | |
11307 | if (((fcode0 == BUILT_IN_SIN && fcode1 == BUILT_IN_COS) | |
11308 | || (fcode0 == BUILT_IN_SINF && fcode1 == BUILT_IN_COSF) | |
11309 | || (fcode0 == BUILT_IN_SINL && fcode1 == BUILT_IN_COSL)) | |
c2f47e15 | 11310 | && operand_equal_p (CALL_EXPR_ARG (arg0, 0), |
11311 | CALL_EXPR_ARG (arg1, 0), 0)) | |
fef10b60 | 11312 | { |
11313 | tree tanfn = mathfn_built_in (type, BUILT_IN_TAN); | |
11314 | ||
11315 | if (tanfn != NULL_TREE) | |
389dd41b | 11316 | return build_call_expr_loc (loc, tanfn, 1, CALL_EXPR_ARG (arg0, 0)); |
fef10b60 | 11317 | } |
11318 | ||
11319 | /* Optimize cos(x)/sin(x) as 1.0/tan(x). */ | |
11320 | if (((fcode0 == BUILT_IN_COS && fcode1 == BUILT_IN_SIN) | |
11321 | || (fcode0 == BUILT_IN_COSF && fcode1 == BUILT_IN_SINF) | |
11322 | || (fcode0 == BUILT_IN_COSL && fcode1 == BUILT_IN_SINL)) | |
c2f47e15 | 11323 | && operand_equal_p (CALL_EXPR_ARG (arg0, 0), |
11324 | CALL_EXPR_ARG (arg1, 0), 0)) | |
fef10b60 | 11325 | { |
11326 | tree tanfn = mathfn_built_in (type, BUILT_IN_TAN); | |
11327 | ||
11328 | if (tanfn != NULL_TREE) | |
11329 | { | |
389dd41b | 11330 | tree tmp = build_call_expr_loc (loc, tanfn, 1, |
11331 | CALL_EXPR_ARG (arg0, 0)); | |
11332 | return fold_build2_loc (loc, RDIV_EXPR, type, | |
7ab7fd4f | 11333 | build_real (type, dconst1), tmp); |
fef10b60 | 11334 | } |
11335 | } | |
11336 | ||
29f9683a | 11337 | /* Optimize sin(x)/tan(x) as cos(x) if we don't care about |
11338 | NaNs or Infinities. */ | |
11339 | if (((fcode0 == BUILT_IN_SIN && fcode1 == BUILT_IN_TAN) | |
11340 | || (fcode0 == BUILT_IN_SINF && fcode1 == BUILT_IN_TANF) | |
11341 | || (fcode0 == BUILT_IN_SINL && fcode1 == BUILT_IN_TANL))) | |
11342 | { | |
c2f47e15 | 11343 | tree arg00 = CALL_EXPR_ARG (arg0, 0); |
11344 | tree arg01 = CALL_EXPR_ARG (arg1, 0); | |
29f9683a | 11345 | |
11346 | if (! HONOR_NANS (TYPE_MODE (TREE_TYPE (arg00))) | |
11347 | && ! HONOR_INFINITIES (TYPE_MODE (TREE_TYPE (arg00))) | |
11348 | && operand_equal_p (arg00, arg01, 0)) | |
11349 | { | |
11350 | tree cosfn = mathfn_built_in (type, BUILT_IN_COS); | |
11351 | ||
11352 | if (cosfn != NULL_TREE) | |
389dd41b | 11353 | return build_call_expr_loc (loc, cosfn, 1, arg00); |
29f9683a | 11354 | } |
11355 | } | |
11356 | ||
11357 | /* Optimize tan(x)/sin(x) as 1.0/cos(x) if we don't care about | |
191ec5a2 | 11358 | NaNs or Infinities. */ |
29f9683a | 11359 | if (((fcode0 == BUILT_IN_TAN && fcode1 == BUILT_IN_SIN) |
11360 | || (fcode0 == BUILT_IN_TANF && fcode1 == BUILT_IN_SINF) | |
11361 | || (fcode0 == BUILT_IN_TANL && fcode1 == BUILT_IN_SINL))) | |
11362 | { | |
c2f47e15 | 11363 | tree arg00 = CALL_EXPR_ARG (arg0, 0); |
11364 | tree arg01 = CALL_EXPR_ARG (arg1, 0); | |
29f9683a | 11365 | |
11366 | if (! HONOR_NANS (TYPE_MODE (TREE_TYPE (arg00))) | |
11367 | && ! HONOR_INFINITIES (TYPE_MODE (TREE_TYPE (arg00))) | |
11368 | && operand_equal_p (arg00, arg01, 0)) | |
11369 | { | |
11370 | tree cosfn = mathfn_built_in (type, BUILT_IN_COS); | |
11371 | ||
11372 | if (cosfn != NULL_TREE) | |
11373 | { | |
389dd41b | 11374 | tree tmp = build_call_expr_loc (loc, cosfn, 1, arg00); |
11375 | return fold_build2_loc (loc, RDIV_EXPR, type, | |
29f9683a | 11376 | build_real (type, dconst1), |
f9c71cdf | 11377 | tmp); |
29f9683a | 11378 | } |
11379 | } | |
11380 | } | |
11381 | ||
fef10b60 | 11382 | /* Optimize pow(x,c)/x as pow(x,c-1). */ |
11383 | if (fcode0 == BUILT_IN_POW | |
11384 | || fcode0 == BUILT_IN_POWF | |
11385 | || fcode0 == BUILT_IN_POWL) | |
11386 | { | |
c2f47e15 | 11387 | tree arg00 = CALL_EXPR_ARG (arg0, 0); |
11388 | tree arg01 = CALL_EXPR_ARG (arg0, 1); | |
fef10b60 | 11389 | if (TREE_CODE (arg01) == REAL_CST |
f96bd2bf | 11390 | && !TREE_OVERFLOW (arg01) |
fef10b60 | 11391 | && operand_equal_p (arg1, arg00, 0)) |
11392 | { | |
c2f47e15 | 11393 | tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0); |
fef10b60 | 11394 | REAL_VALUE_TYPE c; |
c2f47e15 | 11395 | tree arg; |
fef10b60 | 11396 | |
11397 | c = TREE_REAL_CST (arg01); | |
11398 | real_arithmetic (&c, MINUS_EXPR, &c, &dconst1); | |
11399 | arg = build_real (type, c); | |
389dd41b | 11400 | return build_call_expr_loc (loc, powfn, 2, arg1, arg); |
fef10b60 | 11401 | } |
11402 | } | |
29f9683a | 11403 | |
a307ebdf | 11404 | /* Optimize a/root(b/c) into a*root(c/b). */ |
11405 | if (BUILTIN_ROOT_P (fcode1)) | |
93ad369f | 11406 | { |
11407 | tree rootarg = CALL_EXPR_ARG (arg1, 0); | |
11408 | ||
11409 | if (TREE_CODE (rootarg) == RDIV_EXPR) | |
11410 | { | |
11411 | tree rootfn = TREE_OPERAND (CALL_EXPR_FN (arg1), 0); | |
11412 | tree b = TREE_OPERAND (rootarg, 0); | |
11413 | tree c = TREE_OPERAND (rootarg, 1); | |
11414 | ||
389dd41b | 11415 | tree tmp = fold_build2_loc (loc, RDIV_EXPR, type, c, b); |
93ad369f | 11416 | |
389dd41b | 11417 | tmp = build_call_expr_loc (loc, rootfn, 1, tmp); |
11418 | return fold_build2_loc (loc, MULT_EXPR, type, arg0, tmp); | |
93ad369f | 11419 | } |
11420 | } | |
11421 | ||
29f9683a | 11422 | /* Optimize x/expN(y) into x*expN(-y). */ |
11423 | if (BUILTIN_EXPONENT_P (fcode1)) | |
11424 | { | |
c2f47e15 | 11425 | tree expfn = TREE_OPERAND (CALL_EXPR_FN (arg1), 0); |
11426 | tree arg = negate_expr (CALL_EXPR_ARG (arg1, 0)); | |
389dd41b | 11427 | arg1 = build_call_expr_loc (loc, |
11428 | expfn, 1, | |
11429 | fold_convert_loc (loc, type, arg)); | |
11430 | return fold_build2_loc (loc, MULT_EXPR, type, arg0, arg1); | |
29f9683a | 11431 | } |
11432 | ||
11433 | /* Optimize x/pow(y,z) into x*pow(y,-z). */ | |
11434 | if (fcode1 == BUILT_IN_POW | |
11435 | || fcode1 == BUILT_IN_POWF | |
11436 | || fcode1 == BUILT_IN_POWL) | |
11437 | { | |
c2f47e15 | 11438 | tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg1), 0); |
11439 | tree arg10 = CALL_EXPR_ARG (arg1, 0); | |
11440 | tree arg11 = CALL_EXPR_ARG (arg1, 1); | |
389dd41b | 11441 | tree neg11 = fold_convert_loc (loc, type, |
11442 | negate_expr (arg11)); | |
11443 | arg1 = build_call_expr_loc (loc, powfn, 2, arg10, neg11); | |
11444 | return fold_build2_loc (loc, MULT_EXPR, type, arg0, arg1); | |
29f9683a | 11445 | } |
fef10b60 | 11446 | } |
32cef1cc | 11447 | return NULL_TREE; |
fef10b60 | 11448 | |
11449 | case TRUNC_DIV_EXPR: | |
fef10b60 | 11450 | case FLOOR_DIV_EXPR: |
8af0edf2 | 11451 | /* Simplify A / (B << N) where A and B are positive and B is |
11452 | a power of 2, to A >> (N + log2(B)). */ | |
add6ee5e | 11453 | strict_overflow_p = false; |
8af0edf2 | 11454 | if (TREE_CODE (arg1) == LSHIFT_EXPR |
add6ee5e | 11455 | && (TYPE_UNSIGNED (type) |
3778af05 | 11456 | || tree_expr_nonnegative_warnv_p (op0, &strict_overflow_p))) |
8af0edf2 | 11457 | { |
11458 | tree sval = TREE_OPERAND (arg1, 0); | |
11459 | if (integer_pow2p (sval) && tree_int_cst_sgn (sval) > 0) | |
11460 | { | |
11461 | tree sh_cnt = TREE_OPERAND (arg1, 1); | |
11462 | unsigned long pow2 = exact_log2 (TREE_INT_CST_LOW (sval)); | |
11463 | ||
add6ee5e | 11464 | if (strict_overflow_p) |
11465 | fold_overflow_warning (("assuming signed overflow does not " | |
11466 | "occur when simplifying A / (B << N)"), | |
11467 | WARN_STRICT_OVERFLOW_MISC); | |
11468 | ||
389dd41b | 11469 | sh_cnt = fold_build2_loc (loc, PLUS_EXPR, TREE_TYPE (sh_cnt), |
8af0edf2 | 11470 | sh_cnt, build_int_cst (NULL_TREE, pow2)); |
389dd41b | 11471 | return fold_build2_loc (loc, RSHIFT_EXPR, type, |
11472 | fold_convert_loc (loc, type, arg0), sh_cnt); | |
8af0edf2 | 11473 | } |
11474 | } | |
d997554f | 11475 | |
11476 | /* For unsigned integral types, FLOOR_DIV_EXPR is the same as | |
11477 | TRUNC_DIV_EXPR. Rewrite into the latter in this case. */ | |
11478 | if (INTEGRAL_TYPE_P (type) | |
11479 | && TYPE_UNSIGNED (type) | |
11480 | && code == FLOOR_DIV_EXPR) | |
389dd41b | 11481 | return fold_build2_loc (loc, TRUNC_DIV_EXPR, type, op0, op1); |
d997554f | 11482 | |
8af0edf2 | 11483 | /* Fall thru */ |
11484 | ||
11485 | case ROUND_DIV_EXPR: | |
fef10b60 | 11486 | case CEIL_DIV_EXPR: |
11487 | case EXACT_DIV_EXPR: | |
11488 | if (integer_onep (arg1)) | |
389dd41b | 11489 | return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0)); |
fef10b60 | 11490 | if (integer_zerop (arg1)) |
e7edfbbd | 11491 | return NULL_TREE; |
fef10b60 | 11492 | /* X / -1 is -X. */ |
11493 | if (!TYPE_UNSIGNED (type) | |
11494 | && TREE_CODE (arg1) == INTEGER_CST | |
11495 | && TREE_INT_CST_LOW (arg1) == (unsigned HOST_WIDE_INT) -1 | |
11496 | && TREE_INT_CST_HIGH (arg1) == -1) | |
389dd41b | 11497 | return fold_convert_loc (loc, type, negate_expr (arg0)); |
fef10b60 | 11498 | |
4df6b6e5 | 11499 | /* Convert -A / -B to A / B when the type is signed and overflow is |
11500 | undefined. */ | |
981eb798 | 11501 | if ((!INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_UNDEFINED (type)) |
4df6b6e5 | 11502 | && TREE_CODE (arg0) == NEGATE_EXPR |
11503 | && negate_expr_p (arg1)) | |
add6ee5e | 11504 | { |
11505 | if (INTEGRAL_TYPE_P (type)) | |
11506 | fold_overflow_warning (("assuming signed overflow does not occur " | |
11507 | "when distributing negation across " | |
11508 | "division"), | |
11509 | WARN_STRICT_OVERFLOW_MISC); | |
389dd41b | 11510 | return fold_build2_loc (loc, code, type, |
11511 | fold_convert_loc (loc, type, | |
11512 | TREE_OPERAND (arg0, 0)), | |
11513 | fold_convert_loc (loc, type, | |
11514 | negate_expr (arg1))); | |
add6ee5e | 11515 | } |
981eb798 | 11516 | if ((!INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_UNDEFINED (type)) |
4df6b6e5 | 11517 | && TREE_CODE (arg1) == NEGATE_EXPR |
11518 | && negate_expr_p (arg0)) | |
add6ee5e | 11519 | { |
11520 | if (INTEGRAL_TYPE_P (type)) | |
11521 | fold_overflow_warning (("assuming signed overflow does not occur " | |
11522 | "when distributing negation across " | |
11523 | "division"), | |
11524 | WARN_STRICT_OVERFLOW_MISC); | |
389dd41b | 11525 | return fold_build2_loc (loc, code, type, |
11526 | fold_convert_loc (loc, type, | |
11527 | negate_expr (arg0)), | |
11528 | fold_convert_loc (loc, type, | |
11529 | TREE_OPERAND (arg1, 0))); | |
add6ee5e | 11530 | } |
4df6b6e5 | 11531 | |
fef10b60 | 11532 | /* If arg0 is a multiple of arg1, then rewrite to the fastest div |
11533 | operation, EXACT_DIV_EXPR. | |
11534 | ||
11535 | Note that only CEIL_DIV_EXPR and FLOOR_DIV_EXPR are rewritten now. | |
11536 | At one time others generated faster code, it's not clear if they do | |
11537 | after the last round to changes to the DIV code in expmed.c. */ | |
11538 | if ((code == CEIL_DIV_EXPR || code == FLOOR_DIV_EXPR) | |
11539 | && multiple_of_p (type, arg0, arg1)) | |
389dd41b | 11540 | return fold_build2_loc (loc, EXACT_DIV_EXPR, type, arg0, arg1); |
fef10b60 | 11541 | |
add6ee5e | 11542 | strict_overflow_p = false; |
fef10b60 | 11543 | if (TREE_CODE (arg1) == INTEGER_CST |
add6ee5e | 11544 | && 0 != (tem = extract_muldiv (op0, arg1, code, NULL_TREE, |
11545 | &strict_overflow_p))) | |
11546 | { | |
11547 | if (strict_overflow_p) | |
11548 | fold_overflow_warning (("assuming signed overflow does not occur " | |
11549 | "when simplifying division"), | |
11550 | WARN_STRICT_OVERFLOW_MISC); | |
389dd41b | 11551 | return fold_convert_loc (loc, type, tem); |
add6ee5e | 11552 | } |
fef10b60 | 11553 | |
32cef1cc | 11554 | return NULL_TREE; |
fef10b60 | 11555 | |
11556 | case CEIL_MOD_EXPR: | |
11557 | case FLOOR_MOD_EXPR: | |
11558 | case ROUND_MOD_EXPR: | |
11559 | case TRUNC_MOD_EXPR: | |
11560 | /* X % 1 is always zero, but be sure to preserve any side | |
11561 | effects in X. */ | |
11562 | if (integer_onep (arg1)) | |
389dd41b | 11563 | return omit_one_operand_loc (loc, type, integer_zero_node, arg0); |
fef10b60 | 11564 | |
11565 | /* X % 0, return X % 0 unchanged so that we can get the | |
11566 | proper warnings and errors. */ | |
11567 | if (integer_zerop (arg1)) | |
e7edfbbd | 11568 | return NULL_TREE; |
fef10b60 | 11569 | |
11570 | /* 0 % X is always zero, but be sure to preserve any side | |
11571 | effects in X. Place this after checking for X == 0. */ | |
11572 | if (integer_zerop (arg0)) | |
389dd41b | 11573 | return omit_one_operand_loc (loc, type, integer_zero_node, arg1); |
fef10b60 | 11574 | |
11575 | /* X % -1 is zero. */ | |
11576 | if (!TYPE_UNSIGNED (type) | |
11577 | && TREE_CODE (arg1) == INTEGER_CST | |
11578 | && TREE_INT_CST_LOW (arg1) == (unsigned HOST_WIDE_INT) -1 | |
11579 | && TREE_INT_CST_HIGH (arg1) == -1) | |
389dd41b | 11580 | return omit_one_operand_loc (loc, type, integer_zero_node, arg0); |
fef10b60 | 11581 | |
fef10b60 | 11582 | /* X % -C is the same as X % C. */ |
11583 | if (code == TRUNC_MOD_EXPR | |
11584 | && !TYPE_UNSIGNED (type) | |
11585 | && TREE_CODE (arg1) == INTEGER_CST | |
f96bd2bf | 11586 | && !TREE_OVERFLOW (arg1) |
fef10b60 | 11587 | && TREE_INT_CST_HIGH (arg1) < 0 |
981eb798 | 11588 | && !TYPE_OVERFLOW_TRAPS (type) |
fef10b60 | 11589 | /* Avoid this transformation if C is INT_MIN, i.e. C == -C. */ |
11590 | && !sign_bit_p (arg1, arg1)) | |
389dd41b | 11591 | return fold_build2_loc (loc, code, type, |
11592 | fold_convert_loc (loc, type, arg0), | |
11593 | fold_convert_loc (loc, type, | |
11594 | negate_expr (arg1))); | |
fef10b60 | 11595 | |
11596 | /* X % -Y is the same as X % Y. */ | |
11597 | if (code == TRUNC_MOD_EXPR | |
11598 | && !TYPE_UNSIGNED (type) | |
11599 | && TREE_CODE (arg1) == NEGATE_EXPR | |
981eb798 | 11600 | && !TYPE_OVERFLOW_TRAPS (type)) |
389dd41b | 11601 | return fold_build2_loc (loc, code, type, fold_convert_loc (loc, type, arg0), |
11602 | fold_convert_loc (loc, type, | |
11603 | TREE_OPERAND (arg1, 0))); | |
fef10b60 | 11604 | |
c6feb9f1 | 11605 | strict_overflow_p = false; |
fef10b60 | 11606 | if (TREE_CODE (arg1) == INTEGER_CST |
add6ee5e | 11607 | && 0 != (tem = extract_muldiv (op0, arg1, code, NULL_TREE, |
11608 | &strict_overflow_p))) | |
11609 | { | |
11610 | if (strict_overflow_p) | |
11611 | fold_overflow_warning (("assuming signed overflow does not occur " | |
f0b5f617 | 11612 | "when simplifying modulus"), |
add6ee5e | 11613 | WARN_STRICT_OVERFLOW_MISC); |
389dd41b | 11614 | return fold_convert_loc (loc, type, tem); |
add6ee5e | 11615 | } |
fef10b60 | 11616 | |
c6feb9f1 | 11617 | /* Optimize TRUNC_MOD_EXPR by a power of two into a BIT_AND_EXPR, |
11618 | i.e. "X % C" into "X & (C - 1)", if X and C are positive. */ | |
11619 | if ((code == TRUNC_MOD_EXPR || code == FLOOR_MOD_EXPR) | |
11620 | && (TYPE_UNSIGNED (type) | |
11621 | || tree_expr_nonnegative_warnv_p (op0, &strict_overflow_p))) | |
11622 | { | |
11623 | tree c = arg1; | |
11624 | /* Also optimize A % (C << N) where C is a power of 2, | |
11625 | to A & ((C << N) - 1). */ | |
11626 | if (TREE_CODE (arg1) == LSHIFT_EXPR) | |
11627 | c = TREE_OPERAND (arg1, 0); | |
11628 | ||
11629 | if (integer_pow2p (c) && tree_int_cst_sgn (c) > 0) | |
11630 | { | |
11631 | tree mask | |
11632 | = fold_build2_loc (loc, MINUS_EXPR, TREE_TYPE (arg1), arg1, | |
11633 | build_int_cst (TREE_TYPE (arg1), 1)); | |
11634 | if (strict_overflow_p) | |
11635 | fold_overflow_warning (("assuming signed overflow does not " | |
11636 | "occur when simplifying " | |
11637 | "X % (power of two)"), | |
11638 | WARN_STRICT_OVERFLOW_MISC); | |
11639 | return fold_build2_loc (loc, BIT_AND_EXPR, type, | |
11640 | fold_convert_loc (loc, type, arg0), | |
11641 | fold_convert_loc (loc, type, mask)); | |
11642 | } | |
11643 | } | |
11644 | ||
32cef1cc | 11645 | return NULL_TREE; |
fef10b60 | 11646 | |
11647 | case LROTATE_EXPR: | |
11648 | case RROTATE_EXPR: | |
11649 | if (integer_all_onesp (arg0)) | |
389dd41b | 11650 | return omit_one_operand_loc (loc, type, arg0, arg1); |
fef10b60 | 11651 | goto shift; |
11652 | ||
11653 | case RSHIFT_EXPR: | |
11654 | /* Optimize -1 >> x for arithmetic right shifts. */ | |
53cfcf36 | 11655 | if (integer_all_onesp (arg0) && !TYPE_UNSIGNED (type) |
11656 | && tree_expr_nonnegative_p (arg1)) | |
389dd41b | 11657 | return omit_one_operand_loc (loc, type, arg0, arg1); |
fef10b60 | 11658 | /* ... fall through ... */ |
11659 | ||
11660 | case LSHIFT_EXPR: | |
11661 | shift: | |
11662 | if (integer_zerop (arg1)) | |
389dd41b | 11663 | return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0)); |
fef10b60 | 11664 | if (integer_zerop (arg0)) |
389dd41b | 11665 | return omit_one_operand_loc (loc, type, arg0, arg1); |
fef10b60 | 11666 | |
11667 | /* Since negative shift count is not well-defined, | |
11668 | don't try to compute it in the compiler. */ | |
11669 | if (TREE_CODE (arg1) == INTEGER_CST && tree_int_cst_sgn (arg1) < 0) | |
e7edfbbd | 11670 | return NULL_TREE; |
d810c37d | 11671 | |
11672 | /* Turn (a OP c1) OP c2 into a OP (c1+c2). */ | |
2b2c6e11 | 11673 | if (TREE_CODE (op0) == code && host_integerp (arg1, false) |
d810c37d | 11674 | && TREE_INT_CST_LOW (arg1) < TYPE_PRECISION (type) |
11675 | && host_integerp (TREE_OPERAND (arg0, 1), false) | |
11676 | && TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1)) < TYPE_PRECISION (type)) | |
11677 | { | |
11678 | HOST_WIDE_INT low = (TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1)) | |
11679 | + TREE_INT_CST_LOW (arg1)); | |
11680 | ||
11681 | /* Deal with a OP (c1 + c2) being undefined but (a OP c1) OP c2 | |
11682 | being well defined. */ | |
11683 | if (low >= TYPE_PRECISION (type)) | |
11684 | { | |
11685 | if (code == LROTATE_EXPR || code == RROTATE_EXPR) | |
11686 | low = low % TYPE_PRECISION (type); | |
11687 | else if (TYPE_UNSIGNED (type) || code == LSHIFT_EXPR) | |
389dd41b | 11688 | return omit_one_operand_loc (loc, type, build_int_cst (type, 0), |
4377e55f | 11689 | TREE_OPERAND (arg0, 0)); |
d810c37d | 11690 | else |
11691 | low = TYPE_PRECISION (type) - 1; | |
11692 | } | |
11693 | ||
389dd41b | 11694 | return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0), |
d810c37d | 11695 | build_int_cst (type, low)); |
11696 | } | |
11697 | ||
0e7e788d | 11698 | /* Transform (x >> c) << c into x & (-1<<c), or transform (x << c) >> c |
11699 | into x & ((unsigned)-1 >> c) for unsigned types. */ | |
11700 | if (((code == LSHIFT_EXPR && TREE_CODE (arg0) == RSHIFT_EXPR) | |
11701 | || (TYPE_UNSIGNED (type) | |
11702 | && code == RSHIFT_EXPR && TREE_CODE (arg0) == LSHIFT_EXPR)) | |
d810c37d | 11703 | && host_integerp (arg1, false) |
11704 | && TREE_INT_CST_LOW (arg1) < TYPE_PRECISION (type) | |
11705 | && host_integerp (TREE_OPERAND (arg0, 1), false) | |
11706 | && TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1)) < TYPE_PRECISION (type)) | |
11707 | { | |
11708 | HOST_WIDE_INT low0 = TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1)); | |
11709 | HOST_WIDE_INT low1 = TREE_INT_CST_LOW (arg1); | |
d810c37d | 11710 | tree lshift; |
11711 | tree arg00; | |
11712 | ||
11713 | if (low0 == low1) | |
11714 | { | |
389dd41b | 11715 | arg00 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0)); |
d810c37d | 11716 | |
0e7e788d | 11717 | lshift = build_int_cst (type, -1); |
11718 | lshift = int_const_binop (code, lshift, arg1, 0); | |
d810c37d | 11719 | |
389dd41b | 11720 | return fold_build2_loc (loc, BIT_AND_EXPR, type, arg00, lshift); |
d810c37d | 11721 | } |
11722 | } | |
11723 | ||
fef10b60 | 11724 | /* Rewrite an LROTATE_EXPR by a constant into an |
11725 | RROTATE_EXPR by a new constant. */ | |
11726 | if (code == LROTATE_EXPR && TREE_CODE (arg1) == INTEGER_CST) | |
11727 | { | |
2455d3ef | 11728 | tree tem = build_int_cst (TREE_TYPE (arg1), |
6295ca72 | 11729 | TYPE_PRECISION (type)); |
fef10b60 | 11730 | tem = const_binop (MINUS_EXPR, tem, arg1, 0); |
389dd41b | 11731 | return fold_build2_loc (loc, RROTATE_EXPR, type, op0, tem); |
fef10b60 | 11732 | } |
11733 | ||
11734 | /* If we have a rotate of a bit operation with the rotate count and | |
11735 | the second operand of the bit operation both constant, | |
11736 | permute the two operations. */ | |
11737 | if (code == RROTATE_EXPR && TREE_CODE (arg1) == INTEGER_CST | |
11738 | && (TREE_CODE (arg0) == BIT_AND_EXPR | |
11739 | || TREE_CODE (arg0) == BIT_IOR_EXPR | |
11740 | || TREE_CODE (arg0) == BIT_XOR_EXPR) | |
11741 | && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST) | |
389dd41b | 11742 | return fold_build2_loc (loc, TREE_CODE (arg0), type, |
11743 | fold_build2_loc (loc, code, type, | |
7ab7fd4f | 11744 | TREE_OPERAND (arg0, 0), arg1), |
389dd41b | 11745 | fold_build2_loc (loc, code, type, |
7ab7fd4f | 11746 | TREE_OPERAND (arg0, 1), arg1)); |
fef10b60 | 11747 | |
6295ca72 | 11748 | /* Two consecutive rotates adding up to the precision of the |
11749 | type can be ignored. */ | |
fef10b60 | 11750 | if (code == RROTATE_EXPR && TREE_CODE (arg1) == INTEGER_CST |
11751 | && TREE_CODE (arg0) == RROTATE_EXPR | |
11752 | && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST | |
11753 | && TREE_INT_CST_HIGH (arg1) == 0 | |
11754 | && TREE_INT_CST_HIGH (TREE_OPERAND (arg0, 1)) == 0 | |
11755 | && ((TREE_INT_CST_LOW (arg1) | |
11756 | + TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1))) | |
6295ca72 | 11757 | == (unsigned int) TYPE_PRECISION (type))) |
fef10b60 | 11758 | return TREE_OPERAND (arg0, 0); |
11759 | ||
2cf28ced | 11760 | /* Fold (X & C2) << C1 into (X << C1) & (C2 << C1) |
11761 | (X & C2) >> C1 into (X >> C1) & (C2 >> C1) | |
11762 | if the latter can be further optimized. */ | |
11763 | if ((code == LSHIFT_EXPR || code == RSHIFT_EXPR) | |
11764 | && TREE_CODE (arg0) == BIT_AND_EXPR | |
11765 | && TREE_CODE (arg1) == INTEGER_CST | |
11766 | && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST) | |
11767 | { | |
389dd41b | 11768 | tree mask = fold_build2_loc (loc, code, type, |
11769 | fold_convert_loc (loc, type, | |
11770 | TREE_OPERAND (arg0, 1)), | |
2cf28ced | 11771 | arg1); |
389dd41b | 11772 | tree shift = fold_build2_loc (loc, code, type, |
11773 | fold_convert_loc (loc, type, | |
11774 | TREE_OPERAND (arg0, 0)), | |
2cf28ced | 11775 | arg1); |
389dd41b | 11776 | tem = fold_binary_loc (loc, BIT_AND_EXPR, type, shift, mask); |
2cf28ced | 11777 | if (tem) |
11778 | return tem; | |
11779 | } | |
11780 | ||
32cef1cc | 11781 | return NULL_TREE; |
fef10b60 | 11782 | |
11783 | case MIN_EXPR: | |
11784 | if (operand_equal_p (arg0, arg1, 0)) | |
389dd41b | 11785 | return omit_one_operand_loc (loc, type, arg0, arg1); |
fef10b60 | 11786 | if (INTEGRAL_TYPE_P (type) |
11787 | && operand_equal_p (arg1, TYPE_MIN_VALUE (type), OEP_ONLY_CONST)) | |
389dd41b | 11788 | return omit_one_operand_loc (loc, type, arg1, arg0); |
11789 | tem = fold_minmax (loc, MIN_EXPR, type, arg0, arg1); | |
7e50ecae | 11790 | if (tem) |
11791 | return tem; | |
fef10b60 | 11792 | goto associate; |
11793 | ||
11794 | case MAX_EXPR: | |
11795 | if (operand_equal_p (arg0, arg1, 0)) | |
389dd41b | 11796 | return omit_one_operand_loc (loc, type, arg0, arg1); |
fef10b60 | 11797 | if (INTEGRAL_TYPE_P (type) |
11798 | && TYPE_MAX_VALUE (type) | |
11799 | && operand_equal_p (arg1, TYPE_MAX_VALUE (type), OEP_ONLY_CONST)) | |
389dd41b | 11800 | return omit_one_operand_loc (loc, type, arg1, arg0); |
11801 | tem = fold_minmax (loc, MAX_EXPR, type, arg0, arg1); | |
7e50ecae | 11802 | if (tem) |
11803 | return tem; | |
fef10b60 | 11804 | goto associate; |
11805 | ||
11806 | case TRUTH_ANDIF_EXPR: | |
11807 | /* Note that the operands of this must be ints | |
11808 | and their values must be 0 or 1. | |
11809 | ("true" is a fixed value perhaps depending on the language.) */ | |
11810 | /* If first arg is constant zero, return it. */ | |
11811 | if (integer_zerop (arg0)) | |
389dd41b | 11812 | return fold_convert_loc (loc, type, arg0); |
fef10b60 | 11813 | case TRUTH_AND_EXPR: |
11814 | /* If either arg is constant true, drop it. */ | |
11815 | if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0)) | |
389dd41b | 11816 | return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg1)); |
fef10b60 | 11817 | if (TREE_CODE (arg1) == INTEGER_CST && ! integer_zerop (arg1) |
11818 | /* Preserve sequence points. */ | |
11819 | && (code != TRUTH_ANDIF_EXPR || ! TREE_SIDE_EFFECTS (arg0))) | |
389dd41b | 11820 | return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0)); |
fef10b60 | 11821 | /* If second arg is constant zero, result is zero, but first arg |
11822 | must be evaluated. */ | |
11823 | if (integer_zerop (arg1)) | |
389dd41b | 11824 | return omit_one_operand_loc (loc, type, arg1, arg0); |
fef10b60 | 11825 | /* Likewise for first arg, but note that only the TRUTH_AND_EXPR |
11826 | case will be handled here. */ | |
11827 | if (integer_zerop (arg0)) | |
389dd41b | 11828 | return omit_one_operand_loc (loc, type, arg0, arg1); |
fef10b60 | 11829 | |
11830 | /* !X && X is always false. */ | |
11831 | if (TREE_CODE (arg0) == TRUTH_NOT_EXPR | |
11832 | && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)) | |
389dd41b | 11833 | return omit_one_operand_loc (loc, type, integer_zero_node, arg1); |
fef10b60 | 11834 | /* X && !X is always false. */ |
11835 | if (TREE_CODE (arg1) == TRUTH_NOT_EXPR | |
11836 | && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0)) | |
389dd41b | 11837 | return omit_one_operand_loc (loc, type, integer_zero_node, arg0); |
fef10b60 | 11838 | |
11839 | /* A < X && A + 1 > Y ==> A < X && A >= Y. Normally A + 1 > Y | |
11840 | means A >= Y && A != MAX, but in this case we know that | |
11841 | A < X <= MAX. */ | |
11842 | ||
11843 | if (!TREE_SIDE_EFFECTS (arg0) | |
11844 | && !TREE_SIDE_EFFECTS (arg1)) | |
11845 | { | |
389dd41b | 11846 | tem = fold_to_nonsharp_ineq_using_bound (loc, arg0, arg1); |
c2352383 | 11847 | if (tem && !operand_equal_p (tem, arg0, 0)) |
389dd41b | 11848 | return fold_build2_loc (loc, code, type, tem, arg1); |
fef10b60 | 11849 | |
389dd41b | 11850 | tem = fold_to_nonsharp_ineq_using_bound (loc, arg1, arg0); |
c2352383 | 11851 | if (tem && !operand_equal_p (tem, arg1, 0)) |
389dd41b | 11852 | return fold_build2_loc (loc, code, type, arg0, tem); |
fef10b60 | 11853 | } |
11854 | ||
11855 | truth_andor: | |
11856 | /* We only do these simplifications if we are optimizing. */ | |
11857 | if (!optimize) | |
e7edfbbd | 11858 | return NULL_TREE; |
fef10b60 | 11859 | |
11860 | /* Check for things like (A || B) && (A || C). We can convert this | |
11861 | to A || (B && C). Note that either operator can be any of the four | |
11862 | truth and/or operations and the transformation will still be | |
11863 | valid. Also note that we only care about order for the | |
11864 | ANDIF and ORIF operators. If B contains side effects, this | |
11865 | might change the truth-value of A. */ | |
11866 | if (TREE_CODE (arg0) == TREE_CODE (arg1) | |
11867 | && (TREE_CODE (arg0) == TRUTH_ANDIF_EXPR | |
11868 | || TREE_CODE (arg0) == TRUTH_ORIF_EXPR | |
11869 | || TREE_CODE (arg0) == TRUTH_AND_EXPR | |
11870 | || TREE_CODE (arg0) == TRUTH_OR_EXPR) | |
11871 | && ! TREE_SIDE_EFFECTS (TREE_OPERAND (arg0, 1))) | |
11872 | { | |
11873 | tree a00 = TREE_OPERAND (arg0, 0); | |
11874 | tree a01 = TREE_OPERAND (arg0, 1); | |
11875 | tree a10 = TREE_OPERAND (arg1, 0); | |
11876 | tree a11 = TREE_OPERAND (arg1, 1); | |
11877 | int commutative = ((TREE_CODE (arg0) == TRUTH_OR_EXPR | |
11878 | || TREE_CODE (arg0) == TRUTH_AND_EXPR) | |
11879 | && (code == TRUTH_AND_EXPR | |
11880 | || code == TRUTH_OR_EXPR)); | |
11881 | ||
11882 | if (operand_equal_p (a00, a10, 0)) | |
389dd41b | 11883 | return fold_build2_loc (loc, TREE_CODE (arg0), type, a00, |
11884 | fold_build2_loc (loc, code, type, a01, a11)); | |
fef10b60 | 11885 | else if (commutative && operand_equal_p (a00, a11, 0)) |
389dd41b | 11886 | return fold_build2_loc (loc, TREE_CODE (arg0), type, a00, |
11887 | fold_build2_loc (loc, code, type, a01, a10)); | |
fef10b60 | 11888 | else if (commutative && operand_equal_p (a01, a10, 0)) |
389dd41b | 11889 | return fold_build2_loc (loc, TREE_CODE (arg0), type, a01, |
11890 | fold_build2_loc (loc, code, type, a00, a11)); | |
fef10b60 | 11891 | |
11892 | /* This case if tricky because we must either have commutative | |
11893 | operators or else A10 must not have side-effects. */ | |
11894 | ||
11895 | else if ((commutative || ! TREE_SIDE_EFFECTS (a10)) | |
11896 | && operand_equal_p (a01, a11, 0)) | |
389dd41b | 11897 | return fold_build2_loc (loc, TREE_CODE (arg0), type, |
11898 | fold_build2_loc (loc, code, type, a00, a10), | |
7ab7fd4f | 11899 | a01); |
fef10b60 | 11900 | } |
11901 | ||
11902 | /* See if we can build a range comparison. */ | |
389dd41b | 11903 | if (0 != (tem = fold_range_test (loc, code, type, op0, op1))) |
fef10b60 | 11904 | return tem; |
11905 | ||
e230978b | 11906 | if ((code == TRUTH_ANDIF_EXPR && TREE_CODE (arg0) == TRUTH_ORIF_EXPR) |
11907 | || (code == TRUTH_ORIF_EXPR && TREE_CODE (arg0) == TRUTH_ANDIF_EXPR)) | |
11908 | { | |
11909 | tem = merge_truthop_with_opposite_arm (loc, arg0, arg1, true); | |
11910 | if (tem) | |
11911 | return fold_build2_loc (loc, code, type, tem, arg1); | |
11912 | } | |
11913 | ||
11914 | if ((code == TRUTH_ANDIF_EXPR && TREE_CODE (arg1) == TRUTH_ORIF_EXPR) | |
11915 | || (code == TRUTH_ORIF_EXPR && TREE_CODE (arg1) == TRUTH_ANDIF_EXPR)) | |
11916 | { | |
11917 | tem = merge_truthop_with_opposite_arm (loc, arg1, arg0, false); | |
11918 | if (tem) | |
11919 | return fold_build2_loc (loc, code, type, arg0, tem); | |
11920 | } | |
11921 | ||
fef10b60 | 11922 | /* Check for the possibility of merging component references. If our |
11923 | lhs is another similar operation, try to merge its rhs with our | |
11924 | rhs. Then try to merge our lhs and rhs. */ | |
11925 | if (TREE_CODE (arg0) == code | |
389dd41b | 11926 | && 0 != (tem = fold_truthop (loc, code, type, |
fef10b60 | 11927 | TREE_OPERAND (arg0, 1), arg1))) |
389dd41b | 11928 | return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0), tem); |
fef10b60 | 11929 | |
389dd41b | 11930 | if ((tem = fold_truthop (loc, code, type, arg0, arg1)) != 0) |
fef10b60 | 11931 | return tem; |
11932 | ||
e7edfbbd | 11933 | return NULL_TREE; |
fef10b60 | 11934 | |
11935 | case TRUTH_ORIF_EXPR: | |
11936 | /* Note that the operands of this must be ints | |
11937 | and their values must be 0 or true. | |
11938 | ("true" is a fixed value perhaps depending on the language.) */ | |
11939 | /* If first arg is constant true, return it. */ | |
11940 | if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0)) | |
389dd41b | 11941 | return fold_convert_loc (loc, type, arg0); |
fef10b60 | 11942 | case TRUTH_OR_EXPR: |
11943 | /* If either arg is constant zero, drop it. */ | |
11944 | if (TREE_CODE (arg0) == INTEGER_CST && integer_zerop (arg0)) | |
389dd41b | 11945 | return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg1)); |
fef10b60 | 11946 | if (TREE_CODE (arg1) == INTEGER_CST && integer_zerop (arg1) |
11947 | /* Preserve sequence points. */ | |
11948 | && (code != TRUTH_ORIF_EXPR || ! TREE_SIDE_EFFECTS (arg0))) | |
389dd41b | 11949 | return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0)); |
fef10b60 | 11950 | /* If second arg is constant true, result is true, but we must |
11951 | evaluate first arg. */ | |
11952 | if (TREE_CODE (arg1) == INTEGER_CST && ! integer_zerop (arg1)) | |
389dd41b | 11953 | return omit_one_operand_loc (loc, type, arg1, arg0); |
fef10b60 | 11954 | /* Likewise for first arg, but note this only occurs here for |
11955 | TRUTH_OR_EXPR. */ | |
11956 | if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0)) | |
389dd41b | 11957 | return omit_one_operand_loc (loc, type, arg0, arg1); |
fef10b60 | 11958 | |
11959 | /* !X || X is always true. */ | |
11960 | if (TREE_CODE (arg0) == TRUTH_NOT_EXPR | |
11961 | && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)) | |
389dd41b | 11962 | return omit_one_operand_loc (loc, type, integer_one_node, arg1); |
fef10b60 | 11963 | /* X || !X is always true. */ |
11964 | if (TREE_CODE (arg1) == TRUTH_NOT_EXPR | |
11965 | && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0)) | |
389dd41b | 11966 | return omit_one_operand_loc (loc, type, integer_one_node, arg0); |
fef10b60 | 11967 | |
11968 | goto truth_andor; | |
11969 | ||
11970 | case TRUTH_XOR_EXPR: | |
11971 | /* If the second arg is constant zero, drop it. */ | |
11972 | if (integer_zerop (arg1)) | |
389dd41b | 11973 | return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0)); |
fef10b60 | 11974 | /* If the second arg is constant true, this is a logical inversion. */ |
11975 | if (integer_onep (arg1)) | |
ce04dcdc | 11976 | { |
11977 | /* Only call invert_truthvalue if operand is a truth value. */ | |
11978 | if (TREE_CODE (TREE_TYPE (arg0)) != BOOLEAN_TYPE) | |
389dd41b | 11979 | tem = fold_build1_loc (loc, TRUTH_NOT_EXPR, TREE_TYPE (arg0), arg0); |
ce04dcdc | 11980 | else |
389dd41b | 11981 | tem = invert_truthvalue_loc (loc, arg0); |
11982 | return non_lvalue_loc (loc, fold_convert_loc (loc, type, tem)); | |
ce04dcdc | 11983 | } |
fef10b60 | 11984 | /* Identical arguments cancel to zero. */ |
11985 | if (operand_equal_p (arg0, arg1, 0)) | |
389dd41b | 11986 | return omit_one_operand_loc (loc, type, integer_zero_node, arg0); |
fef10b60 | 11987 | |
11988 | /* !X ^ X is always true. */ | |
11989 | if (TREE_CODE (arg0) == TRUTH_NOT_EXPR | |
11990 | && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)) | |
389dd41b | 11991 | return omit_one_operand_loc (loc, type, integer_one_node, arg1); |
fef10b60 | 11992 | |
11993 | /* X ^ !X is always true. */ | |
11994 | if (TREE_CODE (arg1) == TRUTH_NOT_EXPR | |
11995 | && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0)) | |
389dd41b | 11996 | return omit_one_operand_loc (loc, type, integer_one_node, arg0); |
fef10b60 | 11997 | |
e7edfbbd | 11998 | return NULL_TREE; |
fef10b60 | 11999 | |
12000 | case EQ_EXPR: | |
12001 | case NE_EXPR: | |
389dd41b | 12002 | tem = fold_comparison (loc, code, type, op0, op1); |
6a451e87 | 12003 | if (tem != NULL_TREE) |
12004 | return tem; | |
f4a6516d | 12005 | |
66787d4f | 12006 | /* bool_var != 0 becomes bool_var. */ |
12007 | if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE && integer_zerop (arg1) | |
12008 | && code == NE_EXPR) | |
389dd41b | 12009 | return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0)); |
6a451e87 | 12010 | |
66787d4f | 12011 | /* bool_var == 1 becomes bool_var. */ |
12012 | if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE && integer_onep (arg1) | |
12013 | && code == EQ_EXPR) | |
389dd41b | 12014 | return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0)); |
fef10b60 | 12015 | |
931c6823 | 12016 | /* bool_var != 1 becomes !bool_var. */ |
12017 | if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE && integer_onep (arg1) | |
12018 | && code == NE_EXPR) | |
389dd41b | 12019 | return fold_build1_loc (loc, TRUTH_NOT_EXPR, type, |
12020 | fold_convert_loc (loc, type, arg0)); | |
931c6823 | 12021 | |
12022 | /* bool_var == 0 becomes !bool_var. */ | |
12023 | if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE && integer_zerop (arg1) | |
12024 | && code == EQ_EXPR) | |
389dd41b | 12025 | return fold_build1_loc (loc, TRUTH_NOT_EXPR, type, |
12026 | fold_convert_loc (loc, type, arg0)); | |
931c6823 | 12027 | |
a65c4d64 | 12028 | /* !exp != 0 becomes !exp */ |
12029 | if (TREE_CODE (arg0) == TRUTH_NOT_EXPR && integer_zerop (arg1) | |
12030 | && code == NE_EXPR) | |
12031 | return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0)); | |
12032 | ||
fef10b60 | 12033 | /* If this is an equality comparison of the address of two non-weak, |
12034 | unaliased symbols neither of which are extern (since we do not | |
12035 | have access to attributes for externs), then we know the result. */ | |
6a451e87 | 12036 | if (TREE_CODE (arg0) == ADDR_EXPR |
5ded8c6f | 12037 | && VAR_OR_FUNCTION_DECL_P (TREE_OPERAND (arg0, 0)) |
fef10b60 | 12038 | && ! DECL_WEAK (TREE_OPERAND (arg0, 0)) |
12039 | && ! lookup_attribute ("alias", | |
12040 | DECL_ATTRIBUTES (TREE_OPERAND (arg0, 0))) | |
12041 | && ! DECL_EXTERNAL (TREE_OPERAND (arg0, 0)) | |
12042 | && TREE_CODE (arg1) == ADDR_EXPR | |
5ded8c6f | 12043 | && VAR_OR_FUNCTION_DECL_P (TREE_OPERAND (arg1, 0)) |
fef10b60 | 12044 | && ! DECL_WEAK (TREE_OPERAND (arg1, 0)) |
12045 | && ! lookup_attribute ("alias", | |
12046 | DECL_ATTRIBUTES (TREE_OPERAND (arg1, 0))) | |
12047 | && ! DECL_EXTERNAL (TREE_OPERAND (arg1, 0))) | |
16bf25cd | 12048 | { |
12049 | /* We know that we're looking at the address of two | |
12050 | non-weak, unaliased, static _DECL nodes. | |
12051 | ||
12052 | It is both wasteful and incorrect to call operand_equal_p | |
12053 | to compare the two ADDR_EXPR nodes. It is wasteful in that | |
12054 | all we need to do is test pointer equality for the arguments | |
12055 | to the two ADDR_EXPR nodes. It is incorrect to use | |
12056 | operand_equal_p as that function is NOT equivalent to a | |
12057 | C equality test. It can in fact return false for two | |
12058 | objects which would test as equal using the C equality | |
12059 | operator. */ | |
12060 | bool equal = TREE_OPERAND (arg0, 0) == TREE_OPERAND (arg1, 0); | |
12061 | return constant_boolean_node (equal | |
12062 | ? code == EQ_EXPR : code != EQ_EXPR, | |
12063 | type); | |
12064 | } | |
fef10b60 | 12065 | |
6a451e87 | 12066 | /* If this is an EQ or NE comparison of a constant with a PLUS_EXPR or |
12067 | a MINUS_EXPR of a constant, we can convert it into a comparison with | |
12068 | a revised constant as long as no overflow occurs. */ | |
12069 | if (TREE_CODE (arg1) == INTEGER_CST | |
12070 | && (TREE_CODE (arg0) == PLUS_EXPR | |
12071 | || TREE_CODE (arg0) == MINUS_EXPR) | |
12072 | && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST | |
12073 | && 0 != (tem = const_binop (TREE_CODE (arg0) == PLUS_EXPR | |
12074 | ? MINUS_EXPR : PLUS_EXPR, | |
389dd41b | 12075 | fold_convert_loc (loc, TREE_TYPE (arg0), |
12076 | arg1), | |
a73dee2b | 12077 | TREE_OPERAND (arg0, 1), 0)) |
f96bd2bf | 12078 | && !TREE_OVERFLOW (tem)) |
389dd41b | 12079 | return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0), tem); |
89a6fcda | 12080 | |
6a451e87 | 12081 | /* Similarly for a NEGATE_EXPR. */ |
12082 | if (TREE_CODE (arg0) == NEGATE_EXPR | |
12083 | && TREE_CODE (arg1) == INTEGER_CST | |
12084 | && 0 != (tem = negate_expr (arg1)) | |
12085 | && TREE_CODE (tem) == INTEGER_CST | |
f96bd2bf | 12086 | && !TREE_OVERFLOW (tem)) |
389dd41b | 12087 | return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0), tem); |
89a6fcda | 12088 | |
89476fc9 | 12089 | /* Similarly for a BIT_XOR_EXPR; X ^ C1 == C2 is X == (C1 ^ C2). */ |
12090 | if (TREE_CODE (arg0) == BIT_XOR_EXPR | |
12091 | && TREE_CODE (arg1) == INTEGER_CST | |
12092 | && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST) | |
389dd41b | 12093 | return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0), |
12094 | fold_build2_loc (loc, BIT_XOR_EXPR, TREE_TYPE (arg0), | |
12095 | fold_convert_loc (loc, | |
12096 | TREE_TYPE (arg0), | |
12097 | arg1), | |
89476fc9 | 12098 | TREE_OPERAND (arg0, 1))); |
12099 | ||
172e662b | 12100 | /* Transform comparisons of the form X +- Y CMP X to Y CMP 0. */ |
12101 | if ((TREE_CODE (arg0) == PLUS_EXPR | |
12102 | || TREE_CODE (arg0) == POINTER_PLUS_EXPR | |
12103 | || TREE_CODE (arg0) == MINUS_EXPR) | |
428b02b4 | 12104 | && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0) |
428b02b4 | 12105 | && (INTEGRAL_TYPE_P (TREE_TYPE (arg0)) |
12106 | || POINTER_TYPE_P (TREE_TYPE (arg0)))) | |
12107 | { | |
172e662b | 12108 | tree val = TREE_OPERAND (arg0, 1); |
389dd41b | 12109 | return omit_two_operands_loc (loc, type, |
12110 | fold_build2_loc (loc, code, type, | |
172e662b | 12111 | val, |
12112 | build_int_cst (TREE_TYPE (val), | |
12113 | 0)), | |
12114 | TREE_OPERAND (arg0, 0), arg1); | |
12115 | } | |
12116 | ||
12117 | /* Transform comparisons of the form C - X CMP X if C % 2 == 1. */ | |
12118 | if (TREE_CODE (arg0) == MINUS_EXPR | |
12119 | && TREE_CODE (TREE_OPERAND (arg0, 0)) == INTEGER_CST | |
12120 | && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0) | |
12121 | && (TREE_INT_CST_LOW (TREE_OPERAND (arg0, 0)) & 1) == 1) | |
12122 | { | |
389dd41b | 12123 | return omit_two_operands_loc (loc, type, |
172e662b | 12124 | code == NE_EXPR |
12125 | ? boolean_true_node : boolean_false_node, | |
12126 | TREE_OPERAND (arg0, 1), arg1); | |
428b02b4 | 12127 | } |
12128 | ||
6a451e87 | 12129 | /* If we have X - Y == 0, we can convert that to X == Y and similarly |
12130 | for !=. Don't do this for ordered comparisons due to overflow. */ | |
12131 | if (TREE_CODE (arg0) == MINUS_EXPR | |
12132 | && integer_zerop (arg1)) | |
389dd41b | 12133 | return fold_build2_loc (loc, code, type, |
6a451e87 | 12134 | TREE_OPERAND (arg0, 0), TREE_OPERAND (arg0, 1)); |
89a6fcda | 12135 | |
6a451e87 | 12136 | /* Convert ABS_EXPR<x> == 0 or ABS_EXPR<x> != 0 to x == 0 or x != 0. */ |
12137 | if (TREE_CODE (arg0) == ABS_EXPR | |
12138 | && (integer_zerop (arg1) || real_zerop (arg1))) | |
389dd41b | 12139 | return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0), arg1); |
89a6fcda | 12140 | |
6a451e87 | 12141 | /* If this is an EQ or NE comparison with zero and ARG0 is |
12142 | (1 << foo) & bar, convert it to (bar >> foo) & 1. Both require | |
12143 | two operations, but the latter can be done in one less insn | |
12144 | on machines that have only two-operand insns or on which a | |
12145 | constant cannot be the first operand. */ | |
12146 | if (TREE_CODE (arg0) == BIT_AND_EXPR | |
12147 | && integer_zerop (arg1)) | |
12148 | { | |
12149 | tree arg00 = TREE_OPERAND (arg0, 0); | |
12150 | tree arg01 = TREE_OPERAND (arg0, 1); | |
12151 | if (TREE_CODE (arg00) == LSHIFT_EXPR | |
12152 | && integer_onep (TREE_OPERAND (arg00, 0))) | |
66fa37a1 | 12153 | { |
389dd41b | 12154 | tree tem = fold_build2_loc (loc, RSHIFT_EXPR, TREE_TYPE (arg00), |
66fa37a1 | 12155 | arg01, TREE_OPERAND (arg00, 1)); |
389dd41b | 12156 | tem = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg0), tem, |
66fa37a1 | 12157 | build_int_cst (TREE_TYPE (arg0), 1)); |
389dd41b | 12158 | return fold_build2_loc (loc, code, type, |
12159 | fold_convert_loc (loc, TREE_TYPE (arg1), tem), | |
12160 | arg1); | |
66fa37a1 | 12161 | } |
12162 | else if (TREE_CODE (arg01) == LSHIFT_EXPR | |
12163 | && integer_onep (TREE_OPERAND (arg01, 0))) | |
12164 | { | |
389dd41b | 12165 | tree tem = fold_build2_loc (loc, RSHIFT_EXPR, TREE_TYPE (arg01), |
66fa37a1 | 12166 | arg00, TREE_OPERAND (arg01, 1)); |
389dd41b | 12167 | tem = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg0), tem, |
66fa37a1 | 12168 | build_int_cst (TREE_TYPE (arg0), 1)); |
389dd41b | 12169 | return fold_build2_loc (loc, code, type, |
12170 | fold_convert_loc (loc, TREE_TYPE (arg1), tem), | |
12171 | arg1); | |
66fa37a1 | 12172 | } |
6a451e87 | 12173 | } |
12174 | ||
12175 | /* If this is an NE or EQ comparison of zero against the result of a | |
12176 | signed MOD operation whose second operand is a power of 2, make | |
12177 | the MOD operation unsigned since it is simpler and equivalent. */ | |
12178 | if (integer_zerop (arg1) | |
12179 | && !TYPE_UNSIGNED (TREE_TYPE (arg0)) | |
12180 | && (TREE_CODE (arg0) == TRUNC_MOD_EXPR | |
12181 | || TREE_CODE (arg0) == CEIL_MOD_EXPR | |
12182 | || TREE_CODE (arg0) == FLOOR_MOD_EXPR | |
12183 | || TREE_CODE (arg0) == ROUND_MOD_EXPR) | |
12184 | && integer_pow2p (TREE_OPERAND (arg0, 1))) | |
12185 | { | |
71eea85c | 12186 | tree newtype = unsigned_type_for (TREE_TYPE (arg0)); |
389dd41b | 12187 | tree newmod = fold_build2_loc (loc, TREE_CODE (arg0), newtype, |
12188 | fold_convert_loc (loc, newtype, | |
12189 | TREE_OPERAND (arg0, 0)), | |
12190 | fold_convert_loc (loc, newtype, | |
12191 | TREE_OPERAND (arg0, 1))); | |
6a451e87 | 12192 | |
389dd41b | 12193 | return fold_build2_loc (loc, code, type, newmod, |
12194 | fold_convert_loc (loc, newtype, arg1)); | |
6a451e87 | 12195 | } |
12196 | ||
1d8ff0d2 | 12197 | /* Fold ((X >> C1) & C2) == 0 and ((X >> C1) & C2) != 0 where |
12198 | C1 is a valid shift constant, and C2 is a power of two, i.e. | |
12199 | a single bit. */ | |
12200 | if (TREE_CODE (arg0) == BIT_AND_EXPR | |
12201 | && TREE_CODE (TREE_OPERAND (arg0, 0)) == RSHIFT_EXPR | |
12202 | && TREE_CODE (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1)) | |
12203 | == INTEGER_CST | |
12204 | && integer_pow2p (TREE_OPERAND (arg0, 1)) | |
12205 | && integer_zerop (arg1)) | |
12206 | { | |
12207 | tree itype = TREE_TYPE (arg0); | |
12208 | unsigned HOST_WIDE_INT prec = TYPE_PRECISION (itype); | |
12209 | tree arg001 = TREE_OPERAND (TREE_OPERAND (arg0, 0), 1); | |
12210 | ||
12211 | /* Check for a valid shift count. */ | |
12212 | if (TREE_INT_CST_HIGH (arg001) == 0 | |
12213 | && TREE_INT_CST_LOW (arg001) < prec) | |
12214 | { | |
12215 | tree arg01 = TREE_OPERAND (arg0, 1); | |
12216 | tree arg000 = TREE_OPERAND (TREE_OPERAND (arg0, 0), 0); | |
12217 | unsigned HOST_WIDE_INT log2 = tree_log2 (arg01); | |
12218 | /* If (C2 << C1) doesn't overflow, then ((X >> C1) & C2) != 0 | |
12219 | can be rewritten as (X & (C2 << C1)) != 0. */ | |
6b651543 | 12220 | if ((log2 + TREE_INT_CST_LOW (arg001)) < prec) |
1d8ff0d2 | 12221 | { |
389dd41b | 12222 | tem = fold_build2_loc (loc, LSHIFT_EXPR, itype, arg01, arg001); |
12223 | tem = fold_build2_loc (loc, BIT_AND_EXPR, itype, arg000, tem); | |
12224 | return fold_build2_loc (loc, code, type, tem, arg1); | |
1d8ff0d2 | 12225 | } |
12226 | /* Otherwise, for signed (arithmetic) shifts, | |
12227 | ((X >> C1) & C2) != 0 is rewritten as X < 0, and | |
12228 | ((X >> C1) & C2) == 0 is rewritten as X >= 0. */ | |
12229 | else if (!TYPE_UNSIGNED (itype)) | |
389dd41b | 12230 | return fold_build2_loc (loc, code == EQ_EXPR ? GE_EXPR : LT_EXPR, type, |
1d8ff0d2 | 12231 | arg000, build_int_cst (itype, 0)); |
12232 | /* Otherwise, of unsigned (logical) shifts, | |
12233 | ((X >> C1) & C2) != 0 is rewritten as (X,false), and | |
12234 | ((X >> C1) & C2) == 0 is rewritten as (X,true). */ | |
12235 | else | |
389dd41b | 12236 | return omit_one_operand_loc (loc, type, |
1d8ff0d2 | 12237 | code == EQ_EXPR ? integer_one_node |
12238 | : integer_zero_node, | |
12239 | arg000); | |
12240 | } | |
12241 | } | |
12242 | ||
6a451e87 | 12243 | /* If this is an NE comparison of zero with an AND of one, remove the |
12244 | comparison since the AND will give the correct value. */ | |
12245 | if (code == NE_EXPR | |
12246 | && integer_zerop (arg1) | |
12247 | && TREE_CODE (arg0) == BIT_AND_EXPR | |
12248 | && integer_onep (TREE_OPERAND (arg0, 1))) | |
389dd41b | 12249 | return fold_convert_loc (loc, type, arg0); |
6a451e87 | 12250 | |
12251 | /* If we have (A & C) == C where C is a power of 2, convert this into | |
12252 | (A & C) != 0. Similarly for NE_EXPR. */ | |
12253 | if (TREE_CODE (arg0) == BIT_AND_EXPR | |
12254 | && integer_pow2p (TREE_OPERAND (arg0, 1)) | |
12255 | && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0)) | |
389dd41b | 12256 | return fold_build2_loc (loc, code == EQ_EXPR ? NE_EXPR : EQ_EXPR, type, |
12257 | arg0, fold_convert_loc (loc, TREE_TYPE (arg0), | |
12258 | integer_zero_node)); | |
6a451e87 | 12259 | |
12260 | /* If we have (A & C) != 0 or (A & C) == 0 and C is the sign | |
12261 | bit, then fold the expression into A < 0 or A >= 0. */ | |
389dd41b | 12262 | tem = fold_single_bit_test_into_sign_test (loc, code, arg0, arg1, type); |
6a451e87 | 12263 | if (tem) |
12264 | return tem; | |
12265 | ||
12266 | /* If we have (A & C) == D where D & ~C != 0, convert this into 0. | |
12267 | Similarly for NE_EXPR. */ | |
12268 | if (TREE_CODE (arg0) == BIT_AND_EXPR | |
12269 | && TREE_CODE (arg1) == INTEGER_CST | |
12270 | && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST) | |
12271 | { | |
389dd41b | 12272 | tree notc = fold_build1_loc (loc, BIT_NOT_EXPR, |
6a451e87 | 12273 | TREE_TYPE (TREE_OPERAND (arg0, 1)), |
12274 | TREE_OPERAND (arg0, 1)); | |
389dd41b | 12275 | tree dandnotc = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg0), |
6a451e87 | 12276 | arg1, notc); |
12277 | tree rslt = code == EQ_EXPR ? integer_zero_node : integer_one_node; | |
12278 | if (integer_nonzerop (dandnotc)) | |
389dd41b | 12279 | return omit_one_operand_loc (loc, type, rslt, arg0); |
6a451e87 | 12280 | } |
12281 | ||
12282 | /* If we have (A | C) == D where C & ~D != 0, convert this into 0. | |
12283 | Similarly for NE_EXPR. */ | |
12284 | if (TREE_CODE (arg0) == BIT_IOR_EXPR | |
12285 | && TREE_CODE (arg1) == INTEGER_CST | |
12286 | && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST) | |
12287 | { | |
389dd41b | 12288 | tree notd = fold_build1_loc (loc, BIT_NOT_EXPR, TREE_TYPE (arg1), arg1); |
12289 | tree candnotd = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg0), | |
6a451e87 | 12290 | TREE_OPERAND (arg0, 1), notd); |
12291 | tree rslt = code == EQ_EXPR ? integer_zero_node : integer_one_node; | |
12292 | if (integer_nonzerop (candnotd)) | |
389dd41b | 12293 | return omit_one_operand_loc (loc, type, rslt, arg0); |
6a451e87 | 12294 | } |
12295 | ||
2a64c730 | 12296 | /* If this is a comparison of a field, we may be able to simplify it. */ |
12297 | if ((TREE_CODE (arg0) == COMPONENT_REF | |
12298 | || TREE_CODE (arg0) == BIT_FIELD_REF) | |
12299 | /* Handle the constant case even without -O | |
12300 | to make sure the warnings are given. */ | |
12301 | && (optimize || TREE_CODE (arg1) == INTEGER_CST)) | |
12302 | { | |
389dd41b | 12303 | t1 = optimize_bit_field_compare (loc, code, type, arg0, arg1); |
2a64c730 | 12304 | if (t1) |
12305 | return t1; | |
12306 | } | |
12307 | ||
6a451e87 | 12308 | /* Optimize comparisons of strlen vs zero to a compare of the |
12309 | first character of the string vs zero. To wit, | |
12310 | strlen(ptr) == 0 => *ptr == 0 | |
12311 | strlen(ptr) != 0 => *ptr != 0 | |
12312 | Other cases should reduce to one of these two (or a constant) | |
12313 | due to the return value of strlen being unsigned. */ | |
12314 | if (TREE_CODE (arg0) == CALL_EXPR | |
12315 | && integer_zerop (arg1)) | |
12316 | { | |
12317 | tree fndecl = get_callee_fndecl (arg0); | |
6a451e87 | 12318 | |
12319 | if (fndecl | |
12320 | && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL | |
12321 | && DECL_FUNCTION_CODE (fndecl) == BUILT_IN_STRLEN | |
c2f47e15 | 12322 | && call_expr_nargs (arg0) == 1 |
12323 | && TREE_CODE (TREE_TYPE (CALL_EXPR_ARG (arg0, 0))) == POINTER_TYPE) | |
6a451e87 | 12324 | { |
389dd41b | 12325 | tree iref = build_fold_indirect_ref_loc (loc, |
12326 | CALL_EXPR_ARG (arg0, 0)); | |
12327 | return fold_build2_loc (loc, code, type, iref, | |
6a451e87 | 12328 | build_int_cst (TREE_TYPE (iref), 0)); |
12329 | } | |
12330 | } | |
12331 | ||
12332 | /* Fold (X >> C) != 0 into X < 0 if C is one less than the width | |
12333 | of X. Similarly fold (X >> C) == 0 into X >= 0. */ | |
12334 | if (TREE_CODE (arg0) == RSHIFT_EXPR | |
12335 | && integer_zerop (arg1) | |
12336 | && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST) | |
12337 | { | |
12338 | tree arg00 = TREE_OPERAND (arg0, 0); | |
12339 | tree arg01 = TREE_OPERAND (arg0, 1); | |
12340 | tree itype = TREE_TYPE (arg00); | |
12341 | if (TREE_INT_CST_HIGH (arg01) == 0 | |
12342 | && TREE_INT_CST_LOW (arg01) | |
12343 | == (unsigned HOST_WIDE_INT) (TYPE_PRECISION (itype) - 1)) | |
12344 | { | |
12345 | if (TYPE_UNSIGNED (itype)) | |
12346 | { | |
11773141 | 12347 | itype = signed_type_for (itype); |
389dd41b | 12348 | arg00 = fold_convert_loc (loc, itype, arg00); |
6a451e87 | 12349 | } |
389dd41b | 12350 | return fold_build2_loc (loc, code == EQ_EXPR ? GE_EXPR : LT_EXPR, |
6a451e87 | 12351 | type, arg00, build_int_cst (itype, 0)); |
12352 | } | |
12353 | } | |
12354 | ||
47da8b49 | 12355 | /* (X ^ Y) == 0 becomes X == Y, and (X ^ Y) != 0 becomes X != Y. */ |
12356 | if (integer_zerop (arg1) | |
12357 | && TREE_CODE (arg0) == BIT_XOR_EXPR) | |
389dd41b | 12358 | return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0), |
47da8b49 | 12359 | TREE_OPERAND (arg0, 1)); |
12360 | ||
12361 | /* (X ^ Y) == Y becomes X == 0. We know that Y has no side-effects. */ | |
12362 | if (TREE_CODE (arg0) == BIT_XOR_EXPR | |
12363 | && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0)) | |
389dd41b | 12364 | return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0), |
47da8b49 | 12365 | build_int_cst (TREE_TYPE (arg1), 0)); |
12366 | /* Likewise (X ^ Y) == X becomes Y == 0. X has no side-effects. */ | |
12367 | if (TREE_CODE (arg0) == BIT_XOR_EXPR | |
12368 | && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0) | |
12369 | && reorder_operands_p (TREE_OPERAND (arg0, 1), arg1)) | |
389dd41b | 12370 | return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 1), |
47da8b49 | 12371 | build_int_cst (TREE_TYPE (arg1), 0)); |
12372 | ||
12373 | /* (X ^ C1) op C2 can be rewritten as X op (C1 ^ C2). */ | |
12374 | if (TREE_CODE (arg0) == BIT_XOR_EXPR | |
12375 | && TREE_CODE (arg1) == INTEGER_CST | |
12376 | && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST) | |
389dd41b | 12377 | return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0), |
12378 | fold_build2_loc (loc, BIT_XOR_EXPR, TREE_TYPE (arg1), | |
47da8b49 | 12379 | TREE_OPERAND (arg0, 1), arg1)); |
12380 | ||
e08a39b3 | 12381 | /* Fold (~X & C) == 0 into (X & C) != 0 and (~X & C) != 0 into |
12382 | (X & C) == 0 when C is a single bit. */ | |
12383 | if (TREE_CODE (arg0) == BIT_AND_EXPR | |
12384 | && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_NOT_EXPR | |
12385 | && integer_zerop (arg1) | |
12386 | && integer_pow2p (TREE_OPERAND (arg0, 1))) | |
12387 | { | |
389dd41b | 12388 | tem = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg0), |
e08a39b3 | 12389 | TREE_OPERAND (TREE_OPERAND (arg0, 0), 0), |
12390 | TREE_OPERAND (arg0, 1)); | |
389dd41b | 12391 | return fold_build2_loc (loc, code == EQ_EXPR ? NE_EXPR : EQ_EXPR, |
e08a39b3 | 12392 | type, tem, arg1); |
12393 | } | |
12394 | ||
12395 | /* Fold ((X & C) ^ C) eq/ne 0 into (X & C) ne/eq 0, when the | |
12396 | constant C is a power of two, i.e. a single bit. */ | |
12397 | if (TREE_CODE (arg0) == BIT_XOR_EXPR | |
12398 | && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_AND_EXPR | |
12399 | && integer_zerop (arg1) | |
12400 | && integer_pow2p (TREE_OPERAND (arg0, 1)) | |
12401 | && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1), | |
12402 | TREE_OPERAND (arg0, 1), OEP_ONLY_CONST)) | |
12403 | { | |
12404 | tree arg00 = TREE_OPERAND (arg0, 0); | |
389dd41b | 12405 | return fold_build2_loc (loc, code == EQ_EXPR ? NE_EXPR : EQ_EXPR, type, |
e08a39b3 | 12406 | arg00, build_int_cst (TREE_TYPE (arg00), 0)); |
12407 | } | |
12408 | ||
12409 | /* Likewise, fold ((X ^ C) & C) eq/ne 0 into (X & C) ne/eq 0, | |
12410 | when is C is a power of two, i.e. a single bit. */ | |
12411 | if (TREE_CODE (arg0) == BIT_AND_EXPR | |
12412 | && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_XOR_EXPR | |
12413 | && integer_zerop (arg1) | |
12414 | && integer_pow2p (TREE_OPERAND (arg0, 1)) | |
12415 | && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1), | |
12416 | TREE_OPERAND (arg0, 1), OEP_ONLY_CONST)) | |
12417 | { | |
12418 | tree arg000 = TREE_OPERAND (TREE_OPERAND (arg0, 0), 0); | |
389dd41b | 12419 | tem = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg000), |
e08a39b3 | 12420 | arg000, TREE_OPERAND (arg0, 1)); |
389dd41b | 12421 | return fold_build2_loc (loc, code == EQ_EXPR ? NE_EXPR : EQ_EXPR, type, |
e08a39b3 | 12422 | tem, build_int_cst (TREE_TYPE (tem), 0)); |
12423 | } | |
12424 | ||
6a451e87 | 12425 | if (integer_zerop (arg1) |
12426 | && tree_expr_nonzero_p (arg0)) | |
12427 | { | |
12428 | tree res = constant_boolean_node (code==NE_EXPR, type); | |
389dd41b | 12429 | return omit_one_operand_loc (loc, type, res, arg0); |
6a451e87 | 12430 | } |
746443a2 | 12431 | |
12432 | /* Fold -X op -Y as X op Y, where op is eq/ne. */ | |
12433 | if (TREE_CODE (arg0) == NEGATE_EXPR | |
12434 | && TREE_CODE (arg1) == NEGATE_EXPR) | |
389dd41b | 12435 | return fold_build2_loc (loc, code, type, |
746443a2 | 12436 | TREE_OPERAND (arg0, 0), |
12437 | TREE_OPERAND (arg1, 0)); | |
12438 | ||
32484276 | 12439 | /* Fold (X & C) op (Y & C) as (X ^ Y) & C op 0", and symmetries. */ |
12440 | if (TREE_CODE (arg0) == BIT_AND_EXPR | |
12441 | && TREE_CODE (arg1) == BIT_AND_EXPR) | |
12442 | { | |
12443 | tree arg00 = TREE_OPERAND (arg0, 0); | |
12444 | tree arg01 = TREE_OPERAND (arg0, 1); | |
12445 | tree arg10 = TREE_OPERAND (arg1, 0); | |
12446 | tree arg11 = TREE_OPERAND (arg1, 1); | |
12447 | tree itype = TREE_TYPE (arg0); | |
12448 | ||
12449 | if (operand_equal_p (arg01, arg11, 0)) | |
389dd41b | 12450 | return fold_build2_loc (loc, code, type, |
12451 | fold_build2_loc (loc, BIT_AND_EXPR, itype, | |
12452 | fold_build2_loc (loc, | |
12453 | BIT_XOR_EXPR, itype, | |
32484276 | 12454 | arg00, arg10), |
12455 | arg01), | |
12456 | build_int_cst (itype, 0)); | |
12457 | ||
12458 | if (operand_equal_p (arg01, arg10, 0)) | |
389dd41b | 12459 | return fold_build2_loc (loc, code, type, |
12460 | fold_build2_loc (loc, BIT_AND_EXPR, itype, | |
12461 | fold_build2_loc (loc, | |
12462 | BIT_XOR_EXPR, itype, | |
32484276 | 12463 | arg00, arg11), |
12464 | arg01), | |
12465 | build_int_cst (itype, 0)); | |
12466 | ||
12467 | if (operand_equal_p (arg00, arg11, 0)) | |
389dd41b | 12468 | return fold_build2_loc (loc, code, type, |
12469 | fold_build2_loc (loc, BIT_AND_EXPR, itype, | |
12470 | fold_build2_loc (loc, | |
12471 | BIT_XOR_EXPR, itype, | |
32484276 | 12472 | arg01, arg10), |
12473 | arg00), | |
12474 | build_int_cst (itype, 0)); | |
12475 | ||
12476 | if (operand_equal_p (arg00, arg10, 0)) | |
389dd41b | 12477 | return fold_build2_loc (loc, code, type, |
12478 | fold_build2_loc (loc, BIT_AND_EXPR, itype, | |
12479 | fold_build2_loc (loc, | |
12480 | BIT_XOR_EXPR, itype, | |
32484276 | 12481 | arg01, arg11), |
12482 | arg00), | |
12483 | build_int_cst (itype, 0)); | |
12484 | } | |
12485 | ||
89476fc9 | 12486 | if (TREE_CODE (arg0) == BIT_XOR_EXPR |
12487 | && TREE_CODE (arg1) == BIT_XOR_EXPR) | |
12488 | { | |
12489 | tree arg00 = TREE_OPERAND (arg0, 0); | |
12490 | tree arg01 = TREE_OPERAND (arg0, 1); | |
12491 | tree arg10 = TREE_OPERAND (arg1, 0); | |
12492 | tree arg11 = TREE_OPERAND (arg1, 1); | |
12493 | tree itype = TREE_TYPE (arg0); | |
12494 | ||
12495 | /* Optimize (X ^ Z) op (Y ^ Z) as X op Y, and symmetries. | |
12496 | operand_equal_p guarantees no side-effects so we don't need | |
12497 | to use omit_one_operand on Z. */ | |
12498 | if (operand_equal_p (arg01, arg11, 0)) | |
389dd41b | 12499 | return fold_build2_loc (loc, code, type, arg00, arg10); |
89476fc9 | 12500 | if (operand_equal_p (arg01, arg10, 0)) |
389dd41b | 12501 | return fold_build2_loc (loc, code, type, arg00, arg11); |
89476fc9 | 12502 | if (operand_equal_p (arg00, arg11, 0)) |
389dd41b | 12503 | return fold_build2_loc (loc, code, type, arg01, arg10); |
89476fc9 | 12504 | if (operand_equal_p (arg00, arg10, 0)) |
389dd41b | 12505 | return fold_build2_loc (loc, code, type, arg01, arg11); |
89476fc9 | 12506 | |
12507 | /* Optimize (X ^ C1) op (Y ^ C2) as (X ^ (C1 ^ C2)) op Y. */ | |
12508 | if (TREE_CODE (arg01) == INTEGER_CST | |
12509 | && TREE_CODE (arg11) == INTEGER_CST) | |
389dd41b | 12510 | return fold_build2_loc (loc, code, type, |
12511 | fold_build2_loc (loc, BIT_XOR_EXPR, itype, arg00, | |
12512 | fold_build2_loc (loc, | |
12513 | BIT_XOR_EXPR, itype, | |
89476fc9 | 12514 | arg01, arg11)), |
12515 | arg10); | |
12516 | } | |
5dbcb7c4 | 12517 | |
12518 | /* Attempt to simplify equality/inequality comparisons of complex | |
12519 | values. Only lower the comparison if the result is known or | |
12520 | can be simplified to a single scalar comparison. */ | |
12521 | if ((TREE_CODE (arg0) == COMPLEX_EXPR | |
12522 | || TREE_CODE (arg0) == COMPLEX_CST) | |
12523 | && (TREE_CODE (arg1) == COMPLEX_EXPR | |
12524 | || TREE_CODE (arg1) == COMPLEX_CST)) | |
12525 | { | |
12526 | tree real0, imag0, real1, imag1; | |
12527 | tree rcond, icond; | |
12528 | ||
12529 | if (TREE_CODE (arg0) == COMPLEX_EXPR) | |
12530 | { | |
12531 | real0 = TREE_OPERAND (arg0, 0); | |
12532 | imag0 = TREE_OPERAND (arg0, 1); | |
12533 | } | |
12534 | else | |
12535 | { | |
12536 | real0 = TREE_REALPART (arg0); | |
12537 | imag0 = TREE_IMAGPART (arg0); | |
12538 | } | |
12539 | ||
12540 | if (TREE_CODE (arg1) == COMPLEX_EXPR) | |
12541 | { | |
12542 | real1 = TREE_OPERAND (arg1, 0); | |
12543 | imag1 = TREE_OPERAND (arg1, 1); | |
12544 | } | |
12545 | else | |
12546 | { | |
12547 | real1 = TREE_REALPART (arg1); | |
12548 | imag1 = TREE_IMAGPART (arg1); | |
12549 | } | |
12550 | ||
389dd41b | 12551 | rcond = fold_binary_loc (loc, code, type, real0, real1); |
5dbcb7c4 | 12552 | if (rcond && TREE_CODE (rcond) == INTEGER_CST) |
12553 | { | |
12554 | if (integer_zerop (rcond)) | |
12555 | { | |
12556 | if (code == EQ_EXPR) | |
389dd41b | 12557 | return omit_two_operands_loc (loc, type, boolean_false_node, |
5dbcb7c4 | 12558 | imag0, imag1); |
389dd41b | 12559 | return fold_build2_loc (loc, NE_EXPR, type, imag0, imag1); |
5dbcb7c4 | 12560 | } |
12561 | else | |
12562 | { | |
12563 | if (code == NE_EXPR) | |
389dd41b | 12564 | return omit_two_operands_loc (loc, type, boolean_true_node, |
5dbcb7c4 | 12565 | imag0, imag1); |
389dd41b | 12566 | return fold_build2_loc (loc, EQ_EXPR, type, imag0, imag1); |
5dbcb7c4 | 12567 | } |
12568 | } | |
12569 | ||
389dd41b | 12570 | icond = fold_binary_loc (loc, code, type, imag0, imag1); |
5dbcb7c4 | 12571 | if (icond && TREE_CODE (icond) == INTEGER_CST) |
12572 | { | |
12573 | if (integer_zerop (icond)) | |
12574 | { | |
12575 | if (code == EQ_EXPR) | |
389dd41b | 12576 | return omit_two_operands_loc (loc, type, boolean_false_node, |
5dbcb7c4 | 12577 | real0, real1); |
389dd41b | 12578 | return fold_build2_loc (loc, NE_EXPR, type, real0, real1); |
5dbcb7c4 | 12579 | } |
12580 | else | |
12581 | { | |
12582 | if (code == NE_EXPR) | |
389dd41b | 12583 | return omit_two_operands_loc (loc, type, boolean_true_node, |
5dbcb7c4 | 12584 | real0, real1); |
389dd41b | 12585 | return fold_build2_loc (loc, EQ_EXPR, type, real0, real1); |
5dbcb7c4 | 12586 | } |
12587 | } | |
12588 | } | |
12589 | ||
6a451e87 | 12590 | return NULL_TREE; |
12591 | ||
12592 | case LT_EXPR: | |
12593 | case GT_EXPR: | |
12594 | case LE_EXPR: | |
12595 | case GE_EXPR: | |
389dd41b | 12596 | tem = fold_comparison (loc, code, type, op0, op1); |
6a451e87 | 12597 | if (tem != NULL_TREE) |
12598 | return tem; | |
12599 | ||
12600 | /* Transform comparisons of the form X +- C CMP X. */ | |
12601 | if ((TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR) | |
12602 | && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0) | |
12603 | && ((TREE_CODE (TREE_OPERAND (arg0, 1)) == REAL_CST | |
12604 | && !HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))) | |
12605 | || (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST | |
981eb798 | 12606 | && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1))))) |
6a451e87 | 12607 | { |
12608 | tree arg01 = TREE_OPERAND (arg0, 1); | |
12609 | enum tree_code code0 = TREE_CODE (arg0); | |
12610 | int is_positive; | |
12611 | ||
12612 | if (TREE_CODE (arg01) == REAL_CST) | |
12613 | is_positive = REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg01)) ? -1 : 1; | |
12614 | else | |
12615 | is_positive = tree_int_cst_sgn (arg01); | |
12616 | ||
12617 | /* (X - c) > X becomes false. */ | |
12618 | if (code == GT_EXPR | |
12619 | && ((code0 == MINUS_EXPR && is_positive >= 0) | |
12620 | || (code0 == PLUS_EXPR && is_positive <= 0))) | |
add6ee5e | 12621 | { |
12622 | if (TREE_CODE (arg01) == INTEGER_CST | |
12623 | && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1))) | |
12624 | fold_overflow_warning (("assuming signed overflow does not " | |
12625 | "occur when assuming that (X - c) > X " | |
12626 | "is always false"), | |
12627 | WARN_STRICT_OVERFLOW_ALL); | |
12628 | return constant_boolean_node (0, type); | |
12629 | } | |
6a451e87 | 12630 | |
12631 | /* Likewise (X + c) < X becomes false. */ | |
12632 | if (code == LT_EXPR | |
12633 | && ((code0 == PLUS_EXPR && is_positive >= 0) | |
12634 | || (code0 == MINUS_EXPR && is_positive <= 0))) | |
add6ee5e | 12635 | { |
12636 | if (TREE_CODE (arg01) == INTEGER_CST | |
12637 | && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1))) | |
12638 | fold_overflow_warning (("assuming signed overflow does not " | |
12639 | "occur when assuming that " | |
12640 | "(X + c) < X is always false"), | |
12641 | WARN_STRICT_OVERFLOW_ALL); | |
12642 | return constant_boolean_node (0, type); | |
12643 | } | |
6a451e87 | 12644 | |
12645 | /* Convert (X - c) <= X to true. */ | |
12646 | if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg1))) | |
12647 | && code == LE_EXPR | |
89a6fcda | 12648 | && ((code0 == MINUS_EXPR && is_positive >= 0) |
12649 | || (code0 == PLUS_EXPR && is_positive <= 0))) | |
add6ee5e | 12650 | { |
12651 | if (TREE_CODE (arg01) == INTEGER_CST | |
12652 | && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1))) | |
12653 | fold_overflow_warning (("assuming signed overflow does not " | |
12654 | "occur when assuming that " | |
12655 | "(X - c) <= X is always true"), | |
12656 | WARN_STRICT_OVERFLOW_ALL); | |
12657 | return constant_boolean_node (1, type); | |
12658 | } | |
89a6fcda | 12659 | |
12660 | /* Convert (X + c) >= X to true. */ | |
12661 | if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg1))) | |
12662 | && code == GE_EXPR | |
12663 | && ((code0 == PLUS_EXPR && is_positive >= 0) | |
12664 | || (code0 == MINUS_EXPR && is_positive <= 0))) | |
add6ee5e | 12665 | { |
12666 | if (TREE_CODE (arg01) == INTEGER_CST | |
12667 | && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1))) | |
12668 | fold_overflow_warning (("assuming signed overflow does not " | |
12669 | "occur when assuming that " | |
12670 | "(X + c) >= X is always true"), | |
12671 | WARN_STRICT_OVERFLOW_ALL); | |
12672 | return constant_boolean_node (1, type); | |
12673 | } | |
89a6fcda | 12674 | |
12675 | if (TREE_CODE (arg01) == INTEGER_CST) | |
12676 | { | |
12677 | /* Convert X + c > X and X - c < X to true for integers. */ | |
12678 | if (code == GT_EXPR | |
12679 | && ((code0 == PLUS_EXPR && is_positive > 0) | |
12680 | || (code0 == MINUS_EXPR && is_positive < 0))) | |
add6ee5e | 12681 | { |
12682 | if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1))) | |
12683 | fold_overflow_warning (("assuming signed overflow does " | |
12684 | "not occur when assuming that " | |
12685 | "(X + c) > X is always true"), | |
12686 | WARN_STRICT_OVERFLOW_ALL); | |
12687 | return constant_boolean_node (1, type); | |
12688 | } | |
89a6fcda | 12689 | |
12690 | if (code == LT_EXPR | |
12691 | && ((code0 == MINUS_EXPR && is_positive > 0) | |
12692 | || (code0 == PLUS_EXPR && is_positive < 0))) | |
add6ee5e | 12693 | { |
12694 | if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1))) | |
12695 | fold_overflow_warning (("assuming signed overflow does " | |
12696 | "not occur when assuming that " | |
12697 | "(X - c) < X is always true"), | |
12698 | WARN_STRICT_OVERFLOW_ALL); | |
12699 | return constant_boolean_node (1, type); | |
12700 | } | |
89a6fcda | 12701 | |
12702 | /* Convert X + c <= X and X - c >= X to false for integers. */ | |
12703 | if (code == LE_EXPR | |
12704 | && ((code0 == PLUS_EXPR && is_positive > 0) | |
12705 | || (code0 == MINUS_EXPR && is_positive < 0))) | |
add6ee5e | 12706 | { |
12707 | if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1))) | |
12708 | fold_overflow_warning (("assuming signed overflow does " | |
12709 | "not occur when assuming that " | |
12710 | "(X + c) <= X is always false"), | |
12711 | WARN_STRICT_OVERFLOW_ALL); | |
12712 | return constant_boolean_node (0, type); | |
12713 | } | |
89a6fcda | 12714 | |
12715 | if (code == GE_EXPR | |
12716 | && ((code0 == MINUS_EXPR && is_positive > 0) | |
12717 | || (code0 == PLUS_EXPR && is_positive < 0))) | |
add6ee5e | 12718 | { |
12719 | if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1))) | |
12720 | fold_overflow_warning (("assuming signed overflow does " | |
12721 | "not occur when assuming that " | |
558517fd | 12722 | "(X - c) >= X is always false"), |
add6ee5e | 12723 | WARN_STRICT_OVERFLOW_ALL); |
12724 | return constant_boolean_node (0, type); | |
12725 | } | |
89a6fcda | 12726 | } |
12727 | } | |
12728 | ||
fef10b60 | 12729 | /* Comparisons with the highest or lowest possible integer of |
8aa01816 | 12730 | the specified precision will have known values. */ |
fef10b60 | 12731 | { |
8aa01816 | 12732 | tree arg1_type = TREE_TYPE (arg1); |
12733 | unsigned int width = TYPE_PRECISION (arg1_type); | |
fef10b60 | 12734 | |
12735 | if (TREE_CODE (arg1) == INTEGER_CST | |
fef10b60 | 12736 | && width <= 2 * HOST_BITS_PER_WIDE_INT |
8aa01816 | 12737 | && (INTEGRAL_TYPE_P (arg1_type) || POINTER_TYPE_P (arg1_type))) |
fef10b60 | 12738 | { |
12739 | HOST_WIDE_INT signed_max_hi; | |
12740 | unsigned HOST_WIDE_INT signed_max_lo; | |
12741 | unsigned HOST_WIDE_INT max_hi, max_lo, min_hi, min_lo; | |
12742 | ||
12743 | if (width <= HOST_BITS_PER_WIDE_INT) | |
12744 | { | |
12745 | signed_max_lo = ((unsigned HOST_WIDE_INT) 1 << (width - 1)) | |
12746 | - 1; | |
12747 | signed_max_hi = 0; | |
12748 | max_hi = 0; | |
12749 | ||
8aa01816 | 12750 | if (TYPE_UNSIGNED (arg1_type)) |
fef10b60 | 12751 | { |
12752 | max_lo = ((unsigned HOST_WIDE_INT) 2 << (width - 1)) - 1; | |
12753 | min_lo = 0; | |
12754 | min_hi = 0; | |
12755 | } | |
12756 | else | |
12757 | { | |
12758 | max_lo = signed_max_lo; | |
12759 | min_lo = ((unsigned HOST_WIDE_INT) -1 << (width - 1)); | |
12760 | min_hi = -1; | |
12761 | } | |
12762 | } | |
12763 | else | |
12764 | { | |
12765 | width -= HOST_BITS_PER_WIDE_INT; | |
12766 | signed_max_lo = -1; | |
12767 | signed_max_hi = ((unsigned HOST_WIDE_INT) 1 << (width - 1)) | |
12768 | - 1; | |
12769 | max_lo = -1; | |
12770 | min_lo = 0; | |
12771 | ||
8aa01816 | 12772 | if (TYPE_UNSIGNED (arg1_type)) |
fef10b60 | 12773 | { |
12774 | max_hi = ((unsigned HOST_WIDE_INT) 2 << (width - 1)) - 1; | |
12775 | min_hi = 0; | |
12776 | } | |
12777 | else | |
12778 | { | |
12779 | max_hi = signed_max_hi; | |
12780 | min_hi = ((unsigned HOST_WIDE_INT) -1 << (width - 1)); | |
12781 | } | |
12782 | } | |
12783 | ||
12784 | if ((unsigned HOST_WIDE_INT) TREE_INT_CST_HIGH (arg1) == max_hi | |
12785 | && TREE_INT_CST_LOW (arg1) == max_lo) | |
12786 | switch (code) | |
12787 | { | |
12788 | case GT_EXPR: | |
389dd41b | 12789 | return omit_one_operand_loc (loc, type, integer_zero_node, arg0); |
fef10b60 | 12790 | |
12791 | case GE_EXPR: | |
389dd41b | 12792 | return fold_build2_loc (loc, EQ_EXPR, type, op0, op1); |
fef10b60 | 12793 | |
12794 | case LE_EXPR: | |
389dd41b | 12795 | return omit_one_operand_loc (loc, type, integer_one_node, arg0); |
fef10b60 | 12796 | |
12797 | case LT_EXPR: | |
389dd41b | 12798 | return fold_build2_loc (loc, NE_EXPR, type, op0, op1); |
fef10b60 | 12799 | |
12800 | /* The GE_EXPR and LT_EXPR cases above are not normally | |
12801 | reached because of previous transformations. */ | |
12802 | ||
12803 | default: | |
12804 | break; | |
12805 | } | |
12806 | else if ((unsigned HOST_WIDE_INT) TREE_INT_CST_HIGH (arg1) | |
12807 | == max_hi | |
12808 | && TREE_INT_CST_LOW (arg1) == max_lo - 1) | |
12809 | switch (code) | |
12810 | { | |
12811 | case GT_EXPR: | |
2455d3ef | 12812 | arg1 = const_binop (PLUS_EXPR, arg1, |
12813 | build_int_cst (TREE_TYPE (arg1), 1), 0); | |
389dd41b | 12814 | return fold_build2_loc (loc, EQ_EXPR, type, |
12815 | fold_convert_loc (loc, | |
12816 | TREE_TYPE (arg1), arg0), | |
ddb8b0be | 12817 | arg1); |
fef10b60 | 12818 | case LE_EXPR: |
2455d3ef | 12819 | arg1 = const_binop (PLUS_EXPR, arg1, |
12820 | build_int_cst (TREE_TYPE (arg1), 1), 0); | |
389dd41b | 12821 | return fold_build2_loc (loc, NE_EXPR, type, |
12822 | fold_convert_loc (loc, TREE_TYPE (arg1), | |
12823 | arg0), | |
ddb8b0be | 12824 | arg1); |
fef10b60 | 12825 | default: |
12826 | break; | |
12827 | } | |
12828 | else if ((unsigned HOST_WIDE_INT) TREE_INT_CST_HIGH (arg1) | |
12829 | == min_hi | |
12830 | && TREE_INT_CST_LOW (arg1) == min_lo) | |
12831 | switch (code) | |
12832 | { | |
12833 | case LT_EXPR: | |
389dd41b | 12834 | return omit_one_operand_loc (loc, type, integer_zero_node, arg0); |
fef10b60 | 12835 | |
12836 | case LE_EXPR: | |
389dd41b | 12837 | return fold_build2_loc (loc, EQ_EXPR, type, op0, op1); |
fef10b60 | 12838 | |
12839 | case GE_EXPR: | |
389dd41b | 12840 | return omit_one_operand_loc (loc, type, integer_one_node, arg0); |
fef10b60 | 12841 | |
12842 | case GT_EXPR: | |
389dd41b | 12843 | return fold_build2_loc (loc, NE_EXPR, type, op0, op1); |
fef10b60 | 12844 | |
12845 | default: | |
12846 | break; | |
12847 | } | |
12848 | else if ((unsigned HOST_WIDE_INT) TREE_INT_CST_HIGH (arg1) | |
12849 | == min_hi | |
12850 | && TREE_INT_CST_LOW (arg1) == min_lo + 1) | |
12851 | switch (code) | |
12852 | { | |
12853 | case GE_EXPR: | |
12854 | arg1 = const_binop (MINUS_EXPR, arg1, integer_one_node, 0); | |
389dd41b | 12855 | return fold_build2_loc (loc, NE_EXPR, type, |
12856 | fold_convert_loc (loc, | |
12857 | TREE_TYPE (arg1), arg0), | |
ddb8b0be | 12858 | arg1); |
fef10b60 | 12859 | case LT_EXPR: |
12860 | arg1 = const_binop (MINUS_EXPR, arg1, integer_one_node, 0); | |
389dd41b | 12861 | return fold_build2_loc (loc, EQ_EXPR, type, |
12862 | fold_convert_loc (loc, TREE_TYPE (arg1), | |
12863 | arg0), | |
ddb8b0be | 12864 | arg1); |
fef10b60 | 12865 | default: |
12866 | break; | |
12867 | } | |
12868 | ||
f2532264 | 12869 | else if (TREE_INT_CST_HIGH (arg1) == signed_max_hi |
fef10b60 | 12870 | && TREE_INT_CST_LOW (arg1) == signed_max_lo |
8aa01816 | 12871 | && TYPE_UNSIGNED (arg1_type) |
12872 | /* We will flip the signedness of the comparison operator | |
12873 | associated with the mode of arg1, so the sign bit is | |
12874 | specified by this mode. Check that arg1 is the signed | |
12875 | max associated with this sign bit. */ | |
12876 | && width == GET_MODE_BITSIZE (TYPE_MODE (arg1_type)) | |
fef10b60 | 12877 | /* signed_type does not work on pointer types. */ |
8aa01816 | 12878 | && INTEGRAL_TYPE_P (arg1_type)) |
fef10b60 | 12879 | { |
12880 | /* The following case also applies to X < signed_max+1 | |
12881 | and X >= signed_max+1 because previous transformations. */ | |
12882 | if (code == LE_EXPR || code == GT_EXPR) | |
12883 | { | |
ddb8b0be | 12884 | tree st; |
11773141 | 12885 | st = signed_type_for (TREE_TYPE (arg1)); |
389dd41b | 12886 | return fold_build2_loc (loc, |
12887 | code == LE_EXPR ? GE_EXPR : LT_EXPR, | |
12888 | type, fold_convert_loc (loc, st, arg0), | |
ddb8b0be | 12889 | build_int_cst (st, 0)); |
fef10b60 | 12890 | } |
12891 | } | |
12892 | } | |
12893 | } | |
12894 | ||
fef10b60 | 12895 | /* If we are comparing an ABS_EXPR with a constant, we can |
12896 | convert all the cases into explicit comparisons, but they may | |
12897 | well not be faster than doing the ABS and one comparison. | |
12898 | But ABS (X) <= C is a range comparison, which becomes a subtraction | |
12899 | and a comparison, and is probably faster. */ | |
6a451e87 | 12900 | if (code == LE_EXPR |
12901 | && TREE_CODE (arg1) == INTEGER_CST | |
12902 | && TREE_CODE (arg0) == ABS_EXPR | |
12903 | && ! TREE_SIDE_EFFECTS (arg0) | |
12904 | && (0 != (tem = negate_expr (arg1))) | |
12905 | && TREE_CODE (tem) == INTEGER_CST | |
f96bd2bf | 12906 | && !TREE_OVERFLOW (tem)) |
389dd41b | 12907 | return fold_build2_loc (loc, TRUTH_ANDIF_EXPR, type, |
7ab7fd4f | 12908 | build2 (GE_EXPR, type, |
12909 | TREE_OPERAND (arg0, 0), tem), | |
12910 | build2 (LE_EXPR, type, | |
12911 | TREE_OPERAND (arg0, 0), arg1)); | |
fef10b60 | 12912 | |
12913 | /* Convert ABS_EXPR<x> >= 0 to true. */ | |
add6ee5e | 12914 | strict_overflow_p = false; |
6a451e87 | 12915 | if (code == GE_EXPR |
6a451e87 | 12916 | && (integer_zerop (arg1) |
12917 | || (! HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0))) | |
add6ee5e | 12918 | && real_zerop (arg1))) |
12919 | && tree_expr_nonnegative_warnv_p (arg0, &strict_overflow_p)) | |
12920 | { | |
12921 | if (strict_overflow_p) | |
12922 | fold_overflow_warning (("assuming signed overflow does not occur " | |
12923 | "when simplifying comparison of " | |
12924 | "absolute value and zero"), | |
12925 | WARN_STRICT_OVERFLOW_CONDITIONAL); | |
389dd41b | 12926 | return omit_one_operand_loc (loc, type, integer_one_node, arg0); |
add6ee5e | 12927 | } |
fef10b60 | 12928 | |
12929 | /* Convert ABS_EXPR<x> < 0 to false. */ | |
add6ee5e | 12930 | strict_overflow_p = false; |
6a451e87 | 12931 | if (code == LT_EXPR |
add6ee5e | 12932 | && (integer_zerop (arg1) || real_zerop (arg1)) |
12933 | && tree_expr_nonnegative_warnv_p (arg0, &strict_overflow_p)) | |
12934 | { | |
12935 | if (strict_overflow_p) | |
12936 | fold_overflow_warning (("assuming signed overflow does not occur " | |
12937 | "when simplifying comparison of " | |
12938 | "absolute value and zero"), | |
12939 | WARN_STRICT_OVERFLOW_CONDITIONAL); | |
389dd41b | 12940 | return omit_one_operand_loc (loc, type, integer_zero_node, arg0); |
add6ee5e | 12941 | } |
fef10b60 | 12942 | |
fef10b60 | 12943 | /* If X is unsigned, convert X < (1 << Y) into X >> Y == 0 |
12944 | and similarly for >= into !=. */ | |
12945 | if ((code == LT_EXPR || code == GE_EXPR) | |
12946 | && TYPE_UNSIGNED (TREE_TYPE (arg0)) | |
12947 | && TREE_CODE (arg1) == LSHIFT_EXPR | |
12948 | && integer_onep (TREE_OPERAND (arg1, 0))) | |
389dd41b | 12949 | { |
12950 | tem = build2 (code == LT_EXPR ? EQ_EXPR : NE_EXPR, type, | |
12951 | build2 (RSHIFT_EXPR, TREE_TYPE (arg0), arg0, | |
12952 | TREE_OPERAND (arg1, 1)), | |
12953 | build_int_cst (TREE_TYPE (arg0), 0)); | |
12954 | goto fold_binary_exit; | |
12955 | } | |
fef10b60 | 12956 | |
6a451e87 | 12957 | if ((code == LT_EXPR || code == GE_EXPR) |
12958 | && TYPE_UNSIGNED (TREE_TYPE (arg0)) | |
72dd6141 | 12959 | && CONVERT_EXPR_P (arg1) |
6a451e87 | 12960 | && TREE_CODE (TREE_OPERAND (arg1, 0)) == LSHIFT_EXPR |
12961 | && integer_onep (TREE_OPERAND (TREE_OPERAND (arg1, 0), 0))) | |
389dd41b | 12962 | { |
12963 | tem = build2 (code == LT_EXPR ? EQ_EXPR : NE_EXPR, type, | |
12964 | fold_convert_loc (loc, TREE_TYPE (arg0), | |
12965 | build2 (RSHIFT_EXPR, | |
12966 | TREE_TYPE (arg0), arg0, | |
12967 | TREE_OPERAND (TREE_OPERAND (arg1, 0), | |
12968 | 1))), | |
12969 | build_int_cst (TREE_TYPE (arg0), 0)); | |
12970 | goto fold_binary_exit; | |
12971 | } | |
fef10b60 | 12972 | |
6a451e87 | 12973 | return NULL_TREE; |
fef10b60 | 12974 | |
12975 | case UNORDERED_EXPR: | |
12976 | case ORDERED_EXPR: | |
12977 | case UNLT_EXPR: | |
12978 | case UNLE_EXPR: | |
12979 | case UNGT_EXPR: | |
12980 | case UNGE_EXPR: | |
12981 | case UNEQ_EXPR: | |
12982 | case LTGT_EXPR: | |
12983 | if (TREE_CODE (arg0) == REAL_CST && TREE_CODE (arg1) == REAL_CST) | |
12984 | { | |
12985 | t1 = fold_relational_const (code, type, arg0, arg1); | |
12986 | if (t1 != NULL_TREE) | |
12987 | return t1; | |
12988 | } | |
12989 | ||
12990 | /* If the first operand is NaN, the result is constant. */ | |
12991 | if (TREE_CODE (arg0) == REAL_CST | |
12992 | && REAL_VALUE_ISNAN (TREE_REAL_CST (arg0)) | |
12993 | && (code != LTGT_EXPR || ! flag_trapping_math)) | |
12994 | { | |
12995 | t1 = (code == ORDERED_EXPR || code == LTGT_EXPR) | |
12996 | ? integer_zero_node | |
12997 | : integer_one_node; | |
389dd41b | 12998 | return omit_one_operand_loc (loc, type, t1, arg1); |
fef10b60 | 12999 | } |
13000 | ||
13001 | /* If the second operand is NaN, the result is constant. */ | |
13002 | if (TREE_CODE (arg1) == REAL_CST | |
13003 | && REAL_VALUE_ISNAN (TREE_REAL_CST (arg1)) | |
13004 | && (code != LTGT_EXPR || ! flag_trapping_math)) | |
13005 | { | |
13006 | t1 = (code == ORDERED_EXPR || code == LTGT_EXPR) | |
13007 | ? integer_zero_node | |
13008 | : integer_one_node; | |
389dd41b | 13009 | return omit_one_operand_loc (loc, type, t1, arg0); |
fef10b60 | 13010 | } |
13011 | ||
13012 | /* Simplify unordered comparison of something with itself. */ | |
13013 | if ((code == UNLE_EXPR || code == UNGE_EXPR || code == UNEQ_EXPR) | |
13014 | && operand_equal_p (arg0, arg1, 0)) | |
13015 | return constant_boolean_node (1, type); | |
13016 | ||
13017 | if (code == LTGT_EXPR | |
13018 | && !flag_trapping_math | |
13019 | && operand_equal_p (arg0, arg1, 0)) | |
13020 | return constant_boolean_node (0, type); | |
13021 | ||
13022 | /* Fold (double)float1 CMP (double)float2 into float1 CMP float2. */ | |
13023 | { | |
13024 | tree targ0 = strip_float_extensions (arg0); | |
13025 | tree targ1 = strip_float_extensions (arg1); | |
13026 | tree newtype = TREE_TYPE (targ0); | |
13027 | ||
13028 | if (TYPE_PRECISION (TREE_TYPE (targ1)) > TYPE_PRECISION (newtype)) | |
13029 | newtype = TREE_TYPE (targ1); | |
13030 | ||
13031 | if (TYPE_PRECISION (newtype) < TYPE_PRECISION (TREE_TYPE (arg0))) | |
389dd41b | 13032 | return fold_build2_loc (loc, code, type, |
13033 | fold_convert_loc (loc, newtype, targ0), | |
13034 | fold_convert_loc (loc, newtype, targ1)); | |
fef10b60 | 13035 | } |
13036 | ||
e7edfbbd | 13037 | return NULL_TREE; |
fef10b60 | 13038 | |
13039 | case COMPOUND_EXPR: | |
13040 | /* When pedantic, a compound expression can be neither an lvalue | |
13041 | nor an integer constant expression. */ | |
13042 | if (TREE_SIDE_EFFECTS (arg0) || TREE_CONSTANT (arg1)) | |
e7edfbbd | 13043 | return NULL_TREE; |
fef10b60 | 13044 | /* Don't let (0, 0) be null pointer constant. */ |
13045 | tem = integer_zerop (arg1) ? build1 (NOP_EXPR, type, arg1) | |
389dd41b | 13046 | : fold_convert_loc (loc, type, arg1); |
13047 | return pedantic_non_lvalue_loc (loc, tem); | |
fef10b60 | 13048 | |
13049 | case COMPLEX_EXPR: | |
32cef1cc | 13050 | if ((TREE_CODE (arg0) == REAL_CST |
13051 | && TREE_CODE (arg1) == REAL_CST) | |
13052 | || (TREE_CODE (arg0) == INTEGER_CST | |
13053 | && TREE_CODE (arg1) == INTEGER_CST)) | |
fef10b60 | 13054 | return build_complex (type, arg0, arg1); |
e7edfbbd | 13055 | return NULL_TREE; |
fef10b60 | 13056 | |
b0273ac6 | 13057 | case ASSERT_EXPR: |
13058 | /* An ASSERT_EXPR should never be passed to fold_binary. */ | |
13059 | gcc_unreachable (); | |
13060 | ||
fef10b60 | 13061 | default: |
e7edfbbd | 13062 | return NULL_TREE; |
fef10b60 | 13063 | } /* switch (code) */ |
389dd41b | 13064 | fold_binary_exit: |
13065 | protected_set_expr_location (tem, loc); | |
13066 | return tem; | |
fef10b60 | 13067 | } |
13068 | ||
2ba5b763 | 13069 | /* Callback for walk_tree, looking for LABEL_EXPR. Return *TP if it is |
13070 | a LABEL_EXPR; otherwise return NULL_TREE. Do not check the subtrees | |
13071 | of GOTO_EXPR. */ | |
f279e190 | 13072 | |
13073 | static tree | |
2ba5b763 | 13074 | contains_label_1 (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED) |
f279e190 | 13075 | { |
13076 | switch (TREE_CODE (*tp)) | |
13077 | { | |
13078 | case LABEL_EXPR: | |
13079 | return *tp; | |
2ba5b763 | 13080 | |
f279e190 | 13081 | case GOTO_EXPR: |
13082 | *walk_subtrees = 0; | |
2ba5b763 | 13083 | |
13084 | /* ... fall through ... */ | |
13085 | ||
f279e190 | 13086 | default: |
13087 | return NULL_TREE; | |
13088 | } | |
13089 | } | |
13090 | ||
2ba5b763 | 13091 | /* Return whether the sub-tree ST contains a label which is accessible from |
13092 | outside the sub-tree. */ | |
f279e190 | 13093 | |
13094 | static bool | |
13095 | contains_label_p (tree st) | |
13096 | { | |
2ba5b763 | 13097 | return |
13098 | (walk_tree_without_duplicates (&st, contains_label_1 , NULL) != NULL_TREE); | |
f279e190 | 13099 | } |
13100 | ||
6a4a1704 | 13101 | /* Fold a ternary expression of code CODE and type TYPE with operands |
13102 | OP0, OP1, and OP2. Return the folded expression if folding is | |
13103 | successful. Otherwise, return NULL_TREE. */ | |
6ce29c48 | 13104 | |
d3858e14 | 13105 | tree |
389dd41b | 13106 | fold_ternary_loc (location_t loc, enum tree_code code, tree type, |
13107 | tree op0, tree op1, tree op2) | |
6ce29c48 | 13108 | { |
6ce29c48 | 13109 | tree tem; |
13110 | tree arg0 = NULL_TREE, arg1 = NULL_TREE; | |
6ce29c48 | 13111 | enum tree_code_class kind = TREE_CODE_CLASS (code); |
6ce29c48 | 13112 | |
13113 | gcc_assert (IS_EXPR_CODE_CLASS (kind) | |
13114 | && TREE_CODE_LENGTH (code) == 3); | |
13115 | ||
213c530d | 13116 | /* Strip any conversions that don't change the mode. This is safe |
13117 | for every expression, except for a comparison expression because | |
13118 | its signedness is derived from its operands. So, in the latter | |
13119 | case, only strip conversions that don't change the signedness. | |
6ce29c48 | 13120 | |
213c530d | 13121 | Note that this is done as an internal manipulation within the |
13122 | constant folder, in order to find the simplest representation of | |
13123 | the arguments so that their form can be studied. In any cases, | |
13124 | the appropriate type conversions should be put back in the tree | |
13125 | that will get out of the constant folder. */ | |
13126 | if (op0) | |
13127 | { | |
13128 | arg0 = op0; | |
13129 | STRIP_NOPS (arg0); | |
13130 | } | |
6ce29c48 | 13131 | |
213c530d | 13132 | if (op1) |
13133 | { | |
13134 | arg1 = op1; | |
13135 | STRIP_NOPS (arg1); | |
6ce29c48 | 13136 | } |
13137 | ||
13138 | switch (code) | |
13139 | { | |
13140 | case COMPONENT_REF: | |
13141 | if (TREE_CODE (arg0) == CONSTRUCTOR | |
13142 | && ! type_contains_placeholder_p (TREE_TYPE (arg0))) | |
13143 | { | |
c75b4594 | 13144 | unsigned HOST_WIDE_INT idx; |
13145 | tree field, value; | |
13146 | FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (arg0), idx, field, value) | |
13147 | if (field == arg1) | |
13148 | return value; | |
6ce29c48 | 13149 | } |
e7edfbbd | 13150 | return NULL_TREE; |
6ce29c48 | 13151 | |
13152 | case COND_EXPR: | |
13153 | /* Pedantic ANSI C says that a conditional expression is never an lvalue, | |
13154 | so all simple results must be passed through pedantic_non_lvalue. */ | |
13155 | if (TREE_CODE (arg0) == INTEGER_CST) | |
13156 | { | |
f279e190 | 13157 | tree unused_op = integer_zerop (arg0) ? op1 : op2; |
213c530d | 13158 | tem = integer_zerop (arg0) ? op2 : op1; |
6ce29c48 | 13159 | /* Only optimize constant conditions when the selected branch |
13160 | has the same type as the COND_EXPR. This avoids optimizing | |
f279e190 | 13161 | away "c ? x : throw", where the throw has a void type. |
13162 | Avoid throwing away that operand which contains label. */ | |
13163 | if ((!TREE_SIDE_EFFECTS (unused_op) | |
13164 | || !contains_label_p (unused_op)) | |
13165 | && (! VOID_TYPE_P (TREE_TYPE (tem)) | |
13166 | || VOID_TYPE_P (type))) | |
389dd41b | 13167 | return pedantic_non_lvalue_loc (loc, tem); |
e7edfbbd | 13168 | return NULL_TREE; |
6ce29c48 | 13169 | } |
213c530d | 13170 | if (operand_equal_p (arg1, op2, 0)) |
389dd41b | 13171 | return pedantic_omit_one_operand_loc (loc, type, arg1, arg0); |
6ce29c48 | 13172 | |
13173 | /* If we have A op B ? A : C, we may be able to convert this to a | |
13174 | simpler expression, depending on the operation and the values | |
13175 | of B and C. Signed zeros prevent all of these transformations, | |
13176 | for reasons given above each one. | |
13177 | ||
13178 | Also try swapping the arguments and inverting the conditional. */ | |
13179 | if (COMPARISON_CLASS_P (arg0) | |
13180 | && operand_equal_for_comparison_p (TREE_OPERAND (arg0, 0), | |
13181 | arg1, TREE_OPERAND (arg0, 1)) | |
13182 | && !HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg1)))) | |
13183 | { | |
389dd41b | 13184 | tem = fold_cond_expr_with_comparison (loc, type, arg0, op1, op2); |
6ce29c48 | 13185 | if (tem) |
13186 | return tem; | |
13187 | } | |
13188 | ||
13189 | if (COMPARISON_CLASS_P (arg0) | |
13190 | && operand_equal_for_comparison_p (TREE_OPERAND (arg0, 0), | |
213c530d | 13191 | op2, |
6ce29c48 | 13192 | TREE_OPERAND (arg0, 1)) |
213c530d | 13193 | && !HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (op2)))) |
6ce29c48 | 13194 | { |
389dd41b | 13195 | tem = fold_truth_not_expr (loc, arg0); |
6758b11c | 13196 | if (tem && COMPARISON_CLASS_P (tem)) |
6ce29c48 | 13197 | { |
389dd41b | 13198 | tem = fold_cond_expr_with_comparison (loc, type, tem, op2, op1); |
6ce29c48 | 13199 | if (tem) |
13200 | return tem; | |
13201 | } | |
13202 | } | |
13203 | ||
13204 | /* If the second operand is simpler than the third, swap them | |
13205 | since that produces better jump optimization results. */ | |
51164bd6 | 13206 | if (truth_value_p (TREE_CODE (arg0)) |
13207 | && tree_swap_operands_p (op1, op2, false)) | |
6ce29c48 | 13208 | { |
13209 | /* See if this can be inverted. If it can't, possibly because | |
13210 | it was a floating-point inequality comparison, don't do | |
13211 | anything. */ | |
389dd41b | 13212 | tem = fold_truth_not_expr (loc, arg0); |
6758b11c | 13213 | if (tem) |
389dd41b | 13214 | return fold_build3_loc (loc, code, type, tem, op2, op1); |
6ce29c48 | 13215 | } |
13216 | ||
13217 | /* Convert A ? 1 : 0 to simply A. */ | |
213c530d | 13218 | if (integer_onep (op1) |
13219 | && integer_zerop (op2) | |
13220 | /* If we try to convert OP0 to our type, the | |
6ce29c48 | 13221 | call to fold will try to move the conversion inside |
13222 | a COND, which will recurse. In that case, the COND_EXPR | |
13223 | is probably the best choice, so leave it alone. */ | |
13224 | && type == TREE_TYPE (arg0)) | |
389dd41b | 13225 | return pedantic_non_lvalue_loc (loc, arg0); |
6ce29c48 | 13226 | |
13227 | /* Convert A ? 0 : 1 to !A. This prefers the use of NOT_EXPR | |
13228 | over COND_EXPR in cases such as floating point comparisons. */ | |
213c530d | 13229 | if (integer_zerop (op1) |
13230 | && integer_onep (op2) | |
6ce29c48 | 13231 | && truth_value_p (TREE_CODE (arg0))) |
389dd41b | 13232 | return pedantic_non_lvalue_loc (loc, |
13233 | fold_convert_loc (loc, type, | |
13234 | invert_truthvalue_loc (loc, | |
13235 | arg0))); | |
6ce29c48 | 13236 | |
13237 | /* A < 0 ? <sign bit of A> : 0 is simply (A & <sign bit of A>). */ | |
13238 | if (TREE_CODE (arg0) == LT_EXPR | |
71f1bd0c | 13239 | && integer_zerop (TREE_OPERAND (arg0, 1)) |
13240 | && integer_zerop (op2) | |
13241 | && (tem = sign_bit_p (TREE_OPERAND (arg0, 0), arg1))) | |
13242 | { | |
13243 | /* sign_bit_p only checks ARG1 bits within A's precision. | |
13244 | If <sign bit of A> has wider type than A, bits outside | |
13245 | of A's precision in <sign bit of A> need to be checked. | |
13246 | If they are all 0, this optimization needs to be done | |
13247 | in unsigned A's type, if they are all 1 in signed A's type, | |
13248 | otherwise this can't be done. */ | |
13249 | if (TYPE_PRECISION (TREE_TYPE (tem)) | |
13250 | < TYPE_PRECISION (TREE_TYPE (arg1)) | |
13251 | && TYPE_PRECISION (TREE_TYPE (tem)) | |
13252 | < TYPE_PRECISION (type)) | |
13253 | { | |
13254 | unsigned HOST_WIDE_INT mask_lo; | |
13255 | HOST_WIDE_INT mask_hi; | |
13256 | int inner_width, outer_width; | |
13257 | tree tem_type; | |
13258 | ||
13259 | inner_width = TYPE_PRECISION (TREE_TYPE (tem)); | |
13260 | outer_width = TYPE_PRECISION (TREE_TYPE (arg1)); | |
13261 | if (outer_width > TYPE_PRECISION (type)) | |
13262 | outer_width = TYPE_PRECISION (type); | |
13263 | ||
13264 | if (outer_width > HOST_BITS_PER_WIDE_INT) | |
13265 | { | |
13266 | mask_hi = ((unsigned HOST_WIDE_INT) -1 | |
13267 | >> (2 * HOST_BITS_PER_WIDE_INT - outer_width)); | |
13268 | mask_lo = -1; | |
13269 | } | |
13270 | else | |
13271 | { | |
13272 | mask_hi = 0; | |
13273 | mask_lo = ((unsigned HOST_WIDE_INT) -1 | |
13274 | >> (HOST_BITS_PER_WIDE_INT - outer_width)); | |
13275 | } | |
13276 | if (inner_width > HOST_BITS_PER_WIDE_INT) | |
13277 | { | |
13278 | mask_hi &= ~((unsigned HOST_WIDE_INT) -1 | |
13279 | >> (HOST_BITS_PER_WIDE_INT - inner_width)); | |
13280 | mask_lo = 0; | |
13281 | } | |
13282 | else | |
13283 | mask_lo &= ~((unsigned HOST_WIDE_INT) -1 | |
13284 | >> (HOST_BITS_PER_WIDE_INT - inner_width)); | |
13285 | ||
13286 | if ((TREE_INT_CST_HIGH (arg1) & mask_hi) == mask_hi | |
13287 | && (TREE_INT_CST_LOW (arg1) & mask_lo) == mask_lo) | |
13288 | { | |
11773141 | 13289 | tem_type = signed_type_for (TREE_TYPE (tem)); |
389dd41b | 13290 | tem = fold_convert_loc (loc, tem_type, tem); |
71f1bd0c | 13291 | } |
13292 | else if ((TREE_INT_CST_HIGH (arg1) & mask_hi) == 0 | |
13293 | && (TREE_INT_CST_LOW (arg1) & mask_lo) == 0) | |
13294 | { | |
71eea85c | 13295 | tem_type = unsigned_type_for (TREE_TYPE (tem)); |
389dd41b | 13296 | tem = fold_convert_loc (loc, tem_type, tem); |
71f1bd0c | 13297 | } |
13298 | else | |
13299 | tem = NULL; | |
13300 | } | |
13301 | ||
13302 | if (tem) | |
389dd41b | 13303 | return |
13304 | fold_convert_loc (loc, type, | |
13305 | fold_build2_loc (loc, BIT_AND_EXPR, | |
13306 | TREE_TYPE (tem), tem, | |
13307 | fold_convert_loc (loc, | |
13308 | TREE_TYPE (tem), | |
13309 | arg1))); | |
71f1bd0c | 13310 | } |
6ce29c48 | 13311 | |
13312 | /* (A >> N) & 1 ? (1 << N) : 0 is simply A & (1 << N). A & 1 was | |
13313 | already handled above. */ | |
13314 | if (TREE_CODE (arg0) == BIT_AND_EXPR | |
13315 | && integer_onep (TREE_OPERAND (arg0, 1)) | |
213c530d | 13316 | && integer_zerop (op2) |
6ce29c48 | 13317 | && integer_pow2p (arg1)) |
13318 | { | |
13319 | tree tem = TREE_OPERAND (arg0, 0); | |
13320 | STRIP_NOPS (tem); | |
13321 | if (TREE_CODE (tem) == RSHIFT_EXPR | |
13322 | && TREE_CODE (TREE_OPERAND (tem, 1)) == INTEGER_CST | |
13323 | && (unsigned HOST_WIDE_INT) tree_log2 (arg1) == | |
13324 | TREE_INT_CST_LOW (TREE_OPERAND (tem, 1))) | |
389dd41b | 13325 | return fold_build2_loc (loc, BIT_AND_EXPR, type, |
7ab7fd4f | 13326 | TREE_OPERAND (tem, 0), arg1); |
6ce29c48 | 13327 | } |
13328 | ||
13329 | /* A & N ? N : 0 is simply A & N if N is a power of two. This | |
13330 | is probably obsolete because the first operand should be a | |
13331 | truth value (that's why we have the two cases above), but let's | |
13332 | leave it in until we can confirm this for all front-ends. */ | |
213c530d | 13333 | if (integer_zerop (op2) |
6ce29c48 | 13334 | && TREE_CODE (arg0) == NE_EXPR |
13335 | && integer_zerop (TREE_OPERAND (arg0, 1)) | |
13336 | && integer_pow2p (arg1) | |
13337 | && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_AND_EXPR | |
13338 | && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1), | |
13339 | arg1, OEP_ONLY_CONST)) | |
389dd41b | 13340 | return pedantic_non_lvalue_loc (loc, |
13341 | fold_convert_loc (loc, type, | |
13342 | TREE_OPERAND (arg0, 0))); | |
6ce29c48 | 13343 | |
13344 | /* Convert A ? B : 0 into A && B if A and B are truth values. */ | |
213c530d | 13345 | if (integer_zerop (op2) |
6ce29c48 | 13346 | && truth_value_p (TREE_CODE (arg0)) |
13347 | && truth_value_p (TREE_CODE (arg1))) | |
389dd41b | 13348 | return fold_build2_loc (loc, TRUTH_ANDIF_EXPR, type, |
13349 | fold_convert_loc (loc, type, arg0), | |
a0d17866 | 13350 | arg1); |
6ce29c48 | 13351 | |
13352 | /* Convert A ? B : 1 into !A || B if A and B are truth values. */ | |
213c530d | 13353 | if (integer_onep (op2) |
6ce29c48 | 13354 | && truth_value_p (TREE_CODE (arg0)) |
13355 | && truth_value_p (TREE_CODE (arg1))) | |
13356 | { | |
13357 | /* Only perform transformation if ARG0 is easily inverted. */ | |
389dd41b | 13358 | tem = fold_truth_not_expr (loc, arg0); |
6758b11c | 13359 | if (tem) |
389dd41b | 13360 | return fold_build2_loc (loc, TRUTH_ORIF_EXPR, type, |
13361 | fold_convert_loc (loc, type, tem), | |
a0d17866 | 13362 | arg1); |
6ce29c48 | 13363 | } |
13364 | ||
13365 | /* Convert A ? 0 : B into !A && B if A and B are truth values. */ | |
13366 | if (integer_zerop (arg1) | |
13367 | && truth_value_p (TREE_CODE (arg0)) | |
213c530d | 13368 | && truth_value_p (TREE_CODE (op2))) |
6ce29c48 | 13369 | { |
13370 | /* Only perform transformation if ARG0 is easily inverted. */ | |
389dd41b | 13371 | tem = fold_truth_not_expr (loc, arg0); |
6758b11c | 13372 | if (tem) |
389dd41b | 13373 | return fold_build2_loc (loc, TRUTH_ANDIF_EXPR, type, |
13374 | fold_convert_loc (loc, type, tem), | |
a0d17866 | 13375 | op2); |
6ce29c48 | 13376 | } |
13377 | ||
13378 | /* Convert A ? 1 : B into A || B if A and B are truth values. */ | |
13379 | if (integer_onep (arg1) | |
13380 | && truth_value_p (TREE_CODE (arg0)) | |
213c530d | 13381 | && truth_value_p (TREE_CODE (op2))) |
389dd41b | 13382 | return fold_build2_loc (loc, TRUTH_ORIF_EXPR, type, |
13383 | fold_convert_loc (loc, type, arg0), | |
a0d17866 | 13384 | op2); |
6ce29c48 | 13385 | |
e7edfbbd | 13386 | return NULL_TREE; |
6ce29c48 | 13387 | |
13388 | case CALL_EXPR: | |
c2f47e15 | 13389 | /* CALL_EXPRs used to be ternary exprs. Catch any mistaken uses |
13390 | of fold_ternary on them. */ | |
13391 | gcc_unreachable (); | |
6ce29c48 | 13392 | |
b8ddd49b | 13393 | case BIT_FIELD_REF: |
3cbad267 | 13394 | if ((TREE_CODE (arg0) == VECTOR_CST |
13395 | || (TREE_CODE (arg0) == CONSTRUCTOR && TREE_CONSTANT (arg0))) | |
70d2daf7 | 13396 | && type == TREE_TYPE (TREE_TYPE (arg0))) |
b8ddd49b | 13397 | { |
13398 | unsigned HOST_WIDE_INT width = tree_low_cst (arg1, 1); | |
13399 | unsigned HOST_WIDE_INT idx = tree_low_cst (op2, 1); | |
13400 | ||
13401 | if (width != 0 | |
13402 | && simple_cst_equal (arg1, TYPE_SIZE (type)) == 1 | |
13403 | && (idx % width) == 0 | |
13404 | && (idx = idx / width) | |
13405 | < TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0))) | |
13406 | { | |
3cbad267 | 13407 | tree elements = NULL_TREE; |
13408 | ||
13409 | if (TREE_CODE (arg0) == VECTOR_CST) | |
13410 | elements = TREE_VECTOR_CST_ELTS (arg0); | |
13411 | else | |
13412 | { | |
13413 | unsigned HOST_WIDE_INT idx; | |
13414 | tree value; | |
13415 | ||
13416 | FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (arg0), idx, value) | |
13417 | elements = tree_cons (NULL_TREE, value, elements); | |
13418 | } | |
6349b545 | 13419 | while (idx-- > 0 && elements) |
b8ddd49b | 13420 | elements = TREE_CHAIN (elements); |
6349b545 | 13421 | if (elements) |
13422 | return TREE_VALUE (elements); | |
13423 | else | |
389dd41b | 13424 | return fold_convert_loc (loc, type, integer_zero_node); |
b8ddd49b | 13425 | } |
13426 | } | |
9e8a83b4 | 13427 | |
13428 | /* A bit-field-ref that referenced the full argument can be stripped. */ | |
13429 | if (INTEGRAL_TYPE_P (TREE_TYPE (arg0)) | |
13430 | && TYPE_PRECISION (TREE_TYPE (arg0)) == tree_low_cst (arg1, 1) | |
13431 | && integer_zerop (op2)) | |
389dd41b | 13432 | return fold_convert_loc (loc, type, arg0); |
9e8a83b4 | 13433 | |
b8ddd49b | 13434 | return NULL_TREE; |
13435 | ||
6ce29c48 | 13436 | default: |
e7edfbbd | 13437 | return NULL_TREE; |
6ce29c48 | 13438 | } /* switch (code) */ |
13439 | } | |
13440 | ||
2bc77e10 | 13441 | /* Perform constant folding and related simplification of EXPR. |
13442 | The related simplifications include x*1 => x, x*0 => 0, etc., | |
13443 | and application of the associative law. | |
13444 | NOP_EXPR conversions may be removed freely (as long as we | |
c4b03c0f | 13445 | are careful not to change the type of the overall expression). |
2bc77e10 | 13446 | We cannot simplify through a CONVERT_EXPR, FIX_EXPR or FLOAT_EXPR, |
13447 | but we can constant-fold them if they have constant operands. */ | |
13448 | ||
fc3df357 | 13449 | #ifdef ENABLE_FOLD_CHECKING |
13450 | # define fold(x) fold_1 (x) | |
13451 | static tree fold_1 (tree); | |
13452 | static | |
13453 | #endif | |
2bc77e10 | 13454 | tree |
de1b648b | 13455 | fold (tree expr) |
2bc77e10 | 13456 | { |
53f78329 | 13457 | const tree t = expr; |
19cb6b50 | 13458 | enum tree_code code = TREE_CODE (t); |
ce45a448 | 13459 | enum tree_code_class kind = TREE_CODE_CLASS (code); |
e7edfbbd | 13460 | tree tem; |
389dd41b | 13461 | location_t loc = EXPR_LOCATION (expr); |
4ee9c684 | 13462 | |
8541c166 | 13463 | /* Return right away if a constant. */ |
ce45a448 | 13464 | if (kind == tcc_constant) |
8541c166 | 13465 | return t; |
cc049fa3 | 13466 | |
c2f47e15 | 13467 | /* CALL_EXPR-like objects with variable numbers of operands are |
13468 | treated specially. */ | |
13469 | if (kind == tcc_vl_exp) | |
13470 | { | |
13471 | if (code == CALL_EXPR) | |
13472 | { | |
389dd41b | 13473 | tem = fold_call_expr (loc, expr, false); |
c2f47e15 | 13474 | return tem ? tem : expr; |
13475 | } | |
13476 | return expr; | |
13477 | } | |
13478 | ||
75a70cf9 | 13479 | if (IS_EXPR_CODE_CLASS (kind)) |
422c18cb | 13480 | { |
0052b98e | 13481 | tree type = TREE_TYPE (t); |
6a4a1704 | 13482 | tree op0, op1, op2; |
0052b98e | 13483 | |
422c18cb | 13484 | switch (TREE_CODE_LENGTH (code)) |
13485 | { | |
13486 | case 1: | |
0052b98e | 13487 | op0 = TREE_OPERAND (t, 0); |
389dd41b | 13488 | tem = fold_unary_loc (loc, code, type, op0); |
e7edfbbd | 13489 | return tem ? tem : expr; |
fef10b60 | 13490 | case 2: |
0052b98e | 13491 | op0 = TREE_OPERAND (t, 0); |
13492 | op1 = TREE_OPERAND (t, 1); | |
389dd41b | 13493 | tem = fold_binary_loc (loc, code, type, op0, op1); |
e7edfbbd | 13494 | return tem ? tem : expr; |
6ce29c48 | 13495 | case 3: |
6a4a1704 | 13496 | op0 = TREE_OPERAND (t, 0); |
13497 | op1 = TREE_OPERAND (t, 1); | |
13498 | op2 = TREE_OPERAND (t, 2); | |
389dd41b | 13499 | tem = fold_ternary_loc (loc, code, type, op0, op1, op2); |
e7edfbbd | 13500 | return tem ? tem : expr; |
422c18cb | 13501 | default: |
13502 | break; | |
13503 | } | |
13504 | } | |
13505 | ||
2bc77e10 | 13506 | switch (code) |
13507 | { | |
27e9f331 | 13508 | case ARRAY_REF: |
13509 | { | |
13510 | tree op0 = TREE_OPERAND (t, 0); | |
13511 | tree op1 = TREE_OPERAND (t, 1); | |
13512 | ||
13513 | if (TREE_CODE (op1) == INTEGER_CST | |
13514 | && TREE_CODE (op0) == CONSTRUCTOR | |
13515 | && ! type_contains_placeholder_p (TREE_TYPE (op0))) | |
13516 | { | |
13517 | VEC(constructor_elt,gc) *elts = CONSTRUCTOR_ELTS (op0); | |
13518 | unsigned HOST_WIDE_INT end = VEC_length (constructor_elt, elts); | |
13519 | unsigned HOST_WIDE_INT begin = 0; | |
13520 | ||
13521 | /* Find a matching index by means of a binary search. */ | |
13522 | while (begin != end) | |
13523 | { | |
13524 | unsigned HOST_WIDE_INT middle = (begin + end) / 2; | |
13525 | tree index = VEC_index (constructor_elt, elts, middle)->index; | |
13526 | ||
13527 | if (TREE_CODE (index) == INTEGER_CST | |
13528 | && tree_int_cst_lt (index, op1)) | |
13529 | begin = middle + 1; | |
13530 | else if (TREE_CODE (index) == INTEGER_CST | |
13531 | && tree_int_cst_lt (op1, index)) | |
13532 | end = middle; | |
13533 | else if (TREE_CODE (index) == RANGE_EXPR | |
13534 | && tree_int_cst_lt (TREE_OPERAND (index, 1), op1)) | |
13535 | begin = middle + 1; | |
13536 | else if (TREE_CODE (index) == RANGE_EXPR | |
13537 | && tree_int_cst_lt (op1, TREE_OPERAND (index, 0))) | |
13538 | end = middle; | |
13539 | else | |
13540 | return VEC_index (constructor_elt, elts, middle)->value; | |
13541 | } | |
13542 | } | |
13543 | ||
13544 | return t; | |
13545 | } | |
13546 | ||
2bc77e10 | 13547 | case CONST_DECL: |
13548 | return fold (DECL_INITIAL (t)); | |
13549 | ||
2bc77e10 | 13550 | default: |
13551 | return t; | |
13552 | } /* switch (code) */ | |
13553 | } | |
76a0ced5 | 13554 | |
fc3df357 | 13555 | #ifdef ENABLE_FOLD_CHECKING |
13556 | #undef fold | |
13557 | ||
b4b34335 | 13558 | static void fold_checksum_tree (const_tree, struct md5_ctx *, htab_t); |
13559 | static void fold_check_failed (const_tree, const_tree); | |
13560 | void print_fold_checksum (const_tree); | |
fc3df357 | 13561 | |
13562 | /* When --enable-checking=fold, compute a digest of expr before | |
13563 | and after actual fold call to see if fold did not accidentally | |
13564 | change original expr. */ | |
13565 | ||
13566 | tree | |
13567 | fold (tree expr) | |
13568 | { | |
13569 | tree ret; | |
13570 | struct md5_ctx ctx; | |
13571 | unsigned char checksum_before[16], checksum_after[16]; | |
13572 | htab_t ht; | |
13573 | ||
13574 | ht = htab_create (32, htab_hash_pointer, htab_eq_pointer, NULL); | |
13575 | md5_init_ctx (&ctx); | |
13576 | fold_checksum_tree (expr, &ctx, ht); | |
13577 | md5_finish_ctx (&ctx, checksum_before); | |
13578 | htab_empty (ht); | |
13579 | ||
13580 | ret = fold_1 (expr); | |
13581 | ||
13582 | md5_init_ctx (&ctx); | |
13583 | fold_checksum_tree (expr, &ctx, ht); | |
13584 | md5_finish_ctx (&ctx, checksum_after); | |
13585 | htab_delete (ht); | |
13586 | ||
13587 | if (memcmp (checksum_before, checksum_after, 16)) | |
13588 | fold_check_failed (expr, ret); | |
13589 | ||
13590 | return ret; | |
13591 | } | |
13592 | ||
13593 | void | |
b4b34335 | 13594 | print_fold_checksum (const_tree expr) |
fc3df357 | 13595 | { |
13596 | struct md5_ctx ctx; | |
13597 | unsigned char checksum[16], cnt; | |
13598 | htab_t ht; | |
13599 | ||
13600 | ht = htab_create (32, htab_hash_pointer, htab_eq_pointer, NULL); | |
13601 | md5_init_ctx (&ctx); | |
13602 | fold_checksum_tree (expr, &ctx, ht); | |
13603 | md5_finish_ctx (&ctx, checksum); | |
13604 | htab_delete (ht); | |
13605 | for (cnt = 0; cnt < 16; ++cnt) | |
13606 | fprintf (stderr, "%02x", checksum[cnt]); | |
13607 | putc ('\n', stderr); | |
13608 | } | |
13609 | ||
13610 | static void | |
b4b34335 | 13611 | fold_check_failed (const_tree expr ATTRIBUTE_UNUSED, const_tree ret ATTRIBUTE_UNUSED) |
fc3df357 | 13612 | { |
13613 | internal_error ("fold check: original tree changed by fold"); | |
13614 | } | |
13615 | ||
13616 | static void | |
b4b34335 | 13617 | fold_checksum_tree (const_tree expr, struct md5_ctx *ctx, htab_t ht) |
fc3df357 | 13618 | { |
b4b34335 | 13619 | const void **slot; |
fc3df357 | 13620 | enum tree_code code; |
7718e3c3 | 13621 | union tree_node buf; |
fc3df357 | 13622 | int i, len; |
48e1416a | 13623 | |
b732b5f3 | 13624 | recursive_label: |
fc3df357 | 13625 | |
fdada98f | 13626 | gcc_assert ((sizeof (struct tree_exp) + 5 * sizeof (tree) |
68239da4 | 13627 | <= sizeof (struct tree_function_decl)) |
13628 | && sizeof (struct tree_type) <= sizeof (struct tree_function_decl)); | |
fc3df357 | 13629 | if (expr == NULL) |
13630 | return; | |
b4b34335 | 13631 | slot = (const void **) htab_find_slot (ht, expr, INSERT); |
fc3df357 | 13632 | if (*slot != NULL) |
13633 | return; | |
13634 | *slot = expr; | |
13635 | code = TREE_CODE (expr); | |
ce45a448 | 13636 | if (TREE_CODE_CLASS (code) == tcc_declaration |
13637 | && DECL_ASSEMBLER_NAME_SET_P (expr)) | |
fc3df357 | 13638 | { |
13639 | /* Allow DECL_ASSEMBLER_NAME to be modified. */ | |
28aefcdd | 13640 | memcpy ((char *) &buf, expr, tree_size (expr)); |
b4b34335 | 13641 | SET_DECL_ASSEMBLER_NAME ((tree)&buf, NULL); |
28aefcdd | 13642 | expr = (tree) &buf; |
fc3df357 | 13643 | } |
ce45a448 | 13644 | else if (TREE_CODE_CLASS (code) == tcc_type |
4edf9595 | 13645 | && (TYPE_POINTER_TO (expr) |
13646 | || TYPE_REFERENCE_TO (expr) | |
b732b5f3 | 13647 | || TYPE_CACHED_VALUES_P (expr) |
4edf9595 | 13648 | || TYPE_CONTAINS_PLACEHOLDER_INTERNAL (expr) |
13649 | || TYPE_NEXT_VARIANT (expr))) | |
fc3df357 | 13650 | { |
6b29892c | 13651 | /* Allow these fields to be modified. */ |
b4b34335 | 13652 | tree tmp; |
28aefcdd | 13653 | memcpy ((char *) &buf, expr, tree_size (expr)); |
b4b34335 | 13654 | expr = tmp = (tree) &buf; |
13655 | TYPE_CONTAINS_PLACEHOLDER_INTERNAL (tmp) = 0; | |
13656 | TYPE_POINTER_TO (tmp) = NULL; | |
13657 | TYPE_REFERENCE_TO (tmp) = NULL; | |
4edf9595 | 13658 | TYPE_NEXT_VARIANT (tmp) = NULL; |
b4b34335 | 13659 | if (TYPE_CACHED_VALUES_P (tmp)) |
1e612ca4 | 13660 | { |
b4b34335 | 13661 | TYPE_CACHED_VALUES_P (tmp) = 0; |
13662 | TYPE_CACHED_VALUES (tmp) = NULL; | |
1e612ca4 | 13663 | } |
fc3df357 | 13664 | } |
13665 | md5_process_bytes (expr, tree_size (expr), ctx); | |
13666 | fold_checksum_tree (TREE_TYPE (expr), ctx, ht); | |
ce45a448 | 13667 | if (TREE_CODE_CLASS (code) != tcc_type |
b732b5f3 | 13668 | && TREE_CODE_CLASS (code) != tcc_declaration |
2bf4108d | 13669 | && code != TREE_LIST |
13670 | && code != SSA_NAME) | |
fc3df357 | 13671 | fold_checksum_tree (TREE_CHAIN (expr), ctx, ht); |
fc3df357 | 13672 | switch (TREE_CODE_CLASS (code)) |
13673 | { | |
ce45a448 | 13674 | case tcc_constant: |
fc3df357 | 13675 | switch (code) |
13676 | { | |
13677 | case STRING_CST: | |
13678 | md5_process_bytes (TREE_STRING_POINTER (expr), | |
13679 | TREE_STRING_LENGTH (expr), ctx); | |
13680 | break; | |
13681 | case COMPLEX_CST: | |
13682 | fold_checksum_tree (TREE_REALPART (expr), ctx, ht); | |
13683 | fold_checksum_tree (TREE_IMAGPART (expr), ctx, ht); | |
13684 | break; | |
13685 | case VECTOR_CST: | |
13686 | fold_checksum_tree (TREE_VECTOR_CST_ELTS (expr), ctx, ht); | |
13687 | break; | |
13688 | default: | |
13689 | break; | |
13690 | } | |
13691 | break; | |
ce45a448 | 13692 | case tcc_exceptional: |
fc3df357 | 13693 | switch (code) |
13694 | { | |
13695 | case TREE_LIST: | |
13696 | fold_checksum_tree (TREE_PURPOSE (expr), ctx, ht); | |
13697 | fold_checksum_tree (TREE_VALUE (expr), ctx, ht); | |
b732b5f3 | 13698 | expr = TREE_CHAIN (expr); |
13699 | goto recursive_label; | |
fc3df357 | 13700 | break; |
13701 | case TREE_VEC: | |
13702 | for (i = 0; i < TREE_VEC_LENGTH (expr); ++i) | |
13703 | fold_checksum_tree (TREE_VEC_ELT (expr, i), ctx, ht); | |
13704 | break; | |
13705 | default: | |
13706 | break; | |
13707 | } | |
13708 | break; | |
ce45a448 | 13709 | case tcc_expression: |
13710 | case tcc_reference: | |
13711 | case tcc_comparison: | |
13712 | case tcc_unary: | |
13713 | case tcc_binary: | |
13714 | case tcc_statement: | |
c2f47e15 | 13715 | case tcc_vl_exp: |
13716 | len = TREE_OPERAND_LENGTH (expr); | |
fc3df357 | 13717 | for (i = 0; i < len; ++i) |
13718 | fold_checksum_tree (TREE_OPERAND (expr, i), ctx, ht); | |
13719 | break; | |
ce45a448 | 13720 | case tcc_declaration: |
fc3df357 | 13721 | fold_checksum_tree (DECL_NAME (expr), ctx, ht); |
13722 | fold_checksum_tree (DECL_CONTEXT (expr), ctx, ht); | |
aa4936de | 13723 | if (CODE_CONTAINS_STRUCT (TREE_CODE (expr), TS_DECL_COMMON)) |
13724 | { | |
13725 | fold_checksum_tree (DECL_SIZE (expr), ctx, ht); | |
13726 | fold_checksum_tree (DECL_SIZE_UNIT (expr), ctx, ht); | |
13727 | fold_checksum_tree (DECL_INITIAL (expr), ctx, ht); | |
13728 | fold_checksum_tree (DECL_ABSTRACT_ORIGIN (expr), ctx, ht); | |
13729 | fold_checksum_tree (DECL_ATTRIBUTES (expr), ctx, ht); | |
13730 | } | |
68239da4 | 13731 | if (CODE_CONTAINS_STRUCT (TREE_CODE (expr), TS_DECL_WITH_VIS)) |
13732 | fold_checksum_tree (DECL_SECTION_NAME (expr), ctx, ht); | |
48e1416a | 13733 | |
68239da4 | 13734 | if (CODE_CONTAINS_STRUCT (TREE_CODE (expr), TS_DECL_NON_COMMON)) |
13735 | { | |
13736 | fold_checksum_tree (DECL_VINDEX (expr), ctx, ht); | |
13737 | fold_checksum_tree (DECL_RESULT_FLD (expr), ctx, ht); | |
13738 | fold_checksum_tree (DECL_ARGUMENT_FLD (expr), ctx, ht); | |
13739 | } | |
fc3df357 | 13740 | break; |
ce45a448 | 13741 | case tcc_type: |
419ec660 | 13742 | if (TREE_CODE (expr) == ENUMERAL_TYPE) |
13743 | fold_checksum_tree (TYPE_VALUES (expr), ctx, ht); | |
fc3df357 | 13744 | fold_checksum_tree (TYPE_SIZE (expr), ctx, ht); |
13745 | fold_checksum_tree (TYPE_SIZE_UNIT (expr), ctx, ht); | |
13746 | fold_checksum_tree (TYPE_ATTRIBUTES (expr), ctx, ht); | |
13747 | fold_checksum_tree (TYPE_NAME (expr), ctx, ht); | |
419ec660 | 13748 | if (INTEGRAL_TYPE_P (expr) |
13749 | || SCALAR_FLOAT_TYPE_P (expr)) | |
13750 | { | |
13751 | fold_checksum_tree (TYPE_MIN_VALUE (expr), ctx, ht); | |
13752 | fold_checksum_tree (TYPE_MAX_VALUE (expr), ctx, ht); | |
13753 | } | |
fc3df357 | 13754 | fold_checksum_tree (TYPE_MAIN_VARIANT (expr), ctx, ht); |
6b29892c | 13755 | if (TREE_CODE (expr) == RECORD_TYPE |
13756 | || TREE_CODE (expr) == UNION_TYPE | |
13757 | || TREE_CODE (expr) == QUAL_UNION_TYPE) | |
13758 | fold_checksum_tree (TYPE_BINFO (expr), ctx, ht); | |
fc3df357 | 13759 | fold_checksum_tree (TYPE_CONTEXT (expr), ctx, ht); |
13760 | break; | |
13761 | default: | |
13762 | break; | |
13763 | } | |
13764 | } | |
13765 | ||
394e718d | 13766 | /* Helper function for outputting the checksum of a tree T. When |
13767 | debugging with gdb, you can "define mynext" to be "next" followed | |
13768 | by "call debug_fold_checksum (op0)", then just trace down till the | |
13769 | outputs differ. */ | |
13770 | ||
4b987fac | 13771 | DEBUG_FUNCTION void |
b4b34335 | 13772 | debug_fold_checksum (const_tree t) |
394e718d | 13773 | { |
13774 | int i; | |
13775 | unsigned char checksum[16]; | |
13776 | struct md5_ctx ctx; | |
13777 | htab_t ht = htab_create (32, htab_hash_pointer, htab_eq_pointer, NULL); | |
48e1416a | 13778 | |
394e718d | 13779 | md5_init_ctx (&ctx); |
13780 | fold_checksum_tree (t, &ctx, ht); | |
13781 | md5_finish_ctx (&ctx, checksum); | |
13782 | htab_empty (ht); | |
13783 | ||
13784 | for (i = 0; i < 16; i++) | |
13785 | fprintf (stderr, "%d ", checksum[i]); | |
13786 | ||
13787 | fprintf (stderr, "\n"); | |
13788 | } | |
13789 | ||
fc3df357 | 13790 | #endif |
13791 | ||
cfd3d1cc | 13792 | /* Fold a unary tree expression with code CODE of type TYPE with an |
389dd41b | 13793 | operand OP0. LOC is the location of the resulting expression. |
13794 | Return a folded expression if successful. Otherwise, return a tree | |
13795 | expression with code CODE of type TYPE with an operand OP0. */ | |
cfd3d1cc | 13796 | |
13797 | tree | |
389dd41b | 13798 | fold_build1_stat_loc (location_t loc, |
13799 | enum tree_code code, tree type, tree op0 MEM_STAT_DECL) | |
cfd3d1cc | 13800 | { |
e6e279fe | 13801 | tree tem; |
13802 | #ifdef ENABLE_FOLD_CHECKING | |
13803 | unsigned char checksum_before[16], checksum_after[16]; | |
13804 | struct md5_ctx ctx; | |
13805 | htab_t ht; | |
13806 | ||
13807 | ht = htab_create (32, htab_hash_pointer, htab_eq_pointer, NULL); | |
13808 | md5_init_ctx (&ctx); | |
13809 | fold_checksum_tree (op0, &ctx, ht); | |
13810 | md5_finish_ctx (&ctx, checksum_before); | |
13811 | htab_empty (ht); | |
13812 | #endif | |
48e1416a | 13813 | |
389dd41b | 13814 | tem = fold_unary_loc (loc, code, type, op0); |
e6e279fe | 13815 | if (!tem) |
389dd41b | 13816 | { |
13817 | tem = build1_stat (code, type, op0 PASS_MEM_STAT); | |
13818 | SET_EXPR_LOCATION (tem, loc); | |
13819 | } | |
48e1416a | 13820 | |
e6e279fe | 13821 | #ifdef ENABLE_FOLD_CHECKING |
13822 | md5_init_ctx (&ctx); | |
13823 | fold_checksum_tree (op0, &ctx, ht); | |
13824 | md5_finish_ctx (&ctx, checksum_after); | |
13825 | htab_delete (ht); | |
cfd3d1cc | 13826 | |
e6e279fe | 13827 | if (memcmp (checksum_before, checksum_after, 16)) |
13828 | fold_check_failed (op0, tem); | |
13829 | #endif | |
13830 | return tem; | |
cfd3d1cc | 13831 | } |
13832 | ||
13833 | /* Fold a binary tree expression with code CODE of type TYPE with | |
389dd41b | 13834 | operands OP0 and OP1. LOC is the location of the resulting |
13835 | expression. Return a folded expression if successful. Otherwise, | |
13836 | return a tree expression with code CODE of type TYPE with operands | |
13837 | OP0 and OP1. */ | |
cfd3d1cc | 13838 | |
13839 | tree | |
389dd41b | 13840 | fold_build2_stat_loc (location_t loc, |
13841 | enum tree_code code, tree type, tree op0, tree op1 | |
13842 | MEM_STAT_DECL) | |
cfd3d1cc | 13843 | { |
e6e279fe | 13844 | tree tem; |
13845 | #ifdef ENABLE_FOLD_CHECKING | |
13846 | unsigned char checksum_before_op0[16], | |
13847 | checksum_before_op1[16], | |
13848 | checksum_after_op0[16], | |
13849 | checksum_after_op1[16]; | |
13850 | struct md5_ctx ctx; | |
13851 | htab_t ht; | |
13852 | ||
13853 | ht = htab_create (32, htab_hash_pointer, htab_eq_pointer, NULL); | |
13854 | md5_init_ctx (&ctx); | |
13855 | fold_checksum_tree (op0, &ctx, ht); | |
13856 | md5_finish_ctx (&ctx, checksum_before_op0); | |
13857 | htab_empty (ht); | |
13858 | ||
13859 | md5_init_ctx (&ctx); | |
13860 | fold_checksum_tree (op1, &ctx, ht); | |
13861 | md5_finish_ctx (&ctx, checksum_before_op1); | |
13862 | htab_empty (ht); | |
13863 | #endif | |
13864 | ||
389dd41b | 13865 | tem = fold_binary_loc (loc, code, type, op0, op1); |
e6e279fe | 13866 | if (!tem) |
389dd41b | 13867 | { |
13868 | tem = build2_stat (code, type, op0, op1 PASS_MEM_STAT); | |
13869 | SET_EXPR_LOCATION (tem, loc); | |
13870 | } | |
48e1416a | 13871 | |
e6e279fe | 13872 | #ifdef ENABLE_FOLD_CHECKING |
13873 | md5_init_ctx (&ctx); | |
13874 | fold_checksum_tree (op0, &ctx, ht); | |
13875 | md5_finish_ctx (&ctx, checksum_after_op0); | |
13876 | htab_empty (ht); | |
13877 | ||
13878 | if (memcmp (checksum_before_op0, checksum_after_op0, 16)) | |
13879 | fold_check_failed (op0, tem); | |
48e1416a | 13880 | |
e6e279fe | 13881 | md5_init_ctx (&ctx); |
13882 | fold_checksum_tree (op1, &ctx, ht); | |
13883 | md5_finish_ctx (&ctx, checksum_after_op1); | |
13884 | htab_delete (ht); | |
cfd3d1cc | 13885 | |
e6e279fe | 13886 | if (memcmp (checksum_before_op1, checksum_after_op1, 16)) |
13887 | fold_check_failed (op1, tem); | |
13888 | #endif | |
13889 | return tem; | |
cfd3d1cc | 13890 | } |
13891 | ||
13892 | /* Fold a ternary tree expression with code CODE of type TYPE with | |
977b7486 | 13893 | operands OP0, OP1, and OP2. Return a folded expression if |
cfd3d1cc | 13894 | successful. Otherwise, return a tree expression with code CODE of |
13895 | type TYPE with operands OP0, OP1, and OP2. */ | |
13896 | ||
13897 | tree | |
389dd41b | 13898 | fold_build3_stat_loc (location_t loc, enum tree_code code, tree type, |
13899 | tree op0, tree op1, tree op2 MEM_STAT_DECL) | |
ba04ccb0 | 13900 | { |
13901 | tree tem; | |
e6e279fe | 13902 | #ifdef ENABLE_FOLD_CHECKING |
13903 | unsigned char checksum_before_op0[16], | |
13904 | checksum_before_op1[16], | |
13905 | checksum_before_op2[16], | |
13906 | checksum_after_op0[16], | |
13907 | checksum_after_op1[16], | |
13908 | checksum_after_op2[16]; | |
13909 | struct md5_ctx ctx; | |
13910 | htab_t ht; | |
13911 | ||
13912 | ht = htab_create (32, htab_hash_pointer, htab_eq_pointer, NULL); | |
13913 | md5_init_ctx (&ctx); | |
13914 | fold_checksum_tree (op0, &ctx, ht); | |
13915 | md5_finish_ctx (&ctx, checksum_before_op0); | |
13916 | htab_empty (ht); | |
cfd3d1cc | 13917 | |
e6e279fe | 13918 | md5_init_ctx (&ctx); |
13919 | fold_checksum_tree (op1, &ctx, ht); | |
13920 | md5_finish_ctx (&ctx, checksum_before_op1); | |
13921 | htab_empty (ht); | |
13922 | ||
13923 | md5_init_ctx (&ctx); | |
13924 | fold_checksum_tree (op2, &ctx, ht); | |
13925 | md5_finish_ctx (&ctx, checksum_before_op2); | |
13926 | htab_empty (ht); | |
13927 | #endif | |
c2f47e15 | 13928 | |
13929 | gcc_assert (TREE_CODE_CLASS (code) != tcc_vl_exp); | |
389dd41b | 13930 | tem = fold_ternary_loc (loc, code, type, op0, op1, op2); |
e6e279fe | 13931 | if (!tem) |
389dd41b | 13932 | { |
13933 | tem = build3_stat (code, type, op0, op1, op2 PASS_MEM_STAT); | |
13934 | SET_EXPR_LOCATION (tem, loc); | |
13935 | } | |
48e1416a | 13936 | |
e6e279fe | 13937 | #ifdef ENABLE_FOLD_CHECKING |
13938 | md5_init_ctx (&ctx); | |
13939 | fold_checksum_tree (op0, &ctx, ht); | |
13940 | md5_finish_ctx (&ctx, checksum_after_op0); | |
13941 | htab_empty (ht); | |
13942 | ||
13943 | if (memcmp (checksum_before_op0, checksum_after_op0, 16)) | |
13944 | fold_check_failed (op0, tem); | |
48e1416a | 13945 | |
e6e279fe | 13946 | md5_init_ctx (&ctx); |
13947 | fold_checksum_tree (op1, &ctx, ht); | |
13948 | md5_finish_ctx (&ctx, checksum_after_op1); | |
13949 | htab_empty (ht); | |
13950 | ||
13951 | if (memcmp (checksum_before_op1, checksum_after_op1, 16)) | |
13952 | fold_check_failed (op1, tem); | |
48e1416a | 13953 | |
e6e279fe | 13954 | md5_init_ctx (&ctx); |
13955 | fold_checksum_tree (op2, &ctx, ht); | |
13956 | md5_finish_ctx (&ctx, checksum_after_op2); | |
13957 | htab_delete (ht); | |
13958 | ||
13959 | if (memcmp (checksum_before_op2, checksum_after_op2, 16)) | |
13960 | fold_check_failed (op2, tem); | |
13961 | #endif | |
13962 | return tem; | |
cfd3d1cc | 13963 | } |
13964 | ||
d01f58f9 | 13965 | /* Fold a CALL_EXPR expression of type TYPE with operands FN and NARGS |
13966 | arguments in ARGARRAY, and a null static chain. | |
c2f47e15 | 13967 | Return a folded expression if successful. Otherwise, return a CALL_EXPR |
d01f58f9 | 13968 | of type TYPE from the given operands as constructed by build_call_array. */ |
c2f47e15 | 13969 | |
13970 | tree | |
389dd41b | 13971 | fold_build_call_array_loc (location_t loc, tree type, tree fn, |
13972 | int nargs, tree *argarray) | |
c2f47e15 | 13973 | { |
13974 | tree tem; | |
13975 | #ifdef ENABLE_FOLD_CHECKING | |
13976 | unsigned char checksum_before_fn[16], | |
13977 | checksum_before_arglist[16], | |
13978 | checksum_after_fn[16], | |
13979 | checksum_after_arglist[16]; | |
13980 | struct md5_ctx ctx; | |
13981 | htab_t ht; | |
d01f58f9 | 13982 | int i; |
c2f47e15 | 13983 | |
13984 | ht = htab_create (32, htab_hash_pointer, htab_eq_pointer, NULL); | |
13985 | md5_init_ctx (&ctx); | |
13986 | fold_checksum_tree (fn, &ctx, ht); | |
13987 | md5_finish_ctx (&ctx, checksum_before_fn); | |
13988 | htab_empty (ht); | |
13989 | ||
13990 | md5_init_ctx (&ctx); | |
d01f58f9 | 13991 | for (i = 0; i < nargs; i++) |
13992 | fold_checksum_tree (argarray[i], &ctx, ht); | |
c2f47e15 | 13993 | md5_finish_ctx (&ctx, checksum_before_arglist); |
13994 | htab_empty (ht); | |
13995 | #endif | |
13996 | ||
389dd41b | 13997 | tem = fold_builtin_call_array (loc, type, fn, nargs, argarray); |
48e1416a | 13998 | |
c2f47e15 | 13999 | #ifdef ENABLE_FOLD_CHECKING |
14000 | md5_init_ctx (&ctx); | |
14001 | fold_checksum_tree (fn, &ctx, ht); | |
14002 | md5_finish_ctx (&ctx, checksum_after_fn); | |
14003 | htab_empty (ht); | |
14004 | ||
14005 | if (memcmp (checksum_before_fn, checksum_after_fn, 16)) | |
14006 | fold_check_failed (fn, tem); | |
48e1416a | 14007 | |
c2f47e15 | 14008 | md5_init_ctx (&ctx); |
d01f58f9 | 14009 | for (i = 0; i < nargs; i++) |
14010 | fold_checksum_tree (argarray[i], &ctx, ht); | |
c2f47e15 | 14011 | md5_finish_ctx (&ctx, checksum_after_arglist); |
14012 | htab_delete (ht); | |
14013 | ||
14014 | if (memcmp (checksum_before_arglist, checksum_after_arglist, 16)) | |
d01f58f9 | 14015 | fold_check_failed (NULL_TREE, tem); |
c2f47e15 | 14016 | #endif |
14017 | return tem; | |
14018 | } | |
14019 | ||
91c82c20 | 14020 | /* Perform constant folding and related simplification of initializer |
a62b6979 | 14021 | expression EXPR. These behave identically to "fold_buildN" but ignore |
276beea2 | 14022 | potential run-time traps and exceptions that fold must preserve. */ |
14023 | ||
a62b6979 | 14024 | #define START_FOLD_INIT \ |
14025 | int saved_signaling_nans = flag_signaling_nans;\ | |
14026 | int saved_trapping_math = flag_trapping_math;\ | |
14027 | int saved_rounding_math = flag_rounding_math;\ | |
14028 | int saved_trapv = flag_trapv;\ | |
47be647d | 14029 | int saved_folding_initializer = folding_initializer;\ |
a62b6979 | 14030 | flag_signaling_nans = 0;\ |
14031 | flag_trapping_math = 0;\ | |
14032 | flag_rounding_math = 0;\ | |
47be647d | 14033 | flag_trapv = 0;\ |
14034 | folding_initializer = 1; | |
a62b6979 | 14035 | |
14036 | #define END_FOLD_INIT \ | |
14037 | flag_signaling_nans = saved_signaling_nans;\ | |
14038 | flag_trapping_math = saved_trapping_math;\ | |
14039 | flag_rounding_math = saved_rounding_math;\ | |
47be647d | 14040 | flag_trapv = saved_trapv;\ |
14041 | folding_initializer = saved_folding_initializer; | |
a62b6979 | 14042 | |
14043 | tree | |
389dd41b | 14044 | fold_build1_initializer_loc (location_t loc, enum tree_code code, |
14045 | tree type, tree op) | |
a62b6979 | 14046 | { |
14047 | tree result; | |
14048 | START_FOLD_INIT; | |
14049 | ||
389dd41b | 14050 | result = fold_build1_loc (loc, code, type, op); |
a62b6979 | 14051 | |
14052 | END_FOLD_INIT; | |
14053 | return result; | |
14054 | } | |
14055 | ||
276beea2 | 14056 | tree |
389dd41b | 14057 | fold_build2_initializer_loc (location_t loc, enum tree_code code, |
14058 | tree type, tree op0, tree op1) | |
276beea2 | 14059 | { |
276beea2 | 14060 | tree result; |
a62b6979 | 14061 | START_FOLD_INIT; |
14062 | ||
389dd41b | 14063 | result = fold_build2_loc (loc, code, type, op0, op1); |
276beea2 | 14064 | |
a62b6979 | 14065 | END_FOLD_INIT; |
14066 | return result; | |
14067 | } | |
276beea2 | 14068 | |
a62b6979 | 14069 | tree |
389dd41b | 14070 | fold_build3_initializer_loc (location_t loc, enum tree_code code, |
14071 | tree type, tree op0, tree op1, tree op2) | |
a62b6979 | 14072 | { |
14073 | tree result; | |
14074 | START_FOLD_INIT; | |
276beea2 | 14075 | |
389dd41b | 14076 | result = fold_build3_loc (loc, code, type, op0, op1, op2); |
276beea2 | 14077 | |
a62b6979 | 14078 | END_FOLD_INIT; |
276beea2 | 14079 | return result; |
14080 | } | |
14081 | ||
c2f47e15 | 14082 | tree |
389dd41b | 14083 | fold_build_call_array_initializer_loc (location_t loc, tree type, tree fn, |
14084 | int nargs, tree *argarray) | |
c2f47e15 | 14085 | { |
14086 | tree result; | |
14087 | START_FOLD_INIT; | |
14088 | ||
389dd41b | 14089 | result = fold_build_call_array_loc (loc, type, fn, nargs, argarray); |
c2f47e15 | 14090 | |
14091 | END_FOLD_INIT; | |
14092 | return result; | |
14093 | } | |
14094 | ||
a62b6979 | 14095 | #undef START_FOLD_INIT |
14096 | #undef END_FOLD_INIT | |
14097 | ||
7014838c | 14098 | /* Determine if first argument is a multiple of second argument. Return 0 if |
14099 | it is not, or we cannot easily determined it to be. | |
76a0ced5 | 14100 | |
7014838c | 14101 | An example of the sort of thing we care about (at this point; this routine |
14102 | could surely be made more general, and expanded to do what the *_DIV_EXPR's | |
14103 | fold cases do now) is discovering that | |
76a0ced5 | 14104 | |
14105 | SAVE_EXPR (I) * SAVE_EXPR (J * 8) | |
14106 | ||
14107 | is a multiple of | |
14108 | ||
14109 | SAVE_EXPR (J * 8) | |
14110 | ||
7014838c | 14111 | when we know that the two SAVE_EXPR (J * 8) nodes are the same node. |
76a0ced5 | 14112 | |
14113 | This code also handles discovering that | |
14114 | ||
14115 | SAVE_EXPR (I) * SAVE_EXPR (J * 8) | |
14116 | ||
7014838c | 14117 | is a multiple of 8 so we don't have to worry about dealing with a |
76a0ced5 | 14118 | possible remainder. |
14119 | ||
7014838c | 14120 | Note that we *look* inside a SAVE_EXPR only to determine how it was |
14121 | calculated; it is not safe for fold to do much of anything else with the | |
14122 | internals of a SAVE_EXPR, since it cannot know when it will be evaluated | |
14123 | at run time. For example, the latter example above *cannot* be implemented | |
14124 | as SAVE_EXPR (I) * J or any variant thereof, since the value of J at | |
14125 | evaluation time of the original SAVE_EXPR is not necessarily the same at | |
14126 | the time the new expression is evaluated. The only optimization of this | |
76a0ced5 | 14127 | sort that would be valid is changing |
14128 | ||
14129 | SAVE_EXPR (I) * SAVE_EXPR (SAVE_EXPR (J) * 8) | |
76a0ced5 | 14130 | |
7014838c | 14131 | divided by 8 to |
76a0ced5 | 14132 | |
14133 | SAVE_EXPR (I) * SAVE_EXPR (J) | |
14134 | ||
14135 | (where the same SAVE_EXPR (J) is used in the original and the | |
14136 | transformed version). */ | |
14137 | ||
96b038b0 | 14138 | int |
b4b34335 | 14139 | multiple_of_p (tree type, const_tree top, const_tree bottom) |
76a0ced5 | 14140 | { |
14141 | if (operand_equal_p (top, bottom, 0)) | |
14142 | return 1; | |
14143 | ||
14144 | if (TREE_CODE (type) != INTEGER_TYPE) | |
14145 | return 0; | |
14146 | ||
14147 | switch (TREE_CODE (top)) | |
14148 | { | |
d5dd61a2 | 14149 | case BIT_AND_EXPR: |
14150 | /* Bitwise and provides a power of two multiple. If the mask is | |
14151 | a multiple of BOTTOM then TOP is a multiple of BOTTOM. */ | |
14152 | if (!integer_pow2p (bottom)) | |
14153 | return 0; | |
14154 | /* FALLTHRU */ | |
14155 | ||
76a0ced5 | 14156 | case MULT_EXPR: |
14157 | return (multiple_of_p (type, TREE_OPERAND (top, 0), bottom) | |
14158 | || multiple_of_p (type, TREE_OPERAND (top, 1), bottom)); | |
14159 | ||
14160 | case PLUS_EXPR: | |
14161 | case MINUS_EXPR: | |
14162 | return (multiple_of_p (type, TREE_OPERAND (top, 0), bottom) | |
14163 | && multiple_of_p (type, TREE_OPERAND (top, 1), bottom)); | |
14164 | ||
17e3940f | 14165 | case LSHIFT_EXPR: |
14166 | if (TREE_CODE (TREE_OPERAND (top, 1)) == INTEGER_CST) | |
14167 | { | |
14168 | tree op1, t1; | |
14169 | ||
14170 | op1 = TREE_OPERAND (top, 1); | |
14171 | /* const_binop may not detect overflow correctly, | |
14172 | so check for it explicitly here. */ | |
14173 | if (TYPE_PRECISION (TREE_TYPE (size_one_node)) | |
14174 | > TREE_INT_CST_LOW (op1) | |
14175 | && TREE_INT_CST_HIGH (op1) == 0 | |
b30e3dbc | 14176 | && 0 != (t1 = fold_convert (type, |
14177 | const_binop (LSHIFT_EXPR, | |
14178 | size_one_node, | |
14179 | op1, 0))) | |
f96bd2bf | 14180 | && !TREE_OVERFLOW (t1)) |
17e3940f | 14181 | return multiple_of_p (type, t1, bottom); |
14182 | } | |
14183 | return 0; | |
14184 | ||
76a0ced5 | 14185 | case NOP_EXPR: |
7014838c | 14186 | /* Can't handle conversions from non-integral or wider integral type. */ |
76a0ced5 | 14187 | if ((TREE_CODE (TREE_TYPE (TREE_OPERAND (top, 0))) != INTEGER_TYPE) |
14188 | || (TYPE_PRECISION (type) | |
14189 | < TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (top, 0))))) | |
14190 | return 0; | |
7014838c | 14191 | |
6312a35e | 14192 | /* .. fall through ... */ |
7014838c | 14193 | |
76a0ced5 | 14194 | case SAVE_EXPR: |
14195 | return multiple_of_p (type, TREE_OPERAND (top, 0), bottom); | |
14196 | ||
c6feb9f1 | 14197 | case COND_EXPR: |
14198 | return (multiple_of_p (type, TREE_OPERAND (top, 1), bottom) | |
14199 | && multiple_of_p (type, TREE_OPERAND (top, 2), bottom)); | |
14200 | ||
76a0ced5 | 14201 | case INTEGER_CST: |
17e3940f | 14202 | if (TREE_CODE (bottom) != INTEGER_CST |
ee96af51 | 14203 | || integer_zerop (bottom) |
78a8ed03 | 14204 | || (TYPE_UNSIGNED (type) |
17e3940f | 14205 | && (tree_int_cst_sgn (top) < 0 |
14206 | || tree_int_cst_sgn (bottom) < 0))) | |
76a0ced5 | 14207 | return 0; |
426a138f | 14208 | return integer_zerop (int_const_binop (TRUNC_MOD_EXPR, |
14209 | top, bottom, 0)); | |
76a0ced5 | 14210 | |
14211 | default: | |
14212 | return 0; | |
14213 | } | |
14214 | } | |
0f221fb7 | 14215 | |
ea1a85df | 14216 | /* Return true if CODE or TYPE is known to be non-negative. */ |
14217 | ||
14218 | static bool | |
14219 | tree_simple_nonnegative_warnv_p (enum tree_code code, tree type) | |
14220 | { | |
14221 | if ((TYPE_PRECISION (type) != 1 || TYPE_UNSIGNED (type)) | |
14222 | && truth_value_p (code)) | |
14223 | /* Truth values evaluate to 0 or 1, which is nonnegative unless we | |
14224 | have a signed:1 type (where the value is -1 and 0). */ | |
14225 | return true; | |
14226 | return false; | |
14227 | } | |
14228 | ||
14229 | /* Return true if (CODE OP0) is known to be non-negative. If the return | |
add6ee5e | 14230 | value is based on the assumption that signed overflow is undefined, |
14231 | set *STRICT_OVERFLOW_P to true; otherwise, don't change | |
14232 | *STRICT_OVERFLOW_P. */ | |
0f221fb7 | 14233 | |
93116081 | 14234 | bool |
ea1a85df | 14235 | tree_unary_nonnegative_warnv_p (enum tree_code code, tree type, tree op0, |
14236 | bool *strict_overflow_p) | |
0f221fb7 | 14237 | { |
ea1a85df | 14238 | if (TYPE_UNSIGNED (type)) |
cd29ee4b | 14239 | return true; |
00bb4a78 | 14240 | |
ea1a85df | 14241 | switch (code) |
0f221fb7 | 14242 | { |
cde9d0c7 | 14243 | case ABS_EXPR: |
8040d1c5 | 14244 | /* We can't return 1 if flag_wrapv is set because |
14245 | ABS_EXPR<INT_MIN> = INT_MIN. */ | |
ea1a85df | 14246 | if (!INTEGRAL_TYPE_P (type)) |
981eb798 | 14247 | return true; |
ea1a85df | 14248 | if (TYPE_OVERFLOW_UNDEFINED (type)) |
add6ee5e | 14249 | { |
14250 | *strict_overflow_p = true; | |
14251 | return true; | |
14252 | } | |
8040d1c5 | 14253 | break; |
8f4be2be | 14254 | |
ea1a85df | 14255 | case NON_LVALUE_EXPR: |
14256 | case FLOAT_EXPR: | |
14257 | case FIX_TRUNC_EXPR: | |
14258 | return tree_expr_nonnegative_warnv_p (op0, | |
14259 | strict_overflow_p); | |
cfb7235b | 14260 | |
ea1a85df | 14261 | case NOP_EXPR: |
14262 | { | |
14263 | tree inner_type = TREE_TYPE (op0); | |
14264 | tree outer_type = type; | |
cfb7235b | 14265 | |
ea1a85df | 14266 | if (TREE_CODE (outer_type) == REAL_TYPE) |
14267 | { | |
14268 | if (TREE_CODE (inner_type) == REAL_TYPE) | |
14269 | return tree_expr_nonnegative_warnv_p (op0, | |
14270 | strict_overflow_p); | |
14271 | if (TREE_CODE (inner_type) == INTEGER_TYPE) | |
14272 | { | |
14273 | if (TYPE_UNSIGNED (inner_type)) | |
14274 | return true; | |
14275 | return tree_expr_nonnegative_warnv_p (op0, | |
14276 | strict_overflow_p); | |
14277 | } | |
14278 | } | |
14279 | else if (TREE_CODE (outer_type) == INTEGER_TYPE) | |
14280 | { | |
14281 | if (TREE_CODE (inner_type) == REAL_TYPE) | |
14282 | return tree_expr_nonnegative_warnv_p (op0, | |
14283 | strict_overflow_p); | |
14284 | if (TREE_CODE (inner_type) == INTEGER_TYPE) | |
14285 | return TYPE_PRECISION (inner_type) < TYPE_PRECISION (outer_type) | |
14286 | && TYPE_UNSIGNED (inner_type); | |
14287 | } | |
14288 | } | |
14289 | break; | |
14290 | ||
14291 | default: | |
14292 | return tree_simple_nonnegative_warnv_p (code, type); | |
14293 | } | |
14294 | ||
14295 | /* We don't know sign of `t', so be conservative and return false. */ | |
14296 | return false; | |
14297 | } | |
06f0b99c | 14298 | |
ea1a85df | 14299 | /* Return true if (CODE OP0 OP1) is known to be non-negative. If the return |
14300 | value is based on the assumption that signed overflow is undefined, | |
14301 | set *STRICT_OVERFLOW_P to true; otherwise, don't change | |
14302 | *STRICT_OVERFLOW_P. */ | |
14303 | ||
93116081 | 14304 | bool |
ea1a85df | 14305 | tree_binary_nonnegative_warnv_p (enum tree_code code, tree type, tree op0, |
14306 | tree op1, bool *strict_overflow_p) | |
14307 | { | |
14308 | if (TYPE_UNSIGNED (type)) | |
14309 | return true; | |
14310 | ||
14311 | switch (code) | |
14312 | { | |
0de36bdb | 14313 | case POINTER_PLUS_EXPR: |
cfb7235b | 14314 | case PLUS_EXPR: |
ea1a85df | 14315 | if (FLOAT_TYPE_P (type)) |
14316 | return (tree_expr_nonnegative_warnv_p (op0, | |
add6ee5e | 14317 | strict_overflow_p) |
ea1a85df | 14318 | && tree_expr_nonnegative_warnv_p (op1, |
add6ee5e | 14319 | strict_overflow_p)); |
ae98dc4b | 14320 | |
dfcd8f35 | 14321 | /* zero_extend(x) + zero_extend(y) is non-negative if x and y are |
2b8ef647 | 14322 | both unsigned and at least 2 bits shorter than the result. */ |
ea1a85df | 14323 | if (TREE_CODE (type) == INTEGER_TYPE |
14324 | && TREE_CODE (op0) == NOP_EXPR | |
14325 | && TREE_CODE (op1) == NOP_EXPR) | |
ae98dc4b | 14326 | { |
ea1a85df | 14327 | tree inner1 = TREE_TYPE (TREE_OPERAND (op0, 0)); |
14328 | tree inner2 = TREE_TYPE (TREE_OPERAND (op1, 0)); | |
78a8ed03 | 14329 | if (TREE_CODE (inner1) == INTEGER_TYPE && TYPE_UNSIGNED (inner1) |
14330 | && TREE_CODE (inner2) == INTEGER_TYPE && TYPE_UNSIGNED (inner2)) | |
ae98dc4b | 14331 | { |
14332 | unsigned int prec = MAX (TYPE_PRECISION (inner1), | |
14333 | TYPE_PRECISION (inner2)) + 1; | |
ea1a85df | 14334 | return prec < TYPE_PRECISION (type); |
ae98dc4b | 14335 | } |
14336 | } | |
14337 | break; | |
cfb7235b | 14338 | |
14339 | case MULT_EXPR: | |
ea1a85df | 14340 | if (FLOAT_TYPE_P (type)) |
cfb7235b | 14341 | { |
14342 | /* x * x for floating point x is always non-negative. */ | |
ea1a85df | 14343 | if (operand_equal_p (op0, op1, 0)) |
cd29ee4b | 14344 | return true; |
ea1a85df | 14345 | return (tree_expr_nonnegative_warnv_p (op0, |
add6ee5e | 14346 | strict_overflow_p) |
ea1a85df | 14347 | && tree_expr_nonnegative_warnv_p (op1, |
add6ee5e | 14348 | strict_overflow_p)); |
cfb7235b | 14349 | } |
ae98dc4b | 14350 | |
dfcd8f35 | 14351 | /* zero_extend(x) * zero_extend(y) is non-negative if x and y are |
ae98dc4b | 14352 | both unsigned and their total bits is shorter than the result. */ |
ea1a85df | 14353 | if (TREE_CODE (type) == INTEGER_TYPE |
5485b959 | 14354 | && (TREE_CODE (op0) == NOP_EXPR || TREE_CODE (op0) == INTEGER_CST) |
14355 | && (TREE_CODE (op1) == NOP_EXPR || TREE_CODE (op1) == INTEGER_CST)) | |
ae98dc4b | 14356 | { |
48e1416a | 14357 | tree inner0 = (TREE_CODE (op0) == NOP_EXPR) |
5485b959 | 14358 | ? TREE_TYPE (TREE_OPERAND (op0, 0)) |
14359 | : TREE_TYPE (op0); | |
48e1416a | 14360 | tree inner1 = (TREE_CODE (op1) == NOP_EXPR) |
5485b959 | 14361 | ? TREE_TYPE (TREE_OPERAND (op1, 0)) |
14362 | : TREE_TYPE (op1); | |
14363 | ||
14364 | bool unsigned0 = TYPE_UNSIGNED (inner0); | |
14365 | bool unsigned1 = TYPE_UNSIGNED (inner1); | |
14366 | ||
14367 | if (TREE_CODE (op0) == INTEGER_CST) | |
14368 | unsigned0 = unsigned0 || tree_int_cst_sgn (op0) >= 0; | |
14369 | ||
14370 | if (TREE_CODE (op1) == INTEGER_CST) | |
14371 | unsigned1 = unsigned1 || tree_int_cst_sgn (op1) >= 0; | |
14372 | ||
14373 | if (TREE_CODE (inner0) == INTEGER_TYPE && unsigned0 | |
14374 | && TREE_CODE (inner1) == INTEGER_TYPE && unsigned1) | |
14375 | { | |
14376 | unsigned int precision0 = (TREE_CODE (op0) == INTEGER_CST) | |
14377 | ? tree_int_cst_min_precision (op0, /*unsignedp=*/true) | |
14378 | : TYPE_PRECISION (inner0); | |
14379 | ||
14380 | unsigned int precision1 = (TREE_CODE (op1) == INTEGER_CST) | |
14381 | ? tree_int_cst_min_precision (op1, /*unsignedp=*/true) | |
14382 | : TYPE_PRECISION (inner1); | |
14383 | ||
14384 | return precision0 + precision1 < TYPE_PRECISION (type); | |
14385 | } | |
ae98dc4b | 14386 | } |
cd29ee4b | 14387 | return false; |
cfb7235b | 14388 | |
4fc636aa | 14389 | case BIT_AND_EXPR: |
14390 | case MAX_EXPR: | |
ea1a85df | 14391 | return (tree_expr_nonnegative_warnv_p (op0, |
add6ee5e | 14392 | strict_overflow_p) |
ea1a85df | 14393 | || tree_expr_nonnegative_warnv_p (op1, |
add6ee5e | 14394 | strict_overflow_p)); |
4fc636aa | 14395 | |
14396 | case BIT_IOR_EXPR: | |
14397 | case BIT_XOR_EXPR: | |
14398 | case MIN_EXPR: | |
14399 | case RDIV_EXPR: | |
a9436f5c | 14400 | case TRUNC_DIV_EXPR: |
14401 | case CEIL_DIV_EXPR: | |
14402 | case FLOOR_DIV_EXPR: | |
14403 | case ROUND_DIV_EXPR: | |
ea1a85df | 14404 | return (tree_expr_nonnegative_warnv_p (op0, |
add6ee5e | 14405 | strict_overflow_p) |
ea1a85df | 14406 | && tree_expr_nonnegative_warnv_p (op1, |
add6ee5e | 14407 | strict_overflow_p)); |
ae98dc4b | 14408 | |
a9436f5c | 14409 | case TRUNC_MOD_EXPR: |
14410 | case CEIL_MOD_EXPR: | |
14411 | case FLOOR_MOD_EXPR: | |
14412 | case ROUND_MOD_EXPR: | |
ea1a85df | 14413 | return tree_expr_nonnegative_warnv_p (op0, |
add6ee5e | 14414 | strict_overflow_p); |
ea1a85df | 14415 | default: |
14416 | return tree_simple_nonnegative_warnv_p (code, type); | |
14417 | } | |
ae98dc4b | 14418 | |
ea1a85df | 14419 | /* We don't know sign of `t', so be conservative and return false. */ |
14420 | return false; | |
14421 | } | |
ae98dc4b | 14422 | |
ea1a85df | 14423 | /* Return true if T is known to be non-negative. If the return |
14424 | value is based on the assumption that signed overflow is undefined, | |
14425 | set *STRICT_OVERFLOW_P to true; otherwise, don't change | |
14426 | *STRICT_OVERFLOW_P. */ | |
14427 | ||
93116081 | 14428 | bool |
ea1a85df | 14429 | tree_single_nonnegative_warnv_p (tree t, bool *strict_overflow_p) |
14430 | { | |
14431 | if (TYPE_UNSIGNED (TREE_TYPE (t))) | |
14432 | return true; | |
14433 | ||
ad7c187a | 14434 | switch (TREE_CODE (t)) |
ea1a85df | 14435 | { |
ea1a85df | 14436 | case INTEGER_CST: |
14437 | return tree_int_cst_sgn (t) >= 0; | |
14438 | ||
14439 | case REAL_CST: | |
14440 | return ! REAL_VALUE_NEGATIVE (TREE_REAL_CST (t)); | |
14441 | ||
14442 | case FIXED_CST: | |
14443 | return ! FIXED_VALUE_NEGATIVE (TREE_FIXED_CST (t)); | |
4fc636aa | 14444 | |
14445 | case COND_EXPR: | |
add6ee5e | 14446 | return (tree_expr_nonnegative_warnv_p (TREE_OPERAND (t, 1), |
14447 | strict_overflow_p) | |
14448 | && tree_expr_nonnegative_warnv_p (TREE_OPERAND (t, 2), | |
14449 | strict_overflow_p)); | |
ea1a85df | 14450 | default: |
14451 | return tree_simple_nonnegative_warnv_p (TREE_CODE (t), | |
14452 | TREE_TYPE (t)); | |
14453 | } | |
14454 | /* We don't know sign of `t', so be conservative and return false. */ | |
14455 | return false; | |
14456 | } | |
44c9fd6a | 14457 | |
18f22ec3 | 14458 | /* Return true if T is known to be non-negative. If the return |
14459 | value is based on the assumption that signed overflow is undefined, | |
14460 | set *STRICT_OVERFLOW_P to true; otherwise, don't change | |
14461 | *STRICT_OVERFLOW_P. */ | |
14462 | ||
14463 | bool | |
75a70cf9 | 14464 | tree_call_nonnegative_warnv_p (tree type, tree fndecl, |
18f22ec3 | 14465 | tree arg0, tree arg1, bool *strict_overflow_p) |
14466 | { | |
14467 | if (fndecl && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL) | |
14468 | switch (DECL_FUNCTION_CODE (fndecl)) | |
14469 | { | |
14470 | CASE_FLT_FN (BUILT_IN_ACOS): | |
14471 | CASE_FLT_FN (BUILT_IN_ACOSH): | |
14472 | CASE_FLT_FN (BUILT_IN_CABS): | |
14473 | CASE_FLT_FN (BUILT_IN_COSH): | |
14474 | CASE_FLT_FN (BUILT_IN_ERFC): | |
14475 | CASE_FLT_FN (BUILT_IN_EXP): | |
14476 | CASE_FLT_FN (BUILT_IN_EXP10): | |
14477 | CASE_FLT_FN (BUILT_IN_EXP2): | |
14478 | CASE_FLT_FN (BUILT_IN_FABS): | |
14479 | CASE_FLT_FN (BUILT_IN_FDIM): | |
14480 | CASE_FLT_FN (BUILT_IN_HYPOT): | |
14481 | CASE_FLT_FN (BUILT_IN_POW10): | |
14482 | CASE_INT_FN (BUILT_IN_FFS): | |
14483 | CASE_INT_FN (BUILT_IN_PARITY): | |
14484 | CASE_INT_FN (BUILT_IN_POPCOUNT): | |
14485 | case BUILT_IN_BSWAP32: | |
14486 | case BUILT_IN_BSWAP64: | |
14487 | /* Always true. */ | |
14488 | return true; | |
14489 | ||
14490 | CASE_FLT_FN (BUILT_IN_SQRT): | |
14491 | /* sqrt(-0.0) is -0.0. */ | |
14492 | if (!HONOR_SIGNED_ZEROS (TYPE_MODE (type))) | |
14493 | return true; | |
14494 | return tree_expr_nonnegative_warnv_p (arg0, | |
14495 | strict_overflow_p); | |
14496 | ||
14497 | CASE_FLT_FN (BUILT_IN_ASINH): | |
14498 | CASE_FLT_FN (BUILT_IN_ATAN): | |
14499 | CASE_FLT_FN (BUILT_IN_ATANH): | |
14500 | CASE_FLT_FN (BUILT_IN_CBRT): | |
14501 | CASE_FLT_FN (BUILT_IN_CEIL): | |
14502 | CASE_FLT_FN (BUILT_IN_ERF): | |
14503 | CASE_FLT_FN (BUILT_IN_EXPM1): | |
14504 | CASE_FLT_FN (BUILT_IN_FLOOR): | |
14505 | CASE_FLT_FN (BUILT_IN_FMOD): | |
14506 | CASE_FLT_FN (BUILT_IN_FREXP): | |
14507 | CASE_FLT_FN (BUILT_IN_LCEIL): | |
14508 | CASE_FLT_FN (BUILT_IN_LDEXP): | |
14509 | CASE_FLT_FN (BUILT_IN_LFLOOR): | |
14510 | CASE_FLT_FN (BUILT_IN_LLCEIL): | |
14511 | CASE_FLT_FN (BUILT_IN_LLFLOOR): | |
14512 | CASE_FLT_FN (BUILT_IN_LLRINT): | |
14513 | CASE_FLT_FN (BUILT_IN_LLROUND): | |
14514 | CASE_FLT_FN (BUILT_IN_LRINT): | |
14515 | CASE_FLT_FN (BUILT_IN_LROUND): | |
14516 | CASE_FLT_FN (BUILT_IN_MODF): | |
14517 | CASE_FLT_FN (BUILT_IN_NEARBYINT): | |
14518 | CASE_FLT_FN (BUILT_IN_RINT): | |
14519 | CASE_FLT_FN (BUILT_IN_ROUND): | |
14520 | CASE_FLT_FN (BUILT_IN_SCALB): | |
14521 | CASE_FLT_FN (BUILT_IN_SCALBLN): | |
14522 | CASE_FLT_FN (BUILT_IN_SCALBN): | |
14523 | CASE_FLT_FN (BUILT_IN_SIGNBIT): | |
14524 | CASE_FLT_FN (BUILT_IN_SIGNIFICAND): | |
14525 | CASE_FLT_FN (BUILT_IN_SINH): | |
14526 | CASE_FLT_FN (BUILT_IN_TANH): | |
14527 | CASE_FLT_FN (BUILT_IN_TRUNC): | |
14528 | /* True if the 1st argument is nonnegative. */ | |
14529 | return tree_expr_nonnegative_warnv_p (arg0, | |
14530 | strict_overflow_p); | |
14531 | ||
14532 | CASE_FLT_FN (BUILT_IN_FMAX): | |
14533 | /* True if the 1st OR 2nd arguments are nonnegative. */ | |
14534 | return (tree_expr_nonnegative_warnv_p (arg0, | |
14535 | strict_overflow_p) | |
14536 | || (tree_expr_nonnegative_warnv_p (arg1, | |
14537 | strict_overflow_p))); | |
14538 | ||
14539 | CASE_FLT_FN (BUILT_IN_FMIN): | |
14540 | /* True if the 1st AND 2nd arguments are nonnegative. */ | |
14541 | return (tree_expr_nonnegative_warnv_p (arg0, | |
14542 | strict_overflow_p) | |
14543 | && (tree_expr_nonnegative_warnv_p (arg1, | |
14544 | strict_overflow_p))); | |
14545 | ||
14546 | CASE_FLT_FN (BUILT_IN_COPYSIGN): | |
14547 | /* True if the 2nd argument is nonnegative. */ | |
14548 | return tree_expr_nonnegative_warnv_p (arg1, | |
14549 | strict_overflow_p); | |
14550 | ||
14551 | CASE_FLT_FN (BUILT_IN_POWI): | |
14552 | /* True if the 1st argument is nonnegative or the second | |
14553 | argument is an even integer. */ | |
d14c123b | 14554 | if (TREE_CODE (arg1) == INTEGER_CST |
14555 | && (TREE_INT_CST_LOW (arg1) & 1) == 0) | |
14556 | return true; | |
18f22ec3 | 14557 | return tree_expr_nonnegative_warnv_p (arg0, |
14558 | strict_overflow_p); | |
14559 | ||
14560 | CASE_FLT_FN (BUILT_IN_POW): | |
14561 | /* True if the 1st argument is nonnegative or the second | |
14562 | argument is an even integer valued real. */ | |
14563 | if (TREE_CODE (arg1) == REAL_CST) | |
14564 | { | |
14565 | REAL_VALUE_TYPE c; | |
14566 | HOST_WIDE_INT n; | |
14567 | ||
14568 | c = TREE_REAL_CST (arg1); | |
14569 | n = real_to_integer (&c); | |
14570 | if ((n & 1) == 0) | |
14571 | { | |
14572 | REAL_VALUE_TYPE cint; | |
14573 | real_from_integer (&cint, VOIDmode, n, | |
14574 | n < 0 ? -1 : 0, 0); | |
14575 | if (real_identical (&c, &cint)) | |
14576 | return true; | |
14577 | } | |
14578 | } | |
14579 | return tree_expr_nonnegative_warnv_p (arg0, | |
14580 | strict_overflow_p); | |
14581 | ||
14582 | default: | |
14583 | break; | |
14584 | } | |
75a70cf9 | 14585 | return tree_simple_nonnegative_warnv_p (CALL_EXPR, |
18f22ec3 | 14586 | type); |
14587 | } | |
14588 | ||
ea1a85df | 14589 | /* Return true if T is known to be non-negative. If the return |
14590 | value is based on the assumption that signed overflow is undefined, | |
14591 | set *STRICT_OVERFLOW_P to true; otherwise, don't change | |
14592 | *STRICT_OVERFLOW_P. */ | |
ae98dc4b | 14593 | |
93116081 | 14594 | bool |
ea1a85df | 14595 | tree_invalid_nonnegative_warnv_p (tree t, bool *strict_overflow_p) |
14596 | { | |
ad7c187a | 14597 | enum tree_code code = TREE_CODE (t); |
ea1a85df | 14598 | if (TYPE_UNSIGNED (TREE_TYPE (t))) |
14599 | return true; | |
ae98dc4b | 14600 | |
ea1a85df | 14601 | switch (code) |
14602 | { | |
2569a1be | 14603 | case TARGET_EXPR: |
14604 | { | |
14605 | tree temp = TARGET_EXPR_SLOT (t); | |
14606 | t = TARGET_EXPR_INITIAL (t); | |
14607 | ||
14608 | /* If the initializer is non-void, then it's a normal expression | |
14609 | that will be assigned to the slot. */ | |
14610 | if (!VOID_TYPE_P (t)) | |
add6ee5e | 14611 | return tree_expr_nonnegative_warnv_p (t, strict_overflow_p); |
2569a1be | 14612 | |
14613 | /* Otherwise, the initializer sets the slot in some way. One common | |
14614 | way is an assignment statement at the end of the initializer. */ | |
14615 | while (1) | |
14616 | { | |
14617 | if (TREE_CODE (t) == BIND_EXPR) | |
14618 | t = expr_last (BIND_EXPR_BODY (t)); | |
14619 | else if (TREE_CODE (t) == TRY_FINALLY_EXPR | |
14620 | || TREE_CODE (t) == TRY_CATCH_EXPR) | |
14621 | t = expr_last (TREE_OPERAND (t, 0)); | |
14622 | else if (TREE_CODE (t) == STATEMENT_LIST) | |
14623 | t = expr_last (t); | |
14624 | else | |
14625 | break; | |
14626 | } | |
75a70cf9 | 14627 | if (TREE_CODE (t) == MODIFY_EXPR |
14628 | && TREE_OPERAND (t, 0) == temp) | |
14629 | return tree_expr_nonnegative_warnv_p (TREE_OPERAND (t, 1), | |
add6ee5e | 14630 | strict_overflow_p); |
2569a1be | 14631 | |
cd29ee4b | 14632 | return false; |
2569a1be | 14633 | } |
14634 | ||
c63f4ad3 | 14635 | case CALL_EXPR: |
c6e6ecb1 | 14636 | { |
18f22ec3 | 14637 | tree arg0 = call_expr_nargs (t) > 0 ? CALL_EXPR_ARG (t, 0) : NULL_TREE; |
14638 | tree arg1 = call_expr_nargs (t) > 1 ? CALL_EXPR_ARG (t, 1) : NULL_TREE; | |
14639 | ||
75a70cf9 | 14640 | return tree_call_nonnegative_warnv_p (TREE_TYPE (t), |
18f22ec3 | 14641 | get_callee_fndecl (t), |
14642 | arg0, | |
14643 | arg1, | |
14644 | strict_overflow_p); | |
c6e6ecb1 | 14645 | } |
ea1a85df | 14646 | case COMPOUND_EXPR: |
14647 | case MODIFY_EXPR: | |
75a70cf9 | 14648 | return tree_expr_nonnegative_warnv_p (TREE_OPERAND (t, 1), |
ea1a85df | 14649 | strict_overflow_p); |
14650 | case BIND_EXPR: | |
14651 | return tree_expr_nonnegative_warnv_p (expr_last (TREE_OPERAND (t, 1)), | |
14652 | strict_overflow_p); | |
14653 | case SAVE_EXPR: | |
14654 | return tree_expr_nonnegative_warnv_p (TREE_OPERAND (t, 0), | |
14655 | strict_overflow_p); | |
c63f4ad3 | 14656 | |
0f221fb7 | 14657 | default: |
ea1a85df | 14658 | return tree_simple_nonnegative_warnv_p (TREE_CODE (t), |
14659 | TREE_TYPE (t)); | |
0f221fb7 | 14660 | } |
ae98dc4b | 14661 | |
14662 | /* We don't know sign of `t', so be conservative and return false. */ | |
cd29ee4b | 14663 | return false; |
0f221fb7 | 14664 | } |
14665 | ||
ea1a85df | 14666 | /* Return true if T is known to be non-negative. If the return |
14667 | value is based on the assumption that signed overflow is undefined, | |
14668 | set *STRICT_OVERFLOW_P to true; otherwise, don't change | |
14669 | *STRICT_OVERFLOW_P. */ | |
14670 | ||
14671 | bool | |
14672 | tree_expr_nonnegative_warnv_p (tree t, bool *strict_overflow_p) | |
14673 | { | |
14674 | enum tree_code code; | |
14675 | if (t == error_mark_node) | |
14676 | return false; | |
14677 | ||
14678 | code = TREE_CODE (t); | |
14679 | switch (TREE_CODE_CLASS (code)) | |
14680 | { | |
14681 | case tcc_binary: | |
14682 | case tcc_comparison: | |
14683 | return tree_binary_nonnegative_warnv_p (TREE_CODE (t), | |
14684 | TREE_TYPE (t), | |
14685 | TREE_OPERAND (t, 0), | |
14686 | TREE_OPERAND (t, 1), | |
14687 | strict_overflow_p); | |
14688 | ||
14689 | case tcc_unary: | |
14690 | return tree_unary_nonnegative_warnv_p (TREE_CODE (t), | |
14691 | TREE_TYPE (t), | |
14692 | TREE_OPERAND (t, 0), | |
14693 | strict_overflow_p); | |
14694 | ||
14695 | case tcc_constant: | |
14696 | case tcc_declaration: | |
14697 | case tcc_reference: | |
14698 | return tree_single_nonnegative_warnv_p (t, strict_overflow_p); | |
14699 | ||
14700 | default: | |
14701 | break; | |
14702 | } | |
14703 | ||
14704 | switch (code) | |
14705 | { | |
14706 | case TRUTH_AND_EXPR: | |
14707 | case TRUTH_OR_EXPR: | |
14708 | case TRUTH_XOR_EXPR: | |
14709 | return tree_binary_nonnegative_warnv_p (TREE_CODE (t), | |
14710 | TREE_TYPE (t), | |
14711 | TREE_OPERAND (t, 0), | |
14712 | TREE_OPERAND (t, 1), | |
14713 | strict_overflow_p); | |
14714 | case TRUTH_NOT_EXPR: | |
14715 | return tree_unary_nonnegative_warnv_p (TREE_CODE (t), | |
14716 | TREE_TYPE (t), | |
14717 | TREE_OPERAND (t, 0), | |
14718 | strict_overflow_p); | |
14719 | ||
14720 | case COND_EXPR: | |
14721 | case CONSTRUCTOR: | |
14722 | case OBJ_TYPE_REF: | |
14723 | case ASSERT_EXPR: | |
14724 | case ADDR_EXPR: | |
14725 | case WITH_SIZE_EXPR: | |
ea1a85df | 14726 | case SSA_NAME: |
ea1a85df | 14727 | return tree_single_nonnegative_warnv_p (t, strict_overflow_p); |
14728 | ||
14729 | default: | |
14730 | return tree_invalid_nonnegative_warnv_p (t, strict_overflow_p); | |
14731 | } | |
14732 | } | |
14733 | ||
add6ee5e | 14734 | /* Return true if `t' is known to be non-negative. Handle warnings |
14735 | about undefined signed overflow. */ | |
14736 | ||
14737 | bool | |
14738 | tree_expr_nonnegative_p (tree t) | |
14739 | { | |
14740 | bool ret, strict_overflow_p; | |
14741 | ||
14742 | strict_overflow_p = false; | |
14743 | ret = tree_expr_nonnegative_warnv_p (t, &strict_overflow_p); | |
14744 | if (strict_overflow_p) | |
14745 | fold_overflow_warning (("assuming signed overflow does not occur when " | |
14746 | "determining that expression is always " | |
14747 | "non-negative"), | |
14748 | WARN_STRICT_OVERFLOW_MISC); | |
14749 | return ret; | |
14750 | } | |
14751 | ||
937bceaf | 14752 | |
14753 | /* Return true when (CODE OP0) is an address and is known to be nonzero. | |
ad46984d | 14754 | For floating point we further ensure that T is not denormal. |
add6ee5e | 14755 | Similar logic is present in nonzero_address in rtlanal.h. |
14756 | ||
14757 | If the return value is based on the assumption that signed overflow | |
14758 | is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't | |
14759 | change *STRICT_OVERFLOW_P. */ | |
ad46984d | 14760 | |
93116081 | 14761 | bool |
937bceaf | 14762 | tree_unary_nonzero_warnv_p (enum tree_code code, tree type, tree op0, |
14763 | bool *strict_overflow_p) | |
ad46984d | 14764 | { |
937bceaf | 14765 | switch (code) |
14766 | { | |
14767 | case ABS_EXPR: | |
14768 | return tree_expr_nonzero_warnv_p (op0, | |
14769 | strict_overflow_p); | |
ad46984d | 14770 | |
937bceaf | 14771 | case NOP_EXPR: |
14772 | { | |
14773 | tree inner_type = TREE_TYPE (op0); | |
14774 | tree outer_type = type; | |
ad46984d | 14775 | |
937bceaf | 14776 | return (TYPE_PRECISION (outer_type) >= TYPE_PRECISION (inner_type) |
14777 | && tree_expr_nonzero_warnv_p (op0, | |
14778 | strict_overflow_p)); | |
14779 | } | |
14780 | break; | |
8dbf774a | 14781 | |
937bceaf | 14782 | case NON_LVALUE_EXPR: |
14783 | return tree_expr_nonzero_warnv_p (op0, | |
add6ee5e | 14784 | strict_overflow_p); |
ad46984d | 14785 | |
937bceaf | 14786 | default: |
14787 | break; | |
14788 | } | |
14789 | ||
14790 | return false; | |
14791 | } | |
14792 | ||
14793 | /* Return true when (CODE OP0 OP1) is an address and is known to be nonzero. | |
14794 | For floating point we further ensure that T is not denormal. | |
14795 | Similar logic is present in nonzero_address in rtlanal.h. | |
14796 | ||
14797 | If the return value is based on the assumption that signed overflow | |
14798 | is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't | |
14799 | change *STRICT_OVERFLOW_P. */ | |
ad46984d | 14800 | |
93116081 | 14801 | bool |
937bceaf | 14802 | tree_binary_nonzero_warnv_p (enum tree_code code, |
14803 | tree type, | |
14804 | tree op0, | |
14805 | tree op1, bool *strict_overflow_p) | |
14806 | { | |
14807 | bool sub_strict_overflow_p; | |
14808 | switch (code) | |
14809 | { | |
0de36bdb | 14810 | case POINTER_PLUS_EXPR: |
ad46984d | 14811 | case PLUS_EXPR: |
981eb798 | 14812 | if (TYPE_OVERFLOW_UNDEFINED (type)) |
ad46984d | 14813 | { |
14814 | /* With the presence of negative values it is hard | |
14815 | to say something. */ | |
add6ee5e | 14816 | sub_strict_overflow_p = false; |
937bceaf | 14817 | if (!tree_expr_nonnegative_warnv_p (op0, |
add6ee5e | 14818 | &sub_strict_overflow_p) |
937bceaf | 14819 | || !tree_expr_nonnegative_warnv_p (op1, |
add6ee5e | 14820 | &sub_strict_overflow_p)) |
ad46984d | 14821 | return false; |
14822 | /* One of operands must be positive and the other non-negative. */ | |
add6ee5e | 14823 | /* We don't set *STRICT_OVERFLOW_P here: even if this value |
14824 | overflows, on a twos-complement machine the sum of two | |
14825 | nonnegative numbers can never be zero. */ | |
937bceaf | 14826 | return (tree_expr_nonzero_warnv_p (op0, |
add6ee5e | 14827 | strict_overflow_p) |
937bceaf | 14828 | || tree_expr_nonzero_warnv_p (op1, |
add6ee5e | 14829 | strict_overflow_p)); |
ad46984d | 14830 | } |
14831 | break; | |
14832 | ||
14833 | case MULT_EXPR: | |
981eb798 | 14834 | if (TYPE_OVERFLOW_UNDEFINED (type)) |
ad46984d | 14835 | { |
937bceaf | 14836 | if (tree_expr_nonzero_warnv_p (op0, |
add6ee5e | 14837 | strict_overflow_p) |
937bceaf | 14838 | && tree_expr_nonzero_warnv_p (op1, |
add6ee5e | 14839 | strict_overflow_p)) |
14840 | { | |
14841 | *strict_overflow_p = true; | |
14842 | return true; | |
14843 | } | |
ad46984d | 14844 | } |
14845 | break; | |
14846 | ||
937bceaf | 14847 | case MIN_EXPR: |
14848 | sub_strict_overflow_p = false; | |
14849 | if (tree_expr_nonzero_warnv_p (op0, | |
14850 | &sub_strict_overflow_p) | |
14851 | && tree_expr_nonzero_warnv_p (op1, | |
14852 | &sub_strict_overflow_p)) | |
14853 | { | |
14854 | if (sub_strict_overflow_p) | |
14855 | *strict_overflow_p = true; | |
14856 | } | |
14857 | break; | |
ad46984d | 14858 | |
937bceaf | 14859 | case MAX_EXPR: |
14860 | sub_strict_overflow_p = false; | |
14861 | if (tree_expr_nonzero_warnv_p (op0, | |
14862 | &sub_strict_overflow_p)) | |
14863 | { | |
14864 | if (sub_strict_overflow_p) | |
14865 | *strict_overflow_p = true; | |
14866 | ||
14867 | /* When both operands are nonzero, then MAX must be too. */ | |
14868 | if (tree_expr_nonzero_warnv_p (op1, | |
14869 | strict_overflow_p)) | |
14870 | return true; | |
14871 | ||
14872 | /* MAX where operand 0 is positive is positive. */ | |
14873 | return tree_expr_nonnegative_warnv_p (op0, | |
14874 | strict_overflow_p); | |
14875 | } | |
14876 | /* MAX where operand 1 is positive is positive. */ | |
14877 | else if (tree_expr_nonzero_warnv_p (op1, | |
14878 | &sub_strict_overflow_p) | |
14879 | && tree_expr_nonnegative_warnv_p (op1, | |
14880 | &sub_strict_overflow_p)) | |
14881 | { | |
14882 | if (sub_strict_overflow_p) | |
14883 | *strict_overflow_p = true; | |
14884 | return true; | |
14885 | } | |
14886 | break; | |
14887 | ||
14888 | case BIT_IOR_EXPR: | |
14889 | return (tree_expr_nonzero_warnv_p (op1, | |
14890 | strict_overflow_p) | |
14891 | || tree_expr_nonzero_warnv_p (op0, | |
14892 | strict_overflow_p)); | |
14893 | ||
14894 | default: | |
ad46984d | 14895 | break; |
937bceaf | 14896 | } |
ad46984d | 14897 | |
937bceaf | 14898 | return false; |
14899 | } | |
14900 | ||
14901 | /* Return true when T is an address and is known to be nonzero. | |
14902 | For floating point we further ensure that T is not denormal. | |
14903 | Similar logic is present in nonzero_address in rtlanal.h. | |
14904 | ||
14905 | If the return value is based on the assumption that signed overflow | |
14906 | is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't | |
14907 | change *STRICT_OVERFLOW_P. */ | |
14908 | ||
93116081 | 14909 | bool |
937bceaf | 14910 | tree_single_nonzero_warnv_p (tree t, bool *strict_overflow_p) |
14911 | { | |
14912 | bool sub_strict_overflow_p; | |
14913 | switch (TREE_CODE (t)) | |
14914 | { | |
937bceaf | 14915 | case INTEGER_CST: |
14916 | return !integer_zerop (t); | |
14917 | ||
14918 | case ADDR_EXPR: | |
3d1c55e6 | 14919 | { |
3760428f | 14920 | tree base = TREE_OPERAND (t, 0); |
14921 | if (!DECL_P (base)) | |
14922 | base = get_base_address (base); | |
3d1c55e6 | 14923 | |
14924 | if (!base) | |
14925 | return false; | |
14926 | ||
0f921b42 | 14927 | /* Weak declarations may link to NULL. Other things may also be NULL |
14928 | so protect with -fdelete-null-pointer-checks; but not variables | |
14929 | allocated on the stack. */ | |
14930 | if (DECL_P (base) | |
14931 | && (flag_delete_null_pointer_checks | |
3760428f | 14932 | || (DECL_CONTEXT (base) |
14933 | && TREE_CODE (DECL_CONTEXT (base)) == FUNCTION_DECL | |
14934 | && auto_var_in_fn_p (base, DECL_CONTEXT (base))))) | |
dab963fb | 14935 | return !VAR_OR_FUNCTION_DECL_P (base) || !DECL_WEAK (base); |
3d1c55e6 | 14936 | |
14937 | /* Constants are never weak. */ | |
ce45a448 | 14938 | if (CONSTANT_CLASS_P (base)) |
3d1c55e6 | 14939 | return true; |
14940 | ||
14941 | return false; | |
14942 | } | |
ad46984d | 14943 | |
14944 | case COND_EXPR: | |
add6ee5e | 14945 | sub_strict_overflow_p = false; |
14946 | if (tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 1), | |
14947 | &sub_strict_overflow_p) | |
14948 | && tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 2), | |
14949 | &sub_strict_overflow_p)) | |
14950 | { | |
14951 | if (sub_strict_overflow_p) | |
14952 | *strict_overflow_p = true; | |
14953 | return true; | |
14954 | } | |
14955 | break; | |
ad46984d | 14956 | |
937bceaf | 14957 | default: |
add6ee5e | 14958 | break; |
937bceaf | 14959 | } |
14960 | return false; | |
14961 | } | |
ad46984d | 14962 | |
937bceaf | 14963 | /* Return true when T is an address and is known to be nonzero. |
14964 | For floating point we further ensure that T is not denormal. | |
14965 | Similar logic is present in nonzero_address in rtlanal.h. | |
add6ee5e | 14966 | |
937bceaf | 14967 | If the return value is based on the assumption that signed overflow |
14968 | is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't | |
14969 | change *STRICT_OVERFLOW_P. */ | |
ad46984d | 14970 | |
937bceaf | 14971 | bool |
14972 | tree_expr_nonzero_warnv_p (tree t, bool *strict_overflow_p) | |
14973 | { | |
14974 | tree type = TREE_TYPE (t); | |
14975 | enum tree_code code; | |
14976 | ||
14977 | /* Doing something useful for floating point would need more work. */ | |
14978 | if (!INTEGRAL_TYPE_P (type) && !POINTER_TYPE_P (type)) | |
14979 | return false; | |
14980 | ||
14981 | code = TREE_CODE (t); | |
14982 | switch (TREE_CODE_CLASS (code)) | |
14983 | { | |
14984 | case tcc_unary: | |
14985 | return tree_unary_nonzero_warnv_p (code, type, TREE_OPERAND (t, 0), | |
14986 | strict_overflow_p); | |
14987 | case tcc_binary: | |
14988 | case tcc_comparison: | |
14989 | return tree_binary_nonzero_warnv_p (code, type, | |
14990 | TREE_OPERAND (t, 0), | |
14991 | TREE_OPERAND (t, 1), | |
add6ee5e | 14992 | strict_overflow_p); |
937bceaf | 14993 | case tcc_constant: |
14994 | case tcc_declaration: | |
14995 | case tcc_reference: | |
14996 | return tree_single_nonzero_warnv_p (t, strict_overflow_p); | |
14997 | ||
14998 | default: | |
ad46984d | 14999 | break; |
937bceaf | 15000 | } |
15001 | ||
15002 | switch (code) | |
15003 | { | |
15004 | case TRUTH_NOT_EXPR: | |
15005 | return tree_unary_nonzero_warnv_p (code, type, TREE_OPERAND (t, 0), | |
15006 | strict_overflow_p); | |
15007 | ||
15008 | case TRUTH_AND_EXPR: | |
15009 | case TRUTH_OR_EXPR: | |
15010 | case TRUTH_XOR_EXPR: | |
15011 | return tree_binary_nonzero_warnv_p (code, type, | |
15012 | TREE_OPERAND (t, 0), | |
15013 | TREE_OPERAND (t, 1), | |
15014 | strict_overflow_p); | |
15015 | ||
15016 | case COND_EXPR: | |
15017 | case CONSTRUCTOR: | |
15018 | case OBJ_TYPE_REF: | |
15019 | case ASSERT_EXPR: | |
15020 | case ADDR_EXPR: | |
15021 | case WITH_SIZE_EXPR: | |
937bceaf | 15022 | case SSA_NAME: |
937bceaf | 15023 | return tree_single_nonzero_warnv_p (t, strict_overflow_p); |
ad46984d | 15024 | |
15025 | case COMPOUND_EXPR: | |
15026 | case MODIFY_EXPR: | |
15027 | case BIND_EXPR: | |
75a70cf9 | 15028 | return tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 1), |
add6ee5e | 15029 | strict_overflow_p); |
ad46984d | 15030 | |
15031 | case SAVE_EXPR: | |
add6ee5e | 15032 | return tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 0), |
15033 | strict_overflow_p); | |
ad46984d | 15034 | |
de452836 | 15035 | case CALL_EXPR: |
15036 | return alloca_call_p (t); | |
15037 | ||
ad46984d | 15038 | default: |
15039 | break; | |
15040 | } | |
15041 | return false; | |
15042 | } | |
15043 | ||
add6ee5e | 15044 | /* Return true when T is an address and is known to be nonzero. |
15045 | Handle warnings about undefined signed overflow. */ | |
15046 | ||
15047 | bool | |
15048 | tree_expr_nonzero_p (tree t) | |
15049 | { | |
15050 | bool ret, strict_overflow_p; | |
15051 | ||
15052 | strict_overflow_p = false; | |
15053 | ret = tree_expr_nonzero_warnv_p (t, &strict_overflow_p); | |
15054 | if (strict_overflow_p) | |
15055 | fold_overflow_warning (("assuming signed overflow does not occur when " | |
15056 | "determining that expression is always " | |
15057 | "non-zero"), | |
15058 | WARN_STRICT_OVERFLOW_MISC); | |
15059 | return ret; | |
15060 | } | |
15061 | ||
4ee9c684 | 15062 | /* Given the components of a binary expression CODE, TYPE, OP0 and OP1, |
15063 | attempt to fold the expression to a constant without modifying TYPE, | |
15064 | OP0 or OP1. | |
15065 | ||
15066 | If the expression could be simplified to a constant, then return | |
15067 | the constant. If the expression would not be simplified to a | |
f80ab0b5 | 15068 | constant, then return NULL_TREE. */ |
4ee9c684 | 15069 | |
15070 | tree | |
c1677a03 | 15071 | fold_binary_to_constant (enum tree_code code, tree type, tree op0, tree op1) |
4ee9c684 | 15072 | { |
9a4edb3d | 15073 | tree tem = fold_binary (code, type, op0, op1); |
15074 | return (tem && TREE_CONSTANT (tem)) ? tem : NULL_TREE; | |
4ee9c684 | 15075 | } |
15076 | ||
15077 | /* Given the components of a unary expression CODE, TYPE and OP0, | |
15078 | attempt to fold the expression to a constant without modifying | |
7206da1b | 15079 | TYPE or OP0. |
4ee9c684 | 15080 | |
15081 | If the expression could be simplified to a constant, then return | |
15082 | the constant. If the expression would not be simplified to a | |
f80ab0b5 | 15083 | constant, then return NULL_TREE. */ |
4ee9c684 | 15084 | |
15085 | tree | |
c1677a03 | 15086 | fold_unary_to_constant (enum tree_code code, tree type, tree op0) |
4ee9c684 | 15087 | { |
9a4edb3d | 15088 | tree tem = fold_unary (code, type, op0); |
15089 | return (tem && TREE_CONSTANT (tem)) ? tem : NULL_TREE; | |
4ee9c684 | 15090 | } |
15091 | ||
15092 | /* If EXP represents referencing an element in a constant string | |
15093 | (either via pointer arithmetic or array indexing), return the | |
15094 | tree representing the value accessed, otherwise return NULL. */ | |
15095 | ||
15096 | tree | |
15097 | fold_read_from_constant_string (tree exp) | |
15098 | { | |
dd824901 | 15099 | if ((TREE_CODE (exp) == INDIRECT_REF |
15100 | || TREE_CODE (exp) == ARRAY_REF) | |
15101 | && TREE_CODE (TREE_TYPE (exp)) == INTEGER_TYPE) | |
4ee9c684 | 15102 | { |
15103 | tree exp1 = TREE_OPERAND (exp, 0); | |
15104 | tree index; | |
15105 | tree string; | |
389dd41b | 15106 | location_t loc = EXPR_LOCATION (exp); |
4ee9c684 | 15107 | |
15108 | if (TREE_CODE (exp) == INDIRECT_REF) | |
6374121b | 15109 | string = string_constant (exp1, &index); |
4ee9c684 | 15110 | else |
15111 | { | |
6374121b | 15112 | tree low_bound = array_ref_low_bound (exp); |
389dd41b | 15113 | index = fold_convert_loc (loc, sizetype, TREE_OPERAND (exp, 1)); |
7206da1b | 15114 | |
4ee9c684 | 15115 | /* Optimize the special-case of a zero lower bound. |
15116 | ||
15117 | We convert the low_bound to sizetype to avoid some problems | |
15118 | with constant folding. (E.g. suppose the lower bound is 1, | |
15119 | and its mode is QI. Without the conversion,l (ARRAY | |
15120 | +(INDEX-(unsigned char)1)) becomes ((ARRAY+(-(unsigned char)1)) | |
f0b5f617 | 15121 | +INDEX), which becomes (ARRAY+255+INDEX). Oops!) */ |
4ee9c684 | 15122 | if (! integer_zerop (low_bound)) |
389dd41b | 15123 | index = size_diffop_loc (loc, index, |
15124 | fold_convert_loc (loc, sizetype, low_bound)); | |
4ee9c684 | 15125 | |
15126 | string = exp1; | |
15127 | } | |
15128 | ||
15129 | if (string | |
b3acd74f | 15130 | && TYPE_MODE (TREE_TYPE (exp)) == TYPE_MODE (TREE_TYPE (TREE_TYPE (string))) |
4ee9c684 | 15131 | && TREE_CODE (string) == STRING_CST |
15132 | && TREE_CODE (index) == INTEGER_CST | |
15133 | && compare_tree_int (index, TREE_STRING_LENGTH (string)) < 0 | |
15134 | && (GET_MODE_CLASS (TYPE_MODE (TREE_TYPE (TREE_TYPE (string)))) | |
15135 | == MODE_INT) | |
15136 | && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (TREE_TYPE (string)))) == 1)) | |
7b050b7b | 15137 | return build_int_cst_type (TREE_TYPE (exp), |
15138 | (TREE_STRING_POINTER (string) | |
15139 | [TREE_INT_CST_LOW (index)])); | |
4ee9c684 | 15140 | } |
15141 | return NULL; | |
15142 | } | |
15143 | ||
9d77437d | 15144 | /* Return the tree for neg (ARG0) when ARG0 is known to be either |
06f0b99c | 15145 | an integer constant, real, or fixed-point constant. |
9d77437d | 15146 | |
15147 | TYPE is the type of the result. */ | |
15148 | ||
15149 | static tree | |
15150 | fold_negate_const (tree arg0, tree type) | |
15151 | { | |
15152 | tree t = NULL_TREE; | |
15153 | ||
fdada98f | 15154 | switch (TREE_CODE (arg0)) |
9d77437d | 15155 | { |
fdada98f | 15156 | case INTEGER_CST: |
15157 | { | |
15158 | unsigned HOST_WIDE_INT low; | |
15159 | HOST_WIDE_INT high; | |
15160 | int overflow = neg_double (TREE_INT_CST_LOW (arg0), | |
15161 | TREE_INT_CST_HIGH (arg0), | |
15162 | &low, &high); | |
c8110c8f | 15163 | t = force_fit_type_double (type, low, high, 1, |
15164 | (overflow | TREE_OVERFLOW (arg0)) | |
eddad94a | 15165 | && !TYPE_UNSIGNED (type)); |
fdada98f | 15166 | break; |
15167 | } | |
0c5713a2 | 15168 | |
fdada98f | 15169 | case REAL_CST: |
dae0b5cb | 15170 | t = build_real (type, real_value_negate (&TREE_REAL_CST (arg0))); |
fdada98f | 15171 | break; |
7206da1b | 15172 | |
06f0b99c | 15173 | case FIXED_CST: |
15174 | { | |
15175 | FIXED_VALUE_TYPE f; | |
15176 | bool overflow_p = fixed_arithmetic (&f, NEGATE_EXPR, | |
15177 | &(TREE_FIXED_CST (arg0)), NULL, | |
15178 | TYPE_SATURATING (type)); | |
15179 | t = build_fixed (type, f); | |
15180 | /* Propagate overflow flags. */ | |
15181 | if (overflow_p | TREE_OVERFLOW (arg0)) | |
62126877 | 15182 | TREE_OVERFLOW (t) = 1; |
06f0b99c | 15183 | break; |
15184 | } | |
15185 | ||
fdada98f | 15186 | default: |
15187 | gcc_unreachable (); | |
15188 | } | |
0c5713a2 | 15189 | |
9d77437d | 15190 | return t; |
15191 | } | |
15192 | ||
5221d284 | 15193 | /* Return the tree for abs (ARG0) when ARG0 is known to be either |
15194 | an integer constant or real constant. | |
15195 | ||
15196 | TYPE is the type of the result. */ | |
15197 | ||
d1aade50 | 15198 | tree |
5221d284 | 15199 | fold_abs_const (tree arg0, tree type) |
15200 | { | |
15201 | tree t = NULL_TREE; | |
15202 | ||
fdada98f | 15203 | switch (TREE_CODE (arg0)) |
5221d284 | 15204 | { |
fdada98f | 15205 | case INTEGER_CST: |
5221d284 | 15206 | /* If the value is unsigned, then the absolute value is |
15207 | the same as the ordinary value. */ | |
78a8ed03 | 15208 | if (TYPE_UNSIGNED (type)) |
fdada98f | 15209 | t = arg0; |
5221d284 | 15210 | /* Similarly, if the value is non-negative. */ |
15211 | else if (INT_CST_LT (integer_minus_one_node, arg0)) | |
fdada98f | 15212 | t = arg0; |
5221d284 | 15213 | /* If the value is negative, then the absolute value is |
15214 | its negation. */ | |
15215 | else | |
15216 | { | |
15217 | unsigned HOST_WIDE_INT low; | |
15218 | HOST_WIDE_INT high; | |
15219 | int overflow = neg_double (TREE_INT_CST_LOW (arg0), | |
15220 | TREE_INT_CST_HIGH (arg0), | |
15221 | &low, &high); | |
c8110c8f | 15222 | t = force_fit_type_double (type, low, high, -1, |
eddad94a | 15223 | overflow | TREE_OVERFLOW (arg0)); |
5221d284 | 15224 | } |
fdada98f | 15225 | break; |
0c5713a2 | 15226 | |
fdada98f | 15227 | case REAL_CST: |
5221d284 | 15228 | if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg0))) |
dae0b5cb | 15229 | t = build_real (type, real_value_negate (&TREE_REAL_CST (arg0))); |
5221d284 | 15230 | else |
fdada98f | 15231 | t = arg0; |
15232 | break; | |
0c5713a2 | 15233 | |
fdada98f | 15234 | default: |
15235 | gcc_unreachable (); | |
5221d284 | 15236 | } |
0c5713a2 | 15237 | |
5221d284 | 15238 | return t; |
15239 | } | |
15240 | ||
c183306c | 15241 | /* Return the tree for not (ARG0) when ARG0 is known to be an integer |
15242 | constant. TYPE is the type of the result. */ | |
15243 | ||
15244 | static tree | |
15245 | fold_not_const (tree arg0, tree type) | |
15246 | { | |
15247 | tree t = NULL_TREE; | |
15248 | ||
fdada98f | 15249 | gcc_assert (TREE_CODE (arg0) == INTEGER_CST); |
0c5713a2 | 15250 | |
c8110c8f | 15251 | t = force_fit_type_double (type, ~TREE_INT_CST_LOW (arg0), |
15252 | ~TREE_INT_CST_HIGH (arg0), 0, | |
eddad94a | 15253 | TREE_OVERFLOW (arg0)); |
0c5713a2 | 15254 | |
c183306c | 15255 | return t; |
15256 | } | |
15257 | ||
ad46984d | 15258 | /* Given CODE, a relational operator, the target type, TYPE and two |
15259 | constant operands OP0 and OP1, return the result of the | |
15260 | relational operation. If the result is not a compile time | |
15261 | constant, then return NULL_TREE. */ | |
15262 | ||
15263 | static tree | |
15264 | fold_relational_const (enum tree_code code, tree type, tree op0, tree op1) | |
15265 | { | |
39d4c6de | 15266 | int result, invert; |
ad46984d | 15267 | |
15268 | /* From here on, the only cases we handle are when the result is | |
2f64c430 | 15269 | known to be a constant. */ |
15270 | ||
15271 | if (TREE_CODE (op0) == REAL_CST && TREE_CODE (op1) == REAL_CST) | |
15272 | { | |
990af12c | 15273 | const REAL_VALUE_TYPE *c0 = TREE_REAL_CST_PTR (op0); |
15274 | const REAL_VALUE_TYPE *c1 = TREE_REAL_CST_PTR (op1); | |
15275 | ||
2f64c430 | 15276 | /* Handle the cases where either operand is a NaN. */ |
990af12c | 15277 | if (real_isnan (c0) || real_isnan (c1)) |
2f64c430 | 15278 | { |
15279 | switch (code) | |
15280 | { | |
15281 | case EQ_EXPR: | |
15282 | case ORDERED_EXPR: | |
15283 | result = 0; | |
15284 | break; | |
15285 | ||
15286 | case NE_EXPR: | |
15287 | case UNORDERED_EXPR: | |
15288 | case UNLT_EXPR: | |
15289 | case UNLE_EXPR: | |
15290 | case UNGT_EXPR: | |
15291 | case UNGE_EXPR: | |
15292 | case UNEQ_EXPR: | |
15293 | result = 1; | |
15294 | break; | |
15295 | ||
15296 | case LT_EXPR: | |
15297 | case LE_EXPR: | |
15298 | case GT_EXPR: | |
15299 | case GE_EXPR: | |
15300 | case LTGT_EXPR: | |
15301 | if (flag_trapping_math) | |
15302 | return NULL_TREE; | |
15303 | result = 0; | |
15304 | break; | |
15305 | ||
15306 | default: | |
fdada98f | 15307 | gcc_unreachable (); |
2f64c430 | 15308 | } |
15309 | ||
15310 | return constant_boolean_node (result, type); | |
15311 | } | |
15312 | ||
990af12c | 15313 | return constant_boolean_node (real_compare (code, c0, c1), type); |
2f64c430 | 15314 | } |
15315 | ||
06f0b99c | 15316 | if (TREE_CODE (op0) == FIXED_CST && TREE_CODE (op1) == FIXED_CST) |
15317 | { | |
15318 | const FIXED_VALUE_TYPE *c0 = TREE_FIXED_CST_PTR (op0); | |
15319 | const FIXED_VALUE_TYPE *c1 = TREE_FIXED_CST_PTR (op1); | |
15320 | return constant_boolean_node (fixed_compare (code, c0, c1), type); | |
15321 | } | |
15322 | ||
5dbcb7c4 | 15323 | /* Handle equality/inequality of complex constants. */ |
15324 | if (TREE_CODE (op0) == COMPLEX_CST && TREE_CODE (op1) == COMPLEX_CST) | |
15325 | { | |
15326 | tree rcond = fold_relational_const (code, type, | |
15327 | TREE_REALPART (op0), | |
15328 | TREE_REALPART (op1)); | |
15329 | tree icond = fold_relational_const (code, type, | |
15330 | TREE_IMAGPART (op0), | |
15331 | TREE_IMAGPART (op1)); | |
15332 | if (code == EQ_EXPR) | |
15333 | return fold_build2 (TRUTH_ANDIF_EXPR, type, rcond, icond); | |
15334 | else if (code == NE_EXPR) | |
15335 | return fold_build2 (TRUTH_ORIF_EXPR, type, rcond, icond); | |
15336 | else | |
15337 | return NULL_TREE; | |
15338 | } | |
15339 | ||
2f64c430 | 15340 | /* From here on we only handle LT, LE, GT, GE, EQ and NE. |
ad46984d | 15341 | |
15342 | To compute GT, swap the arguments and do LT. | |
15343 | To compute GE, do LT and invert the result. | |
15344 | To compute LE, swap the arguments, do LT and invert the result. | |
15345 | To compute NE, do EQ and invert the result. | |
15346 | ||
15347 | Therefore, the code below must handle only EQ and LT. */ | |
15348 | ||
15349 | if (code == LE_EXPR || code == GT_EXPR) | |
15350 | { | |
39d4c6de | 15351 | tree tem = op0; |
15352 | op0 = op1; | |
15353 | op1 = tem; | |
ad46984d | 15354 | code = swap_tree_comparison (code); |
15355 | } | |
15356 | ||
15357 | /* Note that it is safe to invert for real values here because we | |
2f64c430 | 15358 | have already handled the one case that it matters. */ |
ad46984d | 15359 | |
ad46984d | 15360 | invert = 0; |
15361 | if (code == NE_EXPR || code == GE_EXPR) | |
15362 | { | |
15363 | invert = 1; | |
318a728f | 15364 | code = invert_tree_comparison (code, false); |
ad46984d | 15365 | } |
15366 | ||
15367 | /* Compute a result for LT or EQ if args permit; | |
15368 | Otherwise return T. */ | |
15369 | if (TREE_CODE (op0) == INTEGER_CST && TREE_CODE (op1) == INTEGER_CST) | |
15370 | { | |
15371 | if (code == EQ_EXPR) | |
39d4c6de | 15372 | result = tree_int_cst_equal (op0, op1); |
15373 | else if (TYPE_UNSIGNED (TREE_TYPE (op0))) | |
15374 | result = INT_CST_LT_UNSIGNED (op0, op1); | |
ad46984d | 15375 | else |
39d4c6de | 15376 | result = INT_CST_LT (op0, op1); |
ad46984d | 15377 | } |
39d4c6de | 15378 | else |
ad46984d | 15379 | return NULL_TREE; |
15380 | ||
15381 | if (invert) | |
39d4c6de | 15382 | result ^= 1; |
15383 | return constant_boolean_node (result, type); | |
ad46984d | 15384 | } |
15385 | ||
098a01e7 | 15386 | /* If necessary, return a CLEANUP_POINT_EXPR for EXPR with the |
15387 | indicated TYPE. If no CLEANUP_POINT_EXPR is necessary, return EXPR | |
15388 | itself. */ | |
acbc760a | 15389 | |
15390 | tree | |
15391 | fold_build_cleanup_point_expr (tree type, tree expr) | |
15392 | { | |
15393 | /* If the expression does not have side effects then we don't have to wrap | |
15394 | it with a cleanup point expression. */ | |
15395 | if (!TREE_SIDE_EFFECTS (expr)) | |
15396 | return expr; | |
b997b4b2 | 15397 | |
15398 | /* If the expression is a return, check to see if the expression inside the | |
15399 | return has no side effects or the right hand side of the modify expression | |
15400 | inside the return. If either don't have side effects set we don't need to | |
15401 | wrap the expression in a cleanup point expression. Note we don't check the | |
15402 | left hand side of the modify because it should always be a return decl. */ | |
15403 | if (TREE_CODE (expr) == RETURN_EXPR) | |
15404 | { | |
15405 | tree op = TREE_OPERAND (expr, 0); | |
15406 | if (!op || !TREE_SIDE_EFFECTS (op)) | |
15407 | return expr; | |
15408 | op = TREE_OPERAND (op, 1); | |
15409 | if (!TREE_SIDE_EFFECTS (op)) | |
15410 | return expr; | |
15411 | } | |
48e1416a | 15412 | |
acbc760a | 15413 | return build1 (CLEANUP_POINT_EXPR, type, expr); |
15414 | } | |
15415 | ||
51fc648f | 15416 | /* Given a pointer value OP0 and a type TYPE, return a simplified version |
15417 | of an indirection through OP0, or NULL_TREE if no simplification is | |
15418 | possible. */ | |
fcdd3ab3 | 15419 | |
9ea9fcf0 | 15420 | tree |
389dd41b | 15421 | fold_indirect_ref_1 (location_t loc, tree type, tree op0) |
fcdd3ab3 | 15422 | { |
51fc648f | 15423 | tree sub = op0; |
fcdd3ab3 | 15424 | tree subtype; |
15425 | ||
e7b5ce74 | 15426 | STRIP_NOPS (sub); |
1928904f | 15427 | subtype = TREE_TYPE (sub); |
15428 | if (!POINTER_TYPE_P (subtype)) | |
15429 | return NULL_TREE; | |
15430 | ||
fcdd3ab3 | 15431 | if (TREE_CODE (sub) == ADDR_EXPR) |
15432 | { | |
15433 | tree op = TREE_OPERAND (sub, 0); | |
15434 | tree optype = TREE_TYPE (op); | |
2350f40c | 15435 | /* *&CONST_DECL -> to the value of the const decl. */ |
15436 | if (TREE_CODE (op) == CONST_DECL) | |
15437 | return DECL_INITIAL (op); | |
85ca5610 | 15438 | /* *&p => p; make sure to handle *&"str"[cst] here. */ |
51fc648f | 15439 | if (type == optype) |
85ca5610 | 15440 | { |
15441 | tree fop = fold_read_from_constant_string (op); | |
15442 | if (fop) | |
15443 | return fop; | |
15444 | else | |
15445 | return op; | |
15446 | } | |
fcdd3ab3 | 15447 | /* *(foo *)&fooarray => fooarray[0] */ |
15448 | else if (TREE_CODE (optype) == ARRAY_TYPE | |
51fc648f | 15449 | && type == TREE_TYPE (optype)) |
15274a5a | 15450 | { |
15451 | tree type_domain = TYPE_DOMAIN (optype); | |
15452 | tree min_val = size_zero_node; | |
15453 | if (type_domain && TYPE_MIN_VALUE (type_domain)) | |
15454 | min_val = TYPE_MIN_VALUE (type_domain); | |
389dd41b | 15455 | op0 = build4 (ARRAY_REF, type, op, min_val, NULL_TREE, NULL_TREE); |
15456 | SET_EXPR_LOCATION (op0, loc); | |
15457 | return op0; | |
15274a5a | 15458 | } |
1e4039e0 | 15459 | /* *(foo *)&complexfoo => __real__ complexfoo */ |
15460 | else if (TREE_CODE (optype) == COMPLEX_TYPE | |
15461 | && type == TREE_TYPE (optype)) | |
389dd41b | 15462 | return fold_build1_loc (loc, REALPART_EXPR, type, op); |
8ea8de24 | 15463 | /* *(foo *)&vectorfoo => BIT_FIELD_REF<vectorfoo,...> */ |
15464 | else if (TREE_CODE (optype) == VECTOR_TYPE | |
15465 | && type == TREE_TYPE (optype)) | |
15466 | { | |
15467 | tree part_width = TYPE_SIZE (type); | |
15468 | tree index = bitsize_int (0); | |
389dd41b | 15469 | return fold_build3_loc (loc, BIT_FIELD_REF, type, op, part_width, index); |
8ea8de24 | 15470 | } |
fcdd3ab3 | 15471 | } |
15472 | ||
70a866cf | 15473 | /* ((foo*)&vectorfoo)[1] => BIT_FIELD_REF<vectorfoo,...> */ |
15474 | if (TREE_CODE (sub) == POINTER_PLUS_EXPR | |
15475 | && TREE_CODE (TREE_OPERAND (sub, 1)) == INTEGER_CST) | |
48e1416a | 15476 | { |
70a866cf | 15477 | tree op00 = TREE_OPERAND (sub, 0); |
15478 | tree op01 = TREE_OPERAND (sub, 1); | |
15479 | tree op00type; | |
48e1416a | 15480 | |
70a866cf | 15481 | STRIP_NOPS (op00); |
15482 | op00type = TREE_TYPE (op00); | |
15483 | if (TREE_CODE (op00) == ADDR_EXPR | |
15484 | && TREE_CODE (TREE_TYPE (op00type)) == VECTOR_TYPE | |
15485 | && type == TREE_TYPE (TREE_TYPE (op00type))) | |
48e1416a | 15486 | { |
70a866cf | 15487 | HOST_WIDE_INT offset = tree_low_cst (op01, 0); |
15488 | tree part_width = TYPE_SIZE (type); | |
15489 | unsigned HOST_WIDE_INT part_widthi = tree_low_cst (part_width, 0)/BITS_PER_UNIT; | |
15490 | unsigned HOST_WIDE_INT indexi = offset * BITS_PER_UNIT; | |
15491 | tree index = bitsize_int (indexi); | |
15492 | ||
15493 | if (offset/part_widthi <= TYPE_VECTOR_SUBPARTS (TREE_TYPE (op00type))) | |
389dd41b | 15494 | return fold_build3_loc (loc, |
15495 | BIT_FIELD_REF, type, TREE_OPERAND (op00, 0), | |
70a866cf | 15496 | part_width, index); |
48e1416a | 15497 | |
70a866cf | 15498 | } |
15499 | } | |
15500 | ||
15501 | ||
1e4039e0 | 15502 | /* ((foo*)&complexfoo)[1] => __imag__ complexfoo */ |
0de36bdb | 15503 | if (TREE_CODE (sub) == POINTER_PLUS_EXPR |
1e4039e0 | 15504 | && TREE_CODE (TREE_OPERAND (sub, 1)) == INTEGER_CST) |
15505 | { | |
15506 | tree op00 = TREE_OPERAND (sub, 0); | |
15507 | tree op01 = TREE_OPERAND (sub, 1); | |
15508 | tree op00type; | |
15509 | ||
15510 | STRIP_NOPS (op00); | |
15511 | op00type = TREE_TYPE (op00); | |
15512 | if (TREE_CODE (op00) == ADDR_EXPR | |
15513 | && TREE_CODE (TREE_TYPE (op00type)) == COMPLEX_TYPE | |
15514 | && type == TREE_TYPE (TREE_TYPE (op00type))) | |
15515 | { | |
15516 | tree size = TYPE_SIZE_UNIT (type); | |
15517 | if (tree_int_cst_equal (size, op01)) | |
389dd41b | 15518 | return fold_build1_loc (loc, IMAGPART_EXPR, type, |
15519 | TREE_OPERAND (op00, 0)); | |
1e4039e0 | 15520 | } |
15521 | } | |
48e1416a | 15522 | |
fcdd3ab3 | 15523 | /* *(foo *)fooarrptr => (*fooarrptr)[0] */ |
fcdd3ab3 | 15524 | if (TREE_CODE (TREE_TYPE (subtype)) == ARRAY_TYPE |
51fc648f | 15525 | && type == TREE_TYPE (TREE_TYPE (subtype))) |
fcdd3ab3 | 15526 | { |
15274a5a | 15527 | tree type_domain; |
15528 | tree min_val = size_zero_node; | |
389dd41b | 15529 | sub = build_fold_indirect_ref_loc (loc, sub); |
15274a5a | 15530 | type_domain = TYPE_DOMAIN (TREE_TYPE (sub)); |
15531 | if (type_domain && TYPE_MIN_VALUE (type_domain)) | |
15532 | min_val = TYPE_MIN_VALUE (type_domain); | |
389dd41b | 15533 | op0 = build4 (ARRAY_REF, type, sub, min_val, NULL_TREE, NULL_TREE); |
15534 | SET_EXPR_LOCATION (op0, loc); | |
15535 | return op0; | |
fcdd3ab3 | 15536 | } |
15537 | ||
1928904f | 15538 | return NULL_TREE; |
15539 | } | |
15540 | ||
15541 | /* Builds an expression for an indirection through T, simplifying some | |
15542 | cases. */ | |
15543 | ||
15544 | tree | |
389dd41b | 15545 | build_fold_indirect_ref_loc (location_t loc, tree t) |
1928904f | 15546 | { |
51fc648f | 15547 | tree type = TREE_TYPE (TREE_TYPE (t)); |
389dd41b | 15548 | tree sub = fold_indirect_ref_1 (loc, type, t); |
1928904f | 15549 | |
15550 | if (sub) | |
15551 | return sub; | |
389dd41b | 15552 | |
15553 | t = build1 (INDIRECT_REF, type, t); | |
15554 | SET_EXPR_LOCATION (t, loc); | |
15555 | return t; | |
1928904f | 15556 | } |
15557 | ||
15558 | /* Given an INDIRECT_REF T, return either T or a simplified version. */ | |
15559 | ||
15560 | tree | |
389dd41b | 15561 | fold_indirect_ref_loc (location_t loc, tree t) |
1928904f | 15562 | { |
389dd41b | 15563 | tree sub = fold_indirect_ref_1 (loc, TREE_TYPE (t), TREE_OPERAND (t, 0)); |
1928904f | 15564 | |
15565 | if (sub) | |
15566 | return sub; | |
15567 | else | |
15568 | return t; | |
fcdd3ab3 | 15569 | } |
15570 | ||
db97ad41 | 15571 | /* Strip non-trapping, non-side-effecting tree nodes from an expression |
15572 | whose result is ignored. The type of the returned tree need not be | |
15573 | the same as the original expression. */ | |
15574 | ||
15575 | tree | |
15576 | fold_ignored_result (tree t) | |
15577 | { | |
15578 | if (!TREE_SIDE_EFFECTS (t)) | |
15579 | return integer_zero_node; | |
15580 | ||
15581 | for (;;) | |
15582 | switch (TREE_CODE_CLASS (TREE_CODE (t))) | |
15583 | { | |
ce45a448 | 15584 | case tcc_unary: |
db97ad41 | 15585 | t = TREE_OPERAND (t, 0); |
15586 | break; | |
15587 | ||
ce45a448 | 15588 | case tcc_binary: |
15589 | case tcc_comparison: | |
db97ad41 | 15590 | if (!TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1))) |
15591 | t = TREE_OPERAND (t, 0); | |
15592 | else if (!TREE_SIDE_EFFECTS (TREE_OPERAND (t, 0))) | |
15593 | t = TREE_OPERAND (t, 1); | |
15594 | else | |
15595 | return t; | |
15596 | break; | |
15597 | ||
ce45a448 | 15598 | case tcc_expression: |
db97ad41 | 15599 | switch (TREE_CODE (t)) |
15600 | { | |
15601 | case COMPOUND_EXPR: | |
15602 | if (TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1))) | |
15603 | return t; | |
15604 | t = TREE_OPERAND (t, 0); | |
15605 | break; | |
15606 | ||
15607 | case COND_EXPR: | |
15608 | if (TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1)) | |
15609 | || TREE_SIDE_EFFECTS (TREE_OPERAND (t, 2))) | |
15610 | return t; | |
15611 | t = TREE_OPERAND (t, 0); | |
15612 | break; | |
15613 | ||
15614 | default: | |
15615 | return t; | |
15616 | } | |
15617 | break; | |
15618 | ||
15619 | default: | |
15620 | return t; | |
15621 | } | |
15622 | } | |
15623 | ||
59ff7b6e | 15624 | /* Return the value of VALUE, rounded up to a multiple of DIVISOR. |
15625 | This can only be applied to objects of a sizetype. */ | |
15626 | ||
15627 | tree | |
389dd41b | 15628 | round_up_loc (location_t loc, tree value, int divisor) |
59ff7b6e | 15629 | { |
cda13ce3 | 15630 | tree div = NULL_TREE; |
59ff7b6e | 15631 | |
fdada98f | 15632 | gcc_assert (divisor > 0); |
59ff7b6e | 15633 | if (divisor == 1) |
15634 | return value; | |
15635 | ||
59ff7b6e | 15636 | /* See if VALUE is already a multiple of DIVISOR. If so, we don't |
cda13ce3 | 15637 | have to do anything. Only do this when we are not given a const, |
15638 | because in that case, this check is more expensive than just | |
fbf0afd1 | 15639 | doing it. */ |
cda13ce3 | 15640 | if (TREE_CODE (value) != INTEGER_CST) |
15641 | { | |
85390276 | 15642 | div = build_int_cst (TREE_TYPE (value), divisor); |
cda13ce3 | 15643 | |
15644 | if (multiple_of_p (TREE_TYPE (value), value, div)) | |
15645 | return value; | |
15646 | } | |
59ff7b6e | 15647 | |
15648 | /* If divisor is a power of two, simplify this to bit manipulation. */ | |
15649 | if (divisor == (divisor & -divisor)) | |
15650 | { | |
6117e415 | 15651 | if (TREE_CODE (value) == INTEGER_CST) |
15652 | { | |
15653 | unsigned HOST_WIDE_INT low = TREE_INT_CST_LOW (value); | |
816f7496 | 15654 | unsigned HOST_WIDE_INT high; |
15655 | bool overflow_p; | |
6117e415 | 15656 | |
15657 | if ((low & (divisor - 1)) == 0) | |
15658 | return value; | |
15659 | ||
816f7496 | 15660 | overflow_p = TREE_OVERFLOW (value); |
6117e415 | 15661 | high = TREE_INT_CST_HIGH (value); |
15662 | low &= ~(divisor - 1); | |
15663 | low += divisor; | |
15664 | if (low == 0) | |
6117e415 | 15665 | { |
816f7496 | 15666 | high++; |
15667 | if (high == 0) | |
15668 | overflow_p = true; | |
6117e415 | 15669 | } |
816f7496 | 15670 | |
15671 | return force_fit_type_double (TREE_TYPE (value), low, high, | |
15672 | -1, overflow_p); | |
6117e415 | 15673 | } |
15674 | else | |
15675 | { | |
816f7496 | 15676 | tree t; |
15677 | ||
6117e415 | 15678 | t = build_int_cst (TREE_TYPE (value), divisor - 1); |
389dd41b | 15679 | value = size_binop_loc (loc, PLUS_EXPR, value, t); |
6117e415 | 15680 | t = build_int_cst (TREE_TYPE (value), -divisor); |
389dd41b | 15681 | value = size_binop_loc (loc, BIT_AND_EXPR, value, t); |
6117e415 | 15682 | } |
59ff7b6e | 15683 | } |
15684 | else | |
15685 | { | |
cda13ce3 | 15686 | if (!div) |
85390276 | 15687 | div = build_int_cst (TREE_TYPE (value), divisor); |
389dd41b | 15688 | value = size_binop_loc (loc, CEIL_DIV_EXPR, value, div); |
15689 | value = size_binop_loc (loc, MULT_EXPR, value, div); | |
59ff7b6e | 15690 | } |
15691 | ||
15692 | return value; | |
15693 | } | |
15694 | ||
15695 | /* Likewise, but round down. */ | |
15696 | ||
15697 | tree | |
389dd41b | 15698 | round_down_loc (location_t loc, tree value, int divisor) |
59ff7b6e | 15699 | { |
cda13ce3 | 15700 | tree div = NULL_TREE; |
59ff7b6e | 15701 | |
fdada98f | 15702 | gcc_assert (divisor > 0); |
59ff7b6e | 15703 | if (divisor == 1) |
15704 | return value; | |
15705 | ||
59ff7b6e | 15706 | /* See if VALUE is already a multiple of DIVISOR. If so, we don't |
cda13ce3 | 15707 | have to do anything. Only do this when we are not given a const, |
15708 | because in that case, this check is more expensive than just | |
fbf0afd1 | 15709 | doing it. */ |
cda13ce3 | 15710 | if (TREE_CODE (value) != INTEGER_CST) |
15711 | { | |
85390276 | 15712 | div = build_int_cst (TREE_TYPE (value), divisor); |
cda13ce3 | 15713 | |
15714 | if (multiple_of_p (TREE_TYPE (value), value, div)) | |
15715 | return value; | |
15716 | } | |
59ff7b6e | 15717 | |
15718 | /* If divisor is a power of two, simplify this to bit manipulation. */ | |
15719 | if (divisor == (divisor & -divisor)) | |
15720 | { | |
cda13ce3 | 15721 | tree t; |
0c5713a2 | 15722 | |
7016c612 | 15723 | t = build_int_cst (TREE_TYPE (value), -divisor); |
389dd41b | 15724 | value = size_binop_loc (loc, BIT_AND_EXPR, value, t); |
59ff7b6e | 15725 | } |
15726 | else | |
15727 | { | |
cda13ce3 | 15728 | if (!div) |
85390276 | 15729 | div = build_int_cst (TREE_TYPE (value), divisor); |
389dd41b | 15730 | value = size_binop_loc (loc, FLOOR_DIV_EXPR, value, div); |
15731 | value = size_binop_loc (loc, MULT_EXPR, value, div); | |
59ff7b6e | 15732 | } |
15733 | ||
15734 | return value; | |
15735 | } | |
dbc64c75 | 15736 | |
eb91f88e | 15737 | /* Returns the pointer to the base of the object addressed by EXP and |
15738 | extracts the information about the offset of the access, storing it | |
15739 | to PBITPOS and POFFSET. */ | |
15740 | ||
15741 | static tree | |
15742 | split_address_to_core_and_offset (tree exp, | |
15743 | HOST_WIDE_INT *pbitpos, tree *poffset) | |
15744 | { | |
15745 | tree core; | |
15746 | enum machine_mode mode; | |
15747 | int unsignedp, volatilep; | |
15748 | HOST_WIDE_INT bitsize; | |
389dd41b | 15749 | location_t loc = EXPR_LOCATION (exp); |
eb91f88e | 15750 | |
15751 | if (TREE_CODE (exp) == ADDR_EXPR) | |
15752 | { | |
15753 | core = get_inner_reference (TREE_OPERAND (exp, 0), &bitsize, pbitpos, | |
e7e9416e | 15754 | poffset, &mode, &unsignedp, &volatilep, |
15755 | false); | |
389dd41b | 15756 | core = build_fold_addr_expr_loc (loc, core); |
eb91f88e | 15757 | } |
15758 | else | |
15759 | { | |
15760 | core = exp; | |
15761 | *pbitpos = 0; | |
15762 | *poffset = NULL_TREE; | |
15763 | } | |
15764 | ||
15765 | return core; | |
15766 | } | |
15767 | ||
dbc64c75 | 15768 | /* Returns true if addresses of E1 and E2 differ by a constant, false |
eb91f88e | 15769 | otherwise. If they do, E1 - E2 is stored in *DIFF. */ |
dbc64c75 | 15770 | |
15771 | bool | |
15772 | ptr_difference_const (tree e1, tree e2, HOST_WIDE_INT *diff) | |
15773 | { | |
15774 | tree core1, core2; | |
dbc64c75 | 15775 | HOST_WIDE_INT bitpos1, bitpos2; |
15776 | tree toffset1, toffset2, tdiff, type; | |
0c5713a2 | 15777 | |
eb91f88e | 15778 | core1 = split_address_to_core_and_offset (e1, &bitpos1, &toffset1); |
15779 | core2 = split_address_to_core_and_offset (e2, &bitpos2, &toffset2); | |
dbc64c75 | 15780 | |
15781 | if (bitpos1 % BITS_PER_UNIT != 0 | |
15782 | || bitpos2 % BITS_PER_UNIT != 0 | |
15783 | || !operand_equal_p (core1, core2, 0)) | |
15784 | return false; | |
15785 | ||
15786 | if (toffset1 && toffset2) | |
15787 | { | |
15788 | type = TREE_TYPE (toffset1); | |
15789 | if (type != TREE_TYPE (toffset2)) | |
15790 | toffset2 = fold_convert (type, toffset2); | |
15791 | ||
7ab7fd4f | 15792 | tdiff = fold_build2 (MINUS_EXPR, type, toffset1, toffset2); |
4e359230 | 15793 | if (!cst_and_fits_in_hwi (tdiff)) |
dbc64c75 | 15794 | return false; |
15795 | ||
4e359230 | 15796 | *diff = int_cst_value (tdiff); |
dbc64c75 | 15797 | } |
15798 | else if (toffset1 || toffset2) | |
15799 | { | |
15800 | /* If only one of the offsets is non-constant, the difference cannot | |
15801 | be a constant. */ | |
15802 | return false; | |
15803 | } | |
15804 | else | |
15805 | *diff = 0; | |
15806 | ||
15807 | *diff += (bitpos1 - bitpos2) / BITS_PER_UNIT; | |
15808 | return true; | |
15809 | } | |
198d9bbe | 15810 | |
15811 | /* Simplify the floating point expression EXP when the sign of the | |
15812 | result is not significant. Return NULL_TREE if no simplification | |
15813 | is possible. */ | |
15814 | ||
15815 | tree | |
15816 | fold_strip_sign_ops (tree exp) | |
15817 | { | |
15818 | tree arg0, arg1; | |
389dd41b | 15819 | location_t loc = EXPR_LOCATION (exp); |
198d9bbe | 15820 | |
15821 | switch (TREE_CODE (exp)) | |
15822 | { | |
15823 | case ABS_EXPR: | |
15824 | case NEGATE_EXPR: | |
15825 | arg0 = fold_strip_sign_ops (TREE_OPERAND (exp, 0)); | |
15826 | return arg0 ? arg0 : TREE_OPERAND (exp, 0); | |
15827 | ||
15828 | case MULT_EXPR: | |
15829 | case RDIV_EXPR: | |
15830 | if (HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (TREE_TYPE (exp)))) | |
15831 | return NULL_TREE; | |
15832 | arg0 = fold_strip_sign_ops (TREE_OPERAND (exp, 0)); | |
15833 | arg1 = fold_strip_sign_ops (TREE_OPERAND (exp, 1)); | |
15834 | if (arg0 != NULL_TREE || arg1 != NULL_TREE) | |
389dd41b | 15835 | return fold_build2_loc (loc, TREE_CODE (exp), TREE_TYPE (exp), |
7ab7fd4f | 15836 | arg0 ? arg0 : TREE_OPERAND (exp, 0), |
15837 | arg1 ? arg1 : TREE_OPERAND (exp, 1)); | |
198d9bbe | 15838 | break; |
15839 | ||
2c26730b | 15840 | case COMPOUND_EXPR: |
15841 | arg0 = TREE_OPERAND (exp, 0); | |
15842 | arg1 = fold_strip_sign_ops (TREE_OPERAND (exp, 1)); | |
15843 | if (arg1) | |
389dd41b | 15844 | return fold_build2_loc (loc, COMPOUND_EXPR, TREE_TYPE (exp), arg0, arg1); |
2c26730b | 15845 | break; |
48e1416a | 15846 | |
2c26730b | 15847 | case COND_EXPR: |
15848 | arg0 = fold_strip_sign_ops (TREE_OPERAND (exp, 1)); | |
15849 | arg1 = fold_strip_sign_ops (TREE_OPERAND (exp, 2)); | |
15850 | if (arg0 || arg1) | |
389dd41b | 15851 | return fold_build3_loc (loc, |
15852 | COND_EXPR, TREE_TYPE (exp), TREE_OPERAND (exp, 0), | |
2c26730b | 15853 | arg0 ? arg0 : TREE_OPERAND (exp, 1), |
15854 | arg1 ? arg1 : TREE_OPERAND (exp, 2)); | |
15855 | break; | |
48e1416a | 15856 | |
e6ab33d8 | 15857 | case CALL_EXPR: |
b145cb59 | 15858 | { |
15859 | const enum built_in_function fcode = builtin_mathfn_code (exp); | |
15860 | switch (fcode) | |
15861 | { | |
15862 | CASE_FLT_FN (BUILT_IN_COPYSIGN): | |
15863 | /* Strip copysign function call, return the 1st argument. */ | |
c2f47e15 | 15864 | arg0 = CALL_EXPR_ARG (exp, 0); |
15865 | arg1 = CALL_EXPR_ARG (exp, 1); | |
389dd41b | 15866 | return omit_one_operand_loc (loc, TREE_TYPE (exp), arg0, arg1); |
b145cb59 | 15867 | |
15868 | default: | |
15869 | /* Strip sign ops from the argument of "odd" math functions. */ | |
15870 | if (negate_mathfn_p (fcode)) | |
15871 | { | |
c2f47e15 | 15872 | arg0 = fold_strip_sign_ops (CALL_EXPR_ARG (exp, 0)); |
b145cb59 | 15873 | if (arg0) |
389dd41b | 15874 | return build_call_expr_loc (loc, get_callee_fndecl (exp), 1, arg0); |
b145cb59 | 15875 | } |
15876 | break; | |
e6ab33d8 | 15877 | } |
b145cb59 | 15878 | } |
e6ab33d8 | 15879 | break; |
15880 | ||
198d9bbe | 15881 | default: |
15882 | break; | |
15883 | } | |
15884 | return NULL_TREE; | |
15885 | } |