]>
Commit | Line | Data |
---|---|---|
2bc77e10 | 1 | /* Fold a constant sub-tree into a single node for C-compiler |
4a2064d7 | 2 | Copyright (C) 1987, 88, 92, 93, 94, 95, 1996 Free Software Foundation, Inc. |
2bc77e10 | 3 | |
4 | This file is part of GNU CC. | |
5 | ||
6 | GNU CC is free software; you can redistribute it and/or modify | |
7 | it under the terms of the GNU General Public License as published by | |
8 | the Free Software Foundation; either version 2, or (at your option) | |
9 | any later version. | |
10 | ||
11 | GNU CC is distributed in the hope that it will be useful, | |
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | GNU General Public License for more details. | |
15 | ||
16 | You should have received a copy of the GNU General Public License | |
17 | along with GNU CC; see the file COPYING. If not, write to | |
0355838f | 18 | the Free Software Foundation, 59 Temple Place - Suite 330, |
19 | Boston, MA 02111-1307, USA. */ | |
2bc77e10 | 20 | |
4bbea254 | 21 | /*@@ This file should be rewritten to use an arbitrary precision |
2bc77e10 | 22 | @@ representation for "struct tree_int_cst" and "struct tree_real_cst". |
23 | @@ Perhaps the routines could also be used for bc/dc, and made a lib. | |
24 | @@ The routines that translate from the ap rep should | |
25 | @@ warn if precision et. al. is lost. | |
26 | @@ This would also make life easier when this technology is used | |
27 | @@ for cross-compilers. */ | |
28 | ||
29 | ||
30 | /* The entry points in this file are fold, size_int and size_binop. | |
31 | ||
32 | fold takes a tree as argument and returns a simplified tree. | |
33 | ||
34 | size_binop takes a tree code for an arithmetic operation | |
35 | and two operands that are trees, and produces a tree for the | |
36 | result, assuming the type comes from `sizetype'. | |
37 | ||
38 | size_int takes an integer value, and creates a tree constant | |
39 | with type from `sizetype'. */ | |
40 | ||
41 | #include <stdio.h> | |
42 | #include <setjmp.h> | |
43 | #include "config.h" | |
44 | #include "flags.h" | |
45 | #include "tree.h" | |
46 | ||
c0244247 | 47 | /* Handle floating overflow for `const_binop'. */ |
48 | static jmp_buf float_error; | |
49 | ||
12ec0a8a | 50 | static void encode PROTO((HOST_WIDE_INT *, |
51 | HOST_WIDE_INT, HOST_WIDE_INT)); | |
52 | static void decode PROTO((HOST_WIDE_INT *, | |
53 | HOST_WIDE_INT *, HOST_WIDE_INT *)); | |
54 | int div_and_round_double PROTO((enum tree_code, int, HOST_WIDE_INT, | |
bfd67d2c | 55 | HOST_WIDE_INT, HOST_WIDE_INT, |
56 | HOST_WIDE_INT, HOST_WIDE_INT *, | |
57 | HOST_WIDE_INT *, HOST_WIDE_INT *, | |
58 | HOST_WIDE_INT *)); | |
12ec0a8a | 59 | static int split_tree PROTO((tree, enum tree_code, tree *, |
60 | tree *, int *)); | |
61 | static tree const_binop PROTO((enum tree_code, tree, tree, int)); | |
62 | static tree fold_convert PROTO((tree, tree)); | |
bfd67d2c | 63 | static enum tree_code invert_tree_comparison PROTO((enum tree_code)); |
64 | static enum tree_code swap_tree_comparison PROTO((enum tree_code)); | |
12ec0a8a | 65 | static int truth_value_p PROTO((enum tree_code)); |
bfd67d2c | 66 | static int operand_equal_for_comparison_p PROTO((tree, tree, tree)); |
12ec0a8a | 67 | static int twoval_comparison_p PROTO((tree, tree *, tree *, int *)); |
68 | static tree eval_subst PROTO((tree, tree, tree, tree, tree)); | |
69 | static tree omit_one_operand PROTO((tree, tree, tree)); | |
6df5edfa | 70 | static tree pedantic_omit_one_operand PROTO((tree, tree, tree)); |
bfd67d2c | 71 | static tree distribute_bit_expr PROTO((enum tree_code, tree, tree, tree)); |
12ec0a8a | 72 | static tree make_bit_field_ref PROTO((tree, tree, int, int, int)); |
bfd67d2c | 73 | static tree optimize_bit_field_compare PROTO((enum tree_code, tree, |
74 | tree, tree)); | |
75 | static tree decode_field_reference PROTO((tree, int *, int *, | |
76 | enum machine_mode *, int *, | |
2a6329ae | 77 | int *, tree *, tree *)); |
12ec0a8a | 78 | static int all_ones_mask_p PROTO((tree, int)); |
79 | static int simple_operand_p PROTO((tree)); | |
80 | static tree range_binop PROTO((enum tree_code, tree, tree, int, | |
81 | tree, int)); | |
82 | static tree make_range PROTO((tree, int *, tree *, tree *)); | |
83 | static tree build_range_check PROTO((tree, tree, int, tree, tree)); | |
84 | static int merge_ranges PROTO((int *, tree *, tree *, int, tree, tree, | |
85 | int, tree, tree)); | |
86 | static tree fold_range_test PROTO((tree)); | |
87 | static tree unextend PROTO((tree, int, int, tree)); | |
88 | static tree fold_truthop PROTO((enum tree_code, tree, tree, tree)); | |
58a718ca | 89 | static tree strip_compound_expr PROTO((tree, tree)); |
3638c76b | 90 | |
91 | #ifndef BRANCH_COST | |
92 | #define BRANCH_COST 1 | |
93 | #endif | |
b9e999f0 | 94 | |
b9e999f0 | 95 | /* Suppose A1 + B1 = SUM1, using 2's complement arithmetic ignoring overflow. |
96 | Suppose A, B and SUM have the same respective signs as A1, B1, and SUM1. | |
97 | Then this yields nonzero if overflow occurred during the addition. | |
98 | Overflow occurs if A and B have the same sign, but A and SUM differ in sign. | |
99 | Use `^' to test whether signs differ, and `< 0' to isolate the sign. */ | |
100 | #define overflow_sum_sign(a, b, sum) ((~((a) ^ (b)) & ((a) ^ (sum))) < 0) | |
2bc77e10 | 101 | \f |
b572011e | 102 | /* To do constant folding on INTEGER_CST nodes requires two-word arithmetic. |
bd5b3bce | 103 | We do that by representing the two-word integer in 4 words, with only |
104 | HOST_BITS_PER_WIDE_INT/2 bits stored in each word, as a positive number. */ | |
105 | ||
106 | #define LOWPART(x) \ | |
107 | ((x) & (((unsigned HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT/2)) - 1)) | |
108 | #define HIGHPART(x) \ | |
109 | ((unsigned HOST_WIDE_INT) (x) >> HOST_BITS_PER_WIDE_INT/2) | |
110 | #define BASE ((unsigned HOST_WIDE_INT) 1 << HOST_BITS_PER_WIDE_INT/2) | |
2bc77e10 | 111 | |
bd5b3bce | 112 | /* Unpack a two-word integer into 4 words. |
b572011e | 113 | LOW and HI are the integer, as two `HOST_WIDE_INT' pieces. |
bd5b3bce | 114 | WORDS points to the array of HOST_WIDE_INTs. */ |
2bc77e10 | 115 | |
116 | static void | |
bd5b3bce | 117 | encode (words, low, hi) |
118 | HOST_WIDE_INT *words; | |
b572011e | 119 | HOST_WIDE_INT low, hi; |
2bc77e10 | 120 | { |
bd5b3bce | 121 | words[0] = LOWPART (low); |
122 | words[1] = HIGHPART (low); | |
123 | words[2] = LOWPART (hi); | |
124 | words[3] = HIGHPART (hi); | |
2bc77e10 | 125 | } |
126 | ||
bd5b3bce | 127 | /* Pack an array of 4 words into a two-word integer. |
128 | WORDS points to the array of words. | |
b572011e | 129 | The integer is stored into *LOW and *HI as two `HOST_WIDE_INT' pieces. */ |
2bc77e10 | 130 | |
131 | static void | |
bd5b3bce | 132 | decode (words, low, hi) |
133 | HOST_WIDE_INT *words; | |
b572011e | 134 | HOST_WIDE_INT *low, *hi; |
2bc77e10 | 135 | { |
bd5b3bce | 136 | *low = words[0] | words[1] * BASE; |
137 | *hi = words[2] | words[3] * BASE; | |
2bc77e10 | 138 | } |
139 | \f | |
140 | /* Make the integer constant T valid for its type | |
141 | by setting to 0 or 1 all the bits in the constant | |
f55401f0 | 142 | that don't belong in the type. |
143 | Yield 1 if a signed overflow occurs, 0 otherwise. | |
d54f2451 | 144 | If OVERFLOW is nonzero, a signed overflow has already occurred |
23fed9b2 | 145 | in calculating T, so propagate it. |
146 | ||
147 | Make the real constant T valid for its type by calling CHECK_FLOAT_VALUE, | |
148 | if it exists. */ | |
2bc77e10 | 149 | |
f55401f0 | 150 | int |
151 | force_fit_type (t, overflow) | |
2bc77e10 | 152 | tree t; |
f55401f0 | 153 | int overflow; |
2bc77e10 | 154 | { |
817e5691 | 155 | HOST_WIDE_INT low, high; |
156 | register int prec; | |
2bc77e10 | 157 | |
23fed9b2 | 158 | if (TREE_CODE (t) == REAL_CST) |
159 | { | |
160 | #ifdef CHECK_FLOAT_VALUE | |
161 | CHECK_FLOAT_VALUE (TYPE_MODE (TREE_TYPE (t)), TREE_REAL_CST (t), | |
162 | overflow); | |
163 | #endif | |
164 | return overflow; | |
165 | } | |
166 | ||
167 | else if (TREE_CODE (t) != INTEGER_CST) | |
817e5691 | 168 | return overflow; |
169 | ||
170 | low = TREE_INT_CST_LOW (t); | |
171 | high = TREE_INT_CST_HIGH (t); | |
d7b6c802 | 172 | |
2bc77e10 | 173 | if (TREE_CODE (TREE_TYPE (t)) == POINTER_TYPE) |
174 | prec = POINTER_SIZE; | |
817e5691 | 175 | else |
176 | prec = TYPE_PRECISION (TREE_TYPE (t)); | |
2bc77e10 | 177 | |
178 | /* First clear all bits that are beyond the type's precision. */ | |
179 | ||
b572011e | 180 | if (prec == 2 * HOST_BITS_PER_WIDE_INT) |
2bc77e10 | 181 | ; |
b572011e | 182 | else if (prec > HOST_BITS_PER_WIDE_INT) |
2bc77e10 | 183 | { |
184 | TREE_INT_CST_HIGH (t) | |
b572011e | 185 | &= ~((HOST_WIDE_INT) (-1) << (prec - HOST_BITS_PER_WIDE_INT)); |
2bc77e10 | 186 | } |
187 | else | |
188 | { | |
189 | TREE_INT_CST_HIGH (t) = 0; | |
b572011e | 190 | if (prec < HOST_BITS_PER_WIDE_INT) |
191 | TREE_INT_CST_LOW (t) &= ~((HOST_WIDE_INT) (-1) << prec); | |
2bc77e10 | 192 | } |
193 | ||
f55401f0 | 194 | /* Unsigned types do not suffer sign extension or overflow. */ |
195 | if (TREE_UNSIGNED (TREE_TYPE (t))) | |
941709f7 | 196 | return overflow; |
2bc77e10 | 197 | |
f55401f0 | 198 | /* If the value's sign bit is set, extend the sign. */ |
199 | if (prec != 2 * HOST_BITS_PER_WIDE_INT | |
b572011e | 200 | && (prec > HOST_BITS_PER_WIDE_INT |
201 | ? (TREE_INT_CST_HIGH (t) | |
202 | & ((HOST_WIDE_INT) 1 << (prec - HOST_BITS_PER_WIDE_INT - 1))) | |
203 | : TREE_INT_CST_LOW (t) & ((HOST_WIDE_INT) 1 << (prec - 1)))) | |
2bc77e10 | 204 | { |
205 | /* Value is negative: | |
206 | set to 1 all the bits that are outside this type's precision. */ | |
b572011e | 207 | if (prec > HOST_BITS_PER_WIDE_INT) |
2bc77e10 | 208 | { |
209 | TREE_INT_CST_HIGH (t) | |
b572011e | 210 | |= ((HOST_WIDE_INT) (-1) << (prec - HOST_BITS_PER_WIDE_INT)); |
2bc77e10 | 211 | } |
212 | else | |
213 | { | |
214 | TREE_INT_CST_HIGH (t) = -1; | |
b572011e | 215 | if (prec < HOST_BITS_PER_WIDE_INT) |
216 | TREE_INT_CST_LOW (t) |= ((HOST_WIDE_INT) (-1) << prec); | |
2bc77e10 | 217 | } |
218 | } | |
f55401f0 | 219 | |
220 | /* Yield nonzero if signed overflow occurred. */ | |
221 | return | |
222 | ((overflow | (low ^ TREE_INT_CST_LOW (t)) | (high ^ TREE_INT_CST_HIGH (t))) | |
223 | != 0); | |
2bc77e10 | 224 | } |
225 | \f | |
b572011e | 226 | /* Add two doubleword integers with doubleword result. |
227 | Each argument is given as two `HOST_WIDE_INT' pieces. | |
2bc77e10 | 228 | One argument is L1 and H1; the other, L2 and H2. |
bd5b3bce | 229 | The value is stored as two `HOST_WIDE_INT' pieces in *LV and *HV. */ |
2bc77e10 | 230 | |
b9e999f0 | 231 | int |
2bc77e10 | 232 | add_double (l1, h1, l2, h2, lv, hv) |
b572011e | 233 | HOST_WIDE_INT l1, h1, l2, h2; |
234 | HOST_WIDE_INT *lv, *hv; | |
2bc77e10 | 235 | { |
bd5b3bce | 236 | HOST_WIDE_INT l, h; |
2bc77e10 | 237 | |
bd5b3bce | 238 | l = l1 + l2; |
239 | h = h1 + h2 + ((unsigned HOST_WIDE_INT) l < l1); | |
2bc77e10 | 240 | |
bd5b3bce | 241 | *lv = l; |
242 | *hv = h; | |
243 | return overflow_sum_sign (h1, h2, h); | |
2bc77e10 | 244 | } |
245 | ||
b572011e | 246 | /* Negate a doubleword integer with doubleword result. |
b9e999f0 | 247 | Return nonzero if the operation overflows, assuming it's signed. |
b572011e | 248 | The argument is given as two `HOST_WIDE_INT' pieces in L1 and H1. |
bd5b3bce | 249 | The value is stored as two `HOST_WIDE_INT' pieces in *LV and *HV. */ |
2bc77e10 | 250 | |
b9e999f0 | 251 | int |
2bc77e10 | 252 | neg_double (l1, h1, lv, hv) |
b572011e | 253 | HOST_WIDE_INT l1, h1; |
254 | HOST_WIDE_INT *lv, *hv; | |
2bc77e10 | 255 | { |
256 | if (l1 == 0) | |
257 | { | |
258 | *lv = 0; | |
259 | *hv = - h1; | |
f55401f0 | 260 | return (*hv & h1) < 0; |
2bc77e10 | 261 | } |
262 | else | |
263 | { | |
264 | *lv = - l1; | |
265 | *hv = ~ h1; | |
b9e999f0 | 266 | return 0; |
2bc77e10 | 267 | } |
268 | } | |
269 | \f | |
b572011e | 270 | /* Multiply two doubleword integers with doubleword result. |
b9e999f0 | 271 | Return nonzero if the operation overflows, assuming it's signed. |
b572011e | 272 | Each argument is given as two `HOST_WIDE_INT' pieces. |
2bc77e10 | 273 | One argument is L1 and H1; the other, L2 and H2. |
bd5b3bce | 274 | The value is stored as two `HOST_WIDE_INT' pieces in *LV and *HV. */ |
2bc77e10 | 275 | |
b9e999f0 | 276 | int |
2bc77e10 | 277 | mul_double (l1, h1, l2, h2, lv, hv) |
b572011e | 278 | HOST_WIDE_INT l1, h1, l2, h2; |
279 | HOST_WIDE_INT *lv, *hv; | |
2bc77e10 | 280 | { |
bd5b3bce | 281 | HOST_WIDE_INT arg1[4]; |
282 | HOST_WIDE_INT arg2[4]; | |
283 | HOST_WIDE_INT prod[4 * 2]; | |
284 | register unsigned HOST_WIDE_INT carry; | |
2bc77e10 | 285 | register int i, j, k; |
b9e999f0 | 286 | HOST_WIDE_INT toplow, tophigh, neglow, neghigh; |
2bc77e10 | 287 | |
2bc77e10 | 288 | encode (arg1, l1, h1); |
289 | encode (arg2, l2, h2); | |
290 | ||
748e6d74 | 291 | bzero ((char *) prod, sizeof prod); |
2bc77e10 | 292 | |
bd5b3bce | 293 | for (i = 0; i < 4; i++) |
294 | { | |
295 | carry = 0; | |
296 | for (j = 0; j < 4; j++) | |
297 | { | |
298 | k = i + j; | |
299 | /* This product is <= 0xFFFE0001, the sum <= 0xFFFF0000. */ | |
300 | carry += arg1[i] * arg2[j]; | |
301 | /* Since prod[p] < 0xFFFF, this sum <= 0xFFFFFFFF. */ | |
302 | carry += prod[k]; | |
303 | prod[k] = LOWPART (carry); | |
304 | carry = HIGHPART (carry); | |
305 | } | |
306 | prod[i + 4] = carry; | |
307 | } | |
2bc77e10 | 308 | |
bd5b3bce | 309 | decode (prod, lv, hv); /* This ignores prod[4] through prod[4*2-1] */ |
b9e999f0 | 310 | |
311 | /* Check for overflow by calculating the top half of the answer in full; | |
312 | it should agree with the low half's sign bit. */ | |
bd5b3bce | 313 | decode (prod+4, &toplow, &tophigh); |
b9e999f0 | 314 | if (h1 < 0) |
315 | { | |
316 | neg_double (l2, h2, &neglow, &neghigh); | |
317 | add_double (neglow, neghigh, toplow, tophigh, &toplow, &tophigh); | |
318 | } | |
319 | if (h2 < 0) | |
320 | { | |
321 | neg_double (l1, h1, &neglow, &neghigh); | |
322 | add_double (neglow, neghigh, toplow, tophigh, &toplow, &tophigh); | |
323 | } | |
324 | return (*hv < 0 ? ~(toplow & tophigh) : toplow | tophigh) != 0; | |
2bc77e10 | 325 | } |
326 | \f | |
b572011e | 327 | /* Shift the doubleword integer in L1, H1 left by COUNT places |
2bc77e10 | 328 | keeping only PREC bits of result. |
329 | Shift right if COUNT is negative. | |
330 | ARITH nonzero specifies arithmetic shifting; otherwise use logical shift. | |
b572011e | 331 | Store the value as two `HOST_WIDE_INT' pieces in *LV and *HV. */ |
2bc77e10 | 332 | |
f55401f0 | 333 | void |
2bc77e10 | 334 | lshift_double (l1, h1, count, prec, lv, hv, arith) |
bfd67d2c | 335 | HOST_WIDE_INT l1, h1, count; |
336 | int prec; | |
b572011e | 337 | HOST_WIDE_INT *lv, *hv; |
2bc77e10 | 338 | int arith; |
339 | { | |
2bc77e10 | 340 | if (count < 0) |
341 | { | |
342 | rshift_double (l1, h1, - count, prec, lv, hv, arith); | |
f55401f0 | 343 | return; |
2bc77e10 | 344 | } |
bd5b3bce | 345 | |
0bb60c65 | 346 | #ifdef SHIFT_COUNT_TRUNCATED |
347 | if (SHIFT_COUNT_TRUNCATED) | |
348 | count %= prec; | |
349 | #endif | |
2bc77e10 | 350 | |
bd5b3bce | 351 | if (count >= HOST_BITS_PER_WIDE_INT) |
2bc77e10 | 352 | { |
bd5b3bce | 353 | *hv = (unsigned HOST_WIDE_INT) l1 << count - HOST_BITS_PER_WIDE_INT; |
354 | *lv = 0; | |
355 | } | |
356 | else | |
357 | { | |
358 | *hv = (((unsigned HOST_WIDE_INT) h1 << count) | |
359 | | ((unsigned HOST_WIDE_INT) l1 >> HOST_BITS_PER_WIDE_INT - count - 1 >> 1)); | |
360 | *lv = (unsigned HOST_WIDE_INT) l1 << count; | |
2bc77e10 | 361 | } |
2bc77e10 | 362 | } |
363 | ||
b572011e | 364 | /* Shift the doubleword integer in L1, H1 right by COUNT places |
2bc77e10 | 365 | keeping only PREC bits of result. COUNT must be positive. |
366 | ARITH nonzero specifies arithmetic shifting; otherwise use logical shift. | |
b572011e | 367 | Store the value as two `HOST_WIDE_INT' pieces in *LV and *HV. */ |
2bc77e10 | 368 | |
369 | void | |
370 | rshift_double (l1, h1, count, prec, lv, hv, arith) | |
bfd67d2c | 371 | HOST_WIDE_INT l1, h1, count; |
372 | int prec; | |
b572011e | 373 | HOST_WIDE_INT *lv, *hv; |
2bc77e10 | 374 | int arith; |
375 | { | |
bd5b3bce | 376 | unsigned HOST_WIDE_INT signmask; |
377 | signmask = (arith | |
378 | ? -((unsigned HOST_WIDE_INT) h1 >> (HOST_BITS_PER_WIDE_INT - 1)) | |
379 | : 0); | |
2bc77e10 | 380 | |
0bb60c65 | 381 | #ifdef SHIFT_COUNT_TRUNCATED |
382 | if (SHIFT_COUNT_TRUNCATED) | |
383 | count %= prec; | |
384 | #endif | |
2bc77e10 | 385 | |
bd5b3bce | 386 | if (count >= HOST_BITS_PER_WIDE_INT) |
2bc77e10 | 387 | { |
bd5b3bce | 388 | *hv = signmask; |
389 | *lv = ((signmask << 2 * HOST_BITS_PER_WIDE_INT - count - 1 << 1) | |
390 | | ((unsigned HOST_WIDE_INT) h1 >> count - HOST_BITS_PER_WIDE_INT)); | |
391 | } | |
392 | else | |
393 | { | |
394 | *lv = (((unsigned HOST_WIDE_INT) l1 >> count) | |
395 | | ((unsigned HOST_WIDE_INT) h1 << HOST_BITS_PER_WIDE_INT - count - 1 << 1)); | |
396 | *hv = ((signmask << HOST_BITS_PER_WIDE_INT - count) | |
397 | | ((unsigned HOST_WIDE_INT) h1 >> count)); | |
2bc77e10 | 398 | } |
2bc77e10 | 399 | } |
400 | \f | |
bd5b3bce | 401 | /* Rotate the doubleword integer in L1, H1 left by COUNT places |
2bc77e10 | 402 | keeping only PREC bits of result. |
403 | Rotate right if COUNT is negative. | |
b572011e | 404 | Store the value as two `HOST_WIDE_INT' pieces in *LV and *HV. */ |
2bc77e10 | 405 | |
406 | void | |
407 | lrotate_double (l1, h1, count, prec, lv, hv) | |
bfd67d2c | 408 | HOST_WIDE_INT l1, h1, count; |
409 | int prec; | |
b572011e | 410 | HOST_WIDE_INT *lv, *hv; |
2bc77e10 | 411 | { |
7a1b56a9 | 412 | HOST_WIDE_INT s1l, s1h, s2l, s2h; |
2bc77e10 | 413 | |
7a1b56a9 | 414 | count %= prec; |
2bc77e10 | 415 | if (count < 0) |
7a1b56a9 | 416 | count += prec; |
2bc77e10 | 417 | |
7a1b56a9 | 418 | lshift_double (l1, h1, count, prec, &s1l, &s1h, 0); |
419 | rshift_double (l1, h1, prec - count, prec, &s2l, &s2h, 0); | |
420 | *lv = s1l | s2l; | |
421 | *hv = s1h | s2h; | |
2bc77e10 | 422 | } |
423 | ||
b572011e | 424 | /* Rotate the doubleword integer in L1, H1 left by COUNT places |
2bc77e10 | 425 | keeping only PREC bits of result. COUNT must be positive. |
b572011e | 426 | Store the value as two `HOST_WIDE_INT' pieces in *LV and *HV. */ |
2bc77e10 | 427 | |
428 | void | |
429 | rrotate_double (l1, h1, count, prec, lv, hv) | |
bfd67d2c | 430 | HOST_WIDE_INT l1, h1, count; |
431 | int prec; | |
b572011e | 432 | HOST_WIDE_INT *lv, *hv; |
2bc77e10 | 433 | { |
7a1b56a9 | 434 | HOST_WIDE_INT s1l, s1h, s2l, s2h; |
2bc77e10 | 435 | |
7a1b56a9 | 436 | count %= prec; |
437 | if (count < 0) | |
438 | count += prec; | |
2bc77e10 | 439 | |
7a1b56a9 | 440 | rshift_double (l1, h1, count, prec, &s1l, &s1h, 0); |
441 | lshift_double (l1, h1, prec - count, prec, &s2l, &s2h, 0); | |
442 | *lv = s1l | s2l; | |
443 | *hv = s1h | s2h; | |
2bc77e10 | 444 | } |
445 | \f | |
b572011e | 446 | /* Divide doubleword integer LNUM, HNUM by doubleword integer LDEN, HDEN |
2bc77e10 | 447 | for a quotient (stored in *LQUO, *HQUO) and remainder (in *LREM, *HREM). |
448 | CODE is a tree code for a kind of division, one of | |
449 | TRUNC_DIV_EXPR, FLOOR_DIV_EXPR, CEIL_DIV_EXPR, ROUND_DIV_EXPR | |
450 | or EXACT_DIV_EXPR | |
b9e999f0 | 451 | It controls how the quotient is rounded to a integer. |
452 | Return nonzero if the operation overflows. | |
2bc77e10 | 453 | UNS nonzero says do unsigned division. */ |
454 | ||
15ca565e | 455 | int |
2bc77e10 | 456 | div_and_round_double (code, uns, |
457 | lnum_orig, hnum_orig, lden_orig, hden_orig, | |
458 | lquo, hquo, lrem, hrem) | |
459 | enum tree_code code; | |
460 | int uns; | |
b572011e | 461 | HOST_WIDE_INT lnum_orig, hnum_orig; /* num == numerator == dividend */ |
462 | HOST_WIDE_INT lden_orig, hden_orig; /* den == denominator == divisor */ | |
463 | HOST_WIDE_INT *lquo, *hquo, *lrem, *hrem; | |
2bc77e10 | 464 | { |
465 | int quo_neg = 0; | |
bd5b3bce | 466 | HOST_WIDE_INT num[4 + 1]; /* extra element for scaling. */ |
467 | HOST_WIDE_INT den[4], quo[4]; | |
468 | register int i, j; | |
469 | unsigned HOST_WIDE_INT work; | |
969c62c6 | 470 | register unsigned HOST_WIDE_INT carry = 0; |
802ddb63 | 471 | HOST_WIDE_INT lnum = lnum_orig; |
abd9ac9c | 472 | HOST_WIDE_INT hnum = hnum_orig; |
802ddb63 | 473 | HOST_WIDE_INT lden = lden_orig; |
abd9ac9c | 474 | HOST_WIDE_INT hden = hden_orig; |
b9e999f0 | 475 | int overflow = 0; |
2bc77e10 | 476 | |
477 | if ((hden == 0) && (lden == 0)) | |
478 | abort (); | |
479 | ||
480 | /* calculate quotient sign and convert operands to unsigned. */ | |
481 | if (!uns) | |
482 | { | |
b9e999f0 | 483 | if (hnum < 0) |
2bc77e10 | 484 | { |
485 | quo_neg = ~ quo_neg; | |
b9e999f0 | 486 | /* (minimum integer) / (-1) is the only overflow case. */ |
487 | if (neg_double (lnum, hnum, &lnum, &hnum) && (lden & hden) == -1) | |
488 | overflow = 1; | |
2bc77e10 | 489 | } |
b9e999f0 | 490 | if (hden < 0) |
2bc77e10 | 491 | { |
492 | quo_neg = ~ quo_neg; | |
b9e999f0 | 493 | neg_double (lden, hden, &lden, &hden); |
2bc77e10 | 494 | } |
495 | } | |
496 | ||
497 | if (hnum == 0 && hden == 0) | |
498 | { /* single precision */ | |
499 | *hquo = *hrem = 0; | |
802ddb63 | 500 | /* This unsigned division rounds toward zero. */ |
501 | *lquo = lnum / (unsigned HOST_WIDE_INT) lden; | |
2bc77e10 | 502 | goto finish_up; |
503 | } | |
504 | ||
505 | if (hnum == 0) | |
506 | { /* trivial case: dividend < divisor */ | |
507 | /* hden != 0 already checked. */ | |
508 | *hquo = *lquo = 0; | |
509 | *hrem = hnum; | |
510 | *lrem = lnum; | |
511 | goto finish_up; | |
512 | } | |
513 | ||
748e6d74 | 514 | bzero ((char *) quo, sizeof quo); |
2bc77e10 | 515 | |
748e6d74 | 516 | bzero ((char *) num, sizeof num); /* to zero 9th element */ |
517 | bzero ((char *) den, sizeof den); | |
2bc77e10 | 518 | |
519 | encode (num, lnum, hnum); | |
520 | encode (den, lden, hden); | |
521 | ||
bd5b3bce | 522 | /* Special code for when the divisor < BASE. */ |
523 | if (hden == 0 && lden < BASE) | |
524 | { | |
2bc77e10 | 525 | /* hnum != 0 already checked. */ |
bd5b3bce | 526 | for (i = 4 - 1; i >= 0; i--) |
2bc77e10 | 527 | { |
bd5b3bce | 528 | work = num[i] + carry * BASE; |
802ddb63 | 529 | quo[i] = work / (unsigned HOST_WIDE_INT) lden; |
530 | carry = work % (unsigned HOST_WIDE_INT) lden; | |
2bc77e10 | 531 | } |
532 | } | |
bd5b3bce | 533 | else |
534 | { | |
535 | /* Full double precision division, | |
536 | with thanks to Don Knuth's "Seminumerical Algorithms". */ | |
969c62c6 | 537 | int num_hi_sig, den_hi_sig; |
538 | unsigned HOST_WIDE_INT quo_est, scale; | |
2bc77e10 | 539 | |
540 | /* Find the highest non-zero divisor digit. */ | |
bd5b3bce | 541 | for (i = 4 - 1; ; i--) |
2bc77e10 | 542 | if (den[i] != 0) { |
543 | den_hi_sig = i; | |
544 | break; | |
545 | } | |
2bc77e10 | 546 | |
547 | /* Insure that the first digit of the divisor is at least BASE/2. | |
548 | This is required by the quotient digit estimation algorithm. */ | |
549 | ||
550 | scale = BASE / (den[den_hi_sig] + 1); | |
551 | if (scale > 1) { /* scale divisor and dividend */ | |
552 | carry = 0; | |
bd5b3bce | 553 | for (i = 0; i <= 4 - 1; i++) { |
2bc77e10 | 554 | work = (num[i] * scale) + carry; |
bd5b3bce | 555 | num[i] = LOWPART (work); |
556 | carry = HIGHPART (work); | |
557 | } num[4] = carry; | |
2bc77e10 | 558 | carry = 0; |
bd5b3bce | 559 | for (i = 0; i <= 4 - 1; i++) { |
2bc77e10 | 560 | work = (den[i] * scale) + carry; |
bd5b3bce | 561 | den[i] = LOWPART (work); |
562 | carry = HIGHPART (work); | |
2bc77e10 | 563 | if (den[i] != 0) den_hi_sig = i; |
564 | } | |
565 | } | |
566 | ||
bd5b3bce | 567 | num_hi_sig = 4; |
568 | ||
2bc77e10 | 569 | /* Main loop */ |
bd5b3bce | 570 | for (i = num_hi_sig - den_hi_sig - 1; i >= 0; i--) { |
4bbea254 | 571 | /* guess the next quotient digit, quo_est, by dividing the first |
2bc77e10 | 572 | two remaining dividend digits by the high order quotient digit. |
573 | quo_est is never low and is at most 2 high. */ | |
bd5b3bce | 574 | unsigned HOST_WIDE_INT tmp; |
2bc77e10 | 575 | |
bd5b3bce | 576 | num_hi_sig = i + den_hi_sig + 1; |
577 | work = num[num_hi_sig] * BASE + num[num_hi_sig - 1]; | |
578 | if (num[num_hi_sig] != den[den_hi_sig]) | |
2bc77e10 | 579 | quo_est = work / den[den_hi_sig]; |
bd5b3bce | 580 | else |
2bc77e10 | 581 | quo_est = BASE - 1; |
2bc77e10 | 582 | |
583 | /* refine quo_est so it's usually correct, and at most one high. */ | |
bd5b3bce | 584 | tmp = work - quo_est * den[den_hi_sig]; |
585 | if (tmp < BASE | |
586 | && den[den_hi_sig - 1] * quo_est > (tmp * BASE + num[num_hi_sig - 2])) | |
2bc77e10 | 587 | quo_est--; |
588 | ||
589 | /* Try QUO_EST as the quotient digit, by multiplying the | |
590 | divisor by QUO_EST and subtracting from the remaining dividend. | |
591 | Keep in mind that QUO_EST is the I - 1st digit. */ | |
592 | ||
593 | carry = 0; | |
2bc77e10 | 594 | for (j = 0; j <= den_hi_sig; j++) |
595 | { | |
bd5b3bce | 596 | work = quo_est * den[j] + carry; |
597 | carry = HIGHPART (work); | |
598 | work = num[i + j] - LOWPART (work); | |
599 | num[i + j] = LOWPART (work); | |
600 | carry += HIGHPART (work) != 0; | |
2bc77e10 | 601 | } |
602 | ||
603 | /* if quo_est was high by one, then num[i] went negative and | |
604 | we need to correct things. */ | |
605 | ||
bd5b3bce | 606 | if (num[num_hi_sig] < carry) |
2bc77e10 | 607 | { |
608 | quo_est--; | |
609 | carry = 0; /* add divisor back in */ | |
610 | for (j = 0; j <= den_hi_sig; j++) | |
611 | { | |
bd5b3bce | 612 | work = num[i + j] + den[j] + carry; |
613 | carry = HIGHPART (work); | |
614 | num[i + j] = LOWPART (work); | |
2bc77e10 | 615 | } |
bd5b3bce | 616 | num [num_hi_sig] += carry; |
2bc77e10 | 617 | } |
618 | ||
619 | /* store the quotient digit. */ | |
bd5b3bce | 620 | quo[i] = quo_est; |
2bc77e10 | 621 | } |
622 | } | |
623 | ||
624 | decode (quo, lquo, hquo); | |
625 | ||
626 | finish_up: | |
627 | /* if result is negative, make it so. */ | |
628 | if (quo_neg) | |
629 | neg_double (*lquo, *hquo, lquo, hquo); | |
630 | ||
631 | /* compute trial remainder: rem = num - (quo * den) */ | |
632 | mul_double (*lquo, *hquo, lden_orig, hden_orig, lrem, hrem); | |
633 | neg_double (*lrem, *hrem, lrem, hrem); | |
634 | add_double (lnum_orig, hnum_orig, *lrem, *hrem, lrem, hrem); | |
635 | ||
636 | switch (code) | |
637 | { | |
638 | case TRUNC_DIV_EXPR: | |
639 | case TRUNC_MOD_EXPR: /* round toward zero */ | |
640 | case EXACT_DIV_EXPR: /* for this one, it shouldn't matter */ | |
b9e999f0 | 641 | return overflow; |
2bc77e10 | 642 | |
643 | case FLOOR_DIV_EXPR: | |
644 | case FLOOR_MOD_EXPR: /* round toward negative infinity */ | |
645 | if (quo_neg && (*lrem != 0 || *hrem != 0)) /* ratio < 0 && rem != 0 */ | |
646 | { | |
647 | /* quo = quo - 1; */ | |
b572011e | 648 | add_double (*lquo, *hquo, (HOST_WIDE_INT) -1, (HOST_WIDE_INT) -1, |
649 | lquo, hquo); | |
2bc77e10 | 650 | } |
b9e999f0 | 651 | else return overflow; |
2bc77e10 | 652 | break; |
653 | ||
654 | case CEIL_DIV_EXPR: | |
655 | case CEIL_MOD_EXPR: /* round toward positive infinity */ | |
656 | if (!quo_neg && (*lrem != 0 || *hrem != 0)) /* ratio > 0 && rem != 0 */ | |
657 | { | |
b572011e | 658 | add_double (*lquo, *hquo, (HOST_WIDE_INT) 1, (HOST_WIDE_INT) 0, |
659 | lquo, hquo); | |
2bc77e10 | 660 | } |
b9e999f0 | 661 | else return overflow; |
2bc77e10 | 662 | break; |
663 | ||
664 | case ROUND_DIV_EXPR: | |
665 | case ROUND_MOD_EXPR: /* round to closest integer */ | |
666 | { | |
b572011e | 667 | HOST_WIDE_INT labs_rem = *lrem, habs_rem = *hrem; |
668 | HOST_WIDE_INT labs_den = lden, habs_den = hden, ltwice, htwice; | |
2bc77e10 | 669 | |
670 | /* get absolute values */ | |
671 | if (*hrem < 0) neg_double (*lrem, *hrem, &labs_rem, &habs_rem); | |
672 | if (hden < 0) neg_double (lden, hden, &labs_den, &habs_den); | |
673 | ||
674 | /* if (2 * abs (lrem) >= abs (lden)) */ | |
b572011e | 675 | mul_double ((HOST_WIDE_INT) 2, (HOST_WIDE_INT) 0, |
676 | labs_rem, habs_rem, <wice, &htwice); | |
677 | if (((unsigned HOST_WIDE_INT) habs_den | |
678 | < (unsigned HOST_WIDE_INT) htwice) | |
679 | || (((unsigned HOST_WIDE_INT) habs_den | |
680 | == (unsigned HOST_WIDE_INT) htwice) | |
681 | && ((HOST_WIDE_INT unsigned) labs_den | |
682 | < (unsigned HOST_WIDE_INT) ltwice))) | |
2bc77e10 | 683 | { |
684 | if (*hquo < 0) | |
685 | /* quo = quo - 1; */ | |
b572011e | 686 | add_double (*lquo, *hquo, |
687 | (HOST_WIDE_INT) -1, (HOST_WIDE_INT) -1, lquo, hquo); | |
2bc77e10 | 688 | else |
689 | /* quo = quo + 1; */ | |
b572011e | 690 | add_double (*lquo, *hquo, (HOST_WIDE_INT) 1, (HOST_WIDE_INT) 0, |
691 | lquo, hquo); | |
2bc77e10 | 692 | } |
b9e999f0 | 693 | else return overflow; |
2bc77e10 | 694 | } |
695 | break; | |
696 | ||
697 | default: | |
698 | abort (); | |
699 | } | |
700 | ||
701 | /* compute true remainder: rem = num - (quo * den) */ | |
702 | mul_double (*lquo, *hquo, lden_orig, hden_orig, lrem, hrem); | |
703 | neg_double (*lrem, *hrem, lrem, hrem); | |
704 | add_double (lnum_orig, hnum_orig, *lrem, *hrem, lrem, hrem); | |
b9e999f0 | 705 | return overflow; |
2bc77e10 | 706 | } |
707 | \f | |
64775a93 | 708 | #ifndef REAL_ARITHMETIC |
23fed9b2 | 709 | /* Effectively truncate a real value to represent the nearest possible value |
710 | in a narrower mode. The result is actually represented in the same data | |
711 | type as the argument, but its value is usually different. | |
712 | ||
713 | A trap may occur during the FP operations and it is the responsibility | |
714 | of the calling function to have a handler established. */ | |
2e756f7d | 715 | |
716 | REAL_VALUE_TYPE | |
717 | real_value_truncate (mode, arg) | |
718 | enum machine_mode mode; | |
719 | REAL_VALUE_TYPE arg; | |
720 | { | |
23fed9b2 | 721 | return REAL_VALUE_TRUNCATE (mode, arg); |
2e756f7d | 722 | } |
723 | ||
2bc77e10 | 724 | #if TARGET_FLOAT_FORMAT == IEEE_FLOAT_FORMAT |
725 | ||
726 | /* Check for infinity in an IEEE double precision number. */ | |
727 | ||
728 | int | |
729 | target_isinf (x) | |
730 | REAL_VALUE_TYPE x; | |
731 | { | |
732 | /* The IEEE 64-bit double format. */ | |
733 | union { | |
734 | REAL_VALUE_TYPE d; | |
735 | struct { | |
736 | unsigned sign : 1; | |
737 | unsigned exponent : 11; | |
738 | unsigned mantissa1 : 20; | |
739 | unsigned mantissa2; | |
740 | } little_endian; | |
741 | struct { | |
742 | unsigned mantissa2; | |
743 | unsigned mantissa1 : 20; | |
744 | unsigned exponent : 11; | |
745 | unsigned sign : 1; | |
746 | } big_endian; | |
747 | } u; | |
748 | ||
749 | u.d = dconstm1; | |
750 | if (u.big_endian.sign == 1) | |
751 | { | |
752 | u.d = x; | |
753 | return (u.big_endian.exponent == 2047 | |
754 | && u.big_endian.mantissa1 == 0 | |
755 | && u.big_endian.mantissa2 == 0); | |
756 | } | |
757 | else | |
758 | { | |
759 | u.d = x; | |
760 | return (u.little_endian.exponent == 2047 | |
761 | && u.little_endian.mantissa1 == 0 | |
762 | && u.little_endian.mantissa2 == 0); | |
763 | } | |
764 | } | |
765 | ||
288663fd | 766 | /* Check whether an IEEE double precision number is a NaN. */ |
767 | ||
768 | int | |
769 | target_isnan (x) | |
770 | REAL_VALUE_TYPE x; | |
771 | { | |
772 | /* The IEEE 64-bit double format. */ | |
773 | union { | |
774 | REAL_VALUE_TYPE d; | |
775 | struct { | |
776 | unsigned sign : 1; | |
777 | unsigned exponent : 11; | |
778 | unsigned mantissa1 : 20; | |
779 | unsigned mantissa2; | |
780 | } little_endian; | |
781 | struct { | |
782 | unsigned mantissa2; | |
783 | unsigned mantissa1 : 20; | |
784 | unsigned exponent : 11; | |
785 | unsigned sign : 1; | |
786 | } big_endian; | |
787 | } u; | |
788 | ||
789 | u.d = dconstm1; | |
790 | if (u.big_endian.sign == 1) | |
791 | { | |
792 | u.d = x; | |
793 | return (u.big_endian.exponent == 2047 | |
794 | && (u.big_endian.mantissa1 != 0 | |
795 | || u.big_endian.mantissa2 != 0)); | |
796 | } | |
797 | else | |
798 | { | |
799 | u.d = x; | |
800 | return (u.little_endian.exponent == 2047 | |
801 | && (u.little_endian.mantissa1 != 0 | |
802 | || u.little_endian.mantissa2 != 0)); | |
803 | } | |
804 | } | |
805 | ||
e233264a | 806 | /* Check for a negative IEEE double precision number. */ |
2bc77e10 | 807 | |
808 | int | |
e233264a | 809 | target_negative (x) |
2bc77e10 | 810 | REAL_VALUE_TYPE x; |
811 | { | |
e233264a | 812 | /* The IEEE 64-bit double format. */ |
813 | union { | |
814 | REAL_VALUE_TYPE d; | |
815 | struct { | |
816 | unsigned sign : 1; | |
817 | unsigned exponent : 11; | |
818 | unsigned mantissa1 : 20; | |
819 | unsigned mantissa2; | |
820 | } little_endian; | |
821 | struct { | |
822 | unsigned mantissa2; | |
823 | unsigned mantissa1 : 20; | |
824 | unsigned exponent : 11; | |
825 | unsigned sign : 1; | |
826 | } big_endian; | |
827 | } u; | |
2bc77e10 | 828 | |
e233264a | 829 | u.d = dconstm1; |
830 | if (u.big_endian.sign == 1) | |
831 | { | |
832 | u.d = x; | |
833 | return u.big_endian.sign; | |
834 | } | |
835 | else | |
836 | { | |
837 | u.d = x; | |
838 | return u.little_endian.sign; | |
839 | } | |
2bc77e10 | 840 | } |
841 | #else /* Target not IEEE */ | |
842 | ||
843 | /* Let's assume other float formats don't have infinity. | |
844 | (This can be overridden by redefining REAL_VALUE_ISINF.) */ | |
845 | ||
846 | target_isinf (x) | |
847 | REAL_VALUE_TYPE x; | |
848 | { | |
849 | return 0; | |
850 | } | |
851 | ||
288663fd | 852 | /* Let's assume other float formats don't have NaNs. |
853 | (This can be overridden by redefining REAL_VALUE_ISNAN.) */ | |
854 | ||
855 | target_isnan (x) | |
856 | REAL_VALUE_TYPE x; | |
857 | { | |
858 | return 0; | |
859 | } | |
860 | ||
2bc77e10 | 861 | /* Let's assume other float formats don't have minus zero. |
e233264a | 862 | (This can be overridden by redefining REAL_VALUE_NEGATIVE.) */ |
2bc77e10 | 863 | |
e233264a | 864 | target_negative (x) |
2bc77e10 | 865 | REAL_VALUE_TYPE x; |
866 | { | |
e233264a | 867 | return x < 0; |
2bc77e10 | 868 | } |
869 | #endif /* Target not IEEE */ | |
88181ec5 | 870 | |
871 | /* Try to change R into its exact multiplicative inverse in machine mode | |
872 | MODE. Return nonzero function value if successful. */ | |
873 | ||
874 | int | |
875 | exact_real_inverse (mode, r) | |
876 | enum machine_mode mode; | |
877 | REAL_VALUE_TYPE *r; | |
878 | { | |
879 | union | |
880 | { | |
881 | double d; | |
882 | unsigned short i[4]; | |
883 | }x, t, y; | |
884 | int i; | |
885 | ||
886 | /* Usually disable if bounds checks are not reliable. */ | |
887 | if ((HOST_FLOAT_FORMAT != TARGET_FLOAT_FORMAT) && !flag_pretend_float) | |
888 | return 0; | |
889 | ||
890 | /* Set array index to the less significant bits in the unions, depending | |
891 | on the endian-ness of the host doubles. | |
892 | Disable if insufficient information on the data structure. */ | |
893 | #if HOST_FLOAT_FORMAT == UNKNOWN_FLOAT_FORMAT | |
894 | return 0; | |
895 | #else | |
896 | #if HOST_FLOAT_FORMAT == VAX_FLOAT_FORMAT | |
897 | #define K 2 | |
898 | #else | |
899 | #if HOST_FLOAT_FORMAT == IBM_FLOAT_FORMAT | |
900 | #define K 2 | |
901 | #else | |
902 | #define K (2 * HOST_FLOAT_WORDS_BIG_ENDIAN) | |
903 | #endif | |
904 | #endif | |
905 | #endif | |
906 | ||
907 | if (setjmp (float_error)) | |
908 | { | |
909 | /* Don't do the optimization if there was an arithmetic error. */ | |
910 | fail: | |
911 | set_float_handler (NULL_PTR); | |
912 | return 0; | |
913 | } | |
914 | set_float_handler (float_error); | |
915 | ||
916 | /* Domain check the argument. */ | |
917 | x.d = *r; | |
918 | if (x.d == 0.0) | |
919 | goto fail; | |
920 | ||
921 | #ifdef REAL_INFINITY | |
922 | if (REAL_VALUE_ISINF (x.d) || REAL_VALUE_ISNAN (x.d)) | |
923 | goto fail; | |
924 | #endif | |
925 | ||
926 | /* Compute the reciprocal and check for numerical exactness. | |
927 | It is unnecessary to check all the significand bits to determine | |
928 | whether X is a power of 2. If X is not, then it is impossible for | |
929 | the bottom half significand of both X and 1/X to be all zero bits. | |
930 | Hence we ignore the data structure of the top half and examine only | |
931 | the low order bits of the two significands. */ | |
932 | t.d = 1.0 / x.d; | |
933 | if (x.i[K] != 0 || x.i[K + 1] != 0 || t.i[K] != 0 || t.i[K + 1] != 0) | |
934 | goto fail; | |
935 | ||
936 | /* Truncate to the required mode and range-check the result. */ | |
937 | y.d = REAL_VALUE_TRUNCATE (mode, t.d); | |
938 | #ifdef CHECK_FLOAT_VALUE | |
939 | i = 0; | |
940 | if (CHECK_FLOAT_VALUE (mode, y.d, i)) | |
941 | goto fail; | |
942 | #endif | |
943 | ||
944 | /* Fail if truncation changed the value. */ | |
945 | if (y.d != t.d || y.d == 0.0) | |
946 | goto fail; | |
947 | ||
948 | #ifdef REAL_INFINITY | |
949 | if (REAL_VALUE_ISINF (y.d) || REAL_VALUE_ISNAN (y.d)) | |
950 | goto fail; | |
951 | #endif | |
952 | ||
953 | /* Output the reciprocal and return success flag. */ | |
954 | set_float_handler (NULL_PTR); | |
955 | *r = y.d; | |
956 | return 1; | |
957 | } | |
64775a93 | 958 | #endif /* no REAL_ARITHMETIC */ |
2bc77e10 | 959 | \f |
960 | /* Split a tree IN into a constant and a variable part | |
961 | that could be combined with CODE to make IN. | |
962 | CODE must be a commutative arithmetic operation. | |
963 | Store the constant part into *CONP and the variable in &VARP. | |
964 | Return 1 if this was done; zero means the tree IN did not decompose | |
965 | this way. | |
966 | ||
967 | If CODE is PLUS_EXPR we also split trees that use MINUS_EXPR. | |
968 | Therefore, we must tell the caller whether the variable part | |
969 | was subtracted. We do this by storing 1 or -1 into *VARSIGNP. | |
970 | The value stored is the coefficient for the variable term. | |
971 | The constant term we return should always be added; | |
972 | we negate it if necessary. */ | |
973 | ||
974 | static int | |
975 | split_tree (in, code, varp, conp, varsignp) | |
976 | tree in; | |
977 | enum tree_code code; | |
978 | tree *varp, *conp; | |
979 | int *varsignp; | |
980 | { | |
981 | register tree outtype = TREE_TYPE (in); | |
982 | *varp = 0; | |
983 | *conp = 0; | |
984 | ||
985 | /* Strip any conversions that don't change the machine mode. */ | |
986 | while ((TREE_CODE (in) == NOP_EXPR | |
987 | || TREE_CODE (in) == CONVERT_EXPR) | |
988 | && (TYPE_MODE (TREE_TYPE (in)) | |
989 | == TYPE_MODE (TREE_TYPE (TREE_OPERAND (in, 0))))) | |
990 | in = TREE_OPERAND (in, 0); | |
991 | ||
992 | if (TREE_CODE (in) == code | |
780a4395 | 993 | || (! FLOAT_TYPE_P (TREE_TYPE (in)) |
2bc77e10 | 994 | /* We can associate addition and subtraction together |
995 | (even though the C standard doesn't say so) | |
996 | for integers because the value is not affected. | |
997 | For reals, the value might be affected, so we can't. */ | |
780a4395 | 998 | && ((code == PLUS_EXPR && TREE_CODE (in) == MINUS_EXPR) |
999 | || (code == MINUS_EXPR && TREE_CODE (in) == PLUS_EXPR)))) | |
2bc77e10 | 1000 | { |
1001 | enum tree_code code = TREE_CODE (TREE_OPERAND (in, 0)); | |
1002 | if (code == INTEGER_CST) | |
1003 | { | |
1004 | *conp = TREE_OPERAND (in, 0); | |
1005 | *varp = TREE_OPERAND (in, 1); | |
1006 | if (TYPE_MODE (TREE_TYPE (*varp)) != TYPE_MODE (outtype) | |
1007 | && TREE_TYPE (*varp) != outtype) | |
1008 | *varp = convert (outtype, *varp); | |
1009 | *varsignp = (TREE_CODE (in) == MINUS_EXPR) ? -1 : 1; | |
1010 | return 1; | |
1011 | } | |
1012 | if (TREE_CONSTANT (TREE_OPERAND (in, 1))) | |
1013 | { | |
1014 | *conp = TREE_OPERAND (in, 1); | |
1015 | *varp = TREE_OPERAND (in, 0); | |
1016 | *varsignp = 1; | |
1017 | if (TYPE_MODE (TREE_TYPE (*varp)) != TYPE_MODE (outtype) | |
1018 | && TREE_TYPE (*varp) != outtype) | |
1019 | *varp = convert (outtype, *varp); | |
1020 | if (TREE_CODE (in) == MINUS_EXPR) | |
1021 | { | |
1022 | /* If operation is subtraction and constant is second, | |
1023 | must negate it to get an additive constant. | |
1024 | And this cannot be done unless it is a manifest constant. | |
1025 | It could also be the address of a static variable. | |
1026 | We cannot negate that, so give up. */ | |
1027 | if (TREE_CODE (*conp) == INTEGER_CST) | |
1028 | /* Subtracting from integer_zero_node loses for long long. */ | |
1029 | *conp = fold (build1 (NEGATE_EXPR, TREE_TYPE (*conp), *conp)); | |
1030 | else | |
1031 | return 0; | |
1032 | } | |
1033 | return 1; | |
1034 | } | |
1035 | if (TREE_CONSTANT (TREE_OPERAND (in, 0))) | |
1036 | { | |
1037 | *conp = TREE_OPERAND (in, 0); | |
1038 | *varp = TREE_OPERAND (in, 1); | |
1039 | if (TYPE_MODE (TREE_TYPE (*varp)) != TYPE_MODE (outtype) | |
1040 | && TREE_TYPE (*varp) != outtype) | |
1041 | *varp = convert (outtype, *varp); | |
1042 | *varsignp = (TREE_CODE (in) == MINUS_EXPR) ? -1 : 1; | |
1043 | return 1; | |
1044 | } | |
1045 | } | |
1046 | return 0; | |
1047 | } | |
1048 | \f | |
bb8d2b8d | 1049 | /* Combine two constants ARG1 and ARG2 under operation CODE |
2bc77e10 | 1050 | to produce a new constant. |
1051 | We assume ARG1 and ARG2 have the same data type, | |
5485823f | 1052 | or at least are the same kind of constant and the same machine mode. |
1053 | ||
1054 | If NOTRUNC is nonzero, do not truncate the result to fit the data type. */ | |
2bc77e10 | 1055 | |
2bc77e10 | 1056 | static tree |
5485823f | 1057 | const_binop (code, arg1, arg2, notrunc) |
2bc77e10 | 1058 | enum tree_code code; |
1059 | register tree arg1, arg2; | |
5485823f | 1060 | int notrunc; |
2bc77e10 | 1061 | { |
4a2064d7 | 1062 | STRIP_NOPS (arg1); STRIP_NOPS (arg2); |
1063 | ||
2bc77e10 | 1064 | if (TREE_CODE (arg1) == INTEGER_CST) |
1065 | { | |
b572011e | 1066 | register HOST_WIDE_INT int1l = TREE_INT_CST_LOW (arg1); |
1067 | register HOST_WIDE_INT int1h = TREE_INT_CST_HIGH (arg1); | |
1068 | HOST_WIDE_INT int2l = TREE_INT_CST_LOW (arg2); | |
1069 | HOST_WIDE_INT int2h = TREE_INT_CST_HIGH (arg2); | |
1070 | HOST_WIDE_INT low, hi; | |
1071 | HOST_WIDE_INT garbagel, garbageh; | |
2bc77e10 | 1072 | register tree t; |
1073 | int uns = TREE_UNSIGNED (TREE_TYPE (arg1)); | |
f55401f0 | 1074 | int overflow = 0; |
2bc77e10 | 1075 | |
1076 | switch (code) | |
1077 | { | |
1078 | case BIT_IOR_EXPR: | |
1079 | t = build_int_2 (int1l | int2l, int1h | int2h); | |
1080 | break; | |
1081 | ||
1082 | case BIT_XOR_EXPR: | |
1083 | t = build_int_2 (int1l ^ int2l, int1h ^ int2h); | |
1084 | break; | |
1085 | ||
1086 | case BIT_AND_EXPR: | |
1087 | t = build_int_2 (int1l & int2l, int1h & int2h); | |
1088 | break; | |
1089 | ||
1090 | case BIT_ANDTC_EXPR: | |
1091 | t = build_int_2 (int1l & ~int2l, int1h & ~int2h); | |
1092 | break; | |
1093 | ||
1094 | case RSHIFT_EXPR: | |
1095 | int2l = - int2l; | |
1096 | case LSHIFT_EXPR: | |
f55401f0 | 1097 | /* It's unclear from the C standard whether shifts can overflow. |
1098 | The following code ignores overflow; perhaps a C standard | |
1099 | interpretation ruling is needed. */ | |
1100 | lshift_double (int1l, int1h, int2l, | |
1101 | TYPE_PRECISION (TREE_TYPE (arg1)), | |
1102 | &low, &hi, | |
1103 | !uns); | |
2bc77e10 | 1104 | t = build_int_2 (low, hi); |
4c1f9523 | 1105 | TREE_TYPE (t) = TREE_TYPE (arg1); |
1106 | if (!notrunc) | |
1107 | force_fit_type (t, 0); | |
f17f1965 | 1108 | TREE_OVERFLOW (t) = TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2); |
4c1f9523 | 1109 | TREE_CONSTANT_OVERFLOW (t) |
1110 | = TREE_CONSTANT_OVERFLOW (arg1) | TREE_CONSTANT_OVERFLOW (arg2); | |
1111 | return t; | |
2bc77e10 | 1112 | |
1113 | case RROTATE_EXPR: | |
1114 | int2l = - int2l; | |
1115 | case LROTATE_EXPR: | |
1116 | lrotate_double (int1l, int1h, int2l, | |
1117 | TYPE_PRECISION (TREE_TYPE (arg1)), | |
1118 | &low, &hi); | |
1119 | t = build_int_2 (low, hi); | |
1120 | break; | |
1121 | ||
1122 | case PLUS_EXPR: | |
1123 | if (int1h == 0) | |
1124 | { | |
1125 | int2l += int1l; | |
b572011e | 1126 | if ((unsigned HOST_WIDE_INT) int2l < int1l) |
b9e999f0 | 1127 | { |
1128 | hi = int2h++; | |
f55401f0 | 1129 | overflow = int2h < hi; |
b9e999f0 | 1130 | } |
2bc77e10 | 1131 | t = build_int_2 (int2l, int2h); |
1132 | break; | |
1133 | } | |
1134 | if (int2h == 0) | |
1135 | { | |
1136 | int1l += int2l; | |
b572011e | 1137 | if ((unsigned HOST_WIDE_INT) int1l < int2l) |
b9e999f0 | 1138 | { |
1139 | hi = int1h++; | |
f55401f0 | 1140 | overflow = int1h < hi; |
b9e999f0 | 1141 | } |
2bc77e10 | 1142 | t = build_int_2 (int1l, int1h); |
1143 | break; | |
1144 | } | |
b9e999f0 | 1145 | overflow = add_double (int1l, int1h, int2l, int2h, &low, &hi); |
2bc77e10 | 1146 | t = build_int_2 (low, hi); |
1147 | break; | |
1148 | ||
1149 | case MINUS_EXPR: | |
1150 | if (int2h == 0 && int2l == 0) | |
1151 | { | |
1152 | t = build_int_2 (int1l, int1h); | |
1153 | break; | |
1154 | } | |
b9e999f0 | 1155 | neg_double (int2l, int2h, &low, &hi); |
1156 | add_double (int1l, int1h, low, hi, &low, &hi); | |
1157 | overflow = overflow_sum_sign (hi, int2h, int1h); | |
2bc77e10 | 1158 | t = build_int_2 (low, hi); |
1159 | break; | |
1160 | ||
1161 | case MULT_EXPR: | |
b9e999f0 | 1162 | overflow = mul_double (int1l, int1h, int2l, int2h, &low, &hi); |
2bc77e10 | 1163 | t = build_int_2 (low, hi); |
1164 | break; | |
1165 | ||
1166 | case TRUNC_DIV_EXPR: | |
1167 | case FLOOR_DIV_EXPR: case CEIL_DIV_EXPR: | |
1168 | case EXACT_DIV_EXPR: | |
1169 | /* This is a shortcut for a common special case. | |
1170 | It reduces the number of tree nodes generated | |
1171 | and saves time. */ | |
1172 | if (int2h == 0 && int2l > 0 | |
1173 | && TREE_TYPE (arg1) == sizetype | |
1174 | && int1h == 0 && int1l >= 0) | |
1175 | { | |
1176 | if (code == CEIL_DIV_EXPR) | |
1177 | int1l += int2l-1; | |
1178 | return size_int (int1l / int2l); | |
1179 | } | |
1180 | case ROUND_DIV_EXPR: | |
1181 | if (int2h == 0 && int2l == 1) | |
1182 | { | |
1183 | t = build_int_2 (int1l, int1h); | |
1184 | break; | |
1185 | } | |
1186 | if (int1l == int2l && int1h == int2h) | |
1187 | { | |
1188 | if ((int1l | int1h) == 0) | |
1189 | abort (); | |
1190 | t = build_int_2 (1, 0); | |
1191 | break; | |
1192 | } | |
b9e999f0 | 1193 | overflow = div_and_round_double (code, uns, |
1194 | int1l, int1h, int2l, int2h, | |
1195 | &low, &hi, &garbagel, &garbageh); | |
2bc77e10 | 1196 | t = build_int_2 (low, hi); |
1197 | break; | |
1198 | ||
1199 | case TRUNC_MOD_EXPR: case ROUND_MOD_EXPR: | |
1200 | case FLOOR_MOD_EXPR: case CEIL_MOD_EXPR: | |
b9e999f0 | 1201 | overflow = div_and_round_double (code, uns, |
1202 | int1l, int1h, int2l, int2h, | |
1203 | &garbagel, &garbageh, &low, &hi); | |
2bc77e10 | 1204 | t = build_int_2 (low, hi); |
1205 | break; | |
1206 | ||
1207 | case MIN_EXPR: | |
1208 | case MAX_EXPR: | |
1209 | if (uns) | |
1210 | { | |
b572011e | 1211 | low = (((unsigned HOST_WIDE_INT) int1h |
1212 | < (unsigned HOST_WIDE_INT) int2h) | |
1213 | || (((unsigned HOST_WIDE_INT) int1h | |
1214 | == (unsigned HOST_WIDE_INT) int2h) | |
1215 | && ((unsigned HOST_WIDE_INT) int1l | |
1216 | < (unsigned HOST_WIDE_INT) int2l))); | |
2bc77e10 | 1217 | } |
1218 | else | |
1219 | { | |
1220 | low = ((int1h < int2h) | |
1221 | || ((int1h == int2h) | |
b572011e | 1222 | && ((unsigned HOST_WIDE_INT) int1l |
1223 | < (unsigned HOST_WIDE_INT) int2l))); | |
2bc77e10 | 1224 | } |
1225 | if (low == (code == MIN_EXPR)) | |
1226 | t = build_int_2 (int1l, int1h); | |
1227 | else | |
1228 | t = build_int_2 (int2l, int2h); | |
1229 | break; | |
1230 | ||
1231 | default: | |
1232 | abort (); | |
1233 | } | |
1234 | got_it: | |
1235 | TREE_TYPE (t) = TREE_TYPE (arg1); | |
f17f1965 | 1236 | TREE_OVERFLOW (t) |
9430cdca | 1237 | = ((notrunc ? !uns && overflow : force_fit_type (t, overflow && !uns)) |
f17f1965 | 1238 | | TREE_OVERFLOW (arg1) |
1239 | | TREE_OVERFLOW (arg2)); | |
1240 | TREE_CONSTANT_OVERFLOW (t) = (TREE_OVERFLOW (t) | |
1241 | | TREE_CONSTANT_OVERFLOW (arg1) | |
1242 | | TREE_CONSTANT_OVERFLOW (arg2)); | |
2bc77e10 | 1243 | return t; |
1244 | } | |
1245 | #if ! defined (REAL_IS_NOT_DOUBLE) || defined (REAL_ARITHMETIC) | |
1246 | if (TREE_CODE (arg1) == REAL_CST) | |
1247 | { | |
9a24cfc6 | 1248 | REAL_VALUE_TYPE d1; |
1249 | REAL_VALUE_TYPE d2; | |
23fed9b2 | 1250 | int overflow = 0; |
64775a93 | 1251 | REAL_VALUE_TYPE value; |
c0244247 | 1252 | tree t; |
2bc77e10 | 1253 | |
9a24cfc6 | 1254 | d1 = TREE_REAL_CST (arg1); |
1255 | d2 = TREE_REAL_CST (arg2); | |
9248d3e0 | 1256 | |
1257 | /* If either operand is a NaN, just return it. Otherwise, set up | |
1258 | for floating-point trap; we return an overflow. */ | |
1259 | if (REAL_VALUE_ISNAN (d1)) | |
1260 | return arg1; | |
1261 | else if (REAL_VALUE_ISNAN (d2)) | |
1262 | return arg2; | |
1263 | else if (setjmp (float_error)) | |
2bc77e10 | 1264 | { |
23fed9b2 | 1265 | t = copy_node (arg1); |
1266 | overflow = 1; | |
1267 | goto got_float; | |
2bc77e10 | 1268 | } |
23fed9b2 | 1269 | |
c0244247 | 1270 | set_float_handler (float_error); |
2bc77e10 | 1271 | |
1272 | #ifdef REAL_ARITHMETIC | |
1273 | REAL_ARITHMETIC (value, code, d1, d2); | |
1274 | #else | |
1275 | switch (code) | |
1276 | { | |
1277 | case PLUS_EXPR: | |
1278 | value = d1 + d2; | |
1279 | break; | |
1280 | ||
1281 | case MINUS_EXPR: | |
1282 | value = d1 - d2; | |
1283 | break; | |
1284 | ||
1285 | case MULT_EXPR: | |
1286 | value = d1 * d2; | |
1287 | break; | |
1288 | ||
1289 | case RDIV_EXPR: | |
1290 | #ifndef REAL_INFINITY | |
1291 | if (d2 == 0) | |
1292 | abort (); | |
1293 | #endif | |
1294 | ||
1295 | value = d1 / d2; | |
1296 | break; | |
1297 | ||
1298 | case MIN_EXPR: | |
1299 | value = MIN (d1, d2); | |
1300 | break; | |
1301 | ||
1302 | case MAX_EXPR: | |
1303 | value = MAX (d1, d2); | |
1304 | break; | |
1305 | ||
1306 | default: | |
1307 | abort (); | |
1308 | } | |
1309 | #endif /* no REAL_ARITHMETIC */ | |
c0244247 | 1310 | t = build_real (TREE_TYPE (arg1), |
2e756f7d | 1311 | real_value_truncate (TYPE_MODE (TREE_TYPE (arg1)), value)); |
23fed9b2 | 1312 | got_float: |
b572011e | 1313 | set_float_handler (NULL_PTR); |
23fed9b2 | 1314 | |
1315 | TREE_OVERFLOW (t) | |
1316 | = (force_fit_type (t, overflow) | |
1317 | | TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2)); | |
1318 | TREE_CONSTANT_OVERFLOW (t) | |
1319 | = TREE_OVERFLOW (t) | |
1320 | | TREE_CONSTANT_OVERFLOW (arg1) | |
1321 | | TREE_CONSTANT_OVERFLOW (arg2); | |
c0244247 | 1322 | return t; |
2bc77e10 | 1323 | } |
1324 | #endif /* not REAL_IS_NOT_DOUBLE, or REAL_ARITHMETIC */ | |
1325 | if (TREE_CODE (arg1) == COMPLEX_CST) | |
1326 | { | |
5b2ade4d | 1327 | register tree type = TREE_TYPE (arg1); |
2bc77e10 | 1328 | register tree r1 = TREE_REALPART (arg1); |
1329 | register tree i1 = TREE_IMAGPART (arg1); | |
1330 | register tree r2 = TREE_REALPART (arg2); | |
1331 | register tree i2 = TREE_IMAGPART (arg2); | |
1332 | register tree t; | |
1333 | ||
1334 | switch (code) | |
1335 | { | |
1336 | case PLUS_EXPR: | |
5b2ade4d | 1337 | t = build_complex (type, |
1338 | const_binop (PLUS_EXPR, r1, r2, notrunc), | |
5485823f | 1339 | const_binop (PLUS_EXPR, i1, i2, notrunc)); |
2bc77e10 | 1340 | break; |
1341 | ||
1342 | case MINUS_EXPR: | |
5b2ade4d | 1343 | t = build_complex (type, |
1344 | const_binop (MINUS_EXPR, r1, r2, notrunc), | |
5485823f | 1345 | const_binop (MINUS_EXPR, i1, i2, notrunc)); |
2bc77e10 | 1346 | break; |
1347 | ||
1348 | case MULT_EXPR: | |
5b2ade4d | 1349 | t = build_complex (type, |
1350 | const_binop (MINUS_EXPR, | |
5485823f | 1351 | const_binop (MULT_EXPR, |
1352 | r1, r2, notrunc), | |
1353 | const_binop (MULT_EXPR, | |
1354 | i1, i2, notrunc), | |
1355 | notrunc), | |
2bc77e10 | 1356 | const_binop (PLUS_EXPR, |
5485823f | 1357 | const_binop (MULT_EXPR, |
1358 | r1, i2, notrunc), | |
1359 | const_binop (MULT_EXPR, | |
1360 | i1, r2, notrunc), | |
1361 | notrunc)); | |
2bc77e10 | 1362 | break; |
1363 | ||
1364 | case RDIV_EXPR: | |
1365 | { | |
1366 | register tree magsquared | |
1367 | = const_binop (PLUS_EXPR, | |
5485823f | 1368 | const_binop (MULT_EXPR, r2, r2, notrunc), |
1369 | const_binop (MULT_EXPR, i2, i2, notrunc), | |
1370 | notrunc); | |
56d9b5a8 | 1371 | |
5b2ade4d | 1372 | t = build_complex (type, |
1373 | const_binop | |
1374 | (INTEGRAL_TYPE_P (TREE_TYPE (r1)) | |
1375 | ? TRUNC_DIV_EXPR : RDIV_EXPR, | |
1376 | const_binop (PLUS_EXPR, | |
1377 | const_binop (MULT_EXPR, r1, r2, | |
1378 | notrunc), | |
1379 | const_binop (MULT_EXPR, i1, i2, | |
1380 | notrunc), | |
1381 | notrunc), | |
1382 | magsquared, notrunc), | |
1383 | const_binop | |
1384 | (INTEGRAL_TYPE_P (TREE_TYPE (r1)) | |
1385 | ? TRUNC_DIV_EXPR : RDIV_EXPR, | |
1386 | const_binop (MINUS_EXPR, | |
1387 | const_binop (MULT_EXPR, i1, r2, | |
1388 | notrunc), | |
1389 | const_binop (MULT_EXPR, r1, i2, | |
1390 | notrunc), | |
1391 | notrunc), | |
1392 | magsquared, notrunc)); | |
2bc77e10 | 1393 | } |
1394 | break; | |
1395 | ||
1396 | default: | |
1397 | abort (); | |
1398 | } | |
2bc77e10 | 1399 | return t; |
1400 | } | |
1401 | return 0; | |
1402 | } | |
1403 | \f | |
1404 | /* Return an INTEGER_CST with value V and type from `sizetype'. */ | |
1405 | ||
1406 | tree | |
1407 | size_int (number) | |
f0ae05c4 | 1408 | unsigned HOST_WIDE_INT number; |
2bc77e10 | 1409 | { |
1410 | register tree t; | |
1411 | /* Type-size nodes already made for small sizes. */ | |
b572011e | 1412 | static tree size_table[2*HOST_BITS_PER_WIDE_INT + 1]; |
2bc77e10 | 1413 | |
86814797 | 1414 | if (number < 2*HOST_BITS_PER_WIDE_INT + 1 |
b572011e | 1415 | && size_table[number] != 0) |
2bc77e10 | 1416 | return size_table[number]; |
86814797 | 1417 | if (number < 2*HOST_BITS_PER_WIDE_INT + 1) |
2bc77e10 | 1418 | { |
2bc77e10 | 1419 | push_obstacks_nochange (); |
1420 | /* Make this a permanent node. */ | |
e233264a | 1421 | end_temporary_allocation (); |
2bc77e10 | 1422 | t = build_int_2 (number, 0); |
1423 | TREE_TYPE (t) = sizetype; | |
1424 | size_table[number] = t; | |
1425 | pop_obstacks (); | |
1426 | } | |
1427 | else | |
1428 | { | |
1429 | t = build_int_2 (number, 0); | |
1430 | TREE_TYPE (t) = sizetype; | |
1431 | } | |
1432 | return t; | |
1433 | } | |
1434 | ||
1435 | /* Combine operands OP1 and OP2 with arithmetic operation CODE. | |
1436 | CODE is a tree code. Data type is taken from `sizetype', | |
1437 | If the operands are constant, so is the result. */ | |
1438 | ||
1439 | tree | |
1440 | size_binop (code, arg0, arg1) | |
1441 | enum tree_code code; | |
1442 | tree arg0, arg1; | |
1443 | { | |
1444 | /* Handle the special case of two integer constants faster. */ | |
1445 | if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST) | |
1446 | { | |
1447 | /* And some specific cases even faster than that. */ | |
1448 | if (code == PLUS_EXPR | |
1449 | && TREE_INT_CST_LOW (arg0) == 0 | |
1450 | && TREE_INT_CST_HIGH (arg0) == 0) | |
1451 | return arg1; | |
1452 | if (code == MINUS_EXPR | |
1453 | && TREE_INT_CST_LOW (arg1) == 0 | |
1454 | && TREE_INT_CST_HIGH (arg1) == 0) | |
1455 | return arg0; | |
1456 | if (code == MULT_EXPR | |
1457 | && TREE_INT_CST_LOW (arg0) == 1 | |
1458 | && TREE_INT_CST_HIGH (arg0) == 0) | |
1459 | return arg1; | |
1460 | /* Handle general case of two integer constants. */ | |
2c0b90fe | 1461 | return const_binop (code, arg0, arg1, 0); |
2bc77e10 | 1462 | } |
1463 | ||
1464 | if (arg0 == error_mark_node || arg1 == error_mark_node) | |
1465 | return error_mark_node; | |
1466 | ||
1467 | return fold (build (code, sizetype, arg0, arg1)); | |
1468 | } | |
1469 | \f | |
1470 | /* Given T, a tree representing type conversion of ARG1, a constant, | |
1471 | return a constant tree representing the result of conversion. */ | |
1472 | ||
1473 | static tree | |
1474 | fold_convert (t, arg1) | |
1475 | register tree t; | |
1476 | register tree arg1; | |
1477 | { | |
1478 | register tree type = TREE_TYPE (t); | |
23fed9b2 | 1479 | int overflow = 0; |
2bc77e10 | 1480 | |
780a4395 | 1481 | if (TREE_CODE (type) == POINTER_TYPE || INTEGRAL_TYPE_P (type)) |
2bc77e10 | 1482 | { |
1483 | if (TREE_CODE (arg1) == INTEGER_CST) | |
1484 | { | |
ccf05f85 | 1485 | /* If we would build a constant wider than GCC supports, |
1486 | leave the conversion unfolded. */ | |
1487 | if (TYPE_PRECISION (type) > 2 * HOST_BITS_PER_WIDE_INT) | |
1488 | return t; | |
1489 | ||
2bc77e10 | 1490 | /* Given an integer constant, make new constant with new type, |
1491 | appropriately sign-extended or truncated. */ | |
1492 | t = build_int_2 (TREE_INT_CST_LOW (arg1), | |
1493 | TREE_INT_CST_HIGH (arg1)); | |
1494 | TREE_TYPE (t) = type; | |
f55401f0 | 1495 | /* Indicate an overflow if (1) ARG1 already overflowed, |
f17f1965 | 1496 | or (2) force_fit_type indicates an overflow. |
1497 | Tell force_fit_type that an overflow has already occurred | |
1498 | if ARG1 is a too-large unsigned value and T is signed. */ | |
1499 | TREE_OVERFLOW (t) | |
1500 | = (TREE_OVERFLOW (arg1) | |
1501 | | force_fit_type (t, | |
1502 | (TREE_INT_CST_HIGH (arg1) < 0 | |
1503 | & (TREE_UNSIGNED (type) | |
1504 | < TREE_UNSIGNED (TREE_TYPE (arg1)))))); | |
1505 | TREE_CONSTANT_OVERFLOW (t) | |
1506 | = TREE_OVERFLOW (t) | TREE_CONSTANT_OVERFLOW (arg1); | |
2bc77e10 | 1507 | } |
1508 | #if !defined (REAL_IS_NOT_DOUBLE) || defined (REAL_ARITHMETIC) | |
1509 | else if (TREE_CODE (arg1) == REAL_CST) | |
1510 | { | |
a95b20e3 | 1511 | /* Don't initialize these, use assignments. |
1512 | Initialized local aggregates don't work on old compilers. */ | |
1513 | REAL_VALUE_TYPE x; | |
1514 | REAL_VALUE_TYPE l; | |
1515 | REAL_VALUE_TYPE u; | |
81b60185 | 1516 | tree type1 = TREE_TYPE (arg1); |
a95b20e3 | 1517 | |
1518 | x = TREE_REAL_CST (arg1); | |
81b60185 | 1519 | l = real_value_from_int_cst (type1, TYPE_MIN_VALUE (type)); |
1520 | u = real_value_from_int_cst (type1, TYPE_MAX_VALUE (type)); | |
9658b5af | 1521 | /* See if X will be in range after truncation towards 0. |
1522 | To compensate for truncation, move the bounds away from 0, | |
1523 | but reject if X exactly equals the adjusted bounds. */ | |
1524 | #ifdef REAL_ARITHMETIC | |
1525 | REAL_ARITHMETIC (l, MINUS_EXPR, l, dconst1); | |
1526 | REAL_ARITHMETIC (u, PLUS_EXPR, u, dconst1); | |
1527 | #else | |
1528 | l--; | |
1529 | u++; | |
1530 | #endif | |
9248d3e0 | 1531 | /* If X is a NaN, use zero instead and show we have an overflow. |
1532 | Otherwise, range check. */ | |
1533 | if (REAL_VALUE_ISNAN (x)) | |
1534 | overflow = 1, x = dconst0; | |
1535 | else if (! (REAL_VALUES_LESS (l, x) && REAL_VALUES_LESS (x, u))) | |
23fed9b2 | 1536 | overflow = 1; |
1537 | ||
2bc77e10 | 1538 | #ifndef REAL_ARITHMETIC |
1539 | { | |
b572011e | 1540 | HOST_WIDE_INT low, high; |
1541 | HOST_WIDE_INT half_word | |
1542 | = (HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT / 2); | |
2bc77e10 | 1543 | |
9248d3e0 | 1544 | if (x < 0) |
1545 | x = -x; | |
2bc77e10 | 1546 | |
9248d3e0 | 1547 | high = (HOST_WIDE_INT) (x / half_word / half_word); |
1548 | x -= (REAL_VALUE_TYPE) high * half_word * half_word; | |
1549 | if (x >= (REAL_VALUE_TYPE) half_word * half_word / 2) | |
075a0107 | 1550 | { |
9248d3e0 | 1551 | low = x - (REAL_VALUE_TYPE) half_word * half_word / 2; |
f55401f0 | 1552 | low |= (HOST_WIDE_INT) -1 << (HOST_BITS_PER_WIDE_INT - 1); |
075a0107 | 1553 | } |
1554 | else | |
9248d3e0 | 1555 | low = (HOST_WIDE_INT) x; |
2bc77e10 | 1556 | if (TREE_REAL_CST (arg1) < 0) |
1557 | neg_double (low, high, &low, &high); | |
1558 | t = build_int_2 (low, high); | |
1559 | } | |
1560 | #else | |
1561 | { | |
b572011e | 1562 | HOST_WIDE_INT low, high; |
9248d3e0 | 1563 | REAL_VALUE_TO_INT (&low, &high, x); |
2bc77e10 | 1564 | t = build_int_2 (low, high); |
1565 | } | |
1566 | #endif | |
1567 | TREE_TYPE (t) = type; | |
23fed9b2 | 1568 | TREE_OVERFLOW (t) |
1569 | = TREE_OVERFLOW (arg1) | force_fit_type (t, overflow); | |
1570 | TREE_CONSTANT_OVERFLOW (t) | |
1571 | = TREE_OVERFLOW (t) | TREE_CONSTANT_OVERFLOW (arg1); | |
2bc77e10 | 1572 | } |
1573 | #endif /* not REAL_IS_NOT_DOUBLE, or REAL_ARITHMETIC */ | |
1574 | TREE_TYPE (t) = type; | |
1575 | } | |
1576 | else if (TREE_CODE (type) == REAL_TYPE) | |
1577 | { | |
1578 | #if !defined (REAL_IS_NOT_DOUBLE) || defined (REAL_ARITHMETIC) | |
1579 | if (TREE_CODE (arg1) == INTEGER_CST) | |
1580 | return build_real_from_int_cst (type, arg1); | |
1581 | #endif /* not REAL_IS_NOT_DOUBLE, or REAL_ARITHMETIC */ | |
1582 | if (TREE_CODE (arg1) == REAL_CST) | |
c0244247 | 1583 | { |
9248d3e0 | 1584 | if (REAL_VALUE_ISNAN (TREE_REAL_CST (arg1))) |
55a78ca1 | 1585 | { |
1586 | t = arg1; | |
1587 | TREE_TYPE (arg1) = type; | |
1588 | return t; | |
1589 | } | |
9248d3e0 | 1590 | else if (setjmp (float_error)) |
c0244247 | 1591 | { |
23fed9b2 | 1592 | overflow = 1; |
1593 | t = copy_node (arg1); | |
1594 | goto got_it; | |
c0244247 | 1595 | } |
1596 | set_float_handler (float_error); | |
1597 | ||
2e756f7d | 1598 | t = build_real (type, real_value_truncate (TYPE_MODE (type), |
c0244247 | 1599 | TREE_REAL_CST (arg1))); |
b572011e | 1600 | set_float_handler (NULL_PTR); |
23fed9b2 | 1601 | |
1602 | got_it: | |
1603 | TREE_OVERFLOW (t) | |
1604 | = TREE_OVERFLOW (arg1) | force_fit_type (t, overflow); | |
1605 | TREE_CONSTANT_OVERFLOW (t) | |
1606 | = TREE_OVERFLOW (t) | TREE_CONSTANT_OVERFLOW (arg1); | |
c0244247 | 1607 | return t; |
1608 | } | |
2bc77e10 | 1609 | } |
1610 | TREE_CONSTANT (t) = 1; | |
1611 | return t; | |
1612 | } | |
1613 | \f | |
c3ce5d04 | 1614 | /* Return an expr equal to X but certainly not valid as an lvalue. |
1615 | Also make sure it is not valid as an null pointer constant. */ | |
2bc77e10 | 1616 | |
1617 | tree | |
1618 | non_lvalue (x) | |
1619 | tree x; | |
1620 | { | |
1621 | tree result; | |
1622 | ||
1623 | /* These things are certainly not lvalues. */ | |
1624 | if (TREE_CODE (x) == NON_LVALUE_EXPR | |
1625 | || TREE_CODE (x) == INTEGER_CST | |
1626 | || TREE_CODE (x) == REAL_CST | |
1627 | || TREE_CODE (x) == STRING_CST | |
1628 | || TREE_CODE (x) == ADDR_EXPR) | |
c3ce5d04 | 1629 | { |
1630 | if (TREE_CODE (x) == INTEGER_CST && integer_zerop (x)) | |
1631 | { | |
de8ae3af | 1632 | /* Use NOP_EXPR instead of NON_LVALUE_EXPR |
1633 | so convert_for_assignment won't strip it. | |
1634 | This is so this 0 won't be treated as a null pointer constant. */ | |
c3ce5d04 | 1635 | result = build1 (NOP_EXPR, TREE_TYPE (x), x); |
1636 | TREE_CONSTANT (result) = TREE_CONSTANT (x); | |
1637 | return result; | |
1638 | } | |
1639 | return x; | |
1640 | } | |
2bc77e10 | 1641 | |
1642 | result = build1 (NON_LVALUE_EXPR, TREE_TYPE (x), x); | |
1643 | TREE_CONSTANT (result) = TREE_CONSTANT (x); | |
1644 | return result; | |
1645 | } | |
56753054 | 1646 | |
b12c26dc | 1647 | /* Nonzero means lvalues are limited to those valid in pedantic ANSI C. |
1648 | Zero means allow extended lvalues. */ | |
1649 | ||
1650 | int pedantic_lvalues; | |
1651 | ||
56753054 | 1652 | /* When pedantic, return an expr equal to X but certainly not valid as a |
1653 | pedantic lvalue. Otherwise, return X. */ | |
1654 | ||
1655 | tree | |
1656 | pedantic_non_lvalue (x) | |
1657 | tree x; | |
1658 | { | |
b12c26dc | 1659 | if (pedantic_lvalues) |
56753054 | 1660 | return non_lvalue (x); |
1661 | else | |
1662 | return x; | |
1663 | } | |
e233264a | 1664 | \f |
1665 | /* Given a tree comparison code, return the code that is the logical inverse | |
1666 | of the given code. It is not safe to do this for floating-point | |
1667 | comparisons, except for NE_EXPR and EQ_EXPR. */ | |
2bc77e10 | 1668 | |
e233264a | 1669 | static enum tree_code |
1670 | invert_tree_comparison (code) | |
1671 | enum tree_code code; | |
1672 | { | |
1673 | switch (code) | |
1674 | { | |
1675 | case EQ_EXPR: | |
1676 | return NE_EXPR; | |
1677 | case NE_EXPR: | |
1678 | return EQ_EXPR; | |
1679 | case GT_EXPR: | |
1680 | return LE_EXPR; | |
1681 | case GE_EXPR: | |
1682 | return LT_EXPR; | |
1683 | case LT_EXPR: | |
1684 | return GE_EXPR; | |
1685 | case LE_EXPR: | |
1686 | return GT_EXPR; | |
1687 | default: | |
1688 | abort (); | |
1689 | } | |
1690 | } | |
1691 | ||
1692 | /* Similar, but return the comparison that results if the operands are | |
1693 | swapped. This is safe for floating-point. */ | |
1694 | ||
1695 | static enum tree_code | |
1696 | swap_tree_comparison (code) | |
1697 | enum tree_code code; | |
1698 | { | |
1699 | switch (code) | |
1700 | { | |
1701 | case EQ_EXPR: | |
1702 | case NE_EXPR: | |
1703 | return code; | |
1704 | case GT_EXPR: | |
1705 | return LT_EXPR; | |
1706 | case GE_EXPR: | |
1707 | return LE_EXPR; | |
1708 | case LT_EXPR: | |
1709 | return GT_EXPR; | |
1710 | case LE_EXPR: | |
1711 | return GE_EXPR; | |
1712 | default: | |
1713 | abort (); | |
1714 | } | |
1715 | } | |
8b94828f | 1716 | |
1717 | /* Return nonzero if CODE is a tree code that represents a truth value. */ | |
1718 | ||
1719 | static int | |
1720 | truth_value_p (code) | |
1721 | enum tree_code code; | |
1722 | { | |
1723 | return (TREE_CODE_CLASS (code) == '<' | |
1724 | || code == TRUTH_AND_EXPR || code == TRUTH_ANDIF_EXPR | |
1725 | || code == TRUTH_OR_EXPR || code == TRUTH_ORIF_EXPR | |
1726 | || code == TRUTH_XOR_EXPR || code == TRUTH_NOT_EXPR); | |
1727 | } | |
e233264a | 1728 | \f |
11acc1df | 1729 | /* Return nonzero if two operands are necessarily equal. |
1730 | If ONLY_CONST is non-zero, only return non-zero for constants. | |
1731 | This function tests whether the operands are indistinguishable; | |
1732 | it does not test whether they are equal using C's == operation. | |
1733 | The distinction is important for IEEE floating point, because | |
1734 | (1) -0.0 and 0.0 are distinguishable, but -0.0==0.0, and | |
1735 | (2) two NaNs may be indistinguishable, but NaN!=NaN. */ | |
2bc77e10 | 1736 | |
1737 | int | |
1738 | operand_equal_p (arg0, arg1, only_const) | |
1739 | tree arg0, arg1; | |
1740 | int only_const; | |
1741 | { | |
1742 | /* If both types don't have the same signedness, then we can't consider | |
1743 | them equal. We must check this before the STRIP_NOPS calls | |
1744 | because they may change the signedness of the arguments. */ | |
1745 | if (TREE_UNSIGNED (TREE_TYPE (arg0)) != TREE_UNSIGNED (TREE_TYPE (arg1))) | |
1746 | return 0; | |
1747 | ||
1748 | STRIP_NOPS (arg0); | |
1749 | STRIP_NOPS (arg1); | |
1750 | ||
1751 | /* If ARG0 and ARG1 are the same SAVE_EXPR, they are necessarily equal. | |
1752 | We don't care about side effects in that case because the SAVE_EXPR | |
1753 | takes care of that for us. */ | |
1754 | if (TREE_CODE (arg0) == SAVE_EXPR && arg0 == arg1) | |
1755 | return ! only_const; | |
1756 | ||
1757 | if (TREE_SIDE_EFFECTS (arg0) || TREE_SIDE_EFFECTS (arg1)) | |
1758 | return 0; | |
1759 | ||
1760 | if (TREE_CODE (arg0) == TREE_CODE (arg1) | |
1761 | && TREE_CODE (arg0) == ADDR_EXPR | |
1762 | && TREE_OPERAND (arg0, 0) == TREE_OPERAND (arg1, 0)) | |
1763 | return 1; | |
1764 | ||
1765 | if (TREE_CODE (arg0) == TREE_CODE (arg1) | |
1766 | && TREE_CODE (arg0) == INTEGER_CST | |
1767 | && TREE_INT_CST_LOW (arg0) == TREE_INT_CST_LOW (arg1) | |
1768 | && TREE_INT_CST_HIGH (arg0) == TREE_INT_CST_HIGH (arg1)) | |
1769 | return 1; | |
1770 | ||
11acc1df | 1771 | /* Detect when real constants are equal. */ |
2bc77e10 | 1772 | if (TREE_CODE (arg0) == TREE_CODE (arg1) |
11acc1df | 1773 | && TREE_CODE (arg0) == REAL_CST) |
748e6d74 | 1774 | return !bcmp ((char *) &TREE_REAL_CST (arg0), |
1775 | (char *) &TREE_REAL_CST (arg1), | |
11acc1df | 1776 | sizeof (REAL_VALUE_TYPE)); |
2bc77e10 | 1777 | |
1778 | if (only_const) | |
1779 | return 0; | |
1780 | ||
1781 | if (arg0 == arg1) | |
1782 | return 1; | |
1783 | ||
1784 | if (TREE_CODE (arg0) != TREE_CODE (arg1)) | |
1785 | return 0; | |
1786 | /* This is needed for conversions and for COMPONENT_REF. | |
1787 | Might as well play it safe and always test this. */ | |
1788 | if (TYPE_MODE (TREE_TYPE (arg0)) != TYPE_MODE (TREE_TYPE (arg1))) | |
1789 | return 0; | |
1790 | ||
1791 | switch (TREE_CODE_CLASS (TREE_CODE (arg0))) | |
1792 | { | |
1793 | case '1': | |
1794 | /* Two conversions are equal only if signedness and modes match. */ | |
1795 | if ((TREE_CODE (arg0) == NOP_EXPR || TREE_CODE (arg0) == CONVERT_EXPR) | |
1796 | && (TREE_UNSIGNED (TREE_TYPE (arg0)) | |
1797 | != TREE_UNSIGNED (TREE_TYPE (arg1)))) | |
1798 | return 0; | |
1799 | ||
1800 | return operand_equal_p (TREE_OPERAND (arg0, 0), | |
1801 | TREE_OPERAND (arg1, 0), 0); | |
1802 | ||
1803 | case '<': | |
1804 | case '2': | |
1805 | return (operand_equal_p (TREE_OPERAND (arg0, 0), | |
1806 | TREE_OPERAND (arg1, 0), 0) | |
1807 | && operand_equal_p (TREE_OPERAND (arg0, 1), | |
1808 | TREE_OPERAND (arg1, 1), 0)); | |
1809 | ||
1810 | case 'r': | |
1811 | switch (TREE_CODE (arg0)) | |
1812 | { | |
1813 | case INDIRECT_REF: | |
1814 | return operand_equal_p (TREE_OPERAND (arg0, 0), | |
1815 | TREE_OPERAND (arg1, 0), 0); | |
1816 | ||
1817 | case COMPONENT_REF: | |
1818 | case ARRAY_REF: | |
1819 | return (operand_equal_p (TREE_OPERAND (arg0, 0), | |
1820 | TREE_OPERAND (arg1, 0), 0) | |
1821 | && operand_equal_p (TREE_OPERAND (arg0, 1), | |
1822 | TREE_OPERAND (arg1, 1), 0)); | |
1823 | ||
1824 | case BIT_FIELD_REF: | |
1825 | return (operand_equal_p (TREE_OPERAND (arg0, 0), | |
1826 | TREE_OPERAND (arg1, 0), 0) | |
1827 | && operand_equal_p (TREE_OPERAND (arg0, 1), | |
1828 | TREE_OPERAND (arg1, 1), 0) | |
1829 | && operand_equal_p (TREE_OPERAND (arg0, 2), | |
1830 | TREE_OPERAND (arg1, 2), 0)); | |
1831 | } | |
1832 | break; | |
1833 | } | |
1834 | ||
1835 | return 0; | |
1836 | } | |
e233264a | 1837 | \f |
1838 | /* Similar to operand_equal_p, but see if ARG0 might have been made by | |
1839 | shorten_compare from ARG1 when ARG1 was being compared with OTHER. | |
2bc77e10 | 1840 | |
2bc77e10 | 1841 | When in doubt, return 0. */ |
1842 | ||
1843 | static int | |
e233264a | 1844 | operand_equal_for_comparison_p (arg0, arg1, other) |
1845 | tree arg0, arg1; | |
1846 | tree other; | |
2bc77e10 | 1847 | { |
e233264a | 1848 | int unsignedp1, unsignedpo; |
1849 | tree primarg1, primother; | |
1c7c5fd5 | 1850 | unsigned correct_width; |
2bc77e10 | 1851 | |
e233264a | 1852 | if (operand_equal_p (arg0, arg1, 0)) |
2bc77e10 | 1853 | return 1; |
1854 | ||
154e6f12 | 1855 | if (! INTEGRAL_TYPE_P (TREE_TYPE (arg0)) |
1856 | || ! INTEGRAL_TYPE_P (TREE_TYPE (arg1))) | |
2bc77e10 | 1857 | return 0; |
1858 | ||
e233264a | 1859 | /* Duplicate what shorten_compare does to ARG1 and see if that gives the |
1860 | actual comparison operand, ARG0. | |
2bc77e10 | 1861 | |
e233264a | 1862 | First throw away any conversions to wider types |
2bc77e10 | 1863 | already present in the operands. */ |
2bc77e10 | 1864 | |
e233264a | 1865 | primarg1 = get_narrower (arg1, &unsignedp1); |
1866 | primother = get_narrower (other, &unsignedpo); | |
1867 | ||
1868 | correct_width = TYPE_PRECISION (TREE_TYPE (arg1)); | |
1869 | if (unsignedp1 == unsignedpo | |
1870 | && TYPE_PRECISION (TREE_TYPE (primarg1)) < correct_width | |
1871 | && TYPE_PRECISION (TREE_TYPE (primother)) < correct_width) | |
2bc77e10 | 1872 | { |
e233264a | 1873 | tree type = TREE_TYPE (arg0); |
2bc77e10 | 1874 | |
1875 | /* Make sure shorter operand is extended the right way | |
1876 | to match the longer operand. */ | |
e233264a | 1877 | primarg1 = convert (signed_or_unsigned_type (unsignedp1, |
1878 | TREE_TYPE (primarg1)), | |
1879 | primarg1); | |
2bc77e10 | 1880 | |
e233264a | 1881 | if (operand_equal_p (arg0, convert (type, primarg1), 0)) |
2bc77e10 | 1882 | return 1; |
1883 | } | |
1884 | ||
1885 | return 0; | |
1886 | } | |
1887 | \f | |
eb2f80f3 | 1888 | /* See if ARG is an expression that is either a comparison or is performing |
e233264a | 1889 | arithmetic on comparisons. The comparisons must only be comparing |
1890 | two different values, which will be stored in *CVAL1 and *CVAL2; if | |
1891 | they are non-zero it means that some operands have already been found. | |
1892 | No variables may be used anywhere else in the expression except in the | |
d0314131 | 1893 | comparisons. If SAVE_P is true it means we removed a SAVE_EXPR around |
1894 | the expression and save_expr needs to be called with CVAL1 and CVAL2. | |
e233264a | 1895 | |
1896 | If this is true, return 1. Otherwise, return zero. */ | |
1897 | ||
1898 | static int | |
d0314131 | 1899 | twoval_comparison_p (arg, cval1, cval2, save_p) |
e233264a | 1900 | tree arg; |
1901 | tree *cval1, *cval2; | |
d0314131 | 1902 | int *save_p; |
e233264a | 1903 | { |
1904 | enum tree_code code = TREE_CODE (arg); | |
1905 | char class = TREE_CODE_CLASS (code); | |
1906 | ||
1907 | /* We can handle some of the 'e' cases here. */ | |
d0314131 | 1908 | if (class == 'e' && code == TRUTH_NOT_EXPR) |
e233264a | 1909 | class = '1'; |
1910 | else if (class == 'e' | |
1911 | && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR | |
1912 | || code == COMPOUND_EXPR)) | |
1913 | class = '2'; | |
8be91fe5 | 1914 | |
1915 | /* ??? Disable this since the SAVE_EXPR might already be in use outside | |
1916 | the expression. There may be no way to make this work, but it needs | |
1917 | to be looked at again for 2.6. */ | |
1918 | #if 0 | |
d0314131 | 1919 | else if (class == 'e' && code == SAVE_EXPR && SAVE_EXPR_RTL (arg) == 0) |
1920 | { | |
1921 | /* If we've already found a CVAL1 or CVAL2, this expression is | |
1922 | two complex to handle. */ | |
1923 | if (*cval1 || *cval2) | |
1924 | return 0; | |
1925 | ||
1926 | class = '1'; | |
1927 | *save_p = 1; | |
1928 | } | |
8be91fe5 | 1929 | #endif |
e233264a | 1930 | |
1931 | switch (class) | |
1932 | { | |
1933 | case '1': | |
d0314131 | 1934 | return twoval_comparison_p (TREE_OPERAND (arg, 0), cval1, cval2, save_p); |
e233264a | 1935 | |
1936 | case '2': | |
d0314131 | 1937 | return (twoval_comparison_p (TREE_OPERAND (arg, 0), cval1, cval2, save_p) |
1938 | && twoval_comparison_p (TREE_OPERAND (arg, 1), | |
1939 | cval1, cval2, save_p)); | |
e233264a | 1940 | |
1941 | case 'c': | |
1942 | return 1; | |
1943 | ||
1944 | case 'e': | |
1945 | if (code == COND_EXPR) | |
d0314131 | 1946 | return (twoval_comparison_p (TREE_OPERAND (arg, 0), |
1947 | cval1, cval2, save_p) | |
1948 | && twoval_comparison_p (TREE_OPERAND (arg, 1), | |
1949 | cval1, cval2, save_p) | |
e233264a | 1950 | && twoval_comparison_p (TREE_OPERAND (arg, 2), |
d0314131 | 1951 | cval1, cval2, save_p)); |
e233264a | 1952 | return 0; |
1953 | ||
1954 | case '<': | |
1955 | /* First see if we can handle the first operand, then the second. For | |
1956 | the second operand, we know *CVAL1 can't be zero. It must be that | |
1957 | one side of the comparison is each of the values; test for the | |
1958 | case where this isn't true by failing if the two operands | |
1959 | are the same. */ | |
1960 | ||
1961 | if (operand_equal_p (TREE_OPERAND (arg, 0), | |
1962 | TREE_OPERAND (arg, 1), 0)) | |
1963 | return 0; | |
1964 | ||
1965 | if (*cval1 == 0) | |
1966 | *cval1 = TREE_OPERAND (arg, 0); | |
1967 | else if (operand_equal_p (*cval1, TREE_OPERAND (arg, 0), 0)) | |
1968 | ; | |
1969 | else if (*cval2 == 0) | |
1970 | *cval2 = TREE_OPERAND (arg, 0); | |
1971 | else if (operand_equal_p (*cval2, TREE_OPERAND (arg, 0), 0)) | |
1972 | ; | |
1973 | else | |
1974 | return 0; | |
1975 | ||
1976 | if (operand_equal_p (*cval1, TREE_OPERAND (arg, 1), 0)) | |
1977 | ; | |
1978 | else if (*cval2 == 0) | |
1979 | *cval2 = TREE_OPERAND (arg, 1); | |
1980 | else if (operand_equal_p (*cval2, TREE_OPERAND (arg, 1), 0)) | |
1981 | ; | |
1982 | else | |
1983 | return 0; | |
1984 | ||
1985 | return 1; | |
1986 | } | |
1987 | ||
1988 | return 0; | |
1989 | } | |
1990 | \f | |
1991 | /* ARG is a tree that is known to contain just arithmetic operations and | |
1992 | comparisons. Evaluate the operations in the tree substituting NEW0 for | |
eb2f80f3 | 1993 | any occurrence of OLD0 as an operand of a comparison and likewise for |
e233264a | 1994 | NEW1 and OLD1. */ |
1995 | ||
1996 | static tree | |
1997 | eval_subst (arg, old0, new0, old1, new1) | |
1998 | tree arg; | |
1999 | tree old0, new0, old1, new1; | |
2000 | { | |
2001 | tree type = TREE_TYPE (arg); | |
2002 | enum tree_code code = TREE_CODE (arg); | |
2003 | char class = TREE_CODE_CLASS (code); | |
2004 | ||
2005 | /* We can handle some of the 'e' cases here. */ | |
2006 | if (class == 'e' && code == TRUTH_NOT_EXPR) | |
2007 | class = '1'; | |
2008 | else if (class == 'e' | |
2009 | && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR)) | |
2010 | class = '2'; | |
2011 | ||
2012 | switch (class) | |
2013 | { | |
2014 | case '1': | |
2015 | return fold (build1 (code, type, | |
2016 | eval_subst (TREE_OPERAND (arg, 0), | |
2017 | old0, new0, old1, new1))); | |
2018 | ||
2019 | case '2': | |
2020 | return fold (build (code, type, | |
2021 | eval_subst (TREE_OPERAND (arg, 0), | |
2022 | old0, new0, old1, new1), | |
2023 | eval_subst (TREE_OPERAND (arg, 1), | |
2024 | old0, new0, old1, new1))); | |
2025 | ||
2026 | case 'e': | |
2027 | switch (code) | |
2028 | { | |
2029 | case SAVE_EXPR: | |
2030 | return eval_subst (TREE_OPERAND (arg, 0), old0, new0, old1, new1); | |
2031 | ||
2032 | case COMPOUND_EXPR: | |
2033 | return eval_subst (TREE_OPERAND (arg, 1), old0, new0, old1, new1); | |
2034 | ||
2035 | case COND_EXPR: | |
2036 | return fold (build (code, type, | |
2037 | eval_subst (TREE_OPERAND (arg, 0), | |
2038 | old0, new0, old1, new1), | |
2039 | eval_subst (TREE_OPERAND (arg, 1), | |
2040 | old0, new0, old1, new1), | |
2041 | eval_subst (TREE_OPERAND (arg, 2), | |
2042 | old0, new0, old1, new1))); | |
2043 | } | |
2044 | ||
2045 | case '<': | |
2046 | { | |
2047 | tree arg0 = TREE_OPERAND (arg, 0); | |
2048 | tree arg1 = TREE_OPERAND (arg, 1); | |
2049 | ||
2050 | /* We need to check both for exact equality and tree equality. The | |
2051 | former will be true if the operand has a side-effect. In that | |
2052 | case, we know the operand occurred exactly once. */ | |
2053 | ||
2054 | if (arg0 == old0 || operand_equal_p (arg0, old0, 0)) | |
2055 | arg0 = new0; | |
2056 | else if (arg0 == old1 || operand_equal_p (arg0, old1, 0)) | |
2057 | arg0 = new1; | |
2058 | ||
2059 | if (arg1 == old0 || operand_equal_p (arg1, old0, 0)) | |
2060 | arg1 = new0; | |
2061 | else if (arg1 == old1 || operand_equal_p (arg1, old1, 0)) | |
2062 | arg1 = new1; | |
2063 | ||
2064 | return fold (build (code, type, arg0, arg1)); | |
2065 | } | |
2066 | } | |
2067 | ||
2068 | return arg; | |
2069 | } | |
2070 | \f | |
2bc77e10 | 2071 | /* Return a tree for the case when the result of an expression is RESULT |
2072 | converted to TYPE and OMITTED was previously an operand of the expression | |
2073 | but is now not needed (e.g., we folded OMITTED * 0). | |
2074 | ||
2075 | If OMITTED has side effects, we must evaluate it. Otherwise, just do | |
2076 | the conversion of RESULT to TYPE. */ | |
2077 | ||
2078 | static tree | |
2079 | omit_one_operand (type, result, omitted) | |
2080 | tree type, result, omitted; | |
2081 | { | |
2082 | tree t = convert (type, result); | |
2083 | ||
2084 | if (TREE_SIDE_EFFECTS (omitted)) | |
2085 | return build (COMPOUND_EXPR, type, omitted, t); | |
2086 | ||
c3ce5d04 | 2087 | return non_lvalue (t); |
2bc77e10 | 2088 | } |
6df5edfa | 2089 | |
2090 | /* Similar, but call pedantic_non_lvalue instead of non_lvalue. */ | |
2091 | ||
2092 | static tree | |
2093 | pedantic_omit_one_operand (type, result, omitted) | |
2094 | tree type, result, omitted; | |
2095 | { | |
2096 | tree t = convert (type, result); | |
2097 | ||
2098 | if (TREE_SIDE_EFFECTS (omitted)) | |
2099 | return build (COMPOUND_EXPR, type, omitted, t); | |
2100 | ||
2101 | return pedantic_non_lvalue (t); | |
2102 | } | |
2103 | ||
2104 | ||
2bc77e10 | 2105 | \f |
46b0e007 | 2106 | /* Return a simplified tree node for the truth-negation of ARG. This |
2107 | never alters ARG itself. We assume that ARG is an operation that | |
2bc77e10 | 2108 | returns a truth value (0 or 1). */ |
2109 | ||
2110 | tree | |
2111 | invert_truthvalue (arg) | |
2112 | tree arg; | |
2113 | { | |
2114 | tree type = TREE_TYPE (arg); | |
e233264a | 2115 | enum tree_code code = TREE_CODE (arg); |
2bc77e10 | 2116 | |
c34cc7e5 | 2117 | if (code == ERROR_MARK) |
2118 | return arg; | |
2119 | ||
e233264a | 2120 | /* If this is a comparison, we can simply invert it, except for |
2121 | floating-point non-equality comparisons, in which case we just | |
2122 | enclose a TRUTH_NOT_EXPR around what we have. */ | |
2bc77e10 | 2123 | |
e233264a | 2124 | if (TREE_CODE_CLASS (code) == '<') |
2bc77e10 | 2125 | { |
780a4395 | 2126 | if (FLOAT_TYPE_P (TREE_TYPE (TREE_OPERAND (arg, 0))) |
e233264a | 2127 | && code != NE_EXPR && code != EQ_EXPR) |
2128 | return build1 (TRUTH_NOT_EXPR, type, arg); | |
2129 | else | |
c26f1a45 | 2130 | return build (invert_tree_comparison (code), type, |
46b0e007 | 2131 | TREE_OPERAND (arg, 0), TREE_OPERAND (arg, 1)); |
e233264a | 2132 | } |
2bc77e10 | 2133 | |
e233264a | 2134 | switch (code) |
2135 | { | |
2bc77e10 | 2136 | case INTEGER_CST: |
2137 | return convert (type, build_int_2 (TREE_INT_CST_LOW (arg) == 0 | |
2138 | && TREE_INT_CST_HIGH (arg) == 0, 0)); | |
2139 | ||
2140 | case TRUTH_AND_EXPR: | |
2141 | return build (TRUTH_OR_EXPR, type, | |
2142 | invert_truthvalue (TREE_OPERAND (arg, 0)), | |
2143 | invert_truthvalue (TREE_OPERAND (arg, 1))); | |
2144 | ||
2145 | case TRUTH_OR_EXPR: | |
2146 | return build (TRUTH_AND_EXPR, type, | |
2147 | invert_truthvalue (TREE_OPERAND (arg, 0)), | |
2148 | invert_truthvalue (TREE_OPERAND (arg, 1))); | |
2149 | ||
9a7b73a1 | 2150 | case TRUTH_XOR_EXPR: |
2151 | /* Here we can invert either operand. We invert the first operand | |
2152 | unless the second operand is a TRUTH_NOT_EXPR in which case our | |
2153 | result is the XOR of the first operand with the inside of the | |
2154 | negation of the second operand. */ | |
2155 | ||
2156 | if (TREE_CODE (TREE_OPERAND (arg, 1)) == TRUTH_NOT_EXPR) | |
2157 | return build (TRUTH_XOR_EXPR, type, TREE_OPERAND (arg, 0), | |
2158 | TREE_OPERAND (TREE_OPERAND (arg, 1), 0)); | |
2159 | else | |
2160 | return build (TRUTH_XOR_EXPR, type, | |
2161 | invert_truthvalue (TREE_OPERAND (arg, 0)), | |
2162 | TREE_OPERAND (arg, 1)); | |
2163 | ||
2bc77e10 | 2164 | case TRUTH_ANDIF_EXPR: |
2165 | return build (TRUTH_ORIF_EXPR, type, | |
2166 | invert_truthvalue (TREE_OPERAND (arg, 0)), | |
2167 | invert_truthvalue (TREE_OPERAND (arg, 1))); | |
2168 | ||
2169 | case TRUTH_ORIF_EXPR: | |
2170 | return build (TRUTH_ANDIF_EXPR, type, | |
2171 | invert_truthvalue (TREE_OPERAND (arg, 0)), | |
2172 | invert_truthvalue (TREE_OPERAND (arg, 1))); | |
2173 | ||
2174 | case TRUTH_NOT_EXPR: | |
2175 | return TREE_OPERAND (arg, 0); | |
2176 | ||
2177 | case COND_EXPR: | |
2178 | return build (COND_EXPR, type, TREE_OPERAND (arg, 0), | |
2179 | invert_truthvalue (TREE_OPERAND (arg, 1)), | |
2180 | invert_truthvalue (TREE_OPERAND (arg, 2))); | |
2181 | ||
3139f3ce | 2182 | case COMPOUND_EXPR: |
2183 | return build (COMPOUND_EXPR, type, TREE_OPERAND (arg, 0), | |
2184 | invert_truthvalue (TREE_OPERAND (arg, 1))); | |
2185 | ||
2bc77e10 | 2186 | case NON_LVALUE_EXPR: |
2187 | return invert_truthvalue (TREE_OPERAND (arg, 0)); | |
2188 | ||
2189 | case NOP_EXPR: | |
2190 | case CONVERT_EXPR: | |
2191 | case FLOAT_EXPR: | |
2192 | return build1 (TREE_CODE (arg), type, | |
2193 | invert_truthvalue (TREE_OPERAND (arg, 0))); | |
2194 | ||
2195 | case BIT_AND_EXPR: | |
c35387e1 | 2196 | if (!integer_onep (TREE_OPERAND (arg, 1))) |
2197 | break; | |
2bc77e10 | 2198 | return build (EQ_EXPR, type, arg, convert (type, integer_zero_node)); |
2bc77e10 | 2199 | |
468d693c | 2200 | case SAVE_EXPR: |
2201 | return build1 (TRUTH_NOT_EXPR, type, arg); | |
f33c3a83 | 2202 | |
2203 | case CLEANUP_POINT_EXPR: | |
2204 | return build1 (CLEANUP_POINT_EXPR, type, | |
2205 | invert_truthvalue (TREE_OPERAND (arg, 0))); | |
c35387e1 | 2206 | } |
2207 | if (TREE_CODE (TREE_TYPE (arg)) != BOOLEAN_TYPE) | |
468d693c | 2208 | abort (); |
c35387e1 | 2209 | return build1 (TRUTH_NOT_EXPR, type, arg); |
2bc77e10 | 2210 | } |
2211 | ||
2212 | /* Given a bit-wise operation CODE applied to ARG0 and ARG1, see if both | |
2213 | operands are another bit-wise operation with a common input. If so, | |
2214 | distribute the bit operations to save an operation and possibly two if | |
2215 | constants are involved. For example, convert | |
2216 | (A | B) & (A | C) into A | (B & C) | |
2217 | Further simplification will occur if B and C are constants. | |
2218 | ||
2219 | If this optimization cannot be done, 0 will be returned. */ | |
2220 | ||
2221 | static tree | |
2222 | distribute_bit_expr (code, type, arg0, arg1) | |
2223 | enum tree_code code; | |
2224 | tree type; | |
2225 | tree arg0, arg1; | |
2226 | { | |
2227 | tree common; | |
2228 | tree left, right; | |
2229 | ||
2230 | if (TREE_CODE (arg0) != TREE_CODE (arg1) | |
2231 | || TREE_CODE (arg0) == code | |
5b1de181 | 2232 | || (TREE_CODE (arg0) != BIT_AND_EXPR |
2233 | && TREE_CODE (arg0) != BIT_IOR_EXPR)) | |
2bc77e10 | 2234 | return 0; |
2235 | ||
2236 | if (operand_equal_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg1, 0), 0)) | |
2237 | { | |
2238 | common = TREE_OPERAND (arg0, 0); | |
2239 | left = TREE_OPERAND (arg0, 1); | |
2240 | right = TREE_OPERAND (arg1, 1); | |
2241 | } | |
2242 | else if (operand_equal_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg1, 1), 0)) | |
2243 | { | |
2244 | common = TREE_OPERAND (arg0, 0); | |
2245 | left = TREE_OPERAND (arg0, 1); | |
2246 | right = TREE_OPERAND (arg1, 0); | |
2247 | } | |
2248 | else if (operand_equal_p (TREE_OPERAND (arg0, 1), TREE_OPERAND (arg1, 0), 0)) | |
2249 | { | |
2250 | common = TREE_OPERAND (arg0, 1); | |
2251 | left = TREE_OPERAND (arg0, 0); | |
2252 | right = TREE_OPERAND (arg1, 1); | |
2253 | } | |
2254 | else if (operand_equal_p (TREE_OPERAND (arg0, 1), TREE_OPERAND (arg1, 1), 0)) | |
2255 | { | |
2256 | common = TREE_OPERAND (arg0, 1); | |
2257 | left = TREE_OPERAND (arg0, 0); | |
2258 | right = TREE_OPERAND (arg1, 0); | |
2259 | } | |
2260 | else | |
2261 | return 0; | |
2262 | ||
2263 | return fold (build (TREE_CODE (arg0), type, common, | |
2264 | fold (build (code, type, left, right)))); | |
2265 | } | |
2266 | \f | |
2267 | /* Return a BIT_FIELD_REF of type TYPE to refer to BITSIZE bits of INNER | |
2268 | starting at BITPOS. The field is unsigned if UNSIGNEDP is non-zero. */ | |
2269 | ||
2270 | static tree | |
2271 | make_bit_field_ref (inner, type, bitsize, bitpos, unsignedp) | |
2272 | tree inner; | |
2273 | tree type; | |
2274 | int bitsize, bitpos; | |
2275 | int unsignedp; | |
2276 | { | |
2277 | tree result = build (BIT_FIELD_REF, type, inner, | |
2278 | size_int (bitsize), size_int (bitpos)); | |
2279 | ||
2280 | TREE_UNSIGNED (result) = unsignedp; | |
2281 | ||
2282 | return result; | |
2283 | } | |
2284 | ||
2285 | /* Optimize a bit-field compare. | |
2286 | ||
2287 | There are two cases: First is a compare against a constant and the | |
2288 | second is a comparison of two items where the fields are at the same | |
2289 | bit position relative to the start of a chunk (byte, halfword, word) | |
2290 | large enough to contain it. In these cases we can avoid the shift | |
2291 | implicit in bitfield extractions. | |
2292 | ||
2293 | For constants, we emit a compare of the shifted constant with the | |
2294 | BIT_AND_EXPR of a mask and a byte, halfword, or word of the operand being | |
2295 | compared. For two fields at the same position, we do the ANDs with the | |
2296 | similar mask and compare the result of the ANDs. | |
2297 | ||
2298 | CODE is the comparison code, known to be either NE_EXPR or EQ_EXPR. | |
2299 | COMPARE_TYPE is the type of the comparison, and LHS and RHS | |
2300 | are the left and right operands of the comparison, respectively. | |
2301 | ||
4bbea254 | 2302 | If the optimization described above can be done, we return the resulting |
2bc77e10 | 2303 | tree. Otherwise we return zero. */ |
2304 | ||
2305 | static tree | |
2306 | optimize_bit_field_compare (code, compare_type, lhs, rhs) | |
2307 | enum tree_code code; | |
2308 | tree compare_type; | |
2309 | tree lhs, rhs; | |
2310 | { | |
2311 | int lbitpos, lbitsize, rbitpos, rbitsize; | |
2312 | int lnbitpos, lnbitsize, rnbitpos, rnbitsize; | |
2313 | tree type = TREE_TYPE (lhs); | |
2314 | tree signed_type, unsigned_type; | |
2315 | int const_p = TREE_CODE (rhs) == INTEGER_CST; | |
2316 | enum machine_mode lmode, rmode, lnmode, rnmode; | |
2317 | int lunsignedp, runsignedp; | |
2318 | int lvolatilep = 0, rvolatilep = 0; | |
2319 | tree linner, rinner; | |
2320 | tree mask; | |
bbfbdece | 2321 | tree offset; |
2bc77e10 | 2322 | |
2323 | /* Get all the information about the extractions being done. If the bit size | |
2324 | if the same as the size of the underlying object, we aren't doing an | |
2325 | extraction at all and so can do nothing. */ | |
bbfbdece | 2326 | linner = get_inner_reference (lhs, &lbitsize, &lbitpos, &offset, &lmode, |
2bc77e10 | 2327 | &lunsignedp, &lvolatilep); |
f73497ef | 2328 | if (linner == lhs || lbitsize == GET_MODE_BITSIZE (lmode) || lbitsize < 0 |
bbfbdece | 2329 | || offset != 0) |
2bc77e10 | 2330 | return 0; |
2331 | ||
2332 | if (!const_p) | |
2333 | { | |
2334 | /* If this is not a constant, we can only do something if bit positions, | |
2335 | sizes, and signedness are the same. */ | |
bbfbdece | 2336 | rinner = get_inner_reference (rhs, &rbitsize, &rbitpos, &offset, |
2bc77e10 | 2337 | &rmode, &runsignedp, &rvolatilep); |
2338 | ||
f73497ef | 2339 | if (rinner == rhs || lbitpos != rbitpos || lbitsize != rbitsize |
bbfbdece | 2340 | || lunsignedp != runsignedp || offset != 0) |
2bc77e10 | 2341 | return 0; |
2342 | } | |
2343 | ||
2344 | /* See if we can find a mode to refer to this field. We should be able to, | |
2345 | but fail if we can't. */ | |
2346 | lnmode = get_best_mode (lbitsize, lbitpos, | |
2347 | TYPE_ALIGN (TREE_TYPE (linner)), word_mode, | |
2348 | lvolatilep); | |
2349 | if (lnmode == VOIDmode) | |
2350 | return 0; | |
2351 | ||
2352 | /* Set signed and unsigned types of the precision of this mode for the | |
2353 | shifts below. */ | |
2354 | signed_type = type_for_mode (lnmode, 0); | |
2355 | unsigned_type = type_for_mode (lnmode, 1); | |
2356 | ||
2357 | if (! const_p) | |
2358 | { | |
2359 | rnmode = get_best_mode (rbitsize, rbitpos, | |
2360 | TYPE_ALIGN (TREE_TYPE (rinner)), word_mode, | |
2361 | rvolatilep); | |
2362 | if (rnmode == VOIDmode) | |
2363 | return 0; | |
2364 | } | |
2365 | ||
2366 | /* Compute the bit position and size for the new reference and our offset | |
2367 | within it. If the new reference is the same size as the original, we | |
2368 | won't optimize anything, so return zero. */ | |
2369 | lnbitsize = GET_MODE_BITSIZE (lnmode); | |
2370 | lnbitpos = lbitpos & ~ (lnbitsize - 1); | |
2371 | lbitpos -= lnbitpos; | |
2372 | if (lnbitsize == lbitsize) | |
2373 | return 0; | |
2374 | ||
2375 | if (! const_p) | |
2376 | { | |
2377 | rnbitsize = GET_MODE_BITSIZE (rnmode); | |
2378 | rnbitpos = rbitpos & ~ (rnbitsize - 1); | |
2379 | rbitpos -= rnbitpos; | |
2380 | if (rnbitsize == rbitsize) | |
2381 | return 0; | |
2382 | } | |
2383 | ||
51356f86 | 2384 | if (BYTES_BIG_ENDIAN) |
2385 | lbitpos = lnbitsize - lbitsize - lbitpos; | |
2bc77e10 | 2386 | |
2387 | /* Make the mask to be used against the extracted field. */ | |
52a49c7c | 2388 | mask = build_int_2 (~0, ~0); |
2389 | TREE_TYPE (mask) = unsigned_type; | |
86814797 | 2390 | force_fit_type (mask, 0); |
52a49c7c | 2391 | mask = convert (unsigned_type, mask); |
5485823f | 2392 | mask = const_binop (LSHIFT_EXPR, mask, size_int (lnbitsize - lbitsize), 0); |
2bc77e10 | 2393 | mask = const_binop (RSHIFT_EXPR, mask, |
5485823f | 2394 | size_int (lnbitsize - lbitsize - lbitpos), 0); |
2bc77e10 | 2395 | |
2396 | if (! const_p) | |
2397 | /* If not comparing with constant, just rework the comparison | |
2398 | and return. */ | |
2399 | return build (code, compare_type, | |
66716a97 | 2400 | build (BIT_AND_EXPR, unsigned_type, |
2401 | make_bit_field_ref (linner, unsigned_type, | |
2402 | lnbitsize, lnbitpos, 1), | |
2bc77e10 | 2403 | mask), |
66716a97 | 2404 | build (BIT_AND_EXPR, unsigned_type, |
2405 | make_bit_field_ref (rinner, unsigned_type, | |
2406 | rnbitsize, rnbitpos, 1), | |
2bc77e10 | 2407 | mask)); |
2408 | ||
2409 | /* Otherwise, we are handling the constant case. See if the constant is too | |
2410 | big for the field. Warn and return a tree of for 0 (false) if so. We do | |
2411 | this not only for its own sake, but to avoid having to test for this | |
2412 | error case below. If we didn't, we might generate wrong code. | |
2413 | ||
2414 | For unsigned fields, the constant shifted right by the field length should | |
2415 | be all zero. For signed fields, the high-order bits should agree with | |
2416 | the sign bit. */ | |
2417 | ||
2418 | if (lunsignedp) | |
2419 | { | |
2420 | if (! integer_zerop (const_binop (RSHIFT_EXPR, | |
2421 | convert (unsigned_type, rhs), | |
5485823f | 2422 | size_int (lbitsize), 0))) |
2bc77e10 | 2423 | { |
2424 | warning ("comparison is always %s due to width of bitfield", | |
2425 | code == NE_EXPR ? "one" : "zero"); | |
2426 | return convert (compare_type, | |
2427 | (code == NE_EXPR | |
2428 | ? integer_one_node : integer_zero_node)); | |
2429 | } | |
2430 | } | |
2431 | else | |
2432 | { | |
2433 | tree tem = const_binop (RSHIFT_EXPR, convert (signed_type, rhs), | |
5485823f | 2434 | size_int (lbitsize - 1), 0); |
2bc77e10 | 2435 | if (! integer_zerop (tem) && ! integer_all_onesp (tem)) |
2436 | { | |
2437 | warning ("comparison is always %s due to width of bitfield", | |
2438 | code == NE_EXPR ? "one" : "zero"); | |
2439 | return convert (compare_type, | |
2440 | (code == NE_EXPR | |
2441 | ? integer_one_node : integer_zero_node)); | |
2442 | } | |
2443 | } | |
2444 | ||
2445 | /* Single-bit compares should always be against zero. */ | |
2446 | if (lbitsize == 1 && ! integer_zerop (rhs)) | |
2447 | { | |
2448 | code = code == EQ_EXPR ? NE_EXPR : EQ_EXPR; | |
2449 | rhs = convert (type, integer_zero_node); | |
2450 | } | |
2451 | ||
2452 | /* Make a new bitfield reference, shift the constant over the | |
2453 | appropriate number of bits and mask it with the computed mask | |
2454 | (in case this was a signed field). If we changed it, make a new one. */ | |
66716a97 | 2455 | lhs = make_bit_field_ref (linner, unsigned_type, lnbitsize, lnbitpos, 1); |
e03ab35e | 2456 | if (lvolatilep) |
2457 | { | |
2458 | TREE_SIDE_EFFECTS (lhs) = 1; | |
2459 | TREE_THIS_VOLATILE (lhs) = 1; | |
2460 | } | |
2bc77e10 | 2461 | |
66716a97 | 2462 | rhs = fold (const_binop (BIT_AND_EXPR, |
2463 | const_binop (LSHIFT_EXPR, | |
2464 | convert (unsigned_type, rhs), | |
eb8ae79c | 2465 | size_int (lbitpos), 0), |
5485823f | 2466 | mask, 0)); |
2bc77e10 | 2467 | |
2468 | return build (code, compare_type, | |
66716a97 | 2469 | build (BIT_AND_EXPR, unsigned_type, lhs, mask), |
2bc77e10 | 2470 | rhs); |
2471 | } | |
2472 | \f | |
79109eec | 2473 | /* Subroutine for fold_truthop: decode a field reference. |
2bc77e10 | 2474 | |
2475 | If EXP is a comparison reference, we return the innermost reference. | |
2476 | ||
2477 | *PBITSIZE is set to the number of bits in the reference, *PBITPOS is | |
2478 | set to the starting bit number. | |
2479 | ||
2480 | If the innermost field can be completely contained in a mode-sized | |
2481 | unit, *PMODE is set to that mode. Otherwise, it is set to VOIDmode. | |
2482 | ||
2483 | *PVOLATILEP is set to 1 if the any expression encountered is volatile; | |
2484 | otherwise it is not changed. | |
2485 | ||
2486 | *PUNSIGNEDP is set to the signedness of the field. | |
2487 | ||
2488 | *PMASK is set to the mask used. This is either contained in a | |
2489 | BIT_AND_EXPR or derived from the width of the field. | |
2490 | ||
2a6329ae | 2491 | *PAND_MASK is set the the mask found in a BIT_AND_EXPR, if any. |
2492 | ||
2bc77e10 | 2493 | Return 0 if this is not a component reference or is one that we can't |
2494 | do anything with. */ | |
2495 | ||
2496 | static tree | |
2497 | decode_field_reference (exp, pbitsize, pbitpos, pmode, punsignedp, | |
2a6329ae | 2498 | pvolatilep, pmask, pand_mask) |
2bc77e10 | 2499 | tree exp; |
2500 | int *pbitsize, *pbitpos; | |
2501 | enum machine_mode *pmode; | |
2502 | int *punsignedp, *pvolatilep; | |
2503 | tree *pmask; | |
2a6329ae | 2504 | tree *pand_mask; |
2bc77e10 | 2505 | { |
4843fe7c | 2506 | tree and_mask = 0; |
2507 | tree mask, inner, offset; | |
2508 | tree unsigned_type; | |
2509 | int precision; | |
2bc77e10 | 2510 | |
e40566fc | 2511 | /* All the optimizations using this function assume integer fields. |
2512 | There are problems with FP fields since the type_for_size call | |
2513 | below can fail for, e.g., XFmode. */ | |
2514 | if (! INTEGRAL_TYPE_P (TREE_TYPE (exp))) | |
2515 | return 0; | |
2516 | ||
2bc77e10 | 2517 | STRIP_NOPS (exp); |
2518 | ||
2519 | if (TREE_CODE (exp) == BIT_AND_EXPR) | |
2520 | { | |
4843fe7c | 2521 | and_mask = TREE_OPERAND (exp, 1); |
2bc77e10 | 2522 | exp = TREE_OPERAND (exp, 0); |
4843fe7c | 2523 | STRIP_NOPS (exp); STRIP_NOPS (and_mask); |
2524 | if (TREE_CODE (and_mask) != INTEGER_CST) | |
2bc77e10 | 2525 | return 0; |
2526 | } | |
2527 | ||
2bc77e10 | 2528 | |
bbfbdece | 2529 | inner = get_inner_reference (exp, pbitsize, pbitpos, &offset, pmode, |
2bc77e10 | 2530 | punsignedp, pvolatilep); |
94f29e88 | 2531 | if ((inner == exp && and_mask == 0) |
2532 | || *pbitsize < 0 || offset != 0) | |
e233264a | 2533 | return 0; |
2bc77e10 | 2534 | |
4843fe7c | 2535 | /* Compute the mask to access the bitfield. */ |
2536 | unsigned_type = type_for_size (*pbitsize, 1); | |
2537 | precision = TYPE_PRECISION (unsigned_type); | |
2538 | ||
2539 | mask = build_int_2 (~0, ~0); | |
2540 | TREE_TYPE (mask) = unsigned_type; | |
2541 | force_fit_type (mask, 0); | |
2542 | mask = const_binop (LSHIFT_EXPR, mask, size_int (precision - *pbitsize), 0); | |
2543 | mask = const_binop (RSHIFT_EXPR, mask, size_int (precision - *pbitsize), 0); | |
2544 | ||
2545 | /* Merge it with the mask we found in the BIT_AND_EXPR, if any. */ | |
2546 | if (and_mask != 0) | |
2547 | mask = fold (build (BIT_AND_EXPR, unsigned_type, | |
2548 | convert (unsigned_type, and_mask), mask)); | |
2bc77e10 | 2549 | |
2550 | *pmask = mask; | |
2a6329ae | 2551 | *pand_mask = and_mask; |
2bc77e10 | 2552 | return inner; |
2553 | } | |
2554 | ||
4bbea254 | 2555 | /* Return non-zero if MASK represents a mask of SIZE ones in the low-order |
2bc77e10 | 2556 | bit positions. */ |
2557 | ||
2558 | static int | |
2559 | all_ones_mask_p (mask, size) | |
2560 | tree mask; | |
2561 | int size; | |
2562 | { | |
2563 | tree type = TREE_TYPE (mask); | |
2564 | int precision = TYPE_PRECISION (type); | |
52a49c7c | 2565 | tree tmask; |
2bc77e10 | 2566 | |
52a49c7c | 2567 | tmask = build_int_2 (~0, ~0); |
2568 | TREE_TYPE (tmask) = signed_type (type); | |
86814797 | 2569 | force_fit_type (tmask, 0); |
2bc77e10 | 2570 | return |
94f29e88 | 2571 | tree_int_cst_equal (mask, |
2572 | const_binop (RSHIFT_EXPR, | |
2573 | const_binop (LSHIFT_EXPR, tmask, | |
2574 | size_int (precision - size), | |
2575 | 0), | |
2576 | size_int (precision - size), 0)); | |
2bc77e10 | 2577 | } |
79109eec | 2578 | |
2579 | /* Subroutine for fold_truthop: determine if an operand is simple enough | |
2580 | to be evaluated unconditionally. */ | |
2581 | ||
79109eec | 2582 | static int |
2583 | simple_operand_p (exp) | |
2584 | tree exp; | |
2585 | { | |
2586 | /* Strip any conversions that don't change the machine mode. */ | |
2587 | while ((TREE_CODE (exp) == NOP_EXPR | |
2588 | || TREE_CODE (exp) == CONVERT_EXPR) | |
2589 | && (TYPE_MODE (TREE_TYPE (exp)) | |
2590 | == TYPE_MODE (TREE_TYPE (TREE_OPERAND (exp, 0))))) | |
2591 | exp = TREE_OPERAND (exp, 0); | |
2592 | ||
2593 | return (TREE_CODE_CLASS (TREE_CODE (exp)) == 'c' | |
2594 | || (TREE_CODE_CLASS (TREE_CODE (exp)) == 'd' | |
2595 | && ! TREE_ADDRESSABLE (exp) | |
2596 | && ! TREE_THIS_VOLATILE (exp) | |
7735dddb | 2597 | && ! DECL_NONLOCAL (exp) |
2598 | /* Don't regard global variables as simple. They may be | |
2599 | allocated in ways unknown to the compiler (shared memory, | |
2600 | #pragma weak, etc). */ | |
2601 | && ! TREE_PUBLIC (exp) | |
2602 | && ! DECL_EXTERNAL (exp) | |
2603 | /* Loading a static variable is unduly expensive, but global | |
2604 | registers aren't expensive. */ | |
2605 | && (! TREE_STATIC (exp) || DECL_REGISTER (exp)))); | |
79109eec | 2606 | } |
2bc77e10 | 2607 | \f |
12ec0a8a | 2608 | /* The following functions are subroutines to fold_range_test and allow it to |
2609 | try to change a logical combination of comparisons into a range test. | |
2610 | ||
2611 | For example, both | |
2612 | X == 2 && X == 3 && X == 4 && X == 5 | |
2613 | and | |
2614 | X >= 2 && X <= 5 | |
2615 | are converted to | |
2616 | (unsigned) (X - 2) <= 3 | |
2617 | ||
2618 | We decribe each set of comparisons as being either inside or outside | |
2619 | a range, using a variable named like IN_P, and then describe the | |
2620 | range with a lower and upper bound. If one of the bounds is omitted, | |
2621 | it represents either the highest or lowest value of the type. | |
2622 | ||
2623 | In the comments below, we represent a range by two numbers in brackets | |
2624 | preceeded by a "+" to designate being inside that range, or a "-" to | |
2625 | designate being outside that range, so the condition can be inverted by | |
2626 | flipping the prefix. An omitted bound is represented by a "-". For | |
2627 | example, "- [-, 10]" means being outside the range starting at the lowest | |
2628 | possible value and ending at 10, in other words, being greater than 10. | |
2629 | The range "+ [-, -]" is always true and hence the range "- [-, -]" is | |
2630 | always false. | |
2631 | ||
2632 | We set up things so that the missing bounds are handled in a consistent | |
2633 | manner so neither a missing bound nor "true" and "false" need to be | |
2634 | handled using a special case. */ | |
2635 | ||
2636 | /* Return the result of applying CODE to ARG0 and ARG1, but handle the case | |
2637 | of ARG0 and/or ARG1 being omitted, meaning an unlimited range. UPPER0_P | |
2638 | and UPPER1_P are nonzero if the respective argument is an upper bound | |
2639 | and zero for a lower. TYPE, if nonzero, is the type of the result; it | |
2640 | must be specified for a comparison. ARG1 will be converted to ARG0's | |
2641 | type if both are specified. */ | |
6f725368 | 2642 | |
12ec0a8a | 2643 | static tree |
2644 | range_binop (code, type, arg0, upper0_p, arg1, upper1_p) | |
2645 | enum tree_code code; | |
2646 | tree type; | |
2647 | tree arg0, arg1; | |
2648 | int upper0_p, upper1_p; | |
2649 | { | |
7560c8de | 2650 | tree tem; |
12ec0a8a | 2651 | int result; |
2652 | int sgn0, sgn1; | |
6f725368 | 2653 | |
12ec0a8a | 2654 | /* If neither arg represents infinity, do the normal operation. |
2655 | Else, if not a comparison, return infinity. Else handle the special | |
2656 | comparison rules. Note that most of the cases below won't occur, but | |
2657 | are handled for consistency. */ | |
6f725368 | 2658 | |
12ec0a8a | 2659 | if (arg0 != 0 && arg1 != 0) |
7560c8de | 2660 | { |
2661 | tem = fold (build (code, type != 0 ? type : TREE_TYPE (arg0), | |
2662 | arg0, convert (TREE_TYPE (arg0), arg1))); | |
2663 | STRIP_NOPS (tem); | |
2664 | return TREE_CODE (tem) == INTEGER_CST ? tem : 0; | |
2665 | } | |
6f725368 | 2666 | |
12ec0a8a | 2667 | if (TREE_CODE_CLASS (code) != '<') |
2668 | return 0; | |
2669 | ||
2670 | /* Set SGN[01] to -1 if ARG[01] is a lower bound, 1 for upper, and 0 | |
2671 | for neither. Then compute our result treating them as never equal | |
2672 | and comparing bounds to non-bounds as above. */ | |
2673 | sgn0 = arg0 != 0 ? 0 : (upper0_p ? 1 : -1); | |
2674 | sgn1 = arg1 != 0 ? 1 : (upper1_p ? 1 : -1); | |
2675 | switch (code) | |
2676 | { | |
2677 | case EQ_EXPR: case NE_EXPR: | |
2678 | result = (code == NE_EXPR); | |
2679 | break; | |
2680 | case LT_EXPR: case LE_EXPR: | |
2681 | result = sgn0 < sgn1; | |
2682 | break; | |
2683 | case GT_EXPR: case GE_EXPR: | |
2684 | result = sgn0 > sgn1; | |
2685 | break; | |
2686 | } | |
2687 | ||
2688 | return convert (type, result ? integer_one_node : integer_zero_node); | |
2689 | } | |
2690 | \f | |
2691 | /* Given EXP, a logical expression, set the range it is testing into | |
2692 | variables denoted by PIN_P, PLOW, and PHIGH. Return the expression | |
2693 | actually being tested. *PLOW and *PHIGH will have be made the same type | |
2694 | as the returned expression. If EXP is not a comparison, we will most | |
2695 | likely not be returning a useful value and range. */ | |
6f725368 | 2696 | |
bfd67d2c | 2697 | static tree |
12ec0a8a | 2698 | make_range (exp, pin_p, plow, phigh) |
2699 | tree exp; | |
2700 | int *pin_p; | |
2701 | tree *plow, *phigh; | |
6f725368 | 2702 | { |
12ec0a8a | 2703 | enum tree_code code; |
2704 | tree arg0, arg1, type; | |
2705 | int in_p, n_in_p; | |
2706 | tree low, high, n_low, n_high; | |
6f725368 | 2707 | |
12ec0a8a | 2708 | /* Start with simply saying "EXP != 0" and then look at the code of EXP |
2709 | and see if we can refine the range. Some of the cases below may not | |
2710 | happen, but it doesn't seem worth worrying about this. We "continue" | |
2711 | the outer loop when we've changed something; otherwise we "break" | |
2712 | the switch, which will "break" the while. */ | |
6f725368 | 2713 | |
12ec0a8a | 2714 | in_p = 0, low = high = convert (TREE_TYPE (exp), integer_zero_node); |
2715 | ||
2716 | while (1) | |
6f725368 | 2717 | { |
12ec0a8a | 2718 | code = TREE_CODE (exp); |
2719 | arg0 = TREE_OPERAND (exp, 0), arg1 = TREE_OPERAND (exp, 1); | |
071d4210 | 2720 | if (arg0 != 0 && tree_code_length[(int) code] > 0) |
12ec0a8a | 2721 | type = TREE_TYPE (arg0); |
6f725368 | 2722 | |
12ec0a8a | 2723 | switch (code) |
2724 | { | |
2725 | case TRUTH_NOT_EXPR: | |
2726 | in_p = ! in_p, exp = arg0; | |
2727 | continue; | |
2728 | ||
2729 | case EQ_EXPR: case NE_EXPR: | |
2730 | case LT_EXPR: case LE_EXPR: case GE_EXPR: case GT_EXPR: | |
2731 | /* We can only do something if the range is testing for zero | |
2732 | and if the second operand is an integer constant. Note that | |
2733 | saying something is "in" the range we make is done by | |
2734 | complementing IN_P since it will set in the initial case of | |
2735 | being not equal to zero; "out" is leaving it alone. */ | |
2736 | if (low == 0 || high == 0 | |
2737 | || ! integer_zerop (low) || ! integer_zerop (high) | |
2738 | || TREE_CODE (arg1) != INTEGER_CST) | |
2739 | break; | |
6f725368 | 2740 | |
12ec0a8a | 2741 | switch (code) |
2742 | { | |
2743 | case NE_EXPR: /* - [c, c] */ | |
2744 | low = high = arg1; | |
2745 | break; | |
2746 | case EQ_EXPR: /* + [c, c] */ | |
2747 | in_p = ! in_p, low = high = arg1; | |
2748 | break; | |
2749 | case GT_EXPR: /* - [-, c] */ | |
2750 | low = 0, high = arg1; | |
2751 | break; | |
2752 | case GE_EXPR: /* + [c, -] */ | |
2753 | in_p = ! in_p, low = arg1, high = 0; | |
2754 | break; | |
2755 | case LT_EXPR: /* - [c, -] */ | |
2756 | low = arg1, high = 0; | |
2757 | break; | |
2758 | case LE_EXPR: /* + [-, c] */ | |
2759 | in_p = ! in_p, low = 0, high = arg1; | |
2760 | break; | |
2761 | } | |
6f725368 | 2762 | |
12ec0a8a | 2763 | exp = arg0; |
6f725368 | 2764 | |
c317c285 | 2765 | /* If this is an unsigned comparison, we also know that EXP is |
2766 | greater than or equal to zero and less than the maximum value of | |
2767 | the unsigned type. We base the range tests we make on that fact, | |
2768 | so we record it here so we can parse existing range tests. */ | |
2769 | if (TREE_UNSIGNED (type) && (low == 0 || high == 0)) | |
12ec0a8a | 2770 | { |
2771 | if (! merge_ranges (&n_in_p, &n_low, &n_high, in_p, low, high, | |
2772 | 1, convert (type, integer_zero_node), | |
c317c285 | 2773 | const_binop (MINUS_EXPR, |
2774 | convert (type, | |
2775 | integer_zero_node), | |
2776 | convert (type, | |
2777 | integer_one_node), | |
2778 | 0))) | |
12ec0a8a | 2779 | break; |
6f725368 | 2780 | |
12ec0a8a | 2781 | in_p = n_in_p, low = n_low, high = n_high; |
2782 | } | |
2783 | continue; | |
2784 | ||
2785 | case NEGATE_EXPR: | |
2786 | /* (-x) IN [a,b] -> x in [-b, -a] */ | |
2787 | n_low = range_binop (MINUS_EXPR, type, | |
2788 | convert (type, integer_zero_node), 0, high, 1); | |
2789 | n_high = range_binop (MINUS_EXPR, type, | |
2790 | convert (type, integer_zero_node), 0, low, 0); | |
2791 | low = n_low, high = n_high; | |
2792 | exp = arg0; | |
2793 | continue; | |
2794 | ||
2795 | case BIT_NOT_EXPR: | |
2796 | /* ~ X -> -X - 1 */ | |
2797 | exp = build (MINUS_EXPR, type, build1 (NEGATE_EXPR, type, arg0), | |
7560c8de | 2798 | convert (type, integer_one_node)); |
12ec0a8a | 2799 | continue; |
2800 | ||
2801 | case PLUS_EXPR: case MINUS_EXPR: | |
2802 | if (TREE_CODE (arg1) != INTEGER_CST) | |
2803 | break; | |
2804 | ||
2805 | /* If EXP is signed, any overflow in the computation is undefined, | |
2806 | so we don't worry about it so long as our computations on | |
2807 | the bounds don't overflow. For unsigned, overflow is defined | |
2808 | and this is exactly the right thing. */ | |
2809 | n_low = range_binop (code == MINUS_EXPR ? PLUS_EXPR : MINUS_EXPR, | |
2810 | type, low, 0, arg1, 0); | |
2811 | n_high = range_binop (code == MINUS_EXPR ? PLUS_EXPR : MINUS_EXPR, | |
2812 | type, high, 1, arg1, 0); | |
2813 | if ((n_low != 0 && TREE_OVERFLOW (n_low)) | |
2814 | || (n_high != 0 && TREE_OVERFLOW (n_high))) | |
2815 | break; | |
2816 | ||
6b457c77 | 2817 | /* Check for an unsigned range which has wrapped around the maximum |
2818 | value thus making n_high < n_low, and normalize it. */ | |
98db800f | 2819 | if (n_low && n_high && tree_int_cst_lt (n_high, n_low)) |
6b457c77 | 2820 | { |
2821 | low = range_binop (PLUS_EXPR, type, n_high, 0, | |
2822 | convert (type, integer_one_node), 0); | |
2823 | high = range_binop (MINUS_EXPR, type, n_low, 0, | |
2824 | convert (type, integer_one_node), 0); | |
2825 | in_p = ! in_p; | |
2826 | } | |
98db800f | 2827 | else |
2828 | low = n_low, high = n_high; | |
7560c8de | 2829 | |
12ec0a8a | 2830 | exp = arg0; |
2831 | continue; | |
2832 | ||
2833 | case NOP_EXPR: case NON_LVALUE_EXPR: case CONVERT_EXPR: | |
2834 | if (! INTEGRAL_TYPE_P (type) | |
2835 | || (low != 0 && ! int_fits_type_p (low, type)) | |
2836 | || (high != 0 && ! int_fits_type_p (high, type))) | |
2837 | break; | |
2838 | ||
2839 | if (low != 0) | |
2840 | low = convert (type, low); | |
2841 | ||
2842 | if (high != 0) | |
2843 | high = convert (type, high); | |
2844 | ||
2845 | exp = arg0; | |
2846 | continue; | |
6f725368 | 2847 | } |
12ec0a8a | 2848 | |
2849 | break; | |
6f725368 | 2850 | } |
12ec0a8a | 2851 | |
2852 | *pin_p = in_p, *plow = low, *phigh = high; | |
2853 | return exp; | |
2854 | } | |
2855 | \f | |
2856 | /* Given a range, LOW, HIGH, and IN_P, an expression, EXP, and a result | |
2857 | type, TYPE, return an expression to test if EXP is in (or out of, depending | |
2858 | on IN_P) the range. */ | |
2859 | ||
2860 | static tree | |
2861 | build_range_check (type, exp, in_p, low, high) | |
2862 | tree type; | |
2863 | tree exp; | |
2864 | int in_p; | |
2865 | tree low, high; | |
2866 | { | |
2867 | tree etype = TREE_TYPE (exp); | |
2868 | tree utype, value; | |
2869 | ||
2870 | if (! in_p | |
2871 | && (0 != (value = build_range_check (type, exp, 1, low, high)))) | |
2872 | return invert_truthvalue (value); | |
2873 | ||
2874 | else if (low == 0 && high == 0) | |
2875 | return convert (type, integer_one_node); | |
2876 | ||
2877 | else if (low == 0) | |
2878 | return fold (build (LE_EXPR, type, exp, high)); | |
2879 | ||
2880 | else if (high == 0) | |
2881 | return fold (build (GE_EXPR, type, exp, low)); | |
2882 | ||
2883 | else if (operand_equal_p (low, high, 0)) | |
2884 | return fold (build (EQ_EXPR, type, exp, low)); | |
2885 | ||
2886 | else if (TREE_UNSIGNED (etype) && integer_zerop (low)) | |
2887 | return build_range_check (type, exp, 1, 0, high); | |
2888 | ||
2889 | else if (integer_zerop (low)) | |
6f725368 | 2890 | { |
12ec0a8a | 2891 | utype = unsigned_type (etype); |
2892 | return build_range_check (type, convert (utype, exp), 1, 0, | |
2893 | convert (utype, high)); | |
2894 | } | |
6f725368 | 2895 | |
12ec0a8a | 2896 | else if (0 != (value = const_binop (MINUS_EXPR, high, low, 0)) |
2897 | && ! TREE_OVERFLOW (value)) | |
2898 | return build_range_check (type, | |
2899 | fold (build (MINUS_EXPR, etype, exp, low)), | |
2900 | 1, convert (etype, integer_zero_node), value); | |
2901 | else | |
2902 | return 0; | |
2903 | } | |
2904 | \f | |
2905 | /* Given two ranges, see if we can merge them into one. Return 1 if we | |
2906 | can, 0 if we can't. Set the output range into the specified parameters. */ | |
6f725368 | 2907 | |
12ec0a8a | 2908 | static int |
2909 | merge_ranges (pin_p, plow, phigh, in0_p, low0, high0, in1_p, low1, high1) | |
2910 | int *pin_p; | |
2911 | tree *plow, *phigh; | |
2912 | int in0_p, in1_p; | |
2913 | tree low0, high0, low1, high1; | |
2914 | { | |
2915 | int no_overlap; | |
2916 | int subset; | |
2917 | int temp; | |
2918 | tree tem; | |
2919 | int in_p; | |
2920 | tree low, high; | |
2921 | ||
2922 | /* Make range 0 be the range that starts first. Swap them if it isn't. */ | |
2923 | if (integer_onep (range_binop (GT_EXPR, integer_type_node, | |
2924 | low0, 0, low1, 0)) | |
2925 | || (((low0 == 0 && low1 == 0) | |
2926 | || integer_onep (range_binop (EQ_EXPR, integer_type_node, | |
2927 | low0, 0, low1, 0))) | |
2928 | && integer_onep (range_binop (GT_EXPR, integer_type_node, | |
2929 | high0, 1, high1, 1)))) | |
2930 | { | |
2931 | temp = in0_p, in0_p = in1_p, in1_p = temp; | |
2932 | tem = low0, low0 = low1, low1 = tem; | |
2933 | tem = high0, high0 = high1, high1 = tem; | |
2934 | } | |
6f725368 | 2935 | |
12ec0a8a | 2936 | /* Now flag two cases, whether the ranges are disjoint or whether the |
2937 | second range is totally subsumed in the first. Note that the tests | |
2938 | below are simplified by the ones above. */ | |
2939 | no_overlap = integer_onep (range_binop (LT_EXPR, integer_type_node, | |
2940 | high0, 1, low1, 0)); | |
718acf6d | 2941 | subset = integer_onep (range_binop (LE_EXPR, integer_type_node, |
12ec0a8a | 2942 | high1, 1, high0, 1)); |
2943 | ||
2944 | /* We now have four cases, depending on whether we are including or | |
2945 | excluding the two ranges. */ | |
2946 | if (in0_p && in1_p) | |
2947 | { | |
2948 | /* If they don't overlap, the result is false. If the second range | |
2949 | is a subset it is the result. Otherwise, the range is from the start | |
2950 | of the second to the end of the first. */ | |
2951 | if (no_overlap) | |
2952 | in_p = 0, low = high = 0; | |
2953 | else if (subset) | |
2954 | in_p = 1, low = low1, high = high1; | |
2955 | else | |
2956 | in_p = 1, low = low1, high = high0; | |
2957 | } | |
6f725368 | 2958 | |
12ec0a8a | 2959 | else if (in0_p && ! in1_p) |
2960 | { | |
2961 | /* If they don't overlap, the result is the first range. If the | |
2962 | second range is a subset of the first, we can't describe this as | |
2963 | a single range. Otherwise, we go from the start of the first | |
2964 | range to just before the start of the second. */ | |
2965 | if (no_overlap) | |
2966 | in_p = 1, low = low0, high = high0; | |
2967 | else if (subset) | |
2968 | return 0; | |
2969 | else | |
2970 | { | |
2971 | in_p = 1, low = low0; | |
2972 | high = range_binop (MINUS_EXPR, NULL_TREE, low1, 0, | |
2973 | integer_zero_node, 0); | |
2974 | } | |
2975 | } | |
6f725368 | 2976 | |
12ec0a8a | 2977 | else if (! in0_p && in1_p) |
2978 | { | |
2979 | /* If they don't overlap, the result is the second range. If the second | |
2980 | is a subset of the first, the result is false. Otherwise, | |
2981 | the range starts just after the first range and ends at the | |
2982 | end of the second. */ | |
2983 | if (no_overlap) | |
2984 | in_p = 1, low = low1, high = high1; | |
2985 | else if (subset) | |
2986 | in_p = 0, low = high = 0; | |
2987 | else | |
2988 | { | |
2989 | in_p = 1, high = high1; | |
2990 | low = range_binop (PLUS_EXPR, NULL_TREE, high0, 1, | |
2991 | integer_one_node, 0); | |
6f725368 | 2992 | } |
2993 | } | |
2994 | ||
12ec0a8a | 2995 | else |
2996 | { | |
2997 | /* The case where we are excluding both ranges. Here the complex case | |
2998 | is if they don't overlap. In that case, the only time we have a | |
2999 | range is if they are adjacent. If the second is a subset of the | |
3000 | first, the result is the first. Otherwise, the range to exclude | |
3001 | starts at the beginning of the first range and ends at the end of the | |
3002 | second. */ | |
3003 | if (no_overlap) | |
3004 | { | |
3005 | if (integer_onep (range_binop (EQ_EXPR, integer_type_node, | |
3006 | range_binop (PLUS_EXPR, NULL_TREE, | |
3007 | high0, 1, | |
3008 | integer_one_node, 1), | |
3009 | 1, low1, 0))) | |
3010 | in_p = 0, low = low0, high = high1; | |
3011 | else | |
3012 | return 0; | |
3013 | } | |
3014 | else if (subset) | |
3015 | in_p = 0, low = low0, high = high0; | |
3016 | else | |
3017 | in_p = 0, low = low0, high = high1; | |
3018 | } | |
b29eae68 | 3019 | |
12ec0a8a | 3020 | *pin_p = in_p, *plow = low, *phigh = high; |
3021 | return 1; | |
3022 | } | |
3023 | \f | |
3024 | /* EXP is some logical combination of boolean tests. See if we can | |
3025 | merge it into some range test. Return the new tree if so. */ | |
6f725368 | 3026 | |
12ec0a8a | 3027 | static tree |
3028 | fold_range_test (exp) | |
3029 | tree exp; | |
3030 | { | |
3031 | int or_op = (TREE_CODE (exp) == TRUTH_ORIF_EXPR | |
3032 | || TREE_CODE (exp) == TRUTH_OR_EXPR); | |
3033 | int in0_p, in1_p, in_p; | |
3034 | tree low0, low1, low, high0, high1, high; | |
3035 | tree lhs = make_range (TREE_OPERAND (exp, 0), &in0_p, &low0, &high0); | |
3036 | tree rhs = make_range (TREE_OPERAND (exp, 1), &in1_p, &low1, &high1); | |
3037 | tree tem; | |
6f725368 | 3038 | |
12ec0a8a | 3039 | /* If this is an OR operation, invert both sides; we will invert |
3040 | again at the end. */ | |
3041 | if (or_op) | |
3042 | in0_p = ! in0_p, in1_p = ! in1_p; | |
3043 | ||
3044 | /* If both expressions are the same, if we can merge the ranges, and we | |
3045 | can build the range test, return it or it inverted. */ | |
3046 | if (operand_equal_p (lhs, rhs, 0) | |
3047 | && merge_ranges (&in_p, &low, &high, in0_p, low0, high0, | |
3048 | in1_p, low1, high1) | |
3049 | && 0 != (tem = (build_range_check (TREE_TYPE (exp), lhs, | |
3050 | in_p, low, high)))) | |
3051 | return or_op ? invert_truthvalue (tem) : tem; | |
3052 | ||
3053 | /* On machines where the branch cost is expensive, if this is a | |
3054 | short-circuited branch and the underlying object on both sides | |
3055 | is the same, make a non-short-circuit operation. */ | |
3056 | else if (BRANCH_COST >= 2 | |
3057 | && (TREE_CODE (exp) == TRUTH_ANDIF_EXPR | |
3058 | || TREE_CODE (exp) == TRUTH_ORIF_EXPR) | |
3059 | && operand_equal_p (lhs, rhs, 0)) | |
6f725368 | 3060 | { |
12ec0a8a | 3061 | /* If simple enough, just rewrite. Otherwise, make a SAVE_EXPR. */ |
3062 | if (simple_operand_p (lhs)) | |
3063 | return build (TREE_CODE (exp) == TRUTH_ANDIF_EXPR | |
3064 | ? TRUTH_AND_EXPR : TRUTH_OR_EXPR, | |
3065 | TREE_TYPE (exp), TREE_OPERAND (exp, 0), | |
3066 | TREE_OPERAND (exp, 1)); | |
3067 | else | |
3068 | { | |
3069 | tree common = save_expr (lhs); | |
3070 | ||
3071 | if (0 != (lhs = build_range_check (TREE_TYPE (exp), common, | |
3072 | or_op ? ! in0_p : in0_p, | |
3073 | low0, high0)) | |
3074 | && (0 != (rhs = build_range_check (TREE_TYPE (exp), common, | |
3075 | or_op ? ! in1_p : in1_p, | |
3076 | low1, high1)))) | |
3077 | return build (TREE_CODE (exp) == TRUTH_ANDIF_EXPR | |
3078 | ? TRUTH_AND_EXPR : TRUTH_OR_EXPR, | |
3079 | TREE_TYPE (exp), lhs, rhs); | |
3080 | } | |
6f725368 | 3081 | } |
274f4b1d | 3082 | else |
12ec0a8a | 3083 | return 0; |
6f725368 | 3084 | } |
3085 | \f | |
94f29e88 | 3086 | /* Subroutine for fold_truthop: C is an INTEGER_CST interpreted as a P |
b2dcfbf7 | 3087 | bit value. Arrange things so the extra bits will be set to zero if and |
2a6329ae | 3088 | only if C is signed-extended to its full width. If MASK is nonzero, |
3089 | it is an INTEGER_CST that should be AND'ed with the extra bits. */ | |
94f29e88 | 3090 | |
3091 | static tree | |
2a6329ae | 3092 | unextend (c, p, unsignedp, mask) |
94f29e88 | 3093 | tree c; |
3094 | int p; | |
3095 | int unsignedp; | |
2a6329ae | 3096 | tree mask; |
94f29e88 | 3097 | { |
3098 | tree type = TREE_TYPE (c); | |
3099 | int modesize = GET_MODE_BITSIZE (TYPE_MODE (type)); | |
3100 | tree temp; | |
3101 | ||
3102 | if (p == modesize || unsignedp) | |
3103 | return c; | |
3104 | ||
3105 | if (TREE_UNSIGNED (type)) | |
3106 | c = convert (signed_type (type), c); | |
3107 | ||
3108 | /* We work by getting just the sign bit into the low-order bit, then | |
c3418f42 | 3109 | into the high-order bit, then sign-extend. We then XOR that value |
94f29e88 | 3110 | with C. */ |
3111 | temp = const_binop (RSHIFT_EXPR, c, size_int (p - 1), 0); | |
3112 | temp = const_binop (BIT_AND_EXPR, temp, size_int (1), 0); | |
3113 | temp = const_binop (LSHIFT_EXPR, temp, size_int (modesize - 1), 0); | |
3114 | temp = const_binop (RSHIFT_EXPR, temp, size_int (modesize - p - 1), 0); | |
2a6329ae | 3115 | if (mask != 0) |
3116 | temp = const_binop (BIT_AND_EXPR, temp, convert (TREE_TYPE (c), mask), 0); | |
3117 | ||
94f29e88 | 3118 | return convert (type, const_binop (BIT_XOR_EXPR, c, temp, 0)); |
3119 | } | |
3120 | \f | |
79109eec | 3121 | /* Find ways of folding logical expressions of LHS and RHS: |
3122 | Try to merge two comparisons to the same innermost item. | |
3123 | Look for range tests like "ch >= '0' && ch <= '9'". | |
3124 | Look for combinations of simple terms on machines with expensive branches | |
3125 | and evaluate the RHS unconditionally. | |
2bc77e10 | 3126 | |
3127 | For example, if we have p->a == 2 && p->b == 4 and we can make an | |
3128 | object large enough to span both A and B, we can do this with a comparison | |
3129 | against the object ANDed with the a mask. | |
3130 | ||
3131 | If we have p->a == q->a && p->b == q->b, we may be able to use bit masking | |
3132 | operations to do this with one comparison. | |
3133 | ||
3134 | We check for both normal comparisons and the BIT_AND_EXPRs made this by | |
3135 | function and the one above. | |
3136 | ||
3137 | CODE is the logical operation being done. It can be TRUTH_ANDIF_EXPR, | |
3138 | TRUTH_AND_EXPR, TRUTH_ORIF_EXPR, or TRUTH_OR_EXPR. | |
3139 | ||
3140 | TRUTH_TYPE is the type of the logical operand and LHS and RHS are its | |
3141 | two operands. | |
3142 | ||
3143 | We return the simplified tree or 0 if no optimization is possible. */ | |
3144 | ||
3145 | static tree | |
79109eec | 3146 | fold_truthop (code, truth_type, lhs, rhs) |
2bc77e10 | 3147 | enum tree_code code; |
3148 | tree truth_type, lhs, rhs; | |
3149 | { | |
3150 | /* If this is the "or" of two comparisons, we can do something if we | |
3151 | the comparisons are NE_EXPR. If this is the "and", we can do something | |
3152 | if the comparisons are EQ_EXPR. I.e., | |
3153 | (a->b == 2 && a->c == 4) can become (a->new == NEW). | |
3154 | ||
3155 | WANTED_CODE is this operation code. For single bit fields, we can | |
3156 | convert EQ_EXPR to NE_EXPR so we need not reject the "wrong" | |
3157 | comparison for one-bit fields. */ | |
3158 | ||
79109eec | 3159 | enum tree_code wanted_code; |
2bc77e10 | 3160 | enum tree_code lcode, rcode; |
79109eec | 3161 | tree ll_arg, lr_arg, rl_arg, rr_arg; |
2bc77e10 | 3162 | tree ll_inner, lr_inner, rl_inner, rr_inner; |
3163 | int ll_bitsize, ll_bitpos, lr_bitsize, lr_bitpos; | |
3164 | int rl_bitsize, rl_bitpos, rr_bitsize, rr_bitpos; | |
3165 | int xll_bitpos, xlr_bitpos, xrl_bitpos, xrr_bitpos; | |
3166 | int lnbitsize, lnbitpos, rnbitsize, rnbitpos; | |
3167 | int ll_unsignedp, lr_unsignedp, rl_unsignedp, rr_unsignedp; | |
3168 | enum machine_mode ll_mode, lr_mode, rl_mode, rr_mode; | |
3169 | enum machine_mode lnmode, rnmode; | |
3170 | tree ll_mask, lr_mask, rl_mask, rr_mask; | |
2a6329ae | 3171 | tree ll_and_mask, lr_and_mask, rl_and_mask, rr_and_mask; |
79109eec | 3172 | tree l_const, r_const; |
2bc77e10 | 3173 | tree type, result; |
3174 | int first_bit, end_bit; | |
79109eec | 3175 | int volatilep; |
2bc77e10 | 3176 | |
12ec0a8a | 3177 | /* Start by getting the comparison codes. Fail if anything is volatile. |
3178 | If one operand is a BIT_AND_EXPR with the constant one, treat it as if | |
3179 | it were surrounded with a NE_EXPR. */ | |
2bc77e10 | 3180 | |
12ec0a8a | 3181 | if (TREE_SIDE_EFFECTS (lhs) || TREE_SIDE_EFFECTS (rhs)) |
79109eec | 3182 | return 0; |
3183 | ||
2bc77e10 | 3184 | lcode = TREE_CODE (lhs); |
3185 | rcode = TREE_CODE (rhs); | |
6f725368 | 3186 | |
b5ab1edd | 3187 | if (lcode == BIT_AND_EXPR && integer_onep (TREE_OPERAND (lhs, 1))) |
3188 | lcode = NE_EXPR, lhs = build (NE_EXPR, truth_type, lhs, integer_zero_node); | |
3189 | ||
3190 | if (rcode == BIT_AND_EXPR && integer_onep (TREE_OPERAND (rhs, 1))) | |
3191 | rcode = NE_EXPR, rhs = build (NE_EXPR, truth_type, rhs, integer_zero_node); | |
3192 | ||
12ec0a8a | 3193 | if (TREE_CODE_CLASS (lcode) != '<' || TREE_CODE_CLASS (rcode) != '<') |
6f725368 | 3194 | return 0; |
3195 | ||
79109eec | 3196 | code = ((code == TRUTH_AND_EXPR || code == TRUTH_ANDIF_EXPR) |
76e4a18b | 3197 | ? TRUTH_AND_EXPR : TRUTH_OR_EXPR); |
79109eec | 3198 | |
3199 | ll_arg = TREE_OPERAND (lhs, 0); | |
3200 | lr_arg = TREE_OPERAND (lhs, 1); | |
3201 | rl_arg = TREE_OPERAND (rhs, 0); | |
3202 | rr_arg = TREE_OPERAND (rhs, 1); | |
3203 | ||
7735dddb | 3204 | /* If the RHS can be evaluated unconditionally and its operands are |
79109eec | 3205 | simple, it wins to evaluate the RHS unconditionally on machines |
3206 | with expensive branches. In this case, this isn't a comparison | |
3207 | that can be merged. */ | |
3208 | ||
3209 | /* @@ I'm not sure it wins on the m88110 to do this if the comparisons | |
3210 | are with zero (tmw). */ | |
3211 | ||
3212 | if (BRANCH_COST >= 2 | |
780a4395 | 3213 | && INTEGRAL_TYPE_P (TREE_TYPE (rhs)) |
79109eec | 3214 | && simple_operand_p (rl_arg) |
7735dddb | 3215 | && simple_operand_p (rr_arg)) |
79109eec | 3216 | return build (code, truth_type, lhs, rhs); |
3217 | ||
6f725368 | 3218 | /* See if the comparisons can be merged. Then get all the parameters for |
3219 | each side. */ | |
3220 | ||
2bc77e10 | 3221 | if ((lcode != EQ_EXPR && lcode != NE_EXPR) |
6f725368 | 3222 | || (rcode != EQ_EXPR && rcode != NE_EXPR)) |
2bc77e10 | 3223 | return 0; |
3224 | ||
79109eec | 3225 | volatilep = 0; |
3226 | ll_inner = decode_field_reference (ll_arg, | |
2bc77e10 | 3227 | &ll_bitsize, &ll_bitpos, &ll_mode, |
2a6329ae | 3228 | &ll_unsignedp, &volatilep, &ll_mask, |
3229 | &ll_and_mask); | |
79109eec | 3230 | lr_inner = decode_field_reference (lr_arg, |
2bc77e10 | 3231 | &lr_bitsize, &lr_bitpos, &lr_mode, |
2a6329ae | 3232 | &lr_unsignedp, &volatilep, &lr_mask, |
3233 | &lr_and_mask); | |
79109eec | 3234 | rl_inner = decode_field_reference (rl_arg, |
2bc77e10 | 3235 | &rl_bitsize, &rl_bitpos, &rl_mode, |
2a6329ae | 3236 | &rl_unsignedp, &volatilep, &rl_mask, |
3237 | &rl_and_mask); | |
79109eec | 3238 | rr_inner = decode_field_reference (rr_arg, |
2bc77e10 | 3239 | &rr_bitsize, &rr_bitpos, &rr_mode, |
2a6329ae | 3240 | &rr_unsignedp, &volatilep, &rr_mask, |
3241 | &rr_and_mask); | |
2bc77e10 | 3242 | |
3243 | /* It must be true that the inner operation on the lhs of each | |
3244 | comparison must be the same if we are to be able to do anything. | |
3245 | Then see if we have constants. If not, the same must be true for | |
3246 | the rhs's. */ | |
3247 | if (volatilep || ll_inner == 0 || rl_inner == 0 | |
3248 | || ! operand_equal_p (ll_inner, rl_inner, 0)) | |
3249 | return 0; | |
3250 | ||
79109eec | 3251 | if (TREE_CODE (lr_arg) == INTEGER_CST |
3252 | && TREE_CODE (rr_arg) == INTEGER_CST) | |
3253 | l_const = lr_arg, r_const = rr_arg; | |
2bc77e10 | 3254 | else if (lr_inner == 0 || rr_inner == 0 |
3255 | || ! operand_equal_p (lr_inner, rr_inner, 0)) | |
3256 | return 0; | |
79109eec | 3257 | else |
3258 | l_const = r_const = 0; | |
2bc77e10 | 3259 | |
3260 | /* If either comparison code is not correct for our logical operation, | |
3261 | fail. However, we can convert a one-bit comparison against zero into | |
3262 | the opposite comparison against that bit being set in the field. */ | |
79109eec | 3263 | |
76e4a18b | 3264 | wanted_code = (code == TRUTH_AND_EXPR ? EQ_EXPR : NE_EXPR); |
2bc77e10 | 3265 | if (lcode != wanted_code) |
3266 | { | |
3267 | if (l_const && integer_zerop (l_const) && integer_pow2p (ll_mask)) | |
3268 | l_const = ll_mask; | |
3269 | else | |
3270 | return 0; | |
3271 | } | |
3272 | ||
3273 | if (rcode != wanted_code) | |
3274 | { | |
3275 | if (r_const && integer_zerop (r_const) && integer_pow2p (rl_mask)) | |
3276 | r_const = rl_mask; | |
3277 | else | |
3278 | return 0; | |
3279 | } | |
3280 | ||
3281 | /* See if we can find a mode that contains both fields being compared on | |
3282 | the left. If we can't, fail. Otherwise, update all constants and masks | |
3283 | to be relative to a field of that size. */ | |
3284 | first_bit = MIN (ll_bitpos, rl_bitpos); | |
3285 | end_bit = MAX (ll_bitpos + ll_bitsize, rl_bitpos + rl_bitsize); | |
3286 | lnmode = get_best_mode (end_bit - first_bit, first_bit, | |
3287 | TYPE_ALIGN (TREE_TYPE (ll_inner)), word_mode, | |
3288 | volatilep); | |
3289 | if (lnmode == VOIDmode) | |
3290 | return 0; | |
3291 | ||
3292 | lnbitsize = GET_MODE_BITSIZE (lnmode); | |
3293 | lnbitpos = first_bit & ~ (lnbitsize - 1); | |
3294 | type = type_for_size (lnbitsize, 1); | |
3295 | xll_bitpos = ll_bitpos - lnbitpos, xrl_bitpos = rl_bitpos - lnbitpos; | |
3296 | ||
51356f86 | 3297 | if (BYTES_BIG_ENDIAN) |
3298 | { | |
3299 | xll_bitpos = lnbitsize - xll_bitpos - ll_bitsize; | |
3300 | xrl_bitpos = lnbitsize - xrl_bitpos - rl_bitsize; | |
3301 | } | |
2bc77e10 | 3302 | |
3303 | ll_mask = const_binop (LSHIFT_EXPR, convert (type, ll_mask), | |
5485823f | 3304 | size_int (xll_bitpos), 0); |
2bc77e10 | 3305 | rl_mask = const_binop (LSHIFT_EXPR, convert (type, rl_mask), |
5485823f | 3306 | size_int (xrl_bitpos), 0); |
2bc77e10 | 3307 | |
2bc77e10 | 3308 | if (l_const) |
3309 | { | |
2a6329ae | 3310 | l_const = convert (type, l_const); |
3311 | l_const = unextend (l_const, ll_bitsize, ll_unsignedp, ll_and_mask); | |
94f29e88 | 3312 | l_const = const_binop (LSHIFT_EXPR, l_const, size_int (xll_bitpos), 0); |
3313 | if (! integer_zerop (const_binop (BIT_AND_EXPR, l_const, | |
3314 | fold (build1 (BIT_NOT_EXPR, | |
3315 | type, ll_mask)), | |
3316 | 0))) | |
3317 | { | |
3318 | warning ("comparison is always %s", | |
3319 | wanted_code == NE_EXPR ? "one" : "zero"); | |
3320 | ||
3321 | return convert (truth_type, | |
3322 | wanted_code == NE_EXPR | |
3323 | ? integer_one_node : integer_zero_node); | |
3324 | } | |
2bc77e10 | 3325 | } |
3326 | if (r_const) | |
3327 | { | |
2a6329ae | 3328 | r_const = convert (type, r_const); |
3329 | r_const = unextend (r_const, rl_bitsize, rl_unsignedp, rl_and_mask); | |
94f29e88 | 3330 | r_const = const_binop (LSHIFT_EXPR, r_const, size_int (xrl_bitpos), 0); |
3331 | if (! integer_zerop (const_binop (BIT_AND_EXPR, r_const, | |
3332 | fold (build1 (BIT_NOT_EXPR, | |
3333 | type, rl_mask)), | |
3334 | 0))) | |
3335 | { | |
3336 | warning ("comparison is always %s", | |
3337 | wanted_code == NE_EXPR ? "one" : "zero"); | |
3338 | ||
3339 | return convert (truth_type, | |
3340 | wanted_code == NE_EXPR | |
3341 | ? integer_one_node : integer_zero_node); | |
3342 | } | |
2bc77e10 | 3343 | } |
3344 | ||
3345 | /* If the right sides are not constant, do the same for it. Also, | |
3346 | disallow this optimization if a size or signedness mismatch occurs | |
3347 | between the left and right sides. */ | |
3348 | if (l_const == 0) | |
3349 | { | |
3350 | if (ll_bitsize != lr_bitsize || rl_bitsize != rr_bitsize | |
15e4fe21 | 3351 | || ll_unsignedp != lr_unsignedp || rl_unsignedp != rr_unsignedp |
3352 | /* Make sure the two fields on the right | |
3353 | correspond to the left without being swapped. */ | |
3354 | || ll_bitpos - rl_bitpos != lr_bitpos - rr_bitpos) | |
2bc77e10 | 3355 | return 0; |
3356 | ||
3357 | first_bit = MIN (lr_bitpos, rr_bitpos); | |
3358 | end_bit = MAX (lr_bitpos + lr_bitsize, rr_bitpos + rr_bitsize); | |
3359 | rnmode = get_best_mode (end_bit - first_bit, first_bit, | |
3360 | TYPE_ALIGN (TREE_TYPE (lr_inner)), word_mode, | |
3361 | volatilep); | |
3362 | if (rnmode == VOIDmode) | |
3363 | return 0; | |
3364 | ||
3365 | rnbitsize = GET_MODE_BITSIZE (rnmode); | |
3366 | rnbitpos = first_bit & ~ (rnbitsize - 1); | |
3367 | xlr_bitpos = lr_bitpos - rnbitpos, xrr_bitpos = rr_bitpos - rnbitpos; | |
3368 | ||
51356f86 | 3369 | if (BYTES_BIG_ENDIAN) |
3370 | { | |
3371 | xlr_bitpos = rnbitsize - xlr_bitpos - lr_bitsize; | |
3372 | xrr_bitpos = rnbitsize - xrr_bitpos - rr_bitsize; | |
3373 | } | |
2bc77e10 | 3374 | |
3375 | lr_mask = const_binop (LSHIFT_EXPR, convert (type, lr_mask), | |
5485823f | 3376 | size_int (xlr_bitpos), 0); |
2bc77e10 | 3377 | rr_mask = const_binop (LSHIFT_EXPR, convert (type, rr_mask), |
5485823f | 3378 | size_int (xrr_bitpos), 0); |
2bc77e10 | 3379 | |
3380 | /* Make a mask that corresponds to both fields being compared. | |
3381 | Do this for both items being compared. If the masks agree, | |
3382 | we can do this by masking both and comparing the masked | |
3383 | results. */ | |
5485823f | 3384 | ll_mask = const_binop (BIT_IOR_EXPR, ll_mask, rl_mask, 0); |
3385 | lr_mask = const_binop (BIT_IOR_EXPR, lr_mask, rr_mask, 0); | |
2bc77e10 | 3386 | if (operand_equal_p (ll_mask, lr_mask, 0) && lnbitsize == rnbitsize) |
3387 | { | |
3388 | lhs = make_bit_field_ref (ll_inner, type, lnbitsize, lnbitpos, | |
3389 | ll_unsignedp || rl_unsignedp); | |
3390 | rhs = make_bit_field_ref (lr_inner, type, rnbitsize, rnbitpos, | |
3391 | lr_unsignedp || rr_unsignedp); | |
3392 | if (! all_ones_mask_p (ll_mask, lnbitsize)) | |
3393 | { | |
3394 | lhs = build (BIT_AND_EXPR, type, lhs, ll_mask); | |
3395 | rhs = build (BIT_AND_EXPR, type, rhs, ll_mask); | |
3396 | } | |
3397 | return build (wanted_code, truth_type, lhs, rhs); | |
3398 | } | |
3399 | ||
3400 | /* There is still another way we can do something: If both pairs of | |
3401 | fields being compared are adjacent, we may be able to make a wider | |
3402 | field containing them both. */ | |
3403 | if ((ll_bitsize + ll_bitpos == rl_bitpos | |
3404 | && lr_bitsize + lr_bitpos == rr_bitpos) | |
3405 | || (ll_bitpos == rl_bitpos + rl_bitsize | |
3406 | && lr_bitpos == rr_bitpos + rr_bitsize)) | |
3407 | return build (wanted_code, truth_type, | |
3408 | make_bit_field_ref (ll_inner, type, | |
3409 | ll_bitsize + rl_bitsize, | |
3410 | MIN (ll_bitpos, rl_bitpos), | |
3411 | ll_unsignedp), | |
3412 | make_bit_field_ref (lr_inner, type, | |
3413 | lr_bitsize + rr_bitsize, | |
3414 | MIN (lr_bitpos, rr_bitpos), | |
3415 | lr_unsignedp)); | |
3416 | ||
3417 | return 0; | |
3418 | } | |
3419 | ||
3420 | /* Handle the case of comparisons with constants. If there is something in | |
3421 | common between the masks, those bits of the constants must be the same. | |
3422 | If not, the condition is always false. Test for this to avoid generating | |
3423 | incorrect code below. */ | |
5485823f | 3424 | result = const_binop (BIT_AND_EXPR, ll_mask, rl_mask, 0); |
2bc77e10 | 3425 | if (! integer_zerop (result) |
5485823f | 3426 | && simple_cst_equal (const_binop (BIT_AND_EXPR, result, l_const, 0), |
3427 | const_binop (BIT_AND_EXPR, result, r_const, 0)) != 1) | |
2bc77e10 | 3428 | { |
3429 | if (wanted_code == NE_EXPR) | |
3430 | { | |
3431 | warning ("`or' of unmatched not-equal tests is always 1"); | |
3432 | return convert (truth_type, integer_one_node); | |
3433 | } | |
3434 | else | |
3435 | { | |
3436 | warning ("`and' of mutually exclusive equal-tests is always zero"); | |
3437 | return convert (truth_type, integer_zero_node); | |
3438 | } | |
3439 | } | |
3440 | ||
3441 | /* Construct the expression we will return. First get the component | |
3442 | reference we will make. Unless the mask is all ones the width of | |
3443 | that field, perform the mask operation. Then compare with the | |
3444 | merged constant. */ | |
3445 | result = make_bit_field_ref (ll_inner, type, lnbitsize, lnbitpos, | |
3446 | ll_unsignedp || rl_unsignedp); | |
3447 | ||
5485823f | 3448 | ll_mask = const_binop (BIT_IOR_EXPR, ll_mask, rl_mask, 0); |
2bc77e10 | 3449 | if (! all_ones_mask_p (ll_mask, lnbitsize)) |
3450 | result = build (BIT_AND_EXPR, type, result, ll_mask); | |
3451 | ||
3452 | return build (wanted_code, truth_type, result, | |
5485823f | 3453 | const_binop (BIT_IOR_EXPR, l_const, r_const, 0)); |
2bc77e10 | 3454 | } |
3455 | \f | |
58a718ca | 3456 | /* If T contains a COMPOUND_EXPR which was inserted merely to evaluate |
3457 | S, a SAVE_EXPR, return the expression actually being evaluated. Note | |
3458 | that we may sometimes modify the tree. */ | |
3459 | ||
3460 | static tree | |
3461 | strip_compound_expr (t, s) | |
3462 | tree t; | |
3463 | tree s; | |
3464 | { | |
3465 | tree type = TREE_TYPE (t); | |
3466 | enum tree_code code = TREE_CODE (t); | |
3467 | ||
3468 | /* See if this is the COMPOUND_EXPR we want to eliminate. */ | |
3469 | if (code == COMPOUND_EXPR && TREE_CODE (TREE_OPERAND (t, 0)) == CONVERT_EXPR | |
3470 | && TREE_OPERAND (TREE_OPERAND (t, 0), 0) == s) | |
3471 | return TREE_OPERAND (t, 1); | |
3472 | ||
3473 | /* See if this is a COND_EXPR or a simple arithmetic operator. We | |
3474 | don't bother handling any other types. */ | |
3475 | else if (code == COND_EXPR) | |
3476 | { | |
3477 | TREE_OPERAND (t, 0) = strip_compound_expr (TREE_OPERAND (t, 0), s); | |
3478 | TREE_OPERAND (t, 1) = strip_compound_expr (TREE_OPERAND (t, 1), s); | |
3479 | TREE_OPERAND (t, 2) = strip_compound_expr (TREE_OPERAND (t, 2), s); | |
3480 | } | |
3481 | else if (TREE_CODE_CLASS (code) == '1') | |
3482 | TREE_OPERAND (t, 0) = strip_compound_expr (TREE_OPERAND (t, 0), s); | |
3483 | else if (TREE_CODE_CLASS (code) == '<' | |
3484 | || TREE_CODE_CLASS (code) == '2') | |
3485 | { | |
3486 | TREE_OPERAND (t, 0) = strip_compound_expr (TREE_OPERAND (t, 0), s); | |
3487 | TREE_OPERAND (t, 1) = strip_compound_expr (TREE_OPERAND (t, 1), s); | |
3488 | } | |
3489 | ||
3490 | return t; | |
3491 | } | |
3492 | \f | |
2bc77e10 | 3493 | /* Perform constant folding and related simplification of EXPR. |
3494 | The related simplifications include x*1 => x, x*0 => 0, etc., | |
3495 | and application of the associative law. | |
3496 | NOP_EXPR conversions may be removed freely (as long as we | |
3497 | are careful not to change the C type of the overall expression) | |
3498 | We cannot simplify through a CONVERT_EXPR, FIX_EXPR or FLOAT_EXPR, | |
3499 | but we can constant-fold them if they have constant operands. */ | |
3500 | ||
3501 | tree | |
3502 | fold (expr) | |
3503 | tree expr; | |
3504 | { | |
3505 | register tree t = expr; | |
3506 | tree t1 = NULL_TREE; | |
e233264a | 3507 | tree tem; |
2bc77e10 | 3508 | tree type = TREE_TYPE (expr); |
3509 | register tree arg0, arg1; | |
3510 | register enum tree_code code = TREE_CODE (t); | |
3511 | register int kind; | |
e233264a | 3512 | int invert; |
2bc77e10 | 3513 | |
3514 | /* WINS will be nonzero when the switch is done | |
3515 | if all operands are constant. */ | |
3516 | ||
3517 | int wins = 1; | |
3518 | ||
14a7560f | 3519 | /* Don't try to process an RTL_EXPR since its operands aren't trees. |
3520 | Likewise for a SAVE_EXPR that's already been evaluated. */ | |
3521 | if (code == RTL_EXPR || (code == SAVE_EXPR && SAVE_EXPR_RTL (t)) != 0) | |
e9d8d238 | 3522 | return t; |
3523 | ||
2bc77e10 | 3524 | /* Return right away if already constant. */ |
3525 | if (TREE_CONSTANT (t)) | |
3526 | { | |
3527 | if (code == CONST_DECL) | |
3528 | return DECL_INITIAL (t); | |
3529 | return t; | |
3530 | } | |
3531 | ||
3532 | kind = TREE_CODE_CLASS (code); | |
233c0cbd | 3533 | if (code == NOP_EXPR || code == FLOAT_EXPR || code == CONVERT_EXPR) |
3534 | { | |
bb6b5123 | 3535 | tree subop; |
3536 | ||
233c0cbd | 3537 | /* Special case for conversion ops that can have fixed point args. */ |
3538 | arg0 = TREE_OPERAND (t, 0); | |
3539 | ||
3540 | /* Don't use STRIP_NOPS, because signedness of argument type matters. */ | |
3541 | if (arg0 != 0) | |
3542 | STRIP_TYPE_NOPS (arg0); | |
3543 | ||
bb6b5123 | 3544 | if (arg0 != 0 && TREE_CODE (arg0) == COMPLEX_CST) |
3545 | subop = TREE_REALPART (arg0); | |
3546 | else | |
3547 | subop = arg0; | |
3548 | ||
3549 | if (subop != 0 && TREE_CODE (subop) != INTEGER_CST | |
233c0cbd | 3550 | #if ! defined (REAL_IS_NOT_DOUBLE) || defined (REAL_ARITHMETIC) |
bb6b5123 | 3551 | && TREE_CODE (subop) != REAL_CST |
233c0cbd | 3552 | #endif /* not REAL_IS_NOT_DOUBLE, or REAL_ARITHMETIC */ |
3553 | ) | |
3554 | /* Note that TREE_CONSTANT isn't enough: | |
3555 | static var addresses are constant but we can't | |
3556 | do arithmetic on them. */ | |
3557 | wins = 0; | |
3558 | } | |
3559 | else if (kind == 'e' || kind == '<' | |
3560 | || kind == '1' || kind == '2' || kind == 'r') | |
2bc77e10 | 3561 | { |
3562 | register int len = tree_code_length[(int) code]; | |
3563 | register int i; | |
3564 | for (i = 0; i < len; i++) | |
3565 | { | |
3566 | tree op = TREE_OPERAND (t, i); | |
bb6b5123 | 3567 | tree subop; |
2bc77e10 | 3568 | |
3569 | if (op == 0) | |
3570 | continue; /* Valid for CALL_EXPR, at least. */ | |
3571 | ||
c2cbd9a8 | 3572 | if (kind == '<' || code == RSHIFT_EXPR) |
3573 | { | |
3574 | /* Signedness matters here. Perhaps we can refine this | |
3575 | later. */ | |
3576 | STRIP_TYPE_NOPS (op); | |
3577 | } | |
3578 | else | |
3579 | { | |
3580 | /* Strip any conversions that don't change the mode. */ | |
3581 | STRIP_NOPS (op); | |
3582 | } | |
2bc77e10 | 3583 | |
bb6b5123 | 3584 | if (TREE_CODE (op) == COMPLEX_CST) |
3585 | subop = TREE_REALPART (op); | |
3586 | else | |
3587 | subop = op; | |
3588 | ||
3589 | if (TREE_CODE (subop) != INTEGER_CST | |
2bc77e10 | 3590 | #if ! defined (REAL_IS_NOT_DOUBLE) || defined (REAL_ARITHMETIC) |
bb6b5123 | 3591 | && TREE_CODE (subop) != REAL_CST |
2bc77e10 | 3592 | #endif /* not REAL_IS_NOT_DOUBLE, or REAL_ARITHMETIC */ |
3593 | ) | |
3594 | /* Note that TREE_CONSTANT isn't enough: | |
3595 | static var addresses are constant but we can't | |
3596 | do arithmetic on them. */ | |
3597 | wins = 0; | |
3598 | ||
3599 | if (i == 0) | |
3600 | arg0 = op; | |
3601 | else if (i == 1) | |
3602 | arg1 = op; | |
3603 | } | |
3604 | } | |
3605 | ||
3606 | /* If this is a commutative operation, and ARG0 is a constant, move it | |
3607 | to ARG1 to reduce the number of tests below. */ | |
3608 | if ((code == PLUS_EXPR || code == MULT_EXPR || code == MIN_EXPR | |
3609 | || code == MAX_EXPR || code == BIT_IOR_EXPR || code == BIT_XOR_EXPR | |
3610 | || code == BIT_AND_EXPR) | |
3611 | && (TREE_CODE (arg0) == INTEGER_CST || TREE_CODE (arg0) == REAL_CST)) | |
3612 | { | |
e233264a | 3613 | tem = arg0; arg0 = arg1; arg1 = tem; |
2bc77e10 | 3614 | |
e233264a | 3615 | tem = TREE_OPERAND (t, 0); TREE_OPERAND (t, 0) = TREE_OPERAND (t, 1); |
3616 | TREE_OPERAND (t, 1) = tem; | |
2bc77e10 | 3617 | } |
3618 | ||
3619 | /* Now WINS is set as described above, | |
3620 | ARG0 is the first operand of EXPR, | |
3621 | and ARG1 is the second operand (if it has more than one operand). | |
3622 | ||
3623 | First check for cases where an arithmetic operation is applied to a | |
3624 | compound, conditional, or comparison operation. Push the arithmetic | |
3625 | operation inside the compound or conditional to see if any folding | |
3626 | can then be done. Convert comparison to conditional for this purpose. | |
3627 | The also optimizes non-constant cases that used to be done in | |
b5ab1edd | 3628 | expand_expr. |
3629 | ||
3630 | Before we do that, see if this is a BIT_AND_EXPR or a BIT_OR_EXPR, | |
8b94828f | 3631 | one of the operands is a comparison and the other is a comparison, a |
3632 | BIT_AND_EXPR with the constant 1, or a truth value. In that case, the | |
3633 | code below would make the expression more complex. Change it to a | |
5c0dba00 | 3634 | TRUTH_{AND,OR}_EXPR. Likewise, convert a similar NE_EXPR to |
3635 | TRUTH_XOR_EXPR and an EQ_EXPR to the inversion of a TRUTH_XOR_EXPR. */ | |
b5ab1edd | 3636 | |
5c0dba00 | 3637 | if ((code == BIT_AND_EXPR || code == BIT_IOR_EXPR |
3638 | || code == EQ_EXPR || code == NE_EXPR) | |
8b94828f | 3639 | && ((truth_value_p (TREE_CODE (arg0)) |
3640 | && (truth_value_p (TREE_CODE (arg1)) | |
b5ab1edd | 3641 | || (TREE_CODE (arg1) == BIT_AND_EXPR |
3642 | && integer_onep (TREE_OPERAND (arg1, 1))))) | |
8b94828f | 3643 | || (truth_value_p (TREE_CODE (arg1)) |
3644 | && (truth_value_p (TREE_CODE (arg0)) | |
b5ab1edd | 3645 | || (TREE_CODE (arg0) == BIT_AND_EXPR |
3646 | && integer_onep (TREE_OPERAND (arg0, 1))))))) | |
5c0dba00 | 3647 | { |
3648 | t = fold (build (code == BIT_AND_EXPR ? TRUTH_AND_EXPR | |
3649 | : code == BIT_IOR_EXPR ? TRUTH_OR_EXPR | |
3650 | : TRUTH_XOR_EXPR, | |
3651 | type, arg0, arg1)); | |
3652 | ||
3653 | if (code == EQ_EXPR) | |
3654 | t = invert_truthvalue (t); | |
3655 | ||
3656 | return t; | |
3657 | } | |
b5ab1edd | 3658 | |
2bc77e10 | 3659 | if (TREE_CODE_CLASS (code) == '1') |
3660 | { | |
3661 | if (TREE_CODE (arg0) == COMPOUND_EXPR) | |
3662 | return build (COMPOUND_EXPR, type, TREE_OPERAND (arg0, 0), | |
3663 | fold (build1 (code, type, TREE_OPERAND (arg0, 1)))); | |
3664 | else if (TREE_CODE (arg0) == COND_EXPR) | |
abd9ac9c | 3665 | { |
3666 | t = fold (build (COND_EXPR, type, TREE_OPERAND (arg0, 0), | |
3667 | fold (build1 (code, type, TREE_OPERAND (arg0, 1))), | |
3668 | fold (build1 (code, type, TREE_OPERAND (arg0, 2))))); | |
3669 | ||
3670 | /* If this was a conversion, and all we did was to move into | |
2483911d | 3671 | inside the COND_EXPR, bring it back out. But leave it if |
3672 | it is a conversion from integer to integer and the | |
3673 | result precision is no wider than a word since such a | |
3674 | conversion is cheap and may be optimized away by combine, | |
3675 | while it couldn't if it were outside the COND_EXPR. Then return | |
3676 | so we don't get into an infinite recursion loop taking the | |
3677 | conversion out and then back in. */ | |
abd9ac9c | 3678 | |
3679 | if ((code == NOP_EXPR || code == CONVERT_EXPR | |
3680 | || code == NON_LVALUE_EXPR) | |
3681 | && TREE_CODE (t) == COND_EXPR | |
3682 | && TREE_CODE (TREE_OPERAND (t, 1)) == code | |
6e23378f | 3683 | && TREE_CODE (TREE_OPERAND (t, 2)) == code |
3684 | && (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (t, 1), 0)) | |
2483911d | 3685 | == TREE_TYPE (TREE_OPERAND (TREE_OPERAND (t, 2), 0))) |
3686 | && ! (INTEGRAL_TYPE_P (TREE_TYPE (t)) | |
3687 | && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (t, 1), 0))) | |
3688 | && TYPE_PRECISION (TREE_TYPE (t)) <= BITS_PER_WORD)) | |
abd9ac9c | 3689 | t = build1 (code, type, |
3690 | build (COND_EXPR, | |
3691 | TREE_TYPE (TREE_OPERAND (TREE_OPERAND (t, 1), 0)), | |
3692 | TREE_OPERAND (t, 0), | |
3693 | TREE_OPERAND (TREE_OPERAND (t, 1), 0), | |
3694 | TREE_OPERAND (TREE_OPERAND (t, 2), 0))); | |
3695 | return t; | |
3696 | } | |
2bc77e10 | 3697 | else if (TREE_CODE_CLASS (TREE_CODE (arg0)) == '<') |
3698 | return fold (build (COND_EXPR, type, arg0, | |
3699 | fold (build1 (code, type, integer_one_node)), | |
3700 | fold (build1 (code, type, integer_zero_node)))); | |
3701 | } | |
b5ab1edd | 3702 | else if (TREE_CODE_CLASS (code) == '2' |
3703 | || TREE_CODE_CLASS (code) == '<') | |
2bc77e10 | 3704 | { |
3705 | if (TREE_CODE (arg1) == COMPOUND_EXPR) | |
3706 | return build (COMPOUND_EXPR, type, TREE_OPERAND (arg1, 0), | |
b5ab1edd | 3707 | fold (build (code, type, |
3708 | arg0, TREE_OPERAND (arg1, 1)))); | |
2bc77e10 | 3709 | else if (TREE_CODE (arg1) == COND_EXPR |
3710 | || TREE_CODE_CLASS (TREE_CODE (arg1)) == '<') | |
3711 | { | |
3712 | tree test, true_value, false_value; | |
3713 | ||
3714 | if (TREE_CODE (arg1) == COND_EXPR) | |
3715 | { | |
3716 | test = TREE_OPERAND (arg1, 0); | |
3717 | true_value = TREE_OPERAND (arg1, 1); | |
3718 | false_value = TREE_OPERAND (arg1, 2); | |
3719 | } | |
3720 | else | |
3721 | { | |
b2dcfbf7 | 3722 | tree testtype = TREE_TYPE (arg1); |
2bc77e10 | 3723 | test = arg1; |
b2dcfbf7 | 3724 | true_value = convert (testtype, integer_one_node); |
3725 | false_value = convert (testtype, integer_zero_node); | |
2bc77e10 | 3726 | } |
3727 | ||
b5ab1edd | 3728 | /* If ARG0 is complex we want to make sure we only evaluate |
3729 | it once. Though this is only required if it is volatile, it | |
3730 | might be more efficient even if it is not. However, if we | |
3731 | succeed in folding one part to a constant, we do not need | |
3732 | to make this SAVE_EXPR. Since we do this optimization | |
3733 | primarily to see if we do end up with constant and this | |
c3418f42 | 3734 | SAVE_EXPR interferes with later optimizations, suppressing |
b5ab1edd | 3735 | it when we can is important. */ |
3736 | ||
58a718ca | 3737 | if (TREE_CODE (arg0) != SAVE_EXPR |
3738 | && ((TREE_CODE (arg0) != VAR_DECL | |
3739 | && TREE_CODE (arg0) != PARM_DECL) | |
3740 | || TREE_SIDE_EFFECTS (arg0))) | |
b5ab1edd | 3741 | { |
3742 | tree lhs = fold (build (code, type, arg0, true_value)); | |
3743 | tree rhs = fold (build (code, type, arg0, false_value)); | |
3744 | ||
3745 | if (TREE_CONSTANT (lhs) || TREE_CONSTANT (rhs)) | |
3746 | return fold (build (COND_EXPR, type, test, lhs, rhs)); | |
3747 | ||
3748 | arg0 = save_expr (arg0); | |
3749 | } | |
3750 | ||
2bc77e10 | 3751 | test = fold (build (COND_EXPR, type, test, |
3752 | fold (build (code, type, arg0, true_value)), | |
3753 | fold (build (code, type, arg0, false_value)))); | |
3754 | if (TREE_CODE (arg0) == SAVE_EXPR) | |
3755 | return build (COMPOUND_EXPR, type, | |
58a718ca | 3756 | convert (void_type_node, arg0), |
3757 | strip_compound_expr (test, arg0)); | |
2bc77e10 | 3758 | else |
3759 | return convert (type, test); | |
3760 | } | |
3761 | ||
3762 | else if (TREE_CODE (arg0) == COMPOUND_EXPR) | |
3763 | return build (COMPOUND_EXPR, type, TREE_OPERAND (arg0, 0), | |
3764 | fold (build (code, type, TREE_OPERAND (arg0, 1), arg1))); | |
3765 | else if (TREE_CODE (arg0) == COND_EXPR | |
3766 | || TREE_CODE_CLASS (TREE_CODE (arg0)) == '<') | |
3767 | { | |
3768 | tree test, true_value, false_value; | |
3769 | ||
3770 | if (TREE_CODE (arg0) == COND_EXPR) | |
3771 | { | |
3772 | test = TREE_OPERAND (arg0, 0); | |
3773 | true_value = TREE_OPERAND (arg0, 1); | |
3774 | false_value = TREE_OPERAND (arg0, 2); | |
3775 | } | |
3776 | else | |
3777 | { | |
b2dcfbf7 | 3778 | tree testtype = TREE_TYPE (arg0); |
2bc77e10 | 3779 | test = arg0; |
b2dcfbf7 | 3780 | true_value = convert (testtype, integer_one_node); |
3781 | false_value = convert (testtype, integer_zero_node); | |
2bc77e10 | 3782 | } |
3783 | ||
58a718ca | 3784 | if (TREE_CODE (arg1) != SAVE_EXPR |
3785 | && ((TREE_CODE (arg1) != VAR_DECL | |
3786 | && TREE_CODE (arg1) != PARM_DECL) | |
3787 | || TREE_SIDE_EFFECTS (arg1))) | |
b5ab1edd | 3788 | { |
3789 | tree lhs = fold (build (code, type, true_value, arg1)); | |
3790 | tree rhs = fold (build (code, type, false_value, arg1)); | |
3791 | ||
21ca12f8 | 3792 | if (TREE_CONSTANT (lhs) || TREE_CONSTANT (rhs) |
3793 | || TREE_CONSTANT (arg1)) | |
b5ab1edd | 3794 | return fold (build (COND_EXPR, type, test, lhs, rhs)); |
3795 | ||
3796 | arg1 = save_expr (arg1); | |
3797 | } | |
3798 | ||
2bc77e10 | 3799 | test = fold (build (COND_EXPR, type, test, |
3800 | fold (build (code, type, true_value, arg1)), | |
3801 | fold (build (code, type, false_value, arg1)))); | |
3802 | if (TREE_CODE (arg1) == SAVE_EXPR) | |
3803 | return build (COMPOUND_EXPR, type, | |
58a718ca | 3804 | convert (void_type_node, arg1), |
3805 | strip_compound_expr (test, arg1)); | |
2bc77e10 | 3806 | else |
3807 | return convert (type, test); | |
3808 | } | |
3809 | } | |
e233264a | 3810 | else if (TREE_CODE_CLASS (code) == '<' |
3811 | && TREE_CODE (arg0) == COMPOUND_EXPR) | |
3812 | return build (COMPOUND_EXPR, type, TREE_OPERAND (arg0, 0), | |
3813 | fold (build (code, type, TREE_OPERAND (arg0, 1), arg1))); | |
3814 | else if (TREE_CODE_CLASS (code) == '<' | |
3815 | && TREE_CODE (arg1) == COMPOUND_EXPR) | |
3816 | return build (COMPOUND_EXPR, type, TREE_OPERAND (arg1, 0), | |
3817 | fold (build (code, type, arg0, TREE_OPERAND (arg1, 1)))); | |
2bc77e10 | 3818 | |
3819 | switch (code) | |
3820 | { | |
3821 | case INTEGER_CST: | |
3822 | case REAL_CST: | |
3823 | case STRING_CST: | |
3824 | case COMPLEX_CST: | |
3825 | case CONSTRUCTOR: | |
3826 | return t; | |
3827 | ||
3828 | case CONST_DECL: | |
3829 | return fold (DECL_INITIAL (t)); | |
3830 | ||
3831 | case NOP_EXPR: | |
3832 | case FLOAT_EXPR: | |
3833 | case CONVERT_EXPR: | |
3834 | case FIX_TRUNC_EXPR: | |
3835 | /* Other kinds of FIX are not handled properly by fold_convert. */ | |
4c608263 | 3836 | |
2483911d | 3837 | if (TREE_TYPE (TREE_OPERAND (t, 0)) == TREE_TYPE (t)) |
3838 | return TREE_OPERAND (t, 0); | |
3839 | ||
fa4ebe56 | 3840 | /* Handle cases of two conversions in a row. */ |
3841 | if (TREE_CODE (TREE_OPERAND (t, 0)) == NOP_EXPR | |
3842 | || TREE_CODE (TREE_OPERAND (t, 0)) == CONVERT_EXPR) | |
3843 | { | |
3844 | tree inside_type = TREE_TYPE (TREE_OPERAND (TREE_OPERAND (t, 0), 0)); | |
3845 | tree inter_type = TREE_TYPE (TREE_OPERAND (t, 0)); | |
3846 | tree final_type = TREE_TYPE (t); | |
3847 | int inside_int = INTEGRAL_TYPE_P (inside_type); | |
cc58e392 | 3848 | int inside_ptr = POINTER_TYPE_P (inside_type); |
fa4ebe56 | 3849 | int inside_float = FLOAT_TYPE_P (inside_type); |
3850 | int inside_prec = TYPE_PRECISION (inside_type); | |
3851 | int inside_unsignedp = TREE_UNSIGNED (inside_type); | |
3852 | int inter_int = INTEGRAL_TYPE_P (inter_type); | |
cc58e392 | 3853 | int inter_ptr = POINTER_TYPE_P (inter_type); |
fa4ebe56 | 3854 | int inter_float = FLOAT_TYPE_P (inter_type); |
3855 | int inter_prec = TYPE_PRECISION (inter_type); | |
3856 | int inter_unsignedp = TREE_UNSIGNED (inter_type); | |
3857 | int final_int = INTEGRAL_TYPE_P (final_type); | |
cc58e392 | 3858 | int final_ptr = POINTER_TYPE_P (final_type); |
fa4ebe56 | 3859 | int final_float = FLOAT_TYPE_P (final_type); |
3860 | int final_prec = TYPE_PRECISION (final_type); | |
3861 | int final_unsignedp = TREE_UNSIGNED (final_type); | |
3862 | ||
3863 | /* In addition to the cases of two conversions in a row | |
3864 | handled below, if we are converting something to its own | |
3865 | type via an object of identical or wider precision, neither | |
3866 | conversion is needed. */ | |
3867 | if (inside_type == final_type | |
3868 | && ((inter_int && final_int) || (inter_float && final_float)) | |
3869 | && inter_prec >= final_prec) | |
3870 | return TREE_OPERAND (TREE_OPERAND (t, 0), 0); | |
3871 | ||
3872 | /* Likewise, if the intermediate and final types are either both | |
3873 | float or both integer, we don't need the middle conversion if | |
3874 | it is wider than the final type and doesn't change the signedness | |
cc58e392 | 3875 | (for integers). Avoid this if the final type is a pointer |
7cf60649 | 3876 | since then we sometimes need the inner conversion. Likewise if |
3877 | the outer has a precision not equal to the size of its mode. */ | |
fa4ebe56 | 3878 | if ((((inter_int || inter_ptr) && (inside_int || inside_ptr)) |
3879 | || (inter_float && inside_float)) | |
3880 | && inter_prec >= inside_prec | |
cc58e392 | 3881 | && (inter_float || inter_unsignedp == inside_unsignedp) |
7cf60649 | 3882 | && ! (final_prec != GET_MODE_BITSIZE (TYPE_MODE (final_type)) |
3883 | && TYPE_MODE (final_type) == TYPE_MODE (inter_type)) | |
cc58e392 | 3884 | && ! final_ptr) |
fa4ebe56 | 3885 | return convert (final_type, TREE_OPERAND (TREE_OPERAND (t, 0), 0)); |
3886 | ||
3887 | /* Two conversions in a row are not needed unless: | |
3888 | - some conversion is floating-point (overstrict for now), or | |
3889 | - the intermediate type is narrower than both initial and | |
3890 | final, or | |
3891 | - the intermediate type and innermost type differ in signedness, | |
3892 | and the outermost type is wider than the intermediate, or | |
3893 | - the initial type is a pointer type and the precisions of the | |
3894 | intermediate and final types differ, or | |
3895 | - the final type is a pointer type and the precisions of the | |
3896 | initial and intermediate types differ. */ | |
3897 | if (! inside_float && ! inter_float && ! final_float | |
3898 | && (inter_prec > inside_prec || inter_prec > final_prec) | |
3899 | && ! (inside_int && inter_int | |
3900 | && inter_unsignedp != inside_unsignedp | |
3901 | && inter_prec < final_prec) | |
3902 | && ((inter_unsignedp && inter_prec > inside_prec) | |
3903 | == (final_unsignedp && final_prec > inter_prec)) | |
3904 | && ! (inside_ptr && inter_prec != final_prec) | |
7cf60649 | 3905 | && ! (final_ptr && inside_prec != inter_prec) |
3906 | && ! (final_prec != GET_MODE_BITSIZE (TYPE_MODE (final_type)) | |
3907 | && TYPE_MODE (final_type) == TYPE_MODE (inter_type)) | |
3908 | && ! final_ptr) | |
fa4ebe56 | 3909 | return convert (final_type, TREE_OPERAND (TREE_OPERAND (t, 0), 0)); |
3910 | } | |
2bc77e10 | 3911 | |
3912 | if (TREE_CODE (TREE_OPERAND (t, 0)) == MODIFY_EXPR | |
e70afa40 | 3913 | && TREE_CONSTANT (TREE_OPERAND (TREE_OPERAND (t, 0), 1)) |
3914 | /* Detect assigning a bitfield. */ | |
3915 | && !(TREE_CODE (TREE_OPERAND (TREE_OPERAND (t, 0), 0)) == COMPONENT_REF | |
3916 | && DECL_BIT_FIELD (TREE_OPERAND (TREE_OPERAND (TREE_OPERAND (t, 0), 0), 1)))) | |
2bc77e10 | 3917 | { |
e70afa40 | 3918 | /* Don't leave an assignment inside a conversion |
eb2f80f3 | 3919 | unless assigning a bitfield. */ |
2bc77e10 | 3920 | tree prev = TREE_OPERAND (t, 0); |
3921 | TREE_OPERAND (t, 0) = TREE_OPERAND (prev, 1); | |
3922 | /* First do the assignment, then return converted constant. */ | |
3923 | t = build (COMPOUND_EXPR, TREE_TYPE (t), prev, fold (t)); | |
3924 | TREE_USED (t) = 1; | |
3925 | return t; | |
3926 | } | |
3927 | if (!wins) | |
3928 | { | |
3929 | TREE_CONSTANT (t) = TREE_CONSTANT (arg0); | |
3930 | return t; | |
3931 | } | |
3932 | return fold_convert (t, arg0); | |
3933 | ||
3934 | #if 0 /* This loses on &"foo"[0]. */ | |
3935 | case ARRAY_REF: | |
3936 | { | |
3937 | int i; | |
3938 | ||
3939 | /* Fold an expression like: "foo"[2] */ | |
3940 | if (TREE_CODE (arg0) == STRING_CST | |
3941 | && TREE_CODE (arg1) == INTEGER_CST | |
3942 | && !TREE_INT_CST_HIGH (arg1) | |
3943 | && (i = TREE_INT_CST_LOW (arg1)) < TREE_STRING_LENGTH (arg0)) | |
3944 | { | |
3945 | t = build_int_2 (TREE_STRING_POINTER (arg0)[i], 0); | |
3946 | TREE_TYPE (t) = TREE_TYPE (TREE_TYPE (arg0)); | |
817e5691 | 3947 | force_fit_type (t, 0); |
2bc77e10 | 3948 | } |
3949 | } | |
3950 | return t; | |
3951 | #endif /* 0 */ | |
3952 | ||
09a738e9 | 3953 | case COMPONENT_REF: |
3954 | if (TREE_CODE (arg0) == CONSTRUCTOR) | |
f5541a8b | 3955 | { |
3956 | tree m = purpose_member (arg1, CONSTRUCTOR_ELTS (arg0)); | |
3957 | if (m) | |
3958 | t = TREE_VALUE (m); | |
3959 | } | |
09a738e9 | 3960 | return t; |
3961 | ||
2bc77e10 | 3962 | case RANGE_EXPR: |
3963 | TREE_CONSTANT (t) = wins; | |
3964 | return t; | |
3965 | ||
3966 | case NEGATE_EXPR: | |
3967 | if (wins) | |
3968 | { | |
3969 | if (TREE_CODE (arg0) == INTEGER_CST) | |
3970 | { | |
b9e999f0 | 3971 | HOST_WIDE_INT low, high; |
3972 | int overflow = neg_double (TREE_INT_CST_LOW (arg0), | |
3973 | TREE_INT_CST_HIGH (arg0), | |
3974 | &low, &high); | |
3975 | t = build_int_2 (low, high); | |
2bc77e10 | 3976 | TREE_TYPE (t) = type; |
f17f1965 | 3977 | TREE_OVERFLOW (t) |
3978 | = (TREE_OVERFLOW (arg0) | |
3f430a71 | 3979 | | force_fit_type (t, overflow && !TREE_UNSIGNED (type))); |
f17f1965 | 3980 | TREE_CONSTANT_OVERFLOW (t) |
3981 | = TREE_OVERFLOW (t) | TREE_CONSTANT_OVERFLOW (arg0); | |
2bc77e10 | 3982 | } |
3983 | else if (TREE_CODE (arg0) == REAL_CST) | |
3984 | t = build_real (type, REAL_VALUE_NEGATE (TREE_REAL_CST (arg0))); | |
3985 | TREE_TYPE (t) = type; | |
3986 | } | |
3987 | else if (TREE_CODE (arg0) == NEGATE_EXPR) | |
3988 | return TREE_OPERAND (arg0, 0); | |
3989 | ||
3990 | /* Convert - (a - b) to (b - a) for non-floating-point. */ | |
780a4395 | 3991 | else if (TREE_CODE (arg0) == MINUS_EXPR && ! FLOAT_TYPE_P (type)) |
2bc77e10 | 3992 | return build (MINUS_EXPR, type, TREE_OPERAND (arg0, 1), |
3993 | TREE_OPERAND (arg0, 0)); | |
3994 | ||
3995 | return t; | |
3996 | ||
3997 | case ABS_EXPR: | |
3998 | if (wins) | |
3999 | { | |
4000 | if (TREE_CODE (arg0) == INTEGER_CST) | |
4001 | { | |
4002 | if (! TREE_UNSIGNED (type) | |
4003 | && TREE_INT_CST_HIGH (arg0) < 0) | |
4004 | { | |
f52f18a3 | 4005 | HOST_WIDE_INT low, high; |
4006 | int overflow = neg_double (TREE_INT_CST_LOW (arg0), | |
4007 | TREE_INT_CST_HIGH (arg0), | |
4008 | &low, &high); | |
4009 | t = build_int_2 (low, high); | |
4010 | TREE_TYPE (t) = type; | |
f17f1965 | 4011 | TREE_OVERFLOW (t) |
4012 | = (TREE_OVERFLOW (arg0) | |
f55401f0 | 4013 | | force_fit_type (t, overflow)); |
f17f1965 | 4014 | TREE_CONSTANT_OVERFLOW (t) |
4015 | = TREE_OVERFLOW (t) | TREE_CONSTANT_OVERFLOW (arg0); | |
2bc77e10 | 4016 | } |
4017 | } | |
4018 | else if (TREE_CODE (arg0) == REAL_CST) | |
4019 | { | |
e233264a | 4020 | if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg0))) |
2bc77e10 | 4021 | t = build_real (type, |
4022 | REAL_VALUE_NEGATE (TREE_REAL_CST (arg0))); | |
4023 | } | |
4024 | TREE_TYPE (t) = type; | |
4025 | } | |
4026 | else if (TREE_CODE (arg0) == ABS_EXPR || TREE_CODE (arg0) == NEGATE_EXPR) | |
4027 | return build1 (ABS_EXPR, type, TREE_OPERAND (arg0, 0)); | |
4028 | return t; | |
4029 | ||
03aa4df2 | 4030 | case CONJ_EXPR: |
4031 | if (TREE_CODE (TREE_TYPE (arg0)) != COMPLEX_TYPE) | |
4032 | return arg0; | |
4033 | else if (TREE_CODE (arg0) == COMPLEX_EXPR) | |
4034 | return build (COMPLEX_EXPR, TREE_TYPE (arg0), | |
4035 | TREE_OPERAND (arg0, 0), | |
4036 | fold (build1 (NEGATE_EXPR, | |
4037 | TREE_TYPE (TREE_TYPE (arg0)), | |
4038 | TREE_OPERAND (arg0, 1)))); | |
4039 | else if (TREE_CODE (arg0) == COMPLEX_CST) | |
5b2ade4d | 4040 | return build_complex (type, TREE_OPERAND (arg0, 0), |
03aa4df2 | 4041 | fold (build1 (NEGATE_EXPR, |
4042 | TREE_TYPE (TREE_TYPE (arg0)), | |
4043 | TREE_OPERAND (arg0, 1)))); | |
4044 | else if (TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR) | |
4045 | return fold (build (TREE_CODE (arg0), type, | |
4046 | fold (build1 (CONJ_EXPR, type, | |
4047 | TREE_OPERAND (arg0, 0))), | |
4048 | fold (build1 (CONJ_EXPR, | |
4049 | type, TREE_OPERAND (arg0, 1))))); | |
4050 | else if (TREE_CODE (arg0) == CONJ_EXPR) | |
4051 | return TREE_OPERAND (arg0, 0); | |
4052 | return t; | |
4053 | ||
2bc77e10 | 4054 | case BIT_NOT_EXPR: |
4055 | if (wins) | |
4056 | { | |
4057 | if (TREE_CODE (arg0) == INTEGER_CST) | |
4058 | t = build_int_2 (~ TREE_INT_CST_LOW (arg0), | |
4059 | ~ TREE_INT_CST_HIGH (arg0)); | |
4060 | TREE_TYPE (t) = type; | |
f55401f0 | 4061 | force_fit_type (t, 0); |
f17f1965 | 4062 | TREE_OVERFLOW (t) = TREE_OVERFLOW (arg0); |
b9e999f0 | 4063 | TREE_CONSTANT_OVERFLOW (t) = TREE_CONSTANT_OVERFLOW (arg0); |
2bc77e10 | 4064 | } |
4065 | else if (TREE_CODE (arg0) == BIT_NOT_EXPR) | |
4066 | return TREE_OPERAND (arg0, 0); | |
4067 | return t; | |
4068 | ||
4069 | case PLUS_EXPR: | |
4070 | /* A + (-B) -> A - B */ | |
4071 | if (TREE_CODE (arg1) == NEGATE_EXPR) | |
4072 | return fold (build (MINUS_EXPR, type, arg0, TREE_OPERAND (arg1, 0))); | |
780a4395 | 4073 | else if (! FLOAT_TYPE_P (type)) |
2bc77e10 | 4074 | { |
4075 | if (integer_zerop (arg1)) | |
4076 | return non_lvalue (convert (type, arg0)); | |
4077 | ||
4078 | /* If we are adding two BIT_AND_EXPR's, both of which are and'ing | |
4079 | with a constant, and the two constants have no bits in common, | |
4080 | we should treat this as a BIT_IOR_EXPR since this may produce more | |
4081 | simplifications. */ | |
4082 | if (TREE_CODE (arg0) == BIT_AND_EXPR | |
4083 | && TREE_CODE (arg1) == BIT_AND_EXPR | |
4084 | && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST | |
4085 | && TREE_CODE (TREE_OPERAND (arg1, 1)) == INTEGER_CST | |
4086 | && integer_zerop (const_binop (BIT_AND_EXPR, | |
4087 | TREE_OPERAND (arg0, 1), | |
5485823f | 4088 | TREE_OPERAND (arg1, 1), 0))) |
2bc77e10 | 4089 | { |
4090 | code = BIT_IOR_EXPR; | |
4091 | goto bit_ior; | |
4092 | } | |
e4142c0f | 4093 | |
4094 | /* (A * C) + (B * C) -> (A+B) * C. Since we are most concerned | |
4095 | about the case where C is a constant, just try one of the | |
4096 | four possibilities. */ | |
4097 | ||
4098 | if (TREE_CODE (arg0) == MULT_EXPR && TREE_CODE (arg1) == MULT_EXPR | |
4099 | && operand_equal_p (TREE_OPERAND (arg0, 1), | |
4100 | TREE_OPERAND (arg1, 1), 0)) | |
4101 | return fold (build (MULT_EXPR, type, | |
4102 | fold (build (PLUS_EXPR, type, | |
4103 | TREE_OPERAND (arg0, 0), | |
4104 | TREE_OPERAND (arg1, 0))), | |
4105 | TREE_OPERAND (arg0, 1))); | |
2bc77e10 | 4106 | } |
4107 | /* In IEEE floating point, x+0 may not equal x. */ | |
700d3e4f | 4108 | else if ((TARGET_FLOAT_FORMAT != IEEE_FLOAT_FORMAT |
4109 | || flag_fast_math) | |
2bc77e10 | 4110 | && real_zerop (arg1)) |
4111 | return non_lvalue (convert (type, arg0)); | |
4112 | associate: | |
4113 | /* In most languages, can't associate operations on floats | |
4114 | through parentheses. Rather than remember where the parentheses | |
0f586b9b | 4115 | were, we don't associate floats at all. It shouldn't matter much. |
4116 | However, associating multiplications is only very slightly | |
4117 | inaccurate, so do that if -ffast-math is specified. */ | |
4118 | if (FLOAT_TYPE_P (type) | |
4119 | && ! (flag_fast_math && code == MULT_EXPR)) | |
2bc77e10 | 4120 | goto binary; |
0f586b9b | 4121 | |
2bc77e10 | 4122 | /* The varsign == -1 cases happen only for addition and subtraction. |
4123 | It says that the arg that was split was really CON minus VAR. | |
4124 | The rest of the code applies to all associative operations. */ | |
4125 | if (!wins) | |
4126 | { | |
e233264a | 4127 | tree var, con; |
2bc77e10 | 4128 | int varsign; |
4129 | ||
4130 | if (split_tree (arg0, code, &var, &con, &varsign)) | |
4131 | { | |
4132 | if (varsign == -1) | |
4133 | { | |
4134 | /* EXPR is (CON-VAR) +- ARG1. */ | |
4135 | /* If it is + and VAR==ARG1, return just CONST. */ | |
4136 | if (code == PLUS_EXPR && operand_equal_p (var, arg1, 0)) | |
4137 | return convert (TREE_TYPE (t), con); | |
4138 | ||
0ad421ca | 4139 | /* If ARG0 is a constant, don't change things around; |
4140 | instead keep all the constant computations together. */ | |
4141 | ||
4142 | if (TREE_CONSTANT (arg0)) | |
4143 | return t; | |
4144 | ||
2bc77e10 | 4145 | /* Otherwise return (CON +- ARG1) - VAR. */ |
f13f9c7f | 4146 | t = build (MINUS_EXPR, type, |
4147 | fold (build (code, type, con, arg1)), var); | |
2bc77e10 | 4148 | } |
4149 | else | |
4150 | { | |
4151 | /* EXPR is (VAR+CON) +- ARG1. */ | |
4152 | /* If it is - and VAR==ARG1, return just CONST. */ | |
4153 | if (code == MINUS_EXPR && operand_equal_p (var, arg1, 0)) | |
4154 | return convert (TREE_TYPE (t), con); | |
4155 | ||
0ad421ca | 4156 | /* If ARG0 is a constant, don't change things around; |
4157 | instead keep all the constant computations together. */ | |
4158 | ||
4159 | if (TREE_CONSTANT (arg0)) | |
4160 | return t; | |
4161 | ||
2bc77e10 | 4162 | /* Otherwise return VAR +- (ARG1 +- CON). */ |
f13f9c7f | 4163 | tem = fold (build (code, type, arg1, con)); |
4164 | t = build (code, type, var, tem); | |
4165 | ||
2bc77e10 | 4166 | if (integer_zerop (tem) |
4167 | && (code == PLUS_EXPR || code == MINUS_EXPR)) | |
4168 | return convert (type, var); | |
4169 | /* If we have x +/- (c - d) [c an explicit integer] | |
4170 | change it to x -/+ (d - c) since if d is relocatable | |
4171 | then the latter can be a single immediate insn | |
4172 | and the former cannot. */ | |
4173 | if (TREE_CODE (tem) == MINUS_EXPR | |
4174 | && TREE_CODE (TREE_OPERAND (tem, 0)) == INTEGER_CST) | |
4175 | { | |
4176 | tree tem1 = TREE_OPERAND (tem, 1); | |
4177 | TREE_OPERAND (tem, 1) = TREE_OPERAND (tem, 0); | |
4178 | TREE_OPERAND (tem, 0) = tem1; | |
4179 | TREE_SET_CODE (t, | |
4180 | (code == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR)); | |
4181 | } | |
4182 | } | |
4183 | return t; | |
4184 | } | |
4185 | ||
4186 | if (split_tree (arg1, code, &var, &con, &varsign)) | |
4187 | { | |
b5d70b4f | 4188 | if (TREE_CONSTANT (arg1)) |
4189 | return t; | |
4190 | ||
4191 | if (varsign == -1) | |
4192 | TREE_SET_CODE (t, | |
4193 | (code == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR)); | |
4194 | ||
2bc77e10 | 4195 | /* EXPR is ARG0 +- (CON +- VAR). */ |
2bc77e10 | 4196 | if (TREE_CODE (t) == MINUS_EXPR |
4197 | && operand_equal_p (var, arg0, 0)) | |
4198 | { | |
4199 | /* If VAR and ARG0 cancel, return just CON or -CON. */ | |
4200 | if (code == PLUS_EXPR) | |
4201 | return convert (TREE_TYPE (t), con); | |
4202 | return fold (build1 (NEGATE_EXPR, TREE_TYPE (t), | |
4203 | convert (TREE_TYPE (t), con))); | |
4204 | } | |
b5d70b4f | 4205 | |
f13f9c7f | 4206 | t = build (TREE_CODE (t), type, |
4207 | fold (build (code, TREE_TYPE (t), arg0, con)), var); | |
4208 | ||
2bc77e10 | 4209 | if (integer_zerop (TREE_OPERAND (t, 0)) |
4210 | && TREE_CODE (t) == PLUS_EXPR) | |
4211 | return convert (TREE_TYPE (t), var); | |
4212 | return t; | |
4213 | } | |
4214 | } | |
4215 | binary: | |
4216 | #if defined (REAL_IS_NOT_DOUBLE) && ! defined (REAL_ARITHMETIC) | |
4217 | if (TREE_CODE (arg1) == REAL_CST) | |
4218 | return t; | |
4219 | #endif /* REAL_IS_NOT_DOUBLE, and no REAL_ARITHMETIC */ | |
4220 | if (wins) | |
5485823f | 4221 | t1 = const_binop (code, arg0, arg1, 0); |
2bc77e10 | 4222 | if (t1 != NULL_TREE) |
4223 | { | |
4224 | /* The return value should always have | |
4225 | the same type as the original expression. */ | |
4226 | TREE_TYPE (t1) = TREE_TYPE (t); | |
4227 | return t1; | |
4228 | } | |
4229 | return t; | |
4230 | ||
4231 | case MINUS_EXPR: | |
780a4395 | 4232 | if (! FLOAT_TYPE_P (type)) |
2bc77e10 | 4233 | { |
4234 | if (! wins && integer_zerop (arg0)) | |
4235 | return build1 (NEGATE_EXPR, type, arg1); | |
4236 | if (integer_zerop (arg1)) | |
4237 | return non_lvalue (convert (type, arg0)); | |
e4142c0f | 4238 | |
4239 | /* (A * C) - (B * C) -> (A-B) * C. Since we are most concerned | |
4240 | about the case where C is a constant, just try one of the | |
4241 | four possibilities. */ | |
4242 | ||
4243 | if (TREE_CODE (arg0) == MULT_EXPR && TREE_CODE (arg1) == MULT_EXPR | |
4244 | && operand_equal_p (TREE_OPERAND (arg0, 1), | |
4245 | TREE_OPERAND (arg1, 1), 0)) | |
4246 | return fold (build (MULT_EXPR, type, | |
4247 | fold (build (MINUS_EXPR, type, | |
4248 | TREE_OPERAND (arg0, 0), | |
4249 | TREE_OPERAND (arg1, 0))), | |
4250 | TREE_OPERAND (arg0, 1))); | |
2bc77e10 | 4251 | } |
4252 | /* Convert A - (-B) to A + B. */ | |
4253 | else if (TREE_CODE (arg1) == NEGATE_EXPR) | |
4254 | return fold (build (PLUS_EXPR, type, arg0, TREE_OPERAND (arg1, 0))); | |
8045c7c3 | 4255 | |
4256 | else if (TARGET_FLOAT_FORMAT != IEEE_FLOAT_FORMAT | |
4257 | || flag_fast_math) | |
2bc77e10 | 4258 | { |
e233264a | 4259 | /* Except with IEEE floating point, 0-x equals -x. */ |
2bc77e10 | 4260 | if (! wins && real_zerop (arg0)) |
4261 | return build1 (NEGATE_EXPR, type, arg1); | |
e233264a | 4262 | /* Except with IEEE floating point, x-0 equals x. */ |
4263 | if (real_zerop (arg1)) | |
2bc77e10 | 4264 | return non_lvalue (convert (type, arg0)); |
8045c7c3 | 4265 | } |
b2c6bec0 | 4266 | |
8045c7c3 | 4267 | /* Fold &x - &x. This can happen from &x.foo - &x. |
4268 | This is unsafe for certain floats even in non-IEEE formats. | |
4269 | In IEEE, it is unsafe because it does wrong for NaNs. | |
4270 | Also note that operand_equal_p is always false if an operand | |
4271 | is volatile. */ | |
4272 | ||
d3d5ed2a | 4273 | if ((! FLOAT_TYPE_P (type) || flag_fast_math) |
4274 | && operand_equal_p (arg0, arg1, 0)) | |
8045c7c3 | 4275 | return convert (type, integer_zero_node); |
b2c6bec0 | 4276 | |
2bc77e10 | 4277 | goto associate; |
4278 | ||
4279 | case MULT_EXPR: | |
780a4395 | 4280 | if (! FLOAT_TYPE_P (type)) |
2bc77e10 | 4281 | { |
4282 | if (integer_zerop (arg1)) | |
4283 | return omit_one_operand (type, arg1, arg0); | |
4284 | if (integer_onep (arg1)) | |
4285 | return non_lvalue (convert (type, arg0)); | |
4286 | ||
43bb0a16 | 4287 | /* ((A / C) * C) is A if the division is an |
4288 | EXACT_DIV_EXPR. Since C is normally a constant, | |
4289 | just check for one of the four possibilities. */ | |
4290 | ||
4291 | if (TREE_CODE (arg0) == EXACT_DIV_EXPR | |
4292 | && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0)) | |
4293 | return TREE_OPERAND (arg0, 0); | |
4294 | ||
2bc77e10 | 4295 | /* (a * (1 << b)) is (a << b) */ |
4296 | if (TREE_CODE (arg1) == LSHIFT_EXPR | |
4297 | && integer_onep (TREE_OPERAND (arg1, 0))) | |
4298 | return fold (build (LSHIFT_EXPR, type, arg0, | |
4299 | TREE_OPERAND (arg1, 1))); | |
4300 | if (TREE_CODE (arg0) == LSHIFT_EXPR | |
4301 | && integer_onep (TREE_OPERAND (arg0, 0))) | |
4302 | return fold (build (LSHIFT_EXPR, type, arg1, | |
4303 | TREE_OPERAND (arg0, 1))); | |
4304 | } | |
2bc77e10 | 4305 | else |
4306 | { | |
e233264a | 4307 | /* x*0 is 0, except for IEEE floating point. */ |
8045c7c3 | 4308 | if ((TARGET_FLOAT_FORMAT != IEEE_FLOAT_FORMAT |
4309 | || flag_fast_math) | |
2bc77e10 | 4310 | && real_zerop (arg1)) |
4311 | return omit_one_operand (type, arg1, arg0); | |
e233264a | 4312 | /* In IEEE floating point, x*1 is not equivalent to x for snans. |
2bc77e10 | 4313 | However, ANSI says we can drop signals, |
4314 | so we can do this anyway. */ | |
4315 | if (real_onep (arg1)) | |
4316 | return non_lvalue (convert (type, arg0)); | |
4317 | /* x*2 is x+x */ | |
4318 | if (! wins && real_twop (arg1)) | |
4319 | { | |
4320 | tree arg = save_expr (arg0); | |
4321 | return build (PLUS_EXPR, type, arg, arg); | |
4322 | } | |
4323 | } | |
4324 | goto associate; | |
4325 | ||
4326 | case BIT_IOR_EXPR: | |
4327 | bit_ior: | |
f0921c37 | 4328 | { |
4329 | register enum tree_code code0, code1; | |
4330 | ||
2bc77e10 | 4331 | if (integer_all_onesp (arg1)) |
4332 | return omit_one_operand (type, arg1, arg0); | |
4333 | if (integer_zerop (arg1)) | |
4334 | return non_lvalue (convert (type, arg0)); | |
4335 | t1 = distribute_bit_expr (code, type, arg0, arg1); | |
4336 | if (t1 != NULL_TREE) | |
4337 | return t1; | |
3bba0206 | 4338 | |
f0921c37 | 4339 | /* (A << C1) | (A >> C2) if A is unsigned and C1+C2 is the size of A |
3bba0206 | 4340 | is a rotate of A by C1 bits. */ |
f0921c37 | 4341 | /* (A << B) | (A >> (Z - B)) if A is unsigned and Z is the size of A |
4342 | is a rotate of A by B bits. */ | |
3bba0206 | 4343 | |
f0921c37 | 4344 | code0 = TREE_CODE (arg0); |
4345 | code1 = TREE_CODE (arg1); | |
4346 | if (((code0 == RSHIFT_EXPR && code1 == LSHIFT_EXPR) | |
4347 | || (code1 == RSHIFT_EXPR && code0 == LSHIFT_EXPR)) | |
3bba0206 | 4348 | && operand_equal_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg1,0), 0) |
f0921c37 | 4349 | && TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg0, 0)))) |
4350 | { | |
4351 | register tree tree01, tree11; | |
4352 | register enum tree_code code01, code11; | |
4353 | ||
4354 | tree01 = TREE_OPERAND (arg0, 1); | |
4355 | tree11 = TREE_OPERAND (arg1, 1); | |
4356 | code01 = TREE_CODE (tree01); | |
4357 | code11 = TREE_CODE (tree11); | |
4358 | if (code01 == INTEGER_CST | |
4359 | && code11 == INTEGER_CST | |
4360 | && TREE_INT_CST_HIGH (tree01) == 0 | |
4361 | && TREE_INT_CST_HIGH (tree11) == 0 | |
4362 | && ((TREE_INT_CST_LOW (tree01) + TREE_INT_CST_LOW (tree11)) | |
3bba0206 | 4363 | == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg0, 0))))) |
f0921c37 | 4364 | return build (LROTATE_EXPR, type, TREE_OPERAND (arg0, 0), |
4365 | code0 == LSHIFT_EXPR ? tree01 : tree11); | |
4366 | else if (code11 == MINUS_EXPR | |
4367 | && TREE_CODE (TREE_OPERAND (tree11, 0)) == INTEGER_CST | |
4368 | && TREE_INT_CST_HIGH (TREE_OPERAND (tree11, 0)) == 0 | |
4369 | && TREE_INT_CST_LOW (TREE_OPERAND (tree11, 0)) | |
4370 | == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg0, 0))) | |
4371 | && operand_equal_p (tree01, TREE_OPERAND (tree11, 1), 0)) | |
4372 | return build (code0 == LSHIFT_EXPR ? LROTATE_EXPR : RROTATE_EXPR, | |
4373 | type, TREE_OPERAND (arg0, 0), tree01); | |
4374 | else if (code01 == MINUS_EXPR | |
4375 | && TREE_CODE (TREE_OPERAND (tree01, 0)) == INTEGER_CST | |
4376 | && TREE_INT_CST_HIGH (TREE_OPERAND (tree01, 0)) == 0 | |
4377 | && TREE_INT_CST_LOW (TREE_OPERAND (tree01, 0)) | |
4378 | == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg0, 0))) | |
4379 | && operand_equal_p (tree11, TREE_OPERAND (tree01, 1), 0)) | |
4380 | return build (code0 != LSHIFT_EXPR ? LROTATE_EXPR : RROTATE_EXPR, | |
4381 | type, TREE_OPERAND (arg0, 0), tree11); | |
4382 | } | |
3bba0206 | 4383 | |
2bc77e10 | 4384 | goto associate; |
f0921c37 | 4385 | } |
2bc77e10 | 4386 | |
4387 | case BIT_XOR_EXPR: | |
4388 | if (integer_zerop (arg1)) | |
4389 | return non_lvalue (convert (type, arg0)); | |
4390 | if (integer_all_onesp (arg1)) | |
4391 | return fold (build1 (BIT_NOT_EXPR, type, arg0)); | |
4392 | goto associate; | |
4393 | ||
4394 | case BIT_AND_EXPR: | |
4395 | bit_and: | |
4396 | if (integer_all_onesp (arg1)) | |
4397 | return non_lvalue (convert (type, arg0)); | |
4398 | if (integer_zerop (arg1)) | |
4399 | return omit_one_operand (type, arg1, arg0); | |
4400 | t1 = distribute_bit_expr (code, type, arg0, arg1); | |
4401 | if (t1 != NULL_TREE) | |
4402 | return t1; | |
4403 | /* Simplify ((int)c & 0x377) into (int)c, if c is unsigned char. */ | |
4404 | if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == NOP_EXPR | |
4405 | && TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg1, 0)))) | |
4406 | { | |
4407 | int prec = TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg1, 0))); | |
b572011e | 4408 | if (prec < BITS_PER_WORD && prec < HOST_BITS_PER_WIDE_INT |
4409 | && (~TREE_INT_CST_LOW (arg0) | |
4410 | & (((HOST_WIDE_INT) 1 << prec) - 1)) == 0) | |
2bc77e10 | 4411 | return build1 (NOP_EXPR, type, TREE_OPERAND (arg1, 0)); |
4412 | } | |
4413 | if (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg0) == NOP_EXPR | |
4414 | && TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg0, 0)))) | |
4415 | { | |
4416 | int prec = TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg0, 0))); | |
b572011e | 4417 | if (prec < BITS_PER_WORD && prec < HOST_BITS_PER_WIDE_INT |
4418 | && (~TREE_INT_CST_LOW (arg1) | |
4419 | & (((HOST_WIDE_INT) 1 << prec) - 1)) == 0) | |
2bc77e10 | 4420 | return build1 (NOP_EXPR, type, TREE_OPERAND (arg0, 0)); |
4421 | } | |
4422 | goto associate; | |
4423 | ||
4424 | case BIT_ANDTC_EXPR: | |
4425 | if (integer_all_onesp (arg0)) | |
4426 | return non_lvalue (convert (type, arg1)); | |
4427 | if (integer_zerop (arg0)) | |
4428 | return omit_one_operand (type, arg0, arg1); | |
4429 | if (TREE_CODE (arg1) == INTEGER_CST) | |
4430 | { | |
4431 | arg1 = fold (build1 (BIT_NOT_EXPR, type, arg1)); | |
4432 | code = BIT_AND_EXPR; | |
4433 | goto bit_and; | |
4434 | } | |
4435 | goto binary; | |
4436 | ||
0f586b9b | 4437 | case RDIV_EXPR: |
4438 | /* In most cases, do nothing with a divide by zero. */ | |
4439 | #if !defined (REAL_IS_NOT_DOUBLE) || defined (REAL_ARITHMETIC) | |
4440 | #ifndef REAL_INFINITY | |
4441 | if (TREE_CODE (arg1) == REAL_CST && real_zerop (arg1)) | |
4442 | return t; | |
4443 | #endif | |
4444 | #endif /* not REAL_IS_NOT_DOUBLE, or REAL_ARITHMETIC */ | |
4445 | ||
4446 | /* In IEEE floating point, x/1 is not equivalent to x for snans. | |
4447 | However, ANSI says we can drop signals, so we can do this anyway. */ | |
4448 | if (real_onep (arg1)) | |
4449 | return non_lvalue (convert (type, arg0)); | |
4450 | ||
4451 | /* If ARG1 is a constant, we can convert this to a multiply by the | |
4452 | reciprocal. This does not have the same rounding properties, | |
4453 | so only do this if -ffast-math. We can actually always safely | |
4454 | do it if ARG1 is a power of two, but it's hard to tell if it is | |
4455 | or not in a portable manner. */ | |
88181ec5 | 4456 | if (TREE_CODE (arg1) == REAL_CST) |
4457 | { | |
4458 | if (flag_fast_math | |
4459 | && 0 != (tem = const_binop (code, build_real (type, dconst1), | |
4460 | arg1, 0))) | |
4461 | return fold (build (MULT_EXPR, type, arg0, tem)); | |
4462 | /* Find the reciprocal if optimizing and the result is exact. */ | |
4463 | else if (optimize) | |
4464 | { | |
4465 | REAL_VALUE_TYPE r; | |
4466 | r = TREE_REAL_CST (arg1); | |
4467 | if (exact_real_inverse (TYPE_MODE(TREE_TYPE(arg0)), &r)) | |
4468 | { | |
4469 | tem = build_real (type, r); | |
4470 | return fold (build (MULT_EXPR, type, arg0, tem)); | |
4471 | } | |
4472 | } | |
4473 | } | |
0f586b9b | 4474 | goto binary; |
4475 | ||
2bc77e10 | 4476 | case TRUNC_DIV_EXPR: |
4477 | case ROUND_DIV_EXPR: | |
4478 | case FLOOR_DIV_EXPR: | |
4479 | case CEIL_DIV_EXPR: | |
4480 | case EXACT_DIV_EXPR: | |
2bc77e10 | 4481 | if (integer_onep (arg1)) |
4482 | return non_lvalue (convert (type, arg0)); | |
4483 | if (integer_zerop (arg1)) | |
4484 | return t; | |
39635df9 | 4485 | |
bd5b3bce | 4486 | /* If we have ((a / C1) / C2) where both division are the same type, try |
0f586b9b | 4487 | to simplify. First see if C1 * C2 overflows or not. */ |
4488 | if (TREE_CODE (arg0) == code && TREE_CODE (arg1) == INTEGER_CST | |
4489 | && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST) | |
4490 | { | |
bd5b3bce | 4491 | tree new_divisor; |
0f586b9b | 4492 | |
bd5b3bce | 4493 | new_divisor = const_binop (MULT_EXPR, TREE_OPERAND (arg0, 1), arg1, 0); |
4494 | tem = const_binop (FLOOR_DIV_EXPR, new_divisor, arg1, 0); | |
ae9613cf | 4495 | |
bd5b3bce | 4496 | if (TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1)) == TREE_INT_CST_LOW (tem) |
4497 | && TREE_INT_CST_HIGH (TREE_OPERAND (arg0, 1)) == TREE_INT_CST_HIGH (tem)) | |
0f586b9b | 4498 | { |
bd5b3bce | 4499 | /* If no overflow, divide by C1*C2. */ |
4500 | return fold (build (code, type, TREE_OPERAND (arg0, 0), new_divisor)); | |
0f586b9b | 4501 | } |
0f586b9b | 4502 | } |
4503 | ||
e4142c0f | 4504 | /* Look for ((a * C1) / C3) or (((a * C1) + C2) / C3), |
4505 | where C1 % C3 == 0 or C3 % C1 == 0. We can simplify these | |
4506 | expressions, which often appear in the offsets or sizes of | |
4507 | objects with a varying size. Only deal with positive divisors | |
b16b4faa | 4508 | and multiplicands. If C2 is negative, we must have C2 % C3 == 0. |
e4142c0f | 4509 | |
4510 | Look for NOPs and SAVE_EXPRs inside. */ | |
4511 | ||
39635df9 | 4512 | if (TREE_CODE (arg1) == INTEGER_CST |
5e394bea | 4513 | && tree_int_cst_sgn (arg1) >= 0) |
39635df9 | 4514 | { |
e4142c0f | 4515 | int have_save_expr = 0; |
4516 | tree c2 = integer_zero_node; | |
4517 | tree xarg0 = arg0; | |
39635df9 | 4518 | |
ba11d945 | 4519 | if (TREE_CODE (xarg0) == SAVE_EXPR && SAVE_EXPR_RTL (xarg0) == 0) |
e4142c0f | 4520 | have_save_expr = 1, xarg0 = TREE_OPERAND (xarg0, 0); |
39635df9 | 4521 | |
e4142c0f | 4522 | STRIP_NOPS (xarg0); |
4523 | ||
4524 | if (TREE_CODE (xarg0) == PLUS_EXPR | |
4525 | && TREE_CODE (TREE_OPERAND (xarg0, 1)) == INTEGER_CST) | |
4526 | c2 = TREE_OPERAND (xarg0, 1), xarg0 = TREE_OPERAND (xarg0, 0); | |
4527 | else if (TREE_CODE (xarg0) == MINUS_EXPR | |
b16b4faa | 4528 | && TREE_CODE (TREE_OPERAND (xarg0, 1)) == INTEGER_CST |
4529 | /* If we are doing this computation unsigned, the negate | |
4530 | is incorrect. */ | |
4531 | && ! TREE_UNSIGNED (type)) | |
e4142c0f | 4532 | { |
4533 | c2 = fold (build1 (NEGATE_EXPR, type, TREE_OPERAND (xarg0, 1))); | |
4534 | xarg0 = TREE_OPERAND (xarg0, 0); | |
4535 | } | |
4536 | ||
ba11d945 | 4537 | if (TREE_CODE (xarg0) == SAVE_EXPR && SAVE_EXPR_RTL (xarg0) == 0) |
e4142c0f | 4538 | have_save_expr = 1, xarg0 = TREE_OPERAND (xarg0, 0); |
4539 | ||
4540 | STRIP_NOPS (xarg0); | |
4541 | ||
4542 | if (TREE_CODE (xarg0) == MULT_EXPR | |
4543 | && TREE_CODE (TREE_OPERAND (xarg0, 1)) == INTEGER_CST | |
5e394bea | 4544 | && tree_int_cst_sgn (TREE_OPERAND (xarg0, 1)) >= 0 |
e4142c0f | 4545 | && (integer_zerop (const_binop (TRUNC_MOD_EXPR, |
4546 | TREE_OPERAND (xarg0, 1), arg1, 1)) | |
4547 | || integer_zerop (const_binop (TRUNC_MOD_EXPR, arg1, | |
b16b4faa | 4548 | TREE_OPERAND (xarg0, 1), 1))) |
5e394bea | 4549 | && (tree_int_cst_sgn (c2) >= 0 |
b16b4faa | 4550 | || integer_zerop (const_binop (TRUNC_MOD_EXPR, c2, |
4551 | arg1, 1)))) | |
e4142c0f | 4552 | { |
4553 | tree outer_div = integer_one_node; | |
4554 | tree c1 = TREE_OPERAND (xarg0, 1); | |
4555 | tree c3 = arg1; | |
4556 | ||
4557 | /* If C3 > C1, set them equal and do a divide by | |
4558 | C3/C1 at the end of the operation. */ | |
4559 | if (tree_int_cst_lt (c1, c3)) | |
4560 | outer_div = const_binop (code, c3, c1, 0), c3 = c1; | |
4561 | ||
4562 | /* The result is A * (C1/C3) + (C2/C3). */ | |
4563 | t = fold (build (PLUS_EXPR, type, | |
4564 | fold (build (MULT_EXPR, type, | |
4565 | TREE_OPERAND (xarg0, 0), | |
4566 | const_binop (code, c1, c3, 1))), | |
4567 | const_binop (code, c2, c3, 1))); | |
4568 | ||
4569 | if (! integer_onep (outer_div)) | |
5e394bea | 4570 | t = fold (build (code, type, t, convert (type, outer_div))); |
e4142c0f | 4571 | |
4572 | if (have_save_expr) | |
4573 | t = save_expr (t); | |
4574 | ||
4575 | return t; | |
4576 | } | |
39635df9 | 4577 | } |
4578 | ||
2bc77e10 | 4579 | goto binary; |
4580 | ||
4581 | case CEIL_MOD_EXPR: | |
4582 | case FLOOR_MOD_EXPR: | |
4583 | case ROUND_MOD_EXPR: | |
4584 | case TRUNC_MOD_EXPR: | |
4585 | if (integer_onep (arg1)) | |
4586 | return omit_one_operand (type, integer_zero_node, arg0); | |
4587 | if (integer_zerop (arg1)) | |
4588 | return t; | |
e4142c0f | 4589 | |
4590 | /* Look for ((a * C1) % C3) or (((a * C1) + C2) % C3), | |
4591 | where C1 % C3 == 0. Handle similarly to the division case, | |
4592 | but don't bother with SAVE_EXPRs. */ | |
4593 | ||
4594 | if (TREE_CODE (arg1) == INTEGER_CST | |
4595 | && ! integer_zerop (arg1)) | |
4596 | { | |
4597 | tree c2 = integer_zero_node; | |
4598 | tree xarg0 = arg0; | |
4599 | ||
4600 | if (TREE_CODE (xarg0) == PLUS_EXPR | |
4601 | && TREE_CODE (TREE_OPERAND (xarg0, 1)) == INTEGER_CST) | |
4602 | c2 = TREE_OPERAND (xarg0, 1), xarg0 = TREE_OPERAND (xarg0, 0); | |
4603 | else if (TREE_CODE (xarg0) == MINUS_EXPR | |
b16b4faa | 4604 | && TREE_CODE (TREE_OPERAND (xarg0, 1)) == INTEGER_CST |
4605 | && ! TREE_UNSIGNED (type)) | |
e4142c0f | 4606 | { |
4607 | c2 = fold (build1 (NEGATE_EXPR, type, TREE_OPERAND (xarg0, 1))); | |
4608 | xarg0 = TREE_OPERAND (xarg0, 0); | |
4609 | } | |
4610 | ||
4611 | STRIP_NOPS (xarg0); | |
4612 | ||
4613 | if (TREE_CODE (xarg0) == MULT_EXPR | |
4614 | && TREE_CODE (TREE_OPERAND (xarg0, 1)) == INTEGER_CST | |
4615 | && integer_zerop (const_binop (TRUNC_MOD_EXPR, | |
4616 | TREE_OPERAND (xarg0, 1), | |
b16b4faa | 4617 | arg1, 1)) |
5e394bea | 4618 | && tree_int_cst_sgn (c2) >= 0) |
e4142c0f | 4619 | /* The result is (C2%C3). */ |
4620 | return omit_one_operand (type, const_binop (code, c2, arg1, 1), | |
4621 | TREE_OPERAND (xarg0, 0)); | |
4622 | } | |
4623 | ||
2bc77e10 | 4624 | goto binary; |
4625 | ||
4626 | case LSHIFT_EXPR: | |
4627 | case RSHIFT_EXPR: | |
4628 | case LROTATE_EXPR: | |
4629 | case RROTATE_EXPR: | |
4630 | if (integer_zerop (arg1)) | |
4631 | return non_lvalue (convert (type, arg0)); | |
4632 | /* Since negative shift count is not well-defined, | |
4633 | don't try to compute it in the compiler. */ | |
7a1b56a9 | 4634 | if (TREE_CODE (arg1) == INTEGER_CST && tree_int_cst_sgn (arg1) < 0) |
2bc77e10 | 4635 | return t; |
7a1b56a9 | 4636 | /* Rewrite an LROTATE_EXPR by a constant into an |
4637 | RROTATE_EXPR by a new constant. */ | |
4638 | if (code == LROTATE_EXPR && TREE_CODE (arg1) == INTEGER_CST) | |
4639 | { | |
4640 | TREE_SET_CODE (t, RROTATE_EXPR); | |
4641 | code = RROTATE_EXPR; | |
4642 | TREE_OPERAND (t, 1) = arg1 | |
4643 | = const_binop | |
4644 | (MINUS_EXPR, | |
4645 | convert (TREE_TYPE (arg1), | |
4646 | build_int_2 (GET_MODE_BITSIZE (TYPE_MODE (type)), 0)), | |
4647 | arg1, 0); | |
4648 | if (tree_int_cst_sgn (arg1) < 0) | |
4649 | return t; | |
4650 | } | |
4651 | ||
4652 | /* If we have a rotate of a bit operation with the rotate count and | |
4653 | the second operand of the bit operation both constant, | |
4654 | permute the two operations. */ | |
4655 | if (code == RROTATE_EXPR && TREE_CODE (arg1) == INTEGER_CST | |
4656 | && (TREE_CODE (arg0) == BIT_AND_EXPR | |
4657 | || TREE_CODE (arg0) == BIT_ANDTC_EXPR | |
4658 | || TREE_CODE (arg0) == BIT_IOR_EXPR | |
4659 | || TREE_CODE (arg0) == BIT_XOR_EXPR) | |
4660 | && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST) | |
4661 | return fold (build (TREE_CODE (arg0), type, | |
4662 | fold (build (code, type, | |
4663 | TREE_OPERAND (arg0, 0), arg1)), | |
4664 | fold (build (code, type, | |
4665 | TREE_OPERAND (arg0, 1), arg1)))); | |
4666 | ||
4667 | /* Two consecutive rotates adding up to the width of the mode can | |
4668 | be ignored. */ | |
4669 | if (code == RROTATE_EXPR && TREE_CODE (arg1) == INTEGER_CST | |
4670 | && TREE_CODE (arg0) == RROTATE_EXPR | |
4671 | && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST | |
4672 | && TREE_INT_CST_HIGH (arg1) == 0 | |
4673 | && TREE_INT_CST_HIGH (TREE_OPERAND (arg0, 1)) == 0 | |
4674 | && ((TREE_INT_CST_LOW (arg1) | |
4675 | + TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1))) | |
4676 | == GET_MODE_BITSIZE (TYPE_MODE (type)))) | |
4677 | return TREE_OPERAND (arg0, 0); | |
4678 | ||
2bc77e10 | 4679 | goto binary; |
4680 | ||
4681 | case MIN_EXPR: | |
4682 | if (operand_equal_p (arg0, arg1, 0)) | |
4683 | return arg0; | |
780a4395 | 4684 | if (INTEGRAL_TYPE_P (type) |
2bc77e10 | 4685 | && operand_equal_p (arg1, TYPE_MIN_VALUE (type), 1)) |
4686 | return omit_one_operand (type, arg1, arg0); | |
4687 | goto associate; | |
4688 | ||
4689 | case MAX_EXPR: | |
4690 | if (operand_equal_p (arg0, arg1, 0)) | |
4691 | return arg0; | |
780a4395 | 4692 | if (INTEGRAL_TYPE_P (type) |
2bc77e10 | 4693 | && operand_equal_p (arg1, TYPE_MAX_VALUE (type), 1)) |
4694 | return omit_one_operand (type, arg1, arg0); | |
4695 | goto associate; | |
4696 | ||
4697 | case TRUTH_NOT_EXPR: | |
4698 | /* Note that the operand of this must be an int | |
4699 | and its values must be 0 or 1. | |
4700 | ("true" is a fixed value perhaps depending on the language, | |
4701 | but we don't handle values other than 1 correctly yet.) */ | |
7bbc42b5 | 4702 | tem = invert_truthvalue (arg0); |
4703 | /* Avoid infinite recursion. */ | |
4704 | if (TREE_CODE (tem) == TRUTH_NOT_EXPR) | |
4705 | return t; | |
4706 | return convert (type, tem); | |
2bc77e10 | 4707 | |
4708 | case TRUTH_ANDIF_EXPR: | |
4709 | /* Note that the operands of this must be ints | |
4710 | and their values must be 0 or 1. | |
4711 | ("true" is a fixed value perhaps depending on the language.) */ | |
4712 | /* If first arg is constant zero, return it. */ | |
9a7b73a1 | 4713 | if (integer_zerop (arg0)) |
2bc77e10 | 4714 | return arg0; |
4715 | case TRUTH_AND_EXPR: | |
4716 | /* If either arg is constant true, drop it. */ | |
4717 | if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0)) | |
4718 | return non_lvalue (arg1); | |
4719 | if (TREE_CODE (arg1) == INTEGER_CST && ! integer_zerop (arg1)) | |
4720 | return non_lvalue (arg0); | |
9a7b73a1 | 4721 | /* If second arg is constant zero, result is zero, but first arg |
4722 | must be evaluated. */ | |
4723 | if (integer_zerop (arg1)) | |
4724 | return omit_one_operand (type, arg1, arg0); | |
2bc77e10 | 4725 | |
4726 | truth_andor: | |
935abd69 | 4727 | /* We only do these simplifications if we are optimizing. */ |
4728 | if (!optimize) | |
4729 | return t; | |
4730 | ||
4731 | /* Check for things like (A || B) && (A || C). We can convert this | |
4732 | to A || (B && C). Note that either operator can be any of the four | |
4733 | truth and/or operations and the transformation will still be | |
4734 | valid. Also note that we only care about order for the | |
4735 | ANDIF and ORIF operators. */ | |
4736 | if (TREE_CODE (arg0) == TREE_CODE (arg1) | |
4737 | && (TREE_CODE (arg0) == TRUTH_ANDIF_EXPR | |
4738 | || TREE_CODE (arg0) == TRUTH_ORIF_EXPR | |
4739 | || TREE_CODE (arg0) == TRUTH_AND_EXPR | |
4740 | || TREE_CODE (arg0) == TRUTH_OR_EXPR)) | |
4741 | { | |
4742 | tree a00 = TREE_OPERAND (arg0, 0); | |
4743 | tree a01 = TREE_OPERAND (arg0, 1); | |
4744 | tree a10 = TREE_OPERAND (arg1, 0); | |
4745 | tree a11 = TREE_OPERAND (arg1, 1); | |
4746 | int commutative = ((TREE_CODE (arg0) == TRUTH_OR_EXPR | |
4747 | || TREE_CODE (arg0) == TRUTH_AND_EXPR) | |
4748 | && (code == TRUTH_AND_EXPR | |
4749 | || code == TRUTH_OR_EXPR)); | |
4750 | ||
4751 | if (operand_equal_p (a00, a10, 0)) | |
4752 | return fold (build (TREE_CODE (arg0), type, a00, | |
4753 | fold (build (code, type, a01, a11)))); | |
4754 | else if (commutative && operand_equal_p (a00, a11, 0)) | |
4755 | return fold (build (TREE_CODE (arg0), type, a00, | |
4756 | fold (build (code, type, a01, a10)))); | |
4757 | else if (commutative && operand_equal_p (a01, a10, 0)) | |
4758 | return fold (build (TREE_CODE (arg0), type, a01, | |
4759 | fold (build (code, type, a00, a11)))); | |
4760 | ||
4761 | /* This case if tricky because we must either have commutative | |
4762 | operators or else A10 must not have side-effects. */ | |
4763 | ||
4764 | else if ((commutative || ! TREE_SIDE_EFFECTS (a10)) | |
4765 | && operand_equal_p (a01, a11, 0)) | |
4766 | return fold (build (TREE_CODE (arg0), type, | |
4767 | fold (build (code, type, a00, a10)), | |
4768 | a01)); | |
4769 | } | |
4770 | ||
12ec0a8a | 4771 | /* See if we can build a range comparison. */ |
4772 | if (0 != (tem = fold_range_test (t))) | |
4773 | return tem; | |
4774 | ||
2bc77e10 | 4775 | /* Check for the possibility of merging component references. If our |
4776 | lhs is another similar operation, try to merge its rhs with our | |
4777 | rhs. Then try to merge our lhs and rhs. */ | |
935abd69 | 4778 | if (TREE_CODE (arg0) == code |
4779 | && 0 != (tem = fold_truthop (code, type, | |
4780 | TREE_OPERAND (arg0, 1), arg1))) | |
4781 | return fold (build (code, type, TREE_OPERAND (arg0, 0), tem)); | |
2bc77e10 | 4782 | |
935abd69 | 4783 | if ((tem = fold_truthop (code, type, arg0, arg1)) != 0) |
4784 | return tem; | |
8b94828f | 4785 | |
2bc77e10 | 4786 | return t; |
4787 | ||
4788 | case TRUTH_ORIF_EXPR: | |
4789 | /* Note that the operands of this must be ints | |
4790 | and their values must be 0 or true. | |
4791 | ("true" is a fixed value perhaps depending on the language.) */ | |
4792 | /* If first arg is constant true, return it. */ | |
4793 | if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0)) | |
4794 | return arg0; | |
4795 | case TRUTH_OR_EXPR: | |
4796 | /* If either arg is constant zero, drop it. */ | |
4797 | if (TREE_CODE (arg0) == INTEGER_CST && integer_zerop (arg0)) | |
4798 | return non_lvalue (arg1); | |
4799 | if (TREE_CODE (arg1) == INTEGER_CST && integer_zerop (arg1)) | |
4800 | return non_lvalue (arg0); | |
9a7b73a1 | 4801 | /* If second arg is constant true, result is true, but we must |
4802 | evaluate first arg. */ | |
4803 | if (TREE_CODE (arg1) == INTEGER_CST && ! integer_zerop (arg1)) | |
4804 | return omit_one_operand (type, arg1, arg0); | |
2bc77e10 | 4805 | goto truth_andor; |
4806 | ||
9a7b73a1 | 4807 | case TRUTH_XOR_EXPR: |
4808 | /* If either arg is constant zero, drop it. */ | |
4809 | if (integer_zerop (arg0)) | |
4810 | return non_lvalue (arg1); | |
4811 | if (integer_zerop (arg1)) | |
4812 | return non_lvalue (arg0); | |
4813 | /* If either arg is constant true, this is a logical inversion. */ | |
4814 | if (integer_onep (arg0)) | |
4815 | return non_lvalue (invert_truthvalue (arg1)); | |
4816 | if (integer_onep (arg1)) | |
4817 | return non_lvalue (invert_truthvalue (arg0)); | |
54e99035 | 4818 | return t; |
9a7b73a1 | 4819 | |
2bc77e10 | 4820 | case EQ_EXPR: |
4821 | case NE_EXPR: | |
4822 | case LT_EXPR: | |
4823 | case GT_EXPR: | |
4824 | case LE_EXPR: | |
4825 | case GE_EXPR: | |
4826 | /* If one arg is a constant integer, put it last. */ | |
4827 | if (TREE_CODE (arg0) == INTEGER_CST | |
4828 | && TREE_CODE (arg1) != INTEGER_CST) | |
4829 | { | |
4830 | TREE_OPERAND (t, 0) = arg1; | |
4831 | TREE_OPERAND (t, 1) = arg0; | |
4832 | arg0 = TREE_OPERAND (t, 0); | |
4833 | arg1 = TREE_OPERAND (t, 1); | |
e233264a | 4834 | code = swap_tree_comparison (code); |
2bc77e10 | 4835 | TREE_SET_CODE (t, code); |
4836 | } | |
4837 | ||
4838 | /* Convert foo++ == CONST into ++foo == CONST + INCR. | |
4839 | First, see if one arg is constant; find the constant arg | |
4840 | and the other one. */ | |
4841 | { | |
4842 | tree constop = 0, varop; | |
f13f9c7f | 4843 | int constopnum = -1; |
2bc77e10 | 4844 | |
4845 | if (TREE_CONSTANT (arg1)) | |
f13f9c7f | 4846 | constopnum = 1, constop = arg1, varop = arg0; |
2bc77e10 | 4847 | if (TREE_CONSTANT (arg0)) |
f13f9c7f | 4848 | constopnum = 0, constop = arg0, varop = arg1; |
2bc77e10 | 4849 | |
4850 | if (constop && TREE_CODE (varop) == POSTINCREMENT_EXPR) | |
4851 | { | |
2bc77e10 | 4852 | /* This optimization is invalid for ordered comparisons |
4853 | if CONST+INCR overflows or if foo+incr might overflow. | |
e233264a | 4854 | This optimization is invalid for floating point due to rounding. |
2bc77e10 | 4855 | For pointer types we assume overflow doesn't happen. */ |
4856 | if (TREE_CODE (TREE_TYPE (varop)) == POINTER_TYPE | |
780a4395 | 4857 | || (! FLOAT_TYPE_P (TREE_TYPE (varop)) |
e233264a | 4858 | && (code == EQ_EXPR || code == NE_EXPR))) |
2bc77e10 | 4859 | { |
e233264a | 4860 | tree newconst |
4861 | = fold (build (PLUS_EXPR, TREE_TYPE (varop), | |
4862 | constop, TREE_OPERAND (varop, 1))); | |
4863 | TREE_SET_CODE (varop, PREINCREMENT_EXPR); | |
f13f9c7f | 4864 | |
9d5c6945 | 4865 | /* If VAROP is a reference to a bitfield, we must mask |
4866 | the constant by the width of the field. */ | |
4867 | if (TREE_CODE (TREE_OPERAND (varop, 0)) == COMPONENT_REF | |
4868 | && DECL_BIT_FIELD(TREE_OPERAND | |
4869 | (TREE_OPERAND (varop, 0), 1))) | |
4870 | { | |
4871 | int size | |
4872 | = TREE_INT_CST_LOW (DECL_SIZE | |
4873 | (TREE_OPERAND | |
4874 | (TREE_OPERAND (varop, 0), 1))); | |
4875 | ||
4876 | newconst = fold (build (BIT_AND_EXPR, | |
4877 | TREE_TYPE (varop), newconst, | |
4878 | convert (TREE_TYPE (varop), | |
4879 | build_int_2 (size, 0)))); | |
4880 | } | |
4881 | ||
4882 | ||
f13f9c7f | 4883 | t = build (code, type, TREE_OPERAND (t, 0), |
4884 | TREE_OPERAND (t, 1)); | |
4885 | TREE_OPERAND (t, constopnum) = newconst; | |
e233264a | 4886 | return t; |
2bc77e10 | 4887 | } |
4888 | } | |
4889 | else if (constop && TREE_CODE (varop) == POSTDECREMENT_EXPR) | |
4890 | { | |
2bc77e10 | 4891 | if (TREE_CODE (TREE_TYPE (varop)) == POINTER_TYPE |
780a4395 | 4892 | || (! FLOAT_TYPE_P (TREE_TYPE (varop)) |
e233264a | 4893 | && (code == EQ_EXPR || code == NE_EXPR))) |
2bc77e10 | 4894 | { |
e233264a | 4895 | tree newconst |
4896 | = fold (build (MINUS_EXPR, TREE_TYPE (varop), | |
4897 | constop, TREE_OPERAND (varop, 1))); | |
4898 | TREE_SET_CODE (varop, PREDECREMENT_EXPR); | |
9d5c6945 | 4899 | |
4900 | if (TREE_CODE (TREE_OPERAND (varop, 0)) == COMPONENT_REF | |
4901 | && DECL_BIT_FIELD(TREE_OPERAND | |
4902 | (TREE_OPERAND (varop, 0), 1))) | |
4903 | { | |
4904 | int size | |
4905 | = TREE_INT_CST_LOW (DECL_SIZE | |
4906 | (TREE_OPERAND | |
4907 | (TREE_OPERAND (varop, 0), 1))); | |
4908 | ||
4909 | newconst = fold (build (BIT_AND_EXPR, | |
4910 | TREE_TYPE (varop), newconst, | |
4911 | convert (TREE_TYPE (varop), | |
4912 | build_int_2 (size, 0)))); | |
4913 | } | |
4914 | ||
4915 | ||
f13f9c7f | 4916 | t = build (code, type, TREE_OPERAND (t, 0), |
4917 | TREE_OPERAND (t, 1)); | |
4918 | TREE_OPERAND (t, constopnum) = newconst; | |
e233264a | 4919 | return t; |
2bc77e10 | 4920 | } |
4921 | } | |
4922 | } | |
4923 | ||
4924 | /* Change X >= CST to X > (CST - 1) if CST is positive. */ | |
4925 | if (TREE_CODE (arg1) == INTEGER_CST | |
4926 | && TREE_CODE (arg0) != INTEGER_CST | |
5e394bea | 4927 | && tree_int_cst_sgn (arg1) > 0) |
2bc77e10 | 4928 | { |
4929 | switch (TREE_CODE (t)) | |
4930 | { | |
4931 | case GE_EXPR: | |
4932 | code = GT_EXPR; | |
5485823f | 4933 | arg1 = const_binop (MINUS_EXPR, arg1, integer_one_node, 0); |
f13f9c7f | 4934 | t = build (code, type, TREE_OPERAND (t, 0), arg1); |
2bc77e10 | 4935 | break; |
4936 | ||
4937 | case LT_EXPR: | |
4938 | code = LE_EXPR; | |
5485823f | 4939 | arg1 = const_binop (MINUS_EXPR, arg1, integer_one_node, 0); |
f13f9c7f | 4940 | t = build (code, type, TREE_OPERAND (t, 0), arg1); |
4941 | break; | |
2bc77e10 | 4942 | } |
4943 | } | |
4944 | ||
2bc77e10 | 4945 | /* If this is an EQ or NE comparison with zero and ARG0 is |
4946 | (1 << foo) & bar, convert it to (bar >> foo) & 1. Both require | |
4947 | two operations, but the latter can be done in one less insn | |
4948 | one machine that have only two-operand insns or on which a | |
4949 | constant cannot be the first operand. */ | |
4950 | if (integer_zerop (arg1) && (code == EQ_EXPR || code == NE_EXPR) | |
4951 | && TREE_CODE (arg0) == BIT_AND_EXPR) | |
4952 | { | |
4953 | if (TREE_CODE (TREE_OPERAND (arg0, 0)) == LSHIFT_EXPR | |
4954 | && integer_onep (TREE_OPERAND (TREE_OPERAND (arg0, 0), 0))) | |
4955 | return | |
4956 | fold (build (code, type, | |
4957 | build (BIT_AND_EXPR, TREE_TYPE (arg0), | |
4958 | build (RSHIFT_EXPR, | |
4959 | TREE_TYPE (TREE_OPERAND (arg0, 0)), | |
4960 | TREE_OPERAND (arg0, 1), | |
4961 | TREE_OPERAND (TREE_OPERAND (arg0, 0), 1)), | |
4962 | convert (TREE_TYPE (arg0), | |
4963 | integer_one_node)), | |
4964 | arg1)); | |
4965 | else if (TREE_CODE (TREE_OPERAND (arg0, 1)) == LSHIFT_EXPR | |
4966 | && integer_onep (TREE_OPERAND (TREE_OPERAND (arg0, 1), 0))) | |
4967 | return | |
4968 | fold (build (code, type, | |
4969 | build (BIT_AND_EXPR, TREE_TYPE (arg0), | |
4970 | build (RSHIFT_EXPR, | |
4971 | TREE_TYPE (TREE_OPERAND (arg0, 1)), | |
4972 | TREE_OPERAND (arg0, 0), | |
4973 | TREE_OPERAND (TREE_OPERAND (arg0, 1), 1)), | |
4974 | convert (TREE_TYPE (arg0), | |
4975 | integer_one_node)), | |
4976 | arg1)); | |
4977 | } | |
4978 | ||
c393c7ff | 4979 | /* If this is an NE or EQ comparison of zero against the result of a |
722b90ac | 4980 | signed MOD operation whose second operand is a power of 2, make |
4981 | the MOD operation unsigned since it is simpler and equivalent. */ | |
c393c7ff | 4982 | if ((code == NE_EXPR || code == EQ_EXPR) |
4983 | && integer_zerop (arg1) | |
4984 | && ! TREE_UNSIGNED (TREE_TYPE (arg0)) | |
4985 | && (TREE_CODE (arg0) == TRUNC_MOD_EXPR | |
4986 | || TREE_CODE (arg0) == CEIL_MOD_EXPR | |
4987 | || TREE_CODE (arg0) == FLOOR_MOD_EXPR | |
722b90ac | 4988 | || TREE_CODE (arg0) == ROUND_MOD_EXPR) |
4989 | && integer_pow2p (TREE_OPERAND (arg0, 1))) | |
c393c7ff | 4990 | { |
4991 | tree newtype = unsigned_type (TREE_TYPE (arg0)); | |
4992 | tree newmod = build (TREE_CODE (arg0), newtype, | |
4993 | convert (newtype, TREE_OPERAND (arg0, 0)), | |
4994 | convert (newtype, TREE_OPERAND (arg0, 1))); | |
4995 | ||
4996 | return build (code, type, newmod, convert (newtype, arg1)); | |
4997 | } | |
4998 | ||
2bc77e10 | 4999 | /* If this is an NE comparison of zero with an AND of one, remove the |
5000 | comparison since the AND will give the correct value. */ | |
5001 | if (code == NE_EXPR && integer_zerop (arg1) | |
5002 | && TREE_CODE (arg0) == BIT_AND_EXPR | |
5003 | && integer_onep (TREE_OPERAND (arg0, 1))) | |
5004 | return convert (type, arg0); | |
5005 | ||
5006 | /* If we have (A & C) == C where C is a power of 2, convert this into | |
5007 | (A & C) != 0. Similarly for NE_EXPR. */ | |
5008 | if ((code == EQ_EXPR || code == NE_EXPR) | |
5009 | && TREE_CODE (arg0) == BIT_AND_EXPR | |
5010 | && integer_pow2p (TREE_OPERAND (arg0, 1)) | |
5011 | && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0)) | |
5012 | return build (code == EQ_EXPR ? NE_EXPR : EQ_EXPR, type, | |
5013 | arg0, integer_zero_node); | |
5014 | ||
898bfb9d | 5015 | /* If X is unsigned, convert X < (1 << Y) into X >> Y == 0 |
e8526af9 | 5016 | and similarly for >= into !=. */ |
898bfb9d | 5017 | if ((code == LT_EXPR || code == GE_EXPR) |
5018 | && TREE_UNSIGNED (TREE_TYPE (arg0)) | |
5019 | && TREE_CODE (arg1) == LSHIFT_EXPR | |
5020 | && integer_onep (TREE_OPERAND (arg1, 0))) | |
5021 | return build (code == LT_EXPR ? EQ_EXPR : NE_EXPR, type, | |
5022 | build (RSHIFT_EXPR, TREE_TYPE (arg0), arg0, | |
5023 | TREE_OPERAND (arg1, 1)), | |
5024 | convert (TREE_TYPE (arg0), integer_zero_node)); | |
5025 | ||
5026 | else if ((code == LT_EXPR || code == GE_EXPR) | |
5027 | && TREE_UNSIGNED (TREE_TYPE (arg0)) | |
5028 | && (TREE_CODE (arg1) == NOP_EXPR | |
5029 | || TREE_CODE (arg1) == CONVERT_EXPR) | |
5030 | && TREE_CODE (TREE_OPERAND (arg1, 0)) == LSHIFT_EXPR | |
5031 | && integer_onep (TREE_OPERAND (TREE_OPERAND (arg1, 0), 0))) | |
5032 | return | |
5033 | build (code == LT_EXPR ? EQ_EXPR : NE_EXPR, type, | |
5034 | convert (TREE_TYPE (arg0), | |
5035 | build (RSHIFT_EXPR, TREE_TYPE (arg0), arg0, | |
5036 | TREE_OPERAND (TREE_OPERAND (arg1, 0), 1))), | |
5037 | convert (TREE_TYPE (arg0), integer_zero_node)); | |
5038 | ||
e233264a | 5039 | /* Simplify comparison of something with itself. (For IEEE |
5040 | floating-point, we can only do some of these simplifications.) */ | |
5041 | if (operand_equal_p (arg0, arg1, 0)) | |
2bc77e10 | 5042 | { |
5043 | switch (code) | |
5044 | { | |
5045 | case EQ_EXPR: | |
5046 | case GE_EXPR: | |
5047 | case LE_EXPR: | |
780a4395 | 5048 | if (INTEGRAL_TYPE_P (TREE_TYPE (arg0))) |
e233264a | 5049 | { |
5050 | t = build_int_2 (1, 0); | |
5051 | TREE_TYPE (t) = type; | |
5052 | return t; | |
5053 | } | |
5054 | code = EQ_EXPR; | |
5055 | TREE_SET_CODE (t, code); | |
5056 | break; | |
5057 | ||
2bc77e10 | 5058 | case NE_EXPR: |
e233264a | 5059 | /* For NE, we can only do this simplification if integer. */ |
780a4395 | 5060 | if (! INTEGRAL_TYPE_P (TREE_TYPE (arg0))) |
e233264a | 5061 | break; |
a92771b8 | 5062 | /* ... fall through ... */ |
2bc77e10 | 5063 | case GT_EXPR: |
5064 | case LT_EXPR: | |
5065 | t = build_int_2 (0, 0); | |
5066 | TREE_TYPE (t) = type; | |
5067 | return t; | |
5068 | } | |
5069 | } | |
5070 | ||
5071 | /* An unsigned comparison against 0 can be simplified. */ | |
5072 | if (integer_zerop (arg1) | |
780a4395 | 5073 | && (INTEGRAL_TYPE_P (TREE_TYPE (arg1)) |
2bc77e10 | 5074 | || TREE_CODE (TREE_TYPE (arg1)) == POINTER_TYPE) |
5075 | && TREE_UNSIGNED (TREE_TYPE (arg1))) | |
5076 | { | |
5077 | switch (TREE_CODE (t)) | |
5078 | { | |
5079 | case GT_EXPR: | |
e233264a | 5080 | code = NE_EXPR; |
2bc77e10 | 5081 | TREE_SET_CODE (t, NE_EXPR); |
5082 | break; | |
5083 | case LE_EXPR: | |
e233264a | 5084 | code = EQ_EXPR; |
2bc77e10 | 5085 | TREE_SET_CODE (t, EQ_EXPR); |
5086 | break; | |
5087 | case GE_EXPR: | |
cdc1862f | 5088 | return omit_one_operand (type, |
5089 | convert (type, integer_one_node), | |
5090 | arg0); | |
2bc77e10 | 5091 | case LT_EXPR: |
cdc1862f | 5092 | return omit_one_operand (type, |
5093 | convert (type, integer_zero_node), | |
5094 | arg0); | |
2bc77e10 | 5095 | } |
5096 | } | |
5097 | ||
e233264a | 5098 | /* If we are comparing an expression that just has comparisons |
5099 | of two integer values, arithmetic expressions of those comparisons, | |
5100 | and constants, we can simplify it. There are only three cases | |
5101 | to check: the two values can either be equal, the first can be | |
5102 | greater, or the second can be greater. Fold the expression for | |
5103 | those three values. Since each value must be 0 or 1, we have | |
5104 | eight possibilities, each of which corresponds to the constant 0 | |
5105 | or 1 or one of the six possible comparisons. | |
5106 | ||
5107 | This handles common cases like (a > b) == 0 but also handles | |
5108 | expressions like ((x > y) - (y > x)) > 0, which supposedly | |
5109 | occur in macroized code. */ | |
5110 | ||
5111 | if (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg0) != INTEGER_CST) | |
5112 | { | |
5113 | tree cval1 = 0, cval2 = 0; | |
d0314131 | 5114 | int save_p = 0; |
e233264a | 5115 | |
d0314131 | 5116 | if (twoval_comparison_p (arg0, &cval1, &cval2, &save_p) |
e233264a | 5117 | /* Don't handle degenerate cases here; they should already |
5118 | have been handled anyway. */ | |
5119 | && cval1 != 0 && cval2 != 0 | |
5120 | && ! (TREE_CONSTANT (cval1) && TREE_CONSTANT (cval2)) | |
5121 | && TREE_TYPE (cval1) == TREE_TYPE (cval2) | |
780a4395 | 5122 | && INTEGRAL_TYPE_P (TREE_TYPE (cval1)) |
e233264a | 5123 | && ! operand_equal_p (TYPE_MIN_VALUE (TREE_TYPE (cval1)), |
5124 | TYPE_MAX_VALUE (TREE_TYPE (cval2)), 0)) | |
5125 | { | |
5126 | tree maxval = TYPE_MAX_VALUE (TREE_TYPE (cval1)); | |
5127 | tree minval = TYPE_MIN_VALUE (TREE_TYPE (cval1)); | |
5128 | ||
5129 | /* We can't just pass T to eval_subst in case cval1 or cval2 | |
5130 | was the same as ARG1. */ | |
5131 | ||
5132 | tree high_result | |
5133 | = fold (build (code, type, | |
5134 | eval_subst (arg0, cval1, maxval, cval2, minval), | |
5135 | arg1)); | |
5136 | tree equal_result | |
5137 | = fold (build (code, type, | |
5138 | eval_subst (arg0, cval1, maxval, cval2, maxval), | |
5139 | arg1)); | |
5140 | tree low_result | |
5141 | = fold (build (code, type, | |
5142 | eval_subst (arg0, cval1, minval, cval2, maxval), | |
5143 | arg1)); | |
5144 | ||
5145 | /* All three of these results should be 0 or 1. Confirm they | |
5146 | are. Then use those values to select the proper code | |
5147 | to use. */ | |
5148 | ||
5149 | if ((integer_zerop (high_result) | |
5150 | || integer_onep (high_result)) | |
5151 | && (integer_zerop (equal_result) | |
5152 | || integer_onep (equal_result)) | |
5153 | && (integer_zerop (low_result) | |
5154 | || integer_onep (low_result))) | |
5155 | { | |
5156 | /* Make a 3-bit mask with the high-order bit being the | |
5157 | value for `>', the next for '=', and the low for '<'. */ | |
5158 | switch ((integer_onep (high_result) * 4) | |
5159 | + (integer_onep (equal_result) * 2) | |
5160 | + integer_onep (low_result)) | |
5161 | { | |
5162 | case 0: | |
5163 | /* Always false. */ | |
88d56342 | 5164 | return omit_one_operand (type, integer_zero_node, arg0); |
e233264a | 5165 | case 1: |
5166 | code = LT_EXPR; | |
5167 | break; | |
5168 | case 2: | |
5169 | code = EQ_EXPR; | |
5170 | break; | |
5171 | case 3: | |
5172 | code = LE_EXPR; | |
5173 | break; | |
5174 | case 4: | |
5175 | code = GT_EXPR; | |
5176 | break; | |
5177 | case 5: | |
5178 | code = NE_EXPR; | |
5179 | break; | |
5180 | case 6: | |
5181 | code = GE_EXPR; | |
5182 | break; | |
5183 | case 7: | |
5184 | /* Always true. */ | |
88d56342 | 5185 | return omit_one_operand (type, integer_one_node, arg0); |
e233264a | 5186 | } |
5187 | ||
d0314131 | 5188 | t = build (code, type, cval1, cval2); |
5189 | if (save_p) | |
5190 | return save_expr (t); | |
5191 | else | |
5192 | return fold (t); | |
e233264a | 5193 | } |
5194 | } | |
5195 | } | |
5196 | ||
5197 | /* If this is a comparison of a field, we may be able to simplify it. */ | |
5198 | if ((TREE_CODE (arg0) == COMPONENT_REF | |
12ec0a8a | 5199 | || TREE_CODE (arg0) == BIT_FIELD_REF) |
5200 | && (code == EQ_EXPR || code == NE_EXPR) | |
5201 | /* Handle the constant case even without -O | |
5202 | to make sure the warnings are given. */ | |
5203 | && (optimize || TREE_CODE (arg1) == INTEGER_CST)) | |
e233264a | 5204 | { |
5205 | t1 = optimize_bit_field_compare (code, type, arg0, arg1); | |
5206 | return t1 ? t1 : t; | |
5207 | } | |
5208 | ||
a77cc7ac | 5209 | /* If this is a comparison of complex values and either or both |
5210 | sizes are a COMPLEX_EXPR, it is best to split up the comparisons | |
5211 | and join them with a TRUTH_ANDIF_EXPR or TRUTH_ORIF_EXPR. This | |
5212 | may prevent needless evaluations. */ | |
5213 | if ((code == EQ_EXPR || code == NE_EXPR) | |
5214 | && TREE_CODE (TREE_TYPE (arg0)) == COMPLEX_TYPE | |
5215 | && (TREE_CODE (arg0) == COMPLEX_EXPR | |
5216 | || TREE_CODE (arg1) == COMPLEX_EXPR)) | |
5217 | { | |
5218 | tree subtype = TREE_TYPE (TREE_TYPE (arg0)); | |
5219 | tree real0 = fold (build1 (REALPART_EXPR, subtype, arg0)); | |
5220 | tree imag0 = fold (build1 (IMAGPART_EXPR, subtype, arg0)); | |
5221 | tree real1 = fold (build1 (REALPART_EXPR, subtype, arg1)); | |
5222 | tree imag1 = fold (build1 (IMAGPART_EXPR, subtype, arg1)); | |
5223 | ||
5224 | return fold (build ((code == EQ_EXPR ? TRUTH_ANDIF_EXPR | |
5225 | : TRUTH_ORIF_EXPR), | |
5226 | type, | |
5227 | fold (build (code, type, real0, real1)), | |
5228 | fold (build (code, type, imag0, imag1)))); | |
5229 | } | |
5230 | ||
e233264a | 5231 | /* From here on, the only cases we handle are when the result is |
5232 | known to be a constant. | |
5233 | ||
5234 | To compute GT, swap the arguments and do LT. | |
2bc77e10 | 5235 | To compute GE, do LT and invert the result. |
5236 | To compute LE, swap the arguments, do LT and invert the result. | |
e233264a | 5237 | To compute NE, do EQ and invert the result. |
5238 | ||
5239 | Therefore, the code below must handle only EQ and LT. */ | |
5240 | ||
2bc77e10 | 5241 | if (code == LE_EXPR || code == GT_EXPR) |
5242 | { | |
e233264a | 5243 | tem = arg0, arg0 = arg1, arg1 = tem; |
5244 | code = swap_tree_comparison (code); | |
5245 | } | |
5246 | ||
5247 | /* Note that it is safe to invert for real values here because we | |
5248 | will check below in the one case that it matters. */ | |
5249 | ||
5250 | invert = 0; | |
5251 | if (code == NE_EXPR || code == GE_EXPR) | |
5252 | { | |
5253 | invert = 1; | |
5254 | code = invert_tree_comparison (code); | |
2bc77e10 | 5255 | } |
5256 | ||
5257 | /* Compute a result for LT or EQ if args permit; | |
5258 | otherwise return T. */ | |
e233264a | 5259 | if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST) |
2bc77e10 | 5260 | { |
e233264a | 5261 | if (code == EQ_EXPR) |
5262 | t1 = build_int_2 ((TREE_INT_CST_LOW (arg0) | |
5263 | == TREE_INT_CST_LOW (arg1)) | |
5264 | && (TREE_INT_CST_HIGH (arg0) | |
5265 | == TREE_INT_CST_HIGH (arg1)), | |
5266 | 0); | |
2bc77e10 | 5267 | else |
e233264a | 5268 | t1 = build_int_2 ((TREE_UNSIGNED (TREE_TYPE (arg0)) |
5269 | ? INT_CST_LT_UNSIGNED (arg0, arg1) | |
5270 | : INT_CST_LT (arg0, arg1)), | |
5271 | 0); | |
2bc77e10 | 5272 | } |
e233264a | 5273 | |
2bc77e10 | 5274 | /* Assume a nonexplicit constant cannot equal an explicit one, |
5275 | since such code would be undefined anyway. | |
5276 | Exception: on sysvr4, using #pragma weak, | |
5277 | a label can come out as 0. */ | |
5278 | else if (TREE_CODE (arg1) == INTEGER_CST | |
5279 | && !integer_zerop (arg1) | |
5280 | && TREE_CONSTANT (arg0) | |
5281 | && TREE_CODE (arg0) == ADDR_EXPR | |
e233264a | 5282 | && code == EQ_EXPR) |
5283 | t1 = build_int_2 (0, 0); | |
5284 | ||
2bc77e10 | 5285 | /* Two real constants can be compared explicitly. */ |
e233264a | 5286 | else if (TREE_CODE (arg0) == REAL_CST && TREE_CODE (arg1) == REAL_CST) |
2bc77e10 | 5287 | { |
e233264a | 5288 | /* If either operand is a NaN, the result is false with two |
5289 | exceptions: First, an NE_EXPR is true on NaNs, but that case | |
5290 | is already handled correctly since we will be inverting the | |
5291 | result for NE_EXPR. Second, if we had inverted a LE_EXPR | |
5292 | or a GE_EXPR into a LT_EXPR, we must return true so that it | |
5293 | will be inverted into false. */ | |
5294 | ||
5295 | if (REAL_VALUE_ISNAN (TREE_REAL_CST (arg0)) | |
5296 | || REAL_VALUE_ISNAN (TREE_REAL_CST (arg1))) | |
5297 | t1 = build_int_2 (invert && code == LT_EXPR, 0); | |
5298 | ||
5299 | else if (code == EQ_EXPR) | |
5300 | t1 = build_int_2 (REAL_VALUES_EQUAL (TREE_REAL_CST (arg0), | |
5301 | TREE_REAL_CST (arg1)), | |
5302 | 0); | |
2bc77e10 | 5303 | else |
e233264a | 5304 | t1 = build_int_2 (REAL_VALUES_LESS (TREE_REAL_CST (arg0), |
5305 | TREE_REAL_CST (arg1)), | |
5306 | 0); | |
2bc77e10 | 5307 | } |
5308 | ||
e233264a | 5309 | if (t1 == NULL_TREE) |
5310 | return t; | |
5311 | ||
5312 | if (invert) | |
5313 | TREE_INT_CST_LOW (t1) ^= 1; | |
5314 | ||
5315 | TREE_TYPE (t1) = type; | |
5316 | return t1; | |
2bc77e10 | 5317 | |
5318 | case COND_EXPR: | |
56753054 | 5319 | /* Pedantic ANSI C says that a conditional expression is never an lvalue, |
5320 | so all simple results must be passed through pedantic_non_lvalue. */ | |
2bc77e10 | 5321 | if (TREE_CODE (arg0) == INTEGER_CST) |
56753054 | 5322 | return pedantic_non_lvalue |
5323 | (TREE_OPERAND (t, (integer_zerop (arg0) ? 2 : 1))); | |
2bc77e10 | 5324 | else if (operand_equal_p (arg1, TREE_OPERAND (expr, 2), 0)) |
6df5edfa | 5325 | return pedantic_omit_one_operand (type, arg1, arg0); |
2bc77e10 | 5326 | |
e233264a | 5327 | /* If the second operand is zero, invert the comparison and swap |
5328 | the second and third operands. Likewise if the second operand | |
5329 | is constant and the third is not or if the third operand is | |
5330 | equivalent to the first operand of the comparison. */ | |
2bc77e10 | 5331 | |
e233264a | 5332 | if (integer_zerop (arg1) |
5333 | || (TREE_CONSTANT (arg1) && ! TREE_CONSTANT (TREE_OPERAND (t, 2))) | |
5334 | || (TREE_CODE_CLASS (TREE_CODE (arg0)) == '<' | |
5335 | && operand_equal_for_comparison_p (TREE_OPERAND (arg0, 0), | |
5336 | TREE_OPERAND (t, 2), | |
5337 | TREE_OPERAND (arg0, 1)))) | |
5338 | { | |
5339 | /* See if this can be inverted. If it can't, possibly because | |
5340 | it was a floating-point inequality comparison, don't do | |
5341 | anything. */ | |
5342 | tem = invert_truthvalue (arg0); | |
2bc77e10 | 5343 | |
e233264a | 5344 | if (TREE_CODE (tem) != TRUTH_NOT_EXPR) |
5345 | { | |
f13f9c7f | 5346 | t = build (code, type, tem, |
5347 | TREE_OPERAND (t, 2), TREE_OPERAND (t, 1)); | |
5348 | arg0 = tem; | |
5923aeca | 5349 | arg1 = TREE_OPERAND (t, 2); |
5923aeca | 5350 | STRIP_NOPS (arg1); |
e233264a | 5351 | } |
5352 | } | |
2bc77e10 | 5353 | |
e233264a | 5354 | /* If we have A op B ? A : C, we may be able to convert this to a |
5355 | simpler expression, depending on the operation and the values | |
48c8fc17 | 5356 | of B and C. IEEE floating point prevents this though, |
5357 | because A or B might be -0.0 or a NaN. */ | |
2bc77e10 | 5358 | |
e233264a | 5359 | if (TREE_CODE_CLASS (TREE_CODE (arg0)) == '<' |
48c8fc17 | 5360 | && (TARGET_FLOAT_FORMAT != IEEE_FLOAT_FORMAT |
8045c7c3 | 5361 | || ! FLOAT_TYPE_P (TREE_TYPE (TREE_OPERAND (arg0, 0))) |
5362 | || flag_fast_math) | |
e233264a | 5363 | && operand_equal_for_comparison_p (TREE_OPERAND (arg0, 0), |
5364 | arg1, TREE_OPERAND (arg0, 1))) | |
2bc77e10 | 5365 | { |
e233264a | 5366 | tree arg2 = TREE_OPERAND (t, 2); |
5367 | enum tree_code comp_code = TREE_CODE (arg0); | |
5368 | ||
5923aeca | 5369 | STRIP_NOPS (arg2); |
5370 | ||
e233264a | 5371 | /* If we have A op 0 ? A : -A, this is A, -A, abs (A), or abs (-A), |
5372 | depending on the comparison operation. */ | |
bdb341a3 | 5373 | if ((FLOAT_TYPE_P (TREE_TYPE (TREE_OPERAND (arg0, 1))) |
5374 | ? real_zerop (TREE_OPERAND (arg0, 1)) | |
5375 | : integer_zerop (TREE_OPERAND (arg0, 1))) | |
e233264a | 5376 | && TREE_CODE (arg2) == NEGATE_EXPR |
5377 | && operand_equal_p (TREE_OPERAND (arg2, 0), arg1, 0)) | |
5378 | switch (comp_code) | |
5379 | { | |
5380 | case EQ_EXPR: | |
56753054 | 5381 | return pedantic_non_lvalue |
5382 | (fold (build1 (NEGATE_EXPR, type, arg1))); | |
e233264a | 5383 | case NE_EXPR: |
56753054 | 5384 | return pedantic_non_lvalue (convert (type, arg1)); |
e233264a | 5385 | case GE_EXPR: |
5386 | case GT_EXPR: | |
56753054 | 5387 | return pedantic_non_lvalue |
573bdf94 | 5388 | (convert (type, fold (build1 (ABS_EXPR, |
5389 | TREE_TYPE (arg1), arg1)))); | |
e233264a | 5390 | case LE_EXPR: |
5391 | case LT_EXPR: | |
56753054 | 5392 | return pedantic_non_lvalue |
5393 | (fold (build1 (NEGATE_EXPR, type, | |
573bdf94 | 5394 | convert (type, |
5395 | fold (build1 (ABS_EXPR, | |
5396 | TREE_TYPE (arg1), | |
5397 | arg1)))))); | |
e233264a | 5398 | } |
2bc77e10 | 5399 | |
e233264a | 5400 | /* If this is A != 0 ? A : 0, this is simply A. For ==, it is |
5401 | always zero. */ | |
2bc77e10 | 5402 | |
13cf6a4c | 5403 | if (integer_zerop (TREE_OPERAND (arg0, 1)) && integer_zerop (arg2)) |
e233264a | 5404 | { |
5405 | if (comp_code == NE_EXPR) | |
56753054 | 5406 | return pedantic_non_lvalue (convert (type, arg1)); |
e233264a | 5407 | else if (comp_code == EQ_EXPR) |
56753054 | 5408 | return pedantic_non_lvalue (convert (type, integer_zero_node)); |
e233264a | 5409 | } |
5410 | ||
5411 | /* If this is A op B ? A : B, this is either A, B, min (A, B), | |
5412 | or max (A, B), depending on the operation. */ | |
5413 | ||
5414 | if (operand_equal_for_comparison_p (TREE_OPERAND (arg0, 1), | |
5415 | arg2, TREE_OPERAND (arg0, 0))) | |
5923aeca | 5416 | { |
5417 | tree comp_op0 = TREE_OPERAND (arg0, 0); | |
5418 | tree comp_op1 = TREE_OPERAND (arg0, 1); | |
5419 | tree comp_type = TREE_TYPE (comp_op0); | |
5420 | ||
5421 | switch (comp_code) | |
5422 | { | |
5423 | case EQ_EXPR: | |
5424 | return pedantic_non_lvalue (convert (type, arg2)); | |
5425 | case NE_EXPR: | |
5426 | return pedantic_non_lvalue (convert (type, arg1)); | |
5427 | case LE_EXPR: | |
5428 | case LT_EXPR: | |
5429 | return pedantic_non_lvalue | |
5430 | (convert (type, (fold (build (MIN_EXPR, comp_type, | |
5431 | comp_op0, comp_op1))))); | |
5432 | case GE_EXPR: | |
5433 | case GT_EXPR: | |
5434 | return pedantic_non_lvalue | |
5435 | (convert (type, fold (build (MAX_EXPR, comp_type, | |
5436 | comp_op0, comp_op1)))); | |
5437 | } | |
5438 | } | |
e233264a | 5439 | |
5440 | /* If this is A op C1 ? A : C2 with C1 and C2 constant integers, | |
5441 | we might still be able to simplify this. For example, | |
5442 | if C1 is one less or one more than C2, this might have started | |
85761785 | 5443 | out as a MIN or MAX and been transformed by this function. |
780a4395 | 5444 | Only good for INTEGER_TYPEs, because we need TYPE_MAX_VALUE. */ |
e233264a | 5445 | |
780a4395 | 5446 | if (INTEGRAL_TYPE_P (type) |
85761785 | 5447 | && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST |
e233264a | 5448 | && TREE_CODE (arg2) == INTEGER_CST) |
5449 | switch (comp_code) | |
5450 | { | |
5451 | case EQ_EXPR: | |
5452 | /* We can replace A with C1 in this case. */ | |
f13f9c7f | 5453 | arg1 = convert (type, TREE_OPERAND (arg0, 1)); |
5454 | t = build (code, type, TREE_OPERAND (t, 0), arg1, | |
5455 | TREE_OPERAND (t, 2)); | |
e233264a | 5456 | break; |
5457 | ||
5458 | case LT_EXPR: | |
5459 | /* If C1 is C2 + 1, this is min(A, C2). */ | |
5460 | if (! operand_equal_p (arg2, TYPE_MAX_VALUE (type), 1) | |
5461 | && operand_equal_p (TREE_OPERAND (arg0, 1), | |
5462 | const_binop (PLUS_EXPR, arg2, | |
5485823f | 5463 | integer_one_node, 0), 1)) |
56753054 | 5464 | return pedantic_non_lvalue |
5465 | (fold (build (MIN_EXPR, type, arg1, arg2))); | |
e233264a | 5466 | break; |
5467 | ||
5468 | case LE_EXPR: | |
5469 | /* If C1 is C2 - 1, this is min(A, C2). */ | |
5470 | if (! operand_equal_p (arg2, TYPE_MIN_VALUE (type), 1) | |
5471 | && operand_equal_p (TREE_OPERAND (arg0, 1), | |
5472 | const_binop (MINUS_EXPR, arg2, | |
5485823f | 5473 | integer_one_node, 0), 1)) |
56753054 | 5474 | return pedantic_non_lvalue |
5475 | (fold (build (MIN_EXPR, type, arg1, arg2))); | |
e233264a | 5476 | break; |
5477 | ||
5478 | case GT_EXPR: | |
5479 | /* If C1 is C2 - 1, this is max(A, C2). */ | |
5480 | if (! operand_equal_p (arg2, TYPE_MIN_VALUE (type), 1) | |
5481 | && operand_equal_p (TREE_OPERAND (arg0, 1), | |
5482 | const_binop (MINUS_EXPR, arg2, | |
5485823f | 5483 | integer_one_node, 0), 1)) |
56753054 | 5484 | return pedantic_non_lvalue |
5485 | (fold (build (MAX_EXPR, type, arg1, arg2))); | |
e233264a | 5486 | break; |
5487 | ||
5488 | case GE_EXPR: | |
5489 | /* If C1 is C2 + 1, this is max(A, C2). */ | |
5490 | if (! operand_equal_p (arg2, TYPE_MAX_VALUE (type), 1) | |
5491 | && operand_equal_p (TREE_OPERAND (arg0, 1), | |
5492 | const_binop (PLUS_EXPR, arg2, | |
5485823f | 5493 | integer_one_node, 0), 1)) |
56753054 | 5494 | return pedantic_non_lvalue |
5495 | (fold (build (MAX_EXPR, type, arg1, arg2))); | |
e233264a | 5496 | break; |
5497 | } | |
2bc77e10 | 5498 | } |
5499 | ||
2483911d | 5500 | /* If the second operand is simpler than the third, swap them |
5501 | since that produces better jump optimization results. */ | |
5502 | if ((TREE_CONSTANT (arg1) || TREE_CODE_CLASS (TREE_CODE (arg1)) == 'd' | |
5503 | || TREE_CODE (arg1) == SAVE_EXPR) | |
5504 | && ! (TREE_CONSTANT (TREE_OPERAND (t, 2)) | |
5505 | || TREE_CODE_CLASS (TREE_CODE (TREE_OPERAND (t, 2))) == 'd' | |
5506 | || TREE_CODE (TREE_OPERAND (t, 2)) == SAVE_EXPR)) | |
5507 | { | |
5508 | /* See if this can be inverted. If it can't, possibly because | |
5509 | it was a floating-point inequality comparison, don't do | |
5510 | anything. */ | |
5511 | tem = invert_truthvalue (arg0); | |
5512 | ||
5513 | if (TREE_CODE (tem) != TRUTH_NOT_EXPR) | |
5514 | { | |
f13f9c7f | 5515 | t = build (code, type, tem, |
5516 | TREE_OPERAND (t, 2), TREE_OPERAND (t, 1)); | |
5517 | arg0 = tem; | |
5923aeca | 5518 | arg1 = TREE_OPERAND (t, 2); |
5923aeca | 5519 | STRIP_NOPS (arg1); |
2483911d | 5520 | } |
5521 | } | |
5522 | ||
e233264a | 5523 | /* Convert A ? 1 : 0 to simply A. */ |
5524 | if (integer_onep (TREE_OPERAND (t, 1)) | |
5525 | && integer_zerop (TREE_OPERAND (t, 2)) | |
5526 | /* If we try to convert TREE_OPERAND (t, 0) to our type, the | |
5527 | call to fold will try to move the conversion inside | |
5528 | a COND, which will recurse. In that case, the COND_EXPR | |
5529 | is probably the best choice, so leave it alone. */ | |
5530 | && type == TREE_TYPE (arg0)) | |
56753054 | 5531 | return pedantic_non_lvalue (arg0); |
2bc77e10 | 5532 | |
e233264a | 5533 | /* Look for expressions of the form A & 2 ? 2 : 0. The result of this |
5534 | operation is simply A & 2. */ | |
2bc77e10 | 5535 | |
5536 | if (integer_zerop (TREE_OPERAND (t, 2)) | |
5537 | && TREE_CODE (arg0) == NE_EXPR | |
5538 | && integer_zerop (TREE_OPERAND (arg0, 1)) | |
e233264a | 5539 | && integer_pow2p (arg1) |
5540 | && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_AND_EXPR | |
5541 | && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1), | |
5542 | arg1, 1)) | |
56753054 | 5543 | return pedantic_non_lvalue (convert (type, TREE_OPERAND (arg0, 0))); |
2bc77e10 | 5544 | |
2bc77e10 | 5545 | return t; |
5546 | ||
5547 | case COMPOUND_EXPR: | |
b468bbc6 | 5548 | /* When pedantic, a compound expression can be neither an lvalue |
5549 | nor an integer constant expression. */ | |
5550 | if (TREE_SIDE_EFFECTS (arg0) || pedantic) | |
c3ce5d04 | 5551 | return t; |
5552 | /* Don't let (0, 0) be null pointer constant. */ | |
5553 | if (integer_zerop (arg1)) | |
5554 | return non_lvalue (arg1); | |
5555 | return arg1; | |
2bc77e10 | 5556 | |
bb6b5123 | 5557 | case COMPLEX_EXPR: |
5558 | if (wins) | |
5b2ade4d | 5559 | return build_complex (type, arg0, arg1); |
bb6b5123 | 5560 | return t; |
5561 | ||
5562 | case REALPART_EXPR: | |
27395c25 | 5563 | if (TREE_CODE (TREE_TYPE (arg0)) != COMPLEX_TYPE) |
bb6b5123 | 5564 | return t; |
5565 | else if (TREE_CODE (arg0) == COMPLEX_EXPR) | |
5566 | return omit_one_operand (type, TREE_OPERAND (arg0, 0), | |
5567 | TREE_OPERAND (arg0, 1)); | |
5568 | else if (TREE_CODE (arg0) == COMPLEX_CST) | |
5569 | return TREE_REALPART (arg0); | |
5570 | else if (TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR) | |
a1397da2 | 5571 | return fold (build (TREE_CODE (arg0), type, |
5572 | fold (build1 (REALPART_EXPR, type, | |
5573 | TREE_OPERAND (arg0, 0))), | |
5574 | fold (build1 (REALPART_EXPR, | |
5575 | type, TREE_OPERAND (arg0, 1))))); | |
bb6b5123 | 5576 | return t; |
5577 | ||
5578 | case IMAGPART_EXPR: | |
27395c25 | 5579 | if (TREE_CODE (TREE_TYPE (arg0)) != COMPLEX_TYPE) |
bb6b5123 | 5580 | return convert (type, integer_zero_node); |
5581 | else if (TREE_CODE (arg0) == COMPLEX_EXPR) | |
5582 | return omit_one_operand (type, TREE_OPERAND (arg0, 1), | |
5583 | TREE_OPERAND (arg0, 0)); | |
5584 | else if (TREE_CODE (arg0) == COMPLEX_CST) | |
5585 | return TREE_IMAGPART (arg0); | |
5586 | else if (TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR) | |
a1397da2 | 5587 | return fold (build (TREE_CODE (arg0), type, |
5588 | fold (build1 (IMAGPART_EXPR, type, | |
5589 | TREE_OPERAND (arg0, 0))), | |
5590 | fold (build1 (IMAGPART_EXPR, type, | |
5591 | TREE_OPERAND (arg0, 1))))); | |
bb6b5123 | 5592 | return t; |
5593 | ||
49f9d16d | 5594 | /* Pull arithmetic ops out of the CLEANUP_POINT_EXPR where |
5595 | appropriate. */ | |
5596 | case CLEANUP_POINT_EXPR: | |
80d31816 | 5597 | if (! TREE_SIDE_EFFECTS (arg0)) |
47853bac | 5598 | return TREE_OPERAND (t, 0); |
49f9d16d | 5599 | |
5600 | { | |
5601 | enum tree_code code0 = TREE_CODE (arg0); | |
5602 | int kind0 = TREE_CODE_CLASS (code0); | |
5603 | tree arg00 = TREE_OPERAND (arg0, 0); | |
5604 | tree arg01; | |
5605 | ||
154e6f12 | 5606 | if (kind0 == '1' || code0 == TRUTH_NOT_EXPR) |
49f9d16d | 5607 | return fold (build1 (code0, type, |
5608 | fold (build1 (CLEANUP_POINT_EXPR, | |
5609 | TREE_TYPE (arg00), arg00)))); | |
154e6f12 | 5610 | |
5611 | if (kind0 == '<' || kind0 == '2' | |
5612 | || code0 == TRUTH_ANDIF_EXPR || code0 == TRUTH_ORIF_EXPR | |
5613 | || code0 == TRUTH_AND_EXPR || code0 == TRUTH_OR_EXPR | |
5614 | || code0 == TRUTH_XOR_EXPR) | |
5615 | { | |
5616 | arg01 = TREE_OPERAND (arg0, 1); | |
5617 | ||
5618 | if (! TREE_SIDE_EFFECTS (arg00)) | |
5619 | return fold (build (code0, type, arg00, | |
5620 | fold (build1 (CLEANUP_POINT_EXPR, | |
5621 | TREE_TYPE (arg01), arg01)))); | |
5622 | ||
5623 | if (! TREE_SIDE_EFFECTS (arg01)) | |
5624 | return fold (build (code0, type, | |
5625 | fold (build1 (CLEANUP_POINT_EXPR, | |
5626 | TREE_TYPE (arg00), arg00)), | |
5627 | arg01)); | |
5628 | } | |
49f9d16d | 5629 | |
5630 | return t; | |
5631 | } | |
5632 | ||
2bc77e10 | 5633 | default: |
5634 | return t; | |
5635 | } /* switch (code) */ | |
5636 | } |