]>
Commit | Line | Data |
---|---|---|
6d716ca8 | 1 | /* Fold a constant sub-tree into a single node for C-compiler |
d1e082c2 | 2 | Copyright (C) 1987-2013 Free Software Foundation, Inc. |
6d716ca8 | 3 | |
1322177d | 4 | This file is part of GCC. |
6d716ca8 | 5 | |
1322177d LB |
6 | GCC is free software; you can redistribute it and/or modify it under |
7 | the terms of the GNU General Public License as published by the Free | |
9dcd6f09 | 8 | Software Foundation; either version 3, or (at your option) any later |
1322177d | 9 | version. |
6d716ca8 | 10 | |
1322177d LB |
11 | GCC is distributed in the hope that it will be useful, but WITHOUT ANY |
12 | WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
13 | FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
14 | for more details. | |
6d716ca8 RS |
15 | |
16 | You should have received a copy of the GNU General Public License | |
9dcd6f09 NC |
17 | along with GCC; see the file COPYING3. If not see |
18 | <http://www.gnu.org/licenses/>. */ | |
6d716ca8 | 19 | |
6dc42e49 | 20 | /*@@ This file should be rewritten to use an arbitrary precision |
6d716ca8 RS |
21 | @@ representation for "struct tree_int_cst" and "struct tree_real_cst". |
22 | @@ Perhaps the routines could also be used for bc/dc, and made a lib. | |
23 | @@ The routines that translate from the ap rep should | |
24 | @@ warn if precision et. al. is lost. | |
25 | @@ This would also make life easier when this technology is used | |
26 | @@ for cross-compilers. */ | |
27 | ||
9589f23e | 28 | /* The entry points in this file are fold, size_int_wide and size_binop. |
6d716ca8 RS |
29 | |
30 | fold takes a tree as argument and returns a simplified tree. | |
31 | ||
32 | size_binop takes a tree code for an arithmetic operation | |
33 | and two operands that are trees, and produces a tree for the | |
34 | result, assuming the type comes from `sizetype'. | |
35 | ||
36 | size_int takes an integer value, and creates a tree constant | |
0da6f3db DE |
37 | with type from `sizetype'. |
38 | ||
07beea0d AH |
39 | Note: Since the folders get called on non-gimple code as well as |
40 | gimple code, we need to handle GIMPLE tuples as well as their | |
41 | corresponding tree equivalents. */ | |
0da6f3db | 42 | |
e9a25f70 | 43 | #include "config.h" |
2fde567e | 44 | #include "system.h" |
4977bab6 ZW |
45 | #include "coretypes.h" |
46 | #include "tm.h" | |
6d716ca8 RS |
47 | #include "flags.h" |
48 | #include "tree.h" | |
d49b6e1e | 49 | #include "realmpfr.h" |
efe3eb65 | 50 | #include "rtl.h" |
0e9295cf | 51 | #include "expr.h" |
6baf1cc8 | 52 | #include "tm_p.h" |
bd03c084 | 53 | #include "target.h" |
718f9c0f | 54 | #include "diagnostic-core.h" |
6ac01510 | 55 | #include "intl.h" |
a3770a81 | 56 | #include "ggc.h" |
703c8606 | 57 | #include "hash-table.h" |
43577e6b | 58 | #include "langhooks.h" |
5dfa45d0 | 59 | #include "md5.h" |
726a989a | 60 | #include "gimple.h" |
442b4905 | 61 | #include "tree-dfa.h" |
6d716ca8 | 62 | |
110abdbc | 63 | /* Nonzero if we are folding constants inside an initializer; zero |
63b48197 MS |
64 | otherwise. */ |
65 | int folding_initializer = 0; | |
66 | ||
d1a7edaf PB |
67 | /* The following constants represent a bit based encoding of GCC's |
68 | comparison operators. This encoding simplifies transformations | |
69 | on relational comparison operators, such as AND and OR. */ | |
70 | enum comparison_code { | |
71 | COMPCODE_FALSE = 0, | |
72 | COMPCODE_LT = 1, | |
73 | COMPCODE_EQ = 2, | |
74 | COMPCODE_LE = 3, | |
75 | COMPCODE_GT = 4, | |
76 | COMPCODE_LTGT = 5, | |
77 | COMPCODE_GE = 6, | |
78 | COMPCODE_ORD = 7, | |
79 | COMPCODE_UNORD = 8, | |
80 | COMPCODE_UNLT = 9, | |
81 | COMPCODE_UNEQ = 10, | |
82 | COMPCODE_UNLE = 11, | |
83 | COMPCODE_UNGT = 12, | |
84 | COMPCODE_NE = 13, | |
85 | COMPCODE_UNGE = 14, | |
86 | COMPCODE_TRUE = 15 | |
87 | }; | |
88 | ||
05d362b8 | 89 | static bool negate_mathfn_p (enum built_in_function); |
fa8db1f7 AJ |
90 | static bool negate_expr_p (tree); |
91 | static tree negate_expr (tree); | |
92 | static tree split_tree (tree, enum tree_code, tree *, tree *, tree *, int); | |
db3927fb | 93 | static tree associate_trees (location_t, tree, tree, enum tree_code, tree); |
43a5d30b | 94 | static tree const_binop (enum tree_code, tree, tree); |
d1a7edaf PB |
95 | static enum comparison_code comparison_to_compcode (enum tree_code); |
96 | static enum tree_code compcode_to_comparison (enum comparison_code); | |
fa8db1f7 AJ |
97 | static int operand_equal_for_comparison_p (tree, tree, tree); |
98 | static int twoval_comparison_p (tree, tree *, tree *, int *); | |
db3927fb AH |
99 | static tree eval_subst (location_t, tree, tree, tree, tree, tree); |
100 | static tree pedantic_omit_one_operand_loc (location_t, tree, tree, tree); | |
101 | static tree distribute_bit_expr (location_t, enum tree_code, tree, tree, tree); | |
102 | static tree make_bit_field_ref (location_t, tree, tree, | |
103 | HOST_WIDE_INT, HOST_WIDE_INT, int); | |
104 | static tree optimize_bit_field_compare (location_t, enum tree_code, | |
105 | tree, tree, tree); | |
106 | static tree decode_field_reference (location_t, tree, HOST_WIDE_INT *, | |
107 | HOST_WIDE_INT *, | |
fa8db1f7 AJ |
108 | enum machine_mode *, int *, int *, |
109 | tree *, tree *); | |
45dc13b9 | 110 | static int all_ones_mask_p (const_tree, int); |
ac545c64 KG |
111 | static tree sign_bit_p (tree, const_tree); |
112 | static int simple_operand_p (const_tree); | |
6e796a83 | 113 | static bool simple_operand_p_2 (tree); |
fa8db1f7 | 114 | static tree range_binop (enum tree_code, tree, tree, int, tree, int); |
f8fe0545 EB |
115 | static tree range_predecessor (tree); |
116 | static tree range_successor (tree); | |
db3927fb AH |
117 | static tree fold_range_test (location_t, enum tree_code, tree, tree, tree); |
118 | static tree fold_cond_expr_with_comparison (location_t, tree, tree, tree, tree); | |
fa8db1f7 | 119 | static tree unextend (tree, int, int, tree); |
db3927fb AH |
120 | static tree optimize_minmax_comparison (location_t, enum tree_code, |
121 | tree, tree, tree); | |
6ac01510 ILT |
122 | static tree extract_muldiv (tree, tree, enum tree_code, tree, bool *); |
123 | static tree extract_muldiv_1 (tree, tree, enum tree_code, tree, bool *); | |
db3927fb AH |
124 | static tree fold_binary_op_with_conditional_arg (location_t, |
125 | enum tree_code, tree, | |
e9da788c | 126 | tree, tree, |
3b70b82a | 127 | tree, tree, int); |
db3927fb AH |
128 | static tree fold_mathfn_compare (location_t, |
129 | enum built_in_function, enum tree_code, | |
fa8db1f7 | 130 | tree, tree, tree); |
db3927fb AH |
131 | static tree fold_inf_compare (location_t, enum tree_code, tree, tree, tree); |
132 | static tree fold_div_compare (location_t, enum tree_code, tree, tree, tree); | |
ac545c64 | 133 | static bool reorder_operands_p (const_tree, const_tree); |
33d13fac | 134 | static tree fold_negate_const (tree, tree); |
9589f23e | 135 | static tree fold_not_const (const_tree, tree); |
8e7b3a43 | 136 | static tree fold_relational_const (enum tree_code, tree, tree, tree); |
d1d1c602 | 137 | static tree fold_convert_const (enum tree_code, tree, tree); |
78bf6e2f | 138 | |
6c4e2997 NF |
139 | /* Return EXPR_LOCATION of T if it is not UNKNOWN_LOCATION. |
140 | Otherwise, return LOC. */ | |
141 | ||
142 | static location_t | |
143 | expr_location_or (tree t, location_t loc) | |
144 | { | |
145 | location_t tloc = EXPR_LOCATION (t); | |
2f13f2de | 146 | return tloc == UNKNOWN_LOCATION ? loc : tloc; |
6c4e2997 | 147 | } |
33d13fac | 148 | |
c9019218 JJ |
149 | /* Similar to protected_set_expr_location, but never modify x in place, |
150 | if location can and needs to be set, unshare it. */ | |
151 | ||
152 | static inline tree | |
153 | protected_set_expr_location_unshare (tree x, location_t loc) | |
154 | { | |
155 | if (CAN_HAVE_LOCATION_P (x) | |
156 | && EXPR_LOCATION (x) != loc | |
157 | && !(TREE_CODE (x) == SAVE_EXPR | |
158 | || TREE_CODE (x) == TARGET_EXPR | |
159 | || TREE_CODE (x) == BIND_EXPR)) | |
160 | { | |
161 | x = copy_node (x); | |
162 | SET_EXPR_LOCATION (x, loc); | |
163 | } | |
164 | return x; | |
165 | } | |
6d716ca8 | 166 | \f |
03b0db0a RG |
167 | /* If ARG2 divides ARG1 with zero remainder, carries out the division |
168 | of type CODE and returns the quotient. | |
169 | Otherwise returns NULL_TREE. */ | |
170 | ||
108f6c2f | 171 | tree |
ac545c64 | 172 | div_if_zero_remainder (enum tree_code code, const_tree arg1, const_tree arg2) |
03b0db0a | 173 | { |
2bd1333d | 174 | double_int quo, rem; |
793e86a7 RG |
175 | int uns; |
176 | ||
177 | /* The sign of the division is according to operand two, that | |
178 | does the correct thing for POINTER_PLUS_EXPR where we want | |
179 | a signed division. */ | |
180 | uns = TYPE_UNSIGNED (TREE_TYPE (arg2)); | |
03b0db0a | 181 | |
27bcd47c LC |
182 | quo = tree_to_double_int (arg1).divmod (tree_to_double_int (arg2), |
183 | uns, code, &rem); | |
03b0db0a | 184 | |
27bcd47c | 185 | if (rem.is_zero ()) |
2bd1333d | 186 | return build_int_cst_wide (TREE_TYPE (arg1), quo.low, quo.high); |
03b0db0a | 187 | |
2bd1333d | 188 | return NULL_TREE; |
03b0db0a | 189 | } |
6d716ca8 | 190 | \f |
110abdbc | 191 | /* This is nonzero if we should defer warnings about undefined |
6ac01510 ILT |
192 | overflow. This facility exists because these warnings are a |
193 | special case. The code to estimate loop iterations does not want | |
194 | to issue any warnings, since it works with expressions which do not | |
195 | occur in user code. Various bits of cleanup code call fold(), but | |
196 | only use the result if it has certain characteristics (e.g., is a | |
197 | constant); that code only wants to issue a warning if the result is | |
198 | used. */ | |
199 | ||
200 | static int fold_deferring_overflow_warnings; | |
201 | ||
202 | /* If a warning about undefined overflow is deferred, this is the | |
203 | warning. Note that this may cause us to turn two warnings into | |
204 | one, but that is fine since it is sufficient to only give one | |
205 | warning per expression. */ | |
206 | ||
207 | static const char* fold_deferred_overflow_warning; | |
208 | ||
209 | /* If a warning about undefined overflow is deferred, this is the | |
210 | level at which the warning should be emitted. */ | |
211 | ||
212 | static enum warn_strict_overflow_code fold_deferred_overflow_code; | |
213 | ||
214 | /* Start deferring overflow warnings. We could use a stack here to | |
215 | permit nested calls, but at present it is not necessary. */ | |
216 | ||
217 | void | |
218 | fold_defer_overflow_warnings (void) | |
219 | { | |
220 | ++fold_deferring_overflow_warnings; | |
221 | } | |
222 | ||
223 | /* Stop deferring overflow warnings. If there is a pending warning, | |
224 | and ISSUE is true, then issue the warning if appropriate. STMT is | |
225 | the statement with which the warning should be associated (used for | |
226 | location information); STMT may be NULL. CODE is the level of the | |
227 | warning--a warn_strict_overflow_code value. This function will use | |
228 | the smaller of CODE and the deferred code when deciding whether to | |
229 | issue the warning. CODE may be zero to mean to always use the | |
230 | deferred code. */ | |
231 | ||
232 | void | |
726a989a | 233 | fold_undefer_overflow_warnings (bool issue, const_gimple stmt, int code) |
6ac01510 ILT |
234 | { |
235 | const char *warnmsg; | |
236 | location_t locus; | |
237 | ||
238 | gcc_assert (fold_deferring_overflow_warnings > 0); | |
239 | --fold_deferring_overflow_warnings; | |
240 | if (fold_deferring_overflow_warnings > 0) | |
241 | { | |
242 | if (fold_deferred_overflow_warning != NULL | |
243 | && code != 0 | |
244 | && code < (int) fold_deferred_overflow_code) | |
32e8bb8e | 245 | fold_deferred_overflow_code = (enum warn_strict_overflow_code) code; |
6ac01510 ILT |
246 | return; |
247 | } | |
248 | ||
249 | warnmsg = fold_deferred_overflow_warning; | |
250 | fold_deferred_overflow_warning = NULL; | |
251 | ||
252 | if (!issue || warnmsg == NULL) | |
253 | return; | |
254 | ||
726a989a | 255 | if (gimple_no_warning_p (stmt)) |
e233ac97 ILT |
256 | return; |
257 | ||
6ac01510 ILT |
258 | /* Use the smallest code level when deciding to issue the |
259 | warning. */ | |
260 | if (code == 0 || code > (int) fold_deferred_overflow_code) | |
261 | code = fold_deferred_overflow_code; | |
262 | ||
263 | if (!issue_strict_overflow_warning (code)) | |
264 | return; | |
265 | ||
726a989a | 266 | if (stmt == NULL) |
6ac01510 ILT |
267 | locus = input_location; |
268 | else | |
726a989a | 269 | locus = gimple_location (stmt); |
fab922b1 | 270 | warning_at (locus, OPT_Wstrict_overflow, "%s", warnmsg); |
6ac01510 ILT |
271 | } |
272 | ||
273 | /* Stop deferring overflow warnings, ignoring any deferred | |
274 | warnings. */ | |
275 | ||
276 | void | |
277 | fold_undefer_and_ignore_overflow_warnings (void) | |
278 | { | |
726a989a | 279 | fold_undefer_overflow_warnings (false, NULL, 0); |
6ac01510 ILT |
280 | } |
281 | ||
282 | /* Whether we are deferring overflow warnings. */ | |
283 | ||
284 | bool | |
285 | fold_deferring_overflow_warnings_p (void) | |
286 | { | |
287 | return fold_deferring_overflow_warnings > 0; | |
288 | } | |
289 | ||
290 | /* This is called when we fold something based on the fact that signed | |
291 | overflow is undefined. */ | |
292 | ||
293 | static void | |
294 | fold_overflow_warning (const char* gmsgid, enum warn_strict_overflow_code wc) | |
295 | { | |
6ac01510 ILT |
296 | if (fold_deferring_overflow_warnings > 0) |
297 | { | |
298 | if (fold_deferred_overflow_warning == NULL | |
299 | || wc < fold_deferred_overflow_code) | |
300 | { | |
301 | fold_deferred_overflow_warning = gmsgid; | |
302 | fold_deferred_overflow_code = wc; | |
303 | } | |
304 | } | |
305 | else if (issue_strict_overflow_warning (wc)) | |
306 | warning (OPT_Wstrict_overflow, gmsgid); | |
307 | } | |
308 | \f | |
dd6f2a43 VR |
309 | /* Return true if the built-in mathematical function specified by CODE |
310 | is odd, i.e. -f(x) == f(-x). */ | |
05d362b8 RS |
311 | |
312 | static bool | |
313 | negate_mathfn_p (enum built_in_function code) | |
314 | { | |
315 | switch (code) | |
316 | { | |
ea6a6627 VR |
317 | CASE_FLT_FN (BUILT_IN_ASIN): |
318 | CASE_FLT_FN (BUILT_IN_ASINH): | |
319 | CASE_FLT_FN (BUILT_IN_ATAN): | |
320 | CASE_FLT_FN (BUILT_IN_ATANH): | |
4b26d10b KG |
321 | CASE_FLT_FN (BUILT_IN_CASIN): |
322 | CASE_FLT_FN (BUILT_IN_CASINH): | |
323 | CASE_FLT_FN (BUILT_IN_CATAN): | |
324 | CASE_FLT_FN (BUILT_IN_CATANH): | |
ea6a6627 | 325 | CASE_FLT_FN (BUILT_IN_CBRT): |
4b26d10b KG |
326 | CASE_FLT_FN (BUILT_IN_CPROJ): |
327 | CASE_FLT_FN (BUILT_IN_CSIN): | |
328 | CASE_FLT_FN (BUILT_IN_CSINH): | |
329 | CASE_FLT_FN (BUILT_IN_CTAN): | |
330 | CASE_FLT_FN (BUILT_IN_CTANH): | |
5c5b2155 KG |
331 | CASE_FLT_FN (BUILT_IN_ERF): |
332 | CASE_FLT_FN (BUILT_IN_LLROUND): | |
333 | CASE_FLT_FN (BUILT_IN_LROUND): | |
334 | CASE_FLT_FN (BUILT_IN_ROUND): | |
ea6a6627 VR |
335 | CASE_FLT_FN (BUILT_IN_SIN): |
336 | CASE_FLT_FN (BUILT_IN_SINH): | |
337 | CASE_FLT_FN (BUILT_IN_TAN): | |
338 | CASE_FLT_FN (BUILT_IN_TANH): | |
5c5b2155 | 339 | CASE_FLT_FN (BUILT_IN_TRUNC): |
05d362b8 RS |
340 | return true; |
341 | ||
5c5b2155 KG |
342 | CASE_FLT_FN (BUILT_IN_LLRINT): |
343 | CASE_FLT_FN (BUILT_IN_LRINT): | |
344 | CASE_FLT_FN (BUILT_IN_NEARBYINT): | |
345 | CASE_FLT_FN (BUILT_IN_RINT): | |
346 | return !flag_rounding_math; | |
b8698a0f | 347 | |
05d362b8 RS |
348 | default: |
349 | break; | |
350 | } | |
351 | return false; | |
352 | } | |
353 | ||
82b85a85 ZD |
354 | /* Check whether we may negate an integer constant T without causing |
355 | overflow. */ | |
356 | ||
357 | bool | |
fa233e34 | 358 | may_negate_without_overflow_p (const_tree t) |
82b85a85 ZD |
359 | { |
360 | unsigned HOST_WIDE_INT val; | |
361 | unsigned int prec; | |
362 | tree type; | |
363 | ||
0bccc606 | 364 | gcc_assert (TREE_CODE (t) == INTEGER_CST); |
82b85a85 ZD |
365 | |
366 | type = TREE_TYPE (t); | |
367 | if (TYPE_UNSIGNED (type)) | |
368 | return false; | |
369 | ||
370 | prec = TYPE_PRECISION (type); | |
371 | if (prec > HOST_BITS_PER_WIDE_INT) | |
372 | { | |
373 | if (TREE_INT_CST_LOW (t) != 0) | |
374 | return true; | |
375 | prec -= HOST_BITS_PER_WIDE_INT; | |
376 | val = TREE_INT_CST_HIGH (t); | |
377 | } | |
378 | else | |
379 | val = TREE_INT_CST_LOW (t); | |
380 | if (prec < HOST_BITS_PER_WIDE_INT) | |
381 | val &= ((unsigned HOST_WIDE_INT) 1 << prec) - 1; | |
382 | return val != ((unsigned HOST_WIDE_INT) 1 << (prec - 1)); | |
383 | } | |
384 | ||
080ea642 | 385 | /* Determine whether an expression T can be cheaply negated using |
1af8dcbf | 386 | the function negate_expr without introducing undefined overflow. */ |
080ea642 RS |
387 | |
388 | static bool | |
fa8db1f7 | 389 | negate_expr_p (tree t) |
080ea642 | 390 | { |
080ea642 RS |
391 | tree type; |
392 | ||
393 | if (t == 0) | |
394 | return false; | |
395 | ||
396 | type = TREE_TYPE (t); | |
397 | ||
398 | STRIP_SIGN_NOPS (t); | |
399 | switch (TREE_CODE (t)) | |
400 | { | |
401 | case INTEGER_CST: | |
eeef0e45 | 402 | if (TYPE_OVERFLOW_WRAPS (type)) |
05d362b8 | 403 | return true; |
080ea642 RS |
404 | |
405 | /* Check that -CST will not overflow type. */ | |
82b85a85 | 406 | return may_negate_without_overflow_p (t); |
189d4130 | 407 | case BIT_NOT_EXPR: |
eeef0e45 ILT |
408 | return (INTEGRAL_TYPE_P (type) |
409 | && TYPE_OVERFLOW_WRAPS (type)); | |
080ea642 | 410 | |
325217ed | 411 | case FIXED_CST: |
080ea642 | 412 | case NEGATE_EXPR: |
080ea642 RS |
413 | return true; |
414 | ||
4e62a017 RG |
415 | case REAL_CST: |
416 | /* We want to canonicalize to positive real constants. Pretend | |
417 | that only negative ones can be easily negated. */ | |
418 | return REAL_VALUE_NEGATIVE (TREE_REAL_CST (t)); | |
419 | ||
05d362b8 RS |
420 | case COMPLEX_CST: |
421 | return negate_expr_p (TREE_REALPART (t)) | |
422 | && negate_expr_p (TREE_IMAGPART (t)); | |
423 | ||
948a1fd9 MG |
424 | case VECTOR_CST: |
425 | { | |
426 | if (FLOAT_TYPE_P (TREE_TYPE (type)) || TYPE_OVERFLOW_WRAPS (type)) | |
427 | return true; | |
428 | ||
429 | int count = TYPE_VECTOR_SUBPARTS (type), i; | |
430 | ||
431 | for (i = 0; i < count; i++) | |
432 | if (!negate_expr_p (VECTOR_CST_ELT (t, i))) | |
433 | return false; | |
434 | ||
435 | return true; | |
436 | } | |
437 | ||
1aeef526 KG |
438 | case COMPLEX_EXPR: |
439 | return negate_expr_p (TREE_OPERAND (t, 0)) | |
440 | && negate_expr_p (TREE_OPERAND (t, 1)); | |
441 | ||
8fbbe90b KG |
442 | case CONJ_EXPR: |
443 | return negate_expr_p (TREE_OPERAND (t, 0)); | |
444 | ||
dfb36f9b | 445 | case PLUS_EXPR: |
1b43b967 RS |
446 | if (HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (type)) |
447 | || HONOR_SIGNED_ZEROS (TYPE_MODE (type))) | |
dfb36f9b RS |
448 | return false; |
449 | /* -(A + B) -> (-B) - A. */ | |
450 | if (negate_expr_p (TREE_OPERAND (t, 1)) | |
451 | && reorder_operands_p (TREE_OPERAND (t, 0), | |
452 | TREE_OPERAND (t, 1))) | |
453 | return true; | |
454 | /* -(A + B) -> (-A) - B. */ | |
455 | return negate_expr_p (TREE_OPERAND (t, 0)); | |
456 | ||
02a1994c RS |
457 | case MINUS_EXPR: |
458 | /* We can't turn -(A-B) into B-A when we honor signed zeros. */ | |
1b43b967 RS |
459 | return !HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (type)) |
460 | && !HONOR_SIGNED_ZEROS (TYPE_MODE (type)) | |
05d362b8 RS |
461 | && reorder_operands_p (TREE_OPERAND (t, 0), |
462 | TREE_OPERAND (t, 1)); | |
02a1994c | 463 | |
8ab49fef | 464 | case MULT_EXPR: |
8df83eae | 465 | if (TYPE_UNSIGNED (TREE_TYPE (t))) |
8ab49fef RS |
466 | break; |
467 | ||
468 | /* Fall through. */ | |
469 | ||
470 | case RDIV_EXPR: | |
471 | if (! HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (TREE_TYPE (t)))) | |
472 | return negate_expr_p (TREE_OPERAND (t, 1)) | |
473 | || negate_expr_p (TREE_OPERAND (t, 0)); | |
474 | break; | |
475 | ||
965d7fa4 AP |
476 | case TRUNC_DIV_EXPR: |
477 | case ROUND_DIV_EXPR: | |
478 | case FLOOR_DIV_EXPR: | |
479 | case CEIL_DIV_EXPR: | |
480 | case EXACT_DIV_EXPR: | |
6ac01510 ILT |
481 | /* In general we can't negate A / B, because if A is INT_MIN and |
482 | B is 1, we may turn this into INT_MIN / -1 which is undefined | |
483 | and actually traps on some architectures. But if overflow is | |
484 | undefined, we can negate, because - (INT_MIN / 1) is an | |
485 | overflow. */ | |
dbfc2894 RB |
486 | if (INTEGRAL_TYPE_P (TREE_TYPE (t))) |
487 | { | |
488 | if (!TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (t))) | |
489 | break; | |
490 | /* If overflow is undefined then we have to be careful because | |
491 | we ask whether it's ok to associate the negate with the | |
492 | division which is not ok for example for | |
493 | -((a - b) / c) where (-(a - b)) / c may invoke undefined | |
494 | overflow because of negating INT_MIN. So do not use | |
495 | negate_expr_p here but open-code the two important cases. */ | |
496 | if (TREE_CODE (TREE_OPERAND (t, 0)) == NEGATE_EXPR | |
497 | || (TREE_CODE (TREE_OPERAND (t, 0)) == INTEGER_CST | |
498 | && may_negate_without_overflow_p (TREE_OPERAND (t, 0)))) | |
499 | return true; | |
500 | } | |
501 | else if (negate_expr_p (TREE_OPERAND (t, 0))) | |
502 | return true; | |
503 | return negate_expr_p (TREE_OPERAND (t, 1)); | |
965d7fa4 | 504 | |
05d362b8 RS |
505 | case NOP_EXPR: |
506 | /* Negate -((double)float) as (double)(-float). */ | |
507 | if (TREE_CODE (type) == REAL_TYPE) | |
508 | { | |
509 | tree tem = strip_float_extensions (t); | |
510 | if (tem != t) | |
511 | return negate_expr_p (tem); | |
512 | } | |
513 | break; | |
514 | ||
515 | case CALL_EXPR: | |
516 | /* Negate -f(x) as f(-x). */ | |
517 | if (negate_mathfn_p (builtin_mathfn_code (t))) | |
5039610b | 518 | return negate_expr_p (CALL_EXPR_ARG (t, 0)); |
05d362b8 RS |
519 | break; |
520 | ||
239a625e RS |
521 | case RSHIFT_EXPR: |
522 | /* Optimize -((int)x >> 31) into (unsigned)x >> 31. */ | |
523 | if (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST) | |
524 | { | |
525 | tree op1 = TREE_OPERAND (t, 1); | |
526 | if (TREE_INT_CST_HIGH (op1) == 0 | |
527 | && (unsigned HOST_WIDE_INT) (TYPE_PRECISION (type) - 1) | |
528 | == TREE_INT_CST_LOW (op1)) | |
529 | return true; | |
530 | } | |
531 | break; | |
532 | ||
080ea642 RS |
533 | default: |
534 | break; | |
535 | } | |
536 | return false; | |
537 | } | |
538 | ||
1af8dcbf RG |
539 | /* Given T, an expression, return a folded tree for -T or NULL_TREE, if no |
540 | simplification is possible. | |
541 | If negate_expr_p would return true for T, NULL_TREE will never be | |
542 | returned. */ | |
6d716ca8 | 543 | |
1baa375f | 544 | static tree |
db3927fb | 545 | fold_negate_expr (location_t loc, tree t) |
1baa375f | 546 | { |
1af8dcbf | 547 | tree type = TREE_TYPE (t); |
1baa375f RK |
548 | tree tem; |
549 | ||
1baa375f RK |
550 | switch (TREE_CODE (t)) |
551 | { | |
189d4130 AP |
552 | /* Convert - (~A) to A + 1. */ |
553 | case BIT_NOT_EXPR: | |
1af8dcbf | 554 | if (INTEGRAL_TYPE_P (type)) |
db3927fb | 555 | return fold_build2_loc (loc, PLUS_EXPR, type, TREE_OPERAND (t, 0), |
418d1b87 | 556 | build_one_cst (type)); |
8bce9e98 | 557 | break; |
b8698a0f | 558 | |
1baa375f | 559 | case INTEGER_CST: |
33d13fac | 560 | tem = fold_negate_const (t, type); |
ee7d8048 | 561 | if (TREE_OVERFLOW (tem) == TREE_OVERFLOW (t) |
eeef0e45 | 562 | || !TYPE_OVERFLOW_TRAPS (type)) |
1baa375f RK |
563 | return tem; |
564 | break; | |
565 | ||
8ab49fef | 566 | case REAL_CST: |
33d13fac | 567 | tem = fold_negate_const (t, type); |
8ab49fef | 568 | /* Two's complement FP formats, such as c4x, may overflow. */ |
455f14dd | 569 | if (!TREE_OVERFLOW (tem) || !flag_trapping_math) |
1af8dcbf | 570 | return tem; |
8ab49fef RS |
571 | break; |
572 | ||
325217ed CF |
573 | case FIXED_CST: |
574 | tem = fold_negate_const (t, type); | |
575 | return tem; | |
576 | ||
05d362b8 RS |
577 | case COMPLEX_CST: |
578 | { | |
579 | tree rpart = negate_expr (TREE_REALPART (t)); | |
580 | tree ipart = negate_expr (TREE_IMAGPART (t)); | |
581 | ||
582 | if ((TREE_CODE (rpart) == REAL_CST | |
583 | && TREE_CODE (ipart) == REAL_CST) | |
584 | || (TREE_CODE (rpart) == INTEGER_CST | |
585 | && TREE_CODE (ipart) == INTEGER_CST)) | |
586 | return build_complex (type, rpart, ipart); | |
587 | } | |
588 | break; | |
589 | ||
948a1fd9 MG |
590 | case VECTOR_CST: |
591 | { | |
592 | int count = TYPE_VECTOR_SUBPARTS (type), i; | |
593 | tree *elts = XALLOCAVEC (tree, count); | |
594 | ||
595 | for (i = 0; i < count; i++) | |
596 | { | |
597 | elts[i] = fold_negate_expr (loc, VECTOR_CST_ELT (t, i)); | |
598 | if (elts[i] == NULL_TREE) | |
599 | return NULL_TREE; | |
600 | } | |
601 | ||
602 | return build_vector (type, elts); | |
603 | } | |
604 | ||
1aeef526 KG |
605 | case COMPLEX_EXPR: |
606 | if (negate_expr_p (t)) | |
db3927fb AH |
607 | return fold_build2_loc (loc, COMPLEX_EXPR, type, |
608 | fold_negate_expr (loc, TREE_OPERAND (t, 0)), | |
609 | fold_negate_expr (loc, TREE_OPERAND (t, 1))); | |
1aeef526 | 610 | break; |
b8698a0f | 611 | |
8fbbe90b KG |
612 | case CONJ_EXPR: |
613 | if (negate_expr_p (t)) | |
db3927fb AH |
614 | return fold_build1_loc (loc, CONJ_EXPR, type, |
615 | fold_negate_expr (loc, TREE_OPERAND (t, 0))); | |
8fbbe90b KG |
616 | break; |
617 | ||
1baa375f | 618 | case NEGATE_EXPR: |
1af8dcbf | 619 | return TREE_OPERAND (t, 0); |
1baa375f | 620 | |
dfb36f9b | 621 | case PLUS_EXPR: |
1b43b967 RS |
622 | if (!HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (type)) |
623 | && !HONOR_SIGNED_ZEROS (TYPE_MODE (type))) | |
dfb36f9b RS |
624 | { |
625 | /* -(A + B) -> (-B) - A. */ | |
626 | if (negate_expr_p (TREE_OPERAND (t, 1)) | |
627 | && reorder_operands_p (TREE_OPERAND (t, 0), | |
628 | TREE_OPERAND (t, 1))) | |
59ce6d6b RS |
629 | { |
630 | tem = negate_expr (TREE_OPERAND (t, 1)); | |
db3927fb | 631 | return fold_build2_loc (loc, MINUS_EXPR, type, |
1af8dcbf | 632 | tem, TREE_OPERAND (t, 0)); |
59ce6d6b RS |
633 | } |
634 | ||
dfb36f9b RS |
635 | /* -(A + B) -> (-A) - B. */ |
636 | if (negate_expr_p (TREE_OPERAND (t, 0))) | |
59ce6d6b RS |
637 | { |
638 | tem = negate_expr (TREE_OPERAND (t, 0)); | |
db3927fb | 639 | return fold_build2_loc (loc, MINUS_EXPR, type, |
1af8dcbf | 640 | tem, TREE_OPERAND (t, 1)); |
59ce6d6b | 641 | } |
dfb36f9b RS |
642 | } |
643 | break; | |
644 | ||
1baa375f RK |
645 | case MINUS_EXPR: |
646 | /* - (A - B) -> B - A */ | |
1b43b967 RS |
647 | if (!HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (type)) |
648 | && !HONOR_SIGNED_ZEROS (TYPE_MODE (type)) | |
05d362b8 | 649 | && reorder_operands_p (TREE_OPERAND (t, 0), TREE_OPERAND (t, 1))) |
db3927fb | 650 | return fold_build2_loc (loc, MINUS_EXPR, type, |
1af8dcbf | 651 | TREE_OPERAND (t, 1), TREE_OPERAND (t, 0)); |
1baa375f RK |
652 | break; |
653 | ||
8ab49fef | 654 | case MULT_EXPR: |
1af8dcbf | 655 | if (TYPE_UNSIGNED (type)) |
8ab49fef RS |
656 | break; |
657 | ||
658 | /* Fall through. */ | |
659 | ||
660 | case RDIV_EXPR: | |
1af8dcbf | 661 | if (! HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (type))) |
8ab49fef RS |
662 | { |
663 | tem = TREE_OPERAND (t, 1); | |
664 | if (negate_expr_p (tem)) | |
db3927fb | 665 | return fold_build2_loc (loc, TREE_CODE (t), type, |
1af8dcbf | 666 | TREE_OPERAND (t, 0), negate_expr (tem)); |
8ab49fef RS |
667 | tem = TREE_OPERAND (t, 0); |
668 | if (negate_expr_p (tem)) | |
db3927fb | 669 | return fold_build2_loc (loc, TREE_CODE (t), type, |
1af8dcbf | 670 | negate_expr (tem), TREE_OPERAND (t, 1)); |
8ab49fef RS |
671 | } |
672 | break; | |
673 | ||
965d7fa4 AP |
674 | case TRUNC_DIV_EXPR: |
675 | case ROUND_DIV_EXPR: | |
676 | case FLOOR_DIV_EXPR: | |
677 | case CEIL_DIV_EXPR: | |
678 | case EXACT_DIV_EXPR: | |
6ac01510 ILT |
679 | /* In general we can't negate A / B, because if A is INT_MIN and |
680 | B is 1, we may turn this into INT_MIN / -1 which is undefined | |
681 | and actually traps on some architectures. But if overflow is | |
682 | undefined, we can negate, because - (INT_MIN / 1) is an | |
683 | overflow. */ | |
eeef0e45 | 684 | if (!INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_UNDEFINED (type)) |
965d7fa4 | 685 | { |
6ac01510 ILT |
686 | const char * const warnmsg = G_("assuming signed overflow does not " |
687 | "occur when negating a division"); | |
965d7fa4 AP |
688 | tem = TREE_OPERAND (t, 1); |
689 | if (negate_expr_p (tem)) | |
6ac01510 ILT |
690 | { |
691 | if (INTEGRAL_TYPE_P (type) | |
692 | && (TREE_CODE (tem) != INTEGER_CST | |
693 | || integer_onep (tem))) | |
694 | fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_MISC); | |
db3927fb | 695 | return fold_build2_loc (loc, TREE_CODE (t), type, |
6ac01510 ILT |
696 | TREE_OPERAND (t, 0), negate_expr (tem)); |
697 | } | |
dbfc2894 RB |
698 | /* If overflow is undefined then we have to be careful because |
699 | we ask whether it's ok to associate the negate with the | |
700 | division which is not ok for example for | |
701 | -((a - b) / c) where (-(a - b)) / c may invoke undefined | |
702 | overflow because of negating INT_MIN. So do not use | |
703 | negate_expr_p here but open-code the two important cases. */ | |
965d7fa4 | 704 | tem = TREE_OPERAND (t, 0); |
dbfc2894 RB |
705 | if ((INTEGRAL_TYPE_P (type) |
706 | && (TREE_CODE (tem) == NEGATE_EXPR | |
707 | || (TREE_CODE (tem) == INTEGER_CST | |
708 | && may_negate_without_overflow_p (tem)))) | |
709 | || !INTEGRAL_TYPE_P (type)) | |
710 | return fold_build2_loc (loc, TREE_CODE (t), type, | |
711 | negate_expr (tem), TREE_OPERAND (t, 1)); | |
965d7fa4 AP |
712 | } |
713 | break; | |
714 | ||
05d362b8 RS |
715 | case NOP_EXPR: |
716 | /* Convert -((double)float) into (double)(-float). */ | |
717 | if (TREE_CODE (type) == REAL_TYPE) | |
718 | { | |
719 | tem = strip_float_extensions (t); | |
720 | if (tem != t && negate_expr_p (tem)) | |
db3927fb | 721 | return fold_convert_loc (loc, type, negate_expr (tem)); |
05d362b8 RS |
722 | } |
723 | break; | |
724 | ||
725 | case CALL_EXPR: | |
726 | /* Negate -f(x) as f(-x). */ | |
727 | if (negate_mathfn_p (builtin_mathfn_code (t)) | |
5039610b | 728 | && negate_expr_p (CALL_EXPR_ARG (t, 0))) |
05d362b8 | 729 | { |
5039610b | 730 | tree fndecl, arg; |
05d362b8 RS |
731 | |
732 | fndecl = get_callee_fndecl (t); | |
5039610b | 733 | arg = negate_expr (CALL_EXPR_ARG (t, 0)); |
db3927fb | 734 | return build_call_expr_loc (loc, fndecl, 1, arg); |
05d362b8 RS |
735 | } |
736 | break; | |
737 | ||
239a625e RS |
738 | case RSHIFT_EXPR: |
739 | /* Optimize -((int)x >> 31) into (unsigned)x >> 31. */ | |
740 | if (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST) | |
741 | { | |
742 | tree op1 = TREE_OPERAND (t, 1); | |
743 | if (TREE_INT_CST_HIGH (op1) == 0 | |
744 | && (unsigned HOST_WIDE_INT) (TYPE_PRECISION (type) - 1) | |
745 | == TREE_INT_CST_LOW (op1)) | |
746 | { | |
8df83eae | 747 | tree ntype = TYPE_UNSIGNED (type) |
12753674 | 748 | ? signed_type_for (type) |
ca5ba2a3 | 749 | : unsigned_type_for (type); |
db3927fb AH |
750 | tree temp = fold_convert_loc (loc, ntype, TREE_OPERAND (t, 0)); |
751 | temp = fold_build2_loc (loc, RSHIFT_EXPR, ntype, temp, op1); | |
752 | return fold_convert_loc (loc, type, temp); | |
239a625e RS |
753 | } |
754 | } | |
755 | break; | |
756 | ||
1baa375f RK |
757 | default: |
758 | break; | |
759 | } | |
760 | ||
1af8dcbf RG |
761 | return NULL_TREE; |
762 | } | |
763 | ||
764 | /* Like fold_negate_expr, but return a NEGATE_EXPR tree, if T can not be | |
765 | negated in a simpler way. Also allow for T to be NULL_TREE, in which case | |
766 | return NULL_TREE. */ | |
767 | ||
768 | static tree | |
769 | negate_expr (tree t) | |
770 | { | |
771 | tree type, tem; | |
db3927fb | 772 | location_t loc; |
1af8dcbf RG |
773 | |
774 | if (t == NULL_TREE) | |
775 | return NULL_TREE; | |
776 | ||
db3927fb | 777 | loc = EXPR_LOCATION (t); |
1af8dcbf RG |
778 | type = TREE_TYPE (t); |
779 | STRIP_SIGN_NOPS (t); | |
780 | ||
db3927fb | 781 | tem = fold_negate_expr (loc, t); |
1af8dcbf | 782 | if (!tem) |
c9019218 | 783 | tem = build1_loc (loc, NEGATE_EXPR, TREE_TYPE (t), t); |
db3927fb | 784 | return fold_convert_loc (loc, type, tem); |
1baa375f RK |
785 | } |
786 | \f | |
787 | /* Split a tree IN into a constant, literal and variable parts that could be | |
788 | combined with CODE to make IN. "constant" means an expression with | |
789 | TREE_CONSTANT but that isn't an actual constant. CODE must be a | |
790 | commutative arithmetic operation. Store the constant part into *CONP, | |
cff27795 | 791 | the literal in *LITP and return the variable part. If a part isn't |
1baa375f RK |
792 | present, set it to null. If the tree does not decompose in this way, |
793 | return the entire tree as the variable part and the other parts as null. | |
794 | ||
795 | If CODE is PLUS_EXPR we also split trees that use MINUS_EXPR. In that | |
cff27795 EB |
796 | case, we negate an operand that was subtracted. Except if it is a |
797 | literal for which we use *MINUS_LITP instead. | |
798 | ||
799 | If NEGATE_P is true, we are negating all of IN, again except a literal | |
800 | for which we use *MINUS_LITP instead. | |
1baa375f RK |
801 | |
802 | If IN is itself a literal or constant, return it as appropriate. | |
803 | ||
804 | Note that we do not guarantee that any of the three values will be the | |
805 | same type as IN, but they will have the same signedness and mode. */ | |
806 | ||
807 | static tree | |
75040a04 AJ |
808 | split_tree (tree in, enum tree_code code, tree *conp, tree *litp, |
809 | tree *minus_litp, int negate_p) | |
6d716ca8 | 810 | { |
1baa375f RK |
811 | tree var = 0; |
812 | ||
6d716ca8 | 813 | *conp = 0; |
1baa375f | 814 | *litp = 0; |
cff27795 | 815 | *minus_litp = 0; |
1baa375f | 816 | |
30f7a378 | 817 | /* Strip any conversions that don't change the machine mode or signedness. */ |
1baa375f RK |
818 | STRIP_SIGN_NOPS (in); |
819 | ||
325217ed CF |
820 | if (TREE_CODE (in) == INTEGER_CST || TREE_CODE (in) == REAL_CST |
821 | || TREE_CODE (in) == FIXED_CST) | |
1baa375f | 822 | *litp = in; |
1baa375f | 823 | else if (TREE_CODE (in) == code |
41bb1f06 | 824 | || ((! FLOAT_TYPE_P (TREE_TYPE (in)) || flag_associative_math) |
325217ed | 825 | && ! SAT_FIXED_POINT_TYPE_P (TREE_TYPE (in)) |
1baa375f RK |
826 | /* We can associate addition and subtraction together (even |
827 | though the C standard doesn't say so) for integers because | |
828 | the value is not affected. For reals, the value might be | |
829 | affected, so we can't. */ | |
830 | && ((code == PLUS_EXPR && TREE_CODE (in) == MINUS_EXPR) | |
831 | || (code == MINUS_EXPR && TREE_CODE (in) == PLUS_EXPR)))) | |
832 | { | |
833 | tree op0 = TREE_OPERAND (in, 0); | |
834 | tree op1 = TREE_OPERAND (in, 1); | |
835 | int neg1_p = TREE_CODE (in) == MINUS_EXPR; | |
836 | int neg_litp_p = 0, neg_conp_p = 0, neg_var_p = 0; | |
837 | ||
838 | /* First see if either of the operands is a literal, then a constant. */ | |
325217ed CF |
839 | if (TREE_CODE (op0) == INTEGER_CST || TREE_CODE (op0) == REAL_CST |
840 | || TREE_CODE (op0) == FIXED_CST) | |
1baa375f | 841 | *litp = op0, op0 = 0; |
325217ed CF |
842 | else if (TREE_CODE (op1) == INTEGER_CST || TREE_CODE (op1) == REAL_CST |
843 | || TREE_CODE (op1) == FIXED_CST) | |
1baa375f RK |
844 | *litp = op1, neg_litp_p = neg1_p, op1 = 0; |
845 | ||
846 | if (op0 != 0 && TREE_CONSTANT (op0)) | |
847 | *conp = op0, op0 = 0; | |
848 | else if (op1 != 0 && TREE_CONSTANT (op1)) | |
849 | *conp = op1, neg_conp_p = neg1_p, op1 = 0; | |
850 | ||
851 | /* If we haven't dealt with either operand, this is not a case we can | |
30f7a378 | 852 | decompose. Otherwise, VAR is either of the ones remaining, if any. */ |
1baa375f RK |
853 | if (op0 != 0 && op1 != 0) |
854 | var = in; | |
855 | else if (op0 != 0) | |
856 | var = op0; | |
857 | else | |
858 | var = op1, neg_var_p = neg1_p; | |
6d716ca8 | 859 | |
1baa375f | 860 | /* Now do any needed negations. */ |
cff27795 EB |
861 | if (neg_litp_p) |
862 | *minus_litp = *litp, *litp = 0; | |
863 | if (neg_conp_p) | |
864 | *conp = negate_expr (*conp); | |
865 | if (neg_var_p) | |
866 | var = negate_expr (var); | |
1baa375f | 867 | } |
3068819a RB |
868 | else if (TREE_CODE (in) == BIT_NOT_EXPR |
869 | && code == PLUS_EXPR) | |
870 | { | |
871 | /* -X - 1 is folded to ~X, undo that here. */ | |
872 | *minus_litp = build_one_cst (TREE_TYPE (in)); | |
873 | var = negate_expr (TREE_OPERAND (in, 0)); | |
874 | } | |
1796dff4 RH |
875 | else if (TREE_CONSTANT (in)) |
876 | *conp = in; | |
1baa375f RK |
877 | else |
878 | var = in; | |
879 | ||
880 | if (negate_p) | |
6d716ca8 | 881 | { |
cff27795 EB |
882 | if (*litp) |
883 | *minus_litp = *litp, *litp = 0; | |
884 | else if (*minus_litp) | |
885 | *litp = *minus_litp, *minus_litp = 0; | |
1baa375f | 886 | *conp = negate_expr (*conp); |
cff27795 | 887 | var = negate_expr (var); |
6d716ca8 | 888 | } |
1baa375f RK |
889 | |
890 | return var; | |
891 | } | |
892 | ||
db3927fb AH |
893 | /* Re-associate trees split by the above function. T1 and T2 are |
894 | either expressions to associate or null. Return the new | |
895 | expression, if any. LOC is the location of the new expression. If | |
cff27795 | 896 | we build an operation, do it in TYPE and with CODE. */ |
1baa375f RK |
897 | |
898 | static tree | |
db3927fb | 899 | associate_trees (location_t loc, tree t1, tree t2, enum tree_code code, tree type) |
1baa375f | 900 | { |
1baa375f RK |
901 | if (t1 == 0) |
902 | return t2; | |
903 | else if (t2 == 0) | |
904 | return t1; | |
905 | ||
1baa375f RK |
906 | /* If either input is CODE, a PLUS_EXPR, or a MINUS_EXPR, don't |
907 | try to fold this since we will have infinite recursion. But do | |
908 | deal with any NEGATE_EXPRs. */ | |
909 | if (TREE_CODE (t1) == code || TREE_CODE (t2) == code | |
910 | || TREE_CODE (t1) == MINUS_EXPR || TREE_CODE (t2) == MINUS_EXPR) | |
911 | { | |
1bed5ee3 JJ |
912 | if (code == PLUS_EXPR) |
913 | { | |
914 | if (TREE_CODE (t1) == NEGATE_EXPR) | |
c9019218 JJ |
915 | return build2_loc (loc, MINUS_EXPR, type, |
916 | fold_convert_loc (loc, type, t2), | |
917 | fold_convert_loc (loc, type, | |
918 | TREE_OPERAND (t1, 0))); | |
1bed5ee3 | 919 | else if (TREE_CODE (t2) == NEGATE_EXPR) |
c9019218 JJ |
920 | return build2_loc (loc, MINUS_EXPR, type, |
921 | fold_convert_loc (loc, type, t1), | |
922 | fold_convert_loc (loc, type, | |
923 | TREE_OPERAND (t2, 0))); | |
18522563 | 924 | else if (integer_zerop (t2)) |
db3927fb | 925 | return fold_convert_loc (loc, type, t1); |
1bed5ee3 | 926 | } |
18522563 ZD |
927 | else if (code == MINUS_EXPR) |
928 | { | |
929 | if (integer_zerop (t2)) | |
db3927fb | 930 | return fold_convert_loc (loc, type, t1); |
18522563 ZD |
931 | } |
932 | ||
c9019218 JJ |
933 | return build2_loc (loc, code, type, fold_convert_loc (loc, type, t1), |
934 | fold_convert_loc (loc, type, t2)); | |
1baa375f RK |
935 | } |
936 | ||
db3927fb | 937 | return fold_build2_loc (loc, code, type, fold_convert_loc (loc, type, t1), |
c9019218 | 938 | fold_convert_loc (loc, type, t2)); |
6d716ca8 RS |
939 | } |
940 | \f | |
000d8d44 RS |
941 | /* Check whether TYPE1 and TYPE2 are equivalent integer types, suitable |
942 | for use in int_const_binop, size_binop and size_diffop. */ | |
943 | ||
944 | static bool | |
ac545c64 | 945 | int_binop_types_match_p (enum tree_code code, const_tree type1, const_tree type2) |
000d8d44 | 946 | { |
f3ef18ff | 947 | if (!INTEGRAL_TYPE_P (type1) && !POINTER_TYPE_P (type1)) |
000d8d44 | 948 | return false; |
f3ef18ff | 949 | if (!INTEGRAL_TYPE_P (type2) && !POINTER_TYPE_P (type2)) |
000d8d44 RS |
950 | return false; |
951 | ||
952 | switch (code) | |
953 | { | |
954 | case LSHIFT_EXPR: | |
955 | case RSHIFT_EXPR: | |
956 | case LROTATE_EXPR: | |
957 | case RROTATE_EXPR: | |
958 | return true; | |
959 | ||
960 | default: | |
961 | break; | |
962 | } | |
963 | ||
964 | return TYPE_UNSIGNED (type1) == TYPE_UNSIGNED (type2) | |
965 | && TYPE_PRECISION (type1) == TYPE_PRECISION (type2) | |
966 | && TYPE_MODE (type1) == TYPE_MODE (type2); | |
967 | } | |
968 | ||
969 | ||
e9a25f70 | 970 | /* Combine two integer constants ARG1 and ARG2 under operation CODE |
fd6c76f4 | 971 | to produce a new constant. Return NULL_TREE if we don't know how |
d35936ab | 972 | to evaluate CODE at compile-time. */ |
6d716ca8 | 973 | |
56099f00 RG |
974 | static tree |
975 | int_const_binop_1 (enum tree_code code, const_tree arg1, const_tree arg2, | |
976 | int overflowable) | |
6d716ca8 | 977 | { |
fd7de64c | 978 | double_int op1, op2, res, tmp; |
b3694847 | 979 | tree t; |
4c160717 | 980 | tree type = TREE_TYPE (arg1); |
fd7de64c | 981 | bool uns = TYPE_UNSIGNED (type); |
fd7de64c | 982 | bool overflow = false; |
3dedc65a | 983 | |
fd7de64c AS |
984 | op1 = tree_to_double_int (arg1); |
985 | op2 = tree_to_double_int (arg2); | |
e9a25f70 JL |
986 | |
987 | switch (code) | |
6d716ca8 | 988 | { |
e9a25f70 | 989 | case BIT_IOR_EXPR: |
27bcd47c | 990 | res = op1 | op2; |
e9a25f70 | 991 | break; |
6d716ca8 | 992 | |
e9a25f70 | 993 | case BIT_XOR_EXPR: |
27bcd47c | 994 | res = op1 ^ op2; |
e9a25f70 | 995 | break; |
6d716ca8 | 996 | |
e9a25f70 | 997 | case BIT_AND_EXPR: |
27bcd47c | 998 | res = op1 & op2; |
e9a25f70 | 999 | break; |
6d716ca8 | 1000 | |
e9a25f70 | 1001 | case RSHIFT_EXPR: |
27bcd47c | 1002 | res = op1.rshift (op2.to_shwi (), TYPE_PRECISION (type), !uns); |
fd7de64c AS |
1003 | break; |
1004 | ||
e9a25f70 JL |
1005 | case LSHIFT_EXPR: |
1006 | /* It's unclear from the C standard whether shifts can overflow. | |
1007 | The following code ignores overflow; perhaps a C standard | |
1008 | interpretation ruling is needed. */ | |
27bcd47c | 1009 | res = op1.lshift (op2.to_shwi (), TYPE_PRECISION (type), !uns); |
e9a25f70 | 1010 | break; |
6d716ca8 | 1011 | |
e9a25f70 | 1012 | case RROTATE_EXPR: |
27bcd47c | 1013 | res = op1.rrotate (op2.to_shwi (), TYPE_PRECISION (type)); |
fd7de64c AS |
1014 | break; |
1015 | ||
e9a25f70 | 1016 | case LROTATE_EXPR: |
27bcd47c | 1017 | res = op1.lrotate (op2.to_shwi (), TYPE_PRECISION (type)); |
e9a25f70 | 1018 | break; |
6d716ca8 | 1019 | |
e9a25f70 | 1020 | case PLUS_EXPR: |
27bcd47c | 1021 | res = op1.add_with_sign (op2, false, &overflow); |
e9a25f70 | 1022 | break; |
6d716ca8 | 1023 | |
e9a25f70 | 1024 | case MINUS_EXPR: |
9be0ac8c | 1025 | res = op1.sub_with_overflow (op2, &overflow); |
e9a25f70 | 1026 | break; |
6d716ca8 | 1027 | |
e9a25f70 | 1028 | case MULT_EXPR: |
27bcd47c | 1029 | res = op1.mul_with_sign (op2, false, &overflow); |
e9a25f70 | 1030 | break; |
6d716ca8 | 1031 | |
98449720 | 1032 | case MULT_HIGHPART_EXPR: |
98449720 | 1033 | if (TYPE_PRECISION (type) > HOST_BITS_PER_WIDE_INT) |
06f9b387 JJ |
1034 | { |
1035 | bool dummy_overflow; | |
1036 | if (TYPE_PRECISION (type) != 2 * HOST_BITS_PER_WIDE_INT) | |
1037 | return NULL_TREE; | |
1038 | op1.wide_mul_with_sign (op2, uns, &res, &dummy_overflow); | |
1039 | } | |
1040 | else | |
1041 | { | |
1042 | bool dummy_overflow; | |
1043 | /* MULT_HIGHPART_EXPR can't ever oveflow, as the multiplication | |
1044 | is performed in twice the precision of arguments. */ | |
1045 | tmp = op1.mul_with_sign (op2, false, &dummy_overflow); | |
1046 | res = tmp.rshift (TYPE_PRECISION (type), | |
1047 | 2 * TYPE_PRECISION (type), !uns); | |
1048 | } | |
98449720 RH |
1049 | break; |
1050 | ||
e9a25f70 JL |
1051 | case TRUNC_DIV_EXPR: |
1052 | case FLOOR_DIV_EXPR: case CEIL_DIV_EXPR: | |
1053 | case EXACT_DIV_EXPR: | |
1054 | /* This is a shortcut for a common special case. */ | |
fd7de64c | 1055 | if (op2.high == 0 && (HOST_WIDE_INT) op2.low > 0 |
455f14dd RS |
1056 | && !TREE_OVERFLOW (arg1) |
1057 | && !TREE_OVERFLOW (arg2) | |
fd7de64c | 1058 | && op1.high == 0 && (HOST_WIDE_INT) op1.low >= 0) |
e9a25f70 JL |
1059 | { |
1060 | if (code == CEIL_DIV_EXPR) | |
fd7de64c | 1061 | op1.low += op2.low - 1; |
05bccae2 | 1062 | |
fd7de64c | 1063 | res.low = op1.low / op2.low, res.high = 0; |
6d716ca8 | 1064 | break; |
e9a25f70 | 1065 | } |
6d716ca8 | 1066 | |
30f7a378 | 1067 | /* ... fall through ... */ |
6d716ca8 | 1068 | |
b6cc0a72 | 1069 | case ROUND_DIV_EXPR: |
27bcd47c | 1070 | if (op2.is_zero ()) |
fd6c76f4 | 1071 | return NULL_TREE; |
27bcd47c | 1072 | if (op2.is_one ()) |
e9a25f70 | 1073 | { |
fd7de64c | 1074 | res = op1; |
6d716ca8 | 1075 | break; |
e9a25f70 | 1076 | } |
27bcd47c | 1077 | if (op1 == op2 && !op1.is_zero ()) |
e9a25f70 | 1078 | { |
fd7de64c | 1079 | res = double_int_one; |
63e7fe9b | 1080 | break; |
e9a25f70 | 1081 | } |
9be0ac8c | 1082 | res = op1.divmod_with_overflow (op2, uns, code, &tmp, &overflow); |
e9a25f70 | 1083 | break; |
63e7fe9b | 1084 | |
e9a25f70 JL |
1085 | case TRUNC_MOD_EXPR: |
1086 | case FLOOR_MOD_EXPR: case CEIL_MOD_EXPR: | |
1087 | /* This is a shortcut for a common special case. */ | |
fd7de64c | 1088 | if (op2.high == 0 && (HOST_WIDE_INT) op2.low > 0 |
455f14dd RS |
1089 | && !TREE_OVERFLOW (arg1) |
1090 | && !TREE_OVERFLOW (arg2) | |
fd7de64c | 1091 | && op1.high == 0 && (HOST_WIDE_INT) op1.low >= 0) |
e9a25f70 JL |
1092 | { |
1093 | if (code == CEIL_MOD_EXPR) | |
fd7de64c AS |
1094 | op1.low += op2.low - 1; |
1095 | res.low = op1.low % op2.low, res.high = 0; | |
63e7fe9b | 1096 | break; |
e9a25f70 | 1097 | } |
63e7fe9b | 1098 | |
30f7a378 | 1099 | /* ... fall through ... */ |
e9a25f70 | 1100 | |
b6cc0a72 | 1101 | case ROUND_MOD_EXPR: |
27bcd47c | 1102 | if (op2.is_zero ()) |
fd6c76f4 | 1103 | return NULL_TREE; |
9be0ac8c | 1104 | tmp = op1.divmod_with_overflow (op2, uns, code, &res, &overflow); |
e9a25f70 JL |
1105 | break; |
1106 | ||
1107 | case MIN_EXPR: | |
27bcd47c | 1108 | res = op1.min (op2, uns); |
fd7de64c | 1109 | break; |
d4b60170 | 1110 | |
fd7de64c | 1111 | case MAX_EXPR: |
27bcd47c | 1112 | res = op1.max (op2, uns); |
e9a25f70 | 1113 | break; |
3dedc65a | 1114 | |
e9a25f70 | 1115 | default: |
fd6c76f4 | 1116 | return NULL_TREE; |
3dedc65a | 1117 | } |
e9a25f70 | 1118 | |
56099f00 | 1119 | t = force_fit_type_double (TREE_TYPE (arg1), res, overflowable, |
3ac8781c | 1120 | (!uns && overflow) |
d35936ab | 1121 | | TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2)); |
3e6688a7 | 1122 | |
e9a25f70 JL |
1123 | return t; |
1124 | } | |
1125 | ||
56099f00 RG |
1126 | tree |
1127 | int_const_binop (enum tree_code code, const_tree arg1, const_tree arg2) | |
1128 | { | |
1129 | return int_const_binop_1 (code, arg1, arg2, 1); | |
1130 | } | |
1131 | ||
d4b60170 RK |
1132 | /* Combine two constants ARG1 and ARG2 under operation CODE to produce a new |
1133 | constant. We assume ARG1 and ARG2 have the same data type, or at least | |
858214db | 1134 | are the same kind of constant and the same machine mode. Return zero if |
43a5d30b | 1135 | combining the constants is not allowed in the current operating mode. */ |
e9a25f70 JL |
1136 | |
1137 | static tree | |
43a5d30b | 1138 | const_binop (enum tree_code code, tree arg1, tree arg2) |
e9a25f70 | 1139 | { |
858214db EB |
1140 | /* Sanity check for the recursive cases. */ |
1141 | if (!arg1 || !arg2) | |
1142 | return NULL_TREE; | |
1143 | ||
b6cc0a72 KH |
1144 | STRIP_NOPS (arg1); |
1145 | STRIP_NOPS (arg2); | |
e9a25f70 JL |
1146 | |
1147 | if (TREE_CODE (arg1) == INTEGER_CST) | |
d35936ab | 1148 | return int_const_binop (code, arg1, arg2); |
e9a25f70 | 1149 | |
6d716ca8 RS |
1150 | if (TREE_CODE (arg1) == REAL_CST) |
1151 | { | |
3e4093b6 | 1152 | enum machine_mode mode; |
79c844cd RK |
1153 | REAL_VALUE_TYPE d1; |
1154 | REAL_VALUE_TYPE d2; | |
15e5ad76 | 1155 | REAL_VALUE_TYPE value; |
d284eb28 RS |
1156 | REAL_VALUE_TYPE result; |
1157 | bool inexact; | |
3e4093b6 | 1158 | tree t, type; |
6d716ca8 | 1159 | |
fd6c76f4 RS |
1160 | /* The following codes are handled by real_arithmetic. */ |
1161 | switch (code) | |
1162 | { | |
1163 | case PLUS_EXPR: | |
1164 | case MINUS_EXPR: | |
1165 | case MULT_EXPR: | |
1166 | case RDIV_EXPR: | |
1167 | case MIN_EXPR: | |
1168 | case MAX_EXPR: | |
1169 | break; | |
1170 | ||
1171 | default: | |
1172 | return NULL_TREE; | |
1173 | } | |
1174 | ||
79c844cd RK |
1175 | d1 = TREE_REAL_CST (arg1); |
1176 | d2 = TREE_REAL_CST (arg2); | |
5f610074 | 1177 | |
3e4093b6 RS |
1178 | type = TREE_TYPE (arg1); |
1179 | mode = TYPE_MODE (type); | |
1180 | ||
1181 | /* Don't perform operation if we honor signaling NaNs and | |
1182 | either operand is a NaN. */ | |
1183 | if (HONOR_SNANS (mode) | |
1184 | && (REAL_VALUE_ISNAN (d1) || REAL_VALUE_ISNAN (d2))) | |
1185 | return NULL_TREE; | |
1186 | ||
1187 | /* Don't perform operation if it would raise a division | |
1188 | by zero exception. */ | |
1189 | if (code == RDIV_EXPR | |
1190 | && REAL_VALUES_EQUAL (d2, dconst0) | |
1191 | && (flag_trapping_math || ! MODE_HAS_INFINITIES (mode))) | |
1192 | return NULL_TREE; | |
1193 | ||
5f610074 RK |
1194 | /* If either operand is a NaN, just return it. Otherwise, set up |
1195 | for floating-point trap; we return an overflow. */ | |
1196 | if (REAL_VALUE_ISNAN (d1)) | |
1197 | return arg1; | |
1198 | else if (REAL_VALUE_ISNAN (d2)) | |
1199 | return arg2; | |
a4d3481d | 1200 | |
d284eb28 RS |
1201 | inexact = real_arithmetic (&value, code, &d1, &d2); |
1202 | real_convert (&result, mode, &value); | |
b6cc0a72 | 1203 | |
68328cda EB |
1204 | /* Don't constant fold this floating point operation if |
1205 | the result has overflowed and flag_trapping_math. */ | |
68328cda EB |
1206 | if (flag_trapping_math |
1207 | && MODE_HAS_INFINITIES (mode) | |
1208 | && REAL_VALUE_ISINF (result) | |
1209 | && !REAL_VALUE_ISINF (d1) | |
1210 | && !REAL_VALUE_ISINF (d2)) | |
1211 | return NULL_TREE; | |
1212 | ||
d284eb28 RS |
1213 | /* Don't constant fold this floating point operation if the |
1214 | result may dependent upon the run-time rounding mode and | |
762297d9 RS |
1215 | flag_rounding_math is set, or if GCC's software emulation |
1216 | is unable to accurately represent the result. */ | |
762297d9 | 1217 | if ((flag_rounding_math |
4099e2c2 | 1218 | || (MODE_COMPOSITE_P (mode) && !flag_unsafe_math_optimizations)) |
d284eb28 RS |
1219 | && (inexact || !real_identical (&result, &value))) |
1220 | return NULL_TREE; | |
1221 | ||
1222 | t = build_real (type, result); | |
649ff3b4 | 1223 | |
ca7a3bd7 | 1224 | TREE_OVERFLOW (t) = TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2); |
7c7b029d | 1225 | return t; |
6d716ca8 | 1226 | } |
fd6c76f4 | 1227 | |
325217ed CF |
1228 | if (TREE_CODE (arg1) == FIXED_CST) |
1229 | { | |
1230 | FIXED_VALUE_TYPE f1; | |
1231 | FIXED_VALUE_TYPE f2; | |
1232 | FIXED_VALUE_TYPE result; | |
1233 | tree t, type; | |
1234 | int sat_p; | |
1235 | bool overflow_p; | |
1236 | ||
1237 | /* The following codes are handled by fixed_arithmetic. */ | |
1238 | switch (code) | |
1239 | { | |
1240 | case PLUS_EXPR: | |
1241 | case MINUS_EXPR: | |
1242 | case MULT_EXPR: | |
1243 | case TRUNC_DIV_EXPR: | |
1244 | f2 = TREE_FIXED_CST (arg2); | |
1245 | break; | |
1246 | ||
1247 | case LSHIFT_EXPR: | |
1248 | case RSHIFT_EXPR: | |
1249 | f2.data.high = TREE_INT_CST_HIGH (arg2); | |
1250 | f2.data.low = TREE_INT_CST_LOW (arg2); | |
1251 | f2.mode = SImode; | |
1252 | break; | |
1253 | ||
1254 | default: | |
1255 | return NULL_TREE; | |
1256 | } | |
1257 | ||
1258 | f1 = TREE_FIXED_CST (arg1); | |
1259 | type = TREE_TYPE (arg1); | |
1260 | sat_p = TYPE_SATURATING (type); | |
1261 | overflow_p = fixed_arithmetic (&result, code, &f1, &f2, sat_p); | |
1262 | t = build_fixed (type, result); | |
1263 | /* Propagate overflow flags. */ | |
1264 | if (overflow_p | TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2)) | |
28ddeea1 | 1265 | TREE_OVERFLOW (t) = 1; |
325217ed CF |
1266 | return t; |
1267 | } | |
1268 | ||
6d716ca8 RS |
1269 | if (TREE_CODE (arg1) == COMPLEX_CST) |
1270 | { | |
b3694847 SS |
1271 | tree type = TREE_TYPE (arg1); |
1272 | tree r1 = TREE_REALPART (arg1); | |
1273 | tree i1 = TREE_IMAGPART (arg1); | |
1274 | tree r2 = TREE_REALPART (arg2); | |
1275 | tree i2 = TREE_IMAGPART (arg2); | |
858214db | 1276 | tree real, imag; |
6d716ca8 RS |
1277 | |
1278 | switch (code) | |
1279 | { | |
1280 | case PLUS_EXPR: | |
6d716ca8 | 1281 | case MINUS_EXPR: |
43a5d30b AS |
1282 | real = const_binop (code, r1, r2); |
1283 | imag = const_binop (code, i1, i2); | |
6d716ca8 RS |
1284 | break; |
1285 | ||
1286 | case MULT_EXPR: | |
2f440f6a | 1287 | if (COMPLEX_FLOAT_TYPE_P (type)) |
ca75b926 KG |
1288 | return do_mpc_arg2 (arg1, arg2, type, |
1289 | /* do_nonfinite= */ folding_initializer, | |
1290 | mpc_mul); | |
2f440f6a | 1291 | |
858214db | 1292 | real = const_binop (MINUS_EXPR, |
43a5d30b AS |
1293 | const_binop (MULT_EXPR, r1, r2), |
1294 | const_binop (MULT_EXPR, i1, i2)); | |
858214db | 1295 | imag = const_binop (PLUS_EXPR, |
43a5d30b AS |
1296 | const_binop (MULT_EXPR, r1, i2), |
1297 | const_binop (MULT_EXPR, i1, r2)); | |
6d716ca8 RS |
1298 | break; |
1299 | ||
1300 | case RDIV_EXPR: | |
2f440f6a | 1301 | if (COMPLEX_FLOAT_TYPE_P (type)) |
ca75b926 KG |
1302 | return do_mpc_arg2 (arg1, arg2, type, |
1303 | /* do_nonfinite= */ folding_initializer, | |
1304 | mpc_div); | |
e3d5405d | 1305 | /* Fallthru ... */ |
e3d5405d KG |
1306 | case TRUNC_DIV_EXPR: |
1307 | case CEIL_DIV_EXPR: | |
1308 | case FLOOR_DIV_EXPR: | |
1309 | case ROUND_DIV_EXPR: | |
1310 | if (flag_complex_method == 0) | |
6d716ca8 | 1311 | { |
e3d5405d KG |
1312 | /* Keep this algorithm in sync with |
1313 | tree-complex.c:expand_complex_div_straight(). | |
1314 | ||
1315 | Expand complex division to scalars, straightforward algorithm. | |
1316 | a / b = ((ar*br + ai*bi)/t) + i((ai*br - ar*bi)/t) | |
1317 | t = br*br + bi*bi | |
1318 | */ | |
b3694847 | 1319 | tree magsquared |
6d716ca8 | 1320 | = const_binop (PLUS_EXPR, |
43a5d30b AS |
1321 | const_binop (MULT_EXPR, r2, r2), |
1322 | const_binop (MULT_EXPR, i2, i2)); | |
858214db EB |
1323 | tree t1 |
1324 | = const_binop (PLUS_EXPR, | |
43a5d30b AS |
1325 | const_binop (MULT_EXPR, r1, r2), |
1326 | const_binop (MULT_EXPR, i1, i2)); | |
858214db EB |
1327 | tree t2 |
1328 | = const_binop (MINUS_EXPR, | |
43a5d30b AS |
1329 | const_binop (MULT_EXPR, i1, r2), |
1330 | const_binop (MULT_EXPR, r1, i2)); | |
c10166c4 | 1331 | |
43a5d30b AS |
1332 | real = const_binop (code, t1, magsquared); |
1333 | imag = const_binop (code, t2, magsquared); | |
6d716ca8 | 1334 | } |
e3d5405d KG |
1335 | else |
1336 | { | |
1337 | /* Keep this algorithm in sync with | |
1338 | tree-complex.c:expand_complex_div_wide(). | |
1339 | ||
1340 | Expand complex division to scalars, modified algorithm to minimize | |
1341 | overflow with wide input ranges. */ | |
08d19889 KG |
1342 | tree compare = fold_build2 (LT_EXPR, boolean_type_node, |
1343 | fold_abs_const (r2, TREE_TYPE (type)), | |
1344 | fold_abs_const (i2, TREE_TYPE (type))); | |
b8698a0f | 1345 | |
e3d5405d KG |
1346 | if (integer_nonzerop (compare)) |
1347 | { | |
1348 | /* In the TRUE branch, we compute | |
1349 | ratio = br/bi; | |
1350 | div = (br * ratio) + bi; | |
1351 | tr = (ar * ratio) + ai; | |
1352 | ti = (ai * ratio) - ar; | |
1353 | tr = tr / div; | |
1354 | ti = ti / div; */ | |
43a5d30b | 1355 | tree ratio = const_binop (code, r2, i2); |
08d19889 | 1356 | tree div = const_binop (PLUS_EXPR, i2, |
43a5d30b AS |
1357 | const_binop (MULT_EXPR, r2, ratio)); |
1358 | real = const_binop (MULT_EXPR, r1, ratio); | |
1359 | real = const_binop (PLUS_EXPR, real, i1); | |
1360 | real = const_binop (code, real, div); | |
1361 | ||
1362 | imag = const_binop (MULT_EXPR, i1, ratio); | |
1363 | imag = const_binop (MINUS_EXPR, imag, r1); | |
1364 | imag = const_binop (code, imag, div); | |
e3d5405d KG |
1365 | } |
1366 | else | |
1367 | { | |
1368 | /* In the FALSE branch, we compute | |
1369 | ratio = d/c; | |
1370 | divisor = (d * ratio) + c; | |
1371 | tr = (b * ratio) + a; | |
1372 | ti = b - (a * ratio); | |
1373 | tr = tr / div; | |
1374 | ti = ti / div; */ | |
43a5d30b | 1375 | tree ratio = const_binop (code, i2, r2); |
08d19889 | 1376 | tree div = const_binop (PLUS_EXPR, r2, |
43a5d30b | 1377 | const_binop (MULT_EXPR, i2, ratio)); |
08d19889 | 1378 | |
43a5d30b AS |
1379 | real = const_binop (MULT_EXPR, i1, ratio); |
1380 | real = const_binop (PLUS_EXPR, real, r1); | |
1381 | real = const_binop (code, real, div); | |
08d19889 | 1382 | |
43a5d30b AS |
1383 | imag = const_binop (MULT_EXPR, r1, ratio); |
1384 | imag = const_binop (MINUS_EXPR, i1, imag); | |
1385 | imag = const_binop (code, imag, div); | |
e3d5405d KG |
1386 | } |
1387 | } | |
6d716ca8 RS |
1388 | break; |
1389 | ||
1390 | default: | |
fd6c76f4 | 1391 | return NULL_TREE; |
6d716ca8 | 1392 | } |
858214db EB |
1393 | |
1394 | if (real && imag) | |
1395 | return build_complex (type, real, imag); | |
6d716ca8 | 1396 | } |
858214db | 1397 | |
d2a12ae7 RG |
1398 | if (TREE_CODE (arg1) == VECTOR_CST |
1399 | && TREE_CODE (arg2) == VECTOR_CST) | |
d1d1c602 | 1400 | { |
bb506982 | 1401 | tree type = TREE_TYPE (arg1); |
d1d1c602 | 1402 | int count = TYPE_VECTOR_SUBPARTS (type), i; |
bb506982 | 1403 | tree *elts = XALLOCAVEC (tree, count); |
d1d1c602 BM |
1404 | |
1405 | for (i = 0; i < count; i++) | |
1406 | { | |
bb506982 | 1407 | tree elem1 = VECTOR_CST_ELT (arg1, i); |
d2a12ae7 | 1408 | tree elem2 = VECTOR_CST_ELT (arg2, i); |
b8698a0f | 1409 | |
bb506982 MG |
1410 | elts[i] = const_binop (code, elem1, elem2); |
1411 | ||
1412 | /* It is possible that const_binop cannot handle the given | |
1413 | code and return NULL_TREE */ | |
1414 | if (elts[i] == NULL_TREE) | |
1415 | return NULL_TREE; | |
1416 | } | |
1417 | ||
1418 | return build_vector (type, elts); | |
1419 | } | |
1420 | ||
1421 | /* Shifts allow a scalar offset for a vector. */ | |
1422 | if (TREE_CODE (arg1) == VECTOR_CST | |
1423 | && TREE_CODE (arg2) == INTEGER_CST) | |
1424 | { | |
1425 | tree type = TREE_TYPE (arg1); | |
1426 | int count = TYPE_VECTOR_SUBPARTS (type), i; | |
1427 | tree *elts = XALLOCAVEC (tree, count); | |
1428 | ||
41e10689 JJ |
1429 | if (code == VEC_LSHIFT_EXPR |
1430 | || code == VEC_RSHIFT_EXPR) | |
bb506982 | 1431 | { |
41e10689 JJ |
1432 | if (!host_integerp (arg2, 1)) |
1433 | return NULL_TREE; | |
bb506982 | 1434 | |
41e10689 JJ |
1435 | unsigned HOST_WIDE_INT shiftc = tree_low_cst (arg2, 1); |
1436 | unsigned HOST_WIDE_INT outerc = tree_low_cst (TYPE_SIZE (type), 1); | |
1437 | unsigned HOST_WIDE_INT innerc | |
1438 | = tree_low_cst (TYPE_SIZE (TREE_TYPE (type)), 1); | |
1439 | if (shiftc >= outerc || (shiftc % innerc) != 0) | |
bb506982 | 1440 | return NULL_TREE; |
41e10689 | 1441 | int offset = shiftc / innerc; |
52d84413 JJ |
1442 | /* The direction of VEC_[LR]SHIFT_EXPR is endian dependent. |
1443 | For reductions, compiler emits VEC_RSHIFT_EXPR always, | |
1444 | for !BYTES_BIG_ENDIAN picks first vector element, but | |
1445 | for BYTES_BIG_ENDIAN last element from the vector. */ | |
1446 | if ((code == VEC_RSHIFT_EXPR) ^ (!BYTES_BIG_ENDIAN)) | |
41e10689 JJ |
1447 | offset = -offset; |
1448 | tree zero = build_zero_cst (TREE_TYPE (type)); | |
1449 | for (i = 0; i < count; i++) | |
1450 | { | |
1451 | if (i + offset < 0 || i + offset >= count) | |
1452 | elts[i] = zero; | |
1453 | else | |
1454 | elts[i] = VECTOR_CST_ELT (arg1, i + offset); | |
1455 | } | |
d1d1c602 | 1456 | } |
41e10689 JJ |
1457 | else |
1458 | for (i = 0; i < count; i++) | |
1459 | { | |
1460 | tree elem1 = VECTOR_CST_ELT (arg1, i); | |
1461 | ||
1462 | elts[i] = const_binop (code, elem1, arg2); | |
1463 | ||
1464 | /* It is possible that const_binop cannot handle the given | |
1465 | code and return NULL_TREE */ | |
1466 | if (elts[i] == NULL_TREE) | |
1467 | return NULL_TREE; | |
1468 | } | |
d2a12ae7 RG |
1469 | |
1470 | return build_vector (type, elts); | |
d1d1c602 | 1471 | } |
fd6c76f4 | 1472 | return NULL_TREE; |
6d716ca8 | 1473 | } |
4c160717 | 1474 | |
67ae67ec | 1475 | /* Create a sizetype INT_CST node with NUMBER sign extended. KIND |
ce552f75 | 1476 | indicates which particular sizetype to create. */ |
d4b60170 | 1477 | |
fed3cef0 | 1478 | tree |
3e95a7cb | 1479 | size_int_kind (HOST_WIDE_INT number, enum size_type_kind kind) |
fed3cef0 | 1480 | { |
ce552f75 | 1481 | return build_int_cst (sizetype_tab[(int) kind], number); |
fed3cef0 | 1482 | } |
ce552f75 | 1483 | \f |
fed3cef0 RK |
1484 | /* Combine operands OP1 and OP2 with arithmetic operation CODE. CODE |
1485 | is a tree code. The type of the result is taken from the operands. | |
000d8d44 | 1486 | Both must be equivalent integer types, ala int_binop_types_match_p. |
6d716ca8 RS |
1487 | If the operands are constant, so is the result. */ |
1488 | ||
1489 | tree | |
db3927fb | 1490 | size_binop_loc (location_t loc, enum tree_code code, tree arg0, tree arg1) |
6d716ca8 | 1491 | { |
fed3cef0 RK |
1492 | tree type = TREE_TYPE (arg0); |
1493 | ||
7ebcc52c VR |
1494 | if (arg0 == error_mark_node || arg1 == error_mark_node) |
1495 | return error_mark_node; | |
1496 | ||
000d8d44 RS |
1497 | gcc_assert (int_binop_types_match_p (code, TREE_TYPE (arg0), |
1498 | TREE_TYPE (arg1))); | |
fed3cef0 | 1499 | |
6d716ca8 RS |
1500 | /* Handle the special case of two integer constants faster. */ |
1501 | if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST) | |
1502 | { | |
1503 | /* And some specific cases even faster than that. */ | |
74890d7b RS |
1504 | if (code == PLUS_EXPR) |
1505 | { | |
1506 | if (integer_zerop (arg0) && !TREE_OVERFLOW (arg0)) | |
1507 | return arg1; | |
1508 | if (integer_zerop (arg1) && !TREE_OVERFLOW (arg1)) | |
1509 | return arg0; | |
1510 | } | |
1511 | else if (code == MINUS_EXPR) | |
1512 | { | |
1513 | if (integer_zerop (arg1) && !TREE_OVERFLOW (arg1)) | |
1514 | return arg0; | |
1515 | } | |
1516 | else if (code == MULT_EXPR) | |
1517 | { | |
1518 | if (integer_onep (arg0) && !TREE_OVERFLOW (arg0)) | |
1519 | return arg1; | |
1520 | } | |
9898deac | 1521 | |
56099f00 RG |
1522 | /* Handle general case of two integer constants. For sizetype |
1523 | constant calculations we always want to know about overflow, | |
1524 | even in the unsigned case. */ | |
1525 | return int_const_binop_1 (code, arg0, arg1, -1); | |
6d716ca8 RS |
1526 | } |
1527 | ||
db3927fb | 1528 | return fold_build2_loc (loc, code, type, arg0, arg1); |
6d716ca8 | 1529 | } |
697073d9 | 1530 | |
fed3cef0 RK |
1531 | /* Given two values, either both of sizetype or both of bitsizetype, |
1532 | compute the difference between the two values. Return the value | |
1533 | in signed type corresponding to the type of the operands. */ | |
697073d9 JM |
1534 | |
1535 | tree | |
db3927fb | 1536 | size_diffop_loc (location_t loc, tree arg0, tree arg1) |
697073d9 | 1537 | { |
fed3cef0 RK |
1538 | tree type = TREE_TYPE (arg0); |
1539 | tree ctype; | |
697073d9 | 1540 | |
000d8d44 RS |
1541 | gcc_assert (int_binop_types_match_p (MINUS_EXPR, TREE_TYPE (arg0), |
1542 | TREE_TYPE (arg1))); | |
697073d9 | 1543 | |
fed3cef0 | 1544 | /* If the type is already signed, just do the simple thing. */ |
8df83eae | 1545 | if (!TYPE_UNSIGNED (type)) |
db3927fb | 1546 | return size_binop_loc (loc, MINUS_EXPR, arg0, arg1); |
fed3cef0 | 1547 | |
000d8d44 RS |
1548 | if (type == sizetype) |
1549 | ctype = ssizetype; | |
1550 | else if (type == bitsizetype) | |
1551 | ctype = sbitsizetype; | |
1552 | else | |
12753674 | 1553 | ctype = signed_type_for (type); |
fed3cef0 RK |
1554 | |
1555 | /* If either operand is not a constant, do the conversions to the signed | |
1556 | type and subtract. The hardware will do the right thing with any | |
1557 | overflow in the subtraction. */ | |
1558 | if (TREE_CODE (arg0) != INTEGER_CST || TREE_CODE (arg1) != INTEGER_CST) | |
db3927fb AH |
1559 | return size_binop_loc (loc, MINUS_EXPR, |
1560 | fold_convert_loc (loc, ctype, arg0), | |
1561 | fold_convert_loc (loc, ctype, arg1)); | |
fed3cef0 RK |
1562 | |
1563 | /* If ARG0 is larger than ARG1, subtract and return the result in CTYPE. | |
1564 | Otherwise, subtract the other way, convert to CTYPE (we know that can't | |
1565 | overflow) and negate (which can't either). Special-case a result | |
1566 | of zero while we're here. */ | |
1567 | if (tree_int_cst_equal (arg0, arg1)) | |
57decb7e | 1568 | return build_int_cst (ctype, 0); |
fed3cef0 | 1569 | else if (tree_int_cst_lt (arg1, arg0)) |
db3927fb AH |
1570 | return fold_convert_loc (loc, ctype, |
1571 | size_binop_loc (loc, MINUS_EXPR, arg0, arg1)); | |
fed3cef0 | 1572 | else |
db3927fb AH |
1573 | return size_binop_loc (loc, MINUS_EXPR, build_int_cst (ctype, 0), |
1574 | fold_convert_loc (loc, ctype, | |
1575 | size_binop_loc (loc, | |
1576 | MINUS_EXPR, | |
1577 | arg1, arg0))); | |
697073d9 | 1578 | } |
6d716ca8 | 1579 | \f |
c756af79 RH |
1580 | /* A subroutine of fold_convert_const handling conversions of an |
1581 | INTEGER_CST to another integer type. */ | |
049e524f RS |
1582 | |
1583 | static tree | |
ac545c64 | 1584 | fold_convert_const_int_from_int (tree type, const_tree arg1) |
049e524f | 1585 | { |
c756af79 | 1586 | tree t; |
049e524f | 1587 | |
c756af79 RH |
1588 | /* Given an integer constant, make new constant with new type, |
1589 | appropriately sign-extended or truncated. */ | |
9589f23e | 1590 | t = force_fit_type_double (type, tree_to_double_int (arg1), |
9e9ef331 | 1591 | !POINTER_TYPE_P (TREE_TYPE (arg1)), |
b8fca551 RG |
1592 | (TREE_INT_CST_HIGH (arg1) < 0 |
1593 | && (TYPE_UNSIGNED (type) | |
1594 | < TYPE_UNSIGNED (TREE_TYPE (arg1)))) | |
d95787e6 | 1595 | | TREE_OVERFLOW (arg1)); |
049e524f | 1596 | |
c756af79 | 1597 | return t; |
049e524f RS |
1598 | } |
1599 | ||
c756af79 RH |
1600 | /* A subroutine of fold_convert_const handling conversions a REAL_CST |
1601 | to an integer type. */ | |
6d716ca8 RS |
1602 | |
1603 | static tree | |
ac545c64 | 1604 | fold_convert_const_int_from_real (enum tree_code code, tree type, const_tree arg1) |
6d716ca8 | 1605 | { |
649ff3b4 | 1606 | int overflow = 0; |
fdb33708 RS |
1607 | tree t; |
1608 | ||
c756af79 RH |
1609 | /* The following code implements the floating point to integer |
1610 | conversion rules required by the Java Language Specification, | |
1611 | that IEEE NaNs are mapped to zero and values that overflow | |
1612 | the target precision saturate, i.e. values greater than | |
1613 | INT_MAX are mapped to INT_MAX, and values less than INT_MIN | |
1614 | are mapped to INT_MIN. These semantics are allowed by the | |
1615 | C and C++ standards that simply state that the behavior of | |
1616 | FP-to-integer conversion is unspecified upon overflow. */ | |
6d716ca8 | 1617 | |
2bd1333d | 1618 | double_int val; |
c756af79 RH |
1619 | REAL_VALUE_TYPE r; |
1620 | REAL_VALUE_TYPE x = TREE_REAL_CST (arg1); | |
1621 | ||
1622 | switch (code) | |
6d716ca8 | 1623 | { |
c756af79 RH |
1624 | case FIX_TRUNC_EXPR: |
1625 | real_trunc (&r, VOIDmode, &x); | |
1626 | break; | |
1627 | ||
c756af79 RH |
1628 | default: |
1629 | gcc_unreachable (); | |
1630 | } | |
1631 | ||
1632 | /* If R is NaN, return zero and show we have an overflow. */ | |
1633 | if (REAL_VALUE_ISNAN (r)) | |
1634 | { | |
1635 | overflow = 1; | |
2bd1333d | 1636 | val = double_int_zero; |
c756af79 RH |
1637 | } |
1638 | ||
1639 | /* See if R is less than the lower bound or greater than the | |
1640 | upper bound. */ | |
1641 | ||
1642 | if (! overflow) | |
1643 | { | |
1644 | tree lt = TYPE_MIN_VALUE (type); | |
1645 | REAL_VALUE_TYPE l = real_value_from_int_cst (NULL_TREE, lt); | |
1646 | if (REAL_VALUES_LESS (r, l)) | |
6d716ca8 | 1647 | { |
c756af79 | 1648 | overflow = 1; |
2bd1333d | 1649 | val = tree_to_double_int (lt); |
6d716ca8 | 1650 | } |
c756af79 RH |
1651 | } |
1652 | ||
1653 | if (! overflow) | |
1654 | { | |
1655 | tree ut = TYPE_MAX_VALUE (type); | |
1656 | if (ut) | |
6d716ca8 | 1657 | { |
c756af79 RH |
1658 | REAL_VALUE_TYPE u = real_value_from_int_cst (NULL_TREE, ut); |
1659 | if (REAL_VALUES_LESS (u, r)) | |
fdb33708 | 1660 | { |
c756af79 | 1661 | overflow = 1; |
2bd1333d | 1662 | val = tree_to_double_int (ut); |
c756af79 RH |
1663 | } |
1664 | } | |
1665 | } | |
fdb33708 | 1666 | |
c756af79 | 1667 | if (! overflow) |
2bd1333d | 1668 | real_to_integer2 ((HOST_WIDE_INT *) &val.low, &val.high, &r); |
fdb33708 | 1669 | |
9589f23e | 1670 | t = force_fit_type_double (type, val, -1, overflow | TREE_OVERFLOW (arg1)); |
c756af79 RH |
1671 | return t; |
1672 | } | |
fc627530 | 1673 | |
325217ed CF |
1674 | /* A subroutine of fold_convert_const handling conversions of a |
1675 | FIXED_CST to an integer type. */ | |
1676 | ||
1677 | static tree | |
ac545c64 | 1678 | fold_convert_const_int_from_fixed (tree type, const_tree arg1) |
325217ed CF |
1679 | { |
1680 | tree t; | |
1681 | double_int temp, temp_trunc; | |
1682 | unsigned int mode; | |
1683 | ||
1684 | /* Right shift FIXED_CST to temp by fbit. */ | |
1685 | temp = TREE_FIXED_CST (arg1).data; | |
1686 | mode = TREE_FIXED_CST (arg1).mode; | |
49ab6098 | 1687 | if (GET_MODE_FBIT (mode) < HOST_BITS_PER_DOUBLE_INT) |
325217ed | 1688 | { |
27bcd47c LC |
1689 | temp = temp.rshift (GET_MODE_FBIT (mode), |
1690 | HOST_BITS_PER_DOUBLE_INT, | |
1691 | SIGNED_FIXED_POINT_MODE_P (mode)); | |
325217ed CF |
1692 | |
1693 | /* Left shift temp to temp_trunc by fbit. */ | |
27bcd47c LC |
1694 | temp_trunc = temp.lshift (GET_MODE_FBIT (mode), |
1695 | HOST_BITS_PER_DOUBLE_INT, | |
1696 | SIGNED_FIXED_POINT_MODE_P (mode)); | |
325217ed CF |
1697 | } |
1698 | else | |
1699 | { | |
2bd1333d AS |
1700 | temp = double_int_zero; |
1701 | temp_trunc = double_int_zero; | |
325217ed CF |
1702 | } |
1703 | ||
1704 | /* If FIXED_CST is negative, we need to round the value toward 0. | |
1705 | By checking if the fractional bits are not zero to add 1 to temp. */ | |
2bd1333d | 1706 | if (SIGNED_FIXED_POINT_MODE_P (mode) |
27bcd47c LC |
1707 | && temp_trunc.is_negative () |
1708 | && TREE_FIXED_CST (arg1).data != temp_trunc) | |
1709 | temp += double_int_one; | |
325217ed CF |
1710 | |
1711 | /* Given a fixed-point constant, make new constant with new type, | |
1712 | appropriately sign-extended or truncated. */ | |
9589f23e | 1713 | t = force_fit_type_double (type, temp, -1, |
27bcd47c | 1714 | (temp.is_negative () |
325217ed CF |
1715 | && (TYPE_UNSIGNED (type) |
1716 | < TYPE_UNSIGNED (TREE_TYPE (arg1)))) | |
1717 | | TREE_OVERFLOW (arg1)); | |
1718 | ||
1719 | return t; | |
1720 | } | |
1721 | ||
c756af79 RH |
1722 | /* A subroutine of fold_convert_const handling conversions a REAL_CST |
1723 | to another floating point type. */ | |
fdb33708 | 1724 | |
c756af79 | 1725 | static tree |
ac545c64 | 1726 | fold_convert_const_real_from_real (tree type, const_tree arg1) |
c756af79 | 1727 | { |
d284eb28 | 1728 | REAL_VALUE_TYPE value; |
c756af79 | 1729 | tree t; |
e1ee5cdc | 1730 | |
d284eb28 RS |
1731 | real_convert (&value, TYPE_MODE (type), &TREE_REAL_CST (arg1)); |
1732 | t = build_real (type, value); | |
875eda9c | 1733 | |
d33e4b70 SL |
1734 | /* If converting an infinity or NAN to a representation that doesn't |
1735 | have one, set the overflow bit so that we can produce some kind of | |
1736 | error message at the appropriate point if necessary. It's not the | |
1737 | most user-friendly message, but it's better than nothing. */ | |
1738 | if (REAL_VALUE_ISINF (TREE_REAL_CST (arg1)) | |
1739 | && !MODE_HAS_INFINITIES (TYPE_MODE (type))) | |
1740 | TREE_OVERFLOW (t) = 1; | |
1741 | else if (REAL_VALUE_ISNAN (TREE_REAL_CST (arg1)) | |
1742 | && !MODE_HAS_NANS (TYPE_MODE (type))) | |
1743 | TREE_OVERFLOW (t) = 1; | |
1744 | /* Regular overflow, conversion produced an infinity in a mode that | |
1745 | can't represent them. */ | |
1746 | else if (!MODE_HAS_INFINITIES (TYPE_MODE (type)) | |
1747 | && REAL_VALUE_ISINF (value) | |
1748 | && !REAL_VALUE_ISINF (TREE_REAL_CST (arg1))) | |
1749 | TREE_OVERFLOW (t) = 1; | |
1750 | else | |
1751 | TREE_OVERFLOW (t) = TREE_OVERFLOW (arg1); | |
c756af79 RH |
1752 | return t; |
1753 | } | |
875eda9c | 1754 | |
325217ed CF |
1755 | /* A subroutine of fold_convert_const handling conversions a FIXED_CST |
1756 | to a floating point type. */ | |
1757 | ||
1758 | static tree | |
ac545c64 | 1759 | fold_convert_const_real_from_fixed (tree type, const_tree arg1) |
325217ed CF |
1760 | { |
1761 | REAL_VALUE_TYPE value; | |
1762 | tree t; | |
1763 | ||
1764 | real_convert_from_fixed (&value, TYPE_MODE (type), &TREE_FIXED_CST (arg1)); | |
1765 | t = build_real (type, value); | |
1766 | ||
1767 | TREE_OVERFLOW (t) = TREE_OVERFLOW (arg1); | |
325217ed CF |
1768 | return t; |
1769 | } | |
1770 | ||
1771 | /* A subroutine of fold_convert_const handling conversions a FIXED_CST | |
1772 | to another fixed-point type. */ | |
1773 | ||
1774 | static tree | |
ac545c64 | 1775 | fold_convert_const_fixed_from_fixed (tree type, const_tree arg1) |
325217ed CF |
1776 | { |
1777 | FIXED_VALUE_TYPE value; | |
1778 | tree t; | |
1779 | bool overflow_p; | |
1780 | ||
1781 | overflow_p = fixed_convert (&value, TYPE_MODE (type), &TREE_FIXED_CST (arg1), | |
1782 | TYPE_SATURATING (type)); | |
1783 | t = build_fixed (type, value); | |
1784 | ||
1785 | /* Propagate overflow flags. */ | |
1786 | if (overflow_p | TREE_OVERFLOW (arg1)) | |
28ddeea1 | 1787 | TREE_OVERFLOW (t) = 1; |
325217ed CF |
1788 | return t; |
1789 | } | |
1790 | ||
1791 | /* A subroutine of fold_convert_const handling conversions an INTEGER_CST | |
1792 | to a fixed-point type. */ | |
1793 | ||
1794 | static tree | |
ac545c64 | 1795 | fold_convert_const_fixed_from_int (tree type, const_tree arg1) |
325217ed CF |
1796 | { |
1797 | FIXED_VALUE_TYPE value; | |
1798 | tree t; | |
1799 | bool overflow_p; | |
1800 | ||
1801 | overflow_p = fixed_convert_from_int (&value, TYPE_MODE (type), | |
1802 | TREE_INT_CST (arg1), | |
1803 | TYPE_UNSIGNED (TREE_TYPE (arg1)), | |
1804 | TYPE_SATURATING (type)); | |
1805 | t = build_fixed (type, value); | |
1806 | ||
1807 | /* Propagate overflow flags. */ | |
1808 | if (overflow_p | TREE_OVERFLOW (arg1)) | |
28ddeea1 | 1809 | TREE_OVERFLOW (t) = 1; |
325217ed CF |
1810 | return t; |
1811 | } | |
1812 | ||
1813 | /* A subroutine of fold_convert_const handling conversions a REAL_CST | |
1814 | to a fixed-point type. */ | |
1815 | ||
1816 | static tree | |
ac545c64 | 1817 | fold_convert_const_fixed_from_real (tree type, const_tree arg1) |
325217ed CF |
1818 | { |
1819 | FIXED_VALUE_TYPE value; | |
1820 | tree t; | |
1821 | bool overflow_p; | |
1822 | ||
1823 | overflow_p = fixed_convert_from_real (&value, TYPE_MODE (type), | |
1824 | &TREE_REAL_CST (arg1), | |
1825 | TYPE_SATURATING (type)); | |
1826 | t = build_fixed (type, value); | |
1827 | ||
1828 | /* Propagate overflow flags. */ | |
1829 | if (overflow_p | TREE_OVERFLOW (arg1)) | |
28ddeea1 | 1830 | TREE_OVERFLOW (t) = 1; |
325217ed CF |
1831 | return t; |
1832 | } | |
1833 | ||
c756af79 RH |
1834 | /* Attempt to fold type conversion operation CODE of expression ARG1 to |
1835 | type TYPE. If no simplification can be done return NULL_TREE. */ | |
875eda9c | 1836 | |
c756af79 RH |
1837 | static tree |
1838 | fold_convert_const (enum tree_code code, tree type, tree arg1) | |
1839 | { | |
1840 | if (TREE_TYPE (arg1) == type) | |
1841 | return arg1; | |
ca7a3bd7 | 1842 | |
0e4b00d6 AP |
1843 | if (POINTER_TYPE_P (type) || INTEGRAL_TYPE_P (type) |
1844 | || TREE_CODE (type) == OFFSET_TYPE) | |
c756af79 RH |
1845 | { |
1846 | if (TREE_CODE (arg1) == INTEGER_CST) | |
1847 | return fold_convert_const_int_from_int (type, arg1); | |
1848 | else if (TREE_CODE (arg1) == REAL_CST) | |
1849 | return fold_convert_const_int_from_real (code, type, arg1); | |
325217ed CF |
1850 | else if (TREE_CODE (arg1) == FIXED_CST) |
1851 | return fold_convert_const_int_from_fixed (type, arg1); | |
6d716ca8 RS |
1852 | } |
1853 | else if (TREE_CODE (type) == REAL_TYPE) | |
1854 | { | |
6d716ca8 RS |
1855 | if (TREE_CODE (arg1) == INTEGER_CST) |
1856 | return build_real_from_int_cst (type, arg1); | |
325217ed | 1857 | else if (TREE_CODE (arg1) == REAL_CST) |
c756af79 | 1858 | return fold_convert_const_real_from_real (type, arg1); |
325217ed CF |
1859 | else if (TREE_CODE (arg1) == FIXED_CST) |
1860 | return fold_convert_const_real_from_fixed (type, arg1); | |
1861 | } | |
1862 | else if (TREE_CODE (type) == FIXED_POINT_TYPE) | |
1863 | { | |
1864 | if (TREE_CODE (arg1) == FIXED_CST) | |
1865 | return fold_convert_const_fixed_from_fixed (type, arg1); | |
1866 | else if (TREE_CODE (arg1) == INTEGER_CST) | |
1867 | return fold_convert_const_fixed_from_int (type, arg1); | |
1868 | else if (TREE_CODE (arg1) == REAL_CST) | |
1869 | return fold_convert_const_fixed_from_real (type, arg1); | |
6d716ca8 | 1870 | } |
fdb33708 | 1871 | return NULL_TREE; |
6d716ca8 | 1872 | } |
088414c1 | 1873 | |
c756af79 RH |
1874 | /* Construct a vector of zero elements of vector type TYPE. */ |
1875 | ||
1876 | static tree | |
1877 | build_zero_vector (tree type) | |
1878 | { | |
b9acc9f1 | 1879 | tree t; |
b8698a0f | 1880 | |
b9acc9f1 NF |
1881 | t = fold_convert_const (NOP_EXPR, TREE_TYPE (type), integer_zero_node); |
1882 | return build_vector_from_val (type, t); | |
c756af79 RH |
1883 | } |
1884 | ||
3b357646 RG |
1885 | /* Returns true, if ARG is convertible to TYPE using a NOP_EXPR. */ |
1886 | ||
1887 | bool | |
fa233e34 | 1888 | fold_convertible_p (const_tree type, const_tree arg) |
3b357646 RG |
1889 | { |
1890 | tree orig = TREE_TYPE (arg); | |
1891 | ||
1892 | if (type == orig) | |
1893 | return true; | |
1894 | ||
1895 | if (TREE_CODE (arg) == ERROR_MARK | |
1896 | || TREE_CODE (type) == ERROR_MARK | |
1897 | || TREE_CODE (orig) == ERROR_MARK) | |
1898 | return false; | |
1899 | ||
1900 | if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (orig)) | |
1901 | return true; | |
1902 | ||
1903 | switch (TREE_CODE (type)) | |
1904 | { | |
1905 | case INTEGER_TYPE: case ENUMERAL_TYPE: case BOOLEAN_TYPE: | |
1906 | case POINTER_TYPE: case REFERENCE_TYPE: | |
1907 | case OFFSET_TYPE: | |
1908 | if (INTEGRAL_TYPE_P (orig) || POINTER_TYPE_P (orig) | |
1909 | || TREE_CODE (orig) == OFFSET_TYPE) | |
1910 | return true; | |
1911 | return (TREE_CODE (orig) == VECTOR_TYPE | |
1912 | && tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (orig))); | |
1913 | ||
c17ee676 FXC |
1914 | case REAL_TYPE: |
1915 | case FIXED_POINT_TYPE: | |
1916 | case COMPLEX_TYPE: | |
1917 | case VECTOR_TYPE: | |
1918 | case VOID_TYPE: | |
3b357646 | 1919 | return TREE_CODE (type) == TREE_CODE (orig); |
c17ee676 FXC |
1920 | |
1921 | default: | |
1922 | return false; | |
3b357646 RG |
1923 | } |
1924 | } | |
1925 | ||
088414c1 RS |
1926 | /* Convert expression ARG to type TYPE. Used by the middle-end for |
1927 | simple conversions in preference to calling the front-end's convert. */ | |
1928 | ||
e419fe91 | 1929 | tree |
db3927fb | 1930 | fold_convert_loc (location_t loc, tree type, tree arg) |
088414c1 RS |
1931 | { |
1932 | tree orig = TREE_TYPE (arg); | |
1933 | tree tem; | |
1934 | ||
1935 | if (type == orig) | |
1936 | return arg; | |
1937 | ||
1938 | if (TREE_CODE (arg) == ERROR_MARK | |
1939 | || TREE_CODE (type) == ERROR_MARK | |
1940 | || TREE_CODE (orig) == ERROR_MARK) | |
1941 | return error_mark_node; | |
1942 | ||
0bccc606 | 1943 | switch (TREE_CODE (type)) |
088414c1 | 1944 | { |
09e881c9 BE |
1945 | case POINTER_TYPE: |
1946 | case REFERENCE_TYPE: | |
1947 | /* Handle conversions between pointers to different address spaces. */ | |
1948 | if (POINTER_TYPE_P (orig) | |
1949 | && (TYPE_ADDR_SPACE (TREE_TYPE (type)) | |
1950 | != TYPE_ADDR_SPACE (TREE_TYPE (orig)))) | |
1951 | return fold_build1_loc (loc, ADDR_SPACE_CONVERT_EXPR, type, arg); | |
1952 | /* fall through */ | |
1953 | ||
71d59383 | 1954 | case INTEGER_TYPE: case ENUMERAL_TYPE: case BOOLEAN_TYPE: |
0bccc606 | 1955 | case OFFSET_TYPE: |
088414c1 RS |
1956 | if (TREE_CODE (arg) == INTEGER_CST) |
1957 | { | |
1958 | tem = fold_convert_const (NOP_EXPR, type, arg); | |
1959 | if (tem != NULL_TREE) | |
1960 | return tem; | |
1961 | } | |
908d0773 AP |
1962 | if (INTEGRAL_TYPE_P (orig) || POINTER_TYPE_P (orig) |
1963 | || TREE_CODE (orig) == OFFSET_TYPE) | |
db3927fb | 1964 | return fold_build1_loc (loc, NOP_EXPR, type, arg); |
088414c1 | 1965 | if (TREE_CODE (orig) == COMPLEX_TYPE) |
db3927fb AH |
1966 | return fold_convert_loc (loc, type, |
1967 | fold_build1_loc (loc, REALPART_EXPR, | |
1968 | TREE_TYPE (orig), arg)); | |
0bccc606 NS |
1969 | gcc_assert (TREE_CODE (orig) == VECTOR_TYPE |
1970 | && tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (orig))); | |
db3927fb | 1971 | return fold_build1_loc (loc, NOP_EXPR, type, arg); |
3e6688a7 | 1972 | |
0bccc606 | 1973 | case REAL_TYPE: |
088414c1 RS |
1974 | if (TREE_CODE (arg) == INTEGER_CST) |
1975 | { | |
1976 | tem = fold_convert_const (FLOAT_EXPR, type, arg); | |
1977 | if (tem != NULL_TREE) | |
1978 | return tem; | |
1979 | } | |
1980 | else if (TREE_CODE (arg) == REAL_CST) | |
1981 | { | |
1982 | tem = fold_convert_const (NOP_EXPR, type, arg); | |
1983 | if (tem != NULL_TREE) | |
1984 | return tem; | |
1985 | } | |
325217ed CF |
1986 | else if (TREE_CODE (arg) == FIXED_CST) |
1987 | { | |
1988 | tem = fold_convert_const (FIXED_CONVERT_EXPR, type, arg); | |
1989 | if (tem != NULL_TREE) | |
1990 | return tem; | |
1991 | } | |
088414c1 | 1992 | |
0bccc606 | 1993 | switch (TREE_CODE (orig)) |
088414c1 | 1994 | { |
71d59383 | 1995 | case INTEGER_TYPE: |
0bccc606 NS |
1996 | case BOOLEAN_TYPE: case ENUMERAL_TYPE: |
1997 | case POINTER_TYPE: case REFERENCE_TYPE: | |
db3927fb | 1998 | return fold_build1_loc (loc, FLOAT_EXPR, type, arg); |
3e6688a7 | 1999 | |
0bccc606 | 2000 | case REAL_TYPE: |
db3927fb | 2001 | return fold_build1_loc (loc, NOP_EXPR, type, arg); |
3e6688a7 | 2002 | |
325217ed | 2003 | case FIXED_POINT_TYPE: |
db3927fb | 2004 | return fold_build1_loc (loc, FIXED_CONVERT_EXPR, type, arg); |
325217ed CF |
2005 | |
2006 | case COMPLEX_TYPE: | |
db3927fb AH |
2007 | tem = fold_build1_loc (loc, REALPART_EXPR, TREE_TYPE (orig), arg); |
2008 | return fold_convert_loc (loc, type, tem); | |
325217ed CF |
2009 | |
2010 | default: | |
2011 | gcc_unreachable (); | |
2012 | } | |
2013 | ||
2014 | case FIXED_POINT_TYPE: | |
2015 | if (TREE_CODE (arg) == FIXED_CST || TREE_CODE (arg) == INTEGER_CST | |
2016 | || TREE_CODE (arg) == REAL_CST) | |
2017 | { | |
2018 | tem = fold_convert_const (FIXED_CONVERT_EXPR, type, arg); | |
2019 | if (tem != NULL_TREE) | |
db3927fb | 2020 | goto fold_convert_exit; |
325217ed CF |
2021 | } |
2022 | ||
2023 | switch (TREE_CODE (orig)) | |
2024 | { | |
2025 | case FIXED_POINT_TYPE: | |
2026 | case INTEGER_TYPE: | |
2027 | case ENUMERAL_TYPE: | |
2028 | case BOOLEAN_TYPE: | |
2029 | case REAL_TYPE: | |
db3927fb | 2030 | return fold_build1_loc (loc, FIXED_CONVERT_EXPR, type, arg); |
325217ed | 2031 | |
0bccc606 | 2032 | case COMPLEX_TYPE: |
db3927fb AH |
2033 | tem = fold_build1_loc (loc, REALPART_EXPR, TREE_TYPE (orig), arg); |
2034 | return fold_convert_loc (loc, type, tem); | |
3e6688a7 | 2035 | |
0bccc606 NS |
2036 | default: |
2037 | gcc_unreachable (); | |
088414c1 | 2038 | } |
3e6688a7 | 2039 | |
0bccc606 NS |
2040 | case COMPLEX_TYPE: |
2041 | switch (TREE_CODE (orig)) | |
2042 | { | |
71d59383 | 2043 | case INTEGER_TYPE: |
0bccc606 NS |
2044 | case BOOLEAN_TYPE: case ENUMERAL_TYPE: |
2045 | case POINTER_TYPE: case REFERENCE_TYPE: | |
2046 | case REAL_TYPE: | |
325217ed | 2047 | case FIXED_POINT_TYPE: |
db3927fb AH |
2048 | return fold_build2_loc (loc, COMPLEX_EXPR, type, |
2049 | fold_convert_loc (loc, TREE_TYPE (type), arg), | |
2050 | fold_convert_loc (loc, TREE_TYPE (type), | |
3111cce0 | 2051 | integer_zero_node)); |
0bccc606 NS |
2052 | case COMPLEX_TYPE: |
2053 | { | |
2054 | tree rpart, ipart; | |
3e6688a7 | 2055 | |
0bccc606 NS |
2056 | if (TREE_CODE (arg) == COMPLEX_EXPR) |
2057 | { | |
db3927fb AH |
2058 | rpart = fold_convert_loc (loc, TREE_TYPE (type), |
2059 | TREE_OPERAND (arg, 0)); | |
2060 | ipart = fold_convert_loc (loc, TREE_TYPE (type), | |
2061 | TREE_OPERAND (arg, 1)); | |
2062 | return fold_build2_loc (loc, COMPLEX_EXPR, type, rpart, ipart); | |
0bccc606 | 2063 | } |
3e6688a7 | 2064 | |
0bccc606 | 2065 | arg = save_expr (arg); |
db3927fb AH |
2066 | rpart = fold_build1_loc (loc, REALPART_EXPR, TREE_TYPE (orig), arg); |
2067 | ipart = fold_build1_loc (loc, IMAGPART_EXPR, TREE_TYPE (orig), arg); | |
2068 | rpart = fold_convert_loc (loc, TREE_TYPE (type), rpart); | |
2069 | ipart = fold_convert_loc (loc, TREE_TYPE (type), ipart); | |
2070 | return fold_build2_loc (loc, COMPLEX_EXPR, type, rpart, ipart); | |
0bccc606 | 2071 | } |
3e6688a7 | 2072 | |
0bccc606 NS |
2073 | default: |
2074 | gcc_unreachable (); | |
2075 | } | |
3e6688a7 | 2076 | |
0bccc606 | 2077 | case VECTOR_TYPE: |
049e524f RS |
2078 | if (integer_zerop (arg)) |
2079 | return build_zero_vector (type); | |
0bccc606 NS |
2080 | gcc_assert (tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (orig))); |
2081 | gcc_assert (INTEGRAL_TYPE_P (orig) || POINTER_TYPE_P (orig) | |
2082 | || TREE_CODE (orig) == VECTOR_TYPE); | |
db3927fb | 2083 | return fold_build1_loc (loc, VIEW_CONVERT_EXPR, type, arg); |
088414c1 | 2084 | |
0bccc606 | 2085 | case VOID_TYPE: |
bd7e4636 | 2086 | tem = fold_ignored_result (arg); |
db3927fb | 2087 | return fold_build1_loc (loc, NOP_EXPR, type, tem); |
088414c1 | 2088 | |
0bccc606 | 2089 | default: |
544d960a AS |
2090 | if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (orig)) |
2091 | return fold_build1_loc (loc, NOP_EXPR, type, arg); | |
0bccc606 | 2092 | gcc_unreachable (); |
088414c1 | 2093 | } |
db3927fb | 2094 | fold_convert_exit: |
c9019218 | 2095 | protected_set_expr_location_unshare (tem, loc); |
db3927fb | 2096 | return tem; |
088414c1 | 2097 | } |
6d716ca8 | 2098 | \f |
569b7f6a | 2099 | /* Return false if expr can be assumed not to be an lvalue, true |
283da5df | 2100 | otherwise. */ |
6d716ca8 | 2101 | |
283da5df | 2102 | static bool |
ac545c64 | 2103 | maybe_lvalue_p (const_tree x) |
6d716ca8 | 2104 | { |
8d4a2ff6 RS |
2105 | /* We only need to wrap lvalue tree codes. */ |
2106 | switch (TREE_CODE (x)) | |
2107 | { | |
2108 | case VAR_DECL: | |
2109 | case PARM_DECL: | |
2110 | case RESULT_DECL: | |
2111 | case LABEL_DECL: | |
2112 | case FUNCTION_DECL: | |
2113 | case SSA_NAME: | |
2114 | ||
2115 | case COMPONENT_REF: | |
75421dcd | 2116 | case MEM_REF: |
8d4a2ff6 RS |
2117 | case INDIRECT_REF: |
2118 | case ARRAY_REF: | |
44de5aeb | 2119 | case ARRAY_RANGE_REF: |
8d4a2ff6 | 2120 | case BIT_FIELD_REF: |
0f59171d | 2121 | case OBJ_TYPE_REF: |
8d4a2ff6 RS |
2122 | |
2123 | case REALPART_EXPR: | |
2124 | case IMAGPART_EXPR: | |
2125 | case PREINCREMENT_EXPR: | |
2126 | case PREDECREMENT_EXPR: | |
2127 | case SAVE_EXPR: | |
8d4a2ff6 RS |
2128 | case TRY_CATCH_EXPR: |
2129 | case WITH_CLEANUP_EXPR: | |
2130 | case COMPOUND_EXPR: | |
2131 | case MODIFY_EXPR: | |
2132 | case TARGET_EXPR: | |
2133 | case COND_EXPR: | |
2134 | case BIND_EXPR: | |
8d4a2ff6 RS |
2135 | break; |
2136 | ||
2137 | default: | |
2138 | /* Assume the worst for front-end tree codes. */ | |
2139 | if ((int)TREE_CODE (x) >= NUM_TREE_CODES) | |
2140 | break; | |
283da5df | 2141 | return false; |
8d4a2ff6 | 2142 | } |
283da5df RS |
2143 | |
2144 | return true; | |
2145 | } | |
2146 | ||
2147 | /* Return an expr equal to X but certainly not valid as an lvalue. */ | |
2148 | ||
2149 | tree | |
db3927fb | 2150 | non_lvalue_loc (location_t loc, tree x) |
283da5df RS |
2151 | { |
2152 | /* While we are in GIMPLE, NON_LVALUE_EXPR doesn't mean anything to | |
2153 | us. */ | |
2154 | if (in_gimple_form) | |
2155 | return x; | |
2156 | ||
2157 | if (! maybe_lvalue_p (x)) | |
2158 | return x; | |
c9019218 | 2159 | return build1_loc (loc, NON_LVALUE_EXPR, TREE_TYPE (x), x); |
6d716ca8 | 2160 | } |
a5e9b124 | 2161 | |
e9866da3 JM |
2162 | /* Nonzero means lvalues are limited to those valid in pedantic ANSI C. |
2163 | Zero means allow extended lvalues. */ | |
2164 | ||
2165 | int pedantic_lvalues; | |
2166 | ||
a5e9b124 JW |
2167 | /* When pedantic, return an expr equal to X but certainly not valid as a |
2168 | pedantic lvalue. Otherwise, return X. */ | |
2169 | ||
49995c8e | 2170 | static tree |
db3927fb | 2171 | pedantic_non_lvalue_loc (location_t loc, tree x) |
a5e9b124 | 2172 | { |
e9866da3 | 2173 | if (pedantic_lvalues) |
db3927fb | 2174 | return non_lvalue_loc (loc, x); |
47f647e4 | 2175 | |
c9019218 | 2176 | return protected_set_expr_location_unshare (x, loc); |
a5e9b124 | 2177 | } |
c05a9b68 | 2178 | \f |
4db183a2 EB |
2179 | /* Given a tree comparison code, return the code that is the logical inverse. |
2180 | It is generally not safe to do this for floating-point comparisons, except | |
77b5132f MG |
2181 | for EQ_EXPR, NE_EXPR, ORDERED_EXPR and UNORDERED_EXPR, so we return |
2182 | ERROR_MARK in this case. */ | |
6d716ca8 | 2183 | |
227858d1 | 2184 | enum tree_code |
d1a7edaf | 2185 | invert_tree_comparison (enum tree_code code, bool honor_nans) |
c05a9b68 | 2186 | { |
77b5132f MG |
2187 | if (honor_nans && flag_trapping_math && code != EQ_EXPR && code != NE_EXPR |
2188 | && code != ORDERED_EXPR && code != UNORDERED_EXPR) | |
d1a7edaf PB |
2189 | return ERROR_MARK; |
2190 | ||
c05a9b68 RS |
2191 | switch (code) |
2192 | { | |
2193 | case EQ_EXPR: | |
2194 | return NE_EXPR; | |
2195 | case NE_EXPR: | |
2196 | return EQ_EXPR; | |
2197 | case GT_EXPR: | |
d1a7edaf | 2198 | return honor_nans ? UNLE_EXPR : LE_EXPR; |
c05a9b68 | 2199 | case GE_EXPR: |
d1a7edaf | 2200 | return honor_nans ? UNLT_EXPR : LT_EXPR; |
c05a9b68 | 2201 | case LT_EXPR: |
d1a7edaf | 2202 | return honor_nans ? UNGE_EXPR : GE_EXPR; |
c05a9b68 | 2203 | case LE_EXPR: |
d1a7edaf PB |
2204 | return honor_nans ? UNGT_EXPR : GT_EXPR; |
2205 | case LTGT_EXPR: | |
2206 | return UNEQ_EXPR; | |
2207 | case UNEQ_EXPR: | |
2208 | return LTGT_EXPR; | |
2209 | case UNGT_EXPR: | |
2210 | return LE_EXPR; | |
2211 | case UNGE_EXPR: | |
2212 | return LT_EXPR; | |
2213 | case UNLT_EXPR: | |
2214 | return GE_EXPR; | |
2215 | case UNLE_EXPR: | |
c05a9b68 | 2216 | return GT_EXPR; |
d1a7edaf PB |
2217 | case ORDERED_EXPR: |
2218 | return UNORDERED_EXPR; | |
2219 | case UNORDERED_EXPR: | |
2220 | return ORDERED_EXPR; | |
c05a9b68 | 2221 | default: |
0bccc606 | 2222 | gcc_unreachable (); |
c05a9b68 RS |
2223 | } |
2224 | } | |
2225 | ||
2226 | /* Similar, but return the comparison that results if the operands are | |
2227 | swapped. This is safe for floating-point. */ | |
2228 | ||
fd660b1b | 2229 | enum tree_code |
fa8db1f7 | 2230 | swap_tree_comparison (enum tree_code code) |
c05a9b68 RS |
2231 | { |
2232 | switch (code) | |
2233 | { | |
2234 | case EQ_EXPR: | |
2235 | case NE_EXPR: | |
09b2f9e8 RS |
2236 | case ORDERED_EXPR: |
2237 | case UNORDERED_EXPR: | |
2238 | case LTGT_EXPR: | |
2239 | case UNEQ_EXPR: | |
c05a9b68 RS |
2240 | return code; |
2241 | case GT_EXPR: | |
2242 | return LT_EXPR; | |
2243 | case GE_EXPR: | |
2244 | return LE_EXPR; | |
2245 | case LT_EXPR: | |
2246 | return GT_EXPR; | |
2247 | case LE_EXPR: | |
2248 | return GE_EXPR; | |
09b2f9e8 RS |
2249 | case UNGT_EXPR: |
2250 | return UNLT_EXPR; | |
2251 | case UNGE_EXPR: | |
2252 | return UNLE_EXPR; | |
2253 | case UNLT_EXPR: | |
2254 | return UNGT_EXPR; | |
2255 | case UNLE_EXPR: | |
2256 | return UNGE_EXPR; | |
c05a9b68 | 2257 | default: |
0bccc606 | 2258 | gcc_unreachable (); |
c05a9b68 RS |
2259 | } |
2260 | } | |
61f275ff | 2261 | |
8dcb27ed RS |
2262 | |
2263 | /* Convert a comparison tree code from an enum tree_code representation | |
2264 | into a compcode bit-based encoding. This function is the inverse of | |
2265 | compcode_to_comparison. */ | |
2266 | ||
d1a7edaf | 2267 | static enum comparison_code |
fa8db1f7 | 2268 | comparison_to_compcode (enum tree_code code) |
8dcb27ed RS |
2269 | { |
2270 | switch (code) | |
2271 | { | |
2272 | case LT_EXPR: | |
2273 | return COMPCODE_LT; | |
2274 | case EQ_EXPR: | |
2275 | return COMPCODE_EQ; | |
2276 | case LE_EXPR: | |
2277 | return COMPCODE_LE; | |
2278 | case GT_EXPR: | |
2279 | return COMPCODE_GT; | |
2280 | case NE_EXPR: | |
2281 | return COMPCODE_NE; | |
2282 | case GE_EXPR: | |
2283 | return COMPCODE_GE; | |
d1a7edaf PB |
2284 | case ORDERED_EXPR: |
2285 | return COMPCODE_ORD; | |
2286 | case UNORDERED_EXPR: | |
2287 | return COMPCODE_UNORD; | |
2288 | case UNLT_EXPR: | |
2289 | return COMPCODE_UNLT; | |
2290 | case UNEQ_EXPR: | |
2291 | return COMPCODE_UNEQ; | |
2292 | case UNLE_EXPR: | |
2293 | return COMPCODE_UNLE; | |
2294 | case UNGT_EXPR: | |
2295 | return COMPCODE_UNGT; | |
2296 | case LTGT_EXPR: | |
2297 | return COMPCODE_LTGT; | |
2298 | case UNGE_EXPR: | |
2299 | return COMPCODE_UNGE; | |
8dcb27ed | 2300 | default: |
0bccc606 | 2301 | gcc_unreachable (); |
8dcb27ed RS |
2302 | } |
2303 | } | |
2304 | ||
2305 | /* Convert a compcode bit-based encoding of a comparison operator back | |
2306 | to GCC's enum tree_code representation. This function is the | |
2307 | inverse of comparison_to_compcode. */ | |
2308 | ||
2309 | static enum tree_code | |
d1a7edaf | 2310 | compcode_to_comparison (enum comparison_code code) |
8dcb27ed RS |
2311 | { |
2312 | switch (code) | |
2313 | { | |
2314 | case COMPCODE_LT: | |
2315 | return LT_EXPR; | |
2316 | case COMPCODE_EQ: | |
2317 | return EQ_EXPR; | |
2318 | case COMPCODE_LE: | |
2319 | return LE_EXPR; | |
2320 | case COMPCODE_GT: | |
2321 | return GT_EXPR; | |
2322 | case COMPCODE_NE: | |
2323 | return NE_EXPR; | |
2324 | case COMPCODE_GE: | |
2325 | return GE_EXPR; | |
d1a7edaf PB |
2326 | case COMPCODE_ORD: |
2327 | return ORDERED_EXPR; | |
2328 | case COMPCODE_UNORD: | |
2329 | return UNORDERED_EXPR; | |
2330 | case COMPCODE_UNLT: | |
2331 | return UNLT_EXPR; | |
2332 | case COMPCODE_UNEQ: | |
2333 | return UNEQ_EXPR; | |
2334 | case COMPCODE_UNLE: | |
2335 | return UNLE_EXPR; | |
2336 | case COMPCODE_UNGT: | |
2337 | return UNGT_EXPR; | |
2338 | case COMPCODE_LTGT: | |
2339 | return LTGT_EXPR; | |
2340 | case COMPCODE_UNGE: | |
2341 | return UNGE_EXPR; | |
8dcb27ed | 2342 | default: |
0bccc606 | 2343 | gcc_unreachable (); |
8dcb27ed RS |
2344 | } |
2345 | } | |
2346 | ||
d1a7edaf PB |
2347 | /* Return a tree for the comparison which is the combination of |
2348 | doing the AND or OR (depending on CODE) of the two operations LCODE | |
2349 | and RCODE on the identical operands LL_ARG and LR_ARG. Take into account | |
2350 | the possibility of trapping if the mode has NaNs, and return NULL_TREE | |
2351 | if this makes the transformation invalid. */ | |
2352 | ||
2353 | tree | |
db3927fb AH |
2354 | combine_comparisons (location_t loc, |
2355 | enum tree_code code, enum tree_code lcode, | |
d1a7edaf PB |
2356 | enum tree_code rcode, tree truth_type, |
2357 | tree ll_arg, tree lr_arg) | |
2358 | { | |
2359 | bool honor_nans = HONOR_NANS (TYPE_MODE (TREE_TYPE (ll_arg))); | |
2360 | enum comparison_code lcompcode = comparison_to_compcode (lcode); | |
2361 | enum comparison_code rcompcode = comparison_to_compcode (rcode); | |
32e8bb8e | 2362 | int compcode; |
d1a7edaf PB |
2363 | |
2364 | switch (code) | |
2365 | { | |
2366 | case TRUTH_AND_EXPR: case TRUTH_ANDIF_EXPR: | |
2367 | compcode = lcompcode & rcompcode; | |
2368 | break; | |
2369 | ||
2370 | case TRUTH_OR_EXPR: case TRUTH_ORIF_EXPR: | |
2371 | compcode = lcompcode | rcompcode; | |
2372 | break; | |
2373 | ||
2374 | default: | |
2375 | return NULL_TREE; | |
2376 | } | |
2377 | ||
2378 | if (!honor_nans) | |
2379 | { | |
2380 | /* Eliminate unordered comparisons, as well as LTGT and ORD | |
2381 | which are not used unless the mode has NaNs. */ | |
2382 | compcode &= ~COMPCODE_UNORD; | |
2383 | if (compcode == COMPCODE_LTGT) | |
2384 | compcode = COMPCODE_NE; | |
2385 | else if (compcode == COMPCODE_ORD) | |
2386 | compcode = COMPCODE_TRUE; | |
2387 | } | |
2388 | else if (flag_trapping_math) | |
2389 | { | |
d1822754 | 2390 | /* Check that the original operation and the optimized ones will trap |
d1a7edaf PB |
2391 | under the same condition. */ |
2392 | bool ltrap = (lcompcode & COMPCODE_UNORD) == 0 | |
2393 | && (lcompcode != COMPCODE_EQ) | |
2394 | && (lcompcode != COMPCODE_ORD); | |
2395 | bool rtrap = (rcompcode & COMPCODE_UNORD) == 0 | |
2396 | && (rcompcode != COMPCODE_EQ) | |
2397 | && (rcompcode != COMPCODE_ORD); | |
2398 | bool trap = (compcode & COMPCODE_UNORD) == 0 | |
2399 | && (compcode != COMPCODE_EQ) | |
2400 | && (compcode != COMPCODE_ORD); | |
2401 | ||
2402 | /* In a short-circuited boolean expression the LHS might be | |
2403 | such that the RHS, if evaluated, will never trap. For | |
2404 | example, in ORD (x, y) && (x < y), we evaluate the RHS only | |
2405 | if neither x nor y is NaN. (This is a mixed blessing: for | |
2406 | example, the expression above will never trap, hence | |
2407 | optimizing it to x < y would be invalid). */ | |
2408 | if ((code == TRUTH_ORIF_EXPR && (lcompcode & COMPCODE_UNORD)) | |
2409 | || (code == TRUTH_ANDIF_EXPR && !(lcompcode & COMPCODE_UNORD))) | |
2410 | rtrap = false; | |
2411 | ||
2412 | /* If the comparison was short-circuited, and only the RHS | |
2413 | trapped, we may now generate a spurious trap. */ | |
2414 | if (rtrap && !ltrap | |
2415 | && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR)) | |
2416 | return NULL_TREE; | |
2417 | ||
2418 | /* If we changed the conditions that cause a trap, we lose. */ | |
2419 | if ((ltrap || rtrap) != trap) | |
2420 | return NULL_TREE; | |
2421 | } | |
2422 | ||
2423 | if (compcode == COMPCODE_TRUE) | |
1b0f3e79 | 2424 | return constant_boolean_node (true, truth_type); |
d1a7edaf | 2425 | else if (compcode == COMPCODE_FALSE) |
1b0f3e79 | 2426 | return constant_boolean_node (false, truth_type); |
d1a7edaf | 2427 | else |
32e8bb8e ILT |
2428 | { |
2429 | enum tree_code tcode; | |
2430 | ||
2431 | tcode = compcode_to_comparison ((enum comparison_code) compcode); | |
db3927fb | 2432 | return fold_build2_loc (loc, tcode, truth_type, ll_arg, lr_arg); |
32e8bb8e | 2433 | } |
d1a7edaf | 2434 | } |
c05a9b68 | 2435 | \f |
fae111c1 RS |
2436 | /* Return nonzero if two operands (typically of the same tree node) |
2437 | are necessarily equal. If either argument has side-effects this | |
1ea7e6ad | 2438 | function returns zero. FLAGS modifies behavior as follows: |
fae111c1 | 2439 | |
6de9cd9a | 2440 | If OEP_ONLY_CONST is set, only return nonzero for constants. |
6a1746af RS |
2441 | This function tests whether the operands are indistinguishable; |
2442 | it does not test whether they are equal using C's == operation. | |
2443 | The distinction is important for IEEE floating point, because | |
2444 | (1) -0.0 and 0.0 are distinguishable, but -0.0==0.0, and | |
fae111c1 RS |
2445 | (2) two NaNs may be indistinguishable, but NaN!=NaN. |
2446 | ||
6de9cd9a | 2447 | If OEP_ONLY_CONST is unset, a VAR_DECL is considered equal to itself |
fae111c1 RS |
2448 | even though it may hold multiple values during a function. |
2449 | This is because a GCC tree node guarantees that nothing else is | |
2450 | executed between the evaluation of its "operands" (which may often | |
2451 | be evaluated in arbitrary order). Hence if the operands themselves | |
2452 | don't side-effect, the VAR_DECLs, PARM_DECLs etc... must hold the | |
3dd8069d PB |
2453 | same value in each operand/subexpression. Hence leaving OEP_ONLY_CONST |
2454 | unset means assuming isochronic (or instantaneous) tree equivalence. | |
2455 | Unless comparing arbitrary expression trees, such as from different | |
2456 | statements, this flag can usually be left unset. | |
6de9cd9a DN |
2457 | |
2458 | If OEP_PURE_SAME is set, then pure functions with identical arguments | |
2459 | are considered the same. It is used when the caller has other ways | |
2460 | to ensure that global memory is unchanged in between. */ | |
6d716ca8 RS |
2461 | |
2462 | int | |
fa233e34 | 2463 | operand_equal_p (const_tree arg0, const_tree arg1, unsigned int flags) |
6d716ca8 | 2464 | { |
8df83eae | 2465 | /* If either is ERROR_MARK, they aren't equal. */ |
2aac1924 JM |
2466 | if (TREE_CODE (arg0) == ERROR_MARK || TREE_CODE (arg1) == ERROR_MARK |
2467 | || TREE_TYPE (arg0) == error_mark_node | |
2468 | || TREE_TYPE (arg1) == error_mark_node) | |
8df83eae RK |
2469 | return 0; |
2470 | ||
56c47f22 RG |
2471 | /* Similar, if either does not have a type (like a released SSA name), |
2472 | they aren't equal. */ | |
2473 | if (!TREE_TYPE (arg0) || !TREE_TYPE (arg1)) | |
2474 | return 0; | |
2475 | ||
ba2e1892 RG |
2476 | /* Check equality of integer constants before bailing out due to |
2477 | precision differences. */ | |
2478 | if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST) | |
2479 | return tree_int_cst_equal (arg0, arg1); | |
2480 | ||
6d716ca8 RS |
2481 | /* If both types don't have the same signedness, then we can't consider |
2482 | them equal. We must check this before the STRIP_NOPS calls | |
b13e7b6c RG |
2483 | because they may change the signedness of the arguments. As pointers |
2484 | strictly don't have a signedness, require either two pointers or | |
2485 | two non-pointers as well. */ | |
2486 | if (TYPE_UNSIGNED (TREE_TYPE (arg0)) != TYPE_UNSIGNED (TREE_TYPE (arg1)) | |
2487 | || POINTER_TYPE_P (TREE_TYPE (arg0)) != POINTER_TYPE_P (TREE_TYPE (arg1))) | |
6d716ca8 RS |
2488 | return 0; |
2489 | ||
09e881c9 BE |
2490 | /* We cannot consider pointers to different address space equal. */ |
2491 | if (POINTER_TYPE_P (TREE_TYPE (arg0)) && POINTER_TYPE_P (TREE_TYPE (arg1)) | |
2492 | && (TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (arg0))) | |
2493 | != TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (arg1))))) | |
2494 | return 0; | |
2495 | ||
096dce1b RG |
2496 | /* If both types don't have the same precision, then it is not safe |
2497 | to strip NOPs. */ | |
a5e0cd1d MG |
2498 | if (element_precision (TREE_TYPE (arg0)) |
2499 | != element_precision (TREE_TYPE (arg1))) | |
096dce1b RG |
2500 | return 0; |
2501 | ||
6d716ca8 RS |
2502 | STRIP_NOPS (arg0); |
2503 | STRIP_NOPS (arg1); | |
2504 | ||
a04d8591 RG |
2505 | /* In case both args are comparisons but with different comparison |
2506 | code, try to swap the comparison operands of one arg to produce | |
2507 | a match and compare that variant. */ | |
2508 | if (TREE_CODE (arg0) != TREE_CODE (arg1) | |
2509 | && COMPARISON_CLASS_P (arg0) | |
2510 | && COMPARISON_CLASS_P (arg1)) | |
2511 | { | |
2512 | enum tree_code swap_code = swap_tree_comparison (TREE_CODE (arg1)); | |
2513 | ||
2514 | if (TREE_CODE (arg0) == swap_code) | |
2515 | return operand_equal_p (TREE_OPERAND (arg0, 0), | |
2516 | TREE_OPERAND (arg1, 1), flags) | |
2517 | && operand_equal_p (TREE_OPERAND (arg0, 1), | |
2518 | TREE_OPERAND (arg1, 0), flags); | |
2519 | } | |
2520 | ||
c7cfe938 | 2521 | if (TREE_CODE (arg0) != TREE_CODE (arg1) |
a8355e51 BC |
2522 | /* NOP_EXPR and CONVERT_EXPR are considered equal. */ |
2523 | && !(CONVERT_EXPR_P (arg0) && CONVERT_EXPR_P (arg1))) | |
2524 | return 0; | |
2525 | ||
2526 | /* This is needed for conversions and for COMPONENT_REF. | |
2527 | Might as well play it safe and always test this. */ | |
2528 | if (TREE_CODE (TREE_TYPE (arg0)) == ERROR_MARK | |
e89a9554 | 2529 | || TREE_CODE (TREE_TYPE (arg1)) == ERROR_MARK |
c7cfe938 | 2530 | || TYPE_MODE (TREE_TYPE (arg0)) != TYPE_MODE (TREE_TYPE (arg1))) |
6d716ca8 RS |
2531 | return 0; |
2532 | ||
c7cfe938 RK |
2533 | /* If ARG0 and ARG1 are the same SAVE_EXPR, they are necessarily equal. |
2534 | We don't care about side effects in that case because the SAVE_EXPR | |
2535 | takes care of that for us. In all other cases, two expressions are | |
2536 | equal if they have no side effects. If we have two identical | |
2537 | expressions with side effects that should be treated the same due | |
2538 | to the only side effects being identical SAVE_EXPR's, that will | |
ad0e2567 RG |
2539 | be detected in the recursive calls below. |
2540 | If we are taking an invariant address of two identical objects | |
2541 | they are necessarily equal as well. */ | |
6de9cd9a | 2542 | if (arg0 == arg1 && ! (flags & OEP_ONLY_CONST) |
c7cfe938 | 2543 | && (TREE_CODE (arg0) == SAVE_EXPR |
ad0e2567 | 2544 | || (flags & OEP_CONSTANT_ADDRESS_OF) |
c7cfe938 | 2545 | || (! TREE_SIDE_EFFECTS (arg0) && ! TREE_SIDE_EFFECTS (arg1)))) |
6d716ca8 RS |
2546 | return 1; |
2547 | ||
c7cfe938 RK |
2548 | /* Next handle constant cases, those for which we can return 1 even |
2549 | if ONLY_CONST is set. */ | |
2550 | if (TREE_CONSTANT (arg0) && TREE_CONSTANT (arg1)) | |
2551 | switch (TREE_CODE (arg0)) | |
2552 | { | |
2553 | case INTEGER_CST: | |
85914552 | 2554 | return tree_int_cst_equal (arg0, arg1); |
c7cfe938 | 2555 | |
325217ed CF |
2556 | case FIXED_CST: |
2557 | return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (arg0), | |
2558 | TREE_FIXED_CST (arg1)); | |
2559 | ||
c7cfe938 | 2560 | case REAL_CST: |
0446c9f3 ZD |
2561 | if (REAL_VALUES_IDENTICAL (TREE_REAL_CST (arg0), |
2562 | TREE_REAL_CST (arg1))) | |
2563 | return 1; | |
2564 | ||
b8698a0f | 2565 | |
0446c9f3 ZD |
2566 | if (!HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg0)))) |
2567 | { | |
2568 | /* If we do not distinguish between signed and unsigned zero, | |
2569 | consider them equal. */ | |
2570 | if (real_zerop (arg0) && real_zerop (arg1)) | |
2571 | return 1; | |
2572 | } | |
2573 | return 0; | |
c7cfe938 | 2574 | |
69ef87e2 AH |
2575 | case VECTOR_CST: |
2576 | { | |
d2a12ae7 | 2577 | unsigned i; |
69ef87e2 | 2578 | |
d2a12ae7 RG |
2579 | if (VECTOR_CST_NELTS (arg0) != VECTOR_CST_NELTS (arg1)) |
2580 | return 0; | |
2581 | ||
2582 | for (i = 0; i < VECTOR_CST_NELTS (arg0); ++i) | |
69ef87e2 | 2583 | { |
d2a12ae7 RG |
2584 | if (!operand_equal_p (VECTOR_CST_ELT (arg0, i), |
2585 | VECTOR_CST_ELT (arg1, i), flags)) | |
69ef87e2 | 2586 | return 0; |
69ef87e2 | 2587 | } |
d2a12ae7 | 2588 | return 1; |
69ef87e2 AH |
2589 | } |
2590 | ||
c7cfe938 RK |
2591 | case COMPLEX_CST: |
2592 | return (operand_equal_p (TREE_REALPART (arg0), TREE_REALPART (arg1), | |
6de9cd9a | 2593 | flags) |
c7cfe938 | 2594 | && operand_equal_p (TREE_IMAGPART (arg0), TREE_IMAGPART (arg1), |
6de9cd9a | 2595 | flags)); |
c7cfe938 RK |
2596 | |
2597 | case STRING_CST: | |
2598 | return (TREE_STRING_LENGTH (arg0) == TREE_STRING_LENGTH (arg1) | |
71145810 | 2599 | && ! memcmp (TREE_STRING_POINTER (arg0), |
c7cfe938 RK |
2600 | TREE_STRING_POINTER (arg1), |
2601 | TREE_STRING_LENGTH (arg0))); | |
2602 | ||
2603 | case ADDR_EXPR: | |
2604 | return operand_equal_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg1, 0), | |
ad0e2567 RG |
2605 | TREE_CONSTANT (arg0) && TREE_CONSTANT (arg1) |
2606 | ? OEP_CONSTANT_ADDRESS_OF : 0); | |
e9a25f70 JL |
2607 | default: |
2608 | break; | |
c7cfe938 | 2609 | } |
6d716ca8 | 2610 | |
6de9cd9a | 2611 | if (flags & OEP_ONLY_CONST) |
6d716ca8 RS |
2612 | return 0; |
2613 | ||
38318b73 | 2614 | /* Define macros to test an operand from arg0 and arg1 for equality and a |
624b15fa RK |
2615 | variant that allows null and views null as being different from any |
2616 | non-null value. In the latter case, if either is null, the both | |
2617 | must be; otherwise, do the normal comparison. */ | |
2618 | #define OP_SAME(N) operand_equal_p (TREE_OPERAND (arg0, N), \ | |
2619 | TREE_OPERAND (arg1, N), flags) | |
2620 | ||
2621 | #define OP_SAME_WITH_NULL(N) \ | |
2622 | ((!TREE_OPERAND (arg0, N) || !TREE_OPERAND (arg1, N)) \ | |
2623 | ? TREE_OPERAND (arg0, N) == TREE_OPERAND (arg1, N) : OP_SAME (N)) | |
2624 | ||
6d716ca8 RS |
2625 | switch (TREE_CODE_CLASS (TREE_CODE (arg0))) |
2626 | { | |
6615c446 | 2627 | case tcc_unary: |
6d716ca8 | 2628 | /* Two conversions are equal only if signedness and modes match. */ |
266bff3a JJ |
2629 | switch (TREE_CODE (arg0)) |
2630 | { | |
1043771b | 2631 | CASE_CONVERT: |
266bff3a | 2632 | case FIX_TRUNC_EXPR: |
266bff3a JJ |
2633 | if (TYPE_UNSIGNED (TREE_TYPE (arg0)) |
2634 | != TYPE_UNSIGNED (TREE_TYPE (arg1))) | |
2635 | return 0; | |
2636 | break; | |
2637 | default: | |
2638 | break; | |
2639 | } | |
6d716ca8 | 2640 | |
624b15fa RK |
2641 | return OP_SAME (0); |
2642 | ||
6d716ca8 | 2643 | |
6615c446 JO |
2644 | case tcc_comparison: |
2645 | case tcc_binary: | |
624b15fa | 2646 | if (OP_SAME (0) && OP_SAME (1)) |
c7cfe938 RK |
2647 | return 1; |
2648 | ||
2649 | /* For commutative ops, allow the other order. */ | |
3168cb99 | 2650 | return (commutative_tree_code (TREE_CODE (arg0)) |
c7cfe938 | 2651 | && operand_equal_p (TREE_OPERAND (arg0, 0), |
6de9cd9a | 2652 | TREE_OPERAND (arg1, 1), flags) |
6d716ca8 | 2653 | && operand_equal_p (TREE_OPERAND (arg0, 1), |
6de9cd9a | 2654 | TREE_OPERAND (arg1, 0), flags)); |
6d716ca8 | 2655 | |
6615c446 | 2656 | case tcc_reference: |
21c43754 | 2657 | /* If either of the pointer (or reference) expressions we are |
cecbe5d9 JJ |
2658 | dereferencing contain a side effect, these cannot be equal, |
2659 | but their addresses can be. */ | |
2660 | if ((flags & OEP_CONSTANT_ADDRESS_OF) == 0 | |
2661 | && (TREE_SIDE_EFFECTS (arg0) | |
2662 | || TREE_SIDE_EFFECTS (arg1))) | |
05ca5990 GRK |
2663 | return 0; |
2664 | ||
6d716ca8 RS |
2665 | switch (TREE_CODE (arg0)) |
2666 | { | |
2667 | case INDIRECT_REF: | |
cecbe5d9 JJ |
2668 | flags &= ~OEP_CONSTANT_ADDRESS_OF; |
2669 | return OP_SAME (0); | |
2670 | ||
497be978 RH |
2671 | case REALPART_EXPR: |
2672 | case IMAGPART_EXPR: | |
624b15fa | 2673 | return OP_SAME (0); |
6d716ca8 | 2674 | |
2515d916 | 2675 | case TARGET_MEM_REF: |
cecbe5d9 | 2676 | flags &= ~OEP_CONSTANT_ADDRESS_OF; |
073a8998 | 2677 | /* Require equal extra operands and then fall through to MEM_REF |
2515d916 RG |
2678 | handling of the two common operands. */ |
2679 | if (!OP_SAME_WITH_NULL (2) | |
2680 | || !OP_SAME_WITH_NULL (3) | |
2681 | || !OP_SAME_WITH_NULL (4)) | |
2682 | return 0; | |
2683 | /* Fallthru. */ | |
70f34814 | 2684 | case MEM_REF: |
cecbe5d9 | 2685 | flags &= ~OEP_CONSTANT_ADDRESS_OF; |
359bea1d AO |
2686 | /* Require equal access sizes, and similar pointer types. |
2687 | We can have incomplete types for array references of | |
38c56a5b JJ |
2688 | variable-sized arrays from the Fortran frontend |
2689 | though. Also verify the types are compatible. */ | |
70f34814 RG |
2690 | return ((TYPE_SIZE (TREE_TYPE (arg0)) == TYPE_SIZE (TREE_TYPE (arg1)) |
2691 | || (TYPE_SIZE (TREE_TYPE (arg0)) | |
2692 | && TYPE_SIZE (TREE_TYPE (arg1)) | |
2693 | && operand_equal_p (TYPE_SIZE (TREE_TYPE (arg0)), | |
2694 | TYPE_SIZE (TREE_TYPE (arg1)), flags))) | |
38c56a5b | 2695 | && types_compatible_p (TREE_TYPE (arg0), TREE_TYPE (arg1)) |
f40333af RB |
2696 | && alias_ptr_types_compatible_p |
2697 | (TREE_TYPE (TREE_OPERAND (arg0, 1)), | |
2698 | TREE_TYPE (TREE_OPERAND (arg1, 1))) | |
70f34814 RG |
2699 | && OP_SAME (0) && OP_SAME (1)); |
2700 | ||
6d716ca8 | 2701 | case ARRAY_REF: |
b4e3fabb | 2702 | case ARRAY_RANGE_REF: |
5852948c RG |
2703 | /* Operands 2 and 3 may be null. |
2704 | Compare the array index by value if it is constant first as we | |
2705 | may have different types but same value here. */ | |
cecbe5d9 JJ |
2706 | if (!OP_SAME (0)) |
2707 | return 0; | |
2708 | flags &= ~OEP_CONSTANT_ADDRESS_OF; | |
2709 | return ((tree_int_cst_equal (TREE_OPERAND (arg0, 1), | |
2710 | TREE_OPERAND (arg1, 1)) | |
2711 | || OP_SAME (1)) | |
624b15fa RK |
2712 | && OP_SAME_WITH_NULL (2) |
2713 | && OP_SAME_WITH_NULL (3)); | |
462fdcce RK |
2714 | |
2715 | case COMPONENT_REF: | |
78b76d08 SB |
2716 | /* Handle operand 2 the same as for ARRAY_REF. Operand 0 |
2717 | may be NULL when we're called to compare MEM_EXPRs. */ | |
2445aeac RB |
2718 | if (!OP_SAME_WITH_NULL (0) |
2719 | || !OP_SAME (1)) | |
cecbe5d9 JJ |
2720 | return 0; |
2721 | flags &= ~OEP_CONSTANT_ADDRESS_OF; | |
2445aeac | 2722 | return OP_SAME_WITH_NULL (2); |
a60749f5 | 2723 | |
40b32ef8 | 2724 | case BIT_FIELD_REF: |
cecbe5d9 JJ |
2725 | if (!OP_SAME (0)) |
2726 | return 0; | |
2727 | flags &= ~OEP_CONSTANT_ADDRESS_OF; | |
2728 | return OP_SAME (1) && OP_SAME (2); | |
624b15fa | 2729 | |
e9a25f70 JL |
2730 | default: |
2731 | return 0; | |
6d716ca8 | 2732 | } |
45f97e2e | 2733 | |
6615c446 | 2734 | case tcc_expression: |
1bfedcc8 JM |
2735 | switch (TREE_CODE (arg0)) |
2736 | { | |
2737 | case ADDR_EXPR: | |
2738 | case TRUTH_NOT_EXPR: | |
624b15fa | 2739 | return OP_SAME (0); |
1bfedcc8 | 2740 | |
54d581a2 RS |
2741 | case TRUTH_ANDIF_EXPR: |
2742 | case TRUTH_ORIF_EXPR: | |
624b15fa | 2743 | return OP_SAME (0) && OP_SAME (1); |
54d581a2 | 2744 | |
180ed1b2 RH |
2745 | case FMA_EXPR: |
2746 | case WIDEN_MULT_PLUS_EXPR: | |
2747 | case WIDEN_MULT_MINUS_EXPR: | |
2748 | if (!OP_SAME (2)) | |
2749 | return 0; | |
2750 | /* The multiplcation operands are commutative. */ | |
2751 | /* FALLTHRU */ | |
2752 | ||
54d581a2 RS |
2753 | case TRUTH_AND_EXPR: |
2754 | case TRUTH_OR_EXPR: | |
2755 | case TRUTH_XOR_EXPR: | |
624b15fa RK |
2756 | if (OP_SAME (0) && OP_SAME (1)) |
2757 | return 1; | |
2758 | ||
2759 | /* Otherwise take into account this is a commutative operation. */ | |
54d581a2 | 2760 | return (operand_equal_p (TREE_OPERAND (arg0, 0), |
624b15fa | 2761 | TREE_OPERAND (arg1, 1), flags) |
54d581a2 | 2762 | && operand_equal_p (TREE_OPERAND (arg0, 1), |
624b15fa | 2763 | TREE_OPERAND (arg1, 0), flags)); |
54d581a2 | 2764 | |
05f41289 | 2765 | case COND_EXPR: |
180ed1b2 RH |
2766 | case VEC_COND_EXPR: |
2767 | case DOT_PROD_EXPR: | |
05f41289 | 2768 | return OP_SAME (0) && OP_SAME (1) && OP_SAME (2); |
b8698a0f | 2769 | |
5039610b SL |
2770 | default: |
2771 | return 0; | |
2772 | } | |
2773 | ||
2774 | case tcc_vl_exp: | |
2775 | switch (TREE_CODE (arg0)) | |
2776 | { | |
21c43754 RS |
2777 | case CALL_EXPR: |
2778 | /* If the CALL_EXPRs call different functions, then they | |
2779 | clearly can not be equal. */ | |
5039610b SL |
2780 | if (! operand_equal_p (CALL_EXPR_FN (arg0), CALL_EXPR_FN (arg1), |
2781 | flags)) | |
21c43754 RS |
2782 | return 0; |
2783 | ||
6de9cd9a DN |
2784 | { |
2785 | unsigned int cef = call_expr_flags (arg0); | |
2786 | if (flags & OEP_PURE_SAME) | |
2787 | cef &= ECF_CONST | ECF_PURE; | |
2788 | else | |
2789 | cef &= ECF_CONST; | |
2790 | if (!cef) | |
2791 | return 0; | |
2792 | } | |
21c43754 | 2793 | |
5039610b SL |
2794 | /* Now see if all the arguments are the same. */ |
2795 | { | |
fa233e34 KG |
2796 | const_call_expr_arg_iterator iter0, iter1; |
2797 | const_tree a0, a1; | |
2798 | for (a0 = first_const_call_expr_arg (arg0, &iter0), | |
2799 | a1 = first_const_call_expr_arg (arg1, &iter1); | |
5039610b | 2800 | a0 && a1; |
fa233e34 KG |
2801 | a0 = next_const_call_expr_arg (&iter0), |
2802 | a1 = next_const_call_expr_arg (&iter1)) | |
5039610b | 2803 | if (! operand_equal_p (a0, a1, flags)) |
21c43754 RS |
2804 | return 0; |
2805 | ||
5039610b SL |
2806 | /* If we get here and both argument lists are exhausted |
2807 | then the CALL_EXPRs are equal. */ | |
2808 | return ! (a0 || a1); | |
2809 | } | |
1bfedcc8 JM |
2810 | default: |
2811 | return 0; | |
2812 | } | |
b6cc0a72 | 2813 | |
6615c446 | 2814 | case tcc_declaration: |
6de9cd9a DN |
2815 | /* Consider __builtin_sqrt equal to sqrt. */ |
2816 | return (TREE_CODE (arg0) == FUNCTION_DECL | |
2817 | && DECL_BUILT_IN (arg0) && DECL_BUILT_IN (arg1) | |
2818 | && DECL_BUILT_IN_CLASS (arg0) == DECL_BUILT_IN_CLASS (arg1) | |
2819 | && DECL_FUNCTION_CODE (arg0) == DECL_FUNCTION_CODE (arg1)); | |
21c43754 | 2820 | |
e9a25f70 JL |
2821 | default: |
2822 | return 0; | |
6d716ca8 | 2823 | } |
624b15fa RK |
2824 | |
2825 | #undef OP_SAME | |
2826 | #undef OP_SAME_WITH_NULL | |
6d716ca8 | 2827 | } |
c05a9b68 RS |
2828 | \f |
2829 | /* Similar to operand_equal_p, but see if ARG0 might have been made by | |
b6cc0a72 | 2830 | shorten_compare from ARG1 when ARG1 was being compared with OTHER. |
6d716ca8 | 2831 | |
6d716ca8 RS |
2832 | When in doubt, return 0. */ |
2833 | ||
b6cc0a72 | 2834 | static int |
fa8db1f7 | 2835 | operand_equal_for_comparison_p (tree arg0, tree arg1, tree other) |
6d716ca8 | 2836 | { |
c05a9b68 | 2837 | int unsignedp1, unsignedpo; |
52de9b6c | 2838 | tree primarg0, primarg1, primother; |
770ae6cc | 2839 | unsigned int correct_width; |
6d716ca8 | 2840 | |
c05a9b68 | 2841 | if (operand_equal_p (arg0, arg1, 0)) |
6d716ca8 RS |
2842 | return 1; |
2843 | ||
0982a4b8 JM |
2844 | if (! INTEGRAL_TYPE_P (TREE_TYPE (arg0)) |
2845 | || ! INTEGRAL_TYPE_P (TREE_TYPE (arg1))) | |
6d716ca8 RS |
2846 | return 0; |
2847 | ||
52de9b6c RK |
2848 | /* Discard any conversions that don't change the modes of ARG0 and ARG1 |
2849 | and see if the inner values are the same. This removes any | |
2850 | signedness comparison, which doesn't matter here. */ | |
2851 | primarg0 = arg0, primarg1 = arg1; | |
b6cc0a72 KH |
2852 | STRIP_NOPS (primarg0); |
2853 | STRIP_NOPS (primarg1); | |
52de9b6c RK |
2854 | if (operand_equal_p (primarg0, primarg1, 0)) |
2855 | return 1; | |
2856 | ||
c05a9b68 RS |
2857 | /* Duplicate what shorten_compare does to ARG1 and see if that gives the |
2858 | actual comparison operand, ARG0. | |
6d716ca8 | 2859 | |
c05a9b68 | 2860 | First throw away any conversions to wider types |
6d716ca8 | 2861 | already present in the operands. */ |
6d716ca8 | 2862 | |
c05a9b68 RS |
2863 | primarg1 = get_narrower (arg1, &unsignedp1); |
2864 | primother = get_narrower (other, &unsignedpo); | |
2865 | ||
2866 | correct_width = TYPE_PRECISION (TREE_TYPE (arg1)); | |
2867 | if (unsignedp1 == unsignedpo | |
2868 | && TYPE_PRECISION (TREE_TYPE (primarg1)) < correct_width | |
2869 | && TYPE_PRECISION (TREE_TYPE (primother)) < correct_width) | |
6d716ca8 | 2870 | { |
c05a9b68 | 2871 | tree type = TREE_TYPE (arg0); |
6d716ca8 RS |
2872 | |
2873 | /* Make sure shorter operand is extended the right way | |
2874 | to match the longer operand. */ | |
12753674 | 2875 | primarg1 = fold_convert (signed_or_unsigned_type_for |
088414c1 | 2876 | (unsignedp1, TREE_TYPE (primarg1)), primarg1); |
6d716ca8 | 2877 | |
088414c1 | 2878 | if (operand_equal_p (arg0, fold_convert (type, primarg1), 0)) |
6d716ca8 RS |
2879 | return 1; |
2880 | } | |
2881 | ||
2882 | return 0; | |
2883 | } | |
2884 | \f | |
f72aed24 | 2885 | /* See if ARG is an expression that is either a comparison or is performing |
c05a9b68 RS |
2886 | arithmetic on comparisons. The comparisons must only be comparing |
2887 | two different values, which will be stored in *CVAL1 and *CVAL2; if | |
cc2902df | 2888 | they are nonzero it means that some operands have already been found. |
c05a9b68 | 2889 | No variables may be used anywhere else in the expression except in the |
35e66bd1 RK |
2890 | comparisons. If SAVE_P is true it means we removed a SAVE_EXPR around |
2891 | the expression and save_expr needs to be called with CVAL1 and CVAL2. | |
c05a9b68 RS |
2892 | |
2893 | If this is true, return 1. Otherwise, return zero. */ | |
2894 | ||
2895 | static int | |
fa8db1f7 | 2896 | twoval_comparison_p (tree arg, tree *cval1, tree *cval2, int *save_p) |
c05a9b68 RS |
2897 | { |
2898 | enum tree_code code = TREE_CODE (arg); | |
82d6e6fc | 2899 | enum tree_code_class tclass = TREE_CODE_CLASS (code); |
c05a9b68 | 2900 | |
6615c446 | 2901 | /* We can handle some of the tcc_expression cases here. */ |
82d6e6fc KG |
2902 | if (tclass == tcc_expression && code == TRUTH_NOT_EXPR) |
2903 | tclass = tcc_unary; | |
2904 | else if (tclass == tcc_expression | |
c05a9b68 RS |
2905 | && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR |
2906 | || code == COMPOUND_EXPR)) | |
82d6e6fc | 2907 | tclass = tcc_binary; |
2315a5db | 2908 | |
82d6e6fc | 2909 | else if (tclass == tcc_expression && code == SAVE_EXPR |
d4b60170 | 2910 | && ! TREE_SIDE_EFFECTS (TREE_OPERAND (arg, 0))) |
35e66bd1 RK |
2911 | { |
2912 | /* If we've already found a CVAL1 or CVAL2, this expression is | |
2913 | two complex to handle. */ | |
2914 | if (*cval1 || *cval2) | |
2915 | return 0; | |
2916 | ||
82d6e6fc | 2917 | tclass = tcc_unary; |
35e66bd1 RK |
2918 | *save_p = 1; |
2919 | } | |
c05a9b68 | 2920 | |
82d6e6fc | 2921 | switch (tclass) |
c05a9b68 | 2922 | { |
6615c446 | 2923 | case tcc_unary: |
35e66bd1 | 2924 | return twoval_comparison_p (TREE_OPERAND (arg, 0), cval1, cval2, save_p); |
c05a9b68 | 2925 | |
6615c446 | 2926 | case tcc_binary: |
35e66bd1 RK |
2927 | return (twoval_comparison_p (TREE_OPERAND (arg, 0), cval1, cval2, save_p) |
2928 | && twoval_comparison_p (TREE_OPERAND (arg, 1), | |
2929 | cval1, cval2, save_p)); | |
c05a9b68 | 2930 | |
6615c446 | 2931 | case tcc_constant: |
c05a9b68 RS |
2932 | return 1; |
2933 | ||
6615c446 | 2934 | case tcc_expression: |
c05a9b68 | 2935 | if (code == COND_EXPR) |
35e66bd1 RK |
2936 | return (twoval_comparison_p (TREE_OPERAND (arg, 0), |
2937 | cval1, cval2, save_p) | |
2938 | && twoval_comparison_p (TREE_OPERAND (arg, 1), | |
2939 | cval1, cval2, save_p) | |
c05a9b68 | 2940 | && twoval_comparison_p (TREE_OPERAND (arg, 2), |
35e66bd1 | 2941 | cval1, cval2, save_p)); |
c05a9b68 | 2942 | return 0; |
b6cc0a72 | 2943 | |
6615c446 | 2944 | case tcc_comparison: |
c05a9b68 RS |
2945 | /* First see if we can handle the first operand, then the second. For |
2946 | the second operand, we know *CVAL1 can't be zero. It must be that | |
2947 | one side of the comparison is each of the values; test for the | |
2948 | case where this isn't true by failing if the two operands | |
2949 | are the same. */ | |
2950 | ||
2951 | if (operand_equal_p (TREE_OPERAND (arg, 0), | |
2952 | TREE_OPERAND (arg, 1), 0)) | |
2953 | return 0; | |
2954 | ||
2955 | if (*cval1 == 0) | |
2956 | *cval1 = TREE_OPERAND (arg, 0); | |
2957 | else if (operand_equal_p (*cval1, TREE_OPERAND (arg, 0), 0)) | |
2958 | ; | |
2959 | else if (*cval2 == 0) | |
2960 | *cval2 = TREE_OPERAND (arg, 0); | |
2961 | else if (operand_equal_p (*cval2, TREE_OPERAND (arg, 0), 0)) | |
2962 | ; | |
2963 | else | |
2964 | return 0; | |
2965 | ||
2966 | if (operand_equal_p (*cval1, TREE_OPERAND (arg, 1), 0)) | |
2967 | ; | |
2968 | else if (*cval2 == 0) | |
2969 | *cval2 = TREE_OPERAND (arg, 1); | |
2970 | else if (operand_equal_p (*cval2, TREE_OPERAND (arg, 1), 0)) | |
2971 | ; | |
2972 | else | |
2973 | return 0; | |
2974 | ||
2975 | return 1; | |
c05a9b68 | 2976 | |
e9a25f70 JL |
2977 | default: |
2978 | return 0; | |
2979 | } | |
c05a9b68 RS |
2980 | } |
2981 | \f | |
2982 | /* ARG is a tree that is known to contain just arithmetic operations and | |
2983 | comparisons. Evaluate the operations in the tree substituting NEW0 for | |
f72aed24 | 2984 | any occurrence of OLD0 as an operand of a comparison and likewise for |
c05a9b68 RS |
2985 | NEW1 and OLD1. */ |
2986 | ||
2987 | static tree | |
db3927fb AH |
2988 | eval_subst (location_t loc, tree arg, tree old0, tree new0, |
2989 | tree old1, tree new1) | |
c05a9b68 RS |
2990 | { |
2991 | tree type = TREE_TYPE (arg); | |
2992 | enum tree_code code = TREE_CODE (arg); | |
82d6e6fc | 2993 | enum tree_code_class tclass = TREE_CODE_CLASS (code); |
c05a9b68 | 2994 | |
6615c446 | 2995 | /* We can handle some of the tcc_expression cases here. */ |
82d6e6fc KG |
2996 | if (tclass == tcc_expression && code == TRUTH_NOT_EXPR) |
2997 | tclass = tcc_unary; | |
2998 | else if (tclass == tcc_expression | |
c05a9b68 | 2999 | && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR)) |
82d6e6fc | 3000 | tclass = tcc_binary; |
c05a9b68 | 3001 | |
82d6e6fc | 3002 | switch (tclass) |
c05a9b68 | 3003 | { |
6615c446 | 3004 | case tcc_unary: |
db3927fb AH |
3005 | return fold_build1_loc (loc, code, type, |
3006 | eval_subst (loc, TREE_OPERAND (arg, 0), | |
7f20a5b7 | 3007 | old0, new0, old1, new1)); |
c05a9b68 | 3008 | |
6615c446 | 3009 | case tcc_binary: |
db3927fb AH |
3010 | return fold_build2_loc (loc, code, type, |
3011 | eval_subst (loc, TREE_OPERAND (arg, 0), | |
7f20a5b7 | 3012 | old0, new0, old1, new1), |
db3927fb | 3013 | eval_subst (loc, TREE_OPERAND (arg, 1), |
7f20a5b7 | 3014 | old0, new0, old1, new1)); |
c05a9b68 | 3015 | |
6615c446 | 3016 | case tcc_expression: |
c05a9b68 RS |
3017 | switch (code) |
3018 | { | |
3019 | case SAVE_EXPR: | |
db3927fb AH |
3020 | return eval_subst (loc, TREE_OPERAND (arg, 0), old0, new0, |
3021 | old1, new1); | |
c05a9b68 RS |
3022 | |
3023 | case COMPOUND_EXPR: | |
db3927fb AH |
3024 | return eval_subst (loc, TREE_OPERAND (arg, 1), old0, new0, |
3025 | old1, new1); | |
c05a9b68 RS |
3026 | |
3027 | case COND_EXPR: | |
db3927fb AH |
3028 | return fold_build3_loc (loc, code, type, |
3029 | eval_subst (loc, TREE_OPERAND (arg, 0), | |
7f20a5b7 | 3030 | old0, new0, old1, new1), |
db3927fb | 3031 | eval_subst (loc, TREE_OPERAND (arg, 1), |
7f20a5b7 | 3032 | old0, new0, old1, new1), |
db3927fb | 3033 | eval_subst (loc, TREE_OPERAND (arg, 2), |
7f20a5b7 | 3034 | old0, new0, old1, new1)); |
e9a25f70 JL |
3035 | default: |
3036 | break; | |
c05a9b68 | 3037 | } |
938d968e | 3038 | /* Fall through - ??? */ |
c05a9b68 | 3039 | |
6615c446 | 3040 | case tcc_comparison: |
c05a9b68 RS |
3041 | { |
3042 | tree arg0 = TREE_OPERAND (arg, 0); | |
3043 | tree arg1 = TREE_OPERAND (arg, 1); | |
3044 | ||
3045 | /* We need to check both for exact equality and tree equality. The | |
3046 | former will be true if the operand has a side-effect. In that | |
3047 | case, we know the operand occurred exactly once. */ | |
3048 | ||
3049 | if (arg0 == old0 || operand_equal_p (arg0, old0, 0)) | |
3050 | arg0 = new0; | |
3051 | else if (arg0 == old1 || operand_equal_p (arg0, old1, 0)) | |
3052 | arg0 = new1; | |
3053 | ||
3054 | if (arg1 == old0 || operand_equal_p (arg1, old0, 0)) | |
3055 | arg1 = new0; | |
3056 | else if (arg1 == old1 || operand_equal_p (arg1, old1, 0)) | |
3057 | arg1 = new1; | |
3058 | ||
db3927fb | 3059 | return fold_build2_loc (loc, code, type, arg0, arg1); |
c05a9b68 | 3060 | } |
c05a9b68 | 3061 | |
e9a25f70 JL |
3062 | default: |
3063 | return arg; | |
3064 | } | |
c05a9b68 RS |
3065 | } |
3066 | \f | |
6d716ca8 RS |
3067 | /* Return a tree for the case when the result of an expression is RESULT |
3068 | converted to TYPE and OMITTED was previously an operand of the expression | |
3069 | but is now not needed (e.g., we folded OMITTED * 0). | |
3070 | ||
3071 | If OMITTED has side effects, we must evaluate it. Otherwise, just do | |
3072 | the conversion of RESULT to TYPE. */ | |
3073 | ||
c0a47a61 | 3074 | tree |
db3927fb | 3075 | omit_one_operand_loc (location_t loc, tree type, tree result, tree omitted) |
6d716ca8 | 3076 | { |
db3927fb | 3077 | tree t = fold_convert_loc (loc, type, result); |
6d716ca8 | 3078 | |
15dc95cb | 3079 | /* If the resulting operand is an empty statement, just return the omitted |
e057e0cd AP |
3080 | statement casted to void. */ |
3081 | if (IS_EMPTY_STMT (t) && TREE_SIDE_EFFECTS (omitted)) | |
c9019218 JJ |
3082 | return build1_loc (loc, NOP_EXPR, void_type_node, |
3083 | fold_ignored_result (omitted)); | |
e057e0cd | 3084 | |
6d716ca8 | 3085 | if (TREE_SIDE_EFFECTS (omitted)) |
c9019218 JJ |
3086 | return build2_loc (loc, COMPOUND_EXPR, type, |
3087 | fold_ignored_result (omitted), t); | |
db3927fb AH |
3088 | |
3089 | return non_lvalue_loc (loc, t); | |
6d716ca8 | 3090 | } |
4ab3cb65 RK |
3091 | |
3092 | /* Similar, but call pedantic_non_lvalue instead of non_lvalue. */ | |
3093 | ||
3094 | static tree | |
db3927fb AH |
3095 | pedantic_omit_one_operand_loc (location_t loc, tree type, tree result, |
3096 | tree omitted) | |
4ab3cb65 | 3097 | { |
db3927fb | 3098 | tree t = fold_convert_loc (loc, type, result); |
4ab3cb65 | 3099 | |
15dc95cb | 3100 | /* If the resulting operand is an empty statement, just return the omitted |
e057e0cd AP |
3101 | statement casted to void. */ |
3102 | if (IS_EMPTY_STMT (t) && TREE_SIDE_EFFECTS (omitted)) | |
c9019218 JJ |
3103 | return build1_loc (loc, NOP_EXPR, void_type_node, |
3104 | fold_ignored_result (omitted)); | |
e057e0cd | 3105 | |
4ab3cb65 | 3106 | if (TREE_SIDE_EFFECTS (omitted)) |
c9019218 JJ |
3107 | return build2_loc (loc, COMPOUND_EXPR, type, |
3108 | fold_ignored_result (omitted), t); | |
4ab3cb65 | 3109 | |
db3927fb | 3110 | return pedantic_non_lvalue_loc (loc, t); |
4ab3cb65 | 3111 | } |
08039bd8 RS |
3112 | |
3113 | /* Return a tree for the case when the result of an expression is RESULT | |
3114 | converted to TYPE and OMITTED1 and OMITTED2 were previously operands | |
3115 | of the expression but are now not needed. | |
3116 | ||
3117 | If OMITTED1 or OMITTED2 has side effects, they must be evaluated. | |
3118 | If both OMITTED1 and OMITTED2 have side effects, OMITTED1 is | |
3119 | evaluated before OMITTED2. Otherwise, if neither has side effects, | |
3120 | just do the conversion of RESULT to TYPE. */ | |
3121 | ||
3122 | tree | |
db3927fb | 3123 | omit_two_operands_loc (location_t loc, tree type, tree result, |
c9019218 | 3124 | tree omitted1, tree omitted2) |
08039bd8 | 3125 | { |
db3927fb | 3126 | tree t = fold_convert_loc (loc, type, result); |
08039bd8 RS |
3127 | |
3128 | if (TREE_SIDE_EFFECTS (omitted2)) | |
c9019218 | 3129 | t = build2_loc (loc, COMPOUND_EXPR, type, omitted2, t); |
08039bd8 | 3130 | if (TREE_SIDE_EFFECTS (omitted1)) |
c9019218 | 3131 | t = build2_loc (loc, COMPOUND_EXPR, type, omitted1, t); |
08039bd8 | 3132 | |
db3927fb | 3133 | return TREE_CODE (t) != COMPOUND_EXPR ? non_lvalue_loc (loc, t) : t; |
08039bd8 RS |
3134 | } |
3135 | ||
6d716ca8 | 3136 | \f |
3f783329 RS |
3137 | /* Return a simplified tree node for the truth-negation of ARG. This |
3138 | never alters ARG itself. We assume that ARG is an operation that | |
d1a7edaf | 3139 | returns a truth value (0 or 1). |
6d716ca8 | 3140 | |
d1a7edaf PB |
3141 | FIXME: one would think we would fold the result, but it causes |
3142 | problems with the dominator optimizer. */ | |
d817ed3b | 3143 | |
418d1b87 | 3144 | static tree |
db3927fb | 3145 | fold_truth_not_expr (location_t loc, tree arg) |
6d716ca8 | 3146 | { |
c9019218 | 3147 | tree type = TREE_TYPE (arg); |
c05a9b68 | 3148 | enum tree_code code = TREE_CODE (arg); |
db3927fb | 3149 | location_t loc1, loc2; |
6d716ca8 | 3150 | |
c05a9b68 RS |
3151 | /* If this is a comparison, we can simply invert it, except for |
3152 | floating-point non-equality comparisons, in which case we just | |
3153 | enclose a TRUTH_NOT_EXPR around what we have. */ | |
6d716ca8 | 3154 | |
6615c446 | 3155 | if (TREE_CODE_CLASS (code) == tcc_comparison) |
6d716ca8 | 3156 | { |
d1a7edaf PB |
3157 | tree op_type = TREE_TYPE (TREE_OPERAND (arg, 0)); |
3158 | if (FLOAT_TYPE_P (op_type) | |
3159 | && flag_trapping_math | |
3160 | && code != ORDERED_EXPR && code != UNORDERED_EXPR | |
3161 | && code != NE_EXPR && code != EQ_EXPR) | |
d817ed3b | 3162 | return NULL_TREE; |
ca80e52b EB |
3163 | |
3164 | code = invert_tree_comparison (code, HONOR_NANS (TYPE_MODE (op_type))); | |
3165 | if (code == ERROR_MARK) | |
3166 | return NULL_TREE; | |
3167 | ||
c9019218 JJ |
3168 | return build2_loc (loc, code, type, TREE_OPERAND (arg, 0), |
3169 | TREE_OPERAND (arg, 1)); | |
c05a9b68 | 3170 | } |
6d716ca8 | 3171 | |
c05a9b68 RS |
3172 | switch (code) |
3173 | { | |
6d716ca8 | 3174 | case INTEGER_CST: |
9ace7f9e | 3175 | return constant_boolean_node (integer_zerop (arg), type); |
6d716ca8 RS |
3176 | |
3177 | case TRUTH_AND_EXPR: | |
6c4e2997 NF |
3178 | loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc); |
3179 | loc2 = expr_location_or (TREE_OPERAND (arg, 1), loc); | |
c9019218 JJ |
3180 | return build2_loc (loc, TRUTH_OR_EXPR, type, |
3181 | invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)), | |
3182 | invert_truthvalue_loc (loc2, TREE_OPERAND (arg, 1))); | |
6d716ca8 RS |
3183 | |
3184 | case TRUTH_OR_EXPR: | |
6c4e2997 NF |
3185 | loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc); |
3186 | loc2 = expr_location_or (TREE_OPERAND (arg, 1), loc); | |
c9019218 JJ |
3187 | return build2_loc (loc, TRUTH_AND_EXPR, type, |
3188 | invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)), | |
3189 | invert_truthvalue_loc (loc2, TREE_OPERAND (arg, 1))); | |
6d716ca8 | 3190 | |
772447c5 RK |
3191 | case TRUTH_XOR_EXPR: |
3192 | /* Here we can invert either operand. We invert the first operand | |
3193 | unless the second operand is a TRUTH_NOT_EXPR in which case our | |
3194 | result is the XOR of the first operand with the inside of the | |
3195 | negation of the second operand. */ | |
3196 | ||
3197 | if (TREE_CODE (TREE_OPERAND (arg, 1)) == TRUTH_NOT_EXPR) | |
c9019218 JJ |
3198 | return build2_loc (loc, TRUTH_XOR_EXPR, type, TREE_OPERAND (arg, 0), |
3199 | TREE_OPERAND (TREE_OPERAND (arg, 1), 0)); | |
772447c5 | 3200 | else |
c9019218 JJ |
3201 | return build2_loc (loc, TRUTH_XOR_EXPR, type, |
3202 | invert_truthvalue_loc (loc, TREE_OPERAND (arg, 0)), | |
3203 | TREE_OPERAND (arg, 1)); | |
772447c5 | 3204 | |
6d716ca8 | 3205 | case TRUTH_ANDIF_EXPR: |
6c4e2997 NF |
3206 | loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc); |
3207 | loc2 = expr_location_or (TREE_OPERAND (arg, 1), loc); | |
c9019218 JJ |
3208 | return build2_loc (loc, TRUTH_ORIF_EXPR, type, |
3209 | invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)), | |
3210 | invert_truthvalue_loc (loc2, TREE_OPERAND (arg, 1))); | |
6d716ca8 RS |
3211 | |
3212 | case TRUTH_ORIF_EXPR: | |
6c4e2997 NF |
3213 | loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc); |
3214 | loc2 = expr_location_or (TREE_OPERAND (arg, 1), loc); | |
c9019218 JJ |
3215 | return build2_loc (loc, TRUTH_ANDIF_EXPR, type, |
3216 | invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)), | |
3217 | invert_truthvalue_loc (loc2, TREE_OPERAND (arg, 1))); | |
6d716ca8 RS |
3218 | |
3219 | case TRUTH_NOT_EXPR: | |
3220 | return TREE_OPERAND (arg, 0); | |
3221 | ||
3222 | case COND_EXPR: | |
9ca4afb9 RG |
3223 | { |
3224 | tree arg1 = TREE_OPERAND (arg, 1); | |
3225 | tree arg2 = TREE_OPERAND (arg, 2); | |
db3927fb | 3226 | |
6c4e2997 NF |
3227 | loc1 = expr_location_or (TREE_OPERAND (arg, 1), loc); |
3228 | loc2 = expr_location_or (TREE_OPERAND (arg, 2), loc); | |
db3927fb | 3229 | |
9ca4afb9 RG |
3230 | /* A COND_EXPR may have a throw as one operand, which |
3231 | then has void type. Just leave void operands | |
3232 | as they are. */ | |
c9019218 JJ |
3233 | return build3_loc (loc, COND_EXPR, type, TREE_OPERAND (arg, 0), |
3234 | VOID_TYPE_P (TREE_TYPE (arg1)) | |
3235 | ? arg1 : invert_truthvalue_loc (loc1, arg1), | |
3236 | VOID_TYPE_P (TREE_TYPE (arg2)) | |
3237 | ? arg2 : invert_truthvalue_loc (loc2, arg2)); | |
9ca4afb9 | 3238 | } |
6d716ca8 | 3239 | |
ef9fe0da | 3240 | case COMPOUND_EXPR: |
6c4e2997 | 3241 | loc1 = expr_location_or (TREE_OPERAND (arg, 1), loc); |
c9019218 JJ |
3242 | return build2_loc (loc, COMPOUND_EXPR, type, |
3243 | TREE_OPERAND (arg, 0), | |
3244 | invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 1))); | |
ef9fe0da | 3245 | |
6d716ca8 | 3246 | case NON_LVALUE_EXPR: |
6c4e2997 | 3247 | loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc); |
db3927fb | 3248 | return invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)); |
6d716ca8 | 3249 | |
84fb43a1 | 3250 | CASE_CONVERT: |
6de9cd9a | 3251 | if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE) |
c9019218 | 3252 | return build1_loc (loc, TRUTH_NOT_EXPR, type, arg); |
ca80e52b EB |
3253 | |
3254 | /* ... fall through ... */ | |
6de9cd9a | 3255 | |
6d716ca8 | 3256 | case FLOAT_EXPR: |
6c4e2997 | 3257 | loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc); |
c9019218 JJ |
3258 | return build1_loc (loc, TREE_CODE (arg), type, |
3259 | invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0))); | |
6d716ca8 RS |
3260 | |
3261 | case BIT_AND_EXPR: | |
efc1a4d9 | 3262 | if (!integer_onep (TREE_OPERAND (arg, 1))) |
ca80e52b | 3263 | return NULL_TREE; |
c9019218 | 3264 | return build2_loc (loc, EQ_EXPR, type, arg, build_int_cst (type, 0)); |
6d716ca8 | 3265 | |
dfa90b42 | 3266 | case SAVE_EXPR: |
c9019218 | 3267 | return build1_loc (loc, TRUTH_NOT_EXPR, type, arg); |
a25ee332 RK |
3268 | |
3269 | case CLEANUP_POINT_EXPR: | |
6c4e2997 | 3270 | loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc); |
c9019218 JJ |
3271 | return build1_loc (loc, CLEANUP_POINT_EXPR, type, |
3272 | invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0))); | |
e9a25f70 JL |
3273 | |
3274 | default: | |
c9019218 | 3275 | return NULL_TREE; |
efc1a4d9 | 3276 | } |
d817ed3b RG |
3277 | } |
3278 | ||
418d1b87 MG |
3279 | /* Fold the truth-negation of ARG. This never alters ARG itself. We |
3280 | assume that ARG is an operation that returns a truth value (0 or 1 | |
3281 | for scalars, 0 or -1 for vectors). Return the folded expression if | |
3282 | folding is successful. Otherwise, return NULL_TREE. */ | |
3283 | ||
3284 | static tree | |
3285 | fold_invert_truthvalue (location_t loc, tree arg) | |
3286 | { | |
3287 | tree type = TREE_TYPE (arg); | |
3288 | return fold_unary_loc (loc, VECTOR_TYPE_P (type) | |
3289 | ? BIT_NOT_EXPR | |
3290 | : TRUTH_NOT_EXPR, | |
3291 | type, arg); | |
3292 | } | |
3293 | ||
d817ed3b RG |
3294 | /* Return a simplified tree node for the truth-negation of ARG. This |
3295 | never alters ARG itself. We assume that ARG is an operation that | |
418d1b87 | 3296 | returns a truth value (0 or 1 for scalars, 0 or -1 for vectors). */ |
d817ed3b RG |
3297 | |
3298 | tree | |
db3927fb | 3299 | invert_truthvalue_loc (location_t loc, tree arg) |
d817ed3b | 3300 | { |
d817ed3b RG |
3301 | if (TREE_CODE (arg) == ERROR_MARK) |
3302 | return arg; | |
3303 | ||
418d1b87 MG |
3304 | tree type = TREE_TYPE (arg); |
3305 | return fold_build1_loc (loc, VECTOR_TYPE_P (type) | |
3306 | ? BIT_NOT_EXPR | |
3307 | : TRUTH_NOT_EXPR, | |
3308 | type, arg); | |
6d716ca8 RS |
3309 | } |
3310 | ||
3311 | /* Given a bit-wise operation CODE applied to ARG0 and ARG1, see if both | |
3312 | operands are another bit-wise operation with a common input. If so, | |
3313 | distribute the bit operations to save an operation and possibly two if | |
3314 | constants are involved. For example, convert | |
fa8db1f7 | 3315 | (A | B) & (A | C) into A | (B & C) |
6d716ca8 RS |
3316 | Further simplification will occur if B and C are constants. |
3317 | ||
3318 | If this optimization cannot be done, 0 will be returned. */ | |
3319 | ||
3320 | static tree | |
db3927fb AH |
3321 | distribute_bit_expr (location_t loc, enum tree_code code, tree type, |
3322 | tree arg0, tree arg1) | |
6d716ca8 RS |
3323 | { |
3324 | tree common; | |
3325 | tree left, right; | |
3326 | ||
3327 | if (TREE_CODE (arg0) != TREE_CODE (arg1) | |
3328 | || TREE_CODE (arg0) == code | |
fced8ba3 RS |
3329 | || (TREE_CODE (arg0) != BIT_AND_EXPR |
3330 | && TREE_CODE (arg0) != BIT_IOR_EXPR)) | |
6d716ca8 RS |
3331 | return 0; |
3332 | ||
3333 | if (operand_equal_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg1, 0), 0)) | |
3334 | { | |
3335 | common = TREE_OPERAND (arg0, 0); | |
3336 | left = TREE_OPERAND (arg0, 1); | |
3337 | right = TREE_OPERAND (arg1, 1); | |
3338 | } | |
3339 | else if (operand_equal_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg1, 1), 0)) | |
3340 | { | |
3341 | common = TREE_OPERAND (arg0, 0); | |
3342 | left = TREE_OPERAND (arg0, 1); | |
3343 | right = TREE_OPERAND (arg1, 0); | |
3344 | } | |
3345 | else if (operand_equal_p (TREE_OPERAND (arg0, 1), TREE_OPERAND (arg1, 0), 0)) | |
3346 | { | |
3347 | common = TREE_OPERAND (arg0, 1); | |
3348 | left = TREE_OPERAND (arg0, 0); | |
3349 | right = TREE_OPERAND (arg1, 1); | |
3350 | } | |
3351 | else if (operand_equal_p (TREE_OPERAND (arg0, 1), TREE_OPERAND (arg1, 1), 0)) | |
3352 | { | |
3353 | common = TREE_OPERAND (arg0, 1); | |
3354 | left = TREE_OPERAND (arg0, 0); | |
3355 | right = TREE_OPERAND (arg1, 0); | |
3356 | } | |
3357 | else | |
3358 | return 0; | |
3359 | ||
db3927fb AH |
3360 | common = fold_convert_loc (loc, type, common); |
3361 | left = fold_convert_loc (loc, type, left); | |
3362 | right = fold_convert_loc (loc, type, right); | |
3363 | return fold_build2_loc (loc, TREE_CODE (arg0), type, common, | |
3364 | fold_build2_loc (loc, code, type, left, right)); | |
6d716ca8 | 3365 | } |
f8912a55 PB |
3366 | |
3367 | /* Knowing that ARG0 and ARG1 are both RDIV_EXPRs, simplify a binary operation | |
3368 | with code CODE. This optimization is unsafe. */ | |
3369 | static tree | |
db3927fb AH |
3370 | distribute_real_division (location_t loc, enum tree_code code, tree type, |
3371 | tree arg0, tree arg1) | |
f8912a55 PB |
3372 | { |
3373 | bool mul0 = TREE_CODE (arg0) == MULT_EXPR; | |
3374 | bool mul1 = TREE_CODE (arg1) == MULT_EXPR; | |
3375 | ||
3376 | /* (A / C) +- (B / C) -> (A +- B) / C. */ | |
3377 | if (mul0 == mul1 | |
3378 | && operand_equal_p (TREE_OPERAND (arg0, 1), | |
3379 | TREE_OPERAND (arg1, 1), 0)) | |
db3927fb AH |
3380 | return fold_build2_loc (loc, mul0 ? MULT_EXPR : RDIV_EXPR, type, |
3381 | fold_build2_loc (loc, code, type, | |
f8912a55 PB |
3382 | TREE_OPERAND (arg0, 0), |
3383 | TREE_OPERAND (arg1, 0)), | |
3384 | TREE_OPERAND (arg0, 1)); | |
3385 | ||
3386 | /* (A / C1) +- (A / C2) -> A * (1 / C1 +- 1 / C2). */ | |
3387 | if (operand_equal_p (TREE_OPERAND (arg0, 0), | |
3388 | TREE_OPERAND (arg1, 0), 0) | |
3389 | && TREE_CODE (TREE_OPERAND (arg0, 1)) == REAL_CST | |
3390 | && TREE_CODE (TREE_OPERAND (arg1, 1)) == REAL_CST) | |
3391 | { | |
3392 | REAL_VALUE_TYPE r0, r1; | |
3393 | r0 = TREE_REAL_CST (TREE_OPERAND (arg0, 1)); | |
3394 | r1 = TREE_REAL_CST (TREE_OPERAND (arg1, 1)); | |
3395 | if (!mul0) | |
3396 | real_arithmetic (&r0, RDIV_EXPR, &dconst1, &r0); | |
3397 | if (!mul1) | |
3398 | real_arithmetic (&r1, RDIV_EXPR, &dconst1, &r1); | |
3399 | real_arithmetic (&r0, code, &r0, &r1); | |
db3927fb | 3400 | return fold_build2_loc (loc, MULT_EXPR, type, |
f8912a55 PB |
3401 | TREE_OPERAND (arg0, 0), |
3402 | build_real (type, r0)); | |
3403 | } | |
3404 | ||
3405 | return NULL_TREE; | |
3406 | } | |
6d716ca8 | 3407 | \f |
45dc13b9 JJ |
3408 | /* Return a BIT_FIELD_REF of type TYPE to refer to BITSIZE bits of INNER |
3409 | starting at BITPOS. The field is unsigned if UNSIGNEDP is nonzero. */ | |
3410 | ||
3411 | static tree | |
db3927fb AH |
3412 | make_bit_field_ref (location_t loc, tree inner, tree type, |
3413 | HOST_WIDE_INT bitsize, HOST_WIDE_INT bitpos, int unsignedp) | |
45dc13b9 JJ |
3414 | { |
3415 | tree result, bftype; | |
3416 | ||
3417 | if (bitpos == 0) | |
3418 | { | |
3419 | tree size = TYPE_SIZE (TREE_TYPE (inner)); | |
3420 | if ((INTEGRAL_TYPE_P (TREE_TYPE (inner)) | |
3421 | || POINTER_TYPE_P (TREE_TYPE (inner))) | |
b8698a0f | 3422 | && host_integerp (size, 0) |
45dc13b9 | 3423 | && tree_low_cst (size, 0) == bitsize) |
db3927fb | 3424 | return fold_convert_loc (loc, type, inner); |
45dc13b9 JJ |
3425 | } |
3426 | ||
3427 | bftype = type; | |
3428 | if (TYPE_PRECISION (bftype) != bitsize | |
3429 | || TYPE_UNSIGNED (bftype) == !unsignedp) | |
3430 | bftype = build_nonstandard_integer_type (bitsize, 0); | |
3431 | ||
c9019218 JJ |
3432 | result = build3_loc (loc, BIT_FIELD_REF, bftype, inner, |
3433 | size_int (bitsize), bitsize_int (bitpos)); | |
45dc13b9 JJ |
3434 | |
3435 | if (bftype != type) | |
db3927fb | 3436 | result = fold_convert_loc (loc, type, result); |
45dc13b9 JJ |
3437 | |
3438 | return result; | |
3439 | } | |
3440 | ||
3441 | /* Optimize a bit-field compare. | |
3442 | ||
3443 | There are two cases: First is a compare against a constant and the | |
3444 | second is a comparison of two items where the fields are at the same | |
3445 | bit position relative to the start of a chunk (byte, halfword, word) | |
3446 | large enough to contain it. In these cases we can avoid the shift | |
3447 | implicit in bitfield extractions. | |
3448 | ||
3449 | For constants, we emit a compare of the shifted constant with the | |
3450 | BIT_AND_EXPR of a mask and a byte, halfword, or word of the operand being | |
3451 | compared. For two fields at the same position, we do the ANDs with the | |
3452 | similar mask and compare the result of the ANDs. | |
3453 | ||
3454 | CODE is the comparison code, known to be either NE_EXPR or EQ_EXPR. | |
3455 | COMPARE_TYPE is the type of the comparison, and LHS and RHS | |
3456 | are the left and right operands of the comparison, respectively. | |
3457 | ||
3458 | If the optimization described above can be done, we return the resulting | |
3459 | tree. Otherwise we return zero. */ | |
3460 | ||
3461 | static tree | |
db3927fb AH |
3462 | optimize_bit_field_compare (location_t loc, enum tree_code code, |
3463 | tree compare_type, tree lhs, tree rhs) | |
45dc13b9 JJ |
3464 | { |
3465 | HOST_WIDE_INT lbitpos, lbitsize, rbitpos, rbitsize, nbitpos, nbitsize; | |
3466 | tree type = TREE_TYPE (lhs); | |
3467 | tree signed_type, unsigned_type; | |
3468 | int const_p = TREE_CODE (rhs) == INTEGER_CST; | |
3469 | enum machine_mode lmode, rmode, nmode; | |
3470 | int lunsignedp, runsignedp; | |
3471 | int lvolatilep = 0, rvolatilep = 0; | |
3472 | tree linner, rinner = NULL_TREE; | |
3473 | tree mask; | |
3474 | tree offset; | |
3475 | ||
3476 | /* Get all the information about the extractions being done. If the bit size | |
3477 | if the same as the size of the underlying object, we aren't doing an | |
3478 | extraction at all and so can do nothing. We also don't want to | |
3479 | do anything if the inner expression is a PLACEHOLDER_EXPR since we | |
3480 | then will no longer be able to replace it. */ | |
3481 | linner = get_inner_reference (lhs, &lbitsize, &lbitpos, &offset, &lmode, | |
3482 | &lunsignedp, &lvolatilep, false); | |
3483 | if (linner == lhs || lbitsize == GET_MODE_BITSIZE (lmode) || lbitsize < 0 | |
0cad6830 | 3484 | || offset != 0 || TREE_CODE (linner) == PLACEHOLDER_EXPR || lvolatilep) |
45dc13b9 JJ |
3485 | return 0; |
3486 | ||
3487 | if (!const_p) | |
3488 | { | |
3489 | /* If this is not a constant, we can only do something if bit positions, | |
3490 | sizes, and signedness are the same. */ | |
3491 | rinner = get_inner_reference (rhs, &rbitsize, &rbitpos, &offset, &rmode, | |
3492 | &runsignedp, &rvolatilep, false); | |
3493 | ||
3494 | if (rinner == rhs || lbitpos != rbitpos || lbitsize != rbitsize | |
3495 | || lunsignedp != runsignedp || offset != 0 | |
0cad6830 | 3496 | || TREE_CODE (rinner) == PLACEHOLDER_EXPR || rvolatilep) |
45dc13b9 JJ |
3497 | return 0; |
3498 | } | |
3499 | ||
3500 | /* See if we can find a mode to refer to this field. We should be able to, | |
3501 | but fail if we can't. */ | |
0cad6830 BE |
3502 | nmode = get_best_mode (lbitsize, lbitpos, 0, 0, |
3503 | const_p ? TYPE_ALIGN (TREE_TYPE (linner)) | |
3504 | : MIN (TYPE_ALIGN (TREE_TYPE (linner)), | |
3505 | TYPE_ALIGN (TREE_TYPE (rinner))), | |
3506 | word_mode, false); | |
45dc13b9 JJ |
3507 | if (nmode == VOIDmode) |
3508 | return 0; | |
3509 | ||
3510 | /* Set signed and unsigned types of the precision of this mode for the | |
3511 | shifts below. */ | |
3512 | signed_type = lang_hooks.types.type_for_mode (nmode, 0); | |
3513 | unsigned_type = lang_hooks.types.type_for_mode (nmode, 1); | |
3514 | ||
3515 | /* Compute the bit position and size for the new reference and our offset | |
3516 | within it. If the new reference is the same size as the original, we | |
3517 | won't optimize anything, so return zero. */ | |
3518 | nbitsize = GET_MODE_BITSIZE (nmode); | |
3519 | nbitpos = lbitpos & ~ (nbitsize - 1); | |
3520 | lbitpos -= nbitpos; | |
3521 | if (nbitsize == lbitsize) | |
3522 | return 0; | |
3523 | ||
3524 | if (BYTES_BIG_ENDIAN) | |
3525 | lbitpos = nbitsize - lbitsize - lbitpos; | |
3526 | ||
3527 | /* Make the mask to be used against the extracted field. */ | |
3528 | mask = build_int_cst_type (unsigned_type, -1); | |
43a5d30b | 3529 | mask = const_binop (LSHIFT_EXPR, mask, size_int (nbitsize - lbitsize)); |
45dc13b9 | 3530 | mask = const_binop (RSHIFT_EXPR, mask, |
43a5d30b | 3531 | size_int (nbitsize - lbitsize - lbitpos)); |
45dc13b9 JJ |
3532 | |
3533 | if (! const_p) | |
3534 | /* If not comparing with constant, just rework the comparison | |
3535 | and return. */ | |
db3927fb AH |
3536 | return fold_build2_loc (loc, code, compare_type, |
3537 | fold_build2_loc (loc, BIT_AND_EXPR, unsigned_type, | |
3538 | make_bit_field_ref (loc, linner, | |
45dc13b9 JJ |
3539 | unsigned_type, |
3540 | nbitsize, nbitpos, | |
3541 | 1), | |
3542 | mask), | |
db3927fb AH |
3543 | fold_build2_loc (loc, BIT_AND_EXPR, unsigned_type, |
3544 | make_bit_field_ref (loc, rinner, | |
45dc13b9 JJ |
3545 | unsigned_type, |
3546 | nbitsize, nbitpos, | |
3547 | 1), | |
3548 | mask)); | |
3549 | ||
3550 | /* Otherwise, we are handling the constant case. See if the constant is too | |
3551 | big for the field. Warn and return a tree of for 0 (false) if so. We do | |
3552 | this not only for its own sake, but to avoid having to test for this | |
3553 | error case below. If we didn't, we might generate wrong code. | |
3554 | ||
3555 | For unsigned fields, the constant shifted right by the field length should | |
3556 | be all zero. For signed fields, the high-order bits should agree with | |
3557 | the sign bit. */ | |
3558 | ||
3559 | if (lunsignedp) | |
3560 | { | |
3561 | if (! integer_zerop (const_binop (RSHIFT_EXPR, | |
db3927fb AH |
3562 | fold_convert_loc (loc, |
3563 | unsigned_type, rhs), | |
43a5d30b | 3564 | size_int (lbitsize)))) |
45dc13b9 JJ |
3565 | { |
3566 | warning (0, "comparison is always %d due to width of bit-field", | |
3567 | code == NE_EXPR); | |
3568 | return constant_boolean_node (code == NE_EXPR, compare_type); | |
3569 | } | |
3570 | } | |
3571 | else | |
3572 | { | |
db3927fb AH |
3573 | tree tem = const_binop (RSHIFT_EXPR, |
3574 | fold_convert_loc (loc, signed_type, rhs), | |
43a5d30b | 3575 | size_int (lbitsize - 1)); |
45dc13b9 JJ |
3576 | if (! integer_zerop (tem) && ! integer_all_onesp (tem)) |
3577 | { | |
3578 | warning (0, "comparison is always %d due to width of bit-field", | |
3579 | code == NE_EXPR); | |
3580 | return constant_boolean_node (code == NE_EXPR, compare_type); | |
3581 | } | |
3582 | } | |
3583 | ||
3584 | /* Single-bit compares should always be against zero. */ | |
3585 | if (lbitsize == 1 && ! integer_zerop (rhs)) | |
3586 | { | |
3587 | code = code == EQ_EXPR ? NE_EXPR : EQ_EXPR; | |
3588 | rhs = build_int_cst (type, 0); | |
3589 | } | |
3590 | ||
3591 | /* Make a new bitfield reference, shift the constant over the | |
3592 | appropriate number of bits and mask it with the computed mask | |
3593 | (in case this was a signed field). If we changed it, make a new one. */ | |
db3927fb | 3594 | lhs = make_bit_field_ref (loc, linner, unsigned_type, nbitsize, nbitpos, 1); |
45dc13b9 JJ |
3595 | |
3596 | rhs = const_binop (BIT_AND_EXPR, | |
3597 | const_binop (LSHIFT_EXPR, | |
db3927fb | 3598 | fold_convert_loc (loc, unsigned_type, rhs), |
43a5d30b AS |
3599 | size_int (lbitpos)), |
3600 | mask); | |
45dc13b9 | 3601 | |
c9019218 JJ |
3602 | lhs = build2_loc (loc, code, compare_type, |
3603 | build2 (BIT_AND_EXPR, unsigned_type, lhs, mask), rhs); | |
db3927fb | 3604 | return lhs; |
45dc13b9 JJ |
3605 | } |
3606 | \f | |
6e796a83 | 3607 | /* Subroutine for fold_truth_andor_1: decode a field reference. |
6d716ca8 RS |
3608 | |
3609 | If EXP is a comparison reference, we return the innermost reference. | |
3610 | ||
3611 | *PBITSIZE is set to the number of bits in the reference, *PBITPOS is | |
3612 | set to the starting bit number. | |
3613 | ||
3614 | If the innermost field can be completely contained in a mode-sized | |
3615 | unit, *PMODE is set to that mode. Otherwise, it is set to VOIDmode. | |
3616 | ||
3617 | *PVOLATILEP is set to 1 if the any expression encountered is volatile; | |
3618 | otherwise it is not changed. | |
3619 | ||
3620 | *PUNSIGNEDP is set to the signedness of the field. | |
3621 | ||
3622 | *PMASK is set to the mask used. This is either contained in a | |
3623 | BIT_AND_EXPR or derived from the width of the field. | |
3624 | ||
38e01259 | 3625 | *PAND_MASK is set to the mask found in a BIT_AND_EXPR, if any. |
d4453ee5 | 3626 | |
6d716ca8 RS |
3627 | Return 0 if this is not a component reference or is one that we can't |
3628 | do anything with. */ | |
3629 | ||
3630 | static tree | |
db3927fb | 3631 | decode_field_reference (location_t loc, tree exp, HOST_WIDE_INT *pbitsize, |
75040a04 AJ |
3632 | HOST_WIDE_INT *pbitpos, enum machine_mode *pmode, |
3633 | int *punsignedp, int *pvolatilep, | |
fa8db1f7 | 3634 | tree *pmask, tree *pand_mask) |
6d716ca8 | 3635 | { |
1a8c4ca6 | 3636 | tree outer_type = 0; |
6d9f1f5f RK |
3637 | tree and_mask = 0; |
3638 | tree mask, inner, offset; | |
3639 | tree unsigned_type; | |
770ae6cc | 3640 | unsigned int precision; |
6d716ca8 | 3641 | |
b6cc0a72 | 3642 | /* All the optimizations using this function assume integer fields. |
772ae9f0 RK |
3643 | There are problems with FP fields since the type_for_size call |
3644 | below can fail for, e.g., XFmode. */ | |
3645 | if (! INTEGRAL_TYPE_P (TREE_TYPE (exp))) | |
3646 | return 0; | |
3647 | ||
1a8c4ca6 EB |
3648 | /* We are interested in the bare arrangement of bits, so strip everything |
3649 | that doesn't affect the machine mode. However, record the type of the | |
3650 | outermost expression if it may matter below. */ | |
1043771b | 3651 | if (CONVERT_EXPR_P (exp) |
1a8c4ca6 EB |
3652 | || TREE_CODE (exp) == NON_LVALUE_EXPR) |
3653 | outer_type = TREE_TYPE (exp); | |
df7fb8f9 | 3654 | STRIP_NOPS (exp); |
6d716ca8 RS |
3655 | |
3656 | if (TREE_CODE (exp) == BIT_AND_EXPR) | |
3657 | { | |
6d9f1f5f | 3658 | and_mask = TREE_OPERAND (exp, 1); |
6d716ca8 | 3659 | exp = TREE_OPERAND (exp, 0); |
6d9f1f5f RK |
3660 | STRIP_NOPS (exp); STRIP_NOPS (and_mask); |
3661 | if (TREE_CODE (and_mask) != INTEGER_CST) | |
6d716ca8 RS |
3662 | return 0; |
3663 | } | |
3664 | ||
f1e60ec6 | 3665 | inner = get_inner_reference (exp, pbitsize, pbitpos, &offset, pmode, |
2614034e | 3666 | punsignedp, pvolatilep, false); |
02103577 | 3667 | if ((inner == exp && and_mask == 0) |
14a774a9 RK |
3668 | || *pbitsize < 0 || offset != 0 |
3669 | || TREE_CODE (inner) == PLACEHOLDER_EXPR) | |
c05a9b68 | 3670 | return 0; |
b6cc0a72 | 3671 | |
1a8c4ca6 EB |
3672 | /* If the number of bits in the reference is the same as the bitsize of |
3673 | the outer type, then the outer type gives the signedness. Otherwise | |
3674 | (in case of a small bitfield) the signedness is unchanged. */ | |
fae1b38d | 3675 | if (outer_type && *pbitsize == TYPE_PRECISION (outer_type)) |
8df83eae | 3676 | *punsignedp = TYPE_UNSIGNED (outer_type); |
1a8c4ca6 | 3677 | |
6d9f1f5f | 3678 | /* Compute the mask to access the bitfield. */ |
5785c7de | 3679 | unsigned_type = lang_hooks.types.type_for_size (*pbitsize, 1); |
6d9f1f5f RK |
3680 | precision = TYPE_PRECISION (unsigned_type); |
3681 | ||
2ac7cbb5 | 3682 | mask = build_int_cst_type (unsigned_type, -1); |
3e6688a7 | 3683 | |
43a5d30b AS |
3684 | mask = const_binop (LSHIFT_EXPR, mask, size_int (precision - *pbitsize)); |
3685 | mask = const_binop (RSHIFT_EXPR, mask, size_int (precision - *pbitsize)); | |
6d9f1f5f RK |
3686 | |
3687 | /* Merge it with the mask we found in the BIT_AND_EXPR, if any. */ | |
3688 | if (and_mask != 0) | |
db3927fb AH |
3689 | mask = fold_build2_loc (loc, BIT_AND_EXPR, unsigned_type, |
3690 | fold_convert_loc (loc, unsigned_type, and_mask), mask); | |
6d716ca8 RS |
3691 | |
3692 | *pmask = mask; | |
d4453ee5 | 3693 | *pand_mask = and_mask; |
6d716ca8 RS |
3694 | return inner; |
3695 | } | |
3696 | ||
45dc13b9 JJ |
3697 | /* Return nonzero if MASK represents a mask of SIZE ones in the low-order |
3698 | bit positions. */ | |
3699 | ||
3700 | static int | |
3701 | all_ones_mask_p (const_tree mask, int size) | |
3702 | { | |
3703 | tree type = TREE_TYPE (mask); | |
3704 | unsigned int precision = TYPE_PRECISION (type); | |
3705 | tree tmask; | |
3706 | ||
3707 | tmask = build_int_cst_type (signed_type_for (type), -1); | |
3708 | ||
3709 | return | |
3710 | tree_int_cst_equal (mask, | |
3711 | const_binop (RSHIFT_EXPR, | |
3712 | const_binop (LSHIFT_EXPR, tmask, | |
43a5d30b AS |
3713 | size_int (precision - size)), |
3714 | size_int (precision - size))); | |
45dc13b9 JJ |
3715 | } |
3716 | ||
1f77b5da RS |
3717 | /* Subroutine for fold: determine if VAL is the INTEGER_CONST that |
3718 | represents the sign bit of EXP's type. If EXP represents a sign | |
3719 | or zero extension, also test VAL against the unextended type. | |
3720 | The return value is the (sub)expression whose sign bit is VAL, | |
3721 | or NULL_TREE otherwise. */ | |
3722 | ||
3723 | static tree | |
ac545c64 | 3724 | sign_bit_p (tree exp, const_tree val) |
1f77b5da | 3725 | { |
c87d821b KH |
3726 | unsigned HOST_WIDE_INT mask_lo, lo; |
3727 | HOST_WIDE_INT mask_hi, hi; | |
1f77b5da RS |
3728 | int width; |
3729 | tree t; | |
3730 | ||
68e82b83 | 3731 | /* Tree EXP must have an integral type. */ |
1f77b5da RS |
3732 | t = TREE_TYPE (exp); |
3733 | if (! INTEGRAL_TYPE_P (t)) | |
3734 | return NULL_TREE; | |
3735 | ||
3736 | /* Tree VAL must be an integer constant. */ | |
3737 | if (TREE_CODE (val) != INTEGER_CST | |
455f14dd | 3738 | || TREE_OVERFLOW (val)) |
1f77b5da RS |
3739 | return NULL_TREE; |
3740 | ||
3741 | width = TYPE_PRECISION (t); | |
3742 | if (width > HOST_BITS_PER_WIDE_INT) | |
3743 | { | |
3744 | hi = (unsigned HOST_WIDE_INT) 1 << (width - HOST_BITS_PER_WIDE_INT - 1); | |
3745 | lo = 0; | |
c87d821b | 3746 | |
0cadbfaa | 3747 | mask_hi = (HOST_WIDE_INT_M1U >> (HOST_BITS_PER_DOUBLE_INT - width)); |
c87d821b | 3748 | mask_lo = -1; |
1f77b5da RS |
3749 | } |
3750 | else | |
3751 | { | |
3752 | hi = 0; | |
3753 | lo = (unsigned HOST_WIDE_INT) 1 << (width - 1); | |
c87d821b KH |
3754 | |
3755 | mask_hi = 0; | |
0cadbfaa | 3756 | mask_lo = (HOST_WIDE_INT_M1U >> (HOST_BITS_PER_WIDE_INT - width)); |
1f77b5da RS |
3757 | } |
3758 | ||
c87d821b KH |
3759 | /* We mask off those bits beyond TREE_TYPE (exp) so that we can |
3760 | treat VAL as if it were unsigned. */ | |
3761 | if ((TREE_INT_CST_HIGH (val) & mask_hi) == hi | |
3762 | && (TREE_INT_CST_LOW (val) & mask_lo) == lo) | |
1f77b5da RS |
3763 | return exp; |
3764 | ||
3765 | /* Handle extension from a narrower type. */ | |
3766 | if (TREE_CODE (exp) == NOP_EXPR | |
3767 | && TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (exp, 0))) < width) | |
3768 | return sign_bit_p (TREE_OPERAND (exp, 0), val); | |
3769 | ||
3770 | return NULL_TREE; | |
3771 | } | |
3772 | ||
6e796a83 | 3773 | /* Subroutine for fold_truth_andor_1: determine if an operand is simple enough |
b2215d83 TW |
3774 | to be evaluated unconditionally. */ |
3775 | ||
b6cc0a72 | 3776 | static int |
ac545c64 | 3777 | simple_operand_p (const_tree exp) |
b2215d83 TW |
3778 | { |
3779 | /* Strip any conversions that don't change the machine mode. */ | |
1d481ba8 | 3780 | STRIP_NOPS (exp); |
b2215d83 | 3781 | |
6615c446 | 3782 | return (CONSTANT_CLASS_P (exp) |
6e796a83 | 3783 | || TREE_CODE (exp) == SSA_NAME |
2f939d94 | 3784 | || (DECL_P (exp) |
b2215d83 TW |
3785 | && ! TREE_ADDRESSABLE (exp) |
3786 | && ! TREE_THIS_VOLATILE (exp) | |
8227896c TW |
3787 | && ! DECL_NONLOCAL (exp) |
3788 | /* Don't regard global variables as simple. They may be | |
3789 | allocated in ways unknown to the compiler (shared memory, | |
3790 | #pragma weak, etc). */ | |
3791 | && ! TREE_PUBLIC (exp) | |
3792 | && ! DECL_EXTERNAL (exp) | |
08346abd JH |
3793 | /* Weakrefs are not safe to be read, since they can be NULL. |
3794 | They are !TREE_PUBLIC && !DECL_EXTERNAL but still | |
3795 | have DECL_WEAK flag set. */ | |
3796 | && (! VAR_OR_FUNCTION_DECL_P (exp) || ! DECL_WEAK (exp)) | |
8227896c TW |
3797 | /* Loading a static variable is unduly expensive, but global |
3798 | registers aren't expensive. */ | |
3799 | && (! TREE_STATIC (exp) || DECL_REGISTER (exp)))); | |
b2215d83 | 3800 | } |
6e796a83 KT |
3801 | |
3802 | /* Subroutine for fold_truth_andor: determine if an operand is simple enough | |
3803 | to be evaluated unconditionally. | |
bb35fdd0 KT |
3804 | I addition to simple_operand_p, we assume that comparisons, conversions, |
3805 | and logic-not operations are simple, if their operands are simple, too. */ | |
6e796a83 KT |
3806 | |
3807 | static bool | |
3808 | simple_operand_p_2 (tree exp) | |
3809 | { | |
3810 | enum tree_code code; | |
3811 | ||
6e796a83 KT |
3812 | if (TREE_SIDE_EFFECTS (exp) |
3813 | || tree_could_trap_p (exp)) | |
3814 | return false; | |
3815 | ||
bb35fdd0 KT |
3816 | while (CONVERT_EXPR_P (exp)) |
3817 | exp = TREE_OPERAND (exp, 0); | |
3818 | ||
3819 | code = TREE_CODE (exp); | |
3820 | ||
6e796a83 KT |
3821 | if (TREE_CODE_CLASS (code) == tcc_comparison) |
3822 | return (simple_operand_p (TREE_OPERAND (exp, 0)) | |
3823 | && simple_operand_p (TREE_OPERAND (exp, 1))); | |
3824 | ||
3825 | if (code == TRUTH_NOT_EXPR) | |
3826 | return simple_operand_p_2 (TREE_OPERAND (exp, 0)); | |
3827 | ||
3828 | return simple_operand_p (exp); | |
3829 | } | |
3830 | ||
6d716ca8 | 3831 | \f |
ebde8a27 RK |
3832 | /* The following functions are subroutines to fold_range_test and allow it to |
3833 | try to change a logical combination of comparisons into a range test. | |
3834 | ||
3835 | For example, both | |
fa8db1f7 | 3836 | X == 2 || X == 3 || X == 4 || X == 5 |
ebde8a27 | 3837 | and |
fa8db1f7 | 3838 | X >= 2 && X <= 5 |
ebde8a27 RK |
3839 | are converted to |
3840 | (unsigned) (X - 2) <= 3 | |
3841 | ||
956d6950 | 3842 | We describe each set of comparisons as being either inside or outside |
ebde8a27 RK |
3843 | a range, using a variable named like IN_P, and then describe the |
3844 | range with a lower and upper bound. If one of the bounds is omitted, | |
3845 | it represents either the highest or lowest value of the type. | |
3846 | ||
3847 | In the comments below, we represent a range by two numbers in brackets | |
956d6950 | 3848 | preceded by a "+" to designate being inside that range, or a "-" to |
ebde8a27 RK |
3849 | designate being outside that range, so the condition can be inverted by |
3850 | flipping the prefix. An omitted bound is represented by a "-". For | |
3851 | example, "- [-, 10]" means being outside the range starting at the lowest | |
3852 | possible value and ending at 10, in other words, being greater than 10. | |
3853 | The range "+ [-, -]" is always true and hence the range "- [-, -]" is | |
3854 | always false. | |
3855 | ||
3856 | We set up things so that the missing bounds are handled in a consistent | |
3857 | manner so neither a missing bound nor "true" and "false" need to be | |
3858 | handled using a special case. */ | |
3859 | ||
3860 | /* Return the result of applying CODE to ARG0 and ARG1, but handle the case | |
3861 | of ARG0 and/or ARG1 being omitted, meaning an unlimited range. UPPER0_P | |
3862 | and UPPER1_P are nonzero if the respective argument is an upper bound | |
3863 | and zero for a lower. TYPE, if nonzero, is the type of the result; it | |
3864 | must be specified for a comparison. ARG1 will be converted to ARG0's | |
3865 | type if both are specified. */ | |
ef659ec0 | 3866 | |
ebde8a27 | 3867 | static tree |
75040a04 AJ |
3868 | range_binop (enum tree_code code, tree type, tree arg0, int upper0_p, |
3869 | tree arg1, int upper1_p) | |
ebde8a27 | 3870 | { |
27bae8e5 | 3871 | tree tem; |
ebde8a27 RK |
3872 | int result; |
3873 | int sgn0, sgn1; | |
ef659ec0 | 3874 | |
ebde8a27 RK |
3875 | /* If neither arg represents infinity, do the normal operation. |
3876 | Else, if not a comparison, return infinity. Else handle the special | |
3877 | comparison rules. Note that most of the cases below won't occur, but | |
3878 | are handled for consistency. */ | |
ef659ec0 | 3879 | |
ebde8a27 | 3880 | if (arg0 != 0 && arg1 != 0) |
27bae8e5 | 3881 | { |
7f20a5b7 KH |
3882 | tem = fold_build2 (code, type != 0 ? type : TREE_TYPE (arg0), |
3883 | arg0, fold_convert (TREE_TYPE (arg0), arg1)); | |
27bae8e5 RK |
3884 | STRIP_NOPS (tem); |
3885 | return TREE_CODE (tem) == INTEGER_CST ? tem : 0; | |
3886 | } | |
ef659ec0 | 3887 | |
6615c446 | 3888 | if (TREE_CODE_CLASS (code) != tcc_comparison) |
ebde8a27 RK |
3889 | return 0; |
3890 | ||
3891 | /* Set SGN[01] to -1 if ARG[01] is a lower bound, 1 for upper, and 0 | |
d7b3ea38 NS |
3892 | for neither. In real maths, we cannot assume open ended ranges are |
3893 | the same. But, this is computer arithmetic, where numbers are finite. | |
3894 | We can therefore make the transformation of any unbounded range with | |
3895 | the value Z, Z being greater than any representable number. This permits | |
30f7a378 | 3896 | us to treat unbounded ranges as equal. */ |
ebde8a27 | 3897 | sgn0 = arg0 != 0 ? 0 : (upper0_p ? 1 : -1); |
4e644c93 | 3898 | sgn1 = arg1 != 0 ? 0 : (upper1_p ? 1 : -1); |
ebde8a27 RK |
3899 | switch (code) |
3900 | { | |
d7b3ea38 NS |
3901 | case EQ_EXPR: |
3902 | result = sgn0 == sgn1; | |
3903 | break; | |
3904 | case NE_EXPR: | |
3905 | result = sgn0 != sgn1; | |
ebde8a27 | 3906 | break; |
d7b3ea38 | 3907 | case LT_EXPR: |
ebde8a27 RK |
3908 | result = sgn0 < sgn1; |
3909 | break; | |
d7b3ea38 NS |
3910 | case LE_EXPR: |
3911 | result = sgn0 <= sgn1; | |
3912 | break; | |
3913 | case GT_EXPR: | |
ebde8a27 RK |
3914 | result = sgn0 > sgn1; |
3915 | break; | |
d7b3ea38 NS |
3916 | case GE_EXPR: |
3917 | result = sgn0 >= sgn1; | |
3918 | break; | |
e9a25f70 | 3919 | default: |
0bccc606 | 3920 | gcc_unreachable (); |
ebde8a27 RK |
3921 | } |
3922 | ||
1b0f3e79 | 3923 | return constant_boolean_node (result, type); |
ebde8a27 | 3924 | } |
b6cc0a72 | 3925 | \f |
0ccb5dbf JJ |
3926 | /* Helper routine for make_range. Perform one step for it, return |
3927 | new expression if the loop should continue or NULL_TREE if it should | |
3928 | stop. */ | |
3929 | ||
3930 | tree | |
3931 | make_range_step (location_t loc, enum tree_code code, tree arg0, tree arg1, | |
3932 | tree exp_type, tree *p_low, tree *p_high, int *p_in_p, | |
3933 | bool *strict_overflow_p) | |
3934 | { | |
3935 | tree arg0_type = TREE_TYPE (arg0); | |
3936 | tree n_low, n_high, low = *p_low, high = *p_high; | |
3937 | int in_p = *p_in_p, n_in_p; | |
3938 | ||
3939 | switch (code) | |
3940 | { | |
3941 | case TRUTH_NOT_EXPR: | |
9f419393 EB |
3942 | /* We can only do something if the range is testing for zero. */ |
3943 | if (low == NULL_TREE || high == NULL_TREE | |
3944 | || ! integer_zerop (low) || ! integer_zerop (high)) | |
3945 | return NULL_TREE; | |
0ccb5dbf JJ |
3946 | *p_in_p = ! in_p; |
3947 | return arg0; | |
3948 | ||
3949 | case EQ_EXPR: case NE_EXPR: | |
3950 | case LT_EXPR: case LE_EXPR: case GE_EXPR: case GT_EXPR: | |
3951 | /* We can only do something if the range is testing for zero | |
3952 | and if the second operand is an integer constant. Note that | |
3953 | saying something is "in" the range we make is done by | |
3954 | complementing IN_P since it will set in the initial case of | |
3955 | being not equal to zero; "out" is leaving it alone. */ | |
3956 | if (low == NULL_TREE || high == NULL_TREE | |
3957 | || ! integer_zerop (low) || ! integer_zerop (high) | |
3958 | || TREE_CODE (arg1) != INTEGER_CST) | |
3959 | return NULL_TREE; | |
3960 | ||
3961 | switch (code) | |
3962 | { | |
3963 | case NE_EXPR: /* - [c, c] */ | |
3964 | low = high = arg1; | |
3965 | break; | |
3966 | case EQ_EXPR: /* + [c, c] */ | |
3967 | in_p = ! in_p, low = high = arg1; | |
3968 | break; | |
3969 | case GT_EXPR: /* - [-, c] */ | |
3970 | low = 0, high = arg1; | |
3971 | break; | |
3972 | case GE_EXPR: /* + [c, -] */ | |
3973 | in_p = ! in_p, low = arg1, high = 0; | |
3974 | break; | |
3975 | case LT_EXPR: /* - [c, -] */ | |
3976 | low = arg1, high = 0; | |
3977 | break; | |
3978 | case LE_EXPR: /* + [-, c] */ | |
3979 | in_p = ! in_p, low = 0, high = arg1; | |
3980 | break; | |
3981 | default: | |
3982 | gcc_unreachable (); | |
3983 | } | |
3984 | ||
3985 | /* If this is an unsigned comparison, we also know that EXP is | |
3986 | greater than or equal to zero. We base the range tests we make | |
3987 | on that fact, so we record it here so we can parse existing | |
3988 | range tests. We test arg0_type since often the return type | |
3989 | of, e.g. EQ_EXPR, is boolean. */ | |
3990 | if (TYPE_UNSIGNED (arg0_type) && (low == 0 || high == 0)) | |
3991 | { | |
3992 | if (! merge_ranges (&n_in_p, &n_low, &n_high, | |
3993 | in_p, low, high, 1, | |
3994 | build_int_cst (arg0_type, 0), | |
3995 | NULL_TREE)) | |
3996 | return NULL_TREE; | |
3997 | ||
3998 | in_p = n_in_p, low = n_low, high = n_high; | |
3999 | ||
4000 | /* If the high bound is missing, but we have a nonzero low | |
4001 | bound, reverse the range so it goes from zero to the low bound | |
4002 | minus 1. */ | |
4003 | if (high == 0 && low && ! integer_zerop (low)) | |
4004 | { | |
4005 | in_p = ! in_p; | |
4006 | high = range_binop (MINUS_EXPR, NULL_TREE, low, 0, | |
4007 | integer_one_node, 0); | |
4008 | low = build_int_cst (arg0_type, 0); | |
4009 | } | |
4010 | } | |
4011 | ||
4012 | *p_low = low; | |
4013 | *p_high = high; | |
4014 | *p_in_p = in_p; | |
4015 | return arg0; | |
4016 | ||
4017 | case NEGATE_EXPR: | |
9abd8e8b JJ |
4018 | /* If flag_wrapv and ARG0_TYPE is signed, make sure |
4019 | low and high are non-NULL, then normalize will DTRT. */ | |
4020 | if (!TYPE_UNSIGNED (arg0_type) | |
4021 | && !TYPE_OVERFLOW_UNDEFINED (arg0_type)) | |
4022 | { | |
4023 | if (low == NULL_TREE) | |
4024 | low = TYPE_MIN_VALUE (arg0_type); | |
4025 | if (high == NULL_TREE) | |
4026 | high = TYPE_MAX_VALUE (arg0_type); | |
4027 | } | |
4028 | ||
0ccb5dbf JJ |
4029 | /* (-x) IN [a,b] -> x in [-b, -a] */ |
4030 | n_low = range_binop (MINUS_EXPR, exp_type, | |
4031 | build_int_cst (exp_type, 0), | |
4032 | 0, high, 1); | |
4033 | n_high = range_binop (MINUS_EXPR, exp_type, | |
4034 | build_int_cst (exp_type, 0), | |
4035 | 0, low, 0); | |
4036 | if (n_high != 0 && TREE_OVERFLOW (n_high)) | |
4037 | return NULL_TREE; | |
4038 | goto normalize; | |
4039 | ||
4040 | case BIT_NOT_EXPR: | |
4041 | /* ~ X -> -X - 1 */ | |
4042 | return build2_loc (loc, MINUS_EXPR, exp_type, negate_expr (arg0), | |
4043 | build_int_cst (exp_type, 1)); | |
4044 | ||
4045 | case PLUS_EXPR: | |
4046 | case MINUS_EXPR: | |
4047 | if (TREE_CODE (arg1) != INTEGER_CST) | |
4048 | return NULL_TREE; | |
4049 | ||
4050 | /* If flag_wrapv and ARG0_TYPE is signed, then we cannot | |
4051 | move a constant to the other side. */ | |
4052 | if (!TYPE_UNSIGNED (arg0_type) | |
4053 | && !TYPE_OVERFLOW_UNDEFINED (arg0_type)) | |
4054 | return NULL_TREE; | |
4055 | ||
4056 | /* If EXP is signed, any overflow in the computation is undefined, | |
4057 | so we don't worry about it so long as our computations on | |
4058 | the bounds don't overflow. For unsigned, overflow is defined | |
4059 | and this is exactly the right thing. */ | |
4060 | n_low = range_binop (code == MINUS_EXPR ? PLUS_EXPR : MINUS_EXPR, | |
4061 | arg0_type, low, 0, arg1, 0); | |
4062 | n_high = range_binop (code == MINUS_EXPR ? PLUS_EXPR : MINUS_EXPR, | |
4063 | arg0_type, high, 1, arg1, 0); | |
4064 | if ((n_low != 0 && TREE_OVERFLOW (n_low)) | |
4065 | || (n_high != 0 && TREE_OVERFLOW (n_high))) | |
4066 | return NULL_TREE; | |
4067 | ||
4068 | if (TYPE_OVERFLOW_UNDEFINED (arg0_type)) | |
4069 | *strict_overflow_p = true; | |
4070 | ||
4071 | normalize: | |
4072 | /* Check for an unsigned range which has wrapped around the maximum | |
4073 | value thus making n_high < n_low, and normalize it. */ | |
4074 | if (n_low && n_high && tree_int_cst_lt (n_high, n_low)) | |
4075 | { | |
4076 | low = range_binop (PLUS_EXPR, arg0_type, n_high, 0, | |
4077 | integer_one_node, 0); | |
4078 | high = range_binop (MINUS_EXPR, arg0_type, n_low, 0, | |
4079 | integer_one_node, 0); | |
4080 | ||
4081 | /* If the range is of the form +/- [ x+1, x ], we won't | |
4082 | be able to normalize it. But then, it represents the | |
4083 | whole range or the empty set, so make it | |
4084 | +/- [ -, - ]. */ | |
4085 | if (tree_int_cst_equal (n_low, low) | |
4086 | && tree_int_cst_equal (n_high, high)) | |
4087 | low = high = 0; | |
4088 | else | |
4089 | in_p = ! in_p; | |
4090 | } | |
4091 | else | |
4092 | low = n_low, high = n_high; | |
4093 | ||
4094 | *p_low = low; | |
4095 | *p_high = high; | |
4096 | *p_in_p = in_p; | |
4097 | return arg0; | |
4098 | ||
4099 | CASE_CONVERT: | |
4100 | case NON_LVALUE_EXPR: | |
4101 | if (TYPE_PRECISION (arg0_type) > TYPE_PRECISION (exp_type)) | |
4102 | return NULL_TREE; | |
4103 | ||
4104 | if (! INTEGRAL_TYPE_P (arg0_type) | |
4105 | || (low != 0 && ! int_fits_type_p (low, arg0_type)) | |
4106 | || (high != 0 && ! int_fits_type_p (high, arg0_type))) | |
4107 | return NULL_TREE; | |
4108 | ||
4109 | n_low = low, n_high = high; | |
4110 | ||
4111 | if (n_low != 0) | |
4112 | n_low = fold_convert_loc (loc, arg0_type, n_low); | |
4113 | ||
4114 | if (n_high != 0) | |
4115 | n_high = fold_convert_loc (loc, arg0_type, n_high); | |
4116 | ||
4117 | /* If we're converting arg0 from an unsigned type, to exp, | |
4118 | a signed type, we will be doing the comparison as unsigned. | |
4119 | The tests above have already verified that LOW and HIGH | |
4120 | are both positive. | |
4121 | ||
4122 | So we have to ensure that we will handle large unsigned | |
4123 | values the same way that the current signed bounds treat | |
4124 | negative values. */ | |
4125 | ||
4126 | if (!TYPE_UNSIGNED (exp_type) && TYPE_UNSIGNED (arg0_type)) | |
4127 | { | |
4128 | tree high_positive; | |
4129 | tree equiv_type; | |
4130 | /* For fixed-point modes, we need to pass the saturating flag | |
4131 | as the 2nd parameter. */ | |
4132 | if (ALL_FIXED_POINT_MODE_P (TYPE_MODE (arg0_type))) | |
4133 | equiv_type | |
4134 | = lang_hooks.types.type_for_mode (TYPE_MODE (arg0_type), | |
4135 | TYPE_SATURATING (arg0_type)); | |
4136 | else | |
4137 | equiv_type | |
4138 | = lang_hooks.types.type_for_mode (TYPE_MODE (arg0_type), 1); | |
4139 | ||
4140 | /* A range without an upper bound is, naturally, unbounded. | |
4141 | Since convert would have cropped a very large value, use | |
4142 | the max value for the destination type. */ | |
4143 | high_positive | |
4144 | = TYPE_MAX_VALUE (equiv_type) ? TYPE_MAX_VALUE (equiv_type) | |
4145 | : TYPE_MAX_VALUE (arg0_type); | |
4146 | ||
4147 | if (TYPE_PRECISION (exp_type) == TYPE_PRECISION (arg0_type)) | |
4148 | high_positive = fold_build2_loc (loc, RSHIFT_EXPR, arg0_type, | |
4149 | fold_convert_loc (loc, arg0_type, | |
4150 | high_positive), | |
4151 | build_int_cst (arg0_type, 1)); | |
4152 | ||
4153 | /* If the low bound is specified, "and" the range with the | |
4154 | range for which the original unsigned value will be | |
4155 | positive. */ | |
4156 | if (low != 0) | |
4157 | { | |
4158 | if (! merge_ranges (&n_in_p, &n_low, &n_high, 1, n_low, n_high, | |
4159 | 1, fold_convert_loc (loc, arg0_type, | |
4160 | integer_zero_node), | |
4161 | high_positive)) | |
4162 | return NULL_TREE; | |
4163 | ||
4164 | in_p = (n_in_p == in_p); | |
4165 | } | |
4166 | else | |
4167 | { | |
4168 | /* Otherwise, "or" the range with the range of the input | |
4169 | that will be interpreted as negative. */ | |
4170 | if (! merge_ranges (&n_in_p, &n_low, &n_high, 0, n_low, n_high, | |
4171 | 1, fold_convert_loc (loc, arg0_type, | |
4172 | integer_zero_node), | |
4173 | high_positive)) | |
4174 | return NULL_TREE; | |
4175 | ||
4176 | in_p = (in_p != n_in_p); | |
4177 | } | |
4178 | } | |
4179 | ||
4180 | *p_low = n_low; | |
4181 | *p_high = n_high; | |
4182 | *p_in_p = in_p; | |
4183 | return arg0; | |
4184 | ||
4185 | default: | |
4186 | return NULL_TREE; | |
4187 | } | |
4188 | } | |
4189 | ||
ebde8a27 RK |
4190 | /* Given EXP, a logical expression, set the range it is testing into |
4191 | variables denoted by PIN_P, PLOW, and PHIGH. Return the expression | |
6ac01510 ILT |
4192 | actually being tested. *PLOW and *PHIGH will be made of the same |
4193 | type as the returned expression. If EXP is not a comparison, we | |
4194 | will most likely not be returning a useful value and range. Set | |
4195 | *STRICT_OVERFLOW_P to true if the return value is only valid | |
4196 | because signed overflow is undefined; otherwise, do not change | |
4197 | *STRICT_OVERFLOW_P. */ | |
ef659ec0 | 4198 | |
a243fb4a | 4199 | tree |
6ac01510 ILT |
4200 | make_range (tree exp, int *pin_p, tree *plow, tree *phigh, |
4201 | bool *strict_overflow_p) | |
ef659ec0 | 4202 | { |
ebde8a27 | 4203 | enum tree_code code; |
0ccb5dbf JJ |
4204 | tree arg0, arg1 = NULL_TREE; |
4205 | tree exp_type, nexp; | |
4206 | int in_p; | |
4207 | tree low, high; | |
db3927fb | 4208 | location_t loc = EXPR_LOCATION (exp); |
ef659ec0 | 4209 | |
ebde8a27 RK |
4210 | /* Start with simply saying "EXP != 0" and then look at the code of EXP |
4211 | and see if we can refine the range. Some of the cases below may not | |
4212 | happen, but it doesn't seem worth worrying about this. We "continue" | |
4213 | the outer loop when we've changed something; otherwise we "break" | |
4214 | the switch, which will "break" the while. */ | |
ef659ec0 | 4215 | |
088414c1 | 4216 | in_p = 0; |
57decb7e | 4217 | low = high = build_int_cst (TREE_TYPE (exp), 0); |
ebde8a27 RK |
4218 | |
4219 | while (1) | |
ef659ec0 | 4220 | { |
ebde8a27 | 4221 | code = TREE_CODE (exp); |
d1822754 | 4222 | exp_type = TREE_TYPE (exp); |
0ccb5dbf | 4223 | arg0 = NULL_TREE; |
30d68b86 MM |
4224 | |
4225 | if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (code))) | |
4226 | { | |
5039610b | 4227 | if (TREE_OPERAND_LENGTH (exp) > 0) |
d17811fd | 4228 | arg0 = TREE_OPERAND (exp, 0); |
6615c446 JO |
4229 | if (TREE_CODE_CLASS (code) == tcc_binary |
4230 | || TREE_CODE_CLASS (code) == tcc_comparison | |
4231 | || (TREE_CODE_CLASS (code) == tcc_expression | |
5039610b | 4232 | && TREE_OPERAND_LENGTH (exp) > 1)) |
30d68b86 MM |
4233 | arg1 = TREE_OPERAND (exp, 1); |
4234 | } | |
0ccb5dbf JJ |
4235 | if (arg0 == NULL_TREE) |
4236 | break; | |
ef659ec0 | 4237 | |
0ccb5dbf JJ |
4238 | nexp = make_range_step (loc, code, arg0, arg1, exp_type, &low, |
4239 | &high, &in_p, strict_overflow_p); | |
4240 | if (nexp == NULL_TREE) | |
4241 | break; | |
4242 | exp = nexp; | |
ef659ec0 | 4243 | } |
ebde8a27 | 4244 | |
80906567 RK |
4245 | /* If EXP is a constant, we can evaluate whether this is true or false. */ |
4246 | if (TREE_CODE (exp) == INTEGER_CST) | |
4247 | { | |
4248 | in_p = in_p == (integer_onep (range_binop (GE_EXPR, integer_type_node, | |
4249 | exp, 0, low, 0)) | |
4250 | && integer_onep (range_binop (LE_EXPR, integer_type_node, | |
4251 | exp, 1, high, 1))); | |
4252 | low = high = 0; | |
4253 | exp = 0; | |
4254 | } | |
4255 | ||
ebde8a27 RK |
4256 | *pin_p = in_p, *plow = low, *phigh = high; |
4257 | return exp; | |
4258 | } | |
4259 | \f | |
4260 | /* Given a range, LOW, HIGH, and IN_P, an expression, EXP, and a result | |
4261 | type, TYPE, return an expression to test if EXP is in (or out of, depending | |
e1af8299 | 4262 | on IN_P) the range. Return 0 if the test couldn't be created. */ |
ebde8a27 | 4263 | |
a243fb4a | 4264 | tree |
db3927fb AH |
4265 | build_range_check (location_t loc, tree type, tree exp, int in_p, |
4266 | tree low, tree high) | |
ebde8a27 | 4267 | { |
849d624b | 4268 | tree etype = TREE_TYPE (exp), value; |
ebde8a27 | 4269 | |
f60c951c JDA |
4270 | #ifdef HAVE_canonicalize_funcptr_for_compare |
4271 | /* Disable this optimization for function pointer expressions | |
4272 | on targets that require function pointer canonicalization. */ | |
4273 | if (HAVE_canonicalize_funcptr_for_compare | |
4274 | && TREE_CODE (etype) == POINTER_TYPE | |
4275 | && TREE_CODE (TREE_TYPE (etype)) == FUNCTION_TYPE) | |
4276 | return NULL_TREE; | |
4277 | #endif | |
4278 | ||
e1af8299 JJ |
4279 | if (! in_p) |
4280 | { | |
db3927fb | 4281 | value = build_range_check (loc, type, exp, 1, low, high); |
e1af8299 | 4282 | if (value != 0) |
db3927fb | 4283 | return invert_truthvalue_loc (loc, value); |
e1af8299 JJ |
4284 | |
4285 | return 0; | |
4286 | } | |
ebde8a27 | 4287 | |
dbfb1116 | 4288 | if (low == 0 && high == 0) |
427e6a14 | 4289 | return omit_one_operand_loc (loc, type, build_int_cst (type, 1), exp); |
ebde8a27 | 4290 | |
dbfb1116 | 4291 | if (low == 0) |
db3927fb AH |
4292 | return fold_build2_loc (loc, LE_EXPR, type, exp, |
4293 | fold_convert_loc (loc, etype, high)); | |
ebde8a27 | 4294 | |
dbfb1116 | 4295 | if (high == 0) |
db3927fb AH |
4296 | return fold_build2_loc (loc, GE_EXPR, type, exp, |
4297 | fold_convert_loc (loc, etype, low)); | |
ebde8a27 | 4298 | |
dbfb1116 | 4299 | if (operand_equal_p (low, high, 0)) |
db3927fb AH |
4300 | return fold_build2_loc (loc, EQ_EXPR, type, exp, |
4301 | fold_convert_loc (loc, etype, low)); | |
ebde8a27 | 4302 | |
dbfb1116 | 4303 | if (integer_zerop (low)) |
ef659ec0 | 4304 | { |
8df83eae | 4305 | if (! TYPE_UNSIGNED (etype)) |
dd3f0101 | 4306 | { |
ca5ba2a3 | 4307 | etype = unsigned_type_for (etype); |
db3927fb AH |
4308 | high = fold_convert_loc (loc, etype, high); |
4309 | exp = fold_convert_loc (loc, etype, exp); | |
dd3f0101 | 4310 | } |
db3927fb | 4311 | return build_range_check (loc, type, exp, 1, 0, high); |
ebde8a27 | 4312 | } |
ef659ec0 | 4313 | |
dbfb1116 RS |
4314 | /* Optimize (c>=1) && (c<=127) into (signed char)c > 0. */ |
4315 | if (integer_onep (low) && TREE_CODE (high) == INTEGER_CST) | |
4316 | { | |
4317 | unsigned HOST_WIDE_INT lo; | |
4318 | HOST_WIDE_INT hi; | |
4319 | int prec; | |
4320 | ||
4321 | prec = TYPE_PRECISION (etype); | |
4322 | if (prec <= HOST_BITS_PER_WIDE_INT) | |
dd3f0101 KH |
4323 | { |
4324 | hi = 0; | |
4325 | lo = ((unsigned HOST_WIDE_INT) 1 << (prec - 1)) - 1; | |
4326 | } | |
dbfb1116 | 4327 | else |
dd3f0101 KH |
4328 | { |
4329 | hi = ((HOST_WIDE_INT) 1 << (prec - HOST_BITS_PER_WIDE_INT - 1)) - 1; | |
0cadbfaa | 4330 | lo = HOST_WIDE_INT_M1U; |
dd3f0101 | 4331 | } |
dbfb1116 RS |
4332 | |
4333 | if (TREE_INT_CST_HIGH (high) == hi && TREE_INT_CST_LOW (high) == lo) | |
dd3f0101 | 4334 | { |
8df83eae | 4335 | if (TYPE_UNSIGNED (etype)) |
dd3f0101 | 4336 | { |
972afb58 JJ |
4337 | tree signed_etype = signed_type_for (etype); |
4338 | if (TYPE_PRECISION (signed_etype) != TYPE_PRECISION (etype)) | |
4339 | etype | |
4340 | = build_nonstandard_integer_type (TYPE_PRECISION (etype), 0); | |
4341 | else | |
4342 | etype = signed_etype; | |
db3927fb | 4343 | exp = fold_convert_loc (loc, etype, exp); |
dd3f0101 | 4344 | } |
db3927fb | 4345 | return fold_build2_loc (loc, GT_EXPR, type, exp, |
57decb7e | 4346 | build_int_cst (etype, 0)); |
dd3f0101 | 4347 | } |
dbfb1116 RS |
4348 | } |
4349 | ||
f8fe0545 | 4350 | /* Optimize (c>=low) && (c<=high) into (c-low>=0) && (c-low<=high-low). |
84fb43a1 EB |
4351 | This requires wrap-around arithmetics for the type of the expression. |
4352 | First make sure that arithmetics in this type is valid, then make sure | |
4353 | that it wraps around. */ | |
4354 | if (TREE_CODE (etype) == ENUMERAL_TYPE || TREE_CODE (etype) == BOOLEAN_TYPE) | |
4355 | etype = lang_hooks.types.type_for_size (TYPE_PRECISION (etype), | |
4356 | TYPE_UNSIGNED (etype)); | |
f8fe0545 | 4357 | |
84fb43a1 | 4358 | if (TREE_CODE (etype) == INTEGER_TYPE && !TYPE_OVERFLOW_WRAPS (etype)) |
e1af8299 JJ |
4359 | { |
4360 | tree utype, minv, maxv; | |
4361 | ||
4362 | /* Check if (unsigned) INT_MAX + 1 == (unsigned) INT_MIN | |
4363 | for the type in question, as we rely on this here. */ | |
ca5ba2a3 | 4364 | utype = unsigned_type_for (etype); |
db3927fb | 4365 | maxv = fold_convert_loc (loc, utype, TYPE_MAX_VALUE (etype)); |
f8fe0545 EB |
4366 | maxv = range_binop (PLUS_EXPR, NULL_TREE, maxv, 1, |
4367 | integer_one_node, 1); | |
db3927fb | 4368 | minv = fold_convert_loc (loc, utype, TYPE_MIN_VALUE (etype)); |
f8fe0545 EB |
4369 | |
4370 | if (integer_zerop (range_binop (NE_EXPR, integer_type_node, | |
4371 | minv, 1, maxv, 1))) | |
4372 | etype = utype; | |
4373 | else | |
4374 | return 0; | |
e1af8299 JJ |
4375 | } |
4376 | ||
db3927fb AH |
4377 | high = fold_convert_loc (loc, etype, high); |
4378 | low = fold_convert_loc (loc, etype, low); | |
4379 | exp = fold_convert_loc (loc, etype, exp); | |
438090c3 | 4380 | |
43a5d30b | 4381 | value = const_binop (MINUS_EXPR, high, low); |
f8fe0545 | 4382 | |
5be014d5 AP |
4383 | |
4384 | if (POINTER_TYPE_P (etype)) | |
4385 | { | |
4386 | if (value != 0 && !TREE_OVERFLOW (value)) | |
4387 | { | |
0d82a1c8 | 4388 | low = fold_build1_loc (loc, NEGATE_EXPR, TREE_TYPE (low), low); |
db3927fb | 4389 | return build_range_check (loc, type, |
5d49b6a7 | 4390 | fold_build_pointer_plus_loc (loc, exp, low), |
5be014d5 AP |
4391 | 1, build_int_cst (etype, 0), value); |
4392 | } | |
4393 | return 0; | |
4394 | } | |
4395 | ||
f8fe0545 | 4396 | if (value != 0 && !TREE_OVERFLOW (value)) |
db3927fb AH |
4397 | return build_range_check (loc, type, |
4398 | fold_build2_loc (loc, MINUS_EXPR, etype, exp, low), | |
f8fe0545 | 4399 | 1, build_int_cst (etype, 0), value); |
dbfb1116 RS |
4400 | |
4401 | return 0; | |
ebde8a27 RK |
4402 | } |
4403 | \f | |
2f96b754 EB |
4404 | /* Return the predecessor of VAL in its type, handling the infinite case. */ |
4405 | ||
4406 | static tree | |
4407 | range_predecessor (tree val) | |
4408 | { | |
4409 | tree type = TREE_TYPE (val); | |
4410 | ||
1464eeb8 EB |
4411 | if (INTEGRAL_TYPE_P (type) |
4412 | && operand_equal_p (val, TYPE_MIN_VALUE (type), 0)) | |
2f96b754 EB |
4413 | return 0; |
4414 | else | |
4415 | return range_binop (MINUS_EXPR, NULL_TREE, val, 0, integer_one_node, 0); | |
4416 | } | |
4417 | ||
4418 | /* Return the successor of VAL in its type, handling the infinite case. */ | |
4419 | ||
4420 | static tree | |
4421 | range_successor (tree val) | |
4422 | { | |
4423 | tree type = TREE_TYPE (val); | |
4424 | ||
1464eeb8 EB |
4425 | if (INTEGRAL_TYPE_P (type) |
4426 | && operand_equal_p (val, TYPE_MAX_VALUE (type), 0)) | |
2f96b754 EB |
4427 | return 0; |
4428 | else | |
4429 | return range_binop (PLUS_EXPR, NULL_TREE, val, 0, integer_one_node, 0); | |
4430 | } | |
4431 | ||
b6cc0a72 | 4432 | /* Given two ranges, see if we can merge them into one. Return 1 if we |
ebde8a27 | 4433 | can, 0 if we can't. Set the output range into the specified parameters. */ |
ef659ec0 | 4434 | |
a243fb4a | 4435 | bool |
75040a04 AJ |
4436 | merge_ranges (int *pin_p, tree *plow, tree *phigh, int in0_p, tree low0, |
4437 | tree high0, int in1_p, tree low1, tree high1) | |
ebde8a27 RK |
4438 | { |
4439 | int no_overlap; | |
4440 | int subset; | |
4441 | int temp; | |
4442 | tree tem; | |
4443 | int in_p; | |
4444 | tree low, high; | |
ce2157a1 JL |
4445 | int lowequal = ((low0 == 0 && low1 == 0) |
4446 | || integer_onep (range_binop (EQ_EXPR, integer_type_node, | |
4447 | low0, 0, low1, 0))); | |
4448 | int highequal = ((high0 == 0 && high1 == 0) | |
4449 | || integer_onep (range_binop (EQ_EXPR, integer_type_node, | |
4450 | high0, 1, high1, 1))); | |
4451 | ||
4452 | /* Make range 0 be the range that starts first, or ends last if they | |
4453 | start at the same value. Swap them if it isn't. */ | |
b6cc0a72 | 4454 | if (integer_onep (range_binop (GT_EXPR, integer_type_node, |
ebde8a27 | 4455 | low0, 0, low1, 0)) |
ce2157a1 | 4456 | || (lowequal |
ebde8a27 | 4457 | && integer_onep (range_binop (GT_EXPR, integer_type_node, |
ce2157a1 | 4458 | high1, 1, high0, 1)))) |
ebde8a27 RK |
4459 | { |
4460 | temp = in0_p, in0_p = in1_p, in1_p = temp; | |
4461 | tem = low0, low0 = low1, low1 = tem; | |
4462 | tem = high0, high0 = high1, high1 = tem; | |
4463 | } | |
ef659ec0 | 4464 | |
ebde8a27 RK |
4465 | /* Now flag two cases, whether the ranges are disjoint or whether the |
4466 | second range is totally subsumed in the first. Note that the tests | |
4467 | below are simplified by the ones above. */ | |
4468 | no_overlap = integer_onep (range_binop (LT_EXPR, integer_type_node, | |
4469 | high0, 1, low1, 0)); | |
5df8a1f2 | 4470 | subset = integer_onep (range_binop (LE_EXPR, integer_type_node, |
ebde8a27 RK |
4471 | high1, 1, high0, 1)); |
4472 | ||
4473 | /* We now have four cases, depending on whether we are including or | |
4474 | excluding the two ranges. */ | |
4475 | if (in0_p && in1_p) | |
4476 | { | |
4477 | /* If they don't overlap, the result is false. If the second range | |
4478 | is a subset it is the result. Otherwise, the range is from the start | |
4479 | of the second to the end of the first. */ | |
4480 | if (no_overlap) | |
4481 | in_p = 0, low = high = 0; | |
4482 | else if (subset) | |
4483 | in_p = 1, low = low1, high = high1; | |
4484 | else | |
4485 | in_p = 1, low = low1, high = high0; | |
4486 | } | |
ef659ec0 | 4487 | |
ebde8a27 RK |
4488 | else if (in0_p && ! in1_p) |
4489 | { | |
ce2157a1 JL |
4490 | /* If they don't overlap, the result is the first range. If they are |
4491 | equal, the result is false. If the second range is a subset of the | |
4492 | first, and the ranges begin at the same place, we go from just after | |
f8fe0545 | 4493 | the end of the second range to the end of the first. If the second |
ce2157a1 JL |
4494 | range is not a subset of the first, or if it is a subset and both |
4495 | ranges end at the same place, the range starts at the start of the | |
4496 | first range and ends just before the second range. | |
4497 | Otherwise, we can't describe this as a single range. */ | |
ebde8a27 RK |
4498 | if (no_overlap) |
4499 | in_p = 1, low = low0, high = high0; | |
ce2157a1 | 4500 | else if (lowequal && highequal) |
405862dd | 4501 | in_p = 0, low = high = 0; |
ce2157a1 JL |
4502 | else if (subset && lowequal) |
4503 | { | |
f8fe0545 EB |
4504 | low = range_successor (high1); |
4505 | high = high0; | |
39ac2ffc ILT |
4506 | in_p = 1; |
4507 | if (low == 0) | |
4508 | { | |
4509 | /* We are in the weird situation where high0 > high1 but | |
4510 | high1 has no successor. Punt. */ | |
4511 | return 0; | |
4512 | } | |
ce2157a1 JL |
4513 | } |
4514 | else if (! subset || highequal) | |
ebde8a27 | 4515 | { |
f8fe0545 EB |
4516 | low = low0; |
4517 | high = range_predecessor (low1); | |
39ac2ffc ILT |
4518 | in_p = 1; |
4519 | if (high == 0) | |
4520 | { | |
4521 | /* low0 < low1 but low1 has no predecessor. Punt. */ | |
4522 | return 0; | |
4523 | } | |
ebde8a27 | 4524 | } |
ce2157a1 JL |
4525 | else |
4526 | return 0; | |
ebde8a27 | 4527 | } |
ef659ec0 | 4528 | |
ebde8a27 RK |
4529 | else if (! in0_p && in1_p) |
4530 | { | |
4531 | /* If they don't overlap, the result is the second range. If the second | |
4532 | is a subset of the first, the result is false. Otherwise, | |
4533 | the range starts just after the first range and ends at the | |
4534 | end of the second. */ | |
4535 | if (no_overlap) | |
4536 | in_p = 1, low = low1, high = high1; | |
14a774a9 | 4537 | else if (subset || highequal) |
ebde8a27 RK |
4538 | in_p = 0, low = high = 0; |
4539 | else | |
4540 | { | |
f8fe0545 EB |
4541 | low = range_successor (high0); |
4542 | high = high1; | |
39ac2ffc ILT |
4543 | in_p = 1; |
4544 | if (low == 0) | |
4545 | { | |
4546 | /* high1 > high0 but high0 has no successor. Punt. */ | |
4547 | return 0; | |
4548 | } | |
ef659ec0 TW |
4549 | } |
4550 | } | |
4551 | ||
ebde8a27 RK |
4552 | else |
4553 | { | |
4554 | /* The case where we are excluding both ranges. Here the complex case | |
4555 | is if they don't overlap. In that case, the only time we have a | |
4556 | range is if they are adjacent. If the second is a subset of the | |
4557 | first, the result is the first. Otherwise, the range to exclude | |
4558 | starts at the beginning of the first range and ends at the end of the | |
4559 | second. */ | |
4560 | if (no_overlap) | |
4561 | { | |
4562 | if (integer_onep (range_binop (EQ_EXPR, integer_type_node, | |
f8fe0545 | 4563 | range_successor (high0), |
ebde8a27 RK |
4564 | 1, low1, 0))) |
4565 | in_p = 0, low = low0, high = high1; | |
4566 | else | |
e1af8299 JJ |
4567 | { |
4568 | /* Canonicalize - [min, x] into - [-, x]. */ | |
4569 | if (low0 && TREE_CODE (low0) == INTEGER_CST) | |
4570 | switch (TREE_CODE (TREE_TYPE (low0))) | |
4571 | { | |
4572 | case ENUMERAL_TYPE: | |
4573 | if (TYPE_PRECISION (TREE_TYPE (low0)) | |
4574 | != GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (low0)))) | |
4575 | break; | |
4576 | /* FALLTHROUGH */ | |
4577 | case INTEGER_TYPE: | |
e1af8299 JJ |
4578 | if (tree_int_cst_equal (low0, |
4579 | TYPE_MIN_VALUE (TREE_TYPE (low0)))) | |
4580 | low0 = 0; | |
4581 | break; | |
4582 | case POINTER_TYPE: | |
4583 | if (TYPE_UNSIGNED (TREE_TYPE (low0)) | |
4584 | && integer_zerop (low0)) | |
4585 | low0 = 0; | |
4586 | break; | |
4587 | default: | |
4588 | break; | |
4589 | } | |
4590 | ||
4591 | /* Canonicalize - [x, max] into - [x, -]. */ | |
4592 | if (high1 && TREE_CODE (high1) == INTEGER_CST) | |
4593 | switch (TREE_CODE (TREE_TYPE (high1))) | |
4594 | { | |
4595 | case ENUMERAL_TYPE: | |
4596 | if (TYPE_PRECISION (TREE_TYPE (high1)) | |
4597 | != GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (high1)))) | |
4598 | break; | |
4599 | /* FALLTHROUGH */ | |
4600 | case INTEGER_TYPE: | |
e1af8299 JJ |
4601 | if (tree_int_cst_equal (high1, |
4602 | TYPE_MAX_VALUE (TREE_TYPE (high1)))) | |
4603 | high1 = 0; | |
4604 | break; | |
4605 | case POINTER_TYPE: | |
4606 | if (TYPE_UNSIGNED (TREE_TYPE (high1)) | |
4607 | && integer_zerop (range_binop (PLUS_EXPR, NULL_TREE, | |
4608 | high1, 1, | |
4609 | integer_one_node, 1))) | |
4610 | high1 = 0; | |
4611 | break; | |
4612 | default: | |
4613 | break; | |
4614 | } | |
4615 | ||
4616 | /* The ranges might be also adjacent between the maximum and | |
4617 | minimum values of the given type. For | |
4618 | - [{min,-}, x] and - [y, {max,-}] ranges where x + 1 < y | |
4619 | return + [x + 1, y - 1]. */ | |
4620 | if (low0 == 0 && high1 == 0) | |
4621 | { | |
2f96b754 EB |
4622 | low = range_successor (high0); |
4623 | high = range_predecessor (low1); | |
e1af8299 JJ |
4624 | if (low == 0 || high == 0) |
4625 | return 0; | |
4626 | ||
4627 | in_p = 1; | |
4628 | } | |
4629 | else | |
4630 | return 0; | |
4631 | } | |
ebde8a27 RK |
4632 | } |
4633 | else if (subset) | |
4634 | in_p = 0, low = low0, high = high0; | |
4635 | else | |
4636 | in_p = 0, low = low0, high = high1; | |
4637 | } | |
f5902869 | 4638 | |
ebde8a27 RK |
4639 | *pin_p = in_p, *plow = low, *phigh = high; |
4640 | return 1; | |
4641 | } | |
2c486ea7 PB |
4642 | \f |
4643 | ||
4644 | /* Subroutine of fold, looking inside expressions of the form | |
2851dd68 PB |
4645 | A op B ? A : C, where ARG0, ARG1 and ARG2 are the three operands |
4646 | of the COND_EXPR. This function is being used also to optimize | |
4647 | A op B ? C : A, by reversing the comparison first. | |
2c486ea7 PB |
4648 | |
4649 | Return a folded expression whose code is not a COND_EXPR | |
4650 | anymore, or NULL_TREE if no folding opportunity is found. */ | |
4651 | ||
4652 | static tree | |
db3927fb AH |
4653 | fold_cond_expr_with_comparison (location_t loc, tree type, |
4654 | tree arg0, tree arg1, tree arg2) | |
2c486ea7 PB |
4655 | { |
4656 | enum tree_code comp_code = TREE_CODE (arg0); | |
4657 | tree arg00 = TREE_OPERAND (arg0, 0); | |
4658 | tree arg01 = TREE_OPERAND (arg0, 1); | |
2851dd68 | 4659 | tree arg1_type = TREE_TYPE (arg1); |
2c486ea7 | 4660 | tree tem; |
2851dd68 PB |
4661 | |
4662 | STRIP_NOPS (arg1); | |
2c486ea7 PB |
4663 | STRIP_NOPS (arg2); |
4664 | ||
4665 | /* If we have A op 0 ? A : -A, consider applying the following | |
4666 | transformations: | |
4667 | ||
4668 | A == 0? A : -A same as -A | |
4669 | A != 0? A : -A same as A | |
4670 | A >= 0? A : -A same as abs (A) | |
4671 | A > 0? A : -A same as abs (A) | |
4672 | A <= 0? A : -A same as -abs (A) | |
4673 | A < 0? A : -A same as -abs (A) | |
4674 | ||
4675 | None of these transformations work for modes with signed | |
4676 | zeros. If A is +/-0, the first two transformations will | |
4677 | change the sign of the result (from +0 to -0, or vice | |
4678 | versa). The last four will fix the sign of the result, | |
4679 | even though the original expressions could be positive or | |
4680 | negative, depending on the sign of A. | |
4681 | ||
4682 | Note that all these transformations are correct if A is | |
4683 | NaN, since the two alternatives (A and -A) are also NaNs. */ | |
5ce0e197 UB |
4684 | if (!HONOR_SIGNED_ZEROS (TYPE_MODE (type)) |
4685 | && (FLOAT_TYPE_P (TREE_TYPE (arg01)) | |
4686 | ? real_zerop (arg01) | |
4687 | : integer_zerop (arg01)) | |
a10d70ba PH |
4688 | && ((TREE_CODE (arg2) == NEGATE_EXPR |
4689 | && operand_equal_p (TREE_OPERAND (arg2, 0), arg1, 0)) | |
4690 | /* In the case that A is of the form X-Y, '-A' (arg2) may | |
4691 | have already been folded to Y-X, check for that. */ | |
4692 | || (TREE_CODE (arg1) == MINUS_EXPR | |
4693 | && TREE_CODE (arg2) == MINUS_EXPR | |
4694 | && operand_equal_p (TREE_OPERAND (arg1, 0), | |
4695 | TREE_OPERAND (arg2, 1), 0) | |
4696 | && operand_equal_p (TREE_OPERAND (arg1, 1), | |
4697 | TREE_OPERAND (arg2, 0), 0)))) | |
2c486ea7 PB |
4698 | switch (comp_code) |
4699 | { | |
4700 | case EQ_EXPR: | |
3ae472c2 | 4701 | case UNEQ_EXPR: |
db3927fb AH |
4702 | tem = fold_convert_loc (loc, arg1_type, arg1); |
4703 | return pedantic_non_lvalue_loc (loc, | |
4704 | fold_convert_loc (loc, type, | |
4705 | negate_expr (tem))); | |
2c486ea7 | 4706 | case NE_EXPR: |
3ae472c2 | 4707 | case LTGT_EXPR: |
db3927fb | 4708 | return pedantic_non_lvalue_loc (loc, fold_convert_loc (loc, type, arg1)); |
3ae472c2 RS |
4709 | case UNGE_EXPR: |
4710 | case UNGT_EXPR: | |
4711 | if (flag_trapping_math) | |
4712 | break; | |
4713 | /* Fall through. */ | |
2c486ea7 PB |
4714 | case GE_EXPR: |
4715 | case GT_EXPR: | |
2851dd68 | 4716 | if (TYPE_UNSIGNED (TREE_TYPE (arg1))) |
db3927fb | 4717 | arg1 = fold_convert_loc (loc, signed_type_for |
2851dd68 | 4718 | (TREE_TYPE (arg1)), arg1); |
db3927fb AH |
4719 | tem = fold_build1_loc (loc, ABS_EXPR, TREE_TYPE (arg1), arg1); |
4720 | return pedantic_non_lvalue_loc (loc, fold_convert_loc (loc, type, tem)); | |
3ae472c2 RS |
4721 | case UNLE_EXPR: |
4722 | case UNLT_EXPR: | |
4723 | if (flag_trapping_math) | |
4724 | break; | |
2c486ea7 PB |
4725 | case LE_EXPR: |
4726 | case LT_EXPR: | |
2851dd68 | 4727 | if (TYPE_UNSIGNED (TREE_TYPE (arg1))) |
db3927fb | 4728 | arg1 = fold_convert_loc (loc, signed_type_for |
2851dd68 | 4729 | (TREE_TYPE (arg1)), arg1); |
db3927fb AH |
4730 | tem = fold_build1_loc (loc, ABS_EXPR, TREE_TYPE (arg1), arg1); |
4731 | return negate_expr (fold_convert_loc (loc, type, tem)); | |
2c486ea7 | 4732 | default: |
6615c446 | 4733 | gcc_assert (TREE_CODE_CLASS (comp_code) == tcc_comparison); |
3ae472c2 | 4734 | break; |
2c486ea7 PB |
4735 | } |
4736 | ||
4737 | /* A != 0 ? A : 0 is simply A, unless A is -0. Likewise | |
4738 | A == 0 ? A : 0 is always 0 unless A is -0. Note that | |
4739 | both transformations are correct when A is NaN: A != 0 | |
4740 | is then true, and A == 0 is false. */ | |
4741 | ||
5ce0e197 UB |
4742 | if (!HONOR_SIGNED_ZEROS (TYPE_MODE (type)) |
4743 | && integer_zerop (arg01) && integer_zerop (arg2)) | |
2c486ea7 PB |
4744 | { |
4745 | if (comp_code == NE_EXPR) | |
db3927fb | 4746 | return pedantic_non_lvalue_loc (loc, fold_convert_loc (loc, type, arg1)); |
2c486ea7 | 4747 | else if (comp_code == EQ_EXPR) |
08e0cda6 | 4748 | return build_zero_cst (type); |
2c486ea7 PB |
4749 | } |
4750 | ||
4751 | /* Try some transformations of A op B ? A : B. | |
4752 | ||
4753 | A == B? A : B same as B | |
4754 | A != B? A : B same as A | |
4755 | A >= B? A : B same as max (A, B) | |
4756 | A > B? A : B same as max (B, A) | |
4757 | A <= B? A : B same as min (A, B) | |
4758 | A < B? A : B same as min (B, A) | |
4759 | ||
4760 | As above, these transformations don't work in the presence | |
4761 | of signed zeros. For example, if A and B are zeros of | |
4762 | opposite sign, the first two transformations will change | |
4763 | the sign of the result. In the last four, the original | |
4764 | expressions give different results for (A=+0, B=-0) and | |
4765 | (A=-0, B=+0), but the transformed expressions do not. | |
4766 | ||
4767 | The first two transformations are correct if either A or B | |
4768 | is a NaN. In the first transformation, the condition will | |
4769 | be false, and B will indeed be chosen. In the case of the | |
4770 | second transformation, the condition A != B will be true, | |
4771 | and A will be chosen. | |
4772 | ||
4773 | The conversions to max() and min() are not correct if B is | |
4774 | a number and A is not. The conditions in the original | |
4775 | expressions will be false, so all four give B. The min() | |
4776 | and max() versions would give a NaN instead. */ | |
5ce0e197 UB |
4777 | if (!HONOR_SIGNED_ZEROS (TYPE_MODE (type)) |
4778 | && operand_equal_for_comparison_p (arg01, arg2, arg00) | |
283da5df RS |
4779 | /* Avoid these transformations if the COND_EXPR may be used |
4780 | as an lvalue in the C++ front-end. PR c++/19199. */ | |
4781 | && (in_gimple_form | |
08e0cda6 | 4782 | || VECTOR_TYPE_P (type) |
6b4e9576 FJ |
4783 | || (strcmp (lang_hooks.name, "GNU C++") != 0 |
4784 | && strcmp (lang_hooks.name, "GNU Objective-C++") != 0) | |
283da5df RS |
4785 | || ! maybe_lvalue_p (arg1) |
4786 | || ! maybe_lvalue_p (arg2))) | |
2c486ea7 PB |
4787 | { |
4788 | tree comp_op0 = arg00; | |
4789 | tree comp_op1 = arg01; | |
4790 | tree comp_type = TREE_TYPE (comp_op0); | |
4791 | ||
4792 | /* Avoid adding NOP_EXPRs in case this is an lvalue. */ | |
4793 | if (TYPE_MAIN_VARIANT (comp_type) == TYPE_MAIN_VARIANT (type)) | |
4794 | { | |
4795 | comp_type = type; | |
2851dd68 | 4796 | comp_op0 = arg1; |
2c486ea7 PB |
4797 | comp_op1 = arg2; |
4798 | } | |
4799 | ||
4800 | switch (comp_code) | |
4801 | { | |
4802 | case EQ_EXPR: | |
db3927fb | 4803 | return pedantic_non_lvalue_loc (loc, fold_convert_loc (loc, type, arg2)); |
2c486ea7 | 4804 | case NE_EXPR: |
db3927fb | 4805 | return pedantic_non_lvalue_loc (loc, fold_convert_loc (loc, type, arg1)); |
2c486ea7 PB |
4806 | case LE_EXPR: |
4807 | case LT_EXPR: | |
3ae472c2 RS |
4808 | case UNLE_EXPR: |
4809 | case UNLT_EXPR: | |
2c486ea7 PB |
4810 | /* In C++ a ?: expression can be an lvalue, so put the |
4811 | operand which will be used if they are equal first | |
4812 | so that we can convert this back to the | |
4813 | corresponding COND_EXPR. */ | |
2851dd68 | 4814 | if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg1)))) |
e9ea8bd5 | 4815 | { |
db3927fb AH |
4816 | comp_op0 = fold_convert_loc (loc, comp_type, comp_op0); |
4817 | comp_op1 = fold_convert_loc (loc, comp_type, comp_op1); | |
3ae472c2 | 4818 | tem = (comp_code == LE_EXPR || comp_code == UNLE_EXPR) |
db3927fb AH |
4819 | ? fold_build2_loc (loc, MIN_EXPR, comp_type, comp_op0, comp_op1) |
4820 | : fold_build2_loc (loc, MIN_EXPR, comp_type, | |
4821 | comp_op1, comp_op0); | |
4822 | return pedantic_non_lvalue_loc (loc, | |
4823 | fold_convert_loc (loc, type, tem)); | |
e9ea8bd5 | 4824 | } |
2c486ea7 PB |
4825 | break; |
4826 | case GE_EXPR: | |
4827 | case GT_EXPR: | |
3ae472c2 RS |
4828 | case UNGE_EXPR: |
4829 | case UNGT_EXPR: | |
2851dd68 | 4830 | if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg1)))) |
e9ea8bd5 | 4831 | { |
db3927fb AH |
4832 | comp_op0 = fold_convert_loc (loc, comp_type, comp_op0); |
4833 | comp_op1 = fold_convert_loc (loc, comp_type, comp_op1); | |
3ae472c2 | 4834 | tem = (comp_code == GE_EXPR || comp_code == UNGE_EXPR) |
db3927fb AH |
4835 | ? fold_build2_loc (loc, MAX_EXPR, comp_type, comp_op0, comp_op1) |
4836 | : fold_build2_loc (loc, MAX_EXPR, comp_type, | |
4837 | comp_op1, comp_op0); | |
4838 | return pedantic_non_lvalue_loc (loc, | |
4839 | fold_convert_loc (loc, type, tem)); | |
e9ea8bd5 | 4840 | } |
2c486ea7 | 4841 | break; |
3ae472c2 RS |
4842 | case UNEQ_EXPR: |
4843 | if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg1)))) | |
db3927fb AH |
4844 | return pedantic_non_lvalue_loc (loc, |
4845 | fold_convert_loc (loc, type, arg2)); | |
3ae472c2 RS |
4846 | break; |
4847 | case LTGT_EXPR: | |
4848 | if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg1)))) | |
db3927fb AH |
4849 | return pedantic_non_lvalue_loc (loc, |
4850 | fold_convert_loc (loc, type, arg1)); | |
3ae472c2 | 4851 | break; |
2c486ea7 | 4852 | default: |
6615c446 | 4853 | gcc_assert (TREE_CODE_CLASS (comp_code) == tcc_comparison); |
3ae472c2 | 4854 | break; |
2c486ea7 PB |
4855 | } |
4856 | } | |
4857 | ||
4858 | /* If this is A op C1 ? A : C2 with C1 and C2 constant integers, | |
4859 | we might still be able to simplify this. For example, | |
4860 | if C1 is one less or one more than C2, this might have started | |
4861 | out as a MIN or MAX and been transformed by this function. | |
4862 | Only good for INTEGER_TYPEs, because we need TYPE_MAX_VALUE. */ | |
4863 | ||
4864 | if (INTEGRAL_TYPE_P (type) | |
4865 | && TREE_CODE (arg01) == INTEGER_CST | |
4866 | && TREE_CODE (arg2) == INTEGER_CST) | |
4867 | switch (comp_code) | |
4868 | { | |
4869 | case EQ_EXPR: | |
b9da76de JJ |
4870 | if (TREE_CODE (arg1) == INTEGER_CST) |
4871 | break; | |
2c486ea7 | 4872 | /* We can replace A with C1 in this case. */ |
db3927fb AH |
4873 | arg1 = fold_convert_loc (loc, type, arg01); |
4874 | return fold_build3_loc (loc, COND_EXPR, type, arg0, arg1, arg2); | |
2c486ea7 PB |
4875 | |
4876 | case LT_EXPR: | |
b4e4232d JJ |
4877 | /* If C1 is C2 + 1, this is min(A, C2), but use ARG00's type for |
4878 | MIN_EXPR, to preserve the signedness of the comparison. */ | |
2c486ea7 PB |
4879 | if (! operand_equal_p (arg2, TYPE_MAX_VALUE (type), |
4880 | OEP_ONLY_CONST) | |
4881 | && operand_equal_p (arg01, | |
4882 | const_binop (PLUS_EXPR, arg2, | |
43a5d30b | 4883 | build_int_cst (type, 1)), |
2c486ea7 | 4884 | OEP_ONLY_CONST)) |
b4e4232d | 4885 | { |
db3927fb AH |
4886 | tem = fold_build2_loc (loc, MIN_EXPR, TREE_TYPE (arg00), arg00, |
4887 | fold_convert_loc (loc, TREE_TYPE (arg00), | |
4888 | arg2)); | |
b8698a0f | 4889 | return pedantic_non_lvalue_loc (loc, |
db3927fb | 4890 | fold_convert_loc (loc, type, tem)); |
b4e4232d | 4891 | } |
2c486ea7 PB |
4892 | break; |
4893 | ||
4894 | case LE_EXPR: | |
b4e4232d JJ |
4895 | /* If C1 is C2 - 1, this is min(A, C2), with the same care |
4896 | as above. */ | |
2c486ea7 PB |
4897 | if (! operand_equal_p (arg2, TYPE_MIN_VALUE (type), |
4898 | OEP_ONLY_CONST) | |
4899 | && operand_equal_p (arg01, | |
4900 | const_binop (MINUS_EXPR, arg2, | |
43a5d30b | 4901 | build_int_cst (type, 1)), |
2c486ea7 | 4902 | OEP_ONLY_CONST)) |
b4e4232d | 4903 | { |
db3927fb AH |
4904 | tem = fold_build2_loc (loc, MIN_EXPR, TREE_TYPE (arg00), arg00, |
4905 | fold_convert_loc (loc, TREE_TYPE (arg00), | |
4906 | arg2)); | |
4907 | return pedantic_non_lvalue_loc (loc, | |
4908 | fold_convert_loc (loc, type, tem)); | |
b4e4232d | 4909 | } |
2c486ea7 PB |
4910 | break; |
4911 | ||
4912 | case GT_EXPR: | |
30349c74 PB |
4913 | /* If C1 is C2 - 1, this is max(A, C2), but use ARG00's type for |
4914 | MAX_EXPR, to preserve the signedness of the comparison. */ | |
2c486ea7 PB |
4915 | if (! operand_equal_p (arg2, TYPE_MIN_VALUE (type), |
4916 | OEP_ONLY_CONST) | |
4917 | && operand_equal_p (arg01, | |
4918 | const_binop (MINUS_EXPR, arg2, | |
43a5d30b | 4919 | build_int_cst (type, 1)), |
2c486ea7 | 4920 | OEP_ONLY_CONST)) |
b4e4232d | 4921 | { |
db3927fb AH |
4922 | tem = fold_build2_loc (loc, MAX_EXPR, TREE_TYPE (arg00), arg00, |
4923 | fold_convert_loc (loc, TREE_TYPE (arg00), | |
4924 | arg2)); | |
4925 | return pedantic_non_lvalue_loc (loc, fold_convert_loc (loc, type, tem)); | |
b4e4232d | 4926 | } |
2c486ea7 PB |
4927 | break; |
4928 | ||
4929 | case GE_EXPR: | |
30349c74 | 4930 | /* If C1 is C2 + 1, this is max(A, C2), with the same care as above. */ |
2c486ea7 PB |
4931 | if (! operand_equal_p (arg2, TYPE_MAX_VALUE (type), |
4932 | OEP_ONLY_CONST) | |
4933 | && operand_equal_p (arg01, | |
4934 | const_binop (PLUS_EXPR, arg2, | |
43a5d30b | 4935 | build_int_cst (type, 1)), |
2c486ea7 | 4936 | OEP_ONLY_CONST)) |
b4e4232d | 4937 | { |
db3927fb AH |
4938 | tem = fold_build2_loc (loc, MAX_EXPR, TREE_TYPE (arg00), arg00, |
4939 | fold_convert_loc (loc, TREE_TYPE (arg00), | |
4940 | arg2)); | |
4941 | return pedantic_non_lvalue_loc (loc, fold_convert_loc (loc, type, tem)); | |
b4e4232d | 4942 | } |
2c486ea7 PB |
4943 | break; |
4944 | case NE_EXPR: | |
4945 | break; | |
4946 | default: | |
0bccc606 | 4947 | gcc_unreachable (); |
2c486ea7 PB |
4948 | } |
4949 | ||
4950 | return NULL_TREE; | |
4951 | } | |
4952 | ||
4953 | ||
ebde8a27 | 4954 | \f |
b8610a53 | 4955 | #ifndef LOGICAL_OP_NON_SHORT_CIRCUIT |
3a4fd356 | 4956 | #define LOGICAL_OP_NON_SHORT_CIRCUIT \ |
7f4b6d20 | 4957 | (BRANCH_COST (optimize_function_for_speed_p (cfun), \ |
3a4fd356 | 4958 | false) >= 2) |
85e50b6b DE |
4959 | #endif |
4960 | ||
ebde8a27 RK |
4961 | /* EXP is some logical combination of boolean tests. See if we can |
4962 | merge it into some range test. Return the new tree if so. */ | |
ef659ec0 | 4963 | |
ebde8a27 | 4964 | static tree |
db3927fb AH |
4965 | fold_range_test (location_t loc, enum tree_code code, tree type, |
4966 | tree op0, tree op1) | |
ebde8a27 | 4967 | { |
e1f04615 KH |
4968 | int or_op = (code == TRUTH_ORIF_EXPR |
4969 | || code == TRUTH_OR_EXPR); | |
ebde8a27 RK |
4970 | int in0_p, in1_p, in_p; |
4971 | tree low0, low1, low, high0, high1, high; | |
6ac01510 | 4972 | bool strict_overflow_p = false; |
1e08df0e | 4973 | tree tem, lhs, rhs; |
6ac01510 ILT |
4974 | const char * const warnmsg = G_("assuming signed overflow does not occur " |
4975 | "when simplifying range test"); | |
ef659ec0 | 4976 | |
1e08df0e MP |
4977 | if (!INTEGRAL_TYPE_P (type)) |
4978 | return 0; | |
4979 | ||
4980 | lhs = make_range (op0, &in0_p, &low0, &high0, &strict_overflow_p); | |
4981 | rhs = make_range (op1, &in1_p, &low1, &high1, &strict_overflow_p); | |
4982 | ||
ebde8a27 RK |
4983 | /* If this is an OR operation, invert both sides; we will invert |
4984 | again at the end. */ | |
4985 | if (or_op) | |
4986 | in0_p = ! in0_p, in1_p = ! in1_p; | |
4987 | ||
4988 | /* If both expressions are the same, if we can merge the ranges, and we | |
80906567 RK |
4989 | can build the range test, return it or it inverted. If one of the |
4990 | ranges is always true or always false, consider it to be the same | |
4991 | expression as the other. */ | |
4992 | if ((lhs == 0 || rhs == 0 || operand_equal_p (lhs, rhs, 0)) | |
ebde8a27 RK |
4993 | && merge_ranges (&in_p, &low, &high, in0_p, low0, high0, |
4994 | in1_p, low1, high1) | |
e4c03378 | 4995 | && 0 != (tem = (build_range_check (loc, type, |
80906567 RK |
4996 | lhs != 0 ? lhs |
4997 | : rhs != 0 ? rhs : integer_zero_node, | |
ebde8a27 | 4998 | in_p, low, high)))) |
6ac01510 ILT |
4999 | { |
5000 | if (strict_overflow_p) | |
5001 | fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_COMPARISON); | |
db3927fb | 5002 | return or_op ? invert_truthvalue_loc (loc, tem) : tem; |
6ac01510 | 5003 | } |
ebde8a27 RK |
5004 | |
5005 | /* On machines where the branch cost is expensive, if this is a | |
5006 | short-circuited branch and the underlying object on both sides | |
5007 | is the same, make a non-short-circuit operation. */ | |
b8610a53 | 5008 | else if (LOGICAL_OP_NON_SHORT_CIRCUIT |
7cf5c9e1 | 5009 | && lhs != 0 && rhs != 0 |
e1f04615 KH |
5010 | && (code == TRUTH_ANDIF_EXPR |
5011 | || code == TRUTH_ORIF_EXPR) | |
ebde8a27 | 5012 | && operand_equal_p (lhs, rhs, 0)) |
ef659ec0 | 5013 | { |
f0eebf28 | 5014 | /* If simple enough, just rewrite. Otherwise, make a SAVE_EXPR |
9ec36da5 JL |
5015 | unless we are at top level or LHS contains a PLACEHOLDER_EXPR, in |
5016 | which cases we can't do this. */ | |
ebde8a27 | 5017 | if (simple_operand_p (lhs)) |
c9019218 JJ |
5018 | return build2_loc (loc, code == TRUTH_ANDIF_EXPR |
5019 | ? TRUTH_AND_EXPR : TRUTH_OR_EXPR, | |
5020 | type, op0, op1); | |
f0eebf28 | 5021 | |
c99c0026 EB |
5022 | else if (!lang_hooks.decls.global_bindings_p () |
5023 | && !CONTAINS_PLACEHOLDER_P (lhs)) | |
ebde8a27 RK |
5024 | { |
5025 | tree common = save_expr (lhs); | |
5026 | ||
db3927fb | 5027 | if (0 != (lhs = build_range_check (loc, type, common, |
ebde8a27 RK |
5028 | or_op ? ! in0_p : in0_p, |
5029 | low0, high0)) | |
db3927fb | 5030 | && (0 != (rhs = build_range_check (loc, type, common, |
ebde8a27 RK |
5031 | or_op ? ! in1_p : in1_p, |
5032 | low1, high1)))) | |
6ac01510 ILT |
5033 | { |
5034 | if (strict_overflow_p) | |
5035 | fold_overflow_warning (warnmsg, | |
5036 | WARN_STRICT_OVERFLOW_COMPARISON); | |
c9019218 JJ |
5037 | return build2_loc (loc, code == TRUTH_ANDIF_EXPR |
5038 | ? TRUTH_AND_EXPR : TRUTH_OR_EXPR, | |
5039 | type, lhs, rhs); | |
6ac01510 | 5040 | } |
ebde8a27 | 5041 | } |
ef659ec0 | 5042 | } |
de153e82 | 5043 | |
de153e82 | 5044 | return 0; |
ef659ec0 TW |
5045 | } |
5046 | \f | |
6e796a83 | 5047 | /* Subroutine for fold_truth_andor_1: C is an INTEGER_CST interpreted as a P |
25216284 | 5048 | bit value. Arrange things so the extra bits will be set to zero if and |
d4453ee5 RK |
5049 | only if C is signed-extended to its full width. If MASK is nonzero, |
5050 | it is an INTEGER_CST that should be AND'ed with the extra bits. */ | |
02103577 RK |
5051 | |
5052 | static tree | |
fa8db1f7 | 5053 | unextend (tree c, int p, int unsignedp, tree mask) |
02103577 RK |
5054 | { |
5055 | tree type = TREE_TYPE (c); | |
5056 | int modesize = GET_MODE_BITSIZE (TYPE_MODE (type)); | |
5057 | tree temp; | |
5058 | ||
5059 | if (p == modesize || unsignedp) | |
5060 | return c; | |
5061 | ||
02103577 | 5062 | /* We work by getting just the sign bit into the low-order bit, then |
9faa82d8 | 5063 | into the high-order bit, then sign-extend. We then XOR that value |
02103577 | 5064 | with C. */ |
43a5d30b AS |
5065 | temp = const_binop (RSHIFT_EXPR, c, size_int (p - 1)); |
5066 | temp = const_binop (BIT_AND_EXPR, temp, size_int (1)); | |
cf85c69b JW |
5067 | |
5068 | /* We must use a signed type in order to get an arithmetic right shift. | |
5069 | However, we must also avoid introducing accidental overflows, so that | |
b6cc0a72 | 5070 | a subsequent call to integer_zerop will work. Hence we must |
cf85c69b JW |
5071 | do the type conversion here. At this point, the constant is either |
5072 | zero or one, and the conversion to a signed type can never overflow. | |
5073 | We could get an overflow if this conversion is done anywhere else. */ | |
8df83eae | 5074 | if (TYPE_UNSIGNED (type)) |
12753674 | 5075 | temp = fold_convert (signed_type_for (type), temp); |
cf85c69b | 5076 | |
43a5d30b AS |
5077 | temp = const_binop (LSHIFT_EXPR, temp, size_int (modesize - 1)); |
5078 | temp = const_binop (RSHIFT_EXPR, temp, size_int (modesize - p - 1)); | |
d4453ee5 | 5079 | if (mask != 0) |
088414c1 | 5080 | temp = const_binop (BIT_AND_EXPR, temp, |
43a5d30b | 5081 | fold_convert (TREE_TYPE (c), mask)); |
cf85c69b | 5082 | /* If necessary, convert the type back to match the type of C. */ |
8df83eae | 5083 | if (TYPE_UNSIGNED (type)) |
088414c1 | 5084 | temp = fold_convert (type, temp); |
d4453ee5 | 5085 | |
43a5d30b | 5086 | return fold_convert (type, const_binop (BIT_XOR_EXPR, c, temp)); |
02103577 RK |
5087 | } |
5088 | \f | |
27d0d96a BS |
5089 | /* For an expression that has the form |
5090 | (A && B) || ~B | |
5091 | or | |
5092 | (A || B) && ~B, | |
5093 | we can drop one of the inner expressions and simplify to | |
5094 | A || ~B | |
5095 | or | |
5096 | A && ~B | |
5097 | LOC is the location of the resulting expression. OP is the inner | |
5098 | logical operation; the left-hand side in the examples above, while CMPOP | |
5099 | is the right-hand side. RHS_ONLY is used to prevent us from accidentally | |
5100 | removing a condition that guards another, as in | |
5101 | (A != NULL && A->...) || A == NULL | |
5102 | which we must not transform. If RHS_ONLY is true, only eliminate the | |
5103 | right-most operand of the inner logical operation. */ | |
5104 | ||
5105 | static tree | |
5106 | merge_truthop_with_opposite_arm (location_t loc, tree op, tree cmpop, | |
5107 | bool rhs_only) | |
5108 | { | |
5109 | tree type = TREE_TYPE (cmpop); | |
5110 | enum tree_code code = TREE_CODE (cmpop); | |
5111 | enum tree_code truthop_code = TREE_CODE (op); | |
5112 | tree lhs = TREE_OPERAND (op, 0); | |
5113 | tree rhs = TREE_OPERAND (op, 1); | |
5114 | tree orig_lhs = lhs, orig_rhs = rhs; | |
5115 | enum tree_code rhs_code = TREE_CODE (rhs); | |
5116 | enum tree_code lhs_code = TREE_CODE (lhs); | |
5117 | enum tree_code inv_code; | |
5118 | ||
5119 | if (TREE_SIDE_EFFECTS (op) || TREE_SIDE_EFFECTS (cmpop)) | |
5120 | return NULL_TREE; | |
5121 | ||
5122 | if (TREE_CODE_CLASS (code) != tcc_comparison) | |
5123 | return NULL_TREE; | |
5124 | ||
5125 | if (rhs_code == truthop_code) | |
5126 | { | |
5127 | tree newrhs = merge_truthop_with_opposite_arm (loc, rhs, cmpop, rhs_only); | |
5128 | if (newrhs != NULL_TREE) | |
5129 | { | |
5130 | rhs = newrhs; | |
5131 | rhs_code = TREE_CODE (rhs); | |
5132 | } | |
5133 | } | |
5134 | if (lhs_code == truthop_code && !rhs_only) | |
5135 | { | |
5136 | tree newlhs = merge_truthop_with_opposite_arm (loc, lhs, cmpop, false); | |
5137 | if (newlhs != NULL_TREE) | |
5138 | { | |
5139 | lhs = newlhs; | |
5140 | lhs_code = TREE_CODE (lhs); | |
5141 | } | |
5142 | } | |
5143 | ||
5144 | inv_code = invert_tree_comparison (code, HONOR_NANS (TYPE_MODE (type))); | |
5145 | if (inv_code == rhs_code | |
5146 | && operand_equal_p (TREE_OPERAND (rhs, 0), TREE_OPERAND (cmpop, 0), 0) | |
5147 | && operand_equal_p (TREE_OPERAND (rhs, 1), TREE_OPERAND (cmpop, 1), 0)) | |
5148 | return lhs; | |
5149 | if (!rhs_only && inv_code == lhs_code | |
5150 | && operand_equal_p (TREE_OPERAND (lhs, 0), TREE_OPERAND (cmpop, 0), 0) | |
5151 | && operand_equal_p (TREE_OPERAND (lhs, 1), TREE_OPERAND (cmpop, 1), 0)) | |
5152 | return rhs; | |
5153 | if (rhs != orig_rhs || lhs != orig_lhs) | |
5154 | return fold_build2_loc (loc, truthop_code, TREE_TYPE (cmpop), | |
5155 | lhs, rhs); | |
5156 | return NULL_TREE; | |
5157 | } | |
5158 | ||
b2215d83 TW |
5159 | /* Find ways of folding logical expressions of LHS and RHS: |
5160 | Try to merge two comparisons to the same innermost item. | |
5161 | Look for range tests like "ch >= '0' && ch <= '9'". | |
5162 | Look for combinations of simple terms on machines with expensive branches | |
5163 | and evaluate the RHS unconditionally. | |
6d716ca8 RS |
5164 | |
5165 | For example, if we have p->a == 2 && p->b == 4 and we can make an | |
5166 | object large enough to span both A and B, we can do this with a comparison | |
5167 | against the object ANDed with the a mask. | |
5168 | ||
5169 | If we have p->a == q->a && p->b == q->b, we may be able to use bit masking | |
5170 | operations to do this with one comparison. | |
5171 | ||
5172 | We check for both normal comparisons and the BIT_AND_EXPRs made this by | |
5173 | function and the one above. | |
5174 | ||
5175 | CODE is the logical operation being done. It can be TRUTH_ANDIF_EXPR, | |
5176 | TRUTH_AND_EXPR, TRUTH_ORIF_EXPR, or TRUTH_OR_EXPR. | |
5177 | ||
5178 | TRUTH_TYPE is the type of the logical operand and LHS and RHS are its | |
5179 | two operands. | |
5180 | ||
5181 | We return the simplified tree or 0 if no optimization is possible. */ | |
5182 | ||
5183 | static tree | |
6e796a83 KT |
5184 | fold_truth_andor_1 (location_t loc, enum tree_code code, tree truth_type, |
5185 | tree lhs, tree rhs) | |
6d716ca8 | 5186 | { |
f42ef510 | 5187 | /* If this is the "or" of two comparisons, we can do something if |
6d716ca8 | 5188 | the comparisons are NE_EXPR. If this is the "and", we can do something |
b6cc0a72 | 5189 | if the comparisons are EQ_EXPR. I.e., |
fa8db1f7 | 5190 | (a->b == 2 && a->c == 4) can become (a->new == NEW). |
6d716ca8 RS |
5191 | |
5192 | WANTED_CODE is this operation code. For single bit fields, we can | |
5193 | convert EQ_EXPR to NE_EXPR so we need not reject the "wrong" | |
5194 | comparison for one-bit fields. */ | |
5195 | ||
b2215d83 | 5196 | enum tree_code wanted_code; |
6d716ca8 | 5197 | enum tree_code lcode, rcode; |
b2215d83 | 5198 | tree ll_arg, lr_arg, rl_arg, rr_arg; |
6d716ca8 | 5199 | tree ll_inner, lr_inner, rl_inner, rr_inner; |
770ae6cc RK |
5200 | HOST_WIDE_INT ll_bitsize, ll_bitpos, lr_bitsize, lr_bitpos; |
5201 | HOST_WIDE_INT rl_bitsize, rl_bitpos, rr_bitsize, rr_bitpos; | |
45dc13b9 JJ |
5202 | HOST_WIDE_INT xll_bitpos, xlr_bitpos, xrl_bitpos, xrr_bitpos; |
5203 | HOST_WIDE_INT lnbitsize, lnbitpos, rnbitsize, rnbitpos; | |
6d716ca8 RS |
5204 | int ll_unsignedp, lr_unsignedp, rl_unsignedp, rr_unsignedp; |
5205 | enum machine_mode ll_mode, lr_mode, rl_mode, rr_mode; | |
45dc13b9 | 5206 | enum machine_mode lnmode, rnmode; |
6d716ca8 | 5207 | tree ll_mask, lr_mask, rl_mask, rr_mask; |
d4453ee5 | 5208 | tree ll_and_mask, lr_and_mask, rl_and_mask, rr_and_mask; |
b2215d83 | 5209 | tree l_const, r_const; |
45dc13b9 JJ |
5210 | tree lntype, rntype, result; |
5211 | HOST_WIDE_INT first_bit, end_bit; | |
b2215d83 | 5212 | int volatilep; |
6d716ca8 | 5213 | |
ebde8a27 RK |
5214 | /* Start by getting the comparison codes. Fail if anything is volatile. |
5215 | If one operand is a BIT_AND_EXPR with the constant one, treat it as if | |
5216 | it were surrounded with a NE_EXPR. */ | |
6d716ca8 | 5217 | |
ebde8a27 | 5218 | if (TREE_SIDE_EFFECTS (lhs) || TREE_SIDE_EFFECTS (rhs)) |
b2215d83 TW |
5219 | return 0; |
5220 | ||
6d716ca8 RS |
5221 | lcode = TREE_CODE (lhs); |
5222 | rcode = TREE_CODE (rhs); | |
ef659ec0 | 5223 | |
96d4cf0a | 5224 | if (lcode == BIT_AND_EXPR && integer_onep (TREE_OPERAND (lhs, 1))) |
59ce6d6b | 5225 | { |
e9ea8bd5 | 5226 | lhs = build2 (NE_EXPR, truth_type, lhs, |
57decb7e | 5227 | build_int_cst (TREE_TYPE (lhs), 0)); |
59ce6d6b RS |
5228 | lcode = NE_EXPR; |
5229 | } | |
96d4cf0a RK |
5230 | |
5231 | if (rcode == BIT_AND_EXPR && integer_onep (TREE_OPERAND (rhs, 1))) | |
59ce6d6b | 5232 | { |
e9ea8bd5 | 5233 | rhs = build2 (NE_EXPR, truth_type, rhs, |
57decb7e | 5234 | build_int_cst (TREE_TYPE (rhs), 0)); |
59ce6d6b RS |
5235 | rcode = NE_EXPR; |
5236 | } | |
96d4cf0a | 5237 | |
6615c446 JO |
5238 | if (TREE_CODE_CLASS (lcode) != tcc_comparison |
5239 | || TREE_CODE_CLASS (rcode) != tcc_comparison) | |
ef659ec0 TW |
5240 | return 0; |
5241 | ||
b2215d83 TW |
5242 | ll_arg = TREE_OPERAND (lhs, 0); |
5243 | lr_arg = TREE_OPERAND (lhs, 1); | |
5244 | rl_arg = TREE_OPERAND (rhs, 0); | |
5245 | rr_arg = TREE_OPERAND (rhs, 1); | |
b6cc0a72 | 5246 | |
8dcb27ed RS |
5247 | /* Simplify (x<y) && (x==y) into (x<=y) and related optimizations. */ |
5248 | if (simple_operand_p (ll_arg) | |
d1a7edaf | 5249 | && simple_operand_p (lr_arg)) |
8dcb27ed | 5250 | { |
8dcb27ed RS |
5251 | if (operand_equal_p (ll_arg, rl_arg, 0) |
5252 | && operand_equal_p (lr_arg, rr_arg, 0)) | |
d1a7edaf | 5253 | { |
db3927fb | 5254 | result = combine_comparisons (loc, code, lcode, rcode, |
d1a7edaf PB |
5255 | truth_type, ll_arg, lr_arg); |
5256 | if (result) | |
5257 | return result; | |
5258 | } | |
8dcb27ed RS |
5259 | else if (operand_equal_p (ll_arg, rr_arg, 0) |
5260 | && operand_equal_p (lr_arg, rl_arg, 0)) | |
d1a7edaf | 5261 | { |
db3927fb | 5262 | result = combine_comparisons (loc, code, lcode, |
d1a7edaf PB |
5263 | swap_tree_comparison (rcode), |
5264 | truth_type, ll_arg, lr_arg); | |
5265 | if (result) | |
5266 | return result; | |
5267 | } | |
8dcb27ed RS |
5268 | } |
5269 | ||
d1a7edaf PB |
5270 | code = ((code == TRUTH_AND_EXPR || code == TRUTH_ANDIF_EXPR) |
5271 | ? TRUTH_AND_EXPR : TRUTH_OR_EXPR); | |
5272 | ||
8227896c | 5273 | /* If the RHS can be evaluated unconditionally and its operands are |
b2215d83 TW |
5274 | simple, it wins to evaluate the RHS unconditionally on machines |
5275 | with expensive branches. In this case, this isn't a comparison | |
6e796a83 | 5276 | that can be merged. */ |
b2215d83 | 5277 | |
7f4b6d20 | 5278 | if (BRANCH_COST (optimize_function_for_speed_p (cfun), |
3a4fd356 | 5279 | false) >= 2 |
1d691c53 | 5280 | && ! FLOAT_TYPE_P (TREE_TYPE (rl_arg)) |
b2215d83 | 5281 | && simple_operand_p (rl_arg) |
8227896c | 5282 | && simple_operand_p (rr_arg)) |
01c58f26 RS |
5283 | { |
5284 | /* Convert (a != 0) || (b != 0) into (a | b) != 0. */ | |
5285 | if (code == TRUTH_OR_EXPR | |
5286 | && lcode == NE_EXPR && integer_zerop (lr_arg) | |
5287 | && rcode == NE_EXPR && integer_zerop (rr_arg) | |
87a72aa8 AP |
5288 | && TREE_TYPE (ll_arg) == TREE_TYPE (rl_arg) |
5289 | && INTEGRAL_TYPE_P (TREE_TYPE (ll_arg))) | |
c9019218 | 5290 | return build2_loc (loc, NE_EXPR, truth_type, |
db3927fb AH |
5291 | build2 (BIT_IOR_EXPR, TREE_TYPE (ll_arg), |
5292 | ll_arg, rl_arg), | |
5293 | build_int_cst (TREE_TYPE (ll_arg), 0)); | |
01c58f26 RS |
5294 | |
5295 | /* Convert (a == 0) && (b == 0) into (a | b) == 0. */ | |
5296 | if (code == TRUTH_AND_EXPR | |
5297 | && lcode == EQ_EXPR && integer_zerop (lr_arg) | |
5298 | && rcode == EQ_EXPR && integer_zerop (rr_arg) | |
87a72aa8 AP |
5299 | && TREE_TYPE (ll_arg) == TREE_TYPE (rl_arg) |
5300 | && INTEGRAL_TYPE_P (TREE_TYPE (ll_arg))) | |
c9019218 | 5301 | return build2_loc (loc, EQ_EXPR, truth_type, |
db3927fb AH |
5302 | build2 (BIT_IOR_EXPR, TREE_TYPE (ll_arg), |
5303 | ll_arg, rl_arg), | |
5304 | build_int_cst (TREE_TYPE (ll_arg), 0)); | |
01c58f26 | 5305 | } |
b2215d83 | 5306 | |
ef659ec0 TW |
5307 | /* See if the comparisons can be merged. Then get all the parameters for |
5308 | each side. */ | |
5309 | ||
6d716ca8 | 5310 | if ((lcode != EQ_EXPR && lcode != NE_EXPR) |
ef659ec0 | 5311 | || (rcode != EQ_EXPR && rcode != NE_EXPR)) |
6d716ca8 RS |
5312 | return 0; |
5313 | ||
b2215d83 | 5314 | volatilep = 0; |
db3927fb | 5315 | ll_inner = decode_field_reference (loc, ll_arg, |
6d716ca8 | 5316 | &ll_bitsize, &ll_bitpos, &ll_mode, |
d4453ee5 RK |
5317 | &ll_unsignedp, &volatilep, &ll_mask, |
5318 | &ll_and_mask); | |
db3927fb | 5319 | lr_inner = decode_field_reference (loc, lr_arg, |
6d716ca8 | 5320 | &lr_bitsize, &lr_bitpos, &lr_mode, |
d4453ee5 RK |
5321 | &lr_unsignedp, &volatilep, &lr_mask, |
5322 | &lr_and_mask); | |
db3927fb | 5323 | rl_inner = decode_field_reference (loc, rl_arg, |
6d716ca8 | 5324 | &rl_bitsize, &rl_bitpos, &rl_mode, |
d4453ee5 RK |
5325 | &rl_unsignedp, &volatilep, &rl_mask, |
5326 | &rl_and_mask); | |
db3927fb | 5327 | rr_inner = decode_field_reference (loc, rr_arg, |
6d716ca8 | 5328 | &rr_bitsize, &rr_bitpos, &rr_mode, |
d4453ee5 RK |
5329 | &rr_unsignedp, &volatilep, &rr_mask, |
5330 | &rr_and_mask); | |
6d716ca8 RS |
5331 | |
5332 | /* It must be true that the inner operation on the lhs of each | |
5333 | comparison must be the same if we are to be able to do anything. | |
5334 | Then see if we have constants. If not, the same must be true for | |
5335 | the rhs's. */ | |
5336 | if (volatilep || ll_inner == 0 || rl_inner == 0 | |
5337 | || ! operand_equal_p (ll_inner, rl_inner, 0)) | |
5338 | return 0; | |
5339 | ||
b2215d83 TW |
5340 | if (TREE_CODE (lr_arg) == INTEGER_CST |
5341 | && TREE_CODE (rr_arg) == INTEGER_CST) | |
5342 | l_const = lr_arg, r_const = rr_arg; | |
6d716ca8 RS |
5343 | else if (lr_inner == 0 || rr_inner == 0 |
5344 | || ! operand_equal_p (lr_inner, rr_inner, 0)) | |
5345 | return 0; | |
b2215d83 TW |
5346 | else |
5347 | l_const = r_const = 0; | |
6d716ca8 RS |
5348 | |
5349 | /* If either comparison code is not correct for our logical operation, | |
5350 | fail. However, we can convert a one-bit comparison against zero into | |
5351 | the opposite comparison against that bit being set in the field. */ | |
b2215d83 | 5352 | |
9c0ae98b | 5353 | wanted_code = (code == TRUTH_AND_EXPR ? EQ_EXPR : NE_EXPR); |
6d716ca8 RS |
5354 | if (lcode != wanted_code) |
5355 | { | |
5356 | if (l_const && integer_zerop (l_const) && integer_pow2p (ll_mask)) | |
5a6b3365 | 5357 | { |
2bd21a02 AS |
5358 | /* Make the left operand unsigned, since we are only interested |
5359 | in the value of one bit. Otherwise we are doing the wrong | |
5360 | thing below. */ | |
5361 | ll_unsignedp = 1; | |
71a874cd | 5362 | l_const = ll_mask; |
5a6b3365 | 5363 | } |
6d716ca8 RS |
5364 | else |
5365 | return 0; | |
5366 | } | |
5367 | ||
71a874cd | 5368 | /* This is analogous to the code for l_const above. */ |
6d716ca8 RS |
5369 | if (rcode != wanted_code) |
5370 | { | |
5371 | if (r_const && integer_zerop (r_const) && integer_pow2p (rl_mask)) | |
5a6b3365 | 5372 | { |
2bd21a02 | 5373 | rl_unsignedp = 1; |
71a874cd | 5374 | r_const = rl_mask; |
5a6b3365 | 5375 | } |
6d716ca8 RS |
5376 | else |
5377 | return 0; | |
5378 | } | |
5379 | ||
5380 | /* See if we can find a mode that contains both fields being compared on | |
5381 | the left. If we can't, fail. Otherwise, update all constants and masks | |
5382 | to be relative to a field of that size. */ | |
5383 | first_bit = MIN (ll_bitpos, rl_bitpos); | |
5384 | end_bit = MAX (ll_bitpos + ll_bitsize, rl_bitpos + rl_bitsize); | |
1169e45d | 5385 | lnmode = get_best_mode (end_bit - first_bit, first_bit, 0, 0, |
6d716ca8 RS |
5386 | TYPE_ALIGN (TREE_TYPE (ll_inner)), word_mode, |
5387 | volatilep); | |
5388 | if (lnmode == VOIDmode) | |
5389 | return 0; | |
5390 | ||
5391 | lnbitsize = GET_MODE_BITSIZE (lnmode); | |
5392 | lnbitpos = first_bit & ~ (lnbitsize - 1); | |
5785c7de | 5393 | lntype = lang_hooks.types.type_for_size (lnbitsize, 1); |
6d716ca8 RS |
5394 | xll_bitpos = ll_bitpos - lnbitpos, xrl_bitpos = rl_bitpos - lnbitpos; |
5395 | ||
f76b9db2 ILT |
5396 | if (BYTES_BIG_ENDIAN) |
5397 | { | |
5398 | xll_bitpos = lnbitsize - xll_bitpos - ll_bitsize; | |
5399 | xrl_bitpos = lnbitsize - xrl_bitpos - rl_bitsize; | |
5400 | } | |
6d716ca8 | 5401 | |
db3927fb | 5402 | ll_mask = const_binop (LSHIFT_EXPR, fold_convert_loc (loc, lntype, ll_mask), |
43a5d30b | 5403 | size_int (xll_bitpos)); |
db3927fb | 5404 | rl_mask = const_binop (LSHIFT_EXPR, fold_convert_loc (loc, lntype, rl_mask), |
43a5d30b | 5405 | size_int (xrl_bitpos)); |
6d716ca8 | 5406 | |
6d716ca8 RS |
5407 | if (l_const) |
5408 | { | |
db3927fb | 5409 | l_const = fold_convert_loc (loc, lntype, l_const); |
b6cc0a72 | 5410 | l_const = unextend (l_const, ll_bitsize, ll_unsignedp, ll_and_mask); |
43a5d30b | 5411 | l_const = const_binop (LSHIFT_EXPR, l_const, size_int (xll_bitpos)); |
02103577 | 5412 | if (! integer_zerop (const_binop (BIT_AND_EXPR, l_const, |
db3927fb | 5413 | fold_build1_loc (loc, BIT_NOT_EXPR, |
43a5d30b | 5414 | lntype, ll_mask)))) |
02103577 | 5415 | { |
d4ee4d25 | 5416 | warning (0, "comparison is always %d", wanted_code == NE_EXPR); |
b6cc0a72 | 5417 | |
1b0f3e79 | 5418 | return constant_boolean_node (wanted_code == NE_EXPR, truth_type); |
02103577 | 5419 | } |
6d716ca8 RS |
5420 | } |
5421 | if (r_const) | |
5422 | { | |
db3927fb | 5423 | r_const = fold_convert_loc (loc, lntype, r_const); |
d4453ee5 | 5424 | r_const = unextend (r_const, rl_bitsize, rl_unsignedp, rl_and_mask); |
43a5d30b | 5425 | r_const = const_binop (LSHIFT_EXPR, r_const, size_int (xrl_bitpos)); |
02103577 | 5426 | if (! integer_zerop (const_binop (BIT_AND_EXPR, r_const, |
db3927fb | 5427 | fold_build1_loc (loc, BIT_NOT_EXPR, |
43a5d30b | 5428 | lntype, rl_mask)))) |
02103577 | 5429 | { |
d4ee4d25 | 5430 | warning (0, "comparison is always %d", wanted_code == NE_EXPR); |
ab87f8c8 | 5431 | |
1b0f3e79 | 5432 | return constant_boolean_node (wanted_code == NE_EXPR, truth_type); |
02103577 | 5433 | } |
6d716ca8 RS |
5434 | } |
5435 | ||
45dc13b9 JJ |
5436 | /* If the right sides are not constant, do the same for it. Also, |
5437 | disallow this optimization if a size or signedness mismatch occurs | |
5438 | between the left and right sides. */ | |
5439 | if (l_const == 0) | |
5440 | { | |
5441 | if (ll_bitsize != lr_bitsize || rl_bitsize != rr_bitsize | |
5442 | || ll_unsignedp != lr_unsignedp || rl_unsignedp != rr_unsignedp | |
5443 | /* Make sure the two fields on the right | |
5444 | correspond to the left without being swapped. */ | |
5445 | || ll_bitpos - rl_bitpos != lr_bitpos - rr_bitpos) | |
5446 | return 0; | |
5447 | ||
5448 | first_bit = MIN (lr_bitpos, rr_bitpos); | |
5449 | end_bit = MAX (lr_bitpos + lr_bitsize, rr_bitpos + rr_bitsize); | |
1169e45d | 5450 | rnmode = get_best_mode (end_bit - first_bit, first_bit, 0, 0, |
45dc13b9 JJ |
5451 | TYPE_ALIGN (TREE_TYPE (lr_inner)), word_mode, |
5452 | volatilep); | |
5453 | if (rnmode == VOIDmode) | |
5454 | return 0; | |
5455 | ||
5456 | rnbitsize = GET_MODE_BITSIZE (rnmode); | |
5457 | rnbitpos = first_bit & ~ (rnbitsize - 1); | |
5458 | rntype = lang_hooks.types.type_for_size (rnbitsize, 1); | |
5459 | xlr_bitpos = lr_bitpos - rnbitpos, xrr_bitpos = rr_bitpos - rnbitpos; | |
5460 | ||
5461 | if (BYTES_BIG_ENDIAN) | |
5462 | { | |
5463 | xlr_bitpos = rnbitsize - xlr_bitpos - lr_bitsize; | |
5464 | xrr_bitpos = rnbitsize - xrr_bitpos - rr_bitsize; | |
5465 | } | |
5466 | ||
db3927fb AH |
5467 | lr_mask = const_binop (LSHIFT_EXPR, fold_convert_loc (loc, |
5468 | rntype, lr_mask), | |
43a5d30b | 5469 | size_int (xlr_bitpos)); |
db3927fb AH |
5470 | rr_mask = const_binop (LSHIFT_EXPR, fold_convert_loc (loc, |
5471 | rntype, rr_mask), | |
43a5d30b | 5472 | size_int (xrr_bitpos)); |
45dc13b9 JJ |
5473 | |
5474 | /* Make a mask that corresponds to both fields being compared. | |
5475 | Do this for both items being compared. If the operands are the | |
5476 | same size and the bits being compared are in the same position | |
5477 | then we can do this by masking both and comparing the masked | |
5478 | results. */ | |
43a5d30b AS |
5479 | ll_mask = const_binop (BIT_IOR_EXPR, ll_mask, rl_mask); |
5480 | lr_mask = const_binop (BIT_IOR_EXPR, lr_mask, rr_mask); | |
45dc13b9 JJ |
5481 | if (lnbitsize == rnbitsize && xll_bitpos == xlr_bitpos) |
5482 | { | |
db3927fb | 5483 | lhs = make_bit_field_ref (loc, ll_inner, lntype, lnbitsize, lnbitpos, |
45dc13b9 JJ |
5484 | ll_unsignedp || rl_unsignedp); |
5485 | if (! all_ones_mask_p (ll_mask, lnbitsize)) | |
5486 | lhs = build2 (BIT_AND_EXPR, lntype, lhs, ll_mask); | |
5487 | ||
db3927fb | 5488 | rhs = make_bit_field_ref (loc, lr_inner, rntype, rnbitsize, rnbitpos, |
45dc13b9 JJ |
5489 | lr_unsignedp || rr_unsignedp); |
5490 | if (! all_ones_mask_p (lr_mask, rnbitsize)) | |
5491 | rhs = build2 (BIT_AND_EXPR, rntype, rhs, lr_mask); | |
5492 | ||
c9019218 | 5493 | return build2_loc (loc, wanted_code, truth_type, lhs, rhs); |
45dc13b9 JJ |
5494 | } |
5495 | ||
5496 | /* There is still another way we can do something: If both pairs of | |
5497 | fields being compared are adjacent, we may be able to make a wider | |
5498 | field containing them both. | |
5499 | ||
5500 | Note that we still must mask the lhs/rhs expressions. Furthermore, | |
5501 | the mask must be shifted to account for the shift done by | |
5502 | make_bit_field_ref. */ | |
5503 | if ((ll_bitsize + ll_bitpos == rl_bitpos | |
5504 | && lr_bitsize + lr_bitpos == rr_bitpos) | |
5505 | || (ll_bitpos == rl_bitpos + rl_bitsize | |
5506 | && lr_bitpos == rr_bitpos + rr_bitsize)) | |
5507 | { | |
5508 | tree type; | |
5509 | ||
db3927fb AH |
5510 | lhs = make_bit_field_ref (loc, ll_inner, lntype, |
5511 | ll_bitsize + rl_bitsize, | |
45dc13b9 | 5512 | MIN (ll_bitpos, rl_bitpos), ll_unsignedp); |
db3927fb AH |
5513 | rhs = make_bit_field_ref (loc, lr_inner, rntype, |
5514 | lr_bitsize + rr_bitsize, | |
45dc13b9 JJ |
5515 | MIN (lr_bitpos, rr_bitpos), lr_unsignedp); |
5516 | ||
5517 | ll_mask = const_binop (RSHIFT_EXPR, ll_mask, | |
43a5d30b | 5518 | size_int (MIN (xll_bitpos, xrl_bitpos))); |
45dc13b9 | 5519 | lr_mask = const_binop (RSHIFT_EXPR, lr_mask, |
43a5d30b | 5520 | size_int (MIN (xlr_bitpos, xrr_bitpos))); |
45dc13b9 JJ |
5521 | |
5522 | /* Convert to the smaller type before masking out unwanted bits. */ | |
5523 | type = lntype; | |
5524 | if (lntype != rntype) | |
5525 | { | |
5526 | if (lnbitsize > rnbitsize) | |
5527 | { | |
db3927fb AH |
5528 | lhs = fold_convert_loc (loc, rntype, lhs); |
5529 | ll_mask = fold_convert_loc (loc, rntype, ll_mask); | |
45dc13b9 JJ |
5530 | type = rntype; |
5531 | } | |
5532 | else if (lnbitsize < rnbitsize) | |
5533 | { | |
db3927fb AH |
5534 | rhs = fold_convert_loc (loc, lntype, rhs); |
5535 | lr_mask = fold_convert_loc (loc, lntype, lr_mask); | |
45dc13b9 JJ |
5536 | type = lntype; |
5537 | } | |
5538 | } | |
5539 | ||
5540 | if (! all_ones_mask_p (ll_mask, ll_bitsize + rl_bitsize)) | |
5541 | lhs = build2 (BIT_AND_EXPR, type, lhs, ll_mask); | |
5542 | ||
5543 | if (! all_ones_mask_p (lr_mask, lr_bitsize + rr_bitsize)) | |
5544 | rhs = build2 (BIT_AND_EXPR, type, rhs, lr_mask); | |
5545 | ||
c9019218 | 5546 | return build2_loc (loc, wanted_code, truth_type, lhs, rhs); |
45dc13b9 JJ |
5547 | } |
5548 | ||
5549 | return 0; | |
5550 | } | |
5551 | ||
6d716ca8 RS |
5552 | /* Handle the case of comparisons with constants. If there is something in |
5553 | common between the masks, those bits of the constants must be the same. | |
5554 | If not, the condition is always false. Test for this to avoid generating | |
5555 | incorrect code below. */ | |
43a5d30b | 5556 | result = const_binop (BIT_AND_EXPR, ll_mask, rl_mask); |
6d716ca8 | 5557 | if (! integer_zerop (result) |
43a5d30b AS |
5558 | && simple_cst_equal (const_binop (BIT_AND_EXPR, result, l_const), |
5559 | const_binop (BIT_AND_EXPR, result, r_const)) != 1) | |
6d716ca8 RS |
5560 | { |
5561 | if (wanted_code == NE_EXPR) | |
5562 | { | |
d4ee4d25 | 5563 | warning (0, "%<or%> of unmatched not-equal tests is always 1"); |
1b0f3e79 | 5564 | return constant_boolean_node (true, truth_type); |
6d716ca8 RS |
5565 | } |
5566 | else | |
5567 | { | |
d4ee4d25 | 5568 | warning (0, "%<and%> of mutually exclusive equal-tests is always 0"); |
1b0f3e79 | 5569 | return constant_boolean_node (false, truth_type); |
6d716ca8 RS |
5570 | } |
5571 | } | |
5572 | ||
45dc13b9 JJ |
5573 | /* Construct the expression we will return. First get the component |
5574 | reference we will make. Unless the mask is all ones the width of | |
5575 | that field, perform the mask operation. Then compare with the | |
5576 | merged constant. */ | |
db3927fb | 5577 | result = make_bit_field_ref (loc, ll_inner, lntype, lnbitsize, lnbitpos, |
45dc13b9 JJ |
5578 | ll_unsignedp || rl_unsignedp); |
5579 | ||
43a5d30b | 5580 | ll_mask = const_binop (BIT_IOR_EXPR, ll_mask, rl_mask); |
45dc13b9 | 5581 | if (! all_ones_mask_p (ll_mask, lnbitsize)) |
c9019218 | 5582 | result = build2_loc (loc, BIT_AND_EXPR, lntype, result, ll_mask); |
45dc13b9 | 5583 | |
c9019218 JJ |
5584 | return build2_loc (loc, wanted_code, truth_type, result, |
5585 | const_binop (BIT_IOR_EXPR, l_const, r_const)); | |
6d716ca8 RS |
5586 | } |
5587 | \f | |
b6cc0a72 | 5588 | /* Optimize T, which is a comparison of a MIN_EXPR or MAX_EXPR with a |
14a774a9 RK |
5589 | constant. */ |
5590 | ||
5591 | static tree | |
db3927fb AH |
5592 | optimize_minmax_comparison (location_t loc, enum tree_code code, tree type, |
5593 | tree op0, tree op1) | |
14a774a9 | 5594 | { |
d7e5b287 | 5595 | tree arg0 = op0; |
14a774a9 | 5596 | enum tree_code op_code; |
c071e8bc | 5597 | tree comp_const; |
14a774a9 RK |
5598 | tree minmax_const; |
5599 | int consts_equal, consts_lt; | |
5600 | tree inner; | |
5601 | ||
5602 | STRIP_SIGN_NOPS (arg0); | |
5603 | ||
5604 | op_code = TREE_CODE (arg0); | |
5605 | minmax_const = TREE_OPERAND (arg0, 1); | |
db3927fb | 5606 | comp_const = fold_convert_loc (loc, TREE_TYPE (arg0), op1); |
14a774a9 RK |
5607 | consts_equal = tree_int_cst_equal (minmax_const, comp_const); |
5608 | consts_lt = tree_int_cst_lt (minmax_const, comp_const); | |
5609 | inner = TREE_OPERAND (arg0, 0); | |
5610 | ||
5611 | /* If something does not permit us to optimize, return the original tree. */ | |
5612 | if ((op_code != MIN_EXPR && op_code != MAX_EXPR) | |
5613 | || TREE_CODE (comp_const) != INTEGER_CST | |
455f14dd | 5614 | || TREE_OVERFLOW (comp_const) |
14a774a9 | 5615 | || TREE_CODE (minmax_const) != INTEGER_CST |
455f14dd | 5616 | || TREE_OVERFLOW (minmax_const)) |
d7e5b287 | 5617 | return NULL_TREE; |
14a774a9 RK |
5618 | |
5619 | /* Now handle all the various comparison codes. We only handle EQ_EXPR | |
5620 | and GT_EXPR, doing the rest with recursive calls using logical | |
5621 | simplifications. */ | |
d7e5b287 | 5622 | switch (code) |
14a774a9 RK |
5623 | { |
5624 | case NE_EXPR: case LT_EXPR: case LE_EXPR: | |
d7e5b287 | 5625 | { |
db3927fb AH |
5626 | tree tem |
5627 | = optimize_minmax_comparison (loc, | |
5628 | invert_tree_comparison (code, false), | |
5629 | type, op0, op1); | |
d817ed3b | 5630 | if (tem) |
db3927fb | 5631 | return invert_truthvalue_loc (loc, tem); |
d817ed3b | 5632 | return NULL_TREE; |
d7e5b287 | 5633 | } |
14a774a9 RK |
5634 | |
5635 | case GE_EXPR: | |
5636 | return | |
db3927fb | 5637 | fold_build2_loc (loc, TRUTH_ORIF_EXPR, type, |
7f20a5b7 | 5638 | optimize_minmax_comparison |
db3927fb | 5639 | (loc, EQ_EXPR, type, arg0, comp_const), |
7f20a5b7 | 5640 | optimize_minmax_comparison |
db3927fb | 5641 | (loc, GT_EXPR, type, arg0, comp_const)); |
14a774a9 RK |
5642 | |
5643 | case EQ_EXPR: | |
5644 | if (op_code == MAX_EXPR && consts_equal) | |
5645 | /* MAX (X, 0) == 0 -> X <= 0 */ | |
db3927fb | 5646 | return fold_build2_loc (loc, LE_EXPR, type, inner, comp_const); |
14a774a9 RK |
5647 | |
5648 | else if (op_code == MAX_EXPR && consts_lt) | |
5649 | /* MAX (X, 0) == 5 -> X == 5 */ | |
db3927fb | 5650 | return fold_build2_loc (loc, EQ_EXPR, type, inner, comp_const); |
14a774a9 RK |
5651 | |
5652 | else if (op_code == MAX_EXPR) | |
5653 | /* MAX (X, 0) == -1 -> false */ | |
db3927fb | 5654 | return omit_one_operand_loc (loc, type, integer_zero_node, inner); |
14a774a9 RK |
5655 | |
5656 | else if (consts_equal) | |
5657 | /* MIN (X, 0) == 0 -> X >= 0 */ | |
db3927fb | 5658 | return fold_build2_loc (loc, GE_EXPR, type, inner, comp_const); |
14a774a9 RK |
5659 | |
5660 | else if (consts_lt) | |
5661 | /* MIN (X, 0) == 5 -> false */ | |
db3927fb | 5662 | return omit_one_operand_loc (loc, type, integer_zero_node, inner); |
14a774a9 RK |
5663 | |
5664 | else | |
5665 | /* MIN (X, 0) == -1 -> X == -1 */ | |
db3927fb | 5666 | return fold_build2_loc (loc, EQ_EXPR, type, inner, comp_const); |
14a774a9 RK |
5667 | |
5668 | case GT_EXPR: | |
5669 | if (op_code == MAX_EXPR && (consts_equal || consts_lt)) | |
5670 | /* MAX (X, 0) > 0 -> X > 0 | |
5671 | MAX (X, 0) > 5 -> X > 5 */ | |
db3927fb | 5672 | return fold_build2_loc (loc, GT_EXPR, type, inner, comp_const); |
14a774a9 RK |
5673 | |
5674 | else if (op_code == MAX_EXPR) | |
5675 | /* MAX (X, 0) > -1 -> true */ | |
db3927fb | 5676 | return omit_one_operand_loc (loc, type, integer_one_node, inner); |
14a774a9 RK |
5677 | |
5678 | else if (op_code == MIN_EXPR && (consts_equal || consts_lt)) | |
5679 | /* MIN (X, 0) > 0 -> false | |
5680 | MIN (X, 0) > 5 -> false */ | |
db3927fb | 5681 | return omit_one_operand_loc (loc, type, integer_zero_node, inner); |
14a774a9 RK |
5682 | |
5683 | else | |
5684 | /* MIN (X, 0) > -1 -> X > -1 */ | |
db3927fb | 5685 | return fold_build2_loc (loc, GT_EXPR, type, inner, comp_const); |
14a774a9 RK |
5686 | |
5687 | default: | |
d7e5b287 | 5688 | return NULL_TREE; |
14a774a9 RK |
5689 | } |
5690 | } | |
5691 | \f | |
1baa375f RK |
5692 | /* T is an integer expression that is being multiplied, divided, or taken a |
5693 | modulus (CODE says which and what kind of divide or modulus) by a | |
5694 | constant C. See if we can eliminate that operation by folding it with | |
5695 | other operations already in T. WIDE_TYPE, if non-null, is a type that | |
5696 | should be used for the computation if wider than our type. | |
5697 | ||
cff27795 EB |
5698 | For example, if we are dividing (X * 8) + (Y * 16) by 4, we can return |
5699 | (X * 2) + (Y * 4). We must, however, be assured that either the original | |
8e1ca098 RH |
5700 | expression would not overflow or that overflow is undefined for the type |
5701 | in the language in question. | |
5702 | ||
1baa375f | 5703 | If we return a non-null expression, it is an equivalent form of the |
6ac01510 ILT |
5704 | original computation, but need not be in the original type. |
5705 | ||
5706 | We set *STRICT_OVERFLOW_P to true if the return values depends on | |
5707 | signed overflow being undefined. Otherwise we do not change | |
5708 | *STRICT_OVERFLOW_P. */ | |
1baa375f RK |
5709 | |
5710 | static tree | |
6ac01510 ILT |
5711 | extract_muldiv (tree t, tree c, enum tree_code code, tree wide_type, |
5712 | bool *strict_overflow_p) | |
cdd4b0d4 AB |
5713 | { |
5714 | /* To avoid exponential search depth, refuse to allow recursion past | |
5715 | three levels. Beyond that (1) it's highly unlikely that we'll find | |
5716 | something interesting and (2) we've probably processed it before | |
5717 | when we built the inner expression. */ | |
5718 | ||
5719 | static int depth; | |
5720 | tree ret; | |
5721 | ||
5722 | if (depth > 3) | |
5723 | return NULL; | |
5724 | ||
5725 | depth++; | |
6ac01510 | 5726 | ret = extract_muldiv_1 (t, c, code, wide_type, strict_overflow_p); |
cdd4b0d4 AB |
5727 | depth--; |
5728 | ||
5729 | return ret; | |
5730 | } | |
5731 | ||
5732 | static tree | |
6ac01510 ILT |
5733 | extract_muldiv_1 (tree t, tree c, enum tree_code code, tree wide_type, |
5734 | bool *strict_overflow_p) | |
1baa375f RK |
5735 | { |
5736 | tree type = TREE_TYPE (t); | |
5737 | enum tree_code tcode = TREE_CODE (t); | |
b6cc0a72 | 5738 | tree ctype = (wide_type != 0 && (GET_MODE_SIZE (TYPE_MODE (wide_type)) |
1baa375f RK |
5739 | > GET_MODE_SIZE (TYPE_MODE (type))) |
5740 | ? wide_type : type); | |
5741 | tree t1, t2; | |
5742 | int same_p = tcode == code; | |
9d0878fd | 5743 | tree op0 = NULL_TREE, op1 = NULL_TREE; |
6ac01510 | 5744 | bool sub_strict_overflow_p; |
1baa375f RK |
5745 | |
5746 | /* Don't deal with constants of zero here; they confuse the code below. */ | |
5747 | if (integer_zerop (c)) | |
8e1ca098 | 5748 | return NULL_TREE; |
1baa375f | 5749 | |
6615c446 | 5750 | if (TREE_CODE_CLASS (tcode) == tcc_unary) |
1baa375f RK |
5751 | op0 = TREE_OPERAND (t, 0); |
5752 | ||
6615c446 | 5753 | if (TREE_CODE_CLASS (tcode) == tcc_binary) |
1baa375f RK |
5754 | op0 = TREE_OPERAND (t, 0), op1 = TREE_OPERAND (t, 1); |
5755 | ||
5756 | /* Note that we need not handle conditional operations here since fold | |
5757 | already handles those cases. So just do arithmetic here. */ | |
5758 | switch (tcode) | |
5759 | { | |
5760 | case INTEGER_CST: | |
5761 | /* For a constant, we can always simplify if we are a multiply | |
5762 | or (for divide and modulus) if it is a multiple of our constant. */ | |
5763 | if (code == MULT_EXPR | |
43a5d30b | 5764 | || integer_zerop (const_binop (TRUNC_MOD_EXPR, t, c))) |
088414c1 | 5765 | return const_binop (code, fold_convert (ctype, t), |
43a5d30b | 5766 | fold_convert (ctype, c)); |
1baa375f RK |
5767 | break; |
5768 | ||
1043771b | 5769 | CASE_CONVERT: case NON_LVALUE_EXPR: |
43e4a9d8 | 5770 | /* If op0 is an expression ... */ |
6615c446 JO |
5771 | if ((COMPARISON_CLASS_P (op0) |
5772 | || UNARY_CLASS_P (op0) | |
5773 | || BINARY_CLASS_P (op0) | |
5039610b | 5774 | || VL_EXP_CLASS_P (op0) |
6615c446 | 5775 | || EXPRESSION_CLASS_P (op0)) |
fcb4587e RG |
5776 | /* ... and has wrapping overflow, and its type is smaller |
5777 | than ctype, then we cannot pass through as widening. */ | |
5778 | && ((TYPE_OVERFLOW_WRAPS (TREE_TYPE (op0)) | |
fcb4587e RG |
5779 | && (TYPE_PRECISION (ctype) |
5780 | > TYPE_PRECISION (TREE_TYPE (op0)))) | |
a0fac73d RS |
5781 | /* ... or this is a truncation (t is narrower than op0), |
5782 | then we cannot pass through this narrowing. */ | |
fcb4587e RG |
5783 | || (TYPE_PRECISION (type) |
5784 | < TYPE_PRECISION (TREE_TYPE (op0))) | |
068d2c9d MM |
5785 | /* ... or signedness changes for division or modulus, |
5786 | then we cannot pass through this conversion. */ | |
5787 | || (code != MULT_EXPR | |
8df83eae | 5788 | && (TYPE_UNSIGNED (ctype) |
ac029795 RG |
5789 | != TYPE_UNSIGNED (TREE_TYPE (op0)))) |
5790 | /* ... or has undefined overflow while the converted to | |
5791 | type has not, we cannot do the operation in the inner type | |
5792 | as that would introduce undefined overflow. */ | |
5793 | || (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (op0)) | |
5794 | && !TYPE_OVERFLOW_UNDEFINED (type)))) | |
eff9c80d RH |
5795 | break; |
5796 | ||
1baa375f | 5797 | /* Pass the constant down and see if we can make a simplification. If |
59adecfa RK |
5798 | we can, replace this expression with the inner simplification for |
5799 | possible later conversion to our or some other type. */ | |
088414c1 | 5800 | if ((t2 = fold_convert (TREE_TYPE (op0), c)) != 0 |
3cd58fd7 | 5801 | && TREE_CODE (t2) == INTEGER_CST |
455f14dd | 5802 | && !TREE_OVERFLOW (t2) |
3cd58fd7 OH |
5803 | && (0 != (t1 = extract_muldiv (op0, t2, code, |
5804 | code == MULT_EXPR | |
6ac01510 ILT |
5805 | ? ctype : NULL_TREE, |
5806 | strict_overflow_p)))) | |
1baa375f RK |
5807 | return t1; |
5808 | break; | |
5809 | ||
47d42ce2 JJ |
5810 | case ABS_EXPR: |
5811 | /* If widening the type changes it from signed to unsigned, then we | |
5812 | must avoid building ABS_EXPR itself as unsigned. */ | |
5813 | if (TYPE_UNSIGNED (ctype) && !TYPE_UNSIGNED (type)) | |
5814 | { | |
12753674 | 5815 | tree cstype = (*signed_type_for) (ctype); |
6ac01510 ILT |
5816 | if ((t1 = extract_muldiv (op0, c, code, cstype, strict_overflow_p)) |
5817 | != 0) | |
47d42ce2 | 5818 | { |
7f20a5b7 | 5819 | t1 = fold_build1 (tcode, cstype, fold_convert (cstype, t1)); |
47d42ce2 JJ |
5820 | return fold_convert (ctype, t1); |
5821 | } | |
5822 | break; | |
5823 | } | |
a0857153 RG |
5824 | /* If the constant is negative, we cannot simplify this. */ |
5825 | if (tree_int_cst_sgn (c) == -1) | |
5826 | break; | |
47d42ce2 JJ |
5827 | /* FALLTHROUGH */ |
5828 | case NEGATE_EXPR: | |
600a5961 JJ |
5829 | /* For division and modulus, type can't be unsigned, as e.g. |
5830 | (-(x / 2U)) / 2U isn't equal to -((x / 2U) / 2U) for x >= 2. | |
5831 | For signed types, even with wrapping overflow, this is fine. */ | |
5832 | if (code != MULT_EXPR && TYPE_UNSIGNED (type)) | |
5833 | break; | |
6ac01510 ILT |
5834 | if ((t1 = extract_muldiv (op0, c, code, wide_type, strict_overflow_p)) |
5835 | != 0) | |
7f20a5b7 | 5836 | return fold_build1 (tcode, ctype, fold_convert (ctype, t1)); |
1baa375f RK |
5837 | break; |
5838 | ||
5839 | case MIN_EXPR: case MAX_EXPR: | |
13393c8a JW |
5840 | /* If widening the type changes the signedness, then we can't perform |
5841 | this optimization as that changes the result. */ | |
8df83eae | 5842 | if (TYPE_UNSIGNED (ctype) != TYPE_UNSIGNED (type)) |
13393c8a JW |
5843 | break; |
5844 | ||
1baa375f | 5845 | /* MIN (a, b) / 5 -> MIN (a / 5, b / 5) */ |
6ac01510 ILT |
5846 | sub_strict_overflow_p = false; |
5847 | if ((t1 = extract_muldiv (op0, c, code, wide_type, | |
5848 | &sub_strict_overflow_p)) != 0 | |
5849 | && (t2 = extract_muldiv (op1, c, code, wide_type, | |
5850 | &sub_strict_overflow_p)) != 0) | |
59adecfa RK |
5851 | { |
5852 | if (tree_int_cst_sgn (c) < 0) | |
5853 | tcode = (tcode == MIN_EXPR ? MAX_EXPR : MIN_EXPR); | |
6ac01510 ILT |
5854 | if (sub_strict_overflow_p) |
5855 | *strict_overflow_p = true; | |
7f20a5b7 KH |
5856 | return fold_build2 (tcode, ctype, fold_convert (ctype, t1), |
5857 | fold_convert (ctype, t2)); | |
59adecfa | 5858 | } |
1baa375f RK |
5859 | break; |
5860 | ||
1baa375f RK |
5861 | case LSHIFT_EXPR: case RSHIFT_EXPR: |
5862 | /* If the second operand is constant, this is a multiplication | |
5863 | or floor division, by a power of two, so we can treat it that | |
9e629a80 JM |
5864 | way unless the multiplier or divisor overflows. Signed |
5865 | left-shift overflow is implementation-defined rather than | |
5866 | undefined in C90, so do not convert signed left shift into | |
5867 | multiplication. */ | |
1baa375f | 5868 | if (TREE_CODE (op1) == INTEGER_CST |
9e629a80 | 5869 | && (tcode == RSHIFT_EXPR || TYPE_UNSIGNED (TREE_TYPE (op0))) |
d08230fe NC |
5870 | /* const_binop may not detect overflow correctly, |
5871 | so check for it explicitly here. */ | |
5872 | && TYPE_PRECISION (TREE_TYPE (size_one_node)) > TREE_INT_CST_LOW (op1) | |
5873 | && TREE_INT_CST_HIGH (op1) == 0 | |
088414c1 RS |
5874 | && 0 != (t1 = fold_convert (ctype, |
5875 | const_binop (LSHIFT_EXPR, | |
5876 | size_one_node, | |
43a5d30b | 5877 | op1))) |
455f14dd | 5878 | && !TREE_OVERFLOW (t1)) |
59ce6d6b RS |
5879 | return extract_muldiv (build2 (tcode == LSHIFT_EXPR |
5880 | ? MULT_EXPR : FLOOR_DIV_EXPR, | |
db3927fb AH |
5881 | ctype, |
5882 | fold_convert (ctype, op0), | |
5883 | t1), | |
6ac01510 | 5884 | c, code, wide_type, strict_overflow_p); |
1baa375f RK |
5885 | break; |
5886 | ||
5887 | case PLUS_EXPR: case MINUS_EXPR: | |
5888 | /* See if we can eliminate the operation on both sides. If we can, we | |
5889 | can return a new PLUS or MINUS. If we can't, the only remaining | |
5890 | cases where we can do anything are if the second operand is a | |
5891 | constant. */ | |
6ac01510 ILT |
5892 | sub_strict_overflow_p = false; |
5893 | t1 = extract_muldiv (op0, c, code, wide_type, &sub_strict_overflow_p); | |
5894 | t2 = extract_muldiv (op1, c, code, wide_type, &sub_strict_overflow_p); | |
fba2c0cd JJ |
5895 | if (t1 != 0 && t2 != 0 |
5896 | && (code == MULT_EXPR | |
b77f3744 CE |
5897 | /* If not multiplication, we can only do this if both operands |
5898 | are divisible by c. */ | |
5899 | || (multiple_of_p (ctype, op0, c) | |
5900 | && multiple_of_p (ctype, op1, c)))) | |
6ac01510 ILT |
5901 | { |
5902 | if (sub_strict_overflow_p) | |
5903 | *strict_overflow_p = true; | |
5904 | return fold_build2 (tcode, ctype, fold_convert (ctype, t1), | |
5905 | fold_convert (ctype, t2)); | |
5906 | } | |
1baa375f | 5907 | |
59adecfa RK |
5908 | /* If this was a subtraction, negate OP1 and set it to be an addition. |
5909 | This simplifies the logic below. */ | |
5910 | if (tcode == MINUS_EXPR) | |
ffaf6f25 EB |
5911 | { |
5912 | tcode = PLUS_EXPR, op1 = negate_expr (op1); | |
5913 | /* If OP1 was not easily negatable, the constant may be OP0. */ | |
5914 | if (TREE_CODE (op0) == INTEGER_CST) | |
5915 | { | |
5916 | tree tem = op0; | |
5917 | op0 = op1; | |
5918 | op1 = tem; | |
5919 | tem = t1; | |
5920 | t1 = t2; | |
5921 | t2 = tem; | |
5922 | } | |
5923 | } | |
59adecfa | 5924 | |
f9011d04 RK |
5925 | if (TREE_CODE (op1) != INTEGER_CST) |
5926 | break; | |
5927 | ||
59adecfa RK |
5928 | /* If either OP1 or C are negative, this optimization is not safe for |
5929 | some of the division and remainder types while for others we need | |
5930 | to change the code. */ | |
5931 | if (tree_int_cst_sgn (op1) < 0 || tree_int_cst_sgn (c) < 0) | |
5932 | { | |
5933 | if (code == CEIL_DIV_EXPR) | |
5934 | code = FLOOR_DIV_EXPR; | |
59adecfa RK |
5935 | else if (code == FLOOR_DIV_EXPR) |
5936 | code = CEIL_DIV_EXPR; | |
0629440f RK |
5937 | else if (code != MULT_EXPR |
5938 | && code != CEIL_MOD_EXPR && code != FLOOR_MOD_EXPR) | |
59adecfa RK |
5939 | break; |
5940 | } | |
5941 | ||
12644a9a TM |
5942 | /* If it's a multiply or a division/modulus operation of a multiple |
5943 | of our constant, do the operation and verify it doesn't overflow. */ | |
5944 | if (code == MULT_EXPR | |
43a5d30b | 5945 | || integer_zerop (const_binop (TRUNC_MOD_EXPR, op1, c))) |
dd3f0101 | 5946 | { |
088414c1 | 5947 | op1 = const_binop (code, fold_convert (ctype, op1), |
43a5d30b | 5948 | fold_convert (ctype, c)); |
41ba7ed7 RS |
5949 | /* We allow the constant to overflow with wrapping semantics. */ |
5950 | if (op1 == 0 | |
eeef0e45 | 5951 | || (TREE_OVERFLOW (op1) && !TYPE_OVERFLOW_WRAPS (ctype))) |
dd3f0101 KH |
5952 | break; |
5953 | } | |
12644a9a | 5954 | else |
dd3f0101 | 5955 | break; |
59adecfa | 5956 | |
67ae67ec EB |
5957 | /* If we have an unsigned type, we cannot widen the operation since it |
5958 | will change the result if the original computation overflowed. */ | |
5959 | if (TYPE_UNSIGNED (ctype) && ctype != type) | |
23cdce68 RH |
5960 | break; |
5961 | ||
1baa375f | 5962 | /* If we were able to eliminate our operation from the first side, |
59adecfa RK |
5963 | apply our operation to the second side and reform the PLUS. */ |
5964 | if (t1 != 0 && (TREE_CODE (t1) != code || code == MULT_EXPR)) | |
7f20a5b7 | 5965 | return fold_build2 (tcode, ctype, fold_convert (ctype, t1), op1); |
1baa375f RK |
5966 | |
5967 | /* The last case is if we are a multiply. In that case, we can | |
5968 | apply the distributive law to commute the multiply and addition | |
146b8692 JJ |
5969 | if the multiplication of the constants doesn't overflow |
5970 | and overflow is defined. With undefined overflow | |
5971 | op0 * c might overflow, while (op0 + orig_op1) * c doesn't. */ | |
5972 | if (code == MULT_EXPR && TYPE_OVERFLOW_WRAPS (ctype)) | |
7f20a5b7 KH |
5973 | return fold_build2 (tcode, ctype, |
5974 | fold_build2 (code, ctype, | |
5975 | fold_convert (ctype, op0), | |
5976 | fold_convert (ctype, c)), | |
5977 | op1); | |
1baa375f RK |
5978 | |
5979 | break; | |
5980 | ||
5981 | case MULT_EXPR: | |
5982 | /* We have a special case here if we are doing something like | |
5983 | (C * 8) % 4 since we know that's zero. */ | |
5984 | if ((code == TRUNC_MOD_EXPR || code == CEIL_MOD_EXPR | |
5985 | || code == FLOOR_MOD_EXPR || code == ROUND_MOD_EXPR) | |
3ac8781c RG |
5986 | /* If the multiplication can overflow we cannot optimize this. */ |
5987 | && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (t)) | |
1baa375f | 5988 | && TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST |
43a5d30b | 5989 | && integer_zerop (const_binop (TRUNC_MOD_EXPR, op1, c))) |
beeab17c RG |
5990 | { |
5991 | *strict_overflow_p = true; | |
5992 | return omit_one_operand (type, integer_zero_node, op0); | |
5993 | } | |
1baa375f | 5994 | |
30f7a378 | 5995 | /* ... fall through ... */ |
1baa375f RK |
5996 | |
5997 | case TRUNC_DIV_EXPR: case CEIL_DIV_EXPR: case FLOOR_DIV_EXPR: | |
5998 | case ROUND_DIV_EXPR: case EXACT_DIV_EXPR: | |
5999 | /* If we can extract our operation from the LHS, do so and return a | |
6000 | new operation. Likewise for the RHS from a MULT_EXPR. Otherwise, | |
6001 | do something only if the second operand is a constant. */ | |
6002 | if (same_p | |
6ac01510 ILT |
6003 | && (t1 = extract_muldiv (op0, c, code, wide_type, |
6004 | strict_overflow_p)) != 0) | |
7f20a5b7 KH |
6005 | return fold_build2 (tcode, ctype, fold_convert (ctype, t1), |
6006 | fold_convert (ctype, op1)); | |
1baa375f | 6007 | else if (tcode == MULT_EXPR && code == MULT_EXPR |
6ac01510 ILT |
6008 | && (t1 = extract_muldiv (op1, c, code, wide_type, |
6009 | strict_overflow_p)) != 0) | |
7f20a5b7 KH |
6010 | return fold_build2 (tcode, ctype, fold_convert (ctype, op0), |
6011 | fold_convert (ctype, t1)); | |
1baa375f RK |
6012 | else if (TREE_CODE (op1) != INTEGER_CST) |
6013 | return 0; | |
6014 | ||
6015 | /* If these are the same operation types, we can associate them | |
6016 | assuming no overflow. */ | |
d35936ab RG |
6017 | if (tcode == code) |
6018 | { | |
6019 | double_int mul; | |
27bcd47c LC |
6020 | bool overflow_p; |
6021 | unsigned prec = TYPE_PRECISION (ctype); | |
6022 | bool uns = TYPE_UNSIGNED (ctype); | |
6023 | double_int diop1 = tree_to_double_int (op1).ext (prec, uns); | |
6024 | double_int dic = tree_to_double_int (c).ext (prec, uns); | |
6025 | mul = diop1.mul_with_sign (dic, false, &overflow_p); | |
6026 | overflow_p = ((!uns && overflow_p) | |
d35936ab RG |
6027 | | TREE_OVERFLOW (c) | TREE_OVERFLOW (op1)); |
6028 | if (!double_int_fits_to_tree_p (ctype, mul) | |
27bcd47c | 6029 | && ((uns && tcode != MULT_EXPR) || !uns)) |
d35936ab RG |
6030 | overflow_p = 1; |
6031 | if (!overflow_p) | |
6032 | return fold_build2 (tcode, ctype, fold_convert (ctype, op0), | |
6033 | double_int_to_tree (ctype, mul)); | |
6034 | } | |
1baa375f RK |
6035 | |
6036 | /* If these operations "cancel" each other, we have the main | |
6037 | optimizations of this pass, which occur when either constant is a | |
6038 | multiple of the other, in which case we replace this with either an | |
b6cc0a72 | 6039 | operation or CODE or TCODE. |
8e1ca098 | 6040 | |
56099f00 RG |
6041 | If we have an unsigned type, we cannot do this since it will change |
6042 | the result if the original computation overflowed. */ | |
6043 | if (TYPE_OVERFLOW_UNDEFINED (ctype) | |
8e1ca098 RH |
6044 | && ((code == MULT_EXPR && tcode == EXACT_DIV_EXPR) |
6045 | || (tcode == MULT_EXPR | |
6046 | && code != TRUNC_MOD_EXPR && code != CEIL_MOD_EXPR | |
e6ebd07f ZD |
6047 | && code != FLOOR_MOD_EXPR && code != ROUND_MOD_EXPR |
6048 | && code != MULT_EXPR))) | |
1baa375f | 6049 | { |
43a5d30b | 6050 | if (integer_zerop (const_binop (TRUNC_MOD_EXPR, op1, c))) |
6ac01510 ILT |
6051 | { |
6052 | if (TYPE_OVERFLOW_UNDEFINED (ctype)) | |
6053 | *strict_overflow_p = true; | |
6054 | return fold_build2 (tcode, ctype, fold_convert (ctype, op0), | |
6055 | fold_convert (ctype, | |
6056 | const_binop (TRUNC_DIV_EXPR, | |
43a5d30b | 6057 | op1, c))); |
6ac01510 | 6058 | } |
43a5d30b | 6059 | else if (integer_zerop (const_binop (TRUNC_MOD_EXPR, c, op1))) |
6ac01510 ILT |
6060 | { |
6061 | if (TYPE_OVERFLOW_UNDEFINED (ctype)) | |
6062 | *strict_overflow_p = true; | |
6063 | return fold_build2 (code, ctype, fold_convert (ctype, op0), | |
6064 | fold_convert (ctype, | |
6065 | const_binop (TRUNC_DIV_EXPR, | |
43a5d30b | 6066 | c, op1))); |
6ac01510 | 6067 | } |
1baa375f RK |
6068 | } |
6069 | break; | |
6070 | ||
6071 | default: | |
6072 | break; | |
6073 | } | |
6074 | ||
6075 | return 0; | |
6076 | } | |
6077 | \f | |
f628873f | 6078 | /* Return a node which has the indicated constant VALUE (either 0 or |
544d960a AS |
6079 | 1 for scalars or {-1,-1,..} or {0,0,...} for vectors), |
6080 | and is of the indicated TYPE. */ | |
f628873f | 6081 | |
e9ea8bd5 | 6082 | tree |
544d960a | 6083 | constant_boolean_node (bool value, tree type) |
f628873f MM |
6084 | { |
6085 | if (type == integer_type_node) | |
6086 | return value ? integer_one_node : integer_zero_node; | |
9bb80bb2 RS |
6087 | else if (type == boolean_type_node) |
6088 | return value ? boolean_true_node : boolean_false_node; | |
544d960a AS |
6089 | else if (TREE_CODE (type) == VECTOR_TYPE) |
6090 | return build_vector_from_val (type, | |
6091 | build_int_cst (TREE_TYPE (type), | |
6092 | value ? -1 : 0)); | |
b6cc0a72 | 6093 | else |
544d960a | 6094 | return fold_convert (type, value ? integer_one_node : integer_zero_node); |
f628873f MM |
6095 | } |
6096 | ||
020d90ee | 6097 | |
1f77b5da | 6098 | /* Transform `a + (b ? x : y)' into `b ? (a + x) : (a + y)'. |
68626d4f MM |
6099 | Transform, `a + (x < y)' into `(x < y) ? (a + 1) : (a + 0)'. Here |
6100 | CODE corresponds to the `+', COND to the `(b ? x : y)' or `(x < y)' | |
cc2902df | 6101 | expression, and ARG to `a'. If COND_FIRST_P is nonzero, then the |
68626d4f MM |
6102 | COND is the first argument to CODE; otherwise (as in the example |
6103 | given here), it is the second argument. TYPE is the type of the | |
2b8a92de | 6104 | original expression. Return NULL_TREE if no simplification is |
b3e65ebb | 6105 | possible. */ |
68626d4f MM |
6106 | |
6107 | static tree | |
db3927fb AH |
6108 | fold_binary_op_with_conditional_arg (location_t loc, |
6109 | enum tree_code code, | |
e9da788c KH |
6110 | tree type, tree op0, tree op1, |
6111 | tree cond, tree arg, int cond_first_p) | |
68626d4f | 6112 | { |
e9da788c | 6113 | tree cond_type = cond_first_p ? TREE_TYPE (op0) : TREE_TYPE (op1); |
92db3ec9 | 6114 | tree arg_type = cond_first_p ? TREE_TYPE (op1) : TREE_TYPE (op0); |
68626d4f MM |
6115 | tree test, true_value, false_value; |
6116 | tree lhs = NULL_TREE; | |
6117 | tree rhs = NULL_TREE; | |
2e2e628b | 6118 | enum tree_code cond_code = COND_EXPR; |
b3e65ebb | 6119 | |
2e2e628b MG |
6120 | if (TREE_CODE (cond) == COND_EXPR |
6121 | || TREE_CODE (cond) == VEC_COND_EXPR) | |
68626d4f MM |
6122 | { |
6123 | test = TREE_OPERAND (cond, 0); | |
6124 | true_value = TREE_OPERAND (cond, 1); | |
6125 | false_value = TREE_OPERAND (cond, 2); | |
6126 | /* If this operand throws an expression, then it does not make | |
6127 | sense to try to perform a logical or arithmetic operation | |
f4085d4c | 6128 | involving it. */ |
68626d4f | 6129 | if (VOID_TYPE_P (TREE_TYPE (true_value))) |
f4085d4c | 6130 | lhs = true_value; |
68626d4f | 6131 | if (VOID_TYPE_P (TREE_TYPE (false_value))) |
f4085d4c | 6132 | rhs = false_value; |
68626d4f MM |
6133 | } |
6134 | else | |
6135 | { | |
6136 | tree testtype = TREE_TYPE (cond); | |
6137 | test = cond; | |
1b0f3e79 RS |
6138 | true_value = constant_boolean_node (true, testtype); |
6139 | false_value = constant_boolean_node (false, testtype); | |
68626d4f | 6140 | } |
dd3f0101 | 6141 | |
2e2e628b MG |
6142 | if (TREE_CODE (TREE_TYPE (test)) == VECTOR_TYPE) |
6143 | cond_code = VEC_COND_EXPR; | |
6144 | ||
9e9ef331 | 6145 | /* This transformation is only worthwhile if we don't have to wrap ARG |
dd64a6f7 EB |
6146 | in a SAVE_EXPR and the operation can be simplified without recursing |
6147 | on at least one of the branches once its pushed inside the COND_EXPR. */ | |
9e9ef331 EB |
6148 | if (!TREE_CONSTANT (arg) |
6149 | && (TREE_SIDE_EFFECTS (arg) | |
dd64a6f7 | 6150 | || TREE_CODE (arg) == COND_EXPR || TREE_CODE (arg) == VEC_COND_EXPR |
9e9ef331 EB |
6151 | || TREE_CONSTANT (true_value) || TREE_CONSTANT (false_value))) |
6152 | return NULL_TREE; | |
6153 | ||
db3927fb | 6154 | arg = fold_convert_loc (loc, arg_type, arg); |
68626d4f | 6155 | if (lhs == 0) |
3b70b82a | 6156 | { |
db3927fb | 6157 | true_value = fold_convert_loc (loc, cond_type, true_value); |
6405f32f | 6158 | if (cond_first_p) |
db3927fb | 6159 | lhs = fold_build2_loc (loc, code, type, true_value, arg); |
6405f32f | 6160 | else |
db3927fb | 6161 | lhs = fold_build2_loc (loc, code, type, arg, true_value); |
3b70b82a | 6162 | } |
68626d4f | 6163 | if (rhs == 0) |
3b70b82a | 6164 | { |
db3927fb | 6165 | false_value = fold_convert_loc (loc, cond_type, false_value); |
6405f32f | 6166 | if (cond_first_p) |
db3927fb | 6167 | rhs = fold_build2_loc (loc, code, type, false_value, arg); |
6405f32f | 6168 | else |
db3927fb | 6169 | rhs = fold_build2_loc (loc, code, type, arg, false_value); |
3b70b82a | 6170 | } |
f4085d4c | 6171 | |
9e9ef331 EB |
6172 | /* Check that we have simplified at least one of the branches. */ |
6173 | if (!TREE_CONSTANT (arg) && !TREE_CONSTANT (lhs) && !TREE_CONSTANT (rhs)) | |
6174 | return NULL_TREE; | |
6175 | ||
2e2e628b | 6176 | return fold_build3_loc (loc, cond_code, type, test, lhs, rhs); |
68626d4f MM |
6177 | } |
6178 | ||
ab87f8c8 | 6179 | \f |
71925bc0 RS |
6180 | /* Subroutine of fold() that checks for the addition of +/- 0.0. |
6181 | ||
6182 | If !NEGATE, return true if ADDEND is +/-0.0 and, for all X of type | |
6183 | TYPE, X + ADDEND is the same as X. If NEGATE, return true if X - | |
6184 | ADDEND is the same as X. | |
6185 | ||
cc2902df | 6186 | X + 0 and X - 0 both give X when X is NaN, infinite, or nonzero |
71925bc0 RS |
6187 | and finite. The problematic cases are when X is zero, and its mode |
6188 | has signed zeros. In the case of rounding towards -infinity, | |
6189 | X - 0 is not the same as X because 0 - 0 is -0. In other rounding | |
6190 | modes, X + 0 is not the same as X because -0 + 0 is 0. */ | |
6191 | ||
2dc0f633 | 6192 | bool |
ac545c64 | 6193 | fold_real_zero_addition_p (const_tree type, const_tree addend, int negate) |
71925bc0 RS |
6194 | { |
6195 | if (!real_zerop (addend)) | |
6196 | return false; | |
6197 | ||
3bc400cd RS |
6198 | /* Don't allow the fold with -fsignaling-nans. */ |
6199 | if (HONOR_SNANS (TYPE_MODE (type))) | |
6200 | return false; | |
6201 | ||
71925bc0 RS |
6202 | /* Allow the fold if zeros aren't signed, or their sign isn't important. */ |
6203 | if (!HONOR_SIGNED_ZEROS (TYPE_MODE (type))) | |
6204 | return true; | |
6205 | ||
948a1fd9 MG |
6206 | /* In a vector or complex, we would need to check the sign of all zeros. */ |
6207 | if (TREE_CODE (addend) != REAL_CST) | |
6208 | return false; | |
6209 | ||
71925bc0 | 6210 | /* Treat x + -0 as x - 0 and x - -0 as x + 0. */ |
948a1fd9 | 6211 | if (REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (addend))) |
71925bc0 RS |
6212 | negate = !negate; |
6213 | ||
6214 | /* The mode has signed zeros, and we have to honor their sign. | |
6215 | In this situation, there is only one case we can return true for. | |
6216 | X - 0 is the same as X unless rounding towards -infinity is | |
6217 | supported. */ | |
6218 | return negate && !HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (type)); | |
6219 | } | |
6220 | ||
c876997f RS |
6221 | /* Subroutine of fold() that checks comparisons of built-in math |
6222 | functions against real constants. | |
6223 | ||
6224 | FCODE is the DECL_FUNCTION_CODE of the built-in, CODE is the comparison | |
6225 | operator: EQ_EXPR, NE_EXPR, GT_EXPR, LT_EXPR, GE_EXPR or LE_EXPR. TYPE | |
6226 | is the type of the result and ARG0 and ARG1 are the operands of the | |
6227 | comparison. ARG1 must be a TREE_REAL_CST. | |
6228 | ||
6229 | The function returns the constant folded tree if a simplification | |
6230 | can be made, and NULL_TREE otherwise. */ | |
6231 | ||
6232 | static tree | |
db3927fb AH |
6233 | fold_mathfn_compare (location_t loc, |
6234 | enum built_in_function fcode, enum tree_code code, | |
75040a04 | 6235 | tree type, tree arg0, tree arg1) |
c876997f RS |
6236 | { |
6237 | REAL_VALUE_TYPE c; | |
6238 | ||
82b4201f | 6239 | if (BUILTIN_SQRT_P (fcode)) |
c876997f | 6240 | { |
5039610b | 6241 | tree arg = CALL_EXPR_ARG (arg0, 0); |
c876997f RS |
6242 | enum machine_mode mode = TYPE_MODE (TREE_TYPE (arg0)); |
6243 | ||
6244 | c = TREE_REAL_CST (arg1); | |
6245 | if (REAL_VALUE_NEGATIVE (c)) | |
6246 | { | |
6247 | /* sqrt(x) < y is always false, if y is negative. */ | |
6248 | if (code == EQ_EXPR || code == LT_EXPR || code == LE_EXPR) | |
db3927fb | 6249 | return omit_one_operand_loc (loc, type, integer_zero_node, arg); |
c876997f RS |
6250 | |
6251 | /* sqrt(x) > y is always true, if y is negative and we | |
6252 | don't care about NaNs, i.e. negative values of x. */ | |
6253 | if (code == NE_EXPR || !HONOR_NANS (mode)) | |
db3927fb | 6254 | return omit_one_operand_loc (loc, type, integer_one_node, arg); |
c876997f RS |
6255 | |
6256 | /* sqrt(x) > y is the same as x >= 0, if y is negative. */ | |
db3927fb | 6257 | return fold_build2_loc (loc, GE_EXPR, type, arg, |
7f20a5b7 | 6258 | build_real (TREE_TYPE (arg), dconst0)); |
c876997f RS |
6259 | } |
6260 | else if (code == GT_EXPR || code == GE_EXPR) | |
6261 | { | |
6262 | REAL_VALUE_TYPE c2; | |
6263 | ||
6264 | REAL_ARITHMETIC (c2, MULT_EXPR, c, c); | |
6265 | real_convert (&c2, mode, &c2); | |
6266 | ||
6267 | if (REAL_VALUE_ISINF (c2)) | |
6268 | { | |
6269 | /* sqrt(x) > y is x == +Inf, when y is very large. */ | |
6270 | if (HONOR_INFINITIES (mode)) | |
db3927fb | 6271 | return fold_build2_loc (loc, EQ_EXPR, type, arg, |
7f20a5b7 | 6272 | build_real (TREE_TYPE (arg), c2)); |
c876997f RS |
6273 | |
6274 | /* sqrt(x) > y is always false, when y is very large | |
6275 | and we don't care about infinities. */ | |
db3927fb | 6276 | return omit_one_operand_loc (loc, type, integer_zero_node, arg); |
c876997f RS |
6277 | } |
6278 | ||
6279 | /* sqrt(x) > c is the same as x > c*c. */ | |
db3927fb | 6280 | return fold_build2_loc (loc, code, type, arg, |
7f20a5b7 | 6281 | build_real (TREE_TYPE (arg), c2)); |
c876997f RS |
6282 | } |
6283 | else if (code == LT_EXPR || code == LE_EXPR) | |
6284 | { | |
6285 | REAL_VALUE_TYPE c2; | |
6286 | ||
6287 | REAL_ARITHMETIC (c2, MULT_EXPR, c, c); | |
6288 | real_convert (&c2, mode, &c2); | |
6289 | ||
6290 | if (REAL_VALUE_ISINF (c2)) | |
6291 | { | |
6292 | /* sqrt(x) < y is always true, when y is a very large | |
6293 | value and we don't care about NaNs or Infinities. */ | |
6294 | if (! HONOR_NANS (mode) && ! HONOR_INFINITIES (mode)) | |
db3927fb | 6295 | return omit_one_operand_loc (loc, type, integer_one_node, arg); |
c876997f RS |
6296 | |
6297 | /* sqrt(x) < y is x != +Inf when y is very large and we | |
6298 | don't care about NaNs. */ | |
6299 | if (! HONOR_NANS (mode)) | |
db3927fb | 6300 | return fold_build2_loc (loc, NE_EXPR, type, arg, |
7f20a5b7 | 6301 | build_real (TREE_TYPE (arg), c2)); |
c876997f RS |
6302 | |
6303 | /* sqrt(x) < y is x >= 0 when y is very large and we | |
6304 | don't care about Infinities. */ | |
6305 | if (! HONOR_INFINITIES (mode)) | |
db3927fb | 6306 | return fold_build2_loc (loc, GE_EXPR, type, arg, |
7f20a5b7 | 6307 | build_real (TREE_TYPE (arg), dconst0)); |
c876997f RS |
6308 | |
6309 | /* sqrt(x) < y is x >= 0 && x != +Inf, when y is large. */ | |
c876997f | 6310 | arg = save_expr (arg); |
db3927fb AH |
6311 | return fold_build2_loc (loc, TRUTH_ANDIF_EXPR, type, |
6312 | fold_build2_loc (loc, GE_EXPR, type, arg, | |
7f20a5b7 KH |
6313 | build_real (TREE_TYPE (arg), |
6314 | dconst0)), | |
db3927fb | 6315 | fold_build2_loc (loc, NE_EXPR, type, arg, |
7f20a5b7 KH |
6316 | build_real (TREE_TYPE (arg), |
6317 | c2))); | |
c876997f RS |
6318 | } |
6319 | ||
6320 | /* sqrt(x) < c is the same as x < c*c, if we ignore NaNs. */ | |
6321 | if (! HONOR_NANS (mode)) | |
db3927fb | 6322 | return fold_build2_loc (loc, code, type, arg, |
7f20a5b7 | 6323 | build_real (TREE_TYPE (arg), c2)); |
c876997f RS |
6324 | |
6325 | /* sqrt(x) < c is the same as x >= 0 && x < c*c. */ | |
c99c0026 EB |
6326 | arg = save_expr (arg); |
6327 | return fold_build2_loc (loc, TRUTH_ANDIF_EXPR, type, | |
db3927fb | 6328 | fold_build2_loc (loc, GE_EXPR, type, arg, |
7f20a5b7 KH |
6329 | build_real (TREE_TYPE (arg), |
6330 | dconst0)), | |
db3927fb | 6331 | fold_build2_loc (loc, code, type, arg, |
7f20a5b7 KH |
6332 | build_real (TREE_TYPE (arg), |
6333 | c2))); | |
c876997f RS |
6334 | } |
6335 | } | |
6336 | ||
6337 | return NULL_TREE; | |
6338 | } | |
6339 | ||
9ddae796 RS |
6340 | /* Subroutine of fold() that optimizes comparisons against Infinities, |
6341 | either +Inf or -Inf. | |
6342 | ||
6343 | CODE is the comparison operator: EQ_EXPR, NE_EXPR, GT_EXPR, LT_EXPR, | |
6344 | GE_EXPR or LE_EXPR. TYPE is the type of the result and ARG0 and ARG1 | |
6345 | are the operands of the comparison. ARG1 must be a TREE_REAL_CST. | |
6346 | ||
6347 | The function returns the constant folded tree if a simplification | |
6348 | can be made, and NULL_TREE otherwise. */ | |
6349 | ||
6350 | static tree | |
db3927fb AH |
6351 | fold_inf_compare (location_t loc, enum tree_code code, tree type, |
6352 | tree arg0, tree arg1) | |
9ddae796 | 6353 | { |
18c2511c RS |
6354 | enum machine_mode mode; |
6355 | REAL_VALUE_TYPE max; | |
6356 | tree temp; | |
6357 | bool neg; | |
6358 | ||
6359 | mode = TYPE_MODE (TREE_TYPE (arg0)); | |
6360 | ||
9ddae796 | 6361 | /* For negative infinity swap the sense of the comparison. */ |
18c2511c RS |
6362 | neg = REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg1)); |
6363 | if (neg) | |
9ddae796 RS |
6364 | code = swap_tree_comparison (code); |
6365 | ||
6366 | switch (code) | |
6367 | { | |
6368 | case GT_EXPR: | |
6369 | /* x > +Inf is always false, if with ignore sNANs. */ | |
18c2511c | 6370 | if (HONOR_SNANS (mode)) |
9ddae796 | 6371 | return NULL_TREE; |
db3927fb | 6372 | return omit_one_operand_loc (loc, type, integer_zero_node, arg0); |
9ddae796 RS |
6373 | |
6374 | case LE_EXPR: | |
6375 | /* x <= +Inf is always true, if we don't case about NaNs. */ | |
18c2511c | 6376 | if (! HONOR_NANS (mode)) |
db3927fb | 6377 | return omit_one_operand_loc (loc, type, integer_one_node, arg0); |
9ddae796 RS |
6378 | |
6379 | /* x <= +Inf is the same as x == x, i.e. isfinite(x). */ | |
c99c0026 EB |
6380 | arg0 = save_expr (arg0); |
6381 | return fold_build2_loc (loc, EQ_EXPR, type, arg0, arg0); | |
9ddae796 | 6382 | |
18c2511c RS |
6383 | case EQ_EXPR: |
6384 | case GE_EXPR: | |
6385 | /* x == +Inf and x >= +Inf are always equal to x > DBL_MAX. */ | |
6386 | real_maxval (&max, neg, mode); | |
db3927fb | 6387 | return fold_build2_loc (loc, neg ? LT_EXPR : GT_EXPR, type, |
7f20a5b7 | 6388 | arg0, build_real (TREE_TYPE (arg0), max)); |
18c2511c RS |
6389 | |
6390 | case LT_EXPR: | |
6391 | /* x < +Inf is always equal to x <= DBL_MAX. */ | |
6392 | real_maxval (&max, neg, mode); | |
db3927fb | 6393 | return fold_build2_loc (loc, neg ? GE_EXPR : LE_EXPR, type, |
7f20a5b7 | 6394 | arg0, build_real (TREE_TYPE (arg0), max)); |
18c2511c RS |
6395 | |
6396 | case NE_EXPR: | |
6397 | /* x != +Inf is always equal to !(x > DBL_MAX). */ | |
6398 | real_maxval (&max, neg, mode); | |
6399 | if (! HONOR_NANS (mode)) | |
db3927fb | 6400 | return fold_build2_loc (loc, neg ? GE_EXPR : LE_EXPR, type, |
7f20a5b7 | 6401 | arg0, build_real (TREE_TYPE (arg0), max)); |
3100d647 | 6402 | |
db3927fb | 6403 | temp = fold_build2_loc (loc, neg ? LT_EXPR : GT_EXPR, type, |
7f20a5b7 | 6404 | arg0, build_real (TREE_TYPE (arg0), max)); |
db3927fb | 6405 | return fold_build1_loc (loc, TRUTH_NOT_EXPR, type, temp); |
9ddae796 RS |
6406 | |
6407 | default: | |
6408 | break; | |
6409 | } | |
6410 | ||
6411 | return NULL_TREE; | |
6412 | } | |
71925bc0 | 6413 | |
8dc2384c | 6414 | /* Subroutine of fold() that optimizes comparisons of a division by |
1ea7e6ad | 6415 | a nonzero integer constant against an integer constant, i.e. |
8dc2384c RS |
6416 | X/C1 op C2. |
6417 | ||
6418 | CODE is the comparison operator: EQ_EXPR, NE_EXPR, GT_EXPR, LT_EXPR, | |
6419 | GE_EXPR or LE_EXPR. TYPE is the type of the result and ARG0 and ARG1 | |
6420 | are the operands of the comparison. ARG1 must be a TREE_REAL_CST. | |
6421 | ||
6422 | The function returns the constant folded tree if a simplification | |
6423 | can be made, and NULL_TREE otherwise. */ | |
6424 | ||
6425 | static tree | |
db3927fb AH |
6426 | fold_div_compare (location_t loc, |
6427 | enum tree_code code, tree type, tree arg0, tree arg1) | |
8dc2384c RS |
6428 | { |
6429 | tree prod, tmp, hi, lo; | |
6430 | tree arg00 = TREE_OPERAND (arg0, 0); | |
6431 | tree arg01 = TREE_OPERAND (arg0, 1); | |
9589f23e | 6432 | double_int val; |
6b7283ac | 6433 | bool unsigned_p = TYPE_UNSIGNED (TREE_TYPE (arg0)); |
d56ee62b | 6434 | bool neg_overflow; |
9be0ac8c | 6435 | bool overflow; |
8dc2384c RS |
6436 | |
6437 | /* We have to do this the hard way to detect unsigned overflow. | |
d35936ab | 6438 | prod = int_const_binop (MULT_EXPR, arg01, arg1); */ |
9be0ac8c LC |
6439 | val = TREE_INT_CST (arg01) |
6440 | .mul_with_sign (TREE_INT_CST (arg1), unsigned_p, &overflow); | |
9589f23e | 6441 | prod = force_fit_type_double (TREE_TYPE (arg00), val, -1, overflow); |
d56ee62b | 6442 | neg_overflow = false; |
8dc2384c | 6443 | |
6b7283ac | 6444 | if (unsigned_p) |
8dc2384c | 6445 | { |
000d8d44 | 6446 | tmp = int_const_binop (MINUS_EXPR, arg01, |
d35936ab | 6447 | build_int_cst (TREE_TYPE (arg01), 1)); |
8dc2384c RS |
6448 | lo = prod; |
6449 | ||
d35936ab | 6450 | /* Likewise hi = int_const_binop (PLUS_EXPR, prod, tmp). */ |
9be0ac8c LC |
6451 | val = TREE_INT_CST (prod) |
6452 | .add_with_sign (TREE_INT_CST (tmp), unsigned_p, &overflow); | |
9589f23e | 6453 | hi = force_fit_type_double (TREE_TYPE (arg00), val, |
d95787e6 | 6454 | -1, overflow | TREE_OVERFLOW (prod)); |
8dc2384c RS |
6455 | } |
6456 | else if (tree_int_cst_sgn (arg01) >= 0) | |
6457 | { | |
000d8d44 | 6458 | tmp = int_const_binop (MINUS_EXPR, arg01, |
d35936ab | 6459 | build_int_cst (TREE_TYPE (arg01), 1)); |
8dc2384c RS |
6460 | switch (tree_int_cst_sgn (arg1)) |
6461 | { | |
6462 | case -1: | |
d56ee62b | 6463 | neg_overflow = true; |
d35936ab | 6464 | lo = int_const_binop (MINUS_EXPR, prod, tmp); |
8dc2384c RS |
6465 | hi = prod; |
6466 | break; | |
6467 | ||
6468 | case 0: | |
6469 | lo = fold_negate_const (tmp, TREE_TYPE (arg0)); | |
6470 | hi = tmp; | |
6471 | break; | |
6472 | ||
6473 | case 1: | |
d35936ab | 6474 | hi = int_const_binop (PLUS_EXPR, prod, tmp); |
8dc2384c RS |
6475 | lo = prod; |
6476 | break; | |
6477 | ||
6478 | default: | |
0bccc606 | 6479 | gcc_unreachable (); |
8dc2384c RS |
6480 | } |
6481 | } | |
6482 | else | |
6483 | { | |
d2e74f6f RS |
6484 | /* A negative divisor reverses the relational operators. */ |
6485 | code = swap_tree_comparison (code); | |
6486 | ||
000d8d44 | 6487 | tmp = int_const_binop (PLUS_EXPR, arg01, |
d35936ab | 6488 | build_int_cst (TREE_TYPE (arg01), 1)); |
8dc2384c RS |
6489 | switch (tree_int_cst_sgn (arg1)) |
6490 | { | |
6491 | case -1: | |
d35936ab | 6492 | hi = int_const_binop (MINUS_EXPR, prod, tmp); |
8dc2384c RS |
6493 | lo = prod; |
6494 | break; | |
6495 | ||
6496 | case 0: | |
6497 | hi = fold_negate_const (tmp, TREE_TYPE (arg0)); | |
6498 | lo = tmp; | |
6499 | break; | |
6500 | ||
6501 | case 1: | |
d56ee62b | 6502 | neg_overflow = true; |
d35936ab | 6503 | lo = int_const_binop (PLUS_EXPR, prod, tmp); |
8dc2384c RS |
6504 | hi = prod; |
6505 | break; | |
6506 | ||
6507 | default: | |
0bccc606 | 6508 | gcc_unreachable (); |
8dc2384c RS |
6509 | } |
6510 | } | |
6511 | ||
6512 | switch (code) | |
6513 | { | |
6514 | case EQ_EXPR: | |
6515 | if (TREE_OVERFLOW (lo) && TREE_OVERFLOW (hi)) | |
db3927fb | 6516 | return omit_one_operand_loc (loc, type, integer_zero_node, arg00); |
8dc2384c | 6517 | if (TREE_OVERFLOW (hi)) |
db3927fb | 6518 | return fold_build2_loc (loc, GE_EXPR, type, arg00, lo); |
8dc2384c | 6519 | if (TREE_OVERFLOW (lo)) |
db3927fb AH |
6520 | return fold_build2_loc (loc, LE_EXPR, type, arg00, hi); |
6521 | return build_range_check (loc, type, arg00, 1, lo, hi); | |
8dc2384c RS |
6522 | |
6523 | case NE_EXPR: | |
6524 | if (TREE_OVERFLOW (lo) && TREE_OVERFLOW (hi)) | |
db3927fb | 6525 | return omit_one_operand_loc (loc, type, integer_one_node, arg00); |
8dc2384c | 6526 | if (TREE_OVERFLOW (hi)) |
db3927fb | 6527 | return fold_build2_loc (loc, LT_EXPR, type, arg00, lo); |
8dc2384c | 6528 | if (TREE_OVERFLOW (lo)) |
db3927fb AH |
6529 | return fold_build2_loc (loc, GT_EXPR, type, arg00, hi); |
6530 | return build_range_check (loc, type, arg00, 0, lo, hi); | |
8dc2384c RS |
6531 | |
6532 | case LT_EXPR: | |
6533 | if (TREE_OVERFLOW (lo)) | |
d56ee62b RS |
6534 | { |
6535 | tmp = neg_overflow ? integer_zero_node : integer_one_node; | |
db3927fb | 6536 | return omit_one_operand_loc (loc, type, tmp, arg00); |
d56ee62b | 6537 | } |
db3927fb | 6538 | return fold_build2_loc (loc, LT_EXPR, type, arg00, lo); |
8dc2384c RS |
6539 | |
6540 | case LE_EXPR: | |
6541 | if (TREE_OVERFLOW (hi)) | |
d56ee62b RS |
6542 | { |
6543 | tmp = neg_overflow ? integer_zero_node : integer_one_node; | |
db3927fb | 6544 | return omit_one_operand_loc (loc, type, tmp, arg00); |
d56ee62b | 6545 | } |
db3927fb | 6546 | return fold_build2_loc (loc, LE_EXPR, type, arg00, hi); |
8dc2384c RS |
6547 | |
6548 | case GT_EXPR: | |
6549 | if (TREE_OVERFLOW (hi)) | |
d56ee62b RS |
6550 | { |
6551 | tmp = neg_overflow ? integer_one_node : integer_zero_node; | |
db3927fb | 6552 | return omit_one_operand_loc (loc, type, tmp, arg00); |
d56ee62b | 6553 | } |
db3927fb | 6554 | return fold_build2_loc (loc, GT_EXPR, type, arg00, hi); |
8dc2384c RS |
6555 | |
6556 | case GE_EXPR: | |
6557 | if (TREE_OVERFLOW (lo)) | |
d56ee62b RS |
6558 | { |
6559 | tmp = neg_overflow ? integer_one_node : integer_zero_node; | |
db3927fb | 6560 | return omit_one_operand_loc (loc, type, tmp, arg00); |
d56ee62b | 6561 | } |
db3927fb | 6562 | return fold_build2_loc (loc, GE_EXPR, type, arg00, lo); |
8dc2384c RS |
6563 | |
6564 | default: | |
6565 | break; | |
6566 | } | |
6567 | ||
6568 | return NULL_TREE; | |
6569 | } | |
6570 | ||
6571 | ||
7960bf22 | 6572 | /* If CODE with arguments ARG0 and ARG1 represents a single bit |
a94400fd KH |
6573 | equality/inequality test, then return a simplified form of the test |
6574 | using a sign testing. Otherwise return NULL. TYPE is the desired | |
6575 | result type. */ | |
d1822754 | 6576 | |
a94400fd | 6577 | static tree |
db3927fb AH |
6578 | fold_single_bit_test_into_sign_test (location_t loc, |
6579 | enum tree_code code, tree arg0, tree arg1, | |
a94400fd | 6580 | tree result_type) |
7960bf22 | 6581 | { |
7960bf22 JL |
6582 | /* If this is testing a single bit, we can optimize the test. */ |
6583 | if ((code == NE_EXPR || code == EQ_EXPR) | |
6584 | && TREE_CODE (arg0) == BIT_AND_EXPR && integer_zerop (arg1) | |
6585 | && integer_pow2p (TREE_OPERAND (arg0, 1))) | |
6586 | { | |
7960bf22 JL |
6587 | /* If we have (A & C) != 0 where C is the sign bit of A, convert |
6588 | this into A < 0. Similarly for (A & C) == 0 into A >= 0. */ | |
a94400fd KH |
6589 | tree arg00 = sign_bit_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg0, 1)); |
6590 | ||
1f7a8dcc RS |
6591 | if (arg00 != NULL_TREE |
6592 | /* This is only a win if casting to a signed type is cheap, | |
6593 | i.e. when arg00's type is not a partial mode. */ | |
6594 | && TYPE_PRECISION (TREE_TYPE (arg00)) | |
6595 | == GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (arg00)))) | |
7960bf22 | 6596 | { |
12753674 | 6597 | tree stype = signed_type_for (TREE_TYPE (arg00)); |
db3927fb AH |
6598 | return fold_build2_loc (loc, code == EQ_EXPR ? GE_EXPR : LT_EXPR, |
6599 | result_type, | |
6600 | fold_convert_loc (loc, stype, arg00), | |
57decb7e | 6601 | build_int_cst (stype, 0)); |
7960bf22 | 6602 | } |
a94400fd KH |
6603 | } |
6604 | ||
6605 | return NULL_TREE; | |
6606 | } | |
6607 | ||
6608 | /* If CODE with arguments ARG0 and ARG1 represents a single bit | |
6609 | equality/inequality test, then return a simplified form of | |
6610 | the test using shifts and logical operations. Otherwise return | |
6611 | NULL. TYPE is the desired result type. */ | |
6612 | ||
6613 | tree | |
db3927fb AH |
6614 | fold_single_bit_test (location_t loc, enum tree_code code, |
6615 | tree arg0, tree arg1, tree result_type) | |
a94400fd KH |
6616 | { |
6617 | /* If this is testing a single bit, we can optimize the test. */ | |
6618 | if ((code == NE_EXPR || code == EQ_EXPR) | |
6619 | && TREE_CODE (arg0) == BIT_AND_EXPR && integer_zerop (arg1) | |
6620 | && integer_pow2p (TREE_OPERAND (arg0, 1))) | |
6621 | { | |
6622 | tree inner = TREE_OPERAND (arg0, 0); | |
6623 | tree type = TREE_TYPE (arg0); | |
6624 | int bitnum = tree_log2 (TREE_OPERAND (arg0, 1)); | |
6625 | enum machine_mode operand_mode = TYPE_MODE (type); | |
6626 | int ops_unsigned; | |
6627 | tree signed_type, unsigned_type, intermediate_type; | |
000d8d44 | 6628 | tree tem, one; |
a94400fd KH |
6629 | |
6630 | /* First, see if we can fold the single bit test into a sign-bit | |
6631 | test. */ | |
db3927fb | 6632 | tem = fold_single_bit_test_into_sign_test (loc, code, arg0, arg1, |
a94400fd KH |
6633 | result_type); |
6634 | if (tem) | |
6635 | return tem; | |
c87d821b | 6636 | |
d1822754 | 6637 | /* Otherwise we have (A & C) != 0 where C is a single bit, |
7960bf22 JL |
6638 | convert that into ((A >> C2) & 1). Where C2 = log2(C). |
6639 | Similarly for (A & C) == 0. */ | |
6640 | ||
6641 | /* If INNER is a right shift of a constant and it plus BITNUM does | |
6642 | not overflow, adjust BITNUM and INNER. */ | |
6643 | if (TREE_CODE (inner) == RSHIFT_EXPR | |
6644 | && TREE_CODE (TREE_OPERAND (inner, 1)) == INTEGER_CST | |
26d75703 | 6645 | && host_integerp (TREE_OPERAND (inner, 1), 1) |
7960bf22 | 6646 | && bitnum < TYPE_PRECISION (type) |
26d75703 RB |
6647 | && (TREE_INT_CST_LOW (TREE_OPERAND (inner, 1)) |
6648 | < (unsigned) (TYPE_PRECISION (type) - bitnum))) | |
7960bf22 JL |
6649 | { |
6650 | bitnum += TREE_INT_CST_LOW (TREE_OPERAND (inner, 1)); | |
6651 | inner = TREE_OPERAND (inner, 0); | |
6652 | } | |
6653 | ||
6654 | /* If we are going to be able to omit the AND below, we must do our | |
6655 | operations as unsigned. If we must use the AND, we have a choice. | |
6656 | Normally unsigned is faster, but for some machines signed is. */ | |
7960bf22 | 6657 | #ifdef LOAD_EXTEND_OP |
b8698a0f | 6658 | ops_unsigned = (LOAD_EXTEND_OP (operand_mode) == SIGN_EXTEND |
2a1a3cd5 | 6659 | && !flag_syntax_only) ? 0 : 1; |
7960bf22 | 6660 | #else |
c87d821b | 6661 | ops_unsigned = 1; |
7960bf22 | 6662 | #endif |
7960bf22 | 6663 | |
5785c7de RS |
6664 | signed_type = lang_hooks.types.type_for_mode (operand_mode, 0); |
6665 | unsigned_type = lang_hooks.types.type_for_mode (operand_mode, 1); | |
e7824b3e | 6666 | intermediate_type = ops_unsigned ? unsigned_type : signed_type; |
db3927fb | 6667 | inner = fold_convert_loc (loc, intermediate_type, inner); |
7960bf22 JL |
6668 | |
6669 | if (bitnum != 0) | |
59ce6d6b RS |
6670 | inner = build2 (RSHIFT_EXPR, intermediate_type, |
6671 | inner, size_int (bitnum)); | |
7960bf22 | 6672 | |
000d8d44 RS |
6673 | one = build_int_cst (intermediate_type, 1); |
6674 | ||
7960bf22 | 6675 | if (code == EQ_EXPR) |
db3927fb | 6676 | inner = fold_build2_loc (loc, BIT_XOR_EXPR, intermediate_type, inner, one); |
7960bf22 JL |
6677 | |
6678 | /* Put the AND last so it can combine with more things. */ | |
000d8d44 | 6679 | inner = build2 (BIT_AND_EXPR, intermediate_type, inner, one); |
7960bf22 JL |
6680 | |
6681 | /* Make sure to return the proper type. */ | |
db3927fb | 6682 | inner = fold_convert_loc (loc, result_type, inner); |
7960bf22 JL |
6683 | |
6684 | return inner; | |
6685 | } | |
6686 | return NULL_TREE; | |
6687 | } | |
5dfa45d0 | 6688 | |
05d362b8 RS |
6689 | /* Check whether we are allowed to reorder operands arg0 and arg1, |
6690 | such that the evaluation of arg1 occurs before arg0. */ | |
6691 | ||
6692 | static bool | |
ac545c64 | 6693 | reorder_operands_p (const_tree arg0, const_tree arg1) |
05d362b8 RS |
6694 | { |
6695 | if (! flag_evaluation_order) | |
3e6688a7 | 6696 | return true; |
05d362b8 RS |
6697 | if (TREE_CONSTANT (arg0) || TREE_CONSTANT (arg1)) |
6698 | return true; | |
6699 | return ! TREE_SIDE_EFFECTS (arg0) | |
6700 | && ! TREE_SIDE_EFFECTS (arg1); | |
6701 | } | |
6702 | ||
37af03cb RS |
6703 | /* Test whether it is preferable two swap two operands, ARG0 and |
6704 | ARG1, for example because ARG0 is an integer constant and ARG1 | |
05d362b8 RS |
6705 | isn't. If REORDER is true, only recommend swapping if we can |
6706 | evaluate the operands in reverse order. */ | |
37af03cb | 6707 | |
fd660b1b | 6708 | bool |
fa233e34 | 6709 | tree_swap_operands_p (const_tree arg0, const_tree arg1, bool reorder) |
37af03cb RS |
6710 | { |
6711 | STRIP_SIGN_NOPS (arg0); | |
6712 | STRIP_SIGN_NOPS (arg1); | |
6713 | ||
6714 | if (TREE_CODE (arg1) == INTEGER_CST) | |
6715 | return 0; | |
6716 | if (TREE_CODE (arg0) == INTEGER_CST) | |
6717 | return 1; | |
6718 | ||
6719 | if (TREE_CODE (arg1) == REAL_CST) | |
6720 | return 0; | |
6721 | if (TREE_CODE (arg0) == REAL_CST) | |
6722 | return 1; | |
6723 | ||
325217ed CF |
6724 | if (TREE_CODE (arg1) == FIXED_CST) |
6725 | return 0; | |
6726 | if (TREE_CODE (arg0) == FIXED_CST) | |
6727 | return 1; | |
6728 | ||
37af03cb RS |
6729 | if (TREE_CODE (arg1) == COMPLEX_CST) |
6730 | return 0; | |
6731 | if (TREE_CODE (arg0) == COMPLEX_CST) | |
6732 | return 1; | |
6733 | ||
6734 | if (TREE_CONSTANT (arg1)) | |
6735 | return 0; | |
6736 | if (TREE_CONSTANT (arg0)) | |
6737 | return 1; | |
d1822754 | 6738 | |
7f4b6d20 | 6739 | if (optimize_function_for_size_p (cfun)) |
a352244f | 6740 | return 0; |
37af03cb | 6741 | |
05d362b8 RS |
6742 | if (reorder && flag_evaluation_order |
6743 | && (TREE_SIDE_EFFECTS (arg0) || TREE_SIDE_EFFECTS (arg1))) | |
6744 | return 0; | |
6745 | ||
fd660b1b JL |
6746 | /* It is preferable to swap two SSA_NAME to ensure a canonical form |
6747 | for commutative and comparison operators. Ensuring a canonical | |
6748 | form allows the optimizers to find additional redundancies without | |
6749 | having to explicitly check for both orderings. */ | |
6750 | if (TREE_CODE (arg0) == SSA_NAME | |
6751 | && TREE_CODE (arg1) == SSA_NAME | |
6752 | && SSA_NAME_VERSION (arg0) > SSA_NAME_VERSION (arg1)) | |
6753 | return 1; | |
6754 | ||
421076b5 RG |
6755 | /* Put SSA_NAMEs last. */ |
6756 | if (TREE_CODE (arg1) == SSA_NAME) | |
6757 | return 0; | |
6758 | if (TREE_CODE (arg0) == SSA_NAME) | |
6759 | return 1; | |
6760 | ||
6761 | /* Put variables last. */ | |
6762 | if (DECL_P (arg1)) | |
6763 | return 0; | |
6764 | if (DECL_P (arg0)) | |
6765 | return 1; | |
6766 | ||
37af03cb RS |
6767 | return 0; |
6768 | } | |
6769 | ||
18522563 ZD |
6770 | /* Fold comparison ARG0 CODE ARG1 (with result in TYPE), where |
6771 | ARG0 is extended to a wider type. */ | |
6772 | ||
6773 | static tree | |
db3927fb AH |
6774 | fold_widened_comparison (location_t loc, enum tree_code code, |
6775 | tree type, tree arg0, tree arg1) | |
18522563 ZD |
6776 | { |
6777 | tree arg0_unw = get_unwidened (arg0, NULL_TREE); | |
6778 | tree arg1_unw; | |
6779 | tree shorter_type, outer_type; | |
6780 | tree min, max; | |
6781 | bool above, below; | |
6782 | ||
6783 | if (arg0_unw == arg0) | |
6784 | return NULL_TREE; | |
6785 | shorter_type = TREE_TYPE (arg0_unw); | |
2a0958c5 | 6786 | |
6c6d9d33 JDA |
6787 | #ifdef HAVE_canonicalize_funcptr_for_compare |
6788 | /* Disable this optimization if we're casting a function pointer | |
6789 | type on targets that require function pointer canonicalization. */ | |
6790 | if (HAVE_canonicalize_funcptr_for_compare | |
6791 | && TREE_CODE (shorter_type) == POINTER_TYPE | |
6792 | && TREE_CODE (TREE_TYPE (shorter_type)) == FUNCTION_TYPE) | |
6793 | return NULL_TREE; | |
6794 | #endif | |
6795 | ||
2a0958c5 JJ |
6796 | if (TYPE_PRECISION (TREE_TYPE (arg0)) <= TYPE_PRECISION (shorter_type)) |
6797 | return NULL_TREE; | |
6798 | ||
8f768a5a | 6799 | arg1_unw = get_unwidened (arg1, NULL_TREE); |
18522563 ZD |
6800 | |
6801 | /* If possible, express the comparison in the shorter mode. */ | |
6802 | if ((code == EQ_EXPR || code == NE_EXPR | |
6803 | || TYPE_UNSIGNED (TREE_TYPE (arg0)) == TYPE_UNSIGNED (shorter_type)) | |
6804 | && (TREE_TYPE (arg1_unw) == shorter_type | |
02765a37 | 6805 | || ((TYPE_PRECISION (shorter_type) |
2e1d2474 | 6806 | >= TYPE_PRECISION (TREE_TYPE (arg1_unw))) |
02765a37 RG |
6807 | && (TYPE_UNSIGNED (shorter_type) |
6808 | == TYPE_UNSIGNED (TREE_TYPE (arg1_unw)))) | |
18522563 | 6809 | || (TREE_CODE (arg1_unw) == INTEGER_CST |
a7e1c928 AP |
6810 | && (TREE_CODE (shorter_type) == INTEGER_TYPE |
6811 | || TREE_CODE (shorter_type) == BOOLEAN_TYPE) | |
18522563 | 6812 | && int_fits_type_p (arg1_unw, shorter_type)))) |
db3927fb AH |
6813 | return fold_build2_loc (loc, code, type, arg0_unw, |
6814 | fold_convert_loc (loc, shorter_type, arg1_unw)); | |
18522563 | 6815 | |
1630e763 AS |
6816 | if (TREE_CODE (arg1_unw) != INTEGER_CST |
6817 | || TREE_CODE (shorter_type) != INTEGER_TYPE | |
6818 | || !int_fits_type_p (arg1_unw, shorter_type)) | |
18522563 ZD |
6819 | return NULL_TREE; |
6820 | ||
6821 | /* If we are comparing with the integer that does not fit into the range | |
6822 | of the shorter type, the result is known. */ | |
6823 | outer_type = TREE_TYPE (arg1_unw); | |
6824 | min = lower_bound_in_type (outer_type, shorter_type); | |
6825 | max = upper_bound_in_type (outer_type, shorter_type); | |
6826 | ||
6827 | above = integer_nonzerop (fold_relational_const (LT_EXPR, type, | |
6828 | max, arg1_unw)); | |
6829 | below = integer_nonzerop (fold_relational_const (LT_EXPR, type, | |
6830 | arg1_unw, min)); | |
6831 | ||
6832 | switch (code) | |
6833 | { | |
6834 | case EQ_EXPR: | |
6835 | if (above || below) | |
db3927fb | 6836 | return omit_one_operand_loc (loc, type, integer_zero_node, arg0); |
18522563 ZD |
6837 | break; |
6838 | ||
6839 | case NE_EXPR: | |
6840 | if (above || below) | |
db3927fb | 6841 | return omit_one_operand_loc (loc, type, integer_one_node, arg0); |
18522563 ZD |
6842 | break; |
6843 | ||
6844 | case LT_EXPR: | |
6845 | case LE_EXPR: | |
6846 | if (above) | |
db3927fb | 6847 | return omit_one_operand_loc (loc, type, integer_one_node, arg0); |
18522563 | 6848 | else if (below) |
db3927fb | 6849 | return omit_one_operand_loc (loc, type, integer_zero_node, arg0); |
18522563 ZD |
6850 | |
6851 | case GT_EXPR: | |
6852 | case GE_EXPR: | |
6853 | if (above) | |
db3927fb | 6854 | return omit_one_operand_loc (loc, type, integer_zero_node, arg0); |
18522563 | 6855 | else if (below) |
db3927fb | 6856 | return omit_one_operand_loc (loc, type, integer_one_node, arg0); |
18522563 ZD |
6857 | |
6858 | default: | |
6859 | break; | |
6860 | } | |
6861 | ||
6862 | return NULL_TREE; | |
6863 | } | |
6864 | ||
6865 | /* Fold comparison ARG0 CODE ARG1 (with result in TYPE), where for | |
6866 | ARG0 just the signedness is changed. */ | |
6867 | ||
6868 | static tree | |
db3927fb | 6869 | fold_sign_changed_comparison (location_t loc, enum tree_code code, tree type, |
18522563 ZD |
6870 | tree arg0, tree arg1) |
6871 | { | |
b8fca551 | 6872 | tree arg0_inner; |
18522563 ZD |
6873 | tree inner_type, outer_type; |
6874 | ||
1043771b | 6875 | if (!CONVERT_EXPR_P (arg0)) |
18522563 ZD |
6876 | return NULL_TREE; |
6877 | ||
6878 | outer_type = TREE_TYPE (arg0); | |
6879 | arg0_inner = TREE_OPERAND (arg0, 0); | |
6880 | inner_type = TREE_TYPE (arg0_inner); | |
6881 | ||
6c6d9d33 JDA |
6882 | #ifdef HAVE_canonicalize_funcptr_for_compare |
6883 | /* Disable this optimization if we're casting a function pointer | |
6884 | type on targets that require function pointer canonicalization. */ | |
6885 | if (HAVE_canonicalize_funcptr_for_compare | |
6886 | && TREE_CODE (inner_type) == POINTER_TYPE | |
6887 | && TREE_CODE (TREE_TYPE (inner_type)) == FUNCTION_TYPE) | |
6888 | return NULL_TREE; | |
6889 | #endif | |
6890 | ||
18522563 ZD |
6891 | if (TYPE_PRECISION (inner_type) != TYPE_PRECISION (outer_type)) |
6892 | return NULL_TREE; | |
6893 | ||
6894 | if (TREE_CODE (arg1) != INTEGER_CST | |
1043771b | 6895 | && !(CONVERT_EXPR_P (arg1) |
18522563 ZD |
6896 | && TREE_TYPE (TREE_OPERAND (arg1, 0)) == inner_type)) |
6897 | return NULL_TREE; | |
6898 | ||
af89bd04 | 6899 | if (TYPE_UNSIGNED (inner_type) != TYPE_UNSIGNED (outer_type) |
18522563 ZD |
6900 | && code != NE_EXPR |
6901 | && code != EQ_EXPR) | |
6902 | return NULL_TREE; | |
6903 | ||
af89bd04 MP |
6904 | if (POINTER_TYPE_P (inner_type) != POINTER_TYPE_P (outer_type)) |
6905 | return NULL_TREE; | |
6906 | ||
18522563 | 6907 | if (TREE_CODE (arg1) == INTEGER_CST) |
9589f23e AS |
6908 | arg1 = force_fit_type_double (inner_type, tree_to_double_int (arg1), |
6909 | 0, TREE_OVERFLOW (arg1)); | |
18522563 | 6910 | else |
db3927fb | 6911 | arg1 = fold_convert_loc (loc, inner_type, arg1); |
18522563 | 6912 | |
db3927fb | 6913 | return fold_build2_loc (loc, code, type, arg0_inner, arg1); |
18522563 ZD |
6914 | } |
6915 | ||
5be014d5 | 6916 | /* Tries to replace &a[idx] p+ s * delta with &a[idx + delta], if s is |
db3927fb AH |
6917 | step of the array. Reconstructs s and delta in the case of s * |
6918 | delta being an integer constant (and thus already folded). ADDR is | |
6919 | the address. MULT is the multiplicative expression. If the | |
6920 | function succeeds, the new address expression is returned. | |
6921 | Otherwise NULL_TREE is returned. LOC is the location of the | |
6922 | resulting expression. */ | |
38b0dcb8 ZD |
6923 | |
6924 | static tree | |
db3927fb | 6925 | try_move_mult_to_index (location_t loc, tree addr, tree op1) |
38b0dcb8 ZD |
6926 | { |
6927 | tree s, delta, step; | |
38b0dcb8 ZD |
6928 | tree ref = TREE_OPERAND (addr, 0), pref; |
6929 | tree ret, pos; | |
6930 | tree itype; | |
713e3ec9 | 6931 | bool mdim = false; |
38b0dcb8 | 6932 | |
5be014d5 AP |
6933 | /* Strip the nops that might be added when converting op1 to sizetype. */ |
6934 | STRIP_NOPS (op1); | |
6935 | ||
c5542940 RG |
6936 | /* Canonicalize op1 into a possibly non-constant delta |
6937 | and an INTEGER_CST s. */ | |
6938 | if (TREE_CODE (op1) == MULT_EXPR) | |
38b0dcb8 | 6939 | { |
c5542940 RG |
6940 | tree arg0 = TREE_OPERAND (op1, 0), arg1 = TREE_OPERAND (op1, 1); |
6941 | ||
6942 | STRIP_NOPS (arg0); | |
6943 | STRIP_NOPS (arg1); | |
b8698a0f | 6944 | |
c5542940 RG |
6945 | if (TREE_CODE (arg0) == INTEGER_CST) |
6946 | { | |
6947 | s = arg0; | |
6948 | delta = arg1; | |
6949 | } | |
6950 | else if (TREE_CODE (arg1) == INTEGER_CST) | |
6951 | { | |
6952 | s = arg1; | |
6953 | delta = arg0; | |
6954 | } | |
6955 | else | |
6956 | return NULL_TREE; | |
38b0dcb8 | 6957 | } |
c5542940 | 6958 | else if (TREE_CODE (op1) == INTEGER_CST) |
38b0dcb8 | 6959 | { |
c5542940 RG |
6960 | delta = op1; |
6961 | s = NULL_TREE; | |
38b0dcb8 ZD |
6962 | } |
6963 | else | |
c5542940 RG |
6964 | { |
6965 | /* Simulate we are delta * 1. */ | |
6966 | delta = op1; | |
6967 | s = integer_one_node; | |
6968 | } | |
38b0dcb8 | 6969 | |
005aa1b4 RG |
6970 | /* Handle &x.array the same as we would handle &x.array[0]. */ |
6971 | if (TREE_CODE (ref) == COMPONENT_REF | |
6972 | && TREE_CODE (TREE_TYPE (ref)) == ARRAY_TYPE) | |
38b0dcb8 | 6973 | { |
005aa1b4 | 6974 | tree domain; |
8e281a8d | 6975 | |
005aa1b4 RG |
6976 | /* Remember if this was a multi-dimensional array. */ |
6977 | if (TREE_CODE (TREE_OPERAND (ref, 0)) == ARRAY_REF) | |
6978 | mdim = true; | |
713e3ec9 | 6979 | |
005aa1b4 RG |
6980 | domain = TYPE_DOMAIN (TREE_TYPE (ref)); |
6981 | if (! domain) | |
6982 | goto cont; | |
6983 | itype = TREE_TYPE (domain); | |
03b0db0a | 6984 | |
005aa1b4 RG |
6985 | step = TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (ref))); |
6986 | if (TREE_CODE (step) != INTEGER_CST) | |
6987 | goto cont; | |
38b0dcb8 | 6988 | |
005aa1b4 RG |
6989 | if (s) |
6990 | { | |
6991 | if (! tree_int_cst_equal (step, s)) | |
6992 | goto cont; | |
6993 | } | |
6994 | else | |
6995 | { | |
6996 | /* Try if delta is a multiple of step. */ | |
6997 | tree tmp = div_if_zero_remainder (EXACT_DIV_EXPR, op1, step); | |
6998 | if (! tmp) | |
6999 | goto cont; | |
7000 | delta = tmp; | |
7001 | } | |
38b0dcb8 | 7002 | |
005aa1b4 RG |
7003 | /* Only fold here if we can verify we do not overflow one |
7004 | dimension of a multi-dimensional array. */ | |
7005 | if (mdim) | |
7006 | { | |
7007 | tree tmp; | |
7008 | ||
7009 | if (!TYPE_MIN_VALUE (domain) | |
7010 | || !TYPE_MAX_VALUE (domain) | |
7011 | || TREE_CODE (TYPE_MAX_VALUE (domain)) != INTEGER_CST) | |
7012 | goto cont; | |
7013 | ||
7014 | tmp = fold_binary_loc (loc, PLUS_EXPR, itype, | |
7015 | fold_convert_loc (loc, itype, | |
7016 | TYPE_MIN_VALUE (domain)), | |
7017 | fold_convert_loc (loc, itype, delta)); | |
7018 | if (TREE_CODE (tmp) != INTEGER_CST | |
7019 | || tree_int_cst_lt (TYPE_MAX_VALUE (domain), tmp)) | |
7020 | goto cont; | |
7021 | } | |
713e3ec9 | 7022 | |
005aa1b4 | 7023 | /* We found a suitable component reference. */ |
713e3ec9 | 7024 | |
005aa1b4 RG |
7025 | pref = TREE_OPERAND (addr, 0); |
7026 | ret = copy_node (pref); | |
7027 | SET_EXPR_LOCATION (ret, loc); | |
713e3ec9 | 7028 | |
005aa1b4 RG |
7029 | ret = build4_loc (loc, ARRAY_REF, TREE_TYPE (TREE_TYPE (ref)), ret, |
7030 | fold_build2_loc | |
7031 | (loc, PLUS_EXPR, itype, | |
7032 | fold_convert_loc (loc, itype, | |
7033 | TYPE_MIN_VALUE | |
7034 | (TYPE_DOMAIN (TREE_TYPE (ref)))), | |
7035 | fold_convert_loc (loc, itype, delta)), | |
7036 | NULL_TREE, NULL_TREE); | |
7037 | return build_fold_addr_expr_loc (loc, ret); | |
7038 | } | |
7039 | ||
7040 | cont: | |
7041 | ||
7042 | for (;; ref = TREE_OPERAND (ref, 0)) | |
7043 | { | |
7044 | if (TREE_CODE (ref) == ARRAY_REF) | |
206c3e10 RG |
7045 | { |
7046 | tree domain; | |
7047 | ||
7048 | /* Remember if this was a multi-dimensional array. */ | |
7049 | if (TREE_CODE (TREE_OPERAND (ref, 0)) == ARRAY_REF) | |
7050 | mdim = true; | |
7051 | ||
005aa1b4 | 7052 | domain = TYPE_DOMAIN (TREE_TYPE (TREE_OPERAND (ref, 0))); |
206c3e10 RG |
7053 | if (! domain) |
7054 | continue; | |
7055 | itype = TREE_TYPE (domain); | |
7056 | ||
005aa1b4 | 7057 | step = array_ref_element_size (ref); |
206c3e10 RG |
7058 | if (TREE_CODE (step) != INTEGER_CST) |
7059 | continue; | |
7060 | ||
7061 | if (s) | |
7062 | { | |
7063 | if (! tree_int_cst_equal (step, s)) | |
7064 | continue; | |
7065 | } | |
7066 | else | |
7067 | { | |
7068 | /* Try if delta is a multiple of step. */ | |
7069 | tree tmp = div_if_zero_remainder (EXACT_DIV_EXPR, op1, step); | |
7070 | if (! tmp) | |
7071 | continue; | |
7072 | delta = tmp; | |
7073 | } | |
7074 | ||
7075 | /* Only fold here if we can verify we do not overflow one | |
7076 | dimension of a multi-dimensional array. */ | |
7077 | if (mdim) | |
7078 | { | |
7079 | tree tmp; | |
7080 | ||
005aa1b4 | 7081 | if (TREE_CODE (TREE_OPERAND (ref, 1)) != INTEGER_CST |
206c3e10 RG |
7082 | || !TYPE_MAX_VALUE (domain) |
7083 | || TREE_CODE (TYPE_MAX_VALUE (domain)) != INTEGER_CST) | |
7084 | continue; | |
7085 | ||
7086 | tmp = fold_binary_loc (loc, PLUS_EXPR, itype, | |
7087 | fold_convert_loc (loc, itype, | |
005aa1b4 | 7088 | TREE_OPERAND (ref, 1)), |
206c3e10 | 7089 | fold_convert_loc (loc, itype, delta)); |
005aa1b4 RG |
7090 | if (!tmp |
7091 | || TREE_CODE (tmp) != INTEGER_CST | |
206c3e10 RG |
7092 | || tree_int_cst_lt (TYPE_MAX_VALUE (domain), tmp)) |
7093 | continue; | |
7094 | } | |
7095 | ||
38b0dcb8 ZD |
7096 | break; |
7097 | } | |
713e3ec9 RG |
7098 | else |
7099 | mdim = false; | |
38b0dcb8 ZD |
7100 | |
7101 | if (!handled_component_p (ref)) | |
7102 | return NULL_TREE; | |
7103 | } | |
7104 | ||
7105 | /* We found the suitable array reference. So copy everything up to it, | |
7106 | and replace the index. */ | |
7107 | ||
7108 | pref = TREE_OPERAND (addr, 0); | |
7109 | ret = copy_node (pref); | |
db3927fb | 7110 | SET_EXPR_LOCATION (ret, loc); |
38b0dcb8 ZD |
7111 | pos = ret; |
7112 | ||
7113 | while (pref != ref) | |
7114 | { | |
7115 | pref = TREE_OPERAND (pref, 0); | |
7116 | TREE_OPERAND (pos, 0) = copy_node (pref); | |
7117 | pos = TREE_OPERAND (pos, 0); | |
7118 | } | |
7119 | ||
005aa1b4 RG |
7120 | TREE_OPERAND (pos, 1) |
7121 | = fold_build2_loc (loc, PLUS_EXPR, itype, | |
7122 | fold_convert_loc (loc, itype, TREE_OPERAND (pos, 1)), | |
7123 | fold_convert_loc (loc, itype, delta)); | |
7124 | return fold_build1_loc (loc, ADDR_EXPR, TREE_TYPE (addr), ret); | |
38b0dcb8 ZD |
7125 | } |
7126 | ||
1d481ba8 ZD |
7127 | |
7128 | /* Fold A < X && A + 1 > Y to A < X && A >= Y. Normally A + 1 > Y | |
7129 | means A >= Y && A != MAX, but in this case we know that | |
7130 | A < X <= MAX. INEQ is A + 1 > Y, BOUND is A < X. */ | |
7131 | ||
7132 | static tree | |
db3927fb | 7133 | fold_to_nonsharp_ineq_using_bound (location_t loc, tree ineq, tree bound) |
1d481ba8 ZD |
7134 | { |
7135 | tree a, typea, type = TREE_TYPE (ineq), a1, diff, y; | |
7136 | ||
7137 | if (TREE_CODE (bound) == LT_EXPR) | |
7138 | a = TREE_OPERAND (bound, 0); | |
7139 | else if (TREE_CODE (bound) == GT_EXPR) | |
7140 | a = TREE_OPERAND (bound, 1); | |
7141 | else | |
7142 | return NULL_TREE; | |
7143 | ||
7144 | typea = TREE_TYPE (a); | |
7145 | if (!INTEGRAL_TYPE_P (typea) | |
7146 | && !POINTER_TYPE_P (typea)) | |
7147 | return NULL_TREE; | |
7148 | ||
7149 | if (TREE_CODE (ineq) == LT_EXPR) | |
7150 | { | |
7151 | a1 = TREE_OPERAND (ineq, 1); | |
7152 | y = TREE_OPERAND (ineq, 0); | |
7153 | } | |
7154 | else if (TREE_CODE (ineq) == GT_EXPR) | |
7155 | { | |
7156 | a1 = TREE_OPERAND (ineq, 0); | |
7157 | y = TREE_OPERAND (ineq, 1); | |
7158 | } | |
7159 | else | |
7160 | return NULL_TREE; | |
7161 | ||
7162 | if (TREE_TYPE (a1) != typea) | |
7163 | return NULL_TREE; | |
7164 | ||
5be014d5 AP |
7165 | if (POINTER_TYPE_P (typea)) |
7166 | { | |
7167 | /* Convert the pointer types into integer before taking the difference. */ | |
db3927fb AH |
7168 | tree ta = fold_convert_loc (loc, ssizetype, a); |
7169 | tree ta1 = fold_convert_loc (loc, ssizetype, a1); | |
7170 | diff = fold_binary_loc (loc, MINUS_EXPR, ssizetype, ta1, ta); | |
5be014d5 AP |
7171 | } |
7172 | else | |
db3927fb | 7173 | diff = fold_binary_loc (loc, MINUS_EXPR, typea, a1, a); |
5be014d5 AP |
7174 | |
7175 | if (!diff || !integer_onep (diff)) | |
7176 | return NULL_TREE; | |
1d481ba8 | 7177 | |
db3927fb | 7178 | return fold_build2_loc (loc, GE_EXPR, type, a, y); |
1d481ba8 ZD |
7179 | } |
7180 | ||
0ed9a3e3 RG |
7181 | /* Fold a sum or difference of at least one multiplication. |
7182 | Returns the folded tree or NULL if no simplification could be made. */ | |
7183 | ||
7184 | static tree | |
db3927fb AH |
7185 | fold_plusminus_mult_expr (location_t loc, enum tree_code code, tree type, |
7186 | tree arg0, tree arg1) | |
0ed9a3e3 RG |
7187 | { |
7188 | tree arg00, arg01, arg10, arg11; | |
7189 | tree alt0 = NULL_TREE, alt1 = NULL_TREE, same; | |
7190 | ||
7191 | /* (A * C) +- (B * C) -> (A+-B) * C. | |
7192 | (A * C) +- A -> A * (C+-1). | |
7193 | We are most concerned about the case where C is a constant, | |
7194 | but other combinations show up during loop reduction. Since | |
7195 | it is not difficult, try all four possibilities. */ | |
7196 | ||
7197 | if (TREE_CODE (arg0) == MULT_EXPR) | |
7198 | { | |
7199 | arg00 = TREE_OPERAND (arg0, 0); | |
7200 | arg01 = TREE_OPERAND (arg0, 1); | |
7201 | } | |
b462d62d RG |
7202 | else if (TREE_CODE (arg0) == INTEGER_CST) |
7203 | { | |
7204 | arg00 = build_one_cst (type); | |
7205 | arg01 = arg0; | |
7206 | } | |
0ed9a3e3 RG |
7207 | else |
7208 | { | |
325217ed CF |
7209 | /* We cannot generate constant 1 for fract. */ |
7210 | if (ALL_FRACT_MODE_P (TYPE_MODE (type))) | |
7211 | return NULL_TREE; | |
0ed9a3e3 | 7212 | arg00 = arg0; |
bfabddb6 | 7213 | arg01 = build_one_cst (type); |
0ed9a3e3 RG |
7214 | } |
7215 | if (TREE_CODE (arg1) == MULT_EXPR) | |
7216 | { | |
7217 | arg10 = TREE_OPERAND (arg1, 0); | |
7218 | arg11 = TREE_OPERAND (arg1, 1); | |
7219 | } | |
b462d62d RG |
7220 | else if (TREE_CODE (arg1) == INTEGER_CST) |
7221 | { | |
7222 | arg10 = build_one_cst (type); | |
cef158f9 RG |
7223 | /* As we canonicalize A - 2 to A + -2 get rid of that sign for |
7224 | the purpose of this canonicalization. */ | |
7225 | if (TREE_INT_CST_HIGH (arg1) == -1 | |
7226 | && negate_expr_p (arg1) | |
7227 | && code == PLUS_EXPR) | |
7228 | { | |
7229 | arg11 = negate_expr (arg1); | |
7230 | code = MINUS_EXPR; | |
7231 | } | |
7232 | else | |
7233 | arg11 = arg1; | |
b462d62d | 7234 | } |
0ed9a3e3 RG |
7235 | else |
7236 | { | |
325217ed CF |
7237 | /* We cannot generate constant 1 for fract. */ |
7238 | if (ALL_FRACT_MODE_P (TYPE_MODE (type))) | |
7239 | return NULL_TREE; | |
0ed9a3e3 | 7240 | arg10 = arg1; |
bfabddb6 | 7241 | arg11 = build_one_cst (type); |
0ed9a3e3 RG |
7242 | } |
7243 | same = NULL_TREE; | |
7244 | ||
7245 | if (operand_equal_p (arg01, arg11, 0)) | |
7246 | same = arg01, alt0 = arg00, alt1 = arg10; | |
7247 | else if (operand_equal_p (arg00, arg10, 0)) | |
7248 | same = arg00, alt0 = arg01, alt1 = arg11; | |
7249 | else if (operand_equal_p (arg00, arg11, 0)) | |
7250 | same = arg00, alt0 = arg01, alt1 = arg10; | |
7251 | else if (operand_equal_p (arg01, arg10, 0)) | |
7252 | same = arg01, alt0 = arg00, alt1 = arg11; | |
7253 | ||
7254 | /* No identical multiplicands; see if we can find a common | |
7255 | power-of-two factor in non-power-of-two multiplies. This | |
7256 | can help in multi-dimensional array access. */ | |
7257 | else if (host_integerp (arg01, 0) | |
7258 | && host_integerp (arg11, 0)) | |
7259 | { | |
7260 | HOST_WIDE_INT int01, int11, tmp; | |
7261 | bool swap = false; | |
7262 | tree maybe_same; | |
7263 | int01 = TREE_INT_CST_LOW (arg01); | |
7264 | int11 = TREE_INT_CST_LOW (arg11); | |
7265 | ||
7266 | /* Move min of absolute values to int11. */ | |
4c9cf7af | 7267 | if (absu_hwi (int01) < absu_hwi (int11)) |
0ed9a3e3 RG |
7268 | { |
7269 | tmp = int01, int01 = int11, int11 = tmp; | |
7270 | alt0 = arg00, arg00 = arg10, arg10 = alt0; | |
7271 | maybe_same = arg01; | |
7272 | swap = true; | |
7273 | } | |
7274 | else | |
7275 | maybe_same = arg11; | |
7276 | ||
4c9cf7af | 7277 | if (exact_log2 (absu_hwi (int11)) > 0 && int01 % int11 == 0 |
299b87f8 RG |
7278 | /* The remainder should not be a constant, otherwise we |
7279 | end up folding i * 4 + 2 to (i * 2 + 1) * 2 which has | |
7280 | increased the number of multiplications necessary. */ | |
7281 | && TREE_CODE (arg10) != INTEGER_CST) | |
0ed9a3e3 | 7282 | { |
db3927fb | 7283 | alt0 = fold_build2_loc (loc, MULT_EXPR, TREE_TYPE (arg00), arg00, |
0ed9a3e3 RG |
7284 | build_int_cst (TREE_TYPE (arg00), |
7285 | int01 / int11)); | |
7286 | alt1 = arg10; | |
7287 | same = maybe_same; | |
7288 | if (swap) | |
7289 | maybe_same = alt0, alt0 = alt1, alt1 = maybe_same; | |
7290 | } | |
7291 | } | |
7292 | ||
7293 | if (same) | |
db3927fb AH |
7294 | return fold_build2_loc (loc, MULT_EXPR, type, |
7295 | fold_build2_loc (loc, code, type, | |
7296 | fold_convert_loc (loc, type, alt0), | |
7297 | fold_convert_loc (loc, type, alt1)), | |
7298 | fold_convert_loc (loc, type, same)); | |
0ed9a3e3 RG |
7299 | |
7300 | return NULL_TREE; | |
7301 | } | |
7302 | ||
78bf6e2f RS |
7303 | /* Subroutine of native_encode_expr. Encode the INTEGER_CST |
7304 | specified by EXPR into the buffer PTR of length LEN bytes. | |
7305 | Return the number of bytes placed in the buffer, or zero | |
7306 | upon failure. */ | |
7307 | ||
7308 | static int | |
fa233e34 | 7309 | native_encode_int (const_tree expr, unsigned char *ptr, int len) |
78bf6e2f RS |
7310 | { |
7311 | tree type = TREE_TYPE (expr); | |
7312 | int total_bytes = GET_MODE_SIZE (TYPE_MODE (type)); | |
7313 | int byte, offset, word, words; | |
7314 | unsigned char value; | |
7315 | ||
7316 | if (total_bytes > len) | |
7317 | return 0; | |
7318 | words = total_bytes / UNITS_PER_WORD; | |
7319 | ||
7320 | for (byte = 0; byte < total_bytes; byte++) | |
7321 | { | |
7322 | int bitpos = byte * BITS_PER_UNIT; | |
7323 | if (bitpos < HOST_BITS_PER_WIDE_INT) | |
7324 | value = (unsigned char) (TREE_INT_CST_LOW (expr) >> bitpos); | |
7325 | else | |
7326 | value = (unsigned char) (TREE_INT_CST_HIGH (expr) | |
7327 | >> (bitpos - HOST_BITS_PER_WIDE_INT)); | |
7328 | ||
7329 | if (total_bytes > UNITS_PER_WORD) | |
7330 | { | |
7331 | word = byte / UNITS_PER_WORD; | |
7332 | if (WORDS_BIG_ENDIAN) | |
7333 | word = (words - 1) - word; | |
7334 | offset = word * UNITS_PER_WORD; | |
7335 | if (BYTES_BIG_ENDIAN) | |
7336 | offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD); | |
7337 | else | |
7338 | offset += byte % UNITS_PER_WORD; | |
7339 | } | |
7340 | else | |
7341 | offset = BYTES_BIG_ENDIAN ? (total_bytes - 1) - byte : byte; | |
7342 | ptr[offset] = value; | |
7343 | } | |
7344 | return total_bytes; | |
7345 | } | |
7346 | ||
7347 | ||
cc06c01d GJL |
7348 | /* Subroutine of native_encode_expr. Encode the FIXED_CST |
7349 | specified by EXPR into the buffer PTR of length LEN bytes. | |
7350 | Return the number of bytes placed in the buffer, or zero | |
7351 | upon failure. */ | |
7352 | ||
7353 | static int | |
7354 | native_encode_fixed (const_tree expr, unsigned char *ptr, int len) | |
7355 | { | |
7356 | tree type = TREE_TYPE (expr); | |
7357 | enum machine_mode mode = TYPE_MODE (type); | |
7358 | int total_bytes = GET_MODE_SIZE (mode); | |
7359 | FIXED_VALUE_TYPE value; | |
7360 | tree i_value, i_type; | |
7361 | ||
7362 | if (total_bytes * BITS_PER_UNIT > HOST_BITS_PER_DOUBLE_INT) | |
7363 | return 0; | |
7364 | ||
7365 | i_type = lang_hooks.types.type_for_size (GET_MODE_BITSIZE (mode), 1); | |
7366 | ||
7367 | if (NULL_TREE == i_type | |
7368 | || TYPE_PRECISION (i_type) != total_bytes) | |
7369 | return 0; | |
7370 | ||
7371 | value = TREE_FIXED_CST (expr); | |
7372 | i_value = double_int_to_tree (i_type, value.data); | |
7373 | ||
7374 | return native_encode_int (i_value, ptr, len); | |
7375 | } | |
7376 | ||
7377 | ||
78bf6e2f RS |
7378 | /* Subroutine of native_encode_expr. Encode the REAL_CST |
7379 | specified by EXPR into the buffer PTR of length LEN bytes. | |
7380 | Return the number of bytes placed in the buffer, or zero | |
7381 | upon failure. */ | |
7382 | ||
7383 | static int | |
fa233e34 | 7384 | native_encode_real (const_tree expr, unsigned char *ptr, int len) |
78bf6e2f RS |
7385 | { |
7386 | tree type = TREE_TYPE (expr); | |
7387 | int total_bytes = GET_MODE_SIZE (TYPE_MODE (type)); | |
0a9430a8 | 7388 | int byte, offset, word, words, bitpos; |
78bf6e2f RS |
7389 | unsigned char value; |
7390 | ||
7391 | /* There are always 32 bits in each long, no matter the size of | |
7392 | the hosts long. We handle floating point representations with | |
7393 | up to 192 bits. */ | |
7394 | long tmp[6]; | |
7395 | ||
7396 | if (total_bytes > len) | |
7397 | return 0; | |
54193313 | 7398 | words = (32 / BITS_PER_UNIT) / UNITS_PER_WORD; |
78bf6e2f RS |
7399 | |
7400 | real_to_target (tmp, TREE_REAL_CST_PTR (expr), TYPE_MODE (type)); | |
7401 | ||
0a9430a8 JJ |
7402 | for (bitpos = 0; bitpos < total_bytes * BITS_PER_UNIT; |
7403 | bitpos += BITS_PER_UNIT) | |
78bf6e2f | 7404 | { |
0a9430a8 | 7405 | byte = (bitpos / BITS_PER_UNIT) & 3; |
78bf6e2f RS |
7406 | value = (unsigned char) (tmp[bitpos / 32] >> (bitpos & 31)); |
7407 | ||
0a9430a8 | 7408 | if (UNITS_PER_WORD < 4) |
78bf6e2f RS |
7409 | { |
7410 | word = byte / UNITS_PER_WORD; | |
0a9430a8 | 7411 | if (WORDS_BIG_ENDIAN) |
78bf6e2f RS |
7412 | word = (words - 1) - word; |
7413 | offset = word * UNITS_PER_WORD; | |
7414 | if (BYTES_BIG_ENDIAN) | |
7415 | offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD); | |
7416 | else | |
7417 | offset += byte % UNITS_PER_WORD; | |
7418 | } | |
7419 | else | |
0a9430a8 JJ |
7420 | offset = BYTES_BIG_ENDIAN ? 3 - byte : byte; |
7421 | ptr[offset + ((bitpos / BITS_PER_UNIT) & ~3)] = value; | |
78bf6e2f RS |
7422 | } |
7423 | return total_bytes; | |
7424 | } | |
7425 | ||
7426 | /* Subroutine of native_encode_expr. Encode the COMPLEX_CST | |
7427 | specified by EXPR into the buffer PTR of length LEN bytes. | |
7428 | Return the number of bytes placed in the buffer, or zero | |
7429 | upon failure. */ | |
7430 | ||
7431 | static int | |
fa233e34 | 7432 | native_encode_complex (const_tree expr, unsigned char *ptr, int len) |
78bf6e2f RS |
7433 | { |
7434 | int rsize, isize; | |
7435 | tree part; | |
7436 | ||
7437 | part = TREE_REALPART (expr); | |
7438 | rsize = native_encode_expr (part, ptr, len); | |
7439 | if (rsize == 0) | |
7440 | return 0; | |
7441 | part = TREE_IMAGPART (expr); | |
7442 | isize = native_encode_expr (part, ptr+rsize, len-rsize); | |
7443 | if (isize != rsize) | |
7444 | return 0; | |
7445 | return rsize + isize; | |
7446 | } | |
7447 | ||
7448 | ||
7449 | /* Subroutine of native_encode_expr. Encode the VECTOR_CST | |
7450 | specified by EXPR into the buffer PTR of length LEN bytes. | |
7451 | Return the number of bytes placed in the buffer, or zero | |
7452 | upon failure. */ | |
7453 | ||
7454 | static int | |
fa233e34 | 7455 | native_encode_vector (const_tree expr, unsigned char *ptr, int len) |
78bf6e2f | 7456 | { |
d2a12ae7 RG |
7457 | unsigned i, count; |
7458 | int size, offset; | |
7459 | tree itype, elem; | |
78bf6e2f | 7460 | |
78bf6e2f | 7461 | offset = 0; |
d2a12ae7 | 7462 | count = VECTOR_CST_NELTS (expr); |
1000b34d RS |
7463 | itype = TREE_TYPE (TREE_TYPE (expr)); |
7464 | size = GET_MODE_SIZE (TYPE_MODE (itype)); | |
78bf6e2f RS |
7465 | for (i = 0; i < count; i++) |
7466 | { | |
d2a12ae7 RG |
7467 | elem = VECTOR_CST_ELT (expr, i); |
7468 | if (native_encode_expr (elem, ptr+offset, len-offset) != size) | |
7469 | return 0; | |
78bf6e2f RS |
7470 | offset += size; |
7471 | } | |
7472 | return offset; | |
7473 | } | |
7474 | ||
7475 | ||
27a4e072 JJ |
7476 | /* Subroutine of native_encode_expr. Encode the STRING_CST |
7477 | specified by EXPR into the buffer PTR of length LEN bytes. | |
7478 | Return the number of bytes placed in the buffer, or zero | |
7479 | upon failure. */ | |
7480 | ||
7481 | static int | |
7482 | native_encode_string (const_tree expr, unsigned char *ptr, int len) | |
7483 | { | |
7484 | tree type = TREE_TYPE (expr); | |
7485 | HOST_WIDE_INT total_bytes; | |
7486 | ||
7487 | if (TREE_CODE (type) != ARRAY_TYPE | |
7488 | || TREE_CODE (TREE_TYPE (type)) != INTEGER_TYPE | |
7489 | || GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (type))) != BITS_PER_UNIT | |
7490 | || !host_integerp (TYPE_SIZE_UNIT (type), 0)) | |
7491 | return 0; | |
7492 | total_bytes = tree_low_cst (TYPE_SIZE_UNIT (type), 0); | |
7493 | if (total_bytes > len) | |
7494 | return 0; | |
7495 | if (TREE_STRING_LENGTH (expr) < total_bytes) | |
7496 | { | |
7497 | memcpy (ptr, TREE_STRING_POINTER (expr), TREE_STRING_LENGTH (expr)); | |
7498 | memset (ptr + TREE_STRING_LENGTH (expr), 0, | |
7499 | total_bytes - TREE_STRING_LENGTH (expr)); | |
7500 | } | |
7501 | else | |
7502 | memcpy (ptr, TREE_STRING_POINTER (expr), total_bytes); | |
7503 | return total_bytes; | |
7504 | } | |
7505 | ||
7506 | ||
78bf6e2f RS |
7507 | /* Subroutine of fold_view_convert_expr. Encode the INTEGER_CST, |
7508 | REAL_CST, COMPLEX_CST or VECTOR_CST specified by EXPR into the | |
7509 | buffer PTR of length LEN bytes. Return the number of bytes | |
7510 | placed in the buffer, or zero upon failure. */ | |
7511 | ||
db136335 | 7512 | int |
fa233e34 | 7513 | native_encode_expr (const_tree expr, unsigned char *ptr, int len) |
78bf6e2f RS |
7514 | { |
7515 | switch (TREE_CODE (expr)) | |
7516 | { | |
7517 | case INTEGER_CST: | |
7518 | return native_encode_int (expr, ptr, len); | |
7519 | ||
7520 | case REAL_CST: | |
7521 | return native_encode_real (expr, ptr, len); | |
7522 | ||
cc06c01d GJL |
7523 | case FIXED_CST: |
7524 | return native_encode_fixed (expr, ptr, len); | |
7525 | ||
78bf6e2f RS |
7526 | case COMPLEX_CST: |
7527 | return native_encode_complex (expr, ptr, len); | |
7528 | ||
7529 | case VECTOR_CST: | |
7530 | return native_encode_vector (expr, ptr, len); | |
7531 | ||
27a4e072 JJ |
7532 | case STRING_CST: |
7533 | return native_encode_string (expr, ptr, len); | |
7534 | ||
78bf6e2f RS |
7535 | default: |
7536 | return 0; | |
7537 | } | |
7538 | } | |
7539 | ||
7540 | ||
7541 | /* Subroutine of native_interpret_expr. Interpret the contents of | |
7542 | the buffer PTR of length LEN as an INTEGER_CST of type TYPE. | |
7543 | If the buffer cannot be interpreted, return NULL_TREE. */ | |
7544 | ||
7545 | static tree | |
fa233e34 | 7546 | native_interpret_int (tree type, const unsigned char *ptr, int len) |
78bf6e2f RS |
7547 | { |
7548 | int total_bytes = GET_MODE_SIZE (TYPE_MODE (type)); | |
1961ffb8 | 7549 | double_int result; |
78bf6e2f | 7550 | |
cc06c01d GJL |
7551 | if (total_bytes > len |
7552 | || total_bytes * BITS_PER_UNIT > HOST_BITS_PER_DOUBLE_INT) | |
78bf6e2f | 7553 | return NULL_TREE; |
1961ffb8 | 7554 | |
cc06c01d | 7555 | result = double_int::from_buffer (ptr, total_bytes); |
78bf6e2f | 7556 | |
cc06c01d GJL |
7557 | return double_int_to_tree (type, result); |
7558 | } | |
78bf6e2f | 7559 | |
78bf6e2f | 7560 | |
cc06c01d GJL |
7561 | /* Subroutine of native_interpret_expr. Interpret the contents of |
7562 | the buffer PTR of length LEN as a FIXED_CST of type TYPE. | |
7563 | If the buffer cannot be interpreted, return NULL_TREE. */ | |
7564 | ||
7565 | static tree | |
7566 | native_interpret_fixed (tree type, const unsigned char *ptr, int len) | |
7567 | { | |
7568 | int total_bytes = GET_MODE_SIZE (TYPE_MODE (type)); | |
7569 | double_int result; | |
7570 | FIXED_VALUE_TYPE fixed_value; | |
7571 | ||
7572 | if (total_bytes > len | |
7573 | || total_bytes * BITS_PER_UNIT > HOST_BITS_PER_DOUBLE_INT) | |
7574 | return NULL_TREE; | |
7575 | ||
7576 | result = double_int::from_buffer (ptr, total_bytes); | |
7577 | fixed_value = fixed_from_double_int (result, TYPE_MODE (type)); | |
7578 | ||
7579 | return build_fixed (type, fixed_value); | |
78bf6e2f RS |
7580 | } |
7581 | ||
7582 | ||
7583 | /* Subroutine of native_interpret_expr. Interpret the contents of | |
7584 | the buffer PTR of length LEN as a REAL_CST of type TYPE. | |
7585 | If the buffer cannot be interpreted, return NULL_TREE. */ | |
7586 | ||
7587 | static tree | |
fa233e34 | 7588 | native_interpret_real (tree type, const unsigned char *ptr, int len) |
78bf6e2f | 7589 | { |
15b1c12a RS |
7590 | enum machine_mode mode = TYPE_MODE (type); |
7591 | int total_bytes = GET_MODE_SIZE (mode); | |
0a9430a8 | 7592 | int byte, offset, word, words, bitpos; |
78bf6e2f RS |
7593 | unsigned char value; |
7594 | /* There are always 32 bits in each long, no matter the size of | |
7595 | the hosts long. We handle floating point representations with | |
7596 | up to 192 bits. */ | |
7597 | REAL_VALUE_TYPE r; | |
7598 | long tmp[6]; | |
7599 | ||
7600 | total_bytes = GET_MODE_SIZE (TYPE_MODE (type)); | |
7601 | if (total_bytes > len || total_bytes > 24) | |
7602 | return NULL_TREE; | |
54193313 | 7603 | words = (32 / BITS_PER_UNIT) / UNITS_PER_WORD; |
78bf6e2f RS |
7604 | |
7605 | memset (tmp, 0, sizeof (tmp)); | |
0a9430a8 JJ |
7606 | for (bitpos = 0; bitpos < total_bytes * BITS_PER_UNIT; |
7607 | bitpos += BITS_PER_UNIT) | |
78bf6e2f | 7608 | { |
0a9430a8 JJ |
7609 | byte = (bitpos / BITS_PER_UNIT) & 3; |
7610 | if (UNITS_PER_WORD < 4) | |
78bf6e2f RS |
7611 | { |
7612 | word = byte / UNITS_PER_WORD; | |
0a9430a8 | 7613 | if (WORDS_BIG_ENDIAN) |
78bf6e2f RS |
7614 | word = (words - 1) - word; |
7615 | offset = word * UNITS_PER_WORD; | |
7616 | if (BYTES_BIG_ENDIAN) | |
7617 | offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD); | |
7618 | else | |
7619 | offset += byte % UNITS_PER_WORD; | |
7620 | } | |
7621 | else | |
0a9430a8 JJ |
7622 | offset = BYTES_BIG_ENDIAN ? 3 - byte : byte; |
7623 | value = ptr[offset + ((bitpos / BITS_PER_UNIT) & ~3)]; | |
78bf6e2f RS |
7624 | |
7625 | tmp[bitpos / 32] |= (unsigned long)value << (bitpos & 31); | |
7626 | } | |
7627 | ||
7628 | real_from_target (&r, tmp, mode); | |
7629 | return build_real (type, r); | |
7630 | } | |
7631 | ||
7632 | ||
7633 | /* Subroutine of native_interpret_expr. Interpret the contents of | |
7634 | the buffer PTR of length LEN as a COMPLEX_CST of type TYPE. | |
7635 | If the buffer cannot be interpreted, return NULL_TREE. */ | |
7636 | ||
7637 | static tree | |
fa233e34 | 7638 | native_interpret_complex (tree type, const unsigned char *ptr, int len) |
78bf6e2f RS |
7639 | { |
7640 | tree etype, rpart, ipart; | |
7641 | int size; | |
7642 | ||
7643 | etype = TREE_TYPE (type); | |
7644 | size = GET_MODE_SIZE (TYPE_MODE (etype)); | |
7645 | if (size * 2 > len) | |
7646 | return NULL_TREE; | |
7647 | rpart = native_interpret_expr (etype, ptr, size); | |
7648 | if (!rpart) | |
7649 | return NULL_TREE; | |
7650 | ipart = native_interpret_expr (etype, ptr+size, size); | |
7651 | if (!ipart) | |
7652 | return NULL_TREE; | |
7653 | return build_complex (type, rpart, ipart); | |
7654 | } | |
7655 | ||
7656 | ||
7657 | /* Subroutine of native_interpret_expr. Interpret the contents of | |
7658 | the buffer PTR of length LEN as a VECTOR_CST of type TYPE. | |
7659 | If the buffer cannot be interpreted, return NULL_TREE. */ | |
7660 | ||
7661 | static tree | |
fa233e34 | 7662 | native_interpret_vector (tree type, const unsigned char *ptr, int len) |
78bf6e2f | 7663 | { |
d2a12ae7 | 7664 | tree etype, elem; |
78bf6e2f | 7665 | int i, size, count; |
d2a12ae7 | 7666 | tree *elements; |
78bf6e2f RS |
7667 | |
7668 | etype = TREE_TYPE (type); | |
7669 | size = GET_MODE_SIZE (TYPE_MODE (etype)); | |
7670 | count = TYPE_VECTOR_SUBPARTS (type); | |
7671 | if (size * count > len) | |
7672 | return NULL_TREE; | |
7673 | ||
d2a12ae7 | 7674 | elements = XALLOCAVEC (tree, count); |
78bf6e2f RS |
7675 | for (i = count - 1; i >= 0; i--) |
7676 | { | |
7677 | elem = native_interpret_expr (etype, ptr+(i*size), size); | |
7678 | if (!elem) | |
7679 | return NULL_TREE; | |
d2a12ae7 | 7680 | elements[i] = elem; |
78bf6e2f RS |
7681 | } |
7682 | return build_vector (type, elements); | |
7683 | } | |
7684 | ||
7685 | ||
75c40d56 | 7686 | /* Subroutine of fold_view_convert_expr. Interpret the contents of |
78bf6e2f RS |
7687 | the buffer PTR of length LEN as a constant of type TYPE. For |
7688 | INTEGRAL_TYPE_P we return an INTEGER_CST, for SCALAR_FLOAT_TYPE_P | |
7689 | we return a REAL_CST, etc... If the buffer cannot be interpreted, | |
7690 | return NULL_TREE. */ | |
7691 | ||
db136335 | 7692 | tree |
fa233e34 | 7693 | native_interpret_expr (tree type, const unsigned char *ptr, int len) |
78bf6e2f RS |
7694 | { |
7695 | switch (TREE_CODE (type)) | |
7696 | { | |
7697 | case INTEGER_TYPE: | |
7698 | case ENUMERAL_TYPE: | |
7699 | case BOOLEAN_TYPE: | |
6814f778 RG |
7700 | case POINTER_TYPE: |
7701 | case REFERENCE_TYPE: | |
78bf6e2f RS |
7702 | return native_interpret_int (type, ptr, len); |
7703 | ||
7704 | case REAL_TYPE: | |
7705 | return native_interpret_real (type, ptr, len); | |
7706 | ||
cc06c01d GJL |
7707 | case FIXED_POINT_TYPE: |
7708 | return native_interpret_fixed (type, ptr, len); | |
7709 | ||
78bf6e2f RS |
7710 | case COMPLEX_TYPE: |
7711 | return native_interpret_complex (type, ptr, len); | |
7712 | ||
7713 | case VECTOR_TYPE: | |
7714 | return native_interpret_vector (type, ptr, len); | |
7715 | ||
7716 | default: | |
7717 | return NULL_TREE; | |
7718 | } | |
7719 | } | |
7720 | ||
6814f778 RG |
7721 | /* Returns true if we can interpret the contents of a native encoding |
7722 | as TYPE. */ | |
7723 | ||
7724 | static bool | |
7725 | can_native_interpret_type_p (tree type) | |
7726 | { | |
7727 | switch (TREE_CODE (type)) | |
7728 | { | |
7729 | case INTEGER_TYPE: | |
7730 | case ENUMERAL_TYPE: | |
7731 | case BOOLEAN_TYPE: | |
7732 | case POINTER_TYPE: | |
7733 | case REFERENCE_TYPE: | |
cc06c01d | 7734 | case FIXED_POINT_TYPE: |
6814f778 RG |
7735 | case REAL_TYPE: |
7736 | case COMPLEX_TYPE: | |
7737 | case VECTOR_TYPE: | |
7738 | return true; | |
7739 | default: | |
7740 | return false; | |
7741 | } | |
7742 | } | |
78bf6e2f RS |
7743 | |
7744 | /* Fold a VIEW_CONVERT_EXPR of a constant expression EXPR to type | |
7745 | TYPE at compile-time. If we're unable to perform the conversion | |
7746 | return NULL_TREE. */ | |
7747 | ||
7748 | static tree | |
7749 | fold_view_convert_expr (tree type, tree expr) | |
7750 | { | |
7751 | /* We support up to 512-bit values (for V8DFmode). */ | |
7752 | unsigned char buffer[64]; | |
7753 | int len; | |
7754 | ||
7755 | /* Check that the host and target are sane. */ | |
7756 | if (CHAR_BIT != 8 || BITS_PER_UNIT != 8) | |
7757 | return NULL_TREE; | |
7758 | ||
7759 | len = native_encode_expr (expr, buffer, sizeof (buffer)); | |
7760 | if (len == 0) | |
7761 | return NULL_TREE; | |
7762 | ||
7763 | return native_interpret_expr (type, buffer, len); | |
7764 | } | |
7765 | ||
70826cbb | 7766 | /* Build an expression for the address of T. Folds away INDIRECT_REF |
628c189e | 7767 | to avoid confusing the gimplify process. */ |
70826cbb | 7768 | |
628c189e | 7769 | tree |
db3927fb | 7770 | build_fold_addr_expr_with_type_loc (location_t loc, tree t, tree ptrtype) |
70826cbb SP |
7771 | { |
7772 | /* The size of the object is not relevant when talking about its address. */ | |
7773 | if (TREE_CODE (t) == WITH_SIZE_EXPR) | |
7774 | t = TREE_OPERAND (t, 0); | |
7775 | ||
be1ac4ec | 7776 | if (TREE_CODE (t) == INDIRECT_REF) |
70826cbb SP |
7777 | { |
7778 | t = TREE_OPERAND (t, 0); | |
7779 | ||
7780 | if (TREE_TYPE (t) != ptrtype) | |
c9019218 | 7781 | t = build1_loc (loc, NOP_EXPR, ptrtype, t); |
70826cbb | 7782 | } |
70f34814 | 7783 | else if (TREE_CODE (t) == MEM_REF |
d8f56643 | 7784 | && integer_zerop (TREE_OPERAND (t, 1))) |
70f34814 | 7785 | return TREE_OPERAND (t, 0); |
d8f56643 RG |
7786 | else if (TREE_CODE (t) == MEM_REF |
7787 | && TREE_CODE (TREE_OPERAND (t, 0)) == INTEGER_CST) | |
7788 | return fold_binary (POINTER_PLUS_EXPR, ptrtype, | |
7789 | TREE_OPERAND (t, 0), | |
7790 | convert_to_ptrofftype (TREE_OPERAND (t, 1))); | |
d98e8686 EB |
7791 | else if (TREE_CODE (t) == VIEW_CONVERT_EXPR) |
7792 | { | |
db3927fb | 7793 | t = build_fold_addr_expr_loc (loc, TREE_OPERAND (t, 0)); |
d98e8686 EB |
7794 | |
7795 | if (TREE_TYPE (t) != ptrtype) | |
db3927fb | 7796 | t = fold_convert_loc (loc, ptrtype, t); |
d98e8686 | 7797 | } |
70826cbb | 7798 | else |
c9019218 | 7799 | t = build1_loc (loc, ADDR_EXPR, ptrtype, t); |
70826cbb SP |
7800 | |
7801 | return t; | |
7802 | } | |
7803 | ||
628c189e | 7804 | /* Build an expression for the address of T. */ |
70826cbb SP |
7805 | |
7806 | tree | |
db3927fb | 7807 | build_fold_addr_expr_loc (location_t loc, tree t) |
70826cbb SP |
7808 | { |
7809 | tree ptrtype = build_pointer_type (TREE_TYPE (t)); | |
7810 | ||
db3927fb | 7811 | return build_fold_addr_expr_with_type_loc (loc, t, ptrtype); |
70826cbb | 7812 | } |
78bf6e2f | 7813 | |
ed74d697 JJ |
7814 | static bool vec_cst_ctor_to_array (tree, tree *); |
7815 | ||
7107fa7c KH |
7816 | /* Fold a unary expression of code CODE and type TYPE with operand |
7817 | OP0. Return the folded expression if folding is successful. | |
7818 | Otherwise, return NULL_TREE. */ | |
659d8efa | 7819 | |
721425b6 | 7820 | tree |
db3927fb | 7821 | fold_unary_loc (location_t loc, enum tree_code code, tree type, tree op0) |
659d8efa | 7822 | { |
659d8efa | 7823 | tree tem; |
fbaa905c | 7824 | tree arg0; |
659d8efa KH |
7825 | enum tree_code_class kind = TREE_CODE_CLASS (code); |
7826 | ||
7827 | gcc_assert (IS_EXPR_CODE_CLASS (kind) | |
7828 | && TREE_CODE_LENGTH (code) == 1); | |
7829 | ||
fbaa905c | 7830 | arg0 = op0; |
659d8efa KH |
7831 | if (arg0) |
7832 | { | |
1a87cf0c | 7833 | if (CONVERT_EXPR_CODE_P (code) |
bbc210ab | 7834 | || code == FLOAT_EXPR || code == ABS_EXPR || code == NEGATE_EXPR) |
659d8efa | 7835 | { |
b49ceb45 JM |
7836 | /* Don't use STRIP_NOPS, because signedness of argument type |
7837 | matters. */ | |
659d8efa KH |
7838 | STRIP_SIGN_NOPS (arg0); |
7839 | } | |
7840 | else | |
7841 | { | |
7842 | /* Strip any conversions that don't change the mode. This | |
7843 | is safe for every expression, except for a comparison | |
7844 | expression because its signedness is derived from its | |
7845 | operands. | |
7846 | ||
7847 | Note that this is done as an internal manipulation within | |
7848 | the constant folder, in order to find the simplest | |
7849 | representation of the arguments so that their form can be | |
7850 | studied. In any cases, the appropriate type conversions | |
7851 | should be put back in the tree that will get out of the | |
7852 | constant folder. */ | |
7853 | STRIP_NOPS (arg0); | |
7854 | } | |
7855 | } | |
7856 | ||
7857 | if (TREE_CODE_CLASS (code) == tcc_unary) | |
7858 | { | |
7859 | if (TREE_CODE (arg0) == COMPOUND_EXPR) | |
7860 | return build2 (COMPOUND_EXPR, type, TREE_OPERAND (arg0, 0), | |
db3927fb AH |
7861 | fold_build1_loc (loc, code, type, |
7862 | fold_convert_loc (loc, TREE_TYPE (op0), | |
7863 | TREE_OPERAND (arg0, 1)))); | |
659d8efa KH |
7864 | else if (TREE_CODE (arg0) == COND_EXPR) |
7865 | { | |
7866 | tree arg01 = TREE_OPERAND (arg0, 1); | |
7867 | tree arg02 = TREE_OPERAND (arg0, 2); | |
7868 | if (! VOID_TYPE_P (TREE_TYPE (arg01))) | |
db3927fb AH |
7869 | arg01 = fold_build1_loc (loc, code, type, |
7870 | fold_convert_loc (loc, | |
7871 | TREE_TYPE (op0), arg01)); | |
659d8efa | 7872 | if (! VOID_TYPE_P (TREE_TYPE (arg02))) |
db3927fb AH |
7873 | arg02 = fold_build1_loc (loc, code, type, |
7874 | fold_convert_loc (loc, | |
7875 | TREE_TYPE (op0), arg02)); | |
7876 | tem = fold_build3_loc (loc, COND_EXPR, type, TREE_OPERAND (arg0, 0), | |
7f20a5b7 | 7877 | arg01, arg02); |
659d8efa KH |
7878 | |
7879 | /* If this was a conversion, and all we did was to move into | |
7880 | inside the COND_EXPR, bring it back out. But leave it if | |
7881 | it is a conversion from integer to integer and the | |
7882 | result precision is no wider than a word since such a | |
7883 | conversion is cheap and may be optimized away by combine, | |
7884 | while it couldn't if it were outside the COND_EXPR. Then return | |
7885 | so we don't get into an infinite recursion loop taking the | |
7886 | conversion out and then back in. */ | |
7887 | ||
1a87cf0c | 7888 | if ((CONVERT_EXPR_CODE_P (code) |
659d8efa KH |
7889 | || code == NON_LVALUE_EXPR) |
7890 | && TREE_CODE (tem) == COND_EXPR | |
7891 | && TREE_CODE (TREE_OPERAND (tem, 1)) == code | |
7892 | && TREE_CODE (TREE_OPERAND (tem, 2)) == code | |
7893 | && ! VOID_TYPE_P (TREE_OPERAND (tem, 1)) | |
7894 | && ! VOID_TYPE_P (TREE_OPERAND (tem, 2)) | |
7895 | && (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem, 1), 0)) | |
7896 | == TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem, 2), 0))) | |
7897 | && (! (INTEGRAL_TYPE_P (TREE_TYPE (tem)) | |
7898 | && (INTEGRAL_TYPE_P | |
7899 | (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem, 1), 0)))) | |
7900 | && TYPE_PRECISION (TREE_TYPE (tem)) <= BITS_PER_WORD) | |
7901 | || flag_syntax_only)) | |
c9019218 JJ |
7902 | tem = build1_loc (loc, code, type, |
7903 | build3 (COND_EXPR, | |
7904 | TREE_TYPE (TREE_OPERAND | |
7905 | (TREE_OPERAND (tem, 1), 0)), | |
7906 | TREE_OPERAND (tem, 0), | |
7907 | TREE_OPERAND (TREE_OPERAND (tem, 1), 0), | |
7908 | TREE_OPERAND (TREE_OPERAND (tem, 2), | |
7909 | 0))); | |
659d8efa KH |
7910 | return tem; |
7911 | } | |
659d8efa KH |
7912 | } |
7913 | ||
7914 | switch (code) | |
7915 | { | |
dedd42d5 RG |
7916 | case PAREN_EXPR: |
7917 | /* Re-association barriers around constants and other re-association | |
7918 | barriers can be removed. */ | |
7919 | if (CONSTANT_CLASS_P (op0) | |
7920 | || TREE_CODE (op0) == PAREN_EXPR) | |
db3927fb | 7921 | return fold_convert_loc (loc, type, op0); |
dedd42d5 RG |
7922 | return NULL_TREE; |
7923 | ||
1043771b | 7924 | CASE_CONVERT: |
659d8efa | 7925 | case FLOAT_EXPR: |
659d8efa | 7926 | case FIX_TRUNC_EXPR: |
4b58fc4d KH |
7927 | if (TREE_TYPE (op0) == type) |
7928 | return op0; | |
b8698a0f | 7929 | |
f8f972fc RB |
7930 | if (COMPARISON_CLASS_P (op0)) |
7931 | { | |
7932 | /* If we have (type) (a CMP b) and type is an integral type, return | |
c2299dfe RG |
7933 | new expression involving the new type. Canonicalize |
7934 | (type) (a CMP b) to (a CMP b) ? (type) true : (type) false for | |
7935 | non-integral type. | |
7936 | Do not fold the result as that would not simplify further, also | |
7937 | folding again results in recursions. */ | |
7f3ff782 | 7938 | if (TREE_CODE (type) == BOOLEAN_TYPE) |
c2299dfe RG |
7939 | return build2_loc (loc, TREE_CODE (op0), type, |
7940 | TREE_OPERAND (op0, 0), | |
7941 | TREE_OPERAND (op0, 1)); | |
4d6503ab MG |
7942 | else if (!INTEGRAL_TYPE_P (type) && !VOID_TYPE_P (type) |
7943 | && TREE_CODE (type) != VECTOR_TYPE) | |
c2299dfe | 7944 | return build3_loc (loc, COND_EXPR, type, op0, |
544d960a AS |
7945 | constant_boolean_node (true, type), |
7946 | constant_boolean_node (false, type)); | |
f8f972fc | 7947 | } |
659d8efa KH |
7948 | |
7949 | /* Handle cases of two conversions in a row. */ | |
1043771b | 7950 | if (CONVERT_EXPR_P (op0)) |
659d8efa | 7951 | { |
4b58fc4d KH |
7952 | tree inside_type = TREE_TYPE (TREE_OPERAND (op0, 0)); |
7953 | tree inter_type = TREE_TYPE (op0); | |
659d8efa KH |
7954 | int inside_int = INTEGRAL_TYPE_P (inside_type); |
7955 | int inside_ptr = POINTER_TYPE_P (inside_type); | |
7956 | int inside_float = FLOAT_TYPE_P (inside_type); | |
4b8d544b | 7957 | int inside_vec = TREE_CODE (inside_type) == VECTOR_TYPE; |
659d8efa KH |
7958 | unsigned int inside_prec = TYPE_PRECISION (inside_type); |
7959 | int inside_unsignedp = TYPE_UNSIGNED (inside_type); | |
7960 | int inter_int = INTEGRAL_TYPE_P (inter_type); | |
7961 | int inter_ptr = POINTER_TYPE_P (inter_type); | |
7962 | int inter_float = FLOAT_TYPE_P (inter_type); | |
4b8d544b | 7963 | int inter_vec = TREE_CODE (inter_type) == VECTOR_TYPE; |
659d8efa KH |
7964 | unsigned int inter_prec = TYPE_PRECISION (inter_type); |
7965 | int inter_unsignedp = TYPE_UNSIGNED (inter_type); | |
7966 | int final_int = INTEGRAL_TYPE_P (type); | |
7967 | int final_ptr = POINTER_TYPE_P (type); | |
7968 | int final_float = FLOAT_TYPE_P (type); | |
4b8d544b | 7969 | int final_vec = TREE_CODE (type) == VECTOR_TYPE; |
659d8efa KH |
7970 | unsigned int final_prec = TYPE_PRECISION (type); |
7971 | int final_unsignedp = TYPE_UNSIGNED (type); | |
7972 | ||
7973 | /* In addition to the cases of two conversions in a row | |
7974 | handled below, if we are converting something to its own | |
7975 | type via an object of identical or wider precision, neither | |
7976 | conversion is needed. */ | |
7977 | if (TYPE_MAIN_VARIANT (inside_type) == TYPE_MAIN_VARIANT (type) | |
497cfe24 RG |
7978 | && (((inter_int || inter_ptr) && final_int) |
7979 | || (inter_float && final_float)) | |
659d8efa | 7980 | && inter_prec >= final_prec) |
db3927fb | 7981 | return fold_build1_loc (loc, code, type, TREE_OPERAND (op0, 0)); |
659d8efa | 7982 | |
1803581d EB |
7983 | /* Likewise, if the intermediate and initial types are either both |
7984 | float or both integer, we don't need the middle conversion if the | |
7985 | former is wider than the latter and doesn't change the signedness | |
7986 | (for integers). Avoid this if the final type is a pointer since | |
7987 | then we sometimes need the middle conversion. Likewise if the | |
7988 | final type has a precision not equal to the size of its mode. */ | |
6aa12f4f | 7989 | if (((inter_int && inside_int) |
4b8d544b JJ |
7990 | || (inter_float && inside_float) |
7991 | || (inter_vec && inside_vec)) | |
659d8efa | 7992 | && inter_prec >= inside_prec |
4b8d544b JJ |
7993 | && (inter_float || inter_vec |
7994 | || inter_unsignedp == inside_unsignedp) | |
d3ea1dbd | 7995 | && ! (final_prec != GET_MODE_PRECISION (TYPE_MODE (type)) |
659d8efa | 7996 | && TYPE_MODE (type) == TYPE_MODE (inter_type)) |
4b8d544b JJ |
7997 | && ! final_ptr |
7998 | && (! final_vec || inter_prec == inside_prec)) | |
db3927fb | 7999 | return fold_build1_loc (loc, code, type, TREE_OPERAND (op0, 0)); |
659d8efa KH |
8000 | |
8001 | /* If we have a sign-extension of a zero-extended value, we can | |
1caf8dd6 RG |
8002 | replace that by a single zero-extension. Likewise if the |
8003 | final conversion does not change precision we can drop the | |
8004 | intermediate conversion. */ | |
659d8efa | 8005 | if (inside_int && inter_int && final_int |
1caf8dd6 RG |
8006 | && ((inside_prec < inter_prec && inter_prec < final_prec |
8007 | && inside_unsignedp && !inter_unsignedp) | |
8008 | || final_prec == inter_prec)) | |
db3927fb | 8009 | return fold_build1_loc (loc, code, type, TREE_OPERAND (op0, 0)); |
659d8efa KH |
8010 | |
8011 | /* Two conversions in a row are not needed unless: | |
8012 | - some conversion is floating-point (overstrict for now), or | |
4b8d544b | 8013 | - some conversion is a vector (overstrict for now), or |
659d8efa KH |
8014 | - the intermediate type is narrower than both initial and |
8015 | final, or | |
8016 | - the intermediate type and innermost type differ in signedness, | |
8017 | and the outermost type is wider than the intermediate, or | |
8018 | - the initial type is a pointer type and the precisions of the | |
8019 | intermediate and final types differ, or | |
8020 | - the final type is a pointer type and the precisions of the | |
c4e5b5a8 | 8021 | initial and intermediate types differ. */ |
659d8efa | 8022 | if (! inside_float && ! inter_float && ! final_float |
4b8d544b | 8023 | && ! inside_vec && ! inter_vec && ! final_vec |
497cfe24 | 8024 | && (inter_prec >= inside_prec || inter_prec >= final_prec) |
659d8efa KH |
8025 | && ! (inside_int && inter_int |
8026 | && inter_unsignedp != inside_unsignedp | |
8027 | && inter_prec < final_prec) | |
8028 | && ((inter_unsignedp && inter_prec > inside_prec) | |
8029 | == (final_unsignedp && final_prec > inter_prec)) | |
8030 | && ! (inside_ptr && inter_prec != final_prec) | |
8031 | && ! (final_ptr && inside_prec != inter_prec) | |
d3ea1dbd | 8032 | && ! (final_prec != GET_MODE_PRECISION (TYPE_MODE (type)) |
c4e5b5a8 | 8033 | && TYPE_MODE (type) == TYPE_MODE (inter_type))) |
db3927fb | 8034 | return fold_build1_loc (loc, code, type, TREE_OPERAND (op0, 0)); |
659d8efa KH |
8035 | } |
8036 | ||
46c0a59d | 8037 | /* Handle (T *)&A.B.C for A being of type T and B and C |
a4174ebf | 8038 | living at offset zero. This occurs frequently in |
46c0a59d RG |
8039 | C++ upcasting and then accessing the base. */ |
8040 | if (TREE_CODE (op0) == ADDR_EXPR | |
8041 | && POINTER_TYPE_P (type) | |
8042 | && handled_component_p (TREE_OPERAND (op0, 0))) | |
8043 | { | |
8044 | HOST_WIDE_INT bitsize, bitpos; | |
8045 | tree offset; | |
8046 | enum machine_mode mode; | |
8047 | int unsignedp, volatilep; | |
8048 | tree base = TREE_OPERAND (op0, 0); | |
8049 | base = get_inner_reference (base, &bitsize, &bitpos, &offset, | |
8050 | &mode, &unsignedp, &volatilep, false); | |
8051 | /* If the reference was to a (constant) zero offset, we can use | |
8052 | the address of the base if it has the same base type | |
2ea9dc64 | 8053 | as the result type and the pointer type is unqualified. */ |
46c0a59d | 8054 | if (! offset && bitpos == 0 |
2ea9dc64 | 8055 | && (TYPE_MAIN_VARIANT (TREE_TYPE (type)) |
46c0a59d | 8056 | == TYPE_MAIN_VARIANT (TREE_TYPE (base))) |
2ea9dc64 | 8057 | && TYPE_QUALS (type) == TYPE_UNQUALIFIED) |
db3927fb AH |
8058 | return fold_convert_loc (loc, type, |
8059 | build_fold_addr_expr_loc (loc, base)); | |
46c0a59d RG |
8060 | } |
8061 | ||
726a989a RB |
8062 | if (TREE_CODE (op0) == MODIFY_EXPR |
8063 | && TREE_CONSTANT (TREE_OPERAND (op0, 1)) | |
659d8efa | 8064 | /* Detect assigning a bitfield. */ |
726a989a | 8065 | && !(TREE_CODE (TREE_OPERAND (op0, 0)) == COMPONENT_REF |
07beea0d | 8066 | && DECL_BIT_FIELD |
726a989a | 8067 | (TREE_OPERAND (TREE_OPERAND (op0, 0), 1)))) |
659d8efa KH |
8068 | { |
8069 | /* Don't leave an assignment inside a conversion | |
8070 | unless assigning a bitfield. */ | |
db3927fb | 8071 | tem = fold_build1_loc (loc, code, type, TREE_OPERAND (op0, 1)); |
659d8efa | 8072 | /* First do the assignment, then return converted constant. */ |
c9019218 | 8073 | tem = build2_loc (loc, COMPOUND_EXPR, TREE_TYPE (tem), op0, tem); |
659d8efa KH |
8074 | TREE_NO_WARNING (tem) = 1; |
8075 | TREE_USED (tem) = 1; | |
8076 | return tem; | |
8077 | } | |
8078 | ||
8079 | /* Convert (T)(x & c) into (T)x & (T)c, if c is an integer | |
8080 | constants (if x has signed type, the sign bit cannot be set | |
bfab40f8 EB |
8081 | in c). This folds extension into the BIT_AND_EXPR. |
8082 | ??? We don't do it for BOOLEAN_TYPE or ENUMERAL_TYPE because they | |
8083 | very likely don't have maximal range for their precision and this | |
8084 | transformation effectively doesn't preserve non-maximal ranges. */ | |
1e17e15a | 8085 | if (TREE_CODE (type) == INTEGER_TYPE |
4b58fc4d | 8086 | && TREE_CODE (op0) == BIT_AND_EXPR |
84fb43a1 | 8087 | && TREE_CODE (TREE_OPERAND (op0, 1)) == INTEGER_CST) |
659d8efa | 8088 | { |
3d8b2a98 ILT |
8089 | tree and_expr = op0; |
8090 | tree and0 = TREE_OPERAND (and_expr, 0); | |
8091 | tree and1 = TREE_OPERAND (and_expr, 1); | |
659d8efa KH |
8092 | int change = 0; |
8093 | ||
3d8b2a98 | 8094 | if (TYPE_UNSIGNED (TREE_TYPE (and_expr)) |
659d8efa | 8095 | || (TYPE_PRECISION (type) |
3d8b2a98 | 8096 | <= TYPE_PRECISION (TREE_TYPE (and_expr)))) |
659d8efa KH |
8097 | change = 1; |
8098 | else if (TYPE_PRECISION (TREE_TYPE (and1)) | |
8099 | <= HOST_BITS_PER_WIDE_INT | |
8100 | && host_integerp (and1, 1)) | |
8101 | { | |
8102 | unsigned HOST_WIDE_INT cst; | |
8103 | ||
8104 | cst = tree_low_cst (and1, 1); | |
0cadbfaa | 8105 | cst &= HOST_WIDE_INT_M1U |
659d8efa KH |
8106 | << (TYPE_PRECISION (TREE_TYPE (and1)) - 1); |
8107 | change = (cst == 0); | |
8108 | #ifdef LOAD_EXTEND_OP | |
8109 | if (change | |
8110 | && !flag_syntax_only | |
8111 | && (LOAD_EXTEND_OP (TYPE_MODE (TREE_TYPE (and0))) | |
8112 | == ZERO_EXTEND)) | |
8113 | { | |
ca5ba2a3 | 8114 | tree uns = unsigned_type_for (TREE_TYPE (and0)); |
db3927fb AH |
8115 | and0 = fold_convert_loc (loc, uns, and0); |
8116 | and1 = fold_convert_loc (loc, uns, and1); | |
659d8efa KH |
8117 | } |
8118 | #endif | |
8119 | } | |
8120 | if (change) | |
8121 | { | |
9589f23e AS |
8122 | tem = force_fit_type_double (type, tree_to_double_int (and1), |
8123 | 0, TREE_OVERFLOW (and1)); | |
db3927fb AH |
8124 | return fold_build2_loc (loc, BIT_AND_EXPR, type, |
8125 | fold_convert_loc (loc, type, and0), tem); | |
659d8efa KH |
8126 | } |
8127 | } | |
8128 | ||
5be014d5 | 8129 | /* Convert (T1)(X p+ Y) into ((T1)X p+ Y), for pointer type, |
ac5a28a6 | 8130 | when one of the new casts will fold away. Conservatively we assume |
5be014d5 AP |
8131 | that this happens when X or Y is NOP_EXPR or Y is INTEGER_CST. */ |
8132 | if (POINTER_TYPE_P (type) | |
8133 | && TREE_CODE (arg0) == POINTER_PLUS_EXPR | |
f548a317 | 8134 | && (!TYPE_RESTRICT (type) || TYPE_RESTRICT (TREE_TYPE (arg0))) |
ac5a28a6 JH |
8135 | && (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST |
8136 | || TREE_CODE (TREE_OPERAND (arg0, 0)) == NOP_EXPR | |
8137 | || TREE_CODE (TREE_OPERAND (arg0, 1)) == NOP_EXPR)) | |
659d8efa KH |
8138 | { |
8139 | tree arg00 = TREE_OPERAND (arg0, 0); | |
ac5a28a6 JH |
8140 | tree arg01 = TREE_OPERAND (arg0, 1); |
8141 | ||
0d82a1c8 RG |
8142 | return fold_build_pointer_plus_loc |
8143 | (loc, fold_convert_loc (loc, type, arg00), arg01); | |
659d8efa KH |
8144 | } |
8145 | ||
e8206491 | 8146 | /* Convert (T1)(~(T2)X) into ~(T1)X if T1 and T2 are integral types |
110abdbc | 8147 | of the same precision, and X is an integer type not narrower than |
e8206491 RS |
8148 | types T1 or T2, i.e. the cast (T2)X isn't an extension. */ |
8149 | if (INTEGRAL_TYPE_P (type) | |
8150 | && TREE_CODE (op0) == BIT_NOT_EXPR | |
8151 | && INTEGRAL_TYPE_P (TREE_TYPE (op0)) | |
1043771b | 8152 | && CONVERT_EXPR_P (TREE_OPERAND (op0, 0)) |
e8206491 RS |
8153 | && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (op0))) |
8154 | { | |
8155 | tem = TREE_OPERAND (TREE_OPERAND (op0, 0), 0); | |
8156 | if (INTEGRAL_TYPE_P (TREE_TYPE (tem)) | |
8157 | && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (tem))) | |
db3927fb AH |
8158 | return fold_build1_loc (loc, BIT_NOT_EXPR, type, |
8159 | fold_convert_loc (loc, type, tem)); | |
e8206491 RS |
8160 | } |
8161 | ||
c83bd37c PB |
8162 | /* Convert (T1)(X * Y) into (T1)X * (T1)Y if T1 is narrower than the |
8163 | type of X and Y (integer types only). */ | |
8164 | if (INTEGRAL_TYPE_P (type) | |
8165 | && TREE_CODE (op0) == MULT_EXPR | |
8166 | && INTEGRAL_TYPE_P (TREE_TYPE (op0)) | |
8167 | && TYPE_PRECISION (type) < TYPE_PRECISION (TREE_TYPE (op0))) | |
8168 | { | |
8169 | /* Be careful not to introduce new overflows. */ | |
8170 | tree mult_type; | |
8171 | if (TYPE_OVERFLOW_WRAPS (type)) | |
8172 | mult_type = type; | |
8173 | else | |
8174 | mult_type = unsigned_type_for (type); | |
b7785654 JJ |
8175 | |
8176 | if (TYPE_PRECISION (mult_type) < TYPE_PRECISION (TREE_TYPE (op0))) | |
8177 | { | |
db3927fb AH |
8178 | tem = fold_build2_loc (loc, MULT_EXPR, mult_type, |
8179 | fold_convert_loc (loc, mult_type, | |
8180 | TREE_OPERAND (op0, 0)), | |
8181 | fold_convert_loc (loc, mult_type, | |
8182 | TREE_OPERAND (op0, 1))); | |
8183 | return fold_convert_loc (loc, type, tem); | |
b7785654 | 8184 | } |
c83bd37c PB |
8185 | } |
8186 | ||
84ece8ef | 8187 | tem = fold_convert_const (code, type, op0); |
62ab45cc | 8188 | return tem ? tem : NULL_TREE; |
659d8efa | 8189 | |
09e881c9 BE |
8190 | case ADDR_SPACE_CONVERT_EXPR: |
8191 | if (integer_zerop (arg0)) | |
8192 | return fold_convert_const (code, type, arg0); | |
8193 | return NULL_TREE; | |
8194 | ||
325217ed CF |
8195 | case FIXED_CONVERT_EXPR: |
8196 | tem = fold_convert_const (code, type, arg0); | |
8197 | return tem ? tem : NULL_TREE; | |
8198 | ||
659d8efa | 8199 | case VIEW_CONVERT_EXPR: |
f85242f0 RS |
8200 | if (TREE_TYPE (op0) == type) |
8201 | return op0; | |
9a327766 | 8202 | if (TREE_CODE (op0) == VIEW_CONVERT_EXPR) |
db3927fb AH |
8203 | return fold_build1_loc (loc, VIEW_CONVERT_EXPR, |
8204 | type, TREE_OPERAND (op0, 0)); | |
70f34814 RG |
8205 | if (TREE_CODE (op0) == MEM_REF) |
8206 | return fold_build2_loc (loc, MEM_REF, type, | |
8207 | TREE_OPERAND (op0, 0), TREE_OPERAND (op0, 1)); | |
9a327766 RG |
8208 | |
8209 | /* For integral conversions with the same precision or pointer | |
8210 | conversions use a NOP_EXPR instead. */ | |
3d45dd59 RG |
8211 | if ((INTEGRAL_TYPE_P (type) |
8212 | || POINTER_TYPE_P (type)) | |
8213 | && (INTEGRAL_TYPE_P (TREE_TYPE (op0)) | |
8214 | || POINTER_TYPE_P (TREE_TYPE (op0))) | |
84fb43a1 | 8215 | && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (op0))) |
db3927fb | 8216 | return fold_convert_loc (loc, type, op0); |
9a327766 RG |
8217 | |
8218 | /* Strip inner integral conversions that do not change the precision. */ | |
1043771b | 8219 | if (CONVERT_EXPR_P (op0) |
3d45dd59 RG |
8220 | && (INTEGRAL_TYPE_P (TREE_TYPE (op0)) |
8221 | || POINTER_TYPE_P (TREE_TYPE (op0))) | |
8222 | && (INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (op0, 0))) | |
8223 | || POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (op0, 0)))) | |
9a327766 RG |
8224 | && (TYPE_PRECISION (TREE_TYPE (op0)) |
8225 | == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op0, 0))))) | |
db3927fb AH |
8226 | return fold_build1_loc (loc, VIEW_CONVERT_EXPR, |
8227 | type, TREE_OPERAND (op0, 0)); | |
9a327766 | 8228 | |
78bf6e2f | 8229 | return fold_view_convert_expr (type, op0); |
659d8efa KH |
8230 | |
8231 | case NEGATE_EXPR: | |
db3927fb | 8232 | tem = fold_negate_expr (loc, arg0); |
1af8dcbf | 8233 | if (tem) |
db3927fb | 8234 | return fold_convert_loc (loc, type, tem); |
62ab45cc | 8235 | return NULL_TREE; |
659d8efa KH |
8236 | |
8237 | case ABS_EXPR: | |
8238 | if (TREE_CODE (arg0) == INTEGER_CST || TREE_CODE (arg0) == REAL_CST) | |
8239 | return fold_abs_const (arg0, type); | |
8240 | else if (TREE_CODE (arg0) == NEGATE_EXPR) | |
db3927fb | 8241 | return fold_build1_loc (loc, ABS_EXPR, type, TREE_OPERAND (arg0, 0)); |
659d8efa KH |
8242 | /* Convert fabs((double)float) into (double)fabsf(float). */ |
8243 | else if (TREE_CODE (arg0) == NOP_EXPR | |
8244 | && TREE_CODE (type) == REAL_TYPE) | |
8245 | { | |
8246 | tree targ0 = strip_float_extensions (arg0); | |
8247 | if (targ0 != arg0) | |
db3927fb AH |
8248 | return fold_convert_loc (loc, type, |
8249 | fold_build1_loc (loc, ABS_EXPR, | |
8250 | TREE_TYPE (targ0), | |
8251 | targ0)); | |
659d8efa | 8252 | } |
1ade5842 | 8253 | /* ABS_EXPR<ABS_EXPR<x>> = ABS_EXPR<x> even if flag_wrapv is on. */ |
6ac01510 ILT |
8254 | else if (TREE_CODE (arg0) == ABS_EXPR) |
8255 | return arg0; | |
8256 | else if (tree_expr_nonnegative_p (arg0)) | |
659d8efa KH |
8257 | return arg0; |
8258 | ||
8259 | /* Strip sign ops from argument. */ | |
8260 | if (TREE_CODE (type) == REAL_TYPE) | |
8261 | { | |
8262 | tem = fold_strip_sign_ops (arg0); | |
8263 | if (tem) | |
db3927fb AH |
8264 | return fold_build1_loc (loc, ABS_EXPR, type, |
8265 | fold_convert_loc (loc, type, tem)); | |
659d8efa | 8266 | } |
62ab45cc | 8267 | return NULL_TREE; |
659d8efa KH |
8268 | |
8269 | case CONJ_EXPR: | |
8270 | if (TREE_CODE (TREE_TYPE (arg0)) != COMPLEX_TYPE) | |
db3927fb | 8271 | return fold_convert_loc (loc, type, arg0); |
9734ebaf RS |
8272 | if (TREE_CODE (arg0) == COMPLEX_EXPR) |
8273 | { | |
8274 | tree itype = TREE_TYPE (type); | |
db3927fb AH |
8275 | tree rpart = fold_convert_loc (loc, itype, TREE_OPERAND (arg0, 0)); |
8276 | tree ipart = fold_convert_loc (loc, itype, TREE_OPERAND (arg0, 1)); | |
8277 | return fold_build2_loc (loc, COMPLEX_EXPR, type, rpart, | |
8278 | negate_expr (ipart)); | |
9734ebaf RS |
8279 | } |
8280 | if (TREE_CODE (arg0) == COMPLEX_CST) | |
8281 | { | |
8282 | tree itype = TREE_TYPE (type); | |
db3927fb AH |
8283 | tree rpart = fold_convert_loc (loc, itype, TREE_REALPART (arg0)); |
8284 | tree ipart = fold_convert_loc (loc, itype, TREE_IMAGPART (arg0)); | |
9734ebaf RS |
8285 | return build_complex (type, rpart, negate_expr (ipart)); |
8286 | } | |
8287 | if (TREE_CODE (arg0) == CONJ_EXPR) | |
db3927fb | 8288 | return fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0)); |
62ab45cc | 8289 | return NULL_TREE; |
659d8efa KH |
8290 | |
8291 | case BIT_NOT_EXPR: | |
8292 | if (TREE_CODE (arg0) == INTEGER_CST) | |
8293 | return fold_not_const (arg0, type); | |
8294 | else if (TREE_CODE (arg0) == BIT_NOT_EXPR) | |
db3927fb | 8295 | return fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0)); |
659d8efa KH |
8296 | /* Convert ~ (-A) to A - 1. */ |
8297 | else if (INTEGRAL_TYPE_P (type) && TREE_CODE (arg0) == NEGATE_EXPR) | |
db3927fb AH |
8298 | return fold_build2_loc (loc, MINUS_EXPR, type, |
8299 | fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0)), | |
7f20a5b7 | 8300 | build_int_cst (type, 1)); |
659d8efa KH |
8301 | /* Convert ~ (A - 1) or ~ (A + -1) to -A. */ |
8302 | else if (INTEGRAL_TYPE_P (type) | |
8303 | && ((TREE_CODE (arg0) == MINUS_EXPR | |
8304 | && integer_onep (TREE_OPERAND (arg0, 1))) | |
8305 | || (TREE_CODE (arg0) == PLUS_EXPR | |
8306 | && integer_all_onesp (TREE_OPERAND (arg0, 1))))) | |
db3927fb AH |
8307 | return fold_build1_loc (loc, NEGATE_EXPR, type, |
8308 | fold_convert_loc (loc, type, | |
8309 | TREE_OPERAND (arg0, 0))); | |
f242e769 JM |
8310 | /* Convert ~(X ^ Y) to ~X ^ Y or X ^ ~Y if ~X or ~Y simplify. */ |
8311 | else if (TREE_CODE (arg0) == BIT_XOR_EXPR | |
db3927fb AH |
8312 | && (tem = fold_unary_loc (loc, BIT_NOT_EXPR, type, |
8313 | fold_convert_loc (loc, type, | |
8314 | TREE_OPERAND (arg0, 0))))) | |
8315 | return fold_build2_loc (loc, BIT_XOR_EXPR, type, tem, | |
8316 | fold_convert_loc (loc, type, | |
8317 | TREE_OPERAND (arg0, 1))); | |
f242e769 | 8318 | else if (TREE_CODE (arg0) == BIT_XOR_EXPR |
db3927fb AH |
8319 | && (tem = fold_unary_loc (loc, BIT_NOT_EXPR, type, |
8320 | fold_convert_loc (loc, type, | |
8321 | TREE_OPERAND (arg0, 1))))) | |
8322 | return fold_build2_loc (loc, BIT_XOR_EXPR, type, | |
8323 | fold_convert_loc (loc, type, | |
8324 | TREE_OPERAND (arg0, 0)), tem); | |
c01ee935 JJ |
8325 | /* Perform BIT_NOT_EXPR on each element individually. */ |
8326 | else if (TREE_CODE (arg0) == VECTOR_CST) | |
8327 | { | |
d2a12ae7 RG |
8328 | tree *elements; |
8329 | tree elem; | |
8330 | unsigned count = VECTOR_CST_NELTS (arg0), i; | |
c01ee935 | 8331 | |
d2a12ae7 | 8332 | elements = XALLOCAVEC (tree, count); |
c01ee935 JJ |
8333 | for (i = 0; i < count; i++) |
8334 | { | |
d2a12ae7 RG |
8335 | elem = VECTOR_CST_ELT (arg0, i); |
8336 | elem = fold_unary_loc (loc, BIT_NOT_EXPR, TREE_TYPE (type), elem); | |
8337 | if (elem == NULL_TREE) | |
8338 | break; | |
8339 | elements[i] = elem; | |
c01ee935 JJ |
8340 | } |
8341 | if (i == count) | |
d2a12ae7 | 8342 | return build_vector (type, elements); |
c01ee935 | 8343 | } |
418d1b87 MG |
8344 | else if (COMPARISON_CLASS_P (arg0) |
8345 | && (VECTOR_TYPE_P (type) | |
8346 | || (INTEGRAL_TYPE_P (type) && TYPE_PRECISION (type) == 1))) | |
8347 | { | |
8348 | tree op_type = TREE_TYPE (TREE_OPERAND (arg0, 0)); | |
8349 | enum tree_code subcode = invert_tree_comparison (TREE_CODE (arg0), | |
8350 | HONOR_NANS (TYPE_MODE (op_type))); | |
8351 | if (subcode != ERROR_MARK) | |
8352 | return build2_loc (loc, subcode, type, TREE_OPERAND (arg0, 0), | |
8353 | TREE_OPERAND (arg0, 1)); | |
8354 | } | |
8355 | ||
f242e769 | 8356 | |
62ab45cc | 8357 | return NULL_TREE; |
659d8efa KH |
8358 | |
8359 | case TRUTH_NOT_EXPR: | |
659d8efa KH |
8360 | /* Note that the operand of this must be an int |
8361 | and its values must be 0 or 1. | |
8362 | ("true" is a fixed value perhaps depending on the language, | |
8363 | but we don't handle values other than 1 correctly yet.) */ | |
db3927fb | 8364 | tem = fold_truth_not_expr (loc, arg0); |
d817ed3b | 8365 | if (!tem) |
62ab45cc | 8366 | return NULL_TREE; |
db3927fb | 8367 | return fold_convert_loc (loc, type, tem); |
659d8efa KH |
8368 | |
8369 | case REALPART_EXPR: | |
8370 | if (TREE_CODE (TREE_TYPE (arg0)) != COMPLEX_TYPE) | |
db3927fb | 8371 | return fold_convert_loc (loc, type, arg0); |
9734ebaf | 8372 | if (TREE_CODE (arg0) == COMPLEX_EXPR) |
db3927fb | 8373 | return omit_one_operand_loc (loc, type, TREE_OPERAND (arg0, 0), |
659d8efa | 8374 | TREE_OPERAND (arg0, 1)); |
9734ebaf | 8375 | if (TREE_CODE (arg0) == COMPLEX_CST) |
db3927fb | 8376 | return fold_convert_loc (loc, type, TREE_REALPART (arg0)); |
9734ebaf RS |
8377 | if (TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR) |
8378 | { | |
8379 | tree itype = TREE_TYPE (TREE_TYPE (arg0)); | |
db3927fb AH |
8380 | tem = fold_build2_loc (loc, TREE_CODE (arg0), itype, |
8381 | fold_build1_loc (loc, REALPART_EXPR, itype, | |
9734ebaf | 8382 | TREE_OPERAND (arg0, 0)), |
db3927fb | 8383 | fold_build1_loc (loc, REALPART_EXPR, itype, |
9734ebaf | 8384 | TREE_OPERAND (arg0, 1))); |
db3927fb | 8385 | return fold_convert_loc (loc, type, tem); |
9734ebaf RS |
8386 | } |
8387 | if (TREE_CODE (arg0) == CONJ_EXPR) | |
8388 | { | |
8389 | tree itype = TREE_TYPE (TREE_TYPE (arg0)); | |
db3927fb AH |
8390 | tem = fold_build1_loc (loc, REALPART_EXPR, itype, |
8391 | TREE_OPERAND (arg0, 0)); | |
8392 | return fold_convert_loc (loc, type, tem); | |
9734ebaf | 8393 | } |
85aef79f RG |
8394 | if (TREE_CODE (arg0) == CALL_EXPR) |
8395 | { | |
8396 | tree fn = get_callee_fndecl (arg0); | |
111f1fca | 8397 | if (fn && DECL_BUILT_IN_CLASS (fn) == BUILT_IN_NORMAL) |
85aef79f RG |
8398 | switch (DECL_FUNCTION_CODE (fn)) |
8399 | { | |
8400 | CASE_FLT_FN (BUILT_IN_CEXPI): | |
8401 | fn = mathfn_built_in (type, BUILT_IN_COS); | |
2d38026b | 8402 | if (fn) |
db3927fb | 8403 | return build_call_expr_loc (loc, fn, 1, CALL_EXPR_ARG (arg0, 0)); |
2d38026b | 8404 | break; |
85aef79f | 8405 | |
2d38026b RS |
8406 | default: |
8407 | break; | |
85aef79f RG |
8408 | } |
8409 | } | |
62ab45cc | 8410 | return NULL_TREE; |
659d8efa KH |
8411 | |
8412 | case IMAGPART_EXPR: | |
8413 | if (TREE_CODE (TREE_TYPE (arg0)) != COMPLEX_TYPE) | |
e8160c9a | 8414 | return build_zero_cst (type); |
9734ebaf | 8415 | if (TREE_CODE (arg0) == COMPLEX_EXPR) |
db3927fb | 8416 | return omit_one_operand_loc (loc, type, TREE_OPERAND (arg0, 1), |
659d8efa | 8417 | TREE_OPERAND (arg0, 0)); |
9734ebaf | 8418 | if (TREE_CODE (arg0) == COMPLEX_CST) |
db3927fb | 8419 | return fold_convert_loc (loc, type, TREE_IMAGPART (arg0)); |
9734ebaf RS |
8420 | if (TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR) |
8421 | { | |
8422 | tree itype = TREE_TYPE (TREE_TYPE (arg0)); | |
db3927fb AH |
8423 | tem = fold_build2_loc (loc, TREE_CODE (arg0), itype, |
8424 | fold_build1_loc (loc, IMAGPART_EXPR, itype, | |
9734ebaf | 8425 | TREE_OPERAND (arg0, 0)), |
db3927fb | 8426 | fold_build1_loc (loc, IMAGPART_EXPR, itype, |
9734ebaf | 8427 | TREE_OPERAND (arg0, 1))); |
db3927fb | 8428 | return fold_convert_loc (loc, type, tem); |
9734ebaf RS |
8429 | } |
8430 | if (TREE_CODE (arg0) == CONJ_EXPR) | |
8431 | { | |
8432 | tree itype = TREE_TYPE (TREE_TYPE (arg0)); | |
db3927fb AH |
8433 | tem = fold_build1_loc (loc, IMAGPART_EXPR, itype, TREE_OPERAND (arg0, 0)); |
8434 | return fold_convert_loc (loc, type, negate_expr (tem)); | |
9734ebaf | 8435 | } |
85aef79f RG |
8436 | if (TREE_CODE (arg0) == CALL_EXPR) |
8437 | { | |
8438 | tree fn = get_callee_fndecl (arg0); | |
111f1fca | 8439 | if (fn && DECL_BUILT_IN_CLASS (fn) == BUILT_IN_NORMAL) |
85aef79f RG |
8440 | switch (DECL_FUNCTION_CODE (fn)) |
8441 | { | |
8442 | CASE_FLT_FN (BUILT_IN_CEXPI): | |
8443 | fn = mathfn_built_in (type, BUILT_IN_SIN); | |
2d38026b | 8444 | if (fn) |
db3927fb | 8445 | return build_call_expr_loc (loc, fn, 1, CALL_EXPR_ARG (arg0, 0)); |
2d38026b | 8446 | break; |
85aef79f | 8447 | |
2d38026b RS |
8448 | default: |
8449 | break; | |
85aef79f RG |
8450 | } |
8451 | } | |
62ab45cc | 8452 | return NULL_TREE; |
659d8efa | 8453 | |
48f30f62 RG |
8454 | case INDIRECT_REF: |
8455 | /* Fold *&X to X if X is an lvalue. */ | |
8456 | if (TREE_CODE (op0) == ADDR_EXPR) | |
8457 | { | |
8458 | tree op00 = TREE_OPERAND (op0, 0); | |
8459 | if ((TREE_CODE (op00) == VAR_DECL | |
8460 | || TREE_CODE (op00) == PARM_DECL | |
8461 | || TREE_CODE (op00) == RESULT_DECL) | |
8462 | && !TREE_READONLY (op00)) | |
8463 | return op00; | |
8464 | } | |
8465 | return NULL_TREE; | |
8466 | ||
ed74d697 JJ |
8467 | case VEC_UNPACK_LO_EXPR: |
8468 | case VEC_UNPACK_HI_EXPR: | |
8469 | case VEC_UNPACK_FLOAT_LO_EXPR: | |
8470 | case VEC_UNPACK_FLOAT_HI_EXPR: | |
8471 | { | |
8472 | unsigned int nelts = TYPE_VECTOR_SUBPARTS (type), i; | |
d2a12ae7 | 8473 | tree *elts; |
ed74d697 JJ |
8474 | enum tree_code subcode; |
8475 | ||
8476 | gcc_assert (TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0)) == nelts * 2); | |
8477 | if (TREE_CODE (arg0) != VECTOR_CST) | |
8478 | return NULL_TREE; | |
8479 | ||
8480 | elts = XALLOCAVEC (tree, nelts * 2); | |
8481 | if (!vec_cst_ctor_to_array (arg0, elts)) | |
8482 | return NULL_TREE; | |
8483 | ||
8484 | if ((!BYTES_BIG_ENDIAN) ^ (code == VEC_UNPACK_LO_EXPR | |
8485 | || code == VEC_UNPACK_FLOAT_LO_EXPR)) | |
8486 | elts += nelts; | |
8487 | ||
8488 | if (code == VEC_UNPACK_LO_EXPR || code == VEC_UNPACK_HI_EXPR) | |
8489 | subcode = NOP_EXPR; | |
8490 | else | |
8491 | subcode = FLOAT_EXPR; | |
8492 | ||
8493 | for (i = 0; i < nelts; i++) | |
8494 | { | |
8495 | elts[i] = fold_convert_const (subcode, TREE_TYPE (type), elts[i]); | |
8496 | if (elts[i] == NULL_TREE || !CONSTANT_CLASS_P (elts[i])) | |
8497 | return NULL_TREE; | |
8498 | } | |
8499 | ||
d2a12ae7 | 8500 | return build_vector (type, elts); |
ed74d697 JJ |
8501 | } |
8502 | ||
db9310ef JJ |
8503 | case REDUC_MIN_EXPR: |
8504 | case REDUC_MAX_EXPR: | |
8505 | case REDUC_PLUS_EXPR: | |
8506 | { | |
8507 | unsigned int nelts = TYPE_VECTOR_SUBPARTS (type), i; | |
8508 | tree *elts; | |
8509 | enum tree_code subcode; | |
8510 | ||
8511 | if (TREE_CODE (op0) != VECTOR_CST) | |
8512 | return NULL_TREE; | |
8513 | ||
8514 | elts = XALLOCAVEC (tree, nelts); | |
8515 | if (!vec_cst_ctor_to_array (op0, elts)) | |
8516 | return NULL_TREE; | |
8517 | ||
8518 | switch (code) | |
8519 | { | |
8520 | case REDUC_MIN_EXPR: subcode = MIN_EXPR; break; | |
8521 | case REDUC_MAX_EXPR: subcode = MAX_EXPR; break; | |
8522 | case REDUC_PLUS_EXPR: subcode = PLUS_EXPR; break; | |
8523 | default: gcc_unreachable (); | |
8524 | } | |
8525 | ||
8526 | for (i = 1; i < nelts; i++) | |
8527 | { | |
8528 | elts[0] = const_binop (subcode, elts[0], elts[i]); | |
8529 | if (elts[0] == NULL_TREE || !CONSTANT_CLASS_P (elts[0])) | |
8530 | return NULL_TREE; | |
8531 | elts[i] = build_zero_cst (TREE_TYPE (type)); | |
8532 | } | |
8533 | ||
8534 | return build_vector (type, elts); | |
8535 | } | |
8536 | ||
659d8efa | 8537 | default: |
62ab45cc | 8538 | return NULL_TREE; |
659d8efa KH |
8539 | } /* switch (code) */ |
8540 | } | |
8541 | ||
9bacafeb PB |
8542 | |
8543 | /* If the operation was a conversion do _not_ mark a resulting constant | |
8544 | with TREE_OVERFLOW if the original constant was not. These conversions | |
8545 | have implementation defined behavior and retaining the TREE_OVERFLOW | |
8546 | flag here would confuse later passes such as VRP. */ | |
8547 | tree | |
db3927fb AH |
8548 | fold_unary_ignore_overflow_loc (location_t loc, enum tree_code code, |
8549 | tree type, tree op0) | |
9bacafeb | 8550 | { |
db3927fb | 8551 | tree res = fold_unary_loc (loc, code, type, op0); |
9bacafeb PB |
8552 | if (res |
8553 | && TREE_CODE (res) == INTEGER_CST | |
8554 | && TREE_CODE (op0) == INTEGER_CST | |
8555 | && CONVERT_EXPR_CODE_P (code)) | |
8556 | TREE_OVERFLOW (res) = TREE_OVERFLOW (op0); | |
8557 | ||
8558 | return res; | |
8559 | } | |
8560 | ||
e8e8c74b KT |
8561 | /* Fold a binary bitwise/truth expression of code CODE and type TYPE with |
8562 | operands OP0 and OP1. LOC is the location of the resulting expression. | |
8563 | ARG0 and ARG1 are the NOP_STRIPed results of OP0 and OP1. | |
8564 | Return the folded expression if folding is successful. Otherwise, | |
8565 | return NULL_TREE. */ | |
8566 | static tree | |
8567 | fold_truth_andor (location_t loc, enum tree_code code, tree type, | |
8568 | tree arg0, tree arg1, tree op0, tree op1) | |
8569 | { | |
8570 | tree tem; | |
8571 | ||
8572 | /* We only do these simplifications if we are optimizing. */ | |
8573 | if (!optimize) | |
8574 | return NULL_TREE; | |
8575 | ||
8576 | /* Check for things like (A || B) && (A || C). We can convert this | |
8577 | to A || (B && C). Note that either operator can be any of the four | |
8578 | truth and/or operations and the transformation will still be | |
8579 | valid. Also note that we only care about order for the | |
8580 | ANDIF and ORIF operators. If B contains side effects, this | |
8581 | might change the truth-value of A. */ | |
8582 | if (TREE_CODE (arg0) == TREE_CODE (arg1) | |
8583 | && (TREE_CODE (arg0) == TRUTH_ANDIF_EXPR | |
8584 | || TREE_CODE (arg0) == TRUTH_ORIF_EXPR | |
8585 | || TREE_CODE (arg0) == TRUTH_AND_EXPR | |
8586 | || TREE_CODE (arg0) == TRUTH_OR_EXPR) | |
8587 | && ! TREE_SIDE_EFFECTS (TREE_OPERAND (arg0, 1))) | |
8588 | { | |
8589 | tree a00 = TREE_OPERAND (arg0, 0); | |
8590 | tree a01 = TREE_OPERAND (arg0, 1); | |
8591 | tree a10 = TREE_OPERAND (arg1, 0); | |
8592 | tree a11 = TREE_OPERAND (arg1, 1); | |
8593 | int commutative = ((TREE_CODE (arg0) == TRUTH_OR_EXPR | |
8594 | || TREE_CODE (arg0) == TRUTH_AND_EXPR) | |
8595 | && (code == TRUTH_AND_EXPR | |
8596 | || code == TRUTH_OR_EXPR)); | |
8597 | ||
8598 | if (operand_equal_p (a00, a10, 0)) | |
8599 | return fold_build2_loc (loc, TREE_CODE (arg0), type, a00, | |
8600 | fold_build2_loc (loc, code, type, a01, a11)); | |
8601 | else if (commutative && operand_equal_p (a00, a11, 0)) | |
8602 | return fold_build2_loc (loc, TREE_CODE (arg0), type, a00, | |
8603 | fold_build2_loc (loc, code, type, a01, a10)); | |
8604 | else if (commutative && operand_equal_p (a01, a10, 0)) | |
8605 | return fold_build2_loc (loc, TREE_CODE (arg0), type, a01, | |
8606 | fold_build2_loc (loc, code, type, a00, a11)); | |
8607 | ||
8608 | /* This case if tricky because we must either have commutative | |
8609 | operators or else A10 must not have side-effects. */ | |
8610 | ||
8611 | else if ((commutative || ! TREE_SIDE_EFFECTS (a10)) | |
8612 | && operand_equal_p (a01, a11, 0)) | |
8613 | return fold_build2_loc (loc, TREE_CODE (arg0), type, | |
8614 | fold_build2_loc (loc, code, type, a00, a10), | |
8615 | a01); | |
8616 | } | |
8617 | ||
8618 | /* See if we can build a range comparison. */ | |
8619 | if (0 != (tem = fold_range_test (loc, code, type, op0, op1))) | |
8620 | return tem; | |
8621 | ||
8622 | if ((code == TRUTH_ANDIF_EXPR && TREE_CODE (arg0) == TRUTH_ORIF_EXPR) | |
8623 | || (code == TRUTH_ORIF_EXPR && TREE_CODE (arg0) == TRUTH_ANDIF_EXPR)) | |
8624 | { | |
8625 | tem = merge_truthop_with_opposite_arm (loc, arg0, arg1, true); | |
8626 | if (tem) | |
8627 | return fold_build2_loc (loc, code, type, tem, arg1); | |
8628 | } | |
8629 | ||
8630 | if ((code == TRUTH_ANDIF_EXPR && TREE_CODE (arg1) == TRUTH_ORIF_EXPR) | |
8631 | || (code == TRUTH_ORIF_EXPR && TREE_CODE (arg1) == TRUTH_ANDIF_EXPR)) | |
8632 | { | |
8633 | tem = merge_truthop_with_opposite_arm (loc, arg1, arg0, false); | |
8634 | if (tem) | |
8635 | return fold_build2_loc (loc, code, type, arg0, tem); | |
8636 | } | |
8637 | ||
8638 | /* Check for the possibility of merging component references. If our | |
8639 | lhs is another similar operation, try to merge its rhs with our | |
8640 | rhs. Then try to merge our lhs and rhs. */ | |
8641 | if (TREE_CODE (arg0) == code | |
6e796a83 KT |
8642 | && 0 != (tem = fold_truth_andor_1 (loc, code, type, |
8643 | TREE_OPERAND (arg0, 1), arg1))) | |
e8e8c74b KT |
8644 | return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0), tem); |
8645 | ||
6e796a83 | 8646 | if ((tem = fold_truth_andor_1 (loc, code, type, arg0, arg1)) != 0) |
e8e8c74b KT |
8647 | return tem; |
8648 | ||
afb0c9cd | 8649 | if (LOGICAL_OP_NON_SHORT_CIRCUIT |
6e796a83 KT |
8650 | && (code == TRUTH_AND_EXPR |
8651 | || code == TRUTH_ANDIF_EXPR | |
8652 | || code == TRUTH_OR_EXPR | |
8653 | || code == TRUTH_ORIF_EXPR)) | |
8654 | { | |
8655 | enum tree_code ncode, icode; | |
8656 | ||
8657 | ncode = (code == TRUTH_ANDIF_EXPR || code == TRUTH_AND_EXPR) | |
8658 | ? TRUTH_AND_EXPR : TRUTH_OR_EXPR; | |
8659 | icode = ncode == TRUTH_AND_EXPR ? TRUTH_ANDIF_EXPR : TRUTH_ORIF_EXPR; | |
8660 | ||
8661 | /* Transform ((A AND-IF B) AND[-IF] C) into (A AND-IF (B AND C)), | |
8662 | or ((A OR-IF B) OR[-IF] C) into (A OR-IF (B OR C)) | |
8663 | We don't want to pack more than two leafs to a non-IF AND/OR | |
8664 | expression. | |
8665 | If tree-code of left-hand operand isn't an AND/OR-IF code and not | |
8666 | equal to IF-CODE, then we don't want to add right-hand operand. | |
8667 | If the inner right-hand side of left-hand operand has | |
8668 | side-effects, or isn't simple, then we can't add to it, | |
8669 | as otherwise we might destroy if-sequence. */ | |
8670 | if (TREE_CODE (arg0) == icode | |
8671 | && simple_operand_p_2 (arg1) | |
8672 | /* Needed for sequence points to handle trappings, and | |
8673 | side-effects. */ | |
8674 | && simple_operand_p_2 (TREE_OPERAND (arg0, 1))) | |
8675 | { | |
8676 | tem = fold_build2_loc (loc, ncode, type, TREE_OPERAND (arg0, 1), | |
8677 | arg1); | |
8678 | return fold_build2_loc (loc, icode, type, TREE_OPERAND (arg0, 0), | |
8679 | tem); | |
8680 | } | |
8681 | /* Same as abouve but for (A AND[-IF] (B AND-IF C)) -> ((A AND B) AND-IF C), | |
8682 | or (A OR[-IF] (B OR-IF C) -> ((A OR B) OR-IF C). */ | |
8683 | else if (TREE_CODE (arg1) == icode | |
8684 | && simple_operand_p_2 (arg0) | |
8685 | /* Needed for sequence points to handle trappings, and | |
8686 | side-effects. */ | |
8687 | && simple_operand_p_2 (TREE_OPERAND (arg1, 0))) | |
8688 | { | |
8689 | tem = fold_build2_loc (loc, ncode, type, | |
8690 | arg0, TREE_OPERAND (arg1, 0)); | |
8691 | return fold_build2_loc (loc, icode, type, tem, | |
8692 | TREE_OPERAND (arg1, 1)); | |
8693 | } | |
8694 | /* Transform (A AND-IF B) into (A AND B), or (A OR-IF B) | |
8695 | into (A OR B). | |
8696 | For sequence point consistancy, we need to check for trapping, | |
8697 | and side-effects. */ | |
8698 | else if (code == icode && simple_operand_p_2 (arg0) | |
8699 | && simple_operand_p_2 (arg1)) | |
8700 | return fold_build2_loc (loc, ncode, type, arg0, arg1); | |
8701 | } | |
8702 | ||
e8e8c74b KT |
8703 | return NULL_TREE; |
8704 | } | |
8705 | ||
292f30c5 EB |
8706 | /* Fold a binary expression of code CODE and type TYPE with operands |
8707 | OP0 and OP1, containing either a MIN-MAX or a MAX-MIN combination. | |
8708 | Return the folded expression if folding is successful. Otherwise, | |
8709 | return NULL_TREE. */ | |
8710 | ||
8711 | static tree | |
db3927fb | 8712 | fold_minmax (location_t loc, enum tree_code code, tree type, tree op0, tree op1) |
292f30c5 EB |
8713 | { |
8714 | enum tree_code compl_code; | |
8715 | ||
8716 | if (code == MIN_EXPR) | |
8717 | compl_code = MAX_EXPR; | |
8718 | else if (code == MAX_EXPR) | |
8719 | compl_code = MIN_EXPR; | |
8720 | else | |
5f180d36 | 8721 | gcc_unreachable (); |
292f30c5 | 8722 | |
f0dbdfbb | 8723 | /* MIN (MAX (a, b), b) == b. */ |
292f30c5 EB |
8724 | if (TREE_CODE (op0) == compl_code |
8725 | && operand_equal_p (TREE_OPERAND (op0, 1), op1, 0)) | |
db3927fb | 8726 | return omit_one_operand_loc (loc, type, op1, TREE_OPERAND (op0, 0)); |
292f30c5 | 8727 | |
f0dbdfbb | 8728 | /* MIN (MAX (b, a), b) == b. */ |
292f30c5 EB |
8729 | if (TREE_CODE (op0) == compl_code |
8730 | && operand_equal_p (TREE_OPERAND (op0, 0), op1, 0) | |
8731 | && reorder_operands_p (TREE_OPERAND (op0, 1), op1)) | |
db3927fb | 8732 | return omit_one_operand_loc (loc, type, op1, TREE_OPERAND (op0, 1)); |
292f30c5 | 8733 | |
f0dbdfbb | 8734 | /* MIN (a, MAX (a, b)) == a. */ |
292f30c5 EB |
8735 | if (TREE_CODE (op1) == compl_code |
8736 | && operand_equal_p (op0, TREE_OPERAND (op1, 0), 0) | |
8737 | && reorder_operands_p (op0, TREE_OPERAND (op1, 1))) | |
db3927fb | 8738 | return omit_one_operand_loc (loc, type, op0, TREE_OPERAND (op1, 1)); |
292f30c5 | 8739 | |
f0dbdfbb | 8740 | /* MIN (a, MAX (b, a)) == a. */ |
292f30c5 EB |
8741 | if (TREE_CODE (op1) == compl_code |
8742 | && operand_equal_p (op0, TREE_OPERAND (op1, 1), 0) | |
8743 | && reorder_operands_p (op0, TREE_OPERAND (op1, 0))) | |
db3927fb | 8744 | return omit_one_operand_loc (loc, type, op0, TREE_OPERAND (op1, 0)); |
292f30c5 EB |
8745 | |
8746 | return NULL_TREE; | |
8747 | } | |
8748 | ||
e73dbcae RG |
8749 | /* Helper that tries to canonicalize the comparison ARG0 CODE ARG1 |
8750 | by changing CODE to reduce the magnitude of constants involved in | |
8751 | ARG0 of the comparison. | |
8752 | Returns a canonicalized comparison tree if a simplification was | |
6ac01510 ILT |
8753 | possible, otherwise returns NULL_TREE. |
8754 | Set *STRICT_OVERFLOW_P to true if the canonicalization is only | |
8755 | valid if signed overflow is undefined. */ | |
e73dbcae RG |
8756 | |
8757 | static tree | |
db3927fb | 8758 | maybe_canonicalize_comparison_1 (location_t loc, enum tree_code code, tree type, |
6ac01510 ILT |
8759 | tree arg0, tree arg1, |
8760 | bool *strict_overflow_p) | |
e73dbcae RG |
8761 | { |
8762 | enum tree_code code0 = TREE_CODE (arg0); | |
8763 | tree t, cst0 = NULL_TREE; | |
8764 | int sgn0; | |
8765 | bool swap = false; | |
8766 | ||
0b45fd7a RG |
8767 | /* Match A +- CST code arg1 and CST code arg1. We can change the |
8768 | first form only if overflow is undefined. */ | |
8769 | if (!((TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0)) | |
8770 | /* In principle pointers also have undefined overflow behavior, | |
8771 | but that causes problems elsewhere. */ | |
8772 | && !POINTER_TYPE_P (TREE_TYPE (arg0)) | |
8773 | && (code0 == MINUS_EXPR | |
8774 | || code0 == PLUS_EXPR) | |
e73dbcae RG |
8775 | && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST) |
8776 | || code0 == INTEGER_CST)) | |
8777 | return NULL_TREE; | |
8778 | ||
8779 | /* Identify the constant in arg0 and its sign. */ | |
8780 | if (code0 == INTEGER_CST) | |
8781 | cst0 = arg0; | |
8782 | else | |
8783 | cst0 = TREE_OPERAND (arg0, 1); | |
8784 | sgn0 = tree_int_cst_sgn (cst0); | |
8785 | ||
8786 | /* Overflowed constants and zero will cause problems. */ | |
8787 | if (integer_zerop (cst0) | |
8788 | || TREE_OVERFLOW (cst0)) | |
8789 | return NULL_TREE; | |
8790 | ||
2f8e468b | 8791 | /* See if we can reduce the magnitude of the constant in |
e73dbcae RG |
8792 | arg0 by changing the comparison code. */ |
8793 | if (code0 == INTEGER_CST) | |
8794 | { | |
8795 | /* CST <= arg1 -> CST-1 < arg1. */ | |
8796 | if (code == LE_EXPR && sgn0 == 1) | |
8797 | code = LT_EXPR; | |
8798 | /* -CST < arg1 -> -CST-1 <= arg1. */ | |
8799 | else if (code == LT_EXPR && sgn0 == -1) | |
8800 | code = LE_EXPR; | |
8801 | /* CST > arg1 -> CST-1 >= arg1. */ | |
8802 | else if (code == GT_EXPR && sgn0 == 1) | |
8803 | code = GE_EXPR; | |
8804 | /* -CST >= arg1 -> -CST-1 > arg1. */ | |
8805 | else if (code == GE_EXPR && sgn0 == -1) | |
8806 | code = GT_EXPR; | |
8807 | else | |
8808 | return NULL_TREE; | |
8809 | /* arg1 code' CST' might be more canonical. */ | |
8810 | swap = true; | |
8811 | } | |
8812 | else | |
8813 | { | |
8814 | /* A - CST < arg1 -> A - CST-1 <= arg1. */ | |
8815 | if (code == LT_EXPR | |
8816 | && code0 == ((sgn0 == -1) ? PLUS_EXPR : MINUS_EXPR)) | |
8817 | code = LE_EXPR; | |
8818 | /* A + CST > arg1 -> A + CST-1 >= arg1. */ | |
8819 | else if (code == GT_EXPR | |
8820 | && code0 == ((sgn0 == -1) ? MINUS_EXPR : PLUS_EXPR)) | |
8821 | code = GE_EXPR; | |
8822 | /* A + CST <= arg1 -> A + CST-1 < arg1. */ | |
8823 | else if (code == LE_EXPR | |
8824 | && code0 == ((sgn0 == -1) ? MINUS_EXPR : PLUS_EXPR)) | |
8825 | code = LT_EXPR; | |
8826 | /* A - CST >= arg1 -> A - CST-1 > arg1. */ | |
8827 | else if (code == GE_EXPR | |
8828 | && code0 == ((sgn0 == -1) ? PLUS_EXPR : MINUS_EXPR)) | |
8829 | code = GT_EXPR; | |
8830 | else | |
8831 | return NULL_TREE; | |
6ac01510 | 8832 | *strict_overflow_p = true; |
e73dbcae RG |
8833 | } |
8834 | ||
0b45fd7a RG |
8835 | /* Now build the constant reduced in magnitude. But not if that |
8836 | would produce one outside of its types range. */ | |
8837 | if (INTEGRAL_TYPE_P (TREE_TYPE (cst0)) | |
8838 | && ((sgn0 == 1 | |
8839 | && TYPE_MIN_VALUE (TREE_TYPE (cst0)) | |
8840 | && tree_int_cst_equal (cst0, TYPE_MIN_VALUE (TREE_TYPE (cst0)))) | |
8841 | || (sgn0 == -1 | |
8842 | && TYPE_MAX_VALUE (TREE_TYPE (cst0)) | |
8843 | && tree_int_cst_equal (cst0, TYPE_MAX_VALUE (TREE_TYPE (cst0)))))) | |
8844 | /* We cannot swap the comparison here as that would cause us to | |
8845 | endlessly recurse. */ | |
8846 | return NULL_TREE; | |
8847 | ||
e73dbcae | 8848 | t = int_const_binop (sgn0 == -1 ? PLUS_EXPR : MINUS_EXPR, |
d35936ab | 8849 | cst0, build_int_cst (TREE_TYPE (cst0), 1)); |
e73dbcae | 8850 | if (code0 != INTEGER_CST) |
db3927fb | 8851 | t = fold_build2_loc (loc, code0, TREE_TYPE (arg0), TREE_OPERAND (arg0, 0), t); |
d510820a | 8852 | t = fold_convert (TREE_TYPE (arg1), t); |
e73dbcae RG |
8853 | |
8854 | /* If swapping might yield to a more canonical form, do so. */ | |
8855 | if (swap) | |
db3927fb | 8856 | return fold_build2_loc (loc, swap_tree_comparison (code), type, arg1, t); |
e73dbcae | 8857 | else |
db3927fb | 8858 | return fold_build2_loc (loc, code, type, t, arg1); |
e73dbcae RG |
8859 | } |
8860 | ||
8861 | /* Canonicalize the comparison ARG0 CODE ARG1 with type TYPE with undefined | |
8862 | overflow further. Try to decrease the magnitude of constants involved | |
8863 | by changing LE_EXPR and GE_EXPR to LT_EXPR and GT_EXPR or vice versa | |
8864 | and put sole constants at the second argument position. | |
8865 | Returns the canonicalized tree if changed, otherwise NULL_TREE. */ | |
8866 | ||
8867 | static tree | |
db3927fb | 8868 | maybe_canonicalize_comparison (location_t loc, enum tree_code code, tree type, |
e73dbcae RG |
8869 | tree arg0, tree arg1) |
8870 | { | |
8871 | tree t; | |
6ac01510 ILT |
8872 | bool strict_overflow_p; |
8873 | const char * const warnmsg = G_("assuming signed overflow does not occur " | |
8874 | "when reducing constant in comparison"); | |
e73dbcae | 8875 | |
e73dbcae | 8876 | /* Try canonicalization by simplifying arg0. */ |
6ac01510 | 8877 | strict_overflow_p = false; |
db3927fb | 8878 | t = maybe_canonicalize_comparison_1 (loc, code, type, arg0, arg1, |
6ac01510 | 8879 | &strict_overflow_p); |
e73dbcae | 8880 | if (t) |
6ac01510 ILT |
8881 | { |
8882 | if (strict_overflow_p) | |
8883 | fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_MAGNITUDE); | |
8884 | return t; | |
8885 | } | |
e73dbcae RG |
8886 | |
8887 | /* Try canonicalization by simplifying arg1 using the swapped | |
2f8e468b | 8888 | comparison. */ |
e73dbcae | 8889 | code = swap_tree_comparison (code); |
6ac01510 | 8890 | strict_overflow_p = false; |
db3927fb | 8891 | t = maybe_canonicalize_comparison_1 (loc, code, type, arg1, arg0, |
6ac01510 ILT |
8892 | &strict_overflow_p); |
8893 | if (t && strict_overflow_p) | |
8894 | fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_MAGNITUDE); | |
8895 | return t; | |
e73dbcae RG |
8896 | } |
8897 | ||
6e3c5c30 ILT |
8898 | /* Return whether BASE + OFFSET + BITPOS may wrap around the address |
8899 | space. This is used to avoid issuing overflow warnings for | |
8900 | expressions like &p->x which can not wrap. */ | |
8901 | ||
8902 | static bool | |
8903 | pointer_may_wrap_p (tree base, tree offset, HOST_WIDE_INT bitpos) | |
8904 | { | |
9be0ac8c | 8905 | double_int di_offset, total; |
6e3c5c30 ILT |
8906 | |
8907 | if (!POINTER_TYPE_P (TREE_TYPE (base))) | |
8908 | return true; | |
8909 | ||
8910 | if (bitpos < 0) | |
8911 | return true; | |
8912 | ||
6e3c5c30 | 8913 | if (offset == NULL_TREE) |
9be0ac8c | 8914 | di_offset = double_int_zero; |
6e3c5c30 ILT |
8915 | else if (TREE_CODE (offset) != INTEGER_CST || TREE_OVERFLOW (offset)) |
8916 | return true; | |
8917 | else | |
9be0ac8c | 8918 | di_offset = TREE_INT_CST (offset); |
6e3c5c30 | 8919 | |
9be0ac8c LC |
8920 | bool overflow; |
8921 | double_int units = double_int::from_uhwi (bitpos / BITS_PER_UNIT); | |
8922 | total = di_offset.add_with_sign (units, true, &overflow); | |
8923 | if (overflow) | |
6e3c5c30 ILT |
8924 | return true; |
8925 | ||
9be0ac8c | 8926 | if (total.high != 0) |
6e3c5c30 | 8927 | return true; |
b2f06c39 | 8928 | |
9be0ac8c | 8929 | HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (TREE_TYPE (base))); |
b2f06c39 ILT |
8930 | if (size <= 0) |
8931 | return true; | |
8932 | ||
8933 | /* We can do slightly better for SIZE if we have an ADDR_EXPR of an | |
8934 | array. */ | |
8935 | if (TREE_CODE (base) == ADDR_EXPR) | |
8936 | { | |
8937 | HOST_WIDE_INT base_size; | |
8938 | ||
8939 | base_size = int_size_in_bytes (TREE_TYPE (TREE_OPERAND (base, 0))); | |
8940 | if (base_size > 0 && size < base_size) | |
8941 | size = base_size; | |
8942 | } | |
8943 | ||
9be0ac8c | 8944 | return total.low > (unsigned HOST_WIDE_INT) size; |
6e3c5c30 ILT |
8945 | } |
8946 | ||
e26ec0bb RS |
8947 | /* Subroutine of fold_binary. This routine performs all of the |
8948 | transformations that are common to the equality/inequality | |
8949 | operators (EQ_EXPR and NE_EXPR) and the ordering operators | |
8950 | (LT_EXPR, LE_EXPR, GE_EXPR and GT_EXPR). Callers other than | |
8951 | fold_binary should call fold_binary. Fold a comparison with | |
8952 | tree code CODE and type TYPE with operands OP0 and OP1. Return | |
8953 | the folded comparison or NULL_TREE. */ | |
8954 | ||
8955 | static tree | |
db3927fb AH |
8956 | fold_comparison (location_t loc, enum tree_code code, tree type, |
8957 | tree op0, tree op1) | |
e26ec0bb RS |
8958 | { |
8959 | tree arg0, arg1, tem; | |
8960 | ||
8961 | arg0 = op0; | |
8962 | arg1 = op1; | |
8963 | ||
8964 | STRIP_SIGN_NOPS (arg0); | |
8965 | STRIP_SIGN_NOPS (arg1); | |
8966 | ||
8967 | tem = fold_relational_const (code, type, arg0, arg1); | |
8968 | if (tem != NULL_TREE) | |
8969 | return tem; | |
8970 | ||
8971 | /* If one arg is a real or integer constant, put it last. */ | |
8972 | if (tree_swap_operands_p (arg0, arg1, true)) | |
db3927fb | 8973 | return fold_build2_loc (loc, swap_tree_comparison (code), type, op1, op0); |
e26ec0bb | 8974 | |
e26ec0bb RS |
8975 | /* Transform comparisons of the form X +- C1 CMP C2 to X CMP C2 +- C1. */ |
8976 | if ((TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR) | |
8977 | && (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST | |
8978 | && !TREE_OVERFLOW (TREE_OPERAND (arg0, 1)) | |
eeef0e45 | 8979 | && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1))) |
e26ec0bb RS |
8980 | && (TREE_CODE (arg1) == INTEGER_CST |
8981 | && !TREE_OVERFLOW (arg1))) | |
8982 | { | |
8983 | tree const1 = TREE_OPERAND (arg0, 1); | |
8984 | tree const2 = arg1; | |
8985 | tree variable = TREE_OPERAND (arg0, 0); | |
8986 | tree lhs; | |
8987 | int lhs_add; | |
8988 | lhs_add = TREE_CODE (arg0) != PLUS_EXPR; | |
8989 | ||
db3927fb | 8990 | lhs = fold_build2_loc (loc, lhs_add ? PLUS_EXPR : MINUS_EXPR, |
e26ec0bb | 8991 | TREE_TYPE (arg1), const2, const1); |
b44e7f07 ZD |
8992 | |
8993 | /* If the constant operation overflowed this can be | |
8994 | simplified as a comparison against INT_MAX/INT_MIN. */ | |
8995 | if (TREE_CODE (lhs) == INTEGER_CST | |
8996 | && TREE_OVERFLOW (lhs)) | |
8997 | { | |
8998 | int const1_sgn = tree_int_cst_sgn (const1); | |
8999 | enum tree_code code2 = code; | |
9000 | ||
9001 | /* Get the sign of the constant on the lhs if the | |
9002 | operation were VARIABLE + CONST1. */ | |
9003 | if (TREE_CODE (arg0) == MINUS_EXPR) | |
9004 | const1_sgn = -const1_sgn; | |
9005 | ||
9006 | /* The sign of the constant determines if we overflowed | |
9007 | INT_MAX (const1_sgn == -1) or INT_MIN (const1_sgn == 1). | |
9008 | Canonicalize to the INT_MIN overflow by swapping the comparison | |
9009 | if necessary. */ | |
9010 | if (const1_sgn == -1) | |
9011 | code2 = swap_tree_comparison (code); | |
9012 | ||
9013 | /* We now can look at the canonicalized case | |
9014 | VARIABLE + 1 CODE2 INT_MIN | |
9015 | and decide on the result. */ | |
9016 | if (code2 == LT_EXPR | |
9017 | || code2 == LE_EXPR | |
9018 | || code2 == EQ_EXPR) | |
db3927fb | 9019 | return omit_one_operand_loc (loc, type, boolean_false_node, variable); |
b44e7f07 ZD |
9020 | else if (code2 == NE_EXPR |
9021 | || code2 == GE_EXPR | |
9022 | || code2 == GT_EXPR) | |
db3927fb | 9023 | return omit_one_operand_loc (loc, type, boolean_true_node, variable); |
b44e7f07 ZD |
9024 | } |
9025 | ||
e26ec0bb RS |
9026 | if (TREE_CODE (lhs) == TREE_CODE (arg1) |
9027 | && (TREE_CODE (lhs) != INTEGER_CST | |
9028 | || !TREE_OVERFLOW (lhs))) | |
6ac01510 | 9029 | { |
32bd2409 JJ |
9030 | if (code != EQ_EXPR && code != NE_EXPR) |
9031 | fold_overflow_warning ("assuming signed overflow does not occur " | |
9032 | "when changing X +- C1 cmp C2 to " | |
9033 | "X cmp C1 +- C2", | |
9034 | WARN_STRICT_OVERFLOW_COMPARISON); | |
db3927fb | 9035 | return fold_build2_loc (loc, code, type, variable, lhs); |
6ac01510 | 9036 | } |
e26ec0bb RS |
9037 | } |
9038 | ||
e015f578 RG |
9039 | /* For comparisons of pointers we can decompose it to a compile time |
9040 | comparison of the base objects and the offsets into the object. | |
3e0de255 RG |
9041 | This requires at least one operand being an ADDR_EXPR or a |
9042 | POINTER_PLUS_EXPR to do more than the operand_equal_p test below. */ | |
e015f578 RG |
9043 | if (POINTER_TYPE_P (TREE_TYPE (arg0)) |
9044 | && (TREE_CODE (arg0) == ADDR_EXPR | |
3e0de255 RG |
9045 | || TREE_CODE (arg1) == ADDR_EXPR |
9046 | || TREE_CODE (arg0) == POINTER_PLUS_EXPR | |
9047 | || TREE_CODE (arg1) == POINTER_PLUS_EXPR)) | |
e015f578 RG |
9048 | { |
9049 | tree base0, base1, offset0 = NULL_TREE, offset1 = NULL_TREE; | |
9050 | HOST_WIDE_INT bitsize, bitpos0 = 0, bitpos1 = 0; | |
9051 | enum machine_mode mode; | |
9052 | int volatilep, unsignedp; | |
bd03c084 | 9053 | bool indirect_base0 = false, indirect_base1 = false; |
e015f578 RG |
9054 | |
9055 | /* Get base and offset for the access. Strip ADDR_EXPR for | |
9056 | get_inner_reference, but put it back by stripping INDIRECT_REF | |
bd03c084 RG |
9057 | off the base object if possible. indirect_baseN will be true |
9058 | if baseN is not an address but refers to the object itself. */ | |
e015f578 RG |
9059 | base0 = arg0; |
9060 | if (TREE_CODE (arg0) == ADDR_EXPR) | |
9061 | { | |
9062 | base0 = get_inner_reference (TREE_OPERAND (arg0, 0), | |
9063 | &bitsize, &bitpos0, &offset0, &mode, | |
9064 | &unsignedp, &volatilep, false); | |
9065 | if (TREE_CODE (base0) == INDIRECT_REF) | |
9066 | base0 = TREE_OPERAND (base0, 0); | |
9067 | else | |
9068 | indirect_base0 = true; | |
9069 | } | |
3e0de255 RG |
9070 | else if (TREE_CODE (arg0) == POINTER_PLUS_EXPR) |
9071 | { | |
9072 | base0 = TREE_OPERAND (arg0, 0); | |
743ad76e | 9073 | STRIP_SIGN_NOPS (base0); |
70f34814 RG |
9074 | if (TREE_CODE (base0) == ADDR_EXPR) |
9075 | { | |
9076 | base0 = TREE_OPERAND (base0, 0); | |
9077 | indirect_base0 = true; | |
9078 | } | |
3e0de255 | 9079 | offset0 = TREE_OPERAND (arg0, 1); |
f5a3d840 | 9080 | if (host_integerp (offset0, 0)) |
8b3c2951 | 9081 | { |
f5a3d840 RG |
9082 | HOST_WIDE_INT off = size_low_cst (offset0); |
9083 | if ((HOST_WIDE_INT) (((unsigned HOST_WIDE_INT) off) | |
9084 | * BITS_PER_UNIT) | |
9085 | / BITS_PER_UNIT == (HOST_WIDE_INT) off) | |
9086 | { | |
9087 | bitpos0 = off * BITS_PER_UNIT; | |
9088 | offset0 = NULL_TREE; | |
9089 | } | |
8b3c2951 | 9090 | } |
3e0de255 | 9091 | } |
e015f578 RG |
9092 | |
9093 | base1 = arg1; | |
9094 | if (TREE_CODE (arg1) == ADDR_EXPR) | |
9095 | { | |
9096 | base1 = get_inner_reference (TREE_OPERAND (arg1, 0), | |
9097 | &bitsize, &bitpos1, &offset1, &mode, | |
9098 | &unsignedp, &volatilep, false); | |
bd03c084 | 9099 | if (TREE_CODE (base1) == INDIRECT_REF) |
e015f578 | 9100 | base1 = TREE_OPERAND (base1, 0); |
bd03c084 RG |
9101 | else |
9102 | indirect_base1 = true; | |
e015f578 | 9103 | } |
3e0de255 RG |
9104 | else if (TREE_CODE (arg1) == POINTER_PLUS_EXPR) |
9105 | { | |
9106 | base1 = TREE_OPERAND (arg1, 0); | |
743ad76e | 9107 | STRIP_SIGN_NOPS (base1); |
70f34814 RG |
9108 | if (TREE_CODE (base1) == ADDR_EXPR) |
9109 | { | |
9110 | base1 = TREE_OPERAND (base1, 0); | |
9111 | indirect_base1 = true; | |
9112 | } | |
3e0de255 | 9113 | offset1 = TREE_OPERAND (arg1, 1); |
f5a3d840 | 9114 | if (host_integerp (offset1, 0)) |
8b3c2951 | 9115 | { |
f5a3d840 RG |
9116 | HOST_WIDE_INT off = size_low_cst (offset1); |
9117 | if ((HOST_WIDE_INT) (((unsigned HOST_WIDE_INT) off) | |
9118 | * BITS_PER_UNIT) | |
9119 | / BITS_PER_UNIT == (HOST_WIDE_INT) off) | |
9120 | { | |
9121 | bitpos1 = off * BITS_PER_UNIT; | |
9122 | offset1 = NULL_TREE; | |
9123 | } | |
8b3c2951 | 9124 | } |
3e0de255 | 9125 | } |
e015f578 | 9126 | |
94e85e0a XDL |
9127 | /* A local variable can never be pointed to by |
9128 | the default SSA name of an incoming parameter. */ | |
9129 | if ((TREE_CODE (arg0) == ADDR_EXPR | |
9130 | && indirect_base0 | |
9131 | && TREE_CODE (base0) == VAR_DECL | |
9132 | && auto_var_in_fn_p (base0, current_function_decl) | |
9133 | && !indirect_base1 | |
9134 | && TREE_CODE (base1) == SSA_NAME | |
67386041 RG |
9135 | && SSA_NAME_IS_DEFAULT_DEF (base1) |
9136 | && TREE_CODE (SSA_NAME_VAR (base1)) == PARM_DECL) | |
94e85e0a XDL |
9137 | || (TREE_CODE (arg1) == ADDR_EXPR |
9138 | && indirect_base1 | |
9139 | && TREE_CODE (base1) == VAR_DECL | |
9140 | && auto_var_in_fn_p (base1, current_function_decl) | |
9141 | && !indirect_base0 | |
9142 | && TREE_CODE (base0) == SSA_NAME | |
67386041 RG |
9143 | && SSA_NAME_IS_DEFAULT_DEF (base0) |
9144 | && TREE_CODE (SSA_NAME_VAR (base0)) == PARM_DECL)) | |
94e85e0a XDL |
9145 | { |
9146 | if (code == NE_EXPR) | |
9147 | return constant_boolean_node (1, type); | |
9148 | else if (code == EQ_EXPR) | |
9149 | return constant_boolean_node (0, type); | |
9150 | } | |
e015f578 | 9151 | /* If we have equivalent bases we might be able to simplify. */ |
94e85e0a XDL |
9152 | else if (indirect_base0 == indirect_base1 |
9153 | && operand_equal_p (base0, base1, 0)) | |
e015f578 RG |
9154 | { |
9155 | /* We can fold this expression to a constant if the non-constant | |
9156 | offset parts are equal. */ | |
6e3c5c30 ILT |
9157 | if ((offset0 == offset1 |
9158 | || (offset0 && offset1 | |
9159 | && operand_equal_p (offset0, offset1, 0))) | |
9160 | && (code == EQ_EXPR | |
9161 | || code == NE_EXPR | |
e66132e1 | 9162 | || (indirect_base0 && DECL_P (base0)) |
6e3c5c30 | 9163 | || POINTER_TYPE_OVERFLOW_UNDEFINED)) |
b8698a0f | 9164 | |
e015f578 | 9165 | { |
6e3c5c30 ILT |
9166 | if (code != EQ_EXPR |
9167 | && code != NE_EXPR | |
9168 | && bitpos0 != bitpos1 | |
9169 | && (pointer_may_wrap_p (base0, offset0, bitpos0) | |
9170 | || pointer_may_wrap_p (base1, offset1, bitpos1))) | |
9171 | fold_overflow_warning (("assuming pointer wraparound does not " | |
9172 | "occur when comparing P +- C1 with " | |
9173 | "P +- C2"), | |
9174 | WARN_STRICT_OVERFLOW_CONDITIONAL); | |
9175 | ||
e015f578 RG |
9176 | switch (code) |
9177 | { | |
9178 | case EQ_EXPR: | |
b0331ccb | 9179 | return constant_boolean_node (bitpos0 == bitpos1, type); |
e015f578 | 9180 | case NE_EXPR: |
b0331ccb | 9181 | return constant_boolean_node (bitpos0 != bitpos1, type); |
e015f578 | 9182 | case LT_EXPR: |
b0331ccb | 9183 | return constant_boolean_node (bitpos0 < bitpos1, type); |
e015f578 | 9184 | case LE_EXPR: |
b0331ccb | 9185 | return constant_boolean_node (bitpos0 <= bitpos1, type); |
e015f578 | 9186 | case GE_EXPR: |
b0331ccb | 9187 | return constant_boolean_node (bitpos0 >= bitpos1, type); |
e015f578 | 9188 | case GT_EXPR: |
b0331ccb | 9189 | return constant_boolean_node (bitpos0 > bitpos1, type); |
e015f578 RG |
9190 | default:; |
9191 | } | |
9192 | } | |
9193 | /* We can simplify the comparison to a comparison of the variable | |
9194 | offset parts if the constant offset parts are equal. | |
67ae67ec | 9195 | Be careful to use signed sizetype here because otherwise we |
e015f578 RG |
9196 | mess with array offsets in the wrong way. This is possible |
9197 | because pointer arithmetic is restricted to retain within an | |
9198 | object and overflow on pointer differences is undefined as of | |
9199 | 6.5.6/8 and /9 with respect to the signed ptrdiff_t. */ | |
4c9db6e0 ILT |
9200 | else if (bitpos0 == bitpos1 |
9201 | && ((code == EQ_EXPR || code == NE_EXPR) | |
e66132e1 | 9202 | || (indirect_base0 && DECL_P (base0)) |
4c9db6e0 | 9203 | || POINTER_TYPE_OVERFLOW_UNDEFINED)) |
e015f578 | 9204 | { |
67ae67ec | 9205 | /* By converting to signed sizetype we cover middle-end pointer |
e015f578 RG |
9206 | arithmetic which operates on unsigned pointer types of size |
9207 | type size and ARRAY_REF offsets which are properly sign or | |
9208 | zero extended from their type in case it is narrower than | |
67ae67ec | 9209 | sizetype. */ |
e015f578 | 9210 | if (offset0 == NULL_TREE) |
3b9e5d95 | 9211 | offset0 = build_int_cst (ssizetype, 0); |
e015f578 | 9212 | else |
3b9e5d95 | 9213 | offset0 = fold_convert_loc (loc, ssizetype, offset0); |
e015f578 | 9214 | if (offset1 == NULL_TREE) |
3b9e5d95 | 9215 | offset1 = build_int_cst (ssizetype, 0); |
e015f578 | 9216 | else |
3b9e5d95 | 9217 | offset1 = fold_convert_loc (loc, ssizetype, offset1); |
e015f578 | 9218 | |
6e3c5c30 ILT |
9219 | if (code != EQ_EXPR |
9220 | && code != NE_EXPR | |
9221 | && (pointer_may_wrap_p (base0, offset0, bitpos0) | |
9222 | || pointer_may_wrap_p (base1, offset1, bitpos1))) | |
4c9db6e0 ILT |
9223 | fold_overflow_warning (("assuming pointer wraparound does not " |
9224 | "occur when comparing P +- C1 with " | |
9225 | "P +- C2"), | |
9226 | WARN_STRICT_OVERFLOW_COMPARISON); | |
9227 | ||
db3927fb | 9228 | return fold_build2_loc (loc, code, type, offset0, offset1); |
e015f578 RG |
9229 | } |
9230 | } | |
bd03c084 RG |
9231 | /* For non-equal bases we can simplify if they are addresses |
9232 | of local binding decls or constants. */ | |
9233 | else if (indirect_base0 && indirect_base1 | |
9234 | /* We know that !operand_equal_p (base0, base1, 0) | |
ffd837fe RG |
9235 | because the if condition was false. But make |
9236 | sure two decls are not the same. */ | |
9237 | && base0 != base1 | |
bd03c084 RG |
9238 | && TREE_CODE (arg0) == ADDR_EXPR |
9239 | && TREE_CODE (arg1) == ADDR_EXPR | |
ffd837fe RG |
9240 | && (((TREE_CODE (base0) == VAR_DECL |
9241 | || TREE_CODE (base0) == PARM_DECL) | |
bd03c084 RG |
9242 | && (targetm.binds_local_p (base0) |
9243 | || CONSTANT_CLASS_P (base1))) | |
9244 | || CONSTANT_CLASS_P (base0)) | |
ffd837fe RG |
9245 | && (((TREE_CODE (base1) == VAR_DECL |
9246 | || TREE_CODE (base1) == PARM_DECL) | |
bd03c084 RG |
9247 | && (targetm.binds_local_p (base1) |
9248 | || CONSTANT_CLASS_P (base0))) | |
9249 | || CONSTANT_CLASS_P (base1))) | |
9250 | { | |
9251 | if (code == EQ_EXPR) | |
db3927fb AH |
9252 | return omit_two_operands_loc (loc, type, boolean_false_node, |
9253 | arg0, arg1); | |
bd03c084 | 9254 | else if (code == NE_EXPR) |
db3927fb AH |
9255 | return omit_two_operands_loc (loc, type, boolean_true_node, |
9256 | arg0, arg1); | |
bd03c084 RG |
9257 | } |
9258 | /* For equal offsets we can simplify to a comparison of the | |
9259 | base addresses. */ | |
9260 | else if (bitpos0 == bitpos1 | |
9261 | && (indirect_base0 | |
9262 | ? base0 != TREE_OPERAND (arg0, 0) : base0 != arg0) | |
9263 | && (indirect_base1 | |
9264 | ? base1 != TREE_OPERAND (arg1, 0) : base1 != arg1) | |
9265 | && ((offset0 == offset1) | |
9266 | || (offset0 && offset1 | |
9267 | && operand_equal_p (offset0, offset1, 0)))) | |
9268 | { | |
9269 | if (indirect_base0) | |
db3927fb | 9270 | base0 = build_fold_addr_expr_loc (loc, base0); |
bd03c084 | 9271 | if (indirect_base1) |
db3927fb AH |
9272 | base1 = build_fold_addr_expr_loc (loc, base1); |
9273 | return fold_build2_loc (loc, code, type, base0, base1); | |
bd03c084 | 9274 | } |
e015f578 RG |
9275 | } |
9276 | ||
8a1eca08 RG |
9277 | /* Transform comparisons of the form X +- C1 CMP Y +- C2 to |
9278 | X CMP Y +- C2 +- C1 for signed X, Y. This is valid if | |
9279 | the resulting offset is smaller in absolute value than the | |
9280 | original one. */ | |
eeef0e45 | 9281 | if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0)) |
8a1eca08 RG |
9282 | && (TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR) |
9283 | && (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST | |
9284 | && !TREE_OVERFLOW (TREE_OPERAND (arg0, 1))) | |
9285 | && (TREE_CODE (arg1) == PLUS_EXPR || TREE_CODE (arg1) == MINUS_EXPR) | |
9286 | && (TREE_CODE (TREE_OPERAND (arg1, 1)) == INTEGER_CST | |
9287 | && !TREE_OVERFLOW (TREE_OPERAND (arg1, 1)))) | |
9288 | { | |
9289 | tree const1 = TREE_OPERAND (arg0, 1); | |
9290 | tree const2 = TREE_OPERAND (arg1, 1); | |
9291 | tree variable1 = TREE_OPERAND (arg0, 0); | |
9292 | tree variable2 = TREE_OPERAND (arg1, 0); | |
9293 | tree cst; | |
6ac01510 ILT |
9294 | const char * const warnmsg = G_("assuming signed overflow does not " |
9295 | "occur when combining constants around " | |
9296 | "a comparison"); | |
8a1eca08 RG |
9297 | |
9298 | /* Put the constant on the side where it doesn't overflow and is | |
9299 | of lower absolute value than before. */ | |
9300 | cst = int_const_binop (TREE_CODE (arg0) == TREE_CODE (arg1) | |
9301 | ? MINUS_EXPR : PLUS_EXPR, | |
d35936ab | 9302 | const2, const1); |
8a1eca08 RG |
9303 | if (!TREE_OVERFLOW (cst) |
9304 | && tree_int_cst_compare (const2, cst) == tree_int_cst_sgn (const2)) | |
6ac01510 ILT |
9305 | { |
9306 | fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_COMPARISON); | |
db3927fb | 9307 | return fold_build2_loc (loc, code, type, |
6ac01510 | 9308 | variable1, |
db3927fb AH |
9309 | fold_build2_loc (loc, |
9310 | TREE_CODE (arg1), TREE_TYPE (arg1), | |
6ac01510 ILT |
9311 | variable2, cst)); |
9312 | } | |
8a1eca08 RG |
9313 | |
9314 | cst = int_const_binop (TREE_CODE (arg0) == TREE_CODE (arg1) | |
9315 | ? MINUS_EXPR : PLUS_EXPR, | |
d35936ab | 9316 | const1, const2); |
8a1eca08 RG |
9317 | if (!TREE_OVERFLOW (cst) |
9318 | && tree_int_cst_compare (const1, cst) == tree_int_cst_sgn (const1)) | |
6ac01510 ILT |
9319 | { |
9320 | fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_COMPARISON); | |
db3927fb AH |
9321 | return fold_build2_loc (loc, code, type, |
9322 | fold_build2_loc (loc, TREE_CODE (arg0), TREE_TYPE (arg0), | |
6ac01510 ILT |
9323 | variable1, cst), |
9324 | variable2); | |
9325 | } | |
8a1eca08 RG |
9326 | } |
9327 | ||
6b074ef6 RK |
9328 | /* Transform comparisons of the form X * C1 CMP 0 to X CMP 0 in the |
9329 | signed arithmetic case. That form is created by the compiler | |
9330 | often enough for folding it to be of value. One example is in | |
9331 | computing loop trip counts after Operator Strength Reduction. */ | |
eeef0e45 | 9332 | if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0)) |
6b074ef6 RK |
9333 | && TREE_CODE (arg0) == MULT_EXPR |
9334 | && (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST | |
9335 | && !TREE_OVERFLOW (TREE_OPERAND (arg0, 1))) | |
9336 | && integer_zerop (arg1)) | |
9337 | { | |
9338 | tree const1 = TREE_OPERAND (arg0, 1); | |
9339 | tree const2 = arg1; /* zero */ | |
9340 | tree variable1 = TREE_OPERAND (arg0, 0); | |
9341 | enum tree_code cmp_code = code; | |
9342 | ||
eb12d0ae RG |
9343 | /* Handle unfolded multiplication by zero. */ |
9344 | if (integer_zerop (const1)) | |
9345 | return fold_build2_loc (loc, cmp_code, type, const1, const2); | |
6b074ef6 | 9346 | |
6ac01510 ILT |
9347 | fold_overflow_warning (("assuming signed overflow does not occur when " |
9348 | "eliminating multiplication in comparison " | |
9349 | "with zero"), | |
9350 | WARN_STRICT_OVERFLOW_COMPARISON); | |
9351 | ||
6b074ef6 RK |
9352 | /* If const1 is negative we swap the sense of the comparison. */ |
9353 | if (tree_int_cst_sgn (const1) < 0) | |
9354 | cmp_code = swap_tree_comparison (cmp_code); | |
9355 | ||
db3927fb | 9356 | return fold_build2_loc (loc, cmp_code, type, variable1, const2); |
6b074ef6 RK |
9357 | } |
9358 | ||
d510820a | 9359 | tem = maybe_canonicalize_comparison (loc, code, type, arg0, arg1); |
e73dbcae RG |
9360 | if (tem) |
9361 | return tem; | |
9362 | ||
e26ec0bb RS |
9363 | if (FLOAT_TYPE_P (TREE_TYPE (arg0))) |
9364 | { | |
9365 | tree targ0 = strip_float_extensions (arg0); | |
9366 | tree targ1 = strip_float_extensions (arg1); | |
9367 | tree newtype = TREE_TYPE (targ0); | |
9368 | ||
9369 | if (TYPE_PRECISION (TREE_TYPE (targ1)) > TYPE_PRECISION (newtype)) | |
9370 | newtype = TREE_TYPE (targ1); | |
9371 | ||
9372 | /* Fold (double)float1 CMP (double)float2 into float1 CMP float2. */ | |
9373 | if (TYPE_PRECISION (newtype) < TYPE_PRECISION (TREE_TYPE (arg0))) | |
db3927fb AH |
9374 | return fold_build2_loc (loc, code, type, |
9375 | fold_convert_loc (loc, newtype, targ0), | |
9376 | fold_convert_loc (loc, newtype, targ1)); | |
e26ec0bb RS |
9377 | |
9378 | /* (-a) CMP (-b) -> b CMP a */ | |
9379 | if (TREE_CODE (arg0) == NEGATE_EXPR | |
9380 | && TREE_CODE (arg1) == NEGATE_EXPR) | |
db3927fb | 9381 | return fold_build2_loc (loc, code, type, TREE_OPERAND (arg1, 0), |
e26ec0bb RS |
9382 | TREE_OPERAND (arg0, 0)); |
9383 | ||
9384 | if (TREE_CODE (arg1) == REAL_CST) | |
9385 | { | |
9386 | REAL_VALUE_TYPE cst; | |
9387 | cst = TREE_REAL_CST (arg1); | |
9388 | ||
9389 | /* (-a) CMP CST -> a swap(CMP) (-CST) */ | |
9390 | if (TREE_CODE (arg0) == NEGATE_EXPR) | |
db3927fb | 9391 | return fold_build2_loc (loc, swap_tree_comparison (code), type, |
e26ec0bb RS |
9392 | TREE_OPERAND (arg0, 0), |
9393 | build_real (TREE_TYPE (arg1), | |
d49b6e1e | 9394 | real_value_negate (&cst))); |
e26ec0bb RS |
9395 | |
9396 | /* IEEE doesn't distinguish +0 and -0 in comparisons. */ | |
9397 | /* a CMP (-0) -> a CMP 0 */ | |
9398 | if (REAL_VALUE_MINUS_ZERO (cst)) | |
db3927fb | 9399 | return fold_build2_loc (loc, code, type, arg0, |
e26ec0bb RS |
9400 | build_real (TREE_TYPE (arg1), dconst0)); |
9401 | ||
9402 | /* x != NaN is always true, other ops are always false. */ | |
9403 | if (REAL_VALUE_ISNAN (cst) | |
9404 | && ! HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg1)))) | |
9405 | { | |
9406 | tem = (code == NE_EXPR) ? integer_one_node : integer_zero_node; | |
db3927fb | 9407 | return omit_one_operand_loc (loc, type, tem, arg0); |
e26ec0bb RS |
9408 | } |
9409 | ||
9410 | /* Fold comparisons against infinity. */ | |
dc215785 UW |
9411 | if (REAL_VALUE_ISINF (cst) |
9412 | && MODE_HAS_INFINITIES (TYPE_MODE (TREE_TYPE (arg1)))) | |
e26ec0bb | 9413 | { |
db3927fb | 9414 | tem = fold_inf_compare (loc, code, type, arg0, arg1); |
e26ec0bb RS |
9415 | if (tem != NULL_TREE) |
9416 | return tem; | |
9417 | } | |
9418 | } | |
9419 | ||
9420 | /* If this is a comparison of a real constant with a PLUS_EXPR | |
9421 | or a MINUS_EXPR of a real constant, we can convert it into a | |
9422 | comparison with a revised real constant as long as no overflow | |
9423 | occurs when unsafe_math_optimizations are enabled. */ | |
9424 | if (flag_unsafe_math_optimizations | |
9425 | && TREE_CODE (arg1) == REAL_CST | |
9426 | && (TREE_CODE (arg0) == PLUS_EXPR | |
9427 | || TREE_CODE (arg0) == MINUS_EXPR) | |
9428 | && TREE_CODE (TREE_OPERAND (arg0, 1)) == REAL_CST | |
9429 | && 0 != (tem = const_binop (TREE_CODE (arg0) == PLUS_EXPR | |
9430 | ? MINUS_EXPR : PLUS_EXPR, | |
43a5d30b | 9431 | arg1, TREE_OPERAND (arg0, 1))) |
455f14dd | 9432 | && !TREE_OVERFLOW (tem)) |
db3927fb | 9433 | return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0), tem); |
e26ec0bb RS |
9434 | |
9435 | /* Likewise, we can simplify a comparison of a real constant with | |
9436 | a MINUS_EXPR whose first operand is also a real constant, i.e. | |
b8698a0f | 9437 | (c1 - x) < c2 becomes x > c1-c2. Reordering is allowed on |
a1a82611 RE |
9438 | floating-point types only if -fassociative-math is set. */ |
9439 | if (flag_associative_math | |
e26ec0bb RS |
9440 | && TREE_CODE (arg1) == REAL_CST |
9441 | && TREE_CODE (arg0) == MINUS_EXPR | |
9442 | && TREE_CODE (TREE_OPERAND (arg0, 0)) == REAL_CST | |
9443 | && 0 != (tem = const_binop (MINUS_EXPR, TREE_OPERAND (arg0, 0), | |
43a5d30b | 9444 | arg1)) |
455f14dd | 9445 | && !TREE_OVERFLOW (tem)) |
db3927fb | 9446 | return fold_build2_loc (loc, swap_tree_comparison (code), type, |
e26ec0bb RS |
9447 | TREE_OPERAND (arg0, 1), tem); |
9448 | ||
9449 | /* Fold comparisons against built-in math functions. */ | |
9450 | if (TREE_CODE (arg1) == REAL_CST | |
9451 | && flag_unsafe_math_optimizations | |
9452 | && ! flag_errno_math) | |
9453 | { | |
9454 | enum built_in_function fcode = builtin_mathfn_code (arg0); | |
9455 | ||
9456 | if (fcode != END_BUILTINS) | |
9457 | { | |
db3927fb | 9458 | tem = fold_mathfn_compare (loc, fcode, code, type, arg0, arg1); |
e26ec0bb RS |
9459 | if (tem != NULL_TREE) |
9460 | return tem; | |
9461 | } | |
9462 | } | |
9463 | } | |
9464 | ||
e26ec0bb | 9465 | if (TREE_CODE (TREE_TYPE (arg0)) == INTEGER_TYPE |
1043771b | 9466 | && CONVERT_EXPR_P (arg0)) |
e26ec0bb RS |
9467 | { |
9468 | /* If we are widening one operand of an integer comparison, | |
9469 | see if the other operand is similarly being widened. Perhaps we | |
9470 | can do the comparison in the narrower type. */ | |
db3927fb | 9471 | tem = fold_widened_comparison (loc, code, type, arg0, arg1); |
e26ec0bb RS |
9472 | if (tem) |
9473 | return tem; | |
9474 | ||
9475 | /* Or if we are changing signedness. */ | |
db3927fb | 9476 | tem = fold_sign_changed_comparison (loc, code, type, arg0, arg1); |
e26ec0bb RS |
9477 | if (tem) |
9478 | return tem; | |
9479 | } | |
9480 | ||
9481 | /* If this is comparing a constant with a MIN_EXPR or a MAX_EXPR of a | |
9482 | constant, we can simplify it. */ | |
9483 | if (TREE_CODE (arg1) == INTEGER_CST | |
9484 | && (TREE_CODE (arg0) == MIN_EXPR | |
9485 | || TREE_CODE (arg0) == MAX_EXPR) | |
9486 | && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST) | |
9487 | { | |
db3927fb | 9488 | tem = optimize_minmax_comparison (loc, code, type, op0, op1); |
e26ec0bb RS |
9489 | if (tem) |
9490 | return tem; | |
9491 | } | |
9492 | ||
9493 | /* Simplify comparison of something with itself. (For IEEE | |
9494 | floating-point, we can only do some of these simplifications.) */ | |
9495 | if (operand_equal_p (arg0, arg1, 0)) | |
9496 | { | |
9497 | switch (code) | |
9498 | { | |
9499 | case EQ_EXPR: | |
9500 | if (! FLOAT_TYPE_P (TREE_TYPE (arg0)) | |
9501 | || ! HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0)))) | |
9502 | return constant_boolean_node (1, type); | |
9503 | break; | |
9504 | ||
9505 | case GE_EXPR: | |
9506 | case LE_EXPR: | |
9507 | if (! FLOAT_TYPE_P (TREE_TYPE (arg0)) | |
9508 | || ! HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0)))) | |
9509 | return constant_boolean_node (1, type); | |
db3927fb | 9510 | return fold_build2_loc (loc, EQ_EXPR, type, arg0, arg1); |
e26ec0bb RS |
9511 | |
9512 | case NE_EXPR: | |
9513 | /* For NE, we can only do this simplification if integer | |
9514 | or we don't honor IEEE floating point NaNs. */ | |
9515 | if (FLOAT_TYPE_P (TREE_TYPE (arg0)) | |
9516 | && HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0)))) | |
9517 | break; | |
9518 | /* ... fall through ... */ | |
9519 | case GT_EXPR: | |
9520 | case LT_EXPR: | |
9521 | return constant_boolean_node (0, type); | |
9522 | default: | |
9523 | gcc_unreachable (); | |
9524 | } | |
9525 | } | |
9526 | ||
9527 | /* If we are comparing an expression that just has comparisons | |
9528 | of two integer values, arithmetic expressions of those comparisons, | |
9529 | and constants, we can simplify it. There are only three cases | |
9530 | to check: the two values can either be equal, the first can be | |
9531 | greater, or the second can be greater. Fold the expression for | |
9532 | those three values. Since each value must be 0 or 1, we have | |
9533 | eight possibilities, each of which corresponds to the constant 0 | |
9534 | or 1 or one of the six possible comparisons. | |
9535 | ||
9536 | This handles common cases like (a > b) == 0 but also handles | |
9537 | expressions like ((x > y) - (y > x)) > 0, which supposedly | |
9538 | occur in macroized code. */ | |
9539 | ||
9540 | if (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg0) != INTEGER_CST) | |
9541 | { | |
9542 | tree cval1 = 0, cval2 = 0; | |
9543 | int save_p = 0; | |
9544 | ||
9545 | if (twoval_comparison_p (arg0, &cval1, &cval2, &save_p) | |
9546 | /* Don't handle degenerate cases here; they should already | |
9547 | have been handled anyway. */ | |
9548 | && cval1 != 0 && cval2 != 0 | |
9549 | && ! (TREE_CONSTANT (cval1) && TREE_CONSTANT (cval2)) | |
9550 | && TREE_TYPE (cval1) == TREE_TYPE (cval2) | |
9551 | && INTEGRAL_TYPE_P (TREE_TYPE (cval1)) | |
9552 | && TYPE_MAX_VALUE (TREE_TYPE (cval1)) | |
9553 | && TYPE_MAX_VALUE (TREE_TYPE (cval2)) | |
9554 | && ! operand_equal_p (TYPE_MIN_VALUE (TREE_TYPE (cval1)), | |
9555 | TYPE_MAX_VALUE (TREE_TYPE (cval2)), 0)) | |
9556 | { | |
9557 | tree maxval = TYPE_MAX_VALUE (TREE_TYPE (cval1)); | |
9558 | tree minval = TYPE_MIN_VALUE (TREE_TYPE (cval1)); | |
9559 | ||
9560 | /* We can't just pass T to eval_subst in case cval1 or cval2 | |
9561 | was the same as ARG1. */ | |
9562 | ||
9563 | tree high_result | |
db3927fb AH |
9564 | = fold_build2_loc (loc, code, type, |
9565 | eval_subst (loc, arg0, cval1, maxval, | |
e26ec0bb RS |
9566 | cval2, minval), |
9567 | arg1); | |
9568 | tree equal_result | |
db3927fb AH |
9569 | = fold_build2_loc (loc, code, type, |
9570 | eval_subst (loc, arg0, cval1, maxval, | |
e26ec0bb RS |
9571 | cval2, maxval), |
9572 | arg1); | |
9573 | tree low_result | |
db3927fb AH |
9574 | = fold_build2_loc (loc, code, type, |
9575 | eval_subst (loc, arg0, cval1, minval, | |
e26ec0bb RS |
9576 | cval2, maxval), |
9577 | arg1); | |
9578 | ||
9579 | /* All three of these results should be 0 or 1. Confirm they are. | |
9580 | Then use those values to select the proper code to use. */ | |
9581 | ||
9582 | if (TREE_CODE (high_result) == INTEGER_CST | |
9583 | && TREE_CODE (equal_result) == INTEGER_CST | |
9584 | && TREE_CODE (low_result) == INTEGER_CST) | |
9585 | { | |
9586 | /* Make a 3-bit mask with the high-order bit being the | |
9587 | value for `>', the next for '=', and the low for '<'. */ | |
9588 | switch ((integer_onep (high_result) * 4) | |
9589 | + (integer_onep (equal_result) * 2) | |
9590 | + integer_onep (low_result)) | |
9591 | { | |
9592 | case 0: | |
9593 | /* Always false. */ | |
db3927fb | 9594 | return omit_one_operand_loc (loc, type, integer_zero_node, arg0); |
e26ec0bb RS |
9595 | case 1: |
9596 | code = LT_EXPR; | |
9597 | break; | |
9598 | case 2: | |
9599 | code = EQ_EXPR; | |
9600 | break; | |
9601 | case 3: | |
9602 | code = LE_EXPR; | |
9603 | break; | |
9604 | case 4: | |
9605 | code = GT_EXPR; | |
9606 | break; | |
9607 | case 5: | |
9608 | code = NE_EXPR; | |
9609 | break; | |
9610 | case 6: | |
9611 | code = GE_EXPR; | |
9612 | break; | |
9613 | case 7: | |
9614 | /* Always true. */ | |
db3927fb | 9615 | return omit_one_operand_loc (loc, type, integer_one_node, arg0); |
e26ec0bb RS |
9616 | } |
9617 | ||
9618 | if (save_p) | |
db3927fb AH |
9619 | { |
9620 | tem = save_expr (build2 (code, type, cval1, cval2)); | |
9621 | SET_EXPR_LOCATION (tem, loc); | |
9622 | return tem; | |
9623 | } | |
9624 | return fold_build2_loc (loc, code, type, cval1, cval2); | |
e26ec0bb RS |
9625 | } |
9626 | } | |
9627 | } | |
9628 | ||
e26ec0bb RS |
9629 | /* We can fold X/C1 op C2 where C1 and C2 are integer constants |
9630 | into a single range test. */ | |
9631 | if ((TREE_CODE (arg0) == TRUNC_DIV_EXPR | |
9632 | || TREE_CODE (arg0) == EXACT_DIV_EXPR) | |
9633 | && TREE_CODE (arg1) == INTEGER_CST | |
9634 | && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST | |
9635 | && !integer_zerop (TREE_OPERAND (arg0, 1)) | |
9636 | && !TREE_OVERFLOW (TREE_OPERAND (arg0, 1)) | |
9637 | && !TREE_OVERFLOW (arg1)) | |
9638 | { | |
db3927fb | 9639 | tem = fold_div_compare (loc, code, type, arg0, arg1); |
e26ec0bb RS |
9640 | if (tem != NULL_TREE) |
9641 | return tem; | |
9642 | } | |
9643 | ||
c159ffe7 RS |
9644 | /* Fold ~X op ~Y as Y op X. */ |
9645 | if (TREE_CODE (arg0) == BIT_NOT_EXPR | |
9646 | && TREE_CODE (arg1) == BIT_NOT_EXPR) | |
270d43bf RS |
9647 | { |
9648 | tree cmp_type = TREE_TYPE (TREE_OPERAND (arg0, 0)); | |
db3927fb AH |
9649 | return fold_build2_loc (loc, code, type, |
9650 | fold_convert_loc (loc, cmp_type, | |
9651 | TREE_OPERAND (arg1, 0)), | |
270d43bf RS |
9652 | TREE_OPERAND (arg0, 0)); |
9653 | } | |
c159ffe7 RS |
9654 | |
9655 | /* Fold ~X op C as X op' ~C, where op' is the swapped comparison. */ | |
9656 | if (TREE_CODE (arg0) == BIT_NOT_EXPR | |
418d1b87 | 9657 | && (TREE_CODE (arg1) == INTEGER_CST || TREE_CODE (arg1) == VECTOR_CST)) |
270d43bf RS |
9658 | { |
9659 | tree cmp_type = TREE_TYPE (TREE_OPERAND (arg0, 0)); | |
db3927fb | 9660 | return fold_build2_loc (loc, swap_tree_comparison (code), type, |
270d43bf | 9661 | TREE_OPERAND (arg0, 0), |
db3927fb AH |
9662 | fold_build1_loc (loc, BIT_NOT_EXPR, cmp_type, |
9663 | fold_convert_loc (loc, cmp_type, arg1))); | |
270d43bf | 9664 | } |
c159ffe7 | 9665 | |
e26ec0bb RS |
9666 | return NULL_TREE; |
9667 | } | |
9668 | ||
99b25753 RS |
9669 | |
9670 | /* Subroutine of fold_binary. Optimize complex multiplications of the | |
9671 | form z * conj(z), as pow(realpart(z),2) + pow(imagpart(z),2). The | |
9672 | argument EXPR represents the expression "z" of type TYPE. */ | |
9673 | ||
9674 | static tree | |
db3927fb | 9675 | fold_mult_zconjz (location_t loc, tree type, tree expr) |
99b25753 RS |
9676 | { |
9677 | tree itype = TREE_TYPE (type); | |
9678 | tree rpart, ipart, tem; | |
9679 | ||
9680 | if (TREE_CODE (expr) == COMPLEX_EXPR) | |
9681 | { | |
9682 | rpart = TREE_OPERAND (expr, 0); | |
9683 | ipart = TREE_OPERAND (expr, 1); | |
9684 | } | |
9685 | else if (TREE_CODE (expr) == COMPLEX_CST) | |
9686 | { | |
9687 | rpart = TREE_REALPART (expr); | |
9688 | ipart = TREE_IMAGPART (expr); | |
9689 | } | |
9690 | else | |
9691 | { | |
9692 | expr = save_expr (expr); | |
db3927fb AH |
9693 | rpart = fold_build1_loc (loc, REALPART_EXPR, itype, expr); |
9694 | ipart = fold_build1_loc (loc, IMAGPART_EXPR, itype, expr); | |
99b25753 RS |
9695 | } |
9696 | ||
9697 | rpart = save_expr (rpart); | |
9698 | ipart = save_expr (ipart); | |
db3927fb AH |
9699 | tem = fold_build2_loc (loc, PLUS_EXPR, itype, |
9700 | fold_build2_loc (loc, MULT_EXPR, itype, rpart, rpart), | |
9701 | fold_build2_loc (loc, MULT_EXPR, itype, ipart, ipart)); | |
9702 | return fold_build2_loc (loc, COMPLEX_EXPR, type, tem, | |
e8160c9a | 9703 | build_zero_cst (itype)); |
99b25753 RS |
9704 | } |
9705 | ||
9706 | ||
e5901cad OW |
9707 | /* Subroutine of fold_binary. If P is the value of EXPR, computes |
9708 | power-of-two M and (arbitrary) N such that M divides (P-N). This condition | |
9709 | guarantees that P and N have the same least significant log2(M) bits. | |
9710 | N is not otherwise constrained. In particular, N is not normalized to | |
9711 | 0 <= N < M as is common. In general, the precise value of P is unknown. | |
9712 | M is chosen as large as possible such that constant N can be determined. | |
9713 | ||
617f3897 MJ |
9714 | Returns M and sets *RESIDUE to N. |
9715 | ||
9716 | If ALLOW_FUNC_ALIGN is true, do take functions' DECL_ALIGN_UNIT into | |
9717 | account. This is not always possible due to PR 35705. | |
9718 | */ | |
e5901cad OW |
9719 | |
9720 | static unsigned HOST_WIDE_INT | |
617f3897 MJ |
9721 | get_pointer_modulus_and_residue (tree expr, unsigned HOST_WIDE_INT *residue, |
9722 | bool allow_func_align) | |
e5901cad OW |
9723 | { |
9724 | enum tree_code code; | |
9725 | ||
9726 | *residue = 0; | |
9727 | ||
9728 | code = TREE_CODE (expr); | |
73f6eabc | 9729 | if (code == ADDR_EXPR) |
e5901cad | 9730 | { |
daade206 | 9731 | unsigned int bitalign; |
644ffefd | 9732 | get_object_alignment_1 (TREE_OPERAND (expr, 0), &bitalign, residue); |
daade206 RG |
9733 | *residue /= BITS_PER_UNIT; |
9734 | return bitalign / BITS_PER_UNIT; | |
e5901cad OW |
9735 | } |
9736 | else if (code == POINTER_PLUS_EXPR) | |
9737 | { | |
9738 | tree op0, op1; | |
9739 | unsigned HOST_WIDE_INT modulus; | |
9740 | enum tree_code inner_code; | |
b8698a0f | 9741 | |
e5901cad OW |
9742 | op0 = TREE_OPERAND (expr, 0); |
9743 | STRIP_NOPS (op0); | |
617f3897 MJ |
9744 | modulus = get_pointer_modulus_and_residue (op0, residue, |
9745 | allow_func_align); | |
e5901cad OW |
9746 | |
9747 | op1 = TREE_OPERAND (expr, 1); | |
9748 | STRIP_NOPS (op1); | |
9749 | inner_code = TREE_CODE (op1); | |
9750 | if (inner_code == INTEGER_CST) | |
9751 | { | |
9752 | *residue += TREE_INT_CST_LOW (op1); | |
9753 | return modulus; | |
9754 | } | |
9755 | else if (inner_code == MULT_EXPR) | |
9756 | { | |
9757 | op1 = TREE_OPERAND (op1, 1); | |
9758 | if (TREE_CODE (op1) == INTEGER_CST) | |
9759 | { | |
9760 | unsigned HOST_WIDE_INT align; | |
b8698a0f | 9761 | |
e5901cad OW |
9762 | /* Compute the greatest power-of-2 divisor of op1. */ |
9763 | align = TREE_INT_CST_LOW (op1); | |
9764 | align &= -align; | |
9765 | ||
9766 | /* If align is non-zero and less than *modulus, replace | |
9767 | *modulus with align., If align is 0, then either op1 is 0 | |
9768 | or the greatest power-of-2 divisor of op1 doesn't fit in an | |
9769 | unsigned HOST_WIDE_INT. In either case, no additional | |
9770 | constraint is imposed. */ | |
9771 | if (align) | |
9772 | modulus = MIN (modulus, align); | |
9773 | ||
9774 | return modulus; | |
9775 | } | |
9776 | } | |
9777 | } | |
9778 | ||
daade206 RG |
9779 | /* If we get here, we were unable to determine anything useful about the |
9780 | expression. */ | |
9781 | return 1; | |
e5901cad OW |
9782 | } |
9783 | ||
22d8712a JJ |
9784 | /* Helper function for fold_vec_perm. Store elements of VECTOR_CST or |
9785 | CONSTRUCTOR ARG into array ELTS and return true if successful. */ | |
9786 | ||
9787 | static bool | |
9788 | vec_cst_ctor_to_array (tree arg, tree *elts) | |
9789 | { | |
9790 | unsigned int nelts = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg)), i; | |
9791 | ||
9792 | if (TREE_CODE (arg) == VECTOR_CST) | |
9793 | { | |
d2a12ae7 RG |
9794 | for (i = 0; i < VECTOR_CST_NELTS (arg); ++i) |
9795 | elts[i] = VECTOR_CST_ELT (arg, i); | |
22d8712a JJ |
9796 | } |
9797 | else if (TREE_CODE (arg) == CONSTRUCTOR) | |
9798 | { | |
9799 | constructor_elt *elt; | |
9800 | ||
9771b263 | 9801 | FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (arg), i, elt) |
4a2c20cc | 9802 | if (i >= nelts || TREE_CODE (TREE_TYPE (elt->value)) == VECTOR_TYPE) |
22d8712a JJ |
9803 | return false; |
9804 | else | |
9805 | elts[i] = elt->value; | |
9806 | } | |
9807 | else | |
9808 | return false; | |
9809 | for (; i < nelts; i++) | |
9810 | elts[i] | |
9811 | = fold_convert (TREE_TYPE (TREE_TYPE (arg)), integer_zero_node); | |
9812 | return true; | |
9813 | } | |
9814 | ||
9815 | /* Attempt to fold vector permutation of ARG0 and ARG1 vectors using SEL | |
9816 | selector. Return the folded VECTOR_CST or CONSTRUCTOR if successful, | |
9817 | NULL_TREE otherwise. */ | |
9818 | ||
9819 | static tree | |
9820 | fold_vec_perm (tree type, tree arg0, tree arg1, const unsigned char *sel) | |
9821 | { | |
9822 | unsigned int nelts = TYPE_VECTOR_SUBPARTS (type), i; | |
9823 | tree *elts; | |
9824 | bool need_ctor = false; | |
9825 | ||
9826 | gcc_assert (TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0)) == nelts | |
9827 | && TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg1)) == nelts); | |
9828 | if (TREE_TYPE (TREE_TYPE (arg0)) != TREE_TYPE (type) | |
9829 | || TREE_TYPE (TREE_TYPE (arg1)) != TREE_TYPE (type)) | |
9830 | return NULL_TREE; | |
9831 | ||
9832 | elts = XALLOCAVEC (tree, nelts * 3); | |
9833 | if (!vec_cst_ctor_to_array (arg0, elts) | |
9834 | || !vec_cst_ctor_to_array (arg1, elts + nelts)) | |
9835 | return NULL_TREE; | |
9836 | ||
9837 | for (i = 0; i < nelts; i++) | |
9838 | { | |
9839 | if (!CONSTANT_CLASS_P (elts[sel[i]])) | |
9840 | need_ctor = true; | |
9841 | elts[i + 2 * nelts] = unshare_expr (elts[sel[i]]); | |
9842 | } | |
9843 | ||
9844 | if (need_ctor) | |
9845 | { | |
9771b263 DN |
9846 | vec<constructor_elt, va_gc> *v; |
9847 | vec_alloc (v, nelts); | |
22d8712a JJ |
9848 | for (i = 0; i < nelts; i++) |
9849 | CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, elts[2 * nelts + i]); | |
9850 | return build_constructor (type, v); | |
9851 | } | |
9852 | else | |
d2a12ae7 | 9853 | return build_vector (type, &elts[2 * nelts]); |
22d8712a | 9854 | } |
e5901cad | 9855 | |
bf948210 RG |
9856 | /* Try to fold a pointer difference of type TYPE two address expressions of |
9857 | array references AREF0 and AREF1 using location LOC. Return a | |
9858 | simplified expression for the difference or NULL_TREE. */ | |
9859 | ||
9860 | static tree | |
9861 | fold_addr_of_array_ref_difference (location_t loc, tree type, | |
9862 | tree aref0, tree aref1) | |
9863 | { | |
9864 | tree base0 = TREE_OPERAND (aref0, 0); | |
9865 | tree base1 = TREE_OPERAND (aref1, 0); | |
9866 | tree base_offset = build_int_cst (type, 0); | |
9867 | ||
9868 | /* If the bases are array references as well, recurse. If the bases | |
9869 | are pointer indirections compute the difference of the pointers. | |
9870 | If the bases are equal, we are set. */ | |
9871 | if ((TREE_CODE (base0) == ARRAY_REF | |
9872 | && TREE_CODE (base1) == ARRAY_REF | |
9873 | && (base_offset | |
9874 | = fold_addr_of_array_ref_difference (loc, type, base0, base1))) | |
9875 | || (INDIRECT_REF_P (base0) | |
9876 | && INDIRECT_REF_P (base1) | |
9877 | && (base_offset = fold_binary_loc (loc, MINUS_EXPR, type, | |
9878 | TREE_OPERAND (base0, 0), | |
9879 | TREE_OPERAND (base1, 0)))) | |
9880 | || operand_equal_p (base0, base1, 0)) | |
9881 | { | |
9882 | tree op0 = fold_convert_loc (loc, type, TREE_OPERAND (aref0, 1)); | |
9883 | tree op1 = fold_convert_loc (loc, type, TREE_OPERAND (aref1, 1)); | |
9884 | tree esz = fold_convert_loc (loc, type, array_ref_element_size (aref0)); | |
9885 | tree diff = build2 (MINUS_EXPR, type, op0, op1); | |
9886 | return fold_build2_loc (loc, PLUS_EXPR, type, | |
9887 | base_offset, | |
9888 | fold_build2_loc (loc, MULT_EXPR, type, | |
9889 | diff, esz)); | |
9890 | } | |
9891 | return NULL_TREE; | |
9892 | } | |
9893 | ||
add6207a BS |
9894 | /* If the real or vector real constant CST of type TYPE has an exact |
9895 | inverse, return it, else return NULL. */ | |
9896 | ||
9897 | static tree | |
9898 | exact_inverse (tree type, tree cst) | |
9899 | { | |
9900 | REAL_VALUE_TYPE r; | |
9901 | tree unit_type, *elts; | |
9902 | enum machine_mode mode; | |
9903 | unsigned vec_nelts, i; | |
9904 | ||
9905 | switch (TREE_CODE (cst)) | |
9906 | { | |
9907 | case REAL_CST: | |
9908 | r = TREE_REAL_CST (cst); | |
9909 | ||
9910 | if (exact_real_inverse (TYPE_MODE (type), &r)) | |
9911 | return build_real (type, r); | |
9912 | ||
9913 | return NULL_TREE; | |
9914 | ||
9915 | case VECTOR_CST: | |
9916 | vec_nelts = VECTOR_CST_NELTS (cst); | |
9917 | elts = XALLOCAVEC (tree, vec_nelts); | |
9918 | unit_type = TREE_TYPE (type); | |
9919 | mode = TYPE_MODE (unit_type); | |
9920 | ||
9921 | for (i = 0; i < vec_nelts; i++) | |
9922 | { | |
9923 | r = TREE_REAL_CST (VECTOR_CST_ELT (cst, i)); | |
9924 | if (!exact_real_inverse (mode, &r)) | |
9925 | return NULL_TREE; | |
9926 | elts[i] = build_real (unit_type, r); | |
9927 | } | |
9928 | ||
9929 | return build_vector (type, elts); | |
9930 | ||
9931 | default: | |
9932 | return NULL_TREE; | |
9933 | } | |
9934 | } | |
9935 | ||
e3d3cfb4 KT |
9936 | /* Mask out the tz least significant bits of X of type TYPE where |
9937 | tz is the number of trailing zeroes in Y. */ | |
9938 | static double_int | |
9939 | mask_with_tz (tree type, double_int x, double_int y) | |
9940 | { | |
9941 | int tz = y.trailing_zeros (); | |
9942 | ||
9943 | if (tz > 0) | |
9944 | { | |
9945 | double_int mask; | |
9946 | ||
9947 | mask = ~double_int::mask (tz); | |
9948 | mask = mask.ext (TYPE_PRECISION (type), TYPE_UNSIGNED (type)); | |
9949 | return mask & x; | |
9950 | } | |
9951 | return x; | |
9952 | } | |
9953 | ||
7107fa7c | 9954 | /* Fold a binary expression of code CODE and type TYPE with operands |
db3927fb AH |
9955 | OP0 and OP1. LOC is the location of the resulting expression. |
9956 | Return the folded expression if folding is successful. Otherwise, | |
9957 | return NULL_TREE. */ | |
0aee4751 | 9958 | |
721425b6 | 9959 | tree |
db3927fb AH |
9960 | fold_binary_loc (location_t loc, |
9961 | enum tree_code code, tree type, tree op0, tree op1) | |
0aee4751 | 9962 | { |
0aee4751 | 9963 | enum tree_code_class kind = TREE_CODE_CLASS (code); |
e26ec0bb RS |
9964 | tree arg0, arg1, tem; |
9965 | tree t1 = NULL_TREE; | |
6ac01510 | 9966 | bool strict_overflow_p; |
a5e0cd1d | 9967 | unsigned int prec; |
0aee4751 | 9968 | |
726a989a | 9969 | gcc_assert (IS_EXPR_CODE_CLASS (kind) |
fd6c76f4 RS |
9970 | && TREE_CODE_LENGTH (code) == 2 |
9971 | && op0 != NULL_TREE | |
9972 | && op1 != NULL_TREE); | |
0aee4751 | 9973 | |
fbaa905c KH |
9974 | arg0 = op0; |
9975 | arg1 = op1; | |
1eaea409 | 9976 | |
fd6c76f4 RS |
9977 | /* Strip any conversions that don't change the mode. This is |
9978 | safe for every expression, except for a comparison expression | |
9979 | because its signedness is derived from its operands. So, in | |
9980 | the latter case, only strip conversions that don't change the | |
f61edbf6 JJ |
9981 | signedness. MIN_EXPR/MAX_EXPR also need signedness of arguments |
9982 | preserved. | |
0aee4751 | 9983 | |
fd6c76f4 RS |
9984 | Note that this is done as an internal manipulation within the |
9985 | constant folder, in order to find the simplest representation | |
9986 | of the arguments so that their form can be studied. In any | |
9987 | cases, the appropriate type conversions should be put back in | |
9988 | the tree that will get out of the constant folder. */ | |
0aee4751 | 9989 | |
f61edbf6 | 9990 | if (kind == tcc_comparison || code == MIN_EXPR || code == MAX_EXPR) |
fd6c76f4 RS |
9991 | { |
9992 | STRIP_SIGN_NOPS (arg0); | |
9993 | STRIP_SIGN_NOPS (arg1); | |
1eaea409 | 9994 | } |
fd6c76f4 | 9995 | else |
1eaea409 | 9996 | { |
fd6c76f4 RS |
9997 | STRIP_NOPS (arg0); |
9998 | STRIP_NOPS (arg1); | |
9999 | } | |
0aee4751 | 10000 | |
fd6c76f4 RS |
10001 | /* Note that TREE_CONSTANT isn't enough: static var addresses are |
10002 | constant but we can't do arithmetic on them. */ | |
10003 | if ((TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST) | |
10004 | || (TREE_CODE (arg0) == REAL_CST && TREE_CODE (arg1) == REAL_CST) | |
325217ed CF |
10005 | || (TREE_CODE (arg0) == FIXED_CST && TREE_CODE (arg1) == FIXED_CST) |
10006 | || (TREE_CODE (arg0) == FIXED_CST && TREE_CODE (arg1) == INTEGER_CST) | |
fd6c76f4 | 10007 | || (TREE_CODE (arg0) == COMPLEX_CST && TREE_CODE (arg1) == COMPLEX_CST) |
bb506982 MG |
10008 | || (TREE_CODE (arg0) == VECTOR_CST && TREE_CODE (arg1) == VECTOR_CST) |
10009 | || (TREE_CODE (arg0) == VECTOR_CST && TREE_CODE (arg1) == INTEGER_CST)) | |
fd6c76f4 RS |
10010 | { |
10011 | if (kind == tcc_binary) | |
325217ed CF |
10012 | { |
10013 | /* Make sure type and arg0 have the same saturating flag. */ | |
10014 | gcc_assert (TYPE_SATURATING (type) | |
10015 | == TYPE_SATURATING (TREE_TYPE (arg0))); | |
43a5d30b | 10016 | tem = const_binop (code, arg0, arg1); |
325217ed | 10017 | } |
fd6c76f4 RS |
10018 | else if (kind == tcc_comparison) |
10019 | tem = fold_relational_const (code, type, arg0, arg1); | |
1eaea409 | 10020 | else |
fd6c76f4 | 10021 | tem = NULL_TREE; |
1eaea409 | 10022 | |
fd6c76f4 RS |
10023 | if (tem != NULL_TREE) |
10024 | { | |
10025 | if (TREE_TYPE (tem) != type) | |
db3927fb | 10026 | tem = fold_convert_loc (loc, type, tem); |
fd6c76f4 RS |
10027 | return tem; |
10028 | } | |
0aee4751 KH |
10029 | } |
10030 | ||
10031 | /* If this is a commutative operation, and ARG0 is a constant, move it | |
10032 | to ARG1 to reduce the number of tests below. */ | |
10033 | if (commutative_tree_code (code) | |
10034 | && tree_swap_operands_p (arg0, arg1, true)) | |
db3927fb | 10035 | return fold_build2_loc (loc, code, type, op1, op0); |
0aee4751 | 10036 | |
fd6c76f4 | 10037 | /* ARG0 is the first operand of EXPR, and ARG1 is the second operand. |
0aee4751 KH |
10038 | |
10039 | First check for cases where an arithmetic operation is applied to a | |
10040 | compound, conditional, or comparison operation. Push the arithmetic | |
10041 | operation inside the compound or conditional to see if any folding | |
10042 | can then be done. Convert comparison to conditional for this purpose. | |
10043 | The also optimizes non-constant cases that used to be done in | |
10044 | expand_expr. | |
10045 | ||
10046 | Before we do that, see if this is a BIT_AND_EXPR or a BIT_IOR_EXPR, | |
10047 | one of the operands is a comparison and the other is a comparison, a | |
10048 | BIT_AND_EXPR with the constant 1, or a truth value. In that case, the | |
10049 | code below would make the expression more complex. Change it to a | |
10050 | TRUTH_{AND,OR}_EXPR. Likewise, convert a similar NE_EXPR to | |
10051 | TRUTH_XOR_EXPR and an EQ_EXPR to the inversion of a TRUTH_XOR_EXPR. */ | |
10052 | ||
10053 | if ((code == BIT_AND_EXPR || code == BIT_IOR_EXPR | |
10054 | || code == EQ_EXPR || code == NE_EXPR) | |
31ed6226 | 10055 | && TREE_CODE (type) != VECTOR_TYPE |
0aee4751 KH |
10056 | && ((truth_value_p (TREE_CODE (arg0)) |
10057 | && (truth_value_p (TREE_CODE (arg1)) | |
10058 | || (TREE_CODE (arg1) == BIT_AND_EXPR | |
10059 | && integer_onep (TREE_OPERAND (arg1, 1))))) | |
10060 | || (truth_value_p (TREE_CODE (arg1)) | |
10061 | && (truth_value_p (TREE_CODE (arg0)) | |
10062 | || (TREE_CODE (arg0) == BIT_AND_EXPR | |
10063 | && integer_onep (TREE_OPERAND (arg0, 1))))))) | |
10064 | { | |
db3927fb | 10065 | tem = fold_build2_loc (loc, code == BIT_AND_EXPR ? TRUTH_AND_EXPR |
7f20a5b7 KH |
10066 | : code == BIT_IOR_EXPR ? TRUTH_OR_EXPR |
10067 | : TRUTH_XOR_EXPR, | |
10068 | boolean_type_node, | |
db3927fb AH |
10069 | fold_convert_loc (loc, boolean_type_node, arg0), |
10070 | fold_convert_loc (loc, boolean_type_node, arg1)); | |
0aee4751 KH |
10071 | |
10072 | if (code == EQ_EXPR) | |
db3927fb | 10073 | tem = invert_truthvalue_loc (loc, tem); |
0aee4751 | 10074 | |
db3927fb | 10075 | return fold_convert_loc (loc, type, tem); |
0aee4751 KH |
10076 | } |
10077 | ||
4c17e288 RG |
10078 | if (TREE_CODE_CLASS (code) == tcc_binary |
10079 | || TREE_CODE_CLASS (code) == tcc_comparison) | |
0aee4751 KH |
10080 | { |
10081 | if (TREE_CODE (arg0) == COMPOUND_EXPR) | |
db3927fb AH |
10082 | { |
10083 | tem = fold_build2_loc (loc, code, type, | |
10084 | fold_convert_loc (loc, TREE_TYPE (op0), | |
10085 | TREE_OPERAND (arg0, 1)), op1); | |
c9019218 JJ |
10086 | return build2_loc (loc, COMPOUND_EXPR, type, TREE_OPERAND (arg0, 0), |
10087 | tem); | |
db3927fb | 10088 | } |
0aee4751 KH |
10089 | if (TREE_CODE (arg1) == COMPOUND_EXPR |
10090 | && reorder_operands_p (arg0, TREE_OPERAND (arg1, 0))) | |
db3927fb AH |
10091 | { |
10092 | tem = fold_build2_loc (loc, code, type, op0, | |
10093 | fold_convert_loc (loc, TREE_TYPE (op1), | |
10094 | TREE_OPERAND (arg1, 1))); | |
c9019218 JJ |
10095 | return build2_loc (loc, COMPOUND_EXPR, type, TREE_OPERAND (arg1, 0), |
10096 | tem); | |
db3927fb | 10097 | } |
0aee4751 | 10098 | |
2e2e628b MG |
10099 | if (TREE_CODE (arg0) == COND_EXPR |
10100 | || TREE_CODE (arg0) == VEC_COND_EXPR | |
10101 | || COMPARISON_CLASS_P (arg0)) | |
0aee4751 | 10102 | { |
db3927fb | 10103 | tem = fold_binary_op_with_conditional_arg (loc, code, type, op0, op1, |
b8698a0f | 10104 | arg0, arg1, |
0aee4751 KH |
10105 | /*cond_first_p=*/1); |
10106 | if (tem != NULL_TREE) | |
10107 | return tem; | |
10108 | } | |
10109 | ||
2e2e628b MG |
10110 | if (TREE_CODE (arg1) == COND_EXPR |
10111 | || TREE_CODE (arg1) == VEC_COND_EXPR | |
10112 | || COMPARISON_CLASS_P (arg1)) | |
0aee4751 | 10113 | { |
db3927fb | 10114 | tem = fold_binary_op_with_conditional_arg (loc, code, type, op0, op1, |
b8698a0f | 10115 | arg1, arg0, |
0aee4751 KH |
10116 | /*cond_first_p=*/0); |
10117 | if (tem != NULL_TREE) | |
10118 | return tem; | |
10119 | } | |
10120 | } | |
10121 | ||
10122 | switch (code) | |
10123 | { | |
70f34814 RG |
10124 | case MEM_REF: |
10125 | /* MEM[&MEM[p, CST1], CST2] -> MEM[p, CST1 + CST2]. */ | |
10126 | if (TREE_CODE (arg0) == ADDR_EXPR | |
10127 | && TREE_CODE (TREE_OPERAND (arg0, 0)) == MEM_REF) | |
10128 | { | |
10129 | tree iref = TREE_OPERAND (arg0, 0); | |
10130 | return fold_build2 (MEM_REF, type, | |
10131 | TREE_OPERAND (iref, 0), | |
10132 | int_const_binop (PLUS_EXPR, arg1, | |
d35936ab | 10133 | TREE_OPERAND (iref, 1))); |
70f34814 RG |
10134 | } |
10135 | ||
10136 | /* MEM[&a.b, CST2] -> MEM[&a, offsetof (a, b) + CST2]. */ | |
10137 | if (TREE_CODE (arg0) == ADDR_EXPR | |
10138 | && handled_component_p (TREE_OPERAND (arg0, 0))) | |
10139 | { | |
10140 | tree base; | |
10141 | HOST_WIDE_INT coffset; | |
10142 | base = get_addr_base_and_unit_offset (TREE_OPERAND (arg0, 0), | |
10143 | &coffset); | |
10144 | if (!base) | |
10145 | return NULL_TREE; | |
10146 | return fold_build2 (MEM_REF, type, | |
10147 | build_fold_addr_expr (base), | |
10148 | int_const_binop (PLUS_EXPR, arg1, | |
d35936ab | 10149 | size_int (coffset))); |
70f34814 RG |
10150 | } |
10151 | ||
10152 | return NULL_TREE; | |
10153 | ||
5be014d5 AP |
10154 | case POINTER_PLUS_EXPR: |
10155 | /* 0 +p index -> (type)index */ | |
10156 | if (integer_zerop (arg0)) | |
db3927fb | 10157 | return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg1)); |
5be014d5 AP |
10158 | |
10159 | /* PTR +p 0 -> PTR */ | |
10160 | if (integer_zerop (arg1)) | |
db3927fb | 10161 | return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0)); |
5be014d5 AP |
10162 | |
10163 | /* INT +p INT -> (PTR)(INT + INT). Stripping types allows for this. */ | |
10164 | if (INTEGRAL_TYPE_P (TREE_TYPE (arg1)) | |
10165 | && INTEGRAL_TYPE_P (TREE_TYPE (arg0))) | |
db3927fb AH |
10166 | return fold_convert_loc (loc, type, |
10167 | fold_build2_loc (loc, PLUS_EXPR, sizetype, | |
10168 | fold_convert_loc (loc, sizetype, | |
10169 | arg1), | |
10170 | fold_convert_loc (loc, sizetype, | |
10171 | arg0))); | |
5be014d5 | 10172 | |
5be014d5 AP |
10173 | /* (PTR +p B) +p A -> PTR +p (B + A) */ |
10174 | if (TREE_CODE (arg0) == POINTER_PLUS_EXPR) | |
10175 | { | |
10176 | tree inner; | |
db3927fb | 10177 | tree arg01 = fold_convert_loc (loc, sizetype, TREE_OPERAND (arg0, 1)); |
5be014d5 | 10178 | tree arg00 = TREE_OPERAND (arg0, 0); |
db3927fb AH |
10179 | inner = fold_build2_loc (loc, PLUS_EXPR, sizetype, |
10180 | arg01, fold_convert_loc (loc, sizetype, arg1)); | |
10181 | return fold_convert_loc (loc, type, | |
5d49b6a7 RG |
10182 | fold_build_pointer_plus_loc (loc, |
10183 | arg00, inner)); | |
5be014d5 AP |
10184 | } |
10185 | ||
10186 | /* PTR_CST +p CST -> CST1 */ | |
10187 | if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST) | |
db3927fb AH |
10188 | return fold_build2_loc (loc, PLUS_EXPR, type, arg0, |
10189 | fold_convert_loc (loc, type, arg1)); | |
5be014d5 AP |
10190 | |
10191 | /* Try replacing &a[i1] +p c * i2 with &a[i1 + i2], if c is step | |
10192 | of the array. Loop optimizer sometimes produce this type of | |
10193 | expressions. */ | |
10194 | if (TREE_CODE (arg0) == ADDR_EXPR) | |
10195 | { | |
db3927fb | 10196 | tem = try_move_mult_to_index (loc, arg0, |
56099f00 RG |
10197 | fold_convert_loc (loc, |
10198 | ssizetype, arg1)); | |
5be014d5 | 10199 | if (tem) |
db3927fb | 10200 | return fold_convert_loc (loc, type, tem); |
5be014d5 AP |
10201 | } |
10202 | ||
10203 | return NULL_TREE; | |
8015455a | 10204 | |
0aee4751 KH |
10205 | case PLUS_EXPR: |
10206 | /* A + (-B) -> A - B */ | |
10207 | if (TREE_CODE (arg1) == NEGATE_EXPR) | |
db3927fb AH |
10208 | return fold_build2_loc (loc, MINUS_EXPR, type, |
10209 | fold_convert_loc (loc, type, arg0), | |
10210 | fold_convert_loc (loc, type, | |
10211 | TREE_OPERAND (arg1, 0))); | |
0aee4751 KH |
10212 | /* (-A) + B -> B - A */ |
10213 | if (TREE_CODE (arg0) == NEGATE_EXPR | |
10214 | && reorder_operands_p (TREE_OPERAND (arg0, 0), arg1)) | |
db3927fb AH |
10215 | return fold_build2_loc (loc, MINUS_EXPR, type, |
10216 | fold_convert_loc (loc, type, arg1), | |
10217 | fold_convert_loc (loc, type, | |
10218 | TREE_OPERAND (arg0, 0))); | |
0ed9a3e3 | 10219 | |
948a1fd9 | 10220 | if (INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type)) |
0aee4751 | 10221 | { |
c22f6d33 UB |
10222 | /* Convert ~A + 1 to -A. */ |
10223 | if (TREE_CODE (arg0) == BIT_NOT_EXPR | |
10224 | && integer_onep (arg1)) | |
db3927fb AH |
10225 | return fold_build1_loc (loc, NEGATE_EXPR, type, |
10226 | fold_convert_loc (loc, type, | |
10227 | TREE_OPERAND (arg0, 0))); | |
0aee4751 | 10228 | |
870aa1eb RS |
10229 | /* ~X + X is -1. */ |
10230 | if (TREE_CODE (arg0) == BIT_NOT_EXPR | |
eeef0e45 | 10231 | && !TYPE_OVERFLOW_TRAPS (type)) |
870aa1eb | 10232 | { |
a49c5793 SP |
10233 | tree tem = TREE_OPERAND (arg0, 0); |
10234 | ||
10235 | STRIP_NOPS (tem); | |
10236 | if (operand_equal_p (tem, arg1, 0)) | |
10237 | { | |
948a1fd9 | 10238 | t1 = build_all_ones_cst (type); |
db3927fb | 10239 | return omit_one_operand_loc (loc, type, t1, arg1); |
a49c5793 | 10240 | } |
870aa1eb RS |
10241 | } |
10242 | ||
10243 | /* X + ~X is -1. */ | |
10244 | if (TREE_CODE (arg1) == BIT_NOT_EXPR | |
eeef0e45 | 10245 | && !TYPE_OVERFLOW_TRAPS (type)) |
870aa1eb | 10246 | { |
a49c5793 SP |
10247 | tree tem = TREE_OPERAND (arg1, 0); |
10248 | ||
10249 | STRIP_NOPS (tem); | |
10250 | if (operand_equal_p (arg0, tem, 0)) | |
10251 | { | |
948a1fd9 | 10252 | t1 = build_all_ones_cst (type); |
db3927fb | 10253 | return omit_one_operand_loc (loc, type, t1, arg0); |
a49c5793 SP |
10254 | } |
10255 | } | |
65648dd4 RG |
10256 | |
10257 | /* X + (X / CST) * -CST is X % CST. */ | |
10258 | if (TREE_CODE (arg1) == MULT_EXPR | |
10259 | && TREE_CODE (TREE_OPERAND (arg1, 0)) == TRUNC_DIV_EXPR | |
10260 | && operand_equal_p (arg0, | |
10261 | TREE_OPERAND (TREE_OPERAND (arg1, 0), 0), 0)) | |
10262 | { | |
10263 | tree cst0 = TREE_OPERAND (TREE_OPERAND (arg1, 0), 1); | |
10264 | tree cst1 = TREE_OPERAND (arg1, 1); | |
db3927fb AH |
10265 | tree sum = fold_binary_loc (loc, PLUS_EXPR, TREE_TYPE (cst1), |
10266 | cst1, cst0); | |
65648dd4 | 10267 | if (sum && integer_zerop (sum)) |
db3927fb AH |
10268 | return fold_convert_loc (loc, type, |
10269 | fold_build2_loc (loc, TRUNC_MOD_EXPR, | |
10270 | TREE_TYPE (arg0), arg0, | |
10271 | cst0)); | |
65648dd4 | 10272 | } |
c22f6d33 UB |
10273 | } |
10274 | ||
4efa9261 EB |
10275 | /* Handle (A1 * C1) + (A2 * C2) with A1, A2 or C1, C2 being the same or |
10276 | one. Make sure the type is not saturating and has the signedness of | |
10277 | the stripped operands, as fold_plusminus_mult_expr will re-associate. | |
10278 | ??? The latter condition should use TYPE_OVERFLOW_* flags instead. */ | |
10279 | if ((TREE_CODE (arg0) == MULT_EXPR | |
10280 | || TREE_CODE (arg1) == MULT_EXPR) | |
325217ed | 10281 | && !TYPE_SATURATING (type) |
4efa9261 EB |
10282 | && TYPE_UNSIGNED (type) == TYPE_UNSIGNED (TREE_TYPE (arg0)) |
10283 | && TYPE_UNSIGNED (type) == TYPE_UNSIGNED (TREE_TYPE (arg1)) | |
a1a82611 | 10284 | && (!FLOAT_TYPE_P (type) || flag_associative_math)) |
c22f6d33 | 10285 | { |
4efa9261 | 10286 | tree tem = fold_plusminus_mult_expr (loc, code, type, arg0, arg1); |
c22f6d33 UB |
10287 | if (tem) |
10288 | return tem; | |
10289 | } | |
10290 | ||
10291 | if (! FLOAT_TYPE_P (type)) | |
10292 | { | |
10293 | if (integer_zerop (arg1)) | |
db3927fb | 10294 | return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0)); |
870aa1eb | 10295 | |
0aee4751 KH |
10296 | /* If we are adding two BIT_AND_EXPR's, both of which are and'ing |
10297 | with a constant, and the two constants have no bits in common, | |
10298 | we should treat this as a BIT_IOR_EXPR since this may produce more | |
10299 | simplifications. */ | |
10300 | if (TREE_CODE (arg0) == BIT_AND_EXPR | |
10301 | && TREE_CODE (arg1) == BIT_AND_EXPR | |
10302 | && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST | |
10303 | && TREE_CODE (TREE_OPERAND (arg1, 1)) == INTEGER_CST | |
10304 | && integer_zerop (const_binop (BIT_AND_EXPR, | |
10305 | TREE_OPERAND (arg0, 1), | |
43a5d30b | 10306 | TREE_OPERAND (arg1, 1)))) |
0aee4751 KH |
10307 | { |
10308 | code = BIT_IOR_EXPR; | |
10309 | goto bit_ior; | |
10310 | } | |
10311 | ||
10312 | /* Reassociate (plus (plus (mult) (foo)) (mult)) as | |
10313 | (plus (plus (mult) (mult)) (foo)) so that we can | |
10314 | take advantage of the factoring cases below. */ | |
a130fb24 RG |
10315 | if (TYPE_OVERFLOW_WRAPS (type) |
10316 | && (((TREE_CODE (arg0) == PLUS_EXPR | |
10317 | || TREE_CODE (arg0) == MINUS_EXPR) | |
10318 | && TREE_CODE (arg1) == MULT_EXPR) | |
10319 | || ((TREE_CODE (arg1) == PLUS_EXPR | |
10320 | || TREE_CODE (arg1) == MINUS_EXPR) | |
10321 | && TREE_CODE (arg0) == MULT_EXPR))) | |
0aee4751 KH |
10322 | { |
10323 | tree parg0, parg1, parg, marg; | |
10324 | enum tree_code pcode; | |
10325 | ||
10326 | if (TREE_CODE (arg1) == MULT_EXPR) | |
10327 | parg = arg0, marg = arg1; | |
10328 | else | |
10329 | parg = arg1, marg = arg0; | |
10330 | pcode = TREE_CODE (parg); | |
10331 | parg0 = TREE_OPERAND (parg, 0); | |
10332 | parg1 = TREE_OPERAND (parg, 1); | |
10333 | STRIP_NOPS (parg0); | |
10334 | STRIP_NOPS (parg1); | |
10335 | ||
10336 | if (TREE_CODE (parg0) == MULT_EXPR | |
10337 | && TREE_CODE (parg1) != MULT_EXPR) | |
db3927fb AH |
10338 | return fold_build2_loc (loc, pcode, type, |
10339 | fold_build2_loc (loc, PLUS_EXPR, type, | |
10340 | fold_convert_loc (loc, type, | |
10341 | parg0), | |
10342 | fold_convert_loc (loc, type, | |
10343 | marg)), | |
10344 | fold_convert_loc (loc, type, parg1)); | |
0aee4751 KH |
10345 | if (TREE_CODE (parg0) != MULT_EXPR |
10346 | && TREE_CODE (parg1) == MULT_EXPR) | |
db3927fb AH |
10347 | return |
10348 | fold_build2_loc (loc, PLUS_EXPR, type, | |
10349 | fold_convert_loc (loc, type, parg0), | |
10350 | fold_build2_loc (loc, pcode, type, | |
10351 | fold_convert_loc (loc, type, marg), | |
10352 | fold_convert_loc (loc, type, | |
10353 | parg1))); | |
0aee4751 | 10354 | } |
0aee4751 KH |
10355 | } |
10356 | else | |
10357 | { | |
10358 | /* See if ARG1 is zero and X + ARG1 reduces to X. */ | |
10359 | if (fold_real_zero_addition_p (TREE_TYPE (arg0), arg1, 0)) | |
db3927fb | 10360 | return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0)); |
0aee4751 KH |
10361 | |
10362 | /* Likewise if the operands are reversed. */ | |
10363 | if (fold_real_zero_addition_p (TREE_TYPE (arg1), arg0, 0)) | |
db3927fb | 10364 | return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg1)); |
0aee4751 KH |
10365 | |
10366 | /* Convert X + -C into X - C. */ | |
10367 | if (TREE_CODE (arg1) == REAL_CST | |
10368 | && REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg1))) | |
10369 | { | |
10370 | tem = fold_negate_const (arg1, type); | |
10371 | if (!TREE_OVERFLOW (arg1) || !flag_trapping_math) | |
db3927fb AH |
10372 | return fold_build2_loc (loc, MINUS_EXPR, type, |
10373 | fold_convert_loc (loc, type, arg0), | |
10374 | fold_convert_loc (loc, type, tem)); | |
0aee4751 KH |
10375 | } |
10376 | ||
9f539671 RG |
10377 | /* Fold __complex__ ( x, 0 ) + __complex__ ( 0, y ) |
10378 | to __complex__ ( x, y ). This is not the same for SNaNs or | |
d1ad84c2 | 10379 | if signed zeros are involved. */ |
9f539671 RG |
10380 | if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0))) |
10381 | && !HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg0))) | |
10382 | && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0))) | |
10383 | { | |
10384 | tree rtype = TREE_TYPE (TREE_TYPE (arg0)); | |
db3927fb AH |
10385 | tree arg0r = fold_unary_loc (loc, REALPART_EXPR, rtype, arg0); |
10386 | tree arg0i = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg0); | |
9f539671 RG |
10387 | bool arg0rz = false, arg0iz = false; |
10388 | if ((arg0r && (arg0rz = real_zerop (arg0r))) | |
10389 | || (arg0i && (arg0iz = real_zerop (arg0i)))) | |
10390 | { | |
db3927fb AH |
10391 | tree arg1r = fold_unary_loc (loc, REALPART_EXPR, rtype, arg1); |
10392 | tree arg1i = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg1); | |
9f539671 RG |
10393 | if (arg0rz && arg1i && real_zerop (arg1i)) |
10394 | { | |
10395 | tree rp = arg1r ? arg1r | |
10396 | : build1 (REALPART_EXPR, rtype, arg1); | |
10397 | tree ip = arg0i ? arg0i | |
10398 | : build1 (IMAGPART_EXPR, rtype, arg0); | |
db3927fb | 10399 | return fold_build2_loc (loc, COMPLEX_EXPR, type, rp, ip); |
9f539671 RG |
10400 | } |
10401 | else if (arg0iz && arg1r && real_zerop (arg1r)) | |
10402 | { | |
10403 | tree rp = arg0r ? arg0r | |
10404 | : build1 (REALPART_EXPR, rtype, arg0); | |
10405 | tree ip = arg1i ? arg1i | |
10406 | : build1 (IMAGPART_EXPR, rtype, arg1); | |
db3927fb | 10407 | return fold_build2_loc (loc, COMPLEX_EXPR, type, rp, ip); |
9f539671 RG |
10408 | } |
10409 | } | |
10410 | } | |
10411 | ||
e0dd989a | 10412 | if (flag_unsafe_math_optimizations |
f8912a55 PB |
10413 | && (TREE_CODE (arg0) == RDIV_EXPR || TREE_CODE (arg0) == MULT_EXPR) |
10414 | && (TREE_CODE (arg1) == RDIV_EXPR || TREE_CODE (arg1) == MULT_EXPR) | |
db3927fb | 10415 | && (tem = distribute_real_division (loc, code, type, arg0, arg1))) |
f8912a55 PB |
10416 | return tem; |
10417 | ||
0aee4751 KH |
10418 | /* Convert x+x into x*2.0. */ |
10419 | if (operand_equal_p (arg0, arg1, 0) | |
10420 | && SCALAR_FLOAT_TYPE_P (type)) | |
db3927fb | 10421 | return fold_build2_loc (loc, MULT_EXPR, type, arg0, |
7f20a5b7 | 10422 | build_real (type, dconst2)); |
0aee4751 | 10423 | |
b8698a0f | 10424 | /* Convert a + (b*c + d*e) into (a + b*c) + d*e. |
a1a82611 RE |
10425 | We associate floats only if the user has specified |
10426 | -fassociative-math. */ | |
10427 | if (flag_associative_math | |
0aee4751 KH |
10428 | && TREE_CODE (arg1) == PLUS_EXPR |
10429 | && TREE_CODE (arg0) != MULT_EXPR) | |
10430 | { | |
10431 | tree tree10 = TREE_OPERAND (arg1, 0); | |
10432 | tree tree11 = TREE_OPERAND (arg1, 1); | |
10433 | if (TREE_CODE (tree11) == MULT_EXPR | |
10434 | && TREE_CODE (tree10) == MULT_EXPR) | |
10435 | { | |
10436 | tree tree0; | |
db3927fb AH |
10437 | tree0 = fold_build2_loc (loc, PLUS_EXPR, type, arg0, tree10); |
10438 | return fold_build2_loc (loc, PLUS_EXPR, type, tree0, tree11); | |
0aee4751 KH |
10439 | } |
10440 | } | |
b8698a0f | 10441 | /* Convert (b*c + d*e) + a into b*c + (d*e +a). |
a1a82611 RE |
10442 | We associate floats only if the user has specified |
10443 | -fassociative-math. */ | |
10444 | if (flag_associative_math | |
0aee4751 KH |
10445 | && TREE_CODE (arg0) == PLUS_EXPR |
10446 | && TREE_CODE (arg1) != MULT_EXPR) | |
10447 | { | |
10448 | tree tree00 = TREE_OPERAND (arg0, 0); | |
10449 | tree tree01 = TREE_OPERAND (arg0, 1); | |
10450 | if (TREE_CODE (tree01) == MULT_EXPR | |
10451 | && TREE_CODE (tree00) == MULT_EXPR) | |
10452 | { | |
10453 | tree tree0; | |
db3927fb AH |
10454 | tree0 = fold_build2_loc (loc, PLUS_EXPR, type, tree01, arg1); |
10455 | return fold_build2_loc (loc, PLUS_EXPR, type, tree00, tree0); | |
0aee4751 KH |
10456 | } |
10457 | } | |
10458 | } | |
10459 | ||
10460 | bit_rotate: | |
10461 | /* (A << C1) + (A >> C2) if A is unsigned and C1+C2 is the size of A | |
10462 | is a rotate of A by C1 bits. */ | |
10463 | /* (A << B) + (A >> (Z - B)) if A is unsigned and Z is the size of A | |
10464 | is a rotate of A by B bits. */ | |
10465 | { | |
10466 | enum tree_code code0, code1; | |
70582b3a | 10467 | tree rtype; |
0aee4751 KH |
10468 | code0 = TREE_CODE (arg0); |
10469 | code1 = TREE_CODE (arg1); | |
10470 | if (((code0 == RSHIFT_EXPR && code1 == LSHIFT_EXPR) | |
10471 | || (code1 == RSHIFT_EXPR && code0 == LSHIFT_EXPR)) | |
10472 | && operand_equal_p (TREE_OPERAND (arg0, 0), | |
10473 | TREE_OPERAND (arg1, 0), 0) | |
70582b3a RG |
10474 | && (rtype = TREE_TYPE (TREE_OPERAND (arg0, 0)), |
10475 | TYPE_UNSIGNED (rtype)) | |
10476 | /* Only create rotates in complete modes. Other cases are not | |
10477 | expanded properly. */ | |
a5e0cd1d MG |
10478 | && (element_precision (rtype) |
10479 | == element_precision (TYPE_MODE (rtype)))) | |
0aee4751 KH |
10480 | { |
10481 | tree tree01, tree11; | |
10482 | enum tree_code code01, code11; | |
10483 | ||
10484 | tree01 = TREE_OPERAND (arg0, 1); | |
10485 | tree11 = TREE_OPERAND (arg1, 1); | |
10486 | STRIP_NOPS (tree01); | |
10487 | STRIP_NOPS (tree11); | |
10488 | code01 = TREE_CODE (tree01); | |
10489 | code11 = TREE_CODE (tree11); | |
10490 | if (code01 == INTEGER_CST | |
10491 | && code11 == INTEGER_CST | |
10492 | && TREE_INT_CST_HIGH (tree01) == 0 | |
10493 | && TREE_INT_CST_HIGH (tree11) == 0 | |
10494 | && ((TREE_INT_CST_LOW (tree01) + TREE_INT_CST_LOW (tree11)) | |
a5e0cd1d | 10495 | == element_precision (TREE_TYPE (TREE_OPERAND (arg0, 0))))) |
db3927fb | 10496 | { |
c9019218 JJ |
10497 | tem = build2_loc (loc, LROTATE_EXPR, |
10498 | TREE_TYPE (TREE_OPERAND (arg0, 0)), | |
10499 | TREE_OPERAND (arg0, 0), | |
10500 | code0 == LSHIFT_EXPR ? tree01 : tree11); | |
db3927fb AH |
10501 | return fold_convert_loc (loc, type, tem); |
10502 | } | |
0aee4751 KH |
10503 | else if (code11 == MINUS_EXPR) |
10504 | { | |
10505 | tree tree110, tree111; | |
10506 | tree110 = TREE_OPERAND (tree11, 0); | |
10507 | tree111 = TREE_OPERAND (tree11, 1); | |
10508 | STRIP_NOPS (tree110); | |
10509 | STRIP_NOPS (tree111); | |
10510 | if (TREE_CODE (tree110) == INTEGER_CST | |
10511 | && 0 == compare_tree_int (tree110, | |
a5e0cd1d | 10512 | element_precision |
0aee4751 KH |
10513 | (TREE_TYPE (TREE_OPERAND |
10514 | (arg0, 0)))) | |
10515 | && operand_equal_p (tree01, tree111, 0)) | |
db3927fb AH |
10516 | return |
10517 | fold_convert_loc (loc, type, | |
10518 | build2 ((code0 == LSHIFT_EXPR | |
10519 | ? LROTATE_EXPR | |
10520 | : RROTATE_EXPR), | |
10521 | TREE_TYPE (TREE_OPERAND (arg0, 0)), | |
10522 | TREE_OPERAND (arg0, 0), tree01)); | |
0aee4751 KH |
10523 | } |
10524 | else if (code01 == MINUS_EXPR) | |
10525 | { | |
10526 | tree tree010, tree011; | |
10527 | tree010 = TREE_OPERAND (tree01, 0); | |
10528 | tree011 = TREE_OPERAND (tree01, 1); | |
10529 | STRIP_NOPS (tree010); | |
10530 | STRIP_NOPS (tree011); | |
10531 | if (TREE_CODE (tree010) == INTEGER_CST | |
10532 | && 0 == compare_tree_int (tree010, | |
a5e0cd1d | 10533 | element_precision |
0aee4751 KH |
10534 | (TREE_TYPE (TREE_OPERAND |
10535 | (arg0, 0)))) | |
10536 | && operand_equal_p (tree11, tree011, 0)) | |
db3927fb AH |
10537 | return fold_convert_loc |
10538 | (loc, type, | |
10539 | build2 ((code0 != LSHIFT_EXPR | |
10540 | ? LROTATE_EXPR | |
10541 | : RROTATE_EXPR), | |
10542 | TREE_TYPE (TREE_OPERAND (arg0, 0)), | |
10543 | TREE_OPERAND (arg0, 0), tree11)); | |
0aee4751 KH |
10544 | } |
10545 | } | |
10546 | } | |
10547 | ||
10548 | associate: | |
10549 | /* In most languages, can't associate operations on floats through | |
10550 | parentheses. Rather than remember where the parentheses were, we | |
10551 | don't associate floats at all, unless the user has specified | |
a1a82611 | 10552 | -fassociative-math. |
325217ed | 10553 | And, we need to make sure type is not saturating. */ |
0aee4751 | 10554 | |
a1a82611 | 10555 | if ((! FLOAT_TYPE_P (type) || flag_associative_math) |
325217ed | 10556 | && !TYPE_SATURATING (type)) |
0aee4751 KH |
10557 | { |
10558 | tree var0, con0, lit0, minus_lit0; | |
10559 | tree var1, con1, lit1, minus_lit1; | |
5442fe48 | 10560 | tree atype = type; |
a6d5f37c | 10561 | bool ok = true; |
0aee4751 KH |
10562 | |
10563 | /* Split both trees into variables, constants, and literals. Then | |
10564 | associate each group together, the constants with literals, | |
10565 | then the result with variables. This increases the chances of | |
10566 | literals being recombined later and of generating relocatable | |
10567 | expressions for the sum of a constant and literal. */ | |
10568 | var0 = split_tree (arg0, code, &con0, &lit0, &minus_lit0, 0); | |
10569 | var1 = split_tree (arg1, code, &con1, &lit1, &minus_lit1, | |
10570 | code == MINUS_EXPR); | |
10571 | ||
9e9ef331 EB |
10572 | /* Recombine MINUS_EXPR operands by using PLUS_EXPR. */ |
10573 | if (code == MINUS_EXPR) | |
10574 | code = PLUS_EXPR; | |
10575 | ||
5442fe48 JJ |
10576 | /* With undefined overflow prefer doing association in a type |
10577 | which wraps on overflow, if that is one of the operand types. */ | |
9e9ef331 EB |
10578 | if ((POINTER_TYPE_P (type) && POINTER_TYPE_OVERFLOW_UNDEFINED) |
10579 | || (INTEGRAL_TYPE_P (type) && !TYPE_OVERFLOW_WRAPS (type))) | |
5442fe48 JJ |
10580 | { |
10581 | if (INTEGRAL_TYPE_P (TREE_TYPE (arg0)) | |
10582 | && TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg0))) | |
10583 | atype = TREE_TYPE (arg0); | |
10584 | else if (INTEGRAL_TYPE_P (TREE_TYPE (arg1)) | |
10585 | && TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg1))) | |
10586 | atype = TREE_TYPE (arg1); | |
10587 | gcc_assert (TYPE_PRECISION (atype) == TYPE_PRECISION (type)); | |
10588 | } | |
10589 | ||
10590 | /* With undefined overflow we can only associate constants with one | |
10591 | variable, and constants whose association doesn't overflow. */ | |
10592 | if ((POINTER_TYPE_P (atype) && POINTER_TYPE_OVERFLOW_UNDEFINED) | |
10593 | || (INTEGRAL_TYPE_P (atype) && !TYPE_OVERFLOW_WRAPS (atype))) | |
a6d5f37c | 10594 | { |
9e9ef331 EB |
10595 | if (var0 && var1) |
10596 | { | |
10597 | tree tmp0 = var0; | |
10598 | tree tmp1 = var1; | |
10599 | ||
10600 | if (TREE_CODE (tmp0) == NEGATE_EXPR) | |
10601 | tmp0 = TREE_OPERAND (tmp0, 0); | |
1caf8dd6 RG |
10602 | if (CONVERT_EXPR_P (tmp0) |
10603 | && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (tmp0, 0))) | |
10604 | && (TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (tmp0, 0))) | |
5442fe48 | 10605 | <= TYPE_PRECISION (atype))) |
1caf8dd6 | 10606 | tmp0 = TREE_OPERAND (tmp0, 0); |
9e9ef331 EB |
10607 | if (TREE_CODE (tmp1) == NEGATE_EXPR) |
10608 | tmp1 = TREE_OPERAND (tmp1, 0); | |
1caf8dd6 RG |
10609 | if (CONVERT_EXPR_P (tmp1) |
10610 | && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (tmp1, 0))) | |
10611 | && (TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (tmp1, 0))) | |
5442fe48 | 10612 | <= TYPE_PRECISION (atype))) |
1caf8dd6 | 10613 | tmp1 = TREE_OPERAND (tmp1, 0); |
9e9ef331 | 10614 | /* The only case we can still associate with two variables |
1caf8dd6 RG |
10615 | is if they are the same, modulo negation and bit-pattern |
10616 | preserving conversions. */ | |
9e9ef331 EB |
10617 | if (!operand_equal_p (tmp0, tmp1, 0)) |
10618 | ok = false; | |
10619 | } | |
a6d5f37c RG |
10620 | } |
10621 | ||
0aee4751 KH |
10622 | /* Only do something if we found more than two objects. Otherwise, |
10623 | nothing has changed and we risk infinite recursion. */ | |
a6d5f37c RG |
10624 | if (ok |
10625 | && (2 < ((var0 != 0) + (var1 != 0) | |
10626 | + (con0 != 0) + (con1 != 0) | |
10627 | + (lit0 != 0) + (lit1 != 0) | |
10628 | + (minus_lit0 != 0) + (minus_lit1 != 0)))) | |
0aee4751 | 10629 | { |
5442fe48 JJ |
10630 | bool any_overflows = false; |
10631 | if (lit0) any_overflows |= TREE_OVERFLOW (lit0); | |
10632 | if (lit1) any_overflows |= TREE_OVERFLOW (lit1); | |
10633 | if (minus_lit0) any_overflows |= TREE_OVERFLOW (minus_lit0); | |
10634 | if (minus_lit1) any_overflows |= TREE_OVERFLOW (minus_lit1); | |
10635 | var0 = associate_trees (loc, var0, var1, code, atype); | |
10636 | con0 = associate_trees (loc, con0, con1, code, atype); | |
10637 | lit0 = associate_trees (loc, lit0, lit1, code, atype); | |
10638 | minus_lit0 = associate_trees (loc, minus_lit0, minus_lit1, | |
10639 | code, atype); | |
0aee4751 KH |
10640 | |
10641 | /* Preserve the MINUS_EXPR if the negative part of the literal is | |
10642 | greater than the positive part. Otherwise, the multiplicative | |
10643 | folding code (i.e extract_muldiv) may be fooled in case | |
10644 | unsigned constants are subtracted, like in the following | |
10645 | example: ((X*2 + 4) - 8U)/2. */ | |
10646 | if (minus_lit0 && lit0) | |
10647 | { | |
10648 | if (TREE_CODE (lit0) == INTEGER_CST | |
10649 | && TREE_CODE (minus_lit0) == INTEGER_CST | |
10650 | && tree_int_cst_lt (lit0, minus_lit0)) | |
10651 | { | |
db3927fb | 10652 | minus_lit0 = associate_trees (loc, minus_lit0, lit0, |
5442fe48 | 10653 | MINUS_EXPR, atype); |
0aee4751 KH |
10654 | lit0 = 0; |
10655 | } | |
10656 | else | |
10657 | { | |
db3927fb | 10658 | lit0 = associate_trees (loc, lit0, minus_lit0, |
5442fe48 | 10659 | MINUS_EXPR, atype); |
0aee4751 KH |
10660 | minus_lit0 = 0; |
10661 | } | |
10662 | } | |
5442fe48 JJ |
10663 | |
10664 | /* Don't introduce overflows through reassociation. */ | |
10665 | if (!any_overflows | |
10666 | && ((lit0 && TREE_OVERFLOW (lit0)) | |
10667 | || (minus_lit0 && TREE_OVERFLOW (minus_lit0)))) | |
10668 | return NULL_TREE; | |
10669 | ||
0aee4751 KH |
10670 | if (minus_lit0) |
10671 | { | |
10672 | if (con0 == 0) | |
db3927fb AH |
10673 | return |
10674 | fold_convert_loc (loc, type, | |
10675 | associate_trees (loc, var0, minus_lit0, | |
5442fe48 | 10676 | MINUS_EXPR, atype)); |
0aee4751 KH |
10677 | else |
10678 | { | |
db3927fb | 10679 | con0 = associate_trees (loc, con0, minus_lit0, |
5442fe48 | 10680 | MINUS_EXPR, atype); |
db3927fb AH |
10681 | return |
10682 | fold_convert_loc (loc, type, | |
10683 | associate_trees (loc, var0, con0, | |
5442fe48 | 10684 | PLUS_EXPR, atype)); |
0aee4751 KH |
10685 | } |
10686 | } | |
10687 | ||
5442fe48 | 10688 | con0 = associate_trees (loc, con0, lit0, code, atype); |
db3927fb AH |
10689 | return |
10690 | fold_convert_loc (loc, type, associate_trees (loc, var0, con0, | |
5442fe48 | 10691 | code, atype)); |
0aee4751 KH |
10692 | } |
10693 | } | |
10694 | ||
62ab45cc | 10695 | return NULL_TREE; |
0aee4751 KH |
10696 | |
10697 | case MINUS_EXPR: | |
5be014d5 AP |
10698 | /* Pointer simplifications for subtraction, simple reassociations. */ |
10699 | if (POINTER_TYPE_P (TREE_TYPE (arg1)) && POINTER_TYPE_P (TREE_TYPE (arg0))) | |
10700 | { | |
10701 | /* (PTR0 p+ A) - (PTR1 p+ B) -> (PTR0 - PTR1) + (A - B) */ | |
10702 | if (TREE_CODE (arg0) == POINTER_PLUS_EXPR | |
10703 | && TREE_CODE (arg1) == POINTER_PLUS_EXPR) | |
10704 | { | |
db3927fb AH |
10705 | tree arg00 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0)); |
10706 | tree arg01 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 1)); | |
10707 | tree arg10 = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 0)); | |
10708 | tree arg11 = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 1)); | |
10709 | return fold_build2_loc (loc, PLUS_EXPR, type, | |
10710 | fold_build2_loc (loc, MINUS_EXPR, type, | |
10711 | arg00, arg10), | |
10712 | fold_build2_loc (loc, MINUS_EXPR, type, | |
10713 | arg01, arg11)); | |
5be014d5 AP |
10714 | } |
10715 | /* (PTR0 p+ A) - PTR1 -> (PTR0 - PTR1) + A, assuming PTR0 - PTR1 simplifies. */ | |
10716 | else if (TREE_CODE (arg0) == POINTER_PLUS_EXPR) | |
10717 | { | |
db3927fb AH |
10718 | tree arg00 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0)); |
10719 | tree arg01 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 1)); | |
10720 | tree tmp = fold_binary_loc (loc, MINUS_EXPR, type, arg00, | |
10721 | fold_convert_loc (loc, type, arg1)); | |
5be014d5 | 10722 | if (tmp) |
db3927fb | 10723 | return fold_build2_loc (loc, PLUS_EXPR, type, tmp, arg01); |
5be014d5 AP |
10724 | } |
10725 | } | |
0aee4751 KH |
10726 | /* A - (-B) -> A + B */ |
10727 | if (TREE_CODE (arg1) == NEGATE_EXPR) | |
db3927fb AH |
10728 | return fold_build2_loc (loc, PLUS_EXPR, type, op0, |
10729 | fold_convert_loc (loc, type, | |
10730 | TREE_OPERAND (arg1, 0))); | |
0aee4751 KH |
10731 | /* (-A) - B -> (-B) - A where B is easily negated and we can swap. */ |
10732 | if (TREE_CODE (arg0) == NEGATE_EXPR | |
0aee4751 KH |
10733 | && negate_expr_p (arg1) |
10734 | && reorder_operands_p (arg0, arg1)) | |
db3927fb AH |
10735 | return fold_build2_loc (loc, MINUS_EXPR, type, |
10736 | fold_convert_loc (loc, type, | |
10737 | negate_expr (arg1)), | |
10738 | fold_convert_loc (loc, type, | |
10739 | TREE_OPERAND (arg0, 0))); | |
cbefb99c | 10740 | /* Convert -A - 1 to ~A. */ |
948a1fd9 | 10741 | if (TREE_CODE (type) != COMPLEX_TYPE |
cbefb99c | 10742 | && TREE_CODE (arg0) == NEGATE_EXPR |
870aa1eb | 10743 | && integer_onep (arg1) |
eeef0e45 | 10744 | && !TYPE_OVERFLOW_TRAPS (type)) |
db3927fb AH |
10745 | return fold_build1_loc (loc, BIT_NOT_EXPR, type, |
10746 | fold_convert_loc (loc, type, | |
10747 | TREE_OPERAND (arg0, 0))); | |
cbefb99c JL |
10748 | |
10749 | /* Convert -1 - A to ~A. */ | |
948a1fd9 | 10750 | if (TREE_CODE (type) != COMPLEX_TYPE |
cbefb99c | 10751 | && integer_all_onesp (arg0)) |
db3927fb | 10752 | return fold_build1_loc (loc, BIT_NOT_EXPR, type, op1); |
0aee4751 | 10753 | |
65648dd4 | 10754 | |
948a1fd9 MG |
10755 | /* X - (X / Y) * Y is X % Y. */ |
10756 | if ((INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type)) | |
65648dd4 RG |
10757 | && TREE_CODE (arg1) == MULT_EXPR |
10758 | && TREE_CODE (TREE_OPERAND (arg1, 0)) == TRUNC_DIV_EXPR | |
10759 | && operand_equal_p (arg0, | |
10760 | TREE_OPERAND (TREE_OPERAND (arg1, 0), 0), 0) | |
10761 | && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg1, 0), 1), | |
10762 | TREE_OPERAND (arg1, 1), 0)) | |
db3927fb AH |
10763 | return |
10764 | fold_convert_loc (loc, type, | |
10765 | fold_build2_loc (loc, TRUNC_MOD_EXPR, TREE_TYPE (arg0), | |
10766 | arg0, TREE_OPERAND (arg1, 1))); | |
65648dd4 | 10767 | |
0aee4751 KH |
10768 | if (! FLOAT_TYPE_P (type)) |
10769 | { | |
fd6c76f4 | 10770 | if (integer_zerop (arg0)) |
db3927fb | 10771 | return negate_expr (fold_convert_loc (loc, type, arg1)); |
0aee4751 | 10772 | if (integer_zerop (arg1)) |
db3927fb | 10773 | return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0)); |
0aee4751 KH |
10774 | |
10775 | /* Fold A - (A & B) into ~B & A. */ | |
10776 | if (!TREE_SIDE_EFFECTS (arg0) | |
10777 | && TREE_CODE (arg1) == BIT_AND_EXPR) | |
10778 | { | |
10779 | if (operand_equal_p (arg0, TREE_OPERAND (arg1, 1), 0)) | |
48075623 | 10780 | { |
db3927fb AH |
10781 | tree arg10 = fold_convert_loc (loc, type, |
10782 | TREE_OPERAND (arg1, 0)); | |
10783 | return fold_build2_loc (loc, BIT_AND_EXPR, type, | |
10784 | fold_build1_loc (loc, BIT_NOT_EXPR, | |
10785 | type, arg10), | |
10786 | fold_convert_loc (loc, type, arg0)); | |
48075623 | 10787 | } |
0aee4751 | 10788 | if (operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0)) |
48075623 | 10789 | { |
db3927fb AH |
10790 | tree arg11 = fold_convert_loc (loc, |
10791 | type, TREE_OPERAND (arg1, 1)); | |
10792 | return fold_build2_loc (loc, BIT_AND_EXPR, type, | |
10793 | fold_build1_loc (loc, BIT_NOT_EXPR, | |
10794 | type, arg11), | |
10795 | fold_convert_loc (loc, type, arg0)); | |
48075623 | 10796 | } |
0aee4751 KH |
10797 | } |
10798 | ||
10799 | /* Fold (A & ~B) - (A & B) into (A ^ B) - B, where B is | |
10800 | any power of 2 minus 1. */ | |
10801 | if (TREE_CODE (arg0) == BIT_AND_EXPR | |
10802 | && TREE_CODE (arg1) == BIT_AND_EXPR | |
10803 | && operand_equal_p (TREE_OPERAND (arg0, 0), | |
10804 | TREE_OPERAND (arg1, 0), 0)) | |
10805 | { | |
10806 | tree mask0 = TREE_OPERAND (arg0, 1); | |
10807 | tree mask1 = TREE_OPERAND (arg1, 1); | |
db3927fb | 10808 | tree tem = fold_build1_loc (loc, BIT_NOT_EXPR, type, mask0); |
0aee4751 KH |
10809 | |
10810 | if (operand_equal_p (tem, mask1, 0)) | |
10811 | { | |
db3927fb | 10812 | tem = fold_build2_loc (loc, BIT_XOR_EXPR, type, |
7f20a5b7 | 10813 | TREE_OPERAND (arg0, 0), mask1); |
db3927fb | 10814 | return fold_build2_loc (loc, MINUS_EXPR, type, tem, mask1); |
0aee4751 KH |
10815 | } |
10816 | } | |
10817 | } | |
10818 | ||
10819 | /* See if ARG1 is zero and X - ARG1 reduces to X. */ | |
10820 | else if (fold_real_zero_addition_p (TREE_TYPE (arg0), arg1, 1)) | |
db3927fb | 10821 | return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0)); |
0aee4751 KH |
10822 | |
10823 | /* (ARG0 - ARG1) is the same as (-ARG1 + ARG0). So check whether | |
10824 | ARG0 is zero and X + ARG0 reduces to X, since that would mean | |
10825 | (-ARG1 + ARG0) reduces to -ARG1. */ | |
fd6c76f4 | 10826 | else if (fold_real_zero_addition_p (TREE_TYPE (arg1), arg0, 0)) |
db3927fb | 10827 | return negate_expr (fold_convert_loc (loc, type, arg1)); |
0aee4751 | 10828 | |
d1ad84c2 KG |
10829 | /* Fold __complex__ ( x, 0 ) - __complex__ ( 0, y ) to |
10830 | __complex__ ( x, -y ). This is not the same for SNaNs or if | |
10831 | signed zeros are involved. */ | |
10832 | if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0))) | |
10833 | && !HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg0))) | |
10834 | && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0))) | |
10835 | { | |
10836 | tree rtype = TREE_TYPE (TREE_TYPE (arg0)); | |
db3927fb AH |
10837 | tree arg0r = fold_unary_loc (loc, REALPART_EXPR, rtype, arg0); |
10838 | tree arg0i = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg0); | |
d1ad84c2 KG |
10839 | bool arg0rz = false, arg0iz = false; |
10840 | if ((arg0r && (arg0rz = real_zerop (arg0r))) | |
10841 | || (arg0i && (arg0iz = real_zerop (arg0i)))) | |
10842 | { | |
db3927fb AH |
10843 | tree arg1r = fold_unary_loc (loc, REALPART_EXPR, rtype, arg1); |
10844 | tree arg1i = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg1); | |
d1ad84c2 KG |
10845 | if (arg0rz && arg1i && real_zerop (arg1i)) |
10846 | { | |
db3927fb | 10847 | tree rp = fold_build1_loc (loc, NEGATE_EXPR, rtype, |
d1ad84c2 KG |
10848 | arg1r ? arg1r |
10849 | : build1 (REALPART_EXPR, rtype, arg1)); | |
10850 | tree ip = arg0i ? arg0i | |
10851 | : build1 (IMAGPART_EXPR, rtype, arg0); | |
db3927fb | 10852 | return fold_build2_loc (loc, COMPLEX_EXPR, type, rp, ip); |
d1ad84c2 KG |
10853 | } |
10854 | else if (arg0iz && arg1r && real_zerop (arg1r)) | |
10855 | { | |
10856 | tree rp = arg0r ? arg0r | |
10857 | : build1 (REALPART_EXPR, rtype, arg0); | |
db3927fb | 10858 | tree ip = fold_build1_loc (loc, NEGATE_EXPR, rtype, |
d1ad84c2 KG |
10859 | arg1i ? arg1i |
10860 | : build1 (IMAGPART_EXPR, rtype, arg1)); | |
db3927fb | 10861 | return fold_build2_loc (loc, COMPLEX_EXPR, type, rp, ip); |
d1ad84c2 KG |
10862 | } |
10863 | } | |
10864 | } | |
10865 | ||
0aee4751 KH |
10866 | /* Fold &x - &x. This can happen from &x.foo - &x. |
10867 | This is unsafe for certain floats even in non-IEEE formats. | |
10868 | In IEEE, it is unsafe because it does wrong for NaNs. | |
10869 | Also note that operand_equal_p is always false if an operand | |
10870 | is volatile. */ | |
10871 | ||
81d2fb02 | 10872 | if ((!FLOAT_TYPE_P (type) || !HONOR_NANS (TYPE_MODE (type))) |
0aee4751 | 10873 | && operand_equal_p (arg0, arg1, 0)) |
e8160c9a | 10874 | return build_zero_cst (type); |
0aee4751 KH |
10875 | |
10876 | /* A - B -> A + (-B) if B is easily negatable. */ | |
fd6c76f4 | 10877 | if (negate_expr_p (arg1) |
0aee4751 KH |
10878 | && ((FLOAT_TYPE_P (type) |
10879 | /* Avoid this transformation if B is a positive REAL_CST. */ | |
10880 | && (TREE_CODE (arg1) != REAL_CST | |
10881 | || REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg1)))) | |
b0cd88d2 | 10882 | || INTEGRAL_TYPE_P (type))) |
db3927fb AH |
10883 | return fold_build2_loc (loc, PLUS_EXPR, type, |
10884 | fold_convert_loc (loc, type, arg0), | |
10885 | fold_convert_loc (loc, type, | |
10886 | negate_expr (arg1))); | |
0aee4751 KH |
10887 | |
10888 | /* Try folding difference of addresses. */ | |
10889 | { | |
10890 | HOST_WIDE_INT diff; | |
10891 | ||
10892 | if ((TREE_CODE (arg0) == ADDR_EXPR | |
10893 | || TREE_CODE (arg1) == ADDR_EXPR) | |
10894 | && ptr_difference_const (arg0, arg1, &diff)) | |
10895 | return build_int_cst_type (type, diff); | |
10896 | } | |
75cf42cc RG |
10897 | |
10898 | /* Fold &a[i] - &a[j] to i-j. */ | |
10899 | if (TREE_CODE (arg0) == ADDR_EXPR | |
10900 | && TREE_CODE (TREE_OPERAND (arg0, 0)) == ARRAY_REF | |
10901 | && TREE_CODE (arg1) == ADDR_EXPR | |
10902 | && TREE_CODE (TREE_OPERAND (arg1, 0)) == ARRAY_REF) | |
10903 | { | |
bf948210 RG |
10904 | tree tem = fold_addr_of_array_ref_difference (loc, type, |
10905 | TREE_OPERAND (arg0, 0), | |
10906 | TREE_OPERAND (arg1, 0)); | |
10907 | if (tem) | |
10908 | return tem; | |
75cf42cc RG |
10909 | } |
10910 | ||
e0dd989a RG |
10911 | if (FLOAT_TYPE_P (type) |
10912 | && flag_unsafe_math_optimizations | |
f8912a55 PB |
10913 | && (TREE_CODE (arg0) == RDIV_EXPR || TREE_CODE (arg0) == MULT_EXPR) |
10914 | && (TREE_CODE (arg1) == RDIV_EXPR || TREE_CODE (arg1) == MULT_EXPR) | |
db3927fb | 10915 | && (tem = distribute_real_division (loc, code, type, arg0, arg1))) |
f8912a55 PB |
10916 | return tem; |
10917 | ||
4efa9261 EB |
10918 | /* Handle (A1 * C1) - (A2 * C2) with A1, A2 or C1, C2 being the same or |
10919 | one. Make sure the type is not saturating and has the signedness of | |
10920 | the stripped operands, as fold_plusminus_mult_expr will re-associate. | |
10921 | ??? The latter condition should use TYPE_OVERFLOW_* flags instead. */ | |
10922 | if ((TREE_CODE (arg0) == MULT_EXPR | |
10923 | || TREE_CODE (arg1) == MULT_EXPR) | |
325217ed | 10924 | && !TYPE_SATURATING (type) |
4efa9261 EB |
10925 | && TYPE_UNSIGNED (type) == TYPE_UNSIGNED (TREE_TYPE (arg0)) |
10926 | && TYPE_UNSIGNED (type) == TYPE_UNSIGNED (TREE_TYPE (arg1)) | |
a1a82611 | 10927 | && (!FLOAT_TYPE_P (type) || flag_associative_math)) |
0ed9a3e3 | 10928 | { |
4efa9261 | 10929 | tree tem = fold_plusminus_mult_expr (loc, code, type, arg0, arg1); |
0ed9a3e3 RG |
10930 | if (tem) |
10931 | return tem; | |
0aee4751 KH |
10932 | } |
10933 | ||
10934 | goto associate; | |
10935 | ||
10936 | case MULT_EXPR: | |
10937 | /* (-A) * (-B) -> A * B */ | |
10938 | if (TREE_CODE (arg0) == NEGATE_EXPR && negate_expr_p (arg1)) | |
db3927fb AH |
10939 | return fold_build2_loc (loc, MULT_EXPR, type, |
10940 | fold_convert_loc (loc, type, | |
10941 | TREE_OPERAND (arg0, 0)), | |
10942 | fold_convert_loc (loc, type, | |
10943 | negate_expr (arg1))); | |
0aee4751 | 10944 | if (TREE_CODE (arg1) == NEGATE_EXPR && negate_expr_p (arg0)) |
db3927fb AH |
10945 | return fold_build2_loc (loc, MULT_EXPR, type, |
10946 | fold_convert_loc (loc, type, | |
10947 | negate_expr (arg0)), | |
10948 | fold_convert_loc (loc, type, | |
10949 | TREE_OPERAND (arg1, 0))); | |
0aee4751 | 10950 | |
0aee4751 KH |
10951 | if (! FLOAT_TYPE_P (type)) |
10952 | { | |
10953 | if (integer_zerop (arg1)) | |
db3927fb | 10954 | return omit_one_operand_loc (loc, type, arg1, arg0); |
0aee4751 | 10955 | if (integer_onep (arg1)) |
db3927fb | 10956 | return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0)); |
b9e67f8b RG |
10957 | /* Transform x * -1 into -x. Make sure to do the negation |
10958 | on the original operand with conversions not stripped | |
10959 | because we can only strip non-sign-changing conversions. */ | |
9a0ee7b0 | 10960 | if (integer_minus_onep (arg1)) |
db3927fb | 10961 | return fold_convert_loc (loc, type, negate_expr (op0)); |
b0cd88d2 RG |
10962 | /* Transform x * -C into -x * C if x is easily negatable. */ |
10963 | if (TREE_CODE (arg1) == INTEGER_CST | |
10964 | && tree_int_cst_sgn (arg1) == -1 | |
10965 | && negate_expr_p (arg0) | |
10966 | && (tem = negate_expr (arg1)) != arg1 | |
10967 | && !TREE_OVERFLOW (tem)) | |
db3927fb AH |
10968 | return fold_build2_loc (loc, MULT_EXPR, type, |
10969 | fold_convert_loc (loc, type, | |
10970 | negate_expr (arg0)), | |
10971 | tem); | |
0aee4751 KH |
10972 | |
10973 | /* (a * (1 << b)) is (a << b) */ | |
10974 | if (TREE_CODE (arg1) == LSHIFT_EXPR | |
10975 | && integer_onep (TREE_OPERAND (arg1, 0))) | |
db3927fb | 10976 | return fold_build2_loc (loc, LSHIFT_EXPR, type, op0, |
7f20a5b7 | 10977 | TREE_OPERAND (arg1, 1)); |
0aee4751 KH |
10978 | if (TREE_CODE (arg0) == LSHIFT_EXPR |
10979 | && integer_onep (TREE_OPERAND (arg0, 0))) | |
db3927fb | 10980 | return fold_build2_loc (loc, LSHIFT_EXPR, type, op1, |
7f20a5b7 | 10981 | TREE_OPERAND (arg0, 1)); |
0aee4751 | 10982 | |
1447bf05 RG |
10983 | /* (A + A) * C -> A * 2 * C */ |
10984 | if (TREE_CODE (arg0) == PLUS_EXPR | |
10985 | && TREE_CODE (arg1) == INTEGER_CST | |
10986 | && operand_equal_p (TREE_OPERAND (arg0, 0), | |
10987 | TREE_OPERAND (arg0, 1), 0)) | |
db3927fb AH |
10988 | return fold_build2_loc (loc, MULT_EXPR, type, |
10989 | omit_one_operand_loc (loc, type, | |
10990 | TREE_OPERAND (arg0, 0), | |
1447bf05 | 10991 | TREE_OPERAND (arg0, 1)), |
db3927fb | 10992 | fold_build2_loc (loc, MULT_EXPR, type, |
1447bf05 RG |
10993 | build_int_cst (type, 2) , arg1)); |
10994 | ||
d480e6a5 RB |
10995 | /* ((T) (X /[ex] C)) * C cancels out if the conversion is |
10996 | sign-changing only. */ | |
10997 | if (TREE_CODE (arg1) == INTEGER_CST | |
10998 | && TREE_CODE (arg0) == EXACT_DIV_EXPR | |
10999 | && operand_equal_p (arg1, TREE_OPERAND (arg0, 1), 0)) | |
11000 | return fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0)); | |
11001 | ||
6ac01510 | 11002 | strict_overflow_p = false; |
0aee4751 | 11003 | if (TREE_CODE (arg1) == INTEGER_CST |
ac029795 | 11004 | && 0 != (tem = extract_muldiv (op0, arg1, code, NULL_TREE, |
6ac01510 ILT |
11005 | &strict_overflow_p))) |
11006 | { | |
11007 | if (strict_overflow_p) | |
11008 | fold_overflow_warning (("assuming signed overflow does not " | |
11009 | "occur when simplifying " | |
11010 | "multiplication"), | |
11011 | WARN_STRICT_OVERFLOW_MISC); | |
db3927fb | 11012 | return fold_convert_loc (loc, type, tem); |
6ac01510 | 11013 | } |
0aee4751 | 11014 | |
99b25753 RS |
11015 | /* Optimize z * conj(z) for integer complex numbers. */ |
11016 | if (TREE_CODE (arg0) == CONJ_EXPR | |
11017 | && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)) | |
db3927fb | 11018 | return fold_mult_zconjz (loc, type, arg1); |
99b25753 RS |
11019 | if (TREE_CODE (arg1) == CONJ_EXPR |
11020 | && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0)) | |
db3927fb | 11021 | return fold_mult_zconjz (loc, type, arg0); |
0aee4751 KH |
11022 | } |
11023 | else | |
11024 | { | |
11025 | /* Maybe fold x * 0 to 0. The expressions aren't the same | |
11026 | when x is NaN, since x * 0 is also NaN. Nor are they the | |
11027 | same in modes with signed zeros, since multiplying a | |
11028 | negative value by 0 gives -0, not +0. */ | |
11029 | if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0))) | |
11030 | && !HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg0))) | |
11031 | && real_zerop (arg1)) | |
db3927fb | 11032 | return omit_one_operand_loc (loc, type, arg1, arg0); |
c94f9067 JM |
11033 | /* In IEEE floating point, x*1 is not equivalent to x for snans. |
11034 | Likewise for complex arithmetic with signed zeros. */ | |
0aee4751 | 11035 | if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0))) |
c94f9067 JM |
11036 | && (!HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg0))) |
11037 | || !COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0))) | |
0aee4751 | 11038 | && real_onep (arg1)) |
db3927fb | 11039 | return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0)); |
0aee4751 KH |
11040 | |
11041 | /* Transform x * -1.0 into -x. */ | |
11042 | if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0))) | |
c94f9067 JM |
11043 | && (!HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg0))) |
11044 | || !COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0))) | |
0aee4751 | 11045 | && real_minus_onep (arg1)) |
db3927fb | 11046 | return fold_convert_loc (loc, type, negate_expr (arg0)); |
0aee4751 | 11047 | |
a1a82611 RE |
11048 | /* Convert (C1/X)*C2 into (C1*C2)/X. This transformation may change |
11049 | the result for floating point types due to rounding so it is applied | |
11050 | only if -fassociative-math was specify. */ | |
11051 | if (flag_associative_math | |
0aee4751 KH |
11052 | && TREE_CODE (arg0) == RDIV_EXPR |
11053 | && TREE_CODE (arg1) == REAL_CST | |
11054 | && TREE_CODE (TREE_OPERAND (arg0, 0)) == REAL_CST) | |
11055 | { | |
11056 | tree tem = const_binop (MULT_EXPR, TREE_OPERAND (arg0, 0), | |
43a5d30b | 11057 | arg1); |
0aee4751 | 11058 | if (tem) |
db3927fb | 11059 | return fold_build2_loc (loc, RDIV_EXPR, type, tem, |
7f20a5b7 | 11060 | TREE_OPERAND (arg0, 1)); |
0aee4751 KH |
11061 | } |
11062 | ||
11063 | /* Strip sign operations from X in X*X, i.e. -Y*-Y -> Y*Y. */ | |
11064 | if (operand_equal_p (arg0, arg1, 0)) | |
11065 | { | |
11066 | tree tem = fold_strip_sign_ops (arg0); | |
11067 | if (tem != NULL_TREE) | |
11068 | { | |
db3927fb AH |
11069 | tem = fold_convert_loc (loc, type, tem); |
11070 | return fold_build2_loc (loc, MULT_EXPR, type, tem, tem); | |
0aee4751 KH |
11071 | } |
11072 | } | |
11073 | ||
9f539671 | 11074 | /* Fold z * +-I to __complex__ (-+__imag z, +-__real z). |
d1ad84c2 | 11075 | This is not the same for NaNs or if signed zeros are |
9f539671 RG |
11076 | involved. */ |
11077 | if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0))) | |
11078 | && !HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg0))) | |
11079 | && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0)) | |
11080 | && TREE_CODE (arg1) == COMPLEX_CST | |
11081 | && real_zerop (TREE_REALPART (arg1))) | |
11082 | { | |
11083 | tree rtype = TREE_TYPE (TREE_TYPE (arg0)); | |
11084 | if (real_onep (TREE_IMAGPART (arg1))) | |
db3927fb AH |
11085 | return |
11086 | fold_build2_loc (loc, COMPLEX_EXPR, type, | |
11087 | negate_expr (fold_build1_loc (loc, IMAGPART_EXPR, | |
11088 | rtype, arg0)), | |
11089 | fold_build1_loc (loc, REALPART_EXPR, rtype, arg0)); | |
9f539671 | 11090 | else if (real_minus_onep (TREE_IMAGPART (arg1))) |
db3927fb AH |
11091 | return |
11092 | fold_build2_loc (loc, COMPLEX_EXPR, type, | |
11093 | fold_build1_loc (loc, IMAGPART_EXPR, rtype, arg0), | |
11094 | negate_expr (fold_build1_loc (loc, REALPART_EXPR, | |
11095 | rtype, arg0))); | |
9f539671 RG |
11096 | } |
11097 | ||
99b25753 RS |
11098 | /* Optimize z * conj(z) for floating point complex numbers. |
11099 | Guarded by flag_unsafe_math_optimizations as non-finite | |
11100 | imaginary components don't produce scalar results. */ | |
11101 | if (flag_unsafe_math_optimizations | |
11102 | && TREE_CODE (arg0) == CONJ_EXPR | |
11103 | && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)) | |
db3927fb | 11104 | return fold_mult_zconjz (loc, type, arg1); |
99b25753 RS |
11105 | if (flag_unsafe_math_optimizations |
11106 | && TREE_CODE (arg1) == CONJ_EXPR | |
11107 | && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0)) | |
db3927fb | 11108 | return fold_mult_zconjz (loc, type, arg0); |
99b25753 | 11109 | |
0aee4751 KH |
11110 | if (flag_unsafe_math_optimizations) |
11111 | { | |
11112 | enum built_in_function fcode0 = builtin_mathfn_code (arg0); | |
11113 | enum built_in_function fcode1 = builtin_mathfn_code (arg1); | |
11114 | ||
11115 | /* Optimizations of root(...)*root(...). */ | |
11116 | if (fcode0 == fcode1 && BUILTIN_ROOT_P (fcode0)) | |
11117 | { | |
5039610b SL |
11118 | tree rootfn, arg; |
11119 | tree arg00 = CALL_EXPR_ARG (arg0, 0); | |
11120 | tree arg10 = CALL_EXPR_ARG (arg1, 0); | |
0aee4751 KH |
11121 | |
11122 | /* Optimize sqrt(x)*sqrt(x) as x. */ | |
11123 | if (BUILTIN_SQRT_P (fcode0) | |
11124 | && operand_equal_p (arg00, arg10, 0) | |
11125 | && ! HONOR_SNANS (TYPE_MODE (type))) | |
11126 | return arg00; | |
11127 | ||
11128 | /* Optimize root(x)*root(y) as root(x*y). */ | |
5039610b | 11129 | rootfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0); |
db3927fb AH |
11130 | arg = fold_build2_loc (loc, MULT_EXPR, type, arg00, arg10); |
11131 | return build_call_expr_loc (loc, rootfn, 1, arg); | |
0aee4751 KH |
11132 | } |
11133 | ||
11134 | /* Optimize expN(x)*expN(y) as expN(x+y). */ | |
11135 | if (fcode0 == fcode1 && BUILTIN_EXPONENT_P (fcode0)) | |
11136 | { | |
5039610b | 11137 | tree expfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0); |
db3927fb | 11138 | tree arg = fold_build2_loc (loc, PLUS_EXPR, type, |
5039610b SL |
11139 | CALL_EXPR_ARG (arg0, 0), |
11140 | CALL_EXPR_ARG (arg1, 0)); | |
db3927fb | 11141 | return build_call_expr_loc (loc, expfn, 1, arg); |
0aee4751 KH |
11142 | } |
11143 | ||
11144 | /* Optimizations of pow(...)*pow(...). */ | |
11145 | if ((fcode0 == BUILT_IN_POW && fcode1 == BUILT_IN_POW) | |
11146 | || (fcode0 == BUILT_IN_POWF && fcode1 == BUILT_IN_POWF) | |
11147 | || (fcode0 == BUILT_IN_POWL && fcode1 == BUILT_IN_POWL)) | |
11148 | { | |
5039610b SL |
11149 | tree arg00 = CALL_EXPR_ARG (arg0, 0); |
11150 | tree arg01 = CALL_EXPR_ARG (arg0, 1); | |
11151 | tree arg10 = CALL_EXPR_ARG (arg1, 0); | |
11152 | tree arg11 = CALL_EXPR_ARG (arg1, 1); | |
0aee4751 KH |
11153 | |
11154 | /* Optimize pow(x,y)*pow(z,y) as pow(x*z,y). */ | |
11155 | if (operand_equal_p (arg01, arg11, 0)) | |
11156 | { | |
5039610b | 11157 | tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0); |
db3927fb AH |
11158 | tree arg = fold_build2_loc (loc, MULT_EXPR, type, |
11159 | arg00, arg10); | |
11160 | return build_call_expr_loc (loc, powfn, 2, arg, arg01); | |
0aee4751 KH |
11161 | } |
11162 | ||
11163 | /* Optimize pow(x,y)*pow(x,z) as pow(x,y+z). */ | |
11164 | if (operand_equal_p (arg00, arg10, 0)) | |
11165 | { | |
5039610b | 11166 | tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0); |
db3927fb AH |
11167 | tree arg = fold_build2_loc (loc, PLUS_EXPR, type, |
11168 | arg01, arg11); | |
11169 | return build_call_expr_loc (loc, powfn, 2, arg00, arg); | |
0aee4751 KH |
11170 | } |
11171 | } | |
11172 | ||
11173 | /* Optimize tan(x)*cos(x) as sin(x). */ | |
11174 | if (((fcode0 == BUILT_IN_TAN && fcode1 == BUILT_IN_COS) | |
11175 | || (fcode0 == BUILT_IN_TANF && fcode1 == BUILT_IN_COSF) | |
11176 | || (fcode0 == BUILT_IN_TANL && fcode1 == BUILT_IN_COSL) | |
11177 | || (fcode0 == BUILT_IN_COS && fcode1 == BUILT_IN_TAN) | |
11178 | || (fcode0 == BUILT_IN_COSF && fcode1 == BUILT_IN_TANF) | |
11179 | || (fcode0 == BUILT_IN_COSL && fcode1 == BUILT_IN_TANL)) | |
5039610b SL |
11180 | && operand_equal_p (CALL_EXPR_ARG (arg0, 0), |
11181 | CALL_EXPR_ARG (arg1, 0), 0)) | |
0aee4751 KH |
11182 | { |
11183 | tree sinfn = mathfn_built_in (type, BUILT_IN_SIN); | |
11184 | ||
11185 | if (sinfn != NULL_TREE) | |
db3927fb AH |
11186 | return build_call_expr_loc (loc, sinfn, 1, |
11187 | CALL_EXPR_ARG (arg0, 0)); | |
0aee4751 KH |
11188 | } |
11189 | ||
11190 | /* Optimize x*pow(x,c) as pow(x,c+1). */ | |
11191 | if (fcode1 == BUILT_IN_POW | |
11192 | || fcode1 == BUILT_IN_POWF | |
11193 | || fcode1 == BUILT_IN_POWL) | |
11194 | { | |
5039610b SL |
11195 | tree arg10 = CALL_EXPR_ARG (arg1, 0); |
11196 | tree arg11 = CALL_EXPR_ARG (arg1, 1); | |
0aee4751 | 11197 | if (TREE_CODE (arg11) == REAL_CST |
455f14dd | 11198 | && !TREE_OVERFLOW (arg11) |
0aee4751 KH |
11199 | && operand_equal_p (arg0, arg10, 0)) |
11200 | { | |
5039610b | 11201 | tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg1), 0); |
0aee4751 | 11202 | REAL_VALUE_TYPE c; |
5039610b | 11203 | tree arg; |
0aee4751 KH |
11204 | |
11205 | c = TREE_REAL_CST (arg11); | |
11206 | real_arithmetic (&c, PLUS_EXPR, &c, &dconst1); | |
11207 | arg = build_real (type, c); | |
db3927fb | 11208 | return build_call_expr_loc (loc, powfn, 2, arg0, arg); |
0aee4751 KH |
11209 | } |
11210 | } | |
11211 | ||
11212 | /* Optimize pow(x,c)*x as pow(x,c+1). */ | |
11213 | if (fcode0 == BUILT_IN_POW | |
11214 | || fcode0 == BUILT_IN_POWF | |
11215 | || fcode0 == BUILT_IN_POWL) | |
11216 | { | |
5039610b SL |
11217 | tree arg00 = CALL_EXPR_ARG (arg0, 0); |
11218 | tree arg01 = CALL_EXPR_ARG (arg0, 1); | |
0aee4751 | 11219 | if (TREE_CODE (arg01) == REAL_CST |
455f14dd | 11220 | && !TREE_OVERFLOW (arg01) |
0aee4751 KH |
11221 | && operand_equal_p (arg1, arg00, 0)) |
11222 | { | |
5039610b | 11223 | tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0); |
0aee4751 | 11224 | REAL_VALUE_TYPE c; |
5039610b | 11225 | tree arg; |
0aee4751 KH |
11226 | |
11227 | c = TREE_REAL_CST (arg01); | |
11228 | real_arithmetic (&c, PLUS_EXPR, &c, &dconst1); | |
11229 | arg = build_real (type, c); | |
db3927fb | 11230 | return build_call_expr_loc (loc, powfn, 2, arg1, arg); |
0aee4751 KH |
11231 | } |
11232 | } | |
11233 | ||
4d8b88e8 | 11234 | /* Canonicalize x*x as pow(x,2.0), which is expanded as x*x. */ |
7871eee3 | 11235 | if (!in_gimple_form |
4d8b88e8 | 11236 | && optimize |
0aee4751 KH |
11237 | && operand_equal_p (arg0, arg1, 0)) |
11238 | { | |
11239 | tree powfn = mathfn_built_in (type, BUILT_IN_POW); | |
11240 | ||
11241 | if (powfn) | |
11242 | { | |
11243 | tree arg = build_real (type, dconst2); | |
db3927fb | 11244 | return build_call_expr_loc (loc, powfn, 2, arg0, arg); |
0aee4751 KH |
11245 | } |
11246 | } | |
11247 | } | |
11248 | } | |
11249 | goto associate; | |
11250 | ||
11251 | case BIT_IOR_EXPR: | |
11252 | bit_ior: | |
11253 | if (integer_all_onesp (arg1)) | |
db3927fb | 11254 | return omit_one_operand_loc (loc, type, arg1, arg0); |
0aee4751 | 11255 | if (integer_zerop (arg1)) |
db3927fb | 11256 | return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0)); |
0aee4751 | 11257 | if (operand_equal_p (arg0, arg1, 0)) |
db3927fb | 11258 | return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0)); |
0aee4751 KH |
11259 | |
11260 | /* ~X | X is -1. */ | |
11261 | if (TREE_CODE (arg0) == BIT_NOT_EXPR | |
11262 | && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)) | |
11263 | { | |
e8160c9a | 11264 | t1 = build_zero_cst (type); |
db3927fb AH |
11265 | t1 = fold_unary_loc (loc, BIT_NOT_EXPR, type, t1); |
11266 | return omit_one_operand_loc (loc, type, t1, arg1); | |
0aee4751 KH |
11267 | } |
11268 | ||
11269 | /* X | ~X is -1. */ | |
11270 | if (TREE_CODE (arg1) == BIT_NOT_EXPR | |
11271 | && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0)) | |
11272 | { | |
e8160c9a | 11273 | t1 = build_zero_cst (type); |
db3927fb AH |
11274 | t1 = fold_unary_loc (loc, BIT_NOT_EXPR, type, t1); |
11275 | return omit_one_operand_loc (loc, type, t1, arg0); | |
0aee4751 KH |
11276 | } |
11277 | ||
840992bd RS |
11278 | /* Canonicalize (X & C1) | C2. */ |
11279 | if (TREE_CODE (arg0) == BIT_AND_EXPR | |
11280 | && TREE_CODE (arg1) == INTEGER_CST | |
11281 | && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST) | |
11282 | { | |
5a226e0a | 11283 | double_int c1, c2, c3, msk; |
517ddae9 | 11284 | int width = TYPE_PRECISION (type), w; |
e3d3cfb4 KT |
11285 | bool try_simplify = true; |
11286 | ||
5a226e0a JJ |
11287 | c1 = tree_to_double_int (TREE_OPERAND (arg0, 1)); |
11288 | c2 = tree_to_double_int (arg1); | |
840992bd RS |
11289 | |
11290 | /* If (C1&C2) == C1, then (X&C1)|C2 becomes (X,C2). */ | |
27bcd47c | 11291 | if ((c1 & c2) == c1) |
db3927fb | 11292 | return omit_one_operand_loc (loc, type, arg1, |
5a226e0a | 11293 | TREE_OPERAND (arg0, 0)); |
840992bd | 11294 | |
27bcd47c | 11295 | msk = double_int::mask (width); |
840992bd RS |
11296 | |
11297 | /* If (C1|C2) == ~0 then (X&C1)|C2 becomes X|C2. */ | |
27bcd47c | 11298 | if (msk.and_not (c1 | c2).is_zero ()) |
db3927fb | 11299 | return fold_build2_loc (loc, BIT_IOR_EXPR, type, |
5a226e0a | 11300 | TREE_OPERAND (arg0, 0), arg1); |
840992bd | 11301 | |
517ddae9 JJ |
11302 | /* Minimize the number of bits set in C1, i.e. C1 := C1 & ~C2, |
11303 | unless (C1 & ~C2) | (C2 & C3) for some C3 is a mask of some | |
11304 | mode which allows further optimizations. */ | |
27bcd47c LC |
11305 | c1 &= msk; |
11306 | c2 &= msk; | |
11307 | c3 = c1.and_not (c2); | |
517ddae9 JJ |
11308 | for (w = BITS_PER_UNIT; |
11309 | w <= width && w <= HOST_BITS_PER_WIDE_INT; | |
11310 | w <<= 1) | |
11311 | { | |
11312 | unsigned HOST_WIDE_INT mask | |
0cadbfaa | 11313 | = HOST_WIDE_INT_M1U >> (HOST_BITS_PER_WIDE_INT - w); |
5a226e0a JJ |
11314 | if (((c1.low | c2.low) & mask) == mask |
11315 | && (c1.low & ~mask) == 0 && c1.high == 0) | |
517ddae9 | 11316 | { |
27bcd47c | 11317 | c3 = double_int::from_uhwi (mask); |
517ddae9 JJ |
11318 | break; |
11319 | } | |
11320 | } | |
e3d3cfb4 KT |
11321 | |
11322 | /* If X is a tree of the form (Y * K1) & K2, this might conflict | |
11323 | with that optimization from the BIT_AND_EXPR optimizations. | |
11324 | This could end up in an infinite recursion. */ | |
11325 | if (TREE_CODE (TREE_OPERAND (arg0, 0)) == MULT_EXPR | |
11326 | && TREE_CODE (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1)) | |
11327 | == INTEGER_CST) | |
11328 | { | |
11329 | tree t = TREE_OPERAND (TREE_OPERAND (arg0, 0), 1); | |
11330 | double_int masked = mask_with_tz (type, c3, tree_to_double_int (t)); | |
11331 | ||
11332 | try_simplify = (masked != c1); | |
11333 | } | |
11334 | ||
11335 | if (try_simplify && c3 != c1) | |
db3927fb | 11336 | return fold_build2_loc (loc, BIT_IOR_EXPR, type, |
5a226e0a JJ |
11337 | fold_build2_loc (loc, BIT_AND_EXPR, type, |
11338 | TREE_OPERAND (arg0, 0), | |
11339 | double_int_to_tree (type, | |
11340 | c3)), | |
11341 | arg1); | |
840992bd RS |
11342 | } |
11343 | ||
03bebcac RS |
11344 | /* (X & Y) | Y is (X, Y). */ |
11345 | if (TREE_CODE (arg0) == BIT_AND_EXPR | |
11346 | && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0)) | |
db3927fb | 11347 | return omit_one_operand_loc (loc, type, arg1, TREE_OPERAND (arg0, 0)); |
03bebcac RS |
11348 | /* (X & Y) | X is (Y, X). */ |
11349 | if (TREE_CODE (arg0) == BIT_AND_EXPR | |
11350 | && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0) | |
11351 | && reorder_operands_p (TREE_OPERAND (arg0, 1), arg1)) | |
db3927fb | 11352 | return omit_one_operand_loc (loc, type, arg1, TREE_OPERAND (arg0, 1)); |
03bebcac RS |
11353 | /* X | (X & Y) is (Y, X). */ |
11354 | if (TREE_CODE (arg1) == BIT_AND_EXPR | |
11355 | && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0) | |
11356 | && reorder_operands_p (arg0, TREE_OPERAND (arg1, 1))) | |
db3927fb | 11357 | return omit_one_operand_loc (loc, type, arg0, TREE_OPERAND (arg1, 1)); |
03bebcac RS |
11358 | /* X | (Y & X) is (Y, X). */ |
11359 | if (TREE_CODE (arg1) == BIT_AND_EXPR | |
11360 | && operand_equal_p (arg0, TREE_OPERAND (arg1, 1), 0) | |
11361 | && reorder_operands_p (arg0, TREE_OPERAND (arg1, 0))) | |
db3927fb | 11362 | return omit_one_operand_loc (loc, type, arg0, TREE_OPERAND (arg1, 0)); |
03bebcac | 11363 | |
583722ee KT |
11364 | /* (X & ~Y) | (~X & Y) is X ^ Y */ |
11365 | if (TREE_CODE (arg0) == BIT_AND_EXPR | |
11366 | && TREE_CODE (arg1) == BIT_AND_EXPR) | |
11367 | { | |
11368 | tree a0, a1, l0, l1, n0, n1; | |
11369 | ||
11370 | a0 = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 0)); | |
11371 | a1 = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 1)); | |
11372 | ||
11373 | l0 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0)); | |
11374 | l1 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 1)); | |
11375 | ||
11376 | n0 = fold_build1_loc (loc, BIT_NOT_EXPR, type, l0); | |
11377 | n1 = fold_build1_loc (loc, BIT_NOT_EXPR, type, l1); | |
11378 | ||
11379 | if ((operand_equal_p (n0, a0, 0) | |
11380 | && operand_equal_p (n1, a1, 0)) | |
11381 | || (operand_equal_p (n0, a1, 0) | |
11382 | && operand_equal_p (n1, a0, 0))) | |
11383 | return fold_build2_loc (loc, BIT_XOR_EXPR, type, l0, n1); | |
11384 | } | |
11385 | ||
db3927fb | 11386 | t1 = distribute_bit_expr (loc, code, type, arg0, arg1); |
0aee4751 KH |
11387 | if (t1 != NULL_TREE) |
11388 | return t1; | |
11389 | ||
11390 | /* Convert (or (not arg0) (not arg1)) to (not (and (arg0) (arg1))). | |
11391 | ||
11392 | This results in more efficient code for machines without a NAND | |
11393 | instruction. Combine will canonicalize to the first form | |
11394 | which will allow use of NAND instructions provided by the | |
11395 | backend if they exist. */ | |
11396 | if (TREE_CODE (arg0) == BIT_NOT_EXPR | |
11397 | && TREE_CODE (arg1) == BIT_NOT_EXPR) | |
11398 | { | |
db3927fb AH |
11399 | return |
11400 | fold_build1_loc (loc, BIT_NOT_EXPR, type, | |
11401 | build2 (BIT_AND_EXPR, type, | |
11402 | fold_convert_loc (loc, type, | |
11403 | TREE_OPERAND (arg0, 0)), | |
11404 | fold_convert_loc (loc, type, | |
11405 | TREE_OPERAND (arg1, 0)))); | |
0aee4751 KH |
11406 | } |
11407 | ||
11408 | /* See if this can be simplified into a rotate first. If that | |
11409 | is unsuccessful continue in the association code. */ | |
11410 | goto bit_rotate; | |
11411 | ||
11412 | case BIT_XOR_EXPR: | |
11413 | if (integer_zerop (arg1)) | |
db3927fb | 11414 | return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0)); |
0aee4751 | 11415 | if (integer_all_onesp (arg1)) |
db3927fb | 11416 | return fold_build1_loc (loc, BIT_NOT_EXPR, type, op0); |
0aee4751 | 11417 | if (operand_equal_p (arg0, arg1, 0)) |
db3927fb | 11418 | return omit_one_operand_loc (loc, type, integer_zero_node, arg0); |
0aee4751 KH |
11419 | |
11420 | /* ~X ^ X is -1. */ | |
11421 | if (TREE_CODE (arg0) == BIT_NOT_EXPR | |
11422 | && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)) | |
11423 | { | |
e8160c9a | 11424 | t1 = build_zero_cst (type); |
db3927fb AH |
11425 | t1 = fold_unary_loc (loc, BIT_NOT_EXPR, type, t1); |
11426 | return omit_one_operand_loc (loc, type, t1, arg1); | |
0aee4751 KH |
11427 | } |
11428 | ||
11429 | /* X ^ ~X is -1. */ | |
11430 | if (TREE_CODE (arg1) == BIT_NOT_EXPR | |
11431 | && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0)) | |
11432 | { | |
e8160c9a | 11433 | t1 = build_zero_cst (type); |
db3927fb AH |
11434 | t1 = fold_unary_loc (loc, BIT_NOT_EXPR, type, t1); |
11435 | return omit_one_operand_loc (loc, type, t1, arg0); | |
0aee4751 KH |
11436 | } |
11437 | ||
11438 | /* If we are XORing two BIT_AND_EXPR's, both of which are and'ing | |
11439 | with a constant, and the two constants have no bits in common, | |
11440 | we should treat this as a BIT_IOR_EXPR since this may produce more | |
11441 | simplifications. */ | |
11442 | if (TREE_CODE (arg0) == BIT_AND_EXPR | |
11443 | && TREE_CODE (arg1) == BIT_AND_EXPR | |
11444 | && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST | |
11445 | && TREE_CODE (TREE_OPERAND (arg1, 1)) == INTEGER_CST | |
11446 | && integer_zerop (const_binop (BIT_AND_EXPR, | |
11447 | TREE_OPERAND (arg0, 1), | |
43a5d30b | 11448 | TREE_OPERAND (arg1, 1)))) |
0aee4751 KH |
11449 | { |
11450 | code = BIT_IOR_EXPR; | |
11451 | goto bit_ior; | |
11452 | } | |
11453 | ||
9d24eb54 AP |
11454 | /* (X | Y) ^ X -> Y & ~ X*/ |
11455 | if (TREE_CODE (arg0) == BIT_IOR_EXPR | |
11456 | && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)) | |
11457 | { | |
11458 | tree t2 = TREE_OPERAND (arg0, 1); | |
db3927fb | 11459 | t1 = fold_build1_loc (loc, BIT_NOT_EXPR, TREE_TYPE (arg1), |
9d24eb54 | 11460 | arg1); |
db3927fb AH |
11461 | t1 = fold_build2_loc (loc, BIT_AND_EXPR, type, |
11462 | fold_convert_loc (loc, type, t2), | |
11463 | fold_convert_loc (loc, type, t1)); | |
9d24eb54 AP |
11464 | return t1; |
11465 | } | |
11466 | ||
11467 | /* (Y | X) ^ X -> Y & ~ X*/ | |
11468 | if (TREE_CODE (arg0) == BIT_IOR_EXPR | |
11469 | && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0)) | |
11470 | { | |
11471 | tree t2 = TREE_OPERAND (arg0, 0); | |
db3927fb | 11472 | t1 = fold_build1_loc (loc, BIT_NOT_EXPR, TREE_TYPE (arg1), |
9d24eb54 | 11473 | arg1); |
db3927fb AH |
11474 | t1 = fold_build2_loc (loc, BIT_AND_EXPR, type, |
11475 | fold_convert_loc (loc, type, t2), | |
11476 | fold_convert_loc (loc, type, t1)); | |
9d24eb54 AP |
11477 | return t1; |
11478 | } | |
11479 | ||
11480 | /* X ^ (X | Y) -> Y & ~ X*/ | |
11481 | if (TREE_CODE (arg1) == BIT_IOR_EXPR | |
11482 | && operand_equal_p (TREE_OPERAND (arg1, 0), arg0, 0)) | |
11483 | { | |
11484 | tree t2 = TREE_OPERAND (arg1, 1); | |
db3927fb | 11485 | t1 = fold_build1_loc (loc, BIT_NOT_EXPR, TREE_TYPE (arg0), |
9d24eb54 | 11486 | arg0); |
db3927fb AH |
11487 | t1 = fold_build2_loc (loc, BIT_AND_EXPR, type, |
11488 | fold_convert_loc (loc, type, t2), | |
11489 | fold_convert_loc (loc, type, t1)); | |
9d24eb54 AP |
11490 | return t1; |
11491 | } | |
11492 | ||
11493 | /* X ^ (Y | X) -> Y & ~ X*/ | |
11494 | if (TREE_CODE (arg1) == BIT_IOR_EXPR | |
11495 | && operand_equal_p (TREE_OPERAND (arg1, 1), arg0, 0)) | |
11496 | { | |
11497 | tree t2 = TREE_OPERAND (arg1, 0); | |
db3927fb | 11498 | t1 = fold_build1_loc (loc, BIT_NOT_EXPR, TREE_TYPE (arg0), |
9d24eb54 | 11499 | arg0); |
db3927fb AH |
11500 | t1 = fold_build2_loc (loc, BIT_AND_EXPR, type, |
11501 | fold_convert_loc (loc, type, t2), | |
11502 | fold_convert_loc (loc, type, t1)); | |
9d24eb54 AP |
11503 | return t1; |
11504 | } | |
b8698a0f | 11505 | |
33ab6245 JM |
11506 | /* Convert ~X ^ ~Y to X ^ Y. */ |
11507 | if (TREE_CODE (arg0) == BIT_NOT_EXPR | |
11508 | && TREE_CODE (arg1) == BIT_NOT_EXPR) | |
db3927fb AH |
11509 | return fold_build2_loc (loc, code, type, |
11510 | fold_convert_loc (loc, type, | |
11511 | TREE_OPERAND (arg0, 0)), | |
11512 | fold_convert_loc (loc, type, | |
11513 | TREE_OPERAND (arg1, 0))); | |
33ab6245 | 11514 | |
f8ed9a1c RS |
11515 | /* Convert ~X ^ C to X ^ ~C. */ |
11516 | if (TREE_CODE (arg0) == BIT_NOT_EXPR | |
11517 | && TREE_CODE (arg1) == INTEGER_CST) | |
db3927fb AH |
11518 | return fold_build2_loc (loc, code, type, |
11519 | fold_convert_loc (loc, type, | |
11520 | TREE_OPERAND (arg0, 0)), | |
11521 | fold_build1_loc (loc, BIT_NOT_EXPR, type, arg1)); | |
f8ed9a1c | 11522 | |
cef65eaa RS |
11523 | /* Fold (X & 1) ^ 1 as (X & 1) == 0. */ |
11524 | if (TREE_CODE (arg0) == BIT_AND_EXPR | |
11525 | && integer_onep (TREE_OPERAND (arg0, 1)) | |
11526 | && integer_onep (arg1)) | |
db3927fb | 11527 | return fold_build2_loc (loc, EQ_EXPR, type, arg0, |
27edb974 | 11528 | build_zero_cst (TREE_TYPE (arg0))); |
cef65eaa | 11529 | |
dd2c62dc RS |
11530 | /* Fold (X & Y) ^ Y as ~X & Y. */ |
11531 | if (TREE_CODE (arg0) == BIT_AND_EXPR | |
11532 | && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0)) | |
11533 | { | |
db3927fb | 11534 | tem = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0)); |
b8698a0f | 11535 | return fold_build2_loc (loc, BIT_AND_EXPR, type, |
db3927fb AH |
11536 | fold_build1_loc (loc, BIT_NOT_EXPR, type, tem), |
11537 | fold_convert_loc (loc, type, arg1)); | |
dd2c62dc RS |
11538 | } |
11539 | /* Fold (X & Y) ^ X as ~Y & X. */ | |
11540 | if (TREE_CODE (arg0) == BIT_AND_EXPR | |
11541 | && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0) | |
11542 | && reorder_operands_p (TREE_OPERAND (arg0, 1), arg1)) | |
11543 | { | |
db3927fb AH |
11544 | tem = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 1)); |
11545 | return fold_build2_loc (loc, BIT_AND_EXPR, type, | |
11546 | fold_build1_loc (loc, BIT_NOT_EXPR, type, tem), | |
11547 | fold_convert_loc (loc, type, arg1)); | |
dd2c62dc RS |
11548 | } |
11549 | /* Fold X ^ (X & Y) as X & ~Y. */ | |
11550 | if (TREE_CODE (arg1) == BIT_AND_EXPR | |
11551 | && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0)) | |
11552 | { | |
db3927fb AH |
11553 | tem = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 1)); |
11554 | return fold_build2_loc (loc, BIT_AND_EXPR, type, | |
11555 | fold_convert_loc (loc, type, arg0), | |
11556 | fold_build1_loc (loc, BIT_NOT_EXPR, type, tem)); | |
dd2c62dc RS |
11557 | } |
11558 | /* Fold X ^ (Y & X) as ~Y & X. */ | |
11559 | if (TREE_CODE (arg1) == BIT_AND_EXPR | |
11560 | && operand_equal_p (arg0, TREE_OPERAND (arg1, 1), 0) | |
11561 | && reorder_operands_p (arg0, TREE_OPERAND (arg1, 0))) | |
11562 | { | |
db3927fb AH |
11563 | tem = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 0)); |
11564 | return fold_build2_loc (loc, BIT_AND_EXPR, type, | |
11565 | fold_build1_loc (loc, BIT_NOT_EXPR, type, tem), | |
11566 | fold_convert_loc (loc, type, arg0)); | |
dd2c62dc RS |
11567 | } |
11568 | ||
0aee4751 KH |
11569 | /* See if this can be simplified into a rotate first. If that |
11570 | is unsuccessful continue in the association code. */ | |
11571 | goto bit_rotate; | |
11572 | ||
11573 | case BIT_AND_EXPR: | |
11574 | if (integer_all_onesp (arg1)) | |
db3927fb | 11575 | return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0)); |
0aee4751 | 11576 | if (integer_zerop (arg1)) |
db3927fb | 11577 | return omit_one_operand_loc (loc, type, arg1, arg0); |
0aee4751 | 11578 | if (operand_equal_p (arg0, arg1, 0)) |
db3927fb | 11579 | return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0)); |
0aee4751 | 11580 | |
a95015b6 KT |
11581 | /* ~X & X, (X == 0) & X, and !X & X are always zero. */ |
11582 | if ((TREE_CODE (arg0) == BIT_NOT_EXPR | |
11583 | || TREE_CODE (arg0) == TRUTH_NOT_EXPR | |
11584 | || (TREE_CODE (arg0) == EQ_EXPR | |
11585 | && integer_zerop (TREE_OPERAND (arg0, 1)))) | |
0aee4751 | 11586 | && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)) |
db3927fb | 11587 | return omit_one_operand_loc (loc, type, integer_zero_node, arg1); |
0aee4751 | 11588 | |
a95015b6 KT |
11589 | /* X & ~X , X & (X == 0), and X & !X are always zero. */ |
11590 | if ((TREE_CODE (arg1) == BIT_NOT_EXPR | |
11591 | || TREE_CODE (arg1) == TRUTH_NOT_EXPR | |
11592 | || (TREE_CODE (arg1) == EQ_EXPR | |
11593 | && integer_zerop (TREE_OPERAND (arg1, 1)))) | |
0aee4751 | 11594 | && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0)) |
db3927fb | 11595 | return omit_one_operand_loc (loc, type, integer_zero_node, arg0); |
0aee4751 | 11596 | |
840992bd RS |
11597 | /* Canonicalize (X | C1) & C2 as (X & C2) | (C1 & C2). */ |
11598 | if (TREE_CODE (arg0) == BIT_IOR_EXPR | |
11599 | && TREE_CODE (arg1) == INTEGER_CST | |
11600 | && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST) | |
8174836f | 11601 | { |
db3927fb AH |
11602 | tree tmp1 = fold_convert_loc (loc, type, arg1); |
11603 | tree tmp2 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0)); | |
11604 | tree tmp3 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 1)); | |
11605 | tmp2 = fold_build2_loc (loc, BIT_AND_EXPR, type, tmp2, tmp1); | |
11606 | tmp3 = fold_build2_loc (loc, BIT_AND_EXPR, type, tmp3, tmp1); | |
11607 | return | |
11608 | fold_convert_loc (loc, type, | |
11609 | fold_build2_loc (loc, BIT_IOR_EXPR, | |
11610 | type, tmp2, tmp3)); | |
8174836f | 11611 | } |
840992bd | 11612 | |
03bebcac RS |
11613 | /* (X | Y) & Y is (X, Y). */ |
11614 | if (TREE_CODE (arg0) == BIT_IOR_EXPR | |
11615 | && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0)) | |
db3927fb | 11616 | return omit_one_operand_loc (loc, type, arg1, TREE_OPERAND (arg0, 0)); |
03bebcac RS |
11617 | /* (X | Y) & X is (Y, X). */ |
11618 | if (TREE_CODE (arg0) == BIT_IOR_EXPR | |
11619 | && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0) | |
11620 | && reorder_operands_p (TREE_OPERAND (arg0, 1), arg1)) | |
db3927fb | 11621 | return omit_one_operand_loc (loc, type, arg1, TREE_OPERAND (arg0, 1)); |
03bebcac RS |
11622 | /* X & (X | Y) is (Y, X). */ |
11623 | if (TREE_CODE (arg1) == BIT_IOR_EXPR | |
11624 | && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0) | |
11625 | && reorder_operands_p (arg0, TREE_OPERAND (arg1, 1))) | |
db3927fb | 11626 | return omit_one_operand_loc (loc, type, arg0, TREE_OPERAND (arg1, 1)); |
03bebcac RS |
11627 | /* X & (Y | X) is (Y, X). */ |
11628 | if (TREE_CODE (arg1) == BIT_IOR_EXPR | |
11629 | && operand_equal_p (arg0, TREE_OPERAND (arg1, 1), 0) | |
11630 | && reorder_operands_p (arg0, TREE_OPERAND (arg1, 0))) | |
db3927fb | 11631 | return omit_one_operand_loc (loc, type, arg0, TREE_OPERAND (arg1, 0)); |
03bebcac | 11632 | |
cef65eaa RS |
11633 | /* Fold (X ^ 1) & 1 as (X & 1) == 0. */ |
11634 | if (TREE_CODE (arg0) == BIT_XOR_EXPR | |
11635 | && integer_onep (TREE_OPERAND (arg0, 1)) | |
11636 | && integer_onep (arg1)) | |
11637 | { | |
27edb974 | 11638 | tree tem2; |
cef65eaa | 11639 | tem = TREE_OPERAND (arg0, 0); |
27edb974 JJ |
11640 | tem2 = fold_convert_loc (loc, TREE_TYPE (tem), arg1); |
11641 | tem2 = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (tem), | |
11642 | tem, tem2); | |
11643 | return fold_build2_loc (loc, EQ_EXPR, type, tem2, | |
11644 | build_zero_cst (TREE_TYPE (tem))); | |
cef65eaa RS |
11645 | } |
11646 | /* Fold ~X & 1 as (X & 1) == 0. */ | |
11647 | if (TREE_CODE (arg0) == BIT_NOT_EXPR | |
11648 | && integer_onep (arg1)) | |
11649 | { | |
27edb974 | 11650 | tree tem2; |
cef65eaa | 11651 | tem = TREE_OPERAND (arg0, 0); |
27edb974 JJ |
11652 | tem2 = fold_convert_loc (loc, TREE_TYPE (tem), arg1); |
11653 | tem2 = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (tem), | |
11654 | tem, tem2); | |
11655 | return fold_build2_loc (loc, EQ_EXPR, type, tem2, | |
11656 | build_zero_cst (TREE_TYPE (tem))); | |
cef65eaa | 11657 | } |
a95015b6 KT |
11658 | /* Fold !X & 1 as X == 0. */ |
11659 | if (TREE_CODE (arg0) == TRUTH_NOT_EXPR | |
11660 | && integer_onep (arg1)) | |
11661 | { | |
11662 | tem = TREE_OPERAND (arg0, 0); | |
11663 | return fold_build2_loc (loc, EQ_EXPR, type, tem, | |
27edb974 | 11664 | build_zero_cst (TREE_TYPE (tem))); |
a95015b6 | 11665 | } |
cef65eaa | 11666 | |
dd2c62dc RS |
11667 | /* Fold (X ^ Y) & Y as ~X & Y. */ |
11668 | if (TREE_CODE (arg0) == BIT_XOR_EXPR | |
11669 | && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0)) | |
11670 | { | |
db3927fb | 11671 | tem = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0)); |
b8698a0f | 11672 | return fold_build2_loc (loc, BIT_AND_EXPR, type, |
db3927fb AH |
11673 | fold_build1_loc (loc, BIT_NOT_EXPR, type, tem), |
11674 | fold_convert_loc (loc, type, arg1)); | |
dd2c62dc RS |
11675 | } |
11676 | /* Fold (X ^ Y) & X as ~Y & X. */ | |
11677 | if (TREE_CODE (arg0) == BIT_XOR_EXPR | |
11678 | && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0) | |
11679 | && reorder_operands_p (TREE_OPERAND (arg0, 1), arg1)) | |
11680 | { | |
db3927fb AH |
11681 | tem = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 1)); |
11682 | return fold_build2_loc (loc, BIT_AND_EXPR, type, | |
11683 | fold_build1_loc (loc, BIT_NOT_EXPR, type, tem), | |
11684 | fold_convert_loc (loc, type, arg1)); | |
dd2c62dc RS |
11685 | } |
11686 | /* Fold X & (X ^ Y) as X & ~Y. */ | |
11687 | if (TREE_CODE (arg1) == BIT_XOR_EXPR | |
11688 | && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0)) | |
11689 | { | |
db3927fb AH |
11690 | tem = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 1)); |
11691 | return fold_build2_loc (loc, BIT_AND_EXPR, type, | |
11692 | fold_convert_loc (loc, type, arg0), | |
11693 | fold_build1_loc (loc, BIT_NOT_EXPR, type, tem)); | |
dd2c62dc RS |
11694 | } |
11695 | /* Fold X & (Y ^ X) as ~Y & X. */ | |
11696 | if (TREE_CODE (arg1) == BIT_XOR_EXPR | |
11697 | && operand_equal_p (arg0, TREE_OPERAND (arg1, 1), 0) | |
11698 | && reorder_operands_p (arg0, TREE_OPERAND (arg1, 0))) | |
11699 | { | |
db3927fb AH |
11700 | tem = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 0)); |
11701 | return fold_build2_loc (loc, BIT_AND_EXPR, type, | |
11702 | fold_build1_loc (loc, BIT_NOT_EXPR, type, tem), | |
11703 | fold_convert_loc (loc, type, arg0)); | |
dd2c62dc RS |
11704 | } |
11705 | ||
0c12cd5e RG |
11706 | /* Fold (X * Y) & -(1 << CST) to X * Y if Y is a constant |
11707 | multiple of 1 << CST. */ | |
11708 | if (TREE_CODE (arg1) == INTEGER_CST) | |
11709 | { | |
11710 | double_int cst1 = tree_to_double_int (arg1); | |
c3284718 RS |
11711 | double_int ncst1 = (-cst1).ext (TYPE_PRECISION (TREE_TYPE (arg1)), |
11712 | TYPE_UNSIGNED (TREE_TYPE (arg1))); | |
27bcd47c | 11713 | if ((cst1 & ncst1) == ncst1 |
0c12cd5e RG |
11714 | && multiple_of_p (type, arg0, |
11715 | double_int_to_tree (TREE_TYPE (arg1), ncst1))) | |
11716 | return fold_convert_loc (loc, type, arg0); | |
11717 | } | |
11718 | ||
ad9fc55a RG |
11719 | /* Fold (X * CST1) & CST2 to zero if we can, or drop known zero |
11720 | bits from CST2. */ | |
11721 | if (TREE_CODE (arg1) == INTEGER_CST | |
11722 | && TREE_CODE (arg0) == MULT_EXPR | |
11723 | && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST) | |
11724 | { | |
e3d3cfb4 KT |
11725 | double_int masked |
11726 | = mask_with_tz (type, tree_to_double_int (arg1), | |
11727 | tree_to_double_int (TREE_OPERAND (arg0, 1))); | |
11728 | ||
11729 | if (masked.is_zero ()) | |
11730 | return omit_two_operands_loc (loc, type, build_zero_cst (type), | |
11731 | arg0, arg1); | |
11732 | else if (masked != tree_to_double_int (arg1)) | |
11733 | return fold_build2_loc (loc, code, type, op0, | |
11734 | double_int_to_tree (type, masked)); | |
ad9fc55a RG |
11735 | } |
11736 | ||
140d4eff JJ |
11737 | /* For constants M and N, if M == (1LL << cst) - 1 && (N & M) == M, |
11738 | ((A & N) + B) & M -> (A + B) & M | |
11739 | Similarly if (N & M) == 0, | |
11740 | ((A | N) + B) & M -> (A + B) & M | |
11741 | and for - instead of + (or unary - instead of +) | |
11742 | and/or ^ instead of |. | |
11743 | If B is constant and (B & M) == 0, fold into A & M. */ | |
11744 | if (host_integerp (arg1, 1)) | |
11745 | { | |
11746 | unsigned HOST_WIDE_INT cst1 = tree_low_cst (arg1, 1); | |
11747 | if (~cst1 && (cst1 & (cst1 + 1)) == 0 | |
11748 | && INTEGRAL_TYPE_P (TREE_TYPE (arg0)) | |
11749 | && (TREE_CODE (arg0) == PLUS_EXPR | |
11750 | || TREE_CODE (arg0) == MINUS_EXPR | |
11751 | || TREE_CODE (arg0) == NEGATE_EXPR) | |
11752 | && (TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg0)) | |
11753 | || TREE_CODE (TREE_TYPE (arg0)) == INTEGER_TYPE)) | |
11754 | { | |
11755 | tree pmop[2]; | |
11756 | int which = 0; | |
11757 | unsigned HOST_WIDE_INT cst0; | |
11758 | ||
11759 | /* Now we know that arg0 is (C + D) or (C - D) or | |
11760 | -C and arg1 (M) is == (1LL << cst) - 1. | |
11761 | Store C into PMOP[0] and D into PMOP[1]. */ | |
11762 | pmop[0] = TREE_OPERAND (arg0, 0); | |
11763 | pmop[1] = NULL; | |
11764 | if (TREE_CODE (arg0) != NEGATE_EXPR) | |
11765 | { | |
11766 | pmop[1] = TREE_OPERAND (arg0, 1); | |
11767 | which = 1; | |
11768 | } | |
11769 | ||
11770 | if (!host_integerp (TYPE_MAX_VALUE (TREE_TYPE (arg0)), 1) | |
11771 | || (tree_low_cst (TYPE_MAX_VALUE (TREE_TYPE (arg0)), 1) | |
11772 | & cst1) != cst1) | |
11773 | which = -1; | |
11774 | ||
11775 | for (; which >= 0; which--) | |
11776 | switch (TREE_CODE (pmop[which])) | |
11777 | { | |
11778 | case BIT_AND_EXPR: | |
11779 | case BIT_IOR_EXPR: | |
11780 | case BIT_XOR_EXPR: | |
11781 | if (TREE_CODE (TREE_OPERAND (pmop[which], 1)) | |
11782 | != INTEGER_CST) | |
11783 | break; | |
11784 | /* tree_low_cst not used, because we don't care about | |
11785 | the upper bits. */ | |
11786 | cst0 = TREE_INT_CST_LOW (TREE_OPERAND (pmop[which], 1)); | |
11787 | cst0 &= cst1; | |
11788 | if (TREE_CODE (pmop[which]) == BIT_AND_EXPR) | |
11789 | { | |
11790 | if (cst0 != cst1) | |
11791 | break; | |
11792 | } | |
11793 | else if (cst0 != 0) | |
11794 | break; | |
11795 | /* If C or D is of the form (A & N) where | |
11796 | (N & M) == M, or of the form (A | N) or | |
11797 | (A ^ N) where (N & M) == 0, replace it with A. */ | |
11798 | pmop[which] = TREE_OPERAND (pmop[which], 0); | |
11799 | break; | |
11800 | case INTEGER_CST: | |
11801 | /* If C or D is a N where (N & M) == 0, it can be | |
11802 | omitted (assumed 0). */ | |
11803 | if ((TREE_CODE (arg0) == PLUS_EXPR | |
11804 | || (TREE_CODE (arg0) == MINUS_EXPR && which == 0)) | |
11805 | && (TREE_INT_CST_LOW (pmop[which]) & cst1) == 0) | |
11806 | pmop[which] = NULL; | |
11807 | break; | |
11808 | default: | |
11809 | break; | |
11810 | } | |
11811 | ||
11812 | /* Only build anything new if we optimized one or both arguments | |
11813 | above. */ | |
11814 | if (pmop[0] != TREE_OPERAND (arg0, 0) | |
11815 | || (TREE_CODE (arg0) != NEGATE_EXPR | |
11816 | && pmop[1] != TREE_OPERAND (arg0, 1))) | |
11817 | { | |
828fde80 | 11818 | tree utype = TREE_TYPE (arg0); |
140d4eff JJ |
11819 | if (! TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg0))) |
11820 | { | |
11821 | /* Perform the operations in a type that has defined | |
11822 | overflow behavior. */ | |
828fde80 | 11823 | utype = unsigned_type_for (TREE_TYPE (arg0)); |
140d4eff JJ |
11824 | if (pmop[0] != NULL) |
11825 | pmop[0] = fold_convert_loc (loc, utype, pmop[0]); | |
11826 | if (pmop[1] != NULL) | |
11827 | pmop[1] = fold_convert_loc (loc, utype, pmop[1]); | |
11828 | } | |
11829 | ||
11830 | if (TREE_CODE (arg0) == NEGATE_EXPR) | |
11831 | tem = fold_build1_loc (loc, NEGATE_EXPR, utype, pmop[0]); | |
11832 | else if (TREE_CODE (arg0) == PLUS_EXPR) | |
11833 | { | |
11834 | if (pmop[0] != NULL && pmop[1] != NULL) | |
11835 | tem = fold_build2_loc (loc, PLUS_EXPR, utype, | |
11836 | pmop[0], pmop[1]); | |
11837 | else if (pmop[0] != NULL) | |
11838 | tem = pmop[0]; | |
11839 | else if (pmop[1] != NULL) | |
11840 | tem = pmop[1]; | |
11841 | else | |
11842 | return build_int_cst (type, 0); | |
11843 | } | |
11844 | else if (pmop[0] == NULL) | |
11845 | tem = fold_build1_loc (loc, NEGATE_EXPR, utype, pmop[1]); | |
11846 | else | |
11847 | tem = fold_build2_loc (loc, MINUS_EXPR, utype, | |
11848 | pmop[0], pmop[1]); | |
11849 | /* TEM is now the new binary +, - or unary - replacement. */ | |
828fde80 JJ |
11850 | tem = fold_build2_loc (loc, BIT_AND_EXPR, utype, tem, |
11851 | fold_convert_loc (loc, utype, arg1)); | |
11852 | return fold_convert_loc (loc, type, tem); | |
140d4eff JJ |
11853 | } |
11854 | } | |
11855 | } | |
11856 | ||
db3927fb | 11857 | t1 = distribute_bit_expr (loc, code, type, arg0, arg1); |
0aee4751 KH |
11858 | if (t1 != NULL_TREE) |
11859 | return t1; | |
11860 | /* Simplify ((int)c & 0377) into (int)c, if c is unsigned char. */ | |
11861 | if (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg0) == NOP_EXPR | |
11862 | && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg0, 0)))) | |
11863 | { | |
a5e0cd1d | 11864 | prec = TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg0, 0))); |
0aee4751 KH |
11865 | |
11866 | if (prec < BITS_PER_WORD && prec < HOST_BITS_PER_WIDE_INT | |
11867 | && (~TREE_INT_CST_LOW (arg1) | |
11868 | & (((HOST_WIDE_INT) 1 << prec) - 1)) == 0) | |
db3927fb AH |
11869 | return |
11870 | fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0)); | |
0aee4751 KH |
11871 | } |
11872 | ||
11873 | /* Convert (and (not arg0) (not arg1)) to (not (or (arg0) (arg1))). | |
11874 | ||
11875 | This results in more efficient code for machines without a NOR | |
11876 | instruction. Combine will canonicalize to the first form | |
11877 | which will allow use of NOR instructions provided by the | |
11878 | backend if they exist. */ | |
11879 | if (TREE_CODE (arg0) == BIT_NOT_EXPR | |
11880 | && TREE_CODE (arg1) == BIT_NOT_EXPR) | |
11881 | { | |
db3927fb | 11882 | return fold_build1_loc (loc, BIT_NOT_EXPR, type, |
7f20a5b7 | 11883 | build2 (BIT_IOR_EXPR, type, |
db3927fb AH |
11884 | fold_convert_loc (loc, type, |
11885 | TREE_OPERAND (arg0, 0)), | |
11886 | fold_convert_loc (loc, type, | |
11887 | TREE_OPERAND (arg1, 0)))); | |
0aee4751 KH |
11888 | } |
11889 | ||
e5901cad OW |
11890 | /* If arg0 is derived from the address of an object or function, we may |
11891 | be able to fold this expression using the object or function's | |
11892 | alignment. */ | |
11893 | if (POINTER_TYPE_P (TREE_TYPE (arg0)) && host_integerp (arg1, 1)) | |
11894 | { | |
11895 | unsigned HOST_WIDE_INT modulus, residue; | |
11896 | unsigned HOST_WIDE_INT low = TREE_INT_CST_LOW (arg1); | |
11897 | ||
617f3897 MJ |
11898 | modulus = get_pointer_modulus_and_residue (arg0, &residue, |
11899 | integer_onep (arg1)); | |
e5901cad OW |
11900 | |
11901 | /* This works because modulus is a power of 2. If this weren't the | |
11902 | case, we'd have to replace it by its greatest power-of-2 | |
11903 | divisor: modulus & -modulus. */ | |
11904 | if (low < modulus) | |
11905 | return build_int_cst (type, residue & low); | |
11906 | } | |
11907 | ||
22164c3d JJ |
11908 | /* Fold (X << C1) & C2 into (X << C1) & (C2 | ((1 << C1) - 1)) |
11909 | (X >> C1) & C2 into (X >> C1) & (C2 | ~((type) -1 >> C1)) | |
11910 | if the new mask might be further optimized. */ | |
11911 | if ((TREE_CODE (arg0) == LSHIFT_EXPR | |
11912 | || TREE_CODE (arg0) == RSHIFT_EXPR) | |
11913 | && host_integerp (TREE_OPERAND (arg0, 1), 1) | |
11914 | && host_integerp (arg1, TYPE_UNSIGNED (TREE_TYPE (arg1))) | |
11915 | && tree_low_cst (TREE_OPERAND (arg0, 1), 1) | |
11916 | < TYPE_PRECISION (TREE_TYPE (arg0)) | |
11917 | && TYPE_PRECISION (TREE_TYPE (arg0)) <= HOST_BITS_PER_WIDE_INT | |
11918 | && tree_low_cst (TREE_OPERAND (arg0, 1), 1) > 0) | |
11919 | { | |
11920 | unsigned int shiftc = tree_low_cst (TREE_OPERAND (arg0, 1), 1); | |
11921 | unsigned HOST_WIDE_INT mask | |
11922 | = tree_low_cst (arg1, TYPE_UNSIGNED (TREE_TYPE (arg1))); | |
11923 | unsigned HOST_WIDE_INT newmask, zerobits = 0; | |
11924 | tree shift_type = TREE_TYPE (arg0); | |
11925 | ||
11926 | if (TREE_CODE (arg0) == LSHIFT_EXPR) | |
11927 | zerobits = ((((unsigned HOST_WIDE_INT) 1) << shiftc) - 1); | |
11928 | else if (TREE_CODE (arg0) == RSHIFT_EXPR | |
11929 | && TYPE_PRECISION (TREE_TYPE (arg0)) | |
11930 | == GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (arg0)))) | |
11931 | { | |
a5e0cd1d | 11932 | prec = TYPE_PRECISION (TREE_TYPE (arg0)); |
22164c3d JJ |
11933 | tree arg00 = TREE_OPERAND (arg0, 0); |
11934 | /* See if more bits can be proven as zero because of | |
11935 | zero extension. */ | |
11936 | if (TREE_CODE (arg00) == NOP_EXPR | |
11937 | && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg00, 0)))) | |
11938 | { | |
11939 | tree inner_type = TREE_TYPE (TREE_OPERAND (arg00, 0)); | |
11940 | if (TYPE_PRECISION (inner_type) | |
11941 | == GET_MODE_BITSIZE (TYPE_MODE (inner_type)) | |
11942 | && TYPE_PRECISION (inner_type) < prec) | |
11943 | { | |
11944 | prec = TYPE_PRECISION (inner_type); | |
11945 | /* See if we can shorten the right shift. */ | |
11946 | if (shiftc < prec) | |
11947 | shift_type = inner_type; | |
11948 | } | |
11949 | } | |
11950 | zerobits = ~(unsigned HOST_WIDE_INT) 0; | |
11951 | zerobits >>= HOST_BITS_PER_WIDE_INT - shiftc; | |
11952 | zerobits <<= prec - shiftc; | |
11953 | /* For arithmetic shift if sign bit could be set, zerobits | |
11954 | can contain actually sign bits, so no transformation is | |
11955 | possible, unless MASK masks them all away. In that | |
11956 | case the shift needs to be converted into logical shift. */ | |
11957 | if (!TYPE_UNSIGNED (TREE_TYPE (arg0)) | |
11958 | && prec == TYPE_PRECISION (TREE_TYPE (arg0))) | |
11959 | { | |
11960 | if ((mask & zerobits) == 0) | |
11961 | shift_type = unsigned_type_for (TREE_TYPE (arg0)); | |
11962 | else | |
11963 | zerobits = 0; | |
11964 | } | |
11965 | } | |
11966 | ||
11967 | /* ((X << 16) & 0xff00) is (X, 0). */ | |
11968 | if ((mask & zerobits) == mask) | |
db3927fb AH |
11969 | return omit_one_operand_loc (loc, type, |
11970 | build_int_cst (type, 0), arg0); | |
22164c3d JJ |
11971 | |
11972 | newmask = mask | zerobits; | |
11973 | if (newmask != mask && (newmask & (newmask + 1)) == 0) | |
11974 | { | |
22164c3d JJ |
11975 | /* Only do the transformation if NEWMASK is some integer |
11976 | mode's mask. */ | |
11977 | for (prec = BITS_PER_UNIT; | |
11978 | prec < HOST_BITS_PER_WIDE_INT; prec <<= 1) | |
11979 | if (newmask == (((unsigned HOST_WIDE_INT) 1) << prec) - 1) | |
11980 | break; | |
11981 | if (prec < HOST_BITS_PER_WIDE_INT | |
11982 | || newmask == ~(unsigned HOST_WIDE_INT) 0) | |
11983 | { | |
776248b8 JJ |
11984 | tree newmaskt; |
11985 | ||
22164c3d JJ |
11986 | if (shift_type != TREE_TYPE (arg0)) |
11987 | { | |
db3927fb AH |
11988 | tem = fold_build2_loc (loc, TREE_CODE (arg0), shift_type, |
11989 | fold_convert_loc (loc, shift_type, | |
11990 | TREE_OPERAND (arg0, 0)), | |
22164c3d | 11991 | TREE_OPERAND (arg0, 1)); |
db3927fb | 11992 | tem = fold_convert_loc (loc, type, tem); |
22164c3d JJ |
11993 | } |
11994 | else | |
11995 | tem = op0; | |
776248b8 JJ |
11996 | newmaskt = build_int_cst_type (TREE_TYPE (op1), newmask); |
11997 | if (!tree_int_cst_equal (newmaskt, arg1)) | |
db3927fb | 11998 | return fold_build2_loc (loc, BIT_AND_EXPR, type, tem, newmaskt); |
22164c3d JJ |
11999 | } |
12000 | } | |
12001 | } | |
12002 | ||
0aee4751 KH |
12003 | goto associate; |
12004 | ||
12005 | case RDIV_EXPR: | |
12006 | /* Don't touch a floating-point divide by zero unless the mode | |
12007 | of the constant can represent infinity. */ | |
12008 | if (TREE_CODE (arg1) == REAL_CST | |
12009 | && !MODE_HAS_INFINITIES (TYPE_MODE (TREE_TYPE (arg1))) | |
12010 | && real_zerop (arg1)) | |
62ab45cc | 12011 | return NULL_TREE; |
0aee4751 | 12012 | |
ffbc33cc | 12013 | /* Optimize A / A to 1.0 if we don't care about |
1d8b38a0 UB |
12014 | NaNs or Infinities. Skip the transformation |
12015 | for non-real operands. */ | |
12016 | if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (arg0)) | |
12017 | && ! HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0))) | |
ffbc33cc UB |
12018 | && ! HONOR_INFINITIES (TYPE_MODE (TREE_TYPE (arg0))) |
12019 | && operand_equal_p (arg0, arg1, 0)) | |
12020 | { | |
12021 | tree r = build_real (TREE_TYPE (arg0), dconst1); | |
12022 | ||
db3927fb | 12023 | return omit_two_operands_loc (loc, type, r, arg0, arg1); |
ffbc33cc UB |
12024 | } |
12025 | ||
1d8b38a0 UB |
12026 | /* The complex version of the above A / A optimization. */ |
12027 | if (COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0)) | |
12028 | && operand_equal_p (arg0, arg1, 0)) | |
12029 | { | |
12030 | tree elem_type = TREE_TYPE (TREE_TYPE (arg0)); | |
12031 | if (! HONOR_NANS (TYPE_MODE (elem_type)) | |
12032 | && ! HONOR_INFINITIES (TYPE_MODE (elem_type))) | |
12033 | { | |
12034 | tree r = build_real (elem_type, dconst1); | |
12035 | /* omit_two_operands will call fold_convert for us. */ | |
db3927fb | 12036 | return omit_two_operands_loc (loc, type, r, arg0, arg1); |
1d8b38a0 UB |
12037 | } |
12038 | } | |
12039 | ||
0aee4751 KH |
12040 | /* (-A) / (-B) -> A / B */ |
12041 | if (TREE_CODE (arg0) == NEGATE_EXPR && negate_expr_p (arg1)) | |
db3927fb | 12042 | return fold_build2_loc (loc, RDIV_EXPR, type, |
7f20a5b7 KH |
12043 | TREE_OPERAND (arg0, 0), |
12044 | negate_expr (arg1)); | |
0aee4751 | 12045 | if (TREE_CODE (arg1) == NEGATE_EXPR && negate_expr_p (arg0)) |
db3927fb | 12046 | return fold_build2_loc (loc, RDIV_EXPR, type, |
7f20a5b7 KH |
12047 | negate_expr (arg0), |
12048 | TREE_OPERAND (arg1, 0)); | |
0aee4751 KH |
12049 | |
12050 | /* In IEEE floating point, x/1 is not equivalent to x for snans. */ | |
12051 | if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0))) | |
12052 | && real_onep (arg1)) | |
db3927fb | 12053 | return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0)); |
0aee4751 KH |
12054 | |
12055 | /* In IEEE floating point, x/-1 is not equivalent to -x for snans. */ | |
12056 | if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0))) | |
12057 | && real_minus_onep (arg1)) | |
db3927fb AH |
12058 | return non_lvalue_loc (loc, fold_convert_loc (loc, type, |
12059 | negate_expr (arg0))); | |
0aee4751 KH |
12060 | |
12061 | /* If ARG1 is a constant, we can convert this to a multiply by the | |
12062 | reciprocal. This does not have the same rounding properties, | |
a1a82611 | 12063 | so only do this if -freciprocal-math. We can actually |
0aee4751 KH |
12064 | always safely do it if ARG1 is a power of two, but it's hard to |
12065 | tell if it is or not in a portable manner. */ | |
add6207a BS |
12066 | if (optimize |
12067 | && (TREE_CODE (arg1) == REAL_CST | |
12068 | || (TREE_CODE (arg1) == COMPLEX_CST | |
12069 | && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg1))) | |
12070 | || (TREE_CODE (arg1) == VECTOR_CST | |
12071 | && VECTOR_FLOAT_TYPE_P (TREE_TYPE (arg1))))) | |
0aee4751 | 12072 | { |
a1a82611 | 12073 | if (flag_reciprocal_math |
add6207a | 12074 | && 0 != (tem = const_binop (code, build_one_cst (type), arg1))) |
db3927fb | 12075 | return fold_build2_loc (loc, MULT_EXPR, type, arg0, tem); |
add6207a BS |
12076 | /* Find the reciprocal if optimizing and the result is exact. |
12077 | TODO: Complex reciprocal not implemented. */ | |
12078 | if (TREE_CODE (arg1) != COMPLEX_CST) | |
0aee4751 | 12079 | { |
add6207a BS |
12080 | tree inverse = exact_inverse (TREE_TYPE (arg0), arg1); |
12081 | ||
12082 | if (inverse) | |
12083 | return fold_build2_loc (loc, MULT_EXPR, type, arg0, inverse); | |
0aee4751 KH |
12084 | } |
12085 | } | |
b8698a0f | 12086 | /* Convert A/B/C to A/(B*C). */ |
a1a82611 | 12087 | if (flag_reciprocal_math |
0aee4751 | 12088 | && TREE_CODE (arg0) == RDIV_EXPR) |
db3927fb AH |
12089 | return fold_build2_loc (loc, RDIV_EXPR, type, TREE_OPERAND (arg0, 0), |
12090 | fold_build2_loc (loc, MULT_EXPR, type, | |
7f20a5b7 | 12091 | TREE_OPERAND (arg0, 1), arg1)); |
0aee4751 KH |
12092 | |
12093 | /* Convert A/(B/C) to (A/B)*C. */ | |
a1a82611 | 12094 | if (flag_reciprocal_math |
0aee4751 | 12095 | && TREE_CODE (arg1) == RDIV_EXPR) |
db3927fb AH |
12096 | return fold_build2_loc (loc, MULT_EXPR, type, |
12097 | fold_build2_loc (loc, RDIV_EXPR, type, arg0, | |
7f20a5b7 KH |
12098 | TREE_OPERAND (arg1, 0)), |
12099 | TREE_OPERAND (arg1, 1)); | |
0aee4751 KH |
12100 | |
12101 | /* Convert C1/(X*C2) into (C1/C2)/X. */ | |
a1a82611 | 12102 | if (flag_reciprocal_math |
0aee4751 KH |
12103 | && TREE_CODE (arg1) == MULT_EXPR |
12104 | && TREE_CODE (arg0) == REAL_CST | |
12105 | && TREE_CODE (TREE_OPERAND (arg1, 1)) == REAL_CST) | |
12106 | { | |
12107 | tree tem = const_binop (RDIV_EXPR, arg0, | |
43a5d30b | 12108 | TREE_OPERAND (arg1, 1)); |
0aee4751 | 12109 | if (tem) |
db3927fb | 12110 | return fold_build2_loc (loc, RDIV_EXPR, type, tem, |
7f20a5b7 | 12111 | TREE_OPERAND (arg1, 0)); |
0aee4751 KH |
12112 | } |
12113 | ||
0aee4751 KH |
12114 | if (flag_unsafe_math_optimizations) |
12115 | { | |
12116 | enum built_in_function fcode0 = builtin_mathfn_code (arg0); | |
12117 | enum built_in_function fcode1 = builtin_mathfn_code (arg1); | |
12118 | ||
12119 | /* Optimize sin(x)/cos(x) as tan(x). */ | |
12120 | if (((fcode0 == BUILT_IN_SIN && fcode1 == BUILT_IN_COS) | |
12121 | || (fcode0 == BUILT_IN_SINF && fcode1 == BUILT_IN_COSF) | |
12122 | || (fcode0 == BUILT_IN_SINL && fcode1 == BUILT_IN_COSL)) | |
5039610b SL |
12123 | && operand_equal_p (CALL_EXPR_ARG (arg0, 0), |
12124 | CALL_EXPR_ARG (arg1, 0), 0)) | |
0aee4751 KH |
12125 | { |
12126 | tree tanfn = mathfn_built_in (type, BUILT_IN_TAN); | |
12127 | ||
12128 | if (tanfn != NULL_TREE) | |
db3927fb | 12129 | return build_call_expr_loc (loc, tanfn, 1, CALL_EXPR_ARG (arg0, 0)); |
0aee4751 KH |
12130 | } |
12131 | ||
12132 | /* Optimize cos(x)/sin(x) as 1.0/tan(x). */ | |
12133 | if (((fcode0 == BUILT_IN_COS && fcode1 == BUILT_IN_SIN) | |
12134 | || (fcode0 == BUILT_IN_COSF && fcode1 == BUILT_IN_SINF) | |
12135 | || (fcode0 == BUILT_IN_COSL && fcode1 == BUILT_IN_SINL)) | |
5039610b SL |
12136 | && operand_equal_p (CALL_EXPR_ARG (arg0, 0), |
12137 | CALL_EXPR_ARG (arg1, 0), 0)) | |
0aee4751 KH |
12138 | { |
12139 | tree tanfn = mathfn_built_in (type, BUILT_IN_TAN); | |
12140 | ||
12141 | if (tanfn != NULL_TREE) | |
12142 | { | |
db3927fb AH |
12143 | tree tmp = build_call_expr_loc (loc, tanfn, 1, |
12144 | CALL_EXPR_ARG (arg0, 0)); | |
12145 | return fold_build2_loc (loc, RDIV_EXPR, type, | |
7f20a5b7 | 12146 | build_real (type, dconst1), tmp); |
0aee4751 KH |
12147 | } |
12148 | } | |
12149 | ||
d531830f RS |
12150 | /* Optimize sin(x)/tan(x) as cos(x) if we don't care about |
12151 | NaNs or Infinities. */ | |
12152 | if (((fcode0 == BUILT_IN_SIN && fcode1 == BUILT_IN_TAN) | |
12153 | || (fcode0 == BUILT_IN_SINF && fcode1 == BUILT_IN_TANF) | |
12154 | || (fcode0 == BUILT_IN_SINL && fcode1 == BUILT_IN_TANL))) | |
12155 | { | |
5039610b SL |
12156 | tree arg00 = CALL_EXPR_ARG (arg0, 0); |
12157 | tree arg01 = CALL_EXPR_ARG (arg1, 0); | |
d531830f RS |
12158 | |
12159 | if (! HONOR_NANS (TYPE_MODE (TREE_TYPE (arg00))) | |
12160 | && ! HONOR_INFINITIES (TYPE_MODE (TREE_TYPE (arg00))) | |
12161 | && operand_equal_p (arg00, arg01, 0)) | |
12162 | { | |
12163 | tree cosfn = mathfn_built_in (type, BUILT_IN_COS); | |
12164 | ||
12165 | if (cosfn != NULL_TREE) | |
db3927fb | 12166 | return build_call_expr_loc (loc, cosfn, 1, arg00); |
d531830f RS |
12167 | } |
12168 | } | |
12169 | ||
12170 | /* Optimize tan(x)/sin(x) as 1.0/cos(x) if we don't care about | |
6416ae7f | 12171 | NaNs or Infinities. */ |
d531830f RS |
12172 | if (((fcode0 == BUILT_IN_TAN && fcode1 == BUILT_IN_SIN) |
12173 | || (fcode0 == BUILT_IN_TANF && fcode1 == BUILT_IN_SINF) | |
12174 | || (fcode0 == BUILT_IN_TANL && fcode1 == BUILT_IN_SINL))) | |
12175 | { | |
5039610b SL |
12176 | tree arg00 = CALL_EXPR_ARG (arg0, 0); |
12177 | tree arg01 = CALL_EXPR_ARG (arg1, 0); | |
d531830f RS |
12178 | |
12179 | if (! HONOR_NANS (TYPE_MODE (TREE_TYPE (arg00))) | |
12180 | && ! HONOR_INFINITIES (TYPE_MODE (TREE_TYPE (arg00))) | |
12181 | && operand_equal_p (arg00, arg01, 0)) | |
12182 | { | |
12183 | tree cosfn = mathfn_built_in (type, BUILT_IN_COS); | |
12184 | ||
12185 | if (cosfn != NULL_TREE) | |
12186 | { | |
db3927fb AH |
12187 | tree tmp = build_call_expr_loc (loc, cosfn, 1, arg00); |
12188 | return fold_build2_loc (loc, RDIV_EXPR, type, | |
d531830f | 12189 | build_real (type, dconst1), |
b71b8086 | 12190 | tmp); |
d531830f RS |
12191 | } |
12192 | } | |
12193 | } | |
12194 | ||
0aee4751 KH |
12195 | /* Optimize pow(x,c)/x as pow(x,c-1). */ |
12196 | if (fcode0 == BUILT_IN_POW | |
12197 | || fcode0 == BUILT_IN_POWF | |
12198 | || fcode0 == BUILT_IN_POWL) | |
12199 | { | |
5039610b SL |
12200 | tree arg00 = CALL_EXPR_ARG (arg0, 0); |
12201 | tree arg01 = CALL_EXPR_ARG (arg0, 1); | |
0aee4751 | 12202 | if (TREE_CODE (arg01) == REAL_CST |
455f14dd | 12203 | && !TREE_OVERFLOW (arg01) |
0aee4751 KH |
12204 | && operand_equal_p (arg1, arg00, 0)) |
12205 | { | |
5039610b | 12206 | tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0); |
0aee4751 | 12207 | REAL_VALUE_TYPE c; |
5039610b | 12208 | tree arg; |
0aee4751 KH |
12209 | |
12210 | c = TREE_REAL_CST (arg01); | |
12211 | real_arithmetic (&c, MINUS_EXPR, &c, &dconst1); | |
12212 | arg = build_real (type, c); | |
db3927fb | 12213 | return build_call_expr_loc (loc, powfn, 2, arg1, arg); |
0aee4751 KH |
12214 | } |
12215 | } | |
d531830f | 12216 | |
9883e373 UB |
12217 | /* Optimize a/root(b/c) into a*root(c/b). */ |
12218 | if (BUILTIN_ROOT_P (fcode1)) | |
f1da2df1 UB |
12219 | { |
12220 | tree rootarg = CALL_EXPR_ARG (arg1, 0); | |
12221 | ||
12222 | if (TREE_CODE (rootarg) == RDIV_EXPR) | |
12223 | { | |
12224 | tree rootfn = TREE_OPERAND (CALL_EXPR_FN (arg1), 0); | |
12225 | tree b = TREE_OPERAND (rootarg, 0); | |
12226 | tree c = TREE_OPERAND (rootarg, 1); | |
12227 | ||
db3927fb | 12228 | tree tmp = fold_build2_loc (loc, RDIV_EXPR, type, c, b); |
f1da2df1 | 12229 | |
db3927fb AH |
12230 | tmp = build_call_expr_loc (loc, rootfn, 1, tmp); |
12231 | return fold_build2_loc (loc, MULT_EXPR, type, arg0, tmp); | |
f1da2df1 UB |
12232 | } |
12233 | } | |
12234 | ||
d531830f RS |
12235 | /* Optimize x/expN(y) into x*expN(-y). */ |
12236 | if (BUILTIN_EXPONENT_P (fcode1)) | |
12237 | { | |
5039610b SL |
12238 | tree expfn = TREE_OPERAND (CALL_EXPR_FN (arg1), 0); |
12239 | tree arg = negate_expr (CALL_EXPR_ARG (arg1, 0)); | |
db3927fb AH |
12240 | arg1 = build_call_expr_loc (loc, |
12241 | expfn, 1, | |
12242 | fold_convert_loc (loc, type, arg)); | |
12243 | return fold_build2_loc (loc, MULT_EXPR, type, arg0, arg1); | |
d531830f RS |
12244 | } |
12245 | ||
12246 | /* Optimize x/pow(y,z) into x*pow(y,-z). */ | |
12247 | if (fcode1 == BUILT_IN_POW | |
12248 | || fcode1 == BUILT_IN_POWF | |
12249 | || fcode1 == BUILT_IN_POWL) | |
12250 | { | |
5039610b SL |
12251 | tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg1), 0); |
12252 | tree arg10 = CALL_EXPR_ARG (arg1, 0); | |
12253 | tree arg11 = CALL_EXPR_ARG (arg1, 1); | |
db3927fb AH |
12254 | tree neg11 = fold_convert_loc (loc, type, |
12255 | negate_expr (arg11)); | |
12256 | arg1 = build_call_expr_loc (loc, powfn, 2, arg10, neg11); | |
12257 | return fold_build2_loc (loc, MULT_EXPR, type, arg0, arg1); | |
d531830f | 12258 | } |
0aee4751 | 12259 | } |
fd6c76f4 | 12260 | return NULL_TREE; |
0aee4751 KH |
12261 | |
12262 | case TRUNC_DIV_EXPR: | |
2298ade7 DM |
12263 | /* Optimize (X & (-A)) / A where A is a power of 2, |
12264 | to X >> log2(A) */ | |
12265 | if (TREE_CODE (arg0) == BIT_AND_EXPR | |
12266 | && !TYPE_UNSIGNED (type) && TREE_CODE (arg1) == INTEGER_CST | |
12267 | && integer_pow2p (arg1) && tree_int_cst_sgn (arg1) > 0) | |
12268 | { | |
12269 | tree sum = fold_binary_loc (loc, PLUS_EXPR, TREE_TYPE (arg1), | |
12270 | arg1, TREE_OPERAND (arg0, 1)); | |
12271 | if (sum && integer_zerop (sum)) { | |
12272 | unsigned long pow2; | |
12273 | ||
12274 | if (TREE_INT_CST_LOW (arg1)) | |
12275 | pow2 = exact_log2 (TREE_INT_CST_LOW (arg1)); | |
12276 | else | |
12277 | pow2 = exact_log2 (TREE_INT_CST_HIGH (arg1)) | |
12278 | + HOST_BITS_PER_WIDE_INT; | |
12279 | ||
12280 | return fold_build2_loc (loc, RSHIFT_EXPR, type, | |
12281 | TREE_OPERAND (arg0, 0), | |
9f616812 | 12282 | build_int_cst (integer_type_node, pow2)); |
2298ade7 DM |
12283 | } |
12284 | } | |
12285 | ||
073a8998 | 12286 | /* Fall through */ |
2298ade7 | 12287 | |
0aee4751 | 12288 | case FLOOR_DIV_EXPR: |
0f35201e AM |
12289 | /* Simplify A / (B << N) where A and B are positive and B is |
12290 | a power of 2, to A >> (N + log2(B)). */ | |
6ac01510 | 12291 | strict_overflow_p = false; |
0f35201e | 12292 | if (TREE_CODE (arg1) == LSHIFT_EXPR |
6ac01510 | 12293 | && (TYPE_UNSIGNED (type) |
916c75b4 | 12294 | || tree_expr_nonnegative_warnv_p (op0, &strict_overflow_p))) |
0f35201e AM |
12295 | { |
12296 | tree sval = TREE_OPERAND (arg1, 0); | |
12297 | if (integer_pow2p (sval) && tree_int_cst_sgn (sval) > 0) | |
12298 | { | |
12299 | tree sh_cnt = TREE_OPERAND (arg1, 1); | |
8ddf04c2 JJ |
12300 | unsigned long pow2; |
12301 | ||
12302 | if (TREE_INT_CST_LOW (sval)) | |
12303 | pow2 = exact_log2 (TREE_INT_CST_LOW (sval)); | |
12304 | else | |
12305 | pow2 = exact_log2 (TREE_INT_CST_HIGH (sval)) | |
12306 | + HOST_BITS_PER_WIDE_INT; | |
0f35201e | 12307 | |
6ac01510 ILT |
12308 | if (strict_overflow_p) |
12309 | fold_overflow_warning (("assuming signed overflow does not " | |
12310 | "occur when simplifying A / (B << N)"), | |
12311 | WARN_STRICT_OVERFLOW_MISC); | |
12312 | ||
db3927fb | 12313 | sh_cnt = fold_build2_loc (loc, PLUS_EXPR, TREE_TYPE (sh_cnt), |
9f616812 RG |
12314 | sh_cnt, |
12315 | build_int_cst (TREE_TYPE (sh_cnt), | |
12316 | pow2)); | |
db3927fb AH |
12317 | return fold_build2_loc (loc, RSHIFT_EXPR, type, |
12318 | fold_convert_loc (loc, type, arg0), sh_cnt); | |
0f35201e AM |
12319 | } |
12320 | } | |
65648dd4 RG |
12321 | |
12322 | /* For unsigned integral types, FLOOR_DIV_EXPR is the same as | |
12323 | TRUNC_DIV_EXPR. Rewrite into the latter in this case. */ | |
12324 | if (INTEGRAL_TYPE_P (type) | |
12325 | && TYPE_UNSIGNED (type) | |
12326 | && code == FLOOR_DIV_EXPR) | |
db3927fb | 12327 | return fold_build2_loc (loc, TRUNC_DIV_EXPR, type, op0, op1); |
65648dd4 | 12328 | |
073a8998 | 12329 | /* Fall through */ |
0f35201e AM |
12330 | |
12331 | case ROUND_DIV_EXPR: | |
0aee4751 KH |
12332 | case CEIL_DIV_EXPR: |
12333 | case EXACT_DIV_EXPR: | |
12334 | if (integer_onep (arg1)) | |
db3927fb | 12335 | return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0)); |
0aee4751 | 12336 | if (integer_zerop (arg1)) |
62ab45cc | 12337 | return NULL_TREE; |
0aee4751 KH |
12338 | /* X / -1 is -X. */ |
12339 | if (!TYPE_UNSIGNED (type) | |
12340 | && TREE_CODE (arg1) == INTEGER_CST | |
0cadbfaa | 12341 | && TREE_INT_CST_LOW (arg1) == HOST_WIDE_INT_M1U |
0aee4751 | 12342 | && TREE_INT_CST_HIGH (arg1) == -1) |
db3927fb | 12343 | return fold_convert_loc (loc, type, negate_expr (arg0)); |
0aee4751 | 12344 | |
37d3243d AP |
12345 | /* Convert -A / -B to A / B when the type is signed and overflow is |
12346 | undefined. */ | |
eeef0e45 | 12347 | if ((!INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_UNDEFINED (type)) |
37d3243d AP |
12348 | && TREE_CODE (arg0) == NEGATE_EXPR |
12349 | && negate_expr_p (arg1)) | |
6ac01510 ILT |
12350 | { |
12351 | if (INTEGRAL_TYPE_P (type)) | |
12352 | fold_overflow_warning (("assuming signed overflow does not occur " | |
12353 | "when distributing negation across " | |
12354 | "division"), | |
12355 | WARN_STRICT_OVERFLOW_MISC); | |
db3927fb AH |
12356 | return fold_build2_loc (loc, code, type, |
12357 | fold_convert_loc (loc, type, | |
12358 | TREE_OPERAND (arg0, 0)), | |
12359 | fold_convert_loc (loc, type, | |
12360 | negate_expr (arg1))); | |
6ac01510 | 12361 | } |
eeef0e45 | 12362 | if ((!INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_UNDEFINED (type)) |
37d3243d AP |
12363 | && TREE_CODE (arg1) == NEGATE_EXPR |
12364 | && negate_expr_p (arg0)) | |
6ac01510 ILT |
12365 | { |
12366 | if (INTEGRAL_TYPE_P (type)) | |
12367 | fold_overflow_warning (("assuming signed overflow does not occur " | |
12368 | "when distributing negation across " | |
12369 | "division"), | |
12370 | WARN_STRICT_OVERFLOW_MISC); | |
db3927fb AH |
12371 | return fold_build2_loc (loc, code, type, |
12372 | fold_convert_loc (loc, type, | |
12373 | negate_expr (arg0)), | |
12374 | fold_convert_loc (loc, type, | |
12375 | TREE_OPERAND (arg1, 0))); | |
6ac01510 | 12376 | } |
37d3243d | 12377 | |
0aee4751 KH |
12378 | /* If arg0 is a multiple of arg1, then rewrite to the fastest div |
12379 | operation, EXACT_DIV_EXPR. | |
12380 | ||
12381 | Note that only CEIL_DIV_EXPR and FLOOR_DIV_EXPR are rewritten now. | |
12382 | At one time others generated faster code, it's not clear if they do | |
12383 | after the last round to changes to the DIV code in expmed.c. */ | |
12384 | if ((code == CEIL_DIV_EXPR || code == FLOOR_DIV_EXPR) | |
12385 | && multiple_of_p (type, arg0, arg1)) | |
db3927fb | 12386 | return fold_build2_loc (loc, EXACT_DIV_EXPR, type, arg0, arg1); |
0aee4751 | 12387 | |
6ac01510 | 12388 | strict_overflow_p = false; |
0aee4751 | 12389 | if (TREE_CODE (arg1) == INTEGER_CST |
6ac01510 ILT |
12390 | && 0 != (tem = extract_muldiv (op0, arg1, code, NULL_TREE, |
12391 | &strict_overflow_p))) | |
12392 | { | |
12393 | if (strict_overflow_p) | |
12394 | fold_overflow_warning (("assuming signed overflow does not occur " | |
12395 | "when simplifying division"), | |
12396 | WARN_STRICT_OVERFLOW_MISC); | |
db3927fb | 12397 | return fold_convert_loc (loc, type, tem); |
6ac01510 | 12398 | } |
0aee4751 | 12399 | |
fd6c76f4 | 12400 | return NULL_TREE; |
0aee4751 KH |
12401 | |
12402 | case CEIL_MOD_EXPR: | |
12403 | case FLOOR_MOD_EXPR: | |
12404 | case ROUND_MOD_EXPR: | |
12405 | case TRUNC_MOD_EXPR: | |
12406 | /* X % 1 is always zero, but be sure to preserve any side | |
12407 | effects in X. */ | |
12408 | if (integer_onep (arg1)) | |
db3927fb | 12409 | return omit_one_operand_loc (loc, type, integer_zero_node, arg0); |
0aee4751 KH |
12410 | |
12411 | /* X % 0, return X % 0 unchanged so that we can get the | |
12412 | proper warnings and errors. */ | |
12413 | if (integer_zerop (arg1)) | |
62ab45cc | 12414 | return NULL_TREE; |
0aee4751 KH |
12415 | |
12416 | /* 0 % X is always zero, but be sure to preserve any side | |
12417 | effects in X. Place this after checking for X == 0. */ | |
12418 | if (integer_zerop (arg0)) | |
db3927fb | 12419 | return omit_one_operand_loc (loc, type, integer_zero_node, arg1); |
0aee4751 KH |
12420 | |
12421 | /* X % -1 is zero. */ | |
12422 | if (!TYPE_UNSIGNED (type) | |
12423 | && TREE_CODE (arg1) == INTEGER_CST | |
0cadbfaa | 12424 | && TREE_INT_CST_LOW (arg1) == HOST_WIDE_INT_M1U |
0aee4751 | 12425 | && TREE_INT_CST_HIGH (arg1) == -1) |
db3927fb | 12426 | return omit_one_operand_loc (loc, type, integer_zero_node, arg0); |
0aee4751 | 12427 | |
0aee4751 KH |
12428 | /* X % -C is the same as X % C. */ |
12429 | if (code == TRUNC_MOD_EXPR | |
12430 | && !TYPE_UNSIGNED (type) | |
12431 | && TREE_CODE (arg1) == INTEGER_CST | |
455f14dd | 12432 | && !TREE_OVERFLOW (arg1) |
0aee4751 | 12433 | && TREE_INT_CST_HIGH (arg1) < 0 |
eeef0e45 | 12434 | && !TYPE_OVERFLOW_TRAPS (type) |
0aee4751 KH |
12435 | /* Avoid this transformation if C is INT_MIN, i.e. C == -C. */ |
12436 | && !sign_bit_p (arg1, arg1)) | |
db3927fb AH |
12437 | return fold_build2_loc (loc, code, type, |
12438 | fold_convert_loc (loc, type, arg0), | |
12439 | fold_convert_loc (loc, type, | |
12440 | negate_expr (arg1))); | |
0aee4751 KH |
12441 | |
12442 | /* X % -Y is the same as X % Y. */ | |
12443 | if (code == TRUNC_MOD_EXPR | |
12444 | && !TYPE_UNSIGNED (type) | |
12445 | && TREE_CODE (arg1) == NEGATE_EXPR | |
eeef0e45 | 12446 | && !TYPE_OVERFLOW_TRAPS (type)) |
db3927fb AH |
12447 | return fold_build2_loc (loc, code, type, fold_convert_loc (loc, type, arg0), |
12448 | fold_convert_loc (loc, type, | |
12449 | TREE_OPERAND (arg1, 0))); | |
0aee4751 | 12450 | |
9e9ef331 | 12451 | strict_overflow_p = false; |
0aee4751 | 12452 | if (TREE_CODE (arg1) == INTEGER_CST |
6ac01510 ILT |
12453 | && 0 != (tem = extract_muldiv (op0, arg1, code, NULL_TREE, |
12454 | &strict_overflow_p))) | |
12455 | { | |
12456 | if (strict_overflow_p) | |
12457 | fold_overflow_warning (("assuming signed overflow does not occur " | |
fa10beec | 12458 | "when simplifying modulus"), |
6ac01510 | 12459 | WARN_STRICT_OVERFLOW_MISC); |
db3927fb | 12460 | return fold_convert_loc (loc, type, tem); |
6ac01510 | 12461 | } |
0aee4751 | 12462 | |
9e9ef331 EB |
12463 | /* Optimize TRUNC_MOD_EXPR by a power of two into a BIT_AND_EXPR, |
12464 | i.e. "X % C" into "X & (C - 1)", if X and C are positive. */ | |
12465 | if ((code == TRUNC_MOD_EXPR || code == FLOOR_MOD_EXPR) | |
12466 | && (TYPE_UNSIGNED (type) | |
12467 | || tree_expr_nonnegative_warnv_p (op0, &strict_overflow_p))) | |
12468 | { | |
12469 | tree c = arg1; | |
12470 | /* Also optimize A % (C << N) where C is a power of 2, | |
12471 | to A & ((C << N) - 1). */ | |
12472 | if (TREE_CODE (arg1) == LSHIFT_EXPR) | |
12473 | c = TREE_OPERAND (arg1, 0); | |
12474 | ||
12475 | if (integer_pow2p (c) && tree_int_cst_sgn (c) > 0) | |
12476 | { | |
12477 | tree mask | |
12478 | = fold_build2_loc (loc, MINUS_EXPR, TREE_TYPE (arg1), arg1, | |
12479 | build_int_cst (TREE_TYPE (arg1), 1)); | |
12480 | if (strict_overflow_p) | |
12481 | fold_overflow_warning (("assuming signed overflow does not " | |
12482 | "occur when simplifying " | |
12483 | "X % (power of two)"), | |
12484 | WARN_STRICT_OVERFLOW_MISC); | |
12485 | return fold_build2_loc (loc, BIT_AND_EXPR, type, | |
12486 | fold_convert_loc (loc, type, arg0), | |
12487 | fold_convert_loc (loc, type, mask)); | |
12488 | } | |
12489 | } | |
12490 | ||
fd6c76f4 | 12491 | return NULL_TREE; |
0aee4751 KH |
12492 | |
12493 | case LROTATE_EXPR: | |
12494 | case RROTATE_EXPR: | |
12495 | if (integer_all_onesp (arg0)) | |
db3927fb | 12496 | return omit_one_operand_loc (loc, type, arg0, arg1); |
0aee4751 KH |
12497 | goto shift; |
12498 | ||
12499 | case RSHIFT_EXPR: | |
12500 | /* Optimize -1 >> x for arithmetic right shifts. */ | |
bd170bbc RG |
12501 | if (integer_all_onesp (arg0) && !TYPE_UNSIGNED (type) |
12502 | && tree_expr_nonnegative_p (arg1)) | |
db3927fb | 12503 | return omit_one_operand_loc (loc, type, arg0, arg1); |
0aee4751 KH |
12504 | /* ... fall through ... */ |
12505 | ||
12506 | case LSHIFT_EXPR: | |
12507 | shift: | |
12508 | if (integer_zerop (arg1)) | |
db3927fb | 12509 | return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0)); |
0aee4751 | 12510 | if (integer_zerop (arg0)) |
db3927fb | 12511 | return omit_one_operand_loc (loc, type, arg0, arg1); |
0aee4751 | 12512 | |
640bfeb2 MG |
12513 | /* Prefer vector1 << scalar to vector1 << vector2 |
12514 | if vector2 is uniform. */ | |
12515 | if (VECTOR_TYPE_P (TREE_TYPE (arg1)) | |
12516 | && (tem = uniform_vector_p (arg1)) != NULL_TREE) | |
12517 | return fold_build2_loc (loc, code, type, op0, tem); | |
12518 | ||
0aee4751 KH |
12519 | /* Since negative shift count is not well-defined, |
12520 | don't try to compute it in the compiler. */ | |
12521 | if (TREE_CODE (arg1) == INTEGER_CST && tree_int_cst_sgn (arg1) < 0) | |
62ab45cc | 12522 | return NULL_TREE; |
e3d025cb | 12523 | |
a5e0cd1d MG |
12524 | prec = element_precision (type); |
12525 | ||
e3d025cb | 12526 | /* Turn (a OP c1) OP c2 into a OP (c1+c2). */ |
d4c52634 | 12527 | if (TREE_CODE (op0) == code && host_integerp (arg1, true) |
a5e0cd1d | 12528 | && TREE_INT_CST_LOW (arg1) < prec |
d4c52634 | 12529 | && host_integerp (TREE_OPERAND (arg0, 1), true) |
a5e0cd1d | 12530 | && TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1)) < prec) |
e3d025cb | 12531 | { |
d4c52634 MG |
12532 | unsigned int low = (TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1)) |
12533 | + TREE_INT_CST_LOW (arg1)); | |
e3d025cb JM |
12534 | |
12535 | /* Deal with a OP (c1 + c2) being undefined but (a OP c1) OP c2 | |
12536 | being well defined. */ | |
a5e0cd1d | 12537 | if (low >= prec) |
e3d025cb JM |
12538 | { |
12539 | if (code == LROTATE_EXPR || code == RROTATE_EXPR) | |
a5e0cd1d | 12540 | low = low % prec; |
e3d025cb | 12541 | else if (TYPE_UNSIGNED (type) || code == LSHIFT_EXPR) |
a5e0cd1d | 12542 | return omit_one_operand_loc (loc, type, build_zero_cst (type), |
2c0eba5a | 12543 | TREE_OPERAND (arg0, 0)); |
e3d025cb | 12544 | else |
a5e0cd1d | 12545 | low = prec - 1; |
e3d025cb JM |
12546 | } |
12547 | ||
db3927fb | 12548 | return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0), |
a5e0cd1d | 12549 | build_int_cst (TREE_TYPE (arg1), low)); |
e3d025cb JM |
12550 | } |
12551 | ||
a165e746 JM |
12552 | /* Transform (x >> c) << c into x & (-1<<c), or transform (x << c) >> c |
12553 | into x & ((unsigned)-1 >> c) for unsigned types. */ | |
12554 | if (((code == LSHIFT_EXPR && TREE_CODE (arg0) == RSHIFT_EXPR) | |
12555 | || (TYPE_UNSIGNED (type) | |
12556 | && code == RSHIFT_EXPR && TREE_CODE (arg0) == LSHIFT_EXPR)) | |
e3d025cb | 12557 | && host_integerp (arg1, false) |
a5e0cd1d | 12558 | && TREE_INT_CST_LOW (arg1) < prec |
e3d025cb | 12559 | && host_integerp (TREE_OPERAND (arg0, 1), false) |
a5e0cd1d | 12560 | && TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1)) < prec) |
e3d025cb JM |
12561 | { |
12562 | HOST_WIDE_INT low0 = TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1)); | |
12563 | HOST_WIDE_INT low1 = TREE_INT_CST_LOW (arg1); | |
e3d025cb JM |
12564 | tree lshift; |
12565 | tree arg00; | |
12566 | ||
12567 | if (low0 == low1) | |
12568 | { | |
db3927fb | 12569 | arg00 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0)); |
e3d025cb | 12570 | |
a5e0cd1d MG |
12571 | lshift = build_minus_one_cst (type); |
12572 | lshift = const_binop (code, lshift, arg1); | |
e3d025cb | 12573 | |
db3927fb | 12574 | return fold_build2_loc (loc, BIT_AND_EXPR, type, arg00, lshift); |
e3d025cb JM |
12575 | } |
12576 | } | |
12577 | ||
0aee4751 KH |
12578 | /* Rewrite an LROTATE_EXPR by a constant into an |
12579 | RROTATE_EXPR by a new constant. */ | |
12580 | if (code == LROTATE_EXPR && TREE_CODE (arg1) == INTEGER_CST) | |
12581 | { | |
a5e0cd1d | 12582 | tree tem = build_int_cst (TREE_TYPE (arg1), prec); |
43a5d30b | 12583 | tem = const_binop (MINUS_EXPR, tem, arg1); |
db3927fb | 12584 | return fold_build2_loc (loc, RROTATE_EXPR, type, op0, tem); |
0aee4751 KH |
12585 | } |
12586 | ||
12587 | /* If we have a rotate of a bit operation with the rotate count and | |
12588 | the second operand of the bit operation both constant, | |
12589 | permute the two operations. */ | |
12590 | if (code == RROTATE_EXPR && TREE_CODE (arg1) == INTEGER_CST | |
12591 | && (TREE_CODE (arg0) == BIT_AND_EXPR | |
12592 | || TREE_CODE (arg0) == BIT_IOR_EXPR | |
12593 | || TREE_CODE (arg0) == BIT_XOR_EXPR) | |
12594 | && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST) | |
db3927fb AH |
12595 | return fold_build2_loc (loc, TREE_CODE (arg0), type, |
12596 | fold_build2_loc (loc, code, type, | |
7f20a5b7 | 12597 | TREE_OPERAND (arg0, 0), arg1), |
db3927fb | 12598 | fold_build2_loc (loc, code, type, |
7f20a5b7 | 12599 | TREE_OPERAND (arg0, 1), arg1)); |
0aee4751 | 12600 | |
70582b3a RG |
12601 | /* Two consecutive rotates adding up to the precision of the |
12602 | type can be ignored. */ | |
0aee4751 KH |
12603 | if (code == RROTATE_EXPR && TREE_CODE (arg1) == INTEGER_CST |
12604 | && TREE_CODE (arg0) == RROTATE_EXPR | |
12605 | && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST | |
12606 | && TREE_INT_CST_HIGH (arg1) == 0 | |
12607 | && TREE_INT_CST_HIGH (TREE_OPERAND (arg0, 1)) == 0 | |
12608 | && ((TREE_INT_CST_LOW (arg1) | |
12609 | + TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1))) | |
a5e0cd1d | 12610 | == prec)) |
0aee4751 KH |
12611 | return TREE_OPERAND (arg0, 0); |
12612 | ||
22164c3d JJ |
12613 | /* Fold (X & C2) << C1 into (X << C1) & (C2 << C1) |
12614 | (X & C2) >> C1 into (X >> C1) & (C2 >> C1) | |
12615 | if the latter can be further optimized. */ | |
12616 | if ((code == LSHIFT_EXPR || code == RSHIFT_EXPR) | |
12617 | && TREE_CODE (arg0) == BIT_AND_EXPR | |
12618 | && TREE_CODE (arg1) == INTEGER_CST | |
12619 | && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST) | |
12620 | { | |
db3927fb AH |
12621 | tree mask = fold_build2_loc (loc, code, type, |
12622 | fold_convert_loc (loc, type, | |
12623 | TREE_OPERAND (arg0, 1)), | |
22164c3d | 12624 | arg1); |
db3927fb AH |
12625 | tree shift = fold_build2_loc (loc, code, type, |
12626 | fold_convert_loc (loc, type, | |
12627 | TREE_OPERAND (arg0, 0)), | |
22164c3d | 12628 | arg1); |
db3927fb | 12629 | tem = fold_binary_loc (loc, BIT_AND_EXPR, type, shift, mask); |
22164c3d JJ |
12630 | if (tem) |
12631 | return tem; | |
12632 | } | |
12633 | ||
fd6c76f4 | 12634 | return NULL_TREE; |
0aee4751 KH |
12635 | |
12636 | case MIN_EXPR: | |
12637 | if (operand_equal_p (arg0, arg1, 0)) | |
db3927fb | 12638 | return omit_one_operand_loc (loc, type, arg0, arg1); |
0aee4751 KH |
12639 | if (INTEGRAL_TYPE_P (type) |
12640 | && operand_equal_p (arg1, TYPE_MIN_VALUE (type), OEP_ONLY_CONST)) | |
db3927fb AH |
12641 | return omit_one_operand_loc (loc, type, arg1, arg0); |
12642 | tem = fold_minmax (loc, MIN_EXPR, type, arg0, arg1); | |
292f30c5 EB |
12643 | if (tem) |
12644 | return tem; | |
0aee4751 KH |
12645 | goto associate; |
12646 | ||
12647 | case MAX_EXPR: | |
12648 | if (operand_equal_p (arg0, arg1, 0)) | |
db3927fb | 12649 | return omit_one_operand_loc (loc, type, arg0, arg1); |
0aee4751 KH |
12650 | if (INTEGRAL_TYPE_P (type) |
12651 | && TYPE_MAX_VALUE (type) | |
12652 | && operand_equal_p (arg1, TYPE_MAX_VALUE (type), OEP_ONLY_CONST)) | |
db3927fb AH |
12653 | return omit_one_operand_loc (loc, type, arg1, arg0); |
12654 | tem = fold_minmax (loc, MAX_EXPR, type, arg0, arg1); | |
292f30c5 EB |
12655 | if (tem) |
12656 | return tem; | |
0aee4751 KH |
12657 | goto associate; |
12658 | ||
12659 | case TRUTH_ANDIF_EXPR: | |
12660 | /* Note that the operands of this must be ints | |
12661 | and their values must be 0 or 1. | |
12662 | ("true" is a fixed value perhaps depending on the language.) */ | |
12663 | /* If first arg is constant zero, return it. */ | |
12664 | if (integer_zerop (arg0)) | |
db3927fb | 12665 | return fold_convert_loc (loc, type, arg0); |
0aee4751 KH |
12666 | case TRUTH_AND_EXPR: |
12667 | /* If either arg is constant true, drop it. */ | |
12668 | if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0)) | |
db3927fb | 12669 | return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg1)); |
0aee4751 KH |
12670 | if (TREE_CODE (arg1) == INTEGER_CST && ! integer_zerop (arg1) |
12671 | /* Preserve sequence points. */ | |
12672 | && (code != TRUTH_ANDIF_EXPR || ! TREE_SIDE_EFFECTS (arg0))) | |
db3927fb | 12673 | return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0)); |
0aee4751 KH |
12674 | /* If second arg is constant zero, result is zero, but first arg |
12675 | must be evaluated. */ | |
12676 | if (integer_zerop (arg1)) | |
db3927fb | 12677 | return omit_one_operand_loc (loc, type, arg1, arg0); |
0aee4751 KH |
12678 | /* Likewise for first arg, but note that only the TRUTH_AND_EXPR |
12679 | case will be handled here. */ | |
12680 | if (integer_zerop (arg0)) | |
db3927fb | 12681 | return omit_one_operand_loc (loc, type, arg0, arg1); |
0aee4751 KH |
12682 | |
12683 | /* !X && X is always false. */ | |
12684 | if (TREE_CODE (arg0) == TRUTH_NOT_EXPR | |
12685 | && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)) | |
db3927fb | 12686 | return omit_one_operand_loc (loc, type, integer_zero_node, arg1); |
0aee4751 KH |
12687 | /* X && !X is always false. */ |
12688 | if (TREE_CODE (arg1) == TRUTH_NOT_EXPR | |
12689 | && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0)) | |
db3927fb | 12690 | return omit_one_operand_loc (loc, type, integer_zero_node, arg0); |
0aee4751 KH |
12691 | |
12692 | /* A < X && A + 1 > Y ==> A < X && A >= Y. Normally A + 1 > Y | |
12693 | means A >= Y && A != MAX, but in this case we know that | |
12694 | A < X <= MAX. */ | |
12695 | ||
12696 | if (!TREE_SIDE_EFFECTS (arg0) | |
12697 | && !TREE_SIDE_EFFECTS (arg1)) | |
12698 | { | |
db3927fb | 12699 | tem = fold_to_nonsharp_ineq_using_bound (loc, arg0, arg1); |
70a9e64b | 12700 | if (tem && !operand_equal_p (tem, arg0, 0)) |
db3927fb | 12701 | return fold_build2_loc (loc, code, type, tem, arg1); |
0aee4751 | 12702 | |
db3927fb | 12703 | tem = fold_to_nonsharp_ineq_using_bound (loc, arg1, arg0); |
70a9e64b | 12704 | if (tem && !operand_equal_p (tem, arg1, 0)) |
db3927fb | 12705 | return fold_build2_loc (loc, code, type, arg0, tem); |
0aee4751 KH |
12706 | } |
12707 | ||
e8e8c74b KT |
12708 | if ((tem = fold_truth_andor (loc, code, type, arg0, arg1, op0, op1)) |
12709 | != NULL_TREE) | |
12710 | return tem; | |
0aee4751 | 12711 | |
62ab45cc | 12712 | return NULL_TREE; |
0aee4751 KH |
12713 | |
12714 | case TRUTH_ORIF_EXPR: | |
12715 | /* Note that the operands of this must be ints | |
12716 | and their values must be 0 or true. | |
12717 | ("true" is a fixed value perhaps depending on the language.) */ | |
12718 | /* If first arg is constant true, return it. */ | |
12719 | if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0)) | |
db3927fb | 12720 | return fold_convert_loc (loc, type, arg0); |
0aee4751 KH |
12721 | case TRUTH_OR_EXPR: |
12722 | /* If either arg is constant zero, drop it. */ | |
12723 | if (TREE_CODE (arg0) == INTEGER_CST && integer_zerop (arg0)) | |
db3927fb | 12724 | return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg1)); |
0aee4751 KH |
12725 | if (TREE_CODE (arg1) == INTEGER_CST && integer_zerop (arg1) |
12726 | /* Preserve sequence points. */ | |
12727 | && (code != TRUTH_ORIF_EXPR || ! TREE_SIDE_EFFECTS (arg0))) | |
db3927fb | 12728 | return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0)); |
0aee4751 KH |
12729 | /* If second arg is constant true, result is true, but we must |
12730 | evaluate first arg. */ | |
12731 | if (TREE_CODE (arg1) == INTEGER_CST && ! integer_zerop (arg1)) | |
db3927fb | 12732 | return omit_one_operand_loc (loc, type, arg1, arg0); |
0aee4751 KH |
12733 | /* Likewise for first arg, but note this only occurs here for |
12734 | TRUTH_OR_EXPR. */ | |
12735 | if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0)) | |
db3927fb | 12736 | return omit_one_operand_loc (loc, type, arg0, arg1); |
0aee4751 KH |
12737 | |
12738 | /* !X || X is always true. */ | |
12739 | if (TREE_CODE (arg0) == TRUTH_NOT_EXPR | |
12740 | && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)) | |
db3927fb | 12741 | return omit_one_operand_loc (loc, type, integer_one_node, arg1); |
0aee4751 KH |
12742 | /* X || !X is always true. */ |
12743 | if (TREE_CODE (arg1) == TRUTH_NOT_EXPR | |
12744 | && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0)) | |
db3927fb | 12745 | return omit_one_operand_loc (loc, type, integer_one_node, arg0); |
0aee4751 | 12746 | |
583722ee KT |
12747 | /* (X && !Y) || (!X && Y) is X ^ Y */ |
12748 | if (TREE_CODE (arg0) == TRUTH_AND_EXPR | |
12749 | && TREE_CODE (arg1) == TRUTH_AND_EXPR) | |
12750 | { | |
12751 | tree a0, a1, l0, l1, n0, n1; | |
12752 | ||
12753 | a0 = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 0)); | |
12754 | a1 = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 1)); | |
12755 | ||
12756 | l0 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0)); | |
12757 | l1 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 1)); | |
12758 | ||
12759 | n0 = fold_build1_loc (loc, TRUTH_NOT_EXPR, type, l0); | |
12760 | n1 = fold_build1_loc (loc, TRUTH_NOT_EXPR, type, l1); | |
12761 | ||
12762 | if ((operand_equal_p (n0, a0, 0) | |
12763 | && operand_equal_p (n1, a1, 0)) | |
12764 | || (operand_equal_p (n0, a1, 0) | |
12765 | && operand_equal_p (n1, a0, 0))) | |
12766 | return fold_build2_loc (loc, TRUTH_XOR_EXPR, type, l0, n1); | |
12767 | } | |
e8e8c74b KT |
12768 | |
12769 | if ((tem = fold_truth_andor (loc, code, type, arg0, arg1, op0, op1)) | |
12770 | != NULL_TREE) | |
12771 | return tem; | |
12772 | ||
12773 | return NULL_TREE; | |
0aee4751 KH |
12774 | |
12775 | case TRUTH_XOR_EXPR: | |
12776 | /* If the second arg is constant zero, drop it. */ | |
12777 | if (integer_zerop (arg1)) | |
db3927fb | 12778 | return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0)); |
0aee4751 KH |
12779 | /* If the second arg is constant true, this is a logical inversion. */ |
12780 | if (integer_onep (arg1)) | |
90ec750d | 12781 | { |
418d1b87 | 12782 | tem = invert_truthvalue_loc (loc, arg0); |
db3927fb | 12783 | return non_lvalue_loc (loc, fold_convert_loc (loc, type, tem)); |
90ec750d | 12784 | } |
0aee4751 KH |
12785 | /* Identical arguments cancel to zero. */ |
12786 | if (operand_equal_p (arg0, arg1, 0)) | |
db3927fb | 12787 | return omit_one_operand_loc (loc, type, integer_zero_node, arg0); |
0aee4751 KH |
12788 | |
12789 | /* !X ^ X is always true. */ | |
12790 | if (TREE_CODE (arg0) == TRUTH_NOT_EXPR | |
12791 | && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)) | |
db3927fb | 12792 | return omit_one_operand_loc (loc, type, integer_one_node, arg1); |
0aee4751 KH |
12793 | |
12794 | /* X ^ !X is always true. */ | |
12795 | if (TREE_CODE (arg1) == TRUTH_NOT_EXPR | |
12796 | && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0)) | |
db3927fb | 12797 | return omit_one_operand_loc (loc, type, integer_one_node, arg0); |
0aee4751 | 12798 | |
62ab45cc | 12799 | return NULL_TREE; |
0aee4751 KH |
12800 | |
12801 | case EQ_EXPR: | |
12802 | case NE_EXPR: | |
2e64f8b8 JJ |
12803 | STRIP_NOPS (arg0); |
12804 | STRIP_NOPS (arg1); | |
12805 | ||
db3927fb | 12806 | tem = fold_comparison (loc, code, type, op0, op1); |
e26ec0bb RS |
12807 | if (tem != NULL_TREE) |
12808 | return tem; | |
210dfe6e | 12809 | |
a7e1c928 AP |
12810 | /* bool_var != 0 becomes bool_var. */ |
12811 | if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE && integer_zerop (arg1) | |
12812 | && code == NE_EXPR) | |
db3927fb | 12813 | return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0)); |
e26ec0bb | 12814 | |
a7e1c928 AP |
12815 | /* bool_var == 1 becomes bool_var. */ |
12816 | if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE && integer_onep (arg1) | |
12817 | && code == EQ_EXPR) | |
db3927fb | 12818 | return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0)); |
0aee4751 | 12819 | |
7934558d AP |
12820 | /* bool_var != 1 becomes !bool_var. */ |
12821 | if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE && integer_onep (arg1) | |
12822 | && code == NE_EXPR) | |
fbf3fee2 RG |
12823 | return fold_convert_loc (loc, type, |
12824 | fold_build1_loc (loc, TRUTH_NOT_EXPR, | |
12825 | TREE_TYPE (arg0), arg0)); | |
7934558d AP |
12826 | |
12827 | /* bool_var == 0 becomes !bool_var. */ | |
12828 | if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE && integer_zerop (arg1) | |
12829 | && code == EQ_EXPR) | |
fbf3fee2 RG |
12830 | return fold_convert_loc (loc, type, |
12831 | fold_build1_loc (loc, TRUTH_NOT_EXPR, | |
12832 | TREE_TYPE (arg0), arg0)); | |
7934558d | 12833 | |
44e10129 MM |
12834 | /* !exp != 0 becomes !exp */ |
12835 | if (TREE_CODE (arg0) == TRUTH_NOT_EXPR && integer_zerop (arg1) | |
12836 | && code == NE_EXPR) | |
12837 | return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0)); | |
12838 | ||
0aee4751 KH |
12839 | /* If this is an equality comparison of the address of two non-weak, |
12840 | unaliased symbols neither of which are extern (since we do not | |
12841 | have access to attributes for externs), then we know the result. */ | |
e26ec0bb | 12842 | if (TREE_CODE (arg0) == ADDR_EXPR |
820cc88f | 12843 | && VAR_OR_FUNCTION_DECL_P (TREE_OPERAND (arg0, 0)) |
0aee4751 KH |
12844 | && ! DECL_WEAK (TREE_OPERAND (arg0, 0)) |
12845 | && ! lookup_attribute ("alias", | |
12846 | DECL_ATTRIBUTES (TREE_OPERAND (arg0, 0))) | |
12847 | && ! DECL_EXTERNAL (TREE_OPERAND (arg0, 0)) | |
12848 | && TREE_CODE (arg1) == ADDR_EXPR | |
820cc88f | 12849 | && VAR_OR_FUNCTION_DECL_P (TREE_OPERAND (arg1, 0)) |
0aee4751 KH |
12850 | && ! DECL_WEAK (TREE_OPERAND (arg1, 0)) |
12851 | && ! lookup_attribute ("alias", | |
12852 | DECL_ATTRIBUTES (TREE_OPERAND (arg1, 0))) | |
12853 | && ! DECL_EXTERNAL (TREE_OPERAND (arg1, 0))) | |
59f7a202 JL |
12854 | { |
12855 | /* We know that we're looking at the address of two | |
12856 | non-weak, unaliased, static _DECL nodes. | |
12857 | ||
12858 | It is both wasteful and incorrect to call operand_equal_p | |
12859 | to compare the two ADDR_EXPR nodes. It is wasteful in that | |
12860 | all we need to do is test pointer equality for the arguments | |
12861 | to the two ADDR_EXPR nodes. It is incorrect to use | |
12862 | operand_equal_p as that function is NOT equivalent to a | |
12863 | C equality test. It can in fact return false for two | |
12864 | objects which would test as equal using the C equality | |
12865 | operator. */ | |
12866 | bool equal = TREE_OPERAND (arg0, 0) == TREE_OPERAND (arg1, 0); | |
12867 | return constant_boolean_node (equal | |
12868 | ? code == EQ_EXPR : code != EQ_EXPR, | |
12869 | type); | |
12870 | } | |
0aee4751 | 12871 | |
e26ec0bb RS |
12872 | /* If this is an EQ or NE comparison of a constant with a PLUS_EXPR or |
12873 | a MINUS_EXPR of a constant, we can convert it into a comparison with | |
12874 | a revised constant as long as no overflow occurs. */ | |
12875 | if (TREE_CODE (arg1) == INTEGER_CST | |
12876 | && (TREE_CODE (arg0) == PLUS_EXPR | |
12877 | || TREE_CODE (arg0) == MINUS_EXPR) | |
12878 | && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST | |
12879 | && 0 != (tem = const_binop (TREE_CODE (arg0) == PLUS_EXPR | |
12880 | ? MINUS_EXPR : PLUS_EXPR, | |
db3927fb AH |
12881 | fold_convert_loc (loc, TREE_TYPE (arg0), |
12882 | arg1), | |
43a5d30b | 12883 | TREE_OPERAND (arg0, 1))) |
455f14dd | 12884 | && !TREE_OVERFLOW (tem)) |
db3927fb | 12885 | return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0), tem); |
0eeb03e6 | 12886 | |
e26ec0bb RS |
12887 | /* Similarly for a NEGATE_EXPR. */ |
12888 | if (TREE_CODE (arg0) == NEGATE_EXPR | |
12889 | && TREE_CODE (arg1) == INTEGER_CST | |
2e64f8b8 JJ |
12890 | && 0 != (tem = negate_expr (fold_convert_loc (loc, TREE_TYPE (arg0), |
12891 | arg1))) | |
e26ec0bb | 12892 | && TREE_CODE (tem) == INTEGER_CST |
455f14dd | 12893 | && !TREE_OVERFLOW (tem)) |
db3927fb | 12894 | return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0), tem); |
0eeb03e6 | 12895 | |
cf06e5c1 RS |
12896 | /* Similarly for a BIT_XOR_EXPR; X ^ C1 == C2 is X == (C1 ^ C2). */ |
12897 | if (TREE_CODE (arg0) == BIT_XOR_EXPR | |
12898 | && TREE_CODE (arg1) == INTEGER_CST | |
12899 | && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST) | |
db3927fb AH |
12900 | return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0), |
12901 | fold_build2_loc (loc, BIT_XOR_EXPR, TREE_TYPE (arg0), | |
12902 | fold_convert_loc (loc, | |
12903 | TREE_TYPE (arg0), | |
12904 | arg1), | |
cf06e5c1 RS |
12905 | TREE_OPERAND (arg0, 1))); |
12906 | ||
6b12efe9 RG |
12907 | /* Transform comparisons of the form X +- Y CMP X to Y CMP 0. */ |
12908 | if ((TREE_CODE (arg0) == PLUS_EXPR | |
12909 | || TREE_CODE (arg0) == POINTER_PLUS_EXPR | |
12910 | || TREE_CODE (arg0) == MINUS_EXPR) | |
2e64f8b8 JJ |
12911 | && operand_equal_p (tree_strip_nop_conversions (TREE_OPERAND (arg0, |
12912 | 0)), | |
12913 | arg1, 0) | |
a31498d2 RG |
12914 | && (INTEGRAL_TYPE_P (TREE_TYPE (arg0)) |
12915 | || POINTER_TYPE_P (TREE_TYPE (arg0)))) | |
12916 | { | |
6b12efe9 | 12917 | tree val = TREE_OPERAND (arg0, 1); |
db3927fb AH |
12918 | return omit_two_operands_loc (loc, type, |
12919 | fold_build2_loc (loc, code, type, | |
6b12efe9 RG |
12920 | val, |
12921 | build_int_cst (TREE_TYPE (val), | |
12922 | 0)), | |
12923 | TREE_OPERAND (arg0, 0), arg1); | |
12924 | } | |
12925 | ||
12926 | /* Transform comparisons of the form C - X CMP X if C % 2 == 1. */ | |
12927 | if (TREE_CODE (arg0) == MINUS_EXPR | |
12928 | && TREE_CODE (TREE_OPERAND (arg0, 0)) == INTEGER_CST | |
2e64f8b8 JJ |
12929 | && operand_equal_p (tree_strip_nop_conversions (TREE_OPERAND (arg0, |
12930 | 1)), | |
12931 | arg1, 0) | |
6b12efe9 RG |
12932 | && (TREE_INT_CST_LOW (TREE_OPERAND (arg0, 0)) & 1) == 1) |
12933 | { | |
db3927fb | 12934 | return omit_two_operands_loc (loc, type, |
6b12efe9 RG |
12935 | code == NE_EXPR |
12936 | ? boolean_true_node : boolean_false_node, | |
12937 | TREE_OPERAND (arg0, 1), arg1); | |
a31498d2 RG |
12938 | } |
12939 | ||
e26ec0bb RS |
12940 | /* If we have X - Y == 0, we can convert that to X == Y and similarly |
12941 | for !=. Don't do this for ordered comparisons due to overflow. */ | |
12942 | if (TREE_CODE (arg0) == MINUS_EXPR | |
12943 | && integer_zerop (arg1)) | |
db3927fb | 12944 | return fold_build2_loc (loc, code, type, |
e26ec0bb | 12945 | TREE_OPERAND (arg0, 0), TREE_OPERAND (arg0, 1)); |
0eeb03e6 | 12946 | |
e26ec0bb RS |
12947 | /* Convert ABS_EXPR<x> == 0 or ABS_EXPR<x> != 0 to x == 0 or x != 0. */ |
12948 | if (TREE_CODE (arg0) == ABS_EXPR | |
12949 | && (integer_zerop (arg1) || real_zerop (arg1))) | |
db3927fb | 12950 | return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0), arg1); |
0eeb03e6 | 12951 | |
e26ec0bb RS |
12952 | /* If this is an EQ or NE comparison with zero and ARG0 is |
12953 | (1 << foo) & bar, convert it to (bar >> foo) & 1. Both require | |
12954 | two operations, but the latter can be done in one less insn | |
12955 | on machines that have only two-operand insns or on which a | |
12956 | constant cannot be the first operand. */ | |
12957 | if (TREE_CODE (arg0) == BIT_AND_EXPR | |
12958 | && integer_zerop (arg1)) | |
12959 | { | |
12960 | tree arg00 = TREE_OPERAND (arg0, 0); | |
12961 | tree arg01 = TREE_OPERAND (arg0, 1); | |
12962 | if (TREE_CODE (arg00) == LSHIFT_EXPR | |
12963 | && integer_onep (TREE_OPERAND (arg00, 0))) | |
5abe9685 | 12964 | { |
db3927fb | 12965 | tree tem = fold_build2_loc (loc, RSHIFT_EXPR, TREE_TYPE (arg00), |
5abe9685 | 12966 | arg01, TREE_OPERAND (arg00, 1)); |
db3927fb | 12967 | tem = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg0), tem, |
5abe9685 | 12968 | build_int_cst (TREE_TYPE (arg0), 1)); |
db3927fb AH |
12969 | return fold_build2_loc (loc, code, type, |
12970 | fold_convert_loc (loc, TREE_TYPE (arg1), tem), | |
12971 | arg1); | |
5abe9685 RG |
12972 | } |
12973 | else if (TREE_CODE (arg01) == LSHIFT_EXPR | |
12974 | && integer_onep (TREE_OPERAND (arg01, 0))) | |
12975 | { | |
db3927fb | 12976 | tree tem = fold_build2_loc (loc, RSHIFT_EXPR, TREE_TYPE (arg01), |
5abe9685 | 12977 | arg00, TREE_OPERAND (arg01, 1)); |
db3927fb | 12978 | tem = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg0), tem, |
5abe9685 | 12979 | build_int_cst (TREE_TYPE (arg0), 1)); |
db3927fb AH |
12980 | return fold_build2_loc (loc, code, type, |
12981 | fold_convert_loc (loc, TREE_TYPE (arg1), tem), | |
12982 | arg1); | |
5abe9685 | 12983 | } |
e26ec0bb RS |
12984 | } |
12985 | ||
12986 | /* If this is an NE or EQ comparison of zero against the result of a | |
12987 | signed MOD operation whose second operand is a power of 2, make | |
12988 | the MOD operation unsigned since it is simpler and equivalent. */ | |
12989 | if (integer_zerop (arg1) | |
12990 | && !TYPE_UNSIGNED (TREE_TYPE (arg0)) | |
12991 | && (TREE_CODE (arg0) == TRUNC_MOD_EXPR | |
12992 | || TREE_CODE (arg0) == CEIL_MOD_EXPR | |
12993 | || TREE_CODE (arg0) == FLOOR_MOD_EXPR | |
12994 | || TREE_CODE (arg0) == ROUND_MOD_EXPR) | |
12995 | && integer_pow2p (TREE_OPERAND (arg0, 1))) | |
12996 | { | |
ca5ba2a3 | 12997 | tree newtype = unsigned_type_for (TREE_TYPE (arg0)); |
db3927fb AH |
12998 | tree newmod = fold_build2_loc (loc, TREE_CODE (arg0), newtype, |
12999 | fold_convert_loc (loc, newtype, | |
13000 | TREE_OPERAND (arg0, 0)), | |
13001 | fold_convert_loc (loc, newtype, | |
13002 | TREE_OPERAND (arg0, 1))); | |
e26ec0bb | 13003 | |
db3927fb AH |
13004 | return fold_build2_loc (loc, code, type, newmod, |
13005 | fold_convert_loc (loc, newtype, arg1)); | |
e26ec0bb RS |
13006 | } |
13007 | ||
a861485c RS |
13008 | /* Fold ((X >> C1) & C2) == 0 and ((X >> C1) & C2) != 0 where |
13009 | C1 is a valid shift constant, and C2 is a power of two, i.e. | |
13010 | a single bit. */ | |
13011 | if (TREE_CODE (arg0) == BIT_AND_EXPR | |
13012 | && TREE_CODE (TREE_OPERAND (arg0, 0)) == RSHIFT_EXPR | |
13013 | && TREE_CODE (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1)) | |
13014 | == INTEGER_CST | |
13015 | && integer_pow2p (TREE_OPERAND (arg0, 1)) | |
13016 | && integer_zerop (arg1)) | |
13017 | { | |
13018 | tree itype = TREE_TYPE (arg0); | |
a861485c | 13019 | tree arg001 = TREE_OPERAND (TREE_OPERAND (arg0, 0), 1); |
a5e0cd1d | 13020 | prec = TYPE_PRECISION (itype); |
a861485c RS |
13021 | |
13022 | /* Check for a valid shift count. */ | |
13023 | if (TREE_INT_CST_HIGH (arg001) == 0 | |
13024 | && TREE_INT_CST_LOW (arg001) < prec) | |
13025 | { | |
13026 | tree arg01 = TREE_OPERAND (arg0, 1); | |
13027 | tree arg000 = TREE_OPERAND (TREE_OPERAND (arg0, 0), 0); | |
13028 | unsigned HOST_WIDE_INT log2 = tree_log2 (arg01); | |
13029 | /* If (C2 << C1) doesn't overflow, then ((X >> C1) & C2) != 0 | |
13030 | can be rewritten as (X & (C2 << C1)) != 0. */ | |
0ad12cd3 | 13031 | if ((log2 + TREE_INT_CST_LOW (arg001)) < prec) |
a861485c | 13032 | { |
db3927fb AH |
13033 | tem = fold_build2_loc (loc, LSHIFT_EXPR, itype, arg01, arg001); |
13034 | tem = fold_build2_loc (loc, BIT_AND_EXPR, itype, arg000, tem); | |
29f10e71 JJ |
13035 | return fold_build2_loc (loc, code, type, tem, |
13036 | fold_convert_loc (loc, itype, arg1)); | |
a861485c RS |
13037 | } |
13038 | /* Otherwise, for signed (arithmetic) shifts, | |
13039 | ((X >> C1) & C2) != 0 is rewritten as X < 0, and | |
13040 | ((X >> C1) & C2) == 0 is rewritten as X >= 0. */ | |
13041 | else if (!TYPE_UNSIGNED (itype)) | |
db3927fb | 13042 | return fold_build2_loc (loc, code == EQ_EXPR ? GE_EXPR : LT_EXPR, type, |
a861485c RS |
13043 | arg000, build_int_cst (itype, 0)); |
13044 | /* Otherwise, of unsigned (logical) shifts, | |
13045 | ((X >> C1) & C2) != 0 is rewritten as (X,false), and | |
13046 | ((X >> C1) & C2) == 0 is rewritten as (X,true). */ | |
13047 | else | |
db3927fb | 13048 | return omit_one_operand_loc (loc, type, |
a861485c RS |
13049 | code == EQ_EXPR ? integer_one_node |
13050 | : integer_zero_node, | |
13051 | arg000); | |
13052 | } | |
13053 | } | |
13054 | ||
e26ec0bb RS |
13055 | /* If we have (A & C) == C where C is a power of 2, convert this into |
13056 | (A & C) != 0. Similarly for NE_EXPR. */ | |
13057 | if (TREE_CODE (arg0) == BIT_AND_EXPR | |
13058 | && integer_pow2p (TREE_OPERAND (arg0, 1)) | |
13059 | && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0)) | |
db3927fb AH |
13060 | return fold_build2_loc (loc, code == EQ_EXPR ? NE_EXPR : EQ_EXPR, type, |
13061 | arg0, fold_convert_loc (loc, TREE_TYPE (arg0), | |
13062 | integer_zero_node)); | |
e26ec0bb RS |
13063 | |
13064 | /* If we have (A & C) != 0 or (A & C) == 0 and C is the sign | |
13065 | bit, then fold the expression into A < 0 or A >= 0. */ | |
db3927fb | 13066 | tem = fold_single_bit_test_into_sign_test (loc, code, arg0, arg1, type); |
e26ec0bb RS |
13067 | if (tem) |
13068 | return tem; | |
13069 | ||
13070 | /* If we have (A & C) == D where D & ~C != 0, convert this into 0. | |
13071 | Similarly for NE_EXPR. */ | |
13072 | if (TREE_CODE (arg0) == BIT_AND_EXPR | |
13073 | && TREE_CODE (arg1) == INTEGER_CST | |
13074 | && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST) | |
13075 | { | |
db3927fb | 13076 | tree notc = fold_build1_loc (loc, BIT_NOT_EXPR, |
e26ec0bb RS |
13077 | TREE_TYPE (TREE_OPERAND (arg0, 1)), |
13078 | TREE_OPERAND (arg0, 1)); | |
29f10e71 JJ |
13079 | tree dandnotc |
13080 | = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg0), | |
13081 | fold_convert_loc (loc, TREE_TYPE (arg0), arg1), | |
13082 | notc); | |
e26ec0bb RS |
13083 | tree rslt = code == EQ_EXPR ? integer_zero_node : integer_one_node; |
13084 | if (integer_nonzerop (dandnotc)) | |
db3927fb | 13085 | return omit_one_operand_loc (loc, type, rslt, arg0); |
e26ec0bb RS |
13086 | } |
13087 | ||
13088 | /* If we have (A | C) == D where C & ~D != 0, convert this into 0. | |
13089 | Similarly for NE_EXPR. */ | |
13090 | if (TREE_CODE (arg0) == BIT_IOR_EXPR | |
13091 | && TREE_CODE (arg1) == INTEGER_CST | |
13092 | && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST) | |
13093 | { | |
db3927fb | 13094 | tree notd = fold_build1_loc (loc, BIT_NOT_EXPR, TREE_TYPE (arg1), arg1); |
29f10e71 JJ |
13095 | tree candnotd |
13096 | = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg0), | |
13097 | TREE_OPERAND (arg0, 1), | |
13098 | fold_convert_loc (loc, TREE_TYPE (arg0), notd)); | |
e26ec0bb RS |
13099 | tree rslt = code == EQ_EXPR ? integer_zero_node : integer_one_node; |
13100 | if (integer_nonzerop (candnotd)) | |
db3927fb | 13101 | return omit_one_operand_loc (loc, type, rslt, arg0); |
e26ec0bb RS |
13102 | } |
13103 | ||
45dc13b9 JJ |
13104 | /* If this is a comparison of a field, we may be able to simplify it. */ |
13105 | if ((TREE_CODE (arg0) == COMPONENT_REF | |
13106 | || TREE_CODE (arg0) == BIT_FIELD_REF) | |
13107 | /* Handle the constant case even without -O | |
13108 | to make sure the warnings are given. */ | |
13109 | && (optimize || TREE_CODE (arg1) == INTEGER_CST)) | |
13110 | { | |
db3927fb | 13111 | t1 = optimize_bit_field_compare (loc, code, type, arg0, arg1); |
45dc13b9 JJ |
13112 | if (t1) |
13113 | return t1; | |
13114 | } | |
13115 | ||
e26ec0bb RS |
13116 | /* Optimize comparisons of strlen vs zero to a compare of the |
13117 | first character of the string vs zero. To wit, | |
13118 | strlen(ptr) == 0 => *ptr == 0 | |
13119 | strlen(ptr) != 0 => *ptr != 0 | |
13120 | Other cases should reduce to one of these two (or a constant) | |
13121 | due to the return value of strlen being unsigned. */ | |
13122 | if (TREE_CODE (arg0) == CALL_EXPR | |
13123 | && integer_zerop (arg1)) | |
13124 | { | |
13125 | tree fndecl = get_callee_fndecl (arg0); | |
e26ec0bb RS |
13126 | |
13127 | if (fndecl | |
13128 | && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL | |
13129 | && DECL_FUNCTION_CODE (fndecl) == BUILT_IN_STRLEN | |
5039610b SL |
13130 | && call_expr_nargs (arg0) == 1 |
13131 | && TREE_CODE (TREE_TYPE (CALL_EXPR_ARG (arg0, 0))) == POINTER_TYPE) | |
e26ec0bb | 13132 | { |
db3927fb AH |
13133 | tree iref = build_fold_indirect_ref_loc (loc, |
13134 | CALL_EXPR_ARG (arg0, 0)); | |
13135 | return fold_build2_loc (loc, code, type, iref, | |
e26ec0bb RS |
13136 | build_int_cst (TREE_TYPE (iref), 0)); |
13137 | } | |
13138 | } | |
13139 | ||
13140 | /* Fold (X >> C) != 0 into X < 0 if C is one less than the width | |
13141 | of X. Similarly fold (X >> C) == 0 into X >= 0. */ | |
13142 | if (TREE_CODE (arg0) == RSHIFT_EXPR | |
13143 | && integer_zerop (arg1) | |
13144 | && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST) | |
13145 | { | |
13146 | tree arg00 = TREE_OPERAND (arg0, 0); | |
13147 | tree arg01 = TREE_OPERAND (arg0, 1); | |
13148 | tree itype = TREE_TYPE (arg00); | |
13149 | if (TREE_INT_CST_HIGH (arg01) == 0 | |
13150 | && TREE_INT_CST_LOW (arg01) | |
13151 | == (unsigned HOST_WIDE_INT) (TYPE_PRECISION (itype) - 1)) | |
13152 | { | |
13153 | if (TYPE_UNSIGNED (itype)) | |
13154 | { | |
12753674 | 13155 | itype = signed_type_for (itype); |
db3927fb | 13156 | arg00 = fold_convert_loc (loc, itype, arg00); |
e26ec0bb | 13157 | } |
db3927fb | 13158 | return fold_build2_loc (loc, code == EQ_EXPR ? GE_EXPR : LT_EXPR, |
a212e43f | 13159 | type, arg00, build_zero_cst (itype)); |
e26ec0bb RS |
13160 | } |
13161 | } | |
13162 | ||
eb8dffe0 RS |
13163 | /* (X ^ Y) == 0 becomes X == Y, and (X ^ Y) != 0 becomes X != Y. */ |
13164 | if (integer_zerop (arg1) | |
13165 | && TREE_CODE (arg0) == BIT_XOR_EXPR) | |
db3927fb | 13166 | return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0), |
eb8dffe0 RS |
13167 | TREE_OPERAND (arg0, 1)); |
13168 | ||
13169 | /* (X ^ Y) == Y becomes X == 0. We know that Y has no side-effects. */ | |
13170 | if (TREE_CODE (arg0) == BIT_XOR_EXPR | |
13171 | && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0)) | |
db3927fb | 13172 | return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0), |
27edb974 | 13173 | build_zero_cst (TREE_TYPE (arg0))); |
eb8dffe0 RS |
13174 | /* Likewise (X ^ Y) == X becomes Y == 0. X has no side-effects. */ |
13175 | if (TREE_CODE (arg0) == BIT_XOR_EXPR | |
13176 | && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0) | |
13177 | && reorder_operands_p (TREE_OPERAND (arg0, 1), arg1)) | |
db3927fb | 13178 | return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 1), |
27edb974 | 13179 | build_zero_cst (TREE_TYPE (arg0))); |
eb8dffe0 RS |
13180 | |
13181 | /* (X ^ C1) op C2 can be rewritten as X op (C1 ^ C2). */ | |
13182 | if (TREE_CODE (arg0) == BIT_XOR_EXPR | |
13183 | && TREE_CODE (arg1) == INTEGER_CST | |
13184 | && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST) | |
db3927fb AH |
13185 | return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0), |
13186 | fold_build2_loc (loc, BIT_XOR_EXPR, TREE_TYPE (arg1), | |
eb8dffe0 RS |
13187 | TREE_OPERAND (arg0, 1), arg1)); |
13188 | ||
5881ad5d RS |
13189 | /* Fold (~X & C) == 0 into (X & C) != 0 and (~X & C) != 0 into |
13190 | (X & C) == 0 when C is a single bit. */ | |
13191 | if (TREE_CODE (arg0) == BIT_AND_EXPR | |
13192 | && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_NOT_EXPR | |
13193 | && integer_zerop (arg1) | |
13194 | && integer_pow2p (TREE_OPERAND (arg0, 1))) | |
13195 | { | |
db3927fb | 13196 | tem = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg0), |
29f10e71 JJ |
13197 | TREE_OPERAND (TREE_OPERAND (arg0, 0), 0), |
13198 | TREE_OPERAND (arg0, 1)); | |
db3927fb | 13199 | return fold_build2_loc (loc, code == EQ_EXPR ? NE_EXPR : EQ_EXPR, |
29f10e71 JJ |
13200 | type, tem, |
13201 | fold_convert_loc (loc, TREE_TYPE (arg0), | |
13202 | arg1)); | |
5881ad5d RS |
13203 | } |
13204 | ||
13205 | /* Fold ((X & C) ^ C) eq/ne 0 into (X & C) ne/eq 0, when the | |
13206 | constant C is a power of two, i.e. a single bit. */ | |
13207 | if (TREE_CODE (arg0) == BIT_XOR_EXPR | |
13208 | && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_AND_EXPR | |
13209 | && integer_zerop (arg1) | |
13210 | && integer_pow2p (TREE_OPERAND (arg0, 1)) | |
13211 | && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1), | |
13212 | TREE_OPERAND (arg0, 1), OEP_ONLY_CONST)) | |
13213 | { | |
13214 | tree arg00 = TREE_OPERAND (arg0, 0); | |
db3927fb | 13215 | return fold_build2_loc (loc, code == EQ_EXPR ? NE_EXPR : EQ_EXPR, type, |
5881ad5d RS |
13216 | arg00, build_int_cst (TREE_TYPE (arg00), 0)); |
13217 | } | |
13218 | ||
13219 | /* Likewise, fold ((X ^ C) & C) eq/ne 0 into (X & C) ne/eq 0, | |
13220 | when is C is a power of two, i.e. a single bit. */ | |
13221 | if (TREE_CODE (arg0) == BIT_AND_EXPR | |
13222 | && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_XOR_EXPR | |
13223 | && integer_zerop (arg1) | |
13224 | && integer_pow2p (TREE_OPERAND (arg0, 1)) | |
13225 | && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1), | |
13226 | TREE_OPERAND (arg0, 1), OEP_ONLY_CONST)) | |
13227 | { | |
13228 | tree arg000 = TREE_OPERAND (TREE_OPERAND (arg0, 0), 0); | |
db3927fb | 13229 | tem = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg000), |
5881ad5d | 13230 | arg000, TREE_OPERAND (arg0, 1)); |
db3927fb | 13231 | return fold_build2_loc (loc, code == EQ_EXPR ? NE_EXPR : EQ_EXPR, type, |
5881ad5d RS |
13232 | tem, build_int_cst (TREE_TYPE (tem), 0)); |
13233 | } | |
13234 | ||
e26ec0bb RS |
13235 | if (integer_zerop (arg1) |
13236 | && tree_expr_nonzero_p (arg0)) | |
13237 | { | |
13238 | tree res = constant_boolean_node (code==NE_EXPR, type); | |
db3927fb | 13239 | return omit_one_operand_loc (loc, type, res, arg0); |
e26ec0bb | 13240 | } |
c159ffe7 RS |
13241 | |
13242 | /* Fold -X op -Y as X op Y, where op is eq/ne. */ | |
13243 | if (TREE_CODE (arg0) == NEGATE_EXPR | |
13244 | && TREE_CODE (arg1) == NEGATE_EXPR) | |
db3927fb | 13245 | return fold_build2_loc (loc, code, type, |
29f10e71 JJ |
13246 | TREE_OPERAND (arg0, 0), |
13247 | fold_convert_loc (loc, TREE_TYPE (arg0), | |
13248 | TREE_OPERAND (arg1, 0))); | |
c159ffe7 | 13249 | |
015e23f4 RS |
13250 | /* Fold (X & C) op (Y & C) as (X ^ Y) & C op 0", and symmetries. */ |
13251 | if (TREE_CODE (arg0) == BIT_AND_EXPR | |
13252 | && TREE_CODE (arg1) == BIT_AND_EXPR) | |
13253 | { | |
13254 | tree arg00 = TREE_OPERAND (arg0, 0); | |
13255 | tree arg01 = TREE_OPERAND (arg0, 1); | |
13256 | tree arg10 = TREE_OPERAND (arg1, 0); | |
13257 | tree arg11 = TREE_OPERAND (arg1, 1); | |
13258 | tree itype = TREE_TYPE (arg0); | |
13259 | ||
13260 | if (operand_equal_p (arg01, arg11, 0)) | |
db3927fb AH |
13261 | return fold_build2_loc (loc, code, type, |
13262 | fold_build2_loc (loc, BIT_AND_EXPR, itype, | |
13263 | fold_build2_loc (loc, | |
13264 | BIT_XOR_EXPR, itype, | |
015e23f4 RS |
13265 | arg00, arg10), |
13266 | arg01), | |
27edb974 | 13267 | build_zero_cst (itype)); |
015e23f4 RS |
13268 | |
13269 | if (operand_equal_p (arg01, arg10, 0)) | |
db3927fb AH |
13270 | return fold_build2_loc (loc, code, type, |
13271 | fold_build2_loc (loc, BIT_AND_EXPR, itype, | |
13272 | fold_build2_loc (loc, | |
13273 | BIT_XOR_EXPR, itype, | |
015e23f4 RS |
13274 | arg00, arg11), |
13275 | arg01), | |
27edb974 | 13276 | build_zero_cst (itype)); |
015e23f4 RS |
13277 | |
13278 | if (operand_equal_p (arg00, arg11, 0)) | |
db3927fb AH |
13279 | return fold_build2_loc (loc, code, type, |
13280 | fold_build2_loc (loc, BIT_AND_EXPR, itype, | |
13281 | fold_build2_loc (loc, | |
13282 | BIT_XOR_EXPR, itype, | |
015e23f4 RS |
13283 | arg01, arg10), |
13284 | arg00), | |
27edb974 | 13285 | build_zero_cst (itype)); |
015e23f4 RS |
13286 | |
13287 | if (operand_equal_p (arg00, arg10, 0)) | |
db3927fb AH |
13288 | return fold_build2_loc (loc, code, type, |
13289 | fold_build2_loc (loc, BIT_AND_EXPR, itype, | |
13290 | fold_build2_loc (loc, | |
13291 | BIT_XOR_EXPR, itype, | |
015e23f4 RS |
13292 | arg01, arg11), |
13293 | arg00), | |
27edb974 | 13294 | build_zero_cst (itype)); |
015e23f4 RS |
13295 | } |
13296 | ||
cf06e5c1 RS |
13297 | if (TREE_CODE (arg0) == BIT_XOR_EXPR |
13298 | && TREE_CODE (arg1) == BIT_XOR_EXPR) | |
13299 | { | |
13300 | tree arg00 = TREE_OPERAND (arg0, 0); | |
13301 | tree arg01 = TREE_OPERAND (arg0, 1); | |
13302 | tree arg10 = TREE_OPERAND (arg1, 0); | |
13303 | tree arg11 = TREE_OPERAND (arg1, 1); | |
13304 | tree itype = TREE_TYPE (arg0); | |
13305 | ||
13306 | /* Optimize (X ^ Z) op (Y ^ Z) as X op Y, and symmetries. | |
13307 | operand_equal_p guarantees no side-effects so we don't need | |
13308 | to use omit_one_operand on Z. */ | |
13309 | if (operand_equal_p (arg01, arg11, 0)) | |
8a87e7ab JJ |
13310 | return fold_build2_loc (loc, code, type, arg00, |
13311 | fold_convert_loc (loc, TREE_TYPE (arg00), | |
13312 | arg10)); | |
cf06e5c1 | 13313 | if (operand_equal_p (arg01, arg10, 0)) |
8a87e7ab JJ |
13314 | return fold_build2_loc (loc, code, type, arg00, |
13315 | fold_convert_loc (loc, TREE_TYPE (arg00), | |
13316 | arg11)); | |
cf06e5c1 | 13317 | if (operand_equal_p (arg00, arg11, 0)) |
8a87e7ab JJ |
13318 | return fold_build2_loc (loc, code, type, arg01, |
13319 | fold_convert_loc (loc, TREE_TYPE (arg01), | |
13320 | arg10)); | |
cf06e5c1 | 13321 | if (operand_equal_p (arg00, arg10, 0)) |
8a87e7ab JJ |
13322 | return fold_build2_loc (loc, code, type, arg01, |
13323 | fold_convert_loc (loc, TREE_TYPE (arg01), | |
13324 | arg11)); | |
cf06e5c1 RS |
13325 | |
13326 | /* Optimize (X ^ C1) op (Y ^ C2) as (X ^ (C1 ^ C2)) op Y. */ | |
13327 | if (TREE_CODE (arg01) == INTEGER_CST | |
13328 | && TREE_CODE (arg11) == INTEGER_CST) | |
29f10e71 JJ |
13329 | { |
13330 | tem = fold_build2_loc (loc, BIT_XOR_EXPR, itype, arg01, | |
13331 | fold_convert_loc (loc, itype, arg11)); | |
13332 | tem = fold_build2_loc (loc, BIT_XOR_EXPR, itype, arg00, tem); | |
13333 | return fold_build2_loc (loc, code, type, tem, | |
13334 | fold_convert_loc (loc, itype, arg10)); | |
13335 | } | |
cf06e5c1 | 13336 | } |
23b9463b RS |
13337 | |
13338 | /* Attempt to simplify equality/inequality comparisons of complex | |
13339 | values. Only lower the comparison if the result is known or | |
13340 | can be simplified to a single scalar comparison. */ | |
13341 | if ((TREE_CODE (arg0) == COMPLEX_EXPR | |
13342 | || TREE_CODE (arg0) == COMPLEX_CST) | |
13343 | && (TREE_CODE (arg1) == COMPLEX_EXPR | |
13344 | || TREE_CODE (arg1) == COMPLEX_CST)) | |
13345 | { | |
13346 | tree real0, imag0, real1, imag1; | |
13347 | tree rcond, icond; | |
13348 | ||
13349 | if (TREE_CODE (arg0) == COMPLEX_EXPR) | |
13350 | { | |
13351 | real0 = TREE_OPERAND (arg0, 0); | |
13352 | imag0 = TREE_OPERAND (arg0, 1); | |
13353 | } | |
13354 | else | |
13355 | { | |
13356 | real0 = TREE_REALPART (arg0); | |
13357 | imag0 = TREE_IMAGPART (arg0); | |
13358 | } | |
13359 | ||
13360 | if (TREE_CODE (arg1) == COMPLEX_EXPR) | |
13361 | { | |
13362 | real1 = TREE_OPERAND (arg1, 0); | |
13363 | imag1 = TREE_OPERAND (arg1, 1); | |
13364 | } | |
13365 | else | |
13366 | { | |
13367 | real1 = TREE_REALPART (arg1); | |
13368 | imag1 = TREE_IMAGPART (arg1); | |
13369 | } | |
13370 | ||
db3927fb | 13371 | rcond = fold_binary_loc (loc, code, type, real0, real1); |
23b9463b RS |
13372 | if (rcond && TREE_CODE (rcond) == INTEGER_CST) |
13373 | { | |
13374 | if (integer_zerop (rcond)) | |
13375 | { | |
13376 | if (code == EQ_EXPR) | |
db3927fb | 13377 | return omit_two_operands_loc (loc, type, boolean_false_node, |
23b9463b | 13378 | imag0, imag1); |
db3927fb | 13379 | return fold_build2_loc (loc, NE_EXPR, type, imag0, imag1); |
23b9463b RS |
13380 | } |
13381 | else | |
13382 | { | |
13383 | if (code == NE_EXPR) | |
db3927fb | 13384 | return omit_two_operands_loc (loc, type, boolean_true_node, |
23b9463b | 13385 | imag0, imag1); |
db3927fb | 13386 | return fold_build2_loc (loc, EQ_EXPR, type, imag0, imag1); |
23b9463b RS |
13387 | } |
13388 | } | |
13389 | ||
db3927fb | 13390 | icond = fold_binary_loc (loc, code, type, imag0, imag1); |
23b9463b RS |
13391 | if (icond && TREE_CODE (icond) == INTEGER_CST) |
13392 | { | |
13393 | if (integer_zerop (icond)) | |
13394 | { | |
13395 | if (code == EQ_EXPR) | |
db3927fb | 13396 | return omit_two_operands_loc (loc, type, boolean_false_node, |
23b9463b | 13397 | real0, real1); |
db3927fb | 13398 | return fold_build2_loc (loc, NE_EXPR, type, real0, real1); |
23b9463b RS |
13399 | } |
13400 | else | |
13401 | { | |
13402 | if (code == NE_EXPR) | |
db3927fb | 13403 | return omit_two_operands_loc (loc, type, boolean_true_node, |
23b9463b | 13404 | real0, real1); |
db3927fb | 13405 | return fold_build2_loc (loc, EQ_EXPR, type, real0, real1); |
23b9463b RS |
13406 | } |
13407 | } | |
13408 | } | |
13409 | ||
e26ec0bb RS |
13410 | return NULL_TREE; |
13411 | ||
13412 | case LT_EXPR: | |
13413 | case GT_EXPR: | |
13414 | case LE_EXPR: | |
13415 | case GE_EXPR: | |
db3927fb | 13416 | tem = fold_comparison (loc, code, type, op0, op1); |
e26ec0bb RS |
13417 | if (tem != NULL_TREE) |
13418 | return tem; | |
13419 | ||
13420 | /* Transform comparisons of the form X +- C CMP X. */ | |
13421 | if ((TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR) | |
13422 | && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0) | |
13423 | && ((TREE_CODE (TREE_OPERAND (arg0, 1)) == REAL_CST | |
13424 | && !HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))) | |
13425 | || (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST | |
eeef0e45 | 13426 | && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1))))) |
e26ec0bb RS |
13427 | { |
13428 | tree arg01 = TREE_OPERAND (arg0, 1); | |
13429 | enum tree_code code0 = TREE_CODE (arg0); | |
13430 | int is_positive; | |
13431 | ||
13432 | if (TREE_CODE (arg01) == REAL_CST) | |
13433 | is_positive = REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg01)) ? -1 : 1; | |
13434 | else | |
13435 | is_positive = tree_int_cst_sgn (arg01); | |
13436 | ||
13437 | /* (X - c) > X becomes false. */ | |
13438 | if (code == GT_EXPR | |
13439 | && ((code0 == MINUS_EXPR && is_positive >= 0) | |
13440 | || (code0 == PLUS_EXPR && is_positive <= 0))) | |
6ac01510 ILT |
13441 | { |
13442 | if (TREE_CODE (arg01) == INTEGER_CST | |
13443 | && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1))) | |
13444 | fold_overflow_warning (("assuming signed overflow does not " | |
13445 | "occur when assuming that (X - c) > X " | |
13446 | "is always false"), | |
13447 | WARN_STRICT_OVERFLOW_ALL); | |
13448 | return constant_boolean_node (0, type); | |
13449 | } | |
e26ec0bb RS |
13450 | |
13451 | /* Likewise (X + c) < X becomes false. */ | |
13452 | if (code == LT_EXPR | |
13453 | && ((code0 == PLUS_EXPR && is_positive >= 0) | |
13454 | || (code0 == MINUS_EXPR && is_positive <= 0))) | |
6ac01510 ILT |
13455 | { |
13456 | if (TREE_CODE (arg01) == INTEGER_CST | |
13457 | && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1))) | |
13458 | fold_overflow_warning (("assuming signed overflow does not " | |
13459 | "occur when assuming that " | |
13460 | "(X + c) < X is always false"), | |
13461 | WARN_STRICT_OVERFLOW_ALL); | |
13462 | return constant_boolean_node (0, type); | |
13463 | } | |
e26ec0bb RS |
13464 | |
13465 | /* Convert (X - c) <= X to true. */ | |
13466 | if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg1))) | |
13467 | && code == LE_EXPR | |
0eeb03e6 JM |
13468 | && ((code0 == MINUS_EXPR && is_positive >= 0) |
13469 | || (code0 == PLUS_EXPR && is_positive <= 0))) | |
6ac01510 ILT |
13470 | { |
13471 | if (TREE_CODE (arg01) == INTEGER_CST | |
13472 | && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1))) | |
13473 | fold_overflow_warning (("assuming signed overflow does not " | |
13474 | "occur when assuming that " | |
13475 | "(X - c) <= X is always true"), | |
13476 | WARN_STRICT_OVERFLOW_ALL); | |
13477 | return constant_boolean_node (1, type); | |
13478 | } | |
0eeb03e6 JM |
13479 | |
13480 | /* Convert (X + c) >= X to true. */ | |
13481 | if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg1))) | |
13482 | && code == GE_EXPR | |
13483 | && ((code0 == PLUS_EXPR && is_positive >= 0) | |
13484 | || (code0 == MINUS_EXPR && is_positive <= 0))) | |
6ac01510 ILT |
13485 | { |
13486 | if (TREE_CODE (arg01) == INTEGER_CST | |
13487 | && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1))) | |
13488 | fold_overflow_warning (("assuming signed overflow does not " | |
13489 | "occur when assuming that " | |
13490 | "(X + c) >= X is always true"), | |
13491 | WARN_STRICT_OVERFLOW_ALL); | |
13492 | return constant_boolean_node (1, type); | |
13493 | } | |
0eeb03e6 JM |
13494 | |
13495 | if (TREE_CODE (arg01) == INTEGER_CST) | |
13496 | { | |
13497 | /* Convert X + c > X and X - c < X to true for integers. */ | |
13498 | if (code == GT_EXPR | |
13499 | && ((code0 == PLUS_EXPR && is_positive > 0) | |
13500 | || (code0 == MINUS_EXPR && is_positive < 0))) | |
6ac01510 ILT |
13501 | { |
13502 | if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1))) | |
13503 | fold_overflow_warning (("assuming signed overflow does " | |
13504 | "not occur when assuming that " | |
13505 | "(X + c) > X is always true"), | |
13506 | WARN_STRICT_OVERFLOW_ALL); | |
13507 | return constant_boolean_node (1, type); | |
13508 | } | |
0eeb03e6 JM |
13509 | |
13510 | if (code == LT_EXPR | |
13511 | && ((code0 == MINUS_EXPR && is_positive > 0) | |
13512 | || (code0 == PLUS_EXPR && is_positive < 0))) | |
6ac01510 ILT |
13513 | { |
13514 | if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1))) | |
13515 | fold_overflow_warning (("assuming signed overflow does " | |
13516 | "not occur when assuming that " | |
13517 | "(X - c) < X is always true"), | |
13518 | WARN_STRICT_OVERFLOW_ALL); | |
13519 | return constant_boolean_node (1, type); | |
13520 | } | |
0eeb03e6 JM |
13521 | |
13522 | /* Convert X + c <= X and X - c >= X to false for integers. */ | |
13523 | if (code == LE_EXPR | |
13524 | && ((code0 == PLUS_EXPR && is_positive > 0) | |
13525 | || (code0 == MINUS_EXPR && is_positive < 0))) | |
6ac01510 ILT |
13526 | { |
13527 | if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1))) | |
13528 | fold_overflow_warning (("assuming signed overflow does " | |
13529 | "not occur when assuming that " | |
13530 | "(X + c) <= X is always false"), | |
13531 | WARN_STRICT_OVERFLOW_ALL); | |
13532 | return constant_boolean_node (0, type); | |
13533 | } | |
0eeb03e6 JM |
13534 | |
13535 | if (code == GE_EXPR | |
13536 | && ((code0 == MINUS_EXPR && is_positive > 0) | |
13537 | || (code0 == PLUS_EXPR && is_positive < 0))) | |
6ac01510 ILT |
13538 | { |
13539 | if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1))) | |
13540 | fold_overflow_warning (("assuming signed overflow does " | |
13541 | "not occur when assuming that " | |
f870ab63 | 13542 | "(X - c) >= X is always false"), |
6ac01510 ILT |
13543 | WARN_STRICT_OVERFLOW_ALL); |
13544 | return constant_boolean_node (0, type); | |
13545 | } | |
0eeb03e6 JM |
13546 | } |
13547 | } | |
13548 | ||
0aee4751 | 13549 | /* Comparisons with the highest or lowest possible integer of |
f0dbdfbb | 13550 | the specified precision will have known values. */ |
0aee4751 | 13551 | { |
f0dbdfbb EB |
13552 | tree arg1_type = TREE_TYPE (arg1); |
13553 | unsigned int width = TYPE_PRECISION (arg1_type); | |
0aee4751 KH |
13554 | |
13555 | if (TREE_CODE (arg1) == INTEGER_CST | |
49ab6098 | 13556 | && width <= HOST_BITS_PER_DOUBLE_INT |
f0dbdfbb | 13557 | && (INTEGRAL_TYPE_P (arg1_type) || POINTER_TYPE_P (arg1_type))) |
0aee4751 KH |
13558 | { |
13559 | HOST_WIDE_INT signed_max_hi; | |
13560 | unsigned HOST_WIDE_INT signed_max_lo; | |
13561 | unsigned HOST_WIDE_INT max_hi, max_lo, min_hi, min_lo; | |
13562 | ||
13563 | if (width <= HOST_BITS_PER_WIDE_INT) | |
13564 | { | |
13565 | signed_max_lo = ((unsigned HOST_WIDE_INT) 1 << (width - 1)) | |
13566 | - 1; | |
13567 | signed_max_hi = 0; | |
13568 | max_hi = 0; | |
13569 | ||
f0dbdfbb | 13570 | if (TYPE_UNSIGNED (arg1_type)) |
0aee4751 KH |
13571 | { |
13572 | max_lo = ((unsigned HOST_WIDE_INT) 2 << (width - 1)) - 1; | |
13573 | min_lo = 0; | |
13574 | min_hi = 0; | |
13575 | } | |
13576 | else | |
13577 | { | |
13578 | max_lo = signed_max_lo; | |
0cadbfaa | 13579 | min_lo = (HOST_WIDE_INT_M1U << (width - 1)); |
0aee4751 KH |
13580 | min_hi = -1; |
13581 | } | |
13582 | } | |
13583 | else | |
13584 | { | |
13585 | width -= HOST_BITS_PER_WIDE_INT; | |
13586 | signed_max_lo = -1; | |
13587 | signed_max_hi = ((unsigned HOST_WIDE_INT) 1 << (width - 1)) | |
13588 | - 1; | |
13589 | max_lo = -1; | |
13590 | min_lo = 0; | |
13591 | ||
f0dbdfbb | 13592 | if (TYPE_UNSIGNED (arg1_type)) |
0aee4751 KH |
13593 | { |
13594 | max_hi = ((unsigned HOST_WIDE_INT) 2 << (width - 1)) - 1; | |
13595 | min_hi = 0; | |
13596 | } | |
13597 | else | |
13598 | { | |
13599 | max_hi = signed_max_hi; | |
0cadbfaa | 13600 | min_hi = (HOST_WIDE_INT_M1U << (width - 1)); |
0aee4751 KH |
13601 | } |
13602 | } | |
13603 | ||
13604 | if ((unsigned HOST_WIDE_INT) TREE_INT_CST_HIGH (arg1) == max_hi | |
13605 | && TREE_INT_CST_LOW (arg1) == max_lo) | |
13606 | switch (code) | |
13607 | { | |
13608 | case GT_EXPR: | |
db3927fb | 13609 | return omit_one_operand_loc (loc, type, integer_zero_node, arg0); |
0aee4751 KH |
13610 | |
13611 | case GE_EXPR: | |
db3927fb | 13612 | return fold_build2_loc (loc, EQ_EXPR, type, op0, op1); |
0aee4751 KH |
13613 | |
13614 | case LE_EXPR: | |
db3927fb | 13615 | return omit_one_operand_loc (loc, type, integer_one_node, arg0); |
0aee4751 KH |
13616 | |
13617 | case LT_EXPR: | |
db3927fb | 13618 | return fold_build2_loc (loc, NE_EXPR, type, op0, op1); |
0aee4751 KH |
13619 | |
13620 | /* The GE_EXPR and LT_EXPR cases above are not normally | |
13621 | reached because of previous transformations. */ | |
13622 | ||
13623 | default: | |
13624 | break; | |
13625 | } | |
13626 | else if ((unsigned HOST_WIDE_INT) TREE_INT_CST_HIGH (arg1) | |
13627 | == max_hi | |
13628 | && TREE_INT_CST_LOW (arg1) == max_lo - 1) | |
13629 | switch (code) | |
13630 | { | |
13631 | case GT_EXPR: | |
000d8d44 | 13632 | arg1 = const_binop (PLUS_EXPR, arg1, |
43a5d30b | 13633 | build_int_cst (TREE_TYPE (arg1), 1)); |
db3927fb AH |
13634 | return fold_build2_loc (loc, EQ_EXPR, type, |
13635 | fold_convert_loc (loc, | |
13636 | TREE_TYPE (arg1), arg0), | |
86122f72 | 13637 | arg1); |
0aee4751 | 13638 | case LE_EXPR: |
000d8d44 | 13639 | arg1 = const_binop (PLUS_EXPR, arg1, |
43a5d30b | 13640 | build_int_cst (TREE_TYPE (arg1), 1)); |
db3927fb AH |
13641 | return fold_build2_loc (loc, NE_EXPR, type, |
13642 | fold_convert_loc (loc, TREE_TYPE (arg1), | |
13643 | arg0), | |
86122f72 | 13644 | arg1); |
0aee4751 KH |
13645 | default: |
13646 | break; | |
13647 | } | |
13648 | else if ((unsigned HOST_WIDE_INT) TREE_INT_CST_HIGH (arg1) | |
13649 | == min_hi | |
13650 | && TREE_INT_CST_LOW (arg1) == min_lo) | |
13651 | switch (code) | |
13652 | { | |
13653 | case LT_EXPR: | |
db3927fb | 13654 | return omit_one_operand_loc (loc, type, integer_zero_node, arg0); |
0aee4751 KH |
13655 | |
13656 | case LE_EXPR: | |
db3927fb | 13657 | return fold_build2_loc (loc, EQ_EXPR, type, op0, op1); |
0aee4751 KH |
13658 | |
13659 | case GE_EXPR: | |
db3927fb | 13660 | return omit_one_operand_loc (loc, type, integer_one_node, arg0); |
0aee4751 KH |
13661 | |
13662 | case GT_EXPR: | |
db3927fb | 13663 | return fold_build2_loc (loc, NE_EXPR, type, op0, op1); |
0aee4751 KH |
13664 | |
13665 | default: | |
13666 | break; | |
13667 | } | |
13668 | else if ((unsigned HOST_WIDE_INT) TREE_INT_CST_HIGH (arg1) | |
13669 | == min_hi | |
13670 | && TREE_INT_CST_LOW (arg1) == min_lo + 1) | |
13671 | switch (code) | |
13672 | { | |
13673 | case GE_EXPR: | |
43a5d30b | 13674 | arg1 = const_binop (MINUS_EXPR, arg1, integer_one_node); |
db3927fb AH |
13675 | return fold_build2_loc (loc, NE_EXPR, type, |
13676 | fold_convert_loc (loc, | |
13677 | TREE_TYPE (arg1), arg0), | |
86122f72 | 13678 | arg1); |
0aee4751 | 13679 | case LT_EXPR: |
43a5d30b | 13680 | arg1 = const_binop (MINUS_EXPR, arg1, integer_one_node); |
db3927fb AH |
13681 | return fold_build2_loc (loc, EQ_EXPR, type, |
13682 | fold_convert_loc (loc, TREE_TYPE (arg1), | |
13683 | arg0), | |
86122f72 | 13684 | arg1); |
0aee4751 KH |
13685 | default: |
13686 | break; | |
13687 | } | |
13688 | ||
5cdc4a26 | 13689 | else if (TREE_INT_CST_HIGH (arg1) == signed_max_hi |
0aee4751 | 13690 | && TREE_INT_CST_LOW (arg1) == signed_max_lo |
f0dbdfbb EB |
13691 | && TYPE_UNSIGNED (arg1_type) |
13692 | /* We will flip the signedness of the comparison operator | |
13693 | associated with the mode of arg1, so the sign bit is | |
13694 | specified by this mode. Check that arg1 is the signed | |
13695 | max associated with this sign bit. */ | |
13696 | && width == GET_MODE_BITSIZE (TYPE_MODE (arg1_type)) | |
0aee4751 | 13697 | /* signed_type does not work on pointer types. */ |
f0dbdfbb | 13698 | && INTEGRAL_TYPE_P (arg1_type)) |
0aee4751 KH |
13699 | { |
13700 | /* The following case also applies to X < signed_max+1 | |
13701 | and X >= signed_max+1 because previous transformations. */ | |
13702 | if (code == LE_EXPR || code == GT_EXPR) | |
13703 | { | |
86122f72 | 13704 | tree st; |
12753674 | 13705 | st = signed_type_for (TREE_TYPE (arg1)); |
db3927fb AH |
13706 | return fold_build2_loc (loc, |
13707 | code == LE_EXPR ? GE_EXPR : LT_EXPR, | |
13708 | type, fold_convert_loc (loc, st, arg0), | |
86122f72 | 13709 | build_int_cst (st, 0)); |
0aee4751 KH |
13710 | } |
13711 | } | |
13712 | } | |
13713 | } | |
13714 | ||
0aee4751 KH |
13715 | /* If we are comparing an ABS_EXPR with a constant, we can |
13716 | convert all the cases into explicit comparisons, but they may | |
13717 | well not be faster than doing the ABS and one comparison. | |
13718 | But ABS (X) <= C is a range comparison, which becomes a subtraction | |
13719 | and a comparison, and is probably faster. */ | |
e26ec0bb RS |
13720 | if (code == LE_EXPR |
13721 | && TREE_CODE (arg1) == INTEGER_CST | |
13722 | && TREE_CODE (arg0) == ABS_EXPR | |
13723 | && ! TREE_SIDE_EFFECTS (arg0) | |
13724 | && (0 != (tem = negate_expr (arg1))) | |
13725 | && TREE_CODE (tem) == INTEGER_CST | |
455f14dd | 13726 | && !TREE_OVERFLOW (tem)) |
db3927fb | 13727 | return fold_build2_loc (loc, TRUTH_ANDIF_EXPR, type, |
7f20a5b7 KH |
13728 | build2 (GE_EXPR, type, |
13729 | TREE_OPERAND (arg0, 0), tem), | |
13730 | build2 (LE_EXPR, type, | |
13731 | TREE_OPERAND (arg0, 0), arg1)); | |
0aee4751 KH |
13732 | |
13733 | /* Convert ABS_EXPR<x> >= 0 to true. */ | |
6ac01510 | 13734 | strict_overflow_p = false; |
e26ec0bb | 13735 | if (code == GE_EXPR |
e26ec0bb RS |
13736 | && (integer_zerop (arg1) |
13737 | || (! HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0))) | |
6ac01510 ILT |
13738 | && real_zerop (arg1))) |
13739 | && tree_expr_nonnegative_warnv_p (arg0, &strict_overflow_p)) | |
13740 | { | |
13741 | if (strict_overflow_p) | |
13742 | fold_overflow_warning (("assuming signed overflow does not occur " | |
13743 | "when simplifying comparison of " | |
13744 | "absolute value and zero"), | |
13745 | WARN_STRICT_OVERFLOW_CONDITIONAL); | |
8b5546d6 JJ |
13746 | return omit_one_operand_loc (loc, type, |
13747 | constant_boolean_node (true, type), | |
13748 | arg0); | |
6ac01510 | 13749 | } |
0aee4751 KH |
13750 | |
13751 | /* Convert ABS_EXPR<x> < 0 to false. */ | |
6ac01510 | 13752 | strict_overflow_p = false; |
e26ec0bb | 13753 | if (code == LT_EXPR |
6ac01510 ILT |
13754 | && (integer_zerop (arg1) || real_zerop (arg1)) |
13755 | && tree_expr_nonnegative_warnv_p (arg0, &strict_overflow_p)) | |
13756 | { | |
13757 | if (strict_overflow_p) | |
13758 | fold_overflow_warning (("assuming signed overflow does not occur " | |
13759 | "when simplifying comparison of " | |
13760 | "absolute value and zero"), | |
13761 | WARN_STRICT_OVERFLOW_CONDITIONAL); | |
8b5546d6 JJ |
13762 | return omit_one_operand_loc (loc, type, |
13763 | constant_boolean_node (false, type), | |
13764 | arg0); | |
6ac01510 | 13765 | } |
0aee4751 | 13766 | |
0aee4751 KH |
13767 | /* If X is unsigned, convert X < (1 << Y) into X >> Y == 0 |
13768 | and similarly for >= into !=. */ | |
13769 | if ((code == LT_EXPR || code == GE_EXPR) | |
13770 | && TYPE_UNSIGNED (TREE_TYPE (arg0)) | |
13771 | && TREE_CODE (arg1) == LSHIFT_EXPR | |
13772 | && integer_onep (TREE_OPERAND (arg1, 0))) | |
c9019218 JJ |
13773 | return build2_loc (loc, code == LT_EXPR ? EQ_EXPR : NE_EXPR, type, |
13774 | build2 (RSHIFT_EXPR, TREE_TYPE (arg0), arg0, | |
13775 | TREE_OPERAND (arg1, 1)), | |
27edb974 | 13776 | build_zero_cst (TREE_TYPE (arg0))); |
0aee4751 | 13777 | |
8f498c1b JJ |
13778 | /* Similarly for X < (cast) (1 << Y). But cast can't be narrowing, |
13779 | otherwise Y might be >= # of bits in X's type and thus e.g. | |
13780 | (unsigned char) (1 << Y) for Y 15 might be 0. | |
13781 | If the cast is widening, then 1 << Y should have unsigned type, | |
13782 | otherwise if Y is number of bits in the signed shift type minus 1, | |
13783 | we can't optimize this. E.g. (unsigned long long) (1 << Y) for Y | |
13784 | 31 might be 0xffffffff80000000. */ | |
e26ec0bb RS |
13785 | if ((code == LT_EXPR || code == GE_EXPR) |
13786 | && TYPE_UNSIGNED (TREE_TYPE (arg0)) | |
1043771b | 13787 | && CONVERT_EXPR_P (arg1) |
e26ec0bb | 13788 | && TREE_CODE (TREE_OPERAND (arg1, 0)) == LSHIFT_EXPR |
8f498c1b JJ |
13789 | && (TYPE_PRECISION (TREE_TYPE (arg1)) |
13790 | >= TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg1, 0)))) | |
13791 | && (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg1, 0))) | |
13792 | || (TYPE_PRECISION (TREE_TYPE (arg1)) | |
13793 | == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg1, 0))))) | |
e26ec0bb | 13794 | && integer_onep (TREE_OPERAND (TREE_OPERAND (arg1, 0), 0))) |
db3927fb | 13795 | { |
c9019218 JJ |
13796 | tem = build2 (RSHIFT_EXPR, TREE_TYPE (arg0), arg0, |
13797 | TREE_OPERAND (TREE_OPERAND (arg1, 0), 1)); | |
13798 | return build2_loc (loc, code == LT_EXPR ? EQ_EXPR : NE_EXPR, type, | |
13799 | fold_convert_loc (loc, TREE_TYPE (arg0), tem), | |
27edb974 | 13800 | build_zero_cst (TREE_TYPE (arg0))); |
db3927fb | 13801 | } |
0aee4751 | 13802 | |
e26ec0bb | 13803 | return NULL_TREE; |
0aee4751 KH |
13804 | |
13805 | case UNORDERED_EXPR: | |
13806 | case ORDERED_EXPR: | |
13807 | case UNLT_EXPR: | |
13808 | case UNLE_EXPR: | |
13809 | case UNGT_EXPR: | |
13810 | case UNGE_EXPR: | |
13811 | case UNEQ_EXPR: | |
13812 | case LTGT_EXPR: | |
13813 | if (TREE_CODE (arg0) == REAL_CST && TREE_CODE (arg1) == REAL_CST) | |
13814 | { | |
13815 | t1 = fold_relational_const (code, type, arg0, arg1); | |
13816 | if (t1 != NULL_TREE) | |
13817 | return t1; | |
13818 | } | |
13819 | ||
13820 | /* If the first operand is NaN, the result is constant. */ | |
13821 | if (TREE_CODE (arg0) == REAL_CST | |
13822 | && REAL_VALUE_ISNAN (TREE_REAL_CST (arg0)) | |
13823 | && (code != LTGT_EXPR || ! flag_trapping_math)) | |
13824 | { | |
13825 | t1 = (code == ORDERED_EXPR || code == LTGT_EXPR) | |
13826 | ? integer_zero_node | |
13827 | : integer_one_node; | |
db3927fb | 13828 | return omit_one_operand_loc (loc, type, t1, arg1); |
0aee4751 KH |
13829 | } |
13830 | ||
13831 | /* If the second operand is NaN, the result is constant. */ | |
13832 | if (TREE_CODE (arg1) == REAL_CST | |
13833 | && REAL_VALUE_ISNAN (TREE_REAL_CST (arg1)) | |
13834 | && (code != LTGT_EXPR || ! flag_trapping_math)) | |
13835 | { | |
13836 | t1 = (code == ORDERED_EXPR || code == LTGT_EXPR) | |
13837 | ? integer_zero_node | |
13838 | : integer_one_node; | |
db3927fb | 13839 | return omit_one_operand_loc (loc, type, t1, arg0); |
0aee4751 KH |
13840 | } |
13841 | ||
13842 | /* Simplify unordered comparison of something with itself. */ | |
13843 | if ((code == UNLE_EXPR || code == UNGE_EXPR || code == UNEQ_EXPR) | |
13844 | && operand_equal_p (arg0, arg1, 0)) | |
13845 | return constant_boolean_node (1, type); | |
13846 | ||
13847 | if (code == LTGT_EXPR | |
13848 | && !flag_trapping_math | |
13849 | && operand_equal_p (arg0, arg1, 0)) | |
13850 | return constant_boolean_node (0, type); | |
13851 | ||
13852 | /* Fold (double)float1 CMP (double)float2 into float1 CMP float2. */ | |
13853 | { | |
13854 | tree targ0 = strip_float_extensions (arg0); | |
13855 | tree targ1 = strip_float_extensions (arg1); | |
13856 | tree newtype = TREE_TYPE (targ0); | |
13857 | ||
13858 | if (TYPE_PRECISION (TREE_TYPE (targ1)) > TYPE_PRECISION (newtype)) | |
13859 | newtype = TREE_TYPE (targ1); | |
13860 | ||
13861 | if (TYPE_PRECISION (newtype) < TYPE_PRECISION (TREE_TYPE (arg0))) | |
db3927fb AH |
13862 | return fold_build2_loc (loc, code, type, |
13863 | fold_convert_loc (loc, newtype, targ0), | |
13864 | fold_convert_loc (loc, newtype, targ1)); | |
0aee4751 KH |
13865 | } |
13866 | ||
62ab45cc | 13867 | return NULL_TREE; |
0aee4751 KH |
13868 | |
13869 | case COMPOUND_EXPR: | |
13870 | /* When pedantic, a compound expression can be neither an lvalue | |
13871 | nor an integer constant expression. */ | |
13872 | if (TREE_SIDE_EFFECTS (arg0) || TREE_CONSTANT (arg1)) | |
62ab45cc | 13873 | return NULL_TREE; |
0aee4751 KH |
13874 | /* Don't let (0, 0) be null pointer constant. */ |
13875 | tem = integer_zerop (arg1) ? build1 (NOP_EXPR, type, arg1) | |
db3927fb AH |
13876 | : fold_convert_loc (loc, type, arg1); |
13877 | return pedantic_non_lvalue_loc (loc, tem); | |
0aee4751 KH |
13878 | |
13879 | case COMPLEX_EXPR: | |
fd6c76f4 RS |
13880 | if ((TREE_CODE (arg0) == REAL_CST |
13881 | && TREE_CODE (arg1) == REAL_CST) | |
13882 | || (TREE_CODE (arg0) == INTEGER_CST | |
13883 | && TREE_CODE (arg1) == INTEGER_CST)) | |
0aee4751 | 13884 | return build_complex (type, arg0, arg1); |
0b0081ec RG |
13885 | if (TREE_CODE (arg0) == REALPART_EXPR |
13886 | && TREE_CODE (arg1) == IMAGPART_EXPR | |
544d960a | 13887 | && TREE_TYPE (TREE_OPERAND (arg0, 0)) == type |
0b0081ec RG |
13888 | && operand_equal_p (TREE_OPERAND (arg0, 0), |
13889 | TREE_OPERAND (arg1, 0), 0)) | |
13890 | return omit_one_operand_loc (loc, type, TREE_OPERAND (arg0, 0), | |
13891 | TREE_OPERAND (arg1, 0)); | |
62ab45cc | 13892 | return NULL_TREE; |
0aee4751 | 13893 | |
cb4819f0 KH |
13894 | case ASSERT_EXPR: |
13895 | /* An ASSERT_EXPR should never be passed to fold_binary. */ | |
13896 | gcc_unreachable (); | |
13897 | ||
ed74d697 JJ |
13898 | case VEC_PACK_TRUNC_EXPR: |
13899 | case VEC_PACK_FIX_TRUNC_EXPR: | |
13900 | { | |
13901 | unsigned int nelts = TYPE_VECTOR_SUBPARTS (type), i; | |
d2a12ae7 | 13902 | tree *elts; |
ed74d697 JJ |
13903 | |
13904 | gcc_assert (TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0)) == nelts / 2 | |
13905 | && TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg1)) == nelts / 2); | |
13906 | if (TREE_CODE (arg0) != VECTOR_CST || TREE_CODE (arg1) != VECTOR_CST) | |
13907 | return NULL_TREE; | |
13908 | ||
13909 | elts = XALLOCAVEC (tree, nelts); | |
13910 | if (!vec_cst_ctor_to_array (arg0, elts) | |
13911 | || !vec_cst_ctor_to_array (arg1, elts + nelts / 2)) | |
13912 | return NULL_TREE; | |
13913 | ||
13914 | for (i = 0; i < nelts; i++) | |
13915 | { | |
13916 | elts[i] = fold_convert_const (code == VEC_PACK_TRUNC_EXPR | |
13917 | ? NOP_EXPR : FIX_TRUNC_EXPR, | |
13918 | TREE_TYPE (type), elts[i]); | |
13919 | if (elts[i] == NULL_TREE || !CONSTANT_CLASS_P (elts[i])) | |
13920 | return NULL_TREE; | |
13921 | } | |
13922 | ||
d2a12ae7 | 13923 | return build_vector (type, elts); |
ed74d697 JJ |
13924 | } |
13925 | ||
13926 | case VEC_WIDEN_MULT_LO_EXPR: | |
13927 | case VEC_WIDEN_MULT_HI_EXPR: | |
3f30a9a6 RH |
13928 | case VEC_WIDEN_MULT_EVEN_EXPR: |
13929 | case VEC_WIDEN_MULT_ODD_EXPR: | |
ed74d697 | 13930 | { |
3f30a9a6 RH |
13931 | unsigned int nelts = TYPE_VECTOR_SUBPARTS (type); |
13932 | unsigned int out, ofs, scale; | |
d2a12ae7 | 13933 | tree *elts; |
ed74d697 JJ |
13934 | |
13935 | gcc_assert (TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0)) == nelts * 2 | |
13936 | && TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg1)) == nelts * 2); | |
13937 | if (TREE_CODE (arg0) != VECTOR_CST || TREE_CODE (arg1) != VECTOR_CST) | |
13938 | return NULL_TREE; | |
13939 | ||
13940 | elts = XALLOCAVEC (tree, nelts * 4); | |
13941 | if (!vec_cst_ctor_to_array (arg0, elts) | |
13942 | || !vec_cst_ctor_to_array (arg1, elts + nelts * 2)) | |
13943 | return NULL_TREE; | |
13944 | ||
3f30a9a6 RH |
13945 | if (code == VEC_WIDEN_MULT_LO_EXPR) |
13946 | scale = 0, ofs = BYTES_BIG_ENDIAN ? nelts : 0; | |
13947 | else if (code == VEC_WIDEN_MULT_HI_EXPR) | |
13948 | scale = 0, ofs = BYTES_BIG_ENDIAN ? 0 : nelts; | |
13949 | else if (code == VEC_WIDEN_MULT_EVEN_EXPR) | |
13950 | scale = 1, ofs = 0; | |
13951 | else /* if (code == VEC_WIDEN_MULT_ODD_EXPR) */ | |
13952 | scale = 1, ofs = 1; | |
13953 | ||
13954 | for (out = 0; out < nelts; out++) | |
ed74d697 | 13955 | { |
3f30a9a6 RH |
13956 | unsigned int in1 = (out << scale) + ofs; |
13957 | unsigned int in2 = in1 + nelts * 2; | |
13958 | tree t1, t2; | |
13959 | ||
13960 | t1 = fold_convert_const (NOP_EXPR, TREE_TYPE (type), elts[in1]); | |
13961 | t2 = fold_convert_const (NOP_EXPR, TREE_TYPE (type), elts[in2]); | |
13962 | ||
13963 | if (t1 == NULL_TREE || t2 == NULL_TREE) | |
ed74d697 | 13964 | return NULL_TREE; |
3f30a9a6 RH |
13965 | elts[out] = const_binop (MULT_EXPR, t1, t2); |
13966 | if (elts[out] == NULL_TREE || !CONSTANT_CLASS_P (elts[out])) | |
ed74d697 JJ |
13967 | return NULL_TREE; |
13968 | } | |
13969 | ||
d2a12ae7 | 13970 | return build_vector (type, elts); |
ed74d697 JJ |
13971 | } |
13972 | ||
0aee4751 | 13973 | default: |
62ab45cc | 13974 | return NULL_TREE; |
0aee4751 KH |
13975 | } /* switch (code) */ |
13976 | } | |
13977 | ||
c703e618 EB |
13978 | /* Callback for walk_tree, looking for LABEL_EXPR. Return *TP if it is |
13979 | a LABEL_EXPR; otherwise return NULL_TREE. Do not check the subtrees | |
13980 | of GOTO_EXPR. */ | |
8c900457 GL |
13981 | |
13982 | static tree | |
c703e618 | 13983 | contains_label_1 (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED) |
8c900457 GL |
13984 | { |
13985 | switch (TREE_CODE (*tp)) | |
13986 | { | |
13987 | case LABEL_EXPR: | |
13988 | return *tp; | |
c703e618 | 13989 | |
8c900457 GL |
13990 | case GOTO_EXPR: |
13991 | *walk_subtrees = 0; | |
c703e618 EB |
13992 | |
13993 | /* ... fall through ... */ | |
13994 | ||
8c900457 GL |
13995 | default: |
13996 | return NULL_TREE; | |
13997 | } | |
13998 | } | |
13999 | ||
c703e618 EB |
14000 | /* Return whether the sub-tree ST contains a label which is accessible from |
14001 | outside the sub-tree. */ | |
8c900457 GL |
14002 | |
14003 | static bool | |
14004 | contains_label_p (tree st) | |
14005 | { | |
c703e618 EB |
14006 | return |
14007 | (walk_tree_without_duplicates (&st, contains_label_1 , NULL) != NULL_TREE); | |
8c900457 GL |
14008 | } |
14009 | ||
7cf57259 KH |
14010 | /* Fold a ternary expression of code CODE and type TYPE with operands |
14011 | OP0, OP1, and OP2. Return the folded expression if folding is | |
14012 | successful. Otherwise, return NULL_TREE. */ | |
9bdae6af | 14013 | |
721425b6 | 14014 | tree |
db3927fb | 14015 | fold_ternary_loc (location_t loc, enum tree_code code, tree type, |
16949072 | 14016 | tree op0, tree op1, tree op2) |
9bdae6af | 14017 | { |
9bdae6af | 14018 | tree tem; |
16949072 | 14019 | tree arg0 = NULL_TREE, arg1 = NULL_TREE, arg2 = NULL_TREE; |
9bdae6af | 14020 | enum tree_code_class kind = TREE_CODE_CLASS (code); |
9bdae6af KH |
14021 | |
14022 | gcc_assert (IS_EXPR_CODE_CLASS (kind) | |
14023 | && TREE_CODE_LENGTH (code) == 3); | |
14024 | ||
3ea2c264 KH |
14025 | /* Strip any conversions that don't change the mode. This is safe |
14026 | for every expression, except for a comparison expression because | |
14027 | its signedness is derived from its operands. So, in the latter | |
14028 | case, only strip conversions that don't change the signedness. | |
9bdae6af | 14029 | |
3ea2c264 KH |
14030 | Note that this is done as an internal manipulation within the |
14031 | constant folder, in order to find the simplest representation of | |
14032 | the arguments so that their form can be studied. In any cases, | |
14033 | the appropriate type conversions should be put back in the tree | |
14034 | that will get out of the constant folder. */ | |
14035 | if (op0) | |
14036 | { | |
14037 | arg0 = op0; | |
14038 | STRIP_NOPS (arg0); | |
14039 | } | |
9bdae6af | 14040 | |
3ea2c264 KH |
14041 | if (op1) |
14042 | { | |
14043 | arg1 = op1; | |
14044 | STRIP_NOPS (arg1); | |
9bdae6af KH |
14045 | } |
14046 | ||
16949072 RG |
14047 | if (op2) |
14048 | { | |
14049 | arg2 = op2; | |
14050 | STRIP_NOPS (arg2); | |
14051 | } | |
14052 | ||
9bdae6af KH |
14053 | switch (code) |
14054 | { | |
14055 | case COMPONENT_REF: | |
14056 | if (TREE_CODE (arg0) == CONSTRUCTOR | |
14057 | && ! type_contains_placeholder_p (TREE_TYPE (arg0))) | |
14058 | { | |
4038c495 GB |
14059 | unsigned HOST_WIDE_INT idx; |
14060 | tree field, value; | |
14061 | FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (arg0), idx, field, value) | |
14062 | if (field == arg1) | |
14063 | return value; | |
9bdae6af | 14064 | } |
62ab45cc | 14065 | return NULL_TREE; |
9bdae6af KH |
14066 | |
14067 | case COND_EXPR: | |
08e0cda6 | 14068 | case VEC_COND_EXPR: |
9bdae6af KH |
14069 | /* Pedantic ANSI C says that a conditional expression is never an lvalue, |
14070 | so all simple results must be passed through pedantic_non_lvalue. */ | |
14071 | if (TREE_CODE (arg0) == INTEGER_CST) | |
14072 | { | |
8c900457 | 14073 | tree unused_op = integer_zerop (arg0) ? op1 : op2; |
3ea2c264 | 14074 | tem = integer_zerop (arg0) ? op2 : op1; |
9bdae6af KH |
14075 | /* Only optimize constant conditions when the selected branch |
14076 | has the same type as the COND_EXPR. This avoids optimizing | |
8c900457 GL |
14077 | away "c ? x : throw", where the throw has a void type. |
14078 | Avoid throwing away that operand which contains label. */ | |
14079 | if ((!TREE_SIDE_EFFECTS (unused_op) | |
14080 | || !contains_label_p (unused_op)) | |
14081 | && (! VOID_TYPE_P (TREE_TYPE (tem)) | |
14082 | || VOID_TYPE_P (type))) | |
db3927fb | 14083 | return pedantic_non_lvalue_loc (loc, tem); |
62ab45cc | 14084 | return NULL_TREE; |
9bdae6af | 14085 | } |
08e0cda6 MG |
14086 | else if (TREE_CODE (arg0) == VECTOR_CST) |
14087 | { | |
14088 | if (integer_all_onesp (arg0)) | |
14089 | return pedantic_omit_one_operand_loc (loc, type, arg1, arg2); | |
14090 | if (integer_zerop (arg0)) | |
14091 | return pedantic_omit_one_operand_loc (loc, type, arg2, arg1); | |
6805bd36 MG |
14092 | |
14093 | if ((TREE_CODE (arg1) == VECTOR_CST | |
14094 | || TREE_CODE (arg1) == CONSTRUCTOR) | |
14095 | && (TREE_CODE (arg2) == VECTOR_CST | |
14096 | || TREE_CODE (arg2) == CONSTRUCTOR)) | |
14097 | { | |
14098 | unsigned int nelts = TYPE_VECTOR_SUBPARTS (type), i; | |
14099 | unsigned char *sel = XALLOCAVEC (unsigned char, nelts); | |
14100 | gcc_assert (nelts == VECTOR_CST_NELTS (arg0)); | |
14101 | for (i = 0; i < nelts; i++) | |
14102 | { | |
14103 | tree val = VECTOR_CST_ELT (arg0, i); | |
14104 | if (integer_all_onesp (val)) | |
14105 | sel[i] = i; | |
14106 | else if (integer_zerop (val)) | |
14107 | sel[i] = nelts + i; | |
14108 | else /* Currently unreachable. */ | |
14109 | return NULL_TREE; | |
14110 | } | |
14111 | tree t = fold_vec_perm (type, arg1, arg2, sel); | |
14112 | if (t != NULL_TREE) | |
14113 | return t; | |
14114 | } | |
08e0cda6 MG |
14115 | } |
14116 | ||
3ea2c264 | 14117 | if (operand_equal_p (arg1, op2, 0)) |
db3927fb | 14118 | return pedantic_omit_one_operand_loc (loc, type, arg1, arg0); |
9bdae6af KH |
14119 | |
14120 | /* If we have A op B ? A : C, we may be able to convert this to a | |
14121 | simpler expression, depending on the operation and the values | |
14122 | of B and C. Signed zeros prevent all of these transformations, | |
14123 | for reasons given above each one. | |
14124 | ||
14125 | Also try swapping the arguments and inverting the conditional. */ | |
14126 | if (COMPARISON_CLASS_P (arg0) | |
14127 | && operand_equal_for_comparison_p (TREE_OPERAND (arg0, 0), | |
14128 | arg1, TREE_OPERAND (arg0, 1)) | |
14129 | && !HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg1)))) | |
14130 | { | |
db3927fb | 14131 | tem = fold_cond_expr_with_comparison (loc, type, arg0, op1, op2); |
9bdae6af KH |
14132 | if (tem) |
14133 | return tem; | |
14134 | } | |
14135 | ||
14136 | if (COMPARISON_CLASS_P (arg0) | |
14137 | && operand_equal_for_comparison_p (TREE_OPERAND (arg0, 0), | |
3ea2c264 | 14138 | op2, |
9bdae6af | 14139 | TREE_OPERAND (arg0, 1)) |
3ea2c264 | 14140 | && !HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (op2)))) |
9bdae6af | 14141 | { |
e4c03378 | 14142 | location_t loc0 = expr_location_or (arg0, loc); |
418d1b87 | 14143 | tem = fold_invert_truthvalue (loc0, arg0); |
d817ed3b | 14144 | if (tem && COMPARISON_CLASS_P (tem)) |
9bdae6af | 14145 | { |
db3927fb | 14146 | tem = fold_cond_expr_with_comparison (loc, type, tem, op2, op1); |
9bdae6af KH |
14147 | if (tem) |
14148 | return tem; | |
14149 | } | |
14150 | } | |
14151 | ||
14152 | /* If the second operand is simpler than the third, swap them | |
14153 | since that produces better jump optimization results. */ | |
3dac16bd RG |
14154 | if (truth_value_p (TREE_CODE (arg0)) |
14155 | && tree_swap_operands_p (op1, op2, false)) | |
9bdae6af | 14156 | { |
e4c03378 | 14157 | location_t loc0 = expr_location_or (arg0, loc); |
9bdae6af KH |
14158 | /* See if this can be inverted. If it can't, possibly because |
14159 | it was a floating-point inequality comparison, don't do | |
14160 | anything. */ | |
418d1b87 | 14161 | tem = fold_invert_truthvalue (loc0, arg0); |
d817ed3b | 14162 | if (tem) |
db3927fb | 14163 | return fold_build3_loc (loc, code, type, tem, op2, op1); |
9bdae6af KH |
14164 | } |
14165 | ||
14166 | /* Convert A ? 1 : 0 to simply A. */ | |
418d1b87 MG |
14167 | if ((code == VEC_COND_EXPR ? integer_all_onesp (op1) |
14168 | : (integer_onep (op1) | |
14169 | && !VECTOR_TYPE_P (type))) | |
3ea2c264 KH |
14170 | && integer_zerop (op2) |
14171 | /* If we try to convert OP0 to our type, the | |
9bdae6af KH |
14172 | call to fold will try to move the conversion inside |
14173 | a COND, which will recurse. In that case, the COND_EXPR | |
14174 | is probably the best choice, so leave it alone. */ | |
14175 | && type == TREE_TYPE (arg0)) | |
db3927fb | 14176 | return pedantic_non_lvalue_loc (loc, arg0); |
9bdae6af KH |
14177 | |
14178 | /* Convert A ? 0 : 1 to !A. This prefers the use of NOT_EXPR | |
14179 | over COND_EXPR in cases such as floating point comparisons. */ | |
3ea2c264 | 14180 | if (integer_zerop (op1) |
418d1b87 MG |
14181 | && (code == VEC_COND_EXPR ? integer_all_onesp (op2) |
14182 | : (integer_onep (op2) | |
14183 | && !VECTOR_TYPE_P (type))) | |
9bdae6af | 14184 | && truth_value_p (TREE_CODE (arg0))) |
db3927fb AH |
14185 | return pedantic_non_lvalue_loc (loc, |
14186 | fold_convert_loc (loc, type, | |
14187 | invert_truthvalue_loc (loc, | |
14188 | arg0))); | |
9bdae6af KH |
14189 | |
14190 | /* A < 0 ? <sign bit of A> : 0 is simply (A & <sign bit of A>). */ | |
14191 | if (TREE_CODE (arg0) == LT_EXPR | |
789e604d JJ |
14192 | && integer_zerop (TREE_OPERAND (arg0, 1)) |
14193 | && integer_zerop (op2) | |
14194 | && (tem = sign_bit_p (TREE_OPERAND (arg0, 0), arg1))) | |
14195 | { | |
f66d0891 JJ |
14196 | /* sign_bit_p looks through both zero and sign extensions, |
14197 | but for this optimization only sign extensions are | |
14198 | usable. */ | |
14199 | tree tem2 = TREE_OPERAND (arg0, 0); | |
14200 | while (tem != tem2) | |
14201 | { | |
14202 | if (TREE_CODE (tem2) != NOP_EXPR | |
14203 | || TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (tem2, 0)))) | |
14204 | { | |
14205 | tem = NULL_TREE; | |
14206 | break; | |
14207 | } | |
14208 | tem2 = TREE_OPERAND (tem2, 0); | |
14209 | } | |
789e604d JJ |
14210 | /* sign_bit_p only checks ARG1 bits within A's precision. |
14211 | If <sign bit of A> has wider type than A, bits outside | |
14212 | of A's precision in <sign bit of A> need to be checked. | |
14213 | If they are all 0, this optimization needs to be done | |
14214 | in unsigned A's type, if they are all 1 in signed A's type, | |
14215 | otherwise this can't be done. */ | |
f66d0891 JJ |
14216 | if (tem |
14217 | && TYPE_PRECISION (TREE_TYPE (tem)) | |
14218 | < TYPE_PRECISION (TREE_TYPE (arg1)) | |
789e604d JJ |
14219 | && TYPE_PRECISION (TREE_TYPE (tem)) |
14220 | < TYPE_PRECISION (type)) | |
14221 | { | |
14222 | unsigned HOST_WIDE_INT mask_lo; | |
14223 | HOST_WIDE_INT mask_hi; | |
14224 | int inner_width, outer_width; | |
14225 | tree tem_type; | |
14226 | ||
14227 | inner_width = TYPE_PRECISION (TREE_TYPE (tem)); | |
14228 | outer_width = TYPE_PRECISION (TREE_TYPE (arg1)); | |
14229 | if (outer_width > TYPE_PRECISION (type)) | |
14230 | outer_width = TYPE_PRECISION (type); | |
14231 | ||
14232 | if (outer_width > HOST_BITS_PER_WIDE_INT) | |
14233 | { | |
0cadbfaa | 14234 | mask_hi = (HOST_WIDE_INT_M1U |
49ab6098 | 14235 | >> (HOST_BITS_PER_DOUBLE_INT - outer_width)); |
789e604d JJ |
14236 | mask_lo = -1; |
14237 | } | |
14238 | else | |
14239 | { | |
14240 | mask_hi = 0; | |
0cadbfaa | 14241 | mask_lo = (HOST_WIDE_INT_M1U |
789e604d JJ |
14242 | >> (HOST_BITS_PER_WIDE_INT - outer_width)); |
14243 | } | |
14244 | if (inner_width > HOST_BITS_PER_WIDE_INT) | |
14245 | { | |
0cadbfaa | 14246 | mask_hi &= ~(HOST_WIDE_INT_M1U |
789e604d JJ |
14247 | >> (HOST_BITS_PER_WIDE_INT - inner_width)); |
14248 | mask_lo = 0; | |
14249 | } | |
14250 | else | |
0cadbfaa | 14251 | mask_lo &= ~(HOST_WIDE_INT_M1U |
789e604d JJ |
14252 | >> (HOST_BITS_PER_WIDE_INT - inner_width)); |
14253 | ||
14254 | if ((TREE_INT_CST_HIGH (arg1) & mask_hi) == mask_hi | |
14255 | && (TREE_INT_CST_LOW (arg1) & mask_lo) == mask_lo) | |
14256 | { | |
12753674 | 14257 | tem_type = signed_type_for (TREE_TYPE (tem)); |
db3927fb | 14258 | tem = fold_convert_loc (loc, tem_type, tem); |
789e604d JJ |
14259 | } |
14260 | else if ((TREE_INT_CST_HIGH (arg1) & mask_hi) == 0 | |
14261 | && (TREE_INT_CST_LOW (arg1) & mask_lo) == 0) | |
14262 | { | |
ca5ba2a3 | 14263 | tem_type = unsigned_type_for (TREE_TYPE (tem)); |
db3927fb | 14264 | tem = fold_convert_loc (loc, tem_type, tem); |
789e604d JJ |
14265 | } |
14266 | else | |
14267 | tem = NULL; | |
14268 | } | |
14269 | ||
14270 | if (tem) | |
db3927fb AH |
14271 | return |
14272 | fold_convert_loc (loc, type, | |
14273 | fold_build2_loc (loc, BIT_AND_EXPR, | |
14274 | TREE_TYPE (tem), tem, | |
14275 | fold_convert_loc (loc, | |
14276 | TREE_TYPE (tem), | |
14277 | arg1))); | |
789e604d | 14278 | } |
9bdae6af KH |
14279 | |
14280 | /* (A >> N) & 1 ? (1 << N) : 0 is simply A & (1 << N). A & 1 was | |
14281 | already handled above. */ | |
14282 | if (TREE_CODE (arg0) == BIT_AND_EXPR | |
14283 | && integer_onep (TREE_OPERAND (arg0, 1)) | |
3ea2c264 | 14284 | && integer_zerop (op2) |
9bdae6af KH |
14285 | && integer_pow2p (arg1)) |
14286 | { | |
14287 | tree tem = TREE_OPERAND (arg0, 0); | |
14288 | STRIP_NOPS (tem); | |
14289 | if (TREE_CODE (tem) == RSHIFT_EXPR | |
14290 | && TREE_CODE (TREE_OPERAND (tem, 1)) == INTEGER_CST | |
14291 | && (unsigned HOST_WIDE_INT) tree_log2 (arg1) == | |
14292 | TREE_INT_CST_LOW (TREE_OPERAND (tem, 1))) | |
db3927fb | 14293 | return fold_build2_loc (loc, BIT_AND_EXPR, type, |
7f20a5b7 | 14294 | TREE_OPERAND (tem, 0), arg1); |
9bdae6af KH |
14295 | } |
14296 | ||
14297 | /* A & N ? N : 0 is simply A & N if N is a power of two. This | |
14298 | is probably obsolete because the first operand should be a | |
14299 | truth value (that's why we have the two cases above), but let's | |
14300 | leave it in until we can confirm this for all front-ends. */ | |
3ea2c264 | 14301 | if (integer_zerop (op2) |
9bdae6af KH |
14302 | && TREE_CODE (arg0) == NE_EXPR |
14303 | && integer_zerop (TREE_OPERAND (arg0, 1)) | |
14304 | && integer_pow2p (arg1) | |
14305 | && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_AND_EXPR | |
14306 | && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1), | |
14307 | arg1, OEP_ONLY_CONST)) | |
db3927fb AH |
14308 | return pedantic_non_lvalue_loc (loc, |
14309 | fold_convert_loc (loc, type, | |
14310 | TREE_OPERAND (arg0, 0))); | |
9bdae6af | 14311 | |
43bb4dd1 MG |
14312 | /* Disable the transformations below for vectors, since |
14313 | fold_binary_op_with_conditional_arg may undo them immediately, | |
14314 | yielding an infinite loop. */ | |
14315 | if (code == VEC_COND_EXPR) | |
14316 | return NULL_TREE; | |
14317 | ||
9bdae6af | 14318 | /* Convert A ? B : 0 into A && B if A and B are truth values. */ |
3ea2c264 | 14319 | if (integer_zerop (op2) |
9bdae6af | 14320 | && truth_value_p (TREE_CODE (arg0)) |
418d1b87 MG |
14321 | && truth_value_p (TREE_CODE (arg1)) |
14322 | && (code == VEC_COND_EXPR || !VECTOR_TYPE_P (type))) | |
14323 | return fold_build2_loc (loc, code == VEC_COND_EXPR ? BIT_AND_EXPR | |
14324 | : TRUTH_ANDIF_EXPR, | |
14325 | type, fold_convert_loc (loc, type, arg0), arg1); | |
9bdae6af KH |
14326 | |
14327 | /* Convert A ? B : 1 into !A || B if A and B are truth values. */ | |
418d1b87 | 14328 | if (code == VEC_COND_EXPR ? integer_all_onesp (op2) : integer_onep (op2) |
9bdae6af | 14329 | && truth_value_p (TREE_CODE (arg0)) |
418d1b87 MG |
14330 | && truth_value_p (TREE_CODE (arg1)) |
14331 | && (code == VEC_COND_EXPR || !VECTOR_TYPE_P (type))) | |
9bdae6af | 14332 | { |
e4c03378 | 14333 | location_t loc0 = expr_location_or (arg0, loc); |
9bdae6af | 14334 | /* Only perform transformation if ARG0 is easily inverted. */ |
418d1b87 | 14335 | tem = fold_invert_truthvalue (loc0, arg0); |
d817ed3b | 14336 | if (tem) |
418d1b87 MG |
14337 | return fold_build2_loc (loc, code == VEC_COND_EXPR |
14338 | ? BIT_IOR_EXPR | |
14339 | : TRUTH_ORIF_EXPR, | |
14340 | type, fold_convert_loc (loc, type, tem), | |
14341 | arg1); | |
9bdae6af KH |
14342 | } |
14343 | ||
14344 | /* Convert A ? 0 : B into !A && B if A and B are truth values. */ | |
14345 | if (integer_zerop (arg1) | |
14346 | && truth_value_p (TREE_CODE (arg0)) | |
418d1b87 MG |
14347 | && truth_value_p (TREE_CODE (op2)) |
14348 | && (code == VEC_COND_EXPR || !VECTOR_TYPE_P (type))) | |
9bdae6af | 14349 | { |
e4c03378 | 14350 | location_t loc0 = expr_location_or (arg0, loc); |
9bdae6af | 14351 | /* Only perform transformation if ARG0 is easily inverted. */ |
418d1b87 | 14352 | tem = fold_invert_truthvalue (loc0, arg0); |
d817ed3b | 14353 | if (tem) |
418d1b87 MG |
14354 | return fold_build2_loc (loc, code == VEC_COND_EXPR |
14355 | ? BIT_AND_EXPR : TRUTH_ANDIF_EXPR, | |
14356 | type, fold_convert_loc (loc, type, tem), | |
14357 | op2); | |
9bdae6af KH |
14358 | } |
14359 | ||
14360 | /* Convert A ? 1 : B into A || B if A and B are truth values. */ | |
418d1b87 | 14361 | if (code == VEC_COND_EXPR ? integer_all_onesp (arg1) : integer_onep (arg1) |
9bdae6af | 14362 | && truth_value_p (TREE_CODE (arg0)) |
418d1b87 MG |
14363 | && truth_value_p (TREE_CODE (op2)) |
14364 | && (code == VEC_COND_EXPR || !VECTOR_TYPE_P (type))) | |
14365 | return fold_build2_loc (loc, code == VEC_COND_EXPR | |
14366 | ? BIT_IOR_EXPR : TRUTH_ORIF_EXPR, | |
14367 | type, fold_convert_loc (loc, type, arg0), op2); | |
9bdae6af | 14368 | |
62ab45cc | 14369 | return NULL_TREE; |
9bdae6af KH |
14370 | |
14371 | case CALL_EXPR: | |
5039610b SL |
14372 | /* CALL_EXPRs used to be ternary exprs. Catch any mistaken uses |
14373 | of fold_ternary on them. */ | |
14374 | gcc_unreachable (); | |
9bdae6af | 14375 | |
dcd25113 | 14376 | case BIT_FIELD_REF: |
5773afc5 | 14377 | if ((TREE_CODE (arg0) == VECTOR_CST |
ea814c66 EB |
14378 | || (TREE_CODE (arg0) == CONSTRUCTOR |
14379 | && TREE_CODE (TREE_TYPE (arg0)) == VECTOR_TYPE)) | |
ada3df50 RG |
14380 | && (type == TREE_TYPE (TREE_TYPE (arg0)) |
14381 | || (TREE_CODE (type) == VECTOR_TYPE | |
14382 | && TREE_TYPE (type) == TREE_TYPE (TREE_TYPE (arg0))))) | |
dcd25113 | 14383 | { |
ada3df50 RG |
14384 | tree eltype = TREE_TYPE (TREE_TYPE (arg0)); |
14385 | unsigned HOST_WIDE_INT width = tree_low_cst (TYPE_SIZE (eltype), 1); | |
14386 | unsigned HOST_WIDE_INT n = tree_low_cst (arg1, 1); | |
dcd25113 JJ |
14387 | unsigned HOST_WIDE_INT idx = tree_low_cst (op2, 1); |
14388 | ||
ada3df50 | 14389 | if (n != 0 |
dcd25113 | 14390 | && (idx % width) == 0 |
ada3df50 RG |
14391 | && (n % width) == 0 |
14392 | && ((idx + n) / width) <= TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0))) | |
dcd25113 | 14393 | { |
ada3df50 RG |
14394 | idx = idx / width; |
14395 | n = n / width; | |
e9d6bd8c MG |
14396 | |
14397 | if (TREE_CODE (arg0) == VECTOR_CST) | |
ada3df50 | 14398 | { |
e9d6bd8c MG |
14399 | if (n == 1) |
14400 | return VECTOR_CST_ELT (arg0, idx); | |
14401 | ||
14402 | tree *vals = XALLOCAVEC (tree, n); | |
14403 | for (unsigned i = 0; i < n; ++i) | |
14404 | vals[i] = VECTOR_CST_ELT (arg0, idx + i); | |
14405 | return build_vector (type, vals); | |
ada3df50 | 14406 | } |
e9d6bd8c MG |
14407 | |
14408 | /* Constructor elements can be subvectors. */ | |
14409 | unsigned HOST_WIDE_INT k = 1; | |
14410 | if (CONSTRUCTOR_NELTS (arg0) != 0) | |
ada3df50 | 14411 | { |
e9d6bd8c MG |
14412 | tree cons_elem = TREE_TYPE (CONSTRUCTOR_ELT (arg0, 0)->value); |
14413 | if (TREE_CODE (cons_elem) == VECTOR_TYPE) | |
14414 | k = TYPE_VECTOR_SUBPARTS (cons_elem); | |
14415 | } | |
14416 | ||
14417 | /* We keep an exact subset of the constructor elements. */ | |
14418 | if ((idx % k) == 0 && (n % k) == 0) | |
14419 | { | |
14420 | if (CONSTRUCTOR_NELTS (arg0) == 0) | |
14421 | return build_constructor (type, NULL); | |
14422 | idx /= k; | |
14423 | n /= k; | |
14424 | if (n == 1) | |
4a2c20cc JJ |
14425 | { |
14426 | if (idx < CONSTRUCTOR_NELTS (arg0)) | |
14427 | return CONSTRUCTOR_ELT (arg0, idx)->value; | |
14428 | return build_zero_cst (type); | |
14429 | } | |
e9d6bd8c MG |
14430 | |
14431 | vec<constructor_elt, va_gc> *vals; | |
14432 | vec_alloc (vals, n); | |
14433 | for (unsigned i = 0; | |
14434 | i < n && idx + i < CONSTRUCTOR_NELTS (arg0); | |
14435 | ++i) | |
14436 | CONSTRUCTOR_APPEND_ELT (vals, NULL_TREE, | |
14437 | CONSTRUCTOR_ELT | |
14438 | (arg0, idx + i)->value); | |
14439 | return build_constructor (type, vals); | |
14440 | } | |
14441 | /* The bitfield references a single constructor element. */ | |
14442 | else if (idx + n <= (idx / k + 1) * k) | |
14443 | { | |
14444 | if (CONSTRUCTOR_NELTS (arg0) <= idx / k) | |
14445 | return build_zero_cst (type); | |
14446 | else if (n == k) | |
14447 | return CONSTRUCTOR_ELT (arg0, idx / k)->value; | |
14448 | else | |
14449 | return fold_build3_loc (loc, code, type, | |
14450 | CONSTRUCTOR_ELT (arg0, idx / k)->value, op1, | |
14451 | build_int_cst (TREE_TYPE (op2), (idx % k) * width)); | |
ada3df50 | 14452 | } |
dcd25113 JJ |
14453 | } |
14454 | } | |
ee1f1270 RG |
14455 | |
14456 | /* A bit-field-ref that referenced the full argument can be stripped. */ | |
14457 | if (INTEGRAL_TYPE_P (TREE_TYPE (arg0)) | |
14458 | && TYPE_PRECISION (TREE_TYPE (arg0)) == tree_low_cst (arg1, 1) | |
14459 | && integer_zerop (op2)) | |
db3927fb | 14460 | return fold_convert_loc (loc, type, arg0); |
ee1f1270 | 14461 | |
6814f778 RG |
14462 | /* On constants we can use native encode/interpret to constant |
14463 | fold (nearly) all BIT_FIELD_REFs. */ | |
14464 | if (CONSTANT_CLASS_P (arg0) | |
14465 | && can_native_interpret_type_p (type) | |
14466 | && host_integerp (TYPE_SIZE_UNIT (TREE_TYPE (arg0)), 1) | |
14467 | /* This limitation should not be necessary, we just need to | |
14468 | round this up to mode size. */ | |
14469 | && tree_low_cst (op1, 1) % BITS_PER_UNIT == 0 | |
14470 | /* Need bit-shifting of the buffer to relax the following. */ | |
14471 | && tree_low_cst (op2, 1) % BITS_PER_UNIT == 0) | |
14472 | { | |
e45381b3 | 14473 | unsigned HOST_WIDE_INT bitpos = tree_low_cst (op2, 1); |
6814f778 RG |
14474 | unsigned HOST_WIDE_INT bitsize = tree_low_cst (op1, 1); |
14475 | unsigned HOST_WIDE_INT clen; | |
14476 | clen = tree_low_cst (TYPE_SIZE_UNIT (TREE_TYPE (arg0)), 1); | |
14477 | /* ??? We cannot tell native_encode_expr to start at | |
14478 | some random byte only. So limit us to a reasonable amount | |
14479 | of work. */ | |
14480 | if (clen <= 4096) | |
14481 | { | |
14482 | unsigned char *b = XALLOCAVEC (unsigned char, clen); | |
14483 | unsigned HOST_WIDE_INT len = native_encode_expr (arg0, b, clen); | |
14484 | if (len > 0 | |
14485 | && len * BITS_PER_UNIT >= bitpos + bitsize) | |
14486 | { | |
14487 | tree v = native_interpret_expr (type, | |
14488 | b + bitpos / BITS_PER_UNIT, | |
14489 | bitsize / BITS_PER_UNIT); | |
14490 | if (v) | |
14491 | return v; | |
14492 | } | |
14493 | } | |
14494 | } | |
14495 | ||
dcd25113 JJ |
14496 | return NULL_TREE; |
14497 | ||
16949072 RG |
14498 | case FMA_EXPR: |
14499 | /* For integers we can decompose the FMA if possible. */ | |
14500 | if (TREE_CODE (arg0) == INTEGER_CST | |
14501 | && TREE_CODE (arg1) == INTEGER_CST) | |
14502 | return fold_build2_loc (loc, PLUS_EXPR, type, | |
14503 | const_binop (MULT_EXPR, arg0, arg1), arg2); | |
14504 | if (integer_zerop (arg2)) | |
14505 | return fold_build2_loc (loc, MULT_EXPR, type, arg0, arg1); | |
14506 | ||
14507 | return fold_fma (loc, type, arg0, arg1, arg2); | |
14508 | ||
22d8712a JJ |
14509 | case VEC_PERM_EXPR: |
14510 | if (TREE_CODE (arg2) == VECTOR_CST) | |
14511 | { | |
8a3ffc5d | 14512 | unsigned int nelts = TYPE_VECTOR_SUBPARTS (type), i, mask; |
22d8712a JJ |
14513 | unsigned char *sel = XALLOCAVEC (unsigned char, nelts); |
14514 | tree t; | |
14515 | bool need_mask_canon = false; | |
8a3ffc5d MG |
14516 | bool all_in_vec0 = true; |
14517 | bool all_in_vec1 = true; | |
14518 | bool maybe_identity = true; | |
14519 | bool single_arg = (op0 == op1); | |
14520 | bool changed = false; | |
22d8712a | 14521 | |
8a3ffc5d | 14522 | mask = single_arg ? (nelts - 1) : (2 * nelts - 1); |
d2a12ae7 RG |
14523 | gcc_assert (nelts == VECTOR_CST_NELTS (arg2)); |
14524 | for (i = 0; i < nelts; i++) | |
22d8712a | 14525 | { |
d2a12ae7 RG |
14526 | tree val = VECTOR_CST_ELT (arg2, i); |
14527 | if (TREE_CODE (val) != INTEGER_CST) | |
22d8712a JJ |
14528 | return NULL_TREE; |
14529 | ||
8a3ffc5d | 14530 | sel[i] = TREE_INT_CST_LOW (val) & mask; |
d2a12ae7 | 14531 | if (TREE_INT_CST_HIGH (val) |
22d8712a | 14532 | || ((unsigned HOST_WIDE_INT) |
d2a12ae7 | 14533 | TREE_INT_CST_LOW (val) != sel[i])) |
22d8712a | 14534 | need_mask_canon = true; |
8a3ffc5d MG |
14535 | |
14536 | if (sel[i] < nelts) | |
14537 | all_in_vec1 = false; | |
14538 | else | |
14539 | all_in_vec0 = false; | |
14540 | ||
14541 | if ((sel[i] & (nelts-1)) != i) | |
14542 | maybe_identity = false; | |
14543 | } | |
14544 | ||
14545 | if (maybe_identity) | |
14546 | { | |
14547 | if (all_in_vec0) | |
14548 | return op0; | |
14549 | if (all_in_vec1) | |
14550 | return op1; | |
22d8712a | 14551 | } |
22d8712a | 14552 | |
8a3ffc5d MG |
14553 | if (all_in_vec0) |
14554 | op1 = op0; | |
14555 | else if (all_in_vec1) | |
14556 | { | |
14557 | op0 = op1; | |
14558 | for (i = 0; i < nelts; i++) | |
14559 | sel[i] -= nelts; | |
14560 | need_mask_canon = true; | |
14561 | } | |
14562 | ||
aa369472 MG |
14563 | if ((TREE_CODE (op0) == VECTOR_CST |
14564 | || TREE_CODE (op0) == CONSTRUCTOR) | |
14565 | && (TREE_CODE (op1) == VECTOR_CST | |
14566 | || TREE_CODE (op1) == CONSTRUCTOR)) | |
14567 | { | |
14568 | t = fold_vec_perm (type, op0, op1, sel); | |
14569 | if (t != NULL_TREE) | |
14570 | return t; | |
14571 | } | |
14572 | ||
8a3ffc5d MG |
14573 | if (op0 == op1 && !single_arg) |
14574 | changed = true; | |
14575 | ||
22d8712a JJ |
14576 | if (need_mask_canon && arg2 == op2) |
14577 | { | |
d2a12ae7 RG |
14578 | tree *tsel = XALLOCAVEC (tree, nelts); |
14579 | tree eltype = TREE_TYPE (TREE_TYPE (arg2)); | |
22d8712a | 14580 | for (i = 0; i < nelts; i++) |
09e4850d | 14581 | tsel[i] = build_int_cst (eltype, sel[i]); |
8a3ffc5d MG |
14582 | op2 = build_vector (TREE_TYPE (arg2), tsel); |
14583 | changed = true; | |
22d8712a | 14584 | } |
8a3ffc5d MG |
14585 | |
14586 | if (changed) | |
14587 | return build3_loc (loc, VEC_PERM_EXPR, type, op0, op1, op2); | |
22d8712a JJ |
14588 | } |
14589 | return NULL_TREE; | |
14590 | ||
9bdae6af | 14591 | default: |
62ab45cc | 14592 | return NULL_TREE; |
9bdae6af KH |
14593 | } /* switch (code) */ |
14594 | } | |
14595 | ||
6d716ca8 RS |
14596 | /* Perform constant folding and related simplification of EXPR. |
14597 | The related simplifications include x*1 => x, x*0 => 0, etc., | |
14598 | and application of the associative law. | |
14599 | NOP_EXPR conversions may be removed freely (as long as we | |
af5bdf6a | 14600 | are careful not to change the type of the overall expression). |
6d716ca8 RS |
14601 | We cannot simplify through a CONVERT_EXPR, FIX_EXPR or FLOAT_EXPR, |
14602 | but we can constant-fold them if they have constant operands. */ | |
14603 | ||
5dfa45d0 JJ |
14604 | #ifdef ENABLE_FOLD_CHECKING |
14605 | # define fold(x) fold_1 (x) | |
14606 | static tree fold_1 (tree); | |
14607 | static | |
14608 | #endif | |
6d716ca8 | 14609 | tree |
fa8db1f7 | 14610 | fold (tree expr) |
6d716ca8 | 14611 | { |
ea993805 | 14612 | const tree t = expr; |
b3694847 | 14613 | enum tree_code code = TREE_CODE (t); |
6615c446 | 14614 | enum tree_code_class kind = TREE_CODE_CLASS (code); |
62ab45cc | 14615 | tree tem; |
db3927fb | 14616 | location_t loc = EXPR_LOCATION (expr); |
6de9cd9a | 14617 | |
1796dff4 | 14618 | /* Return right away if a constant. */ |
6615c446 | 14619 | if (kind == tcc_constant) |
1796dff4 | 14620 | return t; |
b6cc0a72 | 14621 | |
5039610b SL |
14622 | /* CALL_EXPR-like objects with variable numbers of operands are |
14623 | treated specially. */ | |
14624 | if (kind == tcc_vl_exp) | |
14625 | { | |
14626 | if (code == CALL_EXPR) | |
14627 | { | |
db3927fb | 14628 | tem = fold_call_expr (loc, expr, false); |
5039610b SL |
14629 | return tem ? tem : expr; |
14630 | } | |
14631 | return expr; | |
14632 | } | |
14633 | ||
726a989a | 14634 | if (IS_EXPR_CODE_CLASS (kind)) |
659d8efa | 14635 | { |
fbaa905c | 14636 | tree type = TREE_TYPE (t); |
7cf57259 | 14637 | tree op0, op1, op2; |
fbaa905c | 14638 | |
659d8efa KH |
14639 | switch (TREE_CODE_LENGTH (code)) |
14640 | { | |
14641 | case 1: | |
fbaa905c | 14642 | op0 = TREE_OPERAND (t, 0); |
db3927fb | 14643 | tem = fold_unary_loc (loc, code, type, op0); |
62ab45cc | 14644 | return tem ? tem : expr; |
0aee4751 | 14645 | case 2: |
fbaa905c KH |
14646 | op0 = TREE_OPERAND (t, 0); |
14647 | op1 = TREE_OPERAND (t, 1); | |
db3927fb | 14648 | tem = fold_binary_loc (loc, code, type, op0, op1); |
62ab45cc | 14649 | return tem ? tem : expr; |
9bdae6af | 14650 | case 3: |
7cf57259 KH |
14651 | op0 = TREE_OPERAND (t, 0); |
14652 | op1 = TREE_OPERAND (t, 1); | |
14653 | op2 = TREE_OPERAND (t, 2); | |
db3927fb | 14654 | tem = fold_ternary_loc (loc, code, type, op0, op1, op2); |
62ab45cc | 14655 | return tem ? tem : expr; |
659d8efa KH |
14656 | default: |
14657 | break; | |
14658 | } | |
14659 | } | |
14660 | ||
6d716ca8 RS |
14661 | switch (code) |
14662 | { | |
39fcde8f EB |
14663 | case ARRAY_REF: |
14664 | { | |
14665 | tree op0 = TREE_OPERAND (t, 0); | |
14666 | tree op1 = TREE_OPERAND (t, 1); | |
14667 | ||
14668 | if (TREE_CODE (op1) == INTEGER_CST | |
14669 | && TREE_CODE (op0) == CONSTRUCTOR | |
14670 | && ! type_contains_placeholder_p (TREE_TYPE (op0))) | |
14671 | { | |
9771b263 DN |
14672 | vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (op0); |
14673 | unsigned HOST_WIDE_INT end = vec_safe_length (elts); | |
39fcde8f EB |
14674 | unsigned HOST_WIDE_INT begin = 0; |
14675 | ||
14676 | /* Find a matching index by means of a binary search. */ | |
14677 | while (begin != end) | |
14678 | { | |
14679 | unsigned HOST_WIDE_INT middle = (begin + end) / 2; | |
9771b263 | 14680 | tree index = (*elts)[middle].index; |
39fcde8f EB |
14681 | |
14682 | if (TREE_CODE (index) == INTEGER_CST | |
14683 | && tree_int_cst_lt (index, op1)) | |
14684 | begin = middle + 1; | |
14685 | else if (TREE_CODE (index) == INTEGER_CST | |
14686 | && tree_int_cst_lt (op1, index)) | |
14687 | end = middle; | |
14688 | else if (TREE_CODE (index) == RANGE_EXPR | |
14689 | && tree_int_cst_lt (TREE_OPERAND (index, 1), op1)) | |
14690 | begin = middle + 1; | |
14691 | else if (TREE_CODE (index) == RANGE_EXPR | |
14692 | && tree_int_cst_lt (op1, TREE_OPERAND (index, 0))) | |
14693 | end = middle; | |
14694 | else | |
9771b263 | 14695 | return (*elts)[middle].value; |
39fcde8f EB |
14696 | } |
14697 | } | |
14698 | ||
14699 | return t; | |
14700 | } | |
14701 | ||
d5a1053a MG |
14702 | /* Return a VECTOR_CST if possible. */ |
14703 | case CONSTRUCTOR: | |
14704 | { | |
14705 | tree type = TREE_TYPE (t); | |
14706 | if (TREE_CODE (type) != VECTOR_TYPE) | |
14707 | return t; | |
14708 | ||
14709 | tree *vec = XALLOCAVEC (tree, TYPE_VECTOR_SUBPARTS (type)); | |
14710 | unsigned HOST_WIDE_INT idx, pos = 0; | |
14711 | tree value; | |
14712 | ||
14713 | FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (t), idx, value) | |
14714 | { | |
14715 | if (!CONSTANT_CLASS_P (value)) | |
14716 | return t; | |
14717 | if (TREE_CODE (value) == VECTOR_CST) | |
14718 | { | |
14719 | for (unsigned i = 0; i < VECTOR_CST_NELTS (value); ++i) | |
14720 | vec[pos++] = VECTOR_CST_ELT (value, i); | |
14721 | } | |
14722 | else | |
14723 | vec[pos++] = value; | |
14724 | } | |
14725 | for (; pos < TYPE_VECTOR_SUBPARTS (type); ++pos) | |
14726 | vec[pos] = build_zero_cst (TREE_TYPE (type)); | |
14727 | ||
14728 | return build_vector (type, vec); | |
14729 | } | |
14730 | ||
6d716ca8 RS |
14731 | case CONST_DECL: |
14732 | return fold (DECL_INITIAL (t)); | |
14733 | ||
6d716ca8 RS |
14734 | default: |
14735 | return t; | |
14736 | } /* switch (code) */ | |
14737 | } | |
39dfb55a | 14738 | |
5dfa45d0 JJ |
14739 | #ifdef ENABLE_FOLD_CHECKING |
14740 | #undef fold | |
14741 | ||
703c8606 LC |
14742 | static void fold_checksum_tree (const_tree, struct md5_ctx *, |
14743 | hash_table <pointer_hash <tree_node> >); | |
ac545c64 KG |
14744 | static void fold_check_failed (const_tree, const_tree); |
14745 | void print_fold_checksum (const_tree); | |
5dfa45d0 JJ |
14746 | |
14747 | /* When --enable-checking=fold, compute a digest of expr before | |
14748 | and after actual fold call to see if fold did not accidentally | |
14749 | change original expr. */ | |
14750 | ||
14751 | tree | |
14752 | fold (tree expr) | |
14753 | { | |
14754 | tree ret; | |
14755 | struct md5_ctx ctx; | |
14756 | unsigned char checksum_before[16], checksum_after[16]; | |
703c8606 | 14757 | hash_table <pointer_hash <tree_node> > ht; |
5dfa45d0 | 14758 | |
703c8606 | 14759 | ht.create (32); |
5dfa45d0 JJ |
14760 | md5_init_ctx (&ctx); |
14761 | fold_checksum_tree (expr, &ctx, ht); | |
14762 | md5_finish_ctx (&ctx, checksum_before); | |
703c8606 | 14763 | ht.empty (); |
5dfa45d0 JJ |
14764 | |
14765 | ret = fold_1 (expr); | |
14766 | ||
14767 | md5_init_ctx (&ctx); | |
14768 | fold_checksum_tree (expr, &ctx, ht); | |
14769 | md5_finish_ctx (&ctx, checksum_after); | |
703c8606 | 14770 | ht.dispose (); |
5dfa45d0 JJ |
14771 | |
14772 | if (memcmp (checksum_before, checksum_after, 16)) | |
14773 | fold_check_failed (expr, ret); | |
14774 | ||
14775 | return ret; | |
14776 | } | |
14777 | ||
14778 | void | |
ac545c64 | 14779 | print_fold_checksum (const_tree expr) |
5dfa45d0 JJ |
14780 | { |
14781 | struct md5_ctx ctx; | |
14782 | unsigned char checksum[16], cnt; | |
703c8606 | 14783 | hash_table <pointer_hash <tree_node> > ht; |
5dfa45d0 | 14784 | |
703c8606 | 14785 | ht.create (32); |
5dfa45d0 JJ |
14786 | md5_init_ctx (&ctx); |
14787 | fold_checksum_tree (expr, &ctx, ht); | |
14788 | md5_finish_ctx (&ctx, checksum); | |
703c8606 | 14789 | ht.dispose (); |
5dfa45d0 JJ |
14790 | for (cnt = 0; cnt < 16; ++cnt) |
14791 | fprintf (stderr, "%02x", checksum[cnt]); | |
14792 | putc ('\n', stderr); | |
14793 | } | |
14794 | ||
14795 | static void | |
ac545c64 | 14796 | fold_check_failed (const_tree expr ATTRIBUTE_UNUSED, const_tree ret ATTRIBUTE_UNUSED) |
5dfa45d0 JJ |
14797 | { |
14798 | internal_error ("fold check: original tree changed by fold"); | |
14799 | } | |
14800 | ||
14801 | static void | |
703c8606 LC |
14802 | fold_checksum_tree (const_tree expr, struct md5_ctx *ctx, |
14803 | hash_table <pointer_hash <tree_node> > ht) | |
5dfa45d0 | 14804 | { |
703c8606 | 14805 | tree_node **slot; |
5dfa45d0 | 14806 | enum tree_code code; |
ea6dafb0 | 14807 | union tree_node buf; |
5dfa45d0 | 14808 | int i, len; |
b8698a0f | 14809 | |
dca80466 | 14810 | recursive_label: |
5dfa45d0 JJ |
14811 | if (expr == NULL) |
14812 | return; | |
703c8606 | 14813 | slot = ht.find_slot (expr, INSERT); |
5dfa45d0 JJ |
14814 | if (*slot != NULL) |
14815 | return; | |
0c3dbcf0 | 14816 | *slot = CONST_CAST_TREE (expr); |
5dfa45d0 | 14817 | code = TREE_CODE (expr); |
6615c446 JO |
14818 | if (TREE_CODE_CLASS (code) == tcc_declaration |
14819 | && DECL_ASSEMBLER_NAME_SET_P (expr)) | |
5dfa45d0 JJ |
14820 | { |
14821 | /* Allow DECL_ASSEMBLER_NAME to be modified. */ | |
3f7f53c7 | 14822 | memcpy ((char *) &buf, expr, tree_size (expr)); |
ac545c64 | 14823 | SET_DECL_ASSEMBLER_NAME ((tree)&buf, NULL); |
3f7f53c7 | 14824 | expr = (tree) &buf; |
5dfa45d0 | 14825 | } |
6615c446 | 14826 | else if (TREE_CODE_CLASS (code) == tcc_type |
5cf96841 JJ |
14827 | && (TYPE_POINTER_TO (expr) |
14828 | || TYPE_REFERENCE_TO (expr) | |
d763bb10 | 14829 | || TYPE_CACHED_VALUES_P (expr) |
5cf96841 JJ |
14830 | || TYPE_CONTAINS_PLACEHOLDER_INTERNAL (expr) |
14831 | || TYPE_NEXT_VARIANT (expr))) | |
5dfa45d0 | 14832 | { |
b9193259 | 14833 | /* Allow these fields to be modified. */ |
ac545c64 | 14834 | tree tmp; |
3f7f53c7 | 14835 | memcpy ((char *) &buf, expr, tree_size (expr)); |
ac545c64 KG |
14836 | expr = tmp = (tree) &buf; |
14837 | TYPE_CONTAINS_PLACEHOLDER_INTERNAL (tmp) = 0; | |
14838 | TYPE_POINTER_TO (tmp) = NULL; | |
14839 | TYPE_REFERENCE_TO (tmp) = NULL; | |
5cf96841 | 14840 | TYPE_NEXT_VARIANT (tmp) = NULL; |
ac545c64 | 14841 | if (TYPE_CACHED_VALUES_P (tmp)) |
0ebfd2c9 | 14842 | { |
ac545c64 KG |
14843 | TYPE_CACHED_VALUES_P (tmp) = 0; |
14844 | TYPE_CACHED_VALUES (tmp) = NULL; | |
0ebfd2c9 | 14845 | } |
5dfa45d0 JJ |
14846 | } |
14847 | md5_process_bytes (expr, tree_size (expr), ctx); | |
fff6a306 UB |
14848 | if (CODE_CONTAINS_STRUCT (code, TS_TYPED)) |
14849 | fold_checksum_tree (TREE_TYPE (expr), ctx, ht); | |
6615c446 | 14850 | if (TREE_CODE_CLASS (code) != tcc_type |
d763bb10 | 14851 | && TREE_CODE_CLASS (code) != tcc_declaration |
70826cbb | 14852 | && code != TREE_LIST |
5fb43dd8 TV |
14853 | && code != SSA_NAME |
14854 | && CODE_CONTAINS_STRUCT (code, TS_COMMON)) | |
5dfa45d0 | 14855 | fold_checksum_tree (TREE_CHAIN (expr), ctx, ht); |
5dfa45d0 JJ |
14856 | switch (TREE_CODE_CLASS (code)) |
14857 | { | |
6615c446 | 14858 | case tcc_constant: |
5dfa45d0 JJ |
14859 | switch (code) |
14860 | { | |
14861 | case STRING_CST: | |
14862 | md5_process_bytes (TREE_STRING_POINTER (expr), | |
14863 | TREE_STRING_LENGTH (expr), ctx); | |
14864 | break; | |
14865 | case COMPLEX_CST: | |
14866 | fold_checksum_tree (TREE_REALPART (expr), ctx, ht); | |
14867 | fold_checksum_tree (TREE_IMAGPART (expr), ctx, ht); | |
14868 | break; | |
14869 | case VECTOR_CST: | |
b7532479 | 14870 | for (i = 0; i < (int) VECTOR_CST_NELTS (expr); ++i) |
1a14ac4b | 14871 | fold_checksum_tree (VECTOR_CST_ELT (expr, i), ctx, ht); |
5dfa45d0 JJ |
14872 | break; |
14873 | default: | |
14874 | break; | |
14875 | } | |
14876 | break; | |
6615c446 | 14877 | case tcc_exceptional: |
5dfa45d0 JJ |
14878 | switch (code) |
14879 | { | |
14880 | case TREE_LIST: | |
14881 | fold_checksum_tree (TREE_PURPOSE (expr), ctx, ht); | |
14882 | fold_checksum_tree (TREE_VALUE (expr), ctx, ht); | |
d763bb10 AP |
14883 | expr = TREE_CHAIN (expr); |
14884 | goto recursive_label; | |
5dfa45d0 JJ |
14885 | break; |
14886 | case TREE_VEC: | |
14887 | for (i = 0; i < TREE_VEC_LENGTH (expr); ++i) | |
14888 | fold_checksum_tree (TREE_VEC_ELT (expr, i), ctx, ht); | |
14889 | break; | |
14890 | default: | |
14891 | break; | |
14892 | } | |
14893 | break; | |
6615c446 JO |
14894 | case tcc_expression: |
14895 | case tcc_reference: | |
14896 | case tcc_comparison: | |
14897 | case tcc_unary: | |
14898 | case tcc_binary: | |
14899 | case tcc_statement: | |
5039610b SL |
14900 | case tcc_vl_exp: |
14901 | len = TREE_OPERAND_LENGTH (expr); | |
5dfa45d0 JJ |
14902 | for (i = 0; i < len; ++i) |
14903 | fold_checksum_tree (TREE_OPERAND (expr, i), ctx, ht); | |
14904 | break; | |
6615c446 | 14905 | case tcc_declaration: |
5dfa45d0 JJ |
14906 | fold_checksum_tree (DECL_NAME (expr), ctx, ht); |
14907 | fold_checksum_tree (DECL_CONTEXT (expr), ctx, ht); | |
3eb04608 DB |
14908 | if (CODE_CONTAINS_STRUCT (TREE_CODE (expr), TS_DECL_COMMON)) |
14909 | { | |
14910 | fold_checksum_tree (DECL_SIZE (expr), ctx, ht); | |
14911 | fold_checksum_tree (DECL_SIZE_UNIT (expr), ctx, ht); | |
14912 | fold_checksum_tree (DECL_INITIAL (expr), ctx, ht); | |
14913 | fold_checksum_tree (DECL_ABSTRACT_ORIGIN (expr), ctx, ht); | |
14914 | fold_checksum_tree (DECL_ATTRIBUTES (expr), ctx, ht); | |
14915 | } | |
46c5394b DB |
14916 | if (CODE_CONTAINS_STRUCT (TREE_CODE (expr), TS_DECL_WITH_VIS)) |
14917 | fold_checksum_tree (DECL_SECTION_NAME (expr), ctx, ht); | |
b8698a0f | 14918 | |
46c5394b DB |
14919 | if (CODE_CONTAINS_STRUCT (TREE_CODE (expr), TS_DECL_NON_COMMON)) |
14920 | { | |
14921 | fold_checksum_tree (DECL_VINDEX (expr), ctx, ht); | |
14922 | fold_checksum_tree (DECL_RESULT_FLD (expr), ctx, ht); | |
14923 | fold_checksum_tree (DECL_ARGUMENT_FLD (expr), ctx, ht); | |
14924 | } | |
5dfa45d0 | 14925 | break; |
6615c446 | 14926 | case tcc_type: |
a40de696 AP |
14927 | if (TREE_CODE (expr) == ENUMERAL_TYPE) |
14928 | fold_checksum_tree (TYPE_VALUES (expr), ctx, ht); | |
5dfa45d0 JJ |
14929 | fold_checksum_tree (TYPE_SIZE (expr), ctx, ht); |
14930 | fold_checksum_tree (TYPE_SIZE_UNIT (expr), ctx, ht); | |
14931 | fold_checksum_tree (TYPE_ATTRIBUTES (expr), ctx, ht); | |
14932 | fold_checksum_tree (TYPE_NAME (expr), ctx, ht); | |
a40de696 AP |
14933 | if (INTEGRAL_TYPE_P (expr) |
14934 | || SCALAR_FLOAT_TYPE_P (expr)) | |
14935 | { | |
14936 | fold_checksum_tree (TYPE_MIN_VALUE (expr), ctx, ht); | |
14937 | fold_checksum_tree (TYPE_MAX_VALUE (expr), ctx, ht); | |
14938 | } | |
5dfa45d0 | 14939 | fold_checksum_tree (TYPE_MAIN_VARIANT (expr), ctx, ht); |
b9193259 DJ |
14940 | if (TREE_CODE (expr) == RECORD_TYPE |
14941 | || TREE_CODE (expr) == UNION_TYPE | |
14942 | || TREE_CODE (expr) == QUAL_UNION_TYPE) | |
14943 | fold_checksum_tree (TYPE_BINFO (expr), ctx, ht); | |
5dfa45d0 JJ |
14944 | fold_checksum_tree (TYPE_CONTEXT (expr), ctx, ht); |
14945 | break; | |
14946 | default: | |
14947 | break; | |
14948 | } | |
14949 | } | |
14950 | ||
f1b42630 AN |
14951 | /* Helper function for outputting the checksum of a tree T. When |
14952 | debugging with gdb, you can "define mynext" to be "next" followed | |
14953 | by "call debug_fold_checksum (op0)", then just trace down till the | |
14954 | outputs differ. */ | |
14955 | ||
24e47c76 | 14956 | DEBUG_FUNCTION void |
ac545c64 | 14957 | debug_fold_checksum (const_tree t) |
f1b42630 AN |
14958 | { |
14959 | int i; | |
14960 | unsigned char checksum[16]; | |
14961 | struct md5_ctx ctx; | |
703c8606 LC |
14962 | hash_table <pointer_hash <tree_node> > ht; |
14963 | ht.create (32); | |
b8698a0f | 14964 | |
f1b42630 AN |
14965 | md5_init_ctx (&ctx); |
14966 | fold_checksum_tree (t, &ctx, ht); | |
14967 | md5_finish_ctx (&ctx, checksum); | |
703c8606 | 14968 | ht.empty (); |
f1b42630 AN |
14969 | |
14970 | for (i = 0; i < 16; i++) | |
14971 | fprintf (stderr, "%d ", checksum[i]); | |
14972 | ||
14973 | fprintf (stderr, "\n"); | |
14974 | } | |
14975 | ||
5dfa45d0 JJ |
14976 | #endif |
14977 | ||
ba199a53 | 14978 | /* Fold a unary tree expression with code CODE of type TYPE with an |
db3927fb AH |
14979 | operand OP0. LOC is the location of the resulting expression. |
14980 | Return a folded expression if successful. Otherwise, return a tree | |
14981 | expression with code CODE of type TYPE with an operand OP0. */ | |
ba199a53 KH |
14982 | |
14983 | tree | |
db3927fb AH |
14984 | fold_build1_stat_loc (location_t loc, |
14985 | enum tree_code code, tree type, tree op0 MEM_STAT_DECL) | |
ba199a53 | 14986 | { |
e2fe73f6 AP |
14987 | tree tem; |
14988 | #ifdef ENABLE_FOLD_CHECKING | |
14989 | unsigned char checksum_before[16], checksum_after[16]; | |
14990 | struct md5_ctx ctx; | |
703c8606 | 14991 | hash_table <pointer_hash <tree_node> > ht; |
e2fe73f6 | 14992 | |
703c8606 | 14993 | ht.create (32); |
e2fe73f6 AP |
14994 | md5_init_ctx (&ctx); |
14995 | fold_checksum_tree (op0, &ctx, ht); | |
14996 | md5_finish_ctx (&ctx, checksum_before); | |
703c8606 | 14997 | ht.empty (); |
e2fe73f6 | 14998 | #endif |
b8698a0f | 14999 | |
db3927fb | 15000 | tem = fold_unary_loc (loc, code, type, op0); |
e2fe73f6 | 15001 | if (!tem) |
c9019218 | 15002 | tem = build1_stat_loc (loc, code, type, op0 PASS_MEM_STAT); |
b8698a0f | 15003 | |
e2fe73f6 AP |
15004 | #ifdef ENABLE_FOLD_CHECKING |
15005 | md5_init_ctx (&ctx); | |
15006 | fold_checksum_tree (op0, &ctx, ht); | |
15007 | md5_finish_ctx (&ctx, checksum_after); | |
703c8606 | 15008 | ht.dispose (); |
ba199a53 | 15009 | |
e2fe73f6 AP |
15010 | if (memcmp (checksum_before, checksum_after, 16)) |
15011 | fold_check_failed (op0, tem); | |
15012 | #endif | |
15013 | return tem; | |
ba199a53 KH |
15014 | } |
15015 | ||
15016 | /* Fold a binary tree expression with code CODE of type TYPE with | |
db3927fb AH |
15017 | operands OP0 and OP1. LOC is the location of the resulting |
15018 | expression. Return a folded expression if successful. Otherwise, | |
15019 | return a tree expression with code CODE of type TYPE with operands | |
15020 | OP0 and OP1. */ | |
ba199a53 KH |
15021 | |
15022 | tree | |
db3927fb AH |
15023 | fold_build2_stat_loc (location_t loc, |
15024 | enum tree_code code, tree type, tree op0, tree op1 | |
15025 | MEM_STAT_DECL) | |
ba199a53 | 15026 | { |
e2fe73f6 AP |
15027 | tree tem; |
15028 | #ifdef ENABLE_FOLD_CHECKING | |
15029 | unsigned char checksum_before_op0[16], | |
15030 | checksum_before_op1[16], | |
15031 | checksum_after_op0[16], | |
15032 | checksum_after_op1[16]; | |
15033 | struct md5_ctx ctx; | |
703c8606 | 15034 | hash_table <pointer_hash <tree_node> > ht; |
e2fe73f6 | 15035 | |
703c8606 | 15036 | ht.create (32); |
e2fe73f6 AP |
15037 | md5_init_ctx (&ctx); |
15038 | fold_checksum_tree (op0, &ctx, ht); | |
15039 | md5_finish_ctx (&ctx, checksum_before_op0); | |
703c8606 | 15040 | ht.empty (); |
e2fe73f6 AP |
15041 | |
15042 | md5_init_ctx (&ctx); | |
15043 | fold_checksum_tree (op1, &ctx, ht); | |
15044 | md5_finish_ctx (&ctx, checksum_before_op1); | |
703c8606 | 15045 | ht.empty (); |
e2fe73f6 AP |
15046 | #endif |
15047 | ||
db3927fb | 15048 | tem = fold_binary_loc (loc, code, type, op0, op1); |
e2fe73f6 | 15049 | if (!tem) |
c9019218 | 15050 | tem = build2_stat_loc (loc, code, type, op0, op1 PASS_MEM_STAT); |
b8698a0f | 15051 | |
e2fe73f6 AP |
15052 | #ifdef ENABLE_FOLD_CHECKING |
15053 | md5_init_ctx (&ctx); | |
15054 | fold_checksum_tree (op0, &ctx, ht); | |
15055 | md5_finish_ctx (&ctx, checksum_after_op0); | |
703c8606 | 15056 | ht.empty (); |
e2fe73f6 AP |
15057 | |
15058 | if (memcmp (checksum_before_op0, checksum_after_op0, 16)) | |
15059 | fold_check_failed (op0, tem); | |
b8698a0f | 15060 | |
e2fe73f6 AP |
15061 | md5_init_ctx (&ctx); |
15062 | fold_checksum_tree (op1, &ctx, ht); | |
15063 | md5_finish_ctx (&ctx, checksum_after_op1); | |
703c8606 | 15064 | ht.dispose (); |
ba199a53 | 15065 | |
e2fe73f6 AP |
15066 | if (memcmp (checksum_before_op1, checksum_after_op1, 16)) |
15067 | fold_check_failed (op1, tem); | |
15068 | #endif | |
15069 | return tem; | |
ba199a53 KH |
15070 | } |
15071 | ||
15072 | /* Fold a ternary tree expression with code CODE of type TYPE with | |
830113fd | 15073 | operands OP0, OP1, and OP2. Return a folded expression if |
ba199a53 KH |
15074 | successful. Otherwise, return a tree expression with code CODE of |
15075 | type TYPE with operands OP0, OP1, and OP2. */ | |
15076 | ||
15077 | tree | |
db3927fb AH |
15078 | fold_build3_stat_loc (location_t loc, enum tree_code code, tree type, |
15079 | tree op0, tree op1, tree op2 MEM_STAT_DECL) | |
5808968e AP |
15080 | { |
15081 | tree tem; | |
e2fe73f6 AP |
15082 | #ifdef ENABLE_FOLD_CHECKING |
15083 | unsigned char checksum_before_op0[16], | |
15084 | checksum_before_op1[16], | |
15085 | checksum_before_op2[16], | |
15086 | checksum_after_op0[16], | |
15087 | checksum_after_op1[16], | |
15088 | checksum_after_op2[16]; | |
15089 | struct md5_ctx ctx; | |
703c8606 | 15090 | hash_table <pointer_hash <tree_node> > ht; |
e2fe73f6 | 15091 | |
703c8606 | 15092 | ht.create (32); |
e2fe73f6 AP |
15093 | md5_init_ctx (&ctx); |
15094 | fold_checksum_tree (op0, &ctx, ht); | |
15095 | md5_finish_ctx (&ctx, checksum_before_op0); | |
703c8606 | 15096 | ht.empty (); |
ba199a53 | 15097 | |
e2fe73f6 AP |
15098 | md5_init_ctx (&ctx); |
15099 | fold_checksum_tree (op1, &ctx, ht); | |
15100 | md5_finish_ctx (&ctx, checksum_before_op1); | |
703c8606 | 15101 | ht.empty (); |
e2fe73f6 AP |
15102 | |
15103 | md5_init_ctx (&ctx); | |
15104 | fold_checksum_tree (op2, &ctx, ht); | |
15105 | md5_finish_ctx (&ctx, checksum_before_op2); | |
703c8606 | 15106 | ht.empty (); |
e2fe73f6 | 15107 | #endif |
5039610b SL |
15108 | |
15109 | gcc_assert (TREE_CODE_CLASS (code) != tcc_vl_exp); | |
db3927fb | 15110 | tem = fold_ternary_loc (loc, code, type, op0, op1, op2); |
e2fe73f6 | 15111 | if (!tem) |
c9019218 | 15112 | tem = build3_stat_loc (loc, code, type, op0, op1, op2 PASS_MEM_STAT); |
b8698a0f | 15113 | |
e2fe73f6 AP |
15114 | #ifdef ENABLE_FOLD_CHECKING |
15115 | md5_init_ctx (&ctx); | |
15116 | fold_checksum_tree (op0, &ctx, ht); | |
15117 | md5_finish_ctx (&ctx, checksum_after_op0); | |
703c8606 | 15118 | ht.empty (); |
e2fe73f6 AP |
15119 | |
15120 | if (memcmp (checksum_before_op0, checksum_after_op0, 16)) | |
15121 | fold_check_failed (op0, tem); | |
b8698a0f | 15122 | |
e2fe73f6 AP |
15123 | md5_init_ctx (&ctx); |
15124 | fold_checksum_tree (op1, &ctx, ht); | |
15125 | md5_finish_ctx (&ctx, checksum_after_op1); | |
703c8606 | 15126 | ht.empty (); |
e2fe73f6 AP |
15127 | |
15128 | if (memcmp (checksum_before_op1, checksum_after_op1, 16)) | |
15129 | fold_check_failed (op1, tem); | |
b8698a0f | 15130 | |
e2fe73f6 AP |
15131 | md5_init_ctx (&ctx); |
15132 | fold_checksum_tree (op2, &ctx, ht); | |
15133 | md5_finish_ctx (&ctx, checksum_after_op2); | |
703c8606 | 15134 | ht.dispose (); |
e2fe73f6 AP |
15135 | |
15136 | if (memcmp (checksum_before_op2, checksum_after_op2, 16)) | |
15137 | fold_check_failed (op2, tem); | |
15138 | #endif | |
15139 | return tem; | |
ba199a53 KH |
15140 | } |
15141 | ||
94a0dd7b SL |
15142 | /* Fold a CALL_EXPR expression of type TYPE with operands FN and NARGS |
15143 | arguments in ARGARRAY, and a null static chain. | |
5039610b | 15144 | Return a folded expression if successful. Otherwise, return a CALL_EXPR |
94a0dd7b | 15145 | of type TYPE from the given operands as constructed by build_call_array. */ |
5039610b SL |
15146 | |
15147 | tree | |
db3927fb AH |
15148 | fold_build_call_array_loc (location_t loc, tree type, tree fn, |
15149 | int nargs, tree *argarray) | |
5039610b SL |
15150 | { |
15151 | tree tem; | |
15152 | #ifdef ENABLE_FOLD_CHECKING | |
15153 | unsigned char checksum_before_fn[16], | |
15154 | checksum_before_arglist[16], | |
15155 | checksum_after_fn[16], | |
15156 | checksum_after_arglist[16]; | |
15157 | struct md5_ctx ctx; | |
703c8606 | 15158 | hash_table <pointer_hash <tree_node> > ht; |
94a0dd7b | 15159 | int i; |
5039610b | 15160 | |
703c8606 | 15161 | ht.create (32); |
5039610b SL |
15162 | md5_init_ctx (&ctx); |
15163 | fold_checksum_tree (fn, &ctx, ht); | |
15164 | md5_finish_ctx (&ctx, checksum_before_fn); | |
703c8606 | 15165 | ht.empty (); |
5039610b SL |
15166 | |
15167 | md5_init_ctx (&ctx); | |
94a0dd7b SL |
15168 | for (i = 0; i < nargs; i++) |
15169 | fold_checksum_tree (argarray[i], &ctx, ht); | |
5039610b | 15170 | md5_finish_ctx (&ctx, checksum_before_arglist); |
703c8606 | 15171 | ht.empty (); |
5039610b SL |
15172 | #endif |
15173 | ||
db3927fb | 15174 | tem = fold_builtin_call_array (loc, type, fn, nargs, argarray); |
b8698a0f | 15175 | |
5039610b SL |
15176 | #ifdef ENABLE_FOLD_CHECKING |
15177 | md5_init_ctx (&ctx); | |
15178 | fold_checksum_tree (fn, &ctx, ht); | |
15179 | md5_finish_ctx (&ctx, checksum_after_fn); | |
703c8606 | 15180 | ht.empty (); |
5039610b SL |
15181 | |
15182 | if (memcmp (checksum_before_fn, checksum_after_fn, 16)) | |
15183 | fold_check_failed (fn, tem); | |
b8698a0f | 15184 | |
5039610b | 15185 | md5_init_ctx (&ctx); |
94a0dd7b SL |
15186 | for (i = 0; i < nargs; i++) |
15187 | fold_checksum_tree (argarray[i], &ctx, ht); | |
5039610b | 15188 | md5_finish_ctx (&ctx, checksum_after_arglist); |
703c8606 | 15189 | ht.dispose (); |
5039610b SL |
15190 | |
15191 | if (memcmp (checksum_before_arglist, checksum_after_arglist, 16)) | |
94a0dd7b | 15192 | fold_check_failed (NULL_TREE, tem); |
5039610b SL |
15193 | #endif |
15194 | return tem; | |
15195 | } | |
15196 | ||
a98ebe2e | 15197 | /* Perform constant folding and related simplification of initializer |
00d1b1d6 | 15198 | expression EXPR. These behave identically to "fold_buildN" but ignore |
3e4093b6 RS |
15199 | potential run-time traps and exceptions that fold must preserve. */ |
15200 | ||
00d1b1d6 JM |
15201 | #define START_FOLD_INIT \ |
15202 | int saved_signaling_nans = flag_signaling_nans;\ | |
15203 | int saved_trapping_math = flag_trapping_math;\ | |
15204 | int saved_rounding_math = flag_rounding_math;\ | |
15205 | int saved_trapv = flag_trapv;\ | |
63b48197 | 15206 | int saved_folding_initializer = folding_initializer;\ |
00d1b1d6 JM |
15207 | flag_signaling_nans = 0;\ |
15208 | flag_trapping_math = 0;\ | |
15209 | flag_rounding_math = 0;\ | |
63b48197 MS |
15210 | flag_trapv = 0;\ |
15211 | folding_initializer = 1; | |
00d1b1d6 JM |
15212 | |
15213 | #define END_FOLD_INIT \ | |
15214 | flag_signaling_nans = saved_signaling_nans;\ | |
15215 | flag_trapping_math = saved_trapping_math;\ | |
15216 | flag_rounding_math = saved_rounding_math;\ | |
63b48197 MS |
15217 | flag_trapv = saved_trapv;\ |
15218 | folding_initializer = saved_folding_initializer; | |
00d1b1d6 JM |
15219 | |
15220 | tree | |
db3927fb AH |
15221 | fold_build1_initializer_loc (location_t loc, enum tree_code code, |
15222 | tree type, tree op) | |
00d1b1d6 JM |
15223 | { |
15224 | tree result; | |
15225 | START_FOLD_INIT; | |
15226 | ||
db3927fb | 15227 | result = fold_build1_loc (loc, code, type, op); |
00d1b1d6 JM |
15228 | |
15229 | END_FOLD_INIT; | |
15230 | return result; | |
15231 | } | |
15232 | ||
3e4093b6 | 15233 | tree |
db3927fb AH |
15234 | fold_build2_initializer_loc (location_t loc, enum tree_code code, |
15235 | tree type, tree op0, tree op1) | |
3e4093b6 | 15236 | { |
3e4093b6 | 15237 | tree result; |
00d1b1d6 JM |
15238 | START_FOLD_INIT; |
15239 | ||
db3927fb | 15240 | result = fold_build2_loc (loc, code, type, op0, op1); |
3e4093b6 | 15241 | |
00d1b1d6 JM |
15242 | END_FOLD_INIT; |
15243 | return result; | |
15244 | } | |
3e4093b6 | 15245 | |
00d1b1d6 | 15246 | tree |
db3927fb AH |
15247 | fold_build3_initializer_loc (location_t loc, enum tree_code code, |
15248 | tree type, tree op0, tree op1, tree op2) | |
00d1b1d6 JM |
15249 | { |
15250 | tree result; | |
15251 | START_FOLD_INIT; | |
3e4093b6 | 15252 | |
db3927fb | 15253 | result = fold_build3_loc (loc, code, type, op0, op1, op2); |
3e4093b6 | 15254 | |
00d1b1d6 | 15255 | END_FOLD_INIT; |
3e4093b6 RS |
15256 | return result; |
15257 | } | |
15258 | ||
5039610b | 15259 | tree |
db3927fb AH |
15260 | fold_build_call_array_initializer_loc (location_t loc, tree type, tree fn, |
15261 | int nargs, tree *argarray) | |
5039610b SL |
15262 | { |
15263 | tree result; | |
15264 | START_FOLD_INIT; | |
15265 | ||
db3927fb | 15266 | result = fold_build_call_array_loc (loc, type, fn, nargs, argarray); |
5039610b SL |
15267 | |
15268 | END_FOLD_INIT; | |
15269 | return result; | |
15270 | } | |
15271 | ||
00d1b1d6 JM |
15272 | #undef START_FOLD_INIT |
15273 | #undef END_FOLD_INIT | |
15274 | ||
c5c76735 JL |
15275 | /* Determine if first argument is a multiple of second argument. Return 0 if |
15276 | it is not, or we cannot easily determined it to be. | |
39dfb55a | 15277 | |
c5c76735 JL |
15278 | An example of the sort of thing we care about (at this point; this routine |
15279 | could surely be made more general, and expanded to do what the *_DIV_EXPR's | |
15280 | fold cases do now) is discovering that | |
39dfb55a JL |
15281 | |
15282 | SAVE_EXPR (I) * SAVE_EXPR (J * 8) | |
15283 | ||
15284 | is a multiple of | |
15285 | ||
15286 | SAVE_EXPR (J * 8) | |
15287 | ||
c5c76735 | 15288 | when we know that the two SAVE_EXPR (J * 8) nodes are the same node. |
39dfb55a JL |
15289 | |
15290 | This code also handles discovering that | |
15291 | ||
15292 | SAVE_EXPR (I) * SAVE_EXPR (J * 8) | |
15293 | ||
c5c76735 | 15294 | is a multiple of 8 so we don't have to worry about dealing with a |
39dfb55a JL |
15295 | possible remainder. |
15296 | ||
c5c76735 JL |
15297 | Note that we *look* inside a SAVE_EXPR only to determine how it was |
15298 | calculated; it is not safe for fold to do much of anything else with the | |
15299 | internals of a SAVE_EXPR, since it cannot know when it will be evaluated | |
15300 | at run time. For example, the latter example above *cannot* be implemented | |
15301 | as SAVE_EXPR (I) * J or any variant thereof, since the value of J at | |
15302 | evaluation time of the original SAVE_EXPR is not necessarily the same at | |
15303 | the time the new expression is evaluated. The only optimization of this | |
39dfb55a JL |
15304 | sort that would be valid is changing |
15305 | ||
15306 | SAVE_EXPR (I) * SAVE_EXPR (SAVE_EXPR (J) * 8) | |
39dfb55a | 15307 | |
c5c76735 | 15308 | divided by 8 to |
39dfb55a JL |
15309 | |
15310 | SAVE_EXPR (I) * SAVE_EXPR (J) | |
15311 | ||
15312 | (where the same SAVE_EXPR (J) is used in the original and the | |
15313 | transformed version). */ | |
15314 | ||
d4e70294 | 15315 | int |
ac545c64 | 15316 | multiple_of_p (tree type, const_tree top, const_tree bottom) |
39dfb55a JL |
15317 | { |
15318 | if (operand_equal_p (top, bottom, 0)) | |
15319 | return 1; | |
15320 | ||
15321 | if (TREE_CODE (type) != INTEGER_TYPE) | |
15322 | return 0; | |
15323 | ||
15324 | switch (TREE_CODE (top)) | |
15325 | { | |
29317008 RH |
15326 | case BIT_AND_EXPR: |
15327 | /* Bitwise and provides a power of two multiple. If the mask is | |
15328 | a multiple of BOTTOM then TOP is a multiple of BOTTOM. */ | |
15329 | if (!integer_pow2p (bottom)) | |
15330 | return 0; | |
15331 | /* FALLTHRU */ | |
15332 | ||
39dfb55a JL |
15333 | case MULT_EXPR: |
15334 | return (multiple_of_p (type, TREE_OPERAND (top, 0), bottom) | |
15335 | || multiple_of_p (type, TREE_OPERAND (top, 1), bottom)); | |
15336 | ||
15337 | case PLUS_EXPR: | |
15338 | case MINUS_EXPR: | |
15339 | return (multiple_of_p (type, TREE_OPERAND (top, 0), bottom) | |
15340 | && multiple_of_p (type, TREE_OPERAND (top, 1), bottom)); | |
15341 | ||
fba2c0cd JJ |
15342 | case LSHIFT_EXPR: |
15343 | if (TREE_CODE (TREE_OPERAND (top, 1)) == INTEGER_CST) | |
15344 | { | |
15345 | tree op1, t1; | |
15346 | ||
15347 | op1 = TREE_OPERAND (top, 1); | |
15348 | /* const_binop may not detect overflow correctly, | |
15349 | so check for it explicitly here. */ | |
15350 | if (TYPE_PRECISION (TREE_TYPE (size_one_node)) | |
15351 | > TREE_INT_CST_LOW (op1) | |
15352 | && TREE_INT_CST_HIGH (op1) == 0 | |
088414c1 RS |
15353 | && 0 != (t1 = fold_convert (type, |
15354 | const_binop (LSHIFT_EXPR, | |
15355 | size_one_node, | |
43a5d30b | 15356 | op1))) |
455f14dd | 15357 | && !TREE_OVERFLOW (t1)) |
fba2c0cd JJ |
15358 | return multiple_of_p (type, t1, bottom); |
15359 | } | |
15360 | return 0; | |
15361 | ||
39dfb55a | 15362 | case NOP_EXPR: |
c5c76735 | 15363 | /* Can't handle conversions from non-integral or wider integral type. */ |
39dfb55a JL |
15364 | if ((TREE_CODE (TREE_TYPE (TREE_OPERAND (top, 0))) != INTEGER_TYPE) |
15365 | || (TYPE_PRECISION (type) | |
15366 | < TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (top, 0))))) | |
15367 | return 0; | |
c5c76735 | 15368 | |
30f7a378 | 15369 | /* .. fall through ... */ |
c5c76735 | 15370 | |
39dfb55a JL |
15371 | case SAVE_EXPR: |
15372 | return multiple_of_p (type, TREE_OPERAND (top, 0), bottom); | |
15373 | ||
9e9ef331 EB |
15374 | case COND_EXPR: |
15375 | return (multiple_of_p (type, TREE_OPERAND (top, 1), bottom) | |
15376 | && multiple_of_p (type, TREE_OPERAND (top, 2), bottom)); | |
15377 | ||
39dfb55a | 15378 | case INTEGER_CST: |
fba2c0cd | 15379 | if (TREE_CODE (bottom) != INTEGER_CST |
81737468 | 15380 | || integer_zerop (bottom) |
8df83eae | 15381 | || (TYPE_UNSIGNED (type) |
fba2c0cd JJ |
15382 | && (tree_int_cst_sgn (top) < 0 |
15383 | || tree_int_cst_sgn (bottom) < 0))) | |
39dfb55a | 15384 | return 0; |
b73a6056 | 15385 | return integer_zerop (int_const_binop (TRUNC_MOD_EXPR, |
d35936ab | 15386 | top, bottom)); |
39dfb55a JL |
15387 | |
15388 | default: | |
15389 | return 0; | |
15390 | } | |
15391 | } | |
a36556a8 | 15392 | |
e918a58a RAE |
15393 | /* Return true if CODE or TYPE is known to be non-negative. */ |
15394 | ||
15395 | static bool | |
15396 | tree_simple_nonnegative_warnv_p (enum tree_code code, tree type) | |
15397 | { | |
15398 | if ((TYPE_PRECISION (type) != 1 || TYPE_UNSIGNED (type)) | |
15399 | && truth_value_p (code)) | |
15400 | /* Truth values evaluate to 0 or 1, which is nonnegative unless we | |
15401 | have a signed:1 type (where the value is -1 and 0). */ | |
15402 | return true; | |
15403 | return false; | |
15404 | } | |
15405 | ||
15406 | /* Return true if (CODE OP0) is known to be non-negative. If the return | |
6ac01510 ILT |
15407 | value is based on the assumption that signed overflow is undefined, |
15408 | set *STRICT_OVERFLOW_P to true; otherwise, don't change | |
15409 | *STRICT_OVERFLOW_P. */ | |
a36556a8 | 15410 | |
2d3cd5d5 | 15411 | bool |
e918a58a RAE |
15412 | tree_unary_nonnegative_warnv_p (enum tree_code code, tree type, tree op0, |
15413 | bool *strict_overflow_p) | |
a36556a8 | 15414 | { |
e918a58a | 15415 | if (TYPE_UNSIGNED (type)) |
682d0395 | 15416 | return true; |
b49ceb45 | 15417 | |
e918a58a | 15418 | switch (code) |
a36556a8 | 15419 | { |
88e3805d | 15420 | case ABS_EXPR: |
1ade5842 JM |
15421 | /* We can't return 1 if flag_wrapv is set because |
15422 | ABS_EXPR<INT_MIN> = INT_MIN. */ | |
e918a58a | 15423 | if (!INTEGRAL_TYPE_P (type)) |
eeef0e45 | 15424 | return true; |
e918a58a | 15425 | if (TYPE_OVERFLOW_UNDEFINED (type)) |
6ac01510 ILT |
15426 | { |
15427 | *strict_overflow_p = true; | |
15428 | return true; | |
15429 | } | |
1ade5842 | 15430 | break; |
7dba8395 | 15431 | |
e918a58a RAE |
15432 | case NON_LVALUE_EXPR: |
15433 | case FLOAT_EXPR: | |
15434 | case FIX_TRUNC_EXPR: | |
15435 | return tree_expr_nonnegative_warnv_p (op0, | |
15436 | strict_overflow_p); | |
f7df23be | 15437 | |
e918a58a RAE |
15438 | case NOP_EXPR: |
15439 | { | |
15440 | tree inner_type = TREE_TYPE (op0); | |
15441 | tree outer_type = type; | |
f7df23be | 15442 | |
e918a58a RAE |
15443 | if (TREE_CODE (outer_type) == REAL_TYPE) |
15444 | { | |
15445 | if (TREE_CODE (inner_type) == REAL_TYPE) | |
15446 | return tree_expr_nonnegative_warnv_p (op0, | |
15447 | strict_overflow_p); | |
d2a365a8 | 15448 | if (INTEGRAL_TYPE_P (inner_type)) |
e918a58a RAE |
15449 | { |
15450 | if (TYPE_UNSIGNED (inner_type)) | |
15451 | return true; | |
15452 | return tree_expr_nonnegative_warnv_p (op0, | |
15453 | strict_overflow_p); | |
15454 | } | |
15455 | } | |
d2a365a8 | 15456 | else if (INTEGRAL_TYPE_P (outer_type)) |
e918a58a RAE |
15457 | { |
15458 | if (TREE_CODE (inner_type) == REAL_TYPE) | |
15459 | return tree_expr_nonnegative_warnv_p (op0, | |
15460 | strict_overflow_p); | |
d2a365a8 | 15461 | if (INTEGRAL_TYPE_P (inner_type)) |
e918a58a RAE |
15462 | return TYPE_PRECISION (inner_type) < TYPE_PRECISION (outer_type) |
15463 | && TYPE_UNSIGNED (inner_type); | |
15464 | } | |
15465 | } | |
15466 | break; | |
15467 | ||
15468 | default: | |
15469 | return tree_simple_nonnegative_warnv_p (code, type); | |
15470 | } | |
15471 | ||
15472 | /* We don't know sign of `t', so be conservative and return false. */ | |
15473 | return false; | |
15474 | } | |
325217ed | 15475 | |
e918a58a RAE |
15476 | /* Return true if (CODE OP0 OP1) is known to be non-negative. If the return |
15477 | value is based on the assumption that signed overflow is undefined, | |
15478 | set *STRICT_OVERFLOW_P to true; otherwise, don't change | |
15479 | *STRICT_OVERFLOW_P. */ | |
15480 | ||
2d3cd5d5 | 15481 | bool |
e918a58a RAE |
15482 | tree_binary_nonnegative_warnv_p (enum tree_code code, tree type, tree op0, |
15483 | tree op1, bool *strict_overflow_p) | |
15484 | { | |
15485 | if (TYPE_UNSIGNED (type)) | |
15486 | return true; | |
15487 | ||
15488 | switch (code) | |
15489 | { | |
5be014d5 | 15490 | case POINTER_PLUS_EXPR: |
f7df23be | 15491 | case PLUS_EXPR: |
e918a58a RAE |
15492 | if (FLOAT_TYPE_P (type)) |
15493 | return (tree_expr_nonnegative_warnv_p (op0, | |
6ac01510 | 15494 | strict_overflow_p) |
e918a58a | 15495 | && tree_expr_nonnegative_warnv_p (op1, |
6ac01510 | 15496 | strict_overflow_p)); |
96f26e41 | 15497 | |
e15bb5c6 | 15498 | /* zero_extend(x) + zero_extend(y) is non-negative if x and y are |
e2cca9be | 15499 | both unsigned and at least 2 bits shorter than the result. */ |
e918a58a RAE |
15500 | if (TREE_CODE (type) == INTEGER_TYPE |
15501 | && TREE_CODE (op0) == NOP_EXPR | |
15502 | && TREE_CODE (op1) == NOP_EXPR) | |
96f26e41 | 15503 | { |
e918a58a RAE |
15504 | tree inner1 = TREE_TYPE (TREE_OPERAND (op0, 0)); |
15505 | tree inner2 = TREE_TYPE (TREE_OPERAND (op1, 0)); | |
8df83eae RK |
15506 | if (TREE_CODE (inner1) == INTEGER_TYPE && TYPE_UNSIGNED (inner1) |
15507 | && TREE_CODE (inner2) == INTEGER_TYPE && TYPE_UNSIGNED (inner2)) | |
96f26e41 RS |
15508 | { |
15509 | unsigned int prec = MAX (TYPE_PRECISION (inner1), | |
15510 | TYPE_PRECISION (inner2)) + 1; | |
e918a58a | 15511 | return prec < TYPE_PRECISION (type); |
96f26e41 RS |
15512 | } |
15513 | } | |
15514 | break; | |
f7df23be RS |
15515 | |
15516 | case MULT_EXPR: | |
1fc5eced | 15517 | if (FLOAT_TYPE_P (type) || TYPE_OVERFLOW_UNDEFINED (type)) |
f7df23be | 15518 | { |
1fc5eced MG |
15519 | /* x * x is always non-negative for floating point x |
15520 | or without overflow. */ | |
15521 | if (operand_equal_p (op0, op1, 0) | |
15522 | || (tree_expr_nonnegative_warnv_p (op0, strict_overflow_p) | |
15523 | && tree_expr_nonnegative_warnv_p (op1, strict_overflow_p))) | |
15524 | { | |
15525 | if (TYPE_OVERFLOW_UNDEFINED (type)) | |
15526 | *strict_overflow_p = true; | |
15527 | return true; | |
15528 | } | |
f7df23be | 15529 | } |
96f26e41 | 15530 | |
e15bb5c6 | 15531 | /* zero_extend(x) * zero_extend(y) is non-negative if x and y are |
96f26e41 | 15532 | both unsigned and their total bits is shorter than the result. */ |
e918a58a | 15533 | if (TREE_CODE (type) == INTEGER_TYPE |
cdd6a337 MLI |
15534 | && (TREE_CODE (op0) == NOP_EXPR || TREE_CODE (op0) == INTEGER_CST) |
15535 | && (TREE_CODE (op1) == NOP_EXPR || TREE_CODE (op1) == INTEGER_CST)) | |
96f26e41 | 15536 | { |
b8698a0f | 15537 | tree inner0 = (TREE_CODE (op0) == NOP_EXPR) |
cdd6a337 MLI |
15538 | ? TREE_TYPE (TREE_OPERAND (op0, 0)) |
15539 | : TREE_TYPE (op0); | |
b8698a0f | 15540 | tree inner1 = (TREE_CODE (op1) == NOP_EXPR) |
cdd6a337 MLI |
15541 | ? TREE_TYPE (TREE_OPERAND (op1, 0)) |
15542 | : TREE_TYPE (op1); | |
15543 | ||
15544 | bool unsigned0 = TYPE_UNSIGNED (inner0); | |
15545 | bool unsigned1 = TYPE_UNSIGNED (inner1); | |
15546 | ||
15547 | if (TREE_CODE (op0) == INTEGER_CST) | |
15548 | unsigned0 = unsigned0 || tree_int_cst_sgn (op0) >= 0; | |
15549 | ||
15550 | if (TREE_CODE (op1) == INTEGER_CST) | |
15551 | unsigned1 = unsigned1 || tree_int_cst_sgn (op1) >= 0; | |
15552 | ||
15553 | if (TREE_CODE (inner0) == INTEGER_TYPE && unsigned0 | |
15554 | && TREE_CODE (inner1) == INTEGER_TYPE && unsigned1) | |
15555 | { | |
15556 | unsigned int precision0 = (TREE_CODE (op0) == INTEGER_CST) | |
15557 | ? tree_int_cst_min_precision (op0, /*unsignedp=*/true) | |
15558 | : TYPE_PRECISION (inner0); | |
15559 | ||
15560 | unsigned int precision1 = (TREE_CODE (op1) == INTEGER_CST) | |
15561 | ? tree_int_cst_min_precision (op1, /*unsignedp=*/true) | |
15562 | : TYPE_PRECISION (inner1); | |
15563 | ||
15564 | return precision0 + precision1 < TYPE_PRECISION (type); | |
15565 | } | |
96f26e41 | 15566 | } |
682d0395 | 15567 | return false; |
f7df23be | 15568 | |
196f5a8d VR |
15569 | case BIT_AND_EXPR: |
15570 | case MAX_EXPR: | |
e918a58a | 15571 | return (tree_expr_nonnegative_warnv_p (op0, |
6ac01510 | 15572 | strict_overflow_p) |
e918a58a | 15573 | || tree_expr_nonnegative_warnv_p (op1, |
6ac01510 | 15574 | strict_overflow_p)); |
196f5a8d VR |
15575 | |
15576 | case BIT_IOR_EXPR: | |
15577 | case BIT_XOR_EXPR: | |
15578 | case MIN_EXPR: | |
15579 | case RDIV_EXPR: | |
ada11335 KG |
15580 | case TRUNC_DIV_EXPR: |
15581 | case CEIL_DIV_EXPR: | |
15582 | case FLOOR_DIV_EXPR: | |
15583 | case ROUND_DIV_EXPR: | |
e918a58a | 15584 | return (tree_expr_nonnegative_warnv_p (op0, |
6ac01510 | 15585 | strict_overflow_p) |
e918a58a | 15586 | && tree_expr_nonnegative_warnv_p (op1, |
6ac01510 | 15587 | strict_overflow_p)); |
96f26e41 | 15588 | |
ada11335 KG |
15589 | case TRUNC_MOD_EXPR: |
15590 | case CEIL_MOD_EXPR: | |
15591 | case FLOOR_MOD_EXPR: | |
15592 | case ROUND_MOD_EXPR: | |
e918a58a | 15593 | return tree_expr_nonnegative_warnv_p (op0, |
6ac01510 | 15594 | strict_overflow_p); |
e918a58a RAE |
15595 | default: |
15596 | return tree_simple_nonnegative_warnv_p (code, type); | |
15597 | } | |
96f26e41 | 15598 | |
e918a58a RAE |
15599 | /* We don't know sign of `t', so be conservative and return false. */ |
15600 | return false; | |
15601 | } | |
96f26e41 | 15602 | |
e918a58a RAE |
15603 | /* Return true if T is known to be non-negative. If the return |
15604 | value is based on the assumption that signed overflow is undefined, | |
15605 | set *STRICT_OVERFLOW_P to true; otherwise, don't change | |
15606 | *STRICT_OVERFLOW_P. */ | |
15607 | ||
2d3cd5d5 | 15608 | bool |
e918a58a RAE |
15609 | tree_single_nonnegative_warnv_p (tree t, bool *strict_overflow_p) |
15610 | { | |
15611 | if (TYPE_UNSIGNED (TREE_TYPE (t))) | |
15612 | return true; | |
15613 | ||
07c40d0b | 15614 | switch (TREE_CODE (t)) |
e918a58a | 15615 | { |
e918a58a RAE |
15616 | case INTEGER_CST: |
15617 | return tree_int_cst_sgn (t) >= 0; | |
15618 | ||
15619 | case REAL_CST: | |
15620 | return ! REAL_VALUE_NEGATIVE (TREE_REAL_CST (t)); | |
15621 | ||
15622 | case FIXED_CST: | |
15623 | return ! FIXED_VALUE_NEGATIVE (TREE_FIXED_CST (t)); | |
196f5a8d VR |
15624 | |
15625 | case COND_EXPR: | |
6ac01510 ILT |
15626 | return (tree_expr_nonnegative_warnv_p (TREE_OPERAND (t, 1), |
15627 | strict_overflow_p) | |
15628 | && tree_expr_nonnegative_warnv_p (TREE_OPERAND (t, 2), | |
15629 | strict_overflow_p)); | |
e918a58a RAE |
15630 | default: |
15631 | return tree_simple_nonnegative_warnv_p (TREE_CODE (t), | |
15632 | TREE_TYPE (t)); | |
15633 | } | |
15634 | /* We don't know sign of `t', so be conservative and return false. */ | |
15635 | return false; | |
15636 | } | |
b1500d00 | 15637 | |
a1a6e271 RAE |
15638 | /* Return true if T is known to be non-negative. If the return |
15639 | value is based on the assumption that signed overflow is undefined, | |
15640 | set *STRICT_OVERFLOW_P to true; otherwise, don't change | |
15641 | *STRICT_OVERFLOW_P. */ | |
15642 | ||
15643 | bool | |
726a989a | 15644 | tree_call_nonnegative_warnv_p (tree type, tree fndecl, |
a1a6e271 RAE |
15645 | tree arg0, tree arg1, bool *strict_overflow_p) |
15646 | { | |
15647 | if (fndecl && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL) | |
15648 | switch (DECL_FUNCTION_CODE (fndecl)) | |
15649 | { | |
15650 | CASE_FLT_FN (BUILT_IN_ACOS): | |
15651 | CASE_FLT_FN (BUILT_IN_ACOSH): | |
15652 | CASE_FLT_FN (BUILT_IN_CABS): | |
15653 | CASE_FLT_FN (BUILT_IN_COSH): | |
15654 | CASE_FLT_FN (BUILT_IN_ERFC): | |
15655 | CASE_FLT_FN (BUILT_IN_EXP): | |
15656 | CASE_FLT_FN (BUILT_IN_EXP10): | |
15657 | CASE_FLT_FN (BUILT_IN_EXP2): | |
15658 | CASE_FLT_FN (BUILT_IN_FABS): | |
15659 | CASE_FLT_FN (BUILT_IN_FDIM): | |
15660 | CASE_FLT_FN (BUILT_IN_HYPOT): | |
15661 | CASE_FLT_FN (BUILT_IN_POW10): | |
15662 | CASE_INT_FN (BUILT_IN_FFS): | |
15663 | CASE_INT_FN (BUILT_IN_PARITY): | |
15664 | CASE_INT_FN (BUILT_IN_POPCOUNT): | |
1f6eac90 JJ |
15665 | CASE_INT_FN (BUILT_IN_CLZ): |
15666 | CASE_INT_FN (BUILT_IN_CLRSB): | |
a1a6e271 RAE |
15667 | case BUILT_IN_BSWAP32: |
15668 | case BUILT_IN_BSWAP64: | |
15669 | /* Always true. */ | |
15670 | return true; | |
15671 | ||
15672 | CASE_FLT_FN (BUILT_IN_SQRT): | |
15673 | /* sqrt(-0.0) is -0.0. */ | |
15674 | if (!HONOR_SIGNED_ZEROS (TYPE_MODE (type))) | |
15675 | return true; | |
15676 | return tree_expr_nonnegative_warnv_p (arg0, | |
15677 | strict_overflow_p); | |
15678 | ||
15679 | CASE_FLT_FN (BUILT_IN_ASINH): | |
15680 | CASE_FLT_FN (BUILT_IN_ATAN): | |
15681 | CASE_FLT_FN (BUILT_IN_ATANH): | |
15682 | CASE_FLT_FN (BUILT_IN_CBRT): | |
15683 | CASE_FLT_FN (BUILT_IN_CEIL): | |
15684 | CASE_FLT_FN (BUILT_IN_ERF): | |
15685 | CASE_FLT_FN (BUILT_IN_EXPM1): | |
15686 | CASE_FLT_FN (BUILT_IN_FLOOR): | |
15687 | CASE_FLT_FN (BUILT_IN_FMOD): | |
15688 | CASE_FLT_FN (BUILT_IN_FREXP): | |
6c32ee74 UB |
15689 | CASE_FLT_FN (BUILT_IN_ICEIL): |
15690 | CASE_FLT_FN (BUILT_IN_IFLOOR): | |
15691 | CASE_FLT_FN (BUILT_IN_IRINT): | |
15692 | CASE_FLT_FN (BUILT_IN_IROUND): | |
a1a6e271 RAE |
15693 | CASE_FLT_FN (BUILT_IN_LCEIL): |
15694 | CASE_FLT_FN (BUILT_IN_LDEXP): | |
15695 | CASE_FLT_FN (BUILT_IN_LFLOOR): | |
15696 | CASE_FLT_FN (BUILT_IN_LLCEIL): | |
15697 | CASE_FLT_FN (BUILT_IN_LLFLOOR): | |
15698 | CASE_FLT_FN (BUILT_IN_LLRINT): | |
15699 | CASE_FLT_FN (BUILT_IN_LLROUND): | |
15700 | CASE_FLT_FN (BUILT_IN_LRINT): | |
15701 | CASE_FLT_FN (BUILT_IN_LROUND): | |
15702 | CASE_FLT_FN (BUILT_IN_MODF): | |
15703 | CASE_FLT_FN (BUILT_IN_NEARBYINT): | |
15704 | CASE_FLT_FN (BUILT_IN_RINT): | |
15705 | CASE_FLT_FN (BUILT_IN_ROUND): | |
15706 | CASE_FLT_FN (BUILT_IN_SCALB): | |
15707 | CASE_FLT_FN (BUILT_IN_SCALBLN): | |
15708 | CASE_FLT_FN (BUILT_IN_SCALBN): | |
15709 | CASE_FLT_FN (BUILT_IN_SIGNBIT): | |
15710 | CASE_FLT_FN (BUILT_IN_SIGNIFICAND): | |
15711 | CASE_FLT_FN (BUILT_IN_SINH): | |
15712 | CASE_FLT_FN (BUILT_IN_TANH): | |
15713 | CASE_FLT_FN (BUILT_IN_TRUNC): | |
15714 | /* True if the 1st argument is nonnegative. */ | |
15715 | return tree_expr_nonnegative_warnv_p (arg0, | |
15716 | strict_overflow_p); | |
15717 | ||
15718 | CASE_FLT_FN (BUILT_IN_FMAX): | |
15719 | /* True if the 1st OR 2nd arguments are nonnegative. */ | |
15720 | return (tree_expr_nonnegative_warnv_p (arg0, | |
15721 | strict_overflow_p) | |
15722 | || (tree_expr_nonnegative_warnv_p (arg1, | |
15723 | strict_overflow_p))); | |
15724 | ||
15725 | CASE_FLT_FN (BUILT_IN_FMIN): | |
15726 | /* True if the 1st AND 2nd arguments are nonnegative. */ | |
15727 | return (tree_expr_nonnegative_warnv_p (arg0, | |
15728 | strict_overflow_p) | |
15729 | && (tree_expr_nonnegative_warnv_p (arg1, | |
15730 | strict_overflow_p))); | |
15731 | ||
15732 | CASE_FLT_FN (BUILT_IN_COPYSIGN): | |
15733 | /* True if the 2nd argument is nonnegative. */ | |
15734 | return tree_expr_nonnegative_warnv_p (arg1, | |
15735 | strict_overflow_p); | |
15736 | ||
15737 | CASE_FLT_FN (BUILT_IN_POWI): | |
15738 | /* True if the 1st argument is nonnegative or the second | |
15739 | argument is an even integer. */ | |
d0599470 RAE |
15740 | if (TREE_CODE (arg1) == INTEGER_CST |
15741 | && (TREE_INT_CST_LOW (arg1) & 1) == 0) | |
15742 | return true; | |
a1a6e271 RAE |
15743 | return tree_expr_nonnegative_warnv_p (arg0, |
15744 | strict_overflow_p); | |
15745 | ||
15746 | CASE_FLT_FN (BUILT_IN_POW): | |
15747 | /* True if the 1st argument is nonnegative or the second | |
15748 | argument is an even integer valued real. */ | |
15749 | if (TREE_CODE (arg1) == REAL_CST) | |
15750 | { | |
15751 | REAL_VALUE_TYPE c; | |
15752 | HOST_WIDE_INT n; | |
15753 | ||
15754 | c = TREE_REAL_CST (arg1); | |
15755 | n = real_to_integer (&c); | |
15756 | if ((n & 1) == 0) | |
15757 | { | |
15758 | REAL_VALUE_TYPE cint; | |
15759 | real_from_integer (&cint, VOIDmode, n, | |
15760 | n < 0 ? -1 : 0, 0); | |
15761 | if (real_identical (&c, &cint)) | |
15762 | return true; | |
15763 | } | |
15764 | } | |
15765 | return tree_expr_nonnegative_warnv_p (arg0, | |
15766 | strict_overflow_p); | |
15767 | ||
15768 | default: | |
15769 | break; | |
15770 | } | |
726a989a | 15771 | return tree_simple_nonnegative_warnv_p (CALL_EXPR, |
a1a6e271 RAE |
15772 | type); |
15773 | } | |
15774 | ||
e918a58a RAE |
15775 | /* Return true if T is known to be non-negative. If the return |
15776 | value is based on the assumption that signed overflow is undefined, | |
15777 | set *STRICT_OVERFLOW_P to true; otherwise, don't change | |
15778 | *STRICT_OVERFLOW_P. */ | |
96f26e41 | 15779 | |
2d3cd5d5 | 15780 | bool |
e918a58a RAE |
15781 | tree_invalid_nonnegative_warnv_p (tree t, bool *strict_overflow_p) |
15782 | { | |
07c40d0b | 15783 | enum tree_code code = TREE_CODE (t); |
e918a58a RAE |
15784 | if (TYPE_UNSIGNED (TREE_TYPE (t))) |
15785 | return true; | |
96f26e41 | 15786 | |
e918a58a RAE |
15787 | switch (code) |
15788 | { | |
3a5b9284 RH |
15789 | case TARGET_EXPR: |
15790 | { | |
15791 | tree temp = TARGET_EXPR_SLOT (t); | |
15792 | t = TARGET_EXPR_INITIAL (t); | |
15793 | ||
15794 | /* If the initializer is non-void, then it's a normal expression | |
15795 | that will be assigned to the slot. */ | |
15796 | if (!VOID_TYPE_P (t)) | |
6ac01510 | 15797 | return tree_expr_nonnegative_warnv_p (t, strict_overflow_p); |
3a5b9284 RH |
15798 | |
15799 | /* Otherwise, the initializer sets the slot in some way. One common | |
15800 | way is an assignment statement at the end of the initializer. */ | |
15801 | while (1) | |
15802 | { | |
15803 | if (TREE_CODE (t) == BIND_EXPR) | |
15804 | t = expr_last (BIND_EXPR_BODY (t)); | |
15805 | else if (TREE_CODE (t) == TRY_FINALLY_EXPR | |
15806 | || TREE_CODE (t) == TRY_CATCH_EXPR) | |
15807 | t = expr_last (TREE_OPERAND (t, 0)); | |
15808 | else if (TREE_CODE (t) == STATEMENT_LIST) | |
15809 | t = expr_last (t); | |
15810 | else | |
15811 | break; | |
15812 | } | |
726a989a RB |
15813 | if (TREE_CODE (t) == MODIFY_EXPR |
15814 | && TREE_OPERAND (t, 0) == temp) | |
15815 | return tree_expr_nonnegative_warnv_p (TREE_OPERAND (t, 1), | |
6ac01510 | 15816 | strict_overflow_p); |
3a5b9284 | 15817 | |
682d0395 | 15818 | return false; |
3a5b9284 RH |
15819 | } |
15820 | ||
07bae5ad | 15821 | case CALL_EXPR: |
2f503025 | 15822 | { |
a1a6e271 RAE |
15823 | tree arg0 = call_expr_nargs (t) > 0 ? CALL_EXPR_ARG (t, 0) : NULL_TREE; |
15824 | tree arg1 = call_expr_nargs (t) > 1 ? CALL_EXPR_ARG (t, 1) : NULL_TREE; | |
15825 | ||
726a989a | 15826 | return tree_call_nonnegative_warnv_p (TREE_TYPE (t), |
a1a6e271 RAE |
15827 | get_callee_fndecl (t), |
15828 | arg0, | |
15829 | arg1, | |
15830 | strict_overflow_p); | |
2f503025 | 15831 | } |
e918a58a RAE |
15832 | case COMPOUND_EXPR: |
15833 | case MODIFY_EXPR: | |
726a989a | 15834 | return tree_expr_nonnegative_warnv_p (TREE_OPERAND (t, 1), |
e918a58a RAE |
15835 | strict_overflow_p); |
15836 | case BIND_EXPR: | |
15837 | return tree_expr_nonnegative_warnv_p (expr_last (TREE_OPERAND (t, 1)), | |
15838 | strict_overflow_p); | |
15839 | case SAVE_EXPR: | |
15840 | return tree_expr_nonnegative_warnv_p (TREE_OPERAND (t, 0), | |
15841 | strict_overflow_p); | |
07bae5ad | 15842 | |
a36556a8 | 15843 | default: |
e918a58a RAE |
15844 | return tree_simple_nonnegative_warnv_p (TREE_CODE (t), |
15845 | TREE_TYPE (t)); | |
a36556a8 | 15846 | } |
96f26e41 RS |
15847 | |
15848 | /* We don't know sign of `t', so be conservative and return false. */ | |
682d0395 | 15849 | return false; |
a36556a8 ZW |
15850 | } |
15851 | ||
e918a58a RAE |
15852 | /* Return true if T is known to be non-negative. If the return |
15853 | value is based on the assumption that signed overflow is undefined, | |
15854 | set *STRICT_OVERFLOW_P to true; otherwise, don't change | |
15855 | *STRICT_OVERFLOW_P. */ | |
15856 | ||
15857 | bool | |
15858 | tree_expr_nonnegative_warnv_p (tree t, bool *strict_overflow_p) | |
15859 | { | |
15860 | enum tree_code code; | |
15861 | if (t == error_mark_node) | |
15862 | return false; | |
15863 | ||
15864 | code = TREE_CODE (t); | |
15865 | switch (TREE_CODE_CLASS (code)) | |
15866 | { | |
15867 | case tcc_binary: | |
15868 | case tcc_comparison: | |
15869 | return tree_binary_nonnegative_warnv_p (TREE_CODE (t), | |
15870 | TREE_TYPE (t), | |
15871 | TREE_OPERAND (t, 0), | |
15872 | TREE_OPERAND (t, 1), | |
15873 | strict_overflow_p); | |
15874 | ||
15875 | case tcc_unary: | |
15876 | return tree_unary_nonnegative_warnv_p (TREE_CODE (t), | |
15877 | TREE_TYPE (t), | |
15878 | TREE_OPERAND (t, 0), | |
15879 | strict_overflow_p); | |
15880 | ||
15881 | case tcc_constant: | |
15882 | case tcc_declaration: | |
15883 | case tcc_reference: | |
15884 | return tree_single_nonnegative_warnv_p (t, strict_overflow_p); | |
15885 | ||
15886 | default: | |
15887 | break; | |
15888 | } | |
15889 | ||
15890 | switch (code) | |
15891 | { | |
15892 | case TRUTH_AND_EXPR: | |
15893 | case TRUTH_OR_EXPR: | |
15894 | case TRUTH_XOR_EXPR: | |
15895 | return tree_binary_nonnegative_warnv_p (TREE_CODE (t), | |
15896 | TREE_TYPE (t), | |
15897 | TREE_OPERAND (t, 0), | |
15898 | TREE_OPERAND (t, 1), | |
15899 | strict_overflow_p); | |
15900 | case TRUTH_NOT_EXPR: | |
15901 | return tree_unary_nonnegative_warnv_p (TREE_CODE (t), | |
15902 | TREE_TYPE (t), | |
15903 | TREE_OPERAND (t, 0), | |
15904 | strict_overflow_p); | |
15905 | ||
15906 | case COND_EXPR: | |
15907 | case CONSTRUCTOR: | |
15908 | case OBJ_TYPE_REF: | |
15909 | case ASSERT_EXPR: | |
15910 | case ADDR_EXPR: | |
15911 | case WITH_SIZE_EXPR: | |
e918a58a | 15912 | case SSA_NAME: |
e918a58a RAE |
15913 | return tree_single_nonnegative_warnv_p (t, strict_overflow_p); |
15914 | ||
15915 | default: | |
15916 | return tree_invalid_nonnegative_warnv_p (t, strict_overflow_p); | |
15917 | } | |
15918 | } | |
15919 | ||
6ac01510 ILT |
15920 | /* Return true if `t' is known to be non-negative. Handle warnings |
15921 | about undefined signed overflow. */ | |
15922 | ||
15923 | bool | |
15924 | tree_expr_nonnegative_p (tree t) | |
15925 | { | |
15926 | bool ret, strict_overflow_p; | |
15927 | ||
15928 | strict_overflow_p = false; | |
15929 | ret = tree_expr_nonnegative_warnv_p (t, &strict_overflow_p); | |
15930 | if (strict_overflow_p) | |
15931 | fold_overflow_warning (("assuming signed overflow does not occur when " | |
15932 | "determining that expression is always " | |
15933 | "non-negative"), | |
15934 | WARN_STRICT_OVERFLOW_MISC); | |
15935 | return ret; | |
15936 | } | |
15937 | ||
74dd418c RAE |
15938 | |
15939 | /* Return true when (CODE OP0) is an address and is known to be nonzero. | |
8e7b3a43 | 15940 | For floating point we further ensure that T is not denormal. |
6ac01510 ILT |
15941 | Similar logic is present in nonzero_address in rtlanal.h. |
15942 | ||
15943 | If the return value is based on the assumption that signed overflow | |
15944 | is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't | |
15945 | change *STRICT_OVERFLOW_P. */ | |
8e7b3a43 | 15946 | |
2d3cd5d5 | 15947 | bool |
74dd418c RAE |
15948 | tree_unary_nonzero_warnv_p (enum tree_code code, tree type, tree op0, |
15949 | bool *strict_overflow_p) | |
8e7b3a43 | 15950 | { |
74dd418c RAE |
15951 | switch (code) |
15952 | { | |
15953 | case ABS_EXPR: | |
15954 | return tree_expr_nonzero_warnv_p (op0, | |
15955 | strict_overflow_p); | |
8e7b3a43 | 15956 | |
74dd418c RAE |
15957 | case NOP_EXPR: |
15958 | { | |
15959 | tree inner_type = TREE_TYPE (op0); | |
15960 | tree outer_type = type; | |
8e7b3a43 | 15961 | |
74dd418c RAE |
15962 | return (TYPE_PRECISION (outer_type) >= TYPE_PRECISION (inner_type) |
15963 | && tree_expr_nonzero_warnv_p (op0, | |
15964 | strict_overflow_p)); | |
15965 | } | |
15966 | break; | |
b16caf72 | 15967 | |
74dd418c RAE |
15968 | case NON_LVALUE_EXPR: |
15969 | return tree_expr_nonzero_warnv_p (op0, | |
6ac01510 | 15970 | strict_overflow_p); |
8e7b3a43 | 15971 | |
74dd418c RAE |
15972 | default: |
15973 | break; | |
15974 | } | |
15975 | ||
15976 | return false; | |
15977 | } | |
15978 | ||
15979 | /* Return true when (CODE OP0 OP1) is an address and is known to be nonzero. | |
15980 | For floating point we further ensure that T is not denormal. | |
15981 | Similar logic is present in nonzero_address in rtlanal.h. | |
15982 | ||
15983 | If the return value is based on the assumption that signed overflow | |
15984 | is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't | |
15985 | change *STRICT_OVERFLOW_P. */ | |
8e7b3a43 | 15986 | |
2d3cd5d5 | 15987 | bool |
74dd418c RAE |
15988 | tree_binary_nonzero_warnv_p (enum tree_code code, |
15989 | tree type, | |
15990 | tree op0, | |
15991 | tree op1, bool *strict_overflow_p) | |
15992 | { | |
15993 | bool sub_strict_overflow_p; | |
15994 | switch (code) | |
15995 | { | |
5be014d5 | 15996 | case POINTER_PLUS_EXPR: |
8e7b3a43 | 15997 | case PLUS_EXPR: |
eeef0e45 | 15998 | if (TYPE_OVERFLOW_UNDEFINED (type)) |
8e7b3a43 KH |
15999 | { |
16000 | /* With the presence of negative values it is hard | |
16001 | to say something. */ | |
6ac01510 | 16002 | sub_strict_overflow_p = false; |
74dd418c | 16003 | if (!tree_expr_nonnegative_warnv_p (op0, |
6ac01510 | 16004 | &sub_strict_overflow_p) |
74dd418c | 16005 | || !tree_expr_nonnegative_warnv_p (op1, |
6ac01510 | 16006 | &sub_strict_overflow_p)) |
8e7b3a43 KH |
16007 | return false; |
16008 | /* One of operands must be positive and the other non-negative. */ | |
6ac01510 ILT |
16009 | /* We don't set *STRICT_OVERFLOW_P here: even if this value |
16010 | overflows, on a twos-complement machine the sum of two | |
16011 | nonnegative numbers can never be zero. */ | |
74dd418c | 16012 | return (tree_expr_nonzero_warnv_p (op0, |
6ac01510 | 16013 | strict_overflow_p) |
74dd418c | 16014 | || tree_expr_nonzero_warnv_p (op1, |
6ac01510 | 16015 | strict_overflow_p)); |
8e7b3a43 KH |
16016 | } |
16017 | break; | |
16018 | ||
16019 | case MULT_EXPR: | |
eeef0e45 | 16020 | if (TYPE_OVERFLOW_UNDEFINED (type)) |
8e7b3a43 | 16021 | { |
74dd418c | 16022 | if (tree_expr_nonzero_warnv_p (op0, |
6ac01510 | 16023 | strict_overflow_p) |
74dd418c | 16024 | && tree_expr_nonzero_warnv_p (op1, |
6ac01510 ILT |
16025 | strict_overflow_p)) |
16026 | { | |
16027 | *strict_overflow_p = true; | |
16028 | return true; | |
16029 | } | |
8e7b3a43 KH |
16030 | } |
16031 | break; | |
16032 | ||
74dd418c RAE |
16033 | case MIN_EXPR: |
16034 | sub_strict_overflow_p = false; | |
16035 | if (tree_expr_nonzero_warnv_p (op0, | |
16036 | &sub_strict_overflow_p) | |
16037 | && tree_expr_nonzero_warnv_p (op1, | |
16038 | &sub_strict_overflow_p)) | |
16039 | { | |
16040 | if (sub_strict_overflow_p) | |
16041 | *strict_overflow_p = true; | |
16042 | } | |
16043 | break; | |
8e7b3a43 | 16044 | |
74dd418c RAE |
16045 | case MAX_EXPR: |
16046 | sub_strict_overflow_p = false; | |
16047 | if (tree_expr_nonzero_warnv_p (op0, | |
16048 | &sub_strict_overflow_p)) | |
16049 | { | |
16050 | if (sub_strict_overflow_p) | |
16051 | *strict_overflow_p = true; | |
16052 | ||
16053 | /* When both operands are nonzero, then MAX must be too. */ | |
16054 | if (tree_expr_nonzero_warnv_p (op1, | |
16055 | strict_overflow_p)) | |
16056 | return true; | |
16057 | ||
16058 | /* MAX where operand 0 is positive is positive. */ | |
16059 | return tree_expr_nonnegative_warnv_p (op0, | |
16060 | strict_overflow_p); | |
16061 | } | |
16062 | /* MAX where operand 1 is positive is positive. */ | |
16063 | else if (tree_expr_nonzero_warnv_p (op1, | |
16064 | &sub_strict_overflow_p) | |
16065 | && tree_expr_nonnegative_warnv_p (op1, | |
16066 | &sub_strict_overflow_p)) | |
16067 | { | |
16068 | if (sub_strict_overflow_p) | |
16069 | *strict_overflow_p = true; | |
16070 | return true; | |
16071 | } | |
16072 | break; | |
16073 | ||
16074 | case BIT_IOR_EXPR: | |
16075 | return (tree_expr_nonzero_warnv_p (op1, | |
16076 | strict_overflow_p) | |
16077 | || tree_expr_nonzero_warnv_p (op0, | |
16078 | strict_overflow_p)); | |
16079 | ||
16080 | default: | |
8e7b3a43 | 16081 | break; |
74dd418c | 16082 | } |
8e7b3a43 | 16083 | |
74dd418c RAE |
16084 | return false; |
16085 | } | |
16086 | ||
16087 | /* Return true when T is an address and is known to be nonzero. | |
16088 | For floating point we further ensure that T is not denormal. | |
16089 | Similar logic is present in nonzero_address in rtlanal.h. | |
16090 | ||
16091 | If the return value is based on the assumption that signed overflow | |
16092 | is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't | |
16093 | change *STRICT_OVERFLOW_P. */ | |
16094 | ||
2d3cd5d5 | 16095 | bool |
74dd418c RAE |
16096 | tree_single_nonzero_warnv_p (tree t, bool *strict_overflow_p) |
16097 | { | |
16098 | bool sub_strict_overflow_p; | |
16099 | switch (TREE_CODE (t)) | |
16100 | { | |
74dd418c RAE |
16101 | case INTEGER_CST: |
16102 | return !integer_zerop (t); | |
16103 | ||
16104 | case ADDR_EXPR: | |
88f19756 | 16105 | { |
3d7a712a RG |
16106 | tree base = TREE_OPERAND (t, 0); |
16107 | if (!DECL_P (base)) | |
16108 | base = get_base_address (base); | |
88f19756 RH |
16109 | |
16110 | if (!base) | |
16111 | return false; | |
16112 | ||
4d35e75c PB |
16113 | /* Weak declarations may link to NULL. Other things may also be NULL |
16114 | so protect with -fdelete-null-pointer-checks; but not variables | |
16115 | allocated on the stack. */ | |
16116 | if (DECL_P (base) | |
16117 | && (flag_delete_null_pointer_checks | |
3d7a712a RG |
16118 | || (DECL_CONTEXT (base) |
16119 | && TREE_CODE (DECL_CONTEXT (base)) == FUNCTION_DECL | |
16120 | && auto_var_in_fn_p (base, DECL_CONTEXT (base))))) | |
b45f0e58 | 16121 | return !VAR_OR_FUNCTION_DECL_P (base) || !DECL_WEAK (base); |
88f19756 RH |
16122 | |
16123 | /* Constants are never weak. */ | |
6615c446 | 16124 | if (CONSTANT_CLASS_P (base)) |
88f19756 RH |
16125 | return true; |
16126 | ||
16127 | return false; | |
16128 | } | |
8e7b3a43 KH |
16129 | |
16130 | case COND_EXPR: | |
6ac01510 ILT |
16131 | sub_strict_overflow_p = false; |
16132 | if (tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 1), | |
16133 | &sub_strict_overflow_p) | |
16134 | && tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 2), | |
16135 | &sub_strict_overflow_p)) | |
16136 | { | |
16137 | if (sub_strict_overflow_p) | |
16138 | *strict_overflow_p = true; | |
16139 | return true; | |
16140 | } | |
16141 | break; | |
8e7b3a43 | 16142 | |
74dd418c | 16143 | default: |
6ac01510 | 16144 | break; |
74dd418c RAE |
16145 | } |
16146 | return false; | |
16147 | } | |
8e7b3a43 | 16148 | |
74dd418c RAE |
16149 | /* Return true when T is an address and is known to be nonzero. |
16150 | For floating point we further ensure that T is not denormal. | |
16151 | Similar logic is present in nonzero_address in rtlanal.h. | |
6ac01510 | 16152 | |
74dd418c RAE |
16153 | If the return value is based on the assumption that signed overflow |
16154 | is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't | |
16155 | change *STRICT_OVERFLOW_P. */ | |
8e7b3a43 | 16156 | |
74dd418c RAE |
16157 | bool |
16158 | tree_expr_nonzero_warnv_p (tree t, bool *strict_overflow_p) | |
16159 | { | |
16160 | tree type = TREE_TYPE (t); | |
16161 | enum tree_code code; | |
16162 | ||
16163 | /* Doing something useful for floating point would need more work. */ | |
16164 | if (!INTEGRAL_TYPE_P (type) && !POINTER_TYPE_P (type)) | |
16165 | return false; | |
16166 | ||
16167 | code = TREE_CODE (t); | |
16168 | switch (TREE_CODE_CLASS (code)) | |
16169 | { | |
16170 | case tcc_unary: | |
16171 | return tree_unary_nonzero_warnv_p (code, type, TREE_OPERAND (t, 0), | |
16172 | strict_overflow_p); | |
16173 | case tcc_binary: | |
16174 | case tcc_comparison: | |
16175 | return tree_binary_nonzero_warnv_p (code, type, | |
16176 | TREE_OPERAND (t, 0), | |
16177 | TREE_OPERAND (t, 1), | |
6ac01510 | 16178 | strict_overflow_p); |
74dd418c RAE |
16179 | case tcc_constant: |
16180 | case tcc_declaration: | |
16181 | case tcc_reference: | |
16182 | return tree_single_nonzero_warnv_p (t, strict_overflow_p); | |
16183 | ||
16184 | default: | |
8e7b3a43 | 16185 | break; |
74dd418c RAE |
16186 | } |
16187 | ||
16188 | switch (code) | |
16189 | { | |
16190 | case TRUTH_NOT_EXPR: | |
16191 | return tree_unary_nonzero_warnv_p (code, type, TREE_OPERAND (t, 0), | |
16192 | strict_overflow_p); | |
16193 | ||
16194 | case TRUTH_AND_EXPR: | |
16195 | case TRUTH_OR_EXPR: | |
16196 | case TRUTH_XOR_EXPR: | |
16197 | return tree_binary_nonzero_warnv_p (code, type, | |
16198 | TREE_OPERAND (t, 0), | |
16199 | TREE_OPERAND (t, 1), | |
16200 | strict_overflow_p); | |
16201 | ||
16202 | case COND_EXPR: | |
16203 | case CONSTRUCTOR: | |
16204 | case OBJ_TYPE_REF: | |
16205 | case ASSERT_EXPR: | |
16206 | case ADDR_EXPR: | |
16207 | case WITH_SIZE_EXPR: | |
74dd418c | 16208 | case SSA_NAME: |
74dd418c | 16209 | return tree_single_nonzero_warnv_p (t, strict_overflow_p); |
8e7b3a43 KH |
16210 | |
16211 | case COMPOUND_EXPR: | |
16212 | case MODIFY_EXPR: | |
16213 | case BIND_EXPR: | |
726a989a | 16214 | return tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 1), |
6ac01510 | 16215 | strict_overflow_p); |
8e7b3a43 KH |
16216 | |
16217 | case SAVE_EXPR: | |
6ac01510 ILT |
16218 | return tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 0), |
16219 | strict_overflow_p); | |
8e7b3a43 | 16220 | |
4db8040c | 16221 | case CALL_EXPR: |
2284b034 MG |
16222 | { |
16223 | tree fndecl = get_callee_fndecl (t); | |
16224 | if (!fndecl) return false; | |
16225 | if (flag_delete_null_pointer_checks && !flag_check_new | |
16226 | && DECL_IS_OPERATOR_NEW (fndecl) | |
16227 | && !TREE_NOTHROW (fndecl)) | |
16228 | return true; | |
826cacfe MG |
16229 | if (flag_delete_null_pointer_checks |
16230 | && lookup_attribute ("returns_nonnull", | |
16231 | TYPE_ATTRIBUTES (TREE_TYPE (fndecl)))) | |
16232 | return true; | |
2284b034 MG |
16233 | return alloca_call_p (t); |
16234 | } | |
4db8040c | 16235 | |
8e7b3a43 KH |
16236 | default: |
16237 | break; | |
16238 | } | |
16239 | return false; | |
16240 | } | |
16241 | ||
6ac01510 ILT |
16242 | /* Return true when T is an address and is known to be nonzero. |
16243 | Handle warnings about undefined signed overflow. */ | |
16244 | ||
16245 | bool | |
16246 | tree_expr_nonzero_p (tree t) | |
16247 | { | |
16248 | bool ret, strict_overflow_p; | |
16249 | ||
16250 | strict_overflow_p = false; | |
16251 | ret = tree_expr_nonzero_warnv_p (t, &strict_overflow_p); | |
16252 | if (strict_overflow_p) | |
16253 | fold_overflow_warning (("assuming signed overflow does not occur when " | |
16254 | "determining that expression is always " | |
16255 | "non-zero"), | |
16256 | WARN_STRICT_OVERFLOW_MISC); | |
16257 | return ret; | |
16258 | } | |
16259 | ||
6de9cd9a DN |
16260 | /* Given the components of a binary expression CODE, TYPE, OP0 and OP1, |
16261 | attempt to fold the expression to a constant without modifying TYPE, | |
16262 | OP0 or OP1. | |
16263 | ||
16264 | If the expression could be simplified to a constant, then return | |
16265 | the constant. If the expression would not be simplified to a | |
41704a38 | 16266 | constant, then return NULL_TREE. */ |
6de9cd9a DN |
16267 | |
16268 | tree | |
b52d5eaa | 16269 | fold_binary_to_constant (enum tree_code code, tree type, tree op0, tree op1) |
6de9cd9a | 16270 | { |
054632e8 RS |
16271 | tree tem = fold_binary (code, type, op0, op1); |
16272 | return (tem && TREE_CONSTANT (tem)) ? tem : NULL_TREE; | |
6de9cd9a DN |
16273 | } |
16274 | ||
16275 | /* Given the components of a unary expression CODE, TYPE and OP0, | |
16276 | attempt to fold the expression to a constant without modifying | |
d1822754 | 16277 | TYPE or OP0. |
6de9cd9a DN |
16278 | |
16279 | If the expression could be simplified to a constant, then return | |
16280 | the constant. If the expression would not be simplified to a | |
41704a38 | 16281 | constant, then return NULL_TREE. */ |
6de9cd9a DN |
16282 | |
16283 | tree | |
b52d5eaa | 16284 | fold_unary_to_constant (enum tree_code code, tree type, tree op0) |
6de9cd9a | 16285 | { |
054632e8 RS |
16286 | tree tem = fold_unary (code, type, op0); |
16287 | return (tem && TREE_CONSTANT (tem)) ? tem : NULL_TREE; | |
6de9cd9a DN |
16288 | } |
16289 | ||
16290 | /* If EXP represents referencing an element in a constant string | |
16291 | (either via pointer arithmetic or array indexing), return the | |
16292 | tree representing the value accessed, otherwise return NULL. */ | |
16293 | ||
16294 | tree | |
16295 | fold_read_from_constant_string (tree exp) | |
16296 | { | |
8e3dc7a3 RG |
16297 | if ((TREE_CODE (exp) == INDIRECT_REF |
16298 | || TREE_CODE (exp) == ARRAY_REF) | |
16299 | && TREE_CODE (TREE_TYPE (exp)) == INTEGER_TYPE) | |
6de9cd9a DN |
16300 | { |
16301 | tree exp1 = TREE_OPERAND (exp, 0); | |
16302 | tree index; | |
16303 | tree string; | |
db3927fb | 16304 | location_t loc = EXPR_LOCATION (exp); |
6de9cd9a DN |
16305 | |
16306 | if (TREE_CODE (exp) == INDIRECT_REF) | |
44de5aeb | 16307 | string = string_constant (exp1, &index); |
6de9cd9a DN |
16308 | else |
16309 | { | |
44de5aeb | 16310 | tree low_bound = array_ref_low_bound (exp); |
db3927fb | 16311 | index = fold_convert_loc (loc, sizetype, TREE_OPERAND (exp, 1)); |
d1822754 | 16312 | |
6de9cd9a DN |
16313 | /* Optimize the special-case of a zero lower bound. |
16314 | ||
16315 | We convert the low_bound to sizetype to avoid some problems | |
16316 | with constant folding. (E.g. suppose the lower bound is 1, | |
16317 | and its mode is QI. Without the conversion,l (ARRAY | |
16318 | +(INDEX-(unsigned char)1)) becomes ((ARRAY+(-(unsigned char)1)) | |
fa10beec | 16319 | +INDEX), which becomes (ARRAY+255+INDEX). Oops!) */ |
6de9cd9a | 16320 | if (! integer_zerop (low_bound)) |
db3927fb AH |
16321 | index = size_diffop_loc (loc, index, |
16322 | fold_convert_loc (loc, sizetype, low_bound)); | |
6de9cd9a DN |
16323 | |
16324 | string = exp1; | |
16325 | } | |
16326 | ||
16327 | if (string | |
f9c3744b | 16328 | && TYPE_MODE (TREE_TYPE (exp)) == TYPE_MODE (TREE_TYPE (TREE_TYPE (string))) |
6de9cd9a DN |
16329 | && TREE_CODE (string) == STRING_CST |
16330 | && TREE_CODE (index) == INTEGER_CST | |
16331 | && compare_tree_int (index, TREE_STRING_LENGTH (string)) < 0 | |
16332 | && (GET_MODE_CLASS (TYPE_MODE (TREE_TYPE (TREE_TYPE (string)))) | |
16333 | == MODE_INT) | |
16334 | && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (TREE_TYPE (string)))) == 1)) | |
0c4d4efb DJ |
16335 | return build_int_cst_type (TREE_TYPE (exp), |
16336 | (TREE_STRING_POINTER (string) | |
16337 | [TREE_INT_CST_LOW (index)])); | |
6de9cd9a DN |
16338 | } |
16339 | return NULL; | |
16340 | } | |
16341 | ||
33d13fac | 16342 | /* Return the tree for neg (ARG0) when ARG0 is known to be either |
325217ed | 16343 | an integer constant, real, or fixed-point constant. |
33d13fac KH |
16344 | |
16345 | TYPE is the type of the result. */ | |
16346 | ||
16347 | static tree | |
16348 | fold_negate_const (tree arg0, tree type) | |
16349 | { | |
16350 | tree t = NULL_TREE; | |
16351 | ||
0bccc606 | 16352 | switch (TREE_CODE (arg0)) |
33d13fac | 16353 | { |
0bccc606 NS |
16354 | case INTEGER_CST: |
16355 | { | |
9589f23e | 16356 | double_int val = tree_to_double_int (arg0); |
9be0ac8c LC |
16357 | bool overflow; |
16358 | val = val.neg_with_overflow (&overflow); | |
9589f23e | 16359 | t = force_fit_type_double (type, val, 1, |
b8fca551 | 16360 | (overflow | TREE_OVERFLOW (arg0)) |
d95787e6 | 16361 | && !TYPE_UNSIGNED (type)); |
0bccc606 NS |
16362 | break; |
16363 | } | |
3e6688a7 | 16364 | |
0bccc606 | 16365 | case REAL_CST: |
d49b6e1e | 16366 | t = build_real (type, real_value_negate (&TREE_REAL_CST (arg0))); |
0bccc606 | 16367 | break; |
d1822754 | 16368 | |
325217ed CF |
16369 | case FIXED_CST: |
16370 | { | |
16371 | FIXED_VALUE_TYPE f; | |
16372 | bool overflow_p = fixed_arithmetic (&f, NEGATE_EXPR, | |
16373 | &(TREE_FIXED_CST (arg0)), NULL, | |
16374 | TYPE_SATURATING (type)); | |
16375 | t = build_fixed (type, f); | |
16376 | /* Propagate overflow flags. */ | |
16377 | if (overflow_p | TREE_OVERFLOW (arg0)) | |
28ddeea1 | 16378 | TREE_OVERFLOW (t) = 1; |
325217ed CF |
16379 | break; |
16380 | } | |
16381 | ||
0bccc606 NS |
16382 | default: |
16383 | gcc_unreachable (); | |
16384 | } | |
3e6688a7 | 16385 | |
33d13fac KH |
16386 | return t; |
16387 | } | |
16388 | ||
73c4ab99 KH |
16389 | /* Return the tree for abs (ARG0) when ARG0 is known to be either |
16390 | an integer constant or real constant. | |
16391 | ||
16392 | TYPE is the type of the result. */ | |
16393 | ||
9655d83b | 16394 | tree |
73c4ab99 KH |
16395 | fold_abs_const (tree arg0, tree type) |
16396 | { | |
16397 | tree t = NULL_TREE; | |
16398 | ||
0bccc606 | 16399 | switch (TREE_CODE (arg0)) |
73c4ab99 | 16400 | { |
0bccc606 | 16401 | case INTEGER_CST: |
9589f23e AS |
16402 | { |
16403 | double_int val = tree_to_double_int (arg0); | |
16404 | ||
16405 | /* If the value is unsigned or non-negative, then the absolute value | |
16406 | is the same as the ordinary value. */ | |
16407 | if (TYPE_UNSIGNED (type) | |
27bcd47c | 16408 | || !val.is_negative ()) |
9589f23e AS |
16409 | t = arg0; |
16410 | ||
16411 | /* If the value is negative, then the absolute value is | |
16412 | its negation. */ | |
16413 | else | |
16414 | { | |
9be0ac8c LC |
16415 | bool overflow; |
16416 | val = val.neg_with_overflow (&overflow); | |
9589f23e AS |
16417 | t = force_fit_type_double (type, val, -1, |
16418 | overflow | TREE_OVERFLOW (arg0)); | |
16419 | } | |
16420 | } | |
0bccc606 | 16421 | break; |
3e6688a7 | 16422 | |
0bccc606 | 16423 | case REAL_CST: |
73c4ab99 | 16424 | if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg0))) |
d49b6e1e | 16425 | t = build_real (type, real_value_negate (&TREE_REAL_CST (arg0))); |
73c4ab99 | 16426 | else |
0bccc606 NS |
16427 | t = arg0; |
16428 | break; | |
3e6688a7 | 16429 | |
0bccc606 NS |
16430 | default: |
16431 | gcc_unreachable (); | |
73c4ab99 | 16432 | } |
3e6688a7 | 16433 | |
73c4ab99 KH |
16434 | return t; |
16435 | } | |
16436 | ||
a653e758 RS |
16437 | /* Return the tree for not (ARG0) when ARG0 is known to be an integer |
16438 | constant. TYPE is the type of the result. */ | |
16439 | ||
16440 | static tree | |
9589f23e | 16441 | fold_not_const (const_tree arg0, tree type) |
a653e758 | 16442 | { |
9589f23e | 16443 | double_int val; |
a653e758 | 16444 | |
0bccc606 | 16445 | gcc_assert (TREE_CODE (arg0) == INTEGER_CST); |
3e6688a7 | 16446 | |
27bcd47c | 16447 | val = ~tree_to_double_int (arg0); |
9589f23e | 16448 | return force_fit_type_double (type, val, 0, TREE_OVERFLOW (arg0)); |
a653e758 RS |
16449 | } |
16450 | ||
8e7b3a43 KH |
16451 | /* Given CODE, a relational operator, the target type, TYPE and two |
16452 | constant operands OP0 and OP1, return the result of the | |
16453 | relational operation. If the result is not a compile time | |
16454 | constant, then return NULL_TREE. */ | |
16455 | ||
16456 | static tree | |
16457 | fold_relational_const (enum tree_code code, tree type, tree op0, tree op1) | |
16458 | { | |
1382f0f0 | 16459 | int result, invert; |
8e7b3a43 KH |
16460 | |
16461 | /* From here on, the only cases we handle are when the result is | |
ee8db92b RS |
16462 | known to be a constant. */ |
16463 | ||
16464 | if (TREE_CODE (op0) == REAL_CST && TREE_CODE (op1) == REAL_CST) | |
16465 | { | |
adb8e07e RS |
16466 | const REAL_VALUE_TYPE *c0 = TREE_REAL_CST_PTR (op0); |
16467 | const REAL_VALUE_TYPE *c1 = TREE_REAL_CST_PTR (op1); | |
16468 | ||
ee8db92b | 16469 | /* Handle the cases where either operand is a NaN. */ |
adb8e07e | 16470 | if (real_isnan (c0) || real_isnan (c1)) |
ee8db92b RS |
16471 | { |
16472 | switch (code) | |
16473 | { | |
16474 | case EQ_EXPR: | |
16475 | case ORDERED_EXPR: | |
16476 | result = 0; | |
16477 | break; | |
16478 | ||
16479 | case NE_EXPR: | |
16480 | case UNORDERED_EXPR: | |
16481 | case UNLT_EXPR: | |
16482 | case UNLE_EXPR: | |
16483 | case UNGT_EXPR: | |
16484 | case UNGE_EXPR: | |
16485 | case UNEQ_EXPR: | |
16486 | result = 1; | |
16487 | break; | |
16488 | ||
16489 | case LT_EXPR: | |
16490 | case LE_EXPR: | |
16491 | case GT_EXPR: | |
16492 | case GE_EXPR: | |
16493 | case LTGT_EXPR: | |
16494 | if (flag_trapping_math) | |
16495 | return NULL_TREE; | |
16496 | result = 0; | |
16497 | break; | |
16498 | ||
16499 | default: | |
0bccc606 | 16500 | gcc_unreachable (); |
ee8db92b RS |
16501 | } |
16502 | ||
16503 | return constant_boolean_node (result, type); | |
16504 | } | |
16505 | ||
adb8e07e | 16506 | return constant_boolean_node (real_compare (code, c0, c1), type); |
ee8db92b RS |
16507 | } |
16508 | ||
325217ed CF |
16509 | if (TREE_CODE (op0) == FIXED_CST && TREE_CODE (op1) == FIXED_CST) |
16510 | { | |
16511 | const FIXED_VALUE_TYPE *c0 = TREE_FIXED_CST_PTR (op0); | |
16512 | const FIXED_VALUE_TYPE *c1 = TREE_FIXED_CST_PTR (op1); | |
16513 | return constant_boolean_node (fixed_compare (code, c0, c1), type); | |
16514 | } | |
16515 | ||
23b9463b RS |
16516 | /* Handle equality/inequality of complex constants. */ |
16517 | if (TREE_CODE (op0) == COMPLEX_CST && TREE_CODE (op1) == COMPLEX_CST) | |
16518 | { | |
16519 | tree rcond = fold_relational_const (code, type, | |
16520 | TREE_REALPART (op0), | |
16521 | TREE_REALPART (op1)); | |
16522 | tree icond = fold_relational_const (code, type, | |
16523 | TREE_IMAGPART (op0), | |
16524 | TREE_IMAGPART (op1)); | |
16525 | if (code == EQ_EXPR) | |
16526 | return fold_build2 (TRUTH_ANDIF_EXPR, type, rcond, icond); | |
16527 | else if (code == NE_EXPR) | |
16528 | return fold_build2 (TRUTH_ORIF_EXPR, type, rcond, icond); | |
16529 | else | |
16530 | return NULL_TREE; | |
16531 | } | |
16532 | ||
a8dcc458 MG |
16533 | if (TREE_CODE (op0) == VECTOR_CST && TREE_CODE (op1) == VECTOR_CST) |
16534 | { | |
16535 | unsigned count = VECTOR_CST_NELTS (op0); | |
16536 | tree *elts = XALLOCAVEC (tree, count); | |
16537 | gcc_assert (VECTOR_CST_NELTS (op1) == count | |
16538 | && TYPE_VECTOR_SUBPARTS (type) == count); | |
16539 | ||
16540 | for (unsigned i = 0; i < count; i++) | |
16541 | { | |
16542 | tree elem_type = TREE_TYPE (type); | |
16543 | tree elem0 = VECTOR_CST_ELT (op0, i); | |
16544 | tree elem1 = VECTOR_CST_ELT (op1, i); | |
16545 | ||
16546 | tree tem = fold_relational_const (code, elem_type, | |
16547 | elem0, elem1); | |
16548 | ||
16549 | if (tem == NULL_TREE) | |
16550 | return NULL_TREE; | |
16551 | ||
16552 | elts[i] = build_int_cst (elem_type, integer_zerop (tem) ? 0 : -1); | |
16553 | } | |
16554 | ||
16555 | return build_vector (type, elts); | |
16556 | } | |
16557 | ||
ee8db92b | 16558 | /* From here on we only handle LT, LE, GT, GE, EQ and NE. |
8e7b3a43 KH |
16559 | |
16560 | To compute GT, swap the arguments and do LT. | |
16561 | To compute GE, do LT and invert the result. | |
16562 | To compute LE, swap the arguments, do LT and invert the result. | |
16563 | To compute NE, do EQ and invert the result. | |
16564 | ||
16565 | Therefore, the code below must handle only EQ and LT. */ | |
16566 | ||
16567 | if (code == LE_EXPR || code == GT_EXPR) | |
16568 | { | |
1382f0f0 RS |
16569 | tree tem = op0; |
16570 | op0 = op1; | |
16571 | op1 = tem; | |
8e7b3a43 KH |
16572 | code = swap_tree_comparison (code); |
16573 | } | |
16574 | ||
16575 | /* Note that it is safe to invert for real values here because we | |
ee8db92b | 16576 | have already handled the one case that it matters. */ |
8e7b3a43 | 16577 | |
8e7b3a43 KH |
16578 | invert = 0; |
16579 | if (code == NE_EXPR || code == GE_EXPR) | |
16580 | { | |
16581 | invert = 1; | |
d1a7edaf | 16582 | code = invert_tree_comparison (code, false); |
8e7b3a43 KH |
16583 | } |
16584 | ||
16585 | /* Compute a result for LT or EQ if args permit; | |
16586 | Otherwise return T. */ | |
16587 | if (TREE_CODE (op0) == INTEGER_CST && TREE_CODE (op1) == INTEGER_CST) | |
16588 | { | |
16589 | if (code == EQ_EXPR) | |
1382f0f0 RS |
16590 | result = tree_int_cst_equal (op0, op1); |
16591 | else if (TYPE_UNSIGNED (TREE_TYPE (op0))) | |
16592 | result = INT_CST_LT_UNSIGNED (op0, op1); | |
8e7b3a43 | 16593 | else |
1382f0f0 | 16594 | result = INT_CST_LT (op0, op1); |
8e7b3a43 | 16595 | } |
1382f0f0 | 16596 | else |
8e7b3a43 KH |
16597 | return NULL_TREE; |
16598 | ||
16599 | if (invert) | |
1382f0f0 RS |
16600 | result ^= 1; |
16601 | return constant_boolean_node (result, type); | |
8e7b3a43 KH |
16602 | } |
16603 | ||
3a687f8b MM |
16604 | /* If necessary, return a CLEANUP_POINT_EXPR for EXPR with the |
16605 | indicated TYPE. If no CLEANUP_POINT_EXPR is necessary, return EXPR | |
16606 | itself. */ | |
0ad28dde AP |
16607 | |
16608 | tree | |
16609 | fold_build_cleanup_point_expr (tree type, tree expr) | |
16610 | { | |
16611 | /* If the expression does not have side effects then we don't have to wrap | |
16612 | it with a cleanup point expression. */ | |
16613 | if (!TREE_SIDE_EFFECTS (expr)) | |
16614 | return expr; | |
0e256a82 AP |
16615 | |
16616 | /* If the expression is a return, check to see if the expression inside the | |
16617 | return has no side effects or the right hand side of the modify expression | |
16618 | inside the return. If either don't have side effects set we don't need to | |
16619 | wrap the expression in a cleanup point expression. Note we don't check the | |
16620 | left hand side of the modify because it should always be a return decl. */ | |
16621 | if (TREE_CODE (expr) == RETURN_EXPR) | |
16622 | { | |
16623 | tree op = TREE_OPERAND (expr, 0); | |
16624 | if (!op || !TREE_SIDE_EFFECTS (op)) | |
16625 | return expr; | |
16626 | op = TREE_OPERAND (op, 1); | |
16627 | if (!TREE_SIDE_EFFECTS (op)) | |
16628 | return expr; | |
16629 | } | |
b8698a0f | 16630 | |
0ad28dde AP |
16631 | return build1 (CLEANUP_POINT_EXPR, type, expr); |
16632 | } | |
16633 | ||
30d2e943 RG |
16634 | /* Given a pointer value OP0 and a type TYPE, return a simplified version |
16635 | of an indirection through OP0, or NULL_TREE if no simplification is | |
16636 | possible. */ | |
cd3ce9b4 | 16637 | |
095ecc24 | 16638 | tree |
db3927fb | 16639 | fold_indirect_ref_1 (location_t loc, tree type, tree op0) |
cd3ce9b4 | 16640 | { |
30d2e943 | 16641 | tree sub = op0; |
cd3ce9b4 JM |
16642 | tree subtype; |
16643 | ||
6033ae2a | 16644 | STRIP_NOPS (sub); |
6a720599 JM |
16645 | subtype = TREE_TYPE (sub); |
16646 | if (!POINTER_TYPE_P (subtype)) | |
16647 | return NULL_TREE; | |
16648 | ||
cd3ce9b4 JM |
16649 | if (TREE_CODE (sub) == ADDR_EXPR) |
16650 | { | |
16651 | tree op = TREE_OPERAND (sub, 0); | |
16652 | tree optype = TREE_TYPE (op); | |
f9f63ff2 AP |
16653 | /* *&CONST_DECL -> to the value of the const decl. */ |
16654 | if (TREE_CODE (op) == CONST_DECL) | |
16655 | return DECL_INITIAL (op); | |
41b9109a | 16656 | /* *&p => p; make sure to handle *&"str"[cst] here. */ |
30d2e943 | 16657 | if (type == optype) |
41b9109a RG |
16658 | { |
16659 | tree fop = fold_read_from_constant_string (op); | |
16660 | if (fop) | |
16661 | return fop; | |
16662 | else | |
16663 | return op; | |
16664 | } | |
cd3ce9b4 JM |
16665 | /* *(foo *)&fooarray => fooarray[0] */ |
16666 | else if (TREE_CODE (optype) == ARRAY_TYPE | |
17dea42f RG |
16667 | && type == TREE_TYPE (optype) |
16668 | && (!in_gimple_form | |
16669 | || TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)) | |
0d56ab33 AP |
16670 | { |
16671 | tree type_domain = TYPE_DOMAIN (optype); | |
16672 | tree min_val = size_zero_node; | |
16673 | if (type_domain && TYPE_MIN_VALUE (type_domain)) | |
16674 | min_val = TYPE_MIN_VALUE (type_domain); | |
17dea42f RG |
16675 | if (in_gimple_form |
16676 | && TREE_CODE (min_val) != INTEGER_CST) | |
16677 | return NULL_TREE; | |
c9019218 JJ |
16678 | return build4_loc (loc, ARRAY_REF, type, op, min_val, |
16679 | NULL_TREE, NULL_TREE); | |
0d56ab33 | 16680 | } |
4853940c AP |
16681 | /* *(foo *)&complexfoo => __real__ complexfoo */ |
16682 | else if (TREE_CODE (optype) == COMPLEX_TYPE | |
16683 | && type == TREE_TYPE (optype)) | |
db3927fb | 16684 | return fold_build1_loc (loc, REALPART_EXPR, type, op); |
0890b981 AP |
16685 | /* *(foo *)&vectorfoo => BIT_FIELD_REF<vectorfoo,...> */ |
16686 | else if (TREE_CODE (optype) == VECTOR_TYPE | |
16687 | && type == TREE_TYPE (optype)) | |
16688 | { | |
16689 | tree part_width = TYPE_SIZE (type); | |
16690 | tree index = bitsize_int (0); | |
db3927fb | 16691 | return fold_build3_loc (loc, BIT_FIELD_REF, type, op, part_width, index); |
0890b981 | 16692 | } |
cd3ce9b4 JM |
16693 | } |
16694 | ||
a12bdb97 AP |
16695 | if (TREE_CODE (sub) == POINTER_PLUS_EXPR |
16696 | && TREE_CODE (TREE_OPERAND (sub, 1)) == INTEGER_CST) | |
b8698a0f | 16697 | { |
a12bdb97 AP |
16698 | tree op00 = TREE_OPERAND (sub, 0); |
16699 | tree op01 = TREE_OPERAND (sub, 1); | |
b8698a0f | 16700 | |
a12bdb97 | 16701 | STRIP_NOPS (op00); |
7bf8ca76 | 16702 | if (TREE_CODE (op00) == ADDR_EXPR) |
b8698a0f | 16703 | { |
7bf8ca76 JM |
16704 | tree op00type; |
16705 | op00 = TREE_OPERAND (op00, 0); | |
16706 | op00type = TREE_TYPE (op00); | |
b8698a0f | 16707 | |
7bf8ca76 JM |
16708 | /* ((foo*)&vectorfoo)[1] => BIT_FIELD_REF<vectorfoo,...> */ |
16709 | if (TREE_CODE (op00type) == VECTOR_TYPE | |
16710 | && type == TREE_TYPE (op00type)) | |
16711 | { | |
16712 | HOST_WIDE_INT offset = tree_low_cst (op01, 0); | |
16713 | tree part_width = TYPE_SIZE (type); | |
16714 | unsigned HOST_WIDE_INT part_widthi = tree_low_cst (part_width, 0)/BITS_PER_UNIT; | |
16715 | unsigned HOST_WIDE_INT indexi = offset * BITS_PER_UNIT; | |
16716 | tree index = bitsize_int (indexi); | |
a12bdb97 | 16717 | |
7bf8ca76 JM |
16718 | if (offset/part_widthi <= TYPE_VECTOR_SUBPARTS (op00type)) |
16719 | return fold_build3_loc (loc, | |
16720 | BIT_FIELD_REF, type, op00, | |
16721 | part_width, index); | |
a12bdb97 | 16722 | |
7bf8ca76 JM |
16723 | } |
16724 | /* ((foo*)&complexfoo)[1] => __imag__ complexfoo */ | |
16725 | else if (TREE_CODE (op00type) == COMPLEX_TYPE | |
16726 | && type == TREE_TYPE (op00type)) | |
16727 | { | |
16728 | tree size = TYPE_SIZE_UNIT (type); | |
16729 | if (tree_int_cst_equal (size, op01)) | |
16730 | return fold_build1_loc (loc, IMAGPART_EXPR, type, op00); | |
16731 | } | |
16732 | /* ((foo *)&fooarray)[1] => fooarray[1] */ | |
16733 | else if (TREE_CODE (op00type) == ARRAY_TYPE | |
16734 | && type == TREE_TYPE (op00type)) | |
16735 | { | |
16736 | tree type_domain = TYPE_DOMAIN (op00type); | |
16737 | tree min_val = size_zero_node; | |
16738 | if (type_domain && TYPE_MIN_VALUE (type_domain)) | |
16739 | min_val = TYPE_MIN_VALUE (type_domain); | |
16740 | op01 = size_binop_loc (loc, EXACT_DIV_EXPR, op01, | |
16741 | TYPE_SIZE_UNIT (type)); | |
16742 | op01 = size_binop_loc (loc, PLUS_EXPR, op01, min_val); | |
c9019218 JJ |
16743 | return build4_loc (loc, ARRAY_REF, type, op00, op01, |
16744 | NULL_TREE, NULL_TREE); | |
7bf8ca76 | 16745 | } |
4853940c AP |
16746 | } |
16747 | } | |
b8698a0f | 16748 | |
cd3ce9b4 | 16749 | /* *(foo *)fooarrptr => (*fooarrptr)[0] */ |
cd3ce9b4 | 16750 | if (TREE_CODE (TREE_TYPE (subtype)) == ARRAY_TYPE |
17dea42f RG |
16751 | && type == TREE_TYPE (TREE_TYPE (subtype)) |
16752 | && (!in_gimple_form | |
16753 | || TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)) | |
cd3ce9b4 | 16754 | { |
0d56ab33 AP |
16755 | tree type_domain; |
16756 | tree min_val = size_zero_node; | |
db3927fb | 16757 | sub = build_fold_indirect_ref_loc (loc, sub); |
0d56ab33 AP |
16758 | type_domain = TYPE_DOMAIN (TREE_TYPE (sub)); |
16759 | if (type_domain && TYPE_MIN_VALUE (type_domain)) | |
16760 | min_val = TYPE_MIN_VALUE (type_domain); | |
17dea42f RG |
16761 | if (in_gimple_form |
16762 | && TREE_CODE (min_val) != INTEGER_CST) | |
16763 | return NULL_TREE; | |
c9019218 JJ |
16764 | return build4_loc (loc, ARRAY_REF, type, sub, min_val, NULL_TREE, |
16765 | NULL_TREE); | |
cd3ce9b4 JM |
16766 | } |
16767 | ||
6a720599 JM |
16768 | return NULL_TREE; |
16769 | } | |
16770 | ||
16771 | /* Builds an expression for an indirection through T, simplifying some | |
16772 | cases. */ | |
16773 | ||
16774 | tree | |
db3927fb | 16775 | build_fold_indirect_ref_loc (location_t loc, tree t) |
6a720599 | 16776 | { |
30d2e943 | 16777 | tree type = TREE_TYPE (TREE_TYPE (t)); |
db3927fb | 16778 | tree sub = fold_indirect_ref_1 (loc, type, t); |
6a720599 JM |
16779 | |
16780 | if (sub) | |
16781 | return sub; | |
db3927fb | 16782 | |
c9019218 | 16783 | return build1_loc (loc, INDIRECT_REF, type, t); |
6a720599 JM |
16784 | } |
16785 | ||
16786 | /* Given an INDIRECT_REF T, return either T or a simplified version. */ | |
16787 | ||
16788 | tree | |
db3927fb | 16789 | fold_indirect_ref_loc (location_t loc, tree t) |
6a720599 | 16790 | { |
db3927fb | 16791 | tree sub = fold_indirect_ref_1 (loc, TREE_TYPE (t), TREE_OPERAND (t, 0)); |
6a720599 JM |
16792 | |
16793 | if (sub) | |
16794 | return sub; | |
16795 | else | |
16796 | return t; | |
cd3ce9b4 JM |
16797 | } |
16798 | ||
9675412f RS |
16799 | /* Strip non-trapping, non-side-effecting tree nodes from an expression |
16800 | whose result is ignored. The type of the returned tree need not be | |
16801 | the same as the original expression. */ | |
16802 | ||
16803 | tree | |
16804 | fold_ignored_result (tree t) | |
16805 | { | |
16806 | if (!TREE_SIDE_EFFECTS (t)) | |
16807 | return integer_zero_node; | |
16808 | ||
16809 | for (;;) | |
16810 | switch (TREE_CODE_CLASS (TREE_CODE (t))) | |
16811 | { | |
6615c446 | 16812 | case tcc_unary: |
9675412f RS |
16813 | t = TREE_OPERAND (t, 0); |
16814 | break; | |
16815 | ||
6615c446 JO |
16816 | case tcc_binary: |
16817 | case tcc_comparison: | |
9675412f RS |
16818 | if (!TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1))) |
16819 | t = TREE_OPERAND (t, 0); | |
16820 | else if (!TREE_SIDE_EFFECTS (TREE_OPERAND (t, 0))) | |
16821 | t = TREE_OPERAND (t, 1); | |
16822 | else | |
16823 | return t; | |
16824 | break; | |
16825 | ||
6615c446 | 16826 | case tcc_expression: |
9675412f RS |
16827 | switch (TREE_CODE (t)) |
16828 | { | |
16829 | case COMPOUND_EXPR: | |
16830 | if (TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1))) | |
16831 | return t; | |
16832 | t = TREE_OPERAND (t, 0); | |
16833 | break; | |
16834 | ||
16835 | case COND_EXPR: | |
16836 | if (TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1)) | |
16837 | || TREE_SIDE_EFFECTS (TREE_OPERAND (t, 2))) | |
16838 | return t; | |
16839 | t = TREE_OPERAND (t, 0); | |
16840 | break; | |
16841 | ||
16842 | default: | |
16843 | return t; | |
16844 | } | |
16845 | break; | |
16846 | ||
16847 | default: | |
16848 | return t; | |
16849 | } | |
16850 | } | |
16851 | ||
15931954 RH |
16852 | /* Return the value of VALUE, rounded up to a multiple of DIVISOR. |
16853 | This can only be applied to objects of a sizetype. */ | |
16854 | ||
16855 | tree | |
db3927fb | 16856 | round_up_loc (location_t loc, tree value, int divisor) |
15931954 | 16857 | { |
0a936b12 | 16858 | tree div = NULL_TREE; |
15931954 | 16859 | |
0bccc606 | 16860 | gcc_assert (divisor > 0); |
15931954 RH |
16861 | if (divisor == 1) |
16862 | return value; | |
16863 | ||
15931954 | 16864 | /* See if VALUE is already a multiple of DIVISOR. If so, we don't |
0a936b12 NS |
16865 | have to do anything. Only do this when we are not given a const, |
16866 | because in that case, this check is more expensive than just | |
8c27b7d4 | 16867 | doing it. */ |
0a936b12 NS |
16868 | if (TREE_CODE (value) != INTEGER_CST) |
16869 | { | |
ce552f75 | 16870 | div = build_int_cst (TREE_TYPE (value), divisor); |
0a936b12 NS |
16871 | |
16872 | if (multiple_of_p (TREE_TYPE (value), value, div)) | |
16873 | return value; | |
16874 | } | |
15931954 RH |
16875 | |
16876 | /* If divisor is a power of two, simplify this to bit manipulation. */ | |
16877 | if (divisor == (divisor & -divisor)) | |
16878 | { | |
74890d7b RS |
16879 | if (TREE_CODE (value) == INTEGER_CST) |
16880 | { | |
9589f23e | 16881 | double_int val = tree_to_double_int (value); |
bcf52d7b | 16882 | bool overflow_p; |
74890d7b | 16883 | |
9589f23e | 16884 | if ((val.low & (divisor - 1)) == 0) |
74890d7b RS |
16885 | return value; |
16886 | ||
bcf52d7b | 16887 | overflow_p = TREE_OVERFLOW (value); |
9589f23e AS |
16888 | val.low &= ~(divisor - 1); |
16889 | val.low += divisor; | |
16890 | if (val.low == 0) | |
74890d7b | 16891 | { |
9589f23e AS |
16892 | val.high++; |
16893 | if (val.high == 0) | |
bcf52d7b | 16894 | overflow_p = true; |
74890d7b | 16895 | } |
bcf52d7b | 16896 | |
9589f23e | 16897 | return force_fit_type_double (TREE_TYPE (value), val, |
bcf52d7b | 16898 | -1, overflow_p); |
74890d7b RS |
16899 | } |
16900 | else | |
16901 | { | |
bcf52d7b RS |
16902 | tree t; |
16903 | ||
74890d7b | 16904 | t = build_int_cst (TREE_TYPE (value), divisor - 1); |
db3927fb | 16905 | value = size_binop_loc (loc, PLUS_EXPR, value, t); |
74890d7b | 16906 | t = build_int_cst (TREE_TYPE (value), -divisor); |
db3927fb | 16907 | value = size_binop_loc (loc, BIT_AND_EXPR, value, t); |
74890d7b | 16908 | } |
15931954 RH |
16909 | } |
16910 | else | |
16911 | { | |
0a936b12 | 16912 | if (!div) |
ce552f75 | 16913 | div = build_int_cst (TREE_TYPE (value), divisor); |
db3927fb AH |
16914 | value = size_binop_loc (loc, CEIL_DIV_EXPR, value, div); |
16915 | value = size_binop_loc (loc, MULT_EXPR, value, div); | |
15931954 RH |
16916 | } |
16917 | ||
16918 | return value; | |
16919 | } | |
16920 | ||
16921 | /* Likewise, but round down. */ | |
16922 | ||
16923 | tree | |
db3927fb | 16924 | round_down_loc (location_t loc, tree value, int divisor) |
15931954 | 16925 | { |
0a936b12 | 16926 | tree div = NULL_TREE; |
15931954 | 16927 | |
0bccc606 | 16928 | gcc_assert (divisor > 0); |
15931954 RH |
16929 | if (divisor == 1) |
16930 | return value; | |
16931 | ||
15931954 | 16932 | /* See if VALUE is already a multiple of DIVISOR. If so, we don't |
0a936b12 NS |
16933 | have to do anything. Only do this when we are not given a const, |
16934 | because in that case, this check is more expensive than just | |
8c27b7d4 | 16935 | doing it. */ |
0a936b12 NS |
16936 | if (TREE_CODE (value) != INTEGER_CST) |
16937 | { | |
ce552f75 | 16938 | div = build_int_cst (TREE_TYPE (value), divisor); |
0a936b12 NS |
16939 | |
16940 | if (multiple_of_p (TREE_TYPE (value), value, div)) | |
16941 | return value; | |
16942 | } | |
15931954 RH |
16943 | |
16944 | /* If divisor is a power of two, simplify this to bit manipulation. */ | |
16945 | if (divisor == (divisor & -divisor)) | |
16946 | { | |
0a936b12 | 16947 | tree t; |
3e6688a7 | 16948 | |
7d60be94 | 16949 | t = build_int_cst (TREE_TYPE (value), -divisor); |
db3927fb | 16950 | value = size_binop_loc (loc, BIT_AND_EXPR, value, t); |
15931954 RH |
16951 | } |
16952 | else | |
16953 | { | |
0a936b12 | 16954 | if (!div) |
ce552f75 | 16955 | div = build_int_cst (TREE_TYPE (value), divisor); |
db3927fb AH |
16956 | value = size_binop_loc (loc, FLOOR_DIV_EXPR, value, div); |
16957 | value = size_binop_loc (loc, MULT_EXPR, value, div); | |
15931954 RH |
16958 | } |
16959 | ||
16960 | return value; | |
16961 | } | |
2f4675b4 | 16962 | |
7299dbfb ZD |
16963 | /* Returns the pointer to the base of the object addressed by EXP and |
16964 | extracts the information about the offset of the access, storing it | |
16965 | to PBITPOS and POFFSET. */ | |
16966 | ||
16967 | static tree | |
16968 | split_address_to_core_and_offset (tree exp, | |
16969 | HOST_WIDE_INT *pbitpos, tree *poffset) | |
16970 | { | |
16971 | tree core; | |
16972 | enum machine_mode mode; | |
16973 | int unsignedp, volatilep; | |
16974 | HOST_WIDE_INT bitsize; | |
db3927fb | 16975 | location_t loc = EXPR_LOCATION (exp); |
7299dbfb ZD |
16976 | |
16977 | if (TREE_CODE (exp) == ADDR_EXPR) | |
16978 | { | |
16979 | core = get_inner_reference (TREE_OPERAND (exp, 0), &bitsize, pbitpos, | |
2614034e EB |
16980 | poffset, &mode, &unsignedp, &volatilep, |
16981 | false); | |
db3927fb | 16982 | core = build_fold_addr_expr_loc (loc, core); |
7299dbfb ZD |
16983 | } |
16984 | else | |
16985 | { | |
16986 | core = exp; | |
16987 | *pbitpos = 0; | |
16988 | *poffset = NULL_TREE; | |
16989 | } | |
16990 | ||
16991 | return core; | |
16992 | } | |
16993 | ||
2f4675b4 | 16994 | /* Returns true if addresses of E1 and E2 differ by a constant, false |
7299dbfb | 16995 | otherwise. If they do, E1 - E2 is stored in *DIFF. */ |
2f4675b4 ZD |
16996 | |
16997 | bool | |
16998 | ptr_difference_const (tree e1, tree e2, HOST_WIDE_INT *diff) | |
16999 | { | |
17000 | tree core1, core2; | |
2f4675b4 ZD |
17001 | HOST_WIDE_INT bitpos1, bitpos2; |
17002 | tree toffset1, toffset2, tdiff, type; | |
3e6688a7 | 17003 | |
7299dbfb ZD |
17004 | core1 = split_address_to_core_and_offset (e1, &bitpos1, &toffset1); |
17005 | core2 = split_address_to_core_and_offset (e2, &bitpos2, &toffset2); | |
2f4675b4 ZD |
17006 | |
17007 | if (bitpos1 % BITS_PER_UNIT != 0 | |
17008 | || bitpos2 % BITS_PER_UNIT != 0 | |
17009 | || !operand_equal_p (core1, core2, 0)) | |
17010 | return false; | |
17011 | ||
17012 | if (toffset1 && toffset2) | |
17013 | { | |
17014 | type = TREE_TYPE (toffset1); | |
17015 | if (type != TREE_TYPE (toffset2)) | |
17016 | toffset2 = fold_convert (type, toffset2); | |
17017 | ||
7f20a5b7 | 17018 | tdiff = fold_build2 (MINUS_EXPR, type, toffset1, toffset2); |
87de2376 | 17019 | if (!cst_and_fits_in_hwi (tdiff)) |
2f4675b4 ZD |
17020 | return false; |
17021 | ||
87de2376 | 17022 | *diff = int_cst_value (tdiff); |
2f4675b4 ZD |
17023 | } |
17024 | else if (toffset1 || toffset2) | |
17025 | { | |
17026 | /* If only one of the offsets is non-constant, the difference cannot | |
17027 | be a constant. */ | |
17028 | return false; | |
17029 | } | |
17030 | else | |
17031 | *diff = 0; | |
17032 | ||
17033 | *diff += (bitpos1 - bitpos2) / BITS_PER_UNIT; | |
17034 | return true; | |
17035 | } | |
e3bb43c0 RS |
17036 | |
17037 | /* Simplify the floating point expression EXP when the sign of the | |
17038 | result is not significant. Return NULL_TREE if no simplification | |
17039 | is possible. */ | |
17040 | ||
17041 | tree | |
17042 | fold_strip_sign_ops (tree exp) | |
17043 | { | |
17044 | tree arg0, arg1; | |
db3927fb | 17045 | location_t loc = EXPR_LOCATION (exp); |
e3bb43c0 RS |
17046 | |
17047 | switch (TREE_CODE (exp)) | |
17048 | { | |
17049 | case ABS_EXPR: | |
17050 | case NEGATE_EXPR: | |
17051 | arg0 = fold_strip_sign_ops (TREE_OPERAND (exp, 0)); | |
17052 | return arg0 ? arg0 : TREE_OPERAND (exp, 0); | |
17053 | ||
17054 | case MULT_EXPR: | |
17055 | case RDIV_EXPR: | |
17056 | if (HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (TREE_TYPE (exp)))) | |
17057 | return NULL_TREE; | |
17058 | arg0 = fold_strip_sign_ops (TREE_OPERAND (exp, 0)); | |
17059 | arg1 = fold_strip_sign_ops (TREE_OPERAND (exp, 1)); | |
17060 | if (arg0 != NULL_TREE || arg1 != NULL_TREE) | |
db3927fb | 17061 | return fold_build2_loc (loc, TREE_CODE (exp), TREE_TYPE (exp), |
7f20a5b7 KH |
17062 | arg0 ? arg0 : TREE_OPERAND (exp, 0), |
17063 | arg1 ? arg1 : TREE_OPERAND (exp, 1)); | |
e3bb43c0 RS |
17064 | break; |
17065 | ||
b7e85170 KG |
17066 | case COMPOUND_EXPR: |
17067 | arg0 = TREE_OPERAND (exp, 0); | |
17068 | arg1 = fold_strip_sign_ops (TREE_OPERAND (exp, 1)); | |
17069 | if (arg1) | |
db3927fb | 17070 | return fold_build2_loc (loc, COMPOUND_EXPR, TREE_TYPE (exp), arg0, arg1); |
b7e85170 | 17071 | break; |
b8698a0f | 17072 | |
b7e85170 KG |
17073 | case COND_EXPR: |
17074 | arg0 = fold_strip_sign_ops (TREE_OPERAND (exp, 1)); | |
17075 | arg1 = fold_strip_sign_ops (TREE_OPERAND (exp, 2)); | |
17076 | if (arg0 || arg1) | |
db3927fb AH |
17077 | return fold_build3_loc (loc, |
17078 | COND_EXPR, TREE_TYPE (exp), TREE_OPERAND (exp, 0), | |
b7e85170 KG |
17079 | arg0 ? arg0 : TREE_OPERAND (exp, 1), |
17080 | arg1 ? arg1 : TREE_OPERAND (exp, 2)); | |
17081 | break; | |
b8698a0f | 17082 | |
b81e7144 | 17083 | case CALL_EXPR: |
6af46feb KG |
17084 | { |
17085 | const enum built_in_function fcode = builtin_mathfn_code (exp); | |
17086 | switch (fcode) | |
17087 | { | |
17088 | CASE_FLT_FN (BUILT_IN_COPYSIGN): | |
17089 | /* Strip copysign function call, return the 1st argument. */ | |
5039610b SL |
17090 | arg0 = CALL_EXPR_ARG (exp, 0); |
17091 | arg1 = CALL_EXPR_ARG (exp, 1); | |
db3927fb | 17092 | return omit_one_operand_loc (loc, TREE_TYPE (exp), arg0, arg1); |
6af46feb KG |
17093 | |
17094 | default: | |
17095 | /* Strip sign ops from the argument of "odd" math functions. */ | |
17096 | if (negate_mathfn_p (fcode)) | |
17097 | { | |
5039610b | 17098 | arg0 = fold_strip_sign_ops (CALL_EXPR_ARG (exp, 0)); |
6af46feb | 17099 | if (arg0) |
db3927fb | 17100 | return build_call_expr_loc (loc, get_callee_fndecl (exp), 1, arg0); |
6af46feb KG |
17101 | } |
17102 | break; | |
b81e7144 | 17103 | } |
6af46feb | 17104 | } |
b81e7144 KG |
17105 | break; |
17106 | ||
e3bb43c0 RS |
17107 | default: |
17108 | break; | |
17109 | } | |
17110 | return NULL_TREE; | |
17111 | } |